Home | History | Annotate | Line # | Download | only in gdb
      1   1.1  christos /* Read dbx symbol tables and convert to internal format, for GDB.
      2  1.11  christos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
      3   1.1  christos 
      4   1.1  christos    This file is part of GDB.
      5   1.1  christos 
      6   1.1  christos    This program is free software; you can redistribute it and/or modify
      7   1.1  christos    it under the terms of the GNU General Public License as published by
      8   1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9   1.1  christos    (at your option) any later version.
     10   1.1  christos 
     11   1.1  christos    This program is distributed in the hope that it will be useful,
     12   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14   1.1  christos    GNU General Public License for more details.
     15   1.1  christos 
     16   1.1  christos    You should have received a copy of the GNU General Public License
     17   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18   1.1  christos 
     19   1.1  christos /* This module provides three functions: dbx_symfile_init,
     20   1.1  christos    which initializes to read a symbol file; dbx_new_init, which
     21   1.1  christos    discards existing cached information when all symbols are being
     22   1.1  christos    discarded; and dbx_symfile_read, which reads a symbol table
     23   1.1  christos    from a file.
     24   1.1  christos 
     25   1.1  christos    dbx_symfile_read only does the minimum work necessary for letting the
     26   1.1  christos    user "name" things symbolically; it does not read the entire symtab.
     27   1.1  christos    Instead, it reads the external and static symbols and puts them in partial
     28   1.1  christos    symbol tables.  When more extensive information is requested of a
     29   1.1  christos    file, the corresponding partial symbol table is mutated into a full
     30   1.1  christos    fledged symbol table by going back and reading the symbols
     31   1.1  christos    for real.  dbx_psymtab_to_symtab() is the function that does this */
     32   1.1  christos 
     33   1.1  christos 
     34  1.11  christos #include "event-top.h"
     35  1.10  christos #include "gdbsupport/gdb_obstack.h"
     36   1.1  christos #include <sys/stat.h>
     37   1.1  christos #include "symtab.h"
     38   1.1  christos #include "breakpoint.h"
     39   1.1  christos #include "target.h"
     40  1.11  christos #include "gdbcore.h"
     41  1.11  christos #include "libaout.h"
     42   1.1  christos #include "filenames.h"
     43   1.1  christos #include "objfiles.h"
     44   1.8  christos #include "buildsym-legacy.h"
     45   1.1  christos #include "stabsread.h"
     46   1.1  christos #include "gdb-stabs.h"
     47   1.1  christos #include "demangle.h"
     48   1.1  christos #include "complaints.h"
     49   1.1  christos #include "cp-abi.h"
     50   1.1  christos #include "cp-support.h"
     51  1.10  christos #include "c-lang.h"
     52  1.11  christos #include "psymtab.h"
     53   1.1  christos #include "block.h"
     54   1.1  christos #include "aout/aout64.h"
     55  1.11  christos #include "aout/stab_gnu.h"
     56   1.1  christos 
     57  1.12  christos /* Required for the following registry.  */
     59   1.1  christos #include "gdb-stabs.h"
     60   1.1  christos 
     61   1.1  christos 
     62   1.1  christos 
     64   1.1  christos 
     66   1.1  christos 
     67   1.1  christos /* Local function prototypes.  */
     68   1.1  christos 
     69   1.1  christos static void dbx_symfile_init (struct objfile *);
     70   1.7  christos 
     71   1.1  christos static void dbx_new_init (struct objfile *);
     72   1.1  christos 
     73   1.1  christos static void dbx_symfile_read (struct objfile *, symfile_add_flags);
     74   1.1  christos 
     75   1.1  christos static void dbx_symfile_finish (struct objfile *);
     76   1.1  christos 
     77   1.1  christos 
     78   1.1  christos #if 0
     79   1.1  christos static struct type **
     80   1.1  christos explicit_lookup_type (int real_filenum, int index)
     81   1.1  christos {
     82   1.1  christos   struct header_file *f = &HEADER_FILES (dbxread_objfile)[real_filenum];
     83   1.1  christos 
     84   1.1  christos   if (index >= f->length)
     85   1.1  christos     {
     86   1.1  christos       f->length *= 2;
     87   1.1  christos       f->vector = (struct type **)
     88   1.1  christos 	xrealloc (f->vector, f->length * sizeof (struct type *));
     89   1.1  christos       memset (&f->vector[f->length / 2],
     90   1.1  christos 	      '\0', f->length * sizeof (struct type *) / 2);
     91   1.1  christos     }
     92   1.1  christos   return &f->vector[index];
     93   1.1  christos }
     94   1.1  christos #endif
     95   1.1  christos 
     96   1.1  christos /* Scan and build partial symbols for a symbol file.
     98   1.1  christos    We have been initialized by a call to dbx_symfile_init, which
     99   1.7  christos    put all the relevant info into a "struct dbx_symfile_info",
    100   1.1  christos    hung off the objfile structure.  */
    101  1.12  christos 
    102   1.1  christos static void
    103   1.1  christos dbx_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
    104   1.1  christos {
    105   1.1  christos   read_stabs_symtab (objfile, symfile_flags);
    106   1.1  christos }
    107   1.1  christos 
    108   1.1  christos /* Initialize anything that needs initializing when a completely new
    109   1.1  christos    symbol file is specified (not just adding some symbols from another
    110   1.1  christos    file, e.g. a shared library).  */
    111   1.1  christos 
    112   1.1  christos static void
    113   1.1  christos dbx_new_init (struct objfile *ignore)
    114   1.1  christos {
    115   1.1  christos   stabsread_new_init ();
    116   1.1  christos   init_header_files ();
    117   1.1  christos }
    118   1.1  christos 
    119   1.1  christos 
    120   1.1  christos /* dbx_symfile_init ()
    121   1.1  christos    is the dbx-specific initialization routine for reading symbols.
    122   1.1  christos    It is passed a struct objfile which contains, among other things,
    123   1.1  christos    the BFD for the file whose symbols are being read, and a slot for a pointer
    124   1.1  christos    to "private data" which we fill with goodies.
    125   1.1  christos 
    126   1.1  christos    We read the string table into malloc'd space and stash a pointer to it.
    127   1.1  christos 
    128   1.1  christos    Since BFD doesn't know how to read debug symbols in a format-independent
    129   1.1  christos    way (and may never do so...), we have to do it ourselves.  We will never
    130   1.1  christos    be called unless this is an a.out (or very similar) file.
    131   1.1  christos    FIXME, there should be a cleaner peephole into the BFD environment here.  */
    132   1.1  christos 
    133   1.1  christos #define DBX_STRINGTAB_SIZE_SIZE sizeof(long)	/* FIXME */
    134   1.1  christos 
    135  1.10  christos static void
    136   1.9  christos dbx_symfile_init (struct objfile *objfile)
    137   1.1  christos {
    138   1.1  christos   int val;
    139   1.1  christos   bfd *sym_bfd = objfile->obfd.get ();
    140   1.1  christos   const char *name = bfd_get_filename (sym_bfd);
    141   1.9  christos   asection *text_sect;
    142   1.1  christos   unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
    143   1.1  christos 
    144   1.1  christos   /* Allocate struct to keep track of the symfile.  */
    145   1.1  christos   dbx_objfile_data_key.emplace (objfile);
    146   1.1  christos 
    147   1.1  christos   DBX_TEXT_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
    148   1.1  christos   DBX_DATA_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".data");
    149   1.1  christos   DBX_BSS_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".bss");
    150   1.1  christos 
    151   1.1  christos   /* FIXME POKING INSIDE BFD DATA STRUCTURES.  */
    152   1.1  christos #define	STRING_TABLE_OFFSET	(sym_bfd->origin + obj_str_filepos (sym_bfd))
    153   1.1  christos #define	SYMBOL_TABLE_OFFSET	(sym_bfd->origin + obj_sym_filepos (sym_bfd))
    154   1.1  christos 
    155   1.1  christos   /* FIXME POKING INSIDE BFD DATA STRUCTURES.  */
    156   1.9  christos 
    157   1.9  christos   text_sect = bfd_get_section_by_name (sym_bfd, ".text");
    158   1.1  christos   if (!text_sect)
    159   1.1  christos     error (_("Can't find .text section in symbol file"));
    160   1.1  christos   DBX_TEXT_ADDR (objfile) = bfd_section_vma (text_sect);
    161   1.1  christos   DBX_TEXT_SIZE (objfile) = bfd_section_size (text_sect);
    162   1.1  christos 
    163   1.1  christos   DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
    164   1.1  christos   DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
    165   1.1  christos   DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
    166   1.1  christos 
    167   1.1  christos   /* Read the string table and stash it away in the objfile_obstack.
    168   1.1  christos      When we blow away the objfile the string table goes away as well.
    169   1.1  christos      Note that gdb used to use the results of attempting to malloc the
    170   1.1  christos      string table, based on the size it read, as a form of sanity check
    171   1.1  christos      for botched byte swapping, on the theory that a byte swapped string
    172   1.1  christos      table size would be so totally bogus that the malloc would fail.  Now
    173   1.1  christos      that we put in on the objfile_obstack, we can't do this since gdb gets
    174   1.1  christos      a fatal error (out of virtual memory) if the size is bogus.  We can
    175   1.1  christos      however at least check to see if the size is less than the size of
    176   1.1  christos      the size field itself, or larger than the size of the entire file.
    177   1.1  christos      Note that all valid string tables have a size greater than zero, since
    178   1.1  christos      the bytes used to hold the size are included in the count.  */
    179  1.10  christos 
    180  1.10  christos   if (STRING_TABLE_OFFSET == 0)
    181   1.1  christos     {
    182   1.1  christos       /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
    183   1.1  christos 	 will never be zero, even when there is no string table.  This
    184   1.1  christos 	 would appear to be a bug in bfd.  */
    185   1.1  christos       DBX_STRINGTAB_SIZE (objfile) = 0;
    186   1.1  christos       DBX_STRINGTAB (objfile) = NULL;
    187   1.1  christos     }
    188   1.1  christos   else
    189   1.1  christos     {
    190   1.1  christos       val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
    191  1.11  christos       if (val < 0)
    192   1.1  christos 	perror_with_name (name);
    193   1.1  christos 
    194   1.1  christos       memset (size_temp, 0, sizeof (size_temp));
    195   1.1  christos       val = bfd_read (size_temp, sizeof (size_temp), sym_bfd);
    196   1.1  christos       if (val < 0)
    197   1.1  christos 	{
    198   1.1  christos 	  perror_with_name (name);
    199   1.1  christos 	}
    200   1.1  christos       else if (val == 0)
    201   1.1  christos 	{
    202   1.1  christos 	  /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
    203   1.1  christos 	     EOF if there is no string table, and attempting to read the size
    204   1.1  christos 	     from EOF will read zero bytes.  */
    205   1.1  christos 	  DBX_STRINGTAB_SIZE (objfile) = 0;
    206   1.1  christos 	  DBX_STRINGTAB (objfile) = NULL;
    207   1.1  christos 	}
    208   1.1  christos       else
    209   1.1  christos 	{
    210   1.1  christos 	  /* Read some data that would appear to be the string table size.
    211   1.1  christos 	     If there really is a string table, then it is probably the right
    212   1.1  christos 	     size.  Byteswap if necessary and validate the size.  Note that
    213   1.1  christos 	     the minimum is DBX_STRINGTAB_SIZE_SIZE.  If we just read some
    214   1.1  christos 	     random data that happened to be at STRING_TABLE_OFFSET, because
    215   1.1  christos 	     bfd can't tell us there is no string table, the sanity checks may
    216   1.1  christos 	     or may not catch this.  */
    217   1.1  christos 	  DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
    218   1.1  christos 
    219   1.1  christos 	  if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
    220   1.1  christos 	      || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
    221   1.1  christos 	    error (_("ridiculous string table size (%d bytes)."),
    222   1.1  christos 		   DBX_STRINGTAB_SIZE (objfile));
    223   1.1  christos 
    224   1.1  christos 	  DBX_STRINGTAB (objfile) =
    225   1.1  christos 	    (char *) obstack_alloc (&objfile->objfile_obstack,
    226   1.1  christos 				    DBX_STRINGTAB_SIZE (objfile));
    227   1.1  christos 	  OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile));
    228   1.1  christos 
    229   1.1  christos 	  /* Now read in the string table in one big gulp.  */
    230  1.11  christos 
    231  1.11  christos 	  val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
    232  1.11  christos 	  if (val < 0)
    233   1.1  christos 	    perror_with_name (name);
    234   1.1  christos 	  val = bfd_read (DBX_STRINGTAB (objfile),
    235   1.1  christos 			  DBX_STRINGTAB_SIZE (objfile),
    236   1.1  christos 			  sym_bfd);
    237   1.1  christos 	  if (val != DBX_STRINGTAB_SIZE (objfile))
    238   1.1  christos 	    perror_with_name (name);
    239   1.1  christos 	}
    240   1.1  christos     }
    241   1.1  christos }
    242   1.1  christos 
    243   1.1  christos /* Perform any local cleanups required when we are done with a particular
    244   1.1  christos    objfile.  I.E, we are in the process of discarding all symbol information
    245   1.1  christos    for an objfile, freeing up all memory held for it, and unlinking the
    246   1.1  christos    objfile struct from the global list of known objfiles.  */
    247   1.1  christos 
    248   1.1  christos static void
    249   1.1  christos dbx_symfile_finish (struct objfile *objfile)
    250   1.1  christos {
    251   1.1  christos   free_header_files ();
    252   1.1  christos }
    253   1.1  christos 
    254   1.1  christos 
    255   1.1  christos 
    257   1.1  christos 
    258   1.1  christos 
    260   1.1  christos 
    262   1.1  christos static const struct sym_fns aout_sym_fns =
    264   1.1  christos {
    265   1.1  christos   dbx_new_init,			/* init anything gbl to entire symtab */
    266   1.1  christos   dbx_symfile_init,		/* read initial info, setup for sym_read() */
    267   1.1  christos   dbx_symfile_read,		/* read a symbol file into symtab */
    268   1.9  christos   dbx_symfile_finish,		/* finished with file, cleanup */
    269   1.1  christos   default_symfile_offsets, 	/* parse user's offsets to internal form */
    270   1.9  christos   default_symfile_segments,	/* Get segment information from a file.  */
    271   1.1  christos   NULL,
    272   1.1  christos   default_symfile_relocate,	/* Relocate a debug section.  */
    273   1.1  christos   NULL,				/* sym_probe_fns */
    274                 };
    275                 
    276                 void _initialize_dbxread ();
    277                 void
    278                 _initialize_dbxread ()
    279                 {
    280                   add_symtab_fns (bfd_target_aout_flavour, &aout_sym_fns);
    281                 }
    282