summaryrefslogtreecommitdiff
path: root/src/patchelf.cc
diff options
context:
space:
mode:
authorRolf Eike Beer <eb@emlix.com>2020-09-17 10:05:43 +0200
committerJörg Thalheim <joerg@thalheim.io>2021-11-27 11:06:49 +0100
commitb4cb6cac7844a112f3fbb1f64ffefd2624d0537e (patch)
treef99a560896eb62af2556662f4fa967569c5df3d1 /src/patchelf.cc
parenta174cf3006baf31e0e9eaa62bc9adead93af63f7 (diff)
downloadpatchelf-b4cb6cac7844a112f3fbb1f64ffefd2624d0537e.tar.gz
fix corrupted library names when using --replace-needed multiple times
When it happens that the .gnu.version_r stores the strings in .dynstr it can come to corruption of the library names written into DT_NEEDED: -the library names in DT_NEEDED are replaced, new entries are written to the end of .dynstr -the version library names are replaced, and written to the end of the string section. If the section for the version strings is also ".dynstr", the previous modifications were _not_ taken into account and things were written from the old end of .dynstr again. The order in which these strings were written is not the same as the previous replacement, so things would end up with the same size, but different offsets. The .gnu.version_r table is correct, the file contents are fine, but the offsets in the DT_NEEDED entries are wrong. Since they are printed as 0-terminated strings the first one replaced will always be shown correct, which also is the case if the argument is only used once as the string is replaced with itself afterwards.
Diffstat (limited to 'src/patchelf.cc')
-rw-r--r--src/patchelf.cc4
1 files changed, 4 insertions, 0 deletions
diff --git a/src/patchelf.cc b/src/patchelf.cc
index 06f41c6..6148abd 100644
--- a/src/patchelf.cc
+++ b/src/patchelf.cc
@@ -1636,6 +1636,10 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
debug("found .gnu.version_r with %i entries, strings in %s\n", verNeedNum, versionRStringsSName.c_str());
unsigned int verStrAddedBytes = 0;
+ // It may be that it is .dynstr again, in which case we must take the already
+ // added bytes into account.
+ if (versionRStringsSName == ".dynstr")
+ verStrAddedBytes += dynStrAddedBytes;
auto need = (Elf_Verneed *)(fileContents->data() + rdi(shdrVersionR.sh_offset));
while (verNeedNum > 0) {