blob: 1d55c3331e22cb787d155427ae37403d1f10977d [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 Espindola192e1fa2015-08-06 15:08:23 +000011#include "Error.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000012#include "Symbols.h"
13
14using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000015using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17using namespace lld;
18using namespace lld::elf2;
19
Michael J. Spencerac5f0482015-09-08 22:51:46 +000020SymbolTable::SymbolTable() {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000022void SymbolTable::addFile(std::unique_ptr<InputFile> File) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000023 File->parse();
24 InputFile *FileP = File.release();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000025 if (auto *AF = dyn_cast<ArchiveFile>(FileP)) {
26 ArchiveFiles.emplace_back(AF);
27 for (Lazy &Sym : AF->getLazySymbols())
28 addLazy(&Sym);
29 return;
30 }
31 addELFFile(cast<ELFFileBase>(FileP));
Michael J. Spencer84487f12015-07-24 21:03:07 +000032}
33
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000034template <class ELFT> void SymbolTable::init() {
Michael J. Spencer546c64c2015-09-08 22:34:57 +000035 EntrySym = new (Alloc) Undefined<ELFT>("_start", Undefined<ELFT>::Synthetic);
36 resolve<ELFT>(EntrySym);
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000037}
38
Rafael Espindola824d1a92015-09-04 00:09:43 +000039template <class ELFT> void SymbolTable::addELFFile(ELFFileBase *File) {
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000040 if (const ELFFileBase *Old = getFirstELF()) {
Rafael Espindola8aeb13f2015-09-03 19:13:13 +000041 if (!Old->isCompatibleWith(*File))
42 error(Twine(Old->getName() + " is incompatible with " + File->getName()));
Rafael Espindoladaa92a62015-08-31 01:16:19 +000043 } else {
Rafael Espindola824d1a92015-09-04 00:09:43 +000044 init<ELFT>();
Rafael Espindola3c9cb4b2015-08-05 12:03:34 +000045 }
46
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000047 if (auto *O = dyn_cast<ObjectFileBase>(File)) {
48 ObjectFiles.emplace_back(O);
Rafael Espindola824d1a92015-09-04 00:09:43 +000049 for (SymbolBody *Body : O->getSymbols())
50 resolve<ELFT>(Body);
Rafael Espindoladaa92a62015-08-31 01:16:19 +000051 }
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000052
Rafael Espindola18173d42015-09-08 15:50:05 +000053 if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) {
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000054 SharedFiles.emplace_back(S);
Rafael Espindola18173d42015-09-08 15:50:05 +000055 for (SharedSymbol<ELFT> &Body : S->getSharedSymbols())
56 resolve<ELFT>(&Body);
57 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000058}
59
Rafael Espindola824d1a92015-09-04 00:09:43 +000060void SymbolTable::addELFFile(ELFFileBase *File) {
61 switch (File->getELFKind()) {
Rui Ueyamad5004e12015-09-09 18:02:23 +000062 case ELF32LEKind:
63 addELFFile<ELF32LE>(File);
64 break;
65 case ELF32BEKind:
66 addELFFile<ELF32BE>(File);
67 break;
68 case ELF64LEKind:
69 addELFFile<ELF64LE>(File);
70 break;
71 case ELF64BEKind:
72 addELFFile<ELF64BE>(File);
73 break;
Rafael Espindola824d1a92015-09-04 00:09:43 +000074 }
75}
76
Michael J. Spencer84487f12015-07-24 21:03:07 +000077// This function resolves conflicts if there's an existing symbol with
78// the same name. Decisions are made based on symbol type.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000079template <class ELFT> void SymbolTable::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +000080 Symbol *Sym = insert(New);
81 if (Sym->Body == New)
82 return;
83
84 SymbolBody *Existing = Sym->Body;
85
86 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
87 if (New->isUndefined()) {
88 addMemberFile(L);
89 return;
90 }
91
92 // Found a definition for something also in an archive. Ignore the archive
93 // definition.
94 Sym->Body = New;
95 return;
96 }
97
98 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
99 // equivalent (conflicting), or more preferable, respectively.
100 int comp = Existing->compare<ELFT>(New);
101 if (comp < 0)
102 Sym->Body = New;
103 if (comp == 0)
104 error(Twine("duplicate symbol: ") + Sym->Body->getName());
105}
106
107Symbol *SymbolTable::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000108 // Find an existing Symbol or create and insert a new one.
109 StringRef Name = New->getName();
110 Symbol *&Sym = Symtab[Name];
111 if (!Sym) {
112 Sym = new (Alloc) Symbol(New);
113 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000114 return Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000115 }
116 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000117 return Sym;
118}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000119
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000120void SymbolTable::addLazy(Lazy *New) {
121 Symbol *Sym = insert(New);
122 if (Sym->Body == New)
123 return;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000124 SymbolBody *Existing = Sym->Body;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000125 if (Existing->isDefined() || Existing->isLazy())
126 return;
127 Sym->Body = New;
128 assert(Existing->isUndefined() && "Unexpected symbol kind.");
129 addMemberFile(New);
130}
131
132void SymbolTable::addMemberFile(Lazy *Body) {
133 std::unique_ptr<InputFile> File = Body->getMember();
134
135 // getMember returns nullptr if the member was already read from the library.
136 if (!File)
137 return;
138
139 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000140}