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