Coverage Report

Created: 2026-03-10 08:46

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