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 15f22ef01cSRoman Divacky #include "llvm/Target/TargetLoweringObjectFile.h" 16db17bf38SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 17139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 18139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h" 19139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h" 20139f7f9bSDimitry Andric #include "llvm/IR/Function.h" 21139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h" 2291bc56edSDimitry Andric #include "llvm/IR/Mangler.h" 2391bc56edSDimitry Andric #include "llvm/MC/MCAsmInfo.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" 3091bc56edSDimitry Andric #include "llvm/Target/TargetLowering.h" 31139f7f9bSDimitry Andric #include "llvm/Target/TargetMachine.h" 32139f7f9bSDimitry Andric #include "llvm/Target/TargetOptions.h" 3339d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h" 34f22ef01cSRoman Divacky using namespace llvm; 35f22ef01cSRoman Divacky 36f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 37f22ef01cSRoman Divacky // Generic Code 38f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 39f22ef01cSRoman Divacky 406122f3e6SDimitry Andric /// Initialize - this method must be called before any actual lowering is 416122f3e6SDimitry Andric /// done. This specifies the current context for codegen, and gives the 426122f3e6SDimitry Andric /// lowering implementations a chance to set up their default sections. 436122f3e6SDimitry Andric void TargetLoweringObjectFile::Initialize(MCContext &ctx, 446122f3e6SDimitry Andric const TargetMachine &TM) { 456122f3e6SDimitry Andric Ctx = &ctx; 46d88c1a5aSDimitry Andric // `Initialize` can be called more than once. 47f37b6182SDimitry Andric delete Mang; 48d88c1a5aSDimitry Andric Mang = new Mangler(); 493ca95b02SDimitry Andric InitMCObjectFileInfo(TM.getTargetTriple(), TM.isPositionIndependent(), 508f0fd8f6SDimitry Andric TM.getCodeModel(), *Ctx); 51f22ef01cSRoman Divacky } 52f22ef01cSRoman Divacky 53f22ef01cSRoman Divacky TargetLoweringObjectFile::~TargetLoweringObjectFile() { 54d88c1a5aSDimitry Andric delete Mang; 55f22ef01cSRoman Divacky } 56f22ef01cSRoman Divacky 57dff0c46cSDimitry Andric static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) { 5817a519f9SDimitry Andric const Constant *C = GV->getInitializer(); 59f22ef01cSRoman Divacky 60f22ef01cSRoman Divacky // Must have zero initializer. 61f22ef01cSRoman Divacky if (!C->isNullValue()) 62f22ef01cSRoman Divacky return false; 63f22ef01cSRoman Divacky 64f22ef01cSRoman Divacky // Leave constant zeros in readonly constant sections, so they can be shared. 65f22ef01cSRoman Divacky if (GV->isConstant()) 66f22ef01cSRoman Divacky return false; 67f22ef01cSRoman Divacky 68f22ef01cSRoman Divacky // If the global has an explicit section specified, don't put it in BSS. 6991bc56edSDimitry Andric if (GV->hasSection()) 70f22ef01cSRoman Divacky return false; 71f22ef01cSRoman Divacky 72f22ef01cSRoman Divacky // If -nozero-initialized-in-bss is specified, don't ever use BSS. 73f22ef01cSRoman Divacky if (NoZerosInBSS) 74f22ef01cSRoman Divacky return false; 75f22ef01cSRoman Divacky 76f22ef01cSRoman Divacky // Otherwise, put it in BSS! 77f22ef01cSRoman Divacky return true; 78f22ef01cSRoman Divacky } 79f22ef01cSRoman Divacky 80f22ef01cSRoman Divacky /// IsNullTerminatedString - Return true if the specified constant (which is 81f22ef01cSRoman Divacky /// known to have a type that is an array of 1/2/4 byte elements) ends with a 82dff0c46cSDimitry Andric /// nul value and contains no other nuls in it. Note that this is more general 83dff0c46cSDimitry Andric /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings. 84f22ef01cSRoman Divacky static bool IsNullTerminatedString(const Constant *C) { 85dff0c46cSDimitry Andric // First check: is we have constant array terminated with zero 86dff0c46cSDimitry Andric if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { 87dff0c46cSDimitry Andric unsigned NumElts = CDS->getNumElements(); 88dff0c46cSDimitry Andric assert(NumElts != 0 && "Can't have an empty CDS"); 89f22ef01cSRoman Divacky 90dff0c46cSDimitry Andric if (CDS->getElementAsInteger(NumElts-1) != 0) 91f22ef01cSRoman Divacky return false; // Not null terminated. 92f22ef01cSRoman Divacky 93f22ef01cSRoman Divacky // Verify that the null doesn't occur anywhere else in the string. 94dff0c46cSDimitry Andric for (unsigned i = 0; i != NumElts-1; ++i) 95dff0c46cSDimitry Andric if (CDS->getElementAsInteger(i) == 0) 96f22ef01cSRoman Divacky return false; 97f22ef01cSRoman Divacky return true; 98f22ef01cSRoman Divacky } 99f22ef01cSRoman Divacky 100f22ef01cSRoman Divacky // Another possibility: [1 x i8] zeroinitializer 101f22ef01cSRoman Divacky if (isa<ConstantAggregateZero>(C)) 102dff0c46cSDimitry Andric return cast<ArrayType>(C->getType())->getNumElements() == 1; 103f22ef01cSRoman Divacky 104f22ef01cSRoman Divacky return false; 105f22ef01cSRoman Divacky } 106f22ef01cSRoman Divacky 10791bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase( 108d88c1a5aSDimitry Andric const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const { 10991bc56edSDimitry Andric assert(!Suffix.empty()); 11091bc56edSDimitry Andric 111f785676fSDimitry Andric SmallString<60> NameStr; 1127d523365SDimitry Andric NameStr += GV->getParent()->getDataLayout().getPrivateGlobalPrefix(); 113d88c1a5aSDimitry Andric TM.getNameWithPrefix(NameStr, GV, *Mang); 11491bc56edSDimitry Andric NameStr.append(Suffix.begin(), Suffix.end()); 115ff0cc061SDimitry Andric return Ctx->getOrCreateSymbol(NameStr); 116f785676fSDimitry Andric } 117f785676fSDimitry Andric 11891bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol( 119d88c1a5aSDimitry Andric const GlobalValue *GV, const TargetMachine &TM, 1203b0f4066SDimitry Andric MachineModuleInfo *MMI) const { 121d88c1a5aSDimitry Andric return TM.getSymbol(GV); 1223b0f4066SDimitry Andric } 1233b0f4066SDimitry Andric 1243b0f4066SDimitry Andric void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer, 1257d523365SDimitry Andric const DataLayout &, 1263b0f4066SDimitry Andric const MCSymbol *Sym) const { 1273b0f4066SDimitry Andric } 1283b0f4066SDimitry Andric 1293b0f4066SDimitry Andric 130f22ef01cSRoman Divacky /// getKindForGlobal - This is a top-level target-independent classifier for 131f22ef01cSRoman Divacky /// a global variable. Given an global variable and information from TM, it 132f22ef01cSRoman Divacky /// classifies the global in a variety of ways that make various target 133f22ef01cSRoman Divacky /// implementations simpler. The target implementation is free to ignore this 134f22ef01cSRoman Divacky /// extra info of course. 135d88c1a5aSDimitry Andric SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO, 136f22ef01cSRoman Divacky const TargetMachine &TM){ 137d88c1a5aSDimitry Andric assert(!GO->isDeclaration() && !GO->hasAvailableExternallyLinkage() && 138f22ef01cSRoman Divacky "Can only be used for global definitions"); 139f22ef01cSRoman Divacky 140f22ef01cSRoman Divacky Reloc::Model ReloModel = TM.getRelocationModel(); 141f22ef01cSRoman Divacky 142f22ef01cSRoman Divacky // Early exit - functions should be always in text sections. 143d88c1a5aSDimitry Andric const auto *GVar = dyn_cast<GlobalVariable>(GO); 14491bc56edSDimitry Andric if (!GVar) 145f22ef01cSRoman Divacky return SectionKind::getText(); 146f22ef01cSRoman Divacky 147f22ef01cSRoman Divacky // Handle thread-local data first. 148f22ef01cSRoman Divacky if (GVar->isThreadLocal()) { 149dff0c46cSDimitry Andric if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) 150f22ef01cSRoman Divacky return SectionKind::getThreadBSS(); 151f22ef01cSRoman Divacky return SectionKind::getThreadData(); 152f22ef01cSRoman Divacky } 153f22ef01cSRoman Divacky 154f22ef01cSRoman Divacky // Variables with common linkage always get classified as common. 155f22ef01cSRoman Divacky if (GVar->hasCommonLinkage()) 156f22ef01cSRoman Divacky return SectionKind::getCommon(); 157f22ef01cSRoman Divacky 158f22ef01cSRoman Divacky // Variable can be easily put to BSS section. 159dff0c46cSDimitry Andric if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) { 160f22ef01cSRoman Divacky if (GVar->hasLocalLinkage()) 161f22ef01cSRoman Divacky return SectionKind::getBSSLocal(); 162f22ef01cSRoman Divacky else if (GVar->hasExternalLinkage()) 163f22ef01cSRoman Divacky return SectionKind::getBSSExtern(); 164f22ef01cSRoman Divacky return SectionKind::getBSS(); 165f22ef01cSRoman Divacky } 166f22ef01cSRoman Divacky 16717a519f9SDimitry Andric const Constant *C = GVar->getInitializer(); 168f22ef01cSRoman Divacky 169f22ef01cSRoman Divacky // If the global is marked constant, we can put it into a mergable section, 170f22ef01cSRoman Divacky // a mergable string section, or general .data if it contains relocations. 171f22ef01cSRoman Divacky if (GVar->isConstant()) { 172f22ef01cSRoman Divacky // If the initializer for the global contains something that requires a 1737ae0e2c9SDimitry Andric // relocation, then we may have to drop this into a writable data section 174f22ef01cSRoman Divacky // even though it is marked const. 1757d523365SDimitry Andric if (!C->needsRelocation()) { 1762754fe60SDimitry Andric // If the global is required to have a unique address, it can't be put 1772754fe60SDimitry Andric // into a mergable section: just drop it into the general read-only 1782754fe60SDimitry Andric // section instead. 1793ca95b02SDimitry Andric if (!GVar->hasGlobalUnnamedAddr()) 1802754fe60SDimitry Andric return SectionKind::getReadOnly(); 1812754fe60SDimitry Andric 182f22ef01cSRoman Divacky // If initializer is a null-terminated string, put it in a "cstring" 183f22ef01cSRoman Divacky // section of the right width. 1846122f3e6SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { 1856122f3e6SDimitry Andric if (IntegerType *ITy = 186f22ef01cSRoman Divacky dyn_cast<IntegerType>(ATy->getElementType())) { 187f22ef01cSRoman Divacky if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || 188f22ef01cSRoman Divacky ITy->getBitWidth() == 32) && 189f22ef01cSRoman Divacky IsNullTerminatedString(C)) { 190f22ef01cSRoman Divacky if (ITy->getBitWidth() == 8) 191f22ef01cSRoman Divacky return SectionKind::getMergeable1ByteCString(); 192f22ef01cSRoman Divacky if (ITy->getBitWidth() == 16) 193f22ef01cSRoman Divacky return SectionKind::getMergeable2ByteCString(); 194f22ef01cSRoman Divacky 195f22ef01cSRoman Divacky assert(ITy->getBitWidth() == 32 && "Unknown width"); 196f22ef01cSRoman Divacky return SectionKind::getMergeable4ByteCString(); 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky } 199f22ef01cSRoman Divacky } 200f22ef01cSRoman Divacky 201f22ef01cSRoman Divacky // Otherwise, just drop it into a mergable constant section. If we have 202f22ef01cSRoman Divacky // a section for this size, use it, otherwise use the arbitrary sized 203f22ef01cSRoman Divacky // mergable section. 204d88c1a5aSDimitry Andric switch ( 205d88c1a5aSDimitry Andric GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) { 206f22ef01cSRoman Divacky case 4: return SectionKind::getMergeableConst4(); 207f22ef01cSRoman Divacky case 8: return SectionKind::getMergeableConst8(); 208f22ef01cSRoman Divacky case 16: return SectionKind::getMergeableConst16(); 2093ca95b02SDimitry Andric case 32: return SectionKind::getMergeableConst32(); 210ff0cc061SDimitry Andric default: 211ff0cc061SDimitry Andric return SectionKind::getReadOnly(); 212f22ef01cSRoman Divacky } 213f22ef01cSRoman Divacky 2147d523365SDimitry Andric } else { 215d88c1a5aSDimitry Andric // In static, ROPI and RWPI relocation models, the linker will resolve 216d88c1a5aSDimitry Andric // all addresses, so the relocation entries will actually be constants by 217d88c1a5aSDimitry Andric // the time the app starts up. However, we can't put this into a 218d88c1a5aSDimitry Andric // mergable section, because the linker doesn't take relocations into 219d88c1a5aSDimitry Andric // consideration when it tries to merge entries in the section. 220d88c1a5aSDimitry Andric if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI || 221d88c1a5aSDimitry Andric ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI) 222f22ef01cSRoman Divacky return SectionKind::getReadOnly(); 223f22ef01cSRoman Divacky 224f22ef01cSRoman Divacky // Otherwise, the dynamic linker needs to fix it up, put it in the 225f22ef01cSRoman Divacky // writable data.rel section. 226f22ef01cSRoman Divacky return SectionKind::getReadOnlyWithRel(); 227f22ef01cSRoman Divacky } 228f22ef01cSRoman Divacky } 229f22ef01cSRoman Divacky 2303ca95b02SDimitry Andric // Okay, this isn't a constant. 2317d523365SDimitry Andric return SectionKind::getData(); 232f22ef01cSRoman Divacky } 233f22ef01cSRoman Divacky 234ff0cc061SDimitry Andric /// This method computes the appropriate section to emit the specified global 235ff0cc061SDimitry Andric /// variable or function definition. This should not be passed external (or 236ff0cc061SDimitry Andric /// available externally) globals. 237d88c1a5aSDimitry Andric MCSection *TargetLoweringObjectFile::SectionForGlobal( 238d88c1a5aSDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { 239f22ef01cSRoman Divacky // Select section name. 240d88c1a5aSDimitry Andric if (GO->hasSection()) 241d88c1a5aSDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM); 242f22ef01cSRoman Divacky 243db17bf38SDimitry Andric if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 244db17bf38SDimitry Andric auto Attrs = GVar->getAttributes(); 245db17bf38SDimitry Andric if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) || 246db17bf38SDimitry Andric (Attrs.hasAttribute("data-section") && Kind.isData()) || 247db17bf38SDimitry Andric (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())) { 248db17bf38SDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM); 249db17bf38SDimitry Andric } 250db17bf38SDimitry Andric } 251db17bf38SDimitry Andric 252db17bf38SDimitry Andric if (auto *F = dyn_cast<Function>(GO)) { 253db17bf38SDimitry Andric if (F->hasFnAttribute("implicit-section-name")) 254db17bf38SDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM); 255db17bf38SDimitry Andric } 256db17bf38SDimitry Andric 257f22ef01cSRoman Divacky // Use default section depending on the 'type' of global 258d88c1a5aSDimitry Andric return SelectSectionForGlobal(GO, Kind, TM); 259f22ef01cSRoman Divacky } 260f22ef01cSRoman Divacky 261ff0cc061SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForJumpTable( 262d88c1a5aSDimitry Andric const Function &F, const TargetMachine &TM) const { 2633ca95b02SDimitry Andric unsigned Align = 0; 2647d523365SDimitry Andric return getSectionForConstant(F.getParent()->getDataLayout(), 2653ca95b02SDimitry Andric SectionKind::getReadOnly(), /*C=*/nullptr, 2663ca95b02SDimitry Andric Align); 267ff0cc061SDimitry Andric } 268ff0cc061SDimitry Andric 269ff0cc061SDimitry Andric bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection( 270ff0cc061SDimitry Andric bool UsesLabelDifference, const Function &F) const { 271ff0cc061SDimitry Andric // In PIC mode, we need to emit the jump table to the same section as the 272ff0cc061SDimitry Andric // function body itself, otherwise the label differences won't make sense. 273ff0cc061SDimitry Andric // FIXME: Need a better predicate for this: what about custom entries? 274ff0cc061SDimitry Andric if (UsesLabelDifference) 275ff0cc061SDimitry Andric return true; 276ff0cc061SDimitry Andric 277ff0cc061SDimitry Andric // We should also do if the section name is NULL or function is declared 278ff0cc061SDimitry Andric // in discardable section 279ff0cc061SDimitry Andric // FIXME: this isn't the right predicate, should be based on the MCSection 280ff0cc061SDimitry Andric // for the function. 2817a7e6055SDimitry Andric return F.isWeakForLinker(); 282ff0cc061SDimitry Andric } 283ff0cc061SDimitry Andric 284ff0cc061SDimitry Andric /// Given a mergable constant with the specified size and relocation 285ff0cc061SDimitry Andric /// information, return a section that it should be placed in. 2867d523365SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForConstant( 2873ca95b02SDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C, 2883ca95b02SDimitry Andric unsigned &Align) const { 28991bc56edSDimitry Andric if (Kind.isReadOnly() && ReadOnlySection != nullptr) 290f22ef01cSRoman Divacky return ReadOnlySection; 291f22ef01cSRoman Divacky 292f22ef01cSRoman Divacky return DataSection; 293f22ef01cSRoman Divacky } 294f22ef01cSRoman Divacky 295139f7f9bSDimitry Andric /// getTTypeGlobalReference - Return an MCExpr to use for a 296f22ef01cSRoman Divacky /// reference to the specified global variable from exception 297f22ef01cSRoman Divacky /// handling information. 29891bc56edSDimitry Andric const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference( 299d88c1a5aSDimitry Andric const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 300d88c1a5aSDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const { 301139f7f9bSDimitry Andric const MCSymbolRefExpr *Ref = 302d88c1a5aSDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(GV), getContext()); 303139f7f9bSDimitry Andric 304139f7f9bSDimitry Andric return getTTypeReference(Ref, Encoding, Streamer); 305f22ef01cSRoman Divacky } 306f22ef01cSRoman Divacky 307f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile:: 308139f7f9bSDimitry Andric getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, 309f22ef01cSRoman Divacky MCStreamer &Streamer) const { 3103b0f4066SDimitry Andric switch (Encoding & 0x70) { 311f22ef01cSRoman Divacky default: 312f22ef01cSRoman Divacky report_fatal_error("We do not support this DWARF encoding yet!"); 313f22ef01cSRoman Divacky case dwarf::DW_EH_PE_absptr: 314f22ef01cSRoman Divacky // Do nothing special 315139f7f9bSDimitry Andric return Sym; 316f22ef01cSRoman Divacky case dwarf::DW_EH_PE_pcrel: { 317f22ef01cSRoman Divacky // Emit a label to the streamer for the current position. This gives us 318f22ef01cSRoman Divacky // .-foo addressing. 319ff0cc061SDimitry Andric MCSymbol *PCSym = getContext().createTempSymbol(); 320f22ef01cSRoman Divacky Streamer.EmitLabel(PCSym); 32197bc6c73SDimitry Andric const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext()); 32297bc6c73SDimitry Andric return MCBinaryExpr::createSub(Sym, PC, getContext()); 323f22ef01cSRoman Divacky } 324f22ef01cSRoman Divacky } 325f22ef01cSRoman Divacky } 326f785676fSDimitry Andric 327f785676fSDimitry Andric const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { 328f785676fSDimitry Andric // FIXME: It's not clear what, if any, default this should have - perhaps a 329f785676fSDimitry Andric // null return could mean 'no location' & we should just do that here. 33097bc6c73SDimitry Andric return MCSymbolRefExpr::create(Sym, *Ctx); 331f785676fSDimitry Andric } 332ff0cc061SDimitry Andric 333ff0cc061SDimitry Andric void TargetLoweringObjectFile::getNameWithPrefix( 334d88c1a5aSDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV, 3357d523365SDimitry Andric const TargetMachine &TM) const { 336d88c1a5aSDimitry Andric Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false); 337ff0cc061SDimitry Andric } 338