From 4a61b24abcd737f99209cc6de9484c4dc85f2619 Mon Sep 17 00:00:00 2001 From: Eytan Naim Date: Tue, 22 Sep 2020 10:53:58 +0300 Subject: [PATCH] Fix corrupted prog headers --- src/patchelf.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/patchelf.cc b/src/patchelf.cc index c62d7b69..de8a3c7a 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -757,12 +757,15 @@ void ElfFile::rewriteSectionsLibrary() PT_LOAD segment located directly after the last virtual address page of other segments. */ Elf_Addr startPage = 0; + Elf_Addr firstPage = 0; for (unsigned int i = 0; i < phdrs.size(); ++i) { Elf_Addr thisPage = roundUp(rdi(phdrs[i].p_vaddr) + rdi(phdrs[i].p_memsz), getPageSize()); if (thisPage > startPage) startPage = thisPage; + if (rdi(phdrs[i].p_type) == PT_PHDR) firstPage = rdi(phdrs[i].p_vaddr) - rdi(phdrs[i].p_offset); } debug("last page is 0x%llx\n", (unsigned long long) startPage); + debug("first page is 0x%llx\n", (unsigned long long) firstPage); /* When normalizing note segments we will in the worst case be adding 1 program header for each SHT_NOTE section. */ @@ -834,8 +837,9 @@ void ElfFile::rewriteSectionsLibrary() writeReplacedSections(curOff, startPage, startOffset); assert(curOff == startOffset + neededSpace); + /* Write out the updated program and section headers */ - rewriteHeaders(hdr->e_phoff); + rewriteHeaders(firstPage + hdr->e_phoff); }