TargetLoweringObjectFile.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements classes used to handle lowerings specific to common
  10. // object file formats.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Target/TargetLoweringObjectFile.h"
  14. #include "llvm/BinaryFormat/Dwarf.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/DataLayout.h"
  17. #include "llvm/IR/DerivedTypes.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/GlobalVariable.h"
  20. #include "llvm/IR/Mangler.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/MC/MCAsmInfo.h"
  23. #include "llvm/MC/MCContext.h"
  24. #include "llvm/MC/MCExpr.h"
  25. #include "llvm/MC/MCStreamer.h"
  26. #include "llvm/MC/SectionKind.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Target/TargetMachine.h"
  29. #include "llvm/Target/TargetOptions.h"
  30. using namespace llvm;
  31. //===----------------------------------------------------------------------===//
  32. // Generic Code
  33. //===----------------------------------------------------------------------===//
  34. /// Initialize - this method must be called before any actual lowering is
  35. /// done. This specifies the current context for codegen, and gives the
  36. /// lowering implementations a chance to set up their default sections.
  37. void TargetLoweringObjectFile::Initialize(MCContext &ctx,
  38. const TargetMachine &TM) {
  39. // `Initialize` can be called more than once.
  40. delete Mang;
  41. Mang = new Mangler();
  42. initMCObjectFileInfo(ctx, TM.isPositionIndependent(),
  43. TM.getCodeModel() == CodeModel::Large);
  44. // Reset various EH DWARF encodings.
  45. PersonalityEncoding = LSDAEncoding = TTypeEncoding = dwarf::DW_EH_PE_absptr;
  46. CallSiteEncoding = dwarf::DW_EH_PE_uleb128;
  47. this->TM = &TM;
  48. }
  49. TargetLoweringObjectFile::~TargetLoweringObjectFile() {
  50. delete Mang;
  51. }
  52. unsigned TargetLoweringObjectFile::getCallSiteEncoding() const {
  53. // If target does not have LEB128 directives, we would need the
  54. // call site encoding to be udata4 so that the alternative path
  55. // for not having LEB128 directives could work.
  56. if (!getContext().getAsmInfo()->hasLEB128Directives())
  57. return dwarf::DW_EH_PE_udata4;
  58. return CallSiteEncoding;
  59. }
  60. static bool isNullOrUndef(const Constant *C) {
  61. // Check that the constant isn't all zeros or undefs.
  62. if (C->isNullValue() || isa<UndefValue>(C))
  63. return true;
  64. if (!isa<ConstantAggregate>(C))
  65. return false;
  66. for (const auto *Operand : C->operand_values()) {
  67. if (!isNullOrUndef(cast<Constant>(Operand)))
  68. return false;
  69. }
  70. return true;
  71. }
  72. static bool isSuitableForBSS(const GlobalVariable *GV) {
  73. const Constant *C = GV->getInitializer();
  74. // Must have zero initializer.
  75. if (!isNullOrUndef(C))
  76. return false;
  77. // Leave constant zeros in readonly constant sections, so they can be shared.
  78. if (GV->isConstant())
  79. return false;
  80. // If the global has an explicit section specified, don't put it in BSS.
  81. if (GV->hasSection())
  82. return false;
  83. // Otherwise, put it in BSS!
  84. return true;
  85. }
  86. /// IsNullTerminatedString - Return true if the specified constant (which is
  87. /// known to have a type that is an array of 1/2/4 byte elements) ends with a
  88. /// nul value and contains no other nuls in it. Note that this is more general
  89. /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
  90. static bool IsNullTerminatedString(const Constant *C) {
  91. // First check: is we have constant array terminated with zero
  92. if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
  93. unsigned NumElts = CDS->getNumElements();
  94. assert(NumElts != 0 && "Can't have an empty CDS");
  95. if (CDS->getElementAsInteger(NumElts-1) != 0)
  96. return false; // Not null terminated.
  97. // Verify that the null doesn't occur anywhere else in the string.
  98. for (unsigned i = 0; i != NumElts-1; ++i)
  99. if (CDS->getElementAsInteger(i) == 0)
  100. return false;
  101. return true;
  102. }
  103. // Another possibility: [1 x i8] zeroinitializer
  104. if (isa<ConstantAggregateZero>(C))
  105. return cast<ArrayType>(C->getType())->getNumElements() == 1;
  106. return false;
  107. }
  108. MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase(
  109. const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const {
  110. assert(!Suffix.empty());
  111. SmallString<60> NameStr;
  112. NameStr += GV->getParent()->getDataLayout().getPrivateGlobalPrefix();
  113. TM.getNameWithPrefix(NameStr, GV, *Mang);
  114. NameStr.append(Suffix.begin(), Suffix.end());
  115. return getContext().getOrCreateSymbol(NameStr);
  116. }
  117. MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol(
  118. const GlobalValue *GV, const TargetMachine &TM,
  119. MachineModuleInfo *MMI) const {
  120. return TM.getSymbol(GV);
  121. }
  122. void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
  123. const DataLayout &,
  124. const MCSymbol *Sym) const {
  125. }
  126. void TargetLoweringObjectFile::emitCGProfileMetadata(MCStreamer &Streamer,
  127. Module &M) const {
  128. MCContext &C = getContext();
  129. SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
  130. M.getModuleFlagsMetadata(ModuleFlags);
  131. MDNode *CFGProfile = nullptr;
  132. for (const auto &MFE : ModuleFlags) {
  133. StringRef Key = MFE.Key->getString();
  134. if (Key == "CG Profile") {
  135. CFGProfile = cast<MDNode>(MFE.Val);
  136. break;
  137. }
  138. }
  139. if (!CFGProfile)
  140. return;
  141. auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
  142. if (!MDO)
  143. return nullptr;
  144. auto *V = cast<ValueAsMetadata>(MDO);
  145. const Function *F = cast<Function>(V->getValue()->stripPointerCasts());
  146. if (F->hasDLLImportStorageClass())
  147. return nullptr;
  148. return TM->getSymbol(F);
  149. };
  150. for (const auto &Edge : CFGProfile->operands()) {
  151. MDNode *E = cast<MDNode>(Edge);
  152. const MCSymbol *From = GetSym(E->getOperand(0));
  153. const MCSymbol *To = GetSym(E->getOperand(1));
  154. // Skip null functions. This can happen if functions are dead stripped after
  155. // the CGProfile pass has been run.
  156. if (!From || !To)
  157. continue;
  158. uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
  159. ->getValue()
  160. ->getUniqueInteger()
  161. .getZExtValue();
  162. Streamer.emitCGProfileEntry(
  163. MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
  164. MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
  165. }
  166. }
  167. /// getKindForGlobal - This is a top-level target-independent classifier for
  168. /// a global object. Given a global variable and information from the TM, this
  169. /// function classifies the global in a target independent manner. This function
  170. /// may be overridden by the target implementation.
  171. SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
  172. const TargetMachine &TM){
  173. assert(!GO->isDeclarationForLinker() &&
  174. "Can only be used for global definitions");
  175. // Functions are classified as text sections.
  176. if (isa<Function>(GO))
  177. return SectionKind::getText();
  178. // Basic blocks are classified as text sections.
  179. if (isa<BasicBlock>(GO))
  180. return SectionKind::getText();
  181. // Global variables require more detailed analysis.
  182. const auto *GVar = cast<GlobalVariable>(GO);
  183. // Handle thread-local data first.
  184. if (GVar->isThreadLocal()) {
  185. if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
  186. // Zero-initialized TLS variables with local linkage always get classified
  187. // as ThreadBSSLocal.
  188. if (GVar->hasLocalLinkage()) {
  189. return SectionKind::getThreadBSSLocal();
  190. }
  191. return SectionKind::getThreadBSS();
  192. }
  193. return SectionKind::getThreadData();
  194. }
  195. // Variables with common linkage always get classified as common.
  196. if (GVar->hasCommonLinkage())
  197. return SectionKind::getCommon();
  198. // Most non-mergeable zero data can be put in the BSS section unless otherwise
  199. // specified.
  200. if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
  201. if (GVar->hasLocalLinkage())
  202. return SectionKind::getBSSLocal();
  203. else if (GVar->hasExternalLinkage())
  204. return SectionKind::getBSSExtern();
  205. return SectionKind::getBSS();
  206. }
  207. // Global variables with '!exclude' should get the exclude section kind if
  208. // they have an explicit section and no other metadata.
  209. if (GVar->hasSection())
  210. if (MDNode *MD = GVar->getMetadata(LLVMContext::MD_exclude))
  211. if (!MD->getNumOperands())
  212. return SectionKind::getExclude();
  213. // If the global is marked constant, we can put it into a mergable section,
  214. // a mergable string section, or general .data if it contains relocations.
  215. if (GVar->isConstant()) {
  216. // If the initializer for the global contains something that requires a
  217. // relocation, then we may have to drop this into a writable data section
  218. // even though it is marked const.
  219. const Constant *C = GVar->getInitializer();
  220. if (!C->needsRelocation()) {
  221. // If the global is required to have a unique address, it can't be put
  222. // into a mergable section: just drop it into the general read-only
  223. // section instead.
  224. if (!GVar->hasGlobalUnnamedAddr())
  225. return SectionKind::getReadOnly();
  226. // If initializer is a null-terminated string, put it in a "cstring"
  227. // section of the right width.
  228. if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
  229. if (IntegerType *ITy =
  230. dyn_cast<IntegerType>(ATy->getElementType())) {
  231. if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
  232. ITy->getBitWidth() == 32) &&
  233. IsNullTerminatedString(C)) {
  234. if (ITy->getBitWidth() == 8)
  235. return SectionKind::getMergeable1ByteCString();
  236. if (ITy->getBitWidth() == 16)
  237. return SectionKind::getMergeable2ByteCString();
  238. assert(ITy->getBitWidth() == 32 && "Unknown width");
  239. return SectionKind::getMergeable4ByteCString();
  240. }
  241. }
  242. }
  243. // Otherwise, just drop it into a mergable constant section. If we have
  244. // a section for this size, use it, otherwise use the arbitrary sized
  245. // mergable section.
  246. switch (
  247. GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) {
  248. case 4: return SectionKind::getMergeableConst4();
  249. case 8: return SectionKind::getMergeableConst8();
  250. case 16: return SectionKind::getMergeableConst16();
  251. case 32: return SectionKind::getMergeableConst32();
  252. default:
  253. return SectionKind::getReadOnly();
  254. }
  255. } else {
  256. // In static, ROPI and RWPI relocation models, the linker will resolve
  257. // all addresses, so the relocation entries will actually be constants by
  258. // the time the app starts up. However, we can't put this into a
  259. // mergable section, because the linker doesn't take relocations into
  260. // consideration when it tries to merge entries in the section.
  261. Reloc::Model ReloModel = TM.getRelocationModel();
  262. if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
  263. ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
  264. !C->needsDynamicRelocation())
  265. return SectionKind::getReadOnly();
  266. // Otherwise, the dynamic linker needs to fix it up, put it in the
  267. // writable data.rel section.
  268. return SectionKind::getReadOnlyWithRel();
  269. }
  270. }
  271. // Okay, this isn't a constant.
  272. return SectionKind::getData();
  273. }
  274. /// This method computes the appropriate section to emit the specified global
  275. /// variable or function definition. This should not be passed external (or
  276. /// available externally) globals.
  277. MCSection *TargetLoweringObjectFile::SectionForGlobal(
  278. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  279. // Select section name.
  280. if (GO->hasSection())
  281. return getExplicitSectionGlobal(GO, Kind, TM);
  282. if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
  283. auto Attrs = GVar->getAttributes();
  284. if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) ||
  285. (Attrs.hasAttribute("data-section") && Kind.isData()) ||
  286. (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) ||
  287. (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())) {
  288. return getExplicitSectionGlobal(GO, Kind, TM);
  289. }
  290. }
  291. if (auto *F = dyn_cast<Function>(GO)) {
  292. if (F->hasFnAttribute("implicit-section-name"))
  293. return getExplicitSectionGlobal(GO, Kind, TM);
  294. }
  295. // Use default section depending on the 'type' of global
  296. return SelectSectionForGlobal(GO, Kind, TM);
  297. }
  298. /// This method computes the appropriate section to emit the specified global
  299. /// variable or function definition. This should not be passed external (or
  300. /// available externally) globals.
  301. MCSection *
  302. TargetLoweringObjectFile::SectionForGlobal(const GlobalObject *GO,
  303. const TargetMachine &TM) const {
  304. return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
  305. }
  306. MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
  307. const Function &F, const TargetMachine &TM) const {
  308. Align Alignment(1);
  309. return getSectionForConstant(F.getParent()->getDataLayout(),
  310. SectionKind::getReadOnly(), /*C=*/nullptr,
  311. Alignment);
  312. }
  313. bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
  314. bool UsesLabelDifference, const Function &F) const {
  315. // In PIC mode, we need to emit the jump table to the same section as the
  316. // function body itself, otherwise the label differences won't make sense.
  317. // FIXME: Need a better predicate for this: what about custom entries?
  318. if (UsesLabelDifference)
  319. return true;
  320. // We should also do if the section name is NULL or function is declared
  321. // in discardable section
  322. // FIXME: this isn't the right predicate, should be based on the MCSection
  323. // for the function.
  324. return F.isWeakForLinker();
  325. }
  326. /// Given a mergable constant with the specified size and relocation
  327. /// information, return a section that it should be placed in.
  328. MCSection *TargetLoweringObjectFile::getSectionForConstant(
  329. const DataLayout &DL, SectionKind Kind, const Constant *C,
  330. Align &Alignment) const {
  331. if (Kind.isReadOnly() && ReadOnlySection != nullptr)
  332. return ReadOnlySection;
  333. return DataSection;
  334. }
  335. MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
  336. const Function &F, const MachineBasicBlock &MBB,
  337. const TargetMachine &TM) const {
  338. return nullptr;
  339. }
  340. MCSection *TargetLoweringObjectFile::getUniqueSectionForFunction(
  341. const Function &F, const TargetMachine &TM) const {
  342. return nullptr;
  343. }
  344. /// getTTypeGlobalReference - Return an MCExpr to use for a
  345. /// reference to the specified global variable from exception
  346. /// handling information.
  347. const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
  348. const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
  349. MachineModuleInfo *MMI, MCStreamer &Streamer) const {
  350. const MCSymbolRefExpr *Ref =
  351. MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
  352. return getTTypeReference(Ref, Encoding, Streamer);
  353. }
  354. const MCExpr *TargetLoweringObjectFile::
  355. getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
  356. MCStreamer &Streamer) const {
  357. switch (Encoding & 0x70) {
  358. default:
  359. report_fatal_error("We do not support this DWARF encoding yet!");
  360. case dwarf::DW_EH_PE_absptr:
  361. // Do nothing special
  362. return Sym;
  363. case dwarf::DW_EH_PE_pcrel: {
  364. // Emit a label to the streamer for the current position. This gives us
  365. // .-foo addressing.
  366. MCSymbol *PCSym = getContext().createTempSymbol();
  367. Streamer.emitLabel(PCSym);
  368. const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
  369. return MCBinaryExpr::createSub(Sym, PC, getContext());
  370. }
  371. }
  372. }
  373. const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
  374. // FIXME: It's not clear what, if any, default this should have - perhaps a
  375. // null return could mean 'no location' & we should just do that here.
  376. return MCSymbolRefExpr::create(Sym, getContext());
  377. }
  378. void TargetLoweringObjectFile::getNameWithPrefix(
  379. SmallVectorImpl<char> &OutName, const GlobalValue *GV,
  380. const TargetMachine &TM) const {
  381. Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
  382. }