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