public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 4/4] Add an optional offset option to the "add-symbol-file" command
  2018-06-08 12:40 [PATCH 0/4] Allow loading symbol files with an offset Petr Tesarik
  2018-06-08 12:40 ` [PATCH 1/4] Add an optional offset option to the "symbol-file" command Petr Tesarik
  2018-06-08 12:40 ` [PATCH 3/4] Make sure that sorting does not change section order Petr Tesarik
@ 2018-06-08 12:40 ` Petr Tesarik
  2018-06-08 12:53 ` [PATCH 2/4] Make add-symbol-file's address argument optional Petr Tesarik
  2018-06-08 13:34 ` [PATCH 0/4] Allow loading symbol files with an offset Pedro Alves
  4 siblings, 0 replies; 12+ messages in thread
From: Petr Tesarik @ 2018-06-08 12:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi, John Baldwin, Petr Tesarik, Jeff Mahoney

From: Petr Tesarik <petr@tesarici.cz>

If all sections of a symbol file are loaded with a fixed offset, it
is easier to specify that offset than listing all sections
explicitly.  There is also a similar option for "symbol-file".

gdb/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* symfile.c (add_symbol_file_command): Add option "-o" to
	add-symbol-file-load to add an offset to each section's load
	address.

gdb/doc/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* gdb.texinfo (Files): Document "add-symbol-file -o offset".

gdb/testsuite/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* gdb.base/relocate.exp: Test test for "add-symbol-file -o ".
---
 gdb/ChangeLog                       |  2 ++
 gdb/doc/ChangeLog                   |  1 +
 gdb/doc/gdb.texinfo                 | 10 +++++--
 gdb/symfile.c                       | 55 +++++++++++++++++++++++++++++++++++-
 gdb/testsuite/ChangeLog             |  1 +
 gdb/testsuite/gdb.base/relocate.exp | 56 +++++++++++++++++++++++++++++++++++++
 6 files changed, 121 insertions(+), 4 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 91a5d7b4b9..2cb02b6959 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -7,6 +7,8 @@
 	omitted, load sections at the addresses specified in the file.
 	(add_symbol_file_command): Make sure that sections with the same
 	name are sorted in the same order.
+	(add_symbol_file_command): Add option "-o" to add-symbol-file-load
+	to add an offset to each section's load address.
 
 2018-06-07  Pedro Alves  <palves@redhat.com>
 
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 7ddf5b0e9d..02eae142ab 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -3,6 +3,7 @@
 	* gdb.texinfo (Files): Document "symbol-file -o offset".
 	(Files): The address argument for "add-symbol-file" is no longer
 	mandatory.
+	(Files): Document "add-symbol-file -o offset".
 
 2018-06-05  Tom Tromey	<tom@tromey.com>
 
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index b83ce052e7..5c4e9f6afe 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18917,9 +18917,9 @@ the program is running.  To do this, use the @code{kill} command
 
 @kindex add-symbol-file
 @cindex dynamic linking
-@item add-symbol-file @var{filename} @r{[} @var{address} @r{]}
-@itemx add-symbol-file @var{filename} @r{[} @var{address} @r{]} @r{[} -readnow @r{|} -readnever @r{]}
-@itemx add-symbol-file @var{filename} @r{[} @var{address} @r{]} -s @var{section} @var{address} @dots{}
+@item add-symbol-file @var{filename} @r{[} @var{address} @r{]} @r{[} -o @var{offset} @r{]}
+@itemx add-symbol-file @var{filename} @r{[} @var{address} @r{]} @r{[} -o @var{offset} @r{]} @r{]} @r{[} -readnow @r{|} -readnever @r{]}
+@itemx add-symbol-file @var{filename} @r{[} @var{address} @r{]} @r{[} -o @var{offset} @r{]} @r{]} -s @var{section} @var{address} @dots{}
 The @code{add-symbol-file} command reads additional symbol table
 information from the file @var{filename}.  You would use this command
 when @var{filename} has been dynamically loaded (by some other means)
@@ -18933,6 +18933,10 @@ If @var{address} is omitted, @value{GDBN} will use the section
 addresses found in @var{filename}.  You can use @samp{-s} to
 override this default and load a section at a different address.
 
+If an optional @var{offset} is specified, it is added to the start
+address of each section, except those for which the address was
+specified explicitly.
+
 The symbol table of the file @var{filename} is added to the symbol table
 originally read with the @code{symbol-file} command.  You can use the
 @code{add-symbol-file} command any number of times; the new symbol data
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 890e2478bf..dc6e5e06d3 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2106,6 +2106,7 @@ add_symbol_file_command (const char *args, int from_tty)
 
   std::vector<sect_opt> sect_opts = { { ".text", NULL } };
   bool stop_processing_options = false;
+  CORE_ADDR offset = 0;
 
   dont_repeat ();
 
@@ -2113,6 +2114,7 @@ add_symbol_file_command (const char *args, int from_tty)
     error (_("add-symbol-file takes a file name and an address"));
 
   bool seen_addr = false;
+  bool seen_offset = false;
   gdb_argv argv (args);
 
   for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
@@ -2150,6 +2152,15 @@ add_symbol_file_command (const char *args, int from_tty)
 	  sect_opts.push_back (sect);
 	  argcnt += 2;
 	}
+      else if (strcmp (arg, "-o") == 0)
+	{
+	  arg = argv[++argcnt];
+	  if (arg == NULL)
+	    error (_("Missing argument to -o"));
+
+	  offset = parse_and_eval_address (arg);
+	  seen_offset = true;
+	}
       else if (strcmp (arg, "--") == 0)
 	stop_processing_options = true;
       else
@@ -2195,7 +2206,13 @@ add_symbol_file_command (const char *args, int from_tty)
 	 At this point, we don't know what file type this is,
 	 so we can't determine what section names are valid.  */
     }
-  if (section_addrs.size () == 0)
+  if (seen_offset)
+      printf_unfiltered (_("%s offset by %s\n"),
+			 (section_addrs.size () == 0
+			  ? _(" with all sections")
+			  : _("with other sections")),
+			 paddress (gdbarch, offset));
+  else if (section_addrs.size () == 0)
     printf_unfiltered ("\n");
 
   if (from_tty && (!query ("%s", "")))
@@ -2204,6 +2221,42 @@ add_symbol_file_command (const char *args, int from_tty)
   objf = symbol_file_add (filename.get (), add_flags, &section_addrs,
 			  flags);
 
+  if (seen_offset)
+    {
+      std::vector<struct section_offsets> new_offsets (objf->num_sections,
+						       { offset });
+
+      std::vector<const struct other_sections *> sect_addrs_sorted
+	= addrs_section_sort (section_addrs);
+
+      section_addr_info objf_addrs
+	= build_section_addr_info_from_objfile (objf);
+      std::vector<const struct other_sections *> objf_addrs_sorted
+	= addrs_section_sort (objf_addrs);
+
+      std::vector<const struct other_sections *>::iterator sect_sorted_iter
+	= sect_addrs_sorted.begin ();
+      for (const struct other_sections *objf_sect : objf_addrs_sorted)
+	{
+	  const char *objf_name = addr_section_name (objf_sect->name.c_str ());
+	  int cmp = -1;
+
+	  while (cmp < 0 && sect_sorted_iter != sect_addrs_sorted.end ())
+	    {
+	      const struct other_sections *sect = *sect_sorted_iter;
+	      const char *sect_name = addr_section_name (sect->name.c_str ());
+	      cmp = strcmp (sect_name, objf_name);
+	      if (cmp <= 0)
+		++sect_sorted_iter;
+	    }
+
+	  if (cmp == 0)
+	    new_offsets[objf_sect->sectindex].offsets[0] = 0;
+	}
+
+      objfile_relocate (objf, new_offsets.data ());
+    }
+
   add_target_sections_of_objfile (objf);
 
   /* Getting new symbols may change our opinion about what is
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index ffd97e66b9..61798a3185 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -3,6 +3,7 @@
 	* gdb.base/relocate.exp: Add test for "symbol-file -o ".
 	Test add-symbol-file behavior when the address argument is
 	omitted.
+	Add test for "add-symbol-file -o ".
 
 2018-06-05  Tom Tromey  <tom@tromey.com>
 
diff --git a/gdb/testsuite/gdb.base/relocate.exp b/gdb/testsuite/gdb.base/relocate.exp
index a3af8cea61..119495dd94 100644
--- a/gdb/testsuite/gdb.base/relocate.exp
+++ b/gdb/testsuite/gdb.base/relocate.exp
@@ -235,6 +235,62 @@ set new_function_foo_addr [get_var_address function_foo]
 gdb_assert {${new_function_foo_addr} == ${function_foo_addr} + $offset} \
     "function foo is moved by offset"
 
+# Load the object using add-symbol-file with an offset and check that
+# all addresses are moved by that offset.
+
+set offset 0x10000
+clean_restart
+gdb_test "add-symbol-file -o $offset $binfile" \
+    "Reading symbols from ${binfile}\.\.\.done\." \
+    "add-symbol-file with offset" \
+    "add symbol table from file \".*${testfile}\\.o\" with all sections offset by $offset\[\r\n\]+\\(y or n\\) " \
+    "y"
+
+# Make sure the address of a static variable is moved by offset.
+set new_static_foo_addr [get_var_address static_foo]
+gdb_assert { ${new_static_foo_addr} == ${static_foo_addr} + $offset } \
+    "static variable foo is moved by offset"
+
+# Make sure the address of a global variable is moved by offset.
+set new_global_foo_addr [get_var_address global_foo]
+gdb_assert { ${new_global_foo_addr} == ${global_foo_addr} + $offset } \
+    "global variable foo is moved by offset"
+
+# Make sure the address of a function is moved by offset.
+set new_function_foo_addr [get_var_address function_foo]
+gdb_assert { ${new_function_foo_addr} == ${function_foo_addr} + $offset } \
+    "function foo is moved by offset"
+
+# Re-load the object giving an explicit address for .text
+
+set text [ format "0x%x" [expr ${function_foo_addr} + 0x20000] ]
+clean_restart
+gdb_test "add-symbol-file $binfile -o $offset $text" \
+    "Reading symbols from ${binfile}\.\.\.done\." \
+    "add-symbol-file with offset, text address given" \
+    "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = ${text}\[\r\n\]+with other sections offset by ${offset}\[\r\n\]+\\(y or n\\) " \
+    "y"
+
+# Make sure function has a different addresses now.
+set function_foo_addr [get_var_address function_foo]
+gdb_assert { ${function_foo_addr} != ${new_function_foo_addr} } \
+    "function foo has a different address"
+
+# Re-load the object giving an explicit address for .data
+
+set data [ format "0x%x" [expr ${global_foo_addr} + 0x20000] ]
+clean_restart
+gdb_test "add-symbol-file $binfile -o $offset -s .data $data" \
+    "Reading symbols from ${binfile}\.\.\.done\." \
+    "add-symbol-file with offset, data address given" \
+    "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.data_addr = ${data}\[\r\n\]+with other sections offset by ${offset}\[\r\n\]+\\(y or n\\) " \
+    "y"
+
+# Make sure variable has a different addresses now.
+set global_foo_addr [get_var_address global_foo]
+gdb_assert { ${global_foo_addr} != ${new_global_foo_addr} } \
+    "global variable foo has a different address"
+
 # Now try loading the object as an exec-file; we should be able to print
 # the values of variables after we do this.
 
-- 
2.13.6

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 3/4] Make sure that sorting does not change section order
  2018-06-08 12:40 [PATCH 0/4] Allow loading symbol files with an offset Petr Tesarik
  2018-06-08 12:40 ` [PATCH 1/4] Add an optional offset option to the "symbol-file" command Petr Tesarik
