Coverage Report

Created: 2023-08-28 06:25

/src/binutils-gdb/bfd/ecoff.c
Line
Count
Source (jump to first uncovered line)
1
/* Generic ECOFF (Extended-COFF) routines.
2
   Copyright (C) 1990-2023 Free Software Foundation, Inc.
3
   Original version by Per Bothner.
4
   Full support added by Ian Lance Taylor, ian@cygnus.com.
5
6
   This file is part of BFD, the Binary File Descriptor library.
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#include "ecoff-bfd.h"
28
#include "aout/ar.h"
29
#include "aout/stab_gnu.h"
30
31
/* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32
   some other stuff which we don't want and which conflicts with stuff
33
   we do want.  */
34
#include "libaout.h"
35
#include "aout/aout64.h"
36
#undef N_ABS
37
#undef exec_hdr
38
#undef obj_sym_filepos
39
40
#include "coff/internal.h"
41
#include "coff/sym.h"
42
#include "coff/symconst.h"
43
#include "coff/ecoff.h"
44
#include "libcoff.h"
45
#include "libecoff.h"
46
#include "libiberty.h"
47
48
356k
#define streq(a, b) (strcmp ((a), (b)) == 0)
49
50

51
/* This stuff is somewhat copied from coffcode.h.  */
52
static asection bfd_debug_section =
53
  BFD_FAKE_SECTION (bfd_debug_section, NULL, "*DEBUG*", 0, 0);
54
55
/* Create an ECOFF object.  */
56
57
bool
58
_bfd_ecoff_mkobject (bfd *abfd)
59
266
{
60
266
  size_t amt = sizeof (ecoff_data_type);
61
62
266
  abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
63
266
  if (abfd->tdata.ecoff_obj_data == NULL)
64
0
    return false;
65
66
266
  return true;
67
266
}
68
69
/* This is a hook called by coff_real_object_p to create any backend
70
   specific information.  */
71
72
void *
73
_bfd_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr)
74
266
{
75
266
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
76
266
  struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
77
266
  ecoff_data_type *ecoff;
78
79
266
  if (! _bfd_ecoff_mkobject (abfd))
80
0
    return NULL;
81
82
266
  ecoff = ecoff_data (abfd);
83
266
  ecoff->gp_size = 8;
84
266
  ecoff->sym_filepos = internal_f->f_symptr;
85
86
266
  if (internal_a != NULL)
87
20
    {
88
20
      int i;
89
90
20
      ecoff->text_start = internal_a->text_start;
91
20
      ecoff->text_end = internal_a->text_start + internal_a->tsize;
92
20
      ecoff->gp = internal_a->gp_value;
93
20
      ecoff->gprmask = internal_a->gprmask;
94
100
      for (i = 0; i < 4; i++)
95
80
  ecoff->cprmask[i] = internal_a->cprmask[i];
96
20
      ecoff->fprmask = internal_a->fprmask;
97
20
      if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
98
0
  abfd->flags |= D_PAGED;
99
20
      else
100
20
  abfd->flags &=~ D_PAGED;
101
20
    }
102
103
  /* It turns out that no special action is required by the MIPS or
104
     Alpha ECOFF backends.  They have different information in the
105
     a.out header, but we just copy it all (e.g., gprmask, cprmask and
106
     fprmask) and let the swapping routines ensure that only relevant
107
     information is written out.  */
108
109
266
  return (void *) ecoff;
110
266
}
111
112
bool
113
_bfd_ecoff_bfd_free_cached_info (bfd *abfd)
114
284
{
115
284
  struct ecoff_tdata *tdata;
116
117
284
  if ((bfd_get_format (abfd) == bfd_object
118
284
       || bfd_get_format (abfd) == bfd_core)
119
284
      && (tdata = ecoff_data (abfd)) != NULL)
120
84
    {
121
84
      while (tdata->mips_refhi_list != NULL)
122
0
  {
123
0
    struct mips_hi *ref = tdata->mips_refhi_list;
124
0
    tdata->mips_refhi_list = ref->next;
125
0
    free (ref);
126
0
  }
127
84
      _bfd_ecoff_free_ecoff_debug_info (&tdata->debug_info);
128
84
    }
129
284
  return _bfd_generic_bfd_free_cached_info (abfd);
130
284
}
131
132
/* Initialize a new section.  */
133
134
bool
135
_bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
136
27.4k
{
137
27.4k
  unsigned int i;
138
27.4k
  static struct
139
27.4k
  {
140
27.4k
    const char * name;
141
27.4k
    flagword flags;
142
27.4k
  }
143
27.4k
  section_flags [] =
144
27.4k
  {
145
27.4k
    { _TEXT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
146
27.4k
    { _INIT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
147
27.4k
    { _FINI,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
148
27.4k
    { _DATA,   SEC_ALLOC | SEC_DATA | SEC_LOAD },
149
27.4k
    { _SDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_SMALL_DATA },
150
27.4k
    { _RDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
151
27.4k
    { _LIT8,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
152
27.4k
    { _LIT4,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
153
27.4k
    { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
154
27.4k
    { _PDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
155
27.4k
    { _BSS,    SEC_ALLOC},
156
27.4k
    { _SBSS,   SEC_ALLOC | SEC_SMALL_DATA},
157
    /* An Irix 4 shared libary.  */
158
27.4k
    { _LIB,    SEC_COFF_SHARED_LIBRARY}
159
27.4k
  };
160
161
27.4k
  section->alignment_power = 4;
162
163
383k
  for (i = 0; i < ARRAY_SIZE (section_flags); i++)
164
356k
    if (streq (section->name, section_flags[i].name))
165
74
      {
166
74
  section->flags |= section_flags[i].flags;
167
74
  break;
168
74
      }
169
170
171
  /* Probably any other section name is SEC_NEVER_LOAD, but I'm
172
     uncertain about .init on some systems and I don't know how shared
173
     libraries work.  */
174
175
27.4k
  return _bfd_generic_new_section_hook (abfd, section);
176
27.4k
}
177
178
void
179
_bfd_ecoff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
180
             asection *section ATTRIBUTE_UNUSED,
181
             void *scnhdr ATTRIBUTE_UNUSED)
182
27.4k
{
183
27.4k
}
184
185
/* Determine the machine architecture and type.  This is called from
186
   the generic COFF routines.  It is the inverse of ecoff_get_magic,
187
   below.  This could be an ECOFF backend routine, with one version
188
   for each target, but there aren't all that many ECOFF targets.  */
189
190
bool
191
_bfd_ecoff_set_arch_mach_hook (bfd *abfd, void * filehdr)
192
246
{
193
246
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
194
246
  enum bfd_architecture arch;
195
246
  unsigned long mach;
196
197
246
  switch (internal_f->f_magic)
198
246
    {
199
162
    case MIPS_MAGIC_1:
200
178
    case MIPS_MAGIC_LITTLE:
201
178
    case MIPS_MAGIC_BIG:
202
178
      arch = bfd_arch_mips;
203
178
      mach = bfd_mach_mips3000;
204
178
      break;
205
206
4
    case MIPS_MAGIC_LITTLE2:
207
28
    case MIPS_MAGIC_BIG2:
208
      /* MIPS ISA level 2: the r6000.  */
209
28
      arch = bfd_arch_mips;
210
28
      mach = bfd_mach_mips6000;
211
28
      break;
212
213
2
    case MIPS_MAGIC_LITTLE3:
214
10
    case MIPS_MAGIC_BIG3:
215
      /* MIPS ISA level 3: the r4000.  */
216
10
      arch = bfd_arch_mips;
217
10
      mach = bfd_mach_mips4000;
218
10
      break;
219
220
24
    case ALPHA_MAGIC:
221
24
      arch = bfd_arch_alpha;
222
24
      mach = 0;
223
24
      break;
224
225
6
    default:
226
6
      arch = bfd_arch_obscure;
227
6
      mach = 0;
228
6
      break;
229
246
    }
230
231
246
  return bfd_default_set_arch_mach (abfd, arch, mach);
232
246
}
233
234
bool
235
_bfd_ecoff_no_long_sections (bfd *abfd, int enable)
236
27.4k
{
237
27.4k
  (void) abfd;
238
27.4k
  (void) enable;
239
27.4k
  return false;
240
27.4k
}
241
242
/* Get the magic number to use based on the architecture and machine.
243
   This is the inverse of _bfd_ecoff_set_arch_mach_hook, above.  */
244
245
static int
246
ecoff_get_magic (bfd *abfd)
247
0
{
248
0
  int big, little;
249
250
0
  switch (bfd_get_arch (abfd))
251
0
    {
252
0
    case bfd_arch_mips:
253
0
      switch (bfd_get_mach (abfd))
254
0
  {
255
0
  default:
256
0
  case 0:
257
0
  case bfd_mach_mips3000:
258
0
    big = MIPS_MAGIC_BIG;
259
0
    little = MIPS_MAGIC_LITTLE;
260
0
    break;
261
262
0
  case bfd_mach_mips6000:
263
0
    big = MIPS_MAGIC_BIG2;
264
0
    little = MIPS_MAGIC_LITTLE2;
265
0
    break;
266
267
0
  case bfd_mach_mips4000:
268
0
    big = MIPS_MAGIC_BIG3;
269
0
    little = MIPS_MAGIC_LITTLE3;
270
0
    break;
271
0
  }
272
273
0
      return bfd_big_endian (abfd) ? big : little;
274
275
0
    case bfd_arch_alpha:
276
0
      return ALPHA_MAGIC;
277
278
0
    default:
279
0
      abort ();
280
0
      return 0;
281
0
    }
282
0
}
283
284
/* Get the section s_flags to use for a section.  */
285
286
static long
287
ecoff_sec_to_styp_flags (const char *name, flagword flags)
288
0
{
289
0
  unsigned int i;
290
0
  static struct
291
0
  {
292
0
    const char * name;
293
0
    long flags;
294
0
  }
295
0
  styp_flags [] =
296
0
  {
297
0
    { _TEXT, STYP_TEXT  },
298
0
    { _DATA, STYP_DATA  },
299
0
    { _SDATA, STYP_SDATA  },
300
0
    { _RDATA, STYP_RDATA  },
301
0
    { _LITA, STYP_LITA  },
302
0
    { _LIT8, STYP_LIT8  },
303
0
    { _LIT4, STYP_LIT4  },
304
0
    { _BSS, STYP_BSS  },
305
0
    { _SBSS, STYP_SBSS  },
306
0
    { _INIT, STYP_ECOFF_INIT },
307
0
    { _FINI, STYP_ECOFF_FINI },
308
0
    { _PDATA, STYP_PDATA  },
309
0
    { _XDATA, STYP_XDATA  },
310
0
    { _LIB, STYP_ECOFF_LIB  },
311
0
    { _GOT, STYP_GOT  },
312
0
    { _HASH, STYP_HASH  },
313
0
    { _DYNAMIC, STYP_DYNAMIC  },
314
0
    { _LIBLIST, STYP_LIBLIST  },
315
0
    { _RELDYN, STYP_RELDYN  },
316
0
    { _CONFLIC, STYP_CONFLIC  },
317
0
    { _DYNSTR, STYP_DYNSTR  },
318
0
    { _DYNSYM, STYP_DYNSYM  },
319
0
    { _RCONST, STYP_RCONST  }
320
0
  };
321
0
  long styp = 0;
322
323
0
  for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
324
0
    if (streq (name, styp_flags[i].name))
325
0
      {
326
0
  styp = styp_flags[i].flags;
327
0
  break;
328
0
      }
329
330
0
  if (styp == 0)
331
0
    {
332
0
      if (streq (name, _COMMENT))
333
0
  {
334
0
    styp = STYP_COMMENT;
335
0
    flags &=~ SEC_NEVER_LOAD;
336
0
  }
337
0
      else if (flags & SEC_CODE)
338
0
  styp = STYP_TEXT;
339
0
      else if (flags & SEC_DATA)
340
0
  styp = STYP_DATA;
341
0
      else if (flags & SEC_READONLY)
342
0
  styp = STYP_RDATA;
343
0
      else if (flags & SEC_LOAD)
344
0
  styp = STYP_REG;
345
0
      else
346
0
  styp = STYP_BSS;
347
0
    }
348
349
0
  if (flags & SEC_NEVER_LOAD)
350
0
    styp |= STYP_NOLOAD;
351
352
0
  return styp;
353
0
}
354
355
/* Get the BFD flags to use for a section.  */
356
357
bool
358
_bfd_ecoff_styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
359
            void * hdr,
360
            const char *name ATTRIBUTE_UNUSED,
361
            asection *section ATTRIBUTE_UNUSED,
362
            flagword * flags_ptr)
363
27.4k
{
364
27.4k
  struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
365
27.4k
  long styp_flags = internal_s->s_flags;
366
27.4k
  flagword sec_flags = 0;
367
368
27.4k
  if (styp_flags & STYP_NOLOAD)
369
11.3k
    sec_flags |= SEC_NEVER_LOAD;
370
371
  /* For 386 COFF, at least, an unloadable text or data section is
372
     actually a shared library section.  */
373
27.4k
  if ((styp_flags & STYP_TEXT)
374
27.4k
      || (styp_flags & STYP_ECOFF_INIT)
375
27.4k
      || (styp_flags & STYP_ECOFF_FINI)
376
27.4k
      || (styp_flags & STYP_DYNAMIC)
377
27.4k
      || (styp_flags & STYP_LIBLIST)
378
27.4k
      || (styp_flags & STYP_RELDYN)
379
27.4k
      || styp_flags == STYP_CONFLIC
380
27.4k
      || (styp_flags & STYP_DYNSTR)
381
27.4k
      || (styp_flags & STYP_DYNSYM)
382
27.4k
      || (styp_flags & STYP_HASH))
383
19.3k
    {
384
19.3k
      if (sec_flags & SEC_NEVER_LOAD)
385
11.0k
  sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
386
8.36k
      else
387
8.36k
  sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
388
19.3k
    }
389
8.03k
  else if ((styp_flags & STYP_DATA)
390
8.03k
     || (styp_flags & STYP_RDATA)
391
8.03k
     || (styp_flags & STYP_SDATA)
392
8.03k
     || styp_flags == STYP_PDATA
393
8.03k
     || styp_flags == STYP_XDATA
394
8.03k
     || (styp_flags & STYP_GOT)
395
8.03k
     || styp_flags == STYP_RCONST)
396
1.13k
    {
397
1.13k
      if (sec_flags & SEC_NEVER_LOAD)
398
134
  sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
399
1.00k
      else
400
1.00k
  sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
401
1.13k
      if ((styp_flags & STYP_RDATA)
402
1.13k
    || styp_flags == STYP_PDATA
403
1.13k
    || styp_flags == STYP_RCONST)
404
310
  sec_flags |= SEC_READONLY;
405
1.13k
      if (styp_flags & STYP_SDATA)
406
190
  sec_flags |= SEC_SMALL_DATA;
407
1.13k
    }
408
6.90k
  else if (styp_flags & STYP_SBSS)
409
46
    sec_flags |= SEC_ALLOC | SEC_SMALL_DATA;
410
6.85k
  else if (styp_flags & STYP_BSS)
411
144
    sec_flags |= SEC_ALLOC;
412
6.71k
  else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
413
10
    sec_flags |= SEC_NEVER_LOAD;
414
6.70k
  else if ((styp_flags & STYP_LITA)
415
6.70k
     || (styp_flags & STYP_LIT8)
416
6.70k
     || (styp_flags & STYP_LIT4))
417
330
    sec_flags |= SEC_DATA |SEC_SMALL_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
418
6.37k
  else if (styp_flags & STYP_ECOFF_LIB)
419
16
    sec_flags |= SEC_COFF_SHARED_LIBRARY;
420
6.35k
  else
421
6.35k
    sec_flags |= SEC_ALLOC | SEC_LOAD;
422
423
27.4k
  * flags_ptr = sec_flags;
424
27.4k
  return true;
425
27.4k
}
426

427
/* Read in the symbolic header for an ECOFF object file.  */
428
429
static bool
430
ecoff_slurp_symbolic_header (bfd *abfd)
431
0
{
432
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
433
0
  bfd_size_type external_hdr_size;
434
0
  void * raw = NULL;
435
0
  HDRR *internal_symhdr;
436
437
  /* See if we've already read it in.  */
438
0
  if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
439
0
      backend->debug_swap.sym_magic)
440
0
    return true;
441
442
  /* See whether there is a symbolic header.  */
443
0
  if (ecoff_data (abfd)->sym_filepos == 0)
444
0
    {
445
0
      abfd->symcount = 0;
446
0
      return true;
447
0
    }
448
449
  /* At this point bfd_get_symcount (abfd) holds the number of symbols
450
     as read from the file header, but on ECOFF this is always the
451
     size of the symbolic information header.  It would be cleaner to
452
     handle this when we first read the file in coffgen.c.  */
453
0
  external_hdr_size = backend->debug_swap.external_hdr_size;
454
0
  if (bfd_get_symcount (abfd) != external_hdr_size)
455
0
    {
456
0
      bfd_set_error (bfd_error_bad_value);
457
0
      return false;
458
0
    }
459
460
  /* Read the symbolic information header.  */
461
0
  if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0)
462
0
    goto error_return;
463
0
  raw = _bfd_malloc_and_read (abfd, external_hdr_size, external_hdr_size);
464
0
  if (raw == NULL)
465
0
    goto error_return;
466
467
0
  internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
468
0
  (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
469
470
0
  if (internal_symhdr->magic != backend->debug_swap.sym_magic)
471
0
    {
472
0
      bfd_set_error (bfd_error_bad_value);
473
0
      goto error_return;
474
0
    }
475
476
0
#define FIX(start, count) \
477
0
  if (internal_symhdr->start == 0) \
478
0
    internal_symhdr->count = 0;
479
480
0
  FIX (cbLineOffset, cbLine);
481
0
  FIX (cbDnOffset, idnMax);
482
0
  FIX (cbPdOffset, ipdMax);
483
0
  FIX (cbSymOffset, isymMax);
484
0
  FIX (cbOptOffset, ioptMax);
485
0
  FIX (cbAuxOffset, iauxMax);
486
0
  FIX (cbSsOffset, issMax);
487
0
  FIX (cbSsExtOffset, issExtMax);
488
0
  FIX (cbFdOffset, ifdMax);
489
0
  FIX (cbRfdOffset, crfd);
490
0
  FIX (cbExtOffset, iextMax);
491
0
#undef FIX
492
493
  /* Now we can get the correct number of symbols.  */
494
0
  abfd->symcount = internal_symhdr->isymMax + internal_symhdr->iextMax;
495
496
0
  free (raw);
497
0
  return true;
498
0
 error_return:
499
0
  free (raw);
500
0
  return false;
501
0
}
502
503
/* Read in and swap the important symbolic information for an ECOFF
504
   object file.  This is called by gdb via the read_debug_info entry
505
   point in the backend structure.  */
506
507
bool
508
_bfd_ecoff_slurp_symbolic_info (bfd *abfd,
509
        asection *ignore ATTRIBUTE_UNUSED,
510
        struct ecoff_debug_info *debug)
511
1
{
512
1
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
513
1
  HDRR *internal_symhdr;
514
1
  bfd_size_type raw_base;
515
1
  bfd_size_type raw_size;
516
1
  void * raw;
517
1
  bfd_size_type external_fdr_size;
518
1
  char *fraw_src;
519
1
  char *fraw_end;
520
1
  struct fdr *fdr_ptr;
521
1
  bfd_size_type raw_end;
522
1
  bfd_size_type cb_end;
523
1
  file_ptr pos;
524
1
  size_t amt;
525
526
1
  BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
527
528
  /* Check whether we've already gotten it, and whether there's any to
529
     get.  */
530
1
  if (debug->alloc_syments)
531
0
    return true;
532
1
  if (ecoff_data (abfd)->sym_filepos == 0)
533
1
    {
534
1
      abfd->symcount = 0;
535
1
      return true;
536
1
    }
537
538
0
  if (! ecoff_slurp_symbolic_header (abfd))
539
0
    return false;
540
541
0
  internal_symhdr = &debug->symbolic_header;
542
543
  /* Read all the symbolic information at once.  */
544
0
  raw_base = (ecoff_data (abfd)->sym_filepos
545
0
        + backend->debug_swap.external_hdr_size);
546
547
  /* Alpha ecoff makes the determination of raw_size difficult. It has
548
     an undocumented debug data section between the symhdr and the first
549
     documented section. And the ordering of the sections varies between
550
     statically and dynamically linked executables.
551
     If bfd supports SEEK_END someday, this code could be simplified.  */
552
0
  raw_end = raw_base;
553
554
0
#define UPDATE_RAW_END(start, count, size) \
555
0
  do                  \
556
0
    if (internal_symhdr->count != 0)         \
557
0
      {                 \
558
0
  if (internal_symhdr->start < raw_base)       \
559
0
    goto err;             \
560
0
  if (_bfd_mul_overflow ((unsigned long) internal_symhdr->count, \
561
0
             (size), &amt))       \
562
0
    goto err;             \
563
0
  cb_end = internal_symhdr->start + amt;        \
564
0
  if (cb_end < internal_symhdr->start)       \
565
0
    goto err;             \
566
0
  if (cb_end > raw_end)           \
567
0
    raw_end = cb_end;           \
568
0
      }                 \
569
0
  while (0)
570
571
0
  UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
572
0
  UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
573
0
  UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
574
0
  UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
575
  /* eraxxon@alumni.rice.edu: ioptMax refers to the size of the
576
     optimization symtab, not the number of entries.  */
577
0
  UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
578
0
  UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
579
0
  UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
580
0
  UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
581
0
  UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
582
0
  UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
583
0
  UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
584
585
0
#undef UPDATE_RAW_END
586
587
0
  raw_size = raw_end - raw_base;
588
0
  if (raw_size == 0)
589
0
    {
590
0
      ecoff_data (abfd)->sym_filepos = 0;
591
0
      return true;
592
0
    }
593
0
  pos = ecoff_data (abfd)->sym_filepos;
594
0
  pos += backend->debug_swap.external_hdr_size;
595
0
  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
596
0
    return false;
597
0
  raw = _bfd_alloc_and_read (abfd, raw_size, raw_size);
598
0
  if (raw == NULL)
599
0
    return false;
600
601
0
  debug->alloc_syments = true;
602
603
  /* Get pointers for the numeric offsets in the HDRR structure.  */
604
0
#define FIX(start, count, ptr, type) \
605
0
  if (internal_symhdr->count == 0)         \
606
0
    debug->ptr = NULL;             \
607
0
  else                  \
608
0
    debug->ptr = (type) ((char *) raw         \
609
0
       + (internal_symhdr->start - raw_base))
610
611
0
  FIX (cbLineOffset, cbLine, line, unsigned char *);
612
0
  FIX (cbDnOffset, idnMax, external_dnr, void *);
613
0
  FIX (cbPdOffset, ipdMax, external_pdr, void *);
614
0
  FIX (cbSymOffset, isymMax, external_sym, void *);
615
0
  FIX (cbOptOffset, ioptMax, external_opt, void *);
616
0
  FIX (cbAuxOffset, iauxMax, external_aux, union aux_ext *);
617
0
  FIX (cbSsOffset, issMax, ss, char *);
618
0
  FIX (cbSsExtOffset, issExtMax, ssext, char *);
619
0
  FIX (cbFdOffset, ifdMax, external_fdr, void *);
620
0
  FIX (cbRfdOffset, crfd, external_rfd, void *);
621
0
  FIX (cbExtOffset, iextMax, external_ext, void *);
622
0
#undef FIX
623
624
  /* Ensure string sections are zero terminated.  */
625
0
  if (debug->ss)
626
0
    debug->ss[internal_symhdr->issMax - 1] = 0;
627
0
  if (debug->ssext)
628
0
    debug->ssext[internal_symhdr->issExtMax - 1] = 0;
629
630
  /* I don't want to always swap all the data, because it will just
631
     waste time and most programs will never look at it.  The only
632
     time the linker needs most of the debugging information swapped
633
     is when linking big-endian and little-endian MIPS object files
634
     together, which is not a common occurrence.
635
636
     We need to look at the fdr to deal with a lot of information in
637
     the symbols, so we swap them here.  */
638
0
  if (_bfd_mul_overflow ((unsigned long) internal_symhdr->ifdMax,
639
0
       sizeof (struct fdr), &amt))
640
0
    {
641
0
    err:
642
0
      bfd_set_error (bfd_error_file_too_big);
643
0
      return false;
644
0
    }
645
0
  debug->fdr = (FDR *) bfd_alloc (abfd, amt);
646
0
  if (debug->fdr == NULL)
647
0
    return false;
648
0
  external_fdr_size = backend->debug_swap.external_fdr_size;
649
0
  fdr_ptr = debug->fdr;
650
0
  fraw_src = (char *) debug->external_fdr;
651
  /* PR 17512: file: 3372-1243-0.004.  */
652
0
  if (fraw_src == NULL && internal_symhdr->ifdMax > 0)
653
0
    return false;
654
0
  fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
655
0
  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
656
0
    (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
657
658
0
  return true;
659
0
}
660

661
/* ECOFF symbol table routines.  The ECOFF symbol table is described
662
   in gcc/mips-tfile.c.  */
663
664
/* ECOFF uses two common sections.  One is the usual one, and the
665
   other is for small objects.  All the small objects are kept
666
   together, and then referenced via the gp pointer, which yields
667
   faster assembler code.  This is what we use for the small common
668
   section.  */
669
static asection ecoff_scom_section;
670
static const asymbol ecoff_scom_symbol =
671
  GLOBAL_SYM_INIT (SCOMMON, &ecoff_scom_section);
672
static asection ecoff_scom_section =
673
  BFD_FAKE_SECTION (ecoff_scom_section, &ecoff_scom_symbol,
674
        SCOMMON, 0, SEC_IS_COMMON | SEC_SMALL_DATA);
675
676
/* Create an empty symbol.  */
677
678
asymbol *
679
_bfd_ecoff_make_empty_symbol (bfd *abfd)
680
27.4k
{
681
27.4k
  ecoff_symbol_type *new_symbol;
682
27.4k
  size_t amt = sizeof (ecoff_symbol_type);
683
684
27.4k
  new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
685
27.4k
  if (new_symbol == NULL)
686
0
    return NULL;
687
27.4k
  new_symbol->symbol.section = NULL;
688
27.4k
  new_symbol->fdr = NULL;
689
27.4k
  new_symbol->local = false;
690
27.4k
  new_symbol->native = NULL;
691
27.4k
  new_symbol->symbol.the_bfd = abfd;
692
27.4k
  return &new_symbol->symbol;
693
27.4k
}
694
695
/* Set the BFD flags and section for an ECOFF symbol.  */
696
697
static bool
698
ecoff_set_symbol_info (bfd *abfd,
699
           SYMR *ecoff_sym,
700
           asymbol *asym,
701
           int ext,
702
           int weak)
703
0
{
704
0
  asym->the_bfd = abfd;
705
0
  asym->value = ecoff_sym->value;
706
0
  asym->section = &bfd_debug_section;
707
0
  asym->udata.i = 0;
708
709
  /* Most symbol types are just for debugging.  */
710
0
  switch (ecoff_sym->st)
711
0
    {
712
0
    case stGlobal:
713
0
    case stStatic:
714
0
    case stLabel:
715
0
    case stProc:
716
0
    case stStaticProc:
717
0
      break;
718
0
    case stNil:
719
0
      if (ECOFF_IS_STAB (ecoff_sym))
720
0
  {
721
0
    asym->flags = BSF_DEBUGGING;
722
0
    return true;
723
0
  }
724
0
      break;
725
0
    default:
726
0
      asym->flags = BSF_DEBUGGING;
727
0
      return true;
728
0
    }
729
730
0
  if (weak)
731
0
    asym->flags = BSF_EXPORT | BSF_WEAK;
732
0
  else if (ext)
733
0
    asym->flags = BSF_EXPORT | BSF_GLOBAL;
734
0
  else
735
0
    {
736
0
      asym->flags = BSF_LOCAL;
737
      /* Normally, a local stProc symbol will have a corresponding
738
   external symbol.  We mark the local symbol as a debugging
739
   symbol, in order to prevent nm from printing both out.
740
   Similarly, we mark stLabel and stabs symbols as debugging
741
   symbols.  In both cases, we do want to set the value
742
   correctly based on the symbol class.  */
743
0
      if (ecoff_sym->st == stProc
744
0
    || ecoff_sym->st == stLabel
745
0
    || ECOFF_IS_STAB (ecoff_sym))
746
0
  asym->flags |= BSF_DEBUGGING;
747
0
    }
748
749
0
  if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
750
0
    asym->flags |= BSF_FUNCTION;
751
752
0
  switch (ecoff_sym->sc)
753
0
    {
754
0
    case scNil:
755
      /* Used for compiler generated labels.  Leave them in the
756
   debugging section, and mark them as local.  If BSF_DEBUGGING
757
   is set, then nm does not display them for some reason.  If no
758
   flags are set then the linker whines about them.  */
759
0
      asym->flags = BSF_LOCAL;
760
0
      break;
761
0
    case scText:
762
0
      asym->section = bfd_make_section_old_way (abfd, _TEXT);
763
0
      asym->value -= asym->section->vma;
764
0
      break;
765
0
    case scData:
766
0
      asym->section = bfd_make_section_old_way (abfd, _DATA);
767
0
      asym->value -= asym->section->vma;
768
0
      break;
769
0
    case scBss:
770
0
      asym->section = bfd_make_section_old_way (abfd, _BSS);
771
0
      asym->value -= asym->section->vma;
772
0
      break;
773
0
    case scRegister:
774
0
      asym->flags = BSF_DEBUGGING;
775
0
      break;
776
0
    case scAbs:
777
0
      asym->section = bfd_abs_section_ptr;
778
0
      break;
779
0
    case scUndefined:
780
0
      asym->section = bfd_und_section_ptr;
781
0
      asym->flags = 0;
782
0
      asym->value = 0;
783
0
      break;
784
0
    case scCdbLocal:
785
0
    case scBits:
786
0
    case scCdbSystem:
787
0
    case scRegImage:
788
0
    case scInfo:
789
0
    case scUserStruct:
790
0
      asym->flags = BSF_DEBUGGING;
791
0
      break;
792
0
    case scSData:
793
0
      asym->section = bfd_make_section_old_way (abfd, ".sdata");
794
0
      asym->value -= asym->section->vma;
795
0
      break;
796
0
    case scSBss:
797
0
      asym->section = bfd_make_section_old_way (abfd, ".sbss");
798
0
      asym->value -= asym->section->vma;
799
0
      break;
800
0
    case scRData:
801
0
      asym->section = bfd_make_section_old_way (abfd, ".rdata");
802
0
      asym->value -= asym->section->vma;
803
0
      break;
804
0
    case scVar:
805
0
      asym->flags = BSF_DEBUGGING;
806
0
      break;
807
0
    case scCommon:
808
0
      if (asym->value > ecoff_data (abfd)->gp_size)
809
0
  {
810
0
    asym->section = bfd_com_section_ptr;
811
0
    asym->flags = 0;
812
0
    break;
813
0
  }
814
      /* Fall through.  */
815
0
    case scSCommon:
816
0
      asym->section = &ecoff_scom_section;
817
0
      asym->flags = 0;
818
0
      break;
819
0
    case scVarRegister:
820
0
    case scVariant:
821
0
      asym->flags = BSF_DEBUGGING;
822
0
      break;
823
0
    case scSUndefined:
824
0
      asym->section = bfd_und_section_ptr;
825
0
      asym->flags = 0;
826
0
      asym->value = 0;
827
0
      break;
828
0
    case scInit:
829
0
      asym->section = bfd_make_section_old_way (abfd, ".init");
830
0
      asym->value -= asym->section->vma;
831
0
      break;
832
0
    case scBasedVar:
833
0
    case scXData:
834
0
    case scPData:
835
0
      asym->flags = BSF_DEBUGGING;
836
0
      break;
837
0
    case scFini:
838
0
      asym->section = bfd_make_section_old_way (abfd, ".fini");
839
0
      asym->value -= asym->section->vma;
840
0
      break;
841
0
    case scRConst:
842
0
      asym->section = bfd_make_section_old_way (abfd, ".rconst");
843
0
      asym->value -= asym->section->vma;
844
0
      break;
845
0
    default:
846
0
      break;
847
0
    }
848
849
  /* Look for special constructors symbols and make relocation entries
850
     in a special construction section.  These are produced by the
851
     -fgnu-linker argument to g++.  */
852
0
  if (ECOFF_IS_STAB (ecoff_sym))
853
0
    {
854
0
      switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
855
0
  {
856
0
  default:
857
0
    break;
858
859
0
  case N_SETA:
860
0
  case N_SETT:
861
0
  case N_SETD:
862
0
  case N_SETB:
863
    /* Mark the symbol as a constructor.  */
864
0
    asym->flags |= BSF_CONSTRUCTOR;
865
0
    break;
866
0
  }
867
0
    }
868
0
  return true;
869
0
}
870
871
/* Read an ECOFF symbol table.  */
872
873
bool
874
_bfd_ecoff_slurp_symbol_table (bfd *abfd)
875
0
{
876
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
877
0
  const bfd_size_type external_ext_size
878
0
    = backend->debug_swap.external_ext_size;
879
0
  const bfd_size_type external_sym_size
880
0
    = backend->debug_swap.external_sym_size;
881
0
  void (* const swap_ext_in) (bfd *, void *, EXTR *)
882
0
    = backend->debug_swap.swap_ext_in;
883
0
  void (* const swap_sym_in) (bfd *, void *, SYMR *)
884
0
    = backend->debug_swap.swap_sym_in;
885
0
  ecoff_symbol_type *internal;
886
0
  ecoff_symbol_type *internal_ptr;
887
0
  char *eraw_src;
888
0
  char *eraw_end;
889
0
  FDR *fdr_ptr;
890
0
  FDR *fdr_end;
891
0
  size_t amt;
892
893
  /* If we've already read in the symbol table, do nothing.  */
894
0
  if (ecoff_data (abfd)->canonical_symbols != NULL)
895
0
    return true;
896
897
  /* Get the symbolic information.  */
898
0
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
899
0
          &ecoff_data (abfd)->debug_info))
900
0
    return false;
901
0
  if (bfd_get_symcount (abfd) == 0)
902
0
    return true;
903
904
0
  if (_bfd_mul_overflow (bfd_get_symcount (abfd),
905
0
       sizeof (ecoff_symbol_type), &amt))
906
0
    {
907
0
      bfd_set_error (bfd_error_file_too_big);
908
0
      return false;
909
0
    }
910
0
  internal = (ecoff_symbol_type *) bfd_alloc (abfd, amt);
911
0
  if (internal == NULL)
912
0
    return false;
913
914
0
  internal_ptr = internal;
915
0
  eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
916
0
  eraw_end = (eraw_src
917
0
        + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
918
0
     * external_ext_size));
919
0
  for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
920
0
    {
921
0
      EXTR internal_esym;
922
923
0
      (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
924
925
      /* PR 17512: file: 3372-1000-0.004.  */
926
0
      HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
927
0
      if (internal_esym.asym.iss >= symhdr->issExtMax
928
0
    || internal_esym.asym.iss < 0)
929
0
  {
930
0
    bfd_set_error (bfd_error_bad_value);
931
0
    return false;
932
0
  }
933
934
0
      internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
935
0
           + internal_esym.asym.iss);
936
937
0
      if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
938
0
          &internal_ptr->symbol, 1,
939
0
          internal_esym.weakext))
940
0
  return false;
941
942
      /* The alpha uses a negative ifd field for section symbols.  */
943
      /* PR 17512: file: 3372-1983-0.004.  */
944
0
      if (internal_esym.ifd >= symhdr->ifdMax
945
0
    || internal_esym.ifd < 0)
946
0
  internal_ptr->fdr = NULL;
947
0
      else
948
0
  internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
949
0
           + internal_esym.ifd);
950
0
      internal_ptr->local = false;
951
0
      internal_ptr->native = (void *) eraw_src;
952
0
    }
953
954
  /* The local symbols must be accessed via the fdr's, because the
955
     string and aux indices are relative to the fdr information.  */
956
0
  fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
957
0
  fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
958
0
  for (; fdr_ptr < fdr_end; fdr_ptr++)
959
0
    {
960
0
      char *lraw_src;
961
0
      char *lraw_end;
962
0
      HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
963
964
0
      if (fdr_ptr->csym == 0)
965
0
  continue;
966
0
      if (fdr_ptr->isymBase < 0
967
0
    || fdr_ptr->isymBase > symhdr->isymMax
968
0
    || fdr_ptr->csym < 0
969
0
    || fdr_ptr->csym > ((long) bfd_get_symcount (abfd)
970
0
            - (internal_ptr - internal))
971
0
    || fdr_ptr->issBase < 0
972
0
    || fdr_ptr->issBase > symhdr->issMax)
973
0
  {
974
0
    bfd_set_error (bfd_error_bad_value);
975
0
    return false;
976
0
  }
977
0
      lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
978
0
      + fdr_ptr->isymBase * external_sym_size);
979
0
      lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
980
0
      for (;
981
0
     lraw_src < lraw_end;
982
0
     lraw_src += external_sym_size, internal_ptr++)
983
0
  {
984
0
    SYMR internal_sym;
985
986
0
    (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
987
988
0
    if (internal_sym.iss >= symhdr->issMax - fdr_ptr->issBase
989
0
        || internal_sym.iss < 0)
990
0
      {
991
0
        bfd_set_error (bfd_error_bad_value);
992
0
        return false;
993
0
      }
994
0
    internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
995
0
               + fdr_ptr->issBase
996
0
               + internal_sym.iss);
997
0
    if (!ecoff_set_symbol_info (abfd, &internal_sym,
998
0
              &internal_ptr->symbol, 0, 0))
999
0
      return false;
1000
0
    internal_ptr->fdr = fdr_ptr;
1001
0
    internal_ptr->local = true;
1002
0
    internal_ptr->native = (void *) lraw_src;
1003
0
  }
1004
0
    }
