summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDomen Kožar <domen@dev.si>2021-08-03 14:54:37 +0200
committerGitHub <noreply@github.com>2021-08-03 14:54:37 +0200
commit8889455e54ace253d3b171deeddc31689545f251 (patch)
tree6b2bbf00119bf61a38577650c37852ff646df9a2
parent7ec8edbe094ee13c91dadca191f92b9dfac8c0f9 (diff)
parent33009993176d923e23ff70414197539774e44c3a (diff)
downloadpatchelf-8889455e54ace253d3b171deeddc31689545f251.tar.gz
Merge pull request #292 from ovpanait/issue-291
addNeeded: fix assertion triggered due to bad .dynstr section resize
-rw-r--r--src/patchelf.cc7
-rwxr-xr-xtests/plain-needed.sh23
2 files changed, 27 insertions, 3 deletions
diff --git a/src/patchelf.cc b/src/patchelf.cc
index 65ead62..4ce5600 100644
--- a/src/patchelf.cc
+++ b/src/patchelf.cc
@@ -1592,12 +1592,15 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
auto shdrDynamic = findSection(".dynamic");
auto shdrDynStr = findSection(".dynstr");
+ unsigned int length = 0;
+
/* add all new libs to the dynstr string table */
- unsigned int length = std::count_if(libs.begin(), libs.end(),
- [](const std::string & lib) { return lib.size() + 1; });
+ for (auto &lib : libs)
+ length += lib.size() + 1;
std::string & newDynStr = replaceSection(".dynstr",
rdi(shdrDynStr.sh_size) + length + 1);
+
std::set<Elf64_Xword> libStrings;
unsigned int pos = 0;
for (auto & i : libs) {
diff --git a/tests/plain-needed.sh b/tests/plain-needed.sh
index 36267fb..8967303 100755
--- a/tests/plain-needed.sh
+++ b/tests/plain-needed.sh
@@ -1,4 +1,25 @@
#! /bin/sh
set -e
+
+SCRATCH=scratch/$(basename $0 .sh)
+MAIN_ELF="${SCRATCH}/main"
+
+PATCHELF="../src/patchelf"
+
+rm -rf ${SCRATCH}
+mkdir -p ${SCRATCH}
+cp main ${SCRATCH}/
+
echo "Confirming main requires libfoo"
-../src/patchelf --print-needed main | grep -q libfoo.so
+${PATCHELF} --print-needed "${MAIN_ELF}" | grep -q libfoo.so
+
+echo "Testing --add-needed functionality"
+${PATCHELF} --add-needed bar.so "${MAIN_ELF}"
+${PATCHELF} --print-needed "${MAIN_ELF}" | grep -q bar.so
+
+echo "Testing --remove-needed functionality"
+${PATCHELF} --remove-needed bar.so "${MAIN_ELF}"
+if ${PATCHELF} --print-needed "${MAIN_ELF}" | grep -q bar.so; then
+ echo "ERROR: --remove-needed did not eliminate bar.so!"
+ exit 1
+fi