gdb/symfile-mem.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Reading symbol files from memory.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* This file defines functions (and commands to exercise those
  15.    functions) for reading debugging information from object files
  16.    whose images are mapped directly into the inferior's memory.  For
  17.    example, the Linux kernel maps a "syscall DSO" into each process's
  18.    address space; this DSO provides kernel-specific code for some
  19.    system calls.

  20.    At the moment, BFD only has functions for parsing object files from
  21.    memory for the ELF format, even though the general idea isn't
  22.    ELF-specific.  This means that BFD only provides the functions GDB
  23.    needs when configured for ELF-based targets.  So these functions
  24.    may only be compiled on ELF-based targets.

  25.    GDB has no idea whether it has been configured for an ELF-based
  26.    target or not: it just tries to handle whatever files it is given.
  27.    But this means there are no preprocessor symbols on which we could
  28.    make these functions' compilation conditional.

  29.    So, for the time being, we put these functions alone in this file,
  30.    and have .mt files reference them as appropriate.  In the future, I
  31.    hope BFD will provide a format-independent bfd_from_remote_memory
  32.    entry point.  */


  33. #include "defs.h"
  34. #include "symtab.h"
  35. #include "gdbcore.h"
  36. #include "objfiles.h"
  37. #include "gdbcmd.h"
  38. #include "target.h"
  39. #include "value.h"
  40. #include "symfile.h"
  41. #include "observer.h"
  42. #include "auxv.h"
  43. #include "elf/common.h"
  44. #include "gdb_bfd.h"

  45. /* Verify parameters of target_read_memory_bfd and target_read_memory are
  46.    compatible.  */

  47. gdb_static_assert (sizeof (CORE_ADDR) == sizeof (bfd_vma));
  48. gdb_static_assert (sizeof (gdb_byte) == sizeof (bfd_byte));
  49. gdb_static_assert (sizeof (ssize_t) <= sizeof (bfd_size_type));

  50. /* Provide bfd/ compatible prototype for target_read_memory.  Casting would not
  51.    be enough as LEN width may differ.  */

  52. static int
  53. target_read_memory_bfd (bfd_vma memaddr, bfd_byte *myaddr, bfd_size_type len)
  54. {
  55.   /* MYADDR must be already allocated for the LEN size so it has to fit in
  56.      ssize_t.  */
  57.   gdb_assert ((ssize_t) len == len);

  58.   return target_read_memory (memaddr, myaddr, len);
  59. }

  60. /* Read inferior memory at ADDR to find the header of a loaded object file
  61.    and read its in-core symbols out of inferior memory.  SIZE, if
  62.    non-zero, is the known size of the object.  TEMPL is a bfd
  63.    representing the target's format.  NAME is the name to use for this
  64.    symbol file in messages; it can be NULL or a malloc-allocated string
  65.    which will be attached to the BFD.  */
  66. static struct objfile *
  67. symbol_file_add_from_memory (struct bfd *templ, CORE_ADDR addr,
  68.                              size_t size, char *name, int from_tty)
  69. {
  70.   struct objfile *objf;
  71.   struct bfd *nbfd;
  72.   struct bfd_section *sec;
  73.   bfd_vma loadbase;
  74.   struct section_addr_info *sai;
  75.   unsigned int i;
  76.   struct cleanup *cleanup;

  77.   if (bfd_get_flavour (templ) != bfd_target_elf_flavour)
  78.     error (_("add-symbol-file-from-memory not supported for this target"));

  79.   nbfd = bfd_elf_bfd_from_remote_memory (templ, addr, size, &loadbase,
  80.                                          target_read_memory_bfd);
  81.   if (nbfd == NULL)
  82.     error (_("Failed to read a valid object file image from memory."));

  83.   gdb_bfd_ref (nbfd);
  84.   xfree (bfd_get_filename (nbfd));
  85.   if (name == NULL)
  86.     nbfd->filename = xstrdup ("shared object read from target memory");
  87.   else
  88.     nbfd->filename = name;

  89.   cleanup = make_cleanup_bfd_unref (nbfd);

  90.   if (!bfd_check_format (nbfd, bfd_object))
  91.     error (_("Got object file from memory but can't read symbols: %s."),
  92.            bfd_errmsg (bfd_get_error ()));

  93.   sai = alloc_section_addr_info (bfd_count_sections (nbfd));
  94.   make_cleanup (xfree, sai);
  95.   i = 0;
  96.   for (sec = nbfd->sections; sec != NULL; sec = sec->next)
  97.     if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
  98.       {
  99.         sai->other[i].addr = bfd_get_section_vma (nbfd, sec) + loadbase;
  100.         sai->other[i].name = (char *) bfd_get_section_name (nbfd, sec);
  101.         sai->other[i].sectindex = sec->index;
  102.         ++i;
  103.       }
  104.   sai->num_sections = i;

  105.   objf = symbol_file_add_from_bfd (nbfd, bfd_get_filename (nbfd),
  106.                                    from_tty ? SYMFILE_VERBOSE : 0,
  107.                                    sai, OBJF_SHARED, NULL);

  108.   add_target_sections_of_objfile (objf);

  109.   /* This might change our ideas about frames already looked at.  */
  110.   reinit_frame_cache ();

  111.   do_cleanups (cleanup);
  112.   return objf;
  113. }


  114. static void
  115. add_symbol_file_from_memory_command (char *args, int from_tty)
  116. {
  117.   CORE_ADDR addr;
  118.   struct bfd *templ;

  119.   if (args == NULL)
  120.     error (_("add-symbol-file-from-memory requires an expression argument"));

  121.   addr = parse_and_eval_address (args);

  122.   /* We need some representative bfd to know the target we are looking at.  */
  123.   if (symfile_objfile != NULL)
  124.     templ = symfile_objfile->obfd;
  125.   else
  126.     templ = exec_bfd;
  127.   if (templ == NULL)
  128.     error (_("Must use symbol-file or exec-file "
  129.              "before add-symbol-file-from-memory."));

  130.   symbol_file_add_from_memory (templ, addr, 0, NULL, from_tty);
  131. }

  132. /* Arguments for symbol_file_add_from_memory_wrapper.  */

  133. struct symbol_file_add_from_memory_args
  134. {
  135.   struct bfd *bfd;
  136.   CORE_ADDR sysinfo_ehdr;
  137.   size_t size;
  138.   char *name;
  139.   int from_tty;
  140. };

  141. /* Wrapper function for symbol_file_add_from_memory, for
  142.    catch_exceptions.  */

  143. static int
  144. symbol_file_add_from_memory_wrapper (struct ui_out *uiout, void *data)
  145. {
  146.   struct symbol_file_add_from_memory_args *args = data;

  147.   symbol_file_add_from_memory (args->bfd, args->sysinfo_ehdr, args->size,
  148.                                args->name, args->from_tty);
  149.   return 0;
  150. }

  151. /* Try to add the symbols for the vsyscall page, if there is one.
  152.    This function is called via the inferior_created observer.  */

  153. static void
  154. add_vsyscall_page (struct target_ops *target, int from_tty)
  155. {
  156.   struct mem_range vsyscall_range;

  157.   if (gdbarch_vsyscall_range (target_gdbarch (), &vsyscall_range))
  158.     {
  159.       struct bfd *bfd;
  160.       struct symbol_file_add_from_memory_args args;

  161.       if (core_bfd != NULL)
  162.         bfd = core_bfd;
  163.       else if (exec_bfd != NULL)
  164.         bfd = exec_bfd;
  165.       else
  166.        /* FIXME: cagney/2004-05-06: Should not require an existing
  167.           BFD when trying to create a run-time BFD of the VSYSCALL
  168.           page in the inferior.  Unfortunately that's the current
  169.           interface so for the moment bail.  Introducing a
  170.           ``bfd_runtime'' (a BFD created using the loaded image) file
  171.           format should fix this.  */
  172.         {
  173.           warning (_("Could not load vsyscall page "
  174.                      "because no executable was specified\n"
  175.                      "try using the \"file\" command first."));
  176.           return;
  177.         }
  178.       args.bfd = bfd;
  179.       args.sysinfo_ehdr = vsyscall_range.start;
  180.       args.size = vsyscall_range.length;

  181.       args.name = xstrprintf ("system-supplied DSO at %s",
  182.                               paddress (target_gdbarch (), vsyscall_range.start));
  183.       /* Pass zero for FROM_TTY, because the action of loading the
  184.          vsyscall DSO was not triggered by the user, even if the user
  185.          typed "run" at the TTY.  */
  186.       args.from_tty = 0;
  187.       catch_exceptions (current_uiout, symbol_file_add_from_memory_wrapper,
  188.                         &args, RETURN_MASK_ALL);
  189.     }
  190. }



  191. /* Provide a prototype to silence -Wmissing-prototypes.  */
  192. extern initialize_file_ftype _initialize_symfile_mem;

  193. void
  194. _initialize_symfile_mem (void)
  195. {
  196.   add_cmd ("add-symbol-file-from-memory", class_files,
  197.            add_symbol_file_from_memory_command,
  198.            _("Load the symbols out of memory from a "
  199.              "dynamically loaded object file.\n"
  200.              "Give an expression for the address "
  201.              "of the file's shared object file header."),
  202.            &cmdlist);

  203.   /* Want to know of each new inferior so that its vsyscall info can
  204.      be extracted.  */
  205.   observer_attach_inferior_created (add_vsyscall_page);
  206. }