Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/bfd/stabs.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    11/* Stabs in sections linking support.
    2    Copyright 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
     2   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
     3   Free Software Foundation, Inc.
    34   Written by Ian Lance Taylor, Cygnus Support.
    45
     
    2627#include "libbfd.h"
    2728#include "aout/stab_gnu.h"
    28 
    29 #include <ctype.h>
     29#include "safe-ctype.h"
    3030
    3131/* Stabs entries use a 12 byte format:
     
    171171   pass of the linker.  */
    172172
    173 boolean
     173bfd_boolean
    174174_bfd_link_section_stabs (abfd, psinfo, stabsec, stabstrsec, psecinfo)
    175175     bfd *abfd;
     
    179179     PTR *psecinfo;
    180180{
    181   boolean first;
     181  bfd_boolean first;
    182182  struct stab_info *sinfo;
    183   bfd_size_type count;
     183  bfd_size_type count, amt;
    184184  struct stab_section_info *secinfo;
    185185  bfd_byte *stabbuf = NULL;
     
    193193    {
    194194      /* This file does not contain stabs debugging information.  */
    195       return true;
     195      return TRUE;
    196196    }
    197197
     
    199199    {
    200200      /* Something is wrong with the format of these stab symbols.
    201         Don't try to optimize them.  */
    202       return true;
     201        Don't try to optimize them.  */
     202      return TRUE;
    203203    }
    204204
     
    206206    {
    207207      /* We shouldn't see relocations in the strings, and we aren't
    208         prepared to handle them.  */
    209       return true;
     208        prepared to handle them.  */
     209      return TRUE;
    210210    }
    211211
     
    216216    {
    217217      /* At least one of the sections is being discarded from the
    218         link, so we should just ignore them.  */
    219       return true;
    220     }
    221 
    222   first = false;
     218        link, so we should just ignore them.  */
     219      return TRUE;
     220    }
     221
     222  first = FALSE;
    223223
    224224  if (*psinfo == NULL)
    225225    {
    226226      /* Initialize the stabs information we need to keep track of.  */
    227       first = true;
    228       *psinfo = (PTR) bfd_alloc (abfd, sizeof (struct stab_info));
     227      first = TRUE;
     228      amt = sizeof (struct stab_info);
     229      *psinfo = (PTR) bfd_alloc (abfd, amt);
    229230      if (*psinfo == NULL)
    230231        goto error_return;
     
    234235        goto error_return;
    235236      /* Make sure the first byte is zero.  */
    236       (void) _bfd_stringtab_add (sinfo->strings, "", true, true);
     237      (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE);
    237238      if (! bfd_hash_table_init_n (&sinfo->includes.root,
    238239                                   stab_link_includes_newfunc,
     
    250251  count = stabsec->_raw_size / STABSIZE;
    251252
    252   *psecinfo = bfd_alloc (abfd,
    253                          (sizeof (struct stab_section_info)
    254                           + (count - 1) * sizeof (bfd_size_type)));
     253  amt = sizeof (struct stab_section_info);
     254  amt += (count - 1) * sizeof (bfd_size_type);
     255  *psecinfo = bfd_alloc (abfd, amt);
    255256  if (*psecinfo == NULL)
    256257    goto error_return;
     
    259260  secinfo->excls = NULL;
    260261  secinfo->cumulative_skips = NULL;
    261   memset (secinfo->stridxs, 0, count * sizeof (bfd_size_type));
     262  memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type));
    262263
    263264  /* Read the stabs information from abfd.  */
     
    268269    goto error_return;
    269270
    270   if (! bfd_get_section_contents (abfd, stabsec, stabbuf, 0,
     271  if (! bfd_get_section_contents (abfd, stabsec, stabbuf, (bfd_vma) 0,
    271272                                  stabsec->_raw_size)
    272       || ! bfd_get_section_contents (abfd, stabstrsec, stabstrbuf, 0,
     273      || ! bfd_get_section_contents (abfd, stabstrsec, stabstrbuf, (bfd_vma) 0,
    273274                                     stabstrsec->_raw_size))
    274275    goto error_return;
     
    286287       sym += STABSIZE, ++pstridx)
    287288    {
     289      bfd_size_type symstroff;
    288290      int type;
    289291      const char *string;
     
    300302        {
    301303          /* Special type 0 stabs indicate the offset to the next
    302              string table.  We only copy the very first one.  */
     304             string table.  We only copy the very first one.  */
    303305          stroff = next_stroff;
    304306          next_stroff += bfd_get_32 (abfd, sym + 8);
     
    309311              continue;
    310312            }
    311           first = false;
     313          first = FALSE;
    312314        }
    313315
    314316      /* Store the string in the hash table, and record the index.  */
    315       string = ((char *) stabstrbuf
    316                 + stroff
    317                 + bfd_get_32 (abfd, sym + STRDXOFF));
    318       *pstridx = _bfd_stringtab_add (sinfo->strings, string, true, true);
     317      symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF);
     318      if (symstroff >= stabstrsec->_raw_size)
     319        {
     320          (*_bfd_error_handler)
     321            (_("%s(%s+0x%lx): Stabs entry has invalid string index."),
     322             bfd_archive_filename (abfd),
     323             bfd_get_section_name (abfd, stabsec),
     324             (long) (sym - stabbuf));
     325          bfd_set_error (bfd_error_bad_value);
     326          goto error_return;
     327        }
     328      string = (char *) stabstrbuf + symstroff;
     329      *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE);
    319330
    320331      /* An N_BINCL symbol indicates the start of the stabs entries
     
    323334         symbol names, not including the file numbers in types (the
    324335         first number after an open parenthesis).  */
    325       if (type == N_BINCL)
     336      if (type == (int) N_BINCL)
    326337        {
    327338          bfd_vma val;
     
    343354              if (incl_type == 0)
    344355                break;
    345               else if (incl_type == N_EINCL)
     356              else if (incl_type == (int) N_EINCL)
    346357                {
    347358                  if (nest == 0)
     
    349360                  --nest;
    350361                }
    351               else if (incl_type == N_BINCL)
     362              else if (incl_type == (int) N_BINCL)
    352363                ++nest;
    353364              else if (nest == 0)
     
    365376                          /* Skip the file number.  */
    366377                          ++str;
    367                           while (isdigit ((unsigned char) *str))
     378                          while (ISDIGIT (*str))
    368379                            ++str;
    369380                          --str;
     
    376387             value, then replaced this one with an N_EXCL symbol.  */
    377388          incl_entry = stab_link_includes_lookup (&sinfo->includes, string,
    378                                                   true, true);
     389                                                  TRUE, TRUE);
    379390          if (incl_entry == NULL)
    380391            goto error_return;
     
    385396
    386397          /* Record this symbol, so that we can set the value
    387              correctly.  */
    388           ne = (struct stab_excl_list *) bfd_alloc (abfd, sizeof *ne);
     398             correctly.  */
     399          amt = sizeof *ne;
     400          ne = (struct stab_excl_list *) bfd_alloc (abfd, amt);
    389401          if (ne == NULL)
    390402            goto error_return;
    391403          ne->offset = sym - stabbuf;
    392404          ne->val = val;
    393           ne->type = N_BINCL;
     405          ne->type = (int) N_BINCL;
    394406          ne->next = secinfo->excls;
    395407          secinfo->excls = ne;
     
    413425              /* We have seen this header file before.  Tell the final
    414426                 pass to change the type to N_EXCL.  */
    415               ne->type = N_EXCL;
     427              ne->type = (int) N_EXCL;
    416428
    417429              /* Mark the skipped symbols.  */
     
    426438                  incl_type = incl_sym[TYPEOFF];
    427439
    428                   if (incl_type == N_EINCL)
     440                  if (incl_type == (int) N_EINCL)
    429441                    {
    430442                      if (nest == 0)
     
    436448                      --nest;
    437449                    }
    438                   else if (incl_type == N_BINCL)
     450                  else if (incl_type == (int) N_BINCL)
    439451                    ++nest;
    440452                  else if (nest == 0)
     
    474486      bfd_size_type *pskips;
    475487
    476       secinfo->cumulative_skips =
    477         (bfd_size_type *) bfd_alloc (abfd, count * sizeof (bfd_size_type));
     488      amt = count * sizeof (bfd_size_type);
     489      secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
    478490      if (secinfo->cumulative_skips == NULL)
    479491        goto error_return;
     
    493505    }
    494506
    495   return true;
     507  return TRUE;
    496508
    497509 error_return:
     
    500512  if (stabstrbuf != NULL)
    501513    free (stabstrbuf);
    502   return false;
     514  return FALSE;
     515}
     516
     517
     518
     519/* This function is called for each input file before the stab
     520   section is relocated.  It discards stab entries for discarded
     521   functions and variables.  The function returns TRUE iff
     522   any entries have been deleted.
     523*/
     524
     525bfd_boolean
     526_bfd_discard_section_stabs (abfd, stabsec, psecinfo,
     527                            reloc_symbol_deleted_p, cookie)
     528     bfd *abfd;
     529     asection *stabsec;
     530     PTR psecinfo;
     531     bfd_boolean (*reloc_symbol_deleted_p) PARAMS ((bfd_vma, PTR));
     532     PTR cookie;
     533{
     534  bfd_size_type count, amt;
     535  struct stab_section_info *secinfo;
     536  bfd_byte *stabbuf = NULL;
     537  bfd_byte *sym, *symend;
     538  bfd_size_type skip;
     539  bfd_size_type *pstridx;
     540  int deleting;
     541
     542  if (stabsec->_raw_size == 0)
     543    {
     544      /* This file does not contain stabs debugging information.  */
     545      return FALSE;
     546    }
     547
     548  if (stabsec->_raw_size % STABSIZE != 0)
     549    {
     550      /* Something is wrong with the format of these stab symbols.
     551         Don't try to optimize them.  */
     552      return FALSE;
     553    }
     554
     555  if ((stabsec->output_section != NULL
     556       && bfd_is_abs_section (stabsec->output_section)))
     557    {
     558      /* At least one of the sections is being discarded from the
     559         link, so we should just ignore them.  */
     560      return FALSE;
     561    }
     562
     563  /* We should have initialized our data in _bfd_link_stab_sections.
     564     If there was some bizarre error reading the string sections, though,
     565     we might not have.  Bail rather than asserting.  */
     566  if (psecinfo == NULL)
     567    return FALSE;
     568
     569  count = stabsec->_raw_size / STABSIZE;
     570  secinfo = (struct stab_section_info *) psecinfo;
     571
     572  /* Read the stabs information from abfd.  */
     573
     574  stabbuf = (bfd_byte *) bfd_malloc (stabsec->_raw_size);
     575  if (stabbuf == NULL)
     576    goto error_return;
     577
     578  if (! bfd_get_section_contents (abfd, stabsec, stabbuf, (bfd_vma) 0,
     579                                  stabsec->_raw_size))
     580    goto error_return;
     581
     582  /* Look through the stabs symbols and discard any information for
     583     discarded functions.  */
     584
     585  skip = 0;
     586  deleting = -1;
     587
     588  symend = stabbuf + stabsec->_raw_size;
     589  for (sym = stabbuf, pstridx = secinfo->stridxs;
     590       sym < symend;
     591       sym += STABSIZE, ++pstridx)
     592    {
     593      int type;
     594
     595      if (*pstridx == (bfd_size_type) -1)
     596        {
     597          /* This stab was deleted in a previous pass.  */
     598          continue;
     599        }
     600
     601      type = sym[TYPEOFF];
     602
     603      if (type == (int) N_FUN)
     604        {
     605          int strx = bfd_get_32 (abfd, sym + STRDXOFF);
     606
     607          if (strx == 0)
     608            {
     609              if (deleting)
     610                {
     611                  skip++;
     612                  *pstridx = -1;
     613                }
     614              deleting = -1;
     615              continue;
     616            }
     617          deleting = 0;
     618          if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
     619            deleting = 1;
     620        }
     621
     622      if (deleting == 1)
     623        {
     624          *pstridx = -1;
     625          skip++;
     626        }
     627      else if (deleting == -1)
     628        {
     629          /* Outside of a function.  Check for deleted variables.  */
     630          if (type == (int) N_STSYM || type == (int) N_LCSYM)
     631            if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
     632              {
     633                *pstridx = -1;
     634                skip ++;
     635              }
     636          /* We should also check for N_GSYM entries which reference a
     637             deleted global, but those are less harmful to debuggers
     638             and would require parsing the stab strings.  */
     639        }
     640    }
     641
     642  free (stabbuf);
     643  stabbuf = NULL;
     644
     645  /* Shrink the stabsec as needed.  */
     646  stabsec->_cooked_size -= skip * STABSIZE;
     647  if (stabsec->_cooked_size == 0)
     648    stabsec->flags |= SEC_EXCLUDE;
     649
     650  /* Recalculate the `cumulative_skips' array now that stabs have been
     651     deleted for this section.  */
     652
     653  if (skip != 0)
     654    {
     655      bfd_size_type i, offset;
     656      bfd_size_type *pskips;
     657
     658      if (secinfo->cumulative_skips == NULL)
     659        {
     660          amt = count * sizeof (bfd_size_type);
     661          secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
     662          if (secinfo->cumulative_skips == NULL)
     663            goto error_return;
     664        }
     665
     666      pskips = secinfo->cumulative_skips;
     667      pstridx = secinfo->stridxs;
     668      offset = 0;
     669
     670      for (i = 0; i < count; i++, pskips++, pstridx++)
     671        {
     672          *pskips = offset;
     673          if (*pstridx == (bfd_size_type) -1)
     674            offset += STABSIZE;
     675        }
     676
     677      BFD_ASSERT (offset != 0);
     678    }
     679
     680  return skip > 0;
     681
     682 error_return:
     683  if (stabbuf != NULL)
     684    free (stabbuf);
     685  return FALSE;
    503686}
    504687
     
    506689   contents.  */
    507690
    508 boolean
     691bfd_boolean
    509692_bfd_write_section_stabs (output_bfd, psinfo, stabsec, psecinfo, contents)
    510693     bfd *output_bfd;
     
    525708  if (secinfo == NULL)
    526709    return bfd_set_section_contents (output_bfd, stabsec->output_section,
    527                                      contents, stabsec->output_offset,
     710                                     contents,
     711                                     (file_ptr) stabsec->output_offset,
    528712                                     stabsec->_raw_size);
    529713
     
    556740            {
    557741              /* This is the header symbol for the stabs section.  We
    558                 don't really need one, since we have merged all the
    559                 input stabs sections into one, but we generate one
    560                 for the benefit of readers which expect to see one.  */
     742                don't really need one, since we have merged all the
     743                input stabs sections into one, but we generate one
     744                for the benefit of readers which expect to see one.  */
    561745              BFD_ASSERT (sym == contents);
    562746              bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
     
    574758
    575759  return bfd_set_section_contents (output_bfd, stabsec->output_section,
    576                                    contents, stabsec->output_offset,
     760                                   contents, (file_ptr) stabsec->output_offset,
    577761                                   stabsec->_cooked_size);
    578762}
     
    580764/* Write out the .stabstr section.  */
    581765
    582 boolean
     766bfd_boolean
    583767_bfd_write_stab_strings (output_bfd, psinfo)
    584768     bfd *output_bfd;
     
    590774
    591775  if (sinfo == NULL)
    592     return true;
     776    return TRUE;
    593777
    594778  if (bfd_is_abs_section (sinfo->stabstr->output_section))
    595779    {
    596780      /* The section was discarded from the link.  */
    597       return true;
     781      return TRUE;
    598782    }
    599783
     
    603787
    604788  if (bfd_seek (output_bfd,
    605                 (sinfo->stabstr->output_section->filepos
    606                 + sinfo->stabstr->output_offset),
     789                (file_ptr) (sinfo->stabstr->output_section->filepos
     790                            + sinfo->stabstr->output_offset),
    607791                SEEK_SET) != 0)
    608     return false;
     792    return FALSE;
    609793
    610794  if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
    611     return false;
     795    return FALSE;
    612796
    613797  /* We no longer need the stabs information.  */
     
    615799  bfd_hash_table_free (&sinfo->includes.root);
    616800
    617   return true;
     801  return TRUE;
    618802}
    619803
Note: See TracChangeset for help on using the changeset viewer.