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