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::RelaIpltStart;
42 Defined *ElfSym::RelaIpltEnd;
44 static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
46 case Symbol::DefinedKind: {
47 auto &D = cast<Defined>(Sym);
48 SectionBase *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::Discarded)
57 // This is an absolute symbol.
63 uint64_t Offset = D.Value;
65 // An object in an SHF_MERGE section might be referenced via a
66 // section symbol (as a hack for reducing the number of local
68 // Depending on the addend, the reference via a section symbol
69 // refers to a different object in the merge section.
70 // Since the objects in the merge section are not necessarily
71 // contiguous in the output, the addend can thus affect the final
72 // VA in a non-linear way.
73 // To make this work, we incorporate the addend into the section
74 // offset (and zero out the addend for later processing) so that
75 // we find the right object in the section.
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 = IS->getVA(Offset);
93 if (D.isTls() && !Config->Relocatable) {
94 // Use the address of the TLS segment's first section rather than the
95 // segment's address, because segment addresses aren't initialized until
96 // after sections are finalized. (e.g. Measuring the size of .rela.dyn
97 // for Android relocation packing requires knowing TLS symbol addresses
98 // during section finalization.)
99 if (!Out::TlsPhdr || !Out::TlsPhdr->FirstSec)
100 fatal(toString(D.File) +
101 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
102 return VA - Out::TlsPhdr->FirstSec->Addr;
106 case Symbol::SharedKind:
107 case Symbol::UndefinedKind:
109 case Symbol::LazyArchiveKind:
110 case Symbol::LazyObjectKind:
111 assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
113 case Symbol::PlaceholderKind:
114 llvm_unreachable("placeholder 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 In.Got->getVA() + getGotOffset(); }
126 uint64_t Symbol::getGotOffset() const {
127 return GotIndex * Target->GotEntrySize;
130 uint64_t Symbol::getGotPltVA() const {
132 return In.IgotPlt->getVA() + getGotPltOffset();
133 return In.GotPlt->getVA() + getGotPltOffset();
136 uint64_t Symbol::getGotPltOffset() const {
138 return PltIndex * Target->GotPltEntrySize;
139 return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
142 uint64_t Symbol::getPPC64LongBranchOffset() const {
143 assert(PPC64BranchltIndex != 0xffff);
144 return PPC64BranchltIndex * Target->GotPltEntrySize;
147 uint64_t Symbol::getPltVA() const {
148 PltSection *Plt = IsInIplt ? In.Iplt : In.Plt;
149 return Plt->getVA() + Plt->HeaderSize + PltIndex * Target->PltEntrySize;
152 uint64_t Symbol::getPPC64LongBranchTableVA() const {
153 assert(PPC64BranchltIndex != 0xffff);
154 return In.PPC64LongBranchTarget->getVA() +
155 PPC64BranchltIndex * Target->GotPltEntrySize;
158 uint64_t Symbol::getSize() const {
159 if (const auto *DR = dyn_cast<Defined>(this))
161 return cast<SharedSymbol>(this)->Size;
164 OutputSection *Symbol::getOutputSection() const {
165 if (auto *S = dyn_cast<Defined>(this)) {
166 if (auto *Sec = S->Section)
167 return Sec->Repl->getOutputSection();
173 // If a symbol name contains '@', the characters after that is
174 // a symbol version name. This function parses that.
175 void Symbol::parseSymbolVersion() {
176 StringRef S = getName();
177 size_t Pos = S.find('@');
178 if (Pos == 0 || Pos == StringRef::npos)
180 StringRef Verstr = S.substr(Pos + 1);
184 // Truncate the symbol name so that it doesn't include the version string.
187 // If this is not in this DSO, it is not a definition.
191 // '@@' in a symbol name means the default version.
192 // It is usually the most recent one.
193 bool IsDefault = (Verstr[0] == '@');
195 Verstr = Verstr.substr(1);
197 for (VersionDefinition &Ver : Config->VersionDefinitions) {
198 if (Ver.Name != Verstr)
204 VersionId = Ver.Id | VERSYM_HIDDEN;
208 // It is an error if the specified version is not defined.
209 // Usually version script is not provided when linking executable,
210 // but we may still want to override a versioned symbol from DSO,
211 // so we do not report error in this case. We also do not error
212 // if the symbol has a local version as it won't be in the dynamic
214 if (Config->Shared && VersionId != VER_NDX_LOCAL)
215 error(toString(File) + ": symbol " + S + " has undefined version " +
219 InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
221 MemoryBufferRef LazyArchive::getMemberBuffer() {
222 Archive::Child C = CHECK(
223 Sym.getMember(), "could not get the member for symbol " + Sym.getName());
225 return CHECK(C.getMemoryBufferRef(),
226 "could not get the buffer for the member defining symbol " +
230 uint8_t Symbol::computeBinding() const {
231 if (Config->Relocatable)
233 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
235 if (VersionId == VER_NDX_LOCAL && isDefined() && !IsPreemptible)
237 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
242 bool Symbol::includeInDynsym() const {
243 if (!Config->HasDynSymTab)
245 if (computeBinding() == STB_LOCAL)
249 return ExportDynamic;
252 // Print out a log message for --trace-symbol.
253 void elf::printTraceSymbol(Symbol *Sym) {
255 if (Sym->isUndefined())
256 S = ": reference to ";
257 else if (Sym->isLazy())
258 S = ": lazy definition of ";
259 else if (Sym->isShared())
260 S = ": shared definition of ";
261 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
262 S = ": common definition of ";
264 S = ": definition of ";
266 message(toString(Sym->File) + S + Sym->getName());
269 void elf::maybeWarnUnorderableSymbol(const Symbol *Sym) {
270 if (!Config->WarnSymbolOrdering)
273 // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
274 // is emitted. It makes sense to not warn on undefined symbols.
276 // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
277 // but we don't have to be compatible here.
278 if (Sym->isUndefined() &&
279 Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
282 const InputFile *File = Sym->File;
283 auto *D = dyn_cast<Defined>(Sym);
285 auto Warn = [&](StringRef S) { warn(toString(File) + S + Sym->getName()); };
287 if (Sym->isUndefined())
288 Warn(": unable to order undefined symbol: ");
289 else if (Sym->isShared())
290 Warn(": unable to order shared symbol: ");
291 else if (D && !D->Section)
292 Warn(": unable to order absolute symbol: ");
293 else if (D && isa<OutputSection>(D->Section))
294 Warn(": unable to order synthetic symbol: ");
295 else if (D && !D->Section->Repl->Live)
296 Warn(": unable to order discarded symbol: ");
299 // Returns a symbol for an error message.
300 std::string lld::toString(const Symbol &B) {
301 if (Config->Demangle)
302 if (Optional<std::string> S = demangleItanium(B.getName()))