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