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