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