blob: e18810c2a010487f20ecaf5db85daefb3dfbf0b2 [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;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000161 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
162 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rafael Espindola99f9e132017-11-11 01:59:47 +0000163 WrappedSymbols.push_back({Sym, Real, Wrap});
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000164
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000165 // We want to tell LTO not to inline symbols to be overwritten
166 // because LTO doesn't know the final symbol contents after renaming.
167 Real->CanInline = false;
168 Sym->CanInline = false;
Rafael Espindola46935082017-10-06 20:09:34 +0000169
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000170 // Tell LTO not to eliminate these symbols.
171 Sym->IsUsedInRegularObj = true;
172 Wrap->IsUsedInRegularObj = true;
Rui Ueyamadeb15402016-01-07 17:20:07 +0000173}
174
Rui Ueyama45b81402017-11-04 22:32:56 +0000175// Apply symbol renames created by -wrap. The renames are created
176// before LTO in addSymbolWrap() to have a chance to inform LTO (if
177// LTO is running) not to include these symbols in IPO. Now that the
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000178// symbols are finalized, we can perform the replacement.
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000179void SymbolTable::applySymbolWrap() {
Rafael Espindola46935082017-10-06 20:09:34 +0000180 // This function rotates 3 symbols:
181 //
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000182 // __real_sym becomes sym
183 // sym becomes __wrap_sym
184 // __wrap_sym becomes __real_sym
Rafael Espindola46935082017-10-06 20:09:34 +0000185 //
186 // The last part is special in that we don't want to change what references to
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000187 // __wrap_sym point to, we just want have __real_sym in the symbol table.
Rafael Espindola46935082017-10-06 20:09:34 +0000188
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000189 for (WrappedSymbol &W : WrappedSymbols) {
190 // First, make a copy of __real_sym.
191 Symbol *Real = nullptr;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000192 if (W.Real->isDefined()) {
Rui Ueyamaaf7242a2018-02-13 18:11:42 +0000193 Real = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000194 memcpy(Real, W.Real, sizeof(SymbolUnion));
195 }
Rafael Espindola46935082017-10-06 20:09:34 +0000196
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000197 // Replace __real_sym with sym and sym with __wrap_sym.
Rafael Espindolab262cbe2017-11-29 00:31:39 +0000198 memcpy(W.Real, W.Sym, sizeof(SymbolUnion));
199 memcpy(W.Sym, W.Wrap, sizeof(SymbolUnion));
Rafael Espindola46935082017-10-06 20:09:34 +0000200
Rafael Espindola5c226612017-11-11 00:53:52 +0000201 // We now have two copies of __wrap_sym. Drop one.
202 W.Wrap->IsUsedInRegularObj = false;
203
204 if (Real)
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000205 SymVector.push_back(Real);
Rafael Espindola46935082017-10-06 20:09:34 +0000206 }
George Rimar9703ad22017-04-26 10:40:02 +0000207}
208
Peter Collingbournedadcc172016-04-22 18:42:48 +0000209static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
210 if (VA == STV_DEFAULT)
211 return VB;
212 if (VB == STV_DEFAULT)
213 return VA;
214 return std::min(VA, VB);
215}
216
Rui Ueyamab4de5952016-01-08 22:01:33 +0000217// Find an existing symbol or create and insert a new one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000218std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000219 // <name>@@<version> means the symbol is the default version. In that
220 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000221 //
222 // Since this is a hot path, the following string search code is
223 // optimized for speed. StringRef::find(char) is much faster than
224 // StringRef::find(StringRef).
225 size_t Pos = Name.find('@');
226 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000227 Name = Name.take_front(Pos);
228
Sam Clegga80d94d2017-11-27 23:16:06 +0000229 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000230 int &SymIndex = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000231 bool IsNew = P.second;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000232 bool Traced = false;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000233
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000234 if (SymIndex == -1) {
235 SymIndex = SymVector.size();
236 IsNew = Traced = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000237 }
238
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000239 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000240 if (IsNew) {
Rui Ueyamaaf7242a2018-02-13 18:11:42 +0000241 Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000242 Sym->Visibility = STV_DEFAULT;
243 Sym->IsUsedInRegularObj = false;
244 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000245 Sym->CanInline = true;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000246 Sym->Traced = Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000247 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000248 SymVector.push_back(Sym);
249 } else {
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000250 Sym = SymVector[SymIndex];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000251 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000252 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000253}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000254
Peter Collingbourne4f952702016-05-01 04:55:03 +0000255// Find an existing symbol or create and insert a new one, then apply the given
256// attributes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000257std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
258 uint8_t Visibility,
259 bool CanOmitFromDynSym,
260 InputFile *File) {
261 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000262 bool WasInserted;
263 std::tie(S, WasInserted) = insert(Name);
264
265 // Merge in the new symbol's visibility.
266 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000267
Peter Collingbourne4f952702016-05-01 04:55:03 +0000268 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
269 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000270
Rui Ueyamaac647252017-10-29 16:46:39 +0000271 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000272 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000273
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000274 if (!WasInserted && S->Type != Symbol::UnknownType &&
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000275 ((Type == STT_TLS) != S->isTls())) {
276 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
277 toString(S->File) + "\n>>> defined in " + toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000278 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000279
280 return {S, WasInserted};
281}
282
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000283template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000284 return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
Rafael Espindola244ef982017-07-26 18:42:48 +0000285 /*Type*/ 0,
286 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000287}
288
Rui Ueyamae50e8072016-12-22 05:11:12 +0000289static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
290
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000291// Do extra check for --warn-backrefs.
292//
293// --warn-backrefs is an option to prevent an undefined reference from
294// fetching an archive member written earlier in the command line. It can be
295// used to keep your program compatible with GNU linkers after you switch to
296// lld. I'll explain the feature and why you may find it useful in this
297// comment.
298//
299// lld's symbol resolution semantics is more relaxed than traditional Unix
300// linkers. For example,
301//
302// ld.lld foo.a bar.o
303//
304// succeeds even if bar.o contains an undefined symbol that have to be
305// resolved by some object file in foo.a. Traditional Unix linkers don't
306// allow this kind of backward reference, as they visit each file only once
307// from left to right in the command line while resolving all undefined
308// symbols at the moment of visiting.
309//
310// In the above case, since there's no undefined symbol when a linker visits
311// foo.a, no files are pulled out from foo.a, and because the linker forgets
312// about foo.a after visiting, it can't resolve undefined symbols in bar.o
313// that could have been resolved otherwise.
314//
315// That lld accepts more relaxed form means that (besides it'd make more
316// sense) you can accidentally write a command line or a build file that
317// works only with lld, even if you have a plan to distribute it to wider
318// users who may be using GNU linkers. With --warn-backrefs, you can detect
319// a library order that doesn't work with other Unix linkers.
320//
321// The option is also useful to detect cyclic dependencies between static
322// archives. Again, lld accepts
323//
324// ld.lld foo.a bar.a
325//
326// even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
327// handled as an error.
328//
329// Here is how the option works. We assign a group ID to each file. A file
330// with a smaller group ID can pull out object files from an archive file
331// with an equal or greater group ID. Otherwise, it is a reverse dependency
332// and an error.
333//
334// A file outside --{start,end}-group gets a fresh ID when instantiated. All
335// files within the same --{start,end}-group get the same group ID. E.g.
336//
337// ld.lld A B --start-group C D --end-group E
338//
339// A forms group 0. B form group 1. C and D (including their member object
340// files) form group 2. E forms group 3. I think that you can see how this
341// group assignment rule simulates the traditional linker's semantics.
342static void checkBackrefs(StringRef Name, InputFile *Old, InputFile *New) {
Fangrui Songc8ac0a62018-04-20 22:50:15 +0000343 if (Config->WarnBackrefs && New && Old->GroupId < New->GroupId)
344 warn("backward reference detected: " + Name + " in " + toString(New) +
345 " refers to " + toString(Old));
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000346}
347
Peter Collingbourne4f952702016-05-01 04:55:03 +0000348template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +0000349Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000350 uint8_t StOther, uint8_t Type,
351 bool CanOmitFromDynSym, InputFile *File) {
352 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000353 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000354 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000355 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000356 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
Rui Ueyamad35b8392018-04-03 22:39:04 +0000357
Rafael Espindola8465d0832017-04-04 20:03:34 +0000358 // An undefined symbol with non default visibility must be satisfied
359 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000360 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000361 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000362 return S;
363 }
Rui Ueyamad35b8392018-04-03 22:39:04 +0000364
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000365 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
366 S->Binding = Binding;
Rui Ueyamad35b8392018-04-03 22:39:04 +0000367
368 if (!Config->GcSections && Binding != STB_WEAK)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000369 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rui Ueyamad35b8392018-04-03 22:39:04 +0000370 SS->getFile<ELFT>().IsNeeded = true;
371
George Rimar1ef746b2018-04-03 17:16:52 +0000372 if (S->isLazy()) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000373 // An undefined weak will not fetch archive members. See comment on Lazy in
374 // Symbols.h for the details.
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000375 if (Binding == STB_WEAK) {
George Rimar1ef746b2018-04-03 17:16:52 +0000376 S->Type = Type;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000377 return S;
378 }
379
Fangrui Songc8ac0a62018-04-20 22:50:15 +0000380 checkBackrefs(Name, S->File, File);
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000381 fetchLazy<ELFT>(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000382 }
383 return S;
384}
385
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000386// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
387// foo@@VER. We want to effectively ignore foo, so give precedence to
388// foo@@VER.
389// FIXME: If users can transition to using
390// .symver foo,foo@@@VER
391// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000392static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000393 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000394 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000395 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000396 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000397 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000398 return -1;
399 return 0;
400}
401
Peter Collingbourne4f952702016-05-01 04:55:03 +0000402// We have a new defined symbol with the specified binding. Return 1 if the new
403// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
404// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000405static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000406 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000407 if (WasInserted)
408 return 1;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000409 if (!S->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000411 if (int R = compareVersion(S, Name))
412 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000413 if (Binding == STB_WEAK)
414 return -1;
415 if (S->isWeak())
416 return 1;
417 return 0;
418}
419
420// We have a new non-common defined symbol with the specified binding. Return 1
421// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
422// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000423static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
424 bool IsAbsolute, uint64_t Value,
425 StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000426 if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000427 return Cmp;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000428 if (auto *R = dyn_cast<Defined>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000429 if (R->Section && isa<BssSection>(R->Section)) {
430 // Non-common symbols take precedence over common symbols.
431 if (Config->WarnCommon)
432 warn("common " + S->getName() + " is overridden");
433 return 1;
434 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000435 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
436 R->Value == Value)
437 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000438 }
439 return 0;
440}
441
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000442Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
443 uint8_t Binding, uint8_t StOther, uint8_t Type,
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000444 InputFile &File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000445 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000446 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000447 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000448 /*CanOmitFromDynSym*/ false, &File);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000449
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000450 int Cmp = compareDefined(S, WasInserted, Binding, N);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000451 if (Cmp < 0)
452 return S;
453
Peter Collingbourne4f952702016-05-01 04:55:03 +0000454 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000455 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000456 Bss->File = &File;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000457 Bss->Live = !Config->GcSections;
458 InputSections.push_back(Bss);
459
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000460 replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000461 return S;
462 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000463
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000464 auto *D = cast<Defined>(S);
465 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
466 if (!Bss) {
467 // Non-common symbols take precedence over common symbols.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000468 if (Config->WarnCommon)
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000469 warn("common " + S->getName() + " is overridden");
470 return S;
471 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000472
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000473 if (Config->WarnCommon)
474 warn("multiple common of " + D->getName());
475
476 Bss->Alignment = std::max(Bss->Alignment, Alignment);
477 if (Size > Bss->Size) {
478 D->File = Bss->File = &File;
479 D->Size = Bss->Size = Size;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000480 }
481 return S;
482}
483
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000484static void reportDuplicate(Symbol *Sym, InputFile *NewFile) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000485 if (!Config->AllowMultipleDefinition)
486 error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
487 toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000488}
489
George Rimarfd5a33d2018-01-31 08:32:35 +0000490static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
491 InputSectionBase *ErrSec, uint64_t ErrOffset) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000492 if (Config->AllowMultipleDefinition)
493 return;
494
Rafael Espindolab2ee25a2017-11-30 18:02:04 +0000495 Defined *D = cast<Defined>(Sym);
496 if (!D->Section || !ErrSec) {
George Rimarfd5a33d2018-01-31 08:32:35 +0000497 reportDuplicate(Sym, NewFile);
Eugene Leviant825e5382016-11-08 16:26:32 +0000498 return;
499 }
500
Rui Ueyama810ce102017-03-31 23:40:21 +0000501 // Construct and print an error message in the form of:
502 //
503 // ld.lld: error: duplicate symbol: foo
504 // >>> defined at bar.c:30
505 // >>> bar.o (/home/alice/src/bar.o)
506 // >>> defined at baz.c:563
507 // >>> baz.o in archive libbaz.a
508 auto *Sec1 = cast<InputSectionBase>(D->Section);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000509 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000510 std::string Obj1 = Sec1->getObjMsg(D->Value);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000511 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000512 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000513
Rui Ueyama810ce102017-03-31 23:40:21 +0000514 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
515 if (!Src1.empty())
516 Msg += Src1 + "\n>>> ";
517 Msg += Obj1 + "\n>>> defined at ";
518 if (!Src2.empty())
519 Msg += Src2 + "\n>>> ";
520 Msg += Obj2;
Rui Ueyama048a6692018-03-19 23:04:04 +0000521 error(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000522}
523
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000524Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
525 uint64_t Value, uint64_t Size, uint8_t Binding,
526 SectionBase *Section, InputFile *File) {
527 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000528 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000529 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000530 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000531 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
532 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000533 if (Cmp > 0)
Rafael Espindolabec37652017-11-17 01:37:50 +0000534 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
535 Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 else if (Cmp == 0)
George Rimarfd5a33d2018-01-31 08:32:35 +0000537 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section),
538 Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000539 return S;
540}
541
542template <typename ELFT>
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000543void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000544 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000545 uint32_t VerdefIndex) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000546 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
547 // as the visibility, which will leave the visibility in the symbol table
548 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000549 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000550 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000551 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000552 /*CanOmitFromDynSym*/ true, &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000553 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000554 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000555 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000556
Rafael Espindola8465d0832017-04-04 20:03:34 +0000557 // An undefined symbol with non default visibility must be satisfied
558 // in the same DSO.
Rafael Espindola7e6aeb62018-01-16 19:02:46 +0000559 if (WasInserted ||
560 ((S->isUndefined() || S->isLazy()) && S->Visibility == STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000561 uint8_t Binding = S->Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000562 bool WasUndefined = S->isUndefined();
Rui Ueyamafbe68a32017-12-15 00:01:33 +0000563 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000564 Sym.getType(), Sym.st_value, Sym.st_size,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000565 Alignment, VerdefIndex);
Rafael Espindolabec37652017-11-17 01:37:50 +0000566 if (!WasInserted) {
567 S->Binding = Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000568 if (!S->isWeak() && !Config->GcSections && WasUndefined)
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000569 File.IsNeeded = true;
Rafael Espindolabec37652017-11-17 01:37:50 +0000570 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000571 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000572}
573
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000574Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
575 uint8_t StOther, uint8_t Type,
Rafael Espindolaf1687122017-12-20 16:16:40 +0000576 bool CanOmitFromDynSym, BitcodeFile &F) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000577 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000578 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000579 std::tie(S, WasInserted) =
Rafael Espindolaf1687122017-12-20 16:16:40 +0000580 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000581 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
582 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000583 if (Cmp > 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000584 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000585 else if (Cmp == 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000586 reportDuplicate(S, &F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000587 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000588}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000589
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000590Symbol *SymbolTable::find(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000591 auto It = SymMap.find(CachedHashStringRef(Name));
592 if (It == SymMap.end())
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000593 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000594 if (It->second == -1)
Rui Ueyamae3357902016-07-18 01:35:00 +0000595 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000596 return SymVector[It->second];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000597}
598
George Rimar19f9b812018-04-24 09:41:56 +0000599// This is used to handle lazy symbols. May replace existent
600// symbol with lazy version or request to Fetch it.
601template <class ELFT, typename LazyT, typename... ArgT>
602void replaceOrFetchLazy(StringRef Name, InputFile &File,
603 llvm::function_ref<InputFile *()> Fetch,
604 ArgT &&... Arg) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000605 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000606 bool WasInserted;
George Rimar19f9b812018-04-24 09:41:56 +0000607 std::tie(S, WasInserted) = Symtab->insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000608 if (WasInserted) {
George Rimar19f9b812018-04-24 09:41:56 +0000609 replaceSymbol<LazyT>(S, File, Symbol::UnknownType,
610 std::forward<ArgT>(Arg)...);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000611 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000612 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000613 if (!S->isUndefined())
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000614 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000615
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000616 // An undefined weak will not fetch archive members. See comment on Lazy in
617 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000618 if (S->isWeak()) {
George Rimar19f9b812018-04-24 09:41:56 +0000619 replaceSymbol<LazyT>(S, File, S->Type, std::forward<ArgT>(Arg)...);
Rafael Espindolabec37652017-11-17 01:37:50 +0000620 S->Binding = STB_WEAK;
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000621 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000622 }
George Rimar19f9b812018-04-24 09:41:56 +0000623
624 if (InputFile *F = Fetch())
625 Symtab->addFile<ELFT>(F);
626}
627
628template <class ELFT>
629void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F,
630 const object::Archive::Symbol Sym) {
631 replaceOrFetchLazy<ELFT, LazyArchive>(Name, F, [&]() { return F.fetch(Sym); },
632 Sym);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000633}
634
635template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000636void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
George Rimar19f9b812018-04-24 09:41:56 +0000637 replaceOrFetchLazy<ELFT, LazyObject>(Name, Obj, [&]() { return Obj.fetch(); },
638 Name);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000639}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000640
Rui Ueyamacc013f62018-04-03 18:01:18 +0000641template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
642 if (auto *S = dyn_cast<LazyArchive>(Sym)) {
643 if (InputFile *File = S->fetch())
644 addFile<ELFT>(File);
645 return;
646 }
647
648 auto *S = cast<LazyObject>(Sym);
649 if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
650 addFile<ELFT>(File);
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000651}
652
Rui Ueyama82492142016-11-15 18:41:52 +0000653// Initialize DemangledSyms with a map from demangled symbols to symbol
654// objects. Used to handle "extern C++" directive in version scripts.
655//
656// The map will contain all demangled symbols. That can be very large,
657// and in LLD we generally want to avoid do anything for each symbol.
658// Then, why are we doing this? Here's why.
659//
660// Users can use "extern C++ {}" directive to match against demangled
661// C++ symbols. For example, you can write a pattern such as
662// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
663// other than trying to match a pattern against all demangled symbols.
664// So, if "extern C++" feature is used, we need to demangle all known
665// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000666StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000667 if (!DemangledSyms) {
668 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000669 for (Symbol *Sym : SymVector) {
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000670 if (!Sym->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000671 continue;
Rui Ueyama53fe4692017-11-28 02:15:26 +0000672 if (Optional<std::string> S = demangleItanium(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000673 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000674 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000675 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000676 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000677 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000678 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000679}
680
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000681std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000682 if (Ver.IsExternCpp)
683 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000684 if (Symbol *B = find(Ver.Name))
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000685 if (B->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000686 return {B};
687 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000688}
689
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000690std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
691 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000692 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000693
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000694 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000695 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000696 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000697 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000698 return Res;
699 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000700
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000701 for (Symbol *Sym : SymVector)
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000702 if (Sym->isDefined() && M.match(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000703 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000704 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000705}
706
Rui Ueyamaea265042016-09-13 20:51:30 +0000707// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000708// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000709// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000710void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000711 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
712 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
713 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
714 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
715 for (SymbolVersion &Ver : Config->VersionScriptLocals)
716 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
717 for (SymbolVersion &Ver : Config->VersionScriptLocals)
718 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000719}
720
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000721// Handles -dynamic-list.
722void SymbolTable::handleDynamicList() {
723 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000724 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000725 if (Ver.HasWildcard)
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000726 Syms = findAllByVersion(Ver);
Evgeniy Stepanov9ac31542017-12-06 00:14:04 +0000727 else
728 Syms = findByVersion(Ver);
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000729
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000730 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000731 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000732 B->ExportDynamic = true;
733 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000734 B->IsPreemptible = true;
735 }
736 }
737}
738
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000739// Set symbol versions to symbols. This function handles patterns
740// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000741void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
742 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000743 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000744 return;
745
746 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000747 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000748 if (Syms.empty()) {
Rui Ueyamaaad2e322018-02-02 21:44:06 +0000749 if (!Config->UndefinedVersion)
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000750 error("version script assignment of '" + VersionName + "' to symbol '" +
751 Ver.Name + "' failed: symbol not defined");
752 return;
753 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000754
755 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000756 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000757 // Skip symbols containing version info because symbol versions
758 // specified by symbol names take precedence over version scripts.
759 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000760 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000761 continue;
762
Rafael Espindoladdb33c02018-03-06 17:05:12 +0000763 if (Sym->VersionId != Config->DefaultSymbolVersion &&
764 Sym->VersionId != VersionId)
765 error("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000766 Sym->VersionId = VersionId;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000767 }
768}
769
Rafael Espindola244ef982017-07-26 18:42:48 +0000770void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000771 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000772 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000773
774 // Exact matching takes precendence over fuzzy matching,
775 // so we set a version to a symbol only if no version has been assigned
776 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000777 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000778 if (B->VersionId == Config->DefaultSymbolVersion)
779 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000780}
781
Rui Ueyamadad2b882016-09-02 22:15:08 +0000782// This function processes version scripts by updating VersionId
783// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000784void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000785 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000786 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000787 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000788
Rui Ueyamadad2b882016-09-02 22:15:08 +0000789 // Now we have version definitions, so we need to set version ids to symbols.
790 // Each version definition has a glob pattern, and all symbols that match
791 // with the pattern get that version.
792
Rui Ueyamadad2b882016-09-02 22:15:08 +0000793 // First, we assign versions to exact matching symbols,
794 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000795 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000796 for (SymbolVersion &Ver : V.Globals)
797 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000798
Rui Ueyamadad2b882016-09-02 22:15:08 +0000799 // Next, we assign versions to fuzzy matching symbols,
800 // i.e. version definitions containing glob meta-characters.
801 // Note that because the last match takes precedence over previous matches,
802 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000803 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
804 for (SymbolVersion &Ver : V.Globals)
805 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000806
807 // Symbol themselves might know their versions because symbols
808 // can contain versions in the form of <name>@<version>.
809 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000810 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000811 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000812}
813
Easwaran Ramanbfa48a12018-01-09 05:35:29 +0000814template void SymbolTable::addFile<ELF32LE>(InputFile *);
815template void SymbolTable::addFile<ELF32BE>(InputFile *);
816template void SymbolTable::addFile<ELF64LE>(InputFile *);
817template void SymbolTable::addFile<ELF64BE>(InputFile *);
818
Rafael Espindola244ef982017-07-26 18:42:48 +0000819template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
820template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
821template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
822template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
823
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000824template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
825template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
826template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
827template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000828
Rafael Espindolabec37652017-11-17 01:37:50 +0000829template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
830 uint8_t, bool, InputFile *);
831template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
832 uint8_t, bool, InputFile *);
833template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
834 uint8_t, bool, InputFile *);
835template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
836 uint8_t, bool, InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000837
Rafael Espindola244ef982017-07-26 18:42:48 +0000838template void SymbolTable::addCombinedLTOObject<ELF32LE>();
839template void SymbolTable::addCombinedLTOObject<ELF32BE>();
840template void SymbolTable::addCombinedLTOObject<ELF64LE>();
841template void SymbolTable::addCombinedLTOObject<ELF64BE>();
842
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000843template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000844SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000845 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000846template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000847SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000848 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000849template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000850SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000851 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000852template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000853SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000854 const object::Archive::Symbol);
855
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000856template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
857template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
858template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
859template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000860
Rui Ueyama61b67ab2018-04-03 18:59:31 +0000861template void SymbolTable::fetchLazy<ELF32LE>(Symbol *);
862template void SymbolTable::fetchLazy<ELF32BE>(Symbol *);
863template void SymbolTable::fetchLazy<ELF64LE>(Symbol *);
864template void SymbolTable::fetchLazy<ELF64BE>(Symbol *);
865
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000866template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000867 const typename ELF32LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000868 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000869template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000870 const typename ELF32BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000871 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000872template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000873 const typename ELF64LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000874 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000875template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000876 const typename ELF64BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000877 uint32_t Alignment, uint32_t);