Skip to content

Commit 62e8db4

Browse files
committed
Added option --add-rpath
1 parent 47dc18d commit 62e8db4

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

patchelf.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Sets DT_SONAME entry of a library to SONAME.
4343
.IP "--set-rpath RPATH"
4444
Change the RPATH of the executable or library to RPATH.
4545

46+
.IP "--add-rpath RPATH"
47+
Add RPATH to the existing RPATH of the executable or library.
48+
4649
.IP --remove-rpath
4750
Removes the DT_RPATH or DT_RUNPATH entry of the executable or library.
4851

src/patchelf.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class ElfFile
201201

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

204-
typedef enum { rpPrint, rpShrink, rpSet, rpRemove } RPathOp;
204+
typedef enum { rpPrint, rpShrink, rpSet, rpAdd, rpRemove } RPathOp;
205205

206206
void modifyRPath(RPathOp op, const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath);
207207

@@ -1396,6 +1396,10 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
13961396
return;
13971397
}
13981398

1399+
if (op == rpAdd) {
1400+
newRPath = std::string(rpath ? rpath : "") + ":" + newRPath;
1401+
}
1402+
13991403
changed = true;
14001404

14011405
/* Zero out the previous rpath to prevent retained dependencies in
@@ -1715,6 +1719,7 @@ static bool shrinkRPath = false;
17151719
static std::vector<std::string> allowedRpathPrefixes;
17161720
static bool removeRPath = false;
17171721
static bool setRPath = false;
1722+
static bool addRPath = false;
17181723
static bool printRPath = false;
17191724
static std::string newRPath;
17201725
static std::set<std::string> neededLibsToRemove;
@@ -1748,6 +1753,8 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, std
17481753
elfFile.modifyRPath(elfFile.rpRemove, {}, "");
17491754
else if (setRPath)
17501755
elfFile.modifyRPath(elfFile.rpSet, {}, newRPath);
1756+
else if (addRPath)
1757+
elfFile.modifyRPath(elfFile.rpAdd, {}, newRPath);
17511758

17521759
if (printNeeded) elfFile.printNeededLibs();
17531760

@@ -1795,6 +1802,7 @@ void showHelp(const std::string & progName)
17951802
[--print-soname]\t\tPrints 'DT_SONAME' entry of .dynamic section. Raises an error if DT_SONAME doesn't exist\n\
17961803
[--set-soname SONAME]\t\tSets 'DT_SONAME' entry to SONAME.\n\
17971804
[--set-rpath RPATH]\n\
1805+
[--add-rpath RPATH]\n\
17981806
[--remove-rpath]\n\
17991807
[--shrink-rpath]\n\
18001808
[--allowed-rpath-prefixes PREFIXES]\t\tWith '--shrink-rpath', reject rpath entries not starting with the allowed prefix\n\
@@ -1860,6 +1868,11 @@ int mainWrapped(int argc, char * * argv)
18601868
setRPath = true;
18611869
newRPath = argv[i];
18621870
}
1871+
else if (arg == "--add-rpath") {
1872+
if (++i == argc) error("missing argument");
1873+
addRPath = true;
1874+
newRPath = argv[i];
1875+
}
18631876
else if (arg == "--print-rpath") {
18641877
printRPath = true;
18651878
}

tests/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ no_rpath_arch_TESTS = \
2020

2121
src_TESTS = \
2222
plain-fail.sh plain-run.sh shrink-rpath.sh set-interpreter-short.sh \
23-
set-interpreter-long.sh set-rpath.sh no-rpath.sh big-dynstr.sh \
23+
set-interpreter-long.sh set-rpath.sh add-rpath.sh no-rpath.sh big-dynstr.sh \
2424
set-rpath-library.sh soname.sh shrink-rpath-with-allowed-prefixes.sh \
2525
force-rpath.sh \
2626
plain-needed.sh \

tests/add-rpath.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#! /bin/sh -e
2+
SCRATCH=scratch/$(basename $0 .sh)
3+
4+
rm -rf ${SCRATCH}
5+
mkdir -p ${SCRATCH}
6+
mkdir -p ${SCRATCH}/libsA
7+
mkdir -p ${SCRATCH}/libsB
8+
9+
cp main ${SCRATCH}/
10+
cp libfoo.so ${SCRATCH}/libsA/
11+
cp libbar.so ${SCRATCH}/libsB/
12+
13+
../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsA ${SCRATCH}/main
14+
../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsB ${SCRATCH}/main
15+
#patchelf --add-rpath $(pwd)/${SCRATCH}/libsA ${SCRATCH}/main
16+
#patchelf --add-rpath $(pwd)/${SCRATCH}/libsB ${SCRATCH}/main
17+
18+
if test "$(uname)" = FreeBSD; then
19+
export LD_LIBRARY_PATH=$(pwd)/${SCRATCH}/libsB
20+
fi
21+
22+
exitCode=0
23+
(cd ${SCRATCH} && ./main) || exitCode=$?
24+
25+
if test "$exitCode" != 46; then
26+
echo "bad exit code!"
27+
exit 1
28+
fi

0 commit comments

Comments
 (0)