OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [ecoff.c] - Blame information for rev 241

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.