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" 16139f7f9bSDimitry Andric #include "llvm/IR/Constants.h" 17139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h" 18139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h" 19139f7f9bSDimitry Andric #include "llvm/IR/Function.h" 20139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h" 2191bc56edSDimitry Andric #include "llvm/IR/Mangler.h" 2291bc56edSDimitry Andric #include "llvm/MC/MCAsmInfo.h" 23f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 24f22ef01cSRoman Divacky #include "llvm/MC/MCExpr.h" 25f22ef01cSRoman Divacky #include "llvm/MC/MCStreamer.h" 26f22ef01cSRoman Divacky #include "llvm/MC/MCSymbol.h" 27f22ef01cSRoman Divacky #include "llvm/Support/Dwarf.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; 4639d628a0SDimitry Andric DL = TM.getSubtargetImpl()->getDataLayout(); 476122f3e6SDimitry Andric InitMCObjectFileInfo(TM.getTargetTriple(), 486122f3e6SDimitry Andric TM.getRelocationModel(), TM.getCodeModel(), *Ctx); 49f22ef01cSRoman Divacky } 50f22ef01cSRoman Divacky 51f22ef01cSRoman Divacky TargetLoweringObjectFile::~TargetLoweringObjectFile() { 52f22ef01cSRoman Divacky } 53f22ef01cSRoman Divacky 54dff0c46cSDimitry Andric static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) { 5517a519f9SDimitry Andric const Constant *C = GV->getInitializer(); 56f22ef01cSRoman Divacky 57f22ef01cSRoman Divacky // Must have zero initializer. 58f22ef01cSRoman Divacky if (!C->isNullValue()) 59f22ef01cSRoman Divacky return false; 60f22ef01cSRoman Divacky 61f22ef01cSRoman Divacky // Leave constant zeros in readonly constant sections, so they can be shared. 62f22ef01cSRoman Divacky if (GV->isConstant()) 63f22ef01cSRoman Divacky return false; 64f22ef01cSRoman Divacky 65f22ef01cSRoman Divacky // If the global has an explicit section specified, don't put it in BSS. 6691bc56edSDimitry Andric if (GV->hasSection()) 67f22ef01cSRoman Divacky return false; 68f22ef01cSRoman Divacky 69f22ef01cSRoman Divacky // If -nozero-initialized-in-bss is specified, don't ever use BSS. 70f22ef01cSRoman Divacky if (NoZerosInBSS) 71f22ef01cSRoman Divacky return false; 72f22ef01cSRoman Divacky 73f22ef01cSRoman Divacky // Otherwise, put it in BSS! 74f22ef01cSRoman Divacky return true; 75f22ef01cSRoman Divacky } 76f22ef01cSRoman Divacky 77f22ef01cSRoman Divacky /// IsNullTerminatedString - Return true if the specified constant (which is 78f22ef01cSRoman Divacky /// known to have a type that is an array of 1/2/4 byte elements) ends with a 79dff0c46cSDimitry Andric /// nul value and contains no other nuls in it. Note that this is more general 80dff0c46cSDimitry Andric /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings. 81f22ef01cSRoman Divacky static bool IsNullTerminatedString(const Constant *C) { 82dff0c46cSDimitry Andric // First check: is we have constant array terminated with zero 83dff0c46cSDimitry Andric if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { 84dff0c46cSDimitry Andric unsigned NumElts = CDS->getNumElements(); 85dff0c46cSDimitry Andric assert(NumElts != 0 && "Can't have an empty CDS"); 86f22ef01cSRoman Divacky 87dff0c46cSDimitry Andric if (CDS->getElementAsInteger(NumElts-1) != 0) 88f22ef01cSRoman Divacky return false; // Not null terminated. 89f22ef01cSRoman Divacky 90f22ef01cSRoman Divacky // Verify that the null doesn't occur anywhere else in the string. 91dff0c46cSDimitry Andric for (unsigned i = 0; i != NumElts-1; ++i) 92dff0c46cSDimitry Andric if (CDS->getElementAsInteger(i) == 0) 93f22ef01cSRoman Divacky return false; 94f22ef01cSRoman Divacky return true; 95f22ef01cSRoman Divacky } 96f22ef01cSRoman Divacky 97f22ef01cSRoman Divacky // Another possibility: [1 x i8] zeroinitializer 98f22ef01cSRoman Divacky if (isa<ConstantAggregateZero>(C)) 99dff0c46cSDimitry Andric return cast<ArrayType>(C->getType())->getNumElements() == 1; 100f22ef01cSRoman Divacky 101f22ef01cSRoman Divacky return false; 102f22ef01cSRoman Divacky } 103f22ef01cSRoman Divacky 10491bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase( 10591bc56edSDimitry Andric const GlobalValue *GV, StringRef Suffix, Mangler &Mang, 10691bc56edSDimitry Andric const TargetMachine &TM) const { 10791bc56edSDimitry Andric assert(!Suffix.empty()); 10891bc56edSDimitry Andric 109f785676fSDimitry Andric SmallString<60> NameStr; 11091bc56edSDimitry Andric NameStr += DL->getPrivateGlobalPrefix(); 11191bc56edSDimitry Andric TM.getNameWithPrefix(NameStr, GV, Mang); 11291bc56edSDimitry Andric NameStr.append(Suffix.begin(), Suffix.end()); 113f785676fSDimitry Andric return Ctx->GetOrCreateSymbol(NameStr.str()); 114f785676fSDimitry Andric } 115f785676fSDimitry Andric 11691bc56edSDimitry Andric MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol( 11791bc56edSDimitry Andric const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 1183b0f4066SDimitry Andric MachineModuleInfo *MMI) const { 11991bc56edSDimitry Andric return TM.getSymbol(GV, Mang); 1203b0f4066SDimitry Andric } 1213b0f4066SDimitry Andric 1223b0f4066SDimitry Andric void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer, 1233b0f4066SDimitry Andric const TargetMachine &TM, 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. 133f22ef01cSRoman Divacky SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, 134f22ef01cSRoman Divacky const TargetMachine &TM){ 135f22ef01cSRoman Divacky assert(!GV->isDeclaration() && !GV->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. 141f22ef01cSRoman Divacky const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 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. 173f22ef01cSRoman Divacky switch (C->getRelocationInfo()) { 174f22ef01cSRoman Divacky case Constant::NoRelocation: 1752754fe60SDimitry Andric // If the global is required to have a unique address, it can't be put 1762754fe60SDimitry Andric // into a mergable section: just drop it into the general read-only 1772754fe60SDimitry Andric // section instead. 1782754fe60SDimitry Andric if (!GVar->hasUnnamedAddr()) 1792754fe60SDimitry Andric return SectionKind::getReadOnly(); 1802754fe60SDimitry Andric 181f22ef01cSRoman Divacky // If initializer is a null-terminated string, put it in a "cstring" 182f22ef01cSRoman Divacky // section of the right width. 1836122f3e6SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { 1846122f3e6SDimitry Andric if (IntegerType *ITy = 185f22ef01cSRoman Divacky dyn_cast<IntegerType>(ATy->getElementType())) { 186f22ef01cSRoman Divacky if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || 187f22ef01cSRoman Divacky ITy->getBitWidth() == 32) && 188f22ef01cSRoman Divacky IsNullTerminatedString(C)) { 189f22ef01cSRoman Divacky if (ITy->getBitWidth() == 8) 190f22ef01cSRoman Divacky return SectionKind::getMergeable1ByteCString(); 191f22ef01cSRoman Divacky if (ITy->getBitWidth() == 16) 192f22ef01cSRoman Divacky return SectionKind::getMergeable2ByteCString(); 193f22ef01cSRoman Divacky 194f22ef01cSRoman Divacky assert(ITy->getBitWidth() == 32 && "Unknown width"); 195f22ef01cSRoman Divacky return SectionKind::getMergeable4ByteCString(); 196f22ef01cSRoman Divacky } 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky } 199f22ef01cSRoman Divacky 200f22ef01cSRoman Divacky // Otherwise, just drop it into a mergable constant section. If we have 201f22ef01cSRoman Divacky // a section for this size, use it, otherwise use the arbitrary sized 202f22ef01cSRoman Divacky // mergable section. 20339d628a0SDimitry Andric switch (TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize( 20439d628a0SDimitry Andric C->getType())) { 205f22ef01cSRoman Divacky case 4: return SectionKind::getMergeableConst4(); 206f22ef01cSRoman Divacky case 8: return SectionKind::getMergeableConst8(); 207f22ef01cSRoman Divacky case 16: return SectionKind::getMergeableConst16(); 208f22ef01cSRoman Divacky default: return SectionKind::getMergeableConst(); 209f22ef01cSRoman Divacky } 210f22ef01cSRoman Divacky 211f22ef01cSRoman Divacky case Constant::LocalRelocation: 212f22ef01cSRoman Divacky // In static relocation model, the linker will resolve all addresses, so 213f22ef01cSRoman Divacky // the relocation entries will actually be constants by the time the app 214f22ef01cSRoman Divacky // starts up. However, we can't put this into a mergable section, because 215f22ef01cSRoman Divacky // the linker doesn't take relocations into consideration when it tries to 216f22ef01cSRoman Divacky // merge entries in the section. 217f22ef01cSRoman Divacky if (ReloModel == Reloc::Static) 218f22ef01cSRoman Divacky return SectionKind::getReadOnly(); 219f22ef01cSRoman Divacky 220f22ef01cSRoman Divacky // Otherwise, the dynamic linker needs to fix it up, put it in the 221f22ef01cSRoman Divacky // writable data.rel.local section. 222f22ef01cSRoman Divacky return SectionKind::getReadOnlyWithRelLocal(); 223f22ef01cSRoman Divacky 224f22ef01cSRoman Divacky case Constant::GlobalRelocations: 225f22ef01cSRoman Divacky // In static relocation model, the linker will resolve all addresses, so 226f22ef01cSRoman Divacky // the relocation entries will actually be constants by the time the app 227f22ef01cSRoman Divacky // starts up. However, we can't put this into a mergable section, because 228f22ef01cSRoman Divacky // the linker doesn't take relocations into consideration when it tries to 229f22ef01cSRoman Divacky // merge entries in the section. 230f22ef01cSRoman Divacky if (ReloModel == Reloc::Static) 231f22ef01cSRoman Divacky return SectionKind::getReadOnly(); 232f22ef01cSRoman Divacky 233f22ef01cSRoman Divacky // Otherwise, the dynamic linker needs to fix it up, put it in the 234f22ef01cSRoman Divacky // writable data.rel section. 235f22ef01cSRoman Divacky return SectionKind::getReadOnlyWithRel(); 236f22ef01cSRoman Divacky } 237f22ef01cSRoman Divacky } 238f22ef01cSRoman Divacky 239f22ef01cSRoman Divacky // Okay, this isn't a constant. If the initializer for the global is going 240f22ef01cSRoman Divacky // to require a runtime relocation by the dynamic linker, put it into a more 241f22ef01cSRoman Divacky // specific section to improve startup time of the app. This coalesces these 242f22ef01cSRoman Divacky // globals together onto fewer pages, improving the locality of the dynamic 243f22ef01cSRoman Divacky // linker. 244f22ef01cSRoman Divacky if (ReloModel == Reloc::Static) 245f22ef01cSRoman Divacky return SectionKind::getDataNoRel(); 246f22ef01cSRoman Divacky 247f22ef01cSRoman Divacky switch (C->getRelocationInfo()) { 248f22ef01cSRoman Divacky case Constant::NoRelocation: 249f22ef01cSRoman Divacky return SectionKind::getDataNoRel(); 250f22ef01cSRoman Divacky case Constant::LocalRelocation: 251f22ef01cSRoman Divacky return SectionKind::getDataRelLocal(); 252f22ef01cSRoman Divacky case Constant::GlobalRelocations: 253f22ef01cSRoman Divacky return SectionKind::getDataRel(); 254f22ef01cSRoman Divacky } 255dff0c46cSDimitry Andric llvm_unreachable("Invalid relocation"); 256f22ef01cSRoman Divacky } 257f22ef01cSRoman Divacky 258f22ef01cSRoman Divacky /// SectionForGlobal - This method computes the appropriate section to emit 259f22ef01cSRoman Divacky /// the specified global variable or function definition. This should not 260f22ef01cSRoman Divacky /// be passed external (or available externally) globals. 261f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFile:: 26291bc56edSDimitry Andric SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 263f22ef01cSRoman Divacky const TargetMachine &TM) const { 264f22ef01cSRoman Divacky // Select section name. 265f22ef01cSRoman Divacky if (GV->hasSection()) 266f22ef01cSRoman Divacky return getExplicitSectionGlobal(GV, Kind, Mang, TM); 267f22ef01cSRoman Divacky 268f22ef01cSRoman Divacky 269f22ef01cSRoman Divacky // Use default section depending on the 'type' of global 270f22ef01cSRoman Divacky return SelectSectionForGlobal(GV, Kind, Mang, TM); 271f22ef01cSRoman Divacky } 272f22ef01cSRoman Divacky 273f22ef01cSRoman Divacky /// getSectionForConstant - Given a mergable constant with the 274f22ef01cSRoman Divacky /// specified size and relocation information, return a section that it 275f22ef01cSRoman Divacky /// should be placed in. 276f22ef01cSRoman Divacky const MCSection * 27791bc56edSDimitry Andric TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind, 27891bc56edSDimitry Andric const Constant *C) const { 27991bc56edSDimitry Andric if (Kind.isReadOnly() && ReadOnlySection != nullptr) 280f22ef01cSRoman Divacky return ReadOnlySection; 281f22ef01cSRoman Divacky 282f22ef01cSRoman Divacky return DataSection; 283f22ef01cSRoman Divacky } 284f22ef01cSRoman Divacky 285139f7f9bSDimitry Andric /// getTTypeGlobalReference - Return an MCExpr to use for a 286f22ef01cSRoman Divacky /// reference to the specified global variable from exception 287f22ef01cSRoman Divacky /// handling information. 28891bc56edSDimitry Andric const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference( 28991bc56edSDimitry Andric const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 29091bc56edSDimitry Andric const TargetMachine &TM, MachineModuleInfo *MMI, 291f22ef01cSRoman Divacky MCStreamer &Streamer) const { 292139f7f9bSDimitry Andric const MCSymbolRefExpr *Ref = 29391bc56edSDimitry Andric MCSymbolRefExpr::Create(TM.getSymbol(GV, Mang), getContext()); 294139f7f9bSDimitry Andric 295139f7f9bSDimitry Andric return getTTypeReference(Ref, Encoding, Streamer); 296f22ef01cSRoman Divacky } 297f22ef01cSRoman Divacky 298f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile:: 299139f7f9bSDimitry Andric getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, 300f22ef01cSRoman Divacky MCStreamer &Streamer) const { 3013b0f4066SDimitry Andric switch (Encoding & 0x70) { 302f22ef01cSRoman Divacky default: 303f22ef01cSRoman Divacky report_fatal_error("We do not support this DWARF encoding yet!"); 304f22ef01cSRoman Divacky case dwarf::DW_EH_PE_absptr: 305f22ef01cSRoman Divacky // Do nothing special 306139f7f9bSDimitry Andric return Sym; 307f22ef01cSRoman Divacky case dwarf::DW_EH_PE_pcrel: { 308f22ef01cSRoman Divacky // Emit a label to the streamer for the current position. This gives us 309f22ef01cSRoman Divacky // .-foo addressing. 310f22ef01cSRoman Divacky MCSymbol *PCSym = getContext().CreateTempSymbol(); 311f22ef01cSRoman Divacky Streamer.EmitLabel(PCSym); 312f22ef01cSRoman Divacky const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext()); 313139f7f9bSDimitry Andric return MCBinaryExpr::CreateSub(Sym, PC, getContext()); 314f22ef01cSRoman Divacky } 315f22ef01cSRoman Divacky } 316f22ef01cSRoman Divacky } 317f785676fSDimitry Andric 318f785676fSDimitry Andric const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { 319f785676fSDimitry Andric // FIXME: It's not clear what, if any, default this should have - perhaps a 320f785676fSDimitry Andric // null return could mean 'no location' & we should just do that here. 321f785676fSDimitry Andric return MCSymbolRefExpr::Create(Sym, *Ctx); 322f785676fSDimitry Andric } 323