From 893450bc4acadb04df9e5ab60aa67fa3db114c7b Mon Sep 17 00:00:00 2001 From: Daisuke Yamane Date: Tue, 30 Sep 2025 13:21:34 +0900 Subject: [PATCH] Add PF_W to PT_LOAD segment where the .dynamic section is placed This commit fix #146. In some binaries, the .dynamic section could be placed on a read-only PT_LOAD segment. Therefore, add a writable flag to PT_LOAD segment where the .dynamic section is placed. Signed-off-by: Daisuke Yamane --- src/patchelf.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/patchelf.cc b/src/patchelf.cc index 73f1b8d3..794785c7 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -724,6 +724,11 @@ void ElfFile::writeReplacedSections(Elf_Off & curOff, must be sync'ed with it. */ else if (sectionName == ".dynamic") { for (auto & phdr : phdrs) { + if (rdi(phdr.p_type) == PT_LOAD && + rdi(phdr.p_vaddr) <= shdr.sh_addr && + shdr.sh_addr < (rdi(phdr.p_vaddr) + rdi(phdr.p_memsz))) { + phdr.p_flags |= PF_W; + } if (rdi(phdr.p_type) == PT_DYNAMIC) { phdr.p_offset = shdr.sh_offset; phdr.p_vaddr = phdr.p_paddr = shdr.sh_addr;