blob: eccf35cf0fcd013047b010e38c3f29c6276c215d [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)
Rui Ueyama25068662016-07-16 03:08:26 +0000173 return Config->DefaultSymbolVersion;
George Rimar43651582016-06-28 08:21:10 +0000174
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
George Rimar7899d482016-07-12 07:44:40 +0000183 for (elf::Version &V : Config->SymbolVersions)
George Rimar43651582016-06-28 08:21:10 +0000184 if (V.Name == Version)
George Rimar7899d482016-07-12 07:44:40 +0000185 return Default ? V.Id : (V.Id | VERSYM_HIDDEN);
186
George Rimarc61bcd82016-07-08 06:47:28 +0000187
188 // If we are not building shared and version script
189 // is not specified, then it is not a error, it is
190 // in common not to use script for linking executables.
191 // In this case we just create new version.
192 if (!Config->Shared && !Config->HasVersionScript) {
George Rimar7899d482016-07-12 07:44:40 +0000193 size_t Id = defineSymbolVersion(Version);
194 return Default ? Id : (Id | VERSYM_HIDDEN);
George Rimarc61bcd82016-07-08 06:47:28 +0000195 }
196
George Rimar43651582016-06-28 08:21:10 +0000197 error("symbol " + Name + " has undefined version " + Version);
198 return 0;
199}
200
Rui Ueyamab4de5952016-01-08 22:01:33 +0000201// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000202template <class ELFT>
203std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000204 unsigned NumSyms = SymVector.size();
205 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
206 Symbol *Sym;
207 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000208 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000209 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000210 Sym->Visibility = STV_DEFAULT;
211 Sym->IsUsedInRegularObj = false;
212 Sym->ExportDynamic = false;
George Rimar43651582016-06-28 08:21:10 +0000213 Sym->VersionId = getVersionId(Sym, Name);
214 Sym->VersionedName =
215 Sym->VersionId != VER_NDX_LOCAL && Sym->VersionId != VER_NDX_GLOBAL;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000216 SymVector.push_back(Sym);
217 } else {
218 Sym = SymVector[P.first->second];
219 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000220 return {Sym, P.second};
221}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000222
Peter Collingbourne4f952702016-05-01 04:55:03 +0000223// Find an existing symbol or create and insert a new one, then apply the given
224// attributes.
225template <class ELFT>
226std::pair<Symbol *, bool>
227SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
228 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
229 InputFile *File) {
230 Symbol *S;
231 bool WasInserted;
232 std::tie(S, WasInserted) = insert(Name);
233
234 // Merge in the new symbol's visibility.
235 S->Visibility = getMinVisibility(S->Visibility, Visibility);
236 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
237 S->ExportDynamic = true;
238 if (IsUsedInRegularObj)
239 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000240 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
241 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000242 error("TLS attribute mismatch for symbol: " +
243 conflictMsg(S->body(), File));
244
245 return {S, WasInserted};
246}
247
248// Construct a string in the form of "Sym in File1 and File2".
249// Used to construct an error message.
250template <typename ELFT>
251std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
252 InputFile *NewFile) {
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000253 std::string Sym = Existing->getName();
254 if (Config->Demangle)
255 Sym = demangle(Sym);
256 return Sym + " in " + getFilename(Existing->getSourceFile<ELFT>()) + " and " +
257 getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000258}
259
260template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
261 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000262 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000263}
264
265template <class ELFT>
266Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
267 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000268 bool CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000269 InputFile *File) {
270 Symbol *S;
271 bool WasInserted;
272 std::tie(S, WasInserted) =
Rafael Espindolacc70da32016-06-15 17:56:10 +0000273 insert(Name, Type, StOther & 3, CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000274 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
275 if (WasInserted) {
276 S->Binding = Binding;
277 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000278 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000279 return S;
280 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000281 if (Binding != STB_WEAK) {
282 if (S->body()->isShared() || S->body()->isLazy())
283 S->Binding = Binding;
284 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
285 SS->File->IsUsed = true;
286 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000287 if (auto *L = dyn_cast<Lazy>(S->body())) {
288 // An undefined weak will not fetch archive members, but we have to remember
289 // its type. See also comment in addLazyArchive.
290 if (S->isWeak())
291 L->Type = Type;
292 else if (auto F = L->getFile())
293 addFile(std::move(F));
294 }
295 return S;
296}
297
298// We have a new defined symbol with the specified binding. Return 1 if the new
299// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
300// strong defined symbols.
301static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
302 if (WasInserted)
303 return 1;
304 SymbolBody *Body = S->body();
305 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
306 return 1;
307 if (Binding == STB_WEAK)
308 return -1;
309 if (S->isWeak())
310 return 1;
311 return 0;
312}
313
314// We have a new non-common defined symbol with the specified binding. Return 1
315// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
316// is a conflict. If the new symbol wins, also update the binding.
317static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
318 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
319 if (Cmp > 0)
320 S->Binding = Binding;
321 return Cmp;
322 }
323 if (isa<DefinedCommon>(S->body())) {
324 // Non-common symbols take precedence over common symbols.
325 if (Config->WarnCommon)
326 warning("common " + S->body()->getName() + " is overridden");
327 return 1;
328 }
329 return 0;
330}
331
332template <class ELFT>
333Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
334 uint64_t Alignment, uint8_t Binding,
335 uint8_t StOther, uint8_t Type,
336 InputFile *File) {
337 Symbol *S;
338 bool WasInserted;
339 std::tie(S, WasInserted) =
340 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
341 /*IsUsedInRegularObj*/ true, File);
342 int Cmp = compareDefined(S, WasInserted, Binding);
343 if (Cmp > 0) {
344 S->Binding = Binding;
345 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
346 } else if (Cmp == 0) {
347 auto *C = dyn_cast<DefinedCommon>(S->body());
348 if (!C) {
349 // Non-common symbols take precedence over common symbols.
350 if (Config->WarnCommon)
351 warning("common " + S->body()->getName() + " is overridden");
352 return S;
353 }
354
355 if (Config->WarnCommon)
356 warning("multiple common of " + S->body()->getName());
357
358 C->Size = std::max(C->Size, Size);
359 C->Alignment = std::max(C->Alignment, Alignment);
360 }
361 return S;
362}
363
364template <class ELFT>
365void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
366 InputFile *NewFile) {
367 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
368 if (Config->AllowMultipleDefinition)
369 warning(Msg);
370 else
371 error(Msg);
372}
373
374template <typename ELFT>
375Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
376 InputSectionBase<ELFT> *Section) {
377 Symbol *S;
378 bool WasInserted;
379 std::tie(S, WasInserted) =
380 insert(Name, Sym.getType(), Sym.getVisibility(),
381 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
382 Section ? Section->getFile() : nullptr);
383 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
384 if (Cmp > 0)
385 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
386 else if (Cmp == 0)
387 reportDuplicate(S->body(), Section->getFile());
388 return S;
389}
390
391template <typename ELFT>
392Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
393 uint8_t StOther) {
394 Symbol *S;
395 bool WasInserted;
396 std::tie(S, WasInserted) =
397 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
398 /*IsUsedInRegularObj*/ true, nullptr);
399 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
400 if (Cmp > 0)
401 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
402 else if (Cmp == 0)
403 reportDuplicate(S->body(), nullptr);
404 return S;
405}
406
407template <typename ELFT>
408Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000409 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 uintX_t Value) {
411 Symbol *S;
412 bool WasInserted;
413 std::tie(S, WasInserted) =
414 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
415 /*IsUsedInRegularObj*/ true, nullptr);
416 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
417 if (Cmp > 0)
418 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
419 else if (Cmp == 0)
420 reportDuplicate(S->body(), nullptr);
421 return S;
422}
423
424template <typename ELFT>
425void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
426 const Elf_Sym &Sym,
427 const typename ELFT::Verdef *Verdef) {
428 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
429 // as the visibility, which will leave the visibility in the symbol table
430 // unchanged.
431 Symbol *S;
432 bool WasInserted;
433 std::tie(S, WasInserted) =
434 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
435 /*IsUsedInRegularObj*/ false, F);
436 // Make sure we preempt DSO symbols with default visibility.
437 if (Sym.getVisibility() == STV_DEFAULT)
438 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000439 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000440 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000441 if (!S->isWeak())
442 F->IsUsed = true;
443 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000444}
445
446template <class ELFT>
447Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
448 uint8_t StOther, uint8_t Type,
449 bool CanOmitFromDynSym, BitcodeFile *F) {
450 Symbol *S;
451 bool WasInserted;
452 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
453 /*IsUsedInRegularObj*/ false, F);
454 int Cmp =
455 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
456 if (Cmp > 0)
457 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
458 else if (Cmp == 0)
459 reportDuplicate(S->body(), F);
460 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000461}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000462
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000463template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
464 auto It = Symtab.find(Name);
465 if (It == Symtab.end())
466 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000467 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000468}
469
Rui Ueyama48e42512016-06-29 04:47:39 +0000470// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000471template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000472std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
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
George Rimarf73a2582016-07-07 07:45:27 +0000565static bool hasWildcard(StringRef S) {
566 return S.find_first_of("?*") != StringRef::npos;
567}
568
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000569// This function processes the --version-script option by marking all global
570// symbols with the VersionScriptGlobal flag, which acts as a filter on the
571// dynamic symbol table.
572template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000573 // If version script does not contain versions declarations,
574 // we just should mark global symbols.
575 if (!Config->VersionScriptGlobals.empty()) {
George Rimardd64bb32016-07-13 08:19:04 +0000576 for (StringRef S : Config->VersionScriptGlobals)
577 if (SymbolBody *B = find(S))
George Rimard3566302016-06-20 11:55:12 +0000578 B->symbol()->VersionId = VER_NDX_GLOBAL;
579 return;
580 }
581
George Rimarf73a2582016-07-07 07:45:27 +0000582 if (Config->SymbolVersions.empty())
583 return;
584
George Rimard3566302016-06-20 11:55:12 +0000585 // If we have symbols version declarations, we should
586 // assign version references for each symbol.
George Rimarf73a2582016-07-07 07:45:27 +0000587 // Current rules are:
George Rimardd64bb32016-07-13 08:19:04 +0000588 // * If there is an exact match for the mangled name, we use it.
George Rimarf73a2582016-07-07 07:45:27 +0000589 // * Otherwise, we look through the wildcard patterns. We look through the
590 // version tags in reverse order. We use the first match we find (the last
591 // matching version tag in the file).
George Rimardd64bb32016-07-13 08:19:04 +0000592 for (size_t I = 0, E = Config->SymbolVersions.size(); I < E; ++I) {
593 Version &V = Config->SymbolVersions[I];
594 for (StringRef Name : V.Globals) {
595 if (hasWildcard(Name))
George Rimare05103e2016-07-13 07:46:00 +0000596 continue;
George Rimardd64bb32016-07-13 08:19:04 +0000597
598 SymbolBody *B = find(Name);
599 if (!B || B->isUndefined()) {
600 if (Config->NoUndefinedVersion)
601 error("version script assignment of " + V.Name + " to symbol " +
602 Name + " failed: symbol not defined");
603 continue;
604 }
605
Rui Ueyama25068662016-07-16 03:08:26 +0000606 if (B->symbol()->VersionId != Config->DefaultSymbolVersion)
George Rimardd64bb32016-07-13 08:19:04 +0000607 warning("duplicate symbol " + Name + " in version script");
608 B->symbol()->VersionId = V.Id;
George Rimar36b2c0a2016-06-28 08:07:26 +0000609 }
George Rimarf73a2582016-07-07 07:45:27 +0000610 }
611
612 for (size_t I = Config->SymbolVersions.size() - 1; I != (size_t)-1; --I) {
613 Version &V = Config->SymbolVersions[I];
George Rimardd64bb32016-07-13 08:19:04 +0000614 for (StringRef Name : V.Globals) {
615 if (!hasWildcard(Name))
George Rimarf73a2582016-07-07 07:45:27 +0000616 continue;
617
George Rimardd64bb32016-07-13 08:19:04 +0000618 for (SymbolBody *B : findAll(Name))
Rui Ueyama25068662016-07-16 03:08:26 +0000619 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
George Rimar7899d482016-07-12 07:44:40 +0000620 B->symbol()->VersionId = V.Id;
George Rimarf73a2582016-07-07 07:45:27 +0000621 }
George Rimard3566302016-06-20 11:55:12 +0000622 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000623}
624
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000625// Print the module names which define the notified
626// symbols provided through -y or --trace-symbol option.
627template <class ELFT> void SymbolTable<ELFT>::traceDefined() {
628 for (const auto &Symbol : Config->TraceSymbol)
629 if (SymbolBody *B = find(Symbol.getKey()))
630 if (B->isDefined() || B->isCommon())
631 if (InputFile *File = B->getSourceFile<ELFT>())
632 outs() << getFilename(File) << ": definition of "
633 << B->getName() << "\n";
634}
635
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000636template class elf::SymbolTable<ELF32LE>;
637template class elf::SymbolTable<ELF32BE>;
638template class elf::SymbolTable<ELF64LE>;
639template class elf::SymbolTable<ELF64BE>;