xref: /llvm-project/llvm/lib/Object/ModuleSymbolTable.cpp (revision b26bc7fddcec7ee0f903573138e99f73dc561062)
1 //===- ModuleSymbolTable.cpp - symbol table for in-memory IR ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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).
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Object/IRObjectFile.h"
17 #include "RecordStreamer.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Bitcode/BitcodeReader.h"
20 #include "llvm/IR/GVMaterializer.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCObjectFileInfo.h"
28 #include "llvm/MC/MCParser/MCAsmParser.h"
29 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/Object/ObjectFile.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/SourceMgr.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
37 using namespace llvm;
38 using namespace object;
39 
40 void ModuleSymbolTable::addModule(Module *M) {
41   if (FirstMod)
42     assert(FirstMod->getTargetTriple() == M->getTargetTriple());
43   else
44     FirstMod = M;
45 
46   for (GlobalValue &GV : M->global_values())
47     SymTab.push_back(&GV);
48 
49   CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) {
50     SymTab.push_back(new (AsmSymbols.Allocate()) AsmSymbol(Name, Flags));
51   });
52 }
53 
54 // Ensure ELF .symver aliases get the same binding as the defined symbol
55 // they alias with.
56 static void handleSymverAliases(const Module &M, RecordStreamer &Streamer) {
57   if (Streamer.symverAliases().empty())
58     return;
59 
60   // The name in the assembler will be mangled, but the name in the IR
61   // might not, so we first compute a mapping from mangled name to GV.
62   Mangler Mang;
63   SmallString<64> MangledName;
64   StringMap<const GlobalValue *> MangledNameMap;
65   auto GetMangledName = [&](const GlobalValue &GV) {
66     if (!GV.hasName())
67       return;
68 
69     MangledName.clear();
70     MangledName.reserve(GV.getName().size() + 1);
71     Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
72     MangledNameMap[MangledName] = &GV;
73   };
74   for (const Function &F : M)
75     GetMangledName(F);
76   for (const GlobalVariable &GV : M.globals())
77     GetMangledName(GV);
78   for (const GlobalAlias &GA : M.aliases())
79     GetMangledName(GA);
80 
81   // Walk all the recorded .symver aliases, and set up the binding
82   // for each alias.
83   for (auto &Symver : Streamer.symverAliases()) {
84     const MCSymbol *Aliasee = Symver.first;
85     MCSymbolAttr Attr = MCSA_Invalid;
86 
87     // First check if the aliasee binding was recorded in the asm.
88     RecordStreamer::State state = Streamer.getSymbolState(Aliasee);
89     switch (state) {
90     case RecordStreamer::Global:
91     case RecordStreamer::DefinedGlobal:
92       Attr = MCSA_Global;
93       break;
94     case RecordStreamer::UndefinedWeak:
95     case RecordStreamer::DefinedWeak:
96       Attr = MCSA_Weak;
97       break;
98     default:
99       break;
100     }
101 
102     // If we don't have a symbol attribute from assembly, then check if
103     // the aliasee was defined in the IR.
104     if (Attr == MCSA_Invalid) {
105       const auto *GV = M.getNamedValue(Aliasee->getName());
106       if (!GV) {
107         auto MI = MangledNameMap.find(Aliasee->getName());
108         if (MI != MangledNameMap.end())
109           GV = MI->second;
110         else
111           continue;
112       }
113       if (GV->hasExternalLinkage())
114         Attr = MCSA_Global;
115       else if (GV->hasLocalLinkage())
116         Attr = MCSA_Local;
117       else if (GV->isWeakForLinker())
118         Attr = MCSA_Weak;
119     }
120     if (Attr == MCSA_Invalid)
121       continue;
122 
123     // Set the detected binding on each alias with this aliasee.
124     for (auto &Alias : Symver.second)
125       Streamer.EmitSymbolAttribute(Alias, Attr);
126   }
127 }
128 
129 void ModuleSymbolTable::CollectAsmSymbols(
130     const Module &M,
131     function_ref<void(StringRef, BasicSymbolRef::Flags)> AsmSymbol) {
132   StringRef InlineAsm = M.getModuleInlineAsm();
133   if (InlineAsm.empty())
134     return;
135 
136   std::string Err;
137   const Triple TT(M.getTargetTriple());
138   const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
139   assert(T && T->hasMCAsmParser());
140 
141   std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
142   if (!MRI)
143     return;
144 
145   std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
146   if (!MAI)
147     return;
148 
149   std::unique_ptr<MCSubtargetInfo> STI(
150       T->createMCSubtargetInfo(TT.str(), "", ""));
151   if (!STI)
152     return;
153 
154   std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
155   if (!MCII)
156     return;
157 
158   MCObjectFileInfo MOFI;
159   MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
160   MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, CodeModel::Default, MCCtx);
161   RecordStreamer Streamer(MCCtx);
162   T->createNullTargetStreamer(Streamer);
163 
164   std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
165   SourceMgr SrcMgr;
166   SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
167   std::unique_ptr<MCAsmParser> Parser(
168       createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
169 
170   MCTargetOptions MCOptions;
171   std::unique_ptr<MCTargetAsmParser> TAP(
172       T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
173   if (!TAP)
174     return;
175 
176   Parser->setTargetParser(*TAP);
177   if (Parser->Run(false))
178     return;
179 
180   handleSymverAliases(M, Streamer);
181 
182   for (auto &KV : Streamer) {
183     StringRef Key = KV.first();
184     RecordStreamer::State Value = KV.second;
185     // FIXME: For now we just assume that all asm symbols are executable.
186     uint32_t Res = BasicSymbolRef::SF_Executable;
187     switch (Value) {
188     case RecordStreamer::NeverSeen:
189       llvm_unreachable("NeverSeen should have been replaced earlier");
190     case RecordStreamer::DefinedGlobal:
191       Res |= BasicSymbolRef::SF_Global;
192       break;
193     case RecordStreamer::Defined:
194       break;
195     case RecordStreamer::Global:
196     case RecordStreamer::Used:
197       Res |= BasicSymbolRef::SF_Undefined;
198       Res |= BasicSymbolRef::SF_Global;
199       break;
200     case RecordStreamer::DefinedWeak:
201       Res |= BasicSymbolRef::SF_Weak;
202       Res |= BasicSymbolRef::SF_Global;
203       break;
204     case RecordStreamer::UndefinedWeak:
205       Res |= BasicSymbolRef::SF_Weak;
206       Res |= BasicSymbolRef::SF_Undefined;
207     }
208     AsmSymbol(Key, BasicSymbolRef::Flags(Res));
209   }
210 }
211 
212 void ModuleSymbolTable::printSymbolName(raw_ostream &OS, Symbol S) const {
213   if (S.is<AsmSymbol *>()) {
214     OS << S.get<AsmSymbol *>()->first;
215     return;
216   }
217 
218   auto *GV = S.get<GlobalValue *>();
219   if (GV->hasDLLImportStorageClass())
220     OS << "__imp_";
221 
222   Mang.getNameWithPrefix(OS, GV, false);
223 }
224 
225 uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S) const {
226   if (S.is<AsmSymbol *>())
227     return S.get<AsmSymbol *>()->second;
228 
229   auto *GV = S.get<GlobalValue *>();
230 
231   uint32_t Res = BasicSymbolRef::SF_None;
232   if (GV->isDeclarationForLinker())
233     Res |= BasicSymbolRef::SF_Undefined;
234   else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
235     Res |= BasicSymbolRef::SF_Hidden;
236   if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
237     if (GVar->isConstant())
238       Res |= BasicSymbolRef::SF_Const;
239   }
240   if (dyn_cast_or_null<Function>(GV->getBaseObject()))
241     Res |= BasicSymbolRef::SF_Executable;
242   if (isa<GlobalAlias>(GV))
243     Res |= BasicSymbolRef::SF_Indirect;
244   if (GV->hasPrivateLinkage())
245     Res |= BasicSymbolRef::SF_FormatSpecific;
246   if (!GV->hasLocalLinkage())
247     Res |= BasicSymbolRef::SF_Global;
248   if (GV->hasCommonLinkage())
249     Res |= BasicSymbolRef::SF_Common;
250   if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
251       GV->hasExternalWeakLinkage())
252     Res |= BasicSymbolRef::SF_Weak;
253 
254   if (GV->getName().startswith("llvm."))
255     Res |= BasicSymbolRef::SF_FormatSpecific;
256   else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
257     if (Var->getSection() == "llvm.metadata")
258       Res |= BasicSymbolRef::SF_FormatSpecific;
259   }
260 
261   return Res;
262 }
263