@ 2018-06-08 12:40 ` Petr Tesarik
  2018-06-08 12:40 ` [PATCH 4/4] Add an optional offset option to the "add-symbol-file" command Petr Tesarik
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Petr Tesarik @ 2018-06-08 12:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi, John Baldwin, Petr Tesarik, Jeff Mahoney

From: Petr Tesarik <petr@tesarici.cz>

Symbol files may contain multiple sections with the same name.
Section addresses specified add-symbol-file are assigned to the
corresponding BFD sections in addr_info_make_relative using sorted
indexes of both vectors.  Since the sort algorithm is not inherently
stable, the comparison function uses sectindex to maintain the
original order.  However, add_symbol_file_command uses zero for all
sections, so if the user specifies multiple sections with the same
name, they will be assigned randomly to symbol file sections with
the same name.

gdb/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* symfile.c (add_symbol_file_command): Make sure that sections
	with the same name are sorted in the same order.
---
 gdb/ChangeLog | 2 ++
 gdb/symfile.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index e6e763b506..91a5d7b4b9 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -5,6 +5,8 @@
 	offset to each section of the symbol file.
 	(add_symbol_file_command): Do not require the second argument.  If
 	omitted, load sections at the addresses specified in the file.
+	(add_symbol_file_command): Make sure that sections with the same
+	name are sorted in the same order.
 
 2018-06-07  Pedro Alves  <palves@redhat.com>
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index d319bb0564..890e2478bf 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2185,7 +2185,7 @@ add_symbol_file_command (const char *args, int from_tty)
 
       /* Here we store the section offsets in the order they were
          entered on the command line.  */
