source: trunk/src/binutils/bfd/ecofflink.c@ 106

Last change on this file since 106 was 10, checked in by bird, 22 years ago

Initial revision

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