blob: 2474bb35a548638dbbed7cd1aaa680a8e7b39f61 [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"
George Rimar7899d482016-07-12 07:44:40 +000022#include "SymbolListFile.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023#include "Symbols.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000024#include "llvm/Bitcode/ReaderWriter.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000025#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000028using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000029using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
31using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000032using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000033
Rui Ueyamac9559d92016-01-05 20:47:37 +000034// All input object files must be for the same architecture
35// (e.g. it does not make sense to link x86 object files with
36// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000037template <class ELFT> static bool isCompatible(InputFile *F) {
38 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(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
George Rimar7899d482016-07-12 07:44:40 +0000184 for (elf::Version &V : Config->SymbolVersions)
George Rimar43651582016-06-28 08:21:10 +0000185 if (V.Name == Version)
George Rimar7899d482016-07-12 07:44:40 +0000186 return Default ? V.Id : (V.Id | VERSYM_HIDDEN);
187
George Rimarc61bcd82016-07-08 06:47:28 +0000188
189 // If we are not building shared and version script
190 // is not specified, then it is not a error, it is
191 // in common not to use script for linking executables.
192 // In this case we just create new version.
193 if (!Config->Shared && !Config->HasVersionScript) {
George Rimar7899d482016-07-12 07:44:40 +0000194 size_t Id = defineSymbolVersion(Version);
195 return Default ? Id : (Id | VERSYM_HIDDEN);
George Rimarc61bcd82016-07-08 06:47:28 +0000196 }
197
George Rimar43651582016-06-28 08:21:10 +0000198 error("symbol " + Name + " has undefined version " + Version);
199 return 0;
200}
201
Rui Ueyamab4de5952016-01-08 22:01:33 +0000202// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000203template <class ELFT>
204std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000205 unsigned NumSyms = SymVector.size();
206 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
207 Symbol *Sym;
208 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000209 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000210 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000211 Sym->Visibility = STV_DEFAULT;
212 Sym->IsUsedInRegularObj = false;
213 Sym->ExportDynamic = false;
George Rimar43651582016-06-28 08:21:10 +0000214 Sym->VersionId = getVersionId(Sym, Name);
215 Sym->VersionedName =
216 Sym->VersionId != VER_NDX_LOCAL && Sym->VersionId != VER_NDX_GLOBAL;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000217 SymVector.push_back(Sym);
218 } else {
219 Sym = SymVector[P.first->second];
220 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000221 return {Sym, P.second};
222}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000223
Peter Collingbourne4f952702016-05-01 04:55:03 +0000224// Find an existing symbol or create and insert a new one, then apply the given
225// attributes.
226template <class ELFT>
227std::pair<Symbol *, bool>
228SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
229 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
230 InputFile *File) {
231 Symbol *S;
232 bool WasInserted;
233 std::tie(S, WasInserted) = insert(Name);
234
235 // Merge in the new symbol's visibility.
236 S->Visibility = getMinVisibility(S->Visibility, Visibility);
237 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
238 S->ExportDynamic = true;
239 if (IsUsedInRegularObj)
240 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000241 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
242 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000243 error("TLS attribute mismatch for symbol: " +
244 conflictMsg(S->body(), File));
245
246 return {S, WasInserted};
247}
248
249// Construct a string in the form of "Sym in File1 and File2".
250// Used to construct an error message.
251template <typename ELFT>
252std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
253 InputFile *NewFile) {
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000254 std::string Sym = Existing->getName();
255 if (Config->Demangle)
256 Sym = demangle(Sym);
257 return Sym + " in " + getFilename(Existing->getSourceFile<ELFT>()) + " and " +
258 getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000259}
260
261template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
262 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000263 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000264}
265
266template <class ELFT>
267Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
268 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000269 bool CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000270 InputFile *File) {
271 Symbol *S;
272 bool WasInserted;
273 std::tie(S, WasInserted) =
Rafael Espindolacc70da32016-06-15 17:56:10 +0000274 insert(Name, Type, StOther & 3, CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000275 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
276 if (WasInserted) {
277 S->Binding = Binding;
278 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000279 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000280 return S;
281 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000282 if (Binding != STB_WEAK) {
283 if (S->body()->isShared() || S->body()->isLazy())
284 S->Binding = Binding;
285 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
286 SS->File->IsUsed = true;
287 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000288 if (auto *L = dyn_cast<Lazy>(S->body())) {
289 // An undefined weak will not fetch archive members, but we have to remember
290 // its type. See also comment in addLazyArchive.
291 if (S->isWeak())
292 L->Type = Type;
293 else if (auto F = L->getFile())
294 addFile(std::move(F));
295 }
296 return S;
297}
298
299// We have a new defined symbol with the specified binding. Return 1 if the new
300// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
301// strong defined symbols.
302static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
303 if (WasInserted)
304 return 1;
305 SymbolBody *Body = S->body();
306 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
307 return 1;
308 if (Binding == STB_WEAK)
309 return -1;
310 if (S->isWeak())
311 return 1;
312 return 0;
313}
314
315// We have a new non-common defined symbol with the specified binding. Return 1
316// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
317// is a conflict. If the new symbol wins, also update the binding.
318static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
319 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
320 if (Cmp > 0)
321 S->Binding = Binding;
322 return Cmp;
323 }
324 if (isa<DefinedCommon>(S->body())) {
325 // Non-common symbols take precedence over common symbols.
326 if (Config->WarnCommon)
327 warning("common " + S->body()->getName() + " is overridden");
328 return 1;
329 }
330 return 0;
331}
332
333template <class ELFT>
334Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
335 uint64_t Alignment, uint8_t Binding,
336 uint8_t StOther, uint8_t Type,
337 InputFile *File) {
338 Symbol *S;
339 bool WasInserted;
340 std::tie(S, WasInserted) =
341 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
342 /*IsUsedInRegularObj*/ true, File);
343 int Cmp = compareDefined(S, WasInserted, Binding);
344 if (Cmp > 0) {
345 S->Binding = Binding;
346 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
347 } else if (Cmp == 0) {
348 auto *C = dyn_cast<DefinedCommon>(S->body());
349 if (!C) {
350 // Non-common symbols take precedence over common symbols.
351 if (Config->WarnCommon)
352 warning("common " + S->body()->getName() + " is overridden");
353 return S;
354 }
355
356 if (Config->WarnCommon)
357 warning("multiple common of " + S->body()->getName());
358
359 C->Size = std::max(C->Size, Size);
360 C->Alignment = std::max(C->Alignment, Alignment);
361 }
362 return S;
363}
364
365template <class ELFT>
366void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
367 InputFile *NewFile) {
368 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
369 if (Config->AllowMultipleDefinition)
370 warning(Msg);
371 else
372 error(Msg);
373}
374
375template <typename ELFT>
376Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
377 InputSectionBase<ELFT> *Section) {
378 Symbol *S;
379 bool WasInserted;
380 std::tie(S, WasInserted) =
381 insert(Name, Sym.getType(), Sym.getVisibility(),
382 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
383 Section ? Section->getFile() : nullptr);
384 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
385 if (Cmp > 0)
386 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
387 else if (Cmp == 0)
388 reportDuplicate(S->body(), Section->getFile());
389 return S;
390}
391
392template <typename ELFT>
393Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
394 uint8_t StOther) {
395 Symbol *S;
396 bool WasInserted;
397 std::tie(S, WasInserted) =
398 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
399 /*IsUsedInRegularObj*/ true, nullptr);
400 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
401 if (Cmp > 0)
402 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
403 else if (Cmp == 0)
404 reportDuplicate(S->body(), nullptr);
405 return S;
406}
407
408template <typename ELFT>
409Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000410 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000411 uintX_t Value) {
412 Symbol *S;
413 bool WasInserted;
414 std::tie(S, WasInserted) =
415 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
416 /*IsUsedInRegularObj*/ true, nullptr);
417 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
418 if (Cmp > 0)
419 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
420 else if (Cmp == 0)
421 reportDuplicate(S->body(), nullptr);
422 return S;
423}
424
425template <typename ELFT>
426void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
427 const Elf_Sym &Sym,
428 const typename ELFT::Verdef *Verdef) {
429 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
430 // as the visibility, which will leave the visibility in the symbol table
431 // unchanged.
432 Symbol *S;
433 bool WasInserted;
434 std::tie(S, WasInserted) =
435 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
436 /*IsUsedInRegularObj*/ false, F);
437 // Make sure we preempt DSO symbols with default visibility.
438 if (Sym.getVisibility() == STV_DEFAULT)
439 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000440 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000441 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000442 if (!S->isWeak())
443 F->IsUsed = true;
444 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000445}
446
447template <class ELFT>
448Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
449 uint8_t StOther, uint8_t Type,
450 bool CanOmitFromDynSym, BitcodeFile *F) {
451 Symbol *S;
452 bool WasInserted;
453 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
454 /*IsUsedInRegularObj*/ false, F);
455 int Cmp =
456 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
457 if (Cmp > 0)
458 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
459 else if (Cmp == 0)
460 reportDuplicate(S->body(), F);
461 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000462}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000463
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000464template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
465 auto It = Symtab.find(Name);
466 if (It == Symtab.end())
467 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000468 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000469}
470
Rui Ueyama48e42512016-06-29 04:47:39 +0000471// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000472template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000473std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000474 std::vector<SymbolBody *> Res;
475 for (auto &It : Symtab) {
476 StringRef Name = It.first.Val;
477 SymbolBody *B = SymVector[It.second]->body();
Rui Ueyama722830a2016-06-29 05:32:09 +0000478 if (!B->isUndefined() && globMatch(Pattern, Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000479 Res.push_back(B);
480 }
481 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000482}
483
484template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000485void SymbolTable<ELFT>::addLazyArchive(
486 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
487 Symbol *S;
488 bool WasInserted;
489 std::tie(S, WasInserted) = insert(Sym.getName());
490 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000491 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000492 return;
493 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000494 if (!S->body()->isUndefined())
495 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000496
Peter Collingbourne4f952702016-05-01 04:55:03 +0000497 // Weak undefined symbols should not fetch members from archives. If we were
498 // to keep old symbol we would not know that an archive member was available
499 // if a strong undefined symbol shows up afterwards in the link. If a strong
500 // undefined symbol never shows up, this lazy symbol will get to the end of
501 // the link and must be treated as the weak undefined one. We already marked
502 // this symbol as used when we added it to the symbol table, but we also need
503 // to preserve its type. FIXME: Move the Type field to Symbol.
504 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000505 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000506 return;
507 }
508 MemoryBufferRef MBRef = F->getMember(&Sym);
509 if (!MBRef.getBuffer().empty())
510 addFile(createObjectFile(MBRef, F->getName()));
511}
512
513template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000514void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515 Symbol *S;
516 bool WasInserted;
517 std::tie(S, WasInserted) = insert(Name);
518 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000519 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000520 return;
521 }
522 if (!S->body()->isUndefined())
523 return;
524
525 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000526 if (S->isWeak()) {
527 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
528 } else {
529 MemoryBufferRef MBRef = Obj.getBuffer();
530 if (!MBRef.getBuffer().empty())
531 addFile(createObjectFile(MBRef));
532 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000533}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000534
Peter Collingbourne892d49802016-04-27 00:05:03 +0000535// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000537 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000538 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
539 if (std::unique_ptr<InputFile> File = L->getFile())
540 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000541}
542
Rui Ueyama93bfee52015-10-13 18:10:33 +0000543// This function takes care of the case in which shared libraries depend on
544// the user program (not the other way, which is usual). Shared libraries
545// may have undefined symbols, expecting that the user program provides
546// the definitions for them. An example is BSD's __progname symbol.
547// We need to put such symbols to the main program's .dynsym so that
548// shared libraries can find them.
549// Except this, we ignore undefined symbols in DSOs.
550template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000551 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
552 for (StringRef U : File->getUndefinedSymbols())
553 if (SymbolBody *Sym = find(U))
554 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000555 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000556}
557
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000558// This function process the dynamic list option by marking all the symbols
559// to be exported in the dynamic table.
560template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
561 for (StringRef S : Config->DynamicList)
562 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000563 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000564}
565
George Rimarf73a2582016-07-07 07:45:27 +0000566static bool hasWildcard(StringRef S) {
567 return S.find_first_of("?*") != StringRef::npos;
568}
569
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000570// This function processes the --version-script option by marking all global
571// symbols with the VersionScriptGlobal flag, which acts as a filter on the
572// dynamic symbol table.
573template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000574 // If version script does not contain versions declarations,
575 // we just should mark global symbols.
576 if (!Config->VersionScriptGlobals.empty()) {
George Rimardd64bb32016-07-13 08:19:04 +0000577 for (StringRef S : Config->VersionScriptGlobals)
578 if (SymbolBody *B = find(S))
George Rimard3566302016-06-20 11:55:12 +0000579 B->symbol()->VersionId = VER_NDX_GLOBAL;
580 return;
581 }
582
George Rimarf73a2582016-07-07 07:45:27 +0000583 if (Config->SymbolVersions.empty())
584 return;
585
George Rimard3566302016-06-20 11:55:12 +0000586 // If we have symbols version declarations, we should
587 // assign version references for each symbol.
George Rimarf73a2582016-07-07 07:45:27 +0000588 // Current rules are:
George Rimardd64bb32016-07-13 08:19:04 +0000589 // * If there is an exact match for the mangled name, we use it.
George Rimarf73a2582016-07-07 07:45:27 +0000590 // * Otherwise, we look through the wildcard patterns. We look through the
591 // version tags in reverse order. We use the first match we find (the last
592 // matching version tag in the file).
George Rimardd64bb32016-07-13 08:19:04 +0000593 for (size_t I = 0, E = Config->SymbolVersions.size(); I < E; ++I) {
594 Version &V = Config->SymbolVersions[I];
595 for (StringRef Name : V.Globals) {
596 if (hasWildcard(Name))
George Rimare05103e2016-07-13 07:46:00 +0000597 continue;
George Rimardd64bb32016-07-13 08:19:04 +0000598
599 SymbolBody *B = find(Name);
600 if (!B || B->isUndefined()) {
601 if (Config->NoUndefinedVersion)
602 error("version script assignment of " + V.Name + " to symbol " +
603 Name + " failed: symbol not defined");
604 continue;
605 }
606
607 if (B->symbol()->VersionId != VER_NDX_GLOBAL &&
608 B->symbol()->VersionId != VER_NDX_LOCAL)
609 warning("duplicate symbol " + Name + " in version script");
610 B->symbol()->VersionId = V.Id;
George Rimar36b2c0a2016-06-28 08:07:26 +0000611 }
George Rimarf73a2582016-07-07 07:45:27 +0000612 }
613
614 for (size_t I = Config->SymbolVersions.size() - 1; I != (size_t)-1; --I) {
615 Version &V = Config->SymbolVersions[I];
George Rimardd64bb32016-07-13 08:19:04 +0000616 for (StringRef Name : V.Globals) {
617 if (!hasWildcard(Name))
George Rimarf73a2582016-07-07 07:45:27 +0000618 continue;
619
George Rimardd64bb32016-07-13 08:19:04 +0000620 for (SymbolBody *B : findAll(Name))
George Rimarf73a2582016-07-07 07:45:27 +0000621 if (B->symbol()->VersionId == VER_NDX_GLOBAL ||
622 B->symbol()->VersionId == VER_NDX_LOCAL)
George Rimar7899d482016-07-12 07:44:40 +0000623 B->symbol()->VersionId = V.Id;
George Rimarf73a2582016-07-07 07:45:27 +0000624 }
George Rimard3566302016-06-20 11:55:12 +0000625 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000626}
627
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000628// Print the module names which define the notified
629// symbols provided through -y or --trace-symbol option.
630template <class ELFT> void SymbolTable<ELFT>::traceDefined() {
631 for (const auto &Symbol : Config->TraceSymbol)
632 if (SymbolBody *B = find(Symbol.getKey()))
633 if (B->isDefined() || B->isCommon())
634 if (InputFile *File = B->getSourceFile<ELFT>())
635 outs() << getFilename(File) << ": definition of "
636 << B->getName() << "\n";
637}
638
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000639template class elf::SymbolTable<ELF32LE>;
640template class elf::SymbolTable<ELF32BE>;
641template class elf::SymbolTable<ELF64LE>;
642template class elf::SymbolTable<ELF64BE>;