blob: e25682f809c9aff37dc383d3e49099641a53cfed [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencer84487f12015-07-24 21:03:07 +00006//
7//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00008//
9// Symbol table is a bag of all known symbols. We put all symbols of
Rui Ueyamac9559d92016-01-05 20:47:37 +000010// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000011// a hash table with the logic to resolve symbol name conflicts using
12// the symbol types.
13//
14//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000015
16#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000017#include "Config.h"
Davide Italiano8e1131d2016-06-29 02:46:51 +000018#include "LinkerScript.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000019#include "Symbols.h"
Peter Collingbourne6c55a702017-11-06 04:33:58 +000020#include "SyntheticSections.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000021#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000022#include "lld/Common/Memory.h"
Rui Ueyama53fe4692017-11-28 02:15:26 +000023#include "lld/Common/Strings.h"
Rui Ueyamacd236a92016-11-17 19:57:43 +000024#include "llvm/ADT/STLExtras.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000027using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000028using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rafael Espindola244ef982017-07-26 18:42:48 +000033SymbolTable *elf::Symtab;
34
Rui Ueyama227cb6b2017-10-15 21:43:09 +000035static InputFile *getFirstElf() {
36 if (!ObjectFiles.empty())
37 return ObjectFiles[0];
38 if (!SharedFiles.empty())
39 return SharedFiles[0];
George Rimarfd6af6d2018-07-11 12:32:00 +000040 return BitcodeFiles[0];
Rui Ueyama227cb6b2017-10-15 21:43:09 +000041}
42
Rui Ueyamac9559d92016-01-05 20:47:37 +000043// All input object files must be for the same architecture
44// (e.g. it does not make sense to link x86 object files with
45// MIPS object files.) This function checks for that error.
Rafael Espindola9f375432017-12-23 00:04:34 +000046static bool isCompatible(InputFile *F) {
47 if (!F->isElf() && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000048 return true;
Rui Ueyama26081ca2016-11-24 20:59:44 +000049
Simon Atanasyan9e0297b2016-11-05 22:58:01 +000050 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
51 if (Config->EMachine != EM_MIPS)
52 return true;
53 if (isMipsN32Abi(F) == Config->MipsN32Abi)
54 return true;
55 }
Rui Ueyama26081ca2016-11-24 20:59:44 +000056
57 if (!Config->Emulation.empty())
58 error(toString(F) + " is incompatible with " + Config->Emulation);
59 else
Rui Ueyama227cb6b2017-10-15 21:43:09 +000060 error(toString(F) + " is incompatible with " + toString(getFirstElf()));
Rui Ueyama16ba6692016-01-29 19:41:13 +000061 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000062}
63
Rui Ueyamac9559d92016-01-05 20:47:37 +000064// Add symbols in File to the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +000065template <class ELFT> void SymbolTable::addFile(InputFile *File) {
Rafael Espindola9f375432017-12-23 00:04:34 +000066 if (!isCompatible(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000067 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000068
Michael J. Spencera9424f32016-09-09 22:08:04 +000069 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000070 if (auto *F = dyn_cast<BinaryFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +000071 BinaryFiles.push_back(F);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +000072 F->parse();
Michael J. Spencera9424f32016-09-09 22:08:04 +000073 return;
74 }
75
Rui Ueyama89575742015-12-16 22:59:13 +000076 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000077 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000078 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000079 return;
80 }
Rui Ueyama3d451792015-10-12 18:03:21 +000081
George Rimar2a78fce2016-04-13 18:07:57 +000082 // Lazy object file
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000083 if (auto *F = dyn_cast<LazyObjFile>(File)) {
Rumeet Dhindsad366e362018-05-02 21:40:07 +000084 LazyObjFiles.push_back(F);
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.
Fangrui Songb4744d32019-02-01 02:25:05 +000095 F->parseDynamic();
Fangrui Song50394f62018-12-27 22:24:45 +000096 if (errorCount())
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000097 return;
Fangrui Song50394f62018-12-27 22:24:45 +000098
99 // If a DSO appears more than once on the command line with and without
100 // --as-needed, --no-as-needed takes precedence over --as-needed because a
101 // user can add an extra DSO with --no-as-needed to force it to be added to
102 // the dependency list.
103 DenseMap<StringRef, InputFile *>::iterator It;
104 bool WasInserted;
105 std::tie(It, WasInserted) = SoNames.try_emplace(F->SoName, F);
106 cast<SharedFile<ELFT>>(It->second)->IsNeeded |= F->IsNeeded;
107 if (!WasInserted)
108 return;
109
George Rimar696a7f92017-09-19 09:20:54 +0000110 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +0000111 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +0000112 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000113 }
Rui Ueyama89575742015-12-16 22:59:13 +0000114
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000115 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000116 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000117 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000118 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000119 return;
120 }
121
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000122 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000123 ObjectFiles.push_back(File);
124 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000125}
126
Rui Ueyama42554752016-04-23 00:26:32 +0000127// This function is where all the optimizations of link-time
128// optimization happens. When LTO is in use, some input files are
129// not in native object file format but in the LLVM bitcode format.
130// This function compiles bitcode files into a few big native files
131// using LLVM functions and replaces bitcode symbols with the results.
Shoaib Meenaic1ca8062018-01-08 23:18:16 +0000132// Because all bitcode files that the program consists of are passed
Rui Ueyama42554752016-04-23 00:26:32 +0000133// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000134template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000135 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000136 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000137
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000138 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000139 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000140 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000141 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000142
Davide Italiano3bfa0812016-11-26 05:37:04 +0000143 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000144 DenseSet<CachedHashStringRef> DummyGroups;
Rafael Espindolac8f774b2018-03-28 22:45:39 +0000145 auto *Obj = cast<ObjFile<ELFT>>(File);
146 Obj->parse(DummyGroups);
147 for (Symbol *Sym : Obj->getGlobalSymbols())
148 Sym->parseSymbolVersion();
George Rimar696a7f92017-09-19 09:20:54 +0000149 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000150 }
151}
152
Rui Ueyama69c778c2016-07-17 17:50:09 +0000153// Set a flag for --trace-symbol so that we can print out a log message
154// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000155void SymbolTable::trace(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000156 SymMap.insert({CachedHashStringRef(Name), -1});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000157}
158
Rui Ueyama07b45362018-08-22 07:02:26 +0000159void SymbolTable::wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap) {
160 // Swap symbols as instructed by -wrap.
Rui Ueyamafbc62972018-10-09 20:22:18 +0000161 int &Idx1 = SymMap[CachedHashStringRef(Sym->getName())];
162 int &Idx2 = SymMap[CachedHashStringRef(Real->getName())];
163 int &Idx3 = SymMap[CachedHashStringRef(Wrap->getName())];
George Rimarc9c0ccc2018-06-22 11:18:11 +0000164
Rui Ueyama07b45362018-08-22 07:02:26 +0000165 Idx2 = Idx1;
166 Idx1 = Idx3;
George Rimarc9c0ccc2018-06-22 11:18:11 +0000167
Rui Ueyama07b45362018-08-22 07:02:26 +0000168 // Now renaming is complete. No one refers Real symbol. We could leave
169 // Real as-is, but if Real is written to the symbol table, that may
170 // contain irrelevant values. So, we copy all values from Sym to Real.
171 StringRef S = Real->getName();
172 memcpy(Real, Sym, sizeof(SymbolUnion));
173 Real->setName(S);
George Rimar9703ad22017-04-26 10:40:02 +0000174}
175
Peter Collingbournedadcc172016-04-22 18:42:48 +0000176static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
177 if (VA == STV_DEFAULT)
178 return VB;
179 if (VB == STV_DEFAULT)
180 return VA;
181 return std::min(VA, VB);
182}
183
Rui Ueyamab4de5952016-01-08 22:01:33 +0000184// Find an existing symbol or create and insert a new one.
Rui Ueyama3c434482018-10-10 22:49:29 +0000185std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000186 // <name>@@<version> means the symbol is the default version. In that
187 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000188 //
189 // Since this is a hot path, the following string search code is
190 // optimized for speed. StringRef::find(char) is much faster than
191 // StringRef::find(StringRef).
192 size_t Pos = Name.find('@');
193 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000194 Name = Name.take_front(Pos);
195
Sam Clegga80d94d2017-11-27 23:16:06 +0000196 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000197 int &SymIndex = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000198 bool IsNew = P.second;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000199 bool Traced = false;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000200
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000201 if (SymIndex == -1) {
202 SymIndex = SymVector.size();
Rui Ueyamae65cb482018-10-09 22:44:42 +0000203 IsNew = true;
204 Traced = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000205 }
206
Rui Ueyamae65cb482018-10-09 22:44:42 +0000207 if (!IsNew)
208 return {SymVector[SymIndex], false};
209
210 auto *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000211 Sym->SymbolKind = Symbol::PlaceholderKind;
Rui Ueyamae65cb482018-10-09 22:44:42 +0000212 Sym->Visibility = STV_DEFAULT;
213 Sym->IsUsedInRegularObj = false;
214 Sym->ExportDynamic = false;
215 Sym->CanInline = true;
216 Sym->Traced = Traced;
217 Sym->VersionId = Config->DefaultSymbolVersion;
218 SymVector.push_back(Sym);
219 return {Sym, true};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000220}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000221
Peter Collingbourne4f952702016-05-01 04:55:03 +0000222// Find an existing symbol or create and insert a new one, then apply the given
223// attributes.
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000224std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000225 uint8_t Visibility,
226 bool CanOmitFromDynSym,
227 InputFile *File) {
228 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000229 bool WasInserted;
Rui Ueyama3c434482018-10-10 22:49:29 +0000230 std::tie(S, WasInserted) = insertName(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000231
232 // Merge in the new symbol's visibility.
233 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000234
Peter Collingbourne4f952702016-05-01 04:55:03 +0000235 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
236 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000237
Rui Ueyamaac647252017-10-29 16:46:39 +0000238 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000239 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000240
Peter Collingbourne4f952702016-05-01 04:55:03 +0000241 return {S, WasInserted};
242}
243
Rui Ueyamae50e8072016-12-22 05:11:12 +0000244static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
245
Peter Collingbourne4f952702016-05-01 04:55:03 +0000246template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +0000247Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000248 uint8_t StOther, uint8_t Type,
249 bool CanOmitFromDynSym, InputFile *File) {
250 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000251 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000252 uint8_t Visibility = getVisibility(StOther);
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000253 std::tie(S, WasInserted) = insert(Name, Visibility, CanOmitFromDynSym, File);
Rui Ueyamad35b8392018-04-03 22:39:04 +0000254
Rafael Espindola8465d0832017-04-04 20:03:34 +0000255 // An undefined symbol with non default visibility must be satisfied
256 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000257 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000258 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000259 return S;
260 }
Rui Ueyamad35b8392018-04-03 22:39:04 +0000261
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000262 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
263 S->Binding = Binding;
Rui Ueyamad35b8392018-04-03 22:39:04 +0000264
George Rimar1ef746b2018-04-03 17:16:52 +0000265 if (S->isLazy()) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000266 // An undefined weak will not fetch archive members. See comment on Lazy in
267 // Symbols.h for the details.
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000268 if (Binding == STB_WEAK) {
George Rimar1ef746b2018-04-03 17:16:52 +0000269 S->Type = Type;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000270 return S;
271 }
272
Fangrui Songc638db52018-05-10 23:53:05 +0000273 // Do extra check for --warn-backrefs.
274 //
275 // --warn-backrefs is an option to prevent an undefined reference from
276 // fetching an archive member written earlier in the command line. It can be
277 // used to keep compatibility with GNU linkers to some degree.
278 // I'll explain the feature and why you may find it useful in this comment.
279 //
280 // lld's symbol resolution semantics is more relaxed than traditional Unix
281 // linkers. For example,
282 //
283 // ld.lld foo.a bar.o
284 //
285 // succeeds even if bar.o contains an undefined symbol that has to be
286 // resolved by some object file in foo.a. Traditional Unix linkers don't
287 // allow this kind of backward reference, as they visit each file only once
288 // from left to right in the command line while resolving all undefined
289 // symbols at the moment of visiting.
290 //
291 // In the above case, since there's no undefined symbol when a linker visits
292 // foo.a, no files are pulled out from foo.a, and because the linker forgets
293 // about foo.a after visiting, it can't resolve undefined symbols in bar.o
294 // that could have been resolved otherwise.
295 //
296 // That lld accepts more relaxed form means that (besides it'd make more
297 // sense) you can accidentally write a command line or a build file that
298 // works only with lld, even if you have a plan to distribute it to wider
299 // users who may be using GNU linkers. With --warn-backrefs, you can detect
300 // a library order that doesn't work with other Unix linkers.
301 //
302 // The option is also useful to detect cyclic dependencies between static
303 // archives. Again, lld accepts
304 //
305 // ld.lld foo.a bar.a
306 //
307 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
308 // handled as an error.
309 //
310 // Here is how the option works. We assign a group ID to each file. A file
311 // with a smaller group ID can pull out object files from an archive file
312 // with an equal or greater group ID. Otherwise, it is a reverse dependency
313 // and an error.
314 //
315 // A file outside --{start,end}-group gets a fresh ID when instantiated. All
316 // files within the same --{start,end}-group get the same group ID. E.g.
317 //
318 // ld.lld A B --start-group C D --end-group E
319 //
320 // A forms group 0. B form group 1. C and D (including their member object
321 // files) form group 2. E forms group 3. I think that you can see how this
322 // group assignment rule simulates the traditional linker's semantics.
323 bool Backref =
324 Config->WarnBackrefs && File && S->File->GroupId < File->GroupId;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000325 fetchLazy<ELFT>(S);
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000326
Fangrui Songc638db52018-05-10 23:53:05 +0000327 // We don't report backward references to weak symbols as they can be
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000328 // overridden later.
Fangrui Songae825992019-01-29 14:24:35 +0000329 if (Backref && !S->isWeak())
Fangrui Songc638db52018-05-10 23:53:05 +0000330 warn("backward reference detected: " + Name + " in " + toString(File) +
331 " refers to " + toString(S->File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000332 }
333 return S;
334}
335
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000336// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
337// foo@@VER. We want to effectively ignore foo, so give precedence to
338// foo@@VER.
339// FIXME: If users can transition to using
340// .symver foo,foo@@@VER
341// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000342static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000343 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000344 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000345 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000346 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000347 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000348 return -1;
349 return 0;
350}
351
Peter Collingbourne4f952702016-05-01 04:55:03 +0000352// We have a new defined symbol with the specified binding. Return 1 if the new
353// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
354// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000355static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000356 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000357 if (WasInserted)
358 return 1;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000359 if (!S->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000360 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000361 if (int R = compareVersion(S, Name))
362 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000363 if (Binding == STB_WEAK)
364 return -1;
365 if (S->isWeak())
366 return 1;
367 return 0;
368}
369
370// We have a new non-common defined symbol with the specified binding. Return 1
371// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
372// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000373static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
374 bool IsAbsolute, uint64_t Value,
375 StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000376 if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000377 return Cmp;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000378 if (auto *R = dyn_cast<Defined>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000379 if (R->Section && isa<BssSection>(R->Section)) {
380 // Non-common symbols take precedence over common symbols.
381 if (Config->WarnCommon)
382 warn("common " + S->getName() + " is overridden");
383 return 1;
384 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000385 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
386 R->Value == Value)
387 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000388 }
389 return 0;
390}
391
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000392Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
393 uint8_t Binding, uint8_t StOther, uint8_t Type,
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000394 InputFile &File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000395 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000396 bool WasInserted;
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000397 std::tie(S, WasInserted) = insert(N, getVisibility(StOther),
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000398 /*CanOmitFromDynSym*/ false, &File);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000399
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000400 int Cmp = compareDefined(S, WasInserted, Binding, N);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000401 if (Cmp < 0)
402 return S;
403
Peter Collingbourne4f952702016-05-01 04:55:03 +0000404 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000405 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000406 Bss->File = &File;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000407 Bss->Live = !Config->GcSections;
408 InputSections.push_back(Bss);
409
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000410 replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000411 return S;
412 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000413
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000414 auto *D = cast<Defined>(S);
415 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
416 if (!Bss) {
417 // Non-common symbols take precedence over common symbols.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000418 if (Config->WarnCommon)
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000419 warn("common " + S->getName() + " is overridden");
420 return S;
421 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000422
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000423 if (Config->WarnCommon)
424 warn("multiple common of " + D->getName());
425
426 Bss->Alignment = std::max(Bss->Alignment, Alignment);
427 if (Size > Bss->Size) {
428 D->File = Bss->File = &File;
429 D->Size = Bss->Size = Size;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000430 }
431 return S;
432}
433
George Rimarfd5a33d2018-01-31 08:32:35 +0000434static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
435 InputSectionBase *ErrSec, uint64_t ErrOffset) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000436 if (Config->AllowMultipleDefinition)
437 return;
438
Rafael Espindolab2ee25a2017-11-30 18:02:04 +0000439 Defined *D = cast<Defined>(Sym);
440 if (!D->Section || !ErrSec) {
Rui Ueyama71cdbb72018-10-09 22:44:53 +0000441 error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
442 toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000443 return;
444 }
445
Rui Ueyama810ce102017-03-31 23:40:21 +0000446 // Construct and print an error message in the form of:
447 //
448 // ld.lld: error: duplicate symbol: foo
449 // >>> defined at bar.c:30
450 // >>> bar.o (/home/alice/src/bar.o)
451 // >>> defined at baz.c:563
452 // >>> baz.o in archive libbaz.a
453 auto *Sec1 = cast<InputSectionBase>(D->Section);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000454 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000455 std::string Obj1 = Sec1->getObjMsg(D->Value);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000456 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000457 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000458
Rui Ueyama810ce102017-03-31 23:40:21 +0000459 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
460 if (!Src1.empty())
461 Msg += Src1 + "\n>>> ";
462 Msg += Obj1 + "\n>>> defined at ";
463 if (!Src2.empty())
464 Msg += Src2 + "\n>>> ";
465 Msg += Obj2;
Rui Ueyama048a6692018-03-19 23:04:04 +0000466 error(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000467}
468
George Rimar94a16cb2018-11-22 11:40:08 +0000469Defined *SymbolTable::addDefined(StringRef Name, uint8_t StOther, uint8_t Type,
470 uint64_t Value, uint64_t Size, uint8_t Binding,
471 SectionBase *Section, InputFile *File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000472 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000473 bool WasInserted;
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000474 std::tie(S, WasInserted) = insert(Name, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000475 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000476 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
477 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000478 if (Cmp > 0)
Rafael Espindolabec37652017-11-17 01:37:50 +0000479 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
480 Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000481 else if (Cmp == 0)
George Rimarfd5a33d2018-01-31 08:32:35 +0000482 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section),
483 Value);
George Rimar94a16cb2018-11-22 11:40:08 +0000484 return cast<Defined>(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000485}
486
487template <typename ELFT>
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000488void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000489 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000490 uint32_t VerdefIndex) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000491 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
492 // as the visibility, which will leave the visibility in the symbol table
493 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000494 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495 bool WasInserted;
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000496 std::tie(S, WasInserted) = insert(Name, STV_DEFAULT,
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000497 /*CanOmitFromDynSym*/ true, &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000498 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000499 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000500 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000501
Rafael Espindola8465d0832017-04-04 20:03:34 +0000502 // An undefined symbol with non default visibility must be satisfied
503 // in the same DSO.
Rui Ueyamafb8143762018-12-20 22:54:41 +0000504 auto Replace = [&](uint8_t Binding) {
505 replaceSymbol<SharedSymbol>(S, File, Name, Binding, Sym.st_other,
Rui Ueyama392f0b22018-12-19 23:25:02 +0000506 Sym.getType(), Sym.st_value, Sym.st_size,
507 Alignment, VerdefIndex);
Rui Ueyamafb8143762018-12-20 22:54:41 +0000508 };
Rui Ueyama392f0b22018-12-19 23:25:02 +0000509
Rui Ueyamafb8143762018-12-20 22:54:41 +0000510 if (WasInserted)
511 Replace(Sym.getBinding());
512 else if (S->Visibility == STV_DEFAULT && (S->isUndefined() || S->isLazy()))
513 Replace(S->Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000514}
515
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000516Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
517 uint8_t StOther, uint8_t Type,
Rafael Espindolaf1687122017-12-20 16:16:40 +0000518 bool CanOmitFromDynSym, BitcodeFile &F) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000519 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000520 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000521 std::tie(S, WasInserted) =
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000522 insert(Name, getVisibility(StOther), CanOmitFromDynSym, &F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000523 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
524 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000525 if (Cmp > 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000526 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000527 else if (Cmp == 0)
Rui Ueyama71cdbb72018-10-09 22:44:53 +0000528 reportDuplicate(S, &F, nullptr, 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000529 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000530}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000531
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000532Symbol *SymbolTable::find(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000533 auto It = SymMap.find(CachedHashStringRef(Name));
534 if (It == SymMap.end())
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000535 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000536 if (It->second == -1)
Rui Ueyamae3357902016-07-18 01:35:00 +0000537 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000538 return SymVector[It->second];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000539}
540
Rui Ueyama659cff32018-10-09 19:54:32 +0000541template <class ELFT>
542void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &File,
543 const object::Archive::Symbol Sym) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000544 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000545 bool WasInserted;
Rui Ueyama3c434482018-10-10 22:49:29 +0000546 std::tie(S, WasInserted) = insertName(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000547 if (WasInserted) {
Rui Ueyama714abec2018-10-09 20:16:16 +0000548 replaceSymbol<LazyArchive>(S, File, STT_NOTYPE, Sym);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000549 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000550 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000551 if (!S->isUndefined())
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000552 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000553
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000554 // An undefined weak will not fetch archive members. See comment on Lazy in
555 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000556 if (S->isWeak()) {
Rui Ueyama659cff32018-10-09 19:54:32 +0000557 replaceSymbol<LazyArchive>(S, File, S->Type, Sym);
Rafael Espindolabec37652017-11-17 01:37:50 +0000558 S->Binding = STB_WEAK;
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000559 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000560 }
George Rimar19f9b812018-04-24 09:41:56 +0000561
Rui Ueyama659cff32018-10-09 19:54:32 +0000562 if (InputFile *F = File.fetch(Sym))
Rui Ueyamafbc62972018-10-09 20:22:18 +0000563 addFile<ELFT>(F);
George Rimar19f9b812018-04-24 09:41:56 +0000564}
565
566template <class ELFT>
Rui Ueyama659cff32018-10-09 19:54:32 +0000567void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &File) {
568 Symbol *S;
569 bool WasInserted;
Rui Ueyama3c434482018-10-10 22:49:29 +0000570 std::tie(S, WasInserted) = insertName(Name);
Rui Ueyama659cff32018-10-09 19:54:32 +0000571 if (WasInserted) {
Rui Ueyama714abec2018-10-09 20:16:16 +0000572 replaceSymbol<LazyObject>(S, File, STT_NOTYPE, Name);
Rui Ueyama659cff32018-10-09 19:54:32 +0000573 return;
574 }
575 if (!S->isUndefined())
576 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000577
Rui Ueyama659cff32018-10-09 19:54:32 +0000578 // An undefined weak will not fetch archive members. See comment on Lazy in
579 // Symbols.h for the details.
580 if (S->isWeak()) {
581 replaceSymbol<LazyObject>(S, File, S->Type, Name);
582 S->Binding = STB_WEAK;
583 return;
584 }
585
586 if (InputFile *F = File.fetch())
Rui Ueyamafbc62972018-10-09 20:22:18 +0000587 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000588}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000589
Rui Ueyamacc013f62018-04-03 18:01:18 +0000590template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
591 if (auto *S = dyn_cast<LazyArchive>(Sym)) {
592 if (InputFile *File = S->fetch())
593 addFile<ELFT>(File);
594 return;
595 }
596
597 auto *S = cast<LazyObject>(Sym);
598 if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
599 addFile<ELFT>(File);
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000600}
601
Rui Ueyama82492142016-11-15 18:41:52 +0000602// Initialize DemangledSyms with a map from demangled symbols to symbol
603// objects. Used to handle "extern C++" directive in version scripts.
604//
605// The map will contain all demangled symbols. That can be very large,
606// and in LLD we generally want to avoid do anything for each symbol.
607// Then, why are we doing this? Here's why.
608//
609// Users can use "extern C++ {}" directive to match against demangled
610// C++ symbols. For example, you can write a pattern such as
611// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
612// other than trying to match a pattern against all demangled symbols.
613// So, if "extern C++" feature is used, we need to demangle all known
614// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000615StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000616 if (!DemangledSyms) {
617 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000618 for (Symbol *Sym : SymVector) {
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000619 if (!Sym->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000620 continue;
Rui Ueyama53fe4692017-11-28 02:15:26 +0000621 if (Optional<std::string> S = demangleItanium(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000622 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000623 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000624 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000625 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000626 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000627 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000628}
629
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000630std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000631 if (Ver.IsExternCpp)
632 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000633 if (Symbol *B = find(Ver.Name))
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000634 if (B->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000635 return {B};
636 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000637}
638
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000639std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
640 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000641 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000642
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000643 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000644 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000645 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000646 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000647 return Res;
648 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000649
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000650 for (Symbol *Sym : SymVector)
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000651 if (Sym->isDefined() && M.match(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000652 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000653 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000654}
655
Rui Ueyamaea265042016-09-13 20:51:30 +0000656// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000657// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000658// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000659void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000660 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
661 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
662 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
663 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
664 for (SymbolVersion &Ver : Config->VersionScriptLocals)
665 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
666 for (SymbolVersion &Ver : Config->VersionScriptLocals)
667 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000668}
669
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000670// Handles -dynamic-list.
671void SymbolTable::handleDynamicList() {
672 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000673 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000674 if (Ver.HasWildcard)
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000675 Syms = findAllByVersion(Ver);
Evgeniy Stepanov9ac31542017-12-06 00:14:04 +0000676 else
677 Syms = findByVersion(Ver);
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000678
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000679 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000680 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000681 B->ExportDynamic = true;
682 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000683 B->IsPreemptible = true;
684 }
685 }
686}
687
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000688// Set symbol versions to symbols. This function handles patterns
689// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000690void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
691 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000692 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000693 return;
694
695 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000696 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000697 if (Syms.empty()) {
Rui Ueyamaaad2e322018-02-02 21:44:06 +0000698 if (!Config->UndefinedVersion)
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000699 error("version script assignment of '" + VersionName + "' to symbol '" +
700 Ver.Name + "' failed: symbol not defined");
701 return;
702 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000703
704 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000705 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000706 // Skip symbols containing version info because symbol versions
707 // specified by symbol names take precedence over version scripts.
708 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000709 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000710 continue;
711
Rafael Espindoladdb33c02018-03-06 17:05:12 +0000712 if (Sym->VersionId != Config->DefaultSymbolVersion &&
713 Sym->VersionId != VersionId)
714 error("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000715 Sym->VersionId = VersionId;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000716 }
717}
718
Rafael Espindola244ef982017-07-26 18:42:48 +0000719void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000720 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000721 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000722
723 // Exact matching takes precendence over fuzzy matching,
724 // so we set a version to a symbol only if no version has been assigned
725 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000726 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000727 if (B->VersionId == Config->DefaultSymbolVersion)
728 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000729}
730
Rui Ueyamadad2b882016-09-02 22:15:08 +0000731// This function processes version scripts by updating VersionId
732// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000733void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000734 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000735 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000736 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000737
Rui Ueyamadad2b882016-09-02 22:15:08 +0000738 // Now we have version definitions, so we need to set version ids to symbols.
739 // Each version definition has a glob pattern, and all symbols that match
740 // with the pattern get that version.
741
Rui Ueyamadad2b882016-09-02 22:15:08 +0000742 // First, we assign versions to exact matching symbols,
743 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000744 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000745 for (SymbolVersion &Ver : V.Globals)
746 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000747
Rui Ueyamadad2b882016-09-02 22:15:08 +0000748 // Next, we assign versions to fuzzy matching symbols,
749 // i.e. version definitions containing glob meta-characters.
750 // Note that because the last match takes precedence over previous matches,
751 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000752 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
753 for (SymbolVersion &Ver : V.Globals)
754 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000755
756 // Symbol themselves might know their versions because symbols
757 // can contain versions in the form of <name>@<version>.
758 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000759 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000760 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000761}
762
Easwaran Ramanbfa48a12018-01-09 05:35:29 +0000763template void SymbolTable::addFile<ELF32LE>(InputFile *);
764template void SymbolTable::addFile<ELF32BE>(InputFile *);
765template void SymbolTable::addFile<ELF64LE>(InputFile *);
766template void SymbolTable::addFile<ELF64BE>(InputFile *);
767
Rafael Espindolabec37652017-11-17 01:37:50 +0000768template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
769 uint8_t, bool, InputFile *);
770template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
771 uint8_t, bool, InputFile *);
772template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
773 uint8_t, bool, InputFile *);
774template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
775 uint8_t, bool, InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000776
Rafael Espindola244ef982017-07-26 18:42:48 +0000777template void SymbolTable::addCombinedLTOObject<ELF32LE>();
778template void SymbolTable::addCombinedLTOObject<ELF32BE>();
779template void SymbolTable::addCombinedLTOObject<ELF64LE>();
780template void SymbolTable::addCombinedLTOObject<ELF64BE>();
781
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000782template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000783SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000784 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000785template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000786SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000787 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000788template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000789SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000790 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000791template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000792SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000793 const object::Archive::Symbol);
794
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000795template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
796template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
797template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
798template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000799
Rui Ueyama61b67ab2018-04-03 18:59:31 +0000800template void SymbolTable::fetchLazy<ELF32LE>(Symbol *);
801template void SymbolTable::fetchLazy<ELF32BE>(Symbol *);
802template void SymbolTable::fetchLazy<ELF64LE>(Symbol *);
803template void SymbolTable::fetchLazy<ELF64BE>(Symbol *);
804
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000805template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000806 const typename ELF32LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000807 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000808template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000809 const typename ELF32BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000810 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000811template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000812 const typename ELF64LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000813 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000814template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000815 const typename ELF64BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000816 uint32_t Alignment, uint32_t);