blob: 4117487b58d33c426c8fc4d5240a360816f62515 [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
Rui Ueyamac9559d92016-01-05 20:47:37 +000048// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000049template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000050void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000051 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000052 if (!isCompatible<ELFT>(FileP))
53 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000054
Rui Ueyama89575742015-12-16 22:59:13 +000055 // .a file
56 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000057 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000058 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000059 return;
60 }
Rui Ueyama3d451792015-10-12 18:03:21 +000061
George Rimar2a78fce2016-04-13 18:07:57 +000062 // Lazy object file
63 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
64 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000065 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000066 return;
67 }
68
69 if (Config->Trace)
70 llvm::outs() << getFilename(FileP) << "\n";
71
Rui Ueyama89575742015-12-16 22:59:13 +000072 // .so file
73 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
74 // DSOs are uniquified not by filename but by soname.
75 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000076 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000077 return;
Rui Ueyama89575742015-12-16 22:59:13 +000078
Rafael Espindola21f7bd42015-12-23 14:35:51 +000079 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000080 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000081 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000082 }
Rui Ueyama89575742015-12-16 22:59:13 +000083
Rui Ueyamaf8baa662016-04-07 19:24:51 +000084 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000085 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
86 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000087 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000088 return;
89 }
90
Rui Ueyamaf8baa662016-04-07 19:24:51 +000091 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +000092 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000093 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000094 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +000095}
96
Rui Ueyama42554752016-04-23 00:26:32 +000097// This function is where all the optimizations of link-time
98// optimization happens. When LTO is in use, some input files are
99// not in native object file format but in the LLVM bitcode format.
100// This function compiles bitcode files into a few big native files
101// using LLVM functions and replaces bitcode symbols with the results.
102// Because all bitcode files that consist of a program are passed
103// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000104template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
105 if (BitcodeFiles.empty())
106 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000107
108 // Compile bitcode files.
109 Lto.reset(new BitcodeCompiler);
110 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
111 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000112 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000113
114 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000115 for (auto &IF : IFs) {
116 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
117
118 llvm::DenseSet<StringRef> DummyGroups;
119 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000120 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000121 }
122}
123
Rafael Espindola0e604f92015-09-25 18:56:53 +0000124template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000125DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
126 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000127 return cast<DefinedRegular<ELFT>>(
128 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000129}
130
Rui Ueyamac9559d92016-01-05 20:47:37 +0000131// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000132// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000133template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000134DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
135 uint8_t Visibility) {
136 if (!find(Name))
137 return nullptr;
138 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000139}
140
Rui Ueyamadeb15402016-01-07 17:20:07 +0000141// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
142// Used to implement --wrap.
143template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000144 SymbolBody *B = find(Name);
145 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000146 return;
147 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000148 Symbol *Sym = B->symbol();
149 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
150 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
151 // We rename symbols by replacing the old symbol's SymbolBody with the new
152 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
153 // old symbol to instead refer to the new symbol.
154 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
155 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000156}
157
Peter Collingbournedadcc172016-04-22 18:42:48 +0000158static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
159 if (VA == STV_DEFAULT)
160 return VB;
161 if (VB == STV_DEFAULT)
162 return VA;
163 return std::min(VA, VB);
164}
165
Rui Ueyamab4de5952016-01-08 22:01:33 +0000166// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000167template <class ELFT>
168std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000169 unsigned NumSyms = SymVector.size();
170 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
171 Symbol *Sym;
172 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000173 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000174 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000175 Sym->Visibility = STV_DEFAULT;
176 Sym->IsUsedInRegularObj = false;
177 Sym->ExportDynamic = false;
178 Sym->VersionScriptGlobal = !Config->VersionScript;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000179 SymVector.push_back(Sym);
180 } else {
181 Sym = SymVector[P.first->second];
182 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000183 return {Sym, P.second};
184}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000185
Peter Collingbourne4f952702016-05-01 04:55:03 +0000186// Find an existing symbol or create and insert a new one, then apply the given
187// attributes.
188template <class ELFT>
189std::pair<Symbol *, bool>
190SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
191 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
192 InputFile *File) {
193 Symbol *S;
194 bool WasInserted;
195 std::tie(S, WasInserted) = insert(Name);
196
197 // Merge in the new symbol's visibility.
198 S->Visibility = getMinVisibility(S->Visibility, Visibility);
199 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
200 S->ExportDynamic = true;
201 if (IsUsedInRegularObj)
202 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000203 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
204 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000205 error("TLS attribute mismatch for symbol: " +
206 conflictMsg(S->body(), File));
207
208 return {S, WasInserted};
209}
210
211// Construct a string in the form of "Sym in File1 and File2".
212// Used to construct an error message.
213template <typename ELFT>
214std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
215 InputFile *NewFile) {
216 StringRef Sym = Existing->getName();
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000217 return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) +
218 " and " + getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000219}
220
221template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
222 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
223 /*File*/ nullptr);
224}
225
226template <class ELFT>
227Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
228 uint8_t StOther, uint8_t Type,
229 InputFile *File) {
230 Symbol *S;
231 bool WasInserted;
232 std::tie(S, WasInserted) =
233 insert(Name, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
234 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
235 if (WasInserted) {
236 S->Binding = Binding;
237 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000238 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000239 return S;
240 }
241 if (Binding != STB_WEAK &&
242 (S->body()->isShared() || S->body()->isLazy()))
243 S->Binding = Binding;
244 if (auto *L = dyn_cast<Lazy>(S->body())) {
245 // An undefined weak will not fetch archive members, but we have to remember
246 // its type. See also comment in addLazyArchive.
247 if (S->isWeak())
248 L->Type = Type;
249 else if (auto F = L->getFile())
250 addFile(std::move(F));
251 }
252 return S;
253}
254
255// We have a new defined symbol with the specified binding. Return 1 if the new
256// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
257// strong defined symbols.
258static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
259 if (WasInserted)
260 return 1;
261 SymbolBody *Body = S->body();
262 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
263 return 1;
264 if (Binding == STB_WEAK)
265 return -1;
266 if (S->isWeak())
267 return 1;
268 return 0;
269}
270
271// We have a new non-common defined symbol with the specified binding. Return 1
272// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
273// is a conflict. If the new symbol wins, also update the binding.
274static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
275 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
276 if (Cmp > 0)
277 S->Binding = Binding;
278 return Cmp;
279 }
280 if (isa<DefinedCommon>(S->body())) {
281 // Non-common symbols take precedence over common symbols.
282 if (Config->WarnCommon)
283 warning("common " + S->body()->getName() + " is overridden");
284 return 1;
285 }
286 return 0;
287}
288
289template <class ELFT>
290Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
291 uint64_t Alignment, uint8_t Binding,
292 uint8_t StOther, uint8_t Type,
293 InputFile *File) {
294 Symbol *S;
295 bool WasInserted;
296 std::tie(S, WasInserted) =
297 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
298 /*IsUsedInRegularObj*/ true, File);
299 int Cmp = compareDefined(S, WasInserted, Binding);
300 if (Cmp > 0) {
301 S->Binding = Binding;
302 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
303 } else if (Cmp == 0) {
304 auto *C = dyn_cast<DefinedCommon>(S->body());
305 if (!C) {
306 // Non-common symbols take precedence over common symbols.
307 if (Config->WarnCommon)
308 warning("common " + S->body()->getName() + " is overridden");
309 return S;
310 }
311
312 if (Config->WarnCommon)
313 warning("multiple common of " + S->body()->getName());
314
315 C->Size = std::max(C->Size, Size);
316 C->Alignment = std::max(C->Alignment, Alignment);
317 }
318 return S;
319}
320
321template <class ELFT>
322void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
323 InputFile *NewFile) {
324 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
325 if (Config->AllowMultipleDefinition)
326 warning(Msg);
327 else
328 error(Msg);
329}
330
331template <typename ELFT>
332Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
333 InputSectionBase<ELFT> *Section) {
334 Symbol *S;
335 bool WasInserted;
336 std::tie(S, WasInserted) =
337 insert(Name, Sym.getType(), Sym.getVisibility(),
338 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
339 Section ? Section->getFile() : nullptr);
340 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
341 if (Cmp > 0)
342 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
343 else if (Cmp == 0)
344 reportDuplicate(S->body(), Section->getFile());
345 return S;
346}
347
348template <typename ELFT>
349Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
350 uint8_t StOther) {
351 Symbol *S;
352 bool WasInserted;
353 std::tie(S, WasInserted) =
354 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
355 /*IsUsedInRegularObj*/ true, nullptr);
356 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
357 if (Cmp > 0)
358 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
359 else if (Cmp == 0)
360 reportDuplicate(S->body(), nullptr);
361 return S;
362}
363
364template <typename ELFT>
365Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000366 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000367 uintX_t Value) {
368 Symbol *S;
369 bool WasInserted;
370 std::tie(S, WasInserted) =
371 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
372 /*IsUsedInRegularObj*/ true, nullptr);
373 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
374 if (Cmp > 0)
375 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
376 else if (Cmp == 0)
377 reportDuplicate(S->body(), nullptr);
378 return S;
379}
380
381template <typename ELFT>
382void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
383 const Elf_Sym &Sym,
384 const typename ELFT::Verdef *Verdef) {
385 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
386 // as the visibility, which will leave the visibility in the symbol table
387 // unchanged.
388 Symbol *S;
389 bool WasInserted;
390 std::tie(S, WasInserted) =
391 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
392 /*IsUsedInRegularObj*/ false, F);
393 // Make sure we preempt DSO symbols with default visibility.
394 if (Sym.getVisibility() == STV_DEFAULT)
395 S->ExportDynamic = true;
396 if (WasInserted || isa<Undefined>(S->body()))
397 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
398}
399
400template <class ELFT>
401Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
402 uint8_t StOther, uint8_t Type,
403 bool CanOmitFromDynSym, BitcodeFile *F) {
404 Symbol *S;
405 bool WasInserted;
406 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
407 /*IsUsedInRegularObj*/ false, F);
408 int Cmp =
409 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
410 if (Cmp > 0)
411 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
412 else if (Cmp == 0)
413 reportDuplicate(S->body(), F);
414 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000415}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000416
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000417template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
418 auto It = Symtab.find(Name);
419 if (It == Symtab.end())
420 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000421 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000422}
423
Rui Ueyama3d451792015-10-12 18:03:21 +0000424template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000425void SymbolTable<ELFT>::addLazyArchive(
426 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
427 Symbol *S;
428 bool WasInserted;
429 std::tie(S, WasInserted) = insert(Sym.getName());
430 if (WasInserted) {
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000431 replaceBody<LazyArchive>(S, F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000432 return;
433 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000434 if (!S->body()->isUndefined())
435 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000436
Peter Collingbourne4f952702016-05-01 04:55:03 +0000437 // Weak undefined symbols should not fetch members from archives. If we were
438 // to keep old symbol we would not know that an archive member was available
439 // if a strong undefined symbol shows up afterwards in the link. If a strong
440 // undefined symbol never shows up, this lazy symbol will get to the end of
441 // the link and must be treated as the weak undefined one. We already marked
442 // this symbol as used when we added it to the symbol table, but we also need
443 // to preserve its type. FIXME: Move the Type field to Symbol.
444 if (S->isWeak()) {
445 replaceBody<LazyArchive>(S, F, Sym, S->body()->Type);
446 return;
447 }
448 MemoryBufferRef MBRef = F->getMember(&Sym);
449 if (!MBRef.getBuffer().empty())
450 addFile(createObjectFile(MBRef, F->getName()));
451}
452
453template <class ELFT>
454void SymbolTable<ELFT>::addLazyObject(StringRef Name, MemoryBufferRef MBRef) {
455 Symbol *S;
456 bool WasInserted;
457 std::tie(S, WasInserted) = insert(Name);
458 if (WasInserted) {
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000459 replaceBody<LazyObject>(S, Name, MBRef, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000460 return;
461 }
462 if (!S->body()->isUndefined())
463 return;
464
465 // See comment for addLazyArchive above.
466 if (S->isWeak())
467 replaceBody<LazyObject>(S, Name, MBRef, S->body()->Type);
468 else
469 addFile(createObjectFile(MBRef));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000470}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000471
Peter Collingbourne892d49802016-04-27 00:05:03 +0000472// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000473template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000474 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000475 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
476 if (std::unique_ptr<InputFile> File = L->getFile())
477 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000478}
479
Rui Ueyama93bfee52015-10-13 18:10:33 +0000480// This function takes care of the case in which shared libraries depend on
481// the user program (not the other way, which is usual). Shared libraries
482// may have undefined symbols, expecting that the user program provides
483// the definitions for them. An example is BSD's __progname symbol.
484// We need to put such symbols to the main program's .dynsym so that
485// shared libraries can find them.
486// Except this, we ignore undefined symbols in DSOs.
487template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000488 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
489 for (StringRef U : File->getUndefinedSymbols())
490 if (SymbolBody *Sym = find(U))
491 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000492 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000493}
494
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000495// This function process the dynamic list option by marking all the symbols
496// to be exported in the dynamic table.
497template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
498 for (StringRef S : Config->DynamicList)
499 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000500 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000501}
502
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000503// This function processes the --version-script option by marking all global
504// symbols with the VersionScriptGlobal flag, which acts as a filter on the
505// dynamic symbol table.
506template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
507 for (StringRef S : Config->VersionScriptGlobals)
508 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000509 B->symbol()->VersionScriptGlobal = true;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000510}
511
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000512template class elf::SymbolTable<ELF32LE>;
513template class elf::SymbolTable<ELF32BE>;
514template class elf::SymbolTable<ELF64LE>;
515template class elf::SymbolTable<ELF64BE>;