1 //===- 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 // Symbol table is a bag of all known symbols. We put all symbols of 11 // all input files to the symbol table. The symbol table is basically 12 // a hash table with the logic to resolve symbol name conflicts using 13 // the symbol types. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "SymbolTable.h" 18 #include "Config.h" 19 #include "Error.h" 20 #include "Symbols.h" 21 #include "llvm/Bitcode/ReaderWriter.h" 22 #include "llvm/Support/StringSaver.h" 23 24 using namespace llvm; 25 using namespace llvm::object; 26 using namespace llvm::ELF; 27 28 using namespace lld; 29 using namespace lld::elf; 30 31 // All input object files must be for the same architecture 32 // (e.g. it does not make sense to link x86 object files with 33 // MIPS object files.) This function checks for that error. 34 template <class ELFT> static bool isCompatible(InputFile *FileP) { 35 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP); 36 if (!F) 37 return true; 38 if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine) 39 return true; 40 StringRef A = F->getName(); 41 StringRef B = Config->Emulation; 42 if (B.empty()) 43 B = Config->FirstElf->getName(); 44 error(A + " is incompatible with " + B); 45 return false; 46 } 47 48 // Add symbols in File to the symbol table. 49 template <class ELFT> 50 void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) { 51 InputFile *FileP = File.get(); 52 if (!isCompatible<ELFT>(FileP)) 53 return; 54 55 // .a file 56 if (auto *F = dyn_cast<ArchiveFile>(FileP)) { 57 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release())); 58 F->parse(); 59 for (Lazy &Sym : F->getLazySymbols()) 60 addLazy(&Sym); 61 return; 62 } 63 64 // .so file 65 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) { 66 // DSOs are uniquified not by filename but by soname. 67 F->parseSoName(); 68 if (!SoNames.insert(F->getSoName()).second) 69 return; 70 71 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release())); 72 F->parseRest(); 73 for (SharedSymbol<ELFT> &B : F->getSharedSymbols()) 74 resolve(&B); 75 return; 76 } 77 78 // LLVM bitcode file. 79 if (auto *F = dyn_cast<BitcodeFile>(FileP)) { 80 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release())); 81 F->parse(ComdatGroups); 82 for (SymbolBody *B : F->getSymbols()) 83 if (B) 84 resolve(B); 85 return; 86 } 87 88 // .o file 89 auto *F = cast<ObjectFile<ELFT>>(FileP); 90 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release())); 91 F->parse(ComdatGroups); 92 for (SymbolBody *B : F->getNonLocalSymbols()) 93 resolve(B); 94 } 95 96 template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() { 97 if (BitcodeFiles.empty()) 98 return; 99 100 // Compile bitcode files. 101 Lto.reset(new BitcodeCompiler); 102 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) 103 Lto->add(*F); 104 std::unique_ptr<ObjectFile<ELFT>> Obj = Lto->compile<ELFT>(); 105 106 // Replace bitcode symbols. 107 llvm::DenseSet<StringRef> DummyGroups; 108 Obj->parse(DummyGroups); 109 for (SymbolBody *Body : Obj->getNonLocalSymbols()) { 110 Symbol *Sym = insert(Body); 111 if (!Sym->Body->isUndefined() && Body->isUndefined()) 112 continue; 113 Sym->Body = Body; 114 } 115 ObjectFiles.push_back(std::move(Obj)); 116 } 117 118 // Add an undefined symbol. 119 template <class ELFT> 120 SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) { 121 auto *Sym = new (Alloc) Undefined(Name, false, STV_DEFAULT, false); 122 resolve(Sym); 123 return Sym; 124 } 125 126 // Add an undefined symbol. Unlike addUndefined, that symbol 127 // doesn't have to be resolved, thus "opt" (optional). 128 template <class ELFT> 129 SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) { 130 auto *Sym = new (Alloc) Undefined(Name, false, STV_HIDDEN, true); 131 resolve(Sym); 132 return Sym; 133 } 134 135 template <class ELFT> 136 SymbolBody *SymbolTable<ELFT>::addAbsolute(StringRef Name, Elf_Sym &ESym) { 137 // Pass nullptr because absolute symbols have no corresponding input sections. 138 auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, ESym, nullptr); 139 resolve(Sym); 140 return Sym; 141 } 142 143 template <class ELFT> 144 SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name, 145 OutputSectionBase<ELFT> &Sec, 146 uintX_t Val, uint8_t Visibility) { 147 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec, Visibility); 148 resolve(Sym); 149 return Sym; 150 } 151 152 // Add Name as an "ignored" symbol. An ignored symbol is a regular 153 // linker-synthesized defined symbol, but it is not recorded to the output 154 // file's symbol table. Such symbols are useful for some linker-defined symbols. 155 template <class ELFT> 156 SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) { 157 return addAbsolute(Name, ElfSym<ELFT>::Ignored); 158 } 159 160 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. 161 // Used to implement --wrap. 162 template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) { 163 if (Symtab.count(Name) == 0) 164 return; 165 StringSaver Saver(Alloc); 166 Symbol *Sym = addUndefined(Name)->getSymbol(); 167 Symbol *Real = addUndefined(Saver.save("__real_" + Name))->getSymbol(); 168 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->getSymbol(); 169 Real->Body = Sym->Body; 170 Sym->Body = Wrap->Body; 171 } 172 173 // Returns a file from which symbol B was created. 174 // If B does not belong to any file, returns a nullptr. 175 template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) { 176 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) { 177 ArrayRef<SymbolBody *> Syms = F->getSymbols(); 178 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) 179 return F.get(); 180 } 181 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) { 182 ArrayRef<SymbolBody *> Syms = F->getSymbols(); 183 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) 184 return F.get(); 185 } 186 return nullptr; 187 } 188 189 // Returns "(internal)", "foo.a(bar.o)" or "baz.o". 190 static std::string getFilename(InputFile *F) { 191 if (!F) 192 return "(internal)"; 193 if (!F->ArchiveName.empty()) 194 return (F->ArchiveName + "(" + F->getName() + ")").str(); 195 return F->getName(); 196 } 197 198 // Construct a string in the form of "Sym in File1 and File2". 199 // Used to construct an error message. 200 template <class ELFT> 201 std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) { 202 InputFile *F1 = findFile(Old); 203 InputFile *F2 = findFile(New); 204 StringRef Sym = Old->getName(); 205 return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2); 206 } 207 208 // This function resolves conflicts if there's an existing symbol with 209 // the same name. Decisions are made based on symbol type. 210 template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) { 211 Symbol *Sym = insert(New); 212 if (Sym->Body == New) 213 return; 214 215 SymbolBody *Existing = Sym->Body; 216 217 if (Lazy *L = dyn_cast<Lazy>(Existing)) { 218 if (auto *Undef = dyn_cast<Undefined>(New)) { 219 addMemberFile(Undef, L); 220 return; 221 } 222 // Found a definition for something also in an archive. 223 // Ignore the archive definition. 224 Sym->Body = New; 225 return; 226 } 227 228 if (New->IsTls != Existing->IsTls) { 229 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New)); 230 return; 231 } 232 233 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, 234 // equivalent (conflicting), or more preferable, respectively. 235 int Comp = Existing->compare<ELFT>(New); 236 if (Comp == 0) { 237 std::string S = "duplicate symbol: " + conflictMsg(Existing, New); 238 if (Config->AllowMultipleDefinition) 239 warning(S); 240 else 241 error(S); 242 return; 243 } 244 if (Comp < 0) 245 Sym->Body = New; 246 } 247 248 // Find an existing symbol or create and insert a new one. 249 template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) { 250 StringRef Name = New->getName(); 251 Symbol *&Sym = Symtab[Name]; 252 if (!Sym) 253 Sym = new (Alloc) Symbol{New}; 254 New->setBackref(Sym); 255 return Sym; 256 } 257 258 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { 259 auto It = Symtab.find(Name); 260 if (It == Symtab.end()) 261 return nullptr; 262 return It->second->Body; 263 } 264 265 template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) { 266 Symbol *Sym = insert(L); 267 if (Sym->Body == L) 268 return; 269 if (auto *Undef = dyn_cast<Undefined>(Sym->Body)) { 270 Sym->Body = L; 271 addMemberFile(Undef, L); 272 } 273 } 274 275 template <class ELFT> 276 void SymbolTable<ELFT>::addMemberFile(Undefined *Undef, Lazy *L) { 277 // Weak undefined symbols should not fetch members from archives. 278 // If we were to keep old symbol we would not know that an archive member was 279 // available if a strong undefined symbol shows up afterwards in the link. 280 // If a strong undefined symbol never shows up, this lazy symbol will 281 // get to the end of the link and must be treated as the weak undefined one. 282 // We set UsedInRegularObj in a similar way to what is done with shared 283 // symbols and copy information to reduce how many special cases are needed. 284 if (Undef->isWeak()) { 285 L->setUsedInRegularObj(); 286 L->setWeak(); 287 288 // FIXME: Do we need to copy more? 289 L->IsTls |= Undef->IsTls; 290 return; 291 } 292 293 // Fetch a member file that has the definition for L. 294 // getMember returns nullptr if the member was already read from the library. 295 if (std::unique_ptr<InputFile> File = L->getMember()) 296 addFile(std::move(File)); 297 } 298 299 // This function takes care of the case in which shared libraries depend on 300 // the user program (not the other way, which is usual). Shared libraries 301 // may have undefined symbols, expecting that the user program provides 302 // the definitions for them. An example is BSD's __progname symbol. 303 // We need to put such symbols to the main program's .dynsym so that 304 // shared libraries can find them. 305 // Except this, we ignore undefined symbols in DSOs. 306 template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { 307 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles) 308 for (StringRef U : File->getUndefinedSymbols()) 309 if (SymbolBody *Sym = find(U)) 310 if (Sym->isDefined()) 311 Sym->MustBeInDynSym = true; 312 } 313 314 template class elf::SymbolTable<ELF32LE>; 315 template class elf::SymbolTable<ELF32BE>; 316 template class elf::SymbolTable<ELF64LE>; 317 template class elf::SymbolTable<ELF64BE>; 318