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;
31 DefinedRegular *ElfSym::Bss;
32 DefinedRegular *ElfSym::Etext1;
33 DefinedRegular *ElfSym::Etext2;
34 DefinedRegular *ElfSym::Edata1;
35 DefinedRegular *ElfSym::Edata2;
36 DefinedRegular *ElfSym::End1;
37 DefinedRegular *ElfSym::End2;
38 DefinedRegular *ElfSym::GlobalOffsetTable;
39 DefinedRegular *ElfSym::MipsGp;
40 DefinedRegular *ElfSym::MipsGpDisp;
41 DefinedRegular *ElfSym::MipsLocalGp;
43 static uint64_t getSymVA(const SymbolBody &Body, int64_t &Addend) {
44 switch (Body.kind()) {
45 case SymbolBody::DefinedRegularKind: {
46 auto &D = cast<DefinedRegular>(Body);
47 SectionBase *IS = D.Section;
48 if (auto *ISB = dyn_cast_or_null<InputSectionBase>(IS))
51 // According to the ELF spec reference to a local symbol from outside
52 // the group are not allowed. Unfortunately .eh_frame breaks that rule
53 // and must be treated specially. For now we just replace the symbol with
55 if (IS == &InputSection::Discarded)
58 // This is an absolute symbol.
62 uint64_t Offset = D.Value;
64 // An object in an SHF_MERGE section might be referenced via a
65 // section symbol (as a hack for reducing the number of local
67 // Depending on the addend, the reference via a section symbol
68 // refers to a different object in the merge section.
69 // Since the objects in the merge section are not necessarily
70 // contiguous in the output, the addend can thus affect the final
71 // VA in a non-linear way.
72 // To make this work, we incorporate the addend into the section
73 // offset (and zero out the addend for later processing) so that
74 // we find the right object in the section.
80 const OutputSection *OutSec = IS->getOutputSection();
82 // In the typical case, this is actually very simple and boils
83 // down to adding together 3 numbers:
84 // 1. The address of the output section.
85 // 2. The offset of the input section within the output section.
86 // 3. The offset within the input section (this addition happens
87 // inside InputSection::getOffset).
89 // If you understand the data structures involved with this next
90 // line (and how they get built), then you have a pretty good
91 // understanding of the linker.
92 uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
94 if (D.isTls() && !Config->Relocatable) {
96 fatal(toString(D.File) +
97 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
98 return VA - Out::TlsPhdr->p_vaddr;
102 case SymbolBody::DefinedCommonKind:
103 if (!Config->DefineCommon)
105 return InX::Common->getParent()->Addr + InX::Common->OutSecOff +
106 cast<DefinedCommon>(Body).Offset;
107 case SymbolBody::SharedKind: {
108 auto &SS = cast<SharedSymbol>(Body);
110 return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff +
113 return Body.getPltVA();
116 case SymbolBody::UndefinedKind:
118 case SymbolBody::LazyArchiveKind:
119 case SymbolBody::LazyObjectKind:
120 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
123 llvm_unreachable("invalid symbol kind");
126 SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
128 : SymbolKind(K), NeedsCopy(false), NeedsPltAddr(false), IsLocal(IsLocal),
129 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
130 IsInIgot(false), Type(Type), StOther(StOther), Name(Name) {}
132 // Returns true if a symbol can be replaced at load-time by a symbol
133 // with the same name defined in other ELF executable or DSO.
134 bool SymbolBody::isPreemptible() const {
138 // Shared symbols resolve to the definition in the DSO. The exceptions are
139 // symbols with copy relocations (which resolve to .bss) or preempt plt
140 // entries (which resolve to that plt entry).
142 return !NeedsCopy && !NeedsPltAddr;
144 // That's all that can be preempted in a non-DSO.
148 // Only symbols that appear in dynsym can be preempted.
149 if (!symbol()->includeInDynsym())
152 // Only default visibility symbols can be preempted.
153 if (symbol()->Visibility != STV_DEFAULT)
156 // -Bsymbolic means that definitions are not preempted.
157 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
162 // Overwrites all attributes with Other's so that this symbol becomes
163 // an alias to Other. This is useful for handling some options such as
165 void SymbolBody::copy(SymbolBody *Other) {
166 memcpy(symbol()->Body.buffer, Other->symbol()->Body.buffer,
167 sizeof(Symbol::Body));
170 uint64_t SymbolBody::getVA(int64_t Addend) const {
171 uint64_t OutVA = getSymVA(*this, Addend);
172 return OutVA + Addend;
175 uint64_t SymbolBody::getGotVA() const {
176 return InX::Got->getVA() + getGotOffset();
179 uint64_t SymbolBody::getGotOffset() const {
180 return GotIndex * Target->GotEntrySize;
183 uint64_t SymbolBody::getGotPltVA() const {
185 return InX::IgotPlt->getVA() + getGotPltOffset();
186 return InX::GotPlt->getVA() + getGotPltOffset();
189 uint64_t SymbolBody::getGotPltOffset() const {
190 return GotPltIndex * Target->GotPltEntrySize;
193 uint64_t SymbolBody::getPltVA() const {
195 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
196 return InX::Plt->getVA() + Target->PltHeaderSize +
197 PltIndex * Target->PltEntrySize;
200 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
201 if (const auto *C = dyn_cast<DefinedCommon>(this))
203 if (const auto *DR = dyn_cast<DefinedRegular>(this))
205 if (const auto *S = dyn_cast<SharedSymbol>(this))
206 return S->getSize<ELFT>();
210 OutputSection *SymbolBody::getOutputSection() const {
211 if (auto *S = dyn_cast<DefinedRegular>(this)) {
213 return S->Section->getOutputSection();
217 if (auto *S = dyn_cast<SharedSymbol>(this)) {
219 return S->CopyRelSec->getParent();
223 if (isa<DefinedCommon>(this)) {
224 if (Config->DefineCommon)
225 return InX::Common->getParent();
232 // If a symbol name contains '@', the characters after that is
233 // a symbol version name. This function parses that.
234 void SymbolBody::parseSymbolVersion() {
235 StringRef S = getName();
236 size_t Pos = S.find('@');
237 if (Pos == 0 || Pos == StringRef::npos)
239 StringRef Verstr = S.substr(Pos + 1);
243 // Truncate the symbol name so that it doesn't include the version string.
244 Name = {S.data(), Pos};
246 // If this is not in this DSO, it is not a definition.
247 if (!isInCurrentDSO())
250 // '@@' in a symbol name means the default version.
251 // It is usually the most recent one.
252 bool IsDefault = (Verstr[0] == '@');
254 Verstr = Verstr.substr(1);
256 for (VersionDefinition &Ver : Config->VersionDefinitions) {
257 if (Ver.Name != Verstr)
261 symbol()->VersionId = Ver.Id;
263 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
267 // It is an error if the specified version is not defined.
268 // Usually version script is not provided when linking executable,
269 // but we may still want to override a versioned symbol from DSO,
270 // so we do not report error in this case.
272 error(toString(File) + ": symbol " + S + " has undefined version " +
276 Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
278 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
280 template <class ELFT> bool DefinedRegular::isMipsPIC() const {
281 typedef typename ELFT::Ehdr Elf_Ehdr;
282 if (!Section || !isFunc())
285 auto *Sec = cast<InputSectionBase>(Section);
286 const Elf_Ehdr *Hdr = Sec->template getFile<ELFT>()->getObj().getHeader();
287 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
288 (Hdr->e_flags & EF_MIPS_PIC);
291 Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
292 uint8_t Type, InputFile *File)
293 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {
297 DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
298 uint8_t StOther, uint8_t Type, InputFile *File)
299 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
301 Alignment(Alignment), Size(Size) {
305 // If a shared symbol is referred via a copy relocation, its alignment
306 // becomes part of the ABI. This function returns a symbol alignment.
307 // Because symbols don't have alignment attributes, we need to infer that.
308 template <class ELFT> uint32_t SharedSymbol::getAlignment() const {
309 auto *File = cast<SharedFile<ELFT>>(this->File);
310 uint32_t SecAlign = File->getSection(getSym<ELFT>())->sh_addralign;
311 uint64_t SymValue = getSym<ELFT>().st_value;
312 uint32_t SymAlign = uint32_t(1) << countTrailingZeros(SymValue);
313 return std::min(SecAlign, SymAlign);
316 InputFile *Lazy::fetch() {
317 if (auto *S = dyn_cast<LazyArchive>(this))
319 return cast<LazyObject>(this)->fetch();
322 LazyArchive::LazyArchive(ArchiveFile &File,
323 const llvm::object::Archive::Symbol S, uint8_t Type)
324 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
328 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
329 : Lazy(LazyObjectKind, Name, Type) {
333 InputFile *LazyArchive::fetch() {
334 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
336 // getMember returns an empty buffer if the member was already
337 // read from the library.
338 if (MBInfo.first.getBuffer().empty())
340 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
343 InputFile *LazyObject::fetch() { return file()->fetch(); }
345 uint8_t Symbol::computeBinding() const {
346 if (Config->Relocatable)
348 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
350 if (VersionId == VER_NDX_LOCAL && body()->isInCurrentDSO())
352 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
357 bool Symbol::includeInDynsym() const {
358 if (computeBinding() == STB_LOCAL)
360 return ExportDynamic || body()->isShared() ||
361 (body()->isUndefined() && Config->Shared);
364 // Print out a log message for --trace-symbol.
365 void elf::printTraceSymbol(Symbol *Sym) {
366 SymbolBody *B = Sym->body();
368 if (B->isUndefined())
369 S = ": reference to ";
370 else if (B->isCommon())
371 S = ": common definition of ";
373 S = ": definition of ";
375 message(toString(B->File) + S + B->getName());
378 // Returns a symbol for an error message.
379 std::string lld::toString(const SymbolBody &B) {
380 if (Config->Demangle)
381 if (Optional<std::string> S = demangle(B.getName()))
386 template uint32_t SymbolBody::template getSize<ELF32LE>() const;
387 template uint32_t SymbolBody::template getSize<ELF32BE>() const;
388 template uint64_t SymbolBody::template getSize<ELF64LE>() const;
389 template uint64_t SymbolBody::template getSize<ELF64BE>() const;
391 template bool DefinedRegular::template isMipsPIC<ELF32LE>() const;
392 template bool DefinedRegular::template isMipsPIC<ELF32BE>() const;
393 template bool DefinedRegular::template isMipsPIC<ELF64LE>() const;
394 template bool DefinedRegular::template isMipsPIC<ELF64BE>() const;
396 template uint32_t SharedSymbol::template getAlignment<ELF32LE>() const;
397 template uint32_t SharedSymbol::template getAlignment<ELF32BE>() const;
398 template uint32_t SharedSymbol::template getAlignment<ELF64LE>() const;
399 template uint32_t SharedSymbol::template getAlignment<ELF64BE>() const;