Index: llvm/include/llvm/BinaryFormat/ELF.h =================================================================== --- llvm/include/llvm/BinaryFormat/ELF.h +++ llvm/include/llvm/BinaryFormat/ELF.h @@ -57,6 +57,11 @@ EI_NIDENT = 16 // Number of bytes in e_ident. }; +constexpr uint8_t ELFMAG0 = 0x7f; +constexpr uint8_t ELFMAG1 = 'E'; +constexpr uint8_t ELFMAG2 = 'L'; +constexpr uint8_t ELFMAG3 = 'F'; + struct Elf32_Ehdr { unsigned char e_ident[EI_NIDENT]; // ELF Identification bytes Elf32_Half e_type; // Type of file (see ET_* below) Index: llvm/test/tools/llvm-elfabi/binary-write-sheaders.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-write-sheaders.test @@ -0,0 +1,42 @@ +# RUN: llvm-elfabi %s --output-target=elf64-little %t +# RUN: llvm-readobj -h %t | FileCheck %s --check-prefix=ELFHEADER +# RUN: llvm-readobj -S %t | FileCheck %s --check-prefix=SECTIONS + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: x86_64 +Symbols: {} +... + +# ELFHEADER: SectionHeaderCount: 2 +# ELFHEADER: StringTableSectionIndex: 1 + +# SECTIONS: Section { +# SECTIONS-NEXT: Index: 0 +# SECTIONS-NEXT: Name: (0) +# SECTIONS-NEXT: Type: SHT_NULL +# SECTIONS-NEXT: Flags [ +# SECTIONS-NEXT: ] +# SECTIONS-NEXT: Address: 0x0 +# SECTIONS-NEXT: Offset: 0x0 +# SECTIONS-NEXT: Size: 0 +# SECTIONS-NEXT: Link: 0 +# SECTIONS-NEXT: Info: 0 +# SECTIONS-NEXT: AddressAlignment: 0 +# SECTIONS-NEXT: EntrySize: 0 +# SECTIONS-NEXT: } +# SECTIONS-NEXT: Section { +# SECTIONS-NEXT: Index: 1 +# SECTIONS-NEXT: Name: .dynstr +# SECTIONS-NEXT: Type: SHT_STRTAB +# SECTIONS-NEXT: Flags [ +# SECTIONS-NEXT: SHF_ALLOC +# SECTIONS-NEXT: ] +# SECTIONS-NEXT: Address: +# SECTIONS-NEXT: Offset: +# SECTIONS-NEXT: Size: +# SECTIONS-NEXT: Link: 0 +# SECTIONS-NEXT: Info: 0 +# SECTIONS-NEXT: AddressAlignment: 0 +# SECTIONS-NEXT: EntrySize: 0 +# SECTIONS-NEXT: } Index: llvm/test/tools/llvm-elfabi/invalid-bin-target.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/invalid-bin-target.test @@ -0,0 +1,10 @@ +# RUN: not llvm-elfabi %s --output-target=nope %t 2>&1 | FileCheck %s + +--- !tapi-tbe +SoName: somelib.so +TbeVersion: 1.0 +Arch: x86_64 +Symbols: {} +... + +# CHECK: llvm-elfabi: for the --output-target option: Cannot find option named 'nope'! Index: llvm/test/tools/llvm-elfabi/missing-bin-target.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/missing-bin-target.test @@ -0,0 +1,10 @@ +# RUN: not llvm-elfabi %s %t 2>&1 | FileCheck %s + +--- !tapi-tbe +SoName: somelib.so +TbeVersion: 1.0 +Arch: x86_64 +Symbols: {} +... + +# CHECK: No binary output target specified. Index: llvm/test/tools/llvm-elfabi/write-elf32be-ehdr.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/write-elf32be-ehdr.test @@ -0,0 +1,28 @@ +# RUN: llvm-elfabi %s --output-target=elf32-big %t +# RUN: llvm-readobj --file-headers %t | FileCheck %s + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: x86_64 +Symbols: {} +... + +# CHECK: ElfHeader { +# CHECK-NEXT: Ident { +# CHECK-NEXT: Magic: (7F 45 4C 46) +# CHECK-NEXT: Class: 32-bit (0x1) +# CHECK-NEXT: DataEncoding: BigEndian (0x2) +# CHECK-NEXT: FileVersion: 1{{$}} +# CHECK-NEXT: OS/ABI: SystemV (0x0) +# CHECK-NEXT: ABIVersion: 0{{$}} +# CHECK-NEXT: Unused: (00 00 00 00 00 00 00) +# CHECK-NEXT: } +# CHECK-NEXT: Type: SharedObject (0x3) +# CHECK-NEXT: Machine: EM_X86_64 (0x3E) +# CHECK-NEXT: Version: 1{{$}} +# CHECK-NEXT: Entry: 0x0{{$}} +# CHECK: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: HeaderSize: 52{{$}} +# CHECK-NEXT: ProgramHeaderEntrySize: 32{{$}} +# CHECK: SectionHeaderEntrySize: 40{{$}} Index: llvm/test/tools/llvm-elfabi/write-elf32le-ehdr.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/write-elf32le-ehdr.test @@ -0,0 +1,28 @@ +# RUN: llvm-elfabi %s --output-target=elf32-little %t +# RUN: llvm-readobj --file-headers %t | FileCheck %s + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: x86_64 +Symbols: {} +... + +# CHECK: ElfHeader { +# CHECK-NEXT: Ident { +# CHECK-NEXT: Magic: (7F 45 4C 46) +# CHECK-NEXT: Class: 32-bit (0x1) +# CHECK-NEXT: DataEncoding: LittleEndian (0x1) +# CHECK-NEXT: FileVersion: 1{{$}} +# CHECK-NEXT: OS/ABI: SystemV (0x0) +# CHECK-NEXT: ABIVersion: 0{{$}} +# CHECK-NEXT: Unused: (00 00 00 00 00 00 00) +# CHECK-NEXT: } +# CHECK-NEXT: Type: SharedObject (0x3) +# CHECK-NEXT: Machine: EM_X86_64 (0x3E) +# CHECK-NEXT: Version: 1{{$}} +# CHECK-NEXT: Entry: 0x0{{$}} +# CHECK: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: HeaderSize: 52{{$}} +# CHECK-NEXT: ProgramHeaderEntrySize: 32{{$}} +# CHECK: SectionHeaderEntrySize: 40{{$}} Index: llvm/test/tools/llvm-elfabi/write-elf64be-ehdr.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/write-elf64be-ehdr.test @@ -0,0 +1,28 @@ +# RUN: llvm-elfabi %s --output-target=elf64-big %t +# RUN: llvm-readobj --file-headers %t | FileCheck %s + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: x86_64 +Symbols: {} +... + +# CHECK: ElfHeader { +# CHECK-NEXT: Ident { +# CHECK-NEXT: Magic: (7F 45 4C 46) +# CHECK-NEXT: Class: 64-bit (0x2) +# CHECK-NEXT: DataEncoding: BigEndian (0x2) +# CHECK-NEXT: FileVersion: 1{{$}} +# CHECK-NEXT: OS/ABI: SystemV (0x0) +# CHECK-NEXT: ABIVersion: 0{{$}} +# CHECK-NEXT: Unused: (00 00 00 00 00 00 00) +# CHECK-NEXT: } +# CHECK-NEXT: Type: SharedObject (0x3) +# CHECK-NEXT: Machine: EM_X86_64 (0x3E) +# CHECK-NEXT: Version: 1{{$}} +# CHECK-NEXT: Entry: 0x0{{$}} +# CHECK: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: HeaderSize: 64{{$}} +# CHECK-NEXT: ProgramHeaderEntrySize: 56{{$}} +# CHECK: SectionHeaderEntrySize: 64{{$}} Index: llvm/test/tools/llvm-elfabi/write-elf64le-ehdr.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/write-elf64le-ehdr.test @@ -0,0 +1,28 @@ +# RUN: llvm-elfabi %s --output-target=elf64-little %t +# RUN: llvm-readobj --file-headers %t | FileCheck %s + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: AArch64 +Symbols: {} +... + +# CHECK: ElfHeader { +# CHECK-NEXT: Ident { +# CHECK-NEXT: Magic: (7F 45 4C 46) +# CHECK-NEXT: Class: 64-bit (0x2) +# CHECK-NEXT: DataEncoding: LittleEndian (0x1) +# CHECK-NEXT: FileVersion: 1{{$}} +# CHECK-NEXT: OS/ABI: SystemV (0x0) +# CHECK-NEXT: ABIVersion: 0{{$}} +# CHECK-NEXT: Unused: (00 00 00 00 00 00 00) +# CHECK-NEXT: } +# CHECK-NEXT: Type: SharedObject (0x3) +# CHECK-NEXT: Machine: EM_AARCH64 (0xB7) +# CHECK-NEXT: Version: 1{{$}} +# CHECK-NEXT: Entry: 0x0{{$}} +# CHECK: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: HeaderSize: 64{{$}} +# CHECK-NEXT: ProgramHeaderEntrySize: 56{{$}} +# CHECK: SectionHeaderEntrySize: 64{{$}} Index: llvm/tools/llvm-elfabi/ELFObjHandler.h =================================================================== --- llvm/tools/llvm-elfabi/ELFObjHandler.h +++ llvm/tools/llvm-elfabi/ELFObjHandler.h @@ -23,9 +23,26 @@ namespace elfabi { +enum class ELFTarget { + ELF32LE, + ELF32BE, + ELF64LE, + ELF64BE +}; + /// Attempt to read a binary ELF file from a MemoryBuffer. Expected> readELFFile(MemoryBufferRef Buf); +/// Attempt to write a binary ELF stub. +/// This function determines appropriate ELFType using the passed ELFTarget and +/// then writes a binary ELF stub to a specified file path. +/// +/// @param FilePath File path for writing the ELF binary. +/// @param Stub Source ELFStub to generate a binary ELF stub from. +/// @param OutputFormat Target ELFType to write binary as. +Error writeBinaryStub(StringRef FilePath, const ELFStub &Stub, + ELFTarget OutputFormat); + } // end namespace elfabi } // end namespace llvm Index: llvm/tools/llvm-elfabi/ELFObjHandler.cpp =================================================================== --- llvm/tools/llvm-elfabi/ELFObjHandler.cpp +++ llvm/tools/llvm-elfabi/ELFObjHandler.cpp @@ -7,14 +7,19 @@ //===-----------------------------------------------------------------------===/ #include "ELFObjHandler.h" +#include "llvm/MC/StringTableBuilder.h" #include "llvm/Object/Binary.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/ELFTypes.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" +#include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/MathExtras.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/TextAPI/ELF/ELFStub.h" +#include + using llvm::MemoryBufferRef; using llvm::object::ELFObjectFile; @@ -22,8 +27,9 @@ using namespace llvm::object; using namespace llvm::ELF; -namespace llvm { -namespace elfabi { +namespace { + +using namespace llvm::elfabi; // Simple struct to hold relevant .dynamic entries. struct DynamicEntries { @@ -38,13 +44,71 @@ Optional GnuHash; }; +// Lazy assumes that T is default constructable. +// Lazy also acts like a read-only, move-only type. +// Lazy is inteneded to be used to form build graphs for ELF stubs. ELF files +// need to be generated in stages because different decisions depend on each +// other. By constructing a build graph of lazy values for these decisions we +// let them auto-resolve the order for them selves. We run the risk of +// introducing a cyclic dependency when constructing the graphs however. To +// check for this we overwrite the evaluation function with a so called +// "blackhole" that causes a failure if evaluating a particular Lazy value +// depends on the same lazy value becuase the blackhole will be called instead. +// This type is not currently threadsafe. +template class Lazy { +private: + mutable std::function Func; + mutable Optional Value; + +public: + Lazy(const Lazy &) = delete; + Lazy(Lazy &&) = default; + // Allow Lazy values to be default constructed to an empty state. + Lazy() = default; + explicit Lazy(std::function &&F) : Func(std::move(F)) {} + + Lazy &operator=(std::function &&F) { + // Once a thunk has been assigned don't allow it to change. + assert(!Value && !Func); + Func = F; + return *this; + } + Lazy &operator=(T &&Val) { + assert(!Value && !Func); + Value = std::move(Val); + return *this; + } + Lazy &operator=(const T &Val) { + assert(!Value && !Func); + Value = Val; + return *this; + } + + const T &operator*() const { + // Assert that a value has been assigned, lazy or otherwise. + assert(Value || Func); + if (Value) + return *Value; + std::function TFunc{std::move(Func)}; + Func = [](T&) { llvm_unreachable("cycle detected"); }; + Value.emplace(); + TFunc(*Value); + return *Value; + } + const T *operator->() const { return &**this; } +}; + +template Lazy makeLazy(std::function &&F) { + return Lazy{std::move(F)}; +} + /// This function behaves similarly to StringRef::substr(), but attempts to /// terminate the returned StringRef at the first null terminator. If no null /// terminator is found, an error is returned. /// /// @param Str Source string to create a substring from. /// @param Offset The start index of the desired substring. -static Expected terminatedSubstr(StringRef Str, size_t Offset) { +Expected terminatedSubstr(StringRef Str, size_t Offset) { size_t StrEnd = Str.find('\0', Offset); if (StrEnd == StringLiteral::npos) { return createError( @@ -78,8 +142,7 @@ /// @param Dyn Target DynamicEntries struct to populate. /// @param DynTable Source dynamic table. template -static Error populateDynamic(DynamicEntries &Dyn, - typename ELFT::DynRange DynTable) { +Error populateDynamic(DynamicEntries &Dyn, typename ELFT::DynRange DynTable) { if (DynTable.empty()) return createError("No .dynamic section found"); @@ -128,19 +191,17 @@ "Couldn't locate dynamic symbol table (no DT_SYMTAB entry)"); } if (Dyn.SONameOffset.hasValue() && *Dyn.SONameOffset >= Dyn.StrSize) { - return createStringError( - object_error::parse_failed, - "DT_SONAME string offset (0x%016" PRIx64 - ") outside of dynamic string table", - *Dyn.SONameOffset); + return createStringError(object_error::parse_failed, + "DT_SONAME string offset (0x%016" PRIx64 + ") outside of dynamic string table", + *Dyn.SONameOffset); } for (uint64_t Offset : Dyn.NeededLibNames) { if (Offset >= Dyn.StrSize) { - return createStringError( - object_error::parse_failed, - "DT_NEEDED string offset (0x%016" PRIx64 - ") outside of dynamic string table", - Offset); + return createStringError(object_error::parse_failed, + "DT_NEEDED string offset (0x%016" PRIx64 + ") outside of dynamic string table", + Offset); } } @@ -151,7 +212,7 @@ /// /// @param Table The GNU hash table for .dynsym. template -static uint64_t getDynSymtabSize(const typename ELFT::GnuHash &Table) { +uint64_t getDynSymtabSize(const typename ELFT::GnuHash &Table) { using Elf_Word = typename ELFT::Word; if (Table.nbuckets == 0) return Table.symndx + 1; @@ -179,8 +240,8 @@ /// @param Dyn Entries with the locations of hash tables. /// @param ElfFile The ElfFile that the section contents reside in. template -static Expected getNumSyms(DynamicEntries &Dyn, - const ELFFile &ElfFile) { +Expected getNumSyms(DynamicEntries &Dyn, + const ELFFile &ElfFile) { using Elf_Hash = typename ELFT::Hash; using Elf_GnuHash = typename ELFT::GnuHash; // Search GNU hash table to try to find the upper bound of dynsym. @@ -209,19 +270,19 @@ /// Other symbol types are mapped to ELFSymbolType::Unknown. /// /// @param Info Binary symbol st_info to extract symbol type from. -static ELFSymbolType convertInfoToType(uint8_t Info) { +ELFSymbolType convertInfoToType(uint8_t Info) { Info = Info & 0xf; switch (Info) { - case ELF::STT_NOTYPE: - return ELFSymbolType::NoType; - case ELF::STT_OBJECT: - return ELFSymbolType::Object; - case ELF::STT_FUNC: - return ELFSymbolType::Func; - case ELF::STT_TLS: - return ELFSymbolType::TLS; - default: - return ELFSymbolType::Unknown; + case ELF::STT_NOTYPE: + return ELFSymbolType::NoType; + case ELF::STT_OBJECT: + return ELFSymbolType::Object; + case ELF::STT_FUNC: + return ELFSymbolType::Func; + case ELF::STT_TLS: + return ELFSymbolType::TLS; + default: + return ELFSymbolType::Unknown; } } @@ -231,8 +292,7 @@ /// @param SymName The desired name of the ELFSymbol. /// @param RawSym ELFT::Sym to extract symbol information from. template -static ELFSymbol createELFSym(StringRef SymName, - const typename ELFT::Sym &RawSym) { +ELFSymbol createELFSym(StringRef SymName, const typename ELFT::Sym &RawSym) { ELFSymbol TargetSym(SymName); uint8_t Binding = RawSym.getBinding(); if (Binding == STB_WEAK) @@ -258,9 +318,8 @@ /// @param DynSym Range of dynamic symbols to add to TargetStub. /// @param DynStr StringRef to the dynamic string table. template -static Error populateSymbols(ELFStub &TargetStub, - const typename ELFT::SymRange DynSym, - StringRef DynStr) { +Error populateSymbols(ELFStub &TargetStub, const typename ELFT::SymRange DynSym, + StringRef DynStr) { // Skips the first symbol since it's the NULL symbol. for (auto RawSym : DynSym.drop_front(1)) { // If a symbol does not have global or weak binding, ignore it. @@ -286,7 +345,7 @@ /// Returns a new ELFStub with all members populated from an ELFObjectFile. /// @param ElfObj Source ELFObjectFile. template -static Expected> +Expected> buildStub(const ELFObjectFile &ElfObj) { using Elf_Dyn_Range = typename ELFT::DynRange; using Elf_Phdr_Range = typename ELFT::PhdrRange; @@ -311,7 +370,7 @@ if (Error Err = populateDynamic(DynEnt, *DynTable)) return std::move(Err); - // Get pointer to in-memory location of .dynstr section. + // Get pointer to in-memory location of .dynstr section. Expected DynStrPtr = ElfFile->toMappedAddr(DynEnt.StrTabAddr); if (!DynStrPtr) @@ -355,9 +414,8 @@ if (!DynSymPtr) return appendToError(DynSymPtr.takeError(), "when locating .dynsym section contents"); - Elf_Sym_Range DynSyms = - ArrayRef(reinterpret_cast(*DynSymPtr), - *SymCount); + Elf_Sym_Range DynSyms = ArrayRef( + reinterpret_cast(*DynSymPtr), *SymCount); Error SymReadError = populateSymbols(*DestStub, DynSyms, DynStr); if (SymReadError) return appendToError(std::move(SymReadError), @@ -367,6 +425,235 @@ return std::move(DestStub); } +/// This initializes an ELF file header with information specific to a binary +/// dynamic shared object. +/// Offsets, indexes, links, etc. for section and program headers are just +/// zero-initialized as they will be updated elsewhere. +/// +/// @param ElfHeader Target ELFT::Ehdr to populate. +/// @param Machine Target architecture (e_machine from ELF specifications). +template +void initELFHeader(typename ELFT::Ehdr &ElfHeader, uint16_t Machine) { + using Elf_Ehdr = typename ELFT::Ehdr; + using Elf_Phdr = typename ELFT::Phdr; + using Elf_Shdr = typename ELFT::Shdr; + + memset(&ElfHeader, 0, sizeof(Elf_Ehdr)); + // ELF identification. + ElfHeader.e_ident[EI_MAG0] = ELFMAG0; + ElfHeader.e_ident[EI_MAG1] = ELFMAG1; + ElfHeader.e_ident[EI_MAG2] = ELFMAG2; + ElfHeader.e_ident[EI_MAG3] = ELFMAG3; + ElfHeader.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; + bool IsLittleEndian = ELFT::TargetEndianness == support::little; + ElfHeader.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; + ElfHeader.e_ident[EI_VERSION] = EV_CURRENT; + ElfHeader.e_ident[EI_OSABI] = ELFOSABI_NONE; + ElfHeader.e_ident[EI_ABIVERSION] = 0; + + // Remainder of ELF header. + ElfHeader.e_type = ET_DYN; + ElfHeader.e_machine = Machine; + ElfHeader.e_version = EV_CURRENT; + ElfHeader.e_entry = 0; + ElfHeader.e_flags = 0; + ElfHeader.e_ehsize = sizeof(Elf_Ehdr); + ElfHeader.e_phentsize = sizeof(Elf_Phdr); + ElfHeader.e_shentsize = sizeof(Elf_Shdr); +} + +template struct OutputSection { + using Elf_Shdr = typename ELFT::Shdr; + std::string Name; + Lazy Shdr; + Lazy Addr; + Lazy Offset; + Lazy Size; + Lazy Align; + uint32_t Index; + bool NoBits = true; +}; + +template +struct ContentSection : public OutputSection { + Lazy Content; + ContentSection() { this->NoBits = false; } +}; + +// Lazy<_> requires a default constructable type but StringTableBuilder is not +// default constructable. This class just wraps it for the purpose of adding a +// default constructor. +class ELFStringTableBuilder : public StringTableBuilder { +public: + ELFStringTableBuilder() : StringTableBuilder(StringTableBuilder::ELF) {} +}; + +template class ELFBuilder { +public: + using Elf_Ehdr = typename ELFT::Ehdr; + using Elf_Shdr = typename ELFT::Shdr; + using Elf_Phdr = typename ELFT::Phdr; + using Elf_Sym = typename ELFT::Sym; + using Elf_Addr = typename ELFT::Addr; + using Elf_Dyn = typename ELFT::Dyn; + +private: + Lazy ElfHeader; + ContentSection StrTab; + + template static void Write(uint8_t *Data, const T &Value) { + *reinterpret_cast(Data) = Value; + } + uint64_t ShdrOffset(const OutputSection &Sec) const { + return ElfHeader->e_shoff + Sec.Index * sizeof(Elf_Shdr); + } + void WriteShdr(uint8_t *Data, const OutputSection &Sec) const { + Write(Data + ShdrOffset(Sec), *Sec.Shdr); + } + +public: + ELFBuilder(const ELFBuilder &) = delete; + ELFBuilder(ELFBuilder &&) = default; + explicit ELFBuilder(const ELFStub &Stub) { + std::vector *> Sections; + Sections.push_back(&StrTab); + + // Each Lazy value we define here should be thought of as nodes in a build + // graph. The dependencies of each step are implicitly captured by useage + // and are not explicitlly stored either. At the end we'll request each of + // the different parts to be written out and no extra work will be done if + // we transitively depend on the same thing twice. Below you can see that + // LastSection->Offset and LastSection->Size are depended on here despite + // us not setting those lazy values until later. The means by which to + // evaluate them will be defined later but during evaluation when we + // request the ElfHeader content it will go on to request the LastSection's + // Offset and Size which might in turn request other things be evaluated. + const OutputSection *LastSection = Sections.back(); + ElfHeader = [this, &Stub, LastSection](Elf_Ehdr &Ehdr) { + initELFHeader(Ehdr, Stub.Arch); + Ehdr.e_shstrndx = StrTab.Index; + Ehdr.e_shnum = LastSection->Index + 1; + Ehdr.e_phnum = 0; + Ehdr.e_phoff = 0; + if (LastSection->NoBits) + Ehdr.e_shoff = alignTo(*LastSection->Offset, sizeof(Elf_Addr)); + else + Ehdr.e_shoff = alignTo(*LastSection->Offset + *LastSection->Size, + sizeof(Elf_Addr)); + }; + + // Manually set the indexes of these. + uint64_t Index = 1; + const OutputSection *Prev = nullptr; + // TODO: Push this back for ProgramHeaders + uint64_t StartOffset = sizeof(Elf_Ehdr); + // Now set the Index, Offset, and Addr of everything. + for (auto Sec : Sections) { + Sec->Index = Index++; + Sec->Offset = [=](uint64_t &Offset) { + uint64_t Align = *Sec->Align ? *Sec->Align : 1; + // Don't align the offset of a NOBITS section. + if (Sec->NoBits) + Align = 1; + if (Prev == nullptr) + Offset = alignTo(StartOffset, Align); + else if (Prev->NoBits) + Offset = alignTo(*Prev->Offset, Align); + else + Offset = alignTo(*Prev->Offset + *Prev->Size, Align); + }; + Sec->Addr = [=](uint64_t &Addr) { + uint64_t Align = *Sec->Align ? *Sec->Align : 1; + if (Prev == nullptr) + Addr = alignTo(StartOffset, Align); + else + Addr = alignTo(*Prev->Addr + *Prev->Size, Align); + }; + Prev = Sec; + } + + // Define the string table. + StrTab.Name = ".dynstr"; + StrTab.Shdr = [this](Elf_Shdr &Shdr) { + Shdr.sh_name = StrTab.Content->getOffset(StrTab.Name); + Shdr.sh_type = SHT_STRTAB; + Shdr.sh_flags = SHF_ALLOC; + Shdr.sh_size = *StrTab.Size; + Shdr.sh_addr = *StrTab.Addr; + Shdr.sh_offset = *StrTab.Offset; + }; + StrTab.Size = [this](uint64_t &Size) { Size = StrTab.Content->getSize(); }; + StrTab.Align = 0; + StrTab.Content = [this](ELFStringTableBuilder &Builder) { + Builder.add(StrTab.Name); + Builder.finalize(); + }; + } + + // GetSize will effectivelly compute the whole layout. + size_t GetSize() const { + return ElfHeader->e_shoff + ElfHeader->e_shnum * sizeof(Elf_Shdr); + } + + void Write(uint8_t *Data) const { + Write(Data, *ElfHeader); + StrTab.Content->write(Data + StrTab.Shdr->sh_offset); + WriteShdr(Data, StrTab); + } +}; + +/// This function opens a file for writing and then writes a binary ELF stub to +/// the file. +/// +/// @param FilePath File path for writing the ELF binary. +/// @param Stub Source ELFStub to generate a binary ELF stub from. +template +Error writeELFBinaryToFile(StringRef FilePath, const ELFStub &Stub) { + ELFBuilder Builder{Stub}; + Expected> BufOrError = + FileOutputBuffer::create(FilePath, Builder.GetSize()); + if (!BufOrError) { + Error FileReadError = BufOrError.takeError(); + std::string Message; + raw_string_ostream Stream(Message); + Stream << FileReadError; + Stream << " when trying to open `" << FilePath << "` for writing"; + consumeError(std::move(FileReadError)); + return createStringError(errc::invalid_argument, Stream.str().c_str()); + } + + // Write binary to file. + std::unique_ptr Buf = std::move(*BufOrError); + Builder.Write(Buf->getBufferStart()); + + if (Error FileWriteError = Buf->commit()) + return FileWriteError; + + return Error::success(); +} + +} // end namespace + +namespace llvm { +namespace elfabi { + +// This function wraps the ELFT writeELFBinaryToFile() so writeBinaryStub() +// can be called without having to use ELFType templates directly. +Error writeBinaryStub(StringRef FilePath, const ELFStub &Stub, + ELFTarget OutputFormat) { + if (OutputFormat == ELFTarget::ELF32LE) { + return writeELFBinaryToFile(FilePath, Stub); + } else if (OutputFormat == ELFTarget::ELF32BE) { + return writeELFBinaryToFile(FilePath, Stub); + } else if (OutputFormat == ELFTarget::ELF64LE) { + return writeELFBinaryToFile(FilePath, Stub); + } else if (OutputFormat == ELFTarget::ELF64BE) { + return writeELFBinaryToFile(FilePath, Stub); + } + return createStringError(errc::invalid_argument, + "Invalid binary output target"); +} + Expected> readELFFile(MemoryBufferRef Buf) { Expected> BinOrErr = createBinary(Buf); if (!BinOrErr) { Index: llvm/tools/llvm-elfabi/llvm-elfabi.cpp =================================================================== --- llvm/tools/llvm-elfabi/llvm-elfabi.cpp +++ llvm/tools/llvm-elfabi/llvm-elfabi.cpp @@ -49,6 +49,17 @@ "soname", cl::desc("Manually set the DT_SONAME entry of any emitted files"), cl::value_desc("name")); +cl::opt BinaryOutputTarget( + "output-target", cl::desc("Create a binary stub for the specified target"), + cl::values(clEnumValN(ELFTarget::ELF32LE, "elf32-little", + "32-bit little-endian ELF stub"), + clEnumValN(ELFTarget::ELF32BE, "elf32-big", + "32-bit big-endian ELF stub"), + clEnumValN(ELFTarget::ELF64LE, "elf64-little", + "64-bit little-endian ELF stub"), + clEnumValN(ELFTarget::ELF64BE, "elf64-big", + "64-bit big-endian ELF stub"))); +cl::opt BinaryOutputFilePath(cl::Positional, cl::desc("output")); /// writeTBE() writes a Text-Based ELF stub to a file using the latest version /// of the YAML parser. @@ -127,16 +138,32 @@ std::unique_ptr TargetStub = std::move(StubOrErr.get()); + // Change SoName before emitting stubs. + if (SOName.getNumOccurrences() == 1) { + TargetStub->SoName = SOName; + } + // Write out .tbe file. if (EmitTBE.getNumOccurrences() == 1) { TargetStub->TbeVersion = TBEVersionCurrent; - if (SOName.getNumOccurrences() == 1) { - TargetStub->SoName = SOName; - } Error TBEWriteError = writeTBE(EmitTBE, *TargetStub); if (TBEWriteError) { WithColor::error() << TBEWriteError << "\n"; exit(1); } } + + // Write out binary ELF stub. + if (BinaryOutputFilePath.getNumOccurrences() == 1) { + if (BinaryOutputTarget.getNumOccurrences() == 0) { + WithColor::error() << "No binary output target specified.\n"; + exit(1); + } + Error BinaryWriteError = writeBinaryStub(BinaryOutputFilePath, *TargetStub, + BinaryOutputTarget); + if (BinaryWriteError) { + WithColor::error() << BinaryWriteError << "\n"; + exit(1); + } + } }