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