1005
1006
  /* PR 17512: file: 3372-3080-0.004.
1007
     A discrepancy between ecoff_data (abfd)->debug_info.symbolic_header.isymMax
1008
     and ecoff_data (abfd)->debug_info.symbolic_header.ifdMax can mean that
1009
     we have fewer symbols than we were expecting.  Allow for this by updating
1010
     the symbol count and warning the user.  */
1011
0
  if (internal_ptr - internal < (ptrdiff_t) bfd_get_symcount (abfd))
1012
0
    {
1013
0
      abfd->symcount = internal_ptr - internal;
1014
0
      _bfd_error_handler
1015
  /* xgettext:c-format */
1016
0
  (_("%pB: warning: isymMax (%ld) is greater than ifdMax (%ld)"),
1017
0
   abfd, ecoff_data (abfd)->debug_info.symbolic_header.isymMax,
1018
0
   ecoff_data (abfd)->debug_info.symbolic_header.ifdMax);
1019
0
    }
1020
1021
0
  ecoff_data (abfd)->canonical_symbols = internal;
1022
1023
0
  return true;
1024
0
}
1025
1026
/* Return the amount of space needed for the canonical symbols.  */
1027
1028
long
1029
_bfd_ecoff_get_symtab_upper_bound (bfd *abfd)
1030
1
{
1031
1
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
1032
1
          &ecoff_data (abfd)->debug_info))
1033
0
    return -1;
1034
1035
1
  if (bfd_get_symcount (abfd) == 0)
1036
1
    return 0;
1037
1038
0
  return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1039
1
}
1040
1041
/* Get the canonical symbols.  */
1042
1043
long
1044
_bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
1045
0
{
1046
0
  unsigned int counter = 0;
1047
0
  ecoff_symbol_type *symbase;
1048
0
  ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1049
1050
0
  if (! _bfd_ecoff_slurp_symbol_table (abfd))
1051
0
    return -1;
1052
0
  if (bfd_get_symcount (abfd) == 0)
1053
0
    return 0;
1054
1055
0
  symbase = ecoff_data (abfd)->canonical_symbols;
1056
0
  while (counter < bfd_get_symcount (abfd))
1057
0
    {
1058
0
      *(location++) = symbase++;
1059
0
      counter++;
1060
0
    }
1061
0
  *location++ = NULL;
1062
0
  return bfd_get_symcount (abfd);
1063
0
}
1064
1065
/* Turn ECOFF type information into a printable string.
1066
   ecoff_emit_aggregate and ecoff_type_to_string are from
1067
   gcc/mips-tdump.c, with swapping added and used_ptr removed.  */
1068
1069
/* Write aggregate information to a string.  */
1070
1071
static void
1072
ecoff_emit_aggregate (bfd *abfd,
1073
          FDR *fdr,
1074
          char *string,
1075
          RNDXR *rndx,
1076
          long isym,
1077
          const char *which)
