@@ -735,14 +735,16 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
735
735
rewriteHeaders (firstPage + rdi (hdr ()->e_phoff ));
736
736
}
737
737
738
+ static bool noSort = false ;
738
739
739
740
template <ElfFileParams>
740
741
void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
741
742
{
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
+ }
746
748
747
749
/* What is the index of the last replaced section? */
748
750
unsigned int lastReplaced = 0 ;
@@ -955,7 +957,9 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
955
957
}
956
958
}
957
959
958
- sortPhdrs ();
960
+ if (!noSort) {
961
+ sortPhdrs ();
962
+ }
959
963
960
964
for (unsigned int i = 0 ; i < phdrs.size (); ++i)
961
965
* ((Elf_Phdr *) (fileContents->data () + rdi (hdr ()->e_phoff )) + i) = phdrs.at (i);
@@ -964,7 +968,9 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
964
968
/* Rewrite the section header table. For neatness, keep the
965
969
sections sorted. */
966
970
assert (rdi (hdr ()->e_shnum ) == shdrs.size ());
967
- sortShdrs ();
971
+ if (!noSort) {
972
+ sortShdrs ();
973
+ }
968
974
for (unsigned int i = 1 ; i < rdi (hdr ()->e_shnum ); ++i)
969
975
* ((Elf_Shdr *) (fileContents->data () + rdi (hdr ()->e_shoff )) + i) = shdrs.at (i);
970
976
@@ -1843,6 +1849,7 @@ void showHelp(const std::string & progName)
1843
1849
[--replace-needed LIBRARY NEW_LIBRARY]\n \
1844
1850
[--print-needed]\n \
1845
1851
[--no-default-lib]\n \
1852
+ [--no-sort]\t\t Do not sort program+section headers; useful for debugging patchelf.\n \
1846
1853
[--clear-symbol-version SYMBOL]\n \
1847
1854
[--add-debug-tag]\n \
1848
1855
[--output FILE]\n \
@@ -1925,6 +1932,9 @@ int mainWrapped(int argc, char * * argv)
1925
1932
else if (arg == " --print-needed" ) {
1926
1933
printNeeded = true ;
1927
1934
}
1935
+ else if (arg == " --no-sort" ) {
1936
+ noSort = true ;
1937
+ }
1928
1938
else if (arg == " --add-needed" ) {
1929
1939
if (++i == argc) error (" missing argument" );
1930
1940
neededLibsToAdd.insert (resolveArgument (argv[i]));
0 commit comments