Coverage Report

Created: 2026-11-21 09:00

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.51M
#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
9.10k
{
60
9.10k
  size_t amt = sizeof (ecoff_data_type);
61
62
9.10k
  abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
63
9.10k
  if (abfd->tdata.ecoff_obj_data == NULL)
64
0
    return false;
65
66
9.10k
  return true;
67
9.10k
}
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.66k
{
75
7.66k
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
76
7.66k
  struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
77
7.66k
  ecoff_data_type *ecoff;
78
79
7.66k
  if (! _bfd_ecoff_mkobject (abfd))
80
0
    return NULL;
81
82
7.66k
  ecoff = ecoff_data (abfd);
83
7.66k
  ecoff->gp_size = 8;
84
7.66k
  ecoff->sym_filepos = internal_f->f_symptr;
85
86
7.66k
  if (internal_a != NULL)
87
1.61k
    {
88
1.61k
      int i;
89
90
1.61k
      ecoff->text_start = internal_a->text_start;
91
1.61k
      ecoff->text_end = internal_a->text_start + internal_a->tsize;
92
1.61k
      ecoff->gp = internal_a->gp_value;
93
1.61k
      ecoff->gprmask = internal_a->gprmask;
94
8.09k
      for (i = 0; i < 4; i++)
95
6.47k
  ecoff->cprmask[i] = internal_a->cprmask[i];
96
1.61k
      ecoff->fprmask = internal_a->fprmask;
97
1.61k
      if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
98
31
  abfd->flags |= D_PAGED;
99
1.58k
      else
100
1.58k
  abfd->flags &=~ D_PAGED;
101
1.61k
    }
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.66k
  return (void *) ecoff;
110
7.66k
}
111
112
bool
113
_bfd_ecoff_bfd_free_cached_info (bfd *abfd)
114
188k
{
115
188k
  struct ecoff_tdata *tdata;
116
117
188k
  if (bfd_get_format (abfd) == bfd_object
118
7.79k
      && (tdata = ecoff_data (abfd)) != NULL)
119
7.79k
    {
120
116k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
121
108k
  {
122
108k
    struct ecoff_section_tdata *sdata = ecoff_section_data (sec);
123
108k
    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
108k
  }
133
7.79k
      _bfd_ecoff_free_ecoff_debug_info (&tdata->debug_info);
134
7.79k
    }
135
188k
  return _bfd_generic_bfd_free_cached_info (abfd);
136
188k
}
137
138
/* Initialize a new section.  */
139
140
bool
141
_bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
142
190k
{
143
190k
  unsigned int i;
144
190k
  static struct
145
190k
  {
146
190k
    const char * name;
147
190k
    flagword flags;
148
190k
  }
149
190k
  section_flags [] =
150
190k
  {
151
190k
    { _TEXT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
152
190k
    { _INIT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
153
190k
    { _FINI,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
154
190k
    { _DATA,   SEC_ALLOC | SEC_DATA | SEC_LOAD },
155
190k
    { _SDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_SMALL_DATA },
156
190k
    { _RDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
157
190k
    { _LIT8,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
158
190k
    { _LIT4,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
159
190k
    { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
160
190k
    { _PDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
161
190k
    { _BSS,    SEC_ALLOC},
162
190k
    { _SBSS,   SEC_ALLOC | SEC_SMALL_DATA},
163
    /* An Irix 4 shared libary.  */
164
190k
    { _LIB,    SEC_COFF_SHARED_LIBRARY}
165
190k
  };
166
167
190k
  section->alignment_power = 4;
168
169
2.64M
  for (i = 0; i < ARRAY_SIZE (section_flags); i++)
170
2.45M
    if (streq (section->name, section_flags[i].name))
171
2.24k
      {
172
2.24k
  section->flags |= section_flags[i].flags;
173
2.24k
  break;
174
2.24k
      }
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
190k
  return _bfd_generic_new_section_hook (abfd, section);
182
190k
}
183
184
void
185
_bfd_ecoff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
186
             asection *section ATTRIBUTE_UNUSED,
187
             void *scnhdr ATTRIBUTE_UNUSED)
188
186k
{
189
186k
}
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
7.45k
{
199
7.45k
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
200
7.45k
  enum bfd_architecture arch;
201
7.45k
  unsigned long mach;
202
203
7.45k
  switch (internal_f->f_magic)
204
7.45k
    {
205
1.55k
    case MIPS_MAGIC_1:
206
1.66k
    case MIPS_MAGIC_LITTLE:
207
1.75k
    case MIPS_MAGIC_BIG:
208
1.75k
      arch = bfd_arch_mips;
209
1.75k
      mach = bfd_mach_mips3000;
210
1.75k
      break;
211
212
1.96k
    case MIPS_MAGIC_LITTLE2:
213
1.99k
    case MIPS_MAGIC_BIG2:
214
      /* MIPS ISA level 2: the r6000.  */
215
1.99k
      arch = bfd_arch_mips;
216
1.99k
      mach = bfd_mach_mips6000;
217
1.99k
      break;
218
219
239
    case MIPS_MAGIC_LITTLE3:
220
301
    case MIPS_MAGIC_BIG3:
221
      /* MIPS ISA level 3: the r4000.  */
222
301
      arch = bfd_arch_mips;
223
301
      mach = bfd_mach_mips4000;
224
301
      break;
225
226
3.39k
    case ALPHA_MAGIC:
227
3.39k
      arch = bfd_arch_alpha;
228
3.39k
      mach = 0;
229
3.39k
      break;
230
231
10
    default:
232
10
      arch = bfd_arch_obscure;
233
10
      mach = 0;
234
10
      break;
235
7.45k
    }
236
237
7.45k
  return bfd_default_set_arch_mach (abfd, arch, mach);
238
7.45k
}
239
240
bool
241
_bfd_ecoff_no_long_sections (bfd *abfd, int enable)
242
186k
{
243
186k
  (void) abfd;
244
186k
  (void) enable;
245
186k
  return false;
246
186k
}
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
182
{
254
182
  int big, little;
255
256
182
  switch (bfd_get_arch (abfd))
257
182
    {
258
115
    case bfd_arch_mips:
259
115
      switch (bfd_get_mach (abfd))
260
115
  {
261
0
  default:
262
0
  case 0:
263
79
  case bfd_mach_mips3000:
264
79
    big = MIPS_MAGIC_BIG;
265
79
    little = MIPS_MAGIC_LITTLE;
266
79
    break;
267
268
23
  case bfd_mach_mips6000:
269
23
    big = MIPS_MAGIC_BIG2;
270
23
    little = MIPS_MAGIC_LITTLE2;
271
23
    break;
272
273
13
  case bfd_mach_mips4000:
274
13
    big = MIPS_MAGIC_BIG3;
275
13
    little = MIPS_MAGIC_LITTLE3;
276
13
    break;
277
115
  }
278
279
115
      return bfd_big_endian (abfd) ? big : little;
280
281
67
    case bfd_arch_alpha:
282
67
      return ALPHA_MAGIC;
283
284
0
    default:
285
0
      abort ();
286
0
      return 0;
287
182
    }
288
182
}
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
507
{
295
507
  unsigned int i;
296
507
  static struct
297
507
  {
298
507
    const char * name;
299
507
    long flags;
300
507
  }
301
507
  styp_flags [] =
302
507
  {
303
507
    { _TEXT, STYP_TEXT  },
304
507
    { _DATA, STYP_DATA  },
305
507
    { _SDATA, STYP_SDATA  },
306
507
    { _RDATA, STYP_RDATA  },
307
507
    { _LITA, STYP_LITA  },
308
507
    { _LIT8, STYP_LIT8  },
309
507
    { _LIT4, STYP_LIT4  },
310
507
    { _BSS, STYP_BSS  },
311
507
    { _SBSS, STYP_SBSS  },
312
507
    { _INIT, STYP_ECOFF_INIT },
313
507
    { _FINI, STYP_ECOFF_FINI },
314
507
    { _PDATA, STYP_PDATA  },
315
507
    { _XDATA, STYP_XDATA  },
316
507
    { _LIB, STYP_ECOFF_LIB  },
317
507
    { _GOT, STYP_GOT  },
318
507
    { _HASH, STYP_HASH  },
319
507
    { _DYNAMIC, STYP_DYNAMIC  },
320
507
    { _LIBLIST, STYP_LIBLIST  },
321
507
    { _RELDYN, STYP_RELDYN  },
322
507
    { _CONFLIC, STYP_CONFLIC  },
323
507
    { _DYNSTR, STYP_DYNSTR  },
324
507
    { _DYNSYM, STYP_DYNSYM  },
325
507
    { _RCONST, STYP_RCONST  }
326
507
  };
327
507
  long styp = 0;
328
329
12.1k
  for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
330
11.6k
    if (streq (name, styp_flags[i].name))
331
3
      {
332
3
  styp = styp_flags[i].flags;
333
3
  break;
334
3
      }
335
336
507
  if (styp == 0)
337
504
    {
338
504
      if (streq (name, _COMMENT))
339
0
  {
340
0
    styp = STYP_COMMENT;
341
0
    flags &=~ SEC_NEVER_LOAD;
342
0
  }
343
504
      else if (flags & SEC_CODE)
344
190
  styp = STYP_TEXT;
345
314
      else if (flags & SEC_DATA)
346
72
  styp = STYP_DATA;
347
242
      else if (flags & SEC_READONLY)
348
0
  styp = STYP_RDATA;
349
242
      else if (flags & SEC_LOAD)
350
206
  styp = STYP_REG;
351
36
      else
352
36
  styp = STYP_BSS;
353
504
    }
354
355
507
  if (flags & SEC_NEVER_LOAD)
356
112
    styp |= STYP_NOLOAD;
357
358
507
  return styp;
359
507
}
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
186k
{
370
186k
  struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
371
186k
  long styp_flags = internal_s->s_flags;
372
186k
  flagword sec_flags = 0;
373
374
186k
  if (styp_flags & STYP_NOLOAD)
375
45.8k
    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
186k
  if ((styp_flags & STYP_TEXT)
380
147k
      || (styp_flags & STYP_ECOFF_INIT)
381
131k
      || (styp_flags & STYP_ECOFF_FINI)
382
116k
      || (styp_flags & STYP_DYNAMIC)
383
106k
      || (styp_flags & STYP_LIBLIST)
384
98.0k
      || (styp_flags & STYP_RELDYN)
385
94.6k
      || styp_flags == STYP_CONFLIC
386
92.7k
      || (styp_flags & STYP_DYNSTR)
387
88.3k
      || (styp_flags & STYP_DYNSYM)
388
84.9k
      || (styp_flags & STYP_HASH))
389
107k
    {
390
107k
      if (sec_flags & SEC_NEVER_LOAD)
391
39.0k
  sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
392
67.9k
      else
393
67.9k
  sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
394
107k
    }
395
79.4k
  else if ((styp_flags & STYP_DATA)
396
75.5k
     || (styp_flags & STYP_RDATA)
397
71.4k
     || (styp_flags & STYP_SDATA)
398
67.4k
     || styp_flags == STYP_PDATA
399
67.1k
     || styp_flags == STYP_XDATA
400
66.9k
     || (styp_flags & STYP_GOT)
401
63.4k
     || styp_flags == STYP_RCONST)
402
16.2k
    {
403
16.2k
      if (sec_flags & SEC_NEVER_LOAD)
404
2.93k
  sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
405
13.3k
      else
406
13.3k
  sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
407
16.2k
      if ((styp_flags & STYP_RDATA)
408
11.5k
    || styp_flags == STYP_PDATA
409
11.2k
    || styp_flags == STYP_RCONST)
410
5.27k
  sec_flags |= SEC_READONLY;
411
16.2k
      if (styp_flags & STYP_SDATA)
412
4.81k
  sec_flags |= SEC_SMALL_DATA;
413
16.2k
    }
414
63.1k
  else if (styp_flags & STYP_SBSS)
415
1.19k
    sec_flags |= SEC_ALLOC | SEC_SMALL_DATA;
416
61.9k
  else if (styp_flags & STYP_BSS)
417
1.23k
    sec_flags |= SEC_ALLOC;
418
60.7k
  else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
419
849
    sec_flags |= SEC_NEVER_LOAD;
420
59.9k
  else if ((styp_flags & STYP_LITA)
421
57.1k
     || (styp_flags & STYP_LIT8)
422
54.3k
     || (styp_flags & STYP_LIT4))
423
8.48k
    sec_flags |= SEC_DATA |SEC_SMALL_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
424
51.4k
  else if (styp_flags & STYP_ECOFF_LIB)
425
1.07k
    sec_flags |= SEC_COFF_SHARED_LIBRARY;
426
50.3k
  else
427
50.3k
    sec_flags |= SEC_ALLOC | SEC_LOAD;
428
429
186k
  * flags_ptr = sec_flags;
430
186k
  return true;
431
186k
}
432

433
/* Read in the symbolic header for an ECOFF object file.  */
434
435
static bool
436
ecoff_slurp_symbolic_header (bfd *abfd)
437
12.4k
{
438
12.4k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
439
12.4k
  bfd_size_type external_hdr_size;
440
12.4k
  void * raw = NULL;
441
12.4k
  HDRR *internal_symhdr;
442
443
  /* See if we've already read it in.  */
444
12.4k
  if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
445
12.4k
      backend->debug_swap.sym_magic)
446
6.97k
    return true;
447
448
  /* See whether there is a symbolic header.  */
449
5.46k
  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
5.46k
  external_hdr_size = backend->debug_swap.external_hdr_size;
460
5.46k
  if (bfd_get_symcount (abfd) != external_hdr_size)
461
1.08k
    {
462
1.08k
      bfd_set_error (bfd_error_bad_value);
463
1.08k
      return false;
464
1.08k
    }
465
466
  /* Read the symbolic information header.  */
467
4.38k
  if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0)
468
350
    goto error_return;
469
4.03k
  raw = _bfd_malloc_and_read (abfd, external_hdr_size, external_hdr_size);
470
4.03k
  if (raw == NULL)
471
324
    goto error_return;
472
473
3.70k
  internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
474
3.70k
  (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
475
476
3.70k
  if (internal_symhdr->magic != backend->debug_swap.sym_magic)
477
1.09k
    {
478
1.09k
      bfd_set_error (bfd_error_bad_value);
479
1.09k
      goto error_return;
480
1.09k
    }
481
482
2.60k
#define FIX(start, count) \
483
28.6k
  if (internal_symhdr->start == 0) \
484
28.6k
    internal_symhdr->count = 0;
485
486
2.60k
  FIX (cbLineOffset, cbLine);
487
2.60k
  FIX (cbDnOffset, idnMax);
488
2.60k
  FIX (cbPdOffset, ipdMax);
489
2.60k
  FIX (cbSymOffset, isymMax);
490
2.60k
  FIX (cbOptOffset, ioptMax);
491
2.60k
  FIX (cbAuxOffset, iauxMax);
492
2.60k
  FIX (cbSsOffset, issMax);
493
2.60k
  FIX (cbSsExtOffset, issExtMax);
494
2.60k
  FIX (cbFdOffset, ifdMax);
495
2.60k
  FIX (cbRfdOffset, crfd);
496
2.60k
  FIX (cbExtOffset, iextMax);
497
2.60k
#undef FIX
498
499
  /* Now we can get the correct number of symbols.  */
500
2.60k
  abfd->symcount = internal_symhdr->isymMax + internal_symhdr->iextMax;
501
502
2.60k
  free (raw);
503
2.60k
  return true;
504
1.77k
 error_return:
505
1.77k
  free (raw);
506
1.77k
  return false;
507
3.70k
}
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
29.5k
{
518
29.5k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
519
29.5k
  HDRR *internal_symhdr;
520
29.5k
  bfd_size_type raw_base;
521
29.5k
  bfd_size_type raw_size;
522
29.5k
  void * raw;
523
29.5k
  bfd_size_type external_fdr_size;
524
29.5k
  char *fraw_src;
525
29.5k
  char *fraw_end;
526
29.5k
  struct fdr *fdr_ptr;
527
29.5k
  bfd_size_type raw_end;
528
29.5k
  bfd_size_type cb_end;
529
29.5k
  file_ptr pos;
530
29.5k
  size_t amt;
531
532
29.5k
  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
29.5k
  if (debug->alloc_syments)
537
12.2k
    return true;
538
17.2k
  if (ecoff_data (abfd)->sym_filepos == 0)
539
4.82k
    {
540
4.82k
      abfd->symcount = 0;
541
4.82k
      return true;
542
4.82k
    }
543
544
12.4k
  if (! ecoff_slurp_symbolic_header (abfd))
545
2.85k
    return false;
546
547
9.58k
  internal_symhdr = &debug->symbolic_header;
548
549
  /* Read all the symbolic information at once.  */
550
9.58k
  raw_base = (ecoff_data (abfd)->sym_filepos
551
9.58k
        + 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
9.58k
  raw_end = raw_base;
559
560
9.58k
#define UPDATE_RAW_END(start, count, size) \
561
82.0k
  do                  \
562
82.0k
    if (internal_symhdr->count != 0)         \
563
82.0k
      {                 \
564
53.5k
  if (internal_symhdr->start < raw_base)       \
565
53.5k
    goto err;             \
566
53.5k
  if (_bfd_mul_overflow ((unsigned long) internal_symhdr->count, \
567
51.1k
             (size), &amt))       \
568
51.1k
    goto err;             \
569
51.1k
  cb_end = internal_symhdr->start + amt;        \
570
51.1k
  if (cb_end < internal_symhdr->start)       \
571
51.1k
    goto err;             \
572
51.1k
  if (cb_end > raw_end)           \
573
48.7k
    raw_end = cb_end;           \
574
48.7k
      }                  \
575
82.0k
  while (0)
576
577
9.58k
  UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
578
9.03k
  UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
579
8.71k
  UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
580
8.25k
  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
7.73k
  UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
584
7.43k
  UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
585
7.20k
  UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
586
6.66k
  UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
587
6.30k
  UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
588
5.80k
  UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
589
5.36k
  UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
590
591
4.85k
#undef UPDATE_RAW_END
592
593
4.85k
  raw_size = raw_end - raw_base;
594
4.85k
  if (raw_size == 0)
595
54
    {
596
54
      ecoff_data (abfd)->sym_filepos = 0;
597
54
      return true;
598
54
    }
599
4.80k
  pos = ecoff_data (abfd)->sym_filepos;
600
4.80k
  pos += backend->debug_swap.external_hdr_size;
601
4.80k
  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
602
0
    return false;
603
4.80k
  raw = _bfd_alloc_and_read (abfd, raw_size, raw_size);
604
4.80k
  if (raw == NULL)
605
3.91k
    return false;
606
607
894
  debug->alloc_syments = true;
608
609
  /* Get pointers for the numeric offsets in the HDRR structure.  */
610
894
#define FIX(start, count, ptr, type) \
611
9.83k
  if (internal_symhdr->count == 0)         \
612
9.83k
    debug->ptr = NULL;             \
613
9.83k
  else                  \
614
9.83k
    debug->ptr = (type) ((char *) raw         \
615
4.14k
       + (internal_symhdr->start - raw_base))
616
617
894
  FIX (cbLineOffset, cbLine, line, unsigned char *);
618
894
  FIX (cbDnOffset, idnMax, external_dnr, void *);
619
894
  FIX (cbPdOffset, ipdMax, external_pdr, void *);
620
894
  FIX (cbSymOffset, isymMax, external_sym, void *);
621
894
  FIX (cbOptOffset, ioptMax, external_opt, void *);
622
894
  FIX (cbAuxOffset, iauxMax, external_aux, union aux_ext *);
623
894
  FIX (cbSsOffset, issMax, ss, char *);
624
894
  FIX (cbSsExtOffset, issExtMax, ssext, char *);
625
894
  FIX (cbFdOffset, ifdMax, external_fdr, void *);
626
894
  FIX (cbRfdOffset, crfd, external_rfd, void *);
627
894
  FIX (cbExtOffset, iextMax, external_ext, void *);
628
894
#undef FIX
629
630
  /* Ensure string sections are zero terminated.  */
631
894
  if (debug->ss)
632
162
    debug->ss[internal_symhdr->issMax - 1] = 0;
633
894
  if (debug->ssext)
634
652
    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
894
  if (_bfd_mul_overflow ((unsigned long) internal_symhdr->ifdMax,
645
894
       sizeof (struct fdr), &amt))
646
0
    {
647
4.72k
    err:
648
4.72k
      bfd_set_error (bfd_error_file_too_big);
649
4.72k
      return false;
650
0
    }
651
894
  debug->fdr = (FDR *) bfd_alloc (abfd, amt);
652
894
  if (debug->fdr == NULL)
653
0
    return false;
654
894
  external_fdr_size = backend->debug_swap.external_fdr_size;
655
894
  fdr_ptr = debug->fdr;
656
894
  fraw_src = (char *) debug->external_fdr;
657
  /* PR 17512: file: 3372-1243-0.004.  */
658
894
  if (fraw_src == NULL && internal_symhdr->ifdMax > 0)
659
0
    goto err;
660
894
  fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
661
7.65k
  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
662
6.76k
    (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
663
664
894
  return true;
665
894
}
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
191k
{
675
191k
  ecoff_symbol_type *new_symbol;
676
191k
  size_t amt = sizeof (ecoff_symbol_type);
677
678
191k
  new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
679
191k
  if (new_symbol == NULL)
680
0
    return NULL;
681
191k
  new_symbol->symbol.section = NULL;
682
191k
  new_symbol->fdr = NULL;
683
191k
  new_symbol->local = false;
684
191k
  new_symbol->native = NULL;
685
191k
  new_symbol->symbol.the_bfd = abfd;
686
191k
  return &new_symbol->symbol;
687
191k
}
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
16.1k
{
698
16.1k
  asym->the_bfd = abfd;
699
16.1k
  asym->value = ecoff_sym->value;
700
16.1k
  asym->section = &bfd_debug_section;
701
16.1k
  asym->udata.i = 0;
702
703
  /* Most symbol types are just for debugging.  */
704
16.1k
  switch (ecoff_sym->st)
705
16.1k
    {
706
427
    case stGlobal:
707
692
    case stStatic:
708
958
    case stLabel:
709
1.08k
    case stProc:
710
1.55k
    case stStaticProc:
711
1.55k
      break;
712
13.4k
    case stNil:
713
13.4k
      if (ECOFF_IS_STAB (ecoff_sym))
714
0
  {
715
0
    asym->flags = BSF_DEBUGGING;
716
0
    return true;
717
0
  }
718
13.4k
      break;
719
13.4k
    default:
720
1.18k
      asym->flags = BSF_DEBUGGING;
721
1.18k
      return true;
722
16.1k
    }
723
724
14.9k
  if (weak)
725
686
    asym->flags = BSF_EXPORT | BSF_WEAK;
726
14.2k
  else if (ext)
727
12.3k
    asym->flags = BSF_EXPORT | BSF_GLOBAL;
728
1.95k
  else
729
1.95k
    {
730
1.95k
      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
1.95k
      if (ecoff_sym->st == stProc
738
1.91k
    || ecoff_sym->st == stLabel
739
1.86k
    || ECOFF_IS_STAB (ecoff_sym))
740
89
  asym->flags |= BSF_DEBUGGING;
741
1.95k
    }
742
743
14.9k
  if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
744
598
    asym->flags |= BSF_FUNCTION;
745
746
14.9k
  switch (ecoff_sym->sc)
747
14.9k
    {
748
11.9k
    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
11.9k
      asym->flags = BSF_LOCAL;
754
11.9k
      break;
755
180
    case scText:
756
180
      asym->section = bfd_make_section_old_way (abfd, _TEXT);
757
180
      asym->value -= asym->section->vma;
758
180
      break;
759
242
    case scData:
760
242
      asym->section = bfd_make_section_old_way (abfd, _DATA);
761
242
      asym->value -= asym->section->vma;
762
242
      break;
763
66
    case scBss:
764
66
      asym->section = bfd_make_section_old_way (abfd, _BSS);
765
66
      asym->value -= asym->section->vma;
766
66
      break;
767
229
    case scRegister:
768
229
      asym->flags = BSF_DEBUGGING;
769
229
      break;
770
6
    case scAbs:
771
6
      asym->section = bfd_abs_section_ptr;
772
6
      break;
773
117
    case scUndefined:
774
117
      asym->section = bfd_und_section_ptr;
775
117
      asym->flags = 0;
776
117
      asym->value = 0;
777
117
      break;
778
5
    case scCdbLocal:
779
216
    case scBits:
780
293
    case scCdbSystem:
781
413
    case scRegImage:
782
428
    case scInfo:
783
632
    case scUserStruct:
784
632
      asym->flags = BSF_DEBUGGING;
785
632
      break;
786
32
    case scSData:
787
32
      asym->section = bfd_make_section_old_way (abfd, ".sdata");
788
32
      asym->value -= asym->section->vma;
789
32
      break;
790
17
    case scSBss:
791
17
      asym->section = bfd_make_section_old_way (abfd, ".sbss");
792
17
      asym->value -= asym->section->vma;
793
17
      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
207
    case scVar:
799
207
      asym->flags = BSF_DEBUGGING;
800
207
      break;
801
31
    case scCommon:
802
31
      if (asym->value > ecoff_data (abfd)->gp_size)
803
13
  {
804
13
    asym->section = bfd_com_section_ptr;
805
13
    asym->flags = 0;
806
13
    break;
807
13
  }
808
      /* Fall through.  */
809
27
    case scSCommon:
810
27
      asym->section = &_bfd_ecoff_scom_section;
811
27
      asym->flags = 0;
812
27
      break;
813
12
    case scVarRegister:
814
150
    case scVariant:
815
150
      asym->flags = BSF_DEBUGGING;
816
150
      break;
817
398
    case scSUndefined:
818
398
      asym->section = bfd_und_section_ptr;
819
398
      asym->flags = 0;
820
398
      asym->value = 0;
821
398
      break;
822
122
    case scInit:
823
122
      asym->section = bfd_make_section_old_way (abfd, ".init");
824
122
      asym->value -= asym->section->vma;
825
122
      break;
826
44
    case scBasedVar:
827
190
    case scXData:
828
219
    case scPData:
829
219
      asym->flags = BSF_DEBUGGING;
830
219
      break;
831
94
    case scFini:
832
94
      asym->section = bfd_make_section_old_way (abfd, ".fini");
833
94
      asym->value -= asym->section->vma;
834
94
      break;
835
5
    case scRConst:
836
5
      asym->section = bfd_make_section_old_way (abfd, ".rconst");
837
5
      asym->value -= asym->section->vma;
838
5
      break;
839
240
    default:
840
240
      break;
841
14.9k
    }
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
14.9k
  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
14.9k
  return true;
863
14.9k
}
864
865
/* Read an ECOFF symbol table.  */
866
867
bool
868
_bfd_ecoff_slurp_symbol_table (bfd *abfd)
869
17.2k
{
870
17.2k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
871
17.2k
  const bfd_size_type external_ext_size
872
17.2k
    = backend->debug_swap.external_ext_size;
873
17.2k
  const bfd_size_type external_sym_size
874
17.2k
    = backend->debug_swap.external_sym_size;
875
17.2k
  void (* const swap_ext_in) (bfd *, void *, EXTR *)
876
17.2k
    = backend->debug_swap.swap_ext_in;
877
17.2k
  void (* const swap_sym_in) (bfd *, void *, SYMR *)
878
17.2k
    = backend->debug_swap.swap_sym_in;
879
17.2k
  ecoff_symbol_type *internal;
880
17.2k
  ecoff_symbol_type *internal_ptr;
881
17.2k
  char *eraw_src;
882
17.2k
  char *eraw_end;
883
17.2k
  FDR *fdr_ptr;
884
17.2k
  FDR *fdr_end;
885
17.2k
  size_t amt;
886
887
  /* If we've already read in the symbol table, do nothing.  */
888
17.2k
  if (ecoff_data (abfd)->canonical_symbols != NULL)
889
6.75k
    return true;
890
891
  /* Get the symbolic information.  */
892
10.5k
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
893
10.5k
          &ecoff_data (abfd)->debug_info))
894
4.02k
    return false;
895
6.47k
  if (bfd_get_symcount (abfd) == 0)
896
4.31k
    return true;
897
898
2.15k
  if (_bfd_mul_overflow (bfd_get_symcount (abfd),
899
2.15k
       sizeof (ecoff_symbol_type), &amt))
900
0
    {
901
0
      bfd_set_error (bfd_error_file_too_big);
902
0
      return false;
903
0
    }
904
2.15k
  internal = (ecoff_symbol_type *) bfd_alloc (abfd, amt);
905
2.15k
  if (internal == NULL)
906
0
    return false;
907
908
2.15k
  internal_ptr = internal;
909
2.15k
  eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
910
2.15k
  eraw_end = (eraw_src
911
2.15k
        + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
912
2.15k
     * external_ext_size));
913
16.1k
  for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
914
15.3k
    {
915
15.3k
      EXTR internal_esym;
916
917
15.3k
      (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
918
919
      /* PR 17512: file: 3372-1000-0.004.  */
920
15.3k
      HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
921
15.3k
      if (internal_esym.asym.iss >= symhdr->issExtMax
922
13.9k
    || internal_esym.asym.iss < 0)
923
1.31k
  {
924
1.31k
    bfd_set_error (bfd_error_bad_value);
925
1.31k
    return false;
926
1.31k
  }
927
928
13.9k
      internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
929
13.9k
           + internal_esym.asym.iss);
930
931
13.9k
      if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
932
13.9k
          &internal_ptr->symbol, 1,
933
13.9k
          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
13.9k
      if (internal_esym.ifd >= symhdr->ifdMax
939
7.36k
    || internal_esym.ifd < 0)
940
7.17k
  internal_ptr->fdr = NULL;
941
6.81k
      else
942
6.81k
  internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
943
6.81k
           + internal_esym.ifd);
944
13.9k
      internal_ptr->local = false;
945
13.9k
      internal_ptr->native = (void *) eraw_src;
946
13.9k
    }
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
843
  fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
951
843
  fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
952
3.62k
  for (; fdr_ptr < fdr_end; fdr_ptr++)
953
3.22k
    {
954
3.22k
      char *lraw_src;
955
3.22k
      char *lraw_end;
956
3.22k
      HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
957
958
3.22k
      if (fdr_ptr->csym == 0)
959
2.66k
  continue;
960
559
      if (fdr_ptr->isymBase < 0
961
559
    || fdr_ptr->isymBase > symhdr->isymMax
962
308
    || fdr_ptr->csym < 0
963
308
    || fdr_ptr->csym > symhdr->isymMax - fdr_ptr->isymBase
964
225
    || fdr_ptr->csym > ((long) bfd_get_symcount (abfd)
965
225
            - (internal_ptr - internal))
966
223
    || fdr_ptr->issBase < 0
967
223
    || fdr_ptr->issBase > symhdr->issMax)
968
346
  {
969
346
    bfd_set_error (bfd_error_bad_value);
970
346
    return false;
971
346
  }
972
213
      lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
973
213
      + fdr_ptr->isymBase * external_sym_size);
974
213
      lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
975
213
      for (;
976
2.36k
     lraw_src < lraw_end;
977
2.15k
     lraw_src += external_sym_size, internal_ptr++)
978
2.24k
  {
979
2.24k
    SYMR internal_sym;
980
981
2.24k
    (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
982
983
2.24k
    if (internal_sym.iss >= symhdr->issMax - fdr_ptr->issBase
984
2.15k
        || internal_sym.iss < 0)
985
97
      {
986
97
        bfd_set_error (bfd_error_bad_value);
987
97
        return false;
988
97
      }
989
2.15k
    internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
990
2.15k
               + fdr_ptr->issBase
991
2.15k
               + internal_sym.iss);
992
2.15k
    if (!ecoff_set_symbol_info (abfd, &internal_sym,
993
2.15k
              &internal_ptr->symbol, 0, 0))
994
0
      return false;
995
2.15k
    internal_ptr->fdr = fdr_ptr;
996
2.15k
    internal_ptr->local = true;
997
2.15k
    internal_ptr->native = (void *) lraw_src;
998
2.15k
  }
999
213
    }
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
400
  if (internal_ptr - internal < (ptrdiff_t) bfd_get_symcount (abfd))
1007
25
    {
1008
25
      abfd->symcount = internal_ptr - internal;
1009
25
      _bfd_error_handler
1010
  /* xgettext:c-format */
1011
25
  (_("%pB: warning: isymMax (%ld) is greater than ifdMax (%ld)"),
1012
25
   abfd, ecoff_data (abfd)->debug_info.symbolic_header.isymMax,
1013
25
   ecoff_data (abfd)->debug_info.symbolic_header.ifdMax);
1014
25
    }
1015
1016
400
  ecoff_data (abfd)->canonical_symbols = internal;
1017
1018
400
  return true;
1019
843
}
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.99k
{
1026
3.99k
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
1027
3.99k
          &ecoff_data (abfd)->debug_info))
1028
2.25k
    return -1;
1029
1030
1.74k
  if (bfd_get_symcount (abfd) == 0)
1031
574
    return 0;
1032
1033
1.16k
  return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1034
1.74k
}
1035
1036
/* Get the canonical symbols.  */
1037
1038
long
1039
_bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
1040
1.63k
{
1041
1.63k
  unsigned int counter = 0;
1042
1.63k
  ecoff_symbol_type *symbase;
1043
1.63k
  ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1044
1045
1.63k
  if (! _bfd_ecoff_slurp_symbol_table (abfd))
1046
455
    return -1;
1047
1.17k
  if (bfd_get_symcount (abfd) == 0)
1048
471
    return 0;
1049
1050
706
  symbase = ecoff_data (abfd)->canonical_symbols;
1051
3.68k
  while (counter < bfd_get_symcount (abfd))
1052
2.97k
    {
1053
2.97k
      *(location++) = symbase++;
1054
2.97k
      counter++;
1055
2.97k
    }
1056
706
  *location++ = NULL;
1057
706
  return bfd_get_symcount (abfd);
1058
1.17k
}
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
1.05k
{
1426
1.05k
  bfd_symbol_info (symbol, ret);
1427
1.05k
}
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.0k
{
1642
16.0k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1643
16.0k
  arelent *internal_relocs;
1644
16.0k
  bfd_size_type external_reloc_size;
1645
16.0k
  bfd_size_type amt;
1646
16.0k
  bfd_byte *external_relocs;
1647
16.0k
  arelent *rptr;
1648
16.0k
  unsigned int i;
1649
1650
16.0k
  if (section->relocation != NULL
1651
16.0k
      || section->reloc_count == 0
1652
15.6k
      || (section->flags & SEC_CONSTRUCTOR) != 0)
1653
379
    return true;
1654
1655
15.6k
  if (! _bfd_ecoff_slurp_symbol_table (abfd))
1656
5.33k
    return false;
1657
1658
10.2k
  external_reloc_size = backend->external_reloc_size;
1659
10.2k
  amt = external_reloc_size * section->reloc_count;
1660
10.2k
  if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1661
2.08k
    return false;
1662
8.21k
  external_relocs = _bfd_malloc_and_read (abfd, amt, amt);
1663
8.21k
  if (external_relocs == NULL)
1664
6.24k
    return false;
1665
1666
1.96k
  amt = section->reloc_count;
1667
1.96k
  amt *= sizeof (arelent);
1668
1.96k
  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1669
1.96k
  if (internal_relocs == NULL)
1670
0
    {
1671
0
      free (external_relocs);
1672
0
      return false;
1673
0
    }
1674
1675
391k
  for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1676
389k
    {
1677
389k
      struct internal_reloc intern;
1678
1679
389k
      (*backend->swap_reloc_in) (abfd,
1680
389k
         external_relocs + i * external_reloc_size,
1681
389k
         &intern);
1682
389k
      rptr->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1683
389k
      rptr->addend = 0;
1684
1685
389k
      if (intern.r_extern)
1686
57.1k
  {
1687
    /* r_symndx is an index into the external symbols.  */
1688
57.1k
    if (symbols != NULL
1689
35.8k
        && intern.r_symndx >= 0
1690
35.8k
        && (intern.r_symndx
1691
35.8k
      < (ecoff_data (abfd)->debug_info.symbolic_header.iextMax)))
1692
7.05k
      rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1693
57.1k
  }
1694
332k
      else
1695
332k
  {
1696
332k
    const char *sec_name;
1697
332k
    asection *sec;
1698
1699
    /* r_symndx is a section key.  */
1700
332k
    switch (intern.r_symndx)
1701
332k
      {
1702
3.66k
      case RELOC_SECTION_TEXT:  sec_name = _TEXT;  break;
1703
2.78k
      case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
1704
1.05k
      case RELOC_SECTION_DATA:  sec_name = _DATA;  break;
1705
1.59k
      case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
1706
1.35k
      case RELOC_SECTION_SBSS:  sec_name = _SBSS;  break;
1707
815
      case RELOC_SECTION_BSS:   sec_name = _BSS;   break;
1708
1.03k
      case RELOC_SECTION_INIT:  sec_name = _INIT;  break;
1709
1.42k
      case RELOC_SECTION_LIT8:  sec_name = _LIT8;  break;
1710
1.02k
      case RELOC_SECTION_LIT4:  sec_name = _LIT4;  break;
1711
746
      case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
1712
392
      case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
1713
386
      case RELOC_SECTION_FINI:  sec_name = _FINI;  break;
1714
245
      case RELOC_SECTION_LITA:  sec_name = _LITA;  break;
1715
492
      case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
1716
315k
      default:
1717
315k
        sec_name = NULL;
1718
315k
        break;
1719
332k
      }
1720
1721
332k
    if (sec_name != NULL)
1722
17.0k
      {
1723
17.0k
        sec = bfd_get_section_by_name (abfd, sec_name);
1724
17.0k
        if (sec != NULL)
1725
686
    {
1726
686
      rptr->sym_ptr_ptr = &sec->symbol;
1727
686
      rptr->addend = - bfd_section_vma (sec);
1728
686
    }
1729
17.0k
      }
1730
332k
  }
1731
1732
389k
      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
389k
      (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1737
389k
    }
1738
1739
1.96k
  free (external_relocs);
1740
1741
1.96k
  section->relocation = internal_relocs;
1742
1743
1.96k
  return true;
1744
1.96k
}
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.0k
{
1754
16.0k
  unsigned int count;
1755
1756
16.0k
  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.0k
  else
1769
16.0k
    {
1770
16.0k
      arelent *tblptr;
1771
1772
16.0k
      if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1773
13.6k
  return -1;
1774
1775
2.34k
      tblptr = section->relocation;
1776
1777
392k
      for (count = 0; count < section->reloc_count; count++)
1778
389k
  *relptr++ = tblptr++;
1779
2.34k
    }
1780
1781
2.34k
  *relptr = NULL;
1782
1783
2.34k
  return section->reloc_count;
1784
16.0k
}
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
15.0k
{
1800
15.0k
  const struct ecoff_debug_swap * const debug_swap
1801
15.0k
    = &ecoff_backend (abfd)->debug_swap;
1802
15.0k
  struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1803
15.0k
  struct ecoff_find_line *line_info;
1804
1805
  /* Make sure we have the FDR's.  */
1806
15.0k
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
1807
9.84k
      || bfd_get_symcount (abfd) == 0)
1808
5.78k
    return false;
1809
1810
9.26k
  if (ecoff_data (abfd)->find_line_info == NULL)
1811
367
    {
1812
367
      size_t amt = sizeof (struct ecoff_find_line);
1813
1814
367
      ecoff_data (abfd)->find_line_info =
1815
367
    (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1816
367
      if (ecoff_data (abfd)->find_line_info == NULL)
1817
0
  return false;
1818
367
    }
1819
1820
9.26k
  if (discriminator_ptr)
1821
1.91k
    *discriminator_ptr = 0;
1822
9.26k
  line_info = ecoff_data (abfd)->find_line_info;
1823
9.26k
  return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1824
9.26k
         debug_swap, line_info, filename_ptr,
1825
9.26k
         functionname_ptr, retline_ptr);
1826
9.26k
}
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
182
{
1844
182
  int i;
1845
182
  asymbol **sym_ptr_ptr;
1846
182
  size_t c;
1847
182
  bool local;
1848
1849
  /* We only want to copy information over if both BFD's use ECOFF
1850
     format.  */
1851
182
  if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour)
1852
0
    return true;
1853
1854
  /* Copy the GP value and the register masks.  */
1855
182
  ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1856
182
  ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1857
182
  ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1858
728
  for (i = 0; i < 3; i++)
1859
546
    ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1860
1861
  /* Copy the version stamp.  */
1862
182
  struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1863
182
  struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1864
182
  oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1865
1866
  /* If there are no symbols, don't copy any debugging information.  */
1867
182
  c = bfd_get_symcount (obfd);
1868
182
  sym_ptr_ptr = bfd_get_outsymbols (obfd);
1869
182
  if (c == 0 || sym_ptr_ptr == NULL)
1870
182
    return true;
1871
1872
  /* See if there are any local symbols.  */
1873
0
  local = false;
1874
0
  for (; c > 0; c--, sym_ptr_ptr++)
1875
0
    {
1876
0
      if (ecoffsymbol (*sym_ptr_ptr)->local)
1877
0
  {
1878
0
    local = true;
1879
0
    break;
1880
0
  }
1881
0
    }
1882
1883
0
  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
0
  else
1925
0
    {
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
0
      c = bfd_get_symcount (obfd);
1930
0
      sym_ptr_ptr = bfd_get_outsymbols (obfd);
1931
0
      for (; c > 0; c--, sym_ptr_ptr++)
1932
0
  {
1933
0
    EXTR esym;
1934
1935
0
    (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1936
0
      (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1937
0
    esym.ifd = ifdNil;
1938
0
    esym.asym.index = indexNil;
1939
0
    (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1940
0
      (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1941
0
  }
1942
0
    }
1943
1944
0
  return true;
1945
182
}
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.42k
{
1956
1.42k
  bfd_default_set_arch_mach (abfd, arch, machine);
1957
1.42k
  return arch == ecoff_backend (abfd)->arch;
1958
1.42k
}
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
372
{
1966
372
  asection *current;
1967
372
  int c;
1968
372
  int ret;
1969
1970
372
  c = 0;
1971
372
  for (current = abfd->sections;
1972
1.54k
       current != NULL;
1973
1.17k
       current = current->next)
1974
1.17k
    ++c;
1975
1976
372
  ret = (bfd_coff_filhsz (abfd)
1977
372
   + bfd_coff_aoutsz (abfd)
1978
372
   + c * bfd_coff_scnhsz (abfd));
1979
372
  return (int) BFD_ALIGN (ret, 16);
1980
372
}
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
28.0k
{
1991
28.0k
  return _bfd_generic_get_section_contents (abfd, section, location,
1992
28.0k
              offset, count);
1993
28.0k
}
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.88k
{
2001
1.88k
  const asection *hdr1 = *(const asection **) arg1;
2002
1.88k
  const asection *hdr2 = *(const asection **) arg2;
2003
2004
1.88k
  if ((hdr1->flags & SEC_ALLOC) != 0)
2005
1.45k
    {
2006
1.45k
      if ((hdr2->flags & SEC_ALLOC) == 0)
2007
116
  return -1;
2008
1.45k
    }
2009
428
  else
2010
428
    {
2011
428
      if ((hdr2->flags & SEC_ALLOC) != 0)
2012
242
  return 1;
2013
428
    }
2014
1.52k
  if (hdr1->vma < hdr2->vma)
2015
244
    return -1;
2016
1.27k
  else if (hdr1->vma > hdr2->vma)
2017
638
    return 1;
2018
641
  else
2019
641
    return 0;
2020
1.52k
}
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
190
{
2028
190
  file_ptr sofar, file_sofar;
2029
190
  asection **sorted_hdrs;
2030
190
  asection *current;
2031
190
  unsigned int i;
2032
190
  file_ptr old_sofar;
2033
190
  bool rdata_in_text;
2034
190
  bool first_data, first_nonalloc;
2035
190
  const bfd_vma round = ecoff_backend (abfd)->round;
2036
190
  bfd_size_type amt;
2037
2038
190
  sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
2039
190
  file_sofar = sofar;
2040
2041
  /* Sort the sections by VMA.  */
2042
190
  amt = abfd->section_count;
2043
190
  amt *= sizeof (asection *);
2044
190
  sorted_hdrs = (asection **) bfd_malloc (amt);
2045
190
  if (sorted_hdrs == NULL)
2046
0
    return false;
2047
190
  for (current = abfd->sections, i = 0;
2048
856
       current != NULL;
2049
666
       current = current->next, i++)
2050
666
    sorted_hdrs[i] = current;
2051
190
  BFD_ASSERT (i == abfd->section_count);
2052
2053
190
  qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2054
190
   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
190
  rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2059
190
  if (rdata_in_text)
2060
68
    {
2061
91
      for (i = 0; i < abfd->section_count; i++)
2062
50
  {
2063
50
    current = sorted_hdrs[i];
2064
50
    if (streq (current->name, _RDATA))
2065
0
      break;
2066
50
    if ((current->flags & SEC_CODE) == 0
2067
28
        && ! streq (current->name, _PDATA)
2068
27
        && ! streq (current->name, _RCONST))
2069
27
      {
2070
27
        rdata_in_text = false;
2071
27
        break;
2072
27
      }
2073
50
  }
2074
68
    }
2075
190
  ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2076
2077
190
  first_data = true;
2078
190
  first_nonalloc = true;
2079
856
  for (i = 0; i < abfd->section_count; i++)
2080
666
    {
2081
666
      unsigned int alignment_power;
2082
2083
666
      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
666
      if (streq (current->name, _PDATA))
2090
3
  current->line_filepos = current->size / 8;
2091
2092
666
      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
666
      if ((abfd->flags & EXEC_P) != 0
2101
34
    && (abfd->flags & D_PAGED) != 0
2102
34
    && ! 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
666
      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
666
      else if (first_nonalloc
2123
574
         && (current->flags & SEC_ALLOC) == 0
2124
46
         && (abfd->flags & D_PAGED) != 0)
2125
46
  {
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
46
    first_nonalloc = false;
2130
46
    sofar = (sofar + round - 1) &~ (round - 1);
2131
46
    file_sofar = (file_sofar + round - 1) &~ (round - 1);
2132
46
  }
2133
2134
      /* Align the sections in the file to the same boundary on
2135
   which they are aligned in virtual memory.  */
2136
666
      sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2137
666
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2138
136
  file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2139
2140
666
      if ((abfd->flags & D_PAGED) != 0
2141
666
    && (current->flags & SEC_ALLOC) != 0)
2142
528
  {
2143
528
    sofar += (current->vma - sofar) % round;
2144
528
    if ((current->flags & SEC_HAS_CONTENTS) != 0)
2145
103
      file_sofar += (current->vma - file_sofar) % round;
2146
528
  }
2147
2148
666
      if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2149
521
  current->filepos = file_sofar;
2150
2151
666
      sofar += current->size;
2152
666
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2153
136
  file_sofar += current->size;
2154
2155
      /* Make sure that this section is of the right size too.  */
2156
666
      old_sofar = sofar;
2157
666
      sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2158
666
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2159
136
  file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2160
666
      current->size += sofar - old_sofar;
2161
666
    }
2162
2163
190
  free (sorted_hdrs);
2164
190
  sorted_hdrs = NULL;
2165
2166
190
  ecoff_data (abfd)->reloc_filepos = file_sofar;
2167
2168
190
  return true;
2169
190
}
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
182
{
2178
182
  const bfd_size_type external_reloc_size =
2179
182
    ecoff_backend (abfd)->external_reloc_size;
2180
182
  file_ptr reloc_base;
2181
182
  bfd_size_type reloc_size;
2182
182
  asection *current;
2183
182
  file_ptr sym_base;
2184
2185
182
  if (! abfd->output_has_begun)
2186
178
    {
2187
178
      if (! ecoff_compute_section_file_positions (abfd))
2188
0
  abort ();
2189
178
      abfd->output_has_begun = true;
2190
178
    }
2191
2192
182
  reloc_base = ecoff_data (abfd)->reloc_filepos;
2193
2194
182
  reloc_size = 0;
2195
182
  for (current = abfd->sections;
2196
689
       current != NULL;
2197
507
       current = current->next)
2198
507
    {
2199
507
      if (current->reloc_count == 0)
2200
447
  current->rel_filepos = 0;
2201
60
      else
2202
60
  {
2203
60
    bfd_size_type relsize;
2204
2205
60
    current->rel_filepos = reloc_base;
2206
60
    relsize = current->reloc_count * external_reloc_size;
2207
60
    reloc_size += relsize;
2208
60
    reloc_base += relsize;
2209
60
  }
2210
507
    }
2211
2212
182
  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
182
  if ((abfd->flags & EXEC_P) != 0
2218
26
      && (abfd->flags & D_PAGED) != 0)
2219
26
    sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2220
26
    &~ (ecoff_backend (abfd)->round - 1));
2221
2222
182
  ecoff_data (abfd)->sym_filepos = sym_base;
2223
2224
182
  return reloc_size;
2225
182
}
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
14
{
2236
14
  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
14
  if (! abfd->output_has_begun
2241
12
      && ! ecoff_compute_section_file_positions (abfd))
2242
0
    return false;
2243
2244
14
  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
14
  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
14
  pos = section->filepos + offset;
2268
14
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
2269
13
      || bfd_write (location, count, abfd) != count)
2270
1
    return false;
2271
2272
13
  return true;
2273
14
}
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
0
{
2331
0
  ecoff_symbol_type *ecoff_sym_ptr;
2332
0
  bfd *input_bfd;
2333
2334
0
  if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2335
0
      || 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
0
  ecoff_sym_ptr = ecoffsymbol (sym);
2368
2369
0
  if (ecoff_sym_ptr->local)
2370
0
    return false;
2371
2372
0
  input_bfd = bfd_asymbol_bfd (sym);
2373
0
  (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2374
0
    (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
0
  if ((esym->asym.sc == scUndefined
2380
0
       || esym->asym.sc == scSUndefined)
2381
0
      && ! 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
0
  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
0
  return true;
2397
0
}
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
0
{
2405
0
  ecoff_set_sym_index (sym, indx);
2406
0
}
2407
2408
/* Write out an ECOFF file.  */
2409
2410
bool
2411
_bfd_ecoff_write_object_contents (bfd *abfd)
2412
182
{
2413
182
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2414
182
  const bfd_vma round = backend->round;
2415
182
  const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2416
182
  const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2417
182
  const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2418
182
  const bfd_size_type external_hdr_size
2419
182
    = backend->debug_swap.external_hdr_size;
2420
182
  const bfd_size_type external_reloc_size = backend->external_reloc_size;
2421
182
  void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
2422
182
    = backend->adjust_reloc_out;
2423
182
  void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
2424
182
    = backend->swap_reloc_out;
2425
182
  struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2426
182
  HDRR * const symhdr = &debug->symbolic_header;
2427
182
  asection *current;
2428
182
  unsigned int count;
2429
182
  bfd_size_type reloc_size;
2430
182
  bfd_size_type text_size;
2431
182
  bfd_vma text_start;
2432
182
  bool set_text_start;
2433
182
  bfd_size_type data_size;
2434
182
  bfd_vma data_start;
2435
182
  bool set_data_start;
2436
182
  bfd_size_type bss_size;
2437
182
  void * buff = NULL;
2438
182
  void * reloc_buff = NULL;
2439
182
  struct internal_filehdr internal_f;
2440
182
  struct internal_aouthdr internal_a;
2441
182
  int i;
2442
2443
  /* Determine where the sections and relocs will go in the output
2444
     file.  */
2445
182
  reloc_size = ecoff_compute_reloc_file_positions (abfd);
2446
2447
182
  count = 1;
2448
182
  for (current = abfd->sections;
2449
689
       current != NULL;
2450
507
       current = current->next)
2451
507
    {
2452
507
      current->target_index = count;
2453
507
      ++count;
2454
507
    }
2455
2456
182
  if ((abfd->flags & D_PAGED) != 0)
2457
182
    text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
2458
0
  else
2459
0
    text_size = 0;
2460
182
  text_start = 0;
2461
182
  set_text_start = false;
2462
182
  data_size = 0;
2463
182
  data_start = 0;
2464
182
  set_data_start = false;
2465
182
  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
182
  {
2472
182
    bfd_size_type siz;
2473
2474
182
    siz = scnhsz;
2475
182
    if (siz < filhsz)
2476
0
      siz = filhsz;
2477
182
    if (siz < aoutsz)
2478
182
      siz = aoutsz;
2479
182
    buff = bfd_malloc (siz);
2480
182
    if (buff == NULL)
2481
0
      goto error_return;
2482
182
  }
2483
2484
182
  internal_f.f_nscns = 0;
2485
182
  if (bfd_seek (abfd, filhsz + aoutsz, SEEK_SET) != 0)
2486
0
    goto error_return;
2487
2488
182
  for (current = abfd->sections;
2489
689
       current != NULL;
2490
507
       current = current->next)
2491
507
    {
2492
507
      struct internal_scnhdr section;
2493
507
      bfd_vma vma;
2494
2495
507
      ++internal_f.f_nscns;
2496
2497
507
      strncpy (section.s_name, current->name, sizeof section.s_name);
2498
2499
      /* This seems to be correct for Irix 4 shared libraries.  */
2500
507
      vma = bfd_section_vma (current);
2501
507
      if (streq (current->name, _LIB))
2502
0
  section.s_vaddr = 0;
2503
507
      else
2504
507
  section.s_vaddr = vma;
2505
2506
507
      section.s_paddr = current->lma;
2507
507
      section.s_size = current->size;
2508
2509
      /* If this section is unloadable then the scnptr will be 0.  */
2510
507
      if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2511
126
  section.s_scnptr = 0;
2512
381
      else
2513
381
  section.s_scnptr = current->filepos;
2514
507
      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
507
      if (! streq (current->name, _PDATA))
2524
504
  section.s_lnnoptr = 0;
2525
3
      else
2526
3
  {
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
3
    section.s_lnnoptr = current->line_filepos;
2532
3
  }
2533
2534
507
      section.s_nreloc = current->reloc_count;
2535
507
      section.s_nlnno = 0;
2536
507
      section.s_flags = ecoff_sec_to_styp_flags (current->name,
2537
507
             current->flags);
2538
2539
507
      if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff, current) == 0
2540
507
    || bfd_write (buff, scnhsz, abfd) != scnhsz)
2541
0
  goto error_return;
2542
2543
507
      if ((section.s_flags & STYP_TEXT) != 0
2544
317
    || ((section.s_flags & STYP_RDATA) != 0
2545
0
        && ecoff_data (abfd)->rdata_in_text)
2546
317
    || section.s_flags == STYP_PDATA
2547
314
    || (section.s_flags & STYP_DYNAMIC) != 0
2548
314
    || (section.s_flags & STYP_LIBLIST) != 0
2549
314
    || (section.s_flags & STYP_RELDYN) != 0
2550
314
    || section.s_flags == STYP_CONFLIC
2551
314
    || (section.s_flags & STYP_DYNSTR) != 0
2552
314
    || (section.s_flags & STYP_DYNSYM) != 0
2553
314
    || (section.s_flags & STYP_HASH) != 0
2554
314
    || (section.s_flags & STYP_ECOFF_INIT) != 0
2555
314
    || (section.s_flags & STYP_ECOFF_FINI) != 0
2556
314
    || section.s_flags == STYP_RCONST)
2557
193
  {
2558
193
    text_size += current->size;
2559
193
    if (! set_text_start || text_start > vma)
2560
111
      {
2561
111
        text_start = vma;
2562
111
        set_text_start = true;
2563
111
      }
2564
193
  }
2565
314
      else if ((section.s_flags & STYP_RDATA) != 0
2566
314
         || (section.s_flags & STYP_DATA) != 0
2567
242
         || (section.s_flags & STYP_LITA) != 0
2568
242
         || (section.s_flags & STYP_LIT8) != 0
2569
242
         || (section.s_flags & STYP_LIT4) != 0
2570
242
         || (section.s_flags & STYP_SDATA) != 0
2571
242
         || section.s_flags == STYP_XDATA
2572
242
         || (section.s_flags & STYP_GOT) != 0)
2573
72
  {
2574
72
    data_size += current->size;
2575
72
    if (! set_data_start || data_start > vma)
2576
22
      {
2577
22
        data_start = vma;
2578
22
        set_data_start = true;
2579
22
      }
2580
72
  }
2581
242
      else if ((section.s_flags & STYP_BSS) != 0
2582
206
         || (section.s_flags & STYP_SBSS) != 0)
2583
36
  bss_size += current->size;
2584
206
      else if (section.s_flags == 0
2585
0
         || (section.s_flags & STYP_ECOFF_LIB) != 0
2586
0
         || section.s_flags == STYP_COMMENT)
2587
206
  /* Do nothing.  */ ;
2588
0
      else
2589
0
  abort ();
2590
507
    }
2591
2592
  /* Set up the file header.  */
2593
182
  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
182
  internal_f.f_timdat = 0;
2601
2602
182
  if (bfd_get_symcount (abfd) != 0)
2603
0
    {
2604
      /* The ECOFF f_nsyms field is not actually the number of
2605
   symbols, it's the size of symbolic information header.  */
2606
0
      internal_f.f_nsyms = external_hdr_size;
2607
0
      internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2608
0
    }
2609
182
  else
2610
182
    {
2611
182
      internal_f.f_nsyms = 0;
2612
182
      internal_f.f_symptr = 0;
2613
182
    }
2614
2615
182
  internal_f.f_opthdr = aoutsz;
2616
2617
182
  internal_f.f_flags = F_LNNO;
2618
182
  if (reloc_size == 0)
2619
123
    internal_f.f_flags |= F_RELFLG;
2620
182
  if (bfd_get_symcount (abfd) == 0)
2621
182
    internal_f.f_flags |= F_LSYMS;
2622
182
  if (abfd->flags & EXEC_P)
2623
26
    internal_f.f_flags |= F_EXEC;
2624
2625
182
  if (bfd_little_endian (abfd))
2626
168
    internal_f.f_flags |= F_AR32WR;
2627
14
  else
2628
14
    internal_f.f_flags |= F_AR32W;
2629
2630
  /* Set up the ``optional'' header.  */
2631
182
  if ((abfd->flags & D_PAGED) != 0)
2632
182
    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
182
  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
182
  if ((abfd->flags & D_PAGED) != 0)
2642
182
    {
2643
182
      internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2644
182
      internal_a.text_start = text_start &~ (round - 1);
2645
182
      internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2646
182
      internal_a.data_start = data_start &~ (round - 1);
2647
182
    }
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
182
  if (bss_size < internal_a.dsize - data_size)
2662
9
    bss_size = 0;
2663
173
  else
2664
173
    bss_size -= internal_a.dsize - data_size;
2665
182
  internal_a.bsize = bss_size;
2666
182
  internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2667
2668
182
  internal_a.entry = bfd_get_start_address (abfd);
2669
2670
182
  internal_a.gp_value = ecoff_data (abfd)->gp;
2671
2672
182
  internal_a.gprmask = ecoff_data (abfd)->gprmask;
2673
182
  internal_a.fprmask = ecoff_data (abfd)->fprmask;
2674
910
  for (i = 0; i < 4; i++)
2675
728
    internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2676
2677
  /* Let the backend adjust the headers if necessary.  */
2678
182
  if (backend->adjust_headers)
2679
67
    {
2680
67
      if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2681
0
  goto error_return;
2682
67
    }
2683
2684
  /* Write out the file header and the optional header.  */
2685
182
  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
2686
0
    goto error_return;
2687
2688
182
  bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
2689
182
  if (bfd_write (buff, filhsz, abfd) != filhsz)
2690
0
    goto error_return;
2691
2692
182
  bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
2693
182
  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
182
  if (! ecoff_data (abfd)->linker)
2701
182
    {
2702
182
      symhdr->iextMax = 0;
2703
182
      symhdr->issExtMax = 0;
2704
182
      debug->external_ext = debug->external_ext_end = NULL;
2705
182
      debug->ssext = debug->ssext_end = NULL;
2706
182
      if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2707
182
               (abfd->flags & EXEC_P) == 0,
2708
182
               ecoff_get_extr, ecoff_set_index))
2709
0
  goto error_return;
2710
2711
      /* Write out the relocs.  */
2712
182
      for (current = abfd->sections;
2713
689
     current != NULL;
2714
507
     current = current->next)
2715
507
  {
2716
507
    arelent **reloc_ptr_ptr;
2717
507
    arelent **reloc_end;
2718
507
    char *out_ptr;
2719
507
    bfd_size_type amt;
2720
2721
507
    if (current->reloc_count == 0)
2722
447
      continue;
2723
2724
60
    amt = current->reloc_count * external_reloc_size;
2725
60
    reloc_buff = bfd_zalloc (abfd, amt);
2726
60
    if (reloc_buff == NULL)
2727
0
      goto error_return;
2728
2729
60
    reloc_ptr_ptr = current->orelocation;
2730
60
    reloc_end = reloc_ptr_ptr + current->reloc_count;
2731
60
    out_ptr = (char *) reloc_buff;
2732
2733
60
    for (;
2734
3.98k
         reloc_ptr_ptr < reloc_end;
2735
3.92k
         reloc_ptr_ptr++, out_ptr += external_reloc_size)
2736
3.92k
      {
2737
3.92k
        arelent *reloc;
2738
3.92k
        asymbol *sym;
2739
3.92k
        struct internal_reloc in;
2740
2741
3.92k
        memset ((void *) &in, 0, sizeof in);
2742
2743
3.92k
        reloc = *reloc_ptr_ptr;
2744
3.92k
        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.92k
        if (reloc->howto == NULL)
2749
947
    continue;
2750
2751
2.97k
        in.r_vaddr = reloc->address + bfd_section_vma (current);
2752
2.97k
        in.r_type = reloc->howto->type;
2753
2754
2.97k
        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.97k
        else
2760
2.97k
    {
2761
2.97k
      const char *name;
2762
2.97k
      unsigned int j;
2763
2.97k
      static struct
2764
2.97k
      {
2765
2.97k
        const char * name;
2766
2.97k
        long r_symndx;
2767
2.97k
      }
2768
2.97k
      section_symndx [] =
2769
2.97k
      {
2770
2.97k
        { _TEXT,   RELOC_SECTION_TEXT   },
2771
2.97k
        { _RDATA,  RELOC_SECTION_RDATA  },
2772
2.97k
        { _DATA,   RELOC_SECTION_DATA   },
2773
2.97k
        { _SDATA,  RELOC_SECTION_SDATA  },
2774
2.97k
        { _SBSS,   RELOC_SECTION_SBSS   },
2775
2.97k
        { _BSS,    RELOC_SECTION_BSS    },
2776
2.97k
        { _INIT,   RELOC_SECTION_INIT   },
2777
2.97k
        { _LIT8,   RELOC_SECTION_LIT8   },
2778
2.97k
        { _LIT4,   RELOC_SECTION_LIT4   },
2779
2.97k
        { _XDATA,  RELOC_SECTION_XDATA  },
2780
2.97k
        { _PDATA,  RELOC_SECTION_PDATA  },
2781
2.97k
        { _FINI,   RELOC_SECTION_FINI   },
2782
2.97k
        { _LITA,   RELOC_SECTION_LITA   },
2783
2.97k
        { "*ABS*", RELOC_SECTION_ABS    },
2784
2.97k
        { _RCONST, RELOC_SECTION_RCONST }
2785
2.97k
      };
2786
2787
2.97k
      name = bfd_section_name (bfd_asymbol_section (sym));
2788
2789
41.6k
      for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
2790
41.6k
        if (streq (name, section_symndx[j].name))
2791
2.97k
          {
2792
2.97k
      in.r_symndx = section_symndx[j].r_symndx;
2793
2.97k
      break;
2794
2.97k
          }
2795
2796
2.97k
      if (j == ARRAY_SIZE (section_symndx))
2797
0
        abort ();
2798
2.97k
      in.r_extern = 0;
2799
2.97k
    }
2800
2801
2.97k
        (*adjust_reloc_out) (abfd, reloc, &in);
2802
2803
2.97k
        (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
2804
2.97k
      }
2805
2806
60
    if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2807
0
      goto error_return;
2808
60
    amt = current->reloc_count * external_reloc_size;
2809
60
    if (bfd_write (reloc_buff, amt, abfd) != amt)
2810
0
      goto error_return;
2811
60
    bfd_release (abfd, reloc_buff);
2812
60
    reloc_buff = NULL;
2813
60
  }
2814
2815
      /* Write out the symbolic debugging information.  */
2816
182
      if (bfd_get_symcount (abfd) > 0)
2817
0
  {
2818
    /* Write out the debugging information.  */
2819
0
    if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2820
0
               ecoff_data (abfd)->sym_filepos))
2821
0
      goto error_return;
2822
0
  }
2823
182
    }
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
182
  if (bfd_get_symcount (abfd) == 0
2830
182
      && (abfd->flags & EXEC_P) != 0
2831
26
      && (abfd->flags & D_PAGED) != 0)
2832
26
    {
2833
26
      char c;
2834
2835
26
      if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2836
0
  goto error_return;
2837
26
      if (bfd_read (&c, 1, abfd) == 0)
2838
26
  c = 0;
2839
26
      if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2840
0
  goto error_return;
2841
26
      if (bfd_write (&c, 1, abfd) != 1)
2842
0
  goto error_return;
2843
26
    }
2844
2845
182
  if (reloc_buff != NULL)
2846
0
    bfd_release (abfd, reloc_buff);
2847
182
  free (buff);
2848
182
  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
182
}
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
10.0k
#define ARMAP_BIG_ENDIAN    'B'
2887
2.78k
#define ARMAP_LITTLE_ENDIAN   'L'
2888
338k
#define ARMAP_MARKER      'E'
2889
166k
#define ARMAP_START_LENGTH    10
2890
2.75k
#define ARMAP_HEADER_MARKER_INDEX 10
2891
4.98k
#define ARMAP_HEADER_ENDIAN_INDEX 11
2892
1.91k
#define ARMAP_OBJECT_MARKER_INDEX 12
2893
4.08k
#define ARMAP_OBJECT_ENDIAN_INDEX 13
2894
1.71k
#define ARMAP_END_INDEX     14
2895
3.43k
#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
187k
{
2928
187k
  char nextname[17];
2929
187k
  unsigned int i;
2930
187k
  struct areltdata *mapdata;
2931
187k
  bfd_size_type parsed_size, stringsize;
2932
187k
  char *raw_armap;
2933
187k
  struct artdata *ardata;
2934
187k
  unsigned int count;
2935
187k
  char *raw_ptr;
2936
187k
  carsym *symdef_ptr;
2937
187k
  char *stringbase;
2938
187k
  bfd_size_type amt;
2939
2940
187k
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
2941
2942
  /* Get the name of the first element.  */
2943
187k
  i = bfd_read (nextname, 16, abfd);
2944
187k
  if (i == 0)
2945
0
    return true;
2946
187k
  if (i != 16)
2947
128
    return false;
2948
2949
187k
  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
187k
  if (startswith (nextname, "/               "))
2958
20.8k
    return bfd_slurp_armap (abfd);
2959
2960
  /* See if the first element is an armap.  */
2961
166k
  if (strncmp (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH) != 0
2962
2.75k
      || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2963
1.97k
      || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2964
1.41k
    && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2965
1.91k
      || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2966
1.77k
      || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2967
1.36k
    && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2968
1.71k
      || strncmp (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1) != 0)
2969
165k
    {
2970
165k
      abfd->has_armap = false;
2971
165k
      return true;
2972
165k
    }
2973
2974
  /* Make sure we have the right byte ordering.  */
2975
1.59k
  if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2976
1.59k
       ^ (bfd_header_big_endian (abfd)))
2977
945
      || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2978
945
    ^ (bfd_big_endian (abfd))))
