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" |
Eugene Leviant | c958d8d | 2016-10-12 08:19:30 | [diff] [blame] | 19 | #include "llvm/Support/Path.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; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 25 | |
| 26 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 | [diff] [blame] | 27 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 28 | |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 29 | Defined *ElfSym::Bss; |
| 30 | Defined *ElfSym::Etext1; |
| 31 | Defined *ElfSym::Etext2; |
| 32 | Defined *ElfSym::Edata1; |
| 33 | Defined *ElfSym::Edata2; |
| 34 | Defined *ElfSym::End1; |
| 35 | Defined *ElfSym::End2; |
| 36 | Defined *ElfSym::GlobalOffsetTable; |
| 37 | Defined *ElfSym::MipsGp; |
| 38 | Defined *ElfSym::MipsGpDisp; |
| 39 | Defined *ElfSym::MipsLocalGp; |
Rui Ueyama | 6f9d49c | 2019-01-15 18:30:23 | [diff] [blame] | 40 | Defined *ElfSym::RelaIpltStart; |
Rafael Espindola | aded409 | 2018-04-19 16:54:30 | [diff] [blame] | 41 | Defined *ElfSym::RelaIpltEnd; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 | [diff] [blame] | 42 | |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 | [diff] [blame] | 43 | static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) { |
| 44 | switch (Sym.kind()) { |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 45 | case Symbol::DefinedKind: { |
| 46 | auto &D = cast<Defined>(Sym); |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 | [diff] [blame] | 47 | SectionBase *IS = D.Section; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 48 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 | [diff] [blame] | 49 | // According to the ELF spec reference to a local symbol from outside |
| 50 | // the group are not allowed. Unfortunately .eh_frame breaks that rule |
| 51 | // and must be treated specially. For now we just replace the symbol with |
| 52 | // 0. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 | [diff] [blame] | 53 | if (IS == &InputSection::Discarded) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 | [diff] [blame] | 54 | return 0; |
| 55 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 56 | // This is an absolute symbol. |
Sean Silva | 902ae3c | 2016-12-15 00:57:53 | [diff] [blame] | 57 | if (!IS) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 | [diff] [blame] | 58 | return D.Value; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 59 | |
Rafael Espindola | f4d6e8c | 2018-04-19 17:26:50 | [diff] [blame] | 60 | IS = IS->Repl; |
| 61 | |
Rafael Espindola | 9371bab | 2017-03-08 15:21:32 | [diff] [blame] | 62 | uint64_t Offset = D.Value; |
Sean Silva | a9ba450 | 2017-02-28 08:32:56 | [diff] [blame] | 63 | |
| 64 | // An object in an SHF_MERGE section might be referenced via a |
| 65 | // section symbol (as a hack for reducing the number of local |
| 66 | // symbols). |
Sean Silva | d4e6062 | 2017-03-01 04:44:04 | [diff] [blame] | 67 | // Depending on the addend, the reference via a section symbol |
| 68 | // refers to a different object in the merge section. |
| 69 | // Since the objects in the merge section are not necessarily |
| 70 | // contiguous in the output, the addend can thus affect the final |
| 71 | // VA in a non-linear way. |
| 72 | // To make this work, we incorporate the addend into the section |
| 73 | // offset (and zero out the addend for later processing) so that |
| 74 | // we find the right object in the section. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 | [diff] [blame] | 75 | if (D.isSection()) { |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 | [diff] [blame] | 76 | Offset += Addend; |
| 77 | Addend = 0; |
| 78 | } |
Sean Silva | a9ba450 | 2017-02-28 08:32:56 | [diff] [blame] | 79 | |
Sean Silva | 6ab3926 | 2017-02-28 09:01:58 | [diff] [blame] | 80 | // In the typical case, this is actually very simple and boils |
| 81 | // down to adding together 3 numbers: |
| 82 | // 1. The address of the output section. |
| 83 | // 2. The offset of the input section within the output section. |
| 84 | // 3. The offset within the input section (this addition happens |
| 85 | // inside InputSection::getOffset). |
| 86 | // |
| 87 | // If you understand the data structures involved with this next |
| 88 | // line (and how they get built), then you have a pretty good |
| 89 | // understanding of the linker. |
Rafael Espindola | 4f058a2 | 2018-03-24 00:35:11 | [diff] [blame] | 90 | uint64_t VA = IS->getVA(Offset); |
Sean Silva | 6ab3926 | 2017-02-28 09:01:58 | [diff] [blame] | 91 | |
Simon Atanasyan | fae2a509 | 2019-02-19 10:36:58 | [diff] [blame] | 92 | // MIPS relocatable files can mix regular and microMIPS code. |
| 93 | // Linker needs to distinguish such code. To do so microMIPS |
| 94 | // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other` |
| 95 | // field. Unfortunately, the `MIPS::relocateOne()` method has |
| 96 | // a symbol value only. To pass type of the symbol (regular/microMIPS) |
| 97 | // to that routine as well as other places where we write |
| 98 | // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry` |
| 99 | // field etc) do the same trick as compiler uses to mark microMIPS |
| 100 | // for CPU - set the less-significant bit. |
| 101 | if (Config->EMachine == EM_MIPS && isMicroMips() && |
| 102 | ((Sym.StOther & STO_MIPS_MICROMIPS) || Sym.NeedsPltAddr)) |
| 103 | VA |= 1; |
| 104 | |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 | [diff] [blame] | 105 | if (D.isTls() && !Config->Relocatable) { |
Ryan Prichard | 1c33d14 | 2018-09-18 00:24:48 | [diff] [blame] | 106 | // Use the address of the TLS segment's first section rather than the |
| 107 | // segment's address, because segment addresses aren't initialized until |
| 108 | // after sections are finalized. (e.g. Measuring the size of .rela.dyn |
| 109 | // for Android relocation packing requires knowing TLS symbol addresses |
| 110 | // during section finalization.) |
| 111 | if (!Out::TlsPhdr || !Out::TlsPhdr->FirstSec) |
Rafael Espindola | dfebd36 | 2017-11-29 22:47:35 | [diff] [blame] | 112 | fatal(toString(D.File) + |
Peter Collingbourne | 3e2abde | 2017-07-14 00:22:46 | [diff] [blame] | 113 | " has an STT_TLS symbol but doesn't have an SHF_TLS section"); |
Ryan Prichard | 1c33d14 | 2018-09-18 00:24:48 | [diff] [blame] | 114 | return VA - Out::TlsPhdr->FirstSec->Addr; |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 | [diff] [blame] | 115 | } |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 | [diff] [blame] | 116 | return VA; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 117 | } |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 | [diff] [blame] | 118 | case Symbol::SharedKind: |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 119 | case Symbol::UndefinedKind: |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 120 | return 0; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 121 | case Symbol::LazyArchiveKind: |
| 122 | case Symbol::LazyObjectKind: |
Chih-Hung Hsieh | 73e0484 | 2018-09-11 23:00:36 | [diff] [blame] | 123 | assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer"); |
| 124 | return 0; |
Rui Ueyama | 5c073a9 | 2019-05-16 03:29:03 | [diff] [blame] | 125 | case Symbol::CommonKind: |
| 126 | llvm_unreachable("common symbol reached writer"); |
Rui Ueyama | f3fad55 | 2018-10-12 18:29:18 | [diff] [blame] | 127 | case Symbol::PlaceholderKind: |
| 128 | llvm_unreachable("placeholder symbol reached writer"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 129 | } |
George Rimar | 777f963 | 2016-03-12 08:31:34 | [diff] [blame] | 130 | llvm_unreachable("invalid symbol kind"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 131 | } |
| 132 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 133 | uint64_t Symbol::getVA(int64_t Addend) const { |
George Rimar | f64618a | 2017-03-17 11:56:54 | [diff] [blame] | 134 | uint64_t OutVA = getSymVA(*this, Addend); |
Rafael Espindola | 8381c56 | 2016-03-17 23:36:19 | [diff] [blame] | 135 | return OutVA + Addend; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 | [diff] [blame] | 136 | } |
| 137 | |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 138 | uint64_t Symbol::getGotVA() const { |
| 139 | if (GotInIgot) |
| 140 | return In.IgotPlt->getVA() + getGotPltOffset(); |
| 141 | return In.Got->getVA() + getGotOffset(); |
| 142 | } |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 | [diff] [blame] | 143 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 144 | uint64_t Symbol::getGotOffset() const { |
Rui Ueyama | 803b120 | 2016-07-13 18:55:14 | [diff] [blame] | 145 | return GotIndex * Target->GotEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 146 | } |
| 147 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 148 | uint64_t Symbol::getGotPltVA() const { |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 149 | if (IsInIplt) |
Rui Ueyama | 4e24752 | 2018-09-25 19:26:58 | [diff] [blame] | 150 | return In.IgotPlt->getVA() + getGotPltOffset(); |
| 151 | return In.GotPlt->getVA() + getGotPltOffset(); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 | [diff] [blame] | 152 | } |
| 153 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 154 | uint64_t Symbol::getGotPltOffset() const { |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 155 | if (IsInIplt) |
Rafael Espindola | f4a9d56 | 2018-04-26 16:09:30 | [diff] [blame] | 156 | return PltIndex * Target->GotPltEntrySize; |
| 157 | return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 158 | } |
| 159 | |
Sean Fertile | 614dc11 | 2018-11-14 17:56:43 | [diff] [blame] | 160 | uint64_t Symbol::getPPC64LongBranchOffset() const { |
| 161 | assert(PPC64BranchltIndex != 0xffff); |
| 162 | return PPC64BranchltIndex * Target->GotPltEntrySize; |
| 163 | } |
| 164 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 165 | uint64_t Symbol::getPltVA() const { |
Rui Ueyama | 63d397e | 2018-11-28 17:42:59 | [diff] [blame] | 166 | PltSection *Plt = IsInIplt ? In.Iplt : In.Plt; |
Simon Atanasyan | fae2a509 | 2019-02-19 10:36:58 | [diff] [blame] | 167 | uint64_t OutVA = |
| 168 | Plt->getVA() + Plt->HeaderSize + PltIndex * Target->PltEntrySize; |
| 169 | // While linking microMIPS code PLT code are always microMIPS |
| 170 | // code. Set the less-significant bit to track that fact. |
| 171 | // See detailed comment in the `getSymVA` function. |
| 172 | if (Config->EMachine == EM_MIPS && isMicroMips()) |
| 173 | OutVA |= 1; |
| 174 | return OutVA; |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 | [diff] [blame] | 175 | } |
| 176 | |
Sean Fertile | 614dc11 | 2018-11-14 17:56:43 | [diff] [blame] | 177 | uint64_t Symbol::getPPC64LongBranchTableVA() const { |
| 178 | assert(PPC64BranchltIndex != 0xffff); |
| 179 | return In.PPC64LongBranchTarget->getVA() + |
| 180 | PPC64BranchltIndex * Target->GotPltEntrySize; |
| 181 | } |
| 182 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 183 | uint64_t Symbol::getSize() const { |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 184 | if (const auto *DR = dyn_cast<Defined>(this)) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 | [diff] [blame] | 185 | return DR->Size; |
George Rimar | 904ed69 | 2018-07-17 11:35:28 | [diff] [blame] | 186 | return cast<SharedSymbol>(this)->Size; |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 | [diff] [blame] | 187 | } |
| 188 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 189 | OutputSection *Symbol::getOutputSection() const { |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 190 | if (auto *S = dyn_cast<Defined>(this)) { |
Rafael Espindola | f4fb5fd | 2017-12-13 22:59:23 | [diff] [blame] | 191 | if (auto *Sec = S->Section) |
Rafael Espindola | f4d6e8c | 2018-04-19 17:26:50 | [diff] [blame] | 192 | return Sec->Repl->getOutputSection(); |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 | [diff] [blame] | 193 | return nullptr; |
| 194 | } |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 | [diff] [blame] | 195 | return nullptr; |
| 196 | } |
| 197 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 198 | // If a symbol name contains '@', the characters after that is |
| 199 | // a symbol version name. This function parses that. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 200 | void Symbol::parseSymbolVersion() { |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 201 | StringRef S = getName(); |
| 202 | size_t Pos = S.find('@'); |
| 203 | if (Pos == 0 || Pos == StringRef::npos) |
| 204 | return; |
| 205 | StringRef Verstr = S.substr(Pos + 1); |
| 206 | if (Verstr.empty()) |
| 207 | return; |
| 208 | |
| 209 | // Truncate the symbol name so that it doesn't include the version string. |
Rafael Espindola | 1eeb262 | 2018-04-25 21:44:37 | [diff] [blame] | 210 | NameSize = Pos; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 211 | |
Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 | [diff] [blame] | 212 | // If this is not in this DSO, it is not a definition. |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 | [diff] [blame] | 213 | if (!isDefined()) |
Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 | [diff] [blame] | 214 | return; |
| 215 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 216 | // '@@' in a symbol name means the default version. |
| 217 | // It is usually the most recent one. |
| 218 | bool IsDefault = (Verstr[0] == '@'); |
| 219 | if (IsDefault) |
| 220 | Verstr = Verstr.substr(1); |
| 221 | |
| 222 | for (VersionDefinition &Ver : Config->VersionDefinitions) { |
| 223 | if (Ver.Name != Verstr) |
| 224 | continue; |
| 225 | |
| 226 | if (IsDefault) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 227 | VersionId = Ver.Id; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 228 | else |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 229 | VersionId = Ver.Id | VERSYM_HIDDEN; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
| 233 | // It is an error if the specified version is not defined. |
George Rimar | 4d2f9762 | 2017-07-04 13:19:13 | [diff] [blame] | 234 | // Usually version script is not provided when linking executable, |
| 235 | // but we may still want to override a versioned symbol from DSO, |
Peter Smith | 796fb99 | 2018-05-14 10:13:56 | [diff] [blame] | 236 | // so we do not report error in this case. We also do not error |
| 237 | // if the symbol has a local version as it won't be in the dynamic |
| 238 | // symbol table. |
| 239 | if (Config->Shared && VersionId != VER_NDX_LOCAL) |
Rafael Espindola | dfebd36 | 2017-11-29 22:47:35 | [diff] [blame] | 240 | error(toString(File) + ": symbol " + S + " has undefined version " + |
George Rimar | 4d2f9762 | 2017-07-04 13:19:13 | [diff] [blame] | 241 | Verstr); |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 242 | } |
| 243 | |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 244 | void Symbol::fetch() const { |
| 245 | if (auto *Sym = dyn_cast<LazyArchive>(this)) { |
Rui Ueyama | f5d9d23 | 2019-05-23 10:15:12 | [diff] [blame] | 246 | cast<ArchiveFile>(Sym->File)->fetch(Sym->Sym); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
| 250 | if (auto *Sym = dyn_cast<LazyObject>(this)) { |
Rui Ueyama | f5d9d23 | 2019-05-23 10:15:12 | [diff] [blame] | 251 | dyn_cast<LazyObjFile>(Sym->File)->fetch(); |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
| 255 | llvm_unreachable("Symbol::fetch() is called on a non-lazy symbol"); |
Rui Ueyama | 7d47619 | 2019-05-16 02:14:00 | [diff] [blame] | 256 | } |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 257 | |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 258 | MemoryBufferRef LazyArchive::getMemberBuffer() { |
| 259 | Archive::Child C = CHECK( |
| 260 | Sym.getMember(), "could not get the member for symbol " + Sym.getName()); |
| 261 | |
| 262 | return CHECK(C.getMemoryBufferRef(), |
| 263 | "could not get the buffer for the member defining symbol " + |
| 264 | Sym.getName()); |
| 265 | } |
| 266 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 267 | uint8_t Symbol::computeBinding() const { |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 268 | if (Config->Relocatable) |
| 269 | return Binding; |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 | [diff] [blame] | 270 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 271 | return STB_LOCAL; |
George Rimar | f79a8ef | 2018-10-03 09:33:00 | [diff] [blame] | 272 | if (VersionId == VER_NDX_LOCAL && isDefined() && !IsPreemptible) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 273 | return STB_LOCAL; |
Rui Ueyama | aad2e32 | 2018-02-02 21:44:06 | [diff] [blame] | 274 | if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 275 | return STB_GLOBAL; |
| 276 | return Binding; |
| 277 | } |
| 278 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 279 | bool Symbol::includeInDynsym() const { |
Rafael Espindola | e05e2f8 | 2017-09-15 18:05:02 | [diff] [blame] | 280 | if (!Config->HasDynSymTab) |
| 281 | return false; |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 282 | if (computeBinding() == STB_LOCAL) |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 | [diff] [blame] | 283 | return false; |
Siva Chandra | 1915e2b | 2019-03-18 15:32:57 | [diff] [blame] | 284 | // If a PIE binary was not linked against any shared libraries, then we can |
| 285 | // safely drop weak undef symbols from .dynsym. |
| 286 | if (isUndefWeak() && Config->Pie && SharedFiles.empty()) |
| 287 | return false; |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 | [diff] [blame] | 288 | if (!isDefined()) |
Rafael Espindola | 3d9f1c0 | 2017-09-13 20:43:04 | [diff] [blame] | 289 | return true; |
Rafael Espindola | c57f8cd | 2017-09-13 20:47:53 | [diff] [blame] | 290 | return ExportDynamic; |
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. |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 | [diff] [blame^] | 294 | void elf::printTraceSymbol(const Symbol *Sym) { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 | [diff] [blame] | 295 | std::string S; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 296 | if (Sym->isUndefined()) |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 | [diff] [blame] | 297 | S = ": reference to "; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 298 | else if (Sym->isLazy()) |
Rafael Espindola | bc2b165 | 2017-10-27 18:30:11 | [diff] [blame] | 299 | S = ": lazy definition of "; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 300 | else if (Sym->isShared()) |
Rafael Espindola | bc2b165 | 2017-10-27 18:30:11 | [diff] [blame] | 301 | S = ": shared definition of "; |
Rui Ueyama | 5c073a9 | 2019-05-16 03:29:03 | [diff] [blame] | 302 | else if (Sym->isCommon()) |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 | [diff] [blame] | 303 | S = ": common definition of "; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 304 | else |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 | [diff] [blame] | 305 | S = ": definition of "; |
| 306 | |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 307 | message(toString(Sym->File) + S + Sym->getName()); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 308 | } |
| 309 | |
Rui Ueyama | 4c06a6c | 2018-10-26 15:07:12 | [diff] [blame] | 310 | void elf::maybeWarnUnorderableSymbol(const Symbol *Sym) { |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 311 | if (!Config->WarnSymbolOrdering) |
| 312 | return; |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 313 | |
Fangrui Song | 11ca54f | 2018-10-10 22:48:57 | [diff] [blame] | 314 | // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning |
| 315 | // is emitted. It makes sense to not warn on undefined symbols. |
| 316 | // |
| 317 | // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols, |
| 318 | // but we don't have to be compatible here. |
| 319 | if (Sym->isUndefined() && |
| 320 | Config->UnresolvedSymbols == UnresolvedPolicy::Ignore) |
| 321 | return; |
| 322 | |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 323 | const InputFile *File = Sym->File; |
| 324 | auto *D = dyn_cast<Defined>(Sym); |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 325 | |
| 326 | auto Warn = [&](StringRef S) { warn(toString(File) + S + Sym->getName()); }; |
| 327 | |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 328 | if (Sym->isUndefined()) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 329 | Warn(": unable to order undefined symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 330 | else if (Sym->isShared()) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 331 | Warn(": unable to order shared symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 332 | else if (D && !D->Section) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 333 | Warn(": unable to order absolute symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 334 | else if (D && isa<OutputSection>(D->Section)) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 335 | Warn(": unable to order synthetic symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 336 | else if (D && !D->Section->Repl->Live) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 337 | Warn(": unable to order discarded symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 338 | } |
| 339 | |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 | [diff] [blame] | 340 | // Returns a symbol for an error message. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 341 | std::string lld::toString(const Symbol &B) { |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 | [diff] [blame] | 342 | if (Config->Demangle) |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 | [diff] [blame] | 343 | if (Optional<std::string> S = demangleItanium(B.getName())) |
Rui Ueyama | 4c5b8ce | 2016-12-07 23:17:05 | [diff] [blame] | 344 | return *S; |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 | [diff] [blame] | 345 | return B.getName(); |
| 346 | } |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 347 | |
| 348 | static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { |
| 349 | if (VA == STV_DEFAULT) |
| 350 | return VB; |
| 351 | if (VB == STV_DEFAULT) |
| 352 | return VA; |
| 353 | return std::min(VA, VB); |
| 354 | } |
| 355 | |
| 356 | // Merge symbol properties. |
| 357 | // |
| 358 | // When we have many symbols of the same name, we choose one of them, |
| 359 | // and that's the result of symbol resolution. However, symbols that |
| 360 | // were not chosen still affect some symbol properties. |
| 361 | void Symbol::mergeProperties(const Symbol &Other) { |
| 362 | if (Other.ExportDynamic) |
| 363 | ExportDynamic = true; |
| 364 | if (Other.IsUsedInRegularObj) |
| 365 | IsUsedInRegularObj = true; |
| 366 | |
| 367 | // DSO symbols do not affect visibility in the output. |
| 368 | if (!Other.isShared()) |
| 369 | Visibility = getMinVisibility(Visibility, Other.Visibility); |
| 370 | } |
| 371 | |
| 372 | void Symbol::resolve(const Symbol &Other) { |
| 373 | mergeProperties(Other); |
| 374 | |
| 375 | if (isPlaceholder()) { |
| 376 | replace(Other); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | switch (Other.kind()) { |
| 381 | case Symbol::UndefinedKind: |
| 382 | resolveUndefined(cast<Undefined>(Other)); |
| 383 | break; |
| 384 | case Symbol::CommonKind: |
| 385 | resolveCommon(cast<CommonSymbol>(Other)); |
| 386 | break; |
| 387 | case Symbol::DefinedKind: |
| 388 | resolveDefined(cast<Defined>(Other)); |
| 389 | break; |
| 390 | case Symbol::LazyArchiveKind: |
| 391 | resolveLazy(cast<LazyArchive>(Other)); |
| 392 | break; |
| 393 | case Symbol::LazyObjectKind: |
| 394 | resolveLazy(cast<LazyObject>(Other)); |
| 395 | break; |
| 396 | case Symbol::SharedKind: |
| 397 | resolveShared(cast<SharedSymbol>(Other)); |
| 398 | break; |
| 399 | case Symbol::PlaceholderKind: |
| 400 | llvm_unreachable("bad symbol kind"); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void Symbol::resolveUndefined(const Undefined &Other) { |
| 405 | // An undefined symbol with non default visibility must be satisfied |
| 406 | // in the same DSO. |
| 407 | // |
| 408 | // If this is a non-weak defined symbol in a discarded section, override the |
| 409 | // existing undefined symbol for better error message later. |
| 410 | if ((isShared() && Other.Visibility != STV_DEFAULT) || |
| 411 | (isUndefined() && Other.Binding != STB_WEAK && Other.DiscardedSecIdx)) { |
| 412 | replace(Other); |
| 413 | return; |
| 414 | } |
| 415 | |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 | [diff] [blame^] | 416 | if (Traced) |
| 417 | printTraceSymbol(&Other); |
| 418 | |
Rui Ueyama | 7f7d2b2 | 2019-05-23 09:58:08 | [diff] [blame] | 419 | if (isShared() || isLazy() || (isUndefined() && Other.Binding != STB_WEAK)) |
| 420 | Binding = Other.Binding; |
| 421 | |
| 422 | if (isLazy()) { |
| 423 | // An undefined weak will not fetch archive members. See comment on Lazy in |
| 424 | // Symbols.h for the details. |
| 425 | if (Other.Binding == STB_WEAK) { |
| 426 | Type = Other.Type; |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | // Do extra check for --warn-backrefs. |
| 431 | // |
| 432 | // --warn-backrefs is an option to prevent an undefined reference from |
| 433 | // fetching an archive member written earlier in the command line. It can be |
| 434 | // used to keep compatibility with GNU linkers to some degree. |
| 435 | // I'll explain the feature and why you may find it useful in this comment. |
| 436 | // |
| 437 | // lld's symbol resolution semantics is more relaxed than traditional Unix |
| 438 | // linkers. For example, |
| 439 | // |
| 440 | // ld.lld foo.a bar.o |
| 441 | // |
| 442 | // succeeds even if bar.o contains an undefined symbol that has to be |
| 443 | // resolved by some object file in foo.a. Traditional Unix linkers don't |
| 444 | // allow this kind of backward reference, as they visit each file only once |
| 445 | // from left to right in the command line while resolving all undefined |
| 446 | // symbols at the moment of visiting. |
| 447 | // |
| 448 | // In the above case, since there's no undefined symbol when a linker visits |
| 449 | // foo.a, no files are pulled out from foo.a, and because the linker forgets |
| 450 | // about foo.a after visiting, it can't resolve undefined symbols in bar.o |
| 451 | // that could have been resolved otherwise. |
| 452 | // |
| 453 | // That lld accepts more relaxed form means that (besides it'd make more |
| 454 | // sense) you can accidentally write a command line or a build file that |
| 455 | // works only with lld, even if you have a plan to distribute it to wider |
| 456 | // users who may be using GNU linkers. With --warn-backrefs, you can detect |
| 457 | // a library order that doesn't work with other Unix linkers. |
| 458 | // |
| 459 | // The option is also useful to detect cyclic dependencies between static |
| 460 | // archives. Again, lld accepts |
| 461 | // |
| 462 | // ld.lld foo.a bar.a |
| 463 | // |
| 464 | // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is |
| 465 | // handled as an error. |
| 466 | // |
| 467 | // Here is how the option works. We assign a group ID to each file. A file |
| 468 | // with a smaller group ID can pull out object files from an archive file |
| 469 | // with an equal or greater group ID. Otherwise, it is a reverse dependency |
| 470 | // and an error. |
| 471 | // |
| 472 | // A file outside --{start,end}-group gets a fresh ID when instantiated. All |
| 473 | // files within the same --{start,end}-group get the same group ID. E.g. |
| 474 | // |
| 475 | // ld.lld A B --start-group C D --end-group E |
| 476 | // |
| 477 | // A forms group 0. B form group 1. C and D (including their member object |
| 478 | // files) form group 2. E forms group 3. I think that you can see how this |
| 479 | // group assignment rule simulates the traditional linker's semantics. |
| 480 | bool Backref = Config->WarnBackrefs && Other.File && |
| 481 | File->GroupId < Other.File->GroupId; |
| 482 | fetch(); |
| 483 | |
| 484 | // We don't report backward references to weak symbols as they can be |
| 485 | // overridden later. |
| 486 | if (Backref && !isWeak()) |
| 487 | warn("backward reference detected: " + Other.getName() + " in " + |
| 488 | toString(Other.File) + " refers to " + toString(File)); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and |
| 493 | // foo@@VER. We want to effectively ignore foo, so give precedence to |
| 494 | // foo@@VER. |
| 495 | // FIXME: If users can transition to using |
| 496 | // .symver foo,foo@@@VER |
| 497 | // we can delete this hack. |
| 498 | static int compareVersion(StringRef A, StringRef B) { |
| 499 | bool X = A.contains("@@"); |
| 500 | bool Y = B.contains("@@"); |
| 501 | if (!X && Y) |
| 502 | return 1; |
| 503 | if (X && !Y) |
| 504 | return -1; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | // Compare two symbols. Return 1 if the new symbol should win, -1 if |
| 509 | // the new symbol should lose, or 0 if there is a conflict. |
| 510 | int Symbol::compare(const Symbol *Other) const { |
| 511 | assert(Other->isDefined() || Other->isCommon()); |
| 512 | |
| 513 | if (!isDefined() && !isCommon()) |
| 514 | return 1; |
| 515 | |
| 516 | if (int Cmp = compareVersion(getName(), Other->getName())) |
| 517 | return Cmp; |
| 518 | |
| 519 | if (Other->isWeak()) |
| 520 | return -1; |
| 521 | |
| 522 | if (isWeak()) |
| 523 | return 1; |
| 524 | |
| 525 | if (isCommon() && Other->isCommon()) { |
| 526 | if (Config->WarnCommon) |
| 527 | warn("multiple common of " + getName()); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | if (isCommon()) { |
| 532 | if (Config->WarnCommon) |
| 533 | warn("common " + getName() + " is overridden"); |
| 534 | return 1; |
| 535 | } |
| 536 | |
| 537 | if (Other->isCommon()) { |
| 538 | if (Config->WarnCommon) |
| 539 | warn("common " + getName() + " is overridden"); |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | auto *OldSym = cast<Defined>(this); |
| 544 | auto *NewSym = cast<Defined>(Other); |
| 545 | |
| 546 | if (Other->File && isa<BitcodeFile>(Other->File)) |
| 547 | return 0; |
| 548 | |
| 549 | if (!OldSym->Section && !NewSym->Section && OldSym->Value == NewSym->Value && |
| 550 | NewSym->Binding == STB_GLOBAL) |
| 551 | return -1; |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static void reportDuplicate(Symbol *Sym, InputFile *NewFile, |
| 557 | InputSectionBase *ErrSec, uint64_t ErrOffset) { |
| 558 | if (Config->AllowMultipleDefinition) |
| 559 | return; |
| 560 | |
| 561 | Defined *D = cast<Defined>(Sym); |
| 562 | if (!D->Section || !ErrSec) { |
| 563 | error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " + |
| 564 | toString(Sym->File) + "\n>>> defined in " + toString(NewFile)); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | // Construct and print an error message in the form of: |
| 569 | // |
| 570 | // ld.lld: error: duplicate symbol: foo |
| 571 | // >>> defined at bar.c:30 |
| 572 | // >>> bar.o (/home/alice/src/bar.o) |
| 573 | // >>> defined at baz.c:563 |
| 574 | // >>> baz.o in archive libbaz.a |
| 575 | auto *Sec1 = cast<InputSectionBase>(D->Section); |
| 576 | std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value); |
| 577 | std::string Obj1 = Sec1->getObjMsg(D->Value); |
| 578 | std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset); |
| 579 | std::string Obj2 = ErrSec->getObjMsg(ErrOffset); |
| 580 | |
| 581 | std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at "; |
| 582 | if (!Src1.empty()) |
| 583 | Msg += Src1 + "\n>>> "; |
| 584 | Msg += Obj1 + "\n>>> defined at "; |
| 585 | if (!Src2.empty()) |
| 586 | Msg += Src2 + "\n>>> "; |
| 587 | Msg += Obj2; |
| 588 | error(Msg); |
| 589 | } |
| 590 | |
| 591 | void Symbol::resolveCommon(const CommonSymbol &Other) { |
| 592 | int Cmp = compare(&Other); |
| 593 | if (Cmp < 0) |
| 594 | return; |
| 595 | |
| 596 | if (Cmp > 0) { |
| 597 | replace(Other); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | CommonSymbol *OldSym = cast<CommonSymbol>(this); |
| 602 | |
| 603 | OldSym->Alignment = std::max(OldSym->Alignment, Other.Alignment); |
| 604 | if (OldSym->Size < Other.Size) { |
| 605 | OldSym->File = Other.File; |
| 606 | OldSym->Size = Other.Size; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | void Symbol::resolveDefined(const Defined &Other) { |
| 611 | int Cmp = compare(&Other); |
| 612 | if (Cmp > 0) |
| 613 | replace(Other); |
| 614 | else if (Cmp == 0) |
| 615 | reportDuplicate(this, Other.File, |
| 616 | dyn_cast_or_null<InputSectionBase>(Other.Section), |
| 617 | Other.Value); |
| 618 | } |
| 619 | |
| 620 | template <class LazyT> void Symbol::resolveLazy(const LazyT &Other) { |
| 621 | if (!isUndefined()) |
| 622 | return; |
| 623 | |
| 624 | // An undefined weak will not fetch archive members. See comment on Lazy in |
| 625 | // Symbols.h for the details. |
| 626 | if (isWeak()) { |
| 627 | uint8_t Ty = Type; |
| 628 | replace(Other); |
| 629 | Type = Ty; |
| 630 | Binding = STB_WEAK; |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | Other.fetch(); |
| 635 | } |
| 636 | |
| 637 | void Symbol::resolveShared(const SharedSymbol &Other) { |
| 638 | if (Visibility == STV_DEFAULT && (isUndefined() || isLazy())) { |
| 639 | // An undefined symbol with non default visibility must be satisfied |
| 640 | // in the same DSO. |
| 641 | uint8_t Bind = Binding; |
| 642 | replace(Other); |
| 643 | Binding = Bind; |
| 644 | } |
| 645 | } |