Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* ieee.c -- Read and write IEEE-695 debugging information.
  2.    Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007,
  3.    2008, 2009, 2010, 2011  Free Software Foundation, Inc.
  4.    Written by Ian Lance Taylor <ian@cygnus.com>.
  5.  
  6.    This file is part of GNU Binutils.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 3 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  21.    02110-1301, USA.  */
  22.  
  23. /* This file reads and writes IEEE-695 debugging information.  */
  24.  
  25. #include "sysdep.h"
  26. #include <assert.h>
  27. #include "bfd.h"
  28. #include "ieee.h"
  29. #include "libiberty.h"
  30. #include "debug.h"
  31. #include "budbg.h"
  32. #include "filenames.h"
  33.  
  34. /* This structure holds an entry on the block stack.  */
  35.  
  36. struct ieee_block
  37. {
  38.   /* The kind of block.  */
  39.   int kind;
  40.   /* The source file name, for a BB5 block.  */
  41.   const char *filename;
  42.   /* The index of the function type, for a BB4 or BB6 block.  */
  43.   unsigned int fnindx;
  44.   /* TRUE if this function is being skipped.  */
  45.   bfd_boolean skip;
  46. };
  47.  
  48. /* This structure is the block stack.  */
  49.  
  50. #define BLOCKSTACK_SIZE (16)
  51.  
  52. struct ieee_blockstack
  53. {
  54.   /* The stack pointer.  */
  55.   struct ieee_block *bsp;
  56.   /* The stack.  */
  57.   struct ieee_block stack[BLOCKSTACK_SIZE];
  58. };
  59.  
  60. /* This structure holds information for a variable.  */
  61.  
  62. enum ieee_var_kind
  63.   {
  64.     IEEE_UNKNOWN,
  65.     IEEE_EXTERNAL,
  66.     IEEE_GLOBAL,
  67.     IEEE_STATIC,
  68.     IEEE_LOCAL,
  69.     IEEE_FUNCTION
  70.   };
  71.  
  72. struct ieee_var
  73. {
  74.   /* Start of name.  */
  75.   const char *name;
  76.   /* Length of name.  */
  77.   unsigned long namlen;
  78.   /* Type.  */
  79.   debug_type type;
  80.   /* Slot if we make an indirect type.  */
  81.   debug_type *pslot;
  82.   /* Kind of variable or function.  */
  83.   enum ieee_var_kind kind;
  84. };
  85.  
  86. /* This structure holds all the variables.  */
  87.  
  88. struct ieee_vars
  89. {
  90.   /* Number of slots allocated.  */
  91.   unsigned int alloc;
  92.   /* Variables.  */
  93.   struct ieee_var *vars;
  94. };
  95.  
  96. /* This structure holds information for a type.  We need this because
  97.    we don't want to represent bitfields as real types.  */
  98.  
  99. struct ieee_type
  100. {
  101.   /* Type.  */
  102.   debug_type type;
  103.   /* Slot if this is type is referenced before it is defined.  */
  104.   debug_type *pslot;
  105.   /* Slots for arguments if we make indirect types for them.  */
  106.   debug_type *arg_slots;
  107.   /* If this is a bitfield, this is the size in bits.  If this is not
  108.      a bitfield, this is zero.  */
  109.   unsigned long bitsize;
  110. };
  111.  
  112. /* This structure holds all the type information.  */
  113.  
  114. struct ieee_types
  115. {
  116.   /* Number of slots allocated.  */
  117.   unsigned int alloc;
  118.   /* Types.  */
  119.   struct ieee_type *types;
  120.   /* Builtin types.  */
  121. #define BUILTIN_TYPE_COUNT (60)
  122.   debug_type builtins[BUILTIN_TYPE_COUNT];
  123. };
  124.  
  125. /* This structure holds a linked last of structs with their tag names,
  126.    so that we can convert them to C++ classes if necessary.  */
  127.  
  128. struct ieee_tag
  129. {
  130.   /* Next tag.  */
  131.   struct ieee_tag *next;
  132.   /* This tag name.  */
  133.   const char *name;
  134.   /* The type of the tag.  */
  135.   debug_type type;
  136.   /* The tagged type is an indirect type pointing at this slot.  */
  137.   debug_type slot;
  138.   /* This is an array of slots used when a field type is converted
  139.      into a indirect type, in case it needs to be later converted into
  140.      a reference type.  */
  141.   debug_type *fslots;
  142. };
  143.  
  144. /* This structure holds the information we pass around to the parsing
  145.    functions.  */
  146.  
  147. struct ieee_info
  148. {
  149.   /* The debugging handle.  */
  150.   void *dhandle;
  151.   /* The BFD.  */
  152.   bfd *abfd;
  153.   /* The start of the bytes to be parsed.  */
  154.   const bfd_byte *bytes;
  155.   /* The end of the bytes to be parsed.  */
  156.   const bfd_byte *pend;
  157.   /* The block stack.  */
  158.   struct ieee_blockstack blockstack;
  159.   /* Whether we have seen a BB1 or BB2.  */
  160.   bfd_boolean saw_filename;
  161.   /* The variables.  */
  162.   struct ieee_vars vars;
  163.   /* The global variables, after a global typedef block.  */
  164.   struct ieee_vars *global_vars;
  165.   /* The types.  */
  166.   struct ieee_types types;
  167.   /* The global types, after a global typedef block.  */
  168.   struct ieee_types *global_types;
  169.   /* The list of tagged structs.  */
  170.   struct ieee_tag *tags;
  171. };
  172.  
  173. /* Basic builtin types, not including the pointers.  */
  174.  
  175. enum builtin_types
  176. {
  177.   builtin_unknown = 0,
  178.   builtin_void = 1,
  179.   builtin_signed_char = 2,
  180.   builtin_unsigned_char = 3,
  181.   builtin_signed_short_int = 4,
  182.   builtin_unsigned_short_int = 5,
  183.   builtin_signed_long = 6,
  184.   builtin_unsigned_long = 7,
  185.   builtin_signed_long_long = 8,
  186.   builtin_unsigned_long_long = 9,
  187.   builtin_float = 10,
  188.   builtin_double = 11,
  189.   builtin_long_double = 12,
  190.   builtin_long_long_double = 13,
  191.   builtin_quoted_string = 14,
  192.   builtin_instruction_address = 15,
  193.   builtin_int = 16,
  194.   builtin_unsigned = 17,
  195.   builtin_unsigned_int = 18,
  196.   builtin_char = 19,
  197.   builtin_long = 20,
  198.   builtin_short = 21,
  199.   builtin_unsigned_short = 22,
  200.   builtin_short_int = 23,
  201.   builtin_signed_short = 24,
  202.   builtin_bcd_float = 25
  203. };
  204.  
  205. /* These are the values found in the derivation flags of a 'b'
  206.    component record of a 'T' type extension record in a C++ pmisc
  207.    record.  These are bitmasks.  */
  208.  
  209. /* Set for a private base class, clear for a public base class.
  210.    Protected base classes are not supported.  */
  211. #define BASEFLAGS_PRIVATE (0x1)
  212. /* Set for a virtual base class.  */
  213. #define BASEFLAGS_VIRTUAL (0x2)
  214. /* Set for a friend class, clear for a base class.  */
  215. #define BASEFLAGS_FRIEND (0x10)
  216.  
  217. /* These are the values found in the specs flags of a 'd', 'm', or 'v'
  218.    component record of a 'T' type extension record in a C++ pmisc
  219.    record.  The same flags are used for a 'M' record in a C++ pmisc
  220.    record.  */
  221.  
  222. /* The lower two bits hold visibility information.  */
  223. #define CXXFLAGS_VISIBILITY (0x3)
  224. /* This value in the lower two bits indicates a public member.  */
  225. #define CXXFLAGS_VISIBILITY_PUBLIC (0x0)
  226. /* This value in the lower two bits indicates a private member.  */
  227. #define CXXFLAGS_VISIBILITY_PRIVATE (0x1)
  228. /* This value in the lower two bits indicates a protected member.  */
  229. #define CXXFLAGS_VISIBILITY_PROTECTED (0x2)
  230. /* Set for a static member.  */
  231. #define CXXFLAGS_STATIC (0x4)
  232. /* Set for a virtual override.  */
  233. #define CXXFLAGS_OVERRIDE (0x8)
  234. /* Set for a friend function.  */
  235. #define CXXFLAGS_FRIEND (0x10)
  236. /* Set for a const function.  */
  237. #define CXXFLAGS_CONST (0x20)
  238. /* Set for a volatile function.  */
  239. #define CXXFLAGS_VOLATILE (0x40)
  240. /* Set for an overloaded function.  */
  241. #define CXXFLAGS_OVERLOADED (0x80)
  242. /* Set for an operator function.  */
  243. #define CXXFLAGS_OPERATOR (0x100)
  244. /* Set for a constructor or destructor.  */
  245. #define CXXFLAGS_CTORDTOR (0x400)
  246. /* Set for a constructor.  */
  247. #define CXXFLAGS_CTOR (0x200)
  248. /* Set for an inline function.  */
  249. #define CXXFLAGS_INLINE (0x800)
  250.  
  251. /* Local functions.  */
  252.  
  253. static void ieee_error (struct ieee_info *, const bfd_byte *, const char *);
  254. static void ieee_eof (struct ieee_info *);
  255. static char *savestring (const char *, unsigned long);
  256. static bfd_boolean ieee_read_number
  257.   (struct ieee_info *, const bfd_byte **, bfd_vma *);
  258. static bfd_boolean ieee_read_optional_number
  259.   (struct ieee_info *, const bfd_byte **, bfd_vma *, bfd_boolean *);
  260. static bfd_boolean ieee_read_id
  261.   (struct ieee_info *, const bfd_byte **, const char **, unsigned long *);
  262. static bfd_boolean ieee_read_optional_id
  263.   (struct ieee_info *, const bfd_byte **, const char **, unsigned long *,
  264.    bfd_boolean *);
  265. static bfd_boolean ieee_read_expression
  266.   (struct ieee_info *, const bfd_byte **, bfd_vma *);
  267. static debug_type ieee_builtin_type
  268.   (struct ieee_info *, const bfd_byte *, unsigned int);
  269. static bfd_boolean ieee_alloc_type
  270.   (struct ieee_info *, unsigned int, bfd_boolean);
  271. static bfd_boolean ieee_read_type_index
  272.   (struct ieee_info *, const bfd_byte **, debug_type *);
  273. static int ieee_regno_to_genreg (bfd *, int);
  274. static int ieee_genreg_to_regno (bfd *, int);
  275. static bfd_boolean parse_ieee_bb (struct ieee_info *, const bfd_byte **);
  276. static bfd_boolean parse_ieee_be (struct ieee_info *, const bfd_byte **);
  277. static bfd_boolean parse_ieee_nn (struct ieee_info *, const bfd_byte **);
  278. static bfd_boolean parse_ieee_ty (struct ieee_info *, const bfd_byte **);
  279. static bfd_boolean parse_ieee_atn (struct ieee_info *, const bfd_byte **);
  280. static bfd_boolean ieee_read_cxx_misc
  281.   (struct ieee_info *, const bfd_byte **, unsigned long);
  282. static bfd_boolean ieee_read_cxx_class
  283.   (struct ieee_info *, const bfd_byte **, unsigned long);
  284. static bfd_boolean ieee_read_cxx_defaults
  285.   (struct ieee_info *, const bfd_byte **, unsigned long);
  286. static bfd_boolean ieee_read_reference
  287.   (struct ieee_info *, const bfd_byte **);
  288. static bfd_boolean ieee_require_asn
  289.   (struct ieee_info *, const bfd_byte **, bfd_vma *);
  290. static bfd_boolean ieee_require_atn65
  291.   (struct ieee_info *, const bfd_byte **, const char **, unsigned long *);
  292.  
  293. /* Report an error in the IEEE debugging information.  */
  294.  
  295. static void
  296. ieee_error (struct ieee_info *info, const bfd_byte *p, const char *s)
  297. {
  298.   if (p != NULL)
  299.     fprintf (stderr, "%s: 0x%lx: %s (0x%x)\n", bfd_get_filename (info->abfd),
  300.              (unsigned long) (p - info->bytes), s, *p);
  301.   else
  302.     fprintf (stderr, "%s: %s\n", bfd_get_filename (info->abfd), s);
  303. }
  304.  
  305. /* Report an unexpected EOF in the IEEE debugging information.  */
  306.  
  307. static void
  308. ieee_eof (struct ieee_info *info)
  309. {
  310.   ieee_error (info, (const bfd_byte *) NULL,
  311.               _("unexpected end of debugging information"));
  312. }
  313.  
  314. /* Save a string in memory.  */
  315.  
  316. static char *
  317. savestring (const char *start, unsigned long len)
  318. {
  319.   char *ret;
  320.  
  321.   ret = (char *) xmalloc (len + 1);
  322.   memcpy (ret, start, len);
  323.   ret[len] = '\0';
  324.   return ret;
  325. }
  326.  
  327. /* Read a number which must be present in an IEEE file.  */
  328.  
  329. static bfd_boolean
  330. ieee_read_number (struct ieee_info *info, const bfd_byte **pp, bfd_vma *pv)
  331. {
  332.   return ieee_read_optional_number (info, pp, pv, (bfd_boolean *) NULL);
  333. }
  334.  
  335. /* Read a number in an IEEE file.  If ppresent is not NULL, the number
  336.    need not be there.  */
  337.  
  338. static bfd_boolean
  339. ieee_read_optional_number (struct ieee_info *info, const bfd_byte **pp,
  340.                            bfd_vma *pv, bfd_boolean *ppresent)
  341. {
  342.   ieee_record_enum_type b;
  343.  
  344.   if (*pp >= info->pend)
  345.     {
  346.       if (ppresent != NULL)
  347.         {
  348.           *ppresent = FALSE;
  349.           return TRUE;
  350.         }
  351.       ieee_eof (info);
  352.       return FALSE;
  353.     }
  354.  
  355.   b = (ieee_record_enum_type) **pp;
  356.   ++*pp;
  357.  
  358.   if (b <= ieee_number_end_enum)
  359.     {
  360.       *pv = (bfd_vma) b;
  361.       if (ppresent != NULL)
  362.         *ppresent = TRUE;
  363.       return TRUE;
  364.     }
  365.  
  366.   if (b >= ieee_number_repeat_start_enum && b <= ieee_number_repeat_end_enum)
  367.     {
  368.       unsigned int i;
  369.  
  370.       i = (int) b - (int) ieee_number_repeat_start_enum;
  371.       if (*pp + i - 1 >= info->pend)
  372.         {
  373.           ieee_eof (info);
  374.           return FALSE;
  375.         }
  376.  
  377.       *pv = 0;
  378.       for (; i > 0; i--)
  379.         {
  380.           *pv <<= 8;
  381.           *pv += **pp;
  382.           ++*pp;
  383.         }
  384.  
  385.       if (ppresent != NULL)
  386.         *ppresent = TRUE;
  387.  
  388.       return TRUE;
  389.     }
  390.  
  391.   if (ppresent != NULL)
  392.     {
  393.       --*pp;
  394.       *ppresent = FALSE;
  395.       return TRUE;
  396.     }
  397.  
  398.   ieee_error (info, *pp - 1, _("invalid number"));
  399.   return FALSE;
  400. }
  401.  
  402. /* Read a required string from an IEEE file.  */
  403.  
  404. static bfd_boolean
  405. ieee_read_id (struct ieee_info *info, const bfd_byte **pp,
  406.               const char **pname, unsigned long *pnamlen)
  407. {
  408.   return ieee_read_optional_id (info, pp, pname, pnamlen, (bfd_boolean *) NULL);
  409. }
  410.  
  411. /* Read a string from an IEEE file.  If ppresent is not NULL, the
  412.    string is optional.  */
  413.  
  414. static bfd_boolean
  415. ieee_read_optional_id (struct ieee_info *info, const bfd_byte **pp,
  416.                        const char **pname, unsigned long *pnamlen,
  417.                        bfd_boolean *ppresent)
  418. {
  419.   bfd_byte b;
  420.   unsigned long len;
  421.  
  422.   if (*pp >= info->pend)
  423.     {
  424.       ieee_eof (info);
  425.       return FALSE;
  426.     }
  427.  
  428.   b = **pp;
  429.   ++*pp;
  430.  
  431.   if (b <= 0x7f)
  432.     len = b;
  433.   else if ((ieee_record_enum_type) b == ieee_extension_length_1_enum)
  434.     {
  435.       len = **pp;
  436.       ++*pp;
  437.     }
  438.   else if ((ieee_record_enum_type) b == ieee_extension_length_2_enum)
  439.     {
  440.       len = (**pp << 8) + (*pp)[1];
  441.       *pp += 2;
  442.     }
  443.   else
  444.     {
  445.       if (ppresent != NULL)
  446.         {
  447.           --*pp;
  448.           *ppresent = FALSE;
  449.           return TRUE;
  450.         }
  451.       ieee_error (info, *pp - 1, _("invalid string length"));
  452.       return FALSE;
  453.     }
  454.  
  455.   if ((unsigned long) (info->pend - *pp) < len)
  456.     {
  457.       ieee_eof (info);
  458.       return FALSE;
  459.     }
  460.  
  461.   *pname = (const char *) *pp;
  462.   *pnamlen = len;
  463.   *pp += len;
  464.  
  465.   if (ppresent != NULL)
  466.     *ppresent = TRUE;
  467.  
  468.   return TRUE;
  469. }
  470.  
  471. /* Read an expression from an IEEE file.  Since this code is only used
  472.    to parse debugging information, I haven't bothered to write a full
  473.    blown IEEE expression parser.  I've only thrown in the things I've
  474.    seen in debugging information.  This can be easily extended if
  475.    necessary.  */
  476.  
  477. static bfd_boolean
  478. ieee_read_expression (struct ieee_info *info, const bfd_byte **pp,
  479.                       bfd_vma *pv)
  480. {
  481.   const bfd_byte *expr_start;
  482. #define EXPR_STACK_SIZE (10)
  483.   bfd_vma expr_stack[EXPR_STACK_SIZE];
  484.   bfd_vma *esp;
  485.  
  486.   expr_start = *pp;
  487.  
  488.   esp = expr_stack;
  489.  
  490.   while (1)
  491.     {
  492.       const bfd_byte *start;
  493.       bfd_vma val;
  494.       bfd_boolean present;
  495.       ieee_record_enum_type c;
  496.  
  497.       start = *pp;
  498.  
  499.       if (! ieee_read_optional_number (info, pp, &val, &present))
  500.         return FALSE;
  501.  
  502.       if (present)
  503.         {
  504.           if (esp - expr_stack >= EXPR_STACK_SIZE)
  505.             {
  506.               ieee_error (info, start, _("expression stack overflow"));
  507.               return FALSE;
  508.             }
  509.           *esp++ = val;
  510.           continue;
  511.         }
  512.  
  513.       c = (ieee_record_enum_type) **pp;
  514.  
  515.       if (c >= ieee_module_beginning_enum)
  516.         break;
  517.  
  518.       ++*pp;
  519.  
  520.       if (c == ieee_comma)
  521.         break;
  522.  
  523.       switch (c)
  524.         {
  525.         default:
  526.           ieee_error (info, start, _("unsupported IEEE expression operator"));
  527.           break;
  528.  
  529.         case ieee_variable_R_enum:
  530.           {
  531.             bfd_vma indx;
  532.             asection *s;
  533.  
  534.             if (! ieee_read_number (info, pp, &indx))
  535.               return FALSE;
  536.             for (s = info->abfd->sections; s != NULL; s = s->next)
  537.               if ((bfd_vma) s->target_index == indx)
  538.                 break;
  539.             if (s == NULL)
  540.               {
  541.                 ieee_error (info, start, _("unknown section"));
  542.                 return FALSE;
  543.               }
  544.  
  545.             if (esp - expr_stack >= EXPR_STACK_SIZE)
  546.               {
  547.                 ieee_error (info, start, _("expression stack overflow"));
  548.                 return FALSE;
  549.               }
  550.  
  551.             *esp++ = bfd_get_section_vma (info->abfd, s);
  552.           }
  553.           break;
  554.  
  555.         case ieee_function_plus_enum:
  556.         case ieee_function_minus_enum:
  557.           {
  558.             bfd_vma v1, v2;
  559.  
  560.             if (esp - expr_stack < 2)
  561.               {
  562.                 ieee_error (info, start, _("expression stack underflow"));
  563.                 return FALSE;
  564.               }
  565.  
  566.             v1 = *--esp;
  567.             v2 = *--esp;
  568.             *esp++ = v1 + v2;
  569.           }
  570.           break;
  571.         }
  572.     }
  573.  
  574.   if (esp - 1 != expr_stack)
  575.     {
  576.       ieee_error (info, expr_start, _("expression stack mismatch"));
  577.       return FALSE;
  578.     }
  579.  
  580.   *pv = *--esp;
  581.  
  582.   return TRUE;
  583. }
  584.  
  585. /* Return an IEEE builtin type.  */
  586.  
  587. static debug_type
  588. ieee_builtin_type (struct ieee_info *info, const bfd_byte *p,
  589.                    unsigned int indx)
  590. {
  591.   void *dhandle;
  592.   debug_type type;
  593.   const char *name;
  594.  
  595.   if (indx < BUILTIN_TYPE_COUNT
  596.       && info->types.builtins[indx] != DEBUG_TYPE_NULL)
  597.     return info->types.builtins[indx];
  598.  
  599.   dhandle = info->dhandle;
  600.  
  601.   if (indx >= 32 && indx < 64)
  602.     {
  603.       type = debug_make_pointer_type (dhandle,
  604.                                       ieee_builtin_type (info, p, indx - 32));
  605.       assert (indx < BUILTIN_TYPE_COUNT);
  606.       info->types.builtins[indx] = type;
  607.       return type;
  608.     }
  609.  
  610.   switch ((enum builtin_types) indx)
  611.     {
  612.     default:
  613.       ieee_error (info, p, _("unknown builtin type"));
  614.       return NULL;
  615.  
  616.     case builtin_unknown:
  617.       type = debug_make_void_type (dhandle);
  618.       name = NULL;
  619.       break;
  620.  
  621.     case builtin_void:
  622.       type = debug_make_void_type (dhandle);
  623.       name = "void";
  624.       break;
  625.  
  626.     case builtin_signed_char:
  627.       type = debug_make_int_type (dhandle, 1, FALSE);
  628.       name = "signed char";
  629.       break;
  630.  
  631.     case builtin_unsigned_char:
  632.       type = debug_make_int_type (dhandle, 1, TRUE);
  633.       name = "unsigned char";
  634.       break;
  635.  
  636.     case builtin_signed_short_int:
  637.       type = debug_make_int_type (dhandle, 2, FALSE);
  638.       name = "signed short int";
  639.       break;
  640.  
  641.     case builtin_unsigned_short_int:
  642.       type = debug_make_int_type (dhandle, 2, TRUE);
  643.       name = "unsigned short int";
  644.       break;
  645.  
  646.     case builtin_signed_long:
  647.       type = debug_make_int_type (dhandle, 4, FALSE);
  648.       name = "signed long";
  649.       break;
  650.  
  651.     case builtin_unsigned_long:
  652.       type = debug_make_int_type (dhandle, 4, TRUE);
  653.       name = "unsigned long";
  654.       break;
  655.  
  656.     case builtin_signed_long_long:
  657.       type = debug_make_int_type (dhandle, 8, FALSE);
  658.       name = "signed long long";
  659.       break;
  660.  
  661.     case builtin_unsigned_long_long:
  662.       type = debug_make_int_type (dhandle, 8, TRUE);
  663.       name = "unsigned long long";
  664.       break;
  665.  
  666.     case builtin_float:
  667.       type = debug_make_float_type (dhandle, 4);
  668.       name = "float";
  669.       break;
  670.  
  671.     case builtin_double:
  672.       type = debug_make_float_type (dhandle, 8);
  673.       name = "double";
  674.       break;
  675.  
  676.     case builtin_long_double:
  677.       /* FIXME: The size for this type should depend upon the
  678.          processor.  */
  679.       type = debug_make_float_type (dhandle, 12);
  680.       name = "long double";
  681.       break;
  682.  
  683.     case builtin_long_long_double:
  684.       type = debug_make_float_type (dhandle, 16);
  685.       name = "long long double";
  686.       break;
  687.  
  688.     case builtin_quoted_string:
  689.       type = debug_make_array_type (dhandle,
  690.                                     ieee_builtin_type (info, p,
  691.                                                        ((unsigned int)
  692.                                                         builtin_char)),
  693.                                     ieee_builtin_type (info, p,
  694.                                                        ((unsigned int)
  695.                                                         builtin_int)),
  696.                                     0, -1, TRUE);
  697.       name = "QUOTED STRING";
  698.       break;
  699.  
  700.     case builtin_instruction_address:
  701.       /* FIXME: This should be a code address.  */
  702.       type = debug_make_int_type (dhandle, 4, TRUE);
  703.       name = "instruction address";
  704.       break;
  705.  
  706.     case builtin_int:
  707.       /* FIXME: The size for this type should depend upon the
  708.          processor.  */
  709.       type = debug_make_int_type (dhandle, 4, FALSE);
  710.       name = "int";
  711.       break;
  712.  
  713.     case builtin_unsigned:
  714.       /* FIXME: The size for this type should depend upon the
  715.          processor.  */
  716.       type = debug_make_int_type (dhandle, 4, TRUE);
  717.       name = "unsigned";
  718.       break;
  719.  
  720.     case builtin_unsigned_int:
  721.       /* FIXME: The size for this type should depend upon the
  722.          processor.  */
  723.       type = debug_make_int_type (dhandle, 4, TRUE);
  724.       name = "unsigned int";
  725.       break;
  726.  
  727.     case builtin_char:
  728.       type = debug_make_int_type (dhandle, 1, FALSE);
  729.       name = "char";
  730.       break;
  731.  
  732.     case builtin_long:
  733.       type = debug_make_int_type (dhandle, 4, FALSE);
  734.       name = "long";
  735.       break;
  736.  
  737.     case builtin_short:
  738.       type = debug_make_int_type (dhandle, 2, FALSE);
  739.       name = "short";
  740.       break;
  741.  
  742.     case builtin_unsigned_short:
  743.       type = debug_make_int_type (dhandle, 2, TRUE);
  744.       name = "unsigned short";
  745.       break;
  746.  
  747.     case builtin_short_int:
  748.       type = debug_make_int_type (dhandle, 2, FALSE);
  749.       name = "short int";
  750.       break;
  751.  
  752.     case builtin_signed_short:
  753.       type = debug_make_int_type (dhandle, 2, FALSE);
  754.       name = "signed short";
  755.       break;
  756.  
  757.     case builtin_bcd_float:
  758.       ieee_error (info, p, _("BCD float type not supported"));
  759.       return DEBUG_TYPE_NULL;
  760.     }
  761.  
  762.   if (name != NULL)
  763.     type = debug_name_type (dhandle, name, type);
  764.  
  765.   assert (indx < BUILTIN_TYPE_COUNT);
  766.  
  767.   info->types.builtins[indx] = type;
  768.  
  769.   return type;
  770. }
  771.  
  772. /* Allocate more space in the type table.  If ref is TRUE, this is a
  773.    reference to the type; if it is not already defined, we should set
  774.    up an indirect type.  */
  775.  
  776. static bfd_boolean
  777. ieee_alloc_type (struct ieee_info *info, unsigned int indx, bfd_boolean ref)
  778. {
  779.   unsigned int nalloc;
  780.   register struct ieee_type *t;
  781.   struct ieee_type *tend;
  782.  
  783.   if (indx >= info->types.alloc)
  784.     {
  785.       nalloc = info->types.alloc;
  786.       if (nalloc == 0)
  787.         nalloc = 4;
  788.       while (indx >= nalloc)
  789.         nalloc *= 2;
  790.  
  791.       info->types.types = ((struct ieee_type *)
  792.                            xrealloc (info->types.types,
  793.                                      nalloc * sizeof *info->types.types));
  794.  
  795.       memset (info->types.types + info->types.alloc, 0,
  796.               (nalloc - info->types.alloc) * sizeof *info->types.types);
  797.  
  798.       tend = info->types.types + nalloc;
  799.       for (t = info->types.types + info->types.alloc; t < tend; t++)
  800.         t->type = DEBUG_TYPE_NULL;
  801.  
  802.       info->types.alloc = nalloc;
  803.     }
  804.  
  805.   if (ref)
  806.     {
  807.       t = info->types.types + indx;
  808.       if (t->type == NULL)
  809.         {
  810.           t->pslot = (debug_type *) xmalloc (sizeof *t->pslot);
  811.           *t->pslot = DEBUG_TYPE_NULL;
  812.           t->type = debug_make_indirect_type (info->dhandle, t->pslot,
  813.                                               (const char *) NULL);
  814.           if (t->type == NULL)
  815.             return FALSE;
  816.         }
  817.     }
  818.  
  819.   return TRUE;
  820. }
  821.  
  822. /* Read a type index and return the corresponding type.  */
  823.  
  824. static bfd_boolean
  825. ieee_read_type_index (struct ieee_info *info, const bfd_byte **pp,
  826.                       debug_type *ptype)
  827. {
  828.   const bfd_byte *start;
  829.   bfd_vma indx;
  830.  
  831.   start = *pp;
  832.  
  833.   if (! ieee_read_number (info, pp, &indx))
  834.     return FALSE;
  835.  
  836.   if (indx < 256)
  837.     {
  838.       *ptype = ieee_builtin_type (info, start, indx);
  839.       if (*ptype == NULL)
  840.         return FALSE;
  841.       return TRUE;
  842.     }
  843.  
  844.   indx -= 256;
  845.   if (! ieee_alloc_type (info, indx, TRUE))
  846.     return FALSE;
  847.  
  848.   *ptype = info->types.types[indx].type;
  849.  
  850.   return TRUE;
  851. }
  852.  
  853. /* Parse IEEE debugging information for a file.  This is passed the
  854.    bytes which compose the Debug Information Part of an IEEE file.  */
  855.  
  856. bfd_boolean
  857. parse_ieee (void *dhandle, bfd *abfd, const bfd_byte *bytes, bfd_size_type len)
  858. {
  859.   struct ieee_info info;
  860.   unsigned int i;
  861.   const bfd_byte *p, *pend;
  862.  
  863.   info.dhandle = dhandle;
  864.   info.abfd = abfd;
  865.   info.bytes = bytes;
  866.   info.pend = bytes + len;
  867.   info.blockstack.bsp = info.blockstack.stack;
  868.   info.saw_filename = FALSE;
  869.   info.vars.alloc = 0;
  870.   info.vars.vars = NULL;
  871.   info.global_vars = NULL;
  872.   info.types.alloc = 0;
  873.   info.types.types = NULL;
  874.   info.global_types = NULL;
  875.   info.tags = NULL;
  876.   for (i = 0; i < BUILTIN_TYPE_COUNT; i++)
  877.     info.types.builtins[i] = DEBUG_TYPE_NULL;
  878.  
  879.   p = bytes;
  880.   pend = info.pend;
  881.   while (p < pend)
  882.     {
  883.       const bfd_byte *record_start;
  884.       ieee_record_enum_type c;
  885.  
  886.       record_start = p;
  887.  
  888.       c = (ieee_record_enum_type) *p++;
  889.  
  890.       if (c == ieee_at_record_enum)
  891.         c = (ieee_record_enum_type) (((unsigned int) c << 8) | *p++);
  892.  
  893.       if (c <= ieee_number_repeat_end_enum)
  894.         {
  895.           ieee_error (&info, record_start, _("unexpected number"));
  896.           return FALSE;
  897.         }
  898.  
  899.       switch (c)
  900.         {
  901.         default:
  902.           ieee_error (&info, record_start, _("unexpected record type"));
  903.           return FALSE;
  904.  
  905.         case ieee_bb_record_enum:
  906.           if (! parse_ieee_bb (&info, &p))
  907.             return FALSE;
  908.           break;
  909.  
  910.         case ieee_be_record_enum:
  911.           if (! parse_ieee_be (&info, &p))
  912.             return FALSE;
  913.           break;
  914.  
  915.         case ieee_nn_record:
  916.           if (! parse_ieee_nn (&info, &p))
  917.             return FALSE;
  918.           break;
  919.  
  920.         case ieee_ty_record_enum:
  921.           if (! parse_ieee_ty (&info, &p))
  922.             return FALSE;
  923.           break;
  924.  
  925.         case ieee_atn_record_enum:
  926.           if (! parse_ieee_atn (&info, &p))
  927.             return FALSE;
  928.           break;
  929.         }
  930.     }
  931.  
  932.   if (info.blockstack.bsp != info.blockstack.stack)
  933.     {
  934.       ieee_error (&info, (const bfd_byte *) NULL,
  935.                   _("blocks left on stack at end"));
  936.       return FALSE;
  937.     }
  938.  
  939.   return TRUE;
  940. }
  941.  
  942. /* Handle an IEEE BB record.  */
  943.  
  944. static bfd_boolean
  945. parse_ieee_bb (struct ieee_info *info, const bfd_byte **pp)
  946. {
  947.   const bfd_byte *block_start;
  948.   bfd_byte b;
  949.   bfd_vma size;
  950.   const char *name;
  951.   unsigned long namlen;
  952.   char *namcopy = NULL;
  953.   unsigned int fnindx;
  954.   bfd_boolean skip;
  955.  
  956.   block_start = *pp;
  957.  
  958.   b = **pp;
  959.   ++*pp;
  960.  
  961.   if (! ieee_read_number (info, pp, &size)
  962.       || ! ieee_read_id (info, pp, &name, &namlen))
  963.     return FALSE;
  964.  
  965.   fnindx = (unsigned int) -1;
  966.   skip = FALSE;
  967.  
  968.   switch (b)
  969.     {
  970.     case 1:
  971.       /* BB1: Type definitions local to a module.  */
  972.       namcopy = savestring (name, namlen);
  973.       if (namcopy == NULL)
  974.         return FALSE;
  975.       if (! debug_set_filename (info->dhandle, namcopy))
  976.         return FALSE;
  977.       info->saw_filename = TRUE;
  978.  
  979.       /* Discard any variables or types we may have seen before.  */
  980.       if (info->vars.vars != NULL)
  981.         free (info->vars.vars);
  982.       info->vars.vars = NULL;
  983.       info->vars.alloc = 0;
  984.       if (info->types.types != NULL)
  985.         free (info->types.types);
  986.       info->types.types = NULL;
  987.       info->types.alloc = 0;
  988.  
  989.       /* Initialize the types to the global types.  */
  990.       if (info->global_types != NULL)
  991.         {
  992.           info->types.alloc = info->global_types->alloc;
  993.           info->types.types = ((struct ieee_type *)
  994.                                xmalloc (info->types.alloc
  995.                                         * sizeof (*info->types.types)));
  996.           memcpy (info->types.types, info->global_types->types,
  997.                   info->types.alloc * sizeof (*info->types.types));
  998.         }
  999.  
  1000.       break;
  1001.  
  1002.     case 2:
  1003.       /* BB2: Global type definitions.  The name is supposed to be
  1004.          empty, but we don't check.  */
  1005.       if (! debug_set_filename (info->dhandle, "*global*"))
  1006.         return FALSE;
  1007.       info->saw_filename = TRUE;
  1008.       break;
  1009.  
  1010.     case 3:
  1011.       /* BB3: High level module block begin.  We don't have to do
  1012.          anything here.  The name is supposed to be the same as for
  1013.          the BB1, but we don't check.  */
  1014.       break;
  1015.  
  1016.     case 4:
  1017.       /* BB4: Global function.  */
  1018.       {
  1019.         bfd_vma stackspace, typindx, offset;
  1020.         debug_type return_type;
  1021.  
  1022.         if (! ieee_read_number (info, pp, &stackspace)
  1023.             || ! ieee_read_number (info, pp, &typindx)
  1024.             || ! ieee_read_expression (info, pp, &offset))
  1025.           return FALSE;
  1026.  
  1027.         /* We have no way to record the stack space.  FIXME.  */
  1028.  
  1029.         if (typindx < 256)
  1030.           {
  1031.             return_type = ieee_builtin_type (info, block_start, typindx);
  1032.             if (return_type == DEBUG_TYPE_NULL)
  1033.               return FALSE;
  1034.           }
  1035.         else
  1036.           {
  1037.             typindx -= 256;
  1038.             if (! ieee_alloc_type (info, typindx, TRUE))
  1039.               return FALSE;
  1040.             fnindx = typindx;
  1041.             return_type = info->types.types[typindx].type;
  1042.             if (debug_get_type_kind (info->dhandle, return_type)
  1043.                 == DEBUG_KIND_FUNCTION)
  1044.               return_type = debug_get_return_type (info->dhandle,
  1045.                                                    return_type);
  1046.           }
  1047.  
  1048.         namcopy = savestring (name, namlen);
  1049.         if (namcopy == NULL)
  1050.           return FALSE;
  1051.         if (! debug_record_function (info->dhandle, namcopy, return_type,
  1052.                                      TRUE, offset))
  1053.           return FALSE;
  1054.       }
  1055.       break;
  1056.  
  1057.     case 5:
  1058.       /* BB5: File name for source line numbers.  */
  1059.       {
  1060.         unsigned int i;
  1061.  
  1062.         /* We ignore the date and time.  FIXME.  */
  1063.         for (i = 0; i < 6; i++)
  1064.           {
  1065.             bfd_vma ignore;
  1066.             bfd_boolean present;
  1067.  
  1068.             if (! ieee_read_optional_number (info, pp, &ignore, &present))
  1069.               return FALSE;
  1070.             if (! present)
  1071.               break;
  1072.           }
  1073.  
  1074.         if (! info->saw_filename)
  1075.           {
  1076.             namcopy = savestring (name, namlen);
  1077.             if (namcopy == NULL)
  1078.               return FALSE;
  1079.             if (! debug_set_filename (info->dhandle, namcopy))
  1080.               return FALSE;
  1081.             info->saw_filename = TRUE;
  1082.           }
  1083.  
  1084.         namcopy = savestring (name, namlen);
  1085.         if (namcopy == NULL)
  1086.           return FALSE;
  1087.         if (! debug_start_source (info->dhandle, namcopy))
  1088.           return FALSE;
  1089.       }
  1090.       break;
  1091.  
  1092.     case 6:
  1093.       /* BB6: Local function or block.  */
  1094.       {
  1095.         bfd_vma stackspace, typindx, offset;
  1096.  
  1097.         if (! ieee_read_number (info, pp, &stackspace)
  1098.             || ! ieee_read_number (info, pp, &typindx)
  1099.             || ! ieee_read_expression (info, pp, &offset))
  1100.           return FALSE;
  1101.  
  1102.         /* We have no way to record the stack space.  FIXME.  */
  1103.  
  1104.         if (namlen == 0)
  1105.           {
  1106.             if (! debug_start_block (info->dhandle, offset))
  1107.               return FALSE;
  1108.             /* Change b to indicate that this is a block
  1109.                rather than a function.  */
  1110.             b = 0x86;
  1111.           }
  1112.         else
  1113.           {
  1114.             /* The MRI C++ compiler will output a fake function named
  1115.                __XRYCPP to hold C++ debugging information.  We skip
  1116.                that function.  This is not crucial, but it makes
  1117.                converting from IEEE to other debug formats work
  1118.                better.  */
  1119.             if (strncmp (name, "__XRYCPP", namlen) == 0)
  1120.               skip = TRUE;
  1121.             else
  1122.               {
  1123.                 debug_type return_type;
  1124.  
  1125.                 if (typindx < 256)
  1126.                   {
  1127.                     return_type = ieee_builtin_type (info, block_start,
  1128.                                                      typindx);
  1129.                     if (return_type == NULL)
  1130.                       return FALSE;
  1131.                   }
  1132.                 else
  1133.                   {
  1134.                     typindx -= 256;
  1135.                     if (! ieee_alloc_type (info, typindx, TRUE))
  1136.                       return FALSE;
  1137.                     fnindx = typindx;
  1138.                     return_type = info->types.types[typindx].type;
  1139.                     if (debug_get_type_kind (info->dhandle, return_type)
  1140.                         == DEBUG_KIND_FUNCTION)
  1141.                       return_type = debug_get_return_type (info->dhandle,
  1142.                                                            return_type);
  1143.                   }
  1144.  
  1145.                 namcopy = savestring (name, namlen);
  1146.                 if (namcopy == NULL)
  1147.                   return FALSE;
  1148.                 if (! debug_record_function (info->dhandle, namcopy,
  1149.                                              return_type, FALSE, offset))
  1150.                   return FALSE;
  1151.               }
  1152.           }
  1153.       }
  1154.       break;
  1155.  
  1156.     case 10:
  1157.       /* BB10: Assembler module scope.  In the normal case, we
  1158.          completely ignore all this information.  FIXME.  */
  1159.       {
  1160.         const char *inam, *vstr;
  1161.         unsigned long inamlen, vstrlen;
  1162.         bfd_vma tool_type;
  1163.         bfd_boolean present;
  1164.         unsigned int i;
  1165.  
  1166.         if (! info->saw_filename)
  1167.           {
  1168.             namcopy = savestring (name, namlen);
  1169.             if (namcopy == NULL)
  1170.               return FALSE;
  1171.             if (! debug_set_filename (info->dhandle, namcopy))
  1172.               return FALSE;
  1173.             info->saw_filename = TRUE;
  1174.           }
  1175.  
  1176.         if (! ieee_read_id (info, pp, &inam, &inamlen)
  1177.             || ! ieee_read_number (info, pp, &tool_type)
  1178.             || ! ieee_read_optional_id (info, pp, &vstr, &vstrlen, &present))
  1179.           return FALSE;
  1180.         for (i = 0; i < 6; i++)
  1181.           {
  1182.             bfd_vma ignore;
  1183.  
  1184.             if (! ieee_read_optional_number (info, pp, &ignore, &present))
  1185.               return FALSE;
  1186.             if (! present)
  1187.               break;
  1188.           }
  1189.       }
  1190.       break;
  1191.  
  1192.     case 11:
  1193.       /* BB11: Module section.  We completely ignore all this
  1194.          information.  FIXME.  */
  1195.       {
  1196.         bfd_vma sectype, secindx, offset, map;
  1197.         bfd_boolean present;
  1198.  
  1199.         if (! ieee_read_number (info, pp, &sectype)
  1200.             || ! ieee_read_number (info, pp, &secindx)
  1201.             || ! ieee_read_expression (info, pp, &offset)
  1202.             || ! ieee_read_optional_number (info, pp, &map, &present))
  1203.           return FALSE;
  1204.       }
  1205.       break;
  1206.  
  1207.     default:
  1208.       ieee_error (info, block_start, _("unknown BB type"));
  1209.       return FALSE;
  1210.     }
  1211.  
  1212.  
  1213.   /* Push this block on the block stack.  */
  1214.  
  1215.   if (info->blockstack.bsp >= info->blockstack.stack + BLOCKSTACK_SIZE)
  1216.     {
  1217.       ieee_error (info, (const bfd_byte *) NULL, _("stack overflow"));
  1218.       return FALSE;
  1219.     }
  1220.  
  1221.   info->blockstack.bsp->kind = b;
  1222.   if (b == 5)
  1223.     info->blockstack.bsp->filename = namcopy;
  1224.   info->blockstack.bsp->fnindx = fnindx;
  1225.   info->blockstack.bsp->skip = skip;
  1226.   ++info->blockstack.bsp;
  1227.  
  1228.   return TRUE;
  1229. }
  1230.  
  1231. /* Handle an IEEE BE record.  */
  1232.  
  1233. static bfd_boolean
  1234. parse_ieee_be (struct ieee_info *info, const bfd_byte **pp)
  1235. {
  1236.   bfd_vma offset;
  1237.  
  1238.   if (info->blockstack.bsp <= info->blockstack.stack)
  1239.     {
  1240.       ieee_error (info, *pp, _("stack underflow"));
  1241.       return FALSE;
  1242.     }
  1243.   --info->blockstack.bsp;
  1244.  
  1245.   switch (info->blockstack.bsp->kind)
  1246.     {
  1247.     case 2:
  1248.       /* When we end the global typedefs block, we copy out the
  1249.          contents of info->vars.  This is because the variable indices
  1250.          may be reused in the local blocks.  However, we need to
  1251.          preserve them so that we can locate a function returning a
  1252.          reference variable whose type is named in the global typedef
  1253.          block.  */
  1254.       info->global_vars = ((struct ieee_vars *)
  1255.                            xmalloc (sizeof *info->global_vars));
  1256.       info->global_vars->alloc = info->vars.alloc;
  1257.       info->global_vars->vars = ((struct ieee_var *)
  1258.                                  xmalloc (info->vars.alloc
  1259.                                           * sizeof (*info->vars.vars)));
  1260.       memcpy (info->global_vars->vars, info->vars.vars,
  1261.               info->vars.alloc * sizeof (*info->vars.vars));
  1262.  
  1263.       /* We also copy out the non builtin parts of info->types, since
  1264.          the types are discarded when we start a new block.  */
  1265.       info->global_types = ((struct ieee_types *)
  1266.                             xmalloc (sizeof *info->global_types));
  1267.       info->global_types->alloc = info->types.alloc;
  1268.       info->global_types->types = ((struct ieee_type *)
  1269.                                    xmalloc (info->types.alloc
  1270.                                             * sizeof (*info->types.types)));
  1271.       memcpy (info->global_types->types, info->types.types,
  1272.               info->types.alloc * sizeof (*info->types.types));
  1273.       memset (info->global_types->builtins, 0,
  1274.               sizeof (info->global_types->builtins));
  1275.  
  1276.       break;
  1277.  
  1278.     case 4:
  1279.     case 6:
  1280.       if (! ieee_read_expression (info, pp, &offset))
  1281.         return FALSE;
  1282.       if (! info->blockstack.bsp->skip)
  1283.         {
  1284.           if (! debug_end_function (info->dhandle, offset + 1))
  1285.             return FALSE;
  1286.         }
  1287.       break;
  1288.  
  1289.     case 0x86:
  1290.       /* This is BE6 when BB6 started a block rather than a local
  1291.          function.  */
  1292.       if (! ieee_read_expression (info, pp, &offset))
  1293.         return FALSE;
  1294.       if (! debug_end_block (info->dhandle, offset + 1))
  1295.         return FALSE;
  1296.       break;
  1297.  
  1298.     case 5:
  1299.       /* When we end a BB5, we look up the stack for the last BB5, if
  1300.          there is one, so that we can call debug_start_source.  */
  1301.       if (info->blockstack.bsp > info->blockstack.stack)
  1302.         {
  1303.           struct ieee_block *bl;
  1304.  
  1305.           bl = info->blockstack.bsp;
  1306.           do
  1307.             {
  1308.               --bl;
  1309.               if (bl->kind == 5)
  1310.                 {
  1311.                   if (! debug_start_source (info->dhandle, bl->filename))
  1312.                     return FALSE;
  1313.                   break;
  1314.                 }
  1315.             }
  1316.           while (bl != info->blockstack.stack);
  1317.         }
  1318.       break;
  1319.  
  1320.     case 11:
  1321.       if (! ieee_read_expression (info, pp, &offset))
  1322.         return FALSE;
  1323.       /* We just ignore the module size.  FIXME.  */
  1324.       break;
  1325.  
  1326.     default:
  1327.       /* Other block types do not have any trailing information.  */
  1328.       break;
  1329.     }
  1330.  
  1331.   return TRUE;
  1332. }
  1333.  
  1334. /* Parse an NN record.  */
  1335.  
  1336. static bfd_boolean
  1337. parse_ieee_nn (struct ieee_info *info, const bfd_byte **pp)
  1338. {
  1339.   const bfd_byte *nn_start;
  1340.   bfd_vma varindx;
  1341.   const char *name;
  1342.   unsigned long namlen;
  1343.  
  1344.   nn_start = *pp;
  1345.  
  1346.   if (! ieee_read_number (info, pp, &varindx)
  1347.       || ! ieee_read_id (info, pp, &name, &namlen))
  1348.     return FALSE;
  1349.  
  1350.   if (varindx < 32)
  1351.     {
  1352.       ieee_error (info, nn_start, _("illegal variable index"));
  1353.       return FALSE;
  1354.     }
  1355.   varindx -= 32;
  1356.  
  1357.   if (varindx >= info->vars.alloc)
  1358.     {
  1359.       unsigned int alloc;
  1360.  
  1361.       alloc = info->vars.alloc;
  1362.       if (alloc == 0)
  1363.         alloc = 4;
  1364.       while (varindx >= alloc)
  1365.         alloc *= 2;
  1366.       info->vars.vars = ((struct ieee_var *)
  1367.                          xrealloc (info->vars.vars,
  1368.                                    alloc * sizeof *info->vars.vars));
  1369.       memset (info->vars.vars + info->vars.alloc, 0,
  1370.               (alloc - info->vars.alloc) * sizeof *info->vars.vars);
  1371.       info->vars.alloc = alloc;
  1372.     }
  1373.  
  1374.   info->vars.vars[varindx].name = name;
  1375.   info->vars.vars[varindx].namlen = namlen;
  1376.  
  1377.   return TRUE;
  1378. }
  1379.  
  1380. /* Parse a TY record.  */
  1381.  
  1382. static bfd_boolean
  1383. parse_ieee_ty (struct ieee_info *info, const bfd_byte **pp)
  1384. {
  1385.   const bfd_byte *ty_start, *ty_var_start, *ty_code_start;
  1386.   bfd_vma typeindx, varindx, tc;
  1387.   void *dhandle;
  1388.   bfd_boolean tag, typdef;
  1389.   debug_type *arg_slots;
  1390.   unsigned long type_bitsize;
  1391.   debug_type type;
  1392.  
  1393.   ty_start = *pp;
  1394.  
  1395.   if (! ieee_read_number (info, pp, &typeindx))
  1396.     return FALSE;
  1397.  
  1398.   if (typeindx < 256)
  1399.     {
  1400.       ieee_error (info, ty_start, _("illegal type index"));
  1401.       return FALSE;
  1402.     }
  1403.  
  1404.   typeindx -= 256;
  1405.   if (! ieee_alloc_type (info, typeindx, FALSE))
  1406.     return FALSE;
  1407.  
  1408.   if (**pp != 0xce)
  1409.     {
  1410.       ieee_error (info, *pp, _("unknown TY code"));
  1411.       return FALSE;
  1412.     }
  1413.   ++*pp;
  1414.  
  1415.   ty_var_start = *pp;
  1416.  
  1417.   if (! ieee_read_number (info, pp, &varindx))
  1418.     return FALSE;
  1419.  
  1420.   if (varindx < 32)
  1421.     {
  1422.       ieee_error (info, ty_var_start, _("illegal variable index"));
  1423.       return FALSE;
  1424.     }
  1425.   varindx -= 32;
  1426.  
  1427.   if (varindx >= info->vars.alloc || info->vars.vars[varindx].name == NULL)
  1428.     {
  1429.       ieee_error (info, ty_var_start, _("undefined variable in TY"));
  1430.       return FALSE;
  1431.     }
  1432.  
  1433.   ty_code_start = *pp;
  1434.  
  1435.   if (! ieee_read_number (info, pp, &tc))
  1436.     return FALSE;
  1437.  
  1438.   dhandle = info->dhandle;
  1439.  
  1440.   tag = FALSE;
  1441.   typdef = FALSE;
  1442.   arg_slots = NULL;
  1443.   type_bitsize = 0;
  1444.   switch (tc)
  1445.     {
  1446.     default:
  1447.       ieee_error (info, ty_code_start, _("unknown TY code"));
  1448.       return FALSE;
  1449.  
  1450.     case '!':
  1451.       /* Unknown type, with size.  We treat it as int.  FIXME.  */
  1452.       {
  1453.         bfd_vma size;
  1454.  
  1455.         if (! ieee_read_number (info, pp, &size))
  1456.           return FALSE;
  1457.         type = debug_make_int_type (dhandle, size, FALSE);
  1458.       }
  1459.       break;
  1460.  
  1461.     case 'A': /* Array.  */
  1462.     case 'a': /* FORTRAN array in column/row order.  FIXME: Not
  1463.                  distinguished from normal array.  */
  1464.       {
  1465.         debug_type ele_type;
  1466.         bfd_vma lower, upper;
  1467.  
  1468.         if (! ieee_read_type_index (info, pp, &ele_type)
  1469.             || ! ieee_read_number (info, pp, &lower)
  1470.             || ! ieee_read_number (info, pp, &upper))
  1471.           return FALSE;
  1472.         type = debug_make_array_type (dhandle, ele_type,
  1473.                                       ieee_builtin_type (info, ty_code_start,
  1474.                                                          ((unsigned int)
  1475.                                                           builtin_int)),
  1476.                                       (bfd_signed_vma) lower,
  1477.                                       (bfd_signed_vma) upper,
  1478.                                       FALSE);
  1479.       }
  1480.       break;
  1481.  
  1482.     case 'E':
  1483.       /* Simple enumeration.  */
  1484.       {
  1485.         bfd_vma size;
  1486.         unsigned int alloc;
  1487.         const char **names;
  1488.         unsigned int c;
  1489.         bfd_signed_vma *vals;
  1490.         unsigned int i;
  1491.  
  1492.         if (! ieee_read_number (info, pp, &size))
  1493.           return FALSE;
  1494.         /* FIXME: we ignore the enumeration size.  */
  1495.  
  1496.         alloc = 10;
  1497.         names = (const char **) xmalloc (alloc * sizeof *names);
  1498.         memset (names, 0, alloc * sizeof *names);
  1499.         c = 0;
  1500.         while (1)
  1501.           {
  1502.             const char *name;
  1503.             unsigned long namlen;
  1504.             bfd_boolean present;
  1505.  
  1506.             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
  1507.               return FALSE;
  1508.             if (! present)
  1509.               break;
  1510.  
  1511.             if (c + 1 >= alloc)
  1512.               {
  1513.                 alloc += 10;
  1514.                 names = ((const char **)
  1515.                          xrealloc (names, alloc * sizeof *names));
  1516.               }
  1517.  
  1518.             names[c] = savestring (name, namlen);
  1519.             if (names[c] == NULL)
  1520.               return FALSE;
  1521.             ++c;
  1522.           }
  1523.  
  1524.         names[c] = NULL;
  1525.  
  1526.         vals = (bfd_signed_vma *) xmalloc (c * sizeof *vals);
  1527.         for (i = 0; i < c; i++)
  1528.           vals[i] = i;
  1529.  
  1530.         type = debug_make_enum_type (dhandle, names, vals);
  1531.         tag = TRUE;
  1532.       }
  1533.       break;
  1534.  
  1535.     case 'G':
  1536.       /* Struct with bit fields.  */
  1537.       {
  1538.         bfd_vma size;
  1539.         unsigned int alloc;
  1540.         debug_field *fields;
  1541.         unsigned int c;
  1542.  
  1543.         if (! ieee_read_number (info, pp, &size))
  1544.           return FALSE;
  1545.  
  1546.         alloc = 10;
  1547.         fields = (debug_field *) xmalloc (alloc * sizeof *fields);
  1548.         c = 0;
  1549.         while (1)
  1550.           {
  1551.             const char *name;
  1552.             unsigned long namlen;
  1553.             bfd_boolean present;
  1554.             debug_type ftype;
  1555.             bfd_vma bitpos, bitsize;
  1556.  
  1557.             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
  1558.               return FALSE;
  1559.             if (! present)
  1560.               break;
  1561.             if (! ieee_read_type_index (info, pp, &ftype)
  1562.                 || ! ieee_read_number (info, pp, &bitpos)
  1563.                 || ! ieee_read_number (info, pp, &bitsize))
  1564.               return FALSE;
  1565.  
  1566.             if (c + 1 >= alloc)
  1567.               {
  1568.                 alloc += 10;
  1569.                 fields = ((debug_field *)
  1570.                           xrealloc (fields, alloc * sizeof *fields));
  1571.               }
  1572.  
  1573.             fields[c] = debug_make_field (dhandle, savestring (name, namlen),
  1574.                                           ftype, bitpos, bitsize,
  1575.                                           DEBUG_VISIBILITY_PUBLIC);
  1576.             if (fields[c] == NULL)
  1577.               return FALSE;
  1578.             ++c;
  1579.           }
  1580.  
  1581.         fields[c] = NULL;
  1582.  
  1583.         type = debug_make_struct_type (dhandle, TRUE, size, fields);
  1584.         tag = TRUE;
  1585.       }
  1586.       break;
  1587.  
  1588.     case 'N':
  1589.       /* Enumeration.  */
  1590.       {
  1591.         unsigned int alloc;
  1592.         const char **names;
  1593.         bfd_signed_vma *vals;
  1594.         unsigned int c;
  1595.  
  1596.         alloc = 10;
  1597.         names = (const char **) xmalloc (alloc * sizeof *names);
  1598.         vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *names);
  1599.         c = 0;
  1600.         while (1)
  1601.           {
  1602.             const char *name;
  1603.             unsigned long namlen;
  1604.             bfd_boolean present;
  1605.             bfd_vma val;
  1606.  
  1607.             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
  1608.               return FALSE;
  1609.             if (! present)
  1610.               break;
  1611.             if (! ieee_read_number (info, pp, &val))
  1612.               return FALSE;
  1613.  
  1614.             /* If the length of the name is zero, then the value is
  1615.                actually the size of the enum.  We ignore this
  1616.                information.  FIXME.  */
  1617.             if (namlen == 0)
  1618.               continue;
  1619.  
  1620.             if (c + 1 >= alloc)
  1621.               {
  1622.                 alloc += 10;
  1623.                 names = ((const char **)
  1624.                          xrealloc (names, alloc * sizeof *names));
  1625.                 vals = ((bfd_signed_vma *)
  1626.                         xrealloc (vals, alloc * sizeof *vals));
  1627.               }
  1628.  
  1629.             names[c] = savestring (name, namlen);
  1630.             if (names[c] == NULL)
  1631.               return FALSE;
  1632.             vals[c] = (bfd_signed_vma) val;
  1633.             ++c;
  1634.           }
  1635.  
  1636.         names[c] = NULL;
  1637.  
  1638.         type = debug_make_enum_type (dhandle, names, vals);
  1639.         tag = TRUE;
  1640.       }
  1641.       break;
  1642.  
  1643.     case 'O': /* Small pointer.  We don't distinguish small and large
  1644.                  pointers.  FIXME.  */
  1645.     case 'P': /* Large pointer.  */
  1646.       {
  1647.         debug_type t;
  1648.  
  1649.         if (! ieee_read_type_index (info, pp, &t))
  1650.           return FALSE;
  1651.         type = debug_make_pointer_type (dhandle, t);
  1652.       }
  1653.       break;
  1654.  
  1655.     case 'R':
  1656.       /* Range.  */
  1657.       {
  1658.         bfd_vma low, high, signedp, size;
  1659.  
  1660.         if (! ieee_read_number (info, pp, &low)
  1661.             || ! ieee_read_number (info, pp, &high)
  1662.             || ! ieee_read_number (info, pp, &signedp)
  1663.             || ! ieee_read_number (info, pp, &size))
  1664.           return FALSE;
  1665.  
  1666.         type = debug_make_range_type (dhandle,
  1667.                                       debug_make_int_type (dhandle, size,
  1668.                                                            ! signedp),
  1669.                                       (bfd_signed_vma) low,
  1670.                                       (bfd_signed_vma) high);
  1671.       }
  1672.       break;
  1673.  
  1674.     case 'S': /* Struct.  */
  1675.     case 'U': /* Union.  */
  1676.       {
  1677.         bfd_vma size;
  1678.         unsigned int alloc;
  1679.         debug_field *fields;
  1680.         unsigned int c;
  1681.  
  1682.         if (! ieee_read_number (info, pp, &size))
  1683.           return FALSE;
  1684.  
  1685.         alloc = 10;
  1686.         fields = (debug_field *) xmalloc (alloc * sizeof *fields);
  1687.         c = 0;
  1688.         while (1)
  1689.           {
  1690.             const char *name;
  1691.             unsigned long namlen;
  1692.             bfd_boolean present;
  1693.             bfd_vma tindx;
  1694.             bfd_vma offset;
  1695.             debug_type ftype;
  1696.             bfd_vma bitsize;
  1697.  
  1698.             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
  1699.               return FALSE;
  1700.             if (! present)
  1701.               break;
  1702.             if (! ieee_read_number (info, pp, &tindx)
  1703.                 || ! ieee_read_number (info, pp, &offset))
  1704.               return FALSE;
  1705.  
  1706.             if (tindx < 256)
  1707.               {
  1708.                 ftype = ieee_builtin_type (info, ty_code_start, tindx);
  1709.                 bitsize = 0;
  1710.                 offset *= 8;
  1711.               }
  1712.             else
  1713.               {
  1714.                 struct ieee_type *t;
  1715.  
  1716.                 tindx -= 256;
  1717.                 if (! ieee_alloc_type (info, tindx, TRUE))
  1718.                   return FALSE;
  1719.                 t = info->types.types + tindx;
  1720.                 ftype = t->type;
  1721.                 bitsize = t->bitsize;
  1722.                 if (bitsize == 0)
  1723.                   offset *= 8;
  1724.               }
  1725.  
  1726.             if (c + 1 >= alloc)
  1727.               {
  1728.                 alloc += 10;
  1729.                 fields = ((debug_field *)
  1730.                           xrealloc (fields, alloc * sizeof *fields));
  1731.               }
  1732.  
  1733.             fields[c] = debug_make_field (dhandle, savestring (name, namlen),
  1734.                                           ftype, offset, bitsize,
  1735.                                           DEBUG_VISIBILITY_PUBLIC);
  1736.             if (fields[c] == NULL)
  1737.               return FALSE;
  1738.             ++c;
  1739.           }
  1740.  
  1741.         fields[c] = NULL;
  1742.  
  1743.         type = debug_make_struct_type (dhandle, tc == 'S', size, fields);
  1744.         tag = TRUE;
  1745.       }
  1746.       break;
  1747.  
  1748.     case 'T':
  1749.       /* Typedef.  */
  1750.       if (! ieee_read_type_index (info, pp, &type))
  1751.         return FALSE;
  1752.       typdef = TRUE;
  1753.       break;
  1754.  
  1755.     case 'X':
  1756.       /* Procedure.  FIXME: This is an extern declaration, which we
  1757.          have no way of representing.  */
  1758.       {
  1759.         bfd_vma attr;
  1760.         debug_type rtype;
  1761.         bfd_vma nargs;
  1762.         bfd_boolean present;
  1763.         struct ieee_var *pv;
  1764.  
  1765.         /* FIXME: We ignore the attribute and the argument names.  */
  1766.  
  1767.         if (! ieee_read_number (info, pp, &attr)
  1768.             || ! ieee_read_type_index (info, pp, &rtype)
  1769.             || ! ieee_read_number (info, pp, &nargs))
  1770.           return FALSE;
  1771.         do
  1772.           {
  1773.             const char *name;
  1774.             unsigned long namlen;
  1775.  
  1776.             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
  1777.               return FALSE;
  1778.           }
  1779.         while (present);
  1780.  
  1781.         pv = info->vars.vars + varindx;
  1782.         pv->kind = IEEE_EXTERNAL;
  1783.         if (pv->namlen > 0
  1784.             && debug_get_type_kind (dhandle, rtype) == DEBUG_KIND_POINTER)
  1785.           {
  1786.             /* Set up the return type as an indirect type pointing to
  1787.                the variable slot, so that we can change it to a
  1788.                reference later if appropriate.  */
  1789.             pv->pslot = (debug_type *) xmalloc (sizeof *pv->pslot);
  1790.             *pv->pslot = rtype;
  1791.             rtype = debug_make_indirect_type (dhandle, pv->pslot,
  1792.                                               (const char *) NULL);
  1793.           }
  1794.  
  1795.         type = debug_make_function_type (dhandle, rtype, (debug_type *) NULL,
  1796.                                          FALSE);
  1797.       }
  1798.       break;
  1799.  
  1800.     case 'V':
  1801.     case 'v':
  1802.       /* Void.  This is not documented, but the MRI compiler emits it.  */
  1803.       type = debug_make_void_type (dhandle);
  1804.       break;
  1805.  
  1806.     case 'Z':
  1807.       /* Array with 0 lower bound.  */
  1808.       {
  1809.         debug_type etype;
  1810.         bfd_vma high;
  1811.  
  1812.         if (! ieee_read_type_index (info, pp, &etype)
  1813.             || ! ieee_read_number (info, pp, &high))
  1814.           return FALSE;
  1815.  
  1816.         type = debug_make_array_type (dhandle, etype,
  1817.                                       ieee_builtin_type (info, ty_code_start,
  1818.                                                          ((unsigned int)
  1819.                                                           builtin_int)),
  1820.                                       0, (bfd_signed_vma) high, FALSE);
  1821.       }
  1822.       break;
  1823.  
  1824.     case 'c': /* Complex.  */
  1825.     case 'd': /* Double complex.  */
  1826.       {
  1827.         const char *name;
  1828.         unsigned long namlen;
  1829.  
  1830.         /* FIXME: I don't know what the name means.  */
  1831.  
  1832.         if (! ieee_read_id (info, pp, &name, &namlen))
  1833.           return FALSE;
  1834.  
  1835.         type = debug_make_complex_type (dhandle, tc == 'c' ? 4 : 8);
  1836.       }
  1837.       break;
  1838.  
  1839.     case 'f':
  1840.       /* Pascal file name.  FIXME.  */
  1841.       ieee_error (info, ty_code_start, _("Pascal file name not supported"));
  1842.       return FALSE;
  1843.  
  1844.     case 'g':
  1845.       /* Bitfield type.  */
  1846.       {
  1847.         bfd_vma signedp, bitsize, dummy;
  1848.         const bfd_byte *hold;
  1849.         bfd_boolean present;
  1850.  
  1851.         if (! ieee_read_number (info, pp, &signedp)
  1852.             || ! ieee_read_number (info, pp, &bitsize))
  1853.           return FALSE;
  1854.  
  1855.         /* I think the documentation says that there is a type index,
  1856.            but some actual files do not have one.  */
  1857.         hold = *pp;
  1858.         if (! ieee_read_optional_number (info, pp, &dummy, &present))
  1859.           return FALSE;
  1860.         if (! present)
  1861.           {
  1862.             /* FIXME: This is just a guess.  */
  1863.             type = debug_make_int_type (dhandle, 4,
  1864.                                         signedp ? FALSE : TRUE);
  1865.           }
  1866.         else
  1867.           {
  1868.             *pp = hold;
  1869.             if (! ieee_read_type_index (info, pp, &type))
  1870.               return FALSE;
  1871.           }
  1872.         type_bitsize = bitsize;
  1873.       }
  1874.       break;
  1875.  
  1876.     case 'n':
  1877.       /* Qualifier.  */
  1878.       {
  1879.         bfd_vma kind;
  1880.         debug_type t;
  1881.  
  1882.         if (! ieee_read_number (info, pp, &kind)
  1883.             || ! ieee_read_type_index (info, pp, &t))
  1884.           return FALSE;
  1885.  
  1886.         switch (kind)
  1887.           {
  1888.           default:
  1889.             ieee_error (info, ty_start, _("unsupported qualifier"));
  1890.             return FALSE;
  1891.  
  1892.           case 1:
  1893.             type = debug_make_const_type (dhandle, t);
  1894.             break;
  1895.  
  1896.           case 2:
  1897.             type = debug_make_volatile_type (dhandle, t);
  1898.             break;
  1899.           }
  1900.       }
  1901.       break;
  1902.  
  1903.     case 's':
  1904.       /* Set.  */
  1905.       {
  1906.         bfd_vma size;
  1907.         debug_type etype;
  1908.  
  1909.         if (! ieee_read_number (info, pp, &size)
  1910.             || ! ieee_read_type_index (info, pp, &etype))
  1911.           return FALSE;
  1912.  
  1913.         /* FIXME: We ignore the size.  */
  1914.  
  1915.         type = debug_make_set_type (dhandle, etype, FALSE);
  1916.       }
  1917.       break;
  1918.  
  1919.     case 'x':
  1920.       /* Procedure with compiler dependencies.  */
  1921.       {
  1922.         struct ieee_var *pv;
  1923.         bfd_vma attr, frame_type, push_mask, nargs, level, father;
  1924.         debug_type rtype;
  1925.         debug_type *arg_types;
  1926.         bfd_boolean varargs;
  1927.         bfd_boolean present;
  1928.  
  1929.         /* FIXME: We ignore some of this information.  */
  1930.  
  1931.         pv = info->vars.vars + varindx;
  1932.  
  1933.         if (! ieee_read_number (info, pp, &attr)
  1934.             || ! ieee_read_number (info, pp, &frame_type)
  1935.             || ! ieee_read_number (info, pp, &push_mask)
  1936.             || ! ieee_read_type_index (info, pp, &rtype)
  1937.             || ! ieee_read_number (info, pp, &nargs))
  1938.           return FALSE;
  1939.         if (nargs == (bfd_vma) -1)
  1940.           {
  1941.             arg_types = NULL;
  1942.             varargs = FALSE;
  1943.           }
  1944.         else
  1945.           {
  1946.             unsigned int i;
  1947.  
  1948.             arg_types = ((debug_type *)
  1949.                          xmalloc ((nargs + 1) * sizeof *arg_types));
  1950.             for (i = 0; i < nargs; i++)
  1951.               if (! ieee_read_type_index (info, pp, arg_types + i))
  1952.                 return FALSE;
  1953.  
  1954.             /* If the last type is pointer to void, this is really a
  1955.                varargs function.  */
  1956.             varargs = FALSE;
  1957.             if (nargs > 0)
  1958.               {
  1959.                 debug_type last;
  1960.  
  1961.                 last = arg_types[nargs - 1];
  1962.                 if (debug_get_type_kind (dhandle, last) == DEBUG_KIND_POINTER
  1963.                     && (debug_get_type_kind (dhandle,
  1964.                                              debug_get_target_type (dhandle,
  1965.                                                                     last))
  1966.                         == DEBUG_KIND_VOID))
  1967.                   {
  1968.                     --nargs;
  1969.                     varargs = TRUE;
  1970.                   }
  1971.               }
  1972.  
  1973.             /* If there are any pointer arguments, turn them into
  1974.                indirect types in case we later need to convert them to
  1975.                reference types.  */
  1976.             for (i = 0; i < nargs; i++)
  1977.               {
  1978.                 if (debug_get_type_kind (dhandle, arg_types[i])
  1979.                     == DEBUG_KIND_POINTER)
  1980.                   {
  1981.                     if (arg_slots == NULL)
  1982.                       {
  1983.                         arg_slots = ((debug_type *)
  1984.                                      xmalloc (nargs * sizeof *arg_slots));
  1985.                         memset (arg_slots, 0, nargs * sizeof *arg_slots);
  1986.                       }
  1987.                     arg_slots[i] = arg_types[i];
  1988.                     arg_types[i] =
  1989.                       debug_make_indirect_type (dhandle,
  1990.                                                 arg_slots + i,
  1991.                                                 (const char *) NULL);
  1992.                   }
  1993.               }
  1994.  
  1995.             arg_types[nargs] = DEBUG_TYPE_NULL;
  1996.           }
  1997.         if (! ieee_read_number (info, pp, &level)
  1998.             || ! ieee_read_optional_number (info, pp, &father, &present))
  1999.           return FALSE;
  2000.  
  2001.         /* We can't distinguish between a global function and a static
  2002.            function.  */
  2003.         pv->kind = IEEE_FUNCTION;
  2004.  
  2005.         if (pv->namlen > 0
  2006.             && debug_get_type_kind (dhandle, rtype) == DEBUG_KIND_POINTER)
  2007.           {
  2008.             /* Set up the return type as an indirect type pointing to
  2009.                the variable slot, so that we can change it to a
  2010.                reference later if appropriate.  */
  2011.             pv->pslot = (debug_type *) xmalloc (sizeof *pv->pslot);
  2012.             *pv->pslot = rtype;
  2013.             rtype = debug_make_indirect_type (dhandle, pv->pslot,
  2014.                                               (const char *) NULL);
  2015.           }
  2016.  
  2017.         type = debug_make_function_type (dhandle, rtype, arg_types, varargs);
  2018.       }
  2019.       break;
  2020.     }
  2021.  
  2022.   /* Record the type in the table.  */
  2023.  
  2024.   if (type == DEBUG_TYPE_NULL)
  2025.     return FALSE;
  2026.  
  2027.   info->vars.vars[varindx].type = type;
  2028.  
  2029.   if ((tag || typdef)
  2030.       && info->vars.vars[varindx].namlen > 0)
  2031.     {
  2032.       const char *name;
  2033.  
  2034.       name = savestring (info->vars.vars[varindx].name,
  2035.                          info->vars.vars[varindx].namlen);
  2036.       if (typdef)
  2037.         type = debug_name_type (dhandle, name, type);
  2038.       else if (tc == 'E' || tc == 'N')
  2039.         type = debug_tag_type (dhandle, name, type);
  2040.       else
  2041.         {
  2042.           struct ieee_tag *it;
  2043.  
  2044.           /* We must allocate all struct tags as indirect types, so
  2045.              that if we later see a definition of the tag as a C++
  2046.              record we can update the indirect slot and automatically
  2047.              change all the existing references.  */
  2048.           it = (struct ieee_tag *) xmalloc (sizeof *it);
  2049.           memset (it, 0, sizeof *it);
  2050.           it->next = info->tags;
  2051.           info->tags = it;
  2052.           it->name = name;
  2053.           it->slot = type;
  2054.  
  2055.           type = debug_make_indirect_type (dhandle, &it->slot, name);
  2056.           type = debug_tag_type (dhandle, name, type);
  2057.  
  2058.           it->type = type;
  2059.         }
  2060.       if (type == NULL)
  2061.         return FALSE;
  2062.     }
  2063.  
  2064.   info->types.types[typeindx].type = type;
  2065.   info->types.types[typeindx].arg_slots = arg_slots;
  2066.   info->types.types[typeindx].bitsize = type_bitsize;
  2067.  
  2068.   /* We may have already allocated type as an indirect type pointing
  2069.      to slot.  It does no harm to replace the indirect type with the
  2070.      real type.  Filling in slot as well handles the indirect types
  2071.      which are already hanging around.  */
  2072.   if (info->types.types[typeindx].pslot != NULL)
  2073.     *info->types.types[typeindx].pslot = type;
  2074.  
  2075.   return TRUE;
  2076. }
  2077.  
  2078. /* Parse an ATN record.  */
  2079.  
  2080. static bfd_boolean
  2081. parse_ieee_atn (struct ieee_info *info, const bfd_byte **pp)
  2082. {
  2083.   const bfd_byte *atn_start, *atn_code_start;
  2084.   bfd_vma varindx;
  2085.   struct ieee_var *pvar;
  2086.   debug_type type;
  2087.   bfd_vma atn_code;
  2088.   void *dhandle;
  2089.   bfd_vma v, v2, v3, v4, v5;
  2090.   const char *name;
  2091.   unsigned long namlen;
  2092.   char *namcopy;
  2093.   bfd_boolean present;
  2094.   int blocktype;
  2095.  
  2096.   atn_start = *pp;
  2097.  
  2098.   if (! ieee_read_number (info, pp, &varindx)
  2099.       || ! ieee_read_type_index (info, pp, &type))
  2100.     return FALSE;
  2101.  
  2102.   atn_code_start = *pp;
  2103.  
  2104.   if (! ieee_read_number (info, pp, &atn_code))
  2105.     return FALSE;
  2106.  
  2107.   if (varindx == 0)
  2108.     {
  2109.       pvar = NULL;
  2110.       name = "";
  2111.       namlen = 0;
  2112.     }
  2113.   else if (varindx < 32)
  2114.     {
  2115.       /* The MRI compiler reportedly sometimes emits variable lifetime
  2116.          information for a register.  We just ignore it.  */
  2117.       if (atn_code == 9)
  2118.         return ieee_read_number (info, pp, &v);
  2119.  
  2120.       ieee_error (info, atn_start, _("illegal variable index"));
  2121.       return FALSE;
  2122.     }
  2123.   else
  2124.     {
  2125.       varindx -= 32;
  2126.       if (varindx >= info->vars.alloc
  2127.           || info->vars.vars[varindx].name == NULL)
  2128.         {
  2129.           /* The MRI compiler or linker sometimes omits the NN record
  2130.              for a pmisc record.  */
  2131.           if (atn_code == 62)
  2132.             {
  2133.               if (varindx >= info->vars.alloc)
  2134.                 {
  2135.                   unsigned int alloc;
  2136.  
  2137.                   alloc = info->vars.alloc;
  2138.                   if (alloc == 0)
  2139.                     alloc = 4;
  2140.                   while (varindx >= alloc)
  2141.                     alloc *= 2;
  2142.                   info->vars.vars = ((struct ieee_var *)
  2143.                                      xrealloc (info->vars.vars,
  2144.                                                (alloc
  2145.                                                 * sizeof *info->vars.vars)));
  2146.                   memset (info->vars.vars + info->vars.alloc, 0,
  2147.                           ((alloc - info->vars.alloc)
  2148.                            * sizeof *info->vars.vars));
  2149.                   info->vars.alloc = alloc;
  2150.                 }
  2151.  
  2152.               pvar = info->vars.vars + varindx;
  2153.               pvar->name = "";
  2154.               pvar->namlen = 0;
  2155.             }
  2156.           else
  2157.             {
  2158.               ieee_error (info, atn_start, _("undefined variable in ATN"));
  2159.               return FALSE;
  2160.             }
  2161.         }
  2162.  
  2163.       pvar = info->vars.vars + varindx;
  2164.  
  2165.       pvar->type = type;
  2166.  
  2167.       name = pvar->name;
  2168.       namlen = pvar->namlen;
  2169.     }
  2170.  
  2171.   dhandle = info->dhandle;
  2172.  
  2173.   /* If we are going to call debug_record_variable with a pointer
  2174.      type, change the type to an indirect type so that we can later
  2175.      change it to a reference type if we encounter a C++ pmisc 'R'
  2176.      record.  */
  2177.   if (pvar != NULL
  2178.       && type != DEBUG_TYPE_NULL
  2179.       && debug_get_type_kind (dhandle, type) == DEBUG_KIND_POINTER)
  2180.     {
  2181.       switch (atn_code)
  2182.         {
  2183.         case 1:
  2184.         case 2:
  2185.         case 3:
  2186.         case 5:
  2187.         case 8:
  2188.         case 10:
  2189.           pvar->pslot = (debug_type *) xmalloc (sizeof *pvar->pslot);
  2190.           *pvar->pslot = type;
  2191.           type = debug_make_indirect_type (dhandle, pvar->pslot,
  2192.                                            (const char *) NULL);
  2193.           pvar->type = type;
  2194.           break;
  2195.         }
  2196.     }
  2197.  
  2198.   switch (atn_code)
  2199.     {
  2200.     default:
  2201.       ieee_error (info, atn_code_start, _("unknown ATN type"));
  2202.       return FALSE;
  2203.  
  2204.     case 1:
  2205.       /* Automatic variable.  */
  2206.       if (! ieee_read_number (info, pp, &v))
  2207.         return FALSE;
  2208.       namcopy = savestring (name, namlen);
  2209.       if (type == NULL)
  2210.         type = debug_make_void_type (dhandle);
  2211.       if (pvar != NULL)
  2212.         pvar->kind = IEEE_LOCAL;
  2213.       return debug_record_variable (dhandle, namcopy, type, DEBUG_LOCAL, v);
  2214.  
  2215.     case 2:
  2216.       /* Register variable.  */
  2217.       if (! ieee_read_number (info, pp, &v))
  2218.         return FALSE;
  2219.       namcopy = savestring (name, namlen);
  2220.       if (type == NULL)
  2221.         type = debug_make_void_type (dhandle);
  2222.       if (pvar != NULL)
  2223.         pvar->kind = IEEE_LOCAL;
  2224.       return debug_record_variable (dhandle, namcopy, type, DEBUG_REGISTER,
  2225.                                     ieee_regno_to_genreg (info->abfd, v));
  2226.  
  2227.     case 3:
  2228.       /* Static variable.  */
  2229.       if (! ieee_require_asn (info, pp, &v))
  2230.         return FALSE;
  2231.       namcopy = savestring (name, namlen);
  2232.       if (type == NULL)
  2233.         type = debug_make_void_type (dhandle);
  2234.       if (info->blockstack.bsp <= info->blockstack.stack)
  2235.         blocktype = 0;
  2236.       else
  2237.         blocktype = info->blockstack.bsp[-1].kind;
  2238.       if (pvar != NULL)
  2239.         {
  2240.           if (blocktype == 4 || blocktype == 6)
  2241.             pvar->kind = IEEE_LOCAL;
  2242.           else
  2243.             pvar->kind = IEEE_STATIC;
  2244.         }
  2245.       return debug_record_variable (dhandle, namcopy, type,
  2246.                                     (blocktype == 4 || blocktype == 6
  2247.                                      ? DEBUG_LOCAL_STATIC
  2248.                                      : DEBUG_STATIC),
  2249.                                     v);
  2250.  
  2251.     case 4:
  2252.       /* External function.  We don't currently record these.  FIXME.  */
  2253.       if (pvar != NULL)
  2254.         pvar->kind = IEEE_EXTERNAL;
  2255.       return TRUE;
  2256.  
  2257.     case 5:
  2258.       /* External variable.  We don't currently record these.  FIXME.  */
  2259.       if (pvar != NULL)
  2260.         pvar->kind = IEEE_EXTERNAL;
  2261.       return TRUE;
  2262.  
  2263.     case 7:
  2264.       if (! ieee_read_number (info, pp, &v)
  2265.           || ! ieee_read_number (info, pp, &v2)
  2266.           || ! ieee_read_optional_number (info, pp, &v3, &present))
  2267.         return FALSE;
  2268.       if (present)
  2269.         {
  2270.           if (! ieee_read_optional_number (info, pp, &v4, &present))
  2271.             return FALSE;
  2272.         }
  2273.  
  2274.       /* We just ignore the two optional fields in v3 and v4, since
  2275.          they are not defined.  */
  2276.  
  2277.       if (! ieee_require_asn (info, pp, &v3))
  2278.         return FALSE;
  2279.  
  2280.       /* We have no way to record the column number.  FIXME.  */
  2281.  
  2282.       return debug_record_line (dhandle, v, v3);
  2283.  
  2284.     case 8:
  2285.       /* Global variable.  */
  2286.       if (! ieee_require_asn (info, pp, &v))
  2287.         return FALSE;
  2288.       namcopy = savestring (name, namlen);
  2289.       if (type == NULL)
  2290.         type = debug_make_void_type (dhandle);
  2291.       if (pvar != NULL)
  2292.         pvar->kind = IEEE_GLOBAL;
  2293.       return debug_record_variable (dhandle, namcopy, type, DEBUG_GLOBAL, v);
  2294.  
  2295.     case 9:
  2296.       /* Variable lifetime information.  */
  2297.       if (! ieee_read_number (info, pp, &v))
  2298.         return FALSE;
  2299.  
  2300.       /* We have no way to record this information.  FIXME.  */
  2301.       return TRUE;
  2302.  
  2303.     case 10:
  2304.       /* Locked register.  The spec says that there are two required
  2305.          fields, but at least on occasion the MRI compiler only emits
  2306.          one.  */
  2307.       if (! ieee_read_number (info, pp, &v)
  2308.           || ! ieee_read_optional_number (info, pp, &v2, &present))
  2309.         return FALSE;
  2310.  
  2311.       /* I think this means a variable that is both in a register and
  2312.          a frame slot.  We ignore the frame slot.  FIXME.  */
  2313.  
  2314.       namcopy = savestring (name, namlen);
  2315.       if (type == NULL)
  2316.         type = debug_make_void_type (dhandle);
  2317.       if (pvar != NULL)
  2318.         pvar->kind = IEEE_LOCAL;
  2319.       return debug_record_variable (dhandle, namcopy, type, DEBUG_REGISTER, v);
  2320.  
  2321.     case 11:
  2322.       /* Reserved for FORTRAN common.  */
  2323.       ieee_error (info, atn_code_start, _("unsupported ATN11"));
  2324.  
  2325.       /* Return TRUE to keep going.  */
  2326.       return TRUE;
  2327.  
  2328.     case 12:
  2329.       /* Based variable.  */
  2330.       v3 = 0;
  2331.       v4 = 0x80;
  2332.       v5 = 0;
  2333.       if (! ieee_read_number (info, pp, &v)
  2334.           || ! ieee_read_number (info, pp, &v2)
  2335.           || ! ieee_read_optional_number (info, pp, &v3, &present))
  2336.         return FALSE;
  2337.       if (present)
  2338.         {
  2339.           if (! ieee_read_optional_number (info, pp, &v4, &present))
  2340.             return FALSE;
  2341.           if (present)
  2342.             {
  2343.               if (! ieee_read_optional_number (info, pp, &v5, &present))
  2344.                 return FALSE;
  2345.             }
  2346.         }
  2347.  
  2348.       /* We have no way to record this information.  FIXME.  */
  2349.  
  2350.       ieee_error (info, atn_code_start, _("unsupported ATN12"));
  2351.  
  2352.       /* Return TRUE to keep going.  */
  2353.       return TRUE;
  2354.  
  2355.     case 16:
  2356.       /* Constant.  The description of this that I have is ambiguous,
  2357.          so I'm not going to try to implement it.  */
  2358.       if (! ieee_read_number (info, pp, &v)
  2359.           || ! ieee_read_optional_number (info, pp, &v2, &present))
  2360.         return FALSE;
  2361.       if (present)
  2362.         {
  2363.           if (! ieee_read_optional_number (info, pp, &v2, &present))
  2364.             return FALSE;
  2365.           if (present)
  2366.             {
  2367.               if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
  2368.                 return FALSE;
  2369.             }
  2370.         }
  2371.  
  2372.       if ((ieee_record_enum_type) **pp == ieee_e2_first_byte_enum)
  2373.         {
  2374.           if (! ieee_require_asn (info, pp, &v3))
  2375.             return FALSE;
  2376.         }
  2377.  
  2378.       return TRUE;
  2379.  
  2380.     case 19:
  2381.       /* Static variable from assembler.  */
  2382.       v2 = 0;
  2383.       if (! ieee_read_number (info, pp, &v)
  2384.           || ! ieee_read_optional_number (info, pp, &v2, &present)
  2385.           || ! ieee_require_asn (info, pp, &v3))
  2386.         return FALSE;
  2387.       namcopy = savestring (name, namlen);
  2388.       /* We don't really handle this correctly.  FIXME.  */
  2389.       return debug_record_variable (dhandle, namcopy,
  2390.                                     debug_make_void_type (dhandle),
  2391.                                     v2 != 0 ? DEBUG_GLOBAL : DEBUG_STATIC,
  2392.                                     v3);
  2393.  
  2394.     case 62:
  2395.       /* Procedure miscellaneous information.  */
  2396.     case 63:
  2397.       /* Variable miscellaneous information.  */
  2398.     case 64:
  2399.       /* Module miscellaneous information.  */
  2400.       if (! ieee_read_number (info, pp, &v)
  2401.           || ! ieee_read_number (info, pp, &v2)
  2402.           || ! ieee_read_optional_id (info, pp, &name, &namlen, &present))
  2403.         return FALSE;
  2404.  
  2405.       if (atn_code == 62 && v == 80)
  2406.         {
  2407.           if (present)
  2408.             {
  2409.               ieee_error (info, atn_code_start,
  2410.                           _("unexpected string in C++ misc"));
  2411.               return FALSE;
  2412.             }
  2413.           return ieee_read_cxx_misc (info, pp, v2);
  2414.         }
  2415.  
  2416.       /* We just ignore all of this stuff.  FIXME.  */
  2417.  
  2418.       for (; v2 > 0; --v2)
  2419.         {
  2420.           switch ((ieee_record_enum_type) **pp)
  2421.             {
  2422.             default:
  2423.               ieee_error (info, *pp, _("bad misc record"));
  2424.               return FALSE;
  2425.  
  2426.             case ieee_at_record_enum:
  2427.               if (! ieee_require_atn65 (info, pp, &name, &namlen))
  2428.                 return FALSE;
  2429.               break;
  2430.  
  2431.             case ieee_e2_first_byte_enum:
  2432.               if (! ieee_require_asn (info, pp, &v3))
  2433.                 return FALSE;
  2434.               break;
  2435.             }
  2436.         }
  2437.  
  2438.       return TRUE;
  2439.     }
  2440.  
  2441.   /*NOTREACHED*/
  2442. }
  2443.  
  2444. /* Handle C++ debugging miscellaneous records.  This is called for
  2445.    procedure miscellaneous records of type 80.  */
  2446.  
  2447. static bfd_boolean
  2448. ieee_read_cxx_misc (struct ieee_info *info, const bfd_byte **pp,
  2449.                     unsigned long count)
  2450. {
  2451.   const bfd_byte *start;
  2452.   bfd_vma category;
  2453.  
  2454.   start = *pp;
  2455.  
  2456.   /* Get the category of C++ misc record.  */
  2457.   if (! ieee_require_asn (info, pp, &category))
  2458.     return FALSE;
  2459.   --count;
  2460.  
  2461.   switch (category)
  2462.     {
  2463.     default:
  2464.       ieee_error (info, start, _("unrecognized C++ misc record"));
  2465.       return FALSE;
  2466.  
  2467.     case 'T':
  2468.       if (! ieee_read_cxx_class (info, pp, count))
  2469.         return FALSE;
  2470.       break;
  2471.  
  2472.     case 'M':
  2473.       {
  2474.         bfd_vma flags;
  2475.         const char *name;
  2476.         unsigned long namlen;
  2477.  
  2478.         /* The IEEE spec indicates that the 'M' record only has a
  2479.            flags field.  The MRI compiler also emits the name of the
  2480.            function.  */
  2481.  
  2482.         if (! ieee_require_asn (info, pp, &flags))
  2483.           return FALSE;
  2484.         if (*pp < info->pend
  2485.             && (ieee_record_enum_type) **pp == ieee_at_record_enum)
  2486.           {
  2487.             if (! ieee_require_atn65 (info, pp, &name, &namlen))
  2488.               return FALSE;
  2489.           }
  2490.  
  2491.         /* This is emitted for method functions, but I don't think we
  2492.            care very much.  It might help if it told us useful
  2493.            information like the class with which this function is
  2494.            associated, but it doesn't, so it isn't helpful.  */
  2495.       }
  2496.       break;
  2497.  
  2498.     case 'B':
  2499.       if (! ieee_read_cxx_defaults (info, pp, count))
  2500.         return FALSE;
  2501.       break;
  2502.  
  2503.     case 'z':
  2504.       {
  2505.         const char *name, *mangled, *cxx_class;
  2506.         unsigned long namlen, mangledlen, classlen;
  2507.         bfd_vma control;
  2508.  
  2509.         /* Pointer to member.  */
  2510.  
  2511.         if (! ieee_require_atn65 (info, pp, &name, &namlen)
  2512.             || ! ieee_require_atn65 (info, pp, &mangled, &mangledlen)
  2513.             || ! ieee_require_atn65 (info, pp, &cxx_class, &classlen)
  2514.             || ! ieee_require_asn (info, pp, &control))
  2515.           return FALSE;
  2516.  
  2517.         /* FIXME: We should now track down name and change its type.  */
  2518.       }
  2519.       break;
  2520.  
  2521.     case 'R':
  2522.       if (! ieee_read_reference (info, pp))
  2523.         return FALSE;
  2524.       break;
  2525.     }
  2526.  
  2527.   return TRUE;
  2528. }
  2529.  
  2530. /* Read a C++ class definition.  This is a pmisc type 80 record of
  2531.    category 'T'.  */
  2532.  
  2533. static bfd_boolean
  2534. ieee_read_cxx_class (struct ieee_info *info, const bfd_byte **pp,
  2535.                      unsigned long count)
  2536. {
  2537.   const bfd_byte *start;
  2538.   bfd_vma cxx_class;
  2539.   const char *tag;
  2540.   unsigned long taglen;
  2541.   struct ieee_tag *it;
  2542.   void *dhandle;
  2543.   debug_field *fields;
  2544.   unsigned int field_count, field_alloc;
  2545.   debug_baseclass *baseclasses;
  2546.   unsigned int baseclasses_count, baseclasses_alloc;
  2547.   const debug_field *structfields;
  2548.   struct ieee_method
  2549.     {
  2550.       const char *name;
  2551.       unsigned long namlen;
  2552.       debug_method_variant *variants;
  2553.       unsigned count;
  2554.       unsigned int alloc;
  2555.     } *methods;
  2556.   unsigned int methods_count, methods_alloc;
  2557.   debug_type vptrbase;
  2558.   bfd_boolean ownvptr;
  2559.   debug_method *dmethods;
  2560.  
  2561.   start = *pp;
  2562.  
  2563.   if (! ieee_require_asn (info, pp, &cxx_class))
  2564.     return FALSE;
  2565.   --count;
  2566.  
  2567.   if (! ieee_require_atn65 (info, pp, &tag, &taglen))
  2568.     return FALSE;
  2569.   --count;
  2570.  
  2571.   /* Find the C struct with this name.  */
  2572.   for (it = info->tags; it != NULL; it = it->next)
  2573.     if (it->name[0] == tag[0]
  2574.         && strncmp (it->name, tag, taglen) == 0
  2575.         && strlen (it->name) == taglen)
  2576.       break;
  2577.   if (it == NULL)
  2578.     {
  2579.       ieee_error (info, start, _("undefined C++ object"));
  2580.       return FALSE;
  2581.     }
  2582.  
  2583.   dhandle = info->dhandle;
  2584.  
  2585.   fields = NULL;
  2586.   field_count = 0;
  2587.   field_alloc = 0;
  2588.   baseclasses = NULL;
  2589.   baseclasses_count = 0;
  2590.   baseclasses_alloc = 0;
  2591.   methods = NULL;
  2592.   methods_count = 0;
  2593.   methods_alloc = 0;
  2594.   vptrbase = DEBUG_TYPE_NULL;
  2595.   ownvptr = FALSE;
  2596.  
  2597.   structfields = debug_get_fields (dhandle, it->type);
  2598.  
  2599.   while (count > 0)
  2600.     {
  2601.       bfd_vma id;
  2602.       const bfd_byte *spec_start;
  2603.  
  2604.       spec_start = *pp;
  2605.  
  2606.       if (! ieee_require_asn (info, pp, &id))
  2607.         return FALSE;
  2608.       --count;
  2609.  
  2610.       switch (id)
  2611.         {
  2612.         default:
  2613.           ieee_error (info, spec_start, _("unrecognized C++ object spec"));
  2614.           return FALSE;
  2615.  
  2616.         case 'b':
  2617.           {
  2618.             bfd_vma flags, cinline;
  2619.             const char *base, *fieldname;
  2620.             unsigned long baselen, fieldlen;
  2621.             char *basecopy;
  2622.             debug_type basetype;
  2623.             bfd_vma bitpos;
  2624.             bfd_boolean virtualp;
  2625.             enum debug_visibility visibility;
  2626.             debug_baseclass baseclass;
  2627.  
  2628.             /* This represents a base or friend class.  */
  2629.  
  2630.             if (! ieee_require_asn (info, pp, &flags)
  2631.                 || ! ieee_require_atn65 (info, pp, &base, &baselen)
  2632.                 || ! ieee_require_asn (info, pp, &cinline)
  2633.                 || ! ieee_require_atn65 (info, pp, &fieldname, &fieldlen))
  2634.               return FALSE;
  2635.             count -= 4;
  2636.  
  2637.             /* We have no way of recording friend information, so we
  2638.                just ignore it.  */
  2639.             if ((flags & BASEFLAGS_FRIEND) != 0)
  2640.               break;
  2641.  
  2642.             /* I assume that either all of the members of the
  2643.                baseclass are included in the object, starting at the
  2644.                beginning of the object, or that none of them are
  2645.                included.  */
  2646.  
  2647.             if ((fieldlen == 0) == (cinline == 0))
  2648.               {
  2649.                 ieee_error (info, start, _("unsupported C++ object type"));
  2650.                 return FALSE;
  2651.               }
  2652.  
  2653.             basecopy = savestring (base, baselen);
  2654.             basetype = debug_find_tagged_type (dhandle, basecopy,
  2655.                                                DEBUG_KIND_ILLEGAL);
  2656.             free (basecopy);
  2657.             if (basetype == DEBUG_TYPE_NULL)
  2658.               {
  2659.                 ieee_error (info, start, _("C++ base class not defined"));
  2660.                 return FALSE;
  2661.               }
  2662.  
  2663.             if (fieldlen == 0)
  2664.               bitpos = 0;
  2665.             else
  2666.               {
  2667.                 const debug_field *pf;
  2668.  
  2669.                 if (structfields == NULL)
  2670.                   {
  2671.                     ieee_error (info, start, _("C++ object has no fields"));
  2672.                     return FALSE;
  2673.                   }
  2674.  
  2675.                 for (pf = structfields; *pf != DEBUG_FIELD_NULL; pf++)
  2676.                   {
  2677.                     const char *fname;
  2678.  
  2679.                     fname = debug_get_field_name (dhandle, *pf);
  2680.                     if (fname == NULL)
  2681.                       return FALSE;
  2682.                     if (fname[0] == fieldname[0]
  2683.                         && strncmp (fname, fieldname, fieldlen) == 0
  2684.                         && strlen (fname) == fieldlen)
  2685.                       break;
  2686.                   }
  2687.                 if (*pf == DEBUG_FIELD_NULL)
  2688.                   {
  2689.                     ieee_error (info, start,
  2690.                                 _("C++ base class not found in container"));
  2691.                     return FALSE;
  2692.                   }
  2693.  
  2694.                 bitpos = debug_get_field_bitpos (dhandle, *pf);
  2695.               }
  2696.  
  2697.             if ((flags & BASEFLAGS_VIRTUAL) != 0)
  2698.               virtualp = TRUE;
  2699.             else
  2700.               virtualp = FALSE;
  2701.             if ((flags & BASEFLAGS_PRIVATE) != 0)
  2702.               visibility = DEBUG_VISIBILITY_PRIVATE;
  2703.             else
  2704.               visibility = DEBUG_VISIBILITY_PUBLIC;
  2705.  
  2706.             baseclass = debug_make_baseclass (dhandle, basetype, bitpos,
  2707.                                               virtualp, visibility);
  2708.             if (baseclass == DEBUG_BASECLASS_NULL)
  2709.               return FALSE;
  2710.  
  2711.             if (baseclasses_count + 1 >= baseclasses_alloc)
  2712.               {
  2713.                 baseclasses_alloc += 10;
  2714.                 baseclasses = ((debug_baseclass *)
  2715.                                xrealloc (baseclasses,
  2716.                                          (baseclasses_alloc
  2717.                                           * sizeof *baseclasses)));
  2718.               }
  2719.  
  2720.             baseclasses[baseclasses_count] = baseclass;
  2721.             ++baseclasses_count;
  2722.             baseclasses[baseclasses_count] = DEBUG_BASECLASS_NULL;
  2723.           }
  2724.           break;
  2725.  
  2726.         case 'd':
  2727.           {
  2728.             bfd_vma flags;
  2729.             const char *fieldname, *mangledname;
  2730.             unsigned long fieldlen, mangledlen;
  2731.             char *fieldcopy;
  2732.             bfd_boolean staticp;
  2733.             debug_type ftype;
  2734.             const debug_field *pf = NULL;
  2735.             enum debug_visibility visibility;
  2736.             debug_field field;
  2737.  
  2738.             /* This represents a data member.  */
  2739.  
  2740.             if (! ieee_require_asn (info, pp, &flags)
  2741.                 || ! ieee_require_atn65 (info, pp, &fieldname, &fieldlen)
  2742.                 || ! ieee_require_atn65 (info, pp, &mangledname, &mangledlen))
  2743.               return FALSE;
  2744.             count -= 3;
  2745.  
  2746.             fieldcopy = savestring (fieldname, fieldlen);
  2747.  
  2748.             staticp = (flags & CXXFLAGS_STATIC) != 0 ? TRUE : FALSE;
  2749.  
  2750.             if (staticp)
  2751.               {
  2752.                 struct ieee_var *pv, *pvend;
  2753.  
  2754.                 /* See if we can find a definition for this variable.  */
  2755.                 pv = info->vars.vars;
  2756.                 pvend = pv + info->vars.alloc;
  2757.                 for (; pv < pvend; pv++)
  2758.                   if (pv->namlen == mangledlen
  2759.                       && strncmp (pv->name, mangledname, mangledlen) == 0)
  2760.                     break;
  2761.                 if (pv < pvend)
  2762.                   ftype = pv->type;
  2763.                 else
  2764.                   {
  2765.                     /* This can happen if the variable is never used.  */
  2766.                     ftype = ieee_builtin_type (info, start,
  2767.                                                (unsigned int) builtin_void);
  2768.                   }
  2769.               }
  2770.             else
  2771.               {
  2772.                 unsigned int findx;
  2773.  
  2774.                 if (structfields == NULL)
  2775.                   {
  2776.                     ieee_error (info, start, _("C++ object has no fields"));
  2777.                     return FALSE;
  2778.                   }
  2779.  
  2780.                 for (pf = structfields, findx = 0;
  2781.                      *pf != DEBUG_FIELD_NULL;
  2782.                      pf++, findx++)
  2783.                   {
  2784.                     const char *fname;
  2785.  
  2786.                     fname = debug_get_field_name (dhandle, *pf);
  2787.                     if (fname == NULL)
  2788.                       return FALSE;
  2789.                     if (fname[0] == mangledname[0]
  2790.                         && strncmp (fname, mangledname, mangledlen) == 0
  2791.                         && strlen (fname) == mangledlen)
  2792.                       break;
  2793.                   }
  2794.                 if (*pf == DEBUG_FIELD_NULL)
  2795.                   {
  2796.                     ieee_error (info, start,
  2797.                                 _("C++ data member not found in container"));
  2798.                     return FALSE;
  2799.                   }
  2800.  
  2801.                 ftype = debug_get_field_type (dhandle, *pf);
  2802.  
  2803.                 if (debug_get_type_kind (dhandle, ftype) == DEBUG_KIND_POINTER)
  2804.                   {
  2805.                     /* We might need to convert this field into a
  2806.                        reference type later on, so make it an indirect
  2807.                        type.  */
  2808.                     if (it->fslots == NULL)
  2809.                       {
  2810.                         unsigned int fcnt;
  2811.                         const debug_field *pfcnt;
  2812.  
  2813.                         fcnt = 0;
  2814.                         for (pfcnt = structfields;
  2815.                              *pfcnt != DEBUG_FIELD_NULL;
  2816.                              pfcnt++)
  2817.                           ++fcnt;
  2818.                         it->fslots = ((debug_type *)
  2819.                                       xmalloc (fcnt * sizeof *it->fslots));
  2820.                         memset (it->fslots, 0,
  2821.                                 fcnt * sizeof *it->fslots);
  2822.                       }
  2823.  
  2824.                     if (ftype == DEBUG_TYPE_NULL)
  2825.                       return FALSE;
  2826.                     it->fslots[findx] = ftype;
  2827.                     ftype = debug_make_indirect_type (dhandle,
  2828.                                                       it->fslots + findx,
  2829.                                                       (const char *) NULL);
  2830.                   }
  2831.               }
  2832.             if (ftype == DEBUG_TYPE_NULL)
  2833.               return FALSE;
  2834.  
  2835.             switch (flags & CXXFLAGS_VISIBILITY)
  2836.               {
  2837.               default:
  2838.                 ieee_error (info, start, _("unknown C++ visibility"));
  2839.                 return FALSE;
  2840.  
  2841.               case CXXFLAGS_VISIBILITY_PUBLIC:
  2842.                 visibility = DEBUG_VISIBILITY_PUBLIC;
  2843.                 break;
  2844.  
  2845.               case CXXFLAGS_VISIBILITY_PRIVATE:
  2846.                 visibility = DEBUG_VISIBILITY_PRIVATE;
  2847.                 break;
  2848.  
  2849.               case CXXFLAGS_VISIBILITY_PROTECTED:
  2850.                 visibility = DEBUG_VISIBILITY_PROTECTED;
  2851.                 break;
  2852.               }
  2853.  
  2854.             if (staticp)
  2855.               {
  2856.                 char *mangledcopy;
  2857.  
  2858.                 mangledcopy = savestring (mangledname, mangledlen);
  2859.  
  2860.                 field = debug_make_static_member (dhandle, fieldcopy,
  2861.                                                   ftype, mangledcopy,
  2862.                                                   visibility);
  2863.               }
  2864.             else
  2865.               {
  2866.                 bfd_vma bitpos, bitsize;
  2867.  
  2868.                 bitpos = debug_get_field_bitpos (dhandle, *pf);
  2869.                 bitsize = debug_get_field_bitsize (dhandle, *pf);
  2870.                 if (bitpos == (bfd_vma) -1 || bitsize == (bfd_vma) -1)
  2871.                   {
  2872.                     ieee_error (info, start, _("bad C++ field bit pos or size"));
  2873.                     return FALSE;
  2874.                   }
  2875.                 field = debug_make_field (dhandle, fieldcopy, ftype, bitpos,
  2876.                                           bitsize, visibility);
  2877.               }
  2878.  
  2879.             if (field == DEBUG_FIELD_NULL)
  2880.               return FALSE;
  2881.  
  2882.             if (field_count + 1 >= field_alloc)
  2883.               {
  2884.                 field_alloc += 10;
  2885.                 fields = ((debug_field *)
  2886.                           xrealloc (fields, field_alloc * sizeof *fields));
  2887.               }
  2888.  
  2889.             fields[field_count] = field;
  2890.             ++field_count;
  2891.             fields[field_count] = DEBUG_FIELD_NULL;
  2892.           }
  2893.           break;
  2894.  
  2895.         case 'm':
  2896.         case 'v':
  2897.           {
  2898.             bfd_vma flags, voffset, control;
  2899.             const char *name, *mangled;
  2900.             unsigned long namlen, mangledlen;
  2901.             struct ieee_var *pv, *pvend;
  2902.             debug_type type;
  2903.             enum debug_visibility visibility;
  2904.             bfd_boolean constp, volatilep;
  2905.             char *mangledcopy;
  2906.             debug_method_variant mv;
  2907.             struct ieee_method *meth;
  2908.             unsigned int im;
  2909.  
  2910.             if (! ieee_require_asn (info, pp, &flags)
  2911.                 || ! ieee_require_atn65 (info, pp, &name, &namlen)
  2912.                 || ! ieee_require_atn65 (info, pp, &mangled, &mangledlen))
  2913.               return FALSE;
  2914.             count -= 3;
  2915.             if (id != 'v')
  2916.               voffset = 0;
  2917.             else
  2918.               {
  2919.                 if (! ieee_require_asn (info, pp, &voffset))
  2920.                   return FALSE;
  2921.                 --count;
  2922.               }
  2923.             if (! ieee_require_asn (info, pp, &control))
  2924.               return FALSE;
  2925.             --count;
  2926.  
  2927.             /* We just ignore the control information.  */
  2928.  
  2929.             /* We have no way to represent friend information, so we
  2930.                just ignore it.  */
  2931.             if ((flags & CXXFLAGS_FRIEND) != 0)
  2932.               break;
  2933.  
  2934.             /* We should already have seen a type for the function.  */
  2935.             pv = info->vars.vars;
  2936.             pvend = pv + info->vars.alloc;
  2937.             for (; pv < pvend; pv++)
  2938.               if (pv->namlen == mangledlen
  2939.                   && strncmp (pv->name, mangled, mangledlen) == 0)
  2940.                 break;
  2941.  
  2942.             if (pv >= pvend)
  2943.               {
  2944.                 /* We won't have type information for this function if
  2945.                    it is not included in this file.  We don't try to
  2946.                    handle this case.  FIXME.  */
  2947.                 type = (debug_make_function_type
  2948.                         (dhandle,
  2949.                          ieee_builtin_type (info, start,
  2950.                                             (unsigned int) builtin_void),
  2951.                          (debug_type *) NULL,
  2952.                          FALSE));
  2953.               }
  2954.             else
  2955.               {
  2956.                 debug_type return_type;
  2957.                 const debug_type *arg_types;
  2958.                 bfd_boolean varargs;
  2959.  
  2960.                 if (debug_get_type_kind (dhandle, pv->type)
  2961.                     != DEBUG_KIND_FUNCTION)
  2962.                   {
  2963.                     ieee_error (info, start,
  2964.                                 _("bad type for C++ method function"));
  2965.                     return FALSE;
  2966.                   }
  2967.  
  2968.                 return_type = debug_get_return_type (dhandle, pv->type);
  2969.                 arg_types = debug_get_parameter_types (dhandle, pv->type,
  2970.                                                        &varargs);
  2971.                 if (return_type == DEBUG_TYPE_NULL || arg_types == NULL)
  2972.                   {
  2973.                     ieee_error (info, start,
  2974.                                 _("no type information for C++ method function"));
  2975.                     return FALSE;
  2976.                   }
  2977.  
  2978.                 type = debug_make_method_type (dhandle, return_type, it->type,
  2979.                                                (debug_type *) arg_types,
  2980.                                                varargs);
  2981.               }
  2982.             if (type == DEBUG_TYPE_NULL)
  2983.               return FALSE;
  2984.  
  2985.             switch (flags & CXXFLAGS_VISIBILITY)
  2986.               {
  2987.               default:
  2988.                 ieee_error (info, start, _("unknown C++ visibility"));
  2989.                 return FALSE;
  2990.  
  2991.               case CXXFLAGS_VISIBILITY_PUBLIC:
  2992.                 visibility = DEBUG_VISIBILITY_PUBLIC;
  2993.                 break;
  2994.  
  2995.               case CXXFLAGS_VISIBILITY_PRIVATE:
  2996.                 visibility = DEBUG_VISIBILITY_PRIVATE;
  2997.                 break;
  2998.  
  2999.               case CXXFLAGS_VISIBILITY_PROTECTED:
  3000.                 visibility = DEBUG_VISIBILITY_PROTECTED;
  3001.                 break;
  3002.               }
  3003.  
  3004.             constp = (flags & CXXFLAGS_CONST) != 0 ? TRUE : FALSE;
  3005.             volatilep = (flags & CXXFLAGS_VOLATILE) != 0 ? TRUE : FALSE;
  3006.  
  3007.             mangledcopy = savestring (mangled, mangledlen);
  3008.  
  3009.             if ((flags & CXXFLAGS_STATIC) != 0)
  3010.               {
  3011.                 if (id == 'v')
  3012.                   {
  3013.                     ieee_error (info, start, _("C++ static virtual method"));
  3014.                     return FALSE;
  3015.                   }
  3016.                 mv = debug_make_static_method_variant (dhandle, mangledcopy,
  3017.                                                        type, visibility,
  3018.                                                        constp, volatilep);
  3019.               }
  3020.             else
  3021.               {
  3022.                 debug_type vcontext;
  3023.  
  3024.                 if (id != 'v')
  3025.                   vcontext = DEBUG_TYPE_NULL;
  3026.                 else
  3027.                   {
  3028.                     /* FIXME: How can we calculate this correctly?  */
  3029.                     vcontext = it->type;
  3030.                   }
  3031.                 mv = debug_make_method_variant (dhandle, mangledcopy, type,
  3032.                                                 visibility, constp,
  3033.                                                 volatilep, voffset,
  3034.                                                 vcontext);
  3035.               }
  3036.             if (mv == DEBUG_METHOD_VARIANT_NULL)
  3037.               return FALSE;
  3038.  
  3039.             for (meth = methods, im = 0; im < methods_count; meth++, im++)
  3040.               if (meth->namlen == namlen
  3041.                   && strncmp (meth->name, name, namlen) == 0)
  3042.                 break;
  3043.             if (im >= methods_count)
  3044.               {
  3045.                 if (methods_count >= methods_alloc)
  3046.                   {
  3047.                     methods_alloc += 10;
  3048.                     methods = ((struct ieee_method *)
  3049.                                xrealloc (methods,
  3050.                                          methods_alloc * sizeof *methods));
  3051.                   }
  3052.                 methods[methods_count].name = name;
  3053.                 methods[methods_count].namlen = namlen;
  3054.                 methods[methods_count].variants = NULL;
  3055.                 methods[methods_count].count = 0;
  3056.                 methods[methods_count].alloc = 0;
  3057.                 meth = methods + methods_count;
  3058.                 ++methods_count;
  3059.               }
  3060.  
  3061.             if (meth->count + 1 >= meth->alloc)
  3062.               {
  3063.                 meth->alloc += 10;
  3064.                 meth->variants = ((debug_method_variant *)
  3065.                                   xrealloc (meth->variants,
  3066.                                             (meth->alloc
  3067.                                              * sizeof *meth->variants)));
  3068.               }
  3069.  
  3070.             meth->variants[meth->count] = mv;
  3071.             ++meth->count;
  3072.             meth->variants[meth->count] = DEBUG_METHOD_VARIANT_NULL;
  3073.           }
  3074.           break;
  3075.  
  3076.         case 'o':
  3077.           {
  3078.             bfd_vma spec;
  3079.  
  3080.             /* We have no way to store this information, so we just
  3081.                ignore it.  */
  3082.             if (! ieee_require_asn (info, pp, &spec))
  3083.               return FALSE;
  3084.             --count;
  3085.             if ((spec & 4) != 0)
  3086.               {
  3087.                 const char *filename;
  3088.                 unsigned long filenamlen;
  3089.                 bfd_vma lineno;
  3090.  
  3091.                 if (! ieee_require_atn65 (info, pp, &filename, &filenamlen)
  3092.                     || ! ieee_require_asn (info, pp, &lineno))
  3093.                   return FALSE;
  3094.                 count -= 2;
  3095.               }
  3096.             else if ((spec & 8) != 0)
  3097.               {
  3098.                 const char *mangled;
  3099.                 unsigned long mangledlen;
  3100.  
  3101.                 if (! ieee_require_atn65 (info, pp, &mangled, &mangledlen))
  3102.                   return FALSE;
  3103.                 --count;
  3104.               }
  3105.             else
  3106.               {
  3107.                 ieee_error (info, start,
  3108.                             _("unrecognized C++ object overhead spec"));
  3109.                 return FALSE;
  3110.               }
  3111.           }
  3112.           break;
  3113.  
  3114.         case 'z':
  3115.           {
  3116.             const char *vname, *base;
  3117.             unsigned long vnamelen, baselen;
  3118.             bfd_vma vsize, control;
  3119.  
  3120.             /* A virtual table pointer.  */
  3121.  
  3122.             if (! ieee_require_atn65 (info, pp, &vname, &vnamelen)
  3123.                 || ! ieee_require_asn (info, pp, &vsize)
  3124.                 || ! ieee_require_atn65 (info, pp, &base, &baselen)
  3125.                 || ! ieee_require_asn (info, pp, &control))
  3126.               return FALSE;
  3127.             count -= 4;
  3128.  
  3129.             /* We just ignore the control number.  We don't care what
  3130.                the virtual table name is.  We have no way to store the
  3131.                virtual table size, and I don't think we care anyhow.  */
  3132.  
  3133.             /* FIXME: We can't handle multiple virtual table pointers.  */
  3134.  
  3135.             if (baselen == 0)
  3136.               ownvptr = TRUE;
  3137.             else
  3138.               {
  3139.                 char *basecopy;
  3140.  
  3141.                 basecopy = savestring (base, baselen);
  3142.                 vptrbase = debug_find_tagged_type (dhandle, basecopy,
  3143.                                                    DEBUG_KIND_ILLEGAL);
  3144.                 free (basecopy);
  3145.                 if (vptrbase == DEBUG_TYPE_NULL)
  3146.                   {
  3147.                     ieee_error (info, start, _("undefined C++ vtable"));
  3148.                     return FALSE;
  3149.                   }
  3150.               }
  3151.           }
  3152.           break;
  3153.         }
  3154.     }
  3155.  
  3156.   /* Now that we have seen all the method variants, we can call
  3157.      debug_make_method for each one.  */
  3158.  
  3159.   if (methods_count == 0)
  3160.     dmethods = NULL;
  3161.   else
  3162.     {
  3163.       unsigned int i;
  3164.  
  3165.       dmethods = ((debug_method *)
  3166.                   xmalloc ((methods_count + 1) * sizeof *dmethods));
  3167.       for (i = 0; i < methods_count; i++)
  3168.         {
  3169.           char *namcopy;
  3170.  
  3171.           namcopy = savestring (methods[i].name, methods[i].namlen);
  3172.           dmethods[i] = debug_make_method (dhandle, namcopy,
  3173.                                            methods[i].variants);
  3174.           if (dmethods[i] == DEBUG_METHOD_NULL)
  3175.             return FALSE;
  3176.         }
  3177.       dmethods[i] = DEBUG_METHOD_NULL;
  3178.       free (methods);
  3179.     }
  3180.  
  3181.   /* The struct type was created as an indirect type pointing at
  3182.      it->slot.  We update it->slot to automatically update all
  3183.      references to this struct.  */
  3184.   it->slot = debug_make_object_type (dhandle,
  3185.                                      cxx_class != 'u',
  3186.                                      debug_get_type_size (dhandle,
  3187.                                                           it->slot),
  3188.                                      fields, baseclasses, dmethods,
  3189.                                      vptrbase, ownvptr);
  3190.   if (it->slot == DEBUG_TYPE_NULL)
  3191.     return FALSE;
  3192.  
  3193.   return TRUE;
  3194. }
  3195.  
  3196. /* Read C++ default argument value and reference type information.  */
  3197.  
  3198. static bfd_boolean
  3199. ieee_read_cxx_defaults (struct ieee_info *info, const bfd_byte **pp,
  3200.                         unsigned long count)
  3201. {
  3202.   const bfd_byte *start;
  3203.   const char *fnname;
  3204.   unsigned long fnlen;
  3205.   bfd_vma defcount;
  3206.  
  3207.   start = *pp;
  3208.  
  3209.   /* Giving the function name before the argument count is an addendum
  3210.      to the spec.  The function name is demangled, though, so this
  3211.      record must always refer to the current function.  */
  3212.  
  3213.   if (info->blockstack.bsp <= info->blockstack.stack
  3214.       || info->blockstack.bsp[-1].fnindx == (unsigned int) -1)
  3215.     {
  3216.       ieee_error (info, start, _("C++ default values not in a function"));
  3217.       return FALSE;
  3218.     }
  3219.  
  3220.   if (! ieee_require_atn65 (info, pp, &fnname, &fnlen)
  3221.       || ! ieee_require_asn (info, pp, &defcount))
  3222.     return FALSE;
  3223.   count -= 2;
  3224.  
  3225.   while (defcount-- > 0)
  3226.     {
  3227.       bfd_vma type, val;
  3228.       const char *strval;
  3229.       unsigned long strvallen;
  3230.  
  3231.       if (! ieee_require_asn (info, pp, &type))
  3232.         return FALSE;
  3233.       --count;
  3234.  
  3235.       switch (type)
  3236.         {
  3237.         case 0:
  3238.         case 4:
  3239.           break;
  3240.  
  3241.         case 1:
  3242.         case 2:
  3243.           if (! ieee_require_asn (info, pp, &val))
  3244.             return FALSE;
  3245.           --count;
  3246.           break;
  3247.  
  3248.         case 3:
  3249.         case 7:
  3250.           if (! ieee_require_atn65 (info, pp, &strval, &strvallen))
  3251.             return FALSE;
  3252.           --count;
  3253.           break;
  3254.  
  3255.         default:
  3256.           ieee_error (info, start, _("unrecognized C++ default type"));
  3257.           return FALSE;
  3258.         }
  3259.  
  3260.       /* We have no way to record the default argument values, so we
  3261.          just ignore them.  FIXME.  */
  3262.     }
  3263.  
  3264.   /* Any remaining arguments are indices of parameters that are really
  3265.      reference type.  */
  3266.   if (count > 0)
  3267.     {
  3268.       void *dhandle;
  3269.       debug_type *arg_slots;
  3270.  
  3271.       dhandle = info->dhandle;
  3272.       arg_slots = info->types.types[info->blockstack.bsp[-1].fnindx].arg_slots;
  3273.       while (count-- > 0)
  3274.         {
  3275.           bfd_vma indx;
  3276.           debug_type target;
  3277.  
  3278.           if (! ieee_require_asn (info, pp, &indx))
  3279.             return FALSE;
  3280.           /* The index is 1 based.  */
  3281.           --indx;
  3282.           if (arg_slots == NULL
  3283.               || arg_slots[indx] == DEBUG_TYPE_NULL
  3284.               || (debug_get_type_kind (dhandle, arg_slots[indx])
  3285.                   != DEBUG_KIND_POINTER))
  3286.             {
  3287.               ieee_error (info, start, _("reference parameter is not a pointer"));
  3288.               return FALSE;
  3289.             }
  3290.  
  3291.           target = debug_get_target_type (dhandle, arg_slots[indx]);
  3292.           arg_slots[indx] = debug_make_reference_type (dhandle, target);
  3293.           if (arg_slots[indx] == DEBUG_TYPE_NULL)
  3294.             return FALSE;
  3295.         }
  3296.     }
  3297.  
  3298.   return TRUE;
  3299. }
  3300.  
  3301. /* Read a C++ reference definition.  */
  3302.  
  3303. static bfd_boolean
  3304. ieee_read_reference (struct ieee_info *info, const bfd_byte **pp)
  3305. {
  3306.   const bfd_byte *start;
  3307.   bfd_vma flags;
  3308.   const char *cxx_class, *name;
  3309.   unsigned long classlen, namlen;
  3310.   debug_type *pslot;
  3311.   debug_type target;
  3312.  
  3313.   start = *pp;
  3314.  
  3315.   if (! ieee_require_asn (info, pp, &flags))
  3316.     return FALSE;
  3317.  
  3318.   /* Giving the class name before the member name is in an addendum to
  3319.      the spec.  */
  3320.   if (flags == 3)
  3321.     {
  3322.       if (! ieee_require_atn65 (info, pp, &cxx_class, &classlen))
  3323.         return FALSE;
  3324.     }
  3325.  
  3326.   if (! ieee_require_atn65 (info, pp, &name, &namlen))
  3327.     return FALSE;
  3328.  
  3329.   pslot = NULL;
  3330.   if (flags != 3)
  3331.     {
  3332.       int pass;
  3333.  
  3334.       /* We search from the last variable indices to the first in
  3335.          hopes of finding local variables correctly.  We search the
  3336.          local variables on the first pass, and the global variables
  3337.          on the second.  FIXME: This probably won't work in all cases.
  3338.          On the other hand, I don't know what will.  */
  3339.       for (pass = 0; pass < 2; pass++)
  3340.         {
  3341.           struct ieee_vars *vars;
  3342.           int i;
  3343.           struct ieee_var *pv = NULL;
  3344.  
  3345.           if (pass == 0)
  3346.             vars = &info->vars;
  3347.           else
  3348.             {
  3349.               vars = info->global_vars;
  3350.               if (vars == NULL)
  3351.                 break;
  3352.             }
  3353.  
  3354.           for (i = (int) vars->alloc - 1; i >= 0; i--)
  3355.             {
  3356.               bfd_boolean found;
  3357.  
  3358.               pv = vars->vars + i;
  3359.  
  3360.               if (pv->pslot == NULL
  3361.                   || pv->namlen != namlen
  3362.                   || strncmp (pv->name, name, namlen) != 0)
  3363.                 continue;
  3364.  
  3365.               found = FALSE;
  3366.               switch (flags)
  3367.                 {
  3368.                 default:
  3369.                   ieee_error (info, start,
  3370.                               _("unrecognized C++ reference type"));
  3371.                   return FALSE;
  3372.  
  3373.                 case 0:
  3374.                   /* Global variable or function.  */
  3375.                   if (pv->kind == IEEE_GLOBAL
  3376.                       || pv->kind == IEEE_EXTERNAL
  3377.                       || pv->kind == IEEE_FUNCTION)
  3378.                     found = TRUE;
  3379.                   break;
  3380.  
  3381.                 case 1:
  3382.                   /* Global static variable or function.  */
  3383.                   if (pv->kind == IEEE_STATIC
  3384.                       || pv->kind == IEEE_FUNCTION)
  3385.                     found = TRUE;
  3386.                   break;
  3387.  
  3388.                 case 2:
  3389.                   /* Local variable.  */
  3390.                   if (pv->kind == IEEE_LOCAL)
  3391.                     found = TRUE;
  3392.                   break;
  3393.                 }
  3394.  
  3395.               if (found)
  3396.                 break;
  3397.             }
  3398.  
  3399.           if (i >= 0)
  3400.             {
  3401.               pslot = pv->pslot;
  3402.               break;
  3403.             }
  3404.         }
  3405.     }
  3406.   else
  3407.     {
  3408.       struct ieee_tag *it;
  3409.  
  3410.       for (it = info->tags; it != NULL; it = it->next)
  3411.         {
  3412.           if (it->name[0] == cxx_class[0]
  3413.               && strncmp (it->name, cxx_class, classlen) == 0
  3414.               && strlen (it->name) == classlen)
  3415.             {
  3416.               if (it->fslots != NULL)
  3417.                 {
  3418.                   const debug_field *pf;
  3419.                   unsigned int findx;
  3420.  
  3421.                   pf = debug_get_fields (info->dhandle, it->type);
  3422.                   if (pf == NULL)
  3423.                     {
  3424.                       ieee_error (info, start,
  3425.                                   "C++ reference in class with no fields");
  3426.                       return FALSE;
  3427.                     }
  3428.  
  3429.                   for (findx = 0; *pf != DEBUG_FIELD_NULL; pf++, findx++)
  3430.                     {
  3431.                       const char *fname;
  3432.  
  3433.                       fname = debug_get_field_name (info->dhandle, *pf);
  3434.                       if (fname == NULL)
  3435.                         return FALSE;
  3436.                       if (strncmp (fname, name, namlen) == 0
  3437.                           && strlen (fname) == namlen)
  3438.                         {
  3439.                           pslot = it->fslots + findx;
  3440.                           break;
  3441.                         }
  3442.                     }
  3443.                 }
  3444.  
  3445.               break;
  3446.             }
  3447.         }
  3448.     }
  3449.  
  3450.   if (pslot == NULL)
  3451.     {
  3452.       ieee_error (info, start, _("C++ reference not found"));
  3453.       return FALSE;
  3454.     }
  3455.  
  3456.   /* We allocated the type of the object as an indirect type pointing
  3457.      to *pslot, which we can now update to be a reference type.  */
  3458.   if (debug_get_type_kind (info->dhandle, *pslot) != DEBUG_KIND_POINTER)
  3459.     {
  3460.       ieee_error (info, start, _("C++ reference is not pointer"));
  3461.       return FALSE;
  3462.     }
  3463.  
  3464.   target = debug_get_target_type (info->dhandle, *pslot);
  3465.   *pslot = debug_make_reference_type (info->dhandle, target);
  3466.   if (*pslot == DEBUG_TYPE_NULL)
  3467.     return FALSE;
  3468.  
  3469.   return TRUE;
  3470. }
  3471.  
  3472. /* Require an ASN record.  */
  3473.  
  3474. static bfd_boolean
  3475. ieee_require_asn (struct ieee_info *info, const bfd_byte **pp, bfd_vma *pv)
  3476. {
  3477.   const bfd_byte *start;
  3478.   ieee_record_enum_type c;
  3479.   bfd_vma varindx;
  3480.  
  3481.   start = *pp;
  3482.  
  3483.   c = (ieee_record_enum_type) **pp;
  3484.   if (c != ieee_e2_first_byte_enum)
  3485.     {
  3486.       ieee_error (info, start, _("missing required ASN"));
  3487.       return FALSE;
  3488.     }
  3489.   ++*pp;
  3490.  
  3491.   c = (ieee_record_enum_type) (((unsigned int) c << 8) | **pp);
  3492.   if (c != ieee_asn_record_enum)
  3493.     {
  3494.       ieee_error (info, start, _("missing required ASN"));
  3495.       return FALSE;
  3496.     }
  3497.   ++*pp;
  3498.  
  3499.   /* Just ignore the variable index.  */
  3500.   if (! ieee_read_number (info, pp, &varindx))
  3501.     return FALSE;
  3502.  
  3503.   return ieee_read_expression (info, pp, pv);
  3504. }
  3505.  
  3506. /* Require an ATN65 record.  */
  3507.  
  3508. static bfd_boolean
  3509. ieee_require_atn65 (struct ieee_info *info, const bfd_byte **pp,
  3510.                     const char **pname, unsigned long *pnamlen)
  3511. {
  3512.   const bfd_byte *start;
  3513.   ieee_record_enum_type c;
  3514.   bfd_vma name_indx, type_indx, atn_code;
  3515.  
  3516.   start = *pp;
  3517.  
  3518.   c = (ieee_record_enum_type) **pp;
  3519.   if (c != ieee_at_record_enum)
  3520.     {
  3521.       ieee_error (info, start, _("missing required ATN65"));
  3522.       return FALSE;
  3523.     }
  3524.   ++*pp;
  3525.  
  3526.   c = (ieee_record_enum_type) (((unsigned int) c << 8) | **pp);
  3527.   if (c != ieee_atn_record_enum)
  3528.     {
  3529.       ieee_error (info, start, _("missing required ATN65"));
  3530.       return FALSE;
  3531.     }
  3532.   ++*pp;
  3533.  
  3534.   if (! ieee_read_number (info, pp, &name_indx)
  3535.       || ! ieee_read_number (info, pp, &type_indx)
  3536.       || ! ieee_read_number (info, pp, &atn_code))
  3537.     return FALSE;
  3538.  
  3539.   /* Just ignore name_indx.  */
  3540.  
  3541.   if (type_indx != 0 || atn_code != 65)
  3542.     {
  3543.       ieee_error (info, start, _("bad ATN65 record"));
  3544.       return FALSE;
  3545.     }
  3546.  
  3547.   return ieee_read_id (info, pp, pname, pnamlen);
  3548. }
  3549. /* Convert a register number in IEEE debugging information into a
  3550.    generic register number.  */
  3551.  
  3552. static int
  3553. ieee_regno_to_genreg (bfd *abfd, int r)
  3554. {
  3555.   switch (bfd_get_arch (abfd))
  3556.     {
  3557.     case bfd_arch_m68k:
  3558.       /* For some reasons stabs adds 2 to the floating point register
  3559.          numbers.  */
  3560.       if (r >= 16)
  3561.         r += 2;
  3562.       break;
  3563.  
  3564.     case bfd_arch_i960:
  3565.       /* Stabs uses 0 to 15 for r0 to r15, 16 to 31 for g0 to g15, and
  3566.          32 to 35 for fp0 to fp3.  */
  3567.       --r;
  3568.       break;
  3569.  
  3570.     default:
  3571.       break;
  3572.     }
  3573.  
  3574.   return r;
  3575. }
  3576.  
  3577. /* Convert a generic register number to an IEEE specific one.  */
  3578.  
  3579. static int
  3580. ieee_genreg_to_regno (bfd *abfd, int r)
  3581. {
  3582.   switch (bfd_get_arch (abfd))
  3583.     {
  3584.     case bfd_arch_m68k:
  3585.       /* For some reason stabs add 2 to the floating point register
  3586.          numbers.  */
  3587.       if (r >= 18)
  3588.         r -= 2;
  3589.       break;
  3590.  
  3591.     case bfd_arch_i960:
  3592.       /* Stabs uses 0 to 15 for r0 to r15, 16 to 31 for g0 to g15, and
  3593.          32 to 35 for fp0 to fp3.  */
  3594.       ++r;
  3595.       break;
  3596.  
  3597.     default:
  3598.       break;
  3599.     }
  3600.  
  3601.   return r;
  3602. }
  3603. /* These routines build IEEE debugging information out of the generic
  3604.    debugging information.  */
  3605.  
  3606. /* We build the IEEE debugging information byte by byte.  Rather than
  3607.    waste time copying data around, we use a linked list of buffers to
  3608.    hold the data.  */
  3609.  
  3610. #define IEEE_BUFSIZE (490)
  3611.  
  3612. struct ieee_buf
  3613. {
  3614.   /* Next buffer.  */
  3615.   struct ieee_buf *next;
  3616.   /* Number of data bytes in this buffer.  */
  3617.   unsigned int c;
  3618.   /* Bytes.  */
  3619.   bfd_byte buf[IEEE_BUFSIZE];
  3620. };
  3621.  
  3622. /* A list of buffers.  */
  3623.  
  3624. struct ieee_buflist
  3625. {
  3626.   /* Head of list.  */
  3627.   struct ieee_buf *head;
  3628.   /* Tail--last buffer on list.  */
  3629.   struct ieee_buf *tail;
  3630. };
  3631.  
  3632. /* In order to generate the BB11 blocks required by the HP emulator,
  3633.    we keep track of ranges of addresses which correspond to a given
  3634.    compilation unit.  */
  3635.  
  3636. struct ieee_range
  3637. {
  3638.   /* Next range.  */
  3639.   struct ieee_range *next;
  3640.   /* Low address.  */
  3641.   bfd_vma low;
  3642.   /* High address.  */
  3643.   bfd_vma high;
  3644. };
  3645.  
  3646. /* This structure holds information for a class on the type stack.  */
  3647.  
  3648. struct ieee_type_class
  3649. {
  3650.   /* The name index in the debugging information.  */
  3651.   unsigned int indx;
  3652.   /* The pmisc records for the class.  */
  3653.   struct ieee_buflist pmiscbuf;
  3654.   /* The number of pmisc records.  */
  3655.   unsigned int pmisccount;
  3656.   /* The name of the class holding the virtual table, if not this
  3657.      class.  */
  3658.   const char *vclass;
  3659.   /* Whether this class holds its own virtual table.  */
  3660.   bfd_boolean ownvptr;
  3661.   /* The largest virtual table offset seen so far.  */
  3662.   bfd_vma voffset;
  3663.   /* The current method.  */
  3664.   const char *method;
  3665.   /* Additional pmisc records used to record fields of reference type.  */
  3666.   struct ieee_buflist refs;
  3667. };
  3668.  
  3669. /* This is how we store types for the writing routines.  Most types
  3670.    are simply represented by a type index.  */
  3671.  
  3672. struct ieee_write_type
  3673. {
  3674.   /* Type index.  */
  3675.   unsigned int indx;
  3676.   /* The size of the type, if known.  */
  3677.   unsigned int size;
  3678.   /* The name of the type, if any.  */
  3679.   const char *name;
  3680.   /* If this is a function or method type, we build the type here, and
  3681.      only add it to the output buffers if we need it.  */
  3682.   struct ieee_buflist fndef;
  3683.   /* If this is a struct, this is where the struct definition is
  3684.      built.  */
  3685.   struct ieee_buflist strdef;
  3686.   /* If this is a class, this is where the class information is built.  */
  3687.   struct ieee_type_class *classdef;
  3688.   /* Whether the type is unsigned.  */
  3689.   unsigned int unsignedp : 1;
  3690.   /* Whether this is a reference type.  */
  3691.   unsigned int referencep : 1;
  3692.   /* Whether this is in the local type block.  */
  3693.   unsigned int localp : 1;
  3694.   /* Whether this is a duplicate struct definition which we are
  3695.      ignoring.  */
  3696.   unsigned int ignorep : 1;
  3697. };
  3698.  
  3699. /* This is the type stack used by the debug writing routines.  FIXME:
  3700.    We could generate more efficient output if we remembered when we
  3701.    have output a particular type before.  */
  3702.  
  3703. struct ieee_type_stack
  3704. {
  3705.   /* Next entry on stack.  */
  3706.   struct ieee_type_stack *next;
  3707.   /* Type information.  */
  3708.   struct ieee_write_type type;
  3709. };
  3710.  
  3711. /* This is a list of associations between a name and some types.
  3712.    These are used for typedefs and tags.  */
  3713.  
  3714. struct ieee_name_type
  3715. {
  3716.   /* Next type for this name.  */
  3717.   struct ieee_name_type *next;
  3718.   /* ID number.  For a typedef, this is the index of the type to which
  3719.      this name is typedefed.  */
  3720.   unsigned int id;
  3721.   /* Type.  */
  3722.   struct ieee_write_type type;
  3723.   /* If this is a tag which has not yet been defined, this is the
  3724.      kind.  If the tag has been defined, this is DEBUG_KIND_ILLEGAL.  */
  3725.   enum debug_type_kind kind;
  3726. };
  3727.  
  3728. /* We use a hash table to associate names and types.  */
  3729.  
  3730. struct ieee_name_type_hash_table
  3731. {
  3732.   struct bfd_hash_table root;
  3733. };
  3734.  
  3735. struct ieee_name_type_hash_entry
  3736. {
  3737.   struct bfd_hash_entry root;
  3738.   /* Information for this name.  */
  3739.   struct ieee_name_type *types;
  3740. };
  3741.  
  3742. /* This is a list of enums.  */
  3743.  
  3744. struct ieee_defined_enum
  3745. {
  3746.   /* Next enum.  */
  3747.   struct ieee_defined_enum *next;
  3748.   /* Type index.  */
  3749.   unsigned int indx;
  3750.   /* Whether this enum has been defined.  */
  3751.   bfd_boolean defined;
  3752.   /* Tag.  */
  3753.   const char *tag;
  3754.   /* Names.  */
  3755.   const char **names;
  3756.   /* Values.  */
  3757.   bfd_signed_vma *vals;
  3758. };
  3759.  
  3760. /* We keep a list of modified versions of types, so that we don't
  3761.    output them more than once.  */
  3762.  
  3763. struct ieee_modified_type
  3764. {
  3765.   /* Pointer to this type.  */
  3766.   unsigned int pointer;
  3767.   /* Function with unknown arguments returning this type.  */
  3768.   unsigned int function;
  3769.   /* Const version of this type.  */
  3770.   unsigned int const_qualified;
  3771.   /* Volatile version of this type.  */
  3772.   unsigned int volatile_qualified;
  3773.   /* List of arrays of this type of various bounds.  */
  3774.   struct ieee_modified_array_type *arrays;
  3775. };
  3776.  
  3777. /* A list of arrays bounds.  */
  3778.  
  3779. struct ieee_modified_array_type
  3780. {
  3781.   /* Next array bounds.  */
  3782.   struct ieee_modified_array_type *next;
  3783.   /* Type index with these bounds.  */
  3784.   unsigned int indx;
  3785.   /* Low bound.  */
  3786.   bfd_signed_vma low;
  3787.   /* High bound.  */
  3788.   bfd_signed_vma high;
  3789. };
  3790.  
  3791. /* This is a list of pending function parameter information.  We don't
  3792.    output them until we see the first block.  */
  3793.  
  3794. struct ieee_pending_parm
  3795. {
  3796.   /* Next pending parameter.  */
  3797.   struct ieee_pending_parm *next;
  3798.   /* Name.  */
  3799.   const char *name;
  3800.   /* Type index.  */
  3801.   unsigned int type;
  3802.   /* Whether the type is a reference.  */
  3803.   bfd_boolean referencep;
  3804.   /* Kind.  */
  3805.   enum debug_parm_kind kind;
  3806.   /* Value.  */
  3807.   bfd_vma val;
  3808. };
  3809.  
  3810. /* This is the handle passed down by debug_write.  */
  3811.  
  3812. struct ieee_handle
  3813. {
  3814.   /* BFD we are writing to.  */
  3815.   bfd *abfd;
  3816.   /* Whether we got an error in a subroutine called via traverse or
  3817.      map_over_sections.  */
  3818.   bfd_boolean error;
  3819.   /* Current data buffer list.  */
  3820.   struct ieee_buflist *current;
  3821.   /* Current data buffer.  */
  3822.   struct ieee_buf *curbuf;
  3823.   /* Filename of current compilation unit.  */
  3824.   const char *filename;
  3825.   /* Module name of current compilation unit.  */
  3826.   const char *modname;
  3827.   /* List of buffer for global types.  */
  3828.   struct ieee_buflist global_types;
  3829.   /* List of finished data buffers.  */
  3830.   struct ieee_buflist data;
  3831.   /* List of buffers for typedefs in the current compilation unit.  */
  3832.   struct ieee_buflist types;
  3833.   /* List of buffers for variables and functions in the current
  3834.      compilation unit.  */
  3835.   struct ieee_buflist vars;
  3836.   /* List of buffers for C++ class definitions in the current
  3837.      compilation unit.  */
  3838.   struct ieee_buflist cxx;
  3839.   /* List of buffers for line numbers in the current compilation unit.  */
  3840.   struct ieee_buflist linenos;
  3841.   /* Ranges for the current compilation unit.  */
  3842.   struct ieee_range *ranges;
  3843.   /* Ranges for all debugging information.  */
  3844.   struct ieee_range *global_ranges;
  3845.   /* Nested pending ranges.  */
  3846.   struct ieee_range *pending_ranges;
  3847.   /* Type stack.  */
  3848.   struct ieee_type_stack *type_stack;
  3849.   /* Next unallocated type index.  */
  3850.   unsigned int type_indx;
  3851.   /* Next unallocated name index.  */
  3852.   unsigned int name_indx;
  3853.   /* Typedefs.  */
  3854.   struct ieee_name_type_hash_table typedefs;
  3855.   /* Tags.  */
  3856.   struct ieee_name_type_hash_table tags;
  3857.   /* Enums.  */
  3858.   struct ieee_defined_enum *enums;
  3859.   /* Modified versions of types.  */
  3860.   struct ieee_modified_type *modified;
  3861.   /* Number of entries allocated in modified.  */
  3862.   unsigned int modified_alloc;
  3863.   /* 4 byte complex type.  */
  3864.   unsigned int complex_float_index;
  3865.   /* 8 byte complex type.  */
  3866.   unsigned int complex_double_index;
  3867.   /* The depth of block nesting.  This is 0 outside a function, and 1
  3868.      just after start_function is called.  */
  3869.   unsigned int block_depth;
  3870.   /* The name of the current function.  */
  3871.   const char *fnname;
  3872.   /* List of buffers for the type of the function we are currently
  3873.      writing out.  */
  3874.   struct ieee_buflist fntype;
  3875.   /* List of buffers for the parameters of the function we are
  3876.      currently writing out.  */
  3877.   struct ieee_buflist fnargs;
  3878.   /* Number of arguments written to fnargs.  */
  3879.   unsigned int fnargcount;
  3880.   /* Pending function parameters.  */
  3881.   struct ieee_pending_parm *pending_parms;
  3882.   /* Current line number filename.  */
  3883.   const char *lineno_filename;
  3884.   /* Line number name index.  */
  3885.   unsigned int lineno_name_indx;
  3886.   /* Filename of pending line number.  */
  3887.   const char *pending_lineno_filename;
  3888.   /* Pending line number.  */
  3889.   unsigned long pending_lineno;
  3890.   /* Address of pending line number.  */
  3891.   bfd_vma pending_lineno_addr;
  3892.   /* Highest address seen at end of procedure.  */
  3893.   bfd_vma highaddr;
  3894. };
  3895.  
  3896. static bfd_boolean ieee_init_buffer
  3897.   (struct ieee_handle *, struct ieee_buflist *);
  3898. static bfd_boolean ieee_change_buffer
  3899.   (struct ieee_handle *, struct ieee_buflist *);
  3900. static bfd_boolean ieee_append_buffer
  3901.   (struct ieee_handle *, struct ieee_buflist *, struct ieee_buflist *);
  3902. static bfd_boolean ieee_real_write_byte (struct ieee_handle *, int);
  3903. static bfd_boolean ieee_write_2bytes (struct ieee_handle *, int);
  3904. static bfd_boolean ieee_write_number (struct ieee_handle *, bfd_vma);
  3905. static bfd_boolean ieee_write_id (struct ieee_handle *, const char *);
  3906. static bfd_boolean ieee_write_asn
  3907.   (struct ieee_handle *, unsigned int, bfd_vma);
  3908. static bfd_boolean ieee_write_atn65
  3909.   (struct ieee_handle *, unsigned int, const char *);
  3910. static bfd_boolean ieee_push_type
  3911.   (struct ieee_handle *, unsigned int, unsigned int, bfd_boolean,
  3912.    bfd_boolean);
  3913. static unsigned int ieee_pop_type (struct ieee_handle *);
  3914. static void ieee_pop_unused_type (struct ieee_handle *);
  3915. static unsigned int ieee_pop_type_used (struct ieee_handle *, bfd_boolean);
  3916. static bfd_boolean ieee_add_range
  3917.   (struct ieee_handle *, bfd_boolean, bfd_vma, bfd_vma);
  3918. static bfd_boolean ieee_start_range (struct ieee_handle *, bfd_vma);
  3919. static bfd_boolean ieee_end_range (struct ieee_handle *, bfd_vma);
  3920. static bfd_boolean ieee_define_type
  3921.   (struct ieee_handle *, unsigned int, bfd_boolean, bfd_boolean);
  3922. static bfd_boolean ieee_define_named_type
  3923.   (struct ieee_handle *, const char *, unsigned int, unsigned int,
  3924.    bfd_boolean, bfd_boolean, struct ieee_buflist *);
  3925. static struct ieee_modified_type *ieee_get_modified_info
  3926.   (struct ieee_handle *, unsigned int);
  3927. static struct bfd_hash_entry *ieee_name_type_newfunc
  3928.   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  3929. static bfd_boolean ieee_write_undefined_tag
  3930.   (struct ieee_name_type_hash_entry *, void *);
  3931. static bfd_boolean ieee_finish_compilation_unit (struct ieee_handle *);
  3932. static void ieee_add_bb11_blocks (bfd *, asection *, void *);
  3933. static bfd_boolean ieee_add_bb11
  3934.   (struct ieee_handle *, asection *, bfd_vma, bfd_vma);
  3935. static bfd_boolean ieee_output_pending_parms (struct ieee_handle *);
  3936. static unsigned int ieee_vis_to_flags (enum debug_visibility);
  3937. static bfd_boolean ieee_class_method_var
  3938.   (struct ieee_handle *, const char *, enum debug_visibility, bfd_boolean,
  3939.    bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
  3940.  
  3941. static bfd_boolean ieee_start_compilation_unit (void *, const char *);
  3942. static bfd_boolean ieee_start_source (void *, const char *);
  3943. static bfd_boolean ieee_empty_type (void *);
  3944. static bfd_boolean ieee_void_type (void *);
  3945. static bfd_boolean ieee_int_type (void *, unsigned int, bfd_boolean);
  3946. static bfd_boolean ieee_float_type (void *, unsigned int);
  3947. static bfd_boolean ieee_complex_type (void *, unsigned int);
  3948. static bfd_boolean ieee_bool_type (void *, unsigned int);
  3949. static bfd_boolean ieee_enum_type
  3950.   (void *, const char *, const char **, bfd_signed_vma *);
  3951. static bfd_boolean ieee_pointer_type (void *);
  3952. static bfd_boolean ieee_function_type (void *, int, bfd_boolean);
  3953. static bfd_boolean ieee_reference_type (void *);
  3954. static bfd_boolean ieee_range_type (void *, bfd_signed_vma, bfd_signed_vma);
  3955. static bfd_boolean ieee_array_type
  3956.   (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
  3957. static bfd_boolean ieee_set_type (void *, bfd_boolean);
  3958. static bfd_boolean ieee_offset_type (void *);
  3959. static bfd_boolean ieee_method_type (void *, bfd_boolean, int, bfd_boolean);
  3960. static bfd_boolean ieee_const_type (void *);
  3961. static bfd_boolean ieee_volatile_type (void *);
  3962. static bfd_boolean ieee_start_struct_type
  3963.   (void *, const char *, unsigned int, bfd_boolean, unsigned int);
  3964. static bfd_boolean ieee_struct_field
  3965.   (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
  3966. static bfd_boolean ieee_end_struct_type (void *);
  3967. static bfd_boolean ieee_start_class_type
  3968.   (void *, const char *, unsigned int, bfd_boolean, unsigned int, bfd_boolean,
  3969.    bfd_boolean);
  3970. static bfd_boolean ieee_class_static_member
  3971.   (void *, const char *, const char *, enum debug_visibility);
  3972. static bfd_boolean ieee_class_baseclass
  3973.   (void *, bfd_vma, bfd_boolean, enum debug_visibility);
  3974. static bfd_boolean ieee_class_start_method (void *, const char *);
  3975. static bfd_boolean ieee_class_method_variant
  3976.   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
  3977.    bfd_vma, bfd_boolean);
  3978. static bfd_boolean ieee_class_static_method_variant
  3979.   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
  3980. static bfd_boolean ieee_class_end_method (void *);
  3981. static bfd_boolean ieee_end_class_type (void *);
  3982. static bfd_boolean ieee_typedef_type (void *, const char *);
  3983. static bfd_boolean ieee_tag_type
  3984.   (void *, const char *, unsigned int, enum debug_type_kind);
  3985. static bfd_boolean ieee_typdef (void *, const char *);
  3986. static bfd_boolean ieee_tag (void *, const char *);
  3987. static bfd_boolean ieee_int_constant (void *, const char *, bfd_vma);
  3988. static bfd_boolean ieee_float_constant (void *, const char *, double);
  3989. static bfd_boolean ieee_typed_constant (void *, const char *, bfd_vma);
  3990. static bfd_boolean ieee_variable
  3991.   (void *, const char *, enum debug_var_kind, bfd_vma);
  3992. static bfd_boolean ieee_start_function (void *, const char *, bfd_boolean);
  3993. static bfd_boolean ieee_function_parameter
  3994.   (void *, const char *, enum debug_parm_kind, bfd_vma);
  3995. static bfd_boolean ieee_start_block (void *, bfd_vma);
  3996. static bfd_boolean ieee_end_block (void *, bfd_vma);
  3997. static bfd_boolean ieee_end_function (void *);
  3998. static bfd_boolean ieee_lineno (void *, const char *, unsigned long, bfd_vma);
  3999.  
  4000. static const struct debug_write_fns ieee_fns =
  4001. {
  4002.   ieee_start_compilation_unit,
  4003.   ieee_start_source,
  4004.   ieee_empty_type,
  4005.   ieee_void_type,
  4006.   ieee_int_type,
  4007.   ieee_float_type,
  4008.   ieee_complex_type,
  4009.   ieee_bool_type,
  4010.   ieee_enum_type,
  4011.   ieee_pointer_type,
  4012.   ieee_function_type,
  4013.   ieee_reference_type,
  4014.   ieee_range_type,
  4015.   ieee_array_type,
  4016.   ieee_set_type,
  4017.   ieee_offset_type,
  4018.   ieee_method_type,
  4019.   ieee_const_type,
  4020.   ieee_volatile_type,
  4021.   ieee_start_struct_type,
  4022.   ieee_struct_field,
  4023.   ieee_end_struct_type,
  4024.   ieee_start_class_type,
  4025.   ieee_class_static_member,
  4026.   ieee_class_baseclass,
  4027.   ieee_class_start_method,
  4028.   ieee_class_method_variant,
  4029.   ieee_class_static_method_variant,
  4030.   ieee_class_end_method,
  4031.   ieee_end_class_type,
  4032.   ieee_typedef_type,
  4033.   ieee_tag_type,
  4034.   ieee_typdef,
  4035.   ieee_tag,
  4036.   ieee_int_constant,
  4037.   ieee_float_constant,
  4038.   ieee_typed_constant,
  4039.   ieee_variable,
  4040.   ieee_start_function,
  4041.   ieee_function_parameter,
  4042.   ieee_start_block,
  4043.   ieee_end_block,
  4044.   ieee_end_function,
  4045.   ieee_lineno
  4046. };
  4047.  
  4048. /* Initialize a buffer to be empty.  */
  4049.  
  4050. static bfd_boolean
  4051. ieee_init_buffer (struct ieee_handle *info ATTRIBUTE_UNUSED,
  4052.                   struct ieee_buflist *buflist)
  4053. {
  4054.   buflist->head = NULL;
  4055.   buflist->tail = NULL;
  4056.   return TRUE;
  4057. }
  4058.  
  4059. /* See whether a buffer list has any data.  */
  4060.  
  4061. #define ieee_buffer_emptyp(buflist) ((buflist)->head == NULL)
  4062.  
  4063. /* Change the current buffer to a specified buffer chain.  */
  4064.  
  4065. static bfd_boolean
  4066. ieee_change_buffer (struct ieee_handle *info, struct ieee_buflist *buflist)
  4067. {
  4068.   if (buflist->head == NULL)
  4069.     {
  4070.       struct ieee_buf *buf;
  4071.  
  4072.       buf = (struct ieee_buf *) xmalloc (sizeof *buf);
  4073.       buf->next = NULL;
  4074.       buf->c = 0;
  4075.       buflist->head = buf;
  4076.       buflist->tail = buf;
  4077.     }
  4078.  
  4079.   info->current = buflist;
  4080.   info->curbuf = buflist->tail;
  4081.  
  4082.   return TRUE;
  4083. }
  4084.  
  4085. /* Append a buffer chain.  */
  4086.  
  4087. static bfd_boolean
  4088. ieee_append_buffer (struct ieee_handle *info ATTRIBUTE_UNUSED,
  4089.                     struct ieee_buflist *mainbuf,
  4090.                     struct ieee_buflist *newbuf)
  4091. {
  4092.   if (newbuf->head != NULL)
  4093.     {
  4094.       if (mainbuf->head == NULL)
  4095.         mainbuf->head = newbuf->head;
  4096.       else
  4097.         mainbuf->tail->next = newbuf->head;
  4098.       mainbuf->tail = newbuf->tail;
  4099.     }
  4100.   return TRUE;
  4101. }
  4102.  
  4103. /* Write a byte into the buffer.  We use a macro for speed and a
  4104.    function for the complex cases.  */
  4105.  
  4106. #define ieee_write_byte(info, b)                                \
  4107.   ((info)->curbuf->c < IEEE_BUFSIZE                             \
  4108.    ? ((info)->curbuf->buf[(info)->curbuf->c++] = (b), TRUE)     \
  4109.    : ieee_real_write_byte ((info), (b)))
  4110.  
  4111. static bfd_boolean
  4112. ieee_real_write_byte (struct ieee_handle *info, int b)
  4113. {
  4114.   if (info->curbuf->c >= IEEE_BUFSIZE)
  4115.     {
  4116.       struct ieee_buf *n;
  4117.  
  4118.       n = (struct ieee_buf *) xmalloc (sizeof *n);
  4119.       n->next = NULL;
  4120.       n->c = 0;
  4121.       if (info->current->head == NULL)
  4122.         info->current->head = n;
  4123.       else
  4124.         info->current->tail->next = n;
  4125.       info->current->tail = n;
  4126.       info->curbuf = n;
  4127.     }
  4128.  
  4129.   info->curbuf->buf[info->curbuf->c] = b;
  4130.   ++info->curbuf->c;
  4131.  
  4132.   return TRUE;
  4133. }
  4134.  
  4135. /* Write out two bytes.  */
  4136.  
  4137. static bfd_boolean
  4138. ieee_write_2bytes (struct ieee_handle *info, int i)
  4139. {
  4140.   return (ieee_write_byte (info, i >> 8)
  4141.           && ieee_write_byte (info, i & 0xff));
  4142. }
  4143.  
  4144. /* Write out an integer.  */
  4145.  
  4146. static bfd_boolean
  4147. ieee_write_number (struct ieee_handle *info, bfd_vma v)
  4148. {
  4149.   bfd_vma t;
  4150.   bfd_byte ab[20];
  4151.   bfd_byte *p;
  4152.   unsigned int c;
  4153.  
  4154.   if (v <= (bfd_vma) ieee_number_end_enum)
  4155.     return ieee_write_byte (info, (int) v);
  4156.  
  4157.   t = v;
  4158.   p = ab + sizeof ab;
  4159.   while (t != 0)
  4160.     {
  4161.       *--p = t & 0xff;
  4162.       t >>= 8;
  4163.     }
  4164.   c = (ab + 20) - p;
  4165.  
  4166.   if (c > (unsigned int) (ieee_number_repeat_end_enum
  4167.                           - ieee_number_repeat_start_enum))
  4168.     {
  4169.       fprintf (stderr, _("IEEE numeric overflow: 0x"));
  4170.       fprintf_vma (stderr, v);
  4171.       fprintf (stderr, "\n");
  4172.       return FALSE;
  4173.     }
  4174.  
  4175.   if (! ieee_write_byte (info, (int) ieee_number_repeat_start_enum + c))
  4176.     return FALSE;
  4177.   for (; c > 0; --c, ++p)
  4178.     {
  4179.       if (! ieee_write_byte (info, *p))
  4180.         return FALSE;
  4181.     }
  4182.  
  4183.   return TRUE;
  4184. }
  4185.  
  4186. /* Write out a string.  */
  4187.  
  4188. static bfd_boolean
  4189. ieee_write_id (struct ieee_handle *info, const char *s)
  4190. {
  4191.   unsigned int len;
  4192.  
  4193.   len = strlen (s);
  4194.   if (len <= 0x7f)
  4195.     {
  4196.       if (! ieee_write_byte (info, len))
  4197.         return FALSE;
  4198.     }
  4199.   else if (len <= 0xff)
  4200.     {
  4201.       if (! ieee_write_byte (info, (int) ieee_extension_length_1_enum)
  4202.           || ! ieee_write_byte (info, len))
  4203.         return FALSE;
  4204.     }
  4205.   else if (len <= 0xffff)
  4206.     {
  4207.       if (! ieee_write_byte (info, (int) ieee_extension_length_2_enum)
  4208.           || ! ieee_write_2bytes (info, len))
  4209.         return FALSE;
  4210.     }
  4211.   else
  4212.     {
  4213.       fprintf (stderr, _("IEEE string length overflow: %u\n"), len);
  4214.       return FALSE;
  4215.     }
  4216.  
  4217.   for (; *s != '\0'; s++)
  4218.     if (! ieee_write_byte (info, *s))
  4219.       return FALSE;
  4220.  
  4221.   return TRUE;
  4222. }
  4223.  
  4224. /* Write out an ASN record.  */
  4225.  
  4226. static bfd_boolean
  4227. ieee_write_asn (struct ieee_handle *info, unsigned int indx, bfd_vma val)
  4228. {
  4229.   return (ieee_write_2bytes (info, (int) ieee_asn_record_enum)
  4230.           && ieee_write_number (info, indx)
  4231.           && ieee_write_number (info, val));
  4232. }
  4233.  
  4234. /* Write out an ATN65 record.  */
  4235.  
  4236. static bfd_boolean
  4237. ieee_write_atn65 (struct ieee_handle *info, unsigned int indx, const char *s)
  4238. {
  4239.   return (ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  4240.           && ieee_write_number (info, indx)
  4241.           && ieee_write_number (info, 0)
  4242.           && ieee_write_number (info, 65)
  4243.           && ieee_write_id (info, s));
  4244. }
  4245.  
  4246. /* Push a type index onto the type stack.  */
  4247.  
  4248. static bfd_boolean
  4249. ieee_push_type (struct ieee_handle *info, unsigned int indx,
  4250.                 unsigned int size, bfd_boolean unsignedp, bfd_boolean localp)
  4251. {
  4252.   struct ieee_type_stack *ts;
  4253.  
  4254.   ts = (struct ieee_type_stack *) xmalloc (sizeof *ts);
  4255.   memset (ts, 0, sizeof *ts);
  4256.  
  4257.   ts->type.indx = indx;
  4258.   ts->type.size = size;
  4259.   ts->type.unsignedp = unsignedp;
  4260.   ts->type.localp = localp;
  4261.  
  4262.   ts->next = info->type_stack;
  4263.   info->type_stack = ts;
  4264.  
  4265.   return TRUE;
  4266. }
  4267.  
  4268. /* Pop a type index off the type stack.  */
  4269.  
  4270. static unsigned int
  4271. ieee_pop_type (struct ieee_handle *info)
  4272. {
  4273.   return ieee_pop_type_used (info, TRUE);
  4274. }
  4275.  
  4276. /* Pop an unused type index off the type stack.  */
  4277.  
  4278. static void
  4279. ieee_pop_unused_type (struct ieee_handle *info)
  4280. {
  4281.   (void) ieee_pop_type_used (info, FALSE);
  4282. }
  4283.  
  4284. /* Pop a used or unused type index off the type stack.  */
  4285.  
  4286. static unsigned int
  4287. ieee_pop_type_used (struct ieee_handle *info, bfd_boolean used)
  4288. {
  4289.   struct ieee_type_stack *ts;
  4290.   unsigned int ret;
  4291.  
  4292.   ts = info->type_stack;
  4293.   assert (ts != NULL);
  4294.  
  4295.   /* If this is a function type, and we need it, we need to append the
  4296.      actual definition to the typedef block now.  */
  4297.   if (used && ! ieee_buffer_emptyp (&ts->type.fndef))
  4298.     {
  4299.       struct ieee_buflist *buflist;
  4300.  
  4301.       if (ts->type.localp)
  4302.         {
  4303.           /* Make sure we have started the types block.  */
  4304.           if (ieee_buffer_emptyp (&info->types))
  4305.             {
  4306.               if (! ieee_change_buffer (info, &info->types)
  4307.                   || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4308.                   || ! ieee_write_byte (info, 1)
  4309.                   || ! ieee_write_number (info, 0)
  4310.                   || ! ieee_write_id (info, info->modname))
  4311.                 return FALSE;
  4312.             }
  4313.           buflist = &info->types;
  4314.         }
  4315.       else
  4316.         {
  4317.           /* Make sure we started the global type block.  */
  4318.           if (ieee_buffer_emptyp (&info->global_types))
  4319.             {
  4320.               if (! ieee_change_buffer (info, &info->global_types)
  4321.                   || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4322.                   || ! ieee_write_byte (info, 2)
  4323.                   || ! ieee_write_number (info, 0)
  4324.                   || ! ieee_write_id (info, ""))
  4325.                 return FALSE;
  4326.             }
  4327.           buflist = &info->global_types;
  4328.         }
  4329.  
  4330.       if (! ieee_append_buffer (info, buflist, &ts->type.fndef))
  4331.         return FALSE;
  4332.     }
  4333.  
  4334.   ret = ts->type.indx;
  4335.   info->type_stack = ts->next;
  4336.   free (ts);
  4337.   return ret;
  4338. }
  4339.  
  4340. /* Add a range of bytes included in the current compilation unit.  */
  4341.  
  4342. static bfd_boolean
  4343. ieee_add_range (struct ieee_handle *info, bfd_boolean global, bfd_vma low,
  4344.                 bfd_vma high)
  4345. {
  4346.   struct ieee_range **plist, *r, **pr;
  4347.  
  4348.   if (low == (bfd_vma) -1 || high == (bfd_vma) -1 || low == high)
  4349.     return TRUE;
  4350.  
  4351.   if (global)
  4352.     plist = &info->global_ranges;
  4353.   else
  4354.     plist = &info->ranges;
  4355.  
  4356.   for (r = *plist; r != NULL; r = r->next)
  4357.     {
  4358.       if (high >= r->low && low <= r->high)
  4359.         {
  4360.           /* The new range overlaps r.  */
  4361.           if (low < r->low)
  4362.             r->low = low;
  4363.           if (high > r->high)
  4364.             r->high = high;
  4365.           pr = &r->next;
  4366.           while (*pr != NULL && (*pr)->low <= r->high)
  4367.             {
  4368.               struct ieee_range *n;
  4369.  
  4370.               if ((*pr)->high > r->high)
  4371.                 r->high = (*pr)->high;
  4372.               n = (*pr)->next;
  4373.               free (*pr);
  4374.               *pr = n;
  4375.             }
  4376.           return TRUE;
  4377.         }
  4378.     }
  4379.  
  4380.   r = (struct ieee_range *) xmalloc (sizeof *r);
  4381.   memset (r, 0, sizeof *r);
  4382.  
  4383.   r->low = low;
  4384.   r->high = high;
  4385.  
  4386.   /* Store the ranges sorted by address.  */
  4387.   for (pr = plist; *pr != NULL; pr = &(*pr)->next)
  4388.     if ((*pr)->low > high)
  4389.       break;
  4390.   r->next = *pr;
  4391.   *pr = r;
  4392.  
  4393.   return TRUE;
  4394. }
  4395.  
  4396. /* Start a new range for which we only have the low address.  */
  4397.  
  4398. static bfd_boolean
  4399. ieee_start_range (struct ieee_handle *info, bfd_vma low)
  4400. {
  4401.   struct ieee_range *r;
  4402.  
  4403.   r = (struct ieee_range *) xmalloc (sizeof *r);
  4404.   memset (r, 0, sizeof *r);
  4405.   r->low = low;
  4406.   r->next = info->pending_ranges;
  4407.   info->pending_ranges = r;
  4408.   return TRUE;
  4409. }
  4410.  
  4411. /* Finish a range started by ieee_start_range.  */
  4412.  
  4413. static bfd_boolean
  4414. ieee_end_range (struct ieee_handle *info, bfd_vma high)
  4415. {
  4416.   struct ieee_range *r;
  4417.   bfd_vma low;
  4418.  
  4419.   assert (info->pending_ranges != NULL);
  4420.   r = info->pending_ranges;
  4421.   low = r->low;
  4422.   info->pending_ranges = r->next;
  4423.   free (r);
  4424.   return ieee_add_range (info, FALSE, low, high);
  4425. }
  4426.  
  4427. /* Start defining a type.  */
  4428.  
  4429. static bfd_boolean
  4430. ieee_define_type (struct ieee_handle *info, unsigned int size,
  4431.                   bfd_boolean unsignedp, bfd_boolean localp)
  4432. {
  4433.   return ieee_define_named_type (info, (const char *) NULL,
  4434.                                  (unsigned int) -1, size, unsignedp,
  4435.                                  localp, (struct ieee_buflist *) NULL);
  4436. }
  4437.  
  4438. /* Start defining a named type.  */
  4439.  
  4440. static bfd_boolean
  4441. ieee_define_named_type (struct ieee_handle *info, const char *name,
  4442.                         unsigned int indx, unsigned int size,
  4443.                         bfd_boolean unsignedp, bfd_boolean localp,
  4444.                         struct ieee_buflist *buflist)
  4445. {
  4446.   unsigned int type_indx;
  4447.   unsigned int name_indx;
  4448.  
  4449.   if (indx != (unsigned int) -1)
  4450.     type_indx = indx;
  4451.   else
  4452.     {
  4453.       type_indx = info->type_indx;
  4454.       ++info->type_indx;
  4455.     }
  4456.  
  4457.   name_indx = info->name_indx;
  4458.   ++info->name_indx;
  4459.  
  4460.   if (name == NULL)
  4461.     name = "";
  4462.  
  4463.   /* If we were given a buffer, use it; otherwise, use either the
  4464.      local or the global type information, and make sure that the type
  4465.      block is started.  */
  4466.   if (buflist != NULL)
  4467.     {
  4468.       if (! ieee_change_buffer (info, buflist))
  4469.         return FALSE;
  4470.     }
  4471.   else if (localp)
  4472.     {
  4473.       if (! ieee_buffer_emptyp (&info->types))
  4474.         {
  4475.           if (! ieee_change_buffer (info, &info->types))
  4476.             return FALSE;
  4477.         }
  4478.       else
  4479.         {
  4480.           if (! ieee_change_buffer (info, &info->types)
  4481.               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4482.               || ! ieee_write_byte (info, 1)
  4483.               || ! ieee_write_number (info, 0)
  4484.               || ! ieee_write_id (info, info->modname))
  4485.             return FALSE;
  4486.         }
  4487.     }
  4488.   else
  4489.     {
  4490.       if (! ieee_buffer_emptyp (&info->global_types))
  4491.         {
  4492.           if (! ieee_change_buffer (info, &info->global_types))
  4493.             return FALSE;
  4494.         }
  4495.       else
  4496.         {
  4497.           if (! ieee_change_buffer (info, &info->global_types)
  4498.               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4499.               || ! ieee_write_byte (info, 2)
  4500.               || ! ieee_write_number (info, 0)
  4501.               || ! ieee_write_id (info, ""))
  4502.             return FALSE;
  4503.         }
  4504.     }
  4505.  
  4506.   /* Push the new type on the type stack, write out an NN record, and
  4507.      write out the start of a TY record.  The caller will then finish
  4508.      the TY record.  */
  4509.   if (! ieee_push_type (info, type_indx, size, unsignedp, localp))
  4510.     return FALSE;
  4511.  
  4512.   return (ieee_write_byte (info, (int) ieee_nn_record)
  4513.           && ieee_write_number (info, name_indx)
  4514.           && ieee_write_id (info, name)
  4515.           && ieee_write_byte (info, (int) ieee_ty_record_enum)
  4516.           && ieee_write_number (info, type_indx)
  4517.           && ieee_write_byte (info, 0xce)
  4518.           && ieee_write_number (info, name_indx));
  4519. }
  4520.  
  4521. /* Get an entry to the list of modified versions of a type.  */
  4522.  
  4523. static struct ieee_modified_type *
  4524. ieee_get_modified_info (struct ieee_handle *info, unsigned int indx)
  4525. {
  4526.   if (indx >= info->modified_alloc)
  4527.     {
  4528.       unsigned int nalloc;
  4529.  
  4530.       nalloc = info->modified_alloc;
  4531.       if (nalloc == 0)
  4532.         nalloc = 16;
  4533.       while (indx >= nalloc)
  4534.         nalloc *= 2;
  4535.       info->modified = ((struct ieee_modified_type *)
  4536.                         xrealloc (info->modified,
  4537.                                   nalloc * sizeof *info->modified));
  4538.       memset (info->modified + info->modified_alloc, 0,
  4539.               (nalloc - info->modified_alloc) * sizeof *info->modified);
  4540.       info->modified_alloc = nalloc;
  4541.     }
  4542.  
  4543.   return info->modified + indx;
  4544. }
  4545. /* Routines for the hash table mapping names to types.  */
  4546.  
  4547. /* Initialize an entry in the hash table.  */
  4548.  
  4549. static struct bfd_hash_entry *
  4550. ieee_name_type_newfunc (struct bfd_hash_entry *entry,
  4551.                         struct bfd_hash_table *table, const char *string)
  4552. {
  4553.   struct ieee_name_type_hash_entry *ret =
  4554.     (struct ieee_name_type_hash_entry *) entry;
  4555.  
  4556.   /* Allocate the structure if it has not already been allocated by a
  4557.      subclass.  */
  4558.   if (ret == NULL)
  4559.     ret = ((struct ieee_name_type_hash_entry *)
  4560.            bfd_hash_allocate (table, sizeof *ret));
  4561.   if (ret == NULL)
  4562.     return NULL;
  4563.  
  4564.   /* Call the allocation method of the superclass.  */
  4565.   ret = ((struct ieee_name_type_hash_entry *)
  4566.          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  4567.   if (ret)
  4568.     {
  4569.       /* Set local fields.  */
  4570.       ret->types = NULL;
  4571.     }
  4572.  
  4573.   return (struct bfd_hash_entry *) ret;
  4574. }
  4575.  
  4576. /* Look up an entry in the hash table.  */
  4577.  
  4578. #define ieee_name_type_hash_lookup(table, string, create, copy) \
  4579.   ((struct ieee_name_type_hash_entry *) \
  4580.    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  4581.  
  4582. /* Traverse the hash table.  */
  4583.  
  4584. #define ieee_name_type_hash_traverse(table, func, info)                 \
  4585.   (bfd_hash_traverse                                                    \
  4586.    (&(table)->root,                                                     \
  4587.     (bfd_boolean (*) (struct bfd_hash_entry *, void *)) (func),         \
  4588.     (info)))
  4589. /* The general routine to write out IEEE debugging information.  */
  4590.  
  4591. bfd_boolean
  4592. write_ieee_debugging_info (bfd *abfd, void *dhandle)
  4593. {
  4594.   struct ieee_handle info;
  4595.   asection *s;
  4596.   const char *err;
  4597.   struct ieee_buf *b;
  4598.  
  4599.   memset (&info, 0, sizeof info);
  4600.   info.abfd = abfd;
  4601.   info.type_indx = 256;
  4602.   info.name_indx = 32;
  4603.  
  4604.   if (!bfd_hash_table_init (&info.typedefs.root, ieee_name_type_newfunc,
  4605.                             sizeof (struct ieee_name_type_hash_entry))
  4606.       || !bfd_hash_table_init (&info.tags.root, ieee_name_type_newfunc,
  4607.                                sizeof (struct ieee_name_type_hash_entry)))
  4608.     return FALSE;
  4609.  
  4610.   if (! ieee_init_buffer (&info, &info.global_types)
  4611.       || ! ieee_init_buffer (&info, &info.data)
  4612.       || ! ieee_init_buffer (&info, &info.types)
  4613.       || ! ieee_init_buffer (&info, &info.vars)
  4614.       || ! ieee_init_buffer (&info, &info.cxx)
  4615.       || ! ieee_init_buffer (&info, &info.linenos)
  4616.       || ! ieee_init_buffer (&info, &info.fntype)
  4617.       || ! ieee_init_buffer (&info, &info.fnargs))
  4618.     return FALSE;
  4619.  
  4620.   if (! debug_write (dhandle, &ieee_fns, (void *) &info))
  4621.     return FALSE;
  4622.  
  4623.   if (info.filename != NULL)
  4624.     {
  4625.       if (! ieee_finish_compilation_unit (&info))
  4626.         return FALSE;
  4627.     }
  4628.  
  4629.   /* Put any undefined tags in the global typedef information.  */
  4630.   info.error = FALSE;
  4631.   ieee_name_type_hash_traverse (&info.tags,
  4632.                                 ieee_write_undefined_tag,
  4633.                                 (void *) &info);
  4634.   if (info.error)
  4635.     return FALSE;
  4636.  
  4637.   /* Prepend the global typedef information to the other data.  */
  4638.   if (! ieee_buffer_emptyp (&info.global_types))
  4639.     {
  4640.       /* The HP debugger seems to have a bug in which it ignores the
  4641.          last entry in the global types, so we add a dummy entry.  */
  4642.       if (! ieee_change_buffer (&info, &info.global_types)
  4643.           || ! ieee_write_byte (&info, (int) ieee_nn_record)
  4644.           || ! ieee_write_number (&info, info.name_indx)
  4645.           || ! ieee_write_id (&info, "")
  4646.           || ! ieee_write_byte (&info, (int) ieee_ty_record_enum)
  4647.           || ! ieee_write_number (&info, info.type_indx)
  4648.           || ! ieee_write_byte (&info, 0xce)
  4649.           || ! ieee_write_number (&info, info.name_indx)
  4650.           || ! ieee_write_number (&info, 'P')
  4651.           || ! ieee_write_number (&info, (int) builtin_void + 32)
  4652.           || ! ieee_write_byte (&info, (int) ieee_be_record_enum))
  4653.         return FALSE;
  4654.  
  4655.       if (! ieee_append_buffer (&info, &info.global_types, &info.data))
  4656.         return FALSE;
  4657.       info.data = info.global_types;
  4658.     }
  4659.  
  4660.   /* Make sure that we have declare BB11 blocks for each range in the
  4661.      file.  They are added to info->vars.  */
  4662.   info.error = FALSE;
  4663.   if (! ieee_init_buffer (&info, &info.vars))
  4664.     return FALSE;
  4665.   bfd_map_over_sections (abfd, ieee_add_bb11_blocks, (void *) &info);
  4666.   if (info.error)
  4667.     return FALSE;
  4668.   if (! ieee_buffer_emptyp (&info.vars))
  4669.     {
  4670.       if (! ieee_change_buffer (&info, &info.vars)
  4671.           || ! ieee_write_byte (&info, (int) ieee_be_record_enum))
  4672.         return FALSE;
  4673.  
  4674.       if (! ieee_append_buffer (&info, &info.data, &info.vars))
  4675.         return FALSE;
  4676.     }
  4677.  
  4678.   /* Now all the data is in info.data.  Write it out to the BFD.  We
  4679.      normally would need to worry about whether all the other sections
  4680.      are set up yet, but the IEEE backend will handle this particular
  4681.      case correctly regardless.  */
  4682.   if (ieee_buffer_emptyp (&info.data))
  4683.     {
  4684.       /* There is no debugging information.  */
  4685.       return TRUE;
  4686.     }
  4687.   err = NULL;
  4688.   s = bfd_make_section_with_flags (abfd, ".debug",
  4689.                                    SEC_DEBUGGING | SEC_HAS_CONTENTS);
  4690.   if (s == NULL)
  4691.     err = "bfd_make_section";
  4692.   if (err == NULL)
  4693.     {
  4694.       bfd_size_type size;
  4695.  
  4696.       size = 0;
  4697.       for (b = info.data.head; b != NULL; b = b->next)
  4698.         size += b->c;
  4699.       if (! bfd_set_section_size (abfd, s, size))
  4700.         err = "bfd_set_section_size";
  4701.     }
  4702.   if (err == NULL)
  4703.     {
  4704.       file_ptr offset;
  4705.  
  4706.       offset = 0;
  4707.       for (b = info.data.head; b != NULL; b = b->next)
  4708.         {
  4709.           if (! bfd_set_section_contents (abfd, s, b->buf, offset, b->c))
  4710.             {
  4711.               err = "bfd_set_section_contents";
  4712.               break;
  4713.             }
  4714.           offset += b->c;
  4715.         }
  4716.     }
  4717.  
  4718.   if (err != NULL)
  4719.     {
  4720.       fprintf (stderr, "%s: %s: %s\n", bfd_get_filename (abfd), err,
  4721.                bfd_errmsg (bfd_get_error ()));
  4722.       return FALSE;
  4723.     }
  4724.  
  4725.   bfd_hash_table_free (&info.typedefs.root);
  4726.   bfd_hash_table_free (&info.tags.root);
  4727.  
  4728.   return TRUE;
  4729. }
  4730.  
  4731. /* Write out information for an undefined tag.  This is called via
  4732.    ieee_name_type_hash_traverse.  */
  4733.  
  4734. static bfd_boolean
  4735. ieee_write_undefined_tag (struct ieee_name_type_hash_entry *h, void *p)
  4736. {
  4737.   struct ieee_handle *info = (struct ieee_handle *) p;
  4738.   struct ieee_name_type *nt;
  4739.  
  4740.   for (nt = h->types; nt != NULL; nt = nt->next)
  4741.     {
  4742.       unsigned int name_indx;
  4743.       char code;
  4744.  
  4745.       if (nt->kind == DEBUG_KIND_ILLEGAL)
  4746.         continue;
  4747.  
  4748.       if (ieee_buffer_emptyp (&info->global_types))
  4749.         {
  4750.           if (! ieee_change_buffer (info, &info->global_types)
  4751.               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4752.               || ! ieee_write_byte (info, 2)
  4753.               || ! ieee_write_number (info, 0)
  4754.               || ! ieee_write_id (info, ""))
  4755.             {
  4756.               info->error = TRUE;
  4757.               return FALSE;
  4758.             }
  4759.         }
  4760.       else
  4761.         {
  4762.           if (! ieee_change_buffer (info, &info->global_types))
  4763.             {
  4764.               info->error = TRUE;
  4765.               return FALSE;
  4766.             }
  4767.         }
  4768.  
  4769.       name_indx = info->name_indx;
  4770.       ++info->name_indx;
  4771.       if (! ieee_write_byte (info, (int) ieee_nn_record)
  4772.           || ! ieee_write_number (info, name_indx)
  4773.           || ! ieee_write_id (info, nt->type.name)
  4774.           || ! ieee_write_byte (info, (int) ieee_ty_record_enum)
  4775.           || ! ieee_write_number (info, nt->type.indx)
  4776.           || ! ieee_write_byte (info, 0xce)
  4777.           || ! ieee_write_number (info, name_indx))
  4778.         {
  4779.           info->error = TRUE;
  4780.           return FALSE;
  4781.         }
  4782.  
  4783.       switch (nt->kind)
  4784.         {
  4785.         default:
  4786.           abort ();
  4787.           info->error = TRUE;
  4788.           return FALSE;
  4789.         case DEBUG_KIND_STRUCT:
  4790.         case DEBUG_KIND_CLASS:
  4791.           code = 'S';
  4792.           break;
  4793.         case DEBUG_KIND_UNION:
  4794.         case DEBUG_KIND_UNION_CLASS:
  4795.           code = 'U';
  4796.           break;
  4797.         case DEBUG_KIND_ENUM:
  4798.           code = 'E';
  4799.           break;
  4800.         }
  4801.       if (! ieee_write_number (info, code)
  4802.           || ! ieee_write_number (info, 0))
  4803.         {
  4804.           info->error = TRUE;
  4805.           return FALSE;
  4806.         }
  4807.     }
  4808.  
  4809.   return TRUE;
  4810. }
  4811.  
  4812. /* Start writing out information for a compilation unit.  */
  4813.  
  4814. static bfd_boolean
  4815. ieee_start_compilation_unit (void *p, const char *filename)
  4816. {
  4817.   struct ieee_handle *info = (struct ieee_handle *) p;
  4818.   const char *modname;
  4819. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  4820.   const char *backslash;
  4821. #endif
  4822.   char *c, *s;
  4823.  
  4824.   if (info->filename != NULL)
  4825.     {
  4826.       if (! ieee_finish_compilation_unit (info))
  4827.         return FALSE;
  4828.     }
  4829.  
  4830.   info->filename = filename;
  4831.   modname = strrchr (filename, '/');
  4832. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  4833.   /* We could have a mixed forward/back slash case.  */
  4834.   backslash = strrchr (filename, '\\');
  4835.   if (modname == NULL || (backslash != NULL && backslash > modname))
  4836.     modname = backslash;
  4837. #endif
  4838.  
  4839.   if (modname != NULL)
  4840.     ++modname;
  4841. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  4842.   else if (filename[0] && filename[1] == ':')
  4843.     modname = filename + 2;
  4844. #endif
  4845.   else
  4846.     modname = filename;
  4847.  
  4848.   c = xstrdup (modname);
  4849.   s = strrchr (c, '.');
  4850.   if (s != NULL)
  4851.     *s = '\0';
  4852.   info->modname = c;
  4853.  
  4854.   if (! ieee_init_buffer (info, &info->types)
  4855.       || ! ieee_init_buffer (info, &info->vars)
  4856.       || ! ieee_init_buffer (info, &info->cxx)
  4857.       || ! ieee_init_buffer (info, &info->linenos))
  4858.     return FALSE;
  4859.   info->ranges = NULL;
  4860.  
  4861.   /* Always include a BB1 and a BB3 block.  That is what the output of
  4862.      the MRI linker seems to look like.  */
  4863.   if (! ieee_change_buffer (info, &info->types)
  4864.       || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4865.       || ! ieee_write_byte (info, 1)
  4866.       || ! ieee_write_number (info, 0)
  4867.       || ! ieee_write_id (info, info->modname))
  4868.     return FALSE;
  4869.  
  4870.   ++info->name_indx;
  4871.   if (! ieee_change_buffer (info, &info->vars)
  4872.       || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4873.       || ! ieee_write_byte (info, 3)
  4874.       || ! ieee_write_number (info, 0)
  4875.       || ! ieee_write_id (info, info->modname))
  4876.     return FALSE;
  4877.  
  4878.   return TRUE;
  4879. }
  4880.  
  4881. /* Finish up a compilation unit.  */
  4882.  
  4883. static bfd_boolean
  4884. ieee_finish_compilation_unit (struct ieee_handle *info)
  4885. {
  4886.   struct ieee_range *r;
  4887.  
  4888.   if (! ieee_buffer_emptyp (&info->types))
  4889.     {
  4890.       if (! ieee_change_buffer (info, &info->types)
  4891.           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
  4892.         return FALSE;
  4893.     }
  4894.  
  4895.   if (! ieee_buffer_emptyp (&info->cxx))
  4896.     {
  4897.       /* Append any C++ information to the global function and
  4898.          variable information.  */
  4899.       assert (! ieee_buffer_emptyp (&info->vars));
  4900.       if (! ieee_change_buffer (info, &info->vars))
  4901.         return FALSE;
  4902.  
  4903.       /* We put the pmisc records in a dummy procedure, just as the
  4904.          MRI compiler does.  */
  4905.       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4906.           || ! ieee_write_byte (info, 6)
  4907.           || ! ieee_write_number (info, 0)
  4908.           || ! ieee_write_id (info, "__XRYCPP")
  4909.           || ! ieee_write_number (info, 0)
  4910.           || ! ieee_write_number (info, 0)
  4911.           || ! ieee_write_number (info, info->highaddr - 1)
  4912.           || ! ieee_append_buffer (info, &info->vars, &info->cxx)
  4913.           || ! ieee_change_buffer (info, &info->vars)
  4914.           || ! ieee_write_byte (info, (int) ieee_be_record_enum)
  4915.           || ! ieee_write_number (info, info->highaddr - 1))
  4916.         return FALSE;
  4917.     }
  4918.  
  4919.   if (! ieee_buffer_emptyp (&info->vars))
  4920.     {
  4921.       if (! ieee_change_buffer (info, &info->vars)
  4922.           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
  4923.         return FALSE;
  4924.     }
  4925.  
  4926.   if (info->pending_lineno_filename != NULL)
  4927.     {
  4928.       /* Force out the pending line number.  */
  4929.       if (! ieee_lineno ((void *) info, (const char *) NULL, 0, (bfd_vma) -1))
  4930.         return FALSE;
  4931.     }
  4932.   if (! ieee_buffer_emptyp (&info->linenos))
  4933.     {
  4934.       if (! ieee_change_buffer (info, &info->linenos)
  4935.           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
  4936.         return FALSE;
  4937.       if (filename_cmp (info->filename, info->lineno_filename) != 0)
  4938.         {
  4939.           /* We were not in the main file.  We just closed the
  4940.              included line number block, and now we must close the
  4941.              main line number block.  */
  4942.           if (! ieee_write_byte (info, (int) ieee_be_record_enum))
  4943.             return FALSE;
  4944.         }
  4945.     }
  4946.  
  4947.   if (! ieee_append_buffer (info, &info->data, &info->types)
  4948.       || ! ieee_append_buffer (info, &info->data, &info->vars)
  4949.       || ! ieee_append_buffer (info, &info->data, &info->linenos))
  4950.     return FALSE;
  4951.  
  4952.   /* Build BB10/BB11 blocks based on the ranges we recorded.  */
  4953.   if (! ieee_change_buffer (info, &info->data))
  4954.     return FALSE;
  4955.  
  4956.   if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
  4957.       || ! ieee_write_byte (info, 10)
  4958.       || ! ieee_write_number (info, 0)
  4959.       || ! ieee_write_id (info, info->modname)
  4960.       || ! ieee_write_id (info, "")
  4961.       || ! ieee_write_number (info, 0)
  4962.       || ! ieee_write_id (info, "GNU objcopy"))
  4963.     return FALSE;
  4964.  
  4965.   for (r = info->ranges; r != NULL; r = r->next)
  4966.     {
  4967.       bfd_vma low, high;
  4968.       asection *s;
  4969.       int kind;
  4970.  
  4971.       low = r->low;
  4972.       high = r->high;
  4973.  
  4974.       /* Find the section corresponding to this range.  */
  4975.       for (s = info->abfd->sections; s != NULL; s = s->next)
  4976.         {
  4977.           if (bfd_get_section_vma (info->abfd, s) <= low
  4978.               && high <= (bfd_get_section_vma (info->abfd, s)
  4979.                           + bfd_section_size (info->abfd, s)))
  4980.             break;
  4981.         }
  4982.  
  4983.       if (s == NULL)
  4984.         {
  4985.           /* Just ignore this range.  */
  4986.           continue;
  4987.         }
  4988.  
  4989.       /* Coalesce ranges if it seems reasonable.  */
  4990.       while (r->next != NULL
  4991.              && high + 0x1000 >= r->next->low
  4992.              && (r->next->high
  4993.                  <= (bfd_get_section_vma (info->abfd, s)
  4994.                      + bfd_section_size (info->abfd, s))))
  4995.         {
  4996.           r = r->next;
  4997.           high = r->high;
  4998.         }
  4999.  
  5000.       if ((s->flags & SEC_CODE) != 0)
  5001.         kind = 1;
  5002.       else if ((s->flags & SEC_READONLY) != 0)
  5003.         kind = 3;
  5004.       else
  5005.         kind = 2;
  5006.  
  5007.       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
  5008.           || ! ieee_write_byte (info, 11)
  5009.           || ! ieee_write_number (info, 0)
  5010.           || ! ieee_write_id (info, "")
  5011.           || ! ieee_write_number (info, kind)
  5012.           || ! ieee_write_number (info, s->index + IEEE_SECTION_NUMBER_BASE)
  5013.           || ! ieee_write_number (info, low)
  5014.           || ! ieee_write_byte (info, (int) ieee_be_record_enum)
  5015.           || ! ieee_write_number (info, high - low))
  5016.         return FALSE;
  5017.  
  5018.       /* Add this range to the list of global ranges.  */
  5019.       if (! ieee_add_range (info, TRUE, low, high))
  5020.         return FALSE;
  5021.     }
  5022.  
  5023.   if (! ieee_write_byte (info, (int) ieee_be_record_enum))
  5024.     return FALSE;
  5025.  
  5026.   return TRUE;
  5027. }
  5028.  
  5029. /* Add BB11 blocks describing each range that we have not already
  5030.    described.  */
  5031.  
  5032. static void
  5033. ieee_add_bb11_blocks (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *data)
  5034. {
  5035.   struct ieee_handle *info = (struct ieee_handle *) data;
  5036.   bfd_vma low, high;
  5037.   struct ieee_range *r;
  5038.  
  5039.   low = bfd_get_section_vma (abfd, sec);
  5040.   high = low + bfd_section_size (abfd, sec);
  5041.  
  5042.   /* Find the first range at or after this section.  The ranges are
  5043.      sorted by address.  */
  5044.   for (r = info->global_ranges; r != NULL; r = r->next)
  5045.     if (r->high > low)
  5046.       break;
  5047.  
  5048.   while (low < high)
  5049.     {
  5050.       if (r == NULL || r->low >= high)
  5051.         {
  5052.           if (! ieee_add_bb11 (info, sec, low, high))
  5053.             info->error = TRUE;
  5054.           return;
  5055.         }
  5056.  
  5057.       if (low < r->low
  5058.           && r->low - low > 0x100)
  5059.         {
  5060.           if (! ieee_add_bb11 (info, sec, low, r->low))
  5061.             {
  5062.               info->error = TRUE;
  5063.               return;
  5064.             }
  5065.         }
  5066.       low = r->high;
  5067.  
  5068.       r = r->next;
  5069.     }
  5070. }
  5071.  
  5072. /* Add a single BB11 block for a range.  We add it to info->vars.  */
  5073.  
  5074. static bfd_boolean
  5075. ieee_add_bb11 (struct ieee_handle *info, asection *sec, bfd_vma low,
  5076.                bfd_vma high)
  5077. {
  5078.   int kind;
  5079.  
  5080.   if (! ieee_buffer_emptyp (&info->vars))
  5081.     {
  5082.       if (! ieee_change_buffer (info, &info->vars))
  5083.         return FALSE;
  5084.     }
  5085.   else
  5086.     {
  5087.       const char *filename, *modname;
  5088. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  5089.       const char *backslash;
  5090. #endif
  5091.       char *c, *s;
  5092.  
  5093.       /* Start the enclosing BB10 block.  */
  5094.       filename = bfd_get_filename (info->abfd);
  5095.       modname = strrchr (filename, '/');
  5096. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  5097.       backslash = strrchr (filename, '\\');
  5098.       if (modname == NULL || (backslash != NULL && backslash > modname))
  5099.         modname = backslash;
  5100. #endif
  5101.  
  5102.       if (modname != NULL)
  5103.         ++modname;
  5104. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  5105.       else if (filename[0] && filename[1] == ':')
  5106.         modname = filename + 2;
  5107. #endif
  5108.       else
  5109.         modname = filename;
  5110.  
  5111.       c = xstrdup (modname);
  5112.       s = strrchr (c, '.');
  5113.       if (s != NULL)
  5114.         *s = '\0';
  5115.  
  5116.       if (! ieee_change_buffer (info, &info->vars)
  5117.           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  5118.           || ! ieee_write_byte (info, 10)
  5119.           || ! ieee_write_number (info, 0)
  5120.           || ! ieee_write_id (info, c)
  5121.           || ! ieee_write_id (info, "")
  5122.           || ! ieee_write_number (info, 0)
  5123.           || ! ieee_write_id (info, "GNU objcopy"))
  5124.         {
  5125.           free (c);
  5126.           return FALSE;
  5127.         }
  5128.  
  5129.       free (c);
  5130.     }
  5131.  
  5132.   if ((sec->flags & SEC_CODE) != 0)
  5133.     kind = 1;
  5134.   else if ((sec->flags & SEC_READONLY) != 0)
  5135.     kind = 3;
  5136.   else
  5137.     kind = 2;
  5138.  
  5139.   if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
  5140.       || ! ieee_write_byte (info, 11)
  5141.       || ! ieee_write_number (info, 0)
  5142.       || ! ieee_write_id (info, "")
  5143.       || ! ieee_write_number (info, kind)
  5144.       || ! ieee_write_number (info, sec->index + IEEE_SECTION_NUMBER_BASE)
  5145.       || ! ieee_write_number (info, low)
  5146.       || ! ieee_write_byte (info, (int) ieee_be_record_enum)
  5147.       || ! ieee_write_number (info, high - low))
  5148.     return FALSE;
  5149.  
  5150.   return TRUE;
  5151. }
  5152.  
  5153. /* Start recording information from a particular source file.  This is
  5154.    used to record which file defined which types, variables, etc.  It
  5155.    is not used for line numbers, since the lineno entry point passes
  5156.    down the file name anyhow.  IEEE debugging information doesn't seem
  5157.    to store this information anywhere.  */
  5158.  
  5159. static bfd_boolean
  5160. ieee_start_source (void *p ATTRIBUTE_UNUSED,
  5161.                    const char *filename ATTRIBUTE_UNUSED)
  5162. {
  5163.   return TRUE;
  5164. }
  5165.  
  5166. /* Make an empty type.  */
  5167.  
  5168. static bfd_boolean
  5169. ieee_empty_type (void *p)
  5170. {
  5171.   struct ieee_handle *info = (struct ieee_handle *) p;
  5172.  
  5173.   return ieee_push_type (info, (int) builtin_unknown, 0, FALSE, FALSE);
  5174. }
  5175.  
  5176. /* Make a void type.  */
  5177.  
  5178. static bfd_boolean
  5179. ieee_void_type (void *p)
  5180. {
  5181.   struct ieee_handle *info = (struct ieee_handle *) p;
  5182.  
  5183.   return ieee_push_type (info, (int) builtin_void, 0, FALSE, FALSE);
  5184. }
  5185.  
  5186. /* Make an integer type.  */
  5187.  
  5188. static bfd_boolean
  5189. ieee_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
  5190. {
  5191.   struct ieee_handle *info = (struct ieee_handle *) p;
  5192.   unsigned int indx;
  5193.  
  5194.   switch (size)
  5195.     {
  5196.     case 1:
  5197.       indx = (int) builtin_signed_char;
  5198.       break;
  5199.     case 2:
  5200.       indx = (int) builtin_signed_short_int;
  5201.       break;
  5202.     case 4:
  5203.       indx = (int) builtin_signed_long;
  5204.       break;
  5205.     case 8:
  5206.       indx = (int) builtin_signed_long_long;
  5207.       break;
  5208.     default:
  5209.       fprintf (stderr, _("IEEE unsupported integer type size %u\n"), size);
  5210.       return FALSE;
  5211.     }
  5212.  
  5213.   if (unsignedp)
  5214.     ++indx;
  5215.  
  5216.   return ieee_push_type (info, indx, size, unsignedp, FALSE);
  5217. }
  5218.  
  5219. /* Make a floating point type.  */
  5220.  
  5221. static bfd_boolean
  5222. ieee_float_type (void *p, unsigned int size)
  5223. {
  5224.   struct ieee_handle *info = (struct ieee_handle *) p;
  5225.   unsigned int indx;
  5226.  
  5227.   switch (size)
  5228.     {
  5229.     case 4:
  5230.       indx = (int) builtin_float;
  5231.       break;
  5232.     case 8:
  5233.       indx = (int) builtin_double;
  5234.       break;
  5235.     case 12:
  5236.       /* FIXME: This size really depends upon the processor.  */
  5237.       indx = (int) builtin_long_double;
  5238.       break;
  5239.     case 16:
  5240.       indx = (int) builtin_long_long_double;
  5241.       break;
  5242.     default:
  5243.       fprintf (stderr, _("IEEE unsupported float type size %u\n"), size);
  5244.       return FALSE;
  5245.     }
  5246.  
  5247.   return ieee_push_type (info, indx, size, FALSE, FALSE);
  5248. }
  5249.  
  5250. /* Make a complex type.  */
  5251.  
  5252. static bfd_boolean
  5253. ieee_complex_type (void *p, unsigned int size)
  5254. {
  5255.   struct ieee_handle *info = (struct ieee_handle *) p;
  5256.   char code;
  5257.  
  5258.   switch (size)
  5259.     {
  5260.     case 4:
  5261.       if (info->complex_float_index != 0)
  5262.         return ieee_push_type (info, info->complex_float_index, size * 2,
  5263.                                FALSE, FALSE);
  5264.       code = 'c';
  5265.       break;
  5266.     case 12:
  5267.     case 16:
  5268.       /* These cases can be output by gcc -gstabs.  Outputting the
  5269.          wrong type is better than crashing.  */
  5270.     case 8:
  5271.       if (info->complex_double_index != 0)
  5272.         return ieee_push_type (info, info->complex_double_index, size * 2,
  5273.                                FALSE, FALSE);
  5274.       code = 'd';
  5275.       break;
  5276.     default:
  5277.       fprintf (stderr, _("IEEE unsupported complex type size %u\n"), size);
  5278.       return FALSE;
  5279.     }
  5280.  
  5281.   /* FIXME: I don't know what the string is for.  */
  5282.   if (! ieee_define_type (info, size * 2, FALSE, FALSE)
  5283.       || ! ieee_write_number (info, code)
  5284.       || ! ieee_write_id (info, ""))
  5285.     return FALSE;
  5286.  
  5287.   if (size == 4)
  5288.     info->complex_float_index = info->type_stack->type.indx;
  5289.   else
  5290.     info->complex_double_index = info->type_stack->type.indx;
  5291.  
  5292.   return TRUE;
  5293. }
  5294.  
  5295. /* Make a boolean type.  IEEE doesn't support these, so we just make
  5296.    an integer type instead.  */
  5297.  
  5298. static bfd_boolean
  5299. ieee_bool_type (void *p, unsigned int size)
  5300. {
  5301.   return ieee_int_type (p, size, TRUE);
  5302. }
  5303.  
  5304. /* Make an enumeration.  */
  5305.  
  5306. static bfd_boolean
  5307. ieee_enum_type (void *p, const char *tag, const char **names,
  5308.                 bfd_signed_vma *vals)
  5309. {
  5310.   struct ieee_handle *info = (struct ieee_handle *) p;
  5311.   struct ieee_defined_enum *e;
  5312.   bfd_boolean localp, simple;
  5313.   unsigned int indx;
  5314.   int i = 0;
  5315.  
  5316.   localp = FALSE;
  5317.   indx = (unsigned int) -1;
  5318.   for (e = info->enums; e != NULL; e = e->next)
  5319.     {
  5320.       if (tag == NULL)
  5321.         {
  5322.           if (e->tag != NULL)
  5323.             continue;
  5324.         }
  5325.       else
  5326.         {
  5327.           if (e->tag == NULL
  5328.               || tag[0] != e->tag[0]
  5329.               || strcmp (tag, e->tag) != 0)
  5330.             continue;
  5331.         }
  5332.  
  5333.       if (! e->defined)
  5334.         {
  5335.           /* This enum tag has been seen but not defined.  */
  5336.           indx = e->indx;
  5337.           break;
  5338.         }
  5339.  
  5340.       if (names != NULL && e->names != NULL)
  5341.         {
  5342.           for (i = 0; names[i] != NULL && e->names[i] != NULL; i++)
  5343.             {
  5344.               if (names[i][0] != e->names[i][0]
  5345.                   || vals[i] != e->vals[i]
  5346.                   || strcmp (names[i], e->names[i]) != 0)
  5347.                 break;
  5348.             }
  5349.         }
  5350.  
  5351.       if ((names == NULL && e->names == NULL)
  5352.           || (names != NULL
  5353.               && e->names != NULL
  5354.               && names[i] == NULL
  5355.               && e->names[i] == NULL))
  5356.         {
  5357.           /* We've seen this enum before.  */
  5358.           return ieee_push_type (info, e->indx, 0, TRUE, FALSE);
  5359.         }
  5360.  
  5361.       if (tag != NULL)
  5362.         {
  5363.           /* We've already seen an enum of the same name, so we must make
  5364.              sure to output this one locally.  */
  5365.           localp = TRUE;
  5366.           break;
  5367.         }
  5368.     }
  5369.  
  5370.   /* If this is a simple enumeration, in which the values start at 0
  5371.      and always increment by 1, we can use type E.  Otherwise we must
  5372.      use type N.  */
  5373.  
  5374.   simple = TRUE;
  5375.   if (names != NULL)
  5376.     {
  5377.       for (i = 0; names[i] != NULL; i++)
  5378.         {
  5379.           if (vals[i] != i)
  5380.             {
  5381.               simple = FALSE;
  5382.               break;
  5383.             }
  5384.         }
  5385.     }
  5386.  
  5387.   if (! ieee_define_named_type (info, tag, indx, 0, TRUE, localp,
  5388.                                 (struct ieee_buflist *) NULL)
  5389.       || ! ieee_write_number (info, simple ? 'E' : 'N'))
  5390.     return FALSE;
  5391.   if (simple)
  5392.     {
  5393.       /* FIXME: This is supposed to be the enumeration size, but we
  5394.          don't store that.  */
  5395.       if (! ieee_write_number (info, 4))
  5396.         return FALSE;
  5397.     }
  5398.   if (names != NULL)
  5399.     {
  5400.       for (i = 0; names[i] != NULL; i++)
  5401.         {
  5402.           if (! ieee_write_id (info, names[i]))
  5403.             return FALSE;
  5404.           if (! simple)
  5405.             {
  5406.               if (! ieee_write_number (info, vals[i]))
  5407.                 return FALSE;
  5408.             }
  5409.         }
  5410.     }
  5411.  
  5412.   if (! localp)
  5413.     {
  5414.       if (indx == (unsigned int) -1)
  5415.         {
  5416.           e = (struct ieee_defined_enum *) xmalloc (sizeof *e);
  5417.           memset (e, 0, sizeof *e);
  5418.           e->indx = info->type_stack->type.indx;
  5419.           e->tag = tag;
  5420.  
  5421.           e->next = info->enums;
  5422.           info->enums = e;
  5423.         }
  5424.  
  5425.       e->names = names;
  5426.       e->vals = vals;
  5427.       e->defined = TRUE;
  5428.     }
  5429.  
  5430.   return TRUE;
  5431. }
  5432.  
  5433. /* Make a pointer type.  */
  5434.  
  5435. static bfd_boolean
  5436. ieee_pointer_type (void *p)
  5437. {
  5438.   struct ieee_handle *info = (struct ieee_handle *) p;
  5439.   bfd_boolean localp;
  5440.   unsigned int indx;
  5441.   struct ieee_modified_type *m = NULL;
  5442.  
  5443.   localp = info->type_stack->type.localp;
  5444.   indx = ieee_pop_type (info);
  5445.  
  5446.   /* A pointer to a simple builtin type can be obtained by adding 32.
  5447.      FIXME: Will this be a short pointer, and will that matter?  */
  5448.   if (indx < 32)
  5449.     return ieee_push_type (info, indx + 32, 0, TRUE, FALSE);
  5450.  
  5451.   if (! localp)
  5452.     {
  5453.       m = ieee_get_modified_info ((struct ieee_handle *) p, indx);
  5454.       if (m == NULL)
  5455.         return FALSE;
  5456.  
  5457.       /* FIXME: The size should depend upon the architecture.  */
  5458.       if (m->pointer > 0)
  5459.         return ieee_push_type (info, m->pointer, 4, TRUE, FALSE);
  5460.     }
  5461.  
  5462.   if (! ieee_define_type (info, 4, TRUE, localp)
  5463.       || ! ieee_write_number (info, 'P')
  5464.       || ! ieee_write_number (info, indx))
  5465.     return FALSE;
  5466.  
  5467.   if (! localp)
  5468.     m->pointer = info->type_stack->type.indx;
  5469.  
  5470.   return TRUE;
  5471. }
  5472.  
  5473. /* Make a function type.  This will be called for a method, but we
  5474.    don't want to actually add it to the type table in that case.  We
  5475.    handle this by defining the type in a private buffer, and only
  5476.    adding that buffer to the typedef block if we are going to use it.  */
  5477.  
  5478. static bfd_boolean
  5479. ieee_function_type (void *p, int argcount, bfd_boolean varargs)
  5480. {
  5481.   struct ieee_handle *info = (struct ieee_handle *) p;
  5482.   bfd_boolean localp;
  5483.   unsigned int *args = NULL;
  5484.   int i;
  5485.   unsigned int retindx;
  5486.   struct ieee_buflist fndef;
  5487.   struct ieee_modified_type *m;
  5488.  
  5489.   localp = FALSE;
  5490.  
  5491.   if (argcount > 0)
  5492.     {
  5493.       args = (unsigned int *) xmalloc (argcount * sizeof *args);
  5494.       for (i = argcount - 1; i >= 0; i--)
  5495.         {
  5496.           if (info->type_stack->type.localp)
  5497.             localp = TRUE;
  5498.           args[i] = ieee_pop_type (info);
  5499.         }
  5500.     }
  5501.   else if (argcount < 0)
  5502.     varargs = FALSE;
  5503.  
  5504.   if (info->type_stack->type.localp)
  5505.     localp = TRUE;
  5506.   retindx = ieee_pop_type (info);
  5507.  
  5508.   m = NULL;
  5509.   if (argcount < 0 && ! localp)
  5510.     {
  5511.       m = ieee_get_modified_info ((struct ieee_handle *) p, retindx);
  5512.       if (m == NULL)
  5513.         return FALSE;
  5514.  
  5515.       if (m->function > 0)
  5516.         return ieee_push_type (info, m->function, 0, TRUE, FALSE);
  5517.     }
  5518.  
  5519.   /* An attribute of 0x41 means that the frame and push mask are
  5520.      unknown.  */
  5521.   if (! ieee_init_buffer (info, &fndef)
  5522.       || ! ieee_define_named_type (info, (const char *) NULL,
  5523.                                    (unsigned int) -1, 0, TRUE, localp,
  5524.                                    &fndef)
  5525.       || ! ieee_write_number (info, 'x')
  5526.       || ! ieee_write_number (info, 0x41)
  5527.       || ! ieee_write_number (info, 0)
  5528.       || ! ieee_write_number (info, 0)
  5529.       || ! ieee_write_number (info, retindx)
  5530.       || ! ieee_write_number (info, (bfd_vma) argcount + (varargs ? 1 : 0)))
  5531.     {
  5532.       free (args);
  5533.       return FALSE;
  5534.     }
  5535.   if (argcount > 0)
  5536.     {
  5537.       for (i = 0; i < argcount; i++)
  5538.         if (! ieee_write_number (info, args[i]))
  5539.           return FALSE;
  5540.       free (args);
  5541.     }
  5542.   if (varargs)
  5543.     {
  5544.       /* A varargs function is represented by writing out the last
  5545.          argument as type void *, although this makes little sense.  */
  5546.       if (! ieee_write_number (info, (bfd_vma) builtin_void + 32))
  5547.         return FALSE;
  5548.     }
  5549.  
  5550.   if (! ieee_write_number (info, 0))
  5551.     return FALSE;
  5552.  
  5553.   /* We wrote the information into fndef, in case we don't need it.
  5554.      It will be appended to info->types by ieee_pop_type.  */
  5555.   info->type_stack->type.fndef = fndef;
  5556.  
  5557.   if (m != NULL)
  5558.     m->function = info->type_stack->type.indx;
  5559.  
  5560.   return TRUE;
  5561. }
  5562.  
  5563. /* Make a reference type.  */
  5564.  
  5565. static bfd_boolean
  5566. ieee_reference_type (void *p)
  5567. {
  5568.   struct ieee_handle *info = (struct ieee_handle *) p;
  5569.  
  5570.   /* IEEE appears to record a normal pointer type, and then use a
  5571.      pmisc record to indicate that it is really a reference.  */
  5572.  
  5573.   if (! ieee_pointer_type (p))
  5574.     return FALSE;
  5575.   info->type_stack->type.referencep = TRUE;
  5576.   return TRUE;
  5577. }
  5578.  
  5579. /* Make a range type.  */
  5580.  
  5581. static bfd_boolean
  5582. ieee_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
  5583. {
  5584.   struct ieee_handle *info = (struct ieee_handle *) p;
  5585.   unsigned int size;
  5586.   bfd_boolean unsignedp, localp;
  5587.  
  5588.   size = info->type_stack->type.size;
  5589.   unsignedp = info->type_stack->type.unsignedp;
  5590.   localp = info->type_stack->type.localp;
  5591.   ieee_pop_unused_type (info);
  5592.   return (ieee_define_type (info, size, unsignedp, localp)
  5593.           && ieee_write_number (info, 'R')
  5594.           && ieee_write_number (info, (bfd_vma) low)
  5595.           && ieee_write_number (info, (bfd_vma) high)
  5596.           && ieee_write_number (info, unsignedp ? 0 : 1)
  5597.           && ieee_write_number (info, size));
  5598. }
  5599.  
  5600. /* Make an array type.  */
  5601.  
  5602. static bfd_boolean
  5603. ieee_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
  5604.                  bfd_boolean stringp ATTRIBUTE_UNUSED)
  5605. {
  5606.   struct ieee_handle *info = (struct ieee_handle *) p;
  5607.   unsigned int eleindx;
  5608.   bfd_boolean localp;
  5609.   unsigned int size;
  5610.   struct ieee_modified_type *m = NULL;
  5611.   struct ieee_modified_array_type *a;
  5612.  
  5613.   /* IEEE does not store the range, so we just ignore it.  */
  5614.   ieee_pop_unused_type (info);
  5615.   localp = info->type_stack->type.localp;
  5616.   size = info->type_stack->type.size;
  5617.   eleindx = ieee_pop_type (info);
  5618.  
  5619.   /* If we don't know the range, treat the size as exactly one
  5620.      element.  */
  5621.   if (low < high)
  5622.     size *= (high - low) + 1;
  5623.  
  5624.   if (! localp)
  5625.     {
  5626.       m = ieee_get_modified_info (info, eleindx);
  5627.       if (m == NULL)
  5628.         return FALSE;
  5629.  
  5630.       for (a = m->arrays; a != NULL; a = a->next)
  5631.         {
  5632.           if (a->low == low && a->high == high)
  5633.             return ieee_push_type (info, a->indx, size, FALSE, FALSE);
  5634.         }
  5635.     }
  5636.  
  5637.   if (! ieee_define_type (info, size, FALSE, localp)
  5638.       || ! ieee_write_number (info, low == 0 ? 'Z' : 'C')
  5639.       || ! ieee_write_number (info, eleindx))
  5640.     return FALSE;
  5641.   if (low != 0)
  5642.     {
  5643.       if (! ieee_write_number (info, low))
  5644.         return FALSE;
  5645.     }
  5646.  
  5647.   if (! ieee_write_number (info, high + 1))
  5648.     return FALSE;
  5649.  
  5650.   if (! localp)
  5651.     {
  5652.       a = (struct ieee_modified_array_type *) xmalloc (sizeof *a);
  5653.       memset (a, 0, sizeof *a);
  5654.  
  5655.       a->indx = info->type_stack->type.indx;
  5656.       a->low = low;
  5657.       a->high = high;
  5658.  
  5659.       a->next = m->arrays;
  5660.       m->arrays = a;
  5661.     }
  5662.  
  5663.   return TRUE;
  5664. }
  5665.  
  5666. /* Make a set type.  */
  5667.  
  5668. static bfd_boolean
  5669. ieee_set_type (void *p, bfd_boolean bitstringp ATTRIBUTE_UNUSED)
  5670. {
  5671.   struct ieee_handle *info = (struct ieee_handle *) p;
  5672.   bfd_boolean localp;
  5673.   unsigned int eleindx;
  5674.  
  5675.   localp = info->type_stack->type.localp;
  5676.   eleindx = ieee_pop_type (info);
  5677.  
  5678.   /* FIXME: We don't know the size, so we just use 4.  */
  5679.  
  5680.   return (ieee_define_type (info, 0, TRUE, localp)
  5681.           && ieee_write_number (info, 's')
  5682.           && ieee_write_number (info, 4)
  5683.           && ieee_write_number (info, eleindx));
  5684. }
  5685.  
  5686. /* Make an offset type.  */
  5687.  
  5688. static bfd_boolean
  5689. ieee_offset_type (void *p)
  5690. {
  5691.   /* FIXME: The MRI C++ compiler does not appear to generate any
  5692.      useful type information about an offset type.  It just records a
  5693.      pointer to member as an integer.  The MRI/HP IEEE spec does
  5694.      describe a pmisc record which can be used for a pointer to
  5695.      member.  Unfortunately, it does not describe the target type,
  5696.      which seems pretty important.  I'm going to punt this for now.  */
  5697.  
  5698.   return ieee_int_type (p, 4, TRUE);
  5699. }
  5700.  
  5701. /* Make a method type.  */
  5702.  
  5703. static bfd_boolean
  5704. ieee_method_type (void *p, bfd_boolean domain, int argcount,
  5705.                   bfd_boolean varargs)
  5706. {
  5707.   struct ieee_handle *info = (struct ieee_handle *) p;
  5708.  
  5709.   /* FIXME: The MRI/HP IEEE spec defines a pmisc record to use for a
  5710.      method, but the definition is incomplete.  We just output an 'x'
  5711.      type.  */
  5712.  
  5713.   if (domain)
  5714.     ieee_pop_unused_type (info);
  5715.  
  5716.   return ieee_function_type (p, argcount, varargs);
  5717. }
  5718.  
  5719. /* Make a const qualified type.  */
  5720.  
  5721. static bfd_boolean
  5722. ieee_const_type (void *p)
  5723. {
  5724.   struct ieee_handle *info = (struct ieee_handle *) p;
  5725.   unsigned int size;
  5726.   bfd_boolean unsignedp, localp;
  5727.   unsigned int indx;
  5728.   struct ieee_modified_type *m = NULL;
  5729.  
  5730.   size = info->type_stack->type.size;
  5731.   unsignedp = info->type_stack->type.unsignedp;
  5732.   localp = info->type_stack->type.localp;
  5733.   indx = ieee_pop_type (info);
  5734.  
  5735.   if (! localp)
  5736.     {
  5737.       m = ieee_get_modified_info (info, indx);
  5738.       if (m == NULL)
  5739.         return FALSE;
  5740.  
  5741.       if (m->const_qualified > 0)
  5742.         return ieee_push_type (info, m->const_qualified, size, unsignedp,
  5743.                                FALSE);
  5744.     }
  5745.  
  5746.   if (! ieee_define_type (info, size, unsignedp, localp)
  5747.       || ! ieee_write_number (info, 'n')
  5748.       || ! ieee_write_number (info, 1)
  5749.       || ! ieee_write_number (info, indx))
  5750.     return FALSE;
  5751.  
  5752.   if (! localp)
  5753.     m->const_qualified = info->type_stack->type.indx;
  5754.  
  5755.   return TRUE;
  5756. }
  5757.  
  5758. /* Make a volatile qualified type.  */
  5759.  
  5760. static bfd_boolean
  5761. ieee_volatile_type (void *p)
  5762. {
  5763.   struct ieee_handle *info = (struct ieee_handle *) p;
  5764.   unsigned int size;
  5765.   bfd_boolean unsignedp, localp;
  5766.   unsigned int indx;
  5767.   struct ieee_modified_type *m = NULL;
  5768.  
  5769.   size = info->type_stack->type.size;
  5770.   unsignedp = info->type_stack->type.unsignedp;
  5771.   localp = info->type_stack->type.localp;
  5772.   indx = ieee_pop_type (info);
  5773.  
  5774.   if (! localp)
  5775.     {
  5776.       m = ieee_get_modified_info (info, indx);
  5777.       if (m == NULL)
  5778.         return FALSE;
  5779.  
  5780.       if (m->volatile_qualified > 0)
  5781.         return ieee_push_type (info, m->volatile_qualified, size, unsignedp,
  5782.                                FALSE);
  5783.     }
  5784.  
  5785.   if (! ieee_define_type (info, size, unsignedp, localp)
  5786.       || ! ieee_write_number (info, 'n')
  5787.       || ! ieee_write_number (info, 2)
  5788.       || ! ieee_write_number (info, indx))
  5789.     return FALSE;
  5790.  
  5791.   if (! localp)
  5792.     m->volatile_qualified = info->type_stack->type.indx;
  5793.  
  5794.   return TRUE;
  5795. }
  5796.  
  5797. /* Convert an enum debug_visibility into a CXXFLAGS value.  */
  5798.  
  5799. static unsigned int
  5800. ieee_vis_to_flags (enum debug_visibility visibility)
  5801. {
  5802.   switch (visibility)
  5803.     {
  5804.     default:
  5805.       abort ();
  5806.     case DEBUG_VISIBILITY_PUBLIC:
  5807.       return CXXFLAGS_VISIBILITY_PUBLIC;
  5808.     case DEBUG_VISIBILITY_PRIVATE:
  5809.       return CXXFLAGS_VISIBILITY_PRIVATE;
  5810.     case DEBUG_VISIBILITY_PROTECTED:
  5811.       return CXXFLAGS_VISIBILITY_PROTECTED;
  5812.     }
  5813.   /*NOTREACHED*/
  5814. }
  5815.  
  5816. /* Start defining a struct type.  We build it in the strdef field on
  5817.    the stack, to avoid confusing type definitions required by the
  5818.    fields with the struct type itself.  */
  5819.  
  5820. static bfd_boolean
  5821. ieee_start_struct_type (void *p, const char *tag, unsigned int id,
  5822.                         bfd_boolean structp, unsigned int size)
  5823. {
  5824.   struct ieee_handle *info = (struct ieee_handle *) p;
  5825.   bfd_boolean localp, ignorep;
  5826.   bfd_boolean copy;
  5827.   char ab[20];
  5828.   const char *look;
  5829.   struct ieee_name_type_hash_entry *h;
  5830.   struct ieee_name_type *nt, *ntlook;
  5831.   struct ieee_buflist strdef;
  5832.  
  5833.   localp = FALSE;
  5834.   ignorep = FALSE;
  5835.  
  5836.   /* We need to create a tag for internal use even if we don't want
  5837.      one for external use.  This will let us refer to an anonymous
  5838.      struct.  */
  5839.   if (tag != NULL)
  5840.     {
  5841.       look = tag;
  5842.       copy = FALSE;
  5843.     }
  5844.   else
  5845.     {
  5846.       sprintf (ab, "__anon%u", id);
  5847.       look = ab;
  5848.       copy = TRUE;
  5849.     }
  5850.  
  5851.   /* If we already have references to the tag, we must use the
  5852.      existing type index.  */
  5853.   h = ieee_name_type_hash_lookup (&info->tags, look, TRUE, copy);
  5854.   if (h == NULL)
  5855.     return FALSE;
  5856.  
  5857.   nt = NULL;
  5858.   for (ntlook = h->types; ntlook != NULL; ntlook = ntlook->next)
  5859.     {
  5860.       if (ntlook->id == id)
  5861.         nt = ntlook;
  5862.       else if (! ntlook->type.localp)
  5863.         {
  5864.           /* We are creating a duplicate definition of a globally
  5865.              defined tag.  Force it to be local to avoid
  5866.              confusion.  */
  5867.           localp = TRUE;
  5868.         }
  5869.     }
  5870.  
  5871.   if (nt != NULL)
  5872.     {
  5873.       assert (localp == nt->type.localp);
  5874.       if (nt->kind == DEBUG_KIND_ILLEGAL && ! localp)
  5875.         {
  5876.           /* We've already seen a global definition of the type.
  5877.              Ignore this new definition.  */
  5878.           ignorep = TRUE;
  5879.         }
  5880.     }
  5881.   else
  5882.     {
  5883.       nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
  5884.       memset (nt, 0, sizeof *nt);
  5885.       nt->id = id;
  5886.       nt->type.name = h->root.string;
  5887.       nt->next = h->types;
  5888.       h->types = nt;
  5889.       nt->type.indx = info->type_indx;
  5890.       ++info->type_indx;
  5891.     }
  5892.  
  5893.   nt->kind = DEBUG_KIND_ILLEGAL;
  5894.  
  5895.   if (! ieee_init_buffer (info, &strdef)
  5896.       || ! ieee_define_named_type (info, tag, nt->type.indx, size, TRUE,
  5897.                                    localp, &strdef)
  5898.       || ! ieee_write_number (info, structp ? 'S' : 'U')
  5899.       || ! ieee_write_number (info, size))
  5900.     return FALSE;
  5901.  
  5902.   if (! ignorep)
  5903.     {
  5904.       const char *hold;
  5905.  
  5906.       /* We never want nt->type.name to be NULL.  We want the rest of
  5907.          the type to be the object set up on the type stack; it will
  5908.          have a NULL name if tag is NULL.  */
  5909.       hold = nt->type.name;
  5910.       nt->type = info->type_stack->type;
  5911.       nt->type.name = hold;
  5912.     }
  5913.  
  5914.   info->type_stack->type.name = tag;
  5915.   info->type_stack->type.strdef = strdef;
  5916.   info->type_stack->type.ignorep = ignorep;
  5917.  
  5918.   return TRUE;
  5919. }
  5920.  
  5921. /* Add a field to a struct.  */
  5922.  
  5923. static bfd_boolean
  5924. ieee_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
  5925.                    enum debug_visibility visibility)
  5926. {
  5927.   struct ieee_handle *info = (struct ieee_handle *) p;
  5928.   unsigned int size;
  5929.   bfd_boolean unsignedp;
  5930.   bfd_boolean referencep;
  5931.   bfd_boolean localp;
  5932.   unsigned int indx;
  5933.   bfd_vma offset;
  5934.  
  5935.   assert (info->type_stack != NULL
  5936.           && info->type_stack->next != NULL
  5937.           && ! ieee_buffer_emptyp (&info->type_stack->next->type.strdef));
  5938.  
  5939.   /* If we are ignoring this struct definition, just pop and ignore
  5940.      the type.  */
  5941.   if (info->type_stack->next->type.ignorep)
  5942.     {
  5943.       ieee_pop_unused_type (info);
  5944.       return TRUE;
  5945.     }
  5946.  
  5947.   size = info->type_stack->type.size;
  5948.   unsignedp = info->type_stack->type.unsignedp;
  5949.   referencep = info->type_stack->type.referencep;
  5950.   localp = info->type_stack->type.localp;
  5951.   indx = ieee_pop_type (info);
  5952.  
  5953.   if (localp)
  5954.     info->type_stack->type.localp = TRUE;
  5955.  
  5956.   if (info->type_stack->type.classdef != NULL)
  5957.     {
  5958.       unsigned int flags;
  5959.       unsigned int nindx;
  5960.  
  5961.       /* This is a class.  We must add a description of this field to
  5962.          the class records we are building.  */
  5963.  
  5964.       flags = ieee_vis_to_flags (visibility);
  5965.       nindx = info->type_stack->type.classdef->indx;
  5966.       if (! ieee_change_buffer (info,
  5967.                                 &info->type_stack->type.classdef->pmiscbuf)
  5968.           || ! ieee_write_asn (info, nindx, 'd')
  5969.           || ! ieee_write_asn (info, nindx, flags)
  5970.           || ! ieee_write_atn65 (info, nindx, name)
  5971.           || ! ieee_write_atn65 (info, nindx, name))
  5972.         return FALSE;
  5973.       info->type_stack->type.classdef->pmisccount += 4;
  5974.  
  5975.       if (referencep)
  5976.         {
  5977.           /* We need to output a record recording that this field is
  5978.              really of reference type.  We put this on the refs field
  5979.              of classdef, so that it can be appended to the C++
  5980.              records after the class is defined.  */
  5981.  
  5982.           nindx = info->name_indx;
  5983.           ++info->name_indx;
  5984.  
  5985.           if (! ieee_change_buffer (info,
  5986.                                     &info->type_stack->type.classdef->refs)
  5987.               || ! ieee_write_byte (info, (int) ieee_nn_record)
  5988.               || ! ieee_write_number (info, nindx)
  5989.               || ! ieee_write_id (info, "")
  5990.               || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  5991.               || ! ieee_write_number (info, nindx)
  5992.               || ! ieee_write_number (info, 0)
  5993.               || ! ieee_write_number (info, 62)
  5994.               || ! ieee_write_number (info, 80)
  5995.               || ! ieee_write_number (info, 4)
  5996.               || ! ieee_write_asn (info, nindx, 'R')
  5997.               || ! ieee_write_asn (info, nindx, 3)
  5998.               || ! ieee_write_atn65 (info, nindx, info->type_stack->type.name)
  5999.               || ! ieee_write_atn65 (info, nindx, name))
  6000.             return FALSE;
  6001.         }
  6002.     }
  6003.  
  6004.   /* If the bitsize doesn't match the expected size, we need to output
  6005.      a bitfield type.  */
  6006.   if (size == 0 || bitsize == 0 || bitsize == size * 8)
  6007.     offset = bitpos / 8;
  6008.   else
  6009.     {
  6010.       if (! ieee_define_type (info, 0, unsignedp,
  6011.                               info->type_stack->type.localp)
  6012.           || ! ieee_write_number (info, 'g')
  6013.           || ! ieee_write_number (info, unsignedp ? 0 : 1)
  6014.           || ! ieee_write_number (info, bitsize)
  6015.           || ! ieee_write_number (info, indx))
  6016.         return FALSE;
  6017.       indx = ieee_pop_type (info);
  6018.       offset = bitpos;
  6019.     }
  6020.  
  6021.   /* Switch to the struct we are building in order to output this
  6022.      field definition.  */
  6023.   return (ieee_change_buffer (info, &info->type_stack->type.strdef)
  6024.           && ieee_write_id (info, name)
  6025.           && ieee_write_number (info, indx)
  6026.           && ieee_write_number (info, offset));
  6027. }
  6028.  
  6029. /* Finish up a struct type.  */
  6030.  
  6031. static bfd_boolean
  6032. ieee_end_struct_type (void *p)
  6033. {
  6034.   struct ieee_handle *info = (struct ieee_handle *) p;
  6035.   struct ieee_buflist *pb;
  6036.  
  6037.   assert (info->type_stack != NULL
  6038.           && ! ieee_buffer_emptyp (&info->type_stack->type.strdef));
  6039.  
  6040.   /* If we were ignoring this struct definition because it was a
  6041.      duplicate definition, just through away whatever bytes we have
  6042.      accumulated.  Leave the type on the stack.  */
  6043.   if (info->type_stack->type.ignorep)
  6044.     return TRUE;
  6045.  
  6046.   /* If this is not a duplicate definition of this tag, then localp
  6047.      will be FALSE, and we can put it in the global type block.
  6048.      FIXME: We should avoid outputting duplicate definitions which are
  6049.      the same.  */
  6050.   if (! info->type_stack->type.localp)
  6051.     {
  6052.       /* Make sure we have started the global type block.  */
  6053.       if (ieee_buffer_emptyp (&info->global_types))
  6054.         {
  6055.           if (! ieee_change_buffer (info, &info->global_types)
  6056.               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  6057.               || ! ieee_write_byte (info, 2)
  6058.               || ! ieee_write_number (info, 0)
  6059.               || ! ieee_write_id (info, ""))
  6060.             return FALSE;
  6061.         }
  6062.       pb = &info->global_types;
  6063.     }
  6064.   else
  6065.     {
  6066.       /* Make sure we have started the types block.  */
  6067.       if (ieee_buffer_emptyp (&info->types))
  6068.         {
  6069.           if (! ieee_change_buffer (info, &info->types)
  6070.               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  6071.               || ! ieee_write_byte (info, 1)
  6072.               || ! ieee_write_number (info, 0)
  6073.               || ! ieee_write_id (info, info->modname))
  6074.             return FALSE;
  6075.         }
  6076.       pb = &info->types;
  6077.     }
  6078.  
  6079.   /* Append the struct definition to the types.  */
  6080.   if (! ieee_append_buffer (info, pb, &info->type_stack->type.strdef)
  6081.       || ! ieee_init_buffer (info, &info->type_stack->type.strdef))
  6082.     return FALSE;
  6083.  
  6084.   /* Leave the struct on the type stack.  */
  6085.  
  6086.   return TRUE;
  6087. }
  6088.  
  6089. /* Start a class type.  */
  6090.  
  6091. static bfd_boolean
  6092. ieee_start_class_type (void *p, const char *tag, unsigned int id,
  6093.                        bfd_boolean structp, unsigned int size,
  6094.                        bfd_boolean vptr, bfd_boolean ownvptr)
  6095. {
  6096.   struct ieee_handle *info = (struct ieee_handle *) p;
  6097.   const char *vclass;
  6098.   struct ieee_buflist pmiscbuf;
  6099.   unsigned int indx;
  6100.   struct ieee_type_class *classdef;
  6101.  
  6102.   /* A C++ class is output as a C++ struct along with a set of pmisc
  6103.      records describing the class.  */
  6104.  
  6105.   /* We need to have a name so that we can associate the struct and
  6106.      the class.  */
  6107.   if (tag == NULL)
  6108.     {
  6109.       char *t;
  6110.  
  6111.       t = (char *) xmalloc (20);
  6112.       sprintf (t, "__anon%u", id);
  6113.       tag = t;
  6114.     }
  6115.  
  6116.   /* We can't write out the virtual table information until we have
  6117.      finished the class, because we don't know the virtual table size.
  6118.      We get the size from the largest voffset we see.  */
  6119.   vclass = NULL;
  6120.   if (vptr && ! ownvptr)
  6121.     {
  6122.       vclass = info->type_stack->type.name;
  6123.       assert (vclass != NULL);
  6124.       /* We don't call ieee_pop_unused_type, since the class should
  6125.          get defined.  */
  6126.       (void) ieee_pop_type (info);
  6127.     }
  6128.  
  6129.   if (! ieee_start_struct_type (p, tag, id, structp, size))
  6130.     return FALSE;
  6131.  
  6132.   indx = info->name_indx;
  6133.   ++info->name_indx;
  6134.  
  6135.   /* We write out pmisc records into the classdef field.  We will
  6136.      write out the pmisc start after we know the number of records we
  6137.      need.  */
  6138.   if (! ieee_init_buffer (info, &pmiscbuf)
  6139.       || ! ieee_change_buffer (info, &pmiscbuf)
  6140.       || ! ieee_write_asn (info, indx, 'T')
  6141.       || ! ieee_write_asn (info, indx, structp ? 'o' : 'u')
  6142.       || ! ieee_write_atn65 (info, indx, tag))
  6143.     return FALSE;
  6144.  
  6145.   classdef = (struct ieee_type_class *) xmalloc (sizeof *classdef);
  6146.   memset (classdef, 0, sizeof *classdef);
  6147.  
  6148.   classdef->indx = indx;
  6149.   classdef->pmiscbuf = pmiscbuf;
  6150.   classdef->pmisccount = 3;
  6151.   classdef->vclass = vclass;
  6152.   classdef->ownvptr = ownvptr;
  6153.  
  6154.   info->type_stack->type.classdef = classdef;
  6155.  
  6156.   return TRUE;
  6157. }
  6158.  
  6159. /* Add a static member to a class.  */
  6160.  
  6161. static bfd_boolean
  6162. ieee_class_static_member (void *p, const char *name, const char *physname,
  6163.                           enum debug_visibility visibility)
  6164. {
  6165.   struct ieee_handle *info = (struct ieee_handle *) p;
  6166.   unsigned int flags;
  6167.   unsigned int nindx;
  6168.  
  6169.   /* We don't care about the type.  Hopefully there will be a call to
  6170.      ieee_variable declaring the physical name and the type, since
  6171.      that is where an IEEE consumer must get the type.  */
  6172.   ieee_pop_unused_type (info);
  6173.  
  6174.   assert (info->type_stack != NULL
  6175.           && info->type_stack->type.classdef != NULL);
  6176.  
  6177.   flags = ieee_vis_to_flags (visibility);
  6178.   flags |= CXXFLAGS_STATIC;
  6179.  
  6180.   nindx = info->type_stack->type.classdef->indx;
  6181.  
  6182.   if (! ieee_change_buffer (info, &info->type_stack->type.classdef->pmiscbuf)
  6183.       || ! ieee_write_asn (info, nindx, 'd')
  6184.       || ! ieee_write_asn (info, nindx, flags)
  6185.       || ! ieee_write_atn65 (info, nindx, name)
  6186.       || ! ieee_write_atn65 (info, nindx, physname))
  6187.     return FALSE;
  6188.   info->type_stack->type.classdef->pmisccount += 4;
  6189.  
  6190.   return TRUE;
  6191. }
  6192.  
  6193. /* Add a base class to a class.  */
  6194.  
  6195. static bfd_boolean
  6196. ieee_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
  6197.                       enum debug_visibility visibility)
  6198. {
  6199.   struct ieee_handle *info = (struct ieee_handle *) p;
  6200.   const char *bname;
  6201.   bfd_boolean localp;
  6202.   unsigned int bindx;
  6203.   char *fname;
  6204.   unsigned int flags;
  6205.   unsigned int nindx;
  6206.  
  6207.   assert (info->type_stack != NULL
  6208.           && info->type_stack->type.name != NULL
  6209.           && info->type_stack->next != NULL
  6210.           && info->type_stack->next->type.classdef != NULL
  6211.           && ! ieee_buffer_emptyp (&info->type_stack->next->type.strdef));
  6212.  
  6213.   bname = info->type_stack->type.name;
  6214.   localp = info->type_stack->type.localp;
  6215.   bindx = ieee_pop_type (info);
  6216.  
  6217.   /* We are currently defining both a struct and a class.  We must
  6218.      write out a field definition in the struct which holds the base
  6219.      class.  The stabs debugging reader will create a field named
  6220.      _vb$CLASS for a virtual base class, so we just use that.  FIXME:
  6221.      we should not depend upon a detail of stabs debugging.  */
  6222.   if (is_virtual)
  6223.     {
  6224.       fname = (char *) xmalloc (strlen (bname) + sizeof "_vb$");
  6225.       sprintf (fname, "_vb$%s", bname);
  6226.       flags = BASEFLAGS_VIRTUAL;
  6227.     }
  6228.   else
  6229.     {
  6230.       if (localp)
  6231.         info->type_stack->type.localp = TRUE;
  6232.  
  6233.       fname = (char *) xmalloc (strlen (bname) + sizeof "_b$");
  6234.       sprintf (fname, "_b$%s", bname);
  6235.  
  6236.       if (! ieee_change_buffer (info, &info->type_stack->type.strdef)
  6237.           || ! ieee_write_id (info, fname)
  6238.           || ! ieee_write_number (info, bindx)
  6239.           || ! ieee_write_number (info, bitpos / 8))
  6240.         {
  6241.           free (fname);
  6242.           return FALSE;
  6243.         }
  6244.       flags = 0;
  6245.     }
  6246.  
  6247.   if (visibility == DEBUG_VISIBILITY_PRIVATE)
  6248.     flags |= BASEFLAGS_PRIVATE;
  6249.  
  6250.   nindx = info->type_stack->type.classdef->indx;
  6251.  
  6252.   if (! ieee_change_buffer (info, &info->type_stack->type.classdef->pmiscbuf)
  6253.       || ! ieee_write_asn (info, nindx, 'b')
  6254.       || ! ieee_write_asn (info, nindx, flags)
  6255.       || ! ieee_write_atn65 (info, nindx, bname)
  6256.       || ! ieee_write_asn (info, nindx, 0)
  6257.       || ! ieee_write_atn65 (info, nindx, fname))
  6258.     {
  6259.       free (fname);
  6260.       return FALSE;
  6261.     }
  6262.   info->type_stack->type.classdef->pmisccount += 5;
  6263.  
  6264.   free (fname);
  6265.  
  6266.   return TRUE;
  6267. }
  6268.  
  6269. /* Start building a method for a class.  */
  6270.  
  6271. static bfd_boolean
  6272. ieee_class_start_method (void *p, const char *name)
  6273. {
  6274.   struct ieee_handle *info = (struct ieee_handle *) p;
  6275.  
  6276.   assert (info->type_stack != NULL
  6277.           && info->type_stack->type.classdef != NULL
  6278.           && info->type_stack->type.classdef->method == NULL);
  6279.  
  6280.   info->type_stack->type.classdef->method = name;
  6281.  
  6282.   return TRUE;
  6283. }
  6284.  
  6285. /* Define a new method variant, either static or not.  */
  6286.  
  6287. static bfd_boolean
  6288. ieee_class_method_var (struct ieee_handle *info, const char *physname,
  6289.                        enum debug_visibility visibility,
  6290.                        bfd_boolean staticp, bfd_boolean constp,
  6291.                        bfd_boolean volatilep, bfd_vma voffset,
  6292.                        bfd_boolean context)
  6293. {
  6294.   unsigned int flags;
  6295.   unsigned int nindx;
  6296.   bfd_boolean is_virtual;
  6297.  
  6298.   /* We don't need the type of the method.  An IEEE consumer which
  6299.      wants the type must track down the function by the physical name
  6300.      and get the type from that.  */
  6301.   ieee_pop_unused_type (info);
  6302.  
  6303.   /* We don't use the context.  FIXME: We probably ought to use it to
  6304.      adjust the voffset somehow, but I don't really know how.  */
  6305.   if (context)
  6306.     ieee_pop_unused_type (info);
  6307.  
  6308.   assert (info->type_stack != NULL
  6309.           && info->type_stack->type.classdef != NULL
  6310.           && info->type_stack->type.classdef->method != NULL);
  6311.  
  6312.   flags = ieee_vis_to_flags (visibility);
  6313.  
  6314.   /* FIXME: We never set CXXFLAGS_OVERRIDE, CXXFLAGS_OPERATOR,
  6315.      CXXFLAGS_CTORDTOR, CXXFLAGS_CTOR, or CXXFLAGS_INLINE.  */
  6316.  
  6317.   if (staticp)
  6318.     flags |= CXXFLAGS_STATIC;
  6319.   if (constp)
  6320.     flags |= CXXFLAGS_CONST;
  6321.   if (volatilep)
  6322.     flags |= CXXFLAGS_VOLATILE;
  6323.  
  6324.   nindx = info->type_stack->type.classdef->indx;
  6325.  
  6326.   is_virtual = context || voffset > 0;
  6327.  
  6328.   if (! ieee_change_buffer (info,
  6329.                             &info->type_stack->type.classdef->pmiscbuf)
  6330.       || ! ieee_write_asn (info, nindx, is_virtual ? 'v' : 'm')
  6331.       || ! ieee_write_asn (info, nindx, flags)
  6332.       || ! ieee_write_atn65 (info, nindx,
  6333.                              info->type_stack->type.classdef->method)
  6334.       || ! ieee_write_atn65 (info, nindx, physname))
  6335.     return FALSE;
  6336.  
  6337.   if (is_virtual)
  6338.     {
  6339.       if (voffset > info->type_stack->type.classdef->voffset)
  6340.         info->type_stack->type.classdef->voffset = voffset;
  6341.       if (! ieee_write_asn (info, nindx, voffset))
  6342.         return FALSE;
  6343.       ++info->type_stack->type.classdef->pmisccount;
  6344.     }
  6345.  
  6346.   if (! ieee_write_asn (info, nindx, 0))
  6347.     return FALSE;
  6348.  
  6349.   info->type_stack->type.classdef->pmisccount += 5;
  6350.  
  6351.   return TRUE;
  6352. }
  6353.  
  6354. /* Define a new method variant.  */
  6355.  
  6356. static bfd_boolean
  6357. ieee_class_method_variant (void *p, const char *physname,
  6358.                            enum debug_visibility visibility,
  6359.                            bfd_boolean constp, bfd_boolean volatilep,
  6360.                            bfd_vma voffset, bfd_boolean context)
  6361. {
  6362.   struct ieee_handle *info = (struct ieee_handle *) p;
  6363.  
  6364.   return ieee_class_method_var (info, physname, visibility, FALSE, constp,
  6365.                                 volatilep, voffset, context);
  6366. }
  6367.  
  6368. /* Define a new static method variant.  */
  6369.  
  6370. static bfd_boolean
  6371. ieee_class_static_method_variant (void *p, const char *physname,
  6372.                                   enum debug_visibility visibility,
  6373.                                   bfd_boolean constp, bfd_boolean volatilep)
  6374. {
  6375.   struct ieee_handle *info = (struct ieee_handle *) p;
  6376.  
  6377.   return ieee_class_method_var (info, physname, visibility, TRUE, constp,
  6378.                                 volatilep, 0, FALSE);
  6379. }
  6380.  
  6381. /* Finish up a method.  */
  6382.  
  6383. static bfd_boolean
  6384. ieee_class_end_method (void *p)
  6385. {
  6386.   struct ieee_handle *info = (struct ieee_handle *) p;
  6387.  
  6388.   assert (info->type_stack != NULL
  6389.           && info->type_stack->type.classdef != NULL
  6390.           && info->type_stack->type.classdef->method != NULL);
  6391.  
  6392.   info->type_stack->type.classdef->method = NULL;
  6393.  
  6394.   return TRUE;
  6395. }
  6396.  
  6397. /* Finish up a class.  */
  6398.  
  6399. static bfd_boolean
  6400. ieee_end_class_type (void *p)
  6401. {
  6402.   struct ieee_handle *info = (struct ieee_handle *) p;
  6403.   unsigned int nindx;
  6404.  
  6405.   assert (info->type_stack != NULL
  6406.           && info->type_stack->type.classdef != NULL);
  6407.  
  6408.   /* If we were ignoring this class definition because it was a
  6409.      duplicate definition, just through away whatever bytes we have
  6410.      accumulated.  Leave the type on the stack.  */
  6411.   if (info->type_stack->type.ignorep)
  6412.     return TRUE;
  6413.  
  6414.   nindx = info->type_stack->type.classdef->indx;
  6415.  
  6416.   /* If we have a virtual table, we can write out the information now.  */
  6417.   if (info->type_stack->type.classdef->vclass != NULL
  6418.       || info->type_stack->type.classdef->ownvptr)
  6419.     {
  6420.       if (! ieee_change_buffer (info,
  6421.                                 &info->type_stack->type.classdef->pmiscbuf)
  6422.           || ! ieee_write_asn (info, nindx, 'z')
  6423.           || ! ieee_write_atn65 (info, nindx, "")
  6424.           || ! ieee_write_asn (info, nindx,
  6425.                                info->type_stack->type.classdef->voffset))
  6426.         return FALSE;
  6427.       if (info->type_stack->type.classdef->ownvptr)
  6428.         {
  6429.           if (! ieee_write_atn65 (info, nindx, ""))
  6430.             return FALSE;
  6431.         }
  6432.       else
  6433.         {
  6434.           if (! ieee_write_atn65 (info, nindx,
  6435.                                   info->type_stack->type.classdef->vclass))
  6436.             return FALSE;
  6437.         }
  6438.       if (! ieee_write_asn (info, nindx, 0))
  6439.         return FALSE;
  6440.       info->type_stack->type.classdef->pmisccount += 5;
  6441.     }
  6442.  
  6443.   /* Now that we know the number of pmisc records, we can write out
  6444.      the atn62 which starts the pmisc records, and append them to the
  6445.      C++ buffers.  */
  6446.  
  6447.   if (! ieee_change_buffer (info, &info->cxx)
  6448.       || ! ieee_write_byte (info, (int) ieee_nn_record)
  6449.       || ! ieee_write_number (info, nindx)
  6450.       || ! ieee_write_id (info, "")
  6451.       || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  6452.       || ! ieee_write_number (info, nindx)
  6453.       || ! ieee_write_number (info, 0)
  6454.       || ! ieee_write_number (info, 62)
  6455.       || ! ieee_write_number (info, 80)
  6456.       || ! ieee_write_number (info,
  6457.                               info->type_stack->type.classdef->pmisccount))
  6458.     return FALSE;
  6459.  
  6460.   if (! ieee_append_buffer (info, &info->cxx,
  6461.                             &info->type_stack->type.classdef->pmiscbuf))
  6462.     return FALSE;
  6463.   if (! ieee_buffer_emptyp (&info->type_stack->type.classdef->refs))
  6464.     {
  6465.       if (! ieee_append_buffer (info, &info->cxx,
  6466.                                 &info->type_stack->type.classdef->refs))
  6467.         return FALSE;
  6468.     }
  6469.  
  6470.   return ieee_end_struct_type (p);
  6471. }
  6472.  
  6473. /* Push a previously seen typedef onto the type stack.  */
  6474.  
  6475. static bfd_boolean
  6476. ieee_typedef_type (void *p, const char *name)
  6477. {
  6478.   struct ieee_handle *info = (struct ieee_handle *) p;
  6479.   struct ieee_name_type_hash_entry *h;
  6480.   struct ieee_name_type *nt;
  6481.  
  6482.   h = ieee_name_type_hash_lookup (&info->typedefs, name, FALSE, FALSE);
  6483.  
  6484.   /* h should never be NULL, since that would imply that the generic
  6485.      debugging code has asked for a typedef which it has not yet
  6486.      defined.  */
  6487.   assert (h != NULL);
  6488.  
  6489.   /* We always use the most recently defined type for this name, which
  6490.      will be the first one on the list.  */
  6491.  
  6492.   nt = h->types;
  6493.   if (! ieee_push_type (info, nt->type.indx, nt->type.size,
  6494.                         nt->type.unsignedp, nt->type.localp))
  6495.     return FALSE;
  6496.  
  6497.   /* Copy over any other type information we may have.  */
  6498.   info->type_stack->type = nt->type;
  6499.  
  6500.   return TRUE;
  6501. }
  6502.  
  6503. /* Push a tagged type onto the type stack.  */
  6504.  
  6505. static bfd_boolean
  6506. ieee_tag_type (void *p, const char *name, unsigned int id,
  6507.                enum debug_type_kind kind)
  6508. {
  6509.   struct ieee_handle *info = (struct ieee_handle *) p;
  6510.   bfd_boolean localp;
  6511.   bfd_boolean copy;
  6512.   char ab[20];
  6513.   struct ieee_name_type_hash_entry *h;
  6514.   struct ieee_name_type *nt;
  6515.  
  6516.   if (kind == DEBUG_KIND_ENUM)
  6517.     {
  6518.       struct ieee_defined_enum *e;
  6519.  
  6520.       if (name == NULL)
  6521.         abort ();
  6522.       for (e = info->enums; e != NULL; e = e->next)
  6523.         if (e->tag != NULL && strcmp (e->tag, name) == 0)
  6524.           return ieee_push_type (info, e->indx, 0, TRUE, FALSE);
  6525.  
  6526.       e = (struct ieee_defined_enum *) xmalloc (sizeof *e);
  6527.       memset (e, 0, sizeof *e);
  6528.  
  6529.       e->indx = info->type_indx;
  6530.       ++info->type_indx;
  6531.       e->tag = name;
  6532.       e->defined = FALSE;
  6533.  
  6534.       e->next = info->enums;
  6535.       info->enums = e;
  6536.  
  6537.       return ieee_push_type (info, e->indx, 0, TRUE, FALSE);
  6538.     }
  6539.  
  6540.   localp = FALSE;
  6541.  
  6542.   copy = FALSE;
  6543.   if (name == NULL)
  6544.     {
  6545.       sprintf (ab, "__anon%u", id);
  6546.       name = ab;
  6547.       copy = TRUE;
  6548.     }
  6549.  
  6550.   h = ieee_name_type_hash_lookup (&info->tags, name, TRUE, copy);
  6551.   if (h == NULL)
  6552.     return FALSE;
  6553.  
  6554.   for (nt = h->types; nt != NULL; nt = nt->next)
  6555.     {
  6556.       if (nt->id == id)
  6557.         {
  6558.           if (! ieee_push_type (info, nt->type.indx, nt->type.size,
  6559.                                 nt->type.unsignedp, nt->type.localp))
  6560.             return FALSE;
  6561.           /* Copy over any other type information we may have.  */
  6562.           info->type_stack->type = nt->type;
  6563.           return TRUE;
  6564.         }
  6565.  
  6566.       if (! nt->type.localp)
  6567.         {
  6568.           /* This is a duplicate of a global type, so it must be
  6569.              local.  */
  6570.           localp = TRUE;
  6571.         }
  6572.     }
  6573.  
  6574.   nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
  6575.   memset (nt, 0, sizeof *nt);
  6576.  
  6577.   nt->id = id;
  6578.   nt->type.name = h->root.string;
  6579.   nt->type.indx = info->type_indx;
  6580.   nt->type.localp = localp;
  6581.   ++info->type_indx;
  6582.   nt->kind = kind;
  6583.  
  6584.   nt->next = h->types;
  6585.   h->types = nt;
  6586.  
  6587.   if (! ieee_push_type (info, nt->type.indx, 0, FALSE, localp))
  6588.     return FALSE;
  6589.  
  6590.   info->type_stack->type.name = h->root.string;
  6591.  
  6592.   return TRUE;
  6593. }
  6594.  
  6595. /* Output a typedef.  */
  6596.  
  6597. static bfd_boolean
  6598. ieee_typdef (void *p, const char *name)
  6599. {
  6600.   struct ieee_handle *info = (struct ieee_handle *) p;
  6601.   struct ieee_write_type type;
  6602.   unsigned int indx;
  6603.   bfd_boolean found;
  6604.   bfd_boolean localp;
  6605.   struct ieee_name_type_hash_entry *h;
  6606.   struct ieee_name_type *nt;
  6607.  
  6608.   type = info->type_stack->type;
  6609.   indx = type.indx;
  6610.  
  6611.   /* If this is a simple builtin type using a builtin name, we don't
  6612.      want to output the typedef itself.  We also want to change the
  6613.      type index to correspond to the name being used.  We recognize
  6614.      names used in stabs debugging output even if they don't exactly
  6615.      correspond to the names used for the IEEE builtin types.  */
  6616.   found = FALSE;
  6617.   if (indx <= (unsigned int) builtin_bcd_float)
  6618.     {
  6619.       switch ((enum builtin_types) indx)
  6620.         {
  6621.         default:
  6622.           break;
  6623.  
  6624.         case builtin_void:
  6625.           if (strcmp (name, "void") == 0)
  6626.             found = TRUE;
  6627.           break;
  6628.  
  6629.         case builtin_signed_char:
  6630.         case builtin_char:
  6631.           if (strcmp (name, "signed char") == 0)
  6632.             {
  6633.               indx = (unsigned int) builtin_signed_char;
  6634.               found = TRUE;
  6635.             }
  6636.           else if (strcmp (name, "char") == 0)
  6637.             {
  6638.               indx = (unsigned int) builtin_char;
  6639.               found = TRUE;
  6640.             }
  6641.           break;
  6642.  
  6643.         case builtin_unsigned_char:
  6644.           if (strcmp (name, "unsigned char") == 0)
  6645.             found = TRUE;
  6646.           break;
  6647.  
  6648.         case builtin_signed_short_int:
  6649.         case builtin_short:
  6650.         case builtin_short_int:
  6651.         case builtin_signed_short:
  6652.           if (strcmp (name, "signed short int") == 0)
  6653.             {
  6654.               indx = (unsigned int) builtin_signed_short_int;
  6655.               found = TRUE;
  6656.             }
  6657.           else if (strcmp (name, "short") == 0)
  6658.             {
  6659.               indx = (unsigned int) builtin_short;
  6660.               found = TRUE;
  6661.             }
  6662.           else if (strcmp (name, "short int") == 0)
  6663.             {
  6664.               indx = (unsigned int) builtin_short_int;
  6665.               found = TRUE;
  6666.             }
  6667.           else if (strcmp (name, "signed short") == 0)
  6668.             {
  6669.               indx = (unsigned int) builtin_signed_short;
  6670.               found = TRUE;
  6671.             }
  6672.           break;
  6673.  
  6674.         case builtin_unsigned_short_int:
  6675.         case builtin_unsigned_short:
  6676.           if (strcmp (name, "unsigned short int") == 0
  6677.               || strcmp (name, "short unsigned int") == 0)
  6678.             {
  6679.               indx = builtin_unsigned_short_int;
  6680.               found = TRUE;
  6681.             }
  6682.           else if (strcmp (name, "unsigned short") == 0)
  6683.             {
  6684.               indx = builtin_unsigned_short;
  6685.               found = TRUE;
  6686.             }
  6687.           break;
  6688.  
  6689.         case builtin_signed_long:
  6690.         case builtin_int: /* FIXME: Size depends upon architecture.  */
  6691.         case builtin_long:
  6692.           if (strcmp (name, "signed long") == 0)
  6693.             {
  6694.               indx = builtin_signed_long;
  6695.               found = TRUE;
  6696.             }
  6697.           else if (strcmp (name, "int") == 0)
  6698.             {
  6699.               indx = builtin_int;
  6700.               found = TRUE;
  6701.             }
  6702.           else if (strcmp (name, "long") == 0
  6703.                    || strcmp (name, "long int") == 0)
  6704.             {
  6705.               indx = builtin_long;
  6706.               found = TRUE;
  6707.             }
  6708.           break;
  6709.  
  6710.         case builtin_unsigned_long:
  6711.         case builtin_unsigned: /* FIXME: Size depends upon architecture.  */
  6712.         case builtin_unsigned_int: /* FIXME: Like builtin_unsigned.  */
  6713.           if (strcmp (name, "unsigned long") == 0
  6714.               || strcmp (name, "long unsigned int") == 0)
  6715.             {
  6716.               indx = builtin_unsigned_long;
  6717.               found = TRUE;
  6718.             }
  6719.           else if (strcmp (name, "unsigned") == 0)
  6720.             {
  6721.               indx = builtin_unsigned;
  6722.               found = TRUE;
  6723.             }
  6724.           else if (strcmp (name, "unsigned int") == 0)
  6725.             {
  6726.               indx = builtin_unsigned_int;
  6727.               found = TRUE;
  6728.             }
  6729.           break;
  6730.  
  6731.         case builtin_signed_long_long:
  6732.           if (strcmp (name, "signed long long") == 0
  6733.               || strcmp (name, "long long int") == 0)
  6734.             found = TRUE;
  6735.           break;
  6736.  
  6737.         case builtin_unsigned_long_long:
  6738.           if (strcmp (name, "unsigned long long") == 0
  6739.               || strcmp (name, "long long unsigned int") == 0)
  6740.             found = TRUE;
  6741.           break;
  6742.  
  6743.         case builtin_float:
  6744.           if (strcmp (name, "float") == 0)
  6745.             found = TRUE;
  6746.           break;
  6747.  
  6748.         case builtin_double:
  6749.           if (strcmp (name, "double") == 0)
  6750.             found = TRUE;
  6751.           break;
  6752.  
  6753.         case builtin_long_double:
  6754.           if (strcmp (name, "long double") == 0)
  6755.             found = TRUE;
  6756.           break;
  6757.  
  6758.         case builtin_long_long_double:
  6759.           if (strcmp (name, "long long double") == 0)
  6760.             found = TRUE;
  6761.           break;
  6762.         }
  6763.  
  6764.       if (found)
  6765.         type.indx = indx;
  6766.     }
  6767.  
  6768.   h = ieee_name_type_hash_lookup (&info->typedefs, name, TRUE, FALSE);
  6769.   if (h == NULL)
  6770.     return FALSE;
  6771.  
  6772.   /* See if we have already defined this type with this name.  */
  6773.   localp = type.localp;
  6774.   for (nt = h->types; nt != NULL; nt = nt->next)
  6775.     {
  6776.       if (nt->id == indx)
  6777.         {
  6778.           /* If this is a global definition, then we don't need to
  6779.              do anything here.  */
  6780.           if (! nt->type.localp)
  6781.             {
  6782.               ieee_pop_unused_type (info);
  6783.               return TRUE;
  6784.             }
  6785.         }
  6786.       else
  6787.         {
  6788.           /* This is a duplicate definition, so make this one local.  */
  6789.           localp = TRUE;
  6790.         }
  6791.     }
  6792.  
  6793.   /* We need to add a new typedef for this type.  */
  6794.  
  6795.   nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
  6796.   memset (nt, 0, sizeof *nt);
  6797.   nt->id = indx;
  6798.   nt->type = type;
  6799.   nt->type.name = name;
  6800.   nt->type.localp = localp;
  6801.   nt->kind = DEBUG_KIND_ILLEGAL;
  6802.  
  6803.   nt->next = h->types;
  6804.   h->types = nt;
  6805.  
  6806.   if (found)
  6807.     {
  6808.       /* This is one of the builtin typedefs, so we don't need to
  6809.          actually define it.  */
  6810.       ieee_pop_unused_type (info);
  6811.       return TRUE;
  6812.     }
  6813.  
  6814.   indx = ieee_pop_type (info);
  6815.  
  6816.   if (! ieee_define_named_type (info, name, (unsigned int) -1, type.size,
  6817.                                 type.unsignedp, localp,
  6818.                                 (struct ieee_buflist *) NULL)
  6819.       || ! ieee_write_number (info, 'T')
  6820.       || ! ieee_write_number (info, indx))
  6821.     return FALSE;
  6822.  
  6823.   /* Remove the type we just added to the type stack.  This should not
  6824.      be ieee_pop_unused_type, since the type is used, we just don't
  6825.      need it now.  */
  6826.   (void) ieee_pop_type (info);
  6827.  
  6828.   return TRUE;
  6829. }
  6830.  
  6831. /* Output a tag for a type.  We don't have to do anything here.  */
  6832.  
  6833. static bfd_boolean
  6834. ieee_tag (void *p, const char *name ATTRIBUTE_UNUSED)
  6835. {
  6836.   struct ieee_handle *info = (struct ieee_handle *) p;
  6837.  
  6838.   /* This should not be ieee_pop_unused_type, since we want the type
  6839.      to be defined.  */
  6840.   (void) ieee_pop_type (info);
  6841.   return TRUE;
  6842. }
  6843.  
  6844. /* Output an integer constant.  */
  6845.  
  6846. static bfd_boolean
  6847. ieee_int_constant (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED,
  6848.                    bfd_vma val ATTRIBUTE_UNUSED)
  6849. {
  6850.   /* FIXME.  */
  6851.   return TRUE;
  6852. }
  6853.  
  6854. /* Output a floating point constant.  */
  6855.  
  6856. static bfd_boolean
  6857. ieee_float_constant (void *p ATTRIBUTE_UNUSED,
  6858.                      const char *name ATTRIBUTE_UNUSED,
  6859.                      double val ATTRIBUTE_UNUSED)
  6860. {
  6861.   /* FIXME.  */
  6862.   return TRUE;
  6863. }
  6864.  
  6865. /* Output a typed constant.  */
  6866.  
  6867. static bfd_boolean
  6868. ieee_typed_constant (void *p, const char *name ATTRIBUTE_UNUSED,
  6869.                      bfd_vma val ATTRIBUTE_UNUSED)
  6870. {
  6871.   struct ieee_handle *info = (struct ieee_handle *) p;
  6872.  
  6873.   /* FIXME.  */
  6874.   ieee_pop_unused_type (info);
  6875.   return TRUE;
  6876. }
  6877.  
  6878. /* Output a variable.  */
  6879.  
  6880. static bfd_boolean
  6881. ieee_variable (void *p, const char *name, enum debug_var_kind kind,
  6882.                bfd_vma val)
  6883. {
  6884.   struct ieee_handle *info = (struct ieee_handle *) p;
  6885.   unsigned int name_indx;
  6886.   unsigned int size;
  6887.   bfd_boolean referencep;
  6888.   unsigned int type_indx;
  6889.   bfd_boolean asn;
  6890.   int refflag;
  6891.  
  6892.   size = info->type_stack->type.size;
  6893.   referencep = info->type_stack->type.referencep;
  6894.   type_indx = ieee_pop_type (info);
  6895.  
  6896.   assert (! ieee_buffer_emptyp (&info->vars));
  6897.   if (! ieee_change_buffer (info, &info->vars))
  6898.     return FALSE;
  6899.  
  6900.   name_indx = info->name_indx;
  6901.   ++info->name_indx;
  6902.  
  6903.   /* Write out an NN and an ATN record for this variable.  */
  6904.   if (! ieee_write_byte (info, (int) ieee_nn_record)
  6905.       || ! ieee_write_number (info, name_indx)
  6906.       || ! ieee_write_id (info, name)
  6907.       || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  6908.       || ! ieee_write_number (info, name_indx)
  6909.       || ! ieee_write_number (info, type_indx))
  6910.     return FALSE;
  6911.   switch (kind)
  6912.     {
  6913.     default:
  6914.       abort ();
  6915.       return FALSE;
  6916.     case DEBUG_GLOBAL:
  6917.       if (! ieee_write_number (info, 8)
  6918.           || ! ieee_add_range (info, FALSE, val, val + size))
  6919.         return FALSE;
  6920.       refflag = 0;
  6921.       asn = TRUE;
  6922.       break;
  6923.     case DEBUG_STATIC:
  6924.       if (! ieee_write_number (info, 3)
  6925.           || ! ieee_add_range (info, FALSE, val, val + size))
  6926.         return FALSE;
  6927.       refflag = 1;
  6928.       asn = TRUE;
  6929.       break;
  6930.     case DEBUG_LOCAL_STATIC:
  6931.       if (! ieee_write_number (info, 3)
  6932.           || ! ieee_add_range (info, FALSE, val, val + size))
  6933.         return FALSE;
  6934.       refflag = 2;
  6935.       asn = TRUE;
  6936.       break;
  6937.     case DEBUG_LOCAL:
  6938.       if (! ieee_write_number (info, 1)
  6939.           || ! ieee_write_number (info, val))
  6940.         return FALSE;
  6941.       refflag = 2;
  6942.       asn = FALSE;
  6943.       break;
  6944.     case DEBUG_REGISTER:
  6945.       if (! ieee_write_number (info, 2)
  6946.           || ! ieee_write_number (info,
  6947.                                   ieee_genreg_to_regno (info->abfd, val)))
  6948.         return FALSE;
  6949.       refflag = 2;
  6950.       asn = FALSE;
  6951.       break;
  6952.     }
  6953.  
  6954.   if (asn)
  6955.     {
  6956.       if (! ieee_write_asn (info, name_indx, val))
  6957.         return FALSE;
  6958.     }
  6959.  
  6960.   /* If this is really a reference type, then we just output it with
  6961.      pointer type, and must now output a C++ record indicating that it
  6962.      is really reference type.  */
  6963.   if (referencep)
  6964.     {
  6965.       unsigned int nindx;
  6966.  
  6967.       nindx = info->name_indx;
  6968.       ++info->name_indx;
  6969.  
  6970.       /* If this is a global variable, we want to output the misc
  6971.          record in the C++ misc record block.  Otherwise, we want to
  6972.          output it just after the variable definition, which is where
  6973.          the current buffer is.  */
  6974.       if (refflag != 2)
  6975.         {
  6976.           if (! ieee_change_buffer (info, &info->cxx))
  6977.             return FALSE;
  6978.         }
  6979.  
  6980.       if (! ieee_write_byte (info, (int) ieee_nn_record)
  6981.           || ! ieee_write_number (info, nindx)
  6982.           || ! ieee_write_id (info, "")
  6983.           || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  6984.           || ! ieee_write_number (info, nindx)
  6985.           || ! ieee_write_number (info, 0)
  6986.           || ! ieee_write_number (info, 62)
  6987.           || ! ieee_write_number (info, 80)
  6988.           || ! ieee_write_number (info, 3)
  6989.           || ! ieee_write_asn (info, nindx, 'R')
  6990.           || ! ieee_write_asn (info, nindx, refflag)
  6991.           || ! ieee_write_atn65 (info, nindx, name))
  6992.         return FALSE;
  6993.     }
  6994.  
  6995.   return TRUE;
  6996. }
  6997.  
  6998. /* Start outputting information for a function.  */
  6999.  
  7000. static bfd_boolean
  7001. ieee_start_function (void *p, const char *name, bfd_boolean global)
  7002. {
  7003.   struct ieee_handle *info = (struct ieee_handle *) p;
  7004.   bfd_boolean referencep;
  7005.   unsigned int retindx, typeindx;
  7006.  
  7007.   referencep = info->type_stack->type.referencep;
  7008.   retindx = ieee_pop_type (info);
  7009.  
  7010.   /* Besides recording a BB4 or BB6 block, we record the type of the
  7011.      function in the BB1 typedef block.  We can't write out the full
  7012.      type until we have seen all the parameters, so we accumulate it
  7013.      in info->fntype and info->fnargs.  */
  7014.   if (! ieee_buffer_emptyp (&info->fntype))
  7015.     {
  7016.       /* FIXME: This might happen someday if we support nested
  7017.          functions.  */
  7018.       abort ();
  7019.     }
  7020.  
  7021.   info->fnname = name;
  7022.  
  7023.   /* An attribute of 0x40 means that the push mask is unknown.  */
  7024.   if (! ieee_define_named_type (info, name, (unsigned int) -1, 0, FALSE, TRUE,
  7025.                                 &info->fntype)
  7026.       || ! ieee_write_number (info, 'x')
  7027.       || ! ieee_write_number (info, 0x40)
  7028.       || ! ieee_write_number (info, 0)
  7029.       || ! ieee_write_number (info, 0)
  7030.       || ! ieee_write_number (info, retindx))
  7031.     return FALSE;
  7032.  
  7033.   typeindx = ieee_pop_type (info);
  7034.  
  7035.   if (! ieee_init_buffer (info, &info->fnargs))
  7036.     return FALSE;
  7037.   info->fnargcount = 0;
  7038.  
  7039.   /* If the function return value is actually a reference type, we
  7040.      must add a record indicating that.  */
  7041.   if (referencep)
  7042.     {
  7043.       unsigned int nindx;
  7044.  
  7045.       nindx = info->name_indx;
  7046.       ++info->name_indx;
  7047.       if (! ieee_change_buffer (info, &info->cxx)
  7048.           || ! ieee_write_byte (info, (int) ieee_nn_record)
  7049.           || ! ieee_write_number (info, nindx)
  7050.           || ! ieee_write_id (info, "")
  7051.           || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  7052.           || ! ieee_write_number (info, nindx)
  7053.           || ! ieee_write_number (info, 0)
  7054.           || ! ieee_write_number (info, 62)
  7055.           || ! ieee_write_number (info, 80)
  7056.           || ! ieee_write_number (info, 3)
  7057.           || ! ieee_write_asn (info, nindx, 'R')
  7058.           || ! ieee_write_asn (info, nindx, global ? 0 : 1)
  7059.           || ! ieee_write_atn65 (info, nindx, name))
  7060.         return FALSE;
  7061.     }
  7062.  
  7063.   assert (! ieee_buffer_emptyp (&info->vars));
  7064.   if (! ieee_change_buffer (info, &info->vars))
  7065.     return FALSE;
  7066.  
  7067.   /* The address is written out as the first block.  */
  7068.  
  7069.   ++info->block_depth;
  7070.  
  7071.   return (ieee_write_byte (info, (int) ieee_bb_record_enum)
  7072.           && ieee_write_byte (info, global ? 4 : 6)
  7073.           && ieee_write_number (info, 0)
  7074.           && ieee_write_id (info, name)
  7075.           && ieee_write_number (info, 0)
  7076.           && ieee_write_number (info, typeindx));
  7077. }
  7078.  
  7079. /* Add a function parameter.  This will normally be called before the
  7080.    first block, so we postpone them until we see the block.  */
  7081.  
  7082. static bfd_boolean
  7083. ieee_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
  7084.                          bfd_vma val)
  7085. {
  7086.   struct ieee_handle *info = (struct ieee_handle *) p;
  7087.   struct ieee_pending_parm *m, **pm;
  7088.  
  7089.   assert (info->block_depth == 1);
  7090.  
  7091.   m = (struct ieee_pending_parm *) xmalloc (sizeof *m);
  7092.   memset (m, 0, sizeof *m);
  7093.  
  7094.   m->next = NULL;
  7095.   m->name = name;
  7096.   m->referencep = info->type_stack->type.referencep;
  7097.   m->type = ieee_pop_type (info);
  7098.   m->kind = kind;
  7099.   m->val = val;
  7100.  
  7101.   for (pm = &info->pending_parms; *pm != NULL; pm = &(*pm)->next)
  7102.     ;
  7103.   *pm = m;
  7104.  
  7105.   /* Add the type to the fnargs list.  */
  7106.   if (! ieee_change_buffer (info, &info->fnargs)
  7107.       || ! ieee_write_number (info, m->type))
  7108.     return FALSE;
  7109.   ++info->fnargcount;
  7110.  
  7111.   return TRUE;
  7112. }
  7113.  
  7114. /* Output pending function parameters.  */
  7115.  
  7116. static bfd_boolean
  7117. ieee_output_pending_parms (struct ieee_handle *info)
  7118. {
  7119.   struct ieee_pending_parm *m;
  7120.   unsigned int refcount;
  7121.  
  7122.   refcount = 0;
  7123.   for (m = info->pending_parms; m != NULL; m = m->next)
  7124.     {
  7125.       enum debug_var_kind vkind;
  7126.  
  7127.       switch (m->kind)
  7128.         {
  7129.         default:
  7130.           abort ();
  7131.           return FALSE;
  7132.         case DEBUG_PARM_STACK:
  7133.         case DEBUG_PARM_REFERENCE:
  7134.           vkind = DEBUG_LOCAL;
  7135.           break;
  7136.         case DEBUG_PARM_REG:
  7137.         case DEBUG_PARM_REF_REG:
  7138.           vkind = DEBUG_REGISTER;
  7139.           break;
  7140.         }
  7141.  
  7142.       if (! ieee_push_type (info, m->type, 0, FALSE, FALSE))
  7143.         return FALSE;
  7144.       info->type_stack->type.referencep = m->referencep;
  7145.       if (m->referencep)
  7146.         ++refcount;
  7147.       if (! ieee_variable ((void *) info, m->name, vkind, m->val))
  7148.         return FALSE;
  7149.     }
  7150.  
  7151.   /* If there are any reference parameters, we need to output a
  7152.      miscellaneous record indicating them.  */
  7153.   if (refcount > 0)
  7154.     {
  7155.       unsigned int nindx, varindx;
  7156.  
  7157.       /* FIXME: The MRI compiler outputs the demangled function name
  7158.          here, but we are outputting the mangled name.  */
  7159.       nindx = info->name_indx;
  7160.       ++info->name_indx;
  7161.       if (! ieee_change_buffer (info, &info->vars)
  7162.           || ! ieee_write_byte (info, (int) ieee_nn_record)
  7163.           || ! ieee_write_number (info, nindx)
  7164.           || ! ieee_write_id (info, "")
  7165.           || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  7166.           || ! ieee_write_number (info, nindx)
  7167.           || ! ieee_write_number (info, 0)
  7168.           || ! ieee_write_number (info, 62)
  7169.           || ! ieee_write_number (info, 80)
  7170.           || ! ieee_write_number (info, refcount + 3)
  7171.           || ! ieee_write_asn (info, nindx, 'B')
  7172.           || ! ieee_write_atn65 (info, nindx, info->fnname)
  7173.           || ! ieee_write_asn (info, nindx, 0))
  7174.         return FALSE;
  7175.       for (m = info->pending_parms, varindx = 1;
  7176.            m != NULL;
  7177.            m = m->next, varindx++)
  7178.         {
  7179.           if (m->referencep)
  7180.             {
  7181.               if (! ieee_write_asn (info, nindx, varindx))
  7182.                 return FALSE;
  7183.             }
  7184.         }
  7185.     }
  7186.  
  7187.   m = info->pending_parms;
  7188.   while (m != NULL)
  7189.     {
  7190.       struct ieee_pending_parm *next;
  7191.  
  7192.       next = m->next;
  7193.       free (m);
  7194.       m = next;
  7195.     }
  7196.  
  7197.   info->pending_parms = NULL;
  7198.  
  7199.   return TRUE;
  7200. }
  7201.  
  7202. /* Start a block.  If this is the first block, we output the address
  7203.    to finish the BB4 or BB6, and then output the function parameters.  */
  7204.  
  7205. static bfd_boolean
  7206. ieee_start_block (void *p, bfd_vma addr)
  7207. {
  7208.   struct ieee_handle *info = (struct ieee_handle *) p;
  7209.  
  7210.   if (! ieee_change_buffer (info, &info->vars))
  7211.     return FALSE;
  7212.  
  7213.   if (info->block_depth == 1)
  7214.     {
  7215.       if (! ieee_write_number (info, addr)
  7216.           || ! ieee_output_pending_parms (info))
  7217.         return FALSE;
  7218.     }
  7219.   else
  7220.     {
  7221.       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
  7222.           || ! ieee_write_byte (info, 6)
  7223.           || ! ieee_write_number (info, 0)
  7224.           || ! ieee_write_id (info, "")
  7225.           || ! ieee_write_number (info, 0)
  7226.           || ! ieee_write_number (info, 0)
  7227.           || ! ieee_write_number (info, addr))
  7228.         return FALSE;
  7229.     }
  7230.  
  7231.   if (! ieee_start_range (info, addr))
  7232.     return FALSE;
  7233.  
  7234.   ++info->block_depth;
  7235.  
  7236.   return TRUE;
  7237. }
  7238.  
  7239. /* End a block.  */
  7240.  
  7241. static bfd_boolean
  7242. ieee_end_block (void *p, bfd_vma addr)
  7243. {
  7244.   struct ieee_handle *info = (struct ieee_handle *) p;
  7245.  
  7246.   /* The address we are given is the end of the block, but IEEE seems
  7247.      to want to the address of the last byte in the block, so we
  7248.      subtract one.  */
  7249.   if (! ieee_change_buffer (info, &info->vars)
  7250.       || ! ieee_write_byte (info, (int) ieee_be_record_enum)
  7251.       || ! ieee_write_number (info, addr - 1))
  7252.     return FALSE;
  7253.  
  7254.   if (! ieee_end_range (info, addr))
  7255.     return FALSE;
  7256.  
  7257.   --info->block_depth;
  7258.  
  7259.   if (addr > info->highaddr)
  7260.     info->highaddr = addr;
  7261.  
  7262.   return TRUE;
  7263. }
  7264.  
  7265. /* End a function.  */
  7266.  
  7267. static bfd_boolean
  7268. ieee_end_function (void *p)
  7269. {
  7270.   struct ieee_handle *info = (struct ieee_handle *) p;
  7271.  
  7272.   assert (info->block_depth == 1);
  7273.  
  7274.   --info->block_depth;
  7275.  
  7276.   /* Now we can finish up fntype, and add it to the typdef section.
  7277.      At this point, fntype is the 'x' type up to the argument count,
  7278.      and fnargs is the argument types.  We must add the argument
  7279.      count, and we must add the level.  FIXME: We don't record varargs
  7280.      functions correctly.  In fact, stabs debugging does not give us
  7281.      enough information to do so.  */
  7282.   if (! ieee_change_buffer (info, &info->fntype)
  7283.       || ! ieee_write_number (info, info->fnargcount)
  7284.       || ! ieee_change_buffer (info, &info->fnargs)
  7285.       || ! ieee_write_number (info, 0))
  7286.     return FALSE;
  7287.  
  7288.   /* Make sure the typdef block has been started.  */
  7289.   if (ieee_buffer_emptyp (&info->types))
  7290.     {
  7291.       if (! ieee_change_buffer (info, &info->types)
  7292.           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  7293.           || ! ieee_write_byte (info, 1)
  7294.           || ! ieee_write_number (info, 0)
  7295.           || ! ieee_write_id (info, info->modname))
  7296.         return FALSE;
  7297.     }
  7298.  
  7299.   if (! ieee_append_buffer (info, &info->types, &info->fntype)
  7300.       || ! ieee_append_buffer (info, &info->types, &info->fnargs))
  7301.     return FALSE;
  7302.  
  7303.   info->fnname = NULL;
  7304.   if (! ieee_init_buffer (info, &info->fntype)
  7305.       || ! ieee_init_buffer (info, &info->fnargs))
  7306.     return FALSE;
  7307.   info->fnargcount = 0;
  7308.  
  7309.   return TRUE;
  7310. }
  7311.  
  7312. /* Record line number information.  */
  7313.  
  7314. static bfd_boolean
  7315. ieee_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
  7316. {
  7317.   struct ieee_handle *info = (struct ieee_handle *) p;
  7318.  
  7319.   assert (info->filename != NULL);
  7320.  
  7321.   /* The HP simulator seems to get confused when more than one line is
  7322.      listed for the same address, at least if they are in different
  7323.      files.  We handle this by always listing the last line for a
  7324.      given address, since that seems to be the one that gdb uses.  */
  7325.   if (info->pending_lineno_filename != NULL
  7326.       && addr != info->pending_lineno_addr)
  7327.     {
  7328.       /* Make sure we have a line number block.  */
  7329.       if (! ieee_buffer_emptyp (&info->linenos))
  7330.         {
  7331.           if (! ieee_change_buffer (info, &info->linenos))
  7332.             return FALSE;
  7333.         }
  7334.       else
  7335.         {
  7336.           info->lineno_name_indx = info->name_indx;
  7337.           ++info->name_indx;
  7338.           if (! ieee_change_buffer (info, &info->linenos)
  7339.               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
  7340.               || ! ieee_write_byte (info, 5)
  7341.               || ! ieee_write_number (info, 0)
  7342.               || ! ieee_write_id (info, info->filename)
  7343.               || ! ieee_write_byte (info, (int) ieee_nn_record)
  7344.               || ! ieee_write_number (info, info->lineno_name_indx)
  7345.               || ! ieee_write_id (info, ""))
  7346.             return FALSE;
  7347.           info->lineno_filename = info->filename;
  7348.         }
  7349.  
  7350.       if (filename_cmp (info->pending_lineno_filename,
  7351.                         info->lineno_filename) != 0)
  7352.         {
  7353.           if (filename_cmp (info->filename, info->lineno_filename) != 0)
  7354.             {
  7355.               /* We were not in the main file.  Close the block for the
  7356.                  included file.  */
  7357.               if (! ieee_write_byte (info, (int) ieee_be_record_enum))
  7358.                 return FALSE;
  7359.               if (filename_cmp (info->filename,
  7360.                                 info->pending_lineno_filename) == 0)
  7361.                 {
  7362.                   /* We need a new NN record, and we aren't about to
  7363.                      output one.  */
  7364.                   info->lineno_name_indx = info->name_indx;
  7365.                   ++info->name_indx;
  7366.                   if (! ieee_write_byte (info, (int) ieee_nn_record)
  7367.                       || ! ieee_write_number (info, info->lineno_name_indx)
  7368.                       || ! ieee_write_id (info, ""))
  7369.                     return FALSE;
  7370.                 }
  7371.             }
  7372.           if (filename_cmp (info->filename,
  7373.                             info->pending_lineno_filename) != 0)
  7374.             {
  7375.               /* We are not changing to the main file.  Open a block for
  7376.                  the new included file.  */
  7377.               info->lineno_name_indx = info->name_indx;
  7378.               ++info->name_indx;
  7379.               if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
  7380.                   || ! ieee_write_byte (info, 5)
  7381.                   || ! ieee_write_number (info, 0)
  7382.                   || ! ieee_write_id (info, info->pending_lineno_filename)
  7383.                   || ! ieee_write_byte (info, (int) ieee_nn_record)
  7384.                   || ! ieee_write_number (info, info->lineno_name_indx)
  7385.                   || ! ieee_write_id (info, ""))
  7386.                 return FALSE;
  7387.             }
  7388.           info->lineno_filename = info->pending_lineno_filename;
  7389.         }
  7390.  
  7391.       if (! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
  7392.           || ! ieee_write_number (info, info->lineno_name_indx)
  7393.           || ! ieee_write_number (info, 0)
  7394.           || ! ieee_write_number (info, 7)
  7395.           || ! ieee_write_number (info, info->pending_lineno)
  7396.           || ! ieee_write_number (info, 0)
  7397.           || ! ieee_write_asn (info, info->lineno_name_indx,
  7398.                                info->pending_lineno_addr))
  7399.         return FALSE;
  7400.     }
  7401.  
  7402.   info->pending_lineno_filename = filename;
  7403.   info->pending_lineno = lineno;
  7404.   info->pending_lineno_addr = addr;
  7405.  
  7406.   return TRUE;
  7407. }
  7408.