Coverage Report

Created: 2025-07-08 11:15

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