blob: 5d7ed8c1bc06ae28527d57aca14a745ec95a0271 [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()) {
61 auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp"));
62 Obj->parse();
63 ObjectFiles.push_back(Obj);
64 }
65}
66
Sam Cleggc94d3932017-11-17 18:14:09 +000067void SymbolTable::reportRemainingUndefines() {
Sam Clegg74fe0ba2017-12-07 01:51:24 +000068 for (Symbol *Sym : SymVector) {
Sam Cleggc729c1b2018-05-30 18:07:52 +000069 if (!Sym->isUndefined() || Sym->isWeak())
70 continue;
71 if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0)
72 continue;
73 if (!Sym->IsUsedInRegularObj)
74 continue;
Sam Clegg47e2b6b2018-08-04 00:04:06 +000075 error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym));
Sam Cleggc94d3932017-11-17 18:14:09 +000076 }
Sam Cleggc94d3932017-11-17 18:14:09 +000077}
78
79Symbol *SymbolTable::find(StringRef Name) {
Sam Clegg1f3f7742019-02-06 02:35:18 +000080 auto It = SymMap.find(CachedHashStringRef(Name));
81 if (It == SymMap.end() || It->second == -1)
82 return nullptr;
83 return SymVector[It->second];
84}
85
Sam Clegg6540e572019-02-20 23:19:31 +000086void SymbolTable::replace(StringRef Name, Symbol* Sym) {
87 auto It = SymMap.find(CachedHashStringRef(Name));
88 SymVector[It->second] = Sym;
89}
90
Sam Clegg1f3f7742019-02-06 02:35:18 +000091std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) {
92 bool Trace = false;
93 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
94 int &SymIndex = P.first->second;
95 bool IsNew = P.second;
96 if (SymIndex == -1) {
97 SymIndex = SymVector.size();
98 Trace = true;
99 IsNew = true;
100 }
101
102 if (!IsNew)
103 return {SymVector[SymIndex], false};
104
105 Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
106 Sym->IsUsedInRegularObj = false;
107 Sym->Traced = Trace;
108 SymVector.emplace_back(Sym);
109 return {Sym, true};
Sam Cleggc94d3932017-11-17 18:14:09 +0000110}
111
Sam Clegg230dc112019-02-07 22:42:16 +0000112std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name,
113 const InputFile *File) {
Sam Clegg1f3f7742019-02-06 02:35:18 +0000114 Symbol *S;
115 bool WasInserted;
116 std::tie(S, WasInserted) = insertName(Name);
117
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000118 if (!File || File->kind() == InputFile::ObjectKind)
Sam Clegg1f3f7742019-02-06 02:35:18 +0000119 S->IsUsedInRegularObj = true;
120
121 return {S, WasInserted};
Sam Cleggc94d3932017-11-17 18:14:09 +0000122}
123
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000124static void reportTypeError(const Symbol *Existing, const InputFile *File,
Sam Clegg3876d89a2018-05-14 22:42:33 +0000125 llvm::wasm::WasmSymbolType Type) {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000126 error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " +
127 toString(Existing->getWasmType()) + " in " +
Sam Clegg3876d89a2018-05-14 22:42:33 +0000128 toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) +
129 " in " + toString(File));
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000130}
131
Heejin Ahn6f4286f2018-11-19 23:31:28 +0000132// Check the type of new symbol matches that of the symbol is replacing.
Sam Clegg6540e572019-02-20 23:19:31 +0000133// Returns true if the function types match, false is there is a singature
134// mismatch.
135bool signatureMatches(FunctionSymbol *Existing, const WasmSignature *NewSig) {
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000136 if (!NewSig)
Sam Clegg6540e572019-02-20 23:19:31 +0000137 return true;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000138
Sam Clegg6540e572019-02-20 23:19:31 +0000139 const WasmSignature *OldSig = Existing->Signature;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000140 if (!OldSig) {
Sam Clegg6540e572019-02-20 23:19:31 +0000141 Existing->Signature = NewSig;
142 return true;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000143 }
144
Sam Clegg6540e572019-02-20 23:19:31 +0000145 return *NewSig == *OldSig;
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000146}
147
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000148static void checkGlobalType(const Symbol *Existing, const InputFile *File,
149 const WasmGlobalType *NewType) {
150 if (!isa<GlobalSymbol>(Existing)) {
Sam Clegg3876d89a2018-05-14 22:42:33 +0000151 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL);
Sam Cleggb8621592017-11-30 01:40:08 +0000152 return;
153 }
154
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000155 const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType();
156 if (*NewType != *OldType) {
157 error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " +
158 toString(*OldType) + " in " + toString(Existing->getFile()) +
159 "\n>>> defined as " + toString(*NewType) + " in " + toString(File));
Sam Clegg93102972018-02-23 05:08:53 +0000160 }
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000161}
162
Heejin Ahne915a712018-12-08 06:17:43 +0000163static void checkEventType(const Symbol *Existing, const InputFile *File,
164 const WasmEventType *NewType,
165 const WasmSignature *NewSig) {
166 auto ExistingEvent = dyn_cast<EventSymbol>(Existing);
167 if (!isa<EventSymbol>(Existing)) {
168 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_EVENT);
169 return;
170 }
171
172 const WasmEventType *OldType = cast<EventSymbol>(Existing)->getEventType();
173 const WasmSignature *OldSig = ExistingEvent->Signature;
174 if (NewType->Attribute != OldType->Attribute)
175 error("Event type mismatch: " + Existing->getName() + "\n>>> defined as " +
176 toString(*OldType) + " in " + toString(Existing->getFile()) +
177 "\n>>> defined as " + toString(*NewType) + " in " + toString(File));
178 if (*NewSig != *OldSig)
179 warn("Event signature mismatch: " + Existing->getName() +
180 "\n>>> defined as " + toString(*OldSig) + " in " +
181 toString(Existing->getFile()) + "\n>>> defined as " +
182 toString(*NewSig) + " in " + toString(File));
183}
184
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000185static void checkDataType(const Symbol *Existing, const InputFile *File) {
186 if (!isa<DataSymbol>(Existing))
Sam Clegg3876d89a2018-05-14 22:42:33 +0000187 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000188}
189
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000190DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name,
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000191 uint32_t Flags,
192 InputFunction *Function) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000193 LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000194 assert(!find(Name));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000195 SyntheticFunctions.emplace_back(Function);
Sam Cleggd15a41542019-03-08 21:10:48 +0000196 return replaceSymbol<DefinedFunction>(insertName(Name).first, Name,
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000197 Flags, nullptr, Function);
Sam Clegg50686852018-01-12 18:35:13 +0000198}
199
Sam Clegg00245532018-02-20 23:38:27 +0000200DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name,
201 uint32_t Flags) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000202 LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000203 assert(!find(Name));
Sam Cleggd15a41542019-03-08 21:10:48 +0000204 return replaceSymbol<DefinedData>(insertName(Name).first, Name, Flags);
Sam Cleggc94d3932017-11-17 18:14:09 +0000205}
206
Sam Clegg93102972018-02-23 05:08:53 +0000207DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags,
208 InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000209 LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global
210 << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000211 assert(!find(Name));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000212 SyntheticGlobals.emplace_back(Global);
Sam Cleggd15a41542019-03-08 21:10:48 +0000213 return replaceSymbol<DefinedGlobal>(insertName(Name).first, Name, Flags,
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000214 nullptr, Global);
Sam Clegg93102972018-02-23 05:08:53 +0000215}
216
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000217static bool shouldReplace(const Symbol *Existing, InputFile *NewFile,
218 uint32_t NewFlags) {
Rui Ueyamac03c9042018-02-20 21:08:47 +0000219 // If existing symbol is undefined, replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000220 if (!Existing->isDefined()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000221 LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
222 << Existing->getName() << "\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000223 return true;
224 }
225
226 // Now we have two defined symbols. If the new one is weak, we can ignore it.
227 if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000228 LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000229 return false;
230 }
231
232 // If the existing symbol is weak, we should replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000233 if (Existing->isWeak()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000234 LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000235 return true;
236 }
237
238 // Neither symbol is week. They conflict.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000239 error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
240 toString(Existing->getFile()) + "\n>>> defined in " +
241 toString(NewFile));
Rui Ueyamac03c9042018-02-20 21:08:47 +0000242 return true;
Sam Clegg93e559b2018-02-20 18:55:06 +0000243}
244
245Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000246 InputFile *File,
247 InputFunction *Function) {
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000248 LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " ["
249 << (Function ? toString(Function->Signature) : "none")
250 << "]\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000251 Symbol *S;
252 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000253 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000254
Sam Clegg6540e572019-02-20 23:19:31 +0000255 auto Replace = [&](Symbol* Sym) {
256 // If the new defined function doesn't have signture (i.e. bitcode
257 // functions) but the old symbol does, then preserve the old signature
258 const WasmSignature *OldSig = S->getSignature();
259 auto* NewSym = replaceSymbol<DefinedFunction>(Sym, Name, Flags, File, Function);
260 if (!NewSym->Signature)
261 NewSym->Signature = OldSig;
262 };
263
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000264 if (WasInserted || S->isLazy()) {
Sam Clegg6540e572019-02-20 23:19:31 +0000265 Replace(S);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000266 return S;
267 }
268
Sam Clegg6540e572019-02-20 23:19:31 +0000269 auto ExistingFunction = dyn_cast<FunctionSymbol>(S);
270 if (!ExistingFunction) {
271 reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION);
272 return S;
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000273 }
Sam Clegg6540e572019-02-20 23:19:31 +0000274
275 if (Function && !signatureMatches(ExistingFunction, &Function->Signature)) {
276 Symbol* Variant;
277 if (getFunctionVariant(S, &Function->Signature, File, &Variant))
278 // New variant, always replace
279 Replace(Variant);
280 else if (shouldReplace(S, File, Flags))
281 // Variant already exists, replace it after checking shouldReplace
282 Replace(Variant);
283
284 // This variant we found take the place in the symbol table as the primary
285 // variant.
286 replace(Name, Variant);
287 return Variant;
288 }
289
290 // Existing function with matching signature.
291 if (shouldReplace(S, File, Flags))
292 Replace(S);
293
Sam Clegg93e559b2018-02-20 18:55:06 +0000294 return S;
295}
296
Sam Clegg00245532018-02-20 23:38:27 +0000297Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000298 InputFile *File, InputSegment *Segment,
Sam Clegg93102972018-02-23 05:08:53 +0000299 uint32_t Address, uint32_t Size) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000300 LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address
301 << "\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000302 Symbol *S;
303 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000304 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000305
Sam Clegg6540e572019-02-20 23:19:31 +0000306 auto Replace = [&]() {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000307 replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
Sam Clegg6540e572019-02-20 23:19:31 +0000308 };
309
310 if (WasInserted || S->isLazy()) {
311 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000312 return S;
313 }
314
315 checkDataType(S, File);
316
317 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000318 Replace();
Sam Cleggc94d3932017-11-17 18:14:09 +0000319 return S;
320}
321
Sam Clegg93102972018-02-23 05:08:53 +0000322Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000323 InputFile *File, InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000324 LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n");
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000325
Sam Clegg93102972018-02-23 05:08:53 +0000326 Symbol *S;
327 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000328 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000329
Sam Clegg6540e572019-02-20 23:19:31 +0000330 auto Replace = [&]() {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000331 replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
Sam Clegg6540e572019-02-20 23:19:31 +0000332 };
333
334 if (WasInserted || S->isLazy()) {
335 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000336 return S;
337 }
338
339 checkGlobalType(S, File, &Global->getType());
340
341 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000342 Replace();
Sam Clegg93102972018-02-23 05:08:53 +0000343 return S;
344}
345
Heejin Ahne915a712018-12-08 06:17:43 +0000346Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags,
347 InputFile *File, InputEvent *Event) {
348 LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n");
349
350 Symbol *S;
351 bool WasInserted;
352 std::tie(S, WasInserted) = insert(Name, File);
353
Sam Clegg6540e572019-02-20 23:19:31 +0000354 auto Replace = [&]() {
Heejin Ahne915a712018-12-08 06:17:43 +0000355 replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event);
Sam Clegg6540e572019-02-20 23:19:31 +0000356 };
357
358 if (WasInserted || S->isLazy()) {
359 Replace();
Heejin Ahne915a712018-12-08 06:17:43 +0000360 return S;
361 }
362
363 checkEventType(S, File, &Event->getType(), &Event->Signature);
364
365 if (shouldReplace(S, File, Flags))
Sam Clegg6540e572019-02-20 23:19:31 +0000366 Replace();
Heejin Ahne915a712018-12-08 06:17:43 +0000367 return S;
368}
369
Dan Gohman9b84eea2019-02-07 22:00:48 +0000370Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
371 StringRef ImportModule,
Sam Clegg7cc07532019-02-01 02:29:57 +0000372 uint32_t Flags, InputFile *File,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000373 const WasmSignature *Sig) {
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000374 LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name <<
375 " [" << (Sig ? toString(*Sig) : "none") << "]\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000376
Sam Cleggc94d3932017-11-17 18:14:09 +0000377 Symbol *S;
378 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000379 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000380
Sam Clegg6540e572019-02-20 23:19:31 +0000381 auto Replace = [&]() {
Dan Gohman9b84eea2019-02-07 22:00:48 +0000382 replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags,
383 File, Sig);
Sam Clegg6540e572019-02-20 23:19:31 +0000384 };
385
386 if (WasInserted)
387 Replace();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000388 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000389 Lazy->fetch();
Sam Clegg6540e572019-02-20 23:19:31 +0000390 else {
391 auto ExistingFunction = dyn_cast<FunctionSymbol>(S);
392 if (!ExistingFunction) {
393 reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION);
394 return S;
395 }
396 if (!signatureMatches(ExistingFunction, Sig))
397 if (getFunctionVariant(S, Sig, File, &S))
398 Replace();
399 }
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000400
Sam Cleggc94d3932017-11-17 18:14:09 +0000401 return S;
402}
403
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000404Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags,
405 InputFile *File) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000406 LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000407
408 Symbol *S;
409 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000410 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggf989a922018-07-17 19:15:02 +0000411
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000412 if (WasInserted)
413 replaceSymbol<UndefinedData>(S, Name, Flags, File);
414 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000415 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000416 else if (S->isDefined())
417 checkDataType(S, File);
418 return S;
419}
420
Dan Gohman9b84eea2019-02-07 22:00:48 +0000421Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName,
422 StringRef ImportModule, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000423 InputFile *File,
424 const WasmGlobalType *Type) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000425 LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000426
427 Symbol *S;
428 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000429 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000430
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000431 if (WasInserted)
Dan Gohman9b84eea2019-02-07 22:00:48 +0000432 replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags,
433 File, Type);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000434 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000435 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000436 else if (S->isDefined())
437 checkGlobalType(S, File, Type);
438 return S;
439}
440
441void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000442 LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +0000443 StringRef Name = Sym->getName();
Rui Ueyamac03c9042018-02-20 21:08:47 +0000444
Sam Cleggc94d3932017-11-17 18:14:09 +0000445 Symbol *S;
446 bool WasInserted;
Sam Cleggd15a41542019-03-08 21:10:48 +0000447 std::tie(S, WasInserted) = insertName(Name);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000448
Sam Cleggc94d3932017-11-17 18:14:09 +0000449 if (WasInserted) {
Sam Clegg37b4ee52019-01-29 22:26:31 +0000450 replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000451 return;
452 }
453
Sam Clegg37b4ee52019-01-29 22:26:31 +0000454 if (!S->isUndefined())
455 return;
456
457 // The existing symbol is undefined, load a new one from the archive,
458 // unless the the existing symbol is weak in which case replace the undefined
459 // symbols with a LazySymbol.
460 if (S->isWeak()) {
461 const WasmSignature *OldSig = nullptr;
462 // In the case of an UndefinedFunction we need to preserve the expected
463 // signature.
464 if (auto *F = dyn_cast<UndefinedFunction>(S))
465 OldSig = F->Signature;
466 LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
467 auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK,
468 File, *Sym);
469 NewSym->Signature = OldSig;
470 return;
Sam Cleggc94d3932017-11-17 18:14:09 +0000471 }
Sam Clegg37b4ee52019-01-29 22:26:31 +0000472
473 LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
474 File->addMember(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000475}
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000476
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000477bool SymbolTable::addComdat(StringRef Name) {
478 return Comdats.insert(CachedHashStringRef(Name)).second;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000479}
Sam Clegg1f3f7742019-02-06 02:35:18 +0000480
Sam Clegg6540e572019-02-20 23:19:31 +0000481// The new signature doesn't match. Create a variant to the symbol with the
482// signature encoded in the name and return that instead. These symbols are
483// then unified later in handleSymbolVariants.
484bool SymbolTable::getFunctionVariant(Symbol* Sym, const WasmSignature *Sig,
485 const InputFile *File, Symbol **Out) {
Richard Trieu14bab092019-02-21 00:36:14 +0000486 LLVM_DEBUG(dbgs() << "getFunctionVariant: " << Sym->getName() << " -> "
Sam Clegg6540e572019-02-20 23:19:31 +0000487 << " " << toString(*Sig) << "\n");
488 Symbol *Variant = nullptr;
489
490 // Linear search through symbol variants. Should never be more than two
491 // or three entries here.
492 auto &Variants = SymVariants[CachedHashStringRef(Sym->getName())];
493 if (Variants.size() == 0)
494 Variants.push_back(Sym);
495
496 for (Symbol* V : Variants) {
497 if (*V->getSignature() == *Sig) {
498 Variant = V;
499 break;
500 }
501 }
502
503 bool WasAdded = !Variant;
504 if (WasAdded) {
505 // Create a new variant;
506 LLVM_DEBUG(dbgs() << "added new variant\n");
507 Variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
508 Variants.push_back(Variant);
509 } else {
510 LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*Variant) << "\n");
511 assert(*Variant->getSignature() == *Sig);
512 }
513
514 *Out = Variant;
515 return WasAdded;
516}
517
Sam Clegg1f3f7742019-02-06 02:35:18 +0000518// Set a flag for --trace-symbol so that we can print out a log message
519// if a new symbol with the same name is inserted into the symbol table.
520void SymbolTable::trace(StringRef Name) {
521 SymMap.insert({CachedHashStringRef(Name), -1});
522}
Sam Clegg230dc112019-02-07 22:42:16 +0000523
524static const uint8_t UnreachableFn[] = {
525 0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
526 0x00 /* opcode unreachable */, 0x0b /* opcode end */
527};
528
529// Replace the given symbol body with an unreachable function.
530// This is used by handleWeakUndefines in order to generate a callable
Sam Clegg6540e572019-02-20 23:19:31 +0000531// equivalent of an undefined function and also handleSymbolVariants for
532// undefined functions that don't match the signature of the definition.
Sam Clegg230dc112019-02-07 22:42:16 +0000533InputFunction *SymbolTable::replaceWithUnreachable(Symbol *Sym,
534 const WasmSignature &Sig,
535 StringRef DebugName) {
536 auto *Func = make<SyntheticFunction>(Sig, Sym->getName(), DebugName);
537 Func->setBody(UnreachableFn);
538 SyntheticFunctions.emplace_back(Func);
Sam Clegg6540e572019-02-20 23:19:31 +0000539 replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr,
540 Func);
Sam Clegg230dc112019-02-07 22:42:16 +0000541 return Func;
542}
543
544// For weak undefined functions, there may be "call" instructions that reference
545// the symbol. In this case, we need to synthesise a dummy/stub function that
546// will abort at runtime, so that relocations can still provided an operand to
547// the call instruction that passes Wasm validation.
548void SymbolTable::handleWeakUndefines() {
549 for (Symbol *Sym : getSymbols()) {
550 if (!Sym->isUndefWeak())
551 continue;
552
Sam Clegg6540e572019-02-20 23:19:31 +0000553 const WasmSignature *Sig = Sym->getSignature();
554 if (!Sig) {
Sam Clegg230dc112019-02-07 22:42:16 +0000555 // It is possible for undefined functions not to have a signature (eg. if
556 // added via "--undefined"), but weak undefined ones do have a signature.
Sam Clegg6540e572019-02-20 23:19:31 +0000557 // Lazy symbols may not be functions and therefore Sig can still be null
558 // in some circumstantce.
559 assert(!isa<FunctionSymbol>(Sym));
Sam Clegg230dc112019-02-07 22:42:16 +0000560 continue;
Sam Clegg6540e572019-02-20 23:19:31 +0000561 }
Sam Clegg230dc112019-02-07 22:42:16 +0000562
563 // Add a synthetic dummy for weak undefined functions. These dummies will
564 // be GC'd if not used as the target of any "call" instructions.
565 StringRef DebugName = Saver.save("undefined:" + toString(*Sym));
566 InputFunction* Func = replaceWithUnreachable(Sym, *Sig, DebugName);
567 // Ensure it compares equal to the null pointer, and so that table relocs
568 // don't pull in the stub body (only call-operand relocs should do that).
569 Func->setTableIndex(0);
570 // Hide our dummy to prevent export.
571 Sym->setHidden(true);
572 }
573}
Sam Clegg6540e572019-02-20 23:19:31 +0000574
575static void reportFunctionSignatureMismatch(StringRef SymName,
576 FunctionSymbol *A,
577 FunctionSymbol *B, bool Error) {
578 std::string msg = ("function signature mismatch: " + SymName +
579 "\n>>> defined as " + toString(*A->Signature) + " in " +
580 toString(A->getFile()) + "\n>>> defined as " +
581 toString(*B->Signature) + " in " + toString(B->getFile()))
582 .str();
583 if (Error)
584 error(msg);
585 else
586 warn(msg);
587}
588
589// Remove any variant symbols that were created due to function signature
590// mismatches.
591void SymbolTable::handleSymbolVariants() {
592 for (auto Pair : SymVariants) {
593 // Push the initial symbol onto the list of variants.
594 StringRef SymName = Pair.first.val();
595 std::vector<Symbol *> &Variants = Pair.second;
596
597#ifndef NDEBUG
Sam Cleggf8d736f2019-02-21 01:33:26 +0000598 LLVM_DEBUG(dbgs() << "symbol with (" << Variants.size()
599 << ") variants: " << SymName << "\n");
Sam Clegg6540e572019-02-20 23:19:31 +0000600 for (auto *S: Variants) {
601 auto *F = cast<FunctionSymbol>(S);
Sam Cleggf8d736f2019-02-21 01:33:26 +0000602 LLVM_DEBUG(dbgs() << " variant: " + F->getName() << " "
603 << toString(*F->Signature) << "\n");
Sam Clegg6540e572019-02-20 23:19:31 +0000604 }
605#endif
606
607 // Find the one definition.
608 DefinedFunction *Defined = nullptr;
609 for (auto *Symbol : Variants) {
610 if (auto F = dyn_cast<DefinedFunction>(Symbol)) {
611 Defined = F;
612 break;
613 }
614 }
615
616 // If there are no definitions, and the undefined symbols disagree on
617 // the signature, there is not we can do since we don't know which one
618 // to use as the signature on the import.
619 if (!Defined) {
620 reportFunctionSignatureMismatch(SymName,
621 cast<FunctionSymbol>(Variants[0]),
622 cast<FunctionSymbol>(Variants[1]), true);
623 return;
624 }
625
626 for (auto *Symbol : Variants) {
627 if (Symbol != Defined) {
628 auto *F = cast<FunctionSymbol>(Symbol);
629 reportFunctionSignatureMismatch(SymName, F, Defined, false);
630 StringRef DebugName = Saver.save("unreachable:" + toString(*F));
631 replaceWithUnreachable(F, *F->Signature, DebugName);
632 }
633 }
634 }
635}