2979
789
    {
2980
789
      bfd_set_error (bfd_error_wrong_format);
2981
789
      return false;
2982
789
    }
2983
2984
  /* Read in the armap.  */
2985
802
  ardata = bfd_ardata (abfd);
2986
802
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2987
802
  if (mapdata == NULL)
2988
52
    return false;
2989
750
  parsed_size = mapdata->parsed_size;
2990
750
  free (mapdata);
2991
2992
750
  if (parsed_size + 1 < 9)
2993
9
    {
2994
9
      bfd_set_error (bfd_error_malformed_archive);
2995
9
      return false;
2996
9
    }
2997
2998
741
  raw_armap = (char *) _bfd_alloc_and_read (abfd, parsed_size + 1, parsed_size);
2999
741
  if (raw_armap == NULL)
3000
308
    return false;
3001
433
  raw_armap[parsed_size] = 0;
3002
3003
433
  ardata->tdata = (void *) raw_armap;
3004
3005
433
  count = H_GET_32 (abfd, raw_armap);
3006
433
  if ((parsed_size - 8) / 8 < count)
3007
43
    goto error_malformed;
3008
3009
390
  ardata->symdef_count = 0;
3010
390
  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
390
  stringbase = raw_armap + count * 8 + 8;
3015
390
  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
