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