blob: c657a3a0608e02743b0c709de4aa6693f671a5ed [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 Ueyama69c778c2016-07-17 17:50:09 +0000143// Set a flag for --trace-symbol so that we can print out a log message
144// if a new symbol with the same name is inserted into the symbol table.
145template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Rui Ueyamae3357902016-07-18 01:35:00 +0000146 Symtab.insert({Name, {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000147}
148
Rui Ueyamadeb15402016-01-07 17:20:07 +0000149// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
150// Used to implement --wrap.
151template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000152 SymbolBody *B = find(Name);
153 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000154 return;
155 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000156 Symbol *Sym = B->symbol();
157 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
158 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
159 // We rename symbols by replacing the old symbol's SymbolBody with the new
160 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
161 // old symbol to instead refer to the new symbol.
162 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
163 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000164}
165
Peter Collingbournedadcc172016-04-22 18:42:48 +0000166static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
167 if (VA == STV_DEFAULT)
168 return VB;
169 if (VB == STV_DEFAULT)
170 return VA;
171 return std::min(VA, VB);
172}
173
Rui Ueyamadace8382016-07-21 13:13:21 +0000174// Parses a symbol in the form of <name>@<version> or <name>@@<version>.
175static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) {
176 if (Config->VersionDefinitions.empty())
177 return {S, Config->DefaultSymbolVersion};
178
179 size_t Pos = S.find('@');
180 if (Pos == 0 || Pos == StringRef::npos)
181 return {S, Config->DefaultSymbolVersion};
182
183 StringRef Name = S.substr(0, Pos);
184 StringRef Verstr = S.substr(Pos + 1);
185 if (Verstr.empty())
186 return {S, Config->DefaultSymbolVersion};
187
188 // '@@' in a symbol name means the default version.
189 // It is usually the most recent one.
190 bool IsDefault = (Verstr[0] == '@');
191 if (IsDefault)
192 Verstr = Verstr.substr(1);
193
194 for (VersionDefinition &V : Config->VersionDefinitions) {
195 if (V.Name == Verstr)
196 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)};
197 }
198
199 // It is an error if the specified version was not defined.
200 error("symbol " + S + " has undefined version " + Verstr);
201 return {S, Config->DefaultSymbolVersion};
202}
203
Rui Ueyamab4de5952016-01-08 22:01:33 +0000204// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000205template <class ELFT>
Rui Ueyamadace8382016-07-21 13:13:21 +0000206std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) {
George Rimarb0841252016-07-20 14:26:48 +0000207 auto P = Symtab.insert({Name, SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000208 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000209 bool IsNew = P.second;
210
Rui Ueyamae3357902016-07-18 01:35:00 +0000211 if (V.Idx == -1) {
212 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000213 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000214 }
215
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000216 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000217 if (IsNew) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000218 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000219 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000220 Sym->Visibility = STV_DEFAULT;
221 Sym->IsUsedInRegularObj = false;
222 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000223 Sym->Traced = V.Traced;
Rui Ueyamadace8382016-07-21 13:13:21 +0000224 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000225 SymVector.push_back(Sym);
226 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000227 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000228 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000229 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000230}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000231
Peter Collingbourne4f952702016-05-01 04:55:03 +0000232// Find an existing symbol or create and insert a new one, then apply the given
233// attributes.
234template <class ELFT>
235std::pair<Symbol *, bool>
Rui Ueyamadace8382016-07-21 13:13:21 +0000236SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000237 bool CanOmitFromDynSym, bool IsUsedInRegularObj,
238 InputFile *File) {
239 Symbol *S;
240 bool WasInserted;
241 std::tie(S, WasInserted) = insert(Name);
242
243 // Merge in the new symbol's visibility.
244 S->Visibility = getMinVisibility(S->Visibility, Visibility);
245 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
246 S->ExportDynamic = true;
247 if (IsUsedInRegularObj)
248 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000249 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
250 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000251 error("TLS attribute mismatch for symbol: " +
252 conflictMsg(S->body(), File));
253
254 return {S, WasInserted};
255}
256
257// Construct a string in the form of "Sym in File1 and File2".
258// Used to construct an error message.
259template <typename ELFT>
260std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
261 InputFile *NewFile) {
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000262 std::string Sym = Existing->getName();
263 if (Config->Demangle)
264 Sym = demangle(Sym);
Rui Ueyama434b5612016-07-17 03:11:46 +0000265 return Sym + " in " + getFilename(Existing->File) + " and " +
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000266 getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000267}
268
269template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
270 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000271 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000272}
273
274template <class ELFT>
275Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
276 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000277 bool CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000278 InputFile *File) {
279 Symbol *S;
280 bool WasInserted;
281 std::tie(S, WasInserted) =
Rafael Espindolacc70da32016-06-15 17:56:10 +0000282 insert(Name, Type, StOther & 3, CanOmitFromDynSym,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000283 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
284 if (WasInserted) {
285 S->Binding = Binding;
Rui Ueyama434b5612016-07-17 03:11:46 +0000286 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000287 return S;
288 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000289 if (Binding != STB_WEAK) {
290 if (S->body()->isShared() || S->body()->isLazy())
291 S->Binding = Binding;
292 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000293 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000294 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000295 if (auto *L = dyn_cast<Lazy>(S->body())) {
296 // An undefined weak will not fetch archive members, but we have to remember
297 // its type. See also comment in addLazyArchive.
298 if (S->isWeak())
299 L->Type = Type;
Rui Ueyama434b5612016-07-17 03:11:46 +0000300 else if (auto F = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000301 addFile(std::move(F));
302 }
303 return S;
304}
305
306// We have a new defined symbol with the specified binding. Return 1 if the new
307// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
308// strong defined symbols.
309static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
310 if (WasInserted)
311 return 1;
312 SymbolBody *Body = S->body();
313 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
314 return 1;
315 if (Binding == STB_WEAK)
316 return -1;
317 if (S->isWeak())
318 return 1;
319 return 0;
320}
321
322// We have a new non-common defined symbol with the specified binding. Return 1
323// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
324// is a conflict. If the new symbol wins, also update the binding.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000325template <class ELFT>
326static int compareDefinedNonCommon(Symbol *S, bool WasInserted,
327 uint8_t Binding) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000328 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
329 if (Cmp > 0)
330 S->Binding = Binding;
331 return Cmp;
332 }
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000333 if (isa<DefinedCommon<ELFT>>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000334 // Non-common symbols take precedence over common symbols.
335 if (Config->WarnCommon)
336 warning("common " + S->body()->getName() + " is overridden");
337 return 1;
338 }
339 return 0;
340}
341
342template <class ELFT>
343Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
344 uint64_t Alignment, uint8_t Binding,
345 uint8_t StOther, uint8_t Type,
346 InputFile *File) {
347 Symbol *S;
348 bool WasInserted;
349 std::tie(S, WasInserted) =
350 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
351 /*IsUsedInRegularObj*/ true, File);
352 int Cmp = compareDefined(S, WasInserted, Binding);
353 if (Cmp > 0) {
354 S->Binding = Binding;
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000355 replaceBody<DefinedCommon<ELFT>>(S, N, Size, Alignment, StOther, Type,
356 File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000357 } else if (Cmp == 0) {
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000358 auto *C = dyn_cast<DefinedCommon<ELFT>>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000359 if (!C) {
360 // Non-common symbols take precedence over common symbols.
361 if (Config->WarnCommon)
362 warning("common " + S->body()->getName() + " is overridden");
363 return S;
364 }
365
366 if (Config->WarnCommon)
367 warning("multiple common of " + S->body()->getName());
368
369 C->Size = std::max(C->Size, Size);
370 C->Alignment = std::max(C->Alignment, Alignment);
371 }
372 return S;
373}
374
375template <class ELFT>
376void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
377 InputFile *NewFile) {
378 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
379 if (Config->AllowMultipleDefinition)
380 warning(Msg);
381 else
382 error(Msg);
383}
384
385template <typename ELFT>
386Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
387 InputSectionBase<ELFT> *Section) {
388 Symbol *S;
389 bool WasInserted;
390 std::tie(S, WasInserted) =
391 insert(Name, Sym.getType(), Sym.getVisibility(),
392 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
393 Section ? Section->getFile() : nullptr);
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000394 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Sym.getBinding());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000395 if (Cmp > 0)
396 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
397 else if (Cmp == 0)
398 reportDuplicate(S->body(), Section->getFile());
399 return S;
400}
401
402template <typename ELFT>
403Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
404 uint8_t StOther) {
405 Symbol *S;
406 bool WasInserted;
407 std::tie(S, WasInserted) =
408 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
409 /*IsUsedInRegularObj*/ true, nullptr);
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000410 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000411 if (Cmp > 0)
412 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
413 else if (Cmp == 0)
414 reportDuplicate(S->body(), nullptr);
415 return S;
416}
417
418template <typename ELFT>
419Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000420 OutputSectionBase<ELFT> *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000421 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000422 Symbol *S;
423 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000424 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
425 /*CanOmitFromDynSym*/ false,
426 /*IsUsedInRegularObj*/ true, nullptr);
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000427 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000428 if (Cmp > 0)
429 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
430 else if (Cmp == 0)
431 reportDuplicate(S->body(), nullptr);
432 return S;
433}
434
435template <typename ELFT>
436void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
437 const Elf_Sym &Sym,
438 const typename ELFT::Verdef *Verdef) {
439 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
440 // as the visibility, which will leave the visibility in the symbol table
441 // unchanged.
442 Symbol *S;
443 bool WasInserted;
444 std::tie(S, WasInserted) =
445 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
446 /*IsUsedInRegularObj*/ false, F);
447 // Make sure we preempt DSO symbols with default visibility.
448 if (Sym.getVisibility() == STV_DEFAULT)
449 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000450 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000451 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000452 if (!S->isWeak())
453 F->IsUsed = true;
454 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000455}
456
457template <class ELFT>
458Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
459 uint8_t StOther, uint8_t Type,
460 bool CanOmitFromDynSym, BitcodeFile *F) {
461 Symbol *S;
462 bool WasInserted;
463 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
464 /*IsUsedInRegularObj*/ false, F);
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000465 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted,
466 IsWeak ? STB_WEAK : STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000467 if (Cmp > 0)
468 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
469 else if (Cmp == 0)
470 reportDuplicate(S->body(), F);
471 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000472}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000473
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000474template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
475 auto It = Symtab.find(Name);
476 if (It == Symtab.end())
477 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000478 SymIndex V = It->second;
479 if (V.Idx == -1)
480 return nullptr;
481 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000482}
483
Rui Ueyama48e42512016-06-29 04:47:39 +0000484// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000485template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000486std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000487 std::vector<SymbolBody *> Res;
Rui Ueyamad6328522016-07-18 01:34:57 +0000488 for (Symbol *Sym : SymVector) {
489 SymbolBody *B = Sym->body();
490 if (!B->isUndefined() && globMatch(Pattern, B->getName()))
Rui Ueyama48e42512016-06-29 04:47:39 +0000491 Res.push_back(B);
492 }
493 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000494}
495
496template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000497void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
498 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000499 Symbol *S;
500 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000501 StringRef Name = Sym.getName();
502 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000503 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000504 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000505 return;
506 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000507 if (!S->body()->isUndefined())
508 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000509
Peter Collingbourne4f952702016-05-01 04:55:03 +0000510 // Weak undefined symbols should not fetch members from archives. If we were
511 // to keep old symbol we would not know that an archive member was available
512 // if a strong undefined symbol shows up afterwards in the link. If a strong
513 // undefined symbol never shows up, this lazy symbol will get to the end of
514 // the link and must be treated as the weak undefined one. We already marked
515 // this symbol as used when we added it to the symbol table, but we also need
516 // to preserve its type. FIXME: Move the Type field to Symbol.
517 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000518 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000519 return;
520 }
521 MemoryBufferRef MBRef = F->getMember(&Sym);
522 if (!MBRef.getBuffer().empty())
523 addFile(createObjectFile(MBRef, F->getName()));
524}
525
526template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000527void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000528 Symbol *S;
529 bool WasInserted;
530 std::tie(S, WasInserted) = insert(Name);
531 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000532 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000533 return;
534 }
535 if (!S->body()->isUndefined())
536 return;
537
538 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000539 if (S->isWeak()) {
540 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
541 } else {
542 MemoryBufferRef MBRef = Obj.getBuffer();
543 if (!MBRef.getBuffer().empty())
544 addFile(createObjectFile(MBRef));
545 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000546}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000547
Peter Collingbourne892d49802016-04-27 00:05:03 +0000548// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000549template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000550 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000551 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama434b5612016-07-17 03:11:46 +0000552 if (std::unique_ptr<InputFile> File = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000553 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000554}
555
Rui Ueyama93bfee52015-10-13 18:10:33 +0000556// This function takes care of the case in which shared libraries depend on
557// the user program (not the other way, which is usual). Shared libraries
558// may have undefined symbols, expecting that the user program provides
559// the definitions for them. An example is BSD's __progname symbol.
560// We need to put such symbols to the main program's .dynsym so that
561// shared libraries can find them.
562// Except this, we ignore undefined symbols in DSOs.
563template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000564 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
565 for (StringRef U : File->getUndefinedSymbols())
566 if (SymbolBody *Sym = find(U))
567 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000568 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000569}
570
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000571// This function process the dynamic list option by marking all the symbols
572// to be exported in the dynamic table.
573template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
574 for (StringRef S : Config->DynamicList)
575 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000576 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000577}
578
George Rimarf73a2582016-07-07 07:45:27 +0000579static bool hasWildcard(StringRef S) {
580 return S.find_first_of("?*") != StringRef::npos;
581}
582
George Rimar50dcece2016-07-16 12:26:39 +0000583static void setVersionId(SymbolBody *Body, StringRef VersionName,
584 StringRef Name, uint16_t Version) {
585 if (!Body || Body->isUndefined()) {
586 if (Config->NoUndefinedVersion)
587 error("version script assignment of " + VersionName + " to symbol " +
588 Name + " failed: symbol not defined");
589 return;
590 }
591
592 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000593 if (Sym->VersionId != Config->DefaultSymbolVersion)
George Rimar50dcece2016-07-16 12:26:39 +0000594 warning("duplicate symbol " + Name + " in version script");
595 Sym->VersionId = Version;
596}
597
598template <class ELFT>
599std::map<std::string, SymbolBody *> SymbolTable<ELFT>::getDemangledSyms() {
600 std::map<std::string, SymbolBody *> Result;
Rui Ueyamad6328522016-07-18 01:34:57 +0000601 for (Symbol *Sym : SymVector) {
602 SymbolBody *B = Sym->body();
603 Result[demangle(B->getName())] = B;
604 }
George Rimar50dcece2016-07-16 12:26:39 +0000605 return Result;
606}
607
608static bool hasExternCpp() {
609 for (VersionDefinition &V : Config->VersionDefinitions)
610 for (SymbolVersion Sym : V.Globals)
611 if (Sym.IsExternCpp)
612 return true;
613 return false;
614}
615
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000616// This function processes the --version-script option by marking all global
617// symbols with the VersionScriptGlobal flag, which acts as a filter on the
618// dynamic symbol table.
619template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000620 // If version script does not contain versions declarations,
621 // we just should mark global symbols.
622 if (!Config->VersionScriptGlobals.empty()) {
George Rimar50dcece2016-07-16 12:26:39 +0000623 for (SymbolVersion &Sym : Config->VersionScriptGlobals)
624 if (SymbolBody *B = find(Sym.Name))
George Rimard3566302016-06-20 11:55:12 +0000625 B->symbol()->VersionId = VER_NDX_GLOBAL;
626 return;
627 }
628
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000629 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000630 return;
631
George Rimard3566302016-06-20 11:55:12 +0000632 // If we have symbols version declarations, we should
633 // assign version references for each symbol.
George Rimarf73a2582016-07-07 07:45:27 +0000634 // Current rules are:
George Rimar50dcece2016-07-16 12:26:39 +0000635 // * If there is an exact match for the mangled name or we have extern C++
636 // exact match, then we use it.
George Rimarf73a2582016-07-07 07:45:27 +0000637 // * Otherwise, we look through the wildcard patterns. We look through the
638 // version tags in reverse order. We use the first match we find (the last
639 // matching version tag in the file).
George Rimar50dcece2016-07-16 12:26:39 +0000640 // Handle exact matches and build a map of demangled externs for
641 // quick search during next step.
642 std::map<std::string, SymbolBody *> Demangled;
643 if (hasExternCpp())
644 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000645
George Rimar50dcece2016-07-16 12:26:39 +0000646 for (VersionDefinition &V : Config->VersionDefinitions) {
647 for (SymbolVersion Sym : V.Globals) {
648 if (hasWildcard(Sym.Name))
George Rimardd64bb32016-07-13 08:19:04 +0000649 continue;
George Rimar50dcece2016-07-16 12:26:39 +0000650 SymbolBody *B = Sym.IsExternCpp ? Demangled[Sym.Name] : find(Sym.Name);
651 setVersionId(B, V.Name, Sym.Name, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000652 }
George Rimarf73a2582016-07-07 07:45:27 +0000653 }
654
George Rimar50dcece2016-07-16 12:26:39 +0000655 // Handle wildcards.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000656 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
657 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimar50dcece2016-07-16 12:26:39 +0000658 for (SymbolVersion &Sym : V.Globals)
659 if (hasWildcard(Sym.Name))
660 for (SymbolBody *B : findAll(Sym.Name))
Rui Ueyamac9b4c072016-07-16 03:12:16 +0000661 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
662 B->symbol()->VersionId = V.Id;
George Rimard3566302016-06-20 11:55:12 +0000663 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000664}
665
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000666template class elf::SymbolTable<ELF32LE>;
667template class elf::SymbolTable<ELF32BE>;
668template class elf::SymbolTable<ELF64LE>;
669template class elf::SymbolTable<ELF64BE>;