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