blob: 00ee88f662e9901c3952555e7ab0186c58928373 [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);
40}
41
Sam Cleggc729c1b2018-05-30 18:07:52 +000042// This function is where all the optimizations of link-time
43// optimization happens. When LTO is in use, some input files are
44// not in native object file format but in the LLVM bitcode format.
45// This function compiles bitcode files into a few big native files
46// using LLVM functions and replaces bitcode symbols with the results.
47// Because all bitcode files that the program consists of are passed
48// to the compiler at once, it can do whole-program optimization.
49void SymbolTable::addCombinedLTOObject() {
50 if (BitcodeFiles.empty())
51 return;
52
53 // Compile bitcode files and replace bitcode symbols.
54 LTO.reset(new BitcodeCompiler);
55 for (BitcodeFile *F : BitcodeFiles)
56 LTO->add(*F);
57
58 for (StringRef Filename : LTO->compile()) {
59 auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp"));
60 Obj->parse();
61 ObjectFiles.push_back(Obj);
62 }
63}
64
Sam Cleggc94d3932017-11-17 18:14:09 +000065void SymbolTable::reportRemainingUndefines() {
Sam Clegg74fe0ba2017-12-07 01:51:24 +000066 for (Symbol *Sym : SymVector) {
Sam Cleggc729c1b2018-05-30 18:07:52 +000067 if (!Sym->isUndefined() || Sym->isWeak())
68 continue;
69 if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0)
70 continue;
71 if (!Sym->IsUsedInRegularObj)
72 continue;
Sam Clegg47e2b6b2018-08-04 00:04:06 +000073 error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym));
Sam Cleggc94d3932017-11-17 18:14:09 +000074 }
Sam Cleggc94d3932017-11-17 18:14:09 +000075}
76
77Symbol *SymbolTable::find(StringRef Name) {
Sam Clegg1f3f7742019-02-06 02:35:18 +000078 auto It = SymMap.find(CachedHashStringRef(Name));
79 if (It == SymMap.end() || It->second == -1)
80 return nullptr;
81 return SymVector[It->second];
82}
83
84std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) {
85 bool Trace = false;
86 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
87 int &SymIndex = P.first->second;
88 bool IsNew = P.second;
89 if (SymIndex == -1) {
90 SymIndex = SymVector.size();
91 Trace = true;
92 IsNew = true;
93 }
94
95 if (!IsNew)
96 return {SymVector[SymIndex], false};
97
98 Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
99 Sym->IsUsedInRegularObj = false;
100 Sym->Traced = Trace;
101 SymVector.emplace_back(Sym);
102 return {Sym, true};
Sam Cleggc94d3932017-11-17 18:14:09 +0000103}
104
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000105std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
Sam Clegg1f3f7742019-02-06 02:35:18 +0000106 Symbol *S;
107 bool WasInserted;
108 std::tie(S, WasInserted) = insertName(Name);
109
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000110 if (!File || File->kind() == InputFile::ObjectKind)
Sam Clegg1f3f7742019-02-06 02:35:18 +0000111 S->IsUsedInRegularObj = true;
112
113 return {S, WasInserted};
Sam Cleggc94d3932017-11-17 18:14:09 +0000114}
115
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000116static void reportTypeError(const Symbol *Existing, const InputFile *File,
Sam Clegg3876d89a2018-05-14 22:42:33 +0000117 llvm::wasm::WasmSymbolType Type) {
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000118 error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " +
119 toString(Existing->getWasmType()) + " in " +
Sam Clegg3876d89a2018-05-14 22:42:33 +0000120 toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) +
121 " in " + toString(File));
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000122}
123
Heejin Ahn6f4286f2018-11-19 23:31:28 +0000124// Check the type of new symbol matches that of the symbol is replacing.
125// For functions this can also involve verifying that the signatures match.
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000126static void checkFunctionType(Symbol *Existing, const InputFile *File,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000127 const WasmSignature *NewSig) {
Sam Clegg65d63802018-05-14 23:01:16 +0000128 auto ExistingFunction = dyn_cast<FunctionSymbol>(Existing);
129 if (!ExistingFunction) {
Sam Clegg3876d89a2018-05-14 22:42:33 +0000130 reportTypeError(Existing, File, WASM_SYMBOL_TYPE_FUNCTION);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000131 return;
132 }
133
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000134 if (!NewSig)
135 return;
136
Heejin Ahne915a712018-12-08 06:17:43 +0000137 const WasmSignature *OldSig = ExistingFunction->Signature;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000138 if (!OldSig) {
Heejin Ahne915a712018-12-08 06:17:43 +0000139 ExistingFunction->Signature = NewSig;
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000140 return;
141 }
142
143 if (*NewSig != *OldSig)
Sam Cleggffd0aaf2018-06-22 15:13:10 +0000144 warn("function signature mismatch: " + Existing->getName() +
145 "\n>>> defined as " + toString(*OldSig) + " in " +
146 toString(Existing->getFile()) + "\n>>> defined as " +
147 toString(*NewSig) + " in " + toString(File));
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 Clegg4c2cbfe2018-08-02 20:39:19 +0000198 return replaceSymbol<DefinedFunction>(insert(Name, nullptr).first, Name,
199 Flags, nullptr, Function);
Sam Clegg50686852018-01-12 18:35:13 +0000200}
201
Sam Clegg00245532018-02-20 23:38:27 +0000202DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name,
203 uint32_t Flags) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000204 LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000205 assert(!find(Name));
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000206 return replaceSymbol<DefinedData>(insert(Name, nullptr).first, Name, Flags);
Sam Cleggc94d3932017-11-17 18:14:09 +0000207}
208
Sam Clegg93102972018-02-23 05:08:53 +0000209DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags,
210 InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000211 LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global
212 << "\n");
Rui Ueyamab961abc2018-02-28 22:51:51 +0000213 assert(!find(Name));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000214 SyntheticGlobals.emplace_back(Global);
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000215 return replaceSymbol<DefinedGlobal>(insert(Name, nullptr).first, Name, Flags,
216 nullptr, Global);
Sam Clegg93102972018-02-23 05:08:53 +0000217}
218
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000219static bool shouldReplace(const Symbol *Existing, InputFile *NewFile,
220 uint32_t NewFlags) {
Rui Ueyamac03c9042018-02-20 21:08:47 +0000221 // If existing symbol is undefined, replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000222 if (!Existing->isDefined()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000223 LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
224 << Existing->getName() << "\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000225 return true;
226 }
227
228 // Now we have two defined symbols. If the new one is weak, we can ignore it.
229 if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000230 LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000231 return false;
232 }
233
234 // If the existing symbol is weak, we should replace it.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000235 if (Existing->isWeak()) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000236 LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000237 return true;
238 }
239
240 // Neither symbol is week. They conflict.
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000241 error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
242 toString(Existing->getFile()) + "\n>>> defined in " +
243 toString(NewFile));
Rui Ueyamac03c9042018-02-20 21:08:47 +0000244 return true;
Sam Clegg93e559b2018-02-20 18:55:06 +0000245}
246
247Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000248 InputFile *File,
249 InputFunction *Function) {
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000250 LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " ["
251 << (Function ? toString(Function->Signature) : "none")
252 << "]\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000253 Symbol *S;
254 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000255 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000256
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000257 if (WasInserted || S->isLazy()) {
258 replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function);
259 return S;
260 }
261
Sam Cleggc729c1b2018-05-30 18:07:52 +0000262 if (Function)
263 checkFunctionType(S, File, &Function->Signature);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000264
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000265 if (shouldReplace(S, File, Flags)) {
266 // If the new defined function doesn't have signture (i.e. bitcode
Sam Clegg37b4ee52019-01-29 22:26:31 +0000267 // functions) but the old symbol does then preserve the old signature
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000268 const WasmSignature *OldSig = nullptr;
269 if (auto* F = dyn_cast<FunctionSymbol>(S))
Heejin Ahne915a712018-12-08 06:17:43 +0000270 OldSig = F->Signature;
Sam Clegg37b4ee52019-01-29 22:26:31 +0000271 if (auto *L = dyn_cast<LazySymbol>(S))
272 OldSig = L->Signature;
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000273 auto NewSym = replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function);
Heejin Ahne915a712018-12-08 06:17:43 +0000274 if (!NewSym->Signature)
275 NewSym->Signature = OldSig;
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000276 }
Sam Clegg93e559b2018-02-20 18:55:06 +0000277 return S;
278}
279
Sam Clegg00245532018-02-20 23:38:27 +0000280Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000281 InputFile *File, InputSegment *Segment,
Sam Clegg93102972018-02-23 05:08:53 +0000282 uint32_t Address, uint32_t Size) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000283 LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address
284 << "\n");
Sam Clegg93e559b2018-02-20 18:55:06 +0000285 Symbol *S;
286 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000287 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000288
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000289 if (WasInserted || S->isLazy()) {
290 replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
291 return S;
292 }
293
294 checkDataType(S, File);
295
296 if (shouldReplace(S, File, Flags))
297 replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
Sam Cleggc94d3932017-11-17 18:14:09 +0000298 return S;
299}
300
Sam Clegg93102972018-02-23 05:08:53 +0000301Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000302 InputFile *File, InputGlobal *Global) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000303 LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n");
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000304
Sam Clegg93102972018-02-23 05:08:53 +0000305 Symbol *S;
306 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000307 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000308
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000309 if (WasInserted || S->isLazy()) {
310 replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
311 return S;
312 }
313
314 checkGlobalType(S, File, &Global->getType());
315
316 if (shouldReplace(S, File, Flags))
317 replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
Sam Clegg93102972018-02-23 05:08:53 +0000318 return S;
319}
320
Heejin Ahne915a712018-12-08 06:17:43 +0000321Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags,
322 InputFile *File, InputEvent *Event) {
323 LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n");
324
325 Symbol *S;
326 bool WasInserted;
327 std::tie(S, WasInserted) = insert(Name, File);
328
329 if (WasInserted || S->isLazy()) {
330 replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event);
331 return S;
332 }
333
334 checkEventType(S, File, &Event->getType(), &Event->Signature);
335
336 if (shouldReplace(S, File, Flags))
337 replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event);
338 return S;
339}
340
Dan Gohman9b84eea2019-02-07 22:00:48 +0000341Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
342 StringRef ImportModule,
Sam Clegg7cc07532019-02-01 02:29:57 +0000343 uint32_t Flags, InputFile *File,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000344 const WasmSignature *Sig) {
Sam Clegg8b0b48f2018-09-28 16:50:14 +0000345 LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name <<
346 " [" << (Sig ? toString(*Sig) : "none") << "]\n");
Rui Ueyamac03c9042018-02-20 21:08:47 +0000347
Sam Cleggc94d3932017-11-17 18:14:09 +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
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000352 if (WasInserted)
Dan Gohman9b84eea2019-02-07 22:00:48 +0000353 replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags,
354 File, Sig);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000355 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000356 Lazy->fetch();
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000357 else
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000358 checkFunctionType(S, File, Sig);
Sam Cleggcefbf9a2018-06-28 16:53:53 +0000359
Sam Cleggc94d3932017-11-17 18:14:09 +0000360 return S;
361}
362
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000363Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags,
364 InputFile *File) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000365 LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000366
367 Symbol *S;
368 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000369 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggf989a922018-07-17 19:15:02 +0000370
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000371 if (WasInserted)
372 replaceSymbol<UndefinedData>(S, Name, Flags, File);
373 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000374 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000375 else if (S->isDefined())
376 checkDataType(S, File);
377 return S;
378}
379
Dan Gohman9b84eea2019-02-07 22:00:48 +0000380Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName,
381 StringRef ImportModule, uint32_t Flags,
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000382 InputFile *File,
383 const WasmGlobalType *Type) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000384 LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n");
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000385
386 Symbol *S;
387 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000388 std::tie(S, WasInserted) = insert(Name, File);
Sam Cleggc729c1b2018-05-30 18:07:52 +0000389
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000390 if (WasInserted)
Dan Gohman9b84eea2019-02-07 22:00:48 +0000391 replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags,
392 File, Type);
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000393 else if (auto *Lazy = dyn_cast<LazySymbol>(S))
Rui Ueyamab961abc2018-02-28 22:51:51 +0000394 Lazy->fetch();
Rui Ueyamae3498ec2018-02-28 00:09:22 +0000395 else if (S->isDefined())
396 checkGlobalType(S, File, Type);
397 return S;
398}
399
400void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000401 LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +0000402 StringRef Name = Sym->getName();
Rui Ueyamac03c9042018-02-20 21:08:47 +0000403
Sam Cleggc94d3932017-11-17 18:14:09 +0000404 Symbol *S;
405 bool WasInserted;
Sam Clegg4c2cbfe2018-08-02 20:39:19 +0000406 std::tie(S, WasInserted) = insert(Name, nullptr);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000407
Sam Cleggc94d3932017-11-17 18:14:09 +0000408 if (WasInserted) {
Sam Clegg37b4ee52019-01-29 22:26:31 +0000409 replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym);
Rui Ueyamac03c9042018-02-20 21:08:47 +0000410 return;
411 }
412
Sam Clegg37b4ee52019-01-29 22:26:31 +0000413 if (!S->isUndefined())
414 return;
415
416 // The existing symbol is undefined, load a new one from the archive,
417 // unless the the existing symbol is weak in which case replace the undefined
418 // symbols with a LazySymbol.
419 if (S->isWeak()) {
420 const WasmSignature *OldSig = nullptr;
421 // In the case of an UndefinedFunction we need to preserve the expected
422 // signature.
423 if (auto *F = dyn_cast<UndefinedFunction>(S))
424 OldSig = F->Signature;
425 LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
426 auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK,
427 File, *Sym);
428 NewSym->Signature = OldSig;
429 return;
Sam Cleggc94d3932017-11-17 18:14:09 +0000430 }
Sam Clegg37b4ee52019-01-29 22:26:31 +0000431
432 LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
433 File->addMember(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000434}
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000435
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000436bool SymbolTable::addComdat(StringRef Name) {
437 return Comdats.insert(CachedHashStringRef(Name)).second;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000438}
Sam Clegg1f3f7742019-02-06 02:35:18 +0000439
440// Set a flag for --trace-symbol so that we can print out a log message
441// if a new symbol with the same name is inserted into the symbol table.
442void SymbolTable::trace(StringRef Name) {
443 SymMap.insert({CachedHashStringRef(Name), -1});
444}