blob: 70c510c8ba1c0fa2ca25df1d5d81ecc33a593efb [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 Ueyamaa3ac1732016-11-24 20:24:1814#include "Strings.h"
Rui Ueyamae8a61022016-11-05 23:05:4715#include "SyntheticSections.h"
Rui Ueyamab5a69702016-02-01 21:00:3516#include "Target.h"
Rafael Espindola17cb7c02016-12-19 17:01:0117#include "Writer.h"
Michael J. Spencer84487f12015-07-24 21:03:0718
Bob Haarmanb8a59c82017-10-25 22:28:3819#include "lld/Common/ErrorHandler.h"
Michael J. Spencer1b348a62015-09-04 22:28:1020#include "llvm/ADT/STLExtras.h"
Eugene Leviantc958d8d2016-10-12 08:19:3021#include "llvm/Support/Path.h"
Rui Ueyamac72ba3a2016-11-23 04:57:2522#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:1023
24using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0725using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5226using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0727
28using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:5429using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0730
Peter Collingbournee9a9e0a2017-11-06 04:35:3131Defined *ElfSym::Bss;
32Defined *ElfSym::Etext1;
33Defined *ElfSym::Etext2;
34Defined *ElfSym::Edata1;
35Defined *ElfSym::Edata2;
36Defined *ElfSym::End1;
37Defined *ElfSym::End2;
38Defined *ElfSym::GlobalOffsetTable;
39Defined *ElfSym::MipsGp;
40Defined *ElfSym::MipsGpDisp;
41Defined *ElfSym::MipsLocalGp;
Rui Ueyama80474a22017-02-28 19:29:5542
Rui Ueyama48882242017-11-04 00:31:0443static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
44 switch (Sym.kind()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:3145 case Symbol::DefinedKind: {
46 auto &D = cast<Defined>(Sym);
Rafael Espindola5616adf2017-03-08 22:36:2847 SectionBase *IS = D.Section;
48 if (auto *ISB = dyn_cast_or_null<InputSectionBase>(IS))
49 IS = ISB->Repl;
Rui Ueyamab5a69702016-02-01 21:00:3550
Rafael Espindolaccfe3cb2016-04-04 14:04:1651 // According to the ELF spec reference to a local symbol from outside
52 // the group are not allowed. Unfortunately .eh_frame breaks that rule
53 // and must be treated specially. For now we just replace the symbol with
54 // 0.
Rafael Espindola774ea7d2017-02-23 16:49:0755 if (IS == &InputSection::Discarded)
Rafael Espindolaccfe3cb2016-04-04 14:04:1656 return 0;
57
Rui Ueyamab5a69702016-02-01 21:00:3558 // This is an absolute symbol.
Sean Silva902ae3c2016-12-15 00:57:5359 if (!IS)
Rafael Espindolaccfe3cb2016-04-04 14:04:1660 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3561
Rafael Espindola9371bab2017-03-08 15:21:3262 uint64_t Offset = D.Value;
Sean Silvaa9ba4502017-02-28 08:32:5663
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
66 // symbols).
Sean Silvad4e60622017-03-01 04:44:0467 // 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.
Rafael Espindolaccfe3cb2016-04-04 14:04:1675 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:3776 Offset += Addend;
77 Addend = 0;
78 }
Sean Silvaa9ba4502017-02-28 08:32:5679
Rafael Espindola5e434b32017-03-08 16:08:3680 const OutputSection *OutSec = IS->getOutputSection();
Sean Silva6ab39262017-02-28 09:01:5881
82 // In the typical case, this is actually very simple and boils
83 // down to adding together 3 numbers:
84 // 1. The address of the output section.
85 // 2. The offset of the input section within the output section.
86 // 3. The offset within the input section (this addition happens
87 // inside InputSection::getOffset).
88 //
89 // If you understand the data structures involved with this next
90 // line (and how they get built), then you have a pretty good
91 // understanding of the linker.
Rafael Espindolae1294092017-03-08 16:03:4192 uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
Sean Silva6ab39262017-02-28 09:01:5893
George Rimar6a3b1542016-10-04 08:52:5194 if (D.isTls() && !Config->Relocatable) {
Rui Ueyama9d1bacb12017-02-27 02:31:2695 if (!Out::TlsPhdr)
Rafael Espindola6e93d052017-08-04 22:31:4296 fatal(toString(D.getFile()) +
Peter Collingbourne3e2abde2017-07-14 00:22:4697 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Rui Ueyama9d1bacb12017-02-27 02:31:2698 return VA - Out::TlsPhdr->p_vaddr;
George Rimar6a3b1542016-10-04 08:52:5199 }
Rafael Espindola1f5b70f2016-03-11 14:21:37100 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35101 }
Rui Ueyamaf52496e2017-11-03 21:21:47102 case Symbol::SharedKind: {
Rui Ueyama48882242017-11-04 00:31:04103 auto &SS = cast<SharedSymbol>(Sym);
Rafael Espindola0afcef22017-08-04 17:43:54104 if (SS.CopyRelSec)
Rafael Espindolaf6c74c42017-09-13 16:59:12105 return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff;
Rui Ueyama007c0022017-03-08 17:24:24106 if (SS.NeedsPltAddr)
Rui Ueyama48882242017-11-04 00:31:04107 return Sym.getPltVA();
Rui Ueyama924b3612017-02-16 06:12:22108 return 0;
Rui Ueyama007c0022017-03-08 17:24:24109 }
Rui Ueyamaf52496e2017-11-03 21:21:47110 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35111 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47112 case Symbol::LazyArchiveKind:
113 case Symbol::LazyObjectKind:
Rui Ueyama48882242017-11-04 00:31:04114 assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35115 return 0;
116 }
George Rimar777f9632016-03-12 08:31:34117 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35118}
119
Rui Ueyamace2f5fd2017-10-13 02:57:59120// Returns true if this is a weak undefined symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47121bool Symbol::isUndefWeak() const {
Rafael Espindola9c8f8532017-10-24 16:27:31122 // See comment on Lazy in Symbols.h for the details.
Rui Ueyamaf1f00842017-10-31 16:07:41123 return !isLocal() && isWeak() && (isUndefined() || isLazy());
Rafael Espindola3d9f1c02017-09-13 20:43:04124}
125
Rui Ueyamaf52496e2017-11-03 21:21:47126InputFile *Symbol::getFile() const {
Rafael Espindoladb1af692017-08-11 17:47:12127 if (isLocal()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:31128 const SectionBase *Sec = cast<Defined>(this)->Section;
Rafael Espindoladb1af692017-08-11 17:47:12129 // Local absolute symbols actually have a file, but that is not currently
130 // used. We could support that by having a mostly redundant InputFile in
Rui Ueyamaf52496e2017-11-03 21:21:47131 // Symbol, or having a special absolute section if needed.
Rafael Espindoladb1af692017-08-11 17:47:12132 return Sec ? cast<InputSectionBase>(Sec)->File : nullptr;
133 }
Rui Ueyamaf1f00842017-10-31 16:07:41134 return File;
Rafael Espindola6e93d052017-08-04 22:31:42135}
136
Rafael Espindolacf00d432017-07-05 00:43:18137// Overwrites all attributes with Other's so that this symbol becomes
138// an alias to Other. This is useful for handling some options such as
139// --wrap.
Rui Ueyamaf52496e2017-11-03 21:21:47140void Symbol::copyFrom(Symbol *Other) {
141 Symbol Sym = *this;
Rui Ueyamaf1f00842017-10-31 16:07:41142 memcpy(this, Other, sizeof(SymbolUnion));
143
144 Binding = Sym.Binding;
145 VersionId = Sym.VersionId;
146 Visibility = Sym.Visibility;
147 IsUsedInRegularObj = Sym.IsUsedInRegularObj;
148 ExportDynamic = Sym.ExportDynamic;
149 CanInline = Sym.CanInline;
150 Traced = Sym.Traced;
151 InVersionScript = Sym.InVersionScript;
Rui Ueyamab2269ec2017-06-28 19:43:02152}
153
Rui Ueyamaf52496e2017-11-03 21:21:47154uint64_t Symbol::getVA(int64_t Addend) const {
George Rimarf64618a2017-03-17 11:56:54155 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19156 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05157}
158
Rui Ueyamaf52496e2017-11-03 21:21:47159uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
Rafael Espindola74031ba2016-04-07 15:20:56160
Rui Ueyamaf52496e2017-11-03 21:21:47161uint64_t Symbol::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14162 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35163}
164
Rui Ueyamaf52496e2017-11-03 21:21:47165uint64_t Symbol::getGotPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55166 if (this->IsInIgot)
George Rimar4670bb02017-03-16 12:58:11167 return InX::IgotPlt->getVA() + getGotPltOffset();
168 return InX::GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56169}
170
Rui Ueyamaf52496e2017-11-03 21:21:47171uint64_t Symbol::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14172 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35173}
174
Rui Ueyamaf52496e2017-11-03 21:21:47175uint64_t Symbol::getPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55176 if (this->IsInIplt)
George Rimarf64618a2017-03-17 11:56:54177 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
178 return InX::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35179 PltIndex * Target->PltEntrySize;
180}
181
Rui Ueyamaf52496e2017-11-03 21:21:47182uint64_t Symbol::getSize() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31183 if (const auto *DR = dyn_cast<Defined>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16184 return DR->Size;
Rui Ueyama4076fa12017-02-26 23:35:34185 if (const auto *S = dyn_cast<SharedSymbol>(this))
Rui Ueyama7f9694a42017-10-28 20:15:56186 return S->Size;
Rui Ueyama512c61d2016-02-03 00:12:24187 return 0;
188}
189
Rui Ueyamaf52496e2017-11-03 21:21:47190OutputSection *Symbol::getOutputSection() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31191 if (auto *S = dyn_cast<Defined>(this)) {
Rui Ueyama968db482017-02-28 04:02:42192 if (S->Section)
Rafael Espindola5e434b32017-03-08 16:08:36193 return S->Section->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42194 return nullptr;
195 }
196
Rui Ueyama007c0022017-03-08 17:24:24197 if (auto *S = dyn_cast<SharedSymbol>(this)) {
Rafael Espindola0afcef22017-08-04 17:43:54198 if (S->CopyRelSec)
Rafael Espindoladb5e56f2017-05-31 20:17:44199 return S->CopyRelSec->getParent();
Rui Ueyama968db482017-02-28 04:02:42200 return nullptr;
Rui Ueyama007c0022017-03-08 17:24:24201 }
Rui Ueyama968db482017-02-28 04:02:42202
Rui Ueyama968db482017-02-28 04:02:42203 return nullptr;
204}
205
Rui Ueyama35fa6c52016-11-23 05:48:40206// If a symbol name contains '@', the characters after that is
207// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47208void Symbol::parseSymbolVersion() {
Rui Ueyama35fa6c52016-11-23 05:48:40209 StringRef S = getName();
210 size_t Pos = S.find('@');
211 if (Pos == 0 || Pos == StringRef::npos)
212 return;
213 StringRef Verstr = S.substr(Pos + 1);
214 if (Verstr.empty())
215 return;
216
217 // Truncate the symbol name so that it doesn't include the version string.
Rui Ueyamaa13efc22016-11-29 18:05:04218 Name = {S.data(), Pos};
Rui Ueyama35fa6c52016-11-23 05:48:40219
Rafael Espindola1d6d1b42017-01-17 16:08:06220 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07221 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35222 return;
223
Rui Ueyama35fa6c52016-11-23 05:48:40224 // '@@' in a symbol name means the default version.
225 // It is usually the most recent one.
226 bool IsDefault = (Verstr[0] == '@');
227 if (IsDefault)
228 Verstr = Verstr.substr(1);
229
230 for (VersionDefinition &Ver : Config->VersionDefinitions) {
231 if (Ver.Name != Verstr)
232 continue;
233
234 if (IsDefault)
Rui Ueyamaf1f00842017-10-31 16:07:41235 VersionId = Ver.Id;
Rui Ueyama35fa6c52016-11-23 05:48:40236 else
Rui Ueyamaf1f00842017-10-31 16:07:41237 VersionId = Ver.Id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40238 return;
239 }
240
241 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13242 // Usually version script is not provided when linking executable,
243 // but we may still want to override a versioned symbol from DSO,
244 // so we do not report error in this case.
245 if (Config->Shared)
Rafael Espindola6e93d052017-08-04 22:31:42246 error(toString(getFile()) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13247 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40248}
249
Peter Collingbournee9a9e0a2017-11-06 04:35:31250template <class ELFT> bool Defined::isMipsPIC() const {
Rui Ueyamad97265f2017-06-09 21:09:08251 typedef typename ELFT::Ehdr Elf_Ehdr;
Simon Atanasyanf967f092016-09-29 12:58:36252 if (!Section || !isFunc())
253 return false;
Rui Ueyamad97265f2017-06-09 21:09:08254
255 auto *Sec = cast<InputSectionBase>(Section);
256 const Elf_Ehdr *Hdr = Sec->template getFile<ELFT>()->getObj().getHeader();
Simon Atanasyanf967f092016-09-29 12:58:36257 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
Rui Ueyamad97265f2017-06-09 21:09:08258 (Hdr->e_flags & EF_MIPS_PIC);
Simon Atanasyanf967f092016-09-29 12:58:36259}
260
Rui Ueyama55518e72016-10-28 20:57:25261InputFile *Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51262 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama55518e72016-10-28 20:57:25263 return S->fetch();
264 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51265}
266
Rafael Espindola6e93d052017-08-04 22:31:42267ArchiveFile *LazyArchive::getFile() {
Rui Ueyamaf52496e2017-11-03 21:21:47268 return cast<ArchiveFile>(Symbol::getFile());
Rui Ueyama434b5612016-07-17 03:11:46269}
270
Rui Ueyama55518e72016-10-28 20:57:25271InputFile *LazyArchive::fetch() {
Rafael Espindola6e93d052017-08-04 22:31:42272 std::pair<MemoryBufferRef, uint64_t> MBInfo = getFile()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10273
274 // getMember returns an empty buffer if the member was already
275 // read from the library.
Davide Italianobcdd6c62016-10-12 19:35:54276 if (MBInfo.first.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51277 return nullptr;
Rafael Espindola6e93d052017-08-04 22:31:42278 return createObjectFile(MBInfo.first, getFile()->getName(), MBInfo.second);
Michael J. Spencer1b348a62015-09-04 22:28:10279}
280
Rafael Espindola6e93d052017-08-04 22:31:42281LazyObjFile *LazyObject::getFile() {
Rui Ueyamaf52496e2017-11-03 21:21:47282 return cast<LazyObjFile>(Symbol::getFile());
Rafael Espindola6e93d052017-08-04 22:31:42283}
284
285InputFile *LazyObject::fetch() { return getFile()->fetch(); }
Rui Ueyamaf8baa662016-04-07 19:24:51286
Rui Ueyamaf52496e2017-11-03 21:21:47287uint8_t Symbol::computeBinding() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13288 if (Config->Relocatable)
289 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48290 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13291 return STB_LOCAL;
Peter Collingbourneb472aa02017-11-06 04:39:07292 if (VersionId == VER_NDX_LOCAL && isDefined())
Rafael Espindolab7e2ee22017-01-10 17:08:13293 return STB_LOCAL;
294 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
295 return STB_GLOBAL;
296 return Binding;
297}
298
Rui Ueyamaf52496e2017-11-03 21:21:47299bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02300 if (!Config->HasDynSymTab)
301 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13302 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25303 return false;
Peter Collingbourneb472aa02017-11-06 04:39:07304 if (!isDefined())
Rafael Espindola3d9f1c02017-09-13 20:43:04305 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53306 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25307}
Rui Ueyama69c778c2016-07-17 17:50:09308
309// Print out a log message for --trace-symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47310void elf::printTraceSymbol(Symbol *Sym) {
Rui Ueyamae6e206d2017-02-21 23:22:56311 std::string S;
Rui Ueyamaf1f00842017-10-31 16:07:41312 if (Sym->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56313 S = ": reference to ";
Rui Ueyamaf1f00842017-10-31 16:07:41314 else if (Sym->isLazy())
Rafael Espindolabc2b1652017-10-27 18:30:11315 S = ": lazy definition of ";
Rui Ueyamaf1f00842017-10-31 16:07:41316 else if (Sym->isShared())
Rafael Espindolabc2b1652017-10-27 18:30:11317 S = ": shared definition of ";
Peter Collingbournee9a9e0a2017-11-06 04:35:31318 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
Peter Collingbourne6c55a702017-11-06 04:33:58319 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09320 else
Rui Ueyamae6e206d2017-02-21 23:22:56321 S = ": definition of ";
322
Rui Ueyamaf1f00842017-10-31 16:07:41323 message(toString(Sym->File) + S + Sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09324}
325
Rui Ueyamaa3ac1732016-11-24 20:24:18326// Returns a symbol for an error message.
Rui Ueyamaf52496e2017-11-03 21:21:47327std::string lld::toString(const Symbol &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18328 if (Config->Demangle)
Rui Ueyama4c5b8ce2016-12-07 23:17:05329 if (Optional<std::string> S = demangle(B.getName()))
330 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18331 return B.getName();
332}
333
Peter Collingbournee9a9e0a2017-11-06 04:35:31334template bool Defined::template isMipsPIC<ELF32LE>() const;
335template bool Defined::template isMipsPIC<ELF32BE>() const;
336template bool Defined::template isMipsPIC<ELF64LE>() const;
337template bool Defined::template isMipsPIC<ELF64BE>() const;