Subversion Repositories Kolibri OS

Rev

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

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