Skip to content

Commit dab4411

Browse files
authored
Merge pull request #246 from xavierabellan/add-rpath
Added option --add-rpath
2 parents 4ee62cb + 8703e46 commit dab4411

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class ElfFile
197197

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

200-
typedef enum { rpPrint, rpShrink, rpSet, rpRemove } RPathOp;
200+
typedef enum { rpPrint, rpShrink, rpSet, rpAdd, rpRemove } RPathOp;
201201

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

@@ -1408,6 +1408,10 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
14081408
return;
14091409
}
14101410

1411+
if (op == rpAdd) {
1412+
newRPath = std::string(rpath ? rpath : "") + ":" + newRPath;
1413+
}
1414+
14111415
changed = true;
14121416

14131417
/* Zero out the previous rpath to prevent retained dependencies in
@@ -1730,6 +1734,7 @@ static bool shrinkRPath = false;
17301734
static std::vector<std::string> allowedRpathPrefixes;
17311735
static bool removeRPath = false;
17321736
static bool setRPath = false;
1737+
static bool addRPath = false;
17331738
static bool printRPath = false;
17341739
static std::string newRPath;
17351740
static std::set<std::string> neededLibsToRemove;
@@ -1763,6 +1768,8 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
17631768
elfFile.modifyRPath(elfFile.rpRemove, {}, "");
17641769
else if (setRPath)
17651770
elfFile.modifyRPath(elfFile.rpSet, {}, newRPath);
1771+
else if (addRPath)
1772+
elfFile.modifyRPath(elfFile.rpAdd, {}, newRPath);
17661773

17671774
if (printNeeded) elfFile.printNeededLibs();
17681775

@@ -1810,6 +1817,7 @@ void showHelp(const std::string & progName)
18101817
[--print-soname]\t\tPrints 'DT_SONAME' entry of .dynamic section. Raises an error if DT_SONAME doesn't exist\n\
18111818
[--set-soname SONAME]\t\tSets 'DT_SONAME' entry to SONAME.\n\
18121819
[--set-rpath RPATH]\n\
1820+
[--add-rpath RPATH]\n\
18131821
[--remove-rpath]\n\
18141822
[--shrink-rpath]\n\
18151823
[--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)
18761884
setRPath = true;
18771885
newRPath = argv[i];
18781886
}
1887+
else if (arg == "--add-rpath") {
1888+
if (++i == argc) error("missing argument");
1889+
addRPath = true;
1890+
newRPath = argv[i];
1891+
}
18791892
else if (arg == "--print-rpath") {
18801893
printRPath = true;
18811894
}
@@ -1942,6 +1955,9 @@ int mainWrapped(int argc, char * * argv)
19421955
if (!outputFileName.empty() && fileNames.size() != 1)
19431956
error("--output option only allowed with single input file");
19441957

1958+
if (setRPath && addRPath)
1959+
error("--set-rpath option not allowed with --add-rpath");
1960+
19451961
patchElf();
19461962

19471963
return 0;

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
16+
if test "$(uname)" = FreeBSD; then
17+
export LD_LIBRARY_PATH=$(pwd)/${SCRATCH}/libsB
18+
fi
19+
20+
exitCode=0
21+
(cd ${SCRATCH} && ./main) || exitCode=$?
22+
23+
if test "$exitCode" != 46; then
24+
echo "bad exit code!"
25+
exit 1
26+
fi

0 commit comments

Comments
 (0)