blob: 7056e34a12c452008191b0b600f23c3d24f192b8 [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 Ueyama55518e72016-10-28 20:57:25 +000021#include "Memory.h"
George Rimar7899d482016-07-12 07:44:40 +000022#include "SymbolListFile.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023#include "Symbols.h"
24
25using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000026using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000027using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
29using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000030using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
Rui Ueyamac9559d92016-01-05 20:47:37 +000032// All input object files must be for the same architecture
33// (e.g. it does not make sense to link x86 object files with
34// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000035template <class ELFT> static bool isCompatible(InputFile *F) {
36 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000037 return true;
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000038 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
39 if (Config->EMachine != EM_MIPS)
40 return true;
41 if (isMipsN32Abi(F) == Config->MipsN32Abi)
42 return true;
43 }
Rui Ueyama25b44c92015-12-16 23:31:22 +000044 StringRef A = F->getName();
45 StringRef B = Config->Emulation;
46 if (B.empty())
47 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000048 error(A + " is incompatible with " + B);
49 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000050}
51
Rui Ueyamac9559d92016-01-05 20:47:37 +000052// Add symbols in File to the symbol table.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000053template <class ELFT> void SymbolTable<ELFT>::addFile(InputFile *File) {
54 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000055 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000056
Michael J. Spencera9424f32016-09-09 22:08:04 +000057 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000058 if (auto *F = dyn_cast<BinaryFile>(File)) {
Rafael Espindola093abab2016-10-27 17:45:40 +000059 BinaryFiles.push_back(F);
60 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000061 return;
62 }
63
Rui Ueyama89575742015-12-16 22:59:13 +000064 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000065 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000066 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000067 return;
68 }
Rui Ueyama3d451792015-10-12 18:03:21 +000069
George Rimar2a78fce2016-04-13 18:07:57 +000070 // Lazy object file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000071 if (auto *F = dyn_cast<LazyObjectFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000072 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000073 return;
74 }
75
76 if (Config->Trace)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000077 outs() << getFilename(File) << "\n";
George Rimar2a78fce2016-04-13 18:07:57 +000078
Rui Ueyama89575742015-12-16 22:59:13 +000079 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000080 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000081 // DSOs are uniquified not by filename but by soname.
82 F->parseSoName();
George Rimarbcba39a2016-11-02 10:16:25 +000083 if (HasError || !SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000084 return;
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000085 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000086 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000087 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000088 }
Rui Ueyama89575742015-12-16 22:59:13 +000089
Rui Ueyamaf8baa662016-04-07 19:24:51 +000090 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000091 if (auto *F = dyn_cast<BitcodeFile>(File)) {
92 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +000093 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000094 return;
95 }
96
Rui Ueyamaf8baa662016-04-07 19:24:51 +000097 // Regular object file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000098 auto *F = cast<ObjectFile<ELFT>>(File);
99 ObjectFiles.push_back(F);
Rui Ueyama52d3b672016-01-06 02:06:33 +0000100 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000101}
102
Rui Ueyama42554752016-04-23 00:26:32 +0000103// This function is where all the optimizations of link-time
104// optimization happens. When LTO is in use, some input files are
105// not in native object file format but in the LLVM bitcode format.
106// This function compiles bitcode files into a few big native files
107// using LLVM functions and replaces bitcode symbols with the results.
108// Because all bitcode files that consist of a program are passed
109// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000110template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
111 if (BitcodeFiles.empty())
112 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000113
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000114 // Compile bitcode files and replace bitcode symbols.
Rui Ueyama25992482016-03-22 20:52:10 +0000115 Lto.reset(new BitcodeCompiler);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000116 for (BitcodeFile *F : BitcodeFiles)
Rui Ueyama25992482016-03-22 20:52:10 +0000117 Lto->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000118
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000119 for (InputFile *File : Lto->compile()) {
120 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(File);
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000121 DenseSet<CachedHashStringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000122 Obj->parse(DummyGroups);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000123 ObjectFiles.push_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000124 }
125}
126
Rafael Espindola0e604f92015-09-25 18:56:53 +0000127template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000128DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
129 uint8_t Visibility) {
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000130 Symbol *Sym =
131 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, STB_GLOBAL, nullptr);
132 return cast<DefinedRegular<ELFT>>(Sym->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000133}
134
Rui Ueyamac9559d92016-01-05 20:47:37 +0000135// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000136// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000137template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000138DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
139 uint8_t Visibility) {
140 if (!find(Name))
141 return nullptr;
142 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000143}
144
Rui Ueyama69c778c2016-07-17 17:50:09 +0000145// Set a flag for --trace-symbol so that we can print out a log message
146// if a new symbol with the same name is inserted into the symbol table.
147template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000148 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000149}
150
Rui Ueyamadeb15402016-01-07 17:20:07 +0000151// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
152// Used to implement --wrap.
153template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000154 SymbolBody *B = find(Name);
155 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000156 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000157 Symbol *Sym = B->symbol();
158 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
159 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000160
Peter Collingbourne4f952702016-05-01 04:55:03 +0000161 // We rename symbols by replacing the old symbol's SymbolBody with the new
162 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
163 // old symbol to instead refer to the new symbol.
164 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
165 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000166}
167
Peter Collingbournedadcc172016-04-22 18:42:48 +0000168static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
169 if (VA == STV_DEFAULT)
170 return VB;
171 if (VB == STV_DEFAULT)
172 return VA;
173 return std::min(VA, VB);
174}
175
Rui Ueyamadace8382016-07-21 13:13:21 +0000176// Parses a symbol in the form of <name>@<version> or <name>@@<version>.
177static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) {
178 if (Config->VersionDefinitions.empty())
179 return {S, Config->DefaultSymbolVersion};
180
181 size_t Pos = S.find('@');
182 if (Pos == 0 || Pos == StringRef::npos)
183 return {S, Config->DefaultSymbolVersion};
184
185 StringRef Name = S.substr(0, Pos);
186 StringRef Verstr = S.substr(Pos + 1);
187 if (Verstr.empty())
188 return {S, Config->DefaultSymbolVersion};
189
190 // '@@' in a symbol name means the default version.
191 // It is usually the most recent one.
192 bool IsDefault = (Verstr[0] == '@');
193 if (IsDefault)
194 Verstr = Verstr.substr(1);
195
196 for (VersionDefinition &V : Config->VersionDefinitions) {
197 if (V.Name == Verstr)
198 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)};
199 }
200
201 // It is an error if the specified version was not defined.
202 error("symbol " + S + " has undefined version " + Verstr);
203 return {S, Config->DefaultSymbolVersion};
204}
205
Rui Ueyamab4de5952016-01-08 22:01:33 +0000206// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000207template <class ELFT>
Rui Ueyamadace8382016-07-21 13:13:21 +0000208std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000209 auto P = Symtab.insert(
210 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000211 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000212 bool IsNew = P.second;
213
Rui Ueyamae3357902016-07-18 01:35:00 +0000214 if (V.Idx == -1) {
215 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000216 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000217 }
218
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000219 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000220 if (IsNew) {
Rui Ueyama55518e72016-10-28 20:57:25 +0000221 Sym = new (BAlloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000222 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000223 Sym->Visibility = STV_DEFAULT;
224 Sym->IsUsedInRegularObj = false;
225 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000226 Sym->Traced = V.Traced;
Rui Ueyamadace8382016-07-21 13:13:21 +0000227 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000228 SymVector.push_back(Sym);
229 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000230 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000231 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000232 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000233}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000234
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000235// Construct a string in the form of "Sym in File1 and File2".
236// Used to construct an error message.
237static std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile) {
Eugene Leviant825e5382016-11-08 16:26:32 +0000238 return "'" + maybeDemangle(Existing->getName()) + "' in " +
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000239 getFilename(Existing->File) + " and " + getFilename(NewFile);
240}
241
Peter Collingbourne4f952702016-05-01 04:55:03 +0000242// Find an existing symbol or create and insert a new one, then apply the given
243// attributes.
244template <class ELFT>
245std::pair<Symbol *, bool>
Rui Ueyamadace8382016-07-21 13:13:21 +0000246SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
Davide Italiano786d8e32016-09-29 00:40:08 +0000247 bool CanOmitFromDynSym, InputFile *File) {
Rafael Espindola05098762016-08-31 13:49:23 +0000248 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000249 Symbol *S;
250 bool WasInserted;
251 std::tie(S, WasInserted) = insert(Name);
252
253 // Merge in the new symbol's visibility.
254 S->Visibility = getMinVisibility(S->Visibility, Visibility);
255 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
256 S->ExportDynamic = true;
257 if (IsUsedInRegularObj)
258 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000259 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
260 ((Type == STT_TLS) != S->body()->isTls()))
Eugene Leviant825e5382016-11-08 16:26:32 +0000261 error("TLS attribute mismatch for symbol " + conflictMsg(S->body(), File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000262
263 return {S, WasInserted};
264}
265
Peter Collingbourne4f952702016-05-01 04:55:03 +0000266template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
267 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Davide Italiano786d8e32016-09-29 00:40:08 +0000268 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000269}
270
271template <class ELFT>
272Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
273 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000274 bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000275 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000276 Symbol *S;
277 bool WasInserted;
278 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000279 insert(Name, Type, StOther & 3, CanOmitFromDynSym, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000280 if (WasInserted) {
281 S->Binding = Binding;
Rui Ueyama434b5612016-07-17 03:11:46 +0000282 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000283 return S;
284 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000285 if (Binding != STB_WEAK) {
286 if (S->body()->isShared() || S->body()->isLazy())
287 S->Binding = Binding;
288 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000289 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000290 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000291 if (auto *L = dyn_cast<Lazy>(S->body())) {
292 // An undefined weak will not fetch archive members, but we have to remember
293 // its type. See also comment in addLazyArchive.
294 if (S->isWeak())
295 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000296 else if (InputFile *F = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000297 addFile(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000298 }
299 return S;
300}
301
302// We have a new defined symbol with the specified binding. Return 1 if the new
303// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
304// strong defined symbols.
305static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
306 if (WasInserted)
307 return 1;
308 SymbolBody *Body = S->body();
309 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
310 return 1;
311 if (Binding == STB_WEAK)
312 return -1;
313 if (S->isWeak())
314 return 1;
315 return 0;
316}
317
318// We have a new non-common defined symbol with the specified binding. Return 1
319// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
320// is a conflict. If the new symbol wins, also update the binding.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000321static int compareDefinedNonCommon(Symbol *S, bool WasInserted,
322 uint8_t Binding) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000323 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
324 if (Cmp > 0)
325 S->Binding = Binding;
326 return Cmp;
327 }
Rafael Espindolae7553e42016-08-31 13:28:33 +0000328 if (isa<DefinedCommon>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000329 // Non-common symbols take precedence over common symbols.
330 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000331 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000332 return 1;
333 }
334 return 0;
335}
336
337template <class ELFT>
338Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
339 uint64_t Alignment, uint8_t Binding,
340 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000341 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000342 Symbol *S;
343 bool WasInserted;
Davide Italiano786d8e32016-09-29 00:40:08 +0000344 std::tie(S, WasInserted) =
345 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000346 int Cmp = compareDefined(S, WasInserted, Binding);
347 if (Cmp > 0) {
348 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000349 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000350 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000351 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000352 if (!C) {
353 // Non-common symbols take precedence over common symbols.
354 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000355 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000356 return S;
357 }
358
359 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000360 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000361
Rafael Espindola8db87292016-08-31 13:42:08 +0000362 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
363 if (Size > C->Size)
364 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000365 }
366 return S;
367}
368
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000369static void print(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000370 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000371 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000372 else
373 error(Msg);
374}
375
Eugene Leviant825e5382016-11-08 16:26:32 +0000376static void reportDuplicate(SymbolBody *Existing, InputFile *NewFile) {
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000377 print("duplicate symbol " + conflictMsg(Existing, NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000378}
379
380template <class ELFT>
381static void reportDuplicate(SymbolBody *Existing,
382 InputSectionBase<ELFT> *ErrSec,
383 typename ELFT::uint ErrOffset) {
384 DefinedRegular<ELFT> *D = dyn_cast<DefinedRegular<ELFT>>(Existing);
385 if (!D || !D->Section || !ErrSec) {
386 reportDuplicate(Existing, ErrSec ? ErrSec->getFile() : nullptr);
387 return;
388 }
389
Rui Ueyamaedc183e2016-11-08 20:30:19 +0000390 std::string OldLoc = getLocation(*D->Section, D->Value);
391 std::string NewLoc = getLocation(*ErrSec, ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000392
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000393 print(NewLoc + ": duplicate symbol '" + maybeDemangle(Existing->getName()) +
394 "'");
395 print(OldLoc + ": previous definition was here");
Eugene Leviant825e5382016-11-08 16:26:32 +0000396}
397
Peter Collingbourne4f952702016-05-01 04:55:03 +0000398template <typename ELFT>
399Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
400 InputSectionBase<ELFT> *Section) {
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000401 return addRegular(Name, Sym.st_other, Sym.getType(), Sym.st_value,
402 Sym.st_size, Sym.getBinding(), Section);
403}
404
405template <typename ELFT>
406Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
407 uint8_t Type, uintX_t Value, uintX_t Size,
408 uint8_t Binding,
409 InputSectionBase<ELFT> *Section) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 Symbol *S;
411 bool WasInserted;
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000412 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3,
Davide Italiano786d8e32016-09-29 00:40:08 +0000413 /*CanOmitFromDynSym*/ false,
414 Section ? Section->getFile() : nullptr);
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000415 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000416 if (Cmp > 0)
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000417 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, Value, Size,
418 Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000419 else if (Cmp == 0)
Eugene Leviant825e5382016-11-08 16:26:32 +0000420 reportDuplicate(S->body(), Section, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000421 return S;
422}
423
424template <typename ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000425Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N, OutputSectionBase *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000426 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000427 Symbol *S;
428 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000429 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
Davide Italiano786d8e32016-09-29 00:40:08 +0000430 /*CanOmitFromDynSym*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000431 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000432 if (Cmp > 0)
433 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
434 else if (Cmp == 0)
435 reportDuplicate(S->body(), nullptr);
436 return S;
437}
438
439template <typename ELFT>
440void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
441 const Elf_Sym &Sym,
442 const typename ELFT::Verdef *Verdef) {
443 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
444 // as the visibility, which will leave the visibility in the symbol table
445 // unchanged.
446 Symbol *S;
447 bool WasInserted;
448 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000449 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000450 // Make sure we preempt DSO symbols with default visibility.
451 if (Sym.getVisibility() == STV_DEFAULT)
452 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000453 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000454 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000455 if (!S->isWeak())
456 F->IsUsed = true;
457 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000458}
459
460template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000461Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000462 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000463 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000464 Symbol *S;
465 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000466 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000467 insert(Name, Type, StOther & 3, CanOmitFromDynSym, F);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000468 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000469 if (Cmp > 0)
Rafael Espindolaa6c97442016-08-31 12:30:34 +0000470 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000471 else if (Cmp == 0)
472 reportDuplicate(S->body(), F);
473 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000474}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000475
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000476template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000477 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000478 if (It == Symtab.end())
479 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000480 SymIndex V = It->second;
481 if (V.Idx == -1)
482 return nullptr;
483 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000484}
485
George Rimarc91930a2016-09-02 21:17:20 +0000486// Returns a list of defined symbols that match with a given regex.
Rui Ueyama3d451792015-10-12 18:03:21 +0000487template <class ELFT>
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000488std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(const StringMatcher &M) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000489 std::vector<SymbolBody *> Res;
Rui Ueyamad6328522016-07-18 01:34:57 +0000490 for (Symbol *Sym : SymVector) {
491 SymbolBody *B = Sym->body();
George Rimarc91930a2016-09-02 21:17:20 +0000492 StringRef Name = B->getName();
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000493 if (!B->isUndefined() && M.match(Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000494 Res.push_back(B);
495 }
496 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000497}
498
499template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000500void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
501 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000502 Symbol *S;
503 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000504 StringRef Name = Sym.getName();
505 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000506 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000507 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000508 return;
509 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000510 if (!S->body()->isUndefined())
511 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000512
Peter Collingbourne4f952702016-05-01 04:55:03 +0000513 // Weak undefined symbols should not fetch members from archives. If we were
514 // to keep old symbol we would not know that an archive member was available
515 // if a strong undefined symbol shows up afterwards in the link. If a strong
516 // undefined symbol never shows up, this lazy symbol will get to the end of
517 // the link and must be treated as the weak undefined one. We already marked
518 // this symbol as used when we added it to the symbol table, but we also need
519 // to preserve its type. FIXME: Move the Type field to Symbol.
520 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000521 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000522 return;
523 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000524 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
525 if (!MBInfo.first.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000526 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000527}
528
529template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000530void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000531 Symbol *S;
532 bool WasInserted;
533 std::tie(S, WasInserted) = insert(Name);
534 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000535 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 return;
537 }
538 if (!S->body()->isUndefined())
539 return;
540
541 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000542 if (S->isWeak()) {
543 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
544 } else {
545 MemoryBufferRef MBRef = Obj.getBuffer();
546 if (!MBRef.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000547 addFile(createObjectFile(MBRef));
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000548 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000549}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000550
Peter Collingbourne892d49802016-04-27 00:05:03 +0000551// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000552template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000553 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000554 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000555 if (InputFile *File = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000556 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000557}
558
Rui Ueyama93bfee52015-10-13 18:10:33 +0000559// This function takes care of the case in which shared libraries depend on
560// the user program (not the other way, which is usual). Shared libraries
561// may have undefined symbols, expecting that the user program provides
562// the definitions for them. An example is BSD's __progname symbol.
563// We need to put such symbols to the main program's .dynsym so that
564// shared libraries can find them.
565// Except this, we ignore undefined symbols in DSOs.
566template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000567 for (SharedFile<ELFT> *File : SharedFiles)
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000568 for (StringRef U : File->getUndefinedSymbols())
569 if (SymbolBody *Sym = find(U))
570 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000571 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000572}
573
Rui Ueyamadad2b882016-09-02 22:15:08 +0000574// This function processes --export-dynamic-symbol and --dynamic-list.
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000575template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
576 for (StringRef S : Config->DynamicList)
577 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000578 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000579}
580
George Rimar50dcece2016-07-16 12:26:39 +0000581static void setVersionId(SymbolBody *Body, StringRef VersionName,
582 StringRef Name, uint16_t Version) {
583 if (!Body || Body->isUndefined()) {
584 if (Config->NoUndefinedVersion)
585 error("version script assignment of " + VersionName + " to symbol " +
586 Name + " failed: symbol not defined");
587 return;
588 }
589
590 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000591 if (Sym->VersionId != Config->DefaultSymbolVersion)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000592 warn("duplicate symbol " + Name + " in version script");
George Rimar50dcece2016-07-16 12:26:39 +0000593 Sym->VersionId = Version;
594}
595
Rui Ueyamafbde7102016-09-13 20:41:06 +0000596// Returns a map from demangled symbols to symbol objects.
597// The relationship is 1:N instead of 1:1 because with the symbol
598// versioning, more than one symbol may have the same name.
George Rimar50dcece2016-07-16 12:26:39 +0000599template <class ELFT>
George Rimar31c25ae2016-09-15 12:44:38 +0000600std::map<std::string, std::vector<SymbolBody *>>
601SymbolTable<ELFT>::getDemangledSyms() {
602 std::map<std::string, std::vector<SymbolBody *>> Result;
Rui Ueyamad6328522016-07-18 01:34:57 +0000603 for (Symbol *Sym : SymVector) {
604 SymbolBody *B = Sym->body();
George Rimar31c25ae2016-09-15 12:44:38 +0000605 Result[demangle(B->getName())].push_back(B);
Rui Ueyamad6328522016-07-18 01:34:57 +0000606 }
George Rimar50dcece2016-07-16 12:26:39 +0000607 return Result;
608}
609
610static bool hasExternCpp() {
611 for (VersionDefinition &V : Config->VersionDefinitions)
612 for (SymbolVersion Sym : V.Globals)
613 if (Sym.IsExternCpp)
614 return true;
615 return false;
616}
617
George Rimar31c25ae2016-09-15 12:44:38 +0000618static ArrayRef<SymbolBody *>
619findDemangled(std::map<std::string, std::vector<SymbolBody *>> &D,
620 StringRef Name) {
George Rimarc3ec9d02016-08-30 09:29:37 +0000621 auto I = D.find(Name);
622 if (I != D.end())
623 return I->second;
George Rimar31c25ae2016-09-15 12:44:38 +0000624 return {};
George Rimarc3ec9d02016-08-30 09:29:37 +0000625}
626
George Rimar397cd87a2016-08-30 09:35:03 +0000627static std::vector<SymbolBody *>
George Rimar31c25ae2016-09-15 12:44:38 +0000628findAllDemangled(const std::map<std::string, std::vector<SymbolBody *>> &D,
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000629 StringMatcher &M) {
George Rimar397cd87a2016-08-30 09:35:03 +0000630 std::vector<SymbolBody *> Res;
631 for (auto &P : D) {
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000632 if (M.match(P.first))
George Rimar31c25ae2016-09-15 12:44:38 +0000633 for (SymbolBody *Body : P.second)
634 if (!Body->isUndefined())
635 Res.push_back(Body);
George Rimar397cd87a2016-08-30 09:35:03 +0000636 }
637 return Res;
638}
639
Rui Ueyamaea265042016-09-13 20:51:30 +0000640// If there's only one anonymous version definition in a version
641// script file, the script does not actullay define any symbol version,
642// but just specifies symbols visibilities. We assume that the script was
643// in the form of { global: foo; bar; local *; }. So, local is default.
644// In this function, we make specified symbols global.
645template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
646 std::vector<StringRef> Patterns;
647 for (SymbolVersion &Sym : Config->VersionScriptGlobals) {
648 if (hasWildcard(Sym.Name)) {
649 Patterns.push_back(Sym.Name);
650 continue;
651 }
652 if (SymbolBody *B = find(Sym.Name))
653 B->symbol()->VersionId = VER_NDX_GLOBAL;
654 }
655 if (Patterns.empty())
656 return;
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000657 StringMatcher M(Patterns);
658 std::vector<SymbolBody *> Syms = findAll(M);
Rui Ueyamaea265042016-09-13 20:51:30 +0000659 for (SymbolBody *B : Syms)
660 B->symbol()->VersionId = VER_NDX_GLOBAL;
661}
662
Rui Ueyamadad2b882016-09-02 22:15:08 +0000663// This function processes version scripts by updating VersionId
664// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000665template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000666 // Handle edge cases first.
George Rimard3566302016-06-20 11:55:12 +0000667 if (!Config->VersionScriptGlobals.empty()) {
Rui Ueyamaea265042016-09-13 20:51:30 +0000668 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000669 return;
670 }
671
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000672 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000673 return;
674
Rui Ueyamadad2b882016-09-02 22:15:08 +0000675 // Now we have version definitions, so we need to set version ids to symbols.
676 // Each version definition has a glob pattern, and all symbols that match
677 // with the pattern get that version.
678
679 // Users can use "extern C++ {}" directive to match against demangled
680 // C++ symbols. For example, you can write a pattern such as
681 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
682 // other than trying to match a regexp against all demangled symbols.
683 // So, if "extern C++" feature is used, we demangle all known symbols.
George Rimar31c25ae2016-09-15 12:44:38 +0000684 std::map<std::string, std::vector<SymbolBody *>> Demangled;
George Rimar50dcece2016-07-16 12:26:39 +0000685 if (hasExternCpp())
686 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000687
Rui Ueyamadad2b882016-09-02 22:15:08 +0000688 // First, we assign versions to exact matching symbols,
689 // i.e. version definitions not containing any glob meta-characters.
George Rimar50dcece2016-07-16 12:26:39 +0000690 for (VersionDefinition &V : Config->VersionDefinitions) {
691 for (SymbolVersion Sym : V.Globals) {
George Rimarcd574a52016-09-09 14:35:36 +0000692 if (Sym.HasWildcards)
George Rimardd64bb32016-07-13 08:19:04 +0000693 continue;
George Rimar31c25ae2016-09-15 12:44:38 +0000694
George Rimarc3ec9d02016-08-30 09:29:37 +0000695 StringRef N = Sym.Name;
George Rimar31c25ae2016-09-15 12:44:38 +0000696 if (Sym.IsExternCpp) {
697 for (SymbolBody *B : findDemangled(Demangled, N))
698 setVersionId(B, V.Name, N, V.Id);
699 continue;
700 }
701 setVersionId(find(N), V.Name, N, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000702 }
George Rimarbb6c01e2016-11-12 07:04:15 +0000703 for (SymbolVersion Sym : V.Locals) {
704 if (Sym.HasWildcards)
705 continue;
706 setVersionId(find(Sym.Name), V.Name, Sym.Name, VER_NDX_LOCAL);
707 }
George Rimarf73a2582016-07-07 07:45:27 +0000708 }
709
Rui Ueyamadad2b882016-09-02 22:15:08 +0000710 // Next, we assign versions to fuzzy matching symbols,
711 // i.e. version definitions containing glob meta-characters.
712 // Note that because the last match takes precedence over previous matches,
713 // we iterate over the definitions in the reverse order.
George Rimarbb6c01e2016-11-12 07:04:15 +0000714 auto assignFuzzyVersion = [&](SymbolVersion &Sym, size_t Version) {
715 if (!Sym.HasWildcards)
716 return;
717 StringMatcher M({Sym.Name});
718 std::vector<SymbolBody *> Syms =
719 Sym.IsExternCpp ? findAllDemangled(Demangled, M) : findAll(M);
720 // Exact matching takes precendence over fuzzy matching,
721 // so we set a version to a symbol only if no version has been assigned
722 // to the symbol. This behavior is compatible with GNU.
723 for (SymbolBody *B : Syms)
724 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
725 B->symbol()->VersionId = Version;
726 };
727
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000728 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
729 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimarbb6c01e2016-11-12 07:04:15 +0000730 for (SymbolVersion &Sym : V.Locals)
731 assignFuzzyVersion(Sym, VER_NDX_LOCAL);
732 for (SymbolVersion &Sym : V.Globals)
733 assignFuzzyVersion(Sym, V.Id);
George Rimard3566302016-06-20 11:55:12 +0000734 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000735}
736
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000737template class elf::SymbolTable<ELF32LE>;
738template class elf::SymbolTable<ELF32BE>;
739template class elf::SymbolTable<ELF64LE>;
740template class elf::SymbolTable<ELF64BE>;