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