1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Original version by Per Bothner.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include "aout/ranlib.h"
29 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
30 some other stuff which we don't want and which conflicts with stuff
33 #include "aout/aout64.h"
36 #undef obj_sym_filepos
38 #include "coff/internal.h"
40 #include "coff/symconst.h"
41 #include "coff/ecoff.h"
45 /* Prototypes for static functions. */
47 static void ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
48 asymbol *asym, int ext,
49 asymbol **indirect_ptr_ptr));
50 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
51 RNDXR *rndx, long isym,
53 static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
54 unsigned int indx, int bigendian));
55 static bfd_reloc_status_type ecoff_generic_reloc PARAMS ((bfd *abfd,
61 static bfd_reloc_status_type ecoff_refhi_reloc PARAMS ((bfd *abfd,
67 static bfd_reloc_status_type ecoff_reflo_reloc PARAMS ((bfd *abfd,
73 static bfd_reloc_status_type ecoff_gprel_reloc PARAMS ((bfd *abfd,
79 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
81 static void ecoff_clear_output_flags PARAMS ((bfd *abfd));
82 static boolean ecoff_rel PARAMS ((bfd *output_bfd, bfd_seclet_type *seclet,
83 asection *output_section, PTR data,
84 boolean relocateable));
85 static boolean ecoff_dump_seclet PARAMS ((bfd *abfd, bfd_seclet_type *seclet,
86 asection *section, PTR data,
87 boolean relocateable));
88 static long ecoff_add_string PARAMS ((bfd *output_bfd, FDR *fdr,
89 CONST char *string, boolean external));
90 static boolean ecoff_get_debug PARAMS ((bfd *output_bfd,
91 bfd_seclet_type *seclet,
93 boolean relocateable));
94 static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
95 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
100 /* How to process the various relocs types. */
102 static reloc_howto_type ecoff_howto_table[] =
104 /* Reloc type 0 is ignored. The reloc reading code ensures that
105 this is a reference to the .abs section, which will cause
106 bfd_perform_relocation to do nothing. */
107 HOWTO (ECOFF_R_IGNORE, /* type */
109 0, /* size (0 = byte, 1 = short, 2 = long) */
111 false, /* pc_relative */
113 complain_overflow_dont, /* complain_on_overflow */
114 0, /* special_function */
116 false, /* partial_inplace */
119 false), /* pcrel_offset */
121 /* A 16 bit reference to a symbol, normally from a data section. */
122 HOWTO (ECOFF_R_REFHALF, /* type */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
126 false, /* pc_relative */
128 complain_overflow_bitfield, /* complain_on_overflow */
129 ecoff_generic_reloc, /* special_function */
130 "REFHALF", /* name */
131 true, /* partial_inplace */
132 0xffff, /* src_mask */
133 0xffff, /* dst_mask */
134 false), /* pcrel_offset */
136 /* A 32 bit reference to a symbol, normally from a data section. */
137 HOWTO (ECOFF_R_REFWORD, /* type */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
141 false, /* pc_relative */
143 complain_overflow_bitfield, /* complain_on_overflow */
144 ecoff_generic_reloc, /* special_function */
145 "REFWORD", /* name */
146 true, /* partial_inplace */
147 0xffffffff, /* src_mask */
148 0xffffffff, /* dst_mask */
149 false), /* pcrel_offset */
151 /* A 26 bit absolute jump address. */
152 HOWTO (ECOFF_R_JMPADDR, /* type */
154 2, /* size (0 = byte, 1 = short, 2 = long) */
156 false, /* pc_relative */
158 complain_overflow_bitfield, /* complain_on_overflow */
159 ecoff_generic_reloc, /* special_function */
160 "JMPADDR", /* name */
161 true, /* partial_inplace */
162 0x3ffffff, /* src_mask */
163 0x3ffffff, /* dst_mask */
164 false), /* pcrel_offset */
166 /* The high 16 bits of a symbol value. Handled by the function
167 ecoff_refhi_reloc. */
168 HOWTO (ECOFF_R_REFHI, /* type */
170 2, /* size (0 = byte, 1 = short, 2 = long) */
172 false, /* pc_relative */
174 complain_overflow_bitfield, /* complain_on_overflow */
175 ecoff_refhi_reloc, /* special_function */
177 true, /* partial_inplace */
178 0xffff, /* src_mask */
179 0xffff, /* dst_mask */
180 false), /* pcrel_offset */
182 /* The low 16 bits of a symbol value. */
183 HOWTO (ECOFF_R_REFLO, /* type */
185 2, /* size (0 = byte, 1 = short, 2 = long) */
187 false, /* pc_relative */
189 complain_overflow_dont, /* complain_on_overflow */
190 ecoff_reflo_reloc, /* special_function */
192 true, /* partial_inplace */
193 0xffff, /* src_mask */
194 0xffff, /* dst_mask */
195 false), /* pcrel_offset */
197 /* A reference to an offset from the gp register. Handled by the
198 function ecoff_gprel_reloc. */
199 HOWTO (ECOFF_R_GPREL, /* type */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
203 false, /* pc_relative */
205 complain_overflow_signed, /* complain_on_overflow */
206 ecoff_gprel_reloc, /* special_function */
208 true, /* partial_inplace */
209 0xffff, /* src_mask */
210 0xffff, /* dst_mask */
211 false), /* pcrel_offset */
213 /* A reference to a literal using an offset from the gp register.
214 Handled by the function ecoff_gprel_reloc. */
215 HOWTO (ECOFF_R_LITERAL, /* type */
217 2, /* size (0 = byte, 1 = short, 2 = long) */
219 false, /* pc_relative */
221 complain_overflow_signed, /* complain_on_overflow */
222 ecoff_gprel_reloc, /* special_function */
223 "LITERAL", /* name */
224 true, /* partial_inplace */
225 0xffff, /* src_mask */
226 0xffff, /* dst_mask */
227 false) /* pcrel_offset */
230 #define ECOFF_HOWTO_COUNT \
231 (sizeof ecoff_howto_table / sizeof ecoff_howto_table[0])
233 /* This stuff is somewhat copied from coffcode.h. */
235 static asection bfd_debug_section = { "*DEBUG*" };
237 /* Create an ECOFF object. */
240 ecoff_mkobject (abfd)
243 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
244 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
245 if (abfd->tdata.ecoff_obj_data == NULL)
247 bfd_error = no_memory;
251 /* Always create a .scommon section for every BFD. This is a hack so
252 that the linker has something to attach scSCommon symbols to. */
253 bfd_make_section (abfd, SCOMMON);
258 /* This is a hook needed by SCO COFF, but we have nothing to do. */
261 ecoff_make_section_hook (abfd, name)
265 return (asection *) NULL;
268 /* Initialize a new section. */
271 ecoff_new_section_hook (abfd, section)
275 section->alignment_power = abfd->xvec->align_power_min;
277 if (strcmp (section->name, _TEXT) == 0)
278 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
279 else if (strcmp (section->name, _DATA) == 0
280 || strcmp (section->name, _SDATA) == 0)
281 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
282 else if (strcmp (section->name, _RDATA) == 0
283 || strcmp (section->name, _LIT8) == 0
284 || strcmp (section->name, _LIT4) == 0)
285 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
286 else if (strcmp (section->name, _BSS) == 0
287 || strcmp (section->name, _SBSS) == 0)
288 section->flags |= SEC_ALLOC;
290 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
291 uncertain about .init on some systems and I don't know how shared
297 /* Determine the machine architecture and type. */
300 ecoff_set_arch_mach_hook (abfd, filehdr)
304 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
305 enum bfd_architecture arch;
307 switch (internal_f->f_magic)
310 case MIPS_MAGIC_LITTLE:
312 arch = bfd_arch_mips;
316 arch = bfd_arch_alpha;
320 arch = bfd_arch_obscure;
324 bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
329 /* Get the section s_flags to use for a section. */
332 ecoff_sec_to_styp_flags (name, flags)
340 if (strcmp (name, _TEXT) == 0)
342 else if (strcmp (name, _DATA) == 0)
344 else if (strcmp (name, _SDATA) == 0)
346 else if (strcmp (name, _RDATA) == 0)
348 else if (strcmp (name, _LIT8) == 0)
350 else if (strcmp (name, _LIT4) == 0)
352 else if (strcmp (name, _BSS) == 0)
354 else if (strcmp (name, _SBSS) == 0)
356 else if (strcmp (name, _INIT) == 0)
357 styp = STYP_ECOFF_INIT;
358 else if (flags & SEC_CODE)
360 else if (flags & SEC_DATA)
362 else if (flags & SEC_READONLY)
364 else if (flags & SEC_LOAD)
369 if (flags & SEC_NEVER_LOAD)
375 /* Get the BFD flags to use for a section. */
378 ecoff_styp_to_sec_flags (abfd, hdr)
382 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
383 long styp_flags = internal_s->s_flags;
384 flagword sec_flags=0;
386 if (styp_flags & STYP_NOLOAD)
387 sec_flags |= SEC_NEVER_LOAD;
389 /* For 386 COFF, at least, an unloadable text or data section is
390 actually a shared library section. */
391 if ((styp_flags & STYP_TEXT)
392 || (styp_flags & STYP_ECOFF_INIT))
394 if (sec_flags & SEC_NEVER_LOAD)
395 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
397 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
399 else if ((styp_flags & STYP_DATA)
400 || (styp_flags & STYP_RDATA)
401 || (styp_flags & STYP_SDATA))
403 if (sec_flags & SEC_NEVER_LOAD)
404 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
406 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
407 if (styp_flags & STYP_RDATA)
408 sec_flags |= SEC_READONLY;
410 else if ((styp_flags & STYP_BSS)
411 || (styp_flags & STYP_SBSS))
413 sec_flags |= SEC_ALLOC;
415 else if (styp_flags & STYP_INFO)
417 sec_flags |= SEC_NEVER_LOAD;
419 else if ((styp_flags & STYP_LIT8)
420 || (styp_flags & STYP_LIT4))
422 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
426 sec_flags |= SEC_ALLOC | SEC_LOAD;
432 /* Routines to swap auxiliary information in and out. I am assuming
433 that the auxiliary information format is always going to be target
436 /* Swap in a type information record.
437 BIGEND says whether AUX symbols are big-endian or little-endian; this
438 info comes from the file header record (fh-fBigendian). */
441 ecoff_swap_tir_in (bigend, ext_copy, intern)
443 struct tir_ext *ext_copy;
446 struct tir_ext ext[1];
448 *ext = *ext_copy; /* Make it reasonable to do in-place. */
450 /* now the fun stuff... */
452 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
453 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
454 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
455 >> TIR_BITS1_BT_SH_BIG;
456 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
457 >> TIR_BITS_TQ4_SH_BIG;
458 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
459 >> TIR_BITS_TQ5_SH_BIG;
460 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
461 >> TIR_BITS_TQ0_SH_BIG;
462 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
463 >> TIR_BITS_TQ1_SH_BIG;
464 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
465 >> TIR_BITS_TQ2_SH_BIG;
466 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
467 >> TIR_BITS_TQ3_SH_BIG;
469 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
470 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
471 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
472 >> TIR_BITS1_BT_SH_LITTLE;
473 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
474 >> TIR_BITS_TQ4_SH_LITTLE;
475 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
476 >> TIR_BITS_TQ5_SH_LITTLE;
477 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
478 >> TIR_BITS_TQ0_SH_LITTLE;
479 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
480 >> TIR_BITS_TQ1_SH_LITTLE;
481 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
482 >> TIR_BITS_TQ2_SH_LITTLE;
483 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
484 >> TIR_BITS_TQ3_SH_LITTLE;
488 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
493 /* Swap out a type information record.
494 BIGEND says whether AUX symbols are big-endian or little-endian; this
495 info comes from the file header record (fh-fBigendian). */
498 ecoff_swap_tir_out (bigend, intern_copy, ext)
505 *intern = *intern_copy; /* Make it reasonable to do in-place. */
507 /* now the fun stuff... */
509 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
510 | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
511 | ((intern->bt << TIR_BITS1_BT_SH_BIG)
512 & TIR_BITS1_BT_BIG));
513 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
515 | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
516 & TIR_BITS_TQ5_BIG));
517 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
519 | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
520 & TIR_BITS_TQ1_BIG));
521 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
523 | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
524 & TIR_BITS_TQ3_BIG));
526 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
527 | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
528 | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
529 & TIR_BITS1_BT_LITTLE));
530 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
531 & TIR_BITS_TQ4_LITTLE)
532 | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
533 & TIR_BITS_TQ5_LITTLE));
534 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
535 & TIR_BITS_TQ0_LITTLE)
536 | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
537 & TIR_BITS_TQ1_LITTLE));
538 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
539 & TIR_BITS_TQ2_LITTLE)
540 | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
541 & TIR_BITS_TQ3_LITTLE));
545 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
550 /* Swap in a relative symbol record. BIGEND says whether it is in
551 big-endian or little-endian format.*/
554 ecoff_swap_rndx_in (bigend, ext_copy, intern)
556 struct rndx_ext *ext_copy;
559 struct rndx_ext ext[1];
561 *ext = *ext_copy; /* Make it reasonable to do in-place. */
563 /* now the fun stuff... */
565 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
566 | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
567 >> RNDX_BITS1_RFD_SH_BIG);
568 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
569 << RNDX_BITS1_INDEX_SH_LEFT_BIG)
570 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
571 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
573 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
574 | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
575 << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
576 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
577 >> RNDX_BITS1_INDEX_SH_LITTLE)
578 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
579 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
583 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
588 /* Swap out a relative symbol record. BIGEND says whether it is in
589 big-endian or little-endian format.*/
592 ecoff_swap_rndx_out (bigend, intern_copy, ext)
595 struct rndx_ext *ext;
599 *intern = *intern_copy; /* Make it reasonable to do in-place. */
601 /* now the fun stuff... */
603 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
604 ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
605 & RNDX_BITS1_RFD_BIG)
606 | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
607 & RNDX_BITS1_INDEX_BIG));
608 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
609 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
611 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
612 ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
613 & RNDX_BITS1_RFD_LITTLE)
614 | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
615 & RNDX_BITS1_INDEX_LITTLE));
616 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
617 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
621 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
626 /* Read in and swap the important symbolic information for an ECOFF
627 object file. This is called by gdb. */
630 ecoff_slurp_symbolic_info (abfd)
633 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
634 bfd_size_type external_hdr_size;
635 HDRR *internal_symhdr;
636 bfd_size_type raw_base;
637 bfd_size_type raw_size;
639 bfd_size_type external_fdr_size;
644 /* Check whether we've already gotten it, and whether there's any to
646 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
648 if (ecoff_data (abfd)->sym_filepos == 0)
650 bfd_get_symcount (abfd) = 0;
654 /* At this point bfd_get_symcount (abfd) holds the number of symbols
655 as read from the file header, but on ECOFF this is always the
656 size of the symbolic information header. It would be cleaner to
657 handle this when we first read the file in coffgen.c. */
658 external_hdr_size = backend->external_hdr_size;
659 if (bfd_get_symcount (abfd) != external_hdr_size)
661 bfd_error = bad_value;
665 /* Read the symbolic information header. */
666 raw = (PTR) alloca (external_hdr_size);
667 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
668 || (bfd_read (raw, external_hdr_size, 1, abfd)
669 != external_hdr_size))
671 bfd_error = system_call_error;
674 internal_symhdr = &ecoff_data (abfd)->symbolic_header;
675 (*backend->swap_hdr_in) (abfd, raw, internal_symhdr);
677 if (internal_symhdr->magic != backend->sym_magic)
679 bfd_error = bad_value;
683 /* Now we can get the correct number of symbols. */
684 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
685 + internal_symhdr->iextMax);
687 /* Read all the symbolic information at once. */
688 raw_base = ecoff_data (abfd)->sym_filepos + external_hdr_size;
690 if (internal_symhdr->cbExtOffset != 0)
691 raw_size = (internal_symhdr->cbExtOffset
693 + internal_symhdr->iextMax * backend->external_ext_size);
696 long cbline, issmax, issextmax;
698 cbline = (internal_symhdr->cbLine + 3) &~ 3;
699 issmax = (internal_symhdr->issMax + 3) &~ 3;
700 issextmax = (internal_symhdr->issExtMax + 3) &~ 3;
701 raw_size = (cbline * sizeof (unsigned char)
702 + internal_symhdr->idnMax * backend->external_dnr_size
703 + internal_symhdr->ipdMax * backend->external_pdr_size
704 + internal_symhdr->isymMax * backend->external_sym_size
705 + internal_symhdr->ioptMax * backend->external_opt_size
706 + internal_symhdr->iauxMax * sizeof (union aux_ext)
707 + issmax * sizeof (char)
708 + issextmax * sizeof (char)
709 + internal_symhdr->ifdMax * backend->external_fdr_size
710 + internal_symhdr->crfd * backend->external_rfd_size
711 + internal_symhdr->iextMax * backend->external_ext_size);
716 ecoff_data (abfd)->sym_filepos = 0;
719 raw = (PTR) bfd_alloc (abfd, raw_size);
722 bfd_error = no_memory;
725 if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
727 bfd_error = system_call_error;
728 bfd_release (abfd, raw);
732 ecoff_data (abfd)->raw_size = raw_size;
733 ecoff_data (abfd)->raw_syments = raw;
735 /* Get pointers for the numeric offsets in the HDRR structure. */
736 #define FIX(off1, off2, type) \
737 if (internal_symhdr->off1 == 0) \
738 ecoff_data (abfd)->off2 = (type) NULL; \
740 ecoff_data (abfd)->off2 = (type) ((char *) raw \
741 + internal_symhdr->off1 \
743 FIX (cbLineOffset, line, unsigned char *);
744 FIX (cbDnOffset, external_dnr, PTR);
745 FIX (cbPdOffset, external_pdr, PTR);
746 FIX (cbSymOffset, external_sym, PTR);
747 FIX (cbOptOffset, external_opt, PTR);
748 FIX (cbAuxOffset, external_aux, union aux_ext *);
749 FIX (cbSsOffset, ss, char *);
750 FIX (cbSsExtOffset, ssext, char *);
751 FIX (cbFdOffset, external_fdr, PTR);
752 FIX (cbRfdOffset, external_rfd, PTR);
753 FIX (cbExtOffset, external_ext, PTR);
756 /* I don't want to always swap all the data, because it will just
757 waste time and most programs will never look at it. The only
758 time the linker needs most of the debugging information swapped
759 is when linking big-endian and little-endian MIPS object files
760 together, which is not a common occurrence.
762 We need to look at the fdr to deal with a lot of information in
763 the symbols, so we swap them here. */
764 ecoff_data (abfd)->fdr =
765 (struct fdr *) bfd_alloc (abfd,
766 (internal_symhdr->ifdMax *
767 sizeof (struct fdr)));
768 if (ecoff_data (abfd)->fdr == NULL)
770 bfd_error = no_memory;
773 external_fdr_size = backend->external_fdr_size;
774 fdr_ptr = ecoff_data (abfd)->fdr;
775 fraw_src = (char *) ecoff_data (abfd)->external_fdr;
776 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
777 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
778 (*backend->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
783 /* ECOFF symbol table routines. The ECOFF symbol table is described
784 in gcc/mips-tfile.c. */
786 /* ECOFF uses two common sections. One is the usual one, and the
787 other is for small objects. All the small objects are kept
788 together, and then referenced via the gp pointer, which yields
789 faster assembler code. This is what we use for the small common
791 static asection ecoff_scom_section;
792 static asymbol ecoff_scom_symbol;
793 static asymbol *ecoff_scom_symbol_ptr;
795 /* Create an empty symbol. */
798 ecoff_make_empty_symbol (abfd)
801 ecoff_symbol_type *new;
803 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
804 if (new == (ecoff_symbol_type *) NULL)
806 bfd_error = no_memory;
807 return (asymbol *) NULL;
809 memset (new, 0, sizeof *new);
810 new->symbol.section = (asection *) NULL;
811 new->fdr = (FDR *) NULL;
814 new->symbol.the_bfd = abfd;
818 /* Set the BFD flags and section for an ECOFF symbol. */
821 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, indirect_ptr_ptr)
826 asymbol **indirect_ptr_ptr;
828 asym->the_bfd = abfd;
829 asym->value = ecoff_sym->value;
830 asym->section = &bfd_debug_section;
833 /* An indirect symbol requires two consecutive stabs symbols. */
834 if (*indirect_ptr_ptr != (asymbol *) NULL)
836 BFD_ASSERT (ECOFF_IS_STAB (ecoff_sym));
838 /* @@ Stuffing pointers into integers is a no-no.
839 We can usually get away with it if the integer is
840 large enough though. */
841 if (sizeof (asym) > sizeof (bfd_vma))
843 (*indirect_ptr_ptr)->value = (bfd_vma) asym;
845 asym->flags = BSF_DEBUGGING;
846 asym->section = &bfd_und_section;
847 *indirect_ptr_ptr = NULL;
851 if (ECOFF_IS_STAB (ecoff_sym)
852 && (ECOFF_UNMARK_STAB (ecoff_sym->index) | N_EXT) == (N_INDR | N_EXT))
854 asym->flags = BSF_DEBUGGING | BSF_INDIRECT;
855 asym->section = &bfd_ind_section;
856 /* Pass this symbol on to the next call to this function. */
857 *indirect_ptr_ptr = asym;
861 /* Most symbol types are just for debugging. */
862 switch (ecoff_sym->st)
871 if (ECOFF_IS_STAB (ecoff_sym))
873 asym->flags = BSF_DEBUGGING;
878 asym->flags = BSF_DEBUGGING;
883 asym->flags = BSF_EXPORT | BSF_GLOBAL;
885 asym->flags = BSF_LOCAL;
886 switch (ecoff_sym->sc)
889 /* Used for compiler generated labels. Leave them in the
890 debugging section, and mark them as local. If BSF_DEBUGGING
891 is set, then nm does not display them for some reason. If no
892 flags are set then the linker whines about them. */
893 asym->flags = BSF_LOCAL;
896 asym->section = bfd_make_section_old_way (abfd, ".text");
897 asym->value -= asym->section->vma;
900 asym->section = bfd_make_section_old_way (abfd, ".data");
901 asym->value -= asym->section->vma;
904 asym->section = bfd_make_section_old_way (abfd, ".bss");
905 asym->value -= asym->section->vma;
908 asym->flags = BSF_DEBUGGING;
911 asym->section = &bfd_abs_section;
914 asym->section = &bfd_und_section;
924 asym->flags = BSF_DEBUGGING;
927 asym->section = bfd_make_section_old_way (abfd, ".sdata");
928 asym->value -= asym->section->vma;
931 asym->section = bfd_make_section_old_way (abfd, ".sbss");
932 asym->value -= asym->section->vma;
935 asym->section = bfd_make_section_old_way (abfd, ".rdata");
936 asym->value -= asym->section->vma;
939 asym->flags = BSF_DEBUGGING;
942 if (asym->value > ecoff_data (abfd)->gp_size)
944 asym->section = &bfd_com_section;
950 if (ecoff_scom_section.name == NULL)
952 /* Initialize the small common section. */
953 ecoff_scom_section.name = SCOMMON;
954 ecoff_scom_section.flags = SEC_IS_COMMON;
955 ecoff_scom_section.output_section = &ecoff_scom_section;
956 ecoff_scom_section.symbol = &ecoff_scom_symbol;
957 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
958 ecoff_scom_symbol.name = SCOMMON;
959 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
960 ecoff_scom_symbol.section = &ecoff_scom_section;
961 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
963 asym->section = &ecoff_scom_section;
968 asym->flags = BSF_DEBUGGING;
971 asym->section = &bfd_und_section;
976 asym->section = bfd_make_section_old_way (abfd, ".init");
977 asym->value -= asym->section->vma;
982 asym->flags = BSF_DEBUGGING;
985 asym->section = bfd_make_section_old_way (abfd, ".fini");
986 asym->value -= asym->section->vma;
992 /* Look for special constructors symbols and make relocation entries
993 in a special construction section. These are produced by the
994 -fgnu-linker argument to g++. */
995 if (ECOFF_IS_STAB (ecoff_sym))
997 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
1009 arelent_chain *reloc_chain;
1010 unsigned int bitsize;
1013 /* Get a section with the same name as the symbol (usually
1014 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
1015 name ___CTOR_LIST (three underscores). We need
1016 __CTOR_LIST (two underscores), since ECOFF doesn't use
1017 a leading underscore. This should be handled by gcc,
1018 but instead we do it here. Actually, this should all
1019 be done differently anyhow. */
1020 name = bfd_asymbol_name (asym);
1021 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
1026 section = bfd_get_section_by_name (abfd, name);
1027 if (section == (asection *) NULL)
1031 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
1032 strcpy (copy, name);
1033 section = bfd_make_section (abfd, copy);
1036 /* Build a reloc pointing to this constructor. */
1038 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1039 reloc_chain->relent.sym_ptr_ptr =
1040 bfd_get_section (asym)->symbol_ptr_ptr;
1041 reloc_chain->relent.address = section->_raw_size;
1042 reloc_chain->relent.addend = asym->value;
1044 bitsize = ecoff_backend (abfd)->constructor_bitsize;
1048 reloc_index = ECOFF_R_REFWORD;
1055 reloc_chain->relent.howto = ecoff_howto_table + reloc_index;
1057 /* Set up the constructor section to hold the reloc. */
1058 section->flags = SEC_CONSTRUCTOR;
1059 ++section->reloc_count;
1061 /* Constructor sections must be rounded to a boundary
1062 based on the bitsize. These are not real sections--
1063 they are handled specially by the linker--so the ECOFF
1064 16 byte alignment restriction does not apply. */
1065 section->alignment_power = 1;
1066 while ((1 << section->alignment_power) < bitsize / 8)
1067 ++section->alignment_power;
1069 reloc_chain->next = section->constructor_chain;
1070 section->constructor_chain = reloc_chain;
1071 section->_raw_size += bitsize / 8;
1073 /* Mark the symbol as a constructor. */
1074 asym->flags |= BSF_CONSTRUCTOR;
1081 /* Read an ECOFF symbol table. */
1084 ecoff_slurp_symbol_table (abfd)
1087 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1088 const bfd_size_type external_ext_size = backend->external_ext_size;
1089 const bfd_size_type external_sym_size = backend->external_sym_size;
1090 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
1091 = backend->swap_ext_in;
1092 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
1093 = backend->swap_sym_in;
1094 bfd_size_type internal_size;
1095 ecoff_symbol_type *internal;
1096 ecoff_symbol_type *internal_ptr;
1097 asymbol *indirect_ptr;
1103 /* If we've already read in the symbol table, do nothing. */
1104 if (ecoff_data (abfd)->canonical_symbols != NULL)
1107 /* Get the symbolic information. */
1108 if (ecoff_slurp_symbolic_info (abfd) == false)
1110 if (bfd_get_symcount (abfd) == 0)
1113 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1114 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1115 if (internal == NULL)
1117 bfd_error = no_memory;
1121 internal_ptr = internal;
1122 indirect_ptr = NULL;
1123 eraw_src = (char *) ecoff_data (abfd)->external_ext;
1124 eraw_end = (eraw_src
1125 + (ecoff_data (abfd)->symbolic_header.iextMax
1126 * external_ext_size));
1127 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
1131 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
1132 internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
1133 + internal_esym.asym.iss);
1134 ecoff_set_symbol_info (abfd, &internal_esym.asym,
1135 &internal_ptr->symbol, 1, &indirect_ptr);
1136 /* The alpha uses a negative ifd field for section symbols. */
1137 if (internal_esym.ifd >= 0)
1138 internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
1140 internal_ptr->fdr = NULL;
1141 internal_ptr->local = false;
1142 internal_ptr->native = (PTR) eraw_src;
1144 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1146 /* The local symbols must be accessed via the fdr's, because the
1147 string and aux indices are relative to the fdr information. */
1148 fdr_ptr = ecoff_data (abfd)->fdr;
1149 fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
1150 for (; fdr_ptr < fdr_end; fdr_ptr++)
1155 lraw_src = ((char *) ecoff_data (abfd)->external_sym
1156 + fdr_ptr->isymBase * external_sym_size);
1157 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1159 lraw_src < lraw_end;
1160 lraw_src += external_sym_size, internal_ptr++)
1164 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1165 internal_ptr->symbol.name = (ecoff_data (abfd)->ss
1167 + internal_sym.iss);
1168 ecoff_set_symbol_info (abfd, &internal_sym,
1169 &internal_ptr->symbol, 0, &indirect_ptr);
1170 internal_ptr->fdr = fdr_ptr;
1171 internal_ptr->local = true;
1172 internal_ptr->native = (PTR) lraw_src;
1175 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1177 ecoff_data (abfd)->canonical_symbols = internal;
1182 /* Return the amount of space needed for the canonical symbols. */
1185 ecoff_get_symtab_upper_bound (abfd)
1188 if (ecoff_slurp_symbolic_info (abfd) == false
1189 || bfd_get_symcount (abfd) == 0)
1192 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1195 /* Get the canonicals symbols. */
1198 ecoff_get_symtab (abfd, alocation)
1200 asymbol **alocation;
1202 unsigned int counter = 0;
1203 ecoff_symbol_type *symbase;
1204 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1206 if (ecoff_slurp_symbol_table (abfd) == false
1207 || bfd_get_symcount (abfd) == 0)
1210 symbase = ecoff_data (abfd)->canonical_symbols;
1211 while (counter < bfd_get_symcount (abfd))
1213 *(location++) = symbase++;
1216 *location++ = (ecoff_symbol_type *) NULL;
1217 return bfd_get_symcount (abfd);
1220 /* Turn ECOFF type information into a printable string.
1221 ecoff_emit_aggregate and ecoff_type_to_string are from
1222 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1224 /* Write aggregate information to a string. */
1227 ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1234 int ifd = rndx->rfd;
1235 int indx = rndx->index;
1236 int sym_base, ss_base;
1242 sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
1243 ss_base = ecoff_data (abfd)->fdr[ifd].issBase;
1245 if (indx == indexNil)
1246 name = "/* no name */";
1249 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1253 (*backend->swap_sym_in) (abfd,
1254 ((char *) ecoff_data (abfd)->external_sym
1255 + indx * backend->external_sym_size),
1257 name = ecoff_data (abfd)->ss + ss_base + sym.iss;
1261 "%s %s { ifd = %d, index = %d }",
1263 indx + ecoff_data (abfd)->symbolic_header.iextMax);
1266 /* Convert the type information to string format. */
1269 ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1271 union aux_ext *aux_ptr;
1283 unsigned int basic_type;
1285 static char buffer1[1024];
1286 static char buffer2[1024];
1291 for (i = 0; i < 7; i++)
1293 qualifiers[i].low_bound = 0;
1294 qualifiers[i].high_bound = 0;
1295 qualifiers[i].stride = 0;
1298 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1299 return "-1 (no type)";
1300 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1302 basic_type = u.ti.bt;
1303 qualifiers[0].type = u.ti.tq0;
1304 qualifiers[1].type = u.ti.tq1;
1305 qualifiers[2].type = u.ti.tq2;
1306 qualifiers[3].type = u.ti.tq3;
1307 qualifiers[4].type = u.ti.tq4;
1308 qualifiers[5].type = u.ti.tq5;
1309 qualifiers[6].type = tqNil;
1312 * Go get the basic type.
1316 case btNil: /* undefined */
1320 case btAdr: /* address - integer same size as pointer */
1321 strcpy (p1, "address");
1324 case btChar: /* character */
1325 strcpy (p1, "char");
1328 case btUChar: /* unsigned character */
1329 strcpy (p1, "unsigned char");
1332 case btShort: /* short */
1333 strcpy (p1, "short");
1336 case btUShort: /* unsigned short */
1337 strcpy (p1, "unsigned short");
1340 case btInt: /* int */
1344 case btUInt: /* unsigned int */
1345 strcpy (p1, "unsigned int");
1348 case btLong: /* long */
1349 strcpy (p1, "long");
1352 case btULong: /* unsigned long */
1353 strcpy (p1, "unsigned long");
1356 case btFloat: /* float (real) */
1357 strcpy (p1, "float");
1360 case btDouble: /* Double (real) */
1361 strcpy (p1, "double");
1364 /* Structures add 1-2 aux words:
1365 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1366 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1368 case btStruct: /* Structure (Record) */
1369 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1370 ecoff_emit_aggregate (abfd, p1, &rndx,
1371 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1373 indx++; /* skip aux words */
1376 /* Unions add 1-2 aux words:
1377 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1378 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1380 case btUnion: /* Union */
1381 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1382 ecoff_emit_aggregate (abfd, p1, &rndx,
1383 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1385 indx++; /* skip aux words */
1388 /* Enumerations add 1-2 aux words:
1389 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1390 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1392 case btEnum: /* Enumeration */
1393 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1394 ecoff_emit_aggregate (abfd, p1, &rndx,
1395 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1397 indx++; /* skip aux words */
1400 case btTypedef: /* defined via a typedef, isymRef points */
1401 strcpy (p1, "typedef");
1404 case btRange: /* subrange of int */
1405 strcpy (p1, "subrange");
1408 case btSet: /* pascal sets */
1412 case btComplex: /* fortran complex */
1413 strcpy (p1, "complex");
1416 case btDComplex: /* fortran double complex */
1417 strcpy (p1, "double complex");
1420 case btIndirect: /* forward or unnamed typedef */
1421 strcpy (p1, "forward/unamed typedef");
1424 case btFixedDec: /* Fixed Decimal */
1425 strcpy (p1, "fixed decimal");
1428 case btFloatDec: /* Float Decimal */
1429 strcpy (p1, "float decimal");
1432 case btString: /* Varying Length Character String */
1433 strcpy (p1, "string");
1436 case btBit: /* Aligned Bit String */
1440 case btPicture: /* Picture */
1441 strcpy (p1, "picture");
1444 case btVoid: /* Void */
1445 strcpy (p1, "void");
1449 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1453 p1 += strlen (buffer1);
1456 * If this is a bitfield, get the bitsize.
1462 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1463 sprintf (p1, " : %d", bitsize);
1464 p1 += strlen (buffer1);
1469 * Deal with any qualifiers.
1471 if (qualifiers[0].type != tqNil)
1474 * Snarf up any array bounds in the correct order. Arrays
1475 * store 5 successive words in the aux. table:
1476 * word 0 RNDXR to type of the bounds (ie, int)
1477 * word 1 Current file descriptor index
1479 * word 3 high bound (or -1 if [])
1480 * word 4 stride size in bits
1482 for (i = 0; i < 7; i++)
1484 if (qualifiers[i].type == tqArray)
1486 qualifiers[i].low_bound =
1487 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1488 qualifiers[i].high_bound =
1489 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1490 qualifiers[i].stride =
1491 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1497 * Now print out the qualifiers.
1499 for (i = 0; i < 6; i++)
1501 switch (qualifiers[i].type)
1508 strcpy (p2, "ptr to ");
1509 p2 += sizeof ("ptr to ")-1;
1513 strcpy (p2, "volatile ");
1514 p2 += sizeof ("volatile ")-1;
1518 strcpy (p2, "far ");
1519 p2 += sizeof ("far ")-1;
1523 strcpy (p2, "func. ret. ");
1524 p2 += sizeof ("func. ret. ");
1529 int first_array = i;
1532 /* Print array bounds reversed (ie, in the order the C
1533 programmer writes them). C is such a fun language.... */
1535 while (i < 5 && qualifiers[i+1].type == tqArray)
1538 for (j = i; j >= first_array; j--)
1540 strcpy (p2, "array [");
1541 p2 += sizeof ("array [")-1;
1542 if (qualifiers[j].low_bound != 0)
1544 "%ld:%ld {%ld bits}",
1545 (long) qualifiers[j].low_bound,
1546 (long) qualifiers[j].high_bound,
1547 (long) qualifiers[j].stride);
1549 else if (qualifiers[j].high_bound != -1)
1552 (long) (qualifiers[j].high_bound + 1),
1553 (long) (qualifiers[j].stride));
1556 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1559 strcpy (p2, "] of ");
1560 p2 += sizeof ("] of ")-1;
1568 strcpy (p2, buffer1);
1572 /* Return information about ECOFF symbol SYMBOL in RET. */
1575 ecoff_get_symbol_info (abfd, symbol, ret)
1576 bfd *abfd; /* Ignored. */
1580 bfd_symbol_info (symbol, ret);
1583 /* Print information about an ECOFF symbol. */
1586 ecoff_print_symbol (abfd, filep, symbol, how)
1590 bfd_print_symbol_type how;
1592 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1593 FILE *file = (FILE *)filep;
1597 case bfd_print_symbol_name:
1598 fprintf (file, "%s", symbol->name);
1600 case bfd_print_symbol_more:
1601 if (ecoffsymbol (symbol)->local)
1605 (*backend->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1607 fprintf (file, "ecoff local ");
1608 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1609 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1610 (unsigned) ecoff_sym.sc);
1616 (*backend->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1618 fprintf (file, "ecoff extern ");
1619 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1620 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1621 (unsigned) ecoff_ext.asym.sc);
1624 case bfd_print_symbol_all:
1625 /* Print out the symbols in a reasonable way */
1634 if (ecoffsymbol (symbol)->local)
1636 (*backend->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1639 pos = ((((char *) ecoffsymbol (symbol)->native
1640 - (char *) ecoff_data (abfd)->external_sym)
1641 / backend->external_sym_size)
1642 + ecoff_data (abfd)->symbolic_header.iextMax);
1649 (*backend->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1652 pos = (((char *) ecoffsymbol (symbol)->native
1653 - (char *) ecoff_data (abfd)->external_ext)
1654 / backend->external_ext_size);
1655 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1656 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1657 weakext = ecoff_ext.weakext ? 'w' : ' ';
1660 fprintf (file, "[%3d] %c ",
1662 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1663 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1664 (unsigned) ecoff_ext.asym.st,
1665 (unsigned) ecoff_ext.asym.sc,
1666 (unsigned) ecoff_ext.asym.index,
1667 jmptbl, cobol_main, weakext,
1670 if (ecoffsymbol (symbol)->fdr != NULL
1671 && ecoff_ext.asym.index != indexNil)
1675 bfd_size_type sym_base;
1676 union aux_ext *aux_base;
1678 indx = ecoff_ext.asym.index;
1680 /* sym_base is used to map the fdr relative indices which
1681 appear in the file to the position number which we are
1683 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1684 if (ecoffsymbol (symbol)->local)
1685 sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1687 /* aux_base is the start of the aux entries for this file;
1688 asym.index is an offset from this. */
1689 aux_base = (ecoff_data (abfd)->external_aux
1690 + ecoffsymbol (symbol)->fdr->iauxBase);
1692 /* The aux entries are stored in host byte order; the
1693 order is indicated by a bit in the fdr. */
1694 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1696 /* This switch is basically from gcc/mips-tdump.c */
1697 switch (ecoff_ext.asym.st)
1705 fprintf (file, "\n End+1 symbol: %ld",
1706 (long) (indx + sym_base));
1710 if (ecoff_ext.asym.sc == scText
1711 || ecoff_ext.asym.sc == scInfo)
1712 fprintf (file, "\n First symbol: %ld",
1713 (long) (indx + sym_base));
1715 fprintf (file, "\n First symbol: %ld",
1716 (long) (AUX_GET_ISYM (bigendian,
1717 &aux_base[ecoff_ext.asym.index])
1723 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1725 else if (ecoffsymbol (symbol)->local)
1726 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1727 (long) (AUX_GET_ISYM (bigendian,
1728 &aux_base[ecoff_ext.asym.index])
1730 ecoff_type_to_string (abfd, aux_base, indx + 1,
1733 fprintf (file, "\n Local symbol: %d",
1736 + ecoff_data (abfd)->symbolic_header.iextMax));
1740 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1741 fprintf (file, "\n Type: %s",
1742 ecoff_type_to_string (abfd, aux_base, indx,
1752 /* ECOFF relocs are either against external symbols, or against
1753 sections. If we are producing relocateable output, and the reloc
1754 is against an external symbol, and nothing has given us any
1755 additional addend, the resulting reloc will also be against the
1756 same symbol. In such a case, we don't want to change anything
1757 about the way the reloc is handled, since it will all be done at
1758 final link time. Rather than put special case code into
1759 bfd_perform_relocation, all the reloc types use this howto
1760 function. It just short circuits the reloc if producing
1761 relocateable output against an external symbol. */
1763 static bfd_reloc_status_type
1764 ecoff_generic_reloc (abfd,
1771 arelent *reloc_entry;
1774 asection *input_section;
1777 if (output_bfd != (bfd *) NULL
1778 && (symbol->flags & BSF_SECTION_SYM) == 0
1779 && reloc_entry->addend == 0)
1781 reloc_entry->address += input_section->output_offset;
1782 return bfd_reloc_ok;
1785 return bfd_reloc_continue;
1788 /* Do a REFHI relocation. This has to be done in combination with a
1789 REFLO reloc, because there is a carry from the REFLO to the REFHI.
1790 Here we just save the information we need; we do the actual
1791 relocation when we see the REFLO. ECOFF requires that the REFLO
1792 immediately follow the REFHI, so this ought to work. */
1794 static bfd_byte *ecoff_refhi_addr;
1795 static bfd_vma ecoff_refhi_addend;
1797 static bfd_reloc_status_type
1798 ecoff_refhi_reloc (abfd,
1805 arelent *reloc_entry;
1808 asection *input_section;
1811 bfd_reloc_status_type ret;
1814 /* If we're relocating, and this an external symbol, we don't want
1815 to change anything. */
1816 if (output_bfd != (bfd *) NULL
1817 && (symbol->flags & BSF_SECTION_SYM) == 0
1818 && reloc_entry->addend == 0)
1820 reloc_entry->address += input_section->output_offset;
1821 return bfd_reloc_ok;
1825 if (symbol->section == &bfd_und_section
1826 && output_bfd == (bfd *) NULL)
1827 ret = bfd_reloc_undefined;
1829 if (bfd_is_com_section (symbol->section))
1832 relocation = symbol->value;
1834 relocation += symbol->section->output_section->vma;
1835 relocation += symbol->section->output_offset;
1836 relocation += reloc_entry->addend;
1838 if (reloc_entry->address > input_section->_cooked_size)
1839 return bfd_reloc_outofrange;
1841 /* Save the information, and let REFLO do the actual relocation. */
1842 ecoff_refhi_addr = (bfd_byte *) data + reloc_entry->address;
1843 ecoff_refhi_addend = relocation;
1845 if (output_bfd != (bfd *) NULL)
1846 reloc_entry->address += input_section->output_offset;
1851 /* Do a REFLO relocation. This is a straightforward 16 bit inplace
1852 relocation; this function exists in order to do the REFHI
1853 relocation described above. */
1855 static bfd_reloc_status_type
1856 ecoff_reflo_reloc (abfd,
1863 arelent *reloc_entry;
1866 asection *input_section;
1869 if (ecoff_refhi_addr != (bfd_byte *) NULL)
1873 unsigned long vallo;
1875 /* Do the REFHI relocation. Note that we actually don't need to
1876 know anything about the REFLO itself, except where to find
1877 the low 16 bits of the addend needed by the REFHI. */
1878 insn = bfd_get_32 (abfd, ecoff_refhi_addr);
1879 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1881 val = ((insn & 0xffff) << 16) + vallo;
1882 val += ecoff_refhi_addend;
1884 /* The low order 16 bits are always treated as a signed value.
1885 Therefore, a negative value in the low order bits requires an
1886 adjustment in the high order bits. We need to make this
1887 adjustment in two ways: once for the bits we took from the
1888 data, and once for the bits we are putting back in to the
1890 if ((vallo & 0x8000) != 0)
1892 if ((val & 0x8000) != 0)
1895 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1896 bfd_put_32 (abfd, insn, ecoff_refhi_addr);
1898 ecoff_refhi_addr = (bfd_byte *) NULL;
1901 /* Now do the REFLO reloc in the usual way. */
1902 return ecoff_generic_reloc (abfd, reloc_entry, symbol, data,
1903 input_section, output_bfd);
1906 /* Do a GPREL relocation. This is a 16 bit value which must become
1907 the offset from the gp register. */
1909 static bfd_reloc_status_type
1910 ecoff_gprel_reloc (abfd,
1917 arelent *reloc_entry;
1920 asection *input_section;
1923 boolean relocateable;
1928 /* If we're relocating, and this is an external symbol with no
1929 addend, we don't want to change anything. We will only have an
1930 addend if this is a newly created reloc, not read from an ECOFF
1932 if (output_bfd != (bfd *) NULL
1933 && (symbol->flags & BSF_SECTION_SYM) == 0
1934 && reloc_entry->addend == 0)
1936 reloc_entry->address += input_section->output_offset;
1937 return bfd_reloc_ok;
1940 if (output_bfd != (bfd *) NULL)
1941 relocateable = true;
1944 relocateable = false;
1945 output_bfd = symbol->section->output_section->owner;
1948 if (symbol->section == &bfd_und_section
1949 && relocateable == false)
1950 return bfd_reloc_undefined;
1952 /* We have to figure out the gp value, so that we can adjust the
1953 symbol value correctly. We look up the symbol _gp in the output
1954 BFD. If we can't find it, we're stuck. We cache it in the ECOFF
1955 target data. We don't need to adjust the symbol value for an
1956 external symbol if we are producing relocateable output. */
1957 if (ecoff_data (output_bfd)->gp == 0
1958 && (relocateable == false
1959 || (symbol->flags & BSF_SECTION_SYM) != 0))
1961 if (relocateable != false)
1963 /* Make up a value. */
1964 ecoff_data (output_bfd)->gp =
1965 symbol->section->output_section->vma + 0x4000;
1973 count = bfd_get_symcount (output_bfd);
1974 sym = bfd_get_outsymbols (output_bfd);
1976 /* We should do something more friendly here, but we don't
1977 have a good reloc status to return. */
1978 if (sym == (asymbol **) NULL)
1981 for (i = 0; i < count; i++, sym++)
1983 register CONST char *name;
1985 name = bfd_asymbol_name (*sym);
1986 if (*name == '_' && strcmp (name, "_gp") == 0)
1988 ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
1993 /* We should do something more friendly here, but we don't have
1994 a good reloc status to return. */
2000 if (bfd_is_com_section (symbol->section))
2003 relocation = symbol->value;
2005 relocation += symbol->section->output_section->vma;
2006 relocation += symbol->section->output_offset;
2008 if (reloc_entry->address > input_section->_cooked_size)
2009 return bfd_reloc_outofrange;
2011 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
2013 /* Set val to the offset into the section or symbol. */
2014 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
2018 /* Adjust val for the final section location and GP value. If we
2019 are producing relocateable output, we don't want to do this for
2020 an external symbol. */
2021 if (relocateable == false
2022 || (symbol->flags & BSF_SECTION_SYM) != 0)
2023 val += relocation - ecoff_data (output_bfd)->gp;
2025 insn = (insn &~ 0xffff) | (val & 0xffff);
2026 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
2028 if (relocateable != false)
2029 reloc_entry->address += input_section->output_offset;
2031 /* Make sure it fit in 16 bits. */
2032 if (val >= 0x8000 && val < 0xffff8000)
2033 return bfd_reloc_outofrange;
2035 return bfd_reloc_ok;
2038 /* Read in the relocs for a section. */
2041 ecoff_slurp_reloc_table (abfd, section, symbols)
2046 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2047 arelent *internal_relocs;
2048 bfd_size_type external_reloc_size;
2049 bfd_size_type external_relocs_size;
2050 char *external_relocs;
2054 if (section->relocation != (arelent *) NULL
2055 || section->reloc_count == 0
2056 || (section->flags & SEC_CONSTRUCTOR) != 0)
2059 if (ecoff_slurp_symbol_table (abfd) == false)
2062 internal_relocs = (arelent *) bfd_alloc (abfd,
2064 * section->reloc_count));
2065 external_reloc_size = backend->external_reloc_size;
2066 external_relocs_size = external_reloc_size * section->reloc_count;
2067 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
2068 if (internal_relocs == (arelent *) NULL
2069 || external_relocs == (char *) NULL)
2071 bfd_error = no_memory;
2074 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
2076 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
2077 != external_relocs_size)
2079 bfd_error = system_call_error;
2083 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
2085 struct internal_reloc intern;
2087 (*backend->swap_reloc_in) (abfd,
2088 external_relocs + i * external_reloc_size,
2091 if (intern.r_type > ECOFF_R_LITERAL)
2094 if (intern.r_extern)
2096 /* r_symndx is an index into the external symbols. */
2097 BFD_ASSERT (intern.r_symndx >= 0
2099 < ecoff_data (abfd)->symbolic_header.iextMax));
2100 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
2105 CONST char *sec_name;
2108 /* r_symndx is a section key. */
2109 switch (intern.r_symndx)
2111 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
2112 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
2113 case RELOC_SECTION_DATA: sec_name = ".data"; break;
2114 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
2115 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
2116 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
2117 case RELOC_SECTION_INIT: sec_name = ".init"; break;
2118 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
2119 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
2123 sec = bfd_get_section_by_name (abfd, sec_name);
2124 if (sec == (asection *) NULL)
2126 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
2128 rptr->addend = - bfd_get_section_vma (abfd, sec);
2129 if (intern.r_type == ECOFF_R_GPREL
2130 || intern.r_type == ECOFF_R_LITERAL)
2131 rptr->addend += ecoff_data (abfd)->gp;
2134 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
2135 rptr->howto = &ecoff_howto_table[intern.r_type];
2137 /* If the type is ECOFF_R_IGNORE, make sure this is a reference
2138 to the absolute section so that the reloc is ignored. */
2139 if (intern.r_type == ECOFF_R_IGNORE)
2140 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2143 bfd_release (abfd, external_relocs);
2145 section->relocation = internal_relocs;
2150 /* Get a canonical list of relocs. */
2153 ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
2161 if (section->flags & SEC_CONSTRUCTOR)
2163 arelent_chain *chain;
2165 /* This section has relocs made up by us, not the file, so take
2166 them out of their chain and place them into the data area
2168 for (count = 0, chain = section->constructor_chain;
2169 count < section->reloc_count;
2170 count++, chain = chain->next)
2171 *relptr++ = &chain->relent;
2177 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
2180 tblptr = section->relocation;
2181 if (tblptr == (arelent *) NULL)
2184 for (count = 0; count < section->reloc_count; count++)
2185 *relptr++ = tblptr++;
2188 *relptr = (arelent *) NULL;
2190 return section->reloc_count;
2193 /* Get the howto structure for a generic reloc type. */
2195 CONST struct reloc_howto_struct *
2196 ecoff_bfd_reloc_type_lookup (abfd, code)
2198 bfd_reloc_code_real_type code;
2205 ecoff_type = ECOFF_R_REFHALF;
2208 ecoff_type = ECOFF_R_REFWORD;
2210 case BFD_RELOC_MIPS_JMP:
2211 ecoff_type = ECOFF_R_JMPADDR;
2213 case BFD_RELOC_HI16_S:
2214 ecoff_type = ECOFF_R_REFHI;
2216 case BFD_RELOC_LO16:
2217 ecoff_type = ECOFF_R_REFLO;
2219 case BFD_RELOC_MIPS_GPREL:
2220 ecoff_type = ECOFF_R_GPREL;
2223 return (CONST struct reloc_howto_struct *) NULL;
2226 return &ecoff_howto_table[ecoff_type];
2229 /* Provided a BFD, a section and an offset into the section, calculate
2230 and return the name of the source file and the line nearest to the
2234 ecoff_find_nearest_line (abfd,
2243 asymbol **ignore_symbols;
2245 CONST char **filename_ptr;
2246 CONST char **functionname_ptr;
2247 unsigned int *retline_ptr;
2249 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2254 bfd_size_type external_pdr_size;
2258 unsigned char *line_ptr;
2259 unsigned char *line_end;
2262 /* If we're not in the .text section, we don't have any line
2264 if (strcmp (section->name, _TEXT) != 0
2265 || offset < ecoff_data (abfd)->text_start
2266 || offset >= ecoff_data (abfd)->text_end)
2269 /* Make sure we have the FDR's. */
2270 if (ecoff_slurp_symbolic_info (abfd) == false
2271 || bfd_get_symcount (abfd) == 0)
2274 /* Each file descriptor (FDR) has a memory address. Here we track
2275 down which FDR we want. The FDR's are stored in increasing
2276 memory order. If speed is ever important, this can become a
2277 binary search. We must ignore FDR's with no PDR entries; they
2278 will have the adr of the FDR before or after them. */
2279 fdr_start = ecoff_data (abfd)->fdr;
2280 fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
2281 fdr_hold = (FDR *) NULL;
2282 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2284 if (fdr_ptr->cpd == 0)
2286 if (offset < fdr_ptr->adr)
2290 if (fdr_hold == (FDR *) NULL)
2294 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2295 have an address, which is relative to the FDR address, and are
2296 also stored in increasing memory order. */
2297 offset -= fdr_ptr->adr;
2298 external_pdr_size = backend->external_pdr_size;
2299 pdr_ptr = ((char *) ecoff_data (abfd)->external_pdr
2300 + fdr_ptr->ipdFirst * external_pdr_size);
2301 pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2302 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2304 /* The address of the first PDR is an offset which applies to the
2305 addresses of all the PDR's. */
2308 for (pdr_ptr += external_pdr_size;
2310 pdr_ptr += external_pdr_size)
2312 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2313 if (offset < pdr.adr)
2317 /* Now we can look for the actual line number. The line numbers are
2318 stored in a very funky format, which I won't try to describe.
2319 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2320 we want; we need this to compute line_end. */
2321 line_end = ecoff_data (abfd)->line;
2322 if (pdr_ptr == pdr_end)
2323 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2325 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2327 /* Now change pdr and pdr_ptr to the one we want. */
2328 pdr_ptr -= external_pdr_size;
2329 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2333 line_ptr = (ecoff_data (abfd)->line
2334 + fdr_ptr->cbLineOffset
2335 + pdr.cbLineOffset);
2336 while (line_ptr < line_end)
2341 delta = *line_ptr >> 4;
2344 count = (*line_ptr & 0xf) + 1;
2348 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2349 if (delta >= 0x8000)
2354 if (offset < count * 4)
2356 offset -= count * 4;
2359 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2360 at least according to gdb/mipsread.c. */
2361 if (fdr_ptr->rss == -1)
2363 *filename_ptr = NULL;
2365 *functionname_ptr = NULL;
2370 (*backend->swap_ext_in) (abfd,
2371 ((char *) ecoff_data (abfd)->external_ext
2372 + pdr.isym * backend->external_ext_size),
2374 *functionname_ptr = ecoff_data (abfd)->ssext + proc_ext.asym.iss;
2381 *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
2382 (*backend->swap_sym_in) (abfd,
2383 ((char *) ecoff_data (abfd)->external_sym
2384 + ((fdr_ptr->isymBase + pdr.isym)
2385 * backend->external_sym_size)),
2387 *functionname_ptr = (ecoff_data (abfd)->ss
2391 if (lineno == ilineNil)
2393 *retline_ptr = lineno;
2397 /* We can't use the generic linking routines for ECOFF, because we
2398 have to handle all the debugging information. The generic link
2399 routine just works out the section contents and attaches a list of
2402 We link by looping over all the seclets. We make two passes. On
2403 the first we set the actual section contents and determine the size
2404 of the debugging information. On the second we accumulate the
2405 debugging information and write it out.
2407 This currently always accumulates the debugging information, which
2408 is incorrect, because it ignores the -s and -S options of the
2409 linker. The linker needs to be modified to give us that
2410 information in a more useful format (currently it just provides a
2411 list of symbols which should appear in the output file). */
2413 /* Clear the output_has_begun flag for all the input BFD's. We use it
2414 to avoid linking in the debugging information for a BFD more than
2418 ecoff_clear_output_flags (abfd)
2421 register asection *o;
2422 register bfd_seclet_type *p;
2424 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2425 for (p = o->seclets_head;
2426 p != (bfd_seclet_type *) NULL;
2428 if (p->type == bfd_indirect_seclet)
2429 p->u.indirect.section->owner->output_has_begun = false;
2432 /* Handle an indirect seclet on the first pass. Set the contents of
2433 the output section, and accumulate the debugging information if
2437 ecoff_rel (output_bfd, seclet, output_section, data, relocateable)
2439 bfd_seclet_type *seclet;
2440 asection *output_section;
2442 boolean relocateable;
2445 HDRR *output_symhdr;
2448 if ((output_section->flags & SEC_HAS_CONTENTS)
2449 && !(output_section->flags & SEC_NEVER_LOAD)
2450 && (output_section->flags & SEC_LOAD)
2453 data = (PTR) bfd_get_relocated_section_contents (output_bfd,
2457 if (bfd_set_section_contents (output_bfd,
2468 input_bfd = seclet->u.indirect.section->owner;
2470 /* We want to figure out how much space will be required to
2471 incorporate all the debugging information from input_bfd. We use
2472 the output_has_begun field to avoid adding it in more than once.
2473 The actual incorporation is done in the second pass, in
2474 ecoff_get_debug. The code has to parallel that code in its
2475 manipulations of output_symhdr. */
2477 if (input_bfd->output_has_begun)
2479 input_bfd->output_has_begun = true;
2481 output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
2483 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2489 /* We just accumulate local symbols from a non-ECOFF BFD. The
2490 external symbols are handled separately. */
2492 symbols = (asymbol **) bfd_alloc (output_bfd,
2493 get_symtab_upper_bound (input_bfd));
2494 if (symbols == (asymbol **) NULL)
2496 bfd_error = no_memory;
2499 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2501 for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
2505 len = strlen ((*sym_ptr)->name);
2506 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2508 ++output_symhdr->isymMax;
2509 output_symhdr->issMax += len + 1;
2513 bfd_release (output_bfd, (PTR) symbols);
2515 ++output_symhdr->ifdMax;
2520 /* We simply add in the information from another ECOFF BFD. First
2521 we make sure we have the symbolic information. */
2522 if (ecoff_slurp_symbol_table (input_bfd) == false)
2524 if (bfd_get_symcount (input_bfd) == 0)
2527 input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2529 /* Figure out how much information we are going to be putting in.
2530 The external symbols are handled separately. */
2531 output_symhdr->ilineMax += input_symhdr->ilineMax;
2532 output_symhdr->cbLine += input_symhdr->cbLine;
2533 output_symhdr->idnMax += input_symhdr->idnMax;
2534 output_symhdr->ipdMax += input_symhdr->ipdMax;
2535 output_symhdr->isymMax += input_symhdr->isymMax;
2536 output_symhdr->ioptMax += input_symhdr->ioptMax;
2537 output_symhdr->iauxMax += input_symhdr->iauxMax;
2538 output_symhdr->issMax += input_symhdr->issMax;
2539 output_symhdr->ifdMax += input_symhdr->ifdMax;
2541 /* The RFD's are special, since we create them if needed. */
2542 if (input_symhdr->crfd > 0)
2543 output_symhdr->crfd += input_symhdr->crfd;
2545 output_symhdr->crfd += input_symhdr->ifdMax;
2550 /* Handle an arbitrary seclet on the first pass. */
2553 ecoff_dump_seclet (abfd, seclet, section, data, relocateable)
2555 bfd_seclet_type *seclet;
2558 boolean relocateable;
2560 switch (seclet->type)
2562 case bfd_indirect_seclet:
2563 /* The contents of this section come from another one somewhere
2565 return ecoff_rel (abfd, seclet, section, data, relocateable);
2567 case bfd_fill_seclet:
2568 /* Fill in the section with fill.value. This is used to pad out
2569 sections, but we must avoid padding the .bss section. */
2570 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2572 if (seclet->u.fill.value != 0)
2577 char *d = (char *) bfd_alloc (abfd, seclet->size);
2581 for (i = 0; i < seclet->size; i+=2)
2582 d[i] = seclet->u.fill.value >> 8;
2583 for (i = 1; i < seclet->size; i+=2)
2584 d[i] = seclet->u.fill.value;
2585 ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2587 bfd_release (abfd, (PTR) d);
2599 /* Add a string to the debugging information we are accumulating for a
2600 file. Return the offset from the fdr string base or from the
2601 external string base. */
2604 ecoff_add_string (output_bfd, fdr, string, external)
2614 symhdr = &ecoff_data (output_bfd)->symbolic_header;
2615 len = strlen (string);
2618 strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2619 ret = symhdr->issExtMax;
2620 symhdr->issExtMax += len + 1;
2624 strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2626 symhdr->issMax += len + 1;
2627 fdr->cbSs += len + 1;
2632 /* Accumulate the debugging information from an input section. */
2635 ecoff_get_debug (output_bfd, seclet, section, relocateable)
2637 bfd_seclet_type *seclet;
2639 boolean relocateable;
2641 const struct ecoff_backend_data * const backend = ecoff_backend (output_bfd);
2642 const bfd_size_type external_sym_size = backend->external_sym_size;
2643 const bfd_size_type external_pdr_size = backend->external_pdr_size;
2644 const bfd_size_type external_fdr_size = backend->external_fdr_size;
2645 const bfd_size_type external_rfd_size = backend->external_rfd_size;
2646 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
2647 = backend->swap_sym_in;
2648 void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
2649 = backend->swap_sym_out;
2650 void (* const swap_pdr_in) PARAMS ((bfd *, PTR, PDR *))
2651 = backend->swap_pdr_in;
2652 void (* const swap_fdr_out) PARAMS ((bfd *, const FDR *, PTR))
2653 = backend->swap_fdr_out;
2654 void (* const swap_rfd_out) PARAMS ((bfd *, const RFDT *, PTR))
2655 = backend->swap_rfd_out;
2657 HDRR *output_symhdr;
2659 ecoff_data_type *output_ecoff;
2660 ecoff_data_type *input_ecoff;
2663 ecoff_symbol_type *esym_ptr;
2664 ecoff_symbol_type *esym_end;
2669 input_bfd = seclet->u.indirect.section->owner;
2671 /* Don't get the information more than once. */
2672 if (input_bfd->output_has_begun)
2674 input_bfd->output_has_begun = true;
2676 output_ecoff = ecoff_data (output_bfd);
2677 output_symhdr = &output_ecoff->symbolic_header;
2679 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2686 /* This is not an ECOFF BFD. Just gather the symbols. */
2688 memset (&fdr, 0, sizeof fdr);
2690 fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2691 fdr.issBase = output_symhdr->issMax;
2693 fdr.rss = ecoff_add_string (output_bfd,
2695 bfd_get_filename (input_bfd),
2697 fdr.isymBase = output_symhdr->isymMax;
2699 /* Get the local symbols from the input BFD. */
2700 symbols = (asymbol **) bfd_alloc (output_bfd,
2701 get_symtab_upper_bound (input_bfd));
2702 if (symbols == (asymbol **) NULL)
2704 bfd_error = no_memory;
2707 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2709 /* Handle the local symbols. Any external symbols are handled
2712 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2716 if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2718 memset (&internal_sym, 0, sizeof internal_sym);
2719 internal_sym.iss = ecoff_add_string (output_bfd,
2724 if (bfd_is_com_section ((*sym_ptr)->section)
2725 || (*sym_ptr)->section == &bfd_und_section)
2726 internal_sym.value = (*sym_ptr)->value;
2728 internal_sym.value = ((*sym_ptr)->value
2729 + (*sym_ptr)->section->output_offset
2730 + (*sym_ptr)->section->output_section->vma);
2731 internal_sym.st = stNil;
2732 internal_sym.sc = scUndefined;
2733 internal_sym.index = indexNil;
2734 (*swap_sym_out) (output_bfd, &internal_sym,
2735 ((char *) output_ecoff->external_sym
2736 + output_symhdr->isymMax * external_sym_size));
2738 ++output_symhdr->isymMax;
2741 bfd_release (output_bfd, (PTR) symbols);
2743 /* Leave everything else in the FDR zeroed out. This will cause
2744 the lang field to be langC. The fBigendian field will
2745 indicate little endian format, but it doesn't matter because
2746 it only applies to aux fields and there are none. */
2748 (*swap_fdr_out) (output_bfd, &fdr,
2749 ((char *) output_ecoff->external_fdr
2750 + output_symhdr->ifdMax * external_fdr_size));
2751 ++output_symhdr->ifdMax;
2755 /* This is an ECOFF BFD. We want to grab the information from
2756 input_bfd and attach it to output_bfd. */
2757 count = bfd_get_symcount (input_bfd);
2760 input_ecoff = ecoff_data (input_bfd);
2761 input_symhdr = &input_ecoff->symbolic_header;
2763 /* I think that it is more efficient to simply copy the debugging
2764 information from the input BFD to the output BFD. Because ECOFF
2765 uses relative pointers for most of the debugging information,
2766 only a little of it has to be changed at all. */
2768 /* Swap in the local symbols, adjust their values, and swap them out
2769 again. The external symbols are handled separately. */
2770 sym_out = ((char *) output_ecoff->external_sym
2771 + output_symhdr->isymMax * external_sym_size);
2773 esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
2774 esym_end = esym_ptr + count;
2775 for (; esym_ptr < esym_end; esym_ptr++)
2777 if (esym_ptr->local)
2781 (*swap_sym_in) (input_bfd, esym_ptr->native, &sym);
2783 /* If we're producing an executable, move common symbols
2785 if (relocateable == false)
2787 if (sym.sc == scCommon)
2789 else if (sym.sc == scSCommon)
2793 if (! bfd_is_com_section (esym_ptr->symbol.section)
2794 && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
2795 && esym_ptr->symbol.section != &bfd_und_section)
2796 sym.value = (esym_ptr->symbol.value
2797 + esym_ptr->symbol.section->output_offset
2798 + esym_ptr->symbol.section->output_section->vma);
2799 (*swap_sym_out) (output_bfd, &sym, sym_out);
2800 sym_out += external_sym_size;
2804 /* That should have accounted for all the local symbols in
2807 /* Copy the information that does not need swapping. */
2808 memcpy (output_ecoff->line + output_symhdr->cbLine,
2810 input_symhdr->cbLine * sizeof (unsigned char));
2811 memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2812 input_ecoff->external_aux,
2813 input_symhdr->iauxMax * sizeof (union aux_ext));
2814 memcpy (output_ecoff->ss + output_symhdr->issMax,
2816 input_symhdr->issMax * sizeof (char));
2818 /* Some of the information may need to be swapped. */
2819 if (output_bfd->xvec->header_byteorder_big_p
2820 == input_bfd->xvec->header_byteorder_big_p)
2822 /* The two BFD's have the same endianness, so memcpy will
2824 if (input_symhdr->idnMax > 0)
2825 memcpy (((char *) output_ecoff->external_dnr
2826 + output_symhdr->idnMax * backend->external_dnr_size),
2827 input_ecoff->external_dnr,
2828 input_symhdr->idnMax * backend->external_dnr_size);
2829 if (input_symhdr->ipdMax > 0)
2830 memcpy (((char *) output_ecoff->external_pdr
2831 + output_symhdr->ipdMax * external_pdr_size),
2832 input_ecoff->external_pdr,
2833 input_symhdr->ipdMax * external_pdr_size);
2834 if (input_symhdr->ioptMax > 0)
2835 memcpy (((char *) output_ecoff->external_opt
2836 + output_symhdr->ioptMax * backend->external_opt_size),
2837 input_ecoff->external_opt,
2838 input_symhdr->ioptMax * backend->external_opt_size);
2847 /* The two BFD's have different endianness, so we must swap
2848 everything in and out. This code would always work, but it
2849 would be slow in the normal case. */
2850 sz = backend->external_dnr_size;
2851 in = (char *) input_ecoff->external_dnr;
2852 end = in + input_symhdr->idnMax * sz;
2853 out = (char *) output_ecoff->external_dnr + output_symhdr->idnMax * sz;
2854 for (; in < end; in += sz, out += sz)
2858 (*backend->swap_dnr_in) (input_bfd, in, &dnr);
2859 (*backend->swap_dnr_out) (output_bfd, &dnr, out);
2862 sz = external_pdr_size;
2863 in = (char *) input_ecoff->external_pdr;
2864 end = in + input_symhdr->ipdMax * sz;
2865 out = (char *) output_ecoff->external_pdr + output_symhdr->ipdMax * sz;
2866 for (; in < end; in += sz, out += sz)
2870 (*swap_pdr_in) (input_bfd, in, &pdr);
2871 (*backend->swap_pdr_out) (output_bfd, &pdr, out);
2874 sz = backend->external_opt_size;
2875 in = (char *) input_ecoff->external_opt;
2876 end = in + input_symhdr->ioptMax * sz;
2877 out = (char *) output_ecoff->external_opt + output_symhdr->ioptMax * sz;
2878 for (; in < end; in += sz, out += sz)
2882 (*backend->swap_opt_in) (input_bfd, in, &opt);
2883 (*backend->swap_opt_out) (output_bfd, &opt, out);
2887 /* Set ifdbase so that the external symbols know how to adjust their
2889 input_ecoff->ifdbase = output_symhdr->ifdMax;
2891 fdr_ptr = input_ecoff->fdr;
2892 fdr_end = fdr_ptr + input_symhdr->ifdMax;
2893 fdr_out = ((char *) output_ecoff->external_fdr
2894 + output_symhdr->ifdMax * external_fdr_size);
2895 for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out += external_fdr_size)
2898 unsigned long pdr_off;
2902 /* The memory address for this fdr is the address for the seclet
2903 plus the offset to this fdr within input_bfd. For some
2904 reason the offset of the first procedure pointer is also
2912 (*swap_pdr_in) (input_bfd,
2913 ((char *) input_ecoff->external_pdr
2914 + fdr.ipdFirst * external_pdr_size),
2918 fdr.adr = (bfd_get_section_vma (output_bfd, section)
2920 + (fdr_ptr->adr - input_ecoff->fdr->adr)
2923 fdr.issBase += output_symhdr->issMax;
2924 fdr.isymBase += output_symhdr->isymMax;
2925 fdr.ilineBase += output_symhdr->ilineMax;
2926 fdr.ioptBase += output_symhdr->ioptMax;
2927 fdr.ipdFirst += output_symhdr->ipdMax;
2928 fdr.iauxBase += output_symhdr->iauxMax;
2929 fdr.rfdBase += output_symhdr->crfd;
2931 /* If there are no RFD's, we are going to add some. We don't
2932 want to adjust irfd for this, so that all the FDR's can share
2934 if (input_symhdr->crfd == 0)
2935 fdr.crfd = input_symhdr->ifdMax;
2937 if (fdr.cbLine != 0)
2938 fdr.cbLineOffset += output_symhdr->cbLine;
2940 (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
2943 if (input_symhdr->crfd > 0)
2945 void (* const swap_rfd_in) PARAMS ((bfd *, PTR, RFDT *))
2946 = backend->swap_rfd_in;
2951 /* Swap and adjust the RFD's. RFD's are only created by the
2952 linker, so this will only be necessary if one of the input
2953 files is the result of a partial link. Presumably all
2954 necessary RFD's are present. */
2955 rfd_in = (char *) input_ecoff->external_rfd;
2956 rfd_end = rfd_in + input_symhdr->crfd * external_rfd_size;
2957 rfd_out = ((char *) output_ecoff->external_rfd
2958 + output_symhdr->crfd * external_rfd_size);
2961 rfd_in += external_rfd_size, rfd_out += external_rfd_size)
2965 (*swap_rfd_in) (input_bfd, rfd_in, &rfd);
2966 rfd += output_symhdr->ifdMax;
2967 (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
2969 output_symhdr->crfd += input_symhdr->crfd;
2977 /* Create RFD's. Some of the debugging information includes
2978 relative file indices. These indices are taken as indices to
2979 the RFD table if there is one, or to the global table if
2980 there is not. If we did not create RFD's, we would have to
2981 parse and adjust all the debugging information which contains
2983 rfd = output_symhdr->ifdMax;
2984 rfd_out = ((char *) output_ecoff->external_rfd
2985 + output_symhdr->crfd * external_rfd_size);
2986 rfd_end = rfd_out + input_symhdr->ifdMax * external_rfd_size;
2987 for (; rfd_out < rfd_end; rfd_out += external_rfd_size, rfd++)
2988 (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
2989 output_symhdr->crfd += input_symhdr->ifdMax;
2992 /* Combine the register masks. Not all of these are used on all
2993 targets, but that's OK because only the relevant ones will be
2994 swapped in and out. */
2998 output_ecoff->gprmask |= input_ecoff->gprmask;
2999 output_ecoff->fprmask |= input_ecoff->fprmask;
3000 for (i = 0; i < 4; i++)
3001 output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
3004 /* Update the counts. */
3005 output_symhdr->ilineMax += input_symhdr->ilineMax;
3006 output_symhdr->cbLine += input_symhdr->cbLine;
3007 output_symhdr->idnMax += input_symhdr->idnMax;
3008 output_symhdr->ipdMax += input_symhdr->ipdMax;
3009 output_symhdr->isymMax += input_symhdr->isymMax;
3010 output_symhdr->ioptMax += input_symhdr->ioptMax;
3011 output_symhdr->iauxMax += input_symhdr->iauxMax;
3012 output_symhdr->issMax += input_symhdr->issMax;
3013 output_symhdr->ifdMax += input_symhdr->ifdMax;
3018 /* This is the actual link routine. It makes two passes over all the
3022 ecoff_bfd_seclet_link (abfd, data, relocateable)
3025 boolean relocateable;
3027 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3030 register asection *o;
3031 register bfd_seclet_type *p;
3032 asymbol **sym_ptr_ptr;
3033 bfd_size_type debug_align;
3037 /* We accumulate the debugging information counts in the symbolic
3039 symhdr = &ecoff_data (abfd)->symbolic_header;
3040 symhdr->magic = backend->sym_magic;
3041 /* FIXME: What should the version stamp be? */
3043 symhdr->ilineMax = 0;
3047 symhdr->isymMax = 0;
3048 symhdr->ioptMax = 0;
3049 symhdr->iauxMax = 0;
3051 symhdr->issExtMax = 0;
3054 symhdr->iextMax = 0;
3056 /* We need to copy over the debugging symbols from each input BFD.
3057 When we do this copying, we have to adjust the text address in
3058 the FDR structures, so we have to know the text address used for
3059 the input BFD. Since we only want to copy the symbols once per
3060 input BFD, but we are going to look at each input BFD multiple
3061 times (once for each section it provides), we arrange to always
3062 look at the text section first. That means that when we copy the
3063 debugging information, we always know the text address. So we
3064 actually do each pass in two sub passes; first the text sections,
3065 then the non-text sections. We use the output_has_begun flag to
3066 determine whether we have copied over the debugging information
3069 /* Do the first pass: set the output section contents and count the
3070 debugging information. */
3071 ecoff_clear_output_flags (abfd);
3072 for (ipass = 0; ipass < 2; ipass++)
3074 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3076 /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
3077 so they are done on pass 0. For other sections the
3078 expression is true, so they are done on pass 1. */
3079 if (((o->flags & SEC_CODE) == 0) != ipass)
3082 for (p = o->seclets_head;
3083 p != (bfd_seclet_type *) NULL;
3086 if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
3093 /* We handle the external symbols differently. We use the ones
3094 attached to the output_bfd. The linker will have already
3095 determined which symbols are to be attached. Here we just
3096 determine how much space we will need for them. */
3097 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3098 if (sym_ptr_ptr != NULL)
3102 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
3103 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
3105 if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
3106 && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
3109 symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
3114 /* Adjust the counts so that structures are longword aligned. */
3115 debug_align = backend->debug_align;
3117 symhdr->cbLine = (symhdr->cbLine + debug_align) &~ debug_align;
3118 symhdr->issMax = (symhdr->issMax + debug_align) &~ debug_align;
3119 symhdr->issExtMax = (symhdr->issExtMax + debug_align) &~ debug_align;
3121 /* Now the counts in symhdr are the correct size for the debugging
3122 information. We allocate the right amount of space, and reset
3123 the counts so that the second pass can use them as indices. It
3124 would be possible to output the debugging information directly to
3125 the file in pass 2, rather than to build it in memory and then
3126 write it out. Outputting to the file would require a lot of
3127 seeks and small writes, though, and I think this approach is
3129 size = (symhdr->cbLine * sizeof (unsigned char)
3130 + symhdr->idnMax * backend->external_dnr_size
3131 + symhdr->ipdMax * backend->external_pdr_size
3132 + symhdr->isymMax * backend->external_sym_size
3133 + symhdr->ioptMax * backend->external_opt_size
3134 + symhdr->iauxMax * sizeof (union aux_ext)
3135 + symhdr->issMax * sizeof (char)
3136 + symhdr->issExtMax * sizeof (char)
3137 + symhdr->ifdMax * backend->external_fdr_size
3138 + symhdr->crfd * backend->external_rfd_size
3139 + symhdr->iextMax * backend->external_ext_size);
3140 raw = (char *) bfd_alloc (abfd, size);
3141 if (raw == (char *) NULL)
3143 bfd_error = no_memory;
3146 ecoff_data (abfd)->raw_size = size;
3147 ecoff_data (abfd)->raw_syments = (PTR) raw;
3149 /* Initialize the raw pointers. */
3150 #define SET(field, count, type, size) \
3151 ecoff_data (abfd)->field = (type) raw; \
3152 raw += symhdr->count * size
3154 SET (line, cbLine, unsigned char *, sizeof (unsigned char));
3155 SET (external_dnr, idnMax, PTR, backend->external_dnr_size);
3156 SET (external_pdr, ipdMax, PTR, backend->external_pdr_size);
3157 SET (external_sym, isymMax, PTR, backend->external_sym_size);
3158 SET (external_opt, ioptMax, PTR, backend->external_opt_size);
3159 SET (external_aux, iauxMax, union aux_ext *, sizeof (union aux_ext));
3160 SET (ss, issMax, char *, sizeof (char));
3161 SET (ssext, issExtMax, char *, sizeof (char));
3162 SET (external_fdr, ifdMax, PTR, backend->external_fdr_size);
3163 SET (external_rfd, crfd, PTR, backend->external_rfd_size);
3164 SET (external_ext, iextMax, PTR, backend->external_ext_size);
3167 /* Reset the counts so the second pass can use them to know how far
3169 symhdr->ilineMax = 0;
3173 symhdr->isymMax = 0;
3174 symhdr->ioptMax = 0;
3175 symhdr->iauxMax = 0;
3177 symhdr->issExtMax = 0;
3180 symhdr->iextMax = 0;
3182 /* Do the second pass: accumulate the debugging information. */
3183 ecoff_clear_output_flags (abfd);
3184 for (ipass = 0; ipass < 2; ipass++)
3186 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3188 if (((o->flags & SEC_CODE) == 0) != ipass)
3190 for (p = o->seclets_head;
3191 p != (bfd_seclet_type *) NULL;
3194 if (p->type == bfd_indirect_seclet)
3196 if (ecoff_get_debug (abfd, p, o, relocateable) == false)
3203 /* Put in the external symbols. */
3204 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3205 if (sym_ptr_ptr != NULL)
3207 const bfd_size_type external_ext_size = backend->external_ext_size;
3208 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3209 = backend->swap_ext_in;
3210 void (* const swap_ext_out) PARAMS ((bfd *, const EXTR *, PTR))
3211 = backend->swap_ext_out;
3215 ssext = ecoff_data (abfd)->ssext;
3216 external_ext = (char *) ecoff_data (abfd)->external_ext;
3217 for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
3222 sym_ptr = *sym_ptr_ptr;
3224 if ((sym_ptr->flags & BSF_DEBUGGING) != 0
3225 || (sym_ptr->flags & BSF_LOCAL) != 0)
3228 /* The native pointer can be NULL for a symbol created by
3229 the linker via ecoff_make_empty_symbol. */
3230 if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
3231 || ecoffsymbol (sym_ptr)->native == NULL)
3234 esym.cobol_main = 0;
3238 /* FIXME: we can do better than this for st and sc. */
3239 esym.asym.st = stGlobal;
3240 esym.asym.sc = scAbs;
3241 esym.asym.reserved = 0;
3242 esym.asym.index = indexNil;
3246 ecoff_symbol_type *ecoff_sym_ptr;
3248 ecoff_sym_ptr = ecoffsymbol (sym_ptr);
3249 if (ecoff_sym_ptr->local)
3251 (*swap_ext_in) (abfd, ecoff_sym_ptr->native, &esym);
3253 /* If we're producing an executable, move common symbols
3255 if (relocateable == false)
3257 if (esym.asym.sc == scCommon)
3258 esym.asym.sc = scBss;
3259 else if (esym.asym.sc == scSCommon)
3260 esym.asym.sc = scSBss;
3263 /* Adjust the FDR index for the symbol by that used for
3265 esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
3268 esym.asym.iss = symhdr->issExtMax;
3270 if (bfd_is_com_section (sym_ptr->section)
3271 || sym_ptr->section == &bfd_und_section)
3272 esym.asym.value = sym_ptr->value;
3274 esym.asym.value = (sym_ptr->value
3275 + sym_ptr->section->output_offset
3276 + sym_ptr->section->output_section->vma);
3278 (*swap_ext_out) (abfd, &esym, external_ext);
3280 ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
3282 external_ext += external_ext_size;
3285 strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
3286 symhdr->issExtMax += strlen (sym_ptr->name) + 1;
3290 /* Adjust the counts so that structures are longword aligned. */
3291 symhdr->cbLine = (symhdr->cbLine + debug_align) &~ debug_align;
3292 symhdr->issMax = (symhdr->issMax + debug_align) &~ debug_align;
3293 symhdr->issExtMax = (symhdr->issExtMax + debug_align) &~ debug_align;
3298 /* Set the architecture. The supported architecture is stored in the
3299 backend pointer. We always set the architecture anyhow, since many
3300 callers ignore the return value. */
3303 ecoff_set_arch_mach (abfd, arch, machine)
3305 enum bfd_architecture arch;
3306 unsigned long machine;
3308 bfd_default_set_arch_mach (abfd, arch, machine);
3309 return arch == ecoff_backend (abfd)->arch;
3312 /* Get the size of the section headers. We do not output the .scommon
3313 section which we created in ecoff_mkobject. */
3316 ecoff_sizeof_headers (abfd, reloc)
3320 return (bfd_coff_filhsz (abfd)
3321 + bfd_coff_aoutsz (abfd)
3322 + (abfd->section_count - 1) * bfd_coff_scnhsz (abfd));
3325 /* Calculate the file position for each section, and set
3329 ecoff_compute_section_file_positions (abfd)
3337 if (bfd_get_start_address (abfd))
3338 abfd->flags |= EXEC_P;
3340 sofar = ecoff_sizeof_headers (abfd, false);
3343 for (current = abfd->sections;
3344 current != (asection *) NULL;
3345 current = current->next)
3347 /* Only deal with sections which have contents */
3348 if (! (current->flags & SEC_HAS_CONTENTS)
3349 || strcmp (current->name, SCOMMON) == 0)
3352 /* On Ultrix, the data sections in an executable file must be
3353 aligned to a page boundary within the file. This does not
3354 affect the section size, though. FIXME: Does this work for
3356 if ((abfd->flags & EXEC_P) != 0
3357 && (abfd->flags & D_PAGED) != 0
3358 && first_data != false
3359 && (current->flags & SEC_CODE) == 0)
3361 const bfd_vma round = ecoff_backend (abfd)->round;
3363 sofar = (sofar + round - 1) &~ (round - 1);
3367 /* Align the sections in the file to the same boundary on
3368 which they are aligned in virtual memory. */
3370 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3372 current->filepos = sofar;
3374 sofar += current->_raw_size;
3376 /* make sure that this section is of the right size too */
3378 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3379 current->_raw_size += sofar - old_sofar;
3382 ecoff_data (abfd)->reloc_filepos = sofar;
3385 /* Set the contents of a section. */
3388 ecoff_set_section_contents (abfd, section, location, offset, count)
3393 bfd_size_type count;
3395 if (abfd->output_has_begun == false)
3396 ecoff_compute_section_file_positions (abfd);
3398 bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
3401 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
3406 /* Write out an ECOFF file. */
3409 ecoff_write_object_contents (abfd)
3412 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3413 const bfd_vma round = backend->round;
3414 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
3415 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
3416 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
3417 const bfd_size_type external_hdr_size = backend->external_hdr_size;
3418 const bfd_size_type external_reloc_size = backend->external_reloc_size;
3419 void (* const swap_reloc_out) PARAMS ((bfd *,
3420 const struct internal_reloc *,
3422 = backend->swap_reloc_out;
3426 file_ptr reloc_base;
3428 unsigned long reloc_size;
3429 unsigned long text_size;
3430 unsigned long text_start;
3431 unsigned long data_size;
3432 unsigned long data_start;
3433 unsigned long bss_size;
3435 struct internal_filehdr internal_f;
3436 struct internal_aouthdr internal_a;
3439 bfd_error = system_call_error;
3441 if(abfd->output_has_begun == false)
3442 ecoff_compute_section_file_positions(abfd);
3444 if (abfd->sections != (asection *) NULL)
3445 scn_base = abfd->sections->filepos;
3448 reloc_base = ecoff_data (abfd)->reloc_filepos;
3452 for (current = abfd->sections;
3453 current != (asection *)NULL;
3454 current = current->next)
3456 if (strcmp (current->name, SCOMMON) == 0)
3458 current->target_index = count;
3460 if (current->reloc_count != 0)
3462 bfd_size_type relsize;
3464 current->rel_filepos = reloc_base;
3465 relsize = current->reloc_count * external_reloc_size;
3466 reloc_size += relsize;
3467 reloc_base += relsize;
3470 current->rel_filepos = 0;
3473 sym_base = reloc_base + reloc_size;
3475 /* At least on Ultrix, the symbol table of an executable file must
3476 be aligned to a page boundary. FIXME: Is this true on other
3478 if ((abfd->flags & EXEC_P) != 0
3479 && (abfd->flags & D_PAGED) != 0)
3480 sym_base = (sym_base + round - 1) &~ (round - 1);
3482 ecoff_data (abfd)->sym_filepos = sym_base;
3484 if ((abfd->flags & D_PAGED) != 0)
3485 text_size = ecoff_sizeof_headers (abfd, false);
3493 /* Write section headers to the file. */
3495 buff = (PTR) alloca (scnhsz);
3496 internal_f.f_nscns = 0;
3497 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
3499 for (current = abfd->sections;
3500 current != (asection *) NULL;
3501 current = current->next)
3503 struct internal_scnhdr section;
3506 if (strcmp (current->name, SCOMMON) == 0)
3508 BFD_ASSERT (bfd_get_section_size_before_reloc (current) == 0
3509 && current->reloc_count == 0);
3513 ++internal_f.f_nscns;
3515 strncpy (section.s_name, current->name, sizeof section.s_name);
3517 /* FIXME: is this correct for shared libraries? I think it is
3518 but I have no platform to check. Ian Lance Taylor. */
3519 vma = bfd_get_section_vma (abfd, current);
3520 if (strcmp (current->name, _LIB) == 0)
3521 section.s_vaddr = 0;
3523 section.s_vaddr = vma;
3525 section.s_paddr = vma;
3526 section.s_size = bfd_get_section_size_before_reloc (current);
3528 /* If this section has no size or is unloadable then the scnptr
3530 if (current->_raw_size == 0
3531 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3532 section.s_scnptr = 0;
3534 section.s_scnptr = current->filepos;
3535 section.s_relptr = current->rel_filepos;
3537 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
3538 object file produced by the assembler is supposed to point to
3539 information about how much room is required by objects of
3540 various different sizes. I think this only matters if we
3541 want the linker to compute the best size to use, or
3542 something. I don't know what happens if the information is
3544 section.s_lnnoptr = 0;
3546 section.s_nreloc = current->reloc_count;
3547 section.s_nlnno = 0;
3548 section.s_flags = ecoff_sec_to_styp_flags (current->name,
3551 bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff);
3552 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
3555 if ((section.s_flags & STYP_TEXT) != 0)
3557 text_size += bfd_get_section_size_before_reloc (current);
3558 if (text_start == 0 || text_start > vma)
3561 else if ((section.s_flags & STYP_RDATA) != 0
3562 || (section.s_flags & STYP_DATA) != 0
3563 || (section.s_flags & STYP_LIT8) != 0
3564 || (section.s_flags & STYP_LIT4) != 0
3565 || (section.s_flags & STYP_SDATA) != 0)
3567 data_size += bfd_get_section_size_before_reloc (current);
3568 if (data_start == 0 || data_start > vma)
3571 else if ((section.s_flags & STYP_BSS) != 0
3572 || (section.s_flags & STYP_SBSS) != 0)
3573 bss_size += bfd_get_section_size_before_reloc (current);
3576 /* Set up the file header. */
3578 if (abfd->xvec->header_byteorder_big_p != false)
3579 internal_f.f_magic = backend->big_magic;
3581 internal_f.f_magic = backend->little_magic;
3583 /* We will NOT put a fucking timestamp in the header here. Every
3584 time you put it back, I will come in and take it out again. I'm
3585 sorry. This field does not belong here. We fill it with a 0 so
3586 it compares the same but is not a reasonable time. --
3588 internal_f.f_timdat = 0;
3590 if (bfd_get_symcount (abfd) != 0)
3592 /* The ECOFF f_nsyms field is not actually the number of
3593 symbols, it's the size of symbolic information header. */
3594 internal_f.f_nsyms = external_hdr_size;
3595 internal_f.f_symptr = sym_base;
3599 internal_f.f_nsyms = 0;
3600 internal_f.f_symptr = 0;
3603 internal_f.f_opthdr = aoutsz;
3605 internal_f.f_flags = F_LNNO;
3606 if (reloc_size == 0)
3607 internal_f.f_flags |= F_RELFLG;
3608 if (bfd_get_symcount (abfd) == 0)
3609 internal_f.f_flags |= F_LSYMS;
3610 if (abfd->flags & EXEC_P)
3611 internal_f.f_flags |= F_EXEC;
3613 if (! abfd->xvec->byteorder_big_p)
3614 internal_f.f_flags |= F_AR32WR;
3616 internal_f.f_flags |= F_AR32W;
3618 /* Set up the ``optional'' header. */
3619 if ((abfd->flags & D_PAGED) != 0)
3620 internal_a.magic = ECOFF_AOUT_ZMAGIC;
3622 internal_a.magic = ECOFF_AOUT_OMAGIC;
3624 /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
3625 linker happy. But, is it right? */
3626 internal_a.vstamp = 0x20a;
3628 /* At least on Ultrix, these have to be rounded to page boundaries.
3629 FIXME: Is this true on other platforms? */
3630 if ((abfd->flags & D_PAGED) != 0)
3632 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
3633 internal_a.text_start = text_start &~ (round - 1);
3634 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
3635 internal_a.data_start = data_start &~ (round - 1);
3639 internal_a.tsize = text_size;
3640 internal_a.text_start = text_start;
3641 internal_a.dsize = data_size;
3642 internal_a.data_start = data_start;
3645 /* On Ultrix, the initial portions of the .sbss and .bss segments
3646 are at the end of the data section. The bsize field in the
3647 optional header records how many bss bytes are required beyond
3648 those in the data section. The value is not rounded to a page
3650 if (bss_size < internal_a.dsize - data_size)
3653 bss_size -= internal_a.dsize - data_size;
3654 internal_a.bsize = bss_size;
3655 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
3657 internal_a.entry = bfd_get_start_address (abfd);
3659 internal_a.gp_value = ecoff_data (abfd)->gp;
3661 internal_a.gprmask = ecoff_data (abfd)->gprmask;
3662 internal_a.fprmask = ecoff_data (abfd)->fprmask;
3663 for (i = 0; i < 4; i++)
3664 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
3666 /* Write out the file header and the optional header. */
3668 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3671 buff = (PTR) alloca (filhsz);
3672 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
3673 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
3676 buff = (PTR) alloca (aoutsz);
3677 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
3678 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
3681 /* Write out the relocs. */
3682 for (current = abfd->sections;
3683 current != (asection *) NULL;
3684 current = current->next)
3686 arelent **reloc_ptr_ptr;
3687 arelent **reloc_end;
3690 if (current->reloc_count == 0)
3693 buff = bfd_alloc (abfd, current->reloc_count * external_reloc_size);
3696 bfd_error = no_memory;
3700 reloc_ptr_ptr = current->orelocation;
3701 reloc_end = reloc_ptr_ptr + current->reloc_count;
3702 out_ptr = (char *) buff;
3704 reloc_ptr_ptr < reloc_end;
3705 reloc_ptr_ptr++, out_ptr += external_reloc_size)
3709 struct internal_reloc in;
3711 memset (&in, 0, sizeof in);
3713 reloc = *reloc_ptr_ptr;
3714 sym = *reloc->sym_ptr_ptr;
3716 /* This must be an ECOFF reloc. */
3717 BFD_ASSERT (reloc->howto != (reloc_howto_type *) NULL
3718 && reloc->howto >= ecoff_howto_table
3720 < (ecoff_howto_table + ECOFF_HOWTO_COUNT)));
3722 in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
3723 in.r_type = reloc->howto->type;
3725 /* If this is a REFHI reloc, the next one must be a REFLO
3726 reloc for the same symbol. */
3727 BFD_ASSERT (in.r_type != ECOFF_R_REFHI
3728 || (reloc_ptr_ptr < reloc_end
3729 && (reloc_ptr_ptr[1]->howto
3730 != (reloc_howto_type *) NULL)
3731 && (reloc_ptr_ptr[1]->howto->type
3733 && (sym == *reloc_ptr_ptr[1]->sym_ptr_ptr)));
3735 if ((sym->flags & BSF_SECTION_SYM) == 0)
3737 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
3744 name = bfd_get_section_name (abfd, bfd_get_section (sym));
3745 if (strcmp (name, ".text") == 0)
3746 in.r_symndx = RELOC_SECTION_TEXT;
3747 else if (strcmp (name, ".rdata") == 0)
3748 in.r_symndx = RELOC_SECTION_RDATA;
3749 else if (strcmp (name, ".data") == 0)
3750 in.r_symndx = RELOC_SECTION_DATA;
3751 else if (strcmp (name, ".sdata") == 0)
3752 in.r_symndx = RELOC_SECTION_SDATA;
3753 else if (strcmp (name, ".sbss") == 0)
3754 in.r_symndx = RELOC_SECTION_SBSS;
3755 else if (strcmp (name, ".bss") == 0)
3756 in.r_symndx = RELOC_SECTION_BSS;
3757 else if (strcmp (name, ".init") == 0)
3758 in.r_symndx = RELOC_SECTION_INIT;
3759 else if (strcmp (name, ".lit8") == 0)
3760 in.r_symndx = RELOC_SECTION_LIT8;
3761 else if (strcmp (name, ".lit4") == 0)
3762 in.r_symndx = RELOC_SECTION_LIT4;
3768 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
3771 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
3773 if (bfd_write (buff, external_reloc_size, current->reloc_count, abfd)
3774 != external_reloc_size * current->reloc_count)
3776 bfd_release (abfd, buff);
3779 /* Write out the symbolic debugging information. */
3780 if (bfd_get_symcount (abfd) > 0)
3783 unsigned long sym_offset;
3785 /* Set up the offsets in the symbolic header. */
3786 symhdr = &ecoff_data (abfd)->symbolic_header;
3787 sym_offset = ecoff_data (abfd)->sym_filepos + external_hdr_size;
3789 #define SET(offset, size, ptr) \
3790 if (symhdr->size == 0) \
3791 symhdr->offset = 0; \
3793 symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
3794 - (char *) ecoff_data (abfd)->raw_syments) \
3797 SET (cbLineOffset, cbLine, line);
3798 SET (cbDnOffset, idnMax, external_dnr);
3799 SET (cbPdOffset, ipdMax, external_pdr);
3800 SET (cbSymOffset, isymMax, external_sym);
3801 SET (cbOptOffset, ioptMax, external_opt);
3802 SET (cbAuxOffset, iauxMax, external_aux);
3803 SET (cbSsOffset, issMax, ss);
3804 SET (cbSsExtOffset, issExtMax, ssext);
3805 SET (cbFdOffset, ifdMax, external_fdr);
3806 SET (cbRfdOffset, crfd, external_rfd);
3807 SET (cbExtOffset, iextMax, external_ext);
3810 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
3813 buff = (PTR) alloca (external_hdr_size);
3814 (*backend->swap_hdr_out) (abfd, &ecoff_data (abfd)->symbolic_header,
3816 if (bfd_write (buff, 1, external_hdr_size, abfd) != external_hdr_size)
3818 if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
3819 ecoff_data (abfd)->raw_size, abfd)
3820 != ecoff_data (abfd)->raw_size)
3823 else if ((abfd->flags & EXEC_P) != 0
3824 && (abfd->flags & D_PAGED) != 0)
3828 /* A demand paged executable must occupy an even number of
3830 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3833 if (bfd_read (&c, 1, 1, abfd) == 0)
3835 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3838 if (bfd_write (&c, 1, 1, abfd) != 1)
3845 /* Archive handling. ECOFF uses what appears to be a unique type of
3846 archive header (which I call an armap). The byte ordering of the
3847 armap and the contents are encoded in the name of the armap itself.
3848 At least for now, we only support archives with the same byte
3849 ordering in the armap and the contents.
3851 The first four bytes in the armap are the number of symbol
3852 definitions. This is always a power of two.
3854 This is followed by the symbol definitions. Each symbol definition
3855 occupies 8 bytes. The first four bytes are the offset from the
3856 start of the armap strings to the null-terminated string naming
3857 this symbol. The second four bytes are the file offset to the
3858 archive member which defines this symbol. If the second four bytes
3859 are 0, then this is not actually a symbol definition, and it should
3862 The symbols are hashed into the armap with a closed hashing scheme.
3863 See the functions below for the details of the algorithm.
3865 We could use the hash table when looking up symbols in a library.
3866 This would require a new BFD target entry point to replace the
3867 bfd_get_next_mapent function used by the linker.
3869 After the symbol definitions comes four bytes holding the size of
3870 the string table, followed by the string table itself. */
3872 /* The name of an archive headers looks like this:
3873 __________E[BL]E[BL]_ (with a trailing space).
3874 The trailing space is changed to an X if the archive is changed to
3875 indicate that the armap is out of date.
3877 The Alpha seems to use ________64E[BL]E[BL]_. */
3879 #define ARMAP_BIG_ENDIAN 'B'
3880 #define ARMAP_LITTLE_ENDIAN 'L'
3881 #define ARMAP_MARKER 'E'
3882 #define ARMAP_START_LENGTH 10
3883 #define ARMAP_HEADER_MARKER_INDEX 10
3884 #define ARMAP_HEADER_ENDIAN_INDEX 11
3885 #define ARMAP_OBJECT_MARKER_INDEX 12
3886 #define ARMAP_OBJECT_ENDIAN_INDEX 13
3887 #define ARMAP_END_INDEX 14
3888 #define ARMAP_END "_ "
3890 /* This is a magic number used in the hashing algorithm. */
3891 #define ARMAP_HASH_MAGIC 0x9dd68ab5
3893 /* This returns the hash value to use for a string. It also sets
3894 *REHASH to the rehash adjustment if the first slot is taken. SIZE
3895 is the number of entries in the hash table, and HLOG is the log
3899 ecoff_armap_hash (s, rehash, size, hlog)
3901 unsigned int *rehash;
3909 hash = ((hash >> 27) | (hash << 5)) + *s++;
3910 hash *= ARMAP_HASH_MAGIC;
3911 *rehash = (hash & (size - 1)) | 1;
3912 return hash >> (32 - hlog);
3915 /* Read in the armap. */
3918 ecoff_slurp_armap (abfd)
3923 struct areltdata *mapdata;
3924 bfd_size_type parsed_size;
3926 struct artdata *ardata;
3929 struct symdef *symdef_ptr;
3932 /* Get the name of the first element. */
3933 i = bfd_read ((PTR) nextname, 1, 16, abfd);
3939 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
3941 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
3942 standard COFF armap. We could move the ECOFF armap stuff into
3943 bfd_slurp_armap, but that seems inappropriate since no other
3944 target uses this format. Instead, we check directly for a COFF
3946 if (strncmp (nextname, "/ ", 16) == 0)
3947 return bfd_slurp_armap (abfd);
3949 /* See if the first element is an armap. */
3950 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
3951 ARMAP_START_LENGTH) != 0
3952 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3953 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3954 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3955 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3956 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3957 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3958 || strncmp (nextname + ARMAP_END_INDEX,
3959 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3961 bfd_has_map (abfd) = false;
3965 /* Make sure we have the right byte ordering. */
3966 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3967 ^ (abfd->xvec->header_byteorder_big_p != false))
3968 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3969 ^ (abfd->xvec->byteorder_big_p != false)))
3971 bfd_error = wrong_format;
3975 /* Read in the armap. */
3976 ardata = bfd_ardata (abfd);
3977 mapdata = snarf_ar_hdr (abfd);
3978 if (mapdata == (struct areltdata *) NULL)
3980 parsed_size = mapdata->parsed_size;
3981 bfd_release (abfd, (PTR) mapdata);
3983 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3984 if (raw_armap == (char *) NULL)
3986 bfd_error = no_memory;
3990 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3992 bfd_error = malformed_archive;
3993 bfd_release (abfd, (PTR) raw_armap);
3997 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3999 ardata->symdef_count = 0;
4000 ardata->cache = (struct ar_cache *) NULL;
4002 /* This code used to overlay the symdefs over the raw archive data,
4003 but that doesn't work on a 64 bit host. */
4005 stringbase = raw_armap + count * (2 * LONG_SIZE) + 2 * LONG_SIZE;
4007 #ifdef CHECK_ARMAP_HASH
4011 /* Double check that I have the hashing algorithm right by making
4012 sure that every symbol can be looked up successfully. */
4014 for (i = 1; i < count; i <<= 1)
4016 BFD_ASSERT (i == count);
4018 raw_ptr = raw_armap + LONG_SIZE;
4019 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
4021 unsigned int name_offset, file_offset;
4022 unsigned int hash, rehash, srch;
4024 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
4025 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
4026 if (file_offset == 0)
4028 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
4033 /* See if we can rehash to this location. */
4034 for (srch = (hash + rehash) & (count - 1);
4035 srch != hash && srch != i;
4036 srch = (srch + rehash) & (count - 1))
4037 BFD_ASSERT (bfd_h_get_32 (abfd,
4040 + (srch * 2 * LONG_SIZE)
4043 BFD_ASSERT (srch == i);
4047 #endif /* CHECK_ARMAP_HASH */
4049 raw_ptr = raw_armap + LONG_SIZE;
4050 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
4051 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE)) != 0)
4052 ++ardata->symdef_count;
4054 symdef_ptr = ((struct symdef *)
4056 ardata->symdef_count * sizeof (struct symdef)));
4057 ardata->symdefs = (carsym *) symdef_ptr;
4059 raw_ptr = raw_armap + LONG_SIZE;
4060 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
4062 unsigned int name_offset, file_offset;
4064 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
4065 if (file_offset == 0)
4067 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
4068 symdef_ptr->s.name = stringbase + name_offset;
4069 symdef_ptr->file_offset = file_offset;
4073 ardata->first_file_filepos = bfd_tell (abfd);
4074 /* Pad to an even boundary. */
4075 ardata->first_file_filepos += ardata->first_file_filepos % 2;
4077 bfd_has_map (abfd) = true;
4082 /* Write out an armap. */
4085 ecoff_write_armap (abfd, elength, map, orl_count, stridx)
4087 unsigned int elength;
4089 unsigned int orl_count;
4092 unsigned int hashsize, hashlog;
4093 unsigned int symdefsize;
4095 unsigned int stringsize;
4096 unsigned int mapsize;
4099 struct stat statbuf;
4101 bfd_byte temp[LONG_SIZE];
4102 bfd_byte *hashtable;
4106 /* Ultrix appears to use as a hash table size the least power of two
4107 greater than twice the number of entries. */
4108 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
4110 hashsize = 1 << hashlog;
4112 symdefsize = hashsize * 2 * LONG_SIZE;
4114 stringsize = stridx + padit;
4116 /* Include 8 bytes to store symdefsize and stringsize in output. */
4117 mapsize = LONG_SIZE + symdefsize + stringsize + LONG_SIZE;
4119 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
4121 memset ((PTR) &hdr, 0, sizeof hdr);
4123 /* Work out the ECOFF armap name. */
4124 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
4125 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
4126 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
4127 (abfd->xvec->header_byteorder_big_p
4129 : ARMAP_LITTLE_ENDIAN);
4130 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
4131 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
4132 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
4133 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
4135 /* Write the timestamp of the archive header to be just a little bit
4136 later than the timestamp of the file, otherwise the linker will
4137 complain that the index is out of date. Actually, the Ultrix
4138 linker just checks the archive name; the GNU linker may check the
4140 stat (abfd->filename, &statbuf);
4141 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
4143 /* The DECstation uses zeroes for the uid, gid and mode of the
4145 hdr.ar_uid[0] = '0';
4146 hdr.ar_gid[0] = '0';
4147 hdr.ar_mode[0] = '0';
4149 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
4151 hdr.ar_fmag[0] = '`';
4152 hdr.ar_fmag[1] = '\n';
4154 /* Turn all null bytes in the header into spaces. */
4155 for (i = 0; i < sizeof (struct ar_hdr); i++)
4156 if (((char *)(&hdr))[i] == '\0')
4157 (((char *)(&hdr))[i]) = ' ';
4159 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
4160 != sizeof (struct ar_hdr))
4163 bfd_h_put_32 (abfd, hashsize, temp);
4164 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4167 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
4169 current = abfd->archive_head;
4171 for (i = 0; i < orl_count; i++)
4173 unsigned int hash, rehash;
4175 /* Advance firstreal to the file position of this archive
4177 if (((bfd *) map[i].pos) != last_elt)
4181 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
4182 firstreal += firstreal % 2;
4183 current = current->next;
4185 while (current != (bfd *) map[i].pos);
4190 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
4191 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4192 + (hash * 2 * LONG_SIZE)
4198 /* The desired slot is already taken. */
4199 for (srch = (hash + rehash) & (hashsize - 1);
4201 srch = (srch + rehash) & (hashsize - 1))
4202 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4203 + (srch * 2 * LONG_SIZE)
4208 BFD_ASSERT (srch != hash);
4213 bfd_h_put_32 (abfd, map[i].namidx,
4214 (PTR) (hashtable + hash * 2 * LONG_SIZE));
4215 bfd_h_put_32 (abfd, firstreal,
4216 (PTR) (hashtable + hash * 2 * LONG_SIZE + LONG_SIZE));
4219 if (bfd_write (hashtable, 1, symdefsize, abfd) != symdefsize)
4222 bfd_release (abfd, hashtable);
4224 /* Now write the strings. */
4225 bfd_h_put_32 (abfd, stringsize, temp);
4226 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4228 for (i = 0; i < orl_count; i++)
4232 len = strlen (*map[i].name) + 1;
4233 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
4237 /* The spec sez this should be a newline. But in order to be
4238 bug-compatible for DECstation ar we use a null. */
4241 if (bfd_write ("\0", 1, 1, abfd) != 1)
4248 /* See whether this BFD is an archive. If it is, read in the armap
4249 and the extended name table. */
4252 ecoff_archive_p (abfd)
4255 char armag[SARMAG + 1];
4257 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
4258 || strncmp (armag, ARMAG, SARMAG) != 0)
4260 bfd_error = wrong_format;
4261 return (bfd_target *) NULL;
4264 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
4265 involves a cast, we can't do it as the left operand of
4267 abfd->tdata.aout_ar_data =
4268 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
4270 if (bfd_ardata (abfd) == (struct artdata *) NULL)
4272 bfd_error = no_memory;
4273 return (bfd_target *) NULL;
4276 bfd_ardata (abfd)->first_file_filepos = SARMAG;
4278 if (ecoff_slurp_armap (abfd) == false
4279 || ecoff_slurp_extended_name_table (abfd) == false)
4281 bfd_release (abfd, bfd_ardata (abfd));
4282 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
4283 return (bfd_target *) NULL;