Coverage Report

Created: 2026-10-02 09:53

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.42M
#define streq(a, b) (strcmp ((a), (b)) == 0)
49
50

51
/* This stuff is somewhat copied from coffcode.h.  */
52
static asection bfd_debug_section =
53
  BFD_FAKE_SECTION (bfd_debug_section, NULL, "*DEBUG*", 0, 0);
54
55
/* Create an ECOFF object.  */
56
57
bool
58
_bfd_ecoff_mkobject (bfd *abfd)
59
6.79k
{
60
6.79k
  size_t amt = sizeof (ecoff_data_type);
61
62
6.79k
  abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
63
6.79k
  if (abfd->tdata.ecoff_obj_data == NULL)
64
0
    return false;
65
66
6.79k
  return true;
67
6.79k
}
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
6.41k
{
75
6.41k
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
76
6.41k
  struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
77
6.41k
  ecoff_data_type *ecoff;
78
79
6.41k
  if (! _bfd_ecoff_mkobject (abfd))
80
0
    return NULL;
81
82
6.41k
  ecoff = ecoff_data (abfd);
83
6.41k
  ecoff->gp_size = 8;
84
6.41k
  ecoff->sym_filepos = internal_f->f_symptr;
85
86
6.41k
  if (internal_a != NULL)
87
1.61k
    {
88
1.61k
      int i;
89
90
1.61k
      ecoff->text_start = internal_a->text_start;
91
1.61k
      ecoff->text_end = internal_a->text_start + internal_a->tsize;
92
1.61k
      ecoff->gp = internal_a->gp_value;
93
1.61k
      ecoff->gprmask = internal_a->gprmask;
94
8.09k
      for (i = 0; i < 4; i++)
95
6.47k
  ecoff->cprmask[i] = internal_a->cprmask[i];
96
1.61k
      ecoff->fprmask = internal_a->fprmask;
97
1.61k
      if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
98
34
  abfd->flags |= D_PAGED;
99
1.58k
      else
100
1.58k
  abfd->flags &=~ D_PAGED;
101
1.61k
    }
102
103
  /* It turns out that no special action is required by the MIPS or
104
     Alpha ECOFF backends.  They have different information in the
105
     a.out header, but we just copy it all (e.g., gprmask, cprmask and
106
     fprmask) and let the swapping routines ensure that only relevant
107
     information is written out.  */
108
109
6.41k
  return (void *) ecoff;
110
6.41k
}
111
112
bool
113
_bfd_ecoff_bfd_free_cached_info (bfd *abfd)
114
203k
{
115
203k
  struct ecoff_tdata *tdata;
116
117
203k
  if (bfd_get_format (abfd) == bfd_object
118
5.59k
      && (tdata = ecoff_data (abfd)) != NULL)
119
5.59k
    {
120
112k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
121
106k
  {
122
106k
    struct ecoff_section_tdata *sdata = ecoff_section_data (sec);
123
106k
    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
106k
  }
133
5.59k
      _bfd_ecoff_free_ecoff_debug_info (&tdata->debug_info);
134
5.59k
    }
135
203k
  return _bfd_generic_bfd_free_cached_info (abfd);
136
203k
}
137
138
/* Initialize a new section.  */
139
140
bool
141
_bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
142
184k
{
143
184k
  unsigned int i;
144
184k
  static struct
145
184k
  {
146
184k
    const char * name;
147
184k
    flagword flags;
148
184k
  }
149
184k
  section_flags [] =
150
184k
  {
151
184k
    { _TEXT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
152
184k
    { _INIT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
153
184k
    { _FINI,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
154
184k
    { _DATA,   SEC_ALLOC | SEC_DATA | SEC_LOAD },
155
184k
    { _SDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_SMALL_DATA },
156
184k
    { _RDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
157
184k
    { _LIT8,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
158
184k
    { _LIT4,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
159
184k
    { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
160
184k
    { _PDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
161
184k
    { _BSS,    SEC_ALLOC},
162
184k
    { _SBSS,   SEC_ALLOC | SEC_SMALL_DATA},
163
    /* An Irix 4 shared libary.  */
164
184k
    { _LIB,    SEC_COFF_SHARED_LIBRARY}
165
184k
  };
166
167
184k
  section->alignment_power = 4;
168
169
2.56M
  for (i = 0; i < ARRAY_SIZE (section_flags); i++)
170
2.38M
    if (streq (section->name, section_flags[i].name))
171
2.04k
      {
172
2.04k
  section->flags |= section_flags[i].flags;
173
2.04k
  break;
174
2.04k
      }
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
184k
  return _bfd_generic_new_section_hook (abfd, section);
182
184k
}
183
184
void
185
_bfd_ecoff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
186
             asection *section ATTRIBUTE_UNUSED,
187
             void *scnhdr ATTRIBUTE_UNUSED)
188
181k
{
189
181k
}
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
6.24k
{
199
6.24k
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
200
6.24k
  enum bfd_architecture arch;
201
6.24k
  unsigned long mach;
202
203
6.24k
  switch (internal_f->f_magic)
204
6.24k
    {
205
1.34k
    case MIPS_MAGIC_1:
206
1.42k
    case MIPS_MAGIC_LITTLE:
207
1.49k
    case MIPS_MAGIC_BIG:
208
1.49k
      arch = bfd_arch_mips;
209
1.49k
      mach = bfd_mach_mips3000;
210
1.49k
      break;
211
212
1.99k
    case MIPS_MAGIC_LITTLE2:
213
2.01k
    case MIPS_MAGIC_BIG2:
214
      /* MIPS ISA level 2: the r6000.  */
215
2.01k
      arch = bfd_arch_mips;
216
2.01k
      mach = bfd_mach_mips6000;
217
2.01k
      break;
218
219
290
    case MIPS_MAGIC_LITTLE3:
220
348
    case MIPS_MAGIC_BIG3:
221
      /* MIPS ISA level 3: the r4000.  */
222
348
      arch = bfd_arch_mips;
223
348
      mach = bfd_mach_mips4000;
224
348
      break;
225
226
2.37k
    case ALPHA_MAGIC:
227
2.37k
      arch = bfd_arch_alpha;
228
2.37k
      mach = 0;
229
2.37k
      break;
230
231
9
    default:
232
9
      arch = bfd_arch_obscure;
233
9
      mach = 0;
234
9
      break;
235
6.24k
    }
236
237
6.24k
  return bfd_default_set_arch_mach (abfd, arch, mach);
238
6.24k
}
239
240
bool
241
_bfd_ecoff_no_long_sections (bfd *abfd, int enable)
242
181k
{
243
181k
  (void) abfd;
244
181k
  (void) enable;
245
181k
  return false;
246
181k
}
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
60
{
254
60
  int big, little;
255
256
60
  switch (bfd_get_arch (abfd))
257
60
    {
258
49
    case bfd_arch_mips:
259
49
      switch (bfd_get_mach (abfd))
260
49
  {
261
0
  default:
262
0
  case 0:
263
30
  case bfd_mach_mips3000:
264
30
    big = MIPS_MAGIC_BIG;
265
30
    little = MIPS_MAGIC_LITTLE;
266
30
    break;
267
268
18
  case bfd_mach_mips6000:
269
18
    big = MIPS_MAGIC_BIG2;
270
18
    little = MIPS_MAGIC_LITTLE2;
271
18
    break;
272
273
1
  case bfd_mach_mips4000:
274
1
    big = MIPS_MAGIC_BIG3;
275
1
    little = MIPS_MAGIC_LITTLE3;
276
1
    break;
277
49
  }
278
279
49
      return bfd_big_endian (abfd) ? big : little;
280
281
11
    case bfd_arch_alpha:
282
11
      return ALPHA_MAGIC;
283
284
0
    default:
285
0
      abort ();
286
0
      return 0;
287
60
    }
288
60
}
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
265
{
295
265
  unsigned int i;
296
265
  static struct
297
265
  {
298
265
    const char * name;
299
265
    long flags;
300
265
  }
301
265
  styp_flags [] =
302
265
  {
303
265
    { _TEXT, STYP_TEXT  },
304
265
    { _DATA, STYP_DATA  },
305
265
    { _SDATA, STYP_SDATA  },
306
265
    { _RDATA, STYP_RDATA  },
307
265
    { _LITA, STYP_LITA  },
308
265
    { _LIT8, STYP_LIT8  },
309
265
    { _LIT4, STYP_LIT4  },
310
265
    { _BSS, STYP_BSS  },
311
265
    { _SBSS, STYP_SBSS  },
312
265
    { _INIT, STYP_ECOFF_INIT },
313
265
    { _FINI, STYP_ECOFF_FINI },
314
265
    { _PDATA, STYP_PDATA  },
315
265
    { _XDATA, STYP_XDATA  },
316
265
    { _LIB, STYP_ECOFF_LIB  },
317
265
    { _GOT, STYP_GOT  },
318
265
    { _HASH, STYP_HASH  },
319
265
    { _DYNAMIC, STYP_DYNAMIC  },
320
265
    { _LIBLIST, STYP_LIBLIST  },
321
265
    { _RELDYN, STYP_RELDYN  },
322
265
    { _CONFLIC, STYP_CONFLIC  },
323
265
    { _DYNSTR, STYP_DYNSTR  },
324
265
    { _DYNSYM, STYP_DYNSYM  },
325
265
    { _RCONST, STYP_RCONST  }
326
265
  };
327
265
  long styp = 0;
328
329
6.36k
  for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
330
6.09k
    if (streq (name, styp_flags[i].name))
331
0
      {
332
0
  styp = styp_flags[i].flags;
333
0
  break;
334
0
      }
335
336
265
  if (styp == 0)
337
265
    {
338
265
      if (streq (name, _COMMENT))
339
0
  {
340
0
    styp = STYP_COMMENT;
341
0
    flags &=~ SEC_NEVER_LOAD;
342
0
  }
343
265
      else if (flags & SEC_CODE)
344
112
  styp = STYP_TEXT;
345
153
      else if (flags & SEC_DATA)
346
35
  styp = STYP_DATA;
347
118
      else if (flags & SEC_READONLY)
348
0
  styp = STYP_RDATA;
349
118
      else if (flags & SEC_LOAD)
350
90
  styp = STYP_REG;
351
28
      else
352
28
  styp = STYP_BSS;
353
265
    }
354
355
265
  if (flags & SEC_NEVER_LOAD)
356
79
    styp |= STYP_NOLOAD;
357
358
265
  return styp;
359
265
}
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
181k
{
370
181k
  struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
371
181k
  long styp_flags = internal_s->s_flags;
372
181k
  flagword sec_flags = 0;
373
374
181k
  if (styp_flags & STYP_NOLOAD)
375
45.7k
    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
181k
  if ((styp_flags & STYP_TEXT)
380
142k
      || (styp_flags & STYP_ECOFF_INIT)
381
126k
      || (styp_flags & STYP_ECOFF_FINI)
382
112k
      || (styp_flags & STYP_DYNAMIC)
383
103k
      || (styp_flags & STYP_LIBLIST)
384
94.7k
      || (styp_flags & STYP_RELDYN)
385
91.6k
      || styp_flags == STYP_CONFLIC
386
89.9k
      || (styp_flags & STYP_DYNSTR)
387
85.6k
      || (styp_flags & STYP_DYNSYM)
388
82.1k
      || (styp_flags & STYP_HASH))
389
104k
    {
390
104k
      if (sec_flags & SEC_NEVER_LOAD)
391
39.4k
  sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
392
65.4k
      else
393
65.4k
  sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
394
104k
    }
395
76.5k
  else if ((styp_flags & STYP_DATA)
396
72.8k
     || (styp_flags & STYP_RDATA)
397
68.8k
     || (styp_flags & STYP_SDATA)
398
65.2k
     || styp_flags == STYP_PDATA
399
64.9k
     || styp_flags == STYP_XDATA
400
64.7k
     || (styp_flags & STYP_GOT)
401
61.4k
     || styp_flags == STYP_RCONST)
402
15.1k
    {
403
15.1k
      if (sec_flags & SEC_NEVER_LOAD)
404
2.74k
  sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
405
12.4k
      else
406
12.4k
  sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
407
15.1k
      if ((styp_flags & STYP_RDATA)
408
10.3k
    || styp_flags == STYP_PDATA
409
10.0k
    || styp_flags == STYP_RCONST)
410
5.30k
  sec_flags |= SEC_READONLY;
411
15.1k
      if (styp_flags & STYP_SDATA)
412
4.28k
  sec_flags |= SEC_SMALL_DATA;
413
15.1k
    }
414
61.3k
  else if (styp_flags & STYP_SBSS)
415
1.03k
    sec_flags |= SEC_ALLOC | SEC_SMALL_DATA;
416
60.2k
  else if (styp_flags & STYP_BSS)
417
1.23k
    sec_flags |= SEC_ALLOC;
418
59.0k
  else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
419
910
    sec_flags |= SEC_NEVER_LOAD;
420
58.1k
  else if ((styp_flags & STYP_LITA)
421
55.2k
     || (styp_flags & STYP_LIT8)
422
52.4k
     || (styp_flags & STYP_LIT4))
423
8.26k
    sec_flags |= SEC_DATA |SEC_SMALL_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
424
49.8k
  else if (styp_flags & STYP_ECOFF_LIB)
425
922
    sec_flags |= SEC_COFF_SHARED_LIBRARY;
426
48.9k
  else
427
48.9k
    sec_flags |= SEC_ALLOC | SEC_LOAD;
428
429
181k
  * flags_ptr = sec_flags;
430
181k
  return true;
431
181k
}
432

433
/* Read in the symbolic header for an ECOFF object file.  */
434
435
static bool
436
ecoff_slurp_symbolic_header (bfd *abfd)
437
11.7k
{
438
11.7k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
439
11.7k
  bfd_size_type external_hdr_size;
440
11.7k
  void * raw = NULL;
441
11.7k
  HDRR *internal_symhdr;
442
443
  /* See if we've already read it in.  */
444
11.7k
  if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
445
11.7k
      backend->debug_swap.sym_magic)
446
7.27k
    return true;
447
448
  /* See whether there is a symbolic header.  */
449
4.49k
  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
4.49k
  external_hdr_size = backend->debug_swap.external_hdr_size;
460
4.49k
  if (bfd_get_symcount (abfd) != external_hdr_size)
461
948
    {
462
948
      bfd_set_error (bfd_error_bad_value);
463
948
      return false;
464
948
    }
465
466
  /* Read the symbolic information header.  */
467
3.54k
  if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0)
468
185
    goto error_return;
469
3.36k
  raw = _bfd_malloc_and_read (abfd, external_hdr_size, external_hdr_size);
470
3.36k
  if (raw == NULL)
471
280
    goto error_return;
472
473
3.08k
  internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
474
3.08k
  (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
475
476
3.08k
  if (internal_symhdr->magic != backend->debug_swap.sym_magic)
477
1.21k
    {
478
1.21k
      bfd_set_error (bfd_error_bad_value);
479
1.21k
      goto error_return;
480
1.21k
    }
481
482
1.86k
#define FIX(start, count) \
483
20.5k
  if (internal_symhdr->start == 0) \
484
20.5k
    internal_symhdr->count = 0;
485
486
1.86k
  FIX (cbLineOffset, cbLine);
487
1.86k
  FIX (cbDnOffset, idnMax);
488
1.86k
  FIX (cbPdOffset, ipdMax);
489
1.86k
  FIX (cbSymOffset, isymMax);
490
1.86k
  FIX (cbOptOffset, ioptMax);
491
1.86k
  FIX (cbAuxOffset, iauxMax);
492
1.86k
  FIX (cbSsOffset, issMax);
493
1.86k
  FIX (cbSsExtOffset, issExtMax);
494
1.86k
  FIX (cbFdOffset, ifdMax);
495
1.86k
  FIX (cbRfdOffset, crfd);
496
1.86k
  FIX (cbExtOffset, iextMax);
497
1.86k
#undef FIX
498
499
  /* Now we can get the correct number of symbols.  */
500
1.86k
  abfd->symcount = internal_symhdr->isymMax + internal_symhdr->iextMax;
501
502
1.86k
  free (raw);
503
1.86k
  return true;
504
1.68k
 error_return:
505
1.68k
  free (raw);
506
1.68k
  return false;
507
3.08k
}
508
509
/* Read in and swap the important symbolic information for an ECOFF
510
   object file.  This is called by gdb via the read_debug_info entry
511
   point in the backend structure.  */
512
513
bool
514
_bfd_ecoff_slurp_symbolic_info (bfd *abfd,
515
        asection *ignore ATTRIBUTE_UNUSED,
516
        struct ecoff_debug_info *debug)
517
29.4k
{
518
29.4k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
519
29.4k
  HDRR *internal_symhdr;
520
29.4k
  bfd_size_type raw_base;
521
29.4k
  bfd_size_type raw_size;
522
29.4k
  void * raw;
523
29.4k
  bfd_size_type external_fdr_size;
524
29.4k
  char *fraw_src;
525
29.4k
  char *fraw_end;
526
29.4k
  struct fdr *fdr_ptr;
527
29.4k
  bfd_size_type raw_end;
528
29.4k
  bfd_size_type cb_end;
529
29.4k
  file_ptr pos;
530
29.4k
  size_t amt;
531
532
29.4k
  BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
533
534
  /* Check whether we've already gotten it, and whether there's any to
535
     get.  */
536
29.4k
  if (debug->alloc_syments)
537
13.8k
    return true;
538
15.6k
  if (ecoff_data (abfd)->sym_filepos == 0)
539
3.88k
    {
540
3.88k
      abfd->symcount = 0;
541
3.88k
      return true;
542
3.88k
    }
543
544
11.7k
  if (! ecoff_slurp_symbolic_header (abfd))
545
2.63k
    return false;
546
547
9.14k
  internal_symhdr = &debug->symbolic_header;
548
549
  /* Read all the symbolic information at once.  */
550
9.14k
  raw_base = (ecoff_data (abfd)->sym_filepos
551
9.14k
        + 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.14k
  raw_end = raw_base;
559
560
9.14k
#define UPDATE_RAW_END(start, count, size) \
561
74.9k
  do                  \
562
74.9k
    if (internal_symhdr->count != 0)         \
563
74.9k
      {                 \
564
48.8k
  if (internal_symhdr->start < raw_base)       \
565
48.8k
    goto err;             \
566
48.8k
  if (_bfd_mul_overflow ((unsigned long) internal_symhdr->count, \
567
46.5k
             (size), &amt))       \
568
46.5k
    goto err;             \
569
46.5k
  cb_end = internal_symhdr->start + amt;        \
570
46.5k
  if (cb_end < internal_symhdr->start)       \
571
46.5k
    goto err;             \
572
46.5k
  if (cb_end > raw_end)           \
573
44.1k
    raw_end = cb_end;           \
574
44.1k
      }                  \
575
74.9k
  while (0)
576
577
9.14k
  UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
578
8.53k
  UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
579
8.08k
  UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
580
7.55k
  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.16k
  UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
584
6.67k
  UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
585
6.34k
  UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
586
5.99k
  UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
587
5.55k
  UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
588
5.20k
  UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
589
4.73k
  UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
590
591
4.42k
#undef UPDATE_RAW_END
592
593
4.42k
  raw_size = raw_end - raw_base;
594
4.42k
  if (raw_size == 0)
595
45
    {
596
45
      ecoff_data (abfd)->sym_filepos = 0;
597
45
      return true;
598
45
    }
599
4.38k
  pos = ecoff_data (abfd)->sym_filepos;
600
4.38k
  pos += backend->debug_swap.external_hdr_size;
601
4.38k
  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
602
0
    return false;
603
4.38k
  raw = _bfd_alloc_and_read (abfd, raw_size, raw_size);
604
4.38k
  if (raw == NULL)
605
3.47k
    return false;
606
607
906
  debug->alloc_syments = true;
608
609
  /* Get pointers for the numeric offsets in the HDRR structure.  */
610
906
#define FIX(start, count, ptr, type) \
611
9.96k
  if (internal_symhdr->count == 0)         \
612
9.96k
    debug->ptr = NULL;             \
613
9.96k
  else                  \
614
9.96k
    debug->ptr = (type) ((char *) raw         \
615
4.45k
       + (internal_symhdr->start - raw_base))
616
617
906
  FIX (cbLineOffset, cbLine, line, unsigned char *);
618
906
  FIX (cbDnOffset, idnMax, external_dnr, void *);
619
906
  FIX (cbPdOffset, ipdMax, external_pdr, void *);
620
906
  FIX (cbSymOffset, isymMax, external_sym, void *);
621
906
  FIX (cbOptOffset, ioptMax, external_opt, void *);
622
906
  FIX (cbAuxOffset, iauxMax, external_aux, union aux_ext *);
623
906
  FIX (cbSsOffset, issMax, ss, char *);
624
906
  FIX (cbSsExtOffset, issExtMax, ssext, char *);
625
906
  FIX (cbFdOffset, ifdMax, external_fdr, void *);
626
906
  FIX (cbRfdOffset, crfd, external_rfd, void *);
627
906
  FIX (cbExtOffset, iextMax, external_ext, void *);
628
906
#undef FIX
629
630
  /* Ensure string sections are zero terminated.  */
631
906
  if (debug->ss)
632
105
    debug->ss[internal_symhdr->issMax - 1] = 0;
633
906
  if (debug->ssext)
634
737
    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
906
  if (_bfd_mul_overflow ((unsigned long) internal_symhdr->ifdMax,
645
906
       sizeof (struct fdr), &amt))
646
0
    {
647
4.71k
    err:
648
4.71k
      bfd_set_error (bfd_error_file_too_big);
649
4.71k
      return false;
650
0
    }
651
906
  debug->fdr = (FDR *) bfd_alloc (abfd, amt);
652
906
  if (debug->fdr == NULL)
653
0
    return false;
654
906
  external_fdr_size = backend->debug_swap.external_fdr_size;
655
906
  fdr_ptr = debug->fdr;
656
906
  fraw_src = (char *) debug->external_fdr;
657
  /* PR 17512: file: 3372-1243-0.004.  */
658
906
  if (fraw_src == NULL && internal_symhdr->ifdMax > 0)
659
0
    goto err;
660
906
  fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
661
6.86k
  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
662
5.96k
    (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
663
664
906
  return true;
665
906
}
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
186k
{
675
186k
  ecoff_symbol_type *new_symbol;
676
186k
  size_t amt = sizeof (ecoff_symbol_type);
677
678
186k
  new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
679
186k
  if (new_symbol == NULL)
680
0
    return NULL;
681
186k
  new_symbol->symbol.section = NULL;
682
186k
  new_symbol->fdr = NULL;
683
186k
  new_symbol->local = false;
684
186k
  new_symbol->native = NULL;
685
186k
  new_symbol->symbol.the_bfd = abfd;
686
186k
  return &new_symbol->symbol;
687
186k
}
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
14.4k
{
698
14.4k
  asym->the_bfd = abfd;
699
14.4k
  asym->value = ecoff_sym->value;
700
14.4k
  asym->section = &bfd_debug_section;
701
14.4k
  asym->udata.i = 0;
702
703
  /* Most symbol types are just for debugging.  */
704
14.4k
  switch (ecoff_sym->st)
705
14.4k
    {
706
324
    case stGlobal:
707
600
    case stStatic:
708
893
    case stLabel:
709
990
    case stProc:
710
1.50k
    case stStaticProc:
711
1.50k
      break;
712
11.8k
    case stNil:
713
11.8k
      if (ECOFF_IS_STAB (ecoff_sym))
714
0
  {
715
0
    asym->flags = BSF_DEBUGGING;
716
0
    return true;
717
0
  }
718
11.8k
      break;
719
11.8k
    default:
720
1.03k
      asym->flags = BSF_DEBUGGING;
721
1.03k
      return true;
722
14.4k
    }
723
724
13.3k
  if (weak)
725
469
    asym->flags = BSF_EXPORT | BSF_WEAK;
726
12.8k
  else if (ext)
727
12.2k
    asym->flags = BSF_EXPORT | BSF_GLOBAL;
728
652
  else
729
652
    {
730
652
      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
652
      if (ecoff_sym->st == stProc
738
650
    || ecoff_sym->st == stLabel
739
585
    || ECOFF_IS_STAB (ecoff_sym))
740
67
  asym->flags |= BSF_DEBUGGING;
741
652
    }
742
743
13.3k
  if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
744
609
    asym->flags |= BSF_FUNCTION;
745
746
13.3k
  switch (ecoff_sym->sc)
747
13.3k
    {
748
10.1k
    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
10.1k
      asym->flags = BSF_LOCAL;
754
10.1k
      break;
755
205
    case scText:
756
205
      asym->section = bfd_make_section_old_way (abfd, _TEXT);
757
205
      asym->value -= asym->section->vma;
758
205
      break;
759
131
    case scData:
760
131
      asym->section = bfd_make_section_old_way (abfd, _DATA);
761
131
      asym->value -= asym->section->vma;
762
131
      break;
763
104
    case scBss:
764
104
      asym->section = bfd_make_section_old_way (abfd, _BSS);
765
104
      asym->value -= asym->section->vma;
766
104
      break;
767
320
    case scRegister:
768
320
      asym->flags = BSF_DEBUGGING;
769
320
      break;
770
8
    case scAbs:
771
8
      asym->section = bfd_abs_section_ptr;
772
8
      break;
773
171
    case scUndefined:
774
171
      asym->section = bfd_und_section_ptr;
775
171
      asym->flags = 0;
776
171
      asym->value = 0;
777
171
      break;
778
8
    case scCdbLocal:
779
210
    case scBits:
780
292
    case scCdbSystem:
781
404
    case scRegImage:
782
432
    case scInfo:
783
623
    case scUserStruct:
784
623
      asym->flags = BSF_DEBUGGING;
785
623
      break;
786
30
    case scSData:
787
30
      asym->section = bfd_make_section_old_way (abfd, ".sdata");
788
30
      asym->value -= asym->section->vma;
789
30
      break;
790
33
    case scSBss:
791
33
      asym->section = bfd_make_section_old_way (abfd, ".sbss");
792
33
      asym->value -= asym->section->vma;
793
33
      break;
794
3
    case scRData:
795
3
      asym->section = bfd_make_section_old_way (abfd, ".rdata");
796
3
      asym->value -= asym->section->vma;
797
3
      break;
798
251
    case scVar:
799
251
      asym->flags = BSF_DEBUGGING;
800
251
      break;
801
39
    case scCommon:
802
39
      if (asym->value > ecoff_data (abfd)->gp_size)
803
13
  {
804
13
    asym->section = bfd_com_section_ptr;
805
13
    asym->flags = 0;
806
13
    break;
807
13
  }
808
      /* Fall through.  */
809
38
    case scSCommon:
810
38
      asym->section = &_bfd_ecoff_scom_section;
811
38
      asym->flags = 0;
812
38
      break;
813
5
    case scVarRegister:
814
166
    case scVariant:
815
166
      asym->flags = BSF_DEBUGGING;
816
166
      break;
817
424
    case scSUndefined:
818
424
      asym->section = bfd_und_section_ptr;
819
424
      asym->flags = 0;
820
424
      asym->value = 0;
821
424
      break;
822
96
    case scInit:
823
96
      asym->section = bfd_make_section_old_way (abfd, ".init");
824
96
      asym->value -= asym->section->vma;
825
96
      break;
826
42
    case scBasedVar:
827
205
    case scXData:
828
236
    case scPData:
829
236
      asym->flags = BSF_DEBUGGING;
830
236
      break;
831
94
    case scFini:
832
94
      asym->section = bfd_make_section_old_way (abfd, ".fini");
833
94
      asym->value -= asym->section->vma;
834
94
      break;
835
5
    case scRConst:
836
5
      asym->section = bfd_make_section_old_way (abfd, ".rconst");
837
5
      asym->value -= asym->section->vma;
838
5
      break;
839
257
    default:
840
257
      break;
841
13.3k
    }
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
13.3k
  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
13.3k
  return true;
863
13.3k
}
864
865
/* Read an ECOFF symbol table.  */
866
867
bool
868
_bfd_ecoff_slurp_symbol_table (bfd *abfd)
869
18.3k
{
870
18.3k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
871
18.3k
  const bfd_size_type external_ext_size
872
18.3k
    = backend->debug_swap.external_ext_size;
873
18.3k
  const bfd_size_type external_sym_size
874
18.3k
    = backend->debug_swap.external_sym_size;
875
18.3k
  void (* const swap_ext_in) (bfd *, void *, EXTR *)
876
18.3k
    = backend->debug_swap.swap_ext_in;
877
18.3k
  void (* const swap_sym_in) (bfd *, void *, SYMR *)
878
18.3k
    = backend->debug_swap.swap_sym_in;
879
18.3k
  ecoff_symbol_type *internal;
880
18.3k
  ecoff_symbol_type *internal_ptr;
881
18.3k
  char *eraw_src;
882
18.3k
  char *eraw_end;
883
18.3k
  FDR *fdr_ptr;
884
18.3k
  FDR *fdr_end;
885
18.3k
  size_t amt;
886
887
  /* If we've already read in the symbol table, do nothing.  */
888
18.3k
  if (ecoff_data (abfd)->canonical_symbols != NULL)
889
8.74k
    return true;
890
891
  /* Get the symbolic information.  */
892
9.59k
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
893
9.59k
          &ecoff_data (abfd)->debug_info))
894
3.50k
    return false;
895
6.09k
  if (bfd_get_symcount (abfd) == 0)
896
3.95k
    return true;
897
898
2.13k
  if (_bfd_mul_overflow (bfd_get_symcount (abfd),
899
2.13k
       sizeof (ecoff_symbol_type), &amt))
900
0
    {
901
0
      bfd_set_error (bfd_error_file_too_big);
902
0
      return false;
903
0
    }
904
2.13k
  internal = (ecoff_symbol_type *) bfd_alloc (abfd, amt);
905
2.13k
  if (internal == NULL)
906
0
    return false;
907
908
2.13k
  internal_ptr = internal;
909
2.13k
  eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
910
2.13k
  eraw_end = (eraw_src
911
2.13k
        + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
912
2.13k
     * external_ext_size));
913
15.8k
  for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
914
14.9k
    {
915
14.9k
      EXTR internal_esym;
916
917
14.9k
      (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
918
919
      /* PR 17512: file: 3372-1000-0.004.  */
920
14.9k
      HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
921
14.9k
      if (internal_esym.asym.iss >= symhdr->issExtMax
922
13.7k
    || internal_esym.asym.iss < 0)
923
1.28k
  {
924
1.28k
    bfd_set_error (bfd_error_bad_value);
925
1.28k
    return false;
926
1.28k
  }
927
928
13.7k
      internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
929
13.7k
           + internal_esym.asym.iss);
930
931
13.7k
      if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
932
13.7k
          &internal_ptr->symbol, 1,
933
13.7k
          internal_esym.weakext))
934
0
  return false;
935
936
      /* The alpha uses a negative ifd field for section symbols.  */
937
      /* PR 17512: file: 3372-1983-0.004.  */
938
13.7k
      if (internal_esym.ifd >= symhdr->ifdMax
939
7.88k
    || internal_esym.ifd < 0)
940
6.10k
  internal_ptr->fdr = NULL;
941
7.61k
      else
942
7.61k
  internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
943
7.61k
           + internal_esym.ifd);
944
13.7k
      internal_ptr->local = false;
945
13.7k
      internal_ptr->native = (void *) eraw_src;
946
13.7k
    }
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
854
  fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
951
854
  fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
952
3.57k
  for (; fdr_ptr < fdr_end; fdr_ptr++)
953
3.14k
    {
954
3.14k
      char *lraw_src;
955
3.14k
      char *lraw_end;
956
3.14k
      HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
957
958
3.14k
      if (fdr_ptr->csym == 0)
959
2.64k
  continue;
960
500
      if (fdr_ptr->isymBase < 0
961
500
    || fdr_ptr->isymBase > symhdr->isymMax
962
252
    || fdr_ptr->csym < 0
963
252
    || fdr_ptr->csym > symhdr->isymMax - fdr_ptr->isymBase
964
148
    || fdr_ptr->csym > ((long) bfd_get_symcount (abfd)
965
148
            - (internal_ptr - internal))
966
147
    || fdr_ptr->issBase < 0
967
147
    || fdr_ptr->issBase > symhdr->issMax)
968
363
  {
969
363
    bfd_set_error (bfd_error_bad_value);
970
363
    return false;
971
363
  }
972
137
      lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
973
137
      + fdr_ptr->isymBase * external_sym_size);
974
137
      lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
975
137
      for (;
976
827
     lraw_src < lraw_end;
977
690
     lraw_src += external_sym_size, internal_ptr++)
978
751
  {
979
751
    SYMR internal_sym;
980
981
751
    (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
982
983
751
    if (internal_sym.iss >= symhdr->issMax - fdr_ptr->issBase
984
696
        || internal_sym.iss < 0)
985
61
      {
986
61
        bfd_set_error (bfd_error_bad_value);
987
61
        return false;
988
61
      }
989
690
    internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
990
690
               + fdr_ptr->issBase
991
690
               + internal_sym.iss);
992
690
    if (!ecoff_set_symbol_info (abfd, &internal_sym,
993
690
              &internal_ptr->symbol, 0, 0))
994
0
      return false;
995
690
    internal_ptr->fdr = fdr_ptr;
996
690
    internal_ptr->local = true;
997
690
    internal_ptr->native = (void *) lraw_src;
998
690
  }
999
137
    }
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
430
  if (internal_ptr - internal < (ptrdiff_t) bfd_get_symcount (abfd))
1007
18
    {
1008
18
      abfd->symcount = internal_ptr - internal;
1009
18
      _bfd_error_handler
1010
  /* xgettext:c-format */
1011
18
  (_("%pB: warning: isymMax (%ld) is greater than ifdMax (%ld)"),
1012
18
   abfd, ecoff_data (abfd)->debug_info.symbolic_header.isymMax,
1013
18
   ecoff_data (abfd)->debug_info.symbolic_header.ifdMax);
1014
18
    }
1015
1016
430
  ecoff_data (abfd)->canonical_symbols = internal;
1017
1018
430
  return true;
1019
854
}
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
2.84k
{
1026
2.84k
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
1027
2.84k
          &ecoff_data (abfd)->debug_info))
