gdb/mipsread.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Read a symbol table in MIPS' format (Third-Eye).

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    Contributed by Alessandro Forin (af@cs.cmu.edu) at CMU.  Major work
  4.    by Per Bothner, John Gilmore and Ian Lance Taylor at Cygnus Support.

  5.    This file is part of GDB.

  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.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

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

  16. /* Read symbols from an ECOFF file.  Most of the work is done in
  17.    mdebugread.c.  */

  18. #include "defs.h"
  19. #include "bfd.h"
  20. #include "symtab.h"
  21. #include "objfiles.h"
  22. #include "buildsym.h"
  23. #include "stabsread.h"

  24. #include "coff/sym.h"
  25. #include "coff/internal.h"
  26. #include "coff/ecoff.h"
  27. #include "libcoff.h"                /* Private BFD COFF information.  */
  28. #include "libecoff.h"                /* Private BFD ECOFF information.  */
  29. #include "elf/common.h"
  30. #include "elf/internal.h"
  31. #include "elf/mips.h"

  32. #include "psymtab.h"

  33. static void
  34. read_alphacoff_dynamic_symtab (struct section_offsets *,
  35.                                struct objfile *objfile);

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

  39. static void
  40. mipscoff_new_init (struct objfile *ignore)
  41. {
  42.   stabsread_new_init ();
  43.   buildsym_new_init ();
  44. }

  45. /* Initialize to read a symbol file (nothing to do).  */

  46. static void
  47. mipscoff_symfile_init (struct objfile *objfile)
  48. {
  49. }

  50. /* Read a symbol file from a file.  */

  51. static void
  52. mipscoff_symfile_read (struct objfile *objfile, int symfile_flags)
  53. {
  54.   bfd *abfd = objfile->obfd;
  55.   struct cleanup *back_to;

  56.   init_minimal_symbol_collection ();
  57.   back_to = make_cleanup_discard_minimal_symbols ();

  58.   /* Now that the executable file is positioned at symbol table,
  59.      process it and define symbols accordingly.  */

  60.   if (!((*ecoff_backend (abfd)->debug_swap.read_debug_info)
  61.         (abfd, (asection *) NULL, &ecoff_data (abfd)->debug_info)))
  62.     error (_("Error reading symbol table: %s"), bfd_errmsg (bfd_get_error ()));

  63.   mdebug_build_psymtabs (objfile, &ecoff_backend (abfd)->debug_swap,
  64.                          &ecoff_data (abfd)->debug_info);

  65.   /* Add alpha coff dynamic symbols.  */

  66.   read_alphacoff_dynamic_symtab (objfile->section_offsets, objfile);

  67.   /* Install any minimal symbols that have been collected as the current
  68.      minimal symbols for this objfile.  */

  69.   install_minimal_symbols (objfile);
  70.   do_cleanups (back_to);
  71. }

  72. /* Perform any local cleanups required when we are done with a
  73.    particular objfile.  */

  74. static void
  75. mipscoff_symfile_finish (struct objfile *objfile)
  76. {
  77. }

  78. /* Alpha OSF/1 encapsulates the dynamic symbols in ELF format in a
  79.    standard COFF section.  The ELF format for the symbols differs from
  80.    the format defined in elf/external.h.  It seems that a normal ELF
  81.    32-bit format is used, and the representation only changes because
  82.    longs are 64-bit on the alpha.  In addition, the handling of
  83.    text/data section indices for symbols is different from the ELF
  84.    ABI.  As the BFD linker currently does not support dynamic linking
  85.    on the alpha, there seems to be no reason to pollute BFD with
  86.    another mixture of object file formats for now.  */

  87. /* Format of an alpha external ELF symbol.  */

  88. typedef struct
  89. {
  90.   unsigned char st_name[4];        /* Symbol name, index in string table.  */
  91.   unsigned char st_pad[4];        /* Pad to long word boundary.  */
  92.   unsigned char st_value[8];        /* Value of the symbol.  */
  93.   unsigned char st_size[4];        /* Associated symbol size.  */
  94.   unsigned char st_info[1];        /* Type and binding attributes.  */
  95.   unsigned char st_other[1];        /* No defined meaning, 0.  */
  96.   unsigned char st_shndx[2];        /* Associated section index.  */
  97. } Elfalpha_External_Sym;

  98. /* Format of an alpha external ELF dynamic info structure.  */

  99. typedef struct
  100. {
  101.   unsigned char d_tag[4];        /* Tag.  */
  102.   unsigned char d_pad[4];        /* Pad to long word boundary.  */
  103.   union
  104.   {
  105.     unsigned char d_ptr[8];        /* Pointer value.  */
  106.     unsigned char d_val[4];        /* Integer value.  */
  107.   }
  108.   d_un;
  109. } Elfalpha_External_Dyn;

  110. /* Struct to obtain the section pointers for alpha dynamic symbol info.  */

  111. struct alphacoff_dynsecinfo
  112. {
  113.   asection *sym_sect;                /* Section pointer for .dynsym section.  */
  114.   asection *str_sect;                /* Section pointer for .dynstr section.  */
  115.   asection *dyninfo_sect;        /* Section pointer for .dynamic section.  */
  116.   asection *got_sect;                /* Section pointer for .got section.  */
  117. };

  118. /* We are called once per section from read_alphacoff_dynamic_symtab.
  119.    We need to examine each section we are passed, check to see if it
  120.    is something we are interested in processing, and if so, stash away
  121.    some access information for the section.  */

  122. static void
  123. alphacoff_locate_sections (bfd *ignore_abfd, asection *sectp, void *sip)
  124. {
  125.   struct alphacoff_dynsecinfo *si;

  126.   si = (struct alphacoff_dynsecinfo *) sip;

  127.   if (strcmp (sectp->name, ".dynsym") == 0)
  128.     si->sym_sect = sectp;
  129.   else if (strcmp (sectp->name, ".dynstr") == 0)
  130.     si->str_sect = sectp;
  131.   else if (strcmp (sectp->name, ".dynamic") == 0)
  132.     si->dyninfo_sect = sectp;
  133.   else if (strcmp (sectp->name, ".got") == 0)
  134.       si->got_sect = sectp;
  135. }

  136. /* Scan an alpha dynamic symbol table for symbols of interest and add
  137.    them to the minimal symbol table.  */

  138. static void
  139. read_alphacoff_dynamic_symtab (struct section_offsets *section_offsets,
  140.                                struct objfile *objfile)
  141. {
  142.   bfd *abfd = objfile->obfd;
  143.   struct alphacoff_dynsecinfo si;
  144.   char *sym_secptr;
  145.   char *str_secptr;
  146.   char *dyninfo_secptr;
  147.   char *got_secptr;
  148.   bfd_size_type sym_secsize;
  149.   bfd_size_type str_secsize;
  150.   bfd_size_type dyninfo_secsize;
  151.   bfd_size_type got_secsize;
  152.   int sym_count;
  153.   int i;
  154.   int stripped;
  155.   Elfalpha_External_Sym *x_symp;
  156.   char *dyninfo_p;
  157.   char *dyninfo_end;
  158.   int got_entry_size = 8;
  159.   int dt_mips_local_gotno = -1;
  160.   int dt_mips_gotsym = -1;
  161.   struct cleanup *cleanups;

  162.   /* We currently only know how to handle alpha dynamic symbols.  */
  163.   if (bfd_get_arch (abfd) != bfd_arch_alpha)
  164.     return;

  165.   /* Locate the dynamic symbols sections and read them in.  */
  166.   memset ((char *) &si, 0, sizeof (si));
  167.   bfd_map_over_sections (abfd, alphacoff_locate_sections, (void *) & si);
  168.   if (si.sym_sect == NULL || si.str_sect == NULL
  169.       || si.dyninfo_sect == NULL || si.got_sect == NULL)
  170.     return;

  171.   sym_secsize = bfd_get_section_size (si.sym_sect);
  172.   str_secsize = bfd_get_section_size (si.str_sect);
  173.   dyninfo_secsize = bfd_get_section_size (si.dyninfo_sect);
  174.   got_secsize = bfd_get_section_size (si.got_sect);
  175.   sym_secptr = xmalloc (sym_secsize);
  176.   cleanups = make_cleanup (xfree, sym_secptr);
  177.   str_secptr = xmalloc (str_secsize);
  178.   make_cleanup (xfree, str_secptr);
  179.   dyninfo_secptr = xmalloc (dyninfo_secsize);
  180.   make_cleanup (xfree, dyninfo_secptr);
  181.   got_secptr = xmalloc (got_secsize);
  182.   make_cleanup (xfree, got_secptr);

  183.   if (!bfd_get_section_contents (abfd, si.sym_sect, sym_secptr,
  184.                                  (file_ptr) 0, sym_secsize))
  185.     {
  186.       do_cleanups (cleanups);
  187.       return;
  188.     }
  189.   if (!bfd_get_section_contents (abfd, si.str_sect, str_secptr,
  190.                                  (file_ptr) 0, str_secsize))
  191.     {
  192.       do_cleanups (cleanups);
  193.       return;
  194.     }
  195.   if (!bfd_get_section_contents (abfd, si.dyninfo_sect, dyninfo_secptr,
  196.                                  (file_ptr) 0, dyninfo_secsize))
  197.     {
  198.       do_cleanups (cleanups);
  199.       return;
  200.     }
  201.   if (!bfd_get_section_contents (abfd, si.got_sect, got_secptr,
  202.                                  (file_ptr) 0, got_secsize))
  203.     {
  204.       do_cleanups (cleanups);
  205.       return;
  206.     }

  207.   /* Find the number of local GOT entries and the index for the
  208.      first dynamic symbol in the GOT.  */
  209.   for (dyninfo_p = dyninfo_secptr, dyninfo_end = dyninfo_p + dyninfo_secsize;
  210.        dyninfo_p < dyninfo_end;
  211.        dyninfo_p += sizeof (Elfalpha_External_Dyn))
  212.     {
  213.       Elfalpha_External_Dyn *x_dynp = (Elfalpha_External_Dyn *) dyninfo_p;
  214.       long dyn_tag;

  215.       dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_tag);
  216.       if (dyn_tag == DT_NULL)
  217.         break;
  218.       else if (dyn_tag == DT_MIPS_LOCAL_GOTNO)
  219.         {
  220.           if (dt_mips_local_gotno < 0)
  221.             dt_mips_local_gotno
  222.               = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_un.d_val);
  223.         }
  224.       else if (dyn_tag == DT_MIPS_GOTSYM)
  225.         {
  226.           if (dt_mips_gotsym < 0)
  227.             dt_mips_gotsym
  228.               = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_un.d_val);
  229.         }
  230.     }
  231.   if (dt_mips_local_gotno < 0 || dt_mips_gotsym < 0)
  232.     {
  233.       do_cleanups (cleanups);
  234.       return;
  235.     }

  236.   /* Scan all dynamic symbols and enter them into the minimal symbol
  237.      table if appropriate.  */
  238.   sym_count = sym_secsize / sizeof (Elfalpha_External_Sym);
  239.   stripped = (bfd_get_symcount (abfd) == 0);

  240.   /* Skip first symbol, which is a null dummy.  */
  241.   for (i = 1, x_symp = (Elfalpha_External_Sym *) sym_secptr + 1;
  242.        i < sym_count;
  243.        i++, x_symp++)
  244.     {
  245.       unsigned long strx;
  246.       char *name;
  247.       bfd_vma sym_value;
  248.       unsigned char sym_info;
  249.       unsigned int sym_shndx;
  250.       int isglobal;
  251.       enum minimal_symbol_type ms_type;

  252.       strx = bfd_h_get_32 (abfd, (bfd_byte *) x_symp->st_name);
  253.       if (strx >= str_secsize)
  254.         continue;
  255.       name = str_secptr + strx;
  256.       if (*name == '\0' || *name == '.')
  257.         continue;

  258.       sym_value = bfd_h_get_64 (abfd, (bfd_byte *) x_symp->st_value);
  259.       sym_info = bfd_h_get_8 (abfd, (bfd_byte *) x_symp->st_info);
  260.       sym_shndx = bfd_h_get_16 (abfd, (bfd_byte *) x_symp->st_shndx);
  261.       if (sym_shndx >= (SHN_LORESERVE & 0xffff))
  262.         sym_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
  263.       isglobal = (ELF_ST_BIND (sym_info) == STB_GLOBAL);

  264.       if (sym_shndx == SHN_UNDEF)
  265.         {
  266.           /* Handle undefined functions which are defined in a shared
  267.              library.  */
  268.           if (ELF_ST_TYPE (sym_info) != STT_FUNC
  269.               || ELF_ST_BIND (sym_info) != STB_GLOBAL)
  270.             continue;

  271.           ms_type = mst_solib_trampoline;

  272.           /* If sym_value is nonzero, it points to the shared library
  273.              trampoline entry, which is what we are looking for.

  274.              If sym_value is zero, then we have to get the GOT entry
  275.              for the symbol.

  276.              If the GOT entry is nonzero, it represents the quickstart
  277.              address of the function and we use that as the symbol
  278.              value.

  279.              If the GOT entry is zero, the function address has to be
  280.              resolved by the runtime loader before the executable is
  281.              started.  We are unable to find any meaningful address
  282.              for these functions in the executable file, so we skip
  283.              them.  */
  284.           if (sym_value == 0)
  285.             {
  286.               int got_entry_offset =
  287.                 (i - dt_mips_gotsym + dt_mips_local_gotno) * got_entry_size;

  288.               if (got_entry_offset < 0 || got_entry_offset >= got_secsize)
  289.                 continue;
  290.               sym_value =
  291.                 bfd_h_get_64 (abfd,
  292.                               (bfd_byte *) (got_secptr + got_entry_offset));
  293.               if (sym_value == 0)
  294.                 continue;
  295.             }
  296.         }
  297.       else
  298.         {
  299.           /* Symbols defined in the executable itself.  We only care
  300.              about them if this is a stripped executable, otherwise
  301.              they have been retrieved from the normal symbol table
  302.              already.  */
  303.           if (!stripped)
  304.             continue;

  305.           if (sym_shndx == SHN_MIPS_TEXT)
  306.             {
  307.               if (isglobal)
  308.                 ms_type = mst_text;
  309.               else
  310.                 ms_type = mst_file_text;
  311.             }
  312.           else if (sym_shndx == SHN_MIPS_DATA)
  313.             {
  314.               if (isglobal)
  315.                 ms_type = mst_data;
  316.               else
  317.                 ms_type = mst_file_data;
  318.             }
  319.           else if (sym_shndx == SHN_MIPS_ACOMMON)
  320.             {
  321.               if (isglobal)
  322.                 ms_type = mst_bss;
  323.               else
  324.                 ms_type = mst_file_bss;
  325.             }
  326.           else if (sym_shndx == SHN_ABS)
  327.             {
  328.               ms_type = mst_abs;
  329.             }
  330.           else
  331.             {
  332.               continue;
  333.             }
  334.         }

  335.       prim_record_minimal_symbol (name, sym_value, ms_type, objfile);
  336.     }

  337.   do_cleanups (cleanups);
  338. }

  339. /* Initialization.  */

  340. static const struct sym_fns ecoff_sym_fns =
  341. {
  342.   mipscoff_new_init,                /* init anything gbl to entire symtab */
  343.   mipscoff_symfile_init,        /* read initial info, setup for sym_read() */
  344.   mipscoff_symfile_read,        /* read a symbol file into symtab */
  345.   NULL,                                /* sym_read_psymbols */
  346.   mipscoff_symfile_finish,        /* finished with file, cleanup */
  347.   default_symfile_offsets,        /* dummy FIXME til implem sym reloc */
  348.   default_symfile_segments,        /* Get segment information from a file.  */
  349.   NULL,
  350.   default_symfile_relocate,        /* Relocate a debug section.  */
  351.   NULL,                                /* sym_probe_fns */
  352.   &psym_functions
  353. };

  354. /* Provide a prototype to silence -Wmissing-prototypes.  */
  355. void _initialize_mipsread (void);

  356. void
  357. _initialize_mipsread (void)
  358. {
  359.   add_symtab_fns (bfd_target_ecoff_flavour, &ecoff_sym_fns);
  360. }