1 //===- ELFObject.cpp ------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ELFObject.h" 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/ADT/Twine.h" 14 #include "llvm/ADT/iterator_range.h" 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/MC/MCTargetOptions.h" 17 #include "llvm/Object/ELF.h" 18 #include "llvm/Object/ELFObjectFile.h" 19 #include "llvm/Support/Compression.h" 20 #include "llvm/Support/Endian.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/FileOutputBuffer.h" 23 #include "llvm/Support/Path.h" 24 #include <algorithm> 25 #include <cstddef> 26 #include <cstdint> 27 #include <iterator> 28 #include <unordered_set> 29 #include <utility> 30 #include <vector> 31 32 using namespace llvm; 33 using namespace llvm::ELF; 34 using namespace llvm::objcopy::elf; 35 using namespace llvm::object; 36 37 template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) { 38 uint8_t *B = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + 39 Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr); 40 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B); 41 Phdr.p_type = Seg.Type; 42 Phdr.p_flags = Seg.Flags; 43 Phdr.p_offset = Seg.Offset; 44 Phdr.p_vaddr = Seg.VAddr; 45 Phdr.p_paddr = Seg.PAddr; 46 Phdr.p_filesz = Seg.FileSize; 47 Phdr.p_memsz = Seg.MemSize; 48 Phdr.p_align = Seg.Align; 49 } 50 51 Error SectionBase::removeSectionReferences( 52 bool, function_ref<bool(const SectionBase *)>) { 53 return Error::success(); 54 } 55 56 Error SectionBase::removeSymbols(function_ref<bool(const Symbol &)>) { 57 return Error::success(); 58 } 59 60 Error SectionBase::initialize(SectionTableRef) { return Error::success(); } 61 void SectionBase::finalize() {} 62 void SectionBase::markSymbols() {} 63 void SectionBase::replaceSectionReferences( 64 const DenseMap<SectionBase *, SectionBase *> &) {} 65 void SectionBase::onRemove() {} 66 67 template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) { 68 uint8_t *B = 69 reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + Sec.HeaderOffset; 70 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B); 71 Shdr.sh_name = Sec.NameIndex; 72 Shdr.sh_type = Sec.Type; 73 Shdr.sh_flags = Sec.Flags; 74 Shdr.sh_addr = Sec.Addr; 75 Shdr.sh_offset = Sec.Offset; 76 Shdr.sh_size = Sec.Size; 77 Shdr.sh_link = Sec.Link; 78 Shdr.sh_info = Sec.Info; 79 Shdr.sh_addralign = Sec.Align; 80 Shdr.sh_entsize = Sec.EntrySize; 81 } 82 83 template <class ELFT> Error ELFSectionSizer<ELFT>::visit(Section &) { 84 return Error::success(); 85 } 86 87 template <class ELFT> Error ELFSectionSizer<ELFT>::visit(OwnedDataSection &) { 88 return Error::success(); 89 } 90 91 template <class ELFT> Error ELFSectionSizer<ELFT>::visit(StringTableSection &) { 92 return Error::success(); 93 } 94 95 template <class ELFT> 96 Error ELFSectionSizer<ELFT>::visit(DynamicRelocationSection &) { 97 return Error::success(); 98 } 99 100 template <class ELFT> 101 Error ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) { 102 Sec.EntrySize = sizeof(Elf_Sym); 103 Sec.Size = Sec.Symbols.size() * Sec.EntrySize; 104 // Align to the largest field in Elf_Sym. 105 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word); 106 return Error::success(); 107 } 108 109 template <class ELFT> 110 Error ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) { 111 Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela); 112 Sec.Size = Sec.Relocations.size() * Sec.EntrySize; 113 // Align to the largest field in Elf_Rel(a). 114 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word); 115 return Error::success(); 116 } 117 118 template <class ELFT> 119 Error ELFSectionSizer<ELFT>::visit(GnuDebugLinkSection &) { 120 return Error::success(); 121 } 122 123 template <class ELFT> Error ELFSectionSizer<ELFT>::visit(GroupSection &Sec) { 124 Sec.Size = sizeof(Elf_Word) + Sec.GroupMembers.size() * sizeof(Elf_Word); 125 return Error::success(); 126 } 127 128 template <class ELFT> 129 Error ELFSectionSizer<ELFT>::visit(SectionIndexSection &) { 130 return Error::success(); 131 } 132 133 template <class ELFT> Error ELFSectionSizer<ELFT>::visit(CompressedSection &) { 134 return Error::success(); 135 } 136 137 template <class ELFT> 138 Error ELFSectionSizer<ELFT>::visit(DecompressedSection &) { 139 return Error::success(); 140 } 141 142 Error BinarySectionWriter::visit(const SectionIndexSection &Sec) { 143 return createStringError(errc::operation_not_permitted, 144 "cannot write symbol section index table '" + 145 Sec.Name + "' "); 146 } 147 148 Error BinarySectionWriter::visit(const SymbolTableSection &Sec) { 149 return createStringError(errc::operation_not_permitted, 150 "cannot write symbol table '" + Sec.Name + 151 "' out to binary"); 152 } 153 154 Error BinarySectionWriter::visit(const RelocationSection &Sec) { 155 return createStringError(errc::operation_not_permitted, 156 "cannot write relocation section '" + Sec.Name + 157 "' out to binary"); 158 } 159 160 Error BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) { 161 return createStringError(errc::operation_not_permitted, 162 "cannot write '" + Sec.Name + "' out to binary"); 163 } 164 165 Error BinarySectionWriter::visit(const GroupSection &Sec) { 166 return createStringError(errc::operation_not_permitted, 167 "cannot write '" + Sec.Name + "' out to binary"); 168 } 169 170 Error SectionWriter::visit(const Section &Sec) { 171 if (Sec.Type != SHT_NOBITS) 172 llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset); 173 174 return Error::success(); 175 } 176 177 static bool addressOverflows32bit(uint64_t Addr) { 178 // Sign extended 32 bit addresses (e.g 0xFFFFFFFF80000000) are ok 179 return Addr > UINT32_MAX && Addr + 0x80000000 > UINT32_MAX; 180 } 181 182 template <class T> static T checkedGetHex(StringRef S) { 183 T Value; 184 bool Fail = S.getAsInteger(16, Value); 185 assert(!Fail); 186 (void)Fail; 187 return Value; 188 } 189 190 // Fills exactly Len bytes of buffer with hexadecimal characters 191 // representing value 'X' 192 template <class T, class Iterator> 193 static Iterator toHexStr(T X, Iterator It, size_t Len) { 194 // Fill range with '0' 195 std::fill(It, It + Len, '0'); 196 197 for (long I = Len - 1; I >= 0; --I) { 198 unsigned char Mod = static_cast<unsigned char>(X) & 15; 199 *(It + I) = hexdigit(Mod, false); 200 X >>= 4; 201 } 202 assert(X == 0); 203 return It + Len; 204 } 205 206 uint8_t IHexRecord::getChecksum(StringRef S) { 207 assert((S.size() & 1) == 0); 208 uint8_t Checksum = 0; 209 while (!S.empty()) { 210 Checksum += checkedGetHex<uint8_t>(S.take_front(2)); 211 S = S.drop_front(2); 212 } 213 return -Checksum; 214 } 215 216 IHexLineData IHexRecord::getLine(uint8_t Type, uint16_t Addr, 217 ArrayRef<uint8_t> Data) { 218 IHexLineData Line(getLineLength(Data.size())); 219 assert(Line.size()); 220 auto Iter = Line.begin(); 221 *Iter++ = ':'; 222 Iter = toHexStr(Data.size(), Iter, 2); 223 Iter = toHexStr(Addr, Iter, 4); 224 Iter = toHexStr(Type, Iter, 2); 225 for (uint8_t X : Data) 226 Iter = toHexStr(X, Iter, 2); 227 StringRef S(Line.data() + 1, std::distance(Line.begin() + 1, Iter)); 228 Iter = toHexStr(getChecksum(S), Iter, 2); 229 *Iter++ = '\r'; 230 *Iter++ = '\n'; 231 assert(Iter == Line.end()); 232 return Line; 233 } 234 235 static Error checkRecord(const IHexRecord &R) { 236 switch (R.Type) { 237 case IHexRecord::Data: 238 if (R.HexData.size() == 0) 239 return createStringError( 240 errc::invalid_argument, 241 "zero data length is not allowed for data records"); 242 break; 243 case IHexRecord::EndOfFile: 244 break; 245 case IHexRecord::SegmentAddr: 246 // 20-bit segment address. Data length must be 2 bytes 247 // (4 bytes in hex) 248 if (R.HexData.size() != 4) 249 return createStringError( 250 errc::invalid_argument, 251 "segment address data should be 2 bytes in size"); 252 break; 253 case IHexRecord::StartAddr80x86: 254 case IHexRecord::StartAddr: 255 if (R.HexData.size() != 8) 256 return createStringError(errc::invalid_argument, 257 "start address data should be 4 bytes in size"); 258 // According to Intel HEX specification '03' record 259 // only specifies the code address within the 20-bit 260 // segmented address space of the 8086/80186. This 261 // means 12 high order bits should be zeroes. 262 if (R.Type == IHexRecord::StartAddr80x86 && 263 R.HexData.take_front(3) != "000") 264 return createStringError(errc::invalid_argument, 265 "start address exceeds 20 bit for 80x86"); 266 break; 267 case IHexRecord::ExtendedAddr: 268 // 16-31 bits of linear base address 269 if (R.HexData.size() != 4) 270 return createStringError( 271 errc::invalid_argument, 272 "extended address data should be 2 bytes in size"); 273 break; 274 default: 275 // Unknown record type 276 return createStringError(errc::invalid_argument, "unknown record type: %u", 277 static_cast<unsigned>(R.Type)); 278 } 279 return Error::success(); 280 } 281 282 // Checks that IHEX line contains valid characters. 283 // This allows converting hexadecimal data to integers 284 // without extra verification. 285 static Error checkChars(StringRef Line) { 286 assert(!Line.empty()); 287 if (Line[0] != ':') 288 return createStringError(errc::invalid_argument, 289 "missing ':' in the beginning of line."); 290 291 for (size_t Pos = 1; Pos < Line.size(); ++Pos) 292 if (hexDigitValue(Line[Pos]) == -1U) 293 return createStringError(errc::invalid_argument, 294 "invalid character at position %zu.", Pos + 1); 295 return Error::success(); 296 } 297 298 Expected<IHexRecord> IHexRecord::parse(StringRef Line) { 299 assert(!Line.empty()); 300 301 // ':' + Length + Address + Type + Checksum with empty data ':LLAAAATTCC' 302 if (Line.size() < 11) 303 return createStringError(errc::invalid_argument, 304 "line is too short: %zu chars.", Line.size()); 305 306 if (Error E = checkChars(Line)) 307 return std::move(E); 308 309 IHexRecord Rec; 310 size_t DataLen = checkedGetHex<uint8_t>(Line.substr(1, 2)); 311 if (Line.size() != getLength(DataLen)) 312 return createStringError(errc::invalid_argument, 313 "invalid line length %zu (should be %zu)", 314 Line.size(), getLength(DataLen)); 315 316 Rec.Addr = checkedGetHex<uint16_t>(Line.substr(3, 4)); 317 Rec.Type = checkedGetHex<uint8_t>(Line.substr(7, 2)); 318 Rec.HexData = Line.substr(9, DataLen * 2); 319 320 if (getChecksum(Line.drop_front(1)) != 0) 321 return createStringError(errc::invalid_argument, "incorrect checksum."); 322 if (Error E = checkRecord(Rec)) 323 return std::move(E); 324 return Rec; 325 } 326 327 static uint64_t sectionPhysicalAddr(const SectionBase *Sec) { 328 Segment *Seg = Sec->ParentSegment; 329 if (Seg && Seg->Type != ELF::PT_LOAD) 330 Seg = nullptr; 331 return Seg ? Seg->PAddr + Sec->OriginalOffset - Seg->OriginalOffset 332 : Sec->Addr; 333 } 334 335 void IHexSectionWriterBase::writeSection(const SectionBase *Sec, 336 ArrayRef<uint8_t> Data) { 337 assert(Data.size() == Sec->Size); 338 const uint32_t ChunkSize = 16; 339 uint32_t Addr = sectionPhysicalAddr(Sec) & 0xFFFFFFFFU; 340 while (!Data.empty()) { 341 uint64_t DataSize = std::min<uint64_t>(Data.size(), ChunkSize); 342 if (Addr > SegmentAddr + BaseAddr + 0xFFFFU) { 343 if (Addr > 0xFFFFFU) { 344 // Write extended address record, zeroing segment address 345 // if needed. 346 if (SegmentAddr != 0) 347 SegmentAddr = writeSegmentAddr(0U); 348 BaseAddr = writeBaseAddr(Addr); 349 } else { 350 // We can still remain 16-bit 351 SegmentAddr = writeSegmentAddr(Addr); 352 } 353 } 354 uint64_t SegOffset = Addr - BaseAddr - SegmentAddr; 355 assert(SegOffset <= 0xFFFFU); 356 DataSize = std::min(DataSize, 0x10000U - SegOffset); 357 writeData(0, SegOffset, Data.take_front(DataSize)); 358 Addr += DataSize; 359 Data = Data.drop_front(DataSize); 360 } 361 } 362 363 uint64_t IHexSectionWriterBase::writeSegmentAddr(uint64_t Addr) { 364 assert(Addr <= 0xFFFFFU); 365 uint8_t Data[] = {static_cast<uint8_t>((Addr & 0xF0000U) >> 12), 0}; 366 writeData(2, 0, Data); 367 return Addr & 0xF0000U; 368 } 369 370 uint64_t IHexSectionWriterBase::writeBaseAddr(uint64_t Addr) { 371 assert(Addr <= 0xFFFFFFFFU); 372 uint64_t Base = Addr & 0xFFFF0000U; 373 uint8_t Data[] = {static_cast<uint8_t>(Base >> 24), 374 static_cast<uint8_t>((Base >> 16) & 0xFF)}; 375 writeData(4, 0, Data); 376 return Base; 377 } 378 379 void IHexSectionWriterBase::writeData(uint8_t, uint16_t, 380 ArrayRef<uint8_t> Data) { 381 Offset += IHexRecord::getLineLength(Data.size()); 382 } 383 384 Error IHexSectionWriterBase::visit(const Section &Sec) { 385 writeSection(&Sec, Sec.Contents); 386 return Error::success(); 387 } 388 389 Error IHexSectionWriterBase::visit(const OwnedDataSection &Sec) { 390 writeSection(&Sec, Sec.Data); 391 return Error::success(); 392 } 393 394 Error IHexSectionWriterBase::visit(const StringTableSection &Sec) { 395 // Check that sizer has already done its work 396 assert(Sec.Size == Sec.StrTabBuilder.getSize()); 397 // We are free to pass an invalid pointer to writeSection as long 398 // as we don't actually write any data. The real writer class has 399 // to override this method . 400 writeSection(&Sec, {nullptr, static_cast<size_t>(Sec.Size)}); 401 return Error::success(); 402 } 403 404 Error IHexSectionWriterBase::visit(const DynamicRelocationSection &Sec) { 405 writeSection(&Sec, Sec.Contents); 406 return Error::success(); 407 } 408 409 void IHexSectionWriter::writeData(uint8_t Type, uint16_t Addr, 410 ArrayRef<uint8_t> Data) { 411 IHexLineData HexData = IHexRecord::getLine(Type, Addr, Data); 412 memcpy(Out.getBufferStart() + Offset, HexData.data(), HexData.size()); 413 Offset += HexData.size(); 414 } 415 416 Error IHexSectionWriter::visit(const StringTableSection &Sec) { 417 assert(Sec.Size == Sec.StrTabBuilder.getSize()); 418 std::vector<uint8_t> Data(Sec.Size); 419 Sec.StrTabBuilder.write(Data.data()); 420 writeSection(&Sec, Data); 421 return Error::success(); 422 } 423 424 Error Section::accept(SectionVisitor &Visitor) const { 425 return Visitor.visit(*this); 426 } 427 428 Error Section::accept(MutableSectionVisitor &Visitor) { 429 return Visitor.visit(*this); 430 } 431 432 Error SectionWriter::visit(const OwnedDataSection &Sec) { 433 llvm::copy(Sec.Data, Out.getBufferStart() + Sec.Offset); 434 return Error::success(); 435 } 436 437 static constexpr std::array<uint8_t, 4> ZlibGnuMagic = {{'Z', 'L', 'I', 'B'}}; 438 439 static bool isDataGnuCompressed(ArrayRef<uint8_t> Data) { 440 return Data.size() > ZlibGnuMagic.size() && 441 std::equal(ZlibGnuMagic.begin(), ZlibGnuMagic.end(), Data.data()); 442 } 443 444 template <class ELFT> 445 static std::tuple<uint64_t, uint64_t> 446 getDecompressedSizeAndAlignment(ArrayRef<uint8_t> Data) { 447 const bool IsGnuDebug = isDataGnuCompressed(Data); 448 const uint64_t DecompressedSize = 449 IsGnuDebug 450 ? support::endian::read64be(Data.data() + ZlibGnuMagic.size()) 451 : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())->ch_size; 452 const uint64_t DecompressedAlign = 453 IsGnuDebug ? 1 454 : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data()) 455 ->ch_addralign; 456 457 return std::make_tuple(DecompressedSize, DecompressedAlign); 458 } 459 460 template <class ELFT> 461 Error ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) { 462 const size_t DataOffset = isDataGnuCompressed(Sec.OriginalData) 463 ? (ZlibGnuMagic.size() + sizeof(Sec.Size)) 464 : sizeof(Elf_Chdr_Impl<ELFT>); 465 466 StringRef CompressedContent( 467 reinterpret_cast<const char *>(Sec.OriginalData.data()) + DataOffset, 468 Sec.OriginalData.size() - DataOffset); 469 470 SmallVector<char, 128> DecompressedContent; 471 if (Error Err = zlib::uncompress(CompressedContent, DecompressedContent, 472 static_cast<size_t>(Sec.Size))) 473 return createStringError(errc::invalid_argument, 474 "'" + Sec.Name + "': " + toString(std::move(Err))); 475 476 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset; 477 std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf); 478 479 return Error::success(); 480 } 481 482 Error BinarySectionWriter::visit(const DecompressedSection &Sec) { 483 return createStringError(errc::operation_not_permitted, 484 "cannot write compressed section '" + Sec.Name + 485 "' "); 486 } 487 488 Error DecompressedSection::accept(SectionVisitor &Visitor) const { 489 return Visitor.visit(*this); 490 } 491 492 Error DecompressedSection::accept(MutableSectionVisitor &Visitor) { 493 return Visitor.visit(*this); 494 } 495 496 Error OwnedDataSection::accept(SectionVisitor &Visitor) const { 497 return Visitor.visit(*this); 498 } 499 500 Error OwnedDataSection::accept(MutableSectionVisitor &Visitor) { 501 return Visitor.visit(*this); 502 } 503 504 void OwnedDataSection::appendHexData(StringRef HexData) { 505 assert((HexData.size() & 1) == 0); 506 while (!HexData.empty()) { 507 Data.push_back(checkedGetHex<uint8_t>(HexData.take_front(2))); 508 HexData = HexData.drop_front(2); 509 } 510 Size = Data.size(); 511 } 512 513 Error BinarySectionWriter::visit(const CompressedSection &Sec) { 514 return createStringError(errc::operation_not_permitted, 515 "cannot write compressed section '" + Sec.Name + 516 "' "); 517 } 518 519 template <class ELFT> 520 Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) { 521 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset; 522 if (Sec.CompressionType == DebugCompressionType::None) { 523 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf); 524 return Error::success(); 525 } 526 527 if (Sec.CompressionType == DebugCompressionType::GNU) { 528 const char *Magic = "ZLIB"; 529 memcpy(Buf, Magic, strlen(Magic)); 530 Buf += strlen(Magic); 531 const uint64_t DecompressedSize = 532 support::endian::read64be(&Sec.DecompressedSize); 533 memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize)); 534 Buf += sizeof(DecompressedSize); 535 } else { 536 Elf_Chdr_Impl<ELFT> Chdr; 537 Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB; 538 Chdr.ch_size = Sec.DecompressedSize; 539 Chdr.ch_addralign = Sec.DecompressedAlign; 540 memcpy(Buf, &Chdr, sizeof(Chdr)); 541 Buf += sizeof(Chdr); 542 } 543 544 std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf); 545 return Error::success(); 546 } 547 548 Expected<CompressedSection> 549 CompressedSection::create(const SectionBase &Sec, 550 DebugCompressionType CompressionType) { 551 return CompressedSection(Sec, CompressionType); 552 } 553 Expected<CompressedSection> 554 CompressedSection::create(ArrayRef<uint8_t> CompressedData, 555 uint64_t DecompressedSize, 556 uint64_t DecompressedAlign) { 557 return CompressedSection(CompressedData, DecompressedSize, DecompressedAlign); 558 } 559 560 CompressedSection::CompressedSection(const SectionBase &Sec, 561 DebugCompressionType CompressionType) 562 : SectionBase(Sec), CompressionType(CompressionType), 563 DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) { 564 zlib::compress(StringRef(reinterpret_cast<const char *>(OriginalData.data()), 565 OriginalData.size()), 566 CompressedData); 567 568 size_t ChdrSize; 569 if (CompressionType == DebugCompressionType::GNU) { 570 Name = ".z" + Sec.Name.substr(1); 571 ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t); 572 } else { 573 Flags |= ELF::SHF_COMPRESSED; 574 ChdrSize = 575 std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>), 576 sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)), 577 std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>), 578 sizeof(object::Elf_Chdr_Impl<object::ELF32BE>))); 579 } 580 Size = ChdrSize + CompressedData.size(); 581 Align = 8; 582 } 583 584 CompressedSection::CompressedSection(ArrayRef<uint8_t> CompressedData, 585 uint64_t DecompressedSize, 586 uint64_t DecompressedAlign) 587 : CompressionType(DebugCompressionType::None), 588 DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) { 589 OriginalData = CompressedData; 590 } 591 592 Error CompressedSection::accept(SectionVisitor &Visitor) const { 593 return Visitor.visit(*this); 594 } 595 596 Error CompressedSection::accept(MutableSectionVisitor &Visitor) { 597 return Visitor.visit(*this); 598 } 599 600 void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); } 601 602 uint32_t StringTableSection::findIndex(StringRef Name) const { 603 return StrTabBuilder.getOffset(Name); 604 } 605 606 void StringTableSection::prepareForLayout() { 607 StrTabBuilder.finalize(); 608 Size = StrTabBuilder.getSize(); 609 } 610 611 Error SectionWriter::visit(const StringTableSection &Sec) { 612 Sec.StrTabBuilder.write(reinterpret_cast<uint8_t *>(Out.getBufferStart()) + 613 Sec.Offset); 614 return Error::success(); 615 } 616 617 Error StringTableSection::accept(SectionVisitor &Visitor) const { 618 return Visitor.visit(*this); 619 } 620 621 Error StringTableSection::accept(MutableSectionVisitor &Visitor) { 622 return Visitor.visit(*this); 623 } 624 625 template <class ELFT> 626 Error ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) { 627 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset; 628 llvm::copy(Sec.Indexes, reinterpret_cast<Elf_Word *>(Buf)); 629 return Error::success(); 630 } 631 632 Error SectionIndexSection::initialize(SectionTableRef SecTable) { 633 Size = 0; 634 Expected<SymbolTableSection *> Sec = 635 SecTable.getSectionOfType<SymbolTableSection>( 636 Link, 637 "Link field value " + Twine(Link) + " in section " + Name + 638 " is invalid", 639 "Link field value " + Twine(Link) + " in section " + Name + 640 " is not a symbol table"); 641 if (!Sec) 642 return Sec.takeError(); 643 644 setSymTab(*Sec); 645 Symbols->setShndxTable(this); 646 return Error::success(); 647 } 648 649 void SectionIndexSection::finalize() { Link = Symbols->Index; } 650 651 Error SectionIndexSection::accept(SectionVisitor &Visitor) const { 652 return Visitor.visit(*this); 653 } 654 655 Error SectionIndexSection::accept(MutableSectionVisitor &Visitor) { 656 return Visitor.visit(*this); 657 } 658 659 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) { 660 switch (Index) { 661 case SHN_ABS: 662 case SHN_COMMON: 663 return true; 664 } 665 666 if (Machine == EM_AMDGPU) { 667 return Index == SHN_AMDGPU_LDS; 668 } 669 670 if (Machine == EM_HEXAGON) { 671 switch (Index) { 672 case SHN_HEXAGON_SCOMMON: 673 case SHN_HEXAGON_SCOMMON_1: 674 case SHN_HEXAGON_SCOMMON_2: 675 case SHN_HEXAGON_SCOMMON_4: 676 case SHN_HEXAGON_SCOMMON_8: 677 return true; 678 } 679 } 680 return false; 681 } 682 683 // Large indexes force us to clarify exactly what this function should do. This 684 // function should return the value that will appear in st_shndx when written 685 // out. 686 uint16_t Symbol::getShndx() const { 687 if (DefinedIn != nullptr) { 688 if (DefinedIn->Index >= SHN_LORESERVE) 689 return SHN_XINDEX; 690 return DefinedIn->Index; 691 } 692 693 if (ShndxType == SYMBOL_SIMPLE_INDEX) { 694 // This means that we don't have a defined section but we do need to 695 // output a legitimate section index. 696 return SHN_UNDEF; 697 } 698 699 assert(ShndxType == SYMBOL_ABS || ShndxType == SYMBOL_COMMON || 700 (ShndxType >= SYMBOL_LOPROC && ShndxType <= SYMBOL_HIPROC) || 701 (ShndxType >= SYMBOL_LOOS && ShndxType <= SYMBOL_HIOS)); 702 return static_cast<uint16_t>(ShndxType); 703 } 704 705 bool Symbol::isCommon() const { return getShndx() == SHN_COMMON; } 706 707 void SymbolTableSection::assignIndices() { 708 uint32_t Index = 0; 709 for (auto &Sym : Symbols) 710 Sym->Index = Index++; 711 } 712 713 void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type, 714 SectionBase *DefinedIn, uint64_t Value, 715 uint8_t Visibility, uint16_t Shndx, 716 uint64_t SymbolSize) { 717 Symbol Sym; 718 Sym.Name = Name.str(); 719 Sym.Binding = Bind; 720 Sym.Type = Type; 721 Sym.DefinedIn = DefinedIn; 722 if (DefinedIn != nullptr) 723 DefinedIn->HasSymbol = true; 724 if (DefinedIn == nullptr) { 725 if (Shndx >= SHN_LORESERVE) 726 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx); 727 else 728 Sym.ShndxType = SYMBOL_SIMPLE_INDEX; 729 } 730 Sym.Value = Value; 731 Sym.Visibility = Visibility; 732 Sym.Size = SymbolSize; 733 Sym.Index = Symbols.size(); 734 Symbols.emplace_back(std::make_unique<Symbol>(Sym)); 735 Size += this->EntrySize; 736 } 737 738 Error SymbolTableSection::removeSectionReferences( 739 bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) { 740 if (ToRemove(SectionIndexTable)) 741 SectionIndexTable = nullptr; 742 if (ToRemove(SymbolNames)) { 743 if (!AllowBrokenLinks) 744 return createStringError( 745 llvm::errc::invalid_argument, 746 "string table '%s' cannot be removed because it is " 747 "referenced by the symbol table '%s'", 748 SymbolNames->Name.data(), this->Name.data()); 749 SymbolNames = nullptr; 750 } 751 return removeSymbols( 752 [ToRemove](const Symbol &Sym) { return ToRemove(Sym.DefinedIn); }); 753 } 754 755 void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) { 756 std::for_each(std::begin(Symbols) + 1, std::end(Symbols), 757 [Callable](SymPtr &Sym) { Callable(*Sym); }); 758 std::stable_partition( 759 std::begin(Symbols), std::end(Symbols), 760 [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; }); 761 assignIndices(); 762 } 763 764 Error SymbolTableSection::removeSymbols( 765 function_ref<bool(const Symbol &)> ToRemove) { 766 Symbols.erase( 767 std::remove_if(std::begin(Symbols) + 1, std::end(Symbols), 768 [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }), 769 std::end(Symbols)); 770 Size = Symbols.size() * EntrySize; 771 assignIndices(); 772 return Error::success(); 773 } 774 775 void SymbolTableSection::replaceSectionReferences( 776 const DenseMap<SectionBase *, SectionBase *> &FromTo) { 777 for (std::unique_ptr<Symbol> &Sym : Symbols) 778 if (SectionBase *To = FromTo.lookup(Sym->DefinedIn)) 779 Sym->DefinedIn = To; 780 } 781 782 Error SymbolTableSection::initialize(SectionTableRef SecTable) { 783 Size = 0; 784 Expected<StringTableSection *> Sec = 785 SecTable.getSectionOfType<StringTableSection>( 786 Link, 787 "Symbol table has link index of " + Twine(Link) + 788 " which is not a valid index", 789 "Symbol table has link index of " + Twine(Link) + 790 " which is not a string table"); 791 if (!Sec) 792 return Sec.takeError(); 793 794 setStrTab(*Sec); 795 return Error::success(); 796 } 797 798 void SymbolTableSection::finalize() { 799 uint32_t MaxLocalIndex = 0; 800 for (std::unique_ptr<Symbol> &Sym : Symbols) { 801 Sym->NameIndex = 802 SymbolNames == nullptr ? 0 : SymbolNames->findIndex(Sym->Name); 803 if (Sym->Binding == STB_LOCAL) 804 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index); 805 } 806 // Now we need to set the Link and Info fields. 807 Link = SymbolNames == nullptr ? 0 : SymbolNames->Index; 808 Info = MaxLocalIndex + 1; 809 } 810 811 void SymbolTableSection::prepareForLayout() { 812 // Reserve proper amount of space in section index table, so we can 813 // layout sections correctly. We will fill the table with correct 814 // indexes later in fillShdnxTable. 815 if (SectionIndexTable) 816 SectionIndexTable->reserve(Symbols.size()); 817 818 // Add all of our strings to SymbolNames so that SymbolNames has the right 819 // size before layout is decided. 820 // If the symbol names section has been removed, don't try to add strings to 821 // the table. 822 if (SymbolNames != nullptr) 823 for (std::unique_ptr<Symbol> &Sym : Symbols) 824 SymbolNames->addString(Sym->Name); 825 } 826 827 void SymbolTableSection::fillShndxTable() { 828 if (SectionIndexTable == nullptr) 829 return; 830 // Fill section index table with real section indexes. This function must 831 // be called after assignOffsets. 832 for (const std::unique_ptr<Symbol> &Sym : Symbols) { 833 if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE) 834 SectionIndexTable->addIndex(Sym->DefinedIn->Index); 835 else 836 SectionIndexTable->addIndex(SHN_UNDEF); 837 } 838 } 839 840 Expected<const Symbol *> 841 SymbolTableSection::getSymbolByIndex(uint32_t Index) const { 842 if (Symbols.size() <= Index) 843 return createStringError(errc::invalid_argument, 844 "invalid symbol index: " + Twine(Index)); 845 return Symbols[Index].get(); 846 } 847 848 Expected<Symbol *> SymbolTableSection::getSymbolByIndex(uint32_t Index) { 849 Expected<const Symbol *> Sym = 850 static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index); 851 if (!Sym) 852 return Sym.takeError(); 853 854 return const_cast<Symbol *>(*Sym); 855 } 856 857 template <class ELFT> 858 Error ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) { 859 Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Out.getBufferStart() + Sec.Offset); 860 // Loop though symbols setting each entry of the symbol table. 861 for (const std::unique_ptr<Symbol> &Symbol : Sec.Symbols) { 862 Sym->st_name = Symbol->NameIndex; 863 Sym->st_value = Symbol->Value; 864 Sym->st_size = Symbol->Size; 865 Sym->st_other = Symbol->Visibility; 866 Sym->setBinding(Symbol->Binding); 867 Sym->setType(Symbol->Type); 868 Sym->st_shndx = Symbol->getShndx(); 869 ++Sym; 870 } 871 return Error::success(); 872 } 873 874 Error SymbolTableSection::accept(SectionVisitor &Visitor) const { 875 return Visitor.visit(*this); 876 } 877 878 Error SymbolTableSection::accept(MutableSectionVisitor &Visitor) { 879 return Visitor.visit(*this); 880 } 881 882 StringRef RelocationSectionBase::getNamePrefix() const { 883 switch (Type) { 884 case SHT_REL: 885 return ".rel"; 886 case SHT_RELA: 887 return ".rela"; 888 default: 889 llvm_unreachable("not a relocation section"); 890 } 891 } 892 893 Error RelocationSection::removeSectionReferences( 894 bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) { 895 if (ToRemove(Symbols)) { 896 if (!AllowBrokenLinks) 897 return createStringError( 898 llvm::errc::invalid_argument, 899 "symbol table '%s' cannot be removed because it is " 900 "referenced by the relocation section '%s'", 901 Symbols->Name.data(), this->Name.data()); 902 Symbols = nullptr; 903 } 904 905 for (const Relocation &R : Relocations) { 906 if (!R.RelocSymbol || !R.RelocSymbol->DefinedIn || 907 !ToRemove(R.RelocSymbol->DefinedIn)) 908 continue; 909 return createStringError(llvm::errc::invalid_argument, 910 "section '%s' cannot be removed: (%s+0x%" PRIx64 911 ") has relocation against symbol '%s'", 912 R.RelocSymbol->DefinedIn->Name.data(), 913 SecToApplyRel->Name.data(), R.Offset, 914 R.RelocSymbol->Name.c_str()); 915 } 916 917 return Error::success(); 918 } 919 920 template <class SymTabType> 921 Error RelocSectionWithSymtabBase<SymTabType>::initialize( 922 SectionTableRef SecTable) { 923 if (Link != SHN_UNDEF) { 924 Expected<SymTabType *> Sec = SecTable.getSectionOfType<SymTabType>( 925 Link, 926 "Link field value " + Twine(Link) + " in section " + Name + 927 " is invalid", 928 "Link field value " + Twine(Link) + " in section " + Name + 929 " is not a symbol table"); 930 if (!Sec) 931 return Sec.takeError(); 932 933 setSymTab(*Sec); 934 } 935 936 if (Info != SHN_UNDEF) { 937 Expected<SectionBase *> Sec = 938 SecTable.getSection(Info, "Info field value " + Twine(Info) + 939 " in section " + Name + " is invalid"); 940 if (!Sec) 941 return Sec.takeError(); 942 943 setSection(*Sec); 944 } else 945 setSection(nullptr); 946 947 return Error::success(); 948 } 949 950 template <class SymTabType> 951 void RelocSectionWithSymtabBase<SymTabType>::finalize() { 952 this->Link = Symbols ? Symbols->Index : 0; 953 954 if (SecToApplyRel != nullptr) 955 this->Info = SecToApplyRel->Index; 956 } 957 958 template <class ELFT> 959 static void setAddend(Elf_Rel_Impl<ELFT, false> &, uint64_t) {} 960 961 template <class ELFT> 962 static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) { 963 Rela.r_addend = Addend; 964 } 965 966 template <class RelRange, class T> 967 static void writeRel(const RelRange &Relocations, T *Buf, bool IsMips64EL) { 968 for (const auto &Reloc : Relocations) { 969 Buf->r_offset = Reloc.Offset; 970 setAddend(*Buf, Reloc.Addend); 971 Buf->setSymbolAndType(Reloc.RelocSymbol ? Reloc.RelocSymbol->Index : 0, 972 Reloc.Type, IsMips64EL); 973 ++Buf; 974 } 975 } 976 977 template <class ELFT> 978 Error ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) { 979 uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset; 980 if (Sec.Type == SHT_REL) 981 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf), 982 Sec.getObject().IsMips64EL); 983 else 984 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf), 985 Sec.getObject().IsMips64EL); 986 return Error::success(); 987 } 988 989 Error RelocationSection::accept(SectionVisitor &Visitor) const { 990 return Visitor.visit(*this); 991 } 992 993 Error RelocationSection::accept(MutableSectionVisitor &Visitor) { 994 return Visitor.visit(*this); 995 } 996 997 Error RelocationSection::removeSymbols( 998 function_ref<bool(const Symbol &)> ToRemove) { 999 for (const Relocation &Reloc : Relocations) 1000 if (Reloc.RelocSymbol && ToRemove(*Reloc.RelocSymbol)) 1001 return createStringError( 1002 llvm::errc::invalid_argument, 1003 "not stripping symbol '%s' because it is named in a relocation", 1004 Reloc.RelocSymbol->Name.data()); 1005 return Error::success(); 1006 } 1007 1008 void RelocationSection::markSymbols() { 1009 for (const Relocation &Reloc : Relocations) 1010 if (Reloc.RelocSymbol) 1011 Reloc.RelocSymbol->Referenced = true; 1012 } 1013 1014 void RelocationSection::replaceSectionReferences( 1015 const DenseMap<SectionBase *, SectionBase *> &FromTo) { 1016 // Update the target section if it was replaced. 1017 if (SectionBase *To = FromTo.lookup(SecToApplyRel)) 1018 SecToApplyRel = To; 1019 } 1020 1021 Error SectionWriter::visit(const DynamicRelocationSection &Sec) { 1022 llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset); 1023 return Error::success(); 1024 } 1025 1026 Error DynamicRelocationSection::accept(SectionVisitor &Visitor) const { 1027 return Visitor.visit(*this); 1028 } 1029 1030 Error DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) { 1031 return Visitor.visit(*this); 1032 } 1033 1034 Error DynamicRelocationSection::removeSectionReferences( 1035 bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) { 1036 if (ToRemove(Symbols)) { 1037 if (!AllowBrokenLinks) 1038 return createStringError( 1039 llvm::errc::invalid_argument, 1040 "symbol table '%s' cannot be removed because it is " 1041 "referenced by the relocation section '%s'", 1042 Symbols->Name.data(), this->Name.data()); 1043 Symbols = nullptr; 1044 } 1045 1046 // SecToApplyRel contains a section referenced by sh_info field. It keeps 1047 // a section to which the relocation section applies. When we remove any 1048 // sections we also remove their relocation sections. Since we do that much 1049 // earlier, this assert should never be triggered. 1050 assert(!SecToApplyRel || !ToRemove(SecToApplyRel)); 1051 return Error::success(); 1052 } 1053 1054 Error Section::removeSectionReferences( 1055 bool AllowBrokenDependency, 1056 function_ref<bool(const SectionBase *)> ToRemove) { 1057 if (ToRemove(LinkSection)) { 1058 if (!AllowBrokenDependency) 1059 return createStringError(llvm::errc::invalid_argument, 1060 "section '%s' cannot be removed because it is " 1061 "referenced by the section '%s'", 1062 LinkSection->Name.data(), this->Name.data()); 1063 LinkSection = nullptr; 1064 } 1065 return Error::success(); 1066 } 1067 1068 void GroupSection::finalize() { 1069 this->Info = Sym ? Sym->Index : 0; 1070 this->Link = SymTab ? SymTab->Index : 0; 1071 // Linker deduplication for GRP_COMDAT is based on Sym->Name. The local/global 1072 // status is not part of the equation. If Sym is localized, the intention is 1073 // likely to make the group fully localized. Drop GRP_COMDAT to suppress 1074 // deduplication. See https://groups.google.com/g/generic-abi/c/2X6mR-s2zoc 1075 if ((FlagWord & GRP_COMDAT) && Sym && Sym->Binding == STB_LOCAL) 1076 this->FlagWord &= ~GRP_COMDAT; 1077 } 1078 1079 Error GroupSection::removeSectionReferences( 1080 bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) { 1081 if (ToRemove(SymTab)) { 1082 if (!AllowBrokenLinks) 1083 return createStringError( 1084 llvm::errc::invalid_argument, 1085 "section '.symtab' cannot be removed because it is " 1086 "referenced by the group section '%s'", 1087 this->Name.data()); 1088 SymTab = nullptr; 1089 Sym = nullptr; 1090 } 1091 llvm::erase_if(GroupMembers, ToRemove); 1092 return Error::success(); 1093 } 1094 1095 Error GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { 1096 if (ToRemove(*Sym)) 1097 return createStringError(llvm::errc::invalid_argument, 1098 "symbol '%s' cannot be removed because it is " 1099 "referenced by the section '%s[%d]'", 1100 Sym->Name.data(), this->Name.data(), this->Index); 1101 return Error::success(); 1102 } 1103 1104 void GroupSection::markSymbols() { 1105 if (Sym) 1106 Sym->Referenced = true; 1107 } 1108 1109 void GroupSection::replaceSectionReferences( 1110 const DenseMap<SectionBase *, SectionBase *> &FromTo) { 1111 for (SectionBase *&Sec : GroupMembers) 1112 if (SectionBase *To = FromTo.lookup(Sec)) 1113 Sec = To; 1114 } 1115 1116 void GroupSection::onRemove() { 1117 // As the header section of the group is removed, drop the Group flag in its 1118 // former members. 1119 for (SectionBase *Sec : GroupMembers) 1120 Sec->Flags &= ~SHF_GROUP; 1121 } 1122 1123 Error Section::initialize(SectionTableRef SecTable) { 1124 if (Link == ELF::SHN_UNDEF) 1125 return Error::success(); 1126 1127 Expected<SectionBase *> Sec = 1128 SecTable.getSection(Link, "Link field value " + Twine(Link) + 1129 " in section " + Name + " is invalid"); 1130 if (!Sec) 1131 return Sec.takeError(); 1132 1133 LinkSection = *Sec; 1134 1135 if (LinkSection->Type == ELF::SHT_SYMTAB) 1136 LinkSection = nullptr; 1137 1138 return Error::success(); 1139 } 1140 1141 void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; } 1142 1143 void GnuDebugLinkSection::init(StringRef File) { 1144 FileName = sys::path::filename(File); 1145 // The format for the .gnu_debuglink starts with the file name and is 1146 // followed by a null terminator and then the CRC32 of the file. The CRC32 1147 // should be 4 byte aligned. So we add the FileName size, a 1 for the null 1148 // byte, and then finally push the size to alignment and add 4. 1149 Size = alignTo(FileName.size() + 1, 4) + 4; 1150 // The CRC32 will only be aligned if we align the whole section. 1151 Align = 4; 1152 Type = OriginalType = ELF::SHT_PROGBITS; 1153 Name = ".gnu_debuglink"; 1154 // For sections not found in segments, OriginalOffset is only used to 1155 // establish the order that sections should go in. By using the maximum 1156 // possible offset we cause this section to wind up at the end. 1157 OriginalOffset = std::numeric_limits<uint64_t>::max(); 1158 } 1159 1160 GnuDebugLinkSection::GnuDebugLinkSection(StringRef File, 1161 uint32_t PrecomputedCRC) 1162 : FileName(File), CRC32(PrecomputedCRC) { 1163 init(File); 1164 } 1165 1166 template <class ELFT> 1167 Error ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) { 1168 unsigned char *Buf = 1169 reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset; 1170 Elf_Word *CRC = 1171 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word)); 1172 *CRC = Sec.CRC32; 1173 llvm::copy(Sec.FileName, Buf); 1174 return Error::success(); 1175 } 1176 1177 Error GnuDebugLinkSection::accept(SectionVisitor &Visitor) const { 1178 return Visitor.visit(*this); 1179 } 1180 1181 Error GnuDebugLinkSection::accept(MutableSectionVisitor &Visitor) { 1182 return Visitor.visit(*this); 1183 } 1184 1185 template <class ELFT> 1186 Error ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) { 1187 ELF::Elf32_Word *Buf = 1188 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset); 1189 support::endian::write32<ELFT::TargetEndianness>(Buf++, Sec.FlagWord); 1190 for (SectionBase *S : Sec.GroupMembers) 1191 support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index); 1192 return Error::success(); 1193 } 1194 1195 Error GroupSection::accept(SectionVisitor &Visitor) const { 1196 return Visitor.visit(*this); 1197 } 1198 1199 Error GroupSection::accept(MutableSectionVisitor &Visitor) { 1200 return Visitor.visit(*this); 1201 } 1202 1203 // Returns true IFF a section is wholly inside the range of a segment 1204 static bool sectionWithinSegment(const SectionBase &Sec, const Segment &Seg) { 1205 // If a section is empty it should be treated like it has a size of 1. This is 1206 // to clarify the case when an empty section lies on a boundary between two 1207 // segments and ensures that the section "belongs" to the second segment and 1208 // not the first. 1209 uint64_t SecSize = Sec.Size ? Sec.Size : 1; 1210 1211 // Ignore just added sections. 1212 if (Sec.OriginalOffset == std::numeric_limits<uint64_t>::max()) 1213 return false; 1214 1215 if (Sec.Type == SHT_NOBITS) { 1216 if (!(Sec.Flags & SHF_ALLOC)) 1217 return false; 1218 1219 bool SectionIsTLS = Sec.Flags & SHF_TLS; 1220 bool SegmentIsTLS = Seg.Type == PT_TLS; 1221 if (SectionIsTLS != SegmentIsTLS) 1222 return false; 1223 1224 return Seg.VAddr <= Sec.Addr && 1225 Seg.VAddr + Seg.MemSize >= Sec.Addr + SecSize; 1226 } 1227 1228 return Seg.Offset <= Sec.OriginalOffset && 1229 Seg.Offset + Seg.FileSize >= Sec.OriginalOffset + SecSize; 1230 } 1231 1232 // Returns true IFF a segment's original offset is inside of another segment's 1233 // range. 1234 static bool segmentOverlapsSegment(const Segment &Child, 1235 const Segment &Parent) { 1236 1237 return Parent.OriginalOffset <= Child.OriginalOffset && 1238 Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset; 1239 } 1240 1241 static bool compareSegmentsByOffset(const Segment *A, const Segment *B) { 1242 // Any segment without a parent segment should come before a segment 1243 // that has a parent segment. 1244 if (A->OriginalOffset < B->OriginalOffset) 1245 return true; 1246 if (A->OriginalOffset > B->OriginalOffset) 1247 return false; 1248 return A->Index < B->Index; 1249 } 1250 1251 void BasicELFBuilder::initFileHeader() { 1252 Obj->Flags = 0x0; 1253 Obj->Type = ET_REL; 1254 Obj->OSABI = ELFOSABI_NONE; 1255 Obj->ABIVersion = 0; 1256 Obj->Entry = 0x0; 1257 Obj->Machine = EM_NONE; 1258 Obj->Version = 1; 1259 } 1260 1261 void BasicELFBuilder::initHeaderSegment() { Obj->ElfHdrSegment.Index = 0; } 1262 1263 StringTableSection *BasicELFBuilder::addStrTab() { 1264 auto &StrTab = Obj->addSection<StringTableSection>(); 1265 StrTab.Name = ".strtab"; 1266 1267 Obj->SectionNames = &StrTab; 1268 return &StrTab; 1269 } 1270 1271 SymbolTableSection *BasicELFBuilder::addSymTab(StringTableSection *StrTab) { 1272 auto &SymTab = Obj->addSection<SymbolTableSection>(); 1273 1274 SymTab.Name = ".symtab"; 1275 SymTab.Link = StrTab->Index; 1276 1277 // The symbol table always needs a null symbol 1278 SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0); 1279 1280 Obj->SymbolTable = &SymTab; 1281 return &SymTab; 1282 } 1283 1284 Error BasicELFBuilder::initSections() { 1285 for (SectionBase &Sec : Obj->sections()) 1286 if (Error Err = Sec.initialize(Obj->sections())) 1287 return Err; 1288 1289 return Error::success(); 1290 } 1291 1292 void BinaryELFBuilder::addData(SymbolTableSection *SymTab) { 1293 auto Data = ArrayRef<uint8_t>( 1294 reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()), 1295 MemBuf->getBufferSize()); 1296 auto &DataSection = Obj->addSection<Section>(Data); 1297 DataSection.Name = ".data"; 1298 DataSection.Type = ELF::SHT_PROGBITS; 1299 DataSection.Size = Data.size(); 1300 DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; 1301 1302 std::string SanitizedFilename = MemBuf->getBufferIdentifier().str(); 1303 std::replace_if( 1304 std::begin(SanitizedFilename), std::end(SanitizedFilename), 1305 [](char C) { return !isAlnum(C); }, '_'); 1306 Twine Prefix = Twine("_binary_") + SanitizedFilename; 1307 1308 SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection, 1309 /*Value=*/0, NewSymbolVisibility, 0, 0); 1310 SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection, 1311 /*Value=*/DataSection.Size, NewSymbolVisibility, 0, 0); 1312 SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr, 1313 /*Value=*/DataSection.Size, NewSymbolVisibility, SHN_ABS, 1314 0); 1315 } 1316 1317 Expected<std::unique_ptr<Object>> BinaryELFBuilder::build() { 1318 initFileHeader(); 1319 initHeaderSegment(); 1320 1321 SymbolTableSection *SymTab = addSymTab(addStrTab()); 1322 if (Error Err = initSections()) 1323 return std::move(Err); 1324 addData(SymTab); 1325 1326 return std::move(Obj); 1327 } 1328 1329 // Adds sections from IHEX data file. Data should have been 1330 // fully validated by this time. 1331 void IHexELFBuilder::addDataSections() { 1332 OwnedDataSection *Section = nullptr; 1333 uint64_t SegmentAddr = 0, BaseAddr = 0; 1334 uint32_t SecNo = 1; 1335 1336 for (const IHexRecord &R : Records) { 1337 uint64_t RecAddr; 1338 switch (R.Type) { 1339 case IHexRecord::Data: 1340 // Ignore empty data records 1341 if (R.HexData.empty()) 1342 continue; 1343 RecAddr = R.Addr + SegmentAddr + BaseAddr; 1344 if (!Section || Section->Addr + Section->Size != RecAddr) { 1345 // OriginalOffset field is only used to sort sections before layout, so 1346 // instead of keeping track of real offsets in IHEX file, and as 1347 // layoutSections() and layoutSectionsForOnlyKeepDebug() use 1348 // llvm::stable_sort(), we can just set it to a constant (zero). 1349 Section = &Obj->addSection<OwnedDataSection>( 1350 ".sec" + std::to_string(SecNo), RecAddr, 1351 ELF::SHF_ALLOC | ELF::SHF_WRITE, 0); 1352 SecNo++; 1353 } 1354 Section->appendHexData(R.HexData); 1355 break; 1356 case IHexRecord::EndOfFile: 1357 break; 1358 case IHexRecord::SegmentAddr: 1359 // 20-bit segment address. 1360 SegmentAddr = checkedGetHex<uint16_t>(R.HexData) << 4; 1361 break; 1362 case IHexRecord::StartAddr80x86: 1363 case IHexRecord::StartAddr: 1364 Obj->Entry = checkedGetHex<uint32_t>(R.HexData); 1365 assert(Obj->Entry <= 0xFFFFFU); 1366 break; 1367 case IHexRecord::ExtendedAddr: 1368 // 16-31 bits of linear base address 1369 BaseAddr = checkedGetHex<uint16_t>(R.HexData) << 16; 1370 break; 1371 default: 1372 llvm_unreachable("unknown record type"); 1373 } 1374 } 1375 } 1376 1377 Expected<std::unique_ptr<Object>> IHexELFBuilder::build() { 1378 initFileHeader(); 1379 initHeaderSegment(); 1380 StringTableSection *StrTab = addStrTab(); 1381 addSymTab(StrTab); 1382 if (Error Err = initSections()) 1383 return std::move(Err); 1384 addDataSections(); 1385 1386 return std::move(Obj); 1387 } 1388 1389 template <class ELFT> 1390 ELFBuilder<ELFT>::ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj, 1391 Optional<StringRef> ExtractPartition) 1392 : ElfFile(ElfObj.getELFFile()), Obj(Obj), 1393 ExtractPartition(ExtractPartition) { 1394 Obj.IsMips64EL = ElfFile.isMips64EL(); 1395 } 1396 1397 template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) { 1398 for (Segment &Parent : Obj.segments()) { 1399 // Every segment will overlap with itself but we don't want a segment to 1400 // be its own parent so we avoid that situation. 1401 if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) { 1402 // We want a canonical "most parental" segment but this requires 1403 // inspecting the ParentSegment. 1404 if (compareSegmentsByOffset(&Parent, &Child)) 1405 if (Child.ParentSegment == nullptr || 1406 compareSegmentsByOffset(&Parent, Child.ParentSegment)) { 1407 Child.ParentSegment = &Parent; 1408 } 1409 } 1410 } 1411 } 1412 1413 template <class ELFT> Error ELFBuilder<ELFT>::findEhdrOffset() { 1414 if (!ExtractPartition) 1415 return Error::success(); 1416 1417 for (const SectionBase &Sec : Obj.sections()) { 1418 if (Sec.Type == SHT_LLVM_PART_EHDR && Sec.Name == *ExtractPartition) { 1419 EhdrOffset = Sec.Offset; 1420 return Error::success(); 1421 } 1422 } 1423 return createStringError(errc::invalid_argument, 1424 "could not find partition named '" + 1425 *ExtractPartition + "'"); 1426 } 1427 1428 template <class ELFT> 1429 Error ELFBuilder<ELFT>::readProgramHeaders(const ELFFile<ELFT> &HeadersFile) { 1430 uint32_t Index = 0; 1431 1432 Expected<typename ELFFile<ELFT>::Elf_Phdr_Range> Headers = 1433 HeadersFile.program_headers(); 1434 if (!Headers) 1435 return Headers.takeError(); 1436 1437 for (const typename ELFFile<ELFT>::Elf_Phdr &Phdr : *Headers) { 1438 if (Phdr.p_offset + Phdr.p_filesz > HeadersFile.getBufSize()) 1439 return createStringError( 1440 errc::invalid_argument, 1441 "program header with offset 0x" + Twine::utohexstr(Phdr.p_offset) + 1442 " and file size 0x" + Twine::utohexstr(Phdr.p_filesz) + 1443 " goes past the end of the file"); 1444 1445 ArrayRef<uint8_t> Data{HeadersFile.base() + Phdr.p_offset, 1446 (size_t)Phdr.p_filesz}; 1447 Segment &Seg = Obj.addSegment(Data); 1448 Seg.Type = Phdr.p_type; 1449 Seg.Flags = Phdr.p_flags; 1450 Seg.OriginalOffset = Phdr.p_offset + EhdrOffset; 1451 Seg.Offset = Phdr.p_offset + EhdrOffset; 1452 Seg.VAddr = Phdr.p_vaddr; 1453 Seg.PAddr = Phdr.p_paddr; 1454 Seg.FileSize = Phdr.p_filesz; 1455 Seg.MemSize = Phdr.p_memsz; 1456 Seg.Align = Phdr.p_align; 1457 Seg.Index = Index++; 1458 for (SectionBase &Sec : Obj.sections()) 1459 if (sectionWithinSegment(Sec, Seg)) { 1460 Seg.addSection(&Sec); 1461 if (!Sec.ParentSegment || Sec.ParentSegment->Offset > Seg.Offset) 1462 Sec.ParentSegment = &Seg; 1463 } 1464 } 1465 1466 auto &ElfHdr = Obj.ElfHdrSegment; 1467 ElfHdr.Index = Index++; 1468 ElfHdr.OriginalOffset = ElfHdr.Offset = EhdrOffset; 1469 1470 const typename ELFT::Ehdr &Ehdr = HeadersFile.getHeader(); 1471 auto &PrHdr = Obj.ProgramHdrSegment; 1472 PrHdr.Type = PT_PHDR; 1473 PrHdr.Flags = 0; 1474 // The spec requires us to have p_vaddr % p_align == p_offset % p_align. 1475 // Whereas this works automatically for ElfHdr, here OriginalOffset is 1476 // always non-zero and to ensure the equation we assign the same value to 1477 // VAddr as well. 1478 PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = EhdrOffset + Ehdr.e_phoff; 1479 PrHdr.PAddr = 0; 1480 PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum; 1481 // The spec requires us to naturally align all the fields. 1482 PrHdr.Align = sizeof(Elf_Addr); 1483 PrHdr.Index = Index++; 1484 1485 // Now we do an O(n^2) loop through the segments in order to match up 1486 // segments. 1487 for (Segment &Child : Obj.segments()) 1488 setParentSegment(Child); 1489 setParentSegment(ElfHdr); 1490 setParentSegment(PrHdr); 1491 1492 return Error::success(); 1493 } 1494 1495 template <class ELFT> 1496 Error ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) { 1497 if (GroupSec->Align % sizeof(ELF::Elf32_Word) != 0) 1498 return createStringError(errc::invalid_argument, 1499 "invalid alignment " + Twine(GroupSec->Align) + 1500 " of group section '" + GroupSec->Name + "'"); 1501 SectionTableRef SecTable = Obj.sections(); 1502 if (GroupSec->Link != SHN_UNDEF) { 1503 auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>( 1504 GroupSec->Link, 1505 "link field value '" + Twine(GroupSec->Link) + "' in section '" + 1506 GroupSec->Name + "' is invalid", 1507 "link field value '" + Twine(GroupSec->Link) + "' in section '" + 1508 GroupSec->Name + "' is not a symbol table"); 1509 if (!SymTab) 1510 return SymTab.takeError(); 1511 1512 Expected<Symbol *> Sym = (*SymTab)->getSymbolByIndex(GroupSec->Info); 1513 if (!Sym) 1514 return createStringError(errc::invalid_argument, 1515 "info field value '" + Twine(GroupSec->Info) + 1516 "' in section '" + GroupSec->Name + 1517 "' is not a valid symbol index"); 1518 GroupSec->setSymTab(*SymTab); 1519 GroupSec->setSymbol(*Sym); 1520 } 1521 if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) || 1522 GroupSec->Contents.empty()) 1523 return createStringError(errc::invalid_argument, 1524 "the content of the section " + GroupSec->Name + 1525 " is malformed"); 1526 const ELF::Elf32_Word *Word = 1527 reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data()); 1528 const ELF::Elf32_Word *End = 1529 Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word); 1530 GroupSec->setFlagWord( 1531 support::endian::read32<ELFT::TargetEndianness>(Word++)); 1532 for (; Word != End; ++Word) { 1533 uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word); 1534 Expected<SectionBase *> Sec = SecTable.getSection( 1535 Index, "group member index " + Twine(Index) + " in section '" + 1536 GroupSec->Name + "' is invalid"); 1537 if (!Sec) 1538 return Sec.takeError(); 1539 1540 GroupSec->addMember(*Sec); 1541 } 1542 1543 return Error::success(); 1544 } 1545 1546 template <class ELFT> 1547 Error ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) { 1548 Expected<const Elf_Shdr *> Shdr = ElfFile.getSection(SymTab->Index); 1549 if (!Shdr) 1550 return Shdr.takeError(); 1551 1552 Expected<StringRef> StrTabData = ElfFile.getStringTableForSymtab(**Shdr); 1553 if (!StrTabData) 1554 return StrTabData.takeError(); 1555 1556 ArrayRef<Elf_Word> ShndxData; 1557 1558 Expected<typename ELFFile<ELFT>::Elf_Sym_Range> Symbols = 1559 ElfFile.symbols(*Shdr); 1560 if (!Symbols) 1561 return Symbols.takeError(); 1562 1563 for (const typename ELFFile<ELFT>::Elf_Sym &Sym : *Symbols) { 1564 SectionBase *DefSection = nullptr; 1565 1566 Expected<StringRef> Name = Sym.getName(*StrTabData); 1567 if (!Name) 1568 return Name.takeError(); 1569 1570 if (Sym.st_shndx == SHN_XINDEX) { 1571 if (SymTab->getShndxTable() == nullptr) 1572 return createStringError(errc::invalid_argument, 1573 "symbol '" + *Name + 1574 "' has index SHN_XINDEX but no " 1575 "SHT_SYMTAB_SHNDX section exists"); 1576 if (ShndxData.data() == nullptr) { 1577 Expected<const Elf_Shdr *> ShndxSec = 1578 ElfFile.getSection(SymTab->getShndxTable()->Index); 1579 if (!ShndxSec) 1580 return ShndxSec.takeError(); 1581 1582 Expected<ArrayRef<Elf_Word>> Data = 1583 ElfFile.template getSectionContentsAsArray<Elf_Word>(**ShndxSec); 1584 if (!Data) 1585 return Data.takeError(); 1586 1587 ShndxData = *Data; 1588 if (ShndxData.size() != Symbols->size()) 1589 return createStringError( 1590 errc::invalid_argument, 1591 "symbol section index table does not have the same number of " 1592 "entries as the symbol table"); 1593 } 1594 Elf_Word Index = ShndxData[&Sym - Symbols->begin()]; 1595 Expected<SectionBase *> Sec = Obj.sections().getSection( 1596 Index, 1597 "symbol '" + *Name + "' has invalid section index " + Twine(Index)); 1598 if (!Sec) 1599 return Sec.takeError(); 1600 1601 DefSection = *Sec; 1602 } else if (Sym.st_shndx >= SHN_LORESERVE) { 1603 if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) { 1604 return createStringError( 1605 errc::invalid_argument, 1606 "symbol '" + *Name + 1607 "' has unsupported value greater than or equal " 1608 "to SHN_LORESERVE: " + 1609 Twine(Sym.st_shndx)); 1610 } 1611 } else if (Sym.st_shndx != SHN_UNDEF) { 1612 Expected<SectionBase *> Sec = Obj.sections().getSection( 1613 Sym.st_shndx, "symbol '" + *Name + 1614 "' is defined has invalid section index " + 1615 Twine(Sym.st_shndx)); 1616 if (!Sec) 1617 return Sec.takeError(); 1618 1619 DefSection = *Sec; 1620 } 1621 1622 SymTab->addSymbol(*Name, Sym.getBinding(), Sym.getType(), DefSection, 1623 Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size); 1624 } 1625 1626 return Error::success(); 1627 } 1628 1629 template <class ELFT> 1630 static void getAddend(uint64_t &, const Elf_Rel_Impl<ELFT, false> &) {} 1631 1632 template <class ELFT> 1633 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) { 1634 ToSet = Rela.r_addend; 1635 } 1636 1637 template <class T> 1638 static Error initRelocations(RelocationSection *Relocs, T RelRange) { 1639 for (const auto &Rel : RelRange) { 1640 Relocation ToAdd; 1641 ToAdd.Offset = Rel.r_offset; 1642 getAddend(ToAdd.Addend, Rel); 1643 ToAdd.Type = Rel.getType(Relocs->getObject().IsMips64EL); 1644 1645 if (uint32_t Sym = Rel.getSymbol(Relocs->getObject().IsMips64EL)) { 1646 if (!Relocs->getObject().SymbolTable) 1647 return createStringError( 1648 errc::invalid_argument, 1649 "'" + Relocs->Name + "': relocation references symbol with index " + 1650 Twine(Sym) + ", but there is no symbol table"); 1651 Expected<Symbol *> SymByIndex = 1652 Relocs->getObject().SymbolTable->getSymbolByIndex(Sym); 1653 if (!SymByIndex) 1654 return SymByIndex.takeError(); 1655 1656 ToAdd.RelocSymbol = *SymByIndex; 1657 } 1658 1659 Relocs->addRelocation(ToAdd); 1660 } 1661 1662 return Error::success(); 1663 } 1664 1665 Expected<SectionBase *> SectionTableRef::getSection(uint32_t Index, 1666 Twine ErrMsg) { 1667 if (Index == SHN_UNDEF || Index > Sections.size()) 1668 return createStringError(errc::invalid_argument, ErrMsg); 1669 return Sections[Index - 1].get(); 1670 } 1671 1672 template <class T> 1673 Expected<T *> SectionTableRef::getSectionOfType(uint32_t Index, 1674 Twine IndexErrMsg, 1675 Twine TypeErrMsg) { 1676 Expected<SectionBase *> BaseSec = getSection(Index, IndexErrMsg); 1677 if (!BaseSec) 1678 return BaseSec.takeError(); 1679 1680 if (T *Sec = dyn_cast<T>(*BaseSec)) 1681 return Sec; 1682 1683 return createStringError(errc::invalid_argument, TypeErrMsg); 1684 } 1685 1686 template <class ELFT> 1687 Expected<SectionBase &> ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) { 1688 switch (Shdr.sh_type) { 1689 case SHT_REL: 1690 case SHT_RELA: 1691 if (Shdr.sh_flags & SHF_ALLOC) { 1692 if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr)) 1693 return Obj.addSection<DynamicRelocationSection>(*Data); 1694 else 1695 return Data.takeError(); 1696 } 1697 return Obj.addSection<RelocationSection>(Obj); 1698 case SHT_STRTAB: 1699 // If a string table is allocated we don't want to mess with it. That would 1700 // mean altering the memory image. There are no special link types or 1701 // anything so we can just use a Section. 1702 if (Shdr.sh_flags & SHF_ALLOC) { 1703 if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr)) 1704 return Obj.addSection<Section>(*Data); 1705 else 1706 return Data.takeError(); 1707 } 1708 return Obj.addSection<StringTableSection>(); 1709 case SHT_HASH: 1710 case SHT_GNU_HASH: 1711 // Hash tables should refer to SHT_DYNSYM which we're not going to change. 1712 // Because of this we don't need to mess with the hash tables either. 1713 if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr)) 1714 return Obj.addSection<Section>(*Data); 1715 else 1716 return Data.takeError(); 1717 case SHT_GROUP: 1718 if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr)) 1719 return Obj.addSection<GroupSection>(*Data); 1720 else 1721 return Data.takeError(); 1722 case SHT_DYNSYM: 1723 if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr)) 1724 return Obj.addSection<DynamicSymbolTableSection>(*Data); 1725 else 1726 return Data.takeError(); 1727 case SHT_DYNAMIC: 1728 if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr)) 1729 return Obj.addSection<DynamicSection>(*Data); 1730 else 1731 return Data.takeError(); 1732 case SHT_SYMTAB: { 1733 auto &SymTab = Obj.addSection<SymbolTableSection>(); 1734 Obj.SymbolTable = &SymTab; 1735 return SymTab; 1736 } 1737 case SHT_SYMTAB_SHNDX: { 1738 auto &ShndxSection = Obj.addSection<SectionIndexSection>(); 1739 Obj.SectionIndexTable = &ShndxSection; 1740 return ShndxSection; 1741 } 1742 case SHT_NOBITS: 1743 return Obj.addSection<Section>(ArrayRef<uint8_t>()); 1744 default: { 1745 Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr); 1746 if (!Data) 1747 return Data.takeError(); 1748 1749 Expected<StringRef> Name = ElfFile.getSectionName(Shdr); 1750 if (!Name) 1751 return Name.takeError(); 1752 1753 if (Name->startswith(".zdebug") || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) { 1754 uint64_t DecompressedSize, DecompressedAlign; 1755 std::tie(DecompressedSize, DecompressedAlign) = 1756 getDecompressedSizeAndAlignment<ELFT>(*Data); 1757 Expected<CompressedSection> NewSection = 1758 CompressedSection::create(*Data, DecompressedSize, DecompressedAlign); 1759 if (!NewSection) 1760 return NewSection.takeError(); 1761 1762 return Obj.addSection<CompressedSection>(std::move(*NewSection)); 1763 } 1764 1765 return Obj.addSection<Section>(*Data); 1766 } 1767 } 1768 } 1769 1770 template <class ELFT> Error ELFBuilder<ELFT>::readSectionHeaders() { 1771 uint32_t Index = 0; 1772 Expected<typename ELFFile<ELFT>::Elf_Shdr_Range> Sections = 1773 ElfFile.sections(); 1774 if (!Sections) 1775 return Sections.takeError(); 1776 1777 for (const typename ELFFile<ELFT>::Elf_Shdr &Shdr : *Sections) { 1778 if (Index == 0) { 1779 ++Index; 1780 continue; 1781 } 1782 Expected<SectionBase &> Sec = makeSection(Shdr); 1783 if (!Sec) 1784 return Sec.takeError(); 1785 1786 Expected<StringRef> SecName = ElfFile.getSectionName(Shdr); 1787 if (!SecName) 1788 return SecName.takeError(); 1789 Sec->Name = SecName->str(); 1790 Sec->Type = Sec->OriginalType = Shdr.sh_type; 1791 Sec->Flags = Sec->OriginalFlags = Shdr.sh_flags; 1792 Sec->Addr = Shdr.sh_addr; 1793 Sec->Offset = Shdr.sh_offset; 1794 Sec->OriginalOffset = Shdr.sh_offset; 1795 Sec->Size = Shdr.sh_size; 1796 Sec->Link = Shdr.sh_link; 1797 Sec->Info = Shdr.sh_info; 1798 Sec->Align = Shdr.sh_addralign; 1799 Sec->EntrySize = Shdr.sh_entsize; 1800 Sec->Index = Index++; 1801 Sec->OriginalIndex = Sec->Index; 1802 Sec->OriginalData = ArrayRef<uint8_t>( 1803 ElfFile.base() + Shdr.sh_offset, 1804 (Shdr.sh_type == SHT_NOBITS) ? (size_t)0 : Shdr.sh_size); 1805 } 1806 1807 return Error::success(); 1808 } 1809 1810 template <class ELFT> Error ELFBuilder<ELFT>::readSections(bool EnsureSymtab) { 1811 uint32_t ShstrIndex = ElfFile.getHeader().e_shstrndx; 1812 if (ShstrIndex == SHN_XINDEX) { 1813 Expected<const Elf_Shdr *> Sec = ElfFile.getSection(0); 1814 if (!Sec) 1815 return Sec.takeError(); 1816 1817 ShstrIndex = (*Sec)->sh_link; 1818 } 1819 1820 if (ShstrIndex == SHN_UNDEF) 1821 Obj.HadShdrs = false; 1822 else { 1823 Expected<StringTableSection *> Sec = 1824 Obj.sections().template getSectionOfType<StringTableSection>( 1825 ShstrIndex, 1826 "e_shstrndx field value " + Twine(ShstrIndex) + " in elf header " + 1827 " is invalid", 1828 "e_shstrndx field value " + Twine(ShstrIndex) + " in elf header " + 1829 " does not reference a string table"); 1830 if (!Sec) 1831 return Sec.takeError(); 1832 1833 Obj.SectionNames = *Sec; 1834 } 1835 1836 // If a section index table exists we'll need to initialize it before we 1837 // initialize the symbol table because the symbol table might need to 1838 // reference it. 1839 if (Obj.SectionIndexTable) 1840 if (Error Err = Obj.SectionIndexTable->initialize(Obj.sections())) 1841 return Err; 1842 1843 // Now that all of the sections have been added we can fill out some extra 1844 // details about symbol tables. We need the symbol table filled out before 1845 // any relocations. 1846 if (Obj.SymbolTable) { 1847 if (Error Err = Obj.SymbolTable->initialize(Obj.sections())) 1848 return Err; 1849 if (Error Err = initSymbolTable(Obj.SymbolTable)) 1850 return Err; 1851 } else if (EnsureSymtab) { 1852 if (Error Err = Obj.addNewSymbolTable()) 1853 return Err; 1854 } 1855 1856 // Now that all sections and symbols have been added we can add 1857 // relocations that reference symbols and set the link and info fields for 1858 // relocation sections. 1859 for (SectionBase &Sec : Obj.sections()) { 1860 if (&Sec == Obj.SymbolTable) 1861 continue; 1862 if (Error Err = Sec.initialize(Obj.sections())) 1863 return Err; 1864 if (auto RelSec = dyn_cast<RelocationSection>(&Sec)) { 1865 Expected<typename ELFFile<ELFT>::Elf_Shdr_Range> Sections = 1866 ElfFile.sections(); 1867 if (!Sections) 1868 return Sections.takeError(); 1869 1870 const typename ELFFile<ELFT>::Elf_Shdr *Shdr = 1871 Sections->begin() + RelSec->Index; 1872 if (RelSec->Type == SHT_REL) { 1873 Expected<typename ELFFile<ELFT>::Elf_Rel_Range> Rels = 1874 ElfFile.rels(*Shdr); 1875 if (!Rels) 1876 return Rels.takeError(); 1877 1878 if (Error Err = initRelocations(RelSec, *Rels)) 1879 return Err; 1880 } else { 1881 Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = 1882 ElfFile.relas(*Shdr); 1883 if (!Relas) 1884 return Relas.takeError(); 1885 1886 if (Error Err = initRelocations(RelSec, *Relas)) 1887 return Err; 1888 } 1889 } else if (auto GroupSec = dyn_cast<GroupSection>(&Sec)) { 1890 if (Error Err = initGroupSection(GroupSec)) 1891 return Err; 1892 } 1893 } 1894 1895 return Error::success(); 1896 } 1897 1898 template <class ELFT> Error ELFBuilder<ELFT>::build(bool EnsureSymtab) { 1899 if (Error E = readSectionHeaders()) 1900 return E; 1901 if (Error E = findEhdrOffset()) 1902 return E; 1903 1904 // The ELFFile whose ELF headers and program headers are copied into the 1905 // output file. Normally the same as ElfFile, but if we're extracting a 1906 // loadable partition it will point to the partition's headers. 1907 Expected<ELFFile<ELFT>> HeadersFile = ELFFile<ELFT>::create(toStringRef( 1908 {ElfFile.base() + EhdrOffset, ElfFile.getBufSize() - EhdrOffset})); 1909 if (!HeadersFile) 1910 return HeadersFile.takeError(); 1911 1912 const typename ELFFile<ELFT>::Elf_Ehdr &Ehdr = HeadersFile->getHeader(); 1913 Obj.OSABI = Ehdr.e_ident[EI_OSABI]; 1914 Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION]; 1915 Obj.Type = Ehdr.e_type; 1916 Obj.Machine = Ehdr.e_machine; 1917 Obj.Version = Ehdr.e_version; 1918 Obj.Entry = Ehdr.e_entry; 1919 Obj.Flags = Ehdr.e_flags; 1920 1921 if (Error E = readSections(EnsureSymtab)) 1922 return E; 1923 return readProgramHeaders(*HeadersFile); 1924 } 1925 1926 Writer::~Writer() {} 1927 1928 Reader::~Reader() {} 1929 1930 Expected<std::unique_ptr<Object>> 1931 BinaryReader::create(bool /*EnsureSymtab*/) const { 1932 return BinaryELFBuilder(MemBuf, NewSymbolVisibility).build(); 1933 } 1934 1935 Expected<std::vector<IHexRecord>> IHexReader::parse() const { 1936 SmallVector<StringRef, 16> Lines; 1937 std::vector<IHexRecord> Records; 1938 bool HasSections = false; 1939 1940 MemBuf->getBuffer().split(Lines, '\n'); 1941 Records.reserve(Lines.size()); 1942 for (size_t LineNo = 1; LineNo <= Lines.size(); ++LineNo) { 1943 StringRef Line = Lines[LineNo - 1].trim(); 1944 if (Line.empty()) 1945 continue; 1946 1947 Expected<IHexRecord> R = IHexRecord::parse(Line); 1948 if (!R) 1949 return parseError(LineNo, R.takeError()); 1950 if (R->Type == IHexRecord::EndOfFile) 1951 break; 1952 HasSections |= (R->Type == IHexRecord::Data); 1953 Records.push_back(*R); 1954 } 1955 if (!HasSections) 1956 return parseError(-1U, "no sections"); 1957 1958 return std::move(Records); 1959 } 1960 1961 Expected<std::unique_ptr<Object>> 1962 IHexReader::create(bool /*EnsureSymtab*/) const { 1963 Expected<std::vector<IHexRecord>> Records = parse(); 1964 if (!Records) 1965 return Records.takeError(); 1966 1967 return IHexELFBuilder(*Records).build(); 1968 } 1969 1970 Expected<std::unique_ptr<Object>> ELFReader::create(bool EnsureSymtab) const { 1971 auto Obj = std::make_unique<Object>(); 1972 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) { 1973 ELFBuilder<ELF32LE> Builder(*O, *Obj, ExtractPartition); 1974 if (Error Err = Builder.build(EnsureSymtab)) 1975 return std::move(Err); 1976 return std::move(Obj); 1977 } else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) { 1978 ELFBuilder<ELF64LE> Builder(*O, *Obj, ExtractPartition); 1979 if (Error Err = Builder.build(EnsureSymtab)) 1980 return std::move(Err); 1981 return std::move(Obj); 1982 } else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) { 1983 ELFBuilder<ELF32BE> Builder(*O, *Obj, ExtractPartition); 1984 if (Error Err = Builder.build(EnsureSymtab)) 1985 return std::move(Err); 1986 return std::move(Obj); 1987 } else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) { 1988 ELFBuilder<ELF64BE> Builder(*O, *Obj, ExtractPartition); 1989 if (Error Err = Builder.build(EnsureSymtab)) 1990 return std::move(Err); 1991 return std::move(Obj); 1992 } 1993 return createStringError(errc::invalid_argument, "invalid file type"); 1994 } 1995 1996 template <class ELFT> void ELFWriter<ELFT>::writeEhdr() { 1997 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf->getBufferStart()); 1998 std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0); 1999 Ehdr.e_ident[EI_MAG0] = 0x7f; 2000 Ehdr.e_ident[EI_MAG1] = 'E'; 2001 Ehdr.e_ident[EI_MAG2] = 'L'; 2002 Ehdr.e_ident[EI_MAG3] = 'F'; 2003 Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; 2004 Ehdr.e_ident[EI_DATA] = 2005 ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB; 2006 Ehdr.e_ident[EI_VERSION] = EV_CURRENT; 2007 Ehdr.e_ident[EI_OSABI] = Obj.OSABI; 2008 Ehdr.e_ident[EI_ABIVERSION] = Obj.ABIVersion; 2009 2010 Ehdr.e_type = Obj.Type; 2011 Ehdr.e_machine = Obj.Machine; 2012 Ehdr.e_version = Obj.Version; 2013 Ehdr.e_entry = Obj.Entry; 2014 // We have to use the fully-qualified name llvm::size 2015 // since some compilers complain on ambiguous resolution. 2016 Ehdr.e_phnum = llvm::size(Obj.segments()); 2017 Ehdr.e_phoff = (Ehdr.e_phnum != 0) ? Obj.ProgramHdrSegment.Offset : 0; 2018 Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0; 2019 Ehdr.e_flags = Obj.Flags; 2020 Ehdr.e_ehsize = sizeof(Elf_Ehdr); 2021 if (WriteSectionHeaders && Obj.sections().size() != 0) { 2022 Ehdr.e_shentsize = sizeof(Elf_Shdr); 2023 Ehdr.e_shoff = Obj.SHOff; 2024 // """ 2025 // If the number of sections is greater than or equal to 2026 // SHN_LORESERVE (0xff00), this member has the value zero and the actual 2027 // number of section header table entries is contained in the sh_size field 2028 // of the section header at index 0. 2029 // """ 2030 auto Shnum = Obj.sections().size() + 1; 2031 if (Shnum >= SHN_LORESERVE) 2032 Ehdr.e_shnum = 0; 2033 else 2034 Ehdr.e_shnum = Shnum; 2035 // """ 2036 // If the section name string table section index is greater than or equal 2037 // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff) 2038 // and the actual index of the section name string table section is 2039 // contained in the sh_link field of the section header at index 0. 2040 // """ 2041 if (Obj.SectionNames->Index >= SHN_LORESERVE) 2042 Ehdr.e_shstrndx = SHN_XINDEX; 2043 else 2044 Ehdr.e_shstrndx = Obj.SectionNames->Index; 2045 } else { 2046 Ehdr.e_shentsize = 0; 2047 Ehdr.e_shoff = 0; 2048 Ehdr.e_shnum = 0; 2049 Ehdr.e_shstrndx = 0; 2050 } 2051 } 2052 2053 template <class ELFT> void ELFWriter<ELFT>::writePhdrs() { 2054 for (auto &Seg : Obj.segments()) 2055 writePhdr(Seg); 2056 } 2057 2058 template <class ELFT> void ELFWriter<ELFT>::writeShdrs() { 2059 // This reference serves to write the dummy section header at the begining 2060 // of the file. It is not used for anything else 2061 Elf_Shdr &Shdr = 2062 *reinterpret_cast<Elf_Shdr *>(Buf->getBufferStart() + Obj.SHOff); 2063 Shdr.sh_name = 0; 2064 Shdr.sh_type = SHT_NULL; 2065 Shdr.sh_flags = 0; 2066 Shdr.sh_addr = 0; 2067 Shdr.sh_offset = 0; 2068 // See writeEhdr for why we do this. 2069 uint64_t Shnum = Obj.sections().size() + 1; 2070 if (Shnum >= SHN_LORESERVE) 2071 Shdr.sh_size = Shnum; 2072 else 2073 Shdr.sh_size = 0; 2074 // See writeEhdr for why we do this. 2075 if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE) 2076 Shdr.sh_link = Obj.SectionNames->Index; 2077 else 2078 Shdr.sh_link = 0; 2079 Shdr.sh_info = 0; 2080 Shdr.sh_addralign = 0; 2081 Shdr.sh_entsize = 0; 2082 2083 for (SectionBase &Sec : Obj.sections()) 2084 writeShdr(Sec); 2085 } 2086 2087 template <class ELFT> Error ELFWriter<ELFT>::writeSectionData() { 2088 for (SectionBase &Sec : Obj.sections()) 2089 // Segments are responsible for writing their contents, so only write the 2090 // section data if the section is not in a segment. Note that this renders 2091 // sections in segments effectively immutable. 2092 if (Sec.ParentSegment == nullptr) 2093 if (Error Err = Sec.accept(*SecWriter)) 2094 return Err; 2095 2096 return Error::success(); 2097 } 2098 2099 template <class ELFT> void ELFWriter<ELFT>::writeSegmentData() { 2100 for (Segment &Seg : Obj.segments()) { 2101 size_t Size = std::min<size_t>(Seg.FileSize, Seg.getContents().size()); 2102 std::memcpy(Buf->getBufferStart() + Seg.Offset, Seg.getContents().data(), 2103 Size); 2104 } 2105 2106 for (auto it : Obj.getUpdatedSections()) { 2107 SectionBase *Sec = it.first; 2108 ArrayRef<uint8_t> Data = it.second; 2109 2110 auto *Parent = Sec->ParentSegment; 2111 assert(Parent && "This section should've been part of a segment."); 2112 uint64_t Offset = 2113 Sec->OriginalOffset - Parent->OriginalOffset + Parent->Offset; 2114 llvm::copy(Data, Buf->getBufferStart() + Offset); 2115 } 2116 2117 // Iterate over removed sections and overwrite their old data with zeroes. 2118 for (auto &Sec : Obj.removedSections()) { 2119 Segment *Parent = Sec.ParentSegment; 2120 if (Parent == nullptr || Sec.Type == SHT_NOBITS || Sec.Size == 0) 2121 continue; 2122 uint64_t Offset = 2123 Sec.OriginalOffset - Parent->OriginalOffset + Parent->Offset; 2124 std::memset(Buf->getBufferStart() + Offset, 0, Sec.Size); 2125 } 2126 } 2127 2128 template <class ELFT> 2129 ELFWriter<ELFT>::ELFWriter(Object &Obj, raw_ostream &Buf, bool WSH, 2130 bool OnlyKeepDebug) 2131 : Writer(Obj, Buf), WriteSectionHeaders(WSH && Obj.HadShdrs), 2132 OnlyKeepDebug(OnlyKeepDebug) {} 2133 2134 Error Object::updateSection(StringRef Name, ArrayRef<uint8_t> Data) { 2135 auto It = llvm::find_if(Sections, 2136 [&](const SecPtr &Sec) { return Sec->Name == Name; }); 2137 if (It == Sections.end()) 2138 return createStringError(errc::invalid_argument, "section '%s' not found", 2139 Name.str().c_str()); 2140 2141 auto *OldSec = It->get(); 2142 if (!OldSec->hasContents()) 2143 return createStringError( 2144 errc::invalid_argument, 2145 "section '%s' cannot be updated because it does not have contents", 2146 Name.str().c_str()); 2147 2148 if (Data.size() > OldSec->Size && OldSec->ParentSegment) 2149 return createStringError(errc::invalid_argument, 2150 "cannot fit data of size %zu into section '%s' " 2151 "with size %zu that is part of a segment", 2152 Data.size(), Name.str().c_str(), OldSec->Size); 2153 2154 if (!OldSec->ParentSegment) { 2155 *It = std::make_unique<OwnedDataSection>(*OldSec, Data); 2156 } else { 2157 // The segment writer will be in charge of updating these contents. 2158 OldSec->Size = Data.size(); 2159 UpdatedSections[OldSec] = Data; 2160 } 2161 2162 return Error::success(); 2163 } 2164 2165 Error Object::removeSections( 2166 bool AllowBrokenLinks, std::function<bool(const SectionBase &)> ToRemove) { 2167 2168 auto Iter = std::stable_partition( 2169 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) { 2170 if (ToRemove(*Sec)) 2171 return false; 2172 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) { 2173 if (auto ToRelSec = RelSec->getSection()) 2174 return !ToRemove(*ToRelSec); 2175 } 2176 return true; 2177 }); 2178 if (SymbolTable != nullptr && ToRemove(*SymbolTable)) 2179 SymbolTable = nullptr; 2180 if (SectionNames != nullptr && ToRemove(*SectionNames)) 2181 SectionNames = nullptr; 2182 if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable)) 2183 SectionIndexTable = nullptr; 2184 // Now make sure there are no remaining references to the sections that will 2185 // be removed. Sometimes it is impossible to remove a reference so we emit 2186 // an error here instead. 2187 std::unordered_set<const SectionBase *> RemoveSections; 2188 RemoveSections.reserve(std::distance(Iter, std::end(Sections))); 2189 for (auto &RemoveSec : make_range(Iter, std::end(Sections))) { 2190 for (auto &Segment : Segments) 2191 Segment->removeSection(RemoveSec.get()); 2192 RemoveSec->onRemove(); 2193 RemoveSections.insert(RemoveSec.get()); 2194 } 2195 2196 // For each section that remains alive, we want to remove the dead references. 2197 // This either might update the content of the section (e.g. remove symbols 2198 // from symbol table that belongs to removed section) or trigger an error if 2199 // a live section critically depends on a section being removed somehow 2200 // (e.g. the removed section is referenced by a relocation). 2201 for (auto &KeepSec : make_range(std::begin(Sections), Iter)) { 2202 if (Error E = KeepSec->removeSectionReferences( 2203 AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) { 2204 return RemoveSections.find(Sec) != RemoveSections.end(); 2205 })) 2206 return E; 2207 } 2208 2209 // Transfer removed sections into the Object RemovedSections container for use 2210 // later. 2211 std::move(Iter, Sections.end(), std::back_inserter(RemovedSections)); 2212 // Now finally get rid of them all together. 2213 Sections.erase(Iter, std::end(Sections)); 2214 return Error::success(); 2215 } 2216 2217 Error Object::replaceSections( 2218 const DenseMap<SectionBase *, SectionBase *> &FromTo) { 2219 auto SectionIndexLess = [](const SecPtr &Lhs, const SecPtr &Rhs) { 2220 return Lhs->Index < Rhs->Index; 2221 }; 2222 assert(llvm::is_sorted(Sections, SectionIndexLess) && 2223 "Sections are expected to be sorted by Index"); 2224 // Set indices of new sections so that they can be later sorted into positions 2225 // of removed ones. 2226 for (auto &I : FromTo) 2227 I.second->Index = I.first->Index; 2228 2229 // Notify all sections about the replacement. 2230 for (auto &Sec : Sections) 2231 Sec->replaceSectionReferences(FromTo); 2232 2233 if (Error E = removeSections( 2234 /*AllowBrokenLinks=*/false, 2235 [=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; })) 2236 return E; 2237 llvm::sort(Sections, SectionIndexLess); 2238 return Error::success(); 2239 } 2240 2241 Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { 2242 if (SymbolTable) 2243 for (const SecPtr &Sec : Sections) 2244 if (Error E = Sec->removeSymbols(ToRemove)) 2245 return E; 2246 return Error::success(); 2247 } 2248 2249 Error Object::addNewSymbolTable() { 2250 assert(!SymbolTable && "Object must not has a SymbolTable."); 2251 2252 // Reuse an existing SHT_STRTAB section if it exists. 2253 StringTableSection *StrTab = nullptr; 2254 for (SectionBase &Sec : sections()) { 2255 if (Sec.Type == ELF::SHT_STRTAB && !(Sec.Flags & SHF_ALLOC)) { 2256 StrTab = static_cast<StringTableSection *>(&Sec); 2257 2258 // Prefer a string table that is not the section header string table, if 2259 // such a table exists. 2260 if (SectionNames != &Sec) 2261 break; 2262 } 2263 } 2264 if (!StrTab) 2265 StrTab = &addSection<StringTableSection>(); 2266 2267 SymbolTableSection &SymTab = addSection<SymbolTableSection>(); 2268 SymTab.Name = ".symtab"; 2269 SymTab.Link = StrTab->Index; 2270 if (Error Err = SymTab.initialize(sections())) 2271 return Err; 2272 SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0); 2273 2274 SymbolTable = &SymTab; 2275 2276 return Error::success(); 2277 } 2278 2279 // Orders segments such that if x = y->ParentSegment then y comes before x. 2280 static void orderSegments(std::vector<Segment *> &Segments) { 2281 llvm::stable_sort(Segments, compareSegmentsByOffset); 2282 } 2283 2284 // This function finds a consistent layout for a list of segments starting from 2285 // an Offset. It assumes that Segments have been sorted by orderSegments and 2286 // returns an Offset one past the end of the last segment. 2287 static uint64_t layoutSegments(std::vector<Segment *> &Segments, 2288 uint64_t Offset) { 2289 assert(llvm::is_sorted(Segments, compareSegmentsByOffset)); 2290 // The only way a segment should move is if a section was between two 2291 // segments and that section was removed. If that section isn't in a segment 2292 // then it's acceptable, but not ideal, to simply move it to after the 2293 // segments. So we can simply layout segments one after the other accounting 2294 // for alignment. 2295 for (Segment *Seg : Segments) { 2296 // We assume that segments have been ordered by OriginalOffset and Index 2297 // such that a parent segment will always come before a child segment in 2298 // OrderedSegments. This means that the Offset of the ParentSegment should 2299 // already be set and we can set our offset relative to it. 2300 if (Seg->ParentSegment != nullptr) { 2301 Segment *Parent = Seg->ParentSegment; 2302 Seg->Offset = 2303 Parent->Offset + Seg->OriginalOffset - Parent->OriginalOffset; 2304 } else { 2305 Seg->Offset = 2306 alignTo(Offset, std::max<uint64_t>(Seg->Align, 1), Seg->VAddr); 2307 } 2308 Offset = std::max(Offset, Seg->Offset + Seg->FileSize); 2309 } 2310 return Offset; 2311 } 2312 2313 // This function finds a consistent layout for a list of sections. It assumes 2314 // that the ->ParentSegment of each section has already been laid out. The 2315 // supplied starting Offset is used for the starting offset of any section that 2316 // does not have a ParentSegment. It returns either the offset given if all 2317 // sections had a ParentSegment or an offset one past the last section if there 2318 // was a section that didn't have a ParentSegment. 2319 template <class Range> 2320 static uint64_t layoutSections(Range Sections, uint64_t Offset) { 2321 // Now the offset of every segment has been set we can assign the offsets 2322 // of each section. For sections that are covered by a segment we should use 2323 // the segment's original offset and the section's original offset to compute 2324 // the offset from the start of the segment. Using the offset from the start 2325 // of the segment we can assign a new offset to the section. For sections not 2326 // covered by segments we can just bump Offset to the next valid location. 2327 // While it is not necessary, layout the sections in the order based on their 2328 // original offsets to resemble the input file as close as possible. 2329 std::vector<SectionBase *> OutOfSegmentSections; 2330 uint32_t Index = 1; 2331 for (auto &Sec : Sections) { 2332 Sec.Index = Index++; 2333 if (Sec.ParentSegment != nullptr) { 2334 auto Segment = *Sec.ParentSegment; 2335 Sec.Offset = 2336 Segment.Offset + (Sec.OriginalOffset - Segment.OriginalOffset); 2337 } else 2338 OutOfSegmentSections.push_back(&Sec); 2339 } 2340 2341 llvm::stable_sort(OutOfSegmentSections, 2342 [](const SectionBase *Lhs, const SectionBase *Rhs) { 2343 return Lhs->OriginalOffset < Rhs->OriginalOffset; 2344 }); 2345 for (auto *Sec : OutOfSegmentSections) { 2346 Offset = alignTo(Offset, Sec->Align == 0 ? 1 : Sec->Align); 2347 Sec->Offset = Offset; 2348 if (Sec->Type != SHT_NOBITS) 2349 Offset += Sec->Size; 2350 } 2351 return Offset; 2352 } 2353 2354 // Rewrite sh_offset after some sections are changed to SHT_NOBITS and thus 2355 // occupy no space in the file. 2356 static uint64_t layoutSectionsForOnlyKeepDebug(Object &Obj, uint64_t Off) { 2357 // The layout algorithm requires the sections to be handled in the order of 2358 // their offsets in the input file, at least inside segments. 2359 std::vector<SectionBase *> Sections; 2360 Sections.reserve(Obj.sections().size()); 2361 uint32_t Index = 1; 2362 for (auto &Sec : Obj.sections()) { 2363 Sec.Index = Index++; 2364 Sections.push_back(&Sec); 2365 } 2366 llvm::stable_sort(Sections, 2367 [](const SectionBase *Lhs, const SectionBase *Rhs) { 2368 return Lhs->OriginalOffset < Rhs->OriginalOffset; 2369 }); 2370 2371 for (auto *Sec : Sections) { 2372 auto *FirstSec = Sec->ParentSegment && Sec->ParentSegment->Type == PT_LOAD 2373 ? Sec->ParentSegment->firstSection() 2374 : nullptr; 2375 2376 // The first section in a PT_LOAD has to have congruent offset and address 2377 // modulo the alignment, which usually equals the maximum page size. 2378 if (FirstSec && FirstSec == Sec) 2379 Off = alignTo(Off, Sec->ParentSegment->Align, Sec->Addr); 2380 2381 // sh_offset is not significant for SHT_NOBITS sections, but the congruence 2382 // rule must be followed if it is the first section in a PT_LOAD. Do not 2383 // advance Off. 2384 if (Sec->Type == SHT_NOBITS) { 2385 Sec->Offset = Off; 2386 continue; 2387 } 2388 2389 if (!FirstSec) { 2390 // FirstSec being nullptr generally means that Sec does not have the 2391 // SHF_ALLOC flag. 2392 Off = Sec->Align ? alignTo(Off, Sec->Align) : Off; 2393 } else if (FirstSec != Sec) { 2394 // The offset is relative to the first section in the PT_LOAD segment. Use 2395 // sh_offset for non-SHF_ALLOC sections. 2396 Off = Sec->OriginalOffset - FirstSec->OriginalOffset + FirstSec->Offset; 2397 } 2398 Sec->Offset = Off; 2399 Off += Sec->Size; 2400 } 2401 return Off; 2402 } 2403 2404 // Rewrite p_offset and p_filesz of non-PT_PHDR segments after sh_offset values 2405 // have been updated. 2406 static uint64_t layoutSegmentsForOnlyKeepDebug(std::vector<Segment *> &Segments, 2407 uint64_t HdrEnd) { 2408 uint64_t MaxOffset = 0; 2409 for (Segment *Seg : Segments) { 2410 if (Seg->Type == PT_PHDR) 2411 continue; 2412 2413 // The segment offset is generally the offset of the first section. 2414 // 2415 // For a segment containing no section (see sectionWithinSegment), if it has 2416 // a parent segment, copy the parent segment's offset field. This works for 2417 // empty PT_TLS. If no parent segment, use 0: the segment is not useful for 2418 // debugging anyway. 2419 const SectionBase *FirstSec = Seg->firstSection(); 2420 uint64_t Offset = 2421 FirstSec ? FirstSec->Offset 2422 : (Seg->ParentSegment ? Seg->ParentSegment->Offset : 0); 2423 uint64_t FileSize = 0; 2424 for (const SectionBase *Sec : Seg->Sections) { 2425 uint64_t Size = Sec->Type == SHT_NOBITS ? 0 : Sec->Size; 2426 if (Sec->Offset + Size > Offset) 2427 FileSize = std::max(FileSize, Sec->Offset + Size - Offset); 2428 } 2429 2430 // If the segment includes EHDR and program headers, don't make it smaller 2431 // than the headers. 2432 if (Seg->Offset < HdrEnd && HdrEnd <= Seg->Offset + Seg->FileSize) { 2433 FileSize += Offset - Seg->Offset; 2434 Offset = Seg->Offset; 2435 FileSize = std::max(FileSize, HdrEnd - Offset); 2436 } 2437 2438 Seg->Offset = Offset; 2439 Seg->FileSize = FileSize; 2440 MaxOffset = std::max(MaxOffset, Offset + FileSize); 2441 } 2442 return MaxOffset; 2443 } 2444 2445 template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() { 2446 Segment &ElfHdr = Obj.ElfHdrSegment; 2447 ElfHdr.Type = PT_PHDR; 2448 ElfHdr.Flags = 0; 2449 ElfHdr.VAddr = 0; 2450 ElfHdr.PAddr = 0; 2451 ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr); 2452 ElfHdr.Align = 0; 2453 } 2454 2455 template <class ELFT> void ELFWriter<ELFT>::assignOffsets() { 2456 // We need a temporary list of segments that has a special order to it 2457 // so that we know that anytime ->ParentSegment is set that segment has 2458 // already had its offset properly set. 2459 std::vector<Segment *> OrderedSegments; 2460 for (Segment &Segment : Obj.segments()) 2461 OrderedSegments.push_back(&Segment); 2462 OrderedSegments.push_back(&Obj.ElfHdrSegment); 2463 OrderedSegments.push_back(&Obj.ProgramHdrSegment); 2464 orderSegments(OrderedSegments); 2465 2466 uint64_t Offset; 2467 if (OnlyKeepDebug) { 2468 // For --only-keep-debug, the sections that did not preserve contents were 2469 // changed to SHT_NOBITS. We now rewrite sh_offset fields of sections, and 2470 // then rewrite p_offset/p_filesz of program headers. 2471 uint64_t HdrEnd = 2472 sizeof(Elf_Ehdr) + llvm::size(Obj.segments()) * sizeof(Elf_Phdr); 2473 Offset = layoutSectionsForOnlyKeepDebug(Obj, HdrEnd); 2474 Offset = std::max(Offset, 2475 layoutSegmentsForOnlyKeepDebug(OrderedSegments, HdrEnd)); 2476 } else { 2477 // Offset is used as the start offset of the first segment to be laid out. 2478 // Since the ELF Header (ElfHdrSegment) must be at the start of the file, 2479 // we start at offset 0. 2480 Offset = layoutSegments(OrderedSegments, 0); 2481 Offset = layoutSections(Obj.sections(), Offset); 2482 } 2483 // If we need to write the section header table out then we need to align the 2484 // Offset so that SHOffset is valid. 2485 if (WriteSectionHeaders) 2486 Offset = alignTo(Offset, sizeof(Elf_Addr)); 2487 Obj.SHOff = Offset; 2488 } 2489 2490 template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const { 2491 // We already have the section header offset so we can calculate the total 2492 // size by just adding up the size of each section header. 2493 if (!WriteSectionHeaders) 2494 return Obj.SHOff; 2495 size_t ShdrCount = Obj.sections().size() + 1; // Includes null shdr. 2496 return Obj.SHOff + ShdrCount * sizeof(Elf_Shdr); 2497 } 2498 2499 template <class ELFT> Error ELFWriter<ELFT>::write() { 2500 // Segment data must be written first, so that the ELF header and program 2501 // header tables can overwrite it, if covered by a segment. 2502 writeSegmentData(); 2503 writeEhdr(); 2504 writePhdrs(); 2505 if (Error E = writeSectionData()) 2506 return E; 2507 if (WriteSectionHeaders) 2508 writeShdrs(); 2509 2510 // TODO: Implement direct writing to the output stream (without intermediate 2511 // memory buffer Buf). 2512 Out.write(Buf->getBufferStart(), Buf->getBufferSize()); 2513 return Error::success(); 2514 } 2515 2516 static Error removeUnneededSections(Object &Obj) { 2517 // We can remove an empty symbol table from non-relocatable objects. 2518 // Relocatable objects typically have relocation sections whose 2519 // sh_link field points to .symtab, so we can't remove .symtab 2520 // even if it is empty. 2521 if (Obj.isRelocatable() || Obj.SymbolTable == nullptr || 2522 !Obj.SymbolTable->empty()) 2523 return Error::success(); 2524 2525 // .strtab can be used for section names. In such a case we shouldn't 2526 // remove it. 2527 auto *StrTab = Obj.SymbolTable->getStrTab() == Obj.SectionNames 2528 ? nullptr 2529 : Obj.SymbolTable->getStrTab(); 2530 return Obj.removeSections(false, [&](const SectionBase &Sec) { 2531 return &Sec == Obj.SymbolTable || &Sec == StrTab; 2532 }); 2533 } 2534 2535 template <class ELFT> Error ELFWriter<ELFT>::finalize() { 2536 // It could happen that SectionNames has been removed and yet the user wants 2537 // a section header table output. We need to throw an error if a user tries 2538 // to do that. 2539 if (Obj.SectionNames == nullptr && WriteSectionHeaders) 2540 return createStringError(llvm::errc::invalid_argument, 2541 "cannot write section header table because " 2542 "section header string table was removed"); 2543 2544 if (Error E = removeUnneededSections(Obj)) 2545 return E; 2546 2547 // We need to assign indexes before we perform layout because we need to know 2548 // if we need large indexes or not. We can assign indexes first and check as 2549 // we go to see if we will actully need large indexes. 2550 bool NeedsLargeIndexes = false; 2551 if (Obj.sections().size() >= SHN_LORESERVE) { 2552 SectionTableRef Sections = Obj.sections(); 2553 // Sections doesn't include the null section header, so account for this 2554 // when skipping the first N sections. 2555 NeedsLargeIndexes = 2556 any_of(drop_begin(Sections, SHN_LORESERVE - 1), 2557 [](const SectionBase &Sec) { return Sec.HasSymbol; }); 2558 // TODO: handle case where only one section needs the large index table but 2559 // only needs it because the large index table hasn't been removed yet. 2560 } 2561 2562 if (NeedsLargeIndexes) { 2563 // This means we definitely need to have a section index table but if we 2564 // already have one then we should use it instead of making a new one. 2565 if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) { 2566 // Addition of a section to the end does not invalidate the indexes of 2567 // other sections and assigns the correct index to the new section. 2568 auto &Shndx = Obj.addSection<SectionIndexSection>(); 2569 Obj.SymbolTable->setShndxTable(&Shndx); 2570 Shndx.setSymTab(Obj.SymbolTable); 2571 } 2572 } else { 2573 // Since we don't need SectionIndexTable we should remove it and all 2574 // references to it. 2575 if (Obj.SectionIndexTable != nullptr) { 2576 // We do not support sections referring to the section index table. 2577 if (Error E = Obj.removeSections(false /*AllowBrokenLinks*/, 2578 [this](const SectionBase &Sec) { 2579 return &Sec == Obj.SectionIndexTable; 2580 })) 2581 return E; 2582 } 2583 } 2584 2585 // Make sure we add the names of all the sections. Importantly this must be 2586 // done after we decide to add or remove SectionIndexes. 2587 if (Obj.SectionNames != nullptr) 2588 for (const SectionBase &Sec : Obj.sections()) 2589 Obj.SectionNames->addString(Sec.Name); 2590 2591 initEhdrSegment(); 2592 2593 // Before we can prepare for layout the indexes need to be finalized. 2594 // Also, the output arch may not be the same as the input arch, so fix up 2595 // size-related fields before doing layout calculations. 2596 uint64_t Index = 0; 2597 auto SecSizer = std::make_unique<ELFSectionSizer<ELFT>>(); 2598 for (SectionBase &Sec : Obj.sections()) { 2599 Sec.Index = Index++; 2600 if (Error Err = Sec.accept(*SecSizer)) 2601 return Err; 2602 } 2603 2604 // The symbol table does not update all other sections on update. For 2605 // instance, symbol names are not added as new symbols are added. This means 2606 // that some sections, like .strtab, don't yet have their final size. 2607 if (Obj.SymbolTable != nullptr) 2608 Obj.SymbolTable->prepareForLayout(); 2609 2610 // Now that all strings are added we want to finalize string table builders, 2611 // because that affects section sizes which in turn affects section offsets. 2612 for (SectionBase &Sec : Obj.sections()) 2613 if (auto StrTab = dyn_cast<StringTableSection>(&Sec)) 2614 StrTab->prepareForLayout(); 2615 2616 assignOffsets(); 2617 2618 // layoutSections could have modified section indexes, so we need 2619 // to fill the index table after assignOffsets. 2620 if (Obj.SymbolTable != nullptr) 2621 Obj.SymbolTable->fillShndxTable(); 2622 2623 // Finally now that all offsets and indexes have been set we can finalize any 2624 // remaining issues. 2625 uint64_t Offset = Obj.SHOff + sizeof(Elf_Shdr); 2626 for (SectionBase &Sec : Obj.sections()) { 2627 Sec.HeaderOffset = Offset; 2628 Offset += sizeof(Elf_Shdr); 2629 if (WriteSectionHeaders) 2630 Sec.NameIndex = Obj.SectionNames->findIndex(Sec.Name); 2631 Sec.finalize(); 2632 } 2633 2634 size_t TotalSize = totalSize(); 2635 Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize); 2636 if (!Buf) 2637 return createStringError(errc::not_enough_memory, 2638 "failed to allocate memory buffer of " + 2639 Twine::utohexstr(TotalSize) + " bytes"); 2640 2641 SecWriter = std::make_unique<ELFSectionWriter<ELFT>>(*Buf); 2642 return Error::success(); 2643 } 2644 2645 Error BinaryWriter::write() { 2646 for (const SectionBase &Sec : Obj.allocSections()) 2647 if (Error Err = Sec.accept(*SecWriter)) 2648 return Err; 2649 2650 // TODO: Implement direct writing to the output stream (without intermediate 2651 // memory buffer Buf). 2652 Out.write(Buf->getBufferStart(), Buf->getBufferSize()); 2653 return Error::success(); 2654 } 2655 2656 Error BinaryWriter::finalize() { 2657 // Compute the section LMA based on its sh_offset and the containing segment's 2658 // p_offset and p_paddr. Also compute the minimum LMA of all non-empty 2659 // sections as MinAddr. In the output, the contents between address 0 and 2660 // MinAddr will be skipped. 2661 uint64_t MinAddr = UINT64_MAX; 2662 for (SectionBase &Sec : Obj.allocSections()) { 2663 if (Sec.ParentSegment != nullptr) 2664 Sec.Addr = 2665 Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr; 2666 if (Sec.Type != SHT_NOBITS && Sec.Size > 0) 2667 MinAddr = std::min(MinAddr, Sec.Addr); 2668 } 2669 2670 // Now that every section has been laid out we just need to compute the total 2671 // file size. This might not be the same as the offset returned by 2672 // layoutSections, because we want to truncate the last segment to the end of 2673 // its last non-empty section, to match GNU objcopy's behaviour. 2674 TotalSize = 0; 2675 for (SectionBase &Sec : Obj.allocSections()) 2676 if (Sec.Type != SHT_NOBITS && Sec.Size > 0) { 2677 Sec.Offset = Sec.Addr - MinAddr; 2678 TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size); 2679 } 2680 2681 Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize); 2682 if (!Buf) 2683 return createStringError(errc::not_enough_memory, 2684 "failed to allocate memory buffer of " + 2685 Twine::utohexstr(TotalSize) + " bytes"); 2686 SecWriter = std::make_unique<BinarySectionWriter>(*Buf); 2687 return Error::success(); 2688 } 2689 2690 bool IHexWriter::SectionCompare::operator()(const SectionBase *Lhs, 2691 const SectionBase *Rhs) const { 2692 return (sectionPhysicalAddr(Lhs) & 0xFFFFFFFFU) < 2693 (sectionPhysicalAddr(Rhs) & 0xFFFFFFFFU); 2694 } 2695 2696 uint64_t IHexWriter::writeEntryPointRecord(uint8_t *Buf) { 2697 IHexLineData HexData; 2698 uint8_t Data[4] = {}; 2699 // We don't write entry point record if entry is zero. 2700 if (Obj.Entry == 0) 2701 return 0; 2702 2703 if (Obj.Entry <= 0xFFFFFU) { 2704 Data[0] = ((Obj.Entry & 0xF0000U) >> 12) & 0xFF; 2705 support::endian::write(&Data[2], static_cast<uint16_t>(Obj.Entry), 2706 support::big); 2707 HexData = IHexRecord::getLine(IHexRecord::StartAddr80x86, 0, Data); 2708 } else { 2709 support::endian::write(Data, static_cast<uint32_t>(Obj.Entry), 2710 support::big); 2711 HexData = IHexRecord::getLine(IHexRecord::StartAddr, 0, Data); 2712 } 2713 memcpy(Buf, HexData.data(), HexData.size()); 2714 return HexData.size(); 2715 } 2716 2717 uint64_t IHexWriter::writeEndOfFileRecord(uint8_t *Buf) { 2718 IHexLineData HexData = IHexRecord::getLine(IHexRecord::EndOfFile, 0, {}); 2719 memcpy(Buf, HexData.data(), HexData.size()); 2720 return HexData.size(); 2721 } 2722 2723 Error IHexWriter::write() { 2724 IHexSectionWriter Writer(*Buf); 2725 // Write sections. 2726 for (const SectionBase *Sec : Sections) 2727 if (Error Err = Sec->accept(Writer)) 2728 return Err; 2729 2730 uint64_t Offset = Writer.getBufferOffset(); 2731 // Write entry point address. 2732 Offset += writeEntryPointRecord( 2733 reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + Offset); 2734 // Write EOF. 2735 Offset += writeEndOfFileRecord( 2736 reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + Offset); 2737 assert(Offset == TotalSize); 2738 2739 // TODO: Implement direct writing to the output stream (without intermediate 2740 // memory buffer Buf). 2741 Out.write(Buf->getBufferStart(), Buf->getBufferSize()); 2742 return Error::success(); 2743 } 2744 2745 Error IHexWriter::checkSection(const SectionBase &Sec) { 2746 uint64_t Addr = sectionPhysicalAddr(&Sec); 2747 if (addressOverflows32bit(Addr) || addressOverflows32bit(Addr + Sec.Size - 1)) 2748 return createStringError( 2749 errc::invalid_argument, 2750 "Section '%s' address range [0x%llx, 0x%llx] is not 32 bit", 2751 Sec.Name.c_str(), Addr, Addr + Sec.Size - 1); 2752 return Error::success(); 2753 } 2754 2755 Error IHexWriter::finalize() { 2756 // We can't write 64-bit addresses. 2757 if (addressOverflows32bit(Obj.Entry)) 2758 return createStringError(errc::invalid_argument, 2759 "Entry point address 0x%llx overflows 32 bits", 2760 Obj.Entry); 2761 2762 for (const SectionBase &Sec : Obj.sections()) 2763 if ((Sec.Flags & ELF::SHF_ALLOC) && Sec.Type != ELF::SHT_NOBITS && 2764 Sec.Size > 0) { 2765 if (Error E = checkSection(Sec)) 2766 return E; 2767 Sections.insert(&Sec); 2768 } 2769 2770 std::unique_ptr<WritableMemoryBuffer> EmptyBuffer = 2771 WritableMemoryBuffer::getNewMemBuffer(0); 2772 if (!EmptyBuffer) 2773 return createStringError(errc::not_enough_memory, 2774 "failed to allocate memory buffer of 0 bytes"); 2775 2776 IHexSectionWriterBase LengthCalc(*EmptyBuffer); 2777 for (const SectionBase *Sec : Sections) 2778 if (Error Err = Sec->accept(LengthCalc)) 2779 return Err; 2780 2781 // We need space to write section records + StartAddress record 2782 // (if start adress is not zero) + EndOfFile record. 2783 TotalSize = LengthCalc.getBufferOffset() + 2784 (Obj.Entry ? IHexRecord::getLineLength(4) : 0) + 2785 IHexRecord::getLineLength(0); 2786 2787 Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize); 2788 if (!Buf) 2789 return createStringError(errc::not_enough_memory, 2790 "failed to allocate memory buffer of " + 2791 Twine::utohexstr(TotalSize) + " bytes"); 2792 2793 return Error::success(); 2794 } 2795 2796 namespace llvm { 2797 namespace objcopy { 2798 namespace elf { 2799 2800 template class ELFBuilder<ELF64LE>; 2801 template class ELFBuilder<ELF64BE>; 2802 template class ELFBuilder<ELF32LE>; 2803 template class ELFBuilder<ELF32BE>; 2804 2805 template class ELFWriter<ELF64LE>; 2806 template class ELFWriter<ELF64BE>; 2807 template class ELFWriter<ELF32LE>; 2808 template class ELFWriter<ELF32BE>; 2809 2810 } // end namespace elf 2811 } // end namespace objcopy 2812 } // end namespace llvm 2813