s/boolean/bfd_boolean/ s/true/TRUE/ s/false/FALSE/. Simplify
[external/binutils.git] / bfd / ecofflink.c
1 /* Routines to link ECOFF debugging information.
2    Copyright 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002
3    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 \f
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 \f
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 \f
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                                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->relocateable)
530     {
531       if (! bfd_hash_table_init (&ainfo->str_hash.table, string_hash_newfunc))
532         return NULL;
533
534       /* The first entry in the string table is the empty string.  */
535       output_debug->symbolic_header.issMax = 1;
536     }
537
538   ainfo->memory = objalloc_create ();
539   if (ainfo->memory == NULL)
540     {
541       bfd_set_error (bfd_error_no_memory);
542       return NULL;
543     }
544
545   return (PTR) ainfo;
546 }
547
548 /* Free the accumulated debugging information.  */
549
550 void
551 bfd_ecoff_debug_free (handle, output_bfd, output_debug, output_swap, info)
552      PTR handle;
553      bfd *output_bfd ATTRIBUTE_UNUSED;
554      struct ecoff_debug_info *output_debug ATTRIBUTE_UNUSED;
555      const struct ecoff_debug_swap *output_swap ATTRIBUTE_UNUSED;
556      struct bfd_link_info *info;
557 {
558   struct accumulate *ainfo = (struct accumulate *) handle;
559
560   bfd_hash_table_free (&ainfo->fdr_hash.table);
561
562   if (! info->relocateable)
563     bfd_hash_table_free (&ainfo->str_hash.table);
564
565   objalloc_free (ainfo->memory);
566
567   free (ainfo);
568 }
569
570 /* Accumulate the debugging information from INPUT_BFD into
571    OUTPUT_BFD.  The INPUT_DEBUG argument points to some ECOFF
572    debugging information which we want to link into the information
573    pointed to by the OUTPUT_DEBUG argument.  OUTPUT_SWAP and
574    INPUT_SWAP point to the swapping information needed.  INFO is the
575    linker information structure.  HANDLE is returned by
576    bfd_ecoff_debug_init.  */
577
578 bfd_boolean
579 bfd_ecoff_debug_accumulate (handle, output_bfd, output_debug, output_swap,
580                             input_bfd, input_debug, input_swap,
581                             info)
582      PTR handle;
583      bfd *output_bfd;
584      struct ecoff_debug_info *output_debug;
585      const struct ecoff_debug_swap *output_swap;
586      bfd *input_bfd;
587      struct ecoff_debug_info *input_debug;
588      const struct ecoff_debug_swap *input_swap;
589      struct bfd_link_info *info;
590 {
591   struct accumulate *ainfo = (struct accumulate *) handle;
592   void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
593     = input_swap->swap_sym_in;
594   void (* const swap_rfd_in) PARAMS ((bfd *, PTR, RFDT *))
595     = input_swap->swap_rfd_in;
596   void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
597     = output_swap->swap_sym_out;
598   void (* const swap_fdr_out) PARAMS ((bfd *, const FDR *, PTR))
599     = output_swap->swap_fdr_out;
600   void (* const swap_rfd_out) PARAMS ((bfd *, const RFDT *, PTR))
601     = output_swap->swap_rfd_out;
602   bfd_size_type external_pdr_size = output_swap->external_pdr_size;
603   bfd_size_type external_sym_size = output_swap->external_sym_size;
604   bfd_size_type external_opt_size = output_swap->external_opt_size;
605   bfd_size_type external_fdr_size = output_swap->external_fdr_size;
606   bfd_size_type external_rfd_size = output_swap->external_rfd_size;
607   HDRR * const output_symhdr = &output_debug->symbolic_header;
608   HDRR * const input_symhdr = &input_debug->symbolic_header;
609   bfd_vma section_adjust[scMax];
610   asection *sec;
611   bfd_byte *fdr_start;
612   bfd_byte *fdr_ptr;
613   bfd_byte *fdr_end;
614   bfd_size_type fdr_add;
615   unsigned int copied;
616   RFDT i;
617   unsigned long sz;
618   bfd_byte *rfd_out;
619   bfd_byte *rfd_in;
620   bfd_byte *rfd_end;
621   long newrfdbase = 0;
622   long oldrfdbase = 0;
623   bfd_byte *fdr_out;
624   bfd_size_type amt;
625
626   /* Use section_adjust to hold the value to add to a symbol in a
627      particular section.  */
628   memset ((PTR) section_adjust, 0, sizeof section_adjust);
629
630 #define SET(name, indx) \
631   sec = bfd_get_section_by_name (input_bfd, name); \
632   if (sec != NULL) \
633     section_adjust[indx] = (sec->output_section->vma \
634                             + sec->output_offset \
635                             - sec->vma);
636
637   SET (".text", scText);
638   SET (".data", scData);
639   SET (".bss", scBss);
640   SET (".sdata", scSData);
641   SET (".sbss", scSBss);
642   /* scRdata section may be either .rdata or .rodata.  */
643   SET (".rdata", scRData);
644   SET (".rodata", scRData);
645   SET (".init", scInit);
646   SET (".fini", scFini);
647   SET (".rconst", scRConst);
648
649 #undef SET
650
651   /* Find all the debugging information based on the FDR's.  We need
652      to handle them whether they are swapped or not.  */
653   if (input_debug->fdr != (FDR *) NULL)
654     {
655       fdr_start = (bfd_byte *) input_debug->fdr;
656       fdr_add = sizeof (FDR);
657     }
658   else
659     {
660       fdr_start = (bfd_byte *) input_debug->external_fdr;
661       fdr_add = input_swap->external_fdr_size;
662     }
663   fdr_end = fdr_start + input_symhdr->ifdMax * fdr_add;
664
665   amt = input_symhdr->ifdMax;
666   amt *= sizeof (RFDT);
667   input_debug->ifdmap = (RFDT *) bfd_alloc (input_bfd, amt);
668
669   sz = (input_symhdr->crfd + input_symhdr->ifdMax) * external_rfd_size;
670   rfd_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
671   if (!input_debug->ifdmap || !rfd_out)
672     {
673       bfd_set_error (bfd_error_no_memory);
674       return FALSE;
675     }
676   if (!add_memory_shuffle (ainfo, &ainfo->rfd, &ainfo->rfd_end, rfd_out, sz))
677     return FALSE;
678
679   copied = 0;
680
681   /* Look through the FDR's to see which ones we are going to include
682      in the final output.  We do not want duplicate FDR information
683      for header files, because ECOFF debugging is often very large.
684      When we find an FDR with no line information which can be merged,
685      we look it up in a hash table to ensure that we only include it
686      once.  We keep a table mapping FDR numbers to the final number
687      they get with the BFD, so that we can refer to it when we write
688      out the external symbols.  */
689   for (fdr_ptr = fdr_start, i = 0;
690        fdr_ptr < fdr_end;
691        fdr_ptr += fdr_add, i++, rfd_out += external_rfd_size)
692     {
693       FDR fdr;
694
695       if (input_debug->fdr != (FDR *) NULL)
696         fdr = *(FDR *) fdr_ptr;
697       else
698         (*input_swap->swap_fdr_in) (input_bfd, (PTR) fdr_ptr, &fdr);
699
700       /* See if this FDR can be merged with an existing one.  */
701       if (fdr.cbLine == 0 && fdr.rss != -1 && fdr.fMerge)
702         {
703           const char *name;
704           char *lookup;
705           struct string_hash_entry *fh;
706
707           /* We look up a string formed from the file name and the
708              number of symbols and aux entries.  Sometimes an include
709              file will conditionally define a typedef or something
710              based on the order of include files.  Using the number of
711              symbols and aux entries as a hash reduces the chance that
712              we will merge symbol information that should not be
713              merged.  */
714           name = input_debug->ss + fdr.issBase + fdr.rss;
715
716           lookup = (char *) bfd_malloc ((bfd_size_type) strlen (name) + 20);
717           if (lookup == NULL)
718             return FALSE;
719           sprintf (lookup, "%s %lx %lx", name, fdr.csym, fdr.caux);
720
721           fh = string_hash_lookup (&ainfo->fdr_hash, lookup, TRUE, TRUE);
722           free (lookup);
723           if (fh == (struct string_hash_entry *) NULL)
724             return FALSE;
725
726           if (fh->val != -1)
727             {
728               input_debug->ifdmap[i] = fh->val;
729               (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i,
730                                (PTR) rfd_out);
731
732               /* Don't copy this FDR.  */
733               continue;
734             }
735
736           fh->val = output_symhdr->ifdMax + copied;
737         }
738
739       input_debug->ifdmap[i] = output_symhdr->ifdMax + copied;
740       (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i, (PTR) rfd_out);
741       ++copied;
742     }
743
744   newrfdbase = output_symhdr->crfd;
745   output_symhdr->crfd += input_symhdr->ifdMax;
746
747   /* Copy over any existing RFD's.  RFD's are only created by the
748      linker, so this will only happen for input files which are the
749      result of a partial link.  */
750   rfd_in = (bfd_byte *) input_debug->external_rfd;
751   rfd_end = rfd_in + input_symhdr->crfd * input_swap->external_rfd_size;
752   for (;
753        rfd_in < rfd_end;
754        rfd_in += input_swap->external_rfd_size)
755     {
756       RFDT rfd;
757
758       (*swap_rfd_in) (input_bfd, (PTR) rfd_in, &rfd);
759       BFD_ASSERT (rfd >= 0 && rfd < input_symhdr->ifdMax);
760       rfd = input_debug->ifdmap[rfd];
761       (*swap_rfd_out) (output_bfd, &rfd, (PTR) rfd_out);
762       rfd_out += external_rfd_size;
763     }
764
765   oldrfdbase = output_symhdr->crfd;
766   output_symhdr->crfd += input_symhdr->crfd;
767
768   /* Look through the FDR's and copy over all associated debugging
769      information.  */
770   sz = copied * external_fdr_size;
771   fdr_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
772   if (!fdr_out)
773     {
774       bfd_set_error (bfd_error_no_memory);
775       return FALSE;
776     }
777   if (!add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end, fdr_out, sz))
778     return FALSE;
779   for (fdr_ptr = fdr_start, i = 0;
780        fdr_ptr < fdr_end;
781        fdr_ptr += fdr_add, i++)
782     {
783       FDR fdr;
784       bfd_vma fdr_adr;
785       bfd_byte *sym_out;
786       bfd_byte *lraw_src;
787       bfd_byte *lraw_end;
788       bfd_boolean fgotfilename;
789
790       if (input_debug->ifdmap[i] < output_symhdr->ifdMax)
791         {
792           /* We are not copying this FDR.  */
793           continue;
794         }
795
796       if (input_debug->fdr != (FDR *) NULL)
797         fdr = *(FDR *) fdr_ptr;
798       else
799         (*input_swap->swap_fdr_in) (input_bfd, (PTR) fdr_ptr, &fdr);
800
801       fdr_adr = fdr.adr;
802
803       /* Adjust the FDR address for any changes that may have been
804          made by relaxing.  */
805       if (input_debug->adjust != (struct ecoff_value_adjust *) NULL)
806         {
807           struct ecoff_value_adjust *adjust;
808
809           for (adjust = input_debug->adjust;
810                adjust != (struct ecoff_value_adjust *) NULL;
811                adjust = adjust->next)
812             if (fdr_adr >= adjust->start
813                 && fdr_adr < adjust->end)
814               fdr.adr += adjust->adjust;
815         }
816
817       /* FIXME: It is conceivable that this FDR points to the .init or
818          .fini section, in which case this will not do the right
819          thing.  */
820       fdr.adr += section_adjust[scText];
821
822       /* Swap in the local symbols, adjust their values, and swap them
823          out again.  */
824       fgotfilename = FALSE;
825       sz = fdr.csym * external_sym_size;
826       sym_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
827       if (!sym_out)
828         {
829           bfd_set_error (bfd_error_no_memory);
830           return FALSE;
831         }
832       if (!add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end, sym_out,
833                                sz))
834         return FALSE;
835       lraw_src = ((bfd_byte *) input_debug->external_sym
836                   + fdr.isymBase * input_swap->external_sym_size);
837       lraw_end = lraw_src + fdr.csym * input_swap->external_sym_size;
838       for (;  lraw_src < lraw_end;  lraw_src += input_swap->external_sym_size)
839         {
840           SYMR internal_sym;
841
842           (*swap_sym_in) (input_bfd, (PTR) lraw_src, &internal_sym);
843
844           BFD_ASSERT (internal_sym.sc != scCommon
845                       && internal_sym.sc != scSCommon);
846
847           /* Adjust the symbol value if appropriate.  */
848           switch (internal_sym.st)
849             {
850             case stNil:
851               if (ECOFF_IS_STAB (&internal_sym))
852                 break;
853               /* Fall through.  */
854             case stGlobal:
855             case stStatic:
856             case stLabel:
857             case stProc:
858             case stStaticProc:
859               if (input_debug->adjust != (struct ecoff_value_adjust *) NULL)
860                 {
861                   bfd_vma value;
862                   struct ecoff_value_adjust *adjust;
863
864                   value = internal_sym.value;
865                   for (adjust = input_debug->adjust;
866                        adjust != (struct ecoff_value_adjust *) NULL;
867                        adjust = adjust->next)
868                     if (value >= adjust->start
869                         && value < adjust->end)
870                       internal_sym.value += adjust->adjust;
871                 }
872               internal_sym.value += section_adjust[internal_sym.sc];
873               break;
874
875             default:
876               break;
877             }
878
879           /* If we are doing a final link, we hash all the strings in
880              the local symbol table together.  This reduces the amount
881              of space required by debugging information.  We don't do
882              this when performing a relocateable link because it would
883              prevent us from easily merging different FDR's.  */
884           if (! info->relocateable)
885             {
886               bfd_boolean ffilename;
887               const char *name;
888
889               if (! fgotfilename && internal_sym.iss == fdr.rss)
890                 ffilename = TRUE;
891               else
892                 ffilename = FALSE;
893
894               /* Hash the name into the string table.  */
895               name = input_debug->ss + fdr.issBase + internal_sym.iss;
896               if (*name == '\0')
897                 internal_sym.iss = 0;
898               else
899                 {
900                   struct string_hash_entry *sh;
901
902                   sh = string_hash_lookup (&ainfo->str_hash, name, TRUE, TRUE);
903                   if (sh == (struct string_hash_entry *) NULL)
904                     return FALSE;
905                   if (sh->val == -1)
906                     {
907                       sh->val = output_symhdr->issMax;
908                       output_symhdr->issMax += strlen (name) + 1;
909                       if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
910                         ainfo->ss_hash = sh;
911                       if (ainfo->ss_hash_end
912                           != (struct string_hash_entry *) NULL)
913                         ainfo->ss_hash_end->next = sh;
914                       ainfo->ss_hash_end = sh;
915                     }
916                   internal_sym.iss = sh->val;
917                 }
918
919               if (ffilename)
920                 {
921                   fdr.rss = internal_sym.iss;
922                   fgotfilename = TRUE;
923                 }
924             }
925
926           (*swap_sym_out) (output_bfd, &internal_sym, sym_out);
927           sym_out += external_sym_size;
928         }
929
930       fdr.isymBase = output_symhdr->isymMax;
931       output_symhdr->isymMax += fdr.csym;
932
933       /* Copy the information that does not need swapping.  */
934
935       /* FIXME: If we are relaxing, we need to adjust the line
936          numbers.  Frankly, forget it.  Anybody using stabs debugging
937          information will not use this line number information, and
938          stabs are adjusted correctly.  */
939       if (fdr.cbLine > 0)
940         {
941           file_ptr pos = input_symhdr->cbLineOffset + fdr.cbLineOffset;
942           if (!add_file_shuffle (ainfo, &ainfo->line, &ainfo->line_end,
943                                  input_bfd, pos, (unsigned long) fdr.cbLine))
944             return FALSE;
945           fdr.ilineBase = output_symhdr->ilineMax;
946           fdr.cbLineOffset = output_symhdr->cbLine;
947           output_symhdr->ilineMax += fdr.cline;
948           output_symhdr->cbLine += fdr.cbLine;
949         }
950       if (fdr.caux > 0)
951         {
952           file_ptr pos = (input_symhdr->cbAuxOffset
953                           + fdr.iauxBase * sizeof (union aux_ext));
954           if (!add_file_shuffle (ainfo, &ainfo->aux, &ainfo->aux_end,
955                                  input_bfd, pos,
956                                  fdr.caux * sizeof (union aux_ext)))
957             return FALSE;
958           fdr.iauxBase = output_symhdr->iauxMax;
959           output_symhdr->iauxMax += fdr.caux;
960         }
961       if (! info->relocateable)
962         {
963
964           /* When are are hashing strings, we lie about the number of
965              strings attached to each FDR.  We need to set cbSs
966              because some versions of dbx apparently use it to decide
967              how much of the string table to read in.  */
968           fdr.issBase = 0;
969           fdr.cbSs = output_symhdr->issMax;
970         }
971       else if (fdr.cbSs > 0)
972         {
973           file_ptr pos = input_symhdr->cbSsOffset + fdr.issBase;
974           if (!add_file_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end,
975                                  input_bfd, pos, (unsigned long) fdr.cbSs))
976             return FALSE;
977           fdr.issBase = output_symhdr->issMax;
978           output_symhdr->issMax += fdr.cbSs;
979         }
980
981       if ((output_bfd->xvec->header_byteorder
982            == input_bfd->xvec->header_byteorder)
983           && input_debug->adjust == (struct ecoff_value_adjust *) NULL)
984         {
985           /* The two BFD's have the same endianness, and we don't have
986              to adjust the PDR addresses, so simply copying the
987              information will suffice.  */
988           BFD_ASSERT (external_pdr_size == input_swap->external_pdr_size);
989           if (fdr.cpd > 0)
990             {
991               file_ptr pos = (input_symhdr->cbPdOffset
992                               + fdr.ipdFirst * external_pdr_size);
993               unsigned long size = fdr.cpd * external_pdr_size;
994               if (!add_file_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end,
995                                      input_bfd, pos, size))
996                 return FALSE;
997             }
998           BFD_ASSERT (external_opt_size == input_swap->external_opt_size);
999           if (fdr.copt > 0)
1000             {
1001               file_ptr pos = (input_symhdr->cbOptOffset
1002                               + fdr.ioptBase * external_opt_size);
1003               unsigned long size = fdr.copt * external_opt_size;
1004               if (!add_file_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end,
1005                                      input_bfd, pos, size))
1006                 return FALSE;
1007             }
1008         }
1009       else
1010         {
1011           bfd_size_type outsz, insz;
1012           bfd_byte *in;
1013           bfd_byte *end;
1014           bfd_byte *out;
1015
1016           /* The two BFD's have different endianness, so we must swap
1017              everything in and out.  This code would always work, but
1018              it would be unnecessarily slow in the normal case.  */
1019           outsz = external_pdr_size;
1020           insz = input_swap->external_pdr_size;
1021           in = ((bfd_byte *) input_debug->external_pdr
1022                 + fdr.ipdFirst * insz);
1023           end = in + fdr.cpd * insz;
1024           sz = fdr.cpd * outsz;
1025           out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
1026           if (!out)
1027             {
1028               bfd_set_error (bfd_error_no_memory);
1029               return FALSE;
1030             }
1031           if (!add_memory_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end, out,
1032                                    sz))
1033             return FALSE;
1034           for (; in < end; in += insz, out += outsz)
1035             {
1036               PDR pdr;
1037
1038               (*input_swap->swap_pdr_in) (input_bfd, (PTR) in, &pdr);
1039
1040               /* If we have been relaxing, we may have to adjust the
1041                  address.  */
1042               if (input_debug->adjust != (struct ecoff_value_adjust *) NULL)
1043                 {
1044                   bfd_vma adr;
1045                   struct ecoff_value_adjust *adjust;
1046
1047                   adr = fdr_adr + pdr.adr;
1048                   for (adjust = input_debug->adjust;
1049                        adjust != (struct ecoff_value_adjust *) NULL;
1050                        adjust = adjust->next)
1051                     if (adr >= adjust->start
1052                         && adr < adjust->end)
1053                       pdr.adr += adjust->adjust;
1054                 }
1055
1056               (*output_swap->swap_pdr_out) (output_bfd, &pdr, (PTR) out);
1057             }
1058
1059           /* Swap over the optimization information.  */
1060           outsz = external_opt_size;
1061           insz = input_swap->external_opt_size;
1062           in = ((bfd_byte *) input_debug->external_opt
1063                 + fdr.ioptBase * insz);
1064           end = in + fdr.copt * insz;
1065           sz = fdr.copt * outsz;
1066           out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
1067           if (!out)
1068             {
1069               bfd_set_error (bfd_error_no_memory);
1070               return FALSE;
1071             }
1072           if (!add_memory_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end, out,
1073                                    sz))
1074             return FALSE;
1075           for (; in < end; in += insz, out += outsz)
1076             {
1077               OPTR opt;
1078
1079               (*input_swap->swap_opt_in) (input_bfd, (PTR) in, &opt);
1080               (*output_swap->swap_opt_out) (output_bfd, &opt, (PTR) out);
1081             }
1082         }
1083
1084       fdr.ipdFirst = output_symhdr->ipdMax;
1085       output_symhdr->ipdMax += fdr.cpd;
1086       fdr.ioptBase = output_symhdr->ioptMax;
1087       output_symhdr->ioptMax += fdr.copt;
1088
1089       if (fdr.crfd <= 0)
1090         {
1091           /* Point this FDR at the table of RFD's we created.  */
1092           fdr.rfdBase = newrfdbase;
1093           fdr.crfd = input_symhdr->ifdMax;
1094         }
1095       else
1096         {
1097           /* Point this FDR at the remapped RFD's.  */
1098           fdr.rfdBase += oldrfdbase;
1099         }
1100
1101       (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
1102       fdr_out += external_fdr_size;
1103       ++output_symhdr->ifdMax;
1104     }
1105
1106   return TRUE;
1107 }
1108
1109 /* Add a string to the debugging information we are accumulating.
1110    Return the offset from the fdr string base.  */
1111
1112 static long ecoff_add_string
1113   PARAMS ((struct accumulate *, struct bfd_link_info *,
1114            struct ecoff_debug_info *, FDR *fdr, const char *string));
1115
1116 static long
1117 ecoff_add_string (ainfo, info, debug, fdr, string)
1118      struct accumulate *ainfo;
1119      struct bfd_link_info *info;
1120      struct ecoff_debug_info *debug;
1121      FDR *fdr;
1122      const char *string;
1123 {
1124   HDRR *symhdr;
1125   size_t len;
1126   bfd_size_type ret;
1127
1128   symhdr = &debug->symbolic_header;
1129   len = strlen (string);
1130   if (info->relocateable)
1131     {
1132       if (!add_memory_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end, (PTR) string,
1133                                len + 1))
1134         return -1;
1135       ret = symhdr->issMax;
1136       symhdr->issMax += len + 1;
1137       fdr->cbSs += len + 1;
1138     }
1139   else
1140     {
1141       struct string_hash_entry *sh;
1142
1143       sh = string_hash_lookup (&ainfo->str_hash, string, TRUE, TRUE);
1144       if (sh == (struct string_hash_entry *) NULL)
1145         return -1;
1146       if (sh->val == -1)
1147         {
1148           sh->val = symhdr->issMax;
1149           symhdr->issMax += len + 1;
1150           if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
1151             ainfo->ss_hash = sh;
1152           if (ainfo->ss_hash_end
1153               != (struct string_hash_entry *) NULL)
1154             ainfo->ss_hash_end->next = sh;
1155           ainfo->ss_hash_end = sh;
1156         }
1157       ret = sh->val;
1158     }
1159
1160   return ret;
1161 }
1162
1163 /* Add debugging information from a non-ECOFF file.  */
1164
1165 bfd_boolean
1166 bfd_ecoff_debug_accumulate_other (handle, output_bfd, output_debug,
1167                                   output_swap, input_bfd, info)
1168      PTR handle;
1169      bfd *output_bfd;
1170      struct ecoff_debug_info *output_debug;
1171      const struct ecoff_debug_swap *output_swap;
1172      bfd *input_bfd;
1173      struct bfd_link_info *info;
1174 {
1175   struct accumulate *ainfo = (struct accumulate *) handle;
1176   void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
1177     = output_swap->swap_sym_out;
1178   HDRR *output_symhdr = &output_debug->symbolic_header;
1179   FDR fdr;
1180   asection *sec;
1181   asymbol **symbols;
1182   asymbol **sym_ptr;
1183   asymbol **sym_end;
1184   long symsize;
1185   long symcount;
1186   PTR external_fdr;
1187
1188   memset ((PTR) &fdr, 0, sizeof fdr);
1189
1190   sec = bfd_get_section_by_name (input_bfd, ".text");
1191   if (sec != NULL)
1192     fdr.adr = sec->output_section->vma + sec->output_offset;
1193   else
1194     {
1195       /* FIXME: What about .init or .fini?  */
1196       fdr.adr = 0;
1197     }
1198
1199   fdr.issBase = output_symhdr->issMax;
1200   fdr.cbSs = 0;
1201   fdr.rss = ecoff_add_string (ainfo, info, output_debug, &fdr,
1202                               bfd_archive_filename (input_bfd));
1203   if (fdr.rss == -1)
1204     return FALSE;
1205   fdr.isymBase = output_symhdr->isymMax;
1206
1207   /* Get the local symbols from the input BFD.  */
1208   symsize = bfd_get_symtab_upper_bound (input_bfd);
1209   if (symsize < 0)
1210     return FALSE;
1211   symbols = (asymbol **) bfd_alloc (output_bfd, (bfd_size_type) symsize);
1212   if (symbols == (asymbol **) NULL)
1213     return FALSE;
1214   symcount = bfd_canonicalize_symtab (input_bfd, symbols);
1215   if (symcount < 0)
1216     return FALSE;
1217   sym_end = symbols + symcount;
1218
1219   /* Handle the local symbols.  Any external symbols are handled
1220      separately.  */
1221   fdr.csym = 0;
1222   for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
1223     {
1224       SYMR internal_sym;
1225       PTR external_sym;
1226
1227       if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
1228         continue;
1229       memset ((PTR) &internal_sym, 0, sizeof internal_sym);
1230       internal_sym.iss = ecoff_add_string (ainfo, info, output_debug, &fdr,
1231                                            (*sym_ptr)->name);
1232
1233       if (internal_sym.iss == -1)
1234         return FALSE;
1235       if (bfd_is_com_section ((*sym_ptr)->section)
1236           || bfd_is_und_section ((*sym_ptr)->section))
1237         internal_sym.value = (*sym_ptr)->value;
1238       else
1239         internal_sym.value = ((*sym_ptr)->value
1240                               + (*sym_ptr)->section->output_offset
1241                               + (*sym_ptr)->section->output_section->vma);
1242       internal_sym.st = stNil;
1243       internal_sym.sc = scUndefined;
1244       internal_sym.index = indexNil;
1245
1246       external_sym = (PTR) objalloc_alloc (ainfo->memory,
1247                                            output_swap->external_sym_size);
1248       if (!external_sym)
1249         {
1250           bfd_set_error (bfd_error_no_memory);
1251           return FALSE;
1252         }
1253       (*swap_sym_out) (output_bfd, &internal_sym, external_sym);
1254       add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end,
1255                           external_sym,
1256                           (unsigned long) output_swap->external_sym_size);
1257       ++fdr.csym;
1258       ++output_symhdr->isymMax;
1259     }
1260
1261   bfd_release (output_bfd, (PTR) symbols);
1262
1263   /* Leave everything else in the FDR zeroed out.  This will cause
1264      the lang field to be langC.  The fBigendian field will
1265      indicate little endian format, but it doesn't matter because
1266      it only applies to aux fields and there are none.  */
1267   external_fdr = (PTR) objalloc_alloc (ainfo->memory,
1268                                        output_swap->external_fdr_size);
1269   if (!external_fdr)
1270     {
1271       bfd_set_error (bfd_error_no_memory);
1272       return FALSE;
1273     }
1274   (*output_swap->swap_fdr_out) (output_bfd, &fdr, external_fdr);
1275   add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end,
1276                       external_fdr,
1277                       (unsigned long) output_swap->external_fdr_size);
1278
1279   ++output_symhdr->ifdMax;
1280
1281   return TRUE;
1282 }
1283
1284 /* Set up ECOFF debugging information for the external symbols.
1285    FIXME: This is done using a memory buffer, but it should be
1286    probably be changed to use a shuffle structure.  The assembler uses
1287    this interface, so that must be changed to do something else.  */
1288
1289 bfd_boolean
1290 bfd_ecoff_debug_externals (abfd, debug, swap, relocateable, get_extr,
1291                            set_index)
1292      bfd *abfd;
1293      struct ecoff_debug_info *debug;
1294      const struct ecoff_debug_swap *swap;
1295      bfd_boolean relocateable;
1296      bfd_boolean (*get_extr) PARAMS ((asymbol *, EXTR *));
1297      void (*set_index) PARAMS ((asymbol *, bfd_size_type));
1298 {
1299   HDRR * const symhdr = &debug->symbolic_header;
1300   asymbol **sym_ptr_ptr;
1301   size_t c;
1302
1303   sym_ptr_ptr = bfd_get_outsymbols (abfd);
1304   if (sym_ptr_ptr == NULL)
1305     return TRUE;
1306
1307   for (c = bfd_get_symcount (abfd); c > 0; c--, sym_ptr_ptr++)
1308     {
1309       asymbol *sym_ptr;
1310       EXTR esym;
1311
1312       sym_ptr = *sym_ptr_ptr;
1313
1314       /* Get the external symbol information.  */
1315       if (! (*get_extr) (sym_ptr, &esym))
1316         continue;
1317
1318       /* If we're producing an executable, move common symbols into
1319          bss.  */
1320       if (! relocateable)
1321         {
1322           if (esym.asym.sc == scCommon)
1323             esym.asym.sc = scBss;
1324           else if (esym.asym.sc == scSCommon)
1325             esym.asym.sc = scSBss;
1326         }
1327
1328       if (bfd_is_com_section (sym_ptr->section)
1329           || bfd_is_und_section (sym_ptr->section)
1330           || sym_ptr->section->output_section == (asection *) NULL)
1331         {
1332           /* FIXME: gas does not keep the value of a small undefined
1333              symbol in the symbol itself, because of relocation
1334              problems.  */
1335           if (esym.asym.sc != scSUndefined
1336               || esym.asym.value == 0
1337               || sym_ptr->value != 0)
1338             esym.asym.value = sym_ptr->value;
1339         }
1340       else
1341         esym.asym.value = (sym_ptr->value
1342                            + sym_ptr->section->output_offset
1343                            + sym_ptr->section->output_section->vma);
1344
1345       if (set_index)
1346         (*set_index) (sym_ptr, (bfd_size_type) symhdr->iextMax);
1347
1348       if (! bfd_ecoff_debug_one_external (abfd, debug, swap,
1349                                           sym_ptr->name, &esym))
1350         return FALSE;
1351     }
1352
1353   return TRUE;
1354 }
1355
1356 /* Add a single external symbol to the debugging information.  */
1357
1358 bfd_boolean
1359 bfd_ecoff_debug_one_external (abfd, debug, swap, name, esym)
1360      bfd *abfd;
1361      struct ecoff_debug_info *debug;
1362      const struct ecoff_debug_swap *swap;
1363      const char *name;
1364      EXTR *esym;
1365 {
1366   const bfd_size_type external_ext_size = swap->external_ext_size;
1367   void (* const swap_ext_out) PARAMS ((bfd *, const EXTR *, PTR))
1368     = swap->swap_ext_out;
1369   HDRR * const symhdr = &debug->symbolic_header;
1370   size_t namelen;
1371
1372   namelen = strlen (name);
1373
1374   if ((size_t) (debug->ssext_end - debug->ssext)
1375       < symhdr->issExtMax + namelen + 1)
1376     {
1377       if (! ecoff_add_bytes ((char **) &debug->ssext,
1378                              (char **) &debug->ssext_end,
1379                              symhdr->issExtMax + namelen + 1))
1380         return FALSE;
1381     }
1382   if ((size_t) ((char *) debug->external_ext_end
1383                 - (char *) debug->external_ext)
1384       < (symhdr->iextMax + 1) * external_ext_size)
1385     {
1386       if (! ecoff_add_bytes ((char **) &debug->external_ext,
1387                              (char **) &debug->external_ext_end,
1388                              (symhdr->iextMax + 1) * (size_t) external_ext_size))
1389         return FALSE;
1390     }
1391
1392   esym->asym.iss = symhdr->issExtMax;
1393
1394   (*swap_ext_out) (abfd, esym,
1395                    ((char *) debug->external_ext
1396                     + symhdr->iextMax * swap->external_ext_size));
1397
1398   ++symhdr->iextMax;
1399
1400   strcpy (debug->ssext + symhdr->issExtMax, name);
1401   symhdr->issExtMax += namelen + 1;
1402
1403   return TRUE;
1404 }
1405
1406 /* Align the ECOFF debugging information.  */
1407
1408 static void
1409 ecoff_align_debug (abfd, debug, swap)
1410      bfd *abfd ATTRIBUTE_UNUSED;
1411      struct ecoff_debug_info *debug;
1412      const struct ecoff_debug_swap *swap;
1413 {
1414   HDRR * const symhdr = &debug->symbolic_header;
1415   bfd_size_type debug_align, aux_align, rfd_align;
1416   size_t add;
1417
1418   /* Adjust the counts so that structures are aligned.  */
1419   debug_align = swap->debug_align;
1420   aux_align = debug_align / sizeof (union aux_ext);
1421   rfd_align = debug_align / swap->external_rfd_size;
1422
1423   add = debug_align - (symhdr->cbLine & (debug_align - 1));
1424   if (add != debug_align)
1425     {
1426       if (debug->line != (unsigned char *) NULL)
1427         memset ((PTR) (debug->line + symhdr->cbLine), 0, add);
1428       symhdr->cbLine += add;
1429     }
1430
1431   add = debug_align - (symhdr->issMax & (debug_align - 1));
1432   if (add != debug_align)
1433     {
1434       if (debug->ss != (char *) NULL)
1435         memset ((PTR) (debug->ss + symhdr->issMax), 0, add);
1436       symhdr->issMax += add;
1437     }
1438
1439   add = debug_align - (symhdr->issExtMax & (debug_align - 1));
1440   if (add != debug_align)
1441     {
1442       if (debug->ssext != (char *) NULL)
1443         memset ((PTR) (debug->ssext + symhdr->issExtMax), 0, add);
1444       symhdr->issExtMax += add;
1445     }
1446
1447   add = aux_align - (symhdr->iauxMax & (aux_align - 1));
1448   if (add != aux_align)
1449     {
1450       if (debug->external_aux != (union aux_ext *) NULL)
1451         memset ((PTR) (debug->external_aux + symhdr->iauxMax), 0,
1452                 add * sizeof (union aux_ext));
1453       symhdr->iauxMax += add;
1454     }
1455
1456   add = rfd_align - (symhdr->crfd & (rfd_align - 1));
1457   if (add != rfd_align)
1458     {
1459       if (debug->external_rfd != (PTR) NULL)
1460         memset ((PTR) ((char *) debug->external_rfd
1461                        + symhdr->crfd * swap->external_rfd_size),
1462                 0, (size_t) (add * swap->external_rfd_size));
1463       symhdr->crfd += add;
1464     }
1465 }
1466
1467 /* Return the size required by the ECOFF debugging information.  */
1468
1469 bfd_size_type
1470 bfd_ecoff_debug_size (abfd, debug, swap)
1471      bfd *abfd;
1472      struct ecoff_debug_info *debug;
1473      const struct ecoff_debug_swap *swap;
1474 {
1475   bfd_size_type tot;
1476
1477   ecoff_align_debug (abfd, debug, swap);
1478   tot = swap->external_hdr_size;
1479
1480 #define ADD(count, size) \
1481   tot += debug->symbolic_header.count * size
1482
1483   ADD (cbLine, sizeof (unsigned char));
1484   ADD (idnMax, swap->external_dnr_size);
1485   ADD (ipdMax, swap->external_pdr_size);
1486   ADD (isymMax, swap->external_sym_size);
1487   ADD (ioptMax, swap->external_opt_size);
1488   ADD (iauxMax, sizeof (union aux_ext));
1489   ADD (issMax, sizeof (char));
1490   ADD (issExtMax, sizeof (char));
1491   ADD (ifdMax, swap->external_fdr_size);
1492   ADD (crfd, swap->external_rfd_size);
1493   ADD (iextMax, swap->external_ext_size);
1494
1495 #undef ADD
1496
1497   return tot;
1498 }
1499
1500 /* Write out the ECOFF symbolic header, given the file position it is
1501    going to be placed at.  This assumes that the counts are set
1502    correctly.  */
1503
1504 static bfd_boolean
1505 ecoff_write_symhdr (abfd, debug, swap, where)
1506      bfd *abfd;
1507      struct ecoff_debug_info *debug;
1508      const struct ecoff_debug_swap *swap;
1509      file_ptr where;
1510 {
1511   HDRR * const symhdr = &debug->symbolic_header;
1512   char *buff = NULL;
1513
1514   ecoff_align_debug (abfd, debug, swap);
1515
1516   /* Go to the right location in the file.  */
1517   if (bfd_seek (abfd, where, SEEK_SET) != 0)
1518     return FALSE;
1519
1520   where += swap->external_hdr_size;
1521
1522   symhdr->magic = swap->sym_magic;
1523
1524   /* Fill in the file offsets.  */
1525 #define SET(offset, count, size) \
1526   if (symhdr->count == 0) \
1527     symhdr->offset = 0; \
1528   else \
1529     { \
1530       symhdr->offset = where; \
1531       where += symhdr->count * size; \
1532     }
1533
1534   SET (cbLineOffset, cbLine, sizeof (unsigned char));
1535   SET (cbDnOffset, idnMax, swap->external_dnr_size);
1536   SET (cbPdOffset, ipdMax, swap->external_pdr_size);
1537   SET (cbSymOffset, isymMax, swap->external_sym_size);
1538   SET (cbOptOffset, ioptMax, swap->external_opt_size);
1539   SET (cbAuxOffset, iauxMax, sizeof (union aux_ext));
1540   SET (cbSsOffset, issMax, sizeof (char));
1541   SET (cbSsExtOffset, issExtMax, sizeof (char));
1542   SET (cbFdOffset, ifdMax, swap->external_fdr_size);
1543   SET (cbRfdOffset, crfd, swap->external_rfd_size);
1544   SET (cbExtOffset, iextMax, swap->external_ext_size);
1545 #undef SET
1546
1547   buff = (PTR) bfd_malloc (swap->external_hdr_size);
1548   if (buff == NULL && swap->external_hdr_size != 0)
1549     goto error_return;
1550
1551   (*swap->swap_hdr_out) (abfd, symhdr, buff);
1552   if (bfd_bwrite (buff, swap->external_hdr_size, abfd)
1553       != swap->external_hdr_size)
1554     goto error_return;
1555
1556   if (buff != NULL)
1557     free (buff);
1558   return TRUE;
1559  error_return:
1560   if (buff != NULL)
1561     free (buff);
1562   return FALSE;
1563 }
1564
1565 /* Write out the ECOFF debugging information.  This function assumes
1566    that the information (the pointers and counts) in *DEBUG have been
1567    set correctly.  WHERE is the position in the file to write the
1568    information to.  This function fills in the file offsets in the
1569    symbolic header.  */
1570
1571 bfd_boolean
1572 bfd_ecoff_write_debug (abfd, debug, swap, where)
1573      bfd *abfd;
1574      struct ecoff_debug_info *debug;
1575      const struct ecoff_debug_swap *swap;
1576      file_ptr where;
1577 {
1578   HDRR * const symhdr = &debug->symbolic_header;
1579
1580   if (! ecoff_write_symhdr (abfd, debug, swap, where))
1581     return FALSE;
1582
1583 #define WRITE(ptr, count, size, offset) \
1584   BFD_ASSERT (symhdr->offset == 0 \
1585               || (bfd_vma) bfd_tell (abfd) == symhdr->offset); \
1586   if (bfd_bwrite ((PTR) debug->ptr, (bfd_size_type) size * symhdr->count, abfd)\
1587       != size * symhdr->count) \
1588     return FALSE;
1589
1590   WRITE (line, cbLine, sizeof (unsigned char), cbLineOffset);
1591   WRITE (external_dnr, idnMax, swap->external_dnr_size, cbDnOffset);
1592   WRITE (external_pdr, ipdMax, swap->external_pdr_size, cbPdOffset);
1593   WRITE (external_sym, isymMax, swap->external_sym_size, cbSymOffset);
1594   WRITE (external_opt, ioptMax, swap->external_opt_size, cbOptOffset);
1595   WRITE (external_aux, iauxMax, (bfd_size_type) sizeof (union aux_ext),
1596          cbAuxOffset);
1597   WRITE (ss, issMax, sizeof (char), cbSsOffset);
1598   WRITE (ssext, issExtMax, sizeof (char), cbSsExtOffset);
1599   WRITE (external_fdr, ifdMax, swap->external_fdr_size, cbFdOffset);
1600   WRITE (external_rfd, crfd, swap->external_rfd_size, cbRfdOffset);
1601   WRITE (external_ext, iextMax, swap->external_ext_size, cbExtOffset);
1602 #undef WRITE
1603
1604   return TRUE;
1605 }
1606
1607 /* Write out a shuffle list.  */
1608
1609 static bfd_boolean ecoff_write_shuffle
1610   PARAMS ((bfd *, const struct ecoff_debug_swap *, struct shuffle *,
1611            PTR space));
1612
1613 static bfd_boolean
1614 ecoff_write_shuffle (abfd, swap, shuffle, space)
1615      bfd *abfd;
1616      const struct ecoff_debug_swap *swap;
1617      struct shuffle *shuffle;
1618      PTR space;
1619 {
1620   register struct shuffle *l;
1621   unsigned long total;
1622
1623   total = 0;
1624   for (l = shuffle; l != (struct shuffle *) NULL; l = l->next)
1625     {
1626       if (! l->filep)
1627         {
1628           if (bfd_bwrite (l->u.memory, (bfd_size_type) l->size, abfd)
1629               != l->size)
1630             return FALSE;
1631         }
1632       else
1633         {
1634           if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
1635               || bfd_bread (space, (bfd_size_type) l->size,
1636                            l->u.file.input_bfd) != l->size
1637               || bfd_bwrite (space, (bfd_size_type) l->size, abfd) != l->size)
1638             return FALSE;
1639         }
1640       total += l->size;
1641     }
1642
1643   if ((total & (swap->debug_align - 1)) != 0)
1644     {
1645       unsigned int i;
1646       bfd_byte *s;
1647
1648       i = swap->debug_align - (total & (swap->debug_align - 1));
1649       s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
1650       if (s == NULL && i != 0)
1651         return FALSE;
1652
1653       if (bfd_bwrite ((PTR) s, (bfd_size_type) i, abfd) != i)
1654         {
1655           free (s);
1656           return FALSE;
1657         }
1658       free (s);
1659     }
1660
1661   return TRUE;
1662 }
1663
1664 /* Write out debugging information using accumulated linker
1665    information.  */
1666
1667 bfd_boolean
1668 bfd_ecoff_write_accumulated_debug (handle, abfd, debug, swap, info, where)
1669      PTR handle;
1670      bfd *abfd;
1671      struct ecoff_debug_info *debug;
1672      const struct ecoff_debug_swap *swap;
1673      struct bfd_link_info *info;
1674      file_ptr where;
1675 {
1676   struct accumulate *ainfo = (struct accumulate *) handle;
1677   PTR space = NULL;
1678   bfd_size_type amt;
1679
1680   if (! ecoff_write_symhdr (abfd, debug, swap, where))
1681     goto error_return;
1682
1683   amt = ainfo->largest_file_shuffle;
1684   space = (PTR) bfd_malloc (amt);
1685   if (space == NULL && ainfo->largest_file_shuffle != 0)
1686     goto error_return;
1687
1688   if (! ecoff_write_shuffle (abfd, swap, ainfo->line, space)
1689       || ! ecoff_write_shuffle (abfd, swap, ainfo->pdr, space)
1690       || ! ecoff_write_shuffle (abfd, swap, ainfo->sym, space)
1691       || ! ecoff_write_shuffle (abfd, swap, ainfo->opt, space)
1692       || ! ecoff_write_shuffle (abfd, swap, ainfo->aux, space))
1693     goto error_return;
1694
1695   /* The string table is written out from the hash table if this is a
1696      final link.  */
1697   if (info->relocateable)
1698     {
1699       BFD_ASSERT (ainfo->ss_hash == (struct string_hash_entry *) NULL);
1700       if (! ecoff_write_shuffle (abfd, swap, ainfo->ss, space))
1701         goto error_return;
1702     }
1703   else
1704     {
1705       unsigned long total;
1706       bfd_byte null;
1707       struct string_hash_entry *sh;
1708
1709       BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
1710       null = 0;
1711       if (bfd_bwrite ((PTR) &null, (bfd_size_type) 1, abfd) != 1)
1712         goto error_return;
1713       total = 1;
1714       BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
1715       for (sh = ainfo->ss_hash;
1716            sh != (struct string_hash_entry *) NULL;
1717            sh = sh->next)
1718         {
1719           size_t len;
1720
1721           len = strlen (sh->root.string);
1722           amt = len + 1;
1723           if (bfd_bwrite ((PTR) sh->root.string, amt, abfd) != amt)
1724             goto error_return;
1725           total += len + 1;
1726         }
1727
1728       if ((total & (swap->debug_align - 1)) != 0)
1729         {
1730           unsigned int i;
1731           bfd_byte *s;
1732
1733           i = swap->debug_align - (total & (swap->debug_align - 1));
1734           s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
1735           if (s == NULL && i != 0)
1736             goto error_return;
1737
1738           if (bfd_bwrite ((PTR) s, (bfd_size_type) i, abfd) != i)
1739             {
1740               free (s);
1741               goto error_return;
1742             }
1743           free (s);
1744         }
1745     }
1746
1747   /* The external strings and symbol are not converted over to using
1748      shuffles.  FIXME: They probably should be.  */
1749   amt = debug->symbolic_header.issExtMax;
1750   if (bfd_bwrite (debug->ssext, amt, abfd) != amt)
1751     goto error_return;
1752   if ((debug->symbolic_header.issExtMax & (swap->debug_align - 1)) != 0)
1753     {
1754       unsigned int i;
1755       bfd_byte *s;
1756
1757       i = (swap->debug_align
1758            - (debug->symbolic_header.issExtMax & (swap->debug_align - 1)));
1759       s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
1760       if (s == NULL && i != 0)
1761         goto error_return;
1762
1763       if (bfd_bwrite ((PTR) s, (bfd_size_type) i, abfd) != i)
1764         {
1765           free (s);
1766           goto error_return;
1767         }
1768       free (s);
1769     }
1770
1771   if (! ecoff_write_shuffle (abfd, swap, ainfo->fdr, space)
1772       || ! ecoff_write_shuffle (abfd, swap, ainfo->rfd, space))
1773     goto error_return;
1774
1775   BFD_ASSERT (debug->symbolic_header.cbExtOffset == 0
1776               || (debug->symbolic_header.cbExtOffset
1777                   == (bfd_vma) bfd_tell (abfd)));
1778
1779   amt = debug->symbolic_header.iextMax * swap->external_ext_size;
1780   if (bfd_bwrite (debug->external_ext, amt, abfd) != amt)
1781     goto error_return;
1782
1783   if (space != NULL)
1784     free (space);
1785   return TRUE;
1786
1787  error_return:
1788   if (space != NULL)
1789     free (space);
1790   return FALSE;
1791 }
1792 \f
1793 /* Handle the find_nearest_line function for both ECOFF and MIPS ELF
1794    files.  */
1795
1796 /* Compare FDR entries.  This is called via qsort.  */
1797
1798 static int
1799 cmp_fdrtab_entry (leftp, rightp)
1800      const PTR leftp;
1801      const PTR rightp;
1802 {
1803   const struct ecoff_fdrtab_entry *lp =
1804     (const struct ecoff_fdrtab_entry *) leftp;
1805   const struct ecoff_fdrtab_entry *rp =
1806     (const struct ecoff_fdrtab_entry *) rightp;
1807
1808   if (lp->base_addr < rp->base_addr)
1809     return -1;
1810   if (lp->base_addr > rp->base_addr)
1811     return 1;
1812   return 0;
1813 }
1814
1815 /* Each file descriptor (FDR) has a memory address, to simplify
1816    looking up an FDR by address, we build a table covering all FDRs
1817    that have a least one procedure descriptor in them.  The final
1818    table will be sorted by address so we can look it up via binary
1819    search.  */
1820
1821 static bfd_boolean
1822 mk_fdrtab (abfd, debug_info, debug_swap, line_info)
1823      bfd *abfd;
1824      struct ecoff_debug_info * const debug_info;
1825      const struct ecoff_debug_swap * const debug_swap;
1826      struct ecoff_find_line *line_info;
1827 {
1828   struct ecoff_fdrtab_entry *tab;
1829   FDR *fdr_ptr;
1830   FDR *fdr_start;
1831   FDR *fdr_end;
1832   bfd_boolean stabs;
1833   long len;
1834   bfd_size_type amt;
1835
1836   fdr_start = debug_info->fdr;
1837   fdr_end = fdr_start + debug_info->symbolic_header.ifdMax;
1838
1839   /* First, let's see how long the table needs to be: */
1840   for (len = 0, fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1841     {
1842       if (fdr_ptr->cpd == 0)    /* skip FDRs that have no PDRs */
1843         continue;
1844       ++len;
1845     }
1846
1847   /* Now, create and fill in the table: */
1848
1849   amt = (bfd_size_type) len * sizeof (struct ecoff_fdrtab_entry);
1850   line_info->fdrtab = (struct ecoff_fdrtab_entry*) bfd_zalloc (abfd, amt);
1851   if (line_info->fdrtab == NULL)
1852     return FALSE;
1853   line_info->fdrtab_len = len;
1854
1855   tab = line_info->fdrtab;
1856   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1857     {
1858       if (fdr_ptr->cpd == 0)
1859         continue;
1860
1861       /* Check whether this file has stabs debugging information.  In
1862          a file with stabs debugging information, the second local
1863          symbol is named @stabs.  */
1864       stabs = FALSE;
1865       if (fdr_ptr->csym >= 2)
1866         {
1867           char *sym_ptr;
1868           SYMR sym;
1869
1870           sym_ptr = ((char *) debug_info->external_sym
1871                      + (fdr_ptr->isymBase + 1)*debug_swap->external_sym_size);
1872           (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
1873           if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
1874                       STABS_SYMBOL) == 0)
1875             stabs = TRUE;
1876         }
1877
1878       if (!stabs)
1879         {
1880           bfd_size_type external_pdr_size;
1881           char *pdr_ptr;
1882           PDR pdr;
1883
1884           external_pdr_size = debug_swap->external_pdr_size;
1885
1886           pdr_ptr = ((char *) debug_info->external_pdr
1887                      + fdr_ptr->ipdFirst * external_pdr_size);
1888           (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
1889           /* The address of the first PDR is the offset of that
1890              procedure relative to the beginning of file FDR.  */
1891           tab->base_addr = fdr_ptr->adr - pdr.adr;
1892         }
1893       else
1894         {
1895           /* XXX I don't know about stabs, so this is a guess
1896              (davidm@cs.arizona.edu): */
1897           tab->base_addr = fdr_ptr->adr;
1898         }
1899       tab->fdr = fdr_ptr;
1900       ++tab;
1901     }
1902
1903   /* Finally, the table is sorted in increasing memory-address order.
1904      The table is mostly sorted already, but there are cases (e.g.,
1905      static functions in include files), where this does not hold.
1906      Use "odump -PFv" to verify...  */
1907   qsort ((PTR) line_info->fdrtab, (size_t) len,
1908          sizeof (struct ecoff_fdrtab_entry), cmp_fdrtab_entry);
1909
1910   return TRUE;
1911 }
1912
1913 /* Return index of first FDR that covers to OFFSET.  */
1914
1915 static long
1916 fdrtab_lookup (line_info, offset)
1917      struct ecoff_find_line *line_info;
1918      bfd_vma offset;
1919 {
1920   long low, high, len;
1921   long mid = -1;
1922   struct ecoff_fdrtab_entry *tab;
1923
1924   len = line_info->fdrtab_len;
1925   if (len == 0)
1926     return -1;
1927
1928   tab = line_info->fdrtab;
1929   for (low = 0, high = len - 1 ; low != high ;)
1930     {
1931       mid = (high + low) / 2;
1932       if (offset >= tab[mid].base_addr && offset < tab[mid + 1].base_addr)
1933         goto find_min;
1934
1935       if (tab[mid].base_addr > offset)
1936         high = mid;
1937       else
1938         low = mid + 1;
1939     }
1940   ++mid;
1941
1942   /* last entry is catch-all for all higher addresses: */
1943   if (offset < tab[mid].base_addr)
1944     return -1;
1945
1946  find_min:
1947
1948   while (mid > 0 && tab[mid - 1].base_addr == tab[mid].base_addr)
1949     --mid;
1950
1951   return mid;
1952 }
1953
1954 /* Look up a line given an address, storing the information in
1955    LINE_INFO->cache.  */
1956
1957 static bfd_boolean
1958 lookup_line (abfd, debug_info, debug_swap, line_info)
1959      bfd *abfd;
1960      struct ecoff_debug_info * const debug_info;
1961      const struct ecoff_debug_swap * const debug_swap;
1962      struct ecoff_find_line *line_info;
1963 {
1964   struct ecoff_fdrtab_entry *tab;
1965   bfd_vma offset;
1966   bfd_boolean stabs;
1967   FDR *fdr_ptr;
1968   int i;
1969
1970   offset = line_info->cache.start;
1971
1972   /* Build FDR table (sorted by object file's base-address) if we
1973      don't have it already.  */
1974   if (line_info->fdrtab == NULL
1975       && !mk_fdrtab (abfd, debug_info, debug_swap, line_info))
1976     return FALSE;
1977
1978   tab = line_info->fdrtab;
1979
1980   /* find first FDR for address OFFSET */
1981   i = fdrtab_lookup (line_info, offset);
1982   if (i < 0)
1983     return FALSE;               /* no FDR, no fun...  */
1984   fdr_ptr = tab[i].fdr;
1985
1986   /* Check whether this file has stabs debugging information.  In a
1987      file with stabs debugging information, the second local symbol is
1988      named @stabs.  */
1989   stabs = FALSE;
1990   if (fdr_ptr->csym >= 2)
1991     {
1992       char *sym_ptr;
1993       SYMR sym;
1994
1995       sym_ptr = ((char *) debug_info->external_sym
1996                  + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
1997       (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
1998       if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
1999                   STABS_SYMBOL) == 0)
2000         stabs = TRUE;
2001     }
2002
2003   if (!stabs)
2004     {
2005       bfd_size_type external_pdr_size;
2006       char *pdr_ptr;
2007       char *best_pdr = NULL;
2008       FDR *best_fdr;
2009       bfd_vma best_dist = ~(bfd_vma) 0;
2010       PDR pdr;
2011       unsigned char *line_ptr;
2012       unsigned char *line_end;
2013       int lineno;
2014       /* This file uses ECOFF debugging information.  Each FDR has a
2015          list of procedure descriptors (PDR).  The address in the FDR
2016          is the absolute address of the first procedure.  The address
2017          in the first PDR gives the offset of that procedure relative
2018          to the object file's base-address.  The addresses in
2019          subsequent PDRs specify each procedure's address relative to
2020          the object file's base-address.  To make things more juicy,
2021          whenever the PROF bit in the PDR is set, the real entry point
2022          of the procedure may be 16 bytes below what would normally be
2023          the procedure's entry point.  Instead, DEC came up with a
2024          wicked scheme to create profiled libraries "on the fly":
2025          instead of shipping a regular and a profiled version of each
2026          library, they insert 16 bytes of unused space in front of
2027          each procedure and set the "prof" bit in the PDR to indicate
2028          that there is a gap there (this is done automagically by "as"
2029          when option "-pg" is specified).  Thus, normally, you link
2030          against such a library and, except for lots of 16 byte gaps
2031          between functions, things will behave as usual.  However,
2032          when invoking "ld" with option "-pg", it will fill those gaps
2033          with code that calls mcount().  It then moves the function's
2034          entry point down by 16 bytes, and out pops a binary that has
2035          all functions profiled.
2036
2037          NOTE: Neither FDRs nor PDRs are strictly sorted in memory
2038                order.  For example, when including header-files that
2039                define functions, the FDRs follow behind the including
2040                file, even though their code may have been generated at
2041                a lower address.  File coff-alpha.c from libbfd
2042                illustrates this (use "odump -PFv" to look at a file's
2043                FDR/PDR).  Similarly, PDRs are sometimes out of order
2044                as well.  An example of this is OSF/1 v3.0 libc's
2045                malloc.c.  I'm not sure why this happens, but it could
2046                be due to optimizations that reorder a function's
2047                position within an object-file.
2048
2049          Strategy:
2050
2051          On the first call to this function, we build a table of FDRs
2052          that is sorted by the base-address of the object-file the FDR
2053          is referring to.  Notice that each object-file may contain
2054          code from multiple source files (e.g., due to code defined in
2055          include files).  Thus, for any given base-address, there may
2056          be multiple FDRs (but this case is, fortunately, uncommon).
2057          lookup(addr) guarantees to return the first FDR that applies
2058          to address ADDR.  Thus, after invoking lookup(), we have a
2059          list of FDRs that may contain the PDR for ADDR.  Next, we
2060          walk through the PDRs of these FDRs and locate the one that
2061          is closest to ADDR (i.e., for which the difference between
2062          ADDR and the PDR's entry point is positive and minimal).
2063          Once, the right FDR and PDR are located, we simply walk
2064          through the line-number table to lookup the line-number that
2065          best matches ADDR.  Obviously, things could be sped up by
2066          keeping a sorted list of PDRs instead of a sorted list of
2067          FDRs.  However, this would increase space requirements
2068          considerably, which is undesirable.  */
2069       external_pdr_size = debug_swap->external_pdr_size;
2070
2071       /* Make offset relative to object file's start-address: */
2072       offset -= tab[i].base_addr;
2073       /* Search FDR list starting at tab[i] for the PDR that best matches
2074          OFFSET.  Normally, the FDR list is only one entry long.  */
2075       best_fdr = NULL;
2076       do
2077         {
2078           bfd_vma dist, min_dist = 0;
2079           char *pdr_hold;
2080           char *pdr_end;
2081
2082           fdr_ptr = tab[i].fdr;
2083
2084           pdr_ptr = ((char *) debug_info->external_pdr
2085                      + fdr_ptr->ipdFirst * external_pdr_size);
2086           pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2087           (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2088           /* Find PDR that is closest to OFFSET.  If pdr.prof is set,
2089              the procedure entry-point *may* be 0x10 below pdr.adr.  We
2090              simply pretend that pdr.prof *implies* a lower entry-point.
2091              This is safe because it just means that may identify 4 NOPs
2092              in front of the function as belonging to the function.  */
2093           for (pdr_hold = NULL;
2094                pdr_ptr < pdr_end;
2095                (pdr_ptr += external_pdr_size,
2096                 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr)))
2097             {
2098               if (offset >= (pdr.adr - 0x10 * pdr.prof))
2099                 {
2100                   dist = offset - (pdr.adr - 0x10 * pdr.prof);
2101                   if (!pdr_hold || dist < min_dist)
2102                     {
2103                       min_dist = dist;
2104                       pdr_hold = pdr_ptr;
2105                     }
2106                 }
2107             }
2108
2109           if (!best_pdr || min_dist < best_dist)
2110             {
2111               best_dist = min_dist;
2112               best_fdr = fdr_ptr;
2113               best_pdr = pdr_hold;
2114             }
2115           /* continue looping until base_addr of next entry is different: */
2116         }
2117       while (++i < line_info->fdrtab_len
2118              && tab[i].base_addr == tab[i - 1].base_addr);
2119
2120       if (!best_fdr || !best_pdr)
2121         return FALSE;                   /* shouldn't happen...  */
2122
2123       /* phew, finally we got something that we can hold onto: */
2124       fdr_ptr = best_fdr;
2125       pdr_ptr = best_pdr;
2126       (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2127       /* Now we can look for the actual line number.  The line numbers
2128          are stored in a very funky format, which I won't try to
2129          describe.  The search is bounded by the end of the FDRs line
2130          number entries.  */
2131       line_end = debug_info->line + fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2132
2133       /* Make offset relative to procedure entry: */
2134       offset -= pdr.adr - 0x10 * pdr.prof;
2135       lineno = pdr.lnLow;
2136       line_ptr = debug_info->line + fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2137       while (line_ptr < line_end)
2138         {
2139           int delta;
2140           unsigned int count;
2141
2142           delta = *line_ptr >> 4;
2143           if (delta >= 0x8)
2144             delta -= 0x10;
2145           count = (*line_ptr & 0xf) + 1;
2146           ++line_ptr;
2147           if (delta == -8)
2148             {
2149               delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2150               if (delta >= 0x8000)
2151                 delta -= 0x10000;
2152               line_ptr += 2;
2153             }
2154           lineno += delta;
2155           if (offset < count * 4)
2156             {
2157               line_info->cache.stop += count * 4 - offset;
2158               break;
2159             }
2160           offset -= count * 4;
2161         }
2162
2163       /* If fdr_ptr->rss is -1, then this file does not have full
2164          symbols, at least according to gdb/mipsread.c.  */
2165       if (fdr_ptr->rss == -1)
2166         {
2167           line_info->cache.filename = NULL;
2168           if (pdr.isym == -1)
2169             line_info->cache.functionname = NULL;
2170           else
2171             {
2172               EXTR proc_ext;
2173
2174               (*debug_swap->swap_ext_in)
2175                 (abfd,
2176                  ((char *) debug_info->external_ext
2177                   + pdr.isym * debug_swap->external_ext_size),
2178                  &proc_ext);
2179               line_info->cache.functionname = (debug_info->ssext
2180                                                + proc_ext.asym.iss);
2181             }
2182         }
2183       else
2184         {
2185           SYMR proc_sym;
2186
2187           line_info->cache.filename = (debug_info->ss
2188                                        + fdr_ptr->issBase
2189                                        + fdr_ptr->rss);
2190           (*debug_swap->swap_sym_in)
2191             (abfd,
2192              ((char *) debug_info->external_sym
2193               + ((fdr_ptr->isymBase + pdr.isym)
2194                  * debug_swap->external_sym_size)),
2195              &proc_sym);
2196           line_info->cache.functionname = (debug_info->ss
2197                                            + fdr_ptr->issBase
2198                                            + proc_sym.iss);
2199         }
2200       if (lineno == ilineNil)
2201         lineno = 0;
2202       line_info->cache.line_num = lineno;
2203     }
2204   else
2205     {
2206       bfd_size_type external_sym_size;
2207       const char *directory_name;
2208       const char *main_file_name;
2209       const char *current_file_name;
2210       const char *function_name;
2211       const char *line_file_name;
2212       bfd_vma low_func_vma;
2213       bfd_vma low_line_vma;
2214       bfd_boolean past_line;
2215       bfd_boolean past_fn;
2216       char *sym_ptr, *sym_ptr_end;
2217       size_t len, funclen;
2218       char *buffer = NULL;
2219
2220       /* This file uses stabs debugging information.  When gcc is not
2221          optimizing, it will put the line number information before
2222          the function name stabs entry.  When gcc is optimizing, it
2223          will put the stabs entry for all the function first, followed
2224          by the line number information.  (This appears to happen
2225          because of the two output files used by the -mgpopt switch,
2226          which is implied by -O).  This means that we must keep
2227          looking through the symbols until we find both a line number
2228          and a function name which are beyond the address we want.  */
2229
2230       line_info->cache.filename = NULL;
2231       line_info->cache.functionname = NULL;
2232       line_info->cache.line_num = 0;
2233
2234       directory_name = NULL;
2235       main_file_name = NULL;
2236       current_file_name = NULL;
2237       function_name = NULL;
2238       line_file_name = NULL;
2239       low_func_vma = 0;
2240       low_line_vma = 0;
2241       past_line = FALSE;
2242       past_fn = FALSE;
2243
2244       external_sym_size = debug_swap->external_sym_size;
2245
2246       sym_ptr = ((char *) debug_info->external_sym
2247                  + (fdr_ptr->isymBase + 2) * external_sym_size);
2248       sym_ptr_end = sym_ptr + (fdr_ptr->csym - 2) * external_sym_size;
2249       for (;
2250            sym_ptr < sym_ptr_end && (! past_line || ! past_fn);
2251            sym_ptr += external_sym_size)
2252         {
2253           SYMR sym;
2254
2255           (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
2256
2257           if (ECOFF_IS_STAB (&sym))
2258             {
2259               switch (ECOFF_UNMARK_STAB (sym.index))
2260                 {
2261                 case N_SO:
2262                   main_file_name = current_file_name =
2263                     debug_info->ss + fdr_ptr->issBase + sym.iss;
2264
2265                   /* Check the next symbol to see if it is also an
2266                      N_SO symbol.  */
2267                   if (sym_ptr + external_sym_size < sym_ptr_end)
2268                     {
2269                       SYMR nextsym;
2270
2271                       (*debug_swap->swap_sym_in) (abfd,
2272                                                   sym_ptr + external_sym_size,
2273                                                   &nextsym);
2274                       if (ECOFF_IS_STAB (&nextsym)
2275                           && ECOFF_UNMARK_STAB (nextsym.index) == N_SO)
2276                         {
2277                           directory_name = current_file_name;
2278                           main_file_name = current_file_name =
2279                             debug_info->ss + fdr_ptr->issBase + nextsym.iss;
2280                           sym_ptr += external_sym_size;
2281                         }
2282                     }
2283                   break;
2284
2285                 case N_SOL:
2286                   current_file_name =
2287                     debug_info->ss + fdr_ptr->issBase + sym.iss;
2288                   break;
2289
2290                 case N_FUN:
2291                   if (sym.value > offset)
2292                     past_fn = TRUE;
2293                   else if (sym.value >= low_func_vma)
2294                     {
2295                       low_func_vma = sym.value;
2296                       function_name =
2297                         debug_info->ss + fdr_ptr->issBase + sym.iss;
2298                     }
2299                   break;
2300                 }
2301             }
2302           else if (sym.st == stLabel && sym.index != indexNil)
2303             {
2304               if (sym.value > offset)
2305                 past_line = TRUE;
2306               else if (sym.value >= low_line_vma)
2307                 {
2308                   low_line_vma = sym.value;
2309                   line_file_name = current_file_name;
2310                   line_info->cache.line_num = sym.index;
2311                 }
2312             }
2313         }
2314
2315       if (line_info->cache.line_num != 0)
2316         main_file_name = line_file_name;
2317
2318       /* We need to remove the stuff after the colon in the function
2319          name.  We also need to put the directory name and the file
2320          name together.  */
2321       if (function_name == NULL)
2322         len = funclen = 0;
2323       else
2324         len = funclen = strlen (function_name) + 1;
2325
2326       if (main_file_name != NULL
2327           && directory_name != NULL
2328           && main_file_name[0] != '/')
2329         len += strlen (directory_name) + strlen (main_file_name) + 1;
2330
2331       if (len != 0)
2332         {
2333           if (line_info->find_buffer != NULL)
2334             free (line_info->find_buffer);
2335           buffer = (char *) bfd_malloc ((bfd_size_type) len);
2336           if (buffer == NULL)
2337             return FALSE;
2338           line_info->find_buffer = buffer;
2339         }
2340
2341       if (function_name != NULL)
2342         {
2343           char *colon;
2344
2345           strcpy (buffer, function_name);
2346           colon = strchr (buffer, ':');
2347           if (colon != NULL)
2348             *colon = '\0';
2349           line_info->cache.functionname = buffer;
2350         }
2351
2352       if (main_file_name != NULL)
2353         {
2354           if (directory_name == NULL || main_file_name[0] == '/')
2355             line_info->cache.filename = main_file_name;
2356           else
2357             {
2358               sprintf (buffer + funclen, "%s%s", directory_name,
2359                        main_file_name);
2360               line_info->cache.filename = buffer + funclen;
2361             }
2362         }
2363     }
2364
2365   return TRUE;
2366 }
2367
2368 /* Do the work of find_nearest_line.  */
2369
2370 bfd_boolean
2371 _bfd_ecoff_locate_line (abfd, section, offset, debug_info, debug_swap,
2372                         line_info, filename_ptr, functionname_ptr, retline_ptr)
2373      bfd *abfd;
2374      asection *section;
2375      bfd_vma offset;
2376      struct ecoff_debug_info * const debug_info;
2377      const struct ecoff_debug_swap * const debug_swap;
2378      struct ecoff_find_line *line_info;
2379      const char **filename_ptr;
2380      const char **functionname_ptr;
2381      unsigned int *retline_ptr;
2382 {
2383   offset += section->vma;
2384
2385   if (line_info->cache.sect == NULL
2386       || line_info->cache.sect != section
2387       || offset < line_info->cache.start
2388       || offset >= line_info->cache.stop)
2389     {
2390       line_info->cache.sect = section;
2391       line_info->cache.start = offset;
2392       line_info->cache.stop = offset;
2393       if (! lookup_line (abfd, debug_info, debug_swap, line_info))
2394         {
2395           line_info->cache.sect = NULL;
2396           return FALSE;
2397         }
2398     }
2399
2400   *filename_ptr = line_info->cache.filename;
2401   *functionname_ptr = line_info->cache.functionname;
2402   *retline_ptr = line_info->cache.line_num;
2403
2404   return TRUE;
2405 }
2406 \f
2407 /* These routines copy symbolic information into a memory buffer.
2408
2409    FIXME: The whole point of the shuffle code is to avoid storing
2410    everything in memory, since the linker is such a memory hog.  This
2411    code makes that effort useless.  It is only called by the MIPS ELF
2412    code when generating a shared library, so it is not that big a
2413    deal, but it should be fixed eventually.  */
2414
2415 /* Collect a shuffle into a memory buffer.  */
2416
2417 static bfd_boolean ecoff_collect_shuffle
2418   PARAMS ((struct shuffle *, bfd_byte *));
2419
2420 static bfd_boolean
2421 ecoff_collect_shuffle (l, buff)
2422      struct shuffle *l;
2423      bfd_byte *buff;
2424 {
2425   unsigned long total;
2426
2427   total = 0;
2428   for (; l != (struct shuffle *) NULL; l = l->next)
2429     {
2430       if (! l->filep)
2431         memcpy (buff, l->u.memory, l->size);
2432       else
2433         {
2434           if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
2435               || (bfd_bread (buff, (bfd_size_type) l->size, l->u.file.input_bfd)
2436                   != l->size))
2437             return FALSE;
2438         }
2439       total += l->size;
2440       buff += l->size;
2441     }
2442
2443   return TRUE;
2444 }
2445
2446 /* Copy PDR information into a memory buffer.  */
2447
2448 bfd_boolean
2449 _bfd_ecoff_get_accumulated_pdr (handle, buff)
2450      PTR handle;
2451      bfd_byte *buff;
2452 {
2453   struct accumulate *ainfo = (struct accumulate *) handle;
2454
2455   return ecoff_collect_shuffle (ainfo->pdr, buff);
2456 }
2457
2458 /* Copy symbol information into a memory buffer.  */
2459
2460 bfd_boolean
2461 _bfd_ecoff_get_accumulated_sym (handle, buff)
2462      PTR handle;
2463      bfd_byte *buff;
2464 {
2465   struct accumulate *ainfo = (struct accumulate *) handle;
2466
2467   return ecoff_collect_shuffle (ainfo->sym, buff);
2468 }
2469
2470 /* Copy the string table into a memory buffer.  */
2471
2472 bfd_boolean
2473 _bfd_ecoff_get_accumulated_ss (handle, buff)
2474      PTR handle;
2475      bfd_byte *buff;
2476 {
2477   struct accumulate *ainfo = (struct accumulate *) handle;
2478   struct string_hash_entry *sh;
2479   unsigned long total;
2480
2481   /* The string table is written out from the hash table if this is a
2482      final link.  */
2483   BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
2484   *buff++ = '\0';
2485   total = 1;
2486   BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
2487   for (sh = ainfo->ss_hash;
2488        sh != (struct string_hash_entry *) NULL;
2489        sh = sh->next)
2490     {
2491       size_t len;
2492
2493       len = strlen (sh->root.string);
2494       memcpy (buff, (PTR) sh->root.string, len + 1);
2495       total += len + 1;
2496       buff += len + 1;
2497     }
2498
2499   return TRUE;
2500 }