blob: d67ad8699f3c1d49394e9bc5641a326e78fa7c41 [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()));
Rui Ueyama89575742015-12-16 22:59:13 +000067 F->parse();
68 for (Lazy &Sym : F->getLazySymbols())
Michael J. Spencer1b348a62015-09-04 22:28:10 +000069 addLazy(&Sym);
70 return;
71 }
Rui Ueyama3d451792015-10-12 18:03:21 +000072
George Rimar2a78fce2016-04-13 18:07:57 +000073 // Lazy object file
74 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
75 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
76 F->parse();
77 for (Lazy &Sym : F->getLazySymbols())
78 addLazy(&Sym);
79 return;
80 }
81
82 if (Config->Trace)
83 llvm::outs() << getFilename(FileP) << "\n";
84
Rui Ueyama89575742015-12-16 22:59:13 +000085 // .so file
86 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
87 // DSOs are uniquified not by filename but by soname.
88 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000089 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000090 return;
Rui Ueyama89575742015-12-16 22:59:13 +000091
Rafael Espindola21f7bd42015-12-23 14:35:51 +000092 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000093 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000094 for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
95 resolve(&B);
96 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000097 }
Rui Ueyama89575742015-12-16 22:59:13 +000098
Rui Ueyamaf8baa662016-04-07 19:24:51 +000099 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000100 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
101 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Rafael Espindola4de44b72016-03-02 15:43:50 +0000102 F->parse(ComdatGroups);
Rafael Espindola297ce4e2016-02-26 21:31:34 +0000103 for (SymbolBody *B : F->getSymbols())
Rui Ueyamaf7149552016-03-11 18:46:51 +0000104 if (B)
105 resolve(B);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000106 return;
107 }
108
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000109 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +0000110 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +0000111 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +0000112 F->parse(ComdatGroups);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000113 for (SymbolBody *B : F->getNonLocalSymbols())
Rui Ueyama89575742015-12-16 22:59:13 +0000114 resolve(B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000115}
116
Rui Ueyama42554752016-04-23 00:26:32 +0000117// This function is where all the optimizations of link-time
118// optimization happens. When LTO is in use, some input files are
119// not in native object file format but in the LLVM bitcode format.
120// This function compiles bitcode files into a few big native files
121// using LLVM functions and replaces bitcode symbols with the results.
122// Because all bitcode files that consist of a program are passed
123// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000124template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
125 if (BitcodeFiles.empty())
126 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000127
128 // Compile bitcode files.
129 Lto.reset(new BitcodeCompiler);
130 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
131 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000132 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000133
134 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000135 for (auto &IF : IFs) {
136 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
137
138 llvm::DenseSet<StringRef> DummyGroups;
139 Obj->parse(DummyGroups);
140 for (SymbolBody *Body : Obj->getNonLocalSymbols()) {
141 Symbol *Sym = insert(Body);
Davide Italianobc176632016-04-15 22:38:10 +0000142 if (!Sym->Body->isUndefined() && Body->isUndefined())
143 continue;
144 Sym->Body = Body;
145 }
146 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000147 }
148}
149
Rui Ueyama01a65b12015-12-24 10:37:32 +0000150// Add an undefined symbol.
Rui Ueyamaff777682015-10-09 21:12:40 +0000151template <class ELFT>
152SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
Rafael Espindolaf4765732016-04-06 13:22:41 +0000153 auto *Sym = new (Alloc)
154 UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, false);
Rui Ueyamaff777682015-10-09 21:12:40 +0000155 resolve(Sym);
156 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000157}
158
Rui Ueyama01a65b12015-12-24 10:37:32 +0000159// Add an undefined symbol. Unlike addUndefined, that symbol
160// doesn't have to be resolved, thus "opt" (optional).
Rui Ueyamaff777682015-10-09 21:12:40 +0000161template <class ELFT>
162SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
Rafael Espindolaf4765732016-04-06 13:22:41 +0000163 auto *Sym = new (Alloc)
164 UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_HIDDEN, /*Type*/ 0, true);
Rui Ueyamaff777682015-10-09 21:12:40 +0000165 resolve(Sym);
166 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +0000167}
168
Rafael Espindola0e604f92015-09-25 18:56:53 +0000169template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000170DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
171 uint8_t Visibility) {
Rui Ueyama79c73732016-01-08 21:53:28 +0000172 // Pass nullptr because absolute symbols have no corresponding input sections.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000173 auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, STB_GLOBAL, Visibility);
Rui Ueyama79c73732016-01-08 21:53:28 +0000174 resolve(Sym);
175 return Sym;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000176}
177
178template <class ELFT>
Rui Ueyama79c73732016-01-08 21:53:28 +0000179SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name,
George Rimaraa4dc202016-03-01 16:23:13 +0000180 OutputSectionBase<ELFT> &Sec,
Peter Collingbournef6e9b4e2016-04-13 16:57:28 +0000181 uintX_t Val) {
182 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000183 resolve(Sym);
Rui Ueyama79c73732016-01-08 21:53:28 +0000184 return Sym;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000185}
186
Rui Ueyamac9559d92016-01-05 20:47:37 +0000187// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000188// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000189template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000190DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
191 uint8_t Visibility) {
192 if (!find(Name))
193 return nullptr;
194 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000195}
196
Rui Ueyamadeb15402016-01-07 17:20:07 +0000197// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
198// Used to implement --wrap.
199template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
200 if (Symtab.count(Name) == 0)
201 return;
202 StringSaver Saver(Alloc);
Peter Collingbournedadcc172016-04-22 18:42:48 +0000203 Symbol *Sym = addUndefined(Name)->Backref;
204 Symbol *Real = addUndefined(Saver.save("__real_" + Name))->Backref;
205 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->Backref;
Rui Ueyamadeb15402016-01-07 17:20:07 +0000206 Real->Body = Sym->Body;
207 Sym->Body = Wrap->Body;
208}
209
Rui Ueyama533336a2015-12-16 22:26:48 +0000210// Returns a file from which symbol B was created.
Rui Ueyama2a65a492016-01-05 20:01:29 +0000211// If B does not belong to any file, returns a nullptr.
Rui Ueyama209f6cb2016-04-23 01:10:13 +0000212// This function is slow, but it's okay as it is used only for error messages.
Rafael Espindola18f09502016-02-26 21:49:38 +0000213template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) {
Rui Ueyama533336a2015-12-16 22:26:48 +0000214 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000215 ArrayRef<SymbolBody *> Syms = F->getSymbols();
216 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
Rui Ueyama533336a2015-12-16 22:26:48 +0000217 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000218 }
Rafael Espindola18f09502016-02-26 21:49:38 +0000219 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) {
220 ArrayRef<SymbolBody *> Syms = F->getSymbols();
221 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
222 return F.get();
223 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000224 return nullptr;
225}
226
Rui Ueyamab4de5952016-01-08 22:01:33 +0000227// Construct a string in the form of "Sym in File1 and File2".
228// Used to construct an error message.
Rui Ueyama533336a2015-12-16 22:26:48 +0000229template <class ELFT>
230std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
Rafael Espindola18f09502016-02-26 21:49:38 +0000231 InputFile *F1 = findFile(Old);
232 InputFile *F2 = findFile(New);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000233 StringRef Sym = Old->getName();
Rui Ueyama71c066d2016-02-02 08:22:41 +0000234 return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000235}
236
Michael J. Spencer84487f12015-07-24 21:03:07 +0000237// This function resolves conflicts if there's an existing symbol with
238// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000239template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000240 Symbol *Sym = insert(New);
241 if (Sym->Body == New)
242 return;
243
244 SymbolBody *Existing = Sym->Body;
245
Davide Italianoc4965002016-04-02 23:47:54 +0000246 if (auto *L = dyn_cast<Lazy>(Existing)) {
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000247 Sym->Binding = New->Binding;
Rafael Espindolaf4765732016-04-06 13:22:41 +0000248 if (New->isUndefined()) {
249 addMemberFile(New, L);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000250 return;
251 }
Rui Ueyamac5b95122015-12-16 23:23:14 +0000252 // Found a definition for something also in an archive.
253 // Ignore the archive definition.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000254 Sym->Body = New;
255 return;
256 }
257
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000258 if (New->isTls() != Existing->isTls()) {
Rui Ueyama16ba6692016-01-29 19:41:13 +0000259 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
260 return;
261 }
Igor Kudrin65bddea2015-10-09 09:58:39 +0000262
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000263 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
264 // equivalent (conflicting), or more preferable, respectively.
Peter Collingbourned0856a62016-04-05 00:47:58 +0000265 int Comp = Existing->compare(New);
Rui Ueyama2e0a9ff2016-01-06 00:09:39 +0000266 if (Comp == 0) {
George Rimar57610422016-03-11 14:43:02 +0000267 std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
Rui Ueyama16ba6692016-01-29 19:41:13 +0000268 if (Config->AllowMultipleDefinition)
269 warning(S);
270 else
271 error(S);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000272 return;
273 }
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000274 if (Comp < 0) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000275 Sym->Body = New;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000276 if (!New->isShared())
277 Sym->Binding = New->Binding;
278 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000279}
280
Peter Collingbournedadcc172016-04-22 18:42:48 +0000281static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
282 if (VA == STV_DEFAULT)
283 return VB;
284 if (VB == STV_DEFAULT)
285 return VA;
286 return std::min(VA, VB);
287}
288
289static bool shouldExport(SymbolBody *B) {
290 if (Config->Shared || Config->ExportDynamic) {
291 // Export most symbols except for those that do not need to be exported.
292 return !B->CanOmitFromDynSym;
293 }
294 // Make sure we preempt DSO symbols with default visibility.
295 return B->isShared() && B->getVisibility() == STV_DEFAULT;
296}
297
Rui Ueyamab4de5952016-01-08 22:01:33 +0000298// Find an existing symbol or create and insert a new one.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000299template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000300 StringRef Name = New->getName();
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000301 unsigned NumSyms = SymVector.size();
302 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
303 Symbol *Sym;
304 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000305 Sym = new (Alloc) Symbol;
306 Sym->Body = New;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000307 Sym->Binding = New->isShared() ? STB_GLOBAL : New->Binding;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000308 Sym->Visibility = STV_DEFAULT;
309 Sym->IsUsedInRegularObj = false;
310 Sym->ExportDynamic = false;
311 Sym->VersionScriptGlobal = !Config->VersionScript;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000312 SymVector.push_back(Sym);
313 } else {
314 Sym = SymVector[P.first->second];
315 }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000316 New->Backref = Sym;
317
318 // Merge in the new symbol's visibility. DSO symbols do not affect visibility
319 // in the output.
320 if (!New->isShared())
321 Sym->Visibility = getMinVisibility(Sym->Visibility, New->getVisibility());
322 Sym->ExportDynamic = Sym->ExportDynamic || shouldExport(New);
323 SymbolBody::Kind K = New->kind();
324 if (K == SymbolBody::DefinedRegularKind ||
325 K == SymbolBody::DefinedCommonKind ||
326 K == SymbolBody::DefinedSyntheticKind ||
327 K == SymbolBody::UndefinedElfKind)
328 Sym->IsUsedInRegularObj = true;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000329 return Sym;
330}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000331
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000332template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
333 auto It = Symtab.find(Name);
334 if (It == Symtab.end())
335 return nullptr;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000336 return SymVector[It->second]->Body;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000337}
338
Rui Ueyamac5b95122015-12-16 23:23:14 +0000339template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
340 Symbol *Sym = insert(L);
Rafael Espindolaf4765732016-04-06 13:22:41 +0000341 SymbolBody *Cur = Sym->Body;
342 if (Cur == L)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000343 return;
Rafael Espindolaf4765732016-04-06 13:22:41 +0000344 if (Cur->isUndefined()) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000345 Sym->Body = L;
Rafael Espindolaf4765732016-04-06 13:22:41 +0000346 addMemberFile(Cur, L);
Rafael Espindola8614c562015-10-06 14:33:58 +0000347 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000348}
349
Rui Ueyama3d451792015-10-12 18:03:21 +0000350template <class ELFT>
Rafael Espindolaf4765732016-04-06 13:22:41 +0000351void SymbolTable<ELFT>::addMemberFile(SymbolBody *Undef, Lazy *L) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000352 // Weak undefined symbols should not fetch members from archives.
353 // If we were to keep old symbol we would not know that an archive member was
354 // available if a strong undefined symbol shows up afterwards in the link.
355 // If a strong undefined symbol never shows up, this lazy symbol will
356 // get to the end of the link and must be treated as the weak undefined one.
Peter Collingbournee8d46622016-04-22 19:56:45 +0000357 // We already marked this symbol as used when we added it to the symbol table,
358 // but we also need to preserve its binding and type.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000359 if (Undef->isWeak()) {
Peter Collingbournee8d46622016-04-22 19:56:45 +0000360 // FIXME: Consider moving these members to Symbol.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000361 L->Type = Undef->Type;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000362 return;
363 }
364
365 // Fetch a member file that has the definition for L.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000366 // getMember returns nullptr if the member was already read from the library.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000367 if (std::unique_ptr<InputFile> File = L->getFile())
Rui Ueyama690db672015-10-14 22:32:10 +0000368 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000369}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000370
Rui Ueyama93bfee52015-10-13 18:10:33 +0000371// This function takes care of the case in which shared libraries depend on
372// the user program (not the other way, which is usual). Shared libraries
373// may have undefined symbols, expecting that the user program provides
374// the definitions for them. An example is BSD's __progname symbol.
375// We need to put such symbols to the main program's .dynsym so that
376// shared libraries can find them.
377// Except this, we ignore undefined symbols in DSOs.
378template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000379 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
380 for (StringRef U : File->getUndefinedSymbols())
381 if (SymbolBody *Sym = find(U))
382 if (Sym->isDefined())
Peter Collingbournedadcc172016-04-22 18:42:48 +0000383 Sym->Backref->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000384}
385
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000386// This function process the dynamic list option by marking all the symbols
387// to be exported in the dynamic table.
388template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
389 for (StringRef S : Config->DynamicList)
390 if (SymbolBody *B = find(S))
Peter Collingbournedadcc172016-04-22 18:42:48 +0000391 B->Backref->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000392}
393
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000394// This function processes the --version-script option by marking all global
395// symbols with the VersionScriptGlobal flag, which acts as a filter on the
396// dynamic symbol table.
397template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
398 for (StringRef S : Config->VersionScriptGlobals)
399 if (SymbolBody *B = find(S))
400 B->Backref->VersionScriptGlobal = true;
401}
402
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000403template class elf::SymbolTable<ELF32LE>;
404template class elf::SymbolTable<ELF32BE>;
405template class elf::SymbolTable<ELF64LE>;
406template class elf::SymbolTable<ELF64BE>;