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