blob: 6b61213896f5a256b9cda3fdf83a86947ce0662d [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "Symbols.h"
Peter Collingbourne6c55a702017-11-06 04:33:58 +000021#include "SyntheticSections.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000022#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000023#include "lld/Common/Memory.h"
Rui Ueyama53fe4692017-11-28 02:15:26 +000024#include "lld/Common/Strings.h"
Rui Ueyamacd236a92016-11-17 19:57:43 +000025#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000028using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000029using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
31using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000032using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000033
Rafael Espindola244ef982017-07-26 18:42:48 +000034SymbolTable *elf::Symtab;
35
Rui Ueyama227cb6b2017-10-15 21:43:09 +000036static InputFile *getFirstElf() {
37 if (!ObjectFiles.empty())
38 return ObjectFiles[0];
39 if (!SharedFiles.empty())
40 return SharedFiles[0];
41 return nullptr;
42}
43
Rui Ueyamac9559d92016-01-05 20:47:37 +000044// All input object files must be for the same architecture
45// (e.g. it does not make sense to link x86 object files with
46// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000047template <class ELFT> static bool isCompatible(InputFile *F) {
48 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000049 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000050
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000051 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
52 if (Config->EMachine != EM_MIPS)
53 return true;
54 if (isMipsN32Abi(F) == Config->MipsN32Abi)
55 return true;
56 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000057
58 if (!Config->Emulation.empty())
59 error(toString(F) + " is incompatible with " + Config->Emulation);
60 else
Rui Ueyama227cb6b2017-10-15 21:43:09 +000061 error(toString(F) + " is incompatible with " + toString(getFirstElf()));
Rui Ueyama16ba6692016-01-29 19:41:13 +000062 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000063}
64
Rui Ueyamac9559d92016-01-05 20:47:37 +000065// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000066template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000067 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000068 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000069
Michael J. Spencera9424f32016-09-09 22:08:04 +000070 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000071 if (auto *F = dyn_cast<BinaryFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +000072 BinaryFiles.push_back(F);
Rafael Espindola093abab2016-10-27 17:45:40 +000073 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000074 return;
75 }
76
Rui Ueyama89575742015-12-16 22:59:13 +000077 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000078 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000079 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000080 return;
81 }
Rui Ueyama3d451792015-10-12 18:03:21 +000082
George Rimar2a78fce2016-04-13 18:07:57 +000083 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000084 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000085 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000086 return;
87 }
88
89 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000090 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000091
Rui Ueyama89575742015-12-16 22:59:13 +000092 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000093 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000094 // DSOs are uniquified not by filename but by soname.
95 F->parseSoName();
Bob Haarmanb8a59c82017-10-25 22:28:38 +000096 if (errorCount() || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000097 return;
George Rimar696a7f92017-09-19 09:20:54 +000098 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000099 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +0000100 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000101 }
Rui Ueyama89575742015-12-16 22:59:13 +0000102
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000103 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000104 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000105 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000106 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000107 return;
108 }
109
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000110 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000111 ObjectFiles.push_back(File);
112 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000113}
114
Rui Ueyama42554752016-04-23 00:26:32 +0000115// This function is where all the optimizations of link-time
116// optimization happens. When LTO is in use, some input files are
117// not in native object file format but in the LLVM bitcode format.
118// This function compiles bitcode files into a few big native files
119// using LLVM functions and replaces bitcode symbols with the results.
120// Because all bitcode files that consist of a program are passed
121// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000122template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000123 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000124 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000125
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000126 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000127 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000128 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000129 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000130
Davide Italiano3bfa0812016-11-26 05:37:04 +0000131 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000132 DenseSet<CachedHashStringRef> DummyGroups;
George Rimar696a7f92017-09-19 09:20:54 +0000133 cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
134 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000135 }
136}
137
Rafael Espindola0e604f92015-09-25 18:56:53 +0000138template <class ELFT>
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000139Defined *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
140 uint8_t Binding) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000141 Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding,
142 nullptr, nullptr);
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000143 return cast<Defined>(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000144}
145
Rui Ueyama69c778c2016-07-17 17:50:09 +0000146// Set a flag for --trace-symbol so that we can print out a log message
147// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000148void SymbolTable::trace(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000149 SymMap.insert({CachedHashStringRef(Name), -1});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000150}
151
Rui Ueyamadeb15402016-01-07 17:20:07 +0000152// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
153// Used to implement --wrap.
Rafael Espindola244ef982017-07-26 18:42:48 +0000154template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000155 Symbol *Sym = find(Name);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000156 if (!Sym)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000157 return;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000158 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
159 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rafael Espindola99f9e132017-11-11 01:59:47 +0000160 WrappedSymbols.push_back({Sym, Real, Wrap});
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000161
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000162 // We want to tell LTO not to inline symbols to be overwritten
163 // because LTO doesn't know the final symbol contents after renaming.
164 Real->CanInline = false;
165 Sym->CanInline = false;
Rafael Espindola46935082017-10-06 20:09:34 +0000166
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000167 // Tell LTO not to eliminate these symbols.
168 Sym->IsUsedInRegularObj = true;
169 Wrap->IsUsedInRegularObj = true;
Rui Ueyamadeb15402016-01-07 17:20:07 +0000170}
171
Rui Ueyama45b81402017-11-04 22:32:56 +0000172// Apply symbol renames created by -wrap. The renames are created
173// before LTO in addSymbolWrap() to have a chance to inform LTO (if
174// LTO is running) not to include these symbols in IPO. Now that the
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000175// symbols are finalized, we can perform the replacement.
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000176void SymbolTable::applySymbolWrap() {
Rafael Espindola46935082017-10-06 20:09:34 +0000177 // This function rotates 3 symbols:
178 //
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000179 // __real_sym becomes sym
180 // sym becomes __wrap_sym
181 // __wrap_sym becomes __real_sym
Rafael Espindola46935082017-10-06 20:09:34 +0000182 //
183 // The last part is special in that we don't want to change what references to
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000184 // __wrap_sym point to, we just want have __real_sym in the symbol table.
Rafael Espindola46935082017-10-06 20:09:34 +0000185
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000186 for (WrappedSymbol &W : WrappedSymbols) {
187 // First, make a copy of __real_sym.
188 Symbol *Real = nullptr;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000189 if (W.Real->isDefined()) {
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000190 Real = (Symbol *)make<SymbolUnion>();
191 memcpy(Real, W.Real, sizeof(SymbolUnion));
192 }
Rafael Espindola46935082017-10-06 20:09:34 +0000193
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000194 // Replace __real_sym with sym and sym with __wrap_sym.
195 W.Real->copyFrom(W.Sym);
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000196 W.Sym->copyFrom(W.Wrap);
Rafael Espindola46935082017-10-06 20:09:34 +0000197
Rafael Espindola5c226612017-11-11 00:53:52 +0000198 // We now have two copies of __wrap_sym. Drop one.
199 W.Wrap->IsUsedInRegularObj = false;
200
201 if (Real)
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000202 SymVector.push_back(Real);
Rafael Espindola46935082017-10-06 20:09:34 +0000203 }
George Rimar9703ad22017-04-26 10:40:02 +0000204}
205
Peter Collingbournedadcc172016-04-22 18:42:48 +0000206static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
207 if (VA == STV_DEFAULT)
208 return VB;
209 if (VB == STV_DEFAULT)
210 return VA;
211 return std::min(VA, VB);
212}
213
Rui Ueyamab4de5952016-01-08 22:01:33 +0000214// Find an existing symbol or create and insert a new one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000215std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000216 // <name>@@<version> means the symbol is the default version. In that
217 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000218 //
219 // Since this is a hot path, the following string search code is
220 // optimized for speed. StringRef::find(char) is much faster than
221 // StringRef::find(StringRef).
222 size_t Pos = Name.find('@');
223 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000224 Name = Name.take_front(Pos);
225
Sam Clegga80d94d2017-11-27 23:16:06 +0000226 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000227 int &SymIndex = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000228 bool IsNew = P.second;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000229 bool Traced = false;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000230
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000231 if (SymIndex == -1) {
232 SymIndex = SymVector.size();
233 IsNew = Traced = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000234 }
235
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000236 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000237 if (IsNew) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000238 Sym = (Symbol *)make<SymbolUnion>();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000239 Sym->InVersionScript = false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000240 Sym->Visibility = STV_DEFAULT;
241 Sym->IsUsedInRegularObj = false;
242 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000243 Sym->CanInline = true;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000244 Sym->Traced = Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000245 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000246 SymVector.push_back(Sym);
247 } else {
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000248 Sym = SymVector[SymIndex];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000249 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000250 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000251}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000252
Peter Collingbourne4f952702016-05-01 04:55:03 +0000253// Find an existing symbol or create and insert a new one, then apply the given
254// attributes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000255std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
256 uint8_t Visibility,
257 bool CanOmitFromDynSym,
258 InputFile *File) {
259 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000260 bool WasInserted;
261 std::tie(S, WasInserted) = insert(Name);
262
263 // Merge in the new symbol's visibility.
264 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000265
Peter Collingbourne4f952702016-05-01 04:55:03 +0000266 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
267 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000268
Rui Ueyamaac647252017-10-29 16:46:39 +0000269 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000270 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000271
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000272 if (!WasInserted && S->Type != Symbol::UnknownType &&
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000273 ((Type == STT_TLS) != S->isTls())) {
274 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
275 toString(S->File) + "\n>>> defined in " + toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000276 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000277
278 return {S, WasInserted};
279}
280
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000281template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000282 return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
Rafael Espindola244ef982017-07-26 18:42:48 +0000283 /*Type*/ 0,
284 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000285}
286
Rui Ueyamae50e8072016-12-22 05:11:12 +0000287static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
288
Peter Collingbourne4f952702016-05-01 04:55:03 +0000289template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +0000290Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000291 uint8_t StOther, uint8_t Type,
292 bool CanOmitFromDynSym, InputFile *File) {
293 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000294 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000295 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000296 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000297 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
298 // An undefined symbol with non default visibility must be satisfied
299 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000300 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000301 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000302 return S;
303 }
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000304 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
305 S->Binding = Binding;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000306 if (Binding != STB_WEAK) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000307 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000308 if (!Config->GcSections)
309 SS->getFile<ELFT>()->IsNeeded = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000310 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000311 if (auto *L = dyn_cast<Lazy>(S)) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000312 // An undefined weak will not fetch archive members. See comment on Lazy in
313 // Symbols.h for the details.
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000314 if (Binding == STB_WEAK)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000315 L->Type = Type;
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000316 else if (InputFile *F = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000317 addFile<ELFT>(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000318 }
319 return S;
320}
321
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000322// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
323// foo@@VER. We want to effectively ignore foo, so give precedence to
324// foo@@VER.
325// FIXME: If users can transition to using
326// .symver foo,foo@@@VER
327// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000328static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000329 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000330 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000331 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000332 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000333 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000334 return -1;
335 return 0;
336}
337
Peter Collingbourne4f952702016-05-01 04:55:03 +0000338// We have a new defined symbol with the specified binding. Return 1 if the new
339// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
340// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000341static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000342 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000343 if (WasInserted)
344 return 1;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000345 if (!S->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000346 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000347 if (int R = compareVersion(S, Name))
348 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000349 if (Binding == STB_WEAK)
350 return -1;
351 if (S->isWeak())
352 return 1;
353 return 0;
354}
355
356// We have a new non-common defined symbol with the specified binding. Return 1
357// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
358// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000359static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
360 bool IsAbsolute, uint64_t Value,
361 StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000362 if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000363 return Cmp;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000364 if (auto *R = dyn_cast<Defined>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000365 if (R->Section && isa<BssSection>(R->Section)) {
366 // Non-common symbols take precedence over common symbols.
367 if (Config->WarnCommon)
368 warn("common " + S->getName() + " is overridden");
369 return 1;
370 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000371 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
372 R->Value == Value)
373 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000374 }
375 return 0;
376}
377
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000378Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
379 uint8_t Binding, uint8_t StOther, uint8_t Type,
380 InputFile *File) {
381 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000382 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000383 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
384 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000385 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000386 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000387 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
388 Bss->File = File;
389 Bss->Live = !Config->GcSections;
390 InputSections.push_back(Bss);
391
Rafael Espindolabec37652017-11-17 01:37:50 +0000392 replaceSymbol<Defined>(S, File, N, Binding, StOther, Type, 0, Size, Bss);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000393 } else if (Cmp == 0) {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000394 auto *D = cast<Defined>(S);
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000395 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
396 if (!Bss) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000397 // Non-common symbols take precedence over common symbols.
398 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000399 warn("common " + S->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000400 return S;
401 }
402
403 if (Config->WarnCommon)
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000404 warn("multiple common of " + D->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000405
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000406 Bss->Alignment = std::max(Bss->Alignment, Alignment);
407 if (Size > Bss->Size) {
408 D->File = Bss->File = File;
409 D->Size = Bss->Size = Size;
410 }
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) {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000431 Defined *D = dyn_cast<Defined>(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)
Rafael Espindolabec37652017-11-17 01:37:50 +0000471 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
472 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)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000498 uint8_t Binding = S->Binding;
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000499 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
500 Sym.getType(), Sym.st_value, Sym.st_size,
501 Alignment, Verdef);
Rafael Espindolabec37652017-11-17 01:37:50 +0000502 if (!WasInserted) {
503 S->Binding = Binding;
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000504 if (!S->isWeak() && !Config->GcSections)
Rafael Espindolade563432017-11-22 17:50:42 +0000505 File->IsNeeded = true;
Rafael Espindolabec37652017-11-17 01:37:50 +0000506 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000507 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000508}
509
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000510Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
511 uint8_t StOther, uint8_t Type,
512 bool CanOmitFromDynSym, BitcodeFile *F) {
513 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000514 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000515 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000516 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000517 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
518 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000519 if (Cmp > 0)
Rafael Espindolabec37652017-11-17 01:37:50 +0000520 replaceSymbol<Defined>(S, F, Name, Binding, StOther, Type, 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000521 else if (Cmp == 0)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000522 reportDuplicate(S, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000523 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000524}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000525
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000526Symbol *SymbolTable::find(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000527 auto It = SymMap.find(CachedHashStringRef(Name));
528 if (It == SymMap.end())
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000529 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000530 if (It->second == -1)
Rui Ueyamae3357902016-07-18 01:35:00 +0000531 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000532 return SymVector[It->second];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000533}
534
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000535template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000536Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F,
537 const object::Archive::Symbol Sym) {
538 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000539 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000540 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000541 if (WasInserted) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000542 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000543 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000544 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000545 if (!S->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000546 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000547
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000548 // An undefined weak will not fetch archive members. See comment on Lazy in
549 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000550 if (S->isWeak()) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000551 replaceSymbol<LazyArchive>(S, F, Sym, S->Type);
Rafael Espindolabec37652017-11-17 01:37:50 +0000552 S->Binding = STB_WEAK;
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) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000563 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000564 bool WasInserted;
565 std::tie(S, WasInserted) = insert(Name);
566 if (WasInserted) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000567 replaceSymbol<LazyObject>(S, &Obj, Name, Symbol::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000568 return;
569 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000570 if (!S->isUndefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000571 return;
572
573 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000574 if (S->isWeak())
Rui Ueyamaf483da02017-11-03 22:48:47 +0000575 replaceSymbol<LazyObject>(S, &Obj, Name, S->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
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000580// If we already saw this symbol, force loading its file.
581template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000582 if (Symbol *B = find(Name)) {
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000583 // Mark the symbol not to be eliminated by LTO
584 // even if it is a bitcode symbol.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000585 B->IsUsedInRegularObj = true;
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000586 if (auto *L = dyn_cast_or_null<Lazy>(B))
587 if (InputFile *File = L->fetch())
588 addFile<ELFT>(File);
589 }
590}
591
Rui Ueyama93bfee52015-10-13 18:10:33 +0000592// This function takes care of the case in which shared libraries depend on
593// the user program (not the other way, which is usual). Shared libraries
594// may have undefined symbols, expecting that the user program provides
595// the definitions for them. An example is BSD's __progname symbol.
596// We need to put such symbols to the main program's .dynsym so that
597// shared libraries can find them.
598// Except this, we ignore undefined symbols in DSOs.
Rafael Espindola244ef982017-07-26 18:42:48 +0000599template <class ELFT> void SymbolTable::scanShlibUndefined() {
George Rimar696a7f92017-09-19 09:20:54 +0000600 for (InputFile *F : SharedFiles) {
601 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000602 Symbol *Sym = find(U);
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000603 if (!Sym || !Sym->isDefined())
604 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000605 Sym->ExportDynamic = true;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000606
607 // If -dynamic-list is given, the default version is set to
608 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
609 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
610 // specified by -dynamic-list.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000611 Sym->VersionId = VER_NDX_GLOBAL;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000612 }
613 }
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000614}
615
Rui Ueyama82492142016-11-15 18:41:52 +0000616// Initialize DemangledSyms with a map from demangled symbols to symbol
617// objects. Used to handle "extern C++" directive in version scripts.
618//
619// The map will contain all demangled symbols. That can be very large,
620// and in LLD we generally want to avoid do anything for each symbol.
621// Then, why are we doing this? Here's why.
622//
623// Users can use "extern C++ {}" directive to match against demangled
624// C++ symbols. For example, you can write a pattern such as
625// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
626// other than trying to match a pattern against all demangled symbols.
627// So, if "extern C++" feature is used, we need to demangle all known
628// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000629StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000630 if (!DemangledSyms) {
631 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000632 for (Symbol *Sym : SymVector) {
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000633 if (!Sym->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000634 continue;
Rui Ueyama53fe4692017-11-28 02:15:26 +0000635 if (Optional<std::string> S = demangleItanium(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000636 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000637 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000638 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000639 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000640 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000641 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000642}
643
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000644std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000645 if (Ver.IsExternCpp)
646 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000647 if (Symbol *B = find(Ver.Name))
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000648 if (B->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000649 return {B};
650 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000651}
652
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000653std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
654 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000655 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000656
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000657 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000658 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000659 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000660 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000661 return Res;
662 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000663
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000664 for (Symbol *Sym : SymVector)
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000665 if (Sym->isDefined() && M.match(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000666 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000667 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000668}
669
Rui Ueyamaea265042016-09-13 20:51:30 +0000670// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000671// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000672// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000673void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000674 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
675 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
676 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
677 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
678 for (SymbolVersion &Ver : Config->VersionScriptLocals)
679 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
680 for (SymbolVersion &Ver : Config->VersionScriptLocals)
681 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000682}
683
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000684// Handles -dynamic-list.
685void SymbolTable::handleDynamicList() {
686 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000687 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000688 if (Ver.HasWildcard)
689 Syms = findByVersion(Ver);
690 else
691 Syms = findAllByVersion(Ver);
692
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000693 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000694 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000695 B->ExportDynamic = true;
696 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000697 B->IsPreemptible = true;
698 }
699 }
700}
701
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000702// Set symbol versions to symbols. This function handles patterns
703// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000704void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
705 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000706 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000707 return;
708
709 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000710 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000711 if (Syms.empty()) {
712 if (Config->NoUndefinedVersion)
713 error("version script assignment of '" + VersionName + "' to symbol '" +
714 Ver.Name + "' failed: symbol not defined");
715 return;
716 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000717
718 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000719 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000720 // Skip symbols containing version info because symbol versions
721 // specified by symbol names take precedence over version scripts.
722 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000723 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000724 continue;
725
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000726 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000727 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000728 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000729 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000730 }
731}
732
Rafael Espindola244ef982017-07-26 18:42:48 +0000733void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000734 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000735 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000736
737 // Exact matching takes precendence over fuzzy matching,
738 // so we set a version to a symbol only if no version has been assigned
739 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000740 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000741 if (B->VersionId == Config->DefaultSymbolVersion)
742 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000743}
744
Rui Ueyamadad2b882016-09-02 22:15:08 +0000745// This function processes version scripts by updating VersionId
746// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000747void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000748 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000749 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000750 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000751
Rui Ueyamadad2b882016-09-02 22:15:08 +0000752 // Now we have version definitions, so we need to set version ids to symbols.
753 // Each version definition has a glob pattern, and all symbols that match
754 // with the pattern get that version.
755
Rui Ueyamadad2b882016-09-02 22:15:08 +0000756 // First, we assign versions to exact matching symbols,
757 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000758 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000759 for (SymbolVersion &Ver : V.Globals)
760 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000761
Rui Ueyamadad2b882016-09-02 22:15:08 +0000762 // Next, we assign versions to fuzzy matching symbols,
763 // i.e. version definitions containing glob meta-characters.
764 // Note that because the last match takes precedence over previous matches,
765 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000766 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
767 for (SymbolVersion &Ver : V.Globals)
768 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000769
770 // Symbol themselves might know their versions because symbols
771 // can contain versions in the form of <name>@<version>.
772 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000773 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000774 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000775}
776
Rafael Espindola244ef982017-07-26 18:42:48 +0000777template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
778template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
779template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
780template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
781
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000782template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
783template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
784template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
785template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000786
Rafael Espindolabec37652017-11-17 01:37:50 +0000787template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
788 uint8_t, bool, InputFile *);
789template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
790 uint8_t, bool, InputFile *);
791template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
792 uint8_t, bool, InputFile *);
793template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
794 uint8_t, bool, InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000795
Rafael Espindola244ef982017-07-26 18:42:48 +0000796template void SymbolTable::addCombinedLTOObject<ELF32LE>();
797template void SymbolTable::addCombinedLTOObject<ELF32BE>();
798template void SymbolTable::addCombinedLTOObject<ELF64LE>();
799template void SymbolTable::addCombinedLTOObject<ELF64BE>();
800
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000801template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t,
802 uint64_t, uint64_t, uint8_t,
803 SectionBase *, InputFile *);
804template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t,
805 uint64_t, uint64_t, uint8_t,
806 SectionBase *, InputFile *);
807template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t,
808 uint64_t, uint64_t, uint8_t,
809 SectionBase *, InputFile *);
810template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t,
811 uint64_t, uint64_t, uint8_t,
812 SectionBase *, InputFile *);
Rafael Espindola244ef982017-07-26 18:42:48 +0000813
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000814template Defined *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t,
815 uint8_t);
816template Defined *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t,
817 uint8_t);
818template Defined *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t,
819 uint8_t);
820template Defined *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t,
821 uint8_t);
Rafael Espindola244ef982017-07-26 18:42:48 +0000822
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000823template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000824SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000825 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000826template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000827SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000828 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000829template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000830SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000831 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000832template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000833SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000834 const object::Archive::Symbol);
835
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000836template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
837template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
838template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
839template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000840
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000841template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000842 const typename ELF32LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000843 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000844 const typename ELF32LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000845template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000846 const typename ELF32BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000847 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000848 const typename ELF32BE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000849template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000850 const typename ELF64LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000851 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000852 const typename ELF64LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000853template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000854 const typename ELF64BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000855 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000856 const typename ELF64BE::Verdef *);
857
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000858template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
859template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
860template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
861template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
862
Rafael Espindola244ef982017-07-26 18:42:48 +0000863template void SymbolTable::scanShlibUndefined<ELF32LE>();
864template void SymbolTable::scanShlibUndefined<ELF32BE>();
865template void SymbolTable::scanShlibUndefined<ELF64LE>();
866template void SymbolTable::scanShlibUndefined<ELF64BE>();