Subversion Repositories Kolibri OS

Rev

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

  1. /* Stabs in sections linking support.
  2.    Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor, Cygnus Support.
  4.  
  5.    This file is part of BFD, the Binary File Descriptor library.
  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,
  20.    MA 02110-1301, USA.  */
  21.  
  22.  
  23. /* This file contains support for linking stabs in sections, as used
  24.    on COFF and ELF.  */
  25.  
  26. #include "sysdep.h"
  27. #include "bfd.h"
  28. #include "libbfd.h"
  29. #include "aout/stab_gnu.h"
  30. #include "safe-ctype.h"
  31.  
  32. /* Stabs entries use a 12 byte format:
  33.      4 byte string table index
  34.      1 byte stab type
  35.      1 byte stab other field
  36.      2 byte stab desc field
  37.      4 byte stab value
  38.    FIXME: This will have to change for a 64 bit object format.
  39.  
  40.    The stabs symbols are divided into compilation units.  For the
  41.    first entry in each unit, the type of 0, the value is the length of
  42.    the string table for this unit, and the desc field is the number of
  43.    stabs symbols for this unit.  */
  44.  
  45. #define STRDXOFF  0
  46. #define TYPEOFF   4
  47. #define OTHEROFF  5
  48. #define DESCOFF   6
  49. #define VALOFF    8
  50. #define STABSIZE  12
  51.  
  52. /* A linked list of totals that we have found for a particular header
  53.    file.  A total is a unique identifier for a particular BINCL...EINCL
  54.    sequence of STABs that can be used to identify duplicate sequences.
  55.    It consists of three fields, 'sum_chars' which is the sum of all the
  56.    STABS characters; 'num_chars' which is the number of these charactes
  57.    and 'symb' which is a buffer of all the symbols in the sequence.  This
  58.    buffer is only checked as a last resort.  */
  59.  
  60. struct stab_link_includes_totals
  61. {
  62.   struct stab_link_includes_totals *next;
  63.   bfd_vma sum_chars;  /* Accumulated sum of STABS characters.  */
  64.   bfd_vma num_chars;  /* Number of STABS characters.  */
  65.   const char* symb;   /* The STABS characters themselves.  */
  66. };
  67.  
  68. /* An entry in the header file hash table.  */
  69.  
  70. struct stab_link_includes_entry
  71. {
  72.   struct bfd_hash_entry root;
  73.   /* List of totals we have found for this file.  */
  74.   struct stab_link_includes_totals *totals;
  75. };
  76.  
  77. /* This structure is used to hold a list of N_BINCL symbols, some of
  78.    which might be converted into N_EXCL symbols.  */
  79.  
  80. struct stab_excl_list
  81. {
  82.   /* The next symbol to convert.  */
  83.   struct stab_excl_list *next;
  84.   /* The offset to this symbol in the section contents.  */
  85.   bfd_size_type offset;
  86.   /* The value to use for the symbol.  */
  87.   bfd_vma val;
  88.   /* The type of this symbol (N_BINCL or N_EXCL).  */
  89.   int type;
  90. };
  91.  
  92. /* This structure is stored with each .stab section.  */
  93.  
  94. struct stab_section_info
  95. {
  96.   /* This is a linked list of N_BINCL symbols which should be
  97.      converted into N_EXCL symbols.  */
  98.   struct stab_excl_list *excls;
  99.  
  100.   /* This is used to map input stab offsets within their sections
  101.      to output stab offsets, to take into account stabs that have
  102.      been deleted.  If it is NULL, the output offsets are the same
  103.      as the input offsets, because no stabs have been deleted from
  104.      this section.  Otherwise the i'th entry is the number of
  105.      bytes of stabs that have been deleted prior to the i'th
  106.      stab.  */
  107.   bfd_size_type *cumulative_skips;
  108.  
  109.   /* This is an array of string indices.  For each stab symbol, we
  110.      store the string index here.  If a stab symbol should not be
  111.      included in the final output, the string index is -1.  */
  112.   bfd_size_type stridxs[1];
  113. };
  114.  
  115. /* The function to create a new entry in the header file hash table.  */
  116.  
  117. static struct bfd_hash_entry *
  118. stab_link_includes_newfunc (struct bfd_hash_entry *entry,
  119.                             struct bfd_hash_table *table,
  120.                             const char *string)
  121. {
  122.   struct stab_link_includes_entry *ret =
  123.     (struct stab_link_includes_entry *) entry;
  124.  
  125.   /* Allocate the structure if it has not already been allocated by a
  126.      subclass.  */
  127.   if (ret == NULL)
  128.     ret = (struct stab_link_includes_entry *)
  129.         bfd_hash_allocate (table, sizeof (struct stab_link_includes_entry));
  130.   if (ret == NULL)
  131.     return NULL;
  132.  
  133.   /* Call the allocation method of the superclass.  */
  134.   ret = ((struct stab_link_includes_entry *)
  135.          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  136.   if (ret)
  137.     /* Set local fields.  */
  138.     ret->totals = NULL;
  139.  
  140.   return (struct bfd_hash_entry *) ret;
  141. }
  142. /* This function is called for each input file from the add_symbols
  143.    pass of the linker.  */
  144.  
  145. bfd_boolean
  146. _bfd_link_section_stabs (bfd *abfd,
  147.                          struct stab_info *sinfo,
  148.                          asection *stabsec,
  149.                          asection *stabstrsec,
  150.                          void * *psecinfo,
  151.                          bfd_size_type *pstring_offset)
  152. {
  153.   bfd_boolean first;
  154.   bfd_size_type count, amt;
  155.   struct stab_section_info *secinfo;
  156.   bfd_byte *stabbuf = NULL;
  157.   bfd_byte *stabstrbuf = NULL;
  158.   bfd_byte *sym, *symend;
  159.   bfd_size_type stroff, next_stroff, skip;
  160.   bfd_size_type *pstridx;
  161.  
  162.   if (stabsec->size == 0
  163.       || stabstrsec->size == 0)
  164.     /* This file does not contain stabs debugging information.  */
  165.     return TRUE;
  166.  
  167.   if (stabsec->size % STABSIZE != 0)
  168.     /* Something is wrong with the format of these stab symbols.
  169.        Don't try to optimize them.  */
  170.     return TRUE;
  171.  
  172.   if ((stabstrsec->flags & SEC_RELOC) != 0)
  173.     /* We shouldn't see relocations in the strings, and we aren't
  174.        prepared to handle them.  */
  175.     return TRUE;
  176.  
  177.   if (bfd_is_abs_section (stabsec->output_section)
  178.       || bfd_is_abs_section (stabstrsec->output_section))
  179.     /* At least one of the sections is being discarded from the
  180.        link, so we should just ignore them.  */
  181.     return TRUE;
  182.  
  183.   first = FALSE;
  184.  
  185.   if (sinfo->stabstr == NULL)
  186.     {
  187.       flagword flags;
  188.  
  189.       /* Initialize the stabs information we need to keep track of.  */
  190.       first = TRUE;
  191.       sinfo->strings = _bfd_stringtab_init ();
  192.       if (sinfo->strings == NULL)
  193.         goto error_return;
  194.       /* Make sure the first byte is zero.  */
  195.       (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE);
  196.       if (! bfd_hash_table_init (&sinfo->includes,
  197.                                  stab_link_includes_newfunc,
  198.                                  sizeof (struct stab_link_includes_entry)))
  199.         goto error_return;
  200.       flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING
  201.                | SEC_LINKER_CREATED);
  202.       sinfo->stabstr = bfd_make_section_anyway_with_flags (abfd, ".stabstr",
  203.                                                            flags);
  204.       if (sinfo->stabstr == NULL)
  205.         goto error_return;
  206.     }
  207.  
  208.   /* Initialize the information we are going to store for this .stab
  209.      section.  */
  210.   count = stabsec->size / STABSIZE;
  211.  
  212.   amt = sizeof (struct stab_section_info);
  213.   amt += (count - 1) * sizeof (bfd_size_type);
  214.   *psecinfo = bfd_alloc (abfd, amt);
  215.   if (*psecinfo == NULL)
  216.     goto error_return;
  217.  
  218.   secinfo = (struct stab_section_info *) *psecinfo;
  219.   secinfo->excls = NULL;
  220.   stabsec->rawsize = stabsec->size;
  221.   secinfo->cumulative_skips = NULL;
  222.   memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type));
  223.  
  224.   /* Read the stabs information from abfd.  */
  225.   if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf)
  226.       || !bfd_malloc_and_get_section (abfd, stabstrsec, &stabstrbuf))
  227.     goto error_return;
  228.  
  229.   /* Look through the stabs symbols, work out the new string indices,
  230.      and identify N_BINCL symbols which can be eliminated.  */
  231.   stroff = 0;
  232.   /* The stabs sections can be split when
  233.      -split-by-reloc/-split-by-file is used.  We must keep track of
  234.      each stab section's place in the single concatenated string
  235.      table.  */
  236.   next_stroff = pstring_offset ? *pstring_offset : 0;
  237.   skip = 0;
  238.  
  239.   symend = stabbuf + stabsec->size;
  240.   for (sym = stabbuf, pstridx = secinfo->stridxs;
  241.        sym < symend;
  242.        sym += STABSIZE, ++pstridx)
  243.     {
  244.       bfd_size_type symstroff;
  245.       int type;
  246.       const char *string;
  247.  
  248.       if (*pstridx != 0)
  249.         /* This symbol has already been handled by an N_BINCL pass.  */
  250.         continue;
  251.  
  252.       type = sym[TYPEOFF];
  253.  
  254.       if (type == 0)
  255.         {
  256.           /* Special type 0 stabs indicate the offset to the next
  257.              string table.  We only copy the very first one.  */
  258.           stroff = next_stroff;
  259.           next_stroff += bfd_get_32 (abfd, sym + 8);
  260.           if (pstring_offset)
  261.             *pstring_offset = next_stroff;
  262.           if (! first)
  263.             {
  264.               *pstridx = (bfd_size_type) -1;
  265.               ++skip;
  266.               continue;
  267.             }
  268.           first = FALSE;
  269.         }
  270.  
  271.       /* Store the string in the hash table, and record the index.  */
  272.       symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF);
  273.       if (symstroff >= stabstrsec->size)
  274.         {
  275.           (*_bfd_error_handler)
  276.             (_("%B(%A+0x%lx): Stabs entry has invalid string index."),
  277.              abfd, stabsec, (long) (sym - stabbuf));
  278.           bfd_set_error (bfd_error_bad_value);
  279.           goto error_return;
  280.         }
  281.       string = (char *) stabstrbuf + symstroff;
  282.       *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE);
  283.  
  284.       /* An N_BINCL symbol indicates the start of the stabs entries
  285.          for a header file.  We need to scan ahead to the next N_EINCL
  286.          symbol, ignoring nesting, adding up all the characters in the
  287.          symbol names, not including the file numbers in types (the
  288.          first number after an open parenthesis).  */
  289.       if (type == (int) N_BINCL)
  290.         {
  291.           bfd_vma sum_chars;
  292.           bfd_vma num_chars;
  293.           bfd_vma buf_len = 0;
  294.           char * symb;
  295.           char * symb_rover;
  296.           int nest;
  297.           bfd_byte * incl_sym;
  298.           struct stab_link_includes_entry * incl_entry;
  299.           struct stab_link_includes_totals * t;
  300.           struct stab_excl_list * ne;
  301.  
  302.           symb = symb_rover = NULL;
  303.           sum_chars = num_chars = 0;
  304.           nest = 0;
  305.  
  306.           for (incl_sym = sym + STABSIZE;
  307.                incl_sym < symend;
  308.                incl_sym += STABSIZE)
  309.             {
  310.               int incl_type;
  311.  
  312.               incl_type = incl_sym[TYPEOFF];
  313.               if (incl_type == 0)
  314.                 break;
  315.               else if (incl_type == (int) N_EXCL)
  316.                 continue;
  317.               else if (incl_type == (int) N_EINCL)
  318.                 {
  319.                   if (nest == 0)
  320.                     break;
  321.                   --nest;
  322.                 }
  323.               else if (incl_type == (int) N_BINCL)
  324.                 ++nest;
  325.               else if (nest == 0)
  326.                 {
  327.                   const char *str;
  328.  
  329.                   str = ((char *) stabstrbuf
  330.                          + stroff
  331.                          + bfd_get_32 (abfd, incl_sym + STRDXOFF));
  332.                   for (; *str != '\0'; str++)
  333.                     {
  334.                       if (num_chars >= buf_len)
  335.                         {
  336.                           buf_len += 32 * 1024;
  337.                           symb = (char *) bfd_realloc_or_free (symb, buf_len);
  338.                           if (symb == NULL)
  339.                             goto error_return;
  340.                           symb_rover = symb + num_chars;
  341.                         }
  342.                       * symb_rover ++ = * str;
  343.                       sum_chars += *str;
  344.                       num_chars ++;
  345.                       if (*str == '(')
  346.                         {
  347.                           /* Skip the file number.  */
  348.                           ++str;
  349.                           while (ISDIGIT (*str))
  350.                             ++str;
  351.                           --str;
  352.                         }
  353.                     }
  354.                 }
  355.             }
  356.  
  357.           BFD_ASSERT (num_chars == (bfd_vma) (symb_rover - symb));
  358.  
  359.           /* If we have already included a header file with the same
  360.              value, then replaced this one with an N_EXCL symbol.  */
  361.           incl_entry = (struct stab_link_includes_entry * )
  362.             bfd_hash_lookup (&sinfo->includes, string, TRUE, TRUE);
  363.           if (incl_entry == NULL)
  364.             goto error_return;
  365.  
  366.           for (t = incl_entry->totals; t != NULL; t = t->next)
  367.             if (t->sum_chars == sum_chars
  368.                 && t->num_chars == num_chars
  369.                 && memcmp (t->symb, symb, num_chars) == 0)
  370.               break;
  371.  
  372.           /* Record this symbol, so that we can set the value
  373.              correctly.  */
  374.           amt = sizeof *ne;
  375.           ne = (struct stab_excl_list *) bfd_alloc (abfd, amt);
  376.           if (ne == NULL)
  377.             goto error_return;
  378.           ne->offset = sym - stabbuf;
  379.           ne->val = sum_chars;
  380.           ne->type = (int) N_BINCL;
  381.           ne->next = secinfo->excls;
  382.           secinfo->excls = ne;
  383.  
  384.           if (t == NULL)
  385.             {
  386.               /* This is the first time we have seen this header file
  387.                  with this set of stabs strings.  */
  388.               t = (struct stab_link_includes_totals *)
  389.                   bfd_hash_allocate (&sinfo->includes, sizeof *t);
  390.               if (t == NULL)
  391.                 goto error_return;
  392.               t->sum_chars = sum_chars;
  393.               t->num_chars = num_chars;
  394.               /* Trim data down.  */
  395.               t->symb = symb = (char *) bfd_realloc_or_free (symb, num_chars);
  396.               t->next = incl_entry->totals;
  397.               incl_entry->totals = t;
  398.             }
  399.           else
  400.             {
  401.               bfd_size_type *incl_pstridx;
  402.  
  403.               /* We have seen this header file before.  Tell the final
  404.                  pass to change the type to N_EXCL.  */
  405.               ne->type = (int) N_EXCL;
  406.  
  407.               /* Free off superfluous symbols.  */
  408.               free (symb);
  409.  
  410.               /* Mark the skipped symbols.  */
  411.  
  412.               nest = 0;
  413.               for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1;
  414.                    incl_sym < symend;
  415.                    incl_sym += STABSIZE, ++incl_pstridx)
  416.                 {
  417.                   int incl_type;
  418.  
  419.                   incl_type = incl_sym[TYPEOFF];
  420.  
  421.                   if (incl_type == (int) N_EINCL)
  422.                     {
  423.                       if (nest == 0)
  424.                         {
  425.                           *incl_pstridx = (bfd_size_type) -1;
  426.                           ++skip;
  427.                           break;
  428.                         }
  429.                       --nest;
  430.                     }
  431.                   else if (incl_type == (int) N_BINCL)
  432.                     ++nest;
  433.                   else if (incl_type == (int) N_EXCL)
  434.                     /* Keep existing exclusion marks.  */
  435.                     continue;
  436.                   else if (nest == 0)
  437.                     {
  438.                       *incl_pstridx = (bfd_size_type) -1;
  439.                       ++skip;
  440.                     }
  441.                 }
  442.             }
  443.         }
  444.     }
  445.  
  446.   free (stabbuf);
  447.   stabbuf = NULL;
  448.   free (stabstrbuf);
  449.   stabstrbuf = NULL;
  450.  
  451.   /* We need to set the section sizes such that the linker will
  452.      compute the output section sizes correctly.  We set the .stab
  453.      size to not include the entries we don't want.  We set
  454.      SEC_EXCLUDE for the .stabstr section, so that it will be dropped
  455.      from the link.  We record the size of the strtab in the first
  456.      .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
  457.      for that section.  */
  458.   stabsec->size = (count - skip) * STABSIZE;
  459.   if (stabsec->size == 0)
  460.     stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
  461.   stabstrsec->flags |= SEC_EXCLUDE | SEC_KEEP;
  462.   sinfo->stabstr->size = _bfd_stringtab_size (sinfo->strings);
  463.  
  464.   /* Calculate the `cumulative_skips' array now that stabs have been
  465.      deleted for this section.  */
  466.  
  467.   if (skip != 0)
  468.     {
  469.       bfd_size_type i, offset;
  470.       bfd_size_type *pskips;
  471.  
  472.       amt = count * sizeof (bfd_size_type);
  473.       secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
  474.       if (secinfo->cumulative_skips == NULL)
  475.         goto error_return;
  476.  
  477.       pskips = secinfo->cumulative_skips;
  478.       pstridx = secinfo->stridxs;
  479.       offset = 0;
  480.  
  481.       for (i = 0; i < count; i++, pskips++, pstridx++)
  482.         {
  483.           *pskips = offset;
  484.           if (*pstridx == (bfd_size_type) -1)
  485.             offset += STABSIZE;
  486.         }
  487.  
  488.       BFD_ASSERT (offset != 0);
  489.     }
  490.  
  491.   return TRUE;
  492.  
  493.  error_return:
  494.   if (stabbuf != NULL)
  495.     free (stabbuf);
  496.   if (stabstrbuf != NULL)
  497.     free (stabstrbuf);
  498.   return FALSE;
  499. }
  500. /* This function is called for each input file before the stab
  501.    section is relocated.  It discards stab entries for discarded
  502.    functions and variables.  The function returns TRUE iff
  503.    any entries have been deleted.
  504. */
  505.  
  506. bfd_boolean
  507. _bfd_discard_section_stabs (bfd *abfd,
  508.                             asection *stabsec,
  509.                             void * psecinfo,
  510.                             bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
  511.                             void * cookie)
  512. {
  513.   bfd_size_type count, amt;
  514.   struct stab_section_info *secinfo;
  515.   bfd_byte *stabbuf = NULL;
  516.   bfd_byte *sym, *symend;
  517.   bfd_size_type skip;
  518.   bfd_size_type *pstridx;
  519.   int deleting;
  520.  
  521.   if (stabsec->size == 0)
  522.     /* This file does not contain stabs debugging information.  */
  523.     return FALSE;
  524.  
  525.   if (stabsec->size % STABSIZE != 0)
  526.     /* Something is wrong with the format of these stab symbols.
  527.        Don't try to optimize them.  */
  528.     return FALSE;
  529.  
  530.   if ((stabsec->output_section != NULL
  531.        && bfd_is_abs_section (stabsec->output_section)))
  532.     /* At least one of the sections is being discarded from the
  533.        link, so we should just ignore them.  */
  534.     return FALSE;
  535.  
  536.   /* We should have initialized our data in _bfd_link_section_stabs.
  537.      If there was some bizarre error reading the string sections, though,
  538.      we might not have.  Bail rather than asserting.  */
  539.   if (psecinfo == NULL)
  540.     return FALSE;
  541.  
  542.   count = stabsec->rawsize / STABSIZE;
  543.   secinfo = (struct stab_section_info *) psecinfo;
  544.  
  545.   /* Read the stabs information from abfd.  */
  546.   if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf))
  547.     goto error_return;
  548.  
  549.   /* Look through the stabs symbols and discard any information for
  550.      discarded functions.  */
  551.   skip = 0;
  552.   deleting = -1;
  553.  
  554.   symend = stabbuf + stabsec->rawsize;
  555.   for (sym = stabbuf, pstridx = secinfo->stridxs;
  556.        sym < symend;
  557.        sym += STABSIZE, ++pstridx)
  558.     {
  559.       int type;
  560.  
  561.       if (*pstridx == (bfd_size_type) -1)
  562.         /* This stab was deleted in a previous pass.  */
  563.         continue;
  564.  
  565.       type = sym[TYPEOFF];
  566.  
  567.       if (type == (int) N_FUN)
  568.         {
  569.           int strx = bfd_get_32 (abfd, sym + STRDXOFF);
  570.  
  571.           if (strx == 0)
  572.             {
  573.               if (deleting)
  574.                 {
  575.                   skip++;
  576.                   *pstridx = -1;
  577.                 }
  578.               deleting = -1;
  579.               continue;
  580.             }
  581.           deleting = 0;
  582.           if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
  583.             deleting = 1;
  584.         }
  585.  
  586.       if (deleting == 1)
  587.         {
  588.           *pstridx = -1;
  589.           skip++;
  590.         }
  591.       else if (deleting == -1)
  592.         {
  593.           /* Outside of a function.  Check for deleted variables.  */
  594.           if (type == (int) N_STSYM || type == (int) N_LCSYM)
  595.             if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
  596.               {
  597.                 *pstridx = -1;
  598.                 skip ++;
  599.               }
  600.           /* We should also check for N_GSYM entries which reference a
  601.              deleted global, but those are less harmful to debuggers
  602.              and would require parsing the stab strings.  */
  603.         }
  604.     }
  605.  
  606.   free (stabbuf);
  607.   stabbuf = NULL;
  608.  
  609.   /* Shrink the stabsec as needed.  */
  610.   stabsec->size -= skip * STABSIZE;
  611.   if (stabsec->size == 0)
  612.     stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
  613.  
  614.   /* Recalculate the `cumulative_skips' array now that stabs have been
  615.      deleted for this section.  */
  616.  
  617.   if (skip != 0)
  618.     {
  619.       bfd_size_type i, offset;
  620.       bfd_size_type *pskips;
  621.  
  622.       if (secinfo->cumulative_skips == NULL)
  623.         {
  624.           amt = count * sizeof (bfd_size_type);
  625.           secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
  626.           if (secinfo->cumulative_skips == NULL)
  627.             goto error_return;
  628.         }
  629.  
  630.       pskips = secinfo->cumulative_skips;
  631.       pstridx = secinfo->stridxs;
  632.       offset = 0;
  633.  
  634.       for (i = 0; i < count; i++, pskips++, pstridx++)
  635.         {
  636.           *pskips = offset;
  637.           if (*pstridx == (bfd_size_type) -1)
  638.             offset += STABSIZE;
  639.         }
  640.  
  641.       BFD_ASSERT (offset != 0);
  642.     }
  643.  
  644.   return skip > 0;
  645.  
  646.  error_return:
  647.   if (stabbuf != NULL)
  648.     free (stabbuf);
  649.   return FALSE;
  650. }
  651.  
  652. /* Write out the stab section.  This is called with the relocated
  653.    contents.  */
  654.  
  655. bfd_boolean
  656. _bfd_write_section_stabs (bfd *output_bfd,
  657.                           struct stab_info *sinfo,
  658.                           asection *stabsec,
  659.                           void * *psecinfo,
  660.                           bfd_byte *contents)
  661. {
  662.   struct stab_section_info *secinfo;
  663.   struct stab_excl_list *e;
  664.   bfd_byte *sym, *tosym, *symend;
  665.   bfd_size_type *pstridx;
  666.  
  667.   secinfo = (struct stab_section_info *) *psecinfo;
  668.  
  669.   if (secinfo == NULL)
  670.     return bfd_set_section_contents (output_bfd, stabsec->output_section,
  671.                                      contents, stabsec->output_offset,
  672.                                      stabsec->size);
  673.  
  674.   /* Handle each N_BINCL entry.  */
  675.   for (e = secinfo->excls; e != NULL; e = e->next)
  676.     {
  677.       bfd_byte *excl_sym;
  678.  
  679.       BFD_ASSERT (e->offset < stabsec->rawsize);
  680.       excl_sym = contents + e->offset;
  681.       bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
  682.       excl_sym[TYPEOFF] = e->type;
  683.     }
  684.  
  685.   /* Copy over all the stabs symbols, omitting the ones we don't want,
  686.      and correcting the string indices for those we do want.  */
  687.   tosym = contents;
  688.   symend = contents + stabsec->rawsize;
  689.   for (sym = contents, pstridx = secinfo->stridxs;
  690.        sym < symend;
  691.        sym += STABSIZE, ++pstridx)
  692.     {
  693.       if (*pstridx != (bfd_size_type) -1)
  694.         {
  695.           if (tosym != sym)
  696.             memcpy (tosym, sym, STABSIZE);
  697.           bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
  698.  
  699.           if (sym[TYPEOFF] == 0)
  700.             {
  701.               /* This is the header symbol for the stabs section.  We
  702.                  don't really need one, since we have merged all the
  703.                  input stabs sections into one, but we generate one
  704.                  for the benefit of readers which expect to see one.  */
  705.               BFD_ASSERT (sym == contents);
  706.               bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
  707.                           tosym + VALOFF);
  708.               bfd_put_16 (output_bfd,
  709.                           stabsec->output_section->size / STABSIZE - 1,
  710.                           tosym + DESCOFF);
  711.             }
  712.  
  713.           tosym += STABSIZE;
  714.         }
  715.     }
  716.  
  717.   BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->size);
  718.  
  719.   return bfd_set_section_contents (output_bfd, stabsec->output_section,
  720.                                    contents, (file_ptr) stabsec->output_offset,
  721.                                    stabsec->size);
  722. }
  723.  
  724. /* Write out the .stabstr section.  */
  725.  
  726. bfd_boolean
  727. _bfd_write_stab_strings (bfd *output_bfd, struct stab_info *sinfo)
  728. {
  729.   if (bfd_is_abs_section (sinfo->stabstr->output_section))
  730.     /* The section was discarded from the link.  */
  731.     return TRUE;
  732.  
  733.   BFD_ASSERT ((sinfo->stabstr->output_offset
  734.                + _bfd_stringtab_size (sinfo->strings))
  735.               <= sinfo->stabstr->output_section->size);
  736.  
  737.   if (bfd_seek (output_bfd,
  738.                 (file_ptr) (sinfo->stabstr->output_section->filepos
  739.                             + sinfo->stabstr->output_offset),
  740.                 SEEK_SET) != 0)
  741.     return FALSE;
  742.  
  743.   if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
  744.     return FALSE;
  745.  
  746.   /* We no longer need the stabs information.  */
  747.   _bfd_stringtab_free (sinfo->strings);
  748.   bfd_hash_table_free (&sinfo->includes);
  749.  
  750.   return TRUE;
  751. }
  752.  
  753. /* Adjust an address in the .stab section.  Given OFFSET within
  754.    STABSEC, this returns the new offset in the adjusted stab section,
  755.    or -1 if the address refers to a stab which has been removed.  */
  756.  
  757. bfd_vma
  758. _bfd_stab_section_offset (asection *stabsec,
  759.                           void * psecinfo,
  760.                           bfd_vma offset)
  761. {
  762.   struct stab_section_info *secinfo;
  763.  
  764.   secinfo = (struct stab_section_info *) psecinfo;
  765.  
  766.   if (secinfo == NULL)
  767.     return offset;
  768.  
  769.   if (offset >= stabsec->rawsize)
  770.     return offset - stabsec->rawsize + stabsec->size;
  771.  
  772.   if (secinfo->cumulative_skips)
  773.     {
  774.       bfd_vma i;
  775.  
  776.       i = offset / STABSIZE;
  777.  
  778.       if (secinfo->stridxs [i] == (bfd_size_type) -1)
  779.         return (bfd_vma) -1;
  780.  
  781.       return offset - secinfo->cumulative_skips [i];
  782.     }
  783.  
  784.   return offset;
  785. }
  786.