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