Merge pull request #246 from xavierabellan/add-rpath

Added option --add-rpath
This commit is contained in:
Domen Kožar 2021-08-05 12:10:59 +02:00 committed by GitHub
commit dab44118d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 2 deletions

View File

@ -43,6 +43,9 @@ Sets DT_SONAME entry of a library to SONAME.
.IP "--set-rpath RPATH"
Change the RPATH of the executable or library to RPATH.
.IP "--add-rpath RPATH"
Add RPATH to the existing RPATH of the executable or library.
.IP --remove-rpath
Removes the DT_RPATH or DT_RUNPATH entry of the executable or library.

View File

@ -197,7 +197,7 @@ public:
void setInterpreter(const std::string & newInterpreter);
typedef enum { rpPrint, rpShrink, rpSet, rpRemove } RPathOp;
typedef enum { rpPrint, rpShrink, rpSet, rpAdd, rpRemove } RPathOp;
void modifyRPath(RPathOp op, const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath);
@ -1408,6 +1408,10 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
return;
}
if (op == rpAdd) {
newRPath = std::string(rpath ? rpath : "") + ":" + newRPath;
}
changed = true;
/* Zero out the previous rpath to prevent retained dependencies in
@ -1730,6 +1734,7 @@ static bool shrinkRPath = false;
static std::vector<std::string> allowedRpathPrefixes;
static bool removeRPath = false;
static bool setRPath = false;
static bool addRPath = false;
static bool printRPath = false;
static std::string newRPath;
static std::set<std::string> neededLibsToRemove;
@ -1763,6 +1768,8 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
elfFile.modifyRPath(elfFile.rpRemove, {}, "");
else if (setRPath)
elfFile.modifyRPath(elfFile.rpSet, {}, newRPath);
else if (addRPath)
elfFile.modifyRPath(elfFile.rpAdd, {}, newRPath);
if (printNeeded) elfFile.printNeededLibs();
@ -1810,6 +1817,7 @@ void showHelp(const std::string & progName)
[--print-soname]\t\tPrints 'DT_SONAME' entry of .dynamic section. Raises an error if DT_SONAME doesn't exist\n\
[--set-soname SONAME]\t\tSets 'DT_SONAME' entry to SONAME.\n\
[--set-rpath RPATH]\n\
[--add-rpath RPATH]\n\
[--remove-rpath]\n\
[--shrink-rpath]\n\
[--allowed-rpath-prefixes PREFIXES]\t\tWith '--shrink-rpath', reject rpath entries not starting with the allowed prefix\n\
@ -1876,6 +1884,11 @@ int mainWrapped(int argc, char * * argv)
setRPath = true;
newRPath = argv[i];
}
else if (arg == "--add-rpath") {
if (++i == argc) error("missing argument");
addRPath = true;
newRPath = argv[i];
}
else if (arg == "--print-rpath") {
printRPath = true;
}
@ -1942,6 +1955,9 @@ int mainWrapped(int argc, char * * argv)
if (!outputFileName.empty() && fileNames.size() != 1)
error("--output option only allowed with single input file");
if (setRPath && addRPath)
error("--set-rpath option not allowed with --add-rpath");
patchElf();
return 0;

View File

@ -20,7 +20,7 @@ no_rpath_arch_TESTS = \
src_TESTS = \
plain-fail.sh plain-run.sh shrink-rpath.sh set-interpreter-short.sh \
set-interpreter-long.sh set-rpath.sh no-rpath.sh big-dynstr.sh \
set-interpreter-long.sh set-rpath.sh add-rpath.sh no-rpath.sh big-dynstr.sh \
set-rpath-library.sh soname.sh shrink-rpath-with-allowed-prefixes.sh \
force-rpath.sh \
plain-needed.sh \

26
tests/add-rpath.sh Executable file
View File

@ -0,0 +1,26 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
mkdir -p ${SCRATCH}/libsA
mkdir -p ${SCRATCH}/libsB
cp main ${SCRATCH}/
cp libfoo.so ${SCRATCH}/libsA/
cp libbar.so ${SCRATCH}/libsB/
../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsA ${SCRATCH}/main
../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsB ${SCRATCH}/main
if test "$(uname)" = FreeBSD; then
export LD_LIBRARY_PATH=$(pwd)/${SCRATCH}/libsB
fi
exitCode=0
(cd ${SCRATCH} && ./main) || exitCode=$?
if test "$exitCode" != 46; then
echo "bad exit code!"
exit 1
fi