OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [binutils-2.18.50/] [bfd/] [ecofflink.c] - Blame information for rev 156

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.