Add an optional offset option to the "symbol-file" command
authorPetr Tesarik <ptesarik@suse.cz>
Thu, 28 Jun 2018 06:01:33 +0000 (08:01 +0200)
committerPetr Tesarik <ptesarik@suse.cz>
Thu, 28 Jun 2018 06:35:34 +0000 (08:35 +0200)
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-28  Petr Tesarik  <ptesarik@suse.cz>

* 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-28  Petr Tesarik  <ptesarik@suse.cz>

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

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

* gdb.base/relocate.exp: Add test for "symbol-file -o ".

gdb/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/symfile.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/relocate.exp

index 0b39f91670eb8c37fd345e234915388ea58f83e3..4bc320440737bcc131f3dfe26d71501c39517642 100644 (file)
@@ -1,3 +1,9 @@
+2018-06-28  Petr Tesarik  <ptesarik@suse.cz>
+
+       * 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-28  Petr Tesarik  <ptesarik@suse.cz>
 
        * MAINTAINERS (Write After Approval): Add Petr Tesarik.
index 13da2f1d4e99d8f62c951b6c925998a1ecb812c7..101746567acefd344111713bb54fc0b498ff1524 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,9 @@
 
 *** Changes since GDB 8.1
 
+* The 'symbol-file' command now accepts an '-o' option to add a relative
+  offset to all sections.
+
 * The endianness used with the 'set endian auto' mode in the absence of
   an executable selected for debugging is now the last endianness chosen
   either by one of the 'set endian big' and 'set endian little' commands
index ade1799b06c963361e60f1fac9a07508d347548b..f31287384c3a4e77cfa763805c937821991b2598 100644 (file)
@@ -1,3 +1,7 @@
+2018-06-28  Petr Tesarik  <ptesarik@suse.cz>
+
+       * gdb.texinfo (Files): Document "symbol-file -o offset".
+
 2018-06-14  Tom de Vries  <tdevries@suse.de>
 
        * gdb.texinfo (Background Execution): Add @cindex for '&'.
index a6bad13d9d423d3364f7aea2b23e714f2030ca65..328256236e45cd28c49bc1708b02968775997540 100644 (file)
@@ -18823,11 +18823,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{[} @var{filename} @r{[} -o @var{offset} @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.
 
index f8177ea8b10fb5e2b35790ba8d8a2105d9a5d282..461f60d0748cb2783f33dae740547c0f47a42f86 100644 (file)
@@ -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);
index edebc81a39c1ee4b99070e4d6887567aead8241f..cea52c78cafdecc107c0de39b22cb04ffe8281e0 100644 (file)
@@ -1,3 +1,7 @@
+2018-06-28  Petr Tesarik  <ptesarik@suse.cz>
+
+       * gdb.base/relocate.exp: Add test for "symbol-file -o ".
+
 2018-06-27  Omair Javaid  <omair.javaid@linaro.org>
 
        PR gdb/21695
index 89f2fffcd9c8fd566fd681e9a80ba93e6485823f..77f6a8815939e5be429208f67522d2ef1ad08af5 100644 (file)
@@ -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.
 
This page took 0.064098 seconds and 4 git commands to generate.