Home | History | Annotate | Download | only in XCore
      1 //===-- XCoreTargetObjectFile.cpp - XCore object files --------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "XCoreTargetObjectFile.h"
     11 #include "XCoreSubtarget.h"
     12 #include "llvm/IR/DataLayout.h"
     13 #include "llvm/MC/MCContext.h"
     14 #include "llvm/MC/MCSectionELF.h"
     15 #include "llvm/Support/ELF.h"
     16 #include "llvm/Target/TargetMachine.h"
     17 
     18 using namespace llvm;
     19 
     20 
     21 void XCoreTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
     22   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
     23 
     24   BSSSection = Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS,
     25                                  ELF::SHF_ALLOC | ELF::SHF_WRITE |
     26                                      ELF::XCORE_SHF_DP_SECTION);
     27   BSSSectionLarge = Ctx.getELFSection(".dp.bss.large", ELF::SHT_NOBITS,
     28                                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
     29                                           ELF::XCORE_SHF_DP_SECTION);
     30   DataSection = Ctx.getELFSection(".dp.data", ELF::SHT_PROGBITS,
     31                                   ELF::SHF_ALLOC | ELF::SHF_WRITE |
     32                                       ELF::XCORE_SHF_DP_SECTION);
     33   DataSectionLarge = Ctx.getELFSection(".dp.data.large", ELF::SHT_PROGBITS,
     34                                        ELF::SHF_ALLOC | ELF::SHF_WRITE |
     35                                            ELF::XCORE_SHF_DP_SECTION);
     36   DataRelROSection = Ctx.getELFSection(".dp.rodata", ELF::SHT_PROGBITS,
     37                                        ELF::SHF_ALLOC | ELF::SHF_WRITE |
     38                                            ELF::XCORE_SHF_DP_SECTION);
     39   DataRelROSectionLarge = Ctx.getELFSection(
     40       ".dp.rodata.large", ELF::SHT_PROGBITS,
     41       ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::XCORE_SHF_DP_SECTION);
     42   ReadOnlySection =
     43       Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
     44                         ELF::SHF_ALLOC | ELF::XCORE_SHF_CP_SECTION);
     45   ReadOnlySectionLarge =
     46       Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
     47                         ELF::SHF_ALLOC | ELF::XCORE_SHF_CP_SECTION);
     48   MergeableConst4Section = Ctx.getELFSection(
     49       ".cp.rodata.cst4", ELF::SHT_PROGBITS,
     50       ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::XCORE_SHF_CP_SECTION, 4, "");
     51   MergeableConst8Section = Ctx.getELFSection(
     52       ".cp.rodata.cst8", ELF::SHT_PROGBITS,
     53       ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::XCORE_SHF_CP_SECTION, 8, "");
     54   MergeableConst16Section = Ctx.getELFSection(
     55       ".cp.rodata.cst16", ELF::SHT_PROGBITS,
     56       ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::XCORE_SHF_CP_SECTION, 16, "");
     57   CStringSection =
     58       Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS,
     59                         ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS |
     60                             ELF::XCORE_SHF_CP_SECTION);
     61   // TextSection       - see MObjectFileInfo.cpp
     62   // StaticCtorSection - see MObjectFileInfo.cpp
     63   // StaticDtorSection - see MObjectFileInfo.cpp
     64  }
     65 
     66 static unsigned getXCoreSectionType(SectionKind K) {
     67   if (K.isBSS())
     68     return ELF::SHT_NOBITS;
     69   return ELF::SHT_PROGBITS;
     70 }
     71 
     72 static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) {
     73   unsigned Flags = 0;
     74 
     75   if (!K.isMetadata())
     76     Flags |= ELF::SHF_ALLOC;
     77 
     78   if (K.isText())
     79     Flags |= ELF::SHF_EXECINSTR;
     80   else if (IsCPRel)
     81     Flags |= ELF::XCORE_SHF_CP_SECTION;
     82   else
     83     Flags |= ELF::XCORE_SHF_DP_SECTION;
     84 
     85   if (K.isWriteable())
     86     Flags |= ELF::SHF_WRITE;
     87 
     88   if (K.isMergeableCString() || K.isMergeableConst4() ||
     89       K.isMergeableConst8() || K.isMergeableConst16())
     90     Flags |= ELF::SHF_MERGE;
     91 
     92   if (K.isMergeableCString())
     93     Flags |= ELF::SHF_STRINGS;
     94 
     95   return Flags;
     96 }
     97 
     98 MCSection *
     99 XCoreTargetObjectFile::getExplicitSectionGlobal(const GlobalValue *GV,
    100                                                 SectionKind Kind, Mangler &Mang,
    101                                                 const TargetMachine &TM) const {
    102   StringRef SectionName = GV->getSection();
    103   // Infer section flags from the section name if we can.
    104   bool IsCPRel = SectionName.startswith(".cp.");
    105   if (IsCPRel && !Kind.isReadOnly())
    106     report_fatal_error("Using .cp. section for writeable object.");
    107   return getContext().getELFSection(SectionName, getXCoreSectionType(Kind),
    108                                     getXCoreSectionFlags(Kind, IsCPRel));
    109 }
    110 
    111 MCSection *
    112 XCoreTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
    113                                               SectionKind Kind, Mangler &Mang,
    114                                               const TargetMachine &TM) const {
    115 
    116   bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
    117 
    118   if (Kind.isText())                    return TextSection;
    119   if (UseCPRel) {
    120     if (Kind.isMergeable1ByteCString()) return CStringSection;
    121     if (Kind.isMergeableConst4())       return MergeableConst4Section;
    122     if (Kind.isMergeableConst8())       return MergeableConst8Section;
    123     if (Kind.isMergeableConst16())      return MergeableConst16Section;
    124   }
    125   Type *ObjType = GV->getValueType();
    126   auto &DL = GV->getParent()->getDataLayout();
    127   if (TM.getCodeModel() == CodeModel::Small || !ObjType->isSized() ||
    128       DL.getTypeAllocSize(ObjType) < CodeModelLargeSize) {
    129     if (Kind.isReadOnly())              return UseCPRel? ReadOnlySection
    130                                                        : DataRelROSection;
    131     if (Kind.isBSS() || Kind.isCommon())return BSSSection;
    132     if (Kind.isData())
    133       return DataSection;
    134     if (Kind.isReadOnlyWithRel())       return DataRelROSection;
    135   } else {
    136     if (Kind.isReadOnly())              return UseCPRel? ReadOnlySectionLarge
    137                                                        : DataRelROSectionLarge;
    138     if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
    139     if (Kind.isData())
    140       return DataSectionLarge;
    141     if (Kind.isReadOnlyWithRel())       return DataRelROSectionLarge;
    142   }
    143 
    144   assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
    145   report_fatal_error("Target does not support TLS or Common sections");
    146 }
    147 
    148 MCSection *XCoreTargetObjectFile::getSectionForConstant(const DataLayout &DL,
    149                                                         SectionKind Kind,
    150                                                         const Constant *C,
    151                                                         unsigned &Align) const {
    152   if (Kind.isMergeableConst4())           return MergeableConst4Section;
    153   if (Kind.isMergeableConst8())           return MergeableConst8Section;
    154   if (Kind.isMergeableConst16())          return MergeableConst16Section;
    155   assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) &&
    156          "Unknown section kind");
    157   // We assume the size of the object is never greater than CodeModelLargeSize.
    158   // To handle CodeModelLargeSize changes to AsmPrinter would be required.
    159   return ReadOnlySection;
    160 }
    161