@@ -1652,6 +1652,38 @@ void ElfFile<ElfFileParamNames>::noDefaultLib()
16521652 changed = true ;
16531653}
16541654
1655+ template <ElfFileParams>
1656+ void ElfFile<ElfFileParamNames>::addDebugTag()
1657+ {
1658+ auto shdrDynamic = findSectionHeader (" .dynamic" );
1659+
1660+ auto dyn = (Elf_Dyn *)(fileContents->data () + rdi (shdrDynamic.sh_offset ));
1661+ for ( ; rdi (dyn->d_tag ) != DT_NULL; dyn++) {
1662+ if (rdi (dyn->d_tag ) == DT_DEBUG) {
1663+ return ;
1664+ }
1665+ }
1666+ std::string & newDynamic = replaceSection (" .dynamic" ,
1667+ rdi (shdrDynamic.sh_size ) + sizeof (Elf_Dyn));
1668+
1669+ unsigned int idx = 0 ;
1670+ for ( ; rdi (((Elf_Dyn *) newDynamic.c_str ())[idx].d_tag ) != DT_NULL; idx++) ;
1671+ debug (" DT_NULL index is %d\n " , idx);
1672+
1673+ /* Shift all entries down by one. */
1674+ setSubstr (newDynamic, sizeof (Elf_Dyn),
1675+ std::string (newDynamic, 0 , sizeof (Elf_Dyn) * (idx + 1 )));
1676+
1677+ /* Add the DT_DEBUG entry at the top. */
1678+ Elf_Dyn newDyn;
1679+ wri (newDyn.d_tag , DT_DEBUG);
1680+ newDyn.d_un .d_val = 0 ;
1681+ setSubstr (newDynamic, 0 , std::string ((char *) &newDyn, sizeof (Elf_Dyn)));
1682+
1683+ this ->rewriteSections ();
1684+ changed = true ;
1685+ }
1686+
16551687template <ElfFileParams>
16561688void ElfFile<ElfFileParamNames>::clearSymbolVersions(const std::set<std::string> & syms)
16571689{
@@ -1691,6 +1723,7 @@ static std::vector<std::string> allowedRpathPrefixes;
16911723static bool removeRPath = false ;
16921724static bool setRPath = false ;
16931725static bool addRPath = false ;
1726+ static bool addDebugTag = false ;
16941727static bool printRPath = false ;
16951728static std::string newRPath;
16961729static std::set<std::string> neededLibsToRemove;
@@ -1737,6 +1770,9 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
17371770 if (noDefaultLib)
17381771 elfFile.noDefaultLib ();
17391772
1773+ if (addDebugTag)
1774+ elfFile.addDebugTag ();
1775+
17401776 if (elfFile.isChanged ()){
17411777 writeFile (fileName, elfFile.fileContents );
17421778 } else if (alwaysWrite) {
@@ -1793,6 +1829,7 @@ void showHelp(const std::string & progName)
17931829 [--print-needed]\n \
17941830 [--no-default-lib]\n \
17951831 [--clear-symbol-version SYMBOL]\n \
1832+ [--add-debug-tag]\n \
17961833 [--output FILE]\n \
17971834 [--debug]\n \
17981835 [--version]\n \
@@ -1901,6 +1938,9 @@ int mainWrapped(int argc, char * * argv)
19011938 else if (arg == " --no-default-lib" ) {
19021939 noDefaultLib = true ;
19031940 }
1941+ else if (arg == " --add-debug-tag" ) {
1942+ addDebugTag = true ;
1943+ }
19041944 else if (arg == " --help" || arg == " -h" ) {
19051945 showHelp (argv[0 ]);
19061946 return 0 ;
0 commit comments