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