Patchwork [4/4] Add an optional offset option to the "add-symbol-file" command

login
register
mail settings
Submitter Petr Tesarik
Date June 8, 2018, 12:40 p.m.
Message ID <20180608124000.10668-5-ptesarik@suse.cz>
Download mbox | patch
Permalink /patch/27714/
State New
Headers show

Comments

Petr Tesarik - June 8, 2018, 12:40 p.m.
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(-)

Patch

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.