From a1c110a3fe02f5cb8de18137f6615005288e849e Mon Sep 17 00:00:00 2001 From: Nick Clifton Date: Wed, 28 Aug 2019 11:39:19 +0100 Subject: [PATCH] Add a --source-comment= option to objdump which provides a prefix to dipslayed source code lines. 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 | 14 ++++++++++ binutils/NEWS | 2 ++ binutils/doc/binutils.texi | 10 ++++++++ binutils/objdump.c | 18 ++++++++++++- binutils/testsuite/binutils-all/objdump.exp | 40 +++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/binutils/ChangeLog b/binutils/ChangeLog index 4de9a93..6ddf000 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,17 @@ +2019-08-28 Nick Clifton + + 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 PR 24510 diff --git a/binutils/NEWS b/binutils/NEWS index b892181..c6c4e29 100644 --- a/binutils/NEWS +++ b/binutils/NEWS @@ -1,4 +1,6 @@ -*- text -*- +* Add --source-comment[=] 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. diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index 25b2202..f6cdbdb 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -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 diff --git a/binutils/objdump.c b/binutils/objdump.c index fffbcf8..33d5d72 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -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= Display assembler contents from \n\ -S, --source Intermix source code with disassembly\n\ + --source-comment[=] Prefix lines of source code with \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); diff --git a/binutils/testsuite/binutils-all/objdump.exp b/binutils/testsuite/binutils-all/objdump.exp index c229194..9630bac 100644 --- a/binutils/testsuite/binutils-all/objdump.exp +++ b/binutils/testsuite/binutils-all/objdump.exp @@ -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 -- 2.7.4