(linenum→info "unix/slp.c:2238")

binutils/2.18/binutils/stabs.c

    1: /* stabs.c -- Parse stabs debugging information
    2:    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
    3:    2006, 2007 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 contains code which parses stabs debugging information.
   24:    The organization of this code is based on the gdb stabs reading
   25:    code.  The job it does is somewhat different, because it is not
   26:    trying to identify the correct address for anything.  */
   27: 
   28: #include "sysdep.h"
   29: #include "bfd.h"
   30: #include "libiberty.h"
   31: #include "safe-ctype.h"
   32: #include "demangle.h"
   33: #include "debug.h"
   34: #include "budbg.h"
   35: #include "filenames.h"
   36: #include "aout/aout64.h"
   37: #include "aout/stab_gnu.h"
   38: 
   39: /* The number of predefined XCOFF types.  */
   40: 
   41: #define XCOFF_TYPE_COUNT 34
   42: 
   43: /* This structure is used as a handle so that the stab parsing doesn't
   44:    need to use any static variables.  */
   45: 
   46: struct stab_handle
   47: {
   48:   /* The BFD.  */
   49:   bfd *abfd;
   50:   /* TRUE if this is stabs in sections.  */
   51:   bfd_boolean sections;
   52:   /* The symbol table.  */
   53:   asymbol **syms;
   54:   /* The number of symbols.  */
   55:   long symcount;
   56:   /* The accumulated file name string.  */
   57:   char *so_string;
   58:   /* The value of the last N_SO symbol.  */
   59:   bfd_vma so_value;
   60:   /* The value of the start of the file, so that we can handle file
   61:      relative N_LBRAC and N_RBRAC symbols.  */
   62:   bfd_vma file_start_offset;
   63:   /* The offset of the start of the function, so that we can handle
   64:      function relative N_LBRAC and N_RBRAC symbols.  */
   65:   bfd_vma function_start_offset;
   66:   /* The version number of gcc which compiled the current compilation
   67:      unit, 0 if not compiled by gcc.  */
   68:   int gcc_compiled;
   69:   /* Whether an N_OPT symbol was seen that was not generated by gcc,
   70:      so that we can detect the SunPRO compiler.  */
   71:   bfd_boolean n_opt_found;
   72:   /* The main file name.  */
   73:   char *main_filename;
   74:   /* A stack of unfinished N_BINCL files.  */
   75:   struct bincl_file *bincl_stack;
   76:   /* A list of finished N_BINCL files.  */
   77:   struct bincl_file *bincl_list;
   78:   /* Whether we are inside a function or not.  */
   79:   bfd_boolean within_function;
   80:   /* The address of the end of the function, used if we have seen an
   81:      N_FUN symbol while in a function.  This is -1 if we have not seen
   82:      an N_FUN (the normal case).  */
   83:   bfd_vma function_end;
   84:   /* The depth of block nesting.  */
   85:   int block_depth;
   86:   /* List of pending variable definitions.  */
   87:   struct stab_pending_var *pending;
   88:   /* Number of files for which we have types.  */
   89:   unsigned int files;
   90:   /* Lists of types per file.  */
   91:   struct stab_types **file_types;
   92:   /* Predefined XCOFF types.  */
   93:   debug_type xcoff_types[XCOFF_TYPE_COUNT];
   94:   /* Undefined tags.  */
   95:   struct stab_tag *tags;
   96:   /* Set by parse_stab_type if it sees a structure defined as a cross
   97:      reference to itself.  Reset by parse_stab_type otherwise.  */
   98:   bfd_boolean self_crossref;
   99: };
  100: 
  101: /* A list of these structures is used to hold pending variable
  102:    definitions seen before the N_LBRAC of a block.  */
  103: 
  104: struct stab_pending_var
  105: {
  106:   /* Next pending variable definition.  */
  107:   struct stab_pending_var *next;
  108:   /* Name.  */
  109:   const char *name;
  110:   /* Type.  */
  111:   debug_type type;
  112:   /* Kind.  */
  113:   enum debug_var_kind kind;
  114:   /* Value.  */
  115:   bfd_vma val;
  116: };
  117: 
  118: /* A list of these structures is used to hold the types for a single
  119:    file.  */
  120: 
  121: struct stab_types
  122: {
  123:   /* Next set of slots for this file.  */
  124:   struct stab_types *next;
  125:   /* Types indexed by type number.  */
  126: #define STAB_TYPES_SLOTS (16)
  127:   debug_type types[STAB_TYPES_SLOTS];
  128: };
  129: 
  130: /* We keep a list of undefined tags that we encounter, so that we can
  131:    fill them in if the tag is later defined.  */
  132: 
  133: struct stab_tag
  134: {
  135:   /* Next undefined tag.  */
  136:   struct stab_tag *next;
  137:   /* Tag name.  */
  138:   const char *name;
  139:   /* Type kind.  */
  140:   enum debug_type_kind kind;
  141:   /* Slot to hold real type when we discover it.  If we don't, we fill
  142:      in an undefined tag type.  */
  143:   debug_type slot;
  144:   /* Indirect type we have created to point at slot.  */
  145:   debug_type type;
  146: };
  147: 
  148: static char *savestring (const char *, int);
  149: static bfd_vma parse_number (const char **, bfd_boolean *);
  150: static void bad_stab (const char *);
  151: static void warn_stab (const char *, const char *);
  152: static bfd_boolean parse_stab_string
  153:   (void *, struct stab_handle *, int, int, bfd_vma, const char *);
  154: static debug_type parse_stab_type
  155:   (void *, struct stab_handle *, const char *, const char **, debug_type **);
  156: static bfd_boolean parse_stab_type_number (const char **, int *);
  157: static debug_type parse_stab_range_type
  158:   (void *, struct stab_handle *, const char *, const char **, const int *);
  159: static debug_type parse_stab_sun_builtin_type (void *, const char **);
  160: static debug_type parse_stab_sun_floating_type (void *, const char **);
  161: static debug_type parse_stab_enum_type (void *, const char **);
  162: static debug_type parse_stab_struct_type
  163:   (void *, struct stab_handle *, const char *, const char **,
  164:    bfd_boolean, const int *);
  165: static bfd_boolean parse_stab_baseclasses
  166:   (void *, struct stab_handle *, const char **, debug_baseclass **);
  167: static bfd_boolean parse_stab_struct_fields
  168:   (void *, struct stab_handle *, const char **, debug_field **, bfd_boolean *);
  169: static bfd_boolean parse_stab_cpp_abbrev
  170:   (void *, struct stab_handle *, const char **, debug_field *);
  171: static bfd_boolean parse_stab_one_struct_field
  172:   (void *, struct stab_handle *, const char **, const char *,
  173:    debug_field *, bfd_boolean *);
  174: static bfd_boolean parse_stab_members
  175:   (void *, struct stab_handle *, const char *, const char **, const int *,
  176:    debug_method **);
  177: static debug_type parse_stab_argtypes
  178:   (void *, struct stab_handle *, debug_type, const char *, const char *,
  179:    debug_type, const char *, bfd_boolean, bfd_boolean, const char **);
  180: static bfd_boolean parse_stab_tilde_field
  181:   (void *, struct stab_handle *, const char **, const int *, debug_type *,
  182:    bfd_boolean *);
  183: static debug_type parse_stab_array_type
  184:   (void *, struct stab_handle *, const char **, bfd_boolean);
  185: static void push_bincl (struct stab_handle *, const char *, bfd_vma);
  186: static const char *pop_bincl (struct stab_handle *);
  187: static bfd_boolean find_excl (struct stab_handle *, const char *, bfd_vma);
  188: static bfd_boolean stab_record_variable
  189:   (void *, struct stab_handle *, const char *, debug_type,
  190:    enum debug_var_kind, bfd_vma);
  191: static bfd_boolean stab_emit_pending_vars (void *, struct stab_handle *);
  192: static debug_type *stab_find_slot (struct stab_handle *, const int *);
  193: static debug_type stab_find_type (void *, struct stab_handle *, const int *);
  194: static bfd_boolean stab_record_type
  195:   (void *, struct stab_handle *, const int *, debug_type);
  196: static debug_type stab_xcoff_builtin_type
  197:   (void *, struct stab_handle *, int);
  198: static debug_type stab_find_tagged_type
  199:   (void *, struct stab_handle *, const char *, int, enum debug_type_kind);
  200: static debug_type *stab_demangle_argtypes
  201:   (void *, struct stab_handle *, const char *, bfd_boolean *, unsigned int);
  202: static debug_type *stab_demangle_v3_argtypes
  203:   (void *, struct stab_handle *, const char *, bfd_boolean *);
  204: static debug_type *stab_demangle_v3_arglist
  205:   (void *, struct stab_handle *, struct demangle_component *, bfd_boolean *);
  206: static debug_type stab_demangle_v3_arg
  207:   (void *, struct stab_handle *, struct demangle_component *, debug_type,
  208:    bfd_boolean *);
  209: 
  210: /* Save a string in memory.  */
  211: 
  212: static char *
  213: savestring (const char *start, int len)
  214: {
  215:   char *ret;
  216: 
  217:   ret = (char *) xmalloc (len + 1);
  218:   memcpy (ret, start, len);
  219:   ret[len] = '\0';
  220:   return ret;
  221: }
  222: 
  223: /* Read a number from a string.  */
  224: 
  225: static bfd_vma
  226: parse_number (const char **pp, bfd_boolean *poverflow)
  227: {
  228:   unsigned long ul;
  229:   const char *orig;
  230: 
  231:   if (poverflow != NULL)
  232:     *poverflow = FALSE;
  233: 
  234:   orig = *pp;
  235: 
  236:   errno = 0;
  237:   ul = strtoul (*pp, (char **) pp, 0);
  238:   if (ul + 1 != 0 || errno == 0)
  239:     {
  240:       /* If bfd_vma is larger than unsigned long, and the number is
  241:          meant to be negative, we have to make sure that we sign
  242:          extend properly.  */
  243:       if (*orig == '-')
  244:         return (bfd_vma) (bfd_signed_vma) (long) ul;
  245:       return (bfd_vma) ul;
  246:     }
  247: 
  248:   /* Note that even though strtoul overflowed, it should have set *pp
  249:      to the end of the number, which is where we want it.  */
  250:   if (sizeof (bfd_vma) > sizeof (unsigned long))
  251:     {
  252:       const char *p;
  253:       bfd_boolean neg;
  254:       int base;
  255:       bfd_vma over, lastdig;
  256:       bfd_boolean overflow;
  257:       bfd_vma v;
  258: 
  259:       /* Our own version of strtoul, for a bfd_vma.  */
  260:       p = orig;
  261: 
  262:       neg = FALSE;
  263:       if (*p == '+')
  264:         ++p;
  265:       else if (*p == '-')
  266:         {
  267:           neg = TRUE;
  268:           ++p;
  269:         }
  270: 
  271:       base = 10;
  272:       if (*p == '0')
  273:         {
  274:           if (p[1] == 'x' || p[1] == 'X')
  275:             {
  276:               base = 16;
  277:               p += 2;
  278:             }
  279:           else
  280:             {
  281:               base = 8;
  282:               ++p;
  283:             }
  284:         }
  285: 
  286:       over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
  287:       lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
  288: 
  289:       overflow = FALSE;
  290:       v = 0;
  291:       while (1)
  292:         {
  293:           int d;
  294: 
  295:           d = *p++;
  296:           if (ISDIGIT (d))
  297:             d -= '0';
  298:           else if (ISUPPER (d))
  299:             d -= 'A';
  300:           else if (ISLOWER (d))
  301:             d -= 'a';
  302:           else
  303:             break;
  304: 
  305:           if (d >= base)
  306:             break;
  307: 
  308:           if (v > over || (v == over && (bfd_vma) d > lastdig))
  309:             {
  310:               overflow = TRUE;
  311:               break;
  312:             }
  313:         }
  314: 
  315:       if (! overflow)
  316:         {
  317:           if (neg)
  318:             v = - v;
  319:           return v;
  320:         }
  321:     }
  322: 
  323:   /* If we get here, the number is too large to represent in a
  324:      bfd_vma.  */
  325:   if (poverflow != NULL)
  326:     *poverflow = TRUE;
  327:   else
  328:     warn_stab (orig, _("numeric overflow"));
  329: 
  330:   return 0;
  331: }
  332: 
  333: /* Give an error for a bad stab string.  */
  334: 
  335: static void
  336: bad_stab (const char *p)
  337: {
  338:   fprintf (stderr, _("Bad stab: %s\n"), p);
  339: }
  340: 
  341: /* Warn about something in a stab string.  */
  342: 
  343: static void
  344: warn_stab (const char *p, const char *err)
  345: {
  346:   fprintf (stderr, _("Warning: %s: %s\n"), err, p);
  347: }
  348: 
  349: /* Create a handle to parse stabs symbols with.  */
  350: 
  351: void *
  352: start_stab (void *dhandle ATTRIBUTE_UNUSED, bfd *abfd, bfd_boolean sections,
  353:             asymbol **syms, long symcount)
  354: {
  355:   struct stab_handle *ret;
  356: 
  357:   ret = (struct stab_handle *) xmalloc (sizeof *ret);
  358:   memset (ret, 0, sizeof *ret);
  359:   ret->abfd = abfd;
  360:   ret->sections = sections;
  361:   ret->syms = syms;
  362:   ret->symcount = symcount;
  363:   ret->files = 1;
  364:   ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
  365:   ret->file_types[0] = NULL;
  366:   ret->function_end = (bfd_vma) -1;
  367:   return (void *) ret;
  368: }
  369: 
  370: /* When we have processed all the stabs information, we need to go
  371:    through and fill in all the undefined tags.  */
  372: 
  373: bfd_boolean
  374: finish_stab (void *dhandle, void *handle)
  375: {
  376:   struct stab_handle *info = (struct stab_handle *) handle;
  377:   struct stab_tag *st;
  378: 
  379:   if (info->within_function)
  380:     {
  381:       if (! stab_emit_pending_vars (dhandle, info)
  382:           || ! debug_end_function (dhandle, info->function_end))
  383:         return FALSE;
  384:       info->within_function = FALSE;
  385:       info->function_end = (bfd_vma) -1;
  386:     }
  387: 
  388:   for (st = info->tags; st != NULL; st = st->next)
  389:     {
  390:       enum debug_type_kind kind;
  391: 
  392:       kind = st->kind;
  393:       if (kind == DEBUG_KIND_ILLEGAL)
  394:         kind = DEBUG_KIND_STRUCT;
  395:       st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
  396:       if (st->slot == DEBUG_TYPE_NULL)
  397:         return FALSE;
  398:     }
  399: 
  400:   return TRUE;
  401: }
  402: 
  403: /* Handle a single stabs symbol.  */
  404: 
  405: bfd_boolean
  406: parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value,
  407:             const char *string)
  408: {
  409:   struct stab_handle *info = (struct stab_handle *) handle;
  410: 
  411:   /* gcc will emit two N_SO strings per compilation unit, one for the
  412:      directory name and one for the file name.  We just collect N_SO
  413:      strings as we see them, and start the new compilation unit when
  414:      we see a non N_SO symbol.  */
  415:   if (info->so_string != NULL
  416:       && (type != N_SO || *string == '\0' || value != info->so_value))
  417:     {
  418:       if (! debug_set_filename (dhandle, info->so_string))
  419:         return FALSE;
  420:       info->main_filename = info->so_string;
  421: 
  422:       info->gcc_compiled = 0;
  423:       info->n_opt_found = FALSE;
  424: 
  425:       /* Generally, for stabs in the symbol table, the N_LBRAC and
  426:          N_RBRAC symbols are relative to the N_SO symbol value.  */
  427:       if (! info->sections)
  428:         info->file_start_offset = info->so_value;
  429: 
  430:       /* We need to reset the mapping from type numbers to types.  We
  431:          can't free the old mapping, because of the use of
  432:          debug_make_indirect_type.  */
  433:       info->files = 1;
  434:       info->file_types = ((struct stab_types **)
  435:                           xmalloc (sizeof *info->file_types));
  436:       info->file_types[0] = NULL;
  437: 
  438:       info->so_string = NULL;
  439: 
  440:       /* Now process whatever type we just got.  */
  441:     }
  442: 
  443:   switch (type)
  444:     {
  445:     case N_FN:
  446:     case N_FN_SEQ:
  447:       break;
  448: 
  449:     case N_LBRAC:
  450:       /* Ignore extra outermost context from SunPRO cc and acc.  */
  451:       if (info->n_opt_found && desc == 1)
  452:         break;
  453: 
  454:       if (! info->within_function)
  455:         {
  456:           fprintf (stderr, _("N_LBRAC not within function\n"));
  457:           return FALSE;
  458:         }
  459: 
  460:       /* Start an inner lexical block.  */
  461:       if (! debug_start_block (dhandle,
  462:                                (value
  463:                                 + info->file_start_offset
  464:                                 + info->function_start_offset)))
  465:         return FALSE;
  466: 
  467:       /* Emit any pending variable definitions.  */
  468:       if (! stab_emit_pending_vars (dhandle, info))
  469:         return FALSE;
  470: 
  471:       ++info->block_depth;
  472:       break;
  473: 
  474:     case N_RBRAC:
  475:       /* Ignore extra outermost context from SunPRO cc and acc.  */
  476:       if (info->n_opt_found && desc == 1)
  477:         break;
  478: 
  479:       /* We shouldn't have any pending variable definitions here, but,
  480:          if we do, we probably need to emit them before closing the
  481:          block.  */
  482:       if (! stab_emit_pending_vars (dhandle, info))
  483:         return FALSE;
  484: 
  485:       /* End an inner lexical block.  */
  486:       if (! debug_end_block (dhandle,
  487:                              (value
  488:                               + info->file_start_offset
  489:                               + info->function_start_offset)))
  490:         return FALSE;
  491: 
  492:       --info->block_depth;
  493:       if (info->block_depth < 0)
  494:         {
  495:           fprintf (stderr, _("Too many N_RBRACs\n"));
  496:           return FALSE;
  497:         }
  498:       break;
  499: 
  500:     case N_SO:
  501:       /* This always ends a function.  */
  502:       if (info->within_function)
  503:         {
  504:           bfd_vma endval;
  505: 
  506:           endval = value;
  507:           if (*string != '\0'
  508:               && info->function_end != (bfd_vma) -1
  509:               && info->function_end < endval)
  510:             endval = info->function_end;
  511:           if (! stab_emit_pending_vars (dhandle, info)
  512:               || ! debug_end_function (dhandle, endval))
  513:             return FALSE;
  514:           info->within_function = FALSE;
  515:           info->function_end = (bfd_vma) -1;
  516:         }
  517: 
  518:       /* An empty string is emitted by gcc at the end of a compilation
  519:          unit.  */
  520:       if (*string == '\0')
  521:         return TRUE;
  522: 
  523:       /* Just accumulate strings until we see a non N_SO symbol.  If
  524:          the string starts with a directory separator or some other
  525:          form of absolute path specification, we discard the previously
  526:          accumulated strings.  */
  527:       if (info->so_string == NULL)
  528:         info->so_string = xstrdup (string);
  529:       else
  530:         {
  531:           char *f;
  532: 
  533:           f = info->so_string;
  534: 
  535:           if (IS_ABSOLUTE_PATH (string))
  536:             info->so_string = xstrdup (string);
  537:           else
  538:             info->so_string = concat (info->so_string, string,
  539:                                       (const char *) NULL);
  540:           free (f);
  541:         }
  542: 
  543:       info->so_value = value;
  544: 
  545:       break;
  546: 
  547:     case N_SOL:
  548:       /* Start an include file.  */
  549:       if (! debug_start_source (dhandle, string))
  550:         return FALSE;
  551:       break;
  552: 
  553:     case N_BINCL:
  554:       /* Start an include file which may be replaced.  */
  555:       push_bincl (info, string, value);
  556:       if (! debug_start_source (dhandle, string))
  557:         return FALSE;
  558:       break;
  559: 
  560:     case N_EINCL:
  561:       /* End an N_BINCL include.  */
  562:       if (! debug_start_source (dhandle, pop_bincl (info)))
  563:         return FALSE;
  564:       break;
  565: 
  566:     case N_EXCL:
  567:       /* This is a duplicate of a header file named by N_BINCL which
  568:          was eliminated by the linker.  */
  569:       if (! find_excl (info, string, value))
  570:         return FALSE;
  571:       break;
  572: 
  573:     case N_SLINE:
  574: