blob: 373a894dd604211dbd47c63b540ccf780072c73d [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// Symbol table is a bag of all known symbols. We put all symbols of
Rui Ueyamac9559d92016-01-05 20:47:37 +000011// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000012// a hash table with the logic to resolve symbol name conflicts using
13// the symbol types.
14//
15//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000018#include "Config.h"
Davide Italiano8e1131d2016-06-29 02:46:51 +000019#include "LinkerScript.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "Symbols.h"
Peter Collingbourne6c55a702017-11-06 04:33:58 +000021#include "SyntheticSections.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000022#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000023#include "lld/Common/Memory.h"
Rui Ueyama53fe4692017-11-28 02:15:26 +000024#include "lld/Common/Strings.h"
Rui Ueyamacd236a92016-11-17 19:57:43 +000025#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000028using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000029using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
31using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000032using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000033
Rafael Espindola244ef982017-07-26 18:42:48 +000034SymbolTable *elf::Symtab;
35
Rui Ueyama227cb6b2017-10-15 21:43:09 +000036static InputFile *getFirstElf() {
37 if (!ObjectFiles.empty())
38 return ObjectFiles[0];
39 if (!SharedFiles.empty())
40 return SharedFiles[0];
41 return nullptr;
42}
43
Rui Ueyamac9559d92016-01-05 20:47:37 +000044// All input object files must be for the same architecture
45// (e.g. it does not make sense to link x86 object files with
46// MIPS object files.) This function checks for that error.
Rafael Espindola9f375432017-12-23 00:04:34 +000047static bool isCompatible(InputFile *F) {
48 if (!F->isElf() && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000049 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000050
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000051 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
52 if (Config->EMachine != EM_MIPS)
53 return true;
54 if (isMipsN32Abi(F) == Config->MipsN32Abi)
55 return true;
56 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000057
58 if (!Config->Emulation.empty())
59 error(toString(F) + " is incompatible with " + Config->Emulation);
60 else
Rui Ueyama227cb6b2017-10-15 21:43:09 +000061 error(toString(F) + " is incompatible with " + toString(getFirstElf()));
Rui Ueyama16ba6692016-01-29 19:41:13 +000062 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000063}
64
Rui Ueyamac9559d92016-01-05 20:47:37 +000065// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000066template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rafael Espindola9f375432017-12-23 00:04:34 +000067 if (!isCompatible(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000068 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000069
Michael J. Spencera9424f32016-09-09 22:08:04 +000070 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000071 if (auto *F = dyn_cast<BinaryFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +000072 BinaryFiles.push_back(F);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +000073 F->parse();
Michael J. Spencera9424f32016-09-09 22:08:04 +000074 return;
75 }
76
Rui Ueyama89575742015-12-16 22:59:13 +000077 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000078 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000079 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000080 return;
81 }
Rui Ueyama3d451792015-10-12 18:03:21 +000082
George Rimar2a78fce2016-04-13 18:07:57 +000083 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000084 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Rumeet Dhindsad366e362018-05-02 21:40:07 +000085 LazyObjFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +000086 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000087 return;
88 }
89
90 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000091 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000092
Rui Ueyama89575742015-12-16 22:59:13 +000093 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000094 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000095 // DSOs are uniquified not by filename but by soname.
96 F->parseSoName();
Bob Haarmanb8a59c82017-10-25 22:28:38 +000097 if (errorCount() || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000098 return;
George Rimar696a7f92017-09-19 09:20:54 +000099 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +0000100 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +0000101 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000102 }
Rui Ueyama89575742015-12-16 22:59:13 +0000103
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000104 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000105 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000106 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000107 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000108 return;
109 }
110
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000111 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000112 ObjectFiles.push_back(File);
113 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114}
115
Rui Ueyama42554752016-04-23 00:26:32 +0000116// This function is where all the optimizations of link-time
117// optimization happens. When LTO is in use, some input files are
118// not in native object file format but in the LLVM bitcode format.
119// This function compiles bitcode files into a few big native files
120// using LLVM functions and replaces bitcode symbols with the results.
Shoaib Meenaic1ca8062018-01-08 23:18:16 +0000121// Because all bitcode files that the program consists of are passed
Rui Ueyama42554752016-04-23 00:26:32 +0000122// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000123template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000124 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000125 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000126
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000127 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000128 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000129 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000130 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000131
Davide Italiano3bfa0812016-11-26 05:37:04 +0000132 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000133 DenseSet<CachedHashStringRef> DummyGroups;
Rafael Espindolac8f774b2018-03-28 22:45:39 +0000134 auto *Obj = cast<ObjFile<ELFT>>(File);
135 Obj->parse(DummyGroups);
136 for (Symbol *Sym : Obj->getGlobalSymbols())
137 Sym->parseSymbolVersion();
George Rimar696a7f92017-09-19 09:20:54 +0000138 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000139 }
140}
141
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000142Defined *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
143 uint8_t Binding) {
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000144 Symbol *Sym =
145 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, nullptr, nullptr);
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000146 return cast<Defined>(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000147}
148
Rui Ueyama69c778c2016-07-17 17:50:09 +0000149// Set a flag for --trace-symbol so that we can print out a log message
150// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000151void SymbolTable::trace(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000152 SymMap.insert({CachedHashStringRef(Name), -1});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000153}
154
Rui Ueyamadeb15402016-01-07 17:20:07 +0000155// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
156// Used to implement --wrap.
Rafael Espindola244ef982017-07-26 18:42:48 +0000157template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000158 Symbol *Sym = find(Name);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000159 if (!Sym)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000160 return;
George Rimarc9c0ccc2018-06-22 11:18:11 +0000161
162 // Do not wrap the same symbol twice.
163 if (llvm::find_if(WrappedSymbols, [&](const WrappedSymbol &S) {
164 return S.Sym == Sym;
165 }) != WrappedSymbols.end())
166 return;
167
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000168 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
169 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rafael Espindola99f9e132017-11-11 01:59:47 +0000170 WrappedSymbols.push_back({Sym, Real, Wrap});
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000171
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000172 // We want to tell LTO not to inline symbols to be overwritten
173 // because LTO doesn't know the final symbol contents after renaming.
174 Real->CanInline = false;
175 Sym->CanInline = false;
Rafael Espindola46935082017-10-06 20:09:34 +0000176
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000177 // Tell LTO not to eliminate these symbols.
178 Sym->IsUsedInRegularObj = true;
179 Wrap->IsUsedInRegularObj = true;
Rui Ueyamadeb15402016-01-07 17:20:07 +0000180}
181
Rui Ueyama45b81402017-11-04 22:32:56 +0000182// Apply symbol renames created by -wrap. The renames are created
183// before LTO in addSymbolWrap() to have a chance to inform LTO (if
184// LTO is running) not to include these symbols in IPO. Now that the
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000185// symbols are finalized, we can perform the replacement.
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000186void SymbolTable::applySymbolWrap() {
Rafael Espindola46935082017-10-06 20:09:34 +0000187 // This function rotates 3 symbols:
188 //
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000189 // __real_sym becomes sym
190 // sym becomes __wrap_sym
191 // __wrap_sym becomes __real_sym
Rafael Espindola46935082017-10-06 20:09:34 +0000192 //
193 // The last part is special in that we don't want to change what references to
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000194 // __wrap_sym point to, we just want have __real_sym in the symbol table.
Rafael Espindola46935082017-10-06 20:09:34 +0000195
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000196 for (WrappedSymbol &W : WrappedSymbols) {
197 // First, make a copy of __real_sym.
198 Symbol *Real = nullptr;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000199 if (W.Real->isDefined()) {
Rui Ueyamaaf7242a2018-02-13 18:11:42 +0000200 Real = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000201 memcpy(Real, W.Real, sizeof(SymbolUnion));
202 }
Rafael Espindola46935082017-10-06 20:09:34 +0000203
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000204 // Replace __real_sym with sym and sym with __wrap_sym.
Rafael Espindolab262cbe2017-11-29 00:31:39 +0000205 memcpy(W.Real, W.Sym, sizeof(SymbolUnion));
206 memcpy(W.Sym, W.Wrap, sizeof(SymbolUnion));
Rafael Espindola46935082017-10-06 20:09:34 +0000207
Rafael Espindola5c226612017-11-11 00:53:52 +0000208 // We now have two copies of __wrap_sym. Drop one.
209 W.Wrap->IsUsedInRegularObj = false;
210
211 if (Real)
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000212 SymVector.push_back(Real);
Rafael Espindola46935082017-10-06 20:09:34 +0000213 }
George Rimar9703ad22017-04-26 10:40:02 +0000214}
215
Peter Collingbournedadcc172016-04-22 18:42:48 +0000216static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
217 if (VA == STV_DEFAULT)
218 return VB;
219 if (VB == STV_DEFAULT)
220 return VA;
221 return std::min(VA, VB);
222}
223
Rui Ueyamab4de5952016-01-08 22:01:33 +0000224// Find an existing symbol or create and insert a new one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000225std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000226 // <name>@@<version> means the symbol is the default version. In that
227 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000228 //
229 // Since this is a hot path, the following string search code is
230 // optimized for speed. StringRef::find(char) is much faster than
231 // StringRef::find(StringRef).
232 size_t Pos = Name.find('@');
233 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000234 Name = Name.take_front(Pos);
235
Sam Clegga80d94d2017-11-27 23:16:06 +0000236 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000237 int &SymIndex = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000238 bool IsNew = P.second;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000239 bool Traced = false;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000240
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000241 if (SymIndex == -1) {
242 SymIndex = SymVector.size();
243 IsNew = Traced = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000244 }
245
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000246 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000247 if (IsNew) {
Rui Ueyamaaf7242a2018-02-13 18:11:42 +0000248 Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000249 Sym->Visibility = STV_DEFAULT;
250 Sym->IsUsedInRegularObj = false;
251 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000252 Sym->CanInline = true;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000253 Sym->Traced = Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000254 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000255 SymVector.push_back(Sym);
256 } else {
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000257 Sym = SymVector[SymIndex];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000258 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000259 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000260}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000261
Peter Collingbourne4f952702016-05-01 04:55:03 +0000262// Find an existing symbol or create and insert a new one, then apply the given
263// attributes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000264std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
265 uint8_t Visibility,
266 bool CanOmitFromDynSym,
267 InputFile *File) {
268 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000269 bool WasInserted;
270 std::tie(S, WasInserted) = insert(Name);
271
272 // Merge in the new symbol's visibility.
273 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000274
Peter Collingbourne4f952702016-05-01 04:55:03 +0000275 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
276 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000277
Rui Ueyamaac647252017-10-29 16:46:39 +0000278 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000279 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000280
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000281 if (!WasInserted && S->Type != Symbol::UnknownType &&
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000282 ((Type == STT_TLS) != S->isTls())) {
283 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
284 toString(S->File) + "\n>>> defined in " + toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000285 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000286
287 return {S, WasInserted};
288}
289
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000290template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000291 return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
Rafael Espindola244ef982017-07-26 18:42:48 +0000292 /*Type*/ 0,
293 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000294}
295
Rui Ueyamae50e8072016-12-22 05:11:12 +0000296static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
297
Peter Collingbourne4f952702016-05-01 04:55:03 +0000298template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +0000299Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000300 uint8_t StOther, uint8_t Type,
301 bool CanOmitFromDynSym, InputFile *File) {
302 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000303 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000304 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000305 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000306 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
Rui Ueyamad35b8392018-04-03 22:39:04 +0000307
Rafael Espindola8465d0832017-04-04 20:03:34 +0000308 // An undefined symbol with non default visibility must be satisfied
309 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000310 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000311 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000312 return S;
313 }
Rui Ueyamad35b8392018-04-03 22:39:04 +0000314
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000315 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
316 S->Binding = Binding;
Rui Ueyamad35b8392018-04-03 22:39:04 +0000317
318 if (!Config->GcSections && Binding != STB_WEAK)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000319 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rui Ueyamad35b8392018-04-03 22:39:04 +0000320 SS->getFile<ELFT>().IsNeeded = true;
321
George Rimar1ef746b2018-04-03 17:16:52 +0000322 if (S->isLazy()) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000323 // An undefined weak will not fetch archive members. See comment on Lazy in
324 // Symbols.h for the details.
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000325 if (Binding == STB_WEAK) {
George Rimar1ef746b2018-04-03 17:16:52 +0000326 S->Type = Type;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000327 return S;
328 }
329
Fangrui Songc638db52018-05-10 23:53:05 +0000330 // Do extra check for --warn-backrefs.
331 //
332 // --warn-backrefs is an option to prevent an undefined reference from
333 // fetching an archive member written earlier in the command line. It can be
334 // used to keep compatibility with GNU linkers to some degree.
335 // I'll explain the feature and why you may find it useful in this comment.
336 //
337 // lld's symbol resolution semantics is more relaxed than traditional Unix
338 // linkers. For example,
339 //
340 // ld.lld foo.a bar.o
341 //
342 // succeeds even if bar.o contains an undefined symbol that has to be
343 // resolved by some object file in foo.a. Traditional Unix linkers don't
344 // allow this kind of backward reference, as they visit each file only once
345 // from left to right in the command line while resolving all undefined
346 // symbols at the moment of visiting.
347 //
348 // In the above case, since there's no undefined symbol when a linker visits
349 // foo.a, no files are pulled out from foo.a, and because the linker forgets
350 // about foo.a after visiting, it can't resolve undefined symbols in bar.o
351 // that could have been resolved otherwise.
352 //
353 // That lld accepts more relaxed form means that (besides it'd make more
354 // sense) you can accidentally write a command line or a build file that
355 // works only with lld, even if you have a plan to distribute it to wider
356 // users who may be using GNU linkers. With --warn-backrefs, you can detect
357 // a library order that doesn't work with other Unix linkers.
358 //
359 // The option is also useful to detect cyclic dependencies between static
360 // archives. Again, lld accepts
361 //
362 // ld.lld foo.a bar.a
363 //
364 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
365 // handled as an error.
366 //
367 // Here is how the option works. We assign a group ID to each file. A file
368 // with a smaller group ID can pull out object files from an archive file
369 // with an equal or greater group ID. Otherwise, it is a reverse dependency
370 // and an error.
371 //
372 // A file outside --{start,end}-group gets a fresh ID when instantiated. All
373 // files within the same --{start,end}-group get the same group ID. E.g.
374 //
375 // ld.lld A B --start-group C D --end-group E
376 //
377 // A forms group 0. B form group 1. C and D (including their member object
378 // files) form group 2. E forms group 3. I think that you can see how this
379 // group assignment rule simulates the traditional linker's semantics.
380 bool Backref =
381 Config->WarnBackrefs && File && S->File->GroupId < File->GroupId;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000382 fetchLazy<ELFT>(S);
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000383
Fangrui Songc638db52018-05-10 23:53:05 +0000384 // We don't report backward references to weak symbols as they can be
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000385 // overridden later.
Fangrui Songc638db52018-05-10 23:53:05 +0000386 if (Backref && S->Binding != STB_WEAK)
387 warn("backward reference detected: " + Name + " in " + toString(File) +
388 " refers to " + toString(S->File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000389 }
390 return S;
391}
392
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000393// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
394// foo@@VER. We want to effectively ignore foo, so give precedence to
395// foo@@VER.
396// FIXME: If users can transition to using
397// .symver foo,foo@@@VER
398// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000399static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000400 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000401 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000402 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000403 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000404 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000405 return -1;
406 return 0;
407}
408
Peter Collingbourne4f952702016-05-01 04:55:03 +0000409// We have a new defined symbol with the specified binding. Return 1 if the new
410// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
411// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000412static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000413 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000414 if (WasInserted)
415 return 1;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000416 if (!S->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000417 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000418 if (int R = compareVersion(S, Name))
419 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000420 if (Binding == STB_WEAK)
421 return -1;
422 if (S->isWeak())
423 return 1;
424 return 0;
425}
426
427// We have a new non-common defined symbol with the specified binding. Return 1
428// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
429// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000430static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
431 bool IsAbsolute, uint64_t Value,
432 StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000433 if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000434 return Cmp;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000435 if (auto *R = dyn_cast<Defined>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000436 if (R->Section && isa<BssSection>(R->Section)) {
437 // Non-common symbols take precedence over common symbols.
438 if (Config->WarnCommon)
439 warn("common " + S->getName() + " is overridden");
440 return 1;
441 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000442 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
443 R->Value == Value)
444 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000445 }
446 return 0;
447}
448
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000449Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
450 uint8_t Binding, uint8_t StOther, uint8_t Type,
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000451 InputFile &File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000452 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000453 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000454 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000455 /*CanOmitFromDynSym*/ false, &File);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000456
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000457 int Cmp = compareDefined(S, WasInserted, Binding, N);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000458 if (Cmp < 0)
459 return S;
460
Peter Collingbourne4f952702016-05-01 04:55:03 +0000461 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000462 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000463 Bss->File = &File;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000464 Bss->Live = !Config->GcSections;
465 InputSections.push_back(Bss);
466
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000467 replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000468 return S;
469 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000470
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000471 auto *D = cast<Defined>(S);
472 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
473 if (!Bss) {
474 // Non-common symbols take precedence over common symbols.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000475 if (Config->WarnCommon)
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000476 warn("common " + S->getName() + " is overridden");
477 return S;
478 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000479
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000480 if (Config->WarnCommon)
481 warn("multiple common of " + D->getName());
482
483 Bss->Alignment = std::max(Bss->Alignment, Alignment);
484 if (Size > Bss->Size) {
485 D->File = Bss->File = &File;
486 D->Size = Bss->Size = Size;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000487 }
488 return S;
489}
490
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000491static void reportDuplicate(Symbol *Sym, InputFile *NewFile) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000492 if (!Config->AllowMultipleDefinition)
493 error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
494 toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000495}
496
George Rimarfd5a33d2018-01-31 08:32:35 +0000497static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
498 InputSectionBase *ErrSec, uint64_t ErrOffset) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000499 if (Config->AllowMultipleDefinition)
500 return;
501
Rafael Espindolab2ee25a2017-11-30 18:02:04 +0000502 Defined *D = cast<Defined>(Sym);
503 if (!D->Section || !ErrSec) {
George Rimarfd5a33d2018-01-31 08:32:35 +0000504 reportDuplicate(Sym, NewFile);
Eugene Leviant825e5382016-11-08 16:26:32 +0000505 return;
506 }
507
Rui Ueyama810ce102017-03-31 23:40:21 +0000508 // Construct and print an error message in the form of:
509 //
510 // ld.lld: error: duplicate symbol: foo
511 // >>> defined at bar.c:30
512 // >>> bar.o (/home/alice/src/bar.o)
513 // >>> defined at baz.c:563
514 // >>> baz.o in archive libbaz.a
515 auto *Sec1 = cast<InputSectionBase>(D->Section);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000516 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000517 std::string Obj1 = Sec1->getObjMsg(D->Value);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000518 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000519 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000520
Rui Ueyama810ce102017-03-31 23:40:21 +0000521 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
522 if (!Src1.empty())
523 Msg += Src1 + "\n>>> ";
524 Msg += Obj1 + "\n>>> defined at ";
525 if (!Src2.empty())
526 Msg += Src2 + "\n>>> ";
527 Msg += Obj2;
Rui Ueyama048a6692018-03-19 23:04:04 +0000528 error(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000529}
530
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000531Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
532 uint64_t Value, uint64_t Size, uint8_t Binding,
533 SectionBase *Section, InputFile *File) {
534 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000535 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000536 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000537 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000538 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
539 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000540 if (Cmp > 0)
Rafael Espindolabec37652017-11-17 01:37:50 +0000541 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
542 Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000543 else if (Cmp == 0)
George Rimarfd5a33d2018-01-31 08:32:35 +0000544 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section),
545 Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000546 return S;
547}
548
549template <typename ELFT>
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000550void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000551 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000552 uint32_t VerdefIndex) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000553 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
554 // as the visibility, which will leave the visibility in the symbol table
555 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000556 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000557 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000558 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000559 /*CanOmitFromDynSym*/ true, &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000560 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000561 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000562 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000563
Rafael Espindola8465d0832017-04-04 20:03:34 +0000564 // An undefined symbol with non default visibility must be satisfied
565 // in the same DSO.
Rafael Espindola7e6aeb62018-01-16 19:02:46 +0000566 if (WasInserted ||
567 ((S->isUndefined() || S->isLazy()) && S->Visibility == STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000568 uint8_t Binding = S->Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000569 bool WasUndefined = S->isUndefined();
Rui Ueyamafbe68a32017-12-15 00:01:33 +0000570 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000571 Sym.getType(), Sym.st_value, Sym.st_size,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000572 Alignment, VerdefIndex);
Rafael Espindolabec37652017-11-17 01:37:50 +0000573 if (!WasInserted) {
574 S->Binding = Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000575 if (!S->isWeak() && !Config->GcSections && WasUndefined)
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000576 File.IsNeeded = true;
Rafael Espindolabec37652017-11-17 01:37:50 +0000577 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000578 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000579}
580
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000581Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
582 uint8_t StOther, uint8_t Type,
Rafael Espindolaf1687122017-12-20 16:16:40 +0000583 bool CanOmitFromDynSym, BitcodeFile &F) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000584 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000585 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000586 std::tie(S, WasInserted) =
Rafael Espindolaf1687122017-12-20 16:16:40 +0000587 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000588 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
589 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000590 if (Cmp > 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000591 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000592 else if (Cmp == 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000593 reportDuplicate(S, &F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000594 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000595}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000596
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000597Symbol *SymbolTable::find(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000598 auto It = SymMap.find(CachedHashStringRef(Name));
599 if (It == SymMap.end())
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000600 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000601 if (It->second == -1)
Rui Ueyamae3357902016-07-18 01:35:00 +0000602 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000603 return SymVector[It->second];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000604}
605
George Rimar19f9b812018-04-24 09:41:56 +0000606// This is used to handle lazy symbols. May replace existent
607// symbol with lazy version or request to Fetch it.
608template <class ELFT, typename LazyT, typename... ArgT>
Benjamin Kramer54550382018-05-15 22:01:54 +0000609static void replaceOrFetchLazy(StringRef Name, InputFile &File,
610 llvm::function_ref<InputFile *()> Fetch,
611 ArgT &&... Arg) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000612 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000613 bool WasInserted;
George Rimar19f9b812018-04-24 09:41:56 +0000614 std::tie(S, WasInserted) = Symtab->insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000615 if (WasInserted) {
George Rimar19f9b812018-04-24 09:41:56 +0000616 replaceSymbol<LazyT>(S, File, Symbol::UnknownType,
617 std::forward<ArgT>(Arg)...);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000618 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000619 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000620 if (!S->isUndefined())
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000621 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000622
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000623 // An undefined weak will not fetch archive members. See comment on Lazy in
624 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000625 if (S->isWeak()) {
George Rimar19f9b812018-04-24 09:41:56 +0000626 replaceSymbol<LazyT>(S, File, S->Type, std::forward<ArgT>(Arg)...);
Rafael Espindolabec37652017-11-17 01:37:50 +0000627 S->Binding = STB_WEAK;
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000628 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000629 }
George Rimar19f9b812018-04-24 09:41:56 +0000630
631 if (InputFile *F = Fetch())
632 Symtab->addFile<ELFT>(F);
633}
634
635template <class ELFT>
636void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F,
637 const object::Archive::Symbol Sym) {
638 replaceOrFetchLazy<ELFT, LazyArchive>(Name, F, [&]() { return F.fetch(Sym); },
639 Sym);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000640}
641
642template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000643void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
George Rimar19f9b812018-04-24 09:41:56 +0000644 replaceOrFetchLazy<ELFT, LazyObject>(Name, Obj, [&]() { return Obj.fetch(); },
645 Name);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000646}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000647
Rui Ueyamacc013f62018-04-03 18:01:18 +0000648template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
649 if (auto *S = dyn_cast<LazyArchive>(Sym)) {
650 if (InputFile *File = S->fetch())
651 addFile<ELFT>(File);
652 return;
653 }
654
655 auto *S = cast<LazyObject>(Sym);
656 if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
657 addFile<ELFT>(File);
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000658}
659
Rui Ueyama82492142016-11-15 18:41:52 +0000660// Initialize DemangledSyms with a map from demangled symbols to symbol
661// objects. Used to handle "extern C++" directive in version scripts.
662//
663// The map will contain all demangled symbols. That can be very large,
664// and in LLD we generally want to avoid do anything for each symbol.
665// Then, why are we doing this? Here's why.
666//
667// Users can use "extern C++ {}" directive to match against demangled
668// C++ symbols. For example, you can write a pattern such as
669// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
670// other than trying to match a pattern against all demangled symbols.
671// So, if "extern C++" feature is used, we need to demangle all known
672// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000673StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000674 if (!DemangledSyms) {
675 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000676 for (Symbol *Sym : SymVector) {
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000677 if (!Sym->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000678 continue;
Rui Ueyama53fe4692017-11-28 02:15:26 +0000679 if (Optional<std::string> S = demangleItanium(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000680 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000681 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000682 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000683 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000684 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000685 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000686}
687
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000688std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000689 if (Ver.IsExternCpp)
690 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000691 if (Symbol *B = find(Ver.Name))
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000692 if (B->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000693 return {B};
694 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000695}
696
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000697std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
698 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000699 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000700
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000701 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000702 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000703 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000704 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000705 return Res;
706 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000707
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000708 for (Symbol *Sym : SymVector)
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000709 if (Sym->isDefined() && M.match(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000710 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000711 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000712}
713
Rui Ueyamaea265042016-09-13 20:51:30 +0000714// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000715// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000716// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000717void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000718 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
719 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
720 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
721 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
722 for (SymbolVersion &Ver : Config->VersionScriptLocals)
723 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
724 for (SymbolVersion &Ver : Config->VersionScriptLocals)
725 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000726}
727
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000728// Handles -dynamic-list.
729void SymbolTable::handleDynamicList() {
730 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000731 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000732 if (Ver.HasWildcard)
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000733 Syms = findAllByVersion(Ver);
Evgeniy Stepanov9ac31542017-12-06 00:14:04 +0000734 else
735 Syms = findByVersion(Ver);
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000736
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000737 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000738 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000739 B->ExportDynamic = true;
740 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000741 B->IsPreemptible = true;
742 }
743 }
744}
745
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000746// Set symbol versions to symbols. This function handles patterns
747// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000748void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
749 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000750 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000751 return;
752
753 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000754 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000755 if (Syms.empty()) {
Rui Ueyamaaad2e322018-02-02 21:44:06 +0000756 if (!Config->UndefinedVersion)
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000757 error("version script assignment of '" + VersionName + "' to symbol '" +
758 Ver.Name + "' failed: symbol not defined");
759 return;
760 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000761
762 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000763 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000764 // Skip symbols containing version info because symbol versions
765 // specified by symbol names take precedence over version scripts.
766 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000767 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000768 continue;
769
Rafael Espindoladdb33c02018-03-06 17:05:12 +0000770 if (Sym->VersionId != Config->DefaultSymbolVersion &&
771 Sym->VersionId != VersionId)
772 error("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000773 Sym->VersionId = VersionId;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000774 }
775}
776
Rafael Espindola244ef982017-07-26 18:42:48 +0000777void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000778 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000779 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000780
781 // Exact matching takes precendence over fuzzy matching,
782 // so we set a version to a symbol only if no version has been assigned
783 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000784 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000785 if (B->VersionId == Config->DefaultSymbolVersion)
786 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000787}
788
Rui Ueyamadad2b882016-09-02 22:15:08 +0000789// This function processes version scripts by updating VersionId
790// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000791void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000792 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000793 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000794 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000795
Rui Ueyamadad2b882016-09-02 22:15:08 +0000796 // Now we have version definitions, so we need to set version ids to symbols.
797 // Each version definition has a glob pattern, and all symbols that match
798 // with the pattern get that version.
799
Rui Ueyamadad2b882016-09-02 22:15:08 +0000800 // First, we assign versions to exact matching symbols,
801 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000802 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000803 for (SymbolVersion &Ver : V.Globals)
804 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000805
Rui Ueyamadad2b882016-09-02 22:15:08 +0000806 // Next, we assign versions to fuzzy matching symbols,
807 // i.e. version definitions containing glob meta-characters.
808 // Note that because the last match takes precedence over previous matches,
809 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000810 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
811 for (SymbolVersion &Ver : V.Globals)
812 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000813
814 // Symbol themselves might know their versions because symbols
815 // can contain versions in the form of <name>@<version>.
816 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000817 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000818 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000819}
820
Easwaran Ramanbfa48a12018-01-09 05:35:29 +0000821template void SymbolTable::addFile<ELF32LE>(InputFile *);
822template void SymbolTable::addFile<ELF32BE>(InputFile *);
823template void SymbolTable::addFile<ELF64LE>(InputFile *);
824template void SymbolTable::addFile<ELF64BE>(InputFile *);
825
Rafael Espindola244ef982017-07-26 18:42:48 +0000826template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
827template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
828template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
829template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
830
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000831template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
832template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
833template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
834template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000835
Rafael Espindolabec37652017-11-17 01:37:50 +0000836template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
837 uint8_t, bool, InputFile *);
838template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
839 uint8_t, bool, InputFile *);
840template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
841 uint8_t, bool, InputFile *);
842template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
843 uint8_t, bool, InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000844
Rafael Espindola244ef982017-07-26 18:42:48 +0000845template void SymbolTable::addCombinedLTOObject<ELF32LE>();
846template void SymbolTable::addCombinedLTOObject<ELF32BE>();
847template void SymbolTable::addCombinedLTOObject<ELF64LE>();
848template void SymbolTable::addCombinedLTOObject<ELF64BE>();
849
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000850template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000851SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000852 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000853template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000854SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000855 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000856template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000857SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000858 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000859template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000860SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000861 const object::Archive::Symbol);
862
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000863template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
864template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
865template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
866template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000867
Rui Ueyama61b67ab2018-04-03 18:59:31 +0000868template void SymbolTable::fetchLazy<ELF32LE>(Symbol *);
869template void SymbolTable::fetchLazy<ELF32BE>(Symbol *);
870template void SymbolTable::fetchLazy<ELF64LE>(Symbol *);
871template void SymbolTable::fetchLazy<ELF64BE>(Symbol *);
872
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000873template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000874 const typename ELF32LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000875 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000876template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000877 const typename ELF32BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000878 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000879template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000880 const typename ELF64LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000881 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000882template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000883 const typename ELF64BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000884 uint32_t Alignment, uint32_t);