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 --add-rpath-and-shrink option to do both operations in one go #570

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(import (fetchTarball https://github.com/edolstra/flake-compat/archive/master.tar.gz) {
(import (fetchTarball "https://github.com/edolstra/flake-compat/archive/master.tar.gz") {
src = ./.;
}).defaultNix
23 changes: 22 additions & 1 deletion src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1477,8 +1477,13 @@ std::string ElfFile<ElfFileParamNames>::shrinkRPath(char* rpath, std::vector<std
std::vector<bool> neededLibFound(neededLibs.size(), false);

std::string newRPath = "";
std::set<std::string> componentsConsidered;

for (auto & dirName : splitColonDelimitedString(rpath)) {
if (componentsConsidered.find(dirName) != componentsConsidered.end()) {
continue;
}
componentsConsidered.insert(dirName);

/* Non-absolute entries are allowed (e.g., the special
"$ORIGIN" hack). */
Expand Down Expand Up @@ -1617,6 +1622,14 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
break;
}
case rpSet: { break; } /* new rpath was provied as input to this function */
case rpShrinkAndAdd: {
auto temp = std::string(rpath ? rpath : "");
appendRPath(temp, newRPath);
newRPath = temp;
char * newRPathCStr = const_cast<char *>(newRPath.c_str());
newRPath = shrinkRPath(newRPathCStr, neededLibs, allowedRpathPrefixes);
break;
}
}

if (!forceRPath && dynRPath && !dynRunPath) { /* convert DT_RPATH to DT_RUNPATH */
Expand Down Expand Up @@ -2419,7 +2432,9 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
else if (setExecstack)
elfFile.modifyExecstack(ElfFile::ExecstackMode::set);

if (shrinkRPath)
if (shrinkRPath && addRPath)
elfFile.modifyRPath(elfFile.rpShrinkAndAdd, allowedRpathPrefixes, newRPath);
else if (shrinkRPath)
elfFile.modifyRPath(elfFile.rpShrink, allowedRpathPrefixes, "");
else if (removeRPath)
elfFile.modifyRPath(elfFile.rpRemove, {}, "");
Expand Down Expand Up @@ -2567,6 +2582,12 @@ static int mainWrapped(int argc, char * * argv)
if (++i == argc) error("missing argument");
allowedRpathPrefixes = splitColonDelimitedString(argv[i]);
}
else if (arg == "--add-rpath-and-shrink") {
if (++i == argc) error("missing argument");
addRPath = true;
newRPath = resolveArgument(argv[i]);
shrinkRPath = true;
}
else if (arg == "--set-rpath") {
if (++i == argc) error("missing argument");
setRPath = 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, rpShrink, rpSet, rpAdd, rpRemove, rpShrinkAndAdd } 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
Loading