blob: a017df35024cc5397dc6ee6a932f4ef24ab381f3 [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 Ueyama38dbd3e2016-09-14 00:05:51 +000050template <class ELFT> void SymbolTable<ELFT>::addFile(InputFile *File) {
51 if (!isCompatible<ELFT>(File))
Rui Ueyama16ba6692016-01-29 19:41:13 +000052 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000053
Michael J. Spencera9424f32016-09-09 22:08:04 +000054 // Binary file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000055 if (auto *F = dyn_cast<BinaryFile>(File)) {
Michael J. Spencera9424f32016-09-09 22:08:04 +000056 addFile(F->createELF<ELFT>());
57 return;
58 }
59
Rui Ueyama89575742015-12-16 22:59:13 +000060 // .a file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000061 if (auto *F = dyn_cast<ArchiveFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000062 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000063 return;
64 }
Rui Ueyama3d451792015-10-12 18:03:21 +000065
George Rimar2a78fce2016-04-13 18:07:57 +000066 // Lazy object file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000067 if (auto *F = dyn_cast<LazyObjectFile>(File)) {
Peter Collingbourne4f952702016-05-01 04:55:03 +000068 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000069 return;
70 }
71
72 if (Config->Trace)
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000073 outs() << getFilename(File) << "\n";
George Rimar2a78fce2016-04-13 18:07:57 +000074
Rui Ueyama89575742015-12-16 22:59:13 +000075 // .so file
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000076 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
Rui Ueyama89575742015-12-16 22:59:13 +000077 // DSOs are uniquified not by filename but by soname.
78 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000079 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000080 return;
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000081 SharedFiles.push_back(F);
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
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000087 if (auto *F = dyn_cast<BitcodeFile>(File)) {
88 BitcodeFiles.push_back(F);
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 Ueyama38dbd3e2016-09-14 00:05:51 +000094 auto *F = cast<ObjectFile<ELFT>>(File);
95 ObjectFiles.push_back(F);
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
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000110 // Compile bitcode files and replace bitcode symbols.
Rui Ueyama25992482016-03-22 20:52:10 +0000111 Lto.reset(new BitcodeCompiler);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000112 for (BitcodeFile *F : BitcodeFiles)
Rui Ueyama25992482016-03-22 20:52:10 +0000113 Lto->add(*F);
Rui Ueyama25992482016-03-22 20:52:10 +0000114
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000115 for (InputFile *File : Lto->compile()) {
116 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(File);
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000117 DenseSet<CachedHashStringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000118 Obj->parse(DummyGroups);
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000119 ObjectFiles.push_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000120 }
121}
122
Rafael Espindola0e604f92015-09-25 18:56:53 +0000123template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000124DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
125 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000126 return cast<DefinedRegular<ELFT>>(
127 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000128}
129
Rui Ueyamac9559d92016-01-05 20:47:37 +0000130// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000131// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000132template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000133DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
134 uint8_t Visibility) {
135 if (!find(Name))
136 return nullptr;
137 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000138}
139
Rui Ueyama69c778c2016-07-17 17:50:09 +0000140// Set a flag for --trace-symbol so that we can print out a log message
141// if a new symbol with the same name is inserted into the symbol table.
142template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000143 Symtab.insert({CachedHashStringRef(Name), {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000144}
145
Rui Ueyamadeb15402016-01-07 17:20:07 +0000146// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
147// Used to implement --wrap.
148template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000149 SymbolBody *B = find(Name);
150 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000151 return;
152 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000153 Symbol *Sym = B->symbol();
154 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
155 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
156 // We rename symbols by replacing the old symbol's SymbolBody with the new
157 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
158 // old symbol to instead refer to the new symbol.
159 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
160 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000161}
162
Peter Collingbournedadcc172016-04-22 18:42:48 +0000163static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
164 if (VA == STV_DEFAULT)
165 return VB;
166 if (VB == STV_DEFAULT)
167 return VA;
168 return std::min(VA, VB);
169}
170
Rui Ueyamadace8382016-07-21 13:13:21 +0000171// Parses a symbol in the form of <name>@<version> or <name>@@<version>.
172static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) {
173 if (Config->VersionDefinitions.empty())
174 return {S, Config->DefaultSymbolVersion};
175
176 size_t Pos = S.find('@');
177 if (Pos == 0 || Pos == StringRef::npos)
178 return {S, Config->DefaultSymbolVersion};
179
180 StringRef Name = S.substr(0, Pos);
181 StringRef Verstr = S.substr(Pos + 1);
182 if (Verstr.empty())
183 return {S, Config->DefaultSymbolVersion};
184
185 // '@@' in a symbol name means the default version.
186 // It is usually the most recent one.
187 bool IsDefault = (Verstr[0] == '@');
188 if (IsDefault)
189 Verstr = Verstr.substr(1);
190
191 for (VersionDefinition &V : Config->VersionDefinitions) {
192 if (V.Name == Verstr)
193 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)};
194 }
195
196 // It is an error if the specified version was not defined.
197 error("symbol " + S + " has undefined version " + Verstr);
198 return {S, Config->DefaultSymbolVersion};
199}
200
Rui Ueyamab4de5952016-01-08 22:01:33 +0000201// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000202template <class ELFT>
Rui Ueyamadace8382016-07-21 13:13:21 +0000203std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000204 auto P = Symtab.insert(
205 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000206 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000207 bool IsNew = P.second;
208
Rui Ueyamae3357902016-07-18 01:35:00 +0000209 if (V.Idx == -1) {
210 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000211 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000212 }
213
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000214 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000215 if (IsNew) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000216 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000217 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000218 Sym->Visibility = STV_DEFAULT;
219 Sym->IsUsedInRegularObj = false;
220 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000221 Sym->Traced = V.Traced;
Rui Ueyamadace8382016-07-21 13:13:21 +0000222 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000223 SymVector.push_back(Sym);
224 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000225 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000226 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000227 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000228}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000229
Rui Ueyama8fcc3af2016-10-26 18:28:08 +0000230// Construct a string in the form of "Sym in File1 and File2".
231// Used to construct an error message.
232static std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile) {
233 return maybeDemangle(Existing->getName()) + " in " +
234 getFilename(Existing->File) + " and " + getFilename(NewFile);
235}
236
Peter Collingbourne4f952702016-05-01 04:55:03 +0000237// Find an existing symbol or create and insert a new one, then apply the given
238// attributes.
239template <class ELFT>
240std::pair<Symbol *, bool>
Rui Ueyamadace8382016-07-21 13:13:21 +0000241SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
Davide Italiano786d8e32016-09-29 00:40:08 +0000242 bool CanOmitFromDynSym, InputFile *File) {
Rafael Espindola05098762016-08-31 13:49:23 +0000243 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000244 Symbol *S;
245 bool WasInserted;
246 std::tie(S, WasInserted) = insert(Name);
247
248 // Merge in the new symbol's visibility.
249 S->Visibility = getMinVisibility(S->Visibility, Visibility);
250 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
251 S->ExportDynamic = true;
252 if (IsUsedInRegularObj)
253 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000254 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
255 ((Type == STT_TLS) != S->body()->isTls()))
George Rimara4c7e742016-10-20 08:36:42 +0000256 error("TLS attribute mismatch for symbol: " + conflictMsg(S->body(), File));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000257
258 return {S, WasInserted};
259}
260
Peter Collingbourne4f952702016-05-01 04:55:03 +0000261template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
262 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Davide Italiano786d8e32016-09-29 00:40:08 +0000263 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000264}
265
266template <class ELFT>
267Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
268 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000269 bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000270 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000271 Symbol *S;
272 bool WasInserted;
273 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000274 insert(Name, Type, StOther & 3, CanOmitFromDynSym, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000275 if (WasInserted) {
276 S->Binding = Binding;
Rui Ueyama434b5612016-07-17 03:11:46 +0000277 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000278 return S;
279 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000280 if (Binding != STB_WEAK) {
281 if (S->body()->isShared() || S->body()->isLazy())
282 S->Binding = Binding;
283 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000284 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000285 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000286 if (auto *L = dyn_cast<Lazy>(S->body())) {
287 // An undefined weak will not fetch archive members, but we have to remember
288 // its type. See also comment in addLazyArchive.
289 if (S->isWeak())
290 L->Type = Type;
Rafael Espindola5da1d882016-10-26 15:34:24 +0000291 else if (InputFile *F = L->fetch(Alloc))
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000292 addFile(F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000293 }
294 return S;
295}
296
297// We have a new defined symbol with the specified binding. Return 1 if the new
298// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
299// strong defined symbols.
300static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
301 if (WasInserted)
302 return 1;
303 SymbolBody *Body = S->body();
304 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
305 return 1;
306 if (Binding == STB_WEAK)
307 return -1;
308 if (S->isWeak())
309 return 1;
310 return 0;
311}
312
313// We have a new non-common defined symbol with the specified binding. Return 1
314// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
315// is a conflict. If the new symbol wins, also update the binding.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000316static int compareDefinedNonCommon(Symbol *S, bool WasInserted,
317 uint8_t Binding) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000318 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
319 if (Cmp > 0)
320 S->Binding = Binding;
321 return Cmp;
322 }
Rafael Espindolae7553e42016-08-31 13:28:33 +0000323 if (isa<DefinedCommon>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000324 // Non-common symbols take precedence over common symbols.
325 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000326 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000327 return 1;
328 }
329 return 0;
330}
331
332template <class ELFT>
333Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
334 uint64_t Alignment, uint8_t Binding,
335 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000336 InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000337 Symbol *S;
338 bool WasInserted;
Davide Italiano786d8e32016-09-29 00:40:08 +0000339 std::tie(S, WasInserted) =
340 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000341 int Cmp = compareDefined(S, WasInserted, Binding);
342 if (Cmp > 0) {
343 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000344 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000345 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000346 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000347 if (!C) {
348 // Non-common symbols take precedence over common symbols.
349 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000350 warn("common " + S->body()->getName() + " is overridden");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000351 return S;
352 }
353
354 if (Config->WarnCommon)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000355 warn("multiple common of " + S->body()->getName());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000356
Rafael Espindola8db87292016-08-31 13:42:08 +0000357 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
358 if (Size > C->Size)
359 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000360 }
361 return S;
362}
363
364template <class ELFT>
365void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
366 InputFile *NewFile) {
367 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
368 if (Config->AllowMultipleDefinition)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000369 warn(Msg);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000370 else
371 error(Msg);
372}
373
374template <typename ELFT>
375Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
376 InputSectionBase<ELFT> *Section) {
377 Symbol *S;
378 bool WasInserted;
Davide Italiano786d8e32016-09-29 00:40:08 +0000379 std::tie(S, WasInserted) = insert(Name, Sym.getType(), Sym.getVisibility(),
380 /*CanOmitFromDynSym*/ false,
381 Section ? Section->getFile() : nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000382 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000383 if (Cmp > 0)
384 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
385 else if (Cmp == 0)
386 reportDuplicate(S->body(), Section->getFile());
387 return S;
388}
389
390template <typename ELFT>
391Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
392 uint8_t StOther) {
393 Symbol *S;
394 bool WasInserted;
Davide Italiano786d8e32016-09-29 00:40:08 +0000395 std::tie(S, WasInserted) = insert(Name, STT_NOTYPE, StOther & 3,
396 /*CanOmitFromDynSym*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000397 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000398 if (Cmp > 0)
399 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
400 else if (Cmp == 0)
401 reportDuplicate(S->body(), nullptr);
402 return S;
403}
404
405template <typename ELFT>
406Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000407 OutputSectionBase<ELFT> *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000408 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000409 Symbol *S;
410 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000411 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
Davide Italiano786d8e32016-09-29 00:40:08 +0000412 /*CanOmitFromDynSym*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000413 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000414 if (Cmp > 0)
415 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
416 else if (Cmp == 0)
417 reportDuplicate(S->body(), nullptr);
418 return S;
419}
420
421template <typename ELFT>
422void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
423 const Elf_Sym &Sym,
424 const typename ELFT::Verdef *Verdef) {
425 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
426 // as the visibility, which will leave the visibility in the symbol table
427 // unchanged.
428 Symbol *S;
429 bool WasInserted;
430 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000431 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000432 // Make sure we preempt DSO symbols with default visibility.
433 if (Sym.getVisibility() == STV_DEFAULT)
434 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000435 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000436 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000437 if (!S->isWeak())
438 F->IsUsed = true;
439 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000440}
441
442template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000443Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000444 uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +0000445 bool CanOmitFromDynSym, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000446 Symbol *S;
447 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000448 std::tie(S, WasInserted) =
Davide Italiano786d8e32016-09-29 00:40:08 +0000449 insert(Name, Type, StOther & 3, CanOmitFromDynSym, F);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000450 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000451 if (Cmp > 0)
Rafael Espindolaa6c97442016-08-31 12:30:34 +0000452 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000453 else if (Cmp == 0)
454 reportDuplicate(S->body(), F);
455 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000456}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000457
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000458template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
Justin Lebar3c11e932016-10-18 17:50:36 +0000459 auto It = Symtab.find(CachedHashStringRef(Name));
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000460 if (It == Symtab.end())
461 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000462 SymIndex V = It->second;
463 if (V.Idx == -1)
464 return nullptr;
465 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000466}
467
George Rimarc91930a2016-09-02 21:17:20 +0000468// Returns a list of defined symbols that match with a given regex.
Rui Ueyama3d451792015-10-12 18:03:21 +0000469template <class ELFT>
George Rimarc91930a2016-09-02 21:17:20 +0000470std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(const Regex &Re) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000471 std::vector<SymbolBody *> Res;
Rui Ueyamad6328522016-07-18 01:34:57 +0000472 for (Symbol *Sym : SymVector) {
473 SymbolBody *B = Sym->body();
George Rimarc91930a2016-09-02 21:17:20 +0000474 StringRef Name = B->getName();
475 if (!B->isUndefined() && const_cast<Regex &>(Re).match(Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000476 Res.push_back(B);
477 }
478 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000479}
480
481template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000482void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
483 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000484 Symbol *S;
485 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000486 StringRef Name = Sym.getName();
487 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000488 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000489 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000490 return;
491 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000492 if (!S->body()->isUndefined())
493 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000494
Peter Collingbourne4f952702016-05-01 04:55:03 +0000495 // Weak undefined symbols should not fetch members from archives. If we were
496 // to keep old symbol we would not know that an archive member was available
497 // if a strong undefined symbol shows up afterwards in the link. If a strong
498 // undefined symbol never shows up, this lazy symbol will get to the end of
499 // the link and must be treated as the weak undefined one. We already marked
500 // this symbol as used when we added it to the symbol table, but we also need
501 // to preserve its type. FIXME: Move the Type field to Symbol.
502 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000503 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000504 return;
505 }
Davide Italianobcdd6c62016-10-12 19:35:54 +0000506 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
507 if (!MBInfo.first.getBuffer().empty())
Rafael Espindola5da1d882016-10-26 15:34:24 +0000508 addFile(createObjectFile(Alloc, MBInfo.first, F->getName(), MBInfo.second));
Peter Collingbourne4f952702016-05-01 04:55:03 +0000509}
510
511template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000512void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000513 Symbol *S;
514 bool WasInserted;
515 std::tie(S, WasInserted) = insert(Name);
516 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000517 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000518 return;
519 }
520 if (!S->body()->isUndefined())
521 return;
522
523 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000524 if (S->isWeak()) {
525 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
526 } else {
527 MemoryBufferRef MBRef = Obj.getBuffer();
528 if (!MBRef.getBuffer().empty())
Rafael Espindola5da1d882016-10-26 15:34:24 +0000529 addFile(createObjectFile(Alloc, MBRef));
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000530 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000531}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000532
Peter Collingbourne892d49802016-04-27 00:05:03 +0000533// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000534template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000535 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000536 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rafael Espindola5da1d882016-10-26 15:34:24 +0000537 if (InputFile *File = L->fetch(Alloc))
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000538 addFile(File);
Peter Collingbourne892d49802016-04-27 00:05:03 +0000539}
540
Rui Ueyama93bfee52015-10-13 18:10:33 +0000541// This function takes care of the case in which shared libraries depend on
542// the user program (not the other way, which is usual). Shared libraries
543// may have undefined symbols, expecting that the user program provides
544// the definitions for them. An example is BSD's __progname symbol.
545// We need to put such symbols to the main program's .dynsym so that
546// shared libraries can find them.
547// Except this, we ignore undefined symbols in DSOs.
548template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000549 for (SharedFile<ELFT> *File : SharedFiles)
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000550 for (StringRef U : File->getUndefinedSymbols())
551 if (SymbolBody *Sym = find(U))
552 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000553 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000554}
555
Rui Ueyamadad2b882016-09-02 22:15:08 +0000556// This function processes --export-dynamic-symbol and --dynamic-list.
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000557template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
558 for (StringRef S : Config->DynamicList)
559 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000560 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000561}
562
George Rimar50dcece2016-07-16 12:26:39 +0000563static void setVersionId(SymbolBody *Body, StringRef VersionName,
564 StringRef Name, uint16_t Version) {
565 if (!Body || Body->isUndefined()) {
566 if (Config->NoUndefinedVersion)
567 error("version script assignment of " + VersionName + " to symbol " +
568 Name + " failed: symbol not defined");
569 return;
570 }
571
572 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000573 if (Sym->VersionId != Config->DefaultSymbolVersion)
Rui Ueyamad31e13f2016-09-29 21:00:23 +0000574 warn("duplicate symbol " + Name + " in version script");
George Rimar50dcece2016-07-16 12:26:39 +0000575 Sym->VersionId = Version;
576}
577
Rui Ueyamafbde7102016-09-13 20:41:06 +0000578// Returns a map from demangled symbols to symbol objects.
579// The relationship is 1:N instead of 1:1 because with the symbol
580// versioning, more than one symbol may have the same name.
George Rimar50dcece2016-07-16 12:26:39 +0000581template <class ELFT>
George Rimar31c25ae2016-09-15 12:44:38 +0000582std::map<std::string, std::vector<SymbolBody *>>
583SymbolTable<ELFT>::getDemangledSyms() {
584 std::map<std::string, std::vector<SymbolBody *>> Result;
Rui Ueyamad6328522016-07-18 01:34:57 +0000585 for (Symbol *Sym : SymVector) {
586 SymbolBody *B = Sym->body();
George Rimar31c25ae2016-09-15 12:44:38 +0000587 Result[demangle(B->getName())].push_back(B);
Rui Ueyamad6328522016-07-18 01:34:57 +0000588 }
George Rimar50dcece2016-07-16 12:26:39 +0000589 return Result;
590}
591
592static bool hasExternCpp() {
593 for (VersionDefinition &V : Config->VersionDefinitions)
594 for (SymbolVersion Sym : V.Globals)
595 if (Sym.IsExternCpp)
596 return true;
597 return false;
598}
599
George Rimar31c25ae2016-09-15 12:44:38 +0000600static ArrayRef<SymbolBody *>
601findDemangled(std::map<std::string, std::vector<SymbolBody *>> &D,
602 StringRef Name) {
George Rimarc3ec9d02016-08-30 09:29:37 +0000603 auto I = D.find(Name);
604 if (I != D.end())
605 return I->second;
George Rimar31c25ae2016-09-15 12:44:38 +0000606 return {};
George Rimarc3ec9d02016-08-30 09:29:37 +0000607}
608
George Rimar397cd87a2016-08-30 09:35:03 +0000609static std::vector<SymbolBody *>
George Rimar31c25ae2016-09-15 12:44:38 +0000610findAllDemangled(const std::map<std::string, std::vector<SymbolBody *>> &D,
George Rimarc91930a2016-09-02 21:17:20 +0000611 const Regex &Re) {
George Rimar397cd87a2016-08-30 09:35:03 +0000612 std::vector<SymbolBody *> Res;
613 for (auto &P : D) {
George Rimar31c25ae2016-09-15 12:44:38 +0000614 if (const_cast<Regex &>(Re).match(P.first))
615 for (SymbolBody *Body : P.second)
616 if (!Body->isUndefined())
617 Res.push_back(Body);
George Rimar397cd87a2016-08-30 09:35:03 +0000618 }
619 return Res;
620}
621
Rui Ueyamaea265042016-09-13 20:51:30 +0000622// If there's only one anonymous version definition in a version
623// script file, the script does not actullay define any symbol version,
624// but just specifies symbols visibilities. We assume that the script was
625// in the form of { global: foo; bar; local *; }. So, local is default.
626// In this function, we make specified symbols global.
627template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
628 std::vector<StringRef> Patterns;
629 for (SymbolVersion &Sym : Config->VersionScriptGlobals) {
630 if (hasWildcard(Sym.Name)) {
631 Patterns.push_back(Sym.Name);
632 continue;
633 }
634 if (SymbolBody *B = find(Sym.Name))
635 B->symbol()->VersionId = VER_NDX_GLOBAL;
636 }
637 if (Patterns.empty())
638 return;
639 Regex Re = compileGlobPatterns(Patterns);
640 std::vector<SymbolBody *> Syms = findAll(Re);
641 for (SymbolBody *B : Syms)
642 B->symbol()->VersionId = VER_NDX_GLOBAL;
643}
644
Rui Ueyamadad2b882016-09-02 22:15:08 +0000645// This function processes version scripts by updating VersionId
646// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000647template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000648 // Handle edge cases first.
George Rimard3566302016-06-20 11:55:12 +0000649 if (!Config->VersionScriptGlobals.empty()) {
Rui Ueyamaea265042016-09-13 20:51:30 +0000650 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000651 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
Rui Ueyamadad2b882016-09-02 22:15:08 +0000657 // Now we have version definitions, so we need to set version ids to symbols.
658 // Each version definition has a glob pattern, and all symbols that match
659 // with the pattern get that version.
660
661 // Users can use "extern C++ {}" directive to match against demangled
662 // C++ symbols. For example, you can write a pattern such as
663 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
664 // other than trying to match a regexp against all demangled symbols.
665 // So, if "extern C++" feature is used, we demangle all known symbols.
George Rimar31c25ae2016-09-15 12:44:38 +0000666 std::map<std::string, std::vector<SymbolBody *>> Demangled;
George Rimar50dcece2016-07-16 12:26:39 +0000667 if (hasExternCpp())
668 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000669
Rui Ueyamadad2b882016-09-02 22:15:08 +0000670 // First, we assign versions to exact matching symbols,
671 // i.e. version definitions not containing any glob meta-characters.
George Rimar50dcece2016-07-16 12:26:39 +0000672 for (VersionDefinition &V : Config->VersionDefinitions) {
673 for (SymbolVersion Sym : V.Globals) {
George Rimarcd574a52016-09-09 14:35:36 +0000674 if (Sym.HasWildcards)
George Rimardd64bb32016-07-13 08:19:04 +0000675 continue;
George Rimar31c25ae2016-09-15 12:44:38 +0000676
George Rimarc3ec9d02016-08-30 09:29:37 +0000677 StringRef N = Sym.Name;
George Rimar31c25ae2016-09-15 12:44:38 +0000678 if (Sym.IsExternCpp) {
679 for (SymbolBody *B : findDemangled(Demangled, N))
680 setVersionId(B, V.Name, N, V.Id);
681 continue;
682 }
683 setVersionId(find(N), V.Name, N, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000684 }
George Rimarf73a2582016-07-07 07:45:27 +0000685 }
686
Rui Ueyamadad2b882016-09-02 22:15:08 +0000687 // Next, we assign versions to fuzzy matching symbols,
688 // i.e. version definitions containing glob meta-characters.
689 // Note that because the last match takes precedence over previous matches,
690 // we iterate over the definitions in the reverse order.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000691 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
692 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimar7af64522016-08-30 09:39:36 +0000693 for (SymbolVersion &Sym : V.Globals) {
George Rimarcd574a52016-09-09 14:35:36 +0000694 if (!Sym.HasWildcards)
George Rimar7af64522016-08-30 09:39:36 +0000695 continue;
Rui Ueyamadad2b882016-09-02 22:15:08 +0000696 Regex Re = compileGlobPatterns({Sym.Name});
697 std::vector<SymbolBody *> Syms =
698 Sym.IsExternCpp ? findAllDemangled(Demangled, Re) : findAll(Re);
George Rimar397cd87a2016-08-30 09:35:03 +0000699
Rui Ueyamadad2b882016-09-02 22:15:08 +0000700 // Exact matching takes precendence over fuzzy matching,
701 // so we set a version to a symbol only if no version has been assigned
702 // to the symbol. This behavior is compatible with GNU.
703 for (SymbolBody *B : Syms)
George Rimar7af64522016-08-30 09:39:36 +0000704 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
705 B->symbol()->VersionId = V.Id;
706 }
George Rimard3566302016-06-20 11:55:12 +0000707 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000708}
709
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000710template class elf::SymbolTable<ELF32LE>;
711template class elf::SymbolTable<ELF32BE>;
712template class elf::SymbolTable<ELF64LE>;
713template class elf::SymbolTable<ELF64BE>;