blob: 01d43d28972759685999e12c56bc8c4ef5ce8b28 [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)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000085 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000086 return;
87 }
88
89 if (Config->Trace)
Rui Ueyamae6e206d2017-02-21 23:22:56 +000090 message(toString(File));
George Rimar2a78fce2016-04-13 18:07:57 +000091
Rui Ueyama89575742015-12-16 22:59:13 +000092 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000093 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000094 // DSOs are uniquified not by filename but by soname.
95 F->parseSoName();
Bob Haarmanb8a59c82017-10-25 22:28:38 +000096 if (errorCount() || !SoNames.insert(F->SoName).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000097 return;
George Rimar696a7f92017-09-19 09:20:54 +000098 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +000099 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +0000100 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000101 }
Rui Ueyama89575742015-12-16 22:59:13 +0000102
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000103 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000104 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000105 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000106 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000107 return;
108 }
109
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000110 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000111 ObjectFiles.push_back(File);
112 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000113}
114
Rui Ueyama42554752016-04-23 00:26:32 +0000115// This function is where all the optimizations of link-time
116// optimization happens. When LTO is in use, some input files are
117// not in native object file format but in the LLVM bitcode format.
118// This function compiles bitcode files into a few big native files
119// using LLVM functions and replaces bitcode symbols with the results.
Shoaib Meenaic1ca8062018-01-08 23:18:16 +0000120// Because all bitcode files that the program consists of are passed
Rui Ueyama42554752016-04-23 00:26:32 +0000121// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000122template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000123 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000124 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000125
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000126 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000127 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000128 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000129 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000130
Davide Italiano3bfa0812016-11-26 05:37:04 +0000131 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000132 DenseSet<CachedHashStringRef> DummyGroups;
George Rimar696a7f92017-09-19 09:20:54 +0000133 cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
134 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000135 }
136}
137
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000138Defined *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
139 uint8_t Binding) {
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000140 Symbol *Sym =
141 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, nullptr, nullptr);
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000142 return cast<Defined>(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000143}
144
Rui Ueyama69c778c2016-07-17 17:50:09 +0000145// Set a flag for --trace-symbol so that we can print out a log message
146// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000147void SymbolTable::trace(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000148 SymMap.insert({CachedHashStringRef(Name), -1});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000149}
150
Rui Ueyamadeb15402016-01-07 17:20:07 +0000151// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
152// Used to implement --wrap.
Rafael Espindola244ef982017-07-26 18:42:48 +0000153template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000154 Symbol *Sym = find(Name);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000155 if (!Sym)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000156 return;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000157 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
158 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
Rafael Espindola99f9e132017-11-11 01:59:47 +0000159 WrappedSymbols.push_back({Sym, Real, Wrap});
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +0000160
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000161 // We want to tell LTO not to inline symbols to be overwritten
162 // because LTO doesn't know the final symbol contents after renaming.
163 Real->CanInline = false;
164 Sym->CanInline = false;
Rafael Espindola46935082017-10-06 20:09:34 +0000165
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000166 // Tell LTO not to eliminate these symbols.
167 Sym->IsUsedInRegularObj = true;
168 Wrap->IsUsedInRegularObj = true;
Rui Ueyamadeb15402016-01-07 17:20:07 +0000169}
170
Rui Ueyama45b81402017-11-04 22:32:56 +0000171// Apply symbol renames created by -wrap. The renames are created
172// before LTO in addSymbolWrap() to have a chance to inform LTO (if
173// LTO is running) not to include these symbols in IPO. Now that the
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000174// symbols are finalized, we can perform the replacement.
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000175void SymbolTable::applySymbolWrap() {
Rafael Espindola46935082017-10-06 20:09:34 +0000176 // This function rotates 3 symbols:
177 //
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000178 // __real_sym becomes sym
179 // sym becomes __wrap_sym
180 // __wrap_sym becomes __real_sym
Rafael Espindola46935082017-10-06 20:09:34 +0000181 //
182 // The last part is special in that we don't want to change what references to
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000183 // __wrap_sym point to, we just want have __real_sym in the symbol table.
Rafael Espindola46935082017-10-06 20:09:34 +0000184
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000185 for (WrappedSymbol &W : WrappedSymbols) {
186 // First, make a copy of __real_sym.
187 Symbol *Real = nullptr;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000188 if (W.Real->isDefined()) {
Rui Ueyamaaf7242a2018-02-13 18:11:42 +0000189 Real = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000190 memcpy(Real, W.Real, sizeof(SymbolUnion));
191 }
Rafael Espindola46935082017-10-06 20:09:34 +0000192
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000193 // Replace __real_sym with sym and sym with __wrap_sym.
Rafael Espindolab262cbe2017-11-29 00:31:39 +0000194 memcpy(W.Real, W.Sym, sizeof(SymbolUnion));
195 memcpy(W.Sym, W.Wrap, sizeof(SymbolUnion));
Rafael Espindola46935082017-10-06 20:09:34 +0000196
Rafael Espindola5c226612017-11-11 00:53:52 +0000197 // We now have two copies of __wrap_sym. Drop one.
198 W.Wrap->IsUsedInRegularObj = false;
199
200 if (Real)
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000201 SymVector.push_back(Real);
Rafael Espindola46935082017-10-06 20:09:34 +0000202 }
George Rimar9703ad22017-04-26 10:40:02 +0000203}
204
Peter Collingbournedadcc172016-04-22 18:42:48 +0000205static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
206 if (VA == STV_DEFAULT)
207 return VB;
208 if (VB == STV_DEFAULT)
209 return VA;
210 return std::min(VA, VB);
211}
212
Rui Ueyamab4de5952016-01-08 22:01:33 +0000213// Find an existing symbol or create and insert a new one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000214std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000215 // <name>@@<version> means the symbol is the default version. In that
216 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000217 //
218 // Since this is a hot path, the following string search code is
219 // optimized for speed. StringRef::find(char) is much faster than
220 // StringRef::find(StringRef).
221 size_t Pos = Name.find('@');
222 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000223 Name = Name.take_front(Pos);
224
Sam Clegga80d94d2017-11-27 23:16:06 +0000225 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000226 int &SymIndex = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000227 bool IsNew = P.second;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000228 bool Traced = false;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000229
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000230 if (SymIndex == -1) {
231 SymIndex = SymVector.size();
232 IsNew = Traced = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000233 }
234
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000235 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000236 if (IsNew) {
Rui Ueyamaaf7242a2018-02-13 18:11:42 +0000237 Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000238 Sym->InVersionScript = false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000239 Sym->Visibility = STV_DEFAULT;
240 Sym->IsUsedInRegularObj = false;
241 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000242 Sym->CanInline = true;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000243 Sym->Traced = Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000244 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000245 SymVector.push_back(Sym);
246 } else {
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000247 Sym = SymVector[SymIndex];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000248 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000249 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000250}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000251
Peter Collingbourne4f952702016-05-01 04:55:03 +0000252// Find an existing symbol or create and insert a new one, then apply the given
253// attributes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000254std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
255 uint8_t Visibility,
256 bool CanOmitFromDynSym,
257 InputFile *File) {
258 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000259 bool WasInserted;
260 std::tie(S, WasInserted) = insert(Name);
261
262 // Merge in the new symbol's visibility.
263 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000264
Peter Collingbourne4f952702016-05-01 04:55:03 +0000265 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
266 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000267
Rui Ueyamaac647252017-10-29 16:46:39 +0000268 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000269 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000270
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000271 if (!WasInserted && S->Type != Symbol::UnknownType &&
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000272 ((Type == STT_TLS) != S->isTls())) {
273 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
274 toString(S->File) + "\n>>> defined in " + toString(File));
Rui Ueyama810ce102017-03-31 23:40:21 +0000275 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000276
277 return {S, WasInserted};
278}
279
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000280template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000281 return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
Rafael Espindola244ef982017-07-26 18:42:48 +0000282 /*Type*/ 0,
283 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000284}
285
Rui Ueyamae50e8072016-12-22 05:11:12 +0000286static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
287
Peter Collingbourne4f952702016-05-01 04:55:03 +0000288template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +0000289Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000290 uint8_t StOther, uint8_t Type,
291 bool CanOmitFromDynSym, InputFile *File) {
292 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000293 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000294 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000295 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000296 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
297 // An undefined symbol with non default visibility must be satisfied
298 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000299 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000300 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000301 return S;
302 }
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000303 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
304 S->Binding = Binding;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000305 if (Binding != STB_WEAK) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000306 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000307 if (!Config->GcSections)
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000308 SS->getFile<ELFT>().IsNeeded = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000309 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000310 if (auto *L = dyn_cast<Lazy>(S)) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000311 // An undefined weak will not fetch archive members. See comment on Lazy in
312 // Symbols.h for the details.
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000313 if (Binding == STB_WEAK)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000314 L->Type = Type;
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000315 else if (InputFile *F = L->fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000316 addFile<ELFT>(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000317 }
318 return S;
319}
320
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000321// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
322// foo@@VER. We want to effectively ignore foo, so give precedence to
323// foo@@VER.
324// FIXME: If users can transition to using
325// .symver foo,foo@@@VER
326// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000327static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000328 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000329 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000330 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000331 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000332 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000333 return -1;
334 return 0;
335}
336
Peter Collingbourne4f952702016-05-01 04:55:03 +0000337// We have a new defined symbol with the specified binding. Return 1 if the new
338// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
339// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000340static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000341 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000342 if (WasInserted)
343 return 1;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000344 if (!S->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000345 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000346 if (int R = compareVersion(S, Name))
347 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000348 if (Binding == STB_WEAK)
349 return -1;
350 if (S->isWeak())
351 return 1;
352 return 0;
353}
354
355// We have a new non-common defined symbol with the specified binding. Return 1
356// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
357// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000358static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
359 bool IsAbsolute, uint64_t Value,
360 StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000361 if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000362 return Cmp;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000363 if (auto *R = dyn_cast<Defined>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000364 if (R->Section && isa<BssSection>(R->Section)) {
365 // Non-common symbols take precedence over common symbols.
366 if (Config->WarnCommon)
367 warn("common " + S->getName() + " is overridden");
368 return 1;
369 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000370 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
371 R->Value == Value)
372 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000373 }
374 return 0;
375}
376
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000377Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
378 uint8_t Binding, uint8_t StOther, uint8_t Type,
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000379 InputFile &File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000380 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000381 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000382 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000383 /*CanOmitFromDynSym*/ false, &File);
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000384 int Cmp = compareDefined(S, WasInserted, Binding, N);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000385 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000386 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000387 Bss->File = &File;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000388 Bss->Live = !Config->GcSections;
389 InputSections.push_back(Bss);
390
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000391 replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000392 } else if (Cmp == 0) {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000393 auto *D = cast<Defined>(S);
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000394 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
395 if (!Bss) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000396 // Non-common symbols take precedence over common symbols.
397 if (Config->WarnCommon)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000398 warn("common " + S->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000399 return S;
400 }
401
402 if (Config->WarnCommon)
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000403 warn("multiple common of " + D->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000404
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000405 Bss->Alignment = std::max(Bss->Alignment, Alignment);
406 if (Size > Bss->Size) {
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000407 D->File = Bss->File = &File;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000408 D->Size = Bss->Size = Size;
409 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 }
411 return S;
412}
413
Rui Ueyama810ce102017-03-31 23:40:21 +0000414static void warnOrError(const Twine &Msg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000415 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000416 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000417 else
418 error(Msg);
419}
420
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000421static void reportDuplicate(Symbol *Sym, InputFile *NewFile) {
George Rimar67c60722017-07-18 11:55:35 +0000422 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
Rafael Espindoladfebd362017-11-29 22:47:35 +0000423 toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000424}
425
George Rimarfd5a33d2018-01-31 08:32:35 +0000426static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
427 InputSectionBase *ErrSec, uint64_t ErrOffset) {
Rafael Espindolab2ee25a2017-11-30 18:02:04 +0000428 Defined *D = cast<Defined>(Sym);
429 if (!D->Section || !ErrSec) {
George Rimarfd5a33d2018-01-31 08:32:35 +0000430 reportDuplicate(Sym, NewFile);
Eugene Leviant825e5382016-11-08 16:26:32 +0000431 return;
432 }
433
Rui Ueyama810ce102017-03-31 23:40:21 +0000434 // Construct and print an error message in the form of:
435 //
436 // ld.lld: error: duplicate symbol: foo
437 // >>> defined at bar.c:30
438 // >>> bar.o (/home/alice/src/bar.o)
439 // >>> defined at baz.c:563
440 // >>> baz.o in archive libbaz.a
441 auto *Sec1 = cast<InputSectionBase>(D->Section);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000442 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000443 std::string Obj1 = Sec1->getObjMsg(D->Value);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000444 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000445 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000446
Rui Ueyama810ce102017-03-31 23:40:21 +0000447 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
448 if (!Src1.empty())
449 Msg += Src1 + "\n>>> ";
450 Msg += Obj1 + "\n>>> defined at ";
451 if (!Src2.empty())
452 Msg += Src2 + "\n>>> ";
453 Msg += Obj2;
454 warnOrError(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000455}
456
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000457Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
458 uint64_t Value, uint64_t Size, uint8_t Binding,
459 SectionBase *Section, InputFile *File) {
460 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000461 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000462 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000463 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000464 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
465 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000466 if (Cmp > 0)
Rafael Espindolabec37652017-11-17 01:37:50 +0000467 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
468 Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000469 else if (Cmp == 0)
George Rimarfd5a33d2018-01-31 08:32:35 +0000470 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section),
471 Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000472 return S;
473}
474
475template <typename ELFT>
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000476void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000477 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000478 uint32_t VerdefIndex) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000479 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
480 // as the visibility, which will leave the visibility in the symbol table
481 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000482 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000483 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000484 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000485 /*CanOmitFromDynSym*/ true, &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000486 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000487 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000488 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000489
Rafael Espindola8465d0832017-04-04 20:03:34 +0000490 // An undefined symbol with non default visibility must be satisfied
491 // in the same DSO.
Rafael Espindola7e6aeb62018-01-16 19:02:46 +0000492 if (WasInserted ||
493 ((S->isUndefined() || S->isLazy()) && S->Visibility == STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000494 uint8_t Binding = S->Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000495 bool WasUndefined = S->isUndefined();
Rui Ueyamafbe68a32017-12-15 00:01:33 +0000496 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000497 Sym.getType(), Sym.st_value, Sym.st_size,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000498 Alignment, VerdefIndex);
Rafael Espindolabec37652017-11-17 01:37:50 +0000499 if (!WasInserted) {
500 S->Binding = Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000501 if (!S->isWeak() && !Config->GcSections && WasUndefined)
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000502 File.IsNeeded = true;
Rafael Espindolabec37652017-11-17 01:37:50 +0000503 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000504 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000505}
506
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000507Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
508 uint8_t StOther, uint8_t Type,
Rafael Espindolaf1687122017-12-20 16:16:40 +0000509 bool CanOmitFromDynSym, BitcodeFile &F) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000510 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000511 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000512 std::tie(S, WasInserted) =
Rafael Espindolaf1687122017-12-20 16:16:40 +0000513 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000514 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
515 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000516 if (Cmp > 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000517 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000518 else if (Cmp == 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000519 reportDuplicate(S, &F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000520 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000521}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000522
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000523Symbol *SymbolTable::find(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000524 auto It = SymMap.find(CachedHashStringRef(Name));
525 if (It == SymMap.end())
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000526 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000527 if (It->second == -1)
Rui Ueyamae3357902016-07-18 01:35:00 +0000528 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000529 return SymVector[It->second];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000530}
531
Rafael Espindola8a59f5c2017-01-13 19:18:11 +0000532template <class ELFT>
Rafael Espindola8cd66742017-12-20 17:48:28 +0000533Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000534 const object::Archive::Symbol Sym) {
535 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000537 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000538 if (WasInserted) {
Rafael Espindola8276f1b2017-12-20 17:59:43 +0000539 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType);
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000540 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000541 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000542 if (!S->isUndefined())
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000543 return S;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000544
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000545 // An undefined weak will not fetch archive members. See comment on Lazy in
546 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000547 if (S->isWeak()) {
Rafael Espindola8276f1b2017-12-20 17:59:43 +0000548 replaceSymbol<LazyArchive>(S, F, Sym, S->Type);
Rafael Espindolabec37652017-11-17 01:37:50 +0000549 S->Binding = STB_WEAK;
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000550 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000551 }
Rafael Espindola8cd66742017-12-20 17:48:28 +0000552 std::pair<MemoryBufferRef, uint64_t> MBInfo = F.getMember(&Sym);
Davide Italianobcdd6c62016-10-12 19:35:54 +0000553 if (!MBInfo.first.getBuffer().empty())
Rafael Espindola8cd66742017-12-20 17:48:28 +0000554 addFile<ELFT>(createObjectFile(MBInfo.first, F.getName(), MBInfo.second));
Rui Ueyamad1f8b812017-06-21 15:36:24 +0000555 return S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000556}
557
558template <class ELFT>
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000559void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000560 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000561 bool WasInserted;
562 std::tie(S, WasInserted) = insert(Name);
563 if (WasInserted) {
Rafael Espindola2e5c71e2017-12-20 17:52:36 +0000564 replaceSymbol<LazyObject>(S, Obj, Name, Symbol::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000565 return;
566 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000567 if (!S->isUndefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000568 return;
569
570 // See comment for addLazyArchive above.
Rafael Espindola808f2d32017-05-04 14:54:48 +0000571 if (S->isWeak())
Rafael Espindola2e5c71e2017-12-20 17:52:36 +0000572 replaceSymbol<LazyObject>(S, Obj, Name, S->Type);
Rafael Espindola808f2d32017-05-04 14:54:48 +0000573 else if (InputFile *F = Obj.fetch())
Rafael Espindola244ef982017-07-26 18:42:48 +0000574 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000575}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000576
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000577// If we already saw this symbol, force loading its file.
578template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000579 if (Symbol *B = find(Name)) {
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000580 // Mark the symbol not to be eliminated by LTO
581 // even if it is a bitcode symbol.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000582 B->IsUsedInRegularObj = true;
Rafael Espindola092ba582017-12-20 01:57:19 +0000583 if (auto *L = dyn_cast<Lazy>(B))
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000584 if (InputFile *File = L->fetch())
585 addFile<ELFT>(File);
586 }
587}
588
Rui Ueyama93bfee52015-10-13 18:10:33 +0000589// This function takes care of the case in which shared libraries depend on
590// the user program (not the other way, which is usual). Shared libraries
591// may have undefined symbols, expecting that the user program provides
592// the definitions for them. An example is BSD's __progname symbol.
593// We need to put such symbols to the main program's .dynsym so that
594// shared libraries can find them.
595// Except this, we ignore undefined symbols in DSOs.
Rafael Espindola244ef982017-07-26 18:42:48 +0000596template <class ELFT> void SymbolTable::scanShlibUndefined() {
George Rimar696a7f92017-09-19 09:20:54 +0000597 for (InputFile *F : SharedFiles) {
598 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000599 Symbol *Sym = find(U);
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000600 if (!Sym || !Sym->isDefined())
601 continue;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000602 Sym->ExportDynamic = true;
Rui Ueyama321b9cd2017-04-25 00:15:48 +0000603 }
604 }
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000605}
606
Rui Ueyama82492142016-11-15 18:41:52 +0000607// Initialize DemangledSyms with a map from demangled symbols to symbol
608// objects. Used to handle "extern C++" directive in version scripts.
609//
610// The map will contain all demangled symbols. That can be very large,
611// and in LLD we generally want to avoid do anything for each symbol.
612// Then, why are we doing this? Here's why.
613//
614// Users can use "extern C++ {}" directive to match against demangled
615// C++ symbols. For example, you can write a pattern such as
616// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
617// other than trying to match a pattern against all demangled symbols.
618// So, if "extern C++" feature is used, we need to demangle all known
619// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000620StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000621 if (!DemangledSyms) {
622 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000623 for (Symbol *Sym : SymVector) {
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000624 if (!Sym->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000625 continue;
Rui Ueyama53fe4692017-11-28 02:15:26 +0000626 if (Optional<std::string> S = demangleItanium(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000627 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000628 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000629 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000630 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000631 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000632 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000633}
634
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000635std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000636 if (Ver.IsExternCpp)
637 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000638 if (Symbol *B = find(Ver.Name))
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000639 if (B->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000640 return {B};
641 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000642}
643
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000644std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
645 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000646 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000647
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000648 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000649 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000650 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000651 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000652 return Res;
653 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000654
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000655 for (Symbol *Sym : SymVector)
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000656 if (Sym->isDefined() && M.match(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000657 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000658 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000659}
660
Rui Ueyamaea265042016-09-13 20:51:30 +0000661// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000662// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000663// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000664void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000665 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
666 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
667 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
668 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
669 for (SymbolVersion &Ver : Config->VersionScriptLocals)
670 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
671 for (SymbolVersion &Ver : Config->VersionScriptLocals)
672 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000673}
674
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000675// Handles -dynamic-list.
676void SymbolTable::handleDynamicList() {
677 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000678 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000679 if (Ver.HasWildcard)
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000680 Syms = findAllByVersion(Ver);
Evgeniy Stepanov9ac31542017-12-06 00:14:04 +0000681 else
682 Syms = findByVersion(Ver);
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000683
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000684 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000685 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000686 B->ExportDynamic = true;
687 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000688 B->IsPreemptible = true;
689 }
690 }
691}
692
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000693// Set symbol versions to symbols. This function handles patterns
694// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000695void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
696 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000697 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000698 return;
699
700 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000701 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000702 if (Syms.empty()) {
Rui Ueyamaaad2e322018-02-02 21:44:06 +0000703 if (!Config->UndefinedVersion)
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000704 error("version script assignment of '" + VersionName + "' to symbol '" +
705 Ver.Name + "' failed: symbol not defined");
706 return;
707 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000708
709 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000710 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000711 // Skip symbols containing version info because symbol versions
712 // specified by symbol names take precedence over version scripts.
713 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000714 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000715 continue;
716
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000717 if (Sym->InVersionScript)
Rui Ueyamaaade0e22016-11-17 03:32:41 +0000718 warn("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000719 Sym->VersionId = VersionId;
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000720 Sym->InVersionScript = true;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000721 }
722}
723
Rafael Espindola244ef982017-07-26 18:42:48 +0000724void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000725 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000726 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000727
728 // Exact matching takes precendence over fuzzy matching,
729 // so we set a version to a symbol only if no version has been assigned
730 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000731 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000732 if (B->VersionId == Config->DefaultSymbolVersion)
733 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000734}
735
Rui Ueyamadad2b882016-09-02 22:15:08 +0000736// This function processes version scripts by updating VersionId
737// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000738void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000739 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000740 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000741 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000742
Rui Ueyamadad2b882016-09-02 22:15:08 +0000743 // Now we have version definitions, so we need to set version ids to symbols.
744 // Each version definition has a glob pattern, and all symbols that match
745 // with the pattern get that version.
746
Rui Ueyamadad2b882016-09-02 22:15:08 +0000747 // First, we assign versions to exact matching symbols,
748 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000749 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000750 for (SymbolVersion &Ver : V.Globals)
751 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000752
Rui Ueyamadad2b882016-09-02 22:15:08 +0000753 // Next, we assign versions to fuzzy matching symbols,
754 // i.e. version definitions containing glob meta-characters.
755 // Note that because the last match takes precedence over previous matches,
756 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000757 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
758 for (SymbolVersion &Ver : V.Globals)
759 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000760
761 // Symbol themselves might know their versions because symbols
762 // can contain versions in the form of <name>@<version>.
763 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000764 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000765 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000766}
767
Easwaran Ramanbfa48a12018-01-09 05:35:29 +0000768template void SymbolTable::addFile<ELF32LE>(InputFile *);
769template void SymbolTable::addFile<ELF32BE>(InputFile *);
770template void SymbolTable::addFile<ELF64LE>(InputFile *);
771template void SymbolTable::addFile<ELF64BE>(InputFile *);
772
Rafael Espindola244ef982017-07-26 18:42:48 +0000773template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
774template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
775template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
776template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
777
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000778template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
779template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
780template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
781template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000782
Rafael Espindolabec37652017-11-17 01:37:50 +0000783template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
784 uint8_t, bool, InputFile *);
785template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
786 uint8_t, bool, InputFile *);
787template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
788 uint8_t, bool, InputFile *);
789template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
790 uint8_t, bool, InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000791
Rafael Espindola244ef982017-07-26 18:42:48 +0000792template void SymbolTable::addCombinedLTOObject<ELF32LE>();
793template void SymbolTable::addCombinedLTOObject<ELF32BE>();
794template void SymbolTable::addCombinedLTOObject<ELF64LE>();
795template void SymbolTable::addCombinedLTOObject<ELF64BE>();
796
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000797template Symbol *
Rafael Espindola8cd66742017-12-20 17:48:28 +0000798SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000799 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000800template Symbol *
Rafael Espindola8cd66742017-12-20 17:48:28 +0000801SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000802 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000803template Symbol *
Rafael Espindola8cd66742017-12-20 17:48:28 +0000804SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000805 const object::Archive::Symbol);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000806template Symbol *
Rafael Espindola8cd66742017-12-20 17:48:28 +0000807SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000808 const object::Archive::Symbol);
809
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000810template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
811template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
812template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
813template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000814
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000815template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000816 const typename ELF32LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000817 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000818template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000819 const typename ELF32BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000820 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000821template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000822 const typename ELF64LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000823 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000824template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000825 const typename ELF64BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000826 uint32_t Alignment, uint32_t);
Rafael Espindola244ef982017-07-26 18:42:48 +0000827
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000828template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
829template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
830template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
831template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
832
Rafael Espindola244ef982017-07-26 18:42:48 +0000833template void SymbolTable::scanShlibUndefined<ELF32LE>();
834template void SymbolTable::scanShlibUndefined<ELF32BE>();
835template void SymbolTable::scanShlibUndefined<ELF64LE>();
836template void SymbolTable::scanShlibUndefined<ELF64BE>();