blob: 7615e12199faf248a60fb7e172248c88879cb744 [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();
Fangrui Song50394f62018-12-27 22:24:45 +000097 if (errorCount())
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000098 return;
Fangrui Song50394f62018-12-27 22:24:45 +000099
100 // If a DSO appears more than once on the command line with and without
101 // --as-needed, --no-as-needed takes precedence over --as-needed because a
102 // user can add an extra DSO with --no-as-needed to force it to be added to
103 // the dependency list.
104 DenseMap<StringRef, InputFile *>::iterator It;
105 bool WasInserted;
106 std::tie(It, WasInserted) = SoNames.try_emplace(F->SoName, F);
107 cast<SharedFile<ELFT>>(It->second)->IsNeeded |= F->IsNeeded;
108 if (!WasInserted)
109 return;
110
George Rimar696a7f92017-09-19 09:20:54 +0000111 SharedFiles.push_back(F);
Rui Ueyama7c713312016-01-06 01:56:36 +0000112 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +0000113 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000114 }
Rui Ueyama89575742015-12-16 22:59:13 +0000115
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000116 // LLVM bitcode file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000117 if (auto *F = dyn_cast<BitcodeFile>(File)) {
George Rimar696a7f92017-09-19 09:20:54 +0000118 BitcodeFiles.push_back(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000119 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000120 return;
121 }
122
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000123 // Regular object file
George Rimar696a7f92017-09-19 09:20:54 +0000124 ObjectFiles.push_back(File);
125 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000126}
127
Rui Ueyama42554752016-04-23 00:26:32 +0000128// This function is where all the optimizations of link-time
129// optimization happens. When LTO is in use, some input files are
130// not in native object file format but in the LLVM bitcode format.
131// This function compiles bitcode files into a few big native files
132// using LLVM functions and replaces bitcode symbols with the results.
Shoaib Meenaic1ca8062018-01-08 23:18:16 +0000133// Because all bitcode files that the program consists of are passed
Rui Ueyama42554752016-04-23 00:26:32 +0000134// to the compiler at once, it can do whole-program optimization.
Rafael Espindola244ef982017-07-26 18:42:48 +0000135template <class ELFT> void SymbolTable::addCombinedLTOObject() {
George Rimar696a7f92017-09-19 09:20:54 +0000136 if (BitcodeFiles.empty())
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000137 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000138
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000139 // Compile bitcode files and replace bitcode symbols.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000140 LTO.reset(new BitcodeCompiler);
George Rimar696a7f92017-09-19 09:20:54 +0000141 for (BitcodeFile *F : BitcodeFiles)
Peter Smith3a52eb02017-02-01 10:26:03 +0000142 LTO->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000143
Davide Italiano3bfa0812016-11-26 05:37:04 +0000144 for (InputFile *File : LTO->compile()) {
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000145 DenseSet<CachedHashStringRef> DummyGroups;
Rafael Espindolac8f774b2018-03-28 22:45:39 +0000146 auto *Obj = cast<ObjFile<ELFT>>(File);
147 Obj->parse(DummyGroups);
148 for (Symbol *Sym : Obj->getGlobalSymbols())
149 Sym->parseSymbolVersion();
George Rimar696a7f92017-09-19 09:20:54 +0000150 ObjectFiles.push_back(File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000151 }
152}
153
Rui Ueyama69c778c2016-07-17 17:50:09 +0000154// Set a flag for --trace-symbol so that we can print out a log message
155// if a new symbol with the same name is inserted into the symbol table.
Rafael Espindola244ef982017-07-26 18:42:48 +0000156void SymbolTable::trace(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000157 SymMap.insert({CachedHashStringRef(Name), -1});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000158}
159
Rui Ueyama07b45362018-08-22 07:02:26 +0000160void SymbolTable::wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap) {
161 // Swap symbols as instructed by -wrap.
Rui Ueyamafbc62972018-10-09 20:22:18 +0000162 int &Idx1 = SymMap[CachedHashStringRef(Sym->getName())];
163 int &Idx2 = SymMap[CachedHashStringRef(Real->getName())];
164 int &Idx3 = SymMap[CachedHashStringRef(Wrap->getName())];
George Rimarc9c0ccc2018-06-22 11:18:11 +0000165
Rui Ueyama07b45362018-08-22 07:02:26 +0000166 Idx2 = Idx1;
167 Idx1 = Idx3;
George Rimarc9c0ccc2018-06-22 11:18:11 +0000168
Rui Ueyama07b45362018-08-22 07:02:26 +0000169 // Now renaming is complete. No one refers Real symbol. We could leave
170 // Real as-is, but if Real is written to the symbol table, that may
171 // contain irrelevant values. So, we copy all values from Sym to Real.
172 StringRef S = Real->getName();
173 memcpy(Real, Sym, sizeof(SymbolUnion));
174 Real->setName(S);
George Rimar9703ad22017-04-26 10:40:02 +0000175}
176
Peter Collingbournedadcc172016-04-22 18:42:48 +0000177static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
178 if (VA == STV_DEFAULT)
179 return VB;
180 if (VB == STV_DEFAULT)
181 return VA;
182 return std::min(VA, VB);
183}
184
Rui Ueyamab4de5952016-01-08 22:01:33 +0000185// Find an existing symbol or create and insert a new one.
Rui Ueyama3c434482018-10-10 22:49:29 +0000186std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) {
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000187 // <name>@@<version> means the symbol is the default version. In that
188 // case <name>@@<version> will be used to resolve references to <name>.
Rui Ueyama68b3acc2017-09-26 04:17:13 +0000189 //
190 // Since this is a hot path, the following string search code is
191 // optimized for speed. StringRef::find(char) is much faster than
192 // StringRef::find(StringRef).
193 size_t Pos = Name.find('@');
194 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000195 Name = Name.take_front(Pos);
196
Sam Clegga80d94d2017-11-27 23:16:06 +0000197 auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()});
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000198 int &SymIndex = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000199 bool IsNew = P.second;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000200 bool Traced = false;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000201
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000202 if (SymIndex == -1) {
203 SymIndex = SymVector.size();
Rui Ueyamae65cb482018-10-09 22:44:42 +0000204 IsNew = true;
205 Traced = true;
Rui Ueyamae3357902016-07-18 01:35:00 +0000206 }
207
Rui Ueyamae65cb482018-10-09 22:44:42 +0000208 if (!IsNew)
209 return {SymVector[SymIndex], false};
210
211 auto *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000212 Sym->SymbolKind = Symbol::PlaceholderKind;
Rui Ueyamae65cb482018-10-09 22:44:42 +0000213 Sym->Visibility = STV_DEFAULT;
214 Sym->IsUsedInRegularObj = false;
215 Sym->ExportDynamic = false;
216 Sym->CanInline = true;
217 Sym->Traced = Traced;
218 Sym->VersionId = Config->DefaultSymbolVersion;
219 SymVector.push_back(Sym);
220 return {Sym, true};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000221}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000222
Peter Collingbourne4f952702016-05-01 04:55:03 +0000223// Find an existing symbol or create and insert a new one, then apply the given
224// attributes.
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000225std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000226 uint8_t Visibility,
227 bool CanOmitFromDynSym,
228 InputFile *File) {
229 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000230 bool WasInserted;
Rui Ueyama3c434482018-10-10 22:49:29 +0000231 std::tie(S, WasInserted) = insertName(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000232
233 // Merge in the new symbol's visibility.
234 S->Visibility = getMinVisibility(S->Visibility, Visibility);
Rui Ueyama810ce102017-03-31 23:40:21 +0000235
Peter Collingbourne4f952702016-05-01 04:55:03 +0000236 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
237 S->ExportDynamic = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000238
Rui Ueyamaac647252017-10-29 16:46:39 +0000239 if (!File || File->kind() == InputFile::ObjKind)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000240 S->IsUsedInRegularObj = true;
Rui Ueyama810ce102017-03-31 23:40:21 +0000241
Peter Collingbourne4f952702016-05-01 04:55:03 +0000242 return {S, WasInserted};
243}
244
Rui Ueyamae50e8072016-12-22 05:11:12 +0000245static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
246
Peter Collingbourne4f952702016-05-01 04:55:03 +0000247template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +0000248Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding,
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000249 uint8_t StOther, uint8_t Type,
250 bool CanOmitFromDynSym, InputFile *File) {
251 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000252 bool WasInserted;
Rafael Espindola8465d0832017-04-04 20:03:34 +0000253 uint8_t Visibility = getVisibility(StOther);
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000254 std::tie(S, WasInserted) = insert(Name, Visibility, CanOmitFromDynSym, File);
Rui Ueyamad35b8392018-04-03 22:39:04 +0000255
Rafael Espindola8465d0832017-04-04 20:03:34 +0000256 // An undefined symbol with non default visibility must be satisfied
257 // in the same DSO.
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000258 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000259 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000260 return S;
261 }
Rui Ueyamad35b8392018-04-03 22:39:04 +0000262
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000263 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
264 S->Binding = Binding;
Rui Ueyamad35b8392018-04-03 22:39:04 +0000265
George Rimar1ef746b2018-04-03 17:16:52 +0000266 if (S->isLazy()) {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000267 // An undefined weak will not fetch archive members. See comment on Lazy in
268 // Symbols.h for the details.
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000269 if (Binding == STB_WEAK) {
George Rimar1ef746b2018-04-03 17:16:52 +0000270 S->Type = Type;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000271 return S;
272 }
273
Fangrui Songc638db52018-05-10 23:53:05 +0000274 // Do extra check for --warn-backrefs.
275 //
276 // --warn-backrefs is an option to prevent an undefined reference from
277 // fetching an archive member written earlier in the command line. It can be
278 // used to keep compatibility with GNU linkers to some degree.
279 // I'll explain the feature and why you may find it useful in this comment.
280 //
281 // lld's symbol resolution semantics is more relaxed than traditional Unix
282 // linkers. For example,
283 //
284 // ld.lld foo.a bar.o
285 //
286 // succeeds even if bar.o contains an undefined symbol that has to be
287 // resolved by some object file in foo.a. Traditional Unix linkers don't
288 // allow this kind of backward reference, as they visit each file only once
289 // from left to right in the command line while resolving all undefined
290 // symbols at the moment of visiting.
291 //
292 // In the above case, since there's no undefined symbol when a linker visits
293 // foo.a, no files are pulled out from foo.a, and because the linker forgets
294 // about foo.a after visiting, it can't resolve undefined symbols in bar.o
295 // that could have been resolved otherwise.
296 //
297 // That lld accepts more relaxed form means that (besides it'd make more
298 // sense) you can accidentally write a command line or a build file that
299 // works only with lld, even if you have a plan to distribute it to wider
300 // users who may be using GNU linkers. With --warn-backrefs, you can detect
301 // a library order that doesn't work with other Unix linkers.
302 //
303 // The option is also useful to detect cyclic dependencies between static
304 // archives. Again, lld accepts
305 //
306 // ld.lld foo.a bar.a
307 //
308 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
309 // handled as an error.
310 //
311 // Here is how the option works. We assign a group ID to each file. A file
312 // with a smaller group ID can pull out object files from an archive file
313 // with an equal or greater group ID. Otherwise, it is a reverse dependency
314 // and an error.
315 //
316 // A file outside --{start,end}-group gets a fresh ID when instantiated. All
317 // files within the same --{start,end}-group get the same group ID. E.g.
318 //
319 // ld.lld A B --start-group C D --end-group E
320 //
321 // A forms group 0. B form group 1. C and D (including their member object
322 // files) form group 2. E forms group 3. I think that you can see how this
323 // group assignment rule simulates the traditional linker's semantics.
324 bool Backref =
325 Config->WarnBackrefs && File && S->File->GroupId < File->GroupId;
Rui Ueyama1d92aa72018-04-09 23:05:48 +0000326 fetchLazy<ELFT>(S);
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000327
Fangrui Songc638db52018-05-10 23:53:05 +0000328 // We don't report backward references to weak symbols as they can be
Rui Ueyama9867c9e2018-05-21 18:12:46 +0000329 // overridden later.
Fangrui Songc638db52018-05-10 23:53:05 +0000330 if (Backref && S->Binding != STB_WEAK)
331 warn("backward reference detected: " + Name + " in " + toString(File) +
332 " refers to " + toString(S->File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000333 }
334 return S;
335}
336
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000337// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
338// foo@@VER. We want to effectively ignore foo, so give precedence to
339// foo@@VER.
340// FIXME: If users can transition to using
341// .symver foo,foo@@@VER
342// we can delete this hack.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000343static int compareVersion(Symbol *S, StringRef Name) {
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000344 bool A = Name.contains("@@");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000345 bool B = S->getName().contains("@@");
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000346 if (A && !B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000347 return 1;
Rui Ueyama0deaacb2017-07-19 21:49:01 +0000348 if (!A && B)
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000349 return -1;
350 return 0;
351}
352
Peter Collingbourne4f952702016-05-01 04:55:03 +0000353// We have a new defined symbol with the specified binding. Return 1 if the new
354// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
355// strong defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000356static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding,
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000357 StringRef Name) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000358 if (WasInserted)
359 return 1;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000360 if (!S->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000361 return 1;
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000362 if (int R = compareVersion(S, Name))
363 return R;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000364 if (Binding == STB_WEAK)
365 return -1;
366 if (S->isWeak())
367 return 1;
368 return 0;
369}
370
371// We have a new non-common defined symbol with the specified binding. Return 1
372// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
373// is a conflict. If the new symbol wins, also update the binding.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000374static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding,
375 bool IsAbsolute, uint64_t Value,
376 StringRef Name) {
Rafael Espindolabec37652017-11-17 01:37:50 +0000377 if (int Cmp = compareDefined(S, WasInserted, Binding, Name))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000378 return Cmp;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000379 if (auto *R = dyn_cast<Defined>(S)) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000380 if (R->Section && isa<BssSection>(R->Section)) {
381 // Non-common symbols take precedence over common symbols.
382 if (Config->WarnCommon)
383 warn("common " + S->getName() + " is overridden");
384 return 1;
385 }
Rafael Espindola858c0922016-12-02 02:58:21 +0000386 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
387 R->Value == Value)
388 return -1;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000389 }
390 return 0;
391}
392
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000393Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment,
394 uint8_t Binding, uint8_t StOther, uint8_t Type,
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000395 InputFile &File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000396 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000397 bool WasInserted;
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000398 std::tie(S, WasInserted) = insert(N, getVisibility(StOther),
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000399 /*CanOmitFromDynSym*/ false, &File);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000400
Rafael Espindola3ddf2112017-07-19 16:45:05 +0000401 int Cmp = compareDefined(S, WasInserted, Binding, N);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000402 if (Cmp < 0)
403 return S;
404
Peter Collingbourne4f952702016-05-01 04:55:03 +0000405 if (Cmp > 0) {
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000406 auto *Bss = make<BssSection>("COMMON", Size, Alignment);
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000407 Bss->File = &File;
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000408 Bss->Live = !Config->GcSections;
409 InputSections.push_back(Bss);
410
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +0000411 replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss);
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000412 return S;
413 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000414
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000415 auto *D = cast<Defined>(S);
416 auto *Bss = dyn_cast_or_null<BssSection>(D->Section);
417 if (!Bss) {
418 // Non-common symbols take precedence over common symbols.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000419 if (Config->WarnCommon)
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000420 warn("common " + S->getName() + " is overridden");
421 return S;
422 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000423
Rui Ueyama90f13ed2018-04-03 22:39:12 +0000424 if (Config->WarnCommon)
425 warn("multiple common of " + D->getName());
426
427 Bss->Alignment = std::max(Bss->Alignment, Alignment);
428 if (Size > Bss->Size) {
429 D->File = Bss->File = &File;
430 D->Size = Bss->Size = Size;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000431 }
432 return S;
433}
434
George Rimarfd5a33d2018-01-31 08:32:35 +0000435static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
436 InputSectionBase *ErrSec, uint64_t ErrOffset) {
Rui Ueyama048a6692018-03-19 23:04:04 +0000437 if (Config->AllowMultipleDefinition)
438 return;
439
Rafael Espindolab2ee25a2017-11-30 18:02:04 +0000440 Defined *D = cast<Defined>(Sym);
441 if (!D->Section || !ErrSec) {
Rui Ueyama71cdbb72018-10-09 22:44:53 +0000442 error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
443 toString(Sym->File) + "\n>>> defined in " + toString(NewFile));
Eugene Leviant825e5382016-11-08 16:26:32 +0000444 return;
445 }
446
Rui Ueyama810ce102017-03-31 23:40:21 +0000447 // Construct and print an error message in the form of:
448 //
449 // ld.lld: error: duplicate symbol: foo
450 // >>> defined at bar.c:30
451 // >>> bar.o (/home/alice/src/bar.o)
452 // >>> defined at baz.c:563
453 // >>> baz.o in archive libbaz.a
454 auto *Sec1 = cast<InputSectionBase>(D->Section);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000455 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000456 std::string Obj1 = Sec1->getObjMsg(D->Value);
Rafael Espindola9a84f6b2017-12-23 17:21:39 +0000457 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset);
Rui Ueyamad6b7a392017-10-27 03:13:54 +0000458 std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
Eugene Leviant825e5382016-11-08 16:26:32 +0000459
Rui Ueyama810ce102017-03-31 23:40:21 +0000460 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
461 if (!Src1.empty())
462 Msg += Src1 + "\n>>> ";
463 Msg += Obj1 + "\n>>> defined at ";
464 if (!Src2.empty())
465 Msg += Src2 + "\n>>> ";
466 Msg += Obj2;
Rui Ueyama048a6692018-03-19 23:04:04 +0000467 error(Msg);
Eugene Leviant825e5382016-11-08 16:26:32 +0000468}
469
George Rimar94a16cb2018-11-22 11:40:08 +0000470Defined *SymbolTable::addDefined(StringRef Name, uint8_t StOther, uint8_t Type,
471 uint64_t Value, uint64_t Size, uint8_t Binding,
472 SectionBase *Section, InputFile *File) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000473 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000474 bool WasInserted;
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000475 std::tie(S, WasInserted) = insert(Name, getVisibility(StOther),
George Rimar463984d2016-11-15 08:07:14 +0000476 /*CanOmitFromDynSym*/ false, File);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000477 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
478 Value, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000479 if (Cmp > 0)
Rafael Espindolabec37652017-11-17 01:37:50 +0000480 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size,
481 Section);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000482 else if (Cmp == 0)
George Rimarfd5a33d2018-01-31 08:32:35 +0000483 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section),
484 Value);
George Rimar94a16cb2018-11-22 11:40:08 +0000485 return cast<Defined>(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000486}
487
488template <typename ELFT>
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000489void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File,
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000490 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000491 uint32_t VerdefIndex) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000492 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
493 // as the visibility, which will leave the visibility in the symbol table
494 // unchanged.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000495 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000496 bool WasInserted;
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000497 std::tie(S, WasInserted) = insert(Name, STV_DEFAULT,
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000498 /*CanOmitFromDynSym*/ true, &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000499 // Make sure we preempt DSO symbols with default visibility.
Rafael Espindola41a93a32017-01-16 17:35:23 +0000500 if (Sym.getVisibility() == STV_DEFAULT)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000501 S->ExportDynamic = true;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000502
Rafael Espindola8465d0832017-04-04 20:03:34 +0000503 // An undefined symbol with non default visibility must be satisfied
504 // in the same DSO.
Rui Ueyamafb8143762018-12-20 22:54:41 +0000505 auto Replace = [&](uint8_t Binding) {
506 replaceSymbol<SharedSymbol>(S, File, Name, Binding, Sym.st_other,
Rui Ueyama392f0b22018-12-19 23:25:02 +0000507 Sym.getType(), Sym.st_value, Sym.st_size,
508 Alignment, VerdefIndex);
Rui Ueyamafb8143762018-12-20 22:54:41 +0000509 };
Rui Ueyama392f0b22018-12-19 23:25:02 +0000510
Rui Ueyamafb8143762018-12-20 22:54:41 +0000511 if (WasInserted)
512 Replace(Sym.getBinding());
513 else if (S->Visibility == STV_DEFAULT && (S->isUndefined() || S->isLazy()))
514 Replace(S->Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515}
516
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000517Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
518 uint8_t StOther, uint8_t Type,
Rafael Espindolaf1687122017-12-20 16:16:40 +0000519 bool CanOmitFromDynSym, BitcodeFile &F) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000520 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000521 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000522 std::tie(S, WasInserted) =
Rui Ueyamaf3fad552018-10-12 18:29:18 +0000523 insert(Name, getVisibility(StOther), CanOmitFromDynSym, &F);
Rafael Espindolad4fbe4f2017-07-25 23:15:35 +0000524 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
525 /*IsAbs*/ false, /*Value*/ 0, Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000526 if (Cmp > 0)
Rafael Espindolaf1687122017-12-20 16:16:40 +0000527 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000528 else if (Cmp == 0)
Rui Ueyama71cdbb72018-10-09 22:44:53 +0000529 reportDuplicate(S, &F, nullptr, 0);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000530 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000531}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000532
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000533Symbol *SymbolTable::find(StringRef Name) {
Sam Clegga80d94d2017-11-27 23:16:06 +0000534 auto It = SymMap.find(CachedHashStringRef(Name));
535 if (It == SymMap.end())
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000536 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000537 if (It->second == -1)
Rui Ueyamae3357902016-07-18 01:35:00 +0000538 return nullptr;
Peter Collingbourne38082ac62017-11-06 04:58:04 +0000539 return SymVector[It->second];
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000540}
541
Rui Ueyama659cff32018-10-09 19:54:32 +0000542template <class ELFT>
543void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &File,
544 const object::Archive::Symbol Sym) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000545 Symbol *S;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000546 bool WasInserted;
Rui Ueyama3c434482018-10-10 22:49:29 +0000547 std::tie(S, WasInserted) = insertName(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000548 if (WasInserted) {
Rui Ueyama714abec2018-10-09 20:16:16 +0000549 replaceSymbol<LazyArchive>(S, File, STT_NOTYPE, Sym);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000550 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000551 }
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000552 if (!S->isUndefined())
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000553 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000554
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000555 // An undefined weak will not fetch archive members. See comment on Lazy in
556 // Symbols.h for the details.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000557 if (S->isWeak()) {
Rui Ueyama659cff32018-10-09 19:54:32 +0000558 replaceSymbol<LazyArchive>(S, File, S->Type, Sym);
Rafael Espindolabec37652017-11-17 01:37:50 +0000559 S->Binding = STB_WEAK;
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000560 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000561 }
George Rimar19f9b812018-04-24 09:41:56 +0000562
Rui Ueyama659cff32018-10-09 19:54:32 +0000563 if (InputFile *F = File.fetch(Sym))
Rui Ueyamafbc62972018-10-09 20:22:18 +0000564 addFile<ELFT>(F);
George Rimar19f9b812018-04-24 09:41:56 +0000565}
566
567template <class ELFT>
Rui Ueyama659cff32018-10-09 19:54:32 +0000568void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &File) {
569 Symbol *S;
570 bool WasInserted;
Rui Ueyama3c434482018-10-10 22:49:29 +0000571 std::tie(S, WasInserted) = insertName(Name);
Rui Ueyama659cff32018-10-09 19:54:32 +0000572 if (WasInserted) {
Rui Ueyama714abec2018-10-09 20:16:16 +0000573 replaceSymbol<LazyObject>(S, File, STT_NOTYPE, Name);
Rui Ueyama659cff32018-10-09 19:54:32 +0000574 return;
575 }
576 if (!S->isUndefined())
577 return;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000578
Rui Ueyama659cff32018-10-09 19:54:32 +0000579 // An undefined weak will not fetch archive members. See comment on Lazy in
580 // Symbols.h for the details.
581 if (S->isWeak()) {
582 replaceSymbol<LazyObject>(S, File, S->Type, Name);
583 S->Binding = STB_WEAK;
584 return;
585 }
586
587 if (InputFile *F = File.fetch())
Rui Ueyamafbc62972018-10-09 20:22:18 +0000588 addFile<ELFT>(F);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000589}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000590
Rui Ueyamacc013f62018-04-03 18:01:18 +0000591template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {
592 if (auto *S = dyn_cast<LazyArchive>(Sym)) {
593 if (InputFile *File = S->fetch())
594 addFile<ELFT>(File);
595 return;
596 }
597
598 auto *S = cast<LazyObject>(Sym);
599 if (InputFile *File = cast<LazyObjFile>(S->File)->fetch())
600 addFile<ELFT>(File);
Igor Kudrinfb7f8be2017-10-03 12:23:46 +0000601}
602
Rui Ueyama82492142016-11-15 18:41:52 +0000603// Initialize DemangledSyms with a map from demangled symbols to symbol
604// objects. Used to handle "extern C++" directive in version scripts.
605//
606// The map will contain all demangled symbols. That can be very large,
607// and in LLD we generally want to avoid do anything for each symbol.
608// Then, why are we doing this? Here's why.
609//
610// Users can use "extern C++ {}" directive to match against demangled
611// C++ symbols. For example, you can write a pattern such as
612// "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
613// other than trying to match a pattern against all demangled symbols.
614// So, if "extern C++" feature is used, we need to demangle all known
615// symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000616StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() {
Rui Ueyama96aff372016-12-22 05:31:52 +0000617 if (!DemangledSyms) {
618 DemangledSyms.emplace();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000619 for (Symbol *Sym : SymVector) {
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000620 if (!Sym->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000621 continue;
Rui Ueyama53fe4692017-11-28 02:15:26 +0000622 if (Optional<std::string> S = demangleItanium(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000623 (*DemangledSyms)[*S].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000624 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000625 (*DemangledSyms)[Sym->getName()].push_back(Sym);
Rui Ueyama96aff372016-12-22 05:31:52 +0000626 }
Rui Ueyamad6328522016-07-18 01:34:57 +0000627 }
Rui Ueyama96aff372016-12-22 05:31:52 +0000628 return *DemangledSyms;
George Rimar50dcece2016-07-16 12:26:39 +0000629}
630
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000631std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000632 if (Ver.IsExternCpp)
633 return getDemangledSyms().lookup(Ver.Name);
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000634 if (Symbol *B = find(Ver.Name))
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000635 if (B->isDefined())
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000636 return {B};
637 return {};
Rafael Espindolac65aee62016-12-08 15:56:33 +0000638}
639
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000640std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
641 std::vector<Symbol *> Res;
Vitaly Buka0b7de062016-12-21 02:27:14 +0000642 StringMatcher M(Ver.Name);
Rafael Espindola191390a2016-12-08 16:26:20 +0000643
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000644 if (Ver.IsExternCpp) {
Rui Ueyama96aff372016-12-22 05:31:52 +0000645 for (auto &P : getDemangledSyms())
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000646 if (M.match(P.first()))
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000647 Res.insert(Res.end(), P.second.begin(), P.second.end());
Rafael Espindoladefdfa82016-12-08 16:02:48 +0000648 return Res;
649 }
Rafael Espindola191390a2016-12-08 16:26:20 +0000650
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000651 for (Symbol *Sym : SymVector)
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000652 if (Sym->isDefined() && M.match(Sym->getName()))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000653 Res.push_back(Sym);
Rafael Espindola191390a2016-12-08 16:26:20 +0000654 return Res;
Rafael Espindolac65aee62016-12-08 15:56:33 +0000655}
656
Rui Ueyamaea265042016-09-13 20:51:30 +0000657// If there's only one anonymous version definition in a version
George Rimar92ca6f42016-11-14 09:56:35 +0000658// script file, the script does not actually define any symbol version,
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000659// but just specifies symbols visibilities.
Rafael Espindola244ef982017-07-26 18:42:48 +0000660void SymbolTable::handleAnonymousVersion() {
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000661 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
662 assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
663 for (SymbolVersion &Ver : Config->VersionScriptGlobals)
664 assignWildcardVersion(Ver, VER_NDX_GLOBAL);
665 for (SymbolVersion &Ver : Config->VersionScriptLocals)
666 assignExactVersion(Ver, VER_NDX_LOCAL, "local");
667 for (SymbolVersion &Ver : Config->VersionScriptLocals)
668 assignWildcardVersion(Ver, VER_NDX_LOCAL);
Rui Ueyamaea265042016-09-13 20:51:30 +0000669}
670
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000671// Handles -dynamic-list.
672void SymbolTable::handleDynamicList() {
673 for (SymbolVersion &Ver : Config->DynamicList) {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000674 std::vector<Symbol *> Syms;
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000675 if (Ver.HasWildcard)
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000676 Syms = findAllByVersion(Ver);
Evgeniy Stepanov9ac31542017-12-06 00:14:04 +0000677 else
678 Syms = findByVersion(Ver);
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000679
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000680 for (Symbol *B : Syms) {
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000681 if (!Config->Shared)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000682 B->ExportDynamic = true;
683 else if (B->includeInDynsym())
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000684 B->IsPreemptible = true;
685 }
686 }
687}
688
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000689// Set symbol versions to symbols. This function handles patterns
690// containing no wildcard characters.
Rafael Espindola244ef982017-07-26 18:42:48 +0000691void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
692 StringRef VersionName) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000693 if (Ver.HasWildcard)
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000694 return;
695
696 // Get a list of symbols which we need to assign the version to.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000697 std::vector<Symbol *> Syms = findByVersion(Ver);
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000698 if (Syms.empty()) {
Rui Ueyamaaad2e322018-02-02 21:44:06 +0000699 if (!Config->UndefinedVersion)
Rui Ueyama4c134ea2016-12-22 09:54:32 +0000700 error("version script assignment of '" + VersionName + "' to symbol '" +
701 Ver.Name + "' failed: symbol not defined");
702 return;
703 }
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000704
705 // Assign the version.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000706 for (Symbol *Sym : Syms) {
George Rimar3e8a4612017-07-12 13:54:42 +0000707 // Skip symbols containing version info because symbol versions
708 // specified by symbol names take precedence over version scripts.
709 // See parseSymbolVersion().
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000710 if (Sym->getName().contains('@'))
George Rimar3e8a4612017-07-12 13:54:42 +0000711 continue;
712
Rafael Espindoladdb33c02018-03-06 17:05:12 +0000713 if (Sym->VersionId != Config->DefaultSymbolVersion &&
714 Sym->VersionId != VersionId)
715 error("duplicate symbol '" + Ver.Name + "' in version script");
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000716 Sym->VersionId = VersionId;
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000717 }
718}
719
Rafael Espindola244ef982017-07-26 18:42:48 +0000720void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
Rui Ueyama8980c922016-11-18 06:30:08 +0000721 if (!Ver.HasWildcard)
Rui Ueyama82492142016-11-15 18:41:52 +0000722 return;
Rui Ueyama82492142016-11-15 18:41:52 +0000723
724 // Exact matching takes precendence over fuzzy matching,
725 // so we set a version to a symbol only if no version has been assigned
726 // to the symbol. This behavior is compatible with GNU.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000727 for (Symbol *B : findAllByVersion(Ver))
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000728 if (B->VersionId == Config->DefaultSymbolVersion)
729 B->VersionId = VersionId;
Rui Ueyama82492142016-11-15 18:41:52 +0000730}
731
Rui Ueyamadad2b882016-09-02 22:15:08 +0000732// This function processes version scripts by updating VersionId
733// member of symbols.
Rafael Espindola244ef982017-07-26 18:42:48 +0000734void SymbolTable::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000735 // Handle edge cases first.
Rafael Espindolae999ddb2017-01-10 16:37:24 +0000736 handleAnonymousVersion();
Rafael Espindolad72d97b2017-09-08 18:16:59 +0000737 handleDynamicList();
George Rimard3566302016-06-20 11:55:12 +0000738
Rui Ueyamadad2b882016-09-02 22:15:08 +0000739 // Now we have version definitions, so we need to set version ids to symbols.
740 // Each version definition has a glob pattern, and all symbols that match
741 // with the pattern get that version.
742
Rui Ueyamadad2b882016-09-02 22:15:08 +0000743 // First, we assign versions to exact matching symbols,
744 // i.e. version definitions not containing any glob meta-characters.
George Rimar17c65af2016-11-16 18:46:23 +0000745 for (VersionDefinition &V : Config->VersionDefinitions)
Rui Ueyama96db27c2016-11-17 19:57:45 +0000746 for (SymbolVersion &Ver : V.Globals)
747 assignExactVersion(Ver, V.Id, V.Name);
George Rimarf73a2582016-07-07 07:45:27 +0000748
Rui Ueyamadad2b882016-09-02 22:15:08 +0000749 // Next, we assign versions to fuzzy matching symbols,
750 // i.e. version definitions containing glob meta-characters.
751 // Note that because the last match takes precedence over previous matches,
752 // we iterate over the definitions in the reverse order.
Rui Ueyamacd236a92016-11-17 19:57:43 +0000753 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
754 for (SymbolVersion &Ver : V.Globals)
755 assignWildcardVersion(Ver, V.Id);
George Rimar3e8a4612017-07-12 13:54:42 +0000756
757 // Symbol themselves might know their versions because symbols
758 // can contain versions in the form of <name>@<version>.
759 // Let them parse and update their names to exclude version suffix.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000760 for (Symbol *Sym : SymVector)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000761 Sym->parseSymbolVersion();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000762}
763
Easwaran Ramanbfa48a12018-01-09 05:35:29 +0000764template void SymbolTable::addFile<ELF32LE>(InputFile *);
765template void SymbolTable::addFile<ELF32BE>(InputFile *);
766template void SymbolTable::addFile<ELF64LE>(InputFile *);
767template void SymbolTable::addFile<ELF64BE>(InputFile *);
768
Rafael Espindolabec37652017-11-17 01:37:50 +0000769template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t,
770 uint8_t, bool, InputFile *);
771template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t,
772 uint8_t, bool, InputFile *);
773template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t,
774 uint8_t, bool, InputFile *);
775template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t,
776 uint8_t, bool, InputFile *);
Rui Ueyama3e96f112017-07-26 21:37:11 +0000777
Rafael Espindola244ef982017-07-26 18:42:48 +0000778template void SymbolTable::addCombinedLTOObject<ELF32LE>();
779template void SymbolTable::addCombinedLTOObject<ELF32BE>();
780template void SymbolTable::addCombinedLTOObject<ELF64LE>();
781template void SymbolTable::addCombinedLTOObject<ELF64BE>();
782
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000783template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000784SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000785 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000786template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000787SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000788 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000789template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000790SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000791 const object::Archive::Symbol);
Peter Collingbourne09e04af2018-02-16 20:23:54 +0000792template void
Rafael Espindola8cd66742017-12-20 17:48:28 +0000793SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000794 const object::Archive::Symbol);
795
Rui Ueyama709fb2bb12017-07-26 22:13:32 +0000796template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
797template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
798template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
799template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
Rafael Espindola244ef982017-07-26 18:42:48 +0000800
Rui Ueyama61b67ab2018-04-03 18:59:31 +0000801template void SymbolTable::fetchLazy<ELF32LE>(Symbol *);
802template void SymbolTable::fetchLazy<ELF32BE>(Symbol *);
803template void SymbolTable::fetchLazy<ELF64LE>(Symbol *);
804template void SymbolTable::fetchLazy<ELF64BE>(Symbol *);
805
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000806template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000807 const typename ELF32LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000808 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000809template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000810 const typename ELF32BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000811 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000812template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000813 const typename ELF64LE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000814 uint32_t Alignment, uint32_t);
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000815template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &,
Rafael Espindola244ef982017-07-26 18:42:48 +0000816 const typename ELF64BE::Sym &,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000817 uint32_t Alignment, uint32_t);