blob: 32cb64d75c886fa8ea3ec8cb515c63bbfc369456 [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
Michael J. Spencera9424f32016-09-09 22:08:04 +000056 // Binary file
57 if (auto *F = dyn_cast<BinaryFile>(FileP)) {
Michael J. Spencer0cb8a702016-09-10 01:42:43 +000058 BinaryFiles.emplace_back(cast<BinaryFile>(File.release()));
Michael J. Spencera9424f32016-09-09 22:08:04 +000059 addFile(F->createELF<ELFT>());
60 return;
61 }
62
Rui Ueyama89575742015-12-16 22:59:13 +000063 // .a file
64 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000065 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000066 F->parse<ELFT>();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000067 return;
68 }
Rui Ueyama3d451792015-10-12 18:03:21 +000069
George Rimar2a78fce2016-04-13 18:07:57 +000070 // Lazy object file
71 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
72 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000073 F->parse<ELFT>();
George Rimar2a78fce2016-04-13 18:07:57 +000074 return;
75 }
76
77 if (Config->Trace)
Rui Ueyama818bb2f2016-07-16 18:55:47 +000078 outs() << getFilename(FileP) << "\n";
George Rimar2a78fce2016-04-13 18:07:57 +000079
Rui Ueyama89575742015-12-16 22:59:13 +000080 // .so file
81 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
82 // DSOs are uniquified not by filename but by soname.
83 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000084 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000085 return;
Rui Ueyama89575742015-12-16 22:59:13 +000086
Rafael Espindola21f7bd42015-12-23 14:35:51 +000087 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000088 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000089 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000090 }
Rui Ueyama89575742015-12-16 22:59:13 +000091
Rui Ueyamaf8baa662016-04-07 19:24:51 +000092 // LLVM bitcode file
Rafael Espindola9f77ef02016-02-12 20:54:57 +000093 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
94 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Peter Collingbourne4f952702016-05-01 04:55:03 +000095 F->parse<ELFT>(ComdatGroups);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000096 return;
97 }
98
Rui Ueyamaf8baa662016-04-07 19:24:51 +000099 // Regular object file
Rui Ueyama89575742015-12-16 22:59:13 +0000100 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +0000101 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +0000102 F->parse(ComdatGroups);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000103}
104
Rui Ueyama42554752016-04-23 00:26:32 +0000105// This function is where all the optimizations of link-time
106// optimization happens. When LTO is in use, some input files are
107// not in native object file format but in the LLVM bitcode format.
108// This function compiles bitcode files into a few big native files
109// using LLVM functions and replaces bitcode symbols with the results.
110// Because all bitcode files that consist of a program are passed
111// to the compiler at once, it can do whole-program optimization.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000112template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
113 if (BitcodeFiles.empty())
114 return;
Rui Ueyama25992482016-03-22 20:52:10 +0000115
116 // Compile bitcode files.
117 Lto.reset(new BitcodeCompiler);
118 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
119 Lto->add(*F);
Davide Italianobc176632016-04-15 22:38:10 +0000120 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
Rui Ueyama25992482016-03-22 20:52:10 +0000121
122 // Replace bitcode symbols.
Davide Italianobc176632016-04-15 22:38:10 +0000123 for (auto &IF : IFs) {
124 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
125
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000126 DenseSet<StringRef> DummyGroups;
Davide Italianobc176632016-04-15 22:38:10 +0000127 Obj->parse(DummyGroups);
Davide Italianobc176632016-04-15 22:38:10 +0000128 ObjectFiles.emplace_back(Obj);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000129 }
130}
131
Rafael Espindola0e604f92015-09-25 18:56:53 +0000132template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000133DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
134 uint8_t Visibility) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000135 return cast<DefinedRegular<ELFT>>(
136 addRegular(Name, STB_GLOBAL, Visibility)->body());
Rafael Espindola0e604f92015-09-25 18:56:53 +0000137}
138
Rui Ueyamac9559d92016-01-05 20:47:37 +0000139// Add Name as an "ignored" symbol. An ignored symbol is a regular
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000140// linker-synthesized defined symbol, but is only defined if needed.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000141template <class ELFT>
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000142DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
143 uint8_t Visibility) {
144 if (!find(Name))
145 return nullptr;
146 return addAbsolute(Name, Visibility);
Rafael Espindola5d413262015-10-01 21:22:26 +0000147}
148
Rui Ueyama69c778c2016-07-17 17:50:09 +0000149// Set a flag for --trace-symbol so that we can print out a log message
150// if a new symbol with the same name is inserted into the symbol table.
151template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) {
Rui Ueyamae3357902016-07-18 01:35:00 +0000152 Symtab.insert({Name, {-1, true}});
Rui Ueyama69c778c2016-07-17 17:50:09 +0000153}
154
Rui Ueyamadeb15402016-01-07 17:20:07 +0000155// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
156// Used to implement --wrap.
157template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
Rui Ueyama1b70d662016-04-28 00:03:38 +0000158 SymbolBody *B = find(Name);
159 if (!B)
Rui Ueyamadeb15402016-01-07 17:20:07 +0000160 return;
161 StringSaver Saver(Alloc);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000162 Symbol *Sym = B->symbol();
163 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
164 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
165 // We rename symbols by replacing the old symbol's SymbolBody with the new
166 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
167 // old symbol to instead refer to the new symbol.
168 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
169 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
Rui Ueyamadeb15402016-01-07 17:20:07 +0000170}
171
Peter Collingbournedadcc172016-04-22 18:42:48 +0000172static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
173 if (VA == STV_DEFAULT)
174 return VB;
175 if (VB == STV_DEFAULT)
176 return VA;
177 return std::min(VA, VB);
178}
179
Rui Ueyamadace8382016-07-21 13:13:21 +0000180// Parses a symbol in the form of <name>@<version> or <name>@@<version>.
181static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) {
182 if (Config->VersionDefinitions.empty())
183 return {S, Config->DefaultSymbolVersion};
184
185 size_t Pos = S.find('@');
186 if (Pos == 0 || Pos == StringRef::npos)
187 return {S, Config->DefaultSymbolVersion};
188
189 StringRef Name = S.substr(0, Pos);
190 StringRef Verstr = S.substr(Pos + 1);
191 if (Verstr.empty())
192 return {S, Config->DefaultSymbolVersion};
193
194 // '@@' in a symbol name means the default version.
195 // It is usually the most recent one.
196 bool IsDefault = (Verstr[0] == '@');
197 if (IsDefault)
198 Verstr = Verstr.substr(1);
199
200 for (VersionDefinition &V : Config->VersionDefinitions) {
201 if (V.Name == Verstr)
202 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)};
203 }
204
205 // It is an error if the specified version was not defined.
206 error("symbol " + S + " has undefined version " + Verstr);
207 return {S, Config->DefaultSymbolVersion};
208}
209
Rui Ueyamab4de5952016-01-08 22:01:33 +0000210// Find an existing symbol or create and insert a new one.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000211template <class ELFT>
Rui Ueyamadace8382016-07-21 13:13:21 +0000212std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) {
George Rimarb0841252016-07-20 14:26:48 +0000213 auto P = Symtab.insert({Name, SymIndex((int)SymVector.size(), false)});
Rui Ueyamae3357902016-07-18 01:35:00 +0000214 SymIndex &V = P.first->second;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000215 bool IsNew = P.second;
216
Rui Ueyamae3357902016-07-18 01:35:00 +0000217 if (V.Idx == -1) {
218 IsNew = true;
George Rimarb0841252016-07-20 14:26:48 +0000219 V = SymIndex((int)SymVector.size(), true);
Rui Ueyamae3357902016-07-18 01:35:00 +0000220 }
221
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000222 Symbol *Sym;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000223 if (IsNew) {
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000224 Sym = new (Alloc) Symbol;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000225 Sym->Binding = STB_WEAK;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000226 Sym->Visibility = STV_DEFAULT;
227 Sym->IsUsedInRegularObj = false;
Davide Italiano35af5b32016-08-30 20:15:03 +0000228 Sym->HasUnnamedAddr = true;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000229 Sym->ExportDynamic = false;
Rui Ueyamae3357902016-07-18 01:35:00 +0000230 Sym->Traced = V.Traced;
Rui Ueyamadace8382016-07-21 13:13:21 +0000231 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000232 SymVector.push_back(Sym);
233 } else {
Rui Ueyamae3357902016-07-18 01:35:00 +0000234 Sym = SymVector[V.Idx];
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000235 }
Rui Ueyama69c778c2016-07-17 17:50:09 +0000236 return {Sym, IsNew};
Peter Collingbourne4f952702016-05-01 04:55:03 +0000237}
Peter Collingbournedadcc172016-04-22 18:42:48 +0000238
Peter Collingbourne4f952702016-05-01 04:55:03 +0000239// Find an existing symbol or create and insert a new one, then apply the given
240// attributes.
241template <class ELFT>
242std::pair<Symbol *, bool>
Rui Ueyamadace8382016-07-21 13:13:21 +0000243SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
Davide Italiano35af5b32016-08-30 20:15:03 +0000244 bool CanOmitFromDynSym, bool HasUnnamedAddr,
Rafael Espindola05098762016-08-31 13:49:23 +0000245 InputFile *File) {
246 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000247 Symbol *S;
248 bool WasInserted;
249 std::tie(S, WasInserted) = insert(Name);
250
Davide Italiano35af5b32016-08-30 20:15:03 +0000251 // Merge in the new unnamed_addr attribute.
252 S->HasUnnamedAddr &= HasUnnamedAddr;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000253 // Merge in the new symbol's visibility.
254 S->Visibility = getMinVisibility(S->Visibility, Visibility);
255 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
256 S->ExportDynamic = true;
257 if (IsUsedInRegularObj)
258 S->IsUsedInRegularObj = true;
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000259 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
260 ((Type == STT_TLS) != S->body()->isTls()))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000261 error("TLS attribute mismatch for symbol: " +
262 conflictMsg(S->body(), File));
263
264 return {S, WasInserted};
265}
266
267// Construct a string in the form of "Sym in File1 and File2".
268// Used to construct an error message.
269template <typename ELFT>
270std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
271 InputFile *NewFile) {
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000272 std::string Sym = Existing->getName();
273 if (Config->Demangle)
274 Sym = demangle(Sym);
Rui Ueyama434b5612016-07-17 03:11:46 +0000275 return Sym + " in " + getFilename(Existing->File) + " and " +
Rui Ueyamaf4d93382016-07-07 23:04:15 +0000276 getFilename(NewFile);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000277}
278
279template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
280 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
Davide Italiano35af5b32016-08-30 20:15:03 +0000281 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
282 /*File*/ nullptr);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000283}
284
285template <class ELFT>
286Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
287 uint8_t StOther, uint8_t Type,
Rafael Espindolacc70da32016-06-15 17:56:10 +0000288 bool CanOmitFromDynSym,
Davide Italiano35af5b32016-08-30 20:15:03 +0000289 bool HasUnnamedAddr, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000290 Symbol *S;
291 bool WasInserted;
292 std::tie(S, WasInserted) =
Rafael Espindola05098762016-08-31 13:49:23 +0000293 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000294 if (WasInserted) {
295 S->Binding = Binding;
Rui Ueyama434b5612016-07-17 03:11:46 +0000296 replaceBody<Undefined>(S, Name, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000297 return S;
298 }
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000299 if (Binding != STB_WEAK) {
300 if (S->body()->isShared() || S->body()->isLazy())
301 S->Binding = Binding;
302 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
Rui Ueyama434b5612016-07-17 03:11:46 +0000303 SS->file()->IsUsed = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000304 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000305 if (auto *L = dyn_cast<Lazy>(S->body())) {
306 // An undefined weak will not fetch archive members, but we have to remember
307 // its type. See also comment in addLazyArchive.
308 if (S->isWeak())
309 L->Type = Type;
Rui Ueyama434b5612016-07-17 03:11:46 +0000310 else if (auto F = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000311 addFile(std::move(F));
312 }
313 return S;
314}
315
316// We have a new defined symbol with the specified binding. Return 1 if the new
317// symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
318// strong defined symbols.
319static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
320 if (WasInserted)
321 return 1;
322 SymbolBody *Body = S->body();
323 if (Body->isLazy() || Body->isUndefined() || Body->isShared())
324 return 1;
325 if (Binding == STB_WEAK)
326 return -1;
327 if (S->isWeak())
328 return 1;
329 return 0;
330}
331
332// We have a new non-common defined symbol with the specified binding. Return 1
333// if the new symbol should win, -1 if the new symbol should lose, or 0 if there
334// is a conflict. If the new symbol wins, also update the binding.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000335static int compareDefinedNonCommon(Symbol *S, bool WasInserted,
336 uint8_t Binding) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000337 if (int Cmp = compareDefined(S, WasInserted, Binding)) {
338 if (Cmp > 0)
339 S->Binding = Binding;
340 return Cmp;
341 }
Rafael Espindolae7553e42016-08-31 13:28:33 +0000342 if (isa<DefinedCommon>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000343 // Non-common symbols take precedence over common symbols.
344 if (Config->WarnCommon)
345 warning("common " + S->body()->getName() + " is overridden");
346 return 1;
347 }
348 return 0;
349}
350
351template <class ELFT>
352Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
353 uint64_t Alignment, uint8_t Binding,
354 uint8_t StOther, uint8_t Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000355 bool HasUnnamedAddr, InputFile *File) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000356 Symbol *S;
357 bool WasInserted;
Rafael Espindola05098762016-08-31 13:49:23 +0000358 std::tie(S, WasInserted) = insert(
359 N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, HasUnnamedAddr, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000360 int Cmp = compareDefined(S, WasInserted, Binding);
361 if (Cmp > 0) {
362 S->Binding = Binding;
Rafael Espindolae7553e42016-08-31 13:28:33 +0000363 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000364 } else if (Cmp == 0) {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000365 auto *C = dyn_cast<DefinedCommon>(S->body());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000366 if (!C) {
367 // Non-common symbols take precedence over common symbols.
368 if (Config->WarnCommon)
369 warning("common " + S->body()->getName() + " is overridden");
370 return S;
371 }
372
373 if (Config->WarnCommon)
374 warning("multiple common of " + S->body()->getName());
375
Rafael Espindola8db87292016-08-31 13:42:08 +0000376 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
377 if (Size > C->Size)
378 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000379 }
380 return S;
381}
382
383template <class ELFT>
384void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
385 InputFile *NewFile) {
386 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
387 if (Config->AllowMultipleDefinition)
388 warning(Msg);
389 else
390 error(Msg);
391}
392
393template <typename ELFT>
394Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
395 InputSectionBase<ELFT> *Section) {
396 Symbol *S;
397 bool WasInserted;
Rafael Espindola05098762016-08-31 13:49:23 +0000398 std::tie(S, WasInserted) =
399 insert(Name, Sym.getType(), Sym.getVisibility(),
400 /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
401 Section ? Section->getFile() : nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000402 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
Peter Collingbourne4f952702016-05-01 04:55:03 +0000403 if (Cmp > 0)
404 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
405 else if (Cmp == 0)
406 reportDuplicate(S->body(), Section->getFile());
407 return S;
408}
409
410template <typename ELFT>
411Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
412 uint8_t StOther) {
413 Symbol *S;
414 bool WasInserted;
415 std::tie(S, WasInserted) =
416 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
Rafael Espindola05098762016-08-31 13:49:23 +0000417 /*HasUnnamedAddr*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000418 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000419 if (Cmp > 0)
420 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
421 else if (Cmp == 0)
422 reportDuplicate(S->body(), nullptr);
423 return S;
424}
425
426template <typename ELFT>
427Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000428 OutputSectionBase<ELFT> *Section,
George Rimare1937bb2016-08-19 15:36:32 +0000429 uintX_t Value, uint8_t StOther) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000430 Symbol *S;
431 bool WasInserted;
George Rimare1937bb2016-08-19 15:36:32 +0000432 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
433 /*CanOmitFromDynSym*/ false,
Rafael Espindola05098762016-08-31 13:49:23 +0000434 /*HasUnnamedAddr*/ false, nullptr);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000435 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000436 if (Cmp > 0)
437 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
438 else if (Cmp == 0)
439 reportDuplicate(S->body(), nullptr);
440 return S;
441}
442
443template <typename ELFT>
444void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
445 const Elf_Sym &Sym,
446 const typename ELFT::Verdef *Verdef) {
447 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
448 // as the visibility, which will leave the visibility in the symbol table
449 // unchanged.
450 Symbol *S;
451 bool WasInserted;
452 std::tie(S, WasInserted) =
453 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
Rafael Espindola05098762016-08-31 13:49:23 +0000454 /*HasUnnamedAddr*/ false, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000455 // Make sure we preempt DSO symbols with default visibility.
456 if (Sym.getVisibility() == STV_DEFAULT)
457 S->ExportDynamic = true;
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000458 if (WasInserted || isa<Undefined>(S->body())) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000459 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
Peter Collingbourneca8c9942016-06-09 18:01:35 +0000460 if (!S->isWeak())
461 F->IsUsed = true;
462 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000463}
464
465template <class ELFT>
Rafael Espindolacceb92a2016-08-30 20:53:26 +0000466Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000467 uint8_t StOther, uint8_t Type,
Davide Italiano35af5b32016-08-30 20:15:03 +0000468 bool CanOmitFromDynSym,
469 bool HasUnnamedAddr, BitcodeFile *F) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000470 Symbol *S;
471 bool WasInserted;
Davide Italiano35af5b32016-08-30 20:15:03 +0000472 std::tie(S, WasInserted) =
Rafael Espindola05098762016-08-31 13:49:23 +0000473 insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr, F);
Rafael Espindolae7553e42016-08-31 13:28:33 +0000474 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000475 if (Cmp > 0)
Rafael Espindolaa6c97442016-08-31 12:30:34 +0000476 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000477 else if (Cmp == 0)
478 reportDuplicate(S->body(), F);
479 return S;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000480}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000481
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000482template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
483 auto It = Symtab.find(Name);
484 if (It == Symtab.end())
485 return nullptr;
Rui Ueyamae3357902016-07-18 01:35:00 +0000486 SymIndex V = It->second;
487 if (V.Idx == -1)
488 return nullptr;
489 return SymVector[V.Idx]->body();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000490}
491
George Rimarc91930a2016-09-02 21:17:20 +0000492// Returns a list of defined symbols that match with a given regex.
Rui Ueyama3d451792015-10-12 18:03:21 +0000493template <class ELFT>
George Rimarc91930a2016-09-02 21:17:20 +0000494std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(const Regex &Re) {
Rui Ueyama48e42512016-06-29 04:47:39 +0000495 std::vector<SymbolBody *> Res;
Rui Ueyamad6328522016-07-18 01:34:57 +0000496 for (Symbol *Sym : SymVector) {
497 SymbolBody *B = Sym->body();
George Rimarc91930a2016-09-02 21:17:20 +0000498 StringRef Name = B->getName();
499 if (!B->isUndefined() && const_cast<Regex &>(Re).match(Name))
Rui Ueyama48e42512016-06-29 04:47:39 +0000500 Res.push_back(B);
501 }
502 return Res;
Davide Italiano8e1131d2016-06-29 02:46:51 +0000503}
504
505template <class ELFT>
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000506void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
507 const object::Archive::Symbol Sym) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000508 Symbol *S;
509 bool WasInserted;
Rui Ueyamadace8382016-07-21 13:13:21 +0000510 StringRef Name = Sym.getName();
511 std::tie(S, WasInserted) = insert(Name);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000512 if (WasInserted) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000513 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
Rui Ueyamac5b95122015-12-16 23:23:14 +0000514 return;
515 }
Peter Collingbourne4f952702016-05-01 04:55:03 +0000516 if (!S->body()->isUndefined())
517 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000518
Peter Collingbourne4f952702016-05-01 04:55:03 +0000519 // Weak undefined symbols should not fetch members from archives. If we were
520 // to keep old symbol we would not know that an archive member was available
521 // if a strong undefined symbol shows up afterwards in the link. If a strong
522 // undefined symbol never shows up, this lazy symbol will get to the end of
523 // the link and must be treated as the weak undefined one. We already marked
524 // this symbol as used when we added it to the symbol table, but we also need
525 // to preserve its type. FIXME: Move the Type field to Symbol.
526 if (S->isWeak()) {
Rafael Espindola07543a82016-06-14 21:40:23 +0000527 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000528 return;
529 }
530 MemoryBufferRef MBRef = F->getMember(&Sym);
531 if (!MBRef.getBuffer().empty())
532 addFile(createObjectFile(MBRef, F->getName()));
533}
534
535template <class ELFT>
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000536void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000537 Symbol *S;
538 bool WasInserted;
539 std::tie(S, WasInserted) = insert(Name);
540 if (WasInserted) {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000541 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000542 return;
543 }
544 if (!S->body()->isUndefined())
545 return;
546
547 // See comment for addLazyArchive above.
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000548 if (S->isWeak()) {
549 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
550 } else {
551 MemoryBufferRef MBRef = Obj.getBuffer();
552 if (!MBRef.getBuffer().empty())
553 addFile(createObjectFile(MBRef));
554 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000555}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000556
Peter Collingbourne892d49802016-04-27 00:05:03 +0000557// Process undefined (-u) flags by loading lazy symbols named by those flags.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000558template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
Peter Collingbourne892d49802016-04-27 00:05:03 +0000559 for (StringRef S : Config->Undefined)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000560 if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
Rui Ueyama434b5612016-07-17 03:11:46 +0000561 if (std::unique_ptr<InputFile> File = L->fetch())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000562 addFile(std::move(File));
Peter Collingbourne892d49802016-04-27 00:05:03 +0000563}
564
Rui Ueyama93bfee52015-10-13 18:10:33 +0000565// This function takes care of the case in which shared libraries depend on
566// the user program (not the other way, which is usual). Shared libraries
567// may have undefined symbols, expecting that the user program provides
568// the definitions for them. An example is BSD's __progname symbol.
569// We need to put such symbols to the main program's .dynsym so that
570// shared libraries can find them.
571// Except this, we ignore undefined symbols in DSOs.
572template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000573 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
574 for (StringRef U : File->getUndefinedSymbols())
575 if (SymbolBody *Sym = find(U))
576 if (Sym->isDefined())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000577 Sym->symbol()->ExportDynamic = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000578}
579
Rui Ueyamadad2b882016-09-02 22:15:08 +0000580// This function processes --export-dynamic-symbol and --dynamic-list.
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000581template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
582 for (StringRef S : Config->DynamicList)
583 if (SymbolBody *B = find(S))
Peter Collingbourne4f952702016-05-01 04:55:03 +0000584 B->symbol()->ExportDynamic = true;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000585}
586
George Rimar50dcece2016-07-16 12:26:39 +0000587static void setVersionId(SymbolBody *Body, StringRef VersionName,
588 StringRef Name, uint16_t Version) {
589 if (!Body || Body->isUndefined()) {
590 if (Config->NoUndefinedVersion)
591 error("version script assignment of " + VersionName + " to symbol " +
592 Name + " failed: symbol not defined");
593 return;
594 }
595
596 Symbol *Sym = Body->symbol();
Rui Ueyama962b2772016-07-16 18:45:25 +0000597 if (Sym->VersionId != Config->DefaultSymbolVersion)
George Rimar50dcece2016-07-16 12:26:39 +0000598 warning("duplicate symbol " + Name + " in version script");
599 Sym->VersionId = Version;
600}
601
Rui Ueyamafbde7102016-09-13 20:41:06 +0000602// Returns a map from demangled symbols to symbol objects.
603// The relationship is 1:N instead of 1:1 because with the symbol
604// versioning, more than one symbol may have the same name.
George Rimar50dcece2016-07-16 12:26:39 +0000605template <class ELFT>
George Rimare2051ef2016-09-13 10:45:39 +0000606std::map<std::string, std::vector<SymbolBody *>>
607SymbolTable<ELFT>::getDemangledSyms() {
608 std::map<std::string, std::vector<SymbolBody *>> Result;
Rui Ueyamad6328522016-07-18 01:34:57 +0000609 for (Symbol *Sym : SymVector) {
610 SymbolBody *B = Sym->body();
George Rimare2051ef2016-09-13 10:45:39 +0000611 Result[demangle(B->getName())].push_back(B);
Rui Ueyamad6328522016-07-18 01:34:57 +0000612 }
George Rimar50dcece2016-07-16 12:26:39 +0000613 return Result;
614}
615
616static bool hasExternCpp() {
617 for (VersionDefinition &V : Config->VersionDefinitions)
618 for (SymbolVersion Sym : V.Globals)
619 if (Sym.IsExternCpp)
620 return true;
621 return false;
622}
623
George Rimare2051ef2016-09-13 10:45:39 +0000624static ArrayRef<SymbolBody *>
625findDemangled(std::map<std::string, std::vector<SymbolBody *>> &D,
626 StringRef Name) {
George Rimarc3ec9d02016-08-30 09:29:37 +0000627 auto I = D.find(Name);
628 if (I != D.end())
629 return I->second;
George Rimare2051ef2016-09-13 10:45:39 +0000630 return {};
George Rimarc3ec9d02016-08-30 09:29:37 +0000631}
632
George Rimar397cd87a2016-08-30 09:35:03 +0000633static std::vector<SymbolBody *>
George Rimare2051ef2016-09-13 10:45:39 +0000634findAllDemangled(const std::map<std::string, std::vector<SymbolBody *>> &D,
George Rimarc91930a2016-09-02 21:17:20 +0000635 const Regex &Re) {
George Rimar397cd87a2016-08-30 09:35:03 +0000636 std::vector<SymbolBody *> Res;
637 for (auto &P : D) {
George Rimare2051ef2016-09-13 10:45:39 +0000638 if (const_cast<Regex &>(Re).match(P.first))
639 for (SymbolBody *Body : P.second)
640 if (!Body->isUndefined())
641 Res.push_back(Body);
George Rimar397cd87a2016-08-30 09:35:03 +0000642 }
643 return Res;
644}
645
Rui Ueyamaea265042016-09-13 20:51:30 +0000646// If there's only one anonymous version definition in a version
647// script file, the script does not actullay define any symbol version,
648// but just specifies symbols visibilities. We assume that the script was
649// in the form of { global: foo; bar; local *; }. So, local is default.
650// In this function, we make specified symbols global.
651template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() {
652 std::vector<StringRef> Patterns;
653 for (SymbolVersion &Sym : Config->VersionScriptGlobals) {
654 if (hasWildcard(Sym.Name)) {
655 Patterns.push_back(Sym.Name);
656 continue;
657 }
658 if (SymbolBody *B = find(Sym.Name))
659 B->symbol()->VersionId = VER_NDX_GLOBAL;
660 }
661 if (Patterns.empty())
662 return;
663 Regex Re = compileGlobPatterns(Patterns);
664 std::vector<SymbolBody *> Syms = findAll(Re);
665 for (SymbolBody *B : Syms)
666 B->symbol()->VersionId = VER_NDX_GLOBAL;
667}
668
Rui Ueyamadad2b882016-09-02 22:15:08 +0000669// This function processes version scripts by updating VersionId
670// member of symbols.
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000671template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
Rui Ueyamaea265042016-09-13 20:51:30 +0000672 // Handle edge cases first.
George Rimard3566302016-06-20 11:55:12 +0000673 if (!Config->VersionScriptGlobals.empty()) {
Rui Ueyamaea265042016-09-13 20:51:30 +0000674 handleAnonymousVersion();
George Rimard3566302016-06-20 11:55:12 +0000675 return;
676 }
677
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000678 if (Config->VersionDefinitions.empty())
George Rimarf73a2582016-07-07 07:45:27 +0000679 return;
680
Rui Ueyamadad2b882016-09-02 22:15:08 +0000681 // Now we have version definitions, so we need to set version ids to symbols.
682 // Each version definition has a glob pattern, and all symbols that match
683 // with the pattern get that version.
684
685 // Users can use "extern C++ {}" directive to match against demangled
686 // C++ symbols. For example, you can write a pattern such as
687 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
688 // other than trying to match a regexp against all demangled symbols.
689 // So, if "extern C++" feature is used, we demangle all known symbols.
George Rimare2051ef2016-09-13 10:45:39 +0000690 std::map<std::string, std::vector<SymbolBody *>> Demangled;
George Rimar50dcece2016-07-16 12:26:39 +0000691 if (hasExternCpp())
692 Demangled = getDemangledSyms();
George Rimardd64bb32016-07-13 08:19:04 +0000693
Rui Ueyamadad2b882016-09-02 22:15:08 +0000694 // First, we assign versions to exact matching symbols,
695 // i.e. version definitions not containing any glob meta-characters.
George Rimar50dcece2016-07-16 12:26:39 +0000696 for (VersionDefinition &V : Config->VersionDefinitions) {
697 for (SymbolVersion Sym : V.Globals) {
George Rimarcd574a52016-09-09 14:35:36 +0000698 if (Sym.HasWildcards)
George Rimardd64bb32016-07-13 08:19:04 +0000699 continue;
George Rimare2051ef2016-09-13 10:45:39 +0000700
George Rimarc3ec9d02016-08-30 09:29:37 +0000701 StringRef N = Sym.Name;
George Rimare2051ef2016-09-13 10:45:39 +0000702 ArrayRef<SymbolBody *> Arr = Sym.IsExternCpp
703 ? findDemangled(Demangled, N)
704 : ArrayRef<SymbolBody *>(find(N));
705 for (SymbolBody *B : Arr)
706 setVersionId(B, V.Name, N, V.Id);
George Rimar36b2c0a2016-06-28 08:07:26 +0000707 }
George Rimarf73a2582016-07-07 07:45:27 +0000708 }
709
Rui Ueyamadad2b882016-09-02 22:15:08 +0000710 // Next, we assign versions to fuzzy matching symbols,
711 // i.e. version definitions containing glob meta-characters.
712 // Note that because the last match takes precedence over previous matches,
713 // we iterate over the definitions in the reverse order.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000714 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) {
715 VersionDefinition &V = Config->VersionDefinitions[I];
George Rimar7af64522016-08-30 09:39:36 +0000716 for (SymbolVersion &Sym : V.Globals) {
George Rimarcd574a52016-09-09 14:35:36 +0000717 if (!Sym.HasWildcards)
George Rimar7af64522016-08-30 09:39:36 +0000718 continue;
Rui Ueyamadad2b882016-09-02 22:15:08 +0000719 Regex Re = compileGlobPatterns({Sym.Name});
720 std::vector<SymbolBody *> Syms =
721 Sym.IsExternCpp ? findAllDemangled(Demangled, Re) : findAll(Re);
George Rimar397cd87a2016-08-30 09:35:03 +0000722
Rui Ueyamadad2b882016-09-02 22:15:08 +0000723 // Exact matching takes precendence over fuzzy matching,
724 // so we set a version to a symbol only if no version has been assigned
725 // to the symbol. This behavior is compatible with GNU.
726 for (SymbolBody *B : Syms)
George Rimar7af64522016-08-30 09:39:36 +0000727 if (B->symbol()->VersionId == Config->DefaultSymbolVersion)
728 B->symbol()->VersionId = V.Id;
729 }
George Rimard3566302016-06-20 11:55:12 +0000730 }
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000731}
732
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000733template class elf::SymbolTable<ELF32LE>;
734template class elf::SymbolTable<ELF32BE>;
735template class elf::SymbolTable<ELF64LE>;
736template class elf::SymbolTable<ELF64BE>;