public inbox for debugedit@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] debugedit: decompress DWARF sections if found
@ 2022-11-06 17:10 Morten Linderud
  0 siblings, 0 replies; only message in thread
From: Morten Linderud @ 2022-11-06 17:10 UTC (permalink / raw)
  To: debugedit; +Cc: Morten Linderud

When encountering compressed DWARF section try to decompress them
before rewriting. This will not recompress any sections.

Introduce --no-decompress-dwarf to skip compressed sections instead of
decompressing them.

Signed-off-by: Morten Linderud <morten@linderud.pw>
---
 tests/debugedit.at | 23 +++++++++++++++++++++++
 tools/debugedit.c  | 35 +++++++++++++++++++++++++++++++++--
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/tests/debugedit.at b/tests/debugedit.at
index 725e68e..84fadaa 100644
--- a/tests/debugedit.at
+++ b/tests/debugedit.at
@@ -696,3 +696,26 @@ $CC $CFLAGS -gdwarf-5 -o main main.c
 AT_CHECK([[debugedit -l sources.list main]])
 AT_CHECK([[grep -q main.c sources.list]])
 AT_CLEANUP
+
+# ===
+# source list with compression dwarf-4
+# ===
+AT_SETUP([debugedit --list-file compessed])
+AT_KEYWORDS([debuginfo] [debugedit])
+echo "int main () { }" > main.c
+$CC $CFLAGS -gz=zlib -gdwarf-4 -o main main.c
+AT_CHECK([[debugedit -l sources.list main]])
+AT_CHECK([[grep -q main.c sources.list]])
+AT_CLEANUP
+
+
+# ===
+# no decompress dwarf
+# ===
+AT_SETUP([debugedit --no-decompress-dwarf --list-file compessed])
+AT_KEYWORDS([debuginfo] [debugedit])
+echo "int main () { }" > main.c
+$CC $CFLAGS -gz=zlib -gdwarf-4 -o main main.c
+AT_CHECK([[debugedit -c -l sources.list main]])
+AT_CHECK([[test `wc -l < sources.list` -eq 0]])
+AT_CLEANUP
diff --git a/tools/debugedit.c b/tools/debugedit.c
index d82ae5a..9d7e29f 100644
--- a/tools/debugedit.c
+++ b/tools/debugedit.c
@@ -18,6 +18,7 @@
    along with this program; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
+#include <libelf.h>
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -92,6 +93,7 @@ char *list_file = NULL;
 int list_file_fd = -1;
 int do_build_id = 0;
 int no_recompute_build_id = 0;
+int no_decompress_dwarf = 0;
 char *build_id_seed = NULL;
 
 int show_version = 0;
@@ -202,6 +204,7 @@ typedef struct
   struct strings debug_str, debug_line_str;
   struct debug_lines lines;
   GElf_Shdr shdr[0];
+  GElf_Chdr chdr[0];
 } DSO;
 
 static void
@@ -2608,6 +2611,27 @@ edit_dwarf2 (DSO *dso)
 		    }
 
 		  scn = dso->scn[i];
+
+		  // Check for compressed DWARF headers
+		  // does not recompress headers if we encounter them
+		  if (dso->shdr[i].sh_flags & SHF_COMPRESSED)
+		  {
+		    // Found compressed sections, abort rewrite if we don't intend to decompress
+		    if (no_decompress_dwarf)
+		      return 0;
+
+		    if (gelf_getchdr(dso->scn[i], &dso->chdr[i]) == NULL)
+		      error (1, 0, "Couldn't get compressed header: %s",
+			  elf_errmsg (-1));
+		    uint32_t ch_type;
+		    ch_type = dso->chdr[i].ch_type;
+		    if(ch_type != ELFCOMPRESS_ZLIB)
+		      error (1, 0, "Unknown compression method");
+		    if (elf_compress (scn, 0, 0) == 0)
+		      error (1, 0, "Failed decompression");
+		    gelf_getshdr (scn, &dso->shdr[i]);
+		  }
+
 		  data = elf_getdata (scn, NULL);
 		  assert (data != NULL && data->d_buf != NULL);
 		  assert (elf_getdata (scn, data) == NULL);
@@ -3029,13 +3053,14 @@ static struct option optionsTable[] =
     { "build-id", no_argument, 0, 'i' },
     { "build-id-seed", required_argument, 0, 's' },
     { "no-recompute-build-id", no_argument, 0, 'n' },
+    { "no-decompress-dwarf", no_argument, 0, 'c' },
     { "version", no_argument, 0, 'V' },
     { "help", no_argument, 0, '?' },
     { "usage", no_argument, 0, 'u' },
     { NULL, 0, 0, 0 }
   };
 
-static const char *optionsChars = "b:d:l:is:nV?u";
+static const char *optionsChars = "b:d:l:is:ncV?u";
 
 static const char *helpText =
   "Usage: %s [OPTION...] FILE\n"
@@ -3049,6 +3074,7 @@ static const char *helpText =
   "                                  this string as hash seed\n"
   "  -n, --no-recompute-build-id     do not recompute build ID note even\n"
   "                                  when-i or -s are given\n"
+  "  -c, --no-decompress-dwarf       do not decompress DWARF headers\n"
   "\n"
   "Help options:\n"
   "  -?, --help                      Show this help message\n"
@@ -3059,7 +3085,8 @@ static const char *usageText =
   "Usage: %s [-in?] [-b|--base-dir STRING] [-d|--dest-dir STRING]\n"
   "        [-l|--list-file STRING] [-i|--build-id] \n"
   "        [-s|--build-id-seed STRING]\n"
-  "        [-n|--no-recompute-build-id] [-?|--help] [-u|--usage]\n"
+  "        [-n|--no-recompute-build-id]\n"
+  "        [-c|--no-decompress-dwarf] [-?|--help] [-u|--usage]\n"
   "        [-V|--version] FILE\n";
 
 static void
@@ -3362,6 +3389,10 @@ main (int argc, char *argv[])
 	  no_recompute_build_id = 1;
 	  break;
 
+	case 'c':
+	  no_decompress_dwarf = 1;
+	  break;
+
 	case 'V':
 	  show_version = 1;
 	  break;
-- 
2.38.1

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-06 17:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-06 17:10 [PATCH] debugedit: decompress DWARF sections if found Morten Linderud

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).