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" |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 22 | #include "llvm/Support/StringSaver.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 25 | using namespace llvm::object; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 26 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 29 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 30 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 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. |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 34 | template <class ELFT> static bool isCompatible(InputFile *FileP) { |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 35 | auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP); |
| 36 | if (!F) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 37 | return true; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 38 | if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 39 | return true; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 40 | StringRef A = F->getName(); |
| 41 | StringRef B = Config->Emulation; |
| 42 | if (B.empty()) |
| 43 | B = Config->FirstElf->getName(); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 44 | error(A + " is incompatible with " + B); |
| 45 | return false; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 46 | } |
| 47 | |
George Rimar | 2a78fce | 2016-04-13 18:07:57 +0000 | [diff] [blame] | 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 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 57 | // Add symbols in File to the symbol table. |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 58 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 59 | void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) { |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 60 | InputFile *FileP = File.get(); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 61 | if (!isCompatible<ELFT>(FileP)) |
| 62 | return; |
Rafael Espindola | 525914d | 2015-10-11 03:36:49 +0000 | [diff] [blame] | 63 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 64 | // .a file |
| 65 | if (auto *F = dyn_cast<ArchiveFile>(FileP)) { |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 66 | ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release())); |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 67 | F->parse(); |
| 68 | for (Lazy &Sym : F->getLazySymbols()) |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 69 | addLazy(&Sym); |
| 70 | return; |
| 71 | } |
Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 72 | |
George Rimar | 2a78fce | 2016-04-13 18:07:57 +0000 | [diff] [blame] | 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 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 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(); |
Rui Ueyama | 131e0ff | 2016-01-08 22:17:42 +0000 | [diff] [blame] | 89 | if (!SoNames.insert(F->getSoName()).second) |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 90 | return; |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 91 | |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 92 | SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release())); |
Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 93 | F->parseRest(); |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 94 | for (SharedSymbol<ELFT> &B : F->getSharedSymbols()) |
| 95 | resolve(&B); |
| 96 | return; |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 97 | } |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 98 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 99 | // LLVM bitcode file |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 100 | if (auto *F = dyn_cast<BitcodeFile>(FileP)) { |
| 101 | BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release())); |
Rafael Espindola | 4de44b7 | 2016-03-02 15:43:50 +0000 | [diff] [blame] | 102 | F->parse(ComdatGroups); |
Rafael Espindola | 297ce4e | 2016-02-26 21:31:34 +0000 | [diff] [blame] | 103 | for (SymbolBody *B : F->getSymbols()) |
Rui Ueyama | f714955 | 2016-03-11 18:46:51 +0000 | [diff] [blame] | 104 | if (B) |
| 105 | resolve(B); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 109 | // Regular object file |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 110 | auto *F = cast<ObjectFile<ELFT>>(FileP); |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 111 | ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release())); |
Rui Ueyama | 52d3b67 | 2016-01-06 02:06:33 +0000 | [diff] [blame] | 112 | F->parse(ComdatGroups); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 113 | for (SymbolBody *B : F->getNonLocalSymbols()) |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 114 | resolve(B); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Rui Ueyama | 4255475 | 2016-04-23 00:26:32 +0000 | [diff] [blame] | 117 | // This function is where all the optimizations of link-time |
| 118 | // optimization happens. When LTO is in use, some input files are |
| 119 | // not in native object file format but in the LLVM bitcode format. |
| 120 | // This function compiles bitcode files into a few big native files |
| 121 | // using LLVM functions and replaces bitcode symbols with the results. |
| 122 | // Because all bitcode files that consist of a program are passed |
| 123 | // to the compiler at once, it can do whole-program optimization. |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 124 | template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() { |
| 125 | if (BitcodeFiles.empty()) |
| 126 | return; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 127 | |
| 128 | // Compile bitcode files. |
| 129 | Lto.reset(new BitcodeCompiler); |
| 130 | for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) |
| 131 | Lto->add(*F); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 132 | std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile(); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 133 | |
| 134 | // Replace bitcode symbols. |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 135 | for (auto &IF : IFs) { |
| 136 | ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release()); |
| 137 | |
| 138 | llvm::DenseSet<StringRef> DummyGroups; |
| 139 | Obj->parse(DummyGroups); |
| 140 | for (SymbolBody *Body : Obj->getNonLocalSymbols()) { |
| 141 | Symbol *Sym = insert(Body); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 142 | if (!Sym->Body->isUndefined() && Body->isUndefined()) |
| 143 | continue; |
| 144 | Sym->Body = Body; |
| 145 | } |
| 146 | ObjectFiles.emplace_back(Obj); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Rui Ueyama | 01a65b1 | 2015-12-24 10:37:32 +0000 | [diff] [blame] | 150 | // Add an undefined symbol. |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 151 | template <class ELFT> |
| 152 | SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) { |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 153 | auto *Sym = new (Alloc) |
| 154 | UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, false); |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 155 | resolve(Sym); |
| 156 | return Sym; |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Rui Ueyama | 01a65b1 | 2015-12-24 10:37:32 +0000 | [diff] [blame] | 159 | // Add an undefined symbol. Unlike addUndefined, that symbol |
| 160 | // doesn't have to be resolved, thus "opt" (optional). |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 161 | template <class ELFT> |
| 162 | SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) { |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 163 | auto *Sym = new (Alloc) |
| 164 | UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_HIDDEN, /*Type*/ 0, true); |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 165 | resolve(Sym); |
| 166 | return Sym; |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 169 | template <class ELFT> |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 170 | DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name, |
| 171 | uint8_t Visibility) { |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 172 | // Pass nullptr because absolute symbols have no corresponding input sections. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 173 | auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, STB_GLOBAL, Visibility); |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 174 | resolve(Sym); |
| 175 | return Sym; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | template <class ELFT> |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 179 | SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name, |
George Rimar | aa4dc20 | 2016-03-01 16:23:13 +0000 | [diff] [blame] | 180 | OutputSectionBase<ELFT> &Sec, |
Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 181 | uintX_t Val) { |
| 182 | auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 183 | resolve(Sym); |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 184 | return Sym; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 187 | // Add Name as an "ignored" symbol. An ignored symbol is a regular |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 188 | // linker-synthesized defined symbol, but is only defined if needed. |
Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 189 | template <class ELFT> |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 190 | DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name, |
| 191 | uint8_t Visibility) { |
| 192 | if (!find(Name)) |
| 193 | return nullptr; |
| 194 | return addAbsolute(Name, Visibility); |
Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 197 | // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. |
| 198 | // Used to implement --wrap. |
| 199 | template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) { |
| 200 | if (Symtab.count(Name) == 0) |
| 201 | return; |
| 202 | StringSaver Saver(Alloc); |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 203 | Symbol *Sym = addUndefined(Name)->Backref; |
| 204 | Symbol *Real = addUndefined(Saver.save("__real_" + Name))->Backref; |
| 205 | Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->Backref; |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 206 | Real->Body = Sym->Body; |
| 207 | Sym->Body = Wrap->Body; |
| 208 | } |
| 209 | |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 210 | // Returns a file from which symbol B was created. |
Rui Ueyama | 2a65a49 | 2016-01-05 20:01:29 +0000 | [diff] [blame] | 211 | // If B does not belong to any file, returns a nullptr. |
Rui Ueyama | 209f6cb | 2016-04-23 01:10:13 +0000 | [diff] [blame] | 212 | // This function is slow, but it's okay as it is used only for error messages. |
Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 213 | template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) { |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 214 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 215 | ArrayRef<SymbolBody *> Syms = F->getSymbols(); |
| 216 | if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 217 | return F.get(); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 218 | } |
Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 219 | for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) { |
| 220 | ArrayRef<SymbolBody *> Syms = F->getSymbols(); |
| 221 | if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) |
| 222 | return F.get(); |
| 223 | } |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 224 | return nullptr; |
| 225 | } |
| 226 | |
Rui Ueyama | b4de595 | 2016-01-08 22:01:33 +0000 | [diff] [blame] | 227 | // Construct a string in the form of "Sym in File1 and File2". |
| 228 | // Used to construct an error message. |
Rui Ueyama | 533336a | 2015-12-16 22:26:48 +0000 | [diff] [blame] | 229 | template <class ELFT> |
| 230 | std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) { |
Rafael Espindola | 18f0950 | 2016-02-26 21:49:38 +0000 | [diff] [blame] | 231 | InputFile *F1 = findFile(Old); |
| 232 | InputFile *F2 = findFile(New); |
Rui Ueyama | f090401 | 2015-12-16 22:26:45 +0000 | [diff] [blame] | 233 | StringRef Sym = Old->getName(); |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 234 | return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 237 | // This function resolves conflicts if there's an existing symbol with |
| 238 | // the same name. Decisions are made based on symbol type. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 239 | template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 240 | Symbol *Sym = insert(New); |
| 241 | if (Sym->Body == New) |
| 242 | return; |
| 243 | |
| 244 | SymbolBody *Existing = Sym->Body; |
| 245 | |
Davide Italiano | c496500 | 2016-04-02 23:47:54 +0000 | [diff] [blame] | 246 | if (auto *L = dyn_cast<Lazy>(Existing)) { |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 247 | Sym->Binding = New->Binding; |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 248 | if (New->isUndefined()) { |
| 249 | addMemberFile(New, L); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 250 | return; |
| 251 | } |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 252 | // Found a definition for something also in an archive. |
| 253 | // Ignore the archive definition. |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 254 | Sym->Body = New; |
| 255 | return; |
| 256 | } |
| 257 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 258 | if (New->isTls() != Existing->isTls()) { |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 259 | error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New)); |
| 260 | return; |
| 261 | } |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 262 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 263 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 264 | // equivalent (conflicting), or more preferable, respectively. |
Peter Collingbourne | d0856a6 | 2016-04-05 00:47:58 +0000 | [diff] [blame] | 265 | int Comp = Existing->compare(New); |
Rui Ueyama | 2e0a9ff | 2016-01-06 00:09:39 +0000 | [diff] [blame] | 266 | if (Comp == 0) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 267 | std::string S = "duplicate symbol: " + conflictMsg(Existing, New); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 268 | if (Config->AllowMultipleDefinition) |
| 269 | warning(S); |
| 270 | else |
| 271 | error(S); |
Rui Ueyama | f090401 | 2015-12-16 22:26:45 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 274 | if (Comp < 0) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 275 | Sym->Body = New; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 276 | if (!New->isShared()) |
| 277 | Sym->Binding = New->Binding; |
| 278 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 281 | static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { |
| 282 | if (VA == STV_DEFAULT) |
| 283 | return VB; |
| 284 | if (VB == STV_DEFAULT) |
| 285 | return VA; |
| 286 | return std::min(VA, VB); |
| 287 | } |
| 288 | |
| 289 | static bool shouldExport(SymbolBody *B) { |
| 290 | if (Config->Shared || Config->ExportDynamic) { |
| 291 | // Export most symbols except for those that do not need to be exported. |
| 292 | return !B->CanOmitFromDynSym; |
| 293 | } |
| 294 | // Make sure we preempt DSO symbols with default visibility. |
| 295 | return B->isShared() && B->getVisibility() == STV_DEFAULT; |
| 296 | } |
| 297 | |
Rui Ueyama | b4de595 | 2016-01-08 22:01:33 +0000 | [diff] [blame] | 298 | // Find an existing symbol or create and insert a new one. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 299 | template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 300 | StringRef Name = New->getName(); |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 301 | unsigned NumSyms = SymVector.size(); |
| 302 | auto P = Symtab.insert(std::make_pair(Name, NumSyms)); |
| 303 | Symbol *Sym; |
| 304 | if (P.second) { |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 305 | Sym = new (Alloc) Symbol; |
| 306 | Sym->Body = New; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 307 | Sym->Binding = New->isShared() ? STB_GLOBAL : New->Binding; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 308 | Sym->Visibility = STV_DEFAULT; |
| 309 | Sym->IsUsedInRegularObj = false; |
| 310 | Sym->ExportDynamic = false; |
| 311 | Sym->VersionScriptGlobal = !Config->VersionScript; |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 312 | SymVector.push_back(Sym); |
| 313 | } else { |
| 314 | Sym = SymVector[P.first->second]; |
| 315 | } |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 316 | New->Backref = Sym; |
| 317 | |
| 318 | // Merge in the new symbol's visibility. DSO symbols do not affect visibility |
| 319 | // in the output. |
| 320 | if (!New->isShared()) |
| 321 | Sym->Visibility = getMinVisibility(Sym->Visibility, New->getVisibility()); |
| 322 | Sym->ExportDynamic = Sym->ExportDynamic || shouldExport(New); |
| 323 | SymbolBody::Kind K = New->kind(); |
| 324 | if (K == SymbolBody::DefinedRegularKind || |
| 325 | K == SymbolBody::DefinedCommonKind || |
| 326 | K == SymbolBody::DefinedSyntheticKind || |
| 327 | K == SymbolBody::UndefinedElfKind) |
| 328 | Sym->IsUsedInRegularObj = true; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 329 | return Sym; |
| 330 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 331 | |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 332 | template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { |
| 333 | auto It = Symtab.find(Name); |
| 334 | if (It == Symtab.end()) |
| 335 | return nullptr; |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 336 | return SymVector[It->second]->Body; |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 339 | template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) { |
| 340 | Symbol *Sym = insert(L); |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 341 | SymbolBody *Cur = Sym->Body; |
| 342 | if (Cur == L) |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 343 | return; |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 344 | if (Cur->isUndefined()) { |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 345 | Sym->Body = L; |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 346 | addMemberFile(Cur, L); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 347 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 350 | template <class ELFT> |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 351 | void SymbolTable<ELFT>::addMemberFile(SymbolBody *Undef, Lazy *L) { |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 352 | // Weak undefined symbols should not fetch members from archives. |
| 353 | // If we were to keep old symbol we would not know that an archive member was |
| 354 | // available if a strong undefined symbol shows up afterwards in the link. |
| 355 | // If a strong undefined symbol never shows up, this lazy symbol will |
| 356 | // get to the end of the link and must be treated as the weak undefined one. |
Peter Collingbourne | e8d4662 | 2016-04-22 19:56:45 +0000 | [diff] [blame] | 357 | // We already marked this symbol as used when we added it to the symbol table, |
| 358 | // but we also need to preserve its binding and type. |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 359 | if (Undef->isWeak()) { |
Peter Collingbourne | e8d4662 | 2016-04-22 19:56:45 +0000 | [diff] [blame] | 360 | // FIXME: Consider moving these members to Symbol. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 361 | L->Type = Undef->Type; |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | |
| 365 | // Fetch a member file that has the definition for L. |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 366 | // getMember returns nullptr if the member was already read from the library. |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 367 | if (std::unique_ptr<InputFile> File = L->getFile()) |
Rui Ueyama | 690db67 | 2015-10-14 22:32:10 +0000 | [diff] [blame] | 368 | addFile(std::move(File)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 369 | } |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 370 | |
Rui Ueyama | 93bfee5 | 2015-10-13 18:10:33 +0000 | [diff] [blame] | 371 | // This function takes care of the case in which shared libraries depend on |
| 372 | // the user program (not the other way, which is usual). Shared libraries |
| 373 | // may have undefined symbols, expecting that the user program provides |
| 374 | // the definitions for them. An example is BSD's __progname symbol. |
| 375 | // We need to put such symbols to the main program's .dynsym so that |
| 376 | // shared libraries can find them. |
| 377 | // Except this, we ignore undefined symbols in DSOs. |
| 378 | template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 379 | for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles) |
| 380 | for (StringRef U : File->getUndefinedSymbols()) |
| 381 | if (SymbolBody *Sym = find(U)) |
| 382 | if (Sym->isDefined()) |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 383 | Sym->Backref->ExportDynamic = true; |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 386 | // This function process the dynamic list option by marking all the symbols |
| 387 | // to be exported in the dynamic table. |
| 388 | template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() { |
| 389 | for (StringRef S : Config->DynamicList) |
| 390 | if (SymbolBody *B = find(S)) |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 391 | B->Backref->ExportDynamic = true; |
Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 394 | // This function processes the --version-script option by marking all global |
| 395 | // symbols with the VersionScriptGlobal flag, which acts as a filter on the |
| 396 | // dynamic symbol table. |
| 397 | template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() { |
| 398 | for (StringRef S : Config->VersionScriptGlobals) |
| 399 | if (SymbolBody *B = find(S)) |
| 400 | B->Backref->VersionScriptGlobal = true; |
| 401 | } |
| 402 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 403 | template class elf::SymbolTable<ELF32LE>; |
| 404 | template class elf::SymbolTable<ELF32BE>; |
| 405 | template class elf::SymbolTable<ELF64LE>; |
| 406 | template class elf::SymbolTable<ELF64BE>; |