Coverage Report

Created: 2025-06-24 06:45

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