Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1 | //===- OutputSections.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 "OutputSections.h" |
| 11 | #include "Config.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 12 | #include "SymbolTable.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 13 | #include "Target.h" |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 14 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 15 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | using namespace llvm::object; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 18 | using namespace llvm::support::endian; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 19 | using namespace llvm::ELF; |
| 20 | |
| 21 | using namespace lld; |
| 22 | using namespace lld::elf2; |
| 23 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 24 | template <class ELFT> |
| 25 | OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type, |
| 26 | uintX_t sh_flags) |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 27 | : Name(Name) { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 28 | memset(&Header, 0, sizeof(Elf_Shdr)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 29 | Header.sh_type = sh_type; |
| 30 | Header.sh_flags = sh_flags; |
| 31 | } |
| 32 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 33 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 34 | GotPltSection<ELFT>::GotPltSection() |
| 35 | : OutputSectionBase<ELFT>(".got.plt", llvm::ELF::SHT_PROGBITS, |
| 36 | llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) { |
| 37 | this->Header.sh_addralign = sizeof(uintX_t); |
| 38 | // .got.plt has 3 reserved entry |
| 39 | Entries.resize(3); |
| 40 | } |
| 41 | |
| 42 | template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) { |
| 43 | Sym->GotPltIndex = Entries.size(); |
| 44 | Entries.push_back(Sym); |
| 45 | } |
| 46 | |
| 47 | template <class ELFT> bool GotPltSection<ELFT>::empty() const { |
| 48 | return Entries.size() == 3; |
| 49 | } |
| 50 | |
| 51 | template <class ELFT> |
| 52 | typename GotPltSection<ELFT>::uintX_t |
| 53 | GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
| 54 | return this->getVA() + B.GotPltIndex * sizeof(uintX_t); |
| 55 | } |
| 56 | |
| 57 | template <class ELFT> void GotPltSection<ELFT>::finalize() { |
| 58 | this->Header.sh_size = Entries.size() * sizeof(uintX_t); |
| 59 | } |
| 60 | |
| 61 | template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 62 | write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>( |
| 63 | Buf, Out<ELFT>::Dynamic->getVA()); |
| 64 | for (const SymbolBody *B : Entries) { |
| 65 | if (B) |
| 66 | Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B)); |
| 67 | Buf += sizeof(uintX_t); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 72 | GotSection<ELFT>::GotSection() |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 73 | : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS, |
| 74 | llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) { |
Rui Ueyama | 5f551ae | 2015-10-14 14:02:06 +0000 | [diff] [blame] | 75 | this->Header.sh_addralign = sizeof(uintX_t); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 78 | template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) { |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 79 | Sym->GotIndex = Entries.size(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 80 | Entries.push_back(Sym); |
| 81 | } |
| 82 | |
| 83 | template <class ELFT> |
| 84 | typename GotSection<ELFT>::uintX_t |
| 85 | GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
Rui Ueyama | 5f551ae | 2015-10-14 14:02:06 +0000 | [diff] [blame] | 86 | return this->getVA() + B.GotIndex * sizeof(uintX_t); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 89 | template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { |
| 90 | for (const SymbolBody *B : Entries) { |
Rafael Espindola | e782f67 | 2015-10-07 03:56:05 +0000 | [diff] [blame] | 91 | uint8_t *Entry = Buf; |
| 92 | Buf += sizeof(uintX_t); |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 93 | if (canBePreempted(B, false)) |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 94 | continue; // The dynamic linker will take care of it. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 95 | uintX_t VA = getSymVA<ELFT>(*B); |
Rafael Espindola | e782f67 | 2015-10-07 03:56:05 +0000 | [diff] [blame] | 96 | write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 100 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 101 | PltSection<ELFT>::PltSection() |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 102 | : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS, |
| 103 | llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 104 | this->Header.sh_addralign = 16; |
| 105 | } |
| 106 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 107 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 242ddf4 | 2015-10-12 18:56:36 +0000 | [diff] [blame] | 108 | size_t Off = 0; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 109 | bool LazyReloc = Target->supportsLazyRelocations(); |
| 110 | if (LazyReloc) { |
| 111 | // First write PLT[0] entry which is special. |
| 112 | Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA()); |
| 113 | Off += Target->getPltZeroEntrySize(); |
| 114 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 115 | for (const SymbolBody *E : Entries) { |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 116 | uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E) |
| 117 | : Out<ELFT>::Got->getEntryAddr(*E); |
Rui Ueyama | 242ddf4 | 2015-10-12 18:56:36 +0000 | [diff] [blame] | 118 | uint64_t Plt = this->getVA() + Off; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 119 | Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex); |
Rui Ueyama | 242ddf4 | 2015-10-12 18:56:36 +0000 | [diff] [blame] | 120 | Off += Target->getPltEntrySize(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) { |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 125 | Sym->PltIndex = Entries.size(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 126 | Entries.push_back(Sym); |
| 127 | } |
| 128 | |
| 129 | template <class ELFT> |
| 130 | typename PltSection<ELFT>::uintX_t |
| 131 | PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 132 | return this->getVA() + Target->getPltZeroEntrySize() + |
| 133 | B.PltIndex * Target->getPltEntrySize(); |
| 134 | } |
| 135 | |
| 136 | template <class ELFT> void PltSection<ELFT>::finalize() { |
| 137 | this->Header.sh_size = Target->getPltZeroEntrySize() + |
| 138 | Entries.size() * Target->getPltEntrySize(); |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 142 | RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela) |
| 143 | : OutputSectionBase<ELFT>(Name, |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 144 | IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL, |
| 145 | llvm::ELF::SHF_ALLOC), |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 146 | IsRela(IsRela) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 147 | this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 148 | this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 149 | } |
| 150 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 151 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 152 | const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 153 | for (const DynamicReloc<ELFT> &Rel : Relocs) { |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 154 | auto *P = reinterpret_cast<Elf_Rel *>(Buf); |
| 155 | Buf += EntrySize; |
| 156 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 157 | const InputSection<ELFT> &C = Rel.C; |
| 158 | const Elf_Rel &RI = Rel.RI; |
Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 159 | uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); |
Rafael Espindola | 41127ad | 2015-10-05 22:49:16 +0000 | [diff] [blame] | 160 | const ObjectFile<ELFT> &File = *C.getFile(); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 161 | SymbolBody *Body = File.getSymbolBody(SymIndex); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 162 | if (Body) |
| 163 | Body = Body->repl(); |
Rafael Espindola | 41127ad | 2015-10-05 22:49:16 +0000 | [diff] [blame] | 164 | |
Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 165 | uint32_t Type = RI.getType(Config->Mips64EL); |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 166 | bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body); |
| 167 | bool CanBePreempted = canBePreempted(Body, NeedsGot); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 168 | bool LazyReloc = Body && Target->supportsLazyRelocations() && |
| 169 | Target->relocNeedsPlt(Type, *Body); |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 170 | |
| 171 | if (CanBePreempted) { |
| 172 | if (NeedsGot) |
| 173 | P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 174 | LazyReloc ? Target->getPltReloc() |
| 175 | : Target->getGotReloc(), |
| 176 | Config->Mips64EL); |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 177 | else |
| 178 | P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type, |
| 179 | Config->Mips64EL); |
| 180 | } else { |
Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 181 | P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL); |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 182 | } |
| 183 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 184 | if (NeedsGot) { |
| 185 | if (LazyReloc) |
| 186 | P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body); |
| 187 | else |
| 188 | P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body); |
| 189 | } else { |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 190 | P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 191 | } |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 192 | |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 193 | uintX_t OrigAddend = 0; |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 194 | if (IsRela && !NeedsGot) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 195 | OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend; |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 196 | |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 197 | uintX_t Addend; |
Rui Ueyama | b7f2867 | 2015-10-19 20:31:49 +0000 | [diff] [blame] | 198 | if (CanBePreempted) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 199 | Addend = OrigAddend; |
Rui Ueyama | b7f2867 | 2015-10-19 20:31:49 +0000 | [diff] [blame] | 200 | else if (Body) |
| 201 | Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend; |
| 202 | else if (IsRela) |
| 203 | Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI)); |
| 204 | else |
| 205 | Addend = getLocalRelTarget(File, RI); |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 206 | |
| 207 | if (IsRela) |
| 208 | static_cast<Elf_Rela *>(P)->r_addend = Addend; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | template <class ELFT> void RelocationSection<ELFT>::finalize() { |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 213 | this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 214 | this->Header.sh_size = Relocs.size() * this->Header.sh_entsize; |
| 215 | } |
| 216 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 217 | template <class ELFT> |
| 218 | InterpSection<ELFT>::InterpSection() |
| 219 | : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS, |
| 220 | llvm::ELF::SHF_ALLOC) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 221 | this->Header.sh_size = Config->DynamicLinker.size() + 1; |
| 222 | this->Header.sh_addralign = 1; |
| 223 | } |
| 224 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 225 | template <class ELFT> |
| 226 | void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) { |
Rui Ueyama | 157c433 | 2015-10-24 17:57:39 +0000 | [diff] [blame] | 227 | Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name); |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 228 | *SHdr = Header; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 231 | template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 232 | memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size()); |
| 233 | } |
| 234 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 235 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 236 | HashTableSection<ELFT>::HashTableSection() |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 237 | : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH, |
| 238 | llvm::ELF::SHF_ALLOC) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 239 | this->Header.sh_entsize = sizeof(Elf_Word); |
| 240 | this->Header.sh_addralign = sizeof(Elf_Word); |
| 241 | } |
| 242 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 243 | static uint32_t hashSysv(StringRef Name) { |
Rui Ueyama | 5f1eee1a | 2015-10-15 21:27:17 +0000 | [diff] [blame] | 244 | uint32_t H = 0; |
| 245 | for (char C : Name) { |
| 246 | H = (H << 4) + C; |
| 247 | uint32_t G = H & 0xf0000000; |
| 248 | if (G) |
| 249 | H ^= G >> 24; |
| 250 | H &= ~G; |
| 251 | } |
| 252 | return H; |
| 253 | } |
| 254 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 255 | template <class ELFT> void HashTableSection<ELFT>::finalize() { |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 256 | this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 257 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 258 | unsigned NumEntries = 2; // nbucket and nchain. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 259 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 260 | |
| 261 | // Create as many buckets as there are symbols. |
| 262 | // FIXME: This is simplistic. We can try to optimize it, but implementing |
| 263 | // support for SHT_GNU_HASH is probably even more profitable. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 264 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 265 | this->Header.sh_size = NumEntries * sizeof(Elf_Word); |
| 266 | } |
| 267 | |
| 268 | template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 269 | unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 270 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 271 | *P++ = NumSymbols; // nbucket |
| 272 | *P++ = NumSymbols; // nchain |
| 273 | |
| 274 | Elf_Word *Buckets = P; |
| 275 | Elf_Word *Chains = P + NumSymbols; |
| 276 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 277 | for (const typename SymbolTableSection<ELFT>::SymbolData &Item : |
| 278 | Out<ELFT>::DynSymTab->getSymbols()) { |
| 279 | SymbolBody *Body = Item.Body; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 280 | StringRef Name = Body->getName(); |
| 281 | unsigned I = Body->getDynamicSymbolTableIndex(); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 282 | uint32_t Hash = hashSysv(Name) % NumSymbols; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 283 | Chains[I] = Buckets[Hash]; |
| 284 | Buckets[Hash] = I; |
| 285 | } |
| 286 | } |
| 287 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 288 | static uint32_t hashGnu(StringRef Name) { |
| 289 | uint32_t H = 5381; |
| 290 | for (uint8_t C : Name) |
| 291 | H = (H << 5) + H + C; |
| 292 | return H; |
| 293 | } |
| 294 | |
| 295 | template <class ELFT> |
| 296 | GnuHashTableSection<ELFT>::GnuHashTableSection() |
| 297 | : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH, |
| 298 | llvm::ELF::SHF_ALLOC) { |
| 299 | this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4; |
| 300 | this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 301 | } |
| 302 | |
| 303 | template <class ELFT> |
| 304 | unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) { |
| 305 | if (!NumHashed) |
| 306 | return 0; |
| 307 | |
| 308 | // These values are prime numbers which are not greater than 2^(N-1) + 1. |
| 309 | // In result, for any particular NumHashed we return a prime number |
| 310 | // which is not greater than NumHashed. |
| 311 | static const unsigned Primes[] = { |
| 312 | 1, 1, 3, 3, 7, 13, 31, 61, 127, 251, |
| 313 | 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071}; |
| 314 | |
| 315 | return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed), |
| 316 | array_lengthof(Primes) - 1)]; |
| 317 | } |
| 318 | |
| 319 | // Bloom filter estimation: at least 8 bits for each hashed symbol. |
| 320 | // GNU Hash table requirement: it should be a power of 2, |
| 321 | // the minimum value is 1, even for an empty table. |
| 322 | // Expected results for a 32-bit target: |
| 323 | // calcMaskWords(0..4) = 1 |
| 324 | // calcMaskWords(5..8) = 2 |
| 325 | // calcMaskWords(9..16) = 4 |
| 326 | // For a 64-bit target: |
| 327 | // calcMaskWords(0..8) = 1 |
| 328 | // calcMaskWords(9..16) = 2 |
| 329 | // calcMaskWords(17..32) = 4 |
| 330 | template <class ELFT> |
| 331 | unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) { |
| 332 | if (!NumHashed) |
| 333 | return 1; |
| 334 | return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off)); |
| 335 | } |
| 336 | |
| 337 | template <class ELFT> void GnuHashTableSection<ELFT>::finalize() { |
| 338 | const unsigned NumHashed = Out<ELFT>::DynSymTab->getNumGnuHashSymbols(); |
| 339 | NBuckets = calcNBuckets(NumHashed); |
| 340 | MaskWords = calcMaskWords(NumHashed); |
| 341 | // Second hash shift estimation: just predefined values. |
| 342 | Shift2 = ELFT::Is64Bits ? 6 : 5; |
| 343 | |
| 344 | this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; |
| 345 | this->Header.sh_size = sizeof(Elf_Word) * 4 // Header |
| 346 | + sizeof(Elf_Off) * MaskWords // Bloom Filter |
| 347 | + sizeof(Elf_Word) * NBuckets // Hash Buckets |
| 348 | + sizeof(Elf_Word) * NumHashed; // Hash Values |
| 349 | } |
| 350 | |
| 351 | template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 352 | writeHeader(Buf); |
| 353 | if (!NBuckets) // There are no hashed symbols |
| 354 | return; |
| 355 | writeBloomFilter(Buf); |
| 356 | writeHashTable(Buf); |
| 357 | } |
| 358 | |
| 359 | template <class ELFT> |
| 360 | void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) { |
| 361 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 362 | *P++ = NBuckets; |
| 363 | *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - |
| 364 | Out<ELFT>::DynSymTab->getNumGnuHashSymbols(); |
| 365 | *P++ = MaskWords; |
| 366 | *P++ = Shift2; |
| 367 | Buf = reinterpret_cast<uint8_t *>(P); |
| 368 | } |
| 369 | |
| 370 | template <class ELFT> |
| 371 | void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) { |
| 372 | unsigned C = sizeof(Elf_Off) * 8; |
| 373 | |
| 374 | auto *Masks = reinterpret_cast<Elf_Off *>(Buf); |
| 375 | for (const typename SymbolTableSection<ELFT>::SymbolData &Item : |
| 376 | Out<ELFT>::DynSymTab->getGnuHashSymbols()) { |
| 377 | size_t Pos = (Item.GnuHash / C) & (MaskWords - 1); |
| 378 | uintX_t V = (uintX_t(1) << (Item.GnuHash % C)) | |
| 379 | (uintX_t(1) << ((Item.GnuHash >> Shift2) % C)); |
| 380 | Masks[Pos] |= V; |
| 381 | } |
| 382 | Buf += sizeof(Elf_Off) * MaskWords; |
| 383 | } |
| 384 | |
| 385 | template <class ELFT> |
| 386 | void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { |
| 387 | Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); |
| 388 | Elf_Word *Values = Buckets + NBuckets; |
| 389 | |
| 390 | int PrevBucket = -1; |
| 391 | int I = 0; |
| 392 | for (const typename SymbolTableSection<ELFT>::SymbolData &Item : |
| 393 | Out<ELFT>::DynSymTab->getGnuHashSymbols()) { |
| 394 | int Bucket = Item.GnuHash % NBuckets; |
| 395 | assert(PrevBucket <= Bucket); |
| 396 | if (Bucket != PrevBucket) { |
| 397 | Buckets[Bucket] = Item.Body->getDynamicSymbolTableIndex(); |
| 398 | PrevBucket = Bucket; |
| 399 | if (I > 0) |
| 400 | Values[I - 1] |= 1; |
| 401 | } |
| 402 | Values[I] = Item.GnuHash & ~1; |
| 403 | ++I; |
| 404 | } |
| 405 | if (I > 0) |
| 406 | Values[I - 1] |= 1; |
| 407 | } |
| 408 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 409 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 410 | DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab) |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 411 | : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC, |
| 412 | llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE), |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 413 | SymTab(SymTab) { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 414 | Elf_Shdr &Header = this->Header; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 415 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 416 | Header.sh_entsize = ELFT::Is64Bits ? 16 : 8; |
| 417 | } |
| 418 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 419 | template <class ELFT> void DynamicSection<ELFT>::finalize() { |
Michael J. Spencer | 52bf0eb | 2015-10-01 21:15:02 +0000 | [diff] [blame] | 420 | if (this->Header.sh_size) |
| 421 | return; // Already finalized. |
| 422 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 423 | Elf_Shdr &Header = this->Header; |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 424 | Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 425 | |
| 426 | unsigned NumEntries = 0; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 427 | if (Out<ELFT>::RelaDyn->hasRelocs()) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 428 | ++NumEntries; // DT_RELA / DT_REL |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 429 | ++NumEntries; // DT_RELASZ / DT_RELSZ |
| 430 | ++NumEntries; // DT_RELAENT / DT_RELENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 431 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 432 | if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) { |
| 433 | ++NumEntries; // DT_JMPREL |
| 434 | ++NumEntries; // DT_PLTRELSZ |
| 435 | ++NumEntries; // DT_PLTGOT |
| 436 | ++NumEntries; // DT_PLTREL |
| 437 | } |
| 438 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 439 | ++NumEntries; // DT_SYMTAB |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 440 | ++NumEntries; // DT_SYMENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 441 | ++NumEntries; // DT_STRTAB |
| 442 | ++NumEntries; // DT_STRSZ |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 443 | if (Out<ELFT>::GnuHashTab) |
| 444 | ++NumEntries; // DT_GNU_HASH |
| 445 | if (Out<ELFT>::HashTab) |
| 446 | ++NumEntries; // DT_HASH |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 447 | |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 448 | if (!Config->RPath.empty()) { |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 449 | ++NumEntries; // DT_RUNPATH / DT_RPATH |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 450 | Out<ELFT>::DynStrTab->add(Config->RPath); |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (!Config->SoName.empty()) { |
| 454 | ++NumEntries; // DT_SONAME |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 455 | Out<ELFT>::DynStrTab->add(Config->SoName); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 458 | if (PreInitArraySec) |
| 459 | NumEntries += 2; |
| 460 | if (InitArraySec) |
| 461 | NumEntries += 2; |
| 462 | if (FiniArraySec) |
| 463 | NumEntries += 2; |
| 464 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 465 | for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) { |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 466 | if (!F->isNeeded()) |
| 467 | continue; |
Rui Ueyama | 6ccc8ca | 2015-10-09 20:32:54 +0000 | [diff] [blame] | 468 | Out<ELFT>::DynStrTab->add(F->getSoName()); |
| 469 | ++NumEntries; |
| 470 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 471 | |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 472 | if (Symbol *S = SymTab.getSymbols().lookup(Config->Init)) |
| 473 | InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body); |
| 474 | if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini)) |
| 475 | FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body); |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 476 | if (InitSym) |
| 477 | ++NumEntries; // DT_INIT |
| 478 | if (FiniSym) |
| 479 | ++NumEntries; // DT_FINI |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 480 | |
| 481 | if (Config->Bsymbolic) |
| 482 | DtFlags |= DF_SYMBOLIC; |
| 483 | if (Config->ZNodelete) |
| 484 | DtFlags1 |= DF_1_NODELETE; |
| 485 | if (Config->ZNow) { |
| 486 | DtFlags |= DF_BIND_NOW; |
| 487 | DtFlags1 |= DF_1_NOW; |
| 488 | } |
| 489 | if (Config->ZOrigin) { |
| 490 | DtFlags |= DF_ORIGIN; |
| 491 | DtFlags1 |= DF_1_ORIGIN; |
| 492 | } |
| 493 | |
| 494 | if (DtFlags) |
Davide Italiano | 58cbaf0 | 2015-10-19 23:32:16 +0000 | [diff] [blame] | 495 | ++NumEntries; // DT_FLAGS |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 496 | if (DtFlags1) |
George Rimar | 97aad17 | 2015-10-07 15:00:21 +0000 | [diff] [blame] | 497 | ++NumEntries; // DT_FLAGS_1 |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 498 | ++NumEntries; // DT_NULL |
| 499 | |
| 500 | Header.sh_size = NumEntries * Header.sh_entsize; |
| 501 | } |
| 502 | |
| 503 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 504 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 505 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 506 | auto WritePtr = [&](int32_t Tag, uint64_t Val) { |
| 507 | P->d_tag = Tag; |
| 508 | P->d_un.d_ptr = Val; |
| 509 | ++P; |
| 510 | }; |
| 511 | |
| 512 | auto WriteVal = [&](int32_t Tag, uint32_t Val) { |
| 513 | P->d_tag = Tag; |
| 514 | P->d_un.d_val = Val; |
| 515 | ++P; |
| 516 | }; |
| 517 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 518 | if (Out<ELFT>::RelaDyn->hasRelocs()) { |
| 519 | bool IsRela = Out<ELFT>::RelaDyn->isRela(); |
| 520 | WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA()); |
| 521 | WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 522 | WriteVal(IsRela ? DT_RELAENT : DT_RELENT, |
| 523 | IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 524 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 525 | if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) { |
| 526 | WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA()); |
| 527 | WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()); |
| 528 | WritePtr(DT_PLTGOT, Out<ELFT>::GotPlt->getVA()); |
| 529 | WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL); |
| 530 | } |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 531 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 532 | WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA()); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 533 | WritePtr(DT_SYMENT, sizeof(Elf_Sym)); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 534 | WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA()); |
| 535 | WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size()); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 536 | if (Out<ELFT>::GnuHashTab) |
| 537 | WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA()); |
| 538 | if (Out<ELFT>::HashTab) |
| 539 | WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 540 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 541 | if (!Config->RPath.empty()) |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 542 | |
| 543 | // If --enable-new-dtags is set lld emits DT_RUNPATH |
| 544 | // instead of DT_RPATH. The two tags are functionally |
| 545 | // equivalent except for the following: |
| 546 | // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while |
| 547 | // DT_RPATH is searched before. |
| 548 | // - DT_RUNPATH is used only to search for direct |
| 549 | // dependencies of the object it's contained in, while |
| 550 | // DT_RPATH is used for indirect dependencies as well. |
| 551 | WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, |
Rui Ueyama | 9fbb3d8 | 2015-10-24 17:44:52 +0000 | [diff] [blame] | 552 | Out<ELFT>::DynStrTab->getOffset(Config->RPath)); |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 553 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 554 | if (!Config->SoName.empty()) |
Rui Ueyama | 9fbb3d8 | 2015-10-24 17:44:52 +0000 | [diff] [blame] | 555 | WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 556 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 557 | auto WriteArray = [&](int32_t T1, int32_t T2, |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 558 | const OutputSectionBase<ELFT> *Sec) { |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 559 | if (!Sec) |
| 560 | return; |
| 561 | WritePtr(T1, Sec->getVA()); |
| 562 | WriteVal(T2, Sec->getSize()); |
| 563 | }; |
| 564 | WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec); |
| 565 | WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec); |
| 566 | WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec); |
| 567 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 568 | for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 569 | if (F->isNeeded()) |
Rui Ueyama | 9fbb3d8 | 2015-10-24 17:44:52 +0000 | [diff] [blame] | 570 | WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName())); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 571 | |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 572 | if (InitSym) |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 573 | WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym)); |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 574 | if (FiniSym) |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 575 | WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym)); |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 576 | if (DtFlags) |
| 577 | WriteVal(DT_FLAGS, DtFlags); |
| 578 | if (DtFlags1) |
| 579 | WriteVal(DT_FLAGS_1, DtFlags1); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 580 | WriteVal(DT_NULL, 0); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 584 | OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 585 | uintX_t sh_flags) |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 586 | : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {} |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 587 | |
| 588 | template <class ELFT> |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 589 | void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) { |
| 590 | Sections.push_back(C); |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 591 | C->OutSec = this; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 592 | uint32_t Align = C->getAlign(); |
| 593 | if (Align > this->Header.sh_addralign) |
| 594 | this->Header.sh_addralign = Align; |
| 595 | |
| 596 | uintX_t Off = this->Header.sh_size; |
| 597 | Off = RoundUpToAlignment(Off, Align); |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 598 | C->OutSecOff = Off; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 599 | Off += C->getSize(); |
| 600 | this->Header.sh_size = Off; |
| 601 | } |
| 602 | |
| 603 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 604 | typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) { |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 605 | switch (S.kind()) { |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 606 | case SymbolBody::DefinedSyntheticKind: { |
| 607 | auto &D = cast<DefinedSynthetic<ELFT>>(S); |
| 608 | return D.Section.getVA() + D.Sym.st_value; |
| 609 | } |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 610 | case SymbolBody::DefinedAbsoluteKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 611 | return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 612 | case SymbolBody::DefinedRegularKind: { |
| 613 | const auto &DR = cast<DefinedRegular<ELFT>>(S); |
Rafael Espindola | 48225b4 | 2015-10-23 19:55:11 +0000 | [diff] [blame] | 614 | InputSectionBase<ELFT> &SC = DR.Section; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 615 | return SC.OutSec->getVA() + SC.getOffset(DR.Sym); |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 616 | } |
| 617 | case SymbolBody::DefinedCommonKind: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 618 | return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 619 | case SymbolBody::SharedKind: |
| 620 | case SymbolBody::UndefinedKind: |
| 621 | return 0; |
| 622 | case SymbolBody::LazyKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 623 | assert(S.isUsedInRegularObj() && "Lazy symbol reached writer"); |
| 624 | return 0; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 625 | } |
Denis Protivensky | 92aa1c0 | 2015-10-07 08:21:34 +0000 | [diff] [blame] | 626 | llvm_unreachable("Invalid symbol kind"); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 629 | // Returns a VA which a relocatin RI refers to. Used only for local symbols. |
| 630 | // For non-local symbols, use getSymVA instead. |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 631 | template <class ELFT, bool IsRela> |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 632 | typename ELFFile<ELFT>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 633 | lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File, |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 634 | const Elf_Rel_Impl<ELFT, IsRela> &RI) { |
| 635 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 636 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 637 | |
| 638 | uintX_t Addend = getAddend<ELFT>(RI); |
| 639 | |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 640 | // PPC64 has a special relocation representing the TOC base pointer |
| 641 | // that does not have a corresponding symbol. |
Hal Finkel | 230c5c5 | 2015-10-16 22:37:32 +0000 | [diff] [blame] | 642 | if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 643 | return getPPC64TocBase() + Addend; |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 644 | |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 645 | const Elf_Sym *Sym = |
| 646 | File.getObj().getRelocationSymbol(&RI, File.getSymbolTable()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 647 | |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 648 | if (!Sym) |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 649 | error("Unsupported relocation without symbol"); |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 650 | |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 651 | // According to the ELF spec reference to a local symbol from outside |
| 652 | // the group are not allowed. Unfortunately .eh_frame breaks that rule |
| 653 | // and must be treated specially. For now we just replace the symbol with |
| 654 | // 0. |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 655 | InputSectionBase<ELFT> *Section = File.getSection(*Sym); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 656 | if (Section == &InputSection<ELFT>::Discarded) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 657 | return Addend; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 658 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 659 | uintX_t VA = Section->OutSec->getVA(); |
| 660 | if (isa<InputSection<ELFT>>(Section)) |
| 661 | return VA + Section->getOffset(*Sym) + Addend; |
| 662 | |
| 663 | uintX_t Offset = Sym->st_value; |
| 664 | if (Sym->getType() == STT_SECTION) { |
| 665 | Offset += Addend; |
Rafael Espindola | f5af835 | 2015-10-20 22:08:49 +0000 | [diff] [blame] | 666 | Addend = 0; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 667 | } |
| 668 | return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) + |
| 669 | Addend; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 672 | // Returns true if a symbol can be replaced at load-time by a symbol |
| 673 | // with the same name defined in other ELF executable or DSO. |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 674 | bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) { |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 675 | if (!Body) |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 676 | return false; // Body is a local symbol. |
Rafael Espindola | cea0b3b | 2015-10-07 04:22:55 +0000 | [diff] [blame] | 677 | if (Body->isShared()) |
| 678 | return true; |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 679 | |
| 680 | if (Body->isUndefined()) { |
| 681 | if (!Body->isWeak()) |
| 682 | return true; |
| 683 | |
| 684 | // This is an horrible corner case. Ideally we would like to say that any |
| 685 | // undefined symbol can be preempted so that the dynamic linker has a |
| 686 | // chance of finding it at runtime. |
| 687 | // |
| 688 | // The problem is that the code sequence used to test for weak undef |
| 689 | // functions looks like |
| 690 | // if (func) func() |
| 691 | // If the code is -fPIC the first reference is a load from the got and |
| 692 | // everything works. |
| 693 | // If the code is not -fPIC there is no reasonable way to solve it: |
| 694 | // * A relocation writing to the text segment will fail (it is ro). |
| 695 | // * A copy relocation doesn't work for functions. |
| 696 | // * The trick of using a plt entry as the address would fail here since |
| 697 | // the plt entry would have a non zero address. |
| 698 | // Since we cannot do anything better, we just resolve the symbol to 0 and |
| 699 | // don't produce a dynamic relocation. |
Hal Finkel | c917406 | 2015-10-16 22:11:05 +0000 | [diff] [blame] | 700 | // |
| 701 | // As an extra hack, assume that if we are producing a shared library the |
| 702 | // user knows what he or she is doing and can handle a dynamic relocation. |
| 703 | return Config->Shared || NeedsGot; |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 704 | } |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 705 | if (!Config->Shared) |
| 706 | return false; |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 707 | return Body->getVisibility() == STV_DEFAULT; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 710 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 711 | for (InputSection<ELFT> *C : Sections) |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 712 | C->writeTo(Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 715 | template <class ELFT> |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 716 | MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type, |
| 717 | uintX_t sh_flags) |
| 718 | : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {} |
| 719 | |
| 720 | template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame^] | 721 | if (shouldTailMerge()) { |
| 722 | StringRef Data = Builder.data(); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 723 | memcpy(Buf, Data.data(), Data.size()); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame^] | 724 | return; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 725 | } |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame^] | 726 | for (const std::pair<StringRef, size_t> &P : Builder.getMap()) { |
| 727 | StringRef Data = P.first; |
| 728 | memcpy(Buf + P.second, Data.data(), Data.size()); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | static size_t findNull(StringRef S, size_t EntSize) { |
| 733 | // Optimize the common case. |
| 734 | if (EntSize == 1) |
| 735 | return S.find(0); |
| 736 | |
| 737 | for (unsigned I = 0, N = S.size(); I != N; I += EntSize) { |
| 738 | const char *B = S.begin() + I; |
| 739 | if (std::all_of(B, B + EntSize, [](char C) { return C == 0; })) |
| 740 | return I; |
| 741 | } |
| 742 | return StringRef::npos; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | template <class ELFT> |
| 746 | void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) { |
| 747 | S->OutSec = this; |
| 748 | uint32_t Align = S->getAlign(); |
| 749 | if (Align > this->Header.sh_addralign) |
| 750 | this->Header.sh_addralign = Align; |
| 751 | |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame^] | 752 | ArrayRef<uint8_t> D = S->getSectionData(); |
| 753 | StringRef Data((char *)D.data(), D.size()); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 754 | uintX_t EntSize = S->getSectionHdr()->sh_entsize; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame^] | 755 | uintX_t Offset = 0; |
| 756 | |
| 757 | if (this->Header.sh_flags & SHF_STRINGS) { |
| 758 | while (!Data.empty()) { |
| 759 | size_t End = findNull(Data, EntSize); |
| 760 | if (End == StringRef::npos) |
| 761 | error("String is not null terminated"); |
| 762 | StringRef Entry = Data.substr(0, End + EntSize); |
| 763 | size_t OutputOffset = Builder.add(Entry); |
| 764 | if (shouldTailMerge()) |
| 765 | OutputOffset = -1; |
| 766 | S->Offsets.push_back(std::make_pair(Offset, OutputOffset)); |
| 767 | uintX_t Size = End + EntSize; |
| 768 | Data = Data.substr(Size); |
| 769 | Offset += Size; |
| 770 | } |
| 771 | } else { |
| 772 | for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) { |
| 773 | StringRef Entry = Data.substr(I, EntSize); |
| 774 | size_t OutputOffset = Builder.add(Entry); |
| 775 | S->Offsets.push_back(std::make_pair(Offset, OutputOffset)); |
| 776 | Offset += EntSize; |
| 777 | } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 778 | } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | template <class ELFT> |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame^] | 782 | unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) { |
| 783 | return Builder.getOffset(Val); |
| 784 | } |
| 785 | |
| 786 | template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const { |
| 787 | return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS; |
| 788 | } |
| 789 | |
| 790 | template <class ELFT> void MergeOutputSection<ELFT>::finalize() { |
| 791 | if (shouldTailMerge()) |
| 792 | Builder.finalize(); |
| 793 | this->Header.sh_size = Builder.getSize(); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | template <class ELFT> |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 797 | StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic) |
| 798 | : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB, |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 799 | Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0), |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 800 | Dynamic(Dynamic) { |
| 801 | this->Header.sh_addralign = 1; |
| 802 | } |
| 803 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 804 | template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 805 | StringRef Data = StrTabBuilder.data(); |
| 806 | memcpy(Buf, Data.data(), Data.size()); |
| 807 | } |
| 808 | |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 809 | template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 810 | if (!B.isUsedInRegularObj()) |
| 811 | return false; |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 812 | |
| 813 | // Don't include synthetic symbols like __init_array_start in every output. |
| 814 | if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B)) |
| 815 | if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef) |
| 816 | return false; |
| 817 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 818 | return true; |
| 819 | } |
| 820 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 821 | bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) { |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 822 | uint8_t V = B.getVisibility(); |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 823 | if (V != STV_DEFAULT && V != STV_PROTECTED) |
| 824 | return false; |
| 825 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 826 | if (Config->ExportDynamic || Config->Shared) |
| 827 | return true; |
| 828 | return B.isUsedInDynamicReloc(); |
| 829 | } |
| 830 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 831 | bool lld::elf2::includeInGnuHashTable(const SymbolBody &B) { |
| 832 | // Assume that includeInDynamicSymtab() is already checked. |
| 833 | return !B.isUndefined(); |
| 834 | } |
| 835 | |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 836 | template <class ELFT> |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 837 | bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File, |
| 838 | StringRef SymName, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 839 | const typename ELFFile<ELFT>::Elf_Sym &Sym) { |
| 840 | if (Sym.getType() == STT_SECTION) |
| 841 | return false; |
| 842 | |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 843 | // If sym references a section in a discarded group, don't keep it. |
Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 844 | if (File.getSection(Sym) == &InputSection<ELFT>::Discarded) |
| 845 | return false; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 846 | |
Davide Italiano | 6993ba4 | 2015-09-26 00:47:56 +0000 | [diff] [blame] | 847 | if (Config->DiscardNone) |
| 848 | return true; |
| 849 | |
| 850 | // ELF defines dynamic locals as symbols which name starts with ".L". |
| 851 | return !(Config->DiscardLocals && SymName.startswith(".L")); |
| 852 | } |
| 853 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 854 | template <class ELFT> |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 855 | SymbolTableSection<ELFT>::SymbolData::SymbolData(SymbolBody *Body, |
| 856 | bool HasGnuHash) |
| 857 | : Body(Body), HasGnuHash(HasGnuHash), |
| 858 | GnuHash(HasGnuHash ? hashGnu(Body->getName()) : 0) {} |
| 859 | |
| 860 | template <class ELFT> |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 861 | SymbolTableSection<ELFT>::SymbolTableSection( |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 862 | SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec) |
| 863 | : OutputSectionBase<ELFT>( |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 864 | StrTabSec.isDynamic() ? ".dynsym" : ".symtab", |
| 865 | StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB, |
| 866 | StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0), |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 867 | Table(Table), StrTabSec(StrTabSec) { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 868 | typedef OutputSectionBase<ELFT> Base; |
| 869 | typename Base::Elf_Shdr &Header = this->Header; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 870 | |
| 871 | Header.sh_entsize = sizeof(Elf_Sym); |
| 872 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 873 | } |
| 874 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 875 | template <class ELFT> void SymbolTableSection<ELFT>::finalize() { |
| 876 | this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym); |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 877 | this->Header.sh_link = StrTabSec.SectionIndex; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 878 | this->Header.sh_info = NumLocals + 1; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 879 | |
| 880 | if (!StrTabSec.isDynamic()) { |
| 881 | std::stable_sort(Symbols.begin(), Symbols.end(), |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 882 | [](const SymbolData &L, const SymbolData &R) { |
| 883 | return getSymbolBinding(L.Body) == STB_LOCAL && |
| 884 | getSymbolBinding(R.Body) != STB_LOCAL; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 885 | }); |
| 886 | return; |
| 887 | } |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 888 | if (NumGnuHashed) { |
| 889 | unsigned NBuckets = GnuHashTableSection<ELFT>::calcNBuckets(NumGnuHashed); |
| 890 | std::stable_sort(Symbols.begin(), Symbols.end(), |
| 891 | [NBuckets](const SymbolData &L, const SymbolData &R) { |
| 892 | if (!L.HasGnuHash || !R.HasGnuHash) |
| 893 | return R.HasGnuHash; |
| 894 | return L.GnuHash % NBuckets < R.GnuHash % NBuckets; |
| 895 | }); |
| 896 | } |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 897 | size_t I = 0; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 898 | for (const SymbolData &Item : Symbols) |
| 899 | Item.Body->setDynamicSymbolTableIndex(++I); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | template <class ELFT> |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 903 | void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) { |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 904 | StrTabSec.add(Name); |
| 905 | ++NumVisible; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 906 | ++NumLocals; |
| 907 | } |
| 908 | |
| 909 | template <class ELFT> |
| 910 | void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) { |
| 911 | StrTabSec.add(Body->getName()); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 912 | const bool HasGnuHash = StrTabSec.isDynamic() && Out<ELFT>::GnuHashTab && |
| 913 | includeInGnuHashTable(*Body); |
| 914 | Symbols.emplace_back(Body, HasGnuHash); |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 915 | ++NumVisible; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 916 | if (HasGnuHash) |
| 917 | ++NumGnuHashed; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 920 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 921 | Buf += sizeof(Elf_Sym); |
| 922 | |
| 923 | // All symbols with STB_LOCAL binding precede the weak and global symbols. |
| 924 | // .dynsym only contains global symbols. |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 925 | if (!Config->DiscardAll && !StrTabSec.isDynamic()) |
| 926 | writeLocalSymbols(Buf); |
| 927 | |
| 928 | writeGlobalSymbols(Buf); |
| 929 | } |
| 930 | |
| 931 | template <class ELFT> |
| 932 | void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { |
| 933 | // Iterate over all input object files to copy their local symbols |
| 934 | // to the output symbol table pointed by Buf. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 935 | for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) { |
| 936 | Elf_Sym_Range Syms = File->getLocalSymbols(); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 937 | for (const Elf_Sym &Sym : Syms) { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 938 | ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable()); |
Rafael Espindola | 26fd69d | 2015-10-09 16:15:57 +0000 | [diff] [blame] | 939 | error(SymNameOrErr); |
| 940 | StringRef SymName = *SymNameOrErr; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 941 | if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym)) |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 942 | continue; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 943 | |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 944 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 945 | uintX_t VA = 0; |
Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 946 | if (Sym.st_shndx == SHN_ABS) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 947 | ESym->st_shndx = SHN_ABS; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 948 | VA = Sym.st_value; |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 949 | } else { |
Rafael Espindola | 48225b4 | 2015-10-23 19:55:11 +0000 | [diff] [blame] | 950 | InputSectionBase<ELFT> *Section = File->getSection(Sym); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 951 | if (!Section->isLive()) |
| 952 | continue; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 953 | const OutputSectionBase<ELFT> *OutSec = Section->OutSec; |
| 954 | ESym->st_shndx = OutSec->SectionIndex; |
| 955 | VA += OutSec->getVA() + Section->getOffset(Sym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 956 | } |
Rui Ueyama | 9fbb3d8 | 2015-10-24 17:44:52 +0000 | [diff] [blame] | 957 | ESym->st_name = StrTabSec.getOffset(SymName); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 958 | ESym->st_size = Sym.st_size; |
| 959 | ESym->setBindingAndType(Sym.getBinding(), Sym.getType()); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 960 | ESym->st_value = VA; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 961 | Buf += sizeof(*ESym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 964 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 965 | |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 966 | template <class ELFT> |
Igor Kudrin | ea6a835 | 2015-10-19 08:01:51 +0000 | [diff] [blame] | 967 | void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 968 | // Write the internal symbol table contents to the output symbol table |
| 969 | // pointed by Buf. |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 970 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 971 | for (const SymbolData &Item : Symbols) { |
| 972 | SymbolBody *Body = Item.Body; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 973 | const OutputSectionBase<ELFT> *OutSec = nullptr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 974 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 975 | switch (Body->kind()) { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 976 | case SymbolBody::DefinedSyntheticKind: |
Rui Ueyama | b490876 | 2015-10-07 17:04:18 +0000 | [diff] [blame] | 977 | OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 978 | break; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 979 | case SymbolBody::DefinedRegularKind: { |
| 980 | auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl()); |
| 981 | if (!Sym->Section.isLive()) |
| 982 | continue; |
| 983 | OutSec = Sym->Section.OutSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 984 | break; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 985 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 986 | case SymbolBody::DefinedCommonKind: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 987 | OutSec = Out<ELFT>::Bss; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 988 | break; |
| 989 | case SymbolBody::UndefinedKind: |
| 990 | case SymbolBody::DefinedAbsoluteKind: |
| 991 | case SymbolBody::SharedKind: |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 992 | case SymbolBody::LazyKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 993 | break; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 996 | StringRef Name = Body->getName(); |
Rui Ueyama | 9fbb3d8 | 2015-10-24 17:44:52 +0000 | [diff] [blame] | 997 | ESym->st_name = StrTabSec.getOffset(Name); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 998 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 999 | unsigned char Type = STT_NOTYPE; |
| 1000 | uintX_t Size = 0; |
| 1001 | if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) { |
| 1002 | const Elf_Sym &InputSym = EBody->Sym; |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1003 | Type = InputSym.getType(); |
| 1004 | Size = InputSym.st_size; |
| 1005 | } |
| 1006 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1007 | ESym->setBindingAndType(getSymbolBinding(Body), Type); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1008 | ESym->st_size = Size; |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 1009 | ESym->setVisibility(Body->getVisibility()); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1010 | ESym->st_value = getSymVA<ELFT>(*Body); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1011 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1012 | if (isa<DefinedAbsolute<ELFT>>(Body)) |
Rafael Espindola | 6f4bd53 | 2015-10-06 14:17:53 +0000 | [diff] [blame] | 1013 | ESym->st_shndx = SHN_ABS; |
Rui Ueyama | b490876 | 2015-10-07 17:04:18 +0000 | [diff] [blame] | 1014 | else if (OutSec) |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 1015 | ESym->st_shndx = OutSec->SectionIndex; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1016 | |
| 1017 | ++ESym; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1021 | template <class ELFT> |
| 1022 | uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) { |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 1023 | uint8_t Visibility = Body->getVisibility(); |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1024 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
| 1025 | return STB_LOCAL; |
| 1026 | if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) |
| 1027 | return EBody->Sym.getBinding(); |
| 1028 | return Body->isWeak() ? STB_WEAK : STB_GLOBAL; |
| 1029 | } |
| 1030 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 1031 | template <class ELFT> |
| 1032 | ArrayRef<typename SymbolTableSection<ELFT>::SymbolData> |
| 1033 | SymbolTableSection<ELFT>::getGnuHashSymbols() const { |
| 1034 | return getSymbols().slice(Symbols.size() - NumGnuHashed); |
| 1035 | } |
| 1036 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1037 | namespace lld { |
| 1038 | namespace elf2 { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1039 | template class OutputSectionBase<ELF32LE>; |
| 1040 | template class OutputSectionBase<ELF32BE>; |
| 1041 | template class OutputSectionBase<ELF64LE>; |
| 1042 | template class OutputSectionBase<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1043 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 1044 | template class GotPltSection<ELF32LE>; |
| 1045 | template class GotPltSection<ELF32BE>; |
| 1046 | template class GotPltSection<ELF64LE>; |
| 1047 | template class GotPltSection<ELF64BE>; |
| 1048 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1049 | template class GotSection<ELF32LE>; |
| 1050 | template class GotSection<ELF32BE>; |
| 1051 | template class GotSection<ELF64LE>; |
| 1052 | template class GotSection<ELF64BE>; |
| 1053 | |
| 1054 | template class PltSection<ELF32LE>; |
| 1055 | template class PltSection<ELF32BE>; |
| 1056 | template class PltSection<ELF64LE>; |
| 1057 | template class PltSection<ELF64BE>; |
| 1058 | |
| 1059 | template class RelocationSection<ELF32LE>; |
| 1060 | template class RelocationSection<ELF32BE>; |
| 1061 | template class RelocationSection<ELF64LE>; |
| 1062 | template class RelocationSection<ELF64BE>; |
| 1063 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1064 | template class InterpSection<ELF32LE>; |
| 1065 | template class InterpSection<ELF32BE>; |
| 1066 | template class InterpSection<ELF64LE>; |
| 1067 | template class InterpSection<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1068 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 1069 | template class GnuHashTableSection<ELF32LE>; |
| 1070 | template class GnuHashTableSection<ELF32BE>; |
| 1071 | template class GnuHashTableSection<ELF64LE>; |
| 1072 | template class GnuHashTableSection<ELF64BE>; |
| 1073 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1074 | template class HashTableSection<ELF32LE>; |
| 1075 | template class HashTableSection<ELF32BE>; |
| 1076 | template class HashTableSection<ELF64LE>; |
| 1077 | template class HashTableSection<ELF64BE>; |
| 1078 | |
| 1079 | template class DynamicSection<ELF32LE>; |
| 1080 | template class DynamicSection<ELF32BE>; |
| 1081 | template class DynamicSection<ELF64LE>; |
| 1082 | template class DynamicSection<ELF64BE>; |
| 1083 | |
| 1084 | template class OutputSection<ELF32LE>; |
| 1085 | template class OutputSection<ELF32BE>; |
| 1086 | template class OutputSection<ELF64LE>; |
| 1087 | template class OutputSection<ELF64BE>; |
| 1088 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1089 | template class MergeOutputSection<ELF32LE>; |
| 1090 | template class MergeOutputSection<ELF32BE>; |
| 1091 | template class MergeOutputSection<ELF64LE>; |
| 1092 | template class MergeOutputSection<ELF64BE>; |
| 1093 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1094 | template class StringTableSection<ELF32LE>; |
| 1095 | template class StringTableSection<ELF32BE>; |
| 1096 | template class StringTableSection<ELF64LE>; |
| 1097 | template class StringTableSection<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1098 | |
| 1099 | template class SymbolTableSection<ELF32LE>; |
| 1100 | template class SymbolTableSection<ELF32BE>; |
| 1101 | template class SymbolTableSection<ELF64LE>; |
| 1102 | template class SymbolTableSection<ELF64BE>; |
| 1103 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1104 | template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &); |
| 1105 | template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &); |
| 1106 | template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &); |
| 1107 | template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 1108 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1109 | template ELFFile<ELF32LE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1110 | getLocalRelTarget(const ObjectFile<ELF32LE> &, |
Hal Finkel | 230c5c5 | 2015-10-16 22:37:32 +0000 | [diff] [blame] | 1111 | const ELFFile<ELF32LE>::Elf_Rel &); |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1112 | template ELFFile<ELF32BE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1113 | getLocalRelTarget(const ObjectFile<ELF32BE> &, |
Hal Finkel | 230c5c5 | 2015-10-16 22:37:32 +0000 | [diff] [blame] | 1114 | const ELFFile<ELF32BE>::Elf_Rel &); |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1115 | template ELFFile<ELF64LE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1116 | getLocalRelTarget(const ObjectFile<ELF64LE> &, |
Hal Finkel | 230c5c5 | 2015-10-16 22:37:32 +0000 | [diff] [blame] | 1117 | const ELFFile<ELF64LE>::Elf_Rel &); |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1118 | template ELFFile<ELF64BE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1119 | getLocalRelTarget(const ObjectFile<ELF64BE> &, |
Hal Finkel | 230c5c5 | 2015-10-16 22:37:32 +0000 | [diff] [blame] | 1120 | const ELFFile<ELF64BE>::Elf_Rel &); |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 1121 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1122 | template ELFFile<ELF32LE>::uintX_t |
| 1123 | getLocalRelTarget(const ObjectFile<ELF32LE> &, |
| 1124 | const ELFFile<ELF32LE>::Elf_Rela &); |
| 1125 | template ELFFile<ELF32BE>::uintX_t |
| 1126 | getLocalRelTarget(const ObjectFile<ELF32BE> &, |
| 1127 | const ELFFile<ELF32BE>::Elf_Rela &); |
| 1128 | template ELFFile<ELF64LE>::uintX_t |
| 1129 | getLocalRelTarget(const ObjectFile<ELF64LE> &, |
| 1130 | const ELFFile<ELF64LE>::Elf_Rela &); |
| 1131 | template ELFFile<ELF64BE>::uintX_t |
| 1132 | getLocalRelTarget(const ObjectFile<ELF64BE> &, |
| 1133 | const ELFFile<ELF64BE>::Elf_Rela &); |
| 1134 | |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 1135 | template bool includeInSymtab<ELF32LE>(const SymbolBody &); |
| 1136 | template bool includeInSymtab<ELF32BE>(const SymbolBody &); |
| 1137 | template bool includeInSymtab<ELF64LE>(const SymbolBody &); |
| 1138 | template bool includeInSymtab<ELF64BE>(const SymbolBody &); |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1139 | |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1140 | template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &, |
| 1141 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1142 | const ELFFile<ELF32LE>::Elf_Sym &); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1143 | template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &, |
| 1144 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1145 | const ELFFile<ELF32BE>::Elf_Sym &); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1146 | template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &, |
| 1147 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1148 | const ELFFile<ELF64LE>::Elf_Sym &); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1149 | template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &, |
| 1150 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1151 | const ELFFile<ELF64BE>::Elf_Sym &); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |