* libbfd.c (bfd_malloc, bfd_realloc): New functions.
[external/binutils.git] / bfd / ecofflink.c
1 /* Routines to link ECOFF debugging information.
2    Copyright 1993 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor, Cygnus Support, <ian@cygnus.com>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "obstack.h"
26 #include "aout/stab_gnu.h"
27 #include "coff/internal.h"
28 #include "coff/sym.h"
29 #include "coff/symconst.h"
30 #include "coff/ecoff.h"
31 \f
32 static boolean ecoff_add_bytes PARAMS ((char **buf, char **bufend,
33                                         size_t need));
34 static struct bfd_hash_entry *string_hash_newfunc
35   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
36            const char *));
37 static void ecoff_align_debug PARAMS ((bfd *abfd,
38                                        struct ecoff_debug_info *debug,
39                                        const struct ecoff_debug_swap *swap));
40 static boolean ecoff_write_symhdr PARAMS ((bfd *, struct ecoff_debug_info *,
41                                            const struct ecoff_debug_swap *,
42                                            file_ptr where));
43 static int cmp_fdrtab_entry PARAMS ((const PTR, const PTR));
44 static boolean mk_fdrtab PARAMS ((bfd *,
45                                   struct ecoff_debug_info * const,
46                                   const struct ecoff_debug_swap * const,
47                                   struct ecoff_find_line *));
48 static long fdrtab_lookup PARAMS ((struct ecoff_find_line *, bfd_vma));
49
50 /* Obstack allocation and deallocation routines.  */
51 #define obstack_chunk_alloc malloc
52 #define obstack_chunk_free free
53 \f
54 /* Routines to swap auxiliary information in and out.  I am assuming
55    that the auxiliary information format is always going to be target
56    independent.  */
57
58 /* Swap in a type information record.
59    BIGEND says whether AUX symbols are big-endian or little-endian; this
60    info comes from the file header record (fh-fBigendian).  */
61
62 void
63 _bfd_ecoff_swap_tir_in (bigend, ext_copy, intern)
64      int bigend;
65      const struct tir_ext *ext_copy;
66      TIR *intern;
67 {
68   struct tir_ext ext[1];
69
70   *ext = *ext_copy;             /* Make it reasonable to do in-place.  */
71   
72   /* now the fun stuff... */
73   if (bigend) {
74     intern->fBitfield   = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
75     intern->continued   = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
76     intern->bt          = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
77                         >>                  TIR_BITS1_BT_SH_BIG;
78     intern->tq4         = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
79                         >>                  TIR_BITS_TQ4_SH_BIG;
80     intern->tq5         = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
81                         >>                  TIR_BITS_TQ5_SH_BIG;
82     intern->tq0         = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
83                         >>                  TIR_BITS_TQ0_SH_BIG;
84     intern->tq1         = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
85                         >>                  TIR_BITS_TQ1_SH_BIG;
86     intern->tq2         = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
87                         >>                  TIR_BITS_TQ2_SH_BIG;
88     intern->tq3         = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
89                         >>                  TIR_BITS_TQ3_SH_BIG;
90   } else {
91     intern->fBitfield   = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
92     intern->continued   = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
93     intern->bt          = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
94                         >>                  TIR_BITS1_BT_SH_LITTLE;
95     intern->tq4         = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
96                         >>                  TIR_BITS_TQ4_SH_LITTLE;
97     intern->tq5         = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
98                         >>                  TIR_BITS_TQ5_SH_LITTLE;
99     intern->tq0         = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
100                         >>                  TIR_BITS_TQ0_SH_LITTLE;
101     intern->tq1         = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
102                         >>                  TIR_BITS_TQ1_SH_LITTLE;
103     intern->tq2         = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
104                         >>                  TIR_BITS_TQ2_SH_LITTLE;
105     intern->tq3         = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
106                         >>                  TIR_BITS_TQ3_SH_LITTLE;
107   }
108
109 #ifdef TEST
110   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
111     abort();
112 #endif
113 }
114
115 /* Swap out a type information record.
116    BIGEND says whether AUX symbols are big-endian or little-endian; this
117    info comes from the file header record (fh-fBigendian).  */
118
119 void
120 _bfd_ecoff_swap_tir_out (bigend, intern_copy, ext)
121      int bigend;
122      const TIR *intern_copy;
123      struct tir_ext *ext;
124 {
125   TIR intern[1];
126
127   *intern = *intern_copy;       /* Make it reasonable to do in-place.  */
128   
129   /* now the fun stuff... */
130   if (bigend) {
131     ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
132                        | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
133                        | ((intern->bt << TIR_BITS1_BT_SH_BIG)
134                           & TIR_BITS1_BT_BIG));
135     ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
136                        & TIR_BITS_TQ4_BIG)
137                       | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
138                          & TIR_BITS_TQ5_BIG));
139     ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
140                        & TIR_BITS_TQ0_BIG)
141                       | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
142                          & TIR_BITS_TQ1_BIG));
143     ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
144                        & TIR_BITS_TQ2_BIG)
145                       | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
146                          & TIR_BITS_TQ3_BIG));
147   } else {
148     ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
149                        | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
150                        | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
151                           & TIR_BITS1_BT_LITTLE));
152     ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
153                        & TIR_BITS_TQ4_LITTLE)
154                       | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
155                          & TIR_BITS_TQ5_LITTLE));
156     ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
157                        & TIR_BITS_TQ0_LITTLE)
158                       | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
159                          & TIR_BITS_TQ1_LITTLE));
160     ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
161                        & TIR_BITS_TQ2_LITTLE)
162                       | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
163                          & TIR_BITS_TQ3_LITTLE));
164   }
165
166 #ifdef TEST
167   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
168     abort();
169 #endif
170 }
171
172 /* Swap in a relative symbol record.  BIGEND says whether it is in
173    big-endian or little-endian format.*/
174
175 void
176 _bfd_ecoff_swap_rndx_in (bigend, ext_copy, intern)
177      int bigend;
178      const struct rndx_ext *ext_copy;
179      RNDXR *intern;
180 {
181   struct rndx_ext ext[1];
182
183   *ext = *ext_copy;             /* Make it reasonable to do in-place.  */
184   
185   /* now the fun stuff... */
186   if (bigend) {
187     intern->rfd   = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
188                   | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
189                                     >> RNDX_BITS1_RFD_SH_BIG);
190     intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
191                                     << RNDX_BITS1_INDEX_SH_LEFT_BIG)
192                   | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
193                   | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
194   } else {
195     intern->rfd   = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
196                   | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
197                                     << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
198     intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
199                                     >> RNDX_BITS1_INDEX_SH_LITTLE)
200                   | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
201                   | ((unsigned int) ext->r_bits[3]
202                      << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
203   }
204
205 #ifdef TEST
206   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
207     abort();
208 #endif
209 }
210
211 /* Swap out a relative symbol record.  BIGEND says whether it is in
212    big-endian or little-endian format.*/
213
214 void
215 _bfd_ecoff_swap_rndx_out (bigend, intern_copy, ext)
216      int bigend;
217      const RNDXR *intern_copy;
218      struct rndx_ext *ext;
219 {
220   RNDXR intern[1];
221
222   *intern = *intern_copy;       /* Make it reasonable to do in-place.  */
223   
224   /* now the fun stuff... */
225   if (bigend) {
226     ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
227     ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
228                        & RNDX_BITS1_RFD_BIG)
229                       | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
230                          & RNDX_BITS1_INDEX_BIG));
231     ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
232     ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
233   } else {
234     ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
235     ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
236                        & RNDX_BITS1_RFD_LITTLE)
237                       | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
238                          & RNDX_BITS1_INDEX_LITTLE));
239     ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
240     ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
241   }
242
243 #ifdef TEST
244   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
245     abort();
246 #endif
247 }
248 \f
249 /* The minimum amount of data to allocate.  */
250 #define ALLOC_SIZE (4064)
251
252 /* Add bytes to a buffer.  Return success.  */
253
254 static boolean
255 ecoff_add_bytes (buf, bufend, need)
256      char **buf;
257      char **bufend;
258      size_t need;
259 {
260   size_t have;
261   size_t want;
262   char *newbuf;
263
264   have = *bufend - *buf;
265   if (have > need)
266     want = ALLOC_SIZE;
267   else
268     {
269       want = need - have;
270       if (want < ALLOC_SIZE)
271         want = ALLOC_SIZE;
272     }
273   newbuf = (char *) bfd_realloc (*buf, have + want);
274   if (newbuf == NULL)
275     return false;
276   *buf = newbuf;
277   *bufend = *buf + have + want;
278   return true;
279 }
280
281 /* We keep a hash table which maps strings to numbers.  We use it to
282    map FDR names to indices in the output file, and to map local
283    strings when combining stabs debugging information.  */
284
285 struct string_hash_entry
286 {
287   struct bfd_hash_entry root;
288   /* FDR index or string table offset.  */
289   long val;
290   /* Next entry in string table.  */
291   struct string_hash_entry *next;
292 };
293
294 struct string_hash_table
295 {
296   struct bfd_hash_table table;
297 };
298
299 /* Routine to create an entry in a string hash table.  */
300
301 static struct bfd_hash_entry *
302 string_hash_newfunc (entry, table, string)
303      struct bfd_hash_entry *entry;
304      struct bfd_hash_table *table;
305      const char *string;
306 {
307   struct string_hash_entry *ret = (struct string_hash_entry *) entry;
308
309   /* Allocate the structure if it has not already been allocated by a
310      subclass.  */
311   if (ret == (struct string_hash_entry *) NULL)
312     ret = ((struct string_hash_entry *)
313            bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
314   if (ret == (struct string_hash_entry *) NULL)
315     return NULL;
316
317   /* Call the allocation method of the superclass.  */
318   ret = ((struct string_hash_entry *)
319          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
320
321   if (ret)
322     {
323       /* Initialize the local fields.  */
324       ret->val = -1;
325       ret->next = NULL;
326     }
327
328   return (struct bfd_hash_entry *) ret;
329 }
330
331 /* Look up an entry in an string hash table.  */
332
333 #define string_hash_lookup(t, string, create, copy) \
334   ((struct string_hash_entry *) \
335    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
336
337 /* We can't afford to read in all the debugging information when we do
338    a link.  Instead, we build a list of these structures to show how
339    different parts of the input file map to the output file.  */
340
341 struct shuffle
342 {
343   /* The next entry in this linked list.  */
344   struct shuffle *next;
345   /* The length of the information.  */
346   unsigned long size;
347   /* Whether this information comes from a file or not.  */
348   boolean filep;
349   union
350     {
351       struct
352         {
353           /* The BFD the data comes from.  */
354           bfd *input_bfd;
355           /* The offset within input_bfd.  */
356           file_ptr offset;
357         } file;
358       /* The data to be written out.  */
359       PTR memory;
360     } u;
361 };
362
363 /* This structure holds information across calls to
364    bfd_ecoff_debug_accumulate.  */
365
366 struct accumulate
367 {
368   /* The FDR hash table.  */
369   struct string_hash_table fdr_hash;
370   /* The strings hash table.  */
371   struct string_hash_table str_hash;
372   /* Linked lists describing how to shuffle the input debug
373      information into the output file.  We keep a pointer to both the
374      head and the tail.  */
375   struct shuffle *line;
376   struct shuffle *line_end;
377   struct shuffle *pdr;
378   struct shuffle *pdr_end;
379   struct shuffle *sym;
380   struct shuffle *sym_end;
381   struct shuffle *opt;
382   struct shuffle *opt_end;
383   struct shuffle *aux;
384   struct shuffle *aux_end;
385   struct shuffle *ss;
386   struct shuffle *ss_end;
387   struct string_hash_entry *ss_hash;
388   struct string_hash_entry *ss_hash_end;
389   struct shuffle *fdr;
390   struct shuffle *fdr_end;
391   struct shuffle *rfd;
392   struct shuffle *rfd_end;
393   /* The size of the largest file shuffle.  */
394   unsigned long largest_file_shuffle;
395   /* An obstack for debugging information.  */
396   struct obstack memory;
397 };
398
399 /* Add a file entry to a shuffle list.  */
400
401 static boolean add_file_shuffle PARAMS ((struct accumulate *,
402                                       struct shuffle **,
403                                       struct shuffle **, bfd *, file_ptr,
404                                       unsigned long));
405
406 static boolean
407 add_file_shuffle (ainfo, head, tail, input_bfd, offset, size)
408      struct accumulate *ainfo;
409      struct shuffle **head;
410      struct shuffle **tail;
411      bfd *input_bfd;
412      file_ptr offset;
413      unsigned long size;
414 {
415   struct shuffle *n;
416
417   if (*tail != (struct shuffle *) NULL
418       && (*tail)->filep
419       && (*tail)->u.file.input_bfd == input_bfd
420       && (*tail)->u.file.offset + (*tail)->size == (unsigned long) offset)
421     {
422       /* Just merge this entry onto the existing one.  */
423       (*tail)->size += size;
424       if ((*tail)->size > ainfo->largest_file_shuffle)
425         ainfo->largest_file_shuffle = (*tail)->size;
426       return true;
427     }
428
429   n = (struct shuffle *) obstack_alloc (&ainfo->memory,
430                                         sizeof (struct shuffle));
431   if (!n)
432     {
433       bfd_set_error (bfd_error_no_memory);
434       return false;
435     }
436   n->next = NULL;
437   n->size = size;
438   n->filep = true;
439   n->u.file.input_bfd = input_bfd;
440   n->u.file.offset = offset;
441   if (*head == (struct shuffle *) NULL)
442     *head = n;
443   if (*tail != (struct shuffle *) NULL)
444     (*tail)->next = n;
445   *tail = n;
446   if (size > ainfo->largest_file_shuffle)
447     ainfo->largest_file_shuffle = size;
448   return true;
449 }
450
451 /* Add a memory entry to a shuffle list.  */
452
453 static boolean add_memory_shuffle PARAMS ((struct accumulate *,
454                                            struct shuffle **head,
455                                            struct shuffle **tail,
456                                            bfd_byte *data, unsigned long size));
457
458 static boolean
459 add_memory_shuffle (ainfo, head, tail, data, size)
460      struct accumulate *ainfo;
461      struct shuffle **head;
462      struct shuffle **tail;
463      bfd_byte *data;
464      unsigned long size;
465 {
466   struct shuffle *n;
467      
468   n = (struct shuffle *) obstack_alloc (&ainfo->memory,
469                                         sizeof (struct shuffle));
470   if (!n)
471     {
472       bfd_set_error (bfd_error_no_memory);
473       return false;
474     }
475   n->next = NULL;
476   n->size = size;
477   n->filep = false;
478   n->u.memory = (PTR) data;
479   if (*head == (struct shuffle *) NULL)
480     *head = n;
481   if (*tail != (struct shuffle *) NULL)
482     (*tail)->next = n;
483   *tail = n;
484   return true;
485 }
486
487 /* Initialize the FDR hash table.  This returns a handle which is then
488    passed in to bfd_ecoff_debug_accumulate, et. al.  */
489
490 /*ARGSUSED*/
491 PTR
492 bfd_ecoff_debug_init (output_bfd, output_debug, output_swap, info)
493      bfd *output_bfd;
494      struct ecoff_debug_info *output_debug;
495      const struct ecoff_debug_swap *output_swap;
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   if (!obstack_begin (&ainfo->memory, 4050))
538     {
539       bfd_set_error (bfd_error_no_memory);
540       return NULL;
541     }
542
543   return (PTR) ainfo;
544 }
545
546 /* Free the accumulated debugging information.  */
547
548 /*ARGSUSED*/
549 void
550 bfd_ecoff_debug_free (handle, output_bfd, output_debug, output_swap, info)
551      PTR handle;
552      bfd *output_bfd;
553      struct ecoff_debug_info *output_debug;
554      const struct ecoff_debug_swap *output_swap;
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   obstack_free (&ainfo->memory, (PTR) NULL);
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 /*ARGSUSED*/
578 boolean
579 bfd_ecoff_debug_accumulate (handle, output_bfd, output_debug, output_swap,
580                             input_bfd, input_debug, input_swap,
581                             info)
582      PTR handle;
583      bfd *output_bfd;
584      struct ecoff_debug_info *output_debug;
585      const struct ecoff_debug_swap *output_swap;
586      bfd *input_bfd;
587      struct ecoff_debug_info *input_debug;
588      const struct ecoff_debug_swap *input_swap;
589      struct bfd_link_info *info;
590 {
591   struct accumulate *ainfo = (struct accumulate *) handle;
592   void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
593     = input_swap->swap_sym_in;
594   void (* const swap_rfd_in) PARAMS ((bfd *, PTR, RFDT *))
595     = input_swap->swap_rfd_in;
596   void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
597     = output_swap->swap_sym_out;
598   void (* const swap_fdr_out) PARAMS ((bfd *, const FDR *, PTR))
599     = output_swap->swap_fdr_out;
600   void (* const swap_rfd_out) PARAMS ((bfd *, const RFDT *, PTR))
601     = output_swap->swap_rfd_out;
602   bfd_size_type external_pdr_size = output_swap->external_pdr_size;
603   bfd_size_type external_sym_size = output_swap->external_sym_size;
604   bfd_size_type external_opt_size = output_swap->external_opt_size;
605   bfd_size_type external_fdr_size = output_swap->external_fdr_size;
606   bfd_size_type external_rfd_size = output_swap->external_rfd_size;
607   HDRR * const output_symhdr = &output_debug->symbolic_header;
608   HDRR * const input_symhdr = &input_debug->symbolic_header;
609   bfd_vma section_adjust[scMax];
610   asection *sec;
611   bfd_byte *fdr_start;
612   bfd_byte *fdr_ptr;
613   bfd_byte *fdr_end;
614   bfd_size_type fdr_add;
615   unsigned int copied;
616   RFDT i;
617   unsigned long sz;
618   bfd_byte *rfd_out;
619   bfd_byte *rfd_in;
620   bfd_byte *rfd_end;
621   long newrfdbase = 0;
622   long oldrfdbase = 0;
623   bfd_byte *fdr_out;
624
625   /* Use section_adjust to hold the value to add to a symbol in a
626      particular section.  */
627   memset ((PTR) section_adjust, 0, sizeof section_adjust);
628
629 #define SET(name, indx) \
630   sec = bfd_get_section_by_name (input_bfd, name); \
631   if (sec != NULL) \
632     section_adjust[indx] = (sec->output_section->vma \
633                             + sec->output_offset \
634                             - sec->vma);
635
636   SET (".text", scText);
637   SET (".data", scData);
638   SET (".bss", scBss);
639   SET (".sdata", scSData);
640   SET (".sbss", scSBss);
641   /* scRdata section may be either .rdata or .rodata.  */
642   SET (".rdata", scRData);
643   SET (".rodata", scRData);
644   SET (".init", scInit);
645   SET (".fini", scFini);
646   SET (".rconst", scRConst);
647
648 #undef SET
649
650   /* Find all the debugging information based on the FDR's.  We need
651      to handle them whether they are swapped or not.  */
652   if (input_debug->fdr != (FDR *) NULL)
653     {
654       fdr_start = (bfd_byte *) input_debug->fdr;
655       fdr_add = sizeof (FDR);
656     }
657   else
658     {
659       fdr_start = (bfd_byte *) input_debug->external_fdr;
660       fdr_add = input_swap->external_fdr_size;
661     }
662   fdr_end = fdr_start + input_symhdr->ifdMax * fdr_add;
663
664   input_debug->ifdmap = (RFDT *) bfd_alloc (input_bfd,
665                                             (input_symhdr->ifdMax
666                                              * sizeof (RFDT)));
667
668   sz = (input_symhdr->crfd + input_symhdr->ifdMax) * external_rfd_size;
669   rfd_out = (bfd_byte *) obstack_alloc (&ainfo->memory, sz);
670   if (!input_debug->ifdmap || !rfd_out)
671     {
672       bfd_set_error (bfd_error_no_memory);
673       return false;
674     }
675   if (!add_memory_shuffle (ainfo, &ainfo->rfd, &ainfo->rfd_end, rfd_out, sz))
676     return false;
677
678   copied = 0;
679
680   /* Look through the FDR's to see which ones we are going to include
681      in the final output.  We do not want duplicate FDR information
682      for header files, because ECOFF debugging is often very large.
683      When we find an FDR with no line information which can be merged,
684      we look it up in a hash table to ensure that we only include it
685      once.  We keep a table mapping FDR numbers to the final number
686      they get with the BFD, so that we can refer to it when we write
687      out the external symbols.  */
688   for (fdr_ptr = fdr_start, i = 0;
689        fdr_ptr < fdr_end;
690        fdr_ptr += fdr_add, i++, rfd_out += external_rfd_size)
691     {
692       FDR fdr;
693
694       if (input_debug->fdr != (FDR *) NULL)
695         fdr = *(FDR *) fdr_ptr;
696       else
697         (*input_swap->swap_fdr_in) (input_bfd, (PTR) fdr_ptr, &fdr);
698
699       /* See if this FDR can be merged with an existing one.  */
700       if (fdr.cbLine == 0 && fdr.rss != -1 && fdr.fMerge)
701         {
702           const char *name;
703           char *lookup;
704           struct string_hash_entry *fh;
705
706           /* We look up a string formed from the file name and the
707              number of symbols.  Sometimes an include file will
708              conditionally define a typedef or something based on the
709              order of include files.  Using the number of symbols as a
710              hash reduces the chance that we will merge symbol
711              information that should not be 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", name, fdr.csym);
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 *) obstack_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 *) obstack_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_big_p
982            == input_bfd->xvec->header_byteorder_big_p)
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 *) obstack_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 *) obstack_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) obstack_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) obstack_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 /*ARGSUSED*/
1410 static void
1411 ecoff_align_debug (abfd, debug, swap)
1412      bfd *abfd;
1413      struct ecoff_debug_info *debug;
1414      const struct ecoff_debug_swap *swap;
1415 {
1416   HDRR * const symhdr = &debug->symbolic_header;
1417   bfd_size_type debug_align, aux_align, rfd_align;
1418   size_t add;
1419
1420   /* Adjust the counts so that structures are aligned.  */
1421   debug_align = swap->debug_align;
1422   aux_align = debug_align / sizeof (union aux_ext);
1423   rfd_align = debug_align / swap->external_rfd_size;
1424
1425   add = debug_align - (symhdr->cbLine & (debug_align - 1));
1426   if (add != debug_align)
1427     {
1428       if (debug->line != (unsigned char *) NULL)
1429         memset ((PTR) (debug->line + symhdr->cbLine), 0, add);
1430       symhdr->cbLine += add;
1431     }
1432
1433   add = debug_align - (symhdr->issMax & (debug_align - 1));
1434   if (add != debug_align)
1435     {
1436       if (debug->ss != (char *) NULL)
1437         memset ((PTR) (debug->ss + symhdr->issMax), 0, add);
1438       symhdr->issMax += add;
1439     }
1440
1441   add = debug_align - (symhdr->issExtMax & (debug_align - 1));
1442   if (add != debug_align)
1443     {
1444       if (debug->ssext != (char *) NULL)
1445         memset ((PTR) (debug->ssext + symhdr->issExtMax), 0, add);
1446       symhdr->issExtMax += add;
1447     }
1448
1449   add = aux_align - (symhdr->iauxMax & (aux_align - 1));
1450   if (add != aux_align)
1451     {
1452       if (debug->external_aux != (union aux_ext *) NULL)
1453         memset ((PTR) (debug->external_aux + symhdr->iauxMax), 0,
1454                 add * sizeof (union aux_ext));
1455       symhdr->iauxMax += add;
1456     }
1457
1458   add = rfd_align - (symhdr->crfd & (rfd_align - 1));
1459   if (add != rfd_align)
1460     {
1461       if (debug->external_rfd != (PTR) NULL)
1462         memset ((PTR) ((char *) debug->external_rfd
1463                        + symhdr->crfd * swap->external_rfd_size),
1464                 0, (size_t) (add * swap->external_rfd_size));
1465       symhdr->crfd += add;
1466     }
1467 }
1468
1469 /* Return the size required by the ECOFF debugging information.  */
1470
1471 bfd_size_type
1472 bfd_ecoff_debug_size (abfd, debug, swap)
1473      bfd *abfd;
1474      struct ecoff_debug_info *debug;
1475      const struct ecoff_debug_swap *swap;
1476 {
1477   bfd_size_type tot;
1478
1479   ecoff_align_debug (abfd, debug, swap);
1480   tot = swap->external_hdr_size;
1481
1482 #define ADD(count, size) \
1483   tot += debug->symbolic_header.count * size
1484
1485   ADD (cbLine, sizeof (unsigned char));
1486   ADD (idnMax, swap->external_dnr_size);
1487   ADD (ipdMax, swap->external_pdr_size);
1488   ADD (isymMax, swap->external_sym_size);
1489   ADD (ioptMax, swap->external_opt_size);
1490   ADD (iauxMax, sizeof (union aux_ext));
1491   ADD (issMax, sizeof (char));
1492   ADD (issExtMax, sizeof (char));
1493   ADD (ifdMax, swap->external_fdr_size);
1494   ADD (crfd, swap->external_rfd_size);
1495   ADD (iextMax, swap->external_ext_size);
1496
1497 #undef ADD
1498
1499   return tot;
1500 }
1501
1502 /* Write out the ECOFF symbolic header, given the file position it is
1503    going to be placed at.  This assumes that the counts are set
1504    correctly.  */
1505
1506 static boolean
1507 ecoff_write_symhdr (abfd, debug, swap, where)
1508      bfd *abfd;
1509      struct ecoff_debug_info *debug;
1510      const struct ecoff_debug_swap *swap;
1511      file_ptr where;
1512 {
1513   HDRR * const symhdr = &debug->symbolic_header;
1514   char *buff = NULL;
1515
1516   ecoff_align_debug (abfd, debug, swap);
1517
1518   /* Go to the right location in the file.  */
1519   if (bfd_seek (abfd, where, SEEK_SET) != 0)
1520     return false;
1521
1522   where += swap->external_hdr_size;
1523
1524   symhdr->magic = swap->sym_magic;
1525
1526   /* Fill in the file offsets.  */
1527 #define SET(offset, count, size) \
1528   if (symhdr->count == 0) \
1529     symhdr->offset = 0; \
1530   else \
1531     { \
1532       symhdr->offset = where; \
1533       where += symhdr->count * size; \
1534     }
1535
1536   SET (cbLineOffset, cbLine, sizeof (unsigned char));
1537   SET (cbDnOffset, idnMax, swap->external_dnr_size);
1538   SET (cbPdOffset, ipdMax, swap->external_pdr_size);
1539   SET (cbSymOffset, isymMax, swap->external_sym_size);
1540   SET (cbOptOffset, ioptMax, swap->external_opt_size);
1541   SET (cbAuxOffset, iauxMax, sizeof (union aux_ext));
1542   SET (cbSsOffset, issMax, sizeof (char));
1543   SET (cbSsExtOffset, issExtMax, sizeof (char));
1544   SET (cbFdOffset, ifdMax, swap->external_fdr_size);
1545   SET (cbRfdOffset, crfd, swap->external_rfd_size);
1546   SET (cbExtOffset, iextMax, swap->external_ext_size);
1547 #undef SET
1548
1549   buff = (PTR) bfd_malloc ((size_t) swap->external_hdr_size);
1550   if (buff == NULL && swap->external_hdr_size != 0)
1551     goto error_return;
1552
1553   (*swap->swap_hdr_out) (abfd, symhdr, buff);
1554   if (bfd_write (buff, 1, swap->external_hdr_size, abfd)
1555       != swap->external_hdr_size)
1556     goto error_return;
1557
1558   if (buff != NULL)
1559     free (buff);
1560   return true;
1561  error_return:
1562   if (buff != NULL)
1563     free (buff);
1564   return false;
1565 }
1566
1567 /* Write out the ECOFF debugging information.  This function assumes
1568    that the information (the pointers and counts) in *DEBUG have been
1569    set correctly.  WHERE is the position in the file to write the
1570    information to.  This function fills in the file offsets in the
1571    symbolic header.  */
1572
1573 boolean
1574 bfd_ecoff_write_debug (abfd, debug, swap, where)
1575      bfd *abfd;
1576      struct ecoff_debug_info *debug;
1577      const struct ecoff_debug_swap *swap;
1578      file_ptr where;
1579 {
1580   HDRR * const symhdr = &debug->symbolic_header;
1581
1582   if (! ecoff_write_symhdr (abfd, debug, swap, where))
1583     return false;
1584
1585 #define WRITE(ptr, count, size, offset) \
1586   BFD_ASSERT (symhdr->offset == 0 \
1587               || (bfd_vma) bfd_tell (abfd) == symhdr->offset); \
1588   if (bfd_write ((PTR) debug->ptr, size, symhdr->count, abfd) \
1589       != size * symhdr->count) \
1590     return false;
1591
1592   WRITE (line, cbLine, sizeof (unsigned char), cbLineOffset);
1593   WRITE (external_dnr, idnMax, swap->external_dnr_size, cbDnOffset);
1594   WRITE (external_pdr, ipdMax, swap->external_pdr_size, cbPdOffset);
1595   WRITE (external_sym, isymMax, swap->external_sym_size, cbSymOffset);
1596   WRITE (external_opt, ioptMax, swap->external_opt_size, cbOptOffset);
1597   WRITE (external_aux, iauxMax, sizeof (union aux_ext), cbAuxOffset);
1598   WRITE (ss, issMax, sizeof (char), cbSsOffset);
1599   WRITE (ssext, issExtMax, sizeof (char), cbSsExtOffset);
1600   WRITE (external_fdr, ifdMax, swap->external_fdr_size, cbFdOffset);
1601   WRITE (external_rfd, crfd, swap->external_rfd_size, cbRfdOffset);
1602   WRITE (external_ext, iextMax, swap->external_ext_size, cbExtOffset);
1603 #undef WRITE
1604
1605   return true;
1606 }
1607
1608 /* Write out a shuffle list.  */
1609
1610 static boolean ecoff_write_shuffle PARAMS ((bfd *,
1611                                             const struct ecoff_debug_swap *,
1612                                             struct shuffle *, PTR space));
1613
1614 static boolean
1615 ecoff_write_shuffle (abfd, swap, shuffle, space)
1616      bfd *abfd;
1617      const struct ecoff_debug_swap *swap;
1618      struct shuffle *shuffle;
1619      PTR space;
1620 {
1621   register struct shuffle *l;
1622   unsigned long total;
1623
1624   total = 0;
1625   for (l = shuffle; l != (struct shuffle *) NULL; l = l->next)
1626     {
1627       if (! l->filep)
1628         {
1629           if (bfd_write (l->u.memory, 1, l->size, abfd) != l->size)
1630             return false;
1631         }
1632       else
1633         {
1634           if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
1635               || bfd_read (space, 1, l->size, l->u.file.input_bfd) != l->size
1636               || bfd_write (space, 1, l->size, abfd) != l->size)
1637             return false;
1638         }
1639       total += l->size;
1640     }
1641
1642   if ((total & (swap->debug_align - 1)) != 0)
1643     {
1644       unsigned int i;
1645       bfd_byte *s;
1646
1647       i = swap->debug_align - (total & (swap->debug_align - 1));
1648       s = (bfd_byte *) bfd_malloc (i);
1649       if (s == NULL && i != 0)
1650         return false;
1651
1652       memset ((PTR) s, 0, i);
1653       if (bfd_write ((PTR) s, 1, i, abfd) != i)
1654         {
1655           free (s);
1656           return false;
1657         }
1658       free (s);
1659     }
1660
1661   return true;
1662 }
1663
1664 /* Write out debugging information using accumulated linker
1665    information.  */
1666
1667 boolean
1668 bfd_ecoff_write_accumulated_debug (handle, abfd, debug, swap, info, where)
1669      PTR handle;
1670      bfd *abfd;
1671      struct ecoff_debug_info *debug;
1672      const struct ecoff_debug_swap *swap;
1673      struct bfd_link_info *info;
1674      file_ptr where;
1675 {
1676   struct accumulate *ainfo = (struct accumulate *) handle;
1677   PTR space = NULL;
1678
1679   if (! ecoff_write_symhdr (abfd, debug, swap, where))
1680     goto error_return;
1681
1682   space = (PTR) bfd_malloc (ainfo->largest_file_shuffle);
1683   if (space == NULL && ainfo->largest_file_shuffle != 0)
1684     goto error_return;
1685
1686   if (! ecoff_write_shuffle (abfd, swap, ainfo->line, space)
1687       || ! ecoff_write_shuffle (abfd, swap, ainfo->pdr, space)
1688       || ! ecoff_write_shuffle (abfd, swap, ainfo->sym, space)
1689       || ! ecoff_write_shuffle (abfd, swap, ainfo->opt, space)
1690       || ! ecoff_write_shuffle (abfd, swap, ainfo->aux, space))
1691     goto error_return;
1692
1693   /* The string table is written out from the hash table if this is a
1694      final link.  */
1695   if (info->relocateable)
1696     {
1697       BFD_ASSERT (ainfo->ss_hash == (struct string_hash_entry *) NULL);
1698       if (! ecoff_write_shuffle (abfd, swap, ainfo->ss, space))
1699         goto error_return;
1700     }
1701   else
1702     {
1703       unsigned long total;
1704       bfd_byte null;
1705       struct string_hash_entry *sh;
1706
1707       BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
1708       null = 0;
1709       if (bfd_write ((PTR) &null, 1, 1, abfd) != 1)
1710         goto error_return;
1711       total = 1;
1712       BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
1713       for (sh = ainfo->ss_hash;
1714            sh != (struct string_hash_entry *) NULL;
1715            sh = sh->next)
1716         {
1717           size_t len;
1718
1719           len = strlen (sh->root.string);
1720           if (bfd_write ((PTR) sh->root.string, 1, len + 1, abfd) != len + 1)
1721             goto error_return;
1722           total += len + 1;
1723         }
1724
1725       if ((total & (swap->debug_align - 1)) != 0)
1726         {
1727           unsigned int i;
1728           bfd_byte *s;
1729
1730           i = swap->debug_align - (total & (swap->debug_align - 1));
1731           s = (bfd_byte *) bfd_malloc (i);
1732           if (s == NULL && i != 0)
1733             goto error_return;
1734           memset ((PTR) s, 0, i);
1735           if (bfd_write ((PTR) s, 1, i, abfd) != i)
1736             {
1737               free (s);
1738               goto error_return;
1739             }
1740           free (s);
1741         }
1742     }
1743
1744   /* The external strings and symbol are not converted over to using
1745      shuffles.  FIXME: They probably should be.  */
1746   if (bfd_write (debug->ssext, 1, debug->symbolic_header.issExtMax, abfd)
1747       != (bfd_size_type) debug->symbolic_header.issExtMax)
1748     goto error_return;
1749   if ((debug->symbolic_header.issExtMax & (swap->debug_align - 1)) != 0)
1750     {
1751       unsigned int i;
1752       bfd_byte *s;
1753
1754       i = (swap->debug_align
1755            - (debug->symbolic_header.issExtMax & (swap->debug_align - 1)));
1756       s = (bfd_byte *) bfd_malloc (i);
1757       if (s == NULL && i != 0)
1758         goto error_return;
1759       memset ((PTR) s, 0, i);
1760       if (bfd_write ((PTR) s, 1, i, abfd) != i)
1761         {
1762           free (s);
1763           goto error_return;
1764         }
1765       free (s);
1766     }
1767
1768   if (! ecoff_write_shuffle (abfd, swap, ainfo->fdr, space)
1769       || ! ecoff_write_shuffle (abfd, swap, ainfo->rfd, space))
1770     goto error_return;
1771
1772   BFD_ASSERT (debug->symbolic_header.cbExtOffset == 0
1773               || (debug->symbolic_header.cbExtOffset
1774                   == (bfd_vma) bfd_tell (abfd)));
1775
1776   if (bfd_write (debug->external_ext, swap->external_ext_size,
1777                  debug->symbolic_header.iextMax, abfd)
1778       != debug->symbolic_header.iextMax * swap->external_ext_size)
1779     goto error_return;
1780
1781   if (space != NULL)
1782     free (space);
1783   return true;
1784
1785  error_return:
1786   if (space != NULL)
1787     free (space);
1788   return false;
1789 }
1790 \f
1791 /* Handle the find_nearest_line function for both ECOFF and MIPS ELF
1792    files.  */
1793
1794 /* Compare FDR entries.  This is called via qsort.  */
1795
1796 static int
1797 cmp_fdrtab_entry (leftp, rightp)
1798      const PTR leftp;
1799      const PTR rightp;
1800 {
1801   const struct ecoff_fdrtab_entry *lp =
1802     (const struct ecoff_fdrtab_entry *) leftp;
1803   const struct ecoff_fdrtab_entry *rp =
1804     (const struct ecoff_fdrtab_entry *) rightp;
1805
1806   if (lp->base_addr < rp->base_addr)
1807     return -1;
1808   if (lp->base_addr > rp->base_addr)
1809     return 1;
1810   return 0;
1811 }
1812
1813 /* Each file descriptor (FDR) has a memory address, to simplify
1814    looking up an FDR by address, we build a table covering all FDRs
1815    that have a least one procedure descriptor in them.  The final
1816    table will be sorted by address so we can look it up via binary
1817    search.  */
1818
1819 static boolean
1820 mk_fdrtab (abfd, debug_info, debug_swap, line_info)
1821      bfd *abfd;
1822      struct ecoff_debug_info * const debug_info;
1823      const struct ecoff_debug_swap * const debug_swap;
1824      struct ecoff_find_line *line_info;
1825 {
1826   struct ecoff_fdrtab_entry *tab;
1827   FDR *fdr_ptr;
1828   FDR *fdr_start;
1829   FDR *fdr_end;
1830   boolean stabs;
1831   long len;
1832
1833   fdr_start = debug_info->fdr;
1834   fdr_end = fdr_start + debug_info->symbolic_header.ifdMax;
1835
1836   /* First, let's see how long the table needs to be: */
1837   for (len = 0, fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1838     {
1839       if (fdr_ptr->cpd == 0)    /* skip FDRs that have no PDRs */
1840         continue;
1841       ++len;
1842     }
1843
1844   /* Now, create and fill in the table: */
1845
1846   line_info->fdrtab = ((struct ecoff_fdrtab_entry*)
1847                        bfd_zalloc (abfd,
1848                                    len * sizeof (struct ecoff_fdrtab_entry)));
1849   if (line_info->fdrtab == NULL)
1850     return false;
1851   line_info->fdrtab_len = len;
1852
1853   tab = line_info->fdrtab;
1854   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1855     {
1856       if (fdr_ptr->cpd == 0)
1857         continue;
1858
1859       /* Check whether this file has stabs debugging information.  In
1860          a file with stabs debugging information, the second local
1861          symbol is named @stabs.  */
1862       stabs = false;
1863       if (fdr_ptr->csym >= 2)
1864         {
1865           char *sym_ptr;
1866           SYMR sym;
1867
1868           sym_ptr = ((char *) debug_info->external_sym
1869                      + (fdr_ptr->isymBase + 1)*debug_swap->external_sym_size);
1870           (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
1871           if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
1872                       STABS_SYMBOL) == 0)
1873             stabs = true;
1874         }
1875
1876       if (!stabs)
1877         {
1878           bfd_size_type external_pdr_size;
1879           char *pdr_ptr;
1880           PDR pdr;
1881
1882           external_pdr_size = debug_swap->external_pdr_size;
1883
1884           pdr_ptr = ((char *) debug_info->external_pdr
1885                      + fdr_ptr->ipdFirst * external_pdr_size);
1886           (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
1887           /* The address of the first PDR is the offset of that
1888              procedure relative to the beginning of file FDR.  */
1889           tab->base_addr = fdr_ptr->adr - pdr.adr;
1890         }
1891       else
1892         {
1893           /* XXX I don't know about stabs, so this is a guess
1894              (davidm@cs.arizona.edu): */
1895           tab->base_addr = fdr_ptr->adr;
1896         }
1897       tab->fdr = fdr_ptr;
1898       ++tab;
1899     }
1900
1901   /* Finally, the table is sorted in increasing memory-address order.
1902      The table is mostly sorted already, but there are cases (e.g.,
1903      static functions in include files), where this does not hold.
1904      Use "odump -PFv" to verify...  */
1905   qsort ((PTR) line_info->fdrtab, len,
1906          sizeof (struct ecoff_fdrtab_entry), cmp_fdrtab_entry);
1907
1908   return true;
1909 }
1910
1911 /* Return index of first FDR that covers to OFFSET.  */
1912
1913 static long
1914 fdrtab_lookup (line_info, offset)
1915      struct ecoff_find_line *line_info;
1916      bfd_vma offset;
1917 {
1918   long low, high, len;
1919   long mid = -1;
1920   struct ecoff_fdrtab_entry *tab;
1921
1922   len = line_info->fdrtab_len;
1923   if (len == 0)
1924     return -1;
1925
1926   tab = line_info->fdrtab;
1927   for (low = 0, high = len - 1 ; low != high ;)
1928     {
1929       mid = (high + low) / 2;
1930       if (offset >= tab[mid].base_addr && offset < tab[mid + 1].base_addr)
1931         goto find_min;
1932
1933       if (tab[mid].base_addr > offset)
1934         high = mid;
1935       else
1936         low = mid + 1;
1937     }
1938   ++mid;
1939
1940   /* last entry is catch-all for all higher addresses: */
1941   if (offset < tab[mid].base_addr)
1942     return -1;
1943
1944  find_min:
1945
1946   while (mid > 0 && tab[mid - 1].base_addr == tab[mid].base_addr)
1947     --mid;
1948
1949   return mid;
1950 }
1951
1952 /* Do the work of find_nearest_line.  */
1953
1954 boolean
1955 _bfd_ecoff_locate_line (abfd, section, offset, debug_info, debug_swap,
1956                         line_info, filename_ptr, functionname_ptr, retline_ptr)
1957      bfd *abfd;
1958      asection *section;
1959      bfd_vma offset;
1960      struct ecoff_debug_info * const debug_info;
1961      const struct ecoff_debug_swap * const debug_swap;
1962      struct ecoff_find_line *line_info;
1963      const char **filename_ptr;
1964      const char **functionname_ptr;
1965      unsigned int *retline_ptr;
1966 {
1967   struct ecoff_fdrtab_entry *tab;
1968   boolean stabs;
1969   FDR *fdr_ptr;
1970   int i;
1971   
1972   offset += section->vma;
1973      
1974   /* Build FDR table (sorted by object file's base-address) if we
1975      don't have it already.  */
1976   if (line_info->fdrtab == NULL
1977       && !mk_fdrtab (abfd, debug_info, debug_swap, line_info))
1978     return false;
1979
1980   tab = line_info->fdrtab;
1981
1982   /* find first FDR for address OFFSET */
1983   i = fdrtab_lookup (line_info, offset);
1984   if (i < 0)
1985     return false;               /* no FDR, no fun... */
1986   fdr_ptr = tab[i].fdr;
1987
1988   /* Check whether this file has stabs debugging information.  In a
1989      file with stabs debugging information, the second local symbol is
1990      named @stabs.  */
1991   stabs = false;
1992   if (fdr_ptr->csym >= 2)
1993     {
1994       char *sym_ptr;
1995       SYMR sym;
1996
1997       sym_ptr = ((char *) debug_info->external_sym
1998                  + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
1999       (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
2000       if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
2001                   STABS_SYMBOL) == 0)
2002         stabs = true;
2003     }
2004
2005   if (!stabs)
2006     {
2007       bfd_size_type external_pdr_size;
2008       char *pdr_ptr;
2009       char *best_pdr = NULL;
2010       FDR *best_fdr;
2011       bfd_vma best_dist = ~0;
2012       PDR pdr;
2013       unsigned char *line_ptr;
2014       unsigned char *line_end;
2015       int lineno;
2016       /* This file uses ECOFF debugging information.  Each FDR has a
2017          list of procedure descriptors (PDR).  The address in the FDR
2018          is the absolute address of the first procedure.  The address
2019          in the first PDR gives the offset of that procedure relative
2020          to the object file's base-address.  The addresses in
2021          subsequent PDRs specify each procedure's address relative to
2022          the object file's base-address.  To make things more juicy,
2023          whenever the PROF bit in the PDR is set, the real entry point
2024          of the procedure may be 16 bytes below what would normally be
2025          the procedure's entry point.  Instead, DEC came up with a
2026          wicked scheme to create profiled libraries "on the fly":
2027          instead of shipping a regular and a profiled version of each
2028          library, they insert 16 bytes of unused space in front of
2029          each procedure and set the "prof" bit in the PDR to indicate
2030          that there is a gap there (this is done automagically by "as"
2031          when option "-pg" is specified).  Thus, normally, you link
2032          against such a library and, except for lots of 16 byte gaps
2033          between functions, things will behave as usual.  However,
2034          when invoking "ld" with option "-pg", it will fill those gaps
2035          with code that calls mcount().  It then moves the function's
2036          entry point down by 16 bytes, and out pops a binary that has
2037          all functions profiled.
2038
2039          NOTE: Neither FDRs nor PDRs are strictly sorted in memory
2040                order.  For example, when including header-files that
2041                define functions, the FDRs follow behind the including
2042                file, even though their code may have been generated at
2043                a lower address.  File coff-alpha.c from libbfd
2044                illustrates this (use "odump -PFv" to look at a file's
2045                FDR/PDR).  Similarly, PDRs are sometimes out of order
2046                as well.  An example of this is OSF/1 v3.0 libc's
2047                malloc.c.  I'm not sure why this happens, but it could
2048                be due to optimizations that reorder a function's
2049                position within an object-file.
2050         
2051          Strategy:
2052          
2053          On the first call to this function, we build a table of FDRs
2054          that is sorted by the base-address of the object-file the FDR
2055          is referring to.  Notice that each object-file may contain
2056          code from multiple source files (e.g., due to code defined in
2057          include files).  Thus, for any given base-address, there may
2058          be multiple FDRs (but this case is, fortunately, uncommon).
2059          lookup(addr) guarantees to return the first FDR that applies
2060          to address ADDR.  Thus, after invoking lookup(), we have a
2061          list of FDRs that may contain the PDR for ADDR.  Next, we
2062          walk through the PDRs of these FDRs and locate the one that
2063          is closest to ADDR (i.e., for which the difference between
2064          ADDR and the PDR's entry point is positive and minimal).
2065          Once, the right FDR and PDR are located, we simply walk
2066          through the line-number table to lookup the line-number that
2067          best matches ADDR.  Obviously, things could be sped up by
2068          keeping a sorted list of PDRs instead of a sorted list of
2069          FDRs.  However, this would increase space requirements
2070          considerably, which is undesirable.  */
2071       external_pdr_size = debug_swap->external_pdr_size;
2072
2073       /* Make offset relative to object file's start-address: */
2074       offset -= tab[i].base_addr;
2075       /* Search FDR list starting at tab[i] for the PDR that best matches
2076          OFFSET.  Normally, the FDR list is only one entry long.  */
2077       best_fdr = NULL;
2078       do
2079         {
2080           bfd_vma dist, min_dist = 0;
2081           char *pdr_hold;
2082           char *pdr_end;
2083           
2084           fdr_ptr = tab[i].fdr;
2085           
2086           pdr_ptr = ((char *) debug_info->external_pdr
2087                      + fdr_ptr->ipdFirst * external_pdr_size);
2088           pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2089           (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2090           /* Find PDR that is closest to OFFSET.  If pdr.prof is set,
2091              the procedure entry-point *may* be 0x10 below pdr.adr.  We
2092              simply pretend that pdr.prof *implies* a lower entry-point.
2093              This is safe because it just means that may identify 4 NOPs
2094              in front of the function as belonging to the function.  */
2095           for (pdr_hold = NULL;
2096                pdr_ptr < pdr_end;
2097                (pdr_ptr += external_pdr_size,
2098                 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr)))
2099             {
2100               if (offset >= (pdr.adr - 0x10 * pdr.prof))
2101                 {
2102                   dist = offset - (pdr.adr - 0x10 * pdr.prof);
2103                   if (!pdr_hold || dist < min_dist)
2104                     {
2105                       min_dist = dist;
2106                       pdr_hold = pdr_ptr;
2107                     }
2108                 }
2109             }
2110           
2111           if (!best_pdr || min_dist < best_dist)
2112             {
2113               best_dist = min_dist;
2114               best_fdr = fdr_ptr;
2115               best_pdr = pdr_hold;
2116             }
2117           /* continue looping until base_addr of next entry is different: */
2118         }
2119       while (++i < line_info->fdrtab_len
2120              && tab[i].base_addr == tab[i - 1].base_addr);
2121
2122       if (!best_fdr || !best_pdr)
2123         return false;                   /* shouldn't happen... */
2124
2125       /* phew, finally we got something that we can hold onto: */
2126       fdr_ptr = best_fdr;
2127       pdr_ptr = best_pdr;
2128       (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2129       /* Now we can look for the actual line number.  The line numbers
2130          are stored in a very funky format, which I won't try to
2131          describe.  The search is bounded by the end of the FDRs line
2132          number entries.  */
2133       line_end = debug_info->line + fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2134
2135       /* Make offset relative to procedure entry: */
2136       offset -= pdr.adr - 0x10 * pdr.prof;
2137       lineno = pdr.lnLow;
2138       line_ptr = debug_info->line + fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2139       while (line_ptr < line_end)
2140         {
2141           int delta;
2142           unsigned int count;
2143
2144           delta = *line_ptr >> 4;
2145           if (delta >= 0x8)
2146             delta -= 0x10;
2147           count = (*line_ptr & 0xf) + 1;
2148           ++line_ptr;
2149           if (delta == -8)
2150             {
2151               delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2152               if (delta >= 0x8000)
2153                 delta -= 0x10000;
2154               line_ptr += 2;
2155             }
2156           lineno += delta;
2157           if (offset < count * 4)
2158             break;
2159           offset -= count * 4;
2160         }
2161
2162       /* If fdr_ptr->rss is -1, then this file does not have full
2163          symbols, at least according to gdb/mipsread.c.  */
2164       if (fdr_ptr->rss == -1)
2165         {
2166           *filename_ptr = NULL;
2167           if (pdr.isym == -1)
2168             *functionname_ptr = NULL;
2169           else
2170             {
2171               EXTR proc_ext;
2172
2173               (*debug_swap->swap_ext_in)
2174                 (abfd,
2175                  ((char *) debug_info->external_ext
2176                   + pdr.isym * debug_swap->external_ext_size),
2177                  &proc_ext);
2178               *functionname_ptr = debug_info->ssext + proc_ext.asym.iss;
2179             }
2180         }
2181       else
2182         {
2183           SYMR proc_sym;
2184
2185           *filename_ptr = debug_info->ss + fdr_ptr->issBase + fdr_ptr->rss;
2186           (*debug_swap->swap_sym_in)
2187             (abfd,
2188              ((char *) debug_info->external_sym
2189               + (fdr_ptr->isymBase + pdr.isym) * debug_swap->external_sym_size),
2190              &proc_sym);
2191           *functionname_ptr = debug_info->ss + fdr_ptr->issBase + proc_sym.iss;
2192         }
2193       if (lineno == ilineNil)
2194         lineno = 0;
2195       *retline_ptr = lineno;
2196     }
2197   else
2198     {
2199       bfd_size_type external_sym_size;
2200       const char *directory_name;
2201       const char *main_file_name;
2202       const char *current_file_name;
2203       const char *function_name;
2204       const char *line_file_name;
2205       bfd_vma low_func_vma;
2206       bfd_vma low_line_vma;
2207       boolean past_line;
2208       boolean past_fn;
2209       char *sym_ptr, *sym_ptr_end;
2210       size_t len, funclen;
2211       char *buffer = NULL;
2212
2213       /* This file uses stabs debugging information.  When gcc is not
2214          optimizing, it will put the line number information before
2215          the function name stabs entry.  When gcc is optimizing, it
2216          will put the stabs entry for all the function first, followed
2217          by the line number information.  (This appears to happen
2218          because of the two output files used by the -mgpopt switch,
2219          which is implied by -O).  This means that we must keep
2220          looking through the symbols until we find both a line number
2221          and a function name which are beyond the address we want.  */
2222
2223       *filename_ptr = NULL;
2224       *functionname_ptr = NULL;
2225       *retline_ptr = 0;
2226
2227       directory_name = NULL;
2228       main_file_name = NULL;
2229       current_file_name = NULL;
2230       function_name = NULL;
2231       line_file_name = NULL;
2232       low_func_vma = 0;
2233       low_line_vma = 0;
2234       past_line = false;
2235       past_fn = false;
2236
2237       external_sym_size = debug_swap->external_sym_size;
2238
2239       sym_ptr = ((char *) debug_info->external_sym
2240                  + (fdr_ptr->isymBase + 2) * external_sym_size);
2241       sym_ptr_end = sym_ptr + (fdr_ptr->csym - 2) * external_sym_size;
2242       for (;
2243            sym_ptr < sym_ptr_end && (! past_line || ! past_fn);
2244            sym_ptr += external_sym_size)
2245         {
2246           SYMR sym;
2247
2248           (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
2249
2250           if (ECOFF_IS_STAB (&sym))
2251             {
2252               switch (ECOFF_UNMARK_STAB (sym.index))
2253                 {
2254                 case N_SO:
2255                   main_file_name = current_file_name =
2256                     debug_info->ss + fdr_ptr->issBase + sym.iss;
2257
2258                   /* Check the next symbol to see if it is also an
2259                      N_SO symbol.  */
2260                   if (sym_ptr + external_sym_size < sym_ptr_end)
2261                     {
2262                       SYMR nextsym;
2263
2264                       (*debug_swap->swap_sym_in) (abfd,
2265                                                   sym_ptr + external_sym_size,
2266                                                   &nextsym);
2267                       if (ECOFF_IS_STAB (&nextsym)
2268                           && ECOFF_UNMARK_STAB (nextsym.index) == N_SO)
2269                         {
2270                           directory_name = current_file_name;
2271                           main_file_name = current_file_name =
2272                             debug_info->ss + fdr_ptr->issBase + nextsym.iss;
2273                           sym_ptr += external_sym_size;
2274                         }
2275                     }
2276                   break;
2277
2278                 case N_SOL:
2279                   current_file_name =
2280                     debug_info->ss + fdr_ptr->issBase + sym.iss;
2281                   break;
2282
2283                 case N_FUN:
2284                   if (sym.value > offset)
2285                     past_fn = true;
2286                   else if (sym.value >= low_func_vma)
2287                     {
2288                       low_func_vma = sym.value;
2289                       function_name =
2290                         debug_info->ss + fdr_ptr->issBase + sym.iss;
2291                     }
2292                   break;
2293                 }
2294             }
2295           else if (sym.st == stLabel && sym.index != indexNil)
2296             {
2297               if (sym.value > offset)
2298                 past_line = true;
2299               else if (sym.value >= low_line_vma)
2300                 {
2301                   low_line_vma = sym.value;
2302                   line_file_name = current_file_name;
2303                   *retline_ptr = sym.index;
2304                 }
2305             }
2306         }
2307
2308       if (*retline_ptr != 0)
2309         main_file_name = line_file_name;
2310
2311       /* We need to remove the stuff after the colon in the function
2312          name.  We also need to put the directory name and the file
2313          name together.  */
2314       if (function_name == NULL)
2315         len = funclen = 0;
2316       else
2317         len = funclen = strlen (function_name) + 1;
2318
2319       if (main_file_name != NULL
2320           && directory_name != NULL
2321           && main_file_name[0] != '/')
2322         len += strlen (directory_name) + strlen (main_file_name) + 1;
2323
2324       if (len != 0)
2325         {
2326           if (line_info->find_buffer != NULL)
2327             free (line_info->find_buffer);
2328           buffer = (char *) bfd_malloc (len);
2329           if (buffer == NULL)
2330             return false;
2331           line_info->find_buffer = buffer;
2332         }
2333
2334       if (function_name != NULL)
2335         {
2336           char *colon;
2337
2338           strcpy (buffer, function_name);
2339           colon = strchr (buffer, ':');
2340           if (colon != NULL)
2341             *colon = '\0';
2342           *functionname_ptr = buffer;
2343         }
2344
2345       if (main_file_name != NULL)
2346         {
2347           if (directory_name == NULL || main_file_name[0] == '/')
2348             *filename_ptr = main_file_name;
2349           else
2350             {
2351               sprintf (buffer + funclen, "%s%s", directory_name,
2352                        main_file_name);
2353               *filename_ptr = buffer + funclen;
2354             }
2355         }
2356     }
2357
2358   return true;
2359 }
2360 \f
2361 /* These routines copy symbolic information into a memory buffer.
2362
2363    FIXME: The whole point of the shuffle code is to avoid storing
2364    everything in memory, since the linker is such a memory hog.  This
2365    code makes that effort useless.  It is only called by the MIPS ELF
2366    code when generating a shared library, so it is not that big a
2367    deal, but it should be fixed eventually.  */
2368
2369 /* Collect a shuffle into a memory buffer.  */
2370
2371 static boolean ecoff_collect_shuffle PARAMS ((struct shuffle *, bfd_byte *));
2372
2373 static boolean
2374 ecoff_collect_shuffle (l, buff)
2375      struct shuffle *l;
2376      bfd_byte *buff;
2377 {
2378   unsigned long total;
2379
2380   total = 0;
2381   for (; l != (struct shuffle *) NULL; l = l->next)
2382     {
2383       if (! l->filep)
2384         memcpy (buff, l->u.memory, l->size);
2385       else
2386         {
2387           if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
2388               || bfd_read (buff, 1, l->size, l->u.file.input_bfd) != l->size)
2389             return false;
2390         }
2391       total += l->size;
2392       buff += l->size;
2393     }
2394
2395   return true;
2396 }
2397
2398 /* Copy PDR information into a memory buffer.  */
2399
2400 boolean
2401 _bfd_ecoff_get_accumulated_pdr (handle, buff)
2402      PTR handle;
2403      bfd_byte *buff;
2404 {
2405   struct accumulate *ainfo = (struct accumulate *) handle;
2406
2407   return ecoff_collect_shuffle (ainfo->pdr, buff);
2408 }
2409
2410 /* Copy symbol information into a memory buffer.  */
2411
2412 boolean
2413 _bfd_ecoff_get_accumulated_sym (handle, buff)
2414      PTR handle;
2415      bfd_byte *buff;
2416 {
2417   struct accumulate *ainfo = (struct accumulate *) handle;
2418
2419   return ecoff_collect_shuffle (ainfo->sym, buff);
2420 }
2421
2422 /* Copy the string table into a memory buffer.  */
2423
2424 boolean
2425 _bfd_ecoff_get_accumulated_ss (handle, buff)
2426      PTR handle;
2427      bfd_byte *buff;
2428 {
2429   struct accumulate *ainfo = (struct accumulate *) handle;
2430   struct string_hash_entry *sh;
2431   unsigned long total;
2432
2433   /* The string table is written out from the hash table if this is a
2434      final link.  */
2435   BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
2436   *buff++ = '\0';
2437   total = 1;
2438   BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
2439   for (sh = ainfo->ss_hash;
2440        sh != (struct string_hash_entry *) NULL;
2441        sh = sh->next)
2442     {
2443       size_t len;
2444
2445       len = strlen (sh->root.string);
2446       memcpy (buff, (PTR) sh->root.string, len + 1);
2447       total += len + 1;
2448       buff += len + 1;
2449     }
2450
2451   return true;
2452 }