blob: a0ebb0257c516c0fe013e78c84c47487aee61b71 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// Symbol table is a bag of all known symbols. We put all symbols of
Rui Ueyamac9559d92016-01-05 20:47:37 +000011// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000012// a hash table with the logic to resolve symbol name conflicts using
13// the symbol types.
14//
15//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000018#include "Config.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000019#include "Error.h"
Davide Italiano8e1131d2016-06-29 02:46:51 +000020#include "LinkerScript.h"
Rui Ueyama93c9af42016-06-29 08:01:32 +000021#include "Strings.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022#include "Symbols.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000023#include "llvm/Bitcode/ReaderWriter.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000024#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000027using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000028using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rui Ueyamac9559d92016-01-05 20:47:37 +000033// All input object files must be for the same architecture
34// (e.g. it does not make sense to link x86 object files with
35// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000036template <class ELFT> static bool isCompatible(InputFile *F) {
37 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000038 return true;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000039 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000040 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000041 StringRef A = F->getName();
42 StringRef B = Config->Emulation;
43 if (B.empty())
44 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000045 error(A + " is incompatible with " + B);
46 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000047}
48
Rui Ueyamac9559d92016-01-05 20:47:37 +000049// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000050template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000051void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000052 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000053 if (!isCompatible<ELFT>(FileP))
54 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000055
Rui Ueyama89575742015-12-16 22:59:13 +000056 // .a file
57 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000058 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000059 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000060 return;
61 }
Rui Ueyama3d451792015-10-12 18:03:21 +000062
George Rimar2a78fce2016-04-13 18:07:57 +000063 // Lazy object file
64 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
65 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000066 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000067 return;
68 }
69
70 if (Config->Trace)
71 llvm::outs() << getFilename(FileP) << "\n";
72
Rui Ueyama89575742015-12-16 22:59:13 +000073 // .so file
74 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
75 // DSOs are uniquified not by filename but by soname.
76 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000077 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000078 return;
Rui Ueyama89575742015-12-16 22:59:13 +000079
Rafael Espindola21f7bd42015-12-23 14:35:51 +000080 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000081 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000082 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000083 }
Rui Ueyama89575742015-12-16 22:59:13 +000084
Rui Ueyamaf8baa662016-04-07 19:24:51 +000085 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000086 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
87 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000088 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000089 return;
90 }
91
Rui Ueyamaf8baa662016-04-07 19:24:51 +000092 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +000093 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000094 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000095 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +000096}
97
Rui Ueyama42554752016-04-23 00:26:32 +000098// This function is where all the optimizations of link-time
99// optimization happens. When LTO is in use, some input files are
100// not in native object file format but in the LLVM bitcode format.
101// This function compiles bitcode files into a few big native files
102// using LLVM functions and replaces bitcode symbols with the results.
103// Because all bitcode files that consist of a program are passed
104// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000105template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
106 if (BitcodeFiles.empty())
107 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000108
109 // Compile bitcode files.
110 Lto.reset(new BitcodeCompiler);
111 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
112 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000113 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000114
115 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000116 for (auto &IF : IFs) {
117 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
118
119 llvm::DenseSet<StringRef> DummyGroups;
120 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000121 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000122 }
123}
124
Rafael Espindola0e604f92015-09-25 18:56:53 +0000125template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000126DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
127 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000128 return cast<DefinedRegular<ELFT>>(
129 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000130}
131
Rui Ueyamac9559d92016-01-05 20:47:37 +0000132// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000133// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000134template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000135DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
136 uint8_t Visibility) {
137 if (!find(Name))
138 return nullptr;
139 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000140}
141
Rui Ueyamadeb15402016-01-07 17:20:07 +0000142// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
143// Used to implement --wrap.
144template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000145 SymbolBody *B = find(Name);
146 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000147 return;
148 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000149 Symbol *Sym = B->symbol();
150 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
151 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
152 // We rename symbols by replacing the old symbol's SymbolBody with the new
153 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
154 // old symbol to instead refer to the new symbol.
155 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
156 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000157}
158
Peter Collingbournedadcc172016-04-22 18:42:48 +0000159static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
160 if (VA == STV_DEFAULT)
161 return VB;
162 if (VB == STV_DEFAULT)
163 return VA;
164 return std::min(VA, VB);
165}
166
George Rimar9fc1d4e2016-06-29 08:36:36 +0000167// A symbol version may be included in a symbol name as a suffix after '@'.
George Rimar43651582016-06-28 08:21:10 +0000168// This function parses that part and returns a version ID number.
169static uint16_t getVersionId(Symbol *Sym, StringRef Name) {
170 size_t VersionBegin = Name.find('@');
171 if (VersionBegin == StringRef::npos)
172 return Config->VersionScriptGlobalByDefault ? VER_NDX_GLOBAL
173 : VER_NDX_LOCAL;
174
175 // If symbol name contains '@' or '@@' we can assign its version id right
George Rimar9fc1d4e2016-06-29 08:36:36 +0000176 // here. '@@' means the default version. It is usually the most recent one.
George Rimar43651582016-06-28 08:21:10 +0000177 // VERSYM_HIDDEN flag should be set for all non-default versions.
178 StringRef Version = Name.drop_front(VersionBegin + 1);
179 bool Default = Version.startswith("@");
180 if (Default)
181 Version = Version.drop_front();
182
183 size_t I = 2;
184 for (elf::Version &V : Config->SymbolVersions) {
185 if (V.Name == Version)
186 return Default ? I : (I | VERSYM_HIDDEN);
187 ++I;
188 }
189 error("symbol " + Name + " has undefined version " + Version);
190 return 0;
191}
192
Rui Ueyamab4de5952016-01-08 22:01:33 +0000193// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000194template <class ELFT>
195std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000196 unsigned NumSyms = SymVector.size();
197 auto P = Symtab.insert(std::make_pair(Name, NumSyms));
198 Symbol *Sym;
199 if (P.second) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000200 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000201 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000202 Sym->Visibility = STV_DEFAULT;
203 Sym->IsUsedInRegularObj = false;
204 Sym->ExportDynamic = false;
George Rimar43651582016-06-28 08:21:10 +0000205 Sym->VersionId = getVersionId(Sym, Name);
206 Sym->VersionedName =
207 Sym->VersionId != VER_NDX_LOCAL && Sym->VersionId != VER_NDX_GLOBAL;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000208 SymVector.push_back(Sym);
209 } else {
210 Sym = SymVector[P.first->second];
211 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000212 return {Sym, P.second};
213}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000214
Peter Collingbourne4f952702016-05-01 04:55:03 +0000215// Find an existing symbol or create and insert a new one, then apply the given
216// attributes.
217template <class ELFT>
218std::pair<Symbol *, bool>
219SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
220 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
221 InputFile *File) {
222 Symbol *S;
223 bool WasInserted;
224 std::tie(S, WasInserted) = insert(Name);
225
226 // Merge in the new symbol's visibility.
227 S->Visibility = getMinVisibility(S->Visibility, Visibility);
228 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
229 S->ExportDynamic = true;
230 if (IsUsedInRegularObj)
231 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000232 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
233 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000234 error("TLS attribute mismatch for symbol: " +
235 conflictMsg(S->body(), File));
236
237 return {S, WasInserted};
238}
239
240// Construct a string in the form of "Sym in File1 and File2".
241// Used to construct an error message.
242template <typename ELFT>
243std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
244 InputFile *NewFile) {
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000245 std::string Sym = Existing->getName();
246 if (Config->Demangle)
247 Sym = demangle(Sym);
248 return Sym + " in " + getFilename(Existing->getSourceFile<ELFT>()) + " and " +
249 getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000250}
251
252template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
253 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000254 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000255}
256
257template <class ELFT>
258Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
259 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000260 bool CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000261 InputFile *File) {
262 Symbol *S;
263 bool WasInserted;
264 std::tie(S, WasInserted) =
Rafael Espindolacc70da32016-06-15 17:56:10 +0000265 insert(Name, Type, StOther & 3, CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000266 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
267 if (WasInserted) {
268 S->Binding = Binding;
269 replaceBody<Undefined>(S, Name, StOther, Type);
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000270 cast<Undefined>(S->body())->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000271 return S;
272 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000273 if (Binding != STB_WEAK) {
274 if (S->body()->isShared() || S->body()->isLazy())
275 S->Binding = Binding;
276 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
277 SS->File->IsUsed = true;
278 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000279 if (auto *L = dyn_cast<Lazy>(S->body())) {
280 // An undefined weak will not fetch archive members, but we have to remember
281 // its type. See also comment in addLazyArchive.
282 if (S->isWeak())
283 L->Type = Type;
284 else if (auto F = L->getFile())
285 addFile(std::move(F));
286 }
287 return S;
288}
289
290// We have a new defined symbol with the specified binding. Return 1 if the new
291// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
292// strong defined symbols.
293static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
294 if (WasInserted)
295 return 1;
296 SymbolBody *Body = S->body();
297 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
298 return 1;
299 if (Binding == STB_WEAK)
300 return -1;
301 if (S->isWeak())
302 return 1;
303 return 0;
304}
305
306// We have a new non-common defined symbol with the specified binding. Return 1
307// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
308// is a conflict. If the new symbol wins, also update the binding.
309static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
310 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
311 if (Cmp > 0)
312 S->Binding = Binding;
313 return Cmp;
314 }
315 if (isa<DefinedCommon>(S->body())) {
316 // Non-common symbols take precedence over common symbols.
317 if (Config->WarnCommon)
318 warning("common " + S->body()->getName() + " is overridden");
319 return 1;
320 }
321 return 0;
322}
323
324template <class ELFT>
325Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
326 uint64_t Alignment, uint8_t Binding,
327 uint8_t StOther, uint8_t Type,
328 InputFile *File) {
329 Symbol *S;
330 bool WasInserted;
331 std::tie(S, WasInserted) =
332 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
333 /*IsUsedInRegularObj*/ true, File);
334 int Cmp = compareDefined(S, WasInserted, Binding);
335 if (Cmp > 0) {
336 S->Binding = Binding;
337 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
338 } else if (Cmp == 0) {
339 auto *C = dyn_cast<DefinedCommon>(S->body());
340 if (!C) {
341 // Non-common symbols take precedence over common symbols.
342 if (Config->WarnCommon)
343 warning("common " + S->body()->getName() + " is overridden");
344 return S;
345 }
346
347 if (Config->WarnCommon)
348 warning("multiple common of " + S->body()->getName());
349
350 C->Size = std::max(C->Size, Size);
351 C->Alignment = std::max(C->Alignment, Alignment);
352 }
353 return S;
354}
355
356template <class ELFT>
357void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
358 InputFile *NewFile) {
359 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
360 if (Config->AllowMultipleDefinition)
361 warning(Msg);
362 else
363 error(Msg);
364}
365
366template <typename ELFT>
367Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
368 InputSectionBase<ELFT> *Section) {
369 Symbol *S;
370 bool WasInserted;
371 std::tie(S, WasInserted) =
372 insert(Name, Sym.getType(), Sym.getVisibility(),
373 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
374 Section ? Section->getFile() : nullptr);
375 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
376 if (Cmp > 0)
377 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
378 else if (Cmp == 0)
379 reportDuplicate(S->body(), Section->getFile());
380 return S;
381}
382
383template <typename ELFT>
384Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
385 uint8_t StOther) {
386 Symbol *S;
387 bool WasInserted;
388 std::tie(S, WasInserted) =
389 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
390 /*IsUsedInRegularObj*/ true, nullptr);
391 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
392 if (Cmp > 0)
393 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
394 else if (Cmp == 0)
395 reportDuplicate(S->body(), nullptr);
396 return S;
397}
398
399template <typename ELFT>
400Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000401 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000402 uintX_t Value) {
403 Symbol *S;
404 bool WasInserted;
405 std::tie(S, WasInserted) =
406 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
407 /*IsUsedInRegularObj*/ true, nullptr);
408 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
409 if (Cmp > 0)
410 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
411 else if (Cmp == 0)
412 reportDuplicate(S->body(), nullptr);
413 return S;
414}
415
416template <typename ELFT>
417void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
418 const Elf_Sym &Sym,
419 const typename ELFT::Verdef *Verdef) {
420 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
421 // as the visibility, which will leave the visibility in the symbol table
422 // unchanged.
423 Symbol *S;
424 bool WasInserted;
425 std::tie(S, WasInserted) =
426 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
427 /*IsUsedInRegularObj*/ false, F);
428 // Make sure we preempt DSO symbols with default visibility.
429 if (Sym.getVisibility() == STV_DEFAULT)
430 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000431 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000432 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000433 if (!S->isWeak())
434 F->IsUsed = true;
435 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000436}
437
438template <class ELFT>
439Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
440 uint8_t StOther, uint8_t Type,
441 bool CanOmitFromDynSym, BitcodeFile *F) {
442 Symbol *S;
443 bool WasInserted;
444 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
445 /*IsUsedInRegularObj*/ false, F);
446 int Cmp =
447 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
448 if (Cmp > 0)
449 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
450 else if (Cmp == 0)
451 reportDuplicate(S->body(), F);
452 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000453}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000454
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000455template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
456 auto It = Symtab.find(Name);
457 if (It == Symtab.end())
458 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000459 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000460}
461
Rui Ueyama48e42512016-06-29 04:47:39 +0000462// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000463template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000464std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000465 std::vector<SymbolBody *> Res;
466 for (auto &It : Symtab) {
467 StringRef Name = It.first.Val;
468 SymbolBody *B = SymVector[It.second]->body();
Rui Ueyama722830a2016-06-29 05:32:09 +0000469 if (!B->isUndefined() && globMatch(Pattern, Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000470 Res.push_back(B);
471 }
472 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000473}
474
475template <class ELFT>
Peter Collingbourne4f952702016-05-01 04:55:03 +0000476void SymbolTable<ELFT>::addLazyArchive(
477 ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
478 Symbol *S;
479 bool WasInserted;
480 std::tie(S, WasInserted) = insert(Sym.getName());
481 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000482 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000483 return;
484 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000485 if (!S->body()->isUndefined())
486 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000487
Peter Collingbourne4f952702016-05-01 04:55:03 +0000488 // Weak undefined symbols should not fetch members from archives. If we were
489 // to keep old symbol we would not know that an archive member was available
490 // if a strong undefined symbol shows up afterwards in the link. If a strong
491 // undefined symbol never shows up, this lazy symbol will get to the end of
492 // the link and must be treated as the weak undefined one. We already marked
493 // this symbol as used when we added it to the symbol table, but we also need
494 // to preserve its type. FIXME: Move the Type field to Symbol.
495 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000496 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000497 return;
498 }
499 MemoryBufferRef MBRef = F->getMember(&Sym);
500 if (!MBRef.getBuffer().empty())
501 addFile(createObjectFile(MBRef, F->getName()));
502}
503
504template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000505void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000506 Symbol *S;
507 bool WasInserted;
508 std::tie(S, WasInserted) = insert(Name);
509 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000510 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000511 return;
512 }
513 if (!S->body()->isUndefined())
514 return;
515
516 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000517 if (S->isWeak()) {
518 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
519 } else {
520 MemoryBufferRef MBRef = Obj.getBuffer();
521 if (!MBRef.getBuffer().empty())
522 addFile(createObjectFile(MBRef));
523 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000524}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000525
Peter Collingbourne892d49802016-04-27 00:05:03 +0000526// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000527template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000528 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000529 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
530 if (std::unique_ptr<InputFile> File = L->getFile())
531 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000532}
533
Rui Ueyama93bfee52015-10-13 18:10:33 +0000534// This function takes care of the case in which shared libraries depend on
535// the user program (not the other way, which is usual). Shared libraries
536// may have undefined symbols, expecting that the user program provides
537// the definitions for them. An example is BSD's __progname symbol.
538// We need to put such symbols to the main program's .dynsym so that
539// shared libraries can find them.
540// Except this, we ignore undefined symbols in DSOs.
541template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000542 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
543 for (StringRef U : File->getUndefinedSymbols())
544 if (SymbolBody *Sym = find(U))
545 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000546 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000547}
548
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000549// This function process the dynamic list option by marking all the symbols
550// to be exported in the dynamic table.
551template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
552 for (StringRef S : Config->DynamicList)
553 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000554 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000555}
556
George Rimarf73a2582016-07-07 07:45:27 +0000557static bool hasWildcard(StringRef S) {
558 return S.find_first_of("?*") != StringRef::npos;
559}
560
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000561// This function processes the --version-script option by marking all global
562// symbols with the VersionScriptGlobal flag, which acts as a filter on the
563// dynamic symbol table.
564template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000565 // If version script does not contain versions declarations,
566 // we just should mark global symbols.
567 if (!Config->VersionScriptGlobals.empty()) {
568 for (StringRef S : Config->VersionScriptGlobals)
569 if (SymbolBody *B = find(S))
570 B->symbol()->VersionId = VER_NDX_GLOBAL;
571 return;
572 }
573
George Rimarf73a2582016-07-07 07:45:27 +0000574 if (Config->SymbolVersions.empty())
575 return;
576
George Rimard3566302016-06-20 11:55:12 +0000577 // If we have symbols version declarations, we should
578 // assign version references for each symbol.
George Rimarf73a2582016-07-07 07:45:27 +0000579 // Current rules are:
580 // * If there is an exact match for the mangled name, we use it.
581 // * Otherwise, we look through the wildcard patterns. We look through the
582 // version tags in reverse order. We use the first match we find (the last
583 // matching version tag in the file).
584 for (size_t I = 0, E = Config->SymbolVersions.size(); I < E; ++I) {
585 Version &V = Config->SymbolVersions[I];
George Rimar36b2c0a2016-06-28 08:07:26 +0000586 for (StringRef Name : V.Globals) {
George Rimarf73a2582016-07-07 07:45:27 +0000587 if (hasWildcard(Name))
588 continue;
589
590 SymbolBody *B = find(Name);
591 if (!B || B->isUndefined()) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000592 if (Config->NoUndefinedVersion)
593 error("version script assignment of " + V.Name + " to symbol " +
594 Name + " failed: symbol not defined");
595 continue;
596 }
George Rimar36b2c0a2016-06-28 08:07:26 +0000597
George Rimarf73a2582016-07-07 07:45:27 +0000598 if (B->symbol()->VersionId != VER_NDX_GLOBAL &&
599 B->symbol()->VersionId != VER_NDX_LOCAL)
600 warning("duplicate symbol " + Name + " in version script");
601 B->symbol()->VersionId = I + 2;
George Rimar36b2c0a2016-06-28 08:07:26 +0000602 }
George Rimarf73a2582016-07-07 07:45:27 +0000603 }
604
605 for (size_t I = Config->SymbolVersions.size() - 1; I != (size_t)-1; --I) {
606 Version &V = Config->SymbolVersions[I];
607 for (StringRef Name : V.Globals) {
608 if (!hasWildcard(Name))
609 continue;
610
611 for (SymbolBody *B : findAll(Name))
612 if (B->symbol()->VersionId == VER_NDX_GLOBAL ||
613 B->symbol()->VersionId == VER_NDX_LOCAL)
614 B->symbol()->VersionId = I + 2;
615 }
George Rimard3566302016-06-20 11:55:12 +0000616 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000617}
618
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000619// Print the module names which define the notified
620// symbols provided through -y or --trace-symbol option.
621template <class ELFT> void SymbolTable<ELFT>::traceDefined() {
622 for (const auto &Symbol : Config->TraceSymbol)
623 if (SymbolBody *B = find(Symbol.getKey()))
624 if (B->isDefined() || B->isCommon())
625 if (InputFile *File = B->getSourceFile<ELFT>())
626 outs() << getFilename(File) << ": definition of "
627 << B->getName() << "\n";
628}
629
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000630template class elf::SymbolTable<ELF32LE>;
631template class elf::SymbolTable<ELF32BE>;
632template class elf::SymbolTable<ELF64LE>;
633template class elf::SymbolTable<ELF64BE>;