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