1028
1.32k
    return -1;
1029
1030
1.51k
  if (bfd_get_symcount (abfd) == 0)
1031
296
    return 0;
1032
1033
1.22k
  return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1034
1.51k
}
1035
1036
/* Get the canonical symbols.  */
1037
1038
long
1039
_bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
1040
1.41k
{
1041
1.41k
  unsigned int counter = 0;
1042
1.41k
  ecoff_symbol_type *symbase;
1043
1.41k
  ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1044
1045
1.41k
  if (! _bfd_ecoff_slurp_symbol_table (abfd))
1046
445
    return -1;
1047
967
  if (bfd_get_symcount (abfd) == 0)
1048
200
    return 0;
1049
1050
767
  symbase = ecoff_data (abfd)->canonical_symbols;
1051
3.78k
  while (counter < bfd_get_symcount (abfd))
1052
3.02k
    {
1053
3.02k
      *(location++) = symbase++;
1054
3.02k
      counter++;
1055
3.02k
    }
1056
767
  *location++ = NULL;
1057
767
  return bfd_get_symcount (abfd);
1058
967
}
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
978
{
1426
978
  bfd_symbol_info (symbol, ret);
1427
978
}
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
17.2k
{
1642
17.2k
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1643
17.2k
  arelent *internal_relocs;
1644
17.2k
  bfd_size_type external_reloc_size;
1645
17.2k
  bfd_size_type amt;
1646
17.2k
  bfd_byte *external_relocs;
1647
17.2k
  arelent *rptr;
1648
17.2k
  unsigned int i;
1649
1650
17.2k
  if (section->relocation != NULL
1651
17.2k
      || section->reloc_count == 0
1652
16.9k
      || (section->flags & SEC_CONSTRUCTOR) != 0)
1653
292
    return true;
1654
1655
16.9k
  if (! _bfd_ecoff_slurp_symbol_table (abfd))
1656
4.76k
    return false;
1657
1658
12.1k
  external_reloc_size = backend->external_reloc_size;
1659
12.1k
  amt = external_reloc_size * section->reloc_count;
1660
12.1k
  if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1661
1.66k
    return false;
1662
10.4k
  external_relocs = _bfd_malloc_and_read (abfd, amt, amt);
1663
10.4k
  if (external_relocs == NULL)
1664
8.01k
    return false;
1665
1666
2.48k
  amt = section->reloc_count;
1667
2.48k
  amt *= sizeof (arelent);
1668
2.48k
  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1669
2.48k
  if (internal_relocs == NULL)
1670
0
    {
1671
0
      free (external_relocs);
1672
0
      return false;
1673
0
    }
1674
1675
535k
  for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1676
533k
    {
1677
533k
      struct internal_reloc intern;
1678
1679
533k
      (*backend->swap_reloc_in) (abfd,
1680
533k
         external_relocs + i * external_reloc_size,
1681
533k
         &intern);
1682
533k
      rptr->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1683
533k
      rptr->addend = 0;
1684
1685
533k
      if (intern.r_extern)
1686
86.2k
  {
1687
    /* r_symndx is an index into the external symbols.  */
1688
86.2k
    if (symbols != NULL
1689
63.3k
        && intern.r_symndx >= 0
1690
63.3k
        && (intern.r_symndx
1691
63.3k
      < (ecoff_data (abfd)->debug_info.symbolic_header.iextMax)))
1692
10.7k
      rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1693
86.2k
  }
1694
447k
      else
1695
447k
  {
1696
447k
    const char *sec_name;
1697
447k
    asection *sec;
1698
1699
    /* r_symndx is a section key.  */
1700
447k
    switch (intern.r_symndx)
1701
447k
      {
1702
5.79k
      case RELOC_SECTION_TEXT:  sec_name = _TEXT;  break;
1703
4.20k
      case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
1704
1.51k
      case RELOC_SECTION_DATA:  sec_name = _DATA;  break;
1705
2.32k
      case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
1706
1.99k
      case RELOC_SECTION_SBSS:  sec_name = _SBSS;  break;
1707
874
      case RELOC_SECTION_BSS:   sec_name = _BSS;   break;
1708
1.22k
      case RELOC_SECTION_INIT:  sec_name = _INIT;  break;
1709
1.78k
      case RELOC_SECTION_LIT8:  sec_name = _LIT8;  break;
1710
1.10k
      case RELOC_SECTION_LIT4:  sec_name = _LIT4;  break;
1711
967
      case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
1712
377
      case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
1713
534
      case RELOC_SECTION_FINI:  sec_name = _FINI;  break;
1714
438
      case RELOC_SECTION_LITA:  sec_name = _LITA;  break;
1715
631
      case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
1716
423k
      default:
1717
423k
        sec_name = NULL;
1718
423k
        break;
1719
447k
      }
1720
1721
447k
    if (sec_name != NULL)
1722
23.7k
      {
1723
23.7k
        sec = bfd_get_section_by_name (abfd, sec_name);
1724
23.7k
        if (sec != NULL)
1725
2.60k
    {
1726
2.60k
      rptr->sym_ptr_ptr = &sec->symbol;
1727
2.60k
      rptr->addend = - bfd_section_vma (sec);
1728
2.60k
    }
1729
23.7k
      }
1730
447k
  }
1731
1732
533k
      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
533k
      (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1737
533k
    }
1738
1739
2.48k
  free (external_relocs);
1740
1741
2.48k
  section->relocation = internal_relocs;
1742
1743
2.48k
  return true;
1744
2.48k
}
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
17.2k
{
1754
17.2k
  unsigned int count;
1755
1756
17.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
17.2k
  else
1769
17.2k
    {
1770
17.2k
      arelent *tblptr;
1771
1772
17.2k
      if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1773
14.4k
  return -1;
1774
1775
2.77k
      tblptr = section->relocation;
1776
1777
536k
      for (count = 0; count < section->reloc_count; count++)
1778
533k
  *relptr++ = tblptr++;
1779
2.77k
    }
1780
1781
2.77k
  *relptr = NULL;
1782
1783
2.77k
  return section->reloc_count;
1784
17.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
17.0k
{
1800
17.0k
  const struct ecoff_debug_swap * const debug_swap
1801
17.0k
    = &ecoff_backend (abfd)->debug_swap;
1802
17.0k
  struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1803
17.0k
  struct ecoff_find_line *line_info;
1804
1805
  /* Make sure we have the FDR's.  */
1806
17.0k
  if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
1807
11.0k
      || bfd_get_symcount (abfd) == 0)
1808
6.28k
    return false;
1809
1810
10.7k
  if (ecoff_data (abfd)->find_line_info == NULL)
1811
423
    {
1812
423
      size_t amt = sizeof (struct ecoff_find_line);
1813
1814
423
      ecoff_data (abfd)->find_line_info =
1815
423
    (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1816
423
      if (ecoff_data (abfd)->find_line_info == NULL)
1817
0
  return false;
1818
423
    }
1819
1820
10.7k
  if (discriminator_ptr)
1821
2.70k
    *discriminator_ptr = 0;
1822
10.7k
  line_info = ecoff_data (abfd)->find_line_info;
1823
10.7k
  return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1824
10.7k
         debug_swap, line_info, filename_ptr,
1825
10.7k
         functionname_ptr, retline_ptr);
1826
10.7k
}
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
60
{
1844
60
  int i;
1845
60
  asymbol **sym_ptr_ptr;
1846
60
  size_t c;
1847
60
  bool local;
1848
1849
  /* We only want to copy information over if both BFD's use ECOFF
1850
     format.  */
1851
60
  if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour)
1852
0
    return true;
1853
1854
  /* Copy the GP value and the register masks.  */
1855
60
  ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1856
60
  ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1857
60
  ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1858
240
  for (i = 0; i < 3; i++)
1859
180
    ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1860
1861
  /* Copy the version stamp.  */
1862
60
  struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1863
60
  struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1864
60
  oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1865
1866
  /* If there are no symbols, don't copy any debugging information.  */
1867
60
  c = bfd_get_symcount (obfd);
1868
60
  sym_ptr_ptr = bfd_get_outsymbols (obfd);
1869
60
  if (c == 0 || sym_ptr_ptr == NULL)
1870
59
    return true;
1871
1872
  /* See if there are any local symbols.  */
1873
1
  local = false;
1874
3
  for (; c > 0; c--, sym_ptr_ptr++)
1875
2
    {
1876
2
      if (ecoffsymbol (*sym_ptr_ptr)->local)
1877
0
  {
1878
0
    local = true;
1879
0
    break;
1880
0
  }
1881
2
    }
1882
1883
1
  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
1
  else
1925
1
    {
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
1
      c = bfd_get_symcount (obfd);
1930
1
      sym_ptr_ptr = bfd_get_outsymbols (obfd);
1931
3
      for (; c > 0; c--, sym_ptr_ptr++)
1932
2
  {
1933
2
    EXTR esym;
1934
1935
2
    (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1936
2
      (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1937
2
    esym.ifd = ifdNil;
1938
2
    esym.asym.index = indexNil;
1939
2
    (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1940
2
      (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1941
2
  }
1942
1
    }
1943
1944
1
  return true;
1945
60
}
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
379
{
1956
379
  bfd_default_set_arch_mach (abfd, arch, machine);
1957
379
  return arch == ecoff_backend (abfd)->arch;
1958
379
}
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
126
{
1966
126
  asection *current;
1967
126
  int c;
1968
126
  int ret;
1969
1970
126
  c = 0;
1971
126
  for (current = abfd->sections;
1972
866
       current != NULL;
1973
740
       current = current->next)
1974
740
    ++c;
1975
1976
126
  ret = (bfd_coff_filhsz (abfd)
1977
126
   + bfd_coff_aoutsz (abfd)
1978
126
   + c * bfd_coff_scnhsz (abfd));
1979
126
  return (int) BFD_ALIGN (ret, 16);
1980
126
}
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
27.3k
{
1991
27.3k
  return _bfd_generic_get_section_contents (abfd, section, location,
1992
27.3k
              offset, count);
1993
27.3k
}
1994
1995
/* Sort sections by VMA, but put SEC_ALLOC sections first.  This is
1996
   called via qsort.  */
1997
1998
static int
1999
ecoff_sort_hdrs (const void * arg1, const void * arg2)
2000
1.47k
{
2001
1.47k
  const asection *hdr1 = *(const asection **) arg1;
2002
1.47k
  const asection *hdr2 = *(const asection **) arg2;
2003
2004
1.47k
  if ((hdr1->flags & SEC_ALLOC) != 0)
2005
1.19k
    {
2006
1.19k
      if ((hdr2->flags & SEC_ALLOC) == 0)
2007
122
  return -1;
2008
1.19k
    }
2009
282
  else
2010
282
    {
2011
282
      if ((hdr2->flags & SEC_ALLOC) != 0)
2012
166
  return 1;
2013
282
    }
2014
1.18k
  if (hdr1->vma < hdr2->vma)
2015
158
    return -1;
2016
1.02k
  else if (hdr1->vma > hdr2->vma)
2017
483
    return 1;
2018
545
  else
2019
545
    return 0;
2020
1.18k
}
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
66
{
2028
66
  file_ptr sofar, file_sofar;
2029
66
  asection **sorted_hdrs;
2030
66
  asection *current;
2031
66
  unsigned int i;
2032
66
  file_ptr old_sofar;
2033
66
  bool rdata_in_text;
2034
66
  bool first_data, first_nonalloc;
2035
66
  const bfd_vma round = ecoff_backend (abfd)->round;
2036
66
  bfd_size_type amt;
2037
2038
66
  sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
2039
66
  file_sofar = sofar;
2040
2041
  /* Sort the sections by VMA.  */
2042
66
  amt = abfd->section_count;
2043
66
  amt *= sizeof (asection *);
2044
66
  sorted_hdrs = (asection **) bfd_malloc (amt);
2045
66
  if (sorted_hdrs == NULL)
2046
0
    return false;
2047
66
  for (current = abfd->sections, i = 0;
2048
541
       current != NULL;
2049
475
       current = current->next, i++)
2050
475
    sorted_hdrs[i] = current;
2051
66
  BFD_ASSERT (i == abfd->section_count);
2052
2053
66
  qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2054
66
   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
66
  rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2059
66
  if (rdata_in_text)
2060
11
    {
2061
11
      for (i = 0; i < abfd->section_count; i++)
2062
10
  {
2063
10
    current = sorted_hdrs[i];
2064
10
    if (streq (current->name, _RDATA))
2065
0
      break;
2066
10
    if ((current->flags & SEC_CODE) == 0
2067
10
        && ! streq (current->name, _PDATA)
2068
10
        && ! streq (current->name, _RCONST))
2069
10
      {
2070
10
        rdata_in_text = false;
2071
10
        break;
2072
10
      }
2073
10
  }
2074
11
    }
2075
66
  ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2076
2077
66
  first_data = true;
2078
66
  first_nonalloc = true;
2079
541
  for (i = 0; i < abfd->section_count; i++)
2080
475
    {
2081
475
      unsigned int alignment_power;
2082
2083
475
      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
475
      if (streq (current->name, _PDATA))
2090
0
  current->line_filepos = current->size / 8;
2091
2092
475
      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
475
      if ((abfd->flags & EXEC_P) != 0
2101
1
    && (abfd->flags & D_PAGED) != 0
2102
1
    && ! 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
475
      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
475
      else if (first_nonalloc
2123
410
         && (current->flags & SEC_ALLOC) == 0
2124
32
         && (abfd->flags & D_PAGED) != 0)
2125
32
  {
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
32
    first_nonalloc = false;
2130
32
    sofar = (sofar + round - 1) &~ (round - 1);
2131
32
    file_sofar = (file_sofar + round - 1) &~ (round - 1);
2132
32
  }
2133
2134
      /* Align the sections in the file to the same boundary on
2135
   which they are aligned in virtual memory.  */
2136
475
      sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2137
475
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2138
81
  file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2139
2140
475
      if ((abfd->flags & D_PAGED) != 0
2141
475
    && (current->flags & SEC_ALLOC) != 0)
2142
378
  {
2143
378
    sofar += (current->vma - sofar) % round;
2144
378
    if ((current->flags & SEC_HAS_CONTENTS) != 0)
2145
65
      file_sofar += (current->vma - file_sofar) % round;
2146
378
  }
2147
2148
475
      if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2149
363
  current->filepos = file_sofar;
2150
2151
475
      sofar += current->size;
2152
475
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2153
81
  file_sofar += current->size;
2154
2155
      /* Make sure that this section is of the right size too.  */
2156
475
      old_sofar = sofar;
2157
475
      sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2158
475
      if ((current->flags & SEC_HAS_CONTENTS) != 0)
2159
81
  file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2160
475
      current->size += sofar - old_sofar;
2161
475
    }
2162
2163
66
  free (sorted_hdrs);
2164
66
  sorted_hdrs = NULL;
2165
2166
66
  ecoff_data (abfd)->reloc_filepos = file_sofar;
2167
2168
66
  return true;
2169
66
}
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
60
{
2178
60
  const bfd_size_type external_reloc_size =
2179
60
    ecoff_backend (abfd)->external_reloc_size;
2180
60
  file_ptr reloc_base;
2181
60
  bfd_size_type reloc_size;
2182
60
  asection *current;
2183
60
  file_ptr sym_base;
2184
2185
60
  if (! abfd->output_has_begun)
2186
58
    {
2187
58
      if (! ecoff_compute_section_file_positions (abfd))
2188
0
  abort ();
2189
58
      abfd->output_has_begun = true;
2190
58
    }
2191
2192
60
  reloc_base = ecoff_data (abfd)->reloc_filepos;
2193
2194
60
  reloc_size = 0;
2195
60
  for (current = abfd->sections;
2196
325
       current != NULL;
2197
265
       current = current->next)
2198
265
    {
2199
265
      if (current->reloc_count == 0)
2200
242
  current->rel_filepos = 0;
2201
23
      else
2202
23
  {
2203
23
    bfd_size_type relsize;
2204
2205
23
    current->rel_filepos = reloc_base;
2206
23
    relsize = current->reloc_count * external_reloc_size;
2207
23
    reloc_size += relsize;
2208
23
    reloc_base += relsize;
2209
23
  }
2210
265
    }
2211
2212
60
  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
60
  if ((abfd->flags & EXEC_P) != 0
2218
1
      && (abfd->flags & D_PAGED) != 0)
2219
1
    sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2220
1
    &~ (ecoff_backend (abfd)->round - 1));
2221
2222
60
  ecoff_data (abfd)->sym_filepos = sym_base;
2223
2224
60
  return reloc_size;
2225
60
}
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
12
{
2236
12
  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
12
  if (! abfd->output_has_begun
2241
8
      && ! ecoff_compute_section_file_positions (abfd))
2242
0
    return false;
2243
2244
12
  if (count == 0)
2245
0
    return true;
2246
2247
  /* Handle the .lib section specially so that Irix 4 shared libraries
2248
     work out.  See coff_set_section_contents in coffcode.h.  */
2249
12
  if (count >= 4 && strcmp (section->name, _LIB) == 0)
2250
0
    {
2251
0
      bfd_size_type off = 0;
2252
2253
0
      while (off <= count - 4)
2254
0
  {
2255
0
    uint32_t len = bfd_get_32 (abfd, (bfd_byte *) location + off);
2256
0
    if (len == 0
2257
0
        || len > (uint32_t) -1 / 4
2258
0
        || len * 4 > count - off)
2259
0
      break;
2260
0
    off += len * 4;
2261
0
    ++section->lma;
2262
0
  }
2263
2264
0
      BFD_ASSERT (off == count);
2265
0
    }
2266
2267
12
  pos = section->filepos + offset;
2268
12
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
2269
12
      || bfd_write (location, count, abfd) != count)
2270
0
    return false;
2271
2272
12
  return true;
2273
12
}
2274
2275
/* Set the GP value for an ECOFF file.  This is a hook used by the
2276
   assembler.  */
2277
2278
bool
2279
bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value)
2280
0
{
2281
0
  if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2282
0
      || bfd_get_format (abfd) != bfd_object)
2283
0
    {
2284
0
      bfd_set_error (bfd_error_invalid_operation);
2285
0
      return false;
2286
0
    }
2287
2288
0
  ecoff_data (abfd)->gp = gp_value;
2289
2290
0
  return true;
2291
0
}
2292
2293
/* Set the register masks for an ECOFF file.  This is a hook used by
2294
   the assembler.  */
2295
2296
bool
2297
bfd_ecoff_set_regmasks (bfd *abfd,
2298
      unsigned long gprmask,
2299
      unsigned long fprmask,
2300
      unsigned long *cprmask)
2301
0
{
2302
0
  ecoff_data_type *tdata;
2303
2304
0
  if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2305
0
      || bfd_get_format (abfd) != bfd_object)
2306
0
    {
2307
0
      bfd_set_error (bfd_error_invalid_operation);
2308
0
      return false;
2309
0
    }
2310
2311
0
  tdata = ecoff_data (abfd);
2312
0
  tdata->gprmask = gprmask;
2313
0
  tdata->fprmask = fprmask;
2314
0
  if (cprmask != NULL)
2315
0
    {
2316
0
      int i;
2317
2318
0
      for (i = 0; i < 3; i++)
2319
0
  tdata->cprmask[i] = cprmask[i];
2320
0
    }
2321
2322
0
  return true;
2323
0
}
2324
2325
/* Get ECOFF EXTR information for an external symbol.  This function
2326
   is passed to bfd_ecoff_debug_externals.  */
