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 @@ -8,6 +8,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-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: 0 (BinaryRead): 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,51 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-elfabi %t -emit-tbe=%t2 +# RUN: cat %t2 | 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 @@ -8,9 +8,35 @@ 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*))}} +#CHECK-NEXT: TbeVersion: {{[1-9]\d*\.(0|([1-9]\d*))}} #CHECK-NEXT: SoName: best.so #CHECK-NEXT: Arch: AArch64 #CHECK-NEXT: Symbols: {} Index: llvm/tools/llvm-elfabi/ELFObjHandler.h =================================================================== --- llvm/tools/llvm-elfabi/ELFObjHandler.h +++ llvm/tools/llvm-elfabi/ELFObjHandler.h @@ -36,6 +36,27 @@ template Error populateArch(ELFStub &TargetStub, const typename ELFT::Ehdr &Header); +/// Attempt to fetch a reference to the dynamic string table of an ELF binary. +/// @param DynamicEntries Reference to an ArrayRef containing dynamic entries. +/// @param ObjBuffer Pointer to the start of the buffer containing the binary. +/// +/// @return A llvm::Error or a StringRef to the dynamic string table. +template +Expected getDynStr(const typename ELFT::DynRange &DynamicEntries, + const uint8_t *ObjBuffer); + +/// Populate the `SoName` member of an ELFStub. +/// Since SoName is optional, this doesn't return any errors and will simply +/// return true if SoName is successfully populated. +/// @param TargetStub Reference to an ELFStub who's member will be populated. +/// @param DynamicEntries Reference to an ArrayRef containing dynamic entries. +/// +/// @return true if TargetStub.SoName is successfully populated. +template +bool populateSoName(ELFStub &TargetStub, + const typename ELFT::DynRange &DynamicEntries, + StringRef DynStr); + /// Attempt to read a binary ELF file from a MemoryBuffer reference. Expected> readELFFile(MemoryBufferRef Buf); Index: llvm/tools/llvm-elfabi/ELFObjHandler.cpp =================================================================== --- llvm/tools/llvm-elfabi/ELFObjHandler.cpp +++ llvm/tools/llvm-elfabi/ELFObjHandler.cpp @@ -32,24 +32,90 @@ buildStub(const object::ELFObjectFile &ElfObj) { std::unique_ptr DestStub = make_unique(); using Elf_Ehdr = typename ELFT::Ehdr; + using Elf_Dyn_Range = typename ELFT::DynRange; const ELFFile *ElfFile = ElfObj.getELFFile(); // Fetch needed tables/headers. This will eventually include .dynamic, // .dynsym, and .dynstr. + // Fetch ELF header. const Elf_Ehdr *ElfHeader = ElfFile->getHeader(); + // Fetch .dynamic entries. + Expected DynOrErr = ElfFile->dynamicEntries(); + if (!DynOrErr) { + return DynOrErr.takeError(); + } + + Elf_Dyn_Range DynamicEntries = DynOrErr.get(); + if (DynamicEntries.size() == 0) + return createError("No .dynamic section found"); + + // Fetch .dynstr string table. + Expected DynStrOrErr = + getDynStr(DynamicEntries, ElfFile->base()); + if (!DynStrOrErr) { + return DynStrOrErr.takeError(); + } + StringRef DynStr = DynStrOrErr.get(); + // Populate architecture. Error ReadArchError = populateArch(*DestStub, *ElfHeader); if (ReadArchError) return std::move(ReadArchError); - // TODO: Populate SoName from .dynamic entries and linked string table. + // Populate SoName from .dynamic entries and dynamic string table. + populateSoName(*DestStub, DynamicEntries, DynStr); + // TODO: Populate NeededLibs from .dynamic entries and linked string table. // TODO: Populate Symbols from .dynsym table and linked string table. return std::move(DestStub); } +template +Expected getDynStr(const typename ELFT::DynRange &DynamicEntries, + const uint8_t *ObjBuffer) { + Optional StrTabOffset; + Optional StrTabSize; + for (auto &Entry : DynamicEntries) { + if (Entry.d_tag == DT_STRTAB) + StrTabOffset = Entry.d_un.d_ptr; + if (Entry.d_tag == DT_STRSZ) + StrTabSize = Entry.d_un.d_val; + } + if (!StrTabOffset.hasValue()) { + return createError( + "Couldn't locate dynamic string table (no DT_STRTAB entry)"); + } + if (!StrTabSize.hasValue()) { + return createError( + "Couldn't determine dynamic string table size (no DT_STRSZ entry)"); + } + const char *StrTabPtr = + reinterpret_cast(ObjBuffer) + StrTabOffset.getValue(); + return StringRef(StrTabPtr, StrTabSize.getValue()); +} + +template +bool populateSoName(ELFStub &TargetStub, + const typename ELFT::DynRange &DynamicEntries, + StringRef DynStr) { + uint64_t SoNameOffset; + for (auto &Entry : DynamicEntries) { + if (Entry.d_tag == DT_SONAME) { + SoNameOffset = Entry.d_un.d_val; + const char *SoNamePtr = DynStr.data() + SoNameOffset; + TargetStub.SoName = std::string(SoNamePtr); + + // SoName has been successfully populated. + return true; + } + } + + // Unable to populate SoName. + return false; +} + template Error populateArch(ELFStub &TargetStub, const typename ELFT::Ehdr &Header) { TargetStub.Arch = Header.e_machine;