-      section_addrs.emplace_back (addr, sec, 0);
+      section_addrs.emplace_back (addr, sec, section_addrs.size ());
       printf_unfiltered ("\t%s_addr = %s\n", sec,
 			 paddress (gdbarch, addr));
 
-- 
2.13.6

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 0/4] Allow loading symbol files with an offset
@ 2018-06-08 12:40 Petr Tesarik
  2018-06-08 12:40 ` [PATCH 1/4] Add an optional offset option to the "symbol-file" command Petr Tesarik
                   ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Petr Tesarik @ 2018-06-08 12:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi, John Baldwin, Petr Tesarik, Jeff Mahoney

From: Petr Tesarik <petr@tesarici.cz>

Implement an "-o" option for symbol-file and add-symbol-file.

Petr Tesarik (4):
  Add an optional offset option to the "symbol-file" command
  Make add-symbol-file's address argument optional
  Make sure that sorting does not change section order
  Add an optional offset option to the "add-symbol-file" command

 gdb/ChangeLog                       |  12 +++++
 gdb/doc/ChangeLog                   |   7 +++
 gdb/doc/gdb.texinfo                 |  24 ++++++---
 gdb/symfile.c                       | 102 +++++++++++++++++++++++++++++-------
 gdb/testsuite/ChangeLog             |   7 +++
 gdb/testsuite/gdb.base/relocate.exp |  95 +++++++++++++++++++++++++++++++++
 6 files changed, 222 insertions(+), 25 deletions(-)

-- 
2.13.6

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/4] Add an optional offset option to the "symbol-file" command
  2018-06-08 12:40 [PATCH 0/4] Allow loading symbol files with an offset Petr Tesarik
@ 2018-06-08 12:40 ` Petr Tesarik
  2018-06-08 12:59   ` Tom Tromey
  2018-06-08 13:09   ` Eli Zaretskii
  2018-06-08 12:40 ` [PATCH 3/4] Make sure that sorting does not change section order Petr Tesarik
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 12+ messages in thread
From: Petr Tesarik @ 2018-06-08 12:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi, John Baldwin, Petr Tesarik, Jeff Mahoney

From: Petr Tesarik <petr@tesarici.cz>

If the main file is relocated at runtime, all symbols are offset by
a fixed amount.  Let the user specify this offset when loading a
symbol file.

gdb/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* symfile.c (symbol_file_command, symbol_file_add_main_1)
	(_initialize_symfile): Add option "-o" to symbol-file to add an
	offset to each section of the symbol file.

gdb/doc/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* gdb.texinfo (Files): Document "symbol-file -o offset".

gdb/testsuite/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* gdb.base/relocate.exp: Add test for "symbol-file -o ".
---
 gdb/ChangeLog                       |  6 ++++++
 gdb/doc/ChangeLog                   |  4 ++++
 gdb/doc/gdb.texinfo                 |  7 ++++++-
 gdb/symfile.c                       | 24 ++++++++++++++++++------
 gdb/testsuite/ChangeLog             |  4 ++++
 gdb/testsuite/gdb.base/relocate.exp | 24 ++++++++++++++++++++++++
 6 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f0b6b95f7e..27c6ed0a7c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2018-06-08  Petr Tesarik  <ptesarik@suse.com>
+
+	* symfile.c (symbol_file_command, symbol_file_add_main_1)
+	(_initialize_symfile): Add option "-o" to symbol-file to add an
+	offset to each section of the symbol file.
+
 2018-06-07  Pedro Alves  <palves@redhat.com>
 
 	* target.h (target_ops) <beneath>: Now a method.  All references
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 6191e84cc0..319d06a25f 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,7 @@
+2018-06-08  Petr Tesarik  <ptesarik@suse.com>
+
+	* gdb.texinfo (Files): Document "symbol-file -o offset".
+
 2018-06-05  Tom Tromey	<tom@tromey.com>
 
 	PR cli/12326:
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4968b374af..9d1c29612b 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18822,11 +18822,16 @@ if necessary to locate your program.  Omitting @var{filename} means to
 discard information on the executable file.
 
 @kindex symbol-file
-@item symbol-file @r{[} @var{filename} @r{]}
+@item symbol-file @r{[} -o @var{offset} @r{]} @r{[} @var{filename} @r{]}
 Read symbol table information from file @var{filename}.  @code{PATH} is
 searched when necessary.  Use the @code{file} command to get both symbol
 table and program to run from the same file.
 
+If an optional @var{offset} is specified, it is added to the start
+address of each section in the symbol file.  This is useful if the
+program is relocated at runtime, such as the Linux kernel with kASLR
+enabled.
+
 @code{symbol-file} with no argument clears out @value{GDBN} information on your
 program's symbol table.
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index f8177ea8b1..09344f83d2 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -87,7 +87,7 @@ int readnever_symbol_files;	/* Never read full symbols.  */
 /* Functions this file defines.  */
 
 static void symbol_file_add_main_1 (const char *args, symfile_add_flags add_flags,
-				    objfile_flags flags);
+				    objfile_flags flags, CORE_ADDR reloff);
 
 static const struct sym_fns *find_sym_fns (bfd *);
 
@@ -1225,16 +1225,18 @@ symbol_file_add (const char *name, symfile_add_flags add_flags,
 void
 symbol_file_add_main (const char *args, symfile_add_flags add_flags)
 {
-  symbol_file_add_main_1 (args, add_flags, 0);
+  symbol_file_add_main_1 (args, add_flags, 0, 0);
 }
 
 static void
 symbol_file_add_main_1 (const char *args, symfile_add_flags add_flags,
-			objfile_flags flags)
+			objfile_flags flags, CORE_ADDR reloff)
 {
   add_flags |= current_inferior ()->symfile_flags | SYMFILE_MAINLINE;
 
-  symbol_file_add (args, add_flags, NULL, flags);
+  struct objfile *objfile = symbol_file_add (args, add_flags, NULL, flags);
+  if (reloff != 0)
+    objfile_rebase (objfile, reloff);
 
   /* Getting new symbols may change our opinion about
      what is frameless.  */
@@ -1551,6 +1553,7 @@ symbol_file_command (const char *args, int from_tty)
       symfile_add_flags add_flags = 0;
       char *name = NULL;
       bool stop_processing_options = false;
+      CORE_ADDR offset = 0;
       int idx;
       char *arg;
 
@@ -1571,6 +1574,14 @@ symbol_file_command (const char *args, int from_tty)
 	    flags |= OBJF_READNOW;
 	  else if (strcmp (arg, "-readnever") == 0)
 	    flags |= OBJF_READNEVER;
+	  else if (strcmp (arg, "-o") == 0)
+	    {
+	      arg = built_argv[++idx];
+	      if (arg == NULL)
+		error (_("Missing argument to -o"));
+
+	      offset = parse_and_eval_address (arg);
+	    }
 	  else if (strcmp (arg, "--") == 0)
 	    stop_processing_options = true;
 	  else
@@ -1582,7 +1593,7 @@ symbol_file_command (const char *args, int from_tty)
 
       validate_readnow_readnever (flags);
 
-      symbol_file_add_main_1 (name, add_flags, flags);
+      symbol_file_add_main_1 (name, add_flags, flags, offset);
     }
 }
 
@@ -3774,7 +3785,8 @@ symbolic debug information."
 
   c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
 Load symbol table from executable file FILE.\n\
-Usage: symbol-file [-readnow | -readnever] FILE\n\
+Usage: symbol-file [-readnow | -readnever] [-o <OFF>] FILE\n\
+OFF is an optional offset which is added to each section address.\n\
 The `file' command can also load symbol tables, as well as setting the file\n\
 to execute.\n" READNOW_READNEVER_HELP), &cmdlist);
   set_cmd_completer (c, filename_completer);
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index eea782f05e..a9e0ec09ff 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2018-06-08  Petr Tesarik  <ptesarik@suse.com>
+
+	* gdb.base/relocate.exp: Add test for "symbol-file -o ".
+
 2018-06-05  Tom Tromey  <tom@tromey.com>
 
 	* gdb.base/default.exp: Update expected "show version" output.