1078
0
{
1079
0
  const struct ecoff_debug_swap * const debug_swap =
1080
0
    &ecoff_backend (abfd)->debug_swap;
1081
0
  struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1082
0
  unsigned int ifd = rndx->rfd;
1083
0
  unsigned int indx = rndx->index;
1084
0
  const char *name;
1085
1086
0
  if (ifd == 0xfff)
1087
0
    ifd = isym;
1088
1089
  /* An ifd of -1 is an opaque type.  An escaped index of 0 is a
1090
     struct return type of a procedure compiled without -g.  */
1091
0
  if (ifd == 0xffffffff
1092
0
      || (rndx->rfd == 0xfff && indx == 0))
1093
0
    name = "<undefined>";
1094
0
  else if (indx == indexNil)
1095
0
    name = "<no name>";
1096
0
  else
1097
0
    {
1098
0
      SYMR sym;
1099
1100
0
      if (debug_info->external_rfd == NULL)
1101
0
  fdr = debug_info->fdr + ifd;
1102
0
      else
1103
0
  {
1104
0
    RFDT rfd;
1105
1106
0
    (*debug_swap->swap_rfd_in) (abfd,
1107
0
              ((char *) debug_info->external_rfd
1108
0
               + ((fdr->rfdBase + ifd)
1109
0
            * debug_swap->external_rfd_size)),
1110
0
              &rfd);
1111
0
    fdr = debug_info->fdr + rfd;
1112
0
  }
1113
1114
0
      indx += fdr->isymBase;
1115
1116
0
      (*debug_swap->swap_sym_in) (abfd,
1117
0
          ((char *) debug_info->external_sym
1118
0
           + indx * debug_swap->external_sym_size),
1119
0
          &sym);
1120
1121
0
      name = debug_info->ss + fdr->issBase + sym.iss;
1122
0
    }
1123
1124
0
  sprintf (string,
1125
0
     "%s %s { ifd = %u, index = %lu }",
1126
0
     which, name, ifd,
1127
0
     ((unsigned long) indx
1128
0
      + debug_info->symbolic_header.iextMax));
1129
0
}
1130
1131
/* Convert the type information to string format.  */
1132
1133
static char *
1134
ecoff_type_to_string (bfd *abfd, FDR *fdr, unsigned int indx, char *buff)
1135
0
{
1136
0
  union aux_ext *aux_ptr;
1137
0
  int bigendian;
1138
0
  AUXU u;
1139
0
  struct qual
1140
0
  {
1141
0
    unsigned int  type;
1142
0
    int  low_bound;
1143
0
    int  high_bound;
1144
0
    int  stride;
1145
0
  } qualifiers[7];
1146
0
  unsigned int basic_type;
1147
0
  int i;
1148
0
  char buffer1[1024];
1149
0
  char *p1 = buffer1;
1150
0
  char *p2 = buff;
1151
0
  RNDXR rndx;
1152
1153
0
  aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1154
0
  bigendian = fdr->fBigendian;
1155
1156
0
  for (i = 0; i < 7; i++)
1157
0
    {
1158
0
      qualifiers[i].low_bound = 0;
1159
0
      qualifiers[i].high_bound = 0;
1160
0
      qualifiers[i].stride = 0;
1161
0
    }
1162
1163
0
  if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1164
0
    return "-1 (no type)";
1165
0
  _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1166
1167
0
  basic_type = u.ti.bt;
1168
0
  qualifiers[0].type = u.ti.tq0;
1169
0
  qualifiers[1].type = u.ti.tq1;
1170
0
  qualifiers[2].type = u.ti.tq2;
1171
0
  qualifiers[3].type = u.ti.tq3;
1172
0
  qualifiers[4].type = u.ti.tq4;
1173
0
  qualifiers[5].type = u.ti.tq5;
1174
0
  qualifiers[6].type = tqNil;
1175
1176
  /* Go get the basic type.  */
1177
0
  switch (basic_type)
1178
0
    {
1179
0
    case btNil:     /* Undefined.  */
1180
0
      strcpy (p1, "nil");
1181
0
      break;
1182
1183
0
    case btAdr:     /* Address - integer same size as pointer.  */
1184
0
      strcpy (p1, "address");
1185
0
      break;
1186
1187
0
    case btChar:   /* Character.  */
1188
0
      strcpy (p1, "char");
1189
0
      break;
1190
1191
0
    case btUChar:   /* Unsigned character.  */
1192
0
      strcpy (p1, "unsigned char");
1193
0
      break;
1194
1195
0
    case btShort:   /* Short.  */
1196
0
      strcpy (p1, "short");
1197
0
      break;
1198
1199
0
    case btUShort:   /* Unsigned short.  */
1200
0
      strcpy (p1, "unsigned short");
1201
0
      break;
1202
1203
0
    case btInt:     /* Int.  */
1204
0
      strcpy (p1, "int");
1205
0
      break;
1206
1207
0
    case btUInt:   /* Unsigned int.  */
1208
0
      strcpy (p1, "unsigned int");
1209
0
      break;
1210
1211
0
    case btLong:   /* Long.  */
1212
0
      strcpy (p1, "long");
1213
0
      break;
1214
1215
0
    case btULong:   /* Unsigned long.  */
1216
0
      strcpy (p1, "unsigned long");
1217
0
      break;
1218
1219
0
    case btFloat:   /* Float (real).  */
1220
0
      strcpy (p1, "float");
1221
0
      break;
1222
1223
0
    case btDouble:   /* Double (real).  */
1224
0
      strcpy (p1, "double");
1225
0
      break;
1226
1227
      /* Structures add 1-2 aux words:
1228
   1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1229
   2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1230
1231
0
    case btStruct:   /* Structure (Record).  */
1232
0
      _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1233
0
      ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1234
0
          (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1235
0
          "struct");
1236
0
      indx++;     /* Skip aux words.  */
1237
0
      break;
1238
1239
      /* Unions add 1-2 aux words:
1240
   1st word is [ST_RFDESCAPE, offset] pointer to union def;
1241
   2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1242
1243
0
    case btUnion:   /* Union.  */
1244
0
      _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1245
0
      ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1246
0
          (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1247
0
          "union");
1248
0
      indx++;     /* Skip aux words.  */
1249
0
      break;
1250
1251
      /* Enumerations add 1-2 aux words:
1252
   1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1253
   2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1254
1255
0
    case btEnum:   /* Enumeration.  */
1256
0
      _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1257
0
      ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1258
0
          (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1259
0
          "enum");
1260
0
      indx++;     /* Skip aux words.  */
1261
0
      break;
1262
1263
0
    case btTypedef:   /* Defined via a typedef, isymRef points.  */
1264
0
      strcpy (p1, "typedef");
1265
0
      break;
1266
1267
0
    case btRange:   /* Subrange of int.  */
1268
0
      strcpy (p1, "subrange");
1269
0
      break;
1270
1271
0
    case btSet:     /* Pascal sets.  */
1272
0
      strcpy (p1, "set");
1273
0
      break;
1274
1275
0
    case btComplex:   /* Fortran complex.  */
1276
0
      strcpy (p1, "complex");
1277
0
      break;
1278
1279
0
    case btDComplex:   /* Fortran double complex.  */
1280
0
      strcpy (p1, "double complex");
1281
0
      break;
1282
1283
0
    case btIndirect:   /* Forward or unnamed typedef.  */
1284
0
      strcpy (p1, "forward/unamed typedef");
1285
0
      break;
1286
1287
0
    case btFixedDec:   /* Fixed Decimal.  */
1288
0
      strcpy (p1, "fixed decimal");
1289
0
      break;
1290
1291
0
    case btFloatDec:   /* Float Decimal.  */
1292
0
      strcpy (p1, "float decimal");
1293
0
      break;
1294
1295
0
    case btString:   /* Varying Length Character String.  */
1296
0
      strcpy (p1, "string");
1297
0
      break;
1298
1299
0
    case btBit:     /* Aligned Bit String.  */
1300
0
      strcpy (p1, "bit");
1301
0
      break;
1302
1303
0
    case btPicture:   /* Picture.  */
1304
0
      strcpy (p1, "picture");
1305
0
      break;
1306
1307
0
    case btVoid:   /* Void.  */
1308
0
      strcpy (p1, "void");
1309
0
      break;
1310
1311
0
    default:
1312
0
      sprintf (p1, _("unknown basic type %d"), (int) basic_type);
1313
0
      break;
1314
0
    }
1315
1316
0
  p1 += strlen (p1);
1317
1318
  /* If this is a bitfield, get the bitsize.  */
1319
0
  if (u.ti.fBitfield)
1320
0
    {
1321
0
      int bitsize;
1322
1323
0
      bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1324
0
      sprintf (p1, " : %d", bitsize);
1325
0
    }
1326
1327
  /* Deal with any qualifiers.  */
1328
0
  if (qualifiers[0].type != tqNil)
1329
0
    {
1330
      /* Snarf up any array bounds in the correct order.  Arrays
1331
   store 5 successive words in the aux. table:
1332
    word 0  RNDXR to type of the bounds (ie, int)
1333
    word 1  Current file descriptor index
1334
    word 2  low bound
1335
    word 3  high bound (or -1 if [])
1336
    word 4  stride size in bits.  */
1337
0
      for (i = 0; i < 7; i++)
1338
0
  {
1339
0
    if (qualifiers[i].type == tqArray)
1340
0
      {
1341
0
        qualifiers[i].low_bound =
1342
0
    AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1343
0
        qualifiers[i].high_bound =
1344
0
    AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1345
0
        qualifiers[i].stride =
1346
0
    AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1347
0
        indx += 5;
1348
0
      }
1349
0
  }
1350
1351
      /* Now print out the qualifiers.  */
1352
0
      for (i = 0; i < 6; i++)
1353
0
  {
1354
0
    switch (qualifiers[i].type)
1355
0
      {
1356
0
      case tqNil:
1357
0
      case tqMax:
1358
0
        break;
1359
1360
0
      case tqPtr:
1361
0
        strcpy (p2, "ptr to ");
1362
0
        p2 += sizeof ("ptr to ")-1;
1363
0
        break;
1364
1365
0
      case tqVol:
1366
0
        strcpy (p2, "volatile ");
1367
0
        p2 += sizeof ("volatile ")-1;
1368
0
        break;
1369
1370
0
      case tqFar:
1371
0
        strcpy (p2, "far ");
1372
0
        p2 += sizeof ("far ")-1;
1373
0
        break;
1374
1375
0
      case tqProc:
1376
0
        strcpy (p2, "func. ret. ");
1377
0
        p2 += sizeof ("func. ret. ");
1378
0
        break;
1379
1380
0
      case tqArray:
1381
0
        {
1382
0
    int first_array = i;
1383
0
    int j;
1384
1385
    /* Print array bounds reversed (ie, in the order the C
1386
       programmer writes them).  C is such a fun language....  */
1387
0
    while (i < 5 && qualifiers[i+1].type == tqArray)
1388
0
      i++;
1389
1390
0
    for (j = i; j >= first_array; j--)
1391
0
      {
1392
0
        strcpy (p2, "array [");
1393
0
        p2 += sizeof ("array [")-1;
1394
0
        if (qualifiers[j].low_bound != 0)
1395
0
          sprintf (p2,
1396
0
             "%ld:%ld {%ld bits}",
1397
0
             (long) qualifiers[j].low_bound,
1398
0
             (long) qualifiers[j].high_bound,
1399
0
             (long) qualifiers[j].stride);
1400
1401
0
        else if (qualifiers[j].high_bound != -1)
1402
0
          sprintf (p2,
1403
0
             "%ld {%ld bits}",
1404
0
             (long) (qualifiers[j].high_bound + 1),
1405
0
             (long) (qualifiers[j].stride));
1406
1407
0
        else
1408
0
          sprintf (p2, " {%ld bits}", (long) qualifiers[j].stride);
1409
1410
0
        p2 += strlen (p2);
1411
0
        strcpy (p2, "] of ");
1412
0
        p2 += sizeof ("] of ")-1;
1413
0
      }
1414
0
        }
1415
0
        break;
1416
0
      }
1417
0
  }
1418
0
    }
1419
1420
0
  strcpy (p2, buffer1);
1421
0
  return buff;
1422
0
}
1423
1424
/* Return information about ECOFF symbol SYMBOL in RET.  */
1425
1426
void
1427
_bfd_ecoff_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1428
          asymbol *symbol,
1429
          symbol_info *ret)
1430
0
{
1431
0
  bfd_symbol_info (symbol, ret);
1432
0
}
1433
1434
/* Return whether this is a local label.  */
1435
1436
bool
1437
_bfd_ecoff_bfd_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
1438
            const char *name)
1439
0
{
1440
0
  return name[0] == '$';
1441
0
}
1442
1443
/* Print information about an ECOFF symbol.  */
1444
1445
void
1446
_bfd_ecoff_print_symbol (bfd *abfd,
1447
       void * filep,
1448
       asymbol *symbol,
1449
       bfd_print_symbol_type how)
1450
0
{
1451
0
  const struct ecoff_debug_swap * const debug_swap
1452
0
    = &ecoff_backend (abfd)->debug_swap;
1453
0
  FILE *file = (FILE *)filep;
1454
1455
0
  switch (how)
1456
0
    {
1457
0
    case bfd_print_symbol_name:
1458
0
      fprintf (file, "%s", symbol->name);
1459
0
      break;
1460
0
    case bfd_print_symbol_more:
1461
0
      if (ecoffsymbol (symbol)->local)
1462
0
  {
1463
0
    SYMR ecoff_sym;
1464
1465
0
    (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1466
0
              &ecoff_sym);
1467
0
    fprintf (file, "ecoff local ");
1468
0
    bfd_fprintf_vma (abfd, file, ecoff_sym.value);
1469
0
    fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1470
0
       (unsigned) ecoff_sym.sc);
1471
0
  }
1472
0
      else
1473
0
  {
1474
0
    EXTR ecoff_ext;
1475
1476
0
    (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1477
0
              &ecoff_ext);
1478
0
    fprintf (file, "ecoff extern ");
1479
0
    bfd_fprintf_vma (abfd, file, ecoff_ext.asym.value);
1480
0
    fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1481
0
       (unsigned) ecoff_ext.asym.sc);
1482
0
  }
1483
0
      break;
1484
0
    case bfd_print_symbol_all:
1485
      /* Print out the symbols in a reasonable way.  */
1486
0
      {
1487
0
  char type;
1488
0
  int pos;
1489
0
  EXTR ecoff_ext;
1490
0
  char jmptbl;
1491
0
  char cobol_main;
1492
0
  char weakext;
1493
1494
0
  if (ecoffsymbol (symbol)->local)
1495
0
    {
1496
0
      (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1497
0
          &ecoff_ext.asym);
1498
0
      type = 'l';
1499
0
      pos = ((((char *) ecoffsymbol (symbol)->native
1500
0
         - (char *) ecoff_data (abfd)->debug_info.external_sym)
1501
0
        / debug_swap->external_sym_size)
1502
0
       + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1503
0
      jmptbl = ' ';
1504
0
      cobol_main = ' ';
1505
0
      weakext = ' ';
1506
0
    }
1507
0
  else
1508
0
    {
1509
0
      (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1510
0
          &ecoff_ext);
1511
0
      type = 'e';
1512
0
      pos = (((char *) ecoffsymbol (symbol)->native
1513
0
        - (char *) ecoff_data (abfd)->debug_info.external_ext)
1514
0
       / debug_swap->external_ext_size);
1515
0
      jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1516
0
      cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1517
0
      weakext = ecoff_ext.weakext ? 'w' : ' ';
1518
0
    }
1519
1520
0
  fprintf (file, "[%3d] %c ",
1521
0
     pos, type);
1522
0
  bfd_fprintf_vma (abfd, file, ecoff_ext.asym.value);
1523
0
  fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1524
0
     (unsigned) ecoff_ext.asym.st,
1525
0
     (unsigned) ecoff_ext.asym.sc,
1526
0
     (unsigned) ecoff_ext.asym.index,
1527
0
     jmptbl, cobol_main, weakext,
1528
0
     symbol->name);
1529
1530
0
  if (ecoffsymbol (symbol)->fdr != NULL
1531
0
      && ecoff_ext.asym.index != indexNil)
1532
0
    {
1533
0
      FDR *fdr;
1534
0
      unsigned int indx;
1535
0
      int bigendian;
1536
0
      bfd_size_type sym_base;
1537
0
      union aux_ext *aux_base;
1538
1539
0
      fdr = ecoffsymbol (symbol)->fdr;
1540
0
      indx = ecoff_ext.asym.index;
1541
1542
      /* sym_base is used to map the fdr relative indices which
1543
         appear in the file to the position number which we are
1544
         using.  */
1545
0
      sym_base = fdr->isymBase;
1546
0
      if (ecoffsymbol (symbol)->local)
1547
0
        sym_base +=
1548
0
    ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1549
1550
      /* aux_base is the start of the aux entries for this file;
1551
         asym.index is an offset from this.  */
1552
0
      aux_base = (ecoff_data (abfd)->debug_info.external_aux
1553
0
      + fdr->iauxBase);
1554
1555
      /* The aux entries are stored in host byte order; the
1556
         order is indicated by a bit in the fdr.  */
1557
0
      bigendian = fdr->fBigendian;
1558
1559
      /* This switch is basically from gcc/mips-tdump.c.  */
1560
0
      switch (ecoff_ext.asym.st)
1561
0
        {
1562
0
        case stNil:
1563
0
        case stLabel:
1564
0
    break;
1565
1566
0
        case stFile:
1567
0
        case stBlock:
1568
0
    fprintf (file, _("\n      End+1 symbol: %ld"),
1569
0
       (long) (indx + sym_base));
1570
0
    break;
1571
1572
0
        case stEnd:
1573
0
    if (ecoff_ext.asym.sc == scText
1574
0
        || ecoff_ext.asym.sc == scInfo)
1575
0
      fprintf (file, _("\n      First symbol: %ld"),
1576
0
         (long) (indx + sym_base));
1577
0
    else
1578
0
      fprintf (file, _("\n      First symbol: %ld"),
1579
0
         ((long)
1580
0
          (AUX_GET_ISYM (bigendian,
1581
0
             &aux_base[ecoff_ext.asym.index])
1582
0
           + sym_base)));
1583
0
    break;
1584
1585
0
        case stProc:
1586
0
        case stStaticProc:
1587
0
    if (ECOFF_IS_STAB (&ecoff_ext.asym))
1588
0
      ;
1589
0
    else if (ecoffsymbol (symbol)->local)
1590
0
      {
1591
0
        char buff[1024];
1592
        /* xgettext:c-format */
1593
0
        fprintf (file, _("\n      End+1 symbol: %-7ld   Type:  %s"),
1594
0
           ((long)
1595
0
            (AUX_GET_ISYM (bigendian,
1596
0
               &aux_base[ecoff_ext.asym.index])
1597
0
             + sym_base)),
1598
0
           ecoff_type_to_string (abfd, fdr, indx + 1, buff));
1599
0
      }
1600
0
    else
1601
0
      fprintf (file, _("\n      Local symbol: %ld"),
1602
0
         ((long) indx
1603
0
          + (long) sym_base
1604
0
          + (ecoff_data (abfd)
1605
0
             ->debug_info.symbolic_header.iextMax)));
1606
0
    break;
1607
1608
0
        case stStruct:
1609
0
    fprintf (file, _("\n      struct; End+1 symbol: %ld"),
1610
0
       (long) (indx + sym_base));
1611
0
    break;
1612
1613
0
        case stUnion:
1614
0
    fprintf (file, _("\n      union; End+1 symbol: %ld"),
1615
0
       (long) (indx + sym_base));
1616
0
    break;
1617
1618
0
        case stEnum:
1619
0
    fprintf (file, _("\n      enum; End+1 symbol: %ld"),
1620
0
       (long) (indx + sym_base));
1621
0
    break;
1622
1623
0
        default:
1624
0
    if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1625
0
      {
1626
0
        char buff[1024];
1627
0
        fprintf (file, _("\n      Type: %s"),
1628
0
           ecoff_type_to_string (abfd, fdr, indx, buff));
1629
0
      }
1630
0
    break;
1631
0
        }
1632
0
    }
1633
0
      }
1634
0
      break;
1635
0
    }
1636
0
}
1637

1638
/* Read in the relocs for a section.  */
1639
1640
static bool
1641
ecoff_slurp_reloc_table (bfd *abfd,
1642
       asection *section,
1643
       asymbol **symbols)
