blob: 8dd300ef77001f7a99172cd472cf42c0f6ce5d1a [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"
Peter Collingbourne6c55a702017-11-06 04:33:58 +000022#include "SyntheticSections.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000023#include "lld/Common/ErrorHandler.h"
Rui Ueyamacd236a92016-11-17 19:57:43 +000024#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000027using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000028using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rafael Espindola244ef982017-07-26 18:42:48 +000033SymbolTable *elf::Symtab;
34
Rui Ueyama227cb6b2017-10-15 21:43:09 +000035static InputFile *getFirstElf() {
36 if (!ObjectFiles.empty())
37 return ObjectFiles[0];
38 if (!SharedFiles.empty())
39 return SharedFiles[0];
40 return nullptr;
41}
42
Rui Ueyamac9559d92016-01-05 20:47:37 +000043// All input object files must be for the same architecture
44// (e.g. it does not make sense to link x86 object files with
45// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000046template <class ELFT> static bool isCompatible(InputFile *F) {
47 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000048 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000049
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000050 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
51 if (Config->EMachine != EM_MIPS)
52 return true;
53 if (isMipsN32Abi(F) == Config->MipsN32Abi)
54 return true;
55 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000056
57 if (!Config->Emulation.empty())
58 error(toString(F) + " is incompatible with " + Config->Emulation);
59 else
Rui Ueyama227cb6b2017-10-15 21:43:09 +000060 error(toString(F) + " is incompatible with " + toString(getFirstElf()));
Rui Ueyama16ba6692016-01-29 19:41:13 +000061 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000062}
63
Rui Ueyamac9559d92016-01-05 20:47:37 +000064// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000065template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000066 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000067 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000068
Michael J. Spencera9424f32016-09-09 22:08:04 +000069 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000070 if (auto *F = dyn_cast<BinaryFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +000071 BinaryFiles.push_back(F);
Rafael Espindola093abab2016-10-27 17:45:40 +000072 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000073 return;
74 }
75
Rui Ueyama89575742015-12-16 22:59:13 +000076 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000077 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000078 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000079 return;
80 }
Rui Ueyama3d451792015-10-12 18:03:21 +000081
George Rimar2a78fce2016-04-13 18:07:57 +000082 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000083 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000084 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000085 return;
86 }
87
88 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000089 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000090
Rui Ueyama89575742015-12-16 22:59:13 +000091 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000092 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000093 // DSOs are uniquified not by filename but by soname.
94 F->parseSoName();
Bob Haarmanb8a59c82017-10-25 22:28:38 +000095 if (errorCount() || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000096 return;
George Rimar696a7f92017-09-19 09:20:54 +000097 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000098 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000099 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000100 }
Rui Ueyama89575742015-12-16 22:59:13 +0000101
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000102 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000103 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000104 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000105 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000106 return;
107 }
108
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000109 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000110 ObjectFiles.push_back(File);
111 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000112}
113
Rui Ueyama42554752016-04-23 00:26:32 +0000114// This function is where all the optimizations of link-time
115// optimization happens. When LTO is in use, some input files are
116// not in native object file format but in the LLVM bitcode format.
117// This function compiles bitcode files into a few big native files
118// using LLVM functions and replaces bitcode symbols with the results.
119// Because all bitcode files that consist of a program are passed
120// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000121template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000122 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000123 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000124
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000125 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000126 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000127 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000128 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000129
Davide Italiano3bfa0812016-11-26 05:37:04 +0000130 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000131 DenseSet<CachedHashStringRef> DummyGroups;
George Rimar696a7f92017-09-19 09:20:54 +0000132 cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
133 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000134 }
135}
136
Rafael Espindola0e604f92015-09-25 18:56:53 +0000137template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000138DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
139 uint8_t Binding) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000140 Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding,
141 nullptr, nullptr);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000142 return cast<DefinedRegular>(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000143}
144
Rui Ueyama69c778c2016-07-17 17:50:09 +0000145// Set a flag for --trace-symbol so that we can print out a log message
146// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000147void SymbolTable::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000148 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000149}
150
Rui Ueyamadeb15402016-01-07 17:20:07 +0000151// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
152// Used to implement --wrap.
Rafael Espindola244ef982017-07-26 18:42:48 +0000153template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000154 Symbol *Sym = find(Name);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000155 if (!Sym)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000156 return;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000157 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
158 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000159 WrappedSymbols.push_back({Sym, Real, Wrap, Sym->Binding, Real->Binding});
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000160
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000161 // We want to tell LTO not to inline symbols to be overwritten
162 // because LTO doesn't know the final symbol contents after renaming.
163 Real->CanInline = false;
164 Sym->CanInline = false;
Rafael Espindola46935082017-10-06 20:09:34 +0000165
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000166 // Tell LTO not to eliminate these symbols.
167 Sym->IsUsedInRegularObj = true;
168 Wrap->IsUsedInRegularObj = true;
Rui Ueyamadeb15402016-01-07 17:20:07 +0000169}
170
Rui Ueyama45b81402017-11-04 22:32:56 +0000171// Apply symbol renames created by -wrap. The renames are created
172// before LTO in addSymbolWrap() to have a chance to inform LTO (if
173// LTO is running) not to include these symbols in IPO. Now that the
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000174// symbols are finalized, we can perform the replacement.
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000175void SymbolTable::applySymbolWrap() {
Rafael Espindola46935082017-10-06 20:09:34 +0000176 // This function rotates 3 symbols:
177 //
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000178 // __real_sym becomes sym
179 // sym becomes __wrap_sym
180 // __wrap_sym becomes __real_sym
Rafael Espindola46935082017-10-06 20:09:34 +0000181 //
182 // The last part is special in that we don't want to change what references to
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000183 // __wrap_sym point to, we just want have __real_sym in the symbol table.
Rafael Espindola46935082017-10-06 20:09:34 +0000184
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000185 for (WrappedSymbol &W : WrappedSymbols) {
186 // First, make a copy of __real_sym.
187 Symbol *Real = nullptr;
188 if (W.Real->isInCurrentOutput()) {
189 Real = (Symbol *)make<SymbolUnion>();
190 memcpy(Real, W.Real, sizeof(SymbolUnion));
191 }
Rafael Espindola46935082017-10-06 20:09:34 +0000192
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000193 // Replace __real_sym with sym and sym with __wrap_sym.
194 W.Real->copyFrom(W.Sym);
195 W.Real->Binding = W.RealBinding;
196 W.Sym->copyFrom(W.Wrap);
197 W.Sym->Binding = W.SymBinding;
Rafael Espindola46935082017-10-06 20:09:34 +0000198
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000199 if (Real) {
200 // We now have two copies of __wrap_sym. Drop one.
201 W.Wrap->IsUsedInRegularObj = false;
202 SymVector.push_back(Real);
203 }
Rafael Espindola46935082017-10-06 20:09:34 +0000204 }
George Rimar9703ad22017-04-26 10:40:02 +0000205}
206
Peter Collingbournedadcc172016-04-22 18:42:48 +0000207static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
208 if (VA == STV_DEFAULT)
209 return VB;
210 if (VB == STV_DEFAULT)
211 return VA;
212 return std::min(VA, VB);
213}
214
Rui Ueyamab4de5952016-01-08 22:01:33 +0000215// Find an existing symbol or create and insert a new one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000216std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000217 // <name>@@<version> means the symbol is the default version. In that
218 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000219 //
220 // Since this is a hot path, the following string search code is
221 // optimized for speed. StringRef::find(char) is much faster than
222 // StringRef::find(StringRef).
223 size_t Pos = Name.find('@');
224 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000225 Name = Name.take_front(Pos);
226
Justin Lebar3c11e932016-10-18 17:50:36 +0000227 auto P = Symtab.insert(
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000228 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000229 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000230 bool IsNew = P.second;
231
Rui Ueyamae3357902016-07-18 01:35:00 +0000232 if (V.Idx == -1) {
233 IsNew = true;
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000234 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000235 }
236
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000237 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000238 if (IsNew) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000239 Sym = (Symbol *)make<SymbolUnion>();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000240 Sym->InVersionScript = false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000241 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000242 Sym->Visibility = STV_DEFAULT;
243 Sym->IsUsedInRegularObj = false;
244 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000245 Sym->CanInline = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000246 Sym->Traced = V.Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000247 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000248 SymVector.push_back(Sym);
249 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000250 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000251 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000252 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000253}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000254
Peter Collingbourne4f952702016-05-01 04:55:03 +0000255// Find an existing symbol or create and insert a new one, then apply the given
256// attributes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000257std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
258 uint8_t Visibility,
259 bool CanOmitFromDynSym,
260 InputFile *File) {
261 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000262 bool WasInserted;
263 std::tie(S, WasInserted) = insert(Name);
264
265 // Merge in the new symbol's visibility.
266 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000267
Peter Collingbourne4f952702016-05-01 04:55:03 +0000268 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
269 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000270
Rui Ueyamaac647252017-10-29 16:46:39 +0000271 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000272 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000273
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000274 if (!WasInserted && S->Type != Symbol::UnknownType &&
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000275 ((Type == STT_TLS) != S->isTls())) {
276 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
277 toString(S->File) + "\n>>> defined in " + toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000278 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000279
280 return {S, WasInserted};
281}
282
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000283template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindola244ef982017-07-26 18:42:48 +0000284 return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
285 /*Type*/ 0,
286 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000287}
288
Rui Ueyamae50e8072016-12-22 05:11:12 +0000289static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
290
Peter Collingbourne4f952702016-05-01 04:55:03 +0000291template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000292Symbol *SymbolTable::addUndefined(StringRef Name, bool IsLocal, uint8_t Binding,
293 uint8_t StOther, uint8_t Type,
294 bool CanOmitFromDynSym, InputFile *File) {
295 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000296 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000297 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000298 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000299 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
300 // An undefined symbol with non default visibility must be satisfied
301 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000302 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000303 S->Binding = Binding;
Rui Ueyamaf483da02017-11-03 22:48:47 +0000304 replaceSymbol<Undefined>(S, File, Name, IsLocal, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000305 return S;
306 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000307 if (Binding != STB_WEAK) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000308 if (!S->isInCurrentOutput())
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000309 S->Binding = Binding;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000310 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rafael Espindola6e93d052017-08-04 22:31:42 +0000311 SS->getFile<ELFT>()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000312 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000313 if (auto *L = dyn_cast<Lazy>(S)) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000314 // An undefined weak will not fetch archive members. See comment on Lazy in
315 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000316 if (S->isWeak())
317 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000318 else if (InputFile *F = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000319 addFile<ELFT>(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000320 }
321 return S;
322}
323
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000324// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
325// foo@@VER. We want to effectively ignore foo, so give precedence to
326// foo@@VER.
327// FIXME: If users can transition to using
328// .symver foo,foo@@@VER
329// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000330static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000331 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000332 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000333 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000334 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000335 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000336 return -1;
337 return 0;
338}
339
Peter Collingbourne4f952702016-05-01 04:55:03 +0000340// We have a new defined symbol with the specified binding. Return 1 if the new
341// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
342// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000343static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000344 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000345 if (WasInserted)
346 return 1;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000347 if (!S->isInCurrentOutput())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000348 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000349 if (int R = compareVersion(S, Name))
350 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000351 if (Binding == STB_WEAK)
352 return -1;
353 if (S->isWeak())
354 return 1;
355 return 0;
356}
357
358// We have a new non-common defined symbol with the specified binding. Return 1
359// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
360// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000361static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
362 bool IsAbsolute, uint64_t Value,
363 StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000364 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000365 if (Cmp > 0)
366 S->Binding = Binding;
367 return Cmp;
368 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000369 if (auto *R = dyn_cast<DefinedRegular>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000370 if (R->Section && isa<BssSection>(R->Section)) {
371 // Non-common symbols take precedence over common symbols.
372 if (Config->WarnCommon)
373 warn("common " + S->getName() + " is overridden");
374 return 1;
375 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000376 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
377 R->Value == Value)
378 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000379 }
380 return 0;
381}
382
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000383Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
384 uint8_t Binding, uint8_t StOther, uint8_t Type,
385 InputFile *File) {
386 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000387 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000388 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
389 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000390 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000391 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000392 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
393 Bss->File = File;
394 Bss->Live = !Config->GcSections;
395 InputSections.push_back(Bss);
396
Peter Collingbourne4f952702016-05-01 04:55:03 +0000397 S->Binding = Binding;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000398 replaceSymbol<DefinedRegular>(S, File, N, /*IsLocal=*/false, StOther, Type,
399 0, Size, Bss);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000400 } else if (Cmp == 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000401 auto *D = cast<DefinedRegular>(S);
402 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
403 if (!Bss) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000404 // Non-common symbols take precedence over common symbols.
405 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000406 warn("common " + S->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000407 return S;
408 }
409
410 if (Config->WarnCommon)
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000411 warn("multiple common of " + D->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000412
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000413 Bss->Alignment = std::max(Bss->Alignment, Alignment);
414 if (Size > Bss->Size) {
415 D->File = Bss->File = File;
416 D->Size = Bss->Size = Size;
417 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000418 }
419 return S;
420}
421
Rui Ueyama810ce102017-03-31 23:40:21 +0000422static void warnOrError(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000423 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000424 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000425 else
426 error(Msg);
427}
428
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000429static void reportDuplicate(Symbol *Sym, InputFile *NewFile) {
George Rimar67c60722017-07-18 11:55:35 +0000430 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
Rafael Espindola6e93d052017-08-04 22:31:42 +0000431 toString(Sym->getFile()) + "\n>>> defined in " +
432 toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000433}
434
435template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000436static void reportDuplicate(Symbol *Sym, InputSectionBase *ErrSec,
Eugene Leviant825e5382016-11-08 16:26:32 +0000437 typename ELFT::uint ErrOffset) {
Rui Ueyama810ce102017-03-31 23:40:21 +0000438 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym);
Eugene Leviant825e5382016-11-08 16:26:32 +0000439 if (!D || !D->Section || !ErrSec) {
Rafael Espindolacb83c8c2017-07-25 23:23:40 +0000440 reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr);
Eugene Leviant825e5382016-11-08 16:26:32 +0000441 return;
442 }
443
Rui Ueyama810ce102017-03-31 23:40:21 +0000444 // Construct and print an error message in the form of:
445 //
446 // ld.lld: error: duplicate symbol: foo
447 // >>> defined at bar.c:30
448 // >>> bar.o (/home/alice/src/bar.o)
449 // >>> defined at baz.c:563
450 // >>> baz.o in archive libbaz.a
451 auto *Sec1 = cast<InputSectionBase>(D->Section);
George Rimar82f0c422017-11-01 07:42:38 +0000452 std::string Src1 = Sec1->getSrcMsg<ELFT>(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000453 std::string Obj1 = Sec1->getObjMsg(D->Value);
George Rimar82f0c422017-11-01 07:42:38 +0000454 std::string Src2 = ErrSec->getSrcMsg<ELFT>(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000455 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000456
Rui Ueyama810ce102017-03-31 23:40:21 +0000457 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
458 if (!Src1.empty())
459 Msg += Src1 + "\n>>> ";
460 Msg += Obj1 + "\n>>> defined at ";
461 if (!Src2.empty())
462 Msg += Src2 + "\n>>> ";
463 Msg += Obj2;
464 warnOrError(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000465}
466
Peter Collingbourne4f952702016-05-01 04:55:03 +0000467template <typename ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000468Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
469 uint64_t Value, uint64_t Size, uint8_t Binding,
470 SectionBase *Section, InputFile *File) {
471 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000472 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000473 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000474 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000475 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
476 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000477 if (Cmp > 0)
Rui Ueyamaf483da02017-11-03 22:48:47 +0000478 replaceSymbol<DefinedRegular>(S, File, Name, /*IsLocal=*/false, StOther,
479 Type, Value, Size, Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000480 else if (Cmp == 0)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000481 reportDuplicate<ELFT>(S, dyn_cast_or_null<InputSectionBase>(Section),
482 Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000483 return S;
484}
485
486template <typename ELFT>
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000487void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> *File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000488 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000489 const typename ELFT::Verdef *Verdef) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000490 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
491 // as the visibility, which will leave the visibility in the symbol table
492 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000493 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000494 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000495 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
496 /*CanOmitFromDynSym*/ true, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000497 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000498 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000499 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000500
Rafael Espindola8465d0832017-04-04 20:03:34 +0000501 // An undefined symbol with non default visibility must be satisfied
502 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000503 if (WasInserted || ((S->isUndefined() || S->isLazy()) &&
504 S->getVisibility() == STV_DEFAULT)) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000505 replaceSymbol<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(),
506 Sym.st_value, Sym.st_size, Alignment, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000507 if (!S->isWeak())
Rui Ueyama4076fa12017-02-26 23:35:34 +0000508 File->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000509 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000510}
511
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000512Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
513 uint8_t StOther, uint8_t Type,
514 bool CanOmitFromDynSym, BitcodeFile *F) {
515 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000516 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000517 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000518 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000519 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
520 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000521 if (Cmp > 0)
Rui Ueyamaf483da02017-11-03 22:48:47 +0000522 replaceSymbol<DefinedRegular>(S, F, Name, /*IsLocal=*/false, StOther, Type,
523 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000524 else if (Cmp == 0)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000525 reportDuplicate(S, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000526 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000527}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000528
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000529Symbol *SymbolTable::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000530 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000531 if (It == Symtab.end())
532 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000533 SymIndex V = It->second;
534 if (V.Idx == -1)
535 return nullptr;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000536 return SymVector[V.Idx];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000537}
538
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000539template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000540Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F,
541 const object::Archive::Symbol Sym) {
542 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000543 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000544 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000545 if (WasInserted) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000546 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000547 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000548 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000549 if (!S->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000550 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000551
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000552 // An undefined weak will not fetch archive members. See comment on Lazy in
553 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000554 if (S->isWeak()) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000555 replaceSymbol<LazyArchive>(S, F, Sym, S->Type);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000556 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000557 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000558 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
559 if (!MBInfo.first.getBuffer().empty())
Rafael Espindola244ef982017-07-26 18:42:48 +0000560 addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000561 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000562}
563
564template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000565void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000566 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000567 bool WasInserted;
568 std::tie(S, WasInserted) = insert(Name);
569 if (WasInserted) {
Rui Ueyamaf483da02017-11-03 22:48:47 +0000570 replaceSymbol<LazyObject>(S, &Obj, Name, Symbol::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000571 return;
572 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000573 if (!S->isUndefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000574 return;
575
576 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000577 if (S->isWeak())
Rui Ueyamaf483da02017-11-03 22:48:47 +0000578 replaceSymbol<LazyObject>(S, &Obj, Name, S->Type);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000579 else if (InputFile *F = Obj.fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000580 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000581}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000582
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000583// If we already saw this symbol, force loading its file.
584template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000585 if (Symbol *B = find(Name)) {
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000586 // Mark the symbol not to be eliminated by LTO
587 // even if it is a bitcode symbol.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000588 B->IsUsedInRegularObj = true;
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000589 if (auto *L = dyn_cast_or_null<Lazy>(B))
590 if (InputFile *File = L->fetch())
591 addFile<ELFT>(File);
592 }
593}
594
Rui Ueyama93bfee52015-10-13 18:10:33 +0000595// This function takes care of the case in which shared libraries depend on
596// the user program (not the other way, which is usual). Shared libraries
597// may have undefined symbols, expecting that the user program provides
598// the definitions for them. An example is BSD's __progname symbol.
599// We need to put such symbols to the main program's .dynsym so that
600// shared libraries can find them.
601// Except this, we ignore undefined symbols in DSOs.
Rafael Espindola244ef982017-07-26 18:42:48 +0000602template <class ELFT> void SymbolTable::scanShlibUndefined() {
George Rimar696a7f92017-09-19 09:20:54 +0000603 for (InputFile *F : SharedFiles) {
604 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000605 Symbol *Sym = find(U);
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000606 if (!Sym || !Sym->isDefined())
607 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000608 Sym->ExportDynamic = true;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000609
610 // If -dynamic-list is given, the default version is set to
611 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
612 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
613 // specified by -dynamic-list.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000614 Sym->VersionId = VER_NDX_GLOBAL;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000615 }
616 }
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000617}
618
Rui Ueyama82492142016-11-15 18:41:52 +0000619// Initialize DemangledSyms with a map from demangled symbols to symbol
620// objects. Used to handle "extern C++" directive in version scripts.
621//
622// The map will contain all demangled symbols. That can be very large,
623// and in LLD we generally want to avoid do anything for each symbol.
624// Then, why are we doing this? Here's why.
625//
626// Users can use "extern C++ {}" directive to match against demangled
627// C++ symbols. For example, you can write a pattern such as
628// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
629// other than trying to match a pattern against all demangled symbols.
630// So, if "extern C++" feature is used, we need to demangle all known
631// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000632StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000633 if (!DemangledSyms) {
634 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000635 for (Symbol *Sym : SymVector) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000636 if (!Sym->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000637 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000638 if (Optional<std::string> S = demangle(Sym->getName()))
639 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000640 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000641 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000642 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000643 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000644 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000645}
646
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000647std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000648 if (Ver.IsExternCpp)
649 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000650 if (Symbol *B = find(Ver.Name))
Rui Ueyamabda337a2017-10-27 22:54:16 +0000651 if (B->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000652 return {B};
653 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000654}
655
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000656std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
657 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000658 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000659
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000660 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000661 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000662 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000663 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000664 return Res;
665 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000666
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000667 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000668 if (Sym->isInCurrentOutput() && M.match(Sym->getName()))
669 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000670 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000671}
672
Rui Ueyamaea265042016-09-13 20:51:30 +0000673// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000674// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000675// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000676void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000677 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
678 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
679 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
680 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
681 for (SymbolVersion &Ver : Config->VersionScriptLocals)
682 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
683 for (SymbolVersion &Ver : Config->VersionScriptLocals)
684 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000685}
686
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000687// Handles -dynamic-list.
688void SymbolTable::handleDynamicList() {
689 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000690 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000691 if (Ver.HasWildcard)
692 Syms = findByVersion(Ver);
693 else
694 Syms = findAllByVersion(Ver);
695
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000696 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000697 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000698 B->ExportDynamic = true;
699 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000700 B->IsPreemptible = true;
701 }
702 }
703}
704
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000705// Set symbol versions to symbols. This function handles patterns
706// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000707void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
708 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000709 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000710 return;
711
712 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000713 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000714 if (Syms.empty()) {
715 if (Config->NoUndefinedVersion)
716 error("version script assignment of '" + VersionName + "' to symbol '" +
717 Ver.Name + "' failed: symbol not defined");
718 return;
719 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000720
721 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000722 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000723 // Skip symbols containing version info because symbol versions
724 // specified by symbol names take precedence over version scripts.
725 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000726 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000727 continue;
728
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000729 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000730 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000731 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000732 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000733 }
734}
735
Rafael Espindola244ef982017-07-26 18:42:48 +0000736void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000737 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000738 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000739
740 // Exact matching takes precendence over fuzzy matching,
741 // so we set a version to a symbol only if no version has been assigned
742 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000743 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000744 if (B->VersionId == Config->DefaultSymbolVersion)
745 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000746}
747
Rui Ueyamadad2b882016-09-02 22:15:08 +0000748// This function processes version scripts by updating VersionId
749// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000750void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000751 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000752 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000753 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000754
Rui Ueyamadad2b882016-09-02 22:15:08 +0000755 // Now we have version definitions, so we need to set version ids to symbols.
756 // Each version definition has a glob pattern, and all symbols that match
757 // with the pattern get that version.
758
Rui Ueyamadad2b882016-09-02 22:15:08 +0000759 // First, we assign versions to exact matching symbols,
760 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000761 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000762 for (SymbolVersion &Ver : V.Globals)
763 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000764
Rui Ueyamadad2b882016-09-02 22:15:08 +0000765 // Next, we assign versions to fuzzy matching symbols,
766 // i.e. version definitions containing glob meta-characters.
767 // Note that because the last match takes precedence over previous matches,
768 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000769 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
770 for (SymbolVersion &Ver : V.Globals)
771 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000772
773 // Symbol themselves might know their versions because symbols
774 // can contain versions in the form of <name>@<version>.
775 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000776 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000777 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000778}
779
Rafael Espindola244ef982017-07-26 18:42:48 +0000780template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
781template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
782template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
783template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
784
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000785template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
786template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
787template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
788template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000789
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000790template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, bool, uint8_t,
791 uint8_t, uint8_t, bool,
792 InputFile *);
793template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, bool, uint8_t,
794 uint8_t, uint8_t, bool,
795 InputFile *);
796template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, bool, uint8_t,
797 uint8_t, uint8_t, bool,
798 InputFile *);
799template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, bool, uint8_t,
800 uint8_t, uint8_t, bool,
801 InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000802
Rafael Espindola244ef982017-07-26 18:42:48 +0000803template void SymbolTable::addCombinedLTOObject<ELF32LE>();
804template void SymbolTable::addCombinedLTOObject<ELF32BE>();
805template void SymbolTable::addCombinedLTOObject<ELF64LE>();
806template void SymbolTable::addCombinedLTOObject<ELF64BE>();
807
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000808template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t,
809 uint64_t, uint64_t, uint8_t,
810 SectionBase *, InputFile *);
811template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t,
812 uint64_t, uint64_t, uint8_t,
813 SectionBase *, InputFile *);
814template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t,
815 uint64_t, uint64_t, uint8_t,
816 SectionBase *, InputFile *);
817template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t,
818 uint64_t, uint64_t, uint8_t,
819 SectionBase *, InputFile *);
Rafael Espindola244ef982017-07-26 18:42:48 +0000820
821template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t,
822 uint8_t);
823template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t,
824 uint8_t);
825template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t,
826 uint8_t);
827template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t,
828 uint8_t);
829
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000830template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000831SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000832 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000833template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000834SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000835 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000836template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000837SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000838 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000839template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000840SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000841 const object::Archive::Symbol);
842
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000843template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
844template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
845template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
846template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000847
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000848template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000849 const typename ELF32LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000850 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000851 const typename ELF32LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000852template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000853 const typename ELF32BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000854 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000855 const typename ELF32BE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000856template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000857 const typename ELF64LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000858 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000859 const typename ELF64LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000860template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000861 const typename ELF64BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000862 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000863 const typename ELF64BE::Verdef *);
864
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000865template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
866template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
867template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
868template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
869
Rafael Espindola244ef982017-07-26 18:42:48 +0000870template void SymbolTable::scanShlibUndefined<ELF32LE>();
871template void SymbolTable::scanShlibUndefined<ELF32BE>();
872template void SymbolTable::scanShlibUndefined<ELF64LE>();
873template void SymbolTable::scanShlibUndefined<ELF64BE>();