2327
2328
static bool
2329
ecoff_get_extr (asymbol *sym, EXTR *esym)
2330
2
{
2331
2
  ecoff_symbol_type *ecoff_sym_ptr;
2332
2
  bfd *input_bfd;
2333
2334
2
  if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2335
2
      || ecoffsymbol (sym)->native == NULL)
2336
0
    {
2337
      /* Don't include debugging, local, or section symbols.  */
2338
0
      if ((sym->flags & BSF_DEBUGGING) != 0
2339
0
    || (sym->flags & BSF_LOCAL) != 0
2340
0
    || (sym->flags & BSF_SECTION_SYM) != 0)
2341
0
  return false;
2342
2343
0
      esym->jmptbl = 0;
2344
0
      esym->cobol_main = 0;
2345
0
      esym->weakext = (sym->flags & BSF_WEAK) != 0;
2346
0
      esym->reserved = 0;
2347
0
      esym->ifd = ifdNil;
2348
      /* FIXME: we can do better than this for st and sc.  */
2349
0
      esym->asym.st = stGlobal;
2350
0
      if (bfd_is_und_section (sym->section))
2351
0
  esym->asym.sc = scUndefined;
2352
0
      else if (bfd_is_com_section (sym->section))
2353
0
  esym->asym.sc = scCommon;
2354
0
      else if (bfd_is_abs_section (sym->section))
2355
0
  esym->asym.sc = scAbs;
2356
0
      else if ((sym->section->flags & SEC_HAS_CONTENTS) == 0)
2357
0
  esym->asym.sc = scBss;
2358
0
      else if ((sym->section->flags & SEC_CODE) != 0)
2359
0
  esym->asym.sc = scText;
2360
0
      else
2361
0
  esym->asym.sc = scData;
2362
0
      esym->asym.reserved = 0;
2363
0
      esym->asym.index = indexNil;
2364
0
      return true;
2365
0
    }