1644
0
{
1645
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1646
0
  arelent *internal_relocs;
1647
0
  bfd_size_type external_reloc_size;
1648
0
  bfd_size_type amt;
1649
0
  bfd_byte *external_relocs;
1650
0
  arelent *rptr;
1651
0
  unsigned int i;
1652
1653
0
  if (section->relocation != NULL
1654
0
      || section->reloc_count == 0
1655
0
      || (section->flags & SEC_CONSTRUCTOR) != 0)
1656
0
    return true;
1657
1658
0
  if (! _bfd_ecoff_slurp_symbol_table (abfd))
1659
0
    return false;
1660
1661
0
  external_reloc_size = backend->external_reloc_size;
1662
0
  amt = external_reloc_size * section->reloc_count;
1663
0
  if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1664
0
    return false;
1665
0
  external_relocs = _bfd_malloc_and_read (abfd, amt, amt);
1666
0
  if (external_relocs == NULL)
1667
0
    return false;
1668
1669
0
  amt = section->reloc_count;
1670
0
  amt *= sizeof (arelent);
1671
0
  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1672
0
  if (internal_relocs == NULL)
1673
0
    {
1674
0
      free (external_relocs);
1675
0
      return false;
1676
0
    }
1677
1678
0
  for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1679
0
    {
1680
0
      struct internal_reloc intern;
1681
1682
0
      (*backend->swap_reloc_in) (abfd,
1683
0
         external_relocs + i * external_reloc_size,
1684
0
         &intern);
1685
0
      rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1686
0
      rptr->addend = 0;
1687
1688
0
      if (intern.r_extern)
1689
0
  {
1690
    /* r_symndx is an index into the external symbols.  */
1691
0
    if (symbols != NULL
1692
0
        && intern.r_symndx >= 0
1693
0
        && (intern.r_symndx
1694
0
      < (ecoff_data (abfd)->debug_info.symbolic_header.iextMax)))
1695
0
      rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1696
0
  }
1697
0
      else
1698
0
  {
1699
0
    const char *sec_name;
1700
0
    asection *sec;
1701
1702
    /* r_symndx is a section key.  */
1703
0
    switch (intern.r_symndx)
1704
0
      {
1705
0
      case RELOC_SECTION_TEXT:  sec_name = _TEXT;  break;
1706
0
      case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
1707
0
      case RELOC_SECTION_DATA:  sec_name = _DATA;  break;
1708
0
      case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
1709
0
      case RELOC_SECTION_SBSS:  sec_name = _SBSS;  break;
1710
0
      case RELOC_SECTION_BSS:   sec_name = _BSS;   break;
1711
0
      case RELOC_SECTION_INIT:  sec_name = _INIT;  break;
1712
0
      case RELOC_SECTION_LIT8:  sec_name = _LIT8;  break;
1713
0
      case RELOC_SECTION_LIT4:  sec_name = _LIT4;  break;
1714
0
      case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
1715
0
      case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
1716
0
      case RELOC_SECTION_FINI:  sec_name = _FINI;  break;
1717
0
      case RELOC_SECTION_LITA:  sec_name = _LITA;  break;
1718
0
      case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
1719
0
      default:
1720
0
        sec_name = NULL;
1721
0
        break;
1722
0
      }
1723
1724
0
    if (sec_name != NULL)
1725
0
      {
1726
0
        sec = bfd_get_section_by_name (abfd, sec_name);
1727
0
        if (sec != NULL)
1728
0
    {
1729
0
      rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1730
0
      rptr->addend = - bfd_section_vma (sec);
1731
0
    }
1732
0
      }
1733
0
  }
1734
1735
0
      rptr->address = intern.r_vaddr - bfd_section_vma (section);
1736
1737
      /* Let the backend select the howto field and do any other
1738
   required processing.  */
1739
0
      (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1740
0
    }
1741
1742
0
  free (external_relocs);
1743
1744
0
  section->relocation = internal_relocs;
1745
1746
0
  return true;
1747
0
}
1748
1749
/* Get a canonical list of relocs.  */
1750
1751
long
1752
_bfd_ecoff_canonicalize_reloc (bfd *abfd,
1753
             asection *section,
1754
             arelent **relptr,
1755
             asymbol **symbols)
1756
0
{
1757
0
  unsigned int count;
1758
1759
0
  if (section->flags & SEC_CONSTRUCTOR)
1760
0
    {
1761
0
      arelent_chain *chain;
1762
1763
      /* This section has relocs made up by us, not the file, so take
1764
   them out of their chain and place them into the data area
1765
   provided.  */
1766
0
      for (count = 0, chain = section->constructor_chain;
1767
0
     count < section->reloc_count;
1768
0
     count++, chain = chain->next)
1769
0
  *relptr++ = &chain->relent;
1770
0
    }
1771
0
  else
1772
0
    {
1773
0
      arelent *tblptr;
1774
1775
0
      if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1776
0
  return -1;
1777
1778
0
      tblptr = section->relocation;
1779
1780
0
      for (count = 0; count < section->reloc_count; count++)
1781
0
  *relptr++ = tblptr++;
1782
0
    }
1783
1784
0
  *relptr = NULL;
1785
1786
0
  return section->reloc_count;
1787
0
}
1788

1789
/* Provided a BFD, a section and an offset into the section, calculate
1790
   and return the name of the source file and the line nearest to the
1791
   wanted location.  */
1792
1793
bool
1794
_bfd_ecoff_find_nearest_line (bfd *abfd,
1795
            asymbol **symbols ATTRIBUTE_UNUSED,
1796
            asection *section,
1797
            bfd_vma offset,
1798
            const char **filename_ptr,
1799
            const char **functionname_ptr,
1800
            unsigned int *retline_ptr,
1801
            unsigned int *discriminator_ptr)
1802
0
{
1803
0
  const struct ecoff_debug_swap * const debug_swap
1804
0
    = &ecoff_backend (abfd)->debug_swap;
1805
0
  struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1806
0
  struct ecoff_find_line *line_info;
1807
1808
  /* Make sure we have the FDR's.  */
1809
0
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
1810
0
      || bfd_get_symcount (abfd) == 0)
1811
0
    return false;
1812
1813
0
  if (ecoff_data (abfd)->find_line_info == NULL)
1814
0
    {
1815
0
      size_t amt = sizeof (struct ecoff_find_line);
1816
1817
0
      ecoff_data (abfd)->find_line_info =
1818
0
    (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1819
0
      if (ecoff_data (abfd)->find_line_info == NULL)
1820
0
  return false;
1821
0
    }
1822
1823
0
  if (discriminator_ptr)
1824
0
    *discriminator_ptr = 0;
1825
0
  line_info = ecoff_data (abfd)->find_line_info;
1826
0
  return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1827
0
         debug_swap, line_info, filename_ptr,
1828
0
         functionname_ptr, retline_ptr);
1829
0
}
1830

1831
/* Copy private BFD data.  This is called by objcopy and strip.  We
1832
   use it to copy the ECOFF debugging information from one BFD to the
1833
   other.  It would be theoretically possible to represent the ECOFF
1834
   debugging information in the symbol table.  However, it would be a
1835
   lot of work, and there would be little gain (gas, gdb, and ld
1836
   already access the ECOFF debugging information via the
1837
   ecoff_debug_info structure, and that structure would have to be
1838
   retained in order to support ECOFF debugging in MIPS ELF).
1839
1840
   The debugging information for the ECOFF external symbols comes from
1841
   the symbol table, so this function only handles the other debugging
1842
   information.  */
1843
1844
bool
1845
_bfd_ecoff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1846
0
{
1847
0
  struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1848
0
  struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1849
0
  int i;
1850
0
  asymbol **sym_ptr_ptr;
1851
0
  size_t c;
1852
0
  bool local;
1853
1854
  /* We only want to copy information over if both BFD's use ECOFF
1855
     format.  */
1856
0
  if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1857
0
      || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1858
0
    return true;
1859
1860
  /* Copy the GP value and the register masks.  */
1861
0
  ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1862
0
  ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1863
0
  ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1864
0
  for (i = 0; i < 3; i++)
1865
0
    ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1866
1867
  /* Copy the version stamp.  */
1868
0
  oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1869
1870
  /* If there are no symbols, don't copy any debugging information.  */
1871
0
  c = bfd_get_symcount (obfd);
1872
0
  sym_ptr_ptr = bfd_get_outsymbols (obfd);
1873
0
  if (c == 0 || sym_ptr_ptr == NULL)
1874
0
    return true;
1875
1876
  /* See if there are any local symbols.  */
1877
0
  local = false;
1878
0
  for (; c > 0; c--, sym_ptr_ptr++)
1879
0
    {
1880
0
      if (ecoffsymbol (*sym_ptr_ptr)->local)
1881
0
  {
1882
0
    local = true;
1883
0
    break;
1884
0
  }
1885
0
    }
1886
1887
0
  if (local)
1888
0
    {
1889
      /* There are some local symbols.  We just bring over all the
1890
   debugging information.  FIXME: This is not quite the right
1891
   thing to do.  If the user has asked us to discard all
1892
   debugging information, then we are probably going to wind up
1893
   keeping it because there will probably be some local symbol
1894
   which objcopy did not discard.  We should actually break
1895
   apart the debugging information and only keep that which
1896
   applies to the symbols we want to keep.  */
1897
0
      oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1898
0
      oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1899
0
      oinfo->line = iinfo->line;
1900
1901
0
      oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1902
0
      oinfo->external_dnr = iinfo->external_dnr;
1903
1904
0
      oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1905
0
      oinfo->external_pdr = iinfo->external_pdr;
1906
1907
0
      oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1908
0
      oinfo->external_sym = iinfo->external_sym;
1909
1910
0
      oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1911
0
      oinfo->external_opt = iinfo->external_opt;
1912
1913
0
      oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1914
0
      oinfo->external_aux = iinfo->external_aux;
1915
1916
0
      oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1917
0
      oinfo->ss = iinfo->ss;
1918
1919
0
      oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1920
0
      oinfo->external_fdr = iinfo->external_fdr;
1921
1922
0
      oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1923
0
      oinfo->external_rfd = iinfo->external_rfd;
1924
1925
      /* Flag that oinfo entries should not be freed.  */
1926
0
      oinfo->alloc_syments = true;
1927
0
    }
1928
0
  else
1929
0
    {
1930
      /* We are discarding all the local symbol information.  Look
1931
   through the external symbols and remove all references to FDR
1932
   or aux information.  */
1933
0
      c = bfd_get_symcount (obfd);
1934
0
      sym_ptr_ptr = bfd_get_outsymbols (obfd);
1935
0
      for (; c > 0; c--, sym_ptr_ptr++)
1936
0
  {
1937
0
    EXTR esym;
1938
1939
0
    (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1940
0
      (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1941
0
    esym.ifd = ifdNil;
1942
0
    esym.asym.index = indexNil;
1943
0
    (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1944
0
      (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1945
0
  }
1946
0
    }
1947
1948
0
  return true;
1949
0
}
1950

1951
/* Set the architecture.  The supported architecture is stored in the
1952
   backend pointer.  We always set the architecture anyhow, since many
1953
   callers ignore the return value.  */
1954
1955
bool
1956
_bfd_ecoff_set_arch_mach (bfd *abfd,
1957
        enum bfd_architecture arch,
1958
        unsigned long machine)
1959
0
{
1960
0
  bfd_default_set_arch_mach (abfd, arch, machine);
1961
0
  return arch == ecoff_backend (abfd)->arch;
1962
0
}
1963
1964
/* Get the size of the section headers.  */
1965
1966
int
1967
_bfd_ecoff_sizeof_headers (bfd *abfd,
1968
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
1969
0
{
1970
0
  asection *current;
1971
0
  int c;
1972
0
  int ret;
1973
1974
0
  c = 0;
1975
0
  for (current = abfd->sections;
1976
0
       current != NULL;
1977
0
       current = current->next)
1978
0
    ++c;
1979
1980
0
  ret = (bfd_coff_filhsz (abfd)
1981
0
   + bfd_coff_aoutsz (abfd)
1982
0
   + c * bfd_coff_scnhsz (abfd));
1983
0
  return (int) BFD_ALIGN (ret, 16);
1984
0
}
1985
1986
/* Get the contents of a section.  */
1987
1988
bool
1989
_bfd_ecoff_get_section_contents (bfd *abfd,
1990
         asection *section,
1991
         void * location,
1992
         file_ptr offset,
1993
         bfd_size_type count)
1994
0
{
1995
0
  return _bfd_generic_get_section_contents (abfd, section, location,
1996
0
              offset, count);
1997
0
}
1998
1999
/* Sort sections by VMA, but put SEC_ALLOC sections first.  This is
2000
   called via qsort.  */
2001
2002
static int
2003
ecoff_sort_hdrs (const void * arg1, const void * arg2)
2004
0
{
2005
0
  const asection *hdr1 = *(const asection **) arg1;
2006
0
  const asection *hdr2 = *(const asection **) arg2;
2007
2008
0
  if ((hdr1->flags & SEC_ALLOC) != 0)
2009
0
    {
2010
0
      if ((hdr2->flags & SEC_ALLOC) == 0)
2011
0
  return -1;
2012
0
    }
2013
0
  else
2014
0
    {
2015
0
      if ((hdr2->flags & SEC_ALLOC) != 0)
2016
0
  return 1;
2017
0
    }
2018
0
  if (hdr1->vma < hdr2->vma)
2019
0
    return -1;
2020
0
  else if (hdr1->vma > hdr2->vma)
2021
0
    return 1;
2022
0
  else
2023
0
    return 0;
2024
0
}
2025
2026
/* Calculate the file position for each section, and set
2027
   reloc_filepos.  */
2028
2029
static bool
2030
ecoff_compute_section_file_positions (bfd *abfd)
2031
0
{
2032
0
  file_ptr sofar, file_sofar;
2033
0
  asection **sorted_hdrs;
2034
0
  asection *current;
2035
0
  unsigned int i;
2036
0
  file_ptr old_sofar;
2037
0
  bool rdata_in_text;
2038
0
  bool first_data, first_nonalloc;
2039
0
  const bfd_vma round = ecoff_backend (abfd)->round;
2040
0
  bfd_size_type amt;
2041
2042
0
  sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
2043
0
  file_sofar = sofar;
2044
2045
  /* Sort the sections by VMA.  */
2046
0
  amt = abfd->section_count;
2047
0
  amt *= sizeof (asection *);
2048
0
  sorted_hdrs = (asection **) bfd_malloc (amt);
2049
0
  if (sorted_hdrs == NULL)
2050
0
    return false;
2051
0
  for (current = abfd->sections, i = 0;
2052
0
       current != NULL;
2053
0
       current = current->next, i++)
2054
0
    sorted_hdrs[i] = current;
2055
0
  BFD_ASSERT (i == abfd->section_count);
2056
2057
0
  qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2058
0
   ecoff_sort_hdrs);
2059
2060
  /* Some versions of the OSF linker put the .rdata section in the
2061
     text segment, and some do not.  */
2062
0
  rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2063
0
  if (rdata_in_text)
2064
0
    {
2065
0
      for (i = 0; i < abfd->section_count; i++)
2066
0
  {
2067
0
    current = sorted_hdrs[i];
2068
0
    if (streq (current->name, _RDATA))
2069
0
      break;
2070
0
    if ((current->flags & SEC_CODE) == 0
2071
0
        && ! streq (current->name, _PDATA)
2072
0
        && ! streq (current->name, _RCONST))
2073
0
      {
2074
0
        rdata_in_text = false;
2075
0
        break;
2076
0
      }
2077
0
  }
2078
0
    }
2079
0
  ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2080
2081
0
  first_data = true;
2082
0
  first_nonalloc = true;
2083
0
  for (i = 0; i < abfd->section_count; i++)
2084
0
    {
2085
0
      unsigned int alignment_power;
2086
2087
0
      current = sorted_hdrs[i];
2088
2089
      /* For the Alpha ECOFF .pdata section the lnnoptr field is
2090
   supposed to indicate the number of .pdata entries that are
2091
   really in the section.  Each entry is 8 bytes.  We store this
2092
   away in line_filepos before increasing the section size.  */
2093
0
      if (streq (current->name, _PDATA))
2094
0
  current->line_filepos = current->size / 8;
2095
2096
0
      alignment_power = current->alignment_power;
2097
2098
      /* On Ultrix, the data sections in an executable file must be
2099
   aligned to a page boundary within the file.  This does not
2100
   affect the section size, though.  FIXME: Does this work for
2101
   other platforms?  It requires some modification for the
2102
   Alpha, because .rdata on the Alpha goes with the text, not
2103
   the data.  */
2104
0
      if ((abfd->flags & EXEC_P) != 0
2105
0
    && (abfd->flags & D_PAGED) != 0
2106
0
    && ! first_data
2107
0
    && (current->flags & SEC_CODE) == 0
2108
0
    && (! rdata_in_text
2109
0
        || ! streq (current->name, _RDATA))
2110
0
    && ! streq (current->name, _PDATA)
2111
0
    && ! streq (current->name, _RCONST))
2112
0
  {
2113
0
    sofar = (sofar + round - 1) &~ (round - 1);
2114
0
    file_sofar = (file_sofar + round - 1) &~ (round - 1);
2115
0
    first_data = false;
2116
0
  }
2117
0
      else if (streq (current->name, _LIB))
2118
0
  {
2119
    /* On Irix 4, the location of contents of the .lib section
2120
       from a shared library section is also rounded up to a
2121
       page boundary.  */
2122
2123
0
    sofar = (sofar + round - 1) &~ (round - 1);
2124
0
    file_sofar = (file_sofar + round - 1) &~ (round - 1);
2125
0
  }
2126
0
      else if (first_nonalloc
2127
0
         && (current->flags & SEC_ALLOC) == 0
2128
0
         && (abfd->flags & D_PAGED) != 0)
2129
0
  {
2130
    /* Skip up to the next page for an unallocated section, such
2131
       as the .comment section on the Alpha.  This leaves room
2132
       for the .bss section.  */
2133
0
    first_nonalloc = false;
2134
0
    sofar = (sofar + round - 1) &~ (round - 1);
2135
0
    file_sofar = (file_sofar + round - 1) &~ (round - 1);
2136
0
  }
2137
2138
      /* Align the sections in the file to the same boundary on
2139
   which they are aligned in virtual memory.  */
2140
0
      sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2141
0
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2142
0
  file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2143
2144
0
      if ((abfd->flags & D_PAGED) != 0
2145
0
    && (current->flags & SEC_ALLOC) != 0)
2146
0
  {
2147
0
    sofar += (current->vma - sofar) % round;
2148
0
    if ((current->flags & SEC_HAS_CONTENTS) != 0)
2149
0
      file_sofar += (current->vma - file_sofar) % round;
2150
0
  }
2151
2152
0
      if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2153
0
  current->filepos = file_sofar;
2154
2155
0
      sofar += current->size;
2156
0
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2157
0
  file_sofar += current->size;
2158
2159
      /* Make sure that this section is of the right size too.  */
2160
0
      old_sofar = sofar;
2161
0
      sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2162
0
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2163
0
  file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2164
0
      current->size += sofar - old_sofar;
2165
0
    }
2166
2167
0
  free (sorted_hdrs);
2168
0
  sorted_hdrs = NULL;
2169
2170
0
  ecoff_data (abfd)->reloc_filepos = file_sofar;
2171
2172
0
  return true;
2173
0
}
2174
2175
/* Determine the location of the relocs for all the sections in the
2176
   output file, as well as the location of the symbolic debugging
2177
   information.  */
2178
2179
static bfd_size_type
2180
ecoff_compute_reloc_file_positions (bfd *abfd)
2181
0
{
2182
0
  const bfd_size_type external_reloc_size =
2183
0
    ecoff_backend (abfd)->external_reloc_size;
2184
0
  file_ptr reloc_base;
2185
0
  bfd_size_type reloc_size;
2186
0
  asection *current;
2187
0
  file_ptr sym_base;
2188
2189
0
  if (! abfd->output_has_begun)
2190
0
    {
2191
0
      if (! ecoff_compute_section_file_positions (abfd))
2192
0
  abort ();
2193
0
      abfd->output_has_begun = true;
2194
0
    }
2195
2196
0
  reloc_base = ecoff_data (abfd)->reloc_filepos;
2197
2198
0
  reloc_size = 0;
2199
0
  for (current = abfd->sections;
2200
0
       current != NULL;
2201
0
       current = current->next)
2202
0
    {
2203
0
      if (current->reloc_count == 0)
2204
0
  current->rel_filepos = 0;
2205
0
      else
2206
0
  {
2207
0
    bfd_size_type relsize;
2208
2209
0
    current->rel_filepos = reloc_base;
2210
0
    relsize = current->reloc_count * external_reloc_size;
2211
0
    reloc_size += relsize;
2212
0
    reloc_base += relsize;
2213
0
  }
2214
0
    }
2215
2216
0
  sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2217
2218
  /* At least on Ultrix, the symbol table of an executable file must
2219
     be aligned to a page boundary.  FIXME: Is this true on other
2220
     platforms?  */
2221
0
  if ((abfd->flags & EXEC_P) != 0
2222
0
      && (abfd->flags & D_PAGED) != 0)
2223
0
    sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2224
0
    &~ (ecoff_backend (abfd)->round - 1));
2225
2226
0
  ecoff_data (abfd)->sym_filepos = sym_base;
2227
2228
0
  return reloc_size;
2229
0
}
2230
2231
/* Set the contents of a section.  */
2232
2233
bool
2234
_bfd_ecoff_set_section_contents (bfd *abfd,
2235
         asection *section,
2236
         const void * location,
2237
         file_ptr offset,
2238
         bfd_size_type count)
2239
0
{
2240
0
  file_ptr pos;
2241
2242
  /* This must be done first, because bfd_set_section_contents is
2243
     going to set output_has_begun to TRUE.  */
2244
0
  if (! abfd->output_has_begun
2245
0
      && ! ecoff_compute_section_file_positions (abfd))
2246
0
    return false;
2247
2248
  /* Handle the .lib section specially so that Irix 4 shared libraries
2249
     work out.  See coff_set_section_contents in coffcode.h.  */
2250
0
  if (streq (section->name, _LIB))
2251
0
    {
2252
0
      bfd_byte *rec, *recend;
2253
2254
0
      rec = (bfd_byte *) location;
2255
0
      recend = rec + count;
2256
0
      while (rec < recend)
2257
0
  {
2258
0
    ++section->lma;
2259
0
    rec += bfd_get_32 (abfd, rec) * 4;
2260
0
  }
2261
2262
0
      BFD_ASSERT (rec == recend);
2263
0
    }
2264
2265
0
  if (count == 0)
2266
0
    return true;
2267
2268
0
  pos = section->filepos + offset;
2269
0
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
2270
0
      || bfd_write (location, count, abfd) != count)
2271
0
    return false;
2272
2273
0
  return true;
2274
0
}
2275
2276
/* Set the GP value for an ECOFF file.  This is a hook used by the
2277
   assembler.  */
2278
2279
bool
2280
bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value)
2281
0
{
2282
0
  if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2283
0
      || bfd_get_format (abfd) != bfd_object)
2284
0
    {
2285
0
      bfd_set_error (bfd_error_invalid_operation);
2286
0
      return false;
2287
0
    }
2288
2289
0
  ecoff_data (abfd)->gp = gp_value;
2290
2291
0
  return true;
2292
0
}
2293
2294
/* Set the register masks for an ECOFF file.  This is a hook used by
2295
   the assembler.  */
