blob: 73ef1cba153e13d60b3fe43fc6b775e856041aeb [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// Symbol table is a bag of all known symbols. We put all symbols of
Rui Ueyamac9559d92016-01-05 20:47:37 +000011// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000012// a hash table with the logic to resolve symbol name conflicts using
13// the symbol types.
14//
15//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000018#include "Config.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000019#include "Error.h"
Davide Italiano8e1131d2016-06-29 02:46:51 +000020#include "LinkerScript.h"
Rui Ueyama55518e72016-10-28 20:57:25 +000021#include "Memory.h"
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;
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.
426 if (Sym.getVisibility() == STV_DEFAULT)
427 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000428 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000429 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000430 if (!S->isWeak())
431 F->IsUsed = true;
432 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000433}
434
435template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000436Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000437 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000438 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000439 Symbol *S;
440 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000441 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000442 insert(Name, Type, StOther & 3, CanOmitFromDynSym, F);
Rafael Espindola858c0922016-12-02 02:58:21 +0000443 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
444 /*IsAbs*/ false, /*Value*/ 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000445 if (Cmp > 0)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000446 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type,
447 0, 0, nullptr, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000448 else if (Cmp == 0)
449 reportDuplicate(S->body(), F);
450 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000451}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000452
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000453template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000454 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000455 if (It == Symtab.end())
456 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000457 SymIndex V = It->second;
458 if (V.Idx == -1)
459 return nullptr;
460 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000461}
462
Rui Ueyama82492142016-11-15 18:41:52 +0000463// Returns a list of defined symbols that match with a given pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000464template <class ELFT>
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000465std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef GlobPat) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000466 std::vector<SymbolBody *> Res;
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000467 StringMatcher M({GlobPat});
Rui Ueyamad6328522016-07-18 01:34:57 +0000468 for (Symbol *Sym : SymVector) {
469 SymbolBody *B = Sym->body();
George Rimarc91930a2016-09-02 21:17:20 +0000470 StringRef Name = B->getName();
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000471 if (!B->isUndefined() && M.match(Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000472 Res.push_back(B);
473 }
474 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000475}
476
477template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000478void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
479 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000480 Symbol *S;
481 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000482 StringRef Name = Sym.getName();
483 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000484 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000485 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000486 return;
487 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000488 if (!S->body()->isUndefined())
489 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000490
Peter Collingbourne4f952702016-05-01 04:55:03 +0000491 // Weak undefined symbols should not fetch members from archives. If we were
492 // to keep old symbol we would not know that an archive member was available
493 // if a strong undefined symbol shows up afterwards in the link. If a strong
494 // undefined symbol never shows up, this lazy symbol will get to the end of
495 // the link and must be treated as the weak undefined one. We already marked
496 // this symbol as used when we added it to the symbol table, but we also need
497 // to preserve its type. FIXME: Move the Type field to Symbol.
498 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000499 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000500 return;
501 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000502 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
503 if (!MBInfo.first.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000504 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000505}
506
507template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000508void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000509 Symbol *S;
510 bool WasInserted;
511 std::tie(S, WasInserted) = insert(Name);
512 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000513 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000514 return;
515 }
516 if (!S->body()->isUndefined())
517 return;
518
519 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000520 if (S->isWeak()) {
521 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
522 } else {
523 MemoryBufferRef MBRef = Obj.getBuffer();
524 if (!MBRef.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000525 addFile(createObjectFile(MBRef));
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000526 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000527}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000528
Peter Collingbourne892d49802016-04-27 00:05:03 +0000529// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000530template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000531 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000532 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000533 if (InputFile *File = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000534 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000535}
536
Rui Ueyama93bfee52015-10-13 18:10:33 +0000537// This function takes care of the case in which shared libraries depend on
538// the user program (not the other way, which is usual). Shared libraries
539// may have undefined symbols, expecting that the user program provides
540// the definitions for them. An example is BSD's __progname symbol.
541// We need to put such symbols to the main program's .dynsym so that
542// shared libraries can find them.
543// Except this, we ignore undefined symbols in DSOs.
544template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000545 for (SharedFile<ELFT> *File : SharedFiles)
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000546 for (StringRef U : File->getUndefinedSymbols())
547 if (SymbolBody *Sym = find(U))
548 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000549 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000550}
551
Rui Ueyamadad2b882016-09-02 22:15:08 +0000552// This function processes --export-dynamic-symbol and --dynamic-list.
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000553template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
554 for (StringRef S : Config->DynamicList)
555 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000556 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000557}
558
Rui Ueyama82492142016-11-15 18:41:52 +0000559// Initialize DemangledSyms with a map from demangled symbols to symbol
560// objects. Used to handle "extern C++" directive in version scripts.
561//
562// The map will contain all demangled symbols. That can be very large,
563// and in LLD we generally want to avoid do anything for each symbol.
564// Then, why are we doing this? Here's why.
565//
566// Users can use "extern C++ {}" directive to match against demangled
567// C++ symbols. For example, you can write a pattern such as
568// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
569// other than trying to match a pattern against all demangled symbols.
570// So, if "extern C++" feature is used, we need to demangle all known
571// symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000572template <class ELFT>
Rui Ueyama82492142016-11-15 18:41:52 +0000573void SymbolTable<ELFT>::initDemangledSyms() {
574 if (DemangledSyms)
575 return;
576 DemangledSyms.emplace();
577
Rui Ueyamad6328522016-07-18 01:34:57 +0000578 for (Symbol *Sym : SymVector) {
579 SymbolBody *B = Sym->body();
Rui Ueyama4c5b8ce2016-12-07 23:17:05 +0000580 if (Optional<std::string> S = demangle(B->getName()))
581 (*DemangledSyms)[*S].push_back(B);
582 else
583 (*DemangledSyms)[B->getName()].push_back(B);
Rui Ueyamad6328522016-07-18 01:34:57 +0000584 }
George Rimar50dcece2016-07-16 12:26:39 +0000585}
586
Rui Ueyama82492142016-11-15 18:41:52 +0000587template <class ELFT>
588ArrayRef<SymbolBody *> SymbolTable<ELFT>::findDemangled(StringRef Name) {
589 initDemangledSyms();
590 auto I = DemangledSyms->find(Name);
591 if (I != DemangledSyms->end())
George Rimarc3ec9d02016-08-30 09:29:37 +0000592 return I->second;
George Rimar31c25ae2016-09-15 12:44:38 +0000593 return {};
George Rimarc3ec9d02016-08-30 09:29:37 +0000594}
595
Rui Ueyama82492142016-11-15 18:41:52 +0000596template <class ELFT>
597std::vector<SymbolBody *>
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000598SymbolTable<ELFT>::findAllDemangled(StringRef GlobPat) {
Rui Ueyama82492142016-11-15 18:41:52 +0000599 initDemangledSyms();
George Rimar397cd87a2016-08-30 09:35:03 +0000600 std::vector<SymbolBody *> Res;
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000601 StringMatcher M({GlobPat});
Rui Ueyama82492142016-11-15 18:41:52 +0000602 for (auto &P : *DemangledSyms)
603 if (M.match(P.first()))
George Rimar31c25ae2016-09-15 12:44:38 +0000604 for (SymbolBody *Body : P.second)
605 if (!Body->isUndefined())
606 Res.push_back(Body);
George Rimar397cd87a2016-08-30 09:35:03 +0000607 return Res;
608}
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) {
617 if (hasWildcard(Ver.Name)) {
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000618 for (SymbolBody *B : findAll(Ver.Name))
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 Ueyamabaf7ee32016-11-15 17:51:09 +0000622 if (SymbolBody *B = find(Ver.Name))
Rui Ueyamaea265042016-09-13 20:51:30 +0000623 B->symbol()->VersionId = VER_NDX_GLOBAL;
624 }
Rui Ueyamaea265042016-09-13 20:51:30 +0000625}
626
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000627// Set symbol versions to symbols. This function handles patterns
628// containing no wildcard characters.
629template <class ELFT>
Rui Ueyamada805c42016-11-17 03:39:21 +0000630void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000631 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000632 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000633 return;
634
635 // Get a list of symbols which we need to assign the version to.
636 std::vector<SymbolBody *> Syms;
637 if (Ver.IsExternCpp)
638 Syms = findDemangled(Ver.Name);
639 else
640 Syms.push_back(find(Ver.Name));
641
642 // Assign the version.
643 for (SymbolBody *B : Syms) {
644 if (!B || B->isUndefined()) {
645 if (Config->NoUndefinedVersion)
Rui Ueyamad84124f2016-11-17 19:57:47 +0000646 error("version script assignment of '" + VersionName + "' to symbol '" +
647 Ver.Name + "' failed: symbol not defined");
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000648 continue;
649 }
650
651 if (B->symbol()->VersionId != Config->DefaultSymbolVersion)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000652 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000653 B->symbol()->VersionId = VersionId;
654 }
655}
656
Rui Ueyama82492142016-11-15 18:41:52 +0000657template <class ELFT>
658void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
Rui Ueyamada805c42016-11-17 03:39:21 +0000659 uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000660 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000661 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000662 std::vector<SymbolBody *> Syms =
Rui Ueyamabac1c3c2016-11-17 16:48:53 +0000663 Ver.IsExternCpp ? findAllDemangled(Ver.Name) : findAll(Ver.Name);
Rui Ueyama82492142016-11-15 18:41:52 +0000664
665 // Exact matching takes precendence over fuzzy matching,
666 // so we set a version to a symbol only if no version has been assigned
667 // to the symbol. This behavior is compatible with GNU.
668 for (SymbolBody *B : Syms)
669 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
670 B->symbol()->VersionId = VersionId;
671}
672
Rui Ueyamadad2b882016-09-02 22:15:08 +0000673// This function processes version scripts by updating VersionId
674// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000675template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000676 // Symbol themselves might know their versions because symbols
677 // can contain versions in the form of <name>@<version>.
678 // Let them parse their names.
679 if (!Config->VersionDefinitions.empty())
680 for (Symbol *Sym : SymVector)
681 Sym->body()->parseSymbolVersion();
682
Rui Ueyamaea265042016-09-13 20:51:30 +0000683 // Handle edge cases first.
George Rimard3566302016-06-20 11:55:12 +0000684 if (!Config->VersionScriptGlobals.empty()) {
Rui Ueyamaea265042016-09-13 20:51:30 +0000685 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000686 return;
687 }
688
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000689 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000690 return;
691
Rui Ueyamadad2b882016-09-02 22:15:08 +0000692 // Now we have version definitions, so we need to set version ids to symbols.
693 // Each version definition has a glob pattern, and all symbols that match
694 // with the pattern get that version.
695
Rui Ueyamadad2b882016-09-02 22:15:08 +0000696 // First, we assign versions to exact matching symbols,
697 // i.e. version definitions not containing any glob meta-characters.
Rui Ueyama96db27c2016-11-17 19:57:45 +0000698 for (SymbolVersion &Ver : Config->VersionScriptLocals)
699 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
George Rimar17c65af2016-11-16 18:46:23 +0000700 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000701 for (SymbolVersion &Ver : V.Globals)
702 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000703
Rui Ueyamadad2b882016-09-02 22:15:08 +0000704 // Next, we assign versions to fuzzy matching symbols,
705 // i.e. version definitions containing glob meta-characters.
706 // Note that because the last match takes precedence over previous matches,
707 // we iterate over the definitions in the reverse order.
George Rimar17c65af2016-11-16 18:46:23 +0000708 for (SymbolVersion &Ver : Config->VersionScriptLocals)
709 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamacd236a92016-11-17 19:57:43 +0000710 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
711 for (SymbolVersion &Ver : V.Globals)
712 assignWildcardVersion(Ver, V.Id);
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000713}
714
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000715template class elf::SymbolTable<ELF32LE>;
716template class elf::SymbolTable<ELF32BE>;
717template class elf::SymbolTable<ELF64LE>;
718template class elf::SymbolTable<ELF64BE>;