1 //===- Symbols.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 "Error.h" 11 #include "InputFiles.h" 12 #include "Symbols.h" 13 #include "llvm/ADT/STLExtras.h" 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/raw_ostream.h" 16 17 using namespace llvm::object; 18 using llvm::sys::fs::identify_magic; 19 using llvm::sys::fs::file_magic; 20 21 namespace lld { 22 namespace coff { 23 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. 32 if (Name.empty()) { 33 auto *D = cast<DefinedCOFF>(this); 34 D->File->getCOFFObj()->getSymbolName(D->Sym, Name); 35 } 36 return Name; 37 } 38 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(); 43 44 // Normalize so that the smaller kind is on the left. 45 if (LK > RK) 46 return -Other->compare(this); 47 48 // First handle comparisons between two different kinds. 49 if (LK != RK) { 50 if (RK > LastDefinedKind) { 51 if (LK == LazyKind && cast<Undefined>(Other)->WeakAlias) 52 return -1; 53 54 // The LHS is either defined or lazy and so it wins. 55 assert((LK <= LastDefinedKind || LK == LazyKind) && "Bad kind!"); 56 return 1; 57 } 58 59 // Bitcode has special complexities. 60 if (RK == DefinedBitcodeKind) { 61 auto *RHS = cast<DefinedBitcode>(Other); 62 63 switch (LK) { 64 case DefinedCommonKind: 65 return 1; 66 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) 75 return 1; 76 77 // Fallthrough to the default of a tie otherwise. 78 default: 79 return 0; 80 } 81 } 82 83 // Either of the object file kind will trump a higher kind. 84 if (LK <= LastDefinedCOFFKind) 85 return 1; 86 87 // The remaining kind pairs are ties amongst defined symbols. 88 return 0; 89 } 90 91 // Now handle the case where the kinds are the same. 92 switch (LK) { 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; 98 return 0; 99 } 100 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; 107 } 108 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) 114 return 0; 115 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; 121 } 122 123 case LazyKind: { 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; 128 } 129 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->repl() != RHS->WeakAlias->repl()) 136 return 0; 137 return uintptr_t(LHS) < uintptr_t(RHS) ? 1 : -1; 138 } 139 return LHS->WeakAlias ? 1 : -1; 140 } 141 142 case DefinedLocalImportKind: 143 case DefinedImportThunkKind: 144 case DefinedImportDataKind: 145 case DefinedAbsoluteKind: 146 // These all simply tie. 147 return 0; 148 } 149 llvm_unreachable("unknown symbol kind"); 150 } 151 152 std::string SymbolBody::getDebugName() { 153 std::string N = getName().str(); 154 if (auto *D = dyn_cast<DefinedCOFF>(this)) { 155 N += " "; 156 N += D->File->getShortName(); 157 } else if (auto *D = dyn_cast<DefinedBitcode>(this)) { 158 N += " "; 159 N += D->File->getShortName(); 160 } 161 return N; 162 } 163 164 uint64_t Defined::getRVA() { 165 switch (kind()) { 166 case DefinedAbsoluteKind: 167 return cast<DefinedAbsolute>(this)->getRVA(); 168 case DefinedImportDataKind: 169 return cast<DefinedImportData>(this)->getRVA(); 170 case DefinedImportThunkKind: 171 return cast<DefinedImportThunk>(this)->getRVA(); 172 case DefinedLocalImportKind: 173 return cast<DefinedLocalImport>(this)->getRVA(); 174 case DefinedCommonKind: 175 return cast<DefinedCommon>(this)->getRVA(); 176 case DefinedRegularKind: 177 return cast<DefinedRegular>(this)->getRVA(); 178 179 case DefinedBitcodeKind: 180 llvm_unreachable("There is no address for a bitcode symbol."); 181 case LazyKind: 182 case UndefinedKind: 183 llvm_unreachable("Cannot get the address for an undefined symbol."); 184 } 185 llvm_unreachable("unknown symbol kind"); 186 } 187 188 uint64_t Defined::getFileOff() { 189 switch (kind()) { 190 case DefinedImportDataKind: 191 return cast<DefinedImportData>(this)->getFileOff(); 192 case DefinedImportThunkKind: 193 return cast<DefinedImportThunk>(this)->getFileOff(); 194 case DefinedLocalImportKind: 195 return cast<DefinedLocalImport>(this)->getFileOff(); 196 case DefinedCommonKind: 197 return cast<DefinedCommon>(this)->getFileOff(); 198 case DefinedRegularKind: 199 return cast<DefinedRegular>(this)->getFileOff(); 200 201 case DefinedBitcodeKind: 202 llvm_unreachable("There is no file offset for a bitcode symbol."); 203 case DefinedAbsoluteKind: 204 llvm_unreachable("Cannot get a file offset for an absolute symbol."); 205 case LazyKind: 206 case UndefinedKind: 207 llvm_unreachable("Cannot get a file offset for an undefined symbol."); 208 } 209 llvm_unreachable("unknown symbol kind"); 210 } 211 212 COFFSymbolRef DefinedCOFF::getCOFFSymbol() { 213 size_t SymSize = File->getCOFFObj()->getSymbolTableEntrySize(); 214 if (SymSize == sizeof(coff_symbol16)) 215 return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(Sym)); 216 assert(SymSize == sizeof(coff_symbol32)); 217 return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(Sym)); 218 } 219 220 ErrorOr<std::unique_ptr<InputFile>> Lazy::getMember() { 221 auto MBRefOrErr = File->getMember(&Sym); 222 if (auto EC = MBRefOrErr.getError()) 223 return EC; 224 MemoryBufferRef MBRef = MBRefOrErr.get(); 225 226 // getMember returns an empty buffer if the member was already 227 // read from the library. 228 if (MBRef.getBuffer().empty()) 229 return std::unique_ptr<InputFile>(nullptr); 230 231 file_magic Magic = identify_magic(MBRef.getBuffer()); 232 if (Magic == file_magic::coff_import_library) 233 return std::unique_ptr<InputFile>(new ImportFile(MBRef)); 234 235 std::unique_ptr<InputFile> Obj; 236 if (Magic == file_magic::coff_object) { 237 Obj.reset(new ObjectFile(MBRef)); 238 } else if (Magic == file_magic::bitcode) { 239 Obj.reset(new BitcodeFile(MBRef)); 240 } else { 241 llvm::errs() << File->getName() << ": unknown file type\n"; 242 return make_error_code(LLDError::InvalidFile); 243 } 244 245 Obj->setParentName(File->getName()); 246 return std::move(Obj); 247 } 248 249 Defined *Undefined::getWeakAlias() { 250 // A weak alias may be a weak alias to another symbol, so check recursively. 251 for (SymbolBody *A = WeakAlias; A; A = cast<Undefined>(A)->WeakAlias) 252 if (auto *D = dyn_cast<Defined>(A->repl())) 253 return D; 254 return nullptr; 255 } 256 257 } // namespace coff 258 } // namespace lld 259