Coverage Report

Created: 2026-09-14 08:07

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