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