blob: 9f64746515bf92b5bc911121df176da37553a6e8 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// Symbol table is a bag of all known symbols. We put all symbols of
Rui Ueyamac9559d92016-01-05 20:47:37 +000011// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000012// a hash table with the logic to resolve symbol name conflicts using
13// the symbol types.
14//
15//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000018#include "Config.h"
Davide Italiano8e1131d2016-06-29 02:46:51 +000019#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000020#include "Memory.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000021#include "Symbols.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000022#include "lld/Common/ErrorHandler.h"
Rui Ueyamacd236a92016-11-17 19:57:43 +000023#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
25using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000026using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000027using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
29using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000030using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
Rafael Espindola244ef982017-07-26 18:42:48 +000032SymbolTable *elf::Symtab;
33
Rui Ueyama227cb6b2017-10-15 21:43:09 +000034static InputFile *getFirstElf() {
35 if (!ObjectFiles.empty())
36 return ObjectFiles[0];
37 if (!SharedFiles.empty())
38 return SharedFiles[0];
39 return nullptr;
40}
41
Rui Ueyamac9559d92016-01-05 20:47:37 +000042// All input object files must be for the same architecture
43// (e.g. it does not make sense to link x86 object files with
44// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000045template <class ELFT> static bool isCompatible(InputFile *F) {
46 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000047 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000048
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000049 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
50 if (Config->EMachine != EM_MIPS)
51 return true;
52 if (isMipsN32Abi(F) == Config->MipsN32Abi)
53 return true;
54 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000055
56 if (!Config->Emulation.empty())
57 error(toString(F) + " is incompatible with " + Config->Emulation);
58 else
Rui Ueyama227cb6b2017-10-15 21:43:09 +000059 error(toString(F) + " is incompatible with " + toString(getFirstElf()));
Rui Ueyama16ba6692016-01-29 19:41:13 +000060 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000061}
62
Rui Ueyamac9559d92016-01-05 20:47:37 +000063// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000064template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000065 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000066 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000067
Michael J. Spencera9424f32016-09-09 22:08:04 +000068 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000069 if (auto *F = dyn_cast<BinaryFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +000070 BinaryFiles.push_back(F);
Rafael Espindola093abab2016-10-27 17:45:40 +000071 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000072 return;
73 }
74
Rui Ueyama89575742015-12-16 22:59:13 +000075 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000076 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000077 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000078 return;
79 }
Rui Ueyama3d451792015-10-12 18:03:21 +000080
George Rimar2a78fce2016-04-13 18:07:57 +000081 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000082 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000083 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000084 return;
85 }
86
87 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000088 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000089
Rui Ueyama89575742015-12-16 22:59:13 +000090 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000091 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000092 // DSOs are uniquified not by filename but by soname.
93 F->parseSoName();
Bob Haarmanb8a59c82017-10-25 22:28:38 +000094 if (errorCount() || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000095 return;
George Rimar696a7f92017-09-19 09:20:54 +000096 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000097 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000098 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000099 }
Rui Ueyama89575742015-12-16 22:59:13 +0000100
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000101 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000102 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000103 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000104 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000105 return;
106 }
107
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000108 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000109 ObjectFiles.push_back(File);
110 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111}
112
Rui Ueyama42554752016-04-23 00:26:32 +0000113// This function is where all the optimizations of link-time
114// optimization happens. When LTO is in use, some input files are
115// not in native object file format but in the LLVM bitcode format.
116// This function compiles bitcode files into a few big native files
117// using LLVM functions and replaces bitcode symbols with the results.
118// Because all bitcode files that consist of a program are passed
119// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000120template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000121 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000122 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000123
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000124 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000125 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000126 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000127 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000128
Davide Italiano3bfa0812016-11-26 05:37:04 +0000129 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000130 DenseSet<CachedHashStringRef> DummyGroups;
George Rimar696a7f92017-09-19 09:20:54 +0000131 cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
132 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000133 }
134}
135
Rafael Espindola0e604f92015-09-25 18:56:53 +0000136template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000137DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
138 uint8_t Binding) {
139 Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding,
140 nullptr, nullptr);
Rui Ueyama80474a22017-02-28 19:29:55 +0000141 return cast<DefinedRegular>(Sym->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000142}
143
Rui Ueyama69c778c2016-07-17 17:50:09 +0000144// Set a flag for --trace-symbol so that we can print out a log message
145// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000146void SymbolTable::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000147 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000148}
149
Rui Ueyamadeb15402016-01-07 17:20:07 +0000150// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
151// Used to implement --wrap.
Rafael Espindola244ef982017-07-26 18:42:48 +0000152template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000153 SymbolBody *B = find(Name);
154 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000155 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000156 Symbol *Sym = B->symbol();
Rafael Espindola244ef982017-07-26 18:42:48 +0000157 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
158 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000159
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000160 defsym(Real, Sym);
161 defsym(Sym, Wrap);
Rafael Espindola46935082017-10-06 20:09:34 +0000162
163 WrapSymbols.push_back({Wrap, Real});
Rui Ueyamadeb15402016-01-07 17:20:07 +0000164}
165
George Rimar9703ad22017-04-26 10:40:02 +0000166// Creates alias for symbol. Used to implement --defsym=ALIAS=SYM.
George Rimar67c60722017-07-18 11:55:35 +0000167template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000168void SymbolTable::addSymbolAlias(StringRef Alias, StringRef Name) {
George Rimar9703ad22017-04-26 10:40:02 +0000169 SymbolBody *B = find(Name);
170 if (!B) {
171 error("-defsym: undefined symbol: " + Name);
172 return;
173 }
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000174
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000175 defsym(addUndefined<ELFT>(Alias), B->symbol());
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000176}
177
178// Apply symbol renames created by -wrap and -defsym. The renames are created
179// before LTO in addSymbolWrap() and addSymbolAlias() to have a chance to inform
180// LTO (if LTO is running) not to include these symbols in IPO. Now that the
181// symbols are finalized, we can perform the replacement.
Rafael Espindola244ef982017-07-26 18:42:48 +0000182void SymbolTable::applySymbolRenames() {
Rafael Espindola46935082017-10-06 20:09:34 +0000183 // This function rotates 3 symbols:
184 //
185 // __real_foo becomes foo
186 // foo becomes __wrap_foo
187 // __wrap_foo becomes __real_foo
188 //
189 // The last part is special in that we don't want to change what references to
190 // __wrap_foo point to, we just want have __real_foo in the symbol table.
191
192 // First make a copy of __real_foo
193 std::vector<Symbol> Origs;
194 for (const auto &P : WrapSymbols)
195 Origs.push_back(*P.second);
196
197 // Replace __real_foo with foo and foo with __wrap_foo
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000198 for (SymbolRenaming &S : Defsyms) {
Rui Ueyama5bbe4a42017-09-25 00:57:30 +0000199 S.Dst->body()->copyFrom(S.Src->body());
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000200 S.Dst->File = S.Src->File;
201 S.Dst->Binding = S.Binding;
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000202 }
Rafael Espindola46935082017-10-06 20:09:34 +0000203
204 // Hide one of the copies of __wrap_foo, create a new symbol and copy
205 // __real_foo into it.
206 for (unsigned I = 0, N = WrapSymbols.size(); I < N; ++I) {
207 // We now have two copies of __wrap_foo. Drop one.
208 Symbol *Wrap = WrapSymbols[I].first;
209 Wrap->IsUsedInRegularObj = false;
210
211 Symbol *Real = &Origs[I];
212 // If __real_foo was undefined, we don't want it in the symbol table.
Rui Ueyamabda337a2017-10-27 22:54:16 +0000213 if (!Real->body()->isInCurrentOutput())
Rafael Espindola46935082017-10-06 20:09:34 +0000214 continue;
215
216 auto *NewSym = make<Symbol>();
217 memcpy(NewSym, Real, sizeof(Symbol));
218 SymVector.push_back(NewSym);
219 }
George Rimar9703ad22017-04-26 10:40:02 +0000220}
221
Peter Collingbournedadcc172016-04-22 18:42:48 +0000222static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
223 if (VA == STV_DEFAULT)
224 return VB;
225 if (VB == STV_DEFAULT)
226 return VA;
227 return std::min(VA, VB);
228}
229
Rui Ueyamab4de5952016-01-08 22:01:33 +0000230// Find an existing symbol or create and insert a new one.
Rafael Espindola244ef982017-07-26 18:42:48 +0000231std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000232 // <name>@@<version> means the symbol is the default version. In that
233 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000234 //
235 // Since this is a hot path, the following string search code is
236 // optimized for speed. StringRef::find(char) is much faster than
237 // StringRef::find(StringRef).
238 size_t Pos = Name.find('@');
239 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000240 Name = Name.take_front(Pos);
241
Justin Lebar3c11e932016-10-18 17:50:36 +0000242 auto P = Symtab.insert(
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000243 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000244 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000245 bool IsNew = P.second;
246
Rui Ueyamae3357902016-07-18 01:35:00 +0000247 if (V.Idx == -1) {
248 IsNew = true;
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000249 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000250 }
251
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000252 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000253 if (IsNew) {
Rui Ueyama175e81c2017-02-28 19:36:30 +0000254 Sym = make<Symbol>();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000255 Sym->InVersionScript = false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000256 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000257 Sym->Visibility = STV_DEFAULT;
258 Sym->IsUsedInRegularObj = false;
259 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000260 Sym->CanInline = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000261 Sym->Traced = V.Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000262 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000263 SymVector.push_back(Sym);
264 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000265 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000266 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000267 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000268}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000269
Peter Collingbourne4f952702016-05-01 04:55:03 +0000270// Find an existing symbol or create and insert a new one, then apply the given
271// attributes.
Rafael Espindola244ef982017-07-26 18:42:48 +0000272std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
273 uint8_t Visibility,
274 bool CanOmitFromDynSym,
275 InputFile *File) {
Rui Ueyama42479e02017-08-19 00:13:54 +0000276 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000277 Symbol *S;
278 bool WasInserted;
279 std::tie(S, WasInserted) = insert(Name);
280
281 // Merge in the new symbol's visibility.
282 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000283
Peter Collingbourne4f952702016-05-01 04:55:03 +0000284 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
285 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000286
Peter Collingbourne4f952702016-05-01 04:55:03 +0000287 if (IsUsedInRegularObj)
288 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000289
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000290 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
Rui Ueyama810ce102017-03-31 23:40:21 +0000291 ((Type == STT_TLS) != S->body()->isTls())) {
292 error("TLS attribute mismatch: " + toString(*S->body()) +
Rafael Espindola6e93d052017-08-04 22:31:42 +0000293 "\n>>> defined in " + toString(S->File) + "\n>>> defined in " +
294 toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000295 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000296
297 return {S, WasInserted};
298}
299
Rafael Espindola244ef982017-07-26 18:42:48 +0000300template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
301 return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
302 /*Type*/ 0,
303 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000304}
305
Rui Ueyamae50e8072016-12-22 05:11:12 +0000306static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
307
Peter Collingbourne4f952702016-05-01 04:55:03 +0000308template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000309Symbol *SymbolTable::addUndefined(StringRef Name, bool IsLocal, uint8_t Binding,
310 uint8_t StOther, uint8_t Type,
311 bool CanOmitFromDynSym, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000312 Symbol *S;
313 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000314 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000315 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000316 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
317 // An undefined symbol with non default visibility must be satisfied
318 // in the same DSO.
319 if (WasInserted ||
320 (isa<SharedSymbol>(S->body()) && Visibility != STV_DEFAULT)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000321 S->Binding = Binding;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000322 replaceBody<Undefined>(S, File, Name, IsLocal, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000323 return S;
324 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000325 if (Binding != STB_WEAK) {
Rafael Espindola5e20c752017-05-03 18:40:27 +0000326 SymbolBody *B = S->body();
Rui Ueyamabda337a2017-10-27 22:54:16 +0000327 if (!B->isInCurrentOutput())
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000328 S->Binding = Binding;
Rafael Espindola5e20c752017-05-03 18:40:27 +0000329 if (auto *SS = dyn_cast<SharedSymbol>(B))
Rafael Espindola6e93d052017-08-04 22:31:42 +0000330 SS->getFile<ELFT>()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000331 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000332 if (auto *L = dyn_cast<Lazy>(S->body())) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000333 // An undefined weak will not fetch archive members. See comment on Lazy in
334 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000335 if (S->isWeak())
336 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000337 else if (InputFile *F = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000338 addFile<ELFT>(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000339 }
340 return S;
341}
342
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000343// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
344// foo@@VER. We want to effectively ignore foo, so give precedence to
345// foo@@VER.
346// FIXME: If users can transition to using
347// .symver foo,foo@@@VER
348// we can delete this hack.
349static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000350 bool A = Name.contains("@@");
351 bool B = S->body()->getName().contains("@@");
352 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000353 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000354 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000355 return -1;
356 return 0;
357}
358
Peter Collingbourne4f952702016-05-01 04:55:03 +0000359// We have a new defined symbol with the specified binding. Return 1 if the new
360// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
361// strong defined symbols.
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000362static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
363 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000364 if (WasInserted)
365 return 1;
366 SymbolBody *Body = S->body();
Rui Ueyamabda337a2017-10-27 22:54:16 +0000367 if (!Body->isInCurrentOutput())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000368 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000369
370 if (int R = compareVersion(S, Name))
371 return R;
372
Peter Collingbourne4f952702016-05-01 04:55:03 +0000373 if (Binding == STB_WEAK)
374 return -1;
375 if (S->isWeak())
376 return 1;
377 return 0;
378}
379
380// We have a new non-common defined symbol with the specified binding. Return 1
381// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
382// is a conflict. If the new symbol wins, also update the binding.
Rafael Espindola858c0922016-12-02 02:58:21 +0000383static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000384 bool IsAbsolute, uint64_t Value,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000385 StringRef Name) {
386 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000387 if (Cmp > 0)
388 S->Binding = Binding;
389 return Cmp;
390 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000391 SymbolBody *B = S->body();
392 if (isa<DefinedCommon>(B)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000393 // Non-common symbols take precedence over common symbols.
394 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000395 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000396 return 1;
Rui Ueyama80474a22017-02-28 19:29:55 +0000397 } else if (auto *R = dyn_cast<DefinedRegular>(B)) {
Rafael Espindola858c0922016-12-02 02:58:21 +0000398 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
399 R->Value == Value)
400 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000401 }
402 return 0;
403}
404
Rafael Espindola244ef982017-07-26 18:42:48 +0000405Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
406 uint8_t Binding, uint8_t StOther, uint8_t Type,
407 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000408 Symbol *S;
409 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000410 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
411 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000412 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000413 if (Cmp > 0) {
414 S->Binding = Binding;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000415 replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000416 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000417 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000418 if (!C) {
419 // Non-common symbols take precedence over common symbols.
420 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000421 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000422 return S;
423 }
424
425 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000426 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000427
Rafael Espindola8db87292016-08-31 13:42:08 +0000428 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
429 if (Size > C->Size)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000430 replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000431 }
432 return S;
433}
434
Rui Ueyama810ce102017-03-31 23:40:21 +0000435static void warnOrError(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000436 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000437 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000438 else
439 error(Msg);
440}
441
Rui Ueyama810ce102017-03-31 23:40:21 +0000442static void reportDuplicate(SymbolBody *Sym, InputFile *NewFile) {
George Rimar67c60722017-07-18 11:55:35 +0000443 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
Rafael Espindola6e93d052017-08-04 22:31:42 +0000444 toString(Sym->getFile()) + "\n>>> defined in " +
445 toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000446}
447
448template <class ELFT>
Rui Ueyama810ce102017-03-31 23:40:21 +0000449static void reportDuplicate(SymbolBody *Sym, InputSectionBase *ErrSec,
Eugene Leviant825e5382016-11-08 16:26:32 +0000450 typename ELFT::uint ErrOffset) {
Rui Ueyama810ce102017-03-31 23:40:21 +0000451 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym);
Eugene Leviant825e5382016-11-08 16:26:32 +0000452 if (!D || !D->Section || !ErrSec) {
Rafael Espindolacb83c8c2017-07-25 23:23:40 +0000453 reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr);
Eugene Leviant825e5382016-11-08 16:26:32 +0000454 return;
455 }
456
Rui Ueyama810ce102017-03-31 23:40:21 +0000457 // Construct and print an error message in the form of:
458 //
459 // ld.lld: error: duplicate symbol: foo
460 // >>> defined at bar.c:30
461 // >>> bar.o (/home/alice/src/bar.o)
462 // >>> defined at baz.c:563
463 // >>> baz.o in archive libbaz.a
464 auto *Sec1 = cast<InputSectionBase>(D->Section);
465 std::string Src1 = Sec1->getSrcMsg<ELFT>(D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000466 std::string Obj1 = Sec1->getObjMsg(D->Value);
Rui Ueyama810ce102017-03-31 23:40:21 +0000467 std::string Src2 = ErrSec->getSrcMsg<ELFT>(ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000468 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000469
Rui Ueyama810ce102017-03-31 23:40:21 +0000470 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
471 if (!Src1.empty())
472 Msg += Src1 + "\n>>> ";
473 Msg += Obj1 + "\n>>> defined at ";
474 if (!Src2.empty())
475 Msg += Src2 + "\n>>> ";
476 Msg += Obj2;
477 warnOrError(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000478}
479
Peter Collingbourne4f952702016-05-01 04:55:03 +0000480template <typename ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000481Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
482 uint64_t Value, uint64_t Size, uint8_t Binding,
483 SectionBase *Section, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000484 Symbol *S;
485 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000486 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000487 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000488 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
489 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000490 if (Cmp > 0)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000491 replaceBody<DefinedRegular>(S, File, Name, /*IsLocal=*/false, StOther, Type,
492 Value, Size, Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000493 else if (Cmp == 0)
Rafael Espindola5616adf2017-03-08 22:36:28 +0000494 reportDuplicate<ELFT>(S->body(),
495 dyn_cast_or_null<InputSectionBase>(Section), Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000496 return S;
497}
498
499template <typename ELFT>
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000500void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> *File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000501 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000502 const typename ELFT::Verdef *Verdef) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000503 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
504 // as the visibility, which will leave the visibility in the symbol table
505 // unchanged.
506 Symbol *S;
507 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000508 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
509 /*CanOmitFromDynSym*/ true, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000510 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000511 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000512 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000513
Rafael Espindola8465d0832017-04-04 20:03:34 +0000514 SymbolBody *Body = S->body();
515 // An undefined symbol with non default visibility must be satisfied
516 // in the same DSO.
Rafael Espindolaea413cb2017-10-13 21:52:33 +0000517 if (WasInserted || ((Body->isUndefined() || Body->isLazy()) &&
518 Body->getVisibility() == STV_DEFAULT)) {
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000519 replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(),
Rui Ueyamabd730e32017-10-28 22:18:17 +0000520 Sym.st_value, Sym.st_size, Alignment, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000521 if (!S->isWeak())
Rui Ueyama4076fa12017-02-26 23:35:34 +0000522 File->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000523 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000524}
525
Rafael Espindola244ef982017-07-26 18:42:48 +0000526Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
527 uint8_t StOther, uint8_t Type,
528 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000529 Symbol *S;
530 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000531 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000532 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000533 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
534 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000535 if (Cmp > 0)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000536 replaceBody<DefinedRegular>(S, F, Name, /*IsLocal=*/false, StOther, Type, 0,
537 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000538 else if (Cmp == 0)
539 reportDuplicate(S->body(), F);
540 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000541}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000542
Rafael Espindola244ef982017-07-26 18:42:48 +0000543SymbolBody *SymbolTable::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000544 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000545 if (It == Symtab.end())
546 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000547 SymIndex V = It->second;
548 if (V.Idx == -1)
549 return nullptr;
550 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000551}
552
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000553void SymbolTable::defsym(Symbol *Dst, Symbol *Src) {
554 // We want to tell LTO not to inline Dst symbol because LTO doesn't
555 // know the final symbol contents after renaming.
556 Dst->CanInline = false;
Rafael Espindolac29b24d2017-10-05 00:35:47 +0000557
558 // Tell LTO not to eliminate this symbol.
559 Src->IsUsedInRegularObj = true;
560
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000561 Defsyms.push_back({Dst, Src, Dst->Binding});
562}
563
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000564template <class ELFT>
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000565Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F,
Rafael Espindola244ef982017-07-26 18:42:48 +0000566 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000567 Symbol *S;
568 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000569 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000570 if (WasInserted) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000571 replaceBody<LazyArchive>(S, F, Sym, SymbolBody::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000572 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000573 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000574 if (!S->body()->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000575 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000576
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000577 // An undefined weak will not fetch archive members. See comment on Lazy in
578 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000579 if (S->isWeak()) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000580 replaceBody<LazyArchive>(S, F, Sym, S->body()->Type);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000581 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000582 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000583 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
584 if (!MBInfo.first.getBuffer().empty())
Rafael Espindola244ef982017-07-26 18:42:48 +0000585 addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000586 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000587}
588
589template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000590void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000591 Symbol *S;
592 bool WasInserted;
593 std::tie(S, WasInserted) = insert(Name);
594 if (WasInserted) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000595 replaceBody<LazyObject>(S, &Obj, Name, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000596 return;
597 }
598 if (!S->body()->isUndefined())
599 return;
600
601 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000602 if (S->isWeak())
Rafael Espindola6e93d052017-08-04 22:31:42 +0000603 replaceBody<LazyObject>(S, &Obj, Name, S->body()->Type);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000604 else if (InputFile *F = Obj.fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000605 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000606}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000607
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000608// If we already saw this symbol, force loading its file.
609template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) {
610 if (SymbolBody *B = find(Name)) {
611 // Mark the symbol not to be eliminated by LTO
612 // even if it is a bitcode symbol.
613 B->symbol()->IsUsedInRegularObj = true;
614 if (auto *L = dyn_cast_or_null<Lazy>(B))
615 if (InputFile *File = L->fetch())
616 addFile<ELFT>(File);
617 }
618}
619
Rui Ueyama93bfee52015-10-13 18:10:33 +0000620// This function takes care of the case in which shared libraries depend on
621// the user program (not the other way, which is usual). Shared libraries
622// may have undefined symbols, expecting that the user program provides
623// the definitions for them. An example is BSD's __progname symbol.
624// We need to put such symbols to the main program's .dynsym so that
625// shared libraries can find them.
626// Except this, we ignore undefined symbols in DSOs.
Rafael Espindola244ef982017-07-26 18:42:48 +0000627template <class ELFT> void SymbolTable::scanShlibUndefined() {
George Rimar696a7f92017-09-19 09:20:54 +0000628 for (InputFile *F : SharedFiles) {
629 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000630 SymbolBody *Sym = find(U);
631 if (!Sym || !Sym->isDefined())
632 continue;
633 Sym->symbol()->ExportDynamic = true;
634
635 // If -dynamic-list is given, the default version is set to
636 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
637 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
638 // specified by -dynamic-list.
639 Sym->symbol()->VersionId = VER_NDX_GLOBAL;
640 }
641 }
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000642}
643
Rui Ueyama82492142016-11-15 18:41:52 +0000644// Initialize DemangledSyms with a map from demangled symbols to symbol
645// objects. Used to handle "extern C++" directive in version scripts.
646//
647// The map will contain all demangled symbols. That can be very large,
648// and in LLD we generally want to avoid do anything for each symbol.
649// Then, why are we doing this? Here's why.
650//
651// Users can use "extern C++ {}" directive to match against demangled
652// C++ symbols. For example, you can write a pattern such as
653// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
654// other than trying to match a pattern against all demangled symbols.
655// So, if "extern C++" feature is used, we need to demangle all known
656// symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000657StringMap<std::vector<SymbolBody *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000658 if (!DemangledSyms) {
659 DemangledSyms.emplace();
660 for (Symbol *Sym : SymVector) {
661 SymbolBody *B = Sym->body();
Rui Ueyamabda337a2017-10-27 22:54:16 +0000662 if (!B->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000663 continue;
Rui Ueyama96aff372016-12-22 05:31:52 +0000664 if (Optional<std::string> S = demangle(B->getName()))
665 (*DemangledSyms)[*S].push_back(B);
666 else
667 (*DemangledSyms)[B->getName()].push_back(B);
668 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000669 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000670 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000671}
672
Rafael Espindola244ef982017-07-26 18:42:48 +0000673std::vector<SymbolBody *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000674 if (Ver.IsExternCpp)
675 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000676 if (SymbolBody *B = find(Ver.Name))
Rui Ueyamabda337a2017-10-27 22:54:16 +0000677 if (B->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000678 return {B};
679 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000680}
681
Rafael Espindola244ef982017-07-26 18:42:48 +0000682std::vector<SymbolBody *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
Rafael Espindola191390a2016-12-08 16:26:20 +0000683 std::vector<SymbolBody *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000684 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000685
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000686 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000687 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000688 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000689 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000690 return Res;
691 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000692
693 for (Symbol *Sym : SymVector) {
694 SymbolBody *B = Sym->body();
Rui Ueyamabda337a2017-10-27 22:54:16 +0000695 if (B->isInCurrentOutput() && M.match(B->getName()))
Rafael Espindola191390a2016-12-08 16:26:20 +0000696 Res.push_back(B);
697 }
698 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000699}
700
Rui Ueyamaea265042016-09-13 20:51:30 +0000701// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000702// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000703// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000704void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000705 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
706 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
707 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
708 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
709 for (SymbolVersion &Ver : Config->VersionScriptLocals)
710 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
711 for (SymbolVersion &Ver : Config->VersionScriptLocals)
712 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000713}
714
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000715// Handles -dynamic-list.
716void SymbolTable::handleDynamicList() {
717 for (SymbolVersion &Ver : Config->DynamicList) {
718 std::vector<SymbolBody *> Syms;
719 if (Ver.HasWildcard)
720 Syms = findByVersion(Ver);
721 else
722 Syms = findAllByVersion(Ver);
723
724 for (SymbolBody *B : Syms) {
725 if (!Config->Shared)
Peter Collingbourned6924d72017-10-06 22:09:03 +0000726 B->symbol()->ExportDynamic = true;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000727 else if (B->symbol()->includeInDynsym())
728 B->IsPreemptible = true;
729 }
730 }
731}
732
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000733// Set symbol versions to symbols. This function handles patterns
734// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000735void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
736 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000737 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000738 return;
739
740 // Get a list of symbols which we need to assign the version to.
Rui Ueyama86581e42016-12-10 00:34:06 +0000741 std::vector<SymbolBody *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000742 if (Syms.empty()) {
743 if (Config->NoUndefinedVersion)
744 error("version script assignment of '" + VersionName + "' to symbol '" +
745 Ver.Name + "' failed: symbol not defined");
746 return;
747 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000748
749 // Assign the version.
750 for (SymbolBody *B : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000751 // Skip symbols containing version info because symbol versions
752 // specified by symbol names take precedence over version scripts.
753 // See parseSymbolVersion().
Rui Ueyama12234f82017-07-19 21:40:26 +0000754 if (B->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000755 continue;
756
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000757 Symbol *Sym = B->symbol();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000758 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000759 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000760 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000761 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000762 }
763}
764
Rafael Espindola244ef982017-07-26 18:42:48 +0000765void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000766 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000767 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000768
769 // Exact matching takes precendence over fuzzy matching,
770 // so we set a version to a symbol only if no version has been assigned
771 // to the symbol. This behavior is compatible with GNU.
Rui Ueyama9a189872017-07-11 20:33:04 +0000772 for (SymbolBody *B : findAllByVersion(Ver))
Rui Ueyama82492142016-11-15 18:41:52 +0000773 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
774 B->symbol()->VersionId = VersionId;
775}
776
Rui Ueyamadad2b882016-09-02 22:15:08 +0000777// This function processes version scripts by updating VersionId
778// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000779void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000780 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000781 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000782 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000783
Rui Ueyamadad2b882016-09-02 22:15:08 +0000784 // Now we have version definitions, so we need to set version ids to symbols.
785 // Each version definition has a glob pattern, and all symbols that match
786 // with the pattern get that version.
787
Rui Ueyamadad2b882016-09-02 22:15:08 +0000788 // First, we assign versions to exact matching symbols,
789 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000790 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000791 for (SymbolVersion &Ver : V.Globals)
792 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000793
Rui Ueyamadad2b882016-09-02 22:15:08 +0000794 // Next, we assign versions to fuzzy matching symbols,
795 // i.e. version definitions containing glob meta-characters.
796 // Note that because the last match takes precedence over previous matches,
797 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000798 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
799 for (SymbolVersion &Ver : V.Globals)
800 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000801
802 // Symbol themselves might know their versions because symbols
803 // can contain versions in the form of <name>@<version>.
804 // Let them parse and update their names to exclude version suffix.
805 for (Symbol *Sym : SymVector)
806 Sym->body()->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000807}
808
Rafael Espindola244ef982017-07-26 18:42:48 +0000809template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
810template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
811template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
812template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
813
814template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
815template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
816template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
817template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
818
Rui Ueyama3e96f112017-07-26 21:37:11 +0000819template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, bool, uint8_t,
820 uint8_t, uint8_t, bool,
821 InputFile *);
822template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, bool, uint8_t,
823 uint8_t, uint8_t, bool,
824 InputFile *);
825template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, bool, uint8_t,
826 uint8_t, uint8_t, bool,
827 InputFile *);
828template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, bool, uint8_t,
829 uint8_t, uint8_t, bool,
830 InputFile *);
831
Rafael Espindola244ef982017-07-26 18:42:48 +0000832template void SymbolTable::addSymbolAlias<ELF32LE>(StringRef, StringRef);
833template void SymbolTable::addSymbolAlias<ELF32BE>(StringRef, StringRef);
834template void SymbolTable::addSymbolAlias<ELF64LE>(StringRef, StringRef);
835template void SymbolTable::addSymbolAlias<ELF64BE>(StringRef, StringRef);
836
837template void SymbolTable::addCombinedLTOObject<ELF32LE>();
838template void SymbolTable::addCombinedLTOObject<ELF32BE>();
839template void SymbolTable::addCombinedLTOObject<ELF64LE>();
840template void SymbolTable::addCombinedLTOObject<ELF64BE>();
841
842template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t,
843 uint64_t, uint64_t, uint8_t,
844 SectionBase *, InputFile *);
845template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t,
846 uint64_t, uint64_t, uint8_t,
847 SectionBase *, InputFile *);
848template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t,
849 uint64_t, uint64_t, uint8_t,
850 SectionBase *, InputFile *);
851template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t,
852 uint64_t, uint64_t, uint8_t,
853 SectionBase *, InputFile *);
854
855template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t,
856 uint8_t);
857template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t,
858 uint8_t);
859template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t,
860 uint8_t);
861template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t,
862 uint8_t);
863
Rafael Espindola244ef982017-07-26 18:42:48 +0000864template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000865SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000866 const object::Archive::Symbol);
867template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000868SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000869 const object::Archive::Symbol);
870template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000871SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000872 const object::Archive::Symbol);
873template Symbol *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000874SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000875 const object::Archive::Symbol);
876
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000877template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
878template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
879template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
880template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000881
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000882template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000883 const typename ELF32LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000884 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000885 const typename ELF32LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000886template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000887 const typename ELF32BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000888 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000889 const typename ELF32BE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000890template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000891 const typename ELF64LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000892 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000893 const typename ELF64LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000894template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000895 const typename ELF64BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000896 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000897 const typename ELF64BE::Verdef *);
898
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000899template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
900template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
901template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
902template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
903
Rafael Espindola244ef982017-07-26 18:42:48 +0000904template void SymbolTable::scanShlibUndefined<ELF32LE>();
905template void SymbolTable::scanShlibUndefined<ELF32BE>();
906template void SymbolTable::scanShlibUndefined<ELF64LE>();
907template void SymbolTable::scanShlibUndefined<ELF64BE>();