Coverage Report

Created: 2026-04-04 08:16

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