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/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCContext.h"
31 #include "llvm/MC/MCDirectives.h"
32 #include "llvm/MC/MCInstrInfo.h"
33 #include "llvm/MC/MCObjectFileInfo.h"
34 #include "llvm/MC/MCParser/MCAsmParser.h"
35 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
36 #include "llvm/MC/MCRegisterInfo.h"
37 #include "llvm/MC/MCSubtargetInfo.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/MC/MCTargetOptions.h"
40 #include "llvm/Object/SymbolicFile.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CodeGen.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/SMLoc.h"
46 #include "llvm/Support/SourceMgr.h"
47 #include "llvm/Support/TargetRegistry.h"
48 #include "llvm/Support/raw_ostream.h"
56 using namespace object
;
58 void ModuleSymbolTable::addModule(Module
*M
) {
60 assert(FirstMod
->getTargetTriple() == M
->getTargetTriple());
64 for (GlobalValue
&GV
: M
->global_values())
65 SymTab
.push_back(&GV
);
67 CollectAsmSymbols(*M
, [this](StringRef Name
, BasicSymbolRef::Flags Flags
) {
68 SymTab
.push_back(new (AsmSymbols
.Allocate()) AsmSymbol(Name
, Flags
));
72 // Ensure ELF .symver aliases get the same binding as the defined symbol
74 static void handleSymverAliases(const Module
&M
, RecordStreamer
&Streamer
) {
75 if (Streamer
.symverAliases().empty())
78 // The name in the assembler will be mangled, but the name in the IR
79 // might not, so we first compute a mapping from mangled name to GV.
81 SmallString
<64> MangledName
;
82 StringMap
<const GlobalValue
*> MangledNameMap
;
83 auto GetMangledName
= [&](const GlobalValue
&GV
) {
88 MangledName
.reserve(GV
.getName().size() + 1);
89 Mang
.getNameWithPrefix(MangledName
, &GV
, /*CannotUsePrivateLabel=*/false);
90 MangledNameMap
[MangledName
] = &GV
;
92 for (const Function
&F
: M
)
94 for (const GlobalVariable
&GV
: M
.globals())
96 for (const GlobalAlias
&GA
: M
.aliases())
99 // Walk all the recorded .symver aliases, and set up the binding
101 for (auto &Symver
: Streamer
.symverAliases()) {
102 const MCSymbol
*Aliasee
= Symver
.first
;
103 MCSymbolAttr Attr
= MCSA_Invalid
;
105 // First check if the aliasee binding was recorded in the asm.
106 RecordStreamer::State state
= Streamer
.getSymbolState(Aliasee
);
108 case RecordStreamer::Global
:
109 case RecordStreamer::DefinedGlobal
:
112 case RecordStreamer::UndefinedWeak
:
113 case RecordStreamer::DefinedWeak
:
120 // If we don't have a symbol attribute from assembly, then check if
121 // the aliasee was defined in the IR.
122 if (Attr
== MCSA_Invalid
) {
123 const auto *GV
= M
.getNamedValue(Aliasee
->getName());
125 auto MI
= MangledNameMap
.find(Aliasee
->getName());
126 if (MI
!= MangledNameMap
.end())
131 if (GV
->hasExternalLinkage())
133 else if (GV
->hasLocalLinkage())
135 else if (GV
->isWeakForLinker())
138 if (Attr
== MCSA_Invalid
)
141 // Set the detected binding on each alias with this aliasee.
142 for (auto &Alias
: Symver
.second
)
143 Streamer
.EmitSymbolAttribute(Alias
, Attr
);
147 void ModuleSymbolTable::CollectAsmSymbols(
149 function_ref
<void(StringRef
, BasicSymbolRef::Flags
)> AsmSymbol
) {
150 StringRef InlineAsm
= M
.getModuleInlineAsm();
151 if (InlineAsm
.empty())
155 const Triple
TT(M
.getTargetTriple());
156 const Target
*T
= TargetRegistry::lookupTarget(TT
.str(), Err
);
157 assert(T
&& T
->hasMCAsmParser());
159 std::unique_ptr
<MCRegisterInfo
> MRI(T
->createMCRegInfo(TT
.str()));
163 std::unique_ptr
<MCAsmInfo
> MAI(T
->createMCAsmInfo(*MRI
, TT
.str()));
167 std::unique_ptr
<MCSubtargetInfo
> STI(
168 T
->createMCSubtargetInfo(TT
.str(), "", ""));
172 std::unique_ptr
<MCInstrInfo
> MCII(T
->createMCInstrInfo());
176 MCObjectFileInfo MOFI
;
177 MCContext
MCCtx(MAI
.get(), MRI
.get(), &MOFI
);
178 MOFI
.InitMCObjectFileInfo(TT
, /*PIC*/ false, MCCtx
);
179 RecordStreamer
Streamer(MCCtx
);
180 T
->createNullTargetStreamer(Streamer
);
182 std::unique_ptr
<MemoryBuffer
> Buffer(MemoryBuffer::getMemBuffer(InlineAsm
));
184 SrcMgr
.AddNewSourceBuffer(std::move(Buffer
), SMLoc());
185 std::unique_ptr
<MCAsmParser
> Parser(
186 createMCAsmParser(SrcMgr
, MCCtx
, Streamer
, *MAI
));
188 MCTargetOptions MCOptions
;
189 std::unique_ptr
<MCTargetAsmParser
> TAP(
190 T
->createMCAsmParser(*STI
, *Parser
, *MCII
, MCOptions
));
194 Parser
->setTargetParser(*TAP
);
195 if (Parser
->Run(false))
198 handleSymverAliases(M
, Streamer
);
200 for (auto &KV
: Streamer
) {
201 StringRef Key
= KV
.first();
202 RecordStreamer::State Value
= KV
.second
;
203 // FIXME: For now we just assume that all asm symbols are executable.
204 uint32_t Res
= BasicSymbolRef::SF_Executable
;
206 case RecordStreamer::NeverSeen
:
207 llvm_unreachable("NeverSeen should have been replaced earlier");
208 case RecordStreamer::DefinedGlobal
:
209 Res
|= BasicSymbolRef::SF_Global
;
211 case RecordStreamer::Defined
:
213 case RecordStreamer::Global
:
214 case RecordStreamer::Used
:
215 Res
|= BasicSymbolRef::SF_Undefined
;
216 Res
|= BasicSymbolRef::SF_Global
;
218 case RecordStreamer::DefinedWeak
:
219 Res
|= BasicSymbolRef::SF_Weak
;
220 Res
|= BasicSymbolRef::SF_Global
;
222 case RecordStreamer::UndefinedWeak
:
223 Res
|= BasicSymbolRef::SF_Weak
;
224 Res
|= BasicSymbolRef::SF_Undefined
;
226 AsmSymbol(Key
, BasicSymbolRef::Flags(Res
));
230 void ModuleSymbolTable::printSymbolName(raw_ostream
&OS
, Symbol S
) const {
231 if (S
.is
<AsmSymbol
*>()) {
232 OS
<< S
.get
<AsmSymbol
*>()->first
;
236 auto *GV
= S
.get
<GlobalValue
*>();
237 if (GV
->hasDLLImportStorageClass())
240 Mang
.getNameWithPrefix(OS
, GV
, false);
243 uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S
) const {
244 if (S
.is
<AsmSymbol
*>())
245 return S
.get
<AsmSymbol
*>()->second
;
247 auto *GV
= S
.get
<GlobalValue
*>();
249 uint32_t Res
= BasicSymbolRef::SF_None
;
250 if (GV
->isDeclarationForLinker())
251 Res
|= BasicSymbolRef::SF_Undefined
;
252 else if (GV
->hasHiddenVisibility() && !GV
->hasLocalLinkage())
253 Res
|= BasicSymbolRef::SF_Hidden
;
254 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
)) {
255 if (GVar
->isConstant())
256 Res
|= BasicSymbolRef::SF_Const
;
258 if (dyn_cast_or_null
<Function
>(GV
->getBaseObject()))
259 Res
|= BasicSymbolRef::SF_Executable
;
260 if (isa
<GlobalAlias
>(GV
))
261 Res
|= BasicSymbolRef::SF_Indirect
;
262 if (GV
->hasPrivateLinkage())
263 Res
|= BasicSymbolRef::SF_FormatSpecific
;
264 if (!GV
->hasLocalLinkage())
265 Res
|= BasicSymbolRef::SF_Global
;
266 if (GV
->hasCommonLinkage())
267 Res
|= BasicSymbolRef::SF_Common
;
268 if (GV
->hasLinkOnceLinkage() || GV
->hasWeakLinkage() ||
269 GV
->hasExternalWeakLinkage())
270 Res
|= BasicSymbolRef::SF_Weak
;
272 if (GV
->getName().startswith("llvm."))
273 Res
|= BasicSymbolRef::SF_FormatSpecific
;
274 else if (auto *Var
= dyn_cast
<GlobalVariable
>(GV
)) {
275 if (Var
->getSection() == "llvm.metadata")
276 Res
|= BasicSymbolRef::SF_FormatSpecific
;