2296
2297
bool
2298
bfd_ecoff_set_regmasks (bfd *abfd,
2299
      unsigned long gprmask,
2300
      unsigned long fprmask,
2301
      unsigned long *cprmask)
2302
0
{
2303
0
  ecoff_data_type *tdata;
2304
2305
0
  if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2306
0
      || bfd_get_format (abfd) != bfd_object)
2307
0
    {
2308
0
      bfd_set_error (bfd_error_invalid_operation);
2309
0
      return false;
2310
0
    }
2311
2312
0
  tdata = ecoff_data (abfd);
2313
0
  tdata->gprmask = gprmask;
2314
0
  tdata->fprmask = fprmask;
2315
0
  if (cprmask != NULL)
2316
0
    {
2317
0
      int i;
2318
2319
0
      for (i = 0; i < 3; i++)
2320
0
  tdata->cprmask[i] = cprmask[i];
2321
0
    }
2322
2323
0
  return true;
2324
0
}
2325
2326
/* Get ECOFF EXTR information for an external symbol.  This function
2327
   is passed to bfd_ecoff_debug_externals.  */
2328
2329
static bool
2330
ecoff_get_extr (asymbol *sym, EXTR *esym)
2331
0
{
2332
0
  ecoff_symbol_type *ecoff_sym_ptr;
2333
0
  bfd *input_bfd;
2334
2335
0
  if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2336
0
      || ecoffsymbol (sym)->native == NULL)
2337
0
    {
2338
      /* Don't include debugging, local, or section symbols.  */
2339
0
      if ((sym->flags & BSF_DEBUGGING) != 0
2340
0
    || (sym->flags & BSF_LOCAL) != 0
2341
0
    || (sym->flags & BSF_SECTION_SYM) != 0)
2342
0
  return false;
2343
2344
0
      esym->jmptbl = 0;
2345
0
      esym->cobol_main = 0;
2346
0
      esym->weakext = (sym->flags & BSF_WEAK) != 0;
2347
0
      esym->reserved = 0;
2348
0
      esym->ifd = ifdNil;
2349
      /* FIXME: we can do better than this for st and sc.  */
2350
0
      esym->asym.st = stGlobal;
2351
0
      esym->asym.sc = scAbs;
2352
0
      esym->asym.reserved = 0;
2353
0
      esym->asym.index = indexNil;
2354
0
      return true;
2355
0
    }
2356
2357
0
  ecoff_sym_ptr = ecoffsymbol (sym);
2358
2359
0
  if (ecoff_sym_ptr->local)
2360
0
    return false;
2361
2362
0
  input_bfd = bfd_asymbol_bfd (sym);
2363
0
  (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2364
0
    (input_bfd, ecoff_sym_ptr->native, esym);
2365
2366
  /* If the symbol was defined by the linker, then esym will be
2367
     undefined but sym will not be.  Get a better class for such a
2368
     symbol.  */
2369
0
  if ((esym->asym.sc == scUndefined
2370
0
       || esym->asym.sc == scSUndefined)
2371
0
      && ! bfd_is_und_section (bfd_asymbol_section (sym)))
2372
0
    esym->asym.sc = scAbs;
2373
2374
  /* Adjust the FDR index for the symbol by that used for the input
2375
     BFD.  */
2376
0
  if (esym->ifd != -1)
2377
0
    {
2378
0
      struct ecoff_debug_info *input_debug;
2379
2380
0
      input_debug = &ecoff_data (input_bfd)->debug_info;
2381
0
      BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2382
0
      if (input_debug->ifdmap != NULL)
2383
0
  esym->ifd = input_debug->ifdmap[esym->ifd];
2384
0
    }
2385
2386
0
  return true;
2387
0
}
2388
2389
/* Set the external symbol index.  This routine is passed to
2390
   bfd_ecoff_debug_externals.  */
2391
2392
static void
2393
ecoff_set_index (asymbol *sym, bfd_size_type indx)
2394
0
{
2395
0
  ecoff_set_sym_index (sym, indx);
2396
0
}
2397
2398
/* Write out an ECOFF file.  */
2399
2400
bool
2401
_bfd_ecoff_write_object_contents (bfd *abfd)
2402
0
{
2403
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2404
0
  const bfd_vma round = backend->round;
2405
0
  const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2406
0
  const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2407
0
  const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2408
0
  const bfd_size_type external_hdr_size
2409
0
    = backend->debug_swap.external_hdr_size;
2410
0
  const bfd_size_type external_reloc_size = backend->external_reloc_size;
2411
0
  void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
2412
0
    = backend->adjust_reloc_out;
2413
0
  void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
2414
0
    = backend->swap_reloc_out;
2415
0
  struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2416
0
  HDRR * const symhdr = &debug->symbolic_header;
2417
0
  asection *current;
2418
0
  unsigned int count;
2419
0
  bfd_size_type reloc_size;
2420
0
  bfd_size_type text_size;
2421
0
  bfd_vma text_start;
2422
0
  bool set_text_start;
2423
0
  bfd_size_type data_size;
2424
0
  bfd_vma data_start;
2425
0
  bool set_data_start;
2426
0
  bfd_size_type bss_size;
2427
0
  void * buff = NULL;
2428
0
  void * reloc_buff = NULL;
2429
0
  struct internal_filehdr internal_f;
2430
0
  struct internal_aouthdr internal_a;
2431
0
  int i;
2432
2433
  /* Determine where the sections and relocs will go in the output
2434
     file.  */
2435
0
  reloc_size = ecoff_compute_reloc_file_positions (abfd);
2436
2437
0
  count = 1;
2438
0
  for (current = abfd->sections;
2439
0
       current != NULL;
2440
0
       current = current->next)
2441
0
    {
2442
0
      current->target_index = count;
2443
0
      ++count;
2444
0
    }
2445
2446
0
  if ((abfd->flags & D_PAGED) != 0)
2447
0
    text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
2448
0
  else
2449
0
    text_size = 0;
2450
0
  text_start = 0;
2451
0
  set_text_start = false;
2452
0
  data_size = 0;
2453
0
  data_start = 0;
2454
0
  set_data_start = false;
2455
0
  bss_size = 0;
2456
2457
  /* Write section headers to the file.  */
2458
2459
  /* Allocate buff big enough to hold a section header,
2460
     file header, or a.out header.  */
2461
0
  {
2462
0
    bfd_size_type siz;
2463
2464
0
    siz = scnhsz;
2465
0
    if (siz < filhsz)
2466
0
      siz = filhsz;
2467
0
    if (siz < aoutsz)
2468
0
      siz = aoutsz;
2469
0
    buff = bfd_malloc (siz);
2470
0
    if (buff == NULL)
2471
0
      goto error_return;
2472
0
  }
2473
2474
0
  internal_f.f_nscns = 0;
2475
0
  if (bfd_seek (abfd, filhsz + aoutsz, SEEK_SET) != 0)
2476
0
    goto error_return;
2477
2478
0
  for (current = abfd->sections;
2479
0
       current != NULL;
2480
0
       current = current->next)
2481
0
    {
2482
0
      struct internal_scnhdr section;
2483
0
      bfd_vma vma;
2484
2485
0
      ++internal_f.f_nscns;
2486
2487
0
      strncpy (section.s_name, current->name, sizeof section.s_name);
2488
2489
      /* This seems to be correct for Irix 4 shared libraries.  */
2490
0
      vma = bfd_section_vma (current);
2491
0
      if (streq (current->name, _LIB))
2492
0
  section.s_vaddr = 0;
2493
0
      else
2494
0
  section.s_vaddr = vma;
2495
2496
0
      section.s_paddr = current->lma;
2497
0
      section.s_size = current->size;
2498
2499
      /* If this section is unloadable then the scnptr will be 0.  */
2500
0
      if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2501
0
  section.s_scnptr = 0;
2502
0
      else
2503
0
  section.s_scnptr = current->filepos;
2504
0
      section.s_relptr = current->rel_filepos;
2505
2506
      /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2507
   object file produced by the assembler is supposed to point to
2508
   information about how much room is required by objects of
2509
   various different sizes.  I think this only matters if we
2510
   want the linker to compute the best size to use, or
2511
   something.  I don't know what happens if the information is
2512
   not present.  */
2513
0
      if (! streq (current->name, _PDATA))
2514
0
  section.s_lnnoptr = 0;
2515
0
      else
2516
0
  {
2517
    /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2518
       hold the number of entries in the section (each entry is
2519
       8 bytes).  We stored this in the line_filepos field in
2520
       ecoff_compute_section_file_positions.  */
2521
0
    section.s_lnnoptr = current->line_filepos;
2522
0
  }
2523
2524
0
      section.s_nreloc = current->reloc_count;
2525
0
      section.s_nlnno = 0;
2526
0
      section.s_flags = ecoff_sec_to_styp_flags (current->name,
2527
0
             current->flags);
2528
2529
0
      if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff) == 0
2530
0
    || bfd_write (buff, scnhsz, abfd) != scnhsz)
2531
0
  goto error_return;
2532
2533
0
      if ((section.s_flags & STYP_TEXT) != 0
2534
0
    || ((section.s_flags & STYP_RDATA) != 0
2535
0
        && ecoff_data (abfd)->rdata_in_text)
2536
0
    || section.s_flags == STYP_PDATA
2537
0
    || (section.s_flags & STYP_DYNAMIC) != 0
2538
0
    || (section.s_flags & STYP_LIBLIST) != 0
2539
0
    || (section.s_flags & STYP_RELDYN) != 0
2540
0
    || section.s_flags == STYP_CONFLIC
2541
0
    || (section.s_flags & STYP_DYNSTR) != 0
2542
0
    || (section.s_flags & STYP_DYNSYM) != 0
2543
0
    || (section.s_flags & STYP_HASH) != 0
2544
0
    || (section.s_flags & STYP_ECOFF_INIT) != 0
2545
0
    || (section.s_flags & STYP_ECOFF_FINI) != 0
2546
0
    || section.s_flags == STYP_RCONST)
2547
0
  {
2548
0
    text_size += current->size;
2549
0
    if (! set_text_start || text_start > vma)
2550
0
      {
2551
0
        text_start = vma;
2552
0
        set_text_start = true;
2553
0
      }
2554
0
  }
2555
0
      else if ((section.s_flags & STYP_RDATA) != 0
2556
0
         || (section.s_flags & STYP_DATA) != 0
2557
0
         || (section.s_flags & STYP_LITA) != 0
2558
0
         || (section.s_flags & STYP_LIT8) != 0
2559
0
         || (section.s_flags & STYP_LIT4) != 0
2560
0
         || (section.s_flags & STYP_SDATA) != 0
2561
0
         || section.s_flags == STYP_XDATA
2562
0
         || (section.s_flags & STYP_GOT) != 0)
2563
0
  {
2564
0
    data_size += current->size;
2565
0
    if (! set_data_start || data_start > vma)
2566
0
      {
2567
0
        data_start = vma;
2568
0
        set_data_start = true;
2569
0
      }
2570
0
  }
2571
0
      else if ((section.s_flags & STYP_BSS) != 0
2572
0
         || (section.s_flags & STYP_SBSS) != 0)
2573
0
  bss_size += current->size;
2574
0
      else if (section.s_flags == 0
2575
0
         || (section.s_flags & STYP_ECOFF_LIB) != 0
2576
0
         || section.s_flags == STYP_COMMENT)
2577
0
  /* Do nothing.  */ ;
2578
0
      else
2579
0
  abort ();
2580
0
    }
2581
2582
  /* Set up the file header.  */
2583
0
  internal_f.f_magic = ecoff_get_magic (abfd);
2584
2585
  /* We will NOT put a fucking timestamp in the header here. Every
2586
     time you put it back, I will come in and take it out again.  I'm
2587
     sorry.  This field does not belong here.  We fill it with a 0 so
2588
     it compares the same but is not a reasonable time. --
2589
     gnu@cygnus.com.  */
2590
0
  internal_f.f_timdat = 0;
2591
2592
0
  if (bfd_get_symcount (abfd) != 0)
2593
0
    {
2594
      /* The ECOFF f_nsyms field is not actually the number of
2595
   symbols, it's the size of symbolic information header.  */
2596
0
      internal_f.f_nsyms = external_hdr_size;
2597
0
      internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2598
0
    }
2599
0
  else
2600
0
    {
2601
0
      internal_f.f_nsyms = 0;
2602
0
      internal_f.f_symptr = 0;
2603
0
    }
2604
2605
0
  internal_f.f_opthdr = aoutsz;
2606
2607
0
  internal_f.f_flags = F_LNNO;
2608
0
  if (reloc_size == 0)
2609
0
    internal_f.f_flags |= F_RELFLG;
2610
0
  if (bfd_get_symcount (abfd) == 0)
2611
0
    internal_f.f_flags |= F_LSYMS;
2612
0
  if (abfd->flags & EXEC_P)
2613
0
    internal_f.f_flags |= F_EXEC;
2614
2615
0
  if (bfd_little_endian (abfd))
2616
0
    internal_f.f_flags |= F_AR32WR;
2617
0
  else
2618
0
    internal_f.f_flags |= F_AR32W;
2619
2620
  /* Set up the ``optional'' header.  */
2621
0
  if ((abfd->flags & D_PAGED) != 0)
2622
0
    internal_a.magic = ECOFF_AOUT_ZMAGIC;
2623
0
  else
2624
0
    internal_a.magic = ECOFF_AOUT_OMAGIC;
2625
2626
  /* FIXME: Is this really correct?  */
2627
0
  internal_a.vstamp = symhdr->vstamp;
2628
2629
  /* At least on Ultrix, these have to be rounded to page boundaries.
2630
     FIXME: Is this true on other platforms?  */
2631
0
  if ((abfd->flags & D_PAGED) != 0)
2632
0
    {
2633
0
      internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2634
0
      internal_a.text_start = text_start &~ (round - 1);
2635
0
      internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2636
0
      internal_a.data_start = data_start &~ (round - 1);
2637
0
    }
2638
0
  else
2639
0
    {
2640
0
      internal_a.tsize = text_size;
2641
0
      internal_a.text_start = text_start;
2642
0
      internal_a.dsize = data_size;
2643
0
      internal_a.data_start = data_start;
2644
0
    }
2645
2646
  /* On Ultrix, the initial portions of the .sbss and .bss segments
2647
     are at the end of the data section.  The bsize field in the
2648
     optional header records how many bss bytes are required beyond
2649
     those in the data section.  The value is not rounded to a page
2650
     boundary.  */
2651
0
  if (bss_size < internal_a.dsize - data_size)
2652
0
    bss_size = 0;
2653
0
  else
2654
0
    bss_size -= internal_a.dsize - data_size;
2655
0
  internal_a.bsize = bss_size;
2656
0
  internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2657
2658
0
  internal_a.entry = bfd_get_start_address (abfd);
2659
2660
0
  internal_a.gp_value = ecoff_data (abfd)->gp;
2661
2662
0
  internal_a.gprmask = ecoff_data (abfd)->gprmask;
2663
0
  internal_a.fprmask = ecoff_data (abfd)->fprmask;
2664
0
  for (i = 0; i < 4; i++)
2665
0
    internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2666
2667
  /* Let the backend adjust the headers if necessary.  */
2668
0
  if (backend->adjust_headers)
2669
0
    {
2670
0
      if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2671
0
  goto error_return;
2672
0
    }
2673
2674
  /* Write out the file header and the optional header.  */
2675
0
  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
2676
0
    goto error_return;
2677
2678
0
  bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
2679
0
  if (bfd_write (buff, filhsz, abfd) != filhsz)
2680
0
    goto error_return;
2681
2682
0
  bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
2683
0
  if (bfd_write (buff, aoutsz, abfd) != aoutsz)
2684
0
    goto error_return;
2685
2686
  /* Build the external symbol information.  This must be done before
2687
     writing out the relocs so that we know the symbol indices.  We
2688
     don't do this if this BFD was created by the backend linker,
2689
     since it will have already handled the symbols and relocs.  */
2690
0
  if (! ecoff_data (abfd)->linker)
2691
0
    {
2692
0
      symhdr->iextMax = 0;
2693
0
      symhdr->issExtMax = 0;
2694
0
      debug->external_ext = debug->external_ext_end = NULL;
2695
0
      debug->ssext = debug->ssext_end = NULL;
2696
0
      if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2697
0
               (abfd->flags & EXEC_P) == 0,
2698
0
               ecoff_get_extr, ecoff_set_index))
2699
0
  goto error_return;
2700
2701
      /* Write out the relocs.  */
2702
0
      for (current = abfd->sections;
2703
0
     current != NULL;
2704
0
     current = current->next)
2705
0
  {
2706
0
    arelent **reloc_ptr_ptr;
2707
0
    arelent **reloc_end;
2708
0
    char *out_ptr;
2709
0
    bfd_size_type amt;
2710
2711
0
    if (current->reloc_count == 0)
2712
0
      continue;
2713
2714
0
    amt = current->reloc_count * external_reloc_size;
2715
0
    reloc_buff = bfd_zalloc (abfd, amt);
2716
0
    if (reloc_buff == NULL)
2717
0
      goto error_return;
2718
2719
0
    reloc_ptr_ptr = current->orelocation;
2720
0
    reloc_end = reloc_ptr_ptr + current->reloc_count;
2721
0
    out_ptr = (char *) reloc_buff;
2722
2723
0
    for (;
2724
0
         reloc_ptr_ptr < reloc_end;
2725
0
         reloc_ptr_ptr++, out_ptr += external_reloc_size)
2726
0
      {
2727
0
        arelent *reloc;
2728
0
        asymbol *sym;
2729
0
        struct internal_reloc in;
2730
2731
0
        memset ((void *) &in, 0, sizeof in);
2732
2733
0
        reloc = *reloc_ptr_ptr;
2734
0
        sym = *reloc->sym_ptr_ptr;
2735
2736
        /* If the howto field has not been initialised then skip this reloc.
2737
     This assumes that an error message has been issued elsewhere.  */
2738
0
        if (reloc->howto == NULL)
2739
0
    continue;
2740
2741
0
        in.r_vaddr = reloc->address + bfd_section_vma (current);
2742
0
        in.r_type = reloc->howto->type;
2743
2744
0
        if ((sym->flags & BSF_SECTION_SYM) == 0)
2745
0
    {
2746
0
      in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2747
0
      in.r_extern = 1;
2748
0
    }
2749
0
        else
2750
0
    {
2751
0
      const char *name;
2752
0
      unsigned int j;
2753
0
      static struct
2754
0
      {
2755
0
        const char * name;
2756
0
        long r_symndx;
2757
0
      }
2758
0
      section_symndx [] =
2759
0
      {
2760
0
        { _TEXT,   RELOC_SECTION_TEXT   },
2761
0
        { _RDATA,  RELOC_SECTION_RDATA  },
2762
0
        { _DATA,   RELOC_SECTION_DATA   },
2763
0
        { _SDATA,  RELOC_SECTION_SDATA  },
2764
0
        { _SBSS,   RELOC_SECTION_SBSS   },
2765
0
        { _BSS,    RELOC_SECTION_BSS    },
2766
0
        { _INIT,   RELOC_SECTION_INIT   },
2767
0
        { _LIT8,   RELOC_SECTION_LIT8   },
2768
0
        { _LIT4,   RELOC_SECTION_LIT4   },
2769
0
        { _XDATA,  RELOC_SECTION_XDATA  },
2770
0
        { _PDATA,  RELOC_SECTION_PDATA  },
2771
0
        { _FINI,   RELOC_SECTION_FINI   },
2772
0
        { _LITA,   RELOC_SECTION_LITA   },
2773
0
        { "*ABS*", RELOC_SECTION_ABS    },
2774
0
        { _RCONST, RELOC_SECTION_RCONST }
2775
0
      };
2776
2777
0
      name = bfd_section_name (bfd_asymbol_section (sym));
2778
2779
0
      for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
2780
0
        if (streq (name, section_symndx[j].name))
2781
0
          {
2782
0
      in.r_symndx = section_symndx[j].r_symndx;
2783
0
      break;
2784
0
          }
2785
2786
0
      if (j == ARRAY_SIZE (section_symndx))
2787
0
        abort ();
2788
0
      in.r_extern = 0;
2789
0
    }
2790
2791
0
        (*adjust_reloc_out) (abfd, reloc, &in);
2792
2793
0
        (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
2794
0
      }
2795
2796
0
    if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2797
0
      goto error_return;
2798
0
    amt = current->reloc_count * external_reloc_size;
2799
0
    if (bfd_write (reloc_buff, amt, abfd) != amt)
2800
0
      goto error_return;
2801
0
    bfd_release (abfd, reloc_buff);
2802
0
    reloc_buff = NULL;
2803
0
  }
2804
2805
      /* Write out the symbolic debugging information.  */
2806
0
      if (bfd_get_symcount (abfd) > 0)
2807
0
  {
2808
    /* Write out the debugging information.  */
2809
0
    if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2810
0
               ecoff_data (abfd)->sym_filepos))
