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 // Returns "(internal)", "foo.a(bar.o)" or "baz.o". 49 static std::string getFilename(InputFile *F) { 50 if (!F) 51 return "(internal)"; 52 if (!F->ArchiveName.empty()) 53 return (F->ArchiveName + "(" + F->getName() + ")").str(); 54 return F->getName(); 55 } 56 57 // Add symbols in File to the symbol table. 58 template <class ELFT> 59 void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) { 60 InputFile *FileP = File.get(); 61 if (!isCompatible<ELFT>(FileP)) 62 return; 63 64 // .a file 65 if (auto *F = dyn_cast<ArchiveFile>(FileP)) { 66 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release())); 67 F->parse(); 68 for (Lazy &Sym : F->getLazySymbols()) 69 addLazy(&Sym); 70 return; 71 } 72 73 // Lazy object file 74 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) { 75 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release())); 76 F->parse(); 77 for (Lazy &Sym : F->getLazySymbols()) 78 addLazy(&Sym); 79 return; 80 } 81 82 if (Config->Trace) 83 llvm::outs() << getFilename(FileP) << "\n"; 84 85 // .so file 86 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) { 87 // DSOs are uniquified not by filename but by soname. 88 F->parseSoName(); 89 if (!SoNames.insert(F->getSoName()).second) 90 return; 91 92 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release())); 93 F->parseRest(); 94 for (SharedSymbol<ELFT> &B : F->getSharedSymbols()) 95 resolve(&B); 96 return; 97 } 98 99 // LLVM bitcode file 100 if (auto *F = dyn_cast<BitcodeFile>(FileP)) { 101 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release())); 102 F->parse(ComdatGroups); 103 for (SymbolBody *B : F->getSymbols()) 104 if (B) 105 resolve(B); 106 return; 107 } 108 109 // Regular object file 110 auto *F = cast<ObjectFile<ELFT>>(FileP); 111 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release())); 112 F->parse(ComdatGroups); 113 for (SymbolBody *B : F->getNonLocalSymbols()) 114 resolve(B); 115 } 116 117 template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() { 118 if (BitcodeFiles.empty()) 119 return; 120 121 // Compile bitcode files. 122 Lto.reset(new BitcodeCompiler); 123 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) 124 Lto->add(*F); 125 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile(); 126 127 // Replace bitcode symbols. 128 for (auto &IF : IFs) { 129 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release()); 130 131 llvm::DenseSet<StringRef> DummyGroups; 132 Obj->parse(DummyGroups); 133 for (SymbolBody *Body : Obj->getNonLocalSymbols()) { 134 Symbol *Sym = insert(Body); 135 Sym->Body->setUsedInRegularObj(); 136 if (Sym->Body->isShared()) 137 Sym->Body->MustBeInDynSym = true; 138 if (Sym->Body->MustBeInDynSym) 139 Body->MustBeInDynSym = true; 140 if (!Sym->Body->isUndefined() && Body->isUndefined()) 141 continue; 142 Sym->Body = Body; 143 } 144 ObjectFiles.emplace_back(Obj); 145 } 146 } 147 148 // Add an undefined symbol. 149 template <class ELFT> 150 SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) { 151 auto *Sym = new (Alloc) 152 UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, false); 153 resolve(Sym); 154 return Sym; 155 } 156 157 // Add an undefined symbol. Unlike addUndefined, that symbol 158 // doesn't have to be resolved, thus "opt" (optional). 159 template <class ELFT> 160 SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) { 161 auto *Sym = new (Alloc) 162 UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_HIDDEN, /*Type*/ 0, true); 163 resolve(Sym); 164 return Sym; 165 } 166 167 template <class ELFT> 168 DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name, 169 uint8_t Visibility) { 170 // Pass nullptr because absolute symbols have no corresponding input sections. 171 auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, STB_GLOBAL, Visibility); 172 resolve(Sym); 173 return Sym; 174 } 175 176 template <class ELFT> 177 SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name, 178 OutputSectionBase<ELFT> &Sec, 179 uintX_t Val) { 180 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec); 181 resolve(Sym); 182 return Sym; 183 } 184 185 // Add Name as an "ignored" symbol. An ignored symbol is a regular 186 // linker-synthesized defined symbol, but is only defined if needed. 187 template <class ELFT> 188 DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name, 189 uint8_t Visibility) { 190 if (!find(Name)) 191 return nullptr; 192 return addAbsolute(Name, Visibility); 193 } 194 195 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. 196 // Used to implement --wrap. 197 template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) { 198 if (Symtab.count(Name) == 0) 199 return; 200 StringSaver Saver(Alloc); 201 Symbol *Sym = addUndefined(Name)->getSymbol(); 202 Symbol *Real = addUndefined(Saver.save("__real_" + Name))->getSymbol(); 203 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->getSymbol(); 204 Real->Body = Sym->Body; 205 Sym->Body = Wrap->Body; 206 } 207 208 // Returns a file from which symbol B was created. 209 // If B does not belong to any file, returns a nullptr. 210 template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) { 211 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) { 212 ArrayRef<SymbolBody *> Syms = F->getSymbols(); 213 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) 214 return F.get(); 215 } 216 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) { 217 ArrayRef<SymbolBody *> Syms = F->getSymbols(); 218 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) 219 return F.get(); 220 } 221 return nullptr; 222 } 223 224 // Construct a string in the form of "Sym in File1 and File2". 225 // Used to construct an error message. 226 template <class ELFT> 227 std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) { 228 InputFile *F1 = findFile(Old); 229 InputFile *F2 = findFile(New); 230 StringRef Sym = Old->getName(); 231 return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2); 232 } 233 234 // This function resolves conflicts if there's an existing symbol with 235 // the same name. Decisions are made based on symbol type. 236 template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) { 237 Symbol *Sym = insert(New); 238 if (Sym->Body == New) 239 return; 240 241 SymbolBody *Existing = Sym->Body; 242 243 if (auto *L = dyn_cast<Lazy>(Existing)) { 244 if (New->isUndefined()) { 245 addMemberFile(New, L); 246 return; 247 } 248 // Found a definition for something also in an archive. 249 // Ignore the archive definition. 250 if (L->isUsedInRegularObj()) 251 New->setUsedInRegularObj(); 252 Sym->Body = New; 253 return; 254 } 255 256 if (New->isTls() != Existing->isTls()) { 257 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New)); 258 return; 259 } 260 261 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, 262 // equivalent (conflicting), or more preferable, respectively. 263 int Comp = Existing->compare(New); 264 if (Comp == 0) { 265 std::string S = "duplicate symbol: " + conflictMsg(Existing, New); 266 if (Config->AllowMultipleDefinition) 267 warning(S); 268 else 269 error(S); 270 return; 271 } 272 if (Comp < 0) 273 Sym->Body = New; 274 } 275 276 // Find an existing symbol or create and insert a new one. 277 template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) { 278 StringRef Name = New->getName(); 279 unsigned NumSyms = SymVector.size(); 280 auto P = Symtab.insert(std::make_pair(Name, NumSyms)); 281 Symbol *Sym; 282 if (P.second) { 283 Sym = new (Alloc) Symbol{New}; 284 SymVector.push_back(Sym); 285 } else { 286 Sym = SymVector[P.first->second]; 287 } 288 New->setBackref(Sym); 289 return Sym; 290 } 291 292 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { 293 auto It = Symtab.find(Name); 294 if (It == Symtab.end()) 295 return nullptr; 296 return SymVector[It->second]->Body; 297 } 298 299 template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) { 300 Symbol *Sym = insert(L); 301 SymbolBody *Cur = Sym->Body; 302 if (Cur == L) 303 return; 304 if (Cur->isUndefined()) { 305 Sym->Body = L; 306 addMemberFile(Cur, L); 307 } 308 } 309 310 template <class ELFT> 311 void SymbolTable<ELFT>::addMemberFile(SymbolBody *Undef, Lazy *L) { 312 if (Undef->isUsedInRegularObj()) 313 L->setUsedInRegularObj(); 314 // Weak undefined symbols should not fetch members from archives. 315 // If we were to keep old symbol we would not know that an archive member was 316 // available if a strong undefined symbol shows up afterwards in the link. 317 // If a strong undefined symbol never shows up, this lazy symbol will 318 // get to the end of the link and must be treated as the weak undefined one. 319 // We set UsedInRegularObj in a similar way to what is done with shared 320 // symbols and copy information to reduce how many special cases are needed. 321 if (Undef->isWeak()) { 322 L->setUsedInRegularObj(); 323 L->Binding = Undef->Binding; 324 L->Type = Undef->Type; 325 326 // FIXME: Do we need to copy more? 327 return; 328 } 329 330 // Fetch a member file that has the definition for L. 331 // getMember returns nullptr if the member was already read from the library. 332 if (std::unique_ptr<InputFile> File = L->getFile()) 333 addFile(std::move(File)); 334 } 335 336 // This function takes care of the case in which shared libraries depend on 337 // the user program (not the other way, which is usual). Shared libraries 338 // may have undefined symbols, expecting that the user program provides 339 // the definitions for them. An example is BSD's __progname symbol. 340 // We need to put such symbols to the main program's .dynsym so that 341 // shared libraries can find them. 342 // Except this, we ignore undefined symbols in DSOs. 343 template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { 344 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles) 345 for (StringRef U : File->getUndefinedSymbols()) 346 if (SymbolBody *Sym = find(U)) 347 if (Sym->isDefined()) 348 Sym->MustBeInDynSym = true; 349 } 350 351 // This function process the dynamic list option by marking all the symbols 352 // to be exported in the dynamic table. 353 template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() { 354 for (StringRef S : Config->DynamicList) 355 if (SymbolBody *B = find(S)) 356 B->MustBeInDynSym = true; 357 } 358 359 template class elf::SymbolTable<ELF32LE>; 360 template class elf::SymbolTable<ELF32BE>; 361 template class elf::SymbolTable<ELF64LE>; 362 template class elf::SymbolTable<ELF64BE>; 363