summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Joseph <adam@westernsemico.com>2022-06-18 18:34:32 -0700
committerAdam Joseph <adam@westernsemico.com>2022-06-18 18:39:35 -0700
commit9995113a40f613b335b2ddf29d9fb5fd9d8c07d5 (patch)
tree545843c109f3a794c060c58631cec69af19f7448
parentbe0cc30a59b2755844bcd48823f6fbc8d97b93a7 (diff)
downloadpatchelf-9995113a40f613b335b2ddf29d9fb5fd9d8c07d5.tar.gz
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.
-rw-r--r--src/patchelf.cc22
1 files 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<ElfFileParamNames>::rewriteSectionsLibrary()
rewriteHeaders(firstPage + rdi(hdr()->e_phoff));
}
+static bool noSort = false;
template<ElfFileParams>
void ElfFile<ElfFileParamNames>::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<ElfFileParamNames>::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<ElfFileParamNames>::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]));