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