Skip to content

Commit 9995113

Browse files
author
Adam Joseph
committed
add --no-sort option
This PR adds a command line option `--no-sort` which causes patchelf to refrain from sorting the program headers and section headers. A comment in the preexisting source code says that this was done only "for neatness". The `--no-sort` option, combined with `readelf -a` and `colordiff` is very useful for debugging patchelf problems. Without `--no-sort` the diffs are not usable -- everything changes because of the sorting.
1 parent be0cc30 commit 9995113

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/patchelf.cc

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,16 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
735735
rewriteHeaders(firstPage + rdi(hdr()->e_phoff));
736736
}
737737

738+
static bool noSort = false;
738739

739740
template<ElfFileParams>
740741
void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
741742
{
742-
/* Sort the sections by offset, otherwise we won't correctly find
743-
all the sections before the last replaced section. */
744-
sortShdrs();
745-
743+
if (!noSort) {
744+
/* Sort the sections by offset, otherwise we won't correctly find
745+
all the sections before the last replaced section. */
746+
sortShdrs();
747+
}
746748

747749
/* What is the index of the last replaced section? */
748750
unsigned int lastReplaced = 0;
@@ -955,7 +957,9 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
955957
}
956958
}
957959

958-
sortPhdrs();
960+
if (!noSort) {
961+
sortPhdrs();
962+
}
959963

960964
for (unsigned int i = 0; i < phdrs.size(); ++i)
961965
* ((Elf_Phdr *) (fileContents->data() + rdi(hdr()->e_phoff)) + i) = phdrs.at(i);
@@ -964,7 +968,9 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
964968
/* Rewrite the section header table. For neatness, keep the
965969
sections sorted. */
966970
assert(rdi(hdr()->e_shnum) == shdrs.size());
967-
sortShdrs();
971+
if (!noSort) {
972+
sortShdrs();
973+
}
968974
for (unsigned int i = 1; i < rdi(hdr()->e_shnum); ++i)
969975
* ((Elf_Shdr *) (fileContents->data() + rdi(hdr()->e_shoff)) + i) = shdrs.at(i);
970976

@@ -1833,6 +1839,7 @@ void showHelp(const std::string & progName)
18331839
[--replace-needed LIBRARY NEW_LIBRARY]\n\
18341840
[--print-needed]\n\
18351841
[--no-default-lib]\n\
1842+
[--no-sort]\t\tDo not sort program+section headers; useful for debugging patchelf.\n\
18361843
[--clear-symbol-version SYMBOL]\n\
18371844
[--add-debug-tag]\n\
18381845
[--output FILE]\n\
@@ -1915,6 +1922,9 @@ int mainWrapped(int argc, char * * argv)
19151922
else if (arg == "--print-needed") {
19161923
printNeeded = true;
19171924
}
1925+
else if (arg == "--no-sort") {
1926+
noSort = true;
1927+
}
19181928
else if (arg == "--add-needed") {
19191929
if (++i == argc) error("missing argument");
19201930
neededLibsToAdd.insert(resolveArgument(argv[i]));

0 commit comments

Comments
 (0)