390
  raw_ptr = raw_armap + 4;
3055
4.72k
  for (i = 0; i < count; i++, raw_ptr += 8)
3056
4.33k
    if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
3057
1.72k
      ++ardata->symdef_count;
3058
3059
390
  amt = ardata->symdef_count;
3060
390
  amt *= sizeof (carsym);
3061
390
  symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
3062
390
  if (!symdef_ptr)
3063
0
    goto error_exit;
3064
3065
390
  ardata->symdefs = symdef_ptr;
3066
3067
390
  raw_ptr = raw_armap + 4;
3068
2.75k
  for (i = 0; i < count; i++, raw_ptr += 8)
3069
2.49k
    {
3070
2.49k
      unsigned int name_offset, file_offset;
3071
3072
2.49k
      file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3073
2.49k
      if (file_offset == 0)
3074
1.80k
  continue;
3075
684
      name_offset = H_GET_32 (abfd, raw_ptr);
3076
684
      if (name_offset > stringsize)
3077
125
  goto error_malformed;
3078
559
      symdef_ptr->name = stringbase + name_offset;
3079
559
      symdef_ptr->u.file_offset = file_offset;
3080
559
      ++symdef_ptr;
3081
559
    }
3082
3083
265
  ardata->first_file.file_offset = bfd_tell (abfd);
