1 //===- Symbols.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "InputFiles.h"
12 #include "InputSection.h"
13 #include "OutputSections.h"
14 #include "SyntheticSections.h"
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Strings.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 Defined *ElfSym::Etext1;
33 Defined *ElfSym::Etext2;
34 Defined *ElfSym::Edata1;
35 Defined *ElfSym::Edata2;
36 Defined *ElfSym::End1;
37 Defined *ElfSym::End2;
38 Defined *ElfSym::GlobalOffsetTable;
39 Defined *ElfSym::MipsGp;
40 Defined *ElfSym::MipsGpDisp;
41 Defined *ElfSym::MipsLocalGp;
43 static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
45 case Symbol::DefinedKind: {
46 auto &D = cast<Defined>(Sym);
47 SectionBase *IS = D.Section;
49 // According to the ELF spec reference to a local symbol from outside
50 // the group are not allowed. Unfortunately .eh_frame breaks that rule
51 // and must be treated specially. For now we just replace the symbol with
53 if (IS == &InputSection::Discarded)
56 // This is an absolute symbol.
61 uint64_t Offset = D.Value;
63 // An object in an SHF_MERGE section might be referenced via a
64 // section symbol (as a hack for reducing the number of local
66 // Depending on the addend, the reference via a section symbol
67 // refers to a different object in the merge section.
68 // Since the objects in the merge section are not necessarily
69 // contiguous in the output, the addend can thus affect the final
70 // VA in a non-linear way.
71 // To make this work, we incorporate the addend into the section
72 // offset (and zero out the addend for later processing) so that
73 // we find the right object in the section.
79 const OutputSection *OutSec = IS->getOutputSection();
81 // In the typical case, this is actually very simple and boils
82 // down to adding together 3 numbers:
83 // 1. The address of the output section.
84 // 2. The offset of the input section within the output section.
85 // 3. The offset within the input section (this addition happens
86 // inside InputSection::getOffset).
88 // If you understand the data structures involved with this next
89 // line (and how they get built), then you have a pretty good
90 // understanding of the linker.
91 uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
93 if (D.isTls() && !Config->Relocatable) {
95 fatal(toString(D.File) +
96 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
97 return VA - Out::TlsPhdr->p_vaddr;
101 case Symbol::SharedKind: {
102 auto &SS = cast<SharedSymbol>(Sym);
104 return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff;
106 return Sym.getPltVA();
109 case Symbol::UndefinedKind:
111 case Symbol::LazyArchiveKind:
112 case Symbol::LazyObjectKind:
113 assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
116 llvm_unreachable("invalid symbol kind");
119 uint64_t Symbol::getVA(int64_t Addend) const {
120 uint64_t OutVA = getSymVA(*this, Addend);
121 return OutVA + Addend;
124 uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
126 uint64_t Symbol::getGotOffset() const {
127 return GotIndex * Target->GotEntrySize;
130 uint64_t Symbol::getGotPltVA() const {
132 return InX::IgotPlt->getVA() + getGotPltOffset();
133 return InX::GotPlt->getVA() + getGotPltOffset();
136 uint64_t Symbol::getGotPltOffset() const {
137 return GotPltIndex * Target->GotPltEntrySize;
140 uint64_t Symbol::getPltVA() const {
142 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
143 return InX::Plt->getVA() + Target->PltHeaderSize +
144 PltIndex * Target->PltEntrySize;
147 uint64_t Symbol::getSize() const {
148 if (const auto *DR = dyn_cast<Defined>(this))
150 if (const auto *S = dyn_cast<SharedSymbol>(this))
155 OutputSection *Symbol::getOutputSection() const {
156 if (auto *S = dyn_cast<Defined>(this)) {
157 if (auto *Sec = S->Section)
158 return Sec->Repl->getOutputSection();
162 if (auto *S = dyn_cast<SharedSymbol>(this)) {
164 return S->CopyRelSec->getParent();
171 // If a symbol name contains '@', the characters after that is
172 // a symbol version name. This function parses that.
173 void Symbol::parseSymbolVersion() {
174 StringRef S = getName();
175 size_t Pos = S.find('@');
176 if (Pos == 0 || Pos == StringRef::npos)
178 StringRef Verstr = S.substr(Pos + 1);
182 // Truncate the symbol name so that it doesn't include the version string.
183 Name = {S.data(), Pos};
185 // If this is not in this DSO, it is not a definition.
189 // '@@' in a symbol name means the default version.
190 // It is usually the most recent one.
191 bool IsDefault = (Verstr[0] == '@');
193 Verstr = Verstr.substr(1);
195 for (VersionDefinition &Ver : Config->VersionDefinitions) {
196 if (Ver.Name != Verstr)
202 VersionId = Ver.Id | VERSYM_HIDDEN;
206 // It is an error if the specified version is not defined.
207 // Usually version script is not provided when linking executable,
208 // but we may still want to override a versioned symbol from DSO,
209 // so we do not report error in this case.
211 error(toString(File) + ": symbol " + S + " has undefined version " +
215 InputFile *Lazy::fetch() {
216 if (auto *S = dyn_cast<LazyArchive>(this))
218 return cast<LazyObject>(this)->fetch();
221 ArchiveFile &LazyArchive::getFile() { return *cast<ArchiveFile>(File); }
223 InputFile *LazyArchive::fetch() {
224 std::pair<MemoryBufferRef, uint64_t> MBInfo = getFile().getMember(&Sym);
226 // getMember returns an empty buffer if the member was already
227 // read from the library.
228 if (MBInfo.first.getBuffer().empty())
230 return createObjectFile(MBInfo.first, getFile().getName(), MBInfo.second);
233 LazyObjFile &LazyObject::getFile() { return *cast<LazyObjFile>(File); }
235 InputFile *LazyObject::fetch() { return getFile().fetch(); }
237 uint8_t Symbol::computeBinding() const {
238 if (Config->Relocatable)
240 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
242 if (VersionId == VER_NDX_LOCAL && isDefined())
244 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
249 bool Symbol::includeInDynsym() const {
250 if (!Config->HasDynSymTab)
252 if (computeBinding() == STB_LOCAL)
256 return ExportDynamic;
259 // Print out a log message for --trace-symbol.
260 void elf::printTraceSymbol(Symbol *Sym) {
262 if (Sym->isUndefined())
263 S = ": reference to ";
264 else if (Sym->isLazy())
265 S = ": lazy definition of ";
266 else if (Sym->isShared())
267 S = ": shared definition of ";
268 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
269 S = ": common definition of ";
271 S = ": definition of ";
273 message(toString(Sym->File) + S + Sym->getName());
276 // Returns a symbol for an error message.
277 std::string lld::toString(const Symbol &B) {
278 if (Config->Demangle)
279 if (Optional<std::string> S = demangleItanium(B.getName()))