gdb/xcoffread.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Read AIX xcoff symbol tables and convert to internal format, for GDB.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.
  3.    Derived from coffread.c, dbxread.c, and a lot of hacking.
  4.    Contributed by IBM Corporation.

  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. #include "defs.h"
  17. #include "bfd.h"

  18. #include <sys/types.h>
  19. #include <fcntl.h>
  20. #include <ctype.h>
  21. #ifdef HAVE_SYS_FILE_H
  22. #include <sys/file.h>
  23. #endif
  24. #include <sys/stat.h>

  25. #include "coff/internal.h"
  26. #include "libcoff.h"                /* FIXME, internal data from BFD */
  27. #include "coff/xcoff.h"
  28. #include "libxcoff.h"
  29. #include "coff/rs6000.h"
  30. #include "xcoffread.h"

  31. #include "symtab.h"
  32. #include "gdbtypes.h"
  33. /* FIXME: ezannoni/2004-02-13 Verify if the include below is really needed.  */
  34. #include "symfile.h"
  35. #include "objfiles.h"
  36. #include "buildsym.h"
  37. #include "stabsread.h"
  38. #include "expression.h"
  39. #include "complaints.h"
  40. #include "psympriv.h"

  41. #include "gdb-stabs.h"

  42. /* For interface with stabsread.c.  */
  43. #include "aout/stab_gnu.h"


  44. /* Key for XCOFF-associated data.  */

  45. static const struct objfile_data *xcoff_objfile_data_key;

  46. /* We put a pointer to this structure in the read_symtab_private field
  47.    of the psymtab.  */

  48. struct symloc
  49.   {

  50.     /* First symbol number for this file.  */

  51.     int first_symnum;

  52.     /* Number of symbols in the section of the symbol table devoted to
  53.        this file's symbols (actually, the section bracketed may contain
  54.        more than just this file's symbols).  If numsyms is 0, the only
  55.        reason for this thing's existence is the dependency list.  Nothing
  56.        else will happen when it is read in.  */

  57.     int numsyms;

  58.     /* Position of the start of the line number information for this
  59.        psymtab.  */
  60.     unsigned int lineno_off;
  61.   };

  62. /* Remember what we deduced to be the source language of this psymtab.  */

  63. static enum language psymtab_language = language_unknown;


  64. /* Simplified internal version of coff symbol table information.  */

  65. struct coff_symbol
  66.   {
  67.     char *c_name;
  68.     int c_symnum;                /* Symbol number of this entry.  */
  69.     int c_naux;                        /* 0 if syment only, 1 if syment + auxent.  */
  70.     CORE_ADDR c_value;
  71.     unsigned char c_sclass;
  72.     int c_secnum;
  73.     unsigned int c_type;
  74.   };

  75. /* Last function's saved coff symbol `cs'.  */

  76. static struct coff_symbol fcn_cs_saved;

  77. static bfd *symfile_bfd;

  78. /* Core address of start and end of text of current source file.
  79.    This is calculated from the first function seen after a C_FILE
  80.    symbol.  */


  81. static CORE_ADDR cur_src_end_addr;

  82. /* Core address of the end of the first object file.  */

  83. static CORE_ADDR first_object_file_end;

  84. /* Initial symbol-table-debug-string vector length.  */

  85. #define        INITIAL_STABVECTOR_LENGTH        40

  86. /* Nonzero if within a function (so symbols should be local,
  87.    if nothing says specifically).  */

  88. int within_function;

  89. /* Size of a COFF symbolI think it is always 18, so I'm not sure
  90.    there is any reason not to just use a #define, but might as well
  91.    ask BFD for the size and store it here, I guess.  */

  92. static unsigned local_symesz;

  93. struct coff_symfile_info
  94.   {
  95.     file_ptr min_lineno_offset;        /* Where in file lowest line#s are.  */
  96.     file_ptr max_lineno_offset;        /* 1+last byte of line#s in file.  */

  97.     /* Pointer to the string table.  */
  98.     char *strtbl;

  99.     /* Pointer to debug section.  */
  100.     char *debugsec;

  101.     /* Pointer to the a.out symbol table.  */
  102.     char *symtbl;

  103.     /* Number of symbols in symtbl.  */
  104.     int symtbl_num_syms;

  105.     /* Offset in data section to TOC anchor.  */
  106.     CORE_ADDR toc_offset;
  107.   };

  108. /* Convenience macro to access the per-objfile XCOFF data.  */

  109. #define XCOFF_DATA(objfile)                                                \
  110.   ((struct coff_symfile_info *) objfile_data ((objfile),                \
  111.                                               xcoff_objfile_data_key))

  112. /* XCOFF names for dwarf sections.  There is no compressed sections.  */

  113. static const struct dwarf2_debug_sections dwarf2_xcoff_names = {
  114.   { ".dwinfo", NULL },
  115.   { ".dwabrev", NULL },
  116.   { ".dwline", NULL },
  117.   { ".dwloc", NULL },
  118.   { NULL, NULL }, /* debug_macinfo */
  119.   { NULL, NULL }, /* debug_macro */
  120.   { ".dwstr", NULL },
  121.   { ".dwrnges", NULL },
  122.   { NULL, NULL }, /* debug_types */
  123.   { NULL, NULL }, /* debug_addr */
  124.   { ".dwframe", NULL },
  125.   { NULL, NULL }, /* eh_frame */
  126.   { NULL, NULL }, /* gdb_index */
  127.   23
  128. };

  129. static void
  130. bf_notfound_complaint (void)
  131. {
  132.   complaint (&symfile_complaints,
  133.              _("line numbers off, `.bf' symbol not found"));
  134. }

  135. static void
  136. ef_complaint (int arg1)
  137. {
  138.   complaint (&symfile_complaints,
  139.              _("Mismatched .ef symbol ignored starting at symnum %d"), arg1);
  140. }

  141. static void
  142. eb_complaint (int arg1)
  143. {
  144.   complaint (&symfile_complaints,
  145.              _("Mismatched .eb symbol ignored starting at symnum %d"), arg1);
  146. }

  147. static void xcoff_initial_scan (struct objfile *, int);

  148. static void scan_xcoff_symtab (struct objfile *);

  149. static char *xcoff_next_symbol_text (struct objfile *);

  150. static void record_include_begin (struct coff_symbol *);

  151. static void
  152. enter_line_range (struct subfile *, unsigned, unsigned,
  153.                   CORE_ADDR, CORE_ADDR, unsigned *);

  154. static void init_stringtab (bfd *, file_ptr, struct objfile *);

  155. static void xcoff_symfile_init (struct objfile *);

  156. static void xcoff_new_init (struct objfile *);

  157. static void xcoff_symfile_finish (struct objfile *);

  158. static char *coff_getfilename (union internal_auxent *, struct objfile *);

  159. static void read_symbol (struct internal_syment *, int);

  160. static int read_symbol_lineno (int);

  161. static CORE_ADDR read_symbol_nvalue (int);

  162. static struct symbol *process_xcoff_symbol (struct coff_symbol *,
  163.                                             struct objfile *);

  164. static void read_xcoff_symtab (struct objfile *, struct partial_symtab *);

  165. #if 0
  166. static void add_stab_to_list (char *, struct pending_stabs **);
  167. #endif

  168. static int compare_lte (const void *, const void *);

  169. static struct linetable *arrange_linetable (struct linetable *);

  170. static void record_include_end (struct coff_symbol *);

  171. static void process_linenos (CORE_ADDR, CORE_ADDR);


  172. /* Translate from a COFF section number (target_index) to a SECT_OFF_*
  173.    code.  */
  174. static int secnum_to_section (int, struct objfile *);
  175. static asection *secnum_to_bfd_section (int, struct objfile *);

  176. struct find_targ_sec_arg
  177.   {
  178.     int targ_index;
  179.     int *resultp;
  180.     asection **bfd_sect;
  181.     struct objfile *objfile;
  182.   };

  183. static void find_targ_sec (bfd *, asection *, void *);

  184. static void
  185. find_targ_sec (bfd *abfd, asection *sect, void *obj)
  186. {
  187.   struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
  188.   struct objfile *objfile = args->objfile;

  189.   if (sect->target_index == args->targ_index)
  190.     {
  191.       /* This is the section.  Figure out what SECT_OFF_* code it is.  */
  192.       if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
  193.         *args->resultp = SECT_OFF_TEXT (objfile);
  194.       else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
  195.         *args->resultp = SECT_OFF_DATA (objfile);
  196.       else
  197.         *args->resultp = gdb_bfd_section_index (abfd, sect);
  198.       *args->bfd_sect = sect;
  199.     }
  200. }

  201. /* Search all BFD sections for the section whose target_index is
  202.    equal to N_SCNUM.  Set *BFD_SECT to that section.  The section's
  203.    associated index in the objfile's section_offset table is also
  204.    stored in *SECNUM.

  205.    If no match is found, *BFD_SECT is set to NULL, and *SECNUM
  206.    is set to the text section's number.  */

  207. static void
  208. xcoff_secnum_to_sections (int n_scnum, struct objfile *objfile,
  209.                           asection **bfd_sect, int *secnum)
  210. {
  211.   struct find_targ_sec_arg args;

  212.   args.targ_index = n_scnum;
  213.   args.resultp = secnum;
  214.   args.bfd_sect = bfd_sect;
  215.   args.objfile = objfile;

  216.   *bfd_sect = NULL;
  217.   *secnum = SECT_OFF_TEXT (objfile);

  218.   bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
  219. }

  220. /* Return the section number (SECT_OFF_*) that N_SCNUM points to.  */

  221. static int
  222. secnum_to_section (int n_scnum, struct objfile *objfile)
  223. {
  224.   int secnum;
  225.   asection *ignored;

  226.   xcoff_secnum_to_sections (n_scnum, objfile, &ignored, &secnum);
  227.   return secnum;
  228. }

  229. /* Return the BFD section that N_SCNUM points to.  */

  230. static asection *
  231. secnum_to_bfd_section (int n_scnum, struct objfile *objfile)
  232. {
  233.   int ignored;
  234.   asection *bfd_sect;

  235.   xcoff_secnum_to_sections (n_scnum, objfile, &bfd_sect, &ignored);
  236.   return bfd_sect;
  237. }

  238. /* add a given stab string into given stab vector.  */

  239. #if 0

  240. static void
  241. add_stab_to_list (char *stabname, struct pending_stabs **stabvector)
  242. {
  243.   if (*stabvector == NULL)
  244.     {
  245.       *stabvector = (struct pending_stabs *)
  246.         xmalloc (sizeof (struct pending_stabs) +
  247.                  INITIAL_STABVECTOR_LENGTH * sizeof (char *));
  248.       (*stabvector)->count = 0;
  249.       (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
  250.     }
  251.   else if ((*stabvector)->count >= (*stabvector)->length)
  252.     {
  253.       (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
  254.       *stabvector = (struct pending_stabs *)
  255.         xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) +
  256.                   (*stabvector)->length * sizeof (char *));
  257.     }
  258.   (*stabvector)->stab[(*stabvector)->count++] = stabname;
  259. }

  260. #endif
  261. /* *INDENT-OFF* */
  262. /* Linenos are processed on a file-by-file basis.

  263.    Two reasons:

  264.    1) xlc (IBM's native c compiler) postpones static function code
  265.    emission to the end of a compilation unit.  This way it can
  266.    determine if those functions (statics) are needed or not, and
  267.    can do some garbage collection (I think).  This makes line
  268.    numbers and corresponding addresses unordered, and we end up
  269.    with a line table like:


  270.    lineno       addr
  271.    foo()          10    0x100
  272.    20   0x200
  273.    30   0x300

  274.    foo3()         70    0x400
  275.    80   0x500
  276.    90   0x600

  277.    static foo2()
  278.    40   0x700
  279.    50   0x800
  280.    60   0x900

  281.    and that breaks gdb's binary search on line numbers, if the
  282.    above table is not sorted on line numbers.  And that sort
  283.    should be on function based, since gcc can emit line numbers
  284.    like:

  285.    10   0x100   - for the init/test part of a for stmt.
  286.    20   0x200
  287.    30   0x300
  288.    10   0x400   - for the increment part of a for stmt.

  289.    arrange_linetable() will do this sorting.

  290.    2)   aix symbol table might look like:

  291.    c_file               // beginning of a new file
  292.    .bi          // beginning of include file
  293.    .ei          // end of include file
  294.    .bi
  295.    .ei

  296.    basically, .bi/.ei pairs do not necessarily encapsulate
  297.    their scope.  They need to be recorded, and processed later
  298.    on when we come the end of the compilation unit.
  299.    Include table (inclTable) and process_linenos() handle
  300.    that.  */
  301. /* *INDENT-ON* */



  302. /* compare line table entry addresses.  */

  303. static int
  304. compare_lte (const void *lte1p, const void *lte2p)
  305. {
  306.   struct linetable_entry *lte1 = (struct linetable_entry *) lte1p;
  307.   struct linetable_entry *lte2 = (struct linetable_entry *) lte2p;

  308.   return lte1->pc - lte2->pc;
  309. }

  310. /* Given a line table with function entries are marked, arrange its
  311.    functions in ascending order and strip off function entry markers
  312.    and return it in a newly created table.  If the old one is good
  313.    enough, return the old one.  */
  314. /* FIXME: I think all this stuff can be replaced by just passing
  315.    sort_linevec = 1 to end_symtab.  */

  316. static struct linetable *
  317. arrange_linetable (struct linetable *oldLineTb)
  318. {
  319.   int ii, jj, newline,                /* new line count */
  320.     function_count;                /* # of functions */

  321.   struct linetable_entry *fentry;        /* function entry vector */
  322.   int fentry_size;                /* # of function entries */
  323.   struct linetable *newLineTb;        /* new line table */
  324.   int extra_lines = 0;

  325. #define NUM_OF_FUNCTIONS 20

  326.   fentry_size = NUM_OF_FUNCTIONS;
  327.   fentry = (struct linetable_entry *)
  328.     xmalloc (fentry_size * sizeof (struct linetable_entry));

  329.   for (function_count = 0, ii = 0; ii < oldLineTb->nitems; ++ii)
  330.     {
  331.       if (oldLineTb->item[ii].line == 0)
  332.         {                        /* Function entry found.  */
  333.           if (function_count >= fentry_size)
  334.             {                        /* Make sure you have room.  */
  335.               fentry_size *= 2;
  336.               fentry = (struct linetable_entry *)
  337.                 xrealloc (fentry,
  338.                           fentry_size * sizeof (struct linetable_entry));
  339.             }
  340.           fentry[function_count].line = ii;
  341.           fentry[function_count].pc = oldLineTb->item[ii].pc;
  342.           ++function_count;

  343.           /* If the function was compiled with XLC, we may have to add an
  344.              extra line entry later.  Reserve space for that.  */
  345.           if (ii + 1 < oldLineTb->nitems
  346.               && oldLineTb->item[ii].pc != oldLineTb->item[ii + 1].pc)
  347.             extra_lines++;
  348.         }
  349.     }

  350.   if (function_count == 0)
  351.     {
  352.       xfree (fentry);
  353.       return oldLineTb;
  354.     }
  355.   else if (function_count > 1)
  356.     qsort (fentry, function_count,
  357.            sizeof (struct linetable_entry), compare_lte);

  358.   /* Allocate a new line table.  */
  359.   newLineTb = (struct linetable *)
  360.     xmalloc
  361.     (sizeof (struct linetable) +
  362.     (oldLineTb->nitems - function_count + extra_lines) * sizeof (struct linetable_entry));

  363.   /* If line table does not start with a function beginning, copy up until
  364.      a function begin.  */

  365.   newline = 0;
  366.   if (oldLineTb->item[0].line != 0)
  367.     for (newline = 0;
  368.     newline < oldLineTb->nitems && oldLineTb->item[newline].line; ++newline)
  369.       newLineTb->item[newline] = oldLineTb->item[newline];

  370.   /* Now copy function lines one by one.  */

  371.   for (ii = 0; ii < function_count; ++ii)
  372.     {
  373.       /* If the function was compiled with XLC, we may have to add an
  374.          extra line to cover the function prologue.  */
  375.       jj = fentry[ii].line;
  376.       if (jj + 1 < oldLineTb->nitems
  377.           && oldLineTb->item[jj].pc != oldLineTb->item[jj + 1].pc)
  378.         {
  379.           newLineTb->item[newline] = oldLineTb->item[jj];
  380.           newLineTb->item[newline].line = oldLineTb->item[jj + 1].line;
  381.           newline++;
  382.         }

  383.       for (jj = fentry[ii].line + 1;
  384.            jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0;
  385.            ++jj, ++newline)
  386.         newLineTb->item[newline] = oldLineTb->item[jj];
  387.     }
  388.   xfree (fentry);
  389.   /* The number of items in the line table must include these
  390.      extra lines which were added in case of XLC compiled functions.  */
  391.   newLineTb->nitems = oldLineTb->nitems - function_count + extra_lines;
  392.   return newLineTb;
  393. }

  394. /* include file support: C_BINCL/C_EINCL pairs will be kept in the
  395.    following `IncludeChain'.  At the end of each symtab (end_symtab),
  396.    we will determine if we should create additional symtab's to
  397.    represent if (the include files.  */


  398. typedef struct _inclTable
  399. {
  400.   char *name;                        /* include filename */

  401.   /* Offsets to the line table.  end points to the last entry which is
  402.      part of this include file.  */
  403.   int begin, end;

  404.   struct subfile *subfile;
  405.   unsigned funStartLine;        /* Start line # of its function.  */
  406. }
  407. InclTable;

  408. #define        INITIAL_INCLUDE_TABLE_LENGTH        20
  409. static InclTable *inclTable;        /* global include table */
  410. static int inclIndx;                /* last entry to table */
  411. static int inclLength;                /* table length */
  412. static int inclDepth;                /* nested include depth */

  413. static void allocate_include_entry (void);

  414. static void
  415. record_include_begin (struct coff_symbol *cs)
  416. {
  417.   if (inclDepth)
  418.     {
  419.       /* In xcoff, we assume include files cannot be nested (not in .c files
  420.          of course, but in corresponding .s files.).  */

  421.       /* This can happen with old versions of GCC.
  422.          GCC 2.3.3-930426 does not exhibit this on a test case which
  423.          a user said produced the message for him.  */
  424.       complaint (&symfile_complaints, _("Nested C_BINCL symbols"));
  425.     }
  426.   ++inclDepth;

  427.   allocate_include_entry ();

  428.   inclTable[inclIndx].name = cs->c_name;
  429.   inclTable[inclIndx].begin = cs->c_value;
  430. }

  431. static void
  432. record_include_end (struct coff_symbol *cs)
  433. {
  434.   InclTable *pTbl;

  435.   if (inclDepth == 0)
  436.     {
  437.       complaint (&symfile_complaints, _("Mismatched C_BINCL/C_EINCL pair"));
  438.     }

  439.   allocate_include_entry ();

  440.   pTbl = &inclTable[inclIndx];
  441.   pTbl->end = cs->c_value;

  442.   --inclDepth;
  443.   ++inclIndx;
  444. }

  445. static void
  446. allocate_include_entry (void)
  447. {
  448.   if (inclTable == NULL)
  449.     {
  450.       inclTable = (InclTable *)
  451.         xmalloc (sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
  452.       memset (inclTable,
  453.               '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
  454.       inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
  455.       inclIndx = 0;
  456.     }
  457.   else if (inclIndx >= inclLength)
  458.     {
  459.       inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
  460.       inclTable = (InclTable *)
  461.         xrealloc (inclTable, sizeof (InclTable) * inclLength);
  462.       memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH,
  463.               '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
  464.     }
  465. }

  466. /* Global variable to pass the psymtab down to all the routines involved
  467.    in psymtab to symtab processing.  */
  468. static struct partial_symtab *this_symtab_psymtab;

  469. /* Objfile related to this_symtab_psymtab; set at the same time.  */
  470. static struct objfile *this_symtab_objfile;

  471. /* given the start and end addresses of a compilation unit (or a csect,
  472.    at times) process its lines and create appropriate line vectors.  */

  473. static void
  474. process_linenos (CORE_ADDR start, CORE_ADDR end)
  475. {
  476.   int offset, ii;
  477.   file_ptr max_offset
  478.     = XCOFF_DATA (this_symtab_objfile)->max_lineno_offset;

  479.   /* subfile structure for the main compilation unit.  */
  480.   struct subfile main_subfile;

  481.   /* In the main source file, any time we see a function entry, we
  482.      reset this variable to function's absolute starting line number.
  483.      All the following line numbers in the function are relative to
  484.      this, and we record absolute line numbers in record_line().  */

  485.   unsigned int main_source_baseline = 0;

  486.   unsigned *firstLine;

  487.   offset =
  488.     ((struct symloc *) this_symtab_psymtab->read_symtab_private)->lineno_off;
  489.   if (offset == 0)
  490.     goto return_after_cleanup;

  491.   memset (&main_subfile, '\0', sizeof (main_subfile));

  492.   if (inclIndx == 0)
  493.     /* All source lines were in the main source file.  None in include
  494.        files.  */

  495.     enter_line_range (&main_subfile, offset, 0, start, end,
  496.                       &main_source_baseline);

  497.   else
  498.     {
  499.       /* There was source with line numbers in include files.  */

  500.       int linesz =
  501.         coff_data (this_symtab_objfile->obfd)->local_linesz;
  502.       main_source_baseline = 0;

  503.       for (ii = 0; ii < inclIndx; ++ii)
  504.         {
  505.           struct subfile *tmpSubfile;

  506.           /* If there is main file source before include file, enter it.  */
  507.           if (offset < inclTable[ii].begin)
  508.             {
  509.               enter_line_range
  510.                 (&main_subfile, offset, inclTable[ii].begin - linesz,
  511.                  start, 0, &main_source_baseline);
  512.             }

  513.           if (strcmp (inclTable[ii].name, get_last_source_file ()) == 0)
  514.             {
  515.               /* The entry in the include table refers to the main source
  516.                  file.  Add the lines to the main subfile.  */

  517.               main_source_baseline = inclTable[ii].funStartLine;
  518.               enter_line_range
  519.                 (&main_subfile, inclTable[ii].begin, inclTable[ii].end,
  520.                  start, 0, &main_source_baseline);
  521.               inclTable[ii].subfile = &main_subfile;
  522.             }
  523.           else
  524.             {
  525.               /* Have a new subfile for the include file.  */

  526.               tmpSubfile = inclTable[ii].subfile =
  527.                 (struct subfile *) xmalloc (sizeof (struct subfile));

  528.               memset (tmpSubfile, '\0', sizeof (struct subfile));
  529.               firstLine = &(inclTable[ii].funStartLine);

  530.               /* Enter include file's lines now.  */
  531.               enter_line_range (tmpSubfile, inclTable[ii].begin,
  532.                                 inclTable[ii].end, start, 0, firstLine);
  533.             }

  534.           if (offset <= inclTable[ii].end)
  535.             offset = inclTable[ii].end + linesz;
  536.         }

  537.       /* All the include files' line have been processed at this point.  Now,
  538.          enter remaining lines of the main file, if any left.  */
  539.       if (offset < max_offset + 1 - linesz)
  540.         {
  541.           enter_line_range (&main_subfile, offset, 0, start, end,
  542.                             &main_source_baseline);
  543.         }
  544.     }

  545.   /* Process main file's line numbers.  */
  546.   if (main_subfile.line_vector)
  547.     {
  548.       struct linetable *lineTb, *lv;

  549.       lv = main_subfile.line_vector;

  550.       /* Line numbers are not necessarily ordered.  xlc compilation will
  551.          put static function to the end.  */

  552.       lineTb = arrange_linetable (lv);
  553.       if (lv == lineTb)
  554.         {
  555.           current_subfile->line_vector = (struct linetable *)
  556.             xrealloc (lv, (sizeof (struct linetable)
  557.                            + lv->nitems * sizeof (struct linetable_entry)));
  558.         }
  559.       else
  560.         {
  561.           xfree (lv);
  562.           current_subfile->line_vector = lineTb;
  563.         }

  564.       current_subfile->line_vector_length =
  565.         current_subfile->line_vector->nitems;
  566.     }

  567.   /* Now, process included files' line numbers.  */

  568.   for (ii = 0; ii < inclIndx; ++ii)
  569.     {
  570.       if (inclTable[ii].subfile != ((struct subfile *) &main_subfile)
  571.           && (inclTable[ii].subfile)->line_vector)        /* Useless if!!!
  572.                                                            FIXMEmgo */
  573.         {
  574.           struct linetable *lineTb, *lv;

  575.           lv = (inclTable[ii].subfile)->line_vector;

  576.           /* Line numbers are not necessarily ordered.  xlc compilation will
  577.              put static function to the end.  */

  578.           lineTb = arrange_linetable (lv);

  579.           push_subfile ();

  580.           /* For the same include file, we might want to have more than one
  581.              subfile.  This happens if we have something like:

  582.              ......
  583.              #include "foo.h"
  584.              ......
  585.              #include "foo.h"
  586.              ......

  587.              while foo.h including code in it.  (stupid but possible)
  588.              Since start_subfile() looks at the name and uses an
  589.              existing one if finds, we need to provide a fake name and
  590.              fool it.  */

  591. #if 0
  592.           start_subfile (inclTable[ii].name);
  593. #else
  594.           {
  595.             /* Pick a fake name that will produce the same results as this
  596.                one when passed to deduce_language_from_filename.  Kludge on
  597.                top of kludge.  */
  598.             char *fakename = strrchr (inclTable[ii].name, '.');

  599.             if (fakename == NULL)
  600.               fakename = " ?";
  601.             start_subfile (fakename);
  602.             xfree (current_subfile->name);
  603.           }
  604.           current_subfile->name = xstrdup (inclTable[ii].name);
  605. #endif

  606.           if (lv == lineTb)
  607.             {
  608.               current_subfile->line_vector =
  609.                 (struct linetable *) xrealloc
  610.                 (lv, (sizeof (struct linetable)
  611.                       + lv->nitems * sizeof (struct linetable_entry)));

  612.             }
  613.           else
  614.             {
  615.               xfree (lv);
  616.               current_subfile->line_vector = lineTb;
  617.             }

  618.           current_subfile->line_vector_length =
  619.             current_subfile->line_vector->nitems;
  620.           start_subfile (pop_subfile ());
  621.         }
  622.     }

  623. return_after_cleanup:

  624.   /* We don't want to keep alloc/free'ing the global include file table.  */
  625.   inclIndx = 0;
  626. }

  627. static void
  628. aix_process_linenos (struct objfile *objfile)
  629. {
  630.   /* There is no linenos to read if there are only dwarf info.  */
  631.   if (this_symtab_psymtab == NULL)
  632.     return;

  633.   /* Process line numbers and enter them into line vector.  */
  634.   process_linenos (last_source_start_addr, cur_src_end_addr);
  635. }


  636. /* Enter a given range of lines into the line vector.
  637.    can be called in the following two ways:
  638.    enter_line_range (subfile, beginoffset, endoffset,
  639.                      startaddr, 0, firstLine)  or
  640.    enter_line_range (subfile, beginoffset, 0,
  641.                      startaddr, endaddr, firstLine)

  642.    endoffset points to the last line table entry that we should pay
  643.    attention to.  */

  644. static void
  645. enter_line_range (struct subfile *subfile, unsigned beginoffset,
  646.                   unsigned endoffset,        /* offsets to line table */
  647.                   CORE_ADDR startaddr,        /* offsets to line table */
  648.                   CORE_ADDR endaddr, unsigned *firstLine)
  649. {
  650.   struct objfile *objfile = this_symtab_objfile;
  651.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  652.   unsigned int curoffset;
  653.   CORE_ADDR addr;
  654.   void *ext_lnno;
  655.   struct internal_lineno int_lnno;
  656.   unsigned int limit_offset;
  657.   bfd *abfd;
  658.   int linesz;

  659.   if (endoffset == 0 && startaddr == 0 && endaddr == 0)
  660.     return;
  661.   curoffset = beginoffset;
  662.   limit_offset = XCOFF_DATA (objfile)->max_lineno_offset;

  663.   if (endoffset != 0)
  664.     {
  665.       if (endoffset >= limit_offset)
  666.         {
  667.           complaint (&symfile_complaints,
  668.                      _("Bad line table offset in C_EINCL directive"));
  669.           return;
  670.         }
  671.       limit_offset = endoffset;
  672.     }
  673.   else
  674.     limit_offset -= 1;

  675.   abfd = objfile->obfd;
  676.   linesz = coff_data (abfd)->local_linesz;
  677.   ext_lnno = alloca (linesz);

  678.   while (curoffset <= limit_offset)
  679.     {
  680.       bfd_seek (abfd, curoffset, SEEK_SET);
  681.       bfd_bread (ext_lnno, linesz, abfd);
  682.       bfd_coff_swap_lineno_in (abfd, ext_lnno, &int_lnno);

  683.       /* Find the address this line represents.  */
  684.       addr = (int_lnno.l_lnno
  685.               ? int_lnno.l_addr.l_paddr
  686.               : read_symbol_nvalue (int_lnno.l_addr.l_symndx));
  687.       addr += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  688.       if (addr < startaddr || (endaddr && addr >= endaddr))
  689.         return;

  690.       if (int_lnno.l_lnno == 0)
  691.         {
  692.           *firstLine = read_symbol_lineno (int_lnno.l_addr.l_symndx);
  693.           record_line (subfile, 0, gdbarch_addr_bits_remove (gdbarch, addr));
  694.           --(*firstLine);
  695.         }
  696.       else
  697.         record_line (subfile, *firstLine + int_lnno.l_lnno,
  698.                      gdbarch_addr_bits_remove (gdbarch, addr));
  699.       curoffset += linesz;
  700.     }
  701. }


  702. /* Save the vital information for use when closing off the current file.
  703.    NAME is the file name the symbols came from, START_ADDR is the first
  704.    text address for the file, and SIZE is the number of bytes of text.  */

  705. #define complete_symtab(name, start_addr) {        \
  706.   set_last_source_file (name);                        \
  707.   last_source_start_addr = start_addr;                \
  708. }


  709. /* Refill the symbol table input buffer
  710.    and set the variables that control fetching entries from it.
  711.    Reports an error if no data available.
  712.    This function can read past the end of the symbol table
  713.    (into the string table) but this does no harm.  */

  714. /* Create a new minimal symbol (using prim_record_minimal_symbol_and_info).

  715.    Creation of all new minimal symbols should go through this function
  716.    rather than calling the various prim_record_[...] functions in order
  717.    to make sure that all symbol addresses get properly relocated.

  718.    Arguments are:

  719.    NAME - the symbol's name (but if NAME starts with a period, that
  720.    leading period is discarded).
  721.    ADDRESS - the symbol's address, prior to relocation.  This function
  722.       relocates the address before recording the minimal symbol.
  723.    MS_TYPE - the symbol's type.
  724.    N_SCNUM - the symbol's XCOFF section number.
  725.    OBJFILE - the objfile associated with the minimal symbol.  */

  726. static void
  727. record_minimal_symbol (const char *name, CORE_ADDR address,
  728.                        enum minimal_symbol_type ms_type,
  729.                        int n_scnum,
  730.                        struct objfile *objfile)
  731. {
  732.   int section = secnum_to_section (n_scnum, objfile);

  733.   if (name[0] == '.')
  734.     ++name;

  735.   prim_record_minimal_symbol_and_info (name, address, ms_type,
  736.                                        secnum_to_section (n_scnum, objfile),
  737.                                        objfile);
  738. }

  739. /* xcoff has static blocks marked in `.bs', `.es' pairs.  They cannot be
  740.    nested.  At any given time, a symbol can only be in one static block.
  741.    This is the base address of current static block, zero if non exists.  */

  742. static int static_block_base = 0;

  743. /* Section number for the current static block.  */

  744. static int static_block_section = -1;

  745. /* true if space for symbol name has been allocated.  */

  746. static int symname_alloced = 0;

  747. /* Next symbol to read.  Pointer into raw seething symbol table.  */

  748. static char *raw_symbol;

  749. /* This is the function which stabsread.c calls to get symbol
  750.    continuations.  */

  751. static char *
  752. xcoff_next_symbol_text (struct objfile *objfile)
  753. {
  754.   struct internal_syment symbol;
  755.   char *retval;

  756.   /* FIXME: is this the same as the passed arg?  */
  757.   if (this_symtab_objfile)
  758.     objfile = this_symtab_objfile;

  759.   bfd_coff_swap_sym_in (objfile->obfd, raw_symbol, &symbol);
  760.   if (symbol.n_zeroes)
  761.     {
  762.       complaint (&symfile_complaints, _("Unexpected symbol continuation"));

  763.       /* Return something which points to '\0' and hope the symbol reading
  764.          code does something reasonable.  */
  765.       retval = "";
  766.     }
  767.   else if (symbol.n_sclass & 0x80)
  768.     {
  769.       retval = XCOFF_DATA (objfile)->debugsec + symbol.n_offset;
  770.       raw_symbol += coff_data (objfile->obfd)->local_symesz;
  771.       ++symnum;
  772.     }
  773.   else
  774.     {
  775.       complaint (&symfile_complaints, _("Unexpected symbol continuation"));

  776.       /* Return something which points to '\0' and hope the symbol reading
  777.          code does something reasonable.  */
  778.       retval = "";
  779.     }
  780.   return retval;
  781. }

  782. /* Read symbols for a given partial symbol table.  */

  783. static void
  784. read_xcoff_symtab (struct objfile *objfile, struct partial_symtab *pst)
  785. {
  786.   bfd *abfd = objfile->obfd;
  787.   char *raw_auxptr;                /* Pointer to first raw aux entry for sym.  */
  788.   struct coff_symfile_info *xcoff = XCOFF_DATA (objfile);
  789.   char *strtbl = xcoff->strtbl;
  790.   char *debugsec = xcoff->debugsec;
  791.   const char *debugfmt = bfd_xcoff_is_xcoff64 (abfd) ? "XCOFF64" : "XCOFF";

  792.   struct internal_syment symbol[1];
  793.   union internal_auxent main_aux;
  794.   struct coff_symbol cs[1];
  795.   CORE_ADDR file_start_addr = 0;
  796.   CORE_ADDR file_end_addr = 0;

  797.   int next_file_symnum = -1;
  798.   unsigned int max_symnum;
  799.   int just_started = 1;
  800.   int depth = 0;
  801.   CORE_ADDR fcn_start_addr = 0;

  802.   struct coff_symbol fcn_stab_saved = { 0 };

  803.   /* fcn_cs_saved is global because process_xcoff_symbol needs it.  */
  804.   union internal_auxent fcn_aux_saved = main_aux;
  805.   struct context_stack *new;

  806.   char *filestring = " _start_ ";        /* Name of the current file.  */

  807.   const char *last_csect_name;        /* Last seen csect's name.  */

  808.   this_symtab_psymtab = pst;
  809.   this_symtab_objfile = objfile;

  810.   /* Get the appropriate COFF "constants" related to the file we're
  811.      handling.  */
  812.   local_symesz = coff_data (abfd)->local_symesz;

  813.   set_last_source_file (NULL);
  814.   last_csect_name = 0;

  815.   start_stabs ();
  816.   start_symtab (objfile, filestring, (char *) NULL, file_start_addr);
  817.   record_debugformat (debugfmt);
  818.   symnum = ((struct symloc *) pst->read_symtab_private)->first_symnum;
  819.   max_symnum =
  820.     symnum + ((struct symloc *) pst->read_symtab_private)->numsyms;
  821.   first_object_file_end = 0;

  822.   raw_symbol = xcoff->symtbl + symnum * local_symesz;

  823.   while (symnum < max_symnum)
  824.     {
  825.       QUIT;                        /* make this command interruptable.  */

  826.       /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
  827.       /* read one symbol into `cs' structure.  After processing the
  828.          whole symbol table, only string table will be kept in memory,
  829.          symbol table and debug section of xcoff will be freed.  Thus
  830.          we can mark symbols with names in string table as
  831.          `alloced'.  */
  832.       {
  833.         int ii;

  834.         /* Swap and align the symbol into a reasonable C structure.  */
  835.         bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);

  836.         cs->c_symnum = symnum;
  837.         cs->c_naux = symbol->n_numaux;
  838.         if (symbol->n_zeroes)
  839.           {
  840.             symname_alloced = 0;
  841.             /* We must use the original, unswapped, name here so the name field
  842.                pointed to by cs->c_name will persist throughout xcoffread.  If
  843.                we use the new field, it gets overwritten for each symbol.  */
  844.             cs->c_name = ((struct external_syment *) raw_symbol)->e.e_name;
  845.             /* If it's exactly E_SYMNMLEN characters long it isn't
  846.                '\0'-terminated.  */
  847.             if (cs->c_name[E_SYMNMLEN - 1] != '\0')
  848.               {
  849.                 char *p;

  850.                 p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
  851.                 strncpy (p, cs->c_name, E_SYMNMLEN);
  852.                 p[E_SYMNMLEN] = '\0';
  853.                 cs->c_name = p;
  854.                 symname_alloced = 1;
  855.               }
  856.           }
  857.         else if (symbol->n_sclass & 0x80)
  858.           {
  859.             cs->c_name = debugsec + symbol->n_offset;
  860.             symname_alloced = 0;
  861.           }
  862.         else
  863.           {
  864.             /* in string table */
  865.             cs->c_name = strtbl + (int) symbol->n_offset;
  866.             symname_alloced = 1;
  867.           }
  868.         cs->c_value = symbol->n_value;
  869.         cs->c_sclass = symbol->n_sclass;
  870.         cs->c_secnum = symbol->n_scnum;
  871.         cs->c_type = (unsigned) symbol->n_type;

  872.         raw_symbol += local_symesz;
  873.         ++symnum;

  874.         /* Save addr of first aux entry.  */
  875.         raw_auxptr = raw_symbol;

  876.         /* Skip all the auxents associated with this symbol.  */
  877.         for (ii = symbol->n_numaux; ii; --ii)
  878.           {
  879.             raw_symbol += coff_data (abfd)->local_auxesz;
  880.             ++symnum;
  881.           }
  882.       }

  883.       /* if symbol name starts with ".$" or "$", ignore it.  */
  884.       if (cs->c_name[0] == '$'
  885.           || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
  886.         continue;

  887.       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
  888.         {
  889.           if (get_last_source_file ())
  890.             {
  891.               pst->compunit_symtab = end_symtab (cur_src_end_addr,
  892.                                                  SECT_OFF_TEXT (objfile));
  893.               end_stabs ();
  894.             }

  895.           start_stabs ();
  896.           start_symtab (objfile, "_globals_", (char *) NULL, (CORE_ADDR) 0);
  897.           record_debugformat (debugfmt);
  898.           cur_src_end_addr = first_object_file_end;
  899.           /* Done with all files, everything from here on is globals.  */
  900.         }

  901.       if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
  902.           && cs->c_naux == 1)
  903.         {
  904.           /* Dealing with a symbol with a csect entry.  */

  905. #define        CSECT(PP) ((PP)->x_csect)
  906. #define        CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
  907. #define        CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
  908. #define        CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
  909. #define        CSECT_SCLAS(PP) (CSECT(PP).x_smclas)

  910.           /* Convert the auxent to something we can access.  */
  911.           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  912.                                 0, cs->c_naux, &main_aux);

  913.           switch (CSECT_SMTYP (&main_aux))
  914.             {

  915.             case XTY_ER:
  916.               /* Ignore all external references.  */
  917.               continue;

  918.             case XTY_SD:
  919.               /* A section description.  */
  920.               {
  921.                 switch (CSECT_SCLAS (&main_aux))
  922.                   {

  923.                   case XMC_PR:
  924.                     {

  925.                       /* A program csect is seen.  We have to allocate one
  926.                          symbol table for each program csect.  Normally gdb
  927.                          prefers one symtab for each source file.  In case
  928.                          of AIX, one source file might include more than one
  929.                          [PR] csect, and they don't have to be adjacent in
  930.                          terms of the space they occupy in memory.  Thus, one
  931.                          single source file might get fragmented in the
  932.                          memory and gdb's file start and end address
  933.                          approach does not work!  GCC (and I think xlc) seem
  934.                          to put all the code in the unnamed program csect.  */

  935.                       if (last_csect_name)
  936.                         {
  937.                           complete_symtab (filestring, file_start_addr);
  938.                           cur_src_end_addr = file_end_addr;
  939.                           end_symtab (file_end_addr, SECT_OFF_TEXT (objfile));
  940.                           end_stabs ();
  941.                           start_stabs ();
  942.                           /* Give all csects for this source file the same
  943.                              name.  */
  944.                           start_symtab (objfile, filestring, NULL,
  945.                                         (CORE_ADDR) 0);
  946.                           record_debugformat (debugfmt);
  947.                         }

  948.                       /* If this is the very first csect seen,
  949.                          basically `__start'.  */
  950.                       if (just_started)
  951.                         {
  952.                           first_object_file_end
  953.                             = cs->c_value + CSECT_LEN (&main_aux);
  954.                           just_started = 0;
  955.                         }

  956.                       file_start_addr =
  957.                         cs->c_value + ANOFFSET (objfile->section_offsets,
  958.                                                 SECT_OFF_TEXT (objfile));
  959.                       file_end_addr = file_start_addr + CSECT_LEN (&main_aux);

  960.                       if (cs->c_name && (cs->c_name[0] == '.' || cs->c_name[0] == '@'))
  961.                         last_csect_name = cs->c_name;
  962.                     }
  963.                     continue;

  964.                     /* All other symbols are put into the minimal symbol
  965.                        table only.  */

  966.                   case XMC_RW:
  967.                     continue;

  968.                   case XMC_TC0:
  969.                     continue;

  970.                   case XMC_TC:
  971.                     continue;

  972.                   default:
  973.                     /* Ignore the symbol.  */
  974.                     continue;
  975.                   }
  976.               }
  977.               break;

  978.             case XTY_LD:

  979.               switch (CSECT_SCLAS (&main_aux))
  980.                 {
  981.                 case XMC_PR:
  982.                   /* a function entry point.  */
  983.                 function_entry_point:

  984.                   fcn_start_addr = cs->c_value;

  985.                   /* save the function header info, which will be used
  986.                      when `.bf' is seen.  */
  987.                   fcn_cs_saved = *cs;
  988.                   fcn_aux_saved = main_aux;
  989.                   continue;

  990.                 case XMC_GL:
  991.                   /* shared library function trampoline code entry point.  */
  992.                   continue;

  993.                 case XMC_DS:
  994.                   /* The symbols often have the same names as debug symbols for
  995.                      functions, and confuse lookup_symbol.  */
  996.                   continue;

  997.                 default:
  998.                   /* xlc puts each variable in a separate csect, so we get
  999.                      an XTY_SD for each variable.  But gcc puts several
  1000.                      variables in a csect, so that each variable only gets
  1001.                      an XTY_LD.  This will typically be XMC_RW; I suspect
  1002.                      XMC_RO and XMC_BS might be possible too.
  1003.                      These variables are put in the minimal symbol table
  1004.                      only.  */
  1005.                   continue;
  1006.                 }
  1007.               break;

  1008.             case XTY_CM:
  1009.               /* Common symbols are put into the minimal symbol table only.  */
  1010.               continue;

  1011.             default:
  1012.               break;
  1013.             }
  1014.         }

  1015.       /* If explicitly specified as a function, treat is as one.  This check
  1016.          evaluates to true for @FIX* bigtoc CSECT symbols, so it must occur
  1017.          after the above CSECT check.  */
  1018.       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
  1019.         {
  1020.           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  1021.                                 0, cs->c_naux, &main_aux);
  1022.           goto function_entry_point;
  1023.         }

  1024.       switch (cs->c_sclass)
  1025.         {
  1026.         case C_FILE:

  1027.           /* c_value field contains symnum of next .file entry in table
  1028.              or symnum of first global after last .file.  */

  1029.           next_file_symnum = cs->c_value;

  1030.           /* Complete symbol table for last object file containing
  1031.              debugging information.  */

  1032.           /* Whether or not there was a csect in the previous file, we
  1033.              have to call `end_stabs' and `start_stabs' to reset
  1034.              type_vector, line_vector, etc. structures.  */

  1035.           complete_symtab (filestring, file_start_addr);
  1036.           cur_src_end_addr = file_end_addr;
  1037.           end_symtab (file_end_addr, SECT_OFF_TEXT (objfile));
  1038.           end_stabs ();

  1039.           /* XCOFF, according to the AIX 3.2 documentation, puts the
  1040.              filename in cs->c_name.  But xlc 1.3.0.2 has decided to
  1041.              do things the standard COFF way and put it in the auxent.
  1042.              We use the auxent if the symbol is ".file" and an auxent
  1043.              exists, otherwise use the symbol itself.  Simple
  1044.              enough.  */
  1045.           if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
  1046.             {
  1047.               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  1048.                                     0, cs->c_naux, &main_aux);
  1049.               filestring = coff_getfilename (&main_aux, objfile);
  1050.             }
  1051.           else
  1052.             filestring = cs->c_name;

  1053.           start_stabs ();
  1054.           start_symtab (objfile, filestring, (char *) NULL, (CORE_ADDR) 0);
  1055.           record_debugformat (debugfmt);
  1056.           last_csect_name = 0;

  1057.           /* reset file start and end addresses.  A compilation unit
  1058.              with no text (only data) should have zero file
  1059.              boundaries.  */
  1060.           file_start_addr = file_end_addr = 0;
  1061.           break;

  1062.         case C_FUN:
  1063.           fcn_stab_saved = *cs;
  1064.           break;

  1065.         case C_FCN:
  1066.           if (strcmp (cs->c_name, ".bf") == 0)
  1067.             {
  1068.               CORE_ADDR off = ANOFFSET (objfile->section_offsets,
  1069.                                         SECT_OFF_TEXT (objfile));

  1070.               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  1071.                                     0, cs->c_naux, &main_aux);

  1072.               within_function = 1;

  1073.               new = push_context (0, fcn_start_addr + off);

  1074.               new->name = define_symbol
  1075.                 (fcn_cs_saved.c_value + off,
  1076.                  fcn_stab_saved.c_name, 0, 0, objfile);
  1077.               if (new->name != NULL)
  1078.                 SYMBOL_SECTION (new->name) = SECT_OFF_TEXT (objfile);
  1079.             }
  1080.           else if (strcmp (cs->c_name, ".ef") == 0)
  1081.             {
  1082.               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
  1083.                                     0, cs->c_naux, &main_aux);

  1084.               /* The value of .ef is the address of epilogue code;
  1085.                  not useful for gdb.  */
  1086.               /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
  1087.                  contains number of lines to '}' */

  1088.               if (context_stack_depth <= 0)
  1089.                 {        /* We attempted to pop an empty context stack.  */
  1090.                   ef_complaint (cs->c_symnum);
  1091.                   within_function = 0;
  1092.                   break;
  1093.                 }
  1094.               new = pop_context ();
  1095.               /* Stack must be empty now.  */
  1096.               if (context_stack_depth > 0 || new == NULL)
  1097.                 {
  1098.                   ef_complaint (cs->c_symnum);
  1099.                   within_function = 0;
  1100.                   break;
  1101.                 }

  1102.               finish_block (new->name, &local_symbols, new->old_blocks,
  1103.                             new->start_addr,
  1104.                             (fcn_cs_saved.c_value
  1105.                              + fcn_aux_saved.x_sym.x_misc.x_fsize
  1106.                              + ANOFFSET (objfile->section_offsets,
  1107.                                          SECT_OFF_TEXT (objfile))));
  1108.               within_function = 0;
  1109.             }
  1110.           break;

  1111.         case C_BSTAT:
  1112.           /* Begin static block.  */
  1113.           {
  1114.             struct internal_syment symbol;

  1115.             read_symbol (&symbol, cs->c_value);
  1116.             static_block_base = symbol.n_value;
  1117.             static_block_section =
  1118.               secnum_to_section (symbol.n_scnum, objfile);
  1119.           }
  1120.           break;

  1121.         case C_ESTAT:
  1122.           /* End of static block.  */
  1123.           static_block_base = 0;
  1124.           static_block_section = -1;
  1125.           break;

  1126.         case C_ARG:
  1127.         case C_REGPARM:
  1128.         case C_REG:
  1129.         case C_TPDEF:
  1130.         case C_STRTAG:
  1131.         case C_UNTAG:
  1132.         case C_ENTAG:
  1133.           {
  1134.             complaint (&symfile_complaints,
  1135.                        _("Unrecognized storage class %d."),
  1136.                        cs->c_sclass);
  1137.           }
  1138.           break;

  1139.         case C_LABEL:
  1140.         case C_NULL:
  1141.           /* Ignore these.  */
  1142.           break;

  1143.         case C_HIDEXT:
  1144.         case C_STAT:
  1145.           break;

  1146.         case C_BINCL:
  1147.           /* beginning of include file */
  1148.           /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
  1149.              order.  Thus, when wee see them, we might not know enough info
  1150.              to process them.  Thus, we'll be saving them into a table
  1151.              (inclTable) and postpone their processing.  */

  1152.           record_include_begin (cs);
  1153.           break;

  1154.         case C_EINCL:
  1155.           /* End of include file.  */
  1156.           /* See the comment after case C_BINCL.  */
  1157.           record_include_end (cs);
  1158.           break;

  1159.         case C_BLOCK:
  1160.           if (strcmp (cs->c_name, ".bb") == 0)
  1161.             {
  1162.               depth++;
  1163.               new = push_context (depth,
  1164.                                   (cs->c_value
  1165.                                    + ANOFFSET (objfile->section_offsets,
  1166.                                                SECT_OFF_TEXT (objfile))));
  1167.             }
  1168.           else if (strcmp (cs->c_name, ".eb") == 0)
  1169.             {
  1170.               if (context_stack_depth <= 0)
  1171.                 {        /* We attempted to pop an empty context stack.  */
  1172.                   eb_complaint (cs->c_symnum);
  1173.                   break;
  1174.                 }
  1175.               new = pop_context ();
  1176.               if (depth-- != new->depth)
  1177.                 {
  1178.                   eb_complaint (cs->c_symnum);
  1179.                   break;
  1180.                 }
  1181.               if (local_symbols && context_stack_depth > 0)
  1182.                 {
  1183.                   /* Make a block for the local symbols within.  */
  1184.                   finish_block (new->name, &local_symbols, new->old_blocks,
  1185.                                 new->start_addr,
  1186.                                 (cs->c_value
  1187.                                  + ANOFFSET (objfile->section_offsets,
  1188.                                              SECT_OFF_TEXT (objfile))));
  1189.                 }
  1190.               local_symbols = new->locals;
  1191.             }
  1192.           break;

  1193.         default:
  1194.           process_xcoff_symbol (cs, objfile);
  1195.           break;
  1196.         }
  1197.     }

  1198.   if (get_last_source_file ())
  1199.     {
  1200.       struct compunit_symtab *cust;

  1201.       complete_symtab (filestring, file_start_addr);
  1202.       cur_src_end_addr = file_end_addr;
  1203.       cust = end_symtab (file_end_addr, SECT_OFF_TEXT (objfile));
  1204.       /* When reading symbols for the last C_FILE of the objfile, try
  1205.          to make sure that we set pst->compunit_symtab to the symtab for the
  1206.          file, not to the _globals_ symtabI'm not sure whether this
  1207.          actually works right or when/if it comes up.  */
  1208.       if (pst->compunit_symtab == NULL)
  1209.         pst->compunit_symtab = cust;
  1210.       end_stabs ();
  1211.     }
  1212. }

  1213. #define        SYMBOL_DUP(SYMBOL1, SYMBOL2)        \
  1214.   (SYMBOL2) = (struct symbol *)                \
  1215.           obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol)); \
  1216.   *(SYMBOL2) = *(SYMBOL1);


  1217. #define        SYMNAME_ALLOC(NAME, ALLOCED)        \
  1218.   ((ALLOCED) ? (NAME) : obstack_copy0 (&objfile->objfile_obstack, \
  1219.                                        (NAME), strlen (NAME)))


  1220. /* process one xcoff symbol.  */

  1221. static struct symbol *
  1222. process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)
  1223. {
  1224.   struct symbol onesymbol;
  1225.   struct symbol *sym = &onesymbol;
  1226.   struct symbol *sym2 = NULL;
  1227.   char *name, *pp;

  1228.   int sec;
  1229.   CORE_ADDR off;

  1230.   if (cs->c_secnum < 0)
  1231.     {
  1232.       /* The value is a register number, offset within a frame, etc.,
  1233.          and does not get relocated.  */
  1234.       off = 0;
  1235.       sec = -1;
  1236.     }
  1237.   else
  1238.     {
  1239.       sec = secnum_to_section (cs->c_secnum, objfile);
  1240.       off = ANOFFSET (objfile->section_offsets, sec);
  1241.     }

  1242.   name = cs->c_name;
  1243.   if (name[0] == '.')
  1244.     ++name;

  1245.   initialize_objfile_symbol (sym);

  1246.   /* default assumptions */
  1247.   SYMBOL_VALUE_ADDRESS (sym) = cs->c_value + off;
  1248.   SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
  1249.   SYMBOL_SECTION (sym) = secnum_to_section (cs->c_secnum, objfile);

  1250.   if (ISFCN (cs->c_type))
  1251.     {
  1252.       /* At this point, we don't know the type of the function.  This
  1253.          will be patched with the type from its stab entry later on in
  1254.          patch_block_stabs (), unless the file was compiled without -g.  */

  1255.       SYMBOL_SET_LINKAGE_NAME (sym, SYMNAME_ALLOC (name, symname_alloced));
  1256.       SYMBOL_TYPE (sym) = objfile_type (objfile)->nodebug_text_symbol;

  1257.       SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
  1258.       SYMBOL_DUP (sym, sym2);

  1259.       if (cs->c_sclass == C_EXT)
  1260.         add_symbol_to_list (sym2, &global_symbols);
  1261.       else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
  1262.         add_symbol_to_list (sym2, &file_symbols);
  1263.     }
  1264.   else
  1265.     {
  1266.       /* In case we can't figure out the type, provide default.  */
  1267.       SYMBOL_TYPE (sym) = objfile_type (objfile)->nodebug_data_symbol;

  1268.       switch (cs->c_sclass)
  1269.         {
  1270. #if 0
  1271.           /* The values of functions and global symbols are now resolved
  1272.              via the global_sym_chain in stabsread.c.  */
  1273.         case C_FUN:
  1274.           if (fcn_cs_saved.c_sclass == C_EXT)
  1275.             add_stab_to_list (name, &global_stabs);
  1276.           else
  1277.             add_stab_to_list (name, &file_stabs);
  1278.           break;

  1279.         case C_GSYM:
  1280.           add_stab_to_list (name, &global_stabs);
  1281.           break;
  1282. #endif

  1283.         case C_BCOMM:
  1284.           common_block_start (cs->c_name, objfile);
  1285.           break;

  1286.         case C_ECOMM:
  1287.           common_block_end (objfile);
  1288.           break;

  1289.         default:
  1290.           complaint (&symfile_complaints, _("Unexpected storage class: %d"),
  1291.                      cs->c_sclass);
  1292.           /* FALLTHROUGH */

  1293.         case C_DECL:
  1294.         case C_PSYM:
  1295.         case C_RPSYM:
  1296.         case C_ECOML:
  1297.         case C_LSYM:
  1298.         case C_RSYM:
  1299.         case C_GSYM:

  1300.           {
  1301.             sym = define_symbol (cs->c_value + off, cs->c_name, 0, 0, objfile);
  1302.             if (sym != NULL)
  1303.               {
  1304.                 SYMBOL_SECTION (sym) = sec;
  1305.               }
  1306.             return sym;
  1307.           }

  1308.         case C_STSYM:

  1309.           /* For xlc (not GCC), the 'V' symbol descriptor is used for
  1310.              all statics and we need to distinguish file-scope versus
  1311.              function-scope using within_function.  We do this by
  1312.              changing the string we pass to define_symbol to use 'S'
  1313.              where we need to, which is not necessarily super-clean,
  1314.              but seems workable enough.  */

  1315.           if (*name == ':')
  1316.             return NULL;

  1317.           pp = strchr (name, ':');
  1318.           if (pp == NULL)
  1319.             return NULL;

  1320.           ++pp;
  1321.           if (*pp == 'V' && !within_function)
  1322.             *pp = 'S';
  1323.           sym = define_symbol ((cs->c_value
  1324.                                 + ANOFFSET (objfile->section_offsets,
  1325.                                             static_block_section)),
  1326.                                cs->c_name, 0, 0, objfile);
  1327.           if (sym != NULL)
  1328.             {
  1329.               SYMBOL_VALUE_ADDRESS (sym) += static_block_base;
  1330.               SYMBOL_SECTION (sym) = static_block_section;
  1331.             }
  1332.           return sym;

  1333.         }
  1334.     }
  1335.   return sym2;
  1336. }

  1337. /* Extract the file name from the aux entry of a C_FILE symbol.
  1338.    Result is in static storage and is only good for temporary use.  */

  1339. static char *
  1340. coff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
  1341. {
  1342.   static char buffer[BUFSIZ];

  1343.   if (aux_entry->x_file.x_n.x_zeroes == 0)
  1344.     strcpy (buffer, (XCOFF_DATA (objfile)->strtbl
  1345.                      + aux_entry->x_file.x_n.x_offset));
  1346.   else
  1347.     {
  1348.       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
  1349.       buffer[FILNMLEN] = '\0';
  1350.     }
  1351.   return (buffer);
  1352. }

  1353. /* Set *SYMBOL to symbol number symno in symtbl.  */
  1354. static void
  1355. read_symbol (struct internal_syment *symbol, int symno)
  1356. {
  1357.   struct coff_symfile_info *xcoff = XCOFF_DATA (this_symtab_objfile);
  1358.   int nsyms = xcoff->symtbl_num_syms;
  1359.   char *stbl = xcoff->symtbl;

  1360.   if (symno < 0 || symno >= nsyms)
  1361.     {
  1362.       complaint (&symfile_complaints, _("Invalid symbol offset"));
  1363.       symbol->n_value = 0;
  1364.       symbol->n_scnum = -1;
  1365.       return;
  1366.     }
  1367.   bfd_coff_swap_sym_in (this_symtab_objfile->obfd,
  1368.                         stbl + (symno * local_symesz),
  1369.                         symbol);
  1370. }

  1371. /* Get value corresponding to symbol number symno in symtbl.  */

  1372. static CORE_ADDR
  1373. read_symbol_nvalue (int symno)
  1374. {
  1375.   struct internal_syment symbol[1];

  1376.   read_symbol (symbol, symno);
  1377.   return symbol->n_value;
  1378. }


  1379. /* Find the address of the function corresponding to symno, where
  1380.    symno is the symbol pointed to by the linetable.  */

  1381. static int
  1382. read_symbol_lineno (int symno)
  1383. {
  1384.   struct objfile *objfile = this_symtab_objfile;
  1385.   int xcoff64 = bfd_xcoff_is_xcoff64 (objfile->obfd);

  1386.   struct coff_symfile_info *info = XCOFF_DATA (objfile);
  1387.   int nsyms = info->symtbl_num_syms;
  1388.   char *stbl = info->symtbl;
  1389.   char *strtbl = info->strtbl;

  1390.   struct internal_syment symbol[1];
  1391.   union internal_auxent main_aux[1];

  1392.   if (symno < 0)
  1393.     {
  1394.       bf_notfound_complaint ();
  1395.       return 0;
  1396.     }

  1397.   /* Note that just searching for a short distance (e.g. 50 symbols)
  1398.      is not enough, at least in the following case.

  1399.      .extern foo
  1400.      [many .stabx entries]
  1401.      [a few functions, referring to foo]
  1402.      .globl foo
  1403.      .bf

  1404.      What happens here is that the assembler moves the .stabx entries
  1405.      to right before the ".bf" for foo, but the symbol for "foo" is before
  1406.      all the stabx entries.  See PR gdb/2222.  */

  1407.   /* Maintaining a table of .bf entries might be preferable to this search.
  1408.      If I understand things correctly it would need to be done only for
  1409.      the duration of a single psymtab to symtab conversion.  */
  1410.   while (symno < nsyms)
  1411.     {
  1412.       bfd_coff_swap_sym_in (symfile_bfd,
  1413.                             stbl + (symno * local_symesz), symbol);
  1414.       if (symbol->n_sclass == C_FCN)
  1415.         {
  1416.           char *name = xcoff64 ? strtbl + symbol->n_offset : symbol->n_name;

  1417.           if (strcmp (name, ".bf") == 0)
  1418.             goto gotit;
  1419.         }
  1420.       symno += symbol->n_numaux + 1;
  1421.     }

  1422.   bf_notfound_complaint ();
  1423.   return 0;

  1424. gotit:
  1425.   /* Take aux entry and return its lineno.  */
  1426.   symno++;
  1427.   bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
  1428.                         symbol->n_type, symbol->n_sclass,
  1429.                         0, symbol->n_numaux, main_aux);

  1430.   return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
  1431. }

  1432. /* Support for line number handling.  */

  1433. /* This function is called for every section; it finds the outer limits
  1434. * of the line table (minimum and maximum file offset) so that the
  1435. * mainline code can read the whole thing for efficiency.
  1436. */
  1437. static void
  1438. find_linenos (struct bfd *abfd, struct bfd_section *asect, void *vpinfo)
  1439. {
  1440.   struct coff_symfile_info *info;
  1441.   int size, count;
  1442.   file_ptr offset, maxoff;

  1443.   count = asect->lineno_count;

  1444.   if (strcmp (asect->name, ".text") != 0 || count == 0)
  1445.     return;

  1446.   size = count * coff_data (abfd)->local_linesz;
  1447.   info = (struct coff_symfile_info *) vpinfo;
  1448.   offset = asect->line_filepos;
  1449.   maxoff = offset + size;

  1450.   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
  1451.     info->min_lineno_offset = offset;

  1452.   if (maxoff > info->max_lineno_offset)
  1453.     info->max_lineno_offset = maxoff;
  1454. }

  1455. static void
  1456. xcoff_psymtab_to_symtab_1 (struct objfile *objfile, struct partial_symtab *pst)
  1457. {
  1458.   struct cleanup *old_chain;
  1459.   int i;

  1460.   if (!pst)
  1461.     return;

  1462.   if (pst->readin)
  1463.     {
  1464.       fprintf_unfiltered
  1465.         (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
  1466.          pst->filename);
  1467.       return;
  1468.     }

  1469.   /* Read in all partial symtabs on which this one is dependent.  */
  1470.   for (i = 0; i < pst->number_of_dependencies; i++)
  1471.     if (!pst->dependencies[i]->readin)
  1472.       {
  1473.         /* Inform about additional files that need to be read in.  */
  1474.         if (info_verbose)
  1475.           {
  1476.             fputs_filtered (" ", gdb_stdout);
  1477.             wrap_here ("");
  1478.             fputs_filtered ("and ", gdb_stdout);
  1479.             wrap_here ("");
  1480.             printf_filtered ("%s...", pst->dependencies[i]->filename);
  1481.             wrap_here ("");        /* Flush output */
  1482.             gdb_flush (gdb_stdout);
  1483.           }
  1484.         xcoff_psymtab_to_symtab_1 (objfile, pst->dependencies[i]);
  1485.       }

  1486.   if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
  1487.     {
  1488.       /* Init stuff necessary for reading in symbols.  */
  1489.       stabsread_init ();
  1490.       buildsym_init ();
  1491.       old_chain = make_cleanup (really_free_pendings, 0);

  1492.       read_xcoff_symtab (objfile, pst);

  1493.       do_cleanups (old_chain);
  1494.     }

  1495.   pst->readin = 1;
  1496. }

  1497. /* Read in all of the symbols for a given psymtab for real.
  1498.    Be verbose about it if the user wants that.  SELF is not NULL.  */

  1499. static void
  1500. xcoff_read_symtab (struct partial_symtab *self, struct objfile *objfile)
  1501. {
  1502.   if (self->readin)
  1503.     {
  1504.       fprintf_unfiltered
  1505.         (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
  1506.          self->filename);
  1507.       return;
  1508.     }

  1509.   if (((struct symloc *) self->read_symtab_private)->numsyms != 0
  1510.       || self->number_of_dependencies)
  1511.     {
  1512.       /* Print the message now, before reading the string table,
  1513.          to avoid disconcerting pauses.  */
  1514.       if (info_verbose)
  1515.         {
  1516.           printf_filtered ("Reading in symbols for %s...", self->filename);
  1517.           gdb_flush (gdb_stdout);
  1518.         }

  1519.       next_symbol_text_func = xcoff_next_symbol_text;

  1520.       xcoff_psymtab_to_symtab_1 (objfile, self);

  1521.       /* Match with global symbols.  This only needs to be done once,
  1522.          after all of the symtabs and dependencies have been read in.   */
  1523.       scan_file_globals (objfile);

  1524.       /* Finish up the debug error message.  */
  1525.       if (info_verbose)
  1526.         printf_filtered ("done.\n");
  1527.     }
  1528. }

  1529. static void
  1530. xcoff_new_init (struct objfile *objfile)
  1531. {
  1532.   stabsread_new_init ();
  1533.   buildsym_new_init ();
  1534. }

  1535. /* Do initialization in preparation for reading symbols from OBJFILE.

  1536.    We will only be called if this is an XCOFF or XCOFF-like file.
  1537.    BFD handles figuring out the format of the file, and code in symfile.c
  1538.    uses BFD's determination to vector to us.  */

  1539. static void
  1540. xcoff_symfile_init (struct objfile *objfile)
  1541. {
  1542.   struct coff_symfile_info *xcoff;

  1543.   /* Allocate struct to keep track of the symfile.  */
  1544.   xcoff = XNEW (struct coff_symfile_info);
  1545.   set_objfile_data (objfile, xcoff_objfile_data_key, xcoff);

  1546.   /* XCOFF objects may be reordered, so set OBJF_REORDERED.  If we
  1547.      find this causes a significant slowdown in gdb then we could
  1548.      set it in the debug symbol readers only when necessary.  */
  1549.   objfile->flags |= OBJF_REORDERED;
  1550. }

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

  1555. static void
  1556. xcoff_symfile_finish (struct objfile *objfile)
  1557. {
  1558.   /* Start with a fresh include table for the next objfile.  */
  1559.   if (inclTable)
  1560.     {
  1561.       xfree (inclTable);
  1562.       inclTable = NULL;
  1563.     }
  1564.   inclIndx = inclLength = inclDepth = 0;

  1565.   dwarf2_free_objfile (objfile);
  1566. }


  1567. static void
  1568. init_stringtab (bfd *abfd, file_ptr offset, struct objfile *objfile)
  1569. {
  1570.   long length;
  1571.   int val;
  1572.   unsigned char lengthbuf[4];
  1573.   char *strtbl;
  1574.   struct coff_symfile_info *xcoff = XCOFF_DATA (objfile);

  1575.   xcoff->strtbl = NULL;

  1576.   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
  1577.     error (_("cannot seek to string table in %s: %s"),
  1578.            bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));

  1579.   val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
  1580.   length = bfd_h_get_32 (abfd, lengthbuf);

  1581.   /* If no string table is needed, then the file may end immediately
  1582.      after the symbols.  Just return with `strtbl' set to NULL.  */

  1583.   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
  1584.     return;

  1585.   /* Allocate string table from objfile_obstack.  We will need this table
  1586.      as long as we have its symbol table around.  */

  1587.   strtbl = (char *) obstack_alloc (&objfile->objfile_obstack, length);
  1588.   xcoff->strtbl = strtbl;

  1589.   /* Copy length buffer, the first byte is usually zero and is
  1590.      used for stabs with a name length of zero.  */
  1591.   memcpy (strtbl, lengthbuf, sizeof lengthbuf);
  1592.   if (length == sizeof lengthbuf)
  1593.     return;

  1594.   val = bfd_bread (strtbl + sizeof lengthbuf, length - sizeof lengthbuf, abfd);

  1595.   if (val != length - sizeof lengthbuf)
  1596.     error (_("cannot read string table from %s: %s"),
  1597.            bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  1598.   if (strtbl[length - 1] != '\0')
  1599.     error (_("bad symbol file: string table "
  1600.              "does not end with null character"));

  1601.   return;
  1602. }

  1603. /* If we have not yet seen a function for this psymtab, this is 0.  If we
  1604.    have seen one, it is the offset in the line numbers of the line numbers
  1605.    for the psymtab.  */
  1606. static unsigned int first_fun_line_offset;

  1607. /* Allocate and partially fill a partial symtab.  It will be
  1608.    completely filled at the end of the symbol list.

  1609.    SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
  1610.    is the address relative to which its symbols are (incremental) or 0
  1611.    (normal).  */

  1612. static struct partial_symtab *
  1613. xcoff_start_psymtab (struct objfile *objfile,
  1614.                      const char *filename, int first_symnum,
  1615.                      struct partial_symbol **global_syms,
  1616.                      struct partial_symbol **static_syms)
  1617. {
  1618.   struct partial_symtab *result =
  1619.     start_psymtab_common (objfile, objfile->section_offsets,
  1620.                           filename,
  1621.                           /* We fill in textlow later.  */
  1622.                           0,
  1623.                           global_syms, static_syms);

  1624.   result->read_symtab_private = obstack_alloc (&objfile->objfile_obstack,
  1625.                                                sizeof (struct symloc));
  1626.   ((struct symloc *) result->read_symtab_private)->first_symnum = first_symnum;
  1627.   result->read_symtab = xcoff_read_symtab;

  1628.   /* Deduce the source language from the filename for this psymtab.  */
  1629.   psymtab_language = deduce_language_from_filename (filename);

  1630.   return result;
  1631. }

  1632. /* Close off the current usage of PST.
  1633.    Returns PST, or NULL if the partial symtab was empty and thrown away.

  1634.    CAPPING_SYMBOL_NUMBER is the end of pst (exclusive).

  1635.    INCLUDE_LIST, NUM_INCLUDES, DEPENDENCY_LIST, and NUMBER_DEPENDENCIES
  1636.    are the information for includes and dependencies.  */

  1637. static struct partial_symtab *
  1638. xcoff_end_psymtab (struct objfile *objfile, struct partial_symtab *pst,
  1639.                    const char **include_list, int num_includes,
  1640.                    int capping_symbol_number,
  1641.                    struct partial_symtab **dependency_list,
  1642.                    int number_dependencies, int textlow_not_set)
  1643. {
  1644.   int i;

  1645.   if (capping_symbol_number != -1)
  1646.     ((struct symloc *) pst->read_symtab_private)->numsyms =
  1647.       capping_symbol_number
  1648.       - ((struct symloc *) pst->read_symtab_private)->first_symnum;
  1649.   ((struct symloc *) pst->read_symtab_private)->lineno_off =
  1650.     first_fun_line_offset;
  1651.   first_fun_line_offset = 0;
  1652.   pst->n_global_syms = objfile->global_psymbols.next
  1653.     - (objfile->global_psymbols.list + pst->globals_offset);
  1654.   pst->n_static_syms = objfile->static_psymbols.next
  1655.     - (objfile->static_psymbols.list + pst->statics_offset);

  1656.   pst->number_of_dependencies = number_dependencies;
  1657.   if (number_dependencies)
  1658.     {
  1659.       pst->dependencies = (struct partial_symtab **)
  1660.         obstack_alloc (&objfile->objfile_obstack,
  1661.                     number_dependencies * sizeof (struct partial_symtab *));
  1662.       memcpy (pst->dependencies, dependency_list,
  1663.               number_dependencies * sizeof (struct partial_symtab *));
  1664.     }
  1665.   else
  1666.     pst->dependencies = 0;

  1667.   for (i = 0; i < num_includes; i++)
  1668.     {
  1669.       struct partial_symtab *subpst =
  1670.         allocate_psymtab (include_list[i], objfile);

  1671.       subpst->section_offsets = pst->section_offsets;
  1672.       subpst->read_symtab_private = obstack_alloc (&objfile->objfile_obstack,
  1673.                                                    sizeof (struct symloc));
  1674.       ((struct symloc *) subpst->read_symtab_private)->first_symnum = 0;
  1675.       ((struct symloc *) subpst->read_symtab_private)->numsyms = 0;
  1676.       subpst->textlow = 0;
  1677.       subpst->texthigh = 0;

  1678.       /* We could save slight bits of space by only making one of these,
  1679.          shared by the entire set of include files.  FIXME-someday.  */
  1680.       subpst->dependencies = (struct partial_symtab **)
  1681.         obstack_alloc (&objfile->objfile_obstack,
  1682.                        sizeof (struct partial_symtab *));
  1683.       subpst->dependencies[0] = pst;
  1684.       subpst->number_of_dependencies = 1;

  1685.       subpst->globals_offset =
  1686.         subpst->n_global_syms =
  1687.         subpst->statics_offset =
  1688.         subpst->n_static_syms = 0;

  1689.       subpst->readin = 0;
  1690.       subpst->compunit_symtab = NULL;
  1691.       subpst->read_symtab = pst->read_symtab;
  1692.     }

  1693.   sort_pst_symbols (objfile, pst);

  1694.   if (num_includes == 0
  1695.       && number_dependencies == 0
  1696.       && pst->n_global_syms == 0
  1697.       && pst->n_static_syms == 0)
  1698.     {
  1699.       /* Throw away this psymtab, it's empty.  We can't deallocate it, since
  1700.          it is on the obstack, but we can forget to chain it on the list.  */
  1701.       /* Empty psymtabs happen as a result of header files which don't have
  1702.          any symbols in them.  There can be a lot of them.  */

  1703.       discard_psymtab (objfile, pst);

  1704.       /* Indicate that psymtab was thrown away.  */
  1705.       pst = (struct partial_symtab *) NULL;
  1706.     }
  1707.   return pst;
  1708. }

  1709. /* Swap raw symbol at *RAW and put the name in *NAME, the symbol in
  1710.    *SYMBOL, the first auxent in *AUX.  Advance *RAW and *SYMNUMP over
  1711.    the symbol and its auxents.  */

  1712. static void
  1713. swap_sym (struct internal_syment *symbol, union internal_auxent *aux,
  1714.           const char **name, char **raw, unsigned int *symnump,
  1715.           struct objfile *objfile)
  1716. {
  1717.   bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
  1718.   if (symbol->n_zeroes)
  1719.     {
  1720.       /* If it's exactly E_SYMNMLEN characters long it isn't
  1721.          '\0'-terminated.  */
  1722.       if (symbol->n_name[E_SYMNMLEN - 1] != '\0')
  1723.         {
  1724.           /* FIXME: wastes memory for symbols which we don't end up putting
  1725.              into the minimal symbols.  */
  1726.           char *p;

  1727.           p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
  1728.           strncpy (p, symbol->n_name, E_SYMNMLEN);
  1729.           p[E_SYMNMLEN] = '\0';
  1730.           *name = p;
  1731.         }
  1732.       else
  1733.         /* Point to the unswapped name as that persists as long as the
  1734.            objfile does.  */
  1735.         *name = ((struct external_syment *) *raw)->e.e_name;
  1736.     }
  1737.   else if (symbol->n_sclass & 0x80)
  1738.     {
  1739.       *name = XCOFF_DATA (objfile)->debugsec + symbol->n_offset;
  1740.     }
  1741.   else
  1742.     {
  1743.       *name = XCOFF_DATA (objfile)->strtbl + symbol->n_offset;
  1744.     }
  1745.   ++*symnump;
  1746.   *raw += coff_data (objfile->obfd)->local_symesz;
  1747.   if (symbol->n_numaux > 0)
  1748.     {
  1749.       bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
  1750.                             symbol->n_sclass, 0, symbol->n_numaux, aux);

  1751.       *symnump += symbol->n_numaux;
  1752.       *raw += coff_data (objfile->obfd)->local_symesz * symbol->n_numaux;
  1753.     }
  1754. }

  1755. static void
  1756. function_outside_compilation_unit_complaint (const char *arg1)
  1757. {
  1758.   complaint (&symfile_complaints,
  1759.              _("function `%s' appears to be defined "
  1760.                "outside of all compilation units"),
  1761.              arg1);
  1762. }

  1763. static void
  1764. scan_xcoff_symtab (struct objfile *objfile)
  1765. {
  1766.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  1767.   CORE_ADDR toc_offset = 0;        /* toc offset value in data section.  */
  1768.   const char *filestring = NULL;

  1769.   const char *namestring;
  1770.   int past_first_source_file = 0;
  1771.   bfd *abfd;
  1772.   asection *bfd_sect;
  1773.   unsigned int nsyms;

  1774.   /* Current partial symtab */
  1775.   struct partial_symtab *pst;

  1776.   /* List of current psymtab's include files.  */
  1777.   const char **psymtab_include_list;
  1778.   int includes_allocated;
  1779.   int includes_used;

  1780.   /* Index within current psymtab dependency list.  */
  1781.   struct partial_symtab **dependency_list;
  1782.   int dependencies_used, dependencies_allocated;

  1783.   char *sraw_symbol;
  1784.   struct internal_syment symbol;
  1785.   union internal_auxent main_aux[5];
  1786.   unsigned int ssymnum;

  1787.   const char *last_csect_name = NULL; /* Last seen csect's name and value.  */
  1788.   CORE_ADDR last_csect_val = 0;
  1789.   int last_csect_sec = 0;
  1790.   int misc_func_recorded = 0;        /* true if any misc. function.  */
  1791.   int textlow_not_set = 1;

  1792.   pst = (struct partial_symtab *) 0;

  1793.   includes_allocated = 30;
  1794.   includes_used = 0;
  1795.   psymtab_include_list = (const char **) alloca (includes_allocated *
  1796.                                                  sizeof (const char *));

  1797.   dependencies_allocated = 30;
  1798.   dependencies_used = 0;
  1799.   dependency_list =
  1800.     (struct partial_symtab **) alloca (dependencies_allocated *
  1801.                                        sizeof (struct partial_symtab *));

  1802.   set_last_source_file (NULL);

  1803.   abfd = objfile->obfd;
  1804.   next_symbol_text_func = xcoff_next_symbol_text;

  1805.   sraw_symbol = XCOFF_DATA (objfile)->symtbl;
  1806.   nsyms = XCOFF_DATA (objfile)->symtbl_num_syms;
  1807.   ssymnum = 0;
  1808.   while (ssymnum < nsyms)
  1809.     {
  1810.       int sclass;

  1811.       QUIT;

  1812.       bfd_coff_swap_sym_in (abfd, sraw_symbol, &symbol);
  1813.       sclass = symbol.n_sclass;

  1814.       switch (sclass)
  1815.         {
  1816.         case C_EXT:
  1817.         case C_HIDEXT:
  1818.           {
  1819.             /* The CSECT auxent--always the last auxent.  */
  1820.             union internal_auxent csect_aux;
  1821.             unsigned int symnum_before = ssymnum;

  1822.             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  1823.                       &ssymnum, objfile);
  1824.             if (symbol.n_numaux > 1)
  1825.               {
  1826.                 bfd_coff_swap_aux_in
  1827.                   (objfile->obfd,
  1828.                    sraw_symbol - coff_data (abfd)->local_symesz,
  1829.                    symbol.n_type,
  1830.                    symbol.n_sclass,
  1831.                    symbol.n_numaux - 1,
  1832.                    symbol.n_numaux,
  1833.                    &csect_aux);
  1834.               }
  1835.             else
  1836.               csect_aux = main_aux[0];

  1837.             /* If symbol name starts with ".$" or "$", ignore it.  */
  1838.             if (namestring[0] == '$'
  1839.                 || (namestring[0] == '.' && namestring[1] == '$'))
  1840.               break;

  1841.             switch (csect_aux.x_csect.x_smtyp & 0x7)
  1842.               {
  1843.               case XTY_SD:
  1844.                 switch (csect_aux.x_csect.x_smclas)
  1845.                   {
  1846.                   case XMC_PR:
  1847.                     if (last_csect_name)
  1848.                       {
  1849.                         /* If no misc. function recorded in the last
  1850.                            seen csect, enter it as a function.  This
  1851.                            will take care of functions like strcmp()
  1852.                            compiled by xlc.  */

  1853.                         if (!misc_func_recorded)
  1854.                           {
  1855.                             record_minimal_symbol
  1856.                               (last_csect_name, last_csect_val,
  1857.                                mst_text, last_csect_sec, objfile);
  1858.                             misc_func_recorded = 1;
  1859.                           }

  1860.                         if (pst != NULL)
  1861.                           {
  1862.                             /* We have to allocate one psymtab for
  1863.                                each program csect, because their text
  1864.                                sections need not be adjacent.  */
  1865.                             xcoff_end_psymtab
  1866.                               (objfile, pst, psymtab_include_list,
  1867.                                includes_used, symnum_before, dependency_list,
  1868.                                dependencies_used, textlow_not_set);
  1869.                             includes_used = 0;
  1870.                             dependencies_used = 0;
  1871.                             /* Give all psymtabs for this source file the same
  1872.                                name.  */
  1873.                             pst = xcoff_start_psymtab
  1874.                               (objfile,
  1875.                                filestring,
  1876.                                symnum_before,
  1877.                                objfile->global_psymbols.next,
  1878.                                objfile->static_psymbols.next);
  1879.                           }
  1880.                       }
  1881.                     /* Activate the misc_func_recorded mechanism for
  1882.                        compiler- and linker-generated CSECTs like ".strcmp"
  1883.                        and "@FIX1".  */
  1884.                     if (namestring && (namestring[0] == '.'
  1885.                                        || namestring[0] == '@'))
  1886.                       {
  1887.                         last_csect_name = namestring;
  1888.                         last_csect_val = symbol.n_value;
  1889.                         last_csect_sec = symbol.n_scnum;
  1890.                       }
  1891.                     if (pst != NULL)
  1892.                       {
  1893.                         CORE_ADDR highval =
  1894.                           symbol.n_value + csect_aux.x_csect.x_scnlen.l;

  1895.                         if (highval > pst->texthigh)
  1896.                           pst->texthigh = highval;
  1897.                         if (pst->textlow == 0 || symbol.n_value < pst->textlow)
  1898.                           pst->textlow = symbol.n_value;
  1899.                       }
  1900.                     misc_func_recorded = 0;
  1901.                     break;

  1902.                   case XMC_RW:
  1903.                   case XMC_TD:
  1904.                     /* Data variables are recorded in the minimal symbol
  1905.                        table, except for section symbols.  */
  1906.                     if (*namestring != '.')
  1907.                       record_minimal_symbol
  1908.                         (namestring, symbol.n_value,
  1909.                          sclass == C_HIDEXT ? mst_file_data : mst_data,
  1910.                          symbol.n_scnum, objfile);
  1911.                     break;

  1912.                   case XMC_TC0:
  1913.                     if (toc_offset)
  1914.                       warning (_("More than one XMC_TC0 symbol found."));
  1915.                     toc_offset = symbol.n_value;

  1916.                     /* Make TOC offset relative to start address of
  1917.                        section.  */
  1918.                     bfd_sect = secnum_to_bfd_section (symbol.n_scnum, objfile);
  1919.                     if (bfd_sect)
  1920.                       toc_offset -= bfd_section_vma (objfile->obfd, bfd_sect);
  1921.                     break;

  1922.                   case XMC_TC:
  1923.                     /* These symbols tell us where the TOC entry for a
  1924.                        variable is, not the variable itself.  */
  1925.                     break;

  1926.                   default:
  1927.                     break;
  1928.                   }
  1929.                 break;

  1930.               case XTY_LD:
  1931.                 switch (csect_aux.x_csect.x_smclas)
  1932.                   {
  1933.                   case XMC_PR:
  1934.                     /* A function entry point.  */

  1935.                     if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
  1936.                       first_fun_line_offset =
  1937.                         main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
  1938.                       {
  1939.                         record_minimal_symbol
  1940.                           (namestring, symbol.n_value,
  1941.                            sclass == C_HIDEXT ? mst_file_text : mst_text,
  1942.                            symbol.n_scnum, objfile);
  1943.                         misc_func_recorded = 1;
  1944.                       }
  1945.                     break;

  1946.                   case XMC_GL:
  1947.                     /* shared library function trampoline code entry
  1948.                        point.  */

  1949.                     /* record trampoline code entries as
  1950.                        mst_solib_trampoline symbol.  When we lookup mst
  1951.                        symbols, we will choose mst_text over
  1952.                        mst_solib_trampoline.  */
  1953.                     record_minimal_symbol
  1954.                       (namestring, symbol.n_value,
  1955.                        mst_solib_trampoline, symbol.n_scnum, objfile);
  1956.                     misc_func_recorded = 1;
  1957.                     break;

  1958.                   case XMC_DS:
  1959.                     /* The symbols often have the same names as
  1960.                        debug symbols for functions, and confuse
  1961.                        lookup_symbol.  */
  1962.                     break;

  1963.                   default:

  1964.                     /* xlc puts each variable in a separate csect,
  1965.                        so we get an XTY_SD for each variable.  But
  1966.                        gcc puts several variables in a csect, so
  1967.                        that each variable only gets an XTY_LD.  We
  1968.                        still need to record them.  This will
  1969.                        typically be XMC_RW; I suspect XMC_RO and
  1970.                        XMC_BS might be possible too.  */
  1971.                     if (*namestring != '.')
  1972.                       record_minimal_symbol
  1973.                         (namestring, symbol.n_value,
  1974.                          sclass == C_HIDEXT ? mst_file_data : mst_data,
  1975.                          symbol.n_scnum, objfile);
  1976.                     break;
  1977.                   }
  1978.                 break;

  1979.               case XTY_CM:
  1980.                 switch (csect_aux.x_csect.x_smclas)
  1981.                   {
  1982.                   case XMC_RW:
  1983.                   case XMC_BS:
  1984.                     /* Common variables are recorded in the minimal symbol
  1985.                        table, except for section symbols.  */
  1986.                     if (*namestring != '.')
  1987.                       record_minimal_symbol
  1988.                         (namestring, symbol.n_value,
  1989.                          sclass == C_HIDEXT ? mst_file_bss : mst_bss,
  1990.                          symbol.n_scnum, objfile);
  1991.                     break;
  1992.                   }
  1993.                 break;

  1994.               default:
  1995.                 break;
  1996.               }
  1997.           }
  1998.           break;
  1999.         case C_FILE:
  2000.           {
  2001.             unsigned int symnum_before;

  2002.             symnum_before = ssymnum;
  2003.             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  2004.                       &ssymnum, objfile);

  2005.             /* See if the last csect needs to be recorded.  */

  2006.             if (last_csect_name && !misc_func_recorded)
  2007.               {
  2008.                 /* If no misc. function recorded in the last seen csect, enter
  2009.                    it as a function.  This will take care of functions like
  2010.                    strcmp() compiled by xlc.  */

  2011.                 record_minimal_symbol (last_csect_name, last_csect_val,
  2012.                                        mst_text, last_csect_sec, objfile);
  2013.                 misc_func_recorded = 1;
  2014.               }

  2015.             if (pst)
  2016.               {
  2017.                 xcoff_end_psymtab (objfile, pst, psymtab_include_list,
  2018.                                    includes_used, symnum_before,
  2019.                                    dependency_list, dependencies_used,
  2020.                                    textlow_not_set);
  2021.                 includes_used = 0;
  2022.                 dependencies_used = 0;
  2023.               }
  2024.             first_fun_line_offset = 0;

  2025.             /* XCOFF, according to the AIX 3.2 documentation, puts the
  2026.                filename in cs->c_name.  But xlc 1.3.0.2 has decided to
  2027.                do things the standard COFF way and put it in the auxent.
  2028.                We use the auxent if the symbol is ".file" and an auxent
  2029.                exists, otherwise use the symbol itself.  */
  2030.             if (!strcmp (namestring, ".file") && symbol.n_numaux > 0)
  2031.               {
  2032.                 filestring = coff_getfilename (&main_aux[0], objfile);
  2033.               }
  2034.             else
  2035.               filestring = namestring;

  2036.             pst = xcoff_start_psymtab (objfile,
  2037.                                        filestring,
  2038.                                        symnum_before,
  2039.                                        objfile->global_psymbols.next,
  2040.                                        objfile->static_psymbols.next);
  2041.             last_csect_name = NULL;
  2042.           }
  2043.           break;

  2044.         default:
  2045.           {
  2046.             complaint (&symfile_complaints,
  2047.                        _("Storage class %d not recognized during scan"),
  2048.                        sclass);
  2049.           }
  2050.           /* FALLTHROUGH */

  2051.           /* C_FCN is .bf and .ef symbols.  I think it is sufficient
  2052.              to handle only the C_FUN and C_EXT.  */
  2053.         case C_FCN:

  2054.         case C_BSTAT:
  2055.         case C_ESTAT:
  2056.         case C_ARG:
  2057.         case C_REGPARM:
  2058.         case C_REG:
  2059.         case C_TPDEF:
  2060.         case C_STRTAG:
  2061.         case C_UNTAG:
  2062.         case C_ENTAG:
  2063.         case C_LABEL:
  2064.         case C_NULL:

  2065.           /* C_EINCL means we are switching back to the main file.  But there
  2066.              is no reason to care; the only thing we want to know about
  2067.              includes is the names of all the included (.h) files.  */
  2068.         case C_EINCL:

  2069.         case C_BLOCK:

  2070.           /* I don't think C_STAT is used in xcoff; C_HIDEXT appears to be
  2071.              used instead.  */
  2072.         case C_STAT:

  2073.           /* I don't think the name of the common block (as opposed to the
  2074.              variables within it) is something which is user visible
  2075.              currently.  */
  2076.         case C_BCOMM:
  2077.         case C_ECOMM:

  2078.         case C_PSYM:
  2079.         case C_RPSYM:

  2080.           /* I think we can ignore C_LSYM; types on xcoff seem to use C_DECL
  2081.              so C_LSYM would appear to be only for locals.  */
  2082.         case C_LSYM:

  2083.         case C_AUTO:
  2084.         case C_RSYM:
  2085.           {
  2086.             /* We probably could save a few instructions by assuming that
  2087.                C_LSYM, C_PSYM, etc., never have auxents.  */
  2088.             int naux1 = symbol.n_numaux + 1;

  2089.             ssymnum += naux1;
  2090.             sraw_symbol += bfd_coff_symesz (abfd) * naux1;
  2091.           }
  2092.           break;

  2093.         case C_BINCL:
  2094.           {
  2095.             /* Mark down an include file in the current psymtab.  */
  2096.             enum language tmp_language;

  2097.             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  2098.                       &ssymnum, objfile);

  2099.             tmp_language = deduce_language_from_filename (namestring);

  2100.             /* Only change the psymtab's language if we've learned
  2101.                something useful (eg. tmp_language is not language_unknown).
  2102.                In addition, to match what start_subfile does, never change
  2103.                from C++ to C.  */
  2104.             if (tmp_language != language_unknown
  2105.                 && (tmp_language != language_c
  2106.                     || psymtab_language != language_cplus))
  2107.               psymtab_language = tmp_language;

  2108.             /* In C++, one may expect the same filename to come round many
  2109.                times, when code is coming alternately from the main file
  2110.                and from inline functions in other files.  So I check to see
  2111.                if this is a file we've seen before -- either the main
  2112.                source file, or a previously included file.

  2113.                This seems to be a lot of time to be spending on N_SOL, but
  2114.                things like "break c-exp.y:435" need to work (I
  2115.                suppose the psymtab_include_list could be hashed or put
  2116.                in a binary tree, if profiling shows this is a major hog).  */
  2117.             if (pst && strcmp (namestring, pst->filename) == 0)
  2118.               continue;

  2119.             {
  2120.               int i;

  2121.               for (i = 0; i < includes_used; i++)
  2122.                 if (strcmp (namestring, psymtab_include_list[i]) == 0)
  2123.                   {
  2124.                     i = -1;
  2125.                     break;
  2126.                   }
  2127.               if (i == -1)
  2128.                 continue;
  2129.             }
  2130.             psymtab_include_list[includes_used++] = namestring;
  2131.             if (includes_used >= includes_allocated)
  2132.               {
  2133.                 const char **orig = psymtab_include_list;

  2134.                 psymtab_include_list = (const char **)
  2135.                   alloca ((includes_allocated *= 2) *
  2136.                           sizeof (const char *));
  2137.                 memcpy (psymtab_include_list, orig,
  2138.                         includes_used * sizeof (const char *));
  2139.               }
  2140.             continue;
  2141.           }
  2142.         case C_FUN:
  2143.           /* The value of the C_FUN is not the address of the function (it
  2144.              appears to be the address before linking), but as long as it
  2145.              is smaller than the actual address, then find_pc_partial_function
  2146.              will use the minimal symbols instead.  I hope.  */

  2147.         case C_GSYM:
  2148.         case C_ECOML:
  2149.         case C_DECL:
  2150.         case C_STSYM:
  2151.           {
  2152.             char *p;

  2153.             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
  2154.                       &ssymnum, objfile);

  2155.             p = strchr (namestring, ':');
  2156.             if (!p)
  2157.               continue;                        /* Not a debugging symbol.   */

  2158.             /* Main processing section for debugging symbols which
  2159.                the initial read through the symbol tables needs to worry
  2160.                about.  If we reach this point, the symbol which we are
  2161.                considering is definitely one we are interested in.
  2162.                p must also contain the (valid) index into the namestring
  2163.                which indicates the debugging type symbol.  */

  2164.             switch (p[1])
  2165.               {
  2166.               case 'S':
  2167.                 symbol.n_value += ANOFFSET (objfile->section_offsets,
  2168.                                             SECT_OFF_DATA (objfile));

  2169.                 if (gdbarch_static_transform_name_p (gdbarch))
  2170.                   namestring = gdbarch_static_transform_name
  2171.                                  (gdbarch, namestring);

  2172.                 add_psymbol_to_list (namestring, p - namestring, 1,
  2173.                                      VAR_DOMAIN, LOC_STATIC,
  2174.                                      &objfile->static_psymbols,
  2175.                                      0, symbol.n_value,
  2176.                                      psymtab_language, objfile);
  2177.                 continue;

  2178.               case 'G':
  2179.                 symbol.n_value += ANOFFSET (objfile->section_offsets,
  2180.                                             SECT_OFF_DATA (objfile));
  2181.                 /* The addresses in these entries are reported to be
  2182.                    wrong.  See the code that reads 'G's for symtabs.  */
  2183.                 add_psymbol_to_list (namestring, p - namestring, 1,
  2184.                                      VAR_DOMAIN, LOC_STATIC,
  2185.                                      &objfile->global_psymbols,
  2186.                                      0, symbol.n_value,
  2187.                                      psymtab_language, objfile);
  2188.                 continue;

  2189.               case 'T':
  2190.                 /* When a 'T' entry is defining an anonymous enum, it
  2191.                    may have a name which is the empty string, or a
  2192.                    single space.  Since they're not really defining a
  2193.                    symbol, those shouldn't go in the partial symbol
  2194.                    table.  We do pick up the elements of such enums at
  2195.                    'check_enum:', below.  */
  2196.                 if (p >= namestring + 2
  2197.                     || (p == namestring + 1
  2198.                         && namestring[0] != ' '))
  2199.                   {
  2200.                     add_psymbol_to_list (namestring, p - namestring, 1,
  2201.                                          STRUCT_DOMAIN, LOC_TYPEDEF,
  2202.                                          &objfile->static_psymbols,
  2203.                                          symbol.n_value, 0,
  2204.                                          psymtab_language, objfile);
  2205.                     if (p[2] == 't')
  2206.                       {
  2207.                         /* Also a typedef with the same name.  */
  2208.                         add_psymbol_to_list (namestring, p - namestring, 1,
  2209.                                              VAR_DOMAIN, LOC_TYPEDEF,
  2210.                                              &objfile->static_psymbols,
  2211.                                              symbol.n_value, 0,
  2212.                                              psymtab_language, objfile);
  2213.                         p += 1;
  2214.                       }
  2215.                   }
  2216.                 goto check_enum;

  2217.               case 't':
  2218.                 if (p != namestring)        /* a name is there, not just :T...  */
  2219.                   {
  2220.                     add_psymbol_to_list (namestring, p - namestring, 1,
  2221.                                          VAR_DOMAIN, LOC_TYPEDEF,
  2222.                                          &objfile->static_psymbols,
  2223.                                          symbol.n_value, 0,
  2224.                                          psymtab_language, objfile);
  2225.                   }
  2226.               check_enum:
  2227.                 /* If this is an enumerated type, we need to
  2228.                    add all the enum constants to the partial symbol
  2229.                    table.  This does not cover enums without names, e.g.
  2230.                    "enum {a, b} c;" in C, but fortunately those are
  2231.                    rare.  There is no way for GDB to find those from the
  2232.                    enum type without spending too much time on it.  Thus
  2233.                    to solve this problem, the compiler needs to put out the
  2234.                    enum in a nameless type.  GCC2 does this.  */

  2235.                 /* We are looking for something of the form
  2236.                    <name> ":" ("t" | "T") [<number> "="] "e"
  2237.                    {<constant> ":" <value> ","} ";".  */

  2238.                 /* Skip over the colon and the 't' or 'T'.  */
  2239.                 p += 2;
  2240.                 /* This type may be given a number.  Also, numbers can come
  2241.                    in pairs like (0,26).  Skip over it.  */
  2242.                 while ((*p >= '0' && *p <= '9')
  2243.                        || *p == '(' || *p == ',' || *p == ')'
  2244.                        || *p == '=')
  2245.                   p++;

  2246.                 if (*p++ == 'e')
  2247.                   {
  2248.                     /* The aix4 compiler emits extra crud before the
  2249.                        members.  */
  2250.                     if (*p == '-')
  2251.                       {
  2252.                         /* Skip over the type (?).  */
  2253.                         while (*p != ':')
  2254.                           p++;

  2255.                         /* Skip over the colon.  */
  2256.                         p++;
  2257.                       }

  2258.                     /* We have found an enumerated type.  */
  2259.                     /* According to comments in read_enum_type
  2260.                        a comma could end it instead of a semicolon.
  2261.                        I don't know where that happens.
  2262.                        Accept either.  */
  2263.                     while (*p && *p != ';' && *p != ',')
  2264.                       {
  2265.                         char *q;

  2266.                         /* Check for and handle cretinous dbx symbol name
  2267.                            continuation!  */
  2268.                         if (*p == '\\' || (*p == '?' && p[1] == '\0'))
  2269.                           p = next_symbol_text (objfile);

  2270.                         /* Point to the character after the name
  2271.                            of the enum constant.  */
  2272.                         for (q = p; *q && *q != ':'; q++)
  2273.                           ;
  2274.                         /* Note that the value doesn't matter for
  2275.                            enum constants in psymtabs, just in symtabs.  */
  2276.                         add_psymbol_to_list (p, q - p, 1,
  2277.                                              VAR_DOMAIN, LOC_CONST,
  2278.                                              &objfile->static_psymbols, 0,
  2279.                                              0, psymtab_language, objfile);
  2280.                         /* Point past the name.  */
  2281.                         p = q;
  2282.                         /* Skip over the value.  */
  2283.                         while (*p && *p != ',')
  2284.                           p++;
  2285.                         /* Advance past the comma.  */
  2286.                         if (*p)
  2287.                           p++;
  2288.                       }
  2289.                   }
  2290.                 continue;

  2291.               case 'c':
  2292.                 /* Constant, e.g. from "const" in Pascal.  */
  2293.                 add_psymbol_to_list (namestring, p - namestring, 1,
  2294.                                      VAR_DOMAIN, LOC_CONST,
  2295.                                      &objfile->static_psymbols, symbol.n_value,
  2296.                                      0, psymtab_language, objfile);
  2297.                 continue;

  2298.               case 'f':
  2299.                 if (! pst)
  2300.                   {
  2301.                     int name_len = p - namestring;
  2302.                     char *name = xmalloc (name_len + 1);

  2303.                     memcpy (name, namestring, name_len);
  2304.                     name[name_len] = '\0';
  2305.                     function_outside_compilation_unit_complaint (name);
  2306.                     xfree (name);
  2307.                   }
  2308.                 symbol.n_value += ANOFFSET (objfile->section_offsets,
  2309.                                             SECT_OFF_TEXT (objfile));
  2310.                 add_psymbol_to_list (namestring, p - namestring, 1,
  2311.                                      VAR_DOMAIN, LOC_BLOCK,
  2312.                                      &objfile->static_psymbols,
  2313.                                      0, symbol.n_value,
  2314.                                      psymtab_language, objfile);
  2315.                 continue;

  2316.                 /* Global functions were ignored here, but now they
  2317.                    are put into the global psymtab like one would expect.
  2318.                    They're also in the minimal symbol table.  */
  2319.               case 'F':
  2320.                 if (! pst)
  2321.                   {
  2322.                     int name_len = p - namestring;
  2323.                     char *name = xmalloc (name_len + 1);

  2324.                     memcpy (name, namestring, name_len);
  2325.                     name[name_len] = '\0';
  2326.                     function_outside_compilation_unit_complaint (name);
  2327.                     xfree (name);
  2328.                   }

  2329.                 /* We need only the minimal symbols for these
  2330.                    loader-generated definitions.  Keeping the global
  2331.                    symbols leads to "in psymbols but not in symbols"
  2332.                    errors.  */
  2333.                 if (strncmp (namestring, "@FIX", 4) == 0)
  2334.                   continue;

  2335.                 symbol.n_value += ANOFFSET (objfile->section_offsets,
  2336.                                             SECT_OFF_TEXT (objfile));
  2337.                 add_psymbol_to_list (namestring, p - namestring, 1,
  2338.                                      VAR_DOMAIN, LOC_BLOCK,
  2339.                                      &objfile->global_psymbols,
  2340.                                      0, symbol.n_value,
  2341.                                      psymtab_language, objfile);
  2342.                 continue;

  2343.                 /* Two things show up here (hopefully); static symbols of
  2344.                    local scope (static used inside braces) or extensions
  2345.                    of structure symbols.  We can ignore both.  */
  2346.               case 'V':
  2347.               case '(':
  2348.               case '0':
  2349.               case '1':
  2350.               case '2':
  2351.               case '3':
  2352.               case '4':
  2353.               case '5':
  2354.               case '6':
  2355.               case '7':
  2356.               case '8':
  2357.               case '9':
  2358.               case '-':
  2359.               case '#':                /* For symbol identification (used in
  2360.                                    live ranges).  */
  2361.                 continue;

  2362.               case ':':
  2363.                 /* It is a C++ nested symbol.  We don't need to record it
  2364.                    (I don't think); if we try to look up foo::bar::baz,
  2365.                    then symbols for the symtab containing foo should get
  2366.                    read in, I think.  */
  2367.                 /* Someone says sun cc puts out symbols like
  2368.                    /foo/baz/maclib::/usr/local/bin/maclib,
  2369.                    which would get here with a symbol type of ':'.  */
  2370.                 continue;

  2371.               default:
  2372.                 /* Unexpected symbol descriptor.  The second and
  2373.                    subsequent stabs of a continued stab can show up
  2374.                    here.  The question is whether they ever can mimic
  2375.                    a normal stab--it would be nice if not, since we
  2376.                    certainly don't want to spend the time searching to
  2377.                    the end of every string looking for a
  2378.                    backslash.  */

  2379.                 complaint (&symfile_complaints,
  2380.                            _("unknown symbol descriptor `%c'"), p[1]);

  2381.                 /* Ignore it; perhaps it is an extension that we don't
  2382.                    know about.  */
  2383.                 continue;
  2384.               }
  2385.           }
  2386.         }
  2387.     }

  2388.   if (pst)
  2389.     {
  2390.       xcoff_end_psymtab (objfile, pst, psymtab_include_list, includes_used,
  2391.                          ssymnum, dependency_list,
  2392.                          dependencies_used, textlow_not_set);
  2393.     }

  2394.   /* Record the toc offset value of this symbol table into objfile
  2395.      structure.  If no XMC_TC0 is found, toc_offset should be zero.
  2396.      Another place to obtain this information would be file auxiliary
  2397.      header.  */

  2398.   XCOFF_DATA (objfile)->toc_offset = toc_offset;
  2399. }

  2400. /* Return the toc offset value for a given objfile.  */

  2401. CORE_ADDR
  2402. xcoff_get_toc_offset (struct objfile *objfile)
  2403. {
  2404.   if (objfile)
  2405.     return XCOFF_DATA (objfile)->toc_offset;
  2406.   return 0;
  2407. }

  2408. /* Scan and build partial symbols for a symbol file.
  2409.    We have been initialized by a call to dbx_symfile_init, which
  2410.    put all the relevant info into a "struct dbx_symfile_info",
  2411.    hung off the objfile structure.

  2412.    SECTION_OFFSETS contains offsets relative to which the symbols in the
  2413.    various sections are (depending where the sections were actually
  2414.    loaded).  */

  2415. static void
  2416. xcoff_initial_scan (struct objfile *objfile, int symfile_flags)
  2417. {
  2418.   bfd *abfd;
  2419.   int val;
  2420.   struct cleanup *back_to;
  2421.   int num_symbols;                /* # of symbols */
  2422.   file_ptr symtab_offset;        /* symbol table and */
  2423.   file_ptr stringtab_offset;        /* string table file offsets */
  2424.   struct coff_symfile_info *info;
  2425.   const char *name;
  2426.   unsigned int size;

  2427.   info = XCOFF_DATA (objfile);
  2428.   symfile_bfd = abfd = objfile->obfd;
  2429.   name = objfile_name (objfile);

  2430.   num_symbols = bfd_get_symcount (abfd);        /* # of symbols */
  2431.   symtab_offset = obj_sym_filepos (abfd);        /* symbol table file offset */
  2432.   stringtab_offset = symtab_offset +
  2433.     num_symbols * coff_data (abfd)->local_symesz;

  2434.   info->min_lineno_offset = 0;
  2435.   info->max_lineno_offset = 0;
  2436.   bfd_map_over_sections (abfd, find_linenos, info);

  2437.   if (num_symbols > 0)
  2438.     {
  2439.       /* Read the string table.  */
  2440.       init_stringtab (abfd, stringtab_offset, objfile);

  2441.       /* Read the .debug section, if present.  */
  2442.       {
  2443.         struct bfd_section *secp;
  2444.         bfd_size_type length;
  2445.         bfd_byte *debugsec = NULL;

  2446.         secp = bfd_get_section_by_name (abfd, ".debug");
  2447.         if (secp)
  2448.           {
  2449.             length = bfd_section_size (abfd, secp);
  2450.             if (length)
  2451.               {
  2452.                 debugsec = obstack_alloc (&objfile->objfile_obstack, length);

  2453.                 if (!bfd_get_full_section_contents (abfd, secp, &debugsec))
  2454.                   {
  2455.                     error (_("Error reading .debug section of `%s': %s"),
  2456.                            name, bfd_errmsg (bfd_get_error ()));
  2457.                   }
  2458.               }
  2459.           }
  2460.         info->debugsec = (char *) debugsec;
  2461.       }
  2462.     }

  2463.   /* Read the symbols.  We keep them in core because we will want to
  2464.      access them randomly in read_symbol*.  */
  2465.   val = bfd_seek (abfd, symtab_offset, SEEK_SET);
  2466.   if (val < 0)
  2467.     error (_("Error reading symbols from %s: %s"),
  2468.            name, bfd_errmsg (bfd_get_error ()));
  2469.   size = coff_data (abfd)->local_symesz * num_symbols;
  2470.   info->symtbl = obstack_alloc (&objfile->objfile_obstack, size);
  2471.   info->symtbl_num_syms = num_symbols;

  2472.   val = bfd_bread (info->symtbl, size, abfd);
  2473.   if (val != size)
  2474.     perror_with_name (_("reading symbol table"));

  2475.   /* If we are reinitializing, or if we have never loaded syms yet, init.  */
  2476.   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
  2477.     /* I'm not sure how how good num_symbols is; the rule of thumb in
  2478.        init_psymbol_list was developed for a.out.  On the one hand,
  2479.        num_symbols includes auxents.  On the other hand, it doesn't
  2480.        include N_SLINE.  */
  2481.     init_psymbol_list (objfile, num_symbols);

  2482.   free_pending_blocks ();
  2483.   back_to = make_cleanup (really_free_pendings, 0);

  2484.   init_minimal_symbol_collection ();
  2485.   make_cleanup_discard_minimal_symbols ();

  2486.   /* Now that the symbol table data of the executable file are all in core,
  2487.      process them and define symbols accordingly.  */

  2488.   scan_xcoff_symtab (objfile);

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

  2491.   install_minimal_symbols (objfile);

  2492.   /* DWARF2 sections.  */

  2493.   if (dwarf2_has_info (objfile, &dwarf2_xcoff_names))
  2494.     dwarf2_build_psymtabs (objfile);

  2495.   dwarf2_build_frame_info (objfile);

  2496.   do_cleanups (back_to);
  2497. }

  2498. static void
  2499. xcoff_symfile_offsets (struct objfile *objfile,
  2500.                        const struct section_addr_info *addrs)
  2501. {
  2502.   const char *first_section_name;

  2503.   default_symfile_offsets (objfile, addrs);

  2504.   /* Oneof the weird side-effects of default_symfile_offsets is that
  2505.      it sometimes sets some section indices to zero for sections that,
  2506.      in fact do not exist. See the body of default_symfile_offsets
  2507.      for more info on when that happens. Undo that, as this then allows
  2508.      us to test whether the associated section exists or not, and then
  2509.      access it quickly (without searching it again).  */

  2510.   if (objfile->num_sections == 0)
  2511.     return; /* Is that even possible?  Better safe than sorry.  */

  2512.   first_section_name
  2513.     = bfd_section_name (objfile->obfd, objfile->sections[0].the_bfd_section);

  2514.   if (objfile->sect_index_text == 0
  2515.       && strcmp (first_section_name, ".text") != 0)
  2516.     objfile->sect_index_text = -1;

  2517.   if (objfile->sect_index_data == 0
  2518.       && strcmp (first_section_name, ".data") != 0)
  2519.     objfile->sect_index_data = -1;

  2520.   if (objfile->sect_index_bss == 0
  2521.       && strcmp (first_section_name, ".bss") != 0)
  2522.     objfile->sect_index_bss = -1;

  2523.   if (objfile->sect_index_rodata == 0
  2524.       && strcmp (first_section_name, ".rodata") != 0)
  2525.     objfile->sect_index_rodata = -1;
  2526. }

  2527. /* Register our ability to parse symbols for xcoff BFD files.  */

  2528. static const struct sym_fns xcoff_sym_fns =
  2529. {

  2530.   /* It is possible that coff and xcoff should be merged as
  2531.      they do have fundamental similarities (for example, the extra storage
  2532.      classes used for stabs could presumably be recognized in any COFF file).
  2533.      However, in addition to obvious things like all the csect hair, there are
  2534.      some subtler differences between xcoffread.c and coffread.c, notably
  2535.      the fact that coffread.c has no need to read in all the symbols, but
  2536.      xcoffread.c reads all the symbols and does in fact randomly access them
  2537.      (in C_BSTAT and line number processing).  */

  2538.   xcoff_new_init,                /* init anything gbl to entire symtab */
  2539.   xcoff_symfile_init,                /* read initial info, setup for sym_read() */
  2540.   xcoff_initial_scan,                /* read a symbol file into symtab */
  2541.   NULL,                                /* sym_read_psymbols */
  2542.   xcoff_symfile_finish,                /* finished with file, cleanup */
  2543.   xcoff_symfile_offsets,        /* xlate offsets ext->int form */
  2544.   default_symfile_segments,        /* Get segment information from a file.  */
  2545.   aix_process_linenos,
  2546.   default_symfile_relocate,        /* Relocate a debug section.  */
  2547.   NULL,                                /* sym_probe_fns */
  2548.   &psym_functions
  2549. };

  2550. /* Same as xcoff_get_n_import_files, but for core files.  */

  2551. static int
  2552. xcoff_get_core_n_import_files (bfd *abfd)
  2553. {
  2554.   asection *sect = bfd_get_section_by_name (abfd, ".ldinfo");
  2555.   gdb_byte buf[4];
  2556.   file_ptr offset = 0;
  2557.   int n_entries = 0;

  2558.   if (sect == NULL)
  2559.     return -1/* Not a core file.  */

  2560.   for (offset = 0; offset < bfd_get_section_size (sect);)
  2561.     {
  2562.       int next;

  2563.       n_entries++;

  2564.       if (!bfd_get_section_contents (abfd, sect, buf, offset, 4))
  2565.         return -1;
  2566.       next = bfd_get_32 (abfd, buf);
  2567.       if (next == 0)
  2568.         break/* This is the last entry.  */
  2569.       offset += next;
  2570.     }

  2571.   /* Return the number of entries, excluding the first one, which is
  2572.      the path to the executable that produced this core file.  */
  2573.   return n_entries - 1;
  2574. }

  2575. /* Return the number of import files (shared libraries) that the given
  2576.    BFD depends on.  Return -1 if this number could not be computed.  */

  2577. int
  2578. xcoff_get_n_import_files (bfd *abfd)
  2579. {
  2580.   asection *sect = bfd_get_section_by_name (abfd, ".loader");
  2581.   gdb_byte buf[4];
  2582.   int l_nimpid;

  2583.   /* If the ".loader" section does not exist, the objfile is probably
  2584.      not an executable.  Might be a core file...  */
  2585.   if (sect == NULL)
  2586.     return xcoff_get_core_n_import_files (abfd);

  2587.   /* The number of entries in the Import Files Table is stored in
  2588.      field l_nimpid.  This field is always at offset 16, and is
  2589.      always 4 bytes long.  Read those 4 bytes.  */

  2590.   if (!bfd_get_section_contents (abfd, sect, buf, 16, 4))
  2591.     return -1;
  2592.   l_nimpid = bfd_get_32 (abfd, buf);

  2593.   /* By convention, the first entry is the default LIBPATH value
  2594.      to be used by the system loader, so it does not count towards
  2595.      the number of import files.  */
  2596.   return l_nimpid - 1;
  2597. }

  2598. /* Free the per-objfile xcoff data.  */

  2599. static void
  2600. xcoff_free_info (struct objfile *objfile, void *arg)
  2601. {
  2602.   xfree (arg);
  2603. }

  2604. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2605. extern initialize_file_ftype _initialize_xcoffread;

  2606. void
  2607. _initialize_xcoffread (void)
  2608. {
  2609.   add_symtab_fns (bfd_target_xcoff_flavour, &xcoff_sym_fns);

  2610.   xcoff_objfile_data_key = register_objfile_data_with_cleanup (NULL,
  2611.                                                                xcoff_free_info);
  2612. }