Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 9 | // |
| 10 | // Symbol table is a bag of all known symbols. We put all symbols of |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 11 | // all input files to the symbol table. The symbol table is basically |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 12 | // a hash table with the logic to resolve symbol name conflicts using |
| 13 | // the symbol types. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | |
| 17 | #include "SymbolTable.h" |
Rafael Espindola | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 18 | #include "Config.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 19 | #include "Error.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 20 | #include "Symbols.h" |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/ReaderWriter.h" |
| 22 | #include "llvm/IR/LegacyPassManager.h" |
Rafael Espindola | 7f6d50b | 2016-03-05 14:51:51 +0000 | [diff] [blame] | 23 | #include "llvm/Linker/IRMover.h" |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 24 | #include "llvm/Support/StringSaver.h" |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 25 | #include "llvm/Support/TargetRegistry.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 29 | using namespace llvm::object; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 30 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 33 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 34 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 35 | // All input object files must be for the same architecture |
| 36 | // (e.g. it does not make sense to link x86 object files with |
| 37 | // MIPS object files.) This function checks for that error. |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 38 | template <class ELFT> static bool isCompatible(InputFile *FileP) { |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 39 | auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP); |
| 40 | if (!F) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 41 | return true; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 42 | if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 43 | return true; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 44 | StringRef A = F->getName(); |
| 45 | StringRef B = Config->Emulation; |
| 46 | if (B.empty()) |
| 47 | B = Config->FirstElf->getName(); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 48 | error(A + " is incompatible with " + B); |
| 49 | return false; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 52 | // Add symbols in File to the symbol table. |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 53 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 54 | void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) { |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 55 | InputFile *FileP = File.get(); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 56 | if (!isCompatible<ELFT>(FileP)) |
| 57 | return; |
Rafael Espindola | 525914d | 2015-10-11 03:36:49 +0000 | [diff] [blame] | 58 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 59 | // .a file |
| 60 | if (auto *F = dyn_cast<ArchiveFile>(FileP)) { |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 61 | ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release())); |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 62 | F->parse(); |
| 63 | for (Lazy &Sym : F->getLazySymbols()) |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 64 | addLazy(&Sym); |
| 65 | return; |
| 66 | } |
Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 67 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 68 | // .so file |
| 69 | if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) { |
| 70 | // DSOs are uniquified not by filename but by soname. |
| 71 | F->parseSoName(); |
Rui Ueyama | 131e0ff | 2016-01-08 22:17:42 +0000 | [diff] [blame] | 72 | if (!SoNames.insert(F->getSoName()).second) |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 73 | return; |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 74 | |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 75 | SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release())); |
Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 76 | F->parseRest(); |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 77 | for (SharedSymbol<ELFT> &B : F->getSharedSymbols()) |
| 78 | resolve(&B); |
| 79 | return; |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 80 | } |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 81 | |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 82 | // LLVM bitcode file. |
| 83 | if (auto *F = dyn_cast<BitcodeFile>(FileP)) { |
| 84 | BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release())); |
Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 85 | F->parse(ComdatGroups); |
Rafael Espindola | 297ce4e | 2016-02-26 21:31:34 +0000 | [diff] [blame] | 86 | for (SymbolBody *B : F->getSymbols()) |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 87 | resolve(B); |
| 88 | return; |
| 89 | } |
| 90 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 91 | // .o file |
| 92 | auto *F = cast<ObjectFile<ELFT>>(FileP); |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 93 | ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release())); |
Rui Ueyama | 52d3b67 | 2016-01-06 02:06:33 +0000 | [diff] [blame] | 94 | F->parse(ComdatGroups); |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 95 | for (SymbolBody *B : F->getSymbols()) |
| 96 | resolve(B); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Sean Silva | 4aaeac6 | 2016-03-09 22:30:05 +0000 | [diff] [blame] | 99 | // This is for use when debugging LTO. |
| 100 | static void saveLtoObjectFile(StringRef Buffer) { |
| 101 | std::error_code EC; |
| 102 | raw_fd_ostream OS(Config->OutputFile.str() + ".lto.o", EC, |
| 103 | sys::fs::OpenFlags::F_None); |
| 104 | check(EC); |
| 105 | OS << Buffer; |
| 106 | } |
| 107 | |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 108 | // Codegen the module M and returns the resulting InputFile. |
| 109 | template <class ELFT> |
| 110 | std::unique_ptr<InputFile> SymbolTable<ELFT>::codegen(Module &M) { |
| 111 | StringRef TripleStr = M.getTargetTriple(); |
| 112 | Triple TheTriple(TripleStr); |
| 113 | |
| 114 | // FIXME: Should we have a default triple? The gold plugin uses |
| 115 | // sys::getDefaultTargetTriple(), but that is probably wrong given that this |
| 116 | // might be a cross linker. |
| 117 | |
| 118 | std::string ErrMsg; |
| 119 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg); |
| 120 | if (!TheTarget) |
| 121 | fatal("Target not found: " + ErrMsg); |
| 122 | |
| 123 | TargetOptions Options; |
Rafael Espindola | 3ca9ee0 | 2016-03-02 17:21:06 +0000 | [diff] [blame] | 124 | Reloc::Model R = Config->Shared ? Reloc::PIC_ : Reloc::Static; |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 125 | std::unique_ptr<TargetMachine> TM( |
Rafael Espindola | 3ca9ee0 | 2016-03-02 17:21:06 +0000 | [diff] [blame] | 126 | TheTarget->createTargetMachine(TripleStr, "", "", Options, R)); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 127 | |
| 128 | raw_svector_ostream OS(OwningLTOData); |
| 129 | legacy::PassManager CodeGenPasses; |
| 130 | if (TM->addPassesToEmitFile(CodeGenPasses, OS, |
| 131 | TargetMachine::CGFT_ObjectFile)) |
| 132 | fatal("Failed to setup codegen"); |
| 133 | CodeGenPasses.run(M); |
| 134 | LtoBuffer = MemoryBuffer::getMemBuffer(OwningLTOData, "", false); |
Sean Silva | 4aaeac6 | 2016-03-09 22:30:05 +0000 | [diff] [blame] | 135 | if (Config->SaveTemps) |
| 136 | saveLtoObjectFile(LtoBuffer->getBuffer()); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 137 | return createObjectFile(*LtoBuffer); |
| 138 | } |
| 139 | |
Rafael Espindola | 7f6d50b | 2016-03-05 14:51:51 +0000 | [diff] [blame] | 140 | static void addBitcodeFile(IRMover &Mover, BitcodeFile &F, |
| 141 | LLVMContext &Context) { |
| 142 | std::unique_ptr<MemoryBuffer> Buffer = |
| 143 | MemoryBuffer::getMemBuffer(F.MB, false); |
| 144 | std::unique_ptr<Module> M = |
| 145 | check(getLazyBitcodeModule(std::move(Buffer), Context, |
Sean Silva | 50d27ff | 2016-03-09 18:38:40 +0000 | [diff] [blame] | 146 | /*ShouldLazyLoadMetadata*/ false)); |
Rafael Espindola | 7f6d50b | 2016-03-05 14:51:51 +0000 | [diff] [blame] | 147 | std::vector<GlobalValue *> Keep; |
| 148 | for (SymbolBody *B : F.getSymbols()) { |
| 149 | if (B->repl() != B) |
| 150 | continue; |
| 151 | auto *DB = dyn_cast<DefinedBitcode>(B); |
| 152 | if (!DB) |
| 153 | continue; |
| 154 | GlobalValue *GV = M->getNamedValue(B->getName()); |
| 155 | assert(GV); |
| 156 | Keep.push_back(GV); |
| 157 | } |
| 158 | Mover.move(std::move(M), Keep, [](GlobalValue &, IRMover::ValueAdder) {}); |
| 159 | } |
| 160 | |
Sean Silva | c43ec67 | 2016-03-09 20:06:24 +0000 | [diff] [blame] | 161 | // This is for use when debugging LTO. |
| 162 | static void saveBCFile(Module &M) { |
Sean Silva | 35ef3d9 | 2016-03-09 20:01:08 +0000 | [diff] [blame] | 163 | std::error_code EC; |
Sean Silva | c43ec67 | 2016-03-09 20:06:24 +0000 | [diff] [blame] | 164 | raw_fd_ostream OS(Config->OutputFile.str() + ".lto.bc", EC, |
| 165 | sys::fs::OpenFlags::F_None); |
Sean Silva | 35ef3d9 | 2016-03-09 20:01:08 +0000 | [diff] [blame] | 166 | check(EC); |
| 167 | WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true); |
| 168 | } |
| 169 | |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 170 | // Merge all the bitcode files we have seen, codegen the result and return |
| 171 | // the resulting ObjectFile. |
| 172 | template <class ELFT> |
| 173 | ObjectFile<ELFT> *SymbolTable<ELFT>::createCombinedLtoObject() { |
| 174 | LLVMContext Context; |
| 175 | Module Combined("ld-temp.o", Context); |
Rafael Espindola | 7f6d50b | 2016-03-05 14:51:51 +0000 | [diff] [blame] | 176 | IRMover Mover(Combined); |
| 177 | for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) |
| 178 | addBitcodeFile(Mover, *F, Context); |
Sean Silva | 35ef3d9 | 2016-03-09 20:01:08 +0000 | [diff] [blame] | 179 | if (Config->SaveTemps) |
Sean Silva | c43ec67 | 2016-03-09 20:06:24 +0000 | [diff] [blame] | 180 | saveBCFile(Combined); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 181 | std::unique_ptr<InputFile> F = codegen(Combined); |
| 182 | ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(F.release())); |
| 183 | return &*ObjectFiles.back(); |
| 184 | } |
| 185 | |
| 186 | template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() { |
| 187 | if (BitcodeFiles.empty()) |
| 188 | return; |
| 189 | ObjectFile<ELFT> *Obj = createCombinedLtoObject(); |
Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 190 | llvm::DenseSet<StringRef> DummyGroups; |
| 191 | Obj->parse(DummyGroups); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 192 | for (SymbolBody *Body : Obj->getSymbols()) { |
| 193 | Symbol *Sym = insert(Body); |
Rafael Espindola | cdf3a2a | 2016-03-02 18:21:46 +0000 | [diff] [blame] | 194 | if (!Sym->Body->isUndefined() && Body->isUndefined()) |
| 195 | continue; |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 196 | Sym->Body = Body; |
| 197 | } |
| 198 | } |
| 199 | |
Rui Ueyama | 01a65b1 | 2015-12-24 10:37:32 +0000 | [diff] [blame] | 200 | // Add an undefined symbol. |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 201 | template <class ELFT> |
| 202 | SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 203 | auto *Sym = new (Alloc) Undefined(Name, false, STV_DEFAULT, false); |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 204 | resolve(Sym); |
| 205 | return Sym; |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Rui Ueyama | 01a65b1 | 2015-12-24 10:37:32 +0000 | [diff] [blame] | 208 | // Add an undefined symbol. Unlike addUndefined, that symbol |
| 209 | // doesn't have to be resolved, thus "opt" (optional). |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 210 | template <class ELFT> |
| 211 | SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 212 | auto *Sym = new (Alloc) Undefined(Name, false, STV_HIDDEN, true); |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 213 | resolve(Sym); |
| 214 | return Sym; |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 217 | template <class ELFT> |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 218 | SymbolBody *SymbolTable<ELFT>::addAbsolute(StringRef Name, Elf_Sym &ESym) { |
| 219 | // Pass nullptr because absolute symbols have no corresponding input sections. |
| 220 | auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, ESym, nullptr); |
| 221 | resolve(Sym); |
| 222 | return Sym; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | template <class ELFT> |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 226 | SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name, |
George Rimar | aa4dc20 | 2016-03-01 16:23:13 +0000 | [diff] [blame] | 227 | OutputSectionBase<ELFT> &Sec, |
| 228 | uintX_t Val, uint8_t Visibility) { |
| 229 | auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec, Visibility); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 230 | resolve(Sym); |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 231 | return Sym; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 234 | // Add Name as an "ignored" symbol. An ignored symbol is a regular |
| 235 | // linker-synthesized defined symbol, but it is not recorded to the output |
| 236 | // file's symbol table. Such symbols are useful for some linker-defined symbols. |
Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 237 | template <class ELFT> |
Rui Ueyama | dd7d998 | 2015-12-16 22:31:14 +0000 | [diff] [blame] | 238 | SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) { |
Rafael Espindola | 65e80b9 | 2016-01-19 21:19:52 +0000 | [diff] [blame] | 239 | return addAbsolute(Name, ElfSym<ELFT>::Ignored); |
Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 242 | // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. |
| 243 | // Used to implement --wrap. |
| 244 | template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) { |
| 245 | if (Symtab.count(Name) == 0) |
| 246 | return; |
| 247 | StringSaver Saver(Alloc); |
| 248 | Symbol *Sym = addUndefined(Name)->getSymbol(); |
| 249 | Symbol *Real = addUndefined(Saver.save("__real_" + Name))->getSymbol(); |
| 250 | Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->getSymbol(); |
| 251 | Real->Body = Sym->Body; |
| 252 | Sym->Body = Wrap->Body; |
| 253 | } |
| 254 | |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 255 | // Returns a file from which symbol B was created. |
Rui Ueyama | 2a65a49 | 2016-01-05 20:01:29 +0000 | [diff] [blame] | 256 | // If B does not belong to any file, returns a nullptr. |
Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 257 | template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) { |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 258 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 259 | ArrayRef<SymbolBody *> Syms = F->getSymbols(); |
| 260 | if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 261 | return F.get(); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 262 | } |
Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 263 | for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) { |
| 264 | ArrayRef<SymbolBody *> Syms = F->getSymbols(); |
| 265 | if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) |
| 266 | return F.get(); |
| 267 | } |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 268 | return nullptr; |
| 269 | } |
| 270 | |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 271 | // Returns "(internal)", "foo.a(bar.o)" or "baz.o". |
Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 272 | static std::string getFilename(InputFile *F) { |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 273 | if (!F) |
| 274 | return "(internal)"; |
| 275 | if (!F->ArchiveName.empty()) |
| 276 | return (F->ArchiveName + "(" + F->getName() + ")").str(); |
| 277 | return F->getName(); |
| 278 | } |
| 279 | |
Rui Ueyama | b4de595 | 2016-01-08 22:01:33 +0000 | [diff] [blame] | 280 | // Construct a string in the form of "Sym in File1 and File2". |
| 281 | // Used to construct an error message. |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 282 | template <class ELFT> |
| 283 | std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) { |
Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 284 | InputFile *F1 = findFile(Old); |
| 285 | InputFile *F2 = findFile(New); |
Rui Ueyama | f090401 | 2015-12-16 22:26:45 +0000 | [diff] [blame] | 286 | StringRef Sym = Old->getName(); |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 287 | return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 290 | // This function resolves conflicts if there's an existing symbol with |
| 291 | // the same name. Decisions are made based on symbol type. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 292 | template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 293 | Symbol *Sym = insert(New); |
| 294 | if (Sym->Body == New) |
| 295 | return; |
| 296 | |
| 297 | SymbolBody *Existing = Sym->Body; |
| 298 | |
| 299 | if (Lazy *L = dyn_cast<Lazy>(Existing)) { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 300 | if (auto *Undef = dyn_cast<Undefined>(New)) { |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 301 | addMemberFile(Undef, L); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 302 | return; |
| 303 | } |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 304 | // Found a definition for something also in an archive. |
| 305 | // Ignore the archive definition. |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 306 | Sym->Body = New; |
| 307 | return; |
| 308 | } |
| 309 | |
George Rimar | 2f0fab5 | 2016-03-06 06:26:18 +0000 | [diff] [blame] | 310 | if (New->IsTls != Existing->IsTls) { |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 311 | error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New)); |
| 312 | return; |
| 313 | } |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 314 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 315 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 316 | // equivalent (conflicting), or more preferable, respectively. |
Rui Ueyama | 2e0a9ff | 2016-01-06 00:09:39 +0000 | [diff] [blame] | 317 | int Comp = Existing->compare<ELFT>(New); |
| 318 | if (Comp == 0) { |
George Rimar | e094388 | 2016-03-10 16:58:34 +0000 | [diff] [blame^] | 319 | std::string S = "Duplicate symbol: " + conflictMsg(Existing, New); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 320 | if (Config->AllowMultipleDefinition) |
| 321 | warning(S); |
| 322 | else |
| 323 | error(S); |
Rui Ueyama | f090401 | 2015-12-16 22:26:45 +0000 | [diff] [blame] | 324 | return; |
| 325 | } |
Rui Ueyama | 2e0a9ff | 2016-01-06 00:09:39 +0000 | [diff] [blame] | 326 | if (Comp < 0) |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 327 | Sym->Body = New; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Rui Ueyama | b4de595 | 2016-01-08 22:01:33 +0000 | [diff] [blame] | 330 | // Find an existing symbol or create and insert a new one. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 331 | template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 332 | StringRef Name = New->getName(); |
| 333 | Symbol *&Sym = Symtab[Name]; |
Rui Ueyama | 38dcc9e | 2015-12-16 23:25:31 +0000 | [diff] [blame] | 334 | if (!Sym) |
Rui Ueyama | 3554f59 | 2015-12-17 00:01:25 +0000 | [diff] [blame] | 335 | Sym = new (Alloc) Symbol{New}; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 336 | New->setBackref(Sym); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 337 | return Sym; |
| 338 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 339 | |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 340 | template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { |
| 341 | auto It = Symtab.find(Name); |
| 342 | if (It == Symtab.end()) |
| 343 | return nullptr; |
| 344 | return It->second->Body; |
| 345 | } |
| 346 | |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 347 | template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) { |
| 348 | Symbol *Sym = insert(L); |
| 349 | if (Sym->Body == L) |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 350 | return; |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 351 | if (auto *Undef = dyn_cast<Undefined>(Sym->Body)) { |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 352 | Sym->Body = L; |
| 353 | addMemberFile(Undef, L); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 354 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 357 | template <class ELFT> |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 358 | void SymbolTable<ELFT>::addMemberFile(Undefined *Undef, Lazy *L) { |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 359 | // Weak undefined symbols should not fetch members from archives. |
| 360 | // If we were to keep old symbol we would not know that an archive member was |
| 361 | // available if a strong undefined symbol shows up afterwards in the link. |
| 362 | // If a strong undefined symbol never shows up, this lazy symbol will |
| 363 | // get to the end of the link and must be treated as the weak undefined one. |
| 364 | // We set UsedInRegularObj in a similar way to what is done with shared |
Rafael Espindola | 8176d57 | 2016-02-22 23:19:29 +0000 | [diff] [blame] | 365 | // symbols and copy information to reduce how many special cases are needed. |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 366 | if (Undef->isWeak()) { |
| 367 | L->setUsedInRegularObj(); |
| 368 | L->setWeak(); |
Rafael Espindola | 8176d57 | 2016-02-22 23:19:29 +0000 | [diff] [blame] | 369 | |
| 370 | // FIXME: Do we need to copy more? |
George Rimar | 2f0fab5 | 2016-03-06 06:26:18 +0000 | [diff] [blame] | 371 | L->IsTls |= Undef->IsTls; |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
| 375 | // Fetch a member file that has the definition for L. |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 376 | // getMember returns nullptr if the member was already read from the library. |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 377 | if (std::unique_ptr<InputFile> File = L->getMember()) |
Rui Ueyama | 690db67 | 2015-10-14 22:32:10 +0000 | [diff] [blame] | 378 | addFile(std::move(File)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 379 | } |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 380 | |
Rui Ueyama | 93bfee5 | 2015-10-13 18:10:33 +0000 | [diff] [blame] | 381 | // This function takes care of the case in which shared libraries depend on |
| 382 | // the user program (not the other way, which is usual). Shared libraries |
| 383 | // may have undefined symbols, expecting that the user program provides |
| 384 | // the definitions for them. An example is BSD's __progname symbol. |
| 385 | // We need to put such symbols to the main program's .dynsym so that |
| 386 | // shared libraries can find them. |
| 387 | // Except this, we ignore undefined symbols in DSOs. |
| 388 | template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 389 | for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles) |
| 390 | for (StringRef U : File->getUndefinedSymbols()) |
| 391 | if (SymbolBody *Sym = find(U)) |
| 392 | if (Sym->isDefined()) |
Rafael Espindola | abebed9 | 2016-02-05 15:27:15 +0000 | [diff] [blame] | 393 | Sym->MustBeInDynSym = true; |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 396 | template class elf::SymbolTable<ELF32LE>; |
| 397 | template class elf::SymbolTable<ELF32BE>; |
| 398 | template class elf::SymbolTable<ELF64LE>; |
| 399 | template class elf::SymbolTable<ELF64BE>; |