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

binutils/2.18/gas/symbols.c

    1: /* symbols.c -symbol table-
    2:    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
    3:    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
    4:    Free Software Foundation, Inc.
    5: 
    6:    This file is part of GAS, the GNU Assembler.
    7: 
    8:    GAS 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, or (at your option)
   11:    any later version.
   12: 
   13:    GAS 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 GAS; see the file COPYING.  If not, write to the Free
   20:    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   21:    02110-1301, USA.  */
   22: 
   23: /* #define DEBUG_SYMS / * to debug symbol list maintenance.  */
   24: 
   25: #include "as.h"
   26: 
   27: #include "safe-ctype.h"
   28: #include "obstack.h"            /* For "symbols.h" */
   29: #include "subsegs.h"
   30: 
   31: #include "struc-symbol.h"
   32: 
   33: /* This is non-zero if symbols are case sensitive, which is the
   34:    default.  */
   35: int symbols_case_sensitive = 1;
   36: 
   37: #ifndef WORKING_DOT_WORD
   38: extern int new_broken_words;
   39: #endif
   40: 
   41: /* symbol-name => struct symbol pointer */
   42: static struct hash_control *sy_hash;
   43: 
   44: /* Table of local symbols.  */
   45: static struct hash_control *local_hash;
   46: 
   47: /* Below are commented in "symbols.h".  */
   48: symbolS *symbol_rootP;
   49: symbolS *symbol_lastP;
   50: symbolS abs_symbol;
   51: 
   52: #ifdef DEBUG_SYMS
   53: #define debug_verify_symchain verify_symbol_chain
   54: #else
   55: #define debug_verify_symchain(root, last) ((void) 0)
   56: #endif
   57: 
   58: #define DOLLAR_LABEL_CHAR       '\001'
   59: #define LOCAL_LABEL_CHAR        '\002'
   60: 
   61: struct obstack notes;
   62: #ifdef USE_UNIQUE
   63: /* The name of an external symbol which is
   64:    used to make weak PE symbol names unique.  */
   65: const char * an_external_name;
   66: #endif
   67: 
   68: static char *save_symbol_name (const char *);
   69: static void fb_label_init (void);
   70: static long dollar_label_instance (long);
   71: static long fb_label_instance (long);
   72: 
   73: static void print_binary (FILE *, const char *, expressionS *);
   74: static void report_op_error (symbolS *, symbolS *, symbolS *);
   75: 
   76: /* Return a pointer to a new symbol.  Die if we can't make a new
   77:    symbol.  Fill in the symbol's values.  Add symbol to end of symbol
   78:    chain.
   79: 
   80:    This function should be called in the general case of creating a
   81:    symbol.  However, if the output file symbol table has already been
   82:    set, and you are certain that this symbol won't be wanted in the
   83:    output file, you can call symbol_create.  */
   84: 
   85: symbolS *
   86: symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
   87: {
   88:   symbolS *symbolP = symbol_create (name, segment, valu, frag);
   89: 
   90:   /* Link to end of symbol chain.  */
   91:   {
   92:     extern int symbol_table_frozen;
   93:     if (symbol_table_frozen)
   94:       abort ();
   95:   }
   96:   symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
   97: 
   98:   return symbolP;
   99: }
  100: 
  101: /* Save a symbol name on a permanent obstack, and convert it according
  102:    to the object file format.  */
  103: 
  104: static char *
  105: save_symbol_name (const char *name)
  106: {
  107:   unsigned int name_length;
  108:   char *ret;
  109: 
  110:   name_length = strlen (name) + 1;      /* +1 for \0.  */
  111:   obstack_grow (&notes, name, name_length);
  112:   ret = obstack_finish (&notes);
  113: 
  114: #ifdef tc_canonicalize_symbol_name
  115:   ret = tc_canonicalize_symbol_name (ret);
  116: #endif
  117: 
  118:   if (! symbols_case_sensitive)
  119:     {
  120:       char *s;
  121: 
  122:       for (s = ret; *s != '\0'; s++)
  123:         *s = TOUPPER (*s);
  124:     }
  125: 
  126:   return ret;
  127: }
  128: 
  129: symbolS *
  130: symbol_create (const char *name, /* It is copied, the caller can destroy/modify.  */
  131:                segT segment,   /* Segment identifier (SEG_<something>).  */
  132:                valueT valu,    /* Symbol value.  */
  133:                fragS *frag     /* Associated fragment.  */)
  134: {
  135:   char *preserved_copy_of_name;
  136:   symbolS *symbolP;
  137: 
  138:   preserved_copy_of_name = save_symbol_name (name);
  139: 
  140:   symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
  141: 
  142:   /* symbol must be born in some fixed state.  This seems as good as any.  */
  143:   memset (symbolP, 0, sizeof (symbolS));
  144: 
  145:   symbolP->bsym = bfd_make_empty_symbol (stdoutput);
  146:   if (symbolP->bsym == NULL)
  147:     as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
  148:   S_SET_NAME (symbolP, preserved_copy_of_name);
  149: 
  150:   S_SET_SEGMENT (symbolP, segment);
  151:   S_SET_VALUE (symbolP, valu);
  152:   symbol_clear_list_pointers (symbolP);
  153: 
  154:   symbolP->sy_frag = frag;
  155: 
  156:   obj_symbol_new_hook (symbolP);
  157: 
  158: #ifdef tc_symbol_new_hook
  159:   tc_symbol_new_hook (symbolP);
  160: #endif
  161: 
  162:   return symbolP;
  163: }
  164: ^L
  165: 
  166: /* Local symbol support.  If we can get away with it, we keep only a
  167:    small amount of information for local symbols.  */
  168: 
  169: static symbolS *local_symbol_convert (struct local_symbol *);
  170: 
  171: /* Used for statistics.  */
  172: 
  173: static unsigned long local_symbol_count;
  174: static unsigned long local_symbol_conversion_count;
  175: 
  176: /* This macro is called with a symbol argument passed by reference.
  177:    It returns whether this is a local symbol.  If necessary, it
  178:    changes its argument to the real symbol.  */
  179: 
  180: #define LOCAL_SYMBOL_CHECK(s)                                           \
  181:   (s->bsym == NULL                                                      \
  182:    ? (local_symbol_converted_p ((struct local_symbol *) s)              \
  183:       ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s),  \
  184:          0)                                                            \
  185:       : 1)                                                              \
  186:    : 0)
  187: 
  188: /* Create a local symbol and insert it into the local hash table.  */
  189: 
  190: static struct local_symbol *
  191: local_symbol_make (const char *name, segT section, valueT value, fragS *frag)
  192: {
  193:   char *name_copy;
  194:   struct local_symbol *ret;
  195: 
  196:   ++local_symbol_count;
  197: 
  198:   name_copy = save_symbol_name (name);
  199: 
  200:   ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
  201:   ret->lsy_marker = NULL;
  202:   ret->lsy_name = name_copy;
  203:   ret->lsy_section = section;
  204:   local_symbol_set_frag (ret, frag);
  205:   ret->lsy_value = value;
  206: 
  207:   hash_jam (local_hash, name_copy, (PTR) ret);
  208: 
  209:   return ret;
  210: }
  211: 
  212: /* Convert a local symbol into a real symbol.  Note that we do not
  213:    reclaim the space used by the local symbol.  */
  214: 
  215: static symbolS *
  216: local_symbol_convert (struct local_symbol *locsym)
  217: {
  218:   symbolS *ret;
  219: 
  220:   assert (locsym->lsy_marker == NULL);
  221:   if (local_symbol_converted_p (locsym))
  222:     return local_symbol_get_real_symbol (locsym);
  223: 
  224:   ++local_symbol_conversion_count;
  225: 
  226:   ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
  227:                     local_symbol_get_frag (locsym));
  228: 
  229:   if (local_symbol_resolved_p (locsym))
  230:     ret->sy_resolved = 1;
  231: 
  232:   /* Local symbols are always either defined or used.  */
  233:   ret->sy_used = 1;
  234: 
  235: #ifdef TC_LOCAL_SYMFIELD_CONVERT
  236:   TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
  237: #endif
  238: 
  239:   symbol_table_insert (ret);
  240: 
  241:   local_symbol_mark_converted (locsym);
  242:   local_symbol_set_real_symbol (locsym, ret);
  243: 
  244:   hash_jam (local_hash, locsym->lsy_name, NULL);
  245: 
  246:   return ret;
  247: }
  248: ^L
  249: /* We have just seen "<name>:".
  250:    Creates a struct symbol unless it already exists.
  251: 
  252:    Gripes if we are redefining a symbol incompatibly (and ignores it).  */
  253: 
  254: symbolS *
  255: colon (/* Just seen "x:" - rattle symbols & frags.  */
  256:        const char *sym_name     /* Symbol name, as a cannonical string.  */
  257:        /* We copy this string: OK to alter later.  */)
  258: {
  259:   register symbolS *symbolP;    /* Symbol we are working with.  */
  260: 
  261:   /* Sun local labels go out of scope whenever a non-local symbol is
  262:      defined.  */
  263:   if (LOCAL_LABELS_DOLLAR
  264:       && !bfd_is_local_label_name (stdoutput, sym_name))
  265:     dollar_label_clear ();
  266: 
  267: #ifndef WORKING_DOT_WORD
  268:   if (new_broken_words)
  269:     {
  270:       struct broken_word *a;
  271:       int possible_bytes;
  272:       fragS *frag_tmp;
  273:       char *frag_opcode;
  274: 
  275:       if (now_seg == absolute_section)
  276:         {
  277:           as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
  278:           return NULL;
  279:         }
  280: 
  281:       possible_bytes = (md_short_jump_size
  282:                         + new_broken_words * md_long_jump_size);
  283: 
  284:       frag_tmp = frag_now;
  285:       frag_opcode = frag_var (rs_broken_word,
  286:                               possible_bytes,
  287:                               possible_bytes,
  288:                               (relax_substateT) 0,
  289:                               (symbolS *) broken_words,
  290:                               (offsetT) 0,
  291:                               NULL);
  292: 
  293:       /* We want to store the pointer to where to insert the jump
  294:          table in the fr_opcode of the rs_broken_word frag.  This
  295:          requires a little hackery.  */
  296:       while (frag_tmp
  297:              && (frag_tmp->fr_type != rs_broken_word
  298:                  || frag_tmp->fr_opcode))
  299:         frag_tmp = frag_tmp->fr_next;
  300:       know (frag_tmp);
  301:       frag_tmp->fr_opcode = frag_opcode;
  302:       new_broken_words = 0;
  303: 
  304:       for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
  305:         a->dispfrag = frag_tmp;
  306:     }
  307: #endif /* WORKING_DOT_WORD */
  308: 
  309:   if ((symbolP = symbol_find (sym_name)) != 0)
  310:     {
  311:       S_CLEAR_WEAKREFR (symbolP);
  312: #ifdef RESOLVE_SYMBOL_REDEFINITION
  313:       if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
  314:         return symbolP;
  315: #endif
  316:       /* Now check for undefined symbols.  */
  317:       if (LOCAL_SYMBOL_CHECK (symbolP))
  318:         {
  319:           struct local_symbol *locsym = (struct local_symbol *) symbolP;
  320: 
  321:           if (locsym->lsy_section != undefined_section
  322:               && (local_symbol_get_frag (locsym) != frag_now
  323:                   || locsym->lsy_section != now_seg
  324:                   || locsym->lsy_value != frag_now_fix ()))
  325:             {
  326:               as_bad (_("symbol `%s' is already defined"), sym_name);
  327:               return symbolP;
  328:             }
  329: 
  330:           locsym->lsy_section = now_seg;
  331:           local_symbol_set_frag (locsym, frag_now);
  332:           locsym->lsy_value = frag_now_fix ();
  333:         }
  334:       else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
  335:                || S_IS_COMMON (symbolP)
  336:                || S_IS_VOLATILE (symbolP))
  337:         {
  338:           if (S_IS_VOLATILE (symbolP))
  339:             {
  340:               symbolP = symbol_clone (symbolP, 1);
  341:               S_SET_VALUE (symbolP, 0);
  342:               S_CLEAR_VOLATILE (symbolP);
  343:             }
  344:           if (S_GET_VALUE (symbolP) == 0)
  345:             {
  346:               symbolP->sy_frag = frag_now;
  347: #ifdef OBJ_VMS
  348:               S_SET_OTHER (symbolP, const_flag);
  349: #endif
  350:               S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
  351:               S_SET_SEGMENT (symbolP, now_seg);
  352: #ifdef N_UNDF
  353:               know (N_UNDF == 0);
  354: #endif /* if we have one, it better be zero.  */
  355: 
  356:             }
  357:           else
  358:             {
  359:               /* There are still several cases to check:
  360: 
  361:                  A .comm/.lcomm symbol being redefined as initialized
  362:                  data is OK
  363: 
  364:                  A .comm/.lcomm symbol being redefined with a larger
  365:                  size is also OK
  366: 
  367:                  This only used to be allowed on VMS gas, but Sun cc
  368:                  on the sparc also depends on it.  */
  369: 
  370:               if (((!S_IS_DEBUG (symbolP)
  371:                     && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
  372:                     && S_IS_EXTERNAL (symbolP))
  373:                    || S_GET_SEGMENT (symbolP) == bss_section)
  374:                   && (now_seg == data_section
  375:                       || now_seg == bss_section
  376:                       || now_seg == S_GET_SEGMENT (symbolP)))
  377:                 {
  378:                   /* Select which of the 2 cases this is.  */
  379:                   if (now_seg != data_section)
  380:                     {
  381:                       /* New .comm for prev .comm symbol.
  382: 
  383:                          If the new size is larger we just change its
  384:                          value.  If the new size is smaller, we ignore
  385:                          this symbol.  */
  386:                       if (S_GET_VALUE (symbolP)
  387:                           < ((unsigned) frag_now_fix ()))
  388:                         {
  389:                           S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
  390:                         }
  391:                     }
  392:                   else
  393:                     {
  394:                       /* It is a .comm/.lcomm being converted to initialized
  395:                          data.  */
  396:                       symbolP->sy_frag = frag_now;
  397: #ifdef OBJ_VMS
  398:                       S_SET_OTHER (symbolP, const_flag);
  399: #endif
  400:                       S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
  401:                       S_SET_SEGMENT (symbolP, now_seg);       /* Keep N_EXT bit.  */
  402:                     }
  403:                 }
  404:               else
  405:                 {
  406: #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
  407:      && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
  408:                   static const char *od_buf = "";
  409: #else
  410:                   char od_buf[100];
  411:                   od_buf[0] = '\0';
  412:                   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
  413:                     sprintf (od_buf, "%d.%d.",
  414:                              S_GET_OTHER (symbolP),
  415:                              S_GET_DESC (symbolP));
  416: #endif
  417:                   as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
  418:                             sym_name,
  419:                             segment_name (S_GET_SEGMENT (symbolP)),
  420:                             od_buf,
  421:                             (long) S_GET_VALUE (symbolP));
  422:                 }
  423:             }                  /* if the undefined symbol has no value  */
  424:         }
  425:       else
  426:         {
  427:           /* Don't blow up if the definition is the same.  */
  428:           if (!(frag_now == symbolP->sy_frag
  429:                 && S_GET_VALUE (symbolP) == frag_now_fix ()
  430:                 && S_GET_SEGMENT (symbolP) == now_seg))
  431:             {
  432:               as_bad (_("symbol `%s' is already defined"), sym_name);
  433:               symbolP = symbol_clone (symbolP, 0);
  434:             }
  435:         }
  436: 
  437:     }
  438:   else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
  439:     {
  440:       symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
  441:                                                (valueT) frag_now_fix (),
  442:                                                frag_now);
  443:     }
  444:   else
  445:     {
  446:       symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
  447:                             frag_now);
  448: #ifdef OBJ_VMS
  449:       S_SET_OTHER (symbolP, const_flag);
  450: #endif /* OBJ_VMS */
  451: 
  452:       symbol_table_insert (symbolP);
  453:     }
  454: 
  455:   if (mri_common_symbol != NULL)
  456:     {
  457:       /* This symbol is actually being defined within an MRI common
  458:          section.  This requires special handling.  */
  459:       if (LOCAL_SYMBOL_CHECK (symbolP))
  460:         symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
  461:       symbolP->sy_value.X_op = O_symbol;
  462:       symbolP->sy_value.X_add_symbol = mri_common_symbol;
  463:       symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
  464:       symbolP->sy_frag = &zero_address_frag;
  465:       S_SET_SEGMENT (symbolP, expr_section);
  466:       symbolP->sy_mri_common = 1;
  467:     }
  468: 
  469: #ifdef tc_frob_label
  470:   tc_frob_label (symbolP);
  471: #endif
  472: #ifdef obj_frob_label
  473:   obj_frob_label (symbolP);
  474: #endif
  475: 
  476:   return symbolP;
  477: }
  478: ^L
  479: /* Die if we can't insert the symbol.  */
  480: 
  481: void
  482: symbol_table_insert (symbolS *symbolP)
  483: {
  484:   register const char *error_string;
  485: 
  486:   know (symbolP);
  487:   know (S_GET_NAME (symbolP));
  488: 
  489:   if (LOCAL_SYMBOL_CHECK (symbolP))
  490:     {
  491:       error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
  492:                                (PTR) symbolP);
  493:       if (error_string != NULL)
  494:         as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
  495:                   S_GET_NAME (symbolP), error_string);
  496:       return;
  497:     }
  498: 
  499:   if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
  500:     {
  501:       as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
  502:                 S_GET_NAME (symbolP), error_string);
  503:     }                           /* on error  */
  504: }
  505: ^L
  506: /* If a symbol name does not exist, create it as undefined, and insert
  507:    it into the symbol table.  Return a pointer to it.  */
  508: 
  509: symbolS *
  510: symbol_find_or_make (const char *name)
  511: {
  512:   register symbolS *symbolP;
  513: 
  514:   symbolP = symbol_find (name);
  515: 
  516:   if (symbolP == NULL)
  517:     {
  518:       if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
  519:         {
  520:           symbolP = md_undefined_symbol ((char *) name);
  521:           if (symbolP != NULL)
  522:             return symbolP;
  523: 
  524:           symbolP = (symbolS *) local_symbol_make (name, undefined_section,
  525:                                                    (valueT) 0,
  526:                                                    &zero_address_frag);
  527:           return symbolP;
  528:         }
  529: 
  530:       symbolP = symbol_make (name);
  531: 
  532:       symbol_table_insert (symbolP);
  533:     }                           /* if symbol wasn't found */
  534: 
  535:   return (symbolP);
  536: }
  537: 
  538: symbolS *
  539: symbol_make (const char *name)
  540: {
  541:   symbolS *symbolP;
  542: 
  543:   /* Let the machine description default it, e.g. for register names.  */
  544:   symbolP = md_undefined_symbol ((char *) name);
  545: 
  546:   if (!symbolP)
  547:     symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
  548: 
  549:   return (symbolP);
  550: }
  551: 
  552: symbolS *
  553: symbol_clone (symbolS *orgsymP, int replace)
  554: {
  555:   symbolS *newsymP;
  556:   asymbol *bsymorg, *bsymnew;
  557: 
  558:   /* Running local_symbol_convert on a clone that's not the one currently
  559:      in local_hash would incorrectly replace the hash entry.  Thus the
  560:      symbol must be converted here.  Note that the rest of the function
  561:      depends on not encountering an unconverted symbol.  */
  562:   if (LOCAL_SYMBOL_CHECK (orgsymP))
  563:     orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
  564:   bsymorg = orgsymP->bsym;
  565: 
  566:   newsymP = obstack_alloc (&notes, sizeof (*newsymP));
  567:   *newsymP = *orgsymP;
  568:   bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
  569:   if (bsymnew == NULL)
  570:     as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
  571:   newsymP->bsym = bsymnew;
  572:   bsymnew->name = bsymorg->name;
  573:   bsymnew->flags =  bsymorg->flags;
  574:   bsymnew->section =  bsymorg->section;
  575:   bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
  576:                                 bfd_asymbol_bfd (bsymnew), bsymnew);
  577: 
  578: #ifdef obj_symbol_clone_hook
  579:   obj_symbol_clone_hook (newsymP, orgsymP);
  580: #endif
  581: 
  582: #ifdef tc_symbol_clone_hook
  583:   tc_symbol_clone_hook (newsymP, orgsymP);
  584: #endif
  585: 
  586:   if (replace)
  587:     {
  588:       if (symbol_rootP == orgsymP)
  589:         symbol_rootP = newsymP;
  590:       else if (orgsymP->sy_previous)
  591:         {
  592:           orgsymP->sy_previous->sy_next = newsymP