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 | |
| 23 | template <bool Is64Bits> |
| 24 | OutputSectionBase<Is64Bits>::OutputSectionBase(StringRef Name, uint32_t sh_type, |
| 25 | uintX_t sh_flags) |
| 26 | : Name(Name) { |
| 27 | memset(&Header, 0, sizeof(HeaderT)); |
| 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> |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 33 | GotSection<ELFT>::GotSection(const OutputSection<ELFT> &BssSec) |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 34 | : OutputSectionBase<ELFT::Is64Bits>(".got", llvm::ELF::SHT_PROGBITS, |
| 35 | llvm::ELF::SHF_ALLOC | |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 36 | llvm::ELF::SHF_WRITE), |
| 37 | BssSec(BssSec) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 38 | this->Header.sh_addralign = this->getAddrSize(); |
| 39 | } |
| 40 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 41 | template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) { |
| 42 | Sym->setGotIndex(Entries.size()); |
| 43 | Entries.push_back(Sym); |
| 44 | } |
| 45 | |
| 46 | template <class ELFT> |
| 47 | typename GotSection<ELFT>::uintX_t |
| 48 | GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
| 49 | return this->getVA() + B.getGotIndex() * this->getAddrSize(); |
| 50 | } |
| 51 | |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 52 | template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { |
| 53 | for (const SymbolBody *B : Entries) { |
Rafael Espindola | e782f67 | 2015-10-07 03:56:05 +0000 | [diff] [blame] | 54 | uint8_t *Entry = Buf; |
| 55 | Buf += sizeof(uintX_t); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 56 | if (canBePreempted(B)) |
| 57 | continue; // The dynamic linker will take care of it. |
| 58 | uintX_t VA = getSymVA(*B, BssSec); |
Rafael Espindola | e782f67 | 2015-10-07 03:56:05 +0000 | [diff] [blame] | 59 | write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 63 | template <class ELFT> |
| 64 | PltSection<ELFT>::PltSection(const GotSection<ELFT> &GotSec) |
| 65 | : OutputSectionBase<ELFT::Is64Bits>(".plt", llvm::ELF::SHT_PROGBITS, |
| 66 | llvm::ELF::SHF_ALLOC | |
| 67 | llvm::ELF::SHF_EXECINSTR), |
| 68 | GotSec(GotSec) { |
| 69 | this->Header.sh_addralign = 16; |
| 70 | } |
| 71 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 72 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 73 | uintptr_t Start = reinterpret_cast<uintptr_t>(Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 74 | for (const SymbolBody *E : Entries) { |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 75 | uint64_t GotEntryAddr = GotSec.getEntryAddr(*E); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 76 | uintptr_t InstPos = reinterpret_cast<uintptr_t>(Buf); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 77 | uint64_t PltEntryAddr = (InstPos - Start) + this->getVA(); |
| 78 | Target->writePltEntry(Buf, GotEntryAddr, PltEntryAddr); |
| 79 | Buf += 8; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) { |
| 84 | Sym->setPltIndex(Entries.size()); |
| 85 | Entries.push_back(Sym); |
| 86 | } |
| 87 | |
| 88 | template <class ELFT> |
| 89 | typename PltSection<ELFT>::uintX_t |
| 90 | PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
| 91 | return this->getVA() + B.getPltIndex() * EntrySize; |
| 92 | } |
| 93 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 94 | template <class ELFT> |
| 95 | RelocationSection<ELFT>::RelocationSection(SymbolTableSection<ELFT> &DynSymSec, |
| 96 | const GotSection<ELFT> &GotSec, |
Rafael Espindola | 9c3e4d2 | 2015-10-05 21:23:08 +0000 | [diff] [blame] | 97 | const OutputSection<ELFT> &BssSec, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 98 | bool IsRela) |
| 99 | : OutputSectionBase<ELFT::Is64Bits>(IsRela ? ".rela.dyn" : ".rel.dyn", |
| 100 | IsRela ? llvm::ELF::SHT_RELA |
| 101 | : llvm::ELF::SHT_REL, |
| 102 | llvm::ELF::SHF_ALLOC), |
Rafael Espindola | 9c3e4d2 | 2015-10-05 21:23:08 +0000 | [diff] [blame] | 103 | DynSymSec(DynSymSec), GotSec(GotSec), BssSec(BssSec), IsRela(IsRela) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 104 | this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 105 | this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 106 | } |
| 107 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 108 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 109 | const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 110 | bool IsMips64EL = Relocs[0].C.getFile()->getObj().isMips64EL(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 111 | for (const DynamicReloc<ELFT> &Rel : Relocs) { |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 112 | auto *P = reinterpret_cast<Elf_Rel *>(Buf); |
| 113 | Buf += EntrySize; |
| 114 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 115 | const InputSection<ELFT> &C = Rel.C; |
| 116 | const Elf_Rel &RI = Rel.RI; |
| 117 | OutputSection<ELFT> *Out = C.getOutputSection(); |
| 118 | uint32_t SymIndex = RI.getSymbol(IsMips64EL); |
Rafael Espindola | 41127ad | 2015-10-05 22:49:16 +0000 | [diff] [blame] | 119 | const ObjectFile<ELFT> &File = *C.getFile(); |
| 120 | const SymbolBody *Body = File.getSymbolBody(SymIndex); |
| 121 | const ELFFile<ELFT> &Obj = File.getObj(); |
| 122 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 123 | uint32_t Type = RI.getType(IsMips64EL); |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 124 | |
| 125 | bool CanBePreempted = canBePreempted(Body); |
| 126 | uintX_t Addend = 0; |
| 127 | if (!CanBePreempted) { |
| 128 | if (IsRela) { |
| 129 | if (Body) |
| 130 | Addend += getSymVA(cast<ELFSymbolBody<ELFT>>(*Body), BssSec); |
| 131 | else |
| 132 | Addend += getLocalSymVA( |
| 133 | Obj.getRelocationSymbol(&RI, File.getSymbolTable()), File); |
| 134 | } |
| 135 | P->setSymbolAndType(0, Target->getRelativeReloc(), IsMips64EL); |
| 136 | } |
| 137 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 138 | if (Body && Target->relocNeedsGot(Type, *Body)) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 139 | P->r_offset = GotSec.getEntryAddr(*Body); |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 140 | if (CanBePreempted) |
| 141 | P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), |
| 142 | Target->getGotReloc(), IsMips64EL); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 143 | } else { |
Rafael Espindola | 3c83e2b | 2015-10-05 21:09:37 +0000 | [diff] [blame] | 144 | if (IsRela) |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 145 | Addend += static_cast<const Elf_Rela &>(RI).r_addend; |
| 146 | P->r_offset = RI.r_offset + C.getOutputSectionOff() + Out->getVA(); |
| 147 | if (CanBePreempted) |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 148 | P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type, |
| 149 | IsMips64EL); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 150 | } |
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() { |
| 158 | this->Header.sh_link = DynSymSec.getSectionIndex(); |
| 159 | this->Header.sh_size = Relocs.size() * this->Header.sh_entsize; |
| 160 | } |
| 161 | |
| 162 | template <bool Is64Bits> |
| 163 | InterpSection<Is64Bits>::InterpSection() |
| 164 | : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS, |
| 165 | llvm::ELF::SHF_ALLOC) { |
| 166 | this->Header.sh_size = Config->DynamicLinker.size() + 1; |
| 167 | this->Header.sh_addralign = 1; |
| 168 | } |
| 169 | |
| 170 | template <bool Is64Bits> |
| 171 | template <endianness E> |
| 172 | void OutputSectionBase<Is64Bits>::writeHeaderTo( |
| 173 | typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) { |
| 174 | SHdr->sh_name = Header.sh_name; |
| 175 | SHdr->sh_type = Header.sh_type; |
| 176 | SHdr->sh_flags = Header.sh_flags; |
| 177 | SHdr->sh_addr = Header.sh_addr; |
| 178 | SHdr->sh_offset = Header.sh_offset; |
| 179 | SHdr->sh_size = Header.sh_size; |
| 180 | SHdr->sh_link = Header.sh_link; |
| 181 | SHdr->sh_info = Header.sh_info; |
| 182 | SHdr->sh_addralign = Header.sh_addralign; |
| 183 | SHdr->sh_entsize = Header.sh_entsize; |
| 184 | } |
| 185 | |
| 186 | template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) { |
| 187 | memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size()); |
| 188 | } |
| 189 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 190 | template <class ELFT> |
| 191 | HashTableSection<ELFT>::HashTableSection(SymbolTableSection<ELFT> &DynSymSec) |
| 192 | : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH, |
| 193 | llvm::ELF::SHF_ALLOC), |
| 194 | DynSymSec(DynSymSec) { |
| 195 | this->Header.sh_entsize = sizeof(Elf_Word); |
| 196 | this->Header.sh_addralign = sizeof(Elf_Word); |
| 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(); |
| 201 | DynSymSec.addSymbol(Name); |
| 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() { |
| 207 | this->Header.sh_link = DynSymSec.getSectionIndex(); |
| 208 | |
| 209 | assert(DynSymSec.getNumSymbols() == Hashes.size() + 1); |
| 210 | unsigned NumEntries = 2; // nbucket and nchain. |
| 211 | NumEntries += DynSymSec.getNumSymbols(); // The chain entries. |
| 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. |
| 216 | NumEntries += DynSymSec.getNumSymbols(); |
| 217 | this->Header.sh_size = NumEntries * sizeof(Elf_Word); |
| 218 | } |
| 219 | |
| 220 | template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 221 | unsigned NumSymbols = DynSymSec.getNumSymbols(); |
| 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> |
| 237 | DynamicSection<ELFT>::DynamicSection(SymbolTable &SymTab, |
| 238 | HashTableSection<ELFT> &HashSec, |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 239 | RelocationSection<ELFT> &RelaDynSec, |
| 240 | const OutputSection<ELFT> &BssSec) |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 241 | : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC, |
| 242 | llvm::ELF::SHF_ALLOC | |
| 243 | llvm::ELF::SHF_WRITE), |
| 244 | HashSec(HashSec), DynSymSec(HashSec.getDynSymSec()), |
| 245 | DynStrSec(DynSymSec.getStrTabSec()), RelaDynSec(RelaDynSec), |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 246 | BssSec(BssSec), SymTab(SymTab) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 247 | typename Base::HeaderT &Header = this->Header; |
| 248 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 249 | Header.sh_entsize = ELFT::Is64Bits ? 16 : 8; |
| 250 | } |
| 251 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 252 | template <class ELFT> void DynamicSection<ELFT>::finalize() { |
Michael J. Spencer | 52bf0eb | 2015-10-01 21:15:02 +0000 | [diff] [blame] | 253 | if (this->Header.sh_size) |
| 254 | return; // Already finalized. |
| 255 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 256 | typename Base::HeaderT &Header = this->Header; |
| 257 | Header.sh_link = DynStrSec.getSectionIndex(); |
| 258 | |
| 259 | unsigned NumEntries = 0; |
| 260 | if (RelaDynSec.hasRelocs()) { |
| 261 | ++NumEntries; // DT_RELA / DT_REL |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 262 | ++NumEntries; // DT_RELASZ / DT_RELSZ |
| 263 | ++NumEntries; // DT_RELAENT / DT_RELENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 264 | } |
| 265 | ++NumEntries; // DT_SYMTAB |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 266 | ++NumEntries; // DT_SYMENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 267 | ++NumEntries; // DT_STRTAB |
| 268 | ++NumEntries; // DT_STRSZ |
| 269 | ++NumEntries; // DT_HASH |
| 270 | |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 271 | if (!Config->RPath.empty()) { |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 272 | ++NumEntries; // DT_RUNPATH / DT_RPATH |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 273 | DynStrSec.add(Config->RPath); |
| 274 | } |
| 275 | |
| 276 | if (!Config->SoName.empty()) { |
| 277 | ++NumEntries; // DT_SONAME |
| 278 | DynStrSec.add(Config->SoName); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 281 | if (PreInitArraySec) |
| 282 | NumEntries += 2; |
| 283 | if (InitArraySec) |
| 284 | NumEntries += 2; |
| 285 | if (FiniArraySec) |
| 286 | NumEntries += 2; |
| 287 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 288 | const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles = |
| 289 | SymTab.getSharedFiles(); |
| 290 | for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) |
Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 291 | DynStrSec.add(File->getSoName()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 292 | NumEntries += SharedFiles.size(); |
| 293 | |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 294 | if (Symbol *S = SymTab.getSymbols().lookup(Config->Init)) |
| 295 | InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body); |
| 296 | if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini)) |
| 297 | FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body); |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 298 | if (InitSym) |
| 299 | ++NumEntries; // DT_INIT |
| 300 | if (FiniSym) |
| 301 | ++NumEntries; // DT_FINI |
George Rimar | 97aad17 | 2015-10-07 15:00:21 +0000 | [diff] [blame] | 302 | if (Config->ZNow) |
| 303 | ++NumEntries; // DT_FLAGS_1 |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 304 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 305 | ++NumEntries; // DT_NULL |
| 306 | |
| 307 | Header.sh_size = NumEntries * Header.sh_entsize; |
| 308 | } |
| 309 | |
| 310 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 311 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 312 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 313 | auto WritePtr = [&](int32_t Tag, uint64_t Val) { |
| 314 | P->d_tag = Tag; |
| 315 | P->d_un.d_ptr = Val; |
| 316 | ++P; |
| 317 | }; |
| 318 | |
| 319 | auto WriteVal = [&](int32_t Tag, uint32_t Val) { |
| 320 | P->d_tag = Tag; |
| 321 | P->d_un.d_val = Val; |
| 322 | ++P; |
| 323 | }; |
| 324 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 325 | if (RelaDynSec.hasRelocs()) { |
| 326 | bool IsRela = RelaDynSec.isRela(); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 327 | WritePtr(IsRela ? DT_RELA : DT_REL, RelaDynSec.getVA()); |
| 328 | WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, RelaDynSec.getSize()); |
| 329 | WriteVal(IsRela ? DT_RELAENT : DT_RELENT, |
| 330 | IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 333 | WritePtr(DT_SYMTAB, DynSymSec.getVA()); |
| 334 | WritePtr(DT_SYMENT, sizeof(Elf_Sym)); |
| 335 | WritePtr(DT_STRTAB, DynStrSec.getVA()); |
| 336 | WriteVal(DT_STRSZ, DynStrSec.data().size()); |
| 337 | WritePtr(DT_HASH, HashSec.getVA()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 338 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 339 | if (!Config->RPath.empty()) |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 340 | |
| 341 | // If --enable-new-dtags is set lld emits DT_RUNPATH |
| 342 | // instead of DT_RPATH. The two tags are functionally |
| 343 | // equivalent except for the following: |
| 344 | // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while |
| 345 | // DT_RPATH is searched before. |
| 346 | // - DT_RUNPATH is used only to search for direct |
| 347 | // dependencies of the object it's contained in, while |
| 348 | // DT_RPATH is used for indirect dependencies as well. |
| 349 | WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, |
| 350 | DynStrSec.getFileOff(Config->RPath)); |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 351 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 352 | if (!Config->SoName.empty()) |
| 353 | WriteVal(DT_SONAME, DynStrSec.getFileOff(Config->SoName)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 354 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 355 | auto WriteArray = [&](int32_t T1, int32_t T2, |
| 356 | const OutputSection<ELFT> *Sec) { |
| 357 | if (!Sec) |
| 358 | return; |
| 359 | WritePtr(T1, Sec->getVA()); |
| 360 | WriteVal(T2, Sec->getSize()); |
| 361 | }; |
| 362 | WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec); |
| 363 | WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec); |
| 364 | WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec); |
| 365 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 366 | const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles = |
| 367 | SymTab.getSharedFiles(); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 368 | for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) |
| 369 | WriteVal(DT_NEEDED, DynStrSec.getFileOff(File->getSoName())); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 370 | |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 371 | if (InitSym) |
| 372 | WritePtr(DT_INIT, getSymVA(*InitSym, BssSec)); |
| 373 | if (FiniSym) |
| 374 | WritePtr(DT_FINI, getSymVA(*FiniSym, BssSec)); |
| 375 | |
George Rimar | 97aad17 | 2015-10-07 15:00:21 +0000 | [diff] [blame] | 376 | if (Config->ZNow) |
| 377 | WriteVal(DT_FLAGS_1, DF_1_NOW); |
| 378 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 379 | WriteVal(DT_NULL, 0); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | template <class ELFT> |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 383 | OutputSection<ELFT>::OutputSection(const PltSection<ELFT> &PltSec, |
| 384 | const GotSection<ELFT> &GotSec, |
| 385 | const OutputSection<ELFT> &BssSec, |
| 386 | StringRef Name, uint32_t sh_type, |
| 387 | uintX_t sh_flags) |
| 388 | : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags), |
| 389 | PltSec(PltSec), GotSec(GotSec), BssSec(BssSec) {} |
| 390 | |
| 391 | template <class ELFT> |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 392 | void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) { |
| 393 | Sections.push_back(C); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 394 | C->setOutputSection(this); |
| 395 | uint32_t Align = C->getAlign(); |
| 396 | if (Align > this->Header.sh_addralign) |
| 397 | this->Header.sh_addralign = Align; |
| 398 | |
| 399 | uintX_t Off = this->Header.sh_size; |
| 400 | Off = RoundUpToAlignment(Off, Align); |
| 401 | C->setOutputSectionOff(Off); |
| 402 | Off += C->getSize(); |
| 403 | this->Header.sh_size = Off; |
| 404 | } |
| 405 | |
| 406 | template <class ELFT> |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 407 | typename ELFFile<ELFT>::uintX_t |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 408 | lld::elf2::getSymVA(const SymbolBody &S, const OutputSection<ELFT> &BssSec) { |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 409 | switch (S.kind()) { |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 410 | case SymbolBody::DefinedSyntheticKind: { |
| 411 | auto &D = cast<DefinedSynthetic<ELFT>>(S); |
| 412 | return D.Section.getVA() + D.Sym.st_value; |
| 413 | } |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 414 | case SymbolBody::DefinedAbsoluteKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 415 | return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 416 | case SymbolBody::DefinedRegularKind: { |
| 417 | const auto &DR = cast<DefinedRegular<ELFT>>(S); |
| 418 | const InputSection<ELFT> *SC = &DR.Section; |
| 419 | OutputSection<ELFT> *OS = SC->getOutputSection(); |
| 420 | return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value; |
| 421 | } |
| 422 | case SymbolBody::DefinedCommonKind: |
| 423 | return BssSec.getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS; |
| 424 | case SymbolBody::SharedKind: |
| 425 | case SymbolBody::UndefinedKind: |
| 426 | return 0; |
| 427 | case SymbolBody::LazyKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 428 | assert(S.isUsedInRegularObj() && "Lazy symbol reached writer"); |
| 429 | return 0; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 430 | } |
Denis Protivensky | 92aa1c0 | 2015-10-07 08:21:34 +0000 | [diff] [blame] | 431 | llvm_unreachable("Invalid symbol kind"); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | template <class ELFT> |
| 435 | typename ELFFile<ELFT>::uintX_t |
| 436 | lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym, |
| 437 | const ObjectFile<ELFT> &File) { |
| 438 | uint32_t SecIndex = Sym->st_shndx; |
| 439 | |
| 440 | if (SecIndex == SHN_XINDEX) |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 441 | SecIndex = File.getObj().getExtendedSymbolTableIndex( |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 442 | Sym, File.getSymbolTable(), File.getSymbolTableShndx()); |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 443 | ArrayRef<InputSection<ELFT> *> Sections = File.getSections(); |
| 444 | InputSection<ELFT> *Section = Sections[SecIndex]; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 445 | OutputSection<ELFT> *Out = Section->getOutputSection(); |
| 446 | return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value; |
| 447 | } |
| 448 | |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 449 | bool lld::elf2::canBePreempted(const SymbolBody *Body) { |
| 450 | if (!Body) |
| 451 | return false; |
Rafael Espindola | cea0b3b | 2015-10-07 04:22:55 +0000 | [diff] [blame] | 452 | if (Body->isShared()) |
| 453 | return true; |
| 454 | if (Body->isUndefined() && !Body->isWeak()) |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 455 | return true; |
| 456 | if (!Config->Shared) |
| 457 | return false; |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 458 | return Body->getMostConstrainingVisibility() == STV_DEFAULT; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 461 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 462 | for (InputSection<ELFT> *C : Sections) |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 463 | C->writeTo(Buf, BssSec, PltSec, GotSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | template <bool Is64Bits> |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 467 | StringTableSection<Is64Bits>::StringTableSection(bool Dynamic) |
| 468 | : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab", |
| 469 | llvm::ELF::SHT_STRTAB, |
| 470 | Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0), |
| 471 | Dynamic(Dynamic) { |
| 472 | this->Header.sh_addralign = 1; |
| 473 | } |
| 474 | |
| 475 | template <bool Is64Bits> |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 476 | void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) { |
| 477 | StringRef Data = StrTabBuilder.data(); |
| 478 | memcpy(Buf, Data.data(), Data.size()); |
| 479 | } |
| 480 | |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 481 | template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 482 | if (!B.isUsedInRegularObj()) |
| 483 | return false; |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 484 | |
| 485 | // Don't include synthetic symbols like __init_array_start in every output. |
| 486 | if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B)) |
| 487 | if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef) |
| 488 | return false; |
| 489 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 490 | return true; |
| 491 | } |
| 492 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 493 | bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) { |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 494 | uint8_t V = B.getMostConstrainingVisibility(); |
| 495 | if (V != STV_DEFAULT && V != STV_PROTECTED) |
| 496 | return false; |
| 497 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 498 | if (Config->ExportDynamic || Config->Shared) |
| 499 | return true; |
| 500 | return B.isUsedInDynamicReloc(); |
| 501 | } |
| 502 | |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 503 | template <class ELFT> |
| 504 | bool lld::elf2::shouldKeepInSymtab(StringRef SymName, |
| 505 | const typename ELFFile<ELFT>::Elf_Sym &Sym) { |
| 506 | if (Sym.getType() == STT_SECTION) |
| 507 | return false; |
| 508 | |
Davide Italiano | 6993ba4 | 2015-09-26 00:47:56 +0000 | [diff] [blame] | 509 | if (Config->DiscardNone) |
| 510 | return true; |
| 511 | |
| 512 | // ELF defines dynamic locals as symbols which name starts with ".L". |
| 513 | return !(Config->DiscardLocals && SymName.startswith(".L")); |
| 514 | } |
| 515 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 516 | template <class ELFT> |
| 517 | SymbolTableSection<ELFT>::SymbolTableSection( |
| 518 | SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec, |
| 519 | const OutputSection<ELFT> &BssSec) |
| 520 | : OutputSectionBase<ELFT::Is64Bits>( |
| 521 | StrTabSec.isDynamic() ? ".dynsym" : ".symtab", |
| 522 | StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB, |
| 523 | StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0), |
| 524 | Table(Table), StrTabSec(StrTabSec), BssSec(BssSec) { |
| 525 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 526 | typename Base::HeaderT &Header = this->Header; |
| 527 | |
| 528 | Header.sh_entsize = sizeof(Elf_Sym); |
| 529 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 530 | } |
| 531 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 532 | template <class ELFT> void SymbolTableSection<ELFT>::finalize() { |
| 533 | this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym); |
| 534 | this->Header.sh_link = StrTabSec.getSectionIndex(); |
| 535 | this->Header.sh_info = NumLocals + 1; |
| 536 | } |
| 537 | |
| 538 | template <class ELFT> |
| 539 | void SymbolTableSection<ELFT>::addSymbol(StringRef Name, bool isLocal) { |
| 540 | StrTabSec.add(Name); |
| 541 | ++NumVisible; |
| 542 | if (isLocal) |
| 543 | ++NumLocals; |
| 544 | } |
| 545 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 546 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 547 | Buf += sizeof(Elf_Sym); |
| 548 | |
| 549 | // All symbols with STB_LOCAL binding precede the weak and global symbols. |
| 550 | // .dynsym only contains global symbols. |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 551 | if (!Config->DiscardAll && !StrTabSec.isDynamic()) |
| 552 | writeLocalSymbols(Buf); |
| 553 | |
| 554 | writeGlobalSymbols(Buf); |
| 555 | } |
| 556 | |
| 557 | template <class ELFT> |
| 558 | void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { |
| 559 | // Iterate over all input object files to copy their local symbols |
| 560 | // to the output symbol table pointed by Buf. |
| 561 | for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) { |
| 562 | auto &File = cast<ObjectFile<ELFT>>(*FileB); |
| 563 | Elf_Sym_Range Syms = File.getLocalSymbols(); |
| 564 | for (const Elf_Sym &Sym : Syms) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 565 | ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable()); |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 566 | if (SymName && !shouldKeepInSymtab<ELFT>(*SymName, Sym)) |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 567 | continue; |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 568 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
| 569 | Buf += sizeof(*ESym); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 570 | ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0; |
| 571 | ESym->st_size = Sym.st_size; |
| 572 | ESym->setBindingAndType(Sym.getBinding(), Sym.getType()); |
| 573 | uint32_t SecIndex = Sym.st_shndx; |
| 574 | uintX_t VA = Sym.st_value; |
| 575 | if (SecIndex == SHN_ABS) { |
| 576 | ESym->st_shndx = SHN_ABS; |
| 577 | } else { |
| 578 | if (SecIndex == SHN_XINDEX) |
| 579 | SecIndex = File.getObj().getExtendedSymbolTableIndex( |
| 580 | &Sym, File.getSymbolTable(), File.getSymbolTableShndx()); |
| 581 | ArrayRef<InputSection<ELFT> *> Sections = File.getSections(); |
| 582 | const InputSection<ELFT> *Section = Sections[SecIndex]; |
| 583 | const OutputSection<ELFT> *Out = Section->getOutputSection(); |
| 584 | ESym->st_shndx = Out->getSectionIndex(); |
| 585 | VA += Out->getVA() + Section->getOutputSectionOff(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 586 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 587 | ESym->st_value = VA; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 590 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 591 | |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 592 | template <class ELFT> |
| 593 | void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) { |
| 594 | // Write the internal symbol table contents to the output symbol table |
| 595 | // pointed by Buf. |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 596 | uint8_t *Start = Buf; |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 597 | for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 598 | StringRef Name = P.first; |
| 599 | Symbol *Sym = P.second; |
| 600 | SymbolBody *Body = Sym->Body; |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 601 | if (!includeInSymtab<ELFT>(*Body)) |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 602 | continue; |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 603 | if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body)) |
| 604 | continue; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 605 | |
| 606 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 607 | Buf += sizeof(*ESym); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 608 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 609 | ESym->st_name = StrTabSec.getFileOff(Name); |
| 610 | |
Rafael Espindola | 25b0acb | 2015-09-25 17:32:37 +0000 | [diff] [blame] | 611 | const OutputSection<ELFT> *Out = nullptr; |
| 612 | const InputSection<ELFT> *Section = nullptr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 613 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 614 | switch (Body->kind()) { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 615 | case SymbolBody::DefinedSyntheticKind: |
| 616 | Out = &cast<DefinedSynthetic<ELFT>>(Body)->Section; |
| 617 | break; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 618 | case SymbolBody::DefinedRegularKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 619 | Section = &cast<DefinedRegular<ELFT>>(Body)->Section; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 620 | break; |
| 621 | case SymbolBody::DefinedCommonKind: |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 622 | Out = &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 623 | break; |
| 624 | case SymbolBody::UndefinedKind: |
| 625 | case SymbolBody::DefinedAbsoluteKind: |
| 626 | case SymbolBody::SharedKind: |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 627 | case SymbolBody::LazyKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 628 | break; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 631 | unsigned char Binding = Body->isWeak() ? STB_WEAK : STB_GLOBAL; |
| 632 | unsigned char Type = STT_NOTYPE; |
| 633 | uintX_t Size = 0; |
| 634 | if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) { |
| 635 | const Elf_Sym &InputSym = EBody->Sym; |
| 636 | Binding = InputSym.getBinding(); |
| 637 | Type = InputSym.getType(); |
| 638 | Size = InputSym.st_size; |
| 639 | } |
| 640 | |
| 641 | unsigned char Visibility = Body->getMostConstrainingVisibility(); |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 642 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
| 643 | Binding = STB_LOCAL; |
| 644 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 645 | ESym->setBindingAndType(Binding, Type); |
| 646 | ESym->st_size = Size; |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 647 | ESym->setVisibility(Visibility); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 648 | ESym->st_value = getSymVA(*Body, BssSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 649 | |
| 650 | if (Section) |
| 651 | Out = Section->getOutputSection(); |
| 652 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 653 | if (isa<DefinedAbsolute<ELFT>>(Body)) |
Rafael Espindola | 6f4bd53 | 2015-10-06 14:17:53 +0000 | [diff] [blame] | 654 | ESym->st_shndx = SHN_ABS; |
| 655 | else if (Out) |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 656 | ESym->st_shndx = Out->getSectionIndex(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 657 | } |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 658 | if (!StrTabSec.isDynamic()) |
| 659 | std::stable_sort( |
| 660 | reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf), |
| 661 | [](const Elf_Sym &A, const Elf_Sym &B) -> bool { |
| 662 | return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL; |
| 663 | }); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | namespace lld { |
| 667 | namespace elf2 { |
| 668 | template class OutputSectionBase<false>; |
| 669 | template class OutputSectionBase<true>; |
| 670 | |
| 671 | template void OutputSectionBase<false>::writeHeaderTo<support::little>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 672 | ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 673 | template void OutputSectionBase<true>::writeHeaderTo<support::little>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 674 | ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 675 | template void OutputSectionBase<false>::writeHeaderTo<support::big>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 676 | ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 677 | template void OutputSectionBase<true>::writeHeaderTo<support::big>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 678 | ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 679 | |
| 680 | template class GotSection<ELF32LE>; |
| 681 | template class GotSection<ELF32BE>; |
| 682 | template class GotSection<ELF64LE>; |
| 683 | template class GotSection<ELF64BE>; |
| 684 | |
| 685 | template class PltSection<ELF32LE>; |
| 686 | template class PltSection<ELF32BE>; |
| 687 | template class PltSection<ELF64LE>; |
| 688 | template class PltSection<ELF64BE>; |
| 689 | |
| 690 | template class RelocationSection<ELF32LE>; |
| 691 | template class RelocationSection<ELF32BE>; |
| 692 | template class RelocationSection<ELF64LE>; |
| 693 | template class RelocationSection<ELF64BE>; |
| 694 | |
| 695 | template class InterpSection<false>; |
| 696 | template class InterpSection<true>; |
| 697 | |
| 698 | template class HashTableSection<ELF32LE>; |
| 699 | template class HashTableSection<ELF32BE>; |
| 700 | template class HashTableSection<ELF64LE>; |
| 701 | template class HashTableSection<ELF64BE>; |
| 702 | |
| 703 | template class DynamicSection<ELF32LE>; |
| 704 | template class DynamicSection<ELF32BE>; |
| 705 | template class DynamicSection<ELF64LE>; |
| 706 | template class DynamicSection<ELF64BE>; |
| 707 | |
| 708 | template class OutputSection<ELF32LE>; |
| 709 | template class OutputSection<ELF32BE>; |
| 710 | template class OutputSection<ELF64LE>; |
| 711 | template class OutputSection<ELF64BE>; |
| 712 | |
| 713 | template class StringTableSection<false>; |
| 714 | template class StringTableSection<true>; |
| 715 | |
| 716 | template class SymbolTableSection<ELF32LE>; |
| 717 | template class SymbolTableSection<ELF32BE>; |
| 718 | template class SymbolTableSection<ELF64LE>; |
| 719 | template class SymbolTableSection<ELF64BE>; |
| 720 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 721 | template ELFFile<ELF32LE>::uintX_t getSymVA(const SymbolBody &, |
| 722 | const OutputSection<ELF32LE> &); |
| 723 | template ELFFile<ELF32BE>::uintX_t getSymVA(const SymbolBody &, |
| 724 | const OutputSection<ELF32BE> &); |
| 725 | template ELFFile<ELF64LE>::uintX_t getSymVA(const SymbolBody &, |
| 726 | const OutputSection<ELF64LE> &); |
| 727 | template ELFFile<ELF64BE>::uintX_t getSymVA(const SymbolBody &, |
| 728 | const OutputSection<ELF64BE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 729 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 730 | template ELFFile<ELF32LE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 731 | getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 732 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 733 | template ELFFile<ELF32BE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 734 | getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 735 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 736 | template ELFFile<ELF64LE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 737 | getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 738 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 739 | template ELFFile<ELF64BE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 740 | getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &); |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 741 | |
| 742 | template bool includeInSymtab<ELF32LE>(const SymbolBody &); |
| 743 | template bool includeInSymtab<ELF32BE>(const SymbolBody &); |
| 744 | template bool includeInSymtab<ELF64LE>(const SymbolBody &); |
| 745 | template bool includeInSymtab<ELF64BE>(const SymbolBody &); |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 746 | |
| 747 | template bool shouldKeepInSymtab<ELF32LE>(StringRef, |
| 748 | const ELFFile<ELF32LE>::Elf_Sym &); |
| 749 | template bool shouldKeepInSymtab<ELF32BE>(StringRef, |
| 750 | const ELFFile<ELF32BE>::Elf_Sym &); |
| 751 | template bool shouldKeepInSymtab<ELF64LE>(StringRef, |
| 752 | const ELFFile<ELF64LE>::Elf_Sym &); |
| 753 | template bool shouldKeepInSymtab<ELF64BE>(StringRef, |
| 754 | const ELFFile<ELF64BE>::Elf_Sym &); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 755 | } |
| 756 | } |