blob: ae424749ffc6348b7cd6cede96133b30f0a4a4b9 [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 Cleggcefbf9a2018-06-28 16:53:53 +0000139 if (!NewSig)
Sam Clegg6540e572019-02-20 23:19:31 +0000140 return true;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000141
Sam Clegg6540e572019-02-20 23:19:31 +0000142 const WasmSignature *OldSig = Existing->Signature;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000143 if (!OldSig) {
Sam Clegg6540e572019-02-20 23:19:31 +0000144 Existing->Signature = NewSig;
145 return true;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000146 }
147
Sam Clegg6540e572019-02-20 23:19:31 +0000148 return *NewSig == *OldSig;
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000149}
150
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000151static void checkGlobalType(const Symbol *Existing, const InputFile *File,
152 const WasmGlobalType *NewType) {
153 if (!isa<GlobalSymbol>(Existing)) {
Sam Clegg3876d89a2018-05-14 22:42:33 +0000154 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL);
Sam Cleggb8621592017-11-30 01:40:08 +0000155 return;
156 }
157
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000158 const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType();
159 if (*NewType != *OldType) {
160 error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " +
161 toString(*OldType) + " in " + toString(Existing->getFile()) +
162 "\n>>> defined as " + toString(*NewType) + " in " + toString(File));
Sam Clegg93102972018-02-23 05:08:53 +0000163 }
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000164}
165
Heejin Ahne915a712018-12-08 06:17:43 +0000166static void checkEventType(const Symbol *Existing, const InputFile *File,
167 const WasmEventType *NewType,
168 const WasmSignature *NewSig) {
169 auto ExistingEvent = dyn_cast<EventSymbol>(Existing);
170 if (!isa<EventSymbol>(Existing)) {
171 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_EVENT);
172 return;
173 }
174
175 const WasmEventType *OldType = cast<EventSymbol>(Existing)->getEventType();
176 const WasmSignature *OldSig = ExistingEvent->Signature;
177 if (NewType->Attribute != OldType->Attribute)
178 error("Event type mismatch: " + Existing->getName() + "\n>>> defined as " +
179 toString(*OldType) + " in " + toString(Existing->getFile()) +
180 "\n>>> defined as " + toString(*NewType) + " in " + toString(File));
181 if (*NewSig != *OldSig)
182 warn("Event signature mismatch: " + Existing->getName() +
183 "\n>>> defined as " + toString(*OldSig) + " in " +
184 toString(Existing->getFile()) + "\n>>> defined as " +
185 toString(*NewSig) + " in " + toString(File));
186}
187
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000188static void checkDataType(const Symbol *Existing, const InputFile *File) {
189 if (!isa<DataSymbol>(Existing))
Sam Clegg3876d89a2018-05-14 22:42:33 +0000190 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000191}
192
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000193DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name,
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000194 uint32_t Flags,
195 InputFunction *Function) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000196 LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000197 assert(!find(Name));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000198 SyntheticFunctions.emplace_back(Function);
Sam Cleggd15a41542019-03-08 21:10:48 +0000199 return replaceSymbol<DefinedFunction>(insertName(Name).first, Name,
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000200 Flags, nullptr, Function);
Sam Clegg50686852018-01-12 18:35:13 +0000201}
202
Sam Clegg4bce63a2019-05-23 10:06:03 +0000203DefinedData *SymbolTable::addOptionalDataSymbol(StringRef Name, uint32_t Value,
204 uint32_t Flags) {
205 Symbol *S = find(Name);
206 if (!S || S->isDefined())
207 return nullptr;
208 LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << Name << "\n");
209 auto *rtn = replaceSymbol<DefinedData>(S, Name, Flags);
210 rtn->setVirtualAddress(Value);
211 return rtn;
212}
213
Sam Clegg00245532018-02-20 23:38:27 +0000214DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name,
215 uint32_t Flags) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000216 LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000217 assert(!find(Name));
Sam Cleggd15a41542019-03-08 21:10:48 +0000218 return replaceSymbol<DefinedData>(insertName(Name).first, Name, Flags);
Sam Cleggc94d3932017-11-17 18:14:09 +0000219}
220
Sam Clegg93102972018-02-23 05:08:53 +0000221DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags,
222 InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000223 LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global
224 << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000225 assert(!find(Name));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000226 SyntheticGlobals.emplace_back(Global);
Sam Cleggd15a41542019-03-08 21:10:48 +0000227 return replaceSymbol<DefinedGlobal>(insertName(Name).first, Name, Flags,
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000228 nullptr, Global);
Sam Clegg93102972018-02-23 05:08:53 +0000229}
230
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000231static bool shouldReplace(const Symbol *Existing, InputFile *NewFile,
232 uint32_t NewFlags) {
Rui Ueyamac03c9042018-02-20 21:08:47 +0000233 // If existing symbol is undefined, replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000234 if (!Existing->isDefined()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000235 LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
236 << Existing->getName() << "\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000237 return true;
238 }
239
240 // Now we have two defined symbols. If the new one is weak, we can ignore it.
241 if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000242 LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000243 return false;
244 }
245
246 // If the existing symbol is weak, we should replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000247 if (Existing->isWeak()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000248 LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000249 return true;
250 }
251
252 // Neither symbol is week. They conflict.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000253 error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
254 toString(Existing->getFile()) + "\n>>> defined in " +
255 toString(NewFile));
Rui Ueyamac03c9042018-02-20 21:08:47 +0000256 return true;
Sam Clegg93e559b2018-02-20 18:55:06 +0000257}
258
259Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000260 InputFile *File,
261 InputFunction *Function) {
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000262 LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " ["
263 << (Function ? toString(Function->Signature) : "none")
264 << "]\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000265 Symbol *S;
266 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000267 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000268
Sam Clegg6540e572019-02-20 23:19:31 +0000269 auto Replace = [&](Symbol* Sym) {
270 // If the new defined function doesn't have signture (i.e. bitcode
271 // functions) but the old symbol does, then preserve the old signature
272 const WasmSignature *OldSig = S->getSignature();
273 auto* NewSym = replaceSymbol<DefinedFunction>(Sym, Name, Flags, File, Function);
274 if (!NewSym->Signature)
275 NewSym->Signature = OldSig;
276 };
277
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000278 if (WasInserted || S->isLazy()) {
Sam Clegg6540e572019-02-20 23:19:31 +0000279 Replace(S);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000280 return S;
281 }
282
Sam Clegg6540e572019-02-20 23:19:31 +0000283 auto ExistingFunction = dyn_cast<FunctionSymbol>(S);
284 if (!ExistingFunction) {
285 reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION);
286 return S;
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000287 }
Sam Clegg6540e572019-02-20 23:19:31 +0000288
Sam Clegg59f959f2019-05-24 22:45:08 +0000289 bool CheckSig = true;
290 if (auto UD = dyn_cast<UndefinedFunction>(ExistingFunction))
291 CheckSig = UD->IsCalledDirectly;
292
293 if (CheckSig && Function && !signatureMatches(ExistingFunction, &Function->Signature)) {
Sam Clegg6540e572019-02-20 23:19:31 +0000294 Symbol* Variant;
295 if (getFunctionVariant(S, &Function->Signature, File, &Variant))
296 // New variant, always replace
297 Replace(Variant);
298 else if (shouldReplace(S, File, Flags))
299 // Variant already exists, replace it after checking shouldReplace
300 Replace(Variant);
301
302 // This variant we found take the place in the symbol table as the primary
303 // variant.
304 replace(Name, Variant);
305 return Variant;
306 }
307
308 // Existing function with matching signature.
309 if (shouldReplace(S, File, Flags))
310 Replace(S);
311
Sam Clegg93e559b2018-02-20 18:55:06 +0000312 return S;
313}
314
Sam Clegg00245532018-02-20 23:38:27 +0000315Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000316 InputFile *File, InputSegment *Segment,
Sam Clegg93102972018-02-23 05:08:53 +0000317 uint32_t Address, uint32_t Size) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000318 LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address
319 << "\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000320 Symbol *S;
321 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000322 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000323
Sam Clegg6540e572019-02-20 23:19:31 +0000324 auto Replace = [&]() {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000325 replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
Sam Clegg6540e572019-02-20 23:19:31 +0000326 };
327
328 if (WasInserted || S->isLazy()) {
329 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000330 return S;
331 }
332
333 checkDataType(S, File);
334
335 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000336 Replace();
Sam Cleggc94d3932017-11-17 18:14:09 +0000337 return S;
338}
339
Sam Clegg93102972018-02-23 05:08:53 +0000340Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000341 InputFile *File, InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000342 LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n");
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000343
Sam Clegg93102972018-02-23 05:08:53 +0000344 Symbol *S;
345 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000346 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000347
Sam Clegg6540e572019-02-20 23:19:31 +0000348 auto Replace = [&]() {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000349 replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
Sam Clegg6540e572019-02-20 23:19:31 +0000350 };
351
352 if (WasInserted || S->isLazy()) {
353 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000354 return S;
355 }
356
357 checkGlobalType(S, File, &Global->getType());
358
359 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000360 Replace();
Sam Clegg93102972018-02-23 05:08:53 +0000361 return S;
362}
363
Heejin Ahne915a712018-12-08 06:17:43 +0000364Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags,
365 InputFile *File, InputEvent *Event) {
366 LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n");
367
368 Symbol *S;
369 bool WasInserted;
370 std::tie(S, WasInserted) = insert(Name, File);
371
Sam Clegg6540e572019-02-20 23:19:31 +0000372 auto Replace = [&]() {
Heejin Ahne915a712018-12-08 06:17:43 +0000373 replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event);
Sam Clegg6540e572019-02-20 23:19:31 +0000374 };
375
376 if (WasInserted || S->isLazy()) {
377 Replace();
Heejin Ahne915a712018-12-08 06:17:43 +0000378 return S;
379 }
380
381 checkEventType(S, File, &Event->getType(), &Event->Signature);
382
383 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000384 Replace();
Heejin Ahne915a712018-12-08 06:17:43 +0000385 return S;
386}
387
Dan Gohman9b84eea2019-02-07 22:00:48 +0000388Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
389 StringRef ImportModule,
Sam Clegg7cc07532019-02-01 02:29:57 +0000390 uint32_t Flags, InputFile *File,
Sam Clegg59f959f2019-05-24 22:45:08 +0000391 const WasmSignature *Sig,
392 bool IsCalledDirectly) {
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000393 LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name <<
394 " [" << (Sig ? toString(*Sig) : "none") << "]\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000395
Sam Cleggc94d3932017-11-17 18:14:09 +0000396 Symbol *S;
397 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000398 std::tie(S, WasInserted) = insert(Name, File);
Sam Clegg7991b682019-05-24 13:29:17 +0000399 if (S->Traced)
400 printTraceSymbolUndefined(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000401
Sam Clegg6540e572019-02-20 23:19:31 +0000402 auto Replace = [&]() {
Dan Gohman9b84eea2019-02-07 22:00:48 +0000403 replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags,
Sam Clegg59f959f2019-05-24 22:45:08 +0000404 File, Sig, IsCalledDirectly);
Sam Clegg6540e572019-02-20 23:19:31 +0000405 };
406
407 if (WasInserted)
408 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000409 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000410 Lazy->fetch();
Sam Clegg6540e572019-02-20 23:19:31 +0000411 else {
412 auto ExistingFunction = dyn_cast<FunctionSymbol>(S);
413 if (!ExistingFunction) {
414 reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION);
415 return S;
416 }
Sam Clegg59f959f2019-05-24 22:45:08 +0000417 if (IsCalledDirectly && !signatureMatches(ExistingFunction, Sig))
Sam Clegg6540e572019-02-20 23:19:31 +0000418 if (getFunctionVariant(S, Sig, File, &S))
419 Replace();
420 }
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000421
Sam Cleggc94d3932017-11-17 18:14:09 +0000422 return S;
423}
424
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000425Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags,
426 InputFile *File) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000427 LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000428
429 Symbol *S;
430 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000431 std::tie(S, WasInserted) = insert(Name, File);
Sam Clegg7991b682019-05-24 13:29:17 +0000432 if (S->Traced)
433 printTraceSymbolUndefined(Name, File);
Sam Cleggf989a922018-07-17 19:15:02 +0000434
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000435 if (WasInserted)
436 replaceSymbol<UndefinedData>(S, Name, Flags, File);
437 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000438 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000439 else if (S->isDefined())
440 checkDataType(S, File);
441 return S;
442}
443
Dan Gohman9b84eea2019-02-07 22:00:48 +0000444Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName,
445 StringRef ImportModule, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000446 InputFile *File,
447 const WasmGlobalType *Type) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000448 LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000449
450 Symbol *S;
451 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000452 std::tie(S, WasInserted) = insert(Name, File);
Sam Clegg7991b682019-05-24 13:29:17 +0000453 if (S->Traced)
454 printTraceSymbolUndefined(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000455
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000456 if (WasInserted)
Dan Gohman9b84eea2019-02-07 22:00:48 +0000457 replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags,
458 File, Type);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000459 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000460 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000461 else if (S->isDefined())
462 checkGlobalType(S, File, Type);
463 return S;
464}
465
466void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000467 LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +0000468 StringRef Name = Sym->getName();
Rui Ueyamac03c9042018-02-20 21:08:47 +0000469
Sam Cleggc94d3932017-11-17 18:14:09 +0000470 Symbol *S;
471 bool WasInserted;
Sam Cleggd15a41542019-03-08 21:10:48 +0000472 std::tie(S, WasInserted) = insertName(Name);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000473
Sam Cleggc94d3932017-11-17 18:14:09 +0000474 if (WasInserted) {
Sam Clegg37b4ee52019-01-29 22:26:31 +0000475 replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000476 return;
477 }
478
Sam Clegg37b4ee52019-01-29 22:26:31 +0000479 if (!S->isUndefined())
480 return;
481
482 // The existing symbol is undefined, load a new one from the archive,
483 // unless the the existing symbol is weak in which case replace the undefined
484 // symbols with a LazySymbol.
485 if (S->isWeak()) {
486 const WasmSignature *OldSig = nullptr;
487 // In the case of an UndefinedFunction we need to preserve the expected
488 // signature.
489 if (auto *F = dyn_cast<UndefinedFunction>(S))
490 OldSig = F->Signature;
491 LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
492 auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK,
493 File, *Sym);
494 NewSym->Signature = OldSig;
495 return;
Sam Cleggc94d3932017-11-17 18:14:09 +0000496 }
Sam Clegg37b4ee52019-01-29 22:26:31 +0000497
498 LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
499 File->addMember(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000500}
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000501
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000502bool SymbolTable::addComdat(StringRef Name) {
Sam Clegg697f2142019-05-15 16:03:28 +0000503 return ComdatGroups.insert(CachedHashStringRef(Name)).second;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000504}
Sam Clegg1f3f7742019-02-06 02:35:18 +0000505
Sam Clegg6540e572019-02-20 23:19:31 +0000506// The new signature doesn't match. Create a variant to the symbol with the
507// signature encoded in the name and return that instead. These symbols are
508// then unified later in handleSymbolVariants.
509bool SymbolTable::getFunctionVariant(Symbol* Sym, const WasmSignature *Sig,
510 const InputFile *File, Symbol **Out) {
Richard Trieu14bab092019-02-21 00:36:14 +0000511 LLVM_DEBUG(dbgs() << "getFunctionVariant: " << Sym->getName() << " -> "
Sam Clegg6540e572019-02-20 23:19:31 +0000512 << " " << toString(*Sig) << "\n");
513 Symbol *Variant = nullptr;
514
515 // Linear search through symbol variants. Should never be more than two
516 // or three entries here.
517 auto &Variants = SymVariants[CachedHashStringRef(Sym->getName())];
Fangrui Song196a4402019-04-18 13:33:29 +0000518 if (Variants.empty())
Sam Clegg6540e572019-02-20 23:19:31 +0000519 Variants.push_back(Sym);
520
521 for (Symbol* V : Variants) {
522 if (*V->getSignature() == *Sig) {
523 Variant = V;
524 break;
525 }
526 }
527
528 bool WasAdded = !Variant;
529 if (WasAdded) {
530 // Create a new variant;
531 LLVM_DEBUG(dbgs() << "added new variant\n");
532 Variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
533 Variants.push_back(Variant);
534 } else {
535 LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*Variant) << "\n");
536 assert(*Variant->getSignature() == *Sig);
537 }
538
539 *Out = Variant;
540 return WasAdded;
541}
542
Sam Clegg1f3f7742019-02-06 02:35:18 +0000543// Set a flag for --trace-symbol so that we can print out a log message
544// if a new symbol with the same name is inserted into the symbol table.
545void SymbolTable::trace(StringRef Name) {
546 SymMap.insert({CachedHashStringRef(Name), -1});
547}
Sam Clegg230dc112019-02-07 22:42:16 +0000548
Sam Clegga5ca34e2019-05-24 14:14:25 +0000549void SymbolTable::wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap) {
550 // Swap symbols as instructed by -wrap.
551 int &OrigIdx = SymMap[CachedHashStringRef(Sym->getName())];
552 int &RealIdx= SymMap[CachedHashStringRef(Real->getName())];
553 int &WrapIdx = SymMap[CachedHashStringRef(Wrap->getName())];
554 LLVM_DEBUG(dbgs() << "wrap: " << Sym->getName() << "\n");
555
556 // Anyone looking up __real symbols should get the original
557 RealIdx = OrigIdx;
558 // Anyone looking up the original should get the __wrap symbol
559 OrigIdx = WrapIdx;
560}
561
Sam Clegg230dc112019-02-07 22:42:16 +0000562static const uint8_t UnreachableFn[] = {
563 0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
564 0x00 /* opcode unreachable */, 0x0b /* opcode end */
565};
566
567// Replace the given symbol body with an unreachable function.
568// This is used by handleWeakUndefines in order to generate a callable
Sam Clegg6540e572019-02-20 23:19:31 +0000569// equivalent of an undefined function and also handleSymbolVariants for
570// undefined functions that don't match the signature of the definition.
Sam Clegg230dc112019-02-07 22:42:16 +0000571InputFunction *SymbolTable::replaceWithUnreachable(Symbol *Sym,
572 const WasmSignature &Sig,
573 StringRef DebugName) {
574 auto *Func = make<SyntheticFunction>(Sig, Sym->getName(), DebugName);
575 Func->setBody(UnreachableFn);
576 SyntheticFunctions.emplace_back(Func);
Sam Clegg6540e572019-02-20 23:19:31 +0000577 replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr,
578 Func);
Sam Clegg230dc112019-02-07 22:42:16 +0000579 return Func;
580}
581
582// For weak undefined functions, there may be "call" instructions that reference
583// the symbol. In this case, we need to synthesise a dummy/stub function that
584// will abort at runtime, so that relocations can still provided an operand to
585// the call instruction that passes Wasm validation.
586void SymbolTable::handleWeakUndefines() {
587 for (Symbol *Sym : getSymbols()) {
588 if (!Sym->isUndefWeak())
589 continue;
590
Sam Clegg6540e572019-02-20 23:19:31 +0000591 const WasmSignature *Sig = Sym->getSignature();
592 if (!Sig) {
Sam Clegg230dc112019-02-07 22:42:16 +0000593 // It is possible for undefined functions not to have a signature (eg. if
594 // added via "--undefined"), but weak undefined ones do have a signature.
Sam Clegg6540e572019-02-20 23:19:31 +0000595 // Lazy symbols may not be functions and therefore Sig can still be null
596 // in some circumstantce.
597 assert(!isa<FunctionSymbol>(Sym));
Sam Clegg230dc112019-02-07 22:42:16 +0000598 continue;
Sam Clegg6540e572019-02-20 23:19:31 +0000599 }
Sam Clegg230dc112019-02-07 22:42:16 +0000600
601 // Add a synthetic dummy for weak undefined functions. These dummies will
602 // be GC'd if not used as the target of any "call" instructions.
603 StringRef DebugName = Saver.save("undefined:" + toString(*Sym));
604 InputFunction* Func = replaceWithUnreachable(Sym, *Sig, DebugName);
605 // Ensure it compares equal to the null pointer, and so that table relocs
606 // don't pull in the stub body (only call-operand relocs should do that).
607 Func->setTableIndex(0);
608 // Hide our dummy to prevent export.
609 Sym->setHidden(true);
610 }
611}
Sam Clegg6540e572019-02-20 23:19:31 +0000612
613static void reportFunctionSignatureMismatch(StringRef SymName,
614 FunctionSymbol *A,
615 FunctionSymbol *B, bool Error) {
616 std::string msg = ("function signature mismatch: " + SymName +
617 "\n>>> defined as " + toString(*A->Signature) + " in " +
618 toString(A->getFile()) + "\n>>> defined as " +
619 toString(*B->Signature) + " in " + toString(B->getFile()))
620 .str();
621 if (Error)
622 error(msg);
623 else
624 warn(msg);
625}
626
627// Remove any variant symbols that were created due to function signature
628// mismatches.
629void SymbolTable::handleSymbolVariants() {
630 for (auto Pair : SymVariants) {
631 // Push the initial symbol onto the list of variants.
632 StringRef SymName = Pair.first.val();
633 std::vector<Symbol *> &Variants = Pair.second;
634
635#ifndef NDEBUG
Sam Cleggf8d736f2019-02-21 01:33:26 +0000636 LLVM_DEBUG(dbgs() << "symbol with (" << Variants.size()
637 << ") variants: " << SymName << "\n");
Sam Clegg6540e572019-02-20 23:19:31 +0000638 for (auto *S: Variants) {
639 auto *F = cast<FunctionSymbol>(S);
Sam Cleggf8d736f2019-02-21 01:33:26 +0000640 LLVM_DEBUG(dbgs() << " variant: " + F->getName() << " "
641 << toString(*F->Signature) << "\n");
Sam Clegg6540e572019-02-20 23:19:31 +0000642 }
643#endif
644
645 // Find the one definition.
646 DefinedFunction *Defined = nullptr;
647 for (auto *Symbol : Variants) {
648 if (auto F = dyn_cast<DefinedFunction>(Symbol)) {
649 Defined = F;
650 break;
651 }
652 }
653
654 // If there are no definitions, and the undefined symbols disagree on
655 // the signature, there is not we can do since we don't know which one
656 // to use as the signature on the import.
657 if (!Defined) {
658 reportFunctionSignatureMismatch(SymName,
659 cast<FunctionSymbol>(Variants[0]),
660 cast<FunctionSymbol>(Variants[1]), true);
661 return;
662 }
663
664 for (auto *Symbol : Variants) {
665 if (Symbol != Defined) {
666 auto *F = cast<FunctionSymbol>(Symbol);
667 reportFunctionSignatureMismatch(SymName, F, Defined, false);
668 StringRef DebugName = Saver.save("unreachable:" + toString(*F));
669 replaceWithUnreachable(F, *F->Signature, DebugName);
670 }
671 }
672 }
673}