blob: bfad8c333b3118250c18fc7f02c66468988575ee [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//===----------------------------------------------------------------------===//
9
10#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000011#include "Config.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000012#include "Error.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000013#include "Symbols.h"
14
15using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000016using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000017using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000018
19using namespace lld;
20using namespace lld::elf2;
21
Rui Ueyama3ce825e2015-10-09 21:07:25 +000022template <class ELFT> SymbolTable<ELFT>::SymbolTable() {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
Rui Ueyama3ce825e2015-10-09 21:07:25 +000024template <class ELFT> bool SymbolTable<ELFT>::shouldUseRela() const {
Rafael Espindola67a5da62015-09-17 14:02:10 +000025 ELFKind K = getFirstELF()->getELFKind();
26 return K == ELF64LEKind || K == ELF64BEKind;
27}
28
Rui Ueyama3ce825e2015-10-09 21:07:25 +000029template <class ELFT>
30void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola525914d2015-10-11 03:36:49 +000031
32 if (auto *E = dyn_cast<ELFFileBase>(File.get())) {
33 if (E->getELFKind() != Config->ElfKind ||
34 E->getEMachine() != Config->EMachine) {
35 StringRef A = E->getName();
36 StringRef B = Config->Emulation;
37 if (B.empty())
Rafael Espindola9afbac42015-10-11 20:19:20 +000038 B = Config->FirstElf->getName();
Rafael Espindola525914d2015-10-11 03:36:49 +000039 error(A + " is incompatible with " + B);
40 }
41 }
42
Igor Kudrin2696bbe2015-10-01 18:02:21 +000043 if (auto *AF = dyn_cast<ArchiveFile>(File.get())) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +000044 ArchiveFiles.emplace_back(std::move(File));
Igor Kudrin2696bbe2015-10-01 18:02:21 +000045 AF->parse();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000046 for (Lazy &Sym : AF->getLazySymbols())
47 addLazy(&Sym);
48 return;
49 }
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000050 if (auto *S = dyn_cast<SharedFileBase>(File.get())) {
51 S->parseSoName();
52 if (!IncludedSoNames.insert(S->getSoName()).second)
53 return;
Rafael Espindola444576d2015-10-09 19:25:07 +000054 S->parse();
55 } else {
56 cast<ObjectFileBase>(File.get())->parse(Comdats);
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000057 }
Igor Kudrin2696bbe2015-10-01 18:02:21 +000058 addELFFile(cast<ELFFileBase>(File.release()));
Michael J. Spencer84487f12015-07-24 21:03:07 +000059}
60
Rui Ueyamaff777682015-10-09 21:12:40 +000061template <class ELFT>
62SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
63 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Required);
64 resolve(Sym);
65 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +000066}
67
Rui Ueyamaff777682015-10-09 21:12:40 +000068template <class ELFT>
69SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
70 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Optional);
71 resolve(Sym);
72 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +000073}
74
Rafael Espindola0e604f92015-09-25 18:56:53 +000075template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000076void SymbolTable<ELFT>::addSyntheticSym(StringRef Name,
77 OutputSection<ELFT> &Section,
78 typename ELFFile<ELFT>::uintX_t Value) {
Rafael Espindola0e604f92015-09-25 18:56:53 +000079 typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
80 auto ESym = new (Alloc) Elf_Sym;
81 memset(ESym, 0, sizeof(Elf_Sym));
82 ESym->st_value = Value;
83 auto Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section);
Rui Ueyama3ce825e2015-10-09 21:07:25 +000084 resolve(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +000085}
86
Rui Ueyama3ce825e2015-10-09 21:07:25 +000087template <class ELFT> void SymbolTable<ELFT>::addIgnoredSym(StringRef Name) {
Rafael Espindola5d413262015-10-01 21:22:26 +000088 auto Sym = new (Alloc)
89 DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
Rui Ueyama3ce825e2015-10-09 21:07:25 +000090 resolve(Sym);
Rafael Espindola5d413262015-10-01 21:22:26 +000091}
92
Rui Ueyama3ce825e2015-10-09 21:07:25 +000093template <class ELFT> void SymbolTable<ELFT>::addELFFile(ELFFileBase *File) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +000094 if (auto *O = dyn_cast<ObjectFile<ELFT>>(File))
Rafael Espindolab90582db2015-10-06 15:03:52 +000095 ObjectFiles.emplace_back(O);
96 else if (auto *S = dyn_cast<SharedFile<ELFT>>(File))
97 SharedFiles.emplace_back(S);
98
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000099 if (auto *O = dyn_cast<ObjectFileBase>(File)) {
Rafael Espindola824d1a92015-09-04 00:09:43 +0000100 for (SymbolBody *Body : O->getSymbols())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000101 resolve(Body);
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000102 }
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000103
Rafael Espindola18173d42015-09-08 15:50:05 +0000104 if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000105 for (SharedSymbol<ELFT> &Body : S->getSharedSymbols())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000106 resolve(&Body);
Rafael Espindola824d1a92015-09-04 00:09:43 +0000107 }
108}
109
Rafael Espindola1a49e582015-09-23 14:10:24 +0000110template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000111void SymbolTable<ELFT>::reportConflict(const Twine &Message,
112 const SymbolBody &Old,
113 const SymbolBody &New, bool Warning) {
Rafael Espindola1a49e582015-09-23 14:10:24 +0000114 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
115 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
116
117 const Elf_Sym &OldE = cast<ELFSymbolBody<ELFT>>(Old).Sym;
118 const Elf_Sym &NewE = cast<ELFSymbolBody<ELFT>>(New).Sym;
119 ELFFileBase *OldFile = nullptr;
120 ELFFileBase *NewFile = nullptr;
121
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000122 for (const std::unique_ptr<ObjectFile<ELFT>> &File : ObjectFiles) {
123 Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable());
Rafael Espindola1a49e582015-09-23 14:10:24 +0000124 if (&OldE > Syms.begin() && &OldE < Syms.end())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000125 OldFile = File.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000126 if (&NewE > Syms.begin() && &NewE < Syms.end())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000127 NewFile = File.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000128 }
129
Igor Kudrin65bddea2015-10-09 09:58:39 +0000130 std::string Msg = (Message + ": " + Old.getName() + " in " +
Rafael Espindola4b2ca852015-09-28 20:30:11 +0000131 OldFile->getName() + " and " + NewFile->getName())
132 .str();
Igor Kudrin65bddea2015-10-09 09:58:39 +0000133 if (Warning)
Rafael Espindola4b2ca852015-09-28 20:30:11 +0000134 warning(Msg);
135 else
136 error(Msg);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000137}
138
Michael J. Spencer84487f12015-07-24 21:03:07 +0000139// This function resolves conflicts if there's an existing symbol with
140// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000141template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000142 Symbol *Sym = insert(New);
143 if (Sym->Body == New)
144 return;
145
146 SymbolBody *Existing = Sym->Body;
147
148 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
149 if (New->isUndefined()) {
Rafael Espindola85a6e0f2015-10-06 15:18:50 +0000150 if (New->isWeak()) {
151 // See the explanation in SymbolTable::addLazy
152 L->setUsedInRegularObj();
153 L->setWeak();
154 return;
155 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000156 addMemberFile(L);
157 return;
158 }
159
160 // Found a definition for something also in an archive. Ignore the archive
161 // definition.
162 Sym->Body = New;
163 return;
164 }
165
Igor Kudrin65bddea2015-10-09 09:58:39 +0000166 if (New->isTLS() != Existing->isTLS())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000167 reportConflict("TLS attribute mismatch for symbol", *Existing, *New, false);
Igor Kudrin65bddea2015-10-09 09:58:39 +0000168
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000169 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
170 // equivalent (conflicting), or more preferable, respectively.
171 int comp = Existing->compare<ELFT>(New);
172 if (comp < 0)
173 Sym->Body = New;
Rafael Espindola1a49e582015-09-23 14:10:24 +0000174 else if (comp == 0)
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000175 reportConflict("duplicate symbol", *Existing, *New,
176 Config->AllowMultipleDefinition);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000177}
178
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000179template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000180 // Find an existing Symbol or create and insert a new one.
181 StringRef Name = New->getName();
182 Symbol *&Sym = Symtab[Name];
183 if (!Sym) {
184 Sym = new (Alloc) Symbol(New);
185 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000186 return Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000187 }
188 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000189 return Sym;
190}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000191
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000192template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000193 Symbol *Sym = insert(New);
194 if (Sym->Body == New)
195 return;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000196 SymbolBody *Existing = Sym->Body;
Rafael Espindola8614c562015-10-06 14:33:58 +0000197 if (Existing->isDefined() || Existing->isLazy())
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000198 return;
199 Sym->Body = New;
200 assert(Existing->isUndefined() && "Unexpected symbol kind.");
Rafael Espindola8614c562015-10-06 14:33:58 +0000201
202 // Weak undefined symbols should not fetch members from archives.
203 // If we were to keep old symbol we would not know that an archive member was
204 // available if a strong undefined symbol shows up afterwards in the link.
205 // If a strong undefined symbol never shows up, this lazy symbol will
206 // get to the end of the link and must be treated as the weak undefined one.
207 // We set UsedInRegularObj in a similar way to what is done with shared
208 // symbols and mark it as weak to reduce how many special cases are needed.
209 if (Existing->isWeak()) {
210 New->setUsedInRegularObj();
211 New->setWeak();
212 return;
213 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000214 addMemberFile(New);
215}
216
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000217template <class ELFT> void SymbolTable<ELFT>::addMemberFile(Lazy *Body) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000218 std::unique_ptr<InputFile> File = Body->getMember();
219
220 // getMember returns nullptr if the member was already read from the library.
221 if (!File)
222 return;
223
224 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000225}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000226
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000227template class lld::elf2::SymbolTable<ELF32LE>;
228template class lld::elf2::SymbolTable<ELF32BE>;
229template class lld::elf2::SymbolTable<ELF64LE>;
230template class lld::elf2::SymbolTable<ELF64BE>;