2811
0
      goto error_return;
2812
0
  }
2813
0
    }
2814
2815
  /* The .bss section of a demand paged executable must receive an
2816
     entire page.  If there are symbols, the symbols will start on the
2817
     next page.  If there are no symbols, we must fill out the page by
2818
     hand.  */
2819
0
  if (bfd_get_symcount (abfd) == 0
2820
0
      && (abfd->flags & EXEC_P) != 0
2821
0
      && (abfd->flags & D_PAGED) != 0)
2822
0
    {
2823
0
      char c;
2824
2825
0
      if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2826
0
  goto error_return;
2827
0
      if (bfd_read (&c, 1, abfd) == 0)
2828
0
  c = 0;
2829
0
      if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2830
0
  goto error_return;
2831
0
      if (bfd_write (&c, 1, abfd) != 1)
2832
0
  goto error_return;
2833
0
    }
2834
2835
0
  if (reloc_buff != NULL)
2836
0
    bfd_release (abfd, reloc_buff);
2837
0
  free (buff);
2838
0
  return true;
2839
0
 error_return:
2840
0
  if (reloc_buff != NULL)
2841
0
    bfd_release (abfd, reloc_buff);
2842
0
  free (buff);
2843
0
  return false;
2844
0
}
2845

2846
/* Archive handling.  ECOFF uses what appears to be a unique type of
2847
   archive header (armap).  The byte ordering of the armap and the
2848
   contents are encoded in the name of the armap itself.  At least for
2849
   now, we only support archives with the same byte ordering in the
2850
   armap and the contents.
2851
2852
   The first four bytes in the armap are the number of symbol
2853
   definitions.  This is always a power of two.
2854
2855
   This is followed by the symbol definitions.  Each symbol definition
2856
   occupies 8 bytes.  The first four bytes are the offset from the
2857
   start of the armap strings to the null-terminated string naming
2858
   this symbol.  The second four bytes are the file offset to the
2859
   archive member which defines this symbol.  If the second four bytes
2860
   are 0, then this is not actually a symbol definition, and it should
2861
   be ignored.
2862
2863
   The symbols are hashed into the armap with a closed hashing scheme.
2864
   See the functions below for the details of the algorithm.
2865
2866
   After the symbol definitions comes four bytes holding the size of
2867
   the string table, followed by the string table itself.  */
2868
2869
/* The name of an archive headers looks like this:
2870
   __________E[BL]E[BL]_ (with a trailing space).
2871
   The trailing space is changed to an X if the archive is changed to
2872
   indicate that the armap is out of date.
2873
2874
   The Alpha seems to use ________64E[BL]E[BL]_.  */
2875
2876
0
#define ARMAP_BIG_ENDIAN    'B'
2877
0
#define ARMAP_LITTLE_ENDIAN   'L'
2878
4.68k
#define ARMAP_MARKER      'E'
2879
2.33k
#define ARMAP_START_LENGTH    10
2880
8
#define ARMAP_HEADER_MARKER_INDEX 10
2881
0
#define ARMAP_HEADER_ENDIAN_INDEX 11
2882
0
#define ARMAP_OBJECT_MARKER_INDEX 12
2883
0
#define ARMAP_OBJECT_ENDIAN_INDEX 13
2884
0
#define ARMAP_END_INDEX     14
2885
0
#define ARMAP_END     "_ "
2886
2887
/* This is a magic number used in the hashing algorithm.  */
2888
0
#define ARMAP_HASH_MAGIC    0x9dd68ab5
2889
2890
/* This returns the hash value to use for a string.  It also sets
2891
   *REHASH to the rehash adjustment if the first slot is taken.  SIZE
2892
   is the number of entries in the hash table, and HLOG is the log
2893
   base 2 of SIZE.  */
2894
2895
static unsigned int
2896
ecoff_armap_hash (const char *s,
2897
      unsigned int *rehash,
2898
      unsigned int size,
2899
      unsigned int hlog)
2900
0
{
2901
0
  unsigned int hash;
2902
2903
0
  if (hlog == 0)
2904
0
    return 0;
2905
0
  hash = *s++;
2906
0
  while (*s != '\0')
2907
0
    hash = ((hash >> 27) | (hash << 5)) + *s++;
2908
0
  hash *= ARMAP_HASH_MAGIC;
2909
0
  *rehash = (hash & (size - 1)) | 1;
2910
0
  return hash >> (32 - hlog);
2911
0
}
2912
2913
/* Read in the armap.  */
2914
2915
bool
2916
_bfd_ecoff_slurp_armap (bfd *abfd)
2917
4.16k
{
2918
4.16k
  char nextname[17];
2919
4.16k
  unsigned int i;
2920
4.16k
  struct areltdata *mapdata;
2921
4.16k
  bfd_size_type parsed_size, stringsize;
2922
4.16k
  char *raw_armap;
2923
4.16k
  struct artdata *ardata;
2924
4.16k
  unsigned int count;
2925
4.16k
  char *raw_ptr;
2926
4.16k
  carsym *symdef_ptr;
2927
4.16k
  char *stringbase;
2928
4.16k
  bfd_size_type amt;
2929
2930
  /* Get the name of the first element.  */
2931
4.16k
  i = bfd_read (nextname, 16, abfd);
2932
4.16k
  if (i == 0)
2933
0
    return true;
2934
4.16k
  if (i != 16)
2935
0
    return false;
2936
2937
4.16k
  if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
2938
0
    return false;
2939
2940
  /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2941
     standard COFF armap.  We could move the ECOFF armap stuff into
2942
     bfd_slurp_armap, but that seems inappropriate since no other
2943
     target uses this format.  Instead, we check directly for a COFF
2944
     armap.  */
2945
4.16k
  if (startswith (nextname, "/               "))
2946
1.82k
    return bfd_slurp_armap (abfd);
2947
2948
  /* See if the first element is an armap.  */
2949
2.33k
  if (strncmp (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH) != 0
2950
2.33k
      || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2951
2.33k
      || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2952
0
    && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2953
2.33k
      || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2954
2.33k
      || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2955
0
    && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2956
2.33k
      || strncmp (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1) != 0)
2957
2.33k
    {
2958
2.33k
      abfd->has_armap = false;
2959
2.33k
      return true;
2960
2.33k
    }
2961
2962
  /* Make sure we have the right byte ordering.  */
2963
0
  if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2964
0
       ^ (bfd_header_big_endian (abfd)))
2965
0
      || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2966
0
    ^ (bfd_big_endian (abfd))))
2967
0
    {
2968
0
      bfd_set_error (bfd_error_wrong_format);
2969
0
      return false;
2970
0
    }
2971
2972
  /* Read in the armap.  */
2973
0
  ardata = bfd_ardata (abfd);
2974
0
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2975
0
  if (mapdata == NULL)
2976
0
    return false;
2977
0
  parsed_size = mapdata->parsed_size;
2978
0
  free (mapdata);
2979
2980
0
  if (parsed_size + 1 < 9)
2981
0
    {
2982
0
      bfd_set_error (bfd_error_malformed_archive);
2983
0
      return false;
2984
0
    }
2985
2986
0
  raw_armap = (char *) _bfd_alloc_and_read (abfd, parsed_size + 1, parsed_size);
2987
0
  if (raw_armap == NULL)
2988
0
    return false;
2989
0
  raw_armap[parsed_size] = 0;
2990
2991
0
  ardata->tdata = (void *) raw_armap;
2992
2993
0
  count = H_GET_32 (abfd, raw_armap);
2994
0
  if ((parsed_size - 8) / 8 < count)
2995
0
    goto error_malformed;
2996
2997
0
  ardata->symdef_count = 0;
2998
0
  ardata->cache = NULL;
2999
3000
  /* This code used to overlay the symdefs over the raw archive data,
3001
     but that doesn't work on a 64 bit host.  */
3002
0
  stringbase = raw_armap + count * 8 + 8;
3003
0
  stringsize = parsed_size - (count * 8 + 8);
3004
3005
#ifdef CHECK_ARMAP_HASH
3006
  {
3007
    unsigned int hlog;
3008
3009
    /* Double check that I have the hashing algorithm right by making
3010
       sure that every symbol can be looked up successfully.  */
3011
    hlog = 0;
3012
    for (i = 1; i < count; i <<= 1)
3013
      hlog++;
3014
    BFD_ASSERT (i == count);
3015
3016
    raw_ptr = raw_armap + 4;
3017
    for (i = 0; i < count; i++, raw_ptr += 8)
3018
      {
3019
  unsigned int name_offset, file_offset;
3020
  unsigned int hash, rehash, srch;
3021
3022
  name_offset = H_GET_32 (abfd, raw_ptr);
3023
  file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3024
  if (file_offset == 0)
3025
    continue;
3026
  hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3027
         hlog);
3028
  if (hash == i)
3029
    continue;
3030
3031
  /* See if we can rehash to this location.  */
3032
  for (srch = (hash + rehash) & (count - 1);
3033
       srch != hash && srch != i;
3034
       srch = (srch + rehash) & (count - 1))
3035
    BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
3036
  BFD_ASSERT (srch == i);
3037
      }
3038
  }
3039
3040
#endif /* CHECK_ARMAP_HASH */
3041
3042
0
  raw_ptr = raw_armap + 4;
3043
0
  for (i = 0; i < count; i++, raw_ptr += 8)
3044
0
    if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
3045
0
      ++ardata->symdef_count;
3046
3047
0
  amt = ardata->symdef_count;
3048
0
  amt *= sizeof (carsym);
3049
0
  symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
3050
0
  if (!symdef_ptr)
3051
0
    goto error_exit;
3052
3053
0
  ardata->symdefs = symdef_ptr;
3054
3055
0
  raw_ptr = raw_armap + 4;
3056
0
  for (i = 0; i < count; i++, raw_ptr += 8)
3057
0
    {
3058
0
      unsigned int name_offset, file_offset;
3059
3060
0
      file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3061
0
      if (file_offset == 0)
3062
0
  continue;
3063
0
      name_offset = H_GET_32 (abfd, raw_ptr);
3064
0
      if (name_offset > stringsize)
3065
0
  goto error_malformed;
3066
0
      symdef_ptr->name = stringbase + name_offset;
3067
0
      symdef_ptr->file_offset = file_offset;
3068
0
      ++symdef_ptr;
3069
0
    }
3070
3071
0
  ardata->first_file_filepos = bfd_tell (abfd);
3072
  /* Pad to an even boundary.  */
3073
0
  ardata->first_file_filepos += ardata->first_file_filepos % 2;
3074
0
  abfd->has_armap = true;
3075
0
  return true;
3076
3077
0
 error_malformed:
3078
0
  bfd_set_error (bfd_error_malformed_archive);
3079
0
 error_exit:
3080
0
  ardata->symdef_count = 0;
3081
0
  ardata->symdefs = NULL;
3082
0
  ardata->tdata = NULL;
3083
0
  bfd_release (abfd, raw_armap);
3084
0
  return false;
3085
0
}
3086
3087
/* Write out an armap.  */
3088
3089
bool
3090
_bfd_ecoff_write_armap (bfd *abfd,
3091
      unsigned int elength,
3092
      struct orl *map,
3093
      unsigned int orl_count,
3094
      int stridx)
3095
0
{
3096
0
  unsigned int hashsize, hashlog;
3097
0
  bfd_size_type symdefsize;
3098
0
  int padit;
3099
0
  unsigned int stringsize;
3100
0
  unsigned int mapsize;
3101
0
  file_ptr firstreal;
3102
0
  struct ar_hdr hdr;
3103
0
  struct stat statbuf;
3104
0
  unsigned int i;
3105
0
  bfd_byte temp[4];
3106
0
  bfd_byte *hashtable;
3107
0
  bfd *current;
3108
0
  bfd *last_elt;
3109
3110
  /* Ultrix appears to use as a hash table size the least power of two
3111
     greater than twice the number of entries.  */
3112
0
  for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3113
0
    ;
3114
0
  hashsize = 1 << hashlog;
3115
3116
0
  symdefsize = hashsize * 8;
3117
0
  padit = stridx % 2;
3118
0
  stringsize = stridx + padit;
3119
3120
  /* Include 8 bytes to store symdefsize and stringsize in output.  */
3121
0
  mapsize = symdefsize + stringsize + 8;
3122
3123
0
  firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3124
3125
0
  memset ((void *) &hdr, 0, sizeof hdr);
3126
3127
  /* Work out the ECOFF armap name.  */
3128
0
  strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3129
0
  hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3130
0
  hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3131
0
    (bfd_header_big_endian (abfd)
3132
0
     ? ARMAP_BIG_ENDIAN
3133
0
     : ARMAP_LITTLE_ENDIAN);
3134
0
  hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3135
0
  hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3136
0
    bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3137
0
  memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3138
3139
  /* Write the timestamp of the archive header to be just a little bit
3140
     later than the timestamp of the file, otherwise the linker will
3141
     complain that the index is out of date.  Actually, the Ultrix
3142
     linker just checks the archive name; the GNU linker may check the
3143
     date.  */
3144
0
  stat (bfd_get_filename (abfd), &statbuf);
3145
0
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
3146
0
        (long) (statbuf.st_mtime + 60));
3147
3148
  /* The DECstation uses zeroes for the uid, gid and mode of the
3149
     armap.  */
3150
0
  hdr.ar_uid[0] = '0';
3151
0
  hdr.ar_gid[0] = '0';
3152
  /* Building gcc ends up extracting the armap as a file - twice.  */
3153
0
  hdr.ar_mode[0] = '6';
3154
0
  hdr.ar_mode[1] = '4';
3155
0
  hdr.ar_mode[2] = '4';
3156
3157
0
  _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
3158
3159
0
  hdr.ar_fmag[0] = '`';
3160
0
  hdr.ar_fmag[1] = '\012';
3161
3162
  /* Turn all null bytes in the header into spaces.  */
3163
0
  for (i = 0; i < sizeof (struct ar_hdr); i++)
3164
0
   if (((char *) (&hdr))[i] == '\0')
3165
0
     (((char *) (&hdr))[i]) = ' ';
