blob: eceb6dc96e146548682c0dbd2bb3ad5fc71a1421 [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)
Peter Smith97c6d782017-01-04 09:45:45 +0000118 LTO->add<ELFT>(*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
Rui Ueyamae50e8072016-12-22 05:11:12 +0000246static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
247
Peter Collingbourne4f952702016-05-01 04:55:03 +0000248template <class ELFT>
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000249Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, bool IsLocal,
250 uint8_t Binding, uint8_t StOther,
251 uint8_t Type, bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000252 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000253 Symbol *S;
254 bool WasInserted;
255 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000256 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000257 if (WasInserted) {
258 S->Binding = Binding;
Peter Smith97c6d782017-01-04 09:45:45 +0000259 replaceBody<Undefined<ELFT>>(S, Name, IsLocal, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000260 return S;
261 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000262 if (Binding != STB_WEAK) {
263 if (S->body()->isShared() || S->body()->isLazy())
264 S->Binding = Binding;
265 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000266 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000267 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000268 if (auto *L = dyn_cast<Lazy>(S->body())) {
269 // An undefined weak will not fetch archive members, but we have to remember
270 // its type. See also comment in addLazyArchive.
271 if (S->isWeak())
272 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000273 else if (InputFile *F = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000274 addFile(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000275 }
276 return S;
277}
278
279// We have a new defined symbol with the specified binding. Return 1 if the new
280// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
281// strong defined symbols.
282static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
283 if (WasInserted)
284 return 1;
285 SymbolBody *Body = S->body();
286 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
287 return 1;
288 if (Binding == STB_WEAK)
289 return -1;
290 if (S->isWeak())
291 return 1;
292 return 0;
293}
294
295// We have a new non-common defined symbol with the specified binding. Return 1
296// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
297// is a conflict. If the new symbol wins, also update the binding.
Rafael Espindola858c0922016-12-02 02:58:21 +0000298template <typename ELFT>
299static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
300 bool IsAbsolute, typename ELFT::uint Value) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000301 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
302 if (Cmp > 0)
303 S->Binding = Binding;
304 return Cmp;
305 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000306 SymbolBody *B = S->body();
307 if (isa<DefinedCommon>(B)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000308 // Non-common symbols take precedence over common symbols.
309 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000310 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000311 return 1;
Rafael Espindola858c0922016-12-02 02:58:21 +0000312 } else if (auto *R = dyn_cast<DefinedRegular<ELFT>>(B)) {
313 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
314 R->Value == Value)
315 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000316 }
317 return 0;
318}
319
320template <class ELFT>
321Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
322 uint64_t Alignment, uint8_t Binding,
323 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000324 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000325 Symbol *S;
326 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000327 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
328 /*CanOmitFromDynSym*/ false, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000329 int Cmp = compareDefined(S, WasInserted, Binding);
330 if (Cmp > 0) {
331 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000332 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000333 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000334 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000335 if (!C) {
336 // Non-common symbols take precedence over common symbols.
337 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000338 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000339 return S;
340 }
341
342 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000343 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000344
Rafael Espindola8db87292016-08-31 13:42:08 +0000345 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
346 if (Size > C->Size)
347 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000348 }
349 return S;
350}
351
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000352static void print(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000353 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000354 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000355 else
356 error(Msg);
357}
358
Eugene Leviant825e5382016-11-08 16:26:32 +0000359static void reportDuplicate(SymbolBody *Existing, InputFile *NewFile) {
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000360 print("duplicate symbol " + conflictMsg(Existing, NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000361}
362
363template <class ELFT>
364static void reportDuplicate(SymbolBody *Existing,
365 InputSectionBase<ELFT> *ErrSec,
366 typename ELFT::uint ErrOffset) {
367 DefinedRegular<ELFT> *D = dyn_cast<DefinedRegular<ELFT>>(Existing);
368 if (!D || !D->Section || !ErrSec) {
369 reportDuplicate(Existing, ErrSec ? ErrSec->getFile() : nullptr);
370 return;
371 }
372
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000373 std::string OldLoc = D->Section->getLocation(D->Value);
374 std::string NewLoc = ErrSec->getLocation(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000375
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000376 print(NewLoc + ": duplicate symbol '" + toString(*Existing) + "'");
Rui Ueyama9c5a69d2016-11-08 20:02:23 +0000377 print(OldLoc + ": previous definition was here");
Eugene Leviant825e5382016-11-08 16:26:32 +0000378}
379
Peter Collingbourne4f952702016-05-01 04:55:03 +0000380template <typename ELFT>
Rafael Espindola5ceeb602016-10-26 20:57:14 +0000381Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
382 uint8_t Type, uintX_t Value, uintX_t Size,
383 uint8_t Binding,
George Rimar463984d2016-11-15 08:07:14 +0000384 InputSectionBase<ELFT> *Section,
385 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000386 Symbol *S;
387 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000388 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000389 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola858c0922016-12-02 02:58:21 +0000390 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
391 Section == nullptr, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000392 if (Cmp > 0)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000393 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type,
394 Value, Size, Section, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000395 else if (Cmp == 0)
Eugene Leviant825e5382016-11-08 16:26:32 +0000396 reportDuplicate(S->body(), Section, Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000397 return S;
398}
399
400template <typename ELFT>
Eugene Leviantafaa9342016-11-16 09:49:39 +0000401Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
402 const OutputSectionBase *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000403 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000404 Symbol *S;
405 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000406 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, getVisibility(StOther),
Davide Italiano786d8e32016-09-29 00:40:08 +0000407 /*CanOmitFromDynSym*/ false, nullptr);
Rafael Espindola858c0922016-12-02 02:58:21 +0000408 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, STB_GLOBAL,
409 /*IsAbsolute*/ false, /*Value*/ 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 if (Cmp > 0)
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000411 replaceBody<DefinedSynthetic>(S, N, Value, Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000412 else if (Cmp == 0)
413 reportDuplicate(S->body(), nullptr);
414 return S;
415}
416
417template <typename ELFT>
418void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
419 const Elf_Sym &Sym,
420 const typename ELFT::Verdef *Verdef) {
421 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
422 // as the visibility, which will leave the visibility in the symbol table
423 // unchanged.
424 Symbol *S;
425 bool WasInserted;
426 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000427 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000428 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola6239ce62016-12-09 14:12:02 +0000429 if (Sym.getVisibility() == STV_DEFAULT) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000430 S->ExportDynamic = true;
Rafael Espindola6239ce62016-12-09 14:12:02 +0000431 // Exporting preempting symbols takes precedence over linker scripts.
432 if (S->VersionId == VER_NDX_LOCAL)
433 S->VersionId = VER_NDX_GLOBAL;
434 }
Peter Smith97c6d782017-01-04 09:45:45 +0000435 if (WasInserted || isa<Undefined<ELFT>>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000436 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000437 if (!S->isWeak())
438 F->IsUsed = true;
439 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000440}
441
442template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000443Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000444 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000445 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000446 Symbol *S;
447 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000448 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000449 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindola858c0922016-12-02 02:58:21 +0000450 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
451 /*IsAbs*/ false, /*Value*/ 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000452 if (Cmp > 0)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000453 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type,
454 0, 0, nullptr, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000455 else if (Cmp == 0)
456 reportDuplicate(S->body(), F);
457 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000458}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000459
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000460template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000461 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000462 if (It == Symtab.end())
463 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000464 SymIndex V = It->second;
465 if (V.Idx == -1)
466 return nullptr;
467 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000468}
469
Davide Italiano8e1131d2016-06-29 02:46:51 +0000470template <class ELFT>
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000471SymbolBody *SymbolTable<ELFT>::findDefined(StringRef Name) {
472 if (SymbolBody *S = find(Name))
473 if (S->isDefined() && !S->isShared())
474 return S;
475 return nullptr;
476}
477
478template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000479void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
480 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000481 Symbol *S;
482 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000483 StringRef Name = Sym.getName();
484 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000485 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000486 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000487 return;
488 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000489 if (!S->body()->isUndefined())
490 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000491
Peter Collingbourne4f952702016-05-01 04:55:03 +0000492 // Weak undefined symbols should not fetch members from archives. If we were
493 // to keep old symbol we would not know that an archive member was available
494 // if a strong undefined symbol shows up afterwards in the link. If a strong
495 // undefined symbol never shows up, this lazy symbol will get to the end of
496 // the link and must be treated as the weak undefined one. We already marked
497 // this symbol as used when we added it to the symbol table, but we also need
498 // to preserve its type. FIXME: Move the Type field to Symbol.
499 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000500 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000501 return;
502 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000503 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
504 if (!MBInfo.first.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000505 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000506}
507
508template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000509void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000510 Symbol *S;
511 bool WasInserted;
512 std::tie(S, WasInserted) = insert(Name);
513 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000514 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515 return;
516 }
517 if (!S->body()->isUndefined())
518 return;
519
520 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000521 if (S->isWeak()) {
522 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
523 } else {
524 MemoryBufferRef MBRef = Obj.getBuffer();
525 if (!MBRef.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000526 addFile(createObjectFile(MBRef));
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000527 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000528}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000529
Peter Collingbourne892d49802016-04-27 00:05:03 +0000530// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000531template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000532 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000533 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000534 if (InputFile *File = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000535 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000536}
537
Rui Ueyama93bfee52015-10-13 18:10:33 +0000538// This function takes care of the case in which shared libraries depend on
539// the user program (not the other way, which is usual). Shared libraries
540// may have undefined symbols, expecting that the user program provides
541// the definitions for them. An example is BSD's __progname symbol.
542// We need to put such symbols to the main program's .dynsym so that
543// shared libraries can find them.
544// Except this, we ignore undefined symbols in DSOs.
545template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000546 for (SharedFile<ELFT> *File : SharedFiles)
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000547 for (StringRef U : File->getUndefinedSymbols())
548 if (SymbolBody *Sym = find(U))
549 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000550 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000551}
552
Rui Ueyama82492142016-11-15 18:41:52 +0000553// Initialize DemangledSyms with a map from demangled symbols to symbol
554// objects. Used to handle "extern C++" directive in version scripts.
555//
556// The map will contain all demangled symbols. That can be very large,
557// and in LLD we generally want to avoid do anything for each symbol.
558// Then, why are we doing this? Here's why.
559//
560// Users can use "extern C++ {}" directive to match against demangled
561// C++ symbols. For example, you can write a pattern such as
562// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
563// other than trying to match a pattern against all demangled symbols.
564// So, if "extern C++" feature is used, we need to demangle all known
565// symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000566template <class ELFT>
Rui Ueyama96aff372016-12-22 05:31:52 +0000567StringMap<std::vector<SymbolBody *>> &SymbolTable<ELFT>::getDemangledSyms() {
568 if (!DemangledSyms) {
569 DemangledSyms.emplace();
570 for (Symbol *Sym : SymVector) {
571 SymbolBody *B = Sym->body();
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000572 if (B->isUndefined())
573 continue;
Rui Ueyama96aff372016-12-22 05:31:52 +0000574 if (Optional<std::string> S = demangle(B->getName()))
575 (*DemangledSyms)[*S].push_back(B);
576 else
577 (*DemangledSyms)[B->getName()].push_back(B);
578 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000579 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000580 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000581}
582
Rui Ueyama82492142016-11-15 18:41:52 +0000583template <class ELFT>
Rui Ueyama86581e42016-12-10 00:34:06 +0000584std::vector<SymbolBody *> SymbolTable<ELFT>::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000585 if (Ver.IsExternCpp)
586 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000587 if (SymbolBody *B = find(Ver.Name))
588 if (!B->isUndefined())
589 return {B};
590 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000591}
592
593template <class ELFT>
Rui Ueyama86581e42016-12-10 00:34:06 +0000594std::vector<SymbolBody *>
595SymbolTable<ELFT>::findAllByVersion(SymbolVersion Ver) {
Rafael Espindola191390a2016-12-08 16:26:20 +0000596 std::vector<SymbolBody *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000597 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000598
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000599 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000600 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000601 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000602 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000603 return Res;
604 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000605
606 for (Symbol *Sym : SymVector) {
607 SymbolBody *B = Sym->body();
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000608 if (!B->isUndefined() && M.match(B->getName()))
Rafael Espindola191390a2016-12-08 16:26:20 +0000609 Res.push_back(B);
610 }
611 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000612}
613
Rui Ueyamaea265042016-09-13 20:51:30 +0000614// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000615// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000616// but just specifies symbols visibilities.
Rui Ueyamaea265042016-09-13 20:51:30 +0000617template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000618 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
619 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
620 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
621 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
622 for (SymbolVersion &Ver : Config->VersionScriptLocals)
623 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
624 for (SymbolVersion &Ver : Config->VersionScriptLocals)
625 assignWildcardVersion(Ver, VER_NDX_LOCAL);
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 Ueyama4c134ea2016-12-22 09:54:32 +0000638 if (Syms.empty()) {
639 if (Config->NoUndefinedVersion)
640 error("version script assignment of '" + VersionName + "' to symbol '" +
641 Ver.Name + "' failed: symbol not defined");
642 return;
643 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000644
645 // Assign the version.
646 for (SymbolBody *B : Syms) {
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000647 Symbol *Sym = B->symbol();
648 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000649 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000650 Sym->VersionId = VersionId;
651 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000652 }
653}
654
Rui Ueyama82492142016-11-15 18:41:52 +0000655template <class ELFT>
656void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
Rui Ueyamada805c42016-11-17 03:39:21 +0000657 uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000658 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000659 return;
Rui Ueyama86581e42016-12-10 00:34:06 +0000660 std::vector<SymbolBody *> Syms = findAllByVersion(Ver);
Rui Ueyama82492142016-11-15 18:41:52 +0000661
662 // Exact matching takes precendence over fuzzy matching,
663 // so we set a version to a symbol only if no version has been assigned
664 // to the symbol. This behavior is compatible with GNU.
665 for (SymbolBody *B : Syms)
666 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
667 B->symbol()->VersionId = VersionId;
668}
669
Rui Ueyamadad2b882016-09-02 22:15:08 +0000670// This function processes version scripts by updating VersionId
671// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000672template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000673 // Symbol themselves might know their versions because symbols
674 // can contain versions in the form of <name>@<version>.
675 // Let them parse their names.
676 if (!Config->VersionDefinitions.empty())
677 for (Symbol *Sym : SymVector)
678 Sym->body()->parseSymbolVersion();
679
Rui Ueyamaea265042016-09-13 20:51:30 +0000680 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000681 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000682
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.
George Rimar17c65af2016-11-16 18:46:23 +0000692 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000693 for (SymbolVersion &Ver : V.Globals)
694 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000695
Rui Ueyamadad2b882016-09-02 22:15:08 +0000696 // Next, we assign versions to fuzzy matching symbols,
697 // i.e. version definitions containing glob meta-characters.
698 // Note that because the last match takes precedence over previous matches,
699 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000700 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
701 for (SymbolVersion &Ver : V.Globals)
702 assignWildcardVersion(Ver, V.Id);
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000703}
704
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000705template class elf::SymbolTable<ELF32LE>;
706template class elf::SymbolTable<ELF32BE>;
707template class elf::SymbolTable<ELF64LE>;
708template class elf::SymbolTable<ELF64BE>;