Merge pull request #316 from andrewla/external-strings

Allow string arguments to be read from files.
This commit is contained in:
Jörg Thalheim 2021-09-11 06:37:54 +01:00 committed by GitHub
commit 88177c44f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 10 deletions

View File

@ -23,6 +23,9 @@ of executables and change the RPATH of executables and libraries.
The single option given operates on each FILE, editing in place.
Any option taking a string argument can also take a file by prefixing the
argument with the @ symbol. See EXAMPLES
.IP "--page-size SIZE"
Uses the given page size instead of the default.
@ -72,11 +75,11 @@ DT_RUNPATH. By default DT_RPATH is converted to DT_RUNPATH.
.IP "--add-needed LIBRARY"
Adds a declared dependency on a dynamic library (DT_NEEDED).
This option can be give multiple times.
This option can be given multiple times.
.IP "--replace-needed LIB_ORIG LIB_NEW"
Replaces a declared dependency on a dynamic library with another one (DT_NEEDED).
This option can be give multiple times.
This option can be given multiple times.
.IP "--remove-needed LIBRARY"
Removes a declared dependency on LIBRARY (DT_NEEDED entry). This
@ -95,6 +98,23 @@ Prints details of the changes made to the input file.
.IP --version
Shows the version of patchelf.
.SH EXAMPLES
To use the contents on an external file as a parameter:
.RS
$ patchelf a.out --add-rpath @/tmp/generated-rpath.bin
.RE
To change the RPATH of a binary. Note that
.BR $ORIGIN
is a special symbol used by the loader, so must be quoted.
.RS
patchelf --set-rpath '$ORIGIN/../lib64' a.out
.RE
.SH AUTHOR
Eelco Dolstra <e.dolstra@tudelft.nl>

View File

@ -1882,6 +1882,15 @@ static void patchElf()
}
}
std::string resolveArgument(const char *arg) {
if (strlen(arg) > 0 && arg[0] == '@') {
FileContents cnts = readFile(arg + 1);
return std::string((char *)cnts->data(), cnts->size());
}
return std::string(arg);
}
void showHelp(const std::string & progName)
{
@ -1926,7 +1935,7 @@ int mainWrapped(int argc, char * * argv)
std::string arg(argv[i]);
if (arg == "--set-interpreter" || arg == "--interpreter") {
if (++i == argc) error("missing argument");
newInterpreter = argv[i];
newInterpreter = resolveArgument(argv[i]);
}
else if (arg == "--page-size") {
if (++i == argc) error("missing argument");
@ -1942,7 +1951,7 @@ int mainWrapped(int argc, char * * argv)
else if (arg == "--set-soname") {
if (++i == argc) error("missing argument");
setSoname = true;
newSoname = argv[i];
newSoname = resolveArgument(argv[i]);
}
else if (arg == "--remove-rpath") {
removeRPath = true;
@ -1957,12 +1966,12 @@ int mainWrapped(int argc, char * * argv)
else if (arg == "--set-rpath") {
if (++i == argc) error("missing argument");
setRPath = true;
newRPath = argv[i];
newRPath = resolveArgument(argv[i]);
}
else if (arg == "--add-rpath") {
if (++i == argc) error("missing argument");
addRPath = true;
newRPath = argv[i];
newRPath = resolveArgument(argv[i]);
}
else if (arg == "--print-rpath") {
printRPath = true;
@ -1986,11 +1995,11 @@ int mainWrapped(int argc, char * * argv)
}
else if (arg == "--add-needed") {
if (++i == argc) error("missing argument");
neededLibsToAdd.insert(argv[i]);
neededLibsToAdd.insert(resolveArgument(argv[i]));
}
else if (arg == "--remove-needed") {
if (++i == argc) error("missing argument");
neededLibsToRemove.insert(argv[i]);
neededLibsToRemove.insert(resolveArgument(argv[i]));
}
else if (arg == "--replace-needed") {
if (i+2 >= argc) error("missing argument(s)");
@ -1999,11 +2008,11 @@ int mainWrapped(int argc, char * * argv)
}
else if (arg == "--clear-symbol-version") {
if (++i == argc) error("missing argument");
symbolsToClearVersion.insert(argv[i]);
symbolsToClearVersion.insert(resolveArgument(argv[i]));
}
else if (arg == "--output") {
if (++i == argc) error("missing argument");
outputFileName = argv[i];
outputFileName = resolveArgument(argv[i]);
alwaysWrite = true;
}
else if (arg == "--debug") {

View File

@ -36,6 +36,7 @@ src_TESTS = \
contiguous-note-sections.sh \
no-gnu-hash.sh \
no-dynamic-section.sh \
args-from-file.sh \
basic-flags.sh
build_TESTS = \

16
tests/args-from-file.sh Executable file
View File

@ -0,0 +1,16 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
cp main ${SCRATCH}/
RANDOM_PATH=$(pwd)/${SCRATCH}/$RANDOM
echo -n ${RANDOM_PATH} >> ${SCRATCH}/add-rpath
! ../src/patchelf --print-rpath ${SCRATCH}/main | grep $RANDOM_PATH
../src/patchelf --add-rpath @${SCRATCH}/add-rpath ${SCRATCH}/main
../src/patchelf --print-rpath ${SCRATCH}/main | grep $RANDOM_PATH
# should print error message and fail
../src/patchelf --set-rpath @${SCRATCH}/does-not-exist ${SCRATCH}/main 2>&1 | grep "getting info about"