Coverage Report

Created: 2026-05-11 07:54

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