1 //===- Object.cpp ---------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "llvm-objcopy.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/ADT/iterator_range.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/Object/ELFObjectFile.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/FileOutputBuffer.h"
21 #include "llvm/Support/Path.h"
30 using namespace object
;
33 template <class ELFT
> void ELFWriter
<ELFT
>::writePhdr(const Segment
&Seg
) {
34 using Elf_Phdr
= typename
ELFT::Phdr
;
36 uint8_t *Buf
= BufPtr
->getBufferStart();
37 Buf
+= Obj
.ProgramHdrSegment
.Offset
+ Seg
.Index
* sizeof(Elf_Phdr
);
38 Elf_Phdr
&Phdr
= *reinterpret_cast<Elf_Phdr
*>(Buf
);
39 Phdr
.p_type
= Seg
.Type
;
40 Phdr
.p_flags
= Seg
.Flags
;
41 Phdr
.p_offset
= Seg
.Offset
;
42 Phdr
.p_vaddr
= Seg
.VAddr
;
43 Phdr
.p_paddr
= Seg
.PAddr
;
44 Phdr
.p_filesz
= Seg
.FileSize
;
45 Phdr
.p_memsz
= Seg
.MemSize
;
46 Phdr
.p_align
= Seg
.Align
;
49 void SectionBase::removeSectionReferences(const SectionBase
*Sec
) {}
50 void SectionBase::initialize(SectionTableRef SecTable
) {}
51 void SectionBase::finalize() {}
53 template <class ELFT
> void ELFWriter
<ELFT
>::writeShdr(const SectionBase
&Sec
) {
54 uint8_t *Buf
= BufPtr
->getBufferStart();
55 Buf
+= Sec
.HeaderOffset
;
56 typename
ELFT::Shdr
&Shdr
= *reinterpret_cast<typename
ELFT::Shdr
*>(Buf
);
57 Shdr
.sh_name
= Sec
.NameIndex
;
58 Shdr
.sh_type
= Sec
.Type
;
59 Shdr
.sh_flags
= Sec
.Flags
;
60 Shdr
.sh_addr
= Sec
.Addr
;
61 Shdr
.sh_offset
= Sec
.Offset
;
62 Shdr
.sh_size
= Sec
.Size
;
63 Shdr
.sh_link
= Sec
.Link
;
64 Shdr
.sh_info
= Sec
.Info
;
65 Shdr
.sh_addralign
= Sec
.Align
;
66 Shdr
.sh_entsize
= Sec
.EntrySize
;
69 SectionVisitor::~SectionVisitor() {}
71 void BinarySectionWriter::visit(const SymbolTableSection
&Sec
) {
72 error("Cannot write symbol table '" + Sec
.Name
+ "' out to binary");
75 void BinarySectionWriter::visit(const RelocationSection
&Sec
) {
76 error("Cannot write relocation section '" + Sec
.Name
+ "' out to binary");
79 void BinarySectionWriter::visit(const GnuDebugLinkSection
&Sec
) {
80 error("Cannot write '.gnu_debuglink' out to binary");
83 void SectionWriter::visit(const Section
&Sec
) {
84 if (Sec
.Type
== SHT_NOBITS
)
86 uint8_t *Buf
= Out
.getBufferStart() + Sec
.Offset
;
87 std::copy(std::begin(Sec
.Contents
), std::end(Sec
.Contents
), Buf
);
90 void Section::accept(SectionVisitor
&Visitor
) const { Visitor
.visit(*this); }
92 void SectionWriter::visit(const OwnedDataSection
&Sec
) {
93 uint8_t *Buf
= Out
.getBufferStart() + Sec
.Offset
;
94 std::copy(std::begin(Sec
.Data
), std::end(Sec
.Data
), Buf
);
97 void OwnedDataSection::accept(SectionVisitor
&Visitor
) const {
101 void StringTableSection::addString(StringRef Name
) {
102 StrTabBuilder
.add(Name
);
103 Size
= StrTabBuilder
.getSize();
106 uint32_t StringTableSection::findIndex(StringRef Name
) const {
107 return StrTabBuilder
.getOffset(Name
);
110 void StringTableSection::finalize() { StrTabBuilder
.finalize(); }
112 void SectionWriter::visit(const StringTableSection
&Sec
) {
113 Sec
.StrTabBuilder
.write(Out
.getBufferStart() + Sec
.Offset
);
116 void StringTableSection::accept(SectionVisitor
&Visitor
) const {
117 Visitor
.visit(*this);
120 static bool isValidReservedSectionIndex(uint16_t Index
, uint16_t Machine
) {
126 if (Machine
== EM_HEXAGON
) {
128 case SHN_HEXAGON_SCOMMON
:
129 case SHN_HEXAGON_SCOMMON_2
:
130 case SHN_HEXAGON_SCOMMON_4
:
131 case SHN_HEXAGON_SCOMMON_8
:
138 uint16_t Symbol::getShndx() const {
139 if (DefinedIn
!= nullptr) {
140 return DefinedIn
->Index
;
143 // This means that we don't have a defined section but we do need to
144 // output a legitimate section index.
145 case SYMBOL_SIMPLE_INDEX
:
149 case SYMBOL_HEXAGON_SCOMMON
:
150 case SYMBOL_HEXAGON_SCOMMON_2
:
151 case SYMBOL_HEXAGON_SCOMMON_4
:
152 case SYMBOL_HEXAGON_SCOMMON_8
:
153 return static_cast<uint16_t>(ShndxType
);
155 llvm_unreachable("Symbol with invalid ShndxType encountered");
158 void SymbolTableSection::addSymbol(StringRef Name
, uint8_t Bind
, uint8_t Type
,
159 SectionBase
*DefinedIn
, uint64_t Value
,
160 uint8_t Visibility
, uint16_t Shndx
,
166 Sym
.DefinedIn
= DefinedIn
;
167 if (DefinedIn
== nullptr) {
168 if (Shndx
>= SHN_LORESERVE
)
169 Sym
.ShndxType
= static_cast<SymbolShndxType
>(Shndx
);
171 Sym
.ShndxType
= SYMBOL_SIMPLE_INDEX
;
174 Sym
.Visibility
= Visibility
;
176 Sym
.Index
= Symbols
.size();
177 Symbols
.emplace_back(llvm::make_unique
<Symbol
>(Sym
));
178 Size
+= this->EntrySize
;
181 void SymbolTableSection::removeSectionReferences(const SectionBase
*Sec
) {
182 if (SymbolNames
== Sec
) {
183 error("String table " + SymbolNames
->Name
+
184 " cannot be removed because it is referenced by the symbol table " +
188 std::remove_if(std::begin(Symbols
), std::end(Symbols
),
189 [=](const SymPtr
&Sym
) { return Sym
->DefinedIn
== Sec
; });
190 Size
-= (std::end(Symbols
) - Iter
) * this->EntrySize
;
191 Symbols
.erase(Iter
, std::end(Symbols
));
194 void SymbolTableSection::localize(
195 std::function
<bool(const Symbol
&)> ToLocalize
) {
196 for (const auto &Sym
: Symbols
) {
197 if (ToLocalize(*Sym
))
198 Sym
->Binding
= STB_LOCAL
;
201 // Now that the local symbols aren't grouped at the start we have to reorder
202 // the symbols to respect this property.
203 std::stable_partition(
204 std::begin(Symbols
), std::end(Symbols
),
205 [](const SymPtr
&Sym
) { return Sym
->Binding
== STB_LOCAL
; });
207 // Lastly we fix the symbol indexes.
209 for (auto &Sym
: Symbols
)
210 Sym
->Index
= Index
++;
213 void SymbolTableSection::initialize(SectionTableRef SecTable
) {
215 setStrTab(SecTable
.getSectionOfType
<StringTableSection
>(
217 "Symbol table has link index of " + Twine(Link
) +
218 " which is not a valid index",
219 "Symbol table has link index of " + Twine(Link
) +
220 " which is not a string table"));
223 void SymbolTableSection::finalize() {
224 // Make sure SymbolNames is finalized before getting name indexes.
225 SymbolNames
->finalize();
227 uint32_t MaxLocalIndex
= 0;
228 for (auto &Sym
: Symbols
) {
229 Sym
->NameIndex
= SymbolNames
->findIndex(Sym
->Name
);
230 if (Sym
->Binding
== STB_LOCAL
)
231 MaxLocalIndex
= std::max(MaxLocalIndex
, Sym
->Index
);
233 // Now we need to set the Link and Info fields.
234 Link
= SymbolNames
->Index
;
235 Info
= MaxLocalIndex
+ 1;
238 void SymbolTableSection::addSymbolNames() {
239 // Add all of our strings to SymbolNames so that SymbolNames has the right
240 // size before layout is decided.
241 for (auto &Sym
: Symbols
)
242 SymbolNames
->addString(Sym
->Name
);
245 const Symbol
*SymbolTableSection::getSymbolByIndex(uint32_t Index
) const {
246 if (Symbols
.size() <= Index
)
247 error("Invalid symbol index: " + Twine(Index
));
248 return Symbols
[Index
].get();
251 template <class ELFT
>
252 void ELFSectionWriter
<ELFT
>::visit(const SymbolTableSection
&Sec
) {
253 uint8_t *Buf
= Out
.getBufferStart();
255 typename
ELFT::Sym
*Sym
= reinterpret_cast<typename
ELFT::Sym
*>(Buf
);
256 // Loop though symbols setting each entry of the symbol table.
257 for (auto &Symbol
: Sec
.Symbols
) {
258 Sym
->st_name
= Symbol
->NameIndex
;
259 Sym
->st_value
= Symbol
->Value
;
260 Sym
->st_size
= Symbol
->Size
;
261 Sym
->st_other
= Symbol
->Visibility
;
262 Sym
->setBinding(Symbol
->Binding
);
263 Sym
->setType(Symbol
->Type
);
264 Sym
->st_shndx
= Symbol
->getShndx();
269 void SymbolTableSection::accept(SectionVisitor
&Visitor
) const {
270 Visitor
.visit(*this);
273 template <class SymTabType
>
274 void RelocSectionWithSymtabBase
<SymTabType
>::removeSectionReferences(
275 const SectionBase
*Sec
) {
276 if (Symbols
== Sec
) {
277 error("Symbol table " + Symbols
->Name
+ " cannot be removed because it is "
278 "referenced by the relocation "
284 template <class SymTabType
>
285 void RelocSectionWithSymtabBase
<SymTabType
>::initialize(
286 SectionTableRef SecTable
) {
287 setSymTab(SecTable
.getSectionOfType
<SymTabType
>(
289 "Link field value " + Twine(Link
) + " in section " + Name
+ " is invalid",
290 "Link field value " + Twine(Link
) + " in section " + Name
+
291 " is not a symbol table"));
293 if (Info
!= SHN_UNDEF
)
294 setSection(SecTable
.getSection(Info
,
295 "Info field value " + Twine(Info
) +
296 " in section " + Name
+ " is invalid"));
301 template <class SymTabType
>
302 void RelocSectionWithSymtabBase
<SymTabType
>::finalize() {
303 this->Link
= Symbols
->Index
;
304 if (SecToApplyRel
!= nullptr)
305 this->Info
= SecToApplyRel
->Index
;
308 template <class ELFT
>
309 void setAddend(Elf_Rel_Impl
<ELFT
, false> &Rel
, uint64_t Addend
) {}
311 template <class ELFT
>
312 void setAddend(Elf_Rel_Impl
<ELFT
, true> &Rela
, uint64_t Addend
) {
313 Rela
.r_addend
= Addend
;
316 template <class RelRange
, class T
>
317 void writeRel(const RelRange
&Relocations
, T
*Buf
) {
318 for (const auto &Reloc
: Relocations
) {
319 Buf
->r_offset
= Reloc
.Offset
;
320 setAddend(*Buf
, Reloc
.Addend
);
321 Buf
->setSymbolAndType(Reloc
.RelocSymbol
->Index
, Reloc
.Type
, false);
326 template <class ELFT
>
327 void ELFSectionWriter
<ELFT
>::visit(const RelocationSection
&Sec
) {
328 uint8_t *Buf
= Out
.getBufferStart() + Sec
.Offset
;
329 if (Sec
.Type
== SHT_REL
)
330 writeRel(Sec
.Relocations
, reinterpret_cast<Elf_Rel
*>(Buf
));
332 writeRel(Sec
.Relocations
, reinterpret_cast<Elf_Rela
*>(Buf
));
335 void RelocationSection::accept(SectionVisitor
&Visitor
) const {
336 Visitor
.visit(*this);
339 void SectionWriter::visit(const DynamicRelocationSection
&Sec
) {
340 std::copy(std::begin(Sec
.Contents
), std::end(Sec
.Contents
),
341 Out
.getBufferStart() + Sec
.Offset
);
344 void DynamicRelocationSection::accept(SectionVisitor
&Visitor
) const {
345 Visitor
.visit(*this);
348 void SectionWithStrTab::removeSectionReferences(const SectionBase
*Sec
) {
350 error("String table " + StrTab
->Name
+ " cannot be removed because it is "
351 "referenced by the section " +
356 bool SectionWithStrTab::classof(const SectionBase
*S
) {
357 return isa
<DynamicSymbolTableSection
>(S
) || isa
<DynamicSection
>(S
);
360 void SectionWithStrTab::initialize(SectionTableRef SecTable
) {
361 auto StrTab
= SecTable
.getSection(Link
,
362 "Link field value " + Twine(Link
) +
363 " in section " + Name
+ " is invalid");
364 if (StrTab
->Type
!= SHT_STRTAB
) {
365 error("Link field value " + Twine(Link
) + " in section " + Name
+
366 " is not a string table");
371 void SectionWithStrTab::finalize() { this->Link
= StrTab
->Index
; }
373 void GnuDebugLinkSection::init(StringRef File
, StringRef Data
) {
374 FileName
= sys::path::filename(File
);
375 // The format for the .gnu_debuglink starts with the file name and is
376 // followed by a null terminator and then the CRC32 of the file. The CRC32
377 // should be 4 byte aligned. So we add the FileName size, a 1 for the null
378 // byte, and then finally push the size to alignment and add 4.
379 Size
= alignTo(FileName
.size() + 1, 4) + 4;
380 // The CRC32 will only be aligned if we align the whole section.
382 Type
= ELF::SHT_PROGBITS
;
383 Name
= ".gnu_debuglink";
384 // For sections not found in segments, OriginalOffset is only used to
385 // establish the order that sections should go in. By using the maximum
386 // possible offset we cause this section to wind up at the end.
387 OriginalOffset
= std::numeric_limits
<uint64_t>::max();
389 crc
.update(ArrayRef
<char>(Data
.data(), Data
.size()));
390 // The CRC32 value needs to be complemented because the JamCRC dosn't
391 // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
392 // but it starts by default at 0xFFFFFFFF which is the complement of zero.
393 CRC32
= ~crc
.getCRC();
396 GnuDebugLinkSection::GnuDebugLinkSection(StringRef File
) : FileName(File
) {
397 // Read in the file to compute the CRC of it.
398 auto DebugOrErr
= MemoryBuffer::getFile(File
);
400 error("'" + File
+ "': " + DebugOrErr
.getError().message());
401 auto Debug
= std::move(*DebugOrErr
);
402 init(File
, Debug
->getBuffer());
405 template <class ELFT
>
406 void ELFSectionWriter
<ELFT
>::visit(const GnuDebugLinkSection
&Sec
) {
407 auto Buf
= Out
.getBufferStart() + Sec
.Offset
;
408 char *File
= reinterpret_cast<char *>(Buf
);
410 reinterpret_cast<Elf_Word
*>(Buf
+ Sec
.Size
- sizeof(Elf_Word
));
412 std::copy(std::begin(Sec
.FileName
), std::end(Sec
.FileName
), File
);
415 void GnuDebugLinkSection::accept(SectionVisitor
&Visitor
) const {
416 Visitor
.visit(*this);
419 // Returns true IFF a section is wholly inside the range of a segment
420 static bool sectionWithinSegment(const SectionBase
&Section
,
421 const Segment
&Segment
) {
422 // If a section is empty it should be treated like it has a size of 1. This is
423 // to clarify the case when an empty section lies on a boundary between two
424 // segments and ensures that the section "belongs" to the second segment and
426 uint64_t SecSize
= Section
.Size
? Section
.Size
: 1;
427 return Segment
.Offset
<= Section
.OriginalOffset
&&
428 Segment
.Offset
+ Segment
.FileSize
>= Section
.OriginalOffset
+ SecSize
;
431 // Returns true IFF a segment's original offset is inside of another segment's
433 static bool segmentOverlapsSegment(const Segment
&Child
,
434 const Segment
&Parent
) {
436 return Parent
.OriginalOffset
<= Child
.OriginalOffset
&&
437 Parent
.OriginalOffset
+ Parent
.FileSize
> Child
.OriginalOffset
;
440 static bool compareSegmentsByOffset(const Segment
*A
, const Segment
*B
) {
441 // Any segment without a parent segment should come before a segment
442 // that has a parent segment.
443 if (A
->OriginalOffset
< B
->OriginalOffset
)
445 if (A
->OriginalOffset
> B
->OriginalOffset
)
447 return A
->Index
< B
->Index
;
450 static bool compareSegmentsByPAddr(const Segment
*A
, const Segment
*B
) {
451 if (A
->PAddr
< B
->PAddr
)
453 if (A
->PAddr
> B
->PAddr
)
455 return A
->Index
< B
->Index
;
458 template <class ELFT
>
459 void ELFBuilder
<ELFT
>::setParentSegment(Segment
&Child
) {
460 for (auto &Parent
: Obj
.segments()) {
461 // Every segment will overlap with itself but we don't want a segment to
462 // be it's own parent so we avoid that situation.
463 if (&Child
!= &Parent
&& segmentOverlapsSegment(Child
, Parent
)) {
464 // We want a canonical "most parental" segment but this requires
465 // inspecting the ParentSegment.
466 if (compareSegmentsByOffset(&Parent
, &Child
))
467 if (Child
.ParentSegment
== nullptr ||
468 compareSegmentsByOffset(&Parent
, Child
.ParentSegment
)) {
469 Child
.ParentSegment
= &Parent
;
475 template <class ELFT
> void ELFBuilder
<ELFT
>::readProgramHeaders() {
477 for (const auto &Phdr
: unwrapOrError(ElfFile
.program_headers())) {
478 ArrayRef
<uint8_t> Data
{ElfFile
.base() + Phdr
.p_offset
,
479 (size_t)Phdr
.p_filesz
};
480 Segment
&Seg
= Obj
.addSegment(Data
);
481 Seg
.Type
= Phdr
.p_type
;
482 Seg
.Flags
= Phdr
.p_flags
;
483 Seg
.OriginalOffset
= Phdr
.p_offset
;
484 Seg
.Offset
= Phdr
.p_offset
;
485 Seg
.VAddr
= Phdr
.p_vaddr
;
486 Seg
.PAddr
= Phdr
.p_paddr
;
487 Seg
.FileSize
= Phdr
.p_filesz
;
488 Seg
.MemSize
= Phdr
.p_memsz
;
489 Seg
.Align
= Phdr
.p_align
;
491 for (auto &Section
: Obj
.sections()) {
492 if (sectionWithinSegment(Section
, Seg
)) {
493 Seg
.addSection(&Section
);
494 if (!Section
.ParentSegment
||
495 Section
.ParentSegment
->Offset
> Seg
.Offset
) {
496 Section
.ParentSegment
= &Seg
;
502 auto &ElfHdr
= Obj
.ElfHdrSegment
;
503 // Creating multiple PT_PHDR segments technically is not valid, but PT_LOAD
504 // segments must not overlap, and other types fit even less.
505 ElfHdr
.Type
= PT_PHDR
;
507 ElfHdr
.OriginalOffset
= ElfHdr
.Offset
= 0;
510 ElfHdr
.FileSize
= ElfHdr
.MemSize
= sizeof(Elf_Ehdr
);
512 ElfHdr
.Index
= Index
++;
514 const auto &Ehdr
= *ElfFile
.getHeader();
515 auto &PrHdr
= Obj
.ProgramHdrSegment
;
516 PrHdr
.Type
= PT_PHDR
;
518 // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
519 // Whereas this works automatically for ElfHdr, here OriginalOffset is
520 // always non-zero and to ensure the equation we assign the same value to
522 PrHdr
.OriginalOffset
= PrHdr
.Offset
= PrHdr
.VAddr
= Ehdr
.e_phoff
;
524 PrHdr
.FileSize
= PrHdr
.MemSize
= Ehdr
.e_phentsize
* Ehdr
.e_phnum
;
525 // The spec requires us to naturally align all the fields.
526 PrHdr
.Align
= sizeof(Elf_Addr
);
527 PrHdr
.Index
= Index
++;
529 // Now we do an O(n^2) loop through the segments in order to match up
531 for (auto &Child
: Obj
.segments())
532 setParentSegment(Child
);
533 setParentSegment(ElfHdr
);
534 setParentSegment(PrHdr
);
537 template <class ELFT
>
538 void ELFBuilder
<ELFT
>::initSymbolTable(SymbolTableSection
*SymTab
) {
539 const Elf_Shdr
&Shdr
= *unwrapOrError(ElfFile
.getSection(SymTab
->Index
));
540 StringRef StrTabData
= unwrapOrError(ElfFile
.getStringTableForSymtab(Shdr
));
542 for (const auto &Sym
: unwrapOrError(ElfFile
.symbols(&Shdr
))) {
543 SectionBase
*DefSection
= nullptr;
544 StringRef Name
= unwrapOrError(Sym
.getName(StrTabData
));
546 if (Sym
.st_shndx
>= SHN_LORESERVE
) {
547 if (!isValidReservedSectionIndex(Sym
.st_shndx
, Obj
.Machine
)) {
550 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
551 Twine(Sym
.st_shndx
));
553 } else if (Sym
.st_shndx
!= SHN_UNDEF
) {
554 DefSection
= Obj
.sections().getSection(
556 "Symbol '" + Name
+ "' is defined in invalid section with index " +
557 Twine(Sym
.st_shndx
));
560 SymTab
->addSymbol(Name
, Sym
.getBinding(), Sym
.getType(), DefSection
,
561 Sym
.getValue(), Sym
.st_other
, Sym
.st_shndx
, Sym
.st_size
);
565 template <class ELFT
>
566 static void getAddend(uint64_t &ToSet
, const Elf_Rel_Impl
<ELFT
, false> &Rel
) {}
568 template <class ELFT
>
569 static void getAddend(uint64_t &ToSet
, const Elf_Rel_Impl
<ELFT
, true> &Rela
) {
570 ToSet
= Rela
.r_addend
;
574 void initRelocations(RelocationSection
*Relocs
, SymbolTableSection
*SymbolTable
,
576 for (const auto &Rel
: RelRange
) {
578 ToAdd
.Offset
= Rel
.r_offset
;
579 getAddend(ToAdd
.Addend
, Rel
);
580 ToAdd
.Type
= Rel
.getType(false);
581 ToAdd
.RelocSymbol
= SymbolTable
->getSymbolByIndex(Rel
.getSymbol(false));
582 Relocs
->addRelocation(ToAdd
);
586 SectionBase
*SectionTableRef::getSection(uint16_t Index
, Twine ErrMsg
) {
587 if (Index
== SHN_UNDEF
|| Index
> Sections
.size())
589 return Sections
[Index
- 1].get();
593 T
*SectionTableRef::getSectionOfType(uint16_t Index
, Twine IndexErrMsg
,
595 if (T
*Sec
= dyn_cast
<T
>(getSection(Index
, IndexErrMsg
)))
600 template <class ELFT
>
601 SectionBase
&ELFBuilder
<ELFT
>::makeSection(const Elf_Shdr
&Shdr
) {
602 ArrayRef
<uint8_t> Data
;
603 switch (Shdr
.sh_type
) {
606 if (Shdr
.sh_flags
& SHF_ALLOC
) {
607 Data
= unwrapOrError(ElfFile
.getSectionContents(&Shdr
));
608 return Obj
.addSection
<DynamicRelocationSection
>(Data
);
610 return Obj
.addSection
<RelocationSection
>();
612 // If a string table is allocated we don't want to mess with it. That would
613 // mean altering the memory image. There are no special link types or
614 // anything so we can just use a Section.
615 if (Shdr
.sh_flags
& SHF_ALLOC
) {
616 Data
= unwrapOrError(ElfFile
.getSectionContents(&Shdr
));
617 return Obj
.addSection
<Section
>(Data
);
619 return Obj
.addSection
<StringTableSection
>();
622 // Hash tables should refer to SHT_DYNSYM which we're not going to change.
623 // Because of this we don't need to mess with the hash tables either.
624 Data
= unwrapOrError(ElfFile
.getSectionContents(&Shdr
));
625 return Obj
.addSection
<Section
>(Data
);
627 Data
= unwrapOrError(ElfFile
.getSectionContents(&Shdr
));
628 return Obj
.addSection
<DynamicSymbolTableSection
>(Data
);
630 Data
= unwrapOrError(ElfFile
.getSectionContents(&Shdr
));
631 return Obj
.addSection
<DynamicSection
>(Data
);
633 auto &SymTab
= Obj
.addSection
<SymbolTableSection
>();
634 Obj
.SymbolTable
= &SymTab
;
638 return Obj
.addSection
<Section
>(Data
);
640 Data
= unwrapOrError(ElfFile
.getSectionContents(&Shdr
));
641 return Obj
.addSection
<Section
>(Data
);
645 template <class ELFT
> void ELFBuilder
<ELFT
>::readSectionHeaders() {
647 for (const auto &Shdr
: unwrapOrError(ElfFile
.sections())) {
652 auto &Sec
= makeSection(Shdr
);
653 Sec
.Name
= unwrapOrError(ElfFile
.getSectionName(&Shdr
));
654 Sec
.Type
= Shdr
.sh_type
;
655 Sec
.Flags
= Shdr
.sh_flags
;
656 Sec
.Addr
= Shdr
.sh_addr
;
657 Sec
.Offset
= Shdr
.sh_offset
;
658 Sec
.OriginalOffset
= Shdr
.sh_offset
;
659 Sec
.Size
= Shdr
.sh_size
;
660 Sec
.Link
= Shdr
.sh_link
;
661 Sec
.Info
= Shdr
.sh_info
;
662 Sec
.Align
= Shdr
.sh_addralign
;
663 Sec
.EntrySize
= Shdr
.sh_entsize
;
667 // Now that all of the sections have been added we can fill out some extra
668 // details about symbol tables. We need the symbol table filled out before
670 if (Obj
.SymbolTable
) {
671 Obj
.SymbolTable
->initialize(Obj
.sections());
672 initSymbolTable(Obj
.SymbolTable
);
675 // Now that all sections and symbols have been added we can add
676 // relocations that reference symbols and set the link and info fields for
677 // relocation sections.
678 for (auto &Section
: Obj
.sections()) {
679 if (&Section
== Obj
.SymbolTable
)
681 Section
.initialize(Obj
.sections());
682 if (auto RelSec
= dyn_cast
<RelocationSection
>(&Section
)) {
683 auto Shdr
= unwrapOrError(ElfFile
.sections()).begin() + RelSec
->Index
;
684 if (RelSec
->Type
== SHT_REL
)
685 initRelocations(RelSec
, Obj
.SymbolTable
,
686 unwrapOrError(ElfFile
.rels(Shdr
)));
688 initRelocations(RelSec
, Obj
.SymbolTable
,
689 unwrapOrError(ElfFile
.relas(Shdr
)));
694 template <class ELFT
> void ELFBuilder
<ELFT
>::build() {
695 const auto &Ehdr
= *ElfFile
.getHeader();
697 std::copy(Ehdr
.e_ident
, Ehdr
.e_ident
+ 16, Obj
.Ident
);
698 Obj
.Type
= Ehdr
.e_type
;
699 Obj
.Machine
= Ehdr
.e_machine
;
700 Obj
.Version
= Ehdr
.e_version
;
701 Obj
.Entry
= Ehdr
.e_entry
;
702 Obj
.Flags
= Ehdr
.e_flags
;
704 readSectionHeaders();
705 readProgramHeaders();
708 Obj
.sections().template getSectionOfType
<StringTableSection
>(
710 "e_shstrndx field value " + Twine(Ehdr
.e_shstrndx
) +
711 " in elf header " + " is invalid",
712 "e_shstrndx field value " + Twine(Ehdr
.e_shstrndx
) +
713 " in elf header " + " is not a string table");
716 // A generic size function which computes sizes of any random access range.
717 template <class R
> size_t size(R
&&Range
) {
718 return static_cast<size_t>(std::end(Range
) - std::begin(Range
));
725 ELFReader::ELFReader(StringRef File
) {
726 auto BinaryOrErr
= createBinary(File
);
728 reportError(File
, BinaryOrErr
.takeError());
729 auto OwnedBin
= std::move(BinaryOrErr
.get());
730 std::tie(Bin
, Data
) = OwnedBin
.takeBinary();
733 ElfType
ELFReader::getElfType() const {
734 if (isa
<ELFObjectFile
<ELF32LE
>>(Bin
.get()))
736 if (isa
<ELFObjectFile
<ELF64LE
>>(Bin
.get()))
738 if (isa
<ELFObjectFile
<ELF32BE
>>(Bin
.get()))
740 if (isa
<ELFObjectFile
<ELF64BE
>>(Bin
.get()))
742 llvm_unreachable("Invalid ELFType");
745 std::unique_ptr
<Object
> ELFReader::create() const {
746 auto Obj
= llvm::make_unique
<Object
>(Data
);
747 if (auto *o
= dyn_cast
<ELFObjectFile
<ELF32LE
>>(Bin
.get())) {
748 ELFBuilder
<ELF32LE
> Builder(*o
, *Obj
);
751 } else if (auto *o
= dyn_cast
<ELFObjectFile
<ELF64LE
>>(Bin
.get())) {
752 ELFBuilder
<ELF64LE
> Builder(*o
, *Obj
);
755 } else if (auto *o
= dyn_cast
<ELFObjectFile
<ELF32BE
>>(Bin
.get())) {
756 ELFBuilder
<ELF32BE
> Builder(*o
, *Obj
);
759 } else if (auto *o
= dyn_cast
<ELFObjectFile
<ELF64BE
>>(Bin
.get())) {
760 ELFBuilder
<ELF64BE
> Builder(*o
, *Obj
);
764 error("Invalid file type");
767 template <class ELFT
> void ELFWriter
<ELFT
>::writeEhdr() {
768 uint8_t *Buf
= BufPtr
->getBufferStart();
769 Elf_Ehdr
&Ehdr
= *reinterpret_cast<Elf_Ehdr
*>(Buf
);
770 std::copy(Obj
.Ident
, Obj
.Ident
+ 16, Ehdr
.e_ident
);
771 Ehdr
.e_type
= Obj
.Type
;
772 Ehdr
.e_machine
= Obj
.Machine
;
773 Ehdr
.e_version
= Obj
.Version
;
774 Ehdr
.e_entry
= Obj
.Entry
;
775 Ehdr
.e_phoff
= Obj
.ProgramHdrSegment
.Offset
;
776 Ehdr
.e_flags
= Obj
.Flags
;
777 Ehdr
.e_ehsize
= sizeof(Elf_Ehdr
);
778 Ehdr
.e_phentsize
= sizeof(Elf_Phdr
);
779 Ehdr
.e_phnum
= size(Obj
.segments());
780 Ehdr
.e_shentsize
= sizeof(Elf_Shdr
);
781 if (WriteSectionHeaders
) {
782 Ehdr
.e_shoff
= Obj
.SHOffset
;
783 Ehdr
.e_shnum
= size(Obj
.sections()) + 1;
784 Ehdr
.e_shstrndx
= Obj
.SectionNames
->Index
;
792 template <class ELFT
> void ELFWriter
<ELFT
>::writePhdrs() {
793 for (auto &Seg
: Obj
.segments())
797 template <class ELFT
> void ELFWriter
<ELFT
>::writeShdrs() {
798 uint8_t *Buf
= BufPtr
->getBufferStart() + Obj
.SHOffset
;
799 // This reference serves to write the dummy section header at the begining
800 // of the file. It is not used for anything else
801 Elf_Shdr
&Shdr
= *reinterpret_cast<Elf_Shdr
*>(Buf
);
803 Shdr
.sh_type
= SHT_NULL
;
810 Shdr
.sh_addralign
= 0;
813 for (auto &Sec
: Obj
.sections())
817 template <class ELFT
> void ELFWriter
<ELFT
>::writeSectionData() {
818 for (auto &Sec
: Obj
.sections())
819 Sec
.accept(*SecWriter
);
822 void Object::removeSections(std::function
<bool(const SectionBase
&)> ToRemove
) {
824 auto Iter
= std::stable_partition(
825 std::begin(Sections
), std::end(Sections
), [=](const SecPtr
&Sec
) {
828 if (auto RelSec
= dyn_cast
<RelocationSectionBase
>(Sec
.get())) {
829 if (auto ToRelSec
= RelSec
->getSection())
830 return !ToRemove(*ToRelSec
);
834 if (SymbolTable
!= nullptr && ToRemove(*SymbolTable
))
835 SymbolTable
= nullptr;
836 if (SectionNames
!= nullptr && ToRemove(*SectionNames
)) {
837 SectionNames
= nullptr;
839 // Now make sure there are no remaining references to the sections that will
840 // be removed. Sometimes it is impossible to remove a reference so we emit
841 // an error here instead.
842 for (auto &RemoveSec
: make_range(Iter
, std::end(Sections
))) {
843 for (auto &Segment
: Segments
)
844 Segment
->removeSection(RemoveSec
.get());
845 for (auto &KeepSec
: make_range(std::begin(Sections
), Iter
))
846 KeepSec
->removeSectionReferences(RemoveSec
.get());
848 // Now finally get rid of them all togethor.
849 Sections
.erase(Iter
, std::end(Sections
));
852 void Object::sortSections() {
853 // Put all sections in offset order. Maintain the ordering as closely as
854 // possible while meeting that demand however.
855 auto CompareSections
= [](const SecPtr
&A
, const SecPtr
&B
) {
856 return A
->OriginalOffset
< B
->OriginalOffset
;
858 std::stable_sort(std::begin(this->Sections
), std::end(this->Sections
),
862 static uint64_t alignToAddr(uint64_t Offset
, uint64_t Addr
, uint64_t Align
) {
863 // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
867 static_cast<int64_t>(Addr
% Align
) - static_cast<int64_t>(Offset
% Align
);
868 // We only want to add to Offset, however, so if Diff < 0 we can add Align and
869 // (Offset + Diff) & -Align == Addr & -Align will still hold.
872 return Offset
+ Diff
;
875 // Orders segments such that if x = y->ParentSegment then y comes before x.
876 static void OrderSegments(std::vector
<Segment
*> &Segments
) {
877 std::stable_sort(std::begin(Segments
), std::end(Segments
),
878 compareSegmentsByOffset
);
881 // This function finds a consistent layout for a list of segments starting from
882 // an Offset. It assumes that Segments have been sorted by OrderSegments and
883 // returns an Offset one past the end of the last segment.
884 static uint64_t LayoutSegments(std::vector
<Segment
*> &Segments
,
886 assert(std::is_sorted(std::begin(Segments
), std::end(Segments
),
887 compareSegmentsByOffset
));
888 // The only way a segment should move is if a section was between two
889 // segments and that section was removed. If that section isn't in a segment
890 // then it's acceptable, but not ideal, to simply move it to after the
891 // segments. So we can simply layout segments one after the other accounting
893 for (auto &Segment
: Segments
) {
894 // We assume that segments have been ordered by OriginalOffset and Index
895 // such that a parent segment will always come before a child segment in
896 // OrderedSegments. This means that the Offset of the ParentSegment should
897 // already be set and we can set our offset relative to it.
898 if (Segment
->ParentSegment
!= nullptr) {
899 auto Parent
= Segment
->ParentSegment
;
901 Parent
->Offset
+ Segment
->OriginalOffset
- Parent
->OriginalOffset
;
903 Offset
= alignToAddr(Offset
, Segment
->VAddr
, Segment
->Align
);
904 Segment
->Offset
= Offset
;
906 Offset
= std::max(Offset
, Segment
->Offset
+ Segment
->FileSize
);
911 // This function finds a consistent layout for a list of sections. It assumes
912 // that the ->ParentSegment of each section has already been laid out. The
913 // supplied starting Offset is used for the starting offset of any section that
914 // does not have a ParentSegment. It returns either the offset given if all
915 // sections had a ParentSegment or an offset one past the last section if there
916 // was a section that didn't have a ParentSegment.
917 template <class Range
>
918 static uint64_t LayoutSections(Range Sections
, uint64_t Offset
) {
919 // Now the offset of every segment has been set we can assign the offsets
920 // of each section. For sections that are covered by a segment we should use
921 // the segment's original offset and the section's original offset to compute
922 // the offset from the start of the segment. Using the offset from the start
923 // of the segment we can assign a new offset to the section. For sections not
924 // covered by segments we can just bump Offset to the next valid location.
926 for (auto &Section
: Sections
) {
927 Section
.Index
= Index
++;
928 if (Section
.ParentSegment
!= nullptr) {
929 auto Segment
= *Section
.ParentSegment
;
931 Segment
.Offset
+ (Section
.OriginalOffset
- Segment
.OriginalOffset
);
933 Offset
= alignTo(Offset
, Section
.Align
== 0 ? 1 : Section
.Align
);
934 Section
.Offset
= Offset
;
935 if (Section
.Type
!= SHT_NOBITS
)
936 Offset
+= Section
.Size
;
942 template <class ELFT
> void ELFWriter
<ELFT
>::assignOffsets() {
943 // We need a temporary list of segments that has a special order to it
944 // so that we know that anytime ->ParentSegment is set that segment has
945 // already had its offset properly set.
946 std::vector
<Segment
*> OrderedSegments
;
947 for (auto &Segment
: Obj
.segments())
948 OrderedSegments
.push_back(&Segment
);
949 OrderedSegments
.push_back(&Obj
.ElfHdrSegment
);
950 OrderedSegments
.push_back(&Obj
.ProgramHdrSegment
);
951 OrderSegments(OrderedSegments
);
952 // Offset is used as the start offset of the first segment to be laid out.
953 // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
954 // we start at offset 0.
956 Offset
= LayoutSegments(OrderedSegments
, Offset
);
957 Offset
= LayoutSections(Obj
.sections(), Offset
);
958 // If we need to write the section header table out then we need to align the
959 // Offset so that SHOffset is valid.
960 if (WriteSectionHeaders
)
961 Offset
= alignTo(Offset
, sizeof(typename
ELFT::Addr
));
962 Obj
.SHOffset
= Offset
;
965 template <class ELFT
> size_t ELFWriter
<ELFT
>::totalSize() const {
966 // We already have the section header offset so we can calculate the total
967 // size by just adding up the size of each section header.
968 auto NullSectionSize
= WriteSectionHeaders
? sizeof(Elf_Shdr
) : 0;
969 return Obj
.SHOffset
+ size(Obj
.sections()) * sizeof(Elf_Shdr
) +
973 template <class ELFT
> void ELFWriter
<ELFT
>::write() {
977 if (WriteSectionHeaders
)
979 if (auto E
= BufPtr
->commit())
980 reportError(File
, errorToErrorCode(std::move(E
)));
983 void Writer::createBuffer(uint64_t Size
) {
985 FileOutputBuffer::create(File
, Size
, FileOutputBuffer::F_executable
);
986 handleAllErrors(BufferOrErr
.takeError(), [this](const ErrorInfoBase
&) {
987 error("failed to open " + File
);
989 BufPtr
= std::move(*BufferOrErr
);
992 template <class ELFT
> void ELFWriter
<ELFT
>::finalize() {
993 // It could happen that SectionNames has been removed and yet the user wants
994 // a section header table output. We need to throw an error if a user tries
996 if (Obj
.SectionNames
== nullptr && WriteSectionHeaders
)
997 error("Cannot write section header table because section header string "
998 "table was removed.");
1000 // Make sure we add the names of all the sections.
1001 if (Obj
.SectionNames
!= nullptr)
1002 for (const auto &Section
: Obj
.sections()) {
1003 Obj
.SectionNames
->addString(Section
.Name
);
1005 // Make sure we add the names of all the symbols.
1006 if (Obj
.SymbolTable
!= nullptr)
1007 Obj
.SymbolTable
->addSymbolNames();
1012 // Finalize SectionNames first so that we can assign name indexes.
1013 if (Obj
.SectionNames
!= nullptr)
1014 Obj
.SectionNames
->finalize();
1015 // Finally now that all offsets and indexes have been set we can finalize any
1016 // remaining issues.
1017 uint64_t Offset
= Obj
.SHOffset
+ sizeof(Elf_Shdr
);
1018 for (auto &Section
: Obj
.sections()) {
1019 Section
.HeaderOffset
= Offset
;
1020 Offset
+= sizeof(Elf_Shdr
);
1021 if (WriteSectionHeaders
)
1022 Section
.NameIndex
= Obj
.SectionNames
->findIndex(Section
.Name
);
1026 createBuffer(totalSize());
1027 SecWriter
= llvm::make_unique
<ELFSectionWriter
<ELFT
>>(*BufPtr
);
1030 void BinaryWriter::write() {
1031 for (auto &Section
: Obj
.sections()) {
1032 if ((Section
.Flags
& SHF_ALLOC
) == 0)
1034 Section
.accept(*SecWriter
);
1036 if (auto E
= BufPtr
->commit())
1037 reportError(File
, errorToErrorCode(std::move(E
)));
1040 void BinaryWriter::finalize() {
1041 // TODO: Create a filter range to construct OrderedSegments from so that this
1042 // code can be deduped with assignOffsets above. This should also solve the
1043 // todo below for LayoutSections.
1044 // We need a temporary list of segments that has a special order to it
1045 // so that we know that anytime ->ParentSegment is set that segment has
1046 // already had it's offset properly set. We only want to consider the segments
1047 // that will affect layout of allocated sections so we only add those.
1048 std::vector
<Segment
*> OrderedSegments
;
1049 for (auto &Section
: Obj
.sections()) {
1050 if ((Section
.Flags
& SHF_ALLOC
) != 0 && Section
.ParentSegment
!= nullptr) {
1051 OrderedSegments
.push_back(Section
.ParentSegment
);
1055 // For binary output, we're going to use physical addresses instead of
1056 // virtual addresses, since a binary output is used for cases like ROM
1057 // loading and physical addresses are intended for ROM loading.
1058 // However, if no segment has a physical address, we'll fallback to using
1059 // virtual addresses for all.
1060 if (std::all_of(std::begin(OrderedSegments
), std::end(OrderedSegments
),
1061 [](const Segment
*Segment
) { return Segment
->PAddr
== 0; }))
1062 for (const auto &Segment
: OrderedSegments
)
1063 Segment
->PAddr
= Segment
->VAddr
;
1065 std::stable_sort(std::begin(OrderedSegments
), std::end(OrderedSegments
),
1066 compareSegmentsByPAddr
);
1068 // Because we add a ParentSegment for each section we might have duplicate
1069 // segments in OrderedSegments. If there were duplicates then LayoutSegments
1070 // would do very strange things.
1072 std::unique(std::begin(OrderedSegments
), std::end(OrderedSegments
));
1073 OrderedSegments
.erase(End
, std::end(OrderedSegments
));
1075 uint64_t Offset
= 0;
1077 // Modify the first segment so that there is no gap at the start. This allows
1078 // our layout algorithm to proceed as expected while not out writing out the
1079 // gap at the start.
1080 if (!OrderedSegments
.empty()) {
1081 auto Seg
= OrderedSegments
[0];
1082 auto Sec
= Seg
->firstSection();
1083 auto Diff
= Sec
->OriginalOffset
- Seg
->OriginalOffset
;
1084 Seg
->OriginalOffset
+= Diff
;
1085 // The size needs to be shrunk as well.
1086 Seg
->FileSize
-= Diff
;
1087 // The PAddr needs to be increased to remove the gap before the first
1090 uint64_t LowestPAddr
= Seg
->PAddr
;
1091 for (auto &Segment
: OrderedSegments
) {
1092 Segment
->Offset
= Segment
->PAddr
- LowestPAddr
;
1093 Offset
= std::max(Offset
, Segment
->Offset
+ Segment
->FileSize
);
1097 // TODO: generalize LayoutSections to take a range. Pass a special range
1098 // constructed from an iterator that skips values for which a predicate does
1099 // not hold. Then pass such a range to LayoutSections instead of constructing
1100 // AllocatedSections here.
1101 std::vector
<SectionBase
*> AllocatedSections
;
1102 for (auto &Section
: Obj
.sections()) {
1103 if ((Section
.Flags
& SHF_ALLOC
) == 0)
1105 AllocatedSections
.push_back(&Section
);
1107 LayoutSections(make_pointee_range(AllocatedSections
), Offset
);
1109 // Now that every section has been laid out we just need to compute the total
1110 // file size. This might not be the same as the offset returned by
1111 // LayoutSections, because we want to truncate the last segment to the end of
1112 // its last section, to match GNU objcopy's behaviour.
1114 for (const auto &Section
: AllocatedSections
) {
1115 if (Section
->Type
!= SHT_NOBITS
)
1116 TotalSize
= std::max(TotalSize
, Section
->Offset
+ Section
->Size
);
1119 createBuffer(TotalSize
);
1120 SecWriter
= llvm::make_unique
<BinarySectionWriter
>(*BufPtr
);
1125 template class ELFBuilder
<ELF64LE
>;
1126 template class ELFBuilder
<ELF64BE
>;
1127 template class ELFBuilder
<ELF32LE
>;
1128 template class ELFBuilder
<ELF32BE
>;
1130 template class ELFWriter
<ELF64LE
>;
1131 template class ELFWriter
<ELF64BE
>;
1132 template class ELFWriter
<ELF32LE
>;
1133 template class ELFWriter
<ELF32BE
>;
1135 } // end namespace llvm