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