ModuleSymbolTable.cpp 7.2 KB

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