Home | History | Annotate | Line # | Download | only in bfd
ecofflink.c revision 1.1.1.7
      1 /* Routines to link ECOFF debugging information.
      2    Copyright (C) 1993-2020 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 bfd_boolean
    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   bfd_boolean 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 bfd_boolean
    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 bfd_boolean
    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   bfd_size_type 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 bfd_boolean
    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       bfd_boolean 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 	      bfd_boolean 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 bfd_boolean
   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 			      input_bfd->filename);
   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 bfd_boolean
   1205 bfd_ecoff_debug_externals (bfd *abfd,
   1206 			   struct ecoff_debug_info *debug,
   1207 			   const struct ecoff_debug_swap *swap,
   1208 			   bfd_boolean relocatable,
   1209 			   bfd_boolean (*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 bfd_boolean
   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 bfd_boolean
   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_bwrite (buff, swap->external_hdr_size, abfd)
   1466       != swap->external_hdr_size)
   1467     goto error_return;
   1468 
   1469   if (buff != NULL)
   1470     free (buff);
   1471   return TRUE;
   1472  error_return:
   1473   if (buff != NULL)
   1474     free (buff);
   1475   return FALSE;
   1476 }
   1477 
   1478 /* Write out the ECOFF debugging information.  This function assumes
   1479    that the information (the pointers and counts) in *DEBUG have been
   1480    set correctly.  WHERE is the position in the file to write the
   1481    information to.  This function fills in the file offsets in the
   1482    symbolic header.  */
   1483 
   1484 bfd_boolean
   1485 bfd_ecoff_write_debug (bfd *abfd,
   1486 		       struct ecoff_debug_info *debug,
   1487 		       const struct ecoff_debug_swap *swap,
   1488 		       file_ptr where)
   1489 {
   1490   HDRR * const symhdr = &debug->symbolic_header;
   1491 
   1492   if (! ecoff_write_symhdr (abfd, debug, swap, where))
   1493     return FALSE;
   1494 
   1495 #define WRITE(ptr, count, size, offset) \
   1496   BFD_ASSERT (symhdr->offset == 0 \
   1497 	      || (bfd_vma) bfd_tell (abfd) == symhdr->offset); \
   1498   if (bfd_bwrite (debug->ptr, (bfd_size_type) size * symhdr->count, abfd)\
   1499       != size * symhdr->count) \
   1500     return FALSE;
   1501 
   1502   WRITE (line, cbLine, sizeof (unsigned char), cbLineOffset);
   1503   WRITE (external_dnr, idnMax, swap->external_dnr_size, cbDnOffset);
   1504   WRITE (external_pdr, ipdMax, swap->external_pdr_size, cbPdOffset);
   1505   WRITE (external_sym, isymMax, swap->external_sym_size, cbSymOffset);
   1506   WRITE (external_opt, ioptMax, swap->external_opt_size, cbOptOffset);
   1507   WRITE (external_aux, iauxMax, (bfd_size_type) sizeof (union aux_ext),
   1508 	 cbAuxOffset);
   1509   WRITE (ss, issMax, sizeof (char), cbSsOffset);
   1510   WRITE (ssext, issExtMax, sizeof (char), cbSsExtOffset);
   1511   WRITE (external_fdr, ifdMax, swap->external_fdr_size, cbFdOffset);
   1512   WRITE (external_rfd, crfd, swap->external_rfd_size, cbRfdOffset);
   1513   WRITE (external_ext, iextMax, swap->external_ext_size, cbExtOffset);
   1514 #undef WRITE
   1515 
   1516   return TRUE;
   1517 }
   1518 
   1519 /* Write out a shuffle list.  */
   1520 
   1521 
   1522 static bfd_boolean
   1523 ecoff_write_shuffle (bfd *abfd,
   1524 		     const struct ecoff_debug_swap *swap,
   1525 		     struct shuffle *shuffle,
   1526 		     void * space)
   1527 {
   1528   struct shuffle *l;
   1529   unsigned long total;
   1530 
   1531   total = 0;
   1532   for (l = shuffle; l != (struct shuffle *) NULL; l = l->next)
   1533     {
   1534       if (! l->filep)
   1535 	{
   1536 	  if (bfd_bwrite (l->u.memory, (bfd_size_type) l->size, abfd)
   1537 	      != l->size)
   1538 	    return FALSE;
   1539 	}
   1540       else
   1541 	{
   1542 	  if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
   1543 	      || bfd_bread (space, (bfd_size_type) l->size,
   1544 			   l->u.file.input_bfd) != l->size
   1545 	      || bfd_bwrite (space, (bfd_size_type) l->size, abfd) != l->size)
   1546 	    return FALSE;
   1547 	}
   1548       total += l->size;
   1549     }
   1550 
   1551   if ((total & (swap->debug_align - 1)) != 0)
   1552     {
   1553       unsigned int i;
   1554       bfd_byte *s;
   1555 
   1556       i = swap->debug_align - (total & (swap->debug_align - 1));
   1557       s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
   1558       if (s == NULL && i != 0)
   1559 	return FALSE;
   1560 
   1561       if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
   1562 	{
   1563 	  free (s);
   1564 	  return FALSE;
   1565 	}
   1566       free (s);
   1567     }
   1568 
   1569   return TRUE;
   1570 }
   1571 
   1572 /* Write out debugging information using accumulated linker
   1573    information.  */
   1574 
   1575 bfd_boolean
   1576 bfd_ecoff_write_accumulated_debug (void * handle,
   1577 				   bfd *abfd,
   1578 				   struct ecoff_debug_info *debug,
   1579 				   const struct ecoff_debug_swap *swap,
   1580 				   struct bfd_link_info *info,
   1581 				   file_ptr where)
   1582 {
   1583   struct accumulate *ainfo = (struct accumulate *) handle;
   1584   void * space = NULL;
   1585   bfd_size_type amt;
   1586 
   1587   if (! ecoff_write_symhdr (abfd, debug, swap, where))
   1588     goto error_return;
   1589 
   1590   amt = ainfo->largest_file_shuffle;
   1591   space = bfd_malloc (amt);
   1592   if (space == NULL && ainfo->largest_file_shuffle != 0)
   1593     goto error_return;
   1594 
   1595   if (! ecoff_write_shuffle (abfd, swap, ainfo->line, space)
   1596       || ! ecoff_write_shuffle (abfd, swap, ainfo->pdr, space)
   1597       || ! ecoff_write_shuffle (abfd, swap, ainfo->sym, space)
   1598       || ! ecoff_write_shuffle (abfd, swap, ainfo->opt, space)
   1599       || ! ecoff_write_shuffle (abfd, swap, ainfo->aux, space))
   1600     goto error_return;
   1601 
   1602   /* The string table is written out from the hash table if this is a
   1603      final link.  */
   1604   if (bfd_link_relocatable (info))
   1605     {
   1606       BFD_ASSERT (ainfo->ss_hash == (struct string_hash_entry *) NULL);
   1607       if (! ecoff_write_shuffle (abfd, swap, ainfo->ss, space))
   1608 	goto error_return;
   1609     }
   1610   else
   1611     {
   1612       unsigned long total;
   1613       bfd_byte null;
   1614       struct string_hash_entry *sh;
   1615 
   1616       BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
   1617       null = 0;
   1618       if (bfd_bwrite (&null, (bfd_size_type) 1, abfd) != 1)
   1619 	goto error_return;
   1620       total = 1;
   1621       BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
   1622       for (sh = ainfo->ss_hash;
   1623 	   sh != (struct string_hash_entry *) NULL;
   1624 	   sh = sh->next)
   1625 	{
   1626 	  size_t len;
   1627 
   1628 	  len = strlen (sh->root.string);
   1629 	  amt = len + 1;
   1630 	  if (bfd_bwrite (sh->root.string, amt, abfd) != amt)
   1631 	    goto error_return;
   1632 	  total += len + 1;
   1633 	}
   1634 
   1635       if ((total & (swap->debug_align - 1)) != 0)
   1636 	{
   1637 	  unsigned int i;
   1638 	  bfd_byte *s;
   1639 
   1640 	  i = swap->debug_align - (total & (swap->debug_align - 1));
   1641 	  s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
   1642 	  if (s == NULL && i != 0)
   1643 	    goto error_return;
   1644 
   1645 	  if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
   1646 	    {
   1647 	      free (s);
   1648 	      goto error_return;
   1649 	    }
   1650 	  free (s);
   1651 	}
   1652     }
   1653 
   1654   /* The external strings and symbol are not converted over to using
   1655      shuffles.  FIXME: They probably should be.  */
   1656   amt = debug->symbolic_header.issExtMax;
   1657   if (bfd_bwrite (debug->ssext, amt, abfd) != amt)
   1658     goto error_return;
   1659   if ((debug->symbolic_header.issExtMax & (swap->debug_align - 1)) != 0)
   1660     {
   1661       unsigned int i;
   1662       bfd_byte *s;
   1663 
   1664       i = (swap->debug_align
   1665 	   - (debug->symbolic_header.issExtMax & (swap->debug_align - 1)));
   1666       s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
   1667       if (s == NULL && i != 0)
   1668 	goto error_return;
   1669 
   1670       if (bfd_bwrite (s, (bfd_size_type) i, abfd) != i)
   1671 	{
   1672 	  free (s);
   1673 	  goto error_return;
   1674 	}
   1675       free (s);
   1676     }
   1677 
   1678   if (! ecoff_write_shuffle (abfd, swap, ainfo->fdr, space)
   1679       || ! ecoff_write_shuffle (abfd, swap, ainfo->rfd, space))
   1680     goto error_return;
   1681 
   1682   BFD_ASSERT (debug->symbolic_header.cbExtOffset == 0
   1683 	      || (debug->symbolic_header.cbExtOffset
   1684 		  == (bfd_vma) bfd_tell (abfd)));
   1685 
   1686   amt = debug->symbolic_header.iextMax * swap->external_ext_size;
   1687   if (bfd_bwrite (debug->external_ext, amt, abfd) != amt)
   1688     goto error_return;
   1689 
   1690   if (space != NULL)
   1691     free (space);
   1692   return TRUE;
   1693 
   1694  error_return:
   1695   if (space != NULL)
   1696     free (space);
   1697   return FALSE;
   1698 }
   1699 
   1700 /* Handle the find_nearest_line function for both ECOFF and MIPS ELF
   1702    files.  */
   1703 
   1704 /* Compare FDR entries.  This is called via qsort.  */
   1705 
   1706 static int
   1707 cmp_fdrtab_entry (const void * leftp, const void * rightp)
   1708 {
   1709   const struct ecoff_fdrtab_entry *lp =
   1710     (const struct ecoff_fdrtab_entry *) leftp;
   1711   const struct ecoff_fdrtab_entry *rp =
   1712     (const struct ecoff_fdrtab_entry *) rightp;
   1713 
   1714   if (lp->base_addr < rp->base_addr)
   1715     return -1;
   1716   if (lp->base_addr > rp->base_addr)
   1717     return 1;
   1718   return 0;
   1719 }
   1720 
   1721 /* Each file descriptor (FDR) has a memory address, to simplify
   1722    looking up an FDR by address, we build a table covering all FDRs
   1723    that have a least one procedure descriptor in them.  The final
   1724    table will be sorted by address so we can look it up via binary
   1725    search.  */
   1726 
   1727 static bfd_boolean
   1728 mk_fdrtab (bfd *abfd,
   1729 	   struct ecoff_debug_info * const debug_info,
   1730 	   const struct ecoff_debug_swap * const debug_swap,
   1731 	   struct ecoff_find_line *line_info)
   1732 {
   1733   struct ecoff_fdrtab_entry *tab;
   1734   FDR *fdr_ptr;
   1735   FDR *fdr_start;
   1736   FDR *fdr_end;
   1737   bfd_boolean stabs;
   1738   long len;
   1739   bfd_size_type amt;
   1740 
   1741   fdr_start = debug_info->fdr;
   1742   fdr_end = fdr_start + debug_info->symbolic_header.ifdMax;
   1743 
   1744   /* First, let's see how long the table needs to be.  */
   1745   for (len = 0, fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
   1746     {
   1747       if (fdr_ptr->cpd == 0)	/* Skip FDRs that have no PDRs.  */
   1748 	continue;
   1749       ++len;
   1750     }
   1751 
   1752   /* Now, create and fill in the table.  */
   1753   amt = (bfd_size_type) len * sizeof (struct ecoff_fdrtab_entry);
   1754   line_info->fdrtab = (struct ecoff_fdrtab_entry*) bfd_zalloc (abfd, amt);
   1755   if (line_info->fdrtab == NULL)
   1756     return FALSE;
   1757   line_info->fdrtab_len = len;
   1758 
   1759   tab = line_info->fdrtab;
   1760   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
   1761     {
   1762       if (fdr_ptr->cpd == 0)
   1763 	continue;
   1764 
   1765       /* Check whether this file has stabs debugging information.  In
   1766 	 a file with stabs debugging information, the second local
   1767 	 symbol is named @stabs.  */
   1768       stabs = FALSE;
   1769       if (fdr_ptr->csym >= 2)
   1770 	{
   1771 	  char *sym_ptr;
   1772 	  SYMR sym;
   1773 
   1774 	  sym_ptr = ((char *) debug_info->external_sym
   1775 		     + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
   1776 	  (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
   1777 	  if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
   1778 		      STABS_SYMBOL) == 0)
   1779 	    stabs = TRUE;
   1780 	}
   1781 
   1782       if (!stabs)
   1783 	{
   1784 	  /* eraxxon: There are at least two problems with this computation:
   1785 	     1) PDRs do *not* contain offsets but full vma's; and typically the
   1786 	     address of the first PDR is the address of the FDR, which will
   1787 	     make (most) of the results of the original computation 0!
   1788 	     2) Once in a wacky while, the Compaq compiler generated PDR
   1789 	     addresses do not equal the FDR vma, but they (the PDR address)
   1790 	     are still vma's and not offsets.  Cf. comments in
   1791 	     'lookup_line'.  */
   1792 	  /* The address of the first PDR is the offset of that
   1793 	     procedure relative to the beginning of file FDR.  */
   1794 	  tab->base_addr = fdr_ptr->adr;
   1795 	}
   1796       else
   1797 	{
   1798 	  /* XXX I don't know about stabs, so this is a guess
   1799 	     (davidm (at) cs.arizona.edu).  */
   1800 	  tab->base_addr = fdr_ptr->adr;
   1801 	}
   1802       tab->fdr = fdr_ptr;
   1803       ++tab;
   1804     }
   1805 
   1806   /* Finally, the table is sorted in increasing memory-address order.
   1807      The table is mostly sorted already, but there are cases (e.g.,
   1808      static functions in include files), where this does not hold.
   1809      Use "odump -PFv" to verify...  */
   1810   qsort (line_info->fdrtab, (size_t) len,
   1811 	 sizeof (struct ecoff_fdrtab_entry), cmp_fdrtab_entry);
   1812 
   1813   return TRUE;
   1814 }
   1815 
   1816 /* Return index of first FDR that covers to OFFSET.  */
   1817 
   1818 static long
   1819 fdrtab_lookup (struct ecoff_find_line *line_info, bfd_vma offset)
   1820 {
   1821   long low, high, len;
   1822   long mid = -1;
   1823   struct ecoff_fdrtab_entry *tab;
   1824 
   1825   len = line_info->fdrtab_len;
   1826   if (len == 0)
   1827     return -1;
   1828 
   1829   tab = line_info->fdrtab;
   1830   for (low = 0, high = len - 1 ; low != high ;)
   1831     {
   1832       mid = (high + low) / 2;
   1833       if (offset >= tab[mid].base_addr && offset < tab[mid + 1].base_addr)
   1834 	goto find_min;
   1835 
   1836       if (tab[mid].base_addr > offset)
   1837 	high = mid;
   1838       else
   1839 	low = mid + 1;
   1840     }
   1841 
   1842   /* eraxxon: at this point 'offset' is either lower than the lowest entry or
   1843      higher than the highest entry. In the former case high = low = mid = 0;
   1844      we want to return -1.  In the latter case, low = high and mid = low - 1;
   1845      we want to return the index of the highest entry.  Only in former case
   1846      will the following 'catch-all' test be true.  */
   1847   ++mid;
   1848 
   1849   /* Last entry is catch-all for all higher addresses.  */
   1850   if (offset < tab[mid].base_addr)
   1851     return -1;
   1852 
   1853  find_min:
   1854 
   1855   /* eraxxon: There may be multiple FDRs in the table with the
   1856      same base_addr; make sure that we are at the first one.  */
   1857   while (mid > 0 && tab[mid - 1].base_addr == tab[mid].base_addr)
   1858     --mid;
   1859 
   1860   return mid;
   1861 }
   1862 
   1863 /* Look up a line given an address, storing the information in
   1864    LINE_INFO->cache.  */
   1865 
   1866 static bfd_boolean
   1867 lookup_line (bfd *abfd,
   1868 	     struct ecoff_debug_info * const debug_info,
   1869 	     const struct ecoff_debug_swap * const debug_swap,
   1870 	     struct ecoff_find_line *line_info)
   1871 {
   1872   struct ecoff_fdrtab_entry *tab;
   1873   bfd_vma offset;
   1874   bfd_boolean stabs;
   1875   FDR *fdr_ptr;
   1876   int i;
   1877 
   1878   /* eraxxon: note that 'offset' is the full vma, not a section offset.  */
   1879   offset = line_info->cache.start;
   1880 
   1881   /* Build FDR table (sorted by object file's base-address) if we
   1882      don't have it already.  */
   1883   if (line_info->fdrtab == NULL
   1884       && !mk_fdrtab (abfd, debug_info, debug_swap, line_info))
   1885     return FALSE;
   1886 
   1887   tab = line_info->fdrtab;
   1888 
   1889   /* Find first FDR for address OFFSET.  */
   1890   i = fdrtab_lookup (line_info, offset);
   1891   if (i < 0)
   1892     return FALSE;		/* no FDR, no fun...  */
   1893 
   1894   /* eraxxon: 'fdrtab_lookup' doesn't give what we want, at least for Compaq's
   1895      C++ compiler 6.2.  Consider three FDRs with starting addresses of x, y,
   1896      and z, respectively, such that x < y < z.  Assume further that
   1897      y < 'offset' < z.  It is possible at times that the PDR for 'offset' is
   1898      associated with FDR x and *not* with FDR y.  Erg!!
   1899 
   1900      From a binary dump of my C++ test case 'moo' using Compaq's coffobjanl
   1901      (output format has been edited for our purposes):
   1902 
   1903      FDR [2]: (main.C): First instruction: 0x12000207c <x>
   1904        PDR [5] for File [2]: LoopTest__Xv		  <0x1200020a0> (a)
   1905        PDR [7] for File [2]: foo__Xv			  <0x120002168>
   1906      FDR [1]: (-1):	First instruction: 0x1200020e8 <y>
   1907        PDR [3] for File [1]:				  <0x120001ad0> (b)
   1908      FDR [6]: (-1):	First instruction: 0x1200026f0 <z>
   1909 
   1910      (a) In the case of PDR5, the vma is such that the first few instructions
   1911      of the procedure can be found.  But since the size of this procedure is
   1912      160b, the vma will soon cross into the 'address space' of FDR1 and no
   1913      debugging info will be found.  How repugnant!
   1914 
   1915      (b) It is also possible for a PDR to have a *lower* vma than its associated
   1916      FDR; see FDR1 and PDR3.  Gross!
   1917 
   1918      Since the FDRs that are causing so much havok (in this case) 1) do not
   1919      describe actual files (fdr.rss == -1), and 2) contain only compiler
   1920      generated routines, I thought a simple fix would be to exclude them from
   1921      the FDR table in 'mk_fdrtab'.  But, besides not knowing for certain
   1922      whether this would be correct, it creates an additional problem.  If we
   1923      happen to ask for source file info on a compiler generated (procedure)
   1924      symbol -- which is still in the symbol table -- the result can be
   1925      information from a real procedure!  This is because compiler generated
   1926      procedures with vma's higher than the last FDR in the fdr table will be
   1927      associated with a PDR from this FDR, specifically the PDR with the
   1928      highest vma.  This wasn't a problem before, because each procedure had a
   1929      PDR.  (Yes, this problem could be eliminated if we kept the size of the
   1930      last PDR around, but things are already getting ugly).
   1931 
   1932      Probably, a better solution would be to have a sorted PDR table.  Each
   1933      PDR would have a pointer to its FDR so file information could still be
   1934      obtained.  A FDR table could still be constructed if necessary -- since
   1935      it only contains pointers, not much extra memory would be used -- but
   1936      the PDR table would be searched to locate debugging info.
   1937 
   1938      There is still at least one remaining issue.  Sometimes a FDR can have a
   1939      bogus name, but contain PDRs that should belong to another FDR with a
   1940      real name.  E.g:
   1941 
   1942      FDR [3]: 0000000120001b50 (/home/.../Array.H~alt~deccxx_5E5A62AD)
   1943        PDR [a] for File [3]: 0000000120001b50
   1944        PDR [b] for File [3]: 0000000120001cf0
   1945        PDR [c] for File [3]: 0000000120001dc8
   1946        PDR [d] for File [3]: 0000000120001e40
   1947        PDR [e] for File [3]: 0000000120001eb8
   1948        PDR [f] for File [3]: 0000000120001f4c
   1949      FDR [4]: 0000000120001b50 (/home/.../Array.H)
   1950 
   1951      Here, FDR4 has the correct name, but should (seemingly) contain PDRa-f.
   1952      The symbol table for PDR4 does contain symbols for PDRa-f, but so does
   1953      the symbol table for FDR3.  However the former is different; perhaps this
   1954      can be detected easily. (I'm not sure at this point.)  This problem only
   1955      seems to be associated with files with templates.  I am assuming the idea
   1956      is that there is a 'fake' FDR (with PDRs) for each differently typed set
   1957      of templates that must be generated.  Currently, FDR4 is completely
   1958      excluded from the FDR table in 'mk_fdrtab' because it contains no PDRs.
   1959 
   1960      Since I don't have time to prepare a real fix for this right now, be
   1961      prepared for 'A Horrible Hack' to force the inspection of all non-stabs
   1962      FDRs.  It's coming...  */
   1963   fdr_ptr = tab[i].fdr;
   1964 
   1965   /* Check whether this file has stabs debugging information.  In a
   1966      file with stabs debugging information, the second local symbol is
   1967      named @stabs.  */
   1968   stabs = FALSE;
   1969   if (fdr_ptr->csym >= 2)
   1970     {
   1971       char *sym_ptr;
   1972       SYMR sym;
   1973 
   1974       sym_ptr = ((char *) debug_info->external_sym
   1975 		 + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
   1976       (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
   1977       if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
   1978 		  STABS_SYMBOL) == 0)
   1979 	stabs = TRUE;
   1980     }
   1981 
   1982   if (!stabs)
   1983     {
   1984       bfd_size_type external_pdr_size;
   1985       char *pdr_ptr;
   1986       char *best_pdr = NULL;
   1987       FDR *best_fdr;
   1988       bfd_signed_vma best_dist = -1;
   1989       PDR pdr;
   1990       unsigned char *line_ptr;
   1991       unsigned char *line_end;
   1992       int lineno;
   1993       /* This file uses ECOFF debugging information.  Each FDR has a
   1994 	 list of procedure descriptors (PDR).  The address in the FDR
   1995 	 is the absolute address of the first procedure.  The address
   1996 	 in the first PDR gives the offset of that procedure relative
   1997 	 to the object file's base-address.  The addresses in
   1998 	 subsequent PDRs specify each procedure's address relative to
   1999 	 the object file's base-address.  To make things more juicy,
   2000 	 whenever the PROF bit in the PDR is set, the real entry point
   2001 	 of the procedure may be 16 bytes below what would normally be
   2002 	 the procedure's entry point.  Instead, DEC came up with a
   2003 	 wicked scheme to create profiled libraries "on the fly":
   2004 	 instead of shipping a regular and a profiled version of each
   2005 	 library, they insert 16 bytes of unused space in front of
   2006 	 each procedure and set the "prof" bit in the PDR to indicate
   2007 	 that there is a gap there (this is done automagically by "as"
   2008 	 when option "-pg" is specified).  Thus, normally, you link
   2009 	 against such a library and, except for lots of 16 byte gaps
   2010 	 between functions, things will behave as usual.  However,
   2011 	 when invoking "ld" with option "-pg", it will fill those gaps
   2012 	 with code that calls mcount().  It then moves the function's
   2013 	 entry point down by 16 bytes, and out pops a binary that has
   2014 	 all functions profiled.
   2015 
   2016 	 NOTE: Neither FDRs nor PDRs are strictly sorted in memory
   2017 	       order.  For example, when including header-files that
   2018 	       define functions, the FDRs follow behind the including
   2019 	       file, even though their code may have been generated at
   2020 	       a lower address.  File coff-alpha.c from libbfd
   2021 	       illustrates this (use "odump -PFv" to look at a file's
   2022 	       FDR/PDR).  Similarly, PDRs are sometimes out of order
   2023 	       as well.  An example of this is OSF/1 v3.0 libc's
   2024 	       malloc.c.  I'm not sure why this happens, but it could
   2025 	       be due to optimizations that reorder a function's
   2026 	       position within an object-file.
   2027 
   2028 	 Strategy:
   2029 
   2030 	 On the first call to this function, we build a table of FDRs
   2031 	 that is sorted by the base-address of the object-file the FDR
   2032 	 is referring to.  Notice that each object-file may contain
   2033 	 code from multiple source files (e.g., due to code defined in
   2034 	 include files).  Thus, for any given base-address, there may
   2035 	 be multiple FDRs (but this case is, fortunately, uncommon).
   2036 	 lookup(addr) guarantees to return the first FDR that applies
   2037 	 to address ADDR.  Thus, after invoking lookup(), we have a
   2038 	 list of FDRs that may contain the PDR for ADDR.  Next, we
   2039 	 walk through the PDRs of these FDRs and locate the one that
   2040 	 is closest to ADDR (i.e., for which the difference between
   2041 	 ADDR and the PDR's entry point is positive and minimal).
   2042 	 Once, the right FDR and PDR are located, we simply walk
   2043 	 through the line-number table to lookup the line-number that
   2044 	 best matches ADDR.  Obviously, things could be sped up by
   2045 	 keeping a sorted list of PDRs instead of a sorted list of
   2046 	 FDRs.  However, this would increase space requirements
   2047 	 considerably, which is undesirable.  */
   2048       external_pdr_size = debug_swap->external_pdr_size;
   2049 
   2050       /* eraxxon: The Horrible Hack: Because of the problems above, set 'i'
   2051 	 to 0 so we look through all FDRs.
   2052 
   2053 	 Because FDR's without any symbols are assumed to be non-stabs,
   2054 	 searching through all FDRs may cause the following code to try to
   2055 	 read stabs FDRs as ECOFF ones.  However, I don't think this will
   2056 	 harm anything.  */
   2057       i = 0;
   2058 
   2059       /* Search FDR list starting at tab[i] for the PDR that best matches
   2060 	 OFFSET.  Normally, the FDR list is only one entry long.  */
   2061       best_fdr = NULL;
   2062       do
   2063 	{
   2064 	  /* eraxxon: 'dist' and 'min_dist' can be negative now
   2065 	     because we iterate over every FDR rather than just ones
   2066 	     with a base address less than or equal to 'offset'.  */
   2067 	  bfd_signed_vma dist = -1, min_dist = -1;
   2068 	  char *pdr_hold;
   2069 	  char *pdr_end;
   2070 
   2071 	  fdr_ptr = tab[i].fdr;
   2072 
   2073 	  pdr_ptr = ((char *) debug_info->external_pdr
   2074 		     + fdr_ptr->ipdFirst * external_pdr_size);
   2075 	  pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
   2076 	  (*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr);
   2077 	  /* Find PDR that is closest to OFFSET.  If pdr.prof is set,
   2078 	     the procedure entry-point *may* be 0x10 below pdr.adr.  We
   2079 	     simply pretend that pdr.prof *implies* a lower entry-point.
   2080 	     This is safe because it just means that may identify 4 NOPs
   2081 	     in front of the function as belonging to the function.  */
   2082 	  for (pdr_hold = NULL;
   2083 	       pdr_ptr < pdr_end;
   2084 	       (pdr_ptr += external_pdr_size,
   2085 		(*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr)))
   2086 	    {
   2087 	      if (offset >= (pdr.adr - 0x10 * pdr.prof))
   2088 		{
   2089 		  dist = offset - (pdr.adr - 0x10 * pdr.prof);
   2090 
   2091 		  /* eraxxon: 'dist' can be negative now.  Note that
   2092 		     'min_dist' can be negative if 'pdr_hold' below is NULL.  */
   2093 		  if (!pdr_hold || (dist >= 0 && dist < min_dist))
   2094 		    {
   2095 		      min_dist = dist;
   2096 		      pdr_hold = pdr_ptr;
   2097 		    }
   2098 		}
   2099 	    }
   2100 
   2101 	  if (!best_pdr || (min_dist >= 0 && min_dist < best_dist))
   2102 	    {
   2103 	      best_dist = (bfd_vma) min_dist;
   2104 	      best_fdr = fdr_ptr;
   2105 	      best_pdr = pdr_hold;
   2106 	    }
   2107 	  /* Continue looping until base_addr of next entry is different.  */
   2108 	}
   2109       /* eraxxon: We want to iterate over all FDRs.
   2110 	 See previous comment about 'fdrtab_lookup'.  */
   2111       while (++i < line_info->fdrtab_len);
   2112 
   2113       if (!best_fdr || !best_pdr)
   2114 	return FALSE;			/* Shouldn't happen...  */
   2115 
   2116       /* Phew, finally we got something that we can hold onto.  */
   2117       fdr_ptr = best_fdr;
   2118       pdr_ptr = best_pdr;
   2119       (*debug_swap->swap_pdr_in) (abfd, pdr_ptr, &pdr);
   2120       /* Now we can look for the actual line number.  The line numbers
   2121 	 are stored in a very funky format, which I won't try to
   2122 	 describe.  The search is bounded by the end of the FDRs line
   2123 	 number entries.  */
   2124       line_end = debug_info->line + fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
   2125 
   2126       /* Make offset relative to procedure entry.  */
   2127       offset -= pdr.adr - 0x10 * pdr.prof;
   2128       lineno = pdr.lnLow;
   2129       line_ptr = debug_info->line + fdr_ptr->cbLineOffset + pdr.cbLineOffset;
   2130       while (line_ptr < line_end)
   2131 	{
   2132 	  int delta;
   2133 	  unsigned int count;
   2134 
   2135 	  delta = *line_ptr >> 4;
   2136 	  if (delta >= 0x8)
   2137 	    delta -= 0x10;
   2138 	  count = (*line_ptr & 0xf) + 1;
   2139 	  ++line_ptr;
   2140 	  if (delta == -8)
   2141 	    {
   2142 	      delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
   2143 	      if (delta >= 0x8000)
   2144 		delta -= 0x10000;
   2145 	      line_ptr += 2;
   2146 	    }
   2147 	  lineno += delta;
   2148 	  if (offset < count * 4)
   2149 	    {
   2150 	      line_info->cache.stop += count * 4 - offset;
   2151 	      break;
   2152 	    }
   2153 	  offset -= count * 4;
   2154 	}
   2155 
   2156       /* If fdr_ptr->rss is -1, then this file does not have full
   2157 	 symbols, at least according to gdb/mipsread.c.  */
   2158       if (fdr_ptr->rss == -1)
   2159 	{
   2160 	  line_info->cache.filename = NULL;
   2161 	  if (pdr.isym == -1)
   2162 	    line_info->cache.functionname = NULL;
   2163 	  else
   2164 	    {
   2165 	      EXTR proc_ext;
   2166 
   2167 	      (*debug_swap->swap_ext_in)
   2168 		(abfd,
   2169 		 ((char *) debug_info->external_ext
   2170 		  + pdr.isym * debug_swap->external_ext_size),
   2171 		 &proc_ext);
   2172 	      line_info->cache.functionname = (debug_info->ssext
   2173 					       + proc_ext.asym.iss);
   2174 	    }
   2175 	}
   2176       else
   2177 	{
   2178 	  SYMR proc_sym;
   2179 
   2180 	  line_info->cache.filename = (debug_info->ss
   2181 				       + fdr_ptr->issBase
   2182 				       + fdr_ptr->rss);
   2183 	  (*debug_swap->swap_sym_in)
   2184 	    (abfd,
   2185 	     ((char *) debug_info->external_sym
   2186 	      + ((fdr_ptr->isymBase + pdr.isym)
   2187 		 * debug_swap->external_sym_size)),
   2188 	     &proc_sym);
   2189 	  line_info->cache.functionname = (debug_info->ss
   2190 					   + fdr_ptr->issBase
   2191 					   + proc_sym.iss);
   2192 	}
   2193       if (lineno == ilineNil)
   2194 	lineno = 0;
   2195       line_info->cache.line_num = lineno;
   2196     }
   2197   else
   2198     {
   2199       bfd_size_type external_sym_size;
   2200       const char *directory_name;
   2201       const char *main_file_name;
   2202       const char *current_file_name;
   2203       const char *function_name;
   2204       const char *line_file_name;
   2205       bfd_vma low_func_vma;
   2206       bfd_vma low_line_vma;
   2207       bfd_boolean past_line;
   2208       bfd_boolean past_fn;
   2209       char *sym_ptr, *sym_ptr_end;
   2210       size_t len, funclen;
   2211       char *buffer = NULL;
   2212 
   2213       /* This file uses stabs debugging information.  When gcc is not
   2214 	 optimizing, it will put the line number information before
   2215 	 the function name stabs entry.  When gcc is optimizing, it
   2216 	 will put the stabs entry for all the function first, followed
   2217 	 by the line number information.  (This appears to happen
   2218 	 because of the two output files used by the -mgpopt switch,
   2219 	 which is implied by -O).  This means that we must keep
   2220 	 looking through the symbols until we find both a line number
   2221 	 and a function name which are beyond the address we want.  */
   2222 
   2223       line_info->cache.filename = NULL;
   2224       line_info->cache.functionname = NULL;
   2225       line_info->cache.line_num = 0;
   2226 
   2227       directory_name = NULL;
   2228       main_file_name = NULL;
   2229       current_file_name = NULL;
   2230       function_name = NULL;
   2231       line_file_name = NULL;
   2232       low_func_vma = 0;
   2233       low_line_vma = 0;
   2234       past_line = FALSE;
   2235       past_fn = FALSE;
   2236 
   2237       external_sym_size = debug_swap->external_sym_size;
   2238 
   2239       sym_ptr = ((char *) debug_info->external_sym
   2240 		 + (fdr_ptr->isymBase + 2) * external_sym_size);
   2241       sym_ptr_end = sym_ptr + (fdr_ptr->csym - 2) * external_sym_size;
   2242       for (;
   2243 	   sym_ptr < sym_ptr_end && (! past_line || ! past_fn);
   2244 	   sym_ptr += external_sym_size)
   2245 	{
   2246 	  SYMR sym;
   2247 
   2248 	  (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
   2249 
   2250 	  if (ECOFF_IS_STAB (&sym))
   2251 	    {
   2252 	      switch (ECOFF_UNMARK_STAB (sym.index))
   2253 		{
   2254 		case N_SO:
   2255 		  main_file_name = current_file_name =
   2256 		    debug_info->ss + fdr_ptr->issBase + sym.iss;
   2257 
   2258 		  /* Check the next symbol to see if it is also an
   2259 		     N_SO symbol.  */
   2260 		  if (sym_ptr + external_sym_size < sym_ptr_end)
   2261 		    {
   2262 		      SYMR nextsym;
   2263 
   2264 		      (*debug_swap->swap_sym_in) (abfd,
   2265 						  sym_ptr + external_sym_size,
   2266 						  &nextsym);
   2267 		      if (ECOFF_IS_STAB (&nextsym)
   2268 			  && ECOFF_UNMARK_STAB (nextsym.index) == N_SO)
   2269 			{
   2270 			  directory_name = current_file_name;
   2271 			  main_file_name = current_file_name =
   2272 			    debug_info->ss + fdr_ptr->issBase + nextsym.iss;
   2273 			  sym_ptr += external_sym_size;
   2274 			}
   2275 		    }
   2276 		  break;
   2277 
   2278 		case N_SOL:
   2279 		  current_file_name =
   2280 		    debug_info->ss + fdr_ptr->issBase + sym.iss;
   2281 		  break;
   2282 
   2283 		case N_FUN:
   2284 		  if (sym.value > offset)
   2285 		    past_fn = TRUE;
   2286 		  else if (sym.value >= low_func_vma)
   2287 		    {
   2288 		      low_func_vma = sym.value;
   2289 		      function_name =
   2290 			debug_info->ss + fdr_ptr->issBase + sym.iss;
   2291 		    }
   2292 		  break;
   2293 		}
   2294 	    }
   2295 	  else if (sym.st == stLabel && sym.index != indexNil)
   2296 	    {
   2297 	      if (sym.value > offset)
   2298 		past_line = TRUE;
   2299 	      else if (sym.value >= low_line_vma)
   2300 		{
   2301 		  low_line_vma = sym.value;
   2302 		  line_file_name = current_file_name;
   2303 		  line_info->cache.line_num = sym.index;
   2304 		}
   2305 	    }
   2306 	}
   2307 
   2308       if (line_info->cache.line_num != 0)
   2309 	main_file_name = line_file_name;
   2310 
   2311       /* We need to remove the stuff after the colon in the function
   2312 	 name.  We also need to put the directory name and the file
   2313 	 name together.  */
   2314       if (function_name == NULL)
   2315 	len = funclen = 0;
   2316       else
   2317 	len = funclen = strlen (function_name) + 1;
   2318 
   2319       if (main_file_name != NULL
   2320 	  && directory_name != NULL
   2321 	  && main_file_name[0] != '/')
   2322 	len += strlen (directory_name) + strlen (main_file_name) + 1;
   2323 
   2324       if (len != 0)
   2325 	{
   2326 	  if (line_info->find_buffer != NULL)
   2327 	    free (line_info->find_buffer);
   2328 	  buffer = (char *) bfd_malloc ((bfd_size_type) len);
   2329 	  if (buffer == NULL)
   2330 	    return FALSE;
   2331 	  line_info->find_buffer = buffer;
   2332 	}
   2333 
   2334       if (function_name != NULL)
   2335 	{
   2336 	  char *colon;
   2337 
   2338 	  strcpy (buffer, function_name);
   2339 	  colon = strchr (buffer, ':');
   2340 	  if (colon != NULL)
   2341 	    *colon = '\0';
   2342 	  line_info->cache.functionname = buffer;
   2343 	}
   2344 
   2345       if (main_file_name != NULL)
   2346 	{
   2347 	  if (directory_name == NULL || main_file_name[0] == '/')
   2348 	    line_info->cache.filename = main_file_name;
   2349 	  else
   2350 	    {
   2351 	      sprintf (buffer + funclen, "%s%s", directory_name,
   2352 		       main_file_name);
   2353 	      line_info->cache.filename = buffer + funclen;
   2354 	    }
   2355 	}
   2356     }
   2357 
   2358   return TRUE;
   2359 }
   2360 
   2361 /* Do the work of find_nearest_line.  */
   2362 
   2363 bfd_boolean
   2364 _bfd_ecoff_locate_line (bfd *abfd,
   2365 			asection *section,
   2366 			bfd_vma offset,
   2367 			struct ecoff_debug_info * const debug_info,
   2368 			const struct ecoff_debug_swap * const debug_swap,
   2369 			struct ecoff_find_line *line_info,
   2370 			const char **filename_ptr,
   2371 			const char **functionname_ptr,
   2372 			unsigned int *retline_ptr)
   2373 {
   2374   offset += section->vma;
   2375 
   2376   if (line_info->cache.sect == NULL
   2377       || line_info->cache.sect != section
   2378       || offset < line_info->cache.start
   2379       || offset >= line_info->cache.stop)
   2380     {
   2381       line_info->cache.sect = section;
   2382       line_info->cache.start = offset;
   2383       line_info->cache.stop = offset;
   2384       if (! lookup_line (abfd, debug_info, debug_swap, line_info))
   2385 	{
   2386 	  line_info->cache.sect = NULL;
   2387 	  return FALSE;
   2388 	}
   2389     }
   2390 
   2391   *filename_ptr = line_info->cache.filename;
   2392   *functionname_ptr = line_info->cache.functionname;
   2393   *retline_ptr = line_info->cache.line_num;
   2394 
   2395   return TRUE;
   2396 }
   2397 
   2398 /* These routines copy symbolic information into a memory buffer.
   2400 
   2401    FIXME: The whole point of the shuffle code is to avoid storing
   2402    everything in memory, since the linker is such a memory hog.  This
   2403    code makes that effort useless.  It is only called by the MIPS ELF
   2404    code when generating a shared library, so it is not that big a
   2405    deal, but it should be fixed eventually.  */
   2406 
   2407 /* Collect a shuffle into a memory buffer.  */
   2408 
   2409 static bfd_boolean
   2410 ecoff_collect_shuffle (struct shuffle *l, bfd_byte *buff)
   2411 {
   2412   unsigned long total;
   2413 
   2414   total = 0;
   2415   for (; l != (struct shuffle *) NULL; l = l->next)
   2416     {
   2417       if (! l->filep)
   2418 	memcpy (buff, l->u.memory, l->size);
   2419       else
   2420 	{
   2421 	  if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
   2422 	      || (bfd_bread (buff, (bfd_size_type) l->size, l->u.file.input_bfd)
   2423 		  != l->size))
   2424 	    return FALSE;
   2425 	}
   2426       total += l->size;
   2427       buff += l->size;
   2428     }
   2429 
   2430   return TRUE;
   2431 }
   2432 
   2433 /* Copy PDR information into a memory buffer.  */
   2434 
   2435 bfd_boolean
   2436 _bfd_ecoff_get_accumulated_pdr (void * handle,
   2437 				bfd_byte *buff)
   2438 {
   2439   struct accumulate *ainfo = (struct accumulate *) handle;
   2440 
   2441   return ecoff_collect_shuffle (ainfo->pdr, buff);
   2442 }
   2443 
   2444 /* Copy symbol information into a memory buffer.  */
   2445 
   2446 bfd_boolean
   2447 _bfd_ecoff_get_accumulated_sym (void * handle, bfd_byte *buff)
   2448 {
   2449   struct accumulate *ainfo = (struct accumulate *) handle;
   2450 
   2451   return ecoff_collect_shuffle (ainfo->sym, buff);
   2452 }
   2453 
   2454 /* Copy the string table into a memory buffer.  */
   2455 
   2456 bfd_boolean
   2457 _bfd_ecoff_get_accumulated_ss (void * handle, bfd_byte *buff)
   2458 {
   2459   struct accumulate *ainfo = (struct accumulate *) handle;
   2460   struct string_hash_entry *sh;
   2461   unsigned long total;
   2462 
   2463   /* The string table is written out from the hash table if this is a
   2464      final link.  */
   2465   BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
   2466   *buff++ = '\0';
   2467   total = 1;
   2468   BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
   2469   for (sh = ainfo->ss_hash;
   2470        sh != (struct string_hash_entry *) NULL;
   2471        sh = sh->next)
   2472     {
   2473       size_t len;
   2474 
   2475       len = strlen (sh->root.string);
   2476       memcpy (buff, sh->root.string, len + 1);
   2477       total += len + 1;
   2478       buff += len + 1;
   2479     }
   2480 
   2481   return TRUE;
   2482 }
   2483