Coverage Report

Created: 2026-07-25 10:20

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