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