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 Out<ELFT>::Bss->Addr + SS.OffsetInBss;
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), Type(Type), StOther(StOther), Name(Name) {}
102 // Returns true if a symbol can be replaced at load-time by a symbol
103 // with the same name defined in other ELF executable or DSO.
104 bool SymbolBody::isPreemptible() const {
108 // Shared symbols resolve to the definition in the DSO. The exceptions are
109 // symbols with copy relocations (which resolve to .bss) or preempt plt
110 // entries (which resolve to that plt entry).
112 return !NeedsCopyOrPltAddr;
114 // That's all that can be preempted in a non-DSO.
118 // Only symbols that appear in dynsym can be preempted.
119 if (!symbol()->includeInDynsym())
122 // Only default visibility symbols can be preempted.
123 if (symbol()->Visibility != STV_DEFAULT)
126 // -Bsymbolic means that definitions are not preempted.
127 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
132 template <class ELFT> bool SymbolBody::hasThunk() const {
133 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
134 return DR->ThunkData != nullptr;
135 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
136 return S->ThunkData != nullptr;
140 template <class ELFT>
141 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
142 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
143 return OutVA + Addend;
146 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
147 return In<ELFT>::Got->getVA() + getGotOffset<ELFT>();
150 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
151 return GotIndex * Target->GotEntrySize;
154 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
156 return In<ELFT>::IgotPlt->getVA() + getGotPltOffset<ELFT>();
157 return In<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
160 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
161 return GotPltIndex * Target->GotPltEntrySize;
164 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
166 return In<ELFT>::Iplt->getVA() + PltIndex * Target->PltEntrySize;
167 return In<ELFT>::Plt->getVA() + Target->PltHeaderSize +
168 PltIndex * Target->PltEntrySize;
171 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
172 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
173 return DR->ThunkData->getVA();
174 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
175 return S->ThunkData->getVA();
176 fatal("getThunkVA() not supported for Symbol class\n");
179 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
180 if (const auto *C = dyn_cast<DefinedCommon>(this))
182 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
184 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
185 return S->Sym.st_size;
189 // If a symbol name contains '@', the characters after that is
190 // a symbol version name. This function parses that.
191 void SymbolBody::parseSymbolVersion() {
192 StringRef S = getName();
193 size_t Pos = S.find('@');
194 if (Pos == 0 || Pos == StringRef::npos)
196 StringRef Verstr = S.substr(Pos + 1);
200 // Truncate the symbol name so that it doesn't include the version string.
201 Name = {S.data(), Pos};
203 // '@@' in a symbol name means the default version.
204 // It is usually the most recent one.
205 bool IsDefault = (Verstr[0] == '@');
207 Verstr = Verstr.substr(1);
209 for (VersionDefinition &Ver : Config->VersionDefinitions) {
210 if (Ver.Name != Verstr)
214 symbol()->VersionId = Ver.Id;
216 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
220 // It is an error if the specified version is not defined.
221 error(toString(File) + ": symbol " + S + " has undefined version " + Verstr);
224 Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
226 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
228 template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const {
229 if (!Section || !isFunc())
231 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
232 (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC);
235 Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
236 uint8_t Type, InputFile *File)
237 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {
241 DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint64_t Alignment,
242 uint8_t StOther, uint8_t Type, InputFile *File)
243 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
245 Alignment(Alignment), Size(Size) {
249 InputFile *Lazy::fetch() {
250 if (auto *S = dyn_cast<LazyArchive>(this))
252 return cast<LazyObject>(this)->fetch();
255 LazyArchive::LazyArchive(ArchiveFile &File,
256 const llvm::object::Archive::Symbol S, uint8_t Type)
257 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
261 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
262 : Lazy(LazyObjectKind, Name, Type) {
266 InputFile *LazyArchive::fetch() {
267 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
269 // getMember returns an empty buffer if the member was already
270 // read from the library.
271 if (MBInfo.first.getBuffer().empty())
273 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
276 InputFile *LazyObject::fetch() {
277 MemoryBufferRef MBRef = file()->getBuffer();
278 if (MBRef.getBuffer().empty())
280 return createObjectFile(MBRef);
283 bool Symbol::includeInDynsym() const {
284 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
286 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
287 (body()->isUndefined() && Config->Shared);
290 // Print out a log message for --trace-symbol.
291 void elf::printTraceSymbol(Symbol *Sym) {
292 SymbolBody *B = Sym->body();
293 outs() << toString(B->File);
295 if (B->isUndefined())
296 outs() << ": reference to ";
297 else if (B->isCommon())
298 outs() << ": common definition of ";
300 outs() << ": definition of ";
301 outs() << B->getName() << "\n";
304 // Returns a symbol for an error message.
305 std::string elf::toString(const SymbolBody &B) {
306 if (Config->Demangle)
307 if (Optional<std::string> S = demangle(B.getName()))
312 template bool SymbolBody::hasThunk<ELF32LE>() const;
313 template bool SymbolBody::hasThunk<ELF32BE>() const;
314 template bool SymbolBody::hasThunk<ELF64LE>() const;
315 template bool SymbolBody::hasThunk<ELF64BE>() const;
317 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
318 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
319 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
320 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
322 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
323 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
324 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
325 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
327 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
328 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
329 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
330 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
332 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
333 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
334 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
335 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
337 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
338 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
339 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
340 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
342 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
343 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
344 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
345 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
347 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
348 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
349 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
350 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
352 template uint32_t SymbolBody::template getSize<ELF32LE>() const;
353 template uint32_t SymbolBody::template getSize<ELF32BE>() const;
354 template uint64_t SymbolBody::template getSize<ELF64LE>() const;
355 template uint64_t SymbolBody::template getSize<ELF64BE>() const;
357 template class elf::DefinedRegular<ELF32LE>;
358 template class elf::DefinedRegular<ELF32BE>;
359 template class elf::DefinedRegular<ELF64LE>;
360 template class elf::DefinedRegular<ELF64BE>;