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