blob: 787976f83d061912ff97079cf6cc7d663589586e [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 Ueyama5e64d3f2016-06-29 01:30:50 +000038 if (F->EKind == Config->EKind && F->EMachine == 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
George Rimar43651582016-06-28 08:21:10 +0000166// A symbol version may be included in a symbol name as a prefix after '@'.
167// This function parses that part and returns a version ID number.
168static uint16_t getVersionId(Symbol *Sym, StringRef Name) {
169 size_t VersionBegin = Name.find('@');
170 if (VersionBegin == StringRef::npos)
171 return Config->VersionScriptGlobalByDefault ? VER_NDX_GLOBAL
172 : VER_NDX_LOCAL;
173
174 // If symbol name contains '@' or '@@' we can assign its version id right
175 // here. '@@' means version by default. It is usually the most recent one.
176 // VERSYM_HIDDEN flag should be set for all non-default versions.
177 StringRef Version = Name.drop_front(VersionBegin + 1);
178 bool Default = Version.startswith("@");
179 if (Default)
180 Version = Version.drop_front();
181
182 size_t I = 2;
183 for (elf::Version &V : Config->SymbolVersions) {
184 if (V.Name == Version)
185 return Default ? I : (I | VERSYM_HIDDEN);
186 ++I;
187 }
188 error("symbol " + Name + " has undefined version " + Version);
189 return 0;
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;
George Rimar43651582016-06-28 08:21:10 +0000204 Sym->VersionId = getVersionId(Sym, Name);
205 Sym->VersionedName =
206 Sym->VersionId != VER_NDX_LOCAL && Sym->VersionId != VER_NDX_GLOBAL;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000207 SymVector.push_back(Sym);
208 } else {
209 Sym = SymVector[P.first->second];
210 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000211 return {Sym, P.second};
212}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000213
Peter Collingbourne4f952702016-05-01 04:55:03 +0000214// Find an existing symbol or create and insert a new one, then apply the given
215// attributes.
216template <class ELFT>
217std::pair<Symbol *, bool>
218SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
219 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
220 InputFile *File) {
221 Symbol *S;
222 bool WasInserted;
223 std::tie(S, WasInserted) = insert(Name);
224
225 // Merge in the new symbol's visibility.
226 S->Visibility = getMinVisibility(S->Visibility, Visibility);
227 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
228 S->ExportDynamic = true;
229 if (IsUsedInRegularObj)
230 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000231 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
232 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000233 error("TLS attribute mismatch for symbol: " +
234 conflictMsg(S->body(), File));
235
236 return {S, WasInserted};
237}
238
239// Construct a string in the form of "Sym in File1 and File2".
240// Used to construct an error message.
241template <typename ELFT>
242std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
243 InputFile *NewFile) {
244 StringRef Sym = Existing->getName();
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000245 return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) +
246 " and " + getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000247}
248
249template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
250 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000251 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000252}
253
254template <class ELFT>
255Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
256 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000257 bool CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000258 InputFile *File) {
259 Symbol *S;
260 bool WasInserted;
261 std::tie(S, WasInserted) =
Rafael Espindolacc70da32016-06-15 17:56:10 +0000262 insert(Name, Type, StOther & 3, CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000263 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
264 if (WasInserted) {
265 S->Binding = Binding;
266 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000267 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000268 return S;
269 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000270 if (Binding != STB_WEAK) {
271 if (S->body()->isShared() || S->body()->isLazy())
272 S->Binding = Binding;
273 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
274 SS->File->IsUsed = true;
275 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000276 if (auto *L = dyn_cast<Lazy>(S->body())) {
277 // An undefined weak will not fetch archive members, but we have to remember
278 // its type. See also comment in addLazyArchive.
279 if (S->isWeak())
280 L->Type = Type;
281 else if (auto F = L->getFile())
282 addFile(std::move(F));
283 }
284 return S;
285}
286
287// We have a new defined symbol with the specified binding. Return 1 if the new
288// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
289// strong defined symbols.
290static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
291 if (WasInserted)
292 return 1;
293 SymbolBody *Body = S->body();
294 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
295 return 1;
296 if (Binding == STB_WEAK)
297 return -1;
298 if (S->isWeak())
299 return 1;
300 return 0;
301}
302
303// We have a new non-common defined symbol with the specified binding. Return 1
304// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
305// is a conflict. If the new symbol wins, also update the binding.
306static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
307 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
308 if (Cmp > 0)
309 S->Binding = Binding;
310 return Cmp;
311 }
312 if (isa<DefinedCommon>(S->body())) {
313 // Non-common symbols take precedence over common symbols.
314 if (Config->WarnCommon)
315 warning("common " + S->body()->getName() + " is overridden");
316 return 1;
317 }
318 return 0;
319}
320
321template <class ELFT>
322Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
323 uint64_t Alignment, uint8_t Binding,
324 uint8_t StOther, uint8_t Type,
325 InputFile *File) {
326 Symbol *S;
327 bool WasInserted;
328 std::tie(S, WasInserted) =
329 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
330 /*IsUsedInRegularObj*/ true, File);
331 int Cmp = compareDefined(S, WasInserted, Binding);
332 if (Cmp > 0) {
333 S->Binding = Binding;
334 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
335 } else if (Cmp == 0) {
336 auto *C = dyn_cast<DefinedCommon>(S->body());
337 if (!C) {
338 // Non-common symbols take precedence over common symbols.
339 if (Config->WarnCommon)
340 warning("common " + S->body()->getName() + " is overridden");
341 return S;
342 }
343
344 if (Config->WarnCommon)
345 warning("multiple common of " + S->body()->getName());
346
347 C->Size = std::max(C->Size, Size);
348 C->Alignment = std::max(C->Alignment, Alignment);
349 }
350 return S;
351}
352
353template <class ELFT>
354void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
355 InputFile *NewFile) {
356 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
357 if (Config->AllowMultipleDefinition)
358 warning(Msg);
359 else
360 error(Msg);
361}
362
363template <typename ELFT>
364Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
365 InputSectionBase<ELFT> *Section) {
366 Symbol *S;
367 bool WasInserted;
368 std::tie(S, WasInserted) =
369 insert(Name, Sym.getType(), Sym.getVisibility(),
370 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
371 Section ? Section->getFile() : nullptr);
372 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
373 if (Cmp > 0)
374 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
375 else if (Cmp == 0)
376 reportDuplicate(S->body(), Section->getFile());
377 return S;
378}
379
380template <typename ELFT>
381Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
382 uint8_t StOther) {
383 Symbol *S;
384 bool WasInserted;
385 std::tie(S, WasInserted) =
386 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
387 /*IsUsedInRegularObj*/ true, nullptr);
388 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
389 if (Cmp > 0)
390 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
391 else if (Cmp == 0)
392 reportDuplicate(S->body(), nullptr);
393 return S;
394}
395
396template <typename ELFT>
397Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000398 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000399 uintX_t Value) {
400 Symbol *S;
401 bool WasInserted;
402 std::tie(S, WasInserted) =
403 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
404 /*IsUsedInRegularObj*/ true, nullptr);
405 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
406 if (Cmp > 0)
407 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
408 else if (Cmp == 0)
409 reportDuplicate(S->body(), nullptr);
410 return S;
411}
412
413template <typename ELFT>
414void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
415 const Elf_Sym &Sym,
416 const typename ELFT::Verdef *Verdef) {
417 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
418 // as the visibility, which will leave the visibility in the symbol table
419 // unchanged.
420 Symbol *S;
421 bool WasInserted;
422 std::tie(S, WasInserted) =
423 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
424 /*IsUsedInRegularObj*/ false, F);
425 // Make sure we preempt DSO symbols with default visibility.
426 if (Sym.getVisibility() == STV_DEFAULT)
427 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000428 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000429 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000430 if (!S->isWeak())
431 F->IsUsed = true;
432 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000433}
434
435template <class ELFT>
436Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
437 uint8_t StOther, uint8_t Type,
438 bool CanOmitFromDynSym, BitcodeFile *F) {
439 Symbol *S;
440 bool WasInserted;
441 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
442 /*IsUsedInRegularObj*/ false, F);
443 int Cmp =
444 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
445 if (Cmp > 0)
446 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
447 else if (Cmp == 0)
448 reportDuplicate(S->body(), F);
449 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000450}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000451
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000452template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
453 auto It = Symtab.find(Name);
454 if (It == Symtab.end())
455 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000456 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000457}
458
Rui Ueyama3d451792015-10-12 18:03:21 +0000459template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000460void SymbolTable<ELFT>::addLazyArchive(
461 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
462 Symbol *S;
463 bool WasInserted;
464 std::tie(S, WasInserted) = insert(Sym.getName());
465 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000466 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000467 return;
468 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000469 if (!S->body()->isUndefined())
470 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000471
Peter Collingbourne4f952702016-05-01 04:55:03 +0000472 // Weak undefined symbols should not fetch members from archives. If we were
473 // to keep old symbol we would not know that an archive member was available
474 // if a strong undefined symbol shows up afterwards in the link. If a strong
475 // undefined symbol never shows up, this lazy symbol will get to the end of
476 // the link and must be treated as the weak undefined one. We already marked
477 // this symbol as used when we added it to the symbol table, but we also need
478 // to preserve its type. FIXME: Move the Type field to Symbol.
479 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000480 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000481 return;
482 }
483 MemoryBufferRef MBRef = F->getMember(&Sym);
484 if (!MBRef.getBuffer().empty())
485 addFile(createObjectFile(MBRef, F->getName()));
486}
487
488template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000489void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000490 Symbol *S;
491 bool WasInserted;
492 std::tie(S, WasInserted) = insert(Name);
493 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000494 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495 return;
496 }
497 if (!S->body()->isUndefined())
498 return;
499
500 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000501 if (S->isWeak()) {
502 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
503 } else {
504 MemoryBufferRef MBRef = Obj.getBuffer();
505 if (!MBRef.getBuffer().empty())
506 addFile(createObjectFile(MBRef));
507 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000508}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000509
Peter Collingbourne892d49802016-04-27 00:05:03 +0000510// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000511template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000512 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000513 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
514 if (std::unique_ptr<InputFile> File = L->getFile())
515 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000516}
517
Rui Ueyama93bfee52015-10-13 18:10:33 +0000518// This function takes care of the case in which shared libraries depend on
519// the user program (not the other way, which is usual). Shared libraries
520// may have undefined symbols, expecting that the user program provides
521// the definitions for them. An example is BSD's __progname symbol.
522// We need to put such symbols to the main program's .dynsym so that
523// shared libraries can find them.
524// Except this, we ignore undefined symbols in DSOs.
525template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000526 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
527 for (StringRef U : File->getUndefinedSymbols())
528 if (SymbolBody *Sym = find(U))
529 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000530 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000531}
532
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000533// This function process the dynamic list option by marking all the symbols
534// to be exported in the dynamic table.
535template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
536 for (StringRef S : Config->DynamicList)
537 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000538 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000539}
540
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000541// This function processes the --version-script option by marking all global
542// symbols with the VersionScriptGlobal flag, which acts as a filter on the
543// dynamic symbol table.
544template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000545 // If version script does not contain versions declarations,
546 // we just should mark global symbols.
547 if (!Config->VersionScriptGlobals.empty()) {
548 for (StringRef S : Config->VersionScriptGlobals)
549 if (SymbolBody *B = find(S))
550 B->symbol()->VersionId = VER_NDX_GLOBAL;
551 return;
552 }
553
554 // If we have symbols version declarations, we should
555 // assign version references for each symbol.
556 size_t I = 2;
557 for (Version &V : Config->SymbolVersions) {
George Rimar36b2c0a2016-06-28 08:07:26 +0000558 for (StringRef Name : V.Globals) {
559 SymbolBody *B = find(Name);
560 if (!B || B->isUndefined()) {
561 if (Config->NoUndefinedVersion)
562 error("version script assignment of " + V.Name + " to symbol " +
563 Name + " failed: symbol not defined");
564 continue;
George Rimar50b80352016-06-22 09:10:38 +0000565 }
George Rimar36b2c0a2016-06-28 08:07:26 +0000566
567 if (B->symbol()->VersionId != VER_NDX_GLOBAL &&
568 B->symbol()->VersionId != VER_NDX_LOCAL)
569 warning("duplicate symbol " + Name + " in version script");
570 B->symbol()->VersionId = I;
571 }
George Rimard3566302016-06-20 11:55:12 +0000572 ++I;
573 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000574}
575
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000576// Print the module names which define the notified
577// symbols provided through -y or --trace-symbol option.
578template <class ELFT> void SymbolTable<ELFT>::traceDefined() {
579 for (const auto &Symbol : Config->TraceSymbol)
580 if (SymbolBody *B = find(Symbol.getKey()))
581 if (B->isDefined() || B->isCommon())
582 if (InputFile *File = B->getSourceFile<ELFT>())
583 outs() << getFilename(File) << ": definition of "
584 << B->getName() << "\n";
585}
586
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000587template class elf::SymbolTable<ELF32LE>;
588template class elf::SymbolTable<ELF32BE>;
589template class elf::SymbolTable<ELF64LE>;
590template class elf::SymbolTable<ELF64BE>;