Merge pull request #378 from a-m-joseph/pr/no-sort

add --no-sort option
This commit is contained in:
Jörg Thalheim
2022-06-19 06:54:15 +01:00
committed by GitHub
2 changed files with 21 additions and 6 deletions

View File

@@ -92,6 +92,11 @@ Prints all DT_NEEDED entries of the executable.
Marks the object so that the search for dependencies of this object will ignore any
default library search paths.
.IP "--no-sort"
Do not sort program headers or section headers. This is useful when
debugging patchelf, because it makes it easier to read diffs of the
output of "readelf -a".
.IP "--add-debug-tag"
Adds DT_DEBUG tag to the .dynamic section if not yet present in an ELF
object. A shared library (-shared) by default does not receive DT_DEBUG tag.

View File

@@ -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);
@@ -1843,6 +1849,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\
@@ -1925,6 +1932,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]));