blob: dccd3a7335272213fa33437c984856e435d11779 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:071//===- 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"
Michael J. Spencercdae0a42015-07-28 22:58:2511#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:3512#include "InputSection.h"
13#include "OutputSections.h"
Rui Ueyamae8a61022016-11-05 23:05:4714#include "SyntheticSections.h"
Rui Ueyamab5a69702016-02-01 21:00:3515#include "Target.h"
Rafael Espindola17cb7c02016-12-19 17:01:0116#include "Writer.h"
Bob Haarmanb8a59c82017-10-25 22:28:3817#include "lld/Common/ErrorHandler.h"
Rui Ueyama53fe4692017-11-28 02:15:2618#include "lld/Common/Strings.h"
Michael J. Spencer1b348a62015-09-04 22:28:1019#include "llvm/ADT/STLExtras.h"
Eugene Leviantc958d8d2016-10-12 08:19:3020#include "llvm/Support/Path.h"
Rui Ueyamac72ba3a2016-11-23 04:57:2521#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:1022
23using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0724using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5225using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0726
27using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:5428using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0729
Peter Collingbournee9a9e0a2017-11-06 04:35:3130Defined *ElfSym::Bss;
31Defined *ElfSym::Etext1;
32Defined *ElfSym::Etext2;
33Defined *ElfSym::Edata1;
34Defined *ElfSym::Edata2;
35Defined *ElfSym::End1;
36Defined *ElfSym::End2;
37Defined *ElfSym::GlobalOffsetTable;
38Defined *ElfSym::MipsGp;
39Defined *ElfSym::MipsGpDisp;
40Defined *ElfSym::MipsLocalGp;
Rui Ueyama80474a22017-02-28 19:29:5541
Rui Ueyama48882242017-11-04 00:31:0442static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
43 switch (Sym.kind()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:3144 case Symbol::DefinedKind: {
45 auto &D = cast<Defined>(Sym);
Rafael Espindola5616adf2017-03-08 22:36:2846 SectionBase *IS = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:3547
Rafael Espindolaccfe3cb2016-04-04 14:04:1648 // According to the ELF spec reference to a local symbol from outside
49 // the group are not allowed. Unfortunately .eh_frame breaks that rule
50 // and must be treated specially. For now we just replace the symbol with
51 // 0.
Rafael Espindola774ea7d2017-02-23 16:49:0752 if (IS == &InputSection::Discarded)
Rafael Espindolaccfe3cb2016-04-04 14:04:1653 return 0;
54
Rui Ueyamab5a69702016-02-01 21:00:3555 // This is an absolute symbol.
Sean Silva902ae3c2016-12-15 00:57:5356 if (!IS)
Rafael Espindolaccfe3cb2016-04-04 14:04:1657 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3558
Rafael Espindolaf4fb5fd2017-12-13 22:59:2359 IS = IS->Repl;
Rafael Espindola9371bab2017-03-08 15:21:3260 uint64_t Offset = D.Value;
Sean Silvaa9ba4502017-02-28 08:32:5661
62 // An object in an SHF_MERGE section might be referenced via a
63 // section symbol (as a hack for reducing the number of local
64 // symbols).
Sean Silvad4e60622017-03-01 04:44:0465 // Depending on the addend, the reference via a section symbol
66 // refers to a different object in the merge section.
67 // Since the objects in the merge section are not necessarily
68 // contiguous in the output, the addend can thus affect the final
69 // VA in a non-linear way.
70 // To make this work, we incorporate the addend into the section
71 // offset (and zero out the addend for later processing) so that
72 // we find the right object in the section.
Rafael Espindolaccfe3cb2016-04-04 14:04:1673 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:3774 Offset += Addend;
75 Addend = 0;
76 }
Sean Silvaa9ba4502017-02-28 08:32:5677
Rafael Espindola5e434b32017-03-08 16:08:3678 const OutputSection *OutSec = IS->getOutputSection();
Sean Silva6ab39262017-02-28 09:01:5879
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).
86 //
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.
Rafael Espindolae1294092017-03-08 16:03:4190 uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
Sean Silva6ab39262017-02-28 09:01:5891
George Rimar6a3b1542016-10-04 08:52:5192 if (D.isTls() && !Config->Relocatable) {
Rui Ueyama9d1bacb12017-02-27 02:31:2693 if (!Out::TlsPhdr)
Rafael Espindoladfebd362017-11-29 22:47:3594 fatal(toString(D.File) +
Peter Collingbourne3e2abde2017-07-14 00:22:4695 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Rui Ueyama9d1bacb12017-02-27 02:31:2696 return VA - Out::TlsPhdr->p_vaddr;
George Rimar6a3b1542016-10-04 08:52:5197 }
Rafael Espindola1f5b70f2016-03-11 14:21:3798 return VA;
Rui Ueyamab5a69702016-02-01 21:00:3599 }
Rui Ueyamaf52496e2017-11-03 21:21:47100 case Symbol::SharedKind: {
Rui Ueyama48882242017-11-04 00:31:04101 auto &SS = cast<SharedSymbol>(Sym);
Rafael Espindola0afcef22017-08-04 17:43:54102 if (SS.CopyRelSec)
Rafael Espindolaf6c74c42017-09-13 16:59:12103 return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff;
Rui Ueyama007c0022017-03-08 17:24:24104 if (SS.NeedsPltAddr)
Rui Ueyama48882242017-11-04 00:31:04105 return Sym.getPltVA();
Rui Ueyama924b3612017-02-16 06:12:22106 return 0;
Rui Ueyama007c0022017-03-08 17:24:24107 }
Rui Ueyamaf52496e2017-11-03 21:21:47108 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35109 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47110 case Symbol::LazyArchiveKind:
111 case Symbol::LazyObjectKind:
Rui Ueyama48882242017-11-04 00:31:04112 assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35113 return 0;
114 }
George Rimar777f9632016-03-12 08:31:34115 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35116}
117
Rui Ueyamaf52496e2017-11-03 21:21:47118uint64_t Symbol::getVA(int64_t Addend) const {
George Rimarf64618a2017-03-17 11:56:54119 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19120 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05121}
122
Rui Ueyamaf52496e2017-11-03 21:21:47123uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
Rafael Espindola74031ba2016-04-07 15:20:56124
Rui Ueyamaf52496e2017-11-03 21:21:47125uint64_t Symbol::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14126 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35127}
128
Rui Ueyamaf52496e2017-11-03 21:21:47129uint64_t Symbol::getGotPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55130 if (this->IsInIgot)
George Rimar4670bb02017-03-16 12:58:11131 return InX::IgotPlt->getVA() + getGotPltOffset();
132 return InX::GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56133}
134
Rui Ueyamaf52496e2017-11-03 21:21:47135uint64_t Symbol::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14136 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35137}
138
Rui Ueyamaf52496e2017-11-03 21:21:47139uint64_t Symbol::getPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55140 if (this->IsInIplt)
George Rimarf64618a2017-03-17 11:56:54141 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
Rafael Espindola74acdfa2018-03-14 17:41:34142 return InX::Plt->getVA() + Target->getPltEntryOffset(PltIndex);
Rui Ueyamab5a69702016-02-01 21:00:35143}
144
Rui Ueyamaf52496e2017-11-03 21:21:47145uint64_t Symbol::getSize() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31146 if (const auto *DR = dyn_cast<Defined>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16147 return DR->Size;
Rui Ueyama4076fa12017-02-26 23:35:34148 if (const auto *S = dyn_cast<SharedSymbol>(this))
Rui Ueyama7f9694a42017-10-28 20:15:56149 return S->Size;
Rui Ueyama512c61d2016-02-03 00:12:24150 return 0;
151}
152
Rui Ueyamaf52496e2017-11-03 21:21:47153OutputSection *Symbol::getOutputSection() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31154 if (auto *S = dyn_cast<Defined>(this)) {
Rafael Espindolaf4fb5fd2017-12-13 22:59:23155 if (auto *Sec = S->Section)
Rafael Espindola4e82a9e2018-03-23 23:53:01156 return Sec->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42157 return nullptr;
158 }
159
Rui Ueyama007c0022017-03-08 17:24:24160 if (auto *S = dyn_cast<SharedSymbol>(this)) {
Rafael Espindola0afcef22017-08-04 17:43:54161 if (S->CopyRelSec)
Rafael Espindoladb5e56f2017-05-31 20:17:44162 return S->CopyRelSec->getParent();
Rui Ueyama968db482017-02-28 04:02:42163 return nullptr;
Rui Ueyama007c0022017-03-08 17:24:24164 }
Rui Ueyama968db482017-02-28 04:02:42165
Rui Ueyama968db482017-02-28 04:02:42166 return nullptr;
167}
168
Rui Ueyama35fa6c52016-11-23 05:48:40169// If a symbol name contains '@', the characters after that is
170// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47171void Symbol::parseSymbolVersion() {
Rui Ueyama35fa6c52016-11-23 05:48:40172 StringRef S = getName();
173 size_t Pos = S.find('@');
174 if (Pos == 0 || Pos == StringRef::npos)
175 return;
176 StringRef Verstr = S.substr(Pos + 1);
177 if (Verstr.empty())
178 return;
179
180 // Truncate the symbol name so that it doesn't include the version string.
Rui Ueyamaa13efc22016-11-29 18:05:04181 Name = {S.data(), Pos};
Rui Ueyama35fa6c52016-11-23 05:48:40182
Rafael Espindola1d6d1b42017-01-17 16:08:06183 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07184 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35185 return;
186
Rui Ueyama35fa6c52016-11-23 05:48:40187 // '@@' in a symbol name means the default version.
188 // It is usually the most recent one.
189 bool IsDefault = (Verstr[0] == '@');
190 if (IsDefault)
191 Verstr = Verstr.substr(1);
192
193 for (VersionDefinition &Ver : Config->VersionDefinitions) {
194 if (Ver.Name != Verstr)
195 continue;
196
197 if (IsDefault)
Rui Ueyamaf1f00842017-10-31 16:07:41198 VersionId = Ver.Id;
Rui Ueyama35fa6c52016-11-23 05:48:40199 else
Rui Ueyamaf1f00842017-10-31 16:07:41200 VersionId = Ver.Id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40201 return;
202 }
203
204 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13205 // Usually version script is not provided when linking executable,
206 // but we may still want to override a versioned symbol from DSO,
207 // so we do not report error in this case.
208 if (Config->Shared)
Rafael Espindoladfebd362017-11-29 22:47:35209 error(toString(File) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13210 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40211}
212
Rui Ueyama55518e72016-10-28 20:57:25213InputFile *Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51214 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama55518e72016-10-28 20:57:25215 return S->fetch();
216 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51217}
218
Rafael Espindola8276f1b2017-12-20 17:59:43219ArchiveFile &LazyArchive::getFile() { return *cast<ArchiveFile>(File); }
Rui Ueyama434b5612016-07-17 03:11:46220
Rui Ueyama55518e72016-10-28 20:57:25221InputFile *LazyArchive::fetch() {
Rafael Espindola8276f1b2017-12-20 17:59:43222 std::pair<MemoryBufferRef, uint64_t> MBInfo = getFile().getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10223
224 // getMember returns an empty buffer if the member was already
225 // read from the library.
Davide Italianobcdd6c62016-10-12 19:35:54226 if (MBInfo.first.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51227 return nullptr;
Rafael Espindola8276f1b2017-12-20 17:59:43228 return createObjectFile(MBInfo.first, getFile().getName(), MBInfo.second);
Michael J. Spencer1b348a62015-09-04 22:28:10229}
230
Rafael Espindola2e5c71e2017-12-20 17:52:36231LazyObjFile &LazyObject::getFile() { return *cast<LazyObjFile>(File); }
Rafael Espindola6e93d052017-08-04 22:31:42232
Rafael Espindola2e5c71e2017-12-20 17:52:36233InputFile *LazyObject::fetch() { return getFile().fetch(); }
Rui Ueyamaf8baa662016-04-07 19:24:51234
Rui Ueyamaf52496e2017-11-03 21:21:47235uint8_t Symbol::computeBinding() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13236 if (Config->Relocatable)
237 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48238 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13239 return STB_LOCAL;
Peter Collingbourneb472aa02017-11-06 04:39:07240 if (VersionId == VER_NDX_LOCAL && isDefined())
Rafael Espindolab7e2ee22017-01-10 17:08:13241 return STB_LOCAL;
Rui Ueyamaaad2e322018-02-02 21:44:06242 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
Rafael Espindolab7e2ee22017-01-10 17:08:13243 return STB_GLOBAL;
244 return Binding;
245}
246
Rui Ueyamaf52496e2017-11-03 21:21:47247bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02248 if (!Config->HasDynSymTab)
249 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13250 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25251 return false;
Peter Collingbourneb472aa02017-11-06 04:39:07252 if (!isDefined())
Rafael Espindola3d9f1c02017-09-13 20:43:04253 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53254 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25255}
Rui Ueyama69c778c2016-07-17 17:50:09256
257// Print out a log message for --trace-symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47258void elf::printTraceSymbol(Symbol *Sym) {
Rui Ueyamae6e206d2017-02-21 23:22:56259 std::string S;
Rui Ueyamaf1f00842017-10-31 16:07:41260 if (Sym->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56261 S = ": reference to ";
Rui Ueyamaf1f00842017-10-31 16:07:41262 else if (Sym->isLazy())
Rafael Espindolabc2b1652017-10-27 18:30:11263 S = ": lazy definition of ";
Rui Ueyamaf1f00842017-10-31 16:07:41264 else if (Sym->isShared())
Rafael Espindolabc2b1652017-10-27 18:30:11265 S = ": shared definition of ";
Peter Collingbournee9a9e0a2017-11-06 04:35:31266 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
Peter Collingbourne6c55a702017-11-06 04:33:58267 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09268 else
Rui Ueyamae6e206d2017-02-21 23:22:56269 S = ": definition of ";
270
Rui Ueyamaf1f00842017-10-31 16:07:41271 message(toString(Sym->File) + S + Sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09272}
273
Rui Ueyamaa3ac1732016-11-24 20:24:18274// Returns a symbol for an error message.
Rui Ueyamaf52496e2017-11-03 21:21:47275std::string lld::toString(const Symbol &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18276 if (Config->Demangle)
Rui Ueyama53fe4692017-11-28 02:15:26277 if (Optional<std::string> S = demangleItanium(B.getName()))
Rui Ueyama4c5b8ce2016-12-07 23:17:05278 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18279 return B.getName();
280}