3084
  /* Pad to an even boundary.  */
3085
265
  ardata->first_file.file_offset += ardata->first_file.file_offset % 2;
3086
265
  abfd->has_armap = true;
3087
265
  return true;
3088
3089
168
 error_malformed:
3090
168
  bfd_set_error (bfd_error_malformed_archive);
3091
168
 error_exit:
3092
168
  ardata->symdef_count = 0;
3093
168
  ardata->symdefs = NULL;
3094
168
  ardata->tdata = NULL;
3095
168
  bfd_release (abfd, raw_armap);
3096
168
  return false;
3097
168
}
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
4
{
3108
4
  unsigned int hashsize, hashlog;
3109
4
  bfd_size_type symdefsize;
3110
4
  int padit;
3111
4
  unsigned int stringsize;
3112
4
  unsigned int mapsize;
3113
4
  file_ptr firstreal;
3114
4
  struct ar_hdr hdr;
3115
4
  struct stat statbuf;
3116
4
  unsigned int i;
3117
4
  bfd_byte temp[4];
3118
4
  bfd_byte *hashtable;
3119
4
  bfd *current;
3120
4
  bfd *last_elt;
3121
4
  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
4
  for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3126
0
    ;
3127
4
  hashsize = 1 << hashlog;
3128
3129
4
  symdefsize = hashsize * 8;
3130
4
  padit = stridx % 2;
3131
4
  stringsize = stridx + padit;
3132
3133
  /* Include 8 bytes to store symdefsize and stringsize in output.  */
3134
4
  mapsize = symdefsize + stringsize + 8;
3135
3136
4
  firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3137
3138
4
  memset ((void *) &hdr, 0, sizeof hdr);
3139
3140
  /* Work out the ECOFF armap name.  */
3141
4
  strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3142
4
  hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3143
4
  hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3144
4
    (bfd_header_big_endian (abfd)
3145
4
     ? ARMAP_BIG_ENDIAN
3146
4
     : ARMAP_LITTLE_ENDIAN);
3147
4
  hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3148
4
  hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3149
4
    bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3150
4
  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
4
  if (stat (bfd_get_filename (abfd), &statbuf) == 0)
3158
4
    _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
3159
4
          (long) (statbuf.st_mtime + ARMAP_TIME_OFFSET));
