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; |
| 17 | using namespace llvm::ELF; |
| 18 | |
| 19 | using namespace lld; |
| 20 | using namespace lld::elf2; |
| 21 | |
| 22 | template <bool Is64Bits> |
| 23 | OutputSectionBase<Is64Bits>::OutputSectionBase(StringRef Name, uint32_t sh_type, |
| 24 | uintX_t sh_flags) |
| 25 | : Name(Name) { |
| 26 | memset(&Header, 0, sizeof(HeaderT)); |
| 27 | Header.sh_type = sh_type; |
| 28 | Header.sh_flags = sh_flags; |
| 29 | } |
| 30 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 31 | template <class ELFT> |
| 32 | GotSection<ELFT>::GotSection() |
| 33 | : OutputSectionBase<ELFT::Is64Bits>(".got", llvm::ELF::SHT_PROGBITS, |
| 34 | llvm::ELF::SHF_ALLOC | |
| 35 | llvm::ELF::SHF_WRITE) { |
| 36 | this->Header.sh_addralign = this->getAddrSize(); |
| 37 | } |
| 38 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 39 | template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) { |
| 40 | Sym->setGotIndex(Entries.size()); |
| 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 { |
| 47 | return this->getVA() + B.getGotIndex() * this->getAddrSize(); |
| 48 | } |
| 49 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 50 | template <class ELFT> |
| 51 | PltSection<ELFT>::PltSection(const GotSection<ELFT> &GotSec) |
| 52 | : OutputSectionBase<ELFT::Is64Bits>(".plt", llvm::ELF::SHT_PROGBITS, |
| 53 | llvm::ELF::SHF_ALLOC | |
| 54 | llvm::ELF::SHF_EXECINSTR), |
| 55 | GotSec(GotSec) { |
| 56 | this->Header.sh_addralign = 16; |
| 57 | } |
| 58 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 59 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 60 | uintptr_t Start = reinterpret_cast<uintptr_t>(Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 61 | for (const SymbolBody *E : Entries) { |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 62 | uint64_t GotEntryAddr = GotSec.getEntryAddr(*E); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 63 | uintptr_t InstPos = reinterpret_cast<uintptr_t>(Buf); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 64 | uint64_t PltEntryAddr = (InstPos - Start) + this->getVA(); |
| 65 | Target->writePltEntry(Buf, GotEntryAddr, PltEntryAddr); |
| 66 | Buf += 8; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) { |
| 71 | Sym->setPltIndex(Entries.size()); |
| 72 | Entries.push_back(Sym); |
| 73 | } |
| 74 | |
| 75 | template <class ELFT> |
| 76 | typename PltSection<ELFT>::uintX_t |
| 77 | PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
| 78 | return this->getVA() + B.getPltIndex() * EntrySize; |
| 79 | } |
| 80 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 81 | template <class ELFT> |
| 82 | RelocationSection<ELFT>::RelocationSection(SymbolTableSection<ELFT> &DynSymSec, |
| 83 | const GotSection<ELFT> &GotSec, |
| 84 | bool IsRela) |
| 85 | : OutputSectionBase<ELFT::Is64Bits>(IsRela ? ".rela.dyn" : ".rel.dyn", |
| 86 | IsRela ? llvm::ELF::SHT_RELA |
| 87 | : llvm::ELF::SHT_REL, |
| 88 | llvm::ELF::SHF_ALLOC), |
| 89 | DynSymSec(DynSymSec), GotSec(GotSec), IsRela(IsRela) { |
| 90 | this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 91 | this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 92 | } |
| 93 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 94 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 95 | const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 96 | bool IsMips64EL = Relocs[0].C.getFile()->getObj().isMips64EL(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 97 | for (const DynamicReloc<ELFT> &Rel : Relocs) { |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 98 | auto *P = reinterpret_cast<Elf_Rel *>(Buf); |
| 99 | Buf += EntrySize; |
| 100 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 101 | const InputSection<ELFT> &C = Rel.C; |
| 102 | const Elf_Rel &RI = Rel.RI; |
| 103 | OutputSection<ELFT> *Out = C.getOutputSection(); |
| 104 | uint32_t SymIndex = RI.getSymbol(IsMips64EL); |
| 105 | const SymbolBody *Body = C.getFile()->getSymbolBody(SymIndex); |
| 106 | uint32_t Type = RI.getType(IsMips64EL); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 107 | if (Target->relocNeedsGot(Type, *Body)) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 108 | P->r_offset = GotSec.getEntryAddr(*Body); |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 109 | P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), |
| 110 | Target->getGotReloc(), IsMips64EL); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 111 | } else { |
| 112 | P->r_offset = RI.r_offset + C.getOutputSectionOff() + Out->getVA(); |
| 113 | P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type, IsMips64EL); |
| 114 | if (IsRela) |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 115 | static_cast<Elf_Rela *>(P)->r_addend = |
| 116 | static_cast<const Elf_Rela &>(RI).r_addend; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 117 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | template <class ELFT> void RelocationSection<ELFT>::finalize() { |
| 122 | this->Header.sh_link = DynSymSec.getSectionIndex(); |
| 123 | this->Header.sh_size = Relocs.size() * this->Header.sh_entsize; |
| 124 | } |
| 125 | |
| 126 | template <bool Is64Bits> |
| 127 | InterpSection<Is64Bits>::InterpSection() |
| 128 | : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS, |
| 129 | llvm::ELF::SHF_ALLOC) { |
| 130 | this->Header.sh_size = Config->DynamicLinker.size() + 1; |
| 131 | this->Header.sh_addralign = 1; |
| 132 | } |
| 133 | |
| 134 | template <bool Is64Bits> |
| 135 | template <endianness E> |
| 136 | void OutputSectionBase<Is64Bits>::writeHeaderTo( |
| 137 | typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) { |
| 138 | SHdr->sh_name = Header.sh_name; |
| 139 | SHdr->sh_type = Header.sh_type; |
| 140 | SHdr->sh_flags = Header.sh_flags; |
| 141 | SHdr->sh_addr = Header.sh_addr; |
| 142 | SHdr->sh_offset = Header.sh_offset; |
| 143 | SHdr->sh_size = Header.sh_size; |
| 144 | SHdr->sh_link = Header.sh_link; |
| 145 | SHdr->sh_info = Header.sh_info; |
| 146 | SHdr->sh_addralign = Header.sh_addralign; |
| 147 | SHdr->sh_entsize = Header.sh_entsize; |
| 148 | } |
| 149 | |
| 150 | template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) { |
| 151 | memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size()); |
| 152 | } |
| 153 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 154 | template <class ELFT> |
| 155 | HashTableSection<ELFT>::HashTableSection(SymbolTableSection<ELFT> &DynSymSec) |
| 156 | : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH, |
| 157 | llvm::ELF::SHF_ALLOC), |
| 158 | DynSymSec(DynSymSec) { |
| 159 | this->Header.sh_entsize = sizeof(Elf_Word); |
| 160 | this->Header.sh_addralign = sizeof(Elf_Word); |
| 161 | } |
| 162 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 163 | template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) { |
| 164 | StringRef Name = S->getName(); |
| 165 | DynSymSec.addSymbol(Name); |
| 166 | Hashes.push_back(hash(Name)); |
| 167 | S->setDynamicSymbolTableIndex(Hashes.size()); |
| 168 | } |
| 169 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 170 | template <class ELFT> |
| 171 | DynamicSection<ELFT>::DynamicSection(SymbolTable &SymTab, |
| 172 | HashTableSection<ELFT> &HashSec, |
| 173 | RelocationSection<ELFT> &RelaDynSec) |
| 174 | : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC, |
| 175 | llvm::ELF::SHF_ALLOC | |
| 176 | llvm::ELF::SHF_WRITE), |
| 177 | HashSec(HashSec), DynSymSec(HashSec.getDynSymSec()), |
| 178 | DynStrSec(DynSymSec.getStrTabSec()), RelaDynSec(RelaDynSec), |
| 179 | SymTab(SymTab) { |
| 180 | typename Base::HeaderT &Header = this->Header; |
| 181 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 182 | Header.sh_entsize = ELFT::Is64Bits ? 16 : 8; |
| 183 | } |
| 184 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 185 | template <class ELFT> void DynamicSection<ELFT>::finalize() { |
Michael J. Spencer | 52bf0eb | 2015-10-01 21:15:02 +0000 | [diff] [blame] | 186 | if (this->Header.sh_size) |
| 187 | return; // Already finalized. |
| 188 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 189 | typename Base::HeaderT &Header = this->Header; |
| 190 | Header.sh_link = DynStrSec.getSectionIndex(); |
| 191 | |
| 192 | unsigned NumEntries = 0; |
| 193 | if (RelaDynSec.hasRelocs()) { |
| 194 | ++NumEntries; // DT_RELA / DT_REL |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 195 | ++NumEntries; // DT_RELASZ / DT_RELSZ |
| 196 | ++NumEntries; // DT_RELAENT / DT_RELENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 197 | } |
| 198 | ++NumEntries; // DT_SYMTAB |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 199 | ++NumEntries; // DT_SYMENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 200 | ++NumEntries; // DT_STRTAB |
| 201 | ++NumEntries; // DT_STRSZ |
| 202 | ++NumEntries; // DT_HASH |
| 203 | |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 204 | if (!Config->RPath.empty()) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 205 | ++NumEntries; // DT_RUNPATH |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 206 | DynStrSec.add(Config->RPath); |
| 207 | } |
| 208 | |
| 209 | if (!Config->SoName.empty()) { |
| 210 | ++NumEntries; // DT_SONAME |
| 211 | DynStrSec.add(Config->SoName); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame^] | 214 | if (PreInitArraySec) |
| 215 | NumEntries += 2; |
| 216 | if (InitArraySec) |
| 217 | NumEntries += 2; |
| 218 | if (FiniArraySec) |
| 219 | NumEntries += 2; |
| 220 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 221 | const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles = |
| 222 | SymTab.getSharedFiles(); |
| 223 | for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) |
Rafael Espindola | c8b1581 | 2015-10-01 15:47:50 +0000 | [diff] [blame] | 224 | DynStrSec.add(File->getSoName()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 225 | NumEntries += SharedFiles.size(); |
| 226 | |
| 227 | ++NumEntries; // DT_NULL |
| 228 | |
| 229 | Header.sh_size = NumEntries * Header.sh_entsize; |
| 230 | } |
| 231 | |
| 232 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 233 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 234 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 235 | auto WritePtr = [&](int32_t Tag, uint64_t Val) { |
| 236 | P->d_tag = Tag; |
| 237 | P->d_un.d_ptr = Val; |
| 238 | ++P; |
| 239 | }; |
| 240 | |
| 241 | auto WriteVal = [&](int32_t Tag, uint32_t Val) { |
| 242 | P->d_tag = Tag; |
| 243 | P->d_un.d_val = Val; |
| 244 | ++P; |
| 245 | }; |
| 246 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 247 | if (RelaDynSec.hasRelocs()) { |
| 248 | bool IsRela = RelaDynSec.isRela(); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 249 | WritePtr(IsRela ? DT_RELA : DT_REL, RelaDynSec.getVA()); |
| 250 | WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, RelaDynSec.getSize()); |
| 251 | WriteVal(IsRela ? DT_RELAENT : DT_RELENT, |
| 252 | IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 255 | WritePtr(DT_SYMTAB, DynSymSec.getVA()); |
| 256 | WritePtr(DT_SYMENT, sizeof(Elf_Sym)); |
| 257 | WritePtr(DT_STRTAB, DynStrSec.getVA()); |
| 258 | WriteVal(DT_STRSZ, DynStrSec.data().size()); |
| 259 | WritePtr(DT_HASH, HashSec.getVA()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 260 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 261 | if (!Config->RPath.empty()) |
| 262 | WriteVal(DT_RUNPATH, DynStrSec.getFileOff(Config->RPath)); |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 263 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 264 | if (!Config->SoName.empty()) |
| 265 | WriteVal(DT_SONAME, DynStrSec.getFileOff(Config->SoName)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 266 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame^] | 267 | auto WriteArray = [&](int32_t T1, int32_t T2, |
| 268 | const OutputSection<ELFT> *Sec) { |
| 269 | if (!Sec) |
| 270 | return; |
| 271 | WritePtr(T1, Sec->getVA()); |
| 272 | WriteVal(T2, Sec->getSize()); |
| 273 | }; |
| 274 | WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec); |
| 275 | WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec); |
| 276 | WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec); |
| 277 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 278 | const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles = |
| 279 | SymTab.getSharedFiles(); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 280 | for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) |
| 281 | WriteVal(DT_NEEDED, DynStrSec.getFileOff(File->getSoName())); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 282 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 283 | WriteVal(DT_NULL, 0); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | template <class ELFT> |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 287 | OutputSection<ELFT>::OutputSection(const PltSection<ELFT> &PltSec, |
| 288 | const GotSection<ELFT> &GotSec, |
| 289 | const OutputSection<ELFT> &BssSec, |
| 290 | StringRef Name, uint32_t sh_type, |
| 291 | uintX_t sh_flags) |
| 292 | : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags), |
| 293 | PltSec(PltSec), GotSec(GotSec), BssSec(BssSec) {} |
| 294 | |
| 295 | template <class ELFT> |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 296 | void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) { |
| 297 | Sections.push_back(C); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 298 | C->setOutputSection(this); |
| 299 | uint32_t Align = C->getAlign(); |
| 300 | if (Align > this->Header.sh_addralign) |
| 301 | this->Header.sh_addralign = Align; |
| 302 | |
| 303 | uintX_t Off = this->Header.sh_size; |
| 304 | Off = RoundUpToAlignment(Off, Align); |
| 305 | C->setOutputSectionOff(Off); |
| 306 | Off += C->getSize(); |
| 307 | this->Header.sh_size = Off; |
| 308 | } |
| 309 | |
| 310 | template <class ELFT> |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 311 | typename ELFFile<ELFT>::uintX_t |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 312 | lld::elf2::getSymVA(const ELFSymbolBody<ELFT> &S, |
| 313 | const OutputSection<ELFT> &BssSec) { |
| 314 | switch (S.kind()) { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 315 | case SymbolBody::DefinedSyntheticKind: |
| 316 | return cast<DefinedSynthetic<ELFT>>(S).Section.getVA() + S.Sym.st_value; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 317 | case SymbolBody::DefinedAbsoluteKind: |
| 318 | return S.Sym.st_value; |
| 319 | case SymbolBody::DefinedRegularKind: { |
| 320 | const auto &DR = cast<DefinedRegular<ELFT>>(S); |
| 321 | const InputSection<ELFT> *SC = &DR.Section; |
| 322 | OutputSection<ELFT> *OS = SC->getOutputSection(); |
| 323 | return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value; |
| 324 | } |
| 325 | case SymbolBody::DefinedCommonKind: |
| 326 | return BssSec.getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS; |
| 327 | case SymbolBody::SharedKind: |
| 328 | case SymbolBody::UndefinedKind: |
| 329 | return 0; |
| 330 | case SymbolBody::LazyKind: |
Rafael Espindola | 2732235 | 2015-09-28 22:12:54 +0000 | [diff] [blame] | 331 | break; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 332 | } |
Rafael Espindola | 2732235 | 2015-09-28 22:12:54 +0000 | [diff] [blame] | 333 | llvm_unreachable("Lazy symbol reached writer"); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | template <class ELFT> |
| 337 | typename ELFFile<ELFT>::uintX_t |
| 338 | lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym, |
| 339 | const ObjectFile<ELFT> &File) { |
| 340 | uint32_t SecIndex = Sym->st_shndx; |
| 341 | |
| 342 | if (SecIndex == SHN_XINDEX) |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 343 | SecIndex = File.getObj().getExtendedSymbolTableIndex( |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 344 | Sym, File.getSymbolTable(), File.getSymbolTableShndx()); |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 345 | ArrayRef<InputSection<ELFT> *> Sections = File.getSections(); |
| 346 | InputSection<ELFT> *Section = Sections[SecIndex]; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 347 | OutputSection<ELFT> *Out = Section->getOutputSection(); |
| 348 | return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value; |
| 349 | } |
| 350 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 351 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 352 | for (InputSection<ELFT> *C : Sections) |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 353 | C->writeTo(Buf, BssSec, PltSec, GotSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | template <bool Is64Bits> |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 357 | StringTableSection<Is64Bits>::StringTableSection(bool Dynamic) |
| 358 | : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab", |
| 359 | llvm::ELF::SHT_STRTAB, |
| 360 | Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0), |
| 361 | Dynamic(Dynamic) { |
| 362 | this->Header.sh_addralign = 1; |
| 363 | } |
| 364 | |
| 365 | template <bool Is64Bits> |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 366 | void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) { |
| 367 | StringRef Data = StrTabBuilder.data(); |
| 368 | memcpy(Buf, Data.data(), Data.size()); |
| 369 | } |
| 370 | |
| 371 | bool lld::elf2::includeInSymtab(const SymbolBody &B) { |
| 372 | if (B.isLazy()) |
| 373 | return false; |
| 374 | if (!B.isUsedInRegularObj()) |
| 375 | return false; |
| 376 | uint8_t V = B.getMostConstrainingVisibility(); |
| 377 | if (V != STV_DEFAULT && V != STV_PROTECTED) |
| 378 | return false; |
| 379 | return true; |
| 380 | } |
| 381 | |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 382 | bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) { |
| 383 | if (Config->ExportDynamic || Config->Shared) |
| 384 | return true; |
| 385 | return B.isUsedInDynamicReloc(); |
| 386 | } |
| 387 | |
Davide Italiano | 6993ba4 | 2015-09-26 00:47:56 +0000 | [diff] [blame] | 388 | bool lld::elf2::shouldKeepInSymtab(StringRef SymName) { |
| 389 | if (Config->DiscardNone) |
| 390 | return true; |
| 391 | |
| 392 | // ELF defines dynamic locals as symbols which name starts with ".L". |
| 393 | return !(Config->DiscardLocals && SymName.startswith(".L")); |
| 394 | } |
| 395 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 396 | template <class ELFT> |
| 397 | SymbolTableSection<ELFT>::SymbolTableSection( |
| 398 | SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec, |
| 399 | const OutputSection<ELFT> &BssSec) |
| 400 | : OutputSectionBase<ELFT::Is64Bits>( |
| 401 | StrTabSec.isDynamic() ? ".dynsym" : ".symtab", |
| 402 | StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB, |
| 403 | StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0), |
| 404 | Table(Table), StrTabSec(StrTabSec), BssSec(BssSec) { |
| 405 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 406 | typename Base::HeaderT &Header = this->Header; |
| 407 | |
| 408 | Header.sh_entsize = sizeof(Elf_Sym); |
| 409 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 410 | } |
| 411 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 412 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 413 | Buf += sizeof(Elf_Sym); |
| 414 | |
| 415 | // All symbols with STB_LOCAL binding precede the weak and global symbols. |
| 416 | // .dynsym only contains global symbols. |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 417 | if (!Config->DiscardAll && !StrTabSec.isDynamic()) |
| 418 | writeLocalSymbols(Buf); |
| 419 | |
| 420 | writeGlobalSymbols(Buf); |
| 421 | } |
| 422 | |
| 423 | template <class ELFT> |
| 424 | void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { |
| 425 | // Iterate over all input object files to copy their local symbols |
| 426 | // to the output symbol table pointed by Buf. |
| 427 | for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) { |
| 428 | auto &File = cast<ObjectFile<ELFT>>(*FileB); |
| 429 | Elf_Sym_Range Syms = File.getLocalSymbols(); |
| 430 | for (const Elf_Sym &Sym : Syms) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 431 | ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable()); |
| 432 | if (SymName && !shouldKeepInSymtab(*SymName)) |
| 433 | continue; |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 434 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
| 435 | Buf += sizeof(*ESym); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 436 | ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0; |
| 437 | ESym->st_size = Sym.st_size; |
| 438 | ESym->setBindingAndType(Sym.getBinding(), Sym.getType()); |
| 439 | uint32_t SecIndex = Sym.st_shndx; |
| 440 | uintX_t VA = Sym.st_value; |
| 441 | if (SecIndex == SHN_ABS) { |
| 442 | ESym->st_shndx = SHN_ABS; |
| 443 | } else { |
| 444 | if (SecIndex == SHN_XINDEX) |
| 445 | SecIndex = File.getObj().getExtendedSymbolTableIndex( |
| 446 | &Sym, File.getSymbolTable(), File.getSymbolTableShndx()); |
| 447 | ArrayRef<InputSection<ELFT> *> Sections = File.getSections(); |
| 448 | const InputSection<ELFT> *Section = Sections[SecIndex]; |
| 449 | const OutputSection<ELFT> *Out = Section->getOutputSection(); |
| 450 | ESym->st_shndx = Out->getSectionIndex(); |
| 451 | VA += Out->getVA() + Section->getOutputSectionOff(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 452 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 453 | ESym->st_value = VA; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 456 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 457 | |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 458 | template <class ELFT> |
| 459 | void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) { |
| 460 | // Write the internal symbol table contents to the output symbol table |
| 461 | // pointed by Buf. |
| 462 | for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 463 | StringRef Name = P.first; |
| 464 | Symbol *Sym = P.second; |
| 465 | SymbolBody *Body = Sym->Body; |
| 466 | if (!includeInSymtab(*Body)) |
| 467 | continue; |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 468 | if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body)) |
| 469 | continue; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 470 | |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 471 | const auto &EBody = *cast<ELFSymbolBody<ELFT>>(Body); |
| 472 | const Elf_Sym &InputSym = EBody.Sym; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 473 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 474 | Buf += sizeof(*ESym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 475 | ESym->st_name = StrTabSec.getFileOff(Name); |
| 476 | |
Rafael Espindola | 25b0acb | 2015-09-25 17:32:37 +0000 | [diff] [blame] | 477 | const OutputSection<ELFT> *Out = nullptr; |
| 478 | const InputSection<ELFT> *Section = nullptr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 479 | |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 480 | switch (EBody.kind()) { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 481 | case SymbolBody::DefinedSyntheticKind: |
| 482 | Out = &cast<DefinedSynthetic<ELFT>>(Body)->Section; |
| 483 | break; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 484 | case SymbolBody::DefinedRegularKind: |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 485 | Section = &cast<DefinedRegular<ELFT>>(EBody).Section; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 486 | break; |
| 487 | case SymbolBody::DefinedCommonKind: |
Rafael Espindola | c2d2119 | 2015-09-23 18:25:05 +0000 | [diff] [blame] | 488 | Out = &BssSec; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 489 | break; |
| 490 | case SymbolBody::UndefinedKind: |
| 491 | case SymbolBody::DefinedAbsoluteKind: |
| 492 | case SymbolBody::SharedKind: |
| 493 | break; |
| 494 | case SymbolBody::LazyKind: |
| 495 | llvm_unreachable("Lazy symbol got to output symbol table!"); |
| 496 | } |
| 497 | |
| 498 | ESym->setBindingAndType(InputSym.getBinding(), InputSym.getType()); |
| 499 | ESym->st_size = InputSym.st_size; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 500 | ESym->setVisibility(EBody.getMostConstrainingVisibility()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 501 | if (InputSym.isAbsolute()) { |
| 502 | ESym->st_shndx = SHN_ABS; |
| 503 | ESym->st_value = InputSym.st_value; |
| 504 | } |
| 505 | |
| 506 | if (Section) |
| 507 | Out = Section->getOutputSection(); |
| 508 | |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 509 | ESym->st_value = getSymVA(EBody, BssSec); |
| 510 | |
| 511 | if (Out) |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 512 | ESym->st_shndx = Out->getSectionIndex(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | namespace lld { |
| 517 | namespace elf2 { |
| 518 | template class OutputSectionBase<false>; |
| 519 | template class OutputSectionBase<true>; |
| 520 | |
| 521 | template void OutputSectionBase<false>::writeHeaderTo<support::little>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 522 | ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 523 | template void OutputSectionBase<true>::writeHeaderTo<support::little>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 524 | ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 525 | template void OutputSectionBase<false>::writeHeaderTo<support::big>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 526 | ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 527 | template void OutputSectionBase<true>::writeHeaderTo<support::big>( |
Rafael Espindola | f68b707 | 2015-09-21 22:21:46 +0000 | [diff] [blame] | 528 | ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 529 | |
| 530 | template class GotSection<ELF32LE>; |
| 531 | template class GotSection<ELF32BE>; |
| 532 | template class GotSection<ELF64LE>; |
| 533 | template class GotSection<ELF64BE>; |
| 534 | |
| 535 | template class PltSection<ELF32LE>; |
| 536 | template class PltSection<ELF32BE>; |
| 537 | template class PltSection<ELF64LE>; |
| 538 | template class PltSection<ELF64BE>; |
| 539 | |
| 540 | template class RelocationSection<ELF32LE>; |
| 541 | template class RelocationSection<ELF32BE>; |
| 542 | template class RelocationSection<ELF64LE>; |
| 543 | template class RelocationSection<ELF64BE>; |
| 544 | |
| 545 | template class InterpSection<false>; |
| 546 | template class InterpSection<true>; |
| 547 | |
| 548 | template class HashTableSection<ELF32LE>; |
| 549 | template class HashTableSection<ELF32BE>; |
| 550 | template class HashTableSection<ELF64LE>; |
| 551 | template class HashTableSection<ELF64BE>; |
| 552 | |
| 553 | template class DynamicSection<ELF32LE>; |
| 554 | template class DynamicSection<ELF32BE>; |
| 555 | template class DynamicSection<ELF64LE>; |
| 556 | template class DynamicSection<ELF64BE>; |
| 557 | |
| 558 | template class OutputSection<ELF32LE>; |
| 559 | template class OutputSection<ELF32BE>; |
| 560 | template class OutputSection<ELF64LE>; |
| 561 | template class OutputSection<ELF64BE>; |
| 562 | |
| 563 | template class StringTableSection<false>; |
| 564 | template class StringTableSection<true>; |
| 565 | |
| 566 | template class SymbolTableSection<ELF32LE>; |
| 567 | template class SymbolTableSection<ELF32BE>; |
| 568 | template class SymbolTableSection<ELF64LE>; |
| 569 | template class SymbolTableSection<ELF64BE>; |
| 570 | |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 571 | template ELFFile<ELF32LE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 572 | getSymVA(const ELFSymbolBody<ELF32LE> &, const OutputSection<ELF32LE> &); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 573 | |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 574 | template ELFFile<ELF32BE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 575 | getSymVA(const ELFSymbolBody<ELF32BE> &, const OutputSection<ELF32BE> &); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 576 | |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 577 | template ELFFile<ELF64LE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 578 | getSymVA(const ELFSymbolBody<ELF64LE> &, const OutputSection<ELF64LE> &); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 579 | |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 580 | template ELFFile<ELF64BE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 581 | getSymVA(const ELFSymbolBody<ELF64BE> &, const OutputSection<ELF64BE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 582 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 583 | template ELFFile<ELF32LE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 584 | getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 585 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 586 | template ELFFile<ELF32BE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 587 | getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 588 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 589 | template ELFFile<ELF64LE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 590 | getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 591 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 592 | template ELFFile<ELF64BE>::uintX_t |
Rui Ueyama | b189b5c | 2015-09-30 00:43:22 +0000 | [diff] [blame] | 593 | getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 594 | } |
| 595 | } |