1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 12:43:36 +01:00

[llvm-elfabi] Emit ELF header and string table sections

This change serves to create the initial framework for outputting ELF
files from llvm-elfabi.

Differential Revision: https://reviews.llvm.org/D61767
This commit is contained in:
Haowei Wu 2020-09-28 22:10:54 -07:00
parent 829e70efba
commit 9267952bee
8 changed files with 388 additions and 14 deletions

View File

@ -23,9 +23,21 @@ class MemoryBuffer;
namespace elfabi { namespace elfabi {
enum class ELFTarget { ELF32LE, ELF32BE, ELF64LE, ELF64BE };
/// Attempt to read a binary ELF file from a MemoryBuffer. /// Attempt to read a binary ELF file from a MemoryBuffer.
Expected<std::unique_ptr<ELFStub>> readELFFile(MemoryBufferRef Buf); Expected<std::unique_ptr<ELFStub>> 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 elfabi
} // end namespace llvm } // end namespace llvm

View File

@ -4,6 +4,7 @@ add_llvm_component_library(LLVMInterfaceStub
TBEHandler.cpp TBEHandler.cpp
LINK_COMPONENTS LINK_COMPONENTS
MC
Object Object
Support Support
) )

View File

@ -8,11 +8,14 @@
#include "llvm/InterfaceStub/ELFObjHandler.h" #include "llvm/InterfaceStub/ELFObjHandler.h"
#include "llvm/InterfaceStub/ELFStub.h" #include "llvm/InterfaceStub/ELFStub.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/Binary.h" #include "llvm/Object/Binary.h"
#include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/ELFTypes.h" #include "llvm/Object/ELFTypes.h"
#include "llvm/Support/Errc.h" #include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h" #include "llvm/Support/Error.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
using llvm::MemoryBufferRef; using llvm::MemoryBufferRef;
@ -38,6 +41,158 @@ struct DynamicEntries {
Optional<uint64_t> GnuHash; Optional<uint64_t> 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 <class ELFT>
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 <class ELFT> 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 <class T, class ELFT>
struct ContentSection : public OutputSection<ELFT> {
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 ELFT> 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<OutputSection<ELFT> *> Sections = {&DynStr, &ShStrTab};
const OutputSection<ELFT> *LastSection = Sections.back();
// Now set the Index and put sections names into ".shstrtab".
uint64_t Index = 1;
for (OutputSection<ELFT> *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<ELFT> *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<ELFT>(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<ELFStringTableBuilder, ELFT> DynStr;
ContentSection<ELFStringTableBuilder, ELFT> ShStrTab;
template <class T> static void write(uint8_t *Data, const T &Value) {
*reinterpret_cast<T *>(Data) = Value;
}
void fillStrTabShdr(ContentSection<ELFStringTableBuilder, ELFT> &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<ELFT> &Sec) const {
return ElfHeader.e_shoff + Sec.Index * sizeof(Elf_Shdr);
}
void writeShdr(uint8_t *Data, const OutputSection<ELFT> &Sec) const {
write(Data + shdrOffset(Sec), Sec.Shdr);
}
};
} // end anonymous namespace
/// This function behaves similarly to StringRef::substr(), but attempts to /// This function behaves similarly to StringRef::substr(), but attempts to
/// terminate the returned StringRef at the first null terminator. If no null /// terminate the returned StringRef at the first null terminator. If no null
/// terminator is found, an error is returned. /// terminator is found, an error is returned.
@ -364,6 +519,32 @@ buildStub(const ELFObjectFile<ELFT> &ElfObj) {
return std::move(DestStub); 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 <class ELFT>
static Error writeELFBinaryToFile(StringRef FilePath, const ELFStub &Stub) {
ELFStubBuilder<ELFT> Builder{Stub};
Expected<std::unique_ptr<FileOutputBuffer>> 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<FileOutputBuffer> Buf = std::move(*BufOrError);
Builder.write(Buf->getBufferStart());
if (Error E = Buf->commit())
return E;
return Error::success();
}
Expected<std::unique_ptr<ELFStub>> readELFFile(MemoryBufferRef Buf) { Expected<std::unique_ptr<ELFStub>> readELFFile(MemoryBufferRef Buf) {
Expected<std::unique_ptr<Binary>> BinOrErr = createBinary(Buf); Expected<std::unique_ptr<Binary>> BinOrErr = createBinary(Buf);
if (!BinOrErr) { if (!BinOrErr) {
@ -380,8 +561,22 @@ Expected<std::unique_ptr<ELFStub>> readELFFile(MemoryBufferRef Buf) {
} else if (auto Obj = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) { } else if (auto Obj = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
return buildStub(*Obj); 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<ELF32LE>(FilePath, Stub);
if (OutputFormat == ELFTarget::ELF32BE)
return writeELFBinaryToFile<ELF32BE>(FilePath, Stub);
if (OutputFormat == ELFTarget::ELF64LE)
return writeELFBinaryToFile<ELF64LE>(FilePath, Stub);
if (OutputFormat == ELFTarget::ELF64BE)
return writeELFBinaryToFile<ELF64BE>(FilePath, Stub);
llvm_unreachable("invalid binary output target");
} }
} // end namespace elfabi } // end namespace elfabi

View File

@ -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

View File

@ -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

View File

@ -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'!

View File

@ -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

View File

@ -46,6 +46,17 @@ cl::opt<std::string>
SOName("soname", SOName("soname",
cl::desc("Manually set the DT_SONAME entry of any emitted files"), cl::desc("Manually set the DT_SONAME entry of any emitted files"),
cl::value_desc("name")); cl::value_desc("name"));
cl::opt<ELFTarget> 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<std::string> BinaryOutputFilePath(cl::Positional, cl::desc("output"));
/// writeTBE() writes a Text-Based ELF stub to a file using the latest version /// writeTBE() writes a Text-Based ELF stub to a file using the latest version
/// of the YAML parser. /// of the YAML parser.
@ -111,29 +122,40 @@ static Expected<std::unique_ptr<ELFStub>> readInputFile(StringRef FilePath) {
return EC.makeError(); return EC.makeError();
} }
static void fatalError(Error Err) {
WithColor::defaultErrorHandler(std::move(Err));
exit(1);
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Parse arguments. // Parse arguments.
cl::ParseCommandLineOptions(argc, argv); cl::ParseCommandLineOptions(argc, argv);
Expected<std::unique_ptr<ELFStub>> StubOrErr = readInputFile(InputFilePath); Expected<std::unique_ptr<ELFStub>> StubOrErr = readInputFile(InputFilePath);
if (!StubOrErr) { if (!StubOrErr)
Error ReadError = StubOrErr.takeError(); fatalError(StubOrErr.takeError());
WithColor::error() << ReadError << "\n";
exit(1);
}
std::unique_ptr<ELFStub> TargetStub = std::move(StubOrErr.get()); std::unique_ptr<ELFStub> 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) { if (EmitTBE.getNumOccurrences() == 1) {
TargetStub->TbeVersion = TBEVersionCurrent; TargetStub->TbeVersion = TBEVersionCurrent;
if (SOName.getNumOccurrences() == 1) {
TargetStub->SoName = SOName;
}
Error TBEWriteError = writeTBE(EmitTBE, *TargetStub); Error TBEWriteError = writeTBE(EmitTBE, *TargetStub);
if (TBEWriteError) { if (TBEWriteError)
WithColor::error() << TBEWriteError << "\n"; fatalError(std::move(TBEWriteError));
exit(1);
} }
// 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));
} }
} }