Coverage Report

Created: 2023-06-29 07:13

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