Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple modifications in same call #361

Merged
merged 6 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ Makefile
.direnv/
.vscode/
.idea/
result
20 changes: 16 additions & 4 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1878,10 +1878,22 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con

if (printNeeded) elfFile.printNeededLibs();

elfFile.removeNeeded(neededLibsToRemove);
elfFile.replaceNeeded(neededLibsToReplace);
elfFile.addNeeded(neededLibsToAdd);
elfFile.clearSymbolVersions(symbolsToClearVersion);
if (!neededLibsToRemove.empty()) {
elfFile.removeNeeded(neededLibsToRemove);
elfFile.rewriteSections();
Mic92 marked this conversation as resolved.
Show resolved Hide resolved
}
if (!neededLibsToReplace.empty()) {
elfFile.replaceNeeded(neededLibsToReplace);
elfFile.rewriteSections();
}
if (!neededLibsToAdd.empty()) {
elfFile.addNeeded(neededLibsToAdd);
elfFile.rewriteSections();
}
if (!symbolsToClearVersion.empty()) {
elfFile.clearSymbolVersions(symbolsToClearVersion);
elfFile.rewriteSections();
}

if (noDefaultLib)
elfFile.noDefaultLib();
Mic92 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ src_TESTS = \
basic-flags.sh \
set-empty-rpath.sh \
phdr-corruption.sh \
replace-needed.sh
replace-needed.sh \
replace-add-needed.sh

build_TESTS = \
$(no_rpath_arch_TESTS)
Expand Down
34 changes: 34 additions & 0 deletions tests/replace-add-needed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /bin/sh -e
Mic92 marked this conversation as resolved.
Show resolved Hide resolved
SCRATCH=scratch/$(basename $0 .sh)
PATCHELF=$(readlink -f "../src/patchelf")

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}

cp simple ${SCRATCH}/
cp libfoo.so ${SCRATCH}/
cp libbar.so ${SCRATCH}/


cd ${SCRATCH}

libcldd=$(ldd ./simple | grep -oP "(?<=libc.so.6 => )[^ ]+")

# We have to set the soname on these libraries
${PATCHELF} --set-soname libbar.so ./libbar.so
Comment on lines +16 to +17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly discussed with @trws that if the shared object doesn't have a DT_SONAME, it won't make use of the previously discovered loaded shared object.

This was discovered while writing this test. You can see below that libbar.so was not found, even though its set with an absolute path.

ldd ~/code/github.com/NixOS/patchelf/tests/scratch/replace-add-needed/simple
	linux-vdso.so.1 (0x00007ffc90565000)
	/home/fmzakari/code/github.com/NixOS/patchelf/tests/scratch/replace-add-needed/libbar.so (0x00007f532a4c4000)
	/home/fmzakari/code/github.com/NixOS/patchelf/tests/scratch/replace-add-needed/libfoo.so (0x00007f532a4bf000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f532a2da000)
	/nix/store/jsp3h3wpzc842j0rz61m5ly71ak6qgdn-glibc-2.32-54/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f532a4cb000)
	libbar.so => not found


# Add a libbar.so so we can rewrite it later
${PATCHELF} --add-needed libbar.so ./simple

${PATCHELF} --replace-needed libc.so.6 ${libcldd} \
--replace-needed libbar.so $(readlink -f ./libbar.so) \
--add-needed $(readlink -f ./libfoo.so) \
./simple

exitCode=0
./simple || exitCode=$?

if test "$exitCode" != 0; then
ldd ./simple
exit 1
fi