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