1 //===- SymbolTable.cpp ----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolTable.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
27 enum SymbolPreference
{
33 /// Checks if an existing symbol S should be kept or replaced by a new symbol.
34 /// Returns SP_EXISTING when S should be kept, SP_NEW when the new symbol
35 /// should be kept, and SP_CONFLICT if no valid resolution exists.
36 static SymbolPreference
compareDefined(Symbol
*S
, bool WasInserted
,
38 // If the symbol wasn't previously known, the new symbol wins by default.
39 if (WasInserted
|| !isa
<Defined
>(S
->body()))
42 // If the existing symbol is a DefinedRegular, both it and the new symbol
43 // must be comdats. In that case, we have no reason to prefer one symbol
44 // over the other, and we keep the existing one. If one of the symbols
45 // is not a comdat, we report a conflict.
46 if (auto *R
= dyn_cast
<DefinedRegular
>(S
->body())) {
47 if (NewIsCOMDAT
&& R
->isCOMDAT())
53 // Existing symbol is not a DefinedRegular; new symbol wins.
59 void SymbolTable::addFile(InputFile
*File
) {
60 log("Reading " + toString(File
));
63 MachineTypes MT
= File
->getMachineType();
64 if (Config
->Machine
== IMAGE_FILE_MACHINE_UNKNOWN
) {
66 } else if (MT
!= IMAGE_FILE_MACHINE_UNKNOWN
&& Config
->Machine
!= MT
) {
67 fatal(toString(File
) + ": machine type " + machineToStr(MT
) +
68 " conflicts with " + machineToStr(Config
->Machine
));
71 if (auto *F
= dyn_cast
<ObjFile
>(File
)) {
72 ObjFile::Instances
.push_back(F
);
73 } else if (auto *F
= dyn_cast
<BitcodeFile
>(File
)) {
74 BitcodeFile::Instances
.push_back(F
);
75 } else if (auto *F
= dyn_cast
<ImportFile
>(File
)) {
76 ImportFile::Instances
.push_back(F
);
79 StringRef S
= File
->getDirectives();
83 log("Directives: " + toString(File
) + ": " + S
);
84 Driver
->parseDirectives(S
);
87 void SymbolTable::reportRemainingUndefines() {
88 SmallPtrSet
<SymbolBody
*, 8> Undefs
;
89 for (auto &I
: Symtab
) {
90 Symbol
*Sym
= I
.second
;
91 auto *Undef
= dyn_cast
<Undefined
>(Sym
->body());
94 if (!Sym
->IsUsedInRegularObj
)
96 StringRef Name
= Undef
->getName();
97 // A weak alias may have been resolved, so check for that.
98 if (Defined
*D
= Undef
->getWeakAlias()) {
99 // We resolve weak aliases by replacing the alias's SymbolBody with the
100 // target's SymbolBody. This causes all SymbolBody pointers referring to
101 // the old symbol to instead refer to the new symbol. However, we can't
102 // just blindly copy sizeof(Symbol::Body) bytes from D to Sym->Body
103 // because D may be an internal symbol, and internal symbols are stored as
104 // "unparented" SymbolBodies. For that reason we need to check which type
105 // of symbol we are dealing with and copy the correct number of bytes.
106 if (isa
<DefinedRegular
>(D
))
107 memcpy(Sym
->Body
.buffer
, D
, sizeof(DefinedRegular
));
108 else if (isa
<DefinedAbsolute
>(D
))
109 memcpy(Sym
->Body
.buffer
, D
, sizeof(DefinedAbsolute
));
111 // No other internal symbols are possible.
112 Sym
->Body
= D
->symbol()->Body
;
115 // If we can resolve a symbol by removing __imp_ prefix, do that.
116 // This odd rule is for compatibility with MSVC linker.
117 if (Name
.startswith("__imp_")) {
118 Symbol
*Imp
= find(Name
.substr(strlen("__imp_")));
119 if (Imp
&& isa
<Defined
>(Imp
->body())) {
120 auto *D
= cast
<Defined
>(Imp
->body());
121 replaceBody
<DefinedLocalImport
>(Sym
, Name
, D
);
122 LocalImportChunks
.push_back(
123 cast
<DefinedLocalImport
>(Sym
->body())->getChunk());
127 // Remaining undefined symbols are not fatal if /force is specified.
128 // They are replaced with dummy defined symbols.
130 replaceBody
<DefinedAbsolute
>(Sym
, Name
, 0);
131 Undefs
.insert(Sym
->body());
135 for (SymbolBody
*B
: Config
->GCRoot
)
137 warn("<root>: undefined symbol: " + B
->getName());
138 for (ObjFile
*File
: ObjFile::Instances
)
139 for (SymbolBody
*Sym
: File
->getSymbols())
140 if (Undefs
.count(Sym
))
141 warn(toString(File
) + ": undefined symbol: " + Sym
->getName());
143 fatal("link failed");
146 std::pair
<Symbol
*, bool> SymbolTable::insert(StringRef Name
) {
147 Symbol
*&Sym
= Symtab
[CachedHashStringRef(Name
)];
150 Sym
= make
<Symbol
>();
151 Sym
->IsUsedInRegularObj
= false;
152 Sym
->PendingArchiveLoad
= false;
156 Symbol
*SymbolTable::addUndefined(StringRef Name
, InputFile
*F
,
160 std::tie(S
, WasInserted
) = insert(Name
);
161 if (!F
|| !isa
<BitcodeFile
>(F
))
162 S
->IsUsedInRegularObj
= true;
163 if (WasInserted
|| (isa
<Lazy
>(S
->body()) && IsWeakAlias
)) {
164 replaceBody
<Undefined
>(S
, Name
);
167 if (auto *L
= dyn_cast
<Lazy
>(S
->body())) {
168 if (!S
->PendingArchiveLoad
) {
169 S
->PendingArchiveLoad
= true;
170 L
->File
->addMember(&L
->Sym
);
176 void SymbolTable::addLazy(ArchiveFile
*F
, const Archive::Symbol Sym
) {
177 StringRef Name
= Sym
.getName();
180 std::tie(S
, WasInserted
) = insert(Name
);
182 replaceBody
<Lazy
>(S
, F
, Sym
);
185 auto *U
= dyn_cast
<Undefined
>(S
->body());
186 if (!U
|| U
->WeakAlias
|| S
->PendingArchiveLoad
)
188 S
->PendingArchiveLoad
= true;
192 void SymbolTable::reportDuplicate(Symbol
*Existing
, InputFile
*NewFile
) {
193 error("duplicate symbol: " + toString(*Existing
->body()) + " in " +
194 toString(Existing
->body()->getFile()) + " and in " +
195 (NewFile
? toString(NewFile
) : "(internal)"));
198 Symbol
*SymbolTable::addAbsolute(StringRef N
, COFFSymbolRef Sym
) {
201 std::tie(S
, WasInserted
) = insert(N
);
202 S
->IsUsedInRegularObj
= true;
203 if (WasInserted
|| isa
<Undefined
>(S
->body()) || isa
<Lazy
>(S
->body()))
204 replaceBody
<DefinedAbsolute
>(S
, N
, Sym
);
205 else if (!isa
<DefinedCOFF
>(S
->body()))
206 reportDuplicate(S
, nullptr);
210 Symbol
*SymbolTable::addAbsolute(StringRef N
, uint64_t VA
) {
213 std::tie(S
, WasInserted
) = insert(N
);
214 S
->IsUsedInRegularObj
= true;
215 if (WasInserted
|| isa
<Undefined
>(S
->body()) || isa
<Lazy
>(S
->body()))
216 replaceBody
<DefinedAbsolute
>(S
, N
, VA
);
217 else if (!isa
<DefinedCOFF
>(S
->body()))
218 reportDuplicate(S
, nullptr);
222 Symbol
*SymbolTable::addSynthetic(StringRef N
, Chunk
*C
) {
225 std::tie(S
, WasInserted
) = insert(N
);
226 S
->IsUsedInRegularObj
= true;
227 if (WasInserted
|| isa
<Undefined
>(S
->body()) || isa
<Lazy
>(S
->body()))
228 replaceBody
<DefinedSynthetic
>(S
, N
, C
);
229 else if (!isa
<DefinedCOFF
>(S
->body()))
230 reportDuplicate(S
, nullptr);
234 Symbol
*SymbolTable::addRegular(InputFile
*F
, StringRef N
, bool IsCOMDAT
,
235 const coff_symbol_generic
*Sym
,
239 std::tie(S
, WasInserted
) = insert(N
);
240 if (!isa
<BitcodeFile
>(F
))
241 S
->IsUsedInRegularObj
= true;
242 SymbolPreference SP
= compareDefined(S
, WasInserted
, IsCOMDAT
);
243 if (SP
== SP_CONFLICT
) {
244 reportDuplicate(S
, F
);
245 } else if (SP
== SP_NEW
) {
246 replaceBody
<DefinedRegular
>(S
, F
, N
, IsCOMDAT
, /*IsExternal*/ true, Sym
, C
);
247 } else if (SP
== SP_EXISTING
&& IsCOMDAT
&& C
) {
249 // Discard associative chunks that we've parsed so far. No need to recurse
250 // because an associative section cannot have children.
251 for (SectionChunk
*Child
: C
->children())
252 Child
->markDiscarded();
257 Symbol
*SymbolTable::addCommon(InputFile
*F
, StringRef N
, uint64_t Size
,
258 const coff_symbol_generic
*Sym
, CommonChunk
*C
) {
261 std::tie(S
, WasInserted
) = insert(N
);
262 if (!isa
<BitcodeFile
>(F
))
263 S
->IsUsedInRegularObj
= true;
264 if (WasInserted
|| !isa
<DefinedCOFF
>(S
->body()))
265 replaceBody
<DefinedCommon
>(S
, F
, N
, Size
, Sym
, C
);
266 else if (auto *DC
= dyn_cast
<DefinedCommon
>(S
->body()))
267 if (Size
> DC
->getSize())
268 replaceBody
<DefinedCommon
>(S
, F
, N
, Size
, Sym
, C
);
272 Symbol
*SymbolTable::addImportData(StringRef N
, ImportFile
*F
) {
275 std::tie(S
, WasInserted
) = insert(N
);
276 S
->IsUsedInRegularObj
= true;
277 if (WasInserted
|| isa
<Undefined
>(S
->body()) || isa
<Lazy
>(S
->body()))
278 replaceBody
<DefinedImportData
>(S
, N
, F
);
279 else if (!isa
<DefinedCOFF
>(S
->body()))
280 reportDuplicate(S
, nullptr);
284 Symbol
*SymbolTable::addImportThunk(StringRef Name
, DefinedImportData
*ID
,
288 std::tie(S
, WasInserted
) = insert(Name
);
289 S
->IsUsedInRegularObj
= true;
290 if (WasInserted
|| isa
<Undefined
>(S
->body()) || isa
<Lazy
>(S
->body()))
291 replaceBody
<DefinedImportThunk
>(S
, Name
, ID
, Machine
);
292 else if (!isa
<DefinedCOFF
>(S
->body()))
293 reportDuplicate(S
, nullptr);
297 std::vector
<Chunk
*> SymbolTable::getChunks() {
298 std::vector
<Chunk
*> Res
;
299 for (ObjFile
*File
: ObjFile::Instances
) {
300 std::vector
<Chunk
*> &V
= File
->getChunks();
301 Res
.insert(Res
.end(), V
.begin(), V
.end());
306 Symbol
*SymbolTable::find(StringRef Name
) {
307 auto It
= Symtab
.find(CachedHashStringRef(Name
));
308 if (It
== Symtab
.end())
313 Symbol
*SymbolTable::findUnderscore(StringRef Name
) {
314 if (Config
->Machine
== I386
)
315 return find(("_" + Name
).str());
319 StringRef
SymbolTable::findByPrefix(StringRef Prefix
) {
320 for (auto Pair
: Symtab
) {
321 StringRef Name
= Pair
.first
.val();
322 if (Name
.startswith(Prefix
))
328 StringRef
SymbolTable::findMangle(StringRef Name
) {
329 if (Symbol
*Sym
= find(Name
))
330 if (!isa
<Undefined
>(Sym
->body()))
332 if (Config
->Machine
!= I386
)
333 return findByPrefix(("?" + Name
+ "@@Y").str());
334 if (!Name
.startswith("_"))
336 // Search for x86 C function.
337 StringRef S
= findByPrefix((Name
+ "@").str());
340 // Search for x86 C++ non-member function.
341 return findByPrefix(("?" + Name
.substr(1) + "@@Y").str());
344 void SymbolTable::mangleMaybe(SymbolBody
*B
) {
345 auto *U
= dyn_cast
<Undefined
>(B
);
346 if (!U
|| U
->WeakAlias
)
348 StringRef Alias
= findMangle(U
->getName());
350 U
->WeakAlias
= addUndefined(Alias
);
353 SymbolBody
*SymbolTable::addUndefined(StringRef Name
) {
354 return addUndefined(Name
, nullptr, false)->body();
357 std::vector
<StringRef
> SymbolTable::compileBitcodeFiles() {
358 LTO
.reset(new BitcodeCompiler
);
359 for (BitcodeFile
*F
: BitcodeFile::Instances
)
361 return LTO
->compile();
364 void SymbolTable::addCombinedLTOObjects() {
365 if (BitcodeFile::Instances
.empty())
367 for (StringRef Object
: compileBitcodeFiles()) {
368 auto *Obj
= make
<ObjFile
>(MemoryBufferRef(Object
, "lto.tmp"));
370 ObjFile::Instances
.push_back(Obj
);