1 //===- InputSection.cpp ---------------------------------------------------===// 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 10 #include "InputSection.h" 11 #include "Config.h" 12 #include "EhFrame.h" 13 #include "Error.h" 14 #include "InputFiles.h" 15 #include "LinkerScript.h" 16 #include "OutputSections.h" 17 #include "Target.h" 18 #include "Thunks.h" 19 20 #include "llvm/Support/Compression.h" 21 #include "llvm/Support/Endian.h" 22 23 using namespace llvm; 24 using namespace llvm::ELF; 25 using namespace llvm::object; 26 using namespace llvm::support::endian; 27 28 using namespace lld; 29 using namespace lld::elf; 30 31 template <class ELFT> 32 static ArrayRef<uint8_t> getSectionContents(elf::ObjectFile<ELFT> *File, 33 const typename ELFT::Shdr *Hdr) { 34 if (!File || Hdr->sh_type == SHT_NOBITS) 35 return {}; 36 return check(File->getObj().getSectionContents(Hdr)); 37 } 38 39 template <class ELFT> 40 InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File, 41 const Elf_Shdr *Hdr, StringRef Name, 42 Kind SectionKind) 43 : InputSectionData(SectionKind, Name, getSectionContents(File, Hdr), 44 Hdr->sh_flags & SHF_COMPRESSED, !Config->GcSections), 45 Header(Hdr), File(File), Repl(this) { 46 // The ELF spec states that a value of 0 means the section has 47 // no alignment constraits. 48 Alignment = std::max<uintX_t>(Header->sh_addralign, 1); 49 } 50 51 template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const { 52 if (auto *D = dyn_cast<InputSection<ELFT>>(this)) 53 if (D->getThunksSize() > 0) 54 return D->getThunkOff() + D->getThunksSize(); 55 return Header->sh_size; 56 } 57 58 // Returns a string for an error message. 59 template <class SectionT> static std::string getName(SectionT *Sec) { 60 return (Sec->getFile()->getName() + "(" + Sec->Name + ")").str(); 61 } 62 63 template <class ELFT> 64 typename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) const { 65 switch (kind()) { 66 case Regular: 67 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset; 68 case EHFrame: 69 // The file crtbeginT.o has relocations pointing to the start of an empty 70 // .eh_frame that is known to be the first in the link. It does that to 71 // identify the start of the output .eh_frame. 72 return Offset; 73 case Merge: 74 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset); 75 case MipsReginfo: 76 case MipsOptions: 77 case MipsAbiFlags: 78 // MIPS .reginfo, .MIPS.options, and .MIPS.abiflags sections are consumed 79 // by the linker, and the linker produces a single output section. It is 80 // possible that input files contain section symbol points to the 81 // corresponding input section. Redirect it to the produced output section. 82 if (Offset != 0) 83 fatal(getName(this) + ": unsupported reference to the middle of '" + 84 Name + "' section"); 85 return this->OutSec->getVA(); 86 } 87 llvm_unreachable("invalid section kind"); 88 } 89 90 template <class ELFT> void InputSectionBase<ELFT>::uncompress() { 91 if (!zlib::isAvailable()) 92 fatal(getName(this) + 93 ": build lld with zlib to enable compressed sections support"); 94 95 // A compressed section consists of a header of Elf_Chdr type 96 // followed by compressed data. 97 if (Data.size() < sizeof(Elf_Chdr)) 98 fatal("corrupt compressed section"); 99 100 auto *Hdr = reinterpret_cast<const Elf_Chdr *>(Data.data()); 101 Data = Data.slice(sizeof(Elf_Chdr)); 102 103 if (Hdr->ch_type != ELFCOMPRESS_ZLIB) 104 fatal(getName(this) + ": unsupported compression type"); 105 106 StringRef Buf((const char *)Data.data(), Data.size()); 107 size_t UncompressedDataSize = Hdr->ch_size; 108 UncompressedData.reset(new char[UncompressedDataSize]); 109 if (zlib::uncompress(Buf, UncompressedData.get(), UncompressedDataSize) != 110 zlib::StatusOK) 111 fatal(getName(this) + ": error uncompressing section"); 112 Data = ArrayRef<uint8_t>((uint8_t *)UncompressedData.get(), 113 UncompressedDataSize); 114 } 115 116 template <class ELFT> 117 typename ELFT::uint 118 InputSectionBase<ELFT>::getOffset(const DefinedRegular<ELFT> &Sym) const { 119 return getOffset(Sym.Value); 120 } 121 122 template <class ELFT> 123 InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F, 124 const Elf_Shdr *Header, StringRef Name) 125 : InputSectionBase<ELFT>(F, Header, Name, Base::Regular) {} 126 127 template <class ELFT> 128 bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 129 return S->kind() == Base::Regular; 130 } 131 132 template <class ELFT> 133 InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() { 134 assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL); 135 ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections(); 136 return Sections[this->Header->sh_info]; 137 } 138 139 template <class ELFT> 140 void InputSection<ELFT>::addThunk(const Thunk<ELFT> *T) { 141 Thunks.push_back(T); 142 } 143 144 template <class ELFT> uint64_t InputSection<ELFT>::getThunkOff() const { 145 return this->Header->sh_size; 146 } 147 148 template <class ELFT> uint64_t InputSection<ELFT>::getThunksSize() const { 149 uint64_t Total = 0; 150 for (const Thunk<ELFT> *T : Thunks) 151 Total += T->size(); 152 return Total; 153 } 154 155 // This is used for -r. We can't use memcpy to copy relocations because we need 156 // to update symbol table offset and section index for each relocation. So we 157 // copy relocations one by one. 158 template <class ELFT> 159 template <class RelTy> 160 void InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) { 161 InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection(); 162 163 for (const RelTy &Rel : Rels) { 164 uint32_t Type = Rel.getType(Config->Mips64EL); 165 SymbolBody &Body = this->File->getRelocTargetSym(Rel); 166 167 Elf_Rela *P = reinterpret_cast<Elf_Rela *>(Buf); 168 Buf += sizeof(RelTy); 169 170 if (Config->Rela) 171 P->r_addend = getAddend<ELFT>(Rel); 172 P->r_offset = RelocatedSection->getOffset(Rel.r_offset); 173 P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL); 174 } 175 } 176 177 // Page(Expr) is the page address of the expression Expr, defined 178 // as (Expr & ~0xFFF). (This applies even if the machine page size 179 // supported by the platform has a different value.) 180 static uint64_t getAArch64Page(uint64_t Expr) { 181 return Expr & (~static_cast<uint64_t>(0xFFF)); 182 } 183 184 template <class ELFT> 185 static typename ELFT::uint getSymVA(uint32_t Type, typename ELFT::uint A, 186 typename ELFT::uint P, 187 const SymbolBody &Body, RelExpr Expr) { 188 switch (Expr) { 189 case R_HINT: 190 llvm_unreachable("cannot relocate hint relocs"); 191 case R_TLSLD: 192 return Out<ELFT>::Got->getTlsIndexOff() + A - Out<ELFT>::Got->getSize(); 193 case R_TLSLD_PC: 194 return Out<ELFT>::Got->getTlsIndexVA() + A - P; 195 case R_THUNK_ABS: 196 return Body.getThunkVA<ELFT>() + A; 197 case R_THUNK_PC: 198 case R_THUNK_PLT_PC: 199 return Body.getThunkVA<ELFT>() + A - P; 200 case R_PPC_TOC: 201 return getPPC64TocBase() + A; 202 case R_TLSGD: 203 return Out<ELFT>::Got->getGlobalDynOffset(Body) + A - 204 Out<ELFT>::Got->getSize(); 205 case R_TLSGD_PC: 206 return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P; 207 case R_TLSDESC: 208 return Out<ELFT>::Got->getGlobalDynAddr(Body) + A; 209 case R_TLSDESC_PAGE: 210 return getAArch64Page(Out<ELFT>::Got->getGlobalDynAddr(Body) + A) - 211 getAArch64Page(P); 212 case R_PLT: 213 return Body.getPltVA<ELFT>() + A; 214 case R_PLT_PC: 215 case R_PPC_PLT_OPD: 216 return Body.getPltVA<ELFT>() + A - P; 217 case R_SIZE: 218 return Body.getSize<ELFT>() + A; 219 case R_GOTREL: 220 return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA(); 221 case R_GOTREL_FROM_END: 222 return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA() - 223 Out<ELFT>::Got->getSize(); 224 case R_RELAX_TLS_GD_TO_IE_END: 225 case R_GOT_FROM_END: 226 return Body.getGotOffset<ELFT>() + A - Out<ELFT>::Got->getSize(); 227 case R_RELAX_TLS_GD_TO_IE_ABS: 228 case R_GOT: 229 return Body.getGotVA<ELFT>() + A; 230 case R_RELAX_TLS_GD_TO_IE_PAGE_PC: 231 case R_GOT_PAGE_PC: 232 return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P); 233 case R_RELAX_TLS_GD_TO_IE: 234 case R_GOT_PC: 235 return Body.getGotVA<ELFT>() + A - P; 236 case R_GOTONLY_PC: 237 return Out<ELFT>::Got->getVA() + A - P; 238 case R_GOTONLY_PC_FROM_END: 239 return Out<ELFT>::Got->getVA() + A - P + Out<ELFT>::Got->getSize(); 240 case R_RELAX_TLS_LD_TO_LE: 241 case R_RELAX_TLS_IE_TO_LE: 242 case R_RELAX_TLS_GD_TO_LE: 243 case R_TLS: 244 // A weak undefined TLS symbol resolves to the base of the TLS 245 // block, i.e. gets a value of zero. If we pass --gc-sections to 246 // lld and .tbss is not referenced, it gets reclaimed and we don't 247 // create a TLS program header. Therefore, we resolve this 248 // statically to zero. 249 if (Body.isTls() && (Body.isLazy() || Body.isUndefined()) && 250 Body.symbol()->isWeak()) 251 return 0; 252 if (Target->TcbSize) 253 return Body.getVA<ELFT>(A) + 254 alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align); 255 return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz; 256 case R_RELAX_TLS_GD_TO_LE_NEG: 257 case R_NEG_TLS: 258 return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A); 259 case R_ABS: 260 case R_RELAX_GOT_PC_NOPIC: 261 return Body.getVA<ELFT>(A); 262 case R_GOT_OFF: 263 return Body.getGotOffset<ELFT>() + A; 264 case R_MIPS_GOT_LOCAL_PAGE: 265 // If relocation against MIPS local symbol requires GOT entry, this entry 266 // should be initialized by 'page address'. This address is high 16-bits 267 // of sum the symbol's value and the addend. 268 return Out<ELFT>::Got->getMipsLocalPageOffset(Body.getVA<ELFT>(A)); 269 case R_MIPS_GOT_OFF: 270 // In case of MIPS if a GOT relocation has non-zero addend this addend 271 // should be applied to the GOT entry content not to the GOT entry offset. 272 // That is why we use separate expression type. 273 return Out<ELFT>::Got->getMipsGotOffset(Body, A); 274 case R_MIPS_TLSGD: 275 return Out<ELFT>::Got->getGlobalDynOffset(Body) + 276 Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset; 277 case R_MIPS_TLSLD: 278 return Out<ELFT>::Got->getTlsIndexOff() + 279 Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset; 280 case R_PPC_OPD: { 281 uint64_t SymVA = Body.getVA<ELFT>(A); 282 // If we have an undefined weak symbol, we might get here with a symbol 283 // address of zero. That could overflow, but the code must be unreachable, 284 // so don't bother doing anything at all. 285 if (!SymVA) 286 return 0; 287 if (Out<ELF64BE>::Opd) { 288 // If this is a local call, and we currently have the address of a 289 // function-descriptor, get the underlying code address instead. 290 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); 291 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); 292 bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd; 293 if (InOpd) 294 SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]); 295 } 296 return SymVA - P; 297 } 298 case R_PC: 299 case R_RELAX_GOT_PC: 300 return Body.getVA<ELFT>(A) - P; 301 case R_PLT_PAGE_PC: 302 case R_PAGE_PC: 303 return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P); 304 } 305 llvm_unreachable("Invalid expression"); 306 } 307 308 // This function applies relocations to sections without SHF_ALLOC bit. 309 // Such sections are never mapped to memory at runtime. Debug sections are 310 // an example. Relocations in non-alloc sections are much easier to 311 // handle than in allocated sections because it will never need complex 312 // treatement such as GOT or PLT (because at runtime no one refers them). 313 // So, we handle relocations for non-alloc sections directly in this 314 // function as a performance optimization. 315 template <class ELFT> 316 template <class RelTy> 317 void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) { 318 const unsigned Bits = sizeof(uintX_t) * 8; 319 for (const RelTy &Rel : Rels) { 320 uint32_t Type = Rel.getType(Config->Mips64EL); 321 uintX_t Offset = this->getOffset(Rel.r_offset); 322 uint8_t *BufLoc = Buf + Offset; 323 uintX_t Addend = getAddend<ELFT>(Rel); 324 if (!RelTy::IsRela) 325 Addend += Target->getImplicitAddend(BufLoc, Type); 326 327 SymbolBody &Sym = this->File->getRelocTargetSym(Rel); 328 if (Target->getRelExpr(Type, Sym) != R_ABS) { 329 error(getName(this) + " has non-ABS reloc"); 330 return; 331 } 332 333 uintX_t AddrLoc = this->OutSec->getVA() + Offset; 334 uint64_t SymVA = 335 SignExtend64<Bits>(getSymVA<ELFT>(Type, Addend, AddrLoc, Sym, R_ABS)); 336 Target->relocateOne(BufLoc, Type, SymVA); 337 } 338 } 339 340 template <class ELFT> 341 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) { 342 // scanReloc function in Writer.cpp constructs Relocations 343 // vector only for SHF_ALLOC'ed sections. For other sections, 344 // we handle relocations directly here. 345 auto *IS = dyn_cast<InputSection<ELFT>>(this); 346 if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) { 347 for (const Elf_Shdr *RelSec : IS->RelocSections) { 348 if (RelSec->sh_type == SHT_RELA) 349 IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec)); 350 else 351 IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec)); 352 } 353 return; 354 } 355 356 const unsigned Bits = sizeof(uintX_t) * 8; 357 for (const Relocation &Rel : Relocations) { 358 uintX_t Offset = getOffset(Rel.Offset); 359 uint8_t *BufLoc = Buf + Offset; 360 uint32_t Type = Rel.Type; 361 uintX_t A = Rel.Addend; 362 363 uintX_t AddrLoc = OutSec->getVA() + Offset; 364 RelExpr Expr = Rel.Expr; 365 uint64_t SymVA = 366 SignExtend64<Bits>(getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, Expr)); 367 368 switch (Expr) { 369 case R_RELAX_GOT_PC: 370 case R_RELAX_GOT_PC_NOPIC: 371 Target->relaxGot(BufLoc, SymVA); 372 break; 373 case R_RELAX_TLS_IE_TO_LE: 374 Target->relaxTlsIeToLe(BufLoc, Type, SymVA); 375 break; 376 case R_RELAX_TLS_LD_TO_LE: 377 Target->relaxTlsLdToLe(BufLoc, Type, SymVA); 378 break; 379 case R_RELAX_TLS_GD_TO_LE: 380 case R_RELAX_TLS_GD_TO_LE_NEG: 381 Target->relaxTlsGdToLe(BufLoc, Type, SymVA); 382 break; 383 case R_RELAX_TLS_GD_TO_IE: 384 case R_RELAX_TLS_GD_TO_IE_ABS: 385 case R_RELAX_TLS_GD_TO_IE_PAGE_PC: 386 case R_RELAX_TLS_GD_TO_IE_END: 387 Target->relaxTlsGdToIe(BufLoc, Type, SymVA); 388 break; 389 case R_PPC_PLT_OPD: 390 // Patch a nop (0x60000000) to a ld. 391 if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000) 392 write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1) 393 // fallthrough 394 default: 395 Target->relocateOne(BufLoc, Type, SymVA); 396 break; 397 } 398 } 399 } 400 401 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) { 402 if (this->Header->sh_type == SHT_NOBITS) 403 return; 404 ELFFile<ELFT> &EObj = this->File->getObj(); 405 406 // If -r is given, then an InputSection may be a relocation section. 407 if (this->Header->sh_type == SHT_RELA) { 408 copyRelocations(Buf + OutSecOff, EObj.relas(this->Header)); 409 return; 410 } 411 if (this->Header->sh_type == SHT_REL) { 412 copyRelocations(Buf + OutSecOff, EObj.rels(this->Header)); 413 return; 414 } 415 416 // Copy section contents from source object file to output file. 417 ArrayRef<uint8_t> Data = this->Data; 418 memcpy(Buf + OutSecOff, Data.data(), Data.size()); 419 420 // Iterate over all relocation sections that apply to this section. 421 uint8_t *BufEnd = Buf + OutSecOff + Data.size(); 422 this->relocate(Buf, BufEnd); 423 424 // The section might have a data/code generated by the linker and need 425 // to be written after the section. Usually these are thunks - small piece 426 // of code used to jump between "incompatible" functions like PIC and non-PIC 427 // or if the jump target too far and its address does not fit to the short 428 // jump istruction. 429 if (!Thunks.empty()) { 430 Buf += OutSecOff + getThunkOff(); 431 for (const Thunk<ELFT> *T : Thunks) { 432 T->writeTo(Buf); 433 Buf += T->size(); 434 } 435 } 436 } 437 438 template <class ELFT> 439 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) { 440 assert(Other->Alignment <= this->Alignment); 441 Other->Repl = this->Repl; 442 Other->Live = false; 443 } 444 445 template <class ELFT> 446 EhInputSection<ELFT>::EhInputSection(elf::ObjectFile<ELFT> *F, 447 const Elf_Shdr *Header, StringRef Name) 448 : InputSectionBase<ELFT>(F, Header, Name, InputSectionBase<ELFT>::EHFrame) { 449 // Mark .eh_frame sections as live by default because there are 450 // usually no relocations that point to .eh_frames. Otherwise, 451 // the garbage collector would drop all .eh_frame sections. 452 this->Live = true; 453 } 454 455 template <class ELFT> 456 bool EhInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 457 return S->kind() == InputSectionBase<ELFT>::EHFrame; 458 } 459 460 // Returns the index of the first relocation that points to a region between 461 // Begin and Begin+Size. 462 template <class IntTy, class RelTy> 463 static unsigned getReloc(IntTy Begin, IntTy Size, const ArrayRef<RelTy> &Rels, 464 unsigned &RelocI) { 465 // Start search from RelocI for fast access. That works because the 466 // relocations are sorted in .eh_frame. 467 for (unsigned N = Rels.size(); RelocI < N; ++RelocI) { 468 const RelTy &Rel = Rels[RelocI]; 469 if (Rel.r_offset < Begin) 470 continue; 471 472 if (Rel.r_offset < Begin + Size) 473 return RelocI; 474 return -1; 475 } 476 return -1; 477 } 478 479 // .eh_frame is a sequence of CIE or FDE records. 480 // This function splits an input section into records and returns them. 481 template <class ELFT> 482 void EhInputSection<ELFT>::split() { 483 // Early exit if already split. 484 if (!this->Pieces.empty()) 485 return; 486 487 if (RelocSection) { 488 ELFFile<ELFT> &Obj = this->File->getObj(); 489 if (RelocSection->sh_type == SHT_RELA) 490 split(Obj.relas(RelocSection)); 491 else 492 split(Obj.rels(RelocSection)); 493 return; 494 } 495 split(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr)); 496 } 497 498 template <class ELFT> 499 template <class RelTy> 500 void EhInputSection<ELFT>::split(ArrayRef<RelTy> Rels) { 501 ArrayRef<uint8_t> Data = this->Data; 502 unsigned RelI = 0; 503 for (size_t Off = 0, End = Data.size(); Off != End;) { 504 size_t Size = readEhRecordSize<ELFT>(Data.slice(Off)); 505 this->Pieces.emplace_back(Off, Data.slice(Off, Size), 506 getReloc(Off, Size, Rels, RelI)); 507 // The empty record is the end marker. 508 if (Size == 4) 509 break; 510 Off += Size; 511 } 512 } 513 514 static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) { 515 // Optimize the common case. 516 StringRef S((const char *)A.data(), A.size()); 517 if (EntSize == 1) 518 return S.find(0); 519 520 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) { 521 const char *B = S.begin() + I; 522 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; })) 523 return I; 524 } 525 return StringRef::npos; 526 } 527 528 // Split SHF_STRINGS section. Such section is a sequence of 529 // null-terminated strings. 530 template <class ELFT> 531 std::vector<SectionPiece> 532 MergeInputSection<ELFT>::splitStrings(ArrayRef<uint8_t> Data, size_t EntSize) { 533 std::vector<SectionPiece> V; 534 size_t Off = 0; 535 while (!Data.empty()) { 536 size_t End = findNull(Data, EntSize); 537 if (End == StringRef::npos) 538 fatal(getName(this) + ": string is not null terminated"); 539 size_t Size = End + EntSize; 540 V.emplace_back(Off, Data.slice(0, Size)); 541 Data = Data.slice(Size); 542 Off += Size; 543 } 544 return V; 545 } 546 547 // Split non-SHF_STRINGS section. Such section is a sequence of 548 // fixed size records. 549 template <class ELFT> 550 std::vector<SectionPiece> 551 MergeInputSection<ELFT>::splitNonStrings(ArrayRef<uint8_t> Data, 552 size_t EntSize) { 553 std::vector<SectionPiece> V; 554 size_t Size = Data.size(); 555 assert((Size % EntSize) == 0); 556 for (unsigned I = 0, N = Size; I != N; I += EntSize) 557 V.emplace_back(I, Data.slice(I, EntSize)); 558 return V; 559 } 560 561 template <class ELFT> 562 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F, 563 const Elf_Shdr *Header, 564 StringRef Name) 565 : InputSectionBase<ELFT>(F, Header, Name, InputSectionBase<ELFT>::Merge) {} 566 567 template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() { 568 ArrayRef<uint8_t> Data = this->Data; 569 uintX_t EntSize = this->Header->sh_entsize; 570 if (this->Header->sh_flags & SHF_STRINGS) 571 this->Pieces = splitStrings(Data, EntSize); 572 else 573 this->Pieces = splitNonStrings(Data, EntSize); 574 575 if (Config->GcSections) 576 for (uintX_t Off : LiveOffsets) 577 this->getSectionPiece(Off)->Live = true; 578 } 579 580 template <class ELFT> 581 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 582 return S->kind() == InputSectionBase<ELFT>::Merge; 583 } 584 585 // Do binary search to get a section piece at a given input offset. 586 template <class ELFT> 587 SectionPiece *MergeInputSection<ELFT>::getSectionPiece(uintX_t Offset) { 588 auto *This = static_cast<const MergeInputSection<ELFT> *>(this); 589 return const_cast<SectionPiece *>(This->getSectionPiece(Offset)); 590 } 591 592 template <class ELFT> 593 const SectionPiece * 594 MergeInputSection<ELFT>::getSectionPiece(uintX_t Offset) const { 595 uintX_t Size = this->Data.size(); 596 if (Offset >= Size) 597 fatal(getName(this) + ": entry is past the end of the section"); 598 599 // Find the element this offset points to. 600 auto I = std::upper_bound( 601 Pieces.begin(), Pieces.end(), Offset, 602 [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; }); 603 --I; 604 return &*I; 605 } 606 607 // Returns the offset in an output section for a given input offset. 608 // Because contents of a mergeable section is not contiguous in output, 609 // it is not just an addition to a base output offset. 610 template <class ELFT> 611 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) const { 612 auto It = OffsetMap.find(Offset); 613 if (It != OffsetMap.end()) 614 return It->second; 615 616 // If Offset is not at beginning of a section piece, it is not in the map. 617 // In that case we need to search from the original section piece vector. 618 const SectionPiece &Piece = *this->getSectionPiece(Offset); 619 assert(Piece.Live); 620 uintX_t Addend = Offset - Piece.InputOff; 621 return Piece.OutputOff + Addend; 622 } 623 624 // Create a map from input offsets to output offsets for all section pieces. 625 // It is called after finalize(). 626 template <class ELFT> void MergeInputSection<ELFT>::finalizePieces() { 627 OffsetMap.grow(this->Pieces.size()); 628 for (SectionPiece &Piece : this->Pieces) { 629 if (!Piece.Live) 630 continue; 631 if (Piece.OutputOff == size_t(-1)) { 632 // Offsets of tail-merged strings are computed lazily. 633 auto *OutSec = static_cast<MergeOutputSection<ELFT> *>(this->OutSec); 634 ArrayRef<uint8_t> D = Piece.data(); 635 StringRef S((const char *)D.data(), D.size()); 636 Piece.OutputOff = OutSec->getOffset(S); 637 } 638 OffsetMap[Piece.InputOff] = Piece.OutputOff; 639 } 640 } 641 642 template <class ELFT> 643 MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F, 644 const Elf_Shdr *Hdr, 645 StringRef Name) 646 : InputSectionBase<ELFT>(F, Hdr, Name, 647 InputSectionBase<ELFT>::MipsReginfo) { 648 ArrayRef<uint8_t> Data = this->Data; 649 // Initialize this->Reginfo. 650 if (Data.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) { 651 error(getName(this) + ": invalid size of .reginfo section"); 652 return; 653 } 654 Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(Data.data()); 655 } 656 657 template <class ELFT> 658 bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 659 return S->kind() == InputSectionBase<ELFT>::MipsReginfo; 660 } 661 662 template <class ELFT> 663 MipsOptionsInputSection<ELFT>::MipsOptionsInputSection(elf::ObjectFile<ELFT> *F, 664 const Elf_Shdr *Hdr, 665 StringRef Name) 666 : InputSectionBase<ELFT>(F, Hdr, Name, 667 InputSectionBase<ELFT>::MipsOptions) { 668 // Find ODK_REGINFO option in the section's content. 669 ArrayRef<uint8_t> D = this->Data; 670 while (!D.empty()) { 671 if (D.size() < sizeof(Elf_Mips_Options<ELFT>)) { 672 error(getName(this) + ": invalid size of .MIPS.options section"); 673 break; 674 } 675 auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(D.data()); 676 if (O->kind == ODK_REGINFO) { 677 Reginfo = &O->getRegInfo(); 678 break; 679 } 680 D = D.slice(O->size); 681 } 682 } 683 684 template <class ELFT> 685 bool MipsOptionsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 686 return S->kind() == InputSectionBase<ELFT>::MipsOptions; 687 } 688 689 template <class ELFT> 690 MipsAbiFlagsInputSection<ELFT>::MipsAbiFlagsInputSection( 691 elf::ObjectFile<ELFT> *F, const Elf_Shdr *Hdr, StringRef Name) 692 : InputSectionBase<ELFT>(F, Hdr, Name, 693 InputSectionBase<ELFT>::MipsAbiFlags) { 694 // Initialize this->Flags. 695 ArrayRef<uint8_t> Data = this->Data; 696 if (Data.size() != sizeof(Elf_Mips_ABIFlags<ELFT>)) { 697 error("invalid size of .MIPS.abiflags section"); 698 return; 699 } 700 Flags = reinterpret_cast<const Elf_Mips_ABIFlags<ELFT> *>(Data.data()); 701 } 702 703 template <class ELFT> 704 bool MipsAbiFlagsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 705 return S->kind() == InputSectionBase<ELFT>::MipsAbiFlags; 706 } 707 708 template <class ELFT> 709 CommonInputSection<ELFT>::CommonInputSection(std::vector<DefinedCommon *> Syms) 710 : InputSection<ELFT>(nullptr, &Hdr, "") { 711 Hdr.sh_size = 0; 712 Hdr.sh_type = SHT_NOBITS; 713 Hdr.sh_flags = SHF_ALLOC | SHF_WRITE; 714 this->Live = true; 715 716 // Sort the common symbols by alignment as an heuristic to pack them better. 717 std::stable_sort(Syms.begin(), Syms.end(), 718 [](const DefinedCommon *A, const DefinedCommon *B) { 719 return A->Alignment > B->Alignment; 720 }); 721 722 for (DefinedCommon *Sym : Syms) { 723 this->Alignment = std::max<uintX_t>(this->Alignment, Sym->Alignment); 724 Hdr.sh_size = alignTo(Hdr.sh_size, Sym->Alignment); 725 726 // Compute symbol offset relative to beginning of input section. 727 Sym->Offset = Hdr.sh_size; 728 Hdr.sh_size += Sym->Size; 729 } 730 } 731 732 template class elf::InputSectionBase<ELF32LE>; 733 template class elf::InputSectionBase<ELF32BE>; 734 template class elf::InputSectionBase<ELF64LE>; 735 template class elf::InputSectionBase<ELF64BE>; 736 737 template class elf::InputSection<ELF32LE>; 738 template class elf::InputSection<ELF32BE>; 739 template class elf::InputSection<ELF64LE>; 740 template class elf::InputSection<ELF64BE>; 741 742 template class elf::EhInputSection<ELF32LE>; 743 template class elf::EhInputSection<ELF32BE>; 744 template class elf::EhInputSection<ELF64LE>; 745 template class elf::EhInputSection<ELF64BE>; 746 747 template class elf::MergeInputSection<ELF32LE>; 748 template class elf::MergeInputSection<ELF32BE>; 749 template class elf::MergeInputSection<ELF64LE>; 750 template class elf::MergeInputSection<ELF64BE>; 751 752 template class elf::MipsReginfoInputSection<ELF32LE>; 753 template class elf::MipsReginfoInputSection<ELF32BE>; 754 template class elf::MipsReginfoInputSection<ELF64LE>; 755 template class elf::MipsReginfoInputSection<ELF64BE>; 756 757 template class elf::MipsOptionsInputSection<ELF32LE>; 758 template class elf::MipsOptionsInputSection<ELF32BE>; 759 template class elf::MipsOptionsInputSection<ELF64LE>; 760 template class elf::MipsOptionsInputSection<ELF64BE>; 761 762 template class elf::MipsAbiFlagsInputSection<ELF32LE>; 763 template class elf::MipsAbiFlagsInputSection<ELF32BE>; 764 template class elf::MipsAbiFlagsInputSection<ELF64LE>; 765 template class elf::MipsAbiFlagsInputSection<ELF64BE>; 766 767 template class elf::CommonInputSection<ELF32LE>; 768 template class elf::CommonInputSection<ELF32BE>; 769 template class elf::CommonInputSection<ELF64LE>; 770 template class elf::CommonInputSection<ELF64BE>; 771