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"
17 #include "lld/Common/ErrorHandler.h"
18 #include "lld/Common/Strings.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Path.h"
24 using namespace llvm::object;
25 using namespace llvm::ELF;
28 using namespace lld::elf;
31 Defined *ElfSym::Etext1;
32 Defined *ElfSym::Etext2;
33 Defined *ElfSym::Edata1;
34 Defined *ElfSym::Edata2;
35 Defined *ElfSym::End1;
36 Defined *ElfSym::End2;
37 Defined *ElfSym::GlobalOffsetTable;
38 Defined *ElfSym::MipsGp;
39 Defined *ElfSym::MipsGpDisp;
40 Defined *ElfSym::MipsLocalGp;
41 Defined *ElfSym::RelaIpltEnd;
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.
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 // In the typical case, this is actually very simple and boils
81 // down to adding together 3 numbers:
82 // 1. The address of the output section.
83 // 2. The offset of the input section within the output section.
84 // 3. The offset within the input section (this addition happens
85 // inside InputSection::getOffset).
87 // If you understand the data structures involved with this next
88 // line (and how they get built), then you have a pretty good
89 // understanding of the linker.
90 uint64_t VA = IS->getVA(Offset);
92 if (D.isTls() && !Config->Relocatable) {
94 fatal(toString(D.File) +
95 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
96 return VA - Out::TlsPhdr->p_vaddr;
100 case Symbol::SharedKind:
101 case Symbol::UndefinedKind:
103 case Symbol::LazyArchiveKind:
104 case Symbol::LazyObjectKind:
105 llvm_unreachable("lazy symbol reached writer");
107 llvm_unreachable("invalid symbol kind");
110 uint64_t Symbol::getVA(int64_t Addend) const {
111 uint64_t OutVA = getSymVA(*this, Addend);
112 return OutVA + Addend;
115 uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
117 uint64_t Symbol::getGotOffset() const {
118 return GotIndex * Target->GotEntrySize;
121 uint64_t Symbol::getGotPltVA() const {
123 return InX::IgotPlt->getVA() + getGotPltOffset();
124 return InX::GotPlt->getVA() + getGotPltOffset();
127 uint64_t Symbol::getGotPltOffset() const {
129 return PltIndex * Target->GotPltEntrySize;
130 return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
133 uint64_t Symbol::getPltVA() const {
135 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
136 return InX::Plt->getVA() + Target->getPltEntryOffset(PltIndex);
139 uint64_t Symbol::getPltOffset() const {
140 assert(!this->IsInIplt);
141 return Target->getPltEntryOffset(PltIndex);
144 uint64_t Symbol::getSize() const {
145 if (const auto *DR = dyn_cast<Defined>(this))
147 return cast<SharedSymbol>(this)->Size;
150 OutputSection *Symbol::getOutputSection() const {
151 if (auto *S = dyn_cast<Defined>(this)) {
152 if (auto *Sec = S->Section)
153 return Sec->Repl->getOutputSection();
159 // If a symbol name contains '@', the characters after that is
160 // a symbol version name. This function parses that.
161 void Symbol::parseSymbolVersion() {
162 StringRef S = getName();
163 size_t Pos = S.find('@');
164 if (Pos == 0 || Pos == StringRef::npos)
166 StringRef Verstr = S.substr(Pos + 1);
170 // Truncate the symbol name so that it doesn't include the version string.
173 // If this is not in this DSO, it is not a definition.
177 // '@@' in a symbol name means the default version.
178 // It is usually the most recent one.
179 bool IsDefault = (Verstr[0] == '@');
181 Verstr = Verstr.substr(1);
183 for (VersionDefinition &Ver : Config->VersionDefinitions) {
184 if (Ver.Name != Verstr)
190 VersionId = Ver.Id | VERSYM_HIDDEN;
194 // It is an error if the specified version is not defined.
195 // Usually version script is not provided when linking executable,
196 // but we may still want to override a versioned symbol from DSO,
197 // so we do not report error in this case. We also do not error
198 // if the symbol has a local version as it won't be in the dynamic
200 if (Config->Shared && VersionId != VER_NDX_LOCAL)
201 error(toString(File) + ": symbol " + S + " has undefined version " +
205 InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
207 uint8_t Symbol::computeBinding() const {
208 if (Config->Relocatable)
210 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
212 if (VersionId == VER_NDX_LOCAL && isDefined() && !IsPreemptible)
214 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
219 bool Symbol::includeInDynsym() const {
220 if (!Config->HasDynSymTab)
222 if (computeBinding() == STB_LOCAL)
226 return ExportDynamic;
229 // Print out a log message for --trace-symbol.
230 void elf::printTraceSymbol(Symbol *Sym) {
232 if (Sym->isUndefined())
233 S = ": reference to ";
234 else if (Sym->isLazy())
235 S = ": lazy definition of ";
236 else if (Sym->isShared())
237 S = ": shared definition of ";
238 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
239 S = ": common definition of ";
241 S = ": definition of ";
243 message(toString(Sym->File) + S + Sym->getName());
246 void elf::warnUnorderableSymbol(const Symbol *Sym) {
247 if (!Config->WarnSymbolOrdering)
250 const InputFile *File = Sym->File;
251 auto *D = dyn_cast<Defined>(Sym);
253 auto Warn = [&](StringRef S) { warn(toString(File) + S + Sym->getName()); };
255 if (Sym->isUndefined())
256 Warn(": unable to order undefined symbol: ");
257 else if (Sym->isShared())
258 Warn(": unable to order shared symbol: ");
259 else if (D && !D->Section)
260 Warn(": unable to order absolute symbol: ");
261 else if (D && isa<OutputSection>(D->Section))
262 Warn(": unable to order synthetic symbol: ");
263 else if (D && !D->Section->Repl->Live)
264 Warn(": unable to order discarded symbol: ");
267 // Returns a symbol for an error message.
268 std::string lld::toString(const Symbol &B) {
269 if (Config->Demangle)
270 if (Optional<std::string> S = demangleItanium(B.getName()))