2366
2367
2
  ecoff_sym_ptr = ecoffsymbol (sym);
2368
2369
2
  if (ecoff_sym_ptr->local)
2370
0
    return false;
2371
2372
2
  input_bfd = bfd_asymbol_bfd (sym);
2373
2
  (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2374
2
    (input_bfd, ecoff_sym_ptr->native, esym);
2375
2376
  /* If the symbol was defined by the linker, then esym will be
2377
     undefined but sym will not be.  Get a better class for such a
2378
     symbol.  */
2379
2
  if ((esym->asym.sc == scUndefined
2380
1
       || esym->asym.sc == scSUndefined)
2381
1
      && ! bfd_is_und_section (bfd_asymbol_section (sym)))
2382
0
    esym->asym.sc = scAbs;
2383
2384
  /* Adjust the FDR index for the symbol by that used for the input
2385
     BFD.  */
2386
2
  if (esym->ifd != -1)
2387
0
    {
2388
0
      struct ecoff_debug_info *input_debug;
2389
2390
0
      input_debug = &ecoff_data (input_bfd)->debug_info;
2391
0
      BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2392
0
      if (input_debug->ifdmap != NULL)
2393
0
  esym->ifd = input_debug->ifdmap[esym->ifd];
2394
0
    }
2395
2396
2
  return true;
2397
2
}
2398
2399
/* Set the external symbol index.  This routine is passed to
2400
   bfd_ecoff_debug_externals.  */
2401
2402
static void
2403
ecoff_set_index (asymbol *sym, bfd_size_type indx)
2404
2
{
2405
2
  ecoff_set_sym_index (sym, indx);
2406
2
}
2407
2408
/* Write out an ECOFF file.  */
2409
2410
bool
2411
_bfd_ecoff_write_object_contents (bfd *abfd)
2412
60
{
2413
60
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2414
60
  const bfd_vma round = backend->round;
2415
60
  const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2416
60
  const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2417
60
  const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2418
60
  const bfd_size_type external_hdr_size
2419
60
    = backend->debug_swap.external_hdr_size;
2420
60
  const bfd_size_type external_reloc_size = backend->external_reloc_size;
2421
60
  void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
2422
60
    = backend->adjust_reloc_out;
2423
60
  void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
2424
60
    = backend->swap_reloc_out;
2425
60
  struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2426
60
  HDRR * const symhdr = &debug->symbolic_header;
2427
60
  asection *current;
2428
60
  unsigned int count;
2429
60
  bfd_size_type reloc_size;
2430
60
  bfd_size_type text_size;
2431
60
  bfd_vma text_start;
2432
60
  bool set_text_start;
2433
60
  bfd_size_type data_size;
2434
60
  bfd_vma data_start;
2435
60
  bool set_data_start;
2436
60
  bfd_size_type bss_size;
2437
60
  void * buff = NULL;
2438
60
  void * reloc_buff = NULL;
2439
60
  struct internal_filehdr internal_f;
2440
60
  struct internal_aouthdr internal_a;
2441
60
  int i;
2442
2443
  /* Determine where the sections and relocs will go in the output
2444
     file.  */
2445
60
  reloc_size = ecoff_compute_reloc_file_positions (abfd);
2446
2447
60
  count = 1;
2448
60
  for (current = abfd->sections;
2449
325
       current != NULL;
2450
265
       current = current->next)
2451
265
    {
2452
265
      current->target_index = count;
2453
265
      ++count;
2454
265
    }
2455
2456
60
  if ((abfd->flags & D_PAGED) != 0)
2457
60
    text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
2458
0
  else
2459
0
    text_size = 0;
2460
60
  text_start = 0;
2461
60
  set_text_start = false;
2462
60
  data_size = 0;
2463
60
  data_start = 0;
2464
60
  set_data_start = false;
2465
60
  bss_size = 0;
2466
2467
  /* Write section headers to the file.  */
2468
2469
  /* Allocate buff big enough to hold a section header,
2470
     file header, or a.out header.  */
2471
60
  {
2472
60
    bfd_size_type siz;
2473
2474
60
    siz = scnhsz;
2475
60
    if (siz < filhsz)
2476
0
      siz = filhsz;
2477
60
    if (siz < aoutsz)
2478
60
      siz = aoutsz;
2479
60
    buff = bfd_malloc (siz);
2480
60
    if (buff == NULL)
2481
0
      goto error_return;
2482
60
  }
2483
2484
60
  internal_f.f_nscns = 0;
2485
60
  if (bfd_seek (abfd, filhsz + aoutsz, SEEK_SET) != 0)
2486
0
    goto error_return;
2487
2488
60
  for (current = abfd->sections;
2489
325
       current != NULL;
2490
265
       current = current->next)
2491
265
    {
2492
265
      struct internal_scnhdr section;
2493
265
      bfd_vma vma;
2494
2495
265
      ++internal_f.f_nscns;
2496
2497
265
      strncpy (section.s_name, current->name, sizeof section.s_name);
2498
2499
      /* This seems to be correct for Irix 4 shared libraries.  */
2500
265
      vma = bfd_section_vma (current);
2501
265
      if (streq (current->name, _LIB))
2502
0
  section.s_vaddr = 0;
2503
265
      else
2504
265
  section.s_vaddr = vma;
2505
2506
265
      section.s_paddr = current->lma;
2507
265
      section.s_size = current->size;
2508
2509
      /* If this section is unloadable then the scnptr will be 0.  */
2510
265
      if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2511
100
  section.s_scnptr = 0;
2512
165
      else
2513
165
  section.s_scnptr = current->filepos;
2514
265
      section.s_relptr = current->rel_filepos;
2515
2516
      /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2517
   object file produced by the assembler is supposed to point to
2518
   information about how much room is required by objects of
2519
   various different sizes.  I think this only matters if we
2520
   want the linker to compute the best size to use, or
2521
   something.  I don't know what happens if the information is
2522
   not present.  */
2523
265
      if (! streq (current->name, _PDATA))
2524
265
  section.s_lnnoptr = 0;
2525
0
      else
2526
0
  {
2527
    /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2528
       hold the number of entries in the section (each entry is
2529
       8 bytes).  We stored this in the line_filepos field in
2530
       ecoff_compute_section_file_positions.  */
2531
0
    section.s_lnnoptr = current->line_filepos;
2532
0
  }
2533
2534
265
      section.s_nreloc = current->reloc_count;
2535
265
      section.s_nlnno = 0;
2536
265
      section.s_flags = ecoff_sec_to_styp_flags (current->name,
2537
265
             current->flags);
2538
2539
265
      if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff, current) == 0
2540
265
    || bfd_write (buff, scnhsz, abfd) != scnhsz)
2541
0
  goto error_return;
2542
2543
265
      if ((section.s_flags & STYP_TEXT) != 0
2544
153
    || ((section.s_flags & STYP_RDATA) != 0
2545
0
        && ecoff_data (abfd)->rdata_in_text)
2546
153
    || section.s_flags == STYP_PDATA
2547
153
    || (section.s_flags & STYP_DYNAMIC) != 0
2548
153
    || (section.s_flags & STYP_LIBLIST) != 0
2549
153
    || (section.s_flags & STYP_RELDYN) != 0
2550
153
    || section.s_flags == STYP_CONFLIC
2551
153
    || (section.s_flags & STYP_DYNSTR) != 0
2552
153
    || (section.s_flags & STYP_DYNSYM) != 0
2553
153
    || (section.s_flags & STYP_HASH) != 0
2554
153
    || (section.s_flags & STYP_ECOFF_INIT) != 0
2555
153
    || (section.s_flags & STYP_ECOFF_FINI) != 0
2556
153
    || section.s_flags == STYP_RCONST)
2557
112
  {
2558
112
    text_size += current->size;
2559
112
    if (! set_text_start || text_start > vma)
2560
51
      {
2561
51
        text_start = vma;
2562
51
        set_text_start = true;
2563
51
      }
2564
112
  }
2565
153
      else if ((section.s_flags & STYP_RDATA) != 0
2566
153
         || (section.s_flags & STYP_DATA) != 0
2567
118
         || (section.s_flags & STYP_LITA) != 0
2568
118
         || (section.s_flags & STYP_LIT8) != 0
2569
118
         || (section.s_flags & STYP_LIT4) != 0
2570
118
         || (section.s_flags & STYP_SDATA) != 0
2571
118
         || section.s_flags == STYP_XDATA
2572
118
         || (section.s_flags & STYP_GOT) != 0)
2573
35
  {
2574
35
    data_size += current->size;
2575
35
    if (! set_data_start || data_start > vma)
2576
9
      {
2577
9
        data_start = vma;
2578
9
        set_data_start = true;
2579
9
      }
2580
35
  }
2581
118
      else if ((section.s_flags & STYP_BSS) != 0
2582
90
         || (section.s_flags & STYP_SBSS) != 0)
2583
28
  bss_size += current->size;
2584
90
      else if (section.s_flags == 0
2585
0
         || (section.s_flags & STYP_ECOFF_LIB) != 0
2586
0
         || section.s_flags == STYP_COMMENT)
2587
90
  /* Do nothing.  */ ;
2588
0
      else
2589
0
  abort ();
2590
265
    }
2591
2592
  /* Set up the file header.  */
2593
60
  internal_f.f_magic = ecoff_get_magic (abfd);
2594
2595
  /* We will NOT put a fucking timestamp in the header here. Every
2596
     time you put it back, I will come in and take it out again.  I'm
2597
     sorry.  This field does not belong here.  We fill it with a 0 so
2598
     it compares the same but is not a reasonable time. --
2599
     gnu@cygnus.com.  */
2600
60
  internal_f.f_timdat = 0;
2601
2602
60
  if (bfd_get_symcount (abfd) != 0)
2603
1
    {
2604
      /* The ECOFF f_nsyms field is not actually the number of
2605
   symbols, it's the size of symbolic information header.  */
2606
1
      internal_f.f_nsyms = external_hdr_size;
2607
1
      internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2608
1
    }
2609
59
  else
2610
59
    {
2611
59
      internal_f.f_nsyms = 0;
2612
59
      internal_f.f_symptr = 0;
2613
59
    }
2614
2615
60
  internal_f.f_opthdr = aoutsz;
2616
2617
60
  internal_f.f_flags = F_LNNO;
2618
60
  if (reloc_size == 0)
2619
37
    internal_f.f_flags |= F_RELFLG;
2620
60
  if (bfd_get_symcount (abfd) == 0)
2621
59
    internal_f.f_flags |= F_LSYMS;
2622
60
  if (abfd->flags & EXEC_P)
2623
1
    internal_f.f_flags |= F_EXEC;
2624
2625
60
  if (bfd_little_endian (abfd))
2626
59
    internal_f.f_flags |= F_AR32WR;
2627
1
  else
2628
1
    internal_f.f_flags |= F_AR32W;
2629
2630
  /* Set up the ``optional'' header.  */
2631
60
  if ((abfd->flags & D_PAGED) != 0)
2632
60
    internal_a.magic = ECOFF_AOUT_ZMAGIC;
2633
0
  else
2634
0
    internal_a.magic = ECOFF_AOUT_OMAGIC;
2635
2636
  /* FIXME: Is this really correct?  */
2637
60
  internal_a.vstamp = symhdr->vstamp;
2638
2639
  /* At least on Ultrix, these have to be rounded to page boundaries.
2640
     FIXME: Is this true on other platforms?  */
2641
60
  if ((abfd->flags & D_PAGED) != 0)
2642
60
    {
2643
60
      internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2644
60
      internal_a.text_start = text_start &~ (round - 1);
2645
60
      internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2646
60
      internal_a.data_start = data_start &~ (round - 1);
2647
60
    }
2648
0
  else
2649
0
    {
2650
0
      internal_a.tsize = text_size;
2651
0
      internal_a.text_start = text_start;
2652
0
      internal_a.dsize = data_size;
2653
0
      internal_a.data_start = data_start;
2654
0
    }
2655
2656
  /* On Ultrix, the initial portions of the .sbss and .bss segments
2657
     are at the end of the data section.  The bsize field in the
2658
     optional header records how many bss bytes are required beyond
2659
     those in the data section.  The value is not rounded to a page
2660
     boundary.  */
2661
60
  if (bss_size < internal_a.dsize - data_size)
2662
4
    bss_size = 0;
2663
56
  else
2664
56
    bss_size -= internal_a.dsize - data_size;
2665
60
  internal_a.bsize = bss_size;
2666
60
  internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2667
2668
60
  internal_a.entry = bfd_get_start_address (abfd);
2669
2670
60
  internal_a.gp_value = ecoff_data (abfd)->gp;
2671
2672
60
  internal_a.gprmask = ecoff_data (abfd)->gprmask;
2673
60
  internal_a.fprmask = ecoff_data (abfd)->fprmask;
2674
300
  for (i = 0; i < 4; i++)
2675
240
    internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2676
2677
  /* Let the backend adjust the headers if necessary.  */
2678
60
  if (backend->adjust_headers)
2679
11
    {
2680
11
      if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2681
0
  goto error_return;
2682
11
    }