diff --git a/gdb/testsuite/gdb.base/relocate.exp b/gdb/testsuite/gdb.base/relocate.exp
index 89f2fffcd9..77f6a88159 100644
--- a/gdb/testsuite/gdb.base/relocate.exp
+++ b/gdb/testsuite/gdb.base/relocate.exp
@@ -196,6 +196,30 @@ if { "${function_foo_addr}" == "${new_function_foo_addr}" } {
   pass "function foo has a different address"
 }
 
+# Load the object using symbol-file with an offset and check that
+# all addresses are moved by that offset.
+
+set offset 0x10000
+clean_restart
+gdb_test "symbol-file -o $offset $binfile" \
+    "Reading symbols from ${binfile}\.\.\.done\." \
+    "symbol-file with offset"
+
+# Make sure the address of a static variable is moved by offset.
+set new_static_foo_addr [get_var_address static_foo]
+gdb_assert {${new_static_foo_addr} == ${static_foo_addr} + $offset} \
+    "static variable foo is moved by offset"
+
+# Make sure the address of a global variable is moved by offset.
+set new_global_foo_addr [get_var_address global_foo]
+gdb_assert {${new_global_foo_addr} == ${global_foo_addr} + $offset} \
+    "global variable foo is moved by offset"
+
+# Make sure the address of a function is moved by offset.
+set new_function_foo_addr [get_var_address function_foo]
+gdb_assert {${new_function_foo_addr} == ${function_foo_addr} + $offset} \
+    "function foo is moved by offset"
+
 # Now try loading the object as an exec-file; we should be able to print
 # the values of variables after we do this.
 
