TargetLoweringObjectFile.cpp 12 KB

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