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