]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add a --source-comment=<text> option to objdump which provides a prefix to dipslayed...
authorNick Clifton <nickc@redhat.com>
Wed, 28 Aug 2019 10:39:19 +0000 (11:39 +0100)
committerNick Clifton <nickc@redhat.com>
Wed, 28 Aug 2019 10:39:19 +0000 (11:39 +0100)
PR 24931
* objdump.c (source_comment): New static variable.
(option_values): Add OPTION_SOURCE_COMMENT.
(long_opions): Add --source-comment.
(print_line): If source comment is set, use it as a prefix to the
source code line.
(main): Handle OPTION_SOURCE_COMMENT.
* doc/binutils.texi: Document the new option.
* NEWS: Mention the new feature.
* testsuite/binutils-all/objdump.exp (test_objdump_S): Add tests
of the -S and --source-comment options.

binutils/ChangeLog
binutils/NEWS
binutils/doc/binutils.texi
binutils/objdump.c
binutils/testsuite/binutils-all/objdump.exp

index 4de9a9302c9f8e2306b0a9ea7071c81f066a49df..6ddf000c05bef67011072a4b69583f2702ecd02c 100644 (file)
@@ -1,3 +1,17 @@
+2019-08-28  Nick Clifton  <nickc@redhat.com>
+
+       PR 24931
+       * objdump.c (source_comment): New static variable.
+       (option_values): Add OPTION_SOURCE_COMMENT.
+       (long_opions): Add --source-comment.
+       (print_line): If source comment is set, use it as a prefix to the
+       source code line.
+       (main): Handle OPTION_SOURCE_COMMENT.
+       * doc/binutils.texi: Document the new option.
+       * NEWS: Mention the new feature.
+       * testsuite/binutils-all/objdump.exp (test_objdump_S): Add tests
+       of the -S and --source-comment options.
+
 2019-08-27  Nick Clifton  <nickc@redhat.com>
 
        PR 24510
index b8921814eeb1e70cee94f4f653086294fcab4514..c6c4e29602945c892333c2c760ae1733080f1d81 100644 (file)
@@ -1,4 +1,6 @@
 -*- text -*-
+* Add --source-comment[=<txt>] option to objdump which if present,
+  provides a prefix to source code lines displayed in a disassembly.
 
 * Add --verilog-data-width option to objcopy for verilog targets to control
   width of data elements in verilog hex format.
index 25b2202dcdaa02418d9cb8a4374e3c82315adabc..f6cdbdb5a7f58b096b536cce345731b1beec7c1c 100644 (file)
@@ -2104,6 +2104,7 @@ objdump [@option{-a}|@option{--archive-headers}]
         [@option{-j} @var{section}|@option{--section=}@var{section}]
         [@option{-l}|@option{--line-numbers}]
         [@option{-S}|@option{--source}]
+        [@option{--source-comment}[=@var{text}]]
         [@option{-m} @var{machine}|@option{--architecture=}@var{machine}]
         [@option{-M} @var{options}|@option{--disassembler-options=}@var{options}]
         [@option{-p}|@option{--private-headers}]
@@ -2613,6 +2614,15 @@ non-empty sections are displayed.
 Display source code intermixed with disassembly, if possible.  Implies
 @option{-d}.
 
