| 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 | |
| Simon Atanasyan | 860fbf0 | 2016-02-25 21:33:56 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Endian.h" |
| 18 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace llvm::ELF; |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 21 | using namespace llvm::object; |
| Simon Atanasyan | 860fbf0 | 2016-02-25 21:33:56 +0000 | [diff] [blame] | 22 | using namespace llvm::support::endian; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace lld; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 25 | using namespace lld::elf; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 26 | |
| 27 | template <class ELFT> |
| Rafael Espindola | b20dcb1 | 2016-03-11 16:23:45 +0000 | [diff] [blame] | 28 | InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File, |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 29 | const Elf_Shdr *Header, |
| 30 | Kind SectionKind) |
| Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 31 | : Header(Header), File(File), SectionKind(SectionKind), Repl(this) { |
| Rui Ueyama | 8fc070d | 2016-02-24 00:23:15 +0000 | [diff] [blame] | 32 | // The garbage collector sets sections' Live bits. |
| 33 | // If GC is disabled, all sections are considered live by default. |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 34 | Live = !Config->GcSections; |
| Rui Ueyama | 5ac5891 | 2016-02-24 00:38:18 +0000 | [diff] [blame] | 35 | |
| 36 | // The ELF spec states that a value of 0 means the section has |
| 37 | // no alignment constraits. |
| Rui Ueyama | 733153d | 2016-02-24 18:33:35 +0000 | [diff] [blame] | 38 | Align = std::max<uintX_t>(Header->sh_addralign, 1); |
| Rui Ueyama | 8fc070d | 2016-02-24 00:23:15 +0000 | [diff] [blame] | 39 | } |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 40 | |
| 41 | template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const { |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 42 | return check(File->getObj().getSectionName(this->Header)); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | template <class ELFT> |
| 46 | ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const { |
| Rafael Espindola | 75714f6 | 2016-03-03 22:24:39 +0000 | [diff] [blame] | 47 | return check(this->File->getObj().getSectionContents(this->Header)); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | template <class ELFT> |
| 51 | typename ELFFile<ELFT>::uintX_t |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 52 | InputSectionBase<ELFT>::getOffset(uintX_t Offset) { |
| 53 | switch (SectionKind) { |
| 54 | case Regular: |
| 55 | return cast<InputSection<ELFT>>(this)->OutSecOff + Offset; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 56 | case EHFrame: |
| 57 | return cast<EHInputSection<ELFT>>(this)->getOffset(Offset); |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 58 | case Merge: |
| 59 | return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset); |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 60 | case MipsReginfo: |
| Rui Ueyama | 58a636a | 2016-01-06 22:01:25 +0000 | [diff] [blame] | 61 | // MIPS .reginfo sections are consumed by the linker, |
| 62 | // so it should never be copied to output. |
| 63 | llvm_unreachable("MIPS .reginfo reached writeTo()."); |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 64 | } |
| Denis Protivensky | 1b1b34e | 2015-11-12 09:11:20 +0000 | [diff] [blame] | 65 | llvm_unreachable("Invalid section kind"); |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | template <class ELFT> |
| 69 | typename ELFFile<ELFT>::uintX_t |
| Rafael Espindola | 48225b4 | 2015-10-23 19:55:11 +0000 | [diff] [blame] | 70 | InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) { |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 71 | return getOffset(Sym.st_value); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 74 | // Returns a section that Rel relocation is pointing to. |
| 75 | template <class ELFT> |
| 76 | InputSectionBase<ELFT> * |
| Rui Ueyama | d7e4a28 | 2016-02-24 00:23:13 +0000 | [diff] [blame] | 77 | InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) const { |
| Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 78 | // Global symbol |
| 79 | uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL); |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 80 | SymbolBody &B = File->getSymbolBody(SymIndex).repl(); |
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 81 | InputSectionBase<ELFT> *S = nullptr; |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 82 | if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) |
| Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 83 | S = D->Section; |
| 84 | if (S) |
| 85 | return S->Repl; |
| Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 86 | return nullptr; |
| 87 | } |
| 88 | |
| 89 | template <class ELFT> |
| 90 | InputSectionBase<ELFT> * |
| Rui Ueyama | d7e4a28 | 2016-02-24 00:23:13 +0000 | [diff] [blame] | 91 | InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) const { |
| Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 92 | return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel)); |
| 93 | } |
| 94 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 95 | template <class ELFT> |
| Rafael Espindola | b20dcb1 | 2016-03-11 16:23:45 +0000 | [diff] [blame] | 96 | InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F, |
| 97 | const Elf_Shdr *Header) |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 98 | : InputSectionBase<ELFT>(F, Header, Base::Regular) {} |
| 99 | |
| 100 | template <class ELFT> |
| 101 | bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| 102 | return S->SectionKind == Base::Regular; |
| 103 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 104 | |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 105 | template <class ELFT> |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 106 | InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() { |
| 107 | assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL); |
| 108 | ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections(); |
| 109 | return Sections[this->Header->sh_info]; |
| 110 | } |
| 111 | |
| 112 | // This is used for -r. We can't use memcpy to copy relocations because we need |
| 113 | // to update symbol table offset and section index for each relocation. So we |
| 114 | // copy relocations one by one. |
| 115 | template <class ELFT> |
| 116 | template <bool isRela> |
| 117 | void InputSection<ELFT>::copyRelocations(uint8_t *Buf, |
| 118 | RelIteratorRange<isRela> Rels) { |
| 119 | typedef Elf_Rel_Impl<ELFT, isRela> RelType; |
| 120 | InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection(); |
| 121 | |
| 122 | for (const RelType &Rel : Rels) { |
| 123 | uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL); |
| 124 | uint32_t Type = Rel.getType(Config->Mips64EL); |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 125 | SymbolBody &Body = this->File->getSymbolBody(SymIndex).repl(); |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 126 | |
| 127 | RelType *P = reinterpret_cast<RelType *>(Buf); |
| 128 | Buf += sizeof(RelType); |
| 129 | |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 130 | P->r_offset = RelocatedSection->getOffset(Rel.r_offset); |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 131 | P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL); |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| Simon Atanasyan | 860fbf0 | 2016-02-25 21:33:56 +0000 | [diff] [blame] | 135 | static uint32_t getMipsPairedRelocType(uint32_t Type) { |
| 136 | if (Config->EMachine != EM_MIPS) |
| 137 | return R_MIPS_NONE; |
| 138 | switch (Type) { |
| 139 | case R_MIPS_HI16: |
| 140 | return R_MIPS_LO16; |
| 141 | case R_MIPS_PCHI16: |
| 142 | return R_MIPS_PCLO16; |
| 143 | case R_MICROMIPS_HI16: |
| 144 | return R_MICROMIPS_LO16; |
| 145 | default: |
| 146 | return R_MIPS_NONE; |
| 147 | } |
| 148 | } |
| 149 | |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 150 | template <class ELFT> |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 151 | template <bool isRela> |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 152 | uint8_t * |
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 153 | InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex, |
| 154 | uint32_t Type, |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 155 | RelIteratorRange<isRela> Rels) { |
| 156 | // Some MIPS relocations use addend calculated from addend of the relocation |
| 157 | // itself and addend of paired relocation. ABI requires to compute such |
| 158 | // combined addend in case of REL relocation record format only. |
| 159 | // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| Simon Atanasyan | 860fbf0 | 2016-02-25 21:33:56 +0000 | [diff] [blame] | 160 | if (isRela || Type == R_MIPS_NONE) |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 161 | return nullptr; |
| 162 | for (const auto &RI : Rels) { |
| 163 | if (RI.getType(Config->Mips64EL) != Type) |
| 164 | continue; |
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 165 | if (RI.getSymbol(Config->Mips64EL) != SymIndex) |
| 166 | continue; |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 167 | uintX_t Offset = getOffset(RI.r_offset); |
| 168 | if (Offset == (uintX_t)-1) |
| 169 | return nullptr; |
| 170 | return Buf + Offset; |
| 171 | } |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | template <class ELFT> |
| 176 | template <bool isRela> |
| 177 | void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd, |
| 178 | RelIteratorRange<isRela> Rels) { |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 179 | typedef Elf_Rel_Impl<ELFT, isRela> RelType; |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 180 | size_t Num = Rels.end() - Rels.begin(); |
| 181 | for (size_t I = 0; I < Num; ++I) { |
| 182 | const RelType &RI = *(Rels.begin() + I); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 183 | uintX_t Offset = getOffset(RI.r_offset); |
| 184 | if (Offset == (uintX_t)-1) |
| 185 | continue; |
| 186 | |
| George Rimar | a4ab97d | 2016-03-11 12:57:52 +0000 | [diff] [blame] | 187 | uintX_t A = getAddend<ELFT>(RI); |
| 188 | uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); |
| 189 | uint32_t Type = RI.getType(Config->Mips64EL); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 190 | uint8_t *BufLoc = Buf + Offset; |
| 191 | uintX_t AddrLoc = OutSec->getVA() + Offset; |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 192 | auto NextRelocs = llvm::make_range(&RI, Rels.end()); |
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 193 | |
| Rafael Espindola | d405f47 | 2016-03-04 21:37:09 +0000 | [diff] [blame] | 194 | if (Target->pointsToLocalDynamicGotEntry(Type) && |
| Rui Ueyama | baf1651 | 2016-01-29 00:20:12 +0000 | [diff] [blame] | 195 | !Target->canRelaxTls(Type, nullptr)) { |
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 196 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, |
| George Rimar | 1d9738c | 2016-03-11 12:53:17 +0000 | [diff] [blame] | 197 | Out<ELFT>::Got->getTlsIndexVA() + A); |
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 198 | continue; |
| 199 | } |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 200 | |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 201 | SymbolBody &Body = File->getSymbolBody(SymIndex).repl(); |
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 202 | |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 203 | if (Target->canRelaxTls(Type, &Body)) { |
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 204 | uintX_t SymVA; |
| Rafael Espindola | 38e5d4e | 2016-03-11 13:04:28 +0000 | [diff] [blame] | 205 | if (Target->needsGot(Type, Body)) |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 206 | SymVA = Body.getGotVA<ELFT>(); |
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 207 | else |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 208 | SymVA = Body.getVA<ELFT>(); |
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 209 | // By optimizing TLS relocations, it is sometimes needed to skip |
| 210 | // relocations that immediately follow TLS relocations. This function |
| 211 | // knows how many slots we need to skip. |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 212 | I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body); |
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 213 | continue; |
| 214 | } |
| 215 | |
| Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 216 | // PPC64 has a special relocation representing the TOC base pointer |
| 217 | // that does not have a corresponding symbol. |
| 218 | if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) { |
| 219 | uintX_t SymVA = getPPC64TocBase() + A; |
| 220 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0); |
| 221 | continue; |
| 222 | } |
| 223 | |
| Rafael Espindola | 38e5d4e | 2016-03-11 13:04:28 +0000 | [diff] [blame] | 224 | if (Target->isTlsGlobalDynamicRel(Type) && |
| 225 | !Target->canRelaxTls(Type, &Body)) { |
| 226 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, |
| 227 | Out<ELFT>::Got->getGlobalDynAddr(Body) + A); |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | uintX_t SymVA = Body.getVA<ELFT>(A); |
| 232 | bool CBP = canBePreempted(Body); |
| 233 | uint8_t *PairedLoc = nullptr; |
| 234 | |
| 235 | if (Target->needsPlt<ELFT>(Type, Body)) { |
| 236 | SymVA = Body.getPltVA<ELFT>() + A; |
| 237 | } else if (Target->needsGot(Type, Body)) { |
| 238 | if (Config->EMachine == EM_MIPS && !CBP) { |
| 239 | if (Body.isLocal()) { |
| Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 240 | // R_MIPS_GOT16 relocation against local symbol requires index of |
| 241 | // a local GOT entry which contains page address corresponds |
| Simon Atanasyan | 860fbf0 | 2016-02-25 21:33:56 +0000 | [diff] [blame] | 242 | // to sum of the symbol address and addend. The addend in that case |
| 243 | // is calculated using addends from R_MIPS_GOT16 and paired |
| 244 | // R_MIPS_LO16 relocations. |
| 245 | const endianness E = ELFT::TargetEndianness; |
| 246 | uint8_t *LowLoc = |
| 247 | findMipsPairedReloc(Buf, SymIndex, R_MIPS_LO16, NextRelocs); |
| 248 | uint64_t AHL = read32<E>(BufLoc) << 16; |
| 249 | if (LowLoc) |
| 250 | AHL += SignExtend64<16>(read32<E>(LowLoc)); |
| 251 | SymVA = Out<ELFT>::Got->getMipsLocalPageAddr(SymVA + AHL); |
| Rafael Espindola | 38e5d4e | 2016-03-11 13:04:28 +0000 | [diff] [blame] | 252 | } else { |
| Simon Atanasyan | 019049f | 2016-03-11 13:57:53 +0000 | [diff] [blame] | 253 | // For non-local symbols GOT entries should contain their full |
| 254 | // addresses. But if such symbol cannot be preempted, we do not |
| 255 | // have to put them into the "global" part of GOT and use dynamic |
| 256 | // linker to determine their actual addresses. That is why we |
| 257 | // create GOT entries for them in the "local" part of GOT. |
| Rafael Espindola | 38e5d4e | 2016-03-11 13:04:28 +0000 | [diff] [blame] | 258 | SymVA = Out<ELFT>::Got->getMipsLocalFullAddr(Body) + A; |
| 259 | } |
| 260 | } else { |
| 261 | SymVA = Body.getGotVA<ELFT>() + A; |
| Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 262 | } |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 263 | if (Body.IsTls) |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 264 | Type = Target->getTlsGotRel(Type); |
| Rafael Espindola | a350e26 | 2016-02-26 14:33:23 +0000 | [diff] [blame] | 265 | } else if (Target->isSizeRel(Type) && CBP) { |
| Rui Ueyama | 3a7c2f6 | 2016-01-08 00:13:23 +0000 | [diff] [blame] | 266 | // A SIZE relocation is supposed to set a symbol size, but if a symbol |
| 267 | // can be preempted, the size at runtime may be different than link time. |
| 268 | // If that's the case, we leave the field alone rather than filling it |
| 269 | // with a possibly incorrect value. |
| George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 270 | continue; |
| Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 271 | } else if (Config->EMachine == EM_MIPS) { |
| Rafael Espindola | 38e5d4e | 2016-03-11 13:04:28 +0000 | [diff] [blame] | 272 | if (Type == R_MIPS_HI16 && &Body == Config->MipsGpDisp) { |
| 273 | SymVA = getMipsGpAddr<ELFT>() - AddrLoc + A; |
| 274 | } else if (Type == R_MIPS_LO16 && &Body == Config->MipsGpDisp) { |
| 275 | SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4 + A; |
| 276 | } else if (&Body == Config->MipsLocalGp) { |
| 277 | SymVA = getMipsGpAddr<ELFT>() + A; |
| 278 | } else if (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32) { |
| 279 | // We need to adjust SymVA value in case of R_MIPS_GPREL16/32 |
| 280 | // relocations because they use the following expression to calculate |
| 281 | // the relocation's result for local symbol: S + A + GP0 - G. |
| 282 | SymVA += File->getMipsGp0(); |
| 283 | } else { |
| 284 | PairedLoc = findMipsPairedReloc( |
| 285 | Buf, SymIndex, getMipsPairedRelocType(Type), NextRelocs); |
| 286 | } |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 287 | } else if (!Target->needsCopyRel<ELFT>(Type, Body) && CBP) { |
| Rafael Espindola | 1f04c44 | 2016-03-08 20:24:36 +0000 | [diff] [blame] | 288 | continue; |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 289 | } |
| Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 290 | uintX_t Size = Body.getSize<ELFT>(); |
| Rafael Espindola | 38e5d4e | 2016-03-11 13:04:28 +0000 | [diff] [blame] | 291 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, Size + A, |
| 292 | PairedLoc); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 296 | template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 297 | if (this->Header->sh_type == SHT_NOBITS) |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 298 | return; |
| 299 | // Copy section contents from source object file to output file. |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 300 | ArrayRef<uint8_t> Data = this->getSectionData(); |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 301 | ELFFile<ELFT> &EObj = this->File->getObj(); |
| 302 | |
| 303 | // That happens with -r. In that case we need fix the relocation position and |
| 304 | // target. No relocations are applied. |
| 305 | if (this->Header->sh_type == SHT_RELA) { |
| 306 | this->copyRelocations(Buf + OutSecOff, EObj.relas(this->Header)); |
| 307 | return; |
| 308 | } |
| 309 | if (this->Header->sh_type == SHT_REL) { |
| 310 | this->copyRelocations(Buf + OutSecOff, EObj.rels(this->Header)); |
| 311 | return; |
| 312 | } |
| 313 | |
| Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 314 | memcpy(Buf + OutSecOff, Data.data(), Data.size()); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 315 | |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 316 | uint8_t *BufEnd = Buf + OutSecOff + Data.size(); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 317 | // Iterate over all relocation sections that apply to this section. |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 318 | for (const Elf_Shdr *RelSec : this->RelocSections) { |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 319 | if (RelSec->sh_type == SHT_RELA) |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 320 | this->relocate(Buf, BufEnd, EObj.relas(RelSec)); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 321 | else |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 322 | this->relocate(Buf, BufEnd, EObj.rels(RelSec)); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 323 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 326 | template <class ELFT> |
| Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 327 | void InputSection<ELFT>::replace(InputSection<ELFT> *Other) { |
| 328 | this->Align = std::max(this->Align, Other->Align); |
| 329 | Other->Repl = this->Repl; |
| 330 | Other->Live = false; |
| 331 | } |
| 332 | |
| 333 | template <class ELFT> |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 334 | SplitInputSection<ELFT>::SplitInputSection( |
| Rafael Espindola | b20dcb1 | 2016-03-11 16:23:45 +0000 | [diff] [blame] | 335 | elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header, |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 336 | typename InputSectionBase<ELFT>::Kind SectionKind) |
| 337 | : InputSectionBase<ELFT>(File, Header, SectionKind) {} |
| 338 | |
| 339 | template <class ELFT> |
| Rafael Espindola | b20dcb1 | 2016-03-11 16:23:45 +0000 | [diff] [blame] | 340 | EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F, |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 341 | const Elf_Shdr *Header) |
| Rui Ueyama | da73532 | 2015-12-24 10:08:54 +0000 | [diff] [blame] | 342 | : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) { |
| 343 | // Mark .eh_frame sections as live by default because there are |
| 344 | // usually no relocations that point to .eh_frames. Otherwise, |
| George Rimar | e9e1d32 | 2016-02-18 15:17:01 +0000 | [diff] [blame] | 345 | // the garbage collector would drop all .eh_frame sections. |
| Rui Ueyama | da73532 | 2015-12-24 10:08:54 +0000 | [diff] [blame] | 346 | this->Live = true; |
| 347 | } |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 348 | |
| 349 | template <class ELFT> |
| 350 | bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| 351 | return S->SectionKind == InputSectionBase<ELFT>::EHFrame; |
| 352 | } |
| 353 | |
| 354 | template <class ELFT> |
| 355 | typename EHInputSection<ELFT>::uintX_t |
| 356 | EHInputSection<ELFT>::getOffset(uintX_t Offset) { |
| George Rimar | 6ab275c | 2015-12-25 09:51:42 +0000 | [diff] [blame] | 357 | // The file crtbeginT.o has relocations pointing to the start of an empty |
| 358 | // .eh_frame that is known to be the first in the link. It does that to |
| 359 | // identify the start of the output .eh_frame. Handle this special case. |
| 360 | if (this->getSectionHdr()->sh_size == 0) |
| 361 | return Offset; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 362 | std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first; |
| 363 | uintX_t Base = I->second; |
| George Rimar | 4b40ebc | 2015-11-13 13:44:59 +0000 | [diff] [blame] | 364 | if (Base == uintX_t(-1)) |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 365 | return -1; // Not in the output |
| 366 | |
| 367 | uintX_t Addend = Offset - I->first; |
| 368 | return Base + Addend; |
| 369 | } |
| 370 | |
| 371 | template <class ELFT> |
| Rafael Espindola | 36a73d2 | 2016-03-11 16:32:46 +0000 | [diff] [blame^] | 372 | MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F, |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 373 | const Elf_Shdr *Header) |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 374 | : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {} |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 375 | |
| 376 | template <class ELFT> |
| 377 | bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 378 | return S->SectionKind == InputSectionBase<ELFT>::Merge; |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 381 | template <class ELFT> |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 382 | std::pair<std::pair<typename ELFFile<ELFT>::uintX_t, |
| 383 | typename ELFFile<ELFT>::uintX_t> *, |
| 384 | typename ELFFile<ELFT>::uintX_t> |
| 385 | SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) { |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 386 | ArrayRef<uint8_t> D = this->getSectionData(); |
| Rui Ueyama | 7ba639b | 2015-10-25 16:25:04 +0000 | [diff] [blame] | 387 | StringRef Data((const char *)D.data(), D.size()); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 388 | uintX_t Size = Data.size(); |
| 389 | if (Offset >= Size) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 390 | fatal("Entry is past the end of the section"); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 391 | |
| 392 | // Find the element this offset points to. |
| 393 | auto I = std::upper_bound( |
| Rafael Espindola | d04c12a | 2015-11-11 15:20:45 +0000 | [diff] [blame] | 394 | Offsets.begin(), Offsets.end(), Offset, |
| Rafael Espindola | 1fe2d1e | 2015-11-11 15:55:00 +0000 | [diff] [blame] | 395 | [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) { |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 396 | return A < B.first; |
| 397 | }); |
| Rafael Espindola | 3299499 | 2015-11-11 15:40:37 +0000 | [diff] [blame] | 398 | uintX_t End = I == Offsets.end() ? Data.size() : I->first; |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 399 | --I; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 400 | return std::make_pair(&*I, End); |
| 401 | } |
| 402 | |
| 403 | template <class ELFT> |
| 404 | typename MergeInputSection<ELFT>::uintX_t |
| 405 | MergeInputSection<ELFT>::getOffset(uintX_t Offset) { |
| George Rimar | 4b40ebc | 2015-11-13 13:44:59 +0000 | [diff] [blame] | 406 | std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T = |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 407 | this->getRangeAndSize(Offset); |
| 408 | std::pair<uintX_t, uintX_t> *I = T.first; |
| 409 | uintX_t End = T.second; |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 410 | uintX_t Start = I->first; |
| 411 | |
| 412 | // Compute the Addend and if the Base is cached, return. |
| 413 | uintX_t Addend = Offset - Start; |
| Rafael Espindola | 3299499 | 2015-11-11 15:40:37 +0000 | [diff] [blame] | 414 | uintX_t &Base = I->second; |
| Rafael Espindola | 1fe2d1e | 2015-11-11 15:55:00 +0000 | [diff] [blame] | 415 | if (Base != uintX_t(-1)) |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 416 | return Base + Addend; |
| 417 | |
| Hal Finkel | f950595 | 2015-11-25 23:54:53 +0000 | [diff] [blame] | 418 | // Map the base to the offset in the output section and cache it. |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 419 | ArrayRef<uint8_t> D = this->getSectionData(); |
| 420 | StringRef Data((const char *)D.data(), D.size()); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 421 | StringRef Entry = Data.substr(Start, End - Start); |
| 422 | Base = |
| 423 | static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry); |
| 424 | return Base + Addend; |
| Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 427 | template <class ELFT> |
| Rafael Espindola | b20dcb1 | 2016-03-11 16:23:45 +0000 | [diff] [blame] | 428 | MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F, |
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 429 | const Elf_Shdr *Hdr) |
| 430 | : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) { |
| 431 | // Initialize this->Reginfo. |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 432 | ArrayRef<uint8_t> D = this->getSectionData(); |
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 433 | if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) |
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 434 | fatal("Invalid size of .reginfo section"); |
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 435 | Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data()); |
| Simon Atanasyan | 57830b6 | 2015-12-25 13:02:13 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 438 | template <class ELFT> |
| 439 | bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| 440 | return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo; |
| 441 | } |
| 442 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 443 | template class elf::InputSectionBase<ELF32LE>; |
| 444 | template class elf::InputSectionBase<ELF32BE>; |
| 445 | template class elf::InputSectionBase<ELF64LE>; |
| 446 | template class elf::InputSectionBase<ELF64BE>; |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 447 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 448 | template class elf::InputSection<ELF32LE>; |
| 449 | template class elf::InputSection<ELF32BE>; |
| 450 | template class elf::InputSection<ELF64LE>; |
| 451 | template class elf::InputSection<ELF64BE>; |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 452 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 453 | template class elf::EHInputSection<ELF32LE>; |
| 454 | template class elf::EHInputSection<ELF32BE>; |
| 455 | template class elf::EHInputSection<ELF64LE>; |
| 456 | template class elf::EHInputSection<ELF64BE>; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 457 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 458 | template class elf::MergeInputSection<ELF32LE>; |
| 459 | template class elf::MergeInputSection<ELF32BE>; |
| 460 | template class elf::MergeInputSection<ELF64LE>; |
| 461 | template class elf::MergeInputSection<ELF64BE>; |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 462 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 463 | template class elf::MipsReginfoInputSection<ELF32LE>; |
| 464 | template class elf::MipsReginfoInputSection<ELF32BE>; |
| 465 | template class elf::MipsReginfoInputSection<ELF64LE>; |
| 466 | template class elf::MipsReginfoInputSection<ELF64BE>; |