From 740b4ff8657a83eba76773ab8e1633350bd307b6 Mon Sep 17 00:00:00 2001 From: John Reiser Date: Sat, 15 Feb 2020 15:04:44 -0800 Subject: [PATCH] ELF shared lib: PT_DYNAMIC might be below xct_off, such as MIPS https://github.com/upx/upx/issues/298 modified: p_lx_elf.cpp --- src/p_lx_elf.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index c03d6ee6..b97d88c7 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -439,6 +439,12 @@ off_t PackLinuxElf32::pack3(OutputFile *fo, Filter &ft) if ((xct_off - ioff) < len) { // Change length of compressed PT_LOAD. set_te32(&phdr->p_filesz, sz_pack2 + lsize - ioff); set_te32(&phdr->p_memsz, sz_pack2 + lsize - ioff); + if (user_init_off < xct_off) { // MIPS puts PT_DYNAMIC here + // Allow for DT_INIT in a new [stolen] slot + unsigned off2 = user_init_off - sizeof(word); + fo->seek(off2, SEEK_SET); + fo->rewrite(&file_image[off2], 2*sizeof(word)); + } } else if (xct_off < ioff) { // Slide subsequent PT_LOAD. if ((1u<<12) < align) { @@ -564,6 +570,12 @@ off_t PackLinuxElf64::pack3(OutputFile *fo, Filter &ft) if ((xct_off - ioff) < len) { // Change length of compressed PT_LOAD. set_te64(&phdr->p_filesz, sz_pack2 + lsize - ioff); set_te64(&phdr->p_memsz, sz_pack2 + lsize - ioff); + if (user_init_off < xct_off) { // MIPS puts PT_DYNAMIC here + // Allow for DT_INIT in a new [stolen] slot + unsigned off2 = user_init_off - sizeof(word); + fo->seek(off2, SEEK_SET); + fo->rewrite(&file_image[off2], 2*sizeof(word)); + } } else if (xct_off < ioff) { // Slide subsequent PT_LOAD. // AMD64 chip supports page sizes of 4KiB, 2MiB, and 1GiB;