GDB (API)
/home/stan/gdb/src/gdb/symfile-mem.c
Go to the documentation of this file.
00001 /* Reading symbol files from memory.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 /* This file defines functions (and commands to exercise those
00021    functions) for reading debugging information from object files
00022    whose images are mapped directly into the inferior's memory.  For
00023    example, the Linux kernel maps a "syscall DSO" into each process's
00024    address space; this DSO provides kernel-specific code for some
00025    system calls.
00026 
00027    At the moment, BFD only has functions for parsing object files from
00028    memory for the ELF format, even though the general idea isn't
00029    ELF-specific.  This means that BFD only provides the functions GDB
00030    needs when configured for ELF-based targets.  So these functions
00031    may only be compiled on ELF-based targets.
00032 
00033    GDB has no idea whether it has been configured for an ELF-based
00034    target or not: it just tries to handle whatever files it is given.
00035    But this means there are no preprocessor symbols on which we could
00036    make these functions' compilation conditional.
00037 
00038    So, for the time being, we put these functions alone in this file,
00039    and have .mt files reference them as appropriate.  In the future, I
00040    hope BFD will provide a format-independent bfd_from_remote_memory
00041    entry point.  */
00042 
00043 
00044 #include "defs.h"
00045 #include "symtab.h"
00046 #include "gdbcore.h"
00047 #include "objfiles.h"
00048 #include "exceptions.h"
00049 #include "gdbcmd.h"
00050 #include "target.h"
00051 #include "value.h"
00052 #include "symfile.h"
00053 #include "observer.h"
00054 #include "auxv.h"
00055 #include "elf/common.h"
00056 #include "gdb_bfd.h"
00057 
00058 /* Verify parameters of target_read_memory_bfd and target_read_memory are
00059    compatible.  */
00060 
00061 gdb_static_assert (sizeof (CORE_ADDR) == sizeof (bfd_vma));
00062 gdb_static_assert (sizeof (gdb_byte) == sizeof (bfd_byte));
00063 gdb_static_assert (sizeof (ssize_t) <= sizeof (bfd_size_type));
00064 
00065 /* Provide bfd/ compatible prototype for target_read_memory.  Casting would not
00066    be enough as LEN width may differ.  */
00067 
00068 static int
00069 target_read_memory_bfd (bfd_vma memaddr, bfd_byte *myaddr, bfd_size_type len)
00070 {
00071   /* MYADDR must be already allocated for the LEN size so it has to fit in
00072      ssize_t.  */
00073   gdb_assert ((ssize_t) len == len);
00074 
00075   return target_read_memory (memaddr, myaddr, len);
00076 }
00077 
00078 /* Read inferior memory at ADDR to find the header of a loaded object file
00079    and read its in-core symbols out of inferior memory.  TEMPL is a bfd
00080    representing the target's format.  NAME is the name to use for this
00081    symbol file in messages; it can be NULL or a malloc-allocated string
00082    which will be attached to the BFD.  */
00083 static struct objfile *
00084 symbol_file_add_from_memory (struct bfd *templ, CORE_ADDR addr, char *name,
00085                              int from_tty)
00086 {
00087   struct objfile *objf;
00088   struct bfd *nbfd;
00089   struct bfd_section *sec;
00090   bfd_vma loadbase;
00091   struct section_addr_info *sai;
00092   unsigned int i;
00093   struct cleanup *cleanup;
00094 
00095   if (bfd_get_flavour (templ) != bfd_target_elf_flavour)
00096     error (_("add-symbol-file-from-memory not supported for this target"));
00097 
00098   nbfd = bfd_elf_bfd_from_remote_memory (templ, addr, &loadbase,
00099                                          target_read_memory_bfd);
00100   if (nbfd == NULL)
00101     error (_("Failed to read a valid object file image from memory."));
00102 
00103   gdb_bfd_ref (nbfd);
00104   if (name == NULL)
00105     nbfd->filename = "shared object read from target memory";
00106   else
00107     {
00108       nbfd->filename = name;
00109       gdb_bfd_stash_filename (nbfd);
00110       xfree (name);
00111     }
00112 
00113   cleanup = make_cleanup_bfd_unref (nbfd);
00114 
00115   if (!bfd_check_format (nbfd, bfd_object))
00116     error (_("Got object file from memory but can't read symbols: %s."),
00117            bfd_errmsg (bfd_get_error ()));
00118 
00119   sai = alloc_section_addr_info (bfd_count_sections (nbfd));
00120   make_cleanup (xfree, sai);
00121   i = 0;
00122   for (sec = nbfd->sections; sec != NULL; sec = sec->next)
00123     if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
00124       {
00125         sai->other[i].addr = bfd_get_section_vma (nbfd, sec) + loadbase;
00126         sai->other[i].name = (char *) bfd_get_section_name (nbfd, sec);
00127         sai->other[i].sectindex = sec->index;
00128         ++i;
00129       }
00130   sai->num_sections = i;
00131 
00132   objf = symbol_file_add_from_bfd (nbfd, bfd_get_filename (nbfd),
00133                                    from_tty ? SYMFILE_VERBOSE : 0,
00134                                    sai, OBJF_SHARED, NULL);
00135 
00136   /* This might change our ideas about frames already looked at.  */
00137   reinit_frame_cache ();
00138 
00139   do_cleanups (cleanup);
00140   return objf;
00141 }
00142 
00143 
00144 static void
00145 add_symbol_file_from_memory_command (char *args, int from_tty)
00146 {
00147   CORE_ADDR addr;
00148   struct bfd *templ;
00149 
00150   if (args == NULL)
00151     error (_("add-symbol-file-from-memory requires an expression argument"));
00152 
00153   addr = parse_and_eval_address (args);
00154 
00155   /* We need some representative bfd to know the target we are looking at.  */
00156   if (symfile_objfile != NULL)
00157     templ = symfile_objfile->obfd;
00158   else
00159     templ = exec_bfd;
00160   if (templ == NULL)
00161     error (_("Must use symbol-file or exec-file "
00162              "before add-symbol-file-from-memory."));
00163 
00164   symbol_file_add_from_memory (templ, addr, NULL, from_tty);
00165 }
00166 
00167 /* Arguments for symbol_file_add_from_memory_wrapper.  */
00168 
00169 struct symbol_file_add_from_memory_args
00170 {
00171   struct bfd *bfd;
00172   CORE_ADDR sysinfo_ehdr;
00173   char *name;
00174   int from_tty;
00175 };
00176 
00177 /* Wrapper function for symbol_file_add_from_memory, for
00178    catch_exceptions.  */
00179 
00180 static int
00181 symbol_file_add_from_memory_wrapper (struct ui_out *uiout, void *data)
00182 {
00183   struct symbol_file_add_from_memory_args *args = data;
00184 
00185   symbol_file_add_from_memory (args->bfd, args->sysinfo_ehdr, args->name,
00186                                args->from_tty);
00187   return 0;
00188 }
00189 
00190 /* Try to add the symbols for the vsyscall page, if there is one.
00191    This function is called via the inferior_created observer.  */
00192 
00193 static void
00194 add_vsyscall_page (struct target_ops *target, int from_tty)
00195 {
00196   CORE_ADDR sysinfo_ehdr;
00197 
00198   if (target_auxv_search (target, AT_SYSINFO_EHDR, &sysinfo_ehdr) > 0
00199       && sysinfo_ehdr != (CORE_ADDR) 0)
00200     {
00201       struct bfd *bfd;
00202       struct symbol_file_add_from_memory_args args;
00203 
00204       if (core_bfd != NULL)
00205         bfd = core_bfd;
00206       else if (exec_bfd != NULL)
00207         bfd = exec_bfd;
00208       else
00209        /* FIXME: cagney/2004-05-06: Should not require an existing
00210           BFD when trying to create a run-time BFD of the VSYSCALL
00211           page in the inferior.  Unfortunately that's the current
00212           interface so for the moment bail.  Introducing a
00213           ``bfd_runtime'' (a BFD created using the loaded image) file
00214           format should fix this.  */
00215         {
00216           warning (_("Could not load vsyscall page "
00217                      "because no executable was specified\n"
00218                      "try using the \"file\" command first."));
00219           return;
00220         }
00221       args.bfd = bfd;
00222       args.sysinfo_ehdr = sysinfo_ehdr;
00223       args.name = xstrprintf ("system-supplied DSO at %s",
00224                               paddress (target_gdbarch (), sysinfo_ehdr));
00225       /* Pass zero for FROM_TTY, because the action of loading the
00226          vsyscall DSO was not triggered by the user, even if the user
00227          typed "run" at the TTY.  */
00228       args.from_tty = 0;
00229       catch_exceptions (current_uiout, symbol_file_add_from_memory_wrapper,
00230                         &args, RETURN_MASK_ALL);
00231     }
00232 }
00233 
00234 
00235 
00236 /* Provide a prototype to silence -Wmissing-prototypes.  */
00237 extern initialize_file_ftype _initialize_symfile_mem;
00238 
00239 void
00240 _initialize_symfile_mem (void)
00241 {
00242   add_cmd ("add-symbol-file-from-memory", class_files,
00243            add_symbol_file_from_memory_command,
00244            _("Load the symbols out of memory from a "
00245              "dynamically loaded object file.\n"
00246              "Give an expression for the address "
00247              "of the file's shared object file header."),
00248            &cmdlist);
00249 
00250   /* Want to know of each new inferior so that its vsyscall info can
00251      be extracted.  */
00252   observer_attach_inferior_created (add_vsyscall_page);
00253 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines