blob: 1af8ca62a8f140ae03469f664b74afb33d322141 [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];
George Rimarfd6af6d2018-07-11 12:32:00 +000041 return BitcodeFiles[0];
Rui Ueyama227cb6b2017-10-15 21:43:09 +000042}
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 Ueyama07b45362018-08-22 07:02:26 +0000155void SymbolTable::wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap) {
156 // Swap symbols as instructed by -wrap.
Rui Ueyamafbc62972018-10-09 20:22:18 +0000157 int &Idx1 = SymMap[CachedHashStringRef(Sym->getName())];
158 int &Idx2 = SymMap[CachedHashStringRef(Real->getName())];
159 int &Idx3 = SymMap[CachedHashStringRef(Wrap->getName())];
George Rimarc9c0ccc2018-06-22 11:18:11 +0000160
Rui Ueyama07b45362018-08-22 07:02:26 +0000161 Idx2 = Idx1;
162 Idx1 = Idx3;
George Rimarc9c0ccc2018-06-22 11:18:11 +0000163
Rui Ueyama07b45362018-08-22 07:02:26 +0000164 // Now renaming is complete. No one refers Real symbol. We could leave
165 // Real as-is, but if Real is written to the symbol table, that may
166 // contain irrelevant values. So, we copy all values from Sym to Real.
167 StringRef S = Real->getName();
168 memcpy(Real, Sym, sizeof(SymbolUnion));
169 Real->setName(S);
George Rimar9703ad22017-04-26 10:40:02 +0000170}
171
Peter Collingbournedadcc172016-04-22 18:42:48 +0000172static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
173 if (VA == STV_DEFAULT)
174 return VB;
175 if (VB == STV_DEFAULT)
176 return VA;
177 return std::min(VA, VB);
178}
179
Rui Ueyamab4de5952016-01-08 22:01:33 +0000180// Find an existing symbol or create and insert a new one.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000181std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000182 // <name>@@<version> means the symbol is the default version. In that
183 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000184 //
185 // Since this is a hot path, the following string search code is
186 // optimized for speed. StringRef::find(char) is much faster than
187 // StringRef::find(StringRef).
188 size_t Pos = Name.find('@');
189 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000190 Name = Name.take_front(Pos);
191
Sam Clegga80d94d2017-11-27 23:16:06 +0000192 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000193 int &SymIndex = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000194 bool IsNew = P.second;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000195 bool Traced = false;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000196
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000197 if (SymIndex == -1) {
198 SymIndex = SymVector.size();
199 IsNew = Traced = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000200 }
201
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000202 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000203 if (IsNew) {
Rui Ueyamaaf7242a2018-02-13 18:11:42 +0000204 Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000205 Sym->Visibility = STV_DEFAULT;
206 Sym->IsUsedInRegularObj = false;
207 Sym->ExportDynamic = false;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000208 Sym->CanInline = true;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000209 Sym->Traced = Traced;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000210 Sym->VersionId = Config->DefaultSymbolVersion;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000211 SymVector.push_back(Sym);
212 } else {
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000213 Sym = SymVector[SymIndex];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000214 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000215 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000216}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000217
Peter Collingbourne4f952702016-05-01 04:55:03 +0000218// Find an existing symbol or create and insert a new one, then apply the given
219// attributes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000220std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
221 uint8_t Visibility,
222 bool CanOmitFromDynSym,
223 InputFile *File) {
224 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000225 bool WasInserted;
226 std::tie(S, WasInserted) = insert(Name);
227
228 // Merge in the new symbol's visibility.
229 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000230
Peter Collingbourne4f952702016-05-01 04:55:03 +0000231 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
232 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000233
Rui Ueyamaac647252017-10-29 16:46:39 +0000234 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000235 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000236
Rui Ueyama714abec2018-10-09 20:16:16 +0000237 bool HasTlsAttr = !WasInserted && (!S->isLazy() || S->isTls());
238 if (HasTlsAttr && (Type == STT_TLS) != S->isTls())
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000239 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
240 toString(S->File) + "\n>>> defined in " + toString(File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000241
242 return {S, WasInserted};
243}
244
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000245template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000246 return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT,
Rafael Espindola244ef982017-07-26 18:42:48 +0000247 /*Type*/ 0,
248 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000249}
250
Rui Ueyamae50e8072016-12-22 05:11:12 +0000251static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
252
Peter Collingbourne4f952702016-05-01 04:55:03 +0000253template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +0000254Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000255 uint8_t StOther, uint8_t Type,
256 bool CanOmitFromDynSym, InputFile *File) {
257 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000258 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000259 uint8_t Visibility = getVisibility(StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000260 std::tie(S, WasInserted) =
Rafael Espindola8465d0832017-04-04 20:03:34 +0000261 insert(Name, Type, Visibility, CanOmitFromDynSym, File);
Rui Ueyamad35b8392018-04-03 22:39:04 +0000262
Rafael Espindola8465d0832017-04-04 20:03:34 +0000263 // An undefined symbol with non default visibility must be satisfied
264 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000265 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000266 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000267 return S;
268 }
Rui Ueyamad35b8392018-04-03 22:39:04 +0000269
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000270 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
271 S->Binding = Binding;
Rui Ueyamad35b8392018-04-03 22:39:04 +0000272
273 if (!Config->GcSections && Binding != STB_WEAK)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000274 if (auto *SS = dyn_cast<SharedSymbol>(S))
Rui Ueyamad35b8392018-04-03 22:39:04 +0000275 SS->getFile<ELFT>().IsNeeded = true;
276
George Rimar1ef746b2018-04-03 17:16:52 +0000277 if (S->isLazy()) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000278 // An undefined weak will not fetch archive members. See comment on Lazy in
279 // Symbols.h for the details.
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000280 if (Binding == STB_WEAK) {
George Rimar1ef746b2018-04-03 17:16:52 +0000281 S->Type = Type;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000282 return S;
283 }
284
Fangrui Songc638db52018-05-10 23:53:05 +0000285 // Do extra check for --warn-backrefs.
286 //
287 // --warn-backrefs is an option to prevent an undefined reference from
288 // fetching an archive member written earlier in the command line. It can be
289 // used to keep compatibility with GNU linkers to some degree.
290 // I'll explain the feature and why you may find it useful in this comment.
291 //
292 // lld's symbol resolution semantics is more relaxed than traditional Unix
293 // linkers. For example,
294 //
295 // ld.lld foo.a bar.o
296 //
297 // succeeds even if bar.o contains an undefined symbol that has to be
298 // resolved by some object file in foo.a. Traditional Unix linkers don't
299 // allow this kind of backward reference, as they visit each file only once
300 // from left to right in the command line while resolving all undefined
301 // symbols at the moment of visiting.
302 //
303 // In the above case, since there's no undefined symbol when a linker visits
304 // foo.a, no files are pulled out from foo.a, and because the linker forgets
305 // about foo.a after visiting, it can't resolve undefined symbols in bar.o
306 // that could have been resolved otherwise.
307 //
308 // That lld accepts more relaxed form means that (besides it'd make more
309 // sense) you can accidentally write a command line or a build file that
310 // works only with lld, even if you have a plan to distribute it to wider
311 // users who may be using GNU linkers. With --warn-backrefs, you can detect
312 // a library order that doesn't work with other Unix linkers.
313 //
314 // The option is also useful to detect cyclic dependencies between static
315 // archives. Again, lld accepts
316 //
317 // ld.lld foo.a bar.a
318 //
319 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
320 // handled as an error.
321 //
322 // Here is how the option works. We assign a group ID to each file. A file
323 // with a smaller group ID can pull out object files from an archive file
324 // with an equal or greater group ID. Otherwise, it is a reverse dependency
325 // and an error.
326 //
327 // A file outside --{start,end}-group gets a fresh ID when instantiated. All
328 // files within the same --{start,end}-group get the same group ID. E.g.
329 //
330 // ld.lld A B --start-group C D --end-group E
331 //
332 // A forms group 0. B form group 1. C and D (including their member object
333 // files) form group 2. E forms group 3. I think that you can see how this
334 // group assignment rule simulates the traditional linker's semantics.
335 bool Backref =
336 Config->WarnBackrefs && File && S->File->GroupId < File->GroupId;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000337 fetchLazy<ELFT>(S);
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000338
Fangrui Songc638db52018-05-10 23:53:05 +0000339 // We don't report backward references to weak symbols as they can be
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000340 // overridden later.
Fangrui Songc638db52018-05-10 23:53:05 +0000341 if (Backref && S->Binding != STB_WEAK)
342 warn("backward reference detected: " + Name + " in " + toString(File) +
343 " refers to " + toString(S->File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000344 }
345 return S;
346}
347
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000348// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
349// foo@@VER. We want to effectively ignore foo, so give precedence to
350// foo@@VER.
351// FIXME: If users can transition to using
352// .symver foo,foo@@@VER
353// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000354static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000355 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000356 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000357 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000358 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000359 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000360 return -1;
361 return 0;
362}
363
Peter Collingbourne4f952702016-05-01 04:55:03 +0000364// We have a new defined symbol with the specified binding. Return 1 if the new
365// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
366// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000367static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000368 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000369 if (WasInserted)
370 return 1;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000371 if (!S->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000372 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000373 if (int R = compareVersion(S, Name))
374 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000375 if (Binding == STB_WEAK)
376 return -1;
377 if (S->isWeak())
378 return 1;
379 return 0;
380}
381
382// We have a new non-common defined symbol with the specified binding. Return 1
383// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
384// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000385static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
386 bool IsAbsolute, uint64_t Value,
387 StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000388 if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000389 return Cmp;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000390 if (auto *R = dyn_cast<Defined>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000391 if (R->Section && isa<BssSection>(R->Section)) {
392 // Non-common symbols take precedence over common symbols.
393 if (Config->WarnCommon)
394 warn("common " + S->getName() + " is overridden");
395 return 1;
396 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000397 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
398 R->Value == Value)
399 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000400 }
401 return 0;
402}
403
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000404Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
405 uint8_t Binding, uint8_t StOther, uint8_t Type,
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000406 InputFile &File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000407 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000408 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000409 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000410 /*CanOmitFromDynSym*/ false, &File);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000411
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000412 int Cmp = compareDefined(S, WasInserted, Binding, N);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000413 if (Cmp < 0)
414 return S;
415
Peter Collingbourne4f952702016-05-01 04:55:03 +0000416 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000417 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000418 Bss->File = &File;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000419 Bss->Live = !Config->GcSections;
420 InputSections.push_back(Bss);
421
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000422 replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000423 return S;
424 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000425
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000426 auto *D = cast<Defined>(S);
427 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
428 if (!Bss) {
429 // Non-common symbols take precedence over common symbols.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000430 if (Config->WarnCommon)
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000431 warn("common " + S->getName() + " is overridden");
432 return S;
433 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000434
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000435 if (Config->WarnCommon)
436 warn("multiple common of " + D->getName());
437
438 Bss->Alignment = std::max(Bss->Alignment, Alignment);
439 if (Size > Bss->Size) {
440 D->File = Bss->File = &File;
441 D->Size = Bss->Size = Size;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000442 }
443 return S;
444}
445
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000446static void reportDuplicate(Symbol *Sym, InputFile *NewFile) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000447 if (!Config->AllowMultipleDefinition)
448 error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
449 toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000450}
451
George Rimarfd5a33d2018-01-31 08:32:35 +0000452static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
453 InputSectionBase *ErrSec, uint64_t ErrOffset) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000454 if (Config->AllowMultipleDefinition)
455 return;
456
Rafael Espindolab2ee25a2017-11-30 18:02:04 +0000457 Defined *D = cast<Defined>(Sym);
458 if (!D->Section || !ErrSec) {
George Rimarfd5a33d2018-01-31 08:32:35 +0000459 reportDuplicate(Sym, NewFile);
Eugene Leviant825e5382016-11-08 16:26:32 +0000460 return;
461 }
462
Rui Ueyama810ce102017-03-31 23:40:21 +0000463 // Construct and print an error message in the form of:
464 //
465 // ld.lld: error: duplicate symbol: foo
466 // >>> defined at bar.c:30
467 // >>> bar.o (/home/alice/src/bar.o)
468 // >>> defined at baz.c:563
469 // >>> baz.o in archive libbaz.a
470 auto *Sec1 = cast<InputSectionBase>(D->Section);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000471 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000472 std::string Obj1 = Sec1->getObjMsg(D->Value);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000473 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000474 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000475
Rui Ueyama810ce102017-03-31 23:40:21 +0000476 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
477 if (!Src1.empty())
478 Msg += Src1 + "\n>>> ";
479 Msg += Obj1 + "\n>>> defined at ";
480 if (!Src2.empty())
481 Msg += Src2 + "\n>>> ";
482 Msg += Obj2;
Rui Ueyama048a6692018-03-19 23:04:04 +0000483 error(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000484}
485
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000486Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
487 uint64_t Value, uint64_t Size, uint8_t Binding,
488 SectionBase *Section, InputFile *File) {
489 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000490 bool WasInserted;
Rui Ueyamae50e8072016-12-22 05:11:12 +0000491 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000492 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000493 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
494 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495 if (Cmp > 0)
Rafael Espindolabec37652017-11-17 01:37:50 +0000496 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
497 Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000498 else if (Cmp == 0)
George Rimarfd5a33d2018-01-31 08:32:35 +0000499 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section),
500 Value);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000501 return S;
502}
503
504template <typename ELFT>
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000505void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000506 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000507 uint32_t VerdefIndex) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000508 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
509 // as the visibility, which will leave the visibility in the symbol table
510 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000511 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000512 bool WasInserted;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000513 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000514 /*CanOmitFromDynSym*/ true, &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000516 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000517 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000518
Rafael Espindola8465d0832017-04-04 20:03:34 +0000519 // An undefined symbol with non default visibility must be satisfied
520 // in the same DSO.
Rafael Espindola7e6aeb62018-01-16 19:02:46 +0000521 if (WasInserted ||
522 ((S->isUndefined() || S->isLazy()) && S->Visibility == STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000523 uint8_t Binding = S->Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000524 bool WasUndefined = S->isUndefined();
Rui Ueyamafbe68a32017-12-15 00:01:33 +0000525 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000526 Sym.getType(), Sym.st_value, Sym.st_size,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000527 Alignment, VerdefIndex);
Rafael Espindolabec37652017-11-17 01:37:50 +0000528 if (!WasInserted) {
529 S->Binding = Binding;
Rafael Espindola8e2fc4f32018-01-23 16:59:20 +0000530 if (!S->isWeak() && !Config->GcSections && WasUndefined)
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000531 File.IsNeeded = true;
Rafael Espindolabec37652017-11-17 01:37:50 +0000532 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000533 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000534}
535
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000536Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
537 uint8_t StOther, uint8_t Type,
Rafael Espindolaf1687122017-12-20 16:16:40 +0000538 bool CanOmitFromDynSym, BitcodeFile &F) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000539 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000540 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000541 std::tie(S, WasInserted) =
Rafael Espindolaf1687122017-12-20 16:16:40 +0000542 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000543 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
544 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000545 if (Cmp > 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000546 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000547 else if (Cmp == 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000548 reportDuplicate(S, &F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000549 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000550}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000551
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000552Symbol *SymbolTable::find(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000553 auto It = SymMap.find(CachedHashStringRef(Name));
554 if (It == SymMap.end())
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000555 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000556 if (It->second == -1)
Rui Ueyamae3357902016-07-18 01:35:00 +0000557 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000558 return SymVector[It->second];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000559}
560
Rui Ueyama659cff32018-10-09 19:54:32 +0000561template <class ELFT>
562void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &File,
563 const object::Archive::Symbol Sym) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000564 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000565 bool WasInserted;
Rui Ueyamafbc62972018-10-09 20:22:18 +0000566 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000567 if (WasInserted) {
Rui Ueyama714abec2018-10-09 20:16:16 +0000568 replaceSymbol<LazyArchive>(S, File, STT_NOTYPE, Sym);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000569 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000570 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000571 if (!S->isUndefined())
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000572 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000573
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000574 // An undefined weak will not fetch archive members. See comment on Lazy in
575 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000576 if (S->isWeak()) {
Rui Ueyama659cff32018-10-09 19:54:32 +0000577 replaceSymbol<LazyArchive>(S, File, S->Type, Sym);
Rafael Espindolabec37652017-11-17 01:37:50 +0000578 S->Binding = STB_WEAK;
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000579 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000580 }
George Rimar19f9b812018-04-24 09:41:56 +0000581
Rui Ueyama659cff32018-10-09 19:54:32 +0000582 if (InputFile *F = File.fetch(Sym))
Rui Ueyamafbc62972018-10-09 20:22:18 +0000583 addFile<ELFT>(F);
George Rimar19f9b812018-04-24 09:41:56 +0000584}
585
586template <class ELFT>
Rui Ueyama659cff32018-10-09 19:54:32 +0000587void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &File) {
588 Symbol *S;
589 bool WasInserted;
Rui Ueyamafbc62972018-10-09 20:22:18 +0000590 std::tie(S, WasInserted) = insert(Name);
Rui Ueyama659cff32018-10-09 19:54:32 +0000591 if (WasInserted) {
Rui Ueyama714abec2018-10-09 20:16:16 +0000592 replaceSymbol<LazyObject>(S, File, STT_NOTYPE, Name);
Rui Ueyama659cff32018-10-09 19:54:32 +0000593 return;
594 }
595 if (!S->isUndefined())
596 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000597
Rui Ueyama659cff32018-10-09 19:54:32 +0000598 // An undefined weak will not fetch archive members. See comment on Lazy in
599 // Symbols.h for the details.
600 if (S->isWeak()) {
601 replaceSymbol<LazyObject>(S, File, S->Type, Name);
602 S->Binding = STB_WEAK;
603 return;
604 }
605
606 if (InputFile *F = File.fetch())
Rui Ueyamafbc62972018-10-09 20:22:18 +0000607 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000608}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000609
Rui Ueyamacc013f62018-04-03 18:01:18 +0000610template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
611 if (auto *S = dyn_cast<LazyArchive>(Sym)) {
612 if (InputFile *File = S->fetch())
613 addFile<ELFT>(File);
614 return;
615 }
616
617 auto *S = cast<LazyObject>(Sym);
618 if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
619 addFile<ELFT>(File);
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000620}
621
Rui Ueyama82492142016-11-15 18:41:52 +0000622// Initialize DemangledSyms with a map from demangled symbols to symbol
623// objects. Used to handle "extern C++" directive in version scripts.
624//
625// The map will contain all demangled symbols. That can be very large,
626// and in LLD we generally want to avoid do anything for each symbol.
627// Then, why are we doing this? Here's why.
628//
629// Users can use "extern C++ {}" directive to match against demangled
630// C++ symbols. For example, you can write a pattern such as
631// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
632// other than trying to match a pattern against all demangled symbols.
633// So, if "extern C++" feature is used, we need to demangle all known
634// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000635StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000636 if (!DemangledSyms) {
637 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000638 for (Symbol *Sym : SymVector) {
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000639 if (!Sym->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000640 continue;
Rui Ueyama53fe4692017-11-28 02:15:26 +0000641 if (Optional<std::string> S = demangleItanium(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000642 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000643 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000644 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000645 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000646 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000647 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000648}
649
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000650std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000651 if (Ver.IsExternCpp)
652 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000653 if (Symbol *B = find(Ver.Name))
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000654 if (B->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000655 return {B};
656 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000657}
658
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000659std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
660 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000661 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000662
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000663 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000664 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000665 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000666 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000667 return Res;
668 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000669
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000670 for (Symbol *Sym : SymVector)
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000671 if (Sym->isDefined() && M.match(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000672 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000673 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000674}
675
Rui Ueyamaea265042016-09-13 20:51:30 +0000676// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000677// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000678// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000679void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000680 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
681 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
682 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
683 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
684 for (SymbolVersion &Ver : Config->VersionScriptLocals)
685 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
686 for (SymbolVersion &Ver : Config->VersionScriptLocals)
687 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000688}
689
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000690// Handles -dynamic-list.
691void SymbolTable::handleDynamicList() {
692 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000693 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000694 if (Ver.HasWildcard)
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000695 Syms = findAllByVersion(Ver);
Evgeniy Stepanov9ac31542017-12-06 00:14:04 +0000696 else
697 Syms = findByVersion(Ver);
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000698
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000699 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000700 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000701 B->ExportDynamic = true;
702 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000703 B->IsPreemptible = true;
704 }
705 }
706}
707
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000708// Set symbol versions to symbols. This function handles patterns
709// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000710void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
711 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000712 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000713 return;
714
715 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000716 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000717 if (Syms.empty()) {
Rui Ueyamaaad2e322018-02-02 21:44:06 +0000718 if (!Config->UndefinedVersion)
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000719 error("version script assignment of '" + VersionName + "' to symbol '" +
720 Ver.Name + "' failed: symbol not defined");
721 return;
722 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000723
724 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000725 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000726 // Skip symbols containing version info because symbol versions
727 // specified by symbol names take precedence over version scripts.
728 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000729 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000730 continue;
731
Rafael Espindoladdb33c02018-03-06 17:05:12 +0000732 if (Sym->VersionId != Config->DefaultSymbolVersion &&
733 Sym->VersionId != VersionId)
734 error("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000735 Sym->VersionId = VersionId;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000736 }
737}
738
Rafael Espindola244ef982017-07-26 18:42:48 +0000739void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000740 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000741 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000742
743 // Exact matching takes precendence over fuzzy matching,
744 // so we set a version to a symbol only if no version has been assigned
745 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000746 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000747 if (B->VersionId == Config->DefaultSymbolVersion)
748 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000749}
750
Rui Ueyamadad2b882016-09-02 22:15:08 +0000751// This function processes version scripts by updating VersionId
752// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000753void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000754 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000755 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000756 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000757
Rui Ueyamadad2b882016-09-02 22:15:08 +0000758 // Now we have version definitions, so we need to set version ids to symbols.
759 // Each version definition has a glob pattern, and all symbols that match
760 // with the pattern get that version.
761
Rui Ueyamadad2b882016-09-02 22:15:08 +0000762 // First, we assign versions to exact matching symbols,
763 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000764 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000765 for (SymbolVersion &Ver : V.Globals)
766 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000767
Rui Ueyamadad2b882016-09-02 22:15:08 +0000768 // Next, we assign versions to fuzzy matching symbols,
769 // i.e. version definitions containing glob meta-characters.
770 // Note that because the last match takes precedence over previous matches,
771 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000772 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
773 for (SymbolVersion &Ver : V.Globals)
774 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000775
776 // Symbol themselves might know their versions because symbols
777 // can contain versions in the form of <name>@<version>.
778 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000779 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000780 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000781}
782
Easwaran Ramanbfa48a12018-01-09 05:35:29 +0000783template void SymbolTable::addFile<ELF32LE>(InputFile *);
784template void SymbolTable::addFile<ELF32BE>(InputFile *);
785template void SymbolTable::addFile<ELF64LE>(InputFile *);
786template void SymbolTable::addFile<ELF64BE>(InputFile *);
787
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000788template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef);
789template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef);
790template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef);
791template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef);
Rafael Espindola244ef982017-07-26 18:42:48 +0000792
Rafael Espindolabec37652017-11-17 01:37:50 +0000793template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
794 uint8_t, bool, InputFile *);
795template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
796 uint8_t, bool, InputFile *);
797template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
798 uint8_t, bool, InputFile *);
799template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
800 uint8_t, bool, InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000801
Rafael Espindola244ef982017-07-26 18:42:48 +0000802template void SymbolTable::addCombinedLTOObject<ELF32LE>();
803template void SymbolTable::addCombinedLTOObject<ELF32BE>();
804template void SymbolTable::addCombinedLTOObject<ELF64LE>();
805template void SymbolTable::addCombinedLTOObject<ELF64BE>();
806
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000807template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000808SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000809 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000810template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000811SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000812 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000813template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000814SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000815 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000816template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000817SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000818 const object::Archive::Symbol);
819
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000820template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
821template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
822template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
823template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000824
Rui Ueyama61b67ab2018-04-03 18:59:31 +0000825template void SymbolTable::fetchLazy<ELF32LE>(Symbol *);
826template void SymbolTable::fetchLazy<ELF32BE>(Symbol *);
827template void SymbolTable::fetchLazy<ELF64LE>(Symbol *);
828template void SymbolTable::fetchLazy<ELF64BE>(Symbol *);
829
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000830template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000831 const typename ELF32LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000832 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000833template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000834 const typename ELF32BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000835 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000836template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000837 const typename ELF64LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000838 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000839template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000840 const typename ELF64BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000841 uint32_t Alignment, uint32_t);