Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-add-soname.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-add-soname.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-add-soname.test @@ -0,0 +1,48 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-elfabi --elf %t --emit-tbe=- --soname=best.so | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_AARCH64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x0000 + Content: "00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x0008 + AddressAlign: 8 + Content: "0a0000000000000001000000000000000500000000000000000000000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_STRTAB 0x0 + # DT_NULL 0x0 + Size: 48 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x0000 + PAddr: 0x0000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x0008 + PAddr: 0x0008 + Sections: + - Section: .dynamic + +# CHECK: --- !tapi-tbe +# CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} +# CHECK-NEXT: SoName: best.so{{$}} +# CHECK-NEXT: Arch: AArch64 +# CHECK-NEXT: Symbols: {} +# CHECK-NEXT: ... Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-arch.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-arch.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-arch.test @@ -1,5 +1,5 @@ # RUN: yaml2obj %s > %t -# RUN: llvm-elfabi %t --emit-tbe=- | FileCheck %s +# RUN: llvm-elfabi --elf %t --emit-tbe=- | FileCheck %s !ELF FileHeader: @@ -7,6 +7,38 @@ Data: ELFDATA2LSB Type: ET_DYN Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x0000 + Content: "00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x0008 + AddressAlign: 8 + Content: "0a0000000000000001000000000000000500000000000000000000000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_STRTAB 0x0 + # DT_NULL 0x0 + Size: 48 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x0000 + PAddr: 0x0000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x0008 + PAddr: 0x0008 + Sections: + - Section: .dynamic # CHECK: --- !tapi-tbe # CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-bad-soname.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-bad-soname.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-bad-soname.test @@ -0,0 +1,44 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-elfabi --elf %t --emit-tbe=%t.tbe 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x0000 + Content: "00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x0008 + AddressAlign: 8 + Content: "0e000000000000000d000000000000000a0000000000000001000000000000000500000000000000000000000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 1 (0x01) + # DT_STRTAB 0x0 + # DT_NULL 0x0 + Size: 64 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x0000 + PAddr: 0x0000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x0008 + PAddr: 0x0008 + Sections: + - Section: .dynamic + +# CHECK: DT_SONAME string offset (0x000000000000000d) outside of dynamic string table Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-bad-vaddr.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-bad-vaddr.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-bad-vaddr.test @@ -0,0 +1,44 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-elfabi --elf %t --emit-tbe=%t.tbe 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1000 + Content: "00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1008 + AddressAlign: 8 + Content: "0e0000000000000000000000000000000a0000000000000001000000000000000500000000000000600200000000000000000000000000000000000000000000" + # DT_SONAME 0 + # DT_STRSZ 1 + # DT_STRTAB 0x0260 # Bad vaddr (no PT_LOAD for 0x0000 to 0x0FFF) + # DT_NULL 0x0 + Size: 64 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1008 + PAddr: 0x1008 + Sections: + - Section: .dynamic + +# CHECK: Virtual address is not in any segment when locating .dynstr section contents Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dt-strsz.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dt-strsz.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dt-strsz.test @@ -0,0 +1,43 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-elfabi --elf %t --emit-tbe=%t.tbe 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x0000 +# \0 b a z\0 + Content: "0062617a00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x0008 + AddressAlign: 8 + Content: "0500000000000000000000000000000000000000000000000000000000000000" + # DT_STRTAB 0x0 + # DT_NULL 0x0 + Size: 32 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x0000 + PAddr: 0x0000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x0008 + PAddr: 0x0008 + Sections: + - Section: .dynamic + +# CHECK: Couldn't determine dynamic string table size (no DT_STRSZ entry) Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dt-strtab.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dt-strtab.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dt-strtab.test @@ -0,0 +1,42 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-elfabi --elf %t --emit-tbe=%t.tbe 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x0000 + Content: "00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x0008 + AddressAlign: 8 + Content: "0a00000000000000010000000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_NULL 0x0 + Size: 32 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x0000 + PAddr: 0x0000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x0008 + PAddr: 0x0008 + Sections: + - Section: .dynamic + +# CHECK: Couldn't locate dynamic string table (no DT_STRTAB entry) Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dynamic.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dynamic.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-no-dynamic.test @@ -0,0 +1,11 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-elfabi --elf %t --emit-tbe=%t.tbe 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 + +# CHECK: No .dynamic section found Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-replace-soname.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-replace-soname.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-replace-soname.test @@ -0,0 +1,48 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-elfabi --elf %t --emit-tbe=- | FileCheck %s --check-prefix=ORIGINAL +# RUN: llvm-elfabi --elf %t --emit-tbe=- --soname=libbest.so | FileCheck %s --check-prefix=REPLACED + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1000 +# \0 b a z\0 s o m e l i b . s o\0 f o o\0 + Content: "0062617a00736f6d656c69622e736f00666f6f00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1018 + AddressAlign: 8 + Content: "0e0000000000000005000000000000000a0000000000000014000000000000000500000000000000001000000000000000000000000000000000000000000000" + # DT_SONAME 5 (0x05) + # DT_STRSZ 20 (0x14) + # DT_STRTAB 0x1000 + # DT_NULL 0x0 + Size: 64 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1018 + PAddr: 0x1018 + Sections: + - Section: .dynamic + +# ORIGINAL: SoName: somelib.so{{$}} + +# REPLACED: SoName: libbest.so{{$}} Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-soname-no-null.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-soname-no-null.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-soname-no-null.test @@ -0,0 +1,45 @@ +# RUN: yaml2obj %s > %t +# RUN: not llvm-elfabi --elf %t --emit-tbe=%t.tbe 2>&1 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1000 +# \0 b a z\0 s o m e l i b . s o z z z + Content: "0062617a00736f6d656c69622e736f7a7a7a" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1018 + AddressAlign: 8 + Content: "0e0000000000000005000000000000000a000000000000000f000000000000000500000000000000001000000000000000000000000000000000000000000000" + # DT_SONAME 5 (0x05) + # DT_STRSZ 15 (0x0F) + # DT_STRTAB 0x1000 + # DT_NULL 0x0 + Size: 64 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1018 + PAddr: 0x1018 + Sections: + - Section: .dynamic + +# CHECK: String overran bounds of string table (no null terminator) when reading DT_SONAME Index: llvm/trunk/test/tools/llvm-elfabi/binary-read-soname.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/binary-read-soname.test +++ llvm/trunk/test/tools/llvm-elfabi/binary-read-soname.test @@ -0,0 +1,50 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-elfabi --elf %t --emit-tbe=- | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1000 +# \0 b a z\0 s o m e l i b . s o\0 f o o\0 + Content: "0062617a00736f6d656c69622e736f00666f6f00" + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1018 + AddressAlign: 8 + Content: "0e0000000000000005000000000000000a0000000000000014000000000000000500000000000000001000000000000000000000000000000000000000000000" + # DT_SONAME 5 (0x05) + # DT_STRSZ 20 (0x14) + # DT_STRTAB 0x1000 + # DT_NULL 0x0 + Size: 64 + Link: .dynstr +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Align: 8 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1018 + PAddr: 0x1018 + Sections: + - Section: .dynamic + +# CHECK: --- !tapi-tbe +# CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} +# CHECK-NEXT: SoName: somelib.so{{$}} +# CHECK-NEXT: Arch: x86_64 +# CHECK-NEXT: Symbols: {} +# CHECK-NEXT: ... Index: llvm/trunk/test/tools/llvm-elfabi/replace-soname-tbe.test =================================================================== --- llvm/trunk/test/tools/llvm-elfabi/replace-soname-tbe.test +++ llvm/trunk/test/tools/llvm-elfabi/replace-soname-tbe.test @@ -1,16 +0,0 @@ -# RUN: yaml2obj %s > %t -# RUN: llvm-elfabi %t --emit-tbe=- --soname=best.so | FileCheck %s - -!ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_DYN - Machine: EM_AARCH64 - -# CHECK: --- !tapi-tbe -# CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} -# CHECK-NEXT: SoName: best.so -# CHECK-NEXT: Arch: AArch64 -# CHECK-NEXT: Symbols: {} -# CHECK-NEXT: ... Index: llvm/trunk/tools/llvm-elfabi/ELFObjHandler.cpp =================================================================== --- llvm/trunk/tools/llvm-elfabi/ELFObjHandler.cpp +++ llvm/trunk/tools/llvm-elfabi/ELFObjHandler.cpp @@ -21,23 +21,149 @@ using namespace llvm; using namespace llvm::object; -using namespace llvm::elfabi; using namespace llvm::ELF; namespace llvm { namespace elfabi { +// Simple struct to hold relevant .dynamic entries. +struct DynamicEntries { + uint64_t StrTabAddr = 0; + uint64_t StrSize = 0; + Optional SONameOffset; +}; + +/// 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) { + size_t StrEnd = Str.find('\0', Offset); + if (StrEnd == StringLiteral::npos) { + return createError( + "String overran bounds of string table (no null terminator)"); + } + + size_t StrLen = StrEnd - Offset; + return Str.substr(Offset, StrLen); +} + +/// This function takes an error, and appends a string of text to the end of +/// that error. Since "appending" to an Error isn't supported behavior of an +/// Error, this function technically creates a new error with the combined +/// message and consumes the old error. +/// +/// @param Err Source error. +/// @param After Text to append at the end of Err's error message. +Error appendToError(Error Err, StringRef After) { + std::string Message; + raw_string_ostream Stream(Message); + Stream << Err; + Stream << " " << After; + consumeError(std::move(Err)); + return createError(Stream.str().c_str()); +} + +/// This function populates a DynamicEntries struct using an ELFT::DynRange. +/// After populating the struct, the members are validated with +/// some basic sanity checks. +/// +/// @param Dyn Target DynamicEntries struct to populate. +/// @param DynTable Source dynamic table. +template +static Error populateDynamic(DynamicEntries &Dyn, + typename ELFT::DynRange DynTable) { + if (DynTable.empty()) + return createError("No .dynamic section found"); + + // Search .dynamic for relevant entries. + bool FoundDynStr = false; + bool FoundDynStrSz = false; + for (auto &Entry : DynTable) { + switch (Entry.d_tag) { + case DT_SONAME: + Dyn.SONameOffset = Entry.d_un.d_val; + break; + case DT_STRTAB: + Dyn.StrTabAddr = Entry.d_un.d_ptr; + FoundDynStr = true; + break; + case DT_STRSZ: + Dyn.StrSize = Entry.d_un.d_val; + FoundDynStrSz = true; + break; + } + } + + if (!FoundDynStr) { + return createError( + "Couldn't locate dynamic string table (no DT_STRTAB entry)"); + } + if (!FoundDynStrSz) { + return createError( + "Couldn't determine dynamic string table size (no DT_STRSZ entry)"); + } + if (Dyn.SONameOffset.hasValue() && *Dyn.SONameOffset >= Dyn.StrSize) { + return createStringError( + object_error::parse_failed, + "DT_SONAME string offset (0x%016x) outside of dynamic string table", + *Dyn.SONameOffset); + } + + return Error::success(); +} + /// Returns a new ELFStub with all members populated from an ELFObjectFile. /// @param ElfObj Source ELFObjectFile. template -Expected> +static Expected> buildStub(const ELFObjectFile &ElfObj) { + using Elf_Dyn_Range = typename ELFT::DynRange; + using Elf_Phdr_Range = typename ELFT::PhdrRange; std::unique_ptr DestStub = make_unique(); const ELFFile *ElfFile = ElfObj.getELFFile(); + // Fetch .dynamic table. + Expected DynTable = ElfFile->dynamicEntries(); + if (!DynTable) { + return DynTable.takeError(); + } + // Fetch program headers. + Expected PHdrs = ElfFile->program_headers(); + if (!PHdrs) { + return PHdrs.takeError(); + } + + // Collect relevant .dynamic entries. + DynamicEntries DynEnt; + if (Error Err = populateDynamic(DynEnt, *DynTable)) + return std::move(Err); + + // Convert .dynstr address to an offset. + Expected DynStrPtr = + ElfFile->toMappedAddr(DynEnt.StrTabAddr); + if (!DynStrPtr) + return appendToError(DynStrPtr.takeError(), + "when locating .dynstr section contents"); + + StringRef DynStr(reinterpret_cast(DynStrPtr.get()), + DynEnt.StrSize); + + // Populate Arch from ELF header. DestStub->Arch = ElfFile->getHeader()->e_machine; - // TODO: Populate SoName from .dynamic entries and linked string table. + // Populate SoName from .dynamic entries and dynamic string table. + if (DynEnt.SONameOffset.hasValue()) { + Expected NameOrErr = + terminatedSubstr(DynStr, *DynEnt.SONameOffset); + if (!NameOrErr) { + return appendToError(NameOrErr.takeError(), "when reading DT_SONAME"); + } + DestStub->SoName = *NameOrErr; + } + // TODO: Populate NeededLibs from .dynamic entries and linked string table. // TODO: Populate Symbols from .dynsym table and linked string table.