]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Symbols.cpp
MFV r336800: libarchive: Cherry-pick upstream 2c8c83b9
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Symbols.cpp
1 //===- Symbols.cpp --------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Symbols.h"
11 #include "InputFiles.h"
12 #include "InputSection.h"
13 #include "OutputSections.h"
14 #include "SyntheticSections.h"
15 #include "Target.h"
16 #include "Writer.h"
17
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Strings.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/Path.h"
22 #include <cstring>
23
24 using namespace llvm;
25 using namespace llvm::object;
26 using namespace llvm::ELF;
27
28 using namespace lld;
29 using namespace lld::elf;
30
31 Defined *ElfSym::Bss;
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;
42
43 static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
44   switch (Sym.kind()) {
45   case Symbol::DefinedKind: {
46     auto &D = cast<Defined>(Sym);
47     SectionBase *IS = D.Section;
48
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
52     // 0.
53     if (IS == &InputSection::Discarded)
54       return 0;
55
56     // This is an absolute symbol.
57     if (!IS)
58       return D.Value;
59
60     IS = IS->Repl;
61     uint64_t Offset = D.Value;
62
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
65     // symbols).
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.
74     if (D.isSection()) {
75       Offset += Addend;
76       Addend = 0;
77     }
78
79     const OutputSection *OutSec = IS->getOutputSection();
80
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).
87     //
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);
92
93     if (D.isTls() && !Config->Relocatable) {
94       if (!Out::TlsPhdr)
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;
98     }
99     return VA;
100   }
101   case Symbol::SharedKind: {
102     auto &SS = cast<SharedSymbol>(Sym);
103     if (SS.CopyRelSec)
104       return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff;
105     if (SS.NeedsPltAddr)
106       return Sym.getPltVA();
107     return 0;
108   }
109   case Symbol::UndefinedKind:
110     return 0;
111   case Symbol::LazyArchiveKind:
112   case Symbol::LazyObjectKind:
113     assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
114     return 0;
115   }
116   llvm_unreachable("invalid symbol kind");
117 }
118
119 uint64_t Symbol::getVA(int64_t Addend) const {
120   uint64_t OutVA = getSymVA(*this, Addend);
121   return OutVA + Addend;
122 }
123
124 uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
125
126 uint64_t Symbol::getGotOffset() const {
127   return GotIndex * Target->GotEntrySize;
128 }
129
130 uint64_t Symbol::getGotPltVA() const {
131   if (this->IsInIgot)
132     return InX::IgotPlt->getVA() + getGotPltOffset();
133   return InX::GotPlt->getVA() + getGotPltOffset();
134 }
135
136 uint64_t Symbol::getGotPltOffset() const {
137   return GotPltIndex * Target->GotPltEntrySize;
138 }
139
140 uint64_t Symbol::getPltVA() const {
141   if (this->IsInIplt)
142     return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
143   return InX::Plt->getVA() + Target->PltHeaderSize +
144          PltIndex * Target->PltEntrySize;
145 }
146
147 uint64_t Symbol::getSize() const {
148   if (const auto *DR = dyn_cast<Defined>(this))
149     return DR->Size;
150   if (const auto *S = dyn_cast<SharedSymbol>(this))
151     return S->Size;
152   return 0;
153 }
154
155 OutputSection *Symbol::getOutputSection() const {
156   if (auto *S = dyn_cast<Defined>(this)) {
157     if (auto *Sec = S->Section)
158       return Sec->Repl->getOutputSection();
159     return nullptr;
160   }
161
162   if (auto *S = dyn_cast<SharedSymbol>(this)) {
163     if (S->CopyRelSec)
164       return S->CopyRelSec->getParent();
165     return nullptr;
166   }
167
168   return nullptr;
169 }
170
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)
177     return;
178   StringRef Verstr = S.substr(Pos + 1);
179   if (Verstr.empty())
180     return;
181
182   // Truncate the symbol name so that it doesn't include the version string.
183   Name = {S.data(), Pos};
184
185   // If this is not in this DSO, it is not a definition.
186   if (!isDefined())
187     return;
188
189   // '@@' in a symbol name means the default version.
190   // It is usually the most recent one.
191   bool IsDefault = (Verstr[0] == '@');
192   if (IsDefault)
193     Verstr = Verstr.substr(1);
194
195   for (VersionDefinition &Ver : Config->VersionDefinitions) {
196     if (Ver.Name != Verstr)
197       continue;
198
199     if (IsDefault)
200       VersionId = Ver.Id;
201     else
202       VersionId = Ver.Id | VERSYM_HIDDEN;
203     return;
204   }
205
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.
210   if (Config->Shared)
211     error(toString(File) + ": symbol " + S + " has undefined version " +
212           Verstr);
213 }
214
215 InputFile *Lazy::fetch() {
216   if (auto *S = dyn_cast<LazyArchive>(this))
217     return S->fetch();
218   return cast<LazyObject>(this)->fetch();
219 }
220
221 ArchiveFile &LazyArchive::getFile() { return *cast<ArchiveFile>(File); }
222
223 InputFile *LazyArchive::fetch() {
224   std::pair<MemoryBufferRef, uint64_t> MBInfo = getFile().getMember(&Sym);
225
226   // getMember returns an empty buffer if the member was already
227   // read from the library.
228   if (MBInfo.first.getBuffer().empty())
229     return nullptr;
230   return createObjectFile(MBInfo.first, getFile().getName(), MBInfo.second);
231 }
232
233 LazyObjFile &LazyObject::getFile() { return *cast<LazyObjFile>(File); }
234
235 InputFile *LazyObject::fetch() { return getFile().fetch(); }
236
237 uint8_t Symbol::computeBinding() const {
238   if (Config->Relocatable)
239     return Binding;
240   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
241     return STB_LOCAL;
242   if (VersionId == VER_NDX_LOCAL && isDefined())
243     return STB_LOCAL;
244   if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
245     return STB_GLOBAL;
246   return Binding;
247 }
248
249 bool Symbol::includeInDynsym() const {
250   if (!Config->HasDynSymTab)
251     return false;
252   if (computeBinding() == STB_LOCAL)
253     return false;
254   if (!isDefined())
255     return true;
256   return ExportDynamic;
257 }
258
259 // Print out a log message for --trace-symbol.
260 void elf::printTraceSymbol(Symbol *Sym) {
261   std::string S;
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 ";
270   else
271     S = ": definition of ";
272
273   message(toString(Sym->File) + S + Sym->getName());
274 }
275
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()))
280       return *S;
281   return B.getName();
282 }