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