-- 
2.13.6

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 2/4] Make add-symbol-file's address argument optional
  2018-06-08 12:40 [PATCH 0/4] Allow loading symbol files with an offset Petr Tesarik
                   ` (2 preceding siblings ...)
  2018-06-08 12:40 ` [PATCH 4/4] Add an optional offset option to the "add-symbol-file" command Petr Tesarik
@ 2018-06-08 12:53 ` Petr Tesarik
  2018-06-08 13:11   ` Eli Zaretskii
  2018-06-08 13:34 ` [PATCH 0/4] Allow loading symbol files with an offset Pedro Alves
  4 siblings, 1 reply; 12+ messages in thread
From: Petr Tesarik @ 2018-06-08 12:53 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi, John Baldwin, Petr Tesarik, Jeff Mahoney

From: Petr Tesarik <petr@tesarici.cz>

The (first) .text section must be always specified as the second
non-option argument.  The documentation states that GDB cannot
figure out this address by itself.  This is true if the object file
was indeed relocated, but it is also confusing, because all other
sections can be omitted and will use the address provided by BFD.

gdb/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* symfile.c (add_symbol_file_command): Do not require the second
	argument.  If omitted, load sections at the addresses specified in
	the file.

gdb/doc/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* gdb.texinfo (Files): The address argument for "add-symbol-file"
	is no longer mandatory.

gdb/testsuite/ChangeLog:
2018-06-08  Petr Tesarik  <ptesarik@suse.com>

	* gdb.base/relocate.exp: Test add-symbol-file behavior when the
	address argument is omitted.
---
 gdb/ChangeLog                       |  2 ++
 gdb/doc/ChangeLog                   |  2 ++
 gdb/doc/gdb.texinfo                 | 13 ++++++++-----
 gdb/symfile.c                       | 23 +++++++++++------------
 gdb/testsuite/ChangeLog             |  2 ++
 gdb/testsuite/gdb.base/relocate.exp | 15 +++++++++++++++
 6 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 27c6ed0a7c..e6e763b506 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -3,6 +3,8 @@
 	* symfile.c (symbol_file_command, symbol_file_add_main_1)
 	(_initialize_symfile): Add option "-o" to symbol-file to add an
 	offset to each section of the symbol file.
+	(add_symbol_file_command): Do not require the second argument.  If
+	omitted, load sections at the addresses specified in the file.
 
 2018-06-07  Pedro Alves  <palves@redhat.com>
 
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 319d06a25f..7ddf5b0e9d 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,6 +1,8 @@
 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
 
 	* gdb.texinfo (Files): Document "symbol-file -o offset".
+	(Files): The address argument for "add-symbol-file" is no longer
+	mandatory.
 
 2018-06-05  Tom Tromey	<tom@tromey.com>
 
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 9d1c29612b..b83ce052e7 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18917,18 +18917,21 @@ the program is running.  To do this, use the @code{kill} command
 
 @kindex add-symbol-file
 @cindex dynamic linking
-@item add-symbol-file @var{filename} @var{address}
-@itemx add-symbol-file @var{filename} @var{address} @r{[} -readnow @r{|} -readnever @r{]}
-@itemx add-symbol-file @var{filename} @var{address} -s @var{section} @var{address} @dots{}
+@item add-symbol-file @var{filename} @r{[} @var{address} @r{]}
+@itemx add-symbol-file @var{filename} @r{[} @var{address} @r{]} @r{[} -readnow @r{|} -readnever @r{]}
+@itemx add-symbol-file @var{filename} @r{[} @var{address} @r{]} -s @var{section} @var{address} @dots{}
 The @code{add-symbol-file} command reads additional symbol table
 information from the file @var{filename}.  You would use this command
 when @var{filename} has been dynamically loaded (by some other means)
 into the program that is running.  The @var{address} should give the memory
-address at which the file has been loaded; @value{GDBN} cannot figure
-this out for itself.  You can additionally specify an arbitrary number
+address at which the file has been loaded.
+You can additionally specify an arbitrary number
 of @samp{-s @var{section} @var{address}} pairs, to give an explicit
 section name and base address for that section.  You can specify any
 @var{address} as an expression.
+If @var{address} is omitted, @value{GDBN} will use the section
+addresses found in @var{filename}.  You can use @samp{-s} to
+override this default and load a section at a different address.
 
 The symbol table of the file @var{filename} is added to the symbol table
 originally read with the @code{symbol-file} command.  You can use the
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 09344f83d2..d319bb0564 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2161,29 +2161,26 @@ add_symbol_file_command (const char *args, int from_tty)
 
   validate_readnow_readnever (flags);
 
-  /* This command takes at least two arguments.  The first one is a
-     filename, and the second is the address where this file has been
-     loaded.  Abort now if this address hasn't been provided by the
-     user.  */
-  if (!seen_addr)
-    error (_("The address where %s has been loaded is missing"),
-	   filename.get ());
-
   /* Print the prompt for the query below.  And save the arguments into
      a sect_addr_info structure to be passed around to other
      functions.  We have to split this up into separate print
      statements because hex_string returns a local static
      string.  */
 
-  printf_unfiltered (_("add symbol table from file \"%s\" at\n"),
+  printf_unfiltered (_("add symbol table from file \"%s\""),
 		     filename.get ());
   section_addr_info section_addrs;
-  for (sect_opt &sect : sect_opts)
+  std::vector<sect_opt>::const_iterator it = sect_opts.begin ();
+  if (!seen_addr)
+    ++it;
+  for (; it != sect_opts.end (); ++it)
     {
       CORE_ADDR addr;
-      const char *val = sect.value;
-      const char *sec = sect.name;
+      const char *val = it->value;
+      const char *sec = it->name;
 
+      if (section_addrs.size () == 0)
+	printf_unfiltered (_(" at\n"));
       addr = parse_and_eval_address (val);
 
       /* Here we store the section offsets in the order they were
@@ -2198,6 +2195,8 @@ add_symbol_file_command (const char *args, int from_tty)
 	 At this point, we don't know what file type this is,
 	 so we can't determine what section names are valid.  */
     }
+  if (section_addrs.size () == 0)
+    printf_unfiltered ("\n");
 
   if (from_tty && (!query ("%s", "")))
     error (_("Not confirmed."));
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index a9e0ec09ff..ffd97e66b9 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,6 +1,8 @@
 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
 
 	* gdb.base/relocate.exp: Add test for "symbol-file -o ".
+	Test add-symbol-file behavior when the address argument is
+	omitted.
 
 2018-06-05  Tom Tromey  <tom@tromey.com>
 
diff --git a/gdb/testsuite/gdb.base/relocate.exp b/gdb/testsuite/gdb.base/relocate.exp
index 77f6a88159..a3af8cea61 100644
--- a/gdb/testsuite/gdb.base/relocate.exp
+++ b/gdb/testsuite/gdb.base/relocate.exp
@@ -73,6 +73,21 @@ gdb_test_multiple "add-symbol-file -s .text 0x200 $binfile 0x100" $test {
 	gdb_test "n" "Not confirmed\." $test
     }
 }
+# Check that passing a single "-s .text" is equivallent to passing
+# the text address in a positional argument.
+set test "add-symbol-file -s .text, no address"
+gdb_test_multiple "add-symbol-file $binfile -s .text 0x100" $test {
+    -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\\(y or n\\) " {
+	gdb_test "n" "Not confirmed\." $test
+    }
+}
+# Check section addresses can be omitted.
+set test "add-symbol-file no address"
+gdb_test_multiple "add-symbol-file $binfile" $test {
+    -re "add symbol table from file \"${binfile}\"\r\n\\(y or n\\) " {
+	gdb_test "n" "Not confirmed\." $test
+    }
+}
 # Test that passing "--" disables option processing.
 gdb_test "add-symbol-file -- $binfile 0x100 -s .bss 0x3" \
     "Unrecognized argument \"-s\"" \
-- 
2.13.6

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/4] Add an optional offset option to the "symbol-file" command
  2018-06-08 12:40 ` [PATCH 1/4] Add an optional offset option to the "symbol-file" command Petr Tesarik
