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 @@ -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,44 @@ Data: ELFDATA2LSB Type: ET_DYN Machine: EM_X86_64 +Sections: + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x0270 + AddressAlign: 8 + Content: "0a0000000000000001000000000000000500000000000000b80200000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_STRTAB 0x02b8 + # DT_NULL 0x0 + Size: 48 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x02a0 + AddressAlign: 8 + EntSize: 24 + Content: "000000000000000000000000000000000000000000000000" + Link: .dynstr + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x02b8 +# \0 + Content: "00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x02a0 + PAddr: 0x02a0 + Sections: + - Section: .dynsym + - Section: .dynstr + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x0270 + PAddr: 0x0270 + 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,46 @@ +# 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 + AddressAlign: 8 + 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 + AddressAlign: 8 + 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-bad-vaddr.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-bad-vaddr.test @@ -0,0 +1,49 @@ +# 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: 0x2000 + Content: "0e000000000000000d000000000000000a000000000000001c000000000000000500000000000000600200000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 28 (0x1c) + # DT_STRTAB 0x0260 + # DT_NULL 0x0 + Size: 64 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x1000 + EntSize: 24 + Content: "000000000000000000000000000000000000000000000000" + Link: .dynstr + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1018 +# \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: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynstr + - Section: .dynsym + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x2000 + PAddr: 0x2000 + Sections: + - Section: .dynamic + +# CHECK: No PT_LOAD segment contained vaddr 0x0000000000000260 when converting .dynstr address to an offset + 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,37 @@ +# 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 + AddressAlign: 8 + 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,37 @@ +# 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 + AddressAlign: 8 + 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-no-null.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-soname-no-null.test @@ -0,0 +1,50 @@ +# 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 + AddressAlign: 8 + Content: "0e000000000000000d000000000000000a0000000000000017000000000000000500000000000000181000000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 23 (0x17) + # DT_STRTAB 0x1018 + # DT_NULL 0x0 + Size: 64 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x1000 + AddressAlign: 8 + EntSize: 24 + Content: "000000000000000000000000000000000000000000000000" + Link: .dynstr + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1018 +# \0 b a z\0 n o t\0 b a r\0 s o m e l i b . s o z z z\0 + Content: "0062617a006e6f740062617200736f6d656c69622e736f7a7a7a00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynstr + - Section: .dynsym + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x2000 + PAddr: 0x2000 + Sections: + - Section: .dynamic + +# CHECK: String overran bounds of string table (no null terminator) when reading DT_SONAME Index: llvm/test/tools/llvm-elfabi/binary-read-soname.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-soname.test @@ -0,0 +1,55 @@ +# 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: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x2000 + AddressAlign: 8 + Content: "0e000000000000000d000000000000000a000000000000001c000000000000000500000000000000181000000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 28 (0x1c) + # DT_STRTAB 0x1018 + # DT_NULL 0x0 + Size: 64 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x1000 + AddressAlign: 8 + EntSize: 24 + Content: "000000000000000000000000000000000000000000000000" + Link: .dynstr + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x1018 +# \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: 0x1000 + PAddr: 0x1000 + Sections: + - Section: .dynstr + - Section: .dynsym + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x2000 + PAddr: 0x2000 + 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/binary-read-zero-dt-strsz.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-zero-dt-strsz.test @@ -0,0 +1,47 @@ +# 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 + AddressAlign: 8 + Content: "0e000000000000000d000000000000000a0000000000000000000000000000000500000000000000b00200000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 0 (0x0) + # DT_STRTAB 0x2b0 + # DT_NULL 0x0 + Size: 64 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x2000 + AddressAlign: 8 + 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: DT_STRSZ must be greater than zero Index: llvm/test/tools/llvm-elfabi/binary-read-zero-dt-strtab.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-elfabi/binary-read-zero-dt-strtab.test @@ -0,0 +1,47 @@ +# 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 + AddressAlign: 8 + Content: "0e000000000000000d000000000000000a000000000000001c000000000000000500000000000000000000000000000000000000000000000000000000000000" + # DT_SONAME 13 (0x0d) + # DT_STRSZ 28 (0x1c) + # DT_STRTAB 0x0 + # DT_NULL 0x0 + Size: 64 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x2000 + AddressAlign: 8 + 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: DT_STRTAB offset cannot be zero 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 @@ -1,5 +1,5 @@ # RUN: yaml2obj %s > %t -# RUN: llvm-elfabi %t --emit-tbe=- --soname=best.so | FileCheck %s +# RUN: llvm-elfabi --elf %t --emit-tbe=- --soname=best.so | FileCheck %s !ELF FileHeader: @@ -7,6 +7,44 @@ Data: ELFDATA2LSB Type: ET_DYN Machine: EM_AARCH64 +Sections: + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC ] + Address: 0x1000 + AddressAlign: 8 + Content: "0a0000000000000001000000000000000500000000000000180000000000000000000000000000000000000000000000" + # DT_STRSZ 1 (0x1) + # DT_STRTAB 0x18 + # DT_NULL 0x0 + Size: 48 + - Name: .dynsym + Type: SHT_DYNSYM + Flags: [ SHF_ALLOC ] + Address: 0x0000 + AddressAlign: 8 + EntSize: 24 + Content: "000000000000000000000000000000000000000000000000" + Link: .dynstr + - Name: .dynstr + Type: SHT_STRTAB + Flags: [ SHF_ALLOC ] + Address: 0x0018 +# \0 + Content: "00" +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x0000 + PAddr: 0x0000 + Sections: + - Section: .dynstr + - Section: .dynsym + - Type: PT_DYNAMIC + Flags: [ PF_X, PF_R ] + VAddr: 0x2000 + PAddr: 0x2000 + 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,179 @@ using namespace llvm::elfabi; using namespace llvm::ELF; +namespace { + +// 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()); +} + +} // end anonymous namespace + 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.StrTabAddr = Entry.d_un.d_ptr; + if (Dyn.StrTabAddr == 0) + return createError("DT_STRTAB offset cannot be zero"); + break; + case DT_STRSZ: + Dyn.StrSize = Entry.d_un.d_val; + if (Dyn.StrSize == 0) + return createError("DT_STRSZ must be greater than zero"); + break; + } + } + + if (Dyn.StrTabAddr == 0) { + return createError( + "Couldn't locate dynamic string table (no DT_STRTAB entry)"); + } + if (Dyn.StrSize == 0) { + 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(); +} + +/// This function uses PT_LOAD segments to convert a virtual address to its +/// offset in the ELF binary. If `Range` is specified, it ensures the PT_LOAD +/// segment contains a range of addresses starting from `Addr` of size `Range`. +/// +/// @param PHdrs Program headers from binary. +/// @param Addr The virtual address to convert into an offset in the binary. +/// @param Range The size of the address range. +template +static Expected addrAsOffset(typename ELFT::PhdrRange PHdrs, + uint64_t Addr, uint64_t Range = 1) { + for (auto &Entry : PHdrs) { + if (Entry.p_type != PT_LOAD) + continue; + + if (Addr >= Entry.p_vaddr && + Addr < Entry.p_vaddr + Entry.p_filesz) { + // Ensure entire address range. + if (Addr + Range > Entry.p_vaddr + Entry.p_filesz) { + return createError("Address range exceeded PT_LOAD segment bounds"); + } + uint64_t OffsetInSegment = Addr - Entry.p_vaddr; + return Entry.p_offset + OffsetInSegment; + } + } + + return createStringError(object_error::parse_failed, + "No PT_LOAD segment contained vaddr 0x%016x", Addr); +} + /// 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; + 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 DynStrOffset = + addrAsOffset(*PHdrs, DynEnt.StrTabAddr, DynEnt.StrSize); + if (!DynStrOffset) + return appendToError(DynStrOffset.takeError(), + "when converting .dynstr address to an offset"); + + const char *DynStrPtr = reinterpret_cast(ElfFile->base()) + + *DynStrOffset; + StringRef DynStr(DynStrPtr, 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.