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