@ 2018-06-08 12:59   ` Tom Tromey
  2018-06-08 13:09   ` Eli Zaretskii
  1 sibling, 0 replies; 12+ messages in thread
From: Tom Tromey @ 2018-06-08 12:59 UTC (permalink / raw)
  To: Petr Tesarik
  Cc: gdb-patches, Simon Marchi, John Baldwin, Petr Tesarik, Jeff Mahoney

>>>>> "Petr" == Petr Tesarik <ptesarik@suse.cz> writes:

Petr> 	* symfile.c (symbol_file_command, symbol_file_add_main_1)
Petr> 	(_initialize_symfile): Add option "-o" to symbol-file to add an
Petr> 	offset to each section of the symbol file.

I think this should also update the help text of the command to mention
the new option.  Patch #4 needs this too.

Tom

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/4] Add an optional offset option to the "symbol-file" command
  2018-06-08 12:40 ` [PATCH 1/4] Add an optional offset option to the "symbol-file" command Petr Tesarik
  2018-06-08 12:59   ` Tom Tromey
@ 2018-06-08 13:09   ` Eli Zaretskii
  1 sibling, 0 replies; 12+ messages in thread
From: Eli Zaretskii @ 2018-06-08 13:09 UTC (permalink / raw)
  To: Petr Tesarik; +Cc: gdb-patches, simon.marchi, jhb, petr, jeffm

> From: Petr Tesarik <ptesarik@suse.cz>
> Cc: Simon Marchi <simon.marchi@polymtl.ca>,	John Baldwin <jhb@freebsd.org>,	Petr Tesarik <petr@tesarici.cz>,	Jeff Mahoney <jeffm@suse.com>
> Date: Fri,  8 Jun 2018 14:39:57 +0200
> 
> gdb/ChangeLog:
> 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
> 
> 	* symfile.c (symbol_file_command, symbol_file_add_main_1)
> 	(_initialize_symfile): Add option "-o" to symbol-file to add an
> 	offset to each section of the symbol file.
> 
> gdb/doc/ChangeLog:
> 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
> 
> 	* gdb.texinfo (Files): Document "symbol-file -o offset".
> 
> gdb/testsuite/ChangeLog:
> 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
> 
> 	* gdb.base/relocate.exp: Add test for "symbol-file -o ".

OK for the documentation parts.

Thanks.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/4] Make add-symbol-file's address argument optional
  2018-06-08 12:53 ` [PATCH 2/4] Make add-symbol-file's address argument optional Petr Tesarik
