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 | f3fad55 | 2018-10-12 18:29:18 | [diff] [blame] | 125 | case Symbol::PlaceholderKind: |
| 126 | llvm_unreachable("placeholder symbol reached writer"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 127 | } |
George Rimar | 777f963 | 2016-03-12 08:31:34 | [diff] [blame] | 128 | llvm_unreachable("invalid symbol kind"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 129 | } |
| 130 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 131 | uint64_t Symbol::getVA(int64_t Addend) const { |
George Rimar | f64618a | 2017-03-17 11:56:54 | [diff] [blame] | 132 | uint64_t OutVA = getSymVA(*this, Addend); |
Rafael Espindola | 8381c56 | 2016-03-17 23:36:19 | [diff] [blame] | 133 | return OutVA + Addend; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 | [diff] [blame] | 134 | } |
| 135 | |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 136 | uint64_t Symbol::getGotVA() const { |
| 137 | if (GotInIgot) |
| 138 | return In.IgotPlt->getVA() + getGotPltOffset(); |
| 139 | return In.Got->getVA() + getGotOffset(); |
| 140 | } |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 | [diff] [blame] | 141 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 142 | uint64_t Symbol::getGotOffset() const { |
Rui Ueyama | 803b120 | 2016-07-13 18:55:14 | [diff] [blame] | 143 | return GotIndex * Target->GotEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 144 | } |
| 145 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 146 | uint64_t Symbol::getGotPltVA() const { |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 147 | if (IsInIplt) |
Rui Ueyama | 4e24752 | 2018-09-25 19:26:58 | [diff] [blame] | 148 | return In.IgotPlt->getVA() + getGotPltOffset(); |
| 149 | return In.GotPlt->getVA() + getGotPltOffset(); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 | [diff] [blame] | 150 | } |
| 151 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 152 | uint64_t Symbol::getGotPltOffset() const { |
Peter Collingbourne | 8331f61 | 2019-02-13 21:49:55 | [diff] [blame] | 153 | if (IsInIplt) |
Rafael Espindola | f4a9d56 | 2018-04-26 16:09:30 | [diff] [blame] | 154 | return PltIndex * Target->GotPltEntrySize; |
| 155 | return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 156 | } |
| 157 | |
Sean Fertile | 614dc11 | 2018-11-14 17:56:43 | [diff] [blame] | 158 | uint64_t Symbol::getPPC64LongBranchOffset() const { |
| 159 | assert(PPC64BranchltIndex != 0xffff); |
| 160 | return PPC64BranchltIndex * Target->GotPltEntrySize; |
| 161 | } |
| 162 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 163 | uint64_t Symbol::getPltVA() const { |
Rui Ueyama | 63d397e | 2018-11-28 17:42:59 | [diff] [blame] | 164 | PltSection *Plt = IsInIplt ? In.Iplt : In.Plt; |
Simon Atanasyan | fae2a509 | 2019-02-19 10:36:58 | [diff] [blame] | 165 | uint64_t OutVA = |
| 166 | Plt->getVA() + Plt->HeaderSize + PltIndex * Target->PltEntrySize; |
| 167 | // While linking microMIPS code PLT code are always microMIPS |
| 168 | // code. Set the less-significant bit to track that fact. |
| 169 | // See detailed comment in the `getSymVA` function. |
| 170 | if (Config->EMachine == EM_MIPS && isMicroMips()) |
| 171 | OutVA |= 1; |
| 172 | return OutVA; |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 | [diff] [blame] | 173 | } |
| 174 | |
Sean Fertile | 614dc11 | 2018-11-14 17:56:43 | [diff] [blame] | 175 | uint64_t Symbol::getPPC64LongBranchTableVA() const { |
| 176 | assert(PPC64BranchltIndex != 0xffff); |
| 177 | return In.PPC64LongBranchTarget->getVA() + |
| 178 | PPC64BranchltIndex * Target->GotPltEntrySize; |
| 179 | } |
| 180 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 181 | uint64_t Symbol::getSize() const { |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 182 | if (const auto *DR = dyn_cast<Defined>(this)) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 | [diff] [blame] | 183 | return DR->Size; |
George Rimar | 904ed69 | 2018-07-17 11:35:28 | [diff] [blame] | 184 | return cast<SharedSymbol>(this)->Size; |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 | [diff] [blame] | 185 | } |
| 186 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 187 | OutputSection *Symbol::getOutputSection() const { |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 188 | if (auto *S = dyn_cast<Defined>(this)) { |
Rafael Espindola | f4fb5fd | 2017-12-13 22:59:23 | [diff] [blame] | 189 | if (auto *Sec = S->Section) |
Rafael Espindola | f4d6e8c | 2018-04-19 17:26:50 | [diff] [blame] | 190 | return Sec->Repl->getOutputSection(); |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 | [diff] [blame] | 191 | return nullptr; |
| 192 | } |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 | [diff] [blame] | 193 | return nullptr; |
| 194 | } |
| 195 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 196 | // If a symbol name contains '@', the characters after that is |
| 197 | // a symbol version name. This function parses that. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 198 | void Symbol::parseSymbolVersion() { |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 199 | StringRef S = getName(); |
| 200 | size_t Pos = S.find('@'); |
| 201 | if (Pos == 0 || Pos == StringRef::npos) |
| 202 | return; |
| 203 | StringRef Verstr = S.substr(Pos + 1); |
| 204 | if (Verstr.empty()) |
| 205 | return; |
| 206 | |
| 207 | // Truncate the symbol name so that it doesn't include the version string. |
Rafael Espindola | 1eeb262 | 2018-04-25 21:44:37 | [diff] [blame] | 208 | NameSize = Pos; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 209 | |
Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 | [diff] [blame] | 210 | // If this is not in this DSO, it is not a definition. |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 | [diff] [blame] | 211 | if (!isDefined()) |
Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 | [diff] [blame] | 212 | return; |
| 213 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 214 | // '@@' in a symbol name means the default version. |
| 215 | // It is usually the most recent one. |
| 216 | bool IsDefault = (Verstr[0] == '@'); |
| 217 | if (IsDefault) |
| 218 | Verstr = Verstr.substr(1); |
| 219 | |
| 220 | for (VersionDefinition &Ver : Config->VersionDefinitions) { |
| 221 | if (Ver.Name != Verstr) |
| 222 | continue; |
| 223 | |
| 224 | if (IsDefault) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 225 | VersionId = Ver.Id; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 226 | else |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 227 | VersionId = Ver.Id | VERSYM_HIDDEN; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | // It is an error if the specified version is not defined. |
George Rimar | 4d2f9762 | 2017-07-04 13:19:13 | [diff] [blame] | 232 | // Usually version script is not provided when linking executable, |
| 233 | // but we may still want to override a versioned symbol from DSO, |
Peter Smith | 796fb99 | 2018-05-14 10:13:56 | [diff] [blame] | 234 | // so we do not report error in this case. We also do not error |
| 235 | // if the symbol has a local version as it won't be in the dynamic |
| 236 | // symbol table. |
| 237 | if (Config->Shared && VersionId != VER_NDX_LOCAL) |
Rafael Espindola | dfebd36 | 2017-11-29 22:47:35 | [diff] [blame] | 238 | error(toString(File) + ": symbol " + S + " has undefined version " + |
George Rimar | 4d2f9762 | 2017-07-04 13:19:13 | [diff] [blame] | 239 | Verstr); |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 | [diff] [blame] | 240 | } |
| 241 | |
Rui Ueyama | 7d47619 | 2019-05-16 02:14:00 | [diff] [blame^] | 242 | InputFile *LazyArchive::fetch() const { |
| 243 | return cast<ArchiveFile>(File)->fetch(Sym); |
| 244 | } |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 245 | |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 246 | MemoryBufferRef LazyArchive::getMemberBuffer() { |
| 247 | Archive::Child C = CHECK( |
| 248 | Sym.getMember(), "could not get the member for symbol " + Sym.getName()); |
| 249 | |
| 250 | return CHECK(C.getMemoryBufferRef(), |
| 251 | "could not get the buffer for the member defining symbol " + |
| 252 | Sym.getName()); |
| 253 | } |
| 254 | |
Rui Ueyama | 7d47619 | 2019-05-16 02:14:00 | [diff] [blame^] | 255 | InputFile *LazyObject::fetch() const { |
| 256 | return cast<LazyObjFile>(File)->fetch(); |
| 257 | } |
| 258 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 259 | uint8_t Symbol::computeBinding() const { |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 260 | if (Config->Relocatable) |
| 261 | return Binding; |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 | [diff] [blame] | 262 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 263 | return STB_LOCAL; |
George Rimar | f79a8ef | 2018-10-03 09:33:00 | [diff] [blame] | 264 | if (VersionId == VER_NDX_LOCAL && isDefined() && !IsPreemptible) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 265 | return STB_LOCAL; |
Rui Ueyama | aad2e32 | 2018-02-02 21:44:06 | [diff] [blame] | 266 | if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 267 | return STB_GLOBAL; |
| 268 | return Binding; |
| 269 | } |
| 270 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 271 | bool Symbol::includeInDynsym() const { |
Rafael Espindola | e05e2f8 | 2017-09-15 18:05:02 | [diff] [blame] | 272 | if (!Config->HasDynSymTab) |
| 273 | return false; |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 | [diff] [blame] | 274 | if (computeBinding() == STB_LOCAL) |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 | [diff] [blame] | 275 | return false; |
Siva Chandra | 1915e2b | 2019-03-18 15:32:57 | [diff] [blame] | 276 | // If a PIE binary was not linked against any shared libraries, then we can |
| 277 | // safely drop weak undef symbols from .dynsym. |
| 278 | if (isUndefWeak() && Config->Pie && SharedFiles.empty()) |
| 279 | return false; |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 | [diff] [blame] | 280 | if (!isDefined()) |
Rafael Espindola | 3d9f1c0 | 2017-09-13 20:43:04 | [diff] [blame] | 281 | return true; |
Rafael Espindola | c57f8cd | 2017-09-13 20:47:53 | [diff] [blame] | 282 | return ExportDynamic; |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 | [diff] [blame] | 283 | } |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 284 | |
| 285 | // Print out a log message for --trace-symbol. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 286 | void elf::printTraceSymbol(Symbol *Sym) { |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 | [diff] [blame] | 287 | std::string S; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 288 | if (Sym->isUndefined()) |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 | [diff] [blame] | 289 | S = ": reference to "; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 290 | else if (Sym->isLazy()) |
Rafael Espindola | bc2b165 | 2017-10-27 18:30:11 | [diff] [blame] | 291 | S = ": lazy definition of "; |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 292 | else if (Sym->isShared()) |
Rafael Espindola | bc2b165 | 2017-10-27 18:30:11 | [diff] [blame] | 293 | S = ": shared definition of "; |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 | [diff] [blame] | 294 | else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section)) |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 | [diff] [blame] | 295 | S = ": common definition of "; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 296 | else |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 | [diff] [blame] | 297 | S = ": definition of "; |
| 298 | |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 299 | message(toString(Sym->File) + S + Sym->getName()); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 300 | } |
| 301 | |
Rui Ueyama | 4c06a6c | 2018-10-26 15:07:12 | [diff] [blame] | 302 | void elf::maybeWarnUnorderableSymbol(const Symbol *Sym) { |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 303 | if (!Config->WarnSymbolOrdering) |
| 304 | return; |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 305 | |
Fangrui Song | 11ca54f | 2018-10-10 22:48:57 | [diff] [blame] | 306 | // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning |
| 307 | // is emitted. It makes sense to not warn on undefined symbols. |
| 308 | // |
| 309 | // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols, |
| 310 | // but we don't have to be compatible here. |
| 311 | if (Sym->isUndefined() && |
| 312 | Config->UnresolvedSymbols == UnresolvedPolicy::Ignore) |
| 313 | return; |
| 314 | |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 315 | const InputFile *File = Sym->File; |
| 316 | auto *D = dyn_cast<Defined>(Sym); |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 317 | |
| 318 | auto Warn = [&](StringRef S) { warn(toString(File) + S + Sym->getName()); }; |
| 319 | |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 320 | if (Sym->isUndefined()) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 321 | Warn(": unable to order undefined symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 322 | else if (Sym->isShared()) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 323 | Warn(": unable to order shared symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 324 | else if (D && !D->Section) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 325 | Warn(": unable to order absolute symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 326 | else if (D && isa<OutputSection>(D->Section)) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 327 | Warn(": unable to order synthetic symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 328 | else if (D && !D->Section->Repl->Live) |
Rui Ueyama | b774c3c | 2018-04-26 01:38:29 | [diff] [blame] | 329 | Warn(": unable to order discarded symbol: "); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 330 | } |
| 331 | |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 | [diff] [blame] | 332 | // Returns a symbol for an error message. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 333 | std::string lld::toString(const Symbol &B) { |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 | [diff] [blame] | 334 | if (Config->Demangle) |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 | [diff] [blame] | 335 | if (Optional<std::string> S = demangleItanium(B.getName())) |
Rui Ueyama | 4c5b8ce | 2016-12-07 23:17:05 | [diff] [blame] | 336 | return *S; |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 | [diff] [blame] | 337 | return B.getName(); |
| 338 | } |