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