blob: c4a460f3ad7aa13948a5aaa69b206981abd57c3d [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam Cleggc94d3932017-11-17 18:14:09 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SymbolTable.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000010#include "Config.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000011#include "InputChunks.h"
Heejin Ahne915a712018-12-08 06:17:43 +000012#include "InputEvent.h"
Sam Clegg93102972018-02-23 05:08:53 +000013#include "InputGlobal.h"
Sam Cleggb8621592017-11-30 01:40:08 +000014#include "WriterUtils.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000016#include "lld/Common/Memory.h"
Rui Ueyama7d67dd12018-02-13 22:30:52 +000017#include "llvm/ADT/SetVector.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000018
19#define DEBUG_TYPE "lld"
20
21using namespace llvm;
Sam Clegg20db3812018-01-10 00:52:20 +000022using namespace llvm::wasm;
Sam Clegg45218f42018-11-27 01:08:16 +000023using namespace llvm::object;
Sam Cleggc94d3932017-11-17 18:14:09 +000024using namespace lld;
25using namespace lld::wasm;
26
27SymbolTable *lld::wasm::Symtab;
28
29void SymbolTable::addFile(InputFile *File) {
30 log("Processing: " + toString(File));
Sam Clegg1f3f7742019-02-06 02:35:18 +000031 if (Config->Trace)
32 message(toString(File));
Sam Cleggc94d3932017-11-17 18:14:09 +000033 File->parse();
34
Sam Cleggc729c1b2018-05-30 18:07:52 +000035 // LLVM bitcode file
36 if (auto *F = dyn_cast<BitcodeFile>(File))
37 BitcodeFiles.push_back(F);
38 else if (auto *F = dyn_cast<ObjFile>(File))
Sam Cleggc94d3932017-11-17 18:14:09 +000039 ObjectFiles.push_back(F);
Sam Clegga688a422019-03-13 21:29:20 +000040 else if (auto *F = dyn_cast<SharedFile>(File))
41 SharedFiles.push_back(F);
Sam Cleggc94d3932017-11-17 18:14:09 +000042}
43
Sam Cleggc729c1b2018-05-30 18:07:52 +000044// This function is where all the optimizations of link-time
45// optimization happens. When LTO is in use, some input files are
46// not in native object file format but in the LLVM bitcode format.
47// This function compiles bitcode files into a few big native files
48// using LLVM functions and replaces bitcode symbols with the results.
49// Because all bitcode files that the program consists of are passed
50// to the compiler at once, it can do whole-program optimization.
51void SymbolTable::addCombinedLTOObject() {
52 if (BitcodeFiles.empty())
53 return;
54
55 // Compile bitcode files and replace bitcode symbols.
56 LTO.reset(new BitcodeCompiler);
57 for (BitcodeFile *F : BitcodeFiles)
58 LTO->add(*F);
59
60 for (StringRef Filename : LTO->compile()) {
Sam Clegg0cfaa242019-04-09 05:41:52 +000061 auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp"), "");
Sam Clegg697f2142019-05-15 16:03:28 +000062 Obj->parse(true);
Sam Cleggc729c1b2018-05-30 18:07:52 +000063 ObjectFiles.push_back(Obj);
64 }
65}
66
Sam Cleggc94d3932017-11-17 18:14:09 +000067void SymbolTable::reportRemainingUndefines() {
Sam Clegga5ca34e2019-05-24 14:14:25 +000068 for (const auto& Pair : SymMap) {
69 const Symbol *Sym = SymVector[Pair.second];
Sam Cleggc729c1b2018-05-30 18:07:52 +000070 if (!Sym->isUndefined() || Sym->isWeak())
71 continue;
72 if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0)
73 continue;
74 if (!Sym->IsUsedInRegularObj)
75 continue;
Sam Clegg47e2b6b2018-08-04 00:04:06 +000076 error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym));
Sam Cleggc94d3932017-11-17 18:14:09 +000077 }
Sam Cleggc94d3932017-11-17 18:14:09 +000078}
79
80Symbol *SymbolTable::find(StringRef Name) {
Sam Clegg1f3f7742019-02-06 02:35:18 +000081 auto It = SymMap.find(CachedHashStringRef(Name));
82 if (It == SymMap.end() || It->second == -1)
83 return nullptr;
84 return SymVector[It->second];
85}
86
Sam Clegg6540e572019-02-20 23:19:31 +000087void SymbolTable::replace(StringRef Name, Symbol* Sym) {
88 auto It = SymMap.find(CachedHashStringRef(Name));
89 SymVector[It->second] = Sym;
90}
91
Sam Clegg1f3f7742019-02-06 02:35:18 +000092std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) {
93 bool Trace = false;
94 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
95 int &SymIndex = P.first->second;
96 bool IsNew = P.second;
97 if (SymIndex == -1) {
98 SymIndex = SymVector.size();
99 Trace = true;
100 IsNew = true;
101 }
102
103 if (!IsNew)
104 return {SymVector[SymIndex], false};
105
106 Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
107 Sym->IsUsedInRegularObj = false;
Sam Clegga5ca34e2019-05-24 14:14:25 +0000108 Sym->CanInline = true;
Sam Clegg1f3f7742019-02-06 02:35:18 +0000109 Sym->Traced = Trace;
110 SymVector.emplace_back(Sym);
111 return {Sym, true};
Sam Cleggc94d3932017-11-17 18:14:09 +0000112}
113
Sam Clegg230dc112019-02-07 22:42:16 +0000114std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name,
115 const InputFile *File) {
Sam Clegg1f3f7742019-02-06 02:35:18 +0000116 Symbol *S;
117 bool WasInserted;
118 std::tie(S, WasInserted) = insertName(Name);
119
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000120 if (!File || File->kind() == InputFile::ObjectKind)
Sam Clegg1f3f7742019-02-06 02:35:18 +0000121 S->IsUsedInRegularObj = true;
122
123 return {S, WasInserted};
Sam Cleggc94d3932017-11-17 18:14:09 +0000124}
125
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000126static void reportTypeError(const Symbol *Existing, const InputFile *File,
Sam Clegg3876d89a2018-05-14 22:42:33 +0000127 llvm::wasm::WasmSymbolType Type) {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000128 error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " +
129 toString(Existing->getWasmType()) + " in " +
Sam Clegg3876d89a2018-05-14 22:42:33 +0000130 toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) +
131 " in " + toString(File));
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000132}
133
Heejin Ahn6f4286f2018-11-19 23:31:28 +0000134// Check the type of new symbol matches that of the symbol is replacing.
Sam Clegg6540e572019-02-20 23:19:31 +0000135// Returns true if the function types match, false is there is a singature
136// mismatch.
Benjamin Kramerba2ea932019-03-28 17:18:42 +0000137static bool signatureMatches(FunctionSymbol *Existing,
138 const WasmSignature *NewSig) {
Sam Clegg6540e572019-02-20 23:19:31 +0000139 const WasmSignature *OldSig = Existing->Signature;
Sam Cleggd506b0a2019-05-29 15:36:42 +0000140
141 // If either function is missing a signature (this happend for bitcode
142 // symbols) then assume they match. Any mismatch will be reported later
143 // when the LTO objects are added.
144 if (!NewSig || !OldSig)
Sam Clegg6540e572019-02-20 23:19:31 +0000145 return true;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000146
Sam Clegg6540e572019-02-20 23:19:31 +0000147 return *NewSig == *OldSig;
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000148}
149
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000150static void checkGlobalType(const Symbol *Existing, const InputFile *File,
151 const WasmGlobalType *NewType) {
152 if (!isa<GlobalSymbol>(Existing)) {
Sam Clegg3876d89a2018-05-14 22:42:33 +0000153 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL);
Sam Cleggb8621592017-11-30 01:40:08 +0000154 return;
155 }
156
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000157 const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType();
158 if (*NewType != *OldType) {
159 error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " +
160 toString(*OldType) + " in " + toString(Existing->getFile()) +
161 "\n>>> defined as " + toString(*NewType) + " in " + toString(File));
Sam Clegg93102972018-02-23 05:08:53 +0000162 }
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000163}
164
Heejin Ahne915a712018-12-08 06:17:43 +0000165static void checkEventType(const Symbol *Existing, const InputFile *File,
166 const WasmEventType *NewType,
167 const WasmSignature *NewSig) {
168 auto ExistingEvent = dyn_cast<EventSymbol>(Existing);
169 if (!isa<EventSymbol>(Existing)) {
170 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_EVENT);
171 return;
172 }
173
174 const WasmEventType *OldType = cast<EventSymbol>(Existing)->getEventType();
175 const WasmSignature *OldSig = ExistingEvent->Signature;
176 if (NewType->Attribute != OldType->Attribute)
177 error("Event type mismatch: " + Existing->getName() + "\n>>> defined as " +
178 toString(*OldType) + " in " + toString(Existing->getFile()) +
179 "\n>>> defined as " + toString(*NewType) + " in " + toString(File));
180 if (*NewSig != *OldSig)
181 warn("Event signature mismatch: " + Existing->getName() +
182 "\n>>> defined as " + toString(*OldSig) + " in " +
183 toString(Existing->getFile()) + "\n>>> defined as " +
184 toString(*NewSig) + " in " + toString(File));
185}
186
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000187static void checkDataType(const Symbol *Existing, const InputFile *File) {
188 if (!isa<DataSymbol>(Existing))
Sam Clegg3876d89a2018-05-14 22:42:33 +0000189 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000190}
191
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000192DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name,
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000193 uint32_t Flags,
194 InputFunction *Function) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000195 LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000196 assert(!find(Name));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000197 SyntheticFunctions.emplace_back(Function);
Sam Cleggd15a41542019-03-08 21:10:48 +0000198 return replaceSymbol<DefinedFunction>(insertName(Name).first, Name,
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000199 Flags, nullptr, Function);
Sam Clegg50686852018-01-12 18:35:13 +0000200}
201
Sam Clegg8e8ddaa2019-06-04 16:35:23 +0000202// Adds an optional, linker generated, data symbols. The symbol will only be
203// added if there is an undefine reference to it, or if it is explictly exported
204// via the --export flag. Otherwise we don't add the symbol and return nullptr.
Sam Clegg4bce63a2019-05-23 10:06:03 +0000205DefinedData *SymbolTable::addOptionalDataSymbol(StringRef Name, uint32_t Value,
206 uint32_t Flags) {
207 Symbol *S = find(Name);
Sam Clegg7d4ec5a2019-05-31 22:51:59 +0000208 if (!S && (Config->ExportAll || Config->ExportedSymbols.count(Name) != 0))
209 S = insertName(Name).first;
210 else if (!S || S->isDefined())
Sam Clegg4bce63a2019-05-23 10:06:03 +0000211 return nullptr;
212 LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << Name << "\n");
213 auto *rtn = replaceSymbol<DefinedData>(S, Name, Flags);
214 rtn->setVirtualAddress(Value);
215 return rtn;
216}
217
Sam Clegg00245532018-02-20 23:38:27 +0000218DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name,
219 uint32_t Flags) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000220 LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000221 assert(!find(Name));
Sam Cleggd15a41542019-03-08 21:10:48 +0000222 return replaceSymbol<DefinedData>(insertName(Name).first, Name, Flags);
Sam Cleggc94d3932017-11-17 18:14:09 +0000223}
224
Sam Clegg93102972018-02-23 05:08:53 +0000225DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags,
226 InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000227 LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global
228 << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000229 assert(!find(Name));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000230 SyntheticGlobals.emplace_back(Global);
Sam Cleggd15a41542019-03-08 21:10:48 +0000231 return replaceSymbol<DefinedGlobal>(insertName(Name).first, Name, Flags,
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000232 nullptr, Global);
Sam Clegg93102972018-02-23 05:08:53 +0000233}
234
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000235static bool shouldReplace(const Symbol *Existing, InputFile *NewFile,
236 uint32_t NewFlags) {
Rui Ueyamac03c9042018-02-20 21:08:47 +0000237 // If existing symbol is undefined, replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000238 if (!Existing->isDefined()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000239 LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
240 << Existing->getName() << "\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000241 return true;
242 }
243
244 // Now we have two defined symbols. If the new one is weak, we can ignore it.
245 if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000246 LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000247 return false;
248 }
249
250 // If the existing symbol is weak, we should replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000251 if (Existing->isWeak()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000252 LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000253 return true;
254 }
255
256 // Neither symbol is week. They conflict.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000257 error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
258 toString(Existing->getFile()) + "\n>>> defined in " +
259 toString(NewFile));
Rui Ueyamac03c9042018-02-20 21:08:47 +0000260 return true;
Sam Clegg93e559b2018-02-20 18:55:06 +0000261}
262
263Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000264 InputFile *File,
265 InputFunction *Function) {
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000266 LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " ["
267 << (Function ? toString(Function->Signature) : "none")
268 << "]\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000269 Symbol *S;
270 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000271 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000272
Sam Clegg6540e572019-02-20 23:19:31 +0000273 auto Replace = [&](Symbol* Sym) {
274 // If the new defined function doesn't have signture (i.e. bitcode
275 // functions) but the old symbol does, then preserve the old signature
276 const WasmSignature *OldSig = S->getSignature();
277 auto* NewSym = replaceSymbol<DefinedFunction>(Sym, Name, Flags, File, Function);
278 if (!NewSym->Signature)
279 NewSym->Signature = OldSig;
280 };
281
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000282 if (WasInserted || S->isLazy()) {
Sam Clegg6540e572019-02-20 23:19:31 +0000283 Replace(S);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000284 return S;
285 }
286
Sam Clegg6540e572019-02-20 23:19:31 +0000287 auto ExistingFunction = dyn_cast<FunctionSymbol>(S);
288 if (!ExistingFunction) {
289 reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION);
290 return S;
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000291 }
Sam Clegg6540e572019-02-20 23:19:31 +0000292
Sam Clegg59f959f2019-05-24 22:45:08 +0000293 bool CheckSig = true;
294 if (auto UD = dyn_cast<UndefinedFunction>(ExistingFunction))
295 CheckSig = UD->IsCalledDirectly;
296
297 if (CheckSig && Function && !signatureMatches(ExistingFunction, &Function->Signature)) {
Sam Clegg6540e572019-02-20 23:19:31 +0000298 Symbol* Variant;
299 if (getFunctionVariant(S, &Function->Signature, File, &Variant))
300 // New variant, always replace
301 Replace(Variant);
302 else if (shouldReplace(S, File, Flags))
303 // Variant already exists, replace it after checking shouldReplace
304 Replace(Variant);
305
306 // This variant we found take the place in the symbol table as the primary
307 // variant.
308 replace(Name, Variant);
309 return Variant;
310 }
311
312 // Existing function with matching signature.
313 if (shouldReplace(S, File, Flags))
314 Replace(S);
315
Sam Clegg93e559b2018-02-20 18:55:06 +0000316 return S;
317}
318
Sam Clegg00245532018-02-20 23:38:27 +0000319Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000320 InputFile *File, InputSegment *Segment,
Sam Clegg93102972018-02-23 05:08:53 +0000321 uint32_t Address, uint32_t Size) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000322 LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address
323 << "\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000324 Symbol *S;
325 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000326 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000327
Sam Clegg6540e572019-02-20 23:19:31 +0000328 auto Replace = [&]() {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000329 replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
Sam Clegg6540e572019-02-20 23:19:31 +0000330 };
331
332 if (WasInserted || S->isLazy()) {
333 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000334 return S;
335 }
336
337 checkDataType(S, File);
338
339 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000340 Replace();
Sam Cleggc94d3932017-11-17 18:14:09 +0000341 return S;
342}
343
Sam Clegg93102972018-02-23 05:08:53 +0000344Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000345 InputFile *File, InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000346 LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n");
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000347
Sam Clegg93102972018-02-23 05:08:53 +0000348 Symbol *S;
349 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000350 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000351
Sam Clegg6540e572019-02-20 23:19:31 +0000352 auto Replace = [&]() {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000353 replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
Sam Clegg6540e572019-02-20 23:19:31 +0000354 };
355
356 if (WasInserted || S->isLazy()) {
357 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000358 return S;
359 }
360
361 checkGlobalType(S, File, &Global->getType());
362
363 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000364 Replace();
Sam Clegg93102972018-02-23 05:08:53 +0000365 return S;
366}
367
Heejin Ahne915a712018-12-08 06:17:43 +0000368Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags,
369 InputFile *File, InputEvent *Event) {
370 LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n");
371
372 Symbol *S;
373 bool WasInserted;
374 std::tie(S, WasInserted) = insert(Name, File);
375
Sam Clegg6540e572019-02-20 23:19:31 +0000376 auto Replace = [&]() {
Heejin Ahne915a712018-12-08 06:17:43 +0000377 replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event);
Sam Clegg6540e572019-02-20 23:19:31 +0000378 };
379
380 if (WasInserted || S->isLazy()) {
381 Replace();
Heejin Ahne915a712018-12-08 06:17:43 +0000382 return S;
383 }
384
385 checkEventType(S, File, &Event->getType(), &Event->Signature);
386
387 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000388 Replace();
Heejin Ahne915a712018-12-08 06:17:43 +0000389 return S;
390}
391
Dan Gohman9b84eea2019-02-07 22:00:48 +0000392Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
393 StringRef ImportModule,
Sam Clegg7cc07532019-02-01 02:29:57 +0000394 uint32_t Flags, InputFile *File,
Sam Clegg59f959f2019-05-24 22:45:08 +0000395 const WasmSignature *Sig,
396 bool IsCalledDirectly) {
Sam Cleggd506b0a2019-05-29 15:36:42 +0000397 LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << " ["
398 << (Sig ? toString(*Sig) : "none")
399 << "] IsCalledDirectly:" << IsCalledDirectly << "\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000400
Sam Cleggc94d3932017-11-17 18:14:09 +0000401 Symbol *S;
402 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000403 std::tie(S, WasInserted) = insert(Name, File);
Sam Clegg7991b682019-05-24 13:29:17 +0000404 if (S->Traced)
405 printTraceSymbolUndefined(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000406
Sam Clegg6540e572019-02-20 23:19:31 +0000407 auto Replace = [&]() {
Dan Gohman9b84eea2019-02-07 22:00:48 +0000408 replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags,
Sam Clegg59f959f2019-05-24 22:45:08 +0000409 File, Sig, IsCalledDirectly);
Sam Clegg6540e572019-02-20 23:19:31 +0000410 };
411
412 if (WasInserted)
413 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000414 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000415 Lazy->fetch();
Sam Clegg6540e572019-02-20 23:19:31 +0000416 else {
417 auto ExistingFunction = dyn_cast<FunctionSymbol>(S);
418 if (!ExistingFunction) {
419 reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION);
420 return S;
421 }
Sam Cleggd506b0a2019-05-29 15:36:42 +0000422 if (!ExistingFunction->Signature && Sig)
423 ExistingFunction->Signature = Sig;
Sam Clegg59f959f2019-05-24 22:45:08 +0000424 if (IsCalledDirectly && !signatureMatches(ExistingFunction, Sig))
Sam Clegg6540e572019-02-20 23:19:31 +0000425 if (getFunctionVariant(S, Sig, File, &S))
426 Replace();
427 }
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000428
Sam Cleggc94d3932017-11-17 18:14:09 +0000429 return S;
430}
431
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000432Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags,
433 InputFile *File) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000434 LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000435
436 Symbol *S;
437 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000438 std::tie(S, WasInserted) = insert(Name, File);
Sam Clegg7991b682019-05-24 13:29:17 +0000439 if (S->Traced)
440 printTraceSymbolUndefined(Name, File);
Sam Cleggf989a922018-07-17 19:15:02 +0000441
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000442 if (WasInserted)
443 replaceSymbol<UndefinedData>(S, Name, Flags, File);
444 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000445 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000446 else if (S->isDefined())
447 checkDataType(S, File);
448 return S;
449}
450
Dan Gohman9b84eea2019-02-07 22:00:48 +0000451Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName,
452 StringRef ImportModule, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000453 InputFile *File,
454 const WasmGlobalType *Type) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000455 LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000456
457 Symbol *S;
458 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000459 std::tie(S, WasInserted) = insert(Name, File);
Sam Clegg7991b682019-05-24 13:29:17 +0000460 if (S->Traced)
461 printTraceSymbolUndefined(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000462
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000463 if (WasInserted)
Dan Gohman9b84eea2019-02-07 22:00:48 +0000464 replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags,
465 File, Type);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000466 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000467 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000468 else if (S->isDefined())
469 checkGlobalType(S, File, Type);
470 return S;
471}
472
473void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000474 LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +0000475 StringRef Name = Sym->getName();
Rui Ueyamac03c9042018-02-20 21:08:47 +0000476
Sam Cleggc94d3932017-11-17 18:14:09 +0000477 Symbol *S;
478 bool WasInserted;
Sam Cleggd15a41542019-03-08 21:10:48 +0000479 std::tie(S, WasInserted) = insertName(Name);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000480
Sam Cleggc94d3932017-11-17 18:14:09 +0000481 if (WasInserted) {
Sam Clegg37b4ee52019-01-29 22:26:31 +0000482 replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000483 return;
484 }
485
Sam Clegg37b4ee52019-01-29 22:26:31 +0000486 if (!S->isUndefined())
487 return;
488
489 // The existing symbol is undefined, load a new one from the archive,
490 // unless the the existing symbol is weak in which case replace the undefined
491 // symbols with a LazySymbol.
492 if (S->isWeak()) {
493 const WasmSignature *OldSig = nullptr;
494 // In the case of an UndefinedFunction we need to preserve the expected
495 // signature.
496 if (auto *F = dyn_cast<UndefinedFunction>(S))
497 OldSig = F->Signature;
498 LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
499 auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK,
500 File, *Sym);
501 NewSym->Signature = OldSig;
502 return;
Sam Cleggc94d3932017-11-17 18:14:09 +0000503 }
Sam Clegg37b4ee52019-01-29 22:26:31 +0000504
505 LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
506 File->addMember(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000507}
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000508
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000509bool SymbolTable::addComdat(StringRef Name) {
Sam Clegg697f2142019-05-15 16:03:28 +0000510 return ComdatGroups.insert(CachedHashStringRef(Name)).second;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000511}
Sam Clegg1f3f7742019-02-06 02:35:18 +0000512
Sam Clegg6540e572019-02-20 23:19:31 +0000513// The new signature doesn't match. Create a variant to the symbol with the
514// signature encoded in the name and return that instead. These symbols are
515// then unified later in handleSymbolVariants.
516bool SymbolTable::getFunctionVariant(Symbol* Sym, const WasmSignature *Sig,
517 const InputFile *File, Symbol **Out) {
Richard Trieu14bab092019-02-21 00:36:14 +0000518 LLVM_DEBUG(dbgs() << "getFunctionVariant: " << Sym->getName() << " -> "
Sam Clegg6540e572019-02-20 23:19:31 +0000519 << " " << toString(*Sig) << "\n");
520 Symbol *Variant = nullptr;
521
522 // Linear search through symbol variants. Should never be more than two
523 // or three entries here.
524 auto &Variants = SymVariants[CachedHashStringRef(Sym->getName())];
Fangrui Song196a4402019-04-18 13:33:29 +0000525 if (Variants.empty())
Sam Clegg6540e572019-02-20 23:19:31 +0000526 Variants.push_back(Sym);
527
528 for (Symbol* V : Variants) {
529 if (*V->getSignature() == *Sig) {
530 Variant = V;
531 break;
532 }
533 }
534
535 bool WasAdded = !Variant;
536 if (WasAdded) {
537 // Create a new variant;
538 LLVM_DEBUG(dbgs() << "added new variant\n");
539 Variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
540 Variants.push_back(Variant);
541 } else {
542 LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*Variant) << "\n");
543 assert(*Variant->getSignature() == *Sig);
544 }
545
546 *Out = Variant;
547 return WasAdded;
548}
549
Sam Clegg1f3f7742019-02-06 02:35:18 +0000550// Set a flag for --trace-symbol so that we can print out a log message
551// if a new symbol with the same name is inserted into the symbol table.
552void SymbolTable::trace(StringRef Name) {
553 SymMap.insert({CachedHashStringRef(Name), -1});
554}
Sam Clegg230dc112019-02-07 22:42:16 +0000555
Sam Clegga5ca34e2019-05-24 14:14:25 +0000556void SymbolTable::wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap) {
557 // Swap symbols as instructed by -wrap.
558 int &OrigIdx = SymMap[CachedHashStringRef(Sym->getName())];
559 int &RealIdx= SymMap[CachedHashStringRef(Real->getName())];
560 int &WrapIdx = SymMap[CachedHashStringRef(Wrap->getName())];
561 LLVM_DEBUG(dbgs() << "wrap: " << Sym->getName() << "\n");
562
563 // Anyone looking up __real symbols should get the original
564 RealIdx = OrigIdx;
565 // Anyone looking up the original should get the __wrap symbol
566 OrigIdx = WrapIdx;
567}
568
Sam Clegg230dc112019-02-07 22:42:16 +0000569static const uint8_t UnreachableFn[] = {
570 0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
571 0x00 /* opcode unreachable */, 0x0b /* opcode end */
572};
573
574// Replace the given symbol body with an unreachable function.
575// This is used by handleWeakUndefines in order to generate a callable
Sam Clegg6540e572019-02-20 23:19:31 +0000576// equivalent of an undefined function and also handleSymbolVariants for
577// undefined functions that don't match the signature of the definition.
Sam Clegg230dc112019-02-07 22:42:16 +0000578InputFunction *SymbolTable::replaceWithUnreachable(Symbol *Sym,
579 const WasmSignature &Sig,
580 StringRef DebugName) {
581 auto *Func = make<SyntheticFunction>(Sig, Sym->getName(), DebugName);
582 Func->setBody(UnreachableFn);
583 SyntheticFunctions.emplace_back(Func);
Sam Clegg6540e572019-02-20 23:19:31 +0000584 replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr,
585 Func);
Sam Clegg230dc112019-02-07 22:42:16 +0000586 return Func;
587}
588
589// For weak undefined functions, there may be "call" instructions that reference
590// the symbol. In this case, we need to synthesise a dummy/stub function that
591// will abort at runtime, so that relocations can still provided an operand to
592// the call instruction that passes Wasm validation.
593void SymbolTable::handleWeakUndefines() {
594 for (Symbol *Sym : getSymbols()) {
595 if (!Sym->isUndefWeak())
596 continue;
597
Sam Clegg6540e572019-02-20 23:19:31 +0000598 const WasmSignature *Sig = Sym->getSignature();
599 if (!Sig) {
Sam Clegg230dc112019-02-07 22:42:16 +0000600 // It is possible for undefined functions not to have a signature (eg. if
601 // added via "--undefined"), but weak undefined ones do have a signature.
Sam Clegg6540e572019-02-20 23:19:31 +0000602 // Lazy symbols may not be functions and therefore Sig can still be null
603 // in some circumstantce.
604 assert(!isa<FunctionSymbol>(Sym));
Sam Clegg230dc112019-02-07 22:42:16 +0000605 continue;
Sam Clegg6540e572019-02-20 23:19:31 +0000606 }
Sam Clegg230dc112019-02-07 22:42:16 +0000607
608 // Add a synthetic dummy for weak undefined functions. These dummies will
609 // be GC'd if not used as the target of any "call" instructions.
610 StringRef DebugName = Saver.save("undefined:" + toString(*Sym));
611 InputFunction* Func = replaceWithUnreachable(Sym, *Sig, DebugName);
612 // Ensure it compares equal to the null pointer, and so that table relocs
613 // don't pull in the stub body (only call-operand relocs should do that).
614 Func->setTableIndex(0);
615 // Hide our dummy to prevent export.
616 Sym->setHidden(true);
617 }
618}
Sam Clegg6540e572019-02-20 23:19:31 +0000619
620static void reportFunctionSignatureMismatch(StringRef SymName,
621 FunctionSymbol *A,
622 FunctionSymbol *B, bool Error) {
623 std::string msg = ("function signature mismatch: " + SymName +
624 "\n>>> defined as " + toString(*A->Signature) + " in " +
625 toString(A->getFile()) + "\n>>> defined as " +
626 toString(*B->Signature) + " in " + toString(B->getFile()))
627 .str();
628 if (Error)
629 error(msg);
630 else
631 warn(msg);
632}
633
634// Remove any variant symbols that were created due to function signature
635// mismatches.
636void SymbolTable::handleSymbolVariants() {
637 for (auto Pair : SymVariants) {
638 // Push the initial symbol onto the list of variants.
639 StringRef SymName = Pair.first.val();
640 std::vector<Symbol *> &Variants = Pair.second;
641
642#ifndef NDEBUG
Sam Cleggf8d736f2019-02-21 01:33:26 +0000643 LLVM_DEBUG(dbgs() << "symbol with (" << Variants.size()
644 << ") variants: " << SymName << "\n");
Sam Clegg6540e572019-02-20 23:19:31 +0000645 for (auto *S: Variants) {
646 auto *F = cast<FunctionSymbol>(S);
Sam Cleggf8d736f2019-02-21 01:33:26 +0000647 LLVM_DEBUG(dbgs() << " variant: " + F->getName() << " "
648 << toString(*F->Signature) << "\n");
Sam Clegg6540e572019-02-20 23:19:31 +0000649 }
650#endif
651
652 // Find the one definition.
653 DefinedFunction *Defined = nullptr;
654 for (auto *Symbol : Variants) {
655 if (auto F = dyn_cast<DefinedFunction>(Symbol)) {
656 Defined = F;
657 break;
658 }
659 }
660
661 // If there are no definitions, and the undefined symbols disagree on
662 // the signature, there is not we can do since we don't know which one
663 // to use as the signature on the import.
664 if (!Defined) {
665 reportFunctionSignatureMismatch(SymName,
666 cast<FunctionSymbol>(Variants[0]),
667 cast<FunctionSymbol>(Variants[1]), true);
668 return;
669 }
670
671 for (auto *Symbol : Variants) {
672 if (Symbol != Defined) {
673 auto *F = cast<FunctionSymbol>(Symbol);
674 reportFunctionSignatureMismatch(SymName, F, Defined, false);
675 StringRef DebugName = Saver.save("unreachable:" + toString(*F));
676 replaceWithUnreachable(F, *F->Signature, DebugName);
677 }
678 }
679 }
680}