@ 2018-06-08 13:11   ` Eli Zaretskii
  0 siblings, 0 replies; 12+ messages in thread
From: Eli Zaretskii @ 2018-06-08 13:11 UTC (permalink / raw)
  To: Petr Tesarik; +Cc: gdb-patches, simon.marchi, jhb, petr, jeffm

> From: Petr Tesarik <ptesarik@suse.cz>
> Cc: Simon Marchi <simon.marchi@polymtl.ca>,	John Baldwin <jhb@freebsd.org>,	Petr Tesarik <petr@tesarici.cz>,	Jeff Mahoney <jeffm@suse.com>
> Date: Fri,  8 Jun 2018 14:39:58 +0200
> 
> gdb/ChangeLog:
> 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
> 
> 	* symfile.c (add_symbol_file_command): Do not require the second
> 	argument.  If omitted, load sections at the addresses specified in
> 	the file.
> 
> gdb/doc/ChangeLog:
> 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
> 
> 	* gdb.texinfo (Files): The address argument for "add-symbol-file"
> 	is no longer mandatory.
> 
> gdb/testsuite/ChangeLog:
> 2018-06-08  Petr Tesarik  <ptesarik@suse.com>
> 
> 	* gdb.base/relocate.exp: Test add-symbol-file behavior when the
> 	address argument is omitted.

Thanks, the documentation part is OK.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/4] Allow loading symbol files with an offset
  2018-06-08 12:40 [PATCH 0/4] Allow loading symbol files with an offset Petr Tesarik
                   ` (3 preceding siblings ...)
  2018-06-08 12:53 ` [PATCH 2/4] Make add-symbol-file's address argument optional Petr Tesarik
@ 2018-06-08 13:34 ` Pedro Alves
  2018-06-11  9:41   ` Petr Tesarik
  4 siblings, 1 reply; 12+ messages in thread
From: Pedro Alves @ 2018-06-08 13:34 UTC (permalink / raw)
  To: Petr Tesarik, gdb-patches
  Cc: Simon Marchi, John Baldwin, Petr Tesarik, Jeff Mahoney

On 06/08/2018 01:39 PM, Petr Tesarik wrote:
> From: Petr Tesarik <petr@tesarici.cz>
> 
> Implement an "-o" option for symbol-file and add-symbol-file.

Seems like this is missing gdb/NEWS entries for both the new
-o options, and for making the address argument of add-symbol-file
optional.

Note that the de facto standard in gdb is for each commit to
have its own separate ChangeLog entry with date/author header instead
of appending to the previous entry.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/4] Allow loading symbol files with an offset
  2018-06-08 13:34 ` [PATCH 0/4] Allow loading symbol files with an offset Pedro Alves
