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