blob: 708ea545d20764e164c7a320d4415c820bc63a5b [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;
Michael J. Spencer84487f12015-07-24 21:03:07 +000017
18using namespace lld;
19using namespace lld::elf2;
20
Michael J. Spencerac5f0482015-09-08 22:51:46 +000021SymbolTable::SymbolTable() {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000023void SymbolTable::addFile(std::unique_ptr<InputFile> File) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000024 File->parse();
25 InputFile *FileP = File.release();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000026 if (auto *AF = dyn_cast<ArchiveFile>(FileP)) {
27 ArchiveFiles.emplace_back(AF);
28 for (Lazy &Sym : AF->getLazySymbols())
29 addLazy(&Sym);
30 return;
31 }
32 addELFFile(cast<ELFFileBase>(FileP));
Michael J. Spencer84487f12015-07-24 21:03:07 +000033}
34
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000035template <class ELFT> void SymbolTable::init() {
Rafael Espindola4340aad2015-09-11 22:42:45 +000036 if (Config->Shared)
37 return;
Michael J. Spencer546c64c2015-09-08 22:34:57 +000038 EntrySym = new (Alloc) Undefined<ELFT>("_start", Undefined<ELFT>::Synthetic);
39 resolve<ELFT>(EntrySym);
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000040}
41
Rafael Espindola824d1a92015-09-04 00:09:43 +000042template <class ELFT> void SymbolTable::addELFFile(ELFFileBase *File) {
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000043 if (const ELFFileBase *Old = getFirstELF()) {
Rafael Espindola8aeb13f2015-09-03 19:13:13 +000044 if (!Old->isCompatibleWith(*File))
45 error(Twine(Old->getName() + " is incompatible with " + File->getName()));
Rafael Espindoladaa92a62015-08-31 01:16:19 +000046 } else {
Rafael Espindola824d1a92015-09-04 00:09:43 +000047 init<ELFT>();
Rafael Espindola3c9cb4b2015-08-05 12:03:34 +000048 }
49
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000050 if (auto *O = dyn_cast<ObjectFileBase>(File)) {
51 ObjectFiles.emplace_back(O);
Rafael Espindola824d1a92015-09-04 00:09:43 +000052 for (SymbolBody *Body : O->getSymbols())
53 resolve<ELFT>(Body);
Rafael Espindoladaa92a62015-08-31 01:16:19 +000054 }
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000055
Rafael Espindola18173d42015-09-08 15:50:05 +000056 if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) {
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000057 SharedFiles.emplace_back(S);
Rafael Espindola18173d42015-09-08 15:50:05 +000058 for (SharedSymbol<ELFT> &Body : S->getSharedSymbols())
59 resolve<ELFT>(&Body);
60 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000061}
62
Rafael Espindola824d1a92015-09-04 00:09:43 +000063void SymbolTable::addELFFile(ELFFileBase *File) {
64 switch (File->getELFKind()) {
Rui Ueyamad5004e12015-09-09 18:02:23 +000065 case ELF32LEKind:
66 addELFFile<ELF32LE>(File);
67 break;
68 case ELF32BEKind:
69 addELFFile<ELF32BE>(File);
70 break;
71 case ELF64LEKind:
72 addELFFile<ELF64LE>(File);
73 break;
74 case ELF64BEKind:
75 addELFFile<ELF64BE>(File);
76 break;
Rafael Espindola824d1a92015-09-04 00:09:43 +000077 }
78}
79
Michael J. Spencer84487f12015-07-24 21:03:07 +000080// This function resolves conflicts if there's an existing symbol with
81// the same name. Decisions are made based on symbol type.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000082template <class ELFT> void SymbolTable::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +000083 Symbol *Sym = insert(New);
84 if (Sym->Body == New)
85 return;
86
87 SymbolBody *Existing = Sym->Body;
88
89 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
90 if (New->isUndefined()) {
91 addMemberFile(L);
92 return;
93 }
94
95 // Found a definition for something also in an archive. Ignore the archive
96 // definition.
97 Sym->Body = New;
98 return;
99 }
100
101 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
102 // equivalent (conflicting), or more preferable, respectively.
103 int comp = Existing->compare<ELFT>(New);
104 if (comp < 0)
105 Sym->Body = New;
106 if (comp == 0)
107 error(Twine("duplicate symbol: ") + Sym->Body->getName());
108}
109
110Symbol *SymbolTable::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111 // Find an existing Symbol or create and insert a new one.
112 StringRef Name = New->getName();
113 Symbol *&Sym = Symtab[Name];
114 if (!Sym) {
115 Sym = new (Alloc) Symbol(New);
116 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000117 return Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000118 }
119 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000120 return Sym;
121}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000122
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000123void SymbolTable::addLazy(Lazy *New) {
124 Symbol *Sym = insert(New);
125 if (Sym->Body == New)
126 return;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000127 SymbolBody *Existing = Sym->Body;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000128 if (Existing->isDefined() || Existing->isLazy())
129 return;
130 Sym->Body = New;
131 assert(Existing->isUndefined() && "Unexpected symbol kind.");
132 addMemberFile(New);
133}
134
135void SymbolTable::addMemberFile(Lazy *Body) {
136 std::unique_ptr<InputFile> File = Body->getMember();
137
138 // getMember returns nullptr if the member was already read from the library.
139 if (!File)
140 return;
141
142 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000143}