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