LLVM API Documentation
00001 //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements classes used to handle lowerings specific to common 00011 // object file formats. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Target/TargetLoweringObjectFile.h" 00016 #include "llvm/IR/Constants.h" 00017 #include "llvm/IR/DataLayout.h" 00018 #include "llvm/IR/DerivedTypes.h" 00019 #include "llvm/IR/Function.h" 00020 #include "llvm/IR/GlobalVariable.h" 00021 #include "llvm/MC/MCContext.h" 00022 #include "llvm/MC/MCExpr.h" 00023 #include "llvm/MC/MCStreamer.h" 00024 #include "llvm/MC/MCSymbol.h" 00025 #include "llvm/Support/Dwarf.h" 00026 #include "llvm/Support/ErrorHandling.h" 00027 #include "llvm/Support/raw_ostream.h" 00028 #include "llvm/Target/Mangler.h" 00029 #include "llvm/Target/TargetMachine.h" 00030 #include "llvm/Target/TargetOptions.h" 00031 using namespace llvm; 00032 00033 //===----------------------------------------------------------------------===// 00034 // Generic Code 00035 //===----------------------------------------------------------------------===// 00036 00037 /// Initialize - this method must be called before any actual lowering is 00038 /// done. This specifies the current context for codegen, and gives the 00039 /// lowering implementations a chance to set up their default sections. 00040 void TargetLoweringObjectFile::Initialize(MCContext &ctx, 00041 const TargetMachine &TM) { 00042 Ctx = &ctx; 00043 InitMCObjectFileInfo(TM.getTargetTriple(), 00044 TM.getRelocationModel(), TM.getCodeModel(), *Ctx); 00045 } 00046 00047 TargetLoweringObjectFile::~TargetLoweringObjectFile() { 00048 } 00049 00050 static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) { 00051 const Constant *C = GV->getInitializer(); 00052 00053 // Must have zero initializer. 00054 if (!C->isNullValue()) 00055 return false; 00056 00057 // Leave constant zeros in readonly constant sections, so they can be shared. 00058 if (GV->isConstant()) 00059 return false; 00060 00061 // If the global has an explicit section specified, don't put it in BSS. 00062 if (!GV->getSection().empty()) 00063 return false; 00064 00065 // If -nozero-initialized-in-bss is specified, don't ever use BSS. 00066 if (NoZerosInBSS) 00067 return false; 00068 00069 // Otherwise, put it in BSS! 00070 return true; 00071 } 00072 00073 /// IsNullTerminatedString - Return true if the specified constant (which is 00074 /// known to have a type that is an array of 1/2/4 byte elements) ends with a 00075 /// nul value and contains no other nuls in it. Note that this is more general 00076 /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings. 00077 static bool IsNullTerminatedString(const Constant *C) { 00078 // First check: is we have constant array terminated with zero 00079 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { 00080 unsigned NumElts = CDS->getNumElements(); 00081 assert(NumElts != 0 && "Can't have an empty CDS"); 00082 00083 if (CDS->getElementAsInteger(NumElts-1) != 0) 00084 return false; // Not null terminated. 00085 00086 // Verify that the null doesn't occur anywhere else in the string. 00087 for (unsigned i = 0; i != NumElts-1; ++i) 00088 if (CDS->getElementAsInteger(i) == 0) 00089 return false; 00090 return true; 00091 } 00092 00093 // Another possibility: [1 x i8] zeroinitializer 00094 if (isa<ConstantAggregateZero>(C)) 00095 return cast<ArrayType>(C->getType())->getNumElements() == 1; 00096 00097 return false; 00098 } 00099 00100 MCSymbol *TargetLoweringObjectFile:: 00101 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang, 00102 MachineModuleInfo *MMI) const { 00103 return Mang->getSymbol(GV); 00104 } 00105 00106 void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer, 00107 const TargetMachine &TM, 00108 const MCSymbol *Sym) const { 00109 } 00110 00111 00112 /// getKindForGlobal - This is a top-level target-independent classifier for 00113 /// a global variable. Given an global variable and information from TM, it 00114 /// classifies the global in a variety of ways that make various target 00115 /// implementations simpler. The target implementation is free to ignore this 00116 /// extra info of course. 00117 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, 00118 const TargetMachine &TM){ 00119 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && 00120 "Can only be used for global definitions"); 00121 00122 Reloc::Model ReloModel = TM.getRelocationModel(); 00123 00124 // Early exit - functions should be always in text sections. 00125 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 00126 if (GVar == 0) 00127 return SectionKind::getText(); 00128 00129 // Handle thread-local data first. 00130 if (GVar->isThreadLocal()) { 00131 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) 00132 return SectionKind::getThreadBSS(); 00133 return SectionKind::getThreadData(); 00134 } 00135 00136 // Variables with common linkage always get classified as common. 00137 if (GVar->hasCommonLinkage()) 00138 return SectionKind::getCommon(); 00139 00140 // Variable can be easily put to BSS section. 00141 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) { 00142 if (GVar->hasLocalLinkage()) 00143 return SectionKind::getBSSLocal(); 00144 else if (GVar->hasExternalLinkage()) 00145 return SectionKind::getBSSExtern(); 00146 return SectionKind::getBSS(); 00147 } 00148 00149 const Constant *C = GVar->getInitializer(); 00150 00151 // If the global is marked constant, we can put it into a mergable section, 00152 // a mergable string section, or general .data if it contains relocations. 00153 if (GVar->isConstant()) { 00154 // If the initializer for the global contains something that requires a 00155 // relocation, then we may have to drop this into a writable data section 00156 // even though it is marked const. 00157 switch (C->getRelocationInfo()) { 00158 case Constant::NoRelocation: 00159 // If the global is required to have a unique address, it can't be put 00160 // into a mergable section: just drop it into the general read-only 00161 // section instead. 00162 if (!GVar->hasUnnamedAddr()) 00163 return SectionKind::getReadOnly(); 00164 00165 // If initializer is a null-terminated string, put it in a "cstring" 00166 // section of the right width. 00167 if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { 00168 if (IntegerType *ITy = 00169 dyn_cast<IntegerType>(ATy->getElementType())) { 00170 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || 00171 ITy->getBitWidth() == 32) && 00172 IsNullTerminatedString(C)) { 00173 if (ITy->getBitWidth() == 8) 00174 return SectionKind::getMergeable1ByteCString(); 00175 if (ITy->getBitWidth() == 16) 00176 return SectionKind::getMergeable2ByteCString(); 00177 00178 assert(ITy->getBitWidth() == 32 && "Unknown width"); 00179 return SectionKind::getMergeable4ByteCString(); 00180 } 00181 } 00182 } 00183 00184 // Otherwise, just drop it into a mergable constant section. If we have 00185 // a section for this size, use it, otherwise use the arbitrary sized 00186 // mergable section. 00187 switch (TM.getDataLayout()->getTypeAllocSize(C->getType())) { 00188 case 4: return SectionKind::getMergeableConst4(); 00189 case 8: return SectionKind::getMergeableConst8(); 00190 case 16: return SectionKind::getMergeableConst16(); 00191 default: return SectionKind::getMergeableConst(); 00192 } 00193 00194 case Constant::LocalRelocation: 00195 // In static relocation model, the linker will resolve all addresses, so 00196 // the relocation entries will actually be constants by the time the app 00197 // starts up. However, we can't put this into a mergable section, because 00198 // the linker doesn't take relocations into consideration when it tries to 00199 // merge entries in the section. 00200 if (ReloModel == Reloc::Static) 00201 return SectionKind::getReadOnly(); 00202 00203 // Otherwise, the dynamic linker needs to fix it up, put it in the 00204 // writable data.rel.local section. 00205 return SectionKind::getReadOnlyWithRelLocal(); 00206 00207 case Constant::GlobalRelocations: 00208 // In static relocation model, the linker will resolve all addresses, so 00209 // the relocation entries will actually be constants by the time the app 00210 // starts up. However, we can't put this into a mergable section, because 00211 // the linker doesn't take relocations into consideration when it tries to 00212 // merge entries in the section. 00213 if (ReloModel == Reloc::Static) 00214 return SectionKind::getReadOnly(); 00215 00216 // Otherwise, the dynamic linker needs to fix it up, put it in the 00217 // writable data.rel section. 00218 return SectionKind::getReadOnlyWithRel(); 00219 } 00220 } 00221 00222 // Okay, this isn't a constant. If the initializer for the global is going 00223 // to require a runtime relocation by the dynamic linker, put it into a more 00224 // specific section to improve startup time of the app. This coalesces these 00225 // globals together onto fewer pages, improving the locality of the dynamic 00226 // linker. 00227 if (ReloModel == Reloc::Static) 00228 return SectionKind::getDataNoRel(); 00229 00230 switch (C->getRelocationInfo()) { 00231 case Constant::NoRelocation: 00232 return SectionKind::getDataNoRel(); 00233 case Constant::LocalRelocation: 00234 return SectionKind::getDataRelLocal(); 00235 case Constant::GlobalRelocations: 00236 return SectionKind::getDataRel(); 00237 } 00238 llvm_unreachable("Invalid relocation"); 00239 } 00240 00241 /// SectionForGlobal - This method computes the appropriate section to emit 00242 /// the specified global variable or function definition. This should not 00243 /// be passed external (or available externally) globals. 00244 const MCSection *TargetLoweringObjectFile:: 00245 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, 00246 const TargetMachine &TM) const { 00247 // Select section name. 00248 if (GV->hasSection()) 00249 return getExplicitSectionGlobal(GV, Kind, Mang, TM); 00250 00251 00252 // Use default section depending on the 'type' of global 00253 return SelectSectionForGlobal(GV, Kind, Mang, TM); 00254 } 00255 00256 00257 // Lame default implementation. Calculate the section name for global. 00258 const MCSection * 00259 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, 00260 SectionKind Kind, 00261 Mangler *Mang, 00262 const TargetMachine &TM) const{ 00263 assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 00264 00265 if (Kind.isText()) 00266 return getTextSection(); 00267 00268 if (Kind.isBSS() && BSSSection != 0) 00269 return BSSSection; 00270 00271 if (Kind.isReadOnly() && ReadOnlySection != 0) 00272 return ReadOnlySection; 00273 00274 return getDataSection(); 00275 } 00276 00277 /// getSectionForConstant - Given a mergable constant with the 00278 /// specified size and relocation information, return a section that it 00279 /// should be placed in. 00280 const MCSection * 00281 TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { 00282 if (Kind.isReadOnly() && ReadOnlySection != 0) 00283 return ReadOnlySection; 00284 00285 return DataSection; 00286 } 00287 00288 /// getTTypeGlobalReference - Return an MCExpr to use for a 00289 /// reference to the specified global variable from exception 00290 /// handling information. 00291 const MCExpr *TargetLoweringObjectFile:: 00292 getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang, 00293 MachineModuleInfo *MMI, unsigned Encoding, 00294 MCStreamer &Streamer) const { 00295 const MCSymbolRefExpr *Ref = 00296 MCSymbolRefExpr::Create(Mang->getSymbol(GV), getContext()); 00297 00298 return getTTypeReference(Ref, Encoding, Streamer); 00299 } 00300 00301 const MCExpr *TargetLoweringObjectFile:: 00302 getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, 00303 MCStreamer &Streamer) const { 00304 switch (Encoding & 0x70) { 00305 default: 00306 report_fatal_error("We do not support this DWARF encoding yet!"); 00307 case dwarf::DW_EH_PE_absptr: 00308 // Do nothing special 00309 return Sym; 00310 case dwarf::DW_EH_PE_pcrel: { 00311 // Emit a label to the streamer for the current position. This gives us 00312 // .-foo addressing. 00313 MCSymbol *PCSym = getContext().CreateTempSymbol(); 00314 Streamer.EmitLabel(PCSym); 00315 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext()); 00316 return MCBinaryExpr::CreateSub(Sym, PC, getContext()); 00317 } 00318 } 00319 }