xref: /llvm-project-15.0.7/lld/ELF/Symbols.cpp (revision 1c16d1b5)
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 
19 using namespace llvm;
20 using namespace llvm::object;
21 using namespace llvm::ELF;
22 
23 using namespace lld;
24 using namespace lld::elf;
25 
26 template <class ELFT>
27 static typename ELFT::uint getSymVA(const SymbolBody &Body,
28                                     typename ELFT::uint &Addend) {
29   typedef typename ELFT::uint uintX_t;
30 
31   switch (Body.kind()) {
32   case SymbolBody::DefinedSyntheticKind: {
33     auto &D = cast<DefinedSynthetic<ELFT>>(Body);
34     const OutputSectionBase<ELFT> *Sec = D.Section;
35     if (!Sec)
36       return D.Value;
37     if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
38       return Sec->getVA() + Sec->getSize();
39     return Sec->getVA() + D.Value;
40   }
41   case SymbolBody::DefinedRegularKind: {
42     auto &D = cast<DefinedRegular<ELFT>>(Body);
43     InputSectionBase<ELFT> *SC = D.Section;
44 
45     // According to the ELF spec reference to a local symbol from outside
46     // the group are not allowed. Unfortunately .eh_frame breaks that rule
47     // and must be treated specially. For now we just replace the symbol with
48     // 0.
49     if (SC == &InputSection<ELFT>::Discarded)
50       return 0;
51 
52     // This is an absolute symbol.
53     if (!SC)
54       return D.Value;
55 
56     uintX_t Offset = D.Value;
57     if (D.isSection()) {
58       Offset += Addend;
59       Addend = 0;
60     }
61     uintX_t VA = (SC->OutSec ? SC->OutSec->getVA() : 0) + SC->getOffset(Offset);
62     if (D.isTls())
63       return VA - Out<ELFT>::TlsPhdr->p_vaddr;
64     return VA;
65   }
66   case SymbolBody::DefinedCommonKind:
67     return CommonInputSection<ELFT>::X->OutSec->getVA() +
68            CommonInputSection<ELFT>::X->OutSecOff +
69            cast<DefinedCommon>(Body).Offset;
70   case SymbolBody::SharedKind: {
71     auto &SS = cast<SharedSymbol<ELFT>>(Body);
72     if (!SS.NeedsCopyOrPltAddr)
73       return 0;
74     if (SS.isFunc())
75       return Body.getPltVA<ELFT>();
76     return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
77   }
78   case SymbolBody::UndefinedKind:
79     return 0;
80   case SymbolBody::LazyArchiveKind:
81   case SymbolBody::LazyObjectKind:
82     assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
83     return 0;
84   }
85   llvm_unreachable("invalid symbol kind");
86 }
87 
88 SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
89                        uint8_t Type)
90     : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(true),
91       IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
92       NameOffset(NameOffset) {}
93 
94 SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
95     : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(false),
96       IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
97       Name({Name.data(), Name.size()}) {}
98 
99 StringRef SymbolBody::getName() const {
100   assert(!isLocal());
101   return StringRef(Name.S, Name.Len);
102 }
103 
104 // Returns true if a symbol can be replaced at load-time by a symbol
105 // with the same name defined in other ELF executable or DSO.
106 bool SymbolBody::isPreemptible() const {
107   if (isLocal())
108     return false;
109 
110   // Shared symbols resolve to the definition in the DSO. The exceptions are
111   // symbols with copy relocations (which resolve to .bss) or preempt plt
112   // entries (which resolve to that plt entry).
113   if (isShared())
114     return !NeedsCopyOrPltAddr;
115 
116   // That's all that can be preempted in a non-DSO.
117   if (!Config->Shared)
118     return false;
119 
120   // Only symbols that appear in dynsym can be preempted.
121   if (!symbol()->includeInDynsym())
122     return false;
123 
124   // Only default visibility symbols can be preempted.
125   if (symbol()->Visibility != STV_DEFAULT)
126     return false;
127 
128   // -Bsymbolic means that definitions are not preempted.
129   if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
130     return !isDefined();
131   return true;
132 }
133 
134 template <class ELFT> bool SymbolBody::hasThunk() const {
135   if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
136     return DR->ThunkData != nullptr;
137   if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
138     return S->ThunkData != nullptr;
139   return false;
140 }
141 
142 template <class ELFT>
143 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
144   typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
145   return OutVA + Addend;
146 }
147 
148 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
149   return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
150 }
151 
152 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
153   return GotIndex * Target->GotEntrySize;
154 }
155 
156 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
157   return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
158 }
159 
160 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
161   return GotPltIndex * Target->GotPltEntrySize;
162 }
163 
164 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
165   return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
166          PltIndex * Target->PltEntrySize;
167 }
168 
169 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
170   if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
171     return DR->ThunkData->getVA();
172   if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
173     return S->ThunkData->getVA();
174   fatal("getThunkVA() not supported for Symbol class\n");
175 }
176 
177 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
178   if (const auto *C = dyn_cast<DefinedCommon>(this))
179     return C->Size;
180   if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
181     return DR->Size;
182   if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
183     return S->Sym.st_size;
184   return 0;
185 }
186 
187 Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
188     : SymbolBody(K, Name, StOther, Type) {}
189 
190 Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
191     : SymbolBody(K, NameOffset, StOther, Type) {}
192 
193 Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type,
194                      InputFile *File)
195     : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
196   this->File = File;
197 }
198 
199 Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
200                      InputFile *File)
201     : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {
202   this->File = File;
203 }
204 
205 template <typename ELFT>
206 DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
207                                          OutputSectionBase<ELFT> *Section)
208     : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
209       Value(Value), Section(Section) {}
210 
211 DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
212                              uint8_t StOther, uint8_t Type, InputFile *File)
213     : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
214       Alignment(Alignment), Size(Size) {
215   this->File = File;
216 }
217 
218 InputFile *Lazy::fetch() {
219   if (auto *S = dyn_cast<LazyArchive>(this))
220     return S->fetch();
221   return cast<LazyObject>(this)->fetch();
222 }
223 
224 LazyArchive::LazyArchive(ArchiveFile &File,
225                          const llvm::object::Archive::Symbol S, uint8_t Type)
226     : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
227   this->File = &File;
228 }
229 
230 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
231     : Lazy(LazyObjectKind, Name, Type) {
232   this->File = &File;
233 }
234 
235 InputFile *LazyArchive::fetch() {
236   MemoryBufferRef MBRef = file()->getMember(&Sym);
237 
238   // getMember returns an empty buffer if the member was already
239   // read from the library.
240   if (MBRef.getBuffer().empty())
241     return nullptr;
242   return createObjectFile(MBRef, file()->getName());
243 }
244 
245 InputFile *LazyObject::fetch() {
246   MemoryBufferRef MBRef = file()->getBuffer();
247   if (MBRef.getBuffer().empty())
248     return nullptr;
249   return createObjectFile(MBRef);
250 }
251 
252 bool Symbol::includeInDynsym() const {
253   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
254     return false;
255   return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
256          (body()->isUndefined() && Config->Shared);
257 }
258 
259 // Print out a log message for --trace-symbol.
260 void elf::printTraceSymbol(Symbol *Sym) {
261   SymbolBody *B = Sym->body();
262   outs() << getFilename(B->File);
263 
264   if (B->isUndefined())
265     outs() << ": reference to ";
266   else if (B->isCommon())
267     outs() << ": common definition of ";
268   else
269     outs() << ": definition of ";
270   outs() << B->getName() << "\n";
271 }
272 
273 template bool SymbolBody::hasThunk<ELF32LE>() const;
274 template bool SymbolBody::hasThunk<ELF32BE>() const;
275 template bool SymbolBody::hasThunk<ELF64LE>() const;
276 template bool SymbolBody::hasThunk<ELF64BE>() const;
277 
278 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
279 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
280 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
281 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
282 
283 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
284 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
285 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
286 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
287 
288 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
289 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
290 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
291 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
292 
293 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
294 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
295 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
296 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
297 
298 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
299 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
300 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
301 template uint64_t SymbolBody::template getThunkVA<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 class elf::DefinedSynthetic<ELF32LE>;
319 template class elf::DefinedSynthetic<ELF32BE>;
320 template class elf::DefinedSynthetic<ELF64LE>;
321 template class elf::DefinedSynthetic<ELF64BE>;
322