blob: ec8ae5d75bb283c916b5ea91986d2a000f88db9a [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
Rafael Espindola244ef982017-07-26 18:42:48 +000032SymbolTable *elf::Symtab;
33
Rui Ueyamac9559d92016-01-05 20:47:37 +000034// All input object files must be for the same architecture
35// (e.g. it does not make sense to link x86 object files with
36// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000037template <class ELFT> static bool isCompatible(InputFile *F) {
38 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000039 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000040
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000041 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
42 if (Config->EMachine != EM_MIPS)
43 return true;
44 if (isMipsN32Abi(F) == Config->MipsN32Abi)
45 return true;
46 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000047
48 if (!Config->Emulation.empty())
49 error(toString(F) + " is incompatible with " + Config->Emulation);
50 else
51 error(toString(F) + " is incompatible with " + toString(Config->FirstElf));
Rui Ueyama16ba6692016-01-29 19:41:13 +000052 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000053}
54
Rui Ueyamac9559d92016-01-05 20:47:37 +000055// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000056template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rui Ueyama330e52b2017-04-26 22:51:51 +000057 if (!Config->FirstElf && isa<ELFFileBase<ELFT>>(File))
58 Config->FirstElf = File;
59
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000060 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000061 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000062
Michael J. Spencera9424f32016-09-09 22:08:04 +000063 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000064 if (auto *F = dyn_cast<BinaryFile>(File)) {
Rafael Espindola244ef982017-07-26 18:42:48 +000065 BinaryFile::Instances.push_back(F);
Rafael Espindola093abab2016-10-27 17:45:40 +000066 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000067 return;
68 }
69
Rui Ueyama89575742015-12-16 22:59:13 +000070 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000071 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000072 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000073 return;
74 }
Rui Ueyama3d451792015-10-12 18:03:21 +000075
George Rimar2a78fce2016-04-13 18:07:57 +000076 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000077 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000078 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000079 return;
80 }
81
82 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000083 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000084
Rui Ueyama89575742015-12-16 22:59:13 +000085 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000086 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000087 // DSOs are uniquified not by filename but by soname.
88 F->parseSoName();
Rafael Espindola3460cdd2017-04-24 21:44:20 +000089 if (ErrorCount || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000090 return;
Rafael Espindola244ef982017-07-26 18:42:48 +000091 SharedFile<ELFT>::Instances.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000092 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000093 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000094 }
Rui Ueyama89575742015-12-16 22:59:13 +000095
Rui Ueyamaf8baa662016-04-07 19:24:51 +000096 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000097 if (auto *F = dyn_cast<BitcodeFile>(File)) {
Rafael Espindola244ef982017-07-26 18:42:48 +000098 BitcodeFile::Instances.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +000099 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000100 return;
101 }
102
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000103 // Regular object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000104 auto *F = cast<ObjFile<ELFT>>(File);
105 ObjFile<ELFT>::Instances.push_back(F);
Rui Ueyama52d3b672016-01-06 02:06:33 +0000106 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000107}
108
Rui Ueyama42554752016-04-23 00:26:32 +0000109// This function is where all the optimizations of link-time
110// optimization happens. When LTO is in use, some input files are
111// not in native object file format but in the LLVM bitcode format.
112// This function compiles bitcode files into a few big native files
113// using LLVM functions and replaces bitcode symbols with the results.
114// Because all bitcode files that consist of a program are passed
115// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000116template <class ELFT> void SymbolTable::addCombinedLTOObject() {
117 if (BitcodeFile::Instances.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000118 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000119
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000120 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000121 LTO.reset(new BitcodeCompiler);
Rafael Espindola244ef982017-07-26 18:42:48 +0000122 for (BitcodeFile *F : BitcodeFile::Instances)
Peter Smith3a52eb02017-02-01 10:26:03 +0000123 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000124
Davide Italiano3bfa0812016-11-26 05:37:04 +0000125 for (InputFile *File : LTO->compile()) {
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000126 ObjFile<ELFT> *Obj = cast<ObjFile<ELFT>>(File);
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000127 DenseSet<CachedHashStringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000128 Obj->parse(DummyGroups);
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000129 ObjFile<ELFT>::Instances.push_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000130 }
131}
132
Rafael Espindola0e604f92015-09-25 18:56:53 +0000133template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000134DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
135 uint8_t Binding) {
136 Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding,
137 nullptr, nullptr);
Rui Ueyama80474a22017-02-28 19:29:55 +0000138 return cast<DefinedRegular>(Sym->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000139}
140
Rui Ueyamac9559d92016-01-05 20:47:37 +0000141// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000142// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000143template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000144DefinedRegular *SymbolTable::addIgnored(StringRef Name, 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;
Rafael Espindola244ef982017-07-26 18:42:48 +0000148 return addAbsolute<ELFT>(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.
Rafael Espindola244ef982017-07-26 18:42:48 +0000153void SymbolTable::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.
Rafael Espindola244ef982017-07-26 18:42:48 +0000159template <class ELFT> void SymbolTable::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();
Rafael Espindola244ef982017-07-26 18:42:48 +0000164 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
165 Symbol *Wrap = addUndefined<ELFT>(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>
Rafael Espindola244ef982017-07-26 18:42:48 +0000176void SymbolTable::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();
Rafael Espindola244ef982017-07-26 18:42:48 +0000183 Symbol *AliasSym = addUndefined<ELFT>(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.
Rafael Espindola244ef982017-07-26 18:42:48 +0000194void SymbolTable::applySymbolRenames() {
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000195 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());
Rafael Espindola6e93d052017-08-04 22:31:42 +0000199 Dst->File = Src->File;
Rui Ueyama4402a392017-06-22 17:30:19 +0000200 Dst->Binding = KV.second.OriginalBinding;
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000201 }
George Rimar9703ad22017-04-26 10:40:02 +0000202}
203
Peter Collingbournedadcc172016-04-22 18:42:48 +0000204static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
205 if (VA == STV_DEFAULT)
206 return VB;
207 if (VB == STV_DEFAULT)
208 return VA;
209 return std::min(VA, VB);
210}
211
Rui Ueyamab4de5952016-01-08 22:01:33 +0000212// Find an existing symbol or create and insert a new one.
Rafael Espindola244ef982017-07-26 18:42:48 +0000213std::pair<Symbol *, bool> SymbolTable::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.
Rafael Espindola244ef982017-07-26 18:42:48 +0000249std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
250 uint8_t Visibility,
251 bool CanOmitFromDynSym,
252 InputFile *File) {
Rui Ueyama42479e02017-08-19 00:13:54 +0000253 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjKind;
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()) +
Rafael Espindola6e93d052017-08-04 22:31:42 +0000270 "\n>>> defined in " + toString(S->File) + "\n>>> defined in " +
271 toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000272 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000273
274 return {S, WasInserted};
275}
276
Rafael Espindola244ef982017-07-26 18:42:48 +0000277template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
278 return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
279 /*Type*/ 0,
280 /*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>
Rafael Espindola244ef982017-07-26 18:42:48 +0000286Symbol *SymbolTable::addUndefined(StringRef Name, bool IsLocal, uint8_t Binding,
287 uint8_t StOther, uint8_t Type,
288 bool CanOmitFromDynSym, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000289 Symbol *S;
290 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000291 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000292 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000293 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
294 // An undefined symbol with non default visibility must be satisfied
295 // in the same DSO.
296 if (WasInserted ||
297 (isa<SharedSymbol>(S->body()) && Visibility != STV_DEFAULT)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000298 S->Binding = Binding;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000299 replaceBody<Undefined>(S, File, Name, IsLocal, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000300 return S;
301 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000302 if (Binding != STB_WEAK) {
Rafael Espindola5e20c752017-05-03 18:40:27 +0000303 SymbolBody *B = S->body();
304 if (B->isShared() || B->isLazy() || B->isUndefined())
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000305 S->Binding = Binding;
Rafael Espindola5e20c752017-05-03 18:40:27 +0000306 if (auto *SS = dyn_cast<SharedSymbol>(B))
Rafael Espindola6e93d052017-08-04 22:31:42 +0000307 SS->getFile<ELFT>()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000308 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000309 if (auto *L = dyn_cast<Lazy>(S->body())) {
310 // An undefined weak will not fetch archive members, but we have to remember
311 // its type. See also comment in addLazyArchive.
312 if (S->isWeak())
313 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000314 else if (InputFile *F = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000315 addFile<ELFT>(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000316 }
317 return S;
318}
319
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000320// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
321// foo@@VER. We want to effectively ignore foo, so give precedence to
322// foo@@VER.
323// FIXME: If users can transition to using
324// .symver foo,foo@@@VER
325// we can delete this hack.
326static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000327 bool A = Name.contains("@@");
328 bool B = S->body()->getName().contains("@@");
329 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000330 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000331 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000332 return -1;
333 return 0;
334}
335
Peter Collingbourne4f952702016-05-01 04:55:03 +0000336// We have a new defined symbol with the specified binding. Return 1 if the new
337// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
338// strong defined symbols.
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000339static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
340 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000341 if (WasInserted)
342 return 1;
343 SymbolBody *Body = S->body();
George Rimard92e1282017-07-12 11:09:46 +0000344 if (!Body->isInCurrentDSO())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000345 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000346
347 if (int R = compareVersion(S, Name))
348 return R;
349
Peter Collingbourne4f952702016-05-01 04:55:03 +0000350 if (Binding == STB_WEAK)
351 return -1;
352 if (S->isWeak())
353 return 1;
354 return 0;
355}
356
357// We have a new non-common defined symbol with the specified binding. Return 1
358// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
359// is a conflict. If the new symbol wins, also update the binding.
Rafael Espindola858c0922016-12-02 02:58:21 +0000360static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000361 bool IsAbsolute, uint64_t Value,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000362 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
Rafael Espindola244ef982017-07-26 18:42:48 +0000382Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
383 uint8_t Binding, uint8_t StOther, uint8_t Type,
384 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000385 Symbol *S;
386 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000387 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
388 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000389 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000390 if (Cmp > 0) {
391 S->Binding = Binding;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000392 replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000393 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000394 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000395 if (!C) {
396 // Non-common symbols take precedence over common symbols.
397 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000398 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000399 return S;
400 }
401
402 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000403 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000404
Rafael Espindola8db87292016-08-31 13:42:08 +0000405 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
406 if (Size > C->Size)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000407 replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000408 }
409 return S;
410}
411
Rui Ueyama810ce102017-03-31 23:40:21 +0000412static void warnOrError(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000413 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000414 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000415 else
416 error(Msg);
417}
418
Rui Ueyama810ce102017-03-31 23:40:21 +0000419static void reportDuplicate(SymbolBody *Sym, InputFile *NewFile) {
George Rimar67c60722017-07-18 11:55:35 +0000420 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
Rafael Espindola6e93d052017-08-04 22:31:42 +0000421 toString(Sym->getFile()) + "\n>>> defined in " +
422 toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000423}
424
425template <class ELFT>
Rui Ueyama810ce102017-03-31 23:40:21 +0000426static void reportDuplicate(SymbolBody *Sym, InputSectionBase *ErrSec,
Eugene Leviant825e5382016-11-08 16:26:32 +0000427 typename ELFT::uint ErrOffset) {
Rui Ueyama810ce102017-03-31 23:40:21 +0000428 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym);
Eugene Leviant825e5382016-11-08 16:26:32 +0000429 if (!D || !D->Section || !ErrSec) {
Rafael Espindolacb83c8c2017-07-25 23:23:40 +0000430 reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr);
Eugene Leviant825e5382016-11-08 16:26:32 +0000431 return;
432 }
433
Rui Ueyama810ce102017-03-31 23:40:21 +0000434 // Construct and print an error message in the form of:
435 //
436 // ld.lld: error: duplicate symbol: foo
437 // >>> defined at bar.c:30
438 // >>> bar.o (/home/alice/src/bar.o)
439 // >>> defined at baz.c:563
440 // >>> baz.o in archive libbaz.a
441 auto *Sec1 = cast<InputSectionBase>(D->Section);
442 std::string Src1 = Sec1->getSrcMsg<ELFT>(D->Value);
443 std::string Obj1 = Sec1->getObjMsg<ELFT>(D->Value);
444 std::string Src2 = ErrSec->getSrcMsg<ELFT>(ErrOffset);
445 std::string Obj2 = ErrSec->getObjMsg<ELFT>(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000446
Rui Ueyama810ce102017-03-31 23:40:21 +0000447 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
448 if (!Src1.empty())
449 Msg += Src1 + "\n>>> ";
450 Msg += Obj1 + "\n>>> defined at ";
451 if (!Src2.empty())
452 Msg += Src2 + "\n>>> ";
453 Msg += Obj2;
454 warnOrError(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000455}
456
Peter Collingbourne4f952702016-05-01 04:55:03 +0000457template <typename ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000458Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
459 uint64_t Value, uint64_t Size, uint8_t Binding,
460 SectionBase *Section, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000461 Symbol *S;
462 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000463 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000464 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000465 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
466 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000467 if (Cmp > 0)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000468 replaceBody<DefinedRegular>(S, File, Name, /*IsLocal=*/false, StOther, Type,
469 Value, Size, Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000470 else if (Cmp == 0)
Rafael Espindola5616adf2017-03-08 22:36:28 +0000471 reportDuplicate<ELFT>(S->body(),
472 dyn_cast_or_null<InputSectionBase>(Section), Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000473 return S;
474}
475
476template <typename ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000477void SymbolTable::addShared(SharedFile<ELFT> *File, StringRef Name,
478 const typename ELFT::Sym &Sym,
479 const typename ELFT::Verdef *Verdef) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000480 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
481 // as the visibility, which will leave the visibility in the symbol table
482 // unchanged.
483 Symbol *S;
484 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000485 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
486 /*CanOmitFromDynSym*/ true, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000487 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000488 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000489 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000490
Rafael Espindola8465d0832017-04-04 20:03:34 +0000491 SymbolBody *Body = S->body();
492 // An undefined symbol with non default visibility must be satisfied
493 // in the same DSO.
494 if (WasInserted ||
495 (isa<Undefined>(Body) && Body->getVisibility() == STV_DEFAULT)) {
Rui Ueyama4076fa12017-02-26 23:35:34 +0000496 replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(), &Sym,
497 Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000498 if (!S->isWeak())
Rui Ueyama4076fa12017-02-26 23:35:34 +0000499 File->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000500 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000501}
502
Rafael Espindola244ef982017-07-26 18:42:48 +0000503Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
504 uint8_t StOther, uint8_t Type,
505 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000506 Symbol *S;
507 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000508 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000509 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000510 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
511 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000512 if (Cmp > 0)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000513 replaceBody<DefinedRegular>(S, F, Name, /*IsLocal=*/false, StOther, Type, 0,
514 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515 else if (Cmp == 0)
516 reportDuplicate(S->body(), F);
517 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000518}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000519
Rafael Espindola244ef982017-07-26 18:42:48 +0000520SymbolBody *SymbolTable::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000521 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000522 if (It == Symtab.end())
523 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000524 SymIndex V = It->second;
525 if (V.Idx == -1)
526 return nullptr;
527 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000528}
529
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000530template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000531Symbol *SymbolTable::addLazyArchive(ArchiveFile *F,
532 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000533 Symbol *S;
534 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000535 StringRef Name = Sym.getName();
536 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000537 if (WasInserted) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000538 replaceBody<LazyArchive>(S, F, Sym, SymbolBody::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000539 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000540 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000541 if (!S->body()->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000542 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000543
Peter Collingbourne4f952702016-05-01 04:55:03 +0000544 // Weak undefined symbols should not fetch members from archives. If we were
545 // to keep old symbol we would not know that an archive member was available
546 // if a strong undefined symbol shows up afterwards in the link. If a strong
547 // undefined symbol never shows up, this lazy symbol will get to the end of
548 // the link and must be treated as the weak undefined one. We already marked
549 // this symbol as used when we added it to the symbol table, but we also need
550 // to preserve its type. FIXME: Move the Type field to Symbol.
551 if (S->isWeak()) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000552 replaceBody<LazyArchive>(S, F, Sym, S->body()->Type);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000553 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000554 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000555 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
556 if (!MBInfo.first.getBuffer().empty())
Rafael Espindola244ef982017-07-26 18:42:48 +0000557 addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000558 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000559}
560
561template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000562void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000563 Symbol *S;
564 bool WasInserted;
565 std::tie(S, WasInserted) = insert(Name);
566 if (WasInserted) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000567 replaceBody<LazyObject>(S, &Obj, Name, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000568 return;
569 }
570 if (!S->body()->isUndefined())
571 return;
572
573 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000574 if (S->isWeak())
Rafael Espindola6e93d052017-08-04 22:31:42 +0000575 replaceBody<LazyObject>(S, &Obj, Name, S->body()->Type);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000576 else if (InputFile *F = Obj.fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000577 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000578}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000579
Peter Collingbourne892d49802016-04-27 00:05:03 +0000580// Process undefined (-u) flags by loading lazy symbols named by those flags.
Rafael Espindola244ef982017-07-26 18:42:48 +0000581template <class ELFT> void SymbolTable::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000582 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000583 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama55518e72016-10-28 20:57:25 +0000584 if (InputFile *File = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000585 addFile<ELFT>(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000586}
587
Rui Ueyama93bfee52015-10-13 18:10:33 +0000588// This function takes care of the case in which shared libraries depend on
589// the user program (not the other way, which is usual). Shared libraries
590// may have undefined symbols, expecting that the user program provides
591// the definitions for them. An example is BSD's __progname symbol.
592// We need to put such symbols to the main program's .dynsym so that
593// shared libraries can find them.
594// Except this, we ignore undefined symbols in DSOs.
Rafael Espindola244ef982017-07-26 18:42:48 +0000595template <class ELFT> void SymbolTable::scanShlibUndefined() {
596 for (SharedFile<ELFT> *File : SharedFile<ELFT>::Instances) {
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000597 for (StringRef U : File->getUndefinedSymbols()) {
598 SymbolBody *Sym = find(U);
599 if (!Sym || !Sym->isDefined())
600 continue;
601 Sym->symbol()->ExportDynamic = true;
602
603 // If -dynamic-list is given, the default version is set to
604 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
605 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
606 // specified by -dynamic-list.
607 Sym->symbol()->VersionId = VER_NDX_GLOBAL;
608 }
609 }
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000610}
611
Rui Ueyama82492142016-11-15 18:41:52 +0000612// Initialize DemangledSyms with a map from demangled symbols to symbol
613// objects. Used to handle "extern C++" directive in version scripts.
614//
615// The map will contain all demangled symbols. That can be very large,
616// and in LLD we generally want to avoid do anything for each symbol.
617// Then, why are we doing this? Here's why.
618//
619// Users can use "extern C++ {}" directive to match against demangled
620// C++ symbols. For example, you can write a pattern such as
621// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
622// other than trying to match a pattern against all demangled symbols.
623// So, if "extern C++" feature is used, we need to demangle all known
624// symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000625StringMap<std::vector<SymbolBody *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000626 if (!DemangledSyms) {
627 DemangledSyms.emplace();
628 for (Symbol *Sym : SymVector) {
629 SymbolBody *B = Sym->body();
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000630 if (B->isUndefined())
631 continue;
Rui Ueyama96aff372016-12-22 05:31:52 +0000632 if (Optional<std::string> S = demangle(B->getName()))
633 (*DemangledSyms)[*S].push_back(B);
634 else
635 (*DemangledSyms)[B->getName()].push_back(B);
636 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000637 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000638 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000639}
640
Rafael Espindola244ef982017-07-26 18:42:48 +0000641std::vector<SymbolBody *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000642 if (Ver.IsExternCpp)
643 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000644 if (SymbolBody *B = find(Ver.Name))
645 if (!B->isUndefined())
646 return {B};
647 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000648}
649
Rafael Espindola244ef982017-07-26 18:42:48 +0000650std::vector<SymbolBody *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
Rafael Espindola191390a2016-12-08 16:26:20 +0000651 std::vector<SymbolBody *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000652 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000653
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000654 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000655 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000656 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000657 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000658 return Res;
659 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000660
661 for (Symbol *Sym : SymVector) {
662 SymbolBody *B = Sym->body();
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000663 if (!B->isUndefined() && M.match(B->getName()))
Rafael Espindola191390a2016-12-08 16:26:20 +0000664 Res.push_back(B);
665 }
666 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000667}
668
Rui Ueyamaea265042016-09-13 20:51:30 +0000669// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000670// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000671// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000672void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000673 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
674 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
675 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
676 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
677 for (SymbolVersion &Ver : Config->VersionScriptLocals)
678 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
679 for (SymbolVersion &Ver : Config->VersionScriptLocals)
680 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000681}
682
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000683// Set symbol versions to symbols. This function handles patterns
684// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000685void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
686 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000687 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000688 return;
689
690 // Get a list of symbols which we need to assign the version to.
Rui Ueyama86581e42016-12-10 00:34:06 +0000691 std::vector<SymbolBody *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000692 if (Syms.empty()) {
693 if (Config->NoUndefinedVersion)
694 error("version script assignment of '" + VersionName + "' to symbol '" +
695 Ver.Name + "' failed: symbol not defined");
696 return;
697 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000698
699 // Assign the version.
700 for (SymbolBody *B : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000701 // Skip symbols containing version info because symbol versions
702 // specified by symbol names take precedence over version scripts.
703 // See parseSymbolVersion().
Rui Ueyama12234f82017-07-19 21:40:26 +0000704 if (B->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000705 continue;
706
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000707 Symbol *Sym = B->symbol();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000708 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000709 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000710 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000711 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000712 }
713}
714
Rafael Espindola244ef982017-07-26 18:42:48 +0000715void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000716 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000717 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000718
719 // Exact matching takes precendence over fuzzy matching,
720 // so we set a version to a symbol only if no version has been assigned
721 // to the symbol. This behavior is compatible with GNU.
Rui Ueyama9a189872017-07-11 20:33:04 +0000722 for (SymbolBody *B : findAllByVersion(Ver))
Rui Ueyama82492142016-11-15 18:41:52 +0000723 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
724 B->symbol()->VersionId = VersionId;
725}
726
Rui Ueyamadad2b882016-09-02 22:15:08 +0000727// This function processes version scripts by updating VersionId
728// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000729void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000730 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000731 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000732
Rui Ueyamadad2b882016-09-02 22:15:08 +0000733 // Now we have version definitions, so we need to set version ids to symbols.
734 // Each version definition has a glob pattern, and all symbols that match
735 // with the pattern get that version.
736
Rui Ueyamadad2b882016-09-02 22:15:08 +0000737 // First, we assign versions to exact matching symbols,
738 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000739 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000740 for (SymbolVersion &Ver : V.Globals)
741 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000742
Rui Ueyamadad2b882016-09-02 22:15:08 +0000743 // Next, we assign versions to fuzzy matching symbols,
744 // i.e. version definitions containing glob meta-characters.
745 // Note that because the last match takes precedence over previous matches,
746 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000747 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
748 for (SymbolVersion &Ver : V.Globals)
749 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000750
751 // Symbol themselves might know their versions because symbols
752 // can contain versions in the form of <name>@<version>.
753 // Let them parse and update their names to exclude version suffix.
754 for (Symbol *Sym : SymVector)
755 Sym->body()->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000756}
757
Rafael Espindola244ef982017-07-26 18:42:48 +0000758template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
759template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
760template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
761template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
762
763template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
764template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
765template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
766template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
767
Rui Ueyama3e96f112017-07-26 21:37:11 +0000768template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, bool, uint8_t,
769 uint8_t, uint8_t, bool,
770 InputFile *);
771template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, bool, uint8_t,
772 uint8_t, uint8_t, bool,
773 InputFile *);
774template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, bool, uint8_t,
775 uint8_t, uint8_t, bool,
776 InputFile *);
777template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, bool, uint8_t,
778 uint8_t, uint8_t, bool,
779 InputFile *);
780
Rafael Espindola244ef982017-07-26 18:42:48 +0000781template void SymbolTable::addSymbolAlias<ELF32LE>(StringRef, StringRef);
782template void SymbolTable::addSymbolAlias<ELF32BE>(StringRef, StringRef);
783template void SymbolTable::addSymbolAlias<ELF64LE>(StringRef, StringRef);
784template void SymbolTable::addSymbolAlias<ELF64BE>(StringRef, StringRef);
785
786template void SymbolTable::addCombinedLTOObject<ELF32LE>();
787template void SymbolTable::addCombinedLTOObject<ELF32BE>();
788template void SymbolTable::addCombinedLTOObject<ELF64LE>();
789template void SymbolTable::addCombinedLTOObject<ELF64BE>();
790
791template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t,
792 uint64_t, uint64_t, uint8_t,
793 SectionBase *, InputFile *);
794template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t,
795 uint64_t, uint64_t, uint8_t,
796 SectionBase *, InputFile *);
797template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t,
798 uint64_t, uint64_t, uint8_t,
799 SectionBase *, InputFile *);
800template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t,
801 uint64_t, uint64_t, uint8_t,
802 SectionBase *, InputFile *);
803
804template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t,
805 uint8_t);
806template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t,
807 uint8_t);
808template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t,
809 uint8_t);
810template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t,
811 uint8_t);
812
813template DefinedRegular *SymbolTable::addIgnored<ELF32LE>(StringRef, uint8_t);
814template DefinedRegular *SymbolTable::addIgnored<ELF32BE>(StringRef, uint8_t);
815template DefinedRegular *SymbolTable::addIgnored<ELF64LE>(StringRef, uint8_t);
816template DefinedRegular *SymbolTable::addIgnored<ELF64BE>(StringRef, uint8_t);
817
818template Symbol *
819SymbolTable::addLazyArchive<ELF32LE>(ArchiveFile *,
820 const object::Archive::Symbol);
821template Symbol *
822SymbolTable::addLazyArchive<ELF32BE>(ArchiveFile *,
823 const object::Archive::Symbol);
824template Symbol *
825SymbolTable::addLazyArchive<ELF64LE>(ArchiveFile *,
826 const object::Archive::Symbol);
827template Symbol *
828SymbolTable::addLazyArchive<ELF64BE>(ArchiveFile *,
829 const object::Archive::Symbol);
830
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000831template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
832template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
833template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
834template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000835
836template void SymbolTable::addShared<ELF32LE>(SharedFile<ELF32LE> *, StringRef,
837 const typename ELF32LE::Sym &,
838 const typename ELF32LE::Verdef *);
839template void SymbolTable::addShared<ELF32BE>(SharedFile<ELF32BE> *, StringRef,
840 const typename ELF32BE::Sym &,
841 const typename ELF32BE::Verdef *);
842template void SymbolTable::addShared<ELF64LE>(SharedFile<ELF64LE> *, StringRef,
843 const typename ELF64LE::Sym &,
844 const typename ELF64LE::Verdef *);
845template void SymbolTable::addShared<ELF64BE>(SharedFile<ELF64BE> *, StringRef,
846 const typename ELF64BE::Sym &,
847 const typename ELF64BE::Verdef *);
848
849template void SymbolTable::scanUndefinedFlags<ELF32LE>();
850template void SymbolTable::scanUndefinedFlags<ELF32BE>();
851template void SymbolTable::scanUndefinedFlags<ELF64LE>();
852template void SymbolTable::scanUndefinedFlags<ELF64BE>();
853
854template void SymbolTable::scanShlibUndefined<ELF32LE>();
855template void SymbolTable::scanShlibUndefined<ELF32BE>();
856template void SymbolTable::scanShlibUndefined<ELF64LE>();
857template void SymbolTable::scanShlibUndefined<ELF64BE>();