Subversion Repositories Kolibri OS

Rev

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

  1. /* wrstabs.c -- Output stabs debugging information
  2.    Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor <ian@cygnus.com>.
  4.  
  5.    This file is part of GNU Binutils.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  20.    02110-1301, USA.  */
  21.  
  22. /* This file contains code which writes out stabs debugging
  23.    information.  */
  24.  
  25. #include "sysdep.h"
  26. #include <assert.h>
  27. #include "bfd.h"
  28. #include "libiberty.h"
  29. #include "filenames.h"
  30. #include "safe-ctype.h"
  31. #include "bucomm.h"
  32. #include "debug.h"
  33. #include "budbg.h"
  34. #include "aout/aout64.h"
  35. #include "aout/stab_gnu.h"
  36.  
  37. /* The size of a stabs symbol.  This presumes 32 bit values.  */
  38.  
  39. #define STAB_SYMBOL_SIZE (12)
  40.  
  41. /* An entry in a string hash table.  */
  42.  
  43. struct string_hash_entry
  44. {
  45.   struct bfd_hash_entry root;
  46.   /* Next string in this table.  */
  47.   struct string_hash_entry *next;
  48.   /* Index in string table.  */
  49.   long index;
  50.   /* Size of type if this is a typedef.  */
  51.   unsigned int size;
  52. };
  53.  
  54. /* A string hash table.  */
  55.  
  56. struct string_hash_table
  57. {
  58.   struct bfd_hash_table table;
  59. };
  60.  
  61. /* The type stack.  Each element on the stack is a string.  */
  62.  
  63. struct stab_type_stack
  64. {
  65.   /* The next element on the stack.  */
  66.   struct stab_type_stack *next;
  67.   /* This element as a string.  */
  68.   char *string;
  69.   /* The type index of this element.  */
  70.   long index;
  71.   /* The size of the type.  */
  72.   unsigned int size;
  73.   /* Whether type string defines a new type.  */
  74.   bfd_boolean definition;
  75.   /* String defining struct fields.  */
  76.   char *fields;
  77.   /* NULL terminated array of strings defining base classes for a
  78.      class.  */
  79.   char **baseclasses;
  80.   /* String defining class methods.  */
  81.   char *methods;
  82.   /* String defining vtable pointer for a class.  */
  83.   char *vtable;
  84. };
  85.  
  86. /* This structure is used to keep track of type indices for tagged
  87.    types.  */
  88.  
  89. struct stab_tag
  90. {
  91.   /* The type index.  */
  92.   long index;
  93.   /* The tag name.  */
  94.   const char *tag;
  95.   /* The kind of type.  This is set to DEBUG_KIND_ILLEGAL when the
  96.      type is defined.  */
  97.   enum debug_type_kind kind;
  98.   /* The size of the struct.  */
  99.   unsigned int size;
  100. };
  101.  
  102. /* We remember various sorts of type indices.  They are not related,
  103.    but, for convenience, we keep all the information in this
  104.    structure.  */
  105.  
  106. struct stab_type_cache
  107. {
  108.   /* The void type index.  */
  109.   long void_type;
  110.   /* Signed integer type indices, indexed by size - 1.  */
  111.   long signed_integer_types[8];
  112.   /* Unsigned integer type indices, indexed by size - 1.  */
  113.   long unsigned_integer_types[8];
  114.   /* Floating point types, indexed by size - 1.  */
  115.   long float_types[16];
  116.   /* Pointers to types, indexed by the type index.  */
  117.   long *pointer_types;
  118.   size_t pointer_types_alloc;
  119.   /* Functions returning types, indexed by the type index.  */
  120.   long *function_types;
  121.   size_t function_types_alloc;
  122.   /* References to types, indexed by the type index.  */
  123.   long *reference_types;
  124.   size_t reference_types_alloc;
  125.   /* Struct/union/class type indices, indexed by the struct id.  */
  126.   struct stab_tag *struct_types;
  127.   size_t struct_types_alloc;
  128. };
  129.  
  130. /* This is the handle passed through debug_write.  */
  131.  
  132. struct stab_write_handle
  133. {
  134.   /* The BFD.  */
  135.   bfd *abfd;
  136.   /* This buffer holds the symbols.  */
  137.   bfd_byte *symbols;
  138.   size_t symbols_size;
  139.   size_t symbols_alloc;
  140.   /* This is a list of hash table entries for the strings.  */
  141.   struct string_hash_entry *strings;
  142.   /* The last string hash table entry.  */
  143.   struct string_hash_entry *last_string;
  144.   /* The size of the strings.  */
  145.   size_t strings_size;
  146.   /* This hash table eliminates duplicate strings.  */
  147.   struct string_hash_table strhash;
  148.   /* The type stack.  */
  149.   struct stab_type_stack *type_stack;
  150.   /* The next type index.  */
  151.   long type_index;
  152.   /* The type cache.  */
  153.   struct stab_type_cache type_cache;
  154.   /* A mapping from typedef names to type indices.  */
  155.   struct string_hash_table typedef_hash;
  156.   /* If this is not -1, it is the offset to the most recent N_SO
  157.      symbol, and the value of that symbol needs to be set.  */
  158.   long so_offset;
  159.   /* If this is not -1, it is the offset to the most recent N_FUN
  160.      symbol, and the value of that symbol needs to be set.  */
  161.   long fun_offset;
  162.   /* The last text section address seen.  */
  163.   bfd_vma last_text_address;
  164.   /* The block nesting depth.  */
  165.   unsigned int nesting;
  166.   /* The function address.  */
  167.   bfd_vma fnaddr;
  168.   /* A pending LBRAC symbol.  */
  169.   bfd_vma pending_lbrac;
  170.   /* The current line number file name.  */
  171.   const char *lineno_filename;
  172. };
  173.  
  174. static struct bfd_hash_entry *string_hash_newfunc
  175.   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  176. static bfd_boolean stab_write_symbol
  177.   (struct stab_write_handle *, int, int, bfd_vma, const char *);
  178. static bfd_boolean stab_push_string
  179.   (struct stab_write_handle *, const char *, long, bfd_boolean, unsigned int);
  180. static bfd_boolean stab_push_defined_type
  181.   (struct stab_write_handle *, long, unsigned int);
  182. static char *stab_pop_type (struct stab_write_handle *);
  183. static bfd_boolean stab_modify_type
  184.   (struct stab_write_handle *, int, unsigned int, long **, size_t *);
  185. static long stab_get_struct_index
  186.   (struct stab_write_handle *, const char *, unsigned int,
  187.    enum debug_type_kind, unsigned int *);
  188. static bfd_boolean stab_class_method_var
  189.   (struct stab_write_handle *, const char *, enum debug_visibility,
  190.    bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
  191. static bfd_boolean stab_start_compilation_unit (void *, const char *);
  192. static bfd_boolean stab_start_source (void *, const char *);
  193. static bfd_boolean stab_empty_type (void *);
  194. static bfd_boolean stab_void_type (void *);
  195. static bfd_boolean stab_int_type (void *, unsigned int, bfd_boolean);
  196. static bfd_boolean stab_float_type (void *, unsigned int);
  197. static bfd_boolean stab_complex_type (void *, unsigned int);
  198. static bfd_boolean stab_bool_type (void *, unsigned int);
  199. static bfd_boolean stab_enum_type
  200.   (void *, const char *, const char **, bfd_signed_vma *);
  201. static bfd_boolean stab_pointer_type (void *);
  202. static bfd_boolean stab_function_type (void *, int, bfd_boolean);
  203. static bfd_boolean stab_reference_type (void *);
  204. static bfd_boolean stab_range_type (void *, bfd_signed_vma, bfd_signed_vma);
  205. static bfd_boolean stab_array_type
  206.   (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
  207. static bfd_boolean stab_set_type (void *, bfd_boolean);
  208. static bfd_boolean stab_offset_type (void *);
  209. static bfd_boolean stab_method_type (void *, bfd_boolean, int, bfd_boolean);
  210. static bfd_boolean stab_const_type (void *);
  211. static bfd_boolean stab_volatile_type (void *);
  212. static bfd_boolean stab_start_struct_type
  213.   (void *, const char *, unsigned int, bfd_boolean, unsigned int);
  214. static bfd_boolean stab_struct_field
  215.   (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
  216. static bfd_boolean stab_end_struct_type (void *);
  217. static bfd_boolean stab_start_class_type
  218.   (void *, const char *, unsigned int, bfd_boolean, unsigned int,
  219.    bfd_boolean, bfd_boolean);
  220. static bfd_boolean stab_class_static_member
  221.   (void *, const char *, const char *, enum debug_visibility);
  222. static bfd_boolean stab_class_baseclass
  223.   (void *, bfd_vma, bfd_boolean, enum debug_visibility);
  224. static bfd_boolean stab_class_start_method (void *, const char *);
  225. static bfd_boolean stab_class_method_variant
  226.   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
  227.    bfd_vma, bfd_boolean);
  228. static bfd_boolean stab_class_static_method_variant
  229.   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
  230. static bfd_boolean stab_class_end_method (void *);
  231. static bfd_boolean stab_end_class_type (void *);
  232. static bfd_boolean stab_typedef_type (void *, const char *);
  233. static bfd_boolean stab_tag_type
  234.   (void *, const char *, unsigned int, enum debug_type_kind);
  235. static bfd_boolean stab_typdef (void *, const char *);
  236. static bfd_boolean stab_tag (void *, const char *);
  237. static bfd_boolean stab_int_constant (void *, const char *, bfd_vma);
  238. static bfd_boolean stab_float_constant (void *, const char *, double);
  239. static bfd_boolean stab_typed_constant (void *, const char *, bfd_vma);
  240. static bfd_boolean stab_variable
  241.   (void *, const char *, enum debug_var_kind, bfd_vma);
  242. static bfd_boolean stab_start_function (void *, const char *, bfd_boolean);
  243. static bfd_boolean stab_function_parameter
  244.   (void *, const char *, enum debug_parm_kind, bfd_vma);
  245. static bfd_boolean stab_start_block (void *, bfd_vma);
  246. static bfd_boolean stab_end_block (void *, bfd_vma);
  247. static bfd_boolean stab_end_function (void *);
  248. static bfd_boolean stab_lineno (void *, const char *, unsigned long, bfd_vma);
  249.  
  250. static const struct debug_write_fns stab_fns =
  251. {
  252.   stab_start_compilation_unit,
  253.   stab_start_source,
  254.   stab_empty_type,
  255.   stab_void_type,
  256.   stab_int_type,
  257.   stab_float_type,
  258.   stab_complex_type,
  259.   stab_bool_type,
  260.   stab_enum_type,
  261.   stab_pointer_type,
  262.   stab_function_type,
  263.   stab_reference_type,
  264.   stab_range_type,
  265.   stab_array_type,
  266.   stab_set_type,
  267.   stab_offset_type,
  268.   stab_method_type,
  269.   stab_const_type,
  270.   stab_volatile_type,
  271.   stab_start_struct_type,
  272.   stab_struct_field,
  273.   stab_end_struct_type,
  274.   stab_start_class_type,
  275.   stab_class_static_member,
  276.   stab_class_baseclass,
  277.   stab_class_start_method,
  278.   stab_class_method_variant,
  279.   stab_class_static_method_variant,
  280.   stab_class_end_method,
  281.   stab_end_class_type,
  282.   stab_typedef_type,
  283.   stab_tag_type,
  284.   stab_typdef,
  285.   stab_tag,
  286.   stab_int_constant,
  287.   stab_float_constant,
  288.   stab_typed_constant,
  289.   stab_variable,
  290.   stab_start_function,
  291.   stab_function_parameter,
  292.   stab_start_block,
  293.   stab_end_block,
  294.   stab_end_function,
  295.   stab_lineno
  296. };
  297. /* Routine to create an entry in a string hash table.  */
  298.  
  299. static struct bfd_hash_entry *
  300. string_hash_newfunc (struct bfd_hash_entry *entry,
  301.                      struct bfd_hash_table *table, const char *string)
  302. {
  303.   struct string_hash_entry *ret = (struct string_hash_entry *) entry;
  304.  
  305.   /* Allocate the structure if it has not already been allocated by a
  306.      subclass.  */
  307.   if (ret == (struct string_hash_entry *) NULL)
  308.     ret = ((struct string_hash_entry *)
  309.            bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
  310.   if (ret == (struct string_hash_entry *) NULL)
  311.     return NULL;
  312.  
  313.   /* Call the allocation method of the superclass.  */
  314.   ret = ((struct string_hash_entry *)
  315.          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  316.  
  317.   if (ret)
  318.     {
  319.       /* Initialize the local fields.  */
  320.       ret->next = NULL;
  321.       ret->index = -1;
  322.       ret->size = 0;
  323.     }
  324.  
  325.   return (struct bfd_hash_entry *) ret;
  326. }
  327.  
  328. /* Look up an entry in a string hash table.  */
  329.  
  330. #define string_hash_lookup(t, string, create, copy) \
  331.   ((struct string_hash_entry *) \
  332.    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
  333.  
  334. /* Add a symbol to the stabs debugging information we are building.  */
  335.  
  336. static bfd_boolean
  337. stab_write_symbol (struct stab_write_handle *info, int type, int desc,
  338.                    bfd_vma value, const char *string)
  339. {
  340.   bfd_size_type strx;
  341.   bfd_byte sym[STAB_SYMBOL_SIZE];
  342.  
  343.   if (string == NULL)
  344.     strx = 0;
  345.   else
  346.     {
  347.       struct string_hash_entry *h;
  348.  
  349.       h = string_hash_lookup (&info->strhash, string, TRUE, TRUE);
  350.       if (h == NULL)
  351.         {
  352.           non_fatal (_("string_hash_lookup failed: %s"),
  353.                      bfd_errmsg (bfd_get_error ()));
  354.           return FALSE;
  355.         }
  356.       if (h->index != -1)
  357.         strx = h->index;
  358.       else
  359.         {
  360.           strx = info->strings_size;
  361.           h->index = strx;
  362.           if (info->last_string == NULL)
  363.             info->strings = h;
  364.           else
  365.             info->last_string->next = h;
  366.           info->last_string = h;
  367.           info->strings_size += strlen (string) + 1;
  368.         }
  369.     }
  370.  
  371.   /* This presumes 32 bit values.  */
  372.   bfd_put_32 (info->abfd, strx, sym);
  373.   bfd_put_8 (info->abfd, type, sym + 4);
  374.   bfd_put_8 (info->abfd, 0, sym + 5);
  375.   bfd_put_16 (info->abfd, desc, sym + 6);
  376.   bfd_put_32 (info->abfd, value, sym + 8);
  377.  
  378.   if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
  379.     {
  380.       info->symbols_alloc *= 2;
  381.       info->symbols = (bfd_byte *) xrealloc (info->symbols,
  382.                                              info->symbols_alloc);
  383.     }
  384.  
  385.   memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
  386.  
  387.   info->symbols_size += STAB_SYMBOL_SIZE;
  388.  
  389.   return TRUE;
  390. }
  391.  
  392. /* Push a string on to the type stack.  */
  393.  
  394. static bfd_boolean
  395. stab_push_string (struct stab_write_handle *info, const char *string,
  396.                   long tindex, bfd_boolean definition, unsigned int size)
  397. {
  398.   struct stab_type_stack *s;
  399.  
  400.   s = (struct stab_type_stack *) xmalloc (sizeof *s);
  401.   s->string = xstrdup (string);
  402.   s->index = tindex;
  403.   s->definition = definition;
  404.   s->size = size;
  405.  
  406.   s->fields = NULL;
  407.   s->baseclasses = NULL;
  408.   s->methods = NULL;
  409.   s->vtable = NULL;
  410.  
  411.   s->next = info->type_stack;
  412.   info->type_stack = s;
  413.  
  414.   return TRUE;
  415. }
  416.  
  417. /* Push a type index which has already been defined.  */
  418.  
  419. static bfd_boolean
  420. stab_push_defined_type (struct stab_write_handle *info, long tindex,
  421.                         unsigned int size)
  422. {
  423.   char buf[20];
  424.  
  425.   sprintf (buf, "%ld", tindex);
  426.   return stab_push_string (info, buf, tindex, FALSE, size);
  427. }
  428.  
  429. /* Pop a type off the type stack.  The caller is responsible for
  430.    freeing the string.  */
  431.  
  432. static char *
  433. stab_pop_type (struct stab_write_handle *info)
  434. {
  435.   struct stab_type_stack *s;
  436.   char *ret;
  437.  
  438.   s = info->type_stack;
  439.   assert (s != NULL);
  440.  
  441.   info->type_stack = s->next;
  442.  
  443.   ret = s->string;
  444.  
  445.   free (s);
  446.  
  447.   return ret;
  448. }
  449. /* The general routine to write out stabs in sections debugging
  450.    information.  This accumulates the stabs symbols and the strings in
  451.    two obstacks.  We can't easily write out the information as we go
  452.    along, because we need to know the section sizes before we can
  453.    write out the section contents.  ABFD is the BFD and DHANDLE is the
  454.    handle for the debugging information.  This sets *PSYMS to point to
  455.    the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
  456.    strings, and *PSTRINGSIZE to the size of the strings.  */
  457.  
  458. bfd_boolean
  459. write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
  460.                                         bfd_byte **psyms,
  461.                                         bfd_size_type *psymsize,
  462.                                         bfd_byte **pstrings,
  463.                                         bfd_size_type *pstringsize)
  464. {
  465.   struct stab_write_handle info;
  466.   struct string_hash_entry *h;
  467.   bfd_byte *p;
  468.  
  469.   info.abfd = abfd;
  470.  
  471.   info.symbols_size = 0;
  472.   info.symbols_alloc = 500;
  473.   info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
  474.  
  475.   info.strings = NULL;
  476.   info.last_string = NULL;
  477.   /* Reserve 1 byte for a null byte.  */
  478.   info.strings_size = 1;
  479.  
  480.   if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
  481.                             sizeof (struct string_hash_entry))
  482.       || !bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc,
  483.                                sizeof (struct string_hash_entry)))
  484.     {
  485.       non_fatal ("bfd_hash_table_init_failed: %s",
  486.                  bfd_errmsg (bfd_get_error ()));
  487.       return FALSE;
  488.     }
  489.  
  490.   info.type_stack = NULL;
  491.   info.type_index = 1;
  492.   memset (&info.type_cache, 0, sizeof info.type_cache);
  493.   info.so_offset = -1;
  494.   info.fun_offset = -1;
  495.   info.last_text_address = 0;
  496.   info.nesting = 0;
  497.   info.fnaddr = 0;
  498.   info.pending_lbrac = (bfd_vma) -1;
  499.  
  500.   /* The initial symbol holds the string size.  */
  501.   if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
  502.     return FALSE;
  503.  
  504.   /* Output an initial N_SO symbol.  */
  505.   info.so_offset = info.symbols_size;
  506.   if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
  507.     return FALSE;
  508.  
  509.   if (! debug_write (dhandle, &stab_fns, (void *) &info))
  510.     return FALSE;
  511.  
  512.   assert (info.pending_lbrac == (bfd_vma) -1);
  513.  
  514.   /* Output a trailing N_SO.  */
  515.   if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
  516.                            (const char *) NULL))
  517.     return FALSE;
  518.  
  519.   /* Put the string size in the initial symbol.  */
  520.   bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
  521.  
  522.   *psyms = info.symbols;
  523.   *psymsize = info.symbols_size;
  524.  
  525.   *pstringsize = info.strings_size;
  526.   *pstrings = (bfd_byte *) xmalloc (info.strings_size);
  527.  
  528.   p = *pstrings;
  529.   *p++ = '\0';
  530.   for (h = info.strings; h != NULL; h = h->next)
  531.     {
  532.       strcpy ((char *) p, h->root.string);
  533.       p += strlen ((char *) p) + 1;
  534.     }
  535.  
  536.   return TRUE;
  537. }
  538.  
  539. /* Start writing out information for a compilation unit.  */
  540.  
  541. static bfd_boolean
  542. stab_start_compilation_unit (void *p, const char *filename)
  543. {
  544.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  545.  
  546.   /* We would normally output an N_SO symbol here.  However, that
  547.      would force us to reset all of our type information.  I think we
  548.      will be better off just outputting an N_SOL symbol, and not
  549.      worrying about splitting information between files.  */
  550.  
  551.   info->lineno_filename = filename;
  552.  
  553.   return stab_write_symbol (info, N_SOL, 0, 0, filename);
  554. }
  555.  
  556. /* Start writing out information for a particular source file.  */
  557.  
  558. static bfd_boolean
  559. stab_start_source (void *p, const char *filename)
  560. {
  561.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  562.  
  563.   /* FIXME: The symbol's value is supposed to be the text section
  564.      address.  However, we would have to fill it in later, and gdb
  565.      doesn't care, so we don't bother with it.  */
  566.  
  567.   info->lineno_filename = filename;
  568.  
  569.   return stab_write_symbol (info, N_SOL, 0, 0, filename);
  570. }
  571.  
  572. /* Push an empty type.  This shouldn't normally happen.  We just use a
  573.    void type.  */
  574.  
  575. static bfd_boolean
  576. stab_empty_type (void *p)
  577. {
  578.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  579.  
  580.   /* We don't call stab_void_type if the type is not yet defined,
  581.      because that might screw up the typedef.  */
  582.  
  583.   if (info->type_cache.void_type != 0)
  584.     return stab_push_defined_type (info, info->type_cache.void_type, 0);
  585.   else
  586.     {
  587.       long tindex;
  588.       char buf[40];
  589.  
  590.       tindex = info->type_index;
  591.       ++info->type_index;
  592.  
  593.       sprintf (buf, "%ld=%ld", tindex, tindex);
  594.  
  595.       return stab_push_string (info, buf, tindex, FALSE, 0);
  596.     }
  597. }
  598.  
  599. /* Push a void type.  */
  600.  
  601. static bfd_boolean
  602. stab_void_type (void *p)
  603. {
  604.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  605.  
  606.   if (info->type_cache.void_type != 0)
  607.     return stab_push_defined_type (info, info->type_cache.void_type, 0);
  608.   else
  609.     {
  610.       long tindex;
  611.       char buf[40];
  612.  
  613.       tindex = info->type_index;
  614.       ++info->type_index;
  615.  
  616.       info->type_cache.void_type = tindex;
  617.  
  618.       sprintf (buf, "%ld=%ld", tindex, tindex);
  619.  
  620.       return stab_push_string (info, buf, tindex, TRUE, 0);
  621.     }
  622. }
  623.  
  624. /* Push an integer type.  */
  625.  
  626. static bfd_boolean
  627. stab_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
  628. {
  629.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  630.   long *cache;
  631.  
  632.   if (size <= 0 || (size > sizeof (long) && size != 8))
  633.     {
  634.       non_fatal (_("stab_int_type: bad size %u"), size);
  635.       return FALSE;
  636.     }
  637.  
  638.   if (unsignedp)
  639.     cache = info->type_cache.signed_integer_types;
  640.   else
  641.     cache = info->type_cache.unsigned_integer_types;
  642.  
  643.   if (cache[size - 1] != 0)
  644.     return stab_push_defined_type (info, cache[size - 1], size);
  645.   else
  646.     {
  647.       long tindex;
  648.       char buf[100];
  649.  
  650.       tindex = info->type_index;
  651.       ++info->type_index;
  652.  
  653.       cache[size - 1] = tindex;
  654.  
  655.       sprintf (buf, "%ld=r%ld;", tindex, tindex);
  656.       if (unsignedp)
  657.         {
  658.           strcat (buf, "0;");
  659.           if (size < sizeof (long))
  660.             sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
  661.           else if (size == sizeof (long))
  662.             strcat (buf, "-1;");
  663.           else if (size == 8)
  664.             strcat (buf, "01777777777777777777777;");
  665.           else
  666.             abort ();
  667.         }
  668.       else
  669.         {
  670.           if (size <= sizeof (long))
  671.             sprintf (buf + strlen (buf), "%ld;%ld;",
  672.                      (long) - ((unsigned long) 1 << (size * 8 - 1)),
  673.                      (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
  674.           else if (size == 8)
  675.             strcat (buf, "01000000000000000000000;0777777777777777777777;");
  676.           else
  677.             abort ();
  678.         }
  679.  
  680.       return stab_push_string (info, buf, tindex, TRUE, size);
  681.     }
  682. }
  683.  
  684. /* Push a floating point type.  */
  685.  
  686. static bfd_boolean
  687. stab_float_type (void *p, unsigned int size)
  688. {
  689.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  690.  
  691.   if (size > 0
  692.       && size - 1 < (sizeof info->type_cache.float_types
  693.                      / sizeof info->type_cache.float_types[0])
  694.       && info->type_cache.float_types[size - 1] != 0)
  695.     return stab_push_defined_type (info,
  696.                                    info->type_cache.float_types[size - 1],
  697.                                    size);
  698.   else
  699.     {
  700.       long tindex;
  701.       char *int_type;
  702.       char buf[50];
  703.  
  704.       /* Floats are defined as a subrange of int.  */
  705.       if (! stab_int_type (info, 4, FALSE))
  706.         return FALSE;
  707.       int_type = stab_pop_type (info);
  708.  
  709.       tindex = info->type_index;
  710.       ++info->type_index;
  711.  
  712.       if (size > 0
  713.           && size - 1 < (sizeof info->type_cache.float_types
  714.                          / sizeof info->type_cache.float_types[0]))
  715.         info->type_cache.float_types[size - 1] = tindex;
  716.  
  717.       sprintf (buf, "%ld=r%s;%u;0;", tindex, int_type, size);
  718.  
  719.       free (int_type);
  720.  
  721.       return stab_push_string (info, buf, tindex, TRUE, size);
  722.     }
  723. }
  724.  
  725. /* Push a complex type.  */
  726.  
  727. static bfd_boolean
  728. stab_complex_type (void *p, unsigned int size)
  729. {
  730.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  731.   char buf[50];
  732.   long tindex;
  733.  
  734.   tindex = info->type_index;
  735.   ++info->type_index;
  736.  
  737.   sprintf (buf, "%ld=r%ld;%u;0;", tindex, tindex, size);
  738.  
  739.   return stab_push_string (info, buf, tindex, TRUE, size * 2);
  740. }
  741.  
  742. /* Push a bfd_boolean type.  We use an XCOFF predefined type, since gdb
  743.    always recognizes them.  */
  744.  
  745. static bfd_boolean
  746. stab_bool_type (void *p, unsigned int size)
  747. {
  748.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  749.   long tindex;
  750.  
  751.   switch (size)
  752.     {
  753.     case 1:
  754.       tindex = -21;
  755.       break;
  756.  
  757.     case 2:
  758.       tindex = -22;
  759.       break;
  760.  
  761.     default:
  762.     case 4:
  763.       tindex = -16;
  764.       break;
  765.  
  766.     case 8:
  767.       tindex = -33;
  768.       break;
  769.     }
  770.  
  771.   return stab_push_defined_type (info, tindex, size);
  772. }
  773.  
  774. /* Push an enum type.  */
  775.  
  776. static bfd_boolean
  777. stab_enum_type (void *p, const char *tag, const char **names,
  778.                 bfd_signed_vma *vals)
  779. {
  780.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  781.   size_t len;
  782.   const char **pn;
  783.   char *buf;
  784.   long tindex = 0;
  785.   bfd_signed_vma *pv;
  786.  
  787.   if (names == NULL)
  788.     {
  789.       assert (tag != NULL);
  790.  
  791.       buf = (char *) xmalloc (10 + strlen (tag));
  792.       sprintf (buf, "xe%s:", tag);
  793.       /* FIXME: The size is just a guess.  */
  794.       if (! stab_push_string (info, buf, 0, FALSE, 4))
  795.         return FALSE;
  796.       free (buf);
  797.       return TRUE;
  798.     }
  799.  
  800.   len = 10;
  801.   if (tag != NULL)
  802.     len += strlen (tag);
  803.   for (pn = names; *pn != NULL; pn++)
  804.     len += strlen (*pn) + 20;
  805.  
  806.   buf = (char *) xmalloc (len);
  807.  
  808.   if (tag == NULL)
  809.     strcpy (buf, "e");
  810.   else
  811.     {
  812.       tindex = info->type_index;
  813.       ++info->type_index;
  814.       sprintf (buf, "%s:T%ld=e", tag, tindex);
  815.     }
  816.  
  817.   for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
  818.     sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
  819.   strcat (buf, ";");
  820.  
  821.   if (tag == NULL)
  822.     {
  823.       /* FIXME: The size is just a guess.  */
  824.       if (! stab_push_string (info, buf, 0, FALSE, 4))
  825.         return FALSE;
  826.     }
  827.   else
  828.     {
  829.       /* FIXME: The size is just a guess.  */
  830.       if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
  831.           || ! stab_push_defined_type (info, tindex, 4))
  832.         return FALSE;
  833.     }
  834.  
  835.   free (buf);
  836.  
  837.   return TRUE;
  838. }
  839.  
  840. /* Push a modification of the top type on the stack.  Cache the
  841.    results in CACHE and CACHE_ALLOC.  */
  842.  
  843. static bfd_boolean
  844. stab_modify_type (struct stab_write_handle *info, int mod,
  845.                   unsigned int size, long **cache, size_t *cache_alloc)
  846. {
  847.   long targindex;
  848.   long tindex;
  849.   char *s, *buf;
  850.  
  851.   assert (info->type_stack != NULL);
  852.   targindex = info->type_stack->index;
  853.  
  854.   if (targindex <= 0
  855.       || cache == NULL)
  856.     {
  857.       bfd_boolean definition;
  858.  
  859.       /* Either the target type has no index, or we aren't caching
  860.          this modifier.  Either way we have no way of recording the
  861.          new type, so we don't bother to define one.  */
  862.       definition = info->type_stack->definition;
  863.       s = stab_pop_type (info);
  864.       buf = (char *) xmalloc (strlen (s) + 2);
  865.       sprintf (buf, "%c%s", mod, s);
  866.       free (s);
  867.       if (! stab_push_string (info, buf, 0, definition, size))
  868.         return FALSE;
  869.       free (buf);
  870.     }
  871.   else
  872.     {
  873.       if ((size_t) targindex >= *cache_alloc)
  874.         {
  875.           size_t alloc;
  876.  
  877.           alloc = *cache_alloc;
  878.           if (alloc == 0)
  879.             alloc = 10;
  880.           while ((size_t) targindex >= alloc)
  881.             alloc *= 2;
  882.           *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
  883.           memset (*cache + *cache_alloc, 0,
  884.                   (alloc - *cache_alloc) * sizeof (long));
  885.           *cache_alloc = alloc;
  886.         }
  887.  
  888.       tindex = (*cache)[targindex];
  889.       if (tindex != 0 && ! info->type_stack->definition)
  890.         {
  891.           /* We have already defined a modification of this type, and
  892.              the entry on the type stack is not a definition, so we
  893.              can safely discard it (we may have a definition on the
  894.              stack, even if we already defined a modification, if it
  895.              is a struct which we did not define at the time it was
  896.              referenced).  */
  897.           free (stab_pop_type (info));
  898.           if (! stab_push_defined_type (info, tindex, size))
  899.             return FALSE;
  900.         }
  901.       else
  902.         {
  903.           tindex = info->type_index;
  904.           ++info->type_index;
  905.  
  906.           s = stab_pop_type (info);
  907.           buf = (char *) xmalloc (strlen (s) + 20);
  908.           sprintf (buf, "%ld=%c%s", tindex, mod, s);
  909.           free (s);
  910.  
  911.           (*cache)[targindex] = tindex;
  912.  
  913.           if (! stab_push_string (info, buf, tindex, TRUE, size))
  914.             return FALSE;
  915.  
  916.           free (buf);
  917.         }
  918.     }
  919.  
  920.   return TRUE;
  921. }
  922.  
  923. /* Push a pointer type.  */
  924.  
  925. static bfd_boolean
  926. stab_pointer_type (void *p)
  927. {
  928.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  929.  
  930.   /* FIXME: The size should depend upon the architecture.  */
  931.   return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
  932.                            &info->type_cache.pointer_types_alloc);
  933. }
  934.  
  935. /* Push a function type.  */
  936.  
  937. static bfd_boolean
  938. stab_function_type (void *p, int argcount,
  939.                     bfd_boolean varargs ATTRIBUTE_UNUSED)
  940. {
  941.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  942.   int i;
  943.  
  944.   /* We have no way to represent the argument types, so we just
  945.      discard them.  However, if they define new types, we must output
  946.      them.  We do this by producing empty typedefs.  */
  947.   for (i = 0; i < argcount; i++)
  948.     {
  949.       if (! info->type_stack->definition)
  950.         free (stab_pop_type (info));
  951.       else
  952.         {
  953.           char *s, *buf;
  954.  
  955.           s = stab_pop_type (info);
  956.  
  957.           buf = (char *) xmalloc (strlen (s) + 3);
  958.           sprintf (buf, ":t%s", s);
  959.           free (s);
  960.  
  961.           if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  962.             return FALSE;
  963.  
  964.           free (buf);
  965.         }
  966.     }
  967.  
  968.   return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
  969.                            &info->type_cache.function_types_alloc);
  970. }
  971.  
  972. /* Push a reference type.  */
  973.  
  974. static bfd_boolean
  975. stab_reference_type (void *p)
  976. {
  977.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  978.  
  979.   /* FIXME: The size should depend upon the architecture.  */
  980.   return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
  981.                            &info->type_cache.reference_types_alloc);
  982. }
  983.  
  984. /* Push a range type.  */
  985.  
  986. static bfd_boolean
  987. stab_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
  988. {
  989.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  990.   bfd_boolean definition;
  991.   unsigned int size;
  992.   char *s, *buf;
  993.  
  994.   definition = info->type_stack->definition;
  995.   size = info->type_stack->size;
  996.  
  997.   s = stab_pop_type (info);
  998.   buf = (char *) xmalloc (strlen (s) + 100);
  999.   sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
  1000.   free (s);
  1001.  
  1002.   if (! stab_push_string (info, buf, 0, definition, size))
  1003.     return FALSE;
  1004.  
  1005.   free (buf);
  1006.  
  1007.   return TRUE;
  1008. }
  1009.  
  1010. /* Push an array type.  */
  1011.  
  1012. static bfd_boolean
  1013. stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
  1014.                  bfd_boolean stringp)
  1015. {
  1016.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1017.   bfd_boolean definition;
  1018.   unsigned int element_size;
  1019.   char *range, *element, *buf;
  1020.   long tindex;
  1021.   unsigned int size;
  1022.  
  1023.   definition = info->type_stack->definition;
  1024.   range = stab_pop_type (info);
  1025.  
  1026.   definition = definition || info->type_stack->definition;
  1027.   element_size = info->type_stack->size;
  1028.   element = stab_pop_type (info);
  1029.  
  1030.   buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
  1031.  
  1032.   if (! stringp)
  1033.     {
  1034.       tindex = 0;
  1035.       *buf = '\0';
  1036.     }
  1037.   else
  1038.     {
  1039.       /* We need to define a type in order to include the string
  1040.          attribute.  */
  1041.       tindex = info->type_index;
  1042.       ++info->type_index;
  1043.       definition = TRUE;
  1044.       sprintf (buf, "%ld=@S;", tindex);
  1045.     }
  1046.  
  1047.   sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
  1048.            range, (long) low, (long) high, element);
  1049.   free (range);
  1050.   free (element);
  1051.  
  1052.   if (high < low)
  1053.     size = 0;
  1054.   else
  1055.     size = element_size * ((high - low) + 1);
  1056.   if (! stab_push_string (info, buf, tindex, definition, size))
  1057.     return FALSE;
  1058.  
  1059.   free (buf);
  1060.  
  1061.   return TRUE;
  1062. }
  1063.  
  1064. /* Push a set type.  */
  1065.  
  1066. static bfd_boolean
  1067. stab_set_type (void *p, bfd_boolean bitstringp)
  1068. {
  1069.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1070.   bfd_boolean definition;
  1071.   char *s, *buf;
  1072.   long tindex;
  1073.  
  1074.   definition = info->type_stack->definition;
  1075.  
  1076.   s = stab_pop_type (info);
  1077.   buf = (char *) xmalloc (strlen (s) + 30);
  1078.  
  1079.   if (! bitstringp)
  1080.     {
  1081.       *buf = '\0';
  1082.       tindex = 0;
  1083.     }
  1084.   else
  1085.     {
  1086.       /* We need to define a type in order to include the string
  1087.          attribute.  */
  1088.       tindex = info->type_index;
  1089.       ++info->type_index;
  1090.       definition = TRUE;
  1091.       sprintf (buf, "%ld=@S;", tindex);
  1092.     }
  1093.  
  1094.   sprintf (buf + strlen (buf), "S%s", s);
  1095.   free (s);
  1096.  
  1097.   if (! stab_push_string (info, buf, tindex, definition, 0))
  1098.     return FALSE;
  1099.  
  1100.   free (buf);
  1101.  
  1102.   return TRUE;
  1103. }
  1104.  
  1105. /* Push an offset type.  */
  1106.  
  1107. static bfd_boolean
  1108. stab_offset_type (void *p)
  1109. {
  1110.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1111.   bfd_boolean definition;
  1112.   char *target, *base, *buf;
  1113.  
  1114.   definition = info->type_stack->definition;
  1115.   target = stab_pop_type (info);
  1116.  
  1117.   definition = definition || info->type_stack->definition;
  1118.   base = stab_pop_type (info);
  1119.  
  1120.   buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
  1121.   sprintf (buf, "@%s,%s", base, target);
  1122.   free (base);
  1123.   free (target);
  1124.  
  1125.   if (! stab_push_string (info, buf, 0, definition, 0))
  1126.     return FALSE;
  1127.  
  1128.   free (buf);
  1129.  
  1130.   return TRUE;
  1131. }
  1132.  
  1133. /* Push a method type.  */
  1134.  
  1135. static bfd_boolean
  1136. stab_method_type (void *p, bfd_boolean domainp, int argcount,
  1137.                   bfd_boolean varargs)
  1138. {
  1139.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1140.   bfd_boolean definition;
  1141.   char *domain, *return_type, *buf;
  1142.   char **args;
  1143.   int i;
  1144.   size_t len;
  1145.  
  1146.   /* We don't bother with stub method types, because that would
  1147.      require a mangler for C++ argument types.  This will waste space
  1148.      in the debugging output.  */
  1149.  
  1150.   /* We need a domain.  I'm not sure DOMAINP can ever be false,
  1151.      anyhow.  */
  1152.   if (! domainp)
  1153.     {
  1154.       if (! stab_empty_type (p))
  1155.         return FALSE;
  1156.     }
  1157.  
  1158.   definition = info->type_stack->definition;
  1159.   domain = stab_pop_type (info);
  1160.  
  1161.   /* A non-varargs function is indicated by making the last parameter
  1162.      type be void.  */
  1163.  
  1164.   if (argcount < 0)
  1165.     {
  1166.       args = NULL;
  1167.       argcount = 0;
  1168.     }
  1169.   else if (argcount == 0)
  1170.     {
  1171.       if (varargs)
  1172.         args = NULL;
  1173.       else
  1174.         {
  1175.           args = (char **) xmalloc (1 * sizeof (*args));
  1176.           if (! stab_empty_type (p))
  1177.             return FALSE;
  1178.           definition = definition || info->type_stack->definition;
  1179.           args[0] = stab_pop_type (info);
  1180.           argcount = 1;
  1181.         }
  1182.     }
  1183.   else
  1184.     {
  1185.       args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
  1186.       for (i = argcount - 1; i >= 0; i--)
  1187.         {
  1188.           definition = definition || info->type_stack->definition;
  1189.           args[i] = stab_pop_type (info);
  1190.         }
  1191.       if (! varargs)
  1192.         {
  1193.           if (! stab_empty_type (p))
  1194.             return FALSE;
  1195.           definition = definition || info->type_stack->definition;
  1196.           args[argcount] = stab_pop_type (info);
  1197.           ++argcount;
  1198.         }
  1199.     }
  1200.  
  1201.   definition = definition || info->type_stack->definition;
  1202.   return_type = stab_pop_type (info);
  1203.  
  1204.   len = strlen (domain) + strlen (return_type) + 10;
  1205.   for (i = 0; i < argcount; i++)
  1206.     len += strlen (args[i]);
  1207.  
  1208.   buf = (char *) xmalloc (len);
  1209.  
  1210.   sprintf (buf, "#%s,%s", domain, return_type);
  1211.   free (domain);
  1212.   free (return_type);
  1213.   for (i = 0; i < argcount; i++)
  1214.     {
  1215.       strcat (buf, ",");
  1216.       strcat (buf, args[i]);
  1217.       free (args[i]);
  1218.     }
  1219.   strcat (buf, ";");
  1220.  
  1221.   if (args != NULL)
  1222.     free (args);
  1223.  
  1224.   if (! stab_push_string (info, buf, 0, definition, 0))
  1225.     return FALSE;
  1226.  
  1227.   free (buf);
  1228.  
  1229.   return TRUE;
  1230. }
  1231.  
  1232. /* Push a const version of a type.  */
  1233.  
  1234. static bfd_boolean
  1235. stab_const_type (void *p)
  1236. {
  1237.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1238.  
  1239.   return stab_modify_type (info, 'k', info->type_stack->size,
  1240.                            (long **) NULL, (size_t *) NULL);
  1241. }
  1242.  
  1243. /* Push a volatile version of a type.  */
  1244.  
  1245. static bfd_boolean
  1246. stab_volatile_type (void *p)
  1247. {
  1248.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1249.  
  1250.   return stab_modify_type (info, 'B', info->type_stack->size,
  1251.                            (long **) NULL, (size_t *) NULL);
  1252. }
  1253.  
  1254. /* Get the type index to use for a struct/union/class ID.  This should
  1255.    return -1 if it fails.  */
  1256.  
  1257. static long
  1258. stab_get_struct_index (struct stab_write_handle *info, const char *tag,
  1259.                        unsigned int id, enum debug_type_kind kind,
  1260.                        unsigned int *psize)
  1261. {
  1262.   if (id >= info->type_cache.struct_types_alloc)
  1263.     {
  1264.       size_t alloc;
  1265.  
  1266.       alloc = info->type_cache.struct_types_alloc;
  1267.       if (alloc == 0)
  1268.         alloc = 10;
  1269.       while (id >= alloc)
  1270.         alloc *= 2;
  1271.       info->type_cache.struct_types =
  1272.         (struct stab_tag *) xrealloc (info->type_cache.struct_types,
  1273.                                       alloc * sizeof (struct stab_tag));
  1274.       memset ((info->type_cache.struct_types
  1275.                + info->type_cache.struct_types_alloc),
  1276.               0,
  1277.               ((alloc - info->type_cache.struct_types_alloc)
  1278.                * sizeof (struct stab_tag)));
  1279.       info->type_cache.struct_types_alloc = alloc;
  1280.     }
  1281.  
  1282.   if (info->type_cache.struct_types[id].index == 0)
  1283.     {
  1284.       info->type_cache.struct_types[id].index = info->type_index;
  1285.       ++info->type_index;
  1286.       info->type_cache.struct_types[id].tag = tag;
  1287.       info->type_cache.struct_types[id].kind = kind;
  1288.     }
  1289.  
  1290.   if (kind == DEBUG_KIND_ILLEGAL)
  1291.     {
  1292.       /* This is a definition of the struct.  */
  1293.       info->type_cache.struct_types[id].kind = kind;
  1294.       info->type_cache.struct_types[id].size = *psize;
  1295.     }
  1296.   else
  1297.     *psize = info->type_cache.struct_types[id].size;
  1298.  
  1299.   return info->type_cache.struct_types[id].index;
  1300. }
  1301.  
  1302. /* Start outputting a struct.  We ignore the tag, and handle it in
  1303.    stab_tag.  */
  1304.  
  1305. static bfd_boolean
  1306. stab_start_struct_type (void *p, const char *tag, unsigned int id,
  1307.                         bfd_boolean structp, unsigned int size)
  1308. {
  1309.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1310.   long tindex;
  1311.   bfd_boolean definition;
  1312.   char buf[40];
  1313.  
  1314.   if (id == 0)
  1315.     {
  1316.       tindex = 0;
  1317.       *buf = '\0';
  1318.       definition = FALSE;
  1319.     }
  1320.   else
  1321.     {
  1322.       tindex = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
  1323.                                      &size);
  1324.       if (tindex < 0)
  1325.         return FALSE;
  1326.       sprintf (buf, "%ld=", tindex);
  1327.       definition = TRUE;
  1328.     }
  1329.  
  1330.   sprintf (buf + strlen (buf), "%c%u",
  1331.            structp ? 's' : 'u',
  1332.            size);
  1333.  
  1334.   if (! stab_push_string (info, buf, tindex, definition, size))
  1335.     return FALSE;
  1336.  
  1337.   info->type_stack->fields = (char *) xmalloc (1);
  1338.   info->type_stack->fields[0] = '\0';
  1339.  
  1340.   return TRUE;
  1341. }
  1342.  
  1343. /* Add a field to a struct.  */
  1344.  
  1345. static bfd_boolean
  1346. stab_struct_field (void *p, const char *name, bfd_vma bitpos,
  1347.                    bfd_vma bitsize, enum debug_visibility visibility)
  1348. {
  1349.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1350.   bfd_boolean definition;
  1351.   unsigned int size;
  1352.   char *s, *n;
  1353.   const char *vis;
  1354.  
  1355.   definition = info->type_stack->definition;
  1356.   size = info->type_stack->size;
  1357.   s = stab_pop_type (info);
  1358.  
  1359.   /* Add this field to the end of the current struct fields, which is
  1360.      currently on the top of the stack.  */
  1361.  
  1362.   assert (info->type_stack->fields != NULL);
  1363.   n = (char *) xmalloc (strlen (info->type_stack->fields)
  1364.                         + strlen (name)
  1365.                         + strlen (s)
  1366.                         + 50);
  1367.  
  1368.   switch (visibility)
  1369.     {
  1370.     default:
  1371.       abort ();
  1372.  
  1373.     case DEBUG_VISIBILITY_PUBLIC:
  1374.       vis = "";
  1375.       break;
  1376.  
  1377.     case DEBUG_VISIBILITY_PRIVATE:
  1378.       vis = "/0";
  1379.       break;
  1380.  
  1381.     case DEBUG_VISIBILITY_PROTECTED:
  1382.       vis = "/1";
  1383.       break;
  1384.     }
  1385.  
  1386.   if (bitsize == 0)
  1387.     {
  1388.       bitsize = size * 8;
  1389.       if (bitsize == 0)
  1390.         non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
  1391.                    bfd_get_filename (info->abfd), name);
  1392.     }
  1393.  
  1394.   sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
  1395.            (long) bitpos, (long) bitsize);
  1396.  
  1397.   free (info->type_stack->fields);
  1398.   info->type_stack->fields = n;
  1399.  
  1400.   if (definition)
  1401.     info->type_stack->definition = TRUE;
  1402.  
  1403.   return TRUE;
  1404. }
  1405.  
  1406. /* Finish up a struct.  */
  1407.  
  1408. static bfd_boolean
  1409. stab_end_struct_type (void *p)
  1410. {
  1411.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1412.   bfd_boolean definition;
  1413.   long tindex;
  1414.   unsigned int size;
  1415.   char *fields, *first, *buf;
  1416.  
  1417.   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1418.  
  1419.   definition = info->type_stack->definition;
  1420.   tindex = info->type_stack->index;
  1421.   size = info->type_stack->size;
  1422.   fields = info->type_stack->fields;
  1423.   first = stab_pop_type (info);
  1424.  
  1425.   buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
  1426.   sprintf (buf, "%s%s;", first, fields);
  1427.   free (first);
  1428.   free (fields);
  1429.  
  1430.   if (! stab_push_string (info, buf, tindex, definition, size))
  1431.     return FALSE;
  1432.  
  1433.   free (buf);
  1434.  
  1435.   return TRUE;
  1436. }
  1437.  
  1438. /* Start outputting a class.  */
  1439.  
  1440. static bfd_boolean
  1441. stab_start_class_type (void *p, const char *tag, unsigned int id, bfd_boolean structp, unsigned int size, bfd_boolean vptr, bfd_boolean ownvptr)
  1442. {
  1443.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1444.   bfd_boolean definition;
  1445.   char *vstring;
  1446.  
  1447.   if (! vptr || ownvptr)
  1448.     {
  1449.       definition = FALSE;
  1450.       vstring = NULL;
  1451.     }
  1452.   else
  1453.     {
  1454.       definition = info->type_stack->definition;
  1455.       vstring = stab_pop_type (info);
  1456.     }
  1457.  
  1458.   if (! stab_start_struct_type (p, tag, id, structp, size))
  1459.     return FALSE;
  1460.  
  1461.   if (vptr)
  1462.     {
  1463.       char *vtable;
  1464.  
  1465.       if (ownvptr)
  1466.         {
  1467.           assert (info->type_stack->index > 0);
  1468.           vtable = (char *) xmalloc (20);
  1469.           sprintf (vtable, "~%%%ld", info->type_stack->index);
  1470.         }
  1471.       else
  1472.         {
  1473.           vtable = (char *) xmalloc (strlen (vstring) + 3);
  1474.           sprintf (vtable, "~%%%s", vstring);
  1475.           free (vstring);
  1476.         }
  1477.  
  1478.       info->type_stack->vtable = vtable;
  1479.     }
  1480.  
  1481.   if (definition)
  1482.     info->type_stack->definition = TRUE;
  1483.  
  1484.   return TRUE;
  1485. }
  1486.  
  1487. /* Add a static member to the class on the type stack.  */
  1488.  
  1489. static bfd_boolean
  1490. stab_class_static_member (void *p, const char *name, const char *physname,
  1491.                           enum debug_visibility visibility)
  1492. {
  1493.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1494.   bfd_boolean definition;
  1495.   char *s, *n;
  1496.   const char *vis;
  1497.  
  1498.   definition = info->type_stack->definition;
  1499.   s = stab_pop_type (info);
  1500.  
  1501.   /* Add this field to the end of the current struct fields, which is
  1502.      currently on the top of the stack.  */
  1503.  
  1504.   assert (info->type_stack->fields != NULL);
  1505.   n = (char *) xmalloc (strlen (info->type_stack->fields)
  1506.                         + strlen (name)
  1507.                         + strlen (s)
  1508.                         + strlen (physname)
  1509.                         + 10);
  1510.  
  1511.   switch (visibility)
  1512.     {
  1513.     default:
  1514.       abort ();
  1515.  
  1516.     case DEBUG_VISIBILITY_PUBLIC:
  1517.       vis = "";
  1518.       break;
  1519.  
  1520.     case DEBUG_VISIBILITY_PRIVATE:
  1521.       vis = "/0";
  1522.       break;
  1523.  
  1524.     case DEBUG_VISIBILITY_PROTECTED:
  1525.       vis = "/1";
  1526.       break;
  1527.     }
  1528.  
  1529.   sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
  1530.            physname);
  1531.  
  1532.   free (info->type_stack->fields);
  1533.   info->type_stack->fields = n;
  1534.  
  1535.   if (definition)
  1536.     info->type_stack->definition = TRUE;
  1537.  
  1538.   return TRUE;
  1539. }
  1540.  
  1541. /* Add a base class to the class on the type stack.  */
  1542.  
  1543. static bfd_boolean
  1544. stab_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
  1545.                       enum debug_visibility visibility)
  1546. {
  1547.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1548.   bfd_boolean definition;
  1549.   char *s;
  1550.   char *buf;
  1551.   unsigned int c;
  1552.   char **baseclasses;
  1553.  
  1554.   definition = info->type_stack->definition;
  1555.   s = stab_pop_type (info);
  1556.  
  1557.   /* Build the base class specifier.  */
  1558.  
  1559.   buf = (char *) xmalloc (strlen (s) + 25);
  1560.   buf[0] = is_virtual ? '1' : '0';
  1561.   switch (visibility)
  1562.     {
  1563.     default:
  1564.       abort ();
  1565.  
  1566.     case DEBUG_VISIBILITY_PRIVATE:
  1567.       buf[1] = '0';
  1568.       break;
  1569.  
  1570.     case DEBUG_VISIBILITY_PROTECTED:
  1571.       buf[1] = '1';
  1572.       break;
  1573.  
  1574.     case DEBUG_VISIBILITY_PUBLIC:
  1575.       buf[1] = '2';
  1576.       break;
  1577.     }
  1578.  
  1579.   sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
  1580.   free (s);
  1581.  
  1582.   /* Add the new baseclass to the existing ones.  */
  1583.  
  1584.   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1585.  
  1586.   if (info->type_stack->baseclasses == NULL)
  1587.     c = 0;
  1588.   else
  1589.     {
  1590.       c = 0;
  1591.       while (info->type_stack->baseclasses[c] != NULL)
  1592.         ++c;
  1593.     }
  1594.  
  1595.   baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
  1596.                                     (c + 2) * sizeof (*baseclasses));
  1597.   baseclasses[c] = buf;
  1598.   baseclasses[c + 1] = NULL;
  1599.  
  1600.   info->type_stack->baseclasses = baseclasses;
  1601.  
  1602.   if (definition)
  1603.     info->type_stack->definition = TRUE;
  1604.  
  1605.   return TRUE;
  1606. }
  1607.  
  1608. /* Start adding a method to the class on the type stack.  */
  1609.  
  1610. static bfd_boolean
  1611. stab_class_start_method (void *p, const char *name)
  1612. {
  1613.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1614.   char *m;
  1615.  
  1616.   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1617.  
  1618.   if (info->type_stack->methods == NULL)
  1619.     {
  1620.       m = (char *) xmalloc (strlen (name) + 3);
  1621.       *m = '\0';
  1622.     }
  1623.   else
  1624.     {
  1625.       m = (char *) xrealloc (info->type_stack->methods,
  1626.                              (strlen (info->type_stack->methods)
  1627.                               + strlen (name)
  1628.                               + 4));
  1629.     }
  1630.  
  1631.   sprintf (m + strlen (m), "%s::", name);
  1632.  
  1633.   info->type_stack->methods = m;
  1634.  
  1635.   return TRUE;
  1636. }
  1637.  
  1638. /* Add a variant, either static or not, to the current method.  */
  1639.  
  1640. static bfd_boolean
  1641. stab_class_method_var (struct stab_write_handle *info, const char *physname,
  1642.                        enum debug_visibility visibility,
  1643.                        bfd_boolean staticp, bfd_boolean constp,
  1644.                        bfd_boolean volatilep, bfd_vma voffset,
  1645.                        bfd_boolean contextp)
  1646. {
  1647.   bfd_boolean definition;
  1648.   char *type;
  1649.   char *context = NULL;
  1650.   char visc, qualc, typec;
  1651.  
  1652.   definition = info->type_stack->definition;
  1653.   type = stab_pop_type (info);
  1654.  
  1655.   if (contextp)
  1656.     {
  1657.       definition = definition || info->type_stack->definition;
  1658.       context = stab_pop_type (info);
  1659.     }
  1660.  
  1661.   assert (info->type_stack != NULL && info->type_stack->methods != NULL);
  1662.  
  1663.   switch (visibility)
  1664.     {
  1665.     default:
  1666.       abort ();
  1667.  
  1668.     case DEBUG_VISIBILITY_PRIVATE:
  1669.       visc = '0';
  1670.       break;
  1671.  
  1672.     case DEBUG_VISIBILITY_PROTECTED:
  1673.       visc = '1';
  1674.       break;
  1675.  
  1676.     case DEBUG_VISIBILITY_PUBLIC:
  1677.       visc = '2';
  1678.       break;
  1679.     }
  1680.  
  1681.   if (constp)
  1682.     {
  1683.       if (volatilep)
  1684.         qualc = 'D';
  1685.       else
  1686.         qualc = 'B';
  1687.     }
  1688.   else
  1689.     {
  1690.       if (volatilep)
  1691.         qualc = 'C';
  1692.       else
  1693.         qualc = 'A';
  1694.     }
  1695.  
  1696.   if (staticp)
  1697.     typec = '?';
  1698.   else if (! contextp)
  1699.     typec = '.';
  1700.   else
  1701.     typec = '*';
  1702.  
  1703.   info->type_stack->methods =
  1704.     (char *) xrealloc (info->type_stack->methods,
  1705.                        (strlen (info->type_stack->methods)
  1706.                         + strlen (type)
  1707.                         + strlen (physname)
  1708.                         + (contextp ? strlen (context) : 0)
  1709.                         + 40));
  1710.  
  1711.   sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
  1712.            "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
  1713.   free (type);
  1714.  
  1715.   if (contextp)
  1716.     {
  1717.       sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
  1718.                "%ld;%s;", (long) voffset, context);
  1719.       free (context);
  1720.     }
  1721.  
  1722.   if (definition)
  1723.     info->type_stack->definition = TRUE;
  1724.  
  1725.   return TRUE;
  1726. }
  1727.  
  1728. /* Add a variant to the current method.  */
  1729.  
  1730. static bfd_boolean
  1731. stab_class_method_variant (void *p, const char *physname,
  1732.                            enum debug_visibility visibility,
  1733.                            bfd_boolean constp, bfd_boolean volatilep,
  1734.                            bfd_vma voffset, bfd_boolean contextp)
  1735. {
  1736.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1737.  
  1738.   return stab_class_method_var (info, physname, visibility, FALSE, constp,
  1739.                                 volatilep, voffset, contextp);
  1740. }
  1741.  
  1742. /* Add a static variant to the current method.  */
  1743.  
  1744. static bfd_boolean
  1745. stab_class_static_method_variant (void *p, const char *physname,
  1746.                                   enum debug_visibility visibility,
  1747.                                   bfd_boolean constp, bfd_boolean volatilep)
  1748. {
  1749.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1750.  
  1751.   return stab_class_method_var (info, physname, visibility, TRUE, constp,
  1752.                                 volatilep, 0, FALSE);
  1753. }
  1754.  
  1755. /* Finish up a method.  */
  1756.  
  1757. static bfd_boolean
  1758. stab_class_end_method (void *p)
  1759. {
  1760.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1761.  
  1762.   assert (info->type_stack != NULL && info->type_stack->methods != NULL);
  1763.  
  1764.   /* We allocated enough room on info->type_stack->methods to add the
  1765.      trailing semicolon.  */
  1766.   strcat (info->type_stack->methods, ";");
  1767.  
  1768.   return TRUE;
  1769. }
  1770.  
  1771. /* Finish up a class.  */
  1772.  
  1773. static bfd_boolean
  1774. stab_end_class_type (void *p)
  1775. {
  1776.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1777.   size_t len;
  1778.   unsigned int i = 0;
  1779.   char *buf;
  1780.  
  1781.   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
  1782.  
  1783.   /* Work out the size we need to allocate for the class definition.  */
  1784.  
  1785.   len = (strlen (info->type_stack->string)
  1786.          + strlen (info->type_stack->fields)
  1787.          + 10);
  1788.   if (info->type_stack->baseclasses != NULL)
  1789.     {
  1790.       len += 20;
  1791.       for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
  1792.         len += strlen (info->type_stack->baseclasses[i]);
  1793.     }
  1794.   if (info->type_stack->methods != NULL)
  1795.     len += strlen (info->type_stack->methods);
  1796.   if (info->type_stack->vtable != NULL)
  1797.     len += strlen (info->type_stack->vtable);
  1798.  
  1799.   /* Build the class definition.  */
  1800.  
  1801.   buf = (char *) xmalloc (len);
  1802.  
  1803.   strcpy (buf, info->type_stack->string);
  1804.  
  1805.   if (info->type_stack->baseclasses != NULL)
  1806.     {
  1807.       sprintf (buf + strlen (buf), "!%u,", i);
  1808.       for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
  1809.         {
  1810.           strcat (buf, info->type_stack->baseclasses[i]);
  1811.           free (info->type_stack->baseclasses[i]);
  1812.         }
  1813.       free (info->type_stack->baseclasses);
  1814.       info->type_stack->baseclasses = NULL;
  1815.     }
  1816.  
  1817.   strcat (buf, info->type_stack->fields);
  1818.   free (info->type_stack->fields);
  1819.   info->type_stack->fields = NULL;
  1820.  
  1821.   if (info->type_stack->methods != NULL)
  1822.     {
  1823.       strcat (buf, info->type_stack->methods);
  1824.       free (info->type_stack->methods);
  1825.       info->type_stack->methods = NULL;
  1826.     }
  1827.  
  1828.   strcat (buf, ";");
  1829.  
  1830.   if (info->type_stack->vtable != NULL)
  1831.     {
  1832.       strcat (buf, info->type_stack->vtable);
  1833.       free (info->type_stack->vtable);
  1834.       info->type_stack->vtable = NULL;
  1835.     }
  1836.  
  1837.   /* Replace the string on the top of the stack with the complete
  1838.      class definition.  */
  1839.   free (info->type_stack->string);
  1840.   info->type_stack->string = buf;
  1841.  
  1842.   return TRUE;
  1843. }
  1844.  
  1845. /* Push a typedef which was previously defined.  */
  1846.  
  1847. static bfd_boolean
  1848. stab_typedef_type (void *p, const char *name)
  1849. {
  1850.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1851.   struct string_hash_entry *h;
  1852.  
  1853.   h = string_hash_lookup (&info->typedef_hash, name, FALSE, FALSE);
  1854.   assert (h != NULL && h->index > 0);
  1855.  
  1856.   return stab_push_defined_type (info, h->index, h->size);
  1857. }
  1858.  
  1859. /* Push a struct, union or class tag.  */
  1860.  
  1861. static bfd_boolean
  1862. stab_tag_type (void *p, const char *name, unsigned int id,
  1863.                enum debug_type_kind kind)
  1864. {
  1865.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1866.   long tindex;
  1867.   unsigned int size = 0;
  1868.  
  1869.   tindex = stab_get_struct_index (info, name, id, kind, &size);
  1870.   if (tindex < 0)
  1871.     return FALSE;
  1872.  
  1873.   return stab_push_defined_type (info, tindex, size);
  1874. }
  1875.  
  1876. /* Define a typedef.  */
  1877.  
  1878. static bfd_boolean
  1879. stab_typdef (void *p, const char *name)
  1880. {
  1881.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1882.   long tindex;
  1883.   unsigned int size;
  1884.   char *s, *buf;
  1885.   struct string_hash_entry *h;
  1886.  
  1887.   tindex = info->type_stack->index;
  1888.   size = info->type_stack->size;
  1889.   s = stab_pop_type (info);
  1890.  
  1891.   buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
  1892.  
  1893.   if (tindex > 0)
  1894.     sprintf (buf, "%s:t%s", name, s);
  1895.   else
  1896.     {
  1897.       tindex = info->type_index;
  1898.       ++info->type_index;
  1899.       sprintf (buf, "%s:t%ld=%s", name, tindex, s);
  1900.     }
  1901.  
  1902.   free (s);
  1903.  
  1904.   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1905.     return FALSE;
  1906.  
  1907.   free (buf);
  1908.  
  1909.   h = string_hash_lookup (&info->typedef_hash, name, TRUE, FALSE);
  1910.   if (h == NULL)
  1911.     {
  1912.       non_fatal (_("string_hash_lookup failed: %s"),
  1913.                  bfd_errmsg (bfd_get_error ()));
  1914.       return FALSE;
  1915.     }
  1916.  
  1917.   /* I don't think we care about redefinitions.  */
  1918.  
  1919.   h->index = tindex;
  1920.   h->size = size;
  1921.  
  1922.   return TRUE;
  1923. }
  1924.  
  1925. /* Define a tag.  */
  1926.  
  1927. static bfd_boolean
  1928. stab_tag (void *p, const char *tag)
  1929. {
  1930.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1931.   char *s, *buf;
  1932.  
  1933.   s = stab_pop_type (info);
  1934.  
  1935.   buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
  1936.  
  1937.   sprintf (buf, "%s:T%s", tag, s);
  1938.   free (s);
  1939.  
  1940.   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1941.     return FALSE;
  1942.  
  1943.   free (buf);
  1944.  
  1945.   return TRUE;
  1946. }
  1947.  
  1948. /* Define an integer constant.  */
  1949.  
  1950. static bfd_boolean
  1951. stab_int_constant (void *p, const char *name, bfd_vma val)
  1952. {
  1953.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1954.   char *buf;
  1955.  
  1956.   buf = (char *) xmalloc (strlen (name) + 20);
  1957.   sprintf (buf, "%s:c=i%ld", name, (long) val);
  1958.  
  1959.   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1960.     return FALSE;
  1961.  
  1962.   free (buf);
  1963.  
  1964.   return TRUE;
  1965. }
  1966.  
  1967. /* Define a floating point constant.  */
  1968.  
  1969. static bfd_boolean
  1970. stab_float_constant (void *p, const char *name, double val)
  1971. {
  1972.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1973.   char *buf;
  1974.  
  1975.   buf = (char *) xmalloc (strlen (name) + 20);
  1976.   sprintf (buf, "%s:c=f%g", name, val);
  1977.  
  1978.   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  1979.     return FALSE;
  1980.  
  1981.   free (buf);
  1982.  
  1983.   return TRUE;
  1984. }
  1985.  
  1986. /* Define a typed constant.  */
  1987.  
  1988. static bfd_boolean
  1989. stab_typed_constant (void *p, const char *name, bfd_vma val)
  1990. {
  1991.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  1992.   char *s, *buf;
  1993.  
  1994.   s = stab_pop_type (info);
  1995.  
  1996.   buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
  1997.   sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
  1998.   free (s);
  1999.  
  2000.   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
  2001.     return FALSE;
  2002.  
  2003.   free (buf);
  2004.  
  2005.   return TRUE;
  2006. }
  2007.  
  2008. /* Record a variable.  */
  2009.  
  2010. static bfd_boolean
  2011. stab_variable (void *p, const char *name, enum debug_var_kind kind,
  2012.                bfd_vma val)
  2013. {
  2014.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  2015.   char *s, *buf;
  2016.   int stab_type;
  2017.   const char *kindstr;
  2018.  
  2019.   s = stab_pop_type (info);
  2020.  
  2021.   switch (kind)
  2022.     {
  2023.     default:
  2024.       abort ();
  2025.  
  2026.     case DEBUG_GLOBAL:
  2027.       stab_type = N_GSYM;
  2028.       kindstr = "G";
  2029.       break;
  2030.  
  2031.     case DEBUG_STATIC:
  2032.       stab_type = N_STSYM;
  2033.       kindstr = "S";
  2034.       break;
  2035.  
  2036.     case DEBUG_LOCAL_STATIC:
  2037.       stab_type = N_STSYM;
  2038.       kindstr = "V";
  2039.       break;
  2040.  
  2041.     case DEBUG_LOCAL:
  2042.       stab_type = N_LSYM;
  2043.       kindstr = "";
  2044.  
  2045.       /* Make sure that this is a type reference or definition.  */
  2046.       if (! ISDIGIT (*s))
  2047.         {
  2048.           char *n;
  2049.           long tindex;
  2050.  
  2051.           tindex = info->type_index;
  2052.           ++info->type_index;
  2053.           n = (char *) xmalloc (strlen (s) + 20);
  2054.           sprintf (n, "%ld=%s", tindex, s);
  2055.           free (s);
  2056.           s = n;
  2057.         }
  2058.       break;
  2059.  
  2060.     case DEBUG_REGISTER:
  2061.       stab_type = N_RSYM;
  2062.       kindstr = "r";
  2063.       break;
  2064.     }
  2065.  
  2066.   buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
  2067.   sprintf (buf, "%s:%s%s", name, kindstr, s);
  2068.   free (s);
  2069.  
  2070.   if (! stab_write_symbol (info, stab_type, 0, val, buf))
  2071.     return FALSE;
  2072.  
  2073.   free (buf);
  2074.  
  2075.   return TRUE;
  2076. }
  2077.  
  2078. /* Start outputting a function.  */
  2079.  
  2080. static bfd_boolean
  2081. stab_start_function (void *p, const char *name, bfd_boolean globalp)
  2082. {
  2083.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  2084.   char *rettype, *buf;
  2085.  
  2086.   assert (info->nesting == 0 && info->fun_offset == -1);
  2087.  
  2088.   rettype = stab_pop_type (info);
  2089.  
  2090.   buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
  2091.   sprintf (buf, "%s:%c%s", name,
  2092.            globalp ? 'F' : 'f',
  2093.            rettype);
  2094.  
  2095.   /* We don't know the value now, so we set it in start_block.  */
  2096.   info->fun_offset = info->symbols_size;
  2097.  
  2098.   if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
  2099.     return FALSE;
  2100.  
  2101.   free (buf);
  2102.  
  2103.   return TRUE;
  2104. }
  2105.  
  2106. /* Output a function parameter.  */
  2107.  
  2108. static bfd_boolean
  2109. stab_function_parameter (void *p, const char *name, enum debug_parm_kind kind, bfd_vma val)
  2110. {
  2111.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  2112.   char *s, *buf;
  2113.   int stab_type;
  2114.   char kindc;
  2115.  
  2116.   s = stab_pop_type (info);
  2117.  
  2118.   switch (kind)
  2119.     {
  2120.     default:
  2121.       abort ();
  2122.  
  2123.     case DEBUG_PARM_STACK:
  2124.       stab_type = N_PSYM;
  2125.       kindc = 'p';
  2126.       break;
  2127.  
  2128.     case DEBUG_PARM_REG:
  2129.       stab_type = N_RSYM;
  2130.       kindc = 'P';
  2131.       break;
  2132.  
  2133.     case DEBUG_PARM_REFERENCE:
  2134.       stab_type = N_PSYM;
  2135.       kindc = 'v';
  2136.       break;
  2137.  
  2138.     case DEBUG_PARM_REF_REG:
  2139.       stab_type = N_RSYM;
  2140.       kindc = 'a';
  2141.       break;
  2142.     }
  2143.  
  2144.   buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
  2145.   sprintf (buf, "%s:%c%s", name, kindc, s);
  2146.   free (s);
  2147.  
  2148.   if (! stab_write_symbol (info, stab_type, 0, val, buf))
  2149.     return FALSE;
  2150.  
  2151.   free (buf);
  2152.  
  2153.   return TRUE;
  2154. }
  2155.  
  2156. /* Start a block.  */
  2157.  
  2158. static bfd_boolean
  2159. stab_start_block (void *p, bfd_vma addr)
  2160. {
  2161.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  2162.  
  2163.   /* Fill in any slots which have been waiting for the first known
  2164.      text address.  */
  2165.  
  2166.   if (info->so_offset != -1)
  2167.     {
  2168.       bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
  2169.       info->so_offset = -1;
  2170.     }
  2171.  
  2172.   if (info->fun_offset != -1)
  2173.     {
  2174.       bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
  2175.       info->fun_offset = -1;
  2176.     }
  2177.  
  2178.   ++info->nesting;
  2179.  
  2180.   /* We will be called with a top level block surrounding the
  2181.      function, but stabs information does not output that block, so we
  2182.      ignore it.  */
  2183.  
  2184.   if (info->nesting == 1)
  2185.     {
  2186.       info->fnaddr = addr;
  2187.       return TRUE;
  2188.     }
  2189.  
  2190.   /* We have to output the LBRAC symbol after any variables which are
  2191.      declared inside the block.  We postpone the LBRAC until the next
  2192.      start_block or end_block.  */
  2193.  
  2194.   /* If we have postponed an LBRAC, output it now.  */
  2195.   if (info->pending_lbrac != (bfd_vma) -1)
  2196.     {
  2197.       if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
  2198.                                (const char *) NULL))
  2199.         return FALSE;
  2200.     }
  2201.  
  2202.   /* Remember the address and output it later.  */
  2203.  
  2204.   info->pending_lbrac = addr - info->fnaddr;
  2205.  
  2206.   return TRUE;
  2207. }
  2208.  
  2209. /* End a block.  */
  2210.  
  2211. static bfd_boolean
  2212. stab_end_block (void *p, bfd_vma addr)
  2213. {
  2214.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  2215.  
  2216.   if (addr > info->last_text_address)
  2217.     info->last_text_address = addr;
  2218.  
  2219.   /* If we have postponed an LBRAC, output it now.  */
  2220.   if (info->pending_lbrac != (bfd_vma) -1)
  2221.     {
  2222.       if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
  2223.                                (const char *) NULL))
  2224.         return FALSE;
  2225.       info->pending_lbrac = (bfd_vma) -1;
  2226.     }
  2227.  
  2228.   assert (info->nesting > 0);
  2229.  
  2230.   --info->nesting;
  2231.  
  2232.   /* We ignore the outermost block.  */
  2233.   if (info->nesting == 0)
  2234.     return TRUE;
  2235.  
  2236.   return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
  2237.                             (const char *) NULL);
  2238. }
  2239.  
  2240. /* End a function.  */
  2241.  
  2242. static bfd_boolean
  2243. stab_end_function (void *p ATTRIBUTE_UNUSED)
  2244. {
  2245.   return TRUE;
  2246. }
  2247.  
  2248. /* Output a line number.  */
  2249.  
  2250. static bfd_boolean
  2251. stab_lineno (void *p, const char *file, unsigned long lineno, bfd_vma addr)
  2252. {
  2253.   struct stab_write_handle *info = (struct stab_write_handle *) p;
  2254.  
  2255.   assert (info->lineno_filename != NULL);
  2256.  
  2257.   if (addr > info->last_text_address)
  2258.     info->last_text_address = addr;
  2259.  
  2260.   if (filename_cmp (file, info->lineno_filename) != 0)
  2261.     {
  2262.       if (! stab_write_symbol (info, N_SOL, 0, addr, file))
  2263.         return FALSE;
  2264.       info->lineno_filename = file;
  2265.     }
  2266.  
  2267.   return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
  2268.                             (const char *) NULL);
  2269. }
  2270.