diff --git a/src/patchelf.cc b/src/patchelf.cc index b2552f7..f129db8 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -1652,6 +1652,38 @@ void ElfFile::noDefaultLib() changed = true; } +template +void ElfFile::addDebug() +{ + auto shdrDynamic = findSectionHeader(".dynamic"); + + auto dyn = (Elf_Dyn *)(fileContents->data() + rdi(shdrDynamic.sh_offset)); + for ( ; rdi(dyn->d_tag) != DT_NULL; dyn++) { + if (rdi(dyn->d_tag) == DT_DEBUG) { + return; + } + } + std::string & newDynamic = replaceSection(".dynamic", + rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn)); + + unsigned int idx = 0; + for ( ; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ; + debug("DT_NULL index is %d\n", idx); + + /* Shift all entries down by one. */ + setSubstr(newDynamic, sizeof(Elf_Dyn), + std::string(newDynamic, 0, sizeof(Elf_Dyn) * (idx + 1))); + + /* Add the DT_DEBUG entry at the top. */ + Elf_Dyn newDyn; + wri(newDyn.d_tag, DT_DEBUG); + newDyn.d_un.d_val = 0; + setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn))); + + this->rewriteSections(); + changed = true; +} + template void ElfFile::clearSymbolVersions(const std::set & syms) { @@ -1691,6 +1723,7 @@ static std::vector allowedRpathPrefixes; static bool removeRPath = false; static bool setRPath = false; static bool addRPath = false; +static bool addDebug = false; static bool printRPath = false; static std::string newRPath; static std::set neededLibsToRemove; @@ -1737,6 +1770,9 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con if (noDefaultLib) elfFile.noDefaultLib(); + if (addDebug) + elfFile.addDebug(); + if (elfFile.isChanged()){ writeFile(fileName, elfFile.fileContents); } else if (alwaysWrite) { @@ -1793,6 +1829,7 @@ void showHelp(const std::string & progName) [--print-needed]\n\ [--no-default-lib]\n\ [--clear-symbol-version SYMBOL]\n\ + [--add-debug]\n\ [--output FILE]\n\ [--debug]\n\ [--version]\n\ @@ -1901,6 +1938,9 @@ int mainWrapped(int argc, char * * argv) else if (arg == "--no-default-lib") { noDefaultLib = true; } + else if (arg == "--add-debug") { + addDebug = true; + } else if (arg == "--help" || arg == "-h" ) { showHelp(argv[0]); return 0; diff --git a/src/patchelf.h b/src/patchelf.h index 93a0e5c..b64e729 100644 --- a/src/patchelf.h +++ b/src/patchelf.h @@ -131,6 +131,8 @@ public: void noDefaultLib(); + void addDebug(); + void clearSymbolVersions(const std::set & syms); private: