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