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