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