Skip to content
TargetLoweringObjectFileImpl.cpp 41.5 KiB
Newer Older
//===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements classes used to handle lowerings specific to common
// object file formats.
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/GlobalVariable.h"
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSectionCOFF.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Target/Mangler.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
using namespace llvm;

//===----------------------------------------------------------------------===//
//                                  ELF
//===----------------------------------------------------------------------===//

void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
                                             const TargetMachine &TM) {
  TargetLoweringObjectFile::Initialize(Ctx, TM);

  BSSSection =
    getContext().getELFSection(".bss", MCSectionELF::SHT_NOBITS,
                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
                               SectionKind::getBSS());
    getContext().getELFSection(".text", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_EXECINSTR |
                               MCSectionELF::SHF_ALLOC,
                               SectionKind::getText());
    getContext().getELFSection(".data", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
                               SectionKind::getDataRel());
    getContext().getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC,
                               SectionKind::getReadOnly());
    getContext().getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
                               MCSectionELF::SHF_WRITE,
                               SectionKind::getThreadData());
    getContext().getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
                               MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
                               MCSectionELF::SHF_WRITE,
                               SectionKind::getThreadBSS());
    getContext().getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
                               SectionKind::getDataRel());
    getContext().getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
                               SectionKind::getDataRelLocal());
    getContext().getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
                               SectionKind::getReadOnlyWithRel());
    getContext().getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
                               SectionKind::getReadOnlyWithRelLocal());
    getContext().getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
                               SectionKind::getMergeableConst4());
    getContext().getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
                               SectionKind::getMergeableConst8());
    getContext().getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_MERGE,
                               SectionKind::getMergeableConst16());
    getContext().getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
                               SectionKind::getDataRel());
    getContext().getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
                               SectionKind::getDataRel());

  // Exception Handling Sections.

  // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
  // it contains relocatable pointers.  In PIC mode, this is probably a big
  // runtime hit for C++ apps.  Either the contents of the LSDA need to be
  // adjusted or this should be a data section.
  LSDASection =
    getContext().getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC,
                               SectionKind::getReadOnly());
    getContext().getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
                               MCSectionELF::SHF_ALLOC |MCSectionELF::SHF_WRITE,
                               SectionKind::getDataRel());
    getContext().getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
    getContext().getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
                               SectionKind::getMetadata());
}


static SectionKind
getELFKindForNamedSection(StringRef Name, SectionKind K) {
  if (Name.empty() || Name[0] != '.') return K;

  // Some lame default implementation based on some magic section names.
  if (Name == ".bss" ||
      Name.startswith(".bss.") ||
      Name.startswith(".gnu.linkonce.b.") ||
      Name.startswith(".llvm.linkonce.b.") ||
      Name == ".sbss" ||
      Name.startswith(".sbss.") ||
      Name.startswith(".gnu.linkonce.sb.") ||
      Name.startswith(".llvm.linkonce.sb."))
    return SectionKind::getBSS();

  if (Name == ".tdata" ||
      Name.startswith(".tdata.") ||
      Name.startswith(".gnu.linkonce.td.") ||
      Name.startswith(".llvm.linkonce.td."))
    return SectionKind::getThreadData();

  if (Name == ".tbss" ||
      Name.startswith(".tbss.") ||
      Name.startswith(".gnu.linkonce.tb.") ||
Loading
Loading full blame...