Skip to content

Commit a8dcbc9

Browse files
authored
Merge pull request #378 from a-m-joseph/pr/no-sort
add --no-sort option
2 parents 1eeac38 + cad0212 commit a8dcbc9

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

patchelf.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ Prints all DT_NEEDED entries of the executable.
9292
Marks the object so that the search for dependencies of this object will ignore any
9393
default library search paths.
9494

95+
.IP "--no-sort"
96+
Do not sort program headers or section headers. This is useful when
97+
debugging patchelf, because it makes it easier to read diffs of the
98+
output of "readelf -a".
99+
95100
.IP "--add-debug-tag"
96101
Adds DT_DEBUG tag to the .dynamic section if not yet present in an ELF
97102
object. A shared library (-shared) by default does not receive DT_DEBUG tag.

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

@@ -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\tDo 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

Comments
 (0)