+@item --source-comment[=@var{txt}]
+@cindex source disassembly
+@cindex disassembly, with source
+Like the @option{-S} option, but all source code lines are displayed
+with a prefix of @var{txt}.  Typically @var{txt} will be a comment
+string which can be used to distinguish the assembler code from the
+source code.  If @var{txt} is not provided then a default string of
+@var{``# ``} (hash followed by a space), will be used.
+
 @item --prefix=@var{prefix}
 @cindex Add prefix to absolute paths
 Specify @var{prefix} to add to the absolute paths when used with
index fffbcf876db704fbb9f5ba151a336e0dfddd329e..33d5d72d3d55d496b8f866a818d8004e68869c0e 100644 (file)
@@ -123,6 +123,7 @@ static int prefix_strip;            /* --prefix-strip */
 static size_t prefix_length;
 static bfd_boolean unwind_inlines;     /* --inlines.  */
 static const char * disasm_sym;                /* Disassembly start symbol.  */
+static const char * source_comment;     /* --source_comment.  */
 
 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
 
@@ -217,6 +218,7 @@ usage (FILE *stream, int status)
   -D, --disassemble-all    Display assembler contents of all sections\n\
       --disassemble=<sym>  Display assembler contents from <sym>\n\
   -S, --source             Intermix source code with disassembly\n\
+      --source-comment[=<txt>] Prefix lines of source code with <txt>\n\
   -s, --full-contents      Display the full contents of all sections requested\n\
   -g, --debugging          Display debug information in object file\n\
   -e, --debugging-tags     Display debug information using ctags style\n\
@@ -313,6 +315,7 @@ enum option_values
     OPTION_RECURSE_LIMIT,
     OPTION_NO_RECURSE_LIMIT,
     OPTION_INLINES,
+    OPTION_SOURCE_COMMENT,
     OPTION_CTF,
     OPTION_CTF_PARENT
   };
@@ -354,6 +357,7 @@ static struct option long_options[]=
   {"section-headers", no_argument, NULL, 'h'},
   {"show-raw-insn", no_argument, &show_raw_insn, 1},
   {"source", no_argument, NULL, 'S'},
+  {"source-comment", optional_argument, NULL, OPTION_SOURCE_COMMENT},
   {"special-syms", no_argument, &dump_special_syms, 1},
   {"include", required_argument, NULL, 'I'},
   {"dwarf", optional_argument, NULL, OPTION_DWARF},
@@ -1594,8 +1598,10 @@ print_line (struct print_file_list *p, unsigned int linenum)
   if (linenum >= p->maxline)
     return;
   l = p->linemap [linenum];
-  /* Test fwrite return value to quiet glibc warning.  */
+  if (source_comment != NULL && strlen (l) > 0)
+    printf ("%s", source_comment);
   len = strcspn (l, "\n\r");
+  /* Test fwrite return value to quiet glibc warning.  */
   if (len == 0 || fwrite (l, len, 1, stdout) == 1)
     putchar ('\n');
 }
@@ -4455,6 +4461,15 @@ main (int argc, char **argv)
          with_source_code = TRUE;
          seenflag = TRUE;
          break;
+       case OPTION_SOURCE_COMMENT:
+         disassemble = TRUE;
+         with_source_code = TRUE;
+         seenflag = TRUE;
+         if (optarg)
+           source_comment = xstrdup (sanitize_string (optarg));
+         else
+           source_comment = xstrdup ("# ");
+         break;
        case 'g':
          dump_debugging = 1;
          seenflag = TRUE;
@@ -4566,6 +4581,7 @@ main (int argc, char **argv)
   free_only_list ();
   free (dump_ctf_section_name);
   free (dump_ctf_parent_name);
+  free ((void *) source_comment);
 
   END_PROGRESS (program_name);
 
index c229194cbb6d5788f57a9dac93685f9acecf30b4..9630bacb9053a750cedfdfaf0ded8bd6c424299e 100644 (file)
@@ -803,6 +803,46 @@ proc test_objdump_dotnet_assemblies {} {
 
 test_objdump_dotnet_assemblies
 
+# Test objdump -S
+
+proc test_objdump_S { } {
+    global srcdir
+    global subdir
+    global OBJDUMP
+    global OBJDUMPFLAGS
+    
+    set test "objdump -S"
+
+    if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog executable debug] != "" } {
+       unsupported "$test (build)"
+       return
+    }
+
+    set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -S tmpdir/testprog"]
+
+    set want "static int local = 2"
+
+    if [regexp $want $got] then {
+       pass $test
+    } else {
+       fail $test
+    }
+
+    set test "objdump --source-comment"
+
+    set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS --source-comment=// tmpdir/testprog"]
+
+    set want "//static int local = 2"
+
+    if [regexp $want $got] then {
+       pass $test
+    } else {
+       fail $test
+    }
+}
+
+test_objdump_S
+
 # Options which are not tested: -a -D -R -T -x -l --stabs
 # I don't see any generic way to test any of these other than -a.
 # Tests could be written for specific targets, and that should be done