blob: 2c1554f355290cd8379e960afb5e2ce7048df628 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// Symbol table is a bag of all known symbols. We put all symbols of
Rui Ueyamac9559d92016-01-05 20:47:37 +000011// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000012// a hash table with the logic to resolve symbol name conflicts using
13// the symbol types.
14//
15//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000018#include "Config.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000019#include "Error.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "Symbols.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000021#include "llvm/Bitcode/ReaderWriter.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000022#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
24using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000025using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000026using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
28using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000029using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
Rui Ueyamac9559d92016-01-05 20:47:37 +000031// All input object files must be for the same architecture
32// (e.g. it does not make sense to link x86 object files with
33// MIPS object files.) This function checks for that error.
Rui Ueyama16ba6692016-01-29 19:41:13 +000034template <class ELFT> static bool isCompatible(InputFile *FileP) {
Rui Ueyama25b44c92015-12-16 23:31:22 +000035 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
36 if (!F)
Rui Ueyama16ba6692016-01-29 19:41:13 +000037 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000038 if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000039 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000040 StringRef A = F->getName();
41 StringRef B = Config->Emulation;
42 if (B.empty())
43 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000044 error(A + " is incompatible with " + B);
45 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000046}
47
George Rimar2a78fce2016-04-13 18:07:57 +000048// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
49static std::string getFilename(InputFile *F) {
50 if (!F)
51 return "(internal)";
52 if (!F->ArchiveName.empty())
53 return (F->ArchiveName + "(" + F->getName() + ")").str();
54 return F->getName();
55}
56
Rui Ueyamac9559d92016-01-05 20:47:37 +000057// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000058template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000059void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000060 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000061 if (!isCompatible<ELFT>(FileP))
62 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000063
Rui Ueyama89575742015-12-16 22:59:13 +000064 // .a file
65 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000066 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000067 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000068 return;
69 }
Rui Ueyama3d451792015-10-12 18:03:21 +000070
George Rimar2a78fce2016-04-13 18:07:57 +000071 // Lazy object file
72 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
73 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000074 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000075 return;
76 }
77
78 if (Config->Trace)
79 llvm::outs() << getFilename(FileP) << "\n";
80
Rui Ueyama89575742015-12-16 22:59:13 +000081 // .so file
82 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
83 // DSOs are uniquified not by filename but by soname.
84 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000085 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000086 return;
Rui Ueyama89575742015-12-16 22:59:13 +000087
Rafael Espindola21f7bd42015-12-23 14:35:51 +000088 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000089 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000090 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000091 }
Rui Ueyama89575742015-12-16 22:59:13 +000092
Rui Ueyamaf8baa662016-04-07 19:24:51 +000093 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000094 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
95 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000096 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000097 return;
98 }
99
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000100 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +0000101 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +0000102 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +0000103 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000104}
105
Rui Ueyama42554752016-04-23 00:26:32 +0000106// This function is where all the optimizations of link-time
107// optimization happens. When LTO is in use, some input files are
108// not in native object file format but in the LLVM bitcode format.
109// This function compiles bitcode files into a few big native files
110// using LLVM functions and replaces bitcode symbols with the results.
111// Because all bitcode files that consist of a program are passed
112// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000113template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
114 if (BitcodeFiles.empty())
115 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000116
117 // Compile bitcode files.
118 Lto.reset(new BitcodeCompiler);
119 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
120 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000121 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000122
123 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000124 for (auto &IF : IFs) {
125 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
126
127 llvm::DenseSet<StringRef> DummyGroups;
128 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000129 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000130 }
131}
132
Rafael Espindola0e604f92015-09-25 18:56:53 +0000133template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000134DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
135 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000136 return cast<DefinedRegular<ELFT>>(
137 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000138}
139
Rui Ueyamac9559d92016-01-05 20:47:37 +0000140// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000141// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000142template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000143DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
144 uint8_t Visibility) {
145 if (!find(Name))
146 return nullptr;
147 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000148}
149
Rui Ueyamadeb15402016-01-07 17:20:07 +0000150// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
151// Used to implement --wrap.
152template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000153 SymbolBody *B = find(Name);
154 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000155 return;
156 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000157 Symbol *Sym = B->symbol();
158 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
159 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
160 // We rename symbols by replacing the old symbol's SymbolBody with the new
161 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
162 // old symbol to instead refer to the new symbol.
163 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
164 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000165}
166
Rui Ueyama533336a2015-12-16 22:26:48 +0000167// Returns a file from which symbol B was created.
Rui Ueyama2a65a492016-01-05 20:01:29 +0000168// If B does not belong to any file, returns a nullptr.
Rui Ueyama209f6cb2016-04-23 01:10:13 +0000169// This function is slow, but it's okay as it is used only for error messages.
Rafael Espindola18f09502016-02-26 21:49:38 +0000170template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000171 // If this symbol has a definition, follow pointers in the symbol to its
172 // defining file.
173 if (auto *R = dyn_cast<DefinedRegular<ELFT>>(B))
174 if (auto *S = R->Section)
175 return S->getFile();
176 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(B))
177 return SS->File;
178 if (auto *BC = dyn_cast<DefinedBitcode>(B))
179 return BC->File;
180 // If not, we might be able to find it by searching symbol tables of files.
181 // This code is generally only used for undefined symbols. Note that we can't
182 // rely exclusively on a file search because we may find what was originally
183 // an undefined symbol that was later replaced with a defined symbol, and we
184 // want to return the file that defined the symbol.
Rui Ueyama533336a2015-12-16 22:26:48 +0000185 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000186 ArrayRef<SymbolBody *> Syms = F->getSymbols();
187 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
Rui Ueyama533336a2015-12-16 22:26:48 +0000188 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000189 }
Rafael Espindola18f09502016-02-26 21:49:38 +0000190 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000191 ArrayRef<Symbol *> Syms = F->getSymbols();
192 if (std::find(Syms.begin(), Syms.end(), B->symbol()) != Syms.end())
Rafael Espindola18f09502016-02-26 21:49:38 +0000193 return F.get();
194 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000195 return nullptr;
196}
197
Peter Collingbournedadcc172016-04-22 18:42:48 +0000198static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
199 if (VA == STV_DEFAULT)
200 return VB;
201 if (VB == STV_DEFAULT)
202 return VA;
203 return std::min(VA, VB);
204}
205
Rui Ueyamab4de5952016-01-08 22:01:33 +0000206// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000207template <class ELFT>
208std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000209 unsigned NumSyms = SymVector.size();
210 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
211 Symbol *Sym;
212 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000213 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000214 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000215 Sym->Visibility = STV_DEFAULT;
216 Sym->IsUsedInRegularObj = false;
217 Sym->ExportDynamic = false;
218 Sym->VersionScriptGlobal = !Config->VersionScript;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000219 SymVector.push_back(Sym);
220 } else {
221 Sym = SymVector[P.first->second];
222 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000223 return {Sym, P.second};
224}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000225
Peter Collingbourne4f952702016-05-01 04:55:03 +0000226// Find an existing symbol or create and insert a new one, then apply the given
227// attributes.
228template <class ELFT>
229std::pair<Symbol *, bool>
230SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
231 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
232 InputFile *File) {
233 Symbol *S;
234 bool WasInserted;
235 std::tie(S, WasInserted) = insert(Name);
236
237 // Merge in the new symbol's visibility.
238 S->Visibility = getMinVisibility(S->Visibility, Visibility);
239 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
240 S->ExportDynamic = true;
241 if (IsUsedInRegularObj)
242 S->IsUsedInRegularObj = true;
243 if (!WasInserted && ((Type == STT_TLS) != S->body()->isTls()))
244 error("TLS attribute mismatch for symbol: " +
245 conflictMsg(S->body(), File));
246
247 return {S, WasInserted};
248}
249
250// Construct a string in the form of "Sym in File1 and File2".
251// Used to construct an error message.
252template <typename ELFT>
253std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
254 InputFile *NewFile) {
255 StringRef Sym = Existing->getName();
256 return demangle(Sym) + " in " + getFilename(findFile(Existing)) + " and " +
257 getFilename(NewFile);
258}
259
260template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
261 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
262 /*File*/ nullptr);
263}
264
265template <class ELFT>
266Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
267 uint8_t StOther, uint8_t Type,
268 InputFile *File) {
269 Symbol *S;
270 bool WasInserted;
271 std::tie(S, WasInserted) =
272 insert(Name, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
273 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
274 if (WasInserted) {
275 S->Binding = Binding;
276 replaceBody<Undefined>(S, Name, StOther, Type);
277 return S;
278 }
279 if (Binding != STB_WEAK &&
280 (S->body()->isShared() || S->body()->isLazy()))
281 S->Binding = Binding;
282 if (auto *L = dyn_cast<Lazy>(S->body())) {
283 // An undefined weak will not fetch archive members, but we have to remember
284 // its type. See also comment in addLazyArchive.
285 if (S->isWeak())
286 L->Type = Type;
287 else if (auto F = L->getFile())
288 addFile(std::move(F));
289 }
290 return S;
291}
292
293// We have a new defined symbol with the specified binding. Return 1 if the new
294// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
295// strong defined symbols.
296static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
297 if (WasInserted)
298 return 1;
299 SymbolBody *Body = S->body();
300 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
301 return 1;
302 if (Binding == STB_WEAK)
303 return -1;
304 if (S->isWeak())
305 return 1;
306 return 0;
307}
308
309// We have a new non-common defined symbol with the specified binding. Return 1
310// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
311// is a conflict. If the new symbol wins, also update the binding.
312static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
313 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
314 if (Cmp > 0)
315 S->Binding = Binding;
316 return Cmp;
317 }
318 if (isa<DefinedCommon>(S->body())) {
319 // Non-common symbols take precedence over common symbols.
320 if (Config->WarnCommon)
321 warning("common " + S->body()->getName() + " is overridden");
322 return 1;
323 }
324 return 0;
325}
326
327template <class ELFT>
328Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
329 uint64_t Alignment, uint8_t Binding,
330 uint8_t StOther, uint8_t Type,
331 InputFile *File) {
332 Symbol *S;
333 bool WasInserted;
334 std::tie(S, WasInserted) =
335 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
336 /*IsUsedInRegularObj*/ true, File);
337 int Cmp = compareDefined(S, WasInserted, Binding);
338 if (Cmp > 0) {
339 S->Binding = Binding;
340 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
341 } else if (Cmp == 0) {
342 auto *C = dyn_cast<DefinedCommon>(S->body());
343 if (!C) {
344 // Non-common symbols take precedence over common symbols.
345 if (Config->WarnCommon)
346 warning("common " + S->body()->getName() + " is overridden");
347 return S;
348 }
349
350 if (Config->WarnCommon)
351 warning("multiple common of " + S->body()->getName());
352
353 C->Size = std::max(C->Size, Size);
354 C->Alignment = std::max(C->Alignment, Alignment);
355 }
356 return S;
357}
358
359template <class ELFT>
360void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
361 InputFile *NewFile) {
362 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
363 if (Config->AllowMultipleDefinition)
364 warning(Msg);
365 else
366 error(Msg);
367}
368
369template <typename ELFT>
370Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
371 InputSectionBase<ELFT> *Section) {
372 Symbol *S;
373 bool WasInserted;
374 std::tie(S, WasInserted) =
375 insert(Name, Sym.getType(), Sym.getVisibility(),
376 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
377 Section ? Section->getFile() : nullptr);
378 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
379 if (Cmp > 0)
380 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
381 else if (Cmp == 0)
382 reportDuplicate(S->body(), Section->getFile());
383 return S;
384}
385
386template <typename ELFT>
387Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
388 uint8_t StOther) {
389 Symbol *S;
390 bool WasInserted;
391 std::tie(S, WasInserted) =
392 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
393 /*IsUsedInRegularObj*/ true, nullptr);
394 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
395 if (Cmp > 0)
396 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
397 else if (Cmp == 0)
398 reportDuplicate(S->body(), nullptr);
399 return S;
400}
401
402template <typename ELFT>
403Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
404 OutputSectionBase<ELFT> &Section,
405 uintX_t Value) {
406 Symbol *S;
407 bool WasInserted;
408 std::tie(S, WasInserted) =
409 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
410 /*IsUsedInRegularObj*/ true, nullptr);
411 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
412 if (Cmp > 0)
413 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
414 else if (Cmp == 0)
415 reportDuplicate(S->body(), nullptr);
416 return S;
417}
418
419template <typename ELFT>
420void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
421 const Elf_Sym &Sym,
422 const typename ELFT::Verdef *Verdef) {
423 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
424 // as the visibility, which will leave the visibility in the symbol table
425 // unchanged.
426 Symbol *S;
427 bool WasInserted;
428 std::tie(S, WasInserted) =
429 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
430 /*IsUsedInRegularObj*/ false, F);
431 // Make sure we preempt DSO symbols with default visibility.
432 if (Sym.getVisibility() == STV_DEFAULT)
433 S->ExportDynamic = true;
434 if (WasInserted || isa<Undefined>(S->body()))
435 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
436}
437
438template <class ELFT>
439Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
440 uint8_t StOther, uint8_t Type,
441 bool CanOmitFromDynSym, BitcodeFile *F) {
442 Symbol *S;
443 bool WasInserted;
444 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
445 /*IsUsedInRegularObj*/ false, F);
446 int Cmp =
447 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
448 if (Cmp > 0)
449 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
450 else if (Cmp == 0)
451 reportDuplicate(S->body(), F);
452 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000453}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000454
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000455template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
456 auto It = Symtab.find(Name);
457 if (It == Symtab.end())
458 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000459 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000460}
461
Rui Ueyama3d451792015-10-12 18:03:21 +0000462template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000463void SymbolTable<ELFT>::addLazyArchive(
464 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
465 Symbol *S;
466 bool WasInserted;
467 std::tie(S, WasInserted) = insert(Sym.getName());
468 if (WasInserted) {
469 replaceBody<LazyArchive>(S, F, Sym, STT_NOTYPE);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000470 return;
471 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000472 if (!S->body()->isUndefined())
473 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000474
Peter Collingbourne4f952702016-05-01 04:55:03 +0000475 // Weak undefined symbols should not fetch members from archives. If we were
476 // to keep old symbol we would not know that an archive member was available
477 // if a strong undefined symbol shows up afterwards in the link. If a strong
478 // undefined symbol never shows up, this lazy symbol will get to the end of
479 // the link and must be treated as the weak undefined one. We already marked
480 // this symbol as used when we added it to the symbol table, but we also need
481 // to preserve its type. FIXME: Move the Type field to Symbol.
482 if (S->isWeak()) {
483 replaceBody<LazyArchive>(S, F, Sym, S->body()->Type);
484 return;
485 }
486 MemoryBufferRef MBRef = F->getMember(&Sym);
487 if (!MBRef.getBuffer().empty())
488 addFile(createObjectFile(MBRef, F->getName()));
489}
490
491template <class ELFT>
492void SymbolTable<ELFT>::addLazyObject(StringRef Name, MemoryBufferRef MBRef) {
493 Symbol *S;
494 bool WasInserted;
495 std::tie(S, WasInserted) = insert(Name);
496 if (WasInserted) {
497 replaceBody<LazyObject>(S, Name, MBRef, STT_NOTYPE);
498 return;
499 }
500 if (!S->body()->isUndefined())
501 return;
502
503 // See comment for addLazyArchive above.
504 if (S->isWeak())
505 replaceBody<LazyObject>(S, Name, MBRef, S->body()->Type);
506 else
507 addFile(createObjectFile(MBRef));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000508}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000509
Peter Collingbourne892d49802016-04-27 00:05:03 +0000510// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000511template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000512 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000513 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
514 if (std::unique_ptr<InputFile> File = L->getFile())
515 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000516}
517
Rui Ueyama93bfee52015-10-13 18:10:33 +0000518// This function takes care of the case in which shared libraries depend on
519// the user program (not the other way, which is usual). Shared libraries
520// may have undefined symbols, expecting that the user program provides
521// the definitions for them. An example is BSD's __progname symbol.
522// We need to put such symbols to the main program's .dynsym so that
523// shared libraries can find them.
524// Except this, we ignore undefined symbols in DSOs.
525template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000526 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
527 for (StringRef U : File->getUndefinedSymbols())
528 if (SymbolBody *Sym = find(U))
529 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000530 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000531}
532
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000533// This function process the dynamic list option by marking all the symbols
534// to be exported in the dynamic table.
535template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
536 for (StringRef S : Config->DynamicList)
537 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000538 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000539}
540
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000541// This function processes the --version-script option by marking all global
542// symbols with the VersionScriptGlobal flag, which acts as a filter on the
543// dynamic symbol table.
544template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
545 for (StringRef S : Config->VersionScriptGlobals)
546 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000547 B->symbol()->VersionScriptGlobal = true;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000548}
549
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000550template class elf::SymbolTable<ELF32LE>;
551template class elf::SymbolTable<ELF32BE>;
552template class elf::SymbolTable<ELF64LE>;
553template class elf::SymbolTable<ELF64BE>;