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