Code clarity and test working with musl

Made changes according to feedback from @Mic92 such as moving the
`rewriteSections` inline into every method.

Improved `replace-add-needed.sh` to work with musl libc
This commit is contained in:
Farid Zakaria
2021-12-21 05:51:04 -08:00
parent dc375bc7cd
commit d02a3ecdd7
2 changed files with 17 additions and 18 deletions

View File

@@ -1311,6 +1311,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
} }
changed = true; changed = true;
this->rewriteSections();
} }
template<ElfFileParams> template<ElfFileParams>
@@ -1319,6 +1320,7 @@ void ElfFile<ElfFileParamNames>::setInterpreter(const std::string & newInterpret
std::string & section = replaceSection(".interp", newInterpreter.size() + 1); std::string & section = replaceSection(".interp", newInterpreter.size() + 1);
setSubstr(section, 0, newInterpreter + '\0'); setSubstr(section, 0, newInterpreter + '\0');
changed = true; changed = true;
this->rewriteSections();
} }
@@ -1395,6 +1397,7 @@ void ElfFile<ElfFileParamNames>::removeRPath(Elf_Shdr & shdrDynamic) {
} }
} }
memset(last, 0, sizeof(Elf_Dyn) * (dyn - last)); memset(last, 0, sizeof(Elf_Dyn) * (dyn - last));
this->rewriteSections();
} }
template<ElfFileParams> template<ElfFileParams>
@@ -1541,6 +1544,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
newDyn.d_un.d_val = shdrDynStr.sh_size; newDyn.d_un.d_val = shdrDynStr.sh_size;
setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn))); setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn)));
} }
this->rewriteSections();
} }
@@ -1570,6 +1574,8 @@ void ElfFile<ElfFileParamNames>::removeNeeded(const std::set<std::string> & libs
} }
memset(last, 0, sizeof(Elf_Dyn) * (dyn - last)); memset(last, 0, sizeof(Elf_Dyn) * (dyn - last));
this->rewriteSections();
} }
template<ElfFileParams> template<ElfFileParams>
@@ -1693,6 +1699,8 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
--verNeedNum; --verNeedNum;
} }
} }
this->rewriteSections();
} }
template<ElfFileParams> template<ElfFileParams>
@@ -1743,6 +1751,8 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
} }
changed = true; changed = true;
this->rewriteSections();
} }
template<ElfFileParams> template<ElfFileParams>
@@ -1799,6 +1809,7 @@ void ElfFile<ElfFileParamNames>::noDefaultLib()
setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn))); setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn)));
} }
this->rewriteSections();
changed = true; changed = true;
} }
@@ -1828,6 +1839,7 @@ void ElfFile<ElfFileParamNames>::clearSymbolVersions(const std::set<std::string>
} }
} }
changed = true; changed = true;
this->rewriteSections();
} }
static bool printInterpreter = false; static bool printInterpreter = false;
@@ -1878,28 +1890,15 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
if (printNeeded) elfFile.printNeededLibs(); if (printNeeded) elfFile.printNeededLibs();
if (!neededLibsToRemove.empty()) {
elfFile.removeNeeded(neededLibsToRemove); elfFile.removeNeeded(neededLibsToRemove);
elfFile.rewriteSections();
}
if (!neededLibsToReplace.empty()) {
elfFile.replaceNeeded(neededLibsToReplace); elfFile.replaceNeeded(neededLibsToReplace);
elfFile.rewriteSections();
}
if (!neededLibsToAdd.empty()) {
elfFile.addNeeded(neededLibsToAdd); elfFile.addNeeded(neededLibsToAdd);
elfFile.rewriteSections();
}
if (!symbolsToClearVersion.empty()) {
elfFile.clearSymbolVersions(symbolsToClearVersion); elfFile.clearSymbolVersions(symbolsToClearVersion);
elfFile.rewriteSections();
}
if (noDefaultLib) if (noDefaultLib)
elfFile.noDefaultLib(); elfFile.noDefaultLib();
if (elfFile.isChanged()){ if (elfFile.isChanged()){
elfFile.rewriteSections();
writeFile(fileName, elfFile.fileContents); writeFile(fileName, elfFile.fileContents);
} else if (alwaysWrite) { } else if (alwaysWrite) {
debug("not modified, but alwaysWrite=true\n"); debug("not modified, but alwaysWrite=true\n");

View File

@@ -12,7 +12,7 @@ cp libbar.so ${SCRATCH}/
cd ${SCRATCH} cd ${SCRATCH}
libcldd=$(ldd ./simple | grep -oP "(?<=libc.so.6 => )[^ ]+") libcldd=$(ldd ./simple | awk '/ => / { print $3 }' | grep .so | head -n 1)
# We have to set the soname on these libraries # We have to set the soname on these libraries
${PATCHELF} --set-soname libbar.so ./libbar.so ${PATCHELF} --set-soname libbar.so ./libbar.so