3160
3161
  /* The DECstation uses zeroes for the uid, gid and mode of the
3162
     armap.  */
3163
4
  hdr.ar_uid[0] = '0';
3164
4
  hdr.ar_gid[0] = '0';
3165
  /* Building gcc ends up extracting the armap as a file - twice.  */
3166
4
  hdr.ar_mode[0] = '6';
3167
4
  hdr.ar_mode[1] = '4';
3168
4
  hdr.ar_mode[2] = '4';
3169
3170
4
  _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
3171
3172
4
  hdr.ar_fmag[0] = '`';
3173
4
  hdr.ar_fmag[1] = '\012';
3174
3175
  /* Turn all null bytes in the header into spaces.  */
3176
244
  for (i = 0; i < sizeof (struct ar_hdr); i++)
3177
240
   if (((char *) (&hdr))[i] == '\0')
3178
60
     (((char *) (&hdr))[i]) = ' ';
3179
3180
4
  if (bfd_write (&hdr, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
3181
0
    return false;
3182
3183
4
  H_PUT_32 (abfd, hashsize, temp);
3184
4
  if (bfd_write (temp, 4, abfd) != 4)
3185
0
    return false;
3186
3187
4
  hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3188
4
  if (!hashtable)
3189
0
    return false;
3190
3191
4
  current = abfd->archive_head;
3192
4
  last_elt = current;
3193
4
  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
4
  if (bfd_write (hashtable, symdefsize, abfd) != symdefsize)
3236
0
    return false;
3237
3238
4
  bfd_release (abfd, hashtable);
3239
3240
  /* Now write the strings.  */
3241
4
  H_PUT_32 (abfd, stringsize, temp);
3242
4
  if (bfd_write (temp, 4, abfd) != 4)
3243
0
    return false;
3244
4
  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
4
  if (padit)
3256
0
    {
3257
0
      if (bfd_write ("", 1, abfd) != 1)
3258
0
  return false;
3259
0
    }
3260
3261
4
  return true;
3262
4
}
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
}