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