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 | |
15 | #include "llvm/Object/ModuleSymbolTable.h" |
16 | #include "RecordStreamer.h" |
17 | #include "llvm/ADT/StringRef.h" |
18 | #include "llvm/IR/DiagnosticInfo.h" |
19 | #include "llvm/IR/Function.h" |
20 | #include "llvm/IR/GlobalAlias.h" |
21 | #include "llvm/IR/GlobalValue.h" |
22 | #include "llvm/IR/GlobalVariable.h" |
23 | #include "llvm/IR/InlineAsm.h" |
24 | #include "llvm/IR/Module.h" |
25 | #include "llvm/MC/MCAsmInfo.h" |
26 | #include "llvm/MC/MCContext.h" |
27 | #include "llvm/MC/MCInstrInfo.h" |
28 | #include "llvm/MC/MCObjectFileInfo.h" |
29 | #include "llvm/MC/MCParser/MCAsmParser.h" |
30 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
31 | #include "llvm/MC/MCRegisterInfo.h" |
32 | #include "llvm/MC/MCSubtargetInfo.h" |
33 | #include "llvm/MC/MCSymbol.h" |
34 | #include "llvm/MC/MCTargetOptions.h" |
35 | #include "llvm/MC/TargetRegistry.h" |
36 | #include "llvm/Object/SymbolicFile.h" |
37 | #include "llvm/Support/Casting.h" |
38 | #include "llvm/Support/ErrorHandling.h" |
39 | #include "llvm/Support/MemoryBuffer.h" |
40 | #include "llvm/Support/SMLoc.h" |
41 | #include "llvm/Support/SourceMgr.h" |
42 | #include "llvm/Support/raw_ostream.h" |
43 | #include "llvm/TargetParser/Triple.h" |
44 | #include <cassert> |
45 | #include <cstdint> |
46 | #include <memory> |
47 | #include <string> |
48 | |
49 | using namespace llvm; |
50 | using namespace object; |
51 | |
52 | void ModuleSymbolTable::addModule(Module *M) { |
53 | if (FirstMod) |
54 | assert(FirstMod->getTargetTriple() == M->getTargetTriple()); |
55 | else |
56 | FirstMod = M; |
57 | |
58 | for (GlobalValue &GV : M->global_values()) |
59 | SymTab.push_back(x: &GV); |
60 | |
61 | CollectAsmSymbols(M: *M, AsmSymbol: [this](StringRef Name, BasicSymbolRef::Flags Flags) { |
62 | SymTab.push_back(x: new (AsmSymbols.Allocate()) |
63 | AsmSymbol(std::string(Name), Flags)); |
64 | }); |
65 | } |
66 | |
67 | static void |
68 | initializeRecordStreamer(const Module &M, |
69 | function_ref<void(RecordStreamer &)> Init) { |
70 | // This function may be called twice, once for ModuleSummaryIndexAnalysis and |
71 | // the other when writing the IR symbol table. If parsing inline assembly has |
72 | // caused errors in the first run, suppress the second run. |
73 | if (M.getContext().getDiagHandlerPtr()->HasErrors) |
74 | return; |
75 | StringRef InlineAsm = M.getModuleInlineAsm(); |
76 | if (InlineAsm.empty()) |
77 | return; |
78 | |
79 | std::string Err; |
80 | const Triple TT(M.getTargetTriple()); |
81 | const Target *T = TargetRegistry::lookupTarget(TheTriple: TT, Error&: Err); |
82 | assert(T && T->hasMCAsmParser()); |
83 | |
84 | std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT: TT.str())); |
85 | if (!MRI) |
86 | return; |
87 | |
88 | MCTargetOptions MCOptions; |
89 | std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(MRI: *MRI, TheTriple: TT.str(), Options: MCOptions)); |
90 | if (!MAI) |
91 | return; |
92 | |
93 | std::unique_ptr<MCSubtargetInfo> STI( |
94 | T->createMCSubtargetInfo(TheTriple: TT.str(), CPU: "" , Features: "" )); |
95 | if (!STI) |
96 | return; |
97 | |
98 | std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo()); |
99 | if (!MCII) |
100 | return; |
101 | |
102 | std::unique_ptr<MemoryBuffer> Buffer( |
103 | MemoryBuffer::getMemBuffer(InputData: InlineAsm, BufferName: "<inline asm>" )); |
104 | SourceMgr SrcMgr; |
105 | SrcMgr.AddNewSourceBuffer(F: std::move(Buffer), IncludeLoc: SMLoc()); |
106 | |
107 | MCContext MCCtx(TT, MAI.get(), MRI.get(), STI.get(), &SrcMgr); |
108 | std::unique_ptr<MCObjectFileInfo> MOFI( |
109 | T->createMCObjectFileInfo(Ctx&: MCCtx, /*PIC=*/false)); |
110 | MCCtx.setObjectFileInfo(MOFI.get()); |
111 | RecordStreamer Streamer(MCCtx, M); |
112 | T->createNullTargetStreamer(S&: Streamer); |
113 | |
114 | std::unique_ptr<MCAsmParser> Parser( |
115 | createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI)); |
116 | |
117 | std::unique_ptr<MCTargetAsmParser> TAP( |
118 | T->createMCAsmParser(STI: *STI, Parser&: *Parser, MII: *MCII, Options: MCOptions)); |
119 | if (!TAP) |
120 | return; |
121 | |
122 | MCCtx.setDiagnosticHandler([&](const SMDiagnostic &SMD, bool IsInlineAsm, |
123 | const SourceMgr &SrcMgr, |
124 | std::vector<const MDNode *> &LocInfos) { |
125 | M.getContext().diagnose( |
126 | DI: DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, /*LocCookie=*/0)); |
127 | }); |
128 | |
129 | // Module-level inline asm is assumed to use At&t syntax (see |
130 | // AsmPrinter::doInitialization()). |
131 | Parser->setAssemblerDialect(InlineAsm::AD_ATT); |
132 | |
133 | Parser->setTargetParser(*TAP); |
134 | if (Parser->Run(NoInitialTextSection: false)) |
135 | return; |
136 | |
137 | Init(Streamer); |
138 | } |
139 | |
140 | void ModuleSymbolTable::CollectAsmSymbols( |
141 | const Module &M, |
142 | function_ref<void(StringRef, BasicSymbolRef::Flags)> AsmSymbol) { |
143 | initializeRecordStreamer(M, Init: [&](RecordStreamer &Streamer) { |
144 | Streamer.flushSymverDirectives(); |
145 | |
146 | for (auto &KV : Streamer) { |
147 | StringRef Key = KV.first(); |
148 | RecordStreamer::State Value = KV.second; |
149 | // FIXME: For now we just assume that all asm symbols are executable. |
150 | uint32_t Res = BasicSymbolRef::SF_Executable; |
151 | switch (Value) { |
152 | case RecordStreamer::NeverSeen: |
153 | llvm_unreachable("NeverSeen should have been replaced earlier" ); |
154 | case RecordStreamer::DefinedGlobal: |
155 | Res |= BasicSymbolRef::SF_Global; |
156 | break; |
157 | case RecordStreamer::Defined: |
158 | break; |
159 | case RecordStreamer::Global: |
160 | case RecordStreamer::Used: |
161 | Res |= BasicSymbolRef::SF_Undefined; |
162 | Res |= BasicSymbolRef::SF_Global; |
163 | break; |
164 | case RecordStreamer::DefinedWeak: |
165 | Res |= BasicSymbolRef::SF_Weak; |
166 | Res |= BasicSymbolRef::SF_Global; |
167 | break; |
168 | case RecordStreamer::UndefinedWeak: |
169 | Res |= BasicSymbolRef::SF_Weak; |
170 | Res |= BasicSymbolRef::SF_Undefined; |
171 | } |
172 | AsmSymbol(Key, BasicSymbolRef::Flags(Res)); |
173 | } |
174 | }); |
175 | |
176 | // In ELF, object code generated for x86-32 and some code models of x86-64 may |
177 | // reference the special symbol _GLOBAL_OFFSET_TABLE_ that is not used in the |
178 | // IR. Record it like inline asm symbols. |
179 | Triple TT(M.getTargetTriple()); |
180 | if (!TT.isOSBinFormatELF() || !TT.isX86()) |
181 | return; |
182 | auto CM = M.getCodeModel(); |
183 | if (TT.getArch() == Triple::x86 || CM == CodeModel::Medium || |
184 | CM == CodeModel::Large) { |
185 | AsmSymbol("_GLOBAL_OFFSET_TABLE_" , |
186 | BasicSymbolRef::Flags(BasicSymbolRef::SF_Undefined | |
187 | BasicSymbolRef::SF_Global)); |
188 | } |
189 | } |
190 | |
191 | void ModuleSymbolTable::CollectAsmSymvers( |
192 | const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) { |
193 | initializeRecordStreamer(M, Init: [&](RecordStreamer &Streamer) { |
194 | for (auto &KV : Streamer.symverAliases()) |
195 | for (auto &Alias : KV.second) |
196 | AsmSymver(KV.first->getName(), Alias); |
197 | }); |
198 | } |
199 | |
200 | void ModuleSymbolTable::printSymbolName(raw_ostream &OS, Symbol S) const { |
201 | if (isa<AsmSymbol *>(Val: S)) { |
202 | OS << cast<AsmSymbol *>(Val&: S)->first; |
203 | return; |
204 | } |
205 | |
206 | auto *GV = cast<GlobalValue *>(Val&: S); |
207 | if (GV->hasDLLImportStorageClass()) |
208 | OS << "__imp_" ; |
209 | |
210 | Mang.getNameWithPrefix(OS, GV, CannotUsePrivateLabel: false); |
211 | } |
212 | |
213 | uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S) const { |
214 | if (isa<AsmSymbol *>(Val: S)) |
215 | return cast<AsmSymbol *>(Val&: S)->second; |
216 | |
217 | auto *GV = cast<GlobalValue *>(Val&: S); |
218 | |
219 | uint32_t Res = BasicSymbolRef::SF_None; |
220 | if (GV->isDeclarationForLinker()) |
221 | Res |= BasicSymbolRef::SF_Undefined; |
222 | else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage()) |
223 | Res |= BasicSymbolRef::SF_Hidden; |
224 | if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(Val: GV)) { |
225 | if (GVar->isConstant()) |
226 | Res |= BasicSymbolRef::SF_Const; |
227 | } |
228 | if (const GlobalObject *GO = GV->getAliaseeObject()) |
229 | if (isa<Function>(Val: GO) || isa<GlobalIFunc>(Val: GO)) |
230 | Res |= BasicSymbolRef::SF_Executable; |
231 | if (isa<GlobalAlias>(Val: GV)) |
232 | Res |= BasicSymbolRef::SF_Indirect; |
233 | if (GV->hasPrivateLinkage()) |
234 | Res |= BasicSymbolRef::SF_FormatSpecific; |
235 | if (!GV->hasLocalLinkage()) |
236 | Res |= BasicSymbolRef::SF_Global; |
237 | if (GV->hasCommonLinkage()) |
238 | Res |= BasicSymbolRef::SF_Common; |
239 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || |
240 | GV->hasExternalWeakLinkage()) |
241 | Res |= BasicSymbolRef::SF_Weak; |
242 | |
243 | if (GV->getName().starts_with(Prefix: "llvm." )) |
244 | Res |= BasicSymbolRef::SF_FormatSpecific; |
245 | else if (auto *Var = dyn_cast<GlobalVariable>(Val: GV)) { |
246 | if (Var->getSection() == "llvm.metadata" ) |
247 | Res |= BasicSymbolRef::SF_FormatSpecific; |
248 | } |
249 | |
250 | return Res; |
251 | } |
252 | |