2683
2684
  /* Write out the file header and the optional header.  */
2685
60
  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
2686
0
    goto error_return;
2687
2688
60
  bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
2689
60
  if (bfd_write (buff, filhsz, abfd) != filhsz)
2690
0
    goto error_return;
2691
2692
60
  bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
2693
60
  if (bfd_write (buff, aoutsz, abfd) != aoutsz)
2694
0
    goto error_return;
2695
2696
  /* Build the external symbol information.  This must be done before
2697
     writing out the relocs so that we know the symbol indices.  We
2698
     don't do this if this BFD was created by the backend linker,
2699
     since it will have already handled the symbols and relocs.  */
2700
60
  if (! ecoff_data (abfd)->linker)
2701
60
    {
2702
60
      symhdr->iextMax = 0;
2703
60
      symhdr->issExtMax = 0;
2704
60
      debug->external_ext = debug->external_ext_end = NULL;
2705
60
      debug->ssext = debug->ssext_end = NULL;
2706
60
      if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2707
60
               (abfd->flags & EXEC_P) == 0,
2708
60
               ecoff_get_extr, ecoff_set_index))
2709
0
  goto error_return;
2710
2711
      /* Write out the relocs.  */
2712
60
      for (current = abfd->sections;
2713
325
     current != NULL;
2714
265
     current = current->next)
2715
265
  {
2716
265
    arelent **reloc_ptr_ptr;
2717
265
    arelent **reloc_end;
2718
265
    char *out_ptr;
2719
265
    bfd_size_type amt;
2720
2721
265
    if (current->reloc_count == 0)
2722
242
      continue;
2723
2724
23
    amt = current->reloc_count * external_reloc_size;
2725
23
    reloc_buff = bfd_zalloc (abfd, amt);
2726
23
    if (reloc_buff == NULL)
2727
0
      goto error_return;
2728
2729
23
    reloc_ptr_ptr = current->orelocation;
2730
23
    reloc_end = reloc_ptr_ptr + current->reloc_count;
2731
23
    out_ptr = (char *) reloc_buff;
2732
2733
23
    for (;
2734
3.03k
         reloc_ptr_ptr < reloc_end;
2735
3.01k
         reloc_ptr_ptr++, out_ptr += external_reloc_size)
2736
3.01k
      {
2737
3.01k
        arelent *reloc;
2738
3.01k
        asymbol *sym;
2739
3.01k
        struct internal_reloc in;
2740
2741
3.01k
        memset ((void *) &in, 0, sizeof in);
2742
2743
3.01k
        reloc = *reloc_ptr_ptr;
2744
3.01k
        sym = *reloc->sym_ptr_ptr;
2745
2746
        /* If the howto field has not been initialised then skip this reloc.
2747
     This assumes that an error message has been issued elsewhere.  */
2748
3.01k
        if (reloc->howto == NULL)
2749
553
    continue;
2750
2751
2.46k
        in.r_vaddr = reloc->address + bfd_section_vma (current);
2752
2.46k
        in.r_type = reloc->howto->type;
2753
2754
2.46k
        if ((sym->flags & BSF_SECTION_SYM) == 0)
2755
0
    {
2756
0
      in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2757
0
      in.r_extern = 1;
2758
0
    }
2759
2.46k
        else
2760
2.46k
    {
2761
2.46k
      const char *name;
2762
2.46k
      unsigned int j;
2763
2.46k
      static struct
2764
2.46k
      {
2765
2.46k
        const char * name;
2766
2.46k
        long r_symndx;
2767
2.46k
      }
2768
2.46k
      section_symndx [] =
2769
2.46k
      {
2770
2.46k
        { _TEXT,   RELOC_SECTION_TEXT   },
2771
2.46k
        { _RDATA,  RELOC_SECTION_RDATA  },
2772
2.46k
        { _DATA,   RELOC_SECTION_DATA   },
2773
2.46k
        { _SDATA,  RELOC_SECTION_SDATA  },
2774
2.46k
        { _SBSS,   RELOC_SECTION_SBSS   },
2775
2.46k
        { _BSS,    RELOC_SECTION_BSS    },
2776
2.46k
        { _INIT,   RELOC_SECTION_INIT   },
2777
2.46k
        { _LIT8,   RELOC_SECTION_LIT8   },
2778
2.46k
        { _LIT4,   RELOC_SECTION_LIT4   },
2779
2.46k
        { _XDATA,  RELOC_SECTION_XDATA  },
2780
2.46k
        { _PDATA,  RELOC_SECTION_PDATA  },
2781
2.46k
        { _FINI,   RELOC_SECTION_FINI   },
2782
2.46k
        { _LITA,   RELOC_SECTION_LITA   },
2783
2.46k
        { "*ABS*", RELOC_SECTION_ABS    },
2784
2.46k
        { _RCONST, RELOC_SECTION_RCONST }
2785
2.46k
      };
2786
2787
2.46k
      name = bfd_section_name (bfd_asymbol_section (sym));
2788
2789
34.4k
      for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
2790
34.4k
        if (streq (name, section_symndx[j].name))
2791
2.46k
          {
2792
2.46k
      in.r_symndx = section_symndx[j].r_symndx;
2793
2.46k
      break;
2794
2.46k
          }
2795
2796
2.46k
      if (j == ARRAY_SIZE (section_symndx))
2797
0
        abort ();
2798
2.46k
      in.r_extern = 0;
2799
2.46k
    }
2800
2801
2.46k
        (*adjust_reloc_out) (abfd, reloc, &in);
2802
2803
2.46k
        (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
2804
2.46k
      }
2805
2806
23
    if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2807
0
      goto error_return;
2808
23
    amt = current->reloc_count * external_reloc_size;
2809
23
    if (bfd_write (reloc_buff, amt, abfd) != amt)
2810
0
      goto error_return;
2811
23
    bfd_release (abfd, reloc_buff);
2812
23
    reloc_buff = NULL;
2813
23
  }
2814
2815
      /* Write out the symbolic debugging information.  */
2816
60
      if (bfd_get_symcount (abfd) > 0)
2817
1
  {
2818
    /* Write out the debugging information.  */
2819
1
    if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2820
1
               ecoff_data (abfd)->sym_filepos))
2821
0
      goto error_return;
2822
1
  }
2823
60
    }
2824
2825
  /* The .bss section of a demand paged executable must receive an
2826
     entire page.  If there are symbols, the symbols will start on the
2827
     next page.  If there are no symbols, we must fill out the page by
2828
     hand.  */
2829
60
  if (bfd_get_symcount (abfd) == 0
2830
59
      && (abfd->flags & EXEC_P) != 0
2831
1
      && (abfd->flags & D_PAGED) != 0)
2832
1
    {
2833
1
      char c;
2834
2835
1
      if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2836
0
  goto error_return;
2837
1
      if (bfd_read (&c, 1, abfd) == 0)
2838
1
  c = 0;
2839
1
      if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2840
0
  goto error_return;
2841
1
      if (bfd_write (&c, 1, abfd) != 1)
2842
0
  goto error_return;
2843
1
    }
2844
2845
60
  if (reloc_buff != NULL)
2846
0
    bfd_release (abfd, reloc_buff);
2847
60
  free (buff);
2848
60
  return true;
2849
0
 error_return:
2850
0
  if (reloc_buff != NULL)
2851
0
    bfd_release (abfd, reloc_buff);
2852
0
  free (buff);
2853
0
  return false;
2854
60
}
2855

2856
/* Archive handling.  ECOFF uses what appears to be a unique type of
2857
   archive header (armap).  The byte ordering of the armap and the
2858
   contents are encoded in the name of the armap itself.  At least for
2859
   now, we only support archives with the same byte ordering in the
2860
   armap and the contents.
2861
2862
   The first four bytes in the armap are the number of symbol
2863
   definitions.  This is always a power of two.
2864
2865
   This is followed by the symbol definitions.  Each symbol definition
2866
   occupies 8 bytes.  The first four bytes are the offset from the
2867
   start of the armap strings to the null-terminated string naming
2868
   this symbol.  The second four bytes are the file offset to the
2869
   archive member which defines this symbol.  If the second four bytes
2870
   are 0, then this is not actually a symbol definition, and it should
2871
   be ignored.
2872
2873
   The symbols are hashed into the armap with a closed hashing scheme.
2874
   See the functions below for the details of the algorithm.
2875
2876
   After the symbol definitions comes four bytes holding the size of
2877
   the string table, followed by the string table itself.  */
2878
2879
/* The name of an archive headers looks like this:
2880
   __________E[BL]E[BL]_ (with a trailing space).
2881
   The trailing space is changed to an X if the archive is changed to
2882
   indicate that the armap is out of date.
2883
2884
   The Alpha seems to use ________64E[BL]E[BL]_.  */
2885
2886
10.6k
#define ARMAP_BIG_ENDIAN    'B'
2887
2.88k
#define ARMAP_LITTLE_ENDIAN   'L'
2888
371k
#define ARMAP_MARKER      'E'
2889
183k
#define ARMAP_START_LENGTH    10
2890
2.97k
#define ARMAP_HEADER_MARKER_INDEX 10
2891
5.24k
#define ARMAP_HEADER_ENDIAN_INDEX 11
2892
1.97k
#define ARMAP_OBJECT_MARKER_INDEX 12
2893
4.30k
#define ARMAP_OBJECT_ENDIAN_INDEX 13
2894
1.83k
#define ARMAP_END_INDEX     14
2895
3.67k
#define ARMAP_END     "_ "
2896
2897
/* This is a magic number used in the hashing algorithm.  */
2898
0
#define ARMAP_HASH_MAGIC    0x9dd68ab5
2899
2900
/* This returns the hash value to use for a string.  It also sets
2901
   *REHASH to the rehash adjustment if the first slot is taken.  SIZE
2902
   is the number of entries in the hash table, and HLOG is the log
2903
   base 2 of SIZE.  */
2904
2905
static unsigned int
2906
ecoff_armap_hash (const char *s,
2907
      unsigned int *rehash,
2908
      unsigned int size,
2909
      unsigned int hlog)
2910
0
{
2911
0
  unsigned int hash;
2912
2913
0
  if (hlog == 0)
2914
0
    return 0;
2915
0
  hash = *s++;
2916
0
  while (*s != '\0')
2917
0
    hash = ((hash >> 27) | (hash << 5)) + *s++;
2918
0
  hash *= ARMAP_HASH_MAGIC;
2919
0
  *rehash = (hash & (size - 1)) | 1;
2920
0
  return hash >> (32 - hlog);
2921
0
}
2922
2923
/* Read in the armap.  */
2924
2925
bool
2926
_bfd_ecoff_slurp_armap (bfd *abfd)
2927
205k
{
2928
205k
  char nextname[17];
2929
205k
  unsigned int i;
2930
205k
  struct areltdata *mapdata;
2931
205k
  bfd_size_type parsed_size, stringsize;
2932
205k
  char *raw_armap;
2933
205k
  struct artdata *ardata;
2934
205k
  unsigned int count;
2935
205k
  char *raw_ptr;
2936
205k
  carsym *symdef_ptr;
2937
205k
  char *stringbase;
2938
205k
  bfd_size_type amt;
2939
2940
205k
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
2941
2942
  /* Get the name of the first element.  */
2943
205k
  i = bfd_read (nextname, 16, abfd);
2944
205k
  if (i == 0)
2945
0
    return true;
2946
205k
  if (i != 16)
2947
88
    return false;
2948
2949
205k
  if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
2950
0
    return false;
2951
2952
  /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2953
     standard COFF armap.  We could move the ECOFF armap stuff into
2954
     bfd_slurp_armap, but that seems inappropriate since no other
2955
     target uses this format.  Instead, we check directly for a COFF
2956
     armap.  */
2957
205k
  if (startswith (nextname, "/               "))
2958
21.9k
    return bfd_slurp_armap (abfd);
2959
2960
  /* See if the first element is an armap.  */
2961
183k
  if (strncmp (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH) != 0
2962
2.97k
      || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2963
2.07k
      || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2964
1.47k
    && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2965
1.97k
      || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2966
1.90k
      || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2967
1.41k
    && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2968
1.83k
      || strncmp (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1) != 0)
2969
181k
    {
2970
181k
      abfd->has_armap = false;
2971
181k
      return true;
2972
181k
    }
2973
2974
  /* Make sure we have the right byte ordering.  */
2975
1.69k
  if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2976
1.69k
       ^ (bfd_header_big_endian (abfd)))
2977
990
      || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2978
990
    ^ (bfd_big_endian (abfd))))
2979
876
    {
2980
876
      bfd_set_error (bfd_error_wrong_format);
2981
876
      return false;
2982
876
    }
2983
2984
  /* Read in the armap.  */
2985
820
  ardata = bfd_ardata (abfd);
2986
820
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2987
820
  if (mapdata == NULL)
2988
74
    return false;
2989
746
  parsed_size = mapdata->parsed_size;
2990
746
  free (mapdata);
2991
2992
746
  if (parsed_size + 1 < 9)
2993
7
    {
2994
7
      bfd_set_error (bfd_error_malformed_archive);
2995
7
      return false;
2996
7
    }
2997
2998
739
  raw_armap = (char *) _bfd_alloc_and_read (abfd, parsed_size + 1, parsed_size);
2999
739
  if (raw_armap == NULL)
3000
305
    return false;
3001
434
  raw_armap[parsed_size] = 0;
3002
3003
434
  ardata->tdata = (void *) raw_armap;
3004
3005
434
  count = H_GET_32 (abfd, raw_armap);
3006
434
  if ((parsed_size - 8) / 8 < count)
3007
18
    goto error_malformed;
3008
3009
416
  ardata->symdef_count = 0;
3010
416
  ardata->cache = NULL;
3011
3012
  /* This code used to overlay the symdefs over the raw archive data,
3013
     but that doesn't work on a 64 bit host.  */
3014
416
  stringbase = raw_armap + count * 8 + 8;
3015
416
  stringsize = parsed_size - (count * 8 + 8);
3016
3017
#ifdef CHECK_ARMAP_HASH
3018
  {
3019
    unsigned int hlog;
3020
3021
    /* Double check that I have the hashing algorithm right by making
3022
       sure that every symbol can be looked up successfully.  */
3023
    hlog = 0;
3024
    for (i = 1; i < count; i <<= 1)
3025
      hlog++;
3026
    BFD_ASSERT (i == count);
3027
3028
    raw_ptr = raw_armap + 4;
3029
    for (i = 0; i < count; i++, raw_ptr += 8)
3030
      {
3031
  unsigned int name_offset, file_offset;
3032
  unsigned int hash, rehash, srch;
3033
3034
  name_offset = H_GET_32 (abfd, raw_ptr);
3035
  file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3036
  if (file_offset == 0)
3037
    continue;
3038
  hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3039
         hlog);
3040
  if (hash == i)
3041
    continue;
3042
3043
  /* See if we can rehash to this location.  */
3044
  for (srch = (hash + rehash) & (count - 1);
3045
       srch != hash && srch != i;
3046
       srch = (srch + rehash) & (count - 1))
3047
    BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
3048
  BFD_ASSERT (srch == i);
3049
      }
3050
  }
3051
3052
#endif /* CHECK_ARMAP_HASH */
3053
3054
416
  raw_ptr = raw_armap + 4;
3055
4.62k
  for (i = 0; i < count; i++, raw_ptr += 8)
3056
4.20k
    if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
3057
1.69k
      ++ardata->symdef_count;
3058
3059
416
  amt = ardata->symdef_count;
3060
416
  amt *= sizeof (carsym);
3061
416
  symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
3062
416
  if (!symdef_ptr)
3063
0
    goto error_exit;
3064
3065
416
  ardata->symdefs = symdef_ptr;
3066
3067
416
  raw_ptr = raw_armap + 4;
3068
2.94k
  for (i = 0; i < count; i++, raw_ptr += 8)
3069
2.64k
    {
3070
2.64k
      unsigned int name_offset, file_offset;
3071
3072
2.64k
      file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3073
2.64k
      if (file_offset == 0)
3074
1.95k
  continue;
3075
690
      name_offset = H_GET_32 (abfd, raw_ptr);
3076
690
      if (name_offset > stringsize)
3077
115
  goto error_malformed;
3078
575
      symdef_ptr->name = stringbase + name_offset;
3079
575
      symdef_ptr->u.file_offset = file_offset;
3080
575
      ++symdef_ptr;
3081
575
    }
3082
3083
301
  ardata->first_file.file_offset = bfd_tell (abfd);
3084
  /* Pad to an even boundary.  */
3085
301
  ardata->first_file.file_offset += ardata->first_file.file_offset % 2;
3086
301
  abfd->has_armap = true;
3087
301
  return true;
3088
3089
133
 error_malformed:
3090
133
  bfd_set_error (bfd_error_malformed_archive);
3091
133
 error_exit:
3092
133
  ardata->symdef_count = 0;
3093
133
  ardata->symdefs = NULL;
3094
133
  ardata->tdata = NULL;
3095
133
  bfd_release (abfd, raw_armap);
3096
133
  return false;
