From 9995113a40f613b335b2ddf29d9fb5fd9d8c07d5 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Sat, 18 Jun 2022 18:34:32 -0700 Subject: 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. --- src/patchelf.cc | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/patchelf.cc b/src/patchelf.cc index 6882b28..91c6e1b 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -735,14 +735,16 @@ void ElfFile::rewriteSectionsLibrary() rewriteHeaders(firstPage + rdi(hdr()->e_phoff)); } +static bool noSort = false; template void ElfFile::rewriteSectionsExecutable() { - /* Sort the sections by offset, otherwise we won't correctly find - all the sections before the last replaced section. */ - sortShdrs(); - + if (!noSort) { + /* Sort the sections by offset, otherwise we won't correctly find + all the sections before the last replaced section. */ + sortShdrs(); + } /* What is the index of the last replaced section? */ unsigned int lastReplaced = 0; @@ -955,7 +957,9 @@ void ElfFile::rewriteHeaders(Elf_Addr phdrAddress) } } - sortPhdrs(); + if (!noSort) { + sortPhdrs(); + } for (unsigned int i = 0; i < phdrs.size(); ++i) * ((Elf_Phdr *) (fileContents->data() + rdi(hdr()->e_phoff)) + i) = phdrs.at(i); @@ -964,7 +968,9 @@ void ElfFile::rewriteHeaders(Elf_Addr phdrAddress) /* Rewrite the section header table. For neatness, keep the sections sorted. */ assert(rdi(hdr()->e_shnum) == shdrs.size()); - sortShdrs(); + if (!noSort) { + sortShdrs(); + } for (unsigned int i = 1; i < rdi(hdr()->e_shnum); ++i) * ((Elf_Shdr *) (fileContents->data() + rdi(hdr()->e_shoff)) + i) = shdrs.at(i); @@ -1833,6 +1839,7 @@ void showHelp(const std::string & progName) [--replace-needed LIBRARY NEW_LIBRARY]\n\ [--print-needed]\n\ [--no-default-lib]\n\ + [--no-sort]\t\tDo not sort program+section headers; useful for debugging patchelf.\n\ [--clear-symbol-version SYMBOL]\n\ [--add-debug-tag]\n\ [--output FILE]\n\ @@ -1915,6 +1922,9 @@ int mainWrapped(int argc, char * * argv) else if (arg == "--print-needed") { printNeeded = true; } + else if (arg == "--no-sort") { + noSort = true; + } else if (arg == "--add-needed") { if (++i == argc) error("missing argument"); neededLibsToAdd.insert(resolveArgument(argv[i])); -- cgit v1.2.1