1 //===- Symbols.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "InputFiles.h"
13 #include "InputSection.h"
14 #include "OutputSections.h"
17 #include "llvm/ADT/STLExtras.h"
20 using namespace llvm::object;
21 using namespace llvm::ELF;
24 using namespace lld::elf;
27 static typename ELFT::uint getSymVA(const SymbolBody &Body,
28 typename ELFT::uint &Addend) {
29 typedef typename ELFT::uint uintX_t;
31 switch (Body.kind()) {
32 case SymbolBody::DefinedSyntheticKind: {
33 auto &D = cast<DefinedSynthetic<ELFT>>(Body);
34 const OutputSectionBase<ELFT> *Sec = D.Section;
37 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
38 return Sec->getVA() + Sec->getSize();
39 return Sec->getVA() + D.Value;
41 case SymbolBody::DefinedRegularKind: {
42 auto &D = cast<DefinedRegular<ELFT>>(Body);
43 InputSectionBase<ELFT> *SC = D.Section;
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
49 if (SC == &InputSection<ELFT>::Discarded)
52 // This is an absolute symbol.
56 uintX_t Offset = D.Value;
61 uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
63 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
66 case SymbolBody::DefinedCommonKind:
67 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
68 case SymbolBody::SharedKind: {
69 auto &SS = cast<SharedSymbol<ELFT>>(Body);
70 if (!SS.NeedsCopyOrPltAddr)
73 return Body.getPltVA<ELFT>();
74 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
76 case SymbolBody::UndefinedKind:
78 case SymbolBody::LazyArchiveKind:
79 case SymbolBody::LazyObjectKind:
80 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
82 case SymbolBody::DefinedBitcodeKind:
83 llvm_unreachable("should have been replaced");
85 llvm_unreachable("invalid symbol kind");
88 SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
90 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(true),
91 IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
92 NameOffset(NameOffset) {}
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()}) {}
99 StringRef SymbolBody::getName() const {
101 return StringRef(Name.S, Name.Len);
104 void SymbolBody::setName(StringRef S) {
109 // Returns true if a symbol can be replaced at load-time by a symbol
110 // with the same name defined in other ELF executable or DSO.
111 bool SymbolBody::isPreemptible() const {
115 // Shared symbols resolve to the definition in the DSO. The exceptions are
116 // symbols with copy relocations (which resolve to .bss) or preempt plt
117 // entries (which resolve to that plt entry).
119 return !NeedsCopyOrPltAddr;
121 // That's all that can be preempted in a non-DSO.
125 // Only symbols that appear in dynsym can be preempted.
126 if (!symbol()->includeInDynsym())
129 // Only default visibility symbols can be preempted.
130 if (symbol()->Visibility != STV_DEFAULT)
133 // -Bsymbolic means that definitions are not preempted.
134 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
139 template <class ELFT> bool SymbolBody::hasThunk() const {
140 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
141 return DR->ThunkData != nullptr;
142 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
143 return S->ThunkData != nullptr;
147 template <class ELFT>
148 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
149 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
150 return OutVA + Addend;
153 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
154 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
157 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
158 return GotIndex * Target->GotEntrySize;
161 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
162 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
165 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
166 return GotPltIndex * Target->GotPltEntrySize;
169 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
170 return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
171 PltIndex * Target->PltEntrySize;
174 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
175 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
176 return DR->ThunkData->getVA();
177 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
178 return S->ThunkData->getVA();
179 fatal("getThunkVA() not supported for Symbol class\n");
182 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
183 if (const auto *C = dyn_cast<DefinedCommon>(this))
185 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
187 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
188 return S->Sym.st_size;
192 Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
193 : SymbolBody(K, Name, StOther, Type) {}
195 Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
196 : SymbolBody(K, NameOffset, StOther, Type) {}
198 DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
200 : Defined(DefinedBitcodeKind, Name, StOther, Type) {
204 bool DefinedBitcode::classof(const SymbolBody *S) {
205 return S->kind() == DefinedBitcodeKind;
208 Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type,
210 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
214 Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
216 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {
220 template <typename ELFT>
221 DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
222 OutputSectionBase<ELFT> *Section)
223 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
224 Value(Value), Section(Section) {}
226 DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
227 uint8_t StOther, uint8_t Type, InputFile *File)
228 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
229 Alignment(Alignment), Size(Size) {
233 std::unique_ptr<InputFile> Lazy::fetch() {
234 if (auto *S = dyn_cast<LazyArchive>(this))
236 return cast<LazyObject>(this)->fetch();
239 LazyArchive::LazyArchive(ArchiveFile &File,
240 const llvm::object::Archive::Symbol S, uint8_t Type)
241 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
245 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
246 : Lazy(LazyObjectKind, Name, Type) {
250 std::unique_ptr<InputFile> LazyArchive::fetch() {
251 MemoryBufferRef MBRef = file()->getMember(&Sym);
253 // getMember returns an empty buffer if the member was already
254 // read from the library.
255 if (MBRef.getBuffer().empty())
256 return std::unique_ptr<InputFile>(nullptr);
257 return createObjectFile(MBRef, file()->getName());
260 std::unique_ptr<InputFile> LazyObject::fetch() {
261 MemoryBufferRef MBRef = file()->getBuffer();
262 if (MBRef.getBuffer().empty())
263 return std::unique_ptr<InputFile>(nullptr);
264 return createObjectFile(MBRef);
267 bool Symbol::includeInDynsym() const {
268 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
270 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
271 (body()->isUndefined() && Config->Shared);
274 // Print out a log message for --trace-symbol.
275 void elf::printTraceSymbol(Symbol *Sym) {
276 SymbolBody *B = Sym->body();
277 outs() << getFilename(B->File);
279 if (B->isUndefined())
280 outs() << ": reference to ";
281 else if (B->isCommon())
282 outs() << ": common definition of ";
284 outs() << ": definition of ";
285 outs() << B->getName() << "\n";
288 template bool SymbolBody::hasThunk<ELF32LE>() const;
289 template bool SymbolBody::hasThunk<ELF32BE>() const;
290 template bool SymbolBody::hasThunk<ELF64LE>() const;
291 template bool SymbolBody::hasThunk<ELF64BE>() const;
293 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
294 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
295 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
296 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
298 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
299 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
300 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
301 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
303 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
304 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
305 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
306 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
308 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
309 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
310 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
311 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
313 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
314 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
315 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
316 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
318 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
319 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
320 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
321 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
323 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
324 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
325 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
326 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
328 template uint32_t SymbolBody::template getSize<ELF32LE>() const;
329 template uint32_t SymbolBody::template getSize<ELF32BE>() const;
330 template uint64_t SymbolBody::template getSize<ELF64LE>() const;
331 template uint64_t SymbolBody::template getSize<ELF64BE>() const;
333 template class elf::DefinedSynthetic<ELF32LE>;
334 template class elf::DefinedSynthetic<ELF32BE>;
335 template class elf::DefinedSynthetic<ELF64LE>;
336 template class elf::DefinedSynthetic<ELF64BE>;