3097
133
}
3098
3099
/* Write out an armap.  */
3100
3101
bool
3102
_bfd_ecoff_write_armap (bfd *abfd,
3103
      unsigned int elength,
3104
      struct orl *map,
3105
      unsigned int orl_count,
3106
      int stridx)
3107
0
{
3108
0
  unsigned int hashsize, hashlog;
3109
0
  bfd_size_type symdefsize;
3110
0
  int padit;
3111
0
  unsigned int stringsize;
3112
0
  unsigned int mapsize;
3113
0
  file_ptr firstreal;
3114
0
  struct ar_hdr hdr;
3115
0
  struct stat statbuf;
3116
0
  unsigned int i;
3117
0
  bfd_byte temp[4];
3118
0
  bfd_byte *hashtable;
3119
0
  bfd *current;
3120
0
  bfd *last_elt;
3121
0
  unsigned int namidx = 0;
3122
3123
  /* Ultrix appears to use as a hash table size the least power of two
3124
     greater than twice the number of entries.  */
3125
0
  for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3126
0
    ;
3127
0
  hashsize = 1 << hashlog;
3128
3129
0
  symdefsize = hashsize * 8;
3130
0
  padit = stridx % 2;
3131
0
  stringsize = stridx + padit;
3132
3133
  /* Include 8 bytes to store symdefsize and stringsize in output.  */
3134
0
  mapsize = symdefsize + stringsize + 8;
3135
3136
0
  firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3137
3138
0
  memset ((void *) &hdr, 0, sizeof hdr);
3139
3140
  /* Work out the ECOFF armap name.  */
3141
0
  strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3142
0
  hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3143
0
  hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3144
0
    (bfd_header_big_endian (abfd)
3145
0
     ? ARMAP_BIG_ENDIAN
3146
0
     : ARMAP_LITTLE_ENDIAN);
3147
0
  hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3148
0
  hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3149
0
    bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3150
0
  memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3151
3152
  /* Write the timestamp of the archive header to be just a little bit
3153
     later than the timestamp of the file, otherwise the linker will
3154
     complain that the index is out of date.  Actually, the Ultrix
3155
     linker just checks the archive name; the GNU linker may check the
3156
     date.  */
3157
0
  if (stat (bfd_get_filename (abfd), &statbuf) == 0)
3158
0
    _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
3159
0
          (long) (statbuf.st_mtime + ARMAP_TIME_OFFSET));
3160
3161
  /* The DECstation uses zeroes for the uid, gid and mode of the
3162
     armap.  */
3163
0
  hdr.ar_uid[0] = '0';
3164
0
  hdr.ar_gid[0] = '0';
3165
  /* Building gcc ends up extracting the armap as a file - twice.  */
3166
0
  hdr.ar_mode[0] = '6';
3167
0
  hdr.ar_mode[1] = '4';
3168
0
  hdr.ar_mode[2] = '4';
3169
3170
0
  _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
3171
3172
0
  hdr.ar_fmag[0] = '`';
3173
0
  hdr.ar_fmag[1] = '\012';
3174
3175
  /* Turn all null bytes in the header into spaces.  */
3176
0
  for (i = 0; i < sizeof (struct ar_hdr); i++)
3177
0
   if (((char *) (&hdr))[i] == '\0')
3178
0
     (((char *) (&hdr))[i]) = ' ';
3179
3180
0
  if (bfd_write (&hdr, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
3181
0
    return false;
3182
3183
0
  H_PUT_32 (abfd, hashsize, temp);
3184
0
  if (bfd_write (temp, 4, abfd) != 4)
3185
0
    return false;
3186
3187
0
  hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3188
0
  if (!hashtable)
3189
0
    return false;
3190
3191
0
  current = abfd->archive_head;
3192
0
  last_elt = current;
3193
0
  for (i = 0; i < orl_count; i++)
3194
0
    {
3195
0
      unsigned int hash, rehash = 0;
3196
3197
      /* Advance firstreal to the file position of this archive
3198
   element.  */
3199
0
      if (map[i].abfd != last_elt)
3200
0
  {
3201
0
    do
3202
0
      {
3203
0
        firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3204
0
        firstreal += firstreal % 2;
3205
0
        current = current->archive_next;
3206
0
      }
3207
0
    while (current != map[i].abfd);
3208
0
  }
3209
3210
0
      last_elt = current;
3211
3212
0
      hash = ecoff_armap_hash (map[i].name, &rehash, hashsize, hashlog);
3213
0
      if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
3214
0
  {
3215
0
    unsigned int srch;
3216
3217
    /* The desired slot is already taken.  */
3218
0
    for (srch = (hash + rehash) & (hashsize - 1);
3219
0
         srch != hash;
3220
0
         srch = (srch + rehash) & (hashsize - 1))
3221
0
      if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
3222
0
        break;
3223
3224
0
    BFD_ASSERT (srch != hash);
3225
3226
0
    hash = srch;
3227
0
  }
3228
3229
0
      H_PUT_32 (abfd, namidx, hashtable + hash * 8);
3230
0
      H_PUT_32 (abfd, firstreal, hashtable + hash * 8 + 4);
3231
3232
0
      namidx += strlen (map[i].name) + 1;
3233
0
    }
3234
3235
0
  if (bfd_write (hashtable, symdefsize, abfd) != symdefsize)
3236
0
    return false;
3237
3238
0
  bfd_release (abfd, hashtable);
3239
3240
  /* Now write the strings.  */
3241
0
  H_PUT_32 (abfd, stringsize, temp);
3242
0
  if (bfd_write (temp, 4, abfd) != 4)
3243
0
    return false;
3244
0
  for (i = 0; i < orl_count; i++)
3245
0
    {
3246
0
      bfd_size_type len;
3247
3248
0
      len = strlen (map[i].name) + 1;
3249
0
      if (bfd_write (map[i].name, len, abfd) != len)
3250
0
  return false;
3251
0
    }
3252
3253
  /* The spec sez this should be a newline.  But in order to be
3254
     bug-compatible for DECstation ar we use a null.  */
3255
0
  if (padit)
3256
0
    {
3257
0
      if (bfd_write ("", 1, abfd) != 1)
3258
0
  return false;
3259
0
    }
3260
3261
0
  return true;
3262
0
}
3263

3264
/* ECOFF linker code.  */
3265
3266
/* Routine to create an entry in an ECOFF link hash table.  */
3267
3268
static struct bfd_hash_entry *
3269
ecoff_link_hash_newfunc (struct bfd_hash_entry *entry,
3270
       struct bfd_hash_table *table,
3271
       const char *string)
3272
0
{
3273
0
  struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3274
3275
  /* Allocate the structure if it has not already been allocated by a
3276
     subclass.  */
3277
0
  if (ret == NULL)
3278
0
    ret = ((struct ecoff_link_hash_entry *)
3279
0
     bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3280
0
  if (ret == NULL)
3281
0
    return NULL;
3282
3283
  /* Call the allocation method of the superclass.  */
3284
0
  ret = ((struct ecoff_link_hash_entry *)
3285
0
   _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3286
0
         table, string));
3287
3288
0
  if (ret)
3289
0
    {
3290
      /* Set local fields.  */
3291
0
      ret->indx = -1;
3292
0
      ret->abfd = NULL;
3293
0
      ret->written = 0;
3294
0
      ret->small = 0;
3295
0
      memset ((void *) &ret->esym, 0, sizeof ret->esym);
3296
0
    }
3297
3298
0
  return (struct bfd_hash_entry *) ret;
3299
0
}
3300
3301
/* Create an ECOFF link hash table.  */
3302
3303
struct bfd_link_hash_table *
3304
_bfd_ecoff_bfd_link_hash_table_create (bfd *abfd)
3305
0
{
3306
0
  struct ecoff_link_hash_table *ret;
3307
0
  size_t amt = sizeof (struct ecoff_link_hash_table);
3308
3309
0
  ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3310
0
  if (ret == NULL)
3311
0
    return NULL;
3312
0
  if (!_bfd_link_hash_table_init (&ret->root, abfd,
3313
0
          ecoff_link_hash_newfunc,
3314
0
          sizeof (struct ecoff_link_hash_entry)))
3315
0
    {
3316
0
      free (ret);
3317
0
      return NULL;
3318
0
    }
3319
0
  return &ret->root;
3320
0
}
3321
3322
/* Look up an entry in an ECOFF link hash table.  */
3323
3324
#define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3325
  ((struct ecoff_link_hash_entry *) \
3326
   bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3327
3328
/* Get the ECOFF link hash table from the info structure.  This is
3329
   just a cast.  */
3330
3331
#define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3332
3333
/* Add the external symbols of an object file to the global linker
3334
   hash table.  The external symbols and strings we are passed are
3335
   just allocated on the stack, and will be discarded.  We must
3336
   explicitly save any information we may need later on in the link.
3337
   We do not want to read the external symbol information again.  */
3338
3339
static bool
3340
ecoff_link_add_externals (bfd *abfd,
3341
        struct bfd_link_info *info,
3342
        void * external_ext,
3343
        char *ssext)
3344
0
{
3345
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3346
0
  void (* const swap_ext_in) (bfd *, void *, EXTR *)
3347
0
    = backend->debug_swap.swap_ext_in;
3348
0
  bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3349
0
  unsigned long ext_count;
3350
0
  struct bfd_link_hash_entry **sym_hash;
3351
0
  char *ext_ptr;
3352
0
  char *ext_end;
3353
0
  bfd_size_type amt;
3354
3355
0
  ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3356
3357
0
  amt = ext_count;
3358
0
  amt *= sizeof (struct bfd_link_hash_entry *);
3359
0
  sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
3360
0
  if (!sym_hash)
3361
0
    return false;
3362
0
  ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
3363
3364
0
  ext_ptr = (char *) external_ext;
3365
0
  ext_end = ext_ptr + ext_count * external_ext_size;
3366
0
  for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3367
0
    {
3368
0
      EXTR esym;
3369
0
      bool skip;
3370
0
      bfd_vma value;
3371
0
      asection *section;
3372
0
      const char *name;
3373
0
      struct ecoff_link_hash_entry *h;
3374
3375
0
      *sym_hash = NULL;
3376
3377
0
      (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
3378
3379
      /* Skip debugging symbols.  */
3380
0
      skip = false;
3381
0
      switch (esym.asym.st)
3382
0
  {
3383
0
  case stGlobal:
3384
0
  case stStatic:
3385
0
  case stLabel:
3386
0
  case stProc:
3387
0
  case stStaticProc:
3388
0
    break;
3389
0
  default:
3390
0
    skip = true;
3391
0
    break;
3392
0
  }
3393
3394
0
      if (skip)
3395
0
  continue;
3396
3397
      /* Get the information for this symbol.  */
3398
0
      value = esym.asym.value;
3399
0
      switch (esym.asym.sc)
3400
0
  {
3401
0
  default:
3402
0
  case scNil:
3403
0
  case scRegister:
3404
0
  case scCdbLocal:
3405
0
  case scBits:
3406
0
  case scCdbSystem:
3407
0
  case scRegImage:
3408
0
  case scInfo:
3409
0
  case scUserStruct:
3410
0
  case scVar:
3411
0
  case scVarRegister:
3412
0
  case scVariant:
3413
0
  case scBasedVar:
3414
0
  case scXData:
3415
0
  case scPData:
3416
0
    section = NULL;
3417
0
    break;
3418
0
  case scText:
3419
0
    section = bfd_make_section_old_way (abfd, _TEXT);
3420
0
    value -= section->vma;
3421
0
    break;
3422
0
  case scData:
3423
0
    section = bfd_make_section_old_way (abfd, _DATA);
3424
0
    value -= section->vma;
3425
0
    break;
3426
0
  case scBss:
3427
0
    section = bfd_make_section_old_way (abfd, _BSS);
3428
0
    value -= section->vma;
3429
0
    break;
3430
0
  case scAbs:
3431
0
    section = bfd_abs_section_ptr;
3432
0
    break;
3433
0
  case scUndefined:
3434
0
    section = bfd_und_section_ptr;
3435
0
    break;
3436
0
  case scSData:
3437
0
    section = bfd_make_section_old_way (abfd, _SDATA);
3438
0
    value -= section->vma;
3439
0
    break;
3440
0
  case scSBss:
3441
0
    section = bfd_make_section_old_way (abfd, _SBSS);
3442
0
    value -= section->vma;
3443
0
    break;
3444
0
  case scRData:
3445
0
    section = bfd_make_section_old_way (abfd, _RDATA);
3446
0
    value -= section->vma;
3447
0
    break;
3448
0
  case scCommon:
3449
0
    if (value > ecoff_data (abfd)->gp_size)
3450
0
      {
3451
0
        section = bfd_com_section_ptr;
3452
0
        break;
3453
0
      }
3454
    /* Fall through.  */
3455
0
  case scSCommon:
3456
0
    section = &_bfd_ecoff_scom_section;
3457
0
    break;
3458
0
  case scSUndefined:
3459
0
    section = bfd_und_section_ptr;
3460
0
    break;
3461
0
  case scInit:
3462
0
    section = bfd_make_section_old_way (abfd, _INIT);
3463
0
    value -= section->vma;
3464
0
    break;
3465
0
  case scFini:
3466
0
    section = bfd_make_section_old_way (abfd, _FINI);
3467
0
    value -= section->vma;
3468
0
    break;
3469
0
  case scRConst:
3470
0
    section = bfd_make_section_old_way (abfd, _RCONST);
3471
0
    value -= section->vma;
3472
0
    break;
3473
0
  }
3474
3475
0
      if (section == NULL)
3476
0
  continue;
3477
3478
0
      name = ssext + esym.asym.iss;
3479
3480
0
      if (! (_bfd_generic_link_add_one_symbol
3481
0
       (info, abfd, name,
3482
0
        (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3483
0
        section, value, NULL, true, true, sym_hash)))
3484
0
  return false;
3485
3486
0
      h = (struct ecoff_link_hash_entry *) *sym_hash;
3487
3488
      /* If we are building an ECOFF hash table, save the external
3489
   symbol information.  */
3490
0
      if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
3491
0
  {
3492
0
    if (h->abfd == NULL
3493
0
        || (! bfd_is_und_section (section)
3494
0
      && (! bfd_is_com_section (section)
3495
0
          || (h->root.type != bfd_link_hash_defined
3496
0
        && h->root.type != bfd_link_hash_defweak))))
3497
0
      {
3498
0
        h->abfd = abfd;
3499
0
        h->esym = esym;
3500
0
      }
3501
3502
    /* Remember whether this symbol was small undefined.  */
3503
0
    if (esym.asym.sc == scSUndefined)
3504
0
      h->small = 1;
3505
3506
    /* If this symbol was ever small undefined, it needs to wind
3507
       up in a GP relative section.  We can't control the
3508
       section of a defined symbol, but we can control the
3509
       section of a common symbol.  This case is actually needed
3510
       on Ultrix 4.2 to handle the symbol cred in -lckrb.  */
3511
0
    if (h->small
3512
0
        && h->root.type == bfd_link_hash_common
3513
0
        && streq (h->root.u.c.p->section->name, SCOMMON))
3514
0
      {
3515
0
        h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3516
0
                 SCOMMON);
3517
0
        h->root.u.c.p->section->flags = SEC_ALLOC;
3518
0
        if (h->esym.asym.sc == scCommon)
3519
0
    h->esym.asym.sc = scSCommon;
3520
0
      }
3521
0
  }
3522
0
    }
3523
3524
0
  return true;
3525
0
}
3526
3527
/* Add symbols from an ECOFF object file to the global linker hash
3528
   table.  */
3529
3530
static bool
3531
ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3532
0
{
3533
0
  HDRR *symhdr;
3534
0
  bfd_size_type external_ext_size;
3535
0
  void * external_ext = NULL;
3536
0
  bfd_size_type esize;
3537
0
  char *ssext = NULL;
3538
0
  bool result;
3539
3540
0
  if (! ecoff_slurp_symbolic_header (abfd))
3541
0
    return false;
3542
3543
  /* If there are no symbols, we don't want it.  */
3544
0
  if (bfd_get_symcount (abfd) == 0)
3545
0
    return true;
3546
3547
0
  symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3548
3549
  /* Read in the external symbols and external strings.  */
3550
0
  if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0)
3551
0
    return false;
3552
0
  external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3553
0
  esize = symhdr->iextMax * external_ext_size;
3554
0
  external_ext = _bfd_malloc_and_read (abfd, esize, esize);
3555
0
  if (external_ext == NULL && esize != 0)
3556
0
    goto error_return;
3557
3558
0
  if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0)
3559
0
    goto error_return;
3560
0
  ssext = (char *) _bfd_malloc_and_read (abfd, symhdr->issExtMax,
3561
0
           symhdr->issExtMax);
3562
0
  if (ssext == NULL && symhdr->issExtMax != 0)
3563
0
    goto error_return;
3564
3565
0
  result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3566
3567
0
  free (ssext);
3568
0
  free (external_ext);
3569
0
  return result;
3570
3571
0
 error_return:
3572
0
  free (ssext);
3573
0
  free (external_ext);
3574
0
  return false;
3575
0
}
3576
3577
/* This is called if we used _bfd_generic_link_add_archive_symbols
3578
   because we were not dealing with an ECOFF archive.  */
3579
3580
static bool
3581
ecoff_link_check_archive_element (bfd *abfd,
3582
          struct bfd_link_info *info,
3583
          struct bfd_link_hash_entry *h,
3584
          const char *name,
3585
          bool *pneeded)
3586
0
{
3587
0
  *pneeded = false;
3588
3589
  /* Unlike the generic linker, we do not pull in elements because
3590
     of common symbols.  */
3591
0
  if (h->type != bfd_link_hash_undefined)
3592
0
    return true;
3593
3594
  /* Include this element?  */
3595
0
  if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
3596
0
    return true;
3597
0
  *pneeded = true;
3598
3599
0
  return ecoff_link_add_object_symbols (abfd, info);
3600
0
}
3601
3602
/* Add the symbols from an archive file to the global hash table.
3603
   This looks through the undefined symbols, looks each one up in the
3604
   archive hash table, and adds any associated object file.  We do not
3605
   use _bfd_generic_link_add_archive_symbols because ECOFF archives
3606
   already have a hash table, so there is no reason to construct
3607
   another one.  */
3608
3609
static bool
3610
ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
3611
0
{
3612
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3613
0
  const bfd_byte *raw_armap;
3614
0
  struct bfd_link_hash_entry **pundef;
3615
0
  unsigned int armap_count;
3616
0
  unsigned int armap_log;
3617
0
  unsigned int i;
3618
0
  const bfd_byte *hashtable;
3619
0
  const char *stringbase;
3620
3621
0
  if (! bfd_has_map (abfd))
3622
0
    {
3623
0
      bfd *first_one = bfd_openr_next_archived_file (abfd, NULL);
3624
3625
      /* An empty archive is a special case.  */
3626
0
      if (first_one == NULL)
3627
0
  return true;
3628
3629
0
      if (!_bfd_make_armap (abfd, first_one))
3630
0
  return false;
3631
0
    }
3632
3633
  /* If we don't have any raw data for this archive, as can happen on
3634
     Irix 4.0.5F, we call the generic routine.
3635
     FIXME: We should be more clever about this, since someday tdata
3636
     may get to something for a generic archive.  */
3637
0
  raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3638
0
  if (raw_armap == NULL)
3639
0
    return (_bfd_generic_link_add_archive_symbols
3640
0
      (abfd, info, ecoff_link_check_archive_element));
3641
3642
0
  armap_count = H_GET_32 (abfd, raw_armap);
3643
3644
0
  armap_log = 0;
3645
0
  for (i = 1; i < armap_count; i <<= 1)
3646
0
    armap_log++;
3647
0
  BFD_ASSERT (i == armap_count);
3648
3649
0
  hashtable = raw_armap + 4;
3650
0
  stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3651
3652
  /* Look through the list of undefined symbols.  */
3653
0
  pundef = &info->hash->undefs;
3654
0
  while (*pundef != NULL)
3655
0
    {
3656
0
      struct bfd_link_hash_entry *h;
3657
0
      unsigned int hash, rehash = 0;
3658
0
      unsigned int file_offset;
3659
0
      const char *name;
3660
0
      bfd *element;
3661
3662
0
      h = *pundef;
3663
3664
      /* When a symbol is defined, it is not necessarily removed from
3665
   the list.  */
3666
0
      if (h->type != bfd_link_hash_undefined
3667
0
    && h->type != bfd_link_hash_common)
3668
0
  {
3669
    /* Remove this entry from the list, for general cleanliness
3670
       and because we are going to look through the list again
3671
       if we search any more libraries.  We can't remove the
3672
       entry if it is the tail, because that would lose any
3673
       entries we add to the list later on.  */
3674
0
    if (*pundef != info->hash->undefs_tail)
3675
0
      *pundef = (*pundef)->u.undef.next;
3676
0
    else
3677
0
      pundef = &(*pundef)->u.undef.next;
3678
0
    continue;
3679
0
  }
3680
3681
      /* Native ECOFF linkers do not pull in archive elements merely
3682
   to satisfy common definitions, so neither do we.  We leave
3683
   them on the list, though, in case we are linking against some
3684
   other object format.  */
3685
0
      if (h->type != bfd_link_hash_undefined)
3686
0
  {
3687
0
    pundef = &(*pundef)->u.undef.next;
3688
0
    continue;
3689
0
  }
3690
3691
      /* Look for this symbol in the archive hash table.  */
3692
0
      hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3693
0
             armap_log);
3694
3695
0
      file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3696
0
      if (file_offset == 0)
3697
0
  {
3698
    /* Nothing in this slot.  */
3699
0
    pundef = &(*pundef)->u.undef.next;
3700
0
    continue;
3701
0
  }
3702
3703
0
      name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3704
0
      if (name[0] != h->root.string[0]
3705
0
    || ! streq (name, h->root.string))
3706
0
  {
3707
0
    unsigned int srch;
3708
0
    bool found;
3709
3710
    /* That was the wrong symbol.  Try rehashing.  */
3711
0
    found = false;
3712
0
    for (srch = (hash + rehash) & (armap_count - 1);
3713
0
         srch != hash;
3714
0
         srch = (srch + rehash) & (armap_count - 1))
3715
0
      {
3716
0
        file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3717
0
        if (file_offset == 0)
3718
0
    break;
3719
0
        name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3720
0
        if (name[0] == h->root.string[0]
3721
0
      && streq (name, h->root.string))
3722
0
    {
3723
0
      found = true;
3724
0
      break;
3725
0
    }
3726
0
      }
3727
3728
0
    if (! found)
3729
0
      {
3730
0
        pundef = &(*pundef)->u.undef.next;
3731
0
        continue;
3732
0
      }
3733
3734
0
    hash = srch;
3735
0
  }
3736
3737
0
      element = (*backend->get_elt_at_filepos) (abfd,
3738
0
            (file_ptr) file_offset,
3739
0
            info);
3740
0
      if (element == NULL)
3741
0
  return false;
3742
3743
0
      if (! bfd_check_format (element, bfd_object))
3744
0
  return false;
3745
3746
      /* Unlike the generic linker, we know that this element provides
3747
   a definition for an undefined symbol and we know that we want
3748
   to include it.  We don't need to check anything.  */
3749
0
      if (!(*info->callbacks
3750
0
      ->add_archive_element) (info, element, name, &element))
3751
0
  return false;
3752
0
      if (! ecoff_link_add_object_symbols (element, info))
3753
0
  return false;
3754
3755
0
      pundef = &(*pundef)->u.undef.next;
3756
0
    }
3757
3758
0
  return true;
3759
0
}
3760
3761
/* Given an ECOFF BFD, add symbols to the global hash table as
3762
   appropriate.  */
