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