blob: ab5ff8b7870bb8645f75667ba3ecc06f2530a2fa [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"
Davide Italiano8e1131d2016-06-29 02:46:51 +000020#include "LinkerScript.h"
Rui Ueyama93c9af42016-06-29 08:01:32 +000021#include "Strings.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022#include "Symbols.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000023#include "llvm/Bitcode/ReaderWriter.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000024#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000027using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000028using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rui Ueyamac9559d92016-01-05 20:47:37 +000033// All input object files must be for the same architecture
34// (e.g. it does not make sense to link x86 object files with
35// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000036template <class ELFT> static bool isCompatible(InputFile *F) {
37 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000038 return true;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000039 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000040 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000041 StringRef A = F->getName();
42 StringRef B = Config->Emulation;
43 if (B.empty())
44 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000045 error(A + " is incompatible with " + B);
46 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000047}
48
Rui Ueyamac9559d92016-01-05 20:47:37 +000049// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000050template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000051void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000052 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000053 if (!isCompatible<ELFT>(FileP))
54 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000055
Rui Ueyama89575742015-12-16 22:59:13 +000056 // .a file
57 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000058 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000059 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000060 return;
61 }
Rui Ueyama3d451792015-10-12 18:03:21 +000062
George Rimar2a78fce2016-04-13 18:07:57 +000063 // Lazy object file
64 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
65 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000066 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000067 return;
68 }
69
70 if (Config->Trace)
71 llvm::outs() << getFilename(FileP) << "\n";
72
Rui Ueyama89575742015-12-16 22:59:13 +000073 // .so file
74 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
75 // DSOs are uniquified not by filename but by soname.
76 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000077 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000078 return;
Rui Ueyama89575742015-12-16 22:59:13 +000079
Rafael Espindola21f7bd42015-12-23 14:35:51 +000080 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000081 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000082 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000083 }
Rui Ueyama89575742015-12-16 22:59:13 +000084
Rui Ueyamaf8baa662016-04-07 19:24:51 +000085 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000086 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
87 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000088 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000089 return;
90 }
91
Rui Ueyamaf8baa662016-04-07 19:24:51 +000092 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +000093 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000094 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000095 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +000096}
97
Rui Ueyama42554752016-04-23 00:26:32 +000098// This function is where all the optimizations of link-time
99// optimization happens. When LTO is in use, some input files are
100// not in native object file format but in the LLVM bitcode format.
101// This function compiles bitcode files into a few big native files
102// using LLVM functions and replaces bitcode symbols with the results.
103// Because all bitcode files that consist of a program are passed
104// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000105template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
106 if (BitcodeFiles.empty())
107 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000108
109 // Compile bitcode files.
110 Lto.reset(new BitcodeCompiler);
111 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
112 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000113 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000114
115 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000116 for (auto &IF : IFs) {
117 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
118
119 llvm::DenseSet<StringRef> DummyGroups;
120 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000121 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000122 }
123}
124
Rafael Espindola0e604f92015-09-25 18:56:53 +0000125template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000126DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
127 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000128 return cast<DefinedRegular<ELFT>>(
129 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000130}
131
Rui Ueyamac9559d92016-01-05 20:47:37 +0000132// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000133// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000134template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000135DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
136 uint8_t Visibility) {
137 if (!find(Name))
138 return nullptr;
139 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000140}
141
Rui Ueyamadeb15402016-01-07 17:20:07 +0000142// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
143// Used to implement --wrap.
144template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000145 SymbolBody *B = find(Name);
146 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000147 return;
148 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000149 Symbol *Sym = B->symbol();
150 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
151 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
152 // We rename symbols by replacing the old symbol's SymbolBody with the new
153 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
154 // old symbol to instead refer to the new symbol.
155 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
156 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000157}
158
Peter Collingbournedadcc172016-04-22 18:42:48 +0000159static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
160 if (VA == STV_DEFAULT)
161 return VB;
162 if (VB == STV_DEFAULT)
163 return VA;
164 return std::min(VA, VB);
165}
166
George Rimar9fc1d4e2016-06-29 08:36:36 +0000167// A symbol version may be included in a symbol name as a suffix after '@'.
George Rimar43651582016-06-28 08:21:10 +0000168// This function parses that part and returns a version ID number.
169static uint16_t getVersionId(Symbol *Sym, StringRef Name) {
170 size_t VersionBegin = Name.find('@');
171 if (VersionBegin == StringRef::npos)
172 return Config->VersionScriptGlobalByDefault ? VER_NDX_GLOBAL
173 : VER_NDX_LOCAL;
174
175 // If symbol name contains '@' or '@@' we can assign its version id right
George Rimar9fc1d4e2016-06-29 08:36:36 +0000176 // here. '@@' means the default version. It is usually the most recent one.
George Rimar43651582016-06-28 08:21:10 +0000177 // VERSYM_HIDDEN flag should be set for all non-default versions.
178 StringRef Version = Name.drop_front(VersionBegin + 1);
179 bool Default = Version.startswith("@");
180 if (Default)
181 Version = Version.drop_front();
182
183 size_t I = 2;
184 for (elf::Version &V : Config->SymbolVersions) {
185 if (V.Name == Version)
186 return Default ? I : (I | VERSYM_HIDDEN);
187 ++I;
188 }
189 error("symbol " + Name + " has undefined version " + Version);
190 return 0;
191}
192
Rui Ueyamab4de5952016-01-08 22:01:33 +0000193// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000194template <class ELFT>
195std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000196 unsigned NumSyms = SymVector.size();
197 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
198 Symbol *Sym;
199 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000200 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000201 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000202 Sym->Visibility = STV_DEFAULT;
203 Sym->IsUsedInRegularObj = false;
204 Sym->ExportDynamic = false;
George Rimar43651582016-06-28 08:21:10 +0000205 Sym->VersionId = getVersionId(Sym, Name);
206 Sym->VersionedName =
207 Sym->VersionId != VER_NDX_LOCAL && Sym->VersionId != VER_NDX_GLOBAL;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000208 SymVector.push_back(Sym);
209 } else {
210 Sym = SymVector[P.first->second];
211 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000212 return {Sym, P.second};
213}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000214
Peter Collingbourne4f952702016-05-01 04:55:03 +0000215// Find an existing symbol or create and insert a new one, then apply the given
216// attributes.
217template <class ELFT>
218std::pair<Symbol *, bool>
219SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
220 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
221 InputFile *File) {
222 Symbol *S;
223 bool WasInserted;
224 std::tie(S, WasInserted) = insert(Name);
225
226 // Merge in the new symbol's visibility.
227 S->Visibility = getMinVisibility(S->Visibility, Visibility);
228 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
229 S->ExportDynamic = true;
230 if (IsUsedInRegularObj)
231 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000232 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
233 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000234 error("TLS attribute mismatch for symbol: " +
235 conflictMsg(S->body(), File));
236
237 return {S, WasInserted};
238}
239
240// Construct a string in the form of "Sym in File1 and File2".
241// Used to construct an error message.
242template <typename ELFT>
243std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
244 InputFile *NewFile) {
245 StringRef Sym = Existing->getName();
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000246 return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) +
247 " and " + getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000248}
249
250template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
251 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000252 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000253}
254
255template <class ELFT>
256Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
257 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000258 bool CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000259 InputFile *File) {
260 Symbol *S;
261 bool WasInserted;
262 std::tie(S, WasInserted) =
Rafael Espindolacc70da32016-06-15 17:56:10 +0000263 insert(Name, Type, StOther & 3, CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000264 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
265 if (WasInserted) {
266 S->Binding = Binding;
267 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000268 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000269 return S;
270 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000271 if (Binding != STB_WEAK) {
272 if (S->body()->isShared() || S->body()->isLazy())
273 S->Binding = Binding;
274 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
275 SS->File->IsUsed = true;
276 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000277 if (auto *L = dyn_cast<Lazy>(S->body())) {
278 // An undefined weak will not fetch archive members, but we have to remember
279 // its type. See also comment in addLazyArchive.
280 if (S->isWeak())
281 L->Type = Type;
282 else if (auto F = L->getFile())
283 addFile(std::move(F));
284 }
285 return S;
286}
287
288// We have a new defined symbol with the specified binding. Return 1 if the new
289// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
290// strong defined symbols.
291static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
292 if (WasInserted)
293 return 1;
294 SymbolBody *Body = S->body();
295 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
296 return 1;
297 if (Binding == STB_WEAK)
298 return -1;
299 if (S->isWeak())
300 return 1;
301 return 0;
302}
303
304// We have a new non-common defined symbol with the specified binding. Return 1
305// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
306// is a conflict. If the new symbol wins, also update the binding.
307static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
308 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
309 if (Cmp > 0)
310 S->Binding = Binding;
311 return Cmp;
312 }
313 if (isa<DefinedCommon>(S->body())) {
314 // Non-common symbols take precedence over common symbols.
315 if (Config->WarnCommon)
316 warning("common " + S->body()->getName() + " is overridden");
317 return 1;
318 }
319 return 0;
320}
321
322template <class ELFT>
323Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
324 uint64_t Alignment, uint8_t Binding,
325 uint8_t StOther, uint8_t Type,
326 InputFile *File) {
327 Symbol *S;
328 bool WasInserted;
329 std::tie(S, WasInserted) =
330 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
331 /*IsUsedInRegularObj*/ true, File);
332 int Cmp = compareDefined(S, WasInserted, Binding);
333 if (Cmp > 0) {
334 S->Binding = Binding;
335 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
336 } else if (Cmp == 0) {
337 auto *C = dyn_cast<DefinedCommon>(S->body());
338 if (!C) {
339 // Non-common symbols take precedence over common symbols.
340 if (Config->WarnCommon)
341 warning("common " + S->body()->getName() + " is overridden");
342 return S;
343 }
344
345 if (Config->WarnCommon)
346 warning("multiple common of " + S->body()->getName());
347
348 C->Size = std::max(C->Size, Size);
349 C->Alignment = std::max(C->Alignment, Alignment);
350 }
351 return S;
352}
353
354template <class ELFT>
355void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
356 InputFile *NewFile) {
357 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
358 if (Config->AllowMultipleDefinition)
359 warning(Msg);
360 else
361 error(Msg);
362}
363
364template <typename ELFT>
365Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
366 InputSectionBase<ELFT> *Section) {
367 Symbol *S;
368 bool WasInserted;
369 std::tie(S, WasInserted) =
370 insert(Name, Sym.getType(), Sym.getVisibility(),
371 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
372 Section ? Section->getFile() : nullptr);
373 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
374 if (Cmp > 0)
375 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
376 else if (Cmp == 0)
377 reportDuplicate(S->body(), Section->getFile());
378 return S;
379}
380
381template <typename ELFT>
382Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
383 uint8_t StOther) {
384 Symbol *S;
385 bool WasInserted;
386 std::tie(S, WasInserted) =
387 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
388 /*IsUsedInRegularObj*/ true, nullptr);
389 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
390 if (Cmp > 0)
391 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
392 else if (Cmp == 0)
393 reportDuplicate(S->body(), nullptr);
394 return S;
395}
396
397template <typename ELFT>
398Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000399 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000400 uintX_t Value) {
401 Symbol *S;
402 bool WasInserted;
403 std::tie(S, WasInserted) =
404 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
405 /*IsUsedInRegularObj*/ true, nullptr);
406 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
407 if (Cmp > 0)
408 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
409 else if (Cmp == 0)
410 reportDuplicate(S->body(), nullptr);
411 return S;
412}
413
414template <typename ELFT>
415void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
416 const Elf_Sym &Sym,
417 const typename ELFT::Verdef *Verdef) {
418 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
419 // as the visibility, which will leave the visibility in the symbol table
420 // unchanged.
421 Symbol *S;
422 bool WasInserted;
423 std::tie(S, WasInserted) =
424 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
425 /*IsUsedInRegularObj*/ false, F);
426 // Make sure we preempt DSO symbols with default visibility.
427 if (Sym.getVisibility() == STV_DEFAULT)
428 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000429 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000430 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000431 if (!S->isWeak())
432 F->IsUsed = true;
433 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000434}
435
436template <class ELFT>
437Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
438 uint8_t StOther, uint8_t Type,
439 bool CanOmitFromDynSym, BitcodeFile *F) {
440 Symbol *S;
441 bool WasInserted;
442 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
443 /*IsUsedInRegularObj*/ false, F);
444 int Cmp =
445 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
446 if (Cmp > 0)
447 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
448 else if (Cmp == 0)
449 reportDuplicate(S->body(), F);
450 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000451}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000452
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000453template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
454 auto It = Symtab.find(Name);
455 if (It == Symtab.end())
456 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000457 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000458}
459
Rui Ueyama48e42512016-06-29 04:47:39 +0000460// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000461template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000462std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
463 // Fast-path. Fallback to find() if Pattern doesn't contain any wildcard
464 // characters.
Rui Ueyama48e42512016-06-29 04:47:39 +0000465 if (Pattern.find_first_of("?*") == StringRef::npos) {
466 if (SymbolBody *B = find(Pattern))
467 if (!B->isUndefined())
468 return {B};
469 return {};
470 }
Davide Italiano8e1131d2016-06-29 02:46:51 +0000471
Rui Ueyama48e42512016-06-29 04:47:39 +0000472 std::vector<SymbolBody *> Res;
473 for (auto &It : Symtab) {
474 StringRef Name = It.first.Val;
475 SymbolBody *B = SymVector[It.second]->body();
Rui Ueyama722830a2016-06-29 05:32:09 +0000476 if (!B->isUndefined() && globMatch(Pattern, Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000477 Res.push_back(B);
478 }
479 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000480}
481
482template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000483void SymbolTable<ELFT>::addLazyArchive(
484 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
485 Symbol *S;
486 bool WasInserted;
487 std::tie(S, WasInserted) = insert(Sym.getName());
488 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000489 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000490 return;
491 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000492 if (!S->body()->isUndefined())
493 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000494
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495 // Weak undefined symbols should not fetch members from archives. If we were
496 // to keep old symbol we would not know that an archive member was available
497 // if a strong undefined symbol shows up afterwards in the link. If a strong
498 // undefined symbol never shows up, this lazy symbol will get to the end of
499 // the link and must be treated as the weak undefined one. We already marked
500 // this symbol as used when we added it to the symbol table, but we also need
501 // to preserve its type. FIXME: Move the Type field to Symbol.
502 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000503 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000504 return;
505 }
506 MemoryBufferRef MBRef = F->getMember(&Sym);
507 if (!MBRef.getBuffer().empty())
508 addFile(createObjectFile(MBRef, F->getName()));
509}
510
511template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000512void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000513 Symbol *S;
514 bool WasInserted;
515 std::tie(S, WasInserted) = insert(Name);
516 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000517 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000518 return;
519 }
520 if (!S->body()->isUndefined())
521 return;
522
523 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000524 if (S->isWeak()) {
525 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
526 } else {
527 MemoryBufferRef MBRef = Obj.getBuffer();
528 if (!MBRef.getBuffer().empty())
529 addFile(createObjectFile(MBRef));
530 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000531}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000532
Peter Collingbourne892d49802016-04-27 00:05:03 +0000533// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000534template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000535 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
537 if (std::unique_ptr<InputFile> File = L->getFile())
538 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000539}
540
Rui Ueyama93bfee52015-10-13 18:10:33 +0000541// This function takes care of the case in which shared libraries depend on
542// the user program (not the other way, which is usual). Shared libraries
543// may have undefined symbols, expecting that the user program provides
544// the definitions for them. An example is BSD's __progname symbol.
545// We need to put such symbols to the main program's .dynsym so that
546// shared libraries can find them.
547// Except this, we ignore undefined symbols in DSOs.
548template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000549 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
550 for (StringRef U : File->getUndefinedSymbols())
551 if (SymbolBody *Sym = find(U))
552 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000553 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000554}
555
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000556// This function process the dynamic list option by marking all the symbols
557// to be exported in the dynamic table.
558template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
559 for (StringRef S : Config->DynamicList)
560 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000561 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000562}
563
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000564// This function processes the --version-script option by marking all global
565// symbols with the VersionScriptGlobal flag, which acts as a filter on the
566// dynamic symbol table.
567template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000568 // If version script does not contain versions declarations,
569 // we just should mark global symbols.
570 if (!Config->VersionScriptGlobals.empty()) {
571 for (StringRef S : Config->VersionScriptGlobals)
572 if (SymbolBody *B = find(S))
573 B->symbol()->VersionId = VER_NDX_GLOBAL;
574 return;
575 }
576
577 // If we have symbols version declarations, we should
578 // assign version references for each symbol.
579 size_t I = 2;
580 for (Version &V : Config->SymbolVersions) {
George Rimar36b2c0a2016-06-28 08:07:26 +0000581 for (StringRef Name : V.Globals) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000582 std::vector<SymbolBody *> Syms = findAll(Name);
583 if (Syms.empty()) {
584 if (Config->NoUndefinedVersion)
585 error("version script assignment of " + V.Name + " to symbol " +
586 Name + " failed: symbol not defined");
587 continue;
588 }
George Rimar36b2c0a2016-06-28 08:07:26 +0000589
Rui Ueyama48e42512016-06-29 04:47:39 +0000590 for (SymbolBody *B : Syms) {
Davide Italiano8e1131d2016-06-29 02:46:51 +0000591 if (B->symbol()->VersionId != VER_NDX_GLOBAL &&
592 B->symbol()->VersionId != VER_NDX_LOCAL)
593 warning("duplicate symbol " + Name + " in version script");
594 B->symbol()->VersionId = I;
595 }
George Rimar36b2c0a2016-06-28 08:07:26 +0000596 }
George Rimard3566302016-06-20 11:55:12 +0000597 ++I;
598 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000599}
600
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000601// Print the module names which define the notified
602// symbols provided through -y or --trace-symbol option.
603template <class ELFT> void SymbolTable<ELFT>::traceDefined() {
604 for (const auto &Symbol : Config->TraceSymbol)
605 if (SymbolBody *B = find(Symbol.getKey()))
606 if (B->isDefined() || B->isCommon())
607 if (InputFile *File = B->getSourceFile<ELFT>())
608 outs() << getFilename(File) << ": definition of "
609 << B->getName() << "\n";
610}
611
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000612template class elf::SymbolTable<ELF32LE>;
613template class elf::SymbolTable<ELF32BE>;
614template class elf::SymbolTable<ELF64LE>;
615template class elf::SymbolTable<ELF64BE>;