3763
3764
bool
3765
_bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
3766
0
{
3767
0
  switch (bfd_get_format (abfd))
3768
0
    {
3769
0
    case bfd_object:
3770
0
      return ecoff_link_add_object_symbols (abfd, info);
3771
0
    case bfd_archive:
3772
0
      return ecoff_link_add_archive_symbols (abfd, info);
3773
0
    default:
3774
0
      bfd_set_error (bfd_error_wrong_format);
3775
0
      return false;
3776
0
    }
3777
0
}
3778
3779

3780
/* ECOFF final link routines.  */
3781
3782
/* Structure used to pass information to ecoff_link_write_external.  */
3783
3784
struct extsym_info
3785
{
3786
  bfd *abfd;
3787
  struct bfd_link_info *info;
3788
};
3789
3790
/* Accumulate the debugging information for an input BFD into the
3791
   output BFD.  This must read in the symbolic information of the
3792
   input BFD.  */
3793
3794
static bool
3795
ecoff_final_link_debug_accumulate (bfd *output_bfd,
3796
           bfd *input_bfd,
3797
           struct bfd_link_info *info,
3798
           void * handle)
3799
0
{
3800
0
  struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
3801
0
  const struct ecoff_debug_swap * const swap =
3802
0
    &ecoff_backend (input_bfd)->debug_swap;
3803
0
  HDRR *symhdr = &debug->symbolic_header;
3804
0
  bool ret;
3805
3806
0
#define READ(ptr, offset, count, size)          \
3807
0
  do                  \
3808
0
    {                 \
3809
0
      size_t amt;             \
3810
0
      debug->ptr = NULL;            \
3811
0
      if (symhdr->count == 0)           \
3812
0
  break;               \
3813
0
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
3814
0
  {               \
3815
0
    bfd_set_error (bfd_error_file_too_big);     \
3816
0
    ret = false;              \
3817
0
    goto return_something;          \
3818
0
  }                \
3819
0
      if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0)   \
3820
0
  {               \
3821
0
    ret = false;              \
3822
0
    goto return_something;          \
3823
0
  }                \
3824
0
      debug->ptr = _bfd_malloc_and_read (input_bfd, amt + 1, amt);  \
3825
0
      if (debug->ptr == NULL)           \
3826
0
  {               \
3827
0
    ret = false;              \
3828
0
    goto return_something;          \
3829
0
  }                \
3830
0
      ((char *) debug->ptr)[amt] = 0;         \
3831
0
    } while (0)
3832
3833
  /* If alloc_syments is true, then the data was already by read by
3834
     _bfd_ecoff_slurp_symbolic_info.  */
3835
0
  if (!debug->alloc_syments)
3836
0
    {
3837
0
      READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
3838
0
      READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
3839
0
      READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
3840
0
      READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
3841
0
      READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
3842
0
      READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
3843
0
      READ (ss, cbSsOffset, issMax, sizeof (char));
3844
0
      READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
3845
0
      READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
3846
0
    }
3847
0
#undef READ
3848
3849
  /* We do not read the external strings or the external symbols.  */
3850
3851
0
  ret = (bfd_ecoff_debug_accumulate
3852
0
   (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
3853
0
    &ecoff_backend (output_bfd)->debug_swap,
3854
0
    input_bfd, debug, swap, info));
3855
3856
0
 return_something:
3857
0
  _bfd_ecoff_free_ecoff_debug_info (debug);
3858
0
  return ret;
3859
0
}
3860
3861
/* Relocate and write an ECOFF section into an ECOFF output file.  */
3862
3863
static bool
3864
ecoff_indirect_link_order (bfd *output_bfd,
3865
         struct bfd_link_info *info,
3866
         asection *output_section,
3867
         struct bfd_link_order *link_order)
3868
0
{
3869
0
  asection *input_section;
3870
0
  bfd *input_bfd;
3871
0
  bfd_byte *contents = NULL;
3872
0
  bfd_size_type external_reloc_size;
3873
0
  bfd_size_type external_relocs_size;
3874
0
  void * external_relocs = NULL;
3875
3876
0
  BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
3877
3878
0
  input_section = link_order->u.indirect.section;
3879
0
  input_bfd = input_section->owner;
3880
0
  if (input_section->size == 0)
3881
0
    return true;
3882
3883
0
  BFD_ASSERT (input_section->output_section == output_section);
3884
0
  BFD_ASSERT (input_section->output_offset == link_order->offset);
3885
0
  BFD_ASSERT (input_section->size == link_order->size);
3886
3887
  /* Get the section contents.  */
3888
0
  if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
3889
0
    goto error_return;
3890
3891
  /* Get the relocs.  If we are relaxing MIPS code, they will already
3892
     have been read in.  Otherwise, we read them in now.  */
3893
0
  external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
3894
0
  external_relocs_size = external_reloc_size * input_section->reloc_count;
3895
3896
0
  if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0)
3897
0
    goto error_return;
3898
0
  external_relocs = _bfd_malloc_and_read (input_bfd, external_relocs_size,
3899
0
            external_relocs_size);
3900
0
  if (external_relocs == NULL && external_relocs_size != 0)
3901
0
    goto error_return;
3902
3903
  /* Relocate the section contents.  */
3904
0
  if (! ((*ecoff_backend (input_bfd)->relocate_section)
3905
0
   (output_bfd, info, input_bfd, input_section, contents,
3906
0
    external_relocs)))
3907
0
    goto error_return;
3908
3909
  /* Write out the relocated section.  */
3910
0
  if (! bfd_set_section_contents (output_bfd,
3911
0
          output_section,
3912
0
          contents,
3913
0
          input_section->output_offset,
3914
0
          input_section->size))
3915
0
    goto error_return;
3916
3917
  /* If we are producing relocatable output, the relocs were
3918
     modified, and we write them out now.  We use the reloc_count
3919
     field of output_section to keep track of the number of relocs we
3920
     have output so far.  */
3921
0
  if (bfd_link_relocatable (info))
3922
0
    {
3923
0
      file_ptr pos = (output_section->rel_filepos
3924
0
          + output_section->reloc_count * external_reloc_size);
3925
0
      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3926
0
    || (bfd_write (external_relocs, external_relocs_size, output_bfd)
3927
0
        != external_relocs_size))
3928
0
  goto error_return;
3929
0
      output_section->reloc_count += input_section->reloc_count;
3930
0
    }
3931
3932
0
  free (contents);
3933
0
  free (external_relocs);
3934
0
  return true;
3935
3936
0
 error_return:
3937
0
  free (contents);
3938
0
  free (external_relocs);
3939
0
  return false;
3940
0
}
3941
3942
/* Generate a reloc when linking an ECOFF file.  This is a reloc
3943
   requested by the linker, and does come from any input file.  This
3944
   is used to build constructor and destructor tables when linking
3945
   with -Ur.  */
3946
3947
static bool
3948
ecoff_reloc_link_order (bfd *output_bfd,
3949
      struct bfd_link_info *info,
3950
      asection *output_section,
3951
      struct bfd_link_order *link_order)
3952
0
{
3953
0
  enum bfd_link_order_type type;
3954
0
  asection *section;
3955
0
  bfd_vma addend;
3956
0
  arelent rel;
3957
0
  struct internal_reloc in;
3958
0
  bfd_size_type external_reloc_size;
3959
0
  bfd_byte *rbuf;
3960
0
  bool ok;
3961
0
  file_ptr pos;
3962
3963
0
  type = link_order->type;
3964
0
  section = NULL;
3965
0
  addend = link_order->u.reloc.p->addend;
3966
3967
  /* We set up an arelent to pass to the backend adjust_reloc_out
3968
     routine.  */
3969
0
  rel.address = link_order->offset;
3970
3971
0
  rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
3972
0
  if (rel.howto == 0)
3973
0
    {
3974
0
      bfd_set_error (bfd_error_bad_value);
3975
0
      return false;
3976
0
    }
3977
3978
0
  if (type == bfd_section_reloc_link_order)
3979
0
    {
3980
0
      section = link_order->u.reloc.p->u.section;
3981
0
      rel.sym_ptr_ptr = &section->symbol;
3982
0
    }
3983
0
  else
3984
0
    {
3985
0
      struct bfd_link_hash_entry *h;
3986
3987
      /* Treat a reloc against a defined symbol as though it were
3988
   actually against the section.  */
3989
0
      h = bfd_wrapped_link_hash_lookup (output_bfd, info,
3990
0
          link_order->u.reloc.p->u.name,
3991
0
          false, false, false);
3992
0
      if (h != NULL
3993
0
    && (h->type == bfd_link_hash_defined
3994
0
        || h->type == bfd_link_hash_defweak))
3995
0
  {
3996
0
    type = bfd_section_reloc_link_order;
3997
0
    section = h->u.def.section->output_section;
3998
    /* It seems that we ought to add the symbol value to the
3999
       addend here, but in practice it has already been added
4000
       because it was passed to constructor_callback.  */
4001
0
    addend += section->vma + h->u.def.section->output_offset;
4002
0
  }
4003
0
      else
4004
0
  {
4005
    /* We can't set up a reloc against a symbol correctly,
4006
       because we have no asymbol structure.  Currently no
4007
       adjust_reloc_out routine cares.  */
4008
0
    rel.sym_ptr_ptr = NULL;
4009
0
  }
4010
0
    }
4011
4012
  /* All ECOFF relocs are in-place.  Put the addend into the object
4013
     file.  */
4014
4015
0
  BFD_ASSERT (rel.howto->partial_inplace);
4016
0
  if (addend != 0)
4017
0
    {
4018
0
      bfd_size_type size;
4019
0
      bfd_reloc_status_type rstat;
4020
0
      bfd_byte *buf;
4021
4022
0
      size = bfd_get_reloc_size (rel.howto);
4023
0
      buf = (bfd_byte *) bfd_zmalloc (size);
4024
0
      if (buf == NULL && size != 0)
4025
0
  return false;
4026
0
      rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4027
0
              (bfd_vma) addend, buf);
4028
0
      switch (rstat)
4029
0
  {
4030
0
  case bfd_reloc_ok:
4031
0
    break;
4032
0
  default:
4033
0
  case bfd_reloc_outofrange:
4034
0
    abort ();
4035
0
  case bfd_reloc_overflow:
4036
0
    (*info->callbacks->reloc_overflow)
4037
0
      (info, NULL,
4038
0
       (link_order->type == bfd_section_reloc_link_order
4039
0
        ? bfd_section_name (section)
4040
0
        : link_order->u.reloc.p->u.name),
4041
0
       rel.howto->name, addend, NULL, NULL, (bfd_vma) 0);
4042
0
    break;
4043
0
  }
4044
0
      ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
4045
0
             (file_ptr) link_order->offset, size);
4046
0
      free (buf);
4047
0
      if (! ok)
4048
0
  return false;
