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"
16 #include "SyntheticSections.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/Path.h"
25 using namespace llvm::object;
26 using namespace llvm::ELF;
29 using namespace lld::elf;
32 static typename ELFT::uint getSymVA(const SymbolBody &Body,
33 typename ELFT::uint &Addend) {
34 typedef typename ELFT::uint uintX_t;
36 switch (Body.kind()) {
37 case SymbolBody::DefinedSyntheticKind: {
38 auto &D = cast<DefinedSynthetic>(Body);
39 const OutputSectionBase *Sec = D.Section;
42 if (D.Value == uintX_t(-1))
43 return Sec->Addr + Sec->Size;
44 return Sec->Addr + D.Value;
46 case SymbolBody::DefinedRegularKind: {
47 auto &D = cast<DefinedRegular<ELFT>>(Body);
48 InputSectionBase<ELFT> *IS = D.Section;
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
54 if (IS == &InputSection<ELFT>::Discarded)
57 // This is an absolute symbol.
61 uintX_t Offset = D.Value;
66 uintX_t VA = (IS->OutSec ? IS->OutSec->Addr : 0) + IS->getOffset(Offset);
67 if (D.isTls() && !Config->Relocatable) {
68 if (!Out<ELFT>::TlsPhdr)
69 fatal(toString(D.File) +
70 " has a STT_TLS symbol but doesn't have a PT_TLS section");
71 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
75 case SymbolBody::DefinedCommonKind:
76 if (!Config->DefineCommon)
78 return In<ELFT>::Common->OutSec->Addr + In<ELFT>::Common->OutSecOff +
79 cast<DefinedCommon>(Body).Offset;
80 case SymbolBody::SharedKind: {
81 auto &SS = cast<SharedSymbol<ELFT>>(Body);
82 if (!SS.NeedsCopyOrPltAddr)
85 return Body.getPltVA<ELFT>();
86 return SS.getBssSectionForCopy()->Addr + SS.CopyOffset;
88 case SymbolBody::UndefinedKind:
90 case SymbolBody::LazyArchiveKind:
91 case SymbolBody::LazyObjectKind:
92 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
95 llvm_unreachable("invalid symbol kind");
98 SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
100 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(IsLocal),
101 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
102 IsInIgot(false), CopyIsInBssRelRo(false), Type(Type), StOther(StOther),
105 // Returns true if a symbol can be replaced at load-time by a symbol
106 // with the same name defined in other ELF executable or DSO.
107 bool SymbolBody::isPreemptible() const {
111 // Shared symbols resolve to the definition in the DSO. The exceptions are
112 // symbols with copy relocations (which resolve to .bss) or preempt plt
113 // entries (which resolve to that plt entry).
115 return !NeedsCopyOrPltAddr;
117 // That's all that can be preempted in a non-DSO.
121 // Only symbols that appear in dynsym can be preempted.
122 if (!symbol()->includeInDynsym())
125 // Only default visibility symbols can be preempted.
126 if (symbol()->Visibility != STV_DEFAULT)
129 // -Bsymbolic means that definitions are not preempted.
130 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
135 template <class ELFT> bool SymbolBody::hasThunk() const {
136 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
137 return DR->ThunkData != nullptr;
138 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
139 return S->ThunkData != nullptr;
143 template <class ELFT>
144 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
145 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
146 return OutVA + Addend;
149 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
150 return In<ELFT>::Got->getVA() + getGotOffset<ELFT>();
153 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
154 return GotIndex * Target->GotEntrySize;
157 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
159 return In<ELFT>::IgotPlt->getVA() + getGotPltOffset<ELFT>();
160 return In<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
163 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
164 return GotPltIndex * Target->GotPltEntrySize;
167 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
169 return In<ELFT>::Iplt->getVA() + PltIndex * Target->PltEntrySize;
170 return In<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 if (const auto *S = dyn_cast<Undefined<ELFT>>(this))
180 return S->ThunkData->getVA();
181 fatal("getThunkVA() not supported for Symbol class\n");
184 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
185 if (const auto *C = dyn_cast<DefinedCommon>(this))
187 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
189 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
190 return S->Sym.st_size;
194 // If a symbol name contains '@', the characters after that is
195 // a symbol version name. This function parses that.
196 void SymbolBody::parseSymbolVersion() {
197 StringRef S = getName();
198 size_t Pos = S.find('@');
199 if (Pos == 0 || Pos == StringRef::npos)
201 StringRef Verstr = S.substr(Pos + 1);
205 // Truncate the symbol name so that it doesn't include the version string.
206 Name = {S.data(), Pos};
208 // If this is not in this DSO, it is not a definition.
209 if (!isInCurrentDSO())
212 // '@@' in a symbol name means the default version.
213 // It is usually the most recent one.
214 bool IsDefault = (Verstr[0] == '@');
216 Verstr = Verstr.substr(1);
218 for (VersionDefinition &Ver : Config->VersionDefinitions) {
219 if (Ver.Name != Verstr)
223 symbol()->VersionId = Ver.Id;
225 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
229 // It is an error if the specified version is not defined.
230 error(toString(File) + ": symbol " + S + " has undefined version " + Verstr);
233 Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
235 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
237 template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const {
238 if (!Section || !isFunc())
240 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
241 (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC);
244 template <typename ELFT>
245 Undefined<ELFT>::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
246 uint8_t Type, InputFile *File)
247 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {
251 template <typename ELFT>
252 OutputSection<ELFT> *SharedSymbol<ELFT>::getBssSectionForCopy() const {
254 return CopyIsInBssRelRo ? Out<ELFT>::BssRelRo : Out<ELFT>::Bss;
257 DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint64_t Alignment,
258 uint8_t StOther, uint8_t Type, InputFile *File)
259 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
261 Alignment(Alignment), Size(Size) {
265 InputFile *Lazy::fetch() {
266 if (auto *S = dyn_cast<LazyArchive>(this))
268 return cast<LazyObject>(this)->fetch();
271 LazyArchive::LazyArchive(ArchiveFile &File,
272 const llvm::object::Archive::Symbol S, uint8_t Type)
273 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
277 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
278 : Lazy(LazyObjectKind, Name, Type) {
282 InputFile *LazyArchive::fetch() {
283 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
285 // getMember returns an empty buffer if the member was already
286 // read from the library.
287 if (MBInfo.first.getBuffer().empty())
289 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
292 InputFile *LazyObject::fetch() {
293 MemoryBufferRef MBRef = file()->getBuffer();
294 if (MBRef.getBuffer().empty())
296 return createObjectFile(MBRef);
299 uint8_t Symbol::computeBinding() const {
300 if (Config->Relocatable)
302 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
304 const SymbolBody *Body = body();
305 if (VersionId == VER_NDX_LOCAL && Body->isInCurrentDSO())
307 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
312 bool Symbol::includeInDynsym() const {
313 if (computeBinding() == STB_LOCAL)
315 return ExportDynamic || body()->isShared() ||
316 (body()->isUndefined() && Config->Shared);
319 // Print out a log message for --trace-symbol.
320 void elf::printTraceSymbol(Symbol *Sym) {
321 SymbolBody *B = Sym->body();
322 outs() << toString(B->File);
324 if (B->isUndefined())
325 outs() << ": reference to ";
326 else if (B->isCommon())
327 outs() << ": common definition of ";
329 outs() << ": definition of ";
330 outs() << B->getName() << "\n";
333 // Returns a symbol for an error message.
334 std::string lld::toString(const SymbolBody &B) {
335 if (Config->Demangle)
336 if (Optional<std::string> S = demangle(B.getName()))
341 template bool SymbolBody::hasThunk<ELF32LE>() const;
342 template bool SymbolBody::hasThunk<ELF32BE>() const;
343 template bool SymbolBody::hasThunk<ELF64LE>() const;
344 template bool SymbolBody::hasThunk<ELF64BE>() const;
346 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
347 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
348 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
349 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
351 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
352 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
353 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
354 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
356 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
357 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
358 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
359 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
361 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
362 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
363 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
364 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
366 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
367 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
368 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
369 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
371 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
372 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
373 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
374 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
376 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
377 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
378 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
379 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
381 template uint32_t SymbolBody::template getSize<ELF32LE>() const;
382 template uint32_t SymbolBody::template getSize<ELF32BE>() const;
383 template uint64_t SymbolBody::template getSize<ELF64LE>() const;
384 template uint64_t SymbolBody::template getSize<ELF64BE>() const;
386 template class elf::Undefined<ELF32LE>;
387 template class elf::Undefined<ELF32BE>;
388 template class elf::Undefined<ELF64LE>;
389 template class elf::Undefined<ELF64BE>;
391 template class elf::SharedSymbol<ELF32LE>;
392 template class elf::SharedSymbol<ELF32BE>;
393 template class elf::SharedSymbol<ELF64LE>;
394 template class elf::SharedSymbol<ELF64BE>;
396 template class elf::DefinedRegular<ELF32LE>;
397 template class elf::DefinedRegular<ELF32BE>;
398 template class elf::DefinedRegular<ELF64LE>;
399 template class elf::DefinedRegular<ELF64BE>;