blob: 457387bbe8f8b461f58c05ef06ac5d2bd4c6eb3d [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// Symbol table is a bag of all known symbols. We put all symbols of
Rui Ueyamac9559d92016-01-05 20:47:37 +000011// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000012// a hash table with the logic to resolve symbol name conflicts using
13// the symbol types.
14//
15//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000018#include "Config.h"
Davide Italiano8e1131d2016-06-29 02:46:51 +000019#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000020#include "Memory.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000021#include "Symbols.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000022#include "lld/Common/ErrorHandler.h"
Rui Ueyamacd236a92016-11-17 19:57:43 +000023#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
25using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000026using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000027using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
29using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000030using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
Rafael Espindola244ef982017-07-26 18:42:48 +000032SymbolTable *elf::Symtab;
33
Rui Ueyama227cb6b2017-10-15 21:43:09 +000034static InputFile *getFirstElf() {
35 if (!ObjectFiles.empty())
36 return ObjectFiles[0];
37 if (!SharedFiles.empty())
38 return SharedFiles[0];
39 return nullptr;
40}
41
Rui Ueyamac9559d92016-01-05 20:47:37 +000042// All input object files must be for the same architecture
43// (e.g. it does not make sense to link x86 object files with
44// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000045template <class ELFT> static bool isCompatible(InputFile *F) {
46 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000047 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000048
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000049 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
50 if (Config->EMachine != EM_MIPS)
51 return true;
52 if (isMipsN32Abi(F) == Config->MipsN32Abi)
53 return true;
54 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000055
56 if (!Config->Emulation.empty())
57 error(toString(F) + " is incompatible with " + Config->Emulation);
58 else
Rui Ueyama227cb6b2017-10-15 21:43:09 +000059 error(toString(F) + " is incompatible with " + toString(getFirstElf()));
Rui Ueyama16ba6692016-01-29 19:41:13 +000060 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000061}
62
Rui Ueyamac9559d92016-01-05 20:47:37 +000063// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000064template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000065 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000066 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000067
Michael J. Spencera9424f32016-09-09 22:08:04 +000068 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000069 if (auto *F = dyn_cast<BinaryFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +000070 BinaryFiles.push_back(F);
Rafael Espindola093abab2016-10-27 17:45:40 +000071 F->parse<ELFT>();
Michael J. Spencera9424f32016-09-09 22:08:04 +000072 return;
73 }
74
Rui Ueyama89575742015-12-16 22:59:13 +000075 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000076 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000077 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000078 return;
79 }
Rui Ueyama3d451792015-10-12 18:03:21 +000080
George Rimar2a78fce2016-04-13 18:07:57 +000081 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000082 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000083 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000084 return;
85 }
86
87 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000088 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000089
Rui Ueyama89575742015-12-16 22:59:13 +000090 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000091 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000092 // DSOs are uniquified not by filename but by soname.
93 F->parseSoName();
Bob Haarmanb8a59c82017-10-25 22:28:38 +000094 if (errorCount() || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000095 return;
George Rimar696a7f92017-09-19 09:20:54 +000096 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000097 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000098 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000099 }
Rui Ueyama89575742015-12-16 22:59:13 +0000100
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000101 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000102 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000103 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000104 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000105 return;
106 }
107
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000108 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000109 ObjectFiles.push_back(File);
110 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111}
112
Rui Ueyama42554752016-04-23 00:26:32 +0000113// This function is where all the optimizations of link-time
114// optimization happens. When LTO is in use, some input files are
115// not in native object file format but in the LLVM bitcode format.
116// This function compiles bitcode files into a few big native files
117// using LLVM functions and replaces bitcode symbols with the results.
118// Because all bitcode files that consist of a program are passed
119// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000120template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000121 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000122 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000123
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000124 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000125 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000126 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000127 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000128
Davide Italiano3bfa0812016-11-26 05:37:04 +0000129 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000130 DenseSet<CachedHashStringRef> DummyGroups;
George Rimar696a7f92017-09-19 09:20:54 +0000131 cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
132 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000133 }
134}
135
Rafael Espindola0e604f92015-09-25 18:56:53 +0000136template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000137DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
138 uint8_t Binding) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000139 SymbolBody *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0,
140 Binding, nullptr, nullptr);
141 return cast<DefinedRegular>(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000142}
143
Rui Ueyama69c778c2016-07-17 17:50:09 +0000144// Set a flag for --trace-symbol so that we can print out a log message
145// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000146void SymbolTable::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000147 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000148}
149
Rui Ueyamadeb15402016-01-07 17:20:07 +0000150// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
151// Used to implement --wrap.
Rafael Espindola244ef982017-07-26 18:42:48 +0000152template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000153 SymbolBody *Sym = find(Name);
154 if (!Sym)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000155 return;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000156 SymbolBody *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
157 SymbolBody *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000158
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000159 defsym(Real, Sym);
160 defsym(Sym, Wrap);
Rafael Espindola46935082017-10-06 20:09:34 +0000161
162 WrapSymbols.push_back({Wrap, Real});
Rui Ueyamadeb15402016-01-07 17:20:07 +0000163}
164
George Rimar9703ad22017-04-26 10:40:02 +0000165// Creates alias for symbol. Used to implement --defsym=ALIAS=SYM.
George Rimar67c60722017-07-18 11:55:35 +0000166template <class ELFT>
Rafael Espindola244ef982017-07-26 18:42:48 +0000167void SymbolTable::addSymbolAlias(StringRef Alias, StringRef Name) {
George Rimar9703ad22017-04-26 10:40:02 +0000168 SymbolBody *B = find(Name);
169 if (!B) {
170 error("-defsym: undefined symbol: " + Name);
171 return;
172 }
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000173
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000174 defsym(addUndefined<ELFT>(Alias), B);
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000175}
176
177// Apply symbol renames created by -wrap and -defsym. The renames are created
178// before LTO in addSymbolWrap() and addSymbolAlias() to have a chance to inform
179// LTO (if LTO is running) not to include these symbols in IPO. Now that the
180// symbols are finalized, we can perform the replacement.
Rafael Espindola244ef982017-07-26 18:42:48 +0000181void SymbolTable::applySymbolRenames() {
Rafael Espindola46935082017-10-06 20:09:34 +0000182 // This function rotates 3 symbols:
183 //
184 // __real_foo becomes foo
185 // foo becomes __wrap_foo
186 // __wrap_foo becomes __real_foo
187 //
188 // The last part is special in that we don't want to change what references to
189 // __wrap_foo point to, we just want have __real_foo in the symbol table.
190
191 // First make a copy of __real_foo
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000192 std::vector<SymbolUnion> Origs;
Rafael Espindola46935082017-10-06 20:09:34 +0000193 for (const auto &P : WrapSymbols)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000194 Origs.emplace_back(*(SymbolUnion *)P.second);
Rafael Espindola46935082017-10-06 20:09:34 +0000195
196 // Replace __real_foo with foo and foo with __wrap_foo
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000197 for (SymbolRenaming &S : Defsyms) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000198 S.Dst->copyFrom(S.Src);
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000199 S.Dst->File = S.Src->File;
200 S.Dst->Binding = S.Binding;
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000201 }
Rafael Espindola46935082017-10-06 20:09:34 +0000202
203 // Hide one of the copies of __wrap_foo, create a new symbol and copy
204 // __real_foo into it.
205 for (unsigned I = 0, N = WrapSymbols.size(); I < N; ++I) {
206 // We now have two copies of __wrap_foo. Drop one.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000207 SymbolBody *Wrap = WrapSymbols[I].first;
Rafael Espindola46935082017-10-06 20:09:34 +0000208 Wrap->IsUsedInRegularObj = false;
209
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000210 auto *Real = (SymbolBody *)&Origs[I];
Rafael Espindola46935082017-10-06 20:09:34 +0000211 // If __real_foo was undefined, we don't want it in the symbol table.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000212 if (!Real->isInCurrentOutput())
Rafael Espindola46935082017-10-06 20:09:34 +0000213 continue;
214
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000215 auto *NewSym = (SymbolBody *)make<SymbolUnion>();
216 memcpy(NewSym, Real, sizeof(SymbolUnion));
Rafael Espindola46935082017-10-06 20:09:34 +0000217 SymVector.push_back(NewSym);
218 }
George Rimar9703ad22017-04-26 10:40:02 +0000219}
220
Peter Collingbournedadcc172016-04-22 18:42:48 +0000221static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
222 if (VA == STV_DEFAULT)
223 return VB;
224 if (VB == STV_DEFAULT)
225 return VA;
226 return std::min(VA, VB);
227}
228
Rui Ueyamab4de5952016-01-08 22:01:33 +0000229// Find an existing symbol or create and insert a new one.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000230std::pair<SymbolBody *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000231 // <name>@@<version> means the symbol is the default version. In that
232 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000233 //
234 // Since this is a hot path, the following string search code is
235 // optimized for speed. StringRef::find(char) is much faster than
236 // StringRef::find(StringRef).
237 size_t Pos = Name.find('@');
238 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000239 Name = Name.take_front(Pos);
240
Justin Lebar3c11e932016-10-18 17:50:36 +0000241 auto P = Symtab.insert(
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000242 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000243 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000244 bool IsNew = P.second;
245
Rui Ueyamae3357902016-07-18 01:35:00 +0000246 if (V.Idx == -1) {
247 IsNew = true;
Konstantin Zhuravlyov3f9b4b72017-10-16 18:49:28 +0000248 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000249 }
250
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000251 SymbolBody *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000252 if (IsNew) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000253 Sym = (SymbolBody *)make<SymbolUnion>();
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000254 Sym->InVersionScript = false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000255 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000256 Sym->Visibility = STV_DEFAULT;
257 Sym->IsUsedInRegularObj = false;
258 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000259 Sym->CanInline = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000260 Sym->Traced = V.Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000261 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000262 SymVector.push_back(Sym);
263 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000264 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000265 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000266 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000267}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000268
Peter Collingbourne4f952702016-05-01 04:55:03 +0000269// Find an existing symbol or create and insert a new one, then apply the given
270// attributes.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000271std::pair<SymbolBody *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
272 uint8_t Visibility,
273 bool CanOmitFromDynSym,
274 InputFile *File) {
275 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000276 bool WasInserted;
277 std::tie(S, WasInserted) = insert(Name);
278
279 // Merge in the new symbol's visibility.
280 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000281
Peter Collingbourne4f952702016-05-01 04:55:03 +0000282 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
283 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000284
Rui Ueyamaac647252017-10-29 16:46:39 +0000285 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000286 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000287
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000288 if (!WasInserted && S->Type != SymbolBody::UnknownType &&
289 ((Type == STT_TLS) != S->isTls())) {
290 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
291 toString(S->File) + "\n>>> defined in " + toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000292 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000293
294 return {S, WasInserted};
295}
296
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000297template <class ELFT> SymbolBody *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindola244ef982017-07-26 18:42:48 +0000298 return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
299 /*Type*/ 0,
300 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000301}
302
Rui Ueyamae50e8072016-12-22 05:11:12 +0000303static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
304
Peter Collingbourne4f952702016-05-01 04:55:03 +0000305template <class ELFT>
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000306SymbolBody *SymbolTable::addUndefined(StringRef Name, bool IsLocal,
307 uint8_t Binding, uint8_t StOther,
308 uint8_t Type, bool CanOmitFromDynSym,
309 InputFile *File) {
310 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000311 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000312 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000313 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000314 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
315 // An undefined symbol with non default visibility must be satisfied
316 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000317 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000318 S->Binding = Binding;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000319 replaceBody<Undefined>(S, File, Name, IsLocal, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000320 return S;
321 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000322 if (Binding != STB_WEAK) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000323 if (!S->isInCurrentOutput())
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000324 S->Binding = Binding;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000325 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rafael Espindola6e93d052017-08-04 22:31:42 +0000326 SS->getFile<ELFT>()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000327 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000328 if (auto *L = dyn_cast<Lazy>(S)) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000329 // An undefined weak will not fetch archive members. See comment on Lazy in
330 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000331 if (S->isWeak())
332 L->Type = Type;
Rui Ueyama55518e72016-10-28 20:57:25 +0000333 else if (InputFile *F = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000334 addFile<ELFT>(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000335 }
336 return S;
337}
338
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000339// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
340// foo@@VER. We want to effectively ignore foo, so give precedence to
341// foo@@VER.
342// FIXME: If users can transition to using
343// .symver foo,foo@@@VER
344// we can delete this hack.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000345static int compareVersion(SymbolBody *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000346 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000347 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000348 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000349 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000350 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000351 return -1;
352 return 0;
353}
354
Peter Collingbourne4f952702016-05-01 04:55:03 +0000355// We have a new defined symbol with the specified binding. Return 1 if the new
356// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
357// strong defined symbols.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000358static int compareDefined(SymbolBody *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000359 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000360 if (WasInserted)
361 return 1;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000362 if (!S->isInCurrentOutput())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000363 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000364 if (int R = compareVersion(S, Name))
365 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000366 if (Binding == STB_WEAK)
367 return -1;
368 if (S->isWeak())
369 return 1;
370 return 0;
371}
372
373// We have a new non-common defined symbol with the specified binding. Return 1
374// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
375// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000376static int compareDefinedNonCommon(SymbolBody *S, bool WasInserted,
377 uint8_t Binding, bool IsAbsolute,
378 uint64_t Value, StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000379 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000380 if (Cmp > 0)
381 S->Binding = Binding;
382 return Cmp;
383 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000384 if (isa<DefinedCommon>(S)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000385 // Non-common symbols take precedence over common symbols.
386 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000387 warn("common " + S->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000388 return 1;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000389 }
390 if (auto *R = dyn_cast<DefinedRegular>(S)) {
Rafael Espindola858c0922016-12-02 02:58:21 +0000391 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
392 R->Value == Value)
393 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000394 }
395 return 0;
396}
397
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000398SymbolBody *SymbolTable::addCommon(StringRef N, uint64_t Size,
399 uint32_t Alignment, uint8_t Binding,
400 uint8_t StOther, uint8_t Type,
401 InputFile *File) {
402 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000403 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000404 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
405 /*CanOmitFromDynSym*/ false, File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000406 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000407 if (Cmp > 0) {
408 S->Binding = Binding;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000409 replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 } else if (Cmp == 0) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000411 auto *C = dyn_cast<DefinedCommon>(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000412 if (!C) {
413 // Non-common symbols take precedence over common symbols.
414 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000415 warn("common " + S->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000416 return S;
417 }
418
419 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000420 warn("multiple common of " + S->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000421
Rafael Espindola8db87292016-08-31 13:42:08 +0000422 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
423 if (Size > C->Size)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000424 replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000425 }
426 return S;
427}
428
Rui Ueyama810ce102017-03-31 23:40:21 +0000429static void warnOrError(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000430 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000431 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000432 else
433 error(Msg);
434}
435
Rui Ueyama810ce102017-03-31 23:40:21 +0000436static void reportDuplicate(SymbolBody *Sym, InputFile *NewFile) {
George Rimar67c60722017-07-18 11:55:35 +0000437 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
Rafael Espindola6e93d052017-08-04 22:31:42 +0000438 toString(Sym->getFile()) + "\n>>> defined in " +
439 toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000440}
441
442template <class ELFT>
Rui Ueyama810ce102017-03-31 23:40:21 +0000443static void reportDuplicate(SymbolBody *Sym, InputSectionBase *ErrSec,
Eugene Leviant825e5382016-11-08 16:26:32 +0000444 typename ELFT::uint ErrOffset) {
Rui Ueyama810ce102017-03-31 23:40:21 +0000445 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym);
Eugene Leviant825e5382016-11-08 16:26:32 +0000446 if (!D || !D->Section || !ErrSec) {
Rafael Espindolacb83c8c2017-07-25 23:23:40 +0000447 reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr);
Eugene Leviant825e5382016-11-08 16:26:32 +0000448 return;
449 }
450
Rui Ueyama810ce102017-03-31 23:40:21 +0000451 // Construct and print an error message in the form of:
452 //
453 // ld.lld: error: duplicate symbol: foo
454 // >>> defined at bar.c:30
455 // >>> bar.o (/home/alice/src/bar.o)
456 // >>> defined at baz.c:563
457 // >>> baz.o in archive libbaz.a
458 auto *Sec1 = cast<InputSectionBase>(D->Section);
George Rimar82f0c422017-11-01 07:42:38 +0000459 std::string Src1 = Sec1->getSrcMsg<ELFT>(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000460 std::string Obj1 = Sec1->getObjMsg(D->Value);
George Rimar82f0c422017-11-01 07:42:38 +0000461 std::string Src2 = ErrSec->getSrcMsg<ELFT>(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000462 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000463
Rui Ueyama810ce102017-03-31 23:40:21 +0000464 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
465 if (!Src1.empty())
466 Msg += Src1 + "\n>>> ";
467 Msg += Obj1 + "\n>>> defined at ";
468 if (!Src2.empty())
469 Msg += Src2 + "\n>>> ";
470 Msg += Obj2;
471 warnOrError(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000472}
473
Peter Collingbourne4f952702016-05-01 04:55:03 +0000474template <typename ELFT>
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000475SymbolBody *SymbolTable::addRegular(StringRef Name, uint8_t StOther,
476 uint8_t Type, uint64_t Value, uint64_t Size,
477 uint8_t Binding, SectionBase *Section,
478 InputFile *File) {
479 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000480 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000481 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000482 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000483 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
484 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000485 if (Cmp > 0)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000486 replaceBody<DefinedRegular>(S, File, Name, /*IsLocal=*/false, StOther, Type,
487 Value, Size, Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000488 else if (Cmp == 0)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000489 reportDuplicate<ELFT>(S, dyn_cast_or_null<InputSectionBase>(Section),
490 Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000491 return S;
492}
493
494template <typename ELFT>
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000495void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> *File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000496 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000497 const typename ELFT::Verdef *Verdef) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000498 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
499 // as the visibility, which will leave the visibility in the symbol table
500 // unchanged.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000501 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000502 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000503 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
504 /*CanOmitFromDynSym*/ true, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000505 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000506 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000507 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000508
Rafael Espindola8465d0832017-04-04 20:03:34 +0000509 // An undefined symbol with non default visibility must be satisfied
510 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000511 if (WasInserted || ((S->isUndefined() || S->isLazy()) &&
512 S->getVisibility() == STV_DEFAULT)) {
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000513 replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(),
Rui Ueyamabd730e32017-10-28 22:18:17 +0000514 Sym.st_value, Sym.st_size, Alignment, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000515 if (!S->isWeak())
Rui Ueyama4076fa12017-02-26 23:35:34 +0000516 File->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000517 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000518}
519
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000520SymbolBody *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
521 uint8_t StOther, uint8_t Type,
522 bool CanOmitFromDynSym, BitcodeFile *F) {
523 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000524 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000525 std::tie(S, WasInserted) =
Rui Ueyamae50e8072016-12-22 05:11:12 +0000526 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000527 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
528 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000529 if (Cmp > 0)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000530 replaceBody<DefinedRegular>(S, F, Name, /*IsLocal=*/false, StOther, Type, 0,
531 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000532 else if (Cmp == 0)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000533 reportDuplicate(S, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000534 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000535}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000536
Rafael Espindola244ef982017-07-26 18:42:48 +0000537SymbolBody *SymbolTable::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000538 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000539 if (It == Symtab.end())
540 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000541 SymIndex V = It->second;
542 if (V.Idx == -1)
543 return nullptr;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000544 return SymVector[V.Idx];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000545}
546
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000547void SymbolTable::defsym(SymbolBody *Dst, SymbolBody *Src) {
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000548 // We want to tell LTO not to inline Dst symbol because LTO doesn't
549 // know the final symbol contents after renaming.
550 Dst->CanInline = false;
Rafael Espindolac29b24d2017-10-05 00:35:47 +0000551
552 // Tell LTO not to eliminate this symbol.
553 Src->IsUsedInRegularObj = true;
554
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000555 Defsyms.push_back({Dst, Src, Dst->Binding});
556}
557
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000558template <class ELFT>
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000559SymbolBody *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F,
560 const object::Archive::Symbol Sym) {
561 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000562 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000563 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000564 if (WasInserted) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000565 replaceBody<LazyArchive>(S, F, Sym, SymbolBody::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000566 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000567 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000568 if (!S->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000569 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000570
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000571 // An undefined weak will not fetch archive members. See comment on Lazy in
572 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000573 if (S->isWeak()) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000574 replaceBody<LazyArchive>(S, F, Sym, S->Type);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000575 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000576 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000577 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
578 if (!MBInfo.first.getBuffer().empty())
Rafael Espindola244ef982017-07-26 18:42:48 +0000579 addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000580 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000581}
582
583template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000584void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000585 SymbolBody *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000586 bool WasInserted;
587 std::tie(S, WasInserted) = insert(Name);
588 if (WasInserted) {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000589 replaceBody<LazyObject>(S, &Obj, Name, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000590 return;
591 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000592 if (!S->isUndefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000593 return;
594
595 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000596 if (S->isWeak())
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000597 replaceBody<LazyObject>(S, &Obj, Name, S->Type);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000598 else if (InputFile *F = Obj.fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000599 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000600}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000601
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000602// If we already saw this symbol, force loading its file.
603template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) {
604 if (SymbolBody *B = find(Name)) {
605 // Mark the symbol not to be eliminated by LTO
606 // even if it is a bitcode symbol.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000607 B->IsUsedInRegularObj = true;
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000608 if (auto *L = dyn_cast_or_null<Lazy>(B))
609 if (InputFile *File = L->fetch())
610 addFile<ELFT>(File);
611 }
612}
613
Rui Ueyama93bfee52015-10-13 18:10:33 +0000614// This function takes care of the case in which shared libraries depend on
615// the user program (not the other way, which is usual). Shared libraries
616// may have undefined symbols, expecting that the user program provides
617// the definitions for them. An example is BSD's __progname symbol.
618// We need to put such symbols to the main program's .dynsym so that
619// shared libraries can find them.
620// Except this, we ignore undefined symbols in DSOs.
Rafael Espindola244ef982017-07-26 18:42:48 +0000621template <class ELFT> void SymbolTable::scanShlibUndefined() {
George Rimar696a7f92017-09-19 09:20:54 +0000622 for (InputFile *F : SharedFiles) {
623 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000624 SymbolBody *Sym = find(U);
625 if (!Sym || !Sym->isDefined())
626 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000627 Sym->ExportDynamic = true;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000628
629 // If -dynamic-list is given, the default version is set to
630 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
631 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
632 // specified by -dynamic-list.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000633 Sym->VersionId = VER_NDX_GLOBAL;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000634 }
635 }
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000636}
637
Rui Ueyama82492142016-11-15 18:41:52 +0000638// Initialize DemangledSyms with a map from demangled symbols to symbol
639// objects. Used to handle "extern C++" directive in version scripts.
640//
641// The map will contain all demangled symbols. That can be very large,
642// and in LLD we generally want to avoid do anything for each symbol.
643// Then, why are we doing this? Here's why.
644//
645// Users can use "extern C++ {}" directive to match against demangled
646// C++ symbols. For example, you can write a pattern such as
647// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
648// other than trying to match a pattern against all demangled symbols.
649// So, if "extern C++" feature is used, we need to demangle all known
650// symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000651StringMap<std::vector<SymbolBody *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000652 if (!DemangledSyms) {
653 DemangledSyms.emplace();
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000654 for (SymbolBody *Sym : SymVector) {
655 if (!Sym->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000656 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000657 if (Optional<std::string> S = demangle(Sym->getName()))
658 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000659 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000660 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000661 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000662 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000663 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000664}
665
Rafael Espindola244ef982017-07-26 18:42:48 +0000666std::vector<SymbolBody *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000667 if (Ver.IsExternCpp)
668 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000669 if (SymbolBody *B = find(Ver.Name))
Rui Ueyamabda337a2017-10-27 22:54:16 +0000670 if (B->isInCurrentOutput())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000671 return {B};
672 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000673}
674
Rafael Espindola244ef982017-07-26 18:42:48 +0000675std::vector<SymbolBody *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
Rafael Espindola191390a2016-12-08 16:26:20 +0000676 std::vector<SymbolBody *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000677 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000678
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000679 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000680 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000681 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000682 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000683 return Res;
684 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000685
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000686 for (SymbolBody *Sym : SymVector)
687 if (Sym->isInCurrentOutput() && M.match(Sym->getName()))
688 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000689 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000690}
691
Rui Ueyamaea265042016-09-13 20:51:30 +0000692// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000693// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000694// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000695void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000696 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
697 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
698 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
699 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
700 for (SymbolVersion &Ver : Config->VersionScriptLocals)
701 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
702 for (SymbolVersion &Ver : Config->VersionScriptLocals)
703 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000704}
705
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000706// Handles -dynamic-list.
707void SymbolTable::handleDynamicList() {
708 for (SymbolVersion &Ver : Config->DynamicList) {
709 std::vector<SymbolBody *> Syms;
710 if (Ver.HasWildcard)
711 Syms = findByVersion(Ver);
712 else
713 Syms = findAllByVersion(Ver);
714
715 for (SymbolBody *B : Syms) {
716 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000717 B->ExportDynamic = true;
718 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000719 B->IsPreemptible = true;
720 }
721 }
722}
723
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000724// Set symbol versions to symbols. This function handles patterns
725// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000726void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
727 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000728 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000729 return;
730
731 // Get a list of symbols which we need to assign the version to.
Rui Ueyama86581e42016-12-10 00:34:06 +0000732 std::vector<SymbolBody *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000733 if (Syms.empty()) {
734 if (Config->NoUndefinedVersion)
735 error("version script assignment of '" + VersionName + "' to symbol '" +
736 Ver.Name + "' failed: symbol not defined");
737 return;
738 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000739
740 // Assign the version.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000741 for (SymbolBody *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000742 // Skip symbols containing version info because symbol versions
743 // specified by symbol names take precedence over version scripts.
744 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000745 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000746 continue;
747
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000748 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000749 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000750 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000751 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000752 }
753}
754
Rafael Espindola244ef982017-07-26 18:42:48 +0000755void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000756 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000757 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000758
759 // Exact matching takes precendence over fuzzy matching,
760 // so we set a version to a symbol only if no version has been assigned
761 // to the symbol. This behavior is compatible with GNU.
Rui Ueyama9a189872017-07-11 20:33:04 +0000762 for (SymbolBody *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000763 if (B->VersionId == Config->DefaultSymbolVersion)
764 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000765}
766
Rui Ueyamadad2b882016-09-02 22:15:08 +0000767// This function processes version scripts by updating VersionId
768// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000769void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000770 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000771 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000772 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000773
Rui Ueyamadad2b882016-09-02 22:15:08 +0000774 // Now we have version definitions, so we need to set version ids to symbols.
775 // Each version definition has a glob pattern, and all symbols that match
776 // with the pattern get that version.
777
Rui Ueyamadad2b882016-09-02 22:15:08 +0000778 // First, we assign versions to exact matching symbols,
779 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000780 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000781 for (SymbolVersion &Ver : V.Globals)
782 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000783
Rui Ueyamadad2b882016-09-02 22:15:08 +0000784 // Next, we assign versions to fuzzy matching symbols,
785 // i.e. version definitions containing glob meta-characters.
786 // Note that because the last match takes precedence over previous matches,
787 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000788 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
789 for (SymbolVersion &Ver : V.Globals)
790 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000791
792 // Symbol themselves might know their versions because symbols
793 // can contain versions in the form of <name>@<version>.
794 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000795 for (SymbolBody *Sym : SymVector)
796 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000797}
798
Rafael Espindola244ef982017-07-26 18:42:48 +0000799template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
800template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
801template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
802template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
803
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000804template SymbolBody *SymbolTable::addUndefined<ELF32LE>(StringRef);
805template SymbolBody *SymbolTable::addUndefined<ELF32BE>(StringRef);
806template SymbolBody *SymbolTable::addUndefined<ELF64LE>(StringRef);
807template SymbolBody *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000808
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000809template SymbolBody *SymbolTable::addUndefined<ELF32LE>(StringRef, bool,
810 uint8_t, uint8_t,
811 uint8_t, bool,
812 InputFile *);
813template SymbolBody *SymbolTable::addUndefined<ELF32BE>(StringRef, bool,
814 uint8_t, uint8_t,
815 uint8_t, bool,
816 InputFile *);
817template SymbolBody *SymbolTable::addUndefined<ELF64LE>(StringRef, bool,
818 uint8_t, uint8_t,
819 uint8_t, bool,
820 InputFile *);
821template SymbolBody *SymbolTable::addUndefined<ELF64BE>(StringRef, bool,
822 uint8_t, uint8_t,
823 uint8_t, bool,
824 InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000825
Rafael Espindola244ef982017-07-26 18:42:48 +0000826template void SymbolTable::addSymbolAlias<ELF32LE>(StringRef, StringRef);
827template void SymbolTable::addSymbolAlias<ELF32BE>(StringRef, StringRef);
828template void SymbolTable::addSymbolAlias<ELF64LE>(StringRef, StringRef);
829template void SymbolTable::addSymbolAlias<ELF64BE>(StringRef, StringRef);
830
831template void SymbolTable::addCombinedLTOObject<ELF32LE>();
832template void SymbolTable::addCombinedLTOObject<ELF32BE>();
833template void SymbolTable::addCombinedLTOObject<ELF64LE>();
834template void SymbolTable::addCombinedLTOObject<ELF64BE>();
835
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000836template SymbolBody *
837SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t, uint64_t,
838 uint64_t, uint8_t, SectionBase *, InputFile *);
839template SymbolBody *
840SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t, uint64_t,
841 uint64_t, uint8_t, SectionBase *, InputFile *);
842template SymbolBody *
843SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t, uint64_t,
844 uint64_t, uint8_t, SectionBase *, InputFile *);
845template SymbolBody *
846SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t, uint64_t,
847 uint64_t, uint8_t, SectionBase *, InputFile *);
Rafael Espindola244ef982017-07-26 18:42:48 +0000848
849template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t,
850 uint8_t);
851template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t,
852 uint8_t);
853template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t,
854 uint8_t);
855template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t,
856 uint8_t);
857
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000858template SymbolBody *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000859SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000860 const object::Archive::Symbol);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000861template SymbolBody *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000862SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000863 const object::Archive::Symbol);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000864template SymbolBody *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000865SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000866 const object::Archive::Symbol);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000867template SymbolBody *
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000868SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000869 const object::Archive::Symbol);
870
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000871template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
872template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
873template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
874template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000875
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000876template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000877 const typename ELF32LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000878 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000879 const typename ELF32LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000880template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000881 const typename ELF32BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000882 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000883 const typename ELF32BE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000884template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000885 const typename ELF64LE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000886 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000887 const typename ELF64LE::Verdef *);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +0000888template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *,
Rafael Espindola244ef982017-07-26 18:42:48 +0000889 const typename ELF64BE::Sym &,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000890 uint32_t Alignment,
Rafael Espindola244ef982017-07-26 18:42:48 +0000891 const typename ELF64BE::Verdef *);
892
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000893template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
894template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
895template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
896template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
897
Rafael Espindola244ef982017-07-26 18:42:48 +0000898template void SymbolTable::scanShlibUndefined<ELF32LE>();
899template void SymbolTable::scanShlibUndefined<ELF32BE>();
900template void SymbolTable::scanShlibUndefined<ELF64LE>();
901template void SymbolTable::scanShlibUndefined<ELF64BE>();