blob: e859e39d22c50dd0d17a65680ccb97840cc6114b [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"
Rui Ueyamacd236a92016-11-17 19:57:43 +000024#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000027using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000028using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rui Ueyamac9559d92016-01-05 20:47:37 +000033// All input object files must be for the same architecture
34// (e.g. it does not make sense to link x86 object files with
35// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000036template <class ELFT> static bool isCompatible(InputFile *F) {
37 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000038 return true;
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000039 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
40 if (Config->EMachine != EM_MIPS)
41 return true;
42 if (isMipsN32Abi(F) == Config->MipsN32Abi)
43 return true;
44 }
Rui Ueyama25b44c92015-12-16 23:31:22 +000045 StringRef A = F->getName();
46 StringRef B = Config->Emulation;
47 if (B.empty())
48 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000049 error(A + " is incompatible with " + B);
50 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000051}
52
Rui Ueyamac9559d92016-01-05 20:47:37 +000053// Add symbols in File to the symbol table.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000054template <class ELFT> void SymbolTable<ELFT>::addFile(InputFile *File) {
55 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000056 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000057
Michael J. Spencera9424f32016-09-09 22:08:04 +000058 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000059 if (auto *F = dyn_cast<BinaryFile>(File)) {
Rafael Espindola093abab2016-10-27 17:45:40 +000060 BinaryFiles.push_back(F);
61 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000062 return;
63 }
64
Rui Ueyama89575742015-12-16 22:59:13 +000065 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000066 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000067 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000068 return;
69 }
Rui Ueyama3d451792015-10-12 18:03:21 +000070
George Rimar2a78fce2016-04-13 18:07:57 +000071 // Lazy object file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000072 if (auto *F = dyn_cast<LazyObjectFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000073 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000074 return;
75 }
76
77 if (Config->Trace)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000078 outs() << getFilename(File) << "\n";
George Rimar2a78fce2016-04-13 18:07:57 +000079
Rui Ueyama89575742015-12-16 22:59:13 +000080 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000081 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000082 // DSOs are uniquified not by filename but by soname.
83 F->parseSoName();
George Rimarbcba39a2016-11-02 10:16:25 +000084 if (HasError || !SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000085 return;
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000086 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000087 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000088 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000089 }
Rui Ueyama89575742015-12-16 22:59:13 +000090
Rui Ueyamaf8baa662016-04-07 19:24:51 +000091 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000092 if (auto *F = dyn_cast<BitcodeFile>(File)) {
93 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +000094 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000095 return;
96 }
97
Rui Ueyamaf8baa662016-04-07 19:24:51 +000098 // Regular object file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000099 auto *F = cast<ObjectFile<ELFT>>(File);
100 ObjectFiles.push_back(F);
Rui Ueyama52d3b672016-01-06 02:06:33 +0000101 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000102}
103
Rui Ueyama42554752016-04-23 00:26:32 +0000104// This function is where all the optimizations of link-time
105// optimization happens. When LTO is in use, some input files are
106// not in native object file format but in the LLVM bitcode format.
107// This function compiles bitcode files into a few big native files
108// using LLVM functions and replaces bitcode symbols with the results.
109// Because all bitcode files that consist of a program are passed
110// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000111template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
112 if (BitcodeFiles.empty())
113 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000114
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000115 // Compile bitcode files and replace bitcode symbols.
Rui Ueyama25992482016-03-22 20:52:10 +0000116 Lto.reset(new BitcodeCompiler);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000117 for (BitcodeFile *F : BitcodeFiles)
Rui Ueyama25992482016-03-22 20:52:10 +0000118 Lto->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000119
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000120 for (InputFile *File : Lto->compile()) {
121 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(File);
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000122 DenseSet<CachedHashStringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000123 Obj->parse(DummyGroups);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000124 ObjectFiles.push_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000125 }
126}
127
Rafael Espindola0e604f92015-09-25 18:56:53 +0000128template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000129DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
130 uint8_t Visibility) {
George Rimar463984d2016-11-15 08:07:14 +0000131 Symbol *Sym = addRegular(Name, Visibility, STT_NOTYPE, 0, 0, STB_GLOBAL,
132 nullptr, nullptr);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000133 return cast<DefinedRegular<ELFT>>(Sym->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000134}
135
Rui Ueyamac9559d92016-01-05 20:47:37 +0000136// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000137// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000138template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000139DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
140 uint8_t Visibility) {
Rafael Espindola95eae572016-11-16 18:01:41 +0000141 SymbolBody *S = find(Name);
142 if (!S || !S->isUndefined())
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000143 return nullptr;
144 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000145}
146
Rui Ueyama69c778c2016-07-17 17:50:09 +0000147// Set a flag for --trace-symbol so that we can print out a log message
148// if a new symbol with the same name is inserted into the symbol table.
149template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000150 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000151}
152
Rui Ueyamadeb15402016-01-07 17:20:07 +0000153// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
154// Used to implement --wrap.
155template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000156 SymbolBody *B = find(Name);
157 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000158 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000159 Symbol *Sym = B->symbol();
160 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
161 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000162
Peter Collingbourne4f952702016-05-01 04:55:03 +0000163 // We rename symbols by replacing the old symbol's SymbolBody with the new
164 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
165 // old symbol to instead refer to the new symbol.
166 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
167 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000168}
169
Peter Collingbournedadcc172016-04-22 18:42:48 +0000170static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
171 if (VA == STV_DEFAULT)
172 return VB;
173 if (VB == STV_DEFAULT)
174 return VA;
175 return std::min(VA, VB);
176}
177
Rui Ueyamadace8382016-07-21 13:13:21 +0000178// Parses a symbol in the form of <name>@<version> or <name>@@<version>.
179static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) {
180 if (Config->VersionDefinitions.empty())
181 return {S, Config->DefaultSymbolVersion};
182
183 size_t Pos = S.find('@');
184 if (Pos == 0 || Pos == StringRef::npos)
185 return {S, Config->DefaultSymbolVersion};
186
187 StringRef Name = S.substr(0, Pos);
188 StringRef Verstr = S.substr(Pos + 1);
189 if (Verstr.empty())
190 return {S, Config->DefaultSymbolVersion};
191
192 // '@@' in a symbol name means the default version.
193 // It is usually the most recent one.
194 bool IsDefault = (Verstr[0] == '@');
195 if (IsDefault)
196 Verstr = Verstr.substr(1);
197
198 for (VersionDefinition &V : Config->VersionDefinitions) {
199 if (V.Name == Verstr)
200 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)};
201 }
202
203 // It is an error if the specified version was not defined.
204 error("symbol " + S + " has undefined version " + Verstr);
205 return {S, Config->DefaultSymbolVersion};
206}
207
Rui Ueyamab4de5952016-01-08 22:01:33 +0000208// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000209template <class ELFT>
Rui Ueyamadace8382016-07-21 13:13:21 +0000210std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000211 auto P = Symtab.insert(
212 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000213 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000214 bool IsNew = P.second;
215
Rui Ueyamae3357902016-07-18 01:35:00 +0000216 if (V.Idx == -1) {
217 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000218 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000219 }
220
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000221 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000222 if (IsNew) {
Rui Ueyama55518e72016-10-28 20:57:25 +0000223 Sym = new (BAlloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000224 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000225 Sym->Visibility = STV_DEFAULT;
226 Sym->IsUsedInRegularObj = false;
227 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000228 Sym->Traced = V.Traced;
Rui Ueyamadace8382016-07-21 13:13:21 +0000229 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000230 SymVector.push_back(Sym);
231 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000232 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000233 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000234 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000235}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000236
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000237// Construct a string in the form of "Sym in File1 and File2".
238// Used to construct an error message.
239static std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile) {
Eugene Leviant825e5382016-11-08 16:26:32 +0000240 return "'" + maybeDemangle(Existing->getName()) + "' in " +
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000241 getFilename(Existing->File) + " and " + getFilename(NewFile);
242}
243
Peter Collingbourne4f952702016-05-01 04:55:03 +0000244// Find an existing symbol or create and insert a new one, then apply the given
245// attributes.
246template <class ELFT>
247std::pair<Symbol *, bool>
Rui Ueyamadace8382016-07-21 13:13:21 +0000248SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
Davide Italiano786d8e32016-09-29 00:40:08 +0000249 bool CanOmitFromDynSym, InputFile *File) {
Rafael Espindola05098762016-08-31 13:49:23 +0000250 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000251 Symbol *S;
252 bool WasInserted;
253 std::tie(S, WasInserted) = insert(Name);
254
255 // Merge in the new symbol's visibility.
256 S->Visibility = getMinVisibility(S->Visibility, Visibility);
257 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
258 S->ExportDynamic = true;
259 if (IsUsedInRegularObj)
260 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000261 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
262 ((Type == STT_TLS) != S->body()->isTls()))
Eugene Leviant825e5382016-11-08 16:26:32 +0000263 error("TLS attribute mismatch for symbol " + conflictMsg(S->body(), File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000264
265 return {S, WasInserted};
266}
267
Peter Collingbourne4f952702016-05-01 04:55:03 +0000268template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
269 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Davide Italiano786d8e32016-09-29 00:40:08 +0000270 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000271}
272
273template <class ELFT>
274Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
275 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000276 bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000277 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000278 Symbol *S;
279 bool WasInserted;
280 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000281 insert(Name, Type, StOther & 3, CanOmitFromDynSym, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000282 if (WasInserted) {
283 S->Binding = Binding;
Rui Ueyama434b5612016-07-17 03:11:46 +0000284 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000285 return S;
286 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000287 if (Binding != STB_WEAK) {
288 if (S->body()->isShared() || S->body()->isLazy())
289 S->Binding = Binding;
290 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000291 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000292 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000293 if (auto *L = dyn_cast<Lazy>(S->body())) {
294 // An undefined weak will not fetch archive members, but we have to remember
295 // its type. See also comment in addLazyArchive.
296 if (S->isWeak())
297 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000298 else if (InputFile *F = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000299 addFile(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000300 }
301 return S;
302}
303
304// We have a new defined symbol with the specified binding. Return 1 if the new
305// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
306// strong defined symbols.
307static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
308 if (WasInserted)
309 return 1;
310 SymbolBody *Body = S->body();
311 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
312 return 1;
313 if (Binding == STB_WEAK)
314 return -1;
315 if (S->isWeak())
316 return 1;
317 return 0;
318}
319
320// We have a new non-common defined symbol with the specified binding. Return 1
321// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
322// is a conflict. If the new symbol wins, also update the binding.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000323static int compareDefinedNonCommon(Symbol *S, bool WasInserted,
324 uint8_t Binding) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000325 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
326 if (Cmp > 0)
327 S->Binding = Binding;
328 return Cmp;
329 }
Rafael Espindolae7553e42016-08-31 13:28:33 +0000330 if (isa<DefinedCommon>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000331 // Non-common symbols take precedence over common symbols.
332 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000333 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000334 return 1;
335 }
336 return 0;
337}
338
339template <class ELFT>
340Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
341 uint64_t Alignment, uint8_t Binding,
342 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000343 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000344 Symbol *S;
345 bool WasInserted;
Davide Italiano786d8e32016-09-29 00:40:08 +0000346 std::tie(S, WasInserted) =
347 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000348 int Cmp = compareDefined(S, WasInserted, Binding);
349 if (Cmp > 0) {
350 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000351 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000352 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000353 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000354 if (!C) {
355 // Non-common symbols take precedence over common symbols.
356 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000357 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000358 return S;
359 }
360
361 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000362 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000363
Rafael Espindola8db87292016-08-31 13:42:08 +0000364 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
365 if (Size > C->Size)
366 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000367 }
368 return S;
369}
370
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000371static void print(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000372 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000373 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000374 else
375 error(Msg);
376}
377
Eugene Leviant825e5382016-11-08 16:26:32 +0000378static void reportDuplicate(SymbolBody *Existing, InputFile *NewFile) {
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000379 print("duplicate symbol " + conflictMsg(Existing, NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000380}
381
382template <class ELFT>
383static void reportDuplicate(SymbolBody *Existing,
384 InputSectionBase<ELFT> *ErrSec,
385 typename ELFT::uint ErrOffset) {
386 DefinedRegular<ELFT> *D = dyn_cast<DefinedRegular<ELFT>>(Existing);
387 if (!D || !D->Section || !ErrSec) {
388 reportDuplicate(Existing, ErrSec ? ErrSec->getFile() : nullptr);
389 return;
390 }
391
Rui Ueyamaedc183e2016-11-08 20:30:19 +0000392 std::string OldLoc = getLocation(*D->Section, D->Value);
393 std::string NewLoc = getLocation(*ErrSec, ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000394
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000395 print(NewLoc + ": duplicate symbol '" + maybeDemangle(Existing->getName()) +
396 "'");
397 print(OldLoc + ": previous definition was here");
Eugene Leviant825e5382016-11-08 16:26:32 +0000398}
399
Peter Collingbourne4f952702016-05-01 04:55:03 +0000400template <typename ELFT>
401Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
George Rimar463984d2016-11-15 08:07:14 +0000402 InputSectionBase<ELFT> *Section,
403 InputFile *File) {
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000404 return addRegular(Name, Sym.st_other, Sym.getType(), Sym.st_value,
George Rimar463984d2016-11-15 08:07:14 +0000405 Sym.st_size, Sym.getBinding(), Section, File);
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000406}
407
408template <typename ELFT>
409Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
410 uint8_t Type, uintX_t Value, uintX_t Size,
411 uint8_t Binding,
George Rimar463984d2016-11-15 08:07:14 +0000412 InputSectionBase<ELFT> *Section,
413 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000414 Symbol *S;
415 bool WasInserted;
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000416 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3,
George Rimar463984d2016-11-15 08:07:14 +0000417 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000418 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000419 if (Cmp > 0)
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000420 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, Value, Size,
George Rimar463984d2016-11-15 08:07:14 +0000421 Section, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000422 else if (Cmp == 0)
Eugene Leviant825e5382016-11-08 16:26:32 +0000423 reportDuplicate(S->body(), Section, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000424 return S;
425}
426
427template <typename ELFT>
Eugene Leviantafaa9342016-11-16 09:49:39 +0000428Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
429 const OutputSectionBase *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000430 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000431 Symbol *S;
432 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000433 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
Davide Italiano786d8e32016-09-29 00:40:08 +0000434 /*CanOmitFromDynSym*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000435 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000436 if (Cmp > 0)
437 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
438 else if (Cmp == 0)
439 reportDuplicate(S->body(), nullptr);
440 return S;
441}
442
443template <typename ELFT>
444void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
445 const Elf_Sym &Sym,
446 const typename ELFT::Verdef *Verdef) {
447 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
448 // as the visibility, which will leave the visibility in the symbol table
449 // unchanged.
450 Symbol *S;
451 bool WasInserted;
452 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000453 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000454 // Make sure we preempt DSO symbols with default visibility.
455 if (Sym.getVisibility() == STV_DEFAULT)
456 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000457 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000458 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000459 if (!S->isWeak())
460 F->IsUsed = true;
461 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000462}
463
464template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000465Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000466 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000467 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000468 Symbol *S;
469 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000470 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000471 insert(Name, Type, StOther & 3, CanOmitFromDynSym, F);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000472 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000473 if (Cmp > 0)
Rafael Espindolaa6c97442016-08-31 12:30:34 +0000474 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000475 else if (Cmp == 0)
476 reportDuplicate(S->body(), F);
477 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000478}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000479
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000480template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000481 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000482 if (It == Symtab.end())
483 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000484 SymIndex V = It->second;
485 if (V.Idx == -1)
486 return nullptr;
487 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000488}
489
Rui Ueyama82492142016-11-15 18:41:52 +0000490// Returns a list of defined symbols that match with a given pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000491template <class ELFT>
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000492std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef GlobPat) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000493 std::vector<SymbolBody *> Res;
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000494 StringMatcher M({GlobPat});
Rui Ueyamad6328522016-07-18 01:34:57 +0000495 for (Symbol *Sym : SymVector) {
496 SymbolBody *B = Sym->body();
George Rimarc91930a2016-09-02 21:17:20 +0000497 StringRef Name = B->getName();
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000498 if (!B->isUndefined() && M.match(Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000499 Res.push_back(B);
500 }
501 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000502}
503
504template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000505void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
506 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000507 Symbol *S;
508 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000509 StringRef Name = Sym.getName();
510 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000511 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000512 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000513 return;
514 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515 if (!S->body()->isUndefined())
516 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000517
Peter Collingbourne4f952702016-05-01 04:55:03 +0000518 // Weak undefined symbols should not fetch members from archives. If we were
519 // to keep old symbol we would not know that an archive member was available
520 // if a strong undefined symbol shows up afterwards in the link. If a strong
521 // undefined symbol never shows up, this lazy symbol will get to the end of
522 // the link and must be treated as the weak undefined one. We already marked
523 // this symbol as used when we added it to the symbol table, but we also need
524 // to preserve its type. FIXME: Move the Type field to Symbol.
525 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000526 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000527 return;
528 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000529 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
530 if (!MBInfo.first.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000531 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000532}
533
534template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000535void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 Symbol *S;
537 bool WasInserted;
538 std::tie(S, WasInserted) = insert(Name);
539 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000540 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000541 return;
542 }
543 if (!S->body()->isUndefined())
544 return;
545
546 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000547 if (S->isWeak()) {
548 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
549 } else {
550 MemoryBufferRef MBRef = Obj.getBuffer();
551 if (!MBRef.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000552 addFile(createObjectFile(MBRef));
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000553 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000554}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000555
Peter Collingbourne892d49802016-04-27 00:05:03 +0000556// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000557template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000558 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000559 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000560 if (InputFile *File = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000561 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000562}
563
Rui Ueyama93bfee52015-10-13 18:10:33 +0000564// This function takes care of the case in which shared libraries depend on
565// the user program (not the other way, which is usual). Shared libraries
566// may have undefined symbols, expecting that the user program provides
567// the definitions for them. An example is BSD's __progname symbol.
568// We need to put such symbols to the main program's .dynsym so that
569// shared libraries can find them.
570// Except this, we ignore undefined symbols in DSOs.
571template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000572 for (SharedFile<ELFT> *File : SharedFiles)
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000573 for (StringRef U : File->getUndefinedSymbols())
574 if (SymbolBody *Sym = find(U))
575 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000576 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000577}
578
Rui Ueyamadad2b882016-09-02 22:15:08 +0000579// This function processes --export-dynamic-symbol and --dynamic-list.
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000580template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
581 for (StringRef S : Config->DynamicList)
582 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000583 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000584}
585
Rui Ueyama82492142016-11-15 18:41:52 +0000586// Initialize DemangledSyms with a map from demangled symbols to symbol
587// objects. Used to handle "extern C++" directive in version scripts.
588//
589// The map will contain all demangled symbols. That can be very large,
590// and in LLD we generally want to avoid do anything for each symbol.
591// Then, why are we doing this? Here's why.
592//
593// Users can use "extern C++ {}" directive to match against demangled
594// C++ symbols. For example, you can write a pattern such as
595// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
596// other than trying to match a pattern against all demangled symbols.
597// So, if "extern C++" feature is used, we need to demangle all known
598// symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000599template <class ELFT>
Rui Ueyama82492142016-11-15 18:41:52 +0000600void SymbolTable<ELFT>::initDemangledSyms() {
601 if (DemangledSyms)
602 return;
603 DemangledSyms.emplace();
604
Rui Ueyamad6328522016-07-18 01:34:57 +0000605 for (Symbol *Sym : SymVector) {
606 SymbolBody *B = Sym->body();
Rui Ueyama82492142016-11-15 18:41:52 +0000607 (*DemangledSyms)[demangle(B->getName())].push_back(B);
Rui Ueyamad6328522016-07-18 01:34:57 +0000608 }
George Rimar50dcece2016-07-16 12:26:39 +0000609}
610
Rui Ueyama82492142016-11-15 18:41:52 +0000611template <class ELFT>
612ArrayRef<SymbolBody *> SymbolTable<ELFT>::findDemangled(StringRef Name) {
613 initDemangledSyms();
614 auto I = DemangledSyms->find(Name);
615 if (I != DemangledSyms->end())
George Rimarc3ec9d02016-08-30 09:29:37 +0000616 return I->second;
George Rimar31c25ae2016-09-15 12:44:38 +0000617 return {};
George Rimarc3ec9d02016-08-30 09:29:37 +0000618}
619
Rui Ueyama82492142016-11-15 18:41:52 +0000620template <class ELFT>
621std::vector<SymbolBody *>
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000622SymbolTable<ELFT>::findAllDemangled(StringRef GlobPat) {
Rui Ueyama82492142016-11-15 18:41:52 +0000623 initDemangledSyms();
George Rimar397cd87a2016-08-30 09:35:03 +0000624 std::vector<SymbolBody *> Res;
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000625 StringMatcher M({GlobPat});
Rui Ueyama82492142016-11-15 18:41:52 +0000626 for (auto &P : *DemangledSyms)
627 if (M.match(P.first()))
George Rimar31c25ae2016-09-15 12:44:38 +0000628 for (SymbolBody *Body : P.second)
629 if (!Body->isUndefined())
630 Res.push_back(Body);
George Rimar397cd87a2016-08-30 09:35:03 +0000631 return Res;
632}
633
Rui Ueyamaea265042016-09-13 20:51:30 +0000634// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000635// script file, the script does not actually define any symbol version,
Rui Ueyamaea265042016-09-13 20:51:30 +0000636// but just specifies symbols visibilities. We assume that the script was
637// in the form of { global: foo; bar; local *; }. So, local is default.
638// In this function, we make specified symbols global.
639template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
Rui Ueyamabaf7ee32016-11-15 17:51:09 +0000640 for (SymbolVersion &Ver : Config->VersionScriptGlobals) {
641 if (hasWildcard(Ver.Name)) {
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000642 for (SymbolBody *B : findAll(Ver.Name))
Rui Ueyama77d917d2016-11-17 03:19:34 +0000643 B->symbol()->VersionId = VER_NDX_GLOBAL;
Rui Ueyamaea265042016-09-13 20:51:30 +0000644 continue;
645 }
Rui Ueyamabaf7ee32016-11-15 17:51:09 +0000646 if (SymbolBody *B = find(Ver.Name))
Rui Ueyamaea265042016-09-13 20:51:30 +0000647 B->symbol()->VersionId = VER_NDX_GLOBAL;
648 }
Rui Ueyamaea265042016-09-13 20:51:30 +0000649}
650
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000651// Set symbol versions to symbols. This function handles patterns
652// containing no wildcard characters.
653template <class ELFT>
Rui Ueyamada805c42016-11-17 03:39:21 +0000654void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000655 StringRef VersionName) {
656 if (Ver.HasWildcards)
657 return;
658
659 // Get a list of symbols which we need to assign the version to.
660 std::vector<SymbolBody *> Syms;
661 if (Ver.IsExternCpp)
662 Syms = findDemangled(Ver.Name);
663 else
664 Syms.push_back(find(Ver.Name));
665
666 // Assign the version.
667 for (SymbolBody *B : Syms) {
668 if (!B || B->isUndefined()) {
669 if (Config->NoUndefinedVersion)
670 error("version script assignment of '" + VersionName + "' to symbol " +
671 Ver.Name + " failed: symbol not defined");
672 continue;
673 }
674
675 if (B->symbol()->VersionId != Config->DefaultSymbolVersion)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000676 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000677 B->symbol()->VersionId = VersionId;
678 }
679}
680
Rui Ueyama82492142016-11-15 18:41:52 +0000681template <class ELFT>
682void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
Rui Ueyamada805c42016-11-17 03:39:21 +0000683 uint16_t VersionId) {
Rui Ueyama82492142016-11-15 18:41:52 +0000684 if (!Ver.HasWildcards)
685 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000686 std::vector<SymbolBody *> Syms =
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000687 Ver.IsExternCpp ? findAllDemangled(Ver.Name) : findAll(Ver.Name);
Rui Ueyama82492142016-11-15 18:41:52 +0000688
689 // Exact matching takes precendence over fuzzy matching,
690 // so we set a version to a symbol only if no version has been assigned
691 // to the symbol. This behavior is compatible with GNU.
692 for (SymbolBody *B : Syms)
693 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
694 B->symbol()->VersionId = VersionId;
695}
696
Rui Ueyamadad2b882016-09-02 22:15:08 +0000697// This function processes version scripts by updating VersionId
698// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000699template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000700 // Handle edge cases first.
George Rimard3566302016-06-20 11:55:12 +0000701 if (!Config->VersionScriptGlobals.empty()) {
Rui Ueyamaea265042016-09-13 20:51:30 +0000702 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000703 return;
704 }
705
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000706 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000707 return;
708
Rui Ueyamadad2b882016-09-02 22:15:08 +0000709 // Now we have version definitions, so we need to set version ids to symbols.
710 // Each version definition has a glob pattern, and all symbols that match
711 // with the pattern get that version.
712
Rui Ueyamadad2b882016-09-02 22:15:08 +0000713 // First, we assign versions to exact matching symbols,
714 // i.e. version definitions not containing any glob meta-characters.
Rui Ueyama96db27c2016-11-17 19:57:45 +0000715 for (SymbolVersion &Ver : Config->VersionScriptLocals)
716 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
George Rimar17c65af2016-11-16 18:46:23 +0000717 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000718 for (SymbolVersion &Ver : V.Globals)
719 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000720
Rui Ueyamadad2b882016-09-02 22:15:08 +0000721 // Next, we assign versions to fuzzy matching symbols,
722 // i.e. version definitions containing glob meta-characters.
723 // Note that because the last match takes precedence over previous matches,
724 // we iterate over the definitions in the reverse order.
George Rimar17c65af2016-11-16 18:46:23 +0000725 for (SymbolVersion &Ver : Config->VersionScriptLocals)
726 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamacd236a92016-11-17 19:57:43 +0000727 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
728 for (SymbolVersion &Ver : V.Globals)
729 assignWildcardVersion(Ver, V.Id);
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000730}
731
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000732template class elf::SymbolTable<ELF32LE>;
733template class elf::SymbolTable<ELF32BE>;
734template class elf::SymbolTable<ELF64LE>;
735template class elf::SymbolTable<ELF64BE>;