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