gdb/somread.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Read HP PA/Risc object files for GDB.
  2.    Copyright (C) 1991-2015 Free Software Foundation, Inc.
  3.    Written by Fred Fish at Cygnus Support.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "bfd.h"
  17. #include "som/aout.h"
  18. #include "symtab.h"
  19. #include "symfile.h"
  20. #include "objfiles.h"
  21. #include "buildsym.h"
  22. #include "stabsread.h"
  23. #include "gdb-stabs.h"
  24. #include "complaints.h"
  25. #include "demangle.h"
  26. #include "som.h"
  27. #include "libhppa.h"
  28. #include "psymtab.h"

  29. #include "solib-som.h"

  30. /* Read the symbol table of a SOM file.

  31.    Given an open bfd, a base address to relocate symbols to, and a
  32.    flag that specifies whether or not this bfd is for an executable
  33.    or not (may be shared library for example), add all the global
  34.    function and data symbols to the minimal symbol table.  */

  35. static void
  36. som_symtab_read (bfd *abfd, struct objfile *objfile,
  37.                  struct section_offsets *section_offsets)
  38. {
  39.   struct cleanup *cleanup;
  40.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  41.   unsigned int number_of_symbols;
  42.   int val, dynamic;
  43.   char *stringtab;
  44.   asection *shlib_info;
  45.   struct som_external_symbol_dictionary_record *buf, *bufp, *endbufp;
  46.   char *symname;
  47.   const int symsize = sizeof (struct som_external_symbol_dictionary_record);


  48.   number_of_symbols = bfd_get_symcount (abfd);

  49.   /* Allocate a buffer to read in the debug info.
  50.      We avoid using alloca because the memory size could be so large
  51.      that we could hit the stack size limit.  */
  52.   buf = xmalloc (symsize * number_of_symbols);
  53.   cleanup = make_cleanup (xfree, buf);
  54.   bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
  55.   val = bfd_bread (buf, symsize * number_of_symbols, abfd);
  56.   if (val != symsize * number_of_symbols)
  57.     error (_("Couldn't read symbol dictionary!"));

  58.   /* Allocate a buffer to read in the som stringtab section of
  59.      the debugging info.  Again, we avoid using alloca because
  60.      the data could be so large that we could potentially hit
  61.      the stack size limitat.  */
  62.   stringtab = xmalloc (obj_som_stringtab_size (abfd));
  63.   make_cleanup (xfree, stringtab);
  64.   bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
  65.   val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
  66.   if (val != obj_som_stringtab_size (abfd))
  67.     error (_("Can't read in HP string table."));

  68.   /* We need to determine if objfile is a dynamic executable (so we
  69.      can do the right thing for ST_ENTRY vs ST_CODE symbols).

  70.      There's nothing in the header which easily allows us to do
  71.      this.

  72.      This code used to rely upon the existence of a $SHLIB_INFO$
  73.      section to make this determination.  HP claims that it is
  74.      more accurate to check for a nonzero text offset, but they
  75.      have not provided any information about why that test is
  76.      more accurate.  */
  77.   dynamic = (ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile)) != 0);

  78.   endbufp = buf + number_of_symbols;
  79.   for (bufp = buf; bufp < endbufp; ++bufp)
  80.     {
  81.       enum minimal_symbol_type ms_type;
  82.       unsigned int flags = bfd_getb32 (bufp->flags);
  83.       unsigned int symbol_type
  84.         = (flags >> SOM_SYMBOL_TYPE_SH) & SOM_SYMBOL_TYPE_MASK;
  85.       unsigned int symbol_scope
  86.         = (flags >> SOM_SYMBOL_SCOPE_SH) & SOM_SYMBOL_SCOPE_MASK;
  87.       CORE_ADDR symbol_value = bfd_getb32 (bufp->symbol_value);
  88.       asection *section = NULL;

  89.       QUIT;

  90.       /* Compute the section.  */
  91.       switch (symbol_scope)
  92.         {
  93.         case SS_EXTERNAL:
  94.           if (symbol_type != ST_STORAGE)
  95.             section = bfd_und_section_ptr;
  96.           else
  97.             section = bfd_com_section_ptr;
  98.           break;

  99.         case SS_UNSAT:
  100.           if (symbol_type != ST_STORAGE)
  101.             section = bfd_und_section_ptr;
  102.           else
  103.             section = bfd_com_section_ptr;
  104.           break;

  105.         case SS_UNIVERSAL:
  106.           section = bfd_section_from_som_symbol (abfd, bufp);
  107.           break;

  108.         case SS_LOCAL:
  109.           section = bfd_section_from_som_symbol (abfd, bufp);
  110.           break;
  111.         }

  112.       switch (symbol_scope)
  113.         {
  114.         case SS_UNIVERSAL:
  115.         case SS_EXTERNAL:
  116.           switch (symbol_type)
  117.             {
  118.             case ST_SYM_EXT:
  119.             case ST_ARG_EXT:
  120.               continue;

  121.             case ST_CODE:
  122.             case ST_PRI_PROG:
  123.             case ST_SEC_PROG:
  124.             case ST_MILLICODE:
  125.               symname = bfd_getb32 (bufp->name) + stringtab;
  126.               ms_type = mst_text;
  127.               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
  128.               break;

  129.             case ST_ENTRY:
  130.               symname = bfd_getb32 (bufp->name) + stringtab;
  131.               /* For a dynamic executable, ST_ENTRY symbols are
  132.                  the stubs, while the ST_CODE symbol is the real
  133.                  function.  */
  134.               if (dynamic)
  135.                 ms_type = mst_solib_trampoline;
  136.               else
  137.                 ms_type = mst_text;
  138.               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
  139.               break;

  140.             case ST_STUB:
  141.               symname = bfd_getb32 (bufp->name) + stringtab;
  142.               ms_type = mst_solib_trampoline;
  143.               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
  144.               break;

  145.             case ST_DATA:
  146.               symname = bfd_getb32 (bufp->name) + stringtab;
  147.               ms_type = mst_data;
  148.               break;
  149.             default:
  150.               continue;
  151.             }
  152.           break;

  153. #if 0
  154.           /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
  155.         case SS_GLOBAL:
  156. #endif
  157.         case SS_LOCAL:
  158.           switch (symbol_type)
  159.             {
  160.             case ST_SYM_EXT:
  161.             case ST_ARG_EXT:
  162.               continue;

  163.             case ST_CODE:
  164.               symname = bfd_getb32 (bufp->name) + stringtab;
  165.               ms_type = mst_file_text;
  166.               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);

  167.             check_strange_names:
  168.               /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
  169.                  label prefixes for stabs, constant data, etc.  So we need
  170.                  only filter out L$ symbols which are left in due to
  171.                  limitations in how GAS generates SOM relocations.

  172.                  When linking in the HPUX C-library the HP linker has
  173.                  the nasty habit of placing section symbols from the literal
  174.                  subspaces in the middle of the program's text.  Filter
  175.                  those out as best we can.  Check for first and last character
  176.                  being '$'.

  177.                  And finally, the newer HP compilers emit crud like $PIC_foo$N
  178.                  in some circumstance (PIC code I guess).  It's also claimed
  179.                  that they emit D$ symbols too.  What stupidity.  */
  180.               if ((symname[0] == 'L' && symname[1] == '$')
  181.               || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
  182.                   || (symname[0] == 'D' && symname[1] == '$')
  183.                   || (strncmp (symname, "L0\001", 3) == 0)
  184.                   || (strncmp (symname, "$PIC", 4) == 0))
  185.                 continue;
  186.               break;

  187.             case ST_PRI_PROG:
  188.             case ST_SEC_PROG:
  189.             case ST_MILLICODE:
  190.               symname = bfd_getb32 (bufp->name) + stringtab;
  191.               ms_type = mst_file_text;
  192.               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
  193.               break;

  194.             case ST_ENTRY:
  195.               symname = bfd_getb32 (bufp->name) + stringtab;
  196.               /* SS_LOCAL symbols in a shared library do not have
  197.                  export stubs, so we do not have to worry about
  198.                  using mst_file_text vs mst_solib_trampoline here like
  199.                  we do for SS_UNIVERSAL and SS_EXTERNAL symbols above.  */
  200.               ms_type = mst_file_text;
  201.               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
  202.               break;

  203.             case ST_STUB:
  204.               symname = bfd_getb32 (bufp->name) + stringtab;
  205.               ms_type = mst_solib_trampoline;
  206.               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
  207.               break;


  208.             case ST_DATA:
  209.               symname = bfd_getb32 (bufp->name) + stringtab;
  210.               ms_type = mst_file_data;
  211.               goto check_strange_names;

  212.             default:
  213.               continue;
  214.             }
  215.           break;

  216.           /* This can happen for common symbols when -E is passed to the
  217.              final link.  No idea _why_ that would make the linker force
  218.              common symbols to have an SS_UNSAT scope, but it does.

  219.              This also happens for weak symbols, but their type is
  220.              ST_DATA.  */
  221.         case SS_UNSAT:
  222.           switch (symbol_type)
  223.             {
  224.             case ST_STORAGE:
  225.             case ST_DATA:
  226.               symname = bfd_getb32 (bufp->name) + stringtab;
  227.               ms_type = mst_data;
  228.               break;

  229.             default:
  230.               continue;
  231.             }
  232.           break;

  233.         default:
  234.           continue;
  235.         }

  236.       if (bfd_getb32 (bufp->name) > obj_som_stringtab_size (abfd))
  237.         error (_("Invalid symbol data; bad HP string table offset: %s"),
  238.                plongest (bfd_getb32 (bufp->name)));

  239.       if (bfd_is_const_section (section))
  240.         {
  241.           struct obj_section *iter;

  242.           ALL_OBJFILE_OSECTIONS (objfile, iter)
  243.             {
  244.               CORE_ADDR start;
  245.               CORE_ADDR len;

  246.               if (bfd_is_const_section (iter->the_bfd_section))
  247.                 continue;

  248.               start = bfd_get_section_vma (iter->objfile->obfd,
  249.                                            iter->the_bfd_section);
  250.               len = bfd_get_section_size (iter->the_bfd_section);
  251.               if (start <= symbol_value && symbol_value < start + len)
  252.                 {
  253.                   section = iter->the_bfd_section;
  254.                   break;
  255.                 }
  256.             }
  257.         }

  258.       prim_record_minimal_symbol_and_info (symname, symbol_value, ms_type,
  259.                                            gdb_bfd_section_index (objfile->obfd,
  260.                                                                   section),
  261.                                            objfile);
  262.     }

  263.   do_cleanups (cleanup);
  264. }

  265. /* Scan and build partial symbols for a symbol file.
  266.    We have been initialized by a call to som_symfile_init, which
  267.    currently does nothing.

  268.    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
  269.    in each section.  This is ignored, as it isn't needed for SOM.

  270.    This function only does the minimum work necessary for letting the
  271.    user "name" things symbolically; it does not read the entire symtab.
  272.    Instead, it reads the external and static symbols and puts them in partial
  273.    symbol tables.  When more extensive information is requested of a
  274.    file, the corresponding partial symbol table is mutated into a full
  275.    fledged symbol table by going back and reading the symbols
  276.    for real.

  277.    We look for sections with specific names, to tell us what debug
  278.    format to look for.

  279.    somstab_build_psymtabs() handles STABS symbols.

  280.    Note that SOM files have a "minimal" symbol table, which is vaguely
  281.    reminiscent of a COFF symbol table, but has only the minimal information
  282.    necessary for linking.  We process this also, and use the information to
  283.    build gdb's minimal symbol table.  This gives us some minimal debugging
  284.    capability even for files compiled without -g.  */

  285. static void
  286. som_symfile_read (struct objfile *objfile, int symfile_flags)
  287. {
  288.   bfd *abfd = objfile->obfd;
  289.   struct cleanup *back_to;

  290.   init_minimal_symbol_collection ();
  291.   back_to = make_cleanup_discard_minimal_symbols ();

  292.   /* Process the normal SOM symbol table first.
  293.      This reads in the DNTT and string table, but doesn't
  294.      actually scan the DNTT.  It does scan the linker symbol
  295.      table and thus build up a "minimal symbol table".  */

  296.   som_symtab_read (abfd, objfile, objfile->section_offsets);

  297.   /* Install any minimal symbols that have been collected as the current
  298.      minimal symbols for this objfile.
  299.      Further symbol-reading is done incrementally, file-by-file,
  300.      in a step known as "psymtab-to-symtab" expansion.  hp-symtab-read.c
  301.      contains the code to do the actual DNTT scanning and symtab building.  */
  302.   install_minimal_symbols (objfile);
  303.   do_cleanups (back_to);

  304.   /* Now read information from the stabs debug sections.
  305.      This is emitted by gcc.  */
  306.   stabsect_build_psymtabs (objfile,
  307.                            "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
  308. }

  309. /* Initialize anything that needs initializing when a completely new symbol
  310.    file is specified (not just adding some symbols from another file, e.g. a
  311.    shared library).

  312.    We reinitialize buildsym, since we may be reading stabs from a SOM file.  */

  313. static void
  314. som_new_init (struct objfile *ignore)
  315. {
  316.   stabsread_new_init ();
  317.   buildsym_new_init ();
  318. }

  319. /* Perform any local cleanups required when we are done with a particular
  320.    objfileI.e, we are in the process of discarding all symbol information
  321.    for an objfile, freeing up all memory held for it, and unlinking the
  322.    objfile struct from the global list of known objfiles.  */

  323. static void
  324. som_symfile_finish (struct objfile *objfile)
  325. {
  326. }

  327. /* SOM specific initialization routine for reading symbols.  */

  328. static void
  329. som_symfile_init (struct objfile *objfile)
  330. {
  331.   /* SOM objects may be reordered, so set OBJF_REORDERED.  If we
  332.      find this causes a significant slowdown in gdb then we could
  333.      set it in the debug symbol readers only when necessary.  */
  334.   objfile->flags |= OBJF_REORDERED;
  335. }

  336. /* An object of this type is passed to find_section_offset.  */

  337. struct find_section_offset_arg
  338. {
  339.   /* The objfile.  */

  340.   struct objfile *objfile;

  341.   /* Flags to invert.  */

  342.   flagword invert;

  343.   /* Flags to look for.  */

  344.   flagword flags;

  345.   /* A text section with non-zero size, if any.  */

  346.   asection *best_section;

  347.   /* An empty text section, if any.  */

  348.   asection *empty_section;
  349. };

  350. /* A callback for bfd_map_over_sections that tries to find a section
  351.    with particular flags in an objfile.  */

  352. static void
  353. find_section_offset (bfd *abfd, asection *sect, void *arg)
  354. {
  355.   struct find_section_offset_arg *info = arg;
  356.   flagword aflag;

  357.   aflag = bfd_get_section_flags (abfd, sect);

  358.   aflag ^= info->invert;

  359.   if ((aflag & info->flags) == info->flags)
  360.     {
  361.       if (bfd_section_size (abfd, sect) > 0)
  362.         {
  363.           if (info->best_section == NULL)
  364.             info->best_section = sect;
  365.         }
  366.       else
  367.         {
  368.           if (info->empty_section == NULL)
  369.             info->empty_section = sect;
  370.         }
  371.     }
  372. }

  373. /* Set a section index from a BFD.  */

  374. static void
  375. set_section_index (struct objfile *objfile, flagword invert, flagword flags,
  376.                    int *index_ptr)
  377. {
  378.   struct find_section_offset_arg info;

  379.   info.objfile = objfile;
  380.   info.best_section = NULL;
  381.   info.empty_section = NULL;
  382.   info.invert = invert;
  383.   info.flags = flags;
  384.   bfd_map_over_sections (objfile->obfd, find_section_offset, &info);

  385.   if (info.best_section)
  386.     *index_ptr = info.best_section->index;
  387.   else if (info.empty_section)
  388.     *index_ptr = info.empty_section->index;
  389. }

  390. /* SOM specific parsing routine for section offsets.

  391.    Plain and simple for now.  */

  392. static void
  393. som_symfile_offsets (struct objfile *objfile,
  394.                      const struct section_addr_info *addrs)
  395. {
  396.   int i;
  397.   CORE_ADDR text_addr;
  398.   asection *sect;

  399.   objfile->num_sections = bfd_count_sections (objfile->obfd);
  400.   objfile->section_offsets = (struct section_offsets *)
  401.     obstack_alloc (&objfile->objfile_obstack,
  402.                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));

  403.   set_section_index (objfile, 0, SEC_ALLOC | SEC_CODE,
  404.                      &objfile->sect_index_text);
  405.   set_section_index (objfile, 0, SEC_ALLOC | SEC_DATA,
  406.                      &objfile->sect_index_data);
  407.   set_section_index (objfile, SEC_LOAD, SEC_ALLOC | SEC_LOAD,
  408.                      &objfile->sect_index_bss);
  409.   set_section_index (objfile, 0, SEC_ALLOC | SEC_READONLY,
  410.                      &objfile->sect_index_rodata);

  411.   /* First see if we're a shared library.  If so, get the section
  412.      offsets from the library, else get them from addrs.  */
  413.   if (!som_solib_section_offsets (objfile, objfile->section_offsets))
  414.     {
  415.       /* Note: Here is OK to compare with ".text" because this is the
  416.          name that gdb itself gives to that section, not the SOM
  417.          name.  */
  418.       for (i = 0; i < addrs->num_sections; i++)
  419.         if (strcmp (addrs->other[i].name, ".text") == 0)
  420.           break;
  421.       text_addr = addrs->other[i].addr;

  422.       for (i = 0; i < objfile->num_sections; i++)
  423.         (objfile->section_offsets)->offsets[i] = text_addr;
  424.     }
  425. }



  426. /* Register that we are able to handle SOM object file formats.  */

  427. static const struct sym_fns som_sym_fns =
  428. {
  429.   som_new_init,                        /* init anything gbl to entire symtab */
  430.   som_symfile_init,                /* read initial info, setup for sym_read() */
  431.   som_symfile_read,                /* read a symbol file into symtab */
  432.   NULL,                                /* sym_read_psymbols */
  433.   som_symfile_finish,                /* finished with file, cleanup */
  434.   som_symfile_offsets,                /* Translate ext. to int. relocation */
  435.   default_symfile_segments,        /* Get segment information from a file.  */
  436.   NULL,
  437.   default_symfile_relocate,        /* Relocate a debug section.  */
  438.   NULL,                                /* sym_get_probes */
  439.   &psym_functions
  440. };

  441. initialize_file_ftype _initialize_somread;

  442. void
  443. _initialize_somread (void)
  444. {
  445.   add_symtab_fns (bfd_target_som_flavour, &som_sym_fns);
  446. }