Coverage Report

Created: 2023-08-28 06:31

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