blob: 4fb2b87bf306cbe99d1d4d34f60b02144bdfd640 [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)
Rui Ueyama818bb2f2016-07-16 18:55:47 +000072 outs() << getFilename(FileP) << "\n";
George Rimar2a78fce2016-04-13 18:07:57 +000073
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
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000120 DenseSet<StringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000121 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
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000183 for (VersionDefinition &V : Config->VersionDefinitions)
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);
Rui Ueyama434b5612016-07-17 03:11:46 +0000256 return Sym + " in " + getFilename(Existing->File) + " and " +
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000257 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;
Rui Ueyama434b5612016-07-17 03:11:46 +0000277 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000278 return S;
279 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000280 if (Binding != STB_WEAK) {
281 if (S->body()->isShared() || S->body()->isLazy())
282 S->Binding = Binding;
283 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000284 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000285 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000286 if (auto *L = dyn_cast<Lazy>(S->body())) {
287 // An undefined weak will not fetch archive members, but we have to remember
288 // its type. See also comment in addLazyArchive.
289 if (S->isWeak())
290 L->Type = Type;
Rui Ueyama434b5612016-07-17 03:11:46 +0000291 else if (auto F = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000292 addFile(std::move(F));
293 }
294 return S;
295}
296
297// We have a new defined symbol with the specified binding. Return 1 if the new
298// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
299// strong defined symbols.
300static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
301 if (WasInserted)
302 return 1;
303 SymbolBody *Body = S->body();
304 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
305 return 1;
306 if (Binding == STB_WEAK)
307 return -1;
308 if (S->isWeak())
309 return 1;
310 return 0;
311}
312
313// We have a new non-common defined symbol with the specified binding. Return 1
314// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
315// is a conflict. If the new symbol wins, also update the binding.
316static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
317 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
318 if (Cmp > 0)
319 S->Binding = Binding;
320 return Cmp;
321 }
322 if (isa<DefinedCommon>(S->body())) {
323 // Non-common symbols take precedence over common symbols.
324 if (Config->WarnCommon)
325 warning("common " + S->body()->getName() + " is overridden");
326 return 1;
327 }
328 return 0;
329}
330
331template <class ELFT>
332Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
333 uint64_t Alignment, uint8_t Binding,
334 uint8_t StOther, uint8_t Type,
335 InputFile *File) {
336 Symbol *S;
337 bool WasInserted;
338 std::tie(S, WasInserted) =
339 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
340 /*IsUsedInRegularObj*/ true, File);
341 int Cmp = compareDefined(S, WasInserted, Binding);
342 if (Cmp > 0) {
343 S->Binding = Binding;
344 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
345 } else if (Cmp == 0) {
346 auto *C = dyn_cast<DefinedCommon>(S->body());
347 if (!C) {
348 // Non-common symbols take precedence over common symbols.
349 if (Config->WarnCommon)
350 warning("common " + S->body()->getName() + " is overridden");
351 return S;
352 }
353
354 if (Config->WarnCommon)
355 warning("multiple common of " + S->body()->getName());
356
357 C->Size = std::max(C->Size, Size);
358 C->Alignment = std::max(C->Alignment, Alignment);
359 }
360 return S;
361}
362
363template <class ELFT>
364void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
365 InputFile *NewFile) {
366 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
367 if (Config->AllowMultipleDefinition)
368 warning(Msg);
369 else
370 error(Msg);
371}
372
373template <typename ELFT>
374Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
375 InputSectionBase<ELFT> *Section) {
376 Symbol *S;
377 bool WasInserted;
378 std::tie(S, WasInserted) =
379 insert(Name, Sym.getType(), Sym.getVisibility(),
380 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
381 Section ? Section->getFile() : nullptr);
382 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
383 if (Cmp > 0)
384 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
385 else if (Cmp == 0)
386 reportDuplicate(S->body(), Section->getFile());
387 return S;
388}
389
390template <typename ELFT>
391Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
392 uint8_t StOther) {
393 Symbol *S;
394 bool WasInserted;
395 std::tie(S, WasInserted) =
396 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
397 /*IsUsedInRegularObj*/ true, nullptr);
398 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
399 if (Cmp > 0)
400 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
401 else if (Cmp == 0)
402 reportDuplicate(S->body(), nullptr);
403 return S;
404}
405
406template <typename ELFT>
407Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000408 OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000409 uintX_t Value) {
410 Symbol *S;
411 bool WasInserted;
412 std::tie(S, WasInserted) =
413 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
414 /*IsUsedInRegularObj*/ true, nullptr);
415 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
416 if (Cmp > 0)
417 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
418 else if (Cmp == 0)
419 reportDuplicate(S->body(), nullptr);
420 return S;
421}
422
423template <typename ELFT>
424void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
425 const Elf_Sym &Sym,
426 const typename ELFT::Verdef *Verdef) {
427 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
428 // as the visibility, which will leave the visibility in the symbol table
429 // unchanged.
430 Symbol *S;
431 bool WasInserted;
432 std::tie(S, WasInserted) =
433 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
434 /*IsUsedInRegularObj*/ false, F);
435 // Make sure we preempt DSO symbols with default visibility.
436 if (Sym.getVisibility() == STV_DEFAULT)
437 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000438 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000439 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000440 if (!S->isWeak())
441 F->IsUsed = true;
442 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000443}
444
445template <class ELFT>
446Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
447 uint8_t StOther, uint8_t Type,
448 bool CanOmitFromDynSym, BitcodeFile *F) {
449 Symbol *S;
450 bool WasInserted;
451 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
452 /*IsUsedInRegularObj*/ false, F);
453 int Cmp =
454 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
455 if (Cmp > 0)
456 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
457 else if (Cmp == 0)
458 reportDuplicate(S->body(), F);
459 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000460}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000461
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000462template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
463 auto It = Symtab.find(Name);
464 if (It == Symtab.end())
465 return nullptr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000466 return SymVector[It->second]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000467}
468
Rui Ueyama48e42512016-06-29 04:47:39 +0000469// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000470template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000471std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000472 std::vector<SymbolBody *> Res;
473 for (auto &It : Symtab) {
474 StringRef Name = It.first.Val;
475 SymbolBody *B = SymVector[It.second]->body();
Rui Ueyama722830a2016-06-29 05:32:09 +0000476 if (!B->isUndefined() && globMatch(Pattern, Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000477 Res.push_back(B);
478 }
479 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000480}
481
482template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000483void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
484 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000485 Symbol *S;
486 bool WasInserted;
487 std::tie(S, WasInserted) = insert(Sym.getName());
488 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000489 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000490 return;
491 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000492 if (!S->body()->isUndefined())
493 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000494
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495 // Weak undefined symbols should not fetch members from archives. If we were
496 // to keep old symbol we would not know that an archive member was available
497 // if a strong undefined symbol shows up afterwards in the link. If a strong
498 // undefined symbol never shows up, this lazy symbol will get to the end of
499 // the link and must be treated as the weak undefined one. We already marked
500 // this symbol as used when we added it to the symbol table, but we also need
501 // to preserve its type. FIXME: Move the Type field to Symbol.
502 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000503 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000504 return;
505 }
506 MemoryBufferRef MBRef = F->getMember(&Sym);
507 if (!MBRef.getBuffer().empty())
508 addFile(createObjectFile(MBRef, F->getName()));
509}
510
511template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000512void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000513 Symbol *S;
514 bool WasInserted;
515 std::tie(S, WasInserted) = insert(Name);
516 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000517 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000518 return;
519 }
520 if (!S->body()->isUndefined())
521 return;
522
523 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000524 if (S->isWeak()) {
525 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
526 } else {
527 MemoryBufferRef MBRef = Obj.getBuffer();
528 if (!MBRef.getBuffer().empty())
529 addFile(createObjectFile(MBRef));
530 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000531}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000532
Peter Collingbourne892d49802016-04-27 00:05:03 +0000533// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000534template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000535 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama434b5612016-07-17 03:11:46 +0000537 if (std::unique_ptr<InputFile> File = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000538 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000539}
540
Rui Ueyama93bfee52015-10-13 18:10:33 +0000541// This function takes care of the case in which shared libraries depend on
542// the user program (not the other way, which is usual). Shared libraries
543// may have undefined symbols, expecting that the user program provides
544// the definitions for them. An example is BSD's __progname symbol.
545// We need to put such symbols to the main program's .dynsym so that
546// shared libraries can find them.
547// Except this, we ignore undefined symbols in DSOs.
548template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000549 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
550 for (StringRef U : File->getUndefinedSymbols())
551 if (SymbolBody *Sym = find(U))
552 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000553 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000554}
555
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000556// This function process the dynamic list option by marking all the symbols
557// to be exported in the dynamic table.
558template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
559 for (StringRef S : Config->DynamicList)
560 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000561 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000562}
563
George Rimarf73a2582016-07-07 07:45:27 +0000564static bool hasWildcard(StringRef S) {
565 return S.find_first_of("?*") != StringRef::npos;
566}
567
George Rimar50dcece2016-07-16 12:26:39 +0000568static void setVersionId(SymbolBody *Body, StringRef VersionName,
569 StringRef Name, uint16_t Version) {
570 if (!Body || Body->isUndefined()) {
571 if (Config->NoUndefinedVersion)
572 error("version script assignment of " + VersionName + " to symbol " +
573 Name + " failed: symbol not defined");
574 return;
575 }
576
577 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000578 if (Sym->VersionId != Config->DefaultSymbolVersion)
George Rimar50dcece2016-07-16 12:26:39 +0000579 warning("duplicate symbol " + Name + " in version script");
580 Sym->VersionId = Version;
581}
582
583template <class ELFT>
584std::map<std::string, SymbolBody *> SymbolTable<ELFT>::getDemangledSyms() {
585 std::map<std::string, SymbolBody *> Result;
586 for (std::pair<SymName, unsigned> Sym : Symtab)
587 Result[demangle(Sym.first.Val)] = SymVector[Sym.second]->body();
588 return Result;
589}
590
591static bool hasExternCpp() {
592 for (VersionDefinition &V : Config->VersionDefinitions)
593 for (SymbolVersion Sym : V.Globals)
594 if (Sym.IsExternCpp)
595 return true;
596 return false;
597}
598
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000599// This function processes the --version-script option by marking all global
600// symbols with the VersionScriptGlobal flag, which acts as a filter on the
601// dynamic symbol table.
602template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000603 // If version script does not contain versions declarations,
604 // we just should mark global symbols.
605 if (!Config->VersionScriptGlobals.empty()) {
George Rimar50dcece2016-07-16 12:26:39 +0000606 for (SymbolVersion &Sym : Config->VersionScriptGlobals)
607 if (SymbolBody *B = find(Sym.Name))
George Rimard3566302016-06-20 11:55:12 +0000608 B->symbol()->VersionId = VER_NDX_GLOBAL;
609 return;
610 }
611
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000612 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000613 return;
614
George Rimard3566302016-06-20 11:55:12 +0000615 // If we have symbols version declarations, we should
616 // assign version references for each symbol.
George Rimarf73a2582016-07-07 07:45:27 +0000617 // Current rules are:
George Rimar50dcece2016-07-16 12:26:39 +0000618 // * If there is an exact match for the mangled name or we have extern C++
619 // exact match, then we use it.
George Rimarf73a2582016-07-07 07:45:27 +0000620 // * Otherwise, we look through the wildcard patterns. We look through the
621 // version tags in reverse order. We use the first match we find (the last
622 // matching version tag in the file).
George Rimar50dcece2016-07-16 12:26:39 +0000623 // Handle exact matches and build a map of demangled externs for
624 // quick search during next step.
625 std::map<std::string, SymbolBody *> Demangled;
626 if (hasExternCpp())
627 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000628
George Rimar50dcece2016-07-16 12:26:39 +0000629 for (VersionDefinition &V : Config->VersionDefinitions) {
630 for (SymbolVersion Sym : V.Globals) {
631 if (hasWildcard(Sym.Name))
George Rimardd64bb32016-07-13 08:19:04 +0000632 continue;
George Rimar50dcece2016-07-16 12:26:39 +0000633 SymbolBody *B = Sym.IsExternCpp ? Demangled[Sym.Name] : find(Sym.Name);
634 setVersionId(B, V.Name, Sym.Name, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000635 }
George Rimarf73a2582016-07-07 07:45:27 +0000636 }
637
George Rimar50dcece2016-07-16 12:26:39 +0000638 // Handle wildcards.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000639 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
640 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimar50dcece2016-07-16 12:26:39 +0000641 for (SymbolVersion &Sym : V.Globals)
642 if (hasWildcard(Sym.Name))
643 for (SymbolBody *B : findAll(Sym.Name))
Rui Ueyamac9b4c072016-07-16 03:12:16 +0000644 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
645 B->symbol()->VersionId = V.Id;
George Rimard3566302016-06-20 11:55:12 +0000646 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000647}
648
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000649// Print the module names which define the notified
650// symbols provided through -y or --trace-symbol option.
651template <class ELFT> void SymbolTable<ELFT>::traceDefined() {
652 for (const auto &Symbol : Config->TraceSymbol)
653 if (SymbolBody *B = find(Symbol.getKey()))
654 if (B->isDefined() || B->isCommon())
Rui Ueyama434b5612016-07-17 03:11:46 +0000655 if (B->File)
656 outs() << getFilename(B->File) << ": definition of " << B->getName()
657 << "\n";
Rui Ueyamad60dae8a2016-06-23 07:00:17 +0000658}
659
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000660template class elf::SymbolTable<ELF32LE>;
661template class elf::SymbolTable<ELF32BE>;
662template class elf::SymbolTable<ELF64LE>;
663template class elf::SymbolTable<ELF64BE>;