1 //===- Symbols.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "InputFiles.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
17 using namespace llvm::object;
18 using llvm::sys::fs::identify_magic;
19 using llvm::sys::fs::file_magic;
24 StringRef SymbolBody::getName() {
25 // DefinedCOFF names are read lazily for a performance reason.
26 // Non-external symbol names are never used by the linker except for logging
27 // or debugging. Their internal references are resolved not by name but by
28 // symbol index. And because they are not external, no one can refer them by
29 // name. Object files contain lots of non-external symbols, and creating
30 // StringRefs for them (which involves lots of strlen() on the string table)
31 // is a waste of time.
33 auto *D = cast<DefinedCOFF>(this);
34 D->File->getCOFFObj()->getSymbolName(D->Sym, Name);
39 // Returns 1, 0 or -1 if this symbol should take precedence
40 // over the Other, tie or lose, respectively.
41 int SymbolBody::compare(SymbolBody *Other) {
42 Kind LK = kind(), RK = Other->kind();
44 // Normalize so that the smaller kind is on the left.
46 return -Other->compare(this);
48 // First handle comparisons between two different kinds.
50 if (RK > LastDefinedKind) {
51 if (LK == LazyKind && cast<Undefined>(Other)->WeakAlias)
54 // The LHS is either defined or lazy and so it wins.
55 assert((LK <= LastDefinedKind || LK == LazyKind) && "Bad kind!");
59 // Bitcode has special complexities.
60 if (RK == DefinedBitcodeKind) {
61 auto *RHS = cast<DefinedBitcode>(Other);
64 case DefinedCommonKind:
67 case DefinedRegularKind:
68 // As an approximation, regular symbols win over bitcode symbols,
69 // but we definitely have a conflict if the regular symbol is not
70 // replaceable and neither is the bitcode symbol. We do not
71 // replicate the rest of the symbol resolution logic here; symbol
72 // resolution will be done accurately after lowering bitcode symbols
73 // to regular symbols in addCombinedLTOObject().
74 if (cast<DefinedRegular>(this)->isCOMDAT() || RHS->IsReplaceable)
77 // Fallthrough to the default of a tie otherwise.
83 // Either of the object file kind will trump a higher kind.
84 if (LK <= LastDefinedCOFFKind)
87 // The remaining kind pairs are ties amongst defined symbols.
91 // Now handle the case where the kinds are the same.
93 case DefinedRegularKind: {
94 auto *LHS = cast<DefinedRegular>(this);
95 auto *RHS = cast<DefinedRegular>(Other);
96 if (LHS->isCOMDAT() && RHS->isCOMDAT())
97 return LHS->getFileIndex() < RHS->getFileIndex() ? 1 : -1;
101 case DefinedCommonKind: {
102 auto *LHS = cast<DefinedCommon>(this);
103 auto *RHS = cast<DefinedCommon>(Other);
104 if (LHS->getSize() == RHS->getSize())
105 return LHS->getFileIndex() < RHS->getFileIndex() ? 1 : -1;
106 return LHS->getSize() > RHS->getSize() ? 1 : -1;
109 case DefinedBitcodeKind: {
110 auto *LHS = cast<DefinedBitcode>(this);
111 auto *RHS = cast<DefinedBitcode>(Other);
112 // If both are non-replaceable, we have a tie.
113 if (!LHS->IsReplaceable && !RHS->IsReplaceable)
116 // Non-replaceable symbols win, but even two replaceable symboles don't
117 // tie. If both symbols are replaceable, choice is arbitrary.
118 if (RHS->IsReplaceable && LHS->IsReplaceable)
119 return uintptr_t(LHS) < uintptr_t(RHS) ? 1 : -1;
120 return LHS->IsReplaceable ? -1 : 1;
124 // Don't tie, pick the earliest.
125 auto *LHS = cast<Lazy>(this);
126 auto *RHS = cast<Lazy>(Other);
127 return LHS->getFileIndex() < RHS->getFileIndex() ? 1 : -1;
130 case UndefinedKind: {
131 auto *LHS = cast<Undefined>(this);
132 auto *RHS = cast<Undefined>(Other);
133 // Tie if both undefined symbols have different weak aliases.
134 if (LHS->WeakAlias && RHS->WeakAlias) {
135 if (LHS->WeakAlias->getName() != RHS->WeakAlias->getName())
137 return uintptr_t(LHS) < uintptr_t(RHS) ? 1 : -1;
139 return LHS->WeakAlias ? 1 : -1;
142 case DefinedLocalImportKind:
143 case DefinedImportThunkKind:
144 case DefinedImportDataKind:
145 case DefinedAbsoluteKind:
146 case DefinedRelativeKind:
147 // These all simply tie.
150 llvm_unreachable("unknown symbol kind");
153 std::string SymbolBody::getDebugName() {
154 std::string N = getName().str();
155 if (auto *D = dyn_cast<DefinedCOFF>(this)) {
157 N += D->File->getShortName();
158 } else if (auto *D = dyn_cast<DefinedBitcode>(this)) {
160 N += D->File->getShortName();
165 COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
166 size_t SymSize = File->getCOFFObj()->getSymbolTableEntrySize();
167 if (SymSize == sizeof(coff_symbol16))
168 return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(Sym));
169 assert(SymSize == sizeof(coff_symbol32));
170 return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(Sym));
173 DefinedImportThunk::DefinedImportThunk(StringRef Name, DefinedImportData *S,
175 : Defined(DefinedImportThunkKind, Name) {
177 case AMD64: Data.reset(new ImportThunkChunkX64(S)); return;
178 case I386: Data.reset(new ImportThunkChunkX86(S)); return;
179 case ARMNT: Data.reset(new ImportThunkChunkARM(S)); return;
180 default: llvm_unreachable("unknown machine type");
184 std::unique_ptr<InputFile> Lazy::getMember() {
185 MemoryBufferRef MBRef = File->getMember(&Sym);
187 // getMember returns an empty buffer if the member was already
188 // read from the library.
189 if (MBRef.getBuffer().empty())
190 return std::unique_ptr<InputFile>(nullptr);
192 file_magic Magic = identify_magic(MBRef.getBuffer());
193 if (Magic == file_magic::coff_import_library)
194 return std::unique_ptr<InputFile>(new ImportFile(MBRef));
196 std::unique_ptr<InputFile> Obj;
197 if (Magic == file_magic::coff_object)
198 Obj.reset(new ObjectFile(MBRef));
199 else if (Magic == file_magic::bitcode)
200 Obj.reset(new BitcodeFile(MBRef));
202 fatal("unknown file type: " + File->getName());
204 Obj->setParentName(File->getName());
208 Defined *Undefined::getWeakAlias() {
209 // A weak alias may be a weak alias to another symbol, so check recursively.
210 for (SymbolBody *A = WeakAlias; A; A = cast<Undefined>(A)->WeakAlias)
211 if (auto *D = dyn_cast<Defined>(A->repl()))