xref: /llvm-project-15.0.7/lld/ELF/Symbols.cpp (revision 74468a5b)
1 //===- Symbols.cpp --------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Symbols.h"
11 #include "Error.h"
12 #include "InputFiles.h"
13 #include "InputSection.h"
14 #include "OutputSections.h"
15 #include "Target.h"
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Config/config.h"
19 
20 #ifdef HAVE_CXXABI_H
21 #include <cxxabi.h>
22 #endif
23 
24 using namespace llvm;
25 using namespace llvm::object;
26 using namespace llvm::ELF;
27 
28 using namespace lld;
29 using namespace lld::elf;
30 
31 template <class ELFT>
32 static typename ELFT::uint getSymVA(const SymbolBody &Body,
33                                     typename ELFT::uint &Addend) {
34   typedef typename ELFT::uint uintX_t;
35 
36   switch (Body.kind()) {
37   case SymbolBody::DefinedSyntheticKind: {
38     auto &D = cast<DefinedSynthetic<ELFT>>(Body);
39     const OutputSectionBase<ELFT> *Sec = D.Section;
40     if (!Sec)
41       return D.Value;
42     if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
43       return Sec->getVA() + Sec->getSize();
44     return Sec->getVA() + D.Value;
45   }
46   case SymbolBody::DefinedRegularKind: {
47     auto &D = cast<DefinedRegular<ELFT>>(Body);
48     InputSectionBase<ELFT> *SC = D.Section;
49 
50     // According to the ELF spec reference to a local symbol from outside
51     // the group are not allowed. Unfortunately .eh_frame breaks that rule
52     // and must be treated specially. For now we just replace the symbol with
53     // 0.
54     if (SC == &InputSection<ELFT>::Discarded)
55       return 0;
56 
57     // This is an absolute symbol.
58     if (!SC)
59       return D.Value;
60 
61     uintX_t Offset = D.Value;
62     if (D.isSection()) {
63       Offset += Addend;
64       Addend = 0;
65     }
66     uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
67     if (D.isTls())
68       return VA - Out<ELFT>::TlsPhdr->p_vaddr;
69     return VA;
70   }
71   case SymbolBody::DefinedCommonKind:
72     return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
73   case SymbolBody::SharedKind: {
74     auto &SS = cast<SharedSymbol<ELFT>>(Body);
75     if (!SS.NeedsCopyOrPltAddr)
76       return 0;
77     if (SS.isFunc())
78       return Body.getPltVA<ELFT>();
79     return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
80   }
81   case SymbolBody::UndefinedKind:
82     return 0;
83   case SymbolBody::LazyArchiveKind:
84   case SymbolBody::LazyObjectKind:
85     assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
86     return 0;
87   case SymbolBody::DefinedBitcodeKind:
88     llvm_unreachable("should have been replaced");
89   }
90   llvm_unreachable("invalid symbol kind");
91 }
92 
93 SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
94                        uint8_t Type)
95     : SymbolKind(K), IsLocal(true), Type(Type), StOther(StOther),
96       NameOffset(NameOffset) {
97   init();
98 }
99 
100 SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
101     : SymbolKind(K), IsLocal(false), Type(Type), StOther(StOther),
102       Name({Name.data(), Name.size()}) {
103   init();
104 }
105 
106 void SymbolBody::init() {
107   NeedsCopyOrPltAddr = false;
108 }
109 
110 // Returns true if a symbol can be replaced at load-time by a symbol
111 // with the same name defined in other ELF executable or DSO.
112 bool SymbolBody::isPreemptible() const {
113   if (isLocal())
114     return false;
115 
116   // Shared symbols resolve to the definition in the DSO. The exceptions are
117   // symbols with copy relocations (which resolve to .bss) or preempt plt
118   // entries (which resolve to that plt entry).
119   if (isShared())
120     return !NeedsCopyOrPltAddr;
121 
122   // That's all that can be preempted in a non-DSO.
123   if (!Config->Shared)
124     return false;
125 
126   // Only symbols that appear in dynsym can be preempted.
127   if (!symbol()->includeInDynsym())
128     return false;
129 
130   // Normally only default visibility symbols can be preempted, but -Bsymbolic
131   // means that not even they can be preempted.
132   if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
133     return !isDefined();
134   return symbol()->Visibility == STV_DEFAULT;
135 }
136 
137 template <class ELFT> InputFile *SymbolBody::getSourceFile() {
138   if (auto *S = dyn_cast<DefinedRegular<ELFT>>(this))
139     return S->Section ? S->Section->getFile() : nullptr;
140   if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
141     return S->File;
142   if (auto *S = dyn_cast<DefinedBitcode>(this))
143     return S->File;
144   if (auto *S = dyn_cast<Undefined>(this))
145     return S->File;
146   return nullptr;
147 }
148 
149 template <class ELFT>
150 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
151   typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
152   return OutVA + Addend;
153 }
154 
155 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
156   return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
157 }
158 
159 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
160   return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
161          sizeof(typename ELFT::uint);
162 }
163 
164 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
165   return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
166 }
167 
168 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
169   return GotPltIndex * sizeof(typename ELFT::uint);
170 }
171 
172 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
173   return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
174          PltIndex * Target->PltEntrySize;
175 }
176 
177 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
178   auto *D = cast<DefinedRegular<ELFT>>(this);
179   auto *S = cast<InputSection<ELFT>>(D->Section);
180   return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() +
181          ThunkIndex * Target->ThunkSize;
182 }
183 
184 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
185   if (const auto *C = dyn_cast<DefinedCommon>(this))
186     return C->Size;
187   if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
188     return DR->Size;
189   if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
190     return S->Sym.st_size;
191   return 0;
192 }
193 
194 Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
195     : SymbolBody(K, Name, StOther, Type) {}
196 
197 Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
198     : SymbolBody(K, NameOffset, StOther, Type) {}
199 
200 DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
201                                BitcodeFile *F)
202     : Defined(DefinedBitcodeKind, Name, StOther, Type), File(F) {}
203 
204 bool DefinedBitcode::classof(const SymbolBody *S) {
205   return S->kind() == DefinedBitcodeKind;
206 }
207 
208 Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type)
209     : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {}
210 
211 Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type)
212     : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {}
213 
214 template <typename ELFT>
215 DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
216                                          OutputSectionBase<ELFT> *Section)
217     : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
218       Value(Value), Section(Section) {}
219 
220 DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
221                              uint8_t StOther, uint8_t Type)
222     : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
223       Alignment(Alignment), Size(Size) {}
224 
225 std::unique_ptr<InputFile> Lazy::getFile() {
226   if (auto *S = dyn_cast<LazyArchive>(this))
227     return S->getFile();
228   return cast<LazyObject>(this)->getFile();
229 }
230 
231 std::unique_ptr<InputFile> LazyArchive::getFile() {
232   MemoryBufferRef MBRef = File->getMember(&Sym);
233 
234   // getMember returns an empty buffer if the member was already
235   // read from the library.
236   if (MBRef.getBuffer().empty())
237     return std::unique_ptr<InputFile>(nullptr);
238   return createObjectFile(MBRef, File->getName());
239 }
240 
241 std::unique_ptr<InputFile> LazyObject::getFile() {
242   return createObjectFile(MBRef);
243 }
244 
245 // Returns the demangled C++ symbol name for Name.
246 std::string elf::demangle(StringRef Name) {
247 #if !defined(HAVE_CXXABI_H)
248   return Name;
249 #else
250   if (!Config->Demangle)
251     return Name;
252 
253   // __cxa_demangle can be used to demangle strings other than symbol
254   // names which do not necessarily start with "_Z". Name can be
255   // either a C or C++ symbol. Don't call __cxa_demangle if the name
256   // does not look like a C++ symbol name to avoid getting unexpected
257   // result for a C symbol that happens to match a mangled type name.
258   if (!Name.startswith("_Z"))
259     return Name;
260 
261   char *Buf =
262       abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
263   if (!Buf)
264     return Name;
265   std::string S(Buf);
266   free(Buf);
267   return S;
268 #endif
269 }
270 
271 bool Symbol::includeInDynsym() const {
272   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
273     return false;
274   return (ExportDynamic && VersionScriptGlobal) || body()->isShared() ||
275          (body()->isUndefined() && Config->Shared);
276 }
277 
278 template InputFile *SymbolBody::template getSourceFile<ELF32LE>();
279 template InputFile *SymbolBody::template getSourceFile<ELF32BE>();
280 template InputFile *SymbolBody::template getSourceFile<ELF64LE>();
281 template InputFile *SymbolBody::template getSourceFile<ELF64BE>();
282 
283 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
284 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
285 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
286 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
287 
288 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
289 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
290 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
291 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
292 
293 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
294 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
295 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
296 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
297 
298 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
299 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
300 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
301 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
302 
303 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
304 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
305 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
306 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
307 
308 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
309 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
310 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
311 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
312 
313 template uint32_t SymbolBody::template getSize<ELF32LE>() const;
314 template uint32_t SymbolBody::template getSize<ELF32BE>() const;
315 template uint64_t SymbolBody::template getSize<ELF64LE>() const;
316 template uint64_t SymbolBody::template getSize<ELF64BE>() const;
317 
318 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
319 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
320 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
321 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
322 
323 template class elf::DefinedSynthetic<ELF32LE>;
324 template class elf::DefinedSynthetic<ELF32BE>;
325 template class elf::DefinedSynthetic<ELF64LE>;
326 template class elf::DefinedSynthetic<ELF64BE>;
327