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