1 //===- SymbolTable.cpp ----------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "SymbolTable.h"
11 
12 #include "Config.h"
13 #include "InputChunks.h"
14 #include "WriterUtils.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 
18 #include <unordered_set>
19 
20 #define DEBUG_TYPE "lld"
21 
22 using namespace llvm;
23 using namespace llvm::wasm;
24 using namespace lld;
25 using namespace lld::wasm;
26 
27 SymbolTable *lld::wasm::Symtab;
28 
29 void SymbolTable::addFile(InputFile *File) {
30   log("Processing: " + toString(File));
31   File->parse();
32 
33   if (auto *F = dyn_cast<ObjFile>(File))
34     ObjectFiles.push_back(F);
35 }
36 
37 void SymbolTable::reportRemainingUndefines() {
38   std::unordered_set<Symbol *> Undefs;
39   for (Symbol *Sym : SymVector) {
40     if (Sym->isUndefined() && !Sym->isWeak() &&
41         Config->AllowUndefinedSymbols.count(Sym->getName()) == 0) {
42       Undefs.insert(Sym);
43     }
44   }
45 
46   if (Undefs.empty())
47     return;
48 
49   for (ObjFile *File : ObjectFiles)
50     for (Symbol *Sym : File->getSymbols())
51       if (Undefs.count(Sym))
52         error(toString(File) + ": undefined symbol: " + toString(*Sym));
53 
54   for (Symbol *Sym : Undefs)
55     if (!Sym->getFile())
56       error("undefined symbol: " + toString(*Sym));
57 }
58 
59 Symbol *SymbolTable::find(StringRef Name) {
60   auto It = SymMap.find(CachedHashStringRef(Name));
61   if (It == SymMap.end())
62     return nullptr;
63   return It->second;
64 }
65 
66 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
67   Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
68   if (Sym)
69     return {Sym, false};
70   Sym = make<Symbol>(Name, false);
71   SymVector.emplace_back(Sym);
72   return {Sym, true};
73 }
74 
75 void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
76   error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
77         toString(Existing->getFile()) + "\n>>> defined in " +
78         toString(NewFile));
79 }
80 
81 // Check the type of new symbol matches that of the symbol is replacing.
82 // For functions this can also involve verifying that the signatures match.
83 static void checkSymbolTypes(const Symbol &Existing, const InputFile &F,
84                              Symbol::Kind Kind, const WasmSignature *NewSig) {
85   if (Existing.isLazy())
86     return;
87 
88   bool NewIsFunction = Kind == Symbol::Kind::UndefinedFunctionKind ||
89                        Kind == Symbol::Kind::DefinedFunctionKind;
90 
91   // First check the symbol types match (i.e. either both are function
92   // symbols or both are data symbols).
93   if (Existing.isFunction() != NewIsFunction) {
94     error("symbol type mismatch: " + Existing.getName() + "\n>>> defined as " +
95           (Existing.isFunction() ? "Function" : "Global") + " in " +
96           toString(Existing.getFile()) + "\n>>> defined as " +
97           (NewIsFunction ? "Function" : "Global") + " in " + F.getName());
98     return;
99   }
100 
101   // For function symbols, optionally check the function signature matches too.
102   if (!NewIsFunction || !Config->CheckSignatures)
103     return;
104   // Skip the signature check if the existing function has no signature (e.g.
105   // if it is an undefined symbol generated by --undefined command line flag).
106   if (!Existing.hasFunctionType())
107     return;
108 
109   DEBUG(dbgs() << "checkSymbolTypes: " << Existing.getName() << "\n");
110   assert(NewSig);
111 
112   const WasmSignature &OldSig = Existing.getFunctionType();
113   if (*NewSig == OldSig)
114     return;
115 
116   error("function signature mismatch: " + Existing.getName() +
117         "\n>>> defined as " + toString(OldSig) + " in " +
118         toString(Existing.getFile()) + "\n>>> defined as " + toString(*NewSig) +
119         " in " + F.getName());
120 }
121 
122 Symbol *SymbolTable::addDefinedFunction(StringRef Name,
123                                         const WasmSignature *Type,
124                                         uint32_t Flags) {
125   DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n");
126   Symbol *S;
127   bool WasInserted;
128   std::tie(S, WasInserted) = insert(Name);
129   if (WasInserted) {
130     S->update(Symbol::DefinedFunctionKind, nullptr, Flags);
131     S->setFunctionType(Type);
132   } else if (!S->isFunction()) {
133     error("symbol type mismatch: " + Name);
134   } else if (!S->isDefined()) {
135     DEBUG(dbgs() << "resolving existing undefined function: " << Name << "\n");
136     S->update(Symbol::DefinedFunctionKind, nullptr, Flags);
137   }
138   return S;
139 }
140 
141 Symbol *SymbolTable::addDefinedGlobal(StringRef Name) {
142   DEBUG(dbgs() << "addDefinedGlobal: " << Name << "\n");
143   Symbol *S;
144   bool WasInserted;
145   std::tie(S, WasInserted) = insert(Name);
146   if (WasInserted) {
147     S->update(Symbol::DefinedGlobalKind);
148   } else if (!S->isGlobal()) {
149     error("symbol type mismatch: " + Name);
150   } else {
151     DEBUG(dbgs() << "resolving existing undefined global: " << Name << "\n");
152     S->update(Symbol::DefinedGlobalKind);
153   }
154   return S;
155 }
156 
157 Symbol *SymbolTable::addDefined(StringRef Name, Symbol::Kind Kind,
158                                 uint32_t Flags, InputFile *F,
159                                 const InputSegment *Segment,
160                                 InputFunction *Function, uint32_t Address) {
161   DEBUG(dbgs() << "addDefined: " << Name << " addr:" << Address << "\n");
162   Symbol *S;
163   bool WasInserted;
164 
165   std::tie(S, WasInserted) = insert(Name);
166   if (WasInserted) {
167     S->update(Kind, F, Flags, Segment, Function, Address);
168   } else if (S->isLazy()) {
169     // The existing symbol is lazy. Replace it without checking types since
170     // lazy symbols don't have any type information.
171     DEBUG(dbgs() << "replacing existing lazy symbol: " << Name << "\n");
172     S->update(Kind, F, Flags, Segment, Function, Address);
173   } else if (!S->isDefined()) {
174     // The existing symbol table entry is undefined. The new symbol replaces
175     // it, after checking the type matches
176     DEBUG(dbgs() << "resolving existing undefined symbol: " << Name << "\n");
177     checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr);
178     S->update(Kind, F, Flags, Segment, Function, Address);
179   } else if ((Flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
180     // the new symbol is weak we can ignore it
181     DEBUG(dbgs() << "existing symbol takes precedence\n");
182   } else if (S->isWeak()) {
183     // the new symbol is not weak and the existing symbol is, so we replace
184     // it
185     DEBUG(dbgs() << "replacing existing weak symbol\n");
186     checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr);
187     S->update(Kind, F, Flags, Segment, Function, Address);
188   } else {
189     // neither symbol is week. They conflict.
190     reportDuplicate(S, F);
191   }
192   return S;
193 }
194 
195 Symbol *SymbolTable::addUndefinedFunction(StringRef Name,
196                                           const WasmSignature *Type) {
197   DEBUG(dbgs() << "addUndefinedFunction: " << Name << "\n");
198   Symbol *S;
199   bool WasInserted;
200   std::tie(S, WasInserted) = insert(Name);
201   if (WasInserted) {
202     S->update(Symbol::UndefinedFunctionKind);
203     S->setFunctionType(Type);
204   } else if (!S->isFunction()) {
205     error("symbol type mismatch: " + Name);
206   }
207   return S;
208 }
209 
210 Symbol *SymbolTable::addUndefined(StringRef Name, Symbol::Kind Kind,
211                                   uint32_t Flags, InputFile *F,
212                                   const WasmSignature *Type) {
213   DEBUG(dbgs() << "addUndefined: " << Name << "\n");
214   Symbol *S;
215   bool WasInserted;
216   std::tie(S, WasInserted) = insert(Name);
217   if (WasInserted) {
218     S->update(Kind, F, Flags);
219     if (Type)
220       S->setFunctionType(Type);
221   } else if (S->isLazy()) {
222     DEBUG(dbgs() << "resolved by existing lazy\n");
223     auto *AF = cast<ArchiveFile>(S->getFile());
224     AF->addMember(&S->getArchiveSymbol());
225   } else if (S->isDefined()) {
226     DEBUG(dbgs() << "resolved by existing\n");
227     checkSymbolTypes(*S, *F, Kind, Type);
228   }
229   return S;
230 }
231 
232 void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol *Sym) {
233   DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
234   StringRef Name = Sym->getName();
235   Symbol *S;
236   bool WasInserted;
237   std::tie(S, WasInserted) = insert(Name);
238   if (WasInserted) {
239     S->update(Symbol::LazyKind, F);
240     S->setArchiveSymbol(*Sym);
241   } else if (S->isUndefined()) {
242     // There is an existing undefined symbol.  The can load from the
243     // archive.
244     DEBUG(dbgs() << "replacing existing undefined\n");
245     F->addMember(Sym);
246   }
247 }
248 
249 bool SymbolTable::addComdat(StringRef Name, ObjFile *F) {
250   DEBUG(dbgs() << "addComdat: " << Name << "\n");
251   ObjFile *&File = ComdatMap[CachedHashStringRef(Name)];
252   if (File) {
253     DEBUG(dbgs() << "COMDAT already defined\n");
254     return false;
255   }
256   File = F;
257   return true;
258 }
259 
260 ObjFile *SymbolTable::findComdat(StringRef Name) const {
261   auto It = ComdatMap.find(CachedHashStringRef(Name));
262   return It == ComdatMap.end() ? nullptr : It->second;
263 }
264