blob: 78a2df593646e5b236a2e8f9e6f522445c8aa0ca [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) {
Rui Ueyama330e52b2017-04-26 22:51:51 +000055 if (!Config->FirstElf && isa<ELFFileBase<ELFT>>(File))
56 Config->FirstElf = File;
57
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000058 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000059 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000060
Michael J. Spencera9424f32016-09-09 22:08:04 +000061 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000062 if (auto *F = dyn_cast<BinaryFile>(File)) {
Rafael Espindola093abab2016-10-27 17:45:40 +000063 BinaryFiles.push_back(F);
64 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000065 return;
66 }
67
Rui Ueyama89575742015-12-16 22:59:13 +000068 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000069 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000070 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000071 return;
72 }
Rui Ueyama3d451792015-10-12 18:03:21 +000073
George Rimar2a78fce2016-04-13 18:07:57 +000074 // Lazy object file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000075 if (auto *F = dyn_cast<LazyObjectFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000076 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000077 return;
78 }
79
80 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000081 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000082
Rui Ueyama89575742015-12-16 22:59:13 +000083 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000084 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000085 // DSOs are uniquified not by filename but by soname.
86 F->parseSoName();
Rafael Espindola3460cdd2017-04-24 21:44:20 +000087 if (ErrorCount || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000088 return;
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000089 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000090 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000091 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000092 }
Rui Ueyama89575742015-12-16 22:59:13 +000093
Rui Ueyamaf8baa662016-04-07 19:24:51 +000094 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000095 if (auto *F = dyn_cast<BitcodeFile>(File)) {
96 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +000097 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000098 return;
99 }
100
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000101 // Regular object file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000102 auto *F = cast<ObjectFile<ELFT>>(File);
103 ObjectFiles.push_back(F);
Rui Ueyama52d3b672016-01-06 02:06:33 +0000104 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000105}
106
Rui Ueyama42554752016-04-23 00:26:32 +0000107// This function is where all the optimizations of link-time
108// optimization happens. When LTO is in use, some input files are
109// not in native object file format but in the LLVM bitcode format.
110// This function compiles bitcode files into a few big native files
111// using LLVM functions and replaces bitcode symbols with the results.
112// Because all bitcode files that consist of a program are passed
113// to the compiler at once, it can do whole-program optimization.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000114template <class ELFT> void SymbolTable<ELFT>::addCombinedLTOObject() {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000115 if (BitcodeFiles.empty())
116 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000117
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000118 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000119 LTO.reset(new BitcodeCompiler);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000120 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000121 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000122
Davide Italiano3bfa0812016-11-26 05:37:04 +0000123 for (InputFile *File : LTO->compile()) {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000124 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(File);
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000125 DenseSet<CachedHashStringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000126 Obj->parse(DummyGroups);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000127 ObjectFiles.push_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000128 }
129}
130
Rafael Espindola0e604f92015-09-25 18:56:53 +0000131template <class ELFT>
Rui Ueyama80474a22017-02-28 19:29:55 +0000132DefinedRegular *SymbolTable<ELFT>::addAbsolute(StringRef Name,
133 uint8_t Visibility,
134 uint8_t Binding) {
Simon Atanasyan6a4eb752016-12-08 06:19:47 +0000135 Symbol *Sym =
Simon Atanasyan872764f2016-12-08 15:29:17 +0000136 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, nullptr, nullptr);
Rui Ueyama80474a22017-02-28 19:29:55 +0000137 return cast<DefinedRegular>(Sym->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000138}
139
Rui Ueyamac9559d92016-01-05 20:47:37 +0000140// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000141// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000142template <class ELFT>
Rui Ueyama80474a22017-02-28 19:29:55 +0000143DefinedRegular *SymbolTable<ELFT>::addIgnored(StringRef Name,
144 uint8_t Visibility) {
Rafael Espindola95eae572016-11-16 18:01:41 +0000145 SymbolBody *S = find(Name);
Rafael Espindolab92e99c2017-01-19 19:43:34 +0000146 if (!S || S->isInCurrentDSO())
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000147 return nullptr;
148 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000149}
150
Rui Ueyama69c778c2016-07-17 17:50:09 +0000151// Set a flag for --trace-symbol so that we can print out a log message
152// if a new symbol with the same name is inserted into the symbol table.
153template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000154 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000155}
156
Rui Ueyamadeb15402016-01-07 17:20:07 +0000157// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
158// Used to implement --wrap.
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000159template <class ELFT> void SymbolTable<ELFT>::addSymbolWrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000160 SymbolBody *B = find(Name);
161 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000162 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000163 Symbol *Sym = B->symbol();
164 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
165 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000166
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000167 // Tell LTO not to eliminate this symbol
168 Wrap->IsUsedInRegularObj = true;
169
Rui Ueyama4402a392017-06-22 17:30:19 +0000170 Config->RenamedSymbols[Real] = {Sym, Real->Binding};
171 Config->RenamedSymbols[Sym] = {Wrap, Sym->Binding};
Rui Ueyamadeb15402016-01-07 17:20:07 +0000172}
173
George Rimar9703ad22017-04-26 10:40:02 +0000174// Creates alias for symbol. Used to implement --defsym=ALIAS=SYM.
George Rimar67c60722017-07-18 11:55:35 +0000175template <class ELFT>
176void SymbolTable<ELFT>::addSymbolAlias(StringRef Alias, StringRef Name) {
George Rimar9703ad22017-04-26 10:40:02 +0000177 SymbolBody *B = find(Name);
178 if (!B) {
179 error("-defsym: undefined symbol: " + Name);
180 return;
181 }
182 Symbol *Sym = B->symbol();
183 Symbol *AliasSym = addUndefined(Alias);
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000184
185 // Tell LTO not to eliminate this symbol
186 Sym->IsUsedInRegularObj = true;
Rui Ueyama4402a392017-06-22 17:30:19 +0000187 Config->RenamedSymbols[AliasSym] = {Sym, AliasSym->Binding};
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000188}
189
190// Apply symbol renames created by -wrap and -defsym. The renames are created
191// before LTO in addSymbolWrap() and addSymbolAlias() to have a chance to inform
192// LTO (if LTO is running) not to include these symbols in IPO. Now that the
193// symbols are finalized, we can perform the replacement.
194template <class ELFT> void SymbolTable<ELFT>::applySymbolRenames() {
195 for (auto &KV : Config->RenamedSymbols) {
Rui Ueyama4402a392017-06-22 17:30:19 +0000196 Symbol *Dst = KV.first;
197 Symbol *Src = KV.second.Target;
Rui Ueyamab2269ec2017-06-28 19:43:02 +0000198 Dst->body()->copy(Src->body());
Rui Ueyama4402a392017-06-22 17:30:19 +0000199 Dst->Binding = KV.second.OriginalBinding;
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000200 }
George Rimar9703ad22017-04-26 10:40:02 +0000201}
202
Peter Collingbournedadcc172016-04-22 18:42:48 +0000203static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
204 if (VA == STV_DEFAULT)
205 return VB;
206 if (VB == STV_DEFAULT)
207 return VA;
208 return std::min(VA, VB);
209}
210
Rui Ueyamab4de5952016-01-08 22:01:33 +0000211// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000212template <class ELFT>
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000213std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000214 // <name>@@<version> means the symbol is the default version. In that
215 // case <name>@@<version> will be used to resolve references to <name>.
216 size_t Pos = Name.find("@@");
217 if (Pos != StringRef::npos)
218 Name = Name.take_front(Pos);
219
Justin Lebar3c11e932016-10-18 17:50:36 +0000220 auto P = Symtab.insert(
221 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000222 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000223 bool IsNew = P.second;
224
Rui Ueyamae3357902016-07-18 01:35:00 +0000225 if (V.Idx == -1) {
226 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000227 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000228 }
229
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000230 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000231 if (IsNew) {
Rui Ueyama175e81c2017-02-28 19:36:30 +0000232 Sym = make<Symbol>();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000233 Sym->InVersionScript = false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000234 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000235 Sym->Visibility = STV_DEFAULT;
236 Sym->IsUsedInRegularObj = false;
237 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000238 Sym->Traced = V.Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000239 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000240 SymVector.push_back(Sym);
241 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000242 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000243 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000244 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000245}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000246
Peter Collingbourne4f952702016-05-01 04:55:03 +0000247// Find an existing symbol or create and insert a new one, then apply the given
248// attributes.
249template <class ELFT>
250std::pair<Symbol *, bool>
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000251SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
Davide Italiano786d8e32016-09-29 00:40:08 +0000252 bool CanOmitFromDynSym, InputFile *File) {
Rafael Espindola05098762016-08-31 13:49:23 +0000253 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000254 Symbol *S;
255 bool WasInserted;
256 std::tie(S, WasInserted) = insert(Name);
257
258 // Merge in the new symbol's visibility.
259 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000260
Peter Collingbourne4f952702016-05-01 04:55:03 +0000261 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
262 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000263
Peter Collingbourne4f952702016-05-01 04:55:03 +0000264 if (IsUsedInRegularObj)
265 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000266
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000267 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
Rui Ueyama810ce102017-03-31 23:40:21 +0000268 ((Type == STT_TLS) != S->body()->isTls())) {
269 error("TLS attribute mismatch: " + toString(*S->body()) +
270 "\n>>> defined in " + toString(S->body()->File) +
271 "\n>>> defined in " + toString(File));
272 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000273
274 return {S, WasInserted};
275}
276
Peter Collingbourne4f952702016-05-01 04:55:03 +0000277template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000278 return addUndefined(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
279 /*Type*/ 0,
Davide Italiano786d8e32016-09-29 00:40:08 +0000280 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000281}
282
Rui Ueyamae50e8072016-12-22 05:11:12 +0000283static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
284
Peter Collingbourne4f952702016-05-01 04:55:03 +0000285template <class ELFT>
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000286Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, bool IsLocal,
287 uint8_t Binding, uint8_t StOther,
288 uint8_t Type, bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000289 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000290 Symbol *S;
291 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000292 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000293 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000294 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
295 // An undefined symbol with non default visibility must be satisfied
296 // in the same DSO.
297 if (WasInserted ||
298 (isa<SharedSymbol>(S->body()) && Visibility != STV_DEFAULT)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000299 S->Binding = Binding;
Peter Smith3a52eb02017-02-01 10:26:03 +0000300 replaceBody<Undefined>(S, Name, IsLocal, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000301 return S;
302 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000303 if (Binding != STB_WEAK) {
Rafael Espindola5e20c752017-05-03 18:40:27 +0000304 SymbolBody *B = S->body();
305 if (B->isShared() || B->isLazy() || B->isUndefined())
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000306 S->Binding = Binding;
Rafael Espindola5e20c752017-05-03 18:40:27 +0000307 if (auto *SS = dyn_cast<SharedSymbol>(B))
Rui Ueyama4076fa12017-02-26 23:35:34 +0000308 cast<SharedFile<ELFT>>(SS->File)->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000309 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000310 if (auto *L = dyn_cast<Lazy>(S->body())) {
311 // An undefined weak will not fetch archive members, but we have to remember
312 // its type. See also comment in addLazyArchive.
313 if (S->isWeak())
314 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000315 else if (InputFile *F = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000316 addFile(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000317 }
318 return S;
319}
320
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000321// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
322// foo@@VER. We want to effectively ignore foo, so give precedence to
323// foo@@VER.
324// FIXME: If users can transition to using
325// .symver foo,foo@@@VER
326// we can delete this hack.
327static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama12234f82017-07-19 21:40:26 +0000328 if (Name.contains("@@") && !S->body()->getName().contains("@@"))
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000329 return 1;
Rui Ueyama12234f82017-07-19 21:40:26 +0000330 if (!Name.contains("@@") && S->body()->getName().contains("@@"))
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000331 return -1;
332 return 0;
333}
334
Peter Collingbourne4f952702016-05-01 04:55:03 +0000335// We have a new defined symbol with the specified binding. Return 1 if the new
336// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
337// strong defined symbols.
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000338static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
339 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000340 if (WasInserted)
341 return 1;
342 SymbolBody *Body = S->body();
George Rimard92e1282017-07-12 11:09:46 +0000343 if (!Body->isInCurrentDSO())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000344 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000345
346 if (int R = compareVersion(S, Name))
347 return R;
348
Peter Collingbourne4f952702016-05-01 04:55:03 +0000349 if (Binding == STB_WEAK)
350 return -1;
351 if (S->isWeak())
352 return 1;
353 return 0;
354}
355
356// We have a new non-common defined symbol with the specified binding. Return 1
357// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
358// is a conflict. If the new symbol wins, also update the binding.
Rafael Espindola858c0922016-12-02 02:58:21 +0000359template <typename ELFT>
360static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000361 bool IsAbsolute, typename ELFT::uint Value,
362 StringRef Name) {
363 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000364 if (Cmp > 0)
365 S->Binding = Binding;
366 return Cmp;
367 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000368 SymbolBody *B = S->body();
369 if (isa<DefinedCommon>(B)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000370 // Non-common symbols take precedence over common symbols.
371 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000372 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000373 return 1;
Rui Ueyama80474a22017-02-28 19:29:55 +0000374 } else if (auto *R = dyn_cast<DefinedRegular>(B)) {
Rafael Espindola858c0922016-12-02 02:58:21 +0000375 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
376 R->Value == Value)
377 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000378 }
379 return 0;
380}
381
382template <class ELFT>
383Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000384 uint32_t Alignment, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000385 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000386 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000387 Symbol *S;
388 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000389 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
390 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000391 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000392 if (Cmp > 0) {
393 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000394 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000395 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000396 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000397 if (!C) {
398 // Non-common symbols take precedence over common symbols.
399 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000400 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000401 return S;
402 }
403
404 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000405 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000406
Rafael Espindola8db87292016-08-31 13:42:08 +0000407 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
408 if (Size > C->Size)
409 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 }
411 return S;
412}
413
Rui Ueyama810ce102017-03-31 23:40:21 +0000414static void warnOrError(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000415 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000416 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000417 else
418 error(Msg);
419}
420
Rui Ueyama810ce102017-03-31 23:40:21 +0000421static void reportDuplicate(SymbolBody *Sym, InputFile *NewFile) {
George Rimar67c60722017-07-18 11:55:35 +0000422 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
423 toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000424}
425
426template <class ELFT>
Rui Ueyama810ce102017-03-31 23:40:21 +0000427static void reportDuplicate(SymbolBody *Sym, InputSectionBase *ErrSec,
Eugene Leviant825e5382016-11-08 16:26:32 +0000428 typename ELFT::uint ErrOffset) {
Rui Ueyama810ce102017-03-31 23:40:21 +0000429 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym);
Eugene Leviant825e5382016-11-08 16:26:32 +0000430 if (!D || !D->Section || !ErrSec) {
Rui Ueyama810ce102017-03-31 23:40:21 +0000431 reportDuplicate(Sym, ErrSec ? ErrSec->getFile<ELFT>() : nullptr);
Eugene Leviant825e5382016-11-08 16:26:32 +0000432 return;
433 }
434
Rui Ueyama810ce102017-03-31 23:40:21 +0000435 // Construct and print an error message in the form of:
436 //
437 // ld.lld: error: duplicate symbol: foo
438 // >>> defined at bar.c:30
439 // >>> bar.o (/home/alice/src/bar.o)
440 // >>> defined at baz.c:563
441 // >>> baz.o in archive libbaz.a
442 auto *Sec1 = cast<InputSectionBase>(D->Section);
443 std::string Src1 = Sec1->getSrcMsg<ELFT>(D->Value);
444 std::string Obj1 = Sec1->getObjMsg<ELFT>(D->Value);
445 std::string Src2 = ErrSec->getSrcMsg<ELFT>(ErrOffset);
446 std::string Obj2 = ErrSec->getObjMsg<ELFT>(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000447
Rui Ueyama810ce102017-03-31 23:40:21 +0000448 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
449 if (!Src1.empty())
450 Msg += Src1 + "\n>>> ";
451 Msg += Obj1 + "\n>>> defined at ";
452 if (!Src2.empty())
453 Msg += Src2 + "\n>>> ";
454 Msg += Obj2;
455 warnOrError(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000456}
457
Peter Collingbourne4f952702016-05-01 04:55:03 +0000458template <typename ELFT>
Rafael Espindola5616adf2017-03-08 22:36:28 +0000459Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
460 uint8_t Type, uint64_t Value,
461 uint64_t Size, uint8_t Binding,
462 SectionBase *Section, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000463 Symbol *S;
464 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000465 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000466 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola858c0922016-12-02 02:58:21 +0000467 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000468 Section == nullptr, Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000469 if (Cmp > 0)
Rui Ueyama80474a22017-02-28 19:29:55 +0000470 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type,
471 Value, Size, Section, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000472 else if (Cmp == 0)
Rafael Espindola5616adf2017-03-08 22:36:28 +0000473 reportDuplicate<ELFT>(S->body(),
474 dyn_cast_or_null<InputSectionBase>(Section), Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000475 return S;
476}
477
478template <typename ELFT>
Rui Ueyama4076fa12017-02-26 23:35:34 +0000479void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *File, StringRef Name,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000480 const Elf_Sym &Sym,
481 const typename ELFT::Verdef *Verdef) {
482 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
483 // as the visibility, which will leave the visibility in the symbol table
484 // unchanged.
485 Symbol *S;
486 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000487 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
488 /*CanOmitFromDynSym*/ true, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000489 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000490 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000491 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000492
Rafael Espindola8465d0832017-04-04 20:03:34 +0000493 SymbolBody *Body = S->body();
494 // An undefined symbol with non default visibility must be satisfied
495 // in the same DSO.
496 if (WasInserted ||
497 (isa<Undefined>(Body) && Body->getVisibility() == STV_DEFAULT)) {
Rui Ueyama4076fa12017-02-26 23:35:34 +0000498 replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(), &Sym,
499 Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000500 if (!S->isWeak())
Rui Ueyama4076fa12017-02-26 23:35:34 +0000501 File->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000502 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000503}
504
505template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000506Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000507 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000508 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000509 Symbol *S;
510 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000511 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000512 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindola858c0922016-12-02 02:58:21 +0000513 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000514 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515 if (Cmp > 0)
Rui Ueyama80474a22017-02-28 19:29:55 +0000516 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type, 0, 0,
517 nullptr, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000518 else if (Cmp == 0)
519 reportDuplicate(S->body(), F);
520 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000521}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000522
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000523template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000524 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000525 if (It == Symtab.end())
526 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000527 SymIndex V = It->second;
528 if (V.Idx == -1)
529 return nullptr;
530 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000531}
532
Davide Italiano8e1131d2016-06-29 02:46:51 +0000533template <class ELFT>
Rafael Espindola1d6d1b42017-01-17 16:08:06 +0000534SymbolBody *SymbolTable<ELFT>::findInCurrentDSO(StringRef Name) {
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000535 if (SymbolBody *S = find(Name))
Rafael Espindola1d6d1b42017-01-17 16:08:06 +0000536 if (S->isInCurrentDSO())
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000537 return S;
538 return nullptr;
539}
540
541template <class ELFT>
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000542Symbol *SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
543 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000544 Symbol *S;
545 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000546 StringRef Name = Sym.getName();
547 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000548 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000549 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000550 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000551 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000552 if (!S->body()->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000553 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000554
Peter Collingbourne4f952702016-05-01 04:55:03 +0000555 // Weak undefined symbols should not fetch members from archives. If we were
556 // to keep old symbol we would not know that an archive member was available
557 // if a strong undefined symbol shows up afterwards in the link. If a strong
558 // undefined symbol never shows up, this lazy symbol will get to the end of
559 // the link and must be treated as the weak undefined one. We already marked
560 // this symbol as used when we added it to the symbol table, but we also need
561 // to preserve its type. FIXME: Move the Type field to Symbol.
562 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000563 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000564 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000565 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000566 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
567 if (!MBInfo.first.getBuffer().empty())
Rui Ueyama55518e72016-10-28 20:57:25 +0000568 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000569 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000570}
571
572template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000573void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000574 Symbol *S;
575 bool WasInserted;
576 std::tie(S, WasInserted) = insert(Name);
577 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000578 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000579 return;
580 }
581 if (!S->body()->isUndefined())
582 return;
583
584 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000585 if (S->isWeak())
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000586 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000587 else if (InputFile *F = Obj.fetch())
588 addFile(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000589}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000590
Peter Collingbourne892d49802016-04-27 00:05:03 +0000591// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000592template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000593 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000594 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000595 if (InputFile *File = L->fetch())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000596 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000597}
598
Rui Ueyama93bfee52015-10-13 18:10:33 +0000599// This function takes care of the case in which shared libraries depend on
600// the user program (not the other way, which is usual). Shared libraries
601// may have undefined symbols, expecting that the user program provides
602// the definitions for them. An example is BSD's __progname symbol.
603// We need to put such symbols to the main program's .dynsym so that
604// shared libraries can find them.
605// Except this, we ignore undefined symbols in DSOs.
606template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000607 for (SharedFile<ELFT> *File : SharedFiles) {
608 for (StringRef U : File->getUndefinedSymbols()) {
609 SymbolBody *Sym = find(U);
610 if (!Sym || !Sym->isDefined())
611 continue;
612 Sym->symbol()->ExportDynamic = true;
613
614 // If -dynamic-list is given, the default version is set to
615 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
616 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
617 // specified by -dynamic-list.
618 Sym->symbol()->VersionId = VER_NDX_GLOBAL;
619 }
620 }
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000621}
622
Rui Ueyama82492142016-11-15 18:41:52 +0000623// Initialize DemangledSyms with a map from demangled symbols to symbol
624// objects. Used to handle "extern C++" directive in version scripts.
625//
626// The map will contain all demangled symbols. That can be very large,
627// and in LLD we generally want to avoid do anything for each symbol.
628// Then, why are we doing this? Here's why.
629//
630// Users can use "extern C++ {}" directive to match against demangled
631// C++ symbols. For example, you can write a pattern such as
632// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
633// other than trying to match a pattern against all demangled symbols.
634// So, if "extern C++" feature is used, we need to demangle all known
635// symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000636template <class ELFT>
Rui Ueyama96aff372016-12-22 05:31:52 +0000637StringMap<std::vector<SymbolBody *>> &SymbolTable<ELFT>::getDemangledSyms() {
638 if (!DemangledSyms) {
639 DemangledSyms.emplace();
640 for (Symbol *Sym : SymVector) {
641 SymbolBody *B = Sym->body();
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000642 if (B->isUndefined())
643 continue;
Rui Ueyama96aff372016-12-22 05:31:52 +0000644 if (Optional<std::string> S = demangle(B->getName()))
645 (*DemangledSyms)[*S].push_back(B);
646 else
647 (*DemangledSyms)[B->getName()].push_back(B);
648 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000649 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000650 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000651}
652
Rui Ueyama82492142016-11-15 18:41:52 +0000653template <class ELFT>
Rui Ueyama86581e42016-12-10 00:34:06 +0000654std::vector<SymbolBody *> SymbolTable<ELFT>::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000655 if (Ver.IsExternCpp)
656 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000657 if (SymbolBody *B = find(Ver.Name))
658 if (!B->isUndefined())
659 return {B};
660 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000661}
662
663template <class ELFT>
Rui Ueyama86581e42016-12-10 00:34:06 +0000664std::vector<SymbolBody *>
665SymbolTable<ELFT>::findAllByVersion(SymbolVersion Ver) {
Rafael Espindola191390a2016-12-08 16:26:20 +0000666 std::vector<SymbolBody *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000667 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000668
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000669 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000670 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000671 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000672 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000673 return Res;
674 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000675
676 for (Symbol *Sym : SymVector) {
677 SymbolBody *B = Sym->body();
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000678 if (!B->isUndefined() && M.match(B->getName()))
Rafael Espindola191390a2016-12-08 16:26:20 +0000679 Res.push_back(B);
680 }
681 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000682}
683
Rui Ueyamaea265042016-09-13 20:51:30 +0000684// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000685// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000686// but just specifies symbols visibilities.
Rui Ueyamaea265042016-09-13 20:51:30 +0000687template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000688 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
689 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
690 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
691 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
692 for (SymbolVersion &Ver : Config->VersionScriptLocals)
693 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
694 for (SymbolVersion &Ver : Config->VersionScriptLocals)
695 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000696}
697
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000698// Set symbol versions to symbols. This function handles patterns
699// containing no wildcard characters.
700template <class ELFT>
George Rimar67c60722017-07-18 11:55:35 +0000701void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver,
702 uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000703 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000704 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000705 return;
706
707 // Get a list of symbols which we need to assign the version to.
Rui Ueyama86581e42016-12-10 00:34:06 +0000708 std::vector<SymbolBody *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000709 if (Syms.empty()) {
710 if (Config->NoUndefinedVersion)
711 error("version script assignment of '" + VersionName + "' to symbol '" +
712 Ver.Name + "' failed: symbol not defined");
713 return;
714 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000715
716 // Assign the version.
717 for (SymbolBody *B : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000718 // Skip symbols containing version info because symbol versions
719 // specified by symbol names take precedence over version scripts.
720 // See parseSymbolVersion().
Rui Ueyama12234f82017-07-19 21:40:26 +0000721 if (B->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000722 continue;
723
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000724 Symbol *Sym = B->symbol();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000725 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000726 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000727 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000728 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000729 }
730}
731
Rui Ueyama82492142016-11-15 18:41:52 +0000732template <class ELFT>
733void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
Rui Ueyamada805c42016-11-17 03:39:21 +0000734 uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000735 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000736 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000737
738 // Exact matching takes precendence over fuzzy matching,
739 // so we set a version to a symbol only if no version has been assigned
740 // to the symbol. This behavior is compatible with GNU.
Rui Ueyama9a189872017-07-11 20:33:04 +0000741 for (SymbolBody *B : findAllByVersion(Ver))
Rui Ueyama82492142016-11-15 18:41:52 +0000742 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
743 B->symbol()->VersionId = VersionId;
744}
745
Rui Ueyamadad2b882016-09-02 22:15:08 +0000746// This function processes version scripts by updating VersionId
747// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000748template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000749 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000750 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000751
Rui Ueyamadad2b882016-09-02 22:15:08 +0000752 // Now we have version definitions, so we need to set version ids to symbols.
753 // Each version definition has a glob pattern, and all symbols that match
754 // with the pattern get that version.
755
Rui Ueyamadad2b882016-09-02 22:15:08 +0000756 // First, we assign versions to exact matching symbols,
757 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000758 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000759 for (SymbolVersion &Ver : V.Globals)
760 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000761
Rui Ueyamadad2b882016-09-02 22:15:08 +0000762 // Next, we assign versions to fuzzy matching symbols,
763 // i.e. version definitions containing glob meta-characters.
764 // Note that because the last match takes precedence over previous matches,
765 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000766 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
767 for (SymbolVersion &Ver : V.Globals)
768 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000769
770 // Symbol themselves might know their versions because symbols
771 // can contain versions in the form of <name>@<version>.
772 // Let them parse and update their names to exclude version suffix.
773 for (Symbol *Sym : SymVector)
774 Sym->body()->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000775}
776
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000777template class elf::SymbolTable<ELF32LE>;
778template class elf::SymbolTable<ELF32BE>;
779template class elf::SymbolTable<ELF64LE>;
780template class elf::SymbolTable<ELF64BE>;