3166
3167
0
  if (bfd_write (&hdr, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
3168
0
    return false;
3169
3170
0
  H_PUT_32 (abfd, hashsize, temp);
3171
0
  if (bfd_write (temp, 4, abfd) != 4)
3172
0
    return false;
3173
3174
0
  hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3175
0
  if (!hashtable)
3176
0
    return false;
3177
3178
0
  current = abfd->archive_head;
3179
0
  last_elt = current;
3180
0
  for (i = 0; i < orl_count; i++)
3181
0
    {
3182
0
      unsigned int hash, rehash = 0;
3183
3184
      /* Advance firstreal to the file position of this archive
3185
   element.  */
3186
0
      if (map[i].u.abfd != last_elt)
3187
0
  {
3188
0
    do
3189
0
      {
3190
0
        firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3191
0
        firstreal += firstreal % 2;
3192
0
        current = current->archive_next;
3193
0
      }
3194
0
    while (current != map[i].u.abfd);
3195
0
  }
3196
3197
0
      last_elt = current;
3198
3199
0
      hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3200
0
      if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
3201
0
  {
3202
0
    unsigned int srch;
3203
3204
    /* The desired slot is already taken.  */
3205
0
    for (srch = (hash + rehash) & (hashsize - 1);
3206
0
         srch != hash;
3207
0
         srch = (srch + rehash) & (hashsize - 1))
3208
0
      if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
3209
0
        break;
3210
3211
0
    BFD_ASSERT (srch != hash);
3212
3213
0
    hash = srch;
3214
0
  }
3215
3216
0
      H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
3217
0
      H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
3218
0
    }
3219
3220
0
  if (bfd_write (hashtable, symdefsize, abfd) != symdefsize)
3221
0
    return false;
3222
3223
0
  bfd_release (abfd, hashtable);
3224
3225
  /* Now write the strings.  */
3226
0
  H_PUT_32 (abfd, stringsize, temp);
3227
0
  if (bfd_write (temp, 4, abfd) != 4)
3228
0
    return false;
3229
0
  for (i = 0; i < orl_count; i++)
3230
0
    {
3231
0
      bfd_size_type len;
3232
3233
0
      len = strlen (*map[i].name) + 1;
3234
0
      if (bfd_write (*map[i].name, len, abfd) != len)
3235
0
  return false;
3236
0
    }
3237
3238
  /* The spec sez this should be a newline.  But in order to be
3239
     bug-compatible for DECstation ar we use a null.  */
3240
0
  if (padit)
3241
0
    {
3242
0
      if (bfd_write ("", 1, abfd) != 1)
3243
0
  return false;
3244
0
    }
3245
3246
0
  return true;
3247
0
}
3248

3249
/* ECOFF linker code.  */
3250
3251
/* Routine to create an entry in an ECOFF link hash table.  */
3252
3253
static struct bfd_hash_entry *
3254
ecoff_link_hash_newfunc (struct bfd_hash_entry *entry,
3255
       struct bfd_hash_table *table,
3256
       const char *string)
3257
0
{
3258
0
  struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3259
3260
  /* Allocate the structure if it has not already been allocated by a
3261
     subclass.  */
3262
0
  if (ret == NULL)
3263
0
    ret = ((struct ecoff_link_hash_entry *)
3264
0
     bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3265
0
  if (ret == NULL)
3266
0
    return NULL;
3267
3268
  /* Call the allocation method of the superclass.  */
3269
0
  ret = ((struct ecoff_link_hash_entry *)
3270
0
   _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3271
0
         table, string));
3272
3273
0
  if (ret)
3274
0
    {
3275
      /* Set local fields.  */
3276
0
      ret->indx = -1;
3277
0
      ret->abfd = NULL;
3278
0
      ret->written = 0;
3279
0
      ret->small = 0;
3280
0
    }
3281
0
  memset ((void *) &ret->esym, 0, sizeof ret->esym);
3282
3283
0
  return (struct bfd_hash_entry *) ret;
3284
0
}
3285
3286
/* Create an ECOFF link hash table.  */
3287
3288
struct bfd_link_hash_table *
3289
_bfd_ecoff_bfd_link_hash_table_create (bfd *abfd)
3290
0
{
3291
0
  struct ecoff_link_hash_table *ret;
3292
0
  size_t amt = sizeof (struct ecoff_link_hash_table);
3293
3294
0
  ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3295
0
  if (ret == NULL)
3296
0
    return NULL;
3297
0
  if (!_bfd_link_hash_table_init (&ret->root, abfd,
3298
0
          ecoff_link_hash_newfunc,
3299
0
          sizeof (struct ecoff_link_hash_entry)))
3300
0
    {
3301
0
      free (ret);
3302
0
      return NULL;
3303
0
    }
3304
0
  return &ret->root;
3305
0
}
3306
3307
/* Look up an entry in an ECOFF link hash table.  */
3308
3309
#define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3310
  ((struct ecoff_link_hash_entry *) \
3311
   bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3312
3313
/* Get the ECOFF link hash table from the info structure.  This is
3314
   just a cast.  */
3315
3316
#define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3317
3318
/* Add the external symbols of an object file to the global linker
3319
   hash table.  The external symbols and strings we are passed are
3320
   just allocated on the stack, and will be discarded.  We must
3321
   explicitly save any information we may need later on in the link.
3322
   We do not want to read the external symbol information again.  */
3323
3324
static bool
3325
ecoff_link_add_externals (bfd *abfd,
3326
        struct bfd_link_info *info,
3327
        void * external_ext,
3328
        char *ssext)
3329
0
{
3330
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3331
0
  void (* const swap_ext_in) (bfd *, void *, EXTR *)
3332
0
    = backend->debug_swap.swap_ext_in;
3333
0
  bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3334
0
  unsigned long ext_count;
3335
0
  struct bfd_link_hash_entry **sym_hash;
3336
0
  char *ext_ptr;
3337
0
  char *ext_end;
3338
0
  bfd_size_type amt;
3339
3340
0
  ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3341
3342
0
  amt = ext_count;
3343
0
  amt *= sizeof (struct bfd_link_hash_entry *);
3344
0
  sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
3345
0
  if (!sym_hash)
3346
0
    return false;
3347
0
  ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
3348
3349
0
  ext_ptr = (char *) external_ext;
3350
0
  ext_end = ext_ptr + ext_count * external_ext_size;
3351
0
  for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3352
0
    {
3353
0
      EXTR esym;
3354
0
      bool skip;
3355
0
      bfd_vma value;
3356
0
      asection *section;
3357
0
      const char *name;
3358
0
      struct ecoff_link_hash_entry *h;
3359
3360
0
      *sym_hash = NULL;
3361
3362
0
      (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
3363
3364
      /* Skip debugging symbols.  */
3365
0
      skip = false;
3366
0
      switch (esym.asym.st)
3367
0
  {
3368
0
  case stGlobal:
3369
0
  case stStatic:
3370
0
  case stLabel:
3371
0
  case stProc:
3372
0
  case stStaticProc:
3373
0
    break;
3374
0
  default:
3375
0
    skip = true;
3376
0
    break;
3377
0
  }
3378
3379
0
      if (skip)
3380
0
  continue;
3381
3382
      /* Get the information for this symbol.  */
3383
0
      value = esym.asym.value;
3384
0
      switch (esym.asym.sc)
3385
0
  {
3386
0
  default:
3387
0
  case scNil:
3388
0
  case scRegister:
3389
0
  case scCdbLocal:
3390
0
  case scBits:
3391
0
  case scCdbSystem:
3392
0
  case scRegImage:
3393
0
  case scInfo:
3394
0
  case scUserStruct:
3395
0
  case scVar:
3396
0
  case scVarRegister:
3397
0
  case scVariant:
3398
0
  case scBasedVar:
3399
0
  case scXData:
3400
0
  case scPData:
3401
0
    section = NULL;
3402
0
    break;
3403
0
  case scText:
3404
0
    section = bfd_make_section_old_way (abfd, _TEXT);
3405
0
    value -= section->vma;
3406
0
    break;
3407
0
  case scData:
3408
0
    section = bfd_make_section_old_way (abfd, _DATA);
3409
0
    value -= section->vma;
3410
0
    break;
3411
0
  case scBss:
3412
0
    section = bfd_make_section_old_way (abfd, _BSS);
3413
0
    value -= section->vma;
3414
0
    break;
3415
0
  case scAbs:
3416
0
    section = bfd_abs_section_ptr;
3417
0
    break;
3418
0
  case scUndefined:
3419
0
    section = bfd_und_section_ptr;
3420
0
    break;
3421
0
  case scSData:
3422
0
    section = bfd_make_section_old_way (abfd, _SDATA);
3423
0
    value -= section->vma;
3424
0
    break;
3425
0
  case scSBss:
3426
0
    section = bfd_make_section_old_way (abfd, _SBSS);
3427
0
    value -= section->vma;
3428
0
    break;
3429
0
  case scRData:
3430
0
    section = bfd_make_section_old_way (abfd, _RDATA);
3431
0
    value -= section->vma;
3432
0
    break;
3433
0
  case scCommon:
3434
0
    if (value > ecoff_data (abfd)->gp_size)
3435
0
      {
3436
0
        section = bfd_com_section_ptr;
3437
0
        break;
3438
0
      }
3439
    /* Fall through.  */
3440
0
  case scSCommon:
3441
0
    section = &ecoff_scom_section;
3442
0
    break;
3443
0
  case scSUndefined:
3444
0
    section = bfd_und_section_ptr;
3445
0
    break;
3446
0
  case scInit:
3447
0
    section = bfd_make_section_old_way (abfd, _INIT);
3448
0
    value -= section->vma;
3449
0
    break;
3450
0
  case scFini:
3451
0
    section = bfd_make_section_old_way (abfd, _FINI);
3452
0
    value -= section->vma;
3453
0
    break;
3454
0
  case scRConst:
3455
0
    section = bfd_make_section_old_way (abfd, _RCONST);
3456
0
    value -= section->vma;
3457
0
    break;
3458
0
  }
3459
3460
0
      if (section == NULL)
3461
0
  continue;
3462
3463
0
      name = ssext + esym.asym.iss;
3464
3465
0
      if (! (_bfd_generic_link_add_one_symbol
3466
0
       (info, abfd, name,
3467
0
        (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3468
0
        section, value, NULL, true, true, sym_hash)))
3469
0
  return false;
3470
3471
0
      h = (struct ecoff_link_hash_entry *) *sym_hash;
3472
3473
      /* If we are building an ECOFF hash table, save the external
3474
   symbol information.  */
3475
0
      if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
3476
0
  {
3477
0
    if (h->abfd == NULL
3478
0
        || (! bfd_is_und_section (section)
3479
0
      && (! bfd_is_com_section (section)
3480
0
          || (h->root.type != bfd_link_hash_defined
3481
0
        && h->root.type != bfd_link_hash_defweak))))
3482
0
      {
3483
0
        h->abfd = abfd;
3484
0
        h->esym = esym;
3485
0
      }
3486
3487
    /* Remember whether this symbol was small undefined.  */
3488
0
    if (esym.asym.sc == scSUndefined)
3489
0
      h->small = 1;
3490
3491
    /* If this symbol was ever small undefined, it needs to wind
3492
       up in a GP relative section.  We can't control the
3493
       section of a defined symbol, but we can control the
3494
       section of a common symbol.  This case is actually needed
3495
       on Ultrix 4.2 to handle the symbol cred in -lckrb.  */
3496
0
    if (h->small
3497
0
        && h->root.type == bfd_link_hash_common
3498
0
        && streq (h->root.u.c.p->section->name, SCOMMON))
3499
0
      {
3500
0
        h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3501
0
                 SCOMMON);
3502
0
        h->root.u.c.p->section->flags = SEC_ALLOC;
3503
0
        if (h->esym.asym.sc == scCommon)
3504
0
    h->esym.asym.sc = scSCommon;
3505
0
      }
3506
0
  }
3507
0
    }
3508
3509
0
  return true;
3510
0
}
3511
3512
/* Add symbols from an ECOFF object file to the global linker hash
3513
   table.  */
3514
3515
static bool
3516
ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3517
0
{
3518
0
  HDRR *symhdr;
3519
0
  bfd_size_type external_ext_size;
3520
0
  void * external_ext = NULL;
3521
0
  bfd_size_type esize;
3522
0
  char *ssext = NULL;
3523
0
  bool result;
3524
3525
0
  if (! ecoff_slurp_symbolic_header (abfd))
3526
0
    return false;
3527
3528
  /* If there are no symbols, we don't want it.  */
3529
0
  if (bfd_get_symcount (abfd) == 0)
3530
0
    return true;
3531
3532
0
  symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3533
3534
  /* Read in the external symbols and external strings.  */
3535
0
  if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0)
3536
0
    return false;
3537
0
  external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3538
0
  esize = symhdr->iextMax * external_ext_size;
3539
0
  external_ext = _bfd_malloc_and_read (abfd, esize, esize);
3540
0
  if (external_ext == NULL && esize != 0)
3541
0
    goto error_return;
3542
3543
0
  if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0)
3544
0
    goto error_return;
3545
0
  ssext = (char *) _bfd_malloc_and_read (abfd, symhdr->issExtMax,
3546
0
           symhdr->issExtMax);
3547
0
  if (ssext == NULL && symhdr->issExtMax != 0)
3548
0
    goto error_return;
3549
3550
0
  result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3551
3552
0
  free (ssext);
3553
0
  free (external_ext);
3554
0
  return result;
3555
3556
0
 error_return:
3557
0
  free (ssext);
3558
0
  free (external_ext);
3559
0
  return false;
3560
0
}
3561
3562
/* This is called if we used _bfd_generic_link_add_archive_symbols
3563
   because we were not dealing with an ECOFF archive.  */
3564
3565
static bool
3566
ecoff_link_check_archive_element (bfd *abfd,
3567
          struct bfd_link_info *info,
3568
          struct bfd_link_hash_entry *h,
3569
          const char *name,
3570
          bool *pneeded)
3571
0
{
3572
0
  *pneeded = false;
3573
3574
  /* Unlike the generic linker, we do not pull in elements because
3575
     of common symbols.  */
3576
0
  if (h->type != bfd_link_hash_undefined)
3577
0
    return true;
3578
3579
  /* Include this element?  */
3580
0
  if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
3581
0
    return true;
3582
0
  *pneeded = true;
3583
3584
0
  return ecoff_link_add_object_symbols (abfd, info);
3585
0
}
3586
3587
/* Add the symbols from an archive file to the global hash table.
3588
   This looks through the undefined symbols, looks each one up in the
3589
   archive hash table, and adds any associated object file.  We do not
3590
   use _bfd_generic_link_add_archive_symbols because ECOFF archives
3591
   already have a hash table, so there is no reason to construct
3592
   another one.  */
3593
3594
static bool
3595
ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
3596
0
{
3597
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3598
0
  const bfd_byte *raw_armap;
3599
0
  struct bfd_link_hash_entry **pundef;
3600
0
  unsigned int armap_count;
3601
0
  unsigned int armap_log;
3602
0
  unsigned int i;
3603
0
  const bfd_byte *hashtable;
3604
0
  const char *stringbase;
3605
3606
0
  if (! bfd_has_map (abfd))
3607
0
    {
3608
      /* An empty archive is a special case.  */
3609
0
      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
3610
0
  return true;
3611
0
      bfd_set_error (bfd_error_no_armap);
3612
0
      return false;
3613
0
    }
3614
3615
  /* If we don't have any raw data for this archive, as can happen on
3616
     Irix 4.0.5F, we call the generic routine.
3617
     FIXME: We should be more clever about this, since someday tdata
3618
     may get to something for a generic archive.  */
3619
0
  raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3620
0
  if (raw_armap == NULL)
3621
0
    return (_bfd_generic_link_add_archive_symbols
3622
0
      (abfd, info, ecoff_link_check_archive_element));
3623
3624
0
  armap_count = H_GET_32 (abfd, raw_armap);
3625
3626
0
  armap_log = 0;
3627
0
  for (i = 1; i < armap_count; i <<= 1)
3628
0
    armap_log++;
3629
0
  BFD_ASSERT (i == armap_count);
3630
3631
0
  hashtable = raw_armap + 4;
3632
0
  stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3633
3634
  /* Look through the list of undefined symbols.  */
3635
0
  pundef = &info->hash->undefs;
3636
0
  while (*pundef != NULL)
3637
0
    {
3638
0
      struct bfd_link_hash_entry *h;
3639
0
      unsigned int hash, rehash = 0;
3640
0
      unsigned int file_offset;
3641
0
      const char *name;
3642
0
      bfd *element;
3643
3644
0
      h = *pundef;
3645
3646
      /* When a symbol is defined, it is not necessarily removed from
3647
   the list.  */
3648
0
      if (h->type != bfd_link_hash_undefined
3649
0
    && h->type != bfd_link_hash_common)
3650
0
  {
3651
    /* Remove this entry from the list, for general cleanliness
3652
       and because we are going to look through the list again
3653
       if we search any more libraries.  We can't remove the
3654
       entry if it is the tail, because that would lose any
3655
       entries we add to the list later on.  */
3656
0
    if (*pundef != info->hash->undefs_tail)
3657
0
      *pundef = (*pundef)->u.undef.next;
3658
0
    else
3659
0
      pundef = &(*pundef)->u.undef.next;
3660
0
    continue;
3661
0
  }
3662
3663
      /* Native ECOFF linkers do not pull in archive elements merely
3664
   to satisfy common definitions, so neither do we.  We leave
3665
   them on the list, though, in case we are linking against some
3666
   other object format.  */
3667
0
      if (h->type != bfd_link_hash_undefined)
3668
0
  {
3669
0
    pundef = &(*pundef)->u.undef.next;
3670
0
    continue;
3671
0
  }
3672
3673
      /* Look for this symbol in the archive hash table.  */
3674
0
      hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3675
0
             armap_log);
3676
3677
0
      file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3678
0
      if (file_offset == 0)
3679
0
  {
3680
    /* Nothing in this slot.  */
3681
0
    pundef = &(*pundef)->u.undef.next;
3682
0
    continue;
3683
0
  }
3684
3685
0
      name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3686
0
      if (name[0] != h->root.string[0]
3687
0
    || ! streq (name, h->root.string))
3688
0
  {
3689
0
    unsigned int srch;
3690
0
    bool found;
3691
3692
    /* That was the wrong symbol.  Try rehashing.  */
3693
0
    found = false;
3694
0
    for (srch = (hash + rehash) & (armap_count - 1);
3695
0
         srch != hash;
3696
0
         srch = (srch + rehash) & (armap_count - 1))
3697
0
      {
3698
0
        file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3699
0
        if (file_offset == 0)
3700
0
    break;
3701
0
        name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3702
0
        if (name[0] == h->root.string[0]
3703
0
      && streq (name, h->root.string))
3704
0
    {
3705
0
      found = true;
3706
0
      break;
3707
0
    }
3708
0
      }
3709
3710
0
    if (! found)
3711
0
      {
3712
0
        pundef = &(*pundef)->u.undef.next;
3713
0
        continue;
3714
0
      }
3715
3716
0
    hash = srch;
3717
0
  }
3718
3719
0
      element = (*backend->get_elt_at_filepos) (abfd,
3720
0
            (file_ptr) file_offset,
3721
0
            info);
3722
0
      if (element == NULL)
3723
0
  return false;
3724
3725
0
      if (! bfd_check_format (element, bfd_object))
3726
0
  return false;
3727
3728
      /* Unlike the generic linker, we know that this element provides
3729
   a definition for an undefined symbol and we know that we want
3730
   to include it.  We don't need to check anything.  */
3731
0
      if (!(*info->callbacks
3732
0
      ->add_archive_element) (info, element, name, &element))
3733
0
  return false;
3734
0
      if (! ecoff_link_add_object_symbols (element, info))
3735
0
  return false;
3736
3737
0
      pundef = &(*pundef)->u.undef.next;
3738
0
    }
3739
3740
0
  return true;
3741
0
}
3742
3743
/* Given an ECOFF BFD, add symbols to the global hash table as
3744
   appropriate.  */
3745
3746
bool
3747
_bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
3748
0
{
3749
0
  switch (bfd_get_format (abfd))
3750
0
    {
3751
0
    case bfd_object:
3752
0
      return ecoff_link_add_object_symbols (abfd, info);
3753
0
    case bfd_archive:
3754
0
      return ecoff_link_add_archive_symbols (abfd, info);
3755
0
    default:
3756
0
      bfd_set_error (bfd_error_wrong_format);
3757
0
      return false;
3758
0
    }
3759
0
}
3760
3761

3762
/* ECOFF final link routines.  */
3763
3764
/* Structure used to pass information to ecoff_link_write_external.  */
3765
3766
struct extsym_info
3767
{
3768
  bfd *abfd;
3769
  struct bfd_link_info *info;
3770
};
3771
3772
/* Accumulate the debugging information for an input BFD into the
3773
   output BFD.  This must read in the symbolic information of the
3774
   input BFD.  */
3775
3776
static bool
3777
ecoff_final_link_debug_accumulate (bfd *output_bfd,
3778
           bfd *input_bfd,
3779
           struct bfd_link_info *info,
3780
           void * handle)
3781
0
{
3782
0
  struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
3783
0
  const struct ecoff_debug_swap * const swap =
3784
0
    &ecoff_backend (input_bfd)->debug_swap;
3785
0
  HDRR *symhdr = &debug->symbolic_header;
3786
0
  bool ret;
3787
3788
0
#define READ(ptr, offset, count, size)          \
3789
0
  do                  \
3790
0
    {                 \
3791
0
      size_t amt;             \
3792
0
      debug->ptr = NULL;            \
3793
0
      if (symhdr->count == 0)           \
3794
0
  break;               \
3795
0
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
3796
0
  {               \
3797
0
    bfd_set_error (bfd_error_file_too_big);     \
3798
0
    ret = false;              \
3799
0
    goto return_something;          \
3800
0
  }                \
3801
0
      if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0)   \
3802
0
  {               \
3803
0
    ret = false;              \
3804
0
    goto return_something;          \
3805
0
  }                \
3806
0
      debug->ptr = _bfd_malloc_and_read (input_bfd, amt + 1, amt);  \
3807
0
      if (debug->ptr == NULL)           \
3808
0
  {               \
3809
0
    ret = false;              \
3810
0
    goto return_something;          \
3811
0
  }                \
3812
0
      ((char *) debug->ptr)[amt] = 0;         \
3813
0
    } while (0)
3814
3815
  /* If alloc_syments is true, then the data was already by read by
3816
     _bfd_ecoff_slurp_symbolic_info.  */
3817
0
  if (!debug->alloc_syments)
3818
0
    {
3819
0
      READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
3820
0
      READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
3821
0
      READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
3822
0
      READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
3823
0
      READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
3824
0
      READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
3825
0
      READ (ss, cbSsOffset, issMax, sizeof (char));
3826
0
      READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
3827
0
      READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
3828
0
    }
3829
0
#undef READ
3830
3831
  /* We do not read the external strings or the external symbols.  */
3832
3833
0
  ret = (bfd_ecoff_debug_accumulate
3834
0
   (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
3835
0
    &ecoff_backend (output_bfd)->debug_swap,
3836
0
    input_bfd, debug, swap, info));
3837
3838
0
 return_something:
3839
0
  _bfd_ecoff_free_ecoff_debug_info (debug);
3840
0
  return ret;
3841
0
}
3842
3843
/* Relocate and write an ECOFF section into an ECOFF output file.  */
3844
3845
static bool
3846
ecoff_indirect_link_order (bfd *output_bfd,
3847
         struct bfd_link_info *info,
3848
         asection *output_section,
3849
         struct bfd_link_order *link_order)
3850
0
{
3851
0
  asection *input_section;
3852
0
  bfd *input_bfd;
3853
0
  bfd_byte *contents = NULL;
3854
0
  bfd_size_type external_reloc_size;
3855
0
  bfd_size_type external_relocs_size;
3856
0
  void * external_relocs = NULL;
3857
3858
0
  BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
3859
3860
0
  input_section = link_order->u.indirect.section;
3861
0
  input_bfd = input_section->owner;
3862
0
  if (input_section->size == 0)
3863
0
    return true;
3864
3865
0
  BFD_ASSERT (input_section->output_section == output_section);
3866
0
  BFD_ASSERT (input_section->output_offset == link_order->offset);
3867
0
  BFD_ASSERT (input_section->size == link_order->size);
3868
3869
  /* Get the section contents.  */
3870
0
  if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
3871
0
    goto error_return;
3872
3873
  /* Get the relocs.  If we are relaxing MIPS code, they will already
3874
     have been read in.  Otherwise, we read them in now.  */
3875
0
  external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
3876
0
  external_relocs_size = external_reloc_size * input_section->reloc_count;
3877
3878
0
  if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0)
3879
0
    goto error_return;
3880
0
  external_relocs = _bfd_malloc_and_read (input_bfd, external_relocs_size,
3881
0
            external_relocs_size);
3882
0
  if (external_relocs == NULL && external_relocs_size != 0)
3883
0
    goto error_return;
3884
3885
  /* Relocate the section contents.  */
3886
0
  if (! ((*ecoff_backend (input_bfd)->relocate_section)
3887
0
   (output_bfd, info, input_bfd, input_section, contents,
3888
0
    external_relocs)))
3889
0
    goto error_return;
3890
3891
  /* Write out the relocated section.  */
3892
0
  if (! bfd_set_section_contents (output_bfd,
3893
0
          output_section,
3894
0
          contents,
3895
0
          input_section->output_offset,
3896
0
          input_section->size))
3897
0
    goto error_return;
3898
3899
  /* If we are producing relocatable output, the relocs were
3900
     modified, and we write them out now.  We use the reloc_count
3901
     field of output_section to keep track of the number of relocs we
3902
     have output so far.  */
3903
0
  if (bfd_link_relocatable (info))
3904
0
    {
3905
0
      file_ptr pos = (output_section->rel_filepos
3906
0
          + output_section->reloc_count * external_reloc_size);
3907
0
      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3908
0
    || (bfd_write (external_relocs, external_relocs_size, output_bfd)
3909
0
        != external_relocs_size))
3910
0
  goto error_return;
3911
0
      output_section->reloc_count += input_section->reloc_count;
3912
0
    }
3913
3914
0
  free (contents);
3915
0
  free (external_relocs);
3916
0
  return true;
3917
3918
0
 error_return:
3919
0
  free (contents);
3920
0
  free (external_relocs);
3921
0
  return false;
3922
0
}
3923
3924
/* Generate a reloc when linking an ECOFF file.  This is a reloc
3925
   requested by the linker, and does come from any input file.  This
3926
   is used to build constructor and destructor tables when linking
3927
   with -Ur.  */
3928
3929
static bool
3930
ecoff_reloc_link_order (bfd *output_bfd,
3931
      struct bfd_link_info *info,
3932
      asection *output_section,
3933
      struct bfd_link_order *link_order)
