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 return In<ELFT>::Common->OutSec->Addr + In<ELFT>::Common->OutSecOff +
77 cast<DefinedCommon>(Body).Offset;
78 case SymbolBody::SharedKind: {
79 auto &SS = cast<SharedSymbol<ELFT>>(Body);
80 if (!SS.NeedsCopyOrPltAddr)
83 return Body.getPltVA<ELFT>();
84 return SS.getBssSectionForCopy()->Addr + SS.CopyOffset;
86 case SymbolBody::UndefinedKind:
88 case SymbolBody::LazyArchiveKind:
89 case SymbolBody::LazyObjectKind:
90 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
93 llvm_unreachable("invalid symbol kind");
96 SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
98 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(IsLocal),
99 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
100 IsInIgot(false), CopyIsInBssRelRo(false), Type(Type), StOther(StOther),
103 // Returns true if a symbol can be replaced at load-time by a symbol
104 // with the same name defined in other ELF executable or DSO.
105 bool SymbolBody::isPreemptible() const {
109 // Shared symbols resolve to the definition in the DSO. The exceptions are
110 // symbols with copy relocations (which resolve to .bss) or preempt plt
111 // entries (which resolve to that plt entry).
113 return !NeedsCopyOrPltAddr;
115 // That's all that can be preempted in a non-DSO.
119 // Only symbols that appear in dynsym can be preempted.
120 if (!symbol()->includeInDynsym())
123 // Only default visibility symbols can be preempted.
124 if (symbol()->Visibility != STV_DEFAULT)
127 // -Bsymbolic means that definitions are not preempted.
128 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
133 template <class ELFT> bool SymbolBody::hasThunk() const {
134 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
135 return DR->ThunkData != nullptr;
136 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
137 return S->ThunkData != nullptr;
141 template <class ELFT>
142 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
143 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
144 return OutVA + Addend;
147 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
148 return In<ELFT>::Got->getVA() + getGotOffset<ELFT>();
151 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
152 return GotIndex * Target->GotEntrySize;
155 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
157 return In<ELFT>::IgotPlt->getVA() + getGotPltOffset<ELFT>();
158 return In<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
161 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
162 return GotPltIndex * Target->GotPltEntrySize;
165 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
167 return In<ELFT>::Iplt->getVA() + PltIndex * Target->PltEntrySize;
168 return In<ELFT>::Plt->getVA() + Target->PltHeaderSize +
169 PltIndex * Target->PltEntrySize;
172 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
173 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
174 return DR->ThunkData->getVA();
175 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
176 return S->ThunkData->getVA();
177 if (const auto *S = dyn_cast<Undefined<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 // If a symbol name contains '@', the characters after that is
193 // a symbol version name. This function parses that.
194 void SymbolBody::parseSymbolVersion() {
195 StringRef S = getName();
196 size_t Pos = S.find('@');
197 if (Pos == 0 || Pos == StringRef::npos)
199 StringRef Verstr = S.substr(Pos + 1);
203 // Truncate the symbol name so that it doesn't include the version string.
204 Name = {S.data(), Pos};
206 // If this is not in this DSO, it is not a definition.
207 if (!isInCurrentDSO())
210 // '@@' in a symbol name means the default version.
211 // It is usually the most recent one.
212 bool IsDefault = (Verstr[0] == '@');
214 Verstr = Verstr.substr(1);
216 for (VersionDefinition &Ver : Config->VersionDefinitions) {
217 if (Ver.Name != Verstr)
221 symbol()->VersionId = Ver.Id;
223 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
227 // It is an error if the specified version is not defined.
228 error(toString(File) + ": symbol " + S + " has undefined version " + Verstr);
231 Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
233 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
235 template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const {
236 if (!Section || !isFunc())
238 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
239 (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC);
242 template <typename ELFT>
243 Undefined<ELFT>::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
244 uint8_t Type, InputFile *File)
245 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {
249 template <typename ELFT>
250 OutputSection<ELFT> *SharedSymbol<ELFT>::getBssSectionForCopy() const {
252 return CopyIsInBssRelRo ? Out<ELFT>::BssRelRo : Out<ELFT>::Bss;
255 DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint64_t Alignment,
256 uint8_t StOther, uint8_t Type, InputFile *File)
257 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
259 Alignment(Alignment), Size(Size) {
263 InputFile *Lazy::fetch() {
264 if (auto *S = dyn_cast<LazyArchive>(this))
266 return cast<LazyObject>(this)->fetch();
269 LazyArchive::LazyArchive(ArchiveFile &File,
270 const llvm::object::Archive::Symbol S, uint8_t Type)
271 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
275 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
276 : Lazy(LazyObjectKind, Name, Type) {
280 InputFile *LazyArchive::fetch() {
281 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
283 // getMember returns an empty buffer if the member was already
284 // read from the library.
285 if (MBInfo.first.getBuffer().empty())
287 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
290 InputFile *LazyObject::fetch() {
291 MemoryBufferRef MBRef = file()->getBuffer();
292 if (MBRef.getBuffer().empty())
294 return createObjectFile(MBRef);
297 uint8_t Symbol::computeBinding() const {
298 if (Config->Relocatable)
300 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
302 const SymbolBody *Body = body();
303 if (VersionId == VER_NDX_LOCAL && Body->isInCurrentDSO())
305 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
310 bool Symbol::includeInDynsym() const {
311 if (computeBinding() == STB_LOCAL)
313 return ExportDynamic || body()->isShared() ||
314 (body()->isUndefined() && Config->Shared);
317 // Print out a log message for --trace-symbol.
318 void elf::printTraceSymbol(Symbol *Sym) {
319 SymbolBody *B = Sym->body();
320 outs() << toString(B->File);
322 if (B->isUndefined())
323 outs() << ": reference to ";
324 else if (B->isCommon())
325 outs() << ": common definition of ";
327 outs() << ": definition of ";
328 outs() << B->getName() << "\n";
331 // Returns a symbol for an error message.
332 std::string lld::toString(const SymbolBody &B) {
333 if (Config->Demangle)
334 if (Optional<std::string> S = demangle(B.getName()))
339 template bool SymbolBody::hasThunk<ELF32LE>() const;
340 template bool SymbolBody::hasThunk<ELF32BE>() const;
341 template bool SymbolBody::hasThunk<ELF64LE>() const;
342 template bool SymbolBody::hasThunk<ELF64BE>() const;
344 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
345 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
346 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
347 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
349 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
350 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
351 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
352 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
354 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
355 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
356 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
357 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
359 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
360 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
361 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
362 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
364 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
365 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
366 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
367 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
369 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
370 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
371 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
372 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
374 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
375 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
376 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
377 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
379 template uint32_t SymbolBody::template getSize<ELF32LE>() const;
380 template uint32_t SymbolBody::template getSize<ELF32BE>() const;
381 template uint64_t SymbolBody::template getSize<ELF64LE>() const;
382 template uint64_t SymbolBody::template getSize<ELF64BE>() const;
384 template class elf::Undefined<ELF32LE>;
385 template class elf::Undefined<ELF32BE>;
386 template class elf::Undefined<ELF64LE>;
387 template class elf::Undefined<ELF64BE>;
389 template class elf::SharedSymbol<ELF32LE>;
390 template class elf::SharedSymbol<ELF32BE>;
391 template class elf::SharedSymbol<ELF64LE>;
392 template class elf::SharedSymbol<ELF64BE>;
394 template class elf::DefinedRegular<ELF32LE>;
395 template class elf::DefinedRegular<ELF32BE>;
396 template class elf::DefinedRegular<ELF64LE>;
397 template class elf::DefinedRegular<ELF64BE>;