GDB (API)
/home/stan/gdb/src/gdb/somread.c
Go to the documentation of this file.
00001 /* Read HP PA/Risc object files for GDB.
00002    Copyright (C) 1991-2013 Free Software Foundation, Inc.
00003    Written by Fred Fish at Cygnus Support.
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 #include "defs.h"
00021 #include "bfd.h"
00022 #include "som/aout.h"
00023 #include "symtab.h"
00024 #include "symfile.h"
00025 #include "objfiles.h"
00026 #include "buildsym.h"
00027 #include "stabsread.h"
00028 #include "gdb-stabs.h"
00029 #include "complaints.h"
00030 #include "gdb_string.h"
00031 #include "demangle.h"
00032 #include "som.h"
00033 #include "libhppa.h"
00034 #include "psymtab.h"
00035 
00036 #include "solib-som.h"
00037 
00038 /* Read the symbol table of a SOM file.
00039 
00040    Given an open bfd, a base address to relocate symbols to, and a
00041    flag that specifies whether or not this bfd is for an executable
00042    or not (may be shared library for example), add all the global
00043    function and data symbols to the minimal symbol table.  */
00044 
00045 static void
00046 som_symtab_read (bfd *abfd, struct objfile *objfile,
00047                  struct section_offsets *section_offsets)
00048 {
00049   struct cleanup *cleanup;
00050   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00051   unsigned int number_of_symbols;
00052   int val, dynamic;
00053   char *stringtab;
00054   asection *shlib_info;
00055   struct som_external_symbol_dictionary_record *buf, *bufp, *endbufp;
00056   char *symname;
00057   CONST int symsize = sizeof (struct som_external_symbol_dictionary_record);
00058 
00059 
00060 #define text_offset ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile))
00061 #define data_offset ANOFFSET (section_offsets, SECT_OFF_DATA (objfile))
00062 
00063   number_of_symbols = bfd_get_symcount (abfd);
00064 
00065   /* Allocate a buffer to read in the debug info.
00066      We avoid using alloca because the memory size could be so large
00067      that we could hit the stack size limit.  */
00068   buf = xmalloc (symsize * number_of_symbols);
00069   cleanup = make_cleanup (xfree, buf);
00070   bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
00071   val = bfd_bread (buf, symsize * number_of_symbols, abfd);
00072   if (val != symsize * number_of_symbols)
00073     error (_("Couldn't read symbol dictionary!"));
00074 
00075   /* Allocate a buffer to read in the som stringtab section of
00076      the debugging info.  Again, we avoid using alloca because
00077      the data could be so large that we could potentially hit
00078      the stack size limitat.  */
00079   stringtab = xmalloc (obj_som_stringtab_size (abfd));
00080   make_cleanup (xfree, stringtab);
00081   bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
00082   val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
00083   if (val != obj_som_stringtab_size (abfd))
00084     error (_("Can't read in HP string table."));
00085 
00086   /* We need to determine if objfile is a dynamic executable (so we
00087      can do the right thing for ST_ENTRY vs ST_CODE symbols).
00088 
00089      There's nothing in the header which easily allows us to do
00090      this.
00091 
00092      This code used to rely upon the existence of a $SHLIB_INFO$
00093      section to make this determination.  HP claims that it is
00094      more accurate to check for a nonzero text offset, but they
00095      have not provided any information about why that test is
00096      more accurate.  */
00097   dynamic = (text_offset != 0);
00098 
00099   endbufp = buf + number_of_symbols;
00100   for (bufp = buf; bufp < endbufp; ++bufp)
00101     {
00102       enum minimal_symbol_type ms_type;
00103       unsigned int flags = bfd_getb32 (bufp->flags);
00104       unsigned int symbol_type
00105         = (flags >> SOM_SYMBOL_TYPE_SH) & SOM_SYMBOL_TYPE_MASK;
00106       unsigned int symbol_scope
00107         = (flags >> SOM_SYMBOL_SCOPE_SH) & SOM_SYMBOL_SCOPE_MASK;
00108       CORE_ADDR symbol_value = bfd_getb32 (bufp->symbol_value);
00109       asection *section = NULL;
00110 
00111       QUIT;
00112 
00113       /* Compute the section.  */
00114       switch (symbol_scope)
00115         {
00116         case SS_EXTERNAL:
00117           if (symbol_type != ST_STORAGE)
00118             section = bfd_und_section_ptr;
00119           else
00120             section = bfd_com_section_ptr;
00121           break;
00122 
00123         case SS_UNSAT:
00124           if (symbol_type != ST_STORAGE)
00125             section = bfd_und_section_ptr;
00126           else
00127             section = bfd_com_section_ptr;
00128           break;
00129 
00130         case SS_UNIVERSAL:
00131           section = bfd_section_from_som_symbol (abfd, bufp);
00132           break;
00133 
00134         case SS_LOCAL:
00135           section = bfd_section_from_som_symbol (abfd, bufp);
00136           break;
00137         }
00138 
00139       switch (symbol_scope)
00140         {
00141         case SS_UNIVERSAL:
00142         case SS_EXTERNAL:
00143           switch (symbol_type)
00144             {
00145             case ST_SYM_EXT:
00146             case ST_ARG_EXT:
00147               continue;
00148 
00149             case ST_CODE:
00150             case ST_PRI_PROG:
00151             case ST_SEC_PROG:
00152             case ST_MILLICODE:
00153               symname = bfd_getb32 (bufp->name) + stringtab;
00154               ms_type = mst_text;
00155               symbol_value += text_offset;
00156               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
00157               break;
00158 
00159             case ST_ENTRY:
00160               symname = bfd_getb32 (bufp->name) + stringtab;
00161               /* For a dynamic executable, ST_ENTRY symbols are
00162                  the stubs, while the ST_CODE symbol is the real
00163                  function.  */
00164               if (dynamic)
00165                 ms_type = mst_solib_trampoline;
00166               else
00167                 ms_type = mst_text;
00168               symbol_value += text_offset;
00169               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
00170               break;
00171 
00172             case ST_STUB:
00173               symname = bfd_getb32 (bufp->name) + stringtab;
00174               ms_type = mst_solib_trampoline;
00175               symbol_value += text_offset;
00176               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
00177               break;
00178 
00179             case ST_DATA:
00180               symname = bfd_getb32 (bufp->name) + stringtab;
00181               symbol_value += data_offset;
00182               ms_type = mst_data;
00183               break;
00184             default:
00185               continue;
00186             }
00187           break;
00188 
00189 #if 0
00190           /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
00191         case SS_GLOBAL:
00192 #endif
00193         case SS_LOCAL:
00194           switch (symbol_type)
00195             {
00196             case ST_SYM_EXT:
00197             case ST_ARG_EXT:
00198               continue;
00199 
00200             case ST_CODE:
00201               symname = bfd_getb32 (bufp->name) + stringtab;
00202               ms_type = mst_file_text;
00203               symbol_value += text_offset;
00204               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
00205 
00206             check_strange_names:
00207               /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
00208                  label prefixes for stabs, constant data, etc.  So we need
00209                  only filter out L$ symbols which are left in due to
00210                  limitations in how GAS generates SOM relocations.
00211 
00212                  When linking in the HPUX C-library the HP linker has
00213                  the nasty habit of placing section symbols from the literal
00214                  subspaces in the middle of the program's text.  Filter
00215                  those out as best we can.  Check for first and last character
00216                  being '$'.
00217 
00218                  And finally, the newer HP compilers emit crud like $PIC_foo$N
00219                  in some circumstance (PIC code I guess).  It's also claimed
00220                  that they emit D$ symbols too.  What stupidity.  */
00221               if ((symname[0] == 'L' && symname[1] == '$')
00222               || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
00223                   || (symname[0] == 'D' && symname[1] == '$')
00224                   || (strncmp (symname, "L0\001", 3) == 0)
00225                   || (strncmp (symname, "$PIC", 4) == 0))
00226                 continue;
00227               break;
00228 
00229             case ST_PRI_PROG:
00230             case ST_SEC_PROG:
00231             case ST_MILLICODE:
00232               symname = bfd_getb32 (bufp->name) + stringtab;
00233               ms_type = mst_file_text;
00234               symbol_value += text_offset;
00235               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
00236               break;
00237 
00238             case ST_ENTRY:
00239               symname = bfd_getb32 (bufp->name) + stringtab;
00240               /* SS_LOCAL symbols in a shared library do not have
00241                  export stubs, so we do not have to worry about
00242                  using mst_file_text vs mst_solib_trampoline here like
00243                  we do for SS_UNIVERSAL and SS_EXTERNAL symbols above.  */
00244               ms_type = mst_file_text;
00245               symbol_value += text_offset;
00246               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
00247               break;
00248 
00249             case ST_STUB:
00250               symname = bfd_getb32 (bufp->name) + stringtab;
00251               ms_type = mst_solib_trampoline;
00252               symbol_value += text_offset;
00253               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
00254               break;
00255 
00256 
00257             case ST_DATA:
00258               symname = bfd_getb32 (bufp->name) + stringtab;
00259               symbol_value += data_offset;
00260               ms_type = mst_file_data;
00261               goto check_strange_names;
00262 
00263             default:
00264               continue;
00265             }
00266           break;
00267 
00268           /* This can happen for common symbols when -E is passed to the
00269              final link.  No idea _why_ that would make the linker force
00270              common symbols to have an SS_UNSAT scope, but it does.
00271 
00272              This also happens for weak symbols, but their type is
00273              ST_DATA.  */
00274         case SS_UNSAT:
00275           switch (symbol_type)
00276             {
00277             case ST_STORAGE:
00278             case ST_DATA:
00279               symname = bfd_getb32 (bufp->name) + stringtab;
00280               symbol_value += data_offset;
00281               ms_type = mst_data;
00282               break;
00283 
00284             default:
00285               continue;
00286             }
00287           break;
00288 
00289         default:
00290           continue;
00291         }
00292 
00293       if (bfd_getb32 (bufp->name) > obj_som_stringtab_size (abfd))
00294         error (_("Invalid symbol data; bad HP string table offset: %s"),
00295                plongest (bfd_getb32 (bufp->name)));
00296 
00297       if (bfd_is_const_section (section))
00298         {
00299           struct obj_section *iter;
00300 
00301           ALL_OBJFILE_OSECTIONS (objfile, iter)
00302             {
00303               if (bfd_is_const_section (iter->the_bfd_section))
00304                 continue;
00305 
00306               if (obj_section_addr (iter) <= symbol_value
00307                   && symbol_value < obj_section_endaddr (iter))
00308                 {
00309                   section = iter->the_bfd_section;
00310                   break;
00311                 }
00312             }
00313         }
00314 
00315       prim_record_minimal_symbol_and_info (symname, symbol_value, ms_type,
00316                                            gdb_bfd_section_index (objfile->obfd,
00317                                                                   section),
00318                                            objfile);
00319     }
00320 
00321   do_cleanups (cleanup);
00322 }
00323 
00324 /* Scan and build partial symbols for a symbol file.
00325    We have been initialized by a call to som_symfile_init, which 
00326    currently does nothing.
00327 
00328    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
00329    in each section.  This is ignored, as it isn't needed for SOM.
00330 
00331    This function only does the minimum work necessary for letting the
00332    user "name" things symbolically; it does not read the entire symtab.
00333    Instead, it reads the external and static symbols and puts them in partial
00334    symbol tables.  When more extensive information is requested of a
00335    file, the corresponding partial symbol table is mutated into a full
00336    fledged symbol table by going back and reading the symbols
00337    for real.
00338 
00339    We look for sections with specific names, to tell us what debug
00340    format to look for.
00341 
00342    somstab_build_psymtabs() handles STABS symbols.
00343 
00344    Note that SOM files have a "minimal" symbol table, which is vaguely
00345    reminiscent of a COFF symbol table, but has only the minimal information
00346    necessary for linking.  We process this also, and use the information to
00347    build gdb's minimal symbol table.  This gives us some minimal debugging
00348    capability even for files compiled without -g.  */
00349 
00350 static void
00351 som_symfile_read (struct objfile *objfile, int symfile_flags)
00352 {
00353   bfd *abfd = objfile->obfd;
00354   struct cleanup *back_to;
00355 
00356   init_minimal_symbol_collection ();
00357   back_to = make_cleanup_discard_minimal_symbols ();
00358 
00359   /* Process the normal SOM symbol table first.
00360      This reads in the DNTT and string table, but doesn't
00361      actually scan the DNTT.  It does scan the linker symbol
00362      table and thus build up a "minimal symbol table".  */
00363 
00364   som_symtab_read (abfd, objfile, objfile->section_offsets);
00365 
00366   /* Install any minimal symbols that have been collected as the current
00367      minimal symbols for this objfile.
00368      Further symbol-reading is done incrementally, file-by-file,
00369      in a step known as "psymtab-to-symtab" expansion.  hp-symtab-read.c
00370      contains the code to do the actual DNTT scanning and symtab building.  */
00371   install_minimal_symbols (objfile);
00372   do_cleanups (back_to);
00373 
00374   /* Now read information from the stabs debug sections.
00375      This is emitted by gcc.  */
00376   stabsect_build_psymtabs (objfile,
00377                            "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
00378 }
00379 
00380 /* Initialize anything that needs initializing when a completely new symbol
00381    file is specified (not just adding some symbols from another file, e.g. a
00382    shared library).
00383 
00384    We reinitialize buildsym, since we may be reading stabs from a SOM file.  */
00385 
00386 static void
00387 som_new_init (struct objfile *ignore)
00388 {
00389   stabsread_new_init ();
00390   buildsym_new_init ();
00391 }
00392 
00393 /* Perform any local cleanups required when we are done with a particular
00394    objfile.  I.e, we are in the process of discarding all symbol information
00395    for an objfile, freeing up all memory held for it, and unlinking the
00396    objfile struct from the global list of known objfiles.  */
00397 
00398 static void
00399 som_symfile_finish (struct objfile *objfile)
00400 {
00401 }
00402 
00403 /* SOM specific initialization routine for reading symbols.  */
00404 
00405 static void
00406 som_symfile_init (struct objfile *objfile)
00407 {
00408   /* SOM objects may be reordered, so set OBJF_REORDERED.  If we
00409      find this causes a significant slowdown in gdb then we could
00410      set it in the debug symbol readers only when necessary.  */
00411   objfile->flags |= OBJF_REORDERED;
00412 }
00413 
00414 /* An object of this type is passed to find_section_offset.  */
00415 
00416 struct find_section_offset_arg
00417 {
00418   /* The objfile.  */
00419 
00420   struct objfile *objfile;
00421 
00422   /* Flags to invert.  */
00423 
00424   flagword invert;
00425 
00426   /* Flags to look for.  */
00427 
00428   flagword flags;
00429 
00430   /* A text section with non-zero size, if any.  */
00431 
00432   asection *best_section;
00433 
00434   /* An empty text section, if any.  */
00435 
00436   asection *empty_section;
00437 };
00438 
00439 /* A callback for bfd_map_over_sections that tries to find a section
00440    with particular flags in an objfile.  */
00441 
00442 static void
00443 find_section_offset (bfd *abfd, asection *sect, void *arg)
00444 {
00445   struct find_section_offset_arg *info = arg;
00446   flagword aflag;
00447 
00448   aflag = bfd_get_section_flags (abfd, sect);
00449 
00450   aflag ^= info->invert;
00451 
00452   if ((aflag & info->flags) == info->flags)
00453     {
00454       if (bfd_section_size (abfd, sect) > 0)
00455         {
00456           if (info->best_section == NULL)
00457             info->best_section = sect;
00458         }
00459       else
00460         {
00461           if (info->empty_section == NULL)
00462             info->empty_section = sect;
00463         }
00464     }
00465 }
00466 
00467 /* Set a section index from a BFD.  */
00468 
00469 static void
00470 set_section_index (struct objfile *objfile, flagword invert, flagword flags,
00471                    int *index_ptr)
00472 {
00473   struct find_section_offset_arg info;
00474 
00475   info.objfile = objfile;
00476   info.best_section = NULL;
00477   info.empty_section = NULL;
00478   info.invert = invert;
00479   info.flags = flags;
00480   bfd_map_over_sections (objfile->obfd, find_section_offset, &info);
00481 
00482   if (info.best_section)
00483     *index_ptr = info.best_section->index;
00484   else if (info.empty_section)
00485     *index_ptr = info.empty_section->index;
00486 }
00487 
00488 /* SOM specific parsing routine for section offsets.
00489 
00490    Plain and simple for now.  */
00491 
00492 static void
00493 som_symfile_offsets (struct objfile *objfile,
00494                      const struct section_addr_info *addrs)
00495 {
00496   int i;
00497   CORE_ADDR text_addr;
00498   asection *sect;
00499 
00500   objfile->num_sections = bfd_count_sections (objfile->obfd);
00501   objfile->section_offsets = (struct section_offsets *)
00502     obstack_alloc (&objfile->objfile_obstack, 
00503                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
00504 
00505   set_section_index (objfile, 0, SEC_ALLOC | SEC_CODE,
00506                      &objfile->sect_index_text);
00507   set_section_index (objfile, 0, SEC_ALLOC | SEC_DATA,
00508                      &objfile->sect_index_data);
00509   set_section_index (objfile, SEC_LOAD, SEC_ALLOC | SEC_LOAD,
00510                      &objfile->sect_index_bss);
00511   set_section_index (objfile, 0, SEC_ALLOC | SEC_READONLY,
00512                      &objfile->sect_index_rodata);
00513 
00514   /* First see if we're a shared library.  If so, get the section
00515      offsets from the library, else get them from addrs.  */
00516   if (!som_solib_section_offsets (objfile, objfile->section_offsets))
00517     {
00518       /* Note: Here is OK to compare with ".text" because this is the
00519          name that gdb itself gives to that section, not the SOM
00520          name.  */
00521       for (i = 0; i < addrs->num_sections; i++)
00522         if (strcmp (addrs->other[i].name, ".text") == 0)
00523           break;
00524       text_addr = addrs->other[i].addr;
00525 
00526       for (i = 0; i < objfile->num_sections; i++)
00527         (objfile->section_offsets)->offsets[i] = text_addr;
00528     }
00529 }
00530 
00531 
00532 
00533 /* Register that we are able to handle SOM object file formats.  */
00534 
00535 static const struct sym_fns som_sym_fns =
00536 {
00537   som_new_init,                 /* init anything gbl to entire symtab */
00538   som_symfile_init,             /* read initial info, setup for sym_read() */
00539   som_symfile_read,             /* read a symbol file into symtab */
00540   NULL,                         /* sym_read_psymbols */
00541   som_symfile_finish,           /* finished with file, cleanup */
00542   som_symfile_offsets,          /* Translate ext. to int. relocation */
00543   default_symfile_segments,     /* Get segment information from a file.  */
00544   NULL,
00545   default_symfile_relocate,     /* Relocate a debug section.  */
00546   NULL,                         /* sym_get_probes */
00547   &psym_functions
00548 };
00549 
00550 initialize_file_ftype _initialize_somread;
00551 
00552 void
00553 _initialize_somread (void)
00554 {
00555   add_symtab_fns (bfd_target_som_flavour, &som_sym_fns);
00556 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines