blob: e5052281c46d39097c2e4df689c426856a134f6a [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.
Rui Ueyama16ba6692016-01-29 19:41:13 +000036template <class ELFT> static bool isCompatible(InputFile *FileP) {
Rui Ueyama25b44c92015-12-16 23:31:22 +000037 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
38 if (!F)
Rui Ueyama16ba6692016-01-29 19:41:13 +000039 return true;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000040 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000041 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000042 StringRef A = F->getName();
43 StringRef B = Config->Emulation;
44 if (B.empty())
45 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000046 error(A + " is incompatible with " + B);
47 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000048}
49
Rui Ueyamac9559d92016-01-05 20:47:37 +000050// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000051template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000052void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000053 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000054 if (!isCompatible<ELFT>(FileP))
55 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000056
Rui Ueyama89575742015-12-16 22:59:13 +000057 // .a file
58 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000059 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000060 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000061 return;
62 }
Rui Ueyama3d451792015-10-12 18:03:21 +000063
George Rimar2a78fce2016-04-13 18:07:57 +000064 // Lazy object file
65 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
66 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000067 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000068 return;
69 }
70
71 if (Config->Trace)
72 llvm::outs() << getFilename(FileP) << "\n";
73
Rui Ueyama89575742015-12-16 22:59:13 +000074 // .so file
75 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
76 // DSOs are uniquified not by filename but by soname.
77 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000078 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000079 return;
Rui Ueyama89575742015-12-16 22:59:13 +000080
Rafael Espindola21f7bd42015-12-23 14:35:51 +000081 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000082 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000083 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000084 }
Rui Ueyama89575742015-12-16 22:59:13 +000085
Rui Ueyamaf8baa662016-04-07 19:24:51 +000086 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000087 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
88 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000089 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000090 return;
91 }
92
Rui Ueyamaf8baa662016-04-07 19:24:51 +000093 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +000094 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000095 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000096 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +000097}
98
Rui Ueyama42554752016-04-23 00:26:32 +000099// This function is where all the optimizations of link-time
100// optimization happens. When LTO is in use, some input files are
101// not in native object file format but in the LLVM bitcode format.
102// This function compiles bitcode files into a few big native files
103// using LLVM functions and replaces bitcode symbols with the results.
104// Because all bitcode files that consist of a program are passed
105// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000106template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
107 if (BitcodeFiles.empty())
108 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000109
110 // Compile bitcode files.
111 Lto.reset(new BitcodeCompiler);
112 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
113 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000114 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000115
116 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000117 for (auto &IF : IFs) {
118 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
119
120 llvm::DenseSet<StringRef> DummyGroups;
121 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000122 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000123 }
124}
125
Rafael Espindola0e604f92015-09-25 18:56:53 +0000126template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000127DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
128 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000129 return cast<DefinedRegular<ELFT>>(
130 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000131}
132
Rui Ueyamac9559d92016-01-05 20:47:37 +0000133// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000134// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000135template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000136DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
137 uint8_t Visibility) {
138 if (!find(Name))
139 return nullptr;
140 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000141}
142
Rui Ueyamadeb15402016-01-07 17:20:07 +0000143// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
144// Used to implement --wrap.
145template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000146 SymbolBody *B = find(Name);
147 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000148 return;
149 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000150 Symbol *Sym = B->symbol();
151 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
152 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
153 // We rename symbols by replacing the old symbol's SymbolBody with the new
154 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
155 // old symbol to instead refer to the new symbol.
156 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
157 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000158}
159
Peter Collingbournedadcc172016-04-22 18:42:48 +0000160static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
161 if (VA == STV_DEFAULT)
162 return VB;
163 if (VB == STV_DEFAULT)
164 return VA;
165 return std::min(VA, VB);
166}
167
George Rimar9fc1d4e2016-06-29 08:36:36 +0000168// A symbol version may be included in a symbol name as a suffix after '@'.
George Rimar43651582016-06-28 08:21:10 +0000169// This function parses that part and returns a version ID number.
170static uint16_t getVersionId(Symbol *Sym, StringRef Name) {
171 size_t VersionBegin = Name.find('@');
172 if (VersionBegin == StringRef::npos)
173 return Config->VersionScriptGlobalByDefault ? VER_NDX_GLOBAL
174 : VER_NDX_LOCAL;
175
176 // If symbol name contains '@' or '@@' we can assign its version id right
George Rimar9fc1d4e2016-06-29 08:36:36 +0000177 // here. '@@' means the default version. It is usually the most recent one.
George Rimar43651582016-06-28 08:21:10 +0000178 // VERSYM_HIDDEN flag should be set for all non-default versions.
179 StringRef Version = Name.drop_front(VersionBegin + 1);
180 bool Default = Version.startswith("@");
181 if (Default)
182 Version = Version.drop_front();
183
184 size_t I = 2;
185 for (elf::Version &V : Config->SymbolVersions) {
186 if (V.Name == Version)
187 return Default ? I : (I | VERSYM_HIDDEN);
188 ++I;
189 }
190 error("symbol " + Name + " has undefined version " + Version);
191 return 0;
192}
193
Rui Ueyamab4de5952016-01-08 22:01:33 +0000194// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000195template <class ELFT>
196std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000197 unsigned NumSyms = SymVector.size();
198 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
199 Symbol *Sym;
200 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000201 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000202 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000203 Sym->Visibility = STV_DEFAULT;
204 Sym->IsUsedInRegularObj = false;
205 Sym->ExportDynamic = false;
George Rimar43651582016-06-28 08:21:10 +0000206 Sym->VersionId = getVersionId(Sym, Name);
207 Sym->VersionedName =
208 Sym->VersionId != VER_NDX_LOCAL && Sym->VersionId != VER_NDX_GLOBAL;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000209 SymVector.push_back(Sym);
210 } else {
211 Sym = SymVector[P.first->second];
212 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000213 return {Sym, P.second};
214}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000215
Peter Collingbourne4f952702016-05-01 04:55:03 +0000216// Find an existing symbol or create and insert a new one, then apply the given
217// attributes.
218template <class ELFT>
219std::pair<Symbol *, bool>
220SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
221 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
222 InputFile *File) {
223 Symbol *S;
224 bool WasInserted;
225 std::tie(S, WasInserted) = insert(Name);
226
227 // Merge in the new symbol's visibility.
228 S->Visibility = getMinVisibility(S->Visibility, Visibility);
229 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
230 S->ExportDynamic = true;
231 if (IsUsedInRegularObj)
232 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000233 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
234 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000235 error("TLS attribute mismatch for symbol: " +
236 conflictMsg(S->body(), File));
237
238 return {S, WasInserted};
239}
240
241// Construct a string in the form of "Sym in File1 and File2".
242// Used to construct an error message.
243template <typename ELFT>
244std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
245 InputFile *NewFile) {
246 StringRef Sym = Existing->getName();
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000247 return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) +
248 " and " + getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000249}
250
251template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
252 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000253 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000254}
255
256template <class ELFT>
257Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
258 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000259 bool CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000260 InputFile *File) {
261 Symbol *S;
262 bool WasInserted;
263 std::tie(S, WasInserted) =
Rafael Espindolacc70da32016-06-15 17:56:10 +0000264 insert(Name, Type, StOther & 3, CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000265 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
266 if (WasInserted) {
267 S->Binding = Binding;
268 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000269 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000270 return S;
271 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000272 if (Binding != STB_WEAK) {
273 if (S->body()->isShared() || S->body()->isLazy())
274 S->Binding = Binding;
275 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
276 SS->File->IsUsed = true;
277 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000278 if (auto *L = dyn_cast<Lazy>(S->body())) {
279 // An undefined weak will not fetch archive members, but we have to remember
280 // its type. See also comment in addLazyArchive.
281 if (S->isWeak())
282 L->Type = Type;
283 else if (auto F = L->getFile())
284 addFile(std::move(F));
285 }
286 return S;
287}
288
289// We have a new defined symbol with the specified binding. Return 1 if the new
290// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
291// strong defined symbols.
292static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
293 if (WasInserted)
294 return 1;
295 SymbolBody *Body = S->body();
296 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
297 return 1;
298 if (Binding == STB_WEAK)
299 return -1;
300 if (S->isWeak())
301 return 1;
302 return 0;
303}
304
305// We have a new non-common defined symbol with the specified binding. Return 1
306// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
307// is a conflict. If the new symbol wins, also update the binding.
308static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
309 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
310 if (Cmp > 0)
311 S->Binding = Binding;
312 return Cmp;
313 }
314 if (isa<DefinedCommon>(S->body())) {
315 // Non-common symbols take precedence over common symbols.
316 if (Config->WarnCommon)
317 warning("common " + S->body()->getName() + " is overridden");
318 return 1;
319 }
320 return 0;
321}
322
323template <class ELFT>
324Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
325 uint64_t Alignment, uint8_t Binding,
326 uint8_t StOther, uint8_t Type,
327 InputFile *File) {
328 Symbol *S;
329 bool WasInserted;
330 std::tie(S, WasInserted) =
331 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
332 /*IsUsedInRegularObj*/ true, File);
333 int Cmp = compareDefined(S, WasInserted, Binding);
334 if (Cmp > 0) {
335 S->Binding = Binding;
336 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
337 } else if (Cmp == 0) {
338 auto *C = dyn_cast<DefinedCommon>(S->body());
339 if (!C) {
340 // Non-common symbols take precedence over common symbols.
341 if (Config->WarnCommon)
342 warning("common " + S->body()->getName() + " is overridden");
343 return S;
344 }
345
346 if (Config->WarnCommon)
347 warning("multiple common of " + S->body()->getName());
348
349 C->Size = std::max(C->Size, Size);
350 C->Alignment = std::max(C->Alignment, Alignment);
351 }
352 return S;
353}
354
355template <class ELFT>
356void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
357 InputFile *NewFile) {
358 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
359 if (Config->AllowMultipleDefinition)
360 warning(Msg);
361 else
362 error(Msg);
363}
364
365template <typename ELFT>
366Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
367 InputSectionBase<ELFT> *Section) {
368 Symbol *S;
369 bool WasInserted;
370 std::tie(S, WasInserted) =
371 insert(Name, Sym.getType(), Sym.getVisibility(),
372 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
373 Section ? Section->getFile() : nullptr);
374 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
375 if (Cmp > 0)
376 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
377 else if (Cmp == 0)
378 reportDuplicate(S->body(), Section->getFile());
379 return S;
380}
381
382template <typename ELFT>
383Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
384 uint8_t StOther) {
385 Symbol *S;
386 bool WasInserted;
387 std::tie(S, WasInserted) =
388 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
389 /*IsUsedInRegularObj*/ true, nullptr);
390 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
391 if (Cmp > 0)
392 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
393 else if (Cmp == 0)
394 reportDuplicate(S->body(), nullptr);
395 return S;
396}
397
398template <typename ELFT>
399Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000400 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000401 uintX_t Value) {
402 Symbol *S;
403 bool WasInserted;
404 std::tie(S, WasInserted) =
405 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
406 /*IsUsedInRegularObj*/ true, nullptr);
407 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
408 if (Cmp > 0)
409 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
410 else if (Cmp == 0)
411 reportDuplicate(S->body(), nullptr);
412 return S;
413}
414
415template <typename ELFT>
416void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
417 const Elf_Sym &Sym,
418 const typename ELFT::Verdef *Verdef) {
419 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
420 // as the visibility, which will leave the visibility in the symbol table
421 // unchanged.
422 Symbol *S;
423 bool WasInserted;
424 std::tie(S, WasInserted) =
425 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
426 /*IsUsedInRegularObj*/ false, F);
427 // Make sure we preempt DSO symbols with default visibility.
428 if (Sym.getVisibility() == STV_DEFAULT)
429 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000430 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000431 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000432 if (!S->isWeak())
433 F->IsUsed = true;
434 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000435}
436
437template <class ELFT>
438Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
439 uint8_t StOther, uint8_t Type,
440 bool CanOmitFromDynSym, BitcodeFile *F) {
441 Symbol *S;
442 bool WasInserted;
443 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
444 /*IsUsedInRegularObj*/ false, F);
445 int Cmp =
446 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
447 if (Cmp > 0)
448 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
449 else if (Cmp == 0)
450 reportDuplicate(S->body(), F);
451 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000452}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000453
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000454template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
455 auto It = Symtab.find(Name);
456 if (It == Symtab.end())
457 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000458 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000459}
460
Rui Ueyama48e42512016-06-29 04:47:39 +0000461// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000462template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000463std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
464 // Fast-path. Fallback to find() if Pattern doesn't contain any wildcard
465 // characters.
Rui Ueyama48e42512016-06-29 04:47:39 +0000466 if (Pattern.find_first_of("?*") == StringRef::npos) {
467 if (SymbolBody *B = find(Pattern))
468 if (!B->isUndefined())
469 return {B};
470 return {};
471 }
Davide Italiano8e1131d2016-06-29 02:46:51 +0000472
Rui Ueyama48e42512016-06-29 04:47:39 +0000473 std::vector<SymbolBody *> Res;
474 for (auto &It : Symtab) {
475 StringRef Name = It.first.Val;
476 SymbolBody *B = SymVector[It.second]->body();
Rui Ueyama722830a2016-06-29 05:32:09 +0000477 if (!B->isUndefined() && globMatch(Pattern, Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000478 Res.push_back(B);
479 }
480 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000481}
482
483template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000484void SymbolTable<ELFT>::addLazyArchive(
485 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
486 Symbol *S;
487 bool WasInserted;
488 std::tie(S, WasInserted) = insert(Sym.getName());
489 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000490 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000491 return;
492 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000493 if (!S->body()->isUndefined())
494 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000495
Peter Collingbourne4f952702016-05-01 04:55:03 +0000496 // Weak undefined symbols should not fetch members from archives. If we were
497 // to keep old symbol we would not know that an archive member was available
498 // if a strong undefined symbol shows up afterwards in the link. If a strong
499 // undefined symbol never shows up, this lazy symbol will get to the end of
500 // the link and must be treated as the weak undefined one. We already marked
501 // this symbol as used when we added it to the symbol table, but we also need
502 // to preserve its type. FIXME: Move the Type field to Symbol.
503 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000504 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000505 return;
506 }
507 MemoryBufferRef MBRef = F->getMember(&Sym);
508 if (!MBRef.getBuffer().empty())
509 addFile(createObjectFile(MBRef, F->getName()));
510}
511
512template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000513void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000514 Symbol *S;
515 bool WasInserted;
516 std::tie(S, WasInserted) = insert(Name);
517 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000518 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000519 return;
520 }
521 if (!S->body()->isUndefined())
522 return;
523
524 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000525 if (S->isWeak()) {
526 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
527 } else {
528 MemoryBufferRef MBRef = Obj.getBuffer();
529 if (!MBRef.getBuffer().empty())
530 addFile(createObjectFile(MBRef));
531 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000532}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000533
Peter Collingbourne892d49802016-04-27 00:05:03 +0000534// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000535template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000536 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000537 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
538 if (std::unique_ptr<InputFile> File = L->getFile())
539 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000540}
541
Rui Ueyama93bfee52015-10-13 18:10:33 +0000542// This function takes care of the case in which shared libraries depend on
543// the user program (not the other way, which is usual). Shared libraries
544// may have undefined symbols, expecting that the user program provides
545// the definitions for them. An example is BSD's __progname symbol.
546// We need to put such symbols to the main program's .dynsym so that
547// shared libraries can find them.
548// Except this, we ignore undefined symbols in DSOs.
549template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000550 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
551 for (StringRef U : File->getUndefinedSymbols())
552 if (SymbolBody *Sym = find(U))
553 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000554 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000555}
556
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000557// This function process the dynamic list option by marking all the symbols
558// to be exported in the dynamic table.
559template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
560 for (StringRef S : Config->DynamicList)
561 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000562 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000563}
564
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000565// This function processes the --version-script option by marking all global
566// symbols with the VersionScriptGlobal flag, which acts as a filter on the
567// dynamic symbol table.
568template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000569 // If version script does not contain versions declarations,
570 // we just should mark global symbols.
571 if (!Config->VersionScriptGlobals.empty()) {
572 for (StringRef S : Config->VersionScriptGlobals)
573 if (SymbolBody *B = find(S))
574 B->symbol()->VersionId = VER_NDX_GLOBAL;
575 return;
576 }
577
578 // If we have symbols version declarations, we should
579 // assign version references for each symbol.
580 size_t I = 2;
581 for (Version &V : Config->SymbolVersions) {
George Rimar36b2c0a2016-06-28 08:07:26 +0000582 for (StringRef Name : V.Globals) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000583 std::vector<SymbolBody *> Syms = findAll(Name);
584 if (Syms.empty()) {
585 if (Config->NoUndefinedVersion)
586 error("version script assignment of " + V.Name + " to symbol " +
587 Name + " failed: symbol not defined");
588 continue;
589 }
George Rimar36b2c0a2016-06-28 08:07:26 +0000590
Rui Ueyama48e42512016-06-29 04:47:39 +0000591 for (SymbolBody *B : Syms) {
Davide Italiano8e1131d2016-06-29 02:46:51 +0000592 if (B->symbol()->VersionId != VER_NDX_GLOBAL &&
593 B->symbol()->VersionId != VER_NDX_LOCAL)
594 warning("duplicate symbol " + Name + " in version script");
595 B->symbol()->VersionId = I;
596 }
George Rimar36b2c0a2016-06-28 08:07:26 +0000597 }
George Rimard3566302016-06-20 11:55:12 +0000598 ++I;
599 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000600}
601
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000602// Print the module names which define the notified
603// symbols provided through -y or --trace-symbol option.
604template <class ELFT> void SymbolTable<ELFT>::traceDefined() {
605 for (const auto &Symbol : Config->TraceSymbol)
606 if (SymbolBody *B = find(Symbol.getKey()))
607 if (B->isDefined() || B->isCommon())
608 if (InputFile *File = B->getSourceFile<ELFT>())
609 outs() << getFilename(File) << ": definition of "
610 << B->getName() << "\n";
611}
612
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000613template class elf::SymbolTable<ELF32LE>;
614template class elf::SymbolTable<ELF32BE>;
615template class elf::SymbolTable<ELF64LE>;
616template class elf::SymbolTable<ELF64BE>;