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

add --update-rpath to change between RPATH and RUNPATH #487

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions patchelf.1
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ the "/foo/lib" reference instead of the "/tmp" entry.
.IP --print-rpath
Prints the DT_RUNPATH or DT_RPATH for an executable or library.

.IP --update-rpath
Updates the DR_RPATH to a DT_RUNTPATH or vice versa if "--force-rpath" is given
Copy link
Member

Choose a reason for hiding this comment

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

Can you elaborate on when you want to set the one over the other?

Copy link
Author

Choose a reason for hiding this comment

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

They have slightly different semantics as detailed in man ld-linux. Would saying that + linking to the other manpage be ok? Or should I add a small summary?

Copy link
Member

@Mic92 Mic92 Apr 28, 2023

Choose a reason for hiding this comment

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

I think this get a different name. It's already confusing that we say --print-rpath and it's actually printing the DT_RUNPATH. "Update" might be also the wrong verb as it convert from DR_RPATH to a DT_RUNPATH, whereas update clashes already with --set-rpath

too.

.IP --force-rpath
Forces the use of the obsolete DT_RPATH in the file instead of
DT_RUNPATH. By default DT_RPATH is converted to DT_RUNPATH.
Expand Down
12 changes: 12 additions & 0 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
newRPath = temp;
break;
}
case rpUpdate: { break; } /* just update the RPATH to RUNPATH or vice versa */
case rpSet: { break; } /* new rpath was provied as input to this function */
}

Expand All @@ -1616,6 +1617,10 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
changed = true;
}

if (op == rpUpdate) {
return;
}

if (rpath && rpath == newRPath) {
return;
}
Expand Down Expand Up @@ -2306,6 +2311,7 @@ static std::vector<std::string> allowedRpathPrefixes;
static bool removeRPath = false;
static bool setRPath = false;
static bool addRPath = false;
static bool updateRPath = false;
static bool addDebugTag = false;
static bool renameDynamicSymbols = false;
static bool printRPath = false;
Expand Down Expand Up @@ -2361,6 +2367,8 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
elfFile.modifyRPath(elfFile.rpSet, {}, newRPath);
else if (addRPath)
elfFile.modifyRPath(elfFile.rpAdd, {}, newRPath);
else if (updateRPath)
elfFile.modifyRPath(elfFile.rpUpdate, {}, "");

if (printNeeded) elfFile.printNeededLibs();

Expand Down Expand Up @@ -2430,6 +2438,7 @@ static void showHelp(const std::string & progName)
[--allowed-rpath-prefixes PREFIXES]\t\tWith '--shrink-rpath', reject rpath entries not starting with the allowed prefix\n\
[--print-rpath]\n\
[--force-rpath]\n\
[--update-rpath]\n\
[--add-needed LIBRARY]\n\
[--remove-needed LIBRARY]\n\
[--replace-needed LIBRARY NEW_LIBRARY]\n\
Expand Down Expand Up @@ -2493,6 +2502,9 @@ static int mainWrapped(int argc, char * * argv)
else if (arg == "--remove-rpath") {
removeRPath = true;
}
else if (arg == "--update-rpath") {
updateRPath = true;
}
else if (arg == "--shrink-rpath") {
shrinkRPath = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/patchelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class ElfFile

void setInterpreter(const std::string & newInterpreter);

typedef enum { rpPrint, rpShrink, rpSet, rpAdd, rpRemove } RPathOp;
typedef enum { rpPrint, rpUpdate, rpShrink, rpSet, rpAdd, rpRemove } RPathOp;

void modifyRPath(RPathOp op, const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath);
std::string shrinkRPath(char* rpath, std::vector<std::string> &neededLibs, const std::vector<std::string> & allowedRpathPrefixes);
Expand Down
36 changes: 36 additions & 0 deletions tests/update-rpath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename "$0" .sh)
OBJDUMP=${OBJDUMP:-objdump}

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

SCRATCHFILE=${SCRATCH}/libfoo.so
cp libfoo.so "$SCRATCHFILE"

doit() {
set +x
../src/patchelf "$@" "$SCRATCHFILE"
set -x
}

expect() {
out=$("$OBJDUMP" -x "$SCRATCHFILE" | grep PATH || true)

for i in $out; do
if [ "$i" != "$1" ]; then
echo "Expected '$*' but got '$out'"
exit 1
fi
shift
done
}

doit --remove-rpath
expect ""
doit --set-rpath foo
expect RUNPATH foo
doit --update-rpath --force-rpath
expect RPATH foo
doit --update-rpath
expect RUNPATH foo