@ 2018-06-11  9:41   ` Petr Tesarik
  2018-06-11 18:41     ` Petr Tesarik
  0 siblings, 1 reply; 12+ messages in thread
From: Petr Tesarik @ 2018-06-11  9:41 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Simon Marchi, John Baldwin, Jeff Mahoney

On Fri, 8 Jun 2018 14:34:13 +0100
Pedro Alves <palves@redhat.com> wrote:

> On 06/08/2018 01:39 PM, Petr Tesarik wrote:
> > From: Petr Tesarik <petr@tesarici.cz>
> > 
> > Implement an "-o" option for symbol-file and add-symbol-file.  
> 
> Seems like this is missing gdb/NEWS entries for both the new
> -o options, and for making the address argument of add-symbol-file
> optional.

Oh, I did not think about the NEWS file.

> Note that the de facto standard in gdb is for each commit to
> have its own separate ChangeLog entry with date/author header instead
> of appending to the previous entry.

That's what Emacs' add-change-log-entry did by default. I'll go find
out I can force it to start a new entry if the same author makes another
change on the same day...

Anyway, thank you for all the feedback!

Petr T

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/4] Allow loading symbol files with an offset
  2018-06-11  9:41   ` Petr Tesarik
@ 2018-06-11 18:41     ` Petr Tesarik
  2018-06-11 19:10       ` Eli Zaretskii
  0 siblings, 1 reply; 12+ messages in thread
From: Petr Tesarik @ 2018-06-11 18:41 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Simon Marchi, John Baldwin

On Mon, 11 Jun 2018 11:41:43 +0200
Petr Tesarik <ptesarik@suse.cz> wrote:

> On Fri, 8 Jun 2018 14:34:13 +0100
> Pedro Alves <palves@redhat.com> wrote:
>[...]
> > Note that the de facto standard in gdb is for each commit to
> > have its own separate ChangeLog entry with date/author header instead
> > of appending to the previous entry.  
> 
> That's what Emacs' add-change-log-entry did by default. I'll go find
> out I can force it to start a new entry if the same author makes another
> change on the same day...

For the record, the corresponding variable is called
add-log-always-start-new-record and should be set to t. Since GDB
sources already include a .dir-locals.el, I wonder if it is worth a
patch...

Petr T

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/4] Allow loading symbol files with an offset
  2018-06-11 18:41     ` Petr Tesarik
@ 2018-06-11 19:10       ` Eli Zaretskii
  0 siblings, 0 replies; 12+ messages in thread
From: Eli Zaretskii @ 2018-06-11 19:10 UTC (permalink / raw)
  To: Petr Tesarik; +Cc: palves, gdb-patches, simon.marchi, jhb

> Date: Mon, 11 Jun 2018 20:41:30 +0200
> From: Petr Tesarik <ptesarik@suse.cz>
> Cc: gdb-patches@sourceware.org, Simon Marchi <simon.marchi@polymtl.ca>, John Baldwin <jhb@freebsd.org>
> 
> On Mon, 11 Jun 2018 11:41:43 +0200
> For the record, the corresponding variable is called
> add-log-always-start-new-record and should be set to t. Since GDB
> sources already include a .dir-locals.el, I wonder if it is worth a
> patch...

IMO, feel free.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2018-06-11 19:10 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-08 12:40 [PATCH 0/4] Allow loading symbol files with an offset Petr Tesarik
2018-06-08 12:40 ` [PATCH 1/4] Add an optional offset option to the "symbol-file" command Petr Tesarik
2018-06-08 12:59   ` Tom Tromey
2018-06-08 13:09   ` Eli Zaretskii
2018-06-08 12:40 ` [PATCH 3/4] Make sure that sorting does not change section order Petr Tesarik
2018-06-08 12:40 ` [PATCH 4/4] Add an optional offset option to the "add-symbol-file" command Petr Tesarik
2018-06-08 12:53 ` [PATCH 2/4] Make add-symbol-file's address argument optional Petr Tesarik
2018-06-08 13:11   ` Eli Zaretskii
2018-06-08 13:34 ` [PATCH 0/4] Allow loading symbol files with an offset Pedro Alves
2018-06-11  9:41   ` Petr Tesarik
2018-06-11 18:41     ` Petr Tesarik
2018-06-11 19:10       ` Eli Zaretskii

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).