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