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