blob: 8a4b148c8b9564a75ed30c02506fb3344f397361 [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"
Davide Italiano8e1131d2016-06-29 02:46:51 +000019#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000020#include "Memory.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000021#include "Symbols.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000022#include "lld/Common/ErrorHandler.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 Ueyama227cb6b2017-10-15 21:43:09 +000034static InputFile *getFirstElf() {
35 if (!ObjectFiles.empty())
36 return ObjectFiles[0];
37 if (!SharedFiles.empty())
38 return SharedFiles[0];
39 return nullptr;
40}
41
Rui Ueyamac9559d92016-01-05 20:47:37 +000042// All input object files must be for the same architecture
43// (e.g. it does not make sense to link x86 object files with
44// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000045template <class ELFT> static bool isCompatible(InputFile *F) {
46 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000047 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000048
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000049 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
50 if (Config->EMachine != EM_MIPS)
51 return true;
52 if (isMipsN32Abi(F) == Config->MipsN32Abi)
53 return true;
54 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000055
56 if (!Config->Emulation.empty())
57 error(toString(F) + " is incompatible with " + Config->Emulation);
58 else
Rui Ueyama227cb6b2017-10-15 21:43:09 +000059 error(toString(F) + " is incompatible with " + toString(getFirstElf()));
Rui Ueyama16ba6692016-01-29 19:41:13 +000060 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000061}
62
Rui Ueyamac9559d92016-01-05 20:47:37 +000063// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000064template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000065 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000066 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000067
Michael J. Spencera9424f32016-09-09 22:08:04 +000068 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000069 if (auto *F = dyn_cast<BinaryFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +000070 BinaryFiles.push_back(F);
Rafael Espindola093abab2016-10-27 17:45:40 +000071 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000072 return;
73 }
74
Rui Ueyama89575742015-12-16 22:59:13 +000075 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000076 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000077 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000078 return;
79 }
Rui Ueyama3d451792015-10-12 18:03:21 +000080
George Rimar2a78fce2016-04-13 18:07:57 +000081 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000082 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000083 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000084 return;
85 }
86
87 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000088 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000089
Rui Ueyama89575742015-12-16 22:59:13 +000090 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000091 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000092 // DSOs are uniquified not by filename but by soname.
93 F->parseSoName();
Bob Haarmanb8a59c82017-10-25 22:28:38 +000094 if (errorCount() || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000095 return;
George Rimar696a7f92017-09-19 09:20:54 +000096 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000097 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000098 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000099 }
Rui Ueyama89575742015-12-16 22:59:13 +0000100
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000101 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000102 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000103 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000104 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000105 return;
106 }
107
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000108 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000109 ObjectFiles.push_back(File);
110 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111}
112
Rui Ueyama42554752016-04-23 00:26:32 +0000113// This function is where all the optimizations of link-time
114// optimization happens. When LTO is in use, some input files are
115// not in native object file format but in the LLVM bitcode format.
116// This function compiles bitcode files into a few big native files
117// using LLVM functions and replaces bitcode symbols with the results.
118// Because all bitcode files that consist of a program are passed
119// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000120template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000121 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000122 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000123
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000124 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000125 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000126 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000127 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000128
Davide Italiano3bfa0812016-11-26 05:37:04 +0000129 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000130 DenseSet<CachedHashStringRef> DummyGroups;
George Rimar696a7f92017-09-19 09:20:54 +0000131 cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
132 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000133 }
134}
135
Rafael Espindola0e604f92015-09-25 18:56:53 +0000136template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000137DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
138 uint8_t Binding) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000139 Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding,
140 nullptr, nullptr);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000141 return cast<DefinedRegular>(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000142}
143
Rui Ueyama69c778c2016-07-17 17:50:09 +0000144// Set a flag for --trace-symbol so that we can print out a log message
145// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000146void SymbolTable::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000147 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000148}
149
Rui Ueyamadeb15402016-01-07 17:20:07 +0000150// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
151// Used to implement --wrap.
Rafael Espindola244ef982017-07-26 18:42:48 +0000152template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000153 Symbol *Sym = find(Name);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000154 if (!Sym)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000155 return;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000156 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
157 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000158
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000159 defsym(Real, Sym);
160 defsym(Sym, Wrap);
Rafael Espindola46935082017-10-06 20:09:34 +0000161
162 WrapSymbols.push_back({Wrap, Real});
Rui Ueyamadeb15402016-01-07 17:20:07 +0000163}
164
Rui Ueyama45b81402017-11-04 22:32:56 +0000165// Apply symbol renames created by -wrap. The renames are created
166// before LTO in addSymbolWrap() to have a chance to inform LTO (if
167// LTO is running) not to include these symbols in IPO. Now that the
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000168// symbols are finalized, we can perform the replacement.
Rafael Espindola244ef982017-07-26 18:42:48 +0000169void SymbolTable::applySymbolRenames() {
Rafael Espindola46935082017-10-06 20:09:34 +0000170 // This function rotates 3 symbols:
171 //
172 // __real_foo becomes foo
173 // foo becomes __wrap_foo
174 // __wrap_foo becomes __real_foo
175 //
176 // The last part is special in that we don't want to change what references to
177 // __wrap_foo point to, we just want have __real_foo in the symbol table.
178
179 // First make a copy of __real_foo
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000180 std::vector<SymbolUnion> Origs;
Rafael Espindola46935082017-10-06 20:09:34 +0000181 for (const auto &P : WrapSymbols)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000182 Origs.emplace_back(*(SymbolUnion *)P.second);
Rafael Espindola46935082017-10-06 20:09:34 +0000183
184 // Replace __real_foo with foo and foo with __wrap_foo
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000185 for (SymbolRenaming &S : Defsyms) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000186 S.Dst->copyFrom(S.Src);
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000187 S.Dst->File = S.Src->File;
188 S.Dst->Binding = S.Binding;
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000189 }
Rafael Espindola46935082017-10-06 20:09:34 +0000190
191 // Hide one of the copies of __wrap_foo, create a new symbol and copy
192 // __real_foo into it.
193 for (unsigned I = 0, N = WrapSymbols.size(); I < N; ++I) {
194 // We now have two copies of __wrap_foo. Drop one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000195 Symbol *Wrap = WrapSymbols[I].first;
Rafael Espindola46935082017-10-06 20:09:34 +0000196 Wrap->IsUsedInRegularObj = false;
197
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000198 auto *Real = (Symbol *)&Origs[I];
Rafael Espindola46935082017-10-06 20:09:34 +0000199 // If __real_foo was undefined, we don't want it in the symbol table.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000200 if (!Real->isInCurrentOutput())
Rafael Espindola46935082017-10-06 20:09:34 +0000201 continue;
202
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000203 auto *NewSym = (Symbol *)make<SymbolUnion>();
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000204 memcpy(NewSym, Real, sizeof(SymbolUnion));
Rafael Espindola46935082017-10-06 20:09:34 +0000205 SymVector.push_back(NewSym);
206 }
George Rimar9703ad22017-04-26 10:40:02 +0000207}
208
Peter Collingbournedadcc172016-04-22 18:42:48 +0000209static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
210 if (VA == STV_DEFAULT)
211 return VB;
212 if (VB == STV_DEFAULT)
213 return VA;
214 return std::min(VA, VB);
215}
216
Rui Ueyamab4de5952016-01-08 22:01:33 +0000217// Find an existing symbol or create and insert a new one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000218std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000219 // <name>@@<version> means the symbol is the default version. In that
220 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000221 //
222 // Since this is a hot path, the following string search code is
223 // optimized for speed. StringRef::find(char) is much faster than
224 // StringRef::find(StringRef).
225 size_t Pos = Name.find('@');
226 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000227 Name = Name.take_front(Pos);
228
Justin Lebar3c11e932016-10-18 17:50:36 +0000229 auto P = Symtab.insert(
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000230 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000231 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000232 bool IsNew = P.second;
233
Rui Ueyamae3357902016-07-18 01:35:00 +0000234 if (V.Idx == -1) {
235 IsNew = true;
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000236 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000237 }
238
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000239 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000240 if (IsNew) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000241 Sym = (Symbol *)make<SymbolUnion>();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000242 Sym->InVersionScript = false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000243 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000244 Sym->Visibility = STV_DEFAULT;
245 Sym->IsUsedInRegularObj = false;
246 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000247 Sym->CanInline = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000248 Sym->Traced = V.Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000249 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000250 SymVector.push_back(Sym);
251 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000252 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000253 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000254 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000255}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000256
Peter Collingbourne4f952702016-05-01 04:55:03 +0000257// Find an existing symbol or create and insert a new one, then apply the given
258// attributes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000259std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
260 uint8_t Visibility,
261 bool CanOmitFromDynSym,
262 InputFile *File) {
263 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000264 bool WasInserted;
265 std::tie(S, WasInserted) = insert(Name);
266
267 // Merge in the new symbol's visibility.
268 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000269
Peter Collingbourne4f952702016-05-01 04:55:03 +0000270 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
271 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000272
Rui Ueyamaac647252017-10-29 16:46:39 +0000273 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000274 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000275
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000276 if (!WasInserted && S->Type != Symbol::UnknownType &&
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000277 ((Type == STT_TLS) != S->isTls())) {
278 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
279 toString(S->File) + "\n>>> defined in " + toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000280 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000281
282 return {S, WasInserted};
283}
284
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000285template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindola244ef982017-07-26 18:42:48 +0000286 return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
287 /*Type*/ 0,
288 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000289}
290
Rui Ueyamae50e8072016-12-22 05:11:12 +0000291static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
292
Peter Collingbourne4f952702016-05-01 04:55:03 +0000293template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000294Symbol *SymbolTable::addUndefined(StringRef Name, bool IsLocal, uint8_t Binding,
295 uint8_t StOther, uint8_t Type,
296 bool CanOmitFromDynSym, InputFile *File) {
297 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000298 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000299 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000300 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000301 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
302 // An undefined symbol with non default visibility must be satisfied
303 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000304 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000305 S->Binding = Binding;
Rui Ueyamaf483da02017-11-03 22:48:47 +0000306 replaceSymbol<Undefined>(S, File, Name, IsLocal, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000307 return S;
308 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000309 if (Binding != STB_WEAK) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000310 if (!S->isInCurrentOutput())
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000311 S->Binding = Binding;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000312 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rafael Espindola6e93d052017-08-04 22:31:42 +0000313 SS->getFile<ELFT>()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000314 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000315 if (auto *L = dyn_cast<Lazy>(S)) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000316 // An undefined weak will not fetch archive members. See comment on Lazy in
317 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000318 if (S->isWeak())
319 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000320 else if (InputFile *F = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000321 addFile<ELFT>(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000322 }
323 return S;
324}
325
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000326// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
327// foo@@VER. We want to effectively ignore foo, so give precedence to
328// foo@@VER.
329// FIXME: If users can transition to using
330// .symver foo,foo@@@VER
331// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000332static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000333 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000334 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000335 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000336 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000337 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000338 return -1;
339 return 0;
340}
341
Peter Collingbourne4f952702016-05-01 04:55:03 +0000342// We have a new defined symbol with the specified binding. Return 1 if the new
343// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
344// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000345static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000346 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000347 if (WasInserted)
348 return 1;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000349 if (!S->isInCurrentOutput())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000350 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000351 if (int R = compareVersion(S, Name))
352 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000353 if (Binding == STB_WEAK)
354 return -1;
355 if (S->isWeak())
356 return 1;
357 return 0;
358}
359
360// We have a new non-common defined symbol with the specified binding. Return 1
361// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
362// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000363static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
364 bool IsAbsolute, uint64_t Value,
365 StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000366 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000367 if (Cmp > 0)
368 S->Binding = Binding;
369 return Cmp;
370 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000371 if (isa<DefinedCommon>(S)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000372 // Non-common symbols take precedence over common symbols.
373 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000374 warn("common " + S->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000375 return 1;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000376 }
377 if (auto *R = dyn_cast<DefinedRegular>(S)) {
Rafael Espindola858c0922016-12-02 02:58:21 +0000378 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
379 R->Value == Value)
380 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000381 }
382 return 0;
383}
384
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000385Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
386 uint8_t Binding, uint8_t StOther, uint8_t Type,
387 InputFile *File) {
388 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000389 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000390 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
391 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000392 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000393 if (Cmp > 0) {
394 S->Binding = Binding;
Rui Ueyamaf483da02017-11-03 22:48:47 +0000395 replaceSymbol<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000396 } else if (Cmp == 0) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000397 auto *C = dyn_cast<DefinedCommon>(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000398 if (!C) {
399 // Non-common symbols take precedence over common symbols.
400 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000401 warn("common " + S->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000402 return S;
403 }
404
405 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000406 warn("multiple common of " + S->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000407
Rafael Espindola8db87292016-08-31 13:42:08 +0000408 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
409 if (Size > C->Size)
Rui Ueyamaf483da02017-11-03 22:48:47 +0000410 replaceSymbol<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000411 }
412 return S;
413}
414
Rui Ueyama810ce102017-03-31 23:40:21 +0000415static void warnOrError(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000416 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000417 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000418 else
419 error(Msg);
420}
421
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000422static void reportDuplicate(Symbol *Sym, InputFile *NewFile) {
George Rimar67c60722017-07-18 11:55:35 +0000423 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
Rafael Espindola6e93d052017-08-04 22:31:42 +0000424 toString(Sym->getFile()) + "\n>>> defined in " +
425 toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000426}
427
428template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000429static void reportDuplicate(Symbol *Sym, InputSectionBase *ErrSec,
Eugene Leviant825e5382016-11-08 16:26:32 +0000430 typename ELFT::uint ErrOffset) {
Rui Ueyama810ce102017-03-31 23:40:21 +0000431 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym);
Eugene Leviant825e5382016-11-08 16:26:32 +0000432 if (!D || !D->Section || !ErrSec) {
Rafael Espindolacb83c8c2017-07-25 23:23:40 +0000433 reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr);
Eugene Leviant825e5382016-11-08 16:26:32 +0000434 return;
435 }
436
Rui Ueyama810ce102017-03-31 23:40:21 +0000437 // Construct and print an error message in the form of:
438 //
439 // ld.lld: error: duplicate symbol: foo
440 // >>> defined at bar.c:30
441 // >>> bar.o (/home/alice/src/bar.o)
442 // >>> defined at baz.c:563
443 // >>> baz.o in archive libbaz.a
444 auto *Sec1 = cast<InputSectionBase>(D->Section);
George Rimar82f0c422017-11-01 07:42:38 +0000445 std::string Src1 = Sec1->getSrcMsg<ELFT>(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000446 std::string Obj1 = Sec1->getObjMsg(D->Value);
George Rimar82f0c422017-11-01 07:42:38 +0000447 std::string Src2 = ErrSec->getSrcMsg<ELFT>(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000448 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000449
Rui Ueyama810ce102017-03-31 23:40:21 +0000450 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
451 if (!Src1.empty())
452 Msg += Src1 + "\n>>> ";
453 Msg += Obj1 + "\n>>> defined at ";
454 if (!Src2.empty())
455 Msg += Src2 + "\n>>> ";
456 Msg += Obj2;
457 warnOrError(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000458}
459
Peter Collingbourne4f952702016-05-01 04:55:03 +0000460template <typename ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000461Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
462 uint64_t Value, uint64_t Size, uint8_t Binding,
463 SectionBase *Section, InputFile *File) {
464 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000465 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000466 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000467 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000468 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
469 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000470 if (Cmp > 0)
Rui Ueyamaf483da02017-11-03 22:48:47 +0000471 replaceSymbol<DefinedRegular>(S, File, Name, /*IsLocal=*/false, StOther,
472 Type, Value, Size, Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000473 else if (Cmp == 0)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000474 reportDuplicate<ELFT>(S, dyn_cast_or_null<InputSectionBase>(Section),
475 Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000476 return S;
477}
478
479template <typename ELFT>
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000480void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> *File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000481 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000482 const typename ELFT::Verdef *Verdef) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000483 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
484 // as the visibility, which will leave the visibility in the symbol table
485 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000486 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000487 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000488 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
489 /*CanOmitFromDynSym*/ true, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000490 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000491 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000492 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000493
Rafael Espindola8465d0832017-04-04 20:03:34 +0000494 // An undefined symbol with non default visibility must be satisfied
495 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000496 if (WasInserted || ((S->isUndefined() || S->isLazy()) &&
497 S->getVisibility() == STV_DEFAULT)) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000498 replaceSymbol<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(),
499 Sym.st_value, Sym.st_size, Alignment, 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
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000505Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
506 uint8_t StOther, uint8_t Type,
507 bool CanOmitFromDynSym, BitcodeFile *F) {
508 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000509 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000510 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000511 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000512 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
513 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000514 if (Cmp > 0)
Rui Ueyamaf483da02017-11-03 22:48:47 +0000515 replaceSymbol<DefinedRegular>(S, F, Name, /*IsLocal=*/false, StOther, Type,
516 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000517 else if (Cmp == 0)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000518 reportDuplicate(S, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000519 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000520}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000521
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000522Symbol *SymbolTable::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000523 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000524 if (It == Symtab.end())
525 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000526 SymIndex V = It->second;
527 if (V.Idx == -1)
528 return nullptr;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000529 return SymVector[V.Idx];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000530}
531
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000532void SymbolTable::defsym(Symbol *Dst, Symbol *Src) {
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000533 // We want to tell LTO not to inline Dst symbol because LTO doesn't
534 // know the final symbol contents after renaming.
535 Dst->CanInline = false;
Rafael Espindolac29b24d2017-10-05 00:35:47 +0000536
537 // Tell LTO not to eliminate this symbol.
538 Src->IsUsedInRegularObj = true;
539
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000540 Defsyms.push_back({Dst, Src, Dst->Binding});
541}
542
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000543template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000544Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F,
545 const object::Archive::Symbol Sym) {
546 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000547 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000548 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000549 if (WasInserted) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000550 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000551 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000552 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000553 if (!S->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000554 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000555
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000556 // An undefined weak will not fetch archive members. See comment on Lazy in
557 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000558 if (S->isWeak()) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000559 replaceSymbol<LazyArchive>(S, F, Sym, S->Type);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000560 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000561 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000562 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
563 if (!MBInfo.first.getBuffer().empty())
Rafael Espindola244ef982017-07-26 18:42:48 +0000564 addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000565 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000566}
567
568template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000569void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000570 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000571 bool WasInserted;
572 std::tie(S, WasInserted) = insert(Name);
573 if (WasInserted) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000574 replaceSymbol<LazyObject>(S, &Obj, Name, Symbol::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000575 return;
576 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000577 if (!S->isUndefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000578 return;
579
580 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000581 if (S->isWeak())
Rui Ueyamaf483da02017-11-03 22:48:47 +0000582 replaceSymbol<LazyObject>(S, &Obj, Name, S->Type);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000583 else if (InputFile *F = Obj.fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000584 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000585}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000586
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000587// If we already saw this symbol, force loading its file.
588template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000589 if (Symbol *B = find(Name)) {
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000590 // Mark the symbol not to be eliminated by LTO
591 // even if it is a bitcode symbol.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000592 B->IsUsedInRegularObj = true;
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000593 if (auto *L = dyn_cast_or_null<Lazy>(B))
594 if (InputFile *File = L->fetch())
595 addFile<ELFT>(File);
596 }
597}
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.
Rafael Espindola244ef982017-07-26 18:42:48 +0000606template <class ELFT> void SymbolTable::scanShlibUndefined() {
George Rimar696a7f92017-09-19 09:20:54 +0000607 for (InputFile *F : SharedFiles) {
608 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000609 Symbol *Sym = find(U);
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000610 if (!Sym || !Sym->isDefined())
611 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000612 Sym->ExportDynamic = true;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000613
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.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000618 Sym->VersionId = VER_NDX_GLOBAL;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000619 }
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.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000636StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000637 if (!DemangledSyms) {
638 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000639 for (Symbol *Sym : SymVector) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000640 if (!Sym->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000641 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000642 if (Optional<std::string> S = demangle(Sym->getName()))
643 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000644 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000645 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000646 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000647 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000648 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000649}
650
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000651std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000652 if (Ver.IsExternCpp)
653 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000654 if (Symbol *B = find(Ver.Name))
Rui Ueyamabda337a2017-10-27 22:54:16 +0000655 if (B->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000656 return {B};
657 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000658}
659
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000660std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
661 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000662 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000663
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000664 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000665 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000666 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000667 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000668 return Res;
669 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000670
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000671 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000672 if (Sym->isInCurrentOutput() && M.match(Sym->getName()))
673 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000674 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000675}
676
Rui Ueyamaea265042016-09-13 20:51:30 +0000677// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000678// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000679// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000680void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000681 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
682 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
683 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
684 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
685 for (SymbolVersion &Ver : Config->VersionScriptLocals)
686 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
687 for (SymbolVersion &Ver : Config->VersionScriptLocals)
688 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000689}
690
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000691// Handles -dynamic-list.
692void SymbolTable::handleDynamicList() {
693 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000694 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000695 if (Ver.HasWildcard)
696 Syms = findByVersion(Ver);
697 else
698 Syms = findAllByVersion(Ver);
699
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000700 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000701 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000702 B->ExportDynamic = true;
703 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000704 B->IsPreemptible = true;
705 }
706 }
707}
708
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000709// Set symbol versions to symbols. This function handles patterns
710// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000711void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
712 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000713 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000714 return;
715
716 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000717 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000718 if (Syms.empty()) {
719 if (Config->NoUndefinedVersion)
720 error("version script assignment of '" + VersionName + "' to symbol '" +
721 Ver.Name + "' failed: symbol not defined");
722 return;
723 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000724
725 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000726 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000727 // Skip symbols containing version info because symbol versions
728 // specified by symbol names take precedence over version scripts.
729 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000730 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000731 continue;
732
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000733 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000734 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000735 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000736 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000737 }
738}
739
Rafael Espindola244ef982017-07-26 18:42:48 +0000740void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000741 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000742 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000743
744 // Exact matching takes precendence over fuzzy matching,
745 // so we set a version to a symbol only if no version has been assigned
746 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000747 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000748 if (B->VersionId == Config->DefaultSymbolVersion)
749 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000750}
751
Rui Ueyamadad2b882016-09-02 22:15:08 +0000752// This function processes version scripts by updating VersionId
753// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000754void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000755 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000756 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000757 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000758
Rui Ueyamadad2b882016-09-02 22:15:08 +0000759 // Now we have version definitions, so we need to set version ids to symbols.
760 // Each version definition has a glob pattern, and all symbols that match
761 // with the pattern get that version.
762
Rui Ueyamadad2b882016-09-02 22:15:08 +0000763 // First, we assign versions to exact matching symbols,
764 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000765 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000766 for (SymbolVersion &Ver : V.Globals)
767 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000768
Rui Ueyamadad2b882016-09-02 22:15:08 +0000769 // Next, we assign versions to fuzzy matching symbols,
770 // i.e. version definitions containing glob meta-characters.
771 // Note that because the last match takes precedence over previous matches,
772 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000773 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
774 for (SymbolVersion &Ver : V.Globals)
775 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000776
777 // Symbol themselves might know their versions because symbols
778 // can contain versions in the form of <name>@<version>.
779 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000780 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000781 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000782}
783
Rafael Espindola244ef982017-07-26 18:42:48 +0000784template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
785template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
786template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
787template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
788
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000789template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
790template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
791template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
792template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000793
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000794template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, bool, uint8_t,
795 uint8_t, uint8_t, bool,
796 InputFile *);
797template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, bool, uint8_t,
798 uint8_t, uint8_t, bool,
799 InputFile *);
800template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, bool, uint8_t,
801 uint8_t, uint8_t, bool,
802 InputFile *);
803template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, bool, uint8_t,
804 uint8_t, uint8_t, bool,
805 InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000806
Rafael Espindola244ef982017-07-26 18:42:48 +0000807template void SymbolTable::addCombinedLTOObject<ELF32LE>();
808template void SymbolTable::addCombinedLTOObject<ELF32BE>();
809template void SymbolTable::addCombinedLTOObject<ELF64LE>();
810template void SymbolTable::addCombinedLTOObject<ELF64BE>();
811
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000812template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t,
813 uint64_t, uint64_t, uint8_t,
814 SectionBase *, InputFile *);
815template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t,
816 uint64_t, uint64_t, uint8_t,
817 SectionBase *, InputFile *);
818template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t,
819 uint64_t, uint64_t, uint8_t,
820 SectionBase *, InputFile *);
821template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t,
822 uint64_t, uint64_t, uint8_t,
823 SectionBase *, InputFile *);
Rafael Espindola244ef982017-07-26 18:42:48 +0000824
825template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t,
826 uint8_t);
827template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t,
828 uint8_t);
829template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t,
830 uint8_t);
831template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t,
832 uint8_t);
833
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000834template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000835SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000836 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000837template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000838SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000839 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000840template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000841SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000842 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000843template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000844SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000845 const object::Archive::Symbol);
846
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000847template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
848template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
849template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
850template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000851
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000852template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000853 const typename ELF32LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000854 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000855 const typename ELF32LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000856template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000857 const typename ELF32BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000858 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000859 const typename ELF32BE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000860template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000861 const typename ELF64LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000862 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000863 const typename ELF64LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000864template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000865 const typename ELF64BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000866 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000867 const typename ELF64BE::Verdef *);
868
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000869template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
870template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
871template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
872template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
873
Rafael Espindola244ef982017-07-26 18:42:48 +0000874template void SymbolTable::scanShlibUndefined<ELF32LE>();
875template void SymbolTable::scanShlibUndefined<ELF32BE>();
876template void SymbolTable::scanShlibUndefined<ELF64LE>();
877template void SymbolTable::scanShlibUndefined<ELF64BE>();