blob: 53a2929563cf0673282806a28fd33e4c62e67135 [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.
Rafael Espindola18f09502016-02-26 21:49:38 +0000169template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000170 // If this symbol has a definition, follow pointers in the symbol to its
171 // defining file.
172 if (auto *R = dyn_cast<DefinedRegular<ELFT>>(B))
173 if (auto *S = R->Section)
174 return S->getFile();
175 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(B))
176 return SS->File;
177 if (auto *BC = dyn_cast<DefinedBitcode>(B))
178 return BC->File;
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000179 if (auto *U = dyn_cast<Undefined>(B))
180 return U->File;
Rui Ueyama533336a2015-12-16 22:26:48 +0000181 return nullptr;
182}
183
Peter Collingbournedadcc172016-04-22 18:42:48 +0000184static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
185 if (VA == STV_DEFAULT)
186 return VB;
187 if (VB == STV_DEFAULT)
188 return VA;
189 return std::min(VA, VB);
190}
191
Rui Ueyamab4de5952016-01-08 22:01:33 +0000192// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000193template <class ELFT>
194std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000195 unsigned NumSyms = SymVector.size();
196 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
197 Symbol *Sym;
198 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000199 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000200 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000201 Sym->Visibility = STV_DEFAULT;
202 Sym->IsUsedInRegularObj = false;
203 Sym->ExportDynamic = false;
204 Sym->VersionScriptGlobal = !Config->VersionScript;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000205 SymVector.push_back(Sym);
206 } else {
207 Sym = SymVector[P.first->second];
208 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000209 return {Sym, P.second};
210}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000211
Peter Collingbourne4f952702016-05-01 04:55:03 +0000212// Find an existing symbol or create and insert a new one, then apply the given
213// attributes.
214template <class ELFT>
215std::pair<Symbol *, bool>
216SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
217 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
218 InputFile *File) {
219 Symbol *S;
220 bool WasInserted;
221 std::tie(S, WasInserted) = insert(Name);
222
223 // Merge in the new symbol's visibility.
224 S->Visibility = getMinVisibility(S->Visibility, Visibility);
225 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
226 S->ExportDynamic = true;
227 if (IsUsedInRegularObj)
228 S->IsUsedInRegularObj = true;
229 if (!WasInserted && ((Type == STT_TLS) != S->body()->isTls()))
230 error("TLS attribute mismatch for symbol: " +
231 conflictMsg(S->body(), File));
232
233 return {S, WasInserted};
234}
235
236// Construct a string in the form of "Sym in File1 and File2".
237// Used to construct an error message.
238template <typename ELFT>
239std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
240 InputFile *NewFile) {
241 StringRef Sym = Existing->getName();
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000242 return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) +
243 " and " + getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000244}
245
246template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
247 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
248 /*File*/ nullptr);
249}
250
251template <class ELFT>
252Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
253 uint8_t StOther, uint8_t Type,
254 InputFile *File) {
255 Symbol *S;
256 bool WasInserted;
257 std::tie(S, WasInserted) =
258 insert(Name, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
259 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
260 if (WasInserted) {
261 S->Binding = Binding;
262 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000263 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000264 return S;
265 }
266 if (Binding != STB_WEAK &&
267 (S->body()->isShared() || S->body()->isLazy()))
268 S->Binding = Binding;
269 if (auto *L = dyn_cast<Lazy>(S->body())) {
270 // An undefined weak will not fetch archive members, but we have to remember
271 // its type. See also comment in addLazyArchive.
272 if (S->isWeak())
273 L->Type = Type;
274 else if (auto F = L->getFile())
275 addFile(std::move(F));
276 }
277 return S;
278}
279
280// We have a new defined symbol with the specified binding. Return 1 if the new
281// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
282// strong defined symbols.
283static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
284 if (WasInserted)
285 return 1;
286 SymbolBody *Body = S->body();
287 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
288 return 1;
289 if (Binding == STB_WEAK)
290 return -1;
291 if (S->isWeak())
292 return 1;
293 return 0;
294}
295
296// We have a new non-common defined symbol with the specified binding. Return 1
297// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
298// is a conflict. If the new symbol wins, also update the binding.
299static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
300 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
301 if (Cmp > 0)
302 S->Binding = Binding;
303 return Cmp;
304 }
305 if (isa<DefinedCommon>(S->body())) {
306 // Non-common symbols take precedence over common symbols.
307 if (Config->WarnCommon)
308 warning("common " + S->body()->getName() + " is overridden");
309 return 1;
310 }
311 return 0;
312}
313
314template <class ELFT>
315Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
316 uint64_t Alignment, uint8_t Binding,
317 uint8_t StOther, uint8_t Type,
318 InputFile *File) {
319 Symbol *S;
320 bool WasInserted;
321 std::tie(S, WasInserted) =
322 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
323 /*IsUsedInRegularObj*/ true, File);
324 int Cmp = compareDefined(S, WasInserted, Binding);
325 if (Cmp > 0) {
326 S->Binding = Binding;
327 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
328 } else if (Cmp == 0) {
329 auto *C = dyn_cast<DefinedCommon>(S->body());
330 if (!C) {
331 // Non-common symbols take precedence over common symbols.
332 if (Config->WarnCommon)
333 warning("common " + S->body()->getName() + " is overridden");
334 return S;
335 }
336
337 if (Config->WarnCommon)
338 warning("multiple common of " + S->body()->getName());
339
340 C->Size = std::max(C->Size, Size);
341 C->Alignment = std::max(C->Alignment, Alignment);
342 }
343 return S;
344}
345
346template <class ELFT>
347void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
348 InputFile *NewFile) {
349 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
350 if (Config->AllowMultipleDefinition)
351 warning(Msg);
352 else
353 error(Msg);
354}
355
356template <typename ELFT>
357Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
358 InputSectionBase<ELFT> *Section) {
359 Symbol *S;
360 bool WasInserted;
361 std::tie(S, WasInserted) =
362 insert(Name, Sym.getType(), Sym.getVisibility(),
363 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
364 Section ? Section->getFile() : nullptr);
365 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
366 if (Cmp > 0)
367 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
368 else if (Cmp == 0)
369 reportDuplicate(S->body(), Section->getFile());
370 return S;
371}
372
373template <typename ELFT>
374Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
375 uint8_t StOther) {
376 Symbol *S;
377 bool WasInserted;
378 std::tie(S, WasInserted) =
379 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
380 /*IsUsedInRegularObj*/ true, nullptr);
381 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
382 if (Cmp > 0)
383 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
384 else if (Cmp == 0)
385 reportDuplicate(S->body(), nullptr);
386 return S;
387}
388
389template <typename ELFT>
390Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
391 OutputSectionBase<ELFT> &Section,
392 uintX_t Value) {
393 Symbol *S;
394 bool WasInserted;
395 std::tie(S, WasInserted) =
396 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
397 /*IsUsedInRegularObj*/ true, nullptr);
398 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
399 if (Cmp > 0)
400 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
401 else if (Cmp == 0)
402 reportDuplicate(S->body(), nullptr);
403 return S;
404}
405
406template <typename ELFT>
407void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
408 const Elf_Sym &Sym,
409 const typename ELFT::Verdef *Verdef) {
410 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
411 // as the visibility, which will leave the visibility in the symbol table
412 // unchanged.
413 Symbol *S;
414 bool WasInserted;
415 std::tie(S, WasInserted) =
416 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
417 /*IsUsedInRegularObj*/ false, F);
418 // Make sure we preempt DSO symbols with default visibility.
419 if (Sym.getVisibility() == STV_DEFAULT)
420 S->ExportDynamic = true;
421 if (WasInserted || isa<Undefined>(S->body()))
422 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
423}
424
425template <class ELFT>
426Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
427 uint8_t StOther, uint8_t Type,
428 bool CanOmitFromDynSym, BitcodeFile *F) {
429 Symbol *S;
430 bool WasInserted;
431 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
432 /*IsUsedInRegularObj*/ false, F);
433 int Cmp =
434 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
435 if (Cmp > 0)
436 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
437 else if (Cmp == 0)
438 reportDuplicate(S->body(), F);
439 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000440}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000441
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000442template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
443 auto It = Symtab.find(Name);
444 if (It == Symtab.end())
445 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000446 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000447}
448
Rui Ueyama3d451792015-10-12 18:03:21 +0000449template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000450void SymbolTable<ELFT>::addLazyArchive(
451 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
452 Symbol *S;
453 bool WasInserted;
454 std::tie(S, WasInserted) = insert(Sym.getName());
455 if (WasInserted) {
456 replaceBody<LazyArchive>(S, F, Sym, STT_NOTYPE);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000457 return;
458 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000459 if (!S->body()->isUndefined())
460 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000461
Peter Collingbourne4f952702016-05-01 04:55:03 +0000462 // Weak undefined symbols should not fetch members from archives. If we were
463 // to keep old symbol we would not know that an archive member was available
464 // if a strong undefined symbol shows up afterwards in the link. If a strong
465 // undefined symbol never shows up, this lazy symbol will get to the end of
466 // the link and must be treated as the weak undefined one. We already marked
467 // this symbol as used when we added it to the symbol table, but we also need
468 // to preserve its type. FIXME: Move the Type field to Symbol.
469 if (S->isWeak()) {
470 replaceBody<LazyArchive>(S, F, Sym, S->body()->Type);
471 return;
472 }
473 MemoryBufferRef MBRef = F->getMember(&Sym);
474 if (!MBRef.getBuffer().empty())
475 addFile(createObjectFile(MBRef, F->getName()));
476}
477
478template <class ELFT>
479void SymbolTable<ELFT>::addLazyObject(StringRef Name, MemoryBufferRef MBRef) {
480 Symbol *S;
481 bool WasInserted;
482 std::tie(S, WasInserted) = insert(Name);
483 if (WasInserted) {
484 replaceBody<LazyObject>(S, Name, MBRef, STT_NOTYPE);
485 return;
486 }
487 if (!S->body()->isUndefined())
488 return;
489
490 // See comment for addLazyArchive above.
491 if (S->isWeak())
492 replaceBody<LazyObject>(S, Name, MBRef, S->body()->Type);
493 else
494 addFile(createObjectFile(MBRef));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000495}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000496
Peter Collingbourne892d49802016-04-27 00:05:03 +0000497// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000498template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000499 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000500 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
501 if (std::unique_ptr<InputFile> File = L->getFile())
502 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000503}
504
Rui Ueyama93bfee52015-10-13 18:10:33 +0000505// This function takes care of the case in which shared libraries depend on
506// the user program (not the other way, which is usual). Shared libraries
507// may have undefined symbols, expecting that the user program provides
508// the definitions for them. An example is BSD's __progname symbol.
509// We need to put such symbols to the main program's .dynsym so that
510// shared libraries can find them.
511// Except this, we ignore undefined symbols in DSOs.
512template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000513 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
514 for (StringRef U : File->getUndefinedSymbols())
515 if (SymbolBody *Sym = find(U))
516 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000517 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000518}
519
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000520// This function process the dynamic list option by marking all the symbols
521// to be exported in the dynamic table.
522template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
523 for (StringRef S : Config->DynamicList)
524 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000525 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000526}
527
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000528// This function processes the --version-script option by marking all global
529// symbols with the VersionScriptGlobal flag, which acts as a filter on the
530// dynamic symbol table.
531template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
532 for (StringRef S : Config->VersionScriptGlobals)
533 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000534 B->symbol()->VersionScriptGlobal = true;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000535}
536
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000537template class elf::SymbolTable<ELF32LE>;
538template class elf::SymbolTable<ELF32BE>;
539template class elf::SymbolTable<ELF64LE>;
540template class elf::SymbolTable<ELF64BE>;