Skip to content

Commit

Permalink
Merge pull request #361 from fzakaria/faridzakaria/fix-add-replace-fo…
Browse files Browse the repository at this point in the history
…r-real

Allow multiple modifications in same call
  • Loading branch information
Mic92 committed Dec 22, 2021
2 parents c172ce6 + b92a6e8 commit 3d0a58b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ Makefile
.direnv/
.vscode/
.idea/
result
13 changes: 12 additions & 1 deletion src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
}

changed = true;
this->rewriteSections();
}

template<ElfFileParams>
Expand All @@ -1158,6 +1159,7 @@ void ElfFile<ElfFileParamNames>::setInterpreter(const std::string & newInterpret
std::string & section = replaceSection(".interp", newInterpreter.size() + 1);
setSubstr(section, 0, newInterpreter + '\0');
changed = true;
this->rewriteSections();
}


Expand Down Expand Up @@ -1234,6 +1236,7 @@ void ElfFile<ElfFileParamNames>::removeRPath(Elf_Shdr & shdrDynamic) {
}
}
memset(last, 0, sizeof(Elf_Dyn) * (dyn - last));
this->rewriteSections();
}

template<ElfFileParams>
Expand Down Expand Up @@ -1380,6 +1383,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
newDyn.d_un.d_val = shdrDynStr.sh_size;
setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn)));
}
this->rewriteSections();
}


Expand Down Expand Up @@ -1409,6 +1413,8 @@ void ElfFile<ElfFileParamNames>::removeNeeded(const std::set<std::string> & libs
}

memset(last, 0, sizeof(Elf_Dyn) * (dyn - last));

this->rewriteSections();
}

template<ElfFileParams>
Expand Down Expand Up @@ -1532,6 +1538,8 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
--verNeedNum;
}
}

this->rewriteSections();
}

template<ElfFileParams>
Expand Down Expand Up @@ -1582,6 +1590,8 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
}

changed = true;

this->rewriteSections();
}

template<ElfFileParams>
Expand Down Expand Up @@ -1638,6 +1648,7 @@ void ElfFile<ElfFileParamNames>::noDefaultLib()
setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn)));
}

this->rewriteSections();
changed = true;
}

Expand Down Expand Up @@ -1667,6 +1678,7 @@ void ElfFile<ElfFileParamNames>::clearSymbolVersions(const std::set<std::string>
}
}
changed = true;
this->rewriteSections();
}

static bool printInterpreter = false;
Expand Down Expand Up @@ -1726,7 +1738,6 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
elfFile.noDefaultLib();

if (elfFile.isChanged()){
elfFile.rewriteSections();
writeFile(fileName, elfFile.fileContents);
} else if (alwaysWrite) {
debug("not modified, but alwaysWrite=true\n");
Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ src_TESTS = \
basic-flags.sh \
set-empty-rpath.sh \
phdr-corruption.sh \
replace-needed.sh
replace-needed.sh \
replace-add-needed.sh

build_TESTS = \
$(no_rpath_arch_TESTS)
Expand Down
38 changes: 38 additions & 0 deletions tests/replace-add-needed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)
PATCHELF=$(readlink -f "../src/patchelf")

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}

cp simple ${SCRATCH}/
cp libfoo.so ${SCRATCH}/
cp libbar.so ${SCRATCH}/

cd ${SCRATCH}

libcldd=$(ldd ./simple | awk '/ => / { print $3 }' | grep -E "(libc.so|ld-musl)")

# We have to set the soname on these libraries
${PATCHELF} --set-soname libbar.so ./libbar.so

# Add a libbar.so so we can rewrite it later
${PATCHELF} --add-needed libbar.so ./simple

# Make the NEEDED in libfoo the same as simple
# This is a current "bug" in musl
# https://www.openwall.com/lists/musl/2021/12/21/1
${PATCHELF} --replace-needed libbar.so $(readlink -f ./libbar.so) ./libfoo.so

${PATCHELF} --replace-needed libc.so.6 ${libcldd} \
--replace-needed libbar.so $(readlink -f ./libbar.so) \
--add-needed $(readlink -f ./libfoo.so) \
./simple

exitCode=0
./simple || exitCode=$?

if test "$exitCode" != 0; then
ldd ./simple
exit 1
fi

0 comments on commit 3d0a58b

Please sign in to comment.