blob: 6cedf35760254aa8cfe0069295cbffe5d4e308ed [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 Ueyama9381eb12016-12-18 14:06:06 +000021#include "Memory.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022#include "Symbols.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;
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000195 Sym->InVersionScript = false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000196 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000197 Sym->Visibility = STV_DEFAULT;
198 Sym->IsUsedInRegularObj = false;
199 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000200 Sym->Traced = V.Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000201 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000202 SymVector.push_back(Sym);
203 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000204 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000205 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000206 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000207}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000208
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000209// Construct a string in the form of "Sym in File1 and File2".
210// Used to construct an error message.
211static std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile) {
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000212 return "'" + toString(*Existing) + "' in " + toString(Existing->File) +
213 " and " + toString(NewFile);
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000214}
215
Peter Collingbourne4f952702016-05-01 04:55:03 +0000216// Find an existing symbol or create and insert a new one, then apply the given
217// attributes.
218template <class ELFT>
219std::pair<Symbol *, bool>
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000220SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
Davide Italiano786d8e32016-09-29 00:40:08 +0000221 bool CanOmitFromDynSym, InputFile *File) {
Rafael Espindola05098762016-08-31 13:49:23 +0000222 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000223 Symbol *S;
224 bool WasInserted;
225 std::tie(S, WasInserted) = insert(Name);
226
227 // Merge in the new symbol's visibility.
228 S->Visibility = getMinVisibility(S->Visibility, Visibility);
229 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
230 S->ExportDynamic = true;
231 if (IsUsedInRegularObj)
232 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000233 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
234 ((Type == STT_TLS) != S->body()->isTls()))
Eugene Leviant825e5382016-11-08 16:26:32 +0000235 error("TLS attribute mismatch for symbol " + conflictMsg(S->body(), File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000236
237 return {S, WasInserted};
238}
239
Peter Collingbourne4f952702016-05-01 04:55:03 +0000240template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000241 return addUndefined(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
242 /*Type*/ 0,
Davide Italiano786d8e32016-09-29 00:40:08 +0000243 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000244}
245
246template <class ELFT>
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000247Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, bool IsLocal,
248 uint8_t Binding, uint8_t StOther,
249 uint8_t Type, bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000250 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000251 Symbol *S;
252 bool WasInserted;
253 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000254 insert(Name, Type, StOther & 3, CanOmitFromDynSym, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000255 if (WasInserted) {
256 S->Binding = Binding;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000257 replaceBody<Undefined>(S, Name, IsLocal, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000258 return S;
259 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000260 if (Binding != STB_WEAK) {
261 if (S->body()->isShared() || S->body()->isLazy())
262 S->Binding = Binding;
263 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000264 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000265 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000266 if (auto *L = dyn_cast<Lazy>(S->body())) {
267 // An undefined weak will not fetch archive members, but we have to remember
268 // its type. See also comment in addLazyArchive.
269 if (S->isWeak())
270 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000271 else if (InputFile *F = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000272 addFile(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000273 }
274 return S;
275}
276
277// We have a new defined symbol with the specified binding. Return 1 if the new
278// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
279// strong defined symbols.
280static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
281 if (WasInserted)
282 return 1;
283 SymbolBody *Body = S->body();
284 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
285 return 1;
286 if (Binding == STB_WEAK)
287 return -1;
288 if (S->isWeak())
289 return 1;
290 return 0;
291}
292
293// We have a new non-common defined symbol with the specified binding. Return 1
294// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
295// is a conflict. If the new symbol wins, also update the binding.
Rafael Espindola858c0922016-12-02 02:58:21 +0000296template <typename ELFT>
297static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
298 bool IsAbsolute, typename ELFT::uint Value) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000299 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
300 if (Cmp > 0)
301 S->Binding = Binding;
302 return Cmp;
303 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000304 SymbolBody *B = S->body();
305 if (isa<DefinedCommon>(B)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000306 // Non-common symbols take precedence over common symbols.
307 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000308 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000309 return 1;
Rafael Espindola858c0922016-12-02 02:58:21 +0000310 } else if (auto *R = dyn_cast<DefinedRegular<ELFT>>(B)) {
311 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
312 R->Value == Value)
313 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000314 }
315 return 0;
316}
317
318template <class ELFT>
319Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
320 uint64_t Alignment, uint8_t Binding,
321 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000322 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000323 Symbol *S;
324 bool WasInserted;
Davide Italiano786d8e32016-09-29 00:40:08 +0000325 std::tie(S, WasInserted) =
326 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000327 int Cmp = compareDefined(S, WasInserted, Binding);
328 if (Cmp > 0) {
329 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000330 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000331 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000332 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000333 if (!C) {
334 // Non-common symbols take precedence over common symbols.
335 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000336 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000337 return S;
338 }
339
340 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000341 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000342
Rafael Espindola8db87292016-08-31 13:42:08 +0000343 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
344 if (Size > C->Size)
345 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000346 }
347 return S;
348}
349
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000350static void print(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000351 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000352 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000353 else
354 error(Msg);
355}
356
Eugene Leviant825e5382016-11-08 16:26:32 +0000357static void reportDuplicate(SymbolBody *Existing, InputFile *NewFile) {
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000358 print("duplicate symbol " + conflictMsg(Existing, NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000359}
360
361template <class ELFT>
362static void reportDuplicate(SymbolBody *Existing,
363 InputSectionBase<ELFT> *ErrSec,
364 typename ELFT::uint ErrOffset) {
365 DefinedRegular<ELFT> *D = dyn_cast<DefinedRegular<ELFT>>(Existing);
366 if (!D || !D->Section || !ErrSec) {
367 reportDuplicate(Existing, ErrSec ? ErrSec->getFile() : nullptr);
368 return;
369 }
370
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000371 std::string OldLoc = D->Section->getLocation(D->Value);
372 std::string NewLoc = ErrSec->getLocation(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000373
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000374 print(NewLoc + ": duplicate symbol '" + toString(*Existing) + "'");
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000375 print(OldLoc + ": previous definition was here");
Eugene Leviant825e5382016-11-08 16:26:32 +0000376}
377
Peter Collingbourne4f952702016-05-01 04:55:03 +0000378template <typename ELFT>
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000379Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
380 uint8_t Type, uintX_t Value, uintX_t Size,
381 uint8_t Binding,
George Rimar463984d2016-11-15 08:07:14 +0000382 InputSectionBase<ELFT> *Section,
383 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000384 Symbol *S;
385 bool WasInserted;
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000386 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3,
George Rimar463984d2016-11-15 08:07:14 +0000387 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola858c0922016-12-02 02:58:21 +0000388 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
389 Section == nullptr, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000390 if (Cmp > 0)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000391 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type,
392 Value, Size, Section, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000393 else if (Cmp == 0)
Eugene Leviant825e5382016-11-08 16:26:32 +0000394 reportDuplicate(S->body(), Section, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000395 return S;
396}
397
398template <typename ELFT>
Eugene Leviantafaa9342016-11-16 09:49:39 +0000399Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
400 const OutputSectionBase *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000401 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000402 Symbol *S;
403 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000404 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
Davide Italiano786d8e32016-09-29 00:40:08 +0000405 /*CanOmitFromDynSym*/ false, nullptr);
Rafael Espindola858c0922016-12-02 02:58:21 +0000406 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, STB_GLOBAL,
407 /*IsAbsolute*/ false, /*Value*/ 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000408 if (Cmp > 0)
409 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
410 else if (Cmp == 0)
411 reportDuplicate(S->body(), nullptr);
412 return S;
413}
414
415template <typename ELFT>
416void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
417 const Elf_Sym &Sym,
418 const typename ELFT::Verdef *Verdef) {
419 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
420 // as the visibility, which will leave the visibility in the symbol table
421 // unchanged.
422 Symbol *S;
423 bool WasInserted;
424 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000425 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000426 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola6239ce62016-12-09 14:12:02 +0000427 if (Sym.getVisibility() == STV_DEFAULT) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000428 S->ExportDynamic = true;
Rafael Espindola6239ce62016-12-09 14:12:02 +0000429 // Exporting preempting symbols takes precedence over linker scripts.
430 if (S->VersionId == VER_NDX_LOCAL)
431 S->VersionId = VER_NDX_GLOBAL;
432 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000433 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000434 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000435 if (!S->isWeak())
436 F->IsUsed = true;
437 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000438}
439
440template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000441Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000442 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000443 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000444 Symbol *S;
445 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000446 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000447 insert(Name, Type, StOther & 3, CanOmitFromDynSym, F);
Rafael Espindola858c0922016-12-02 02:58:21 +0000448 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
449 /*IsAbs*/ false, /*Value*/ 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000450 if (Cmp > 0)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000451 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type,
452 0, 0, nullptr, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000453 else if (Cmp == 0)
454 reportDuplicate(S->body(), F);
455 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000456}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000457
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000458template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000459 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000460 if (It == Symtab.end())
461 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000462 SymIndex V = It->second;
463 if (V.Idx == -1)
464 return nullptr;
465 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000466}
467
Davide Italiano8e1131d2016-06-29 02:46:51 +0000468template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000469void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
470 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000471 Symbol *S;
472 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000473 StringRef Name = Sym.getName();
474 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000475 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000476 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000477 return;
478 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000479 if (!S->body()->isUndefined())
480 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000481
Peter Collingbourne4f952702016-05-01 04:55:03 +0000482 // Weak undefined symbols should not fetch members from archives. If we were
483 // to keep old symbol we would not know that an archive member was available
484 // if a strong undefined symbol shows up afterwards in the link. If a strong
485 // undefined symbol never shows up, this lazy symbol will get to the end of
486 // the link and must be treated as the weak undefined one. We already marked
487 // this symbol as used when we added it to the symbol table, but we also need
488 // to preserve its type. FIXME: Move the Type field to Symbol.
489 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000490 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000491 return;
492 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000493 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
494 if (!MBInfo.first.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000495 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000496}
497
498template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000499void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000500 Symbol *S;
501 bool WasInserted;
502 std::tie(S, WasInserted) = insert(Name);
503 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000504 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000505 return;
506 }
507 if (!S->body()->isUndefined())
508 return;
509
510 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000511 if (S->isWeak()) {
512 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
513 } else {
514 MemoryBufferRef MBRef = Obj.getBuffer();
515 if (!MBRef.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000516 addFile(createObjectFile(MBRef));
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000517 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000518}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000519
Peter Collingbourne892d49802016-04-27 00:05:03 +0000520// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000521template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000522 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000523 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000524 if (InputFile *File = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000525 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000526}
527
Rui Ueyama93bfee52015-10-13 18:10:33 +0000528// This function takes care of the case in which shared libraries depend on
529// the user program (not the other way, which is usual). Shared libraries
530// may have undefined symbols, expecting that the user program provides
531// the definitions for them. An example is BSD's __progname symbol.
532// We need to put such symbols to the main program's .dynsym so that
533// shared libraries can find them.
534// Except this, we ignore undefined symbols in DSOs.
535template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000536 for (SharedFile<ELFT> *File : SharedFiles)
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000537 for (StringRef U : File->getUndefinedSymbols())
538 if (SymbolBody *Sym = find(U))
539 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000540 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000541}
542
Rui Ueyama82492142016-11-15 18:41:52 +0000543// Initialize DemangledSyms with a map from demangled symbols to symbol
544// objects. Used to handle "extern C++" directive in version scripts.
545//
546// The map will contain all demangled symbols. That can be very large,
547// and in LLD we generally want to avoid do anything for each symbol.
548// Then, why are we doing this? Here's why.
549//
550// Users can use "extern C++ {}" directive to match against demangled
551// C++ symbols. For example, you can write a pattern such as
552// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
553// other than trying to match a pattern against all demangled symbols.
554// So, if "extern C++" feature is used, we need to demangle all known
555// symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000556template <class ELFT>
Rui Ueyama82492142016-11-15 18:41:52 +0000557void SymbolTable<ELFT>::initDemangledSyms() {
558 if (DemangledSyms)
559 return;
560 DemangledSyms.emplace();
561
Rui Ueyamad6328522016-07-18 01:34:57 +0000562 for (Symbol *Sym : SymVector) {
563 SymbolBody *B = Sym->body();
Rui Ueyama4c5b8ce2016-12-07 23:17:05 +0000564 if (Optional<std::string> S = demangle(B->getName()))
565 (*DemangledSyms)[*S].push_back(B);
566 else
567 (*DemangledSyms)[B->getName()].push_back(B);
Rui Ueyamad6328522016-07-18 01:34:57 +0000568 }
George Rimar50dcece2016-07-16 12:26:39 +0000569}
570
Rui Ueyama82492142016-11-15 18:41:52 +0000571template <class ELFT>
Rui Ueyama86581e42016-12-10 00:34:06 +0000572std::vector<SymbolBody *> SymbolTable<ELFT>::findByVersion(SymbolVersion Ver) {
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000573 if (Ver.IsExternCpp) {
574 initDemangledSyms();
575 auto I = DemangledSyms->find(Ver.Name);
576 if (I != DemangledSyms->end())
577 return I->second;
578 return {};
579 }
Rafael Espindolac65aee62016-12-08 15:56:33 +0000580 std::vector<SymbolBody *> Syms;
581 Syms.push_back(find(Ver.Name));
582 return Syms;
583}
584
585template <class ELFT>
Rui Ueyama86581e42016-12-10 00:34:06 +0000586std::vector<SymbolBody *>
587SymbolTable<ELFT>::findAllByVersion(SymbolVersion Ver) {
Rafael Espindola191390a2016-12-08 16:26:20 +0000588 std::vector<SymbolBody *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000589 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000590
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000591 if (Ver.IsExternCpp) {
592 initDemangledSyms();
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000593 for (auto &P : *DemangledSyms)
594 if (M.match(P.first()))
595 for (SymbolBody *Body : P.second)
596 if (!Body->isUndefined())
597 Res.push_back(Body);
598 return Res;
599 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000600
601 for (Symbol *Sym : SymVector) {
602 SymbolBody *B = Sym->body();
603 StringRef Name = B->getName();
604 if (!B->isUndefined() && M.match(Name))
605 Res.push_back(B);
606 }
607 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000608}
609
Rui Ueyamaea265042016-09-13 20:51:30 +0000610// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000611// script file, the script does not actually define any symbol version,
Rui Ueyamaea265042016-09-13 20:51:30 +0000612// but just specifies symbols visibilities. We assume that the script was
613// in the form of { global: foo; bar; local *; }. So, local is default.
614// In this function, we make specified symbols global.
615template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
Rui Ueyamabaf7ee32016-11-15 17:51:09 +0000616 for (SymbolVersion &Ver : Config->VersionScriptGlobals) {
Rafael Espindola39c16df2016-12-08 15:36:58 +0000617 if (Ver.HasWildcard) {
Rui Ueyama86581e42016-12-10 00:34:06 +0000618 for (SymbolBody *B : findAllByVersion(Ver))
Rui Ueyama77d917d2016-11-17 03:19:34 +0000619 B->symbol()->VersionId = VER_NDX_GLOBAL;
Rui Ueyamaea265042016-09-13 20:51:30 +0000620 continue;
621 }
Rui Ueyama86581e42016-12-10 00:34:06 +0000622 for (SymbolBody *B : findByVersion(Ver))
Rafael Espindola49cd0932016-12-09 15:08:40 +0000623 if (B)
624 B->symbol()->VersionId = VER_NDX_GLOBAL;
Rui Ueyamaea265042016-09-13 20:51:30 +0000625 }
Rui Ueyamaea265042016-09-13 20:51:30 +0000626}
627
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000628// Set symbol versions to symbols. This function handles patterns
629// containing no wildcard characters.
630template <class ELFT>
Rui Ueyamada805c42016-11-17 03:39:21 +0000631void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000632 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000633 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000634 return;
635
636 // Get a list of symbols which we need to assign the version to.
Rui Ueyama86581e42016-12-10 00:34:06 +0000637 std::vector<SymbolBody *> Syms = findByVersion(Ver);
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000638
639 // Assign the version.
640 for (SymbolBody *B : Syms) {
641 if (!B || B->isUndefined()) {
642 if (Config->NoUndefinedVersion)
Rui Ueyamad84124f2016-11-17 19:57:47 +0000643 error("version script assignment of '" + VersionName + "' to symbol '" +
644 Ver.Name + "' failed: symbol not defined");
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000645 continue;
646 }
647
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000648 Symbol *Sym = B->symbol();
649 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000650 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000651 Sym->VersionId = VersionId;
652 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000653 }
654}
655
Rui Ueyama82492142016-11-15 18:41:52 +0000656template <class ELFT>
657void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
Rui Ueyamada805c42016-11-17 03:39:21 +0000658 uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000659 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000660 return;
Rui Ueyama86581e42016-12-10 00:34:06 +0000661 std::vector<SymbolBody *> Syms = findAllByVersion(Ver);
Rui Ueyama82492142016-11-15 18:41:52 +0000662
663 // Exact matching takes precendence over fuzzy matching,
664 // so we set a version to a symbol only if no version has been assigned
665 // to the symbol. This behavior is compatible with GNU.
666 for (SymbolBody *B : Syms)
667 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
668 B->symbol()->VersionId = VersionId;
669}
670
Rui Ueyamadad2b882016-09-02 22:15:08 +0000671// This function processes version scripts by updating VersionId
672// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000673template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000674 // Symbol themselves might know their versions because symbols
675 // can contain versions in the form of <name>@<version>.
676 // Let them parse their names.
677 if (!Config->VersionDefinitions.empty())
678 for (Symbol *Sym : SymVector)
679 Sym->body()->parseSymbolVersion();
680
Rui Ueyamaea265042016-09-13 20:51:30 +0000681 // Handle edge cases first.
George Rimard3566302016-06-20 11:55:12 +0000682 if (!Config->VersionScriptGlobals.empty()) {
Rui Ueyamaea265042016-09-13 20:51:30 +0000683 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000684 return;
685 }
686
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000687 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000688 return;
689
Rui Ueyamadad2b882016-09-02 22:15:08 +0000690 // Now we have version definitions, so we need to set version ids to symbols.
691 // Each version definition has a glob pattern, and all symbols that match
692 // with the pattern get that version.
693
Rui Ueyamadad2b882016-09-02 22:15:08 +0000694 // First, we assign versions to exact matching symbols,
695 // i.e. version definitions not containing any glob meta-characters.
Rui Ueyama96db27c2016-11-17 19:57:45 +0000696 for (SymbolVersion &Ver : Config->VersionScriptLocals)
697 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
George Rimar17c65af2016-11-16 18:46:23 +0000698 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000699 for (SymbolVersion &Ver : V.Globals)
700 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000701
Rui Ueyamadad2b882016-09-02 22:15:08 +0000702 // Next, we assign versions to fuzzy matching symbols,
703 // i.e. version definitions containing glob meta-characters.
704 // Note that because the last match takes precedence over previous matches,
705 // we iterate over the definitions in the reverse order.
George Rimar17c65af2016-11-16 18:46:23 +0000706 for (SymbolVersion &Ver : Config->VersionScriptLocals)
707 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamacd236a92016-11-17 19:57:43 +0000708 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
709 for (SymbolVersion &Ver : V.Globals)
710 assignWildcardVersion(Ver, V.Id);
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000711}
712
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000713template class elf::SymbolTable<ELF32LE>;
714template class elf::SymbolTable<ELF32BE>;
715template class elf::SymbolTable<ELF64LE>;
716template class elf::SymbolTable<ELF64BE>;