Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 1 | //===- Symbols.cpp --------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Symbols.h" |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 | [diff] [blame] | 10 | #include "InputFiles.h" |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 11 | #include "InputSection.h" |
| 12 | #include "OutputSections.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 | [diff] [blame] | 13 | #include "SyntheticSections.h" |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 14 | #include "Target.h" |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 | [diff] [blame] | 15 | #include "Writer.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 16 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 | [diff] [blame] | 17 | #include "lld/Common/Strings.h" |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Fangrui Song | 2325788 | 2020-04-05 04:31:36 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Eugene Leviant | c958d8d | 2016-10-12 08:19:30 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
Rui Ueyama | c72ba3a | 2016-11-23 04:57:25 | [diff] [blame] | 21 | #include <cstring> |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 24 | using namespace llvm::object; |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 | [diff] [blame] | 25 | using namespace llvm::ELF; |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 26 | using namespace lld; |
| 27 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 28 | |
Fangrui Song | bd8cfe6 | 2019-10-07 08:31:18 | [diff] [blame] | 29 | // Returns a symbol for an error message. |
| 30 | static std::string demangle(StringRef symName) { |
| 31 | if (elf::config->demangle) |
| 32 | return demangleItanium(symName); |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 33 | return std::string(symName); |
Fangrui Song | bd8cfe6 | 2019-10-07 08:31:18 | [diff] [blame] | 34 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 35 | |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 36 | std::string lld::toString(const elf::Symbol &sym) { |
Fangrui Song | f2036a1 | 2020-03-28 22:48:38 | [diff] [blame] | 37 | StringRef name = sym.getName(); |
| 38 | std::string ret = demangle(name); |
| 39 | |
Fangrui Song | 941e933 | 2020-12-01 16:54:01 | [diff] [blame^] | 40 | const char *suffix = sym.getVersionSuffix(); |
| 41 | if (*suffix == '@') |
| 42 | ret += suffix; |
Fangrui Song | f2036a1 | 2020-03-28 22:48:38 | [diff] [blame] | 43 | return ret; |
| 44 | } |
| 45 | |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 46 | std::string lld::toELFString(const Archive::Symbol &b) { |
Fangrui Song | bd8cfe6 | 2019-10-07 08:31:18 | [diff] [blame] | 47 | return demangle(b.getName()); |
| 48 | } |
| 49 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 50 | Defined *ElfSym::bss; |
| 51 | Defined *ElfSym::etext1; |
| 52 | Defined *ElfSym::etext2; |
| 53 | Defined *ElfSym::edata1; |
| 54 | Defined *ElfSym::edata2; |
| 55 | Defined *ElfSym::end1; |
| 56 | Defined *ElfSym::end2; |
| 57 | Defined *ElfSym::globalOffsetTable; |
| 58 | Defined *ElfSym::mipsGp; |
| 59 | Defined *ElfSym::mipsGpDisp; |
| 60 | Defined *ElfSym::mipsLocalGp; |
| 61 | Defined *ElfSym::relaIpltStart; |
| 62 | Defined *ElfSym::relaIpltEnd; |
| 63 | Defined *ElfSym::riscvGlobalPointer; |
| 64 | Defined *ElfSym::tlsModuleBase; |
Fangrui Song | ce3c5da | 2020-10-22 22:26:52 | [diff] [blame] | 65 | DenseMap<const Symbol *, std::pair<const InputFile *, const InputFile *>> |
| 66 | elf::backwardReferences; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 | [diff] [blame] | 67 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 68 | static uint64_t getSymVA(const Symbol &sym, int64_t &addend) { |
| 69 | switch (sym.kind()) { |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 70 | case Symbol::DefinedKind: { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 71 | auto &d = cast<Defined>(sym); |
| 72 | SectionBase *isec = d.section; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 73 | |
| 74 | // This is an absolute symbol. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 75 | if (!isec) |
| 76 | return d.value; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 77 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 78 | assert(isec != &InputSection::discarded); |
| 79 | isec = isec->repl; |
Rafael Espindola | f4d6e8c | 2018-04-19 17:26:50 | [diff] [blame] | 80 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 81 | uint64_t offset = d.value; |
Sean Silva | a9ba450 | 2017-02-28 08:32:56 | [diff] [blame] | 82 | |
| 83 | // An object in an SHF_MERGE section might be referenced via a |
| 84 | // section symbol (as a hack for reducing the number of local |
| 85 | // symbols). |
Sean Silva | d4e6062 | 2017-03-01 04:44:04 | [diff] [blame] | 86 | // Depending on the addend, the reference via a section symbol |
| 87 | // refers to a different object in the merge section. |
| 88 | // Since the objects in the merge section are not necessarily |
| 89 | // contiguous in the output, the addend can thus affect the final |
| 90 | // VA in a non-linear way. |
| 91 | // To make this work, we incorporate the addend into the section |
| 92 | // offset (and zero out the addend for later processing) so that |
| 93 | // we find the right object in the section. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 94 | if (d.isSection()) { |
| 95 | offset += addend; |
| 96 | addend = 0; |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 | [diff] [blame] | 97 | } |
Sean Silva | a9ba450 | 2017-02-28 08:32:56 | [diff] [blame] | 98 | |
Sean Silva | 6ab3926 | 2017-02-28 09:01:58 | [diff] [blame] | 99 | // In the typical case, this is actually very simple and boils |
| 100 | // down to adding together 3 numbers: |
| 101 | // 1. The address of the output section. |
| 102 | // 2. The offset of the input section within the output section. |
| 103 | // 3. The offset within the input section (this addition happens |
| 104 | // inside InputSection::getOffset). |
| 105 | // |
| 106 | // If you understand the data structures involved with this next |
| 107 | // line (and how they get built), then you have a pretty good |
| 108 | // understanding of the linker. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 109 | uint64_t va = isec->getVA(offset); |
Sean Silva | 6ab3926 | 2017-02-28 09:01:58 | [diff] [blame] | 110 | |
Simon Atanasyan | fae2a509 | 2019-02-19 10:36:58 | [diff] [blame] | 111 | // MIPS relocatable files can mix regular and microMIPS code. |
| 112 | // Linker needs to distinguish such code. To do so microMIPS |
| 113 | // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other` |
Fangrui Song | deb5819 | 2020-01-23 05:39:16 | [diff] [blame] | 114 | // field. Unfortunately, the `MIPS::relocate()` method has |
Simon Atanasyan | fae2a509 | 2019-02-19 10:36:58 | [diff] [blame] | 115 | // a symbol value only. To pass type of the symbol (regular/microMIPS) |
| 116 | // to that routine as well as other places where we write |
| 117 | // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry` |
| 118 | // field etc) do the same trick as compiler uses to mark microMIPS |
| 119 | // for CPU - set the less-significant bit. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 120 | if (config->emachine == EM_MIPS && isMicroMips() && |
| 121 | ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsPltAddr)) |
| 122 | va |= 1; |
Simon Atanasyan | fae2a509 | 2019-02-19 10:36:58 | [diff] [blame] | 123 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 124 | if (d.isTls() && !config->relocatable) { |
Ryan Prichard | 1c33d14 | 2018-09-18 00:24:48 | [diff] [blame] | 125 | // Use the address of the TLS segment's first section rather than the |
| 126 | // segment's address, because segment addresses aren't initialized until |
| 127 | // after sections are finalized. (e.g. Measuring the size of .rela.dyn |
| 128 | // for Android relocation packing requires knowing TLS symbol addresses |
| 129 | // during section finalization.) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 130 | if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec) |
| 131 | fatal(toString(d.file) + |
Peter Collingbourne | 3e2abde | 2017-07-14 00:22:46 | [diff] [blame] | 132 | " has an STT_TLS symbol but doesn't have an SHF_TLS section"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 133 | return va - Out::tlsPhdr->firstSec->addr; |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 | [diff] [blame] | 134 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 135 | return va; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 136 | } |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 | [diff] [blame] | 137 | case Symbol::SharedKind: |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 138 | case Symbol::UndefinedKind: |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 139 | return 0; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 140 | case Symbol::LazyArchiveKind: |
| 141 | case Symbol::LazyObjectKind: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 142 | assert(sym.isUsedInRegularObj && "lazy symbol reached writer"); |
Chih-Hung Hsieh | 73e0484 | 2018-09-11 23:00:36 | [diff] [blame] | 143 | return 0; |
Rui Ueyama | 5c073a9 | 2019-05-16 03:29:03 | [diff] [blame] | 144 | case Symbol::CommonKind: |
| 145 | llvm_unreachable("common symbol reached writer"); |
Rui Ueyama | f3fad55 | 2018-10-12 18:29:18 | [diff] [blame] | 146 | case Symbol::PlaceholderKind: |
| 147 | llvm_unreachable("placeholder symbol reached writer"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 148 | } |
George Rimar | 777f963 | 2016-03-12 08:31:34 | [diff] [blame] | 149 | llvm_unreachable("invalid symbol kind"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 150 | } |
| 151 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 152 | uint64_t Symbol::getVA(int64_t addend) const { |
| 153 | uint64_t outVA = getSymVA(*this, addend); |
| 154 | return outVA + addend; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 | [diff] [blame] | 155 | } |
| 156 | |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 157 | uint64_t Symbol::getGotVA() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 158 | if (gotInIgot) |
| 159 | return in.igotPlt->getVA() + getGotPltOffset(); |
| 160 | return in.got->getVA() + getGotOffset(); |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 161 | } |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 | [diff] [blame] | 162 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 163 | uint64_t Symbol::getGotOffset() const { return gotIndex * config->wordsize; } |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 164 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 165 | uint64_t Symbol::getGotPltVA() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 166 | if (isInIplt) |
| 167 | return in.igotPlt->getVA() + getGotPltOffset(); |
| 168 | return in.gotPlt->getVA() + getGotPltOffset(); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 | [diff] [blame] | 169 | } |
| 170 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 171 | uint64_t Symbol::getGotPltOffset() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 172 | if (isInIplt) |
| 173 | return pltIndex * config->wordsize; |
| 174 | return (pltIndex + target->gotPltHeaderEntriesNum) * config->wordsize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 175 | } |
| 176 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 177 | uint64_t Symbol::getPltVA() const { |
Fangrui Song | 891a865 | 2019-12-14 22:17:35 | [diff] [blame] | 178 | uint64_t outVA = isInIplt |
| 179 | ? in.iplt->getVA() + pltIndex * target->ipltEntrySize |
| 180 | : in.plt->getVA() + in.plt->headerSize + |
| 181 | pltIndex * target->pltEntrySize; |
| 182 | |
Simon Atanasyan | fae2a509 | 2019-02-19 10:36:58 | [diff] [blame] | 183 | // While linking microMIPS code PLT code are always microMIPS |
| 184 | // code. Set the less-significant bit to track that fact. |
| 185 | // See detailed comment in the `getSymVA` function. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 186 | if (config->emachine == EM_MIPS && isMicroMips()) |
| 187 | outVA |= 1; |
| 188 | return outVA; |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 | [diff] [blame] | 189 | } |
| 190 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 191 | uint64_t Symbol::getSize() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 192 | if (const auto *dr = dyn_cast<Defined>(this)) |
| 193 | return dr->size; |
| 194 | return cast<SharedSymbol>(this)->size; |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 | [diff] [blame] | 195 | } |
| 196 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 197 | OutputSection *Symbol::getOutputSection() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 198 | if (auto *s = dyn_cast<Defined>(this)) { |
| 199 | if (auto *sec = s->section) |
| 200 | return sec->repl->getOutputSection(); |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 | [diff] [blame] | 201 | return nullptr; |
| 202 | } |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 | [diff] [blame] | 203 | return nullptr; |
| 204 | } |
| 205 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 206 | // If a symbol name contains '@', the characters after that is |
| 207 | // a symbol version name. This function parses that. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 208 | void Symbol::parseSymbolVersion() { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 209 | StringRef s = getName(); |
| 210 | size_t pos = s.find('@'); |
| 211 | if (pos == 0 || pos == StringRef::npos) |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 212 | return; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 213 | StringRef verstr = s.substr(pos + 1); |
| 214 | if (verstr.empty()) |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 215 | return; |
| 216 | |
| 217 | // Truncate the symbol name so that it doesn't include the version string. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 218 | nameSize = pos; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 219 | |
Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 | [diff] [blame] | 220 | // If this is not in this DSO, it is not a definition. |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 | [diff] [blame] | 221 | if (!isDefined()) |
Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 | [diff] [blame] | 222 | return; |
| 223 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 224 | // '@@' in a symbol name means the default version. |
| 225 | // It is usually the most recent one. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 226 | bool isDefault = (verstr[0] == '@'); |
| 227 | if (isDefault) |
| 228 | verstr = verstr.substr(1); |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 229 | |
Fangrui Song | e28a70d | 2019-08-05 14:31:39 | [diff] [blame] | 230 | for (const VersionDefinition &ver : namedVersionDefs()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 231 | if (ver.name != verstr) |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 232 | continue; |
| 233 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 234 | if (isDefault) |
| 235 | versionId = ver.id; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 236 | else |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 237 | versionId = ver.id | VERSYM_HIDDEN; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
| 241 | // It is an error if the specified version is not defined. |
George Rimar | 4d2f9762 | 2017-07-04 13:19:13 | [diff] [blame] | 242 | // Usually version script is not provided when linking executable, |
| 243 | // but we may still want to override a versioned symbol from DSO, |
Peter Smith | 796fb99 | 2018-05-14 10:13:56 | [diff] [blame] | 244 | // so we do not report error in this case. We also do not error |
| 245 | // if the symbol has a local version as it won't be in the dynamic |
| 246 | // symbol table. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 247 | if (config->shared && versionId != VER_NDX_LOCAL) |
| 248 | error(toString(file) + ": symbol " + s + " has undefined version " + |
| 249 | verstr); |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 250 | } |
| 251 | |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 252 | void Symbol::fetch() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 253 | if (auto *sym = dyn_cast<LazyArchive>(this)) { |
| 254 | cast<ArchiveFile>(sym->file)->fetch(sym->sym); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 255 | return; |
| 256 | } |
| 257 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 258 | if (auto *sym = dyn_cast<LazyObject>(this)) { |
| 259 | dyn_cast<LazyObjFile>(sym->file)->fetch(); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
| 263 | llvm_unreachable("Symbol::fetch() is called on a non-lazy symbol"); |
Rui Ueyama | 7d47619 | 2019-05-16 02:14:00 | [diff] [blame] | 264 | } |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 265 | |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 266 | MemoryBufferRef LazyArchive::getMemberBuffer() { |
Nico Weber | 9c0716f | 2019-07-23 19:00:01 | [diff] [blame] | 267 | Archive::Child c = |
| 268 | CHECK(sym.getMember(), |
| 269 | "could not get the member for symbol " + toELFString(sym)); |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 270 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 271 | return CHECK(c.getMemoryBufferRef(), |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 272 | "could not get the buffer for the member defining symbol " + |
Nico Weber | 9c0716f | 2019-07-23 19:00:01 | [diff] [blame] | 273 | toELFString(sym)); |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 274 | } |
| 275 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 276 | uint8_t Symbol::computeBinding() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 277 | if (config->relocatable) |
| 278 | return binding; |
Fangrui Song | cfdd458 | 2019-08-11 17:03:00 | [diff] [blame] | 279 | if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) || |
Fangrui Song | c1c679e | 2020-04-01 23:11:31 | [diff] [blame] | 280 | (versionId == VER_NDX_LOCAL && isDefined())) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 281 | return STB_LOCAL; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 282 | if (!config->gnuUnique && binding == STB_GNU_UNIQUE) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 283 | return STB_GLOBAL; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 284 | return binding; |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 285 | } |
| 286 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 287 | bool Symbol::includeInDynsym() const { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 288 | if (!config->hasDynSymTab) |
Rafael Espindola | e05e2f8 | 2017-09-15 18:05:02 | [diff] [blame] | 289 | return false; |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 290 | if (computeBinding() == STB_LOCAL) |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 | [diff] [blame] | 291 | return false; |
Fangrui Song | 375371c | 2020-01-09 23:53:52 | [diff] [blame] | 292 | if (!isDefined() && !isCommon()) |
Fangrui Song | 0fbf28f | 2020-01-23 19:52:03 | [diff] [blame] | 293 | // This should unconditionally return true, unfortunately glibc -static-pie |
| 294 | // expects undefined weak symbols not to exist in .dynsym, e.g. |
| 295 | // __pthread_mutex_lock reference in _dl_add_to_namespace_list, |
| 296 | // __pthread_initialize_minimal reference in csu/libc-start.c. |
| 297 | return !(config->noDynamicLinker && isUndefWeak()); |
Rui Ueyama | e63ae7f | 2019-06-25 06:58:07 | [diff] [blame] | 298 | |
Fangrui Song | 375371c | 2020-01-09 23:53:52 | [diff] [blame] | 299 | return exportDynamic || inDynamicList; |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 | [diff] [blame] | 300 | } |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 301 | |
| 302 | // Print out a log message for --trace-symbol. |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 303 | void elf::printTraceSymbol(const Symbol *sym) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 304 | std::string s; |
| 305 | if (sym->isUndefined()) |
| 306 | s = ": reference to "; |
| 307 | else if (sym->isLazy()) |
| 308 | s = ": lazy definition of "; |
| 309 | else if (sym->isShared()) |
| 310 | s = ": shared definition of "; |
| 311 | else if (sym->isCommon()) |
| 312 | s = ": common definition of "; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 313 | else |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 314 | s = ": definition of "; |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 | [diff] [blame] | 315 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 316 | message(toString(sym->file) + s + sym->getName()); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 317 | } |
| 318 | |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 319 | void elf::maybeWarnUnorderableSymbol(const Symbol *sym) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 320 | if (!config->warnSymbolOrdering) |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 321 | return; |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 322 | |
Fangrui Song | 11ca54f | 2018-10-10 22:48:57 | [diff] [blame] | 323 | // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning |
| 324 | // is emitted. It makes sense to not warn on undefined symbols. |
| 325 | // |
| 326 | // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols, |
| 327 | // but we don't have to be compatible here. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 328 | if (sym->isUndefined() && |
| 329 | config->unresolvedSymbols == UnresolvedPolicy::Ignore) |
Fangrui Song | 11ca54f | 2018-10-10 22:48:57 | [diff] [blame] | 330 | return; |
| 331 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 332 | const InputFile *file = sym->file; |
| 333 | auto *d = dyn_cast<Defined>(sym); |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 334 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 335 | auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); }; |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 336 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 337 | if (sym->isUndefined()) |
| 338 | report(": unable to order undefined symbol: "); |
| 339 | else if (sym->isShared()) |
| 340 | report(": unable to order shared symbol: "); |
| 341 | else if (d && !d->section) |
| 342 | report(": unable to order absolute symbol: "); |
| 343 | else if (d && isa<OutputSection>(d->section)) |
| 344 | report(": unable to order synthetic symbol: "); |
| 345 | else if (d && !d->section->repl->isLive()) |
| 346 | report(": unable to order discarded symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 347 | } |
| 348 | |
Fangrui Song | cd0ab24 | 2019-11-30 05:58:36 | [diff] [blame] | 349 | // Returns true if a symbol can be replaced at load-time by a symbol |
| 350 | // with the same name defined in other ELF executable or DSO. |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 351 | bool elf::computeIsPreemptible(const Symbol &sym) { |
Fangrui Song | cd0ab24 | 2019-11-30 05:58:36 | [diff] [blame] | 352 | assert(!sym.isLocal()); |
| 353 | |
| 354 | // Only symbols with default visibility that appear in dynsym can be |
| 355 | // preempted. Symbols with protected visibility cannot be preempted. |
| 356 | if (!sym.includeInDynsym() || sym.visibility != STV_DEFAULT) |
| 357 | return false; |
| 358 | |
| 359 | // At this point copy relocations have not been created yet, so any |
| 360 | // symbol that is not defined locally is preemptible. |
| 361 | if (!sym.isDefined()) |
| 362 | return true; |
| 363 | |
| 364 | if (!config->shared) |
| 365 | return false; |
| 366 | |
Fangrui Song | 751f18e | 2020-06-01 18:27:53 | [diff] [blame] | 367 | // If -Bsymbolic or --dynamic-list is specified, or -Bsymbolic-functions is |
| 368 | // specified and the symbol is STT_FUNC, the symbol is preemptible iff it is |
| 369 | // in the dynamic list. |
| 370 | if (config->symbolic || (config->bsymbolicFunctions && sym.isFunc())) |
Fangrui Song | cd0ab24 | 2019-11-30 05:58:36 | [diff] [blame] | 371 | return sym.inDynamicList; |
Fangrui Song | cd0ab24 | 2019-11-30 05:58:36 | [diff] [blame] | 372 | return true; |
| 373 | } |
| 374 | |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 375 | void elf::reportBackrefs() { |
Fangrui Song | 03c825c | 2020-04-06 05:27:46 | [diff] [blame] | 376 | for (auto &it : backwardReferences) { |
| 377 | const Symbol &sym = *it.first; |
Fangrui Song | 3ba33422 | 2020-11-08 04:17:41 | [diff] [blame] | 378 | std::string to = toString(it.second.second); |
| 379 | // Some libraries have known problems and can cause noise. Filter them out |
| 380 | // with --warn-backrefs-exclude=. to may look like *.o or *.a(*.o). |
| 381 | bool exclude = false; |
| 382 | for (const llvm::GlobPattern &pat : config->warnBackrefsExclude) |
| 383 | if (pat.match(to)) { |
| 384 | exclude = true; |
| 385 | break; |
| 386 | } |
| 387 | if (!exclude) |
| 388 | warn("backward reference detected: " + sym.getName() + " in " + |
| 389 | toString(it.second.first) + " refers to " + to); |
Fangrui Song | 03c825c | 2020-04-06 05:27:46 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 393 | static uint8_t getMinVisibility(uint8_t va, uint8_t vb) { |
| 394 | if (va == STV_DEFAULT) |
| 395 | return vb; |
| 396 | if (vb == STV_DEFAULT) |
| 397 | return va; |
| 398 | return std::min(va, vb); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Merge symbol properties. |
| 402 | // |
| 403 | // When we have many symbols of the same name, we choose one of them, |
| 404 | // and that's the result of symbol resolution. However, symbols that |
| 405 | // were not chosen still affect some symbol properties. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 406 | void Symbol::mergeProperties(const Symbol &other) { |
| 407 | if (other.exportDynamic) |
| 408 | exportDynamic = true; |
| 409 | if (other.isUsedInRegularObj) |
| 410 | isUsedInRegularObj = true; |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 411 | |
| 412 | // DSO symbols do not affect visibility in the output. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 413 | if (!other.isShared()) |
| 414 | visibility = getMinVisibility(visibility, other.visibility); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 415 | } |
| 416 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 417 | void Symbol::resolve(const Symbol &other) { |
| 418 | mergeProperties(other); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 419 | |
| 420 | if (isPlaceholder()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 421 | replace(other); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 425 | switch (other.kind()) { |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 426 | case Symbol::UndefinedKind: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 427 | resolveUndefined(cast<Undefined>(other)); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 428 | break; |
| 429 | case Symbol::CommonKind: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 430 | resolveCommon(cast<CommonSymbol>(other)); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 431 | break; |
| 432 | case Symbol::DefinedKind: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 433 | resolveDefined(cast<Defined>(other)); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 434 | break; |
| 435 | case Symbol::LazyArchiveKind: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 436 | resolveLazy(cast<LazyArchive>(other)); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 437 | break; |
| 438 | case Symbol::LazyObjectKind: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 439 | resolveLazy(cast<LazyObject>(other)); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 440 | break; |
| 441 | case Symbol::SharedKind: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 442 | resolveShared(cast<SharedSymbol>(other)); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 443 | break; |
| 444 | case Symbol::PlaceholderKind: |
| 445 | llvm_unreachable("bad symbol kind"); |
| 446 | } |
| 447 | } |
| 448 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 449 | void Symbol::resolveUndefined(const Undefined &other) { |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 450 | // An undefined symbol with non default visibility must be satisfied |
| 451 | // in the same DSO. |
| 452 | // |
| 453 | // If this is a non-weak defined symbol in a discarded section, override the |
| 454 | // existing undefined symbol for better error message later. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 455 | if ((isShared() && other.visibility != STV_DEFAULT) || |
| 456 | (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) { |
| 457 | replace(other); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 458 | return; |
| 459 | } |
| 460 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 461 | if (traced) |
| 462 | printTraceSymbol(&other); |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 | [diff] [blame] | 463 | |
Fangrui Song | f347541 | 2019-07-04 10:38:04 | [diff] [blame] | 464 | if (isLazy()) { |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 465 | // An undefined weak will not fetch archive members. See comment on Lazy in |
| 466 | // Symbols.h for the details. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 467 | if (other.binding == STB_WEAK) { |
| 468 | binding = STB_WEAK; |
| 469 | type = other.type; |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
| 473 | // Do extra check for --warn-backrefs. |
| 474 | // |
| 475 | // --warn-backrefs is an option to prevent an undefined reference from |
| 476 | // fetching an archive member written earlier in the command line. It can be |
| 477 | // used to keep compatibility with GNU linkers to some degree. |
| 478 | // I'll explain the feature and why you may find it useful in this comment. |
| 479 | // |
| 480 | // lld's symbol resolution semantics is more relaxed than traditional Unix |
| 481 | // linkers. For example, |
| 482 | // |
| 483 | // ld.lld foo.a bar.o |
| 484 | // |
| 485 | // succeeds even if bar.o contains an undefined symbol that has to be |
| 486 | // resolved by some object file in foo.a. Traditional Unix linkers don't |
| 487 | // allow this kind of backward reference, as they visit each file only once |
| 488 | // from left to right in the command line while resolving all undefined |
| 489 | // symbols at the moment of visiting. |
| 490 | // |
| 491 | // In the above case, since there's no undefined symbol when a linker visits |
| 492 | // foo.a, no files are pulled out from foo.a, and because the linker forgets |
| 493 | // about foo.a after visiting, it can't resolve undefined symbols in bar.o |
| 494 | // that could have been resolved otherwise. |
| 495 | // |
| 496 | // That lld accepts more relaxed form means that (besides it'd make more |
| 497 | // sense) you can accidentally write a command line or a build file that |
| 498 | // works only with lld, even if you have a plan to distribute it to wider |
| 499 | // users who may be using GNU linkers. With --warn-backrefs, you can detect |
| 500 | // a library order that doesn't work with other Unix linkers. |
| 501 | // |
| 502 | // The option is also useful to detect cyclic dependencies between static |
| 503 | // archives. Again, lld accepts |
| 504 | // |
| 505 | // ld.lld foo.a bar.a |
| 506 | // |
| 507 | // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is |
| 508 | // handled as an error. |
| 509 | // |
| 510 | // Here is how the option works. We assign a group ID to each file. A file |
| 511 | // with a smaller group ID can pull out object files from an archive file |
| 512 | // with an equal or greater group ID. Otherwise, it is a reverse dependency |
| 513 | // and an error. |
| 514 | // |
| 515 | // A file outside --{start,end}-group gets a fresh ID when instantiated. All |
| 516 | // files within the same --{start,end}-group get the same group ID. E.g. |
| 517 | // |
| 518 | // ld.lld A B --start-group C D --end-group E |
| 519 | // |
| 520 | // A forms group 0. B form group 1. C and D (including their member object |
| 521 | // files) form group 2. E forms group 3. I think that you can see how this |
| 522 | // group assignment rule simulates the traditional linker's semantics. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 523 | bool backref = config->warnBackrefs && other.file && |
| 524 | file->groupId < other.file->groupId; |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 525 | fetch(); |
| 526 | |
| 527 | // We don't report backward references to weak symbols as they can be |
| 528 | // overridden later. |
Fangrui Song | 03c825c | 2020-04-06 05:27:46 | [diff] [blame] | 529 | // |
| 530 | // A traditional linker does not error for -ldef1 -lref -ldef2 (linking |
| 531 | // sandwich), where def2 may or may not be the same as def1. We don't want |
| 532 | // to warn for this case, so dismiss the warning if we see a subsequent lazy |
Fangrui Song | ce3c5da | 2020-10-22 22:26:52 | [diff] [blame] | 533 | // definition. this->file needs to be saved because in the case of LTO it |
| 534 | // may be reset to nullptr or be replaced with a file named lto.tmp. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 535 | if (backref && !isWeak()) |
Fangrui Song | ce3c5da | 2020-10-22 22:26:52 | [diff] [blame] | 536 | backwardReferences.try_emplace(this, std::make_pair(other.file, file)); |
Fangrui Song | f347541 | 2019-07-04 10:38:04 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
| 540 | // Undefined symbols in a SharedFile do not change the binding. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 541 | if (dyn_cast_or_null<SharedFile>(other.file)) |
Fangrui Song | f347541 | 2019-07-04 10:38:04 | [diff] [blame] | 542 | return; |
| 543 | |
Fangrui Song | e49c417 | 2019-08-06 14:03:45 | [diff] [blame] | 544 | if (isUndefined() || isShared()) { |
| 545 | // The binding will be weak if there is at least one reference and all are |
| 546 | // weak. The binding has one opportunity to change to weak: if the first |
| 547 | // reference is weak. |
| 548 | if (other.binding != STB_WEAK || !referenced) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 549 | binding = other.binding; |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and |
| 554 | // foo@@VER. We want to effectively ignore foo, so give precedence to |
| 555 | // foo@@VER. |
| 556 | // FIXME: If users can transition to using |
| 557 | // .symver foo,foo@@@VER |
| 558 | // we can delete this hack. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 559 | static int compareVersion(StringRef a, StringRef b) { |
| 560 | bool x = a.contains("@@"); |
| 561 | bool y = b.contains("@@"); |
| 562 | if (!x && y) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 563 | return 1; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 564 | if (x && !y) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 565 | return -1; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | // Compare two symbols. Return 1 if the new symbol should win, -1 if |
| 570 | // the new symbol should lose, or 0 if there is a conflict. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 571 | int Symbol::compare(const Symbol *other) const { |
| 572 | assert(other->isDefined() || other->isCommon()); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 573 | |
| 574 | if (!isDefined() && !isCommon()) |
| 575 | return 1; |
| 576 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 577 | if (int cmp = compareVersion(getName(), other->getName())) |
| 578 | return cmp; |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 579 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 580 | if (other->isWeak()) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 581 | return -1; |
| 582 | |
| 583 | if (isWeak()) |
| 584 | return 1; |
| 585 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 586 | if (isCommon() && other->isCommon()) { |
| 587 | if (config->warnCommon) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 588 | warn("multiple common of " + getName()); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | if (isCommon()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 593 | if (config->warnCommon) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 594 | warn("common " + getName() + " is overridden"); |
| 595 | return 1; |
| 596 | } |
| 597 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 598 | if (other->isCommon()) { |
| 599 | if (config->warnCommon) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 600 | warn("common " + getName() + " is overridden"); |
| 601 | return -1; |
| 602 | } |
| 603 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 604 | auto *oldSym = cast<Defined>(this); |
| 605 | auto *newSym = cast<Defined>(other); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 606 | |
Fangrui Song | d6c4482 | 2019-07-26 16:29:15 | [diff] [blame] | 607 | if (dyn_cast_or_null<BitcodeFile>(other->file)) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 608 | return 0; |
| 609 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 610 | if (!oldSym->section && !newSym->section && oldSym->value == newSym->value && |
| 611 | newSym->binding == STB_GLOBAL) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 612 | return -1; |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 617 | static void reportDuplicate(Symbol *sym, InputFile *newFile, |
| 618 | InputSectionBase *errSec, uint64_t errOffset) { |
| 619 | if (config->allowMultipleDefinition) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 620 | return; |
| 621 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 622 | Defined *d = cast<Defined>(sym); |
| 623 | if (!d->section || !errSec) { |
| 624 | error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " + |
| 625 | toString(sym->file) + "\n>>> defined in " + toString(newFile)); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | |
| 629 | // Construct and print an error message in the form of: |
| 630 | // |
| 631 | // ld.lld: error: duplicate symbol: foo |
| 632 | // >>> defined at bar.c:30 |
| 633 | // >>> bar.o (/home/alice/src/bar.o) |
| 634 | // >>> defined at baz.c:563 |
| 635 | // >>> baz.o in archive libbaz.a |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 636 | auto *sec1 = cast<InputSectionBase>(d->section); |
| 637 | std::string src1 = sec1->getSrcMsg(*sym, d->value); |
| 638 | std::string obj1 = sec1->getObjMsg(d->value); |
| 639 | std::string src2 = errSec->getSrcMsg(*sym, errOffset); |
| 640 | std::string obj2 = errSec->getObjMsg(errOffset); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 641 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 642 | std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at "; |
| 643 | if (!src1.empty()) |
| 644 | msg += src1 + "\n>>> "; |
| 645 | msg += obj1 + "\n>>> defined at "; |
| 646 | if (!src2.empty()) |
| 647 | msg += src2 + "\n>>> "; |
| 648 | msg += obj2; |
| 649 | error(msg); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 650 | } |
| 651 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 652 | void Symbol::resolveCommon(const CommonSymbol &other) { |
| 653 | int cmp = compare(&other); |
| 654 | if (cmp < 0) |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 655 | return; |
| 656 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 657 | if (cmp > 0) { |
Fangrui Song | 69d10d2 | 2019-12-07 05:18:31 | [diff] [blame] | 658 | if (auto *s = dyn_cast<SharedSymbol>(this)) { |
| 659 | // Increase st_size if the shared symbol has a larger st_size. The shared |
| 660 | // symbol may be created from common symbols. The fact that some object |
| 661 | // files were linked into a shared object first should not change the |
| 662 | // regular rule that picks the largest st_size. |
| 663 | uint64_t size = s->size; |
| 664 | replace(other); |
| 665 | if (size > cast<CommonSymbol>(this)->size) |
| 666 | cast<CommonSymbol>(this)->size = size; |
| 667 | } else { |
| 668 | replace(other); |
| 669 | } |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 670 | return; |
| 671 | } |
| 672 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 673 | CommonSymbol *oldSym = cast<CommonSymbol>(this); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 674 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 675 | oldSym->alignment = std::max(oldSym->alignment, other.alignment); |
| 676 | if (oldSym->size < other.size) { |
| 677 | oldSym->file = other.file; |
| 678 | oldSym->size = other.size; |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 682 | void Symbol::resolveDefined(const Defined &other) { |
| 683 | int cmp = compare(&other); |
| 684 | if (cmp > 0) |
| 685 | replace(other); |
| 686 | else if (cmp == 0) |
| 687 | reportDuplicate(this, other.file, |
| 688 | dyn_cast_or_null<InputSectionBase>(other.section), |
| 689 | other.value); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 690 | } |
| 691 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 692 | template <class LazyT> void Symbol::resolveLazy(const LazyT &other) { |
Fangrui Song | 03c825c | 2020-04-06 05:27:46 | [diff] [blame] | 693 | if (!isUndefined()) { |
| 694 | // See the comment in resolveUndefined(). |
| 695 | if (isDefined()) |
| 696 | backwardReferences.erase(this); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 697 | return; |
Fangrui Song | 03c825c | 2020-04-06 05:27:46 | [diff] [blame] | 698 | } |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 699 | |
| 700 | // An undefined weak will not fetch archive members. See comment on Lazy in |
| 701 | // Symbols.h for the details. |
| 702 | if (isWeak()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 703 | uint8_t ty = type; |
| 704 | replace(other); |
| 705 | type = ty; |
| 706 | binding = STB_WEAK; |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 710 | other.fetch(); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 711 | } |
| 712 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 713 | void Symbol::resolveShared(const SharedSymbol &other) { |
Fangrui Song | 69d10d2 | 2019-12-07 05:18:31 | [diff] [blame] | 714 | if (isCommon()) { |
| 715 | // See the comment in resolveCommon() above. |
| 716 | if (other.size > cast<CommonSymbol>(this)->size) |
| 717 | cast<CommonSymbol>(this)->size = other.size; |
| 718 | return; |
| 719 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 720 | if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) { |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 721 | // An undefined symbol with non default visibility must be satisfied |
| 722 | // in the same DSO. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 723 | uint8_t bind = binding; |
| 724 | replace(other); |
| 725 | binding = bind; |
Fangrui Song | 6467649 | 2020-05-18 17:15:59 | [diff] [blame] | 726 | } else if (traced) |
| 727 | printTraceSymbol(&other); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 728 | } |