diff --git a/llvm/include/llvm/InterfaceStub/ELFObjHandler.h b/llvm/include/llvm/InterfaceStub/ELFObjHandler.h --- a/llvm/include/llvm/InterfaceStub/ELFObjHandler.h +++ b/llvm/include/llvm/InterfaceStub/ELFObjHandler.h @@ -23,9 +23,21 @@ 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 diff --git a/llvm/lib/InterfaceStub/ELFObjHandler.cpp b/llvm/lib/InterfaceStub/ELFObjHandler.cpp --- a/llvm/lib/InterfaceStub/ELFObjHandler.cpp +++ b/llvm/lib/InterfaceStub/ELFObjHandler.cpp @@ -8,11 +8,14 @@ #include "llvm/InterfaceStub/ELFObjHandler.h" #include "llvm/InterfaceStub/ELFStub.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" using llvm::MemoryBufferRef; @@ -38,6 +41,158 @@ Optional GnuHash; }; +/// 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 +static void initELFHeader(typename ELFT::Ehdr &ElfHeader, uint16_t Machine) { + memset(&ElfHeader, 0, sizeof(ElfHeader)); + // ELF identification. + ElfHeader.e_ident[EI_MAG0] = ElfMagic[EI_MAG0]; + ElfHeader.e_ident[EI_MAG1] = ElfMagic[EI_MAG1]; + ElfHeader.e_ident[EI_MAG2] = ElfMagic[EI_MAG2]; + ElfHeader.e_ident[EI_MAG3] = ElfMagic[EI_MAG3]; + 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; + + // Remainder of ELF header. + ElfHeader.e_type = ET_DYN; + ElfHeader.e_machine = Machine; + ElfHeader.e_version = EV_CURRENT; + ElfHeader.e_ehsize = sizeof(typename ELFT::Ehdr); + ElfHeader.e_phentsize = sizeof(typename ELFT::Phdr); + ElfHeader.e_shentsize = sizeof(typename ELFT::Shdr); +} + +namespace { +template struct OutputSection { + using Elf_Shdr = typename ELFT::Shdr; + std::string Name; + Elf_Shdr Shdr; + uint64_t Addr; + uint64_t Offset; + uint64_t Size; + uint64_t Align; + uint32_t Index; + bool NoBits = true; +}; + +template +struct ContentSection : public OutputSection { + T Content; + ContentSection() { this->NoBits = false; } +}; + +// This class just wraps StringTableBuilder for the purpose of adding a +// default constructor. +class ELFStringTableBuilder : public StringTableBuilder { +public: + ELFStringTableBuilder() : StringTableBuilder(StringTableBuilder::ELF) {} +}; + +template class ELFStubBuilder { +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; + + ELFStubBuilder(const ELFStubBuilder &) = delete; + ELFStubBuilder(ELFStubBuilder &&) = default; + + explicit ELFStubBuilder(const ELFStub &Stub) { + // Populate string tables. + ShStrTab.Name = ".shstrtab"; + ShStrTab.Align = 1; + DynStr.Name = ".dynstr"; + DynStr.Align = 1; + for (const ELFSymbol &Sym : Stub.Symbols) + DynStr.Content.add(Sym.Name); + + std::vector *> Sections = {&DynStr, &ShStrTab}; + const OutputSection *LastSection = Sections.back(); + // Now set the Index and put sections names into ".shstrtab". + uint64_t Index = 1; + for (OutputSection *Sec : Sections) { + Sec->Index = Index++; + ShStrTab.Content.add(Sec->Name); + } + ShStrTab.Content.finalize(); + ShStrTab.Size = ShStrTab.Content.getSize(); + DynStr.Content.finalize(); + DynStr.Size = DynStr.Content.getSize(); + // Calculate sections' addresses and offsets. + uint64_t CurrentOffset = sizeof(Elf_Ehdr); + for (OutputSection *Sec : Sections) { + Sec->Offset = alignTo(CurrentOffset, Sec->Align); + Sec->Addr = Sec->Offset; + CurrentOffset = Sec->Offset + Sec->Size; + } + // Write section headers of string tables. + fillStrTabShdr(DynStr, SHF_ALLOC); + fillStrTabShdr(ShStrTab); + // Finish initializing the ELF header. + initELFHeader(ElfHeader, Stub.Arch); + ElfHeader.e_shstrndx = ShStrTab.Index; + ElfHeader.e_shnum = LastSection->Index + 1; + ElfHeader.e_shoff = + alignTo(LastSection->Offset + LastSection->Size, sizeof(Elf_Addr)); + } + + size_t getSize() const { + return ElfHeader.e_shoff + ElfHeader.e_shnum * sizeof(Elf_Shdr); + } + + void write(uint8_t *Data) const { + write(Data, ElfHeader); + DynStr.Content.write(Data + DynStr.Shdr.sh_offset); + ShStrTab.Content.write(Data + ShStrTab.Shdr.sh_offset); + writeShdr(Data, DynStr); + writeShdr(Data, ShStrTab); + } + +private: + Elf_Ehdr ElfHeader; + ContentSection DynStr; + ContentSection ShStrTab; + + template static void write(uint8_t *Data, const T &Value) { + *reinterpret_cast(Data) = Value; + } + + void fillStrTabShdr(ContentSection &StrTab, + uint32_t ShFlags = 0) const { + StrTab.Shdr.sh_type = SHT_STRTAB; + StrTab.Shdr.sh_flags = ShFlags; + StrTab.Shdr.sh_addr = StrTab.Addr; + StrTab.Shdr.sh_offset = StrTab.Offset; + StrTab.Shdr.sh_info = 0; + StrTab.Shdr.sh_size = StrTab.Size; + StrTab.Shdr.sh_name = ShStrTab.Content.getOffset(StrTab.Name); + StrTab.Shdr.sh_addralign = StrTab.Align; + StrTab.Shdr.sh_entsize = 0; + StrTab.Shdr.sh_link = 0; + } + + 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); + } +}; +} // end anonymous namespace + /// 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. @@ -364,6 +519,32 @@ return std::move(DestStub); } +/// 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 +static Error writeELFBinaryToFile(StringRef FilePath, const ELFStub &Stub) { + ELFStubBuilder Builder{Stub}; + Expected> BufOrError = + FileOutputBuffer::create(FilePath, Builder.getSize()); + if (!BufOrError) + return createStringError(errc::invalid_argument, + toString(BufOrError.takeError()) + + " when trying to open `" + FilePath + + "` for writing"); + + // Write binary to file. + std::unique_ptr Buf = std::move(*BufOrError); + Builder.write(Buf->getBufferStart()); + + if (Error E = Buf->commit()) + return E; + + return Error::success(); +} + Expected> readELFFile(MemoryBufferRef Buf) { Expected> BinOrErr = createBinary(Buf); if (!BinOrErr) { @@ -380,8 +561,22 @@ } else if (auto Obj = dyn_cast>(Bin)) { return buildStub(*Obj); } + return createStringError(errc::not_supported, "unsupported binary format"); +} - return createStringError(errc::not_supported, "Unsupported binary format"); +// 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); + if (OutputFormat == ELFTarget::ELF32BE) + return writeELFBinaryToFile(FilePath, Stub); + if (OutputFormat == ELFTarget::ELF64LE) + return writeELFBinaryToFile(FilePath, Stub); + if (OutputFormat == ELFTarget::ELF64BE) + return writeELFBinaryToFile(FilePath, Stub); + llvm_unreachable("invalid binary output target"); } } // end namespace elfabi diff --git a/llvm/test/tools/llvm-elfabi/fail-file-write-windows.test b/llvm/test/tools/llvm-elfabi/fail-file-write-windows.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-elfabi/fail-file-write-windows.test @@ -0,0 +1,16 @@ +## Test failing to write output file on windows platform. + +# REQUIRES: system-windows +# RUN: touch %t.TestFile +# RUN: chmod 400 %t.TestFile +# RUN: not llvm-elfabi %s --output-target=elf64-little %t.TestFile 2>&1 | FileCheck %s --check-prefix=ERR +# RUN: chmod 777 %t.TestFile +# RUN: rm -rf %t.TestFile + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: AArch64 +Symbols: {} +... + +# ERR: error: permission denied diff --git a/llvm/test/tools/llvm-elfabi/fail-file-write.test b/llvm/test/tools/llvm-elfabi/fail-file-write.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-elfabi/fail-file-write.test @@ -0,0 +1,18 @@ +## Test failing to write output file on non-windows platforms. + +# UNSUPPORTED: system-windows +# RUN: rm -rf %t.TestDir +# RUN: mkdir %t.TestDir +# RUN: touch %t.TestDir/Output.TestFile +# RUN: chmod 400 %t.TestDir +# RUN: not llvm-elfabi %s --output-target=elf64-little %t.TestDir/Output.TestFile 2>&1 | FileCheck %s --check-prefix=ERR +# RUN: chmod 777 %t.TestDir +# RUN: rm -rf %t.TestDir + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: AArch64 +Symbols: {} +... + +# ERR: Permission denied when trying to open `{{.*}}.TestDir/Output.TestFile` for writing diff --git a/llvm/test/tools/llvm-elfabi/output-target-error.test b/llvm/test/tools/llvm-elfabi/output-target-error.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-elfabi/output-target-error.test @@ -0,0 +1,15 @@ +## Test running llvm-elfabi without specifying a valid target. + +# RUN: not llvm-elfabi %s %t 2>&1 | FileCheck %s --check-prefix=MISSING +# RUN: not llvm-elfabi %s --output-target=nope %t 2>&1 | FileCheck %s --check-prefix=INVALID + +--- !tapi-tbe +SoName: somelib.so +TbeVersion: 1.0 +Arch: x86_64 +Symbols: {} +... + +# MISSING: error: no binary output target specified. + +# INVALID: llvm-elfabi: for the --output-target option: Cannot find option named 'nope'! diff --git a/llvm/test/tools/llvm-elfabi/write-stub.test b/llvm/test/tools/llvm-elfabi/write-stub.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-elfabi/write-stub.test @@ -0,0 +1,95 @@ +## Test writing stub elf with minimal sections. + +# RUN: llvm-elfabi %s --output-target=elf32-little %t.elf32l +# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab %t.elf32l | FileCheck %s -DCLASS="32-bit (0x1)" -DDE="LittleEndian (0x1)" -DHS=52 -DPHES=32 -DSHES=40 + +# RUN: llvm-elfabi %s --output-target=elf32-big %t.elf32b +# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab %t.elf32b | FileCheck %s -DCLASS="32-bit (0x1)" -DDE="BigEndian (0x2)" -DHS=52 -DPHES=32 -DSHES=40 + +# RUN: llvm-elfabi %s --output-target=elf64-little %t.elf64l +# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab %t.elf64l | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 + +# RUN: llvm-elfabi %s --output-target=elf64-big %t.elf64b +# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab %t.elf64b | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="BigEndian (0x2)" -DHS=64 -DPHES=56 -DSHES=64 + +--- !tapi-tbe +TbeVersion: 1.0 +Arch: x86_64 +Symbols: + bar: { Type: Object, Size: 42 } + baz: { Type: TLS, Size: 3 } +... + +# CHECK: ElfHeader { +# CHECK-NEXT: Ident { +# CHECK-NEXT: Magic: (7F 45 4C 46) +# CHECK-NEXT: Class: [[CLASS]] +# CHECK-NEXT: DataEncoding: [[DE]] +# 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: [[HS]] +# CHECK-NEXT: ProgramHeaderEntrySize: [[PHES]] +# CHECK: SectionHeaderEntrySize: [[SHES]] +# CHECK: SectionHeaderCount: 3 +# CHECK: StringTableSectionIndex: 2 + +# CHECK: Section { +# CHECK-NEXT: Index: 0 +# CHECK-NEXT: Name: (0) +# CHECK-NEXT: Type: SHT_NULL +# CHECK-NEXT: Flags [ +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x0 +# CHECK-NEXT: Offset: 0x0 +# CHECK-NEXT: Size: 0 +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 0 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: } +# CHECK-NEXT: Section { +# CHECK-NEXT: Index: 1 +# CHECK-NEXT: Name: .dynstr +# CHECK-NEXT: Type: SHT_STRTAB +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: ] +# CHECK-NEXT: Address: +# CHECK-NEXT: Offset: +# CHECK-NEXT: Size: +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 1 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: } +# CHECK-NEXT: Section { +# CHECK-NEXT: Index: 2 +# CHECK-NEXT: Name: .shstrtab +# CHECK-NEXT: Type: SHT_STRTAB +# CHECK-NEXT: Flags [ +# CHECK-NEXT: ] +# CHECK-NEXT: Address: +# CHECK-NEXT: Offset: +# CHECK-NEXT: Size: +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 1 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: } + +# CHECK: String dump of section '.dynstr': +# CHECK-NEXT: [ 1] baz +# CHECK-NEXT: [ 5] bar + +# CHECK: String dump of section '.shstrtab': +# CHECK-NEXT: [ 1] .dynstr +# CHECK-NEXT: [ 9] .shstrtab diff --git a/llvm/tools/llvm-elfabi/llvm-elfabi.cpp b/llvm/tools/llvm-elfabi/llvm-elfabi.cpp --- a/llvm/tools/llvm-elfabi/llvm-elfabi.cpp +++ b/llvm/tools/llvm-elfabi/llvm-elfabi.cpp @@ -46,6 +46,17 @@ SOName("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. @@ -111,29 +122,40 @@ return EC.makeError(); } +static void fatalError(Error Err) { + WithColor::defaultErrorHandler(std::move(Err)); + exit(1); +} + int main(int argc, char *argv[]) { // Parse arguments. cl::ParseCommandLineOptions(argc, argv); Expected> StubOrErr = readInputFile(InputFilePath); - if (!StubOrErr) { - Error ReadError = StubOrErr.takeError(); - WithColor::error() << ReadError << "\n"; - exit(1); - } + if (!StubOrErr) + fatalError(StubOrErr.takeError()); std::unique_ptr TargetStub = std::move(StubOrErr.get()); - // Write out .tbe file. + // Change SoName before emitting stubs. + if (SOName.getNumOccurrences() == 1) + TargetStub->SoName = SOName; + 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); - } + if (TBEWriteError) + fatalError(std::move(TBEWriteError)); + } + + // Write out binary ELF stub. + if (BinaryOutputFilePath.getNumOccurrences() == 1) { + if (BinaryOutputTarget.getNumOccurrences() == 0) + fatalError(createStringError(errc::not_supported, + "no binary output target specified.")); + Error BinaryWriteError = + writeBinaryStub(BinaryOutputFilePath, *TargetStub, BinaryOutputTarget); + if (BinaryWriteError) + fatalError(std::move(BinaryWriteError)); } }