3934
0
{
3935
0
  enum bfd_link_order_type type;
3936
0
  asection *section;
3937
0
  bfd_vma addend;
3938
0
  arelent rel;
3939
0
  struct internal_reloc in;
3940
0
  bfd_size_type external_reloc_size;
3941
0
  bfd_byte *rbuf;
3942
0
  bool ok;
3943
0
  file_ptr pos;
3944
3945
0
  type = link_order->type;
3946
0
  section = NULL;
3947
0
  addend = link_order->u.reloc.p->addend;
3948
3949
  /* We set up an arelent to pass to the backend adjust_reloc_out
3950
     routine.  */
3951
0
  rel.address = link_order->offset;
3952
3953
0
  rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
3954
0
  if (rel.howto == 0)
3955
0
    {
3956
0
      bfd_set_error (bfd_error_bad_value);
3957
0
      return false;
3958
0
    }
3959
3960
0
  if (type == bfd_section_reloc_link_order)
3961
0
    {
3962
0
      section = link_order->u.reloc.p->u.section;
3963
0
      rel.sym_ptr_ptr = section->symbol_ptr_ptr;
3964
0
    }
3965
0
  else
3966
0
    {
3967
0
      struct bfd_link_hash_entry *h;
3968
3969
      /* Treat a reloc against a defined symbol as though it were
3970
   actually against the section.  */
3971
0
      h = bfd_wrapped_link_hash_lookup (output_bfd, info,
3972
0
          link_order->u.reloc.p->u.name,
3973
0
          false, false, false);
3974
0
      if (h != NULL
3975
0
    && (h->type == bfd_link_hash_defined
3976
0
        || h->type == bfd_link_hash_defweak))
3977
0
  {
3978
0
    type = bfd_section_reloc_link_order;
3979
0
    section = h->u.def.section->output_section;
3980
    /* It seems that we ought to add the symbol value to the
3981
       addend here, but in practice it has already been added
3982
       because it was passed to constructor_callback.  */
3983
0
    addend += section->vma + h->u.def.section->output_offset;
3984
0
  }
3985
0
      else
3986
0
  {
3987
    /* We can't set up a reloc against a symbol correctly,
3988
       because we have no asymbol structure.  Currently no
3989
       adjust_reloc_out routine cares.  */
3990
0
    rel.sym_ptr_ptr = NULL;
3991
0
  }
3992
0
    }
3993
3994
  /* All ECOFF relocs are in-place.  Put the addend into the object
3995
     file.  */
3996
3997
0
  BFD_ASSERT (rel.howto->partial_inplace);
3998
0
  if (addend != 0)
3999
0
    {
4000
0
      bfd_size_type size;
4001
0
      bfd_reloc_status_type rstat;
4002
0
      bfd_byte *buf;
4003
4004
0
      size = bfd_get_reloc_size (rel.howto);
4005
0
      buf = (bfd_byte *) bfd_zmalloc (size);
4006
0
      if (buf == NULL && size != 0)
4007
0
  return false;
4008
0
      rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4009
0
              (bfd_vma) addend, buf);
4010
0
      switch (rstat)
4011
0
  {
4012
0
  case bfd_reloc_ok:
4013
0
    break;
4014
0
  default:
4015
0
  case bfd_reloc_outofrange:
4016
0
    abort ();
4017
0
  case bfd_reloc_overflow:
4018
0
    (*info->callbacks->reloc_overflow)
4019
0
      (info, NULL,
4020
0
       (link_order->type == bfd_section_reloc_link_order
4021
0
        ? bfd_section_name (section)
4022
0
        : link_order->u.reloc.p->u.name),
4023
0
       rel.howto->name, addend, NULL, NULL, (bfd_vma) 0);
4024
0
    break;
4025
0
  }
4026
0
      ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
4027
0
             (file_ptr) link_order->offset, size);
4028
0
      free (buf);
4029
0
      if (! ok)
4030
0
  return false;
4031
0
    }
4032
4033
0
  rel.addend = 0;
4034
4035
  /* Move the information into an internal_reloc structure.  */
4036
0
  in.r_vaddr = rel.address + bfd_section_vma (output_section);
4037
0
  in.r_type = rel.howto->type;
4038
4039
0
  if (type == bfd_symbol_reloc_link_order)
4040
0
    {
4041
0
      struct ecoff_link_hash_entry *h;
4042
4043
0
      h = ((struct ecoff_link_hash_entry *)
4044
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
4045
0
           link_order->u.reloc.p->u.name,
4046
0
           false, false, true));
4047
0
      if (h != NULL
4048
0
    && h->indx != -1)
4049
0
  in.r_symndx = h->indx;
4050
0
      else
4051
0
  {
4052
0
    (*info->callbacks->unattached_reloc)
4053
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
4054
0
    in.r_symndx = 0;
4055
0
  }
4056
0
      in.r_extern = 1;
4057
0
    }
4058
0
  else
4059
0
    {
4060
0
      const char *name;
4061
0
      unsigned int i;
4062
0
      static struct
4063
0
      {
4064
0
  const char * name;
4065
0
  long r_symndx;
4066
0
      }
4067
0
      section_symndx [] =
4068
0
      {
4069
0
  { _TEXT,   RELOC_SECTION_TEXT   },
4070
0
  { _RDATA,  RELOC_SECTION_RDATA  },
4071
0
  { _DATA,   RELOC_SECTION_DATA   },
4072
0
  { _SDATA,  RELOC_SECTION_SDATA  },
4073
0
  { _SBSS,   RELOC_SECTION_SBSS   },
4074
0
  { _BSS,    RELOC_SECTION_BSS    },
4075
0
  { _INIT,   RELOC_SECTION_INIT   },
4076
0
  { _LIT8,   RELOC_SECTION_LIT8   },
4077
0
  { _LIT4,   RELOC_SECTION_LIT4   },
4078
0
  { _XDATA,  RELOC_SECTION_XDATA  },
4079
0
  { _PDATA,  RELOC_SECTION_PDATA  },
4080
0
  { _FINI,   RELOC_SECTION_FINI   },
4081
0
  { _LITA,   RELOC_SECTION_LITA   },
4082
0
  { "*ABS*", RELOC_SECTION_ABS    },
4083
0
  { _RCONST, RELOC_SECTION_RCONST }
4084
0
      };
4085
4086
0
      name = bfd_section_name (section);
4087
4088
0
      for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
4089
0
  if (streq (name, section_symndx[i].name))
4090
0
    {
4091
0
      in.r_symndx = section_symndx[i].r_symndx;
4092
0
      break;
4093
0
    }
4094
4095
0
      if (i == ARRAY_SIZE (section_symndx))
4096
0
  abort ();
4097
4098
0
      in.r_extern = 0;
4099
0
    }
4100
4101
  /* Let the BFD backend adjust the reloc.  */
4102
0
  (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4103
4104
  /* Get some memory and swap out the reloc.  */
4105
0
  external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4106
0
  rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4107
0
  if (rbuf == NULL)
4108
0
    return false;
4109
4110
0
  (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
4111
4112
0
  pos = (output_section->rel_filepos
4113
0
   + output_section->reloc_count * external_reloc_size);
4114
0
  ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4115
0
  && (bfd_write (rbuf, external_reloc_size, output_bfd)
4116
0
      == external_reloc_size));
4117
4118
0
  if (ok)
4119
0
    ++output_section->reloc_count;
4120
4121
0
  free (rbuf);
4122
4123
0
  return ok;
4124
0
}
4125
4126
/* Put out information for an external symbol.  These come only from
4127
   the hash table.  */
4128
4129
static bool
4130
ecoff_link_write_external (struct bfd_hash_entry *bh, void * data)
4131
0
{
4132
0
  struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh;
4133
0
  struct extsym_info *einfo = (struct extsym_info *) data;
4134
0
  bfd *output_bfd = einfo->abfd;
4135
0
  bool strip;
4136
4137
0
  if (h->root.type == bfd_link_hash_warning)
4138
0
    {
4139
0
      h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4140
0
      if (h->root.type == bfd_link_hash_new)
4141
0
  return true;
4142
0
    }
4143
4144
  /* We need to check if this symbol is being stripped.  */
4145
0
  if (h->root.type == bfd_link_hash_undefined
4146
0
      || h->root.type == bfd_link_hash_undefweak)
4147
0
    strip = false;
4148
0
  else if (einfo->info->strip == strip_all
4149
0
     || (einfo->info->strip == strip_some
4150
0
         && bfd_hash_lookup (einfo->info->keep_hash,
4151
0
           h->root.root.string,
4152
0
           false, false) == NULL))
4153
0
    strip = true;
4154
0
  else
4155
0
    strip = false;
4156
4157
0
  if (strip || h->written)
4158
0
    return true;
4159
4160
0
  if (h->abfd == NULL)
4161
0
    {
4162
0
      h->esym.jmptbl = 0;
4163
0
      h->esym.cobol_main = 0;
4164
0
      h->esym.weakext = 0;
4165
0
      h->esym.reserved = 0;
4166
0
      h->esym.ifd = ifdNil;
4167
0
      h->esym.asym.value = 0;
4168
0
      h->esym.asym.st = stGlobal;
4169
4170
0
      if (h->root.type != bfd_link_hash_defined
4171
0
    && h->root.type != bfd_link_hash_defweak)
4172
0
  h->esym.asym.sc = scAbs;
4173
0
      else
4174
0
  {
4175
0
    asection *output_section;
4176
0
    const char *name;
4177
0
    unsigned int i;
4178
0
    static struct
4179
0
    {
4180
0
      const char * name;
4181
0
      int sc;
4182
0
    }
4183
0
    section_storage_classes [] =
4184
0
    {
4185
0
      { _TEXT,   scText   },
4186
0
      { _DATA,   scData   },
4187
0
      { _SDATA,  scSData  },
4188
0
      { _RDATA,  scRData  },
4189
0
      { _BSS,    scBss    },
4190
0
      { _SBSS,   scSBss   },
4191
0
      { _INIT,   scInit   },
4192
0
      { _FINI,   scFini   },
4193
0
      { _PDATA,  scPData  },
4194
0
      { _XDATA,  scXData  },
4195
0
      { _RCONST, scRConst }
4196
0
    };
4197
4198
0
    output_section = h->root.u.def.section->output_section;
4199
0
    name = bfd_section_name (output_section);
4200
4201
0
    for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++)
4202
0
      if (streq (name, section_storage_classes[i].name))
4203
0
        {
4204
0
    h->esym.asym.sc = section_storage_classes[i].sc;
4205
0
    break;
4206
0
        }
4207
4208
0
    if (i == ARRAY_SIZE (section_storage_classes))
4209
0
      h->esym.asym.sc = scAbs;
4210
0
  }
4211
4212
0
      h->esym.asym.reserved = 0;
4213
0
      h->esym.asym.index = indexNil;
4214
0
    }
4215
0
  else if (h->esym.ifd != -1)
4216
0
    {
4217
0
      struct ecoff_debug_info *debug;
4218
4219
      /* Adjust the FDR index for the symbol by that used for the
4220
   input BFD.  */
4221
0
      debug = &ecoff_data (h->abfd)->debug_info;
4222
0
      BFD_ASSERT (h->esym.ifd >= 0
4223
0
      && h->esym.ifd < debug->symbolic_header.ifdMax);
4224
0
      h->esym.ifd = debug->ifdmap[h->esym.ifd];
4225
0
    }
4226
4227
0
  switch (h->root.type)
4228
0
    {
4229
0
    default:
4230
0
    case bfd_link_hash_warning:
4231
0
    case bfd_link_hash_new:
4232
0
      abort ();
4233
0
    case bfd_link_hash_undefined:
4234
0
    case bfd_link_hash_undefweak:
4235
0
      if (h->esym.asym.sc != scUndefined
4236
0
    && h->esym.asym.sc != scSUndefined)
4237
0
  h->esym.asym.sc = scUndefined;
4238
0
      break;
4239
0
    case bfd_link_hash_defined:
4240
0
    case bfd_link_hash_defweak:
4241
0
      if (h->esym.asym.sc == scUndefined
4242
0
    || h->esym.asym.sc == scSUndefined)
4243
0
  h->esym.asym.sc = scAbs;
4244
0
      else if (h->esym.asym.sc == scCommon)
4245
0
  h->esym.asym.sc = scBss;
4246
0
      else if (h->esym.asym.sc == scSCommon)
4247
0
  h->esym.asym.sc = scSBss;
4248
0
      h->esym.asym.value = (h->root.u.def.value
4249
0
          + h->root.u.def.section->output_section->vma
4250
0
          + h->root.u.def.section->output_offset);
4251
0
      break;
4252
0
    case bfd_link_hash_common:
4253
0
      if (h->esym.asym.sc != scCommon
4254
0
    && h->esym.asym.sc != scSCommon)
4255
0
  h->esym.asym.sc = scCommon;
4256
0
      h->esym.asym.value = h->root.u.c.size;
4257
0
      break;
4258
0
    case bfd_link_hash_indirect:
4259
      /* We ignore these symbols, since the indirected symbol is
4260
   already in the hash table.  */
4261
0
      return true;
4262
0
    }
4263
4264
  /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4265
     symbol number.  */
4266
0
  h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4267
0
  h->written = 1;
4268
4269
0
  return (bfd_ecoff_debug_one_external
4270
0
    (output_bfd, &ecoff_data (output_bfd)->debug_info,
4271
0
     &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4272
0
     &h->esym));
4273
0
}
4274
4275
/* ECOFF final link routine.  This looks through all the input BFDs
4276
   and gathers together all the debugging information, and then
4277
   processes all the link order information.  This may cause it to
4278
   close and reopen some input BFDs; I'll see how bad this is.  */
4279
4280
bool
4281
_bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
4282
0
{
4283
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4284
0
  struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4285
0
  HDRR *symhdr;
4286
0
  void * handle;
4287
0
  bfd *input_bfd;
4288
0
  asection *o;
4289
0
  struct bfd_link_order *p;
4290
0
  struct extsym_info einfo;
4291
4292
  /* We accumulate the debugging information counts in the symbolic
4293
     header.  */
4294
0
  symhdr = &debug->symbolic_header;
4295
0
  symhdr->vstamp = 0;
4296
0
  symhdr->ilineMax = 0;
4297
0
  symhdr->cbLine = 0;
4298
0
  symhdr->idnMax = 0;
4299
0
  symhdr->ipdMax = 0;
4300
0
  symhdr->isymMax = 0;
4301
0
  symhdr->ioptMax = 0;
4302
0
  symhdr->iauxMax = 0;
4303
0
  symhdr->issMax = 0;
4304
0
  symhdr->issExtMax = 0;
4305
0
  symhdr->ifdMax = 0;
4306
0
  symhdr->crfd = 0;
4307
0
  symhdr->iextMax = 0;
4308
4309
  /* We accumulate the debugging information itself in the debug_info
4310
     structure.  */
4311
0
  debug->line = NULL;
4312
0
  debug->external_dnr = NULL;
4313
0
  debug->external_pdr = NULL;
4314
0
  debug->external_sym = NULL;
4315
0
  debug->external_opt = NULL;
4316
0
  debug->external_aux = NULL;
4317
0
  debug->ss = NULL;
4318
0
  debug->ssext = debug->ssext_end = NULL;
4319
0
  debug->external_fdr = NULL;
4320
0
  debug->external_rfd = NULL;
4321
0
  debug->external_ext = debug->external_ext_end = NULL;
4322
4323
0
  handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4324
0
  if (handle == NULL)
4325
0
    return false;
4326
4327
  /* Accumulate the debugging symbols from each input BFD.  */
4328
0
  for (input_bfd = info->input_bfds;
4329
0
       input_bfd != NULL;
4330
0
       input_bfd = input_bfd->link.next)
4331
0
    {
4332
0
      bool ret;
4333
4334
0
      if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4335
0
  {
4336
    /* Arbitrarily set the symbolic header vstamp to the vstamp
4337
       of the first object file in the link.  */
4338
0
    if (symhdr->vstamp == 0)
4339
0
      symhdr->vstamp
4340
0
        = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4341
0
    ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4342
0
               handle);
4343
0
  }
4344
0
      else
4345
0
  ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4346
0
            debug, &backend->debug_swap,
4347
0
            input_bfd, info);
4348
0
      if (! ret)
4349
0
  return false;
4350
4351
      /* Combine the register masks.  */
4352
0
      ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4353
0
      ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4354
0
      ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4355
0
      ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4356
0
      ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4357
0
      ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4358
0
    }
4359
4360
  /* Write out the external symbols.  */
4361
0
  einfo.abfd = abfd;
4362
0
  einfo.info = info;
4363
0
  bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo);
4364
4365
0
  if (bfd_link_relocatable (info))
4366
0
    {
4367
      /* We need to make a pass over the link_orders to count up the
4368
   number of relocations we will need to output, so that we know
4369
   how much space they will take up.  */
4370
0
      for (o = abfd->sections; o != NULL; o = o->next)
4371
0
  {
4372
0
    o->reloc_count = 0;
4373
0
    for (p = o->map_head.link_order;
4374
0
         p != NULL;
4375
0
         p = p->next)
4376
0
      if (p->type == bfd_indirect_link_order)
4377
0
        o->reloc_count += p->u.indirect.section->reloc_count;
4378
0
      else if (p->type == bfd_section_reloc_link_order
4379
0
         || p->type == bfd_symbol_reloc_link_order)
4380
0
        ++o->reloc_count;
4381
0
  }
4382
0
    }
4383
4384
  /* Compute the reloc and symbol file positions.  */
4385
0
  ecoff_compute_reloc_file_positions (abfd);
4386
4387
  /* Write out the debugging information.  */
4388
0
  if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4389
0
             &backend->debug_swap, info,
4390
0
             ecoff_data (abfd)->sym_filepos))
4391
0
    return false;
4392
4393
0
  bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4394
4395
0
  if (bfd_link_relocatable (info))
4396
0
    {
4397
      /* Now reset the reloc_count field of the sections in the output
4398
   BFD to 0, so that we can use them to keep track of how many
4399
   relocs we have output thus far.  */
4400
0
      for (o = abfd->sections; o != NULL; o = o->next)
4401
0
  o->reloc_count = 0;
4402
0
    }
4403
4404
  /* Get a value for the GP register.  */
4405
0
  if (ecoff_data (abfd)->gp == 0)
4406
0
    {
4407
0
      struct bfd_link_hash_entry *h;
4408
4409
0
      h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4410
0
      if (h != NULL
4411
0
    && h->type == bfd_link_hash_defined)
4412
0
  ecoff_data (abfd)->gp = (h->u.def.value
4413
0
         + h->u.def.section->output_section->vma
4414
0
         + h->u.def.section->output_offset);
4415
0
      else if (bfd_link_relocatable (info))
4416
0
  {
4417
0
    bfd_vma lo;
4418
4419
    /* Make up a value.  */
4420
0
    lo = (bfd_vma) -1;
4421
0
    for (o = abfd->sections; o != NULL; o = o->next)
4422
0
      {
4423
0
        if (o->vma < lo
4424
0
      && (streq (o->name, _SBSS)
4425
0
          || streq (o->name, _SDATA)
4426
0
          || streq (o->name, _LIT4)
4427
0
          || streq (o->name, _LIT8)
4428
0
          || streq (o->name, _LITA)))
4429
0
    lo = o->vma;
4430
0
      }
4431
0
    ecoff_data (abfd)->gp = lo + 0x8000;
4432
0
  }
4433
0
      else
4434
0
  {
4435
    /* If the relocate_section function needs to do a reloc
4436
       involving the GP value, it should make a reloc_dangerous
4437
       callback to warn that GP is not defined.  */
4438
0
  }
4439
0
    }
4440
4441
0
  for (o = abfd->sections; o != NULL; o = o->next)
4442
0
    {
4443
0
      for (p = o->map_head.link_order;
4444
0
     p != NULL;
4445
0
     p = p->next)
4446
0
  {
4447
0
    if (p->type == bfd_indirect_link_order
4448
0
        && (bfd_get_flavour (p->u.indirect.section->owner)
4449
0
      == bfd_target_ecoff_flavour))
4450
0
      {
4451
0
        if (! ecoff_indirect_link_order (abfd, info, o, p))
4452
0
    return false;
4453
0
      }
4454
0
    else if (p->type == bfd_section_reloc_link_order
4455
0
       || p->type == bfd_symbol_reloc_link_order)
4456
0
      {
4457
0
        if (! ecoff_reloc_link_order (abfd, info, o, p))
4458
0
    return false;
4459
0
      }
4460
0
    else
4461
0
      {
4462
0
        if (! _bfd_default_link_order (abfd, info, o, p))
4463
0
    return false;
4464
0
      }
4465
0
  }
4466
0
    }
4467
4468
0
  abfd->symcount = symhdr->iextMax + symhdr->isymMax;
4469
4470
0
  ecoff_data (abfd)->linker = true;
4471
4472
0
  return true;
4473
0
}