ModuleSymbolTable.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //===- ModuleSymbolTable.cpp - symbol table for in-memory IR --------------===//
  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 class represents a symbol table built from in-memory IR. It provides
  10. // access to GlobalValues and should only be used if such access is required
  11. // (e.g. in the LTO implementation).
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Object/ModuleSymbolTable.h"
  15. #include "RecordStreamer.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/IR/GlobalAlias.h"
  22. #include "llvm/IR/GlobalValue.h"
  23. #include "llvm/IR/GlobalVariable.h"
  24. #include "llvm/IR/InlineAsm.h"
  25. #include "llvm/IR/Module.h"
  26. #include "llvm/MC/MCAsmInfo.h"
  27. #include "llvm/MC/MCContext.h"
  28. #include "llvm/MC/MCInstrInfo.h"
  29. #include "llvm/MC/MCObjectFileInfo.h"
  30. #include "llvm/MC/MCParser/MCAsmParser.h"
  31. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  32. #include "llvm/MC/MCRegisterInfo.h"
  33. #include "llvm/MC/MCSubtargetInfo.h"
  34. #include "llvm/MC/MCSymbol.h"
  35. #include "llvm/MC/MCTargetOptions.h"
  36. #include "llvm/MC/TargetRegistry.h"
  37. #include "llvm/Object/SymbolicFile.h"
  38. #include "llvm/Support/Casting.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/MemoryBuffer.h"
  41. #include "llvm/Support/SMLoc.h"
  42. #include "llvm/Support/SourceMgr.h"
  43. #include "llvm/Support/raw_ostream.h"
  44. #include <algorithm>
  45. #include <cassert>
  46. #include <cstdint>
  47. #include <memory>
  48. #include <string>
  49. using namespace llvm;
  50. using namespace object;
  51. void ModuleSymbolTable::addModule(Module *M) {
  52. if (FirstMod)
  53. assert(FirstMod->getTargetTriple() == M->getTargetTriple());
  54. else
  55. FirstMod = M;
  56. for (GlobalValue &GV : M->global_values())
  57. SymTab.push_back(&GV);
  58. CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) {
  59. SymTab.push_back(new (AsmSymbols.Allocate())
  60. AsmSymbol(std::string(Name), Flags));
  61. });
  62. }
  63. static void
  64. initializeRecordStreamer(const Module &M,
  65. function_ref<void(RecordStreamer &)> Init) {
  66. StringRef InlineAsm = M.getModuleInlineAsm();
  67. if (InlineAsm.empty())
  68. return;
  69. std::string Err;
  70. const Triple TT(M.getTargetTriple());
  71. const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
  72. assert(T && T->hasMCAsmParser());
  73. std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
  74. if (!MRI)
  75. return;
  76. MCTargetOptions MCOptions;
  77. std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str(), MCOptions));
  78. if (!MAI)
  79. return;
  80. std::unique_ptr<MCSubtargetInfo> STI(
  81. T->createMCSubtargetInfo(TT.str(), "", ""));
  82. if (!STI)
  83. return;
  84. std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
  85. if (!MCII)
  86. return;
  87. std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
  88. SourceMgr SrcMgr;
  89. SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
  90. MCContext MCCtx(TT, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
  91. std::unique_ptr<MCObjectFileInfo> MOFI(
  92. T->createMCObjectFileInfo(MCCtx, /*PIC=*/false));
  93. MOFI->setSDKVersion(M.getSDKVersion());
  94. MCCtx.setObjectFileInfo(MOFI.get());
  95. RecordStreamer Streamer(MCCtx, M);
  96. T->createNullTargetStreamer(Streamer);
  97. std::unique_ptr<MCAsmParser> Parser(
  98. createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
  99. std::unique_ptr<MCTargetAsmParser> TAP(
  100. T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
  101. if (!TAP)
  102. return;
  103. // Module-level inline asm is assumed to use At&t syntax (see
  104. // AsmPrinter::doInitialization()).
  105. Parser->setAssemblerDialect(InlineAsm::AD_ATT);
  106. Parser->setTargetParser(*TAP);
  107. if (Parser->Run(false))
  108. return;
  109. Init(Streamer);
  110. }
  111. void ModuleSymbolTable::CollectAsmSymbols(
  112. const Module &M,
  113. function_ref<void(StringRef, BasicSymbolRef::Flags)> AsmSymbol) {
  114. initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
  115. Streamer.flushSymverDirectives();
  116. for (auto &KV : Streamer) {
  117. StringRef Key = KV.first();
  118. RecordStreamer::State Value = KV.second;
  119. // FIXME: For now we just assume that all asm symbols are executable.
  120. uint32_t Res = BasicSymbolRef::SF_Executable;
  121. switch (Value) {
  122. case RecordStreamer::NeverSeen:
  123. llvm_unreachable("NeverSeen should have been replaced earlier");
  124. case RecordStreamer::DefinedGlobal:
  125. Res |= BasicSymbolRef::SF_Global;
  126. break;
  127. case RecordStreamer::Defined:
  128. break;
  129. case RecordStreamer::Global:
  130. case RecordStreamer::Used:
  131. Res |= BasicSymbolRef::SF_Undefined;
  132. Res |= BasicSymbolRef::SF_Global;
  133. break;
  134. case RecordStreamer::DefinedWeak:
  135. Res |= BasicSymbolRef::SF_Weak;
  136. Res |= BasicSymbolRef::SF_Global;
  137. break;
  138. case RecordStreamer::UndefinedWeak:
  139. Res |= BasicSymbolRef::SF_Weak;
  140. Res |= BasicSymbolRef::SF_Undefined;
  141. }
  142. AsmSymbol(Key, BasicSymbolRef::Flags(Res));
  143. }
  144. });
  145. }
  146. void ModuleSymbolTable::CollectAsmSymvers(
  147. const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) {
  148. initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
  149. for (auto &KV : Streamer.symverAliases())
  150. for (auto &Alias : KV.second)
  151. AsmSymver(KV.first->getName(), Alias);
  152. });
  153. }
  154. void ModuleSymbolTable::printSymbolName(raw_ostream &OS, Symbol S) const {
  155. if (S.is<AsmSymbol *>()) {
  156. OS << S.get<AsmSymbol *>()->first;
  157. return;
  158. }
  159. auto *GV = S.get<GlobalValue *>();
  160. if (GV->hasDLLImportStorageClass())
  161. OS << "__imp_";
  162. Mang.getNameWithPrefix(OS, GV, false);
  163. }
  164. uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S) const {
  165. if (S.is<AsmSymbol *>())
  166. return S.get<AsmSymbol *>()->second;
  167. auto *GV = S.get<GlobalValue *>();
  168. uint32_t Res = BasicSymbolRef::SF_None;
  169. if (GV->isDeclarationForLinker())
  170. Res |= BasicSymbolRef::SF_Undefined;
  171. else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
  172. Res |= BasicSymbolRef::SF_Hidden;
  173. if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
  174. if (GVar->isConstant())
  175. Res |= BasicSymbolRef::SF_Const;
  176. }
  177. if (const GlobalObject *GO = GV->getAliaseeObject())
  178. if (isa<Function>(GO) || isa<GlobalIFunc>(GO))
  179. Res |= BasicSymbolRef::SF_Executable;
  180. if (isa<GlobalAlias>(GV))
  181. Res |= BasicSymbolRef::SF_Indirect;
  182. if (GV->hasPrivateLinkage())
  183. Res |= BasicSymbolRef::SF_FormatSpecific;
  184. if (!GV->hasLocalLinkage())
  185. Res |= BasicSymbolRef::SF_Global;
  186. if (GV->hasCommonLinkage())
  187. Res |= BasicSymbolRef::SF_Common;
  188. if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
  189. GV->hasExternalWeakLinkage())
  190. Res |= BasicSymbolRef::SF_Weak;
  191. if (GV->getName().startswith("llvm."))
  192. Res |= BasicSymbolRef::SF_FormatSpecific;
  193. else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
  194. if (Var->getSection() == "llvm.metadata")
  195. Res |= BasicSymbolRef::SF_FormatSpecific;
  196. }
  197. return Res;
  198. }