Skip to content

Commit 57fe1d3

Browse files
committed
fix binaries without .gnu.hash section
1 parent 5d6f086 commit 57fe1d3

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/patchelf.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,9 +1106,12 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11061106
dyn->d_un.d_ptr = findSection(".dynsym").sh_addr;
11071107
else if (d_tag == DT_HASH)
11081108
dyn->d_un.d_ptr = findSection(".hash").sh_addr;
1109-
else if (d_tag == DT_GNU_HASH)
1110-
dyn->d_un.d_ptr = findSection(".gnu.hash").sh_addr;
1111-
else if (d_tag == DT_JMPREL) {
1109+
else if (d_tag == DT_GNU_HASH) {
1110+
auto shdr = findSection2(".gnu.hash");
1111+
// some binaries might this section stripped
1112+
// in which case we just ignore the value.
1113+
if (shdr) dyn->d_un.d_ptr = shdr->sh_addr;
1114+
} else if (d_tag == DT_JMPREL) {
11121115
auto shdr = findSection2(".rel.plt");
11131116
if (!shdr) shdr = findSection2(".rela.plt"); /* 64-bit Linux, x86-64 */
11141117
if (!shdr) shdr = findSection2(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */

tests/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ src_TESTS = \
3030
build-id.sh \
3131
invalid-elf.sh \
3232
endianness.sh \
33-
contiguous_note_sections.sh
33+
contiguous_note_sections.sh \
34+
no-gnu-hash.sh
3435

3536
build_TESTS = \
3637
$(no_rpath_arch_TESTS)

tests/no-gnu-hash.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#! /bin/sh -e
2+
SCRATCH=scratch/$(basename $0 .sh)
3+
4+
rm -rf ${SCRATCH}
5+
mkdir -p ${SCRATCH}
6+
7+
cp simple ${SCRATCH}/
8+
9+
strip --remove-section=.gnu.hash ${SCRATCH}/simple
10+
11+
# Check if patchelf handles binaries with GNU_HASH in dynamic section but
12+
# without .gnu.hash section
13+
../src/patchelf --set-interpreter /oops ${SCRATCH}/simple

0 commit comments

Comments
 (0)