Coverage Report

Created: 2026-07-12 09:22

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