blob: 152881dcb8255e0e0dda2a0068d9237282f5d083 [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"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000019#include "Error.h"
Davide Italiano8e1131d2016-06-29 02:46:51 +000020#include "LinkerScript.h"
Rui Ueyama93c9af42016-06-29 08:01:32 +000021#include "Strings.h"
George Rimar7899d482016-07-12 07:44:40 +000022#include "SymbolListFile.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023#include "Symbols.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000024#include "llvm/Bitcode/ReaderWriter.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000025#include "llvm/Support/StringSaver.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
Rui Ueyamac9559d92016-01-05 20:47:37 +000034// All input object files must be for the same architecture
35// (e.g. it does not make sense to link x86 object files with
36// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000037template <class ELFT> static bool isCompatible(InputFile *F) {
38 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000039 return true;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000040 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000041 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000042 StringRef A = F->getName();
43 StringRef B = Config->Emulation;
44 if (B.empty())
45 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000046 error(A + " is incompatible with " + B);
47 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000048}
49
Rui Ueyamac9559d92016-01-05 20:47:37 +000050// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000051template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000052void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000053 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000054 if (!isCompatible<ELFT>(FileP))
55 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000056
Rui Ueyama89575742015-12-16 22:59:13 +000057 // .a file
58 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000059 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000060 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000061 return;
62 }
Rui Ueyama3d451792015-10-12 18:03:21 +000063
George Rimar2a78fce2016-04-13 18:07:57 +000064 // Lazy object file
65 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
66 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000067 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000068 return;
69 }
70
71 if (Config->Trace)
Rui Ueyama818bb2f2016-07-16 18:55:47 +000072 outs() << getFilename(FileP) << "\n";
George Rimar2a78fce2016-04-13 18:07:57 +000073
Rui Ueyama89575742015-12-16 22:59:13 +000074 // .so file
75 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
76 // DSOs are uniquified not by filename but by soname.
77 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000078 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000079 return;
Rui Ueyama89575742015-12-16 22:59:13 +000080
Rafael Espindola21f7bd42015-12-23 14:35:51 +000081 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000082 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000083 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000084 }
Rui Ueyama89575742015-12-16 22:59:13 +000085
Rui Ueyamaf8baa662016-04-07 19:24:51 +000086 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000087 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
88 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000089 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000090 return;
91 }
92
Rui Ueyamaf8baa662016-04-07 19:24:51 +000093 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +000094 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000095 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000096 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +000097}
98
Rui Ueyama42554752016-04-23 00:26:32 +000099// This function is where all the optimizations of link-time
100// optimization happens. When LTO is in use, some input files are
101// not in native object file format but in the LLVM bitcode format.
102// This function compiles bitcode files into a few big native files
103// using LLVM functions and replaces bitcode symbols with the results.
104// Because all bitcode files that consist of a program are passed
105// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000106template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
107 if (BitcodeFiles.empty())
108 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000109
110 // Compile bitcode files.
111 Lto.reset(new BitcodeCompiler);
112 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
113 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000114 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000115
116 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000117 for (auto &IF : IFs) {
118 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
119
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000120 DenseSet<StringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000121 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000122 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000123 }
124}
125
Rafael Espindola0e604f92015-09-25 18:56:53 +0000126template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000127DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
128 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000129 return cast<DefinedRegular<ELFT>>(
130 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000131}
132
Rui Ueyamac9559d92016-01-05 20:47:37 +0000133// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000134// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000135template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000136DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
137 uint8_t Visibility) {
138 if (!find(Name))
139 return nullptr;
140 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000141}
142
Rui Ueyama69c778c2016-07-17 17:50:09 +0000143// Set a flag for --trace-symbol so that we can print out a log message
144// if a new symbol with the same name is inserted into the symbol table.
145template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Rui Ueyamae3357902016-07-18 01:35:00 +0000146 Symtab.insert({Name, {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000147}
148
Rui Ueyamadeb15402016-01-07 17:20:07 +0000149// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
150// Used to implement --wrap.
151template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000152 SymbolBody *B = find(Name);
153 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000154 return;
155 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000156 Symbol *Sym = B->symbol();
157 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
158 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
159 // We rename symbols by replacing the old symbol's SymbolBody with the new
160 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
161 // old symbol to instead refer to the new symbol.
162 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
163 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000164}
165
Peter Collingbournedadcc172016-04-22 18:42:48 +0000166static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
167 if (VA == STV_DEFAULT)
168 return VB;
169 if (VB == STV_DEFAULT)
170 return VA;
171 return std::min(VA, VB);
172}
173
Rui Ueyamadace8382016-07-21 13:13:21 +0000174// Parses a symbol in the form of <name>@<version> or <name>@@<version>.
175static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) {
176 if (Config->VersionDefinitions.empty())
177 return {S, Config->DefaultSymbolVersion};
178
179 size_t Pos = S.find('@');
180 if (Pos == 0 || Pos == StringRef::npos)
181 return {S, Config->DefaultSymbolVersion};
182
183 StringRef Name = S.substr(0, Pos);
184 StringRef Verstr = S.substr(Pos + 1);
185 if (Verstr.empty())
186 return {S, Config->DefaultSymbolVersion};
187
188 // '@@' in a symbol name means the default version.
189 // It is usually the most recent one.
190 bool IsDefault = (Verstr[0] == '@');
191 if (IsDefault)
192 Verstr = Verstr.substr(1);
193
194 for (VersionDefinition &V : Config->VersionDefinitions) {
195 if (V.Name == Verstr)
196 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)};
197 }
198
199 // It is an error if the specified version was not defined.
200 error("symbol " + S + " has undefined version " + Verstr);
201 return {S, Config->DefaultSymbolVersion};
202}
203
Rui Ueyamab4de5952016-01-08 22:01:33 +0000204// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000205template <class ELFT>
Rui Ueyamadace8382016-07-21 13:13:21 +0000206std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) {
George Rimarb0841252016-07-20 14:26:48 +0000207 auto P = Symtab.insert({Name, SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000208 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000209 bool IsNew = P.second;
210
Rui Ueyamae3357902016-07-18 01:35:00 +0000211 if (V.Idx == -1) {
212 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000213 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000214 }
215
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000216 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000217 if (IsNew) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000218 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000219 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000220 Sym->Visibility = STV_DEFAULT;
221 Sym->IsUsedInRegularObj = false;
Davide Italiano35af5b32016-08-30 20:15:03 +0000222 Sym->HasUnnamedAddr = true;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000223 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000224 Sym->Traced = V.Traced;
Rui Ueyamadace8382016-07-21 13:13:21 +0000225 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000226 SymVector.push_back(Sym);
227 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000228 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000229 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000230 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000231}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000232
Peter Collingbourne4f952702016-05-01 04:55:03 +0000233// Find an existing symbol or create and insert a new one, then apply the given
234// attributes.
235template <class ELFT>
236std::pair<Symbol *, bool>
Rui Ueyamadace8382016-07-21 13:13:21 +0000237SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
Davide Italiano35af5b32016-08-30 20:15:03 +0000238 bool CanOmitFromDynSym, bool HasUnnamedAddr,
Rafael Espindola05098762016-08-31 13:49:23 +0000239 InputFile *File) {
240 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000241 Symbol *S;
242 bool WasInserted;
243 std::tie(S, WasInserted) = insert(Name);
244
Davide Italiano35af5b32016-08-30 20:15:03 +0000245 // Merge in the new unnamed_addr attribute.
246 S->HasUnnamedAddr &= HasUnnamedAddr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000247 // Merge in the new symbol's visibility.
248 S->Visibility = getMinVisibility(S->Visibility, Visibility);
249 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
250 S->ExportDynamic = true;
251 if (IsUsedInRegularObj)
252 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000253 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
254 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000255 error("TLS attribute mismatch for symbol: " +
256 conflictMsg(S->body(), File));
257
258 return {S, WasInserted};
259}
260
261// Construct a string in the form of "Sym in File1 and File2".
262// Used to construct an error message.
263template <typename ELFT>
264std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
265 InputFile *NewFile) {
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000266 std::string Sym = Existing->getName();
267 if (Config->Demangle)
268 Sym = demangle(Sym);
Rui Ueyama434b5612016-07-17 03:11:46 +0000269 return Sym + " in " + getFilename(Existing->File) + " and " +
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000270 getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000271}
272
273template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
274 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Davide Italiano35af5b32016-08-30 20:15:03 +0000275 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
276 /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000277}
278
279template <class ELFT>
280Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
281 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000282 bool CanOmitFromDynSym,
Davide Italiano35af5b32016-08-30 20:15:03 +0000283 bool HasUnnamedAddr, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000284 Symbol *S;
285 bool WasInserted;
286 std::tie(S, WasInserted) =
Rafael Espindola05098762016-08-31 13:49:23 +0000287 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000288 if (WasInserted) {
289 S->Binding = Binding;
Rui Ueyama434b5612016-07-17 03:11:46 +0000290 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000291 return S;
292 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000293 if (Binding != STB_WEAK) {
294 if (S->body()->isShared() || S->body()->isLazy())
295 S->Binding = Binding;
296 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000297 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000298 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000299 if (auto *L = dyn_cast<Lazy>(S->body())) {
300 // An undefined weak will not fetch archive members, but we have to remember
301 // its type. See also comment in addLazyArchive.
302 if (S->isWeak())
303 L->Type = Type;
Rui Ueyama434b5612016-07-17 03:11:46 +0000304 else if (auto F = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000305 addFile(std::move(F));
306 }
307 return S;
308}
309
310// We have a new defined symbol with the specified binding. Return 1 if the new
311// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
312// strong defined symbols.
313static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
314 if (WasInserted)
315 return 1;
316 SymbolBody *Body = S->body();
317 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
318 return 1;
319 if (Binding == STB_WEAK)
320 return -1;
321 if (S->isWeak())
322 return 1;
323 return 0;
324}
325
326// We have a new non-common defined symbol with the specified binding. Return 1
327// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
328// is a conflict. If the new symbol wins, also update the binding.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000329static int compareDefinedNonCommon(Symbol *S, bool WasInserted,
330 uint8_t Binding) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000331 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
332 if (Cmp > 0)
333 S->Binding = Binding;
334 return Cmp;
335 }
Rafael Espindolae7553e42016-08-31 13:28:33 +0000336 if (isa<DefinedCommon>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000337 // Non-common symbols take precedence over common symbols.
338 if (Config->WarnCommon)
339 warning("common " + S->body()->getName() + " is overridden");
340 return 1;
341 }
342 return 0;
343}
344
345template <class ELFT>
346Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
347 uint64_t Alignment, uint8_t Binding,
348 uint8_t StOther, uint8_t Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000349 bool HasUnnamedAddr, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000350 Symbol *S;
351 bool WasInserted;
Rafael Espindola05098762016-08-31 13:49:23 +0000352 std::tie(S, WasInserted) = insert(
353 N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, HasUnnamedAddr, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000354 int Cmp = compareDefined(S, WasInserted, Binding);
355 if (Cmp > 0) {
356 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000357 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000358 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000359 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000360 if (!C) {
361 // Non-common symbols take precedence over common symbols.
362 if (Config->WarnCommon)
363 warning("common " + S->body()->getName() + " is overridden");
364 return S;
365 }
366
367 if (Config->WarnCommon)
368 warning("multiple common of " + S->body()->getName());
369
Rafael Espindola8db87292016-08-31 13:42:08 +0000370 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
371 if (Size > C->Size)
372 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000373 }
374 return S;
375}
376
377template <class ELFT>
378void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
379 InputFile *NewFile) {
380 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
381 if (Config->AllowMultipleDefinition)
382 warning(Msg);
383 else
384 error(Msg);
385}
386
387template <typename ELFT>
388Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
389 InputSectionBase<ELFT> *Section) {
390 Symbol *S;
391 bool WasInserted;
Rafael Espindola05098762016-08-31 13:49:23 +0000392 std::tie(S, WasInserted) =
393 insert(Name, Sym.getType(), Sym.getVisibility(),
394 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
395 Section ? Section->getFile() : nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000396 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000397 if (Cmp > 0)
398 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
399 else if (Cmp == 0)
400 reportDuplicate(S->body(), Section->getFile());
401 return S;
402}
403
404template <typename ELFT>
405Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
406 uint8_t StOther) {
407 Symbol *S;
408 bool WasInserted;
409 std::tie(S, WasInserted) =
410 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
Rafael Espindola05098762016-08-31 13:49:23 +0000411 /*HasUnnamedAddr*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000412 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000413 if (Cmp > 0)
414 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
415 else if (Cmp == 0)
416 reportDuplicate(S->body(), nullptr);
417 return S;
418}
419
420template <typename ELFT>
421Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000422 OutputSectionBase<ELFT> *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000423 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000424 Symbol *S;
425 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000426 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
427 /*CanOmitFromDynSym*/ false,
Rafael Espindola05098762016-08-31 13:49:23 +0000428 /*HasUnnamedAddr*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000429 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000430 if (Cmp > 0)
431 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
432 else if (Cmp == 0)
433 reportDuplicate(S->body(), nullptr);
434 return S;
435}
436
437template <typename ELFT>
438void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
439 const Elf_Sym &Sym,
440 const typename ELFT::Verdef *Verdef) {
441 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
442 // as the visibility, which will leave the visibility in the symbol table
443 // unchanged.
444 Symbol *S;
445 bool WasInserted;
446 std::tie(S, WasInserted) =
447 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
Rafael Espindola05098762016-08-31 13:49:23 +0000448 /*HasUnnamedAddr*/ false, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000449 // Make sure we preempt DSO symbols with default visibility.
450 if (Sym.getVisibility() == STV_DEFAULT)
451 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000452 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000453 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000454 if (!S->isWeak())
455 F->IsUsed = true;
456 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000457}
458
459template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000460Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000461 uint8_t StOther, uint8_t Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000462 bool CanOmitFromDynSym,
463 bool HasUnnamedAddr, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000464 Symbol *S;
465 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000466 std::tie(S, WasInserted) =
Rafael Espindola05098762016-08-31 13:49:23 +0000467 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr, F);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000468 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000469 if (Cmp > 0)
Rafael Espindolaa6c97442016-08-31 12:30:34 +0000470 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000471 else if (Cmp == 0)
472 reportDuplicate(S->body(), F);
473 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000474}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000475
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000476template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
477 auto It = Symtab.find(Name);
478 if (It == Symtab.end())
479 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000480 SymIndex V = It->second;
481 if (V.Idx == -1)
482 return nullptr;
483 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000484}
485
George Rimarc91930a2016-09-02 21:17:20 +0000486// Returns a list of defined symbols that match with a given regex.
Rui Ueyama3d451792015-10-12 18:03:21 +0000487template <class ELFT>
George Rimarc91930a2016-09-02 21:17:20 +0000488std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(const Regex &Re) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000489 std::vector<SymbolBody *> Res;
Rui Ueyamad6328522016-07-18 01:34:57 +0000490 for (Symbol *Sym : SymVector) {
491 SymbolBody *B = Sym->body();
George Rimarc91930a2016-09-02 21:17:20 +0000492 StringRef Name = B->getName();
493 if (!B->isUndefined() && const_cast<Regex &>(Re).match(Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000494 Res.push_back(B);
495 }
496 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000497}
498
499template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000500void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
501 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000502 Symbol *S;
503 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000504 StringRef Name = Sym.getName();
505 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000506 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000507 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000508 return;
509 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000510 if (!S->body()->isUndefined())
511 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000512
Peter Collingbourne4f952702016-05-01 04:55:03 +0000513 // Weak undefined symbols should not fetch members from archives. If we were
514 // to keep old symbol we would not know that an archive member was available
515 // if a strong undefined symbol shows up afterwards in the link. If a strong
516 // undefined symbol never shows up, this lazy symbol will get to the end of
517 // the link and must be treated as the weak undefined one. We already marked
518 // this symbol as used when we added it to the symbol table, but we also need
519 // to preserve its type. FIXME: Move the Type field to Symbol.
520 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000521 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000522 return;
523 }
524 MemoryBufferRef MBRef = F->getMember(&Sym);
525 if (!MBRef.getBuffer().empty())
526 addFile(createObjectFile(MBRef, F->getName()));
527}
528
529template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000530void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000531 Symbol *S;
532 bool WasInserted;
533 std::tie(S, WasInserted) = insert(Name);
534 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000535 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 return;
537 }
538 if (!S->body()->isUndefined())
539 return;
540
541 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000542 if (S->isWeak()) {
543 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
544 } else {
545 MemoryBufferRef MBRef = Obj.getBuffer();
546 if (!MBRef.getBuffer().empty())
547 addFile(createObjectFile(MBRef));
548 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000549}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000550
Peter Collingbourne892d49802016-04-27 00:05:03 +0000551// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000552template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000553 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000554 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama434b5612016-07-17 03:11:46 +0000555 if (std::unique_ptr<InputFile> File = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000556 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000557}
558
Rui Ueyama93bfee52015-10-13 18:10:33 +0000559// This function takes care of the case in which shared libraries depend on
560// the user program (not the other way, which is usual). Shared libraries
561// may have undefined symbols, expecting that the user program provides
562// the definitions for them. An example is BSD's __progname symbol.
563// We need to put such symbols to the main program's .dynsym so that
564// shared libraries can find them.
565// Except this, we ignore undefined symbols in DSOs.
566template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000567 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
568 for (StringRef U : File->getUndefinedSymbols())
569 if (SymbolBody *Sym = find(U))
570 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000571 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000572}
573
Rui Ueyamadad2b882016-09-02 22:15:08 +0000574// This function processes --export-dynamic-symbol and --dynamic-list.
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000575template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
576 for (StringRef S : Config->DynamicList)
577 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000578 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000579}
580
George Rimar50dcece2016-07-16 12:26:39 +0000581static void setVersionId(SymbolBody *Body, StringRef VersionName,
582 StringRef Name, uint16_t Version) {
583 if (!Body || Body->isUndefined()) {
584 if (Config->NoUndefinedVersion)
585 error("version script assignment of " + VersionName + " to symbol " +
586 Name + " failed: symbol not defined");
587 return;
588 }
589
590 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000591 if (Sym->VersionId != Config->DefaultSymbolVersion)
George Rimar50dcece2016-07-16 12:26:39 +0000592 warning("duplicate symbol " + Name + " in version script");
593 Sym->VersionId = Version;
594}
595
596template <class ELFT>
597std::map<std::string, SymbolBody *> SymbolTable<ELFT>::getDemangledSyms() {
598 std::map<std::string, SymbolBody *> Result;
Rui Ueyamad6328522016-07-18 01:34:57 +0000599 for (Symbol *Sym : SymVector) {
600 SymbolBody *B = Sym->body();
601 Result[demangle(B->getName())] = B;
602 }
George Rimar50dcece2016-07-16 12:26:39 +0000603 return Result;
604}
605
606static bool hasExternCpp() {
607 for (VersionDefinition &V : Config->VersionDefinitions)
608 for (SymbolVersion Sym : V.Globals)
609 if (Sym.IsExternCpp)
610 return true;
611 return false;
612}
613
George Rimarc3ec9d02016-08-30 09:29:37 +0000614static SymbolBody *findDemangled(const std::map<std::string, SymbolBody *> &D,
615 StringRef Name) {
616 auto I = D.find(Name);
617 if (I != D.end())
618 return I->second;
619 return nullptr;
620}
621
George Rimar397cd87a2016-08-30 09:35:03 +0000622static std::vector<SymbolBody *>
623findAllDemangled(const std::map<std::string, SymbolBody *> &D,
George Rimarc91930a2016-09-02 21:17:20 +0000624 const Regex &Re) {
George Rimar397cd87a2016-08-30 09:35:03 +0000625 std::vector<SymbolBody *> Res;
626 for (auto &P : D) {
627 SymbolBody *Body = P.second;
George Rimarc91930a2016-09-02 21:17:20 +0000628 if (!Body->isUndefined() && const_cast<Regex &>(Re).match(P.first))
George Rimar397cd87a2016-08-30 09:35:03 +0000629 Res.push_back(Body);
630 }
631 return Res;
632}
633
Rui Ueyamadad2b882016-09-02 22:15:08 +0000634// This function processes version scripts by updating VersionId
635// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000636template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyamadad2b882016-09-02 22:15:08 +0000637 // If there's only one anonymous version definition in a version
638 // script file, the script does not actullay define any symbol version,
639 // but just specifies symbols visibilities. We assume that the script was
640 // in the form of { global: foo; bar; local *; }. So, local is default.
641 // Here, we make specified symbols global.
George Rimard3566302016-06-20 11:55:12 +0000642 if (!Config->VersionScriptGlobals.empty()) {
George Rimar50dcece2016-07-16 12:26:39 +0000643 for (SymbolVersion &Sym : Config->VersionScriptGlobals)
644 if (SymbolBody *B = find(Sym.Name))
George Rimard3566302016-06-20 11:55:12 +0000645 B->symbol()->VersionId = VER_NDX_GLOBAL;
646 return;
647 }
648
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000649 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000650 return;
651
Rui Ueyamadad2b882016-09-02 22:15:08 +0000652 // Now we have version definitions, so we need to set version ids to symbols.
653 // Each version definition has a glob pattern, and all symbols that match
654 // with the pattern get that version.
655
656 // Users can use "extern C++ {}" directive to match against demangled
657 // C++ symbols. For example, you can write a pattern such as
658 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
659 // other than trying to match a regexp against all demangled symbols.
660 // So, if "extern C++" feature is used, we demangle all known symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000661 std::map<std::string, SymbolBody *> Demangled;
662 if (hasExternCpp())
663 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000664
Rui Ueyamadad2b882016-09-02 22:15:08 +0000665 // First, we assign versions to exact matching symbols,
666 // i.e. version definitions not containing any glob meta-characters.
George Rimar50dcece2016-07-16 12:26:39 +0000667 for (VersionDefinition &V : Config->VersionDefinitions) {
668 for (SymbolVersion Sym : V.Globals) {
669 if (hasWildcard(Sym.Name))
George Rimardd64bb32016-07-13 08:19:04 +0000670 continue;
George Rimarc3ec9d02016-08-30 09:29:37 +0000671 StringRef N = Sym.Name;
672 SymbolBody *B = Sym.IsExternCpp ? findDemangled(Demangled, N) : find(N);
673 setVersionId(B, V.Name, N, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000674 }
George Rimarf73a2582016-07-07 07:45:27 +0000675 }
676
Rui Ueyamadad2b882016-09-02 22:15:08 +0000677 // Next, we assign versions to fuzzy matching symbols,
678 // i.e. version definitions containing glob meta-characters.
679 // Note that because the last match takes precedence over previous matches,
680 // we iterate over the definitions in the reverse order.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000681 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
682 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimar7af64522016-08-30 09:39:36 +0000683 for (SymbolVersion &Sym : V.Globals) {
684 if (!hasWildcard(Sym.Name))
685 continue;
Rui Ueyamadad2b882016-09-02 22:15:08 +0000686 Regex Re = compileGlobPatterns({Sym.Name});
687 std::vector<SymbolBody *> Syms =
688 Sym.IsExternCpp ? findAllDemangled(Demangled, Re) : findAll(Re);
George Rimar397cd87a2016-08-30 09:35:03 +0000689
Rui Ueyamadad2b882016-09-02 22:15:08 +0000690 // Exact matching takes precendence over fuzzy matching,
691 // so we set a version to a symbol only if no version has been assigned
692 // to the symbol. This behavior is compatible with GNU.
693 for (SymbolBody *B : Syms)
George Rimar7af64522016-08-30 09:39:36 +0000694 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
695 B->symbol()->VersionId = V.Id;
696 }
George Rimard3566302016-06-20 11:55:12 +0000697 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000698}
699
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000700template class elf::SymbolTable<ELF32LE>;
701template class elf::SymbolTable<ELF32BE>;
702template class elf::SymbolTable<ELF64LE>;
703template class elf::SymbolTable<ELF64BE>;