blob: 863e1c893d0df3b02f0236b041305c5f39e68de9 [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000021#include "Symbols.h"
Rui Ueyama520d9162016-12-08 18:31:13 +000022#include "lld/Support/Memory.h"
Rui Ueyamacd236a92016-11-17 19:57:43 +000023#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
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;
Rui Ueyama26081ca2016-11-24 20:59:44 +000038
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 Ueyama26081ca2016-11-24 20:59:44 +000045
46 if (!Config->Emulation.empty())
47 error(toString(F) + " is incompatible with " + Config->Emulation);
48 else
49 error(toString(F) + " is incompatible with " + toString(Config->FirstElf));
Rui Ueyama16ba6692016-01-29 19:41:13 +000050 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 Ueyama3fc0f7e2016-11-23 18:07:33 +000078 outs() << toString(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();
Rui Ueyamaf373dd72016-11-24 01:43:21 +000084 if (ErrorCount || !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.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000111template <class ELFT> void SymbolTable<ELFT>::addCombinedLTOObject() {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000112 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.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000116 LTO.reset(new BitcodeCompiler);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000117 for (BitcodeFile *F : BitcodeFiles)
Davide Italiano3bfa0812016-11-26 05:37:04 +0000118 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000119
Davide Italiano3bfa0812016-11-26 05:37:04 +0000120 for (InputFile *File : LTO->compile()) {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000121 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,
Simon Atanasyan6a4eb752016-12-08 06:19:47 +0000130 uint8_t Visibility,
Simon Atanasyan872764f2016-12-08 15:29:17 +0000131 uint8_t Binding) {
Simon Atanasyan6a4eb752016-12-08 06:19:47 +0000132 Symbol *Sym =
Simon Atanasyan872764f2016-12-08 15:29:17 +0000133 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, nullptr, nullptr);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000134 return cast<DefinedRegular<ELFT>>(Sym->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000135}
136
Rui Ueyamac9559d92016-01-05 20:47:37 +0000137// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000138// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000139template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000140DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
141 uint8_t Visibility) {
Rafael Espindola95eae572016-11-16 18:01:41 +0000142 SymbolBody *S = find(Name);
143 if (!S || !S->isUndefined())
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000144 return nullptr;
145 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000146}
147
Rui Ueyama69c778c2016-07-17 17:50:09 +0000148// Set a flag for --trace-symbol so that we can print out a log message
149// if a new symbol with the same name is inserted into the symbol table.
150template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000151 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000152}
153
Rui Ueyamadeb15402016-01-07 17:20:07 +0000154// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
155// Used to implement --wrap.
156template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000157 SymbolBody *B = find(Name);
158 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000159 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000160 Symbol *Sym = B->symbol();
161 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
162 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000163
Peter Collingbourne4f952702016-05-01 04:55:03 +0000164 // We rename symbols by replacing the old symbol's SymbolBody with the new
165 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
166 // old symbol to instead refer to the new symbol.
167 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
168 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000169}
170
Peter Collingbournedadcc172016-04-22 18:42:48 +0000171static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
172 if (VA == STV_DEFAULT)
173 return VB;
174 if (VB == STV_DEFAULT)
175 return VA;
176 return std::min(VA, VB);
177}
178
Rui Ueyamab4de5952016-01-08 22:01:33 +0000179// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000180template <class ELFT>
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000181std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000182 auto P = Symtab.insert(
183 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000184 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000185 bool IsNew = P.second;
186
Rui Ueyamae3357902016-07-18 01:35:00 +0000187 if (V.Idx == -1) {
188 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000189 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000190 }
191
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000192 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000193 if (IsNew) {
Rui Ueyama55518e72016-10-28 20:57:25 +0000194 Sym = new (BAlloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000195 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000196 Sym->Visibility = STV_DEFAULT;
197 Sym->IsUsedInRegularObj = false;
198 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000199 Sym->Traced = V.Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000200 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000201 SymVector.push_back(Sym);
202 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000203 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000204 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000205 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000206}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000207
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000208// Construct a string in the form of "Sym in File1 and File2".
209// Used to construct an error message.
210static std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile) {
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000211 return "'" + toString(*Existing) + "' in " + toString(Existing->File) +
212 " and " + toString(NewFile);
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000213}
214
Peter Collingbourne4f952702016-05-01 04:55:03 +0000215// Find an existing symbol or create and insert a new one, then apply the given
216// attributes.
217template <class ELFT>
218std::pair<Symbol *, bool>
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000219SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
Davide Italiano786d8e32016-09-29 00:40:08 +0000220 bool CanOmitFromDynSym, InputFile *File) {
Rafael Espindola05098762016-08-31 13:49:23 +0000221 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000222 Symbol *S;
223 bool WasInserted;
224 std::tie(S, WasInserted) = insert(Name);
225
226 // Merge in the new symbol's visibility.
227 S->Visibility = getMinVisibility(S->Visibility, Visibility);
228 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
229 S->ExportDynamic = true;
230 if (IsUsedInRegularObj)
231 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000232 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
233 ((Type == STT_TLS) != S->body()->isTls()))
Eugene Leviant825e5382016-11-08 16:26:32 +0000234 error("TLS attribute mismatch for symbol " + conflictMsg(S->body(), File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000235
236 return {S, WasInserted};
237}
238
Peter Collingbourne4f952702016-05-01 04:55:03 +0000239template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000240 return addUndefined(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
241 /*Type*/ 0,
Davide Italiano786d8e32016-09-29 00:40:08 +0000242 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000243}
244
245template <class ELFT>
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000246Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, bool IsLocal,
247 uint8_t Binding, uint8_t StOther,
248 uint8_t Type, bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000249 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000250 Symbol *S;
251 bool WasInserted;
252 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000253 insert(Name, Type, StOther & 3, CanOmitFromDynSym, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000254 if (WasInserted) {
255 S->Binding = Binding;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000256 replaceBody<Undefined>(S, Name, IsLocal, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000257 return S;
258 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000259 if (Binding != STB_WEAK) {
260 if (S->body()->isShared() || S->body()->isLazy())
261 S->Binding = Binding;
262 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000263 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000264 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000265 if (auto *L = dyn_cast<Lazy>(S->body())) {
266 // An undefined weak will not fetch archive members, but we have to remember
267 // its type. See also comment in addLazyArchive.
268 if (S->isWeak())
269 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000270 else if (InputFile *F = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000271 addFile(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000272 }
273 return S;
274}
275
276// We have a new defined symbol with the specified binding. Return 1 if the new
277// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
278// strong defined symbols.
279static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
280 if (WasInserted)
281 return 1;
282 SymbolBody *Body = S->body();
283 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
284 return 1;
285 if (Binding == STB_WEAK)
286 return -1;
287 if (S->isWeak())
288 return 1;
289 return 0;
290}
291
292// We have a new non-common defined symbol with the specified binding. Return 1
293// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
294// is a conflict. If the new symbol wins, also update the binding.
Rafael Espindola858c0922016-12-02 02:58:21 +0000295template <typename ELFT>
296static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
297 bool IsAbsolute, typename ELFT::uint Value) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000298 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
299 if (Cmp > 0)
300 S->Binding = Binding;
301 return Cmp;
302 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000303 SymbolBody *B = S->body();
304 if (isa<DefinedCommon>(B)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000305 // Non-common symbols take precedence over common symbols.
306 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000307 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000308 return 1;
Rafael Espindola858c0922016-12-02 02:58:21 +0000309 } else if (auto *R = dyn_cast<DefinedRegular<ELFT>>(B)) {
310 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
311 R->Value == Value)
312 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000313 }
314 return 0;
315}
316
317template <class ELFT>
318Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
319 uint64_t Alignment, uint8_t Binding,
320 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000321 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000322 Symbol *S;
323 bool WasInserted;
Davide Italiano786d8e32016-09-29 00:40:08 +0000324 std::tie(S, WasInserted) =
325 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000326 int Cmp = compareDefined(S, WasInserted, Binding);
327 if (Cmp > 0) {
328 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000329 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000330 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000331 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000332 if (!C) {
333 // Non-common symbols take precedence over common symbols.
334 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000335 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000336 return S;
337 }
338
339 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000340 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000341
Rafael Espindola8db87292016-08-31 13:42:08 +0000342 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
343 if (Size > C->Size)
344 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000345 }
346 return S;
347}
348
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000349static void print(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000350 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000351 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000352 else
353 error(Msg);
354}
355
Eugene Leviant825e5382016-11-08 16:26:32 +0000356static void reportDuplicate(SymbolBody *Existing, InputFile *NewFile) {
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000357 print("duplicate symbol " + conflictMsg(Existing, NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000358}
359
360template <class ELFT>
361static void reportDuplicate(SymbolBody *Existing,
362 InputSectionBase<ELFT> *ErrSec,
363 typename ELFT::uint ErrOffset) {
364 DefinedRegular<ELFT> *D = dyn_cast<DefinedRegular<ELFT>>(Existing);
365 if (!D || !D->Section || !ErrSec) {
366 reportDuplicate(Existing, ErrSec ? ErrSec->getFile() : nullptr);
367 return;
368 }
369
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000370 std::string OldLoc = D->Section->getLocation(D->Value);
371 std::string NewLoc = ErrSec->getLocation(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000372
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000373 print(NewLoc + ": duplicate symbol '" + toString(*Existing) + "'");
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000374 print(OldLoc + ": previous definition was here");
Eugene Leviant825e5382016-11-08 16:26:32 +0000375}
376
Peter Collingbourne4f952702016-05-01 04:55:03 +0000377template <typename ELFT>
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000378Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
379 uint8_t Type, uintX_t Value, uintX_t Size,
380 uint8_t Binding,
George Rimar463984d2016-11-15 08:07:14 +0000381 InputSectionBase<ELFT> *Section,
382 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000383 Symbol *S;
384 bool WasInserted;
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000385 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3,
George Rimar463984d2016-11-15 08:07:14 +0000386 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola858c0922016-12-02 02:58:21 +0000387 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
388 Section == nullptr, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000389 if (Cmp > 0)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000390 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type,
391 Value, Size, Section, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000392 else if (Cmp == 0)
Eugene Leviant825e5382016-11-08 16:26:32 +0000393 reportDuplicate(S->body(), Section, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000394 return S;
395}
396
397template <typename ELFT>
Eugene Leviantafaa9342016-11-16 09:49:39 +0000398Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
399 const OutputSectionBase *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000400 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000401 Symbol *S;
402 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000403 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
Davide Italiano786d8e32016-09-29 00:40:08 +0000404 /*CanOmitFromDynSym*/ false, nullptr);
Rafael Espindola858c0922016-12-02 02:58:21 +0000405 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, STB_GLOBAL,
406 /*IsAbsolute*/ false, /*Value*/ 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000407 if (Cmp > 0)
408 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
409 else if (Cmp == 0)
410 reportDuplicate(S->body(), nullptr);
411 return S;
412}
413
414template <typename ELFT>
415void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
416 const Elf_Sym &Sym,
417 const typename ELFT::Verdef *Verdef) {
418 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
419 // as the visibility, which will leave the visibility in the symbol table
420 // unchanged.
421 Symbol *S;
422 bool WasInserted;
423 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000424 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000425 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola6239ce62016-12-09 14:12:02 +0000426 if (Sym.getVisibility() == STV_DEFAULT) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000427 S->ExportDynamic = true;
Rafael Espindola6239ce62016-12-09 14:12:02 +0000428 // Exporting preempting symbols takes precedence over linker scripts.
429 if (S->VersionId == VER_NDX_LOCAL)
430 S->VersionId = VER_NDX_GLOBAL;
431 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000432 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000433 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000434 if (!S->isWeak())
435 F->IsUsed = true;
436 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000437}
438
439template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000440Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000441 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000442 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000443 Symbol *S;
444 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000445 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000446 insert(Name, Type, StOther & 3, CanOmitFromDynSym, F);
Rafael Espindola858c0922016-12-02 02:58:21 +0000447 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
448 /*IsAbs*/ false, /*Value*/ 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000449 if (Cmp > 0)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000450 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type,
451 0, 0, nullptr, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000452 else if (Cmp == 0)
453 reportDuplicate(S->body(), F);
454 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000455}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000456
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000457template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000458 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000459 if (It == Symtab.end())
460 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000461 SymIndex V = It->second;
462 if (V.Idx == -1)
463 return nullptr;
464 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000465}
466
Davide Italiano8e1131d2016-06-29 02:46:51 +0000467template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000468void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
469 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000470 Symbol *S;
471 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000472 StringRef Name = Sym.getName();
473 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000474 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000475 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000476 return;
477 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000478 if (!S->body()->isUndefined())
479 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000480
Peter Collingbourne4f952702016-05-01 04:55:03 +0000481 // Weak undefined symbols should not fetch members from archives. If we were
482 // to keep old symbol we would not know that an archive member was available
483 // if a strong undefined symbol shows up afterwards in the link. If a strong
484 // undefined symbol never shows up, this lazy symbol will get to the end of
485 // the link and must be treated as the weak undefined one. We already marked
486 // this symbol as used when we added it to the symbol table, but we also need
487 // to preserve its type. FIXME: Move the Type field to Symbol.
488 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000489 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000490 return;
491 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000492 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
493 if (!MBInfo.first.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000494 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495}
496
497template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000498void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000499 Symbol *S;
500 bool WasInserted;
501 std::tie(S, WasInserted) = insert(Name);
502 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000503 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000504 return;
505 }
506 if (!S->body()->isUndefined())
507 return;
508
509 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000510 if (S->isWeak()) {
511 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
512 } else {
513 MemoryBufferRef MBRef = Obj.getBuffer();
514 if (!MBRef.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000515 addFile(createObjectFile(MBRef));
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000516 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000517}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000518
Peter Collingbourne892d49802016-04-27 00:05:03 +0000519// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000520template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000521 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000522 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000523 if (InputFile *File = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000524 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000525}
526
Rui Ueyama93bfee52015-10-13 18:10:33 +0000527// This function takes care of the case in which shared libraries depend on
528// the user program (not the other way, which is usual). Shared libraries
529// may have undefined symbols, expecting that the user program provides
530// the definitions for them. An example is BSD's __progname symbol.
531// We need to put such symbols to the main program's .dynsym so that
532// shared libraries can find them.
533// Except this, we ignore undefined symbols in DSOs.
534template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000535 for (SharedFile<ELFT> *File : SharedFiles)
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000536 for (StringRef U : File->getUndefinedSymbols())
537 if (SymbolBody *Sym = find(U))
538 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000539 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000540}
541
Rui Ueyama82492142016-11-15 18:41:52 +0000542// Initialize DemangledSyms with a map from demangled symbols to symbol
543// objects. Used to handle "extern C++" directive in version scripts.
544//
545// The map will contain all demangled symbols. That can be very large,
546// and in LLD we generally want to avoid do anything for each symbol.
547// Then, why are we doing this? Here's why.
548//
549// Users can use "extern C++ {}" directive to match against demangled
550// C++ symbols. For example, you can write a pattern such as
551// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
552// other than trying to match a pattern against all demangled symbols.
553// So, if "extern C++" feature is used, we need to demangle all known
554// symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000555template <class ELFT>
Rui Ueyama82492142016-11-15 18:41:52 +0000556void SymbolTable<ELFT>::initDemangledSyms() {
557 if (DemangledSyms)
558 return;
559 DemangledSyms.emplace();
560
Rui Ueyamad6328522016-07-18 01:34:57 +0000561 for (Symbol *Sym : SymVector) {
562 SymbolBody *B = Sym->body();
Rui Ueyama4c5b8ce2016-12-07 23:17:05 +0000563 if (Optional<std::string> S = demangle(B->getName()))
564 (*DemangledSyms)[*S].push_back(B);
565 else
566 (*DemangledSyms)[B->getName()].push_back(B);
Rui Ueyamad6328522016-07-18 01:34:57 +0000567 }
George Rimar50dcece2016-07-16 12:26:39 +0000568}
569
Rui Ueyama82492142016-11-15 18:41:52 +0000570template <class ELFT>
Rafael Espindolac65aee62016-12-08 15:56:33 +0000571std::vector<SymbolBody *> SymbolTable<ELFT>::find(SymbolVersion Ver) {
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000572 if (Ver.IsExternCpp) {
573 initDemangledSyms();
574 auto I = DemangledSyms->find(Ver.Name);
575 if (I != DemangledSyms->end())
576 return I->second;
577 return {};
578 }
Rafael Espindolac65aee62016-12-08 15:56:33 +0000579 std::vector<SymbolBody *> Syms;
580 Syms.push_back(find(Ver.Name));
581 return Syms;
582}
583
584template <class ELFT>
Rafael Espindolac65aee62016-12-08 15:56:33 +0000585std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(SymbolVersion Ver) {
Rafael Espindola191390a2016-12-08 16:26:20 +0000586 std::vector<SymbolBody *> Res;
587 StringMatcher M({Ver.Name});
588
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000589 if (Ver.IsExternCpp) {
590 initDemangledSyms();
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000591 for (auto &P : *DemangledSyms)
592 if (M.match(P.first()))
593 for (SymbolBody *Body : P.second)
594 if (!Body->isUndefined())
595 Res.push_back(Body);
596 return Res;
597 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000598
599 for (Symbol *Sym : SymVector) {
600 SymbolBody *B = Sym->body();
601 StringRef Name = B->getName();
602 if (!B->isUndefined() && M.match(Name))
603 Res.push_back(B);
604 }
605 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000606}
607
Rui Ueyamaea265042016-09-13 20:51:30 +0000608// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000609// script file, the script does not actually define any symbol version,
Rui Ueyamaea265042016-09-13 20:51:30 +0000610// but just specifies symbols visibilities. We assume that the script was
611// in the form of { global: foo; bar; local *; }. So, local is default.
612// In this function, we make specified symbols global.
613template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
Rui Ueyamabaf7ee32016-11-15 17:51:09 +0000614 for (SymbolVersion &Ver : Config->VersionScriptGlobals) {
Rafael Espindola39c16df2016-12-08 15:36:58 +0000615 if (Ver.HasWildcard) {
Rafael Espindola361da4c2016-12-08 16:20:29 +0000616 for (SymbolBody *B : findAll(Ver))
Rui Ueyama77d917d2016-11-17 03:19:34 +0000617 B->symbol()->VersionId = VER_NDX_GLOBAL;
Rui Ueyamaea265042016-09-13 20:51:30 +0000618 continue;
619 }
Rafael Espindola361da4c2016-12-08 16:20:29 +0000620 for (SymbolBody *B : find(Ver))
Rafael Espindola49cd0932016-12-09 15:08:40 +0000621 if (B)
622 B->symbol()->VersionId = VER_NDX_GLOBAL;
Rui Ueyamaea265042016-09-13 20:51:30 +0000623 }
Rui Ueyamaea265042016-09-13 20:51:30 +0000624}
625
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000626// Set symbol versions to symbols. This function handles patterns
627// containing no wildcard characters.
628template <class ELFT>
Rui Ueyamada805c42016-11-17 03:39:21 +0000629void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000630 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000631 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000632 return;
633
634 // Get a list of symbols which we need to assign the version to.
Rafael Espindolac65aee62016-12-08 15:56:33 +0000635 std::vector<SymbolBody *> Syms = find(Ver);
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000636
637 // Assign the version.
638 for (SymbolBody *B : Syms) {
639 if (!B || B->isUndefined()) {
640 if (Config->NoUndefinedVersion)
Rui Ueyamad84124f2016-11-17 19:57:47 +0000641 error("version script assignment of '" + VersionName + "' to symbol '" +
642 Ver.Name + "' failed: symbol not defined");
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000643 continue;
644 }
645
646 if (B->symbol()->VersionId != Config->DefaultSymbolVersion)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000647 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000648 B->symbol()->VersionId = VersionId;
649 }
650}
651
Rui Ueyama82492142016-11-15 18:41:52 +0000652template <class ELFT>
653void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
Rui Ueyamada805c42016-11-17 03:39:21 +0000654 uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000655 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000656 return;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000657 std::vector<SymbolBody *> Syms = findAll(Ver);
Rui Ueyama82492142016-11-15 18:41:52 +0000658
659 // Exact matching takes precendence over fuzzy matching,
660 // so we set a version to a symbol only if no version has been assigned
661 // to the symbol. This behavior is compatible with GNU.
662 for (SymbolBody *B : Syms)
663 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
664 B->symbol()->VersionId = VersionId;
665}
666
Rui Ueyamadad2b882016-09-02 22:15:08 +0000667// This function processes version scripts by updating VersionId
668// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000669template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000670 // Symbol themselves might know their versions because symbols
671 // can contain versions in the form of <name>@<version>.
672 // Let them parse their names.
673 if (!Config->VersionDefinitions.empty())
674 for (Symbol *Sym : SymVector)
675 Sym->body()->parseSymbolVersion();
676
Rui Ueyamaea265042016-09-13 20:51:30 +0000677 // Handle edge cases first.
George Rimard3566302016-06-20 11:55:12 +0000678 if (!Config->VersionScriptGlobals.empty()) {
Rui Ueyamaea265042016-09-13 20:51:30 +0000679 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000680 return;
681 }
682
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000683 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000684 return;
685
Rui Ueyamadad2b882016-09-02 22:15:08 +0000686 // Now we have version definitions, so we need to set version ids to symbols.
687 // Each version definition has a glob pattern, and all symbols that match
688 // with the pattern get that version.
689
Rui Ueyamadad2b882016-09-02 22:15:08 +0000690 // First, we assign versions to exact matching symbols,
691 // i.e. version definitions not containing any glob meta-characters.
Rui Ueyama96db27c2016-11-17 19:57:45 +0000692 for (SymbolVersion &Ver : Config->VersionScriptLocals)
693 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
George Rimar17c65af2016-11-16 18:46:23 +0000694 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000695 for (SymbolVersion &Ver : V.Globals)
696 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000697
Rui Ueyamadad2b882016-09-02 22:15:08 +0000698 // Next, we assign versions to fuzzy matching symbols,
699 // i.e. version definitions containing glob meta-characters.
700 // Note that because the last match takes precedence over previous matches,
701 // we iterate over the definitions in the reverse order.
George Rimar17c65af2016-11-16 18:46:23 +0000702 for (SymbolVersion &Ver : Config->VersionScriptLocals)
703 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamacd236a92016-11-17 19:57:43 +0000704 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
705 for (SymbolVersion &Ver : V.Globals)
706 assignWildcardVersion(Ver, V.Id);
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000707}
708
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000709template class elf::SymbolTable<ELF32LE>;
710template class elf::SymbolTable<ELF32BE>;
711template class elf::SymbolTable<ELF64LE>;
712template class elf::SymbolTable<ELF64BE>;