1f22ef01cSRoman Divacky //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This file implements classes used to handle lowerings specific to common 11f22ef01cSRoman Divacky // object file formats. 12f22ef01cSRoman Divacky // 13f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 14f22ef01cSRoman Divacky 152cab237bSDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFile.h" 16db17bf38SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 172cab237bSDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 18139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 19139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h" 20139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h" 21139f7f9bSDimitry Andric #include "llvm/IR/Function.h" 22139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h" 2391bc56edSDimitry Andric #include "llvm/IR/Mangler.h" 24f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 25f22ef01cSRoman Divacky #include "llvm/MC/MCExpr.h" 26f22ef01cSRoman Divacky #include "llvm/MC/MCStreamer.h" 27f22ef01cSRoman Divacky #include "llvm/MC/MCSymbol.h" 28f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h" 29f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 30139f7f9bSDimitry Andric #include "llvm/Target/TargetMachine.h" 31139f7f9bSDimitry Andric #include "llvm/Target/TargetOptions.h" 32f22ef01cSRoman Divacky using namespace llvm; 33f22ef01cSRoman Divacky 34f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 35f22ef01cSRoman Divacky // Generic Code 36f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 37f22ef01cSRoman Divacky 386122f3e6SDimitry Andric /// Initialize - this method must be called before any actual lowering is 396122f3e6SDimitry Andric /// done. This specifies the current context for codegen, and gives the 406122f3e6SDimitry Andric /// lowering implementations a chance to set up their default sections. 416122f3e6SDimitry Andric void TargetLoweringObjectFile::Initialize(MCContext &ctx, 426122f3e6SDimitry Andric const TargetMachine &TM) { 436122f3e6SDimitry Andric Ctx = &ctx; 44d88c1a5aSDimitry Andric // `Initialize` can be called more than once. 45f37b6182SDimitry Andric delete Mang; 46d88c1a5aSDimitry Andric Mang = new Mangler(); 472cab237bSDimitry Andric InitMCObjectFileInfo(TM.getTargetTriple(), TM.isPositionIndependent(), *Ctx, 482cab237bSDimitry Andric TM.getCodeModel() == CodeModel::Large); 49f22ef01cSRoman Divacky } 50f22ef01cSRoman Divacky 51f22ef01cSRoman Divacky TargetLoweringObjectFile::~TargetLoweringObjectFile() { 52d88c1a5aSDimitry Andric delete Mang; 53f22ef01cSRoman Divacky } 54f22ef01cSRoman Divacky 55dff0c46cSDimitry Andric static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) { 5617a519f9SDimitry Andric const Constant *C = GV->getInitializer(); 57f22ef01cSRoman Divacky 58f22ef01cSRoman Divacky // Must have zero initializer. 59f22ef01cSRoman Divacky if (!C->isNullValue()) 60f22ef01cSRoman Divacky return false; 61f22ef01cSRoman Divacky 62f22ef01cSRoman Divacky // Leave constant zeros in readonly constant sections, so they can be shared. 63f22ef01cSRoman Divacky if (GV->isConstant()) 64f22ef01cSRoman Divacky return false; 65f22ef01cSRoman Divacky 66f22ef01cSRoman Divacky // If the global has an explicit section specified, don't put it in BSS. 6791bc56edSDimitry Andric if (GV->hasSection()) 68f22ef01cSRoman Divacky return false; 69f22ef01cSRoman Divacky 70f22ef01cSRoman Divacky // If -nozero-initialized-in-bss is specified, don't ever use BSS. 71f22ef01cSRoman Divacky if (NoZerosInBSS) 72f22ef01cSRoman Divacky return false; 73f22ef01cSRoman Divacky 74f22ef01cSRoman Divacky // Otherwise, put it in BSS! 75f22ef01cSRoman Divacky return true; 76f22ef01cSRoman Divacky } 77f22ef01cSRoman Divacky 78f22ef01cSRoman Divacky /// IsNullTerminatedString - Return true if the specified constant (which is 79f22ef01cSRoman Divacky /// known to have a type that is an array of 1/2/4 byte elements) ends with a 80dff0c46cSDimitry Andric /// nul value and contains no other nuls in it. Note that this is more general 81dff0c46cSDimitry Andric /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings. 82f22ef01cSRoman Divacky static bool IsNullTerminatedString(const Constant *C) { 83dff0c46cSDimitry Andric // First check: is we have constant array terminated with zero 84dff0c46cSDimitry Andric if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { 85dff0c46cSDimitry Andric unsigned NumElts = CDS->getNumElements(); 86dff0c46cSDimitry Andric assert(NumElts != 0 && "Can't have an empty CDS"); 87f22ef01cSRoman Divacky 88dff0c46cSDimitry Andric if (CDS->getElementAsInteger(NumElts-1) != 0) 89f22ef01cSRoman Divacky return false; // Not null terminated. 90f22ef01cSRoman Divacky 91f22ef01cSRoman Divacky // Verify that the null doesn't occur anywhere else in the string. 92dff0c46cSDimitry Andric for (unsigned i = 0; i != NumElts-1; ++i) 93dff0c46cSDimitry Andric if (CDS->getElementAsInteger(i) == 0) 94f22ef01cSRoman Divacky return false; 95f22ef01cSRoman Divacky return true; 96f22ef01cSRoman Divacky } 97f22ef01cSRoman Divacky 98f22ef01cSRoman Divacky // Another possibility: [1 x i8] zeroinitializer 99f22ef01cSRoman Divacky if (isa<ConstantAggregateZero>(C)) 100dff0c46cSDimitry Andric return cast<ArrayType>(C->getType())->getNumElements() == 1; 101f22ef01cSRoman Divacky 102f22ef01cSRoman Divacky return false; 103f22ef01cSRoman Divacky } 104f22ef01cSRoman Divacky 10591bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase( 106d88c1a5aSDimitry Andric const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const { 10791bc56edSDimitry Andric assert(!Suffix.empty()); 10891bc56edSDimitry Andric 109f785676fSDimitry Andric SmallString<60> NameStr; 1107d523365SDimitry Andric NameStr += GV->getParent()->getDataLayout().getPrivateGlobalPrefix(); 111d88c1a5aSDimitry Andric TM.getNameWithPrefix(NameStr, GV, *Mang); 11291bc56edSDimitry Andric NameStr.append(Suffix.begin(), Suffix.end()); 113ff0cc061SDimitry Andric return Ctx->getOrCreateSymbol(NameStr); 114f785676fSDimitry Andric } 115f785676fSDimitry Andric 11691bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol( 117d88c1a5aSDimitry Andric const GlobalValue *GV, const TargetMachine &TM, 1183b0f4066SDimitry Andric MachineModuleInfo *MMI) const { 119d88c1a5aSDimitry Andric return TM.getSymbol(GV); 1203b0f4066SDimitry Andric } 1213b0f4066SDimitry Andric 1223b0f4066SDimitry Andric void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer, 1237d523365SDimitry Andric const DataLayout &, 1243b0f4066SDimitry Andric const MCSymbol *Sym) const { 1253b0f4066SDimitry Andric } 1263b0f4066SDimitry Andric 1273b0f4066SDimitry Andric 128f22ef01cSRoman Divacky /// getKindForGlobal - This is a top-level target-independent classifier for 129f22ef01cSRoman Divacky /// a global variable. Given an global variable and information from TM, it 130f22ef01cSRoman Divacky /// classifies the global in a variety of ways that make various target 131f22ef01cSRoman Divacky /// implementations simpler. The target implementation is free to ignore this 132f22ef01cSRoman Divacky /// extra info of course. 133d88c1a5aSDimitry Andric SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO, 134f22ef01cSRoman Divacky const TargetMachine &TM){ 135d88c1a5aSDimitry Andric assert(!GO->isDeclaration() && !GO->hasAvailableExternallyLinkage() && 136f22ef01cSRoman Divacky "Can only be used for global definitions"); 137f22ef01cSRoman Divacky 138f22ef01cSRoman Divacky Reloc::Model ReloModel = TM.getRelocationModel(); 139f22ef01cSRoman Divacky 140f22ef01cSRoman Divacky // Early exit - functions should be always in text sections. 141d88c1a5aSDimitry Andric const auto *GVar = dyn_cast<GlobalVariable>(GO); 14291bc56edSDimitry Andric if (!GVar) 143f22ef01cSRoman Divacky return SectionKind::getText(); 144f22ef01cSRoman Divacky 145f22ef01cSRoman Divacky // Handle thread-local data first. 146f22ef01cSRoman Divacky if (GVar->isThreadLocal()) { 147dff0c46cSDimitry Andric if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) 148f22ef01cSRoman Divacky return SectionKind::getThreadBSS(); 149f22ef01cSRoman Divacky return SectionKind::getThreadData(); 150f22ef01cSRoman Divacky } 151f22ef01cSRoman Divacky 152f22ef01cSRoman Divacky // Variables with common linkage always get classified as common. 153f22ef01cSRoman Divacky if (GVar->hasCommonLinkage()) 154f22ef01cSRoman Divacky return SectionKind::getCommon(); 155f22ef01cSRoman Divacky 156f22ef01cSRoman Divacky // Variable can be easily put to BSS section. 157dff0c46cSDimitry Andric if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) { 158f22ef01cSRoman Divacky if (GVar->hasLocalLinkage()) 159f22ef01cSRoman Divacky return SectionKind::getBSSLocal(); 160f22ef01cSRoman Divacky else if (GVar->hasExternalLinkage()) 161f22ef01cSRoman Divacky return SectionKind::getBSSExtern(); 162f22ef01cSRoman Divacky return SectionKind::getBSS(); 163f22ef01cSRoman Divacky } 164f22ef01cSRoman Divacky 16517a519f9SDimitry Andric const Constant *C = GVar->getInitializer(); 166f22ef01cSRoman Divacky 167f22ef01cSRoman Divacky // If the global is marked constant, we can put it into a mergable section, 168f22ef01cSRoman Divacky // a mergable string section, or general .data if it contains relocations. 169f22ef01cSRoman Divacky if (GVar->isConstant()) { 170f22ef01cSRoman Divacky // If the initializer for the global contains something that requires a 1717ae0e2c9SDimitry Andric // relocation, then we may have to drop this into a writable data section 172f22ef01cSRoman Divacky // even though it is marked const. 1737d523365SDimitry Andric if (!C->needsRelocation()) { 1742754fe60SDimitry Andric // If the global is required to have a unique address, it can't be put 1752754fe60SDimitry Andric // into a mergable section: just drop it into the general read-only 1762754fe60SDimitry Andric // section instead. 1773ca95b02SDimitry Andric if (!GVar->hasGlobalUnnamedAddr()) 1782754fe60SDimitry Andric return SectionKind::getReadOnly(); 1792754fe60SDimitry Andric 180f22ef01cSRoman Divacky // If initializer is a null-terminated string, put it in a "cstring" 181f22ef01cSRoman Divacky // section of the right width. 1826122f3e6SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { 1836122f3e6SDimitry Andric if (IntegerType *ITy = 184f22ef01cSRoman Divacky dyn_cast<IntegerType>(ATy->getElementType())) { 185f22ef01cSRoman Divacky if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || 186f22ef01cSRoman Divacky ITy->getBitWidth() == 32) && 187f22ef01cSRoman Divacky IsNullTerminatedString(C)) { 188f22ef01cSRoman Divacky if (ITy->getBitWidth() == 8) 189f22ef01cSRoman Divacky return SectionKind::getMergeable1ByteCString(); 190f22ef01cSRoman Divacky if (ITy->getBitWidth() == 16) 191f22ef01cSRoman Divacky return SectionKind::getMergeable2ByteCString(); 192f22ef01cSRoman Divacky 193f22ef01cSRoman Divacky assert(ITy->getBitWidth() == 32 && "Unknown width"); 194f22ef01cSRoman Divacky return SectionKind::getMergeable4ByteCString(); 195f22ef01cSRoman Divacky } 196f22ef01cSRoman Divacky } 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky 199f22ef01cSRoman Divacky // Otherwise, just drop it into a mergable constant section. If we have 200f22ef01cSRoman Divacky // a section for this size, use it, otherwise use the arbitrary sized 201f22ef01cSRoman Divacky // mergable section. 202d88c1a5aSDimitry Andric switch ( 203d88c1a5aSDimitry Andric GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) { 204f22ef01cSRoman Divacky case 4: return SectionKind::getMergeableConst4(); 205f22ef01cSRoman Divacky case 8: return SectionKind::getMergeableConst8(); 206f22ef01cSRoman Divacky case 16: return SectionKind::getMergeableConst16(); 2073ca95b02SDimitry Andric case 32: return SectionKind::getMergeableConst32(); 208ff0cc061SDimitry Andric default: 209ff0cc061SDimitry Andric return SectionKind::getReadOnly(); 210f22ef01cSRoman Divacky } 211f22ef01cSRoman Divacky 2127d523365SDimitry Andric } else { 213d88c1a5aSDimitry Andric // In static, ROPI and RWPI relocation models, the linker will resolve 214d88c1a5aSDimitry Andric // all addresses, so the relocation entries will actually be constants by 215d88c1a5aSDimitry Andric // the time the app starts up. However, we can't put this into a 216d88c1a5aSDimitry Andric // mergable section, because the linker doesn't take relocations into 217d88c1a5aSDimitry Andric // consideration when it tries to merge entries in the section. 218d88c1a5aSDimitry Andric if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI || 219d88c1a5aSDimitry Andric ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI) 220f22ef01cSRoman Divacky return SectionKind::getReadOnly(); 221f22ef01cSRoman Divacky 222f22ef01cSRoman Divacky // Otherwise, the dynamic linker needs to fix it up, put it in the 223f22ef01cSRoman Divacky // writable data.rel section. 224f22ef01cSRoman Divacky return SectionKind::getReadOnlyWithRel(); 225f22ef01cSRoman Divacky } 226f22ef01cSRoman Divacky } 227f22ef01cSRoman Divacky 2283ca95b02SDimitry Andric // Okay, this isn't a constant. 2297d523365SDimitry Andric return SectionKind::getData(); 230f22ef01cSRoman Divacky } 231f22ef01cSRoman Divacky 232ff0cc061SDimitry Andric /// This method computes the appropriate section to emit the specified global 233ff0cc061SDimitry Andric /// variable or function definition. This should not be passed external (or 234ff0cc061SDimitry Andric /// available externally) globals. 235d88c1a5aSDimitry Andric MCSection *TargetLoweringObjectFile::SectionForGlobal( 236d88c1a5aSDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 237f22ef01cSRoman Divacky // Select section name. 238d88c1a5aSDimitry Andric if (GO->hasSection()) 239d88c1a5aSDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM); 240f22ef01cSRoman Divacky 241db17bf38SDimitry Andric if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 242db17bf38SDimitry Andric auto Attrs = GVar->getAttributes(); 243db17bf38SDimitry Andric if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) || 244db17bf38SDimitry Andric (Attrs.hasAttribute("data-section") && Kind.isData()) || 245db17bf38SDimitry Andric (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())) { 246db17bf38SDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM); 247db17bf38SDimitry Andric } 248db17bf38SDimitry Andric } 249db17bf38SDimitry Andric 250db17bf38SDimitry Andric if (auto *F = dyn_cast<Function>(GO)) { 251db17bf38SDimitry Andric if (F->hasFnAttribute("implicit-section-name")) 252db17bf38SDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM); 253db17bf38SDimitry Andric } 254db17bf38SDimitry Andric 255f22ef01cSRoman Divacky // Use default section depending on the 'type' of global 256d88c1a5aSDimitry Andric return SelectSectionForGlobal(GO, Kind, TM); 257f22ef01cSRoman Divacky } 258f22ef01cSRoman Divacky 259ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForJumpTable( 260d88c1a5aSDimitry Andric const Function &F, const TargetMachine &TM) const { 2613ca95b02SDimitry Andric unsigned Align = 0; 2627d523365SDimitry Andric return getSectionForConstant(F.getParent()->getDataLayout(), 2633ca95b02SDimitry Andric SectionKind::getReadOnly(), /*C=*/nullptr, 2643ca95b02SDimitry Andric Align); 265ff0cc061SDimitry Andric } 266ff0cc061SDimitry Andric 267ff0cc061SDimitry Andric bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection( 268ff0cc061SDimitry Andric bool UsesLabelDifference, const Function &F) const { 269ff0cc061SDimitry Andric // In PIC mode, we need to emit the jump table to the same section as the 270ff0cc061SDimitry Andric // function body itself, otherwise the label differences won't make sense. 271ff0cc061SDimitry Andric // FIXME: Need a better predicate for this: what about custom entries? 272ff0cc061SDimitry Andric if (UsesLabelDifference) 273ff0cc061SDimitry Andric return true; 274ff0cc061SDimitry Andric 275ff0cc061SDimitry Andric // We should also do if the section name is NULL or function is declared 276ff0cc061SDimitry Andric // in discardable section 277ff0cc061SDimitry Andric // FIXME: this isn't the right predicate, should be based on the MCSection 278ff0cc061SDimitry Andric // for the function. 2797a7e6055SDimitry Andric return F.isWeakForLinker(); 280ff0cc061SDimitry Andric } 281ff0cc061SDimitry Andric 282ff0cc061SDimitry Andric /// Given a mergable constant with the specified size and relocation 283ff0cc061SDimitry Andric /// information, return a section that it should be placed in. 2847d523365SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForConstant( 2853ca95b02SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 2863ca95b02SDimitry Andric unsigned &Align) const { 28791bc56edSDimitry Andric if (Kind.isReadOnly() && ReadOnlySection != nullptr) 288f22ef01cSRoman Divacky return ReadOnlySection; 289f22ef01cSRoman Divacky 290f22ef01cSRoman Divacky return DataSection; 291f22ef01cSRoman Divacky } 292f22ef01cSRoman Divacky 293139f7f9bSDimitry Andric /// getTTypeGlobalReference - Return an MCExpr to use for a 294f22ef01cSRoman Divacky /// reference to the specified global variable from exception 295f22ef01cSRoman Divacky /// handling information. 29691bc56edSDimitry Andric const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference( 297d88c1a5aSDimitry Andric const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 298d88c1a5aSDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const { 299139f7f9bSDimitry Andric const MCSymbolRefExpr *Ref = 300d88c1a5aSDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(GV), getContext()); 301139f7f9bSDimitry Andric 302139f7f9bSDimitry Andric return getTTypeReference(Ref, Encoding, Streamer); 303f22ef01cSRoman Divacky } 304f22ef01cSRoman Divacky 305f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile:: 306139f7f9bSDimitry Andric getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, 307f22ef01cSRoman Divacky MCStreamer &Streamer) const { 3083b0f4066SDimitry Andric switch (Encoding & 0x70) { 309f22ef01cSRoman Divacky default: 310f22ef01cSRoman Divacky report_fatal_error("We do not support this DWARF encoding yet!"); 311f22ef01cSRoman Divacky case dwarf::DW_EH_PE_absptr: 312f22ef01cSRoman Divacky // Do nothing special 313139f7f9bSDimitry Andric return Sym; 314f22ef01cSRoman Divacky case dwarf::DW_EH_PE_pcrel: { 315f22ef01cSRoman Divacky // Emit a label to the streamer for the current position. This gives us 316f22ef01cSRoman Divacky // .-foo addressing. 317ff0cc061SDimitry Andric MCSymbol *PCSym = getContext().createTempSymbol(); 318f22ef01cSRoman Divacky Streamer.EmitLabel(PCSym); 31997bc6c73SDimitry Andric const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext()); 32097bc6c73SDimitry Andric return MCBinaryExpr::createSub(Sym, PC, getContext()); 321f22ef01cSRoman Divacky } 322f22ef01cSRoman Divacky } 323f22ef01cSRoman Divacky } 324f785676fSDimitry Andric 325f785676fSDimitry Andric const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { 326f785676fSDimitry Andric // FIXME: It's not clear what, if any, default this should have - perhaps a 327f785676fSDimitry Andric // null return could mean 'no location' & we should just do that here. 32897bc6c73SDimitry Andric return MCSymbolRefExpr::create(Sym, *Ctx); 329f785676fSDimitry Andric } 330ff0cc061SDimitry Andric 331ff0cc061SDimitry Andric void TargetLoweringObjectFile::getNameWithPrefix( 332d88c1a5aSDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, 3337d523365SDimitry Andric const TargetMachine &TM) const { 334d88c1a5aSDimitry Andric Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false); 335ff0cc061SDimitry Andric } 336