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