blob: 5c659522dac52b952956f5aedabc858a45947a60 [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"
Rafael Espindola9d06ab62015-09-22 00:01:39 +000011#include "InputSection.h"
Rafael Espindola49a2ca62015-08-06 15:33:19 +000012#include "Error.h"
Michael J. Spencercdae0a42015-07-28 22:58:25 +000013#include "InputFiles.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014
Michael J. Spencer1b348a62015-09-04 22:28:10 +000015#include "llvm/ADT/STLExtras.h"
16
17using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000018using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:52 +000019using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
21using namespace lld;
22using namespace lld::elf2;
23
Rafael Espindola78471f02015-09-01 23:12:52 +000024static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
25 if (VA == STV_DEFAULT)
26 return VB;
27 if (VB == STV_DEFAULT)
28 return VA;
29 return std::min(VA, VB);
30}
31
Michael J. Spencer84487f12015-07-24 21:03:07 +000032// Returns 1, 0 or -1 if this symbol should take precedence
33// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000034template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19 +000035 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10 +000036 assert(!isLazy() && !Other->isLazy());
Rafael Espindola30e17972015-08-30 23:17:30 +000037 std::pair<bool, bool> L(isDefined(), !isWeak());
38 std::pair<bool, bool> R(Other->isDefined(), !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000039
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000040 // Normalize
41 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +000042 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000043
Rui Ueyama8f2c4da2015-10-21 18:13:47 +000044 Visibility = Other->Visibility =
45 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +000046
Rui Ueyama86696f32015-10-21 19:41:03 +000047 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
48 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +000049
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000050 if (L != R)
51 return -1;
Rui Ueyama7da94a52015-09-09 17:40:51 +000052 if (!L.first || !L.second)
53 return 1;
Rafael Espindola8e5560d2015-09-23 14:23:59 +000054 if (isShared())
55 return -1;
56 if (Other->isShared())
57 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51 +000058 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09 +000059 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30 +000060 return -1;
Rui Ueyama6666f6a2015-09-09 17:55:09 +000061 auto *ThisC = cast<DefinedCommon<ELFT>>(this);
62 auto *OtherC = cast<DefinedCommon<ELFT>>(Other);
Rui Ueyama6be68522015-12-16 23:49:19 +000063 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rui Ueyama6666f6a2015-09-09 17:55:09 +000064 if (ThisC->Sym.st_size >= OtherC->Sym.st_size) {
Rui Ueyama6be68522015-12-16 23:49:19 +000065 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09 +000066 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000067 }
Rui Ueyama6be68522015-12-16 23:49:19 +000068 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51 +000069 return -1;
Rafael Espindola30e17972015-08-30 23:17:30 +000070 }
Rui Ueyama7da94a52015-09-09 17:40:51 +000071 if (Other->isCommon())
72 return 1;
73 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07 +000074}
Rafael Espindoladaa92a62015-08-31 01:16:19 +000075
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000076Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
77 bool IsTls)
78 : SymbolBody(K, Name, IsWeak, Visibility, IsTls) {}
79
Rafael Espindola5d7593b2015-12-22 23:00:50 +000080Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
81 uint8_t Visibility, bool IsTls)
82 : SymbolBody(K, N, IsWeak, Visibility, IsTls), CanKeepUndefined(false) {}
83
84Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
85 bool CanKeepUndefined)
86 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility,
87 /*IsTls*/ false) {
88 this->CanKeepUndefined = CanKeepUndefined;
89}
90
91template <typename ELFT>
92UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
93 : Undefined(SymbolBody::UndefinedElfKind, N,
94 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
95 Sym.getType() == llvm::ELF::STT_TLS),
96 Sym(Sym) {}
97
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000098template <typename ELFT>
99DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
100 OutputSectionBase<ELFT> &Section)
101 : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, false),
102 Value(Value), Section(Section) {}
103
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000104std::unique_ptr<InputFile> Lazy::getMember() {
105 MemoryBufferRef MBRef = File->getMember(&Sym);
106
107 // getMember returns an empty buffer if the member was already
108 // read from the library.
109 if (MBRef.getBuffer().empty())
110 return std::unique_ptr<InputFile>(nullptr);
111
112 return createELFFile<ObjectFile>(MBRef);
113}
114
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000115template <class ELFT> static void doInitSymbols() {
Igor Kudrinb044af52015-11-20 02:32:35 +0000116 DefinedAbsolute<ELFT>::End.setBinding(STB_GLOBAL);
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000117 DefinedAbsolute<ELFT>::IgnoreUndef.setBinding(STB_WEAK);
118 DefinedAbsolute<ELFT>::IgnoreUndef.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000119}
120
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000121void lld::elf2::initSymbols() {
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000122 doInitSymbols<ELF32LE>();
123 doInitSymbols<ELF32BE>();
124 doInitSymbols<ELF64LE>();
125 doInitSymbols<ELF64BE>();
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000126}
127
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000128template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
129template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
130template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
131template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000132
133template class lld::elf2::UndefinedElf<ELF32LE>;
134template class lld::elf2::UndefinedElf<ELF32BE>;
135template class lld::elf2::UndefinedElf<ELF64LE>;
136template class lld::elf2::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000137
138template class lld::elf2::DefinedSynthetic<ELF32LE>;
139template class lld::elf2::DefinedSynthetic<ELF32BE>;
140template class lld::elf2::DefinedSynthetic<ELF64LE>;
141template class lld::elf2::DefinedSynthetic<ELF64BE>;