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 = 25 Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS, 26 ELF::SHF_ALLOC | ELF::SHF_WRITE | 27 ELF::XCORE_SHF_DP_SECTION, 28 SectionKind::getBSS()); 29 BSSSectionLarge = 30 Ctx.getELFSection(".dp.bss.large", ELF::SHT_NOBITS, 31 ELF::SHF_ALLOC | ELF::SHF_WRITE | 32 ELF::XCORE_SHF_DP_SECTION, 33 SectionKind::getBSS()); 34 DataSection = 35 Ctx.getELFSection(".dp.data", ELF::SHT_PROGBITS, 36 ELF::SHF_ALLOC | ELF::SHF_WRITE | 37 ELF::XCORE_SHF_DP_SECTION, 38 SectionKind::getDataRel()); 39 DataSectionLarge = 40 Ctx.getELFSection(".dp.data.large", ELF::SHT_PROGBITS, 41 ELF::SHF_ALLOC | ELF::SHF_WRITE | 42 ELF::XCORE_SHF_DP_SECTION, 43 SectionKind::getDataRel()); 44 DataRelROSection = 45 Ctx.getELFSection(".dp.rodata", ELF::SHT_PROGBITS, 46 ELF::SHF_ALLOC | ELF::SHF_WRITE | 47 ELF::XCORE_SHF_DP_SECTION, 48 SectionKind::getReadOnlyWithRel()); 49 DataRelROSectionLarge = 50 Ctx.getELFSection(".dp.rodata.large", ELF::SHT_PROGBITS, 51 ELF::SHF_ALLOC | ELF::SHF_WRITE | 52 ELF::XCORE_SHF_DP_SECTION, 53 SectionKind::getReadOnlyWithRel()); 54 ReadOnlySection = 55 Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS, 56 ELF::SHF_ALLOC | 57 ELF::XCORE_SHF_CP_SECTION, 58 SectionKind::getReadOnlyWithRel()); 59 ReadOnlySectionLarge = 60 Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS, 61 ELF::SHF_ALLOC | 62 ELF::XCORE_SHF_CP_SECTION, 63 SectionKind::getReadOnlyWithRel()); 64 MergeableConst4Section = 65 Ctx.getELFSection(".cp.rodata.cst4", ELF::SHT_PROGBITS, 66 ELF::SHF_ALLOC | ELF::SHF_MERGE | 67 ELF::XCORE_SHF_CP_SECTION, 68 SectionKind::getMergeableConst4()); 69 MergeableConst8Section = 70 Ctx.getELFSection(".cp.rodata.cst8", ELF::SHT_PROGBITS, 71 ELF::SHF_ALLOC | ELF::SHF_MERGE | 72 ELF::XCORE_SHF_CP_SECTION, 73 SectionKind::getMergeableConst8()); 74 MergeableConst16Section = 75 Ctx.getELFSection(".cp.rodata.cst16", ELF::SHT_PROGBITS, 76 ELF::SHF_ALLOC | ELF::SHF_MERGE | 77 ELF::XCORE_SHF_CP_SECTION, 78 SectionKind::getMergeableConst16()); 79 CStringSection = 80 Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS, 81 ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS | 82 ELF::XCORE_SHF_CP_SECTION, 83 SectionKind::getReadOnlyWithRel()); 84 // TextSection - see MObjectFileInfo.cpp 85 // StaticCtorSection - see MObjectFileInfo.cpp 86 // StaticDtorSection - see MObjectFileInfo.cpp 87 } 88 89 static unsigned getXCoreSectionType(SectionKind K) { 90 if (K.isBSS()) 91 return ELF::SHT_NOBITS; 92 return ELF::SHT_PROGBITS; 93 } 94 95 static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) { 96 unsigned Flags = 0; 97 98 if (!K.isMetadata()) 99 Flags |= ELF::SHF_ALLOC; 100 101 if (K.isText()) 102 Flags |= ELF::SHF_EXECINSTR; 103 else if (IsCPRel) 104 Flags |= ELF::XCORE_SHF_CP_SECTION; 105 else 106 Flags |= ELF::XCORE_SHF_DP_SECTION; 107 108 if (K.isWriteable()) 109 Flags |= ELF::SHF_WRITE; 110 111 if (K.isMergeableCString() || K.isMergeableConst4() || 112 K.isMergeableConst8() || K.isMergeableConst16()) 113 Flags |= ELF::SHF_MERGE; 114 115 if (K.isMergeableCString()) 116 Flags |= ELF::SHF_STRINGS; 117 118 return Flags; 119 } 120 121 const MCSection *XCoreTargetObjectFile:: 122 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 123 Mangler &Mang, const TargetMachine &TM) const { 124 StringRef SectionName = GV->getSection(); 125 // Infer section flags from the section name if we can. 126 bool IsCPRel = SectionName.startswith(".cp."); 127 if (IsCPRel && !Kind.isReadOnly()) 128 report_fatal_error("Using .cp. section for writeable object."); 129 return getContext().getELFSection(SectionName, getXCoreSectionType(Kind), 130 getXCoreSectionFlags(Kind, IsCPRel), Kind); 131 } 132 133 const MCSection *XCoreTargetObjectFile:: 134 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 135 const TargetMachine &TM) const{ 136 137 bool UseCPRel = GV->isLocalLinkage(GV->getLinkage()); 138 139 if (Kind.isText()) return TextSection; 140 if (UseCPRel) { 141 if (Kind.isMergeable1ByteCString()) return CStringSection; 142 if (Kind.isMergeableConst4()) return MergeableConst4Section; 143 if (Kind.isMergeableConst8()) return MergeableConst8Section; 144 if (Kind.isMergeableConst16()) return MergeableConst16Section; 145 } 146 Type *ObjType = GV->getType()->getPointerElementType(); 147 if (TM.getCodeModel() == CodeModel::Small || 148 !ObjType->isSized() || 149 TM.getDataLayout()->getTypeAllocSize(ObjType) < CodeModelLargeSize) { 150 if (Kind.isReadOnly()) return UseCPRel? ReadOnlySection 151 : DataRelROSection; 152 if (Kind.isBSS() || Kind.isCommon())return BSSSection; 153 if (Kind.isDataRel()) return DataSection; 154 if (Kind.isReadOnlyWithRel()) return DataRelROSection; 155 } else { 156 if (Kind.isReadOnly()) return UseCPRel? ReadOnlySectionLarge 157 : DataRelROSectionLarge; 158 if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge; 159 if (Kind.isDataRel()) return DataSectionLarge; 160 if (Kind.isReadOnlyWithRel()) return DataRelROSectionLarge; 161 } 162 163 assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind"); 164 report_fatal_error("Target does not support TLS or Common sections"); 165 } 166 167 const MCSection *XCoreTargetObjectFile:: 168 getSectionForConstant(SectionKind Kind) const { 169 if (Kind.isMergeableConst4()) return MergeableConst4Section; 170 if (Kind.isMergeableConst8()) return MergeableConst8Section; 171 if (Kind.isMergeableConst16()) return MergeableConst16Section; 172 assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) && 173 "Unknown section kind"); 174 // We assume the size of the object is never greater than CodeModelLargeSize. 175 // To handle CodeModelLargeSize changes to AsmPrinter would be required. 176 return ReadOnlySection; 177 } 178