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