home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / amigaread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-09  |  8.2 KB  |  257 lines

  1. /* Read Amigados object files for GDB.
  2.    Copyright 1991, 1992, 1994 Free Software Foundation, Inc.
  3.    Converted to Amigados format files by Leonard Norrgard.  Original
  4.    was paread.c by Fred Fish.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "bfd.h"
  24. #include <time.h> /* For time_t in libbfd.h.  */
  25. #include <sys/types.h> /* For time_t, if not in time.h.  */
  26. #include "libbfd.h"
  27. #include "libamiga.h"
  28. #include "symtab.h"
  29. #include "symfile.h"
  30. #include "objfiles.h"
  31. #include "buildsym.h"
  32. #include "stabsread.h"
  33. #include "gdb-stabs.h"
  34. #include "complaints.h"
  35. #include <string.h>
  36. #include "demangle.h"
  37. #include <sys/file.h>
  38.  
  39. #include "aout/aout64.h"
  40.  
  41. /* Various things we might complain about... */
  42.  
  43. static void
  44. amiga_symfile_init PARAMS ((struct objfile *));
  45.  
  46. static void
  47. amiga_new_init PARAMS ((struct objfile *));
  48.  
  49. extern void
  50. amiga_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
  51.  
  52. static void
  53. amiga_symfile_finish PARAMS ((struct objfile *));
  54.  
  55. static void
  56. free_amigainfo PARAMS ((PTR));
  57.  
  58. static struct section_offsets *
  59. amiga_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
  60.  
  61. static void
  62. record_minimal_symbol PARAMS ((char *, CORE_ADDR,
  63.                    enum minimal_symbol_type,
  64.                    struct objfile *));
  65.  
  66. static void
  67. record_minimal_symbol (name, address, ms_type, objfile)
  68.      char *name;
  69.      CORE_ADDR address;
  70.      enum minimal_symbol_type ms_type;
  71.      struct objfile *objfile;
  72. {
  73.   name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
  74.   prim_record_minimal_symbol (name, address, ms_type, objfile);
  75. }
  76.  
  77. /* This cleans up the objfile's sym_stab_info pointer, and the chain of
  78.    stab_section_info's, that might be dangling from it.  */
  79.  
  80. static void
  81. free_amigainfo (objp)
  82.      PTR objp;
  83. {
  84.   struct objfile *objfile = (struct objfile *)objp;
  85.   struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *)
  86.                      objfile->sym_stab_info;
  87.   struct stab_section_info *ssi, *nssi;
  88.  
  89.   ssi = dbxinfo->stab_section_info;
  90.   while (ssi)
  91.     {
  92.       nssi = ssi->next;
  93.       mfree (objfile->md, ssi);
  94.       ssi = nssi;
  95.     }
  96.  
  97.   dbxinfo->stab_section_info = 0;    /* Just say No mo info about this.  */
  98. }
  99.  
  100. /* Initialize anything that needs initializing when a completely new symbol
  101.    file is specified (not just adding some symbols from another file, e.g. a
  102.    shared library).
  103.  
  104.    We reinitialize buildsym, since we may be reading stabs from an amigados file.  */
  105.  
  106. static void
  107. amiga_new_init (ignore)
  108.      struct objfile *ignore;
  109. {
  110.   stabsread_new_init ();
  111.   buildsym_new_init ();
  112. }
  113.  
  114. /* Perform any local cleanups required when we are done with a particular
  115.    objfile.  I.E, we are in the process of discarding all symbol information
  116.    for an objfile, freeing up all memory held for it, and unlinking the
  117.    objfile struct from the global list of known objfiles. */
  118.  
  119. static void
  120. amiga_symfile_finish (objfile)
  121.      struct objfile *objfile;
  122. {
  123.   if (objfile -> sym_stab_info != NULL)
  124.     {
  125.       mfree (objfile -> md, objfile -> sym_stab_info);
  126.     }
  127. }
  128.  
  129. /* Amigados specific initialization routine for reading symbols.
  130.  
  131.    It is passed a pointer to a struct sym_fns which contains, among other
  132.    things, the BFD for the file whose symbols are being read, and a slot for
  133.    a pointer to "private data" which we can fill with goodies.
  134.  
  135.    This routine is almost a complete ripoff of dbx_symfile_init.  The
  136.    common parts of these routines should be extracted and used instead of
  137.    duplicating this code.  FIXME. */
  138.  
  139. static void
  140. amiga_symfile_init (objfile)
  141.      struct objfile *objfile;
  142. {
  143.   int val;
  144.   bfd *sym_bfd = objfile->obfd;
  145.   char *name = bfd_get_filename (sym_bfd);
  146.  
  147.   /* Allocate struct to keep track of the symfile */
  148.   objfile->sym_stab_info = (PTR)
  149.     xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
  150.   memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
  151.  
  152.   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
  153. #define    STRING_TABLE_OFFSET    adata(sym_bfd).str_filepos
  154. #define    SYMBOL_TABLE_OFFSET    adata(sym_bfd).sym_filepos
  155.  
  156.   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
  157.  
  158.   DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
  159.   DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
  160.   if (!DBX_TEXT_SECT (objfile))
  161.     error ("Can't find .text section in symbol file");
  162.  
  163.   /* FIXME: I suspect this should be external_nlist.  The size of host
  164.      types like long and bfd_vma should not affect how we read the
  165.      file.  */
  166.   DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist);
  167.   DBX_SYMCOUNT (objfile) = AMIGA_DATA(sym_bfd)->symtab_size
  168.     / DBX_SYMBOL_SIZE (objfile);
  169.   DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
  170.  
  171.   /* Read the string table and stash it away in the psymbol_obstack.  It is
  172.      only needed as long as we need to expand psymbols into full symbols,
  173.      so when we blow away the psymbol the string table goes away as well.
  174.      Note that gdb used to use the results of attempting to malloc the
  175.      string table, based on the size it read, as a form of sanity check
  176.      for botched byte swapping, on the theory that a byte swapped string
  177.      table size would be so totally bogus that the malloc would fail.  Now
  178.      that we put in on the psymbol_obstack, we can't do this since gdb gets
  179.      a fatal error (out of virtual memory) if the size is bogus.  We can
  180.      however at least check to see if the size is zero or some negative
  181.      value. */
  182.  
  183.   DBX_STRINGTAB_SIZE (objfile) = AMIGA_DATA (sym_bfd)->stringtab_size;
  184.  
  185.   if (DBX_SYMCOUNT (objfile) == 0
  186.       || DBX_STRINGTAB_SIZE (objfile) == 0)
  187.     return;
  188.  
  189.   if (DBX_STRINGTAB_SIZE (objfile) <= 0
  190.       || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
  191.     error ("ridiculous string table size (%d bytes).",
  192.        DBX_STRINGTAB_SIZE (objfile));
  193.  
  194.   DBX_STRINGTAB (objfile) =
  195.     (char *) obstack_alloc (&objfile -> psymbol_obstack,
  196.                 DBX_STRINGTAB_SIZE (objfile));
  197.  
  198.   /* Now read in the string table in one big gulp.  */
  199.  
  200.   val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
  201.   if (val < 0)
  202.     perror_with_name (name);
  203.   val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
  204.           sym_bfd);
  205.   if (val == 0)
  206.     error ("End of file reading string table");
  207.   else if (val < 0)
  208.     /* It's possible bfd_read should be setting bfd_error, and we should be
  209.        checking that.  But currently it doesn't set bfd_error.  */
  210.     perror_with_name (name);
  211.   else if (val != DBX_STRINGTAB_SIZE (objfile))
  212.     error ("Short read reading string table");
  213. }
  214.  
  215. /* Amiga specific parsing routine for section offsets.
  216.  
  217.    Plain and simple for now.  */
  218.  
  219. static struct section_offsets *
  220. amiga_symfile_offsets (objfile, addr)
  221.      struct objfile *objfile;
  222.      CORE_ADDR addr;
  223. {
  224.   struct section_offsets *section_offsets;
  225.   int i;
  226.  
  227.   objfile->num_sections = SECT_OFF_MAX;
  228.   section_offsets = (struct section_offsets *)
  229.     obstack_alloc (&objfile -> psymbol_obstack,
  230.            sizeof (struct section_offsets)
  231.            + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
  232.  
  233.   for (i = 0; i < SECT_OFF_MAX; i++)
  234.     ANOFFSET (section_offsets, i) = addr;
  235.  
  236.   return section_offsets;
  237. }
  238.  
  239. /* Register that we are able to handle Amigados object file formats.  */
  240.  
  241. static struct sym_fns amiga_sym_fns =
  242. {
  243.   bfd_target_amiga_flavour,
  244.   amiga_new_init,    /* sym_new_init: init anything gbl to entire symtab */
  245.   amiga_symfile_init,    /* sym_init: read initial info, setup for sym_read() */
  246.   amiga_symfile_read,    /* sym_read: read a symbol file into symtab */
  247.   amiga_symfile_finish,    /* sym_finish: finished with file, cleanup */
  248.   amiga_symfile_offsets,/* sym_offsets:  Translate ext. to int. relocation */
  249.   NULL            /* next: pointer to next struct sym_fns */
  250. };
  251.  
  252. void
  253. _initialize_amigaread ()
  254. {
  255.   add_symtab_fns (&amiga_sym_fns);
  256. }
  257.