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