From 57fe1d383512ea3961e821ee40a6e11402dc9096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 10 Aug 2021 07:54:50 +0200 Subject: [PATCH] fix binaries without .gnu.hash section --- src/patchelf.cc | 9 ++++++--- tests/Makefile.am | 3 ++- tests/no-gnu-hash.sh | 13 +++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100755 tests/no-gnu-hash.sh diff --git a/src/patchelf.cc b/src/patchelf.cc index 75df70cf..47d95c3d 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -1106,9 +1106,12 @@ void ElfFile::rewriteHeaders(Elf_Addr phdrAddress) dyn->d_un.d_ptr = findSection(".dynsym").sh_addr; else if (d_tag == DT_HASH) dyn->d_un.d_ptr = findSection(".hash").sh_addr; - else if (d_tag == DT_GNU_HASH) - dyn->d_un.d_ptr = findSection(".gnu.hash").sh_addr; - else if (d_tag == DT_JMPREL) { + else if (d_tag == DT_GNU_HASH) { + auto shdr = findSection2(".gnu.hash"); + // some binaries might this section stripped + // in which case we just ignore the value. + if (shdr) dyn->d_un.d_ptr = shdr->sh_addr; + } else if (d_tag == DT_JMPREL) { auto shdr = findSection2(".rel.plt"); if (!shdr) shdr = findSection2(".rela.plt"); /* 64-bit Linux, x86-64 */ if (!shdr) shdr = findSection2(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */ diff --git a/tests/Makefile.am b/tests/Makefile.am index eb77313e..23a7098e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -30,7 +30,8 @@ src_TESTS = \ build-id.sh \ invalid-elf.sh \ endianness.sh \ - contiguous_note_sections.sh + contiguous_note_sections.sh \ + no-gnu-hash.sh build_TESTS = \ $(no_rpath_arch_TESTS) diff --git a/tests/no-gnu-hash.sh b/tests/no-gnu-hash.sh new file mode 100755 index 00000000..a98f4599 --- /dev/null +++ b/tests/no-gnu-hash.sh @@ -0,0 +1,13 @@ +#! /bin/sh -e +SCRATCH=scratch/$(basename $0 .sh) + +rm -rf ${SCRATCH} +mkdir -p ${SCRATCH} + +cp simple ${SCRATCH}/ + +strip --remove-section=.gnu.hash ${SCRATCH}/simple + +# Check if patchelf handles binaries with GNU_HASH in dynamic section but +# without .gnu.hash section +../src/patchelf --set-interpreter /oops ${SCRATCH}/simple