4049
0
    }
4050
4051
0
  rel.addend = 0;
4052
4053
  /* Move the information into an internal_reloc structure.  */
4054
0
  in.r_vaddr = rel.address + bfd_section_vma (output_section);
4055
0
  in.r_type = rel.howto->type;
4056
4057
0
  if (type == bfd_symbol_reloc_link_order)
4058
0
    {
4059
0
      struct ecoff_link_hash_entry *h;
4060
4061
0
      h = ((struct ecoff_link_hash_entry *)
4062
0
     bfd_wrapped_link_hash_lookup (output_bfd, info,
4063
0
           link_order->u.reloc.p->u.name,
4064
0
           false, false, true));
4065
0
      if (h != NULL
4066
0
    && h->indx != -1)
4067
0
  in.r_symndx = h->indx;
4068
0
      else
4069
0
  {
4070
0
    (*info->callbacks->unattached_reloc)
4071
0
      (info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
4072
0
    in.r_symndx = 0;
4073
0
  }
4074
0
      in.r_extern = 1;
4075
0
    }
4076
0
  else
4077
0
    {
4078
0
      const char *name;
4079
0
      unsigned int i;
4080
0
      static struct
4081
0
      {
4082
0
  const char * name;
4083
0
  long r_symndx;
4084
0
      }
4085
0
      section_symndx [] =
4086
0
      {
4087
0
  { _TEXT,   RELOC_SECTION_TEXT   },
4088
0
  { _RDATA,  RELOC_SECTION_RDATA  },
4089
0
  { _DATA,   RELOC_SECTION_DATA   },
4090
0
  { _SDATA,  RELOC_SECTION_SDATA  },
4091
0
  { _SBSS,   RELOC_SECTION_SBSS   },
4092
0
  { _BSS,    RELOC_SECTION_BSS    },
4093
0
  { _INIT,   RELOC_SECTION_INIT   },
4094
0
  { _LIT8,   RELOC_SECTION_LIT8   },
4095
0
  { _LIT4,   RELOC_SECTION_LIT4   },
4096
0
  { _XDATA,  RELOC_SECTION_XDATA  },
4097
0
  { _PDATA,  RELOC_SECTION_PDATA  },
4098
0
  { _FINI,   RELOC_SECTION_FINI   },
4099
0
  { _LITA,   RELOC_SECTION_LITA   },
4100
0
  { "*ABS*", RELOC_SECTION_ABS    },
4101
0
  { _RCONST, RELOC_SECTION_RCONST }
4102
0
      };
4103
4104
0
      name = bfd_section_name (section);
4105
4106
0
      for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
4107
0
  if (streq (name, section_symndx[i].name))
4108
0
    {
4109
0
      in.r_symndx = section_symndx[i].r_symndx;
4110
0
      break;
4111
0
    }
4112
4113
0
      if (i == ARRAY_SIZE (section_symndx))
4114
0
  abort ();
4115
4116
0
      in.r_extern = 0;
4117
0
    }
4118
4119
  /* Let the BFD backend adjust the reloc.  */
4120
0
  (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4121
4122
  /* Get some memory and swap out the reloc.  */
4123
0
  external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4124
0
  rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4125
0
  if (rbuf == NULL)
4126
0
    return false;
4127
4128
0
  (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
4129
4130
0
  pos = (output_section->rel_filepos
4131
0
   + output_section->reloc_count * external_reloc_size);
4132
0
  ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4133
0
  && (bfd_write (rbuf, external_reloc_size, output_bfd)
4134
0
      == external_reloc_size));
4135
4136
0
  if (ok)
4137
0
    ++output_section->reloc_count;
4138
4139
0
  free (rbuf);
4140
4141
0
  return ok;
4142
0
}
4143
4144
/* Put out information for an external symbol.  These come only from
4145
   the hash table.  */
4146
4147
static bool
4148
ecoff_link_write_external (struct bfd_hash_entry *bh, void * data)
4149
0
{
4150
0
  struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh;
4151
0
  struct extsym_info *einfo = (struct extsym_info *) data;
4152
0
  bfd *output_bfd = einfo->abfd;
4153
0
  bool strip;
4154
4155
0
  if (h->root.type == bfd_link_hash_warning)
4156
0
    {
4157
0
      h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4158
0
      if (h->root.type == bfd_link_hash_new)
4159
0
  return true;
4160
0
    }
4161
4162
  /* We need to check if this symbol is being stripped.  */
4163
0
  if (h->root.type == bfd_link_hash_undefined
4164
0
      || h->root.type == bfd_link_hash_undefweak)
4165
0
    strip = false;
4166
0
  else if (einfo->info->strip == strip_all
4167
0
     || (einfo->info->strip == strip_some
4168
0
         && bfd_hash_lookup (einfo->info->keep_hash,
4169
0
           h->root.root.string,
4170
0
           false, false) == NULL))
4171
0
    strip = true;
4172
0
  else
4173
0
    strip = false;
4174
4175
0
  if (strip || h->written)
4176
0
    return true;
4177
4178
0
  if (h->abfd == NULL)
4179
0
    {
4180
0
      h->esym.jmptbl = 0;
4181
0
      h->esym.cobol_main = 0;
4182
0
      h->esym.weakext = 0;
4183
0
      h->esym.reserved = 0;
4184
0
      h->esym.ifd = ifdNil;
4185
0
      h->esym.asym.value = 0;
4186
0
      h->esym.asym.st = stGlobal;
4187
4188
0
      if (h->root.type != bfd_link_hash_defined
4189
0
    && h->root.type != bfd_link_hash_defweak)
4190
0
  h->esym.asym.sc = scAbs;
4191
0
      else
4192
0
  {
4193
0
    asection *output_section;
4194
0
    const char *name;
4195
0
    unsigned int i;
4196
0
    static struct
4197
0
    {
4198
0
      const char * name;
4199
0
      int sc;
4200
0
    }
4201
0
    section_storage_classes [] =
4202
0
    {
4203
0
      { _TEXT,   scText   },
4204
0
      { _DATA,   scData   },
4205
0
      { _SDATA,  scSData  },
4206
0
      { _RDATA,  scRData  },
4207
0
      { _BSS,    scBss    },
4208
0
      { _SBSS,   scSBss   },
4209
0
      { _INIT,   scInit   },
4210
0
      { _FINI,   scFini   },
4211
0
      { _PDATA,  scPData  },
4212
0
      { _XDATA,  scXData  },
4213
0
      { _RCONST, scRConst }
4214
0
    };
4215
4216
0
    output_section = h->root.u.def.section->output_section;
4217
0
    name = bfd_section_name (output_section);
4218
4219
0
    for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++)
4220
0
      if (streq (name, section_storage_classes[i].name))
4221
0
        {
4222
0
    h->esym.asym.sc = section_storage_classes[i].sc;
4223
0
    break;
4224
0
        }
4225
4226
0
    if (i == ARRAY_SIZE (section_storage_classes))
4227
0
      h->esym.asym.sc = scAbs;
4228
0
  }
4229
4230
0
      h->esym.asym.reserved = 0;
4231
0
      h->esym.asym.index = indexNil;
4232
0
    }
4233
0
  else if (h->esym.ifd != -1)
4234
0
    {
4235
0
      struct ecoff_debug_info *debug;
4236
4237
      /* Adjust the FDR index for the symbol by that used for the
4238
   input BFD.  */
4239
0
      debug = &ecoff_data (h->abfd)->debug_info;
4240
0
      BFD_ASSERT (h->esym.ifd >= 0
4241
0
      && h->esym.ifd < debug->symbolic_header.ifdMax);
4242
0
      h->esym.ifd = debug->ifdmap[h->esym.ifd];
4243
0
    }
4244
4245
0
  switch (h->root.type)
4246
0
    {
4247
0
    default:
4248
0
    case bfd_link_hash_warning:
4249
0
    case bfd_link_hash_new:
4250
0
      abort ();
4251
0
    case bfd_link_hash_undefined:
4252
0
    case bfd_link_hash_undefweak:
4253
0
      if (h->esym.asym.sc != scUndefined
4254
0
    && h->esym.asym.sc != scSUndefined)
4255
0
  h->esym.asym.sc = scUndefined;
4256
0
      break;
4257
0
    case bfd_link_hash_defined:
4258
0
    case bfd_link_hash_defweak:
4259
0
      if (h->esym.asym.sc == scUndefined
4260
0
    || h->esym.asym.sc == scSUndefined)
4261
0
  h->esym.asym.sc = scAbs;
4262
0
      else if (h->esym.asym.sc == scCommon)
4263
0
  h->esym.asym.sc = scBss;
4264
0
      else if (h->esym.asym.sc == scSCommon)
4265
0
  h->esym.asym.sc = scSBss;
4266
0
      h->esym.asym.value = (h->root.u.def.value
4267
0
          + h->root.u.def.section->output_section->vma
4268
0
          + h->root.u.def.section->output_offset);
4269
0
      break;
4270
0
    case bfd_link_hash_common:
4271
0
      if (h->esym.asym.sc != scCommon
4272
0
    && h->esym.asym.sc != scSCommon)
4273
0
  h->esym.asym.sc = scCommon;
4274
0
      h->esym.asym.value = h->root.u.c.size;
4275
0
      break;
4276
0
    case bfd_link_hash_indirect:
4277
      /* We ignore these symbols, since the indirected symbol is
4278
   already in the hash table.  */
4279
0
      return true;
4280
0
    }
4281
4282
  /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4283
     symbol number.  */
4284
0
  h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4285
0
  h->written = 1;
4286
4287
0
  return (bfd_ecoff_debug_one_external
4288
0
    (output_bfd, &ecoff_data (output_bfd)->debug_info,
4289
0
     &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4290
0
     &h->esym));
4291
0
}
4292
4293
/* ECOFF final link routine.  This looks through all the input BFDs
4294
   and gathers together all the debugging information, and then
4295
   processes all the link order information.  This may cause it to
4296
   close and reopen some input BFDs; I'll see how bad this is.  */
4297
4298
bool
4299
_bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
4300
0
{
4301
0
  const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4302
0
  struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4303
0
  HDRR *symhdr;
4304
0
  void * handle;
4305
0
  bfd *input_bfd;
4306
0
  asection *o;
4307
0
  struct bfd_link_order *p;
4308
0
  struct extsym_info einfo;
4309
4310
  /* We accumulate the debugging information counts in the symbolic
4311
     header.  */
4312
0
  symhdr = &debug->symbolic_header;
4313
0
  symhdr->vstamp = 0;
4314
0
  symhdr->ilineMax = 0;
4315
0
  symhdr->cbLine = 0;
4316
0
  symhdr->idnMax = 0;
4317
0
  symhdr->ipdMax = 0;
4318
0
  symhdr->isymMax = 0;
4319
0
  symhdr->ioptMax = 0;
4320
0
  symhdr->iauxMax = 0;
4321
0
  symhdr->issMax = 0;
4322
0
  symhdr->issExtMax = 0;
4323
0
  symhdr->ifdMax = 0;
4324
0
  symhdr->crfd = 0;
4325
0
  symhdr->iextMax = 0;
4326
4327
  /* We accumulate the debugging information itself in the debug_info
4328
     structure.  */
4329
0
  debug->line = NULL;
4330
0
  debug->external_dnr = NULL;
4331
0
  debug->external_pdr = NULL;
4332
0
  debug->external_sym = NULL;
4333
0
  debug->external_opt = NULL;
4334
0
  debug->external_aux = NULL;
4335
0
  debug->ss = NULL;
4336
0
  debug->ssext = debug->ssext_end = NULL;
4337
0
  debug->external_fdr = NULL;
4338
0
  debug->external_rfd = NULL;
4339
0
  debug->external_ext = debug->external_ext_end = NULL;
4340
4341
0
  handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4342
0
  if (handle == NULL)
4343
0
    return false;
4344
4345
  /* Accumulate the debugging symbols from each input BFD.  */
4346
0
  for (input_bfd = info->input_bfds;
4347
0
       input_bfd != NULL;
4348
0
       input_bfd = input_bfd->link.next)
4349
0
    {
4350
0
      bool ret;
4351
4352
0
      if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4353
0
  {
4354
    /* Arbitrarily set the symbolic header vstamp to the vstamp
4355
       of the first object file in the link.  */
4356
0
    if (symhdr->vstamp == 0)
4357
0
      symhdr->vstamp
4358
0
        = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4359
0
    ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4360
0
               handle);
4361
0
  }
4362
0
      else
4363
0
  ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4364
0
            debug, &backend->debug_swap,
4365
0
            input_bfd, info);
4366
0
      if (! ret)
4367
0
  return false;
4368
4369
      /* Combine the register masks.  */
4370
0
      ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4371
0
      ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4372
0
      ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4373
0
      ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4374
0
      ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4375
0
      ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4376
0
    }
4377
4378
  /* Write out the external symbols.  */
4379
0
  einfo.abfd = abfd;
4380
0
  einfo.info = info;
4381
0
  bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo);
4382
4383
0
  if (bfd_link_relocatable (info))
4384
0
    {
4385
      /* We need to make a pass over the link_orders to count up the
4386
   number of relocations we will need to output, so that we know
4387
   how much space they will take up.  */
4388
0
      for (o = abfd->sections; o != NULL; o = o->next)
4389
0
  {
4390
0
    o->reloc_count = 0;
4391
0
    for (p = o->map_head.link_order;
4392
0
         p != NULL;
4393
0
         p = p->next)
4394
0
      if (p->type == bfd_indirect_link_order)
4395
0
        o->reloc_count += p->u.indirect.section->reloc_count;
4396
0
      else if (p->type == bfd_section_reloc_link_order
4397
0
         || p->type == bfd_symbol_reloc_link_order)
4398
0
        ++o->reloc_count;
4399
0
  }
4400
0
    }
4401
4402
  /* Compute the reloc and symbol file positions.  */
4403
0
  ecoff_compute_reloc_file_positions (abfd);
4404
4405
  /* Write out the debugging information.  */
4406
0
  if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4407
0
             &backend->debug_swap, info,
4408
0
             ecoff_data (abfd)->sym_filepos))
4409
0
    return false;
4410
4411
0
  bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4412
4413
0
  if (bfd_link_relocatable (info))
4414
0
    {
4415
      /* Now reset the reloc_count field of the sections in the output
4416
   BFD to 0, so that we can use them to keep track of how many
4417
   relocs we have output thus far.  */
4418
0
      for (o = abfd->sections; o != NULL; o = o->next)
4419
0
  o->reloc_count = 0;
4420
0
    }
4421
4422
  /* Get a value for the GP register.  */
4423
0
  if (ecoff_data (abfd)->gp == 0)
4424
0
    {
4425
0
      struct bfd_link_hash_entry *h;
4426
4427
0
      h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4428
0
      if (h != NULL
4429
0
    && h->type == bfd_link_hash_defined)
4430
0
  ecoff_data (abfd)->gp = (h->u.def.value
4431
0
         + h->u.def.section->output_section->vma
4432
0
         + h->u.def.section->output_offset);
4433
0
      else if (bfd_link_relocatable (info))
4434
0
  {
4435
0
    bfd_vma lo;
4436
4437
    /* Make up a value.  */
4438
0
    lo = (bfd_vma) -1;
4439
0
    for (o = abfd->sections; o != NULL; o = o->next)
4440
0
      {
4441
0
        if (o->vma < lo
4442
0
      && (streq (o->name, _SBSS)
4443
0
          || streq (o->name, _SDATA)
4444
0
          || streq (o->name, _LIT4)
4445
0
          || streq (o->name, _LIT8)
4446
0
          || streq (o->name, _LITA)))
4447
0
    lo = o->vma;
4448
0
      }
4449
0
    ecoff_data (abfd)->gp = lo + 0x8000;
4450
0
  }
4451
0
      else
4452
0
  {
4453
    /* If the relocate_section function needs to do a reloc
4454
       involving the GP value, it should make a reloc_dangerous
4455
       callback to warn that GP is not defined.  */
4456
0
  }
4457
0
    }
4458
4459
0
  for (o = abfd->sections; o != NULL; o = o->next)
4460
0
    {
4461
0
      for (p = o->map_head.link_order;
4462
0
     p != NULL;
4463
0
     p = p->next)
4464
0
  {
4465
0
    if (p->type == bfd_indirect_link_order
4466
0
        && (bfd_get_flavour (p->u.indirect.section->owner)
4467
0
      == bfd_target_ecoff_flavour))
4468
0
      {
4469
0
        if (! ecoff_indirect_link_order (abfd, info, o, p))
4470
0
    return false;
4471
0
      }
4472
0
    else if (p->type == bfd_section_reloc_link_order
4473
0
       || p->type == bfd_symbol_reloc_link_order)
4474
0
      {
4475
0
        if (! ecoff_reloc_link_order (abfd, info, o, p))
4476
0
    return false;
4477
0
      }
4478
0
    else
4479
0
      {
4480
0
        if (! _bfd_default_link_order (abfd, info, o, p))
4481
0
    return false;
4482
0
      }
4483
0
  }
4484
0
    }
4485
4486
0
  abfd->symcount = symhdr->iextMax + symhdr->isymMax;
4487
4488
0
  ecoff_data (abfd)->linker = true;
4489
4490
  return true;
4491
0
}