@@ -735,14 +735,16 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
735735 rewriteHeaders (firstPage + rdi (hdr ()->e_phoff ));
736736}
737737
738+ static bool noSort = false ;
738739
739740template <ElfFileParams>
740741void 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
@@ -1843,6 +1849,7 @@ void showHelp(const std::string & progName)
18431849 [--replace-needed LIBRARY NEW_LIBRARY]\n \
18441850 [--print-needed]\n \
18451851 [--no-default-lib]\n \
1852+ [--no-sort]\t\t Do not sort program+section headers; useful for debugging patchelf.\n \
18461853 [--clear-symbol-version SYMBOL]\n \
18471854 [--add-debug-tag]\n \
18481855 [--output FILE]\n \
@@ -1925,6 +1932,9 @@ int mainWrapped(int argc, char * * argv)
19251932 else if (arg == " --print-needed" ) {
19261933 printNeeded = true ;
19271934 }
1935+ else if (arg == " --no-sort" ) {
1936+ noSort = true ;
1937+ }
19281938 else if (arg == " --add-needed" ) {
19291939 if (++i == argc) error (" missing argument" );
19301940 neededLibsToAdd.insert (resolveArgument (argv[i]));
0 commit comments