blob: 9a2b03b23e4a49887d2748ce1d5f926b448415f5 [file] [log] [blame]
Rui Ueyama411c63602015-05-28 19:09:30 +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 "Config.h"
11#include "Driver.h"
12#include "SymbolTable.h"
13#include "lld/Core/Error.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/raw_ostream.h"
17
18namespace lld {
19namespace coff {
20
21SymbolTable::SymbolTable() {
Rui Ueyama5cff6852015-05-31 03:34:08 +000022 addSymbol(new DefinedAbsolute("__ImageBase", Config->ImageBase));
23 if (!Config->EntryName.empty())
24 addSymbol(new Undefined(Config->EntryName));
Rui Ueyama411c63602015-05-28 19:09:30 +000025}
26
27std::error_code SymbolTable::addFile(std::unique_ptr<InputFile> File) {
28 if (auto EC = File->parse())
29 return EC;
30 InputFile *FileP = File.release();
31 if (auto *P = dyn_cast<ObjectFile>(FileP))
32 return addObject(P);
33 if (auto *P = dyn_cast<ArchiveFile>(FileP))
34 return addArchive(P);
35 return addImport(cast<ImportFile>(FileP));
36}
37
38std::error_code SymbolTable::addObject(ObjectFile *File) {
39 ObjectFiles.emplace_back(File);
40 for (SymbolBody *Body : File->getSymbols())
41 if (Body->isExternal())
42 if (auto EC = resolve(Body))
43 return EC;
44
45 // If an object file contains .drectve section, read it and add
46 // files listed in the section.
47 StringRef Dir = File->getDirectives();
48 if (!Dir.empty()) {
49 std::vector<std::unique_ptr<InputFile>> Libs;
50 if (auto EC = parseDirectives(Dir, &Libs, &StringAlloc))
51 return EC;
52 for (std::unique_ptr<InputFile> &Lib : Libs)
53 addFile(std::move(Lib));
54 }
55 return std::error_code();
56}
57
58std::error_code SymbolTable::addArchive(ArchiveFile *File) {
59 ArchiveFiles.emplace_back(File);
60 for (SymbolBody *Body : File->getSymbols())
61 if (auto EC = resolve(Body))
62 return EC;
63 return std::error_code();
64}
65
66std::error_code SymbolTable::addImport(ImportFile *File) {
67 ImportFiles.emplace_back(File);
68 for (SymbolBody *Body : File->getSymbols())
69 if (auto EC = resolve(Body))
70 return EC;
71 return std::error_code();
72}
73
74bool SymbolTable::reportRemainingUndefines() {
75 bool Ret = false;
76 for (auto &I : Symtab) {
77 Symbol *Sym = I.second;
78 auto *Undef = dyn_cast<Undefined>(Sym->Body);
79 if (!Undef)
80 continue;
81 if (SymbolBody *Alias = Undef->getWeakAlias()) {
82 Sym->Body = Alias->getReplacement();
83 if (!isa<Defined>(Sym->Body)) {
84 // Aliases are yet another symbols pointed by other symbols
85 // that could also remain undefined.
86 llvm::errs() << "undefined symbol: " << Undef->getName() << "\n";
87 Ret = true;
88 }
89 continue;
90 }
91 llvm::errs() << "undefined symbol: " << Undef->getName() << "\n";
92 Ret = true;
93 }
94 return Ret;
95}
96
97// This function resolves conflicts if there's an existing symbol with
98// the same name. Decisions are made based on symbol type.
99std::error_code SymbolTable::resolve(SymbolBody *New) {
100 // Find an existing Symbol or create and insert a new one.
101 StringRef Name = New->getName();
102 Symbol *&Sym = Symtab[Name];
103 if (!Sym) {
104 Sym = new (Alloc) Symbol(New);
105 New->setBackref(Sym);
106 return std::error_code();
107 }
108 New->setBackref(Sym);
109
110 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
111 // equivalent (conflicting), or more preferable, respectively.
112 SymbolBody *Existing = Sym->Body;
113 int comp = Existing->compare(New);
114 if (comp < 0)
115 Sym->Body = New;
116 if (comp == 0)
117 return make_dynamic_error_code(Twine("duplicate symbol: ") + Name);
118
119 // If we have an Undefined symbol for a Lazy symbol, we need
120 // to read an archive member to replace the Lazy symbol with
121 // a Defined symbol.
122 if (isa<Undefined>(Existing) || isa<Undefined>(New))
123 if (auto *B = dyn_cast<Lazy>(Sym->Body))
124 return addMemberFile(B);
125 return std::error_code();
126}
127
128// Reads an archive member file pointed by a given symbol.
129std::error_code SymbolTable::addMemberFile(Lazy *Body) {
130 auto FileOrErr = Body->getMember();
131 if (auto EC = FileOrErr.getError())
132 return EC;
133 std::unique_ptr<InputFile> File = std::move(FileOrErr.get());
134
135 // getMember returns an empty buffer if the member was already
136 // read from the library.
137 if (!File)
138 return std::error_code();
139 if (Config->Verbose)
140 llvm::dbgs() << "Loaded " << File->getShortName() << " for "
141 << Body->getName() << "\n";
142 return addFile(std::move(File));
143}
144
145std::vector<Chunk *> SymbolTable::getChunks() {
146 std::vector<Chunk *> Res;
147 for (std::unique_ptr<ObjectFile> &File : ObjectFiles) {
148 std::vector<Chunk *> &V = File->getChunks();
149 Res.insert(Res.end(), V.begin(), V.end());
150 }
151 return Res;
152}
153
Rui Ueyama5cff6852015-05-31 03:34:08 +0000154Defined *SymbolTable::find(StringRef Name) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000155 auto It = Symtab.find(Name);
156 if (It == Symtab.end())
157 return nullptr;
Rui Ueyama5cff6852015-05-31 03:34:08 +0000158 if (auto *Def = dyn_cast<Defined>(It->second->Body))
159 return Def;
160 return nullptr;
161}
162
163// Link default entry point name.
164ErrorOr<StringRef> SymbolTable::findDefaultEntry() {
165 static const char *Entries[][2] = {
166 {"mainCRTStartup", "mainCRTStartup"},
167 {"wmainCRTStartup", "wmainCRTStartup"},
168 {"WinMainCRTStartup", "WinMainCRTStartup"},
169 {"wWinMainCRTStartup", "wWinMainCRTStartup"},
170 {"main", "mainCRTStartup"},
171 {"wmain", "wmainCRTStartup"},
172 {"WinMain", "WinMainCRTStartup"},
173 {"wWinMain", "wWinMainCRTStartup"},
174 };
175 for (size_t I = 0; I < sizeof(Entries); ++I) {
176 if (!find(Entries[I][0]))
177 continue;
178 if (auto EC = addSymbol(new Undefined(Entries[I][1])))
179 return EC;
180 return StringRef(Entries[I][1]);
181 }
182 return make_dynamic_error_code("entry point must be defined");
183}
184
185std::error_code SymbolTable::addSymbol(SymbolBody *Body) {
186 OwningSymbols.push_back(std::unique_ptr<SymbolBody>(Body));
187 return resolve(Body);
Rui Ueyama411c63602015-05-28 19:09:30 +0000188}
189
190void SymbolTable::dump() {
191 for (auto &P : Symtab) {
192 Symbol *Ref = P.second;
193 if (auto *Body = dyn_cast<Defined>(Ref->Body))
194 llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA())
195 << " " << Body->getName() << "\n";
196 }
197}
198
199} // namespace coff
200} // namespace lld