TargetLoweringObjectFile.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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(TM.getTargetTriple(), TM.isPositionIndependent(), ctx,
  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. return SectionKind::getThreadBSS();
  189. return SectionKind::getThreadData();
  190. }
  191. // Variables with common linkage always get classified as common.
  192. if (GVar->hasCommonLinkage())
  193. return SectionKind::getCommon();
  194. // Most non-mergeable zero data can be put in the BSS section unless otherwise
  195. // specified.
  196. if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
  197. if (GVar->hasLocalLinkage())
  198. return SectionKind::getBSSLocal();
  199. else if (GVar->hasExternalLinkage())
  200. return SectionKind::getBSSExtern();
  201. return SectionKind::getBSS();
  202. }
  203. // If the global is marked constant, we can put it into a mergable section,
  204. // a mergable string section, or general .data if it contains relocations.
  205. if (GVar->isConstant()) {
  206. // If the initializer for the global contains something that requires a
  207. // relocation, then we may have to drop this into a writable data section
  208. // even though it is marked const.
  209. const Constant *C = GVar->getInitializer();
  210. if (!C->needsRelocation()) {
  211. // If the global is required to have a unique address, it can't be put
  212. // into a mergable section: just drop it into the general read-only
  213. // section instead.
  214. if (!GVar->hasGlobalUnnamedAddr())
  215. return SectionKind::getReadOnly();
  216. // If initializer is a null-terminated string, put it in a "cstring"
  217. // section of the right width.
  218. if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
  219. if (IntegerType *ITy =
  220. dyn_cast<IntegerType>(ATy->getElementType())) {
  221. if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
  222. ITy->getBitWidth() == 32) &&
  223. IsNullTerminatedString(C)) {
  224. if (ITy->getBitWidth() == 8)
  225. return SectionKind::getMergeable1ByteCString();
  226. if (ITy->getBitWidth() == 16)
  227. return SectionKind::getMergeable2ByteCString();
  228. assert(ITy->getBitWidth() == 32 && "Unknown width");
  229. return SectionKind::getMergeable4ByteCString();
  230. }
  231. }
  232. }
  233. // Otherwise, just drop it into a mergable constant section. If we have
  234. // a section for this size, use it, otherwise use the arbitrary sized
  235. // mergable section.
  236. switch (
  237. GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) {
  238. case 4: return SectionKind::getMergeableConst4();
  239. case 8: return SectionKind::getMergeableConst8();
  240. case 16: return SectionKind::getMergeableConst16();
  241. case 32: return SectionKind::getMergeableConst32();
  242. default:
  243. return SectionKind::getReadOnly();
  244. }
  245. } else {
  246. // In static, ROPI and RWPI relocation models, the linker will resolve
  247. // all addresses, so the relocation entries will actually be constants by
  248. // the time the app starts up. However, we can't put this into a
  249. // mergable section, because the linker doesn't take relocations into
  250. // consideration when it tries to merge entries in the section.
  251. Reloc::Model ReloModel = TM.getRelocationModel();
  252. if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
  253. ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI)
  254. return SectionKind::getReadOnly();
  255. // Otherwise, the dynamic linker needs to fix it up, put it in the
  256. // writable data.rel section.
  257. return SectionKind::getReadOnlyWithRel();
  258. }
  259. }
  260. // Okay, this isn't a constant.
  261. return SectionKind::getData();
  262. }
  263. /// This method computes the appropriate section to emit the specified global
  264. /// variable or function definition. This should not be passed external (or
  265. /// available externally) globals.
  266. MCSection *TargetLoweringObjectFile::SectionForGlobal(
  267. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  268. // Select section name.
  269. if (GO->hasSection())
  270. return getExplicitSectionGlobal(GO, Kind, TM);
  271. if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
  272. auto Attrs = GVar->getAttributes();
  273. if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) ||
  274. (Attrs.hasAttribute("data-section") && Kind.isData()) ||
  275. (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) ||
  276. (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())) {
  277. return getExplicitSectionGlobal(GO, Kind, TM);
  278. }
  279. }
  280. if (auto *F = dyn_cast<Function>(GO)) {
  281. if (F->hasFnAttribute("implicit-section-name"))
  282. return getExplicitSectionGlobal(GO, Kind, TM);
  283. }
  284. // Use default section depending on the 'type' of global
  285. return SelectSectionForGlobal(GO, Kind, TM);
  286. }
  287. /// This method computes the appropriate section to emit the specified global
  288. /// variable or function definition. This should not be passed external (or
  289. /// available externally) globals.
  290. MCSection *
  291. TargetLoweringObjectFile::SectionForGlobal(const GlobalObject *GO,
  292. const TargetMachine &TM) const {
  293. return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
  294. }
  295. MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
  296. const Function &F, const TargetMachine &TM) const {
  297. Align Alignment(1);
  298. return getSectionForConstant(F.getParent()->getDataLayout(),
  299. SectionKind::getReadOnly(), /*C=*/nullptr,
  300. Alignment);
  301. }
  302. bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
  303. bool UsesLabelDifference, const Function &F) const {
  304. // In PIC mode, we need to emit the jump table to the same section as the
  305. // function body itself, otherwise the label differences won't make sense.
  306. // FIXME: Need a better predicate for this: what about custom entries?
  307. if (UsesLabelDifference)
  308. return true;
  309. // We should also do if the section name is NULL or function is declared
  310. // in discardable section
  311. // FIXME: this isn't the right predicate, should be based on the MCSection
  312. // for the function.
  313. return F.isWeakForLinker();
  314. }
  315. /// Given a mergable constant with the specified size and relocation
  316. /// information, return a section that it should be placed in.
  317. MCSection *TargetLoweringObjectFile::getSectionForConstant(
  318. const DataLayout &DL, SectionKind Kind, const Constant *C,
  319. Align &Alignment) const {
  320. if (Kind.isReadOnly() && ReadOnlySection != nullptr)
  321. return ReadOnlySection;
  322. return DataSection;
  323. }
  324. MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
  325. const Function &F, const MachineBasicBlock &MBB,
  326. const TargetMachine &TM) const {
  327. return nullptr;
  328. }
  329. /// getTTypeGlobalReference - Return an MCExpr to use for a
  330. /// reference to the specified global variable from exception
  331. /// handling information.
  332. const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
  333. const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
  334. MachineModuleInfo *MMI, MCStreamer &Streamer) const {
  335. const MCSymbolRefExpr *Ref =
  336. MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
  337. return getTTypeReference(Ref, Encoding, Streamer);
  338. }
  339. const MCExpr *TargetLoweringObjectFile::
  340. getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
  341. MCStreamer &Streamer) const {
  342. switch (Encoding & 0x70) {
  343. default:
  344. report_fatal_error("We do not support this DWARF encoding yet!");
  345. case dwarf::DW_EH_PE_absptr:
  346. // Do nothing special
  347. return Sym;
  348. case dwarf::DW_EH_PE_pcrel: {
  349. // Emit a label to the streamer for the current position. This gives us
  350. // .-foo addressing.
  351. MCSymbol *PCSym = getContext().createTempSymbol();
  352. Streamer.emitLabel(PCSym);
  353. const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
  354. return MCBinaryExpr::createSub(Sym, PC, getContext());
  355. }
  356. }
  357. }
  358. const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
  359. // FIXME: It's not clear what, if any, default this should have - perhaps a
  360. // null return could mean 'no location' & we should just do that here.
  361. return MCSymbolRefExpr::create(Sym, getContext());
  362. }
  363. void TargetLoweringObjectFile::getNameWithPrefix(
  364. SmallVectorImpl<char> &OutName, const GlobalValue *GV,
  365. const TargetMachine &TM) const {
  366. Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
  367. }