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" |
Davide Italiano | 8e1131d | 2016-06-29 02:46:51 +0000 | [diff] [blame] | 19 | #include "LinkerScript.h" |
Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 20 | #include "Memory.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | #include "Symbols.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 22 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | cd236a9 | 2016-11-17 19:57:43 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 26 | using namespace llvm::object; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 27 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 30 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 32 | SymbolTable *elf::Symtab; |
| 33 | |
Rui Ueyama | 227cb6b | 2017-10-15 21:43:09 +0000 | [diff] [blame] | 34 | static InputFile *getFirstElf() { |
| 35 | if (!ObjectFiles.empty()) |
| 36 | return ObjectFiles[0]; |
| 37 | if (!SharedFiles.empty()) |
| 38 | return SharedFiles[0]; |
| 39 | return nullptr; |
| 40 | } |
| 41 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 42 | // All input object files must be for the same architecture |
| 43 | // (e.g. it does not make sense to link x86 object files with |
| 44 | // MIPS object files.) This function checks for that error. |
George Rimar | dbbf60e | 2016-06-29 09:46:00 +0000 | [diff] [blame] | 45 | template <class ELFT> static bool isCompatible(InputFile *F) { |
| 46 | if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F)) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 47 | return true; |
Rui Ueyama | 26081ca | 2016-11-24 20:59:44 +0000 | [diff] [blame] | 48 | |
Simon Atanasyan | 9e0297b | 2016-11-05 22:58:01 +0000 | [diff] [blame] | 49 | if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) { |
| 50 | if (Config->EMachine != EM_MIPS) |
| 51 | return true; |
| 52 | if (isMipsN32Abi(F) == Config->MipsN32Abi) |
| 53 | return true; |
| 54 | } |
Rui Ueyama | 26081ca | 2016-11-24 20:59:44 +0000 | [diff] [blame] | 55 | |
| 56 | if (!Config->Emulation.empty()) |
| 57 | error(toString(F) + " is incompatible with " + Config->Emulation); |
| 58 | else |
Rui Ueyama | 227cb6b | 2017-10-15 21:43:09 +0000 | [diff] [blame] | 59 | error(toString(F) + " is incompatible with " + toString(getFirstElf())); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 60 | return false; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 63 | // Add symbols in File to the symbol table. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 64 | template <class ELFT> void SymbolTable::addFile(InputFile *File) { |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 65 | if (!isCompatible<ELFT>(File)) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 66 | return; |
Rafael Espindola | 525914d | 2015-10-11 03:36:49 +0000 | [diff] [blame] | 67 | |
Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 +0000 | [diff] [blame] | 68 | // Binary file |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 69 | if (auto *F = dyn_cast<BinaryFile>(File)) { |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 70 | BinaryFiles.push_back(F); |
Rafael Espindola | 093abab | 2016-10-27 17:45:40 +0000 | [diff] [blame] | 71 | F->parse<ELFT>(); |
Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 +0000 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 75 | // .a file |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 76 | if (auto *F = dyn_cast<ArchiveFile>(File)) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 77 | F->parse<ELFT>(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 78 | return; |
| 79 | } |
Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 80 | |
George Rimar | 2a78fce | 2016-04-13 18:07:57 +0000 | [diff] [blame] | 81 | // Lazy object file |
Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 +0000 | [diff] [blame] | 82 | if (auto *F = dyn_cast<LazyObjFile>(File)) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 83 | F->parse<ELFT>(); |
George Rimar | 2a78fce | 2016-04-13 18:07:57 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (Config->Trace) |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 88 | message(toString(File)); |
George Rimar | 2a78fce | 2016-04-13 18:07:57 +0000 | [diff] [blame] | 89 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 90 | // .so file |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 91 | if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) { |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 92 | // DSOs are uniquified not by filename but by soname. |
| 93 | F->parseSoName(); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 94 | if (errorCount() || !SoNames.insert(F->SoName).second) |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 95 | return; |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 96 | SharedFiles.push_back(F); |
Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 97 | F->parseRest(); |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 98 | return; |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 99 | } |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 100 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 101 | // LLVM bitcode file |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 102 | if (auto *F = dyn_cast<BitcodeFile>(File)) { |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 103 | BitcodeFiles.push_back(F); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 104 | F->parse<ELFT>(ComdatGroups); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 108 | // Regular object file |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 109 | ObjectFiles.push_back(File); |
| 110 | cast<ObjFile<ELFT>>(File)->parse(ComdatGroups); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Rui Ueyama | 4255475 | 2016-04-23 00:26:32 +0000 | [diff] [blame] | 113 | // This function is where all the optimizations of link-time |
| 114 | // optimization happens. When LTO is in use, some input files are |
| 115 | // not in native object file format but in the LLVM bitcode format. |
| 116 | // This function compiles bitcode files into a few big native files |
| 117 | // using LLVM functions and replaces bitcode symbols with the results. |
| 118 | // Because all bitcode files that consist of a program are passed |
| 119 | // to the compiler at once, it can do whole-program optimization. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 120 | template <class ELFT> void SymbolTable::addCombinedLTOObject() { |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 121 | if (BitcodeFiles.empty()) |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 122 | return; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 123 | |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 124 | // Compile bitcode files and replace bitcode symbols. |
Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 125 | LTO.reset(new BitcodeCompiler); |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 126 | for (BitcodeFile *F : BitcodeFiles) |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 127 | LTO->add(*F); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 128 | |
Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 129 | for (InputFile *File : LTO->compile()) { |
Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 130 | DenseSet<CachedHashStringRef> DummyGroups; |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 131 | cast<ObjFile<ELFT>>(File)->parse(DummyGroups); |
| 132 | ObjectFiles.push_back(File); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 136 | template <class ELFT> |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 137 | DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility, |
| 138 | uint8_t Binding) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 139 | Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding, |
| 140 | nullptr, nullptr); |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 141 | return cast<DefinedRegular>(Sym); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 144 | // Set a flag for --trace-symbol so that we can print out a log message |
| 145 | // if a new symbol with the same name is inserted into the symbol table. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 146 | void SymbolTable::trace(StringRef Name) { |
Justin Lebar | 3c11e93 | 2016-10-18 17:50:36 +0000 | [diff] [blame] | 147 | Symtab.insert({CachedHashStringRef(Name), {-1, true}}); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 150 | // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. |
| 151 | // Used to implement --wrap. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 152 | template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 153 | Symbol *Sym = find(Name); |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 154 | if (!Sym) |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 155 | return; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 156 | Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name)); |
| 157 | Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name)); |
Rui Ueyama | 1bdaf3e | 2016-11-09 23:37:40 +0000 | [diff] [blame] | 158 | |
Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 159 | defsym(Real, Sym); |
| 160 | defsym(Sym, Wrap); |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 161 | |
| 162 | WrapSymbols.push_back({Wrap, Real}); |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Rui Ueyama | 45b8140 | 2017-11-04 22:32:56 +0000 | [diff] [blame^] | 165 | // Apply symbol renames created by -wrap. The renames are created |
| 166 | // before LTO in addSymbolWrap() to have a chance to inform LTO (if |
| 167 | // LTO is running) not to include these symbols in IPO. Now that the |
Dmitry Mikulin | db3b87b | 2017-06-05 16:24:25 +0000 | [diff] [blame] | 168 | // symbols are finalized, we can perform the replacement. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 169 | void SymbolTable::applySymbolRenames() { |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 170 | // This function rotates 3 symbols: |
| 171 | // |
| 172 | // __real_foo becomes foo |
| 173 | // foo becomes __wrap_foo |
| 174 | // __wrap_foo becomes __real_foo |
| 175 | // |
| 176 | // The last part is special in that we don't want to change what references to |
| 177 | // __wrap_foo point to, we just want have __real_foo in the symbol table. |
| 178 | |
| 179 | // First make a copy of __real_foo |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 180 | std::vector<SymbolUnion> Origs; |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 181 | for (const auto &P : WrapSymbols) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 182 | Origs.emplace_back(*(SymbolUnion *)P.second); |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 183 | |
| 184 | // Replace __real_foo with foo and foo with __wrap_foo |
Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 185 | for (SymbolRenaming &S : Defsyms) { |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 186 | S.Dst->copyFrom(S.Src); |
Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 187 | S.Dst->File = S.Src->File; |
| 188 | S.Dst->Binding = S.Binding; |
Dmitry Mikulin | db3b87b | 2017-06-05 16:24:25 +0000 | [diff] [blame] | 189 | } |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 190 | |
| 191 | // Hide one of the copies of __wrap_foo, create a new symbol and copy |
| 192 | // __real_foo into it. |
| 193 | for (unsigned I = 0, N = WrapSymbols.size(); I < N; ++I) { |
| 194 | // We now have two copies of __wrap_foo. Drop one. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 195 | Symbol *Wrap = WrapSymbols[I].first; |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 196 | Wrap->IsUsedInRegularObj = false; |
| 197 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 198 | auto *Real = (Symbol *)&Origs[I]; |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 199 | // If __real_foo was undefined, we don't want it in the symbol table. |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 200 | if (!Real->isInCurrentOutput()) |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 201 | continue; |
| 202 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 203 | auto *NewSym = (Symbol *)make<SymbolUnion>(); |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 204 | memcpy(NewSym, Real, sizeof(SymbolUnion)); |
Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 205 | SymVector.push_back(NewSym); |
| 206 | } |
George Rimar | 9703ad2 | 2017-04-26 10:40:02 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 209 | static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { |
| 210 | if (VA == STV_DEFAULT) |
| 211 | return VB; |
| 212 | if (VB == STV_DEFAULT) |
| 213 | return VA; |
| 214 | return std::min(VA, VB); |
| 215 | } |
| 216 | |
Rui Ueyama | b4de595 | 2016-01-08 22:01:33 +0000 | [diff] [blame] | 217 | // Find an existing symbol or create and insert a new one. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 218 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) { |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 219 | // <name>@@<version> means the symbol is the default version. In that |
| 220 | // case <name>@@<version> will be used to resolve references to <name>. |
Rui Ueyama | 68b3acc | 2017-09-26 04:17:13 +0000 | [diff] [blame] | 221 | // |
| 222 | // Since this is a hot path, the following string search code is |
| 223 | // optimized for speed. StringRef::find(char) is much faster than |
| 224 | // StringRef::find(StringRef). |
| 225 | size_t Pos = Name.find('@'); |
| 226 | if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@') |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 227 | Name = Name.take_front(Pos); |
| 228 | |
Justin Lebar | 3c11e93 | 2016-10-18 17:50:36 +0000 | [diff] [blame] | 229 | auto P = Symtab.insert( |
Konstantin Zhuravlyov | 3f9b4b7 | 2017-10-16 18:49:28 +0000 | [diff] [blame] | 230 | {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)}); |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 231 | SymIndex &V = P.first->second; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 232 | bool IsNew = P.second; |
| 233 | |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 234 | if (V.Idx == -1) { |
| 235 | IsNew = true; |
Konstantin Zhuravlyov | 3f9b4b7 | 2017-10-16 18:49:28 +0000 | [diff] [blame] | 236 | V = SymIndex((int)SymVector.size(), true); |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 239 | Symbol *Sym; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 240 | if (IsNew) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 241 | Sym = (Symbol *)make<SymbolUnion>(); |
Rafael Espindola | d3fc0c9 | 2017-07-12 17:49:17 +0000 | [diff] [blame] | 242 | Sym->InVersionScript = false; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 243 | Sym->Binding = STB_WEAK; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 244 | Sym->Visibility = STV_DEFAULT; |
| 245 | Sym->IsUsedInRegularObj = false; |
| 246 | Sym->ExportDynamic = false; |
Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 247 | Sym->CanInline = true; |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 248 | Sym->Traced = V.Traced; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 249 | Sym->VersionId = Config->DefaultSymbolVersion; |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 250 | SymVector.push_back(Sym); |
| 251 | } else { |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 252 | Sym = SymVector[V.Idx]; |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 253 | } |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 254 | return {Sym, IsNew}; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 255 | } |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 256 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 257 | // Find an existing symbol or create and insert a new one, then apply the given |
| 258 | // attributes. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 259 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type, |
| 260 | uint8_t Visibility, |
| 261 | bool CanOmitFromDynSym, |
| 262 | InputFile *File) { |
| 263 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 264 | bool WasInserted; |
| 265 | std::tie(S, WasInserted) = insert(Name); |
| 266 | |
| 267 | // Merge in the new symbol's visibility. |
| 268 | S->Visibility = getMinVisibility(S->Visibility, Visibility); |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 269 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 270 | if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic)) |
| 271 | S->ExportDynamic = true; |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 272 | |
Rui Ueyama | ac64725 | 2017-10-29 16:46:39 +0000 | [diff] [blame] | 273 | if (!File || File->kind() == InputFile::ObjKind) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 274 | S->IsUsedInRegularObj = true; |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 275 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 276 | if (!WasInserted && S->Type != Symbol::UnknownType && |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 277 | ((Type == STT_TLS) != S->isTls())) { |
| 278 | error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " + |
| 279 | toString(S->File) + "\n>>> defined in " + toString(File)); |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 280 | } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 281 | |
| 282 | return {S, WasInserted}; |
| 283 | } |
| 284 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 285 | template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) { |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 286 | return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT, |
| 287 | /*Type*/ 0, |
| 288 | /*CanOmitFromDynSym*/ false, /*File*/ nullptr); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Rui Ueyama | e50e807 | 2016-12-22 05:11:12 +0000 | [diff] [blame] | 291 | static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; } |
| 292 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 293 | template <class ELFT> |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 294 | Symbol *SymbolTable::addUndefined(StringRef Name, bool IsLocal, uint8_t Binding, |
| 295 | uint8_t StOther, uint8_t Type, |
| 296 | bool CanOmitFromDynSym, InputFile *File) { |
| 297 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 298 | bool WasInserted; |
Rafael Espindola | 8465d083 | 2017-04-04 20:03:34 +0000 | [diff] [blame] | 299 | uint8_t Visibility = getVisibility(StOther); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 300 | std::tie(S, WasInserted) = |
Rafael Espindola | 8465d083 | 2017-04-04 20:03:34 +0000 | [diff] [blame] | 301 | insert(Name, Type, Visibility, CanOmitFromDynSym, File); |
| 302 | // An undefined symbol with non default visibility must be satisfied |
| 303 | // in the same DSO. |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 304 | if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 305 | S->Binding = Binding; |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 306 | replaceSymbol<Undefined>(S, File, Name, IsLocal, StOther, Type); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 307 | return S; |
| 308 | } |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 309 | if (Binding != STB_WEAK) { |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 310 | if (!S->isInCurrentOutput()) |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 311 | S->Binding = Binding; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 312 | if (auto *SS = dyn_cast<SharedSymbol>(S)) |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 313 | SS->getFile<ELFT>()->IsUsed = true; |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 314 | } |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 315 | if (auto *L = dyn_cast<Lazy>(S)) { |
Rafael Espindola | 9c8f853 | 2017-10-24 16:27:31 +0000 | [diff] [blame] | 316 | // An undefined weak will not fetch archive members. See comment on Lazy in |
| 317 | // Symbols.h for the details. |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 318 | if (S->isWeak()) |
| 319 | L->Type = Type; |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 320 | else if (InputFile *F = L->fetch()) |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 321 | addFile<ELFT>(F); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 322 | } |
| 323 | return S; |
| 324 | } |
| 325 | |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 326 | // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and |
| 327 | // foo@@VER. We want to effectively ignore foo, so give precedence to |
| 328 | // foo@@VER. |
| 329 | // FIXME: If users can transition to using |
| 330 | // .symver foo,foo@@@VER |
| 331 | // we can delete this hack. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 332 | static int compareVersion(Symbol *S, StringRef Name) { |
Rui Ueyama | 0deaacb | 2017-07-19 21:49:01 +0000 | [diff] [blame] | 333 | bool A = Name.contains("@@"); |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 334 | bool B = S->getName().contains("@@"); |
Rui Ueyama | 0deaacb | 2017-07-19 21:49:01 +0000 | [diff] [blame] | 335 | if (A && !B) |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 336 | return 1; |
Rui Ueyama | 0deaacb | 2017-07-19 21:49:01 +0000 | [diff] [blame] | 337 | if (!A && B) |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 338 | return -1; |
| 339 | return 0; |
| 340 | } |
| 341 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 342 | // We have a new defined symbol with the specified binding. Return 1 if the new |
| 343 | // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are |
| 344 | // strong defined symbols. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 345 | static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding, |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 346 | StringRef Name) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 347 | if (WasInserted) |
| 348 | return 1; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 349 | if (!S->isInCurrentOutput()) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 350 | return 1; |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 351 | if (int R = compareVersion(S, Name)) |
| 352 | return R; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 353 | if (Binding == STB_WEAK) |
| 354 | return -1; |
| 355 | if (S->isWeak()) |
| 356 | return 1; |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | // We have a new non-common defined symbol with the specified binding. Return 1 |
| 361 | // if the new symbol should win, -1 if the new symbol should lose, or 0 if there |
| 362 | // is a conflict. If the new symbol wins, also update the binding. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 363 | static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding, |
| 364 | bool IsAbsolute, uint64_t Value, |
| 365 | StringRef Name) { |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 366 | if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 367 | if (Cmp > 0) |
| 368 | S->Binding = Binding; |
| 369 | return Cmp; |
| 370 | } |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 371 | if (isa<DefinedCommon>(S)) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 372 | // Non-common symbols take precedence over common symbols. |
| 373 | if (Config->WarnCommon) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 374 | warn("common " + S->getName() + " is overridden"); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 375 | return 1; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 376 | } |
| 377 | if (auto *R = dyn_cast<DefinedRegular>(S)) { |
Rafael Espindola | 858c092 | 2016-12-02 02:58:21 +0000 | [diff] [blame] | 378 | if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute && |
| 379 | R->Value == Value) |
| 380 | return -1; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 385 | Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment, |
| 386 | uint8_t Binding, uint8_t StOther, uint8_t Type, |
| 387 | InputFile *File) { |
| 388 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 389 | bool WasInserted; |
Rui Ueyama | e50e807 | 2016-12-22 05:11:12 +0000 | [diff] [blame] | 390 | std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther), |
| 391 | /*CanOmitFromDynSym*/ false, File); |
Rafael Espindola | 3ddf211 | 2017-07-19 16:45:05 +0000 | [diff] [blame] | 392 | int Cmp = compareDefined(S, WasInserted, Binding, N); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 393 | if (Cmp > 0) { |
| 394 | S->Binding = Binding; |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 395 | replaceSymbol<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 396 | } else if (Cmp == 0) { |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 397 | auto *C = dyn_cast<DefinedCommon>(S); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 398 | if (!C) { |
| 399 | // Non-common symbols take precedence over common symbols. |
| 400 | if (Config->WarnCommon) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 401 | warn("common " + S->getName() + " is overridden"); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 402 | return S; |
| 403 | } |
| 404 | |
| 405 | if (Config->WarnCommon) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 406 | warn("multiple common of " + S->getName()); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 407 | |
Rafael Espindola | 8db8729 | 2016-08-31 13:42:08 +0000 | [diff] [blame] | 408 | Alignment = C->Alignment = std::max(C->Alignment, Alignment); |
| 409 | if (Size > C->Size) |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 410 | replaceSymbol<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 411 | } |
| 412 | return S; |
| 413 | } |
| 414 | |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 415 | static void warnOrError(const Twine &Msg) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 416 | if (Config->AllowMultipleDefinition) |
Rui Ueyama | d31e13f | 2016-09-29 21:00:23 +0000 | [diff] [blame] | 417 | warn(Msg); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 418 | else |
| 419 | error(Msg); |
| 420 | } |
| 421 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 422 | static void reportDuplicate(Symbol *Sym, InputFile *NewFile) { |
George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 423 | warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " + |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 424 | toString(Sym->getFile()) + "\n>>> defined in " + |
| 425 | toString(NewFile)); |
Eugene Leviant | 825e538 | 2016-11-08 16:26:32 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | template <class ELFT> |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 429 | static void reportDuplicate(Symbol *Sym, InputSectionBase *ErrSec, |
Eugene Leviant | 825e538 | 2016-11-08 16:26:32 +0000 | [diff] [blame] | 430 | typename ELFT::uint ErrOffset) { |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 431 | DefinedRegular *D = dyn_cast<DefinedRegular>(Sym); |
Eugene Leviant | 825e538 | 2016-11-08 16:26:32 +0000 | [diff] [blame] | 432 | if (!D || !D->Section || !ErrSec) { |
Rafael Espindola | cb83c8c | 2017-07-25 23:23:40 +0000 | [diff] [blame] | 433 | reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr); |
Eugene Leviant | 825e538 | 2016-11-08 16:26:32 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 437 | // Construct and print an error message in the form of: |
| 438 | // |
| 439 | // ld.lld: error: duplicate symbol: foo |
| 440 | // >>> defined at bar.c:30 |
| 441 | // >>> bar.o (/home/alice/src/bar.o) |
| 442 | // >>> defined at baz.c:563 |
| 443 | // >>> baz.o in archive libbaz.a |
| 444 | auto *Sec1 = cast<InputSectionBase>(D->Section); |
George Rimar | 82f0c42 | 2017-11-01 07:42:38 +0000 | [diff] [blame] | 445 | std::string Src1 = Sec1->getSrcMsg<ELFT>(*Sym, D->Value); |
Rui Ueyama | d6b7a39 | 2017-10-27 03:13:54 +0000 | [diff] [blame] | 446 | std::string Obj1 = Sec1->getObjMsg(D->Value); |
George Rimar | 82f0c42 | 2017-11-01 07:42:38 +0000 | [diff] [blame] | 447 | std::string Src2 = ErrSec->getSrcMsg<ELFT>(*Sym, ErrOffset); |
Rui Ueyama | d6b7a39 | 2017-10-27 03:13:54 +0000 | [diff] [blame] | 448 | std::string Obj2 = ErrSec->getObjMsg(ErrOffset); |
Eugene Leviant | 825e538 | 2016-11-08 16:26:32 +0000 | [diff] [blame] | 449 | |
Rui Ueyama | 810ce10 | 2017-03-31 23:40:21 +0000 | [diff] [blame] | 450 | std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at "; |
| 451 | if (!Src1.empty()) |
| 452 | Msg += Src1 + "\n>>> "; |
| 453 | Msg += Obj1 + "\n>>> defined at "; |
| 454 | if (!Src2.empty()) |
| 455 | Msg += Src2 + "\n>>> "; |
| 456 | Msg += Obj2; |
| 457 | warnOrError(Msg); |
Eugene Leviant | 825e538 | 2016-11-08 16:26:32 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 460 | template <typename ELFT> |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 461 | Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type, |
| 462 | uint64_t Value, uint64_t Size, uint8_t Binding, |
| 463 | SectionBase *Section, InputFile *File) { |
| 464 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 465 | bool WasInserted; |
Rui Ueyama | e50e807 | 2016-12-22 05:11:12 +0000 | [diff] [blame] | 466 | std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther), |
George Rimar | 463984d | 2016-11-15 08:07:14 +0000 | [diff] [blame] | 467 | /*CanOmitFromDynSym*/ false, File); |
Rafael Espindola | d4fbe4f | 2017-07-25 23:15:35 +0000 | [diff] [blame] | 468 | int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr, |
| 469 | Value, Name); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 470 | if (Cmp > 0) |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 471 | replaceSymbol<DefinedRegular>(S, File, Name, /*IsLocal=*/false, StOther, |
| 472 | Type, Value, Size, Section); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 473 | else if (Cmp == 0) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 474 | reportDuplicate<ELFT>(S, dyn_cast_or_null<InputSectionBase>(Section), |
| 475 | Value); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 476 | return S; |
| 477 | } |
| 478 | |
| 479 | template <typename ELFT> |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 480 | void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> *File, |
Rui Ueyama | 7f9694a | 2017-10-28 20:15:56 +0000 | [diff] [blame] | 481 | const typename ELFT::Sym &Sym, uint32_t Alignment, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 482 | const typename ELFT::Verdef *Verdef) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 483 | // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT |
| 484 | // as the visibility, which will leave the visibility in the symbol table |
| 485 | // unchanged. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 486 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 487 | bool WasInserted; |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 488 | std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT, |
| 489 | /*CanOmitFromDynSym*/ true, File); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 490 | // Make sure we preempt DSO symbols with default visibility. |
Rafael Espindola | 41a93a3 | 2017-01-16 17:35:23 +0000 | [diff] [blame] | 491 | if (Sym.getVisibility() == STV_DEFAULT) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 492 | S->ExportDynamic = true; |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 493 | |
Rafael Espindola | 8465d083 | 2017-04-04 20:03:34 +0000 | [diff] [blame] | 494 | // An undefined symbol with non default visibility must be satisfied |
| 495 | // in the same DSO. |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 496 | if (WasInserted || ((S->isUndefined() || S->isLazy()) && |
| 497 | S->getVisibility() == STV_DEFAULT)) { |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 498 | replaceSymbol<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(), |
| 499 | Sym.st_value, Sym.st_size, Alignment, Verdef); |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 500 | if (!S->isWeak()) |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 501 | File->IsUsed = true; |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 502 | } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 505 | Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding, |
| 506 | uint8_t StOther, uint8_t Type, |
| 507 | bool CanOmitFromDynSym, BitcodeFile *F) { |
| 508 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 509 | bool WasInserted; |
Davide Italiano | 35af5b3 | 2016-08-30 20:15:03 +0000 | [diff] [blame] | 510 | std::tie(S, WasInserted) = |
Rui Ueyama | e50e807 | 2016-12-22 05:11:12 +0000 | [diff] [blame] | 511 | insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F); |
Rafael Espindola | d4fbe4f | 2017-07-25 23:15:35 +0000 | [diff] [blame] | 512 | int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, |
| 513 | /*IsAbs*/ false, /*Value*/ 0, Name); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 514 | if (Cmp > 0) |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 515 | replaceSymbol<DefinedRegular>(S, F, Name, /*IsLocal=*/false, StOther, Type, |
| 516 | 0, 0, nullptr); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 517 | else if (Cmp == 0) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 518 | reportDuplicate(S, F); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 519 | return S; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 520 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 521 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 522 | Symbol *SymbolTable::find(StringRef Name) { |
Justin Lebar | 3c11e93 | 2016-10-18 17:50:36 +0000 | [diff] [blame] | 523 | auto It = Symtab.find(CachedHashStringRef(Name)); |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 524 | if (It == Symtab.end()) |
| 525 | return nullptr; |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 526 | SymIndex V = It->second; |
| 527 | if (V.Idx == -1) |
| 528 | return nullptr; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 529 | return SymVector[V.Idx]; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 532 | void SymbolTable::defsym(Symbol *Dst, Symbol *Src) { |
Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 533 | // We want to tell LTO not to inline Dst symbol because LTO doesn't |
| 534 | // know the final symbol contents after renaming. |
| 535 | Dst->CanInline = false; |
Rafael Espindola | c29b24d | 2017-10-05 00:35:47 +0000 | [diff] [blame] | 536 | |
| 537 | // Tell LTO not to eliminate this symbol. |
| 538 | Src->IsUsedInRegularObj = true; |
| 539 | |
Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 540 | Defsyms.push_back({Dst, Src, Dst->Binding}); |
| 541 | } |
| 542 | |
Rafael Espindola | 8a59f5c | 2017-01-13 19:18:11 +0000 | [diff] [blame] | 543 | template <class ELFT> |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 544 | Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F, |
| 545 | const object::Archive::Symbol Sym) { |
| 546 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 547 | bool WasInserted; |
Rui Ueyama | dace838 | 2016-07-21 13:13:21 +0000 | [diff] [blame] | 548 | std::tie(S, WasInserted) = insert(Name); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 549 | if (WasInserted) { |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 550 | replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType); |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 +0000 | [diff] [blame] | 551 | return S; |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 552 | } |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 553 | if (!S->isUndefined()) |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 +0000 | [diff] [blame] | 554 | return S; |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 555 | |
Rafael Espindola | 9c8f853 | 2017-10-24 16:27:31 +0000 | [diff] [blame] | 556 | // An undefined weak will not fetch archive members. See comment on Lazy in |
| 557 | // Symbols.h for the details. |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 558 | if (S->isWeak()) { |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 559 | replaceSymbol<LazyArchive>(S, F, Sym, S->Type); |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 +0000 | [diff] [blame] | 560 | return S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 561 | } |
Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 562 | std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym); |
| 563 | if (!MBInfo.first.getBuffer().empty()) |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 564 | addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second)); |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 +0000 | [diff] [blame] | 565 | return S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | template <class ELFT> |
Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 +0000 | [diff] [blame] | 569 | void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 570 | Symbol *S; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 571 | bool WasInserted; |
| 572 | std::tie(S, WasInserted) = insert(Name); |
| 573 | if (WasInserted) { |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 574 | replaceSymbol<LazyObject>(S, &Obj, Name, Symbol::UnknownType); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 575 | return; |
| 576 | } |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 577 | if (!S->isUndefined()) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 578 | return; |
| 579 | |
| 580 | // See comment for addLazyArchive above. |
Rafael Espindola | 808f2d3 | 2017-05-04 14:54:48 +0000 | [diff] [blame] | 581 | if (S->isWeak()) |
Rui Ueyama | f483da0 | 2017-11-03 22:48:47 +0000 | [diff] [blame] | 582 | replaceSymbol<LazyObject>(S, &Obj, Name, S->Type); |
Rafael Espindola | 808f2d3 | 2017-05-04 14:54:48 +0000 | [diff] [blame] | 583 | else if (InputFile *F = Obj.fetch()) |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 584 | addFile<ELFT>(F); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 585 | } |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 586 | |
Igor Kudrin | fb7f8be | 2017-10-03 12:23:46 +0000 | [diff] [blame] | 587 | // If we already saw this symbol, force loading its file. |
| 588 | template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 589 | if (Symbol *B = find(Name)) { |
Igor Kudrin | fb7f8be | 2017-10-03 12:23:46 +0000 | [diff] [blame] | 590 | // Mark the symbol not to be eliminated by LTO |
| 591 | // even if it is a bitcode symbol. |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 592 | B->IsUsedInRegularObj = true; |
Igor Kudrin | fb7f8be | 2017-10-03 12:23:46 +0000 | [diff] [blame] | 593 | if (auto *L = dyn_cast_or_null<Lazy>(B)) |
| 594 | if (InputFile *File = L->fetch()) |
| 595 | addFile<ELFT>(File); |
| 596 | } |
| 597 | } |
| 598 | |
Rui Ueyama | 93bfee5 | 2015-10-13 18:10:33 +0000 | [diff] [blame] | 599 | // This function takes care of the case in which shared libraries depend on |
| 600 | // the user program (not the other way, which is usual). Shared libraries |
| 601 | // may have undefined symbols, expecting that the user program provides |
| 602 | // the definitions for them. An example is BSD's __progname symbol. |
| 603 | // We need to put such symbols to the main program's .dynsym so that |
| 604 | // shared libraries can find them. |
| 605 | // Except this, we ignore undefined symbols in DSOs. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 606 | template <class ELFT> void SymbolTable::scanShlibUndefined() { |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 607 | for (InputFile *F : SharedFiles) { |
| 608 | for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 609 | Symbol *Sym = find(U); |
Rui Ueyama | 321b9cd | 2017-04-25 00:15:48 +0000 | [diff] [blame] | 610 | if (!Sym || !Sym->isDefined()) |
| 611 | continue; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 612 | Sym->ExportDynamic = true; |
Rui Ueyama | 321b9cd | 2017-04-25 00:15:48 +0000 | [diff] [blame] | 613 | |
| 614 | // If -dynamic-list is given, the default version is set to |
| 615 | // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym. |
| 616 | // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were |
| 617 | // specified by -dynamic-list. |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 618 | Sym->VersionId = VER_NDX_GLOBAL; |
Rui Ueyama | 321b9cd | 2017-04-25 00:15:48 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 623 | // Initialize DemangledSyms with a map from demangled symbols to symbol |
| 624 | // objects. Used to handle "extern C++" directive in version scripts. |
| 625 | // |
| 626 | // The map will contain all demangled symbols. That can be very large, |
| 627 | // and in LLD we generally want to avoid do anything for each symbol. |
| 628 | // Then, why are we doing this? Here's why. |
| 629 | // |
| 630 | // Users can use "extern C++ {}" directive to match against demangled |
| 631 | // C++ symbols. For example, you can write a pattern such as |
| 632 | // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this |
| 633 | // other than trying to match a pattern against all demangled symbols. |
| 634 | // So, if "extern C++" feature is used, we need to demangle all known |
| 635 | // symbols. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 636 | StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() { |
Rui Ueyama | 96aff37 | 2016-12-22 05:31:52 +0000 | [diff] [blame] | 637 | if (!DemangledSyms) { |
| 638 | DemangledSyms.emplace(); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 639 | for (Symbol *Sym : SymVector) { |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 640 | if (!Sym->isInCurrentOutput()) |
Rui Ueyama | 4c134ea | 2016-12-22 09:54:32 +0000 | [diff] [blame] | 641 | continue; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 642 | if (Optional<std::string> S = demangle(Sym->getName())) |
| 643 | (*DemangledSyms)[*S].push_back(Sym); |
Rui Ueyama | 96aff37 | 2016-12-22 05:31:52 +0000 | [diff] [blame] | 644 | else |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 645 | (*DemangledSyms)[Sym->getName()].push_back(Sym); |
Rui Ueyama | 96aff37 | 2016-12-22 05:31:52 +0000 | [diff] [blame] | 646 | } |
Rui Ueyama | d632852 | 2016-07-18 01:34:57 +0000 | [diff] [blame] | 647 | } |
Rui Ueyama | 96aff37 | 2016-12-22 05:31:52 +0000 | [diff] [blame] | 648 | return *DemangledSyms; |
George Rimar | 50dcece | 2016-07-16 12:26:39 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 651 | std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) { |
Rui Ueyama | 96aff37 | 2016-12-22 05:31:52 +0000 | [diff] [blame] | 652 | if (Ver.IsExternCpp) |
| 653 | return getDemangledSyms().lookup(Ver.Name); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 654 | if (Symbol *B = find(Ver.Name)) |
Rui Ueyama | bda337a | 2017-10-27 22:54:16 +0000 | [diff] [blame] | 655 | if (B->isInCurrentOutput()) |
Rui Ueyama | 4c134ea | 2016-12-22 09:54:32 +0000 | [diff] [blame] | 656 | return {B}; |
| 657 | return {}; |
Rafael Espindola | c65aee6 | 2016-12-08 15:56:33 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 660 | std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) { |
| 661 | std::vector<Symbol *> Res; |
Vitaly Buka | 0b7de06 | 2016-12-21 02:27:14 +0000 | [diff] [blame] | 662 | StringMatcher M(Ver.Name); |
Rafael Espindola | 191390a | 2016-12-08 16:26:20 +0000 | [diff] [blame] | 663 | |
Rafael Espindola | defdfa8 | 2016-12-08 16:02:48 +0000 | [diff] [blame] | 664 | if (Ver.IsExternCpp) { |
Rui Ueyama | 96aff37 | 2016-12-22 05:31:52 +0000 | [diff] [blame] | 665 | for (auto &P : getDemangledSyms()) |
Rafael Espindola | defdfa8 | 2016-12-08 16:02:48 +0000 | [diff] [blame] | 666 | if (M.match(P.first())) |
Rui Ueyama | 4c134ea | 2016-12-22 09:54:32 +0000 | [diff] [blame] | 667 | Res.insert(Res.end(), P.second.begin(), P.second.end()); |
Rafael Espindola | defdfa8 | 2016-12-08 16:02:48 +0000 | [diff] [blame] | 668 | return Res; |
| 669 | } |
Rafael Espindola | 191390a | 2016-12-08 16:26:20 +0000 | [diff] [blame] | 670 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 671 | for (Symbol *Sym : SymVector) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 672 | if (Sym->isInCurrentOutput() && M.match(Sym->getName())) |
| 673 | Res.push_back(Sym); |
Rafael Espindola | 191390a | 2016-12-08 16:26:20 +0000 | [diff] [blame] | 674 | return Res; |
Rafael Espindola | c65aee6 | 2016-12-08 15:56:33 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Rui Ueyama | ea26504 | 2016-09-13 20:51:30 +0000 | [diff] [blame] | 677 | // If there's only one anonymous version definition in a version |
George Rimar | 92ca6f4 | 2016-11-14 09:56:35 +0000 | [diff] [blame] | 678 | // script file, the script does not actually define any symbol version, |
Rafael Espindola | e999ddb | 2017-01-10 16:37:24 +0000 | [diff] [blame] | 679 | // but just specifies symbols visibilities. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 680 | void SymbolTable::handleAnonymousVersion() { |
Rafael Espindola | e999ddb | 2017-01-10 16:37:24 +0000 | [diff] [blame] | 681 | for (SymbolVersion &Ver : Config->VersionScriptGlobals) |
| 682 | assignExactVersion(Ver, VER_NDX_GLOBAL, "global"); |
| 683 | for (SymbolVersion &Ver : Config->VersionScriptGlobals) |
| 684 | assignWildcardVersion(Ver, VER_NDX_GLOBAL); |
| 685 | for (SymbolVersion &Ver : Config->VersionScriptLocals) |
| 686 | assignExactVersion(Ver, VER_NDX_LOCAL, "local"); |
| 687 | for (SymbolVersion &Ver : Config->VersionScriptLocals) |
| 688 | assignWildcardVersion(Ver, VER_NDX_LOCAL); |
Rui Ueyama | ea26504 | 2016-09-13 20:51:30 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 +0000 | [diff] [blame] | 691 | // Handles -dynamic-list. |
| 692 | void SymbolTable::handleDynamicList() { |
| 693 | for (SymbolVersion &Ver : Config->DynamicList) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 694 | std::vector<Symbol *> Syms; |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 +0000 | [diff] [blame] | 695 | if (Ver.HasWildcard) |
| 696 | Syms = findByVersion(Ver); |
| 697 | else |
| 698 | Syms = findAllByVersion(Ver); |
| 699 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 700 | for (Symbol *B : Syms) { |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 +0000 | [diff] [blame] | 701 | if (!Config->Shared) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 702 | B->ExportDynamic = true; |
| 703 | else if (B->includeInDynsym()) |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 +0000 | [diff] [blame] | 704 | B->IsPreemptible = true; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Rui Ueyama | 94bcfae | 2016-11-17 02:09:42 +0000 | [diff] [blame] | 709 | // Set symbol versions to symbols. This function handles patterns |
| 710 | // containing no wildcard characters. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 711 | void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId, |
| 712 | StringRef VersionName) { |
Rui Ueyama | 8980c92 | 2016-11-18 06:30:08 +0000 | [diff] [blame] | 713 | if (Ver.HasWildcard) |
Rui Ueyama | 94bcfae | 2016-11-17 02:09:42 +0000 | [diff] [blame] | 714 | return; |
| 715 | |
| 716 | // Get a list of symbols which we need to assign the version to. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 717 | std::vector<Symbol *> Syms = findByVersion(Ver); |
Rui Ueyama | 4c134ea | 2016-12-22 09:54:32 +0000 | [diff] [blame] | 718 | if (Syms.empty()) { |
| 719 | if (Config->NoUndefinedVersion) |
| 720 | error("version script assignment of '" + VersionName + "' to symbol '" + |
| 721 | Ver.Name + "' failed: symbol not defined"); |
| 722 | return; |
| 723 | } |
Rui Ueyama | 94bcfae | 2016-11-17 02:09:42 +0000 | [diff] [blame] | 724 | |
| 725 | // Assign the version. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 726 | for (Symbol *Sym : Syms) { |
George Rimar | 3e8a461 | 2017-07-12 13:54:42 +0000 | [diff] [blame] | 727 | // Skip symbols containing version info because symbol versions |
| 728 | // specified by symbol names take precedence over version scripts. |
| 729 | // See parseSymbolVersion(). |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 730 | if (Sym->getName().contains('@')) |
George Rimar | 3e8a461 | 2017-07-12 13:54:42 +0000 | [diff] [blame] | 731 | continue; |
| 732 | |
Rafael Espindola | d3fc0c9 | 2017-07-12 17:49:17 +0000 | [diff] [blame] | 733 | if (Sym->InVersionScript) |
Rui Ueyama | aade0e2 | 2016-11-17 03:32:41 +0000 | [diff] [blame] | 734 | warn("duplicate symbol '" + Ver.Name + "' in version script"); |
Rafael Espindola | dd9dd48 | 2016-12-09 22:40:49 +0000 | [diff] [blame] | 735 | Sym->VersionId = VersionId; |
Rafael Espindola | d3fc0c9 | 2017-07-12 17:49:17 +0000 | [diff] [blame] | 736 | Sym->InVersionScript = true; |
Rui Ueyama | 94bcfae | 2016-11-17 02:09:42 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 740 | void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) { |
Rui Ueyama | 8980c92 | 2016-11-18 06:30:08 +0000 | [diff] [blame] | 741 | if (!Ver.HasWildcard) |
Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 742 | return; |
Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 743 | |
| 744 | // Exact matching takes precendence over fuzzy matching, |
| 745 | // so we set a version to a symbol only if no version has been assigned |
| 746 | // to the symbol. This behavior is compatible with GNU. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 747 | for (Symbol *B : findAllByVersion(Ver)) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 748 | if (B->VersionId == Config->DefaultSymbolVersion) |
| 749 | B->VersionId = VersionId; |
Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Rui Ueyama | dad2b88 | 2016-09-02 22:15:08 +0000 | [diff] [blame] | 752 | // This function processes version scripts by updating VersionId |
| 753 | // member of symbols. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 754 | void SymbolTable::scanVersionScript() { |
Rui Ueyama | ea26504 | 2016-09-13 20:51:30 +0000 | [diff] [blame] | 755 | // Handle edge cases first. |
Rafael Espindola | e999ddb | 2017-01-10 16:37:24 +0000 | [diff] [blame] | 756 | handleAnonymousVersion(); |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 +0000 | [diff] [blame] | 757 | handleDynamicList(); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 758 | |
Rui Ueyama | dad2b88 | 2016-09-02 22:15:08 +0000 | [diff] [blame] | 759 | // Now we have version definitions, so we need to set version ids to symbols. |
| 760 | // Each version definition has a glob pattern, and all symbols that match |
| 761 | // with the pattern get that version. |
| 762 | |
Rui Ueyama | dad2b88 | 2016-09-02 22:15:08 +0000 | [diff] [blame] | 763 | // First, we assign versions to exact matching symbols, |
| 764 | // i.e. version definitions not containing any glob meta-characters. |
George Rimar | 17c65af | 2016-11-16 18:46:23 +0000 | [diff] [blame] | 765 | for (VersionDefinition &V : Config->VersionDefinitions) |
Rui Ueyama | 96db27c | 2016-11-17 19:57:45 +0000 | [diff] [blame] | 766 | for (SymbolVersion &Ver : V.Globals) |
| 767 | assignExactVersion(Ver, V.Id, V.Name); |
George Rimar | f73a258 | 2016-07-07 07:45:27 +0000 | [diff] [blame] | 768 | |
Rui Ueyama | dad2b88 | 2016-09-02 22:15:08 +0000 | [diff] [blame] | 769 | // Next, we assign versions to fuzzy matching symbols, |
| 770 | // i.e. version definitions containing glob meta-characters. |
| 771 | // Note that because the last match takes precedence over previous matches, |
| 772 | // we iterate over the definitions in the reverse order. |
Rui Ueyama | cd236a9 | 2016-11-17 19:57:43 +0000 | [diff] [blame] | 773 | for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions)) |
| 774 | for (SymbolVersion &Ver : V.Globals) |
| 775 | assignWildcardVersion(Ver, V.Id); |
George Rimar | 3e8a461 | 2017-07-12 13:54:42 +0000 | [diff] [blame] | 776 | |
| 777 | // Symbol themselves might know their versions because symbols |
| 778 | // can contain versions in the form of <name>@<version>. |
| 779 | // Let them parse and update their names to exclude version suffix. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 780 | for (Symbol *Sym : SymVector) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 781 | Sym->parseSymbolVersion(); |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 784 | template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef); |
| 785 | template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef); |
| 786 | template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef); |
| 787 | template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef); |
| 788 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 789 | template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef); |
| 790 | template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef); |
| 791 | template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef); |
| 792 | template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef); |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 793 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 794 | template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, bool, uint8_t, |
| 795 | uint8_t, uint8_t, bool, |
| 796 | InputFile *); |
| 797 | template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, bool, uint8_t, |
| 798 | uint8_t, uint8_t, bool, |
| 799 | InputFile *); |
| 800 | template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, bool, uint8_t, |
| 801 | uint8_t, uint8_t, bool, |
| 802 | InputFile *); |
| 803 | template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, bool, uint8_t, |
| 804 | uint8_t, uint8_t, bool, |
| 805 | InputFile *); |
Rui Ueyama | 3e96f11 | 2017-07-26 21:37:11 +0000 | [diff] [blame] | 806 | |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 807 | template void SymbolTable::addCombinedLTOObject<ELF32LE>(); |
| 808 | template void SymbolTable::addCombinedLTOObject<ELF32BE>(); |
| 809 | template void SymbolTable::addCombinedLTOObject<ELF64LE>(); |
| 810 | template void SymbolTable::addCombinedLTOObject<ELF64BE>(); |
| 811 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 812 | template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t, |
| 813 | uint64_t, uint64_t, uint8_t, |
| 814 | SectionBase *, InputFile *); |
| 815 | template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t, |
| 816 | uint64_t, uint64_t, uint8_t, |
| 817 | SectionBase *, InputFile *); |
| 818 | template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t, |
| 819 | uint64_t, uint64_t, uint8_t, |
| 820 | SectionBase *, InputFile *); |
| 821 | template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t, |
| 822 | uint64_t, uint64_t, uint8_t, |
| 823 | SectionBase *, InputFile *); |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 824 | |
| 825 | template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t, |
| 826 | uint8_t); |
| 827 | template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t, |
| 828 | uint8_t); |
| 829 | template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t, |
| 830 | uint8_t); |
| 831 | template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t, |
| 832 | uint8_t); |
| 833 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 834 | template Symbol * |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 835 | SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 836 | const object::Archive::Symbol); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 837 | template Symbol * |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 838 | SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 839 | const object::Archive::Symbol); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 840 | template Symbol * |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 841 | SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 842 | const object::Archive::Symbol); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 843 | template Symbol * |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 844 | SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 845 | const object::Archive::Symbol); |
| 846 | |
Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 +0000 | [diff] [blame] | 847 | template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &); |
| 848 | template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &); |
| 849 | template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &); |
| 850 | template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &); |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 851 | |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 852 | template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 853 | const typename ELF32LE::Sym &, |
Rui Ueyama | 7f9694a | 2017-10-28 20:15:56 +0000 | [diff] [blame] | 854 | uint32_t Alignment, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 855 | const typename ELF32LE::Verdef *); |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 856 | template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 857 | const typename ELF32BE::Sym &, |
Rui Ueyama | 7f9694a | 2017-10-28 20:15:56 +0000 | [diff] [blame] | 858 | uint32_t Alignment, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 859 | const typename ELF32BE::Verdef *); |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 860 | template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 861 | const typename ELF64LE::Sym &, |
Rui Ueyama | 7f9694a | 2017-10-28 20:15:56 +0000 | [diff] [blame] | 862 | uint32_t Alignment, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 863 | const typename ELF64LE::Verdef *); |
Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 864 | template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 865 | const typename ELF64BE::Sym &, |
Rui Ueyama | 7f9694a | 2017-10-28 20:15:56 +0000 | [diff] [blame] | 866 | uint32_t Alignment, |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 867 | const typename ELF64BE::Verdef *); |
| 868 | |
Igor Kudrin | fb7f8be | 2017-10-03 12:23:46 +0000 | [diff] [blame] | 869 | template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef); |
| 870 | template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef); |
| 871 | template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef); |
| 872 | template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef); |
| 873 | |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 874 | template void SymbolTable::scanShlibUndefined<ELF32LE>(); |
| 875 | template void SymbolTable::scanShlibUndefined<ELF32BE>(); |
| 876 | template void SymbolTable::scanShlibUndefined<ELF64LE>(); |
| 877 | template void SymbolTable::scanShlibUndefined<ELF64BE>(); |