blob: 741e460b5e0ac71177daf86059744183e58df6fe [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "Symbols.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000021#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/IR/LegacyPassManager.h"
Rafael Espindola7f6d50b2016-03-05 14:51:51 +000023#include "llvm/Linker/IRMover.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000024#include "llvm/Support/StringSaver.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000025#include "llvm/Support/TargetRegistry.h"
26#include "llvm/Target/TargetMachine.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
28using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000029using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000030using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
32using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000033using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000034
Rui Ueyamac9559d92016-01-05 20:47:37 +000035// All input object files must be for the same architecture
36// (e.g. it does not make sense to link x86 object files with
37// MIPS object files.) This function checks for that error.
Rui Ueyama16ba6692016-01-29 19:41:13 +000038template <class ELFT> static bool isCompatible(InputFile *FileP) {
Rui Ueyama25b44c92015-12-16 23:31:22 +000039 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
40 if (!F)
Rui Ueyama16ba6692016-01-29 19:41:13 +000041 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000042 if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000043 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000044 StringRef A = F->getName();
45 StringRef B = Config->Emulation;
46 if (B.empty())
47 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000048 error(A + " is incompatible with " + B);
49 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000050}
51
Rui Ueyamac9559d92016-01-05 20:47:37 +000052// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000053template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000054void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000055 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000056 if (!isCompatible<ELFT>(FileP))
57 return;
Rafael Espindola525914d2015-10-11 03:36:49 +000058
Rui Ueyama89575742015-12-16 22:59:13 +000059 // .a file
60 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000061 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Rui Ueyama89575742015-12-16 22:59:13 +000062 F->parse();
63 for (Lazy &Sym : F->getLazySymbols())
Michael J. Spencer1b348a62015-09-04 22:28:10 +000064 addLazy(&Sym);
65 return;
66 }
Rui Ueyama3d451792015-10-12 18:03:21 +000067
Rui Ueyama89575742015-12-16 22:59:13 +000068 // .so file
69 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
70 // DSOs are uniquified not by filename but by soname.
71 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000072 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000073 return;
Rui Ueyama89575742015-12-16 22:59:13 +000074
Rafael Espindola21f7bd42015-12-23 14:35:51 +000075 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000076 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000077 for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
78 resolve(&B);
79 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000080 }
Rui Ueyama89575742015-12-16 22:59:13 +000081
Rafael Espindola9f77ef02016-02-12 20:54:57 +000082 // LLVM bitcode file.
83 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
84 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Rafael Espindola4de44b72016-03-02 15:43:50 +000085 F->parse(ComdatGroups);
Rafael Espindola297ce4e2016-02-26 21:31:34 +000086 for (SymbolBody *B : F->getSymbols())
Rafael Espindola9f77ef02016-02-12 20:54:57 +000087 resolve(B);
88 return;
89 }
90
Rui Ueyama89575742015-12-16 22:59:13 +000091 // .o file
92 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000093 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000094 F->parse(ComdatGroups);
Rafael Espindola67d72c02016-03-11 12:06:30 +000095 for (SymbolBody *B : F->getNonLocalSymbols())
Rui Ueyama89575742015-12-16 22:59:13 +000096 resolve(B);
Michael J. Spencer84487f12015-07-24 21:03:07 +000097}
98
Sean Silva4aaeac62016-03-09 22:30:05 +000099// This is for use when debugging LTO.
100static void saveLtoObjectFile(StringRef Buffer) {
101 std::error_code EC;
102 raw_fd_ostream OS(Config->OutputFile.str() + ".lto.o", EC,
103 sys::fs::OpenFlags::F_None);
104 check(EC);
105 OS << Buffer;
106}
107
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000108// Codegen the module M and returns the resulting InputFile.
109template <class ELFT>
110std::unique_ptr<InputFile> SymbolTable<ELFT>::codegen(Module &M) {
111 StringRef TripleStr = M.getTargetTriple();
112 Triple TheTriple(TripleStr);
113
114 // FIXME: Should we have a default triple? The gold plugin uses
115 // sys::getDefaultTargetTriple(), but that is probably wrong given that this
116 // might be a cross linker.
117
118 std::string ErrMsg;
119 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
120 if (!TheTarget)
121 fatal("Target not found: " + ErrMsg);
122
123 TargetOptions Options;
Rafael Espindola3ca9ee02016-03-02 17:21:06 +0000124 Reloc::Model R = Config->Shared ? Reloc::PIC_ : Reloc::Static;
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000125 std::unique_ptr<TargetMachine> TM(
Rafael Espindola3ca9ee02016-03-02 17:21:06 +0000126 TheTarget->createTargetMachine(TripleStr, "", "", Options, R));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000127
128 raw_svector_ostream OS(OwningLTOData);
129 legacy::PassManager CodeGenPasses;
130 if (TM->addPassesToEmitFile(CodeGenPasses, OS,
131 TargetMachine::CGFT_ObjectFile))
132 fatal("Failed to setup codegen");
133 CodeGenPasses.run(M);
134 LtoBuffer = MemoryBuffer::getMemBuffer(OwningLTOData, "", false);
Sean Silva4aaeac62016-03-09 22:30:05 +0000135 if (Config->SaveTemps)
136 saveLtoObjectFile(LtoBuffer->getBuffer());
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000137 return createObjectFile(*LtoBuffer);
138}
139
Rafael Espindola7f6d50b2016-03-05 14:51:51 +0000140static void addBitcodeFile(IRMover &Mover, BitcodeFile &F,
141 LLVMContext &Context) {
142 std::unique_ptr<MemoryBuffer> Buffer =
143 MemoryBuffer::getMemBuffer(F.MB, false);
144 std::unique_ptr<Module> M =
145 check(getLazyBitcodeModule(std::move(Buffer), Context,
Sean Silva50d27ff2016-03-09 18:38:40 +0000146 /*ShouldLazyLoadMetadata*/ false));
Rafael Espindola7f6d50b2016-03-05 14:51:51 +0000147 std::vector<GlobalValue *> Keep;
148 for (SymbolBody *B : F.getSymbols()) {
Rafael Espindola67d72c02016-03-11 12:06:30 +0000149 if (&B->repl() != B)
Rafael Espindola7f6d50b2016-03-05 14:51:51 +0000150 continue;
151 auto *DB = dyn_cast<DefinedBitcode>(B);
152 if (!DB)
153 continue;
154 GlobalValue *GV = M->getNamedValue(B->getName());
155 assert(GV);
156 Keep.push_back(GV);
157 }
Sean Silvab1b5cc82016-03-11 00:50:05 +0000158 for (StringRef S : F.getExtraKeeps()) {
159 GlobalValue *GV = M->getNamedValue(S);
160 assert(GV);
161 Keep.push_back(GV);
162 }
Rafael Espindola7f6d50b2016-03-05 14:51:51 +0000163 Mover.move(std::move(M), Keep, [](GlobalValue &, IRMover::ValueAdder) {});
164}
165
Sean Silvac43ec672016-03-09 20:06:24 +0000166// This is for use when debugging LTO.
167static void saveBCFile(Module &M) {
Sean Silva35ef3d92016-03-09 20:01:08 +0000168 std::error_code EC;
Sean Silvac43ec672016-03-09 20:06:24 +0000169 raw_fd_ostream OS(Config->OutputFile.str() + ".lto.bc", EC,
170 sys::fs::OpenFlags::F_None);
Sean Silva35ef3d92016-03-09 20:01:08 +0000171 check(EC);
172 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
173}
174
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000175// Merge all the bitcode files we have seen, codegen the result and return
176// the resulting ObjectFile.
177template <class ELFT>
178ObjectFile<ELFT> *SymbolTable<ELFT>::createCombinedLtoObject() {
179 LLVMContext Context;
180 Module Combined("ld-temp.o", Context);
Rafael Espindola7f6d50b2016-03-05 14:51:51 +0000181 IRMover Mover(Combined);
182 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
183 addBitcodeFile(Mover, *F, Context);
Sean Silva35ef3d92016-03-09 20:01:08 +0000184 if (Config->SaveTemps)
Sean Silvac43ec672016-03-09 20:06:24 +0000185 saveBCFile(Combined);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000186 std::unique_ptr<InputFile> F = codegen(Combined);
187 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(F.release()));
188 return &*ObjectFiles.back();
189}
190
191template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
192 if (BitcodeFiles.empty())
193 return;
194 ObjectFile<ELFT> *Obj = createCombinedLtoObject();
Rafael Espindola4de44b72016-03-02 15:43:50 +0000195 llvm::DenseSet<StringRef> DummyGroups;
196 Obj->parse(DummyGroups);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000197 for (SymbolBody *Body : Obj->getNonLocalSymbols()) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000198 Symbol *Sym = insert(Body);
Rafael Espindolacdf3a2a2016-03-02 18:21:46 +0000199 if (!Sym->Body->isUndefined() && Body->isUndefined())
200 continue;
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000201 Sym->Body = Body;
202 }
203}
204
Rui Ueyama01a65b12015-12-24 10:37:32 +0000205// Add an undefined symbol.
Rui Ueyamaff777682015-10-09 21:12:40 +0000206template <class ELFT>
207SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000208 auto *Sym = new (Alloc) Undefined(Name, false, STV_DEFAULT, false);
Rui Ueyamaff777682015-10-09 21:12:40 +0000209 resolve(Sym);
210 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000211}
212
Rui Ueyama01a65b12015-12-24 10:37:32 +0000213// Add an undefined symbol. Unlike addUndefined, that symbol
214// doesn't have to be resolved, thus "opt" (optional).
Rui Ueyamaff777682015-10-09 21:12:40 +0000215template <class ELFT>
216SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000217 auto *Sym = new (Alloc) Undefined(Name, false, STV_HIDDEN, true);
Rui Ueyamaff777682015-10-09 21:12:40 +0000218 resolve(Sym);
219 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +0000220}
221
Rafael Espindola0e604f92015-09-25 18:56:53 +0000222template <class ELFT>
Rui Ueyama79c73732016-01-08 21:53:28 +0000223SymbolBody *SymbolTable<ELFT>::addAbsolute(StringRef Name, Elf_Sym &ESym) {
224 // Pass nullptr because absolute symbols have no corresponding input sections.
225 auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, ESym, nullptr);
226 resolve(Sym);
227 return Sym;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000228}
229
230template <class ELFT>
Rui Ueyama79c73732016-01-08 21:53:28 +0000231SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name,
George Rimaraa4dc202016-03-01 16:23:13 +0000232 OutputSectionBase<ELFT> &Sec,
233 uintX_t Val, uint8_t Visibility) {
234 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec, Visibility);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000235 resolve(Sym);
Rui Ueyama79c73732016-01-08 21:53:28 +0000236 return Sym;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000237}
238
Rui Ueyamac9559d92016-01-05 20:47:37 +0000239// Add Name as an "ignored" symbol. An ignored symbol is a regular
240// linker-synthesized defined symbol, but it is not recorded to the output
241// file's symbol table. Such symbols are useful for some linker-defined symbols.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000242template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000243SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) {
Rafael Espindola65e80b92016-01-19 21:19:52 +0000244 return addAbsolute(Name, ElfSym<ELFT>::Ignored);
Rafael Espindola5d413262015-10-01 21:22:26 +0000245}
246
Rui Ueyamadeb15402016-01-07 17:20:07 +0000247// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
248// Used to implement --wrap.
249template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
250 if (Symtab.count(Name) == 0)
251 return;
252 StringSaver Saver(Alloc);
253 Symbol *Sym = addUndefined(Name)->getSymbol();
254 Symbol *Real = addUndefined(Saver.save("__real_" + Name))->getSymbol();
255 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->getSymbol();
256 Real->Body = Sym->Body;
257 Sym->Body = Wrap->Body;
258}
259
Rui Ueyama533336a2015-12-16 22:26:48 +0000260// Returns a file from which symbol B was created.
Rui Ueyama2a65a492016-01-05 20:01:29 +0000261// If B does not belong to any file, returns a nullptr.
Rafael Espindola18f09502016-02-26 21:49:38 +0000262template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) {
Rui Ueyama533336a2015-12-16 22:26:48 +0000263 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000264 ArrayRef<SymbolBody *> Syms = F->getSymbols();
265 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
Rui Ueyama533336a2015-12-16 22:26:48 +0000266 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000267 }
Rafael Espindola18f09502016-02-26 21:49:38 +0000268 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) {
269 ArrayRef<SymbolBody *> Syms = F->getSymbols();
270 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
271 return F.get();
272 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000273 return nullptr;
274}
275
Rui Ueyama71c066d2016-02-02 08:22:41 +0000276// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Rafael Espindola18f09502016-02-26 21:49:38 +0000277static std::string getFilename(InputFile *F) {
Rui Ueyama71c066d2016-02-02 08:22:41 +0000278 if (!F)
279 return "(internal)";
280 if (!F->ArchiveName.empty())
281 return (F->ArchiveName + "(" + F->getName() + ")").str();
282 return F->getName();
283}
284
Rui Ueyamab4de5952016-01-08 22:01:33 +0000285// Construct a string in the form of "Sym in File1 and File2".
286// Used to construct an error message.
Rui Ueyama533336a2015-12-16 22:26:48 +0000287template <class ELFT>
288std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
Rafael Espindola18f09502016-02-26 21:49:38 +0000289 InputFile *F1 = findFile(Old);
290 InputFile *F2 = findFile(New);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000291 StringRef Sym = Old->getName();
Rui Ueyama71c066d2016-02-02 08:22:41 +0000292 return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000293}
294
Michael J. Spencer84487f12015-07-24 21:03:07 +0000295// This function resolves conflicts if there's an existing symbol with
296// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000297template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000298 Symbol *Sym = insert(New);
299 if (Sym->Body == New)
300 return;
301
302 SymbolBody *Existing = Sym->Body;
303
304 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000305 if (auto *Undef = dyn_cast<Undefined>(New)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000306 addMemberFile(Undef, L);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000307 return;
308 }
Rui Ueyamac5b95122015-12-16 23:23:14 +0000309 // Found a definition for something also in an archive.
310 // Ignore the archive definition.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000311 Sym->Body = New;
312 return;
313 }
314
George Rimar2f0fab52016-03-06 06:26:18 +0000315 if (New->IsTls != Existing->IsTls) {
Rui Ueyama16ba6692016-01-29 19:41:13 +0000316 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
317 return;
318 }
Igor Kudrin65bddea2015-10-09 09:58:39 +0000319
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000320 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
321 // equivalent (conflicting), or more preferable, respectively.
Rui Ueyama2e0a9ff2016-01-06 00:09:39 +0000322 int Comp = Existing->compare<ELFT>(New);
323 if (Comp == 0) {
George Rimare0943882016-03-10 16:58:34 +0000324 std::string S = "Duplicate symbol: " + conflictMsg(Existing, New);
Rui Ueyama16ba6692016-01-29 19:41:13 +0000325 if (Config->AllowMultipleDefinition)
326 warning(S);
327 else
328 error(S);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000329 return;
330 }
Rui Ueyama2e0a9ff2016-01-06 00:09:39 +0000331 if (Comp < 0)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000332 Sym->Body = New;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000333}
334
Rui Ueyamab4de5952016-01-08 22:01:33 +0000335// Find an existing symbol or create and insert a new one.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000336template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000337 StringRef Name = New->getName();
338 Symbol *&Sym = Symtab[Name];
Rui Ueyama38dcc9e2015-12-16 23:25:31 +0000339 if (!Sym)
Rui Ueyama3554f592015-12-17 00:01:25 +0000340 Sym = new (Alloc) Symbol{New};
Michael J. Spencer84487f12015-07-24 21:03:07 +0000341 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000342 return Sym;
343}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000344
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000345template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
346 auto It = Symtab.find(Name);
347 if (It == Symtab.end())
348 return nullptr;
349 return It->second->Body;
350}
351
Rui Ueyamac5b95122015-12-16 23:23:14 +0000352template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
353 Symbol *Sym = insert(L);
354 if (Sym->Body == L)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000355 return;
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000356 if (auto *Undef = dyn_cast<Undefined>(Sym->Body)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000357 Sym->Body = L;
358 addMemberFile(Undef, L);
Rafael Espindola8614c562015-10-06 14:33:58 +0000359 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000360}
361
Rui Ueyama3d451792015-10-12 18:03:21 +0000362template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000363void SymbolTable<ELFT>::addMemberFile(Undefined *Undef, Lazy *L) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000364 // Weak undefined symbols should not fetch members from archives.
365 // If we were to keep old symbol we would not know that an archive member was
366 // available if a strong undefined symbol shows up afterwards in the link.
367 // If a strong undefined symbol never shows up, this lazy symbol will
368 // get to the end of the link and must be treated as the weak undefined one.
369 // We set UsedInRegularObj in a similar way to what is done with shared
Rafael Espindola8176d572016-02-22 23:19:29 +0000370 // symbols and copy information to reduce how many special cases are needed.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000371 if (Undef->isWeak()) {
372 L->setUsedInRegularObj();
373 L->setWeak();
Rafael Espindola8176d572016-02-22 23:19:29 +0000374
375 // FIXME: Do we need to copy more?
George Rimar2f0fab52016-03-06 06:26:18 +0000376 L->IsTls |= Undef->IsTls;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000377 return;
378 }
379
380 // Fetch a member file that has the definition for L.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000381 // getMember returns nullptr if the member was already read from the library.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000382 if (std::unique_ptr<InputFile> File = L->getMember())
Rui Ueyama690db672015-10-14 22:32:10 +0000383 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000384}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000385
Rui Ueyama93bfee52015-10-13 18:10:33 +0000386// This function takes care of the case in which shared libraries depend on
387// the user program (not the other way, which is usual). Shared libraries
388// may have undefined symbols, expecting that the user program provides
389// the definitions for them. An example is BSD's __progname symbol.
390// We need to put such symbols to the main program's .dynsym so that
391// shared libraries can find them.
392// Except this, we ignore undefined symbols in DSOs.
393template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000394 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
395 for (StringRef U : File->getUndefinedSymbols())
396 if (SymbolBody *Sym = find(U))
397 if (Sym->isDefined())
Rafael Espindolaabebed92016-02-05 15:27:15 +0000398 Sym->MustBeInDynSym = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000399}
400
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000401template class elf::SymbolTable<ELF32LE>;
402template class elf::SymbolTable<ELF32BE>;
403template class elf::SymbolTable<ELF64LE>;
404template class elf::SymbolTable<ELF64BE>;