Home | History | Annotate | Line # | Download | only in bfd
ecofflink.c revision 1.1.1.7
      1      1.1  christos /* Routines to link ECOFF debugging information.
      2  1.1.1.7  christos    Copyright (C) 1993-2020 Free Software Foundation, Inc.
      3      1.1  christos    Written by Ian Lance Taylor, Cygnus Support, <ian (at) cygnus.com>.
      4      1.1  christos 
      5      1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program; if not, write to the Free Software
     19      1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20      1.1  christos    MA 02110-1301, USA.  */
     21      1.1  christos 
     22      1.1  christos #include "sysdep.h"
     23      1.1  christos #include "bfd.h"
     24      1.1  christos #include "bfdlink.h"
     25      1.1  christos #include "libbfd.h"
     26  1.1.1.7  christos #include "ecoff-bfd.h"
     27      1.1  christos #include "objalloc.h"
     28      1.1  christos #include "aout/stab_gnu.h"
     29      1.1  christos #include "coff/internal.h"
     30      1.1  christos #include "coff/sym.h"
     31      1.1  christos #include "coff/symconst.h"
     32      1.1  christos #include "coff/ecoff.h"
     33      1.1  christos #include "libcoff.h"
     34      1.1  christos #include "libecoff.h"
     35      1.1  christos 
     36      1.1  christos /* Routines to swap auxiliary information in and out.  I am assuming
     38      1.1  christos    that the auxiliary information format is always going to be target
     39      1.1  christos    independent.  */
     40      1.1  christos 
     41      1.1  christos /* Swap in a type information record.
     42      1.1  christos    BIGEND says whether AUX symbols are big-endian or little-endian; this
     43      1.1  christos    info comes from the file header record (fh-fBigendian).  */
     44      1.1  christos 
     45  1.1.1.2  christos void
     46  1.1.1.2  christos _bfd_ecoff_swap_tir_in (int bigend, const struct tir_ext *ext_copy,
     47      1.1  christos 			TIR *intern)
     48      1.1  christos {
     49      1.1  christos   struct tir_ext ext[1];
     50      1.1  christos 
     51      1.1  christos   *ext = *ext_copy;		/* Make it reasonable to do in-place.  */
     52      1.1  christos 
     53  1.1.1.2  christos   /* now the fun stuff...  */
     54  1.1.1.2  christos   if (bigend)
     55  1.1.1.6  christos     {
     56  1.1.1.6  christos       intern->fBitfield	  = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
     57  1.1.1.6  christos       intern->continued	  = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
     58  1.1.1.6  christos       intern->bt	  = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
     59  1.1.1.6  christos 			  >>		       TIR_BITS1_BT_SH_BIG;
     60  1.1.1.2  christos       intern->tq4	  = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
     61  1.1.1.6  christos 			  >>		      TIR_BITS_TQ4_SH_BIG;
     62  1.1.1.2  christos       intern->tq5	  = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
     63  1.1.1.6  christos 			  >>		      TIR_BITS_TQ5_SH_BIG;
     64  1.1.1.2  christos       intern->tq0	  = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
     65  1.1.1.6  christos 			  >>		      TIR_BITS_TQ0_SH_BIG;
     66  1.1.1.2  christos       intern->tq1	  = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
     67  1.1.1.6  christos 			  >>		      TIR_BITS_TQ1_SH_BIG;
     68  1.1.1.2  christos       intern->tq2	  = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
     69  1.1.1.6  christos 			  >>		      TIR_BITS_TQ2_SH_BIG;
     70  1.1.1.2  christos       intern->tq3	  = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
     71  1.1.1.2  christos 			  >>		      TIR_BITS_TQ3_SH_BIG;
     72  1.1.1.2  christos     }
     73  1.1.1.2  christos   else
     74  1.1.1.6  christos     {
     75  1.1.1.6  christos       intern->fBitfield	  = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
     76  1.1.1.6  christos       intern->continued	  = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
     77  1.1.1.2  christos       intern->bt	  = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
     78  1.1.1.6  christos 			  >>		    TIR_BITS1_BT_SH_LITTLE;
     79  1.1.1.2  christos       intern->tq4	  = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
     80  1.1.1.6  christos 			  >>		    TIR_BITS_TQ4_SH_LITTLE;
     81  1.1.1.2  christos       intern->tq5	  = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
     82  1.1.1.6  christos 			  >>		    TIR_BITS_TQ5_SH_LITTLE;
     83  1.1.1.2  christos       intern->tq0	  = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
     84  1.1.1.6  christos 			  >>		    TIR_BITS_TQ0_SH_LITTLE;
     85  1.1.1.2  christos       intern->tq1	  = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
     86  1.1.1.6  christos 			  >>		    TIR_BITS_TQ1_SH_LITTLE;
     87  1.1.1.2  christos       intern->tq2	  = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
     88  1.1.1.6  christos 			  >>		    TIR_BITS_TQ2_SH_LITTLE;
     89  1.1.1.2  christos       intern->tq3	  = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
     90  1.1.1.2  christos 			  >>		    TIR_BITS_TQ3_SH_LITTLE;
     91      1.1  christos     }
     92      1.1  christos 
     93      1.1  christos #ifdef TEST
     94      1.1  christos   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
     95      1.1  christos     abort ();
     96      1.1  christos #endif
     97      1.1  christos }
     98      1.1  christos 
     99      1.1  christos /* Swap out a type information record.
    100      1.1  christos    BIGEND says whether AUX symbols are big-endian or little-endian; this
    101      1.1  christos    info comes from the file header record (fh-fBigendian).  */
    102      1.1  christos 
    103  1.1.1.2  christos void
    104  1.1.1.2  christos _bfd_ecoff_swap_tir_out (int bigend,
    105  1.1.1.2  christos 			 const TIR *intern_copy,
    106      1.1  christos 			 struct tir_ext *ext)
    107      1.1  christos {
    108      1.1  christos   TIR intern[1];
    109      1.1  christos 
    110      1.1  christos   *intern = *intern_copy;	/* Make it reasonable to do in-place.  */
    111      1.1  christos 
    112  1.1.1.2  christos   /* now the fun stuff...  */
    113  1.1.1.2  christos   if (bigend)
    114  1.1.1.2  christos     {
    115      1.1  christos       ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
    116      1.1  christos 		       | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
    117      1.1  christos 		       | ((intern->bt << TIR_BITS1_BT_SH_BIG)
    118  1.1.1.2  christos 			  & TIR_BITS1_BT_BIG));
    119      1.1  christos       ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
    120      1.1  christos 		       & TIR_BITS_TQ4_BIG)
    121      1.1  christos 		      | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
    122  1.1.1.2  christos 			 & TIR_BITS_TQ5_BIG));
    123      1.1  christos       ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
    124      1.1  christos 		       & TIR_BITS_TQ0_BIG)
    125      1.1  christos 		      | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
    126  1.1.1.2  christos 			 & TIR_BITS_TQ1_BIG));
    127      1.1  christos       ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
    128      1.1  christos 		       & TIR_BITS_TQ2_BIG)
    129      1.1  christos 		      | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
    130  1.1.1.2  christos 			 & TIR_BITS_TQ3_BIG));
    131  1.1.1.2  christos     }
    132  1.1.1.2  christos   else
    133  1.1.1.2  christos     {
    134      1.1  christos       ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
    135      1.1  christos 		       | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
    136      1.1  christos 		       | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
    137  1.1.1.2  christos 			  & TIR_BITS1_BT_LITTLE));
    138      1.1  christos       ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
    139      1.1  christos 		       & TIR_BITS_TQ4_LITTLE)
    140      1.1  christos 		      | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
    141  1.1.1.2  christos 			 & TIR_BITS_TQ5_LITTLE));
    142      1.1  christos       ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
    143      1.1  christos 		       & TIR_BITS_TQ0_LITTLE)
    144      1.1  christos 		      | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
    145  1.1.1.2  christos 			 & TIR_BITS_TQ1_LITTLE));
    146      1.1  christos       ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
    147      1.1  christos 		       & TIR_BITS_TQ2_LITTLE)
    148      1.1  christos 		      | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
    149  1.1.1.2  christos 			 & TIR_BITS_TQ3_LITTLE));
    150      1.1  christos     }
    151      1.1  christos 
    152      1.1  christos #ifdef TEST
    153      1.1  christos   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
    154      1.1  christos     abort ();
    155      1.1  christos #endif
    156      1.1  christos }
    157      1.1  christos 
    158      1.1  christos /* Swap in a relative symbol record.  BIGEND says whether it is in
    159      1.1  christos    big-endian or little-endian format.*/
    160      1.1  christos 
    161  1.1.1.2  christos void
    162  1.1.1.2  christos _bfd_ecoff_swap_rndx_in (int bigend,
    163  1.1.1.2  christos 			 const struct rndx_ext *ext_copy,
    164      1.1  christos 			 RNDXR *intern)
    165      1.1  christos {
    166      1.1  christos   struct rndx_ext ext[1];
    167      1.1  christos 
    168      1.1  christos   *ext = *ext_copy;		/* Make it reasonable to do in-place.  */
    169      1.1  christos 
    170  1.1.1.2  christos   /* now the fun stuff...  */
    171  1.1.1.2  christos   if (bigend)
    172  1.1.1.2  christos     {
    173      1.1  christos       intern->rfd   = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
    174  1.1.1.6  christos 		  | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
    175  1.1.1.2  christos 				    >> RNDX_BITS1_RFD_SH_BIG);
    176  1.1.1.6  christos       intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
    177      1.1  christos 				    << RNDX_BITS1_INDEX_SH_LEFT_BIG)
    178      1.1  christos 		  | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
    179  1.1.1.2  christos 		  | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
    180  1.1.1.2  christos     }
    181  1.1.1.2  christos   else
    182  1.1.1.2  christos     {
    183      1.1  christos       intern->rfd   = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
    184  1.1.1.6  christos 		  | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
    185  1.1.1.2  christos 				    << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
    186  1.1.1.6  christos       intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
    187      1.1  christos 				    >> RNDX_BITS1_INDEX_SH_LITTLE)
    188      1.1  christos 		  | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
    189      1.1  christos 		  | ((unsigned int) ext->r_bits[3]
    190  1.1.1.2  christos 		     << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
    191      1.1  christos     }
    192      1.1  christos 
    193      1.1  christos #ifdef TEST
    194      1.1  christos   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
    195      1.1  christos     abort ();
    196      1.1  christos #endif
    197      1.1  christos }
    198      1.1  christos 
    199      1.1  christos /* Swap out a relative symbol record.  BIGEND says whether it is in
    200      1.1  christos    big-endian or little-endian format.*/
    201      1.1  christos 
    202  1.1.1.2  christos void
    203  1.1.1.2  christos _bfd_ecoff_swap_rndx_out (int bigend,
    204  1.1.1.2  christos 			  const RNDXR *intern_copy,
    205      1.1  christos 			  struct rndx_ext *ext)
    206      1.1  christos {
    207      1.1  christos   RNDXR intern[1];
    208      1.1  christos 
    209      1.1  christos   *intern = *intern_copy;	/* Make it reasonable to do in-place.  */
    210      1.1  christos 
    211  1.1.1.2  christos   /* now the fun stuff...  */
    212  1.1.1.2  christos   if (bigend)
    213  1.1.1.2  christos     {
    214  1.1.1.2  christos       ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
    215      1.1  christos       ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
    216      1.1  christos 		       & RNDX_BITS1_RFD_BIG)
    217      1.1  christos 		      | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
    218  1.1.1.2  christos 			 & RNDX_BITS1_INDEX_BIG));
    219  1.1.1.2  christos       ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
    220  1.1.1.2  christos       ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
    221  1.1.1.2  christos     }
    222  1.1.1.2  christos   else
    223  1.1.1.2  christos     {
    224  1.1.1.2  christos       ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
    225      1.1  christos       ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
    226      1.1  christos 		       & RNDX_BITS1_RFD_LITTLE)
    227      1.1  christos 		      | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
    228  1.1.1.2  christos 			 & RNDX_BITS1_INDEX_LITTLE));
    229  1.1.1.2  christos       ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
    230  1.1.1.2  christos       ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
    231      1.1  christos     }
    232      1.1  christos 
    233      1.1  christos #ifdef TEST
    234      1.1  christos   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
    235      1.1  christos     abort ();
    236      1.1  christos #endif
    237      1.1  christos }
    238      1.1  christos 
    239      1.1  christos /* The minimum amount of data to allocate.  */
    241      1.1  christos #define ALLOC_SIZE (4064)
    242      1.1  christos 
    243      1.1  christos /* Add bytes to a buffer.  Return success.  */
    244  1.1.1.2  christos 
    245      1.1  christos static bfd_boolean
    246      1.1  christos ecoff_add_bytes (char **buf, char **bufend, size_t need)
    247      1.1  christos {
    248      1.1  christos   size_t have;
    249      1.1  christos   size_t want;
    250      1.1  christos   char *newbuf;
    251      1.1  christos 
    252      1.1  christos   have = *bufend - *buf;
    253      1.1  christos   if (have > need)
    254      1.1  christos     want = ALLOC_SIZE;
    255      1.1  christos   else
    256      1.1  christos     {
    257      1.1  christos       want = need - have;
    258      1.1  christos       if (want < ALLOC_SIZE)
    259      1.1  christos 	want = ALLOC_SIZE;
    260      1.1  christos     }
    261      1.1  christos   newbuf = (char *) bfd_realloc (*buf, (bfd_size_type) have + want);
    262      1.1  christos   if (newbuf == NULL)
    263      1.1  christos     return FALSE;
    264      1.1  christos   *buf = newbuf;
    265      1.1  christos   *bufend = *buf + have + want;
    266      1.1  christos   return TRUE;
    267      1.1  christos }
    268      1.1  christos 
    269      1.1  christos /* We keep a hash table which maps strings to numbers.  We use it to
    270      1.1  christos    map FDR names to indices in the output file, and to map local
    271      1.1  christos    strings when combining stabs debugging information.  */
    272      1.1  christos 
    273      1.1  christos struct string_hash_entry
    274      1.1  christos {
    275      1.1  christos   struct bfd_hash_entry root;
    276      1.1  christos   /* FDR index or string table offset.  */
    277      1.1  christos   long val;
    278      1.1  christos   /* Next entry in string table.  */
    279      1.1  christos   struct string_hash_entry *next;
    280      1.1  christos };
    281      1.1  christos 
    282      1.1  christos struct string_hash_table
    283      1.1  christos {
    284      1.1  christos   struct bfd_hash_table table;
    285      1.1  christos };
    286      1.1  christos 
    287      1.1  christos /* Routine to create an entry in a string hash table.  */
    288  1.1.1.2  christos 
    289  1.1.1.2  christos static struct bfd_hash_entry *
    290  1.1.1.2  christos string_hash_newfunc (struct bfd_hash_entry *entry,
    291      1.1  christos 		     struct bfd_hash_table *table,
    292      1.1  christos 		     const char *string)
    293      1.1  christos {
    294      1.1  christos   struct string_hash_entry *ret = (struct string_hash_entry *) entry;
    295      1.1  christos 
    296      1.1  christos   /* Allocate the structure if it has not already been allocated by a
    297      1.1  christos      subclass.  */
    298      1.1  christos   if (ret == (struct string_hash_entry *) NULL)
    299      1.1  christos     ret = ((struct string_hash_entry *)
    300      1.1  christos 	   bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
    301      1.1  christos   if (ret == (struct string_hash_entry *) NULL)
    302      1.1  christos     return NULL;
    303      1.1  christos 
    304      1.1  christos   /* Call the allocation method of the superclass.  */
    305      1.1  christos   ret = ((struct string_hash_entry *)
    306      1.1  christos 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
    307      1.1  christos 
    308      1.1  christos   if (ret)
    309      1.1  christos     {
    310      1.1  christos       /* Initialize the local fields.  */
    311      1.1  christos       ret->val = -1;
    312      1.1  christos       ret->next = NULL;
    313      1.1  christos     }
    314      1.1  christos 
    315      1.1  christos   return (struct bfd_hash_entry *) ret;
    316      1.1  christos }
    317      1.1  christos 
    318      1.1  christos /* Look up an entry in an string hash table.  */
    319      1.1  christos 
    320      1.1  christos #define string_hash_lookup(t, string, create, copy) \
    321      1.1  christos   ((struct string_hash_entry *) \
    322      1.1  christos    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
    323      1.1  christos 
    324      1.1  christos /* We can't afford to read in all the debugging information when we do
    325      1.1  christos    a link.  Instead, we build a list of these structures to show how
    326      1.1  christos    different parts of the input file map to the output file.  */
    327      1.1  christos 
    328      1.1  christos struct shuffle
    329      1.1  christos {
    330      1.1  christos   /* The next entry in this linked list.  */
    331      1.1  christos   struct shuffle *next;
    332      1.1  christos   /* The length of the information.  */
    333      1.1  christos   unsigned long size;
    334      1.1  christos   /* Whether this information comes from a file or not.  */
    335      1.1  christos   bfd_boolean filep;
    336      1.1  christos   union
    337      1.1  christos     {
    338      1.1  christos       struct
    339      1.1  christos 	{
    340      1.1  christos 	  /* The BFD the data comes from.  */
    341      1.1  christos 	  bfd *input_bfd;
    342      1.1  christos 	  /* The offset within input_bfd.  */
    343      1.1  christos 	  file_ptr offset;
    344  1.1.1.2  christos 	} file;
    345      1.1  christos       /* The data to be written out.  */
    346      1.1  christos       void * memory;
    347      1.1  christos     } u;
    348      1.1  christos };
    349      1.1  christos 
    350      1.1  christos /* This structure holds information across calls to
    351      1.1  christos    bfd_ecoff_debug_accumulate.  */
    352      1.1  christos 
    353      1.1  christos struct accumulate
    354      1.1  christos {
    355      1.1  christos   /* The FDR hash table.  */
    356      1.1  christos   struct string_hash_table fdr_hash;
    357      1.1  christos   /* The strings hash table.  */
    358      1.1  christos   struct string_hash_table str_hash;
    359      1.1  christos   /* Linked lists describing how to shuffle the input debug
    360      1.1  christos      information into the output file.  We keep a pointer to both the
    361      1.1  christos      head and the tail.  */
    362      1.1  christos   struct shuffle *line;
    363      1.1  christos   struct shuffle *line_end;
    364      1.1  christos   struct shuffle *pdr;
    365      1.1  christos   struct shuffle *pdr_end;
    366      1.1  christos   struct shuffle *sym;
    367      1.1  christos   struct shuffle *sym_end;
    368      1.1  christos   struct shuffle *opt;
    369      1.1  christos   struct shuffle *opt_end;
    370      1.1  christos   struct shuffle *aux;
    371      1.1  christos   struct shuffle *aux_end;
    372      1.1  christos   struct shuffle *ss;
    373      1.1  christos   struct shuffle *ss_end;
    374      1.1  christos   struct string_hash_entry *ss_hash;
    375      1.1  christos   struct string_hash_entry *ss_hash_end;
    376      1.1  christos   struct shuffle *fdr;
    377      1.1  christos   struct shuffle *fdr_end;
    378      1.1  christos   struct shuffle *rfd;
    379      1.1  christos   struct shuffle *rfd_end;
    380      1.1  christos   /* The size of the largest file shuffle.  */
    381      1.1  christos   unsigned long largest_file_shuffle;
    382      1.1  christos   /* An objalloc for debugging information.  */
    383      1.1  christos   struct objalloc *memory;
    384      1.1  christos };
    385      1.1  christos 
    386      1.1  christos /* Add a file entry to a shuffle list.  */
    387  1.1.1.2  christos 
    388  1.1.1.2  christos static bfd_boolean
    389  1.1.1.2  christos add_file_shuffle (struct accumulate *ainfo,
    390  1.1.1.2  christos 		  struct shuffle **head,
    391  1.1.1.2  christos 		  struct shuffle **tail,
    392  1.1.1.2  christos 		  bfd *input_bfd,
    393      1.1  christos 		  file_ptr offset,
    394      1.1  christos 		  unsigned long size)
    395      1.1  christos {
    396      1.1  christos   struct shuffle *n;
    397      1.1  christos 
    398      1.1  christos   if (*tail != (struct shuffle *) NULL
    399      1.1  christos       && (*tail)->filep
    400      1.1  christos       && (*tail)->u.file.input_bfd == input_bfd
    401      1.1  christos       && (*tail)->u.file.offset + (*tail)->size == (unsigned long) offset)
    402      1.1  christos     {
    403      1.1  christos       /* Just merge this entry onto the existing one.  */
    404      1.1  christos       (*tail)->size += size;
    405      1.1  christos       if ((*tail)->size > ainfo->largest_file_shuffle)
    406      1.1  christos 	ainfo->largest_file_shuffle = (*tail)->size;
    407      1.1  christos       return TRUE;
    408      1.1  christos     }
    409      1.1  christos 
    410      1.1  christos   n = (struct shuffle *) objalloc_alloc (ainfo->memory,
    411      1.1  christos 					 sizeof (struct shuffle));
    412      1.1  christos   if (!n)
    413      1.1  christos     {
    414      1.1  christos       bfd_set_error (bfd_error_no_memory);
    415      1.1  christos       return FALSE;
    416      1.1  christos     }
    417      1.1  christos   n->next = NULL;
    418      1.1  christos   n->size = size;
    419      1.1  christos   n->filep = TRUE;
    420      1.1  christos   n->u.file.input_bfd = input_bfd;
    421      1.1  christos   n->u.file.offset = offset;
    422      1.1  christos   if (*head == (struct shuffle *) NULL)
    423      1.1  christos     *head = n;
    424      1.1  christos   if (*tail != (struct shuffle *) NULL)
    425      1.1  christos     (*tail)->next = n;
    426      1.1  christos   *tail = n;
    427      1.1  christos   if (size > ainfo->largest_file_shuffle)
    428      1.1  christos     ainfo->largest_file_shuffle = size;
    429      1.1  christos   return TRUE;
    430      1.1  christos }
    431      1.1  christos 
    432      1.1  christos /* Add a memory entry to a shuffle list.  */
    433  1.1.1.2  christos 
    434  1.1.1.2  christos static bfd_boolean
    435  1.1.1.2  christos add_memory_shuffle (struct accumulate *ainfo,
    436  1.1.1.2  christos 		    struct shuffle **head,
    437  1.1.1.2  christos 		    struct shuffle **tail,
    438      1.1  christos 		    bfd_byte *data,
    439      1.1  christos 		    unsigned long size)
    440      1.1  christos {
    441      1.1  christos   struct shuffle *n;
    442      1.1  christos 
    443      1.1  christos   n = (struct shuffle *) objalloc_alloc (ainfo->memory,
    444      1.1  christos 					 sizeof (struct shuffle));
    445      1.1  christos   if (!n)
    446      1.1  christos     {
    447      1.1  christos       bfd_set_error (bfd_error_no_memory);
    448      1.1  christos       return FALSE;
    449      1.1  christos     }
    450      1.1  christos   n->next = NULL;
    451  1.1.1.2  christos   n->size = size;
    452      1.1  christos   n->filep = FALSE;
    453      1.1  christos   n->u.memory = data;
    454      1.1  christos   if (*head == (struct shuffle *) NULL)
    455      1.1  christos     *head = n;
    456      1.1  christos   if (*tail != (struct shuffle *) NULL)
    457      1.1  christos     (*tail)->next = n;
    458      1.1  christos   *tail = n;
    459      1.1  christos   return TRUE;
    460      1.1  christos }
    461      1.1  christos 
    462      1.1  christos /* Initialize the FDR hash table.  This returns a handle which is then
    463  1.1.1.2  christos    passed in to bfd_ecoff_debug_accumulate, et. al.  */
    464  1.1.1.2  christos 
    465  1.1.1.2  christos void *
    466  1.1.1.2  christos bfd_ecoff_debug_init (bfd *output_bfd ATTRIBUTE_UNUSED,
    467  1.1.1.2  christos 		      struct ecoff_debug_info *output_debug,
    468      1.1  christos 		      const struct ecoff_debug_swap *output_swap ATTRIBUTE_UNUSED,
    469      1.1  christos 		      struct bfd_link_info *info)
    470  1.1.1.7  christos {
    471      1.1  christos   struct accumulate *ainfo;
    472      1.1  christos   size_t amt = sizeof (struct accumulate);
    473      1.1  christos 
    474      1.1  christos   ainfo = (struct accumulate *) bfd_malloc (amt);
    475      1.1  christos   if (!ainfo)
    476      1.1  christos     return NULL;
    477      1.1  christos   if (!bfd_hash_table_init_n (&ainfo->fdr_hash.table, string_hash_newfunc,
    478      1.1  christos 			      sizeof (struct string_hash_entry), 1021))
    479      1.1  christos     return NULL;
    480      1.1  christos 
    481      1.1  christos   ainfo->line = NULL;
    482      1.1  christos   ainfo->line_end = NULL;
    483      1.1  christos   ainfo->pdr = NULL;
    484      1.1  christos   ainfo->pdr_end = NULL;
    485      1.1  christos   ainfo->sym = NULL;
    486      1.1  christos   ainfo->sym_end = NULL;
    487      1.1  christos   ainfo->opt = NULL;
    488      1.1  christos   ainfo->opt_end = NULL;
    489      1.1  christos   ainfo->aux = NULL;
    490      1.1  christos   ainfo->aux_end = NULL;
    491      1.1  christos   ainfo->ss = NULL;
    492      1.1  christos   ainfo->ss_end = NULL;
    493      1.1  christos   ainfo->ss_hash = NULL;
    494      1.1  christos   ainfo->ss_hash_end = NULL;
    495      1.1  christos   ainfo->fdr = NULL;
    496      1.1  christos   ainfo->fdr_end = NULL;
    497      1.1  christos   ainfo->rfd = NULL;
    498      1.1  christos   ainfo->rfd_end = NULL;
    499      1.1  christos 
    500  1.1.1.4  christos   ainfo->largest_file_shuffle = 0;
    501      1.1  christos 
    502      1.1  christos   if (! bfd_link_relocatable (info))
    503      1.1  christos     {
    504      1.1  christos       if (!bfd_hash_table_init (&ainfo->str_hash.table, string_hash_newfunc,
    505      1.1  christos 				sizeof (struct string_hash_entry)))
    506      1.1  christos 	return NULL;
    507      1.1  christos 
    508      1.1  christos       /* The first entry in the string table is the empty string.  */
    509      1.1  christos       output_debug->symbolic_header.issMax = 1;
    510      1.1  christos     }
    511      1.1  christos 
    512      1.1  christos   ainfo->memory = objalloc_create ();
    513      1.1  christos   if (ainfo->memory == NULL)
    514      1.1  christos     {
    515      1.1  christos       bfd_set_error (bfd_error_no_memory);
    516      1.1  christos       return NULL;
    517  1.1.1.2  christos     }
    518      1.1  christos 
    519      1.1  christos   return ainfo;
    520      1.1  christos }
    521      1.1  christos 
    522      1.1  christos /* Free the accumulated debugging information.  */
    523  1.1.1.2  christos 
    524  1.1.1.2  christos void
    525  1.1.1.2  christos bfd_ecoff_debug_free (void * handle,
    526  1.1.1.2  christos 		      bfd *output_bfd ATTRIBUTE_UNUSED,
    527  1.1.1.2  christos 		      struct ecoff_debug_info *output_debug ATTRIBUTE_UNUSED,
    528      1.1  christos 		      const struct ecoff_debug_swap *output_swap ATTRIBUTE_UNUSED,
    529      1.1  christos 		      struct bfd_link_info *info)
    530      1.1  christos {
    531      1.1  christos   struct accumulate *ainfo = (struct accumulate *) handle;
    532      1.1  christos 
    533  1.1.1.4  christos   bfd_hash_table_free (&ainfo->fdr_hash.table);
    534      1.1  christos 
    535      1.1  christos   if (! bfd_link_relocatable (info))
    536      1.1  christos     bfd_hash_table_free (&ainfo->str_hash.table);
    537      1.1  christos 
    538      1.1  christos   objalloc_free (ainfo->memory);
    539      1.1  christos 
    540      1.1  christos   free (ainfo);
    541      1.1  christos }
    542      1.1  christos 
    543      1.1  christos /* Accumulate the debugging information from INPUT_BFD into
    544      1.1  christos    OUTPUT_BFD.  The INPUT_DEBUG argument points to some ECOFF
    545      1.1  christos    debugging information which we want to link into the information
    546      1.1  christos    pointed to by the OUTPUT_DEBUG argument.  OUTPUT_SWAP and
    547      1.1  christos    INPUT_SWAP point to the swapping information needed.  INFO is the
    548      1.1  christos    linker information structure.  HANDLE is returned by
    549      1.1  christos    bfd_ecoff_debug_init.  */
    550  1.1.1.2  christos 
    551  1.1.1.2  christos bfd_boolean
    552  1.1.1.2  christos bfd_ecoff_debug_accumulate (void * handle,
    553  1.1.1.2  christos 			    bfd *output_bfd,
    554  1.1.1.2  christos 			    struct ecoff_debug_info *output_debug,
    555  1.1.1.2  christos 			    const struct ecoff_debug_swap *output_swap,
    556  1.1.1.2  christos 			    bfd *input_bfd,
    557  1.1.1.2  christos 			    struct ecoff_debug_info *input_debug,
    558      1.1  christos 			    const struct ecoff_debug_swap *input_swap,
    559      1.1  christos 			    struct bfd_link_info *info)
    560  1.1.1.2  christos {
    561      1.1  christos   struct accumulate *ainfo = (struct accumulate *) handle;
    562  1.1.1.2  christos   void (* const swap_sym_in) (bfd *, void *, SYMR *)
    563      1.1  christos     = input_swap->swap_sym_in;
    564  1.1.1.2  christos   void (* const swap_rfd_in) (bfd *, void *, RFDT *)
    565      1.1  christos     = input_swap->swap_rfd_in;
    566  1.1.1.2  christos   void (* const swap_sym_out) (bfd *, const SYMR *, void *)
    567      1.1  christos     = output_swap->swap_sym_out;
    568  1.1.1.2  christos   void (* const swap_fdr_out) (bfd *, const FDR *, void *)
    569      1.1  christos     = output_swap->swap_fdr_out;
    570      1.1  christos   void (* const swap_rfd_out) (bfd *, const RFDT *, void *)
    571      1.1  christos     = output_swap->swap_rfd_out;
    572      1.1  christos   bfd_size_type external_pdr_size = output_swap->external_pdr_size;
    573      1.1  christos   bfd_size_type external_sym_size = output_swap->external_sym_size;
    574      1.1  christos   bfd_size_type external_opt_size = output_swap->external_opt_size;
    575      1.1  christos   bfd_size_type external_fdr_size = output_swap->external_fdr_size;
    576      1.1  christos   bfd_size_type external_rfd_size = output_swap->external_rfd_size;
    577      1.1  christos   HDRR * const output_symhdr = &output_debug->symbolic_header;
    578      1.1  christos   HDRR * const input_symhdr = &input_debug->symbolic_header;
    579      1.1  christos   bfd_vma section_adjust[scMax];
    580      1.1  christos   asection *sec;
    581      1.1  christos   bfd_byte *fdr_start;
    582      1.1  christos   bfd_byte *fdr_ptr;
    583      1.1  christos   bfd_byte *fdr_end;
    584      1.1  christos   bfd_size_type fdr_add;
    585      1.1  christos   unsigned int copied;
    586      1.1  christos   RFDT i;
    587      1.1  christos   unsigned long sz;
    588      1.1  christos   bfd_byte *rfd_out;
    589      1.1  christos   bfd_byte *rfd_in;
    590      1.1  christos   bfd_byte *rfd_end;
    591      1.1  christos   long newrfdbase = 0;
    592      1.1  christos   long oldrfdbase = 0;
    593      1.1  christos   bfd_byte *fdr_out;
    594      1.1  christos   bfd_size_type amt;
    595      1.1  christos 
    596  1.1.1.2  christos   /* Use section_adjust to hold the value to add to a symbol in a
    597      1.1  christos      particular section.  */
    598      1.1  christos   memset (section_adjust, 0, sizeof section_adjust);
    599      1.1  christos 
    600      1.1  christos #define SET(name, indx) \
    601      1.1  christos   sec = bfd_get_section_by_name (input_bfd, name); \
    602      1.1  christos   if (sec != NULL) \
    603      1.1  christos     section_adjust[indx] = (sec->output_section->vma \
    604      1.1  christos 			    + sec->output_offset \
    605      1.1  christos 			    - sec->vma);
    606      1.1  christos 
    607      1.1  christos   SET (".text", scText);
    608      1.1  christos   SET (".data", scData);
    609      1.1  christos   SET (".bss", scBss);
    610      1.1  christos   SET (".sdata", scSData);
    611      1.1  christos   SET (".sbss", scSBss);
    612      1.1  christos   /* scRdata section may be either .rdata or .rodata.  */
    613      1.1  christos   SET (".rdata", scRData);
    614      1.1  christos   SET (".rodata", scRData);
    615      1.1  christos   SET (".init", scInit);
    616      1.1  christos   SET (".fini", scFini);
    617      1.1  christos   SET (".rconst", scRConst);
    618      1.1  christos 
    619      1.1  christos #undef SET
    620      1.1  christos 
    621      1.1  christos   /* Find all the debugging information based on the FDR's.  We need
    622      1.1  christos      to handle them whether they are swapped or not.  */
    623      1.1  christos   if (input_debug->fdr != (FDR *) NULL)
    624      1.1  christos     {
    625      1.1  christos       fdr_start = (bfd_byte *) input_debug->fdr;
    626      1.1  christos       fdr_add = sizeof (FDR);
    627      1.1  christos     }
    628      1.1  christos   else
    629      1.1  christos     {
    630      1.1  christos       fdr_start = (bfd_byte *) input_debug->external_fdr;
    631      1.1  christos       fdr_add = input_swap->external_fdr_size;
    632      1.1  christos     }
    633      1.1  christos   fdr_end = fdr_start + input_symhdr->ifdMax * fdr_add;
    634      1.1  christos 
    635      1.1  christos   amt = input_symhdr->ifdMax;
    636      1.1  christos   amt *= sizeof (RFDT);
    637      1.1  christos   input_debug->ifdmap = (RFDT *) bfd_alloc (input_bfd, amt);
    638      1.1  christos 
    639      1.1  christos   sz = (input_symhdr->crfd + input_symhdr->ifdMax) * external_rfd_size;
    640      1.1  christos   rfd_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
    641      1.1  christos   if (!input_debug->ifdmap || !rfd_out)
    642      1.1  christos     {
    643      1.1  christos       bfd_set_error (bfd_error_no_memory);
    644      1.1  christos       return FALSE;
    645      1.1  christos     }
    646      1.1  christos   if (!add_memory_shuffle (ainfo, &ainfo->rfd, &ainfo->rfd_end, rfd_out, sz))
    647      1.1  christos     return FALSE;
    648      1.1  christos 
    649      1.1  christos   copied = 0;
    650      1.1  christos 
    651      1.1  christos   /* Look through the FDR's to see which ones we are going to include
    652      1.1  christos      in the final output.  We do not want duplicate FDR information
    653      1.1  christos      for header files, because ECOFF debugging is often very large.
    654      1.1  christos      When we find an FDR with no line information which can be merged,
    655      1.1  christos      we look it up in a hash table to ensure that we only include it
    656      1.1  christos      once.  We keep a table mapping FDR numbers to the final number
    657      1.1  christos      they get with the BFD, so that we can refer to it when we write
    658      1.1  christos      out the external symbols.  */
    659      1.1  christos   for (fdr_ptr = fdr_start, i = 0;
    660      1.1  christos        fdr_ptr < fdr_end;
    661      1.1  christos        fdr_ptr += fdr_add, i++, rfd_out += external_rfd_size)
    662      1.1  christos     {
    663      1.1  christos       FDR fdr;
    664      1.1  christos 
    665      1.1  christos       if (input_debug->fdr != (FDR *) NULL)
    666  1.1.1.2  christos 	fdr = *(FDR *) fdr_ptr;
    667      1.1  christos       else
    668      1.1  christos 	(*input_swap->swap_fdr_in) (input_bfd, fdr_ptr, &fdr);
    669      1.1  christos 
    670      1.1  christos       /* See if this FDR can be merged with an existing one.  */
    671      1.1  christos       if (fdr.cbLine == 0 && fdr.rss != -1 && fdr.fMerge)
    672      1.1  christos 	{
    673      1.1  christos 	  const char *name;
    674      1.1  christos 	  char *lookup;
    675      1.1  christos 	  struct string_hash_entry *fh;
    676      1.1  christos 
    677      1.1  christos 	  /* We look up a string formed from the file name and the
    678      1.1  christos 	     number of symbols and aux entries.  Sometimes an include
    679      1.1  christos 	     file will conditionally define a typedef or something
    680      1.1  christos 	     based on the order of include files.  Using the number of
    681      1.1  christos 	     symbols and aux entries as a hash reduces the chance that
    682      1.1  christos 	     we will merge symbol information that should not be
    683      1.1  christos 	     merged.  */
    684      1.1  christos 	  name = input_debug->ss + fdr.issBase + fdr.rss;
    685      1.1  christos 
    686      1.1  christos 	  lookup = (char *) bfd_malloc ((bfd_size_type) strlen (name) + 20);
    687      1.1  christos 	  if (lookup == NULL)
    688      1.1  christos 	    return FALSE;
    689      1.1  christos 	  sprintf (lookup, "%s %lx %lx", name, (unsigned long) fdr.csym,
    690      1.1  christos 		   (unsigned long) fdr.caux);
    691      1.1  christos 
    692      1.1  christos 	  fh = string_hash_lookup (&ainfo->fdr_hash, lookup, TRUE, TRUE);
    693      1.1  christos 	  free (lookup);
    694      1.1  christos 	  if (fh == (struct string_hash_entry *) NULL)
    695      1.1  christos 	    return FALSE;
    696      1.1  christos 
    697      1.1  christos 	  if (fh->val != -1)
    698  1.1.1.2  christos 	    {
    699      1.1  christos 	      input_debug->ifdmap[i] = fh->val;
    700      1.1  christos 	      (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i, rfd_out);
    701      1.1  christos 
    702      1.1  christos 	      /* Don't copy this FDR.  */
    703      1.1  christos 	      continue;
    704      1.1  christos 	    }
    705      1.1  christos 
    706      1.1  christos 	  fh->val = output_symhdr->ifdMax + copied;
    707      1.1  christos 	}
    708  1.1.1.2  christos 
    709      1.1  christos       input_debug->ifdmap[i] = output_symhdr->ifdMax + copied;
    710      1.1  christos       (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i, rfd_out);
    711      1.1  christos       ++copied;
    712      1.1  christos     }
    713      1.1  christos 
    714      1.1  christos   newrfdbase = output_symhdr->crfd;
    715      1.1  christos   output_symhdr->crfd += input_symhdr->ifdMax;
    716      1.1  christos 
    717      1.1  christos   /* Copy over any existing RFD's.  RFD's are only created by the
    718      1.1  christos      linker, so this will only happen for input files which are the
    719      1.1  christos      result of a partial link.  */
    720      1.1  christos   rfd_in = (bfd_byte *) input_debug->external_rfd;
    721      1.1  christos   rfd_end = rfd_in + input_symhdr->crfd * input_swap->external_rfd_size;
    722      1.1  christos   for (;
    723      1.1  christos        rfd_in < rfd_end;
    724      1.1  christos        rfd_in += input_swap->external_rfd_size)
    725      1.1  christos     {
    726  1.1.1.2  christos       RFDT rfd;
    727      1.1  christos 
    728      1.1  christos       (*swap_rfd_in) (input_bfd, rfd_in, &rfd);
    729  1.1.1.2  christos       BFD_ASSERT (rfd >= 0 && rfd < input_symhdr->ifdMax);
    730      1.1  christos       rfd = input_debug->ifdmap[rfd];
    731      1.1  christos       (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
    732      1.1  christos       rfd_out += external_rfd_size;
    733      1.1  christos     }
    734      1.1  christos 
    735      1.1  christos   oldrfdbase = output_symhdr->crfd;
    736      1.1  christos   output_symhdr->crfd += input_symhdr->crfd;
    737      1.1  christos 
    738      1.1  christos   /* Look through the FDR's and copy over all associated debugging
    739      1.1  christos      information.  */
    740      1.1  christos   sz = copied * external_fdr_size;
    741      1.1  christos   fdr_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
    742      1.1  christos   if (!fdr_out)
    743      1.1  christos     {
    744      1.1  christos       bfd_set_error (bfd_error_no_memory);
    745      1.1  christos       return FALSE;
    746      1.1  christos     }
    747      1.1  christos   if (!add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end, fdr_out, sz))
    748      1.1  christos     return FALSE;
    749      1.1  christos   for (fdr_ptr = fdr_start, i = 0;
    750      1.1  christos        fdr_ptr < fdr_end;
    751      1.1  christos        fdr_ptr += fdr_add, i++)
    752      1.1  christos     {
    753      1.1  christos       FDR fdr;
    754      1.1  christos       bfd_byte *sym_out;
    755      1.1  christos       bfd_byte *lraw_src;
    756      1.1  christos       bfd_byte *lraw_end;
    757      1.1  christos       bfd_boolean fgotfilename;
    758      1.1  christos 
    759      1.1  christos       if (input_debug->ifdmap[i] < output_symhdr->ifdMax)
    760      1.1  christos 	{
    761      1.1  christos 	  /* We are not copying this FDR.  */
    762      1.1  christos 	  continue;
    763      1.1  christos 	}
    764      1.1  christos 
    765      1.1  christos       if (input_debug->fdr != (FDR *) NULL)
    766  1.1.1.2  christos 	fdr = *(FDR *) fdr_ptr;
    767      1.1  christos       else
    768      1.1  christos 	(*input_swap->swap_fdr_in) (input_bfd, fdr_ptr, &fdr);
    769      1.1  christos 
    770      1.1  christos       /* FIXME: It is conceivable that this FDR points to the .init or
    771      1.1  christos 	 .fini section, in which case this will not do the right
    772      1.1  christos 	 thing.  */
    773      1.1  christos       fdr.adr += section_adjust[scText];
    774      1.1  christos 
    775      1.1  christos       /* Swap in the local symbols, adjust their values, and swap them
    776      1.1  christos 	 out again.  */
    777      1.1  christos       fgotfilename = FALSE;
    778      1.1  christos       sz = fdr.csym * external_sym_size;
    779      1.1  christos       sym_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
    780      1.1  christos       if (!sym_out)
    781      1.1  christos 	{
    782      1.1  christos 	  bfd_set_error (bfd_error_no_memory);
    783      1.1  christos 	  return FALSE;
    784      1.1  christos 	}
    785      1.1  christos       if (!add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end, sym_out,
    786      1.1  christos 			       sz))
    787      1.1  christos 	return FALSE;
    788      1.1  christos       lraw_src = ((bfd_byte *) input_debug->external_sym
    789      1.1  christos 		  + fdr.isymBase * input_swap->external_sym_size);
    790      1.1  christos       lraw_end = lraw_src + fdr.csym * input_swap->external_sym_size;
    791      1.1  christos       for (;  lraw_src < lraw_end;  lraw_src += input_swap->external_sym_size)
    792      1.1  christos 	{
    793  1.1.1.2  christos 	  SYMR internal_sym;
    794      1.1  christos 
    795      1.1  christos 	  (*swap_sym_in) (input_bfd, lraw_src, &internal_sym);
    796      1.1  christos 
    797      1.1  christos 	  BFD_ASSERT (internal_sym.sc != scCommon
    798      1.1  christos 		      && internal_sym.sc != scSCommon);
    799      1.1  christos 
    800      1.1  christos 	  /* Adjust the symbol value if appropriate.  */
    801      1.1  christos 	  switch (internal_sym.st)
    802      1.1  christos 	    {
    803      1.1  christos 	    case stNil:
    804      1.1  christos 	      if (ECOFF_IS_STAB (&internal_sym))
    805      1.1  christos 		break;
    806      1.1  christos 	      /* Fall through.  */
    807      1.1  christos 	    case stGlobal:
    808      1.1  christos 	    case stStatic:
    809      1.1  christos 	    case stLabel:
    810      1.1  christos 	    case stProc:
    811      1.1  christos 	    case stStaticProc:
    812      1.1  christos 	      internal_sym.value += section_adjust[internal_sym.sc];
    813      1.1  christos 	      break;
    814      1.1  christos 
    815      1.1  christos 	    default:
    816      1.1  christos 	      break;
    817      1.1  christos 	    }
    818      1.1  christos 
    819      1.1  christos 	  /* If we are doing a final link, we hash all the strings in
    820      1.1  christos 	     the local symbol table together.  This reduces the amount
    821      1.1  christos 	     of space required by debugging information.  We don't do
    822  1.1.1.4  christos 	     this when performing a relocatable link because it would
    823      1.1  christos 	     prevent us from easily merging different FDR's.  */
    824      1.1  christos 	  if (! bfd_link_relocatable (info))
    825      1.1  christos 	    {
    826      1.1  christos 	      bfd_boolean ffilename;
    827      1.1  christos 	      const char *name;
    828      1.1  christos 
    829      1.1  christos 	      if (! fgotfilename && internal_sym.iss == fdr.rss)
    830      1.1  christos 		ffilename = TRUE;
    831      1.1  christos 	      else
    832      1.1  christos 		ffilename = FALSE;
    833      1.1  christos 
    834      1.1  christos 	      /* Hash the name into the string table.  */
    835      1.1  christos 	      name = input_debug->ss + fdr.issBase + internal_sym.iss;
    836      1.1  christos 	      if (*name == '\0')
    837      1.1  christos 		internal_sym.iss = 0;
    838      1.1  christos 	      else
    839      1.1  christos 		{
    840      1.1  christos 		  struct string_hash_entry *sh;
    841      1.1  christos 
    842      1.1  christos 		  sh = string_hash_lookup (&ainfo->str_hash, name, TRUE, TRUE);
    843      1.1  christos 		  if (sh == (struct string_hash_entry *) NULL)
    844      1.1  christos 		    return FALSE;
    845      1.1  christos 		  if (sh->val == -1)
    846      1.1  christos 		    {
    847      1.1  christos 		      sh->val = output_symhdr->issMax;
    848      1.1  christos 		      output_symhdr->issMax += strlen (name) + 1;
    849      1.1  christos 		      if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
    850      1.1  christos 			ainfo->ss_hash = sh;
    851      1.1  christos 		      if (ainfo->ss_hash_end
    852      1.1  christos 			  != (struct string_hash_entry *) NULL)
    853      1.1  christos 			ainfo->ss_hash_end->next = sh;
    854      1.1  christos 		      ainfo->ss_hash_end = sh;
    855      1.1  christos 		    }
    856      1.1  christos 		  internal_sym.iss = sh->val;
    857      1.1  christos 		}
    858      1.1  christos 
    859      1.1  christos 	      if (ffilename)
    860      1.1  christos 		{
    861      1.1  christos 		  fdr.rss = internal_sym.iss;
    862      1.1  christos 		  fgotfilename = TRUE;
    863      1.1  christos 		}
    864      1.1  christos 	    }
    865      1.1  christos 
    866      1.1  christos 	  (*swap_sym_out) (output_bfd, &internal_sym, sym_out);
    867      1.1  christos 	  sym_out += external_sym_size;
    868      1.1  christos 	}
    869      1.1  christos 
    870      1.1  christos       fdr.isymBase = output_symhdr->isymMax;
    871      1.1  christos       output_symhdr->isymMax += fdr.csym;
    872      1.1  christos 
    873      1.1  christos       /* Copy the information that does not need swapping.  */
    874      1.1  christos 
    875      1.1  christos       /* FIXME: If we are relaxing, we need to adjust the line
    876      1.1  christos 	 numbers.  Frankly, forget it.  Anybody using stabs debugging
    877      1.1  christos 	 information will not use this line number information, and
    878      1.1  christos 	 stabs are adjusted correctly.  */
    879      1.1  christos       if (fdr.cbLine > 0)
    880      1.1  christos 	{
    881      1.1  christos 	  file_ptr pos = input_symhdr->cbLineOffset + fdr.cbLineOffset;
    882      1.1  christos 	  if (!add_file_shuffle (ainfo, &ainfo->line, &ainfo->line_end,
    883      1.1  christos 				 input_bfd, pos, (unsigned long) fdr.cbLine))
    884      1.1  christos 	    return FALSE;
    885      1.1  christos 	  fdr.ilineBase = output_symhdr->ilineMax;
    886      1.1  christos 	  fdr.cbLineOffset = output_symhdr->cbLine;
    887      1.1  christos 	  output_symhdr->ilineMax += fdr.cline;
    888      1.1  christos 	  output_symhdr->cbLine += fdr.cbLine;
    889      1.1  christos 	}
    890      1.1  christos       if (fdr.caux > 0)
    891      1.1  christos 	{
    892      1.1  christos 	  file_ptr pos = (input_symhdr->cbAuxOffset
    893      1.1  christos 			  + fdr.iauxBase * sizeof (union aux_ext));
    894      1.1  christos 	  if (!add_file_shuffle (ainfo, &ainfo->aux, &ainfo->aux_end,
    895      1.1  christos 				 input_bfd, pos,
    896      1.1  christos 				 fdr.caux * sizeof (union aux_ext)))
    897      1.1  christos 	    return FALSE;
    898      1.1  christos 	  fdr.iauxBase = output_symhdr->iauxMax;
    899  1.1.1.4  christos 	  output_symhdr->iauxMax += fdr.caux;
    900      1.1  christos 	}
    901      1.1  christos       if (! bfd_link_relocatable (info))
    902  1.1.1.6  christos 	{
    903      1.1  christos 
    904      1.1  christos 	  /* When we are hashing strings, we lie about the number of
    905      1.1  christos 	     strings attached to each FDR.  We need to set cbSs
    906      1.1  christos 	     because some versions of dbx apparently use it to decide
    907      1.1  christos 	     how much of the string table to read in.  */
    908      1.1  christos 	  fdr.issBase = 0;
    909      1.1  christos 	  fdr.cbSs = output_symhdr->issMax;
    910      1.1  christos 	}
    911      1.1  christos       else if (fdr.cbSs > 0)
    912      1.1  christos 	{
    913      1.1  christos 	  file_ptr pos = input_symhdr->cbSsOffset + fdr.issBase;
    914      1.1  christos 	  if (!add_file_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end,
    915      1.1  christos 				 input_bfd, pos, (unsigned long) fdr.cbSs))
    916      1.1  christos 	    return FALSE;
    917      1.1  christos 	  fdr.issBase = output_symhdr->issMax;
    918      1.1  christos 	  output_symhdr->issMax += fdr.cbSs;
    919      1.1  christos 	}
    920      1.1  christos 
    921      1.1  christos       if (output_bfd->xvec->header_byteorder
    922      1.1  christos 	  == input_bfd->xvec->header_byteorder)
    923      1.1  christos 	{
    924      1.1  christos 	  /* The two BFD's have the same endianness, and we don't have
    925      1.1  christos 	     to adjust the PDR addresses, so simply copying the
    926      1.1  christos 	     information will suffice.  */
    927      1.1  christos 	  BFD_ASSERT (external_pdr_size == input_swap->external_pdr_size);
    928      1.1  christos 	  if (fdr.cpd > 0)
    929      1.1  christos 	    {
    930      1.1  christos 	      file_ptr pos = (input_symhdr->cbPdOffset
    931      1.1  christos 			      + fdr.ipdFirst * external_pdr_size);
    932      1.1  christos 	      unsigned long size = fdr.cpd * external_pdr_size;
    933      1.1  christos 	      if (!add_file_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end,
    934      1.1  christos 				     input_bfd, pos, size))
    935      1.1  christos 		return FALSE;
    936      1.1  christos 	    }
    937      1.1  christos 	  BFD_ASSERT (external_opt_size == input_swap->external_opt_size);
    938      1.1  christos 	  if (fdr.copt > 0)
    939      1.1  christos 	    {
    940      1.1  christos 	      file_ptr pos = (input_symhdr->cbOptOffset
    941      1.1  christos 			      + fdr.ioptBase * external_opt_size);
    942      1.1  christos 	      unsigned long size = fdr.copt * external_opt_size;
    943      1.1  christos 	      if (!add_file_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end,
    944      1.1  christos 				     input_bfd, pos, size))
    945      1.1  christos 		return FALSE;
    946      1.1  christos 	    }
    947      1.1  christos 	}
    948      1.1  christos       else
    949      1.1  christos 	{
    950      1.1  christos 	  bfd_size_type outsz, insz;
    951      1.1  christos 	  bfd_byte *in;
    952      1.1  christos 	  bfd_byte *end;
    953      1.1  christos 	  bfd_byte *out;
    954      1.1  christos 
    955      1.1  christos 	  /* The two BFD's have different endianness, so we must swap
    956      1.1  christos 	     everything in and out.  This code would always work, but
    957      1.1  christos 	     it would be unnecessarily slow in the normal case.  */
    958      1.1  christos 	  outsz = external_pdr_size;
    959      1.1  christos 	  insz = input_swap->external_pdr_size;
    960      1.1  christos 	  in = ((bfd_byte *) input_debug->external_pdr
    961      1.1  christos 		+ fdr.ipdFirst * insz);
    962      1.1  christos 	  end = in + fdr.cpd * insz;
    963      1.1  christos 	  sz = fdr.cpd * outsz;
    964      1.1  christos 	  out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
    965      1.1  christos 	  if (!out)
    966      1.1  christos 	    {
    967      1.1  christos 	      bfd_set_error (bfd_error_no_memory);
    968      1.1  christos 	      return FALSE;
    969      1.1  christos 	    }
    970      1.1  christos 	  if (!add_memory_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end, out,
    971      1.1  christos 				   sz))
    972      1.1  christos 	    return FALSE;
    973      1.1  christos 	  for (; in < end; in += insz, out += outsz)
    974      1.1  christos 	    {
    975  1.1.1.2  christos 	      PDR pdr;
    976  1.1.1.2  christos 
    977      1.1  christos 	      (*input_swap->swap_pdr_in) (input_bfd, in, &pdr);
    978      1.1  christos 	      (*output_swap->swap_pdr_out) (output_bfd, &pdr, out);
    979      1.1  christos 	    }
    980      1.1  christos 
    981      1.1  christos 	  /* Swap over the optimization information.  */
    982      1.1  christos 	  outsz = external_opt_size;
    983      1.1  christos 	  insz = input_swap->external_opt_size;
    984      1.1  christos 	  in = ((bfd_byte *) input_debug->external_opt
    985      1.1  christos 		+ fdr.ioptBase * insz);
    986      1.1  christos 	  end = in + fdr.copt * insz;
    987      1.1  christos 	  sz = fdr.copt * outsz;
    988      1.1  christos 	  out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
    989      1.1  christos 	  if (!out)
    990      1.1  christos 	    {
    991      1.1  christos 	      bfd_set_error (bfd_error_no_memory);
    992      1.1  christos 	      return FALSE;
    993      1.1  christos 	    }
    994      1.1  christos 	  if (!add_memory_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end, out,
    995      1.1  christos 				   sz))
    996      1.1  christos 	    return FALSE;
    997      1.1  christos 	  for (; in < end; in += insz, out += outsz)
    998      1.1  christos 	    {
    999  1.1.1.2  christos 	      OPTR opt;
   1000  1.1.1.2  christos 
   1001      1.1  christos 	      (*input_swap->swap_opt_in) (input_bfd, in, &opt);
   1002      1.1  christos 	      (*output_swap->swap_opt_out) (output_bfd, &opt, out);
   1003      1.1  christos 	    }
   1004      1.1  christos 	}
   1005      1.1  christos 
   1006      1.1  christos       fdr.ipdFirst = output_symhdr->ipdMax;
   1007      1.1  christos       output_symhdr->ipdMax += fdr.cpd;
   1008      1.1  christos       fdr.ioptBase = output_symhdr->ioptMax;
   1009      1.1  christos       output_symhdr->ioptMax += fdr.copt;
   1010      1.1  christos 
   1011      1.1  christos       if (fdr.crfd <= 0)
   1012      1.1  christos 	{
   1013      1.1  christos 	  /* Point this FDR at the table of RFD's we created.  */
   1014      1.1  christos 	  fdr.rfdBase = newrfdbase;
   1015      1.1  christos 	  fdr.crfd = input_symhdr->ifdMax;
   1016      1.1  christos 	}
   1017      1.1  christos       else
   1018      1.1  christos 	{
   1019      1.1  christos 	  /* Point this FDR at the remapped RFD's.  */
   1020      1.1  christos 	  fdr.rfdBase += oldrfdbase;
   1021      1.1  christos 	}
   1022      1.1  christos 
   1023      1.1  christos       (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
   1024      1.1  christos       fdr_out += external_fdr_size;
   1025      1.1  christos       ++output_symhdr->ifdMax;
   1026      1.1  christos     }
   1027      1.1  christos 
   1028      1.1  christos   return TRUE;
   1029      1.1  christos }
   1030      1.1  christos 
   1031      1.1  christos /* Add a string to the debugging information we are accumulating.
   1032      1.1  christos    Return the offset from the fdr string base.  */
   1033  1.1.1.2  christos 
   1034  1.1.1.2  christos static long
   1035  1.1.1.2  christos ecoff_add_string (struct accumulate *ainfo,
   1036  1.1.1.2  christos 		  struct bfd_link_info *info,
   1037  1.1.1.2  christos 		  struct ecoff_debug_info *debug,
   1038      1.1  christos 		  FDR *fdr,
   1039      1.1  christos 		  const char *string)
   1040      1.1  christos {
   1041      1.1  christos   HDRR *symhdr;
   1042      1.1  christos   size_t len;
   1043      1.1  christos   bfd_size_type ret;
   1044      1.1  christos 
   1045  1.1.1.4  christos   symhdr = &debug->symbolic_header;
   1046      1.1  christos   len = strlen (string);
   1047      1.1  christos   if (bfd_link_relocatable (info))
   1048  1.1.1.6  christos     {
   1049      1.1  christos       if (!add_memory_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end,
   1050      1.1  christos 			       (bfd_byte *) string, len + 1))
   1051      1.1  christos 	return -1;
   1052      1.1  christos       ret = symhdr->issMax;
   1053      1.1  christos       symhdr->issMax += len + 1;
   1054      1.1  christos       fdr->cbSs += len + 1;
   1055      1.1  christos     }
   1056      1.1  christos   else
   1057      1.1  christos     {
   1058      1.1  christos       struct string_hash_entry *sh;
   1059      1.1  christos 
   1060      1.1  christos       sh = string_hash_lookup (&ainfo->str_hash, string, TRUE, TRUE);
   1061      1.1  christos       if (sh == (struct string_hash_entry *) NULL)
   1062      1.1  christos 	return -1;
   1063      1.1  christos       if (sh->val == -1)
   1064      1.1  christos 	{
   1065      1.1  christos 	  sh->val = symhdr->issMax;
   1066      1.1  christos 	  symhdr->issMax += len + 1;
   1067      1.1  christos 	  if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
   1068      1.1  christos 	    ainfo->ss_hash = sh;
   1069      1.1  christos 	  if (ainfo->ss_hash_end
   1070      1.1  christos 	      != (struct string_hash_entry *) NULL)
   1071      1.1  christos 	    ainfo->ss_hash_end->next = sh;
   1072      1.1  christos 	  ainfo->ss_hash_end = sh;
   1073      1.1  christos 	}
   1074      1.1  christos       ret = sh->val;
   1075      1.1  christos     }
   1076      1.1  christos 
   1077      1.1  christos   return ret;
   1078      1.1  christos }
   1079      1.1  christos 
   1080      1.1  christos /* Add debugging information from a non-ECOFF file.  */
   1081  1.1.1.2  christos 
   1082  1.1.1.2  christos bfd_boolean
   1083  1.1.1.2  christos bfd_ecoff_debug_accumulate_other (void * handle,
   1084  1.1.1.2  christos 				  bfd *output_bfd,
   1085  1.1.1.2  christos 				  struct ecoff_debug_info *output_debug,
   1086  1.1.1.2  christos 				  const struct ecoff_debug_swap *output_swap,
   1087      1.1  christos 				  bfd *input_bfd,
   1088      1.1  christos 				  struct bfd_link_info *info)
   1089  1.1.1.2  christos {
   1090      1.1  christos   struct accumulate *ainfo = (struct accumulate *) handle;
   1091      1.1  christos   void (* const swap_sym_out) (bfd *, const SYMR *, void *)
   1092      1.1  christos     = output_swap->swap_sym_out;
   1093      1.1  christos   HDRR *output_symhdr = &output_debug->symbolic_header;
   1094      1.1  christos   FDR fdr;
   1095      1.1  christos   asection *sec;
   1096      1.1  christos   asymbol **symbols;
   1097      1.1  christos   asymbol **sym_ptr;
   1098      1.1  christos   asymbol **sym_end;
   1099  1.1.1.2  christos   long symsize;
   1100      1.1  christos   long symcount;
   1101  1.1.1.2  christos   void * external_fdr;
   1102      1.1  christos 
   1103      1.1  christos   memset (&fdr, 0, sizeof fdr);
   1104      1.1  christos 
   1105      1.1  christos   sec = bfd_get_section_by_name (input_bfd, ".text");
   1106      1.1  christos   if (sec != NULL)
   1107      1.1  christos     fdr.adr = sec->output_section->vma + sec->output_offset;
   1108      1.1  christos   else
   1109      1.1  christos     {
   1110      1.1  christos       /* FIXME: What about .init or .fini?  */
   1111      1.1  christos       fdr.adr = 0;
   1112      1.1  christos     }
   1113      1.1  christos 
   1114      1.1  christos   fdr.issBase = output_symhdr->issMax;
   1115  1.1.1.7  christos   fdr.cbSs = 0;
   1116      1.1  christos   fdr.rss = ecoff_add_string (ainfo, info, output_debug, &fdr,
   1117      1.1  christos 			      bfd_get_filename (input_bfd));
   1118      1.1  christos   if (fdr.rss == -1)
   1119      1.1  christos     return FALSE;
   1120      1.1  christos   fdr.isymBase = output_symhdr->isymMax;
   1121      1.1  christos 
   1122      1.1  christos   /* Get the local symbols from the input BFD.  */
   1123      1.1  christos   symsize = bfd_get_symtab_upper_bound (input_bfd);
   1124      1.1  christos   if (symsize < 0)
   1125      1.1  christos     return FALSE;
   1126      1.1  christos   symbols = (asymbol **) bfd_alloc (output_bfd, (bfd_size_type) symsize);
   1127      1.1  christos   if (symbols == (asymbol **) NULL)
   1128      1.1  christos     return FALSE;
   1129      1.1  christos   symcount = bfd_canonicalize_symtab (input_bfd, symbols);
   1130      1.1  christos   if (symcount < 0)
   1131      1.1  christos     return FALSE;
   1132      1.1  christos   sym_end = symbols + symcount;
   1133      1.1  christos 
   1134      1.1  christos   /* Handle the local symbols.  Any external symbols are handled
   1135      1.1  christos      separately.  */
   1136      1.1  christos   fdr.csym = 0;
   1137      1.1  christos   for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
   1138  1.1.1.2  christos     {
   1139      1.1  christos       SYMR internal_sym;
   1140      1.1  christos       void * external_sym;
   1141      1.1  christos 
   1142  1.1.1.2  christos       if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
   1143      1.1  christos 	continue;
   1144      1.1  christos       memset (&internal_sym, 0, sizeof internal_sym);
   1145      1.1  christos       internal_sym.iss = ecoff_add_string (ainfo, info, output_debug, &fdr,
   1146      1.1  christos 					   (*sym_ptr)->name);
   1147      1.1  christos 
   1148      1.1  christos       if (internal_sym.iss == -1)
   1149      1.1  christos 	return FALSE;
   1150      1.1  christos       if (bfd_is_com_section ((*sym_ptr)->section)
   1151      1.1  christos 	  || bfd_is_und_section ((*sym_ptr)->section))
   1152      1.1  christos 	internal_sym.value = (*sym_ptr)->value;
   1153      1.1  christos       else
   1154      1.1  christos 	internal_sym.value = ((*sym_ptr)->value
   1155      1.1  christos 			      + (*sym_ptr)->section->output_offset
   1156      1.1  christos 			      + (*sym_ptr)->section->output_section->vma);
   1157      1.1  christos       internal_sym.st = stNil;
   1158      1.1  christos       internal_sym.sc = scUndefined;
   1159  1.1.1.2  christos       internal_sym.index = indexNil;
   1160  1.1.1.2  christos 
   1161      1.1  christos       external_sym = objalloc_alloc (ainfo->memory,
   1162      1.1  christos 				     output_swap->external_sym_size);
   1163      1.1  christos       if (!external_sym)
   1164      1.1  christos 	{
   1165      1.1  christos 	  bfd_set_error (bfd_error_no_memory);
   1166      1.1  christos 	  return FALSE;
   1167      1.1  christos 	}
   1168      1.1  christos       (*swap_sym_out) (output_bfd, &internal_sym, external_sym);
   1169      1.1  christos       add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end,
   1170      1.1  christos 			  (bfd_byte *) external_sym,
   1171      1.1  christos 			  (unsigned long) output_swap->external_sym_size);
   1172      1.1  christos       ++fdr.csym;
   1173      1.1  christos       ++output_symhdr->isymMax;
   1174  1.1.1.2  christos     }
   1175      1.1  christos 
   1176      1.1  christos   bfd_release (output_bfd, symbols);
   1177      1.1  christos 
   1178      1.1  christos   /* Leave everything else in the FDR zeroed out.  This will cause
   1179      1.1  christos      the lang field to be langC.  The fBigendian field will
   1180  1.1.1.2  christos      indicate little endian format, but it doesn't matter because
   1181  1.1.1.2  christos      it only applies to aux fields and there are none.  */
   1182      1.1  christos   external_fdr = objalloc_alloc (ainfo->memory,
   1183      1.1  christos 				 output_swap->external_fdr_size);
   1184      1.1  christos   if (!external_fdr)
   1185      1.1  christos     {
   1186      1.1  christos       bfd_set_error (bfd_error_no_memory);
   1187      1.1  christos       return FALSE;
   1188      1.1  christos     }
   1189      1.1  christos   (*output_swap->swap_fdr_out) (output_bfd, &fdr, external_fdr);
   1190      1.1  christos   add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end,
   1191      1.1  christos 		      (bfd_byte *) external_fdr,
   1192      1.1  christos 		      (unsigned long) output_swap->external_fdr_size);
   1193      1.1  christos 
   1194      1.1  christos   ++output_symhdr->ifdMax;
   1195      1.1  christos 
   1196      1.1  christos   return TRUE;
   1197      1.1  christos }
   1198      1.1  christos 
   1199      1.1  christos /* Set up ECOFF debugging information for the external symbols.
   1200      1.1  christos    FIXME: This is done using a memory buffer, but it should be
   1201      1.1  christos    probably be changed to use a shuffle structure.  The assembler uses
   1202      1.1  christos    this interface, so that must be changed to do something else.  */
   1203  1.1.1.2  christos 
   1204  1.1.1.2  christos bfd_boolean
   1205  1.1.1.2  christos bfd_ecoff_debug_externals (bfd *abfd,
   1206  1.1.1.2  christos 			   struct ecoff_debug_info *debug,
   1207  1.1.1.2  christos 			   const struct ecoff_debug_swap *swap,
   1208  1.1.1.2  christos 			   bfd_boolean relocatable,
   1209      1.1  christos 			   bfd_boolean (*get_extr) (asymbol *, EXTR *),
   1210      1.1  christos 			   void (*set_index) (asymbol *, bfd_size_type))
   1211      1.1  christos {
   1212      1.1  christos   HDRR * const symhdr = &debug->symbolic_header;
   1213      1.1  christos   asymbol **sym_ptr_ptr;
   1214      1.1  christos   size_t c;
   1215      1.1  christos 
   1216      1.1  christos   sym_ptr_ptr = bfd_get_outsymbols (abfd);
   1217      1.1  christos   if (sym_ptr_ptr == NULL)
   1218      1.1  christos     return TRUE;
   1219      1.1  christos 
   1220      1.1  christos   for (c = bfd_get_symcount (abfd); c > 0; c--, sym_ptr_ptr++)
   1221      1.1  christos     {
   1222      1.1  christos       asymbol *sym_ptr;
   1223      1.1  christos       EXTR esym;
   1224      1.1  christos 
   1225      1.1  christos       sym_ptr = *sym_ptr_ptr;
   1226      1.1  christos 
   1227      1.1  christos       /* Get the external symbol information.  */
   1228      1.1  christos       if (! (*get_extr) (sym_ptr, &esym))
   1229      1.1  christos 	continue;
   1230      1.1  christos 
   1231      1.1  christos       /* If we're producing an executable, move common symbols into
   1232      1.1  christos 	 bss.  */
   1233      1.1  christos       if (! relocatable)
   1234      1.1  christos 	{
   1235      1.1  christos 	  if (esym.asym.sc == scCommon)
   1236      1.1  christos 	    esym.asym.sc = scBss;
   1237      1.1  christos 	  else if (esym.asym.sc == scSCommon)
   1238      1.1  christos 	    esym.asym.sc = scSBss;
   1239      1.1  christos 	}
   1240      1.1  christos 
   1241      1.1  christos       if (bfd_is_com_section (sym_ptr->section)
   1242      1.1  christos 	  || bfd_is_und_section (sym_ptr->section)
   1243      1.1  christos 	  || sym_ptr->section->output_section == (asection *) NULL)
   1244      1.1  christos 	{
   1245      1.1  christos 	  /* FIXME: gas does not keep the value of a small undefined
   1246      1.1  christos 	     symbol in the symbol itself, because of relocation
   1247      1.1  christos 	     problems.  */
   1248      1.1  christos 	  if (esym.asym.sc != scSUndefined
   1249      1.1  christos 	      || esym.asym.value == 0
   1250      1.1  christos 	      || sym_ptr->value != 0)
   1251      1.1  christos 	    esym.asym.value = sym_ptr->value;
   1252      1.1  christos 	}
   1253      1.1  christos       else
   1254      1.1  christos 	esym.asym.value = (sym_ptr->value
   1255      1.1  christos 			   + sym_ptr->section->output_offset
   1256      1.1  christos 			   + sym_ptr->section->output_section->vma);
   1257      1.1  christos 
   1258      1.1  christos       if (set_index)
   1259      1.1  christos 	(*set_index) (sym_ptr, (bfd_size_type) symhdr->iextMax);
   1260      1.1  christos 
   1261      1.1  christos       if (! bfd_ecoff_debug_one_external (abfd, debug, swap,
   1262      1.1  christos 					  sym_ptr->name, &esym))
   1263      1.1  christos 	return FALSE;
   1264      1.1  christos     }
   1265      1.1  christos 
   1266      1.1  christos   return TRUE;
   1267      1.1  christos }
   1268      1.1  christos 
   1269      1.1  christos /* Add a single external symbol to the debugging information.  */
   1270  1.1.1.2  christos 
   1271  1.1.1.2  christos bfd_boolean
   1272  1.1.1.2  christos bfd_ecoff_debug_one_external (bfd *abfd,
   1273  1.1.1.2  christos 			      struct ecoff_debug_info *debug,
   1274  1.1.1.2  christos 			      const struct ecoff_debug_swap *swap,
   1275      1.1  christos 			      const char *name,
   1276      1.1  christos 			      EXTR *esym)
   1277  1.1.1.2  christos {
   1278      1.1  christos   const bfd_size_type external_ext_size = swap->external_ext_size;
   1279      1.1  christos   void (* const swap_ext_out) (bfd *, const EXTR *, void *)
   1280      1.1  christos     = swap->swap_ext_out;
   1281      1.1  christos   HDRR * const symhdr = &debug->symbolic_header;
   1282      1.1  christos   size_t namelen;
   1283      1.1  christos 
   1284      1.1  christos   namelen = strlen (name);
   1285      1.1  christos 
   1286      1.1  christos   if ((size_t) (debug->ssext_end - debug->ssext)
   1287      1.1  christos       < symhdr->issExtMax + namelen + 1)
   1288      1.1  christos     {
   1289      1.1  christos       if (! ecoff_add_bytes ((char **) &debug->ssext,
   1290      1.1  christos 			     (char **) &debug->ssext_end,
   1291      1.1  christos 			     symhdr->issExtMax + namelen + 1))
   1292      1.1  christos 	return FALSE;
   1293      1.1  christos     }
   1294      1.1  christos   if ((size_t) ((char *) debug->external_ext_end
   1295      1.1  christos 		- (char *) debug->external_ext)
   1296      1.1  christos       < (symhdr->iextMax + 1) * external_ext_size)
   1297      1.1  christos     {
   1298      1.1  christos       char *external_ext = (char *) debug->external_ext;
   1299      1.1  christos       char *external_ext_end = (char *) debug->external_ext_end;
   1300      1.1  christos       if (! ecoff_add_bytes ((char **) &external_ext,
   1301      1.1  christos 			     (char **) &external_ext_end,
   1302      1.1  christos 			     (symhdr->iextMax + 1) * (size_t) external_ext_size))
   1303      1.1  christos 	return FALSE;
   1304      1.1  christos       debug->external_ext = external_ext;
   1305      1.1  christos       debug->external_ext_end = external_ext_end;
   1306      1.1  christos     }
   1307      1.1  christos 
   1308      1.1  christos   esym->asym.iss = symhdr->issExtMax;
   1309      1.1  christos 
   1310      1.1  christos   (*swap_ext_out) (abfd, esym,
   1311      1.1  christos 		   ((char *) debug->external_ext
   1312      1.1  christos 		    + symhdr->iextMax * swap->external_ext_size));
   1313      1.1  christos 
   1314      1.1  christos   ++symhdr->iextMax;
   1315      1.1  christos 
   1316      1.1  christos   strcpy (debug->ssext + symhdr->issExtMax, name);
   1317      1.1  christos   symhdr->issExtMax += namelen + 1;
   1318      1.1  christos 
   1319      1.1  christos   return TRUE;
   1320      1.1  christos }
   1321      1.1  christos 
   1322      1.1  christos /* Align the ECOFF debugging information.  */
   1323  1.1.1.2  christos 
   1324  1.1.1.2  christos static void
   1325  1.1.1.2  christos ecoff_align_debug (bfd *abfd ATTRIBUTE_UNUSED,
   1326      1.1  christos 		   struct ecoff_debug_info *debug,
   1327      1.1  christos 		   const struct ecoff_debug_swap *swap)
   1328      1.1  christos {
   1329      1.1  christos   HDRR * const symhdr = &debug->symbolic_header;
   1330      1.1  christos   bfd_size_type debug_align, aux_align, rfd_align;
   1331      1.1  christos   size_t add;
   1332      1.1  christos 
   1333      1.1  christos   /* Adjust the counts so that structures are aligned.  */
   1334      1.1  christos   debug_align = swap->debug_align;
   1335      1.1  christos   aux_align = debug_align / sizeof (union aux_ext);
   1336      1.1  christos   rfd_align = debug_align / swap->external_rfd_size;
   1337      1.1  christos 
   1338      1.1  christos   add = debug_align - (symhdr->cbLine & (debug_align - 1));
   1339      1.1  christos   if (add != debug_align)
   1340  1.1.1.2  christos     {
   1341      1.1  christos       if (debug->line != (unsigned char *) NULL)
   1342      1.1  christos 	memset ((debug->line + symhdr->cbLine), 0, add);
   1343      1.1  christos       symhdr->cbLine += add;
   1344      1.1  christos     }
   1345      1.1  christos 
   1346      1.1  christos   add = debug_align - (symhdr->issMax & (debug_align - 1));
   1347      1.1  christos   if (add != debug_align)
   1348  1.1.1.2  christos     {
   1349      1.1  christos       if (debug->ss != (char *) NULL)
   1350      1.1  christos 	memset ((debug->ss + symhdr->issMax), 0, add);
   1351      1.1  christos       symhdr->issMax += add;
   1352      1.1  christos     }
   1353      1.1  christos 
   1354      1.1  christos   add = debug_align - (symhdr->issExtMax & (debug_align - 1));
   1355      1.1  christos   if (add != debug_align)
   1356  1.1.1.2  christos     {
   1357      1.1  christos       if (debug->ssext != (char *) NULL)
   1358      1.1  christos 	memset ((debug->ssext + symhdr->issExtMax), 0, add);
   1359      1.1  christos       symhdr->issExtMax += add;
   1360      1.1  christos     }
   1361      1.1  christos 
   1362      1.1  christos   add = aux_align - (symhdr->iauxMax & (aux_align - 1));
   1363      1.1  christos   if (add != aux_align)
   1364  1.1.1.2  christos     {
   1365      1.1  christos       if (debug->external_aux != (union aux_ext *) NULL)
   1366      1.1  christos 	memset ((debug->external_aux + symhdr->iauxMax), 0,
   1367      1.1  christos 		add * sizeof (union aux_ext));
   1368      1.1  christos       symhdr->iauxMax += add;
   1369      1.1  christos     }
   1370      1.1  christos 
   1371      1.1  christos   add = rfd_align - (symhdr->crfd & (rfd_align - 1));
   1372  1.1.1.2  christos   if (add != rfd_align)
   1373  1.1.1.2  christos     {
   1374  1.1.1.2  christos       if (debug->external_rfd != NULL)
   1375      1.1  christos 	memset (((char *) debug->external_rfd
   1376      1.1  christos 		 + symhdr->crfd * swap->external_rfd_size),
   1377      1.1  christos 		0, (size_t) (add * swap->external_rfd_size));
   1378      1.1  christos       symhdr->crfd += add;
   1379      1.1  christos     }
   1380      1.1  christos }
   1381      1.1  christos 
   1382      1.1  christos /* Return the size required by the ECOFF debugging information.  */
   1383  1.1.1.2  christos 
   1384  1.1.1.2  christos bfd_size_type
   1385  1.1.1.2  christos bfd_ecoff_debug_size (bfd *abfd,
   1386      1.1  christos 		      struct ecoff_debug_info *debug,
   1387      1.1  christos 		      const struct ecoff_debug_swap *swap)
   1388      1.1  christos {
   1389      1.1  christos   bfd_size_type tot;
   1390      1.1  christos 
   1391      1.1  christos   ecoff_align_debug (abfd, debug, swap);
   1392      1.1  christos   tot = swap->external_hdr_size;
   1393      1.1  christos 
   1394      1.1  christos #define ADD(count, size) \
   1395      1.1  christos   tot += debug->symbolic_header.count * size
   1396      1.1  christos 
   1397      1.1  christos   ADD (cbLine, sizeof (unsigned char));
   1398      1.1  christos   ADD (idnMax, swap->external_dnr_size);
   1399      1.1  christos   ADD (ipdMax, swap->external_pdr_size);
   1400      1.1  christos   ADD (isymMax, swap->external_sym_size);
   1401      1.1  christos   ADD (ioptMax, swap->external_opt_size);
   1402      1.1  christos   ADD (iauxMax, sizeof (union aux_ext));
   1403      1.1  christos   ADD (issMax, sizeof (char));
   1404      1.1  christos   ADD (issExtMax, sizeof (char));
   1405      1.1  christos   ADD (ifdMax, swap->external_fdr_size);
   1406      1.1  christos   ADD (crfd, swap->external_rfd_size);
   1407      1.1  christos   ADD (iextMax, swap->external_ext_size);
   1408      1.1  christos 
   1409      1.1  christos #undef ADD
   1410      1.1  christos 
   1411      1.1  christos   return tot;
   1412      1.1  christos }
   1413      1.1  christos 
   1414      1.1  christos /* Write out the ECOFF symbolic header, given the file position it is
   1415      1.1  christos    going to be placed at.  This assumes that the counts are set
   1416      1.1  christos    correctly.  */
   1417  1.1.1.2  christos 
   1418  1.1.1.2  christos static bfd_boolean
   1419  1.1.1.2  christos ecoff_write_symhdr (bfd *abfd,
   1420  1.1.1.2  christos 		    struct ecoff_debug_info *debug,
   1421      1.1  christos 		    const struct ecoff_debug_swap *swap,
   1422      1.1  christos 		    file_ptr where)
   1423      1.1  christos {
   1424      1.1  christos   HDRR * const symhdr = &debug->symbolic_header;
   1425      1.1  christos   char *buff = NULL;
   1426      1.1  christos 
   1427      1.1  christos   ecoff_align_debug (abfd, debug, swap);
   1428      1.1  christos 
   1429      1.1  christos   /* Go to the right location in the file.  */
   1430      1.1  christos   if (bfd_seek (abfd, where, SEEK_SET) != 0)
   1431      1.1  christos     return FALSE;
   1432      1.1  christos 
   1433      1.1  christos   where += swap->external_hdr_size;
   1434      1.1  christos 
   1435      1.1  christos   symhdr->magic = swap->sym_magic;
   1436      1.1  christos 
   1437      1.1  christos   /* Fill in the file offsets.  */
   1438      1.1  christos #define SET(offset, count, size) \
   1439      1.1  christos   if (symhdr->count == 0) \
   1440      1.1  christos     symhdr->offset = 0; \
   1441      1.1  christos   else \
   1442      1.1  christos     { \
   1443      1.1  christos       symhdr->offset = where; \
   1444      1.1  christos       where += symhdr->count * size; \
   1445      1.1  christos     }
   1446      1.1  christos 
   1447      1.1  christos   SET (cbLineOffset, cbLine, sizeof (unsigned char));
   1448      1.1  christos   SET (cbDnOffset, idnMax, swap->external_dnr_size);
   1449      1.1  christos   SET (cbPdOffset, ipdMax, swap->external_pdr_size);
   1450      1.1  christos   SET (cbSymOffset, isymMax, swap->external_sym_size);
   1451      1.1  christos   SET (cbOptOffset, ioptMax, swap->external_opt_size);
   1452      1.1  christos   SET (cbAuxOffset, iauxMax, sizeof (union aux_ext));
   1453      1.1  christos   SET (cbSsOffset, issMax, sizeof (char));
   1454      1.1  christos   SET (cbSsExtOffset, issExtMax, sizeof (char));
   1455      1.1  christos   SET (cbFdOffset, ifdMax, swap->external_fdr_size);
   1456      1.1  christos   SET (cbRfdOffset, crfd, swap->external_rfd_size);
   1457      1.1  christos   SET (cbExtOffset, iextMax, swap->external_ext_size);
   1458      1.1  christos #undef SET
   1459      1.1  christos 
   1460      1.1  christos   buff = (char *) bfd_malloc (swap->external_hdr_size);
   1461      1.1  christos   if (buff == NULL && swap->external_hdr_size != 0)
   1462      1.1  christos     goto error_return;
   1463      1.1  christos 
   1464      1.1  christos   (*swap->swap_hdr_out) (abfd, symhdr, buff);
   1465      1.1  christos   if (bfd_bwrite (buff, swap->external_hdr_size, abfd)
   1466      1.1  christos       != swap->external_hdr_size)
   1467  1.1.1.7  christos     goto error_return;
   1468      1.1  christos 
   1469      1.1  christos   free (buff);
   1470  1.1.1.7  christos   return TRUE;
   1471      1.1  christos  error_return:
   1472      1.1  christos   free (buff);
   1473      1.1  christos   return FALSE;
   1474      1.1  christos }
   1475      1.1  christos 
   1476      1.1  christos /* Write out the ECOFF debugging information.  This function assumes
   1477      1.1  christos    that the information (the pointers and counts) in *DEBUG have been
   1478      1.1  christos    set correctly.  WHERE is the position in the file to write the
   1479      1.1  christos    information to.  This function fills in the file offsets in the
   1480      1.1  christos    symbolic header.  */
   1481  1.1.1.2  christos 
   1482  1.1.1.2  christos bfd_boolean
   1483  1.1.1.2  christos bfd_ecoff_write_debug (bfd *abfd,
   1484  1.1.1.2  christos 		       struct ecoff_debug_info *debug,
   1485      1.1  christos 		       const struct ecoff_debug_swap *swap,
   1486      1.1  christos 		       file_ptr where)
   1487      1.1  christos {
   1488      1.1  christos   HDRR * const symhdr = &debug->symbolic_header;
   1489      1.1  christos 
   1490      1.1  christos   if (! ecoff_write_symhdr (abfd, debug, swap, where))
   1491      1.1  christos     return FALSE;
   1492  1.1.1.7  christos 
   1493  1.1.1.7  christos #define WRITE(ptr, count, size, offset) \
   1494  1.1.1.7  christos   BFD_ASSERT (symhdr->offset == 0				\
   1495  1.1.1.7  christos 	      || (bfd_vma) bfd_tell (abfd) == symhdr->offset);	\
   1496  1.1.1.7  christos   if (symhdr->count != 0					\
   1497  1.1.1.7  christos       && bfd_bwrite (debug->ptr,				\
   1498      1.1  christos 		     (bfd_size_type) size * symhdr->count,	\
   1499      1.1  christos 		     abfd) != size * symhdr->count)		\
   1500      1.1  christos     return FALSE;
   1501      1.1  christos 
   1502      1.1  christos   WRITE (line, cbLine, sizeof (unsigned char), cbLineOffset);
   1503      1.1  christos   WRITE (external_dnr, idnMax, swap->external_dnr_size, cbDnOffset);
   1504      1.1  christos   WRITE (external_pdr, ipdMax, swap->external_pdr_size, cbPdOffset);
   1505      1.1  christos   WRITE (external_sym, isymMax, swap->external_sym_size, cbSymOffset);
   1506      1.1  christos   WRITE (external_opt, ioptMax, swap->external_opt_size, cbOptOffset);
   1507      1.1  christos   WRITE (external_aux, iauxMax, (bfd_size_type) sizeof (union aux_ext),
   1508      1.1  christos 	 cbAuxOffset);
   1509      1.1  christos   WRITE (ss, issMax, sizeof (char), cbSsOffset);
   1510      1.1  christos   WRITE (ssext, issExtMax, sizeof (char), cbSsExtOffset);
   1511      1.1  christos   WRITE (external_fdr, ifdMax, swap->external_fdr_size, cbFdOffset);
   1512      1.1  christos   WRITE (external_rfd, crfd, swap->external_rfd_size, cbRfdOffset);
   1513      1.1  christos   WRITE (external_ext, iextMax, swap->external_ext_size, cbExtOffset);
   1514      1.1  christos #undef WRITE
   1515      1.1  christos 
   1516      1.1  christos   return TRUE;
   1517      1.1  christos }
   1518      1.1  christos 
   1519      1.1  christos /* Write out a shuffle list.  */
   1520      1.1  christos 
   1521  1.1.1.2  christos 
   1522  1.1.1.2  christos static bfd_boolean
   1523  1.1.1.2  christos ecoff_write_shuffle (bfd *abfd,
   1524  1.1.1.2  christos 		     const struct ecoff_debug_swap *swap,
   1525      1.1  christos 		     struct shuffle *shuffle,
   1526  1.1.1.2  christos 		     void * space)
   1527      1.1  christos {
   1528      1.1  christos   struct shuffle *l;
   1529      1.1  christos   unsigned long total;
   1530      1.1  christos 
   1531      1.1  christos   total = 0;
   1532      1.1  christos   for (l = shuffle; l != (struct shuffle *) NULL; l = l->next)
   1533      1.1  christos     {
   1534      1.1  christos       if (! l->filep)
   1535      1.1  christos 	{
   1536      1.1  christos 	  if (bfd_bwrite (l->u.memory, (bfd_size_type) l->size, abfd)
   1537      1.1  christos 	      != l->size)
   1538      1.1  christos 	    return FALSE;
   1539      1.1  christos 	}
   1540      1.1  christos       else
   1541      1.1  christos 	{
   1542      1.1  christos 	  if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
   1543      1.1  christos 	      || bfd_bread (space, (bfd_size_type) l->size,
   1544      1.1  christos 			   l->u.file.input_bfd) != l->size
   1545      1.1  christos 	      || bfd_bwrite (space, (bfd_size_type) l->size, abfd) != l->size)
   1546      1.1  christos 	    return FALSE;
   1547      1.1  christos 	}
   1548      1.1  christos       total += l->size;
   1549      1.1  christos     }
   1550      1.1  christos 
   1551      1.1  christos   if ((total & (swap->debug_align - 1)) != 0)
   1552      1.1  christos     {
   1553      1.1  christos       unsigned int i;
   1554      1.1  christos       bfd_byte *s;
   1555      1.1  christos 
   1556      1.1  christos       i = swap->debug_align - (total & (swap->debug_align - 1));
   1557      1.1  christos       s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
   1558      1.1  christos       if (s == NULL && i != 0)
   1559  1.1.1.2  christos 	return FALSE;
   1560      1.1  christos 
   1561      1.1  christos       if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
   1562      1.1  christos 	{
   1563      1.1  christos 	  free (s);
   1564      1.1  christos 	  return FALSE;
   1565      1.1  christos 	}
   1566      1.1  christos       free (s);
   1567      1.1  christos     }
   1568      1.1  christos 
   1569      1.1  christos   return TRUE;
   1570      1.1  christos }
   1571      1.1  christos 
   1572      1.1  christos /* Write out debugging information using accumulated linker
   1573      1.1  christos    information.  */
   1574  1.1.1.2  christos 
   1575  1.1.1.2  christos bfd_boolean
   1576  1.1.1.2  christos bfd_ecoff_write_accumulated_debug (void * handle,
   1577  1.1.1.2  christos 				   bfd *abfd,
   1578  1.1.1.2  christos 				   struct ecoff_debug_info *debug,
   1579  1.1.1.2  christos 				   const struct ecoff_debug_swap *swap,
   1580      1.1  christos 				   struct bfd_link_info *info,
   1581      1.1  christos 				   file_ptr where)
   1582  1.1.1.2  christos {
   1583      1.1  christos   struct accumulate *ainfo = (struct accumulate *) handle;
   1584      1.1  christos   void * space = NULL;
   1585      1.1  christos   bfd_size_type amt;
   1586      1.1  christos 
   1587      1.1  christos   if (! ecoff_write_symhdr (abfd, debug, swap, where))
   1588      1.1  christos     goto error_return;
   1589  1.1.1.2  christos 
   1590      1.1  christos   amt = ainfo->largest_file_shuffle;
   1591      1.1  christos   space = bfd_malloc (amt);
   1592      1.1  christos   if (space == NULL && ainfo->largest_file_shuffle != 0)
   1593      1.1  christos     goto error_return;
   1594      1.1  christos 
   1595      1.1  christos   if (! ecoff_write_shuffle (abfd, swap, ainfo->line, space)
   1596      1.1  christos       || ! ecoff_write_shuffle (abfd, swap, ainfo->pdr, space)
   1597      1.1  christos       || ! ecoff_write_shuffle (abfd, swap, ainfo->sym, space)
   1598      1.1  christos       || ! ecoff_write_shuffle (abfd, swap, ainfo->opt, space)
   1599      1.1  christos       || ! ecoff_write_shuffle (abfd, swap, ainfo->aux, space))
   1600      1.1  christos     goto error_return;
   1601      1.1  christos 
   1602  1.1.1.4  christos   /* The string table is written out from the hash table if this is a
   1603      1.1  christos      final link.  */
   1604      1.1  christos   if (bfd_link_relocatable (info))
   1605      1.1  christos     {
   1606      1.1  christos       BFD_ASSERT (ainfo->ss_hash == (struct string_hash_entry *) NULL);
   1607      1.1  christos       if (! ecoff_write_shuffle (abfd, swap, ainfo->ss, space))
   1608      1.1  christos 	goto error_return;
   1609      1.1  christos     }
   1610      1.1  christos   else
   1611      1.1  christos     {
   1612      1.1  christos       unsigned long total;
   1613      1.1  christos       bfd_byte null;
   1614      1.1  christos       struct string_hash_entry *sh;
   1615      1.1  christos 
   1616  1.1.1.2  christos       BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
   1617      1.1  christos       null = 0;
   1618      1.1  christos       if (bfd_bwrite (&null, (bfd_size_type) 1, abfd) != 1)
   1619      1.1  christos 	goto error_return;
   1620      1.1  christos       total = 1;
   1621      1.1  christos       BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
   1622      1.1  christos       for (sh = ainfo->ss_hash;
   1623      1.1  christos 	   sh != (struct string_hash_entry *) NULL;
   1624      1.1  christos 	   sh = sh->next)
   1625      1.1  christos 	{
   1626      1.1  christos 	  size_t len;
   1627      1.1  christos 
   1628  1.1.1.2  christos 	  len = strlen (sh->root.string);
   1629      1.1  christos 	  amt = len + 1;
   1630      1.1  christos 	  if (bfd_bwrite (sh->root.string, amt, abfd) != amt)
   1631      1.1  christos 	    goto error_return;
   1632      1.1  christos 	  total += len + 1;
   1633      1.1  christos 	}
   1634      1.1  christos 
   1635      1.1  christos       if ((total & (swap->debug_align - 1)) != 0)
   1636      1.1  christos 	{
   1637      1.1  christos 	  unsigned int i;
   1638      1.1  christos 	  bfd_byte *s;
   1639      1.1  christos 
   1640      1.1  christos 	  i = swap->debug_align - (total & (swap->debug_align - 1));
   1641      1.1  christos 	  s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
   1642      1.1  christos 	  if (s == NULL && i != 0)
   1643  1.1.1.2  christos 	    goto error_return;
   1644      1.1  christos 
   1645      1.1  christos 	  if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
   1646      1.1  christos 	    {
   1647      1.1  christos 	      free (s);
   1648      1.1  christos 	      goto error_return;
   1649      1.1  christos 	    }
   1650      1.1  christos 	  free (s);
   1651      1.1  christos 	}
   1652      1.1  christos     }
   1653      1.1  christos 
   1654      1.1  christos   /* The external strings and symbol are not converted over to using
   1655  1.1.1.7  christos      shuffles.  FIXME: They probably should be.  */
   1656      1.1  christos   amt = debug->symbolic_header.issExtMax;
   1657      1.1  christos   if (amt != 0 && bfd_bwrite (debug->ssext, amt, abfd) != amt)
   1658      1.1  christos     goto error_return;
   1659      1.1  christos   if ((debug->symbolic_header.issExtMax & (swap->debug_align - 1)) != 0)
   1660      1.1  christos     {
   1661      1.1  christos       unsigned int i;
   1662      1.1  christos       bfd_byte *s;
   1663      1.1  christos 
   1664      1.1  christos       i = (swap->debug_align
   1665      1.1  christos 	   - (debug->symbolic_header.issExtMax & (swap->debug_align - 1)));
   1666      1.1  christos       s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
   1667      1.1  christos       if (s == NULL && i != 0)
   1668  1.1.1.2  christos 	goto error_return;
   1669      1.1  christos 
   1670      1.1  christos       if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
   1671      1.1  christos 	{
   1672      1.1  christos 	  free (s);
   1673      1.1  christos 	  goto error_return;
   1674      1.1  christos 	}
   1675      1.1  christos       free (s);
   1676      1.1  christos     }
   1677      1.1  christos 
   1678      1.1  christos   if (! ecoff_write_shuffle (abfd, swap, ainfo->fdr, space)
   1679      1.1  christos       || ! ecoff_write_shuffle (abfd, swap, ainfo->rfd, space))
   1680      1.1  christos     goto error_return;
   1681      1.1  christos 
   1682      1.1  christos   BFD_ASSERT (debug->symbolic_header.cbExtOffset == 0
   1683      1.1  christos 	      || (debug->symbolic_header.cbExtOffset
   1684      1.1  christos 		  == (bfd_vma) bfd_tell (abfd)));
   1685  1.1.1.7  christos 
   1686      1.1  christos   amt = debug->symbolic_header.iextMax * swap->external_ext_size;
   1687      1.1  christos   if (amt != 0 && bfd_bwrite (debug->external_ext, amt, abfd) != amt)
   1688  1.1.1.7  christos     goto error_return;
   1689      1.1  christos 
   1690      1.1  christos   free (space);
   1691      1.1  christos   return TRUE;
   1692  1.1.1.7  christos 
   1693      1.1  christos  error_return:
   1694      1.1  christos   free (space);
   1695      1.1  christos   return FALSE;
   1696      1.1  christos }
   1697      1.1  christos 
   1698      1.1  christos /* Handle the find_nearest_line function for both ECOFF and MIPS ELF
   1700      1.1  christos    files.  */
   1701      1.1  christos 
   1702  1.1.1.2  christos /* Compare FDR entries.  This is called via qsort.  */
   1703      1.1  christos 
   1704      1.1  christos static int
   1705      1.1  christos cmp_fdrtab_entry (const void * leftp, const void * rightp)
   1706      1.1  christos {
   1707      1.1  christos   const struct ecoff_fdrtab_entry *lp =
   1708      1.1  christos     (const struct ecoff_fdrtab_entry *) leftp;
   1709      1.1  christos   const struct ecoff_fdrtab_entry *rp =
   1710      1.1  christos     (const struct ecoff_fdrtab_entry *) rightp;
   1711      1.1  christos 
   1712      1.1  christos   if (lp->base_addr < rp->base_addr)
   1713      1.1  christos     return -1;
   1714      1.1  christos   if (lp->base_addr > rp->base_addr)
   1715      1.1  christos     return 1;
   1716      1.1  christos   return 0;
   1717      1.1  christos }
   1718      1.1  christos 
   1719      1.1  christos /* Each file descriptor (FDR) has a memory address, to simplify
   1720      1.1  christos    looking up an FDR by address, we build a table covering all FDRs
   1721      1.1  christos    that have a least one procedure descriptor in them.  The final
   1722      1.1  christos    table will be sorted by address so we can look it up via binary
   1723  1.1.1.2  christos    search.  */
   1724  1.1.1.2  christos 
   1725  1.1.1.2  christos static bfd_boolean
   1726  1.1.1.2  christos mk_fdrtab (bfd *abfd,
   1727      1.1  christos 	   struct ecoff_debug_info * const debug_info,
   1728      1.1  christos 	   const struct ecoff_debug_swap * const debug_swap,
   1729      1.1  christos 	   struct ecoff_find_line *line_info)
   1730      1.1  christos {
   1731      1.1  christos   struct ecoff_fdrtab_entry *tab;
   1732      1.1  christos   FDR *fdr_ptr;
   1733      1.1  christos   FDR *fdr_start;
   1734      1.1  christos   FDR *fdr_end;
   1735      1.1  christos   bfd_boolean stabs;
   1736      1.1  christos   long len;
   1737      1.1  christos   bfd_size_type amt;
   1738      1.1  christos 
   1739      1.1  christos   fdr_start = debug_info->fdr;
   1740      1.1  christos   fdr_end = fdr_start + debug_info->symbolic_header.ifdMax;
   1741      1.1  christos 
   1742      1.1  christos   /* First, let's see how long the table needs to be.  */
   1743      1.1  christos   for (len = 0, fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
   1744      1.1  christos     {
   1745      1.1  christos       if (fdr_ptr->cpd == 0)	/* Skip FDRs that have no PDRs.  */
   1746      1.1  christos 	continue;
   1747      1.1  christos       ++len;
   1748      1.1  christos     }
   1749      1.1  christos 
   1750      1.1  christos   /* Now, create and fill in the table.  */
   1751      1.1  christos   amt = (bfd_size_type) len * sizeof (struct ecoff_fdrtab_entry);
   1752      1.1  christos   line_info->fdrtab = (struct ecoff_fdrtab_entry*) bfd_zalloc (abfd, amt);
   1753      1.1  christos   if (line_info->fdrtab == NULL)
   1754      1.1  christos     return FALSE;
   1755      1.1  christos   line_info->fdrtab_len = len;
   1756      1.1  christos 
   1757      1.1  christos   tab = line_info->fdrtab;
   1758      1.1  christos   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
   1759      1.1  christos     {
   1760      1.1  christos       if (fdr_ptr->cpd == 0)
   1761      1.1  christos 	continue;
   1762      1.1  christos 
   1763      1.1  christos       /* Check whether this file has stabs debugging information.  In
   1764      1.1  christos 	 a file with stabs debugging information, the second local
   1765      1.1  christos 	 symbol is named @stabs.  */
   1766      1.1  christos       stabs = FALSE;
   1767      1.1  christos       if (fdr_ptr->csym >= 2)
   1768      1.1  christos 	{
   1769      1.1  christos 	  char *sym_ptr;
   1770      1.1  christos 	  SYMR sym;
   1771      1.1  christos 
   1772      1.1  christos 	  sym_ptr = ((char *) debug_info->external_sym
   1773      1.1  christos 		     + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
   1774      1.1  christos 	  (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
   1775      1.1  christos 	  if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
   1776      1.1  christos 		      STABS_SYMBOL) == 0)
   1777      1.1  christos 	    stabs = TRUE;
   1778      1.1  christos 	}
   1779      1.1  christos 
   1780      1.1  christos       if (!stabs)
   1781      1.1  christos 	{
   1782      1.1  christos 	  /* eraxxon: There are at least two problems with this computation:
   1783      1.1  christos 	     1) PDRs do *not* contain offsets but full vma's; and typically the
   1784      1.1  christos 	     address of the first PDR is the address of the FDR, which will
   1785      1.1  christos 	     make (most) of the results of the original computation 0!
   1786      1.1  christos 	     2) Once in a wacky while, the Compaq compiler generated PDR
   1787      1.1  christos 	     addresses do not equal the FDR vma, but they (the PDR address)
   1788      1.1  christos 	     are still vma's and not offsets.  Cf. comments in
   1789  1.1.1.2  christos 	     'lookup_line'.  */
   1790      1.1  christos 	  /* The address of the first PDR is the offset of that
   1791      1.1  christos 	     procedure relative to the beginning of file FDR.  */
   1792      1.1  christos 	  tab->base_addr = fdr_ptr->adr;
   1793      1.1  christos 	}
   1794      1.1  christos       else
   1795      1.1  christos 	{
   1796      1.1  christos 	  /* XXX I don't know about stabs, so this is a guess
   1797      1.1  christos 	     (davidm (at) cs.arizona.edu).  */
   1798      1.1  christos 	  tab->base_addr = fdr_ptr->adr;
   1799      1.1  christos 	}
   1800      1.1  christos       tab->fdr = fdr_ptr;
   1801      1.1  christos       ++tab;
   1802      1.1  christos     }
   1803      1.1  christos 
   1804      1.1  christos   /* Finally, the table is sorted in increasing memory-address order.
   1805  1.1.1.2  christos      The table is mostly sorted already, but there are cases (e.g.,
   1806      1.1  christos      static functions in include files), where this does not hold.
   1807      1.1  christos      Use "odump -PFv" to verify...  */
   1808      1.1  christos   qsort (line_info->fdrtab, (size_t) len,
   1809      1.1  christos 	 sizeof (struct ecoff_fdrtab_entry), cmp_fdrtab_entry);
   1810      1.1  christos 
   1811      1.1  christos   return TRUE;
   1812      1.1  christos }
   1813      1.1  christos 
   1814  1.1.1.2  christos /* Return index of first FDR that covers to OFFSET.  */
   1815      1.1  christos 
   1816      1.1  christos static long
   1817      1.1  christos fdrtab_lookup (struct ecoff_find_line *line_info, bfd_vma offset)
   1818      1.1  christos {
   1819      1.1  christos   long low, high, len;
   1820      1.1  christos   long mid = -1;
   1821      1.1  christos   struct ecoff_fdrtab_entry *tab;
   1822      1.1  christos 
   1823      1.1  christos   len = line_info->fdrtab_len;
   1824      1.1  christos   if (len == 0)
   1825      1.1  christos     return -1;
   1826      1.1  christos 
   1827      1.1  christos   tab = line_info->fdrtab;
   1828      1.1  christos   for (low = 0, high = len - 1 ; low != high ;)
   1829      1.1  christos     {
   1830      1.1  christos       mid = (high + low) / 2;
   1831      1.1  christos       if (offset >= tab[mid].base_addr && offset < tab[mid + 1].base_addr)
   1832      1.1  christos 	goto find_min;
   1833      1.1  christos 
   1834      1.1  christos       if (tab[mid].base_addr > offset)
   1835      1.1  christos 	high = mid;
   1836      1.1  christos       else
   1837      1.1  christos 	low = mid + 1;
   1838      1.1  christos     }
   1839      1.1  christos 
   1840      1.1  christos   /* eraxxon: at this point 'offset' is either lower than the lowest entry or
   1841      1.1  christos      higher than the highest entry. In the former case high = low = mid = 0;
   1842      1.1  christos      we want to return -1.  In the latter case, low = high and mid = low - 1;
   1843      1.1  christos      we want to return the index of the highest entry.  Only in former case
   1844      1.1  christos      will the following 'catch-all' test be true.  */
   1845      1.1  christos   ++mid;
   1846      1.1  christos 
   1847      1.1  christos   /* Last entry is catch-all for all higher addresses.  */
   1848      1.1  christos   if (offset < tab[mid].base_addr)
   1849      1.1  christos     return -1;
   1850      1.1  christos 
   1851      1.1  christos  find_min:
   1852      1.1  christos 
   1853      1.1  christos   /* eraxxon: There may be multiple FDRs in the table with the
   1854      1.1  christos      same base_addr; make sure that we are at the first one.  */
   1855      1.1  christos   while (mid > 0 && tab[mid - 1].base_addr == tab[mid].base_addr)
   1856      1.1  christos     --mid;
   1857      1.1  christos 
   1858      1.1  christos   return mid;
   1859      1.1  christos }
   1860      1.1  christos 
   1861      1.1  christos /* Look up a line given an address, storing the information in
   1862  1.1.1.2  christos    LINE_INFO->cache.  */
   1863  1.1.1.2  christos 
   1864  1.1.1.2  christos static bfd_boolean
   1865  1.1.1.2  christos lookup_line (bfd *abfd,
   1866      1.1  christos 	     struct ecoff_debug_info * const debug_info,
   1867      1.1  christos 	     const struct ecoff_debug_swap * const debug_swap,
   1868      1.1  christos 	     struct ecoff_find_line *line_info)
   1869      1.1  christos {
   1870      1.1  christos   struct ecoff_fdrtab_entry *tab;
   1871      1.1  christos   bfd_vma offset;
   1872      1.1  christos   bfd_boolean stabs;
   1873      1.1  christos   FDR *fdr_ptr;
   1874      1.1  christos   int i;
   1875      1.1  christos 
   1876      1.1  christos   /* eraxxon: note that 'offset' is the full vma, not a section offset.  */
   1877      1.1  christos   offset = line_info->cache.start;
   1878      1.1  christos 
   1879      1.1  christos   /* Build FDR table (sorted by object file's base-address) if we
   1880      1.1  christos      don't have it already.  */
   1881      1.1  christos   if (line_info->fdrtab == NULL
   1882      1.1  christos       && !mk_fdrtab (abfd, debug_info, debug_swap, line_info))
   1883      1.1  christos     return FALSE;
   1884      1.1  christos 
   1885      1.1  christos   tab = line_info->fdrtab;
   1886      1.1  christos 
   1887      1.1  christos   /* Find first FDR for address OFFSET.  */
   1888  1.1.1.2  christos   i = fdrtab_lookup (line_info, offset);
   1889      1.1  christos   if (i < 0)
   1890      1.1  christos     return FALSE;		/* no FDR, no fun...  */
   1891      1.1  christos 
   1892      1.1  christos   /* eraxxon: 'fdrtab_lookup' doesn't give what we want, at least for Compaq's
   1893      1.1  christos      C++ compiler 6.2.  Consider three FDRs with starting addresses of x, y,
   1894      1.1  christos      and z, respectively, such that x < y < z.  Assume further that
   1895      1.1  christos      y < 'offset' < z.  It is possible at times that the PDR for 'offset' is
   1896      1.1  christos      associated with FDR x and *not* with FDR y.  Erg!!
   1897      1.1  christos 
   1898      1.1  christos      From a binary dump of my C++ test case 'moo' using Compaq's coffobjanl
   1899  1.1.1.6  christos      (output format has been edited for our purposes):
   1900  1.1.1.6  christos 
   1901  1.1.1.6  christos      FDR [2]: (main.C): First instruction: 0x12000207c <x>
   1902  1.1.1.6  christos        PDR [5] for File [2]: LoopTest__Xv		  <0x1200020a0> (a)
   1903  1.1.1.6  christos        PDR [7] for File [2]: foo__Xv			  <0x120002168>
   1904      1.1  christos      FDR [1]: (-1):	First instruction: 0x1200020e8 <y>
   1905      1.1  christos        PDR [3] for File [1]:				  <0x120001ad0> (b)
   1906      1.1  christos      FDR [6]: (-1):	First instruction: 0x1200026f0 <z>
   1907      1.1  christos 
   1908      1.1  christos      (a) In the case of PDR5, the vma is such that the first few instructions
   1909      1.1  christos      of the procedure can be found.  But since the size of this procedure is
   1910      1.1  christos      160b, the vma will soon cross into the 'address space' of FDR1 and no
   1911      1.1  christos      debugging info will be found.  How repugnant!
   1912      1.1  christos 
   1913      1.1  christos      (b) It is also possible for a PDR to have a *lower* vma than its associated
   1914      1.1  christos      FDR; see FDR1 and PDR3.  Gross!
   1915      1.1  christos 
   1916      1.1  christos      Since the FDRs that are causing so much havok (in this case) 1) do not
   1917      1.1  christos      describe actual files (fdr.rss == -1), and 2) contain only compiler
   1918      1.1  christos      generated routines, I thought a simple fix would be to exclude them from
   1919      1.1  christos      the FDR table in 'mk_fdrtab'.  But, besides not knowing for certain
   1920      1.1  christos      whether this would be correct, it creates an additional problem.  If we
   1921      1.1  christos      happen to ask for source file info on a compiler generated (procedure)
   1922      1.1  christos      symbol -- which is still in the symbol table -- the result can be
   1923      1.1  christos      information from a real procedure!  This is because compiler generated
   1924      1.1  christos      procedures with vma's higher than the last FDR in the fdr table will be
   1925      1.1  christos      associated with a PDR from this FDR, specifically the PDR with the
   1926      1.1  christos      highest vma.  This wasn't a problem before, because each procedure had a
   1927      1.1  christos      PDR.  (Yes, this problem could be eliminated if we kept the size of the
   1928      1.1  christos      last PDR around, but things are already getting ugly).
   1929      1.1  christos 
   1930      1.1  christos      Probably, a better solution would be to have a sorted PDR table.  Each
   1931      1.1  christos      PDR would have a pointer to its FDR so file information could still be
   1932      1.1  christos      obtained.  A FDR table could still be constructed if necessary -- since
   1933      1.1  christos      it only contains pointers, not much extra memory would be used -- but
   1934      1.1  christos      the PDR table would be searched to locate debugging info.
   1935      1.1  christos 
   1936      1.1  christos      There is still at least one remaining issue.  Sometimes a FDR can have a
   1937      1.1  christos      bogus name, but contain PDRs that should belong to another FDR with a
   1938      1.1  christos      real name.  E.g:
   1939      1.1  christos 
   1940      1.1  christos      FDR [3]: 0000000120001b50 (/home/.../Array.H~alt~deccxx_5E5A62AD)
   1941      1.1  christos        PDR [a] for File [3]: 0000000120001b50
   1942      1.1  christos        PDR [b] for File [3]: 0000000120001cf0
   1943      1.1  christos        PDR [c] for File [3]: 0000000120001dc8
   1944      1.1  christos        PDR [d] for File [3]: 0000000120001e40
   1945      1.1  christos        PDR [e] for File [3]: 0000000120001eb8
   1946      1.1  christos        PDR [f] for File [3]: 0000000120001f4c
   1947      1.1  christos      FDR [4]: 0000000120001b50 (/home/.../Array.H)
   1948      1.1  christos 
   1949      1.1  christos      Here, FDR4 has the correct name, but should (seemingly) contain PDRa-f.
   1950      1.1  christos      The symbol table for PDR4 does contain symbols for PDRa-f, but so does
   1951      1.1  christos      the symbol table for FDR3.  However the former is different; perhaps this
   1952      1.1  christos      can be detected easily. (I'm not sure at this point.)  This problem only
   1953      1.1  christos      seems to be associated with files with templates.  I am assuming the idea
   1954      1.1  christos      is that there is a 'fake' FDR (with PDRs) for each differently typed set
   1955      1.1  christos      of templates that must be generated.  Currently, FDR4 is completely
   1956      1.1  christos      excluded from the FDR table in 'mk_fdrtab' because it contains no PDRs.
   1957      1.1  christos 
   1958      1.1  christos      Since I don't have time to prepare a real fix for this right now, be
   1959      1.1  christos      prepared for 'A Horrible Hack' to force the inspection of all non-stabs
   1960      1.1  christos      FDRs.  It's coming...  */
   1961      1.1  christos   fdr_ptr = tab[i].fdr;
   1962      1.1  christos 
   1963      1.1  christos   /* Check whether this file has stabs debugging information.  In a
   1964      1.1  christos      file with stabs debugging information, the second local symbol is
   1965      1.1  christos      named @stabs.  */
   1966      1.1  christos   stabs = FALSE;
   1967      1.1  christos   if (fdr_ptr->csym >= 2)
   1968      1.1  christos     {
   1969      1.1  christos       char *sym_ptr;
   1970      1.1  christos       SYMR sym;
   1971      1.1  christos 
   1972      1.1  christos       sym_ptr = ((char *) debug_info->external_sym
   1973      1.1  christos 		 + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
   1974      1.1  christos       (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
   1975      1.1  christos       if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
   1976      1.1  christos 		  STABS_SYMBOL) == 0)
   1977      1.1  christos 	stabs = TRUE;
   1978      1.1  christos     }
   1979      1.1  christos 
   1980      1.1  christos   if (!stabs)
   1981      1.1  christos     {
   1982      1.1  christos       bfd_size_type external_pdr_size;
   1983      1.1  christos       char *pdr_ptr;
   1984      1.1  christos       char *best_pdr = NULL;
   1985      1.1  christos       FDR *best_fdr;
   1986      1.1  christos       bfd_signed_vma best_dist = -1;
   1987      1.1  christos       PDR pdr;
   1988      1.1  christos       unsigned char *line_ptr;
   1989  1.1.1.6  christos       unsigned char *line_end;
   1990  1.1.1.6  christos       int lineno;
   1991  1.1.1.6  christos       /* This file uses ECOFF debugging information.  Each FDR has a
   1992  1.1.1.6  christos 	 list of procedure descriptors (PDR).  The address in the FDR
   1993  1.1.1.6  christos 	 is the absolute address of the first procedure.  The address
   1994  1.1.1.6  christos 	 in the first PDR gives the offset of that procedure relative
   1995  1.1.1.6  christos 	 to the object file's base-address.  The addresses in
   1996  1.1.1.6  christos 	 subsequent PDRs specify each procedure's address relative to
   1997  1.1.1.6  christos 	 the object file's base-address.  To make things more juicy,
   1998  1.1.1.6  christos 	 whenever the PROF bit in the PDR is set, the real entry point
   1999  1.1.1.6  christos 	 of the procedure may be 16 bytes below what would normally be
   2000  1.1.1.6  christos 	 the procedure's entry point.  Instead, DEC came up with a
   2001  1.1.1.6  christos 	 wicked scheme to create profiled libraries "on the fly":
   2002  1.1.1.6  christos 	 instead of shipping a regular and a profiled version of each
   2003  1.1.1.6  christos 	 library, they insert 16 bytes of unused space in front of
   2004  1.1.1.6  christos 	 each procedure and set the "prof" bit in the PDR to indicate
   2005  1.1.1.6  christos 	 that there is a gap there (this is done automagically by "as"
   2006  1.1.1.6  christos 	 when option "-pg" is specified).  Thus, normally, you link
   2007  1.1.1.6  christos 	 against such a library and, except for lots of 16 byte gaps
   2008  1.1.1.6  christos 	 between functions, things will behave as usual.  However,
   2009  1.1.1.6  christos 	 when invoking "ld" with option "-pg", it will fill those gaps
   2010  1.1.1.6  christos 	 with code that calls mcount().  It then moves the function's
   2011  1.1.1.6  christos 	 entry point down by 16 bytes, and out pops a binary that has
   2012  1.1.1.6  christos 	 all functions profiled.
   2013  1.1.1.6  christos 
   2014  1.1.1.6  christos 	 NOTE: Neither FDRs nor PDRs are strictly sorted in memory
   2015  1.1.1.6  christos 	       order.  For example, when including header-files that
   2016  1.1.1.6  christos 	       define functions, the FDRs follow behind the including
   2017  1.1.1.6  christos 	       file, even though their code may have been generated at
   2018  1.1.1.6  christos 	       a lower address.  File coff-alpha.c from libbfd
   2019  1.1.1.6  christos 	       illustrates this (use "odump -PFv" to look at a file's
   2020  1.1.1.6  christos 	       FDR/PDR).  Similarly, PDRs are sometimes out of order
   2021  1.1.1.6  christos 	       as well.  An example of this is OSF/1 v3.0 libc's
   2022  1.1.1.6  christos 	       malloc.c.  I'm not sure why this happens, but it could
   2023  1.1.1.6  christos 	       be due to optimizations that reorder a function's
   2024  1.1.1.6  christos 	       position within an object-file.
   2025  1.1.1.6  christos 
   2026  1.1.1.6  christos 	 Strategy:
   2027  1.1.1.6  christos 
   2028  1.1.1.6  christos 	 On the first call to this function, we build a table of FDRs
   2029  1.1.1.6  christos 	 that is sorted by the base-address of the object-file the FDR
   2030  1.1.1.6  christos 	 is referring to.  Notice that each object-file may contain
   2031  1.1.1.6  christos 	 code from multiple source files (e.g., due to code defined in
   2032  1.1.1.6  christos 	 include files).  Thus, for any given base-address, there may
   2033  1.1.1.6  christos 	 be multiple FDRs (but this case is, fortunately, uncommon).
   2034  1.1.1.6  christos 	 lookup(addr) guarantees to return the first FDR that applies
   2035  1.1.1.6  christos 	 to address ADDR.  Thus, after invoking lookup(), we have a
   2036  1.1.1.6  christos 	 list of FDRs that may contain the PDR for ADDR.  Next, we
   2037  1.1.1.6  christos 	 walk through the PDRs of these FDRs and locate the one that
   2038  1.1.1.6  christos 	 is closest to ADDR (i.e., for which the difference between
   2039  1.1.1.6  christos 	 ADDR and the PDR's entry point is positive and minimal).
   2040  1.1.1.6  christos 	 Once, the right FDR and PDR are located, we simply walk
   2041  1.1.1.6  christos 	 through the line-number table to lookup the line-number that
   2042  1.1.1.6  christos 	 best matches ADDR.  Obviously, things could be sped up by
   2043      1.1  christos 	 keeping a sorted list of PDRs instead of a sorted list of
   2044      1.1  christos 	 FDRs.  However, this would increase space requirements
   2045      1.1  christos 	 considerably, which is undesirable.  */
   2046      1.1  christos       external_pdr_size = debug_swap->external_pdr_size;
   2047      1.1  christos 
   2048      1.1  christos       /* eraxxon: The Horrible Hack: Because of the problems above, set 'i'
   2049      1.1  christos 	 to 0 so we look through all FDRs.
   2050      1.1  christos 
   2051      1.1  christos 	 Because FDR's without any symbols are assumed to be non-stabs,
   2052      1.1  christos 	 searching through all FDRs may cause the following code to try to
   2053  1.1.1.2  christos 	 read stabs FDRs as ECOFF ones.  However, I don't think this will
   2054      1.1  christos 	 harm anything.  */
   2055  1.1.1.6  christos       i = 0;
   2056      1.1  christos 
   2057      1.1  christos       /* Search FDR list starting at tab[i] for the PDR that best matches
   2058      1.1  christos 	 OFFSET.  Normally, the FDR list is only one entry long.  */
   2059      1.1  christos       best_fdr = NULL;
   2060  1.1.1.6  christos       do
   2061  1.1.1.6  christos 	{
   2062      1.1  christos 	  /* eraxxon: 'dist' and 'min_dist' can be negative now
   2063      1.1  christos 	     because we iterate over every FDR rather than just ones
   2064      1.1  christos 	     with a base address less than or equal to 'offset'.  */
   2065      1.1  christos 	  bfd_signed_vma dist = -1, min_dist = -1;
   2066      1.1  christos 	  char *pdr_hold;
   2067      1.1  christos 	  char *pdr_end;
   2068      1.1  christos 
   2069      1.1  christos 	  fdr_ptr = tab[i].fdr;
   2070      1.1  christos 
   2071  1.1.1.2  christos 	  pdr_ptr = ((char *) debug_info->external_pdr
   2072      1.1  christos 		     + fdr_ptr->ipdFirst * external_pdr_size);
   2073      1.1  christos 	  pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
   2074      1.1  christos 	  (*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr);
   2075      1.1  christos 	  /* Find PDR that is closest to OFFSET.  If pdr.prof is set,
   2076      1.1  christos 	     the procedure entry-point *may* be 0x10 below pdr.adr.  We
   2077      1.1  christos 	     simply pretend that pdr.prof *implies* a lower entry-point.
   2078      1.1  christos 	     This is safe because it just means that may identify 4 NOPs
   2079      1.1  christos 	     in front of the function as belonging to the function.  */
   2080  1.1.1.2  christos 	  for (pdr_hold = NULL;
   2081      1.1  christos 	       pdr_ptr < pdr_end;
   2082      1.1  christos 	       (pdr_ptr += external_pdr_size,
   2083      1.1  christos 		(*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr)))
   2084      1.1  christos 	    {
   2085      1.1  christos 	      if (offset >= (pdr.adr - 0x10 * pdr.prof))
   2086      1.1  christos 		{
   2087  1.1.1.6  christos 		  dist = offset - (pdr.adr - 0x10 * pdr.prof);
   2088      1.1  christos 
   2089      1.1  christos 		  /* eraxxon: 'dist' can be negative now.  Note that
   2090      1.1  christos 		     'min_dist' can be negative if 'pdr_hold' below is NULL.  */
   2091      1.1  christos 		  if (!pdr_hold || (dist >= 0 && dist < min_dist))
   2092      1.1  christos 		    {
   2093      1.1  christos 		      min_dist = dist;
   2094      1.1  christos 		      pdr_hold = pdr_ptr;
   2095      1.1  christos 		    }
   2096      1.1  christos 		}
   2097      1.1  christos 	    }
   2098  1.1.1.2  christos 
   2099      1.1  christos 	  if (!best_pdr || (min_dist >= 0 && min_dist < best_dist))
   2100      1.1  christos 	    {
   2101      1.1  christos 	      best_dist = (bfd_vma) min_dist;
   2102      1.1  christos 	      best_fdr = fdr_ptr;
   2103      1.1  christos 	      best_pdr = pdr_hold;
   2104      1.1  christos 	    }
   2105      1.1  christos 	  /* Continue looping until base_addr of next entry is different.  */
   2106      1.1  christos 	}
   2107      1.1  christos       /* eraxxon: We want to iterate over all FDRs.
   2108      1.1  christos 	 See previous comment about 'fdrtab_lookup'.  */
   2109      1.1  christos       while (++i < line_info->fdrtab_len);
   2110      1.1  christos 
   2111      1.1  christos       if (!best_fdr || !best_pdr)
   2112      1.1  christos 	return FALSE;			/* Shouldn't happen...  */
   2113      1.1  christos 
   2114  1.1.1.2  christos       /* Phew, finally we got something that we can hold onto.  */
   2115      1.1  christos       fdr_ptr = best_fdr;
   2116  1.1.1.6  christos       pdr_ptr = best_pdr;
   2117  1.1.1.6  christos       (*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr);
   2118  1.1.1.6  christos       /* Now we can look for the actual line number.  The line numbers
   2119      1.1  christos 	 are stored in a very funky format, which I won't try to
   2120      1.1  christos 	 describe.  The search is bounded by the end of the FDRs line
   2121      1.1  christos 	 number entries.  */
   2122      1.1  christos       line_end = debug_info->line + fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
   2123      1.1  christos 
   2124      1.1  christos       /* Make offset relative to procedure entry.  */
   2125      1.1  christos       offset -= pdr.adr - 0x10 * pdr.prof;
   2126      1.1  christos       lineno = pdr.lnLow;
   2127      1.1  christos       line_ptr = debug_info->line + fdr_ptr->cbLineOffset + pdr.cbLineOffset;
   2128      1.1  christos       while (line_ptr < line_end)
   2129      1.1  christos 	{
   2130      1.1  christos 	  int delta;
   2131      1.1  christos 	  unsigned int count;
   2132      1.1  christos 
   2133      1.1  christos 	  delta = *line_ptr >> 4;
   2134      1.1  christos 	  if (delta >= 0x8)
   2135      1.1  christos 	    delta -= 0x10;
   2136      1.1  christos 	  count = (*line_ptr & 0xf) + 1;
   2137      1.1  christos 	  ++line_ptr;
   2138      1.1  christos 	  if (delta == -8)
   2139      1.1  christos 	    {
   2140      1.1  christos 	      delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
   2141      1.1  christos 	      if (delta >= 0x8000)
   2142      1.1  christos 		delta -= 0x10000;
   2143      1.1  christos 	      line_ptr += 2;
   2144      1.1  christos 	    }
   2145      1.1  christos 	  lineno += delta;
   2146      1.1  christos 	  if (offset < count * 4)
   2147      1.1  christos 	    {
   2148      1.1  christos 	      line_info->cache.stop += count * 4 - offset;
   2149      1.1  christos 	      break;
   2150      1.1  christos 	    }
   2151      1.1  christos 	  offset -= count * 4;
   2152  1.1.1.6  christos 	}
   2153      1.1  christos 
   2154      1.1  christos       /* If fdr_ptr->rss is -1, then this file does not have full
   2155      1.1  christos 	 symbols, at least according to gdb/mipsread.c.  */
   2156      1.1  christos       if (fdr_ptr->rss == -1)
   2157      1.1  christos 	{
   2158      1.1  christos 	  line_info->cache.filename = NULL;
   2159      1.1  christos 	  if (pdr.isym == -1)
   2160      1.1  christos 	    line_info->cache.functionname = NULL;
   2161      1.1  christos 	  else
   2162      1.1  christos 	    {
   2163      1.1  christos 	      EXTR proc_ext;
   2164      1.1  christos 
   2165      1.1  christos 	      (*debug_swap->swap_ext_in)
   2166      1.1  christos 		(abfd,
   2167      1.1  christos 		 ((char *) debug_info->external_ext
   2168      1.1  christos 		  + pdr.isym * debug_swap->external_ext_size),
   2169      1.1  christos 		 &proc_ext);
   2170      1.1  christos 	      line_info->cache.functionname = (debug_info->ssext
   2171      1.1  christos 					       + proc_ext.asym.iss);
   2172      1.1  christos 	    }
   2173      1.1  christos 	}
   2174      1.1  christos       else
   2175      1.1  christos 	{
   2176      1.1  christos 	  SYMR proc_sym;
   2177      1.1  christos 
   2178      1.1  christos 	  line_info->cache.filename = (debug_info->ss
   2179      1.1  christos 				       + fdr_ptr->issBase
   2180      1.1  christos 				       + fdr_ptr->rss);
   2181      1.1  christos 	  (*debug_swap->swap_sym_in)
   2182      1.1  christos 	    (abfd,
   2183      1.1  christos 	     ((char *) debug_info->external_sym
   2184      1.1  christos 	      + ((fdr_ptr->isymBase + pdr.isym)
   2185      1.1  christos 		 * debug_swap->external_sym_size)),
   2186      1.1  christos 	     &proc_sym);
   2187      1.1  christos 	  line_info->cache.functionname = (debug_info->ss
   2188      1.1  christos 					   + fdr_ptr->issBase
   2189      1.1  christos 					   + proc_sym.iss);
   2190      1.1  christos 	}
   2191      1.1  christos       if (lineno == ilineNil)
   2192      1.1  christos 	lineno = 0;
   2193      1.1  christos       line_info->cache.line_num = lineno;
   2194      1.1  christos     }
   2195      1.1  christos   else
   2196      1.1  christos     {
   2197      1.1  christos       bfd_size_type external_sym_size;
   2198      1.1  christos       const char *directory_name;
   2199      1.1  christos       const char *main_file_name;
   2200      1.1  christos       const char *current_file_name;
   2201      1.1  christos       const char *function_name;
   2202      1.1  christos       const char *line_file_name;
   2203      1.1  christos       bfd_vma low_func_vma;
   2204      1.1  christos       bfd_vma low_line_vma;
   2205      1.1  christos       bfd_boolean past_line;
   2206      1.1  christos       bfd_boolean past_fn;
   2207      1.1  christos       char *sym_ptr, *sym_ptr_end;
   2208      1.1  christos       size_t len, funclen;
   2209      1.1  christos       char *buffer = NULL;
   2210      1.1  christos 
   2211      1.1  christos       /* This file uses stabs debugging information.  When gcc is not
   2212      1.1  christos 	 optimizing, it will put the line number information before
   2213      1.1  christos 	 the function name stabs entry.  When gcc is optimizing, it
   2214      1.1  christos 	 will put the stabs entry for all the function first, followed
   2215      1.1  christos 	 by the line number information.  (This appears to happen
   2216      1.1  christos 	 because of the two output files used by the -mgpopt switch,
   2217      1.1  christos 	 which is implied by -O).  This means that we must keep
   2218      1.1  christos 	 looking through the symbols until we find both a line number
   2219      1.1  christos 	 and a function name which are beyond the address we want.  */
   2220      1.1  christos 
   2221      1.1  christos       line_info->cache.filename = NULL;
   2222      1.1  christos       line_info->cache.functionname = NULL;
   2223      1.1  christos       line_info->cache.line_num = 0;
   2224      1.1  christos 
   2225      1.1  christos       directory_name = NULL;
   2226      1.1  christos       main_file_name = NULL;
   2227      1.1  christos       current_file_name = NULL;
   2228      1.1  christos       function_name = NULL;
   2229      1.1  christos       line_file_name = NULL;
   2230      1.1  christos       low_func_vma = 0;
   2231      1.1  christos       low_line_vma = 0;
   2232      1.1  christos       past_line = FALSE;
   2233      1.1  christos       past_fn = FALSE;
   2234      1.1  christos 
   2235      1.1  christos       external_sym_size = debug_swap->external_sym_size;
   2236      1.1  christos 
   2237      1.1  christos       sym_ptr = ((char *) debug_info->external_sym
   2238      1.1  christos 		 + (fdr_ptr->isymBase + 2) * external_sym_size);
   2239      1.1  christos       sym_ptr_end = sym_ptr + (fdr_ptr->csym - 2) * external_sym_size;
   2240      1.1  christos       for (;
   2241      1.1  christos 	   sym_ptr < sym_ptr_end && (! past_line || ! past_fn);
   2242      1.1  christos 	   sym_ptr += external_sym_size)
   2243      1.1  christos 	{
   2244      1.1  christos 	  SYMR sym;
   2245      1.1  christos 
   2246      1.1  christos 	  (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
   2247      1.1  christos 
   2248      1.1  christos 	  if (ECOFF_IS_STAB (&sym))
   2249      1.1  christos 	    {
   2250      1.1  christos 	      switch (ECOFF_UNMARK_STAB (sym.index))
   2251      1.1  christos 		{
   2252      1.1  christos 		case N_SO:
   2253      1.1  christos 		  main_file_name = current_file_name =
   2254  1.1.1.6  christos 		    debug_info->ss + fdr_ptr->issBase + sym.iss;
   2255      1.1  christos 
   2256      1.1  christos 		  /* Check the next symbol to see if it is also an
   2257      1.1  christos 		     N_SO symbol.  */
   2258      1.1  christos 		  if (sym_ptr + external_sym_size < sym_ptr_end)
   2259      1.1  christos 		    {
   2260      1.1  christos 		      SYMR nextsym;
   2261      1.1  christos 
   2262      1.1  christos 		      (*debug_swap->swap_sym_in) (abfd,
   2263      1.1  christos 						  sym_ptr + external_sym_size,
   2264      1.1  christos 						  &nextsym);
   2265  1.1.1.6  christos 		      if (ECOFF_IS_STAB (&nextsym)
   2266      1.1  christos 			  && ECOFF_UNMARK_STAB (nextsym.index) == N_SO)
   2267      1.1  christos 			{
   2268      1.1  christos 			  directory_name = current_file_name;
   2269      1.1  christos 			  main_file_name = current_file_name =
   2270      1.1  christos 			    debug_info->ss + fdr_ptr->issBase + nextsym.iss;
   2271      1.1  christos 			  sym_ptr += external_sym_size;
   2272      1.1  christos 			}
   2273      1.1  christos 		    }
   2274      1.1  christos 		  break;
   2275      1.1  christos 
   2276      1.1  christos 		case N_SOL:
   2277      1.1  christos 		  current_file_name =
   2278      1.1  christos 		    debug_info->ss + fdr_ptr->issBase + sym.iss;
   2279      1.1  christos 		  break;
   2280      1.1  christos 
   2281      1.1  christos 		case N_FUN:
   2282      1.1  christos 		  if (sym.value > offset)
   2283      1.1  christos 		    past_fn = TRUE;
   2284      1.1  christos 		  else if (sym.value >= low_func_vma)
   2285      1.1  christos 		    {
   2286      1.1  christos 		      low_func_vma = sym.value;
   2287      1.1  christos 		      function_name =
   2288      1.1  christos 			debug_info->ss + fdr_ptr->issBase + sym.iss;
   2289      1.1  christos 		    }
   2290      1.1  christos 		  break;
   2291      1.1  christos 		}
   2292      1.1  christos 	    }
   2293      1.1  christos 	  else if (sym.st == stLabel && sym.index != indexNil)
   2294      1.1  christos 	    {
   2295      1.1  christos 	      if (sym.value > offset)
   2296      1.1  christos 		past_line = TRUE;
   2297      1.1  christos 	      else if (sym.value >= low_line_vma)
   2298      1.1  christos 		{
   2299      1.1  christos 		  low_line_vma = sym.value;
   2300      1.1  christos 		  line_file_name = current_file_name;
   2301      1.1  christos 		  line_info->cache.line_num = sym.index;
   2302      1.1  christos 		}
   2303      1.1  christos 	    }
   2304      1.1  christos 	}
   2305      1.1  christos 
   2306      1.1  christos       if (line_info->cache.line_num != 0)
   2307  1.1.1.6  christos 	main_file_name = line_file_name;
   2308  1.1.1.6  christos 
   2309      1.1  christos       /* We need to remove the stuff after the colon in the function
   2310      1.1  christos 	 name.  We also need to put the directory name and the file
   2311      1.1  christos 	 name together.  */
   2312      1.1  christos       if (function_name == NULL)
   2313      1.1  christos 	len = funclen = 0;
   2314      1.1  christos       else
   2315      1.1  christos 	len = funclen = strlen (function_name) + 1;
   2316      1.1  christos 
   2317      1.1  christos       if (main_file_name != NULL
   2318      1.1  christos 	  && directory_name != NULL
   2319      1.1  christos 	  && main_file_name[0] != '/')
   2320      1.1  christos 	len += strlen (directory_name) + strlen (main_file_name) + 1;
   2321  1.1.1.7  christos 
   2322      1.1  christos       if (len != 0)
   2323  1.1.1.7  christos 	{
   2324      1.1  christos 	  free (line_info->find_buffer);
   2325      1.1  christos 	  buffer = (char *) bfd_malloc ((bfd_size_type) len);
   2326      1.1  christos 	  line_info->find_buffer = buffer;
   2327      1.1  christos 	  if (buffer == NULL)
   2328      1.1  christos 	    return FALSE;
   2329      1.1  christos 	}
   2330      1.1  christos 
   2331      1.1  christos       if (function_name != NULL)
   2332      1.1  christos 	{
   2333      1.1  christos 	  char *colon;
   2334      1.1  christos 
   2335      1.1  christos 	  strcpy (buffer, function_name);
   2336      1.1  christos 	  colon = strchr (buffer, ':');
   2337      1.1  christos 	  if (colon != NULL)
   2338      1.1  christos 	    *colon = '\0';
   2339      1.1  christos 	  line_info->cache.functionname = buffer;
   2340      1.1  christos 	}
   2341      1.1  christos 
   2342      1.1  christos       if (main_file_name != NULL)
   2343      1.1  christos 	{
   2344      1.1  christos 	  if (directory_name == NULL || main_file_name[0] == '/')
   2345      1.1  christos 	    line_info->cache.filename = main_file_name;
   2346      1.1  christos 	  else
   2347      1.1  christos 	    {
   2348      1.1  christos 	      sprintf (buffer + funclen, "%s%s", directory_name,
   2349      1.1  christos 		       main_file_name);
   2350      1.1  christos 	      line_info->cache.filename = buffer + funclen;
   2351      1.1  christos 	    }
   2352      1.1  christos 	}
   2353      1.1  christos     }
   2354      1.1  christos 
   2355      1.1  christos   return TRUE;
   2356      1.1  christos }
   2357      1.1  christos 
   2358  1.1.1.2  christos /* Do the work of find_nearest_line.  */
   2359  1.1.1.2  christos 
   2360  1.1.1.2  christos bfd_boolean
   2361  1.1.1.2  christos _bfd_ecoff_locate_line (bfd *abfd,
   2362  1.1.1.2  christos 			asection *section,
   2363  1.1.1.2  christos 			bfd_vma offset,
   2364  1.1.1.2  christos 			struct ecoff_debug_info * const debug_info,
   2365  1.1.1.2  christos 			const struct ecoff_debug_swap * const debug_swap,
   2366  1.1.1.2  christos 			struct ecoff_find_line *line_info,
   2367      1.1  christos 			const char **filename_ptr,
   2368      1.1  christos 			const char **functionname_ptr,
   2369      1.1  christos 			unsigned int *retline_ptr)
   2370      1.1  christos {
   2371      1.1  christos   offset += section->vma;
   2372      1.1  christos 
   2373      1.1  christos   if (line_info->cache.sect == NULL
   2374      1.1  christos       || line_info->cache.sect != section
   2375      1.1  christos       || offset < line_info->cache.start
   2376      1.1  christos       || offset >= line_info->cache.stop)
   2377      1.1  christos     {
   2378      1.1  christos       line_info->cache.sect = section;
   2379      1.1  christos       line_info->cache.start = offset;
   2380      1.1  christos       line_info->cache.stop = offset;
   2381      1.1  christos       if (! lookup_line (abfd, debug_info, debug_swap, line_info))
   2382      1.1  christos 	{
   2383      1.1  christos 	  line_info->cache.sect = NULL;
   2384      1.1  christos 	  return FALSE;
   2385      1.1  christos 	}
   2386      1.1  christos     }
   2387      1.1  christos 
   2388      1.1  christos   *filename_ptr = line_info->cache.filename;
   2389      1.1  christos   *functionname_ptr = line_info->cache.functionname;
   2390      1.1  christos   *retline_ptr = line_info->cache.line_num;
   2391      1.1  christos 
   2392      1.1  christos   return TRUE;
   2393      1.1  christos }
   2394      1.1  christos 
   2395      1.1  christos /* These routines copy symbolic information into a memory buffer.
   2397      1.1  christos 
   2398      1.1  christos    FIXME: The whole point of the shuffle code is to avoid storing
   2399      1.1  christos    everything in memory, since the linker is such a memory hog.  This
   2400      1.1  christos    code makes that effort useless.  It is only called by the MIPS ELF
   2401      1.1  christos    code when generating a shared library, so it is not that big a
   2402      1.1  christos    deal, but it should be fixed eventually.  */
   2403  1.1.1.2  christos 
   2404      1.1  christos /* Collect a shuffle into a memory buffer.  */
   2405      1.1  christos 
   2406      1.1  christos static bfd_boolean
   2407      1.1  christos ecoff_collect_shuffle (struct shuffle *l, bfd_byte *buff)
   2408      1.1  christos {
   2409      1.1  christos   unsigned long total;
   2410      1.1  christos 
   2411      1.1  christos   total = 0;
   2412      1.1  christos   for (; l != (struct shuffle *) NULL; l = l->next)
   2413      1.1  christos     {
   2414      1.1  christos       if (! l->filep)
   2415      1.1  christos 	memcpy (buff, l->u.memory, l->size);
   2416      1.1  christos       else
   2417      1.1  christos 	{
   2418      1.1  christos 	  if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
   2419      1.1  christos 	      || (bfd_bread (buff, (bfd_size_type) l->size, l->u.file.input_bfd)
   2420      1.1  christos 		  != l->size))
   2421      1.1  christos 	    return FALSE;
   2422      1.1  christos 	}
   2423      1.1  christos       total += l->size;
   2424      1.1  christos       buff += l->size;
   2425      1.1  christos     }
   2426      1.1  christos 
   2427      1.1  christos   return TRUE;
   2428      1.1  christos }
   2429  1.1.1.2  christos 
   2430  1.1.1.2  christos /* Copy PDR information into a memory buffer.  */
   2431      1.1  christos 
   2432      1.1  christos bfd_boolean
   2433      1.1  christos _bfd_ecoff_get_accumulated_pdr (void * handle,
   2434      1.1  christos 				bfd_byte *buff)
   2435      1.1  christos {
   2436      1.1  christos   struct accumulate *ainfo = (struct accumulate *) handle;
   2437      1.1  christos 
   2438      1.1  christos   return ecoff_collect_shuffle (ainfo->pdr, buff);
   2439      1.1  christos }
   2440  1.1.1.2  christos 
   2441      1.1  christos /* Copy symbol information into a memory buffer.  */
   2442      1.1  christos 
   2443      1.1  christos bfd_boolean
   2444      1.1  christos _bfd_ecoff_get_accumulated_sym (void * handle, bfd_byte *buff)
   2445      1.1  christos {
   2446      1.1  christos   struct accumulate *ainfo = (struct accumulate *) handle;
   2447      1.1  christos 
   2448      1.1  christos   return ecoff_collect_shuffle (ainfo->sym, buff);
   2449      1.1  christos }
   2450  1.1.1.2  christos 
   2451      1.1  christos /* Copy the string table into a memory buffer.  */
   2452      1.1  christos 
   2453      1.1  christos bfd_boolean
   2454      1.1  christos _bfd_ecoff_get_accumulated_ss (void * handle, bfd_byte *buff)
   2455      1.1  christos {
   2456      1.1  christos   struct accumulate *ainfo = (struct accumulate *) handle;
   2457      1.1  christos   struct string_hash_entry *sh;
   2458      1.1  christos   unsigned long total;
   2459      1.1  christos 
   2460      1.1  christos   /* The string table is written out from the hash table if this is a
   2461      1.1  christos      final link.  */
   2462      1.1  christos   BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
   2463      1.1  christos   *buff++ = '\0';
   2464      1.1  christos   total = 1;
   2465      1.1  christos   BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
   2466      1.1  christos   for (sh = ainfo->ss_hash;
   2467      1.1  christos        sh != (struct string_hash_entry *) NULL;
   2468      1.1  christos        sh = sh->next)
   2469  1.1.1.2  christos     {
   2470      1.1  christos       size_t len;
   2471      1.1  christos 
   2472      1.1  christos       len = strlen (sh->root.string);
   2473      1.1  christos       memcpy (buff, sh->root.string, len + 1);
   2474      1.1  christos       total += len + 1;
   2475      1.1  christos       buff += len + 1;
   2476                        }
   2477                    
   2478                      return TRUE;
   2479                    }
   2480