blob: caae09787660fb6462b587517aa94c20e1f10f55 [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,
239 bool IsUsedInRegularObj, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000240 Symbol *S;
241 bool WasInserted;
242 std::tie(S, WasInserted) = insert(Name);
243
Davide Italiano35af5b32016-08-30 20:15:03 +0000244 // Merge in the new unnamed_addr attribute.
245 S->HasUnnamedAddr &= HasUnnamedAddr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000246 // Merge in the new symbol's visibility.
247 S->Visibility = getMinVisibility(S->Visibility, Visibility);
248 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
249 S->ExportDynamic = true;
250 if (IsUsedInRegularObj)
251 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000252 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
253 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000254 error("TLS attribute mismatch for symbol: " +
255 conflictMsg(S->body(), File));
256
257 return {S, WasInserted};
258}
259
260// Construct a string in the form of "Sym in File1 and File2".
261// Used to construct an error message.
262template <typename ELFT>
263std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
264 InputFile *NewFile) {
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000265 std::string Sym = Existing->getName();
266 if (Config->Demangle)
267 Sym = demangle(Sym);
Rui Ueyama434b5612016-07-17 03:11:46 +0000268 return Sym + " in " + getFilename(Existing->File) + " and " +
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000269 getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000270}
271
272template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
273 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Davide Italiano35af5b32016-08-30 20:15:03 +0000274 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
275 /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000276}
277
278template <class ELFT>
279Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
280 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000281 bool CanOmitFromDynSym,
Davide Italiano35af5b32016-08-30 20:15:03 +0000282 bool HasUnnamedAddr, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000283 Symbol *S;
284 bool WasInserted;
285 std::tie(S, WasInserted) =
Davide Italiano35af5b32016-08-30 20:15:03 +0000286 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000287 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
288 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;
352 std::tie(S, WasInserted) =
Davide Italiano35af5b32016-08-30 20:15:03 +0000353 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, HasUnnamedAddr,
Rafael Espindola8db87292016-08-31 13:42:08 +0000354 !isa<BitcodeFile>(File), File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000355 int Cmp = compareDefined(S, WasInserted, Binding);
356 if (Cmp > 0) {
357 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000358 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000359 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000360 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000361 if (!C) {
362 // Non-common symbols take precedence over common symbols.
363 if (Config->WarnCommon)
364 warning("common " + S->body()->getName() + " is overridden");
365 return S;
366 }
367
368 if (Config->WarnCommon)
369 warning("multiple common of " + S->body()->getName());
370
Rafael Espindola8db87292016-08-31 13:42:08 +0000371 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
372 if (Size > C->Size)
373 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000374 }
375 return S;
376}
377
378template <class ELFT>
379void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
380 InputFile *NewFile) {
381 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
382 if (Config->AllowMultipleDefinition)
383 warning(Msg);
384 else
385 error(Msg);
386}
387
388template <typename ELFT>
389Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
390 InputSectionBase<ELFT> *Section) {
391 Symbol *S;
392 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000393 std::tie(S, WasInserted) = insert(
394 Name, Sym.getType(), Sym.getVisibility(),
395 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
396 /*IsUsedInRegularObj*/ true, Section ? Section->getFile() : nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000397 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000398 if (Cmp > 0)
399 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
400 else if (Cmp == 0)
401 reportDuplicate(S->body(), Section->getFile());
402 return S;
403}
404
405template <typename ELFT>
406Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
407 uint8_t StOther) {
408 Symbol *S;
409 bool WasInserted;
410 std::tie(S, WasInserted) =
411 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
Davide Italiano35af5b32016-08-30 20:15:03 +0000412 /*HasUnnamedAddr*/ false, /*IsUsedInRegularObj*/ true, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000413 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000414 if (Cmp > 0)
415 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
416 else if (Cmp == 0)
417 reportDuplicate(S->body(), nullptr);
418 return S;
419}
420
421template <typename ELFT>
422Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000423 OutputSectionBase<ELFT> *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000424 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000425 Symbol *S;
426 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000427 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
428 /*CanOmitFromDynSym*/ false,
Davide Italiano35af5b32016-08-30 20:15:03 +0000429 /*HasUnnamedAddr*/ false,
George Rimare1937bb2016-08-19 15:36:32 +0000430 /*IsUsedInRegularObj*/ true, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000431 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000432 if (Cmp > 0)
433 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
434 else if (Cmp == 0)
435 reportDuplicate(S->body(), nullptr);
436 return S;
437}
438
439template <typename ELFT>
440void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
441 const Elf_Sym &Sym,
442 const typename ELFT::Verdef *Verdef) {
443 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
444 // as the visibility, which will leave the visibility in the symbol table
445 // unchanged.
446 Symbol *S;
447 bool WasInserted;
448 std::tie(S, WasInserted) =
449 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
Davide Italiano35af5b32016-08-30 20:15:03 +0000450 /*HasUnnamedAddr*/ false, /*IsUsedInRegularObj*/ false, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000451 // Make sure we preempt DSO symbols with default visibility.
452 if (Sym.getVisibility() == STV_DEFAULT)
453 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000454 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000455 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000456 if (!S->isWeak())
457 F->IsUsed = true;
458 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000459}
460
461template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000462Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000463 uint8_t StOther, uint8_t Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000464 bool CanOmitFromDynSym,
465 bool HasUnnamedAddr, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000466 Symbol *S;
467 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000468 std::tie(S, WasInserted) =
469 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr,
470 /*IsUsedInRegularObj*/ false, F);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000471 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000472 if (Cmp > 0)
Rafael Espindolaa6c97442016-08-31 12:30:34 +0000473 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000474 else if (Cmp == 0)
475 reportDuplicate(S->body(), F);
476 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000477}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000478
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000479template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
480 auto It = Symtab.find(Name);
481 if (It == Symtab.end())
482 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000483 SymIndex V = It->second;
484 if (V.Idx == -1)
485 return nullptr;
486 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000487}
488
Rui Ueyama48e42512016-06-29 04:47:39 +0000489// Returns a list of defined symbols that match with a given glob pattern.
Rui Ueyama3d451792015-10-12 18:03:21 +0000490template <class ELFT>
Davide Italiano8e1131d2016-06-29 02:46:51 +0000491std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000492 std::vector<SymbolBody *> Res;
Rui Ueyamad6328522016-07-18 01:34:57 +0000493 for (Symbol *Sym : SymVector) {
494 SymbolBody *B = Sym->body();
495 if (!B->isUndefined() && globMatch(Pattern, B->getName()))
Rui Ueyama48e42512016-06-29 04:47:39 +0000496 Res.push_back(B);
497 }
498 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000499}
500
501template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000502void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
503 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000504 Symbol *S;
505 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000506 StringRef Name = Sym.getName();
507 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000508 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000509 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000510 return;
511 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000512 if (!S->body()->isUndefined())
513 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000514
Peter Collingbourne4f952702016-05-01 04:55:03 +0000515 // Weak undefined symbols should not fetch members from archives. If we were
516 // to keep old symbol we would not know that an archive member was available
517 // if a strong undefined symbol shows up afterwards in the link. If a strong
518 // undefined symbol never shows up, this lazy symbol will get to the end of
519 // the link and must be treated as the weak undefined one. We already marked
520 // this symbol as used when we added it to the symbol table, but we also need
521 // to preserve its type. FIXME: Move the Type field to Symbol.
522 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000523 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000524 return;
525 }
526 MemoryBufferRef MBRef = F->getMember(&Sym);
527 if (!MBRef.getBuffer().empty())
528 addFile(createObjectFile(MBRef, F->getName()));
529}
530
531template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000532void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000533 Symbol *S;
534 bool WasInserted;
535 std::tie(S, WasInserted) = insert(Name);
536 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000537 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000538 return;
539 }
540 if (!S->body()->isUndefined())
541 return;
542
543 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000544 if (S->isWeak()) {
545 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
546 } else {
547 MemoryBufferRef MBRef = Obj.getBuffer();
548 if (!MBRef.getBuffer().empty())
549 addFile(createObjectFile(MBRef));
550 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000551}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000552
Peter Collingbourne892d49802016-04-27 00:05:03 +0000553// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000554template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000555 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000556 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama434b5612016-07-17 03:11:46 +0000557 if (std::unique_ptr<InputFile> File = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000558 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000559}
560
Rui Ueyama93bfee52015-10-13 18:10:33 +0000561// This function takes care of the case in which shared libraries depend on
562// the user program (not the other way, which is usual). Shared libraries
563// may have undefined symbols, expecting that the user program provides
564// the definitions for them. An example is BSD's __progname symbol.
565// We need to put such symbols to the main program's .dynsym so that
566// shared libraries can find them.
567// Except this, we ignore undefined symbols in DSOs.
568template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000569 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
570 for (StringRef U : File->getUndefinedSymbols())
571 if (SymbolBody *Sym = find(U))
572 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000573 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000574}
575
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000576// This function process the dynamic list option by marking all the symbols
577// to be exported in the dynamic table.
578template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
579 for (StringRef S : Config->DynamicList)
580 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000581 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000582}
583
George Rimarf73a2582016-07-07 07:45:27 +0000584static bool hasWildcard(StringRef S) {
585 return S.find_first_of("?*") != StringRef::npos;
586}
587
George Rimar50dcece2016-07-16 12:26:39 +0000588static void setVersionId(SymbolBody *Body, StringRef VersionName,
589 StringRef Name, uint16_t Version) {
590 if (!Body || Body->isUndefined()) {
591 if (Config->NoUndefinedVersion)
592 error("version script assignment of " + VersionName + " to symbol " +
593 Name + " failed: symbol not defined");
594 return;
595 }
596
597 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000598 if (Sym->VersionId != Config->DefaultSymbolVersion)
George Rimar50dcece2016-07-16 12:26:39 +0000599 warning("duplicate symbol " + Name + " in version script");
600 Sym->VersionId = Version;
601}
602
603template <class ELFT>
604std::map<std::string, SymbolBody *> SymbolTable<ELFT>::getDemangledSyms() {
605 std::map<std::string, SymbolBody *> Result;
Rui Ueyamad6328522016-07-18 01:34:57 +0000606 for (Symbol *Sym : SymVector) {
607 SymbolBody *B = Sym->body();
608 Result[demangle(B->getName())] = B;
609 }
George Rimar50dcece2016-07-16 12:26:39 +0000610 return Result;
611}
612
613static bool hasExternCpp() {
614 for (VersionDefinition &V : Config->VersionDefinitions)
615 for (SymbolVersion Sym : V.Globals)
616 if (Sym.IsExternCpp)
617 return true;
618 return false;
619}
620
George Rimarc3ec9d02016-08-30 09:29:37 +0000621static SymbolBody *findDemangled(const std::map<std::string, SymbolBody *> &D,
622 StringRef Name) {
623 auto I = D.find(Name);
624 if (I != D.end())
625 return I->second;
626 return nullptr;
627}
628
George Rimar397cd87a2016-08-30 09:35:03 +0000629static std::vector<SymbolBody *>
630findAllDemangled(const std::map<std::string, SymbolBody *> &D,
631 StringRef Pattern) {
632 std::vector<SymbolBody *> Res;
633 for (auto &P : D) {
634 SymbolBody *Body = P.second;
635 if (!Body->isUndefined() && globMatch(Pattern, P.first))
636 Res.push_back(Body);
637 }
638 return Res;
639}
640
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000641// This function processes the --version-script option by marking all global
642// symbols with the VersionScriptGlobal flag, which acts as a filter on the
643// dynamic symbol table.
644template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
George Rimard3566302016-06-20 11:55:12 +0000645 // If version script does not contain versions declarations,
646 // we just should mark global symbols.
647 if (!Config->VersionScriptGlobals.empty()) {
George Rimar50dcece2016-07-16 12:26:39 +0000648 for (SymbolVersion &Sym : Config->VersionScriptGlobals)
649 if (SymbolBody *B = find(Sym.Name))
George Rimard3566302016-06-20 11:55:12 +0000650 B->symbol()->VersionId = VER_NDX_GLOBAL;
651 return;
652 }
653
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000654 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000655 return;
656
George Rimard3566302016-06-20 11:55:12 +0000657 // If we have symbols version declarations, we should
658 // assign version references for each symbol.
George Rimarf73a2582016-07-07 07:45:27 +0000659 // Current rules are:
George Rimar50dcece2016-07-16 12:26:39 +0000660 // * If there is an exact match for the mangled name or we have extern C++
661 // exact match, then we use it.
George Rimarf73a2582016-07-07 07:45:27 +0000662 // * Otherwise, we look through the wildcard patterns. We look through the
663 // version tags in reverse order. We use the first match we find (the last
664 // matching version tag in the file).
George Rimar50dcece2016-07-16 12:26:39 +0000665 // Handle exact matches and build a map of demangled externs for
666 // quick search during next step.
667 std::map<std::string, SymbolBody *> Demangled;
668 if (hasExternCpp())
669 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000670
George Rimar50dcece2016-07-16 12:26:39 +0000671 for (VersionDefinition &V : Config->VersionDefinitions) {
672 for (SymbolVersion Sym : V.Globals) {
673 if (hasWildcard(Sym.Name))
George Rimardd64bb32016-07-13 08:19:04 +0000674 continue;
George Rimarc3ec9d02016-08-30 09:29:37 +0000675 StringRef N = Sym.Name;
676 SymbolBody *B = Sym.IsExternCpp ? findDemangled(Demangled, N) : find(N);
677 setVersionId(B, V.Name, N, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000678 }
George Rimarf73a2582016-07-07 07:45:27 +0000679 }
680
George Rimar50dcece2016-07-16 12:26:39 +0000681 // Handle wildcards.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000682 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
683 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimar7af64522016-08-30 09:39:36 +0000684 for (SymbolVersion &Sym : V.Globals) {
685 if (!hasWildcard(Sym.Name))
686 continue;
687 std::vector<SymbolBody *> All =
688 Sym.IsExternCpp ? findAllDemangled(Demangled, Sym.Name)
689 : findAll(Sym.Name);
George Rimar397cd87a2016-08-30 09:35:03 +0000690
George Rimar7af64522016-08-30 09:39:36 +0000691 for (SymbolBody *B : All)
692 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
693 B->symbol()->VersionId = V.Id;
694 }
George Rimard3566302016-06-20 11:55:12 +0000695 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000696}
697
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000698template class elf::SymbolTable<ELF32LE>;
699template class elf::SymbolTable<ELF32BE>;
700template class elf::SymbolTable<ELF64LE>;
701template class elf::SymbolTable<ELF64BE>;