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