Index: llvm/test/tools/llvm-elfabi/binary-read-arch.test =================================================================== --- llvm/test/tools/llvm-elfabi/binary-read-arch.test +++ llvm/test/tools/llvm-elfabi/binary-read-arch.test @@ -7,6 +7,32 @@ Data: ELFDATA2LSB Type: ET_DYN Machine: EM_X86_64 +Sections: + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1000 + Content: "0a0000000000000001000000000000000500000000000000600200000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_STRTAB 0x0260 + # DT_NULL 0x0 + Size: 48 + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x3000 +# \0 + Content: "00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x0000 + PAddr: 0x0000 + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynamic # CHECK: --- !tapi-tbe # CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} Index: llvm/test/tools/llvm-elfabi/binary-read-bad-soname.test =================================================================== --- /dev/null +++ llvm/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: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1000 + Content: "0e000000000000000d000000000000000a0000000000000001000000000000000500000000000000b00200000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 1 (0x01) + # DT_STRTAB 0x2b0 + # DT_NULL 0x0 + Size: 64 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x2000 + EntSize: 24 + Size: 0 + Link: .dynstr + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x3000 + Content: "00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x0000 + PAddr: 0x0000 + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynamic + +# CHECK: DT_SONAME string offset outside of dynamic string table Index: llvm/test/tools/llvm-elfabi/binary-read-no-dt-strsz.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-no-dt-strsz.test @@ -0,0 +1,36 @@ +# 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: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1000 + Content: "0500000000000000500200000000000000000000000000000000000000000000" + # DT_STRTAB 0x0250 + # DT_NULL 0x0 + Size: 32 + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x3000 +# \0 b a z\0 n o t\0 b a r\0 s o m e l i b . s o\0 f o o\0 + Content: "0062617a006e6f740062617200736f6d656c69622e736f00666f6f00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x0000 + PAddr: 0x0000 + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynamic + +# CHECK: Couldn't determine dynamic string table size (no DT_STRSZ entry) Index: llvm/test/tools/llvm-elfabi/binary-read-no-dt-strtab.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-no-dt-strtab.test @@ -0,0 +1,36 @@ +# 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: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1000 + Content: "0a00000000000000010000000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_NULL 0x0 + Size: 32 + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x3000 +# \0 + Content: "00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x0000 + PAddr: 0x0000 + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynamic + +# CHECK: Couldn't locate dynamic string table (no DT_STRTAB entry) Index: llvm/test/tools/llvm-elfabi/binary-read-no-dynamic.test =================================================================== --- /dev/null +++ llvm/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/test/tools/llvm-elfabi/binary-read-soname.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-soname.test @@ -0,0 +1,50 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-elfabi %t --emit-tbe=- | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1000 + Content: "0e000000000000000d000000000000000a000000000000001c000000000000000500000000000000b00200000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 28 (0x1c) + # DT_STRTAB 0x2b0 + # DT_NULL 0x0 + Size: 64 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x2000 + EntSize: 24 + Size: 0 + Link: .dynstr + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x3000 +# \0 b a z\0 n o t\0 b a r\0 s o m e l i b . s o\0 f o o\0 + Content: "0062617a006e6f740062617200736f6d656c69622e736f00666f6f00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x0000 + PAddr: 0x0000 + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + 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/test/tools/llvm-elfabi/replace-soname-tbe.test =================================================================== --- llvm/test/tools/llvm-elfabi/replace-soname-tbe.test +++ llvm/test/tools/llvm-elfabi/replace-soname-tbe.test @@ -7,6 +7,32 @@ Data: ELFDATA2LSB Type: ET_DYN Machine: EM_AARCH64 +Sections: + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1000 + Content: "0a0000000000000001000000000000000500000000000000600200000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_STRTAB 0x0260 + # DT_NULL 0x0 + Size: 48 + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x3000 +# \0 + Content: "00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x0000 + PAddr: 0x0000 + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynamic # CHECK: --- !tapi-tbe # CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} Index: llvm/tools/llvm-elfabi/ELFObjHandler.cpp =================================================================== --- llvm/tools/llvm-elfabi/ELFObjHandler.cpp +++ llvm/tools/llvm-elfabi/ELFObjHandler.cpp @@ -24,20 +24,93 @@ using namespace llvm::elfabi; using namespace llvm::ELF; +namespace { + +// Simple struct to hold relevant .dynamic entries. +struct DynamicEntries { + Optional StrTabOffset; + Optional StrSize; + Optional SONameOffset; +}; + +} + namespace llvm { namespace elfabi { +/// This function populates a DynamicEntries struct using a binary +/// 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 binary 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. + for (auto &Entry : DynTable) { + switch (Entry.d_tag) { + case DT_SONAME: + Dyn.SONameOffset = Entry.d_un.d_val; + break; + case DT_STRTAB: + Dyn.StrTabOffset = Entry.d_un.d_ptr; + break; + case DT_STRSZ: + Dyn.StrSize = Entry.d_un.d_val; + break; + } + } + + if (!Dyn.StrTabOffset.hasValue()) { + return createError( + "Couldn't locate dynamic string table (no DT_STRTAB entry)"); + } + if (!Dyn.StrSize.hasValue()) { + return createError( + "Couldn't determine dynamic string table size (no DT_STRSZ entry)"); + } + if (Dyn.SONameOffset.hasValue() && *Dyn.SONameOffset >= *Dyn.StrSize) { + return createError( + "DT_SONAME string offset outside of dynamic string table"); + } + + return Error::success(); +} + /// Returns a new ELFStub with all members populated from an ELFObjectFile. /// @param ElfObj Source ELFObjectFile. template Expected> -buildStub(const ELFObjectFile &ElfObj) { +static buildStub(const ELFObjectFile &ElfObj) { + using Elf_Dyn_Range = typename ELFT::DynRange; std::unique_ptr DestStub = make_unique(); const ELFFile *ElfFile = ElfObj.getELFFile(); + // Fetch .dynamic table. + Expected DynTable = ElfFile->dynamicEntries(); + if (!DynTable) { + return DynTable.takeError(); + } + // Collect relevant .dynamic entries. + DynamicEntries DynEnt; + if (Error Err = populateDynamic(DynEnt, *DynTable)) + return std::move(Err); + + const char *DynStrPtr = reinterpret_cast(ElfFile->base()) + + *DynEnt.StrTabOffset; + + // 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()) { + DestStub->SoName = std::string(DynStrPtr + *DynEnt.SONameOffset); + } + // TODO: Populate NeededLibs from .dynamic entries and linked string table. // TODO: Populate Symbols from .dynsym table and linked string table.