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