Skip to content

Commit db3845a

Browse files
committed
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
1 parent dc375bc commit db3845a

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/patchelf.cc

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
13111311
}
13121312

13131313
changed = true;
1314+
this->rewriteSections();
13141315
}
13151316

13161317
template<ElfFileParams>
@@ -1319,6 +1320,7 @@ void ElfFile<ElfFileParamNames>::setInterpreter(const std::string & newInterpret
13191320
std::string & section = replaceSection(".interp", newInterpreter.size() + 1);
13201321
setSubstr(section, 0, newInterpreter + '\0');
13211322
changed = true;
1323+
this->rewriteSections();
13221324
}
13231325

13241326

@@ -1395,6 +1397,7 @@ void ElfFile<ElfFileParamNames>::removeRPath(Elf_Shdr & shdrDynamic) {
13951397
}
13961398
}
13971399
memset(last, 0, sizeof(Elf_Dyn) * (dyn - last));
1400+
this->rewriteSections();
13981401
}
13991402

14001403
template<ElfFileParams>
@@ -1541,6 +1544,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
15411544
newDyn.d_un.d_val = shdrDynStr.sh_size;
15421545
setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn)));
15431546
}
1547+
this->rewriteSections();
15441548
}
15451549

15461550

@@ -1570,6 +1574,8 @@ void ElfFile<ElfFileParamNames>::removeNeeded(const std::set<std::string> & libs
15701574
}
15711575

15721576
memset(last, 0, sizeof(Elf_Dyn) * (dyn - last));
1577+
1578+
this->rewriteSections();
15731579
}
15741580

15751581
template<ElfFileParams>
@@ -1693,6 +1699,8 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
16931699
--verNeedNum;
16941700
}
16951701
}
1702+
1703+
this->rewriteSections();
16961704
}
16971705

16981706
template<ElfFileParams>
@@ -1743,6 +1751,8 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
17431751
}
17441752

17451753
changed = true;
1754+
1755+
this->rewriteSections();
17461756
}
17471757

17481758
template<ElfFileParams>
@@ -1799,6 +1809,7 @@ void ElfFile<ElfFileParamNames>::noDefaultLib()
17991809
setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn)));
18001810
}
18011811

1812+
this->rewriteSections();
18021813
changed = true;
18031814
}
18041815

@@ -1828,6 +1839,7 @@ void ElfFile<ElfFileParamNames>::clearSymbolVersions(const std::set<std::string>
18281839
}
18291840
}
18301841
changed = true;
1842+
this->rewriteSections();
18311843
}
18321844

18331845
static bool printInterpreter = false;
@@ -1878,28 +1890,15 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
18781890

18791891
if (printNeeded) elfFile.printNeededLibs();
18801892

1881-
if (!neededLibsToRemove.empty()) {
1882-
elfFile.removeNeeded(neededLibsToRemove);
1883-
elfFile.rewriteSections();
1884-
}
1885-
if (!neededLibsToReplace.empty()) {
1886-
elfFile.replaceNeeded(neededLibsToReplace);
1887-
elfFile.rewriteSections();
1888-
}
1889-
if (!neededLibsToAdd.empty()) {
1890-
elfFile.addNeeded(neededLibsToAdd);
1891-
elfFile.rewriteSections();
1892-
}
1893-
if (!symbolsToClearVersion.empty()) {
1894-
elfFile.clearSymbolVersions(symbolsToClearVersion);
1895-
elfFile.rewriteSections();
1896-
}
1893+
elfFile.removeNeeded(neededLibsToRemove);
1894+
elfFile.replaceNeeded(neededLibsToReplace);
1895+
elfFile.addNeeded(neededLibsToAdd);
1896+
elfFile.clearSymbolVersions(symbolsToClearVersion);
18971897

18981898
if (noDefaultLib)
18991899
elfFile.noDefaultLib();
19001900

19011901
if (elfFile.isChanged()){
1902-
elfFile.rewriteSections();
19031902
writeFile(fileName, elfFile.fileContents);
19041903
} else if (alwaysWrite) {
19051904
debug("not modified, but alwaysWrite=true\n");

tests/replace-add-needed.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ cp libbar.so ${SCRATCH}/
1212

1313
cd ${SCRATCH}
1414

15-
libcldd=$(ldd ./simple | grep -oP "(?<=libc.so.6 => )[^ ]+")
15+
# This grep is necessary to catch either glibc or libc.so for musl
16+
libcldd=$(ldd ./simple | awk '/ => / { print $3 }' | grep libc.so)
1617

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

0 commit comments

Comments
 (0)