Skip to content

Commit

Permalink
feat: add --no-clobber-old-sections switch
Browse files Browse the repository at this point in the history
Works around NixOS#520, may be useful for other cursed self-modifying things.
  • Loading branch information
K900 committed Sep 21, 2023
1 parent afd3cc9 commit 6eaa2e9
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
static bool debugMode = false;

static bool forceRPath = false;
static bool clobberOldSections = true;

static std::vector<std::string> fileNames;
static std::string outputFileName;
Expand Down Expand Up @@ -664,14 +665,16 @@ template<ElfFileParams>
void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
Elf_Addr startAddr, Elf_Off startOffset)
{
/* Overwrite the old section contents with 'Z's. Do this
*before* writing the new section contents (below) to prevent
clobbering previously written new section contents. */
for (auto & i : replacedSections) {
const std::string & sectionName = i.first;
const Elf_Shdr & shdr = findSectionHeader(sectionName);
if (rdi(shdr.sh_type) != SHT_NOBITS)
memset(fileContents->data() + rdi(shdr.sh_offset), 'Z', rdi(shdr.sh_size));
if (clobberOldSections) {
/* Overwrite the old section contents with 'Z's. Do this
*before* writing the new section contents (below) to prevent
clobbering previously written new section contents. */
for (auto & i : replacedSections) {
const std::string & sectionName = i.first;
const Elf_Shdr & shdr = findSectionHeader(sectionName);
if (rdi(shdr.sh_type) != SHT_NOBITS)
memset(fileContents->data() + rdi(shdr.sh_offset), 'Z', rdi(shdr.sh_size));
}
}

std::set<unsigned int> noted_phdrs = {};
Expand Down Expand Up @@ -2505,6 +2508,7 @@ static void showHelp(const std::string & progName)
[--clear-execstack]\n\
[--set-execstack]\n\
[--rename-dynamic-symbols NAME_MAP_FILE]\tRenames dynamic symbols. The map file should contain two symbols (old_name new_name) per line\n\
[--no-clobber-old-sections]\t\tDo not clobber old section values - only use when executable does self-referential tricks.\n\
[--output FILE]\n\
[--debug]\n\
[--version]\n\
Expand Down Expand Up @@ -2661,6 +2665,9 @@ static int mainWrapped(int argc, char * * argv)
symbolsToRename[*symbolsToRenameKeys.insert(from).first] = to;
}
}
else if (arg == "--no-clobber-old-sections") {
clobberOldSections = false;
}
else if (arg == "--help" || arg == "-h" ) {
showHelp(argv[0]);
return 0;
Expand Down

0 comments on commit 6eaa2e9

Please sign in to comment.