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