TargetLoweringObjectFile.cpp 16 KB

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