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 | 93c9af4 | 2016-06-29 08:01:32 +0000 | [diff] [blame^] | 21 | #include "Strings.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 22 | #include "Symbols.h" |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 23 | #include "llvm/Bitcode/ReaderWriter.h" |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 24 | #include "llvm/Support/StringSaver.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. |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 36 | template <class ELFT> static bool isCompatible(InputFile *FileP) { |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 37 | auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP); |
| 38 | if (!F) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 39 | return true; |
Rui Ueyama | 5e64d3f | 2016-06-29 01:30:50 +0000 | [diff] [blame] | 40 | if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 41 | return true; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 42 | StringRef A = F->getName(); |
| 43 | StringRef B = Config->Emulation; |
| 44 | if (B.empty()) |
| 45 | B = Config->FirstElf->getName(); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 46 | error(A + " is incompatible with " + B); |
| 47 | return false; |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 50 | // Add symbols in File to the symbol table. |
Rui Ueyama | 25b44c9 | 2015-12-16 23:31:22 +0000 | [diff] [blame] | 51 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 52 | void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) { |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 53 | InputFile *FileP = File.get(); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 54 | if (!isCompatible<ELFT>(FileP)) |
| 55 | return; |
Rafael Espindola | 525914d | 2015-10-11 03:36:49 +0000 | [diff] [blame] | 56 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 57 | // .a file |
| 58 | if (auto *F = dyn_cast<ArchiveFile>(FileP)) { |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 59 | ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release())); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 60 | F->parse<ELFT>(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 61 | return; |
| 62 | } |
Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 63 | |
George Rimar | 2a78fce | 2016-04-13 18:07:57 +0000 | [diff] [blame] | 64 | // Lazy object file |
| 65 | if (auto *F = dyn_cast<LazyObjectFile>(FileP)) { |
| 66 | LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release())); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 67 | F->parse<ELFT>(); |
George Rimar | 2a78fce | 2016-04-13 18:07:57 +0000 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
| 71 | if (Config->Trace) |
| 72 | llvm::outs() << getFilename(FileP) << "\n"; |
| 73 | |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 74 | // .so file |
| 75 | if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) { |
| 76 | // DSOs are uniquified not by filename but by soname. |
| 77 | F->parseSoName(); |
Rui Ueyama | 131e0ff | 2016-01-08 22:17:42 +0000 | [diff] [blame] | 78 | if (!SoNames.insert(F->getSoName()).second) |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 79 | return; |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 80 | |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 81 | SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release())); |
Rui Ueyama | 7c71331 | 2016-01-06 01:56:36 +0000 | [diff] [blame] | 82 | F->parseRest(); |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 83 | return; |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 84 | } |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 85 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 86 | // LLVM bitcode file |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 87 | if (auto *F = dyn_cast<BitcodeFile>(FileP)) { |
| 88 | BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release())); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 89 | F->parse<ELFT>(ComdatGroups); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 93 | // Regular object file |
Rui Ueyama | 8957574 | 2015-12-16 22:59:13 +0000 | [diff] [blame] | 94 | auto *F = cast<ObjectFile<ELFT>>(FileP); |
Rafael Espindola | 21f7bd4 | 2015-12-23 14:35:51 +0000 | [diff] [blame] | 95 | ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release())); |
Rui Ueyama | 52d3b67 | 2016-01-06 02:06:33 +0000 | [diff] [blame] | 96 | F->parse(ComdatGroups); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Rui Ueyama | 4255475 | 2016-04-23 00:26:32 +0000 | [diff] [blame] | 99 | // This function is where all the optimizations of link-time |
| 100 | // optimization happens. When LTO is in use, some input files are |
| 101 | // not in native object file format but in the LLVM bitcode format. |
| 102 | // This function compiles bitcode files into a few big native files |
| 103 | // using LLVM functions and replaces bitcode symbols with the results. |
| 104 | // Because all bitcode files that consist of a program are passed |
| 105 | // to the compiler at once, it can do whole-program optimization. |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 106 | template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() { |
| 107 | if (BitcodeFiles.empty()) |
| 108 | return; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 109 | |
| 110 | // Compile bitcode files. |
| 111 | Lto.reset(new BitcodeCompiler); |
| 112 | for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) |
| 113 | Lto->add(*F); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 114 | std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile(); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 115 | |
| 116 | // Replace bitcode symbols. |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 117 | for (auto &IF : IFs) { |
| 118 | ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release()); |
| 119 | |
| 120 | llvm::DenseSet<StringRef> DummyGroups; |
| 121 | Obj->parse(DummyGroups); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 122 | ObjectFiles.emplace_back(Obj); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 126 | template <class ELFT> |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 127 | DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name, |
| 128 | uint8_t Visibility) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 129 | return cast<DefinedRegular<ELFT>>( |
| 130 | addRegular(Name, STB_GLOBAL, Visibility)->body()); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Rui Ueyama | c9559d9 | 2016-01-05 20:47:37 +0000 | [diff] [blame] | 133 | // Add Name as an "ignored" symbol. An ignored symbol is a regular |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 134 | // linker-synthesized defined symbol, but is only defined if needed. |
Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 135 | template <class ELFT> |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 136 | DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name, |
| 137 | uint8_t Visibility) { |
| 138 | if (!find(Name)) |
| 139 | return nullptr; |
| 140 | return addAbsolute(Name, Visibility); |
Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 143 | // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. |
| 144 | // Used to implement --wrap. |
| 145 | template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) { |
Rui Ueyama | 1b70d66 | 2016-04-28 00:03:38 +0000 | [diff] [blame] | 146 | SymbolBody *B = find(Name); |
| 147 | if (!B) |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 148 | return; |
| 149 | StringSaver Saver(Alloc); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 150 | Symbol *Sym = B->symbol(); |
| 151 | Symbol *Real = addUndefined(Saver.save("__real_" + Name)); |
| 152 | Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name)); |
| 153 | // We rename symbols by replacing the old symbol's SymbolBody with the new |
| 154 | // symbol's SymbolBody. This causes all SymbolBody pointers referring to the |
| 155 | // old symbol to instead refer to the new symbol. |
| 156 | memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body)); |
| 157 | memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body)); |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 160 | static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { |
| 161 | if (VA == STV_DEFAULT) |
| 162 | return VB; |
| 163 | if (VB == STV_DEFAULT) |
| 164 | return VA; |
| 165 | return std::min(VA, VB); |
| 166 | } |
| 167 | |
George Rimar | 4365158 | 2016-06-28 08:21:10 +0000 | [diff] [blame] | 168 | // A symbol version may be included in a symbol name as a prefix after '@'. |
| 169 | // This function parses that part and returns a version ID number. |
| 170 | static uint16_t getVersionId(Symbol *Sym, StringRef Name) { |
| 171 | size_t VersionBegin = Name.find('@'); |
| 172 | if (VersionBegin == StringRef::npos) |
| 173 | return Config->VersionScriptGlobalByDefault ? VER_NDX_GLOBAL |
| 174 | : VER_NDX_LOCAL; |
| 175 | |
| 176 | // If symbol name contains '@' or '@@' we can assign its version id right |
| 177 | // here. '@@' means version by default. It is usually the most recent one. |
| 178 | // VERSYM_HIDDEN flag should be set for all non-default versions. |
| 179 | StringRef Version = Name.drop_front(VersionBegin + 1); |
| 180 | bool Default = Version.startswith("@"); |
| 181 | if (Default) |
| 182 | Version = Version.drop_front(); |
| 183 | |
| 184 | size_t I = 2; |
| 185 | for (elf::Version &V : Config->SymbolVersions) { |
| 186 | if (V.Name == Version) |
| 187 | return Default ? I : (I | VERSYM_HIDDEN); |
| 188 | ++I; |
| 189 | } |
| 190 | error("symbol " + Name + " has undefined version " + Version); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Rui Ueyama | b4de595 | 2016-01-08 22:01:33 +0000 | [diff] [blame] | 194 | // Find an existing symbol or create and insert a new one. |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 195 | template <class ELFT> |
| 196 | std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) { |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 197 | unsigned NumSyms = SymVector.size(); |
| 198 | auto P = Symtab.insert(std::make_pair(Name, NumSyms)); |
| 199 | Symbol *Sym; |
| 200 | if (P.second) { |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 201 | Sym = new (Alloc) Symbol; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 202 | Sym->Binding = STB_WEAK; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 203 | Sym->Visibility = STV_DEFAULT; |
| 204 | Sym->IsUsedInRegularObj = false; |
| 205 | Sym->ExportDynamic = false; |
George Rimar | 4365158 | 2016-06-28 08:21:10 +0000 | [diff] [blame] | 206 | Sym->VersionId = getVersionId(Sym, Name); |
| 207 | Sym->VersionedName = |
| 208 | Sym->VersionId != VER_NDX_LOCAL && Sym->VersionId != VER_NDX_GLOBAL; |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 209 | SymVector.push_back(Sym); |
| 210 | } else { |
| 211 | Sym = SymVector[P.first->second]; |
| 212 | } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 213 | return {Sym, P.second}; |
| 214 | } |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 215 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 216 | // Find an existing symbol or create and insert a new one, then apply the given |
| 217 | // attributes. |
| 218 | template <class ELFT> |
| 219 | std::pair<Symbol *, bool> |
| 220 | SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility, |
| 221 | bool CanOmitFromDynSym, bool IsUsedInRegularObj, |
| 222 | InputFile *File) { |
| 223 | Symbol *S; |
| 224 | bool WasInserted; |
| 225 | std::tie(S, WasInserted) = insert(Name); |
| 226 | |
| 227 | // Merge in the new symbol's visibility. |
| 228 | S->Visibility = getMinVisibility(S->Visibility, Visibility); |
| 229 | if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic)) |
| 230 | S->ExportDynamic = true; |
| 231 | if (IsUsedInRegularObj) |
| 232 | S->IsUsedInRegularObj = true; |
Peter Collingbourne | f3a2b0e | 2016-05-03 18:03:47 +0000 | [diff] [blame] | 233 | if (!WasInserted && S->body()->Type != SymbolBody::UnknownType && |
| 234 | ((Type == STT_TLS) != S->body()->isTls())) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 235 | error("TLS attribute mismatch for symbol: " + |
| 236 | conflictMsg(S->body(), File)); |
| 237 | |
| 238 | return {S, WasInserted}; |
| 239 | } |
| 240 | |
| 241 | // Construct a string in the form of "Sym in File1 and File2". |
| 242 | // Used to construct an error message. |
| 243 | template <typename ELFT> |
| 244 | std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing, |
| 245 | InputFile *NewFile) { |
| 246 | StringRef Sym = Existing->getName(); |
Rui Ueyama | 6d0cd2b | 2016-05-02 21:30:42 +0000 | [diff] [blame] | 247 | return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) + |
| 248 | " and " + getFilename(NewFile); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) { |
| 252 | return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, |
Rafael Espindola | cc70da3 | 2016-06-15 17:56:10 +0000 | [diff] [blame] | 253 | /*CanOmitFromDynSym*/ false, /*File*/ nullptr); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | template <class ELFT> |
| 257 | Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding, |
| 258 | uint8_t StOther, uint8_t Type, |
Rafael Espindola | cc70da3 | 2016-06-15 17:56:10 +0000 | [diff] [blame] | 259 | bool CanOmitFromDynSym, |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 260 | InputFile *File) { |
| 261 | Symbol *S; |
| 262 | bool WasInserted; |
| 263 | std::tie(S, WasInserted) = |
Rafael Espindola | cc70da3 | 2016-06-15 17:56:10 +0000 | [diff] [blame] | 264 | insert(Name, Type, StOther & 3, CanOmitFromDynSym, |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 265 | /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File); |
| 266 | if (WasInserted) { |
| 267 | S->Binding = Binding; |
| 268 | replaceBody<Undefined>(S, Name, StOther, Type); |
Rui Ueyama | 6d0cd2b | 2016-05-02 21:30:42 +0000 | [diff] [blame] | 269 | cast<Undefined>(S->body())->File = File; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 270 | return S; |
| 271 | } |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 272 | if (Binding != STB_WEAK) { |
| 273 | if (S->body()->isShared() || S->body()->isLazy()) |
| 274 | S->Binding = Binding; |
| 275 | if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body())) |
| 276 | SS->File->IsUsed = true; |
| 277 | } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 278 | if (auto *L = dyn_cast<Lazy>(S->body())) { |
| 279 | // An undefined weak will not fetch archive members, but we have to remember |
| 280 | // its type. See also comment in addLazyArchive. |
| 281 | if (S->isWeak()) |
| 282 | L->Type = Type; |
| 283 | else if (auto F = L->getFile()) |
| 284 | addFile(std::move(F)); |
| 285 | } |
| 286 | return S; |
| 287 | } |
| 288 | |
| 289 | // We have a new defined symbol with the specified binding. Return 1 if the new |
| 290 | // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are |
| 291 | // strong defined symbols. |
| 292 | static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) { |
| 293 | if (WasInserted) |
| 294 | return 1; |
| 295 | SymbolBody *Body = S->body(); |
| 296 | if (Body->isLazy() || Body->isUndefined() || Body->isShared()) |
| 297 | return 1; |
| 298 | if (Binding == STB_WEAK) |
| 299 | return -1; |
| 300 | if (S->isWeak()) |
| 301 | return 1; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | // We have a new non-common defined symbol with the specified binding. Return 1 |
| 306 | // if the new symbol should win, -1 if the new symbol should lose, or 0 if there |
| 307 | // is a conflict. If the new symbol wins, also update the binding. |
| 308 | static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) { |
| 309 | if (int Cmp = compareDefined(S, WasInserted, Binding)) { |
| 310 | if (Cmp > 0) |
| 311 | S->Binding = Binding; |
| 312 | return Cmp; |
| 313 | } |
| 314 | if (isa<DefinedCommon>(S->body())) { |
| 315 | // Non-common symbols take precedence over common symbols. |
| 316 | if (Config->WarnCommon) |
| 317 | warning("common " + S->body()->getName() + " is overridden"); |
| 318 | return 1; |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | template <class ELFT> |
| 324 | Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size, |
| 325 | uint64_t Alignment, uint8_t Binding, |
| 326 | uint8_t StOther, uint8_t Type, |
| 327 | InputFile *File) { |
| 328 | Symbol *S; |
| 329 | bool WasInserted; |
| 330 | std::tie(S, WasInserted) = |
| 331 | insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, |
| 332 | /*IsUsedInRegularObj*/ true, File); |
| 333 | int Cmp = compareDefined(S, WasInserted, Binding); |
| 334 | if (Cmp > 0) { |
| 335 | S->Binding = Binding; |
| 336 | replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type); |
| 337 | } else if (Cmp == 0) { |
| 338 | auto *C = dyn_cast<DefinedCommon>(S->body()); |
| 339 | if (!C) { |
| 340 | // Non-common symbols take precedence over common symbols. |
| 341 | if (Config->WarnCommon) |
| 342 | warning("common " + S->body()->getName() + " is overridden"); |
| 343 | return S; |
| 344 | } |
| 345 | |
| 346 | if (Config->WarnCommon) |
| 347 | warning("multiple common of " + S->body()->getName()); |
| 348 | |
| 349 | C->Size = std::max(C->Size, Size); |
| 350 | C->Alignment = std::max(C->Alignment, Alignment); |
| 351 | } |
| 352 | return S; |
| 353 | } |
| 354 | |
| 355 | template <class ELFT> |
| 356 | void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing, |
| 357 | InputFile *NewFile) { |
| 358 | std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile); |
| 359 | if (Config->AllowMultipleDefinition) |
| 360 | warning(Msg); |
| 361 | else |
| 362 | error(Msg); |
| 363 | } |
| 364 | |
| 365 | template <typename ELFT> |
| 366 | Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym, |
| 367 | InputSectionBase<ELFT> *Section) { |
| 368 | Symbol *S; |
| 369 | bool WasInserted; |
| 370 | std::tie(S, WasInserted) = |
| 371 | insert(Name, Sym.getType(), Sym.getVisibility(), |
| 372 | /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true, |
| 373 | Section ? Section->getFile() : nullptr); |
| 374 | int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding()); |
| 375 | if (Cmp > 0) |
| 376 | replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section); |
| 377 | else if (Cmp == 0) |
| 378 | reportDuplicate(S->body(), Section->getFile()); |
| 379 | return S; |
| 380 | } |
| 381 | |
| 382 | template <typename ELFT> |
| 383 | Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding, |
| 384 | uint8_t StOther) { |
| 385 | Symbol *S; |
| 386 | bool WasInserted; |
| 387 | std::tie(S, WasInserted) = |
| 388 | insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false, |
| 389 | /*IsUsedInRegularObj*/ true, nullptr); |
| 390 | int Cmp = compareDefinedNonCommon(S, WasInserted, Binding); |
| 391 | if (Cmp > 0) |
| 392 | replaceBody<DefinedRegular<ELFT>>(S, Name, StOther); |
| 393 | else if (Cmp == 0) |
| 394 | reportDuplicate(S->body(), nullptr); |
| 395 | return S; |
| 396 | } |
| 397 | |
| 398 | template <typename ELFT> |
| 399 | Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N, |
Peter Collingbourne | 6a42259 | 2016-05-03 01:21:08 +0000 | [diff] [blame] | 400 | OutputSectionBase<ELFT> *Section, |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 401 | uintX_t Value) { |
| 402 | Symbol *S; |
| 403 | bool WasInserted; |
| 404 | std::tie(S, WasInserted) = |
| 405 | insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false, |
| 406 | /*IsUsedInRegularObj*/ true, nullptr); |
| 407 | int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL); |
| 408 | if (Cmp > 0) |
| 409 | replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section); |
| 410 | else if (Cmp == 0) |
| 411 | reportDuplicate(S->body(), nullptr); |
| 412 | return S; |
| 413 | } |
| 414 | |
| 415 | template <typename ELFT> |
| 416 | void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name, |
| 417 | const Elf_Sym &Sym, |
| 418 | const typename ELFT::Verdef *Verdef) { |
| 419 | // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT |
| 420 | // as the visibility, which will leave the visibility in the symbol table |
| 421 | // unchanged. |
| 422 | Symbol *S; |
| 423 | bool WasInserted; |
| 424 | std::tie(S, WasInserted) = |
| 425 | insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, |
| 426 | /*IsUsedInRegularObj*/ false, F); |
| 427 | // Make sure we preempt DSO symbols with default visibility. |
| 428 | if (Sym.getVisibility() == STV_DEFAULT) |
| 429 | S->ExportDynamic = true; |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 430 | if (WasInserted || isa<Undefined>(S->body())) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 431 | replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef); |
Peter Collingbourne | ca8c994 | 2016-06-09 18:01:35 +0000 | [diff] [blame] | 432 | if (!S->isWeak()) |
| 433 | F->IsUsed = true; |
| 434 | } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | template <class ELFT> |
| 438 | Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak, |
| 439 | uint8_t StOther, uint8_t Type, |
| 440 | bool CanOmitFromDynSym, BitcodeFile *F) { |
| 441 | Symbol *S; |
| 442 | bool WasInserted; |
| 443 | std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym, |
| 444 | /*IsUsedInRegularObj*/ false, F); |
| 445 | int Cmp = |
| 446 | compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL); |
| 447 | if (Cmp > 0) |
| 448 | replaceBody<DefinedBitcode>(S, Name, StOther, Type, F); |
| 449 | else if (Cmp == 0) |
| 450 | reportDuplicate(S->body(), F); |
| 451 | return S; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 452 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 453 | |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 454 | template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { |
| 455 | auto It = Symtab.find(Name); |
| 456 | if (It == Symtab.end()) |
| 457 | return nullptr; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 458 | return SymVector[It->second]->body(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Rui Ueyama | 48e4251 | 2016-06-29 04:47:39 +0000 | [diff] [blame] | 461 | // Returns a list of defined symbols that match with a given glob pattern. |
Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 462 | template <class ELFT> |
Davide Italiano | 8e1131d | 2016-06-29 02:46:51 +0000 | [diff] [blame] | 463 | std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) { |
| 464 | // Fast-path. Fallback to find() if Pattern doesn't contain any wildcard |
| 465 | // characters. |
Rui Ueyama | 48e4251 | 2016-06-29 04:47:39 +0000 | [diff] [blame] | 466 | if (Pattern.find_first_of("?*") == StringRef::npos) { |
| 467 | if (SymbolBody *B = find(Pattern)) |
| 468 | if (!B->isUndefined()) |
| 469 | return {B}; |
| 470 | return {}; |
| 471 | } |
Davide Italiano | 8e1131d | 2016-06-29 02:46:51 +0000 | [diff] [blame] | 472 | |
Rui Ueyama | 48e4251 | 2016-06-29 04:47:39 +0000 | [diff] [blame] | 473 | std::vector<SymbolBody *> Res; |
| 474 | for (auto &It : Symtab) { |
| 475 | StringRef Name = It.first.Val; |
| 476 | SymbolBody *B = SymVector[It.second]->body(); |
Rui Ueyama | 722830a | 2016-06-29 05:32:09 +0000 | [diff] [blame] | 477 | if (!B->isUndefined() && globMatch(Pattern, Name)) |
Rui Ueyama | 48e4251 | 2016-06-29 04:47:39 +0000 | [diff] [blame] | 478 | Res.push_back(B); |
| 479 | } |
| 480 | return Res; |
Davide Italiano | 8e1131d | 2016-06-29 02:46:51 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | template <class ELFT> |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 484 | void SymbolTable<ELFT>::addLazyArchive( |
| 485 | ArchiveFile *F, const llvm::object::Archive::Symbol Sym) { |
| 486 | Symbol *S; |
| 487 | bool WasInserted; |
| 488 | std::tie(S, WasInserted) = insert(Sym.getName()); |
| 489 | if (WasInserted) { |
Rafael Espindola | 07543a8 | 2016-06-14 21:40:23 +0000 | [diff] [blame] | 490 | replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType); |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 491 | return; |
| 492 | } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 493 | if (!S->body()->isUndefined()) |
| 494 | return; |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 495 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 496 | // Weak undefined symbols should not fetch members from archives. If we were |
| 497 | // to keep old symbol we would not know that an archive member was available |
| 498 | // if a strong undefined symbol shows up afterwards in the link. If a strong |
| 499 | // undefined symbol never shows up, this lazy symbol will get to the end of |
| 500 | // the link and must be treated as the weak undefined one. We already marked |
| 501 | // this symbol as used when we added it to the symbol table, but we also need |
| 502 | // to preserve its type. FIXME: Move the Type field to Symbol. |
| 503 | if (S->isWeak()) { |
Rafael Espindola | 07543a8 | 2016-06-14 21:40:23 +0000 | [diff] [blame] | 504 | replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 505 | return; |
| 506 | } |
| 507 | MemoryBufferRef MBRef = F->getMember(&Sym); |
| 508 | if (!MBRef.getBuffer().empty()) |
| 509 | addFile(createObjectFile(MBRef, F->getName())); |
| 510 | } |
| 511 | |
| 512 | template <class ELFT> |
Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 513 | void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 514 | Symbol *S; |
| 515 | bool WasInserted; |
| 516 | std::tie(S, WasInserted) = insert(Name); |
| 517 | if (WasInserted) { |
Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 518 | replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | if (!S->body()->isUndefined()) |
| 522 | return; |
| 523 | |
| 524 | // See comment for addLazyArchive above. |
Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 525 | if (S->isWeak()) { |
| 526 | replaceBody<LazyObject>(S, Name, Obj, S->body()->Type); |
| 527 | } else { |
| 528 | MemoryBufferRef MBRef = Obj.getBuffer(); |
| 529 | if (!MBRef.getBuffer().empty()) |
| 530 | addFile(createObjectFile(MBRef)); |
| 531 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 532 | } |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 533 | |
Peter Collingbourne | 892d4980 | 2016-04-27 00:05:03 +0000 | [diff] [blame] | 534 | // Process undefined (-u) flags by loading lazy symbols named by those flags. |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 535 | template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() { |
Peter Collingbourne | 892d4980 | 2016-04-27 00:05:03 +0000 | [diff] [blame] | 536 | for (StringRef S : Config->Undefined) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 537 | if (auto *L = dyn_cast_or_null<Lazy>(find(S))) |
| 538 | if (std::unique_ptr<InputFile> File = L->getFile()) |
| 539 | addFile(std::move(File)); |
Peter Collingbourne | 892d4980 | 2016-04-27 00:05:03 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Rui Ueyama | 93bfee5 | 2015-10-13 18:10:33 +0000 | [diff] [blame] | 542 | // This function takes care of the case in which shared libraries depend on |
| 543 | // the user program (not the other way, which is usual). Shared libraries |
| 544 | // may have undefined symbols, expecting that the user program provides |
| 545 | // the definitions for them. An example is BSD's __progname symbol. |
| 546 | // We need to put such symbols to the main program's .dynsym so that |
| 547 | // shared libraries can find them. |
| 548 | // Except this, we ignore undefined symbols in DSOs. |
| 549 | template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 550 | for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles) |
| 551 | for (StringRef U : File->getUndefinedSymbols()) |
| 552 | if (SymbolBody *Sym = find(U)) |
| 553 | if (Sym->isDefined()) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 554 | Sym->symbol()->ExportDynamic = true; |
Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 557 | // This function process the dynamic list option by marking all the symbols |
| 558 | // to be exported in the dynamic table. |
| 559 | template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() { |
| 560 | for (StringRef S : Config->DynamicList) |
| 561 | if (SymbolBody *B = find(S)) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 562 | B->symbol()->ExportDynamic = true; |
Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 565 | // This function processes the --version-script option by marking all global |
| 566 | // symbols with the VersionScriptGlobal flag, which acts as a filter on the |
| 567 | // dynamic symbol table. |
| 568 | template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() { |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 569 | // If version script does not contain versions declarations, |
| 570 | // we just should mark global symbols. |
| 571 | if (!Config->VersionScriptGlobals.empty()) { |
| 572 | for (StringRef S : Config->VersionScriptGlobals) |
| 573 | if (SymbolBody *B = find(S)) |
| 574 | B->symbol()->VersionId = VER_NDX_GLOBAL; |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | // If we have symbols version declarations, we should |
| 579 | // assign version references for each symbol. |
| 580 | size_t I = 2; |
| 581 | for (Version &V : Config->SymbolVersions) { |
George Rimar | 36b2c0a | 2016-06-28 08:07:26 +0000 | [diff] [blame] | 582 | for (StringRef Name : V.Globals) { |
Rui Ueyama | 48e4251 | 2016-06-29 04:47:39 +0000 | [diff] [blame] | 583 | std::vector<SymbolBody *> Syms = findAll(Name); |
| 584 | if (Syms.empty()) { |
| 585 | if (Config->NoUndefinedVersion) |
| 586 | error("version script assignment of " + V.Name + " to symbol " + |
| 587 | Name + " failed: symbol not defined"); |
| 588 | continue; |
| 589 | } |
George Rimar | 36b2c0a | 2016-06-28 08:07:26 +0000 | [diff] [blame] | 590 | |
Rui Ueyama | 48e4251 | 2016-06-29 04:47:39 +0000 | [diff] [blame] | 591 | for (SymbolBody *B : Syms) { |
Davide Italiano | 8e1131d | 2016-06-29 02:46:51 +0000 | [diff] [blame] | 592 | if (B->symbol()->VersionId != VER_NDX_GLOBAL && |
| 593 | B->symbol()->VersionId != VER_NDX_LOCAL) |
| 594 | warning("duplicate symbol " + Name + " in version script"); |
| 595 | B->symbol()->VersionId = I; |
| 596 | } |
George Rimar | 36b2c0a | 2016-06-28 08:07:26 +0000 | [diff] [blame] | 597 | } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 598 | ++I; |
| 599 | } |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Rui Ueyama | d60dae8a | 2016-06-23 07:00:17 +0000 | [diff] [blame] | 602 | // Print the module names which define the notified |
| 603 | // symbols provided through -y or --trace-symbol option. |
| 604 | template <class ELFT> void SymbolTable<ELFT>::traceDefined() { |
| 605 | for (const auto &Symbol : Config->TraceSymbol) |
| 606 | if (SymbolBody *B = find(Symbol.getKey())) |
| 607 | if (B->isDefined() || B->isCommon()) |
| 608 | if (InputFile *File = B->getSourceFile<ELFT>()) |
| 609 | outs() << getFilename(File) << ": definition of " |
| 610 | << B->getName() << "\n"; |
| 611 | } |
| 612 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 613 | template class elf::SymbolTable<ELF32LE>; |
| 614 | template class elf::SymbolTable<ELF32BE>; |
| 615 | template class elf::SymbolTable<ELF64LE>; |
| 616 | template class elf::SymbolTable<ELF64BE>; |