| Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 1 | //===- InputSection.cpp ---------------------------------------------------===// |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | |
| Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 10 | #include "InputSection.h" |
| Rafael Espindola | 551dfd8 | 2015-09-25 19:24:57 +0000 | [diff] [blame] | 11 | #include "Config.h" |
| Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 12 | #include "Error.h" |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 14 | #include "OutputSections.h" |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 15 | #include "Target.h" |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 16 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | using namespace llvm::ELF; |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 19 | using namespace llvm::object; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lld; |
| 22 | using namespace lld::elf2; |
| 23 | |
| 24 | template <class ELFT> |
| Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 25 | InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header) |
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 26 | : File(F), Header(Header) {} |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 28 | template <class ELFT> |
| Rafael Espindola | aa19708 | 2015-10-15 15:52:12 +0000 | [diff] [blame] | 29 | void InputSection<ELFT>::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 30 | const Elf_Rel &Rel, uint32_t Type, |
| 31 | uintX_t BaseAddr, uintX_t SymVA) { |
| 32 | Target->relocateOne(Buf, BufEnd, reinterpret_cast<const void *>(&Rel), Type, |
| 33 | BaseAddr, SymVA); |
| 34 | } |
| 35 | |
| 36 | template <class ELFT> |
| 37 | void InputSection<ELFT>::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 38 | const Elf_Rela &Rel, uint32_t Type, |
| 39 | uintX_t BaseAddr, uintX_t SymVA) { |
| Rafael Espindola | aa19708 | 2015-10-15 15:52:12 +0000 | [diff] [blame] | 40 | Target->relocateOne(Buf, BufEnd, reinterpret_cast<const void *>(&Rel), Type, |
| Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 41 | BaseAddr, SymVA + Rel.r_addend); |
| Rafael Espindola | aa19708 | 2015-10-15 15:52:12 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | template <class ELFT> |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 45 | template <bool isRela> |
| 46 | void InputSection<ELFT>::relocate( |
| Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 47 | uint8_t *Buf, uint8_t *BufEnd, |
| 48 | iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels, |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 49 | const ObjectFile<ELFT> &File, uintX_t BaseAddr) { |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 50 | typedef Elf_Rel_Impl<ELFT, isRela> RelType; |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 51 | for (const RelType &RI : Rels) { |
| Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame^] | 52 | uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); |
| 53 | uint32_t Type = RI.getType(Config->Mips64EL); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 54 | |
| 55 | // Handle relocations for local symbols -- they never get |
| 56 | // resolved so we don't allocate a SymbolBody. |
| 57 | const Elf_Shdr *SymTab = File.getSymbolTable(); |
| 58 | if (SymIndex < SymTab->sh_info) { |
| Hal Finkel | 230c5c5 | 2015-10-16 22:37:32 +0000 | [diff] [blame] | 59 | uintX_t SymVA = getLocalRelTarget(File, RI); |
| Rafael Espindola | aa19708 | 2015-10-15 15:52:12 +0000 | [diff] [blame] | 60 | relocateOne(Buf, BufEnd, RI, Type, BaseAddr, SymVA); |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 61 | continue; |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 64 | SymbolBody &Body = *File.getSymbolBody(SymIndex)->repl(); |
| 65 | uintX_t SymVA = getSymVA<ELFT>(Body); |
| 66 | if (Target->relocNeedsPlt(Type, Body)) { |
| 67 | SymVA = Out<ELFT>::Plt->getEntryAddr(Body); |
| Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 68 | Type = Target->getPLTRefReloc(Type); |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 69 | } else if (Target->relocNeedsGot(Type, Body)) { |
| 70 | SymVA = Out<ELFT>::Got->getEntryAddr(Body); |
| 71 | Type = Target->getGotRefReloc(); |
| 72 | } else if (Target->relocPointsToGot(Type)) { |
| 73 | SymVA = Out<ELFT>::Got->getVA(); |
| 74 | Type = Target->getPCRelReloc(); |
| 75 | } else if (isa<SharedSymbol<ELFT>>(Body)) { |
| 76 | continue; |
| 77 | } |
| Rafael Espindola | aa19708 | 2015-10-15 15:52:12 +0000 | [diff] [blame] | 78 | relocateOne(Buf, BufEnd, RI, Type, BaseAddr, SymVA); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 82 | template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 83 | if (Header->sh_type == SHT_NOBITS) |
| 84 | return; |
| 85 | // Copy section contents from source object file to output file. |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 86 | ArrayRef<uint8_t> Data = *File->getObj().getSectionContents(Header); |
| Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 87 | memcpy(Buf + OutSecOff, Data.data(), Data.size()); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 88 | |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 89 | ELFFile<ELFT> &EObj = File->getObj(); |
| Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 90 | uint8_t *Base = Buf + OutSecOff; |
| 91 | uintX_t BaseAddr = OutSec->getVA() + OutSecOff; |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 92 | // Iterate over all relocation sections that apply to this section. |
| 93 | for (const Elf_Shdr *RelSec : RelocSections) { |
| 94 | if (RelSec->sh_type == SHT_RELA) |
| Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 95 | relocate(Base, Base + Data.size(), EObj.relas(RelSec), *File, BaseAddr); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 96 | else |
| Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 97 | relocate(Base, Base + Data.size(), EObj.rels(RelSec), *File, BaseAddr); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 98 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 101 | template <class ELFT> StringRef InputSection<ELFT>::getSectionName() const { |
| Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 102 | ErrorOr<StringRef> Name = File->getObj().getSectionName(Header); |
| Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 103 | error(Name); |
| 104 | return *Name; |
| 105 | } |
| 106 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 107 | namespace lld { |
| 108 | namespace elf2 { |
| Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 109 | template class InputSection<object::ELF32LE>; |
| 110 | template class InputSection<object::ELF32BE>; |
| 111 | template class InputSection<object::ELF64LE>; |
| 112 | template class InputSection<object::ELF64BE>; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 113 | } |
| 114 | } |