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" 16f22ef01cSRoman Divacky #include "llvm/Constants.h" 17f22ef01cSRoman Divacky #include "llvm/DerivedTypes.h" 18f22ef01cSRoman Divacky #include "llvm/Function.h" 19f22ef01cSRoman Divacky #include "llvm/GlobalVariable.h" 20f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h" 21f22ef01cSRoman Divacky #include "llvm/MC/MCExpr.h" 22f22ef01cSRoman Divacky #include "llvm/MC/MCStreamer.h" 23f22ef01cSRoman Divacky #include "llvm/MC/MCSymbol.h" 24f22ef01cSRoman Divacky #include "llvm/Target/Mangler.h" 25f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h" 26f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h" 27f22ef01cSRoman Divacky #include "llvm/Target/TargetOptions.h" 28f22ef01cSRoman Divacky #include "llvm/Support/Dwarf.h" 29f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h" 30f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h" 31f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h" 32f22ef01cSRoman Divacky using namespace llvm; 33f22ef01cSRoman Divacky 34f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 35f22ef01cSRoman Divacky // Generic Code 36f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 37f22ef01cSRoman Divacky 38f22ef01cSRoman Divacky TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) { 39f22ef01cSRoman Divacky TextSection = 0; 40f22ef01cSRoman Divacky DataSection = 0; 41f22ef01cSRoman Divacky BSSSection = 0; 42f22ef01cSRoman Divacky ReadOnlySection = 0; 43f22ef01cSRoman Divacky StaticCtorSection = 0; 44f22ef01cSRoman Divacky StaticDtorSection = 0; 45f22ef01cSRoman Divacky LSDASection = 0; 46f22ef01cSRoman Divacky EHFrameSection = 0; 47f22ef01cSRoman Divacky 48f22ef01cSRoman Divacky DwarfAbbrevSection = 0; 49f22ef01cSRoman Divacky DwarfInfoSection = 0; 50f22ef01cSRoman Divacky DwarfLineSection = 0; 51f22ef01cSRoman Divacky DwarfFrameSection = 0; 52f22ef01cSRoman Divacky DwarfPubNamesSection = 0; 53f22ef01cSRoman Divacky DwarfPubTypesSection = 0; 54f22ef01cSRoman Divacky DwarfDebugInlineSection = 0; 55f22ef01cSRoman Divacky DwarfStrSection = 0; 56f22ef01cSRoman Divacky DwarfLocSection = 0; 57f22ef01cSRoman Divacky DwarfARangesSection = 0; 58f22ef01cSRoman Divacky DwarfRangesSection = 0; 59f22ef01cSRoman Divacky DwarfMacroInfoSection = 0; 60f22ef01cSRoman Divacky 61f22ef01cSRoman Divacky IsFunctionEHSymbolGlobal = false; 62f22ef01cSRoman Divacky IsFunctionEHFrameSymbolPrivate = true; 63f22ef01cSRoman Divacky SupportsWeakOmittedEHFrame = true; 64f22ef01cSRoman Divacky } 65f22ef01cSRoman Divacky 66f22ef01cSRoman Divacky TargetLoweringObjectFile::~TargetLoweringObjectFile() { 67f22ef01cSRoman Divacky } 68f22ef01cSRoman Divacky 69f22ef01cSRoman Divacky static bool isSuitableForBSS(const GlobalVariable *GV) { 70f22ef01cSRoman Divacky Constant *C = GV->getInitializer(); 71f22ef01cSRoman Divacky 72f22ef01cSRoman Divacky // Must have zero initializer. 73f22ef01cSRoman Divacky if (!C->isNullValue()) 74f22ef01cSRoman Divacky return false; 75f22ef01cSRoman Divacky 76f22ef01cSRoman Divacky // Leave constant zeros in readonly constant sections, so they can be shared. 77f22ef01cSRoman Divacky if (GV->isConstant()) 78f22ef01cSRoman Divacky return false; 79f22ef01cSRoman Divacky 80f22ef01cSRoman Divacky // If the global has an explicit section specified, don't put it in BSS. 81f22ef01cSRoman Divacky if (!GV->getSection().empty()) 82f22ef01cSRoman Divacky return false; 83f22ef01cSRoman Divacky 84f22ef01cSRoman Divacky // If -nozero-initialized-in-bss is specified, don't ever use BSS. 85f22ef01cSRoman Divacky if (NoZerosInBSS) 86f22ef01cSRoman Divacky return false; 87f22ef01cSRoman Divacky 88f22ef01cSRoman Divacky // Otherwise, put it in BSS! 89f22ef01cSRoman Divacky return true; 90f22ef01cSRoman Divacky } 91f22ef01cSRoman Divacky 92f22ef01cSRoman Divacky /// IsNullTerminatedString - Return true if the specified constant (which is 93f22ef01cSRoman Divacky /// known to have a type that is an array of 1/2/4 byte elements) ends with a 94f22ef01cSRoman Divacky /// nul value and contains no other nuls in it. 95f22ef01cSRoman Divacky static bool IsNullTerminatedString(const Constant *C) { 96f22ef01cSRoman Divacky const ArrayType *ATy = cast<ArrayType>(C->getType()); 97f22ef01cSRoman Divacky 98f22ef01cSRoman Divacky // First check: is we have constant array of i8 terminated with zero 99f22ef01cSRoman Divacky if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) { 100f22ef01cSRoman Divacky if (ATy->getNumElements() == 0) return false; 101f22ef01cSRoman Divacky 102f22ef01cSRoman Divacky ConstantInt *Null = 103f22ef01cSRoman Divacky dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1)); 104f22ef01cSRoman Divacky if (Null == 0 || Null->getZExtValue() != 0) 105f22ef01cSRoman Divacky return false; // Not null terminated. 106f22ef01cSRoman Divacky 107f22ef01cSRoman Divacky // Verify that the null doesn't occur anywhere else in the string. 108f22ef01cSRoman Divacky for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i) 109f22ef01cSRoman Divacky // Reject constantexpr elements etc. 110f22ef01cSRoman Divacky if (!isa<ConstantInt>(CVA->getOperand(i)) || 111f22ef01cSRoman Divacky CVA->getOperand(i) == Null) 112f22ef01cSRoman Divacky return false; 113f22ef01cSRoman Divacky return true; 114f22ef01cSRoman Divacky } 115f22ef01cSRoman Divacky 116f22ef01cSRoman Divacky // Another possibility: [1 x i8] zeroinitializer 117f22ef01cSRoman Divacky if (isa<ConstantAggregateZero>(C)) 118f22ef01cSRoman Divacky return ATy->getNumElements() == 1; 119f22ef01cSRoman Divacky 120f22ef01cSRoman Divacky return false; 121f22ef01cSRoman Divacky } 122f22ef01cSRoman Divacky 123f22ef01cSRoman Divacky /// getKindForGlobal - This is a top-level target-independent classifier for 124f22ef01cSRoman Divacky /// a global variable. Given an global variable and information from TM, it 125f22ef01cSRoman Divacky /// classifies the global in a variety of ways that make various target 126f22ef01cSRoman Divacky /// implementations simpler. The target implementation is free to ignore this 127f22ef01cSRoman Divacky /// extra info of course. 128f22ef01cSRoman Divacky SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, 129f22ef01cSRoman Divacky const TargetMachine &TM){ 130f22ef01cSRoman Divacky assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && 131f22ef01cSRoman Divacky "Can only be used for global definitions"); 132f22ef01cSRoman Divacky 133f22ef01cSRoman Divacky Reloc::Model ReloModel = TM.getRelocationModel(); 134f22ef01cSRoman Divacky 135f22ef01cSRoman Divacky // Early exit - functions should be always in text sections. 136f22ef01cSRoman Divacky const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 137f22ef01cSRoman Divacky if (GVar == 0) 138f22ef01cSRoman Divacky return SectionKind::getText(); 139f22ef01cSRoman Divacky 140f22ef01cSRoman Divacky // Handle thread-local data first. 141f22ef01cSRoman Divacky if (GVar->isThreadLocal()) { 142f22ef01cSRoman Divacky if (isSuitableForBSS(GVar)) 143f22ef01cSRoman Divacky return SectionKind::getThreadBSS(); 144f22ef01cSRoman Divacky return SectionKind::getThreadData(); 145f22ef01cSRoman Divacky } 146f22ef01cSRoman Divacky 147f22ef01cSRoman Divacky // Variables with common linkage always get classified as common. 148f22ef01cSRoman Divacky if (GVar->hasCommonLinkage()) 149f22ef01cSRoman Divacky return SectionKind::getCommon(); 150f22ef01cSRoman Divacky 151f22ef01cSRoman Divacky // Variable can be easily put to BSS section. 152f22ef01cSRoman Divacky if (isSuitableForBSS(GVar)) { 153f22ef01cSRoman Divacky if (GVar->hasLocalLinkage()) 154f22ef01cSRoman Divacky return SectionKind::getBSSLocal(); 155f22ef01cSRoman Divacky else if (GVar->hasExternalLinkage()) 156f22ef01cSRoman Divacky return SectionKind::getBSSExtern(); 157f22ef01cSRoman Divacky return SectionKind::getBSS(); 158f22ef01cSRoman Divacky } 159f22ef01cSRoman Divacky 160f22ef01cSRoman Divacky Constant *C = GVar->getInitializer(); 161f22ef01cSRoman Divacky 162f22ef01cSRoman Divacky // If the global is marked constant, we can put it into a mergable section, 163f22ef01cSRoman Divacky // a mergable string section, or general .data if it contains relocations. 164f22ef01cSRoman Divacky if (GVar->isConstant()) { 165f22ef01cSRoman Divacky // If the initializer for the global contains something that requires a 166f22ef01cSRoman Divacky // relocation, then we may have to drop this into a wriable data section 167f22ef01cSRoman Divacky // even though it is marked const. 168f22ef01cSRoman Divacky switch (C->getRelocationInfo()) { 169f22ef01cSRoman Divacky default: assert(0 && "unknown relocation info kind"); 170f22ef01cSRoman Divacky case Constant::NoRelocation: 171f22ef01cSRoman Divacky // If initializer is a null-terminated string, put it in a "cstring" 172f22ef01cSRoman Divacky // section of the right width. 173f22ef01cSRoman Divacky if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { 174f22ef01cSRoman Divacky if (const IntegerType *ITy = 175f22ef01cSRoman Divacky dyn_cast<IntegerType>(ATy->getElementType())) { 176f22ef01cSRoman Divacky if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || 177f22ef01cSRoman Divacky ITy->getBitWidth() == 32) && 178f22ef01cSRoman Divacky IsNullTerminatedString(C)) { 179f22ef01cSRoman Divacky if (ITy->getBitWidth() == 8) 180f22ef01cSRoman Divacky return SectionKind::getMergeable1ByteCString(); 181f22ef01cSRoman Divacky if (ITy->getBitWidth() == 16) 182f22ef01cSRoman Divacky return SectionKind::getMergeable2ByteCString(); 183f22ef01cSRoman Divacky 184f22ef01cSRoman Divacky assert(ITy->getBitWidth() == 32 && "Unknown width"); 185f22ef01cSRoman Divacky return SectionKind::getMergeable4ByteCString(); 186f22ef01cSRoman Divacky } 187f22ef01cSRoman Divacky } 188f22ef01cSRoman Divacky } 189f22ef01cSRoman Divacky 190f22ef01cSRoman Divacky // Otherwise, just drop it into a mergable constant section. If we have 191f22ef01cSRoman Divacky // a section for this size, use it, otherwise use the arbitrary sized 192f22ef01cSRoman Divacky // mergable section. 193f22ef01cSRoman Divacky switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { 194f22ef01cSRoman Divacky case 4: return SectionKind::getMergeableConst4(); 195f22ef01cSRoman Divacky case 8: return SectionKind::getMergeableConst8(); 196f22ef01cSRoman Divacky case 16: return SectionKind::getMergeableConst16(); 197f22ef01cSRoman Divacky default: return SectionKind::getMergeableConst(); 198f22ef01cSRoman Divacky } 199f22ef01cSRoman Divacky 200f22ef01cSRoman Divacky case Constant::LocalRelocation: 201f22ef01cSRoman Divacky // In static relocation model, the linker will resolve all addresses, so 202f22ef01cSRoman Divacky // the relocation entries will actually be constants by the time the app 203f22ef01cSRoman Divacky // starts up. However, we can't put this into a mergable section, because 204f22ef01cSRoman Divacky // the linker doesn't take relocations into consideration when it tries to 205f22ef01cSRoman Divacky // merge entries in the section. 206f22ef01cSRoman Divacky if (ReloModel == Reloc::Static) 207f22ef01cSRoman Divacky return SectionKind::getReadOnly(); 208f22ef01cSRoman Divacky 209f22ef01cSRoman Divacky // Otherwise, the dynamic linker needs to fix it up, put it in the 210f22ef01cSRoman Divacky // writable data.rel.local section. 211f22ef01cSRoman Divacky return SectionKind::getReadOnlyWithRelLocal(); 212f22ef01cSRoman Divacky 213f22ef01cSRoman Divacky case Constant::GlobalRelocations: 214f22ef01cSRoman Divacky // In static relocation model, the linker will resolve all addresses, so 215f22ef01cSRoman Divacky // the relocation entries will actually be constants by the time the app 216f22ef01cSRoman Divacky // starts up. However, we can't put this into a mergable section, because 217f22ef01cSRoman Divacky // the linker doesn't take relocations into consideration when it tries to 218f22ef01cSRoman Divacky // merge entries in the section. 219f22ef01cSRoman Divacky if (ReloModel == Reloc::Static) 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 228f22ef01cSRoman Divacky // Okay, this isn't a constant. If the initializer for the global is going 229f22ef01cSRoman Divacky // to require a runtime relocation by the dynamic linker, put it into a more 230f22ef01cSRoman Divacky // specific section to improve startup time of the app. This coalesces these 231f22ef01cSRoman Divacky // globals together onto fewer pages, improving the locality of the dynamic 232f22ef01cSRoman Divacky // linker. 233f22ef01cSRoman Divacky if (ReloModel == Reloc::Static) 234f22ef01cSRoman Divacky return SectionKind::getDataNoRel(); 235f22ef01cSRoman Divacky 236f22ef01cSRoman Divacky switch (C->getRelocationInfo()) { 237f22ef01cSRoman Divacky default: assert(0 && "unknown relocation info kind"); 238f22ef01cSRoman Divacky case Constant::NoRelocation: 239f22ef01cSRoman Divacky return SectionKind::getDataNoRel(); 240f22ef01cSRoman Divacky case Constant::LocalRelocation: 241f22ef01cSRoman Divacky return SectionKind::getDataRelLocal(); 242f22ef01cSRoman Divacky case Constant::GlobalRelocations: 243f22ef01cSRoman Divacky return SectionKind::getDataRel(); 244f22ef01cSRoman Divacky } 245f22ef01cSRoman Divacky } 246f22ef01cSRoman Divacky 247f22ef01cSRoman Divacky /// SectionForGlobal - This method computes the appropriate section to emit 248f22ef01cSRoman Divacky /// the specified global variable or function definition. This should not 249f22ef01cSRoman Divacky /// be passed external (or available externally) globals. 250f22ef01cSRoman Divacky const MCSection *TargetLoweringObjectFile:: 251f22ef01cSRoman Divacky SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, 252f22ef01cSRoman Divacky const TargetMachine &TM) const { 253f22ef01cSRoman Divacky // Select section name. 254f22ef01cSRoman Divacky if (GV->hasSection()) 255f22ef01cSRoman Divacky return getExplicitSectionGlobal(GV, Kind, Mang, TM); 256f22ef01cSRoman Divacky 257f22ef01cSRoman Divacky 258f22ef01cSRoman Divacky // Use default section depending on the 'type' of global 259f22ef01cSRoman Divacky return SelectSectionForGlobal(GV, Kind, Mang, TM); 260f22ef01cSRoman Divacky } 261f22ef01cSRoman Divacky 262f22ef01cSRoman Divacky 263f22ef01cSRoman Divacky // Lame default implementation. Calculate the section name for global. 264f22ef01cSRoman Divacky const MCSection * 265f22ef01cSRoman Divacky TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, 266f22ef01cSRoman Divacky SectionKind Kind, 267f22ef01cSRoman Divacky Mangler *Mang, 268f22ef01cSRoman Divacky const TargetMachine &TM) const{ 269f22ef01cSRoman Divacky assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 270f22ef01cSRoman Divacky 271f22ef01cSRoman Divacky if (Kind.isText()) 272f22ef01cSRoman Divacky return getTextSection(); 273f22ef01cSRoman Divacky 274f22ef01cSRoman Divacky if (Kind.isBSS() && BSSSection != 0) 275f22ef01cSRoman Divacky return BSSSection; 276f22ef01cSRoman Divacky 277f22ef01cSRoman Divacky if (Kind.isReadOnly() && ReadOnlySection != 0) 278f22ef01cSRoman Divacky return ReadOnlySection; 279f22ef01cSRoman Divacky 280f22ef01cSRoman Divacky return getDataSection(); 281f22ef01cSRoman Divacky } 282f22ef01cSRoman Divacky 283f22ef01cSRoman Divacky /// getSectionForConstant - Given a mergable constant with the 284f22ef01cSRoman Divacky /// specified size and relocation information, return a section that it 285f22ef01cSRoman Divacky /// should be placed in. 286f22ef01cSRoman Divacky const MCSection * 287f22ef01cSRoman Divacky TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { 288f22ef01cSRoman Divacky if (Kind.isReadOnly() && ReadOnlySection != 0) 289f22ef01cSRoman Divacky return ReadOnlySection; 290f22ef01cSRoman Divacky 291f22ef01cSRoman Divacky return DataSection; 292f22ef01cSRoman Divacky } 293f22ef01cSRoman Divacky 294f22ef01cSRoman Divacky /// getExprForDwarfGlobalReference - Return an MCExpr to use for a 295f22ef01cSRoman Divacky /// reference to the specified global variable from exception 296f22ef01cSRoman Divacky /// handling information. 297f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile:: 298f22ef01cSRoman Divacky getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang, 299f22ef01cSRoman Divacky MachineModuleInfo *MMI, unsigned Encoding, 300f22ef01cSRoman Divacky MCStreamer &Streamer) const { 301f22ef01cSRoman Divacky const MCSymbol *Sym = Mang->getSymbol(GV); 302f22ef01cSRoman Divacky return getExprForDwarfReference(Sym, Mang, MMI, Encoding, Streamer); 303f22ef01cSRoman Divacky } 304f22ef01cSRoman Divacky 305f22ef01cSRoman Divacky const MCExpr *TargetLoweringObjectFile:: 306f22ef01cSRoman Divacky getExprForDwarfReference(const MCSymbol *Sym, Mangler *Mang, 307f22ef01cSRoman Divacky MachineModuleInfo *MMI, unsigned Encoding, 308f22ef01cSRoman Divacky MCStreamer &Streamer) const { 309f22ef01cSRoman Divacky const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext()); 310f22ef01cSRoman Divacky 311f22ef01cSRoman Divacky switch (Encoding & 0xF0) { 312f22ef01cSRoman Divacky default: 313f22ef01cSRoman Divacky report_fatal_error("We do not support this DWARF encoding yet!"); 314f22ef01cSRoman Divacky case dwarf::DW_EH_PE_absptr: 315f22ef01cSRoman Divacky // Do nothing special 316f22ef01cSRoman Divacky return Res; 317f22ef01cSRoman Divacky case dwarf::DW_EH_PE_pcrel: { 318f22ef01cSRoman Divacky // Emit a label to the streamer for the current position. This gives us 319f22ef01cSRoman Divacky // .-foo addressing. 320f22ef01cSRoman Divacky MCSymbol *PCSym = getContext().CreateTempSymbol(); 321f22ef01cSRoman Divacky Streamer.EmitLabel(PCSym); 322f22ef01cSRoman Divacky const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext()); 323f22ef01cSRoman Divacky return MCBinaryExpr::CreateSub(Res, PC, getContext()); 324f22ef01cSRoman Divacky } 325f22ef01cSRoman Divacky } 326f22ef01cSRoman Divacky } 327f22ef01cSRoman Divacky 328f22ef01cSRoman Divacky unsigned TargetLoweringObjectFile::getPersonalityEncoding() const { 329f22ef01cSRoman Divacky return dwarf::DW_EH_PE_absptr; 330f22ef01cSRoman Divacky } 331f22ef01cSRoman Divacky 332f22ef01cSRoman Divacky unsigned TargetLoweringObjectFile::getLSDAEncoding() const { 333f22ef01cSRoman Divacky return dwarf::DW_EH_PE_absptr; 334f22ef01cSRoman Divacky } 335f22ef01cSRoman Divacky 336f22ef01cSRoman Divacky unsigned TargetLoweringObjectFile::getFDEEncoding() const { 337f22ef01cSRoman Divacky return dwarf::DW_EH_PE_absptr; 338f22ef01cSRoman Divacky } 339f22ef01cSRoman Divacky 340f22ef01cSRoman Divacky unsigned TargetLoweringObjectFile::getTTypeEncoding() const { 341f22ef01cSRoman Divacky return dwarf::DW_EH_PE_absptr; 342f22ef01cSRoman Divacky } 343f22ef01cSRoman Divacky 344