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