1 //===- ModuleSymbolTable.cpp - symbol table for in-memory IR --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class represents a symbol table built from in-memory IR. It provides
11 // access to GlobalValues and should only be used if such access is required
12 // (e.g. in the LTO implementation).
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Object/ModuleSymbolTable.h"
17 #include "RecordStreamer.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/MCDirectives.h"
31 #include "llvm/MC/MCInstrInfo.h"
32 #include "llvm/MC/MCObjectFileInfo.h"
33 #include "llvm/MC/MCParser/MCAsmParser.h"
34 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/MC/MCSubtargetInfo.h"
37 #include "llvm/MC/MCSymbol.h"
38 #include "llvm/MC/MCTargetOptions.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/TargetRegistry.h"
47 #include "llvm/Support/raw_ostream.h"
55 using namespace object;
57 void ModuleSymbolTable::addModule(Module *M) {
59 assert(FirstMod->getTargetTriple() == M->getTargetTriple());
63 for (GlobalValue &GV : M->global_values())
64 SymTab.push_back(&GV);
66 CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) {
67 SymTab.push_back(new (AsmSymbols.Allocate()) AsmSymbol(Name, Flags));
71 void ModuleSymbolTable::CollectAsmSymbols(
73 function_ref<void(StringRef, BasicSymbolRef::Flags)> AsmSymbol) {
74 StringRef InlineAsm = M.getModuleInlineAsm();
75 if (InlineAsm.empty())
79 const Triple TT(M.getTargetTriple());
80 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
81 assert(T && T->hasMCAsmParser());
83 std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
87 std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
91 std::unique_ptr<MCSubtargetInfo> STI(
92 T->createMCSubtargetInfo(TT.str(), "", ""));
96 std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
100 MCObjectFileInfo MOFI;
101 MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
102 MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, MCCtx);
103 RecordStreamer Streamer(MCCtx, M);
104 T->createNullTargetStreamer(Streamer);
106 std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
108 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
109 std::unique_ptr<MCAsmParser> Parser(
110 createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
112 MCTargetOptions MCOptions;
113 std::unique_ptr<MCTargetAsmParser> TAP(
114 T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
118 Parser->setTargetParser(*TAP);
119 if (Parser->Run(false))
122 Streamer.flushSymverDirectives();
124 for (auto &KV : Streamer) {
125 StringRef Key = KV.first();
126 RecordStreamer::State Value = KV.second;
127 // FIXME: For now we just assume that all asm symbols are executable.
128 uint32_t Res = BasicSymbolRef::SF_Executable;
130 case RecordStreamer::NeverSeen:
131 llvm_unreachable("NeverSeen should have been replaced earlier");
132 case RecordStreamer::DefinedGlobal:
133 Res |= BasicSymbolRef::SF_Global;
135 case RecordStreamer::Defined:
137 case RecordStreamer::Global:
138 case RecordStreamer::Used:
139 Res |= BasicSymbolRef::SF_Undefined;
140 Res |= BasicSymbolRef::SF_Global;
142 case RecordStreamer::DefinedWeak:
143 Res |= BasicSymbolRef::SF_Weak;
144 Res |= BasicSymbolRef::SF_Global;
146 case RecordStreamer::UndefinedWeak:
147 Res |= BasicSymbolRef::SF_Weak;
148 Res |= BasicSymbolRef::SF_Undefined;
150 AsmSymbol(Key, BasicSymbolRef::Flags(Res));
154 void ModuleSymbolTable::printSymbolName(raw_ostream &OS, Symbol S) const {
155 if (S.is<AsmSymbol *>()) {
156 OS << S.get<AsmSymbol *>()->first;
160 auto *GV = S.get<GlobalValue *>();
161 if (GV->hasDLLImportStorageClass())
164 Mang.getNameWithPrefix(OS, GV, false);
167 uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S) const {
168 if (S.is<AsmSymbol *>())
169 return S.get<AsmSymbol *>()->second;
171 auto *GV = S.get<GlobalValue *>();
173 uint32_t Res = BasicSymbolRef::SF_None;
174 if (GV->isDeclarationForLinker())
175 Res |= BasicSymbolRef::SF_Undefined;
176 else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
177 Res |= BasicSymbolRef::SF_Hidden;
178 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
179 if (GVar->isConstant())
180 Res |= BasicSymbolRef::SF_Const;
182 if (dyn_cast_or_null<Function>(GV->getBaseObject()))
183 Res |= BasicSymbolRef::SF_Executable;
184 if (isa<GlobalAlias>(GV))
185 Res |= BasicSymbolRef::SF_Indirect;
186 if (GV->hasPrivateLinkage())
187 Res |= BasicSymbolRef::SF_FormatSpecific;
188 if (!GV->hasLocalLinkage())
189 Res |= BasicSymbolRef::SF_Global;
190 if (GV->hasCommonLinkage())
191 Res |= BasicSymbolRef::SF_Common;
192 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
193 GV->hasExternalWeakLinkage())
194 Res |= BasicSymbolRef::SF_Weak;
196 if (GV->getName().startswith("llvm."))
197 Res |= BasicSymbolRef::SF_FormatSpecific;
198 else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
199 if (Var->getSection() == "llvm.metadata")
200 Res |= BasicSymbolRef::SF_FormatSpecific;