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