blob: 8f6b4082d83c02e28f91362cb34af4ffae150fd5 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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:25 +000011#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:35 +000012#include "InputSection.h"
13#include "OutputSections.h"
Rui Ueyamaa3ac1732016-11-24 20:24:18 +000014#include "Strings.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000015#include "SyntheticSections.h"
Rui Ueyamab5a69702016-02-01 21:00:35 +000016#include "Target.h"
Rafael Espindola17cb7c02016-12-19 17:01:01 +000017#include "Writer.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018
Bob Haarmanb8a59c82017-10-25 22:28:38 +000019#include "lld/Common/ErrorHandler.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000020#include "llvm/ADT/STLExtras.h"
Eugene Leviantc958d8d2016-10-12 08:19:30 +000021#include "llvm/Support/Path.h"
Rui Ueyamac72ba3a2016-11-23 04:57:25 +000022#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:10 +000023
24using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:52 +000026using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
28using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000029using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
George Rimare6c5d3862017-04-05 10:03:25 +000031DefinedRegular *ElfSym::Bss;
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +000032DefinedRegular *ElfSym::Etext1;
Rafael Espindola5616adf2017-03-08 22:36:28 +000033DefinedRegular *ElfSym::Etext2;
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +000034DefinedRegular *ElfSym::Edata1;
Rafael Espindola5616adf2017-03-08 22:36:28 +000035DefinedRegular *ElfSym::Edata2;
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +000036DefinedRegular *ElfSym::End1;
Rafael Espindola5616adf2017-03-08 22:36:28 +000037DefinedRegular *ElfSym::End2;
Rui Ueyama92c37812017-06-26 15:11:24 +000038DefinedRegular *ElfSym::GlobalOffsetTable;
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +000039DefinedRegular *ElfSym::MipsGp;
Rui Ueyama80474a22017-02-28 19:29:55 +000040DefinedRegular *ElfSym::MipsGpDisp;
41DefinedRegular *ElfSym::MipsLocalGp;
Rui Ueyama80474a22017-02-28 19:29:55 +000042
George Rimarf64618a2017-03-17 11:56:54 +000043static uint64_t getSymVA(const SymbolBody &Body, int64_t &Addend) {
Rafael Espindola87d9f102016-03-11 12:19:05 +000044 switch (Body.kind()) {
Rafael Espindola87d9f102016-03-11 12:19:05 +000045 case SymbolBody::DefinedRegularKind: {
Rui Ueyama80474a22017-02-28 19:29:55 +000046 auto &D = cast<DefinedRegular>(Body);
Rafael Espindola5616adf2017-03-08 22:36:28 +000047 SectionBase *IS = D.Section;
48 if (auto *ISB = dyn_cast_or_null<InputSectionBase>(IS))
49 IS = ISB->Repl;
Rui Ueyamab5a69702016-02-01 21:00:35 +000050
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000051 // 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:07 +000055 if (IS == &InputSection::Discarded)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000056 return 0;
57
Rui Ueyamab5a69702016-02-01 21:00:35 +000058 // This is an absolute symbol.
Sean Silva902ae3c2016-12-15 00:57:53 +000059 if (!IS)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000060 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000061
Rafael Espindola9371bab2017-03-08 15:21:32 +000062 uint64_t Offset = D.Value;
Sean Silvaa9ba4502017-02-28 08:32:56 +000063
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:04 +000067 // 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:16 +000075 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000076 Offset += Addend;
77 Addend = 0;
78 }
Sean Silvaa9ba4502017-02-28 08:32:56 +000079
Rafael Espindola5e434b32017-03-08 16:08:36 +000080 const OutputSection *OutSec = IS->getOutputSection();
Sean Silva6ab39262017-02-28 09:01:58 +000081
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:41 +000092 uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
Sean Silva6ab39262017-02-28 09:01:58 +000093
George Rimar6a3b1542016-10-04 08:52:51 +000094 if (D.isTls() && !Config->Relocatable) {
Rui Ueyama9d1bacb12017-02-27 02:31:26 +000095 if (!Out::TlsPhdr)
Rafael Espindola6e93d052017-08-04 22:31:42 +000096 fatal(toString(D.getFile()) +
Peter Collingbourne3e2abde2017-07-14 00:22:46 +000097 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Rui Ueyama9d1bacb12017-02-27 02:31:26 +000098 return VA - Out::TlsPhdr->p_vaddr;
George Rimar6a3b1542016-10-04 08:52:51 +000099 }
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000100 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000101 }
Ben Dunbobbin73eabf22017-09-29 09:08:26 +0000102 case SymbolBody::DefinedCommonKind:
103 llvm_unreachable("common are converted to bss");
Rui Ueyama007c0022017-03-08 17:24:24 +0000104 case SymbolBody::SharedKind: {
105 auto &SS = cast<SharedSymbol>(Body);
Rafael Espindola0afcef22017-08-04 17:43:54 +0000106 if (SS.CopyRelSec)
Rafael Espindolaf6c74c42017-09-13 16:59:12 +0000107 return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff;
Rui Ueyama007c0022017-03-08 17:24:24 +0000108 if (SS.NeedsPltAddr)
George Rimarf64618a2017-03-17 11:56:54 +0000109 return Body.getPltVA();
Rui Ueyama924b3612017-02-16 06:12:22 +0000110 return 0;
Rui Ueyama007c0022017-03-08 17:24:24 +0000111 }
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000112 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000113 return 0;
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000114 case SymbolBody::LazyArchiveKind:
115 case SymbolBody::LazyObjectKind:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000116 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35 +0000117 return 0;
118 }
George Rimar777f9632016-03-12 08:31:34 +0000119 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +0000120}
121
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000122SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
123 uint8_t Type)
Rui Ueyama662bb002017-10-13 03:37:26 +0000124 : SymbolKind(K), IsLocal(IsLocal), NeedsPltAddr(false),
Peter Smithbaffdb82016-12-08 12:58:55 +0000125 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
Rafael Espindola35c908f2017-08-10 15:05:37 +0000126 IsInIgot(false), IsPreemptible(false), Type(Type), StOther(StOther),
127 Name(Name) {}
Rafael Espindolaf4765732016-04-06 13:22:41 +0000128
Rui Ueyamace2f5fd2017-10-13 02:57:59 +0000129// Returns true if this is a weak undefined symbol.
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000130bool SymbolBody::isUndefWeak() const {
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000131 // See comment on Lazy in Symbols.h for the details.
Rui Ueyamace2f5fd2017-10-13 02:57:59 +0000132 return !isLocal() && symbol()->isWeak() && (isUndefined() || isLazy());
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000133}
134
Rafael Espindola6e93d052017-08-04 22:31:42 +0000135InputFile *SymbolBody::getFile() const {
Rafael Espindoladb1af692017-08-11 17:47:12 +0000136 if (isLocal()) {
137 const SectionBase *Sec = cast<DefinedRegular>(this)->Section;
138 // Local absolute symbols actually have a file, but that is not currently
139 // used. We could support that by having a mostly redundant InputFile in
140 // SymbolBody, or having a special absolute section if needed.
141 return Sec ? cast<InputSectionBase>(Sec)->File : nullptr;
142 }
Rafael Espindola6e93d052017-08-04 22:31:42 +0000143 return symbol()->File;
144}
145
Rafael Espindolacf00d432017-07-05 00:43:18 +0000146// Overwrites all attributes with Other's so that this symbol becomes
147// an alias to Other. This is useful for handling some options such as
148// --wrap.
Rui Ueyama5bbe4a42017-09-25 00:57:30 +0000149void SymbolBody::copyFrom(SymbolBody *Other) {
Rui Ueyamab2269ec2017-06-28 19:43:02 +0000150 memcpy(symbol()->Body.buffer, Other->symbol()->Body.buffer,
151 sizeof(Symbol::Body));
Rui Ueyamab2269ec2017-06-28 19:43:02 +0000152}
153
George Rimarf64618a2017-03-17 11:56:54 +0000154uint64_t SymbolBody::getVA(int64_t Addend) const {
155 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19 +0000156 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000157}
158
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +0000159uint64_t SymbolBody::getGotVA() const {
Rafael Espindola88ab9fb2017-05-11 23:26:03 +0000160 return InX::Got->getVA() + getGotOffset();
Rafael Espindola74031ba2016-04-07 15:20:56 +0000161}
162
George Rimar4afe42e2017-03-17 14:12:51 +0000163uint64_t SymbolBody::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000164 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000165}
166
George Rimar4670bb02017-03-16 12:58:11 +0000167uint64_t SymbolBody::getGotPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55 +0000168 if (this->IsInIgot)
George Rimar4670bb02017-03-16 12:58:11 +0000169 return InX::IgotPlt->getVA() + getGotPltOffset();
170 return InX::GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56 +0000171}
172
George Rimar4670bb02017-03-16 12:58:11 +0000173uint64_t SymbolBody::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000174 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000175}
176
George Rimarf64618a2017-03-17 11:56:54 +0000177uint64_t SymbolBody::getPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55 +0000178 if (this->IsInIplt)
George Rimarf64618a2017-03-17 11:56:54 +0000179 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
180 return InX::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35 +0000181 PltIndex * Target->PltEntrySize;
182}
183
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000184template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000185 if (const auto *C = dyn_cast<DefinedCommon>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000186 return C->Size;
Rui Ueyama80474a22017-02-28 19:29:55 +0000187 if (const auto *DR = dyn_cast<DefinedRegular>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000188 return DR->Size;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000189 if (const auto *S = dyn_cast<SharedSymbol>(this))
190 return S->getSize<ELFT>();
Rui Ueyama512c61d2016-02-03 00:12:24 +0000191 return 0;
192}
193
George Rimar69268a82017-03-16 11:06:13 +0000194OutputSection *SymbolBody::getOutputSection() const {
Rui Ueyama80474a22017-02-28 19:29:55 +0000195 if (auto *S = dyn_cast<DefinedRegular>(this)) {
Rui Ueyama968db482017-02-28 04:02:42 +0000196 if (S->Section)
Rafael Espindola5e434b32017-03-08 16:08:36 +0000197 return S->Section->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42 +0000198 return nullptr;
199 }
200
Rui Ueyama007c0022017-03-08 17:24:24 +0000201 if (auto *S = dyn_cast<SharedSymbol>(this)) {
Rafael Espindola0afcef22017-08-04 17:43:54 +0000202 if (S->CopyRelSec)
Rafael Espindoladb5e56f2017-05-31 20:17:44 +0000203 return S->CopyRelSec->getParent();
Rui Ueyama968db482017-02-28 04:02:42 +0000204 return nullptr;
Rui Ueyama007c0022017-03-08 17:24:24 +0000205 }
Rui Ueyama968db482017-02-28 04:02:42 +0000206
Dmitry Mikulin1e30f072017-09-08 16:22:43 +0000207 if (auto *S = dyn_cast<DefinedCommon>(this)) {
Rui Ueyama968db482017-02-28 04:02:42 +0000208 if (Config->DefineCommon)
Dmitry Mikulin1e30f072017-09-08 16:22:43 +0000209 return S->Section->getParent();
Rui Ueyama968db482017-02-28 04:02:42 +0000210 return nullptr;
211 }
212
Rui Ueyama968db482017-02-28 04:02:42 +0000213 return nullptr;
214}
215
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000216// If a symbol name contains '@', the characters after that is
217// a symbol version name. This function parses that.
218void SymbolBody::parseSymbolVersion() {
219 StringRef S = getName();
220 size_t Pos = S.find('@');
221 if (Pos == 0 || Pos == StringRef::npos)
222 return;
223 StringRef Verstr = S.substr(Pos + 1);
224 if (Verstr.empty())
225 return;
226
227 // Truncate the symbol name so that it doesn't include the version string.
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000228 Name = {S.data(), Pos};
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000229
Rafael Espindola1d6d1b42017-01-17 16:08:06 +0000230 // If this is not in this DSO, it is not a definition.
231 if (!isInCurrentDSO())
Rafael Espindola2756e042017-01-06 22:30:35 +0000232 return;
233
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000234 // '@@' in a symbol name means the default version.
235 // It is usually the most recent one.
236 bool IsDefault = (Verstr[0] == '@');
237 if (IsDefault)
238 Verstr = Verstr.substr(1);
239
240 for (VersionDefinition &Ver : Config->VersionDefinitions) {
241 if (Ver.Name != Verstr)
242 continue;
243
244 if (IsDefault)
245 symbol()->VersionId = Ver.Id;
246 else
247 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
248 return;
249 }
250
251 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13 +0000252 // Usually version script is not provided when linking executable,
253 // but we may still want to override a versioned symbol from DSO,
254 // so we do not report error in this case.
255 if (Config->Shared)
Rafael Espindola6e93d052017-08-04 22:31:42 +0000256 error(toString(getFile()) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13 +0000257 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000258}
259
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000260Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
261 uint8_t Type)
262 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000263
Rui Ueyama80474a22017-02-28 19:29:55 +0000264template <class ELFT> bool DefinedRegular::isMipsPIC() const {
Rui Ueyamad97265f2017-06-09 21:09:08 +0000265 typedef typename ELFT::Ehdr Elf_Ehdr;
Simon Atanasyanf967f092016-09-29 12:58:36 +0000266 if (!Section || !isFunc())
267 return false;
Rui Ueyamad97265f2017-06-09 21:09:08 +0000268
269 auto *Sec = cast<InputSectionBase>(Section);
270 const Elf_Ehdr *Hdr = Sec->template getFile<ELFT>()->getObj().getHeader();
Simon Atanasyanf967f092016-09-29 12:58:36 +0000271 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
Rui Ueyamad97265f2017-06-09 21:09:08 +0000272 (Hdr->e_flags & EF_MIPS_PIC);
Simon Atanasyanf967f092016-09-29 12:58:36 +0000273}
274
Peter Smith3a52eb02017-02-01 10:26:03 +0000275Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
Rafael Espindola6e93d052017-08-04 22:31:42 +0000276 uint8_t Type)
277 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000278
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000279DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
Rafael Espindola6e93d052017-08-04 22:31:42 +0000280 uint8_t StOther, uint8_t Type)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000281 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
282 Type),
Ben Dunbobbin73eabf22017-09-29 09:08:26 +0000283 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000284
Rui Ueyama4076fa12017-02-26 23:35:34 +0000285// If a shared symbol is referred via a copy relocation, its alignment
286// becomes part of the ABI. This function returns a symbol alignment.
287// Because symbols don't have alignment attributes, we need to infer that.
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000288template <class ELFT> uint32_t SharedSymbol::getAlignment() const {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000289 SharedFile<ELFT> *File = getFile<ELFT>();
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000290 uint32_t SecAlign = File->getSection(getSym<ELFT>())->sh_addralign;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000291 uint64_t SymValue = getSym<ELFT>().st_value;
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000292 uint32_t SymAlign = uint32_t(1) << countTrailingZeros(SymValue);
Rui Ueyama4076fa12017-02-26 23:35:34 +0000293 return std::min(SecAlign, SymAlign);
294}
295
Rui Ueyama55518e72016-10-28 20:57:25 +0000296InputFile *Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000297 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama55518e72016-10-28 20:57:25 +0000298 return S->fetch();
299 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000300}
301
Rafael Espindola6e93d052017-08-04 22:31:42 +0000302LazyArchive::LazyArchive(const llvm::object::Archive::Symbol S, uint8_t Type)
303 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {}
Rui Ueyama434b5612016-07-17 03:11:46 +0000304
Rafael Espindola6e93d052017-08-04 22:31:42 +0000305LazyObject::LazyObject(StringRef Name, uint8_t Type)
306 : Lazy(LazyObjectKind, Name, Type) {}
307
308ArchiveFile *LazyArchive::getFile() {
309 return cast<ArchiveFile>(SymbolBody::getFile());
Rui Ueyama434b5612016-07-17 03:11:46 +0000310}
311
Rui Ueyama55518e72016-10-28 20:57:25 +0000312InputFile *LazyArchive::fetch() {
Rafael Espindola6e93d052017-08-04 22:31:42 +0000313 std::pair<MemoryBufferRef, uint64_t> MBInfo = getFile()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000314
315 // getMember returns an empty buffer if the member was already
316 // read from the library.
Davide Italianobcdd6c62016-10-12 19:35:54 +0000317 if (MBInfo.first.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000318 return nullptr;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000319 return createObjectFile(MBInfo.first, getFile()->getName(), MBInfo.second);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000320}
321
Rafael Espindola6e93d052017-08-04 22:31:42 +0000322LazyObjFile *LazyObject::getFile() {
323 return cast<LazyObjFile>(SymbolBody::getFile());
324}
325
326InputFile *LazyObject::fetch() { return getFile()->fetch(); }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000327
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000328uint8_t Symbol::computeBinding() const {
329 if (Config->Relocatable)
330 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48 +0000331 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000332 return STB_LOCAL;
Rui Ueyama273bbbc2017-04-24 23:50:58 +0000333 if (VersionId == VER_NDX_LOCAL && body()->isInCurrentDSO())
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000334 return STB_LOCAL;
335 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
336 return STB_GLOBAL;
337 return Binding;
338}
339
340bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02 +0000341 if (!Config->HasDynSymTab)
342 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000343 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000344 return false;
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000345 if (!body()->isInCurrentDSO())
346 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53 +0000347 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25 +0000348}
Rui Ueyama69c778c2016-07-17 17:50:09 +0000349
350// Print out a log message for --trace-symbol.
351void elf::printTraceSymbol(Symbol *Sym) {
352 SymbolBody *B = Sym->body();
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000353 std::string S;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000354 if (B->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000355 S = ": reference to ";
Rui Ueyama69c778c2016-07-17 17:50:09 +0000356 else if (B->isCommon())
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000357 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09 +0000358 else
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000359 S = ": definition of ";
360
Rafael Espindola6e93d052017-08-04 22:31:42 +0000361 message(toString(Sym->File) + S + B->getName());
Rui Ueyama69c778c2016-07-17 17:50:09 +0000362}
363
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000364// Returns a symbol for an error message.
Rui Ueyamace039262017-01-06 10:04:08 +0000365std::string lld::toString(const SymbolBody &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000366 if (Config->Demangle)
Rui Ueyama4c5b8ce2016-12-07 23:17:05 +0000367 if (Optional<std::string> S = demangle(B.getName()))
368 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000369 return B.getName();
370}
371
Rui Ueyama512c61d2016-02-03 00:12:24 +0000372template uint32_t SymbolBody::template getSize<ELF32LE>() const;
373template uint32_t SymbolBody::template getSize<ELF32BE>() const;
374template uint64_t SymbolBody::template getSize<ELF64LE>() const;
375template uint64_t SymbolBody::template getSize<ELF64BE>() const;
376
Rui Ueyama80474a22017-02-28 19:29:55 +0000377template bool DefinedRegular::template isMipsPIC<ELF32LE>() const;
378template bool DefinedRegular::template isMipsPIC<ELF32BE>() const;
379template bool DefinedRegular::template isMipsPIC<ELF64LE>() const;
380template bool DefinedRegular::template isMipsPIC<ELF64BE>() const;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000381
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000382template uint32_t SharedSymbol::template getAlignment<ELF32LE>() const;
383template uint32_t SharedSymbol::template getAlignment<ELF32BE>() const;
384template uint32_t SharedSymbol::template getAlignment<ELF64LE>() const;
385template uint32_t SharedSymbol::template getAlignment<ELF64BE>() const;