blob: f758247ac9c452c07775b4c4cc27acb7deccf43e [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"
George Rimar7899d482016-07-12 07:44:40 +000021#include "SymbolListFile.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022#include "Symbols.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000023#include "llvm/Bitcode/ReaderWriter.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000024#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000027using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000028using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rui Ueyamac9559d92016-01-05 20:47:37 +000033// All input object files must be for the same architecture
34// (e.g. it does not make sense to link x86 object files with
35// MIPS object files.) This function checks for that error.
George Rimardbbf60e2016-06-29 09:46:00 +000036template <class ELFT> static bool isCompatible(InputFile *F) {
37 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
Rui Ueyama16ba6692016-01-29 19:41:13 +000038 return true;
Rui Ueyama5e64d3f2016-06-29 01:30:50 +000039 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000040 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000041 StringRef A = F->getName();
42 StringRef B = Config->Emulation;
43 if (B.empty())
44 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000045 error(A + " is incompatible with " + B);
46 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000047}
48
Rui Ueyamac9559d92016-01-05 20:47:37 +000049// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000050template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000051void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000052 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000053 if (!isCompatible<ELFT>(FileP))
54 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000055
Rui Ueyama89575742015-12-16 22:59:13 +000056 // .a file
57 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000058 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000059 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000060 return;
61 }
Rui Ueyama3d451792015-10-12 18:03:21 +000062
George Rimar2a78fce2016-04-13 18:07:57 +000063 // Lazy object file
64 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
65 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000066 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000067 return;
68 }
69
70 if (Config->Trace)
Rui Ueyama818bb2f2016-07-16 18:55:47 +000071 outs() << getFilename(FileP) << "\n";
George Rimar2a78fce2016-04-13 18:07:57 +000072
Rui Ueyama89575742015-12-16 22:59:13 +000073 // .so file
74 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
75 // DSOs are uniquified not by filename but by soname.
76 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000077 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000078 return;
Rui Ueyama89575742015-12-16 22:59:13 +000079
Rafael Espindola21f7bd42015-12-23 14:35:51 +000080 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000081 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000082 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000083 }
Rui Ueyama89575742015-12-16 22:59:13 +000084
Rui Ueyamaf8baa662016-04-07 19:24:51 +000085 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000086 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
87 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000088 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000089 return;
90 }
91
Rui Ueyamaf8baa662016-04-07 19:24:51 +000092 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +000093 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000094 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000095 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +000096}
97
Rui Ueyama42554752016-04-23 00:26:32 +000098// This function is where all the optimizations of link-time
99// optimization happens. When LTO is in use, some input files are
100// not in native object file format but in the LLVM bitcode format.
101// This function compiles bitcode files into a few big native files
102// using LLVM functions and replaces bitcode symbols with the results.
103// Because all bitcode files that consist of a program are passed
104// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000105template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
106 if (BitcodeFiles.empty())
107 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000108
109 // Compile bitcode files.
110 Lto.reset(new BitcodeCompiler);
111 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
112 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000113 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000114
115 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000116 for (auto &IF : IFs) {
117 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
118
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000119 DenseSet<StringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000120 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000121 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000122 }
123}
124
Rafael Espindola0e604f92015-09-25 18:56:53 +0000125template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000126DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
127 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000128 return cast<DefinedRegular<ELFT>>(
129 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000130}
131
Rui Ueyamac9559d92016-01-05 20:47:37 +0000132// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000133// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000134template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000135DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
136 uint8_t Visibility) {
137 if (!find(Name))
138 return nullptr;
139 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000140}
141
Rui Ueyama69c778c2016-07-17 17:50:09 +0000142// Set a flag for --trace-symbol so that we can print out a log message
143// if a new symbol with the same name is inserted into the symbol table.
144template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Rui Ueyamae3357902016-07-18 01:35:00 +0000145 Symtab.insert({Name, {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000146}
147
Rui Ueyamadeb15402016-01-07 17:20:07 +0000148// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
149// Used to implement --wrap.
150template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000151 SymbolBody *B = find(Name);
152 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000153 return;
154 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000155 Symbol *Sym = B->symbol();
156 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
157 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
158 // We rename symbols by replacing the old symbol's SymbolBody with the new
159 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
160 // old symbol to instead refer to the new symbol.
161 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
162 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000163}
164
Peter Collingbournedadcc172016-04-22 18:42:48 +0000165static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
166 if (VA == STV_DEFAULT)
167 return VB;
168 if (VB == STV_DEFAULT)
169 return VA;
170 return std::min(VA, VB);
171}
172
Rui Ueyamadace8382016-07-21 13:13:21 +0000173// Parses a symbol in the form of <name>@<version> or <name>@@<version>.
174static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) {
175 if (Config->VersionDefinitions.empty())
176 return {S, Config->DefaultSymbolVersion};
177
178 size_t Pos = S.find('@');
179 if (Pos == 0 || Pos == StringRef::npos)
180 return {S, Config->DefaultSymbolVersion};
181
182 StringRef Name = S.substr(0, Pos);
183 StringRef Verstr = S.substr(Pos + 1);
184 if (Verstr.empty())
185 return {S, Config->DefaultSymbolVersion};
186
187 // '@@' in a symbol name means the default version.
188 // It is usually the most recent one.
189 bool IsDefault = (Verstr[0] == '@');
190 if (IsDefault)
191 Verstr = Verstr.substr(1);
192
193 for (VersionDefinition &V : Config->VersionDefinitions) {
194 if (V.Name == Verstr)
195 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)};
196 }
197
198 // It is an error if the specified version was not defined.
199 error("symbol " + S + " has undefined version " + Verstr);
200 return {S, Config->DefaultSymbolVersion};
201}
202
Rui Ueyamab4de5952016-01-08 22:01:33 +0000203// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000204template <class ELFT>
Rui Ueyamadace8382016-07-21 13:13:21 +0000205std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) {
George Rimarb0841252016-07-20 14:26:48 +0000206 auto P = Symtab.insert({Name, SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000207 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000208 bool IsNew = P.second;
209
Rui Ueyamae3357902016-07-18 01:35:00 +0000210 if (V.Idx == -1) {
211 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000212 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000213 }
214
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000215 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000216 if (IsNew) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000217 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000218 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000219 Sym->Visibility = STV_DEFAULT;
220 Sym->IsUsedInRegularObj = false;
Davide Italiano35af5b32016-08-30 20:15:03 +0000221 Sym->HasUnnamedAddr = true;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000222 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000223 Sym->Traced = V.Traced;
Rui Ueyamadace8382016-07-21 13:13:21 +0000224 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000225 SymVector.push_back(Sym);
226 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000227 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000228 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000229 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000230}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000231
Peter Collingbourne4f952702016-05-01 04:55:03 +0000232// Find an existing symbol or create and insert a new one, then apply the given
233// attributes.
234template <class ELFT>
235std::pair<Symbol *, bool>
Rui Ueyamadace8382016-07-21 13:13:21 +0000236SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
Davide Italiano35af5b32016-08-30 20:15:03 +0000237 bool CanOmitFromDynSym, bool HasUnnamedAddr,
Rafael Espindola05098762016-08-31 13:49:23 +0000238 InputFile *File) {
239 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
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) =
Rafael Espindola05098762016-08-31 13:49:23 +0000286 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000287 if (WasInserted) {
288 S->Binding = Binding;
Rui Ueyama434b5612016-07-17 03:11:46 +0000289 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000290 return S;
291 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000292 if (Binding != STB_WEAK) {
293 if (S->body()->isShared() || S->body()->isLazy())
294 S->Binding = Binding;
295 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000296 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000297 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000298 if (auto *L = dyn_cast<Lazy>(S->body())) {
299 // An undefined weak will not fetch archive members, but we have to remember
300 // its type. See also comment in addLazyArchive.
301 if (S->isWeak())
302 L->Type = Type;
Rui Ueyama434b5612016-07-17 03:11:46 +0000303 else if (auto F = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000304 addFile(std::move(F));
305 }
306 return S;
307}
308
309// We have a new defined symbol with the specified binding. Return 1 if the new
310// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
311// strong defined symbols.
312static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
313 if (WasInserted)
314 return 1;
315 SymbolBody *Body = S->body();
316 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
317 return 1;
318 if (Binding == STB_WEAK)
319 return -1;
320 if (S->isWeak())
321 return 1;
322 return 0;
323}
324
325// We have a new non-common defined symbol with the specified binding. Return 1
326// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
327// is a conflict. If the new symbol wins, also update the binding.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000328static int compareDefinedNonCommon(Symbol *S, bool WasInserted,
329 uint8_t Binding) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000330 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
331 if (Cmp > 0)
332 S->Binding = Binding;
333 return Cmp;
334 }
Rafael Espindolae7553e42016-08-31 13:28:33 +0000335 if (isa<DefinedCommon>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000336 // Non-common symbols take precedence over common symbols.
337 if (Config->WarnCommon)
338 warning("common " + S->body()->getName() + " is overridden");
339 return 1;
340 }
341 return 0;
342}
343
344template <class ELFT>
345Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
346 uint64_t Alignment, uint8_t Binding,
347 uint8_t StOther, uint8_t Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000348 bool HasUnnamedAddr, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000349 Symbol *S;
350 bool WasInserted;
Rafael Espindola05098762016-08-31 13:49:23 +0000351 std::tie(S, WasInserted) = insert(
352 N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, HasUnnamedAddr, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000353 int Cmp = compareDefined(S, WasInserted, Binding);
354 if (Cmp > 0) {
355 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000356 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000357 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000358 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000359 if (!C) {
360 // Non-common symbols take precedence over common symbols.
361 if (Config->WarnCommon)
362 warning("common " + S->body()->getName() + " is overridden");
363 return S;
364 }
365
366 if (Config->WarnCommon)
367 warning("multiple common of " + S->body()->getName());
368
Rafael Espindola8db87292016-08-31 13:42:08 +0000369 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
370 if (Size > C->Size)
371 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000372 }
373 return S;
374}
375
376template <class ELFT>
377void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
378 InputFile *NewFile) {
379 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
380 if (Config->AllowMultipleDefinition)
381 warning(Msg);
382 else
383 error(Msg);
384}
385
386template <typename ELFT>
387Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
388 InputSectionBase<ELFT> *Section) {
389 Symbol *S;
390 bool WasInserted;
Rafael Espindola05098762016-08-31 13:49:23 +0000391 std::tie(S, WasInserted) =
392 insert(Name, Sym.getType(), Sym.getVisibility(),
393 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
394 Section ? Section->getFile() : nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000395 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000396 if (Cmp > 0)
397 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
398 else if (Cmp == 0)
399 reportDuplicate(S->body(), Section->getFile());
400 return S;
401}
402
403template <typename ELFT>
404Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
405 uint8_t StOther) {
406 Symbol *S;
407 bool WasInserted;
408 std::tie(S, WasInserted) =
409 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
Rafael Espindola05098762016-08-31 13:49:23 +0000410 /*HasUnnamedAddr*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000411 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000412 if (Cmp > 0)
413 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
414 else if (Cmp == 0)
415 reportDuplicate(S->body(), nullptr);
416 return S;
417}
418
419template <typename ELFT>
420Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000421 OutputSectionBase<ELFT> *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000422 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000423 Symbol *S;
424 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000425 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
426 /*CanOmitFromDynSym*/ false,
Rafael Espindola05098762016-08-31 13:49:23 +0000427 /*HasUnnamedAddr*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000428 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000429 if (Cmp > 0)
430 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
431 else if (Cmp == 0)
432 reportDuplicate(S->body(), nullptr);
433 return S;
434}
435
436template <typename ELFT>
437void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
438 const Elf_Sym &Sym,
439 const typename ELFT::Verdef *Verdef) {
440 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
441 // as the visibility, which will leave the visibility in the symbol table
442 // unchanged.
443 Symbol *S;
444 bool WasInserted;
445 std::tie(S, WasInserted) =
446 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
Rafael Espindola05098762016-08-31 13:49:23 +0000447 /*HasUnnamedAddr*/ false, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000448 // Make sure we preempt DSO symbols with default visibility.
449 if (Sym.getVisibility() == STV_DEFAULT)
450 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000451 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000452 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000453 if (!S->isWeak())
454 F->IsUsed = true;
455 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000456}
457
458template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000459Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000460 uint8_t StOther, uint8_t Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000461 bool CanOmitFromDynSym,
462 bool HasUnnamedAddr, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000463 Symbol *S;
464 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000465 std::tie(S, WasInserted) =
Rafael Espindola05098762016-08-31 13:49:23 +0000466 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr, F);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000467 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000468 if (Cmp > 0)
Rafael Espindolaa6c97442016-08-31 12:30:34 +0000469 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000470 else if (Cmp == 0)
471 reportDuplicate(S->body(), F);
472 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000473}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000474
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000475template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
476 auto It = Symtab.find(Name);
477 if (It == Symtab.end())
478 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000479 SymIndex V = It->second;
480 if (V.Idx == -1)
481 return nullptr;
482 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000483}
484
George Rimarc91930a2016-09-02 21:17:20 +0000485// Returns a list of defined symbols that match with a given regex.
Rui Ueyama3d451792015-10-12 18:03:21 +0000486template <class ELFT>
George Rimarc91930a2016-09-02 21:17:20 +0000487std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(const Regex &Re) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000488 std::vector<SymbolBody *> Res;
Rui Ueyamad6328522016-07-18 01:34:57 +0000489 for (Symbol *Sym : SymVector) {
490 SymbolBody *B = Sym->body();
George Rimarc91930a2016-09-02 21:17:20 +0000491 StringRef Name = B->getName();
492 if (!B->isUndefined() && const_cast<Regex &>(Re).match(Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000493 Res.push_back(B);
494 }
495 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000496}
497
498template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000499void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
500 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000501 Symbol *S;
502 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000503 StringRef Name = Sym.getName();
504 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000505 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000506 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000507 return;
508 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000509 if (!S->body()->isUndefined())
510 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000511
Peter Collingbourne4f952702016-05-01 04:55:03 +0000512 // Weak undefined symbols should not fetch members from archives. If we were
513 // to keep old symbol we would not know that an archive member was available
514 // if a strong undefined symbol shows up afterwards in the link. If a strong
515 // undefined symbol never shows up, this lazy symbol will get to the end of
516 // the link and must be treated as the weak undefined one. We already marked
517 // this symbol as used when we added it to the symbol table, but we also need
518 // to preserve its type. FIXME: Move the Type field to Symbol.
519 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000520 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000521 return;
522 }
523 MemoryBufferRef MBRef = F->getMember(&Sym);
524 if (!MBRef.getBuffer().empty())
525 addFile(createObjectFile(MBRef, F->getName()));
526}
527
528template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000529void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000530 Symbol *S;
531 bool WasInserted;
532 std::tie(S, WasInserted) = insert(Name);
533 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000534 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000535 return;
536 }
537 if (!S->body()->isUndefined())
538 return;
539
540 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000541 if (S->isWeak()) {
542 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
543 } else {
544 MemoryBufferRef MBRef = Obj.getBuffer();
545 if (!MBRef.getBuffer().empty())
546 addFile(createObjectFile(MBRef));
547 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000548}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000549
Peter Collingbourne892d49802016-04-27 00:05:03 +0000550// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000551template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000552 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000553 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama434b5612016-07-17 03:11:46 +0000554 if (std::unique_ptr<InputFile> File = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000555 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000556}
557
Rui Ueyama93bfee52015-10-13 18:10:33 +0000558// This function takes care of the case in which shared libraries depend on
559// the user program (not the other way, which is usual). Shared libraries
560// may have undefined symbols, expecting that the user program provides
561// the definitions for them. An example is BSD's __progname symbol.
562// We need to put such symbols to the main program's .dynsym so that
563// shared libraries can find them.
564// Except this, we ignore undefined symbols in DSOs.
565template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000566 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
567 for (StringRef U : File->getUndefinedSymbols())
568 if (SymbolBody *Sym = find(U))
569 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000570 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000571}
572
Rui Ueyamadad2b882016-09-02 22:15:08 +0000573// This function processes --export-dynamic-symbol and --dynamic-list.
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000574template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
575 for (StringRef S : Config->DynamicList)
576 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000577 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000578}
579
George Rimar50dcece2016-07-16 12:26:39 +0000580static void setVersionId(SymbolBody *Body, StringRef VersionName,
581 StringRef Name, uint16_t Version) {
582 if (!Body || Body->isUndefined()) {
583 if (Config->NoUndefinedVersion)
584 error("version script assignment of " + VersionName + " to symbol " +
585 Name + " failed: symbol not defined");
586 return;
587 }
588
589 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000590 if (Sym->VersionId != Config->DefaultSymbolVersion)
George Rimar50dcece2016-07-16 12:26:39 +0000591 warning("duplicate symbol " + Name + " in version script");
592 Sym->VersionId = Version;
593}
594
595template <class ELFT>
596std::map<std::string, SymbolBody *> SymbolTable<ELFT>::getDemangledSyms() {
597 std::map<std::string, SymbolBody *> Result;
Rui Ueyamad6328522016-07-18 01:34:57 +0000598 for (Symbol *Sym : SymVector) {
599 SymbolBody *B = Sym->body();
600 Result[demangle(B->getName())] = B;
601 }
George Rimar50dcece2016-07-16 12:26:39 +0000602 return Result;
603}
604
605static bool hasExternCpp() {
606 for (VersionDefinition &V : Config->VersionDefinitions)
607 for (SymbolVersion Sym : V.Globals)
608 if (Sym.IsExternCpp)
609 return true;
610 return false;
611}
612
George Rimarc3ec9d02016-08-30 09:29:37 +0000613static SymbolBody *findDemangled(const std::map<std::string, SymbolBody *> &D,
614 StringRef Name) {
615 auto I = D.find(Name);
616 if (I != D.end())
617 return I->second;
618 return nullptr;
619}
620
George Rimar397cd87a2016-08-30 09:35:03 +0000621static std::vector<SymbolBody *>
622findAllDemangled(const std::map<std::string, SymbolBody *> &D,
George Rimarc91930a2016-09-02 21:17:20 +0000623 const Regex &Re) {
George Rimar397cd87a2016-08-30 09:35:03 +0000624 std::vector<SymbolBody *> Res;
625 for (auto &P : D) {
626 SymbolBody *Body = P.second;
George Rimarc91930a2016-09-02 21:17:20 +0000627 if (!Body->isUndefined() && const_cast<Regex &>(Re).match(P.first))
George Rimar397cd87a2016-08-30 09:35:03 +0000628 Res.push_back(Body);
629 }
630 return Res;
631}
632
Rui Ueyamadad2b882016-09-02 22:15:08 +0000633// This function processes version scripts by updating VersionId
634// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000635template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyamadad2b882016-09-02 22:15:08 +0000636 // If there's only one anonymous version definition in a version
637 // script file, the script does not actullay define any symbol version,
638 // but just specifies symbols visibilities. We assume that the script was
639 // in the form of { global: foo; bar; local *; }. So, local is default.
640 // Here, we make specified symbols global.
George Rimard3566302016-06-20 11:55:12 +0000641 if (!Config->VersionScriptGlobals.empty()) {
Rafael Espindola868fc922016-09-08 14:50:55 +0000642 std::vector<StringRef> Globs;
643 for (SymbolVersion &Sym : Config->VersionScriptGlobals) {
644 if (hasWildcard(Sym.Name)) {
645 Globs.push_back(Sym.Name);
646 continue;
647 }
George Rimar50dcece2016-07-16 12:26:39 +0000648 if (SymbolBody *B = find(Sym.Name))
George Rimard3566302016-06-20 11:55:12 +0000649 B->symbol()->VersionId = VER_NDX_GLOBAL;
Rafael Espindola868fc922016-09-08 14:50:55 +0000650 }
651 if (Globs.empty())
652 return;
653 Regex Re = compileGlobPatterns(Globs);
654 std::vector<SymbolBody *> Syms = findAll(Re);
655 for (SymbolBody *B : Syms)
656 B->symbol()->VersionId = VER_NDX_GLOBAL;
George Rimard3566302016-06-20 11:55:12 +0000657 return;
658 }
659
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000660 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000661 return;
662
Rui Ueyamadad2b882016-09-02 22:15:08 +0000663 // Now we have version definitions, so we need to set version ids to symbols.
664 // Each version definition has a glob pattern, and all symbols that match
665 // with the pattern get that version.
666
667 // Users can use "extern C++ {}" directive to match against demangled
668 // C++ symbols. For example, you can write a pattern such as
669 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
670 // other than trying to match a regexp against all demangled symbols.
671 // So, if "extern C++" feature is used, we demangle all known symbols.
George Rimar50dcece2016-07-16 12:26:39 +0000672 std::map<std::string, SymbolBody *> Demangled;
673 if (hasExternCpp())
674 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000675
Rui Ueyamadad2b882016-09-02 22:15:08 +0000676 // First, we assign versions to exact matching symbols,
677 // i.e. version definitions not containing any glob meta-characters.
George Rimar50dcece2016-07-16 12:26:39 +0000678 for (VersionDefinition &V : Config->VersionDefinitions) {
679 for (SymbolVersion Sym : V.Globals) {
George Rimarcd574a52016-09-09 14:35:36 +0000680 if (Sym.HasWildcards)
George Rimardd64bb32016-07-13 08:19:04 +0000681 continue;
George Rimarc3ec9d02016-08-30 09:29:37 +0000682 StringRef N = Sym.Name;
683 SymbolBody *B = Sym.IsExternCpp ? findDemangled(Demangled, N) : find(N);
684 setVersionId(B, V.Name, N, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000685 }
George Rimarf73a2582016-07-07 07:45:27 +0000686 }
687
Rui Ueyamadad2b882016-09-02 22:15:08 +0000688 // Next, we assign versions to fuzzy matching symbols,
689 // i.e. version definitions containing glob meta-characters.
690 // Note that because the last match takes precedence over previous matches,
691 // we iterate over the definitions in the reverse order.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000692 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
693 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimar7af64522016-08-30 09:39:36 +0000694 for (SymbolVersion &Sym : V.Globals) {
George Rimarcd574a52016-09-09 14:35:36 +0000695 if (!Sym.HasWildcards)
George Rimar7af64522016-08-30 09:39:36 +0000696 continue;
Rui Ueyamadad2b882016-09-02 22:15:08 +0000697 Regex Re = compileGlobPatterns({Sym.Name});
698 std::vector<SymbolBody *> Syms =
699 Sym.IsExternCpp ? findAllDemangled(Demangled, Re) : findAll(Re);
George Rimar397cd87a2016-08-30 09:35:03 +0000700
Rui Ueyamadad2b882016-09-02 22:15:08 +0000701 // Exact matching takes precendence over fuzzy matching,
702 // so we set a version to a symbol only if no version has been assigned
703 // to the symbol. This behavior is compatible with GNU.
704 for (SymbolBody *B : Syms)
George Rimar7af64522016-08-30 09:39:36 +0000705 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
706 B->symbol()->VersionId = V.Id;
707 }
George Rimard3566302016-06-20 11:55:12 +0000708 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000709}
710
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000711template class elf::SymbolTable<ELF32LE>;
712template class elf::SymbolTable<ELF32BE>;
713template class elf::SymbolTable<ELF64LE>;
714template class elf::SymbolTable<ELF64BE>;