| 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) |
| 28 | : Header(Header), File(File), SectionKind(SectionKind) {} |
| 29 | |
| 30 | template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const { |
| 31 | ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header); |
| 32 | error(Name); |
| 33 | return *Name; |
| 34 | } |
| 35 | |
| 36 | template <class ELFT> |
| 37 | ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const { |
| 38 | ErrorOr<ArrayRef<uint8_t>> Ret = |
| 39 | this->File->getObj().getSectionContents(this->Header); |
| 40 | error(Ret); |
| 41 | return *Ret; |
| 42 | } |
| 43 | |
| 44 | template <class ELFT> |
| 45 | typename ELFFile<ELFT>::uintX_t |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 46 | InputSectionBase<ELFT>::getOffset(uintX_t Offset) { |
| 47 | switch (SectionKind) { |
| 48 | case Regular: |
| 49 | return cast<InputSection<ELFT>>(this)->OutSecOff + Offset; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 50 | case EHFrame: |
| 51 | return cast<EHInputSection<ELFT>>(this)->getOffset(Offset); |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 52 | case Merge: |
| 53 | return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset); |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 54 | case MipsReginfo: |
| 55 | return cast<MipsReginfoInputSection<ELFT>>(this)->getOffset(Offset); |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 56 | } |
| Denis Protivensky | 1b1b34e | 2015-11-12 09:11:20 +0000 | [diff] [blame] | 57 | llvm_unreachable("Invalid section kind"); |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | template <class ELFT> |
| 61 | typename ELFFile<ELFT>::uintX_t |
| Rafael Espindola | 48225b4 | 2015-10-23 19:55:11 +0000 | [diff] [blame] | 62 | InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) { |
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 63 | return getOffset(Sym.st_value); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 66 | // Returns a section that Rel relocation is pointing to. |
| 67 | template <class ELFT> |
| 68 | InputSectionBase<ELFT> * |
| 69 | InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) { |
| 70 | // Global symbol |
| 71 | uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL); |
| 72 | if (SymbolBody *B = File->getSymbolBody(SymIndex)) |
| 73 | if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl())) |
| 74 | return &D->Section; |
| 75 | // Local symbol |
| 76 | if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex)) |
| 77 | if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym)) |
| 78 | return Sec; |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | template <class ELFT> |
| 83 | InputSectionBase<ELFT> * |
| 84 | InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) { |
| 85 | return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel)); |
| 86 | } |
| 87 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 88 | template <class ELFT> |
| Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 89 | InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header) |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 90 | : InputSectionBase<ELFT>(F, Header, Base::Regular) {} |
| 91 | |
| 92 | template <class ELFT> |
| 93 | bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| 94 | return S->SectionKind == Base::Regular; |
| 95 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 96 | |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 97 | template <class ELFT> |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 98 | template <bool isRela> |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 99 | uint8_t * |
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 100 | InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex, |
| 101 | uint32_t Type, |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 102 | RelIteratorRange<isRela> Rels) { |
| 103 | // Some MIPS relocations use addend calculated from addend of the relocation |
| 104 | // itself and addend of paired relocation. ABI requires to compute such |
| 105 | // combined addend in case of REL relocation record format only. |
| 106 | // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 107 | if (isRela || Config->EMachine != EM_MIPS) |
| 108 | return nullptr; |
| 109 | if (Type == R_MIPS_HI16) |
| 110 | Type = R_MIPS_LO16; |
| 111 | else if (Type == R_MIPS_PCHI16) |
| 112 | Type = R_MIPS_PCLO16; |
| 113 | else if (Type == R_MICROMIPS_HI16) |
| 114 | Type = R_MICROMIPS_LO16; |
| 115 | else |
| 116 | return nullptr; |
| 117 | for (const auto &RI : Rels) { |
| 118 | if (RI.getType(Config->Mips64EL) != Type) |
| 119 | continue; |
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 120 | if (RI.getSymbol(Config->Mips64EL) != SymIndex) |
| 121 | continue; |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 122 | uintX_t Offset = getOffset(RI.r_offset); |
| 123 | if (Offset == (uintX_t)-1) |
| 124 | return nullptr; |
| 125 | return Buf + Offset; |
| 126 | } |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
| 130 | template <class ELFT> |
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 131 | static typename llvm::object::ELFFile<ELFT>::uintX_t |
| 132 | getSymSize(SymbolBody &Body) { |
| 133 | if (auto *SS = dyn_cast<Defined<ELFT>>(&Body)) |
| 134 | return SS->Sym.st_size; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | template <class ELFT> |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 139 | template <bool isRela> |
| 140 | void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd, |
| 141 | RelIteratorRange<isRela> Rels) { |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 142 | typedef Elf_Rel_Impl<ELFT, isRela> RelType; |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 143 | size_t Num = Rels.end() - Rels.begin(); |
| 144 | for (size_t I = 0; I < Num; ++I) { |
| 145 | const RelType &RI = *(Rels.begin() + I); |
| Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 146 | uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); |
| 147 | uint32_t Type = RI.getType(Config->Mips64EL); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 148 | uintX_t Offset = getOffset(RI.r_offset); |
| 149 | if (Offset == (uintX_t)-1) |
| 150 | continue; |
| 151 | |
| 152 | uint8_t *BufLoc = Buf + Offset; |
| 153 | uintX_t AddrLoc = OutSec->getVA() + Offset; |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 154 | auto NextRelocs = llvm::make_range(&RI, Rels.end()); |
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 155 | |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 156 | if (Target->isTlsLocalDynamicReloc(Type) && |
| 157 | !Target->isTlsOptimized(Type, nullptr)) { |
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 158 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, |
| George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 159 | Out<ELFT>::Got->getLocalTlsIndexVA() + |
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 160 | getAddend<ELFT>(RI)); |
| 161 | continue; |
| 162 | } |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 163 | |
| 164 | // Handle relocations for local symbols -- they never get |
| 165 | // resolved so we don't allocate a SymbolBody. |
| Rafael Espindola | 8e37f79 | 2015-11-11 10:23:32 +0000 | [diff] [blame] | 166 | const Elf_Shdr *SymTab = File->getSymbolTable(); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 167 | if (SymIndex < SymTab->sh_info) { |
| Rafael Espindola | 8e37f79 | 2015-11-11 10:23:32 +0000 | [diff] [blame] | 168 | uintX_t SymVA = getLocalRelTarget(*File, RI); |
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 169 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0, |
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 170 | findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs)); |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 171 | continue; |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| Rafael Espindola | 8e37f79 | 2015-11-11 10:23:32 +0000 | [diff] [blame] | 174 | SymbolBody &Body = *File->getSymbolBody(SymIndex)->repl(); |
| Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 175 | |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 176 | if (Target->isTlsGlobalDynamicReloc(Type) && |
| 177 | !Target->isTlsOptimized(Type, &Body)) { |
| Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 178 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, |
| George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 179 | Out<ELFT>::Got->getGlobalDynAddr(Body) + |
| Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 180 | getAddend<ELFT>(RI)); |
| 181 | continue; |
| 182 | } |
| 183 | |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 184 | if (Target->isTlsOptimized(Type, &Body)) { |
| George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 185 | uintX_t SymVA = Target->relocNeedsGot(Type, Body) |
| 186 | ? Out<ELFT>::Got->getEntryAddr(Body) |
| 187 | : getSymVA<ELFT>(Body); |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 188 | // By optimizing TLS relocations, it is sometimes needed to skip |
| 189 | // relocations that immediately follow TLS relocations. This function |
| 190 | // knows how many slots we need to skip. |
| George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 191 | I += Target->relocateTlsOptimize(BufLoc, BufEnd, Type, AddrLoc, SymVA, |
| 192 | Body); |
| George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 193 | continue; |
| 194 | } |
| 195 | |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 196 | uintX_t SymVA = getSymVA<ELFT>(Body); |
| 197 | if (Target->relocNeedsPlt(Type, Body)) { |
| 198 | SymVA = Out<ELFT>::Plt->getEntryAddr(Body); |
| Igor Kudrin | e7ad093 | 2015-11-17 17:47:53 +0000 | [diff] [blame] | 199 | Type = Target->getPltRefReloc(Type); |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 200 | } else if (Target->relocNeedsGot(Type, Body)) { |
| 201 | SymVA = Out<ELFT>::Got->getEntryAddr(Body); |
| Rui Ueyama | 62d0e32 | 2015-12-17 00:04:18 +0000 | [diff] [blame] | 202 | if (Body.isTls()) |
| George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 203 | Type = Target->getTlsGotReloc(Type); |
| Rui Ueyama | 02dfd49 | 2015-12-17 01:18:40 +0000 | [diff] [blame] | 204 | } else if (!Target->needsCopyRel(Type, Body) && |
| George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 205 | isa<SharedSymbol<ELFT>>(Body)) { |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 206 | continue; |
| George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 207 | } else if (Target->isTlsDynReloc(Type, Body) || |
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 208 | Target->isSizeDynReloc(Type, Body)) { |
| George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 209 | continue; |
| Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 210 | } else if (Config->EMachine == EM_MIPS) { |
| 211 | if (Type == R_MIPS_HI16 && &Body == Config->MipsGpDisp) |
| 212 | SymVA = getMipsGpAddr<ELFT>() - AddrLoc; |
| 213 | else if (Type == R_MIPS_LO16 && &Body == Config->MipsGpDisp) |
| 214 | SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4; |
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 215 | } |
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 216 | uintX_t A = getAddend<ELFT>(RI); |
| 217 | uintX_t Size = getSymSize<ELFT>(Body); |
| 218 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A, |
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 219 | findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs)); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 223 | template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 224 | if (this->Header->sh_type == SHT_NOBITS) |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 225 | return; |
| 226 | // Copy section contents from source object file to output file. |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 227 | ArrayRef<uint8_t> Data = this->getSectionData(); |
| Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 228 | memcpy(Buf + OutSecOff, Data.data(), Data.size()); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 229 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 230 | ELFFile<ELFT> &EObj = this->File->getObj(); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 231 | uint8_t *BufEnd = Buf + OutSecOff + Data.size(); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 232 | // Iterate over all relocation sections that apply to this section. |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 233 | for (const Elf_Shdr *RelSec : this->RelocSections) { |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 234 | if (RelSec->sh_type == SHT_RELA) |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 235 | this->relocate(Buf, BufEnd, EObj.relas(RelSec)); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 236 | else |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 237 | this->relocate(Buf, BufEnd, EObj.rels(RelSec)); |
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 238 | } |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 241 | template <class ELFT> |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 242 | SplitInputSection<ELFT>::SplitInputSection( |
| 243 | ObjectFile<ELFT> *File, const Elf_Shdr *Header, |
| 244 | typename InputSectionBase<ELFT>::Kind SectionKind) |
| 245 | : InputSectionBase<ELFT>(File, Header, SectionKind) {} |
| 246 | |
| 247 | template <class ELFT> |
| 248 | EHInputSection<ELFT>::EHInputSection(ObjectFile<ELFT> *F, |
| 249 | const Elf_Shdr *Header) |
| 250 | : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {} |
| 251 | |
| 252 | template <class ELFT> |
| 253 | bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| 254 | return S->SectionKind == InputSectionBase<ELFT>::EHFrame; |
| 255 | } |
| 256 | |
| 257 | template <class ELFT> |
| 258 | typename EHInputSection<ELFT>::uintX_t |
| 259 | EHInputSection<ELFT>::getOffset(uintX_t Offset) { |
| 260 | std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first; |
| 261 | uintX_t Base = I->second; |
| George Rimar | 4b40ebc | 2015-11-13 13:44:59 +0000 | [diff] [blame] | 262 | if (Base == uintX_t(-1)) |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 263 | return -1; // Not in the output |
| 264 | |
| 265 | uintX_t Addend = Offset - I->first; |
| 266 | return Base + Addend; |
| 267 | } |
| 268 | |
| 269 | template <class ELFT> |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 270 | MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F, |
| 271 | const Elf_Shdr *Header) |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 272 | : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {} |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 273 | |
| 274 | template <class ELFT> |
| 275 | bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 276 | return S->SectionKind == InputSectionBase<ELFT>::Merge; |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 279 | template <class ELFT> |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 280 | std::pair<std::pair<typename ELFFile<ELFT>::uintX_t, |
| 281 | typename ELFFile<ELFT>::uintX_t> *, |
| 282 | typename ELFFile<ELFT>::uintX_t> |
| 283 | SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) { |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 284 | ArrayRef<uint8_t> D = this->getSectionData(); |
| Rui Ueyama | 7ba639b | 2015-10-25 16:25:04 +0000 | [diff] [blame] | 285 | StringRef Data((const char *)D.data(), D.size()); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 286 | uintX_t Size = Data.size(); |
| 287 | if (Offset >= Size) |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 288 | error("Entry is past the end of the section"); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 289 | |
| 290 | // Find the element this offset points to. |
| 291 | auto I = std::upper_bound( |
| Rafael Espindola | d04c12a | 2015-11-11 15:20:45 +0000 | [diff] [blame] | 292 | Offsets.begin(), Offsets.end(), Offset, |
| Rafael Espindola | 1fe2d1e | 2015-11-11 15:55:00 +0000 | [diff] [blame] | 293 | [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) { |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 294 | return A < B.first; |
| 295 | }); |
| Rafael Espindola | 3299499 | 2015-11-11 15:40:37 +0000 | [diff] [blame] | 296 | uintX_t End = I == Offsets.end() ? Data.size() : I->first; |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 297 | --I; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 298 | return std::make_pair(&*I, End); |
| 299 | } |
| 300 | |
| 301 | template <class ELFT> |
| 302 | typename MergeInputSection<ELFT>::uintX_t |
| 303 | MergeInputSection<ELFT>::getOffset(uintX_t Offset) { |
| George Rimar | 4b40ebc | 2015-11-13 13:44:59 +0000 | [diff] [blame] | 304 | std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T = |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 305 | this->getRangeAndSize(Offset); |
| 306 | std::pair<uintX_t, uintX_t> *I = T.first; |
| 307 | uintX_t End = T.second; |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 308 | uintX_t Start = I->first; |
| 309 | |
| 310 | // Compute the Addend and if the Base is cached, return. |
| 311 | uintX_t Addend = Offset - Start; |
| Rafael Espindola | 3299499 | 2015-11-11 15:40:37 +0000 | [diff] [blame] | 312 | uintX_t &Base = I->second; |
| Rafael Espindola | 1fe2d1e | 2015-11-11 15:55:00 +0000 | [diff] [blame] | 313 | if (Base != uintX_t(-1)) |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 314 | return Base + Addend; |
| 315 | |
| Hal Finkel | f950595 | 2015-11-25 23:54:53 +0000 | [diff] [blame] | 316 | // 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] | 317 | ArrayRef<uint8_t> D = this->getSectionData(); |
| 318 | StringRef Data((const char *)D.data(), D.size()); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 319 | StringRef Entry = Data.substr(Start, End - Start); |
| 320 | Base = |
| 321 | static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry); |
| 322 | return Base + Addend; |
| Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 325 | template <class ELFT> |
| 326 | MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F, |
| 327 | const Elf_Shdr *Header) |
| 328 | : InputSectionBase<ELFT>(F, Header, InputSectionBase<ELFT>::MipsReginfo) {} |
| 329 | |
| 330 | template <class ELFT> |
| 331 | uint32_t MipsReginfoInputSection<ELFT>::getGeneralMask() const { |
| 332 | ArrayRef<uint8_t> D = this->getSectionData(); |
| 333 | if (D.size() != sizeof(Elf_Mips_RegInfo)) |
| 334 | error("Invalid size of .reginfo section"); |
| 335 | return reinterpret_cast<const Elf_Mips_RegInfo *>(D.data())->ri_gprmask; |
| 336 | } |
| 337 | |
| 338 | template <class ELFT> |
| 339 | bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| 340 | return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo; |
| 341 | } |
| 342 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 343 | namespace lld { |
| 344 | namespace elf2 { |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 345 | template class InputSectionBase<object::ELF32LE>; |
| 346 | template class InputSectionBase<object::ELF32BE>; |
| 347 | template class InputSectionBase<object::ELF64LE>; |
| 348 | template class InputSectionBase<object::ELF64BE>; |
| 349 | |
| Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 350 | template class InputSection<object::ELF32LE>; |
| 351 | template class InputSection<object::ELF32BE>; |
| 352 | template class InputSection<object::ELF64LE>; |
| 353 | template class InputSection<object::ELF64BE>; |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 354 | |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 355 | template class EHInputSection<object::ELF32LE>; |
| 356 | template class EHInputSection<object::ELF32BE>; |
| 357 | template class EHInputSection<object::ELF64LE>; |
| 358 | template class EHInputSection<object::ELF64BE>; |
| 359 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 360 | template class MergeInputSection<object::ELF32LE>; |
| 361 | template class MergeInputSection<object::ELF32BE>; |
| 362 | template class MergeInputSection<object::ELF64LE>; |
| 363 | template class MergeInputSection<object::ELF64BE>; |
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 364 | |
| 365 | template class MipsReginfoInputSection<object::ELF32LE>; |
| 366 | template class MipsReginfoInputSection<object::ELF32BE>; |
| 367 | template class MipsReginfoInputSection<object::ELF64LE>; |
| 368 | template class MipsReginfoInputSection<object::ELF64BE>; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 369 | } |
| 370 | } |