1From cb8326de54ad7a56658b0dc8efb7da5e71684a7c Mon Sep 17 00:00:00 2001
2From: Pablo Galindo <pablogsal@gmail.com>
3Date: Tue, 22 Sep 2020 01:33:47 +0100
4Subject: [PATCH] Use sh_offset instead of sh_addr when checking already
5 replaced libs
6
7When checking for already replaced libs, the check against the size must
8be done using the section header offset, not the section file address.
9This was not crashing in many situations because normally sh_address and
10sh_offset have the same value but these two may differ and using the
11sh_address value instead can cause library corruption in these
12situations.
13
14Fetch from: https://github.com/NixOS/patchelf/commit/83aa89addf8757e2d63aa73222f2fa9bc6d7321a
15
16Backported to v0.9
17
18Signed-off-by: Conrad Ratschan <conrad.ratschan@rockwellcollins.com>
19---
20 src/patchelf.cc | 2 +-
21 1 file changed, 1 insertion(+), 1 deletion(-)
22
23diff --git a/src/patchelf.cc b/src/patchelf.cc
24index 4676157..c025ae2 100644
25--- a/src/patchelf.cc
26+++ b/src/patchelf.cc
27@@ -666,7 +666,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
28     /* Some sections may already be replaced so account for that */
29     unsigned int i = 1;
30     Elf_Addr pht_size = sizeof(Elf_Ehdr) + (phdrs.size() + 1)*sizeof(Elf_Phdr);
31-    while( shdrs[i].sh_addr <= pht_size && i < rdi(hdr->e_shnum) ) {
32+    while( shdrs[i].sh_offset <= pht_size && i < rdi(hdr->e_shnum) ) {
33         if (not haveReplacedSection(getSectionName(shdrs[i])))
34             replaceSection(getSectionName(shdrs[i]), shdrs[i].sh_size);
35         i++;
36--
372.17.1
38
39