1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support added by Ian Lance Taylor, ian@cygnus.com.
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;
906 asym->section = &bfd_com_section;
911 asym->section = bfd_make_section_old_way (abfd, ".bss");
912 asym->value -= asym->section->vma;
916 asym->flags = BSF_DEBUGGING;
919 asym->section = &bfd_abs_section;
922 asym->section = &bfd_und_section;
932 asym->flags = BSF_DEBUGGING;
935 asym->section = bfd_make_section_old_way (abfd, ".sdata");
936 asym->value -= asym->section->vma;
939 asym->section = bfd_make_section_old_way (abfd, ".sbss");
941 asym->value -= asym->section->vma;
944 asym->section = bfd_make_section_old_way (abfd, ".rdata");
945 asym->value -= asym->section->vma;
948 asym->flags = BSF_DEBUGGING;
951 if (asym->value > ecoff_data (abfd)->gp_size)
953 asym->section = &bfd_com_section;
959 if (ecoff_scom_section.name == NULL)
961 /* Initialize the small common section. */
962 ecoff_scom_section.name = SCOMMON;
963 ecoff_scom_section.flags = SEC_IS_COMMON;
964 ecoff_scom_section.output_section = &ecoff_scom_section;
965 ecoff_scom_section.symbol = &ecoff_scom_symbol;
966 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
967 ecoff_scom_symbol.name = SCOMMON;
968 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
969 ecoff_scom_symbol.section = &ecoff_scom_section;
970 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
972 asym->section = &ecoff_scom_section;
977 asym->flags = BSF_DEBUGGING;
980 asym->section = &bfd_und_section;
985 asym->section = bfd_make_section_old_way (abfd, ".init");
986 asym->value -= asym->section->vma;
991 asym->flags = BSF_DEBUGGING;
994 asym->section = bfd_make_section_old_way (abfd, ".fini");
995 asym->value -= asym->section->vma;
1001 /* Look for special constructors symbols and make relocation entries
1002 in a special construction section. These are produced by the
1003 -fgnu-linker argument to g++. */
1004 if (ECOFF_IS_STAB (ecoff_sym))
1006 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
1018 arelent_chain *reloc_chain;
1019 unsigned int bitsize;
1022 /* Get a section with the same name as the symbol (usually
1023 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
1024 name ___CTOR_LIST (three underscores). We need
1025 __CTOR_LIST (two underscores), since ECOFF doesn't use
1026 a leading underscore. This should be handled by gcc,
1027 but instead we do it here. Actually, this should all
1028 be done differently anyhow. */
1029 name = bfd_asymbol_name (asym);
1030 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
1035 section = bfd_get_section_by_name (abfd, name);
1036 if (section == (asection *) NULL)
1040 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
1041 strcpy (copy, name);
1042 section = bfd_make_section (abfd, copy);
1045 /* Build a reloc pointing to this constructor. */
1047 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1048 reloc_chain->relent.sym_ptr_ptr =
1049 bfd_get_section (asym)->symbol_ptr_ptr;
1050 reloc_chain->relent.address = section->_raw_size;
1051 reloc_chain->relent.addend = asym->value;
1053 bitsize = ecoff_backend (abfd)->constructor_bitsize;
1057 reloc_index = ECOFF_R_REFWORD;
1064 reloc_chain->relent.howto = ecoff_howto_table + reloc_index;
1066 /* Set up the constructor section to hold the reloc. */
1067 section->flags = SEC_CONSTRUCTOR;
1068 ++section->reloc_count;
1070 /* Constructor sections must be rounded to a boundary
1071 based on the bitsize. These are not real sections--
1072 they are handled specially by the linker--so the ECOFF
1073 16 byte alignment restriction does not apply. */
1074 section->alignment_power = 1;
1075 while ((1 << section->alignment_power) < bitsize / 8)
1076 ++section->alignment_power;
1078 reloc_chain->next = section->constructor_chain;
1079 section->constructor_chain = reloc_chain;
1080 section->_raw_size += bitsize / 8;
1082 /* Mark the symbol as a constructor. */
1083 asym->flags |= BSF_CONSTRUCTOR;
1090 /* Read an ECOFF symbol table. */
1093 ecoff_slurp_symbol_table (abfd)
1096 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1097 const bfd_size_type external_ext_size = backend->external_ext_size;
1098 const bfd_size_type external_sym_size = backend->external_sym_size;
1099 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
1100 = backend->swap_ext_in;
1101 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
1102 = backend->swap_sym_in;
1103 bfd_size_type internal_size;
1104 ecoff_symbol_type *internal;
1105 ecoff_symbol_type *internal_ptr;
1106 asymbol *indirect_ptr;
1112 /* If we've already read in the symbol table, do nothing. */
1113 if (ecoff_data (abfd)->canonical_symbols != NULL)
1116 /* Get the symbolic information. */
1117 if (ecoff_slurp_symbolic_info (abfd) == false)
1119 if (bfd_get_symcount (abfd) == 0)
1122 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1123 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1124 if (internal == NULL)
1126 bfd_error = no_memory;
1130 internal_ptr = internal;
1131 indirect_ptr = NULL;
1132 eraw_src = (char *) ecoff_data (abfd)->external_ext;
1133 eraw_end = (eraw_src
1134 + (ecoff_data (abfd)->symbolic_header.iextMax
1135 * external_ext_size));
1136 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
1140 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
1141 internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
1142 + internal_esym.asym.iss);
1143 ecoff_set_symbol_info (abfd, &internal_esym.asym,
1144 &internal_ptr->symbol, 1, &indirect_ptr);
1145 /* The alpha uses a negative ifd field for section symbols. */
1146 if (internal_esym.ifd >= 0)
1147 internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
1149 internal_ptr->fdr = NULL;
1150 internal_ptr->local = false;
1151 internal_ptr->native = (PTR) eraw_src;
1153 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1155 /* The local symbols must be accessed via the fdr's, because the
1156 string and aux indices are relative to the fdr information. */
1157 fdr_ptr = ecoff_data (abfd)->fdr;
1158 fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
1159 for (; fdr_ptr < fdr_end; fdr_ptr++)
1164 lraw_src = ((char *) ecoff_data (abfd)->external_sym
1165 + fdr_ptr->isymBase * external_sym_size);
1166 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1168 lraw_src < lraw_end;
1169 lraw_src += external_sym_size, internal_ptr++)
1173 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1174 internal_ptr->symbol.name = (ecoff_data (abfd)->ss
1176 + internal_sym.iss);
1177 ecoff_set_symbol_info (abfd, &internal_sym,
1178 &internal_ptr->symbol, 0, &indirect_ptr);
1179 internal_ptr->fdr = fdr_ptr;
1180 internal_ptr->local = true;
1181 internal_ptr->native = (PTR) lraw_src;
1184 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1186 ecoff_data (abfd)->canonical_symbols = internal;
1191 /* Return the amount of space needed for the canonical symbols. */
1194 ecoff_get_symtab_upper_bound (abfd)
1197 if (ecoff_slurp_symbolic_info (abfd) == false
1198 || bfd_get_symcount (abfd) == 0)
1201 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1204 /* Get the canonicals symbols. */
1207 ecoff_get_symtab (abfd, alocation)
1209 asymbol **alocation;
1211 unsigned int counter = 0;
1212 ecoff_symbol_type *symbase;
1213 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1215 if (ecoff_slurp_symbol_table (abfd) == false
1216 || bfd_get_symcount (abfd) == 0)
1219 symbase = ecoff_data (abfd)->canonical_symbols;
1220 while (counter < bfd_get_symcount (abfd))
1222 *(location++) = symbase++;
1225 *location++ = (ecoff_symbol_type *) NULL;
1226 return bfd_get_symcount (abfd);
1229 /* Turn ECOFF type information into a printable string.
1230 ecoff_emit_aggregate and ecoff_type_to_string are from
1231 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1233 /* Write aggregate information to a string. */
1236 ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1243 int ifd = rndx->rfd;
1244 int indx = rndx->index;
1245 int sym_base, ss_base;
1251 sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
1252 ss_base = ecoff_data (abfd)->fdr[ifd].issBase;
1254 if (indx == indexNil)
1255 name = "/* no name */";
1258 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1262 (*backend->swap_sym_in) (abfd,
1263 ((char *) ecoff_data (abfd)->external_sym
1264 + indx * backend->external_sym_size),
1266 name = ecoff_data (abfd)->ss + ss_base + sym.iss;
1270 "%s %s { ifd = %d, index = %d }",
1272 indx + ecoff_data (abfd)->symbolic_header.iextMax);
1275 /* Convert the type information to string format. */
1278 ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1280 union aux_ext *aux_ptr;
1292 unsigned int basic_type;
1294 static char buffer1[1024];
1295 static char buffer2[1024];
1300 for (i = 0; i < 7; i++)
1302 qualifiers[i].low_bound = 0;
1303 qualifiers[i].high_bound = 0;
1304 qualifiers[i].stride = 0;
1307 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1308 return "-1 (no type)";
1309 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1311 basic_type = u.ti.bt;
1312 qualifiers[0].type = u.ti.tq0;
1313 qualifiers[1].type = u.ti.tq1;
1314 qualifiers[2].type = u.ti.tq2;
1315 qualifiers[3].type = u.ti.tq3;
1316 qualifiers[4].type = u.ti.tq4;
1317 qualifiers[5].type = u.ti.tq5;
1318 qualifiers[6].type = tqNil;
1321 * Go get the basic type.
1325 case btNil: /* undefined */
1329 case btAdr: /* address - integer same size as pointer */
1330 strcpy (p1, "address");
1333 case btChar: /* character */
1334 strcpy (p1, "char");
1337 case btUChar: /* unsigned character */
1338 strcpy (p1, "unsigned char");
1341 case btShort: /* short */
1342 strcpy (p1, "short");
1345 case btUShort: /* unsigned short */
1346 strcpy (p1, "unsigned short");
1349 case btInt: /* int */
1353 case btUInt: /* unsigned int */
1354 strcpy (p1, "unsigned int");
1357 case btLong: /* long */
1358 strcpy (p1, "long");
1361 case btULong: /* unsigned long */
1362 strcpy (p1, "unsigned long");
1365 case btFloat: /* float (real) */
1366 strcpy (p1, "float");
1369 case btDouble: /* Double (real) */
1370 strcpy (p1, "double");
1373 /* Structures add 1-2 aux words:
1374 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1375 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1377 case btStruct: /* Structure (Record) */
1378 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1379 ecoff_emit_aggregate (abfd, p1, &rndx,
1380 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1382 indx++; /* skip aux words */
1385 /* Unions add 1-2 aux words:
1386 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1387 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1389 case btUnion: /* Union */
1390 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1391 ecoff_emit_aggregate (abfd, p1, &rndx,
1392 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1394 indx++; /* skip aux words */
1397 /* Enumerations add 1-2 aux words:
1398 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1399 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1401 case btEnum: /* Enumeration */
1402 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1403 ecoff_emit_aggregate (abfd, p1, &rndx,
1404 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1406 indx++; /* skip aux words */
1409 case btTypedef: /* defined via a typedef, isymRef points */
1410 strcpy (p1, "typedef");
1413 case btRange: /* subrange of int */
1414 strcpy (p1, "subrange");
1417 case btSet: /* pascal sets */
1421 case btComplex: /* fortran complex */
1422 strcpy (p1, "complex");
1425 case btDComplex: /* fortran double complex */
1426 strcpy (p1, "double complex");
1429 case btIndirect: /* forward or unnamed typedef */
1430 strcpy (p1, "forward/unamed typedef");
1433 case btFixedDec: /* Fixed Decimal */
1434 strcpy (p1, "fixed decimal");
1437 case btFloatDec: /* Float Decimal */
1438 strcpy (p1, "float decimal");
1441 case btString: /* Varying Length Character String */
1442 strcpy (p1, "string");
1445 case btBit: /* Aligned Bit String */
1449 case btPicture: /* Picture */
1450 strcpy (p1, "picture");
1453 case btVoid: /* Void */
1454 strcpy (p1, "void");
1458 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1462 p1 += strlen (buffer1);
1465 * If this is a bitfield, get the bitsize.
1471 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1472 sprintf (p1, " : %d", bitsize);
1473 p1 += strlen (buffer1);
1478 * Deal with any qualifiers.
1480 if (qualifiers[0].type != tqNil)
1483 * Snarf up any array bounds in the correct order. Arrays
1484 * store 5 successive words in the aux. table:
1485 * word 0 RNDXR to type of the bounds (ie, int)
1486 * word 1 Current file descriptor index
1488 * word 3 high bound (or -1 if [])
1489 * word 4 stride size in bits
1491 for (i = 0; i < 7; i++)
1493 if (qualifiers[i].type == tqArray)
1495 qualifiers[i].low_bound =
1496 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1497 qualifiers[i].high_bound =
1498 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1499 qualifiers[i].stride =
1500 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1506 * Now print out the qualifiers.
1508 for (i = 0; i < 6; i++)
1510 switch (qualifiers[i].type)
1517 strcpy (p2, "ptr to ");
1518 p2 += sizeof ("ptr to ")-1;
1522 strcpy (p2, "volatile ");
1523 p2 += sizeof ("volatile ")-1;
1527 strcpy (p2, "far ");
1528 p2 += sizeof ("far ")-1;
1532 strcpy (p2, "func. ret. ");
1533 p2 += sizeof ("func. ret. ");
1538 int first_array = i;
1541 /* Print array bounds reversed (ie, in the order the C
1542 programmer writes them). C is such a fun language.... */
1544 while (i < 5 && qualifiers[i+1].type == tqArray)
1547 for (j = i; j >= first_array; j--)
1549 strcpy (p2, "array [");
1550 p2 += sizeof ("array [")-1;
1551 if (qualifiers[j].low_bound != 0)
1553 "%ld:%ld {%ld bits}",
1554 (long) qualifiers[j].low_bound,
1555 (long) qualifiers[j].high_bound,
1556 (long) qualifiers[j].stride);
1558 else if (qualifiers[j].high_bound != -1)
1561 (long) (qualifiers[j].high_bound + 1),
1562 (long) (qualifiers[j].stride));
1565 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1568 strcpy (p2, "] of ");
1569 p2 += sizeof ("] of ")-1;
1577 strcpy (p2, buffer1);
1581 /* Return information about ECOFF symbol SYMBOL in RET. */
1584 ecoff_get_symbol_info (abfd, symbol, ret)
1585 bfd *abfd; /* Ignored. */
1589 bfd_symbol_info (symbol, ret);
1592 /* Print information about an ECOFF symbol. */
1595 ecoff_print_symbol (abfd, filep, symbol, how)
1599 bfd_print_symbol_type how;
1601 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1602 FILE *file = (FILE *)filep;
1606 case bfd_print_symbol_name:
1607 fprintf (file, "%s", symbol->name);
1609 case bfd_print_symbol_more:
1610 if (ecoffsymbol (symbol)->local)
1614 (*backend->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1616 fprintf (file, "ecoff local ");
1617 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1618 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1619 (unsigned) ecoff_sym.sc);
1625 (*backend->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1627 fprintf (file, "ecoff extern ");
1628 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1629 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1630 (unsigned) ecoff_ext.asym.sc);
1633 case bfd_print_symbol_all:
1634 /* Print out the symbols in a reasonable way */
1643 if (ecoffsymbol (symbol)->local)
1645 (*backend->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1648 pos = ((((char *) ecoffsymbol (symbol)->native
1649 - (char *) ecoff_data (abfd)->external_sym)
1650 / backend->external_sym_size)
1651 + ecoff_data (abfd)->symbolic_header.iextMax);
1658 (*backend->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1661 pos = (((char *) ecoffsymbol (symbol)->native
1662 - (char *) ecoff_data (abfd)->external_ext)
1663 / backend->external_ext_size);
1664 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1665 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1666 weakext = ecoff_ext.weakext ? 'w' : ' ';
1669 fprintf (file, "[%3d] %c ",
1671 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1672 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1673 (unsigned) ecoff_ext.asym.st,
1674 (unsigned) ecoff_ext.asym.sc,
1675 (unsigned) ecoff_ext.asym.index,
1676 jmptbl, cobol_main, weakext,
1679 if (ecoffsymbol (symbol)->fdr != NULL
1680 && ecoff_ext.asym.index != indexNil)
1684 bfd_size_type sym_base;
1685 union aux_ext *aux_base;
1687 indx = ecoff_ext.asym.index;
1689 /* sym_base is used to map the fdr relative indices which
1690 appear in the file to the position number which we are
1692 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1693 if (ecoffsymbol (symbol)->local)
1694 sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1696 /* aux_base is the start of the aux entries for this file;
1697 asym.index is an offset from this. */
1698 aux_base = (ecoff_data (abfd)->external_aux
1699 + ecoffsymbol (symbol)->fdr->iauxBase);
1701 /* The aux entries are stored in host byte order; the
1702 order is indicated by a bit in the fdr. */
1703 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1705 /* This switch is basically from gcc/mips-tdump.c */
1706 switch (ecoff_ext.asym.st)
1714 fprintf (file, "\n End+1 symbol: %ld",
1715 (long) (indx + sym_base));
1719 if (ecoff_ext.asym.sc == scText
1720 || ecoff_ext.asym.sc == scInfo)
1721 fprintf (file, "\n First symbol: %ld",
1722 (long) (indx + sym_base));
1724 fprintf (file, "\n First symbol: %ld",
1725 (long) (AUX_GET_ISYM (bigendian,
1726 &aux_base[ecoff_ext.asym.index])
1732 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1734 else if (ecoffsymbol (symbol)->local)
1735 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1736 (long) (AUX_GET_ISYM (bigendian,
1737 &aux_base[ecoff_ext.asym.index])
1739 ecoff_type_to_string (abfd, aux_base, indx + 1,
1742 fprintf (file, "\n Local symbol: %d",
1745 + ecoff_data (abfd)->symbolic_header.iextMax));
1749 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1750 fprintf (file, "\n Type: %s",
1751 ecoff_type_to_string (abfd, aux_base, indx,
1761 /* ECOFF relocs are either against external symbols, or against
1762 sections. If we are producing relocateable output, and the reloc
1763 is against an external symbol, and nothing has given us any
1764 additional addend, the resulting reloc will also be against the
1765 same symbol. In such a case, we don't want to change anything
1766 about the way the reloc is handled, since it will all be done at
1767 final link time. Rather than put special case code into
1768 bfd_perform_relocation, all the reloc types use this howto
1769 function. It just short circuits the reloc if producing
1770 relocateable output against an external symbol. */
1772 static bfd_reloc_status_type
1773 ecoff_generic_reloc (abfd,
1780 arelent *reloc_entry;
1783 asection *input_section;
1786 if (output_bfd != (bfd *) NULL
1787 && (symbol->flags & BSF_SECTION_SYM) == 0
1788 && reloc_entry->addend == 0)
1790 reloc_entry->address += input_section->output_offset;
1791 return bfd_reloc_ok;
1794 return bfd_reloc_continue;
1797 /* Do a REFHI relocation. This has to be done in combination with a
1798 REFLO reloc, because there is a carry from the REFLO to the REFHI.
1799 Here we just save the information we need; we do the actual
1800 relocation when we see the REFLO. ECOFF requires that the REFLO
1801 immediately follow the REFHI, so this ought to work. */
1803 static bfd_byte *ecoff_refhi_addr;
1804 static bfd_vma ecoff_refhi_addend;
1806 static bfd_reloc_status_type
1807 ecoff_refhi_reloc (abfd,
1814 arelent *reloc_entry;
1817 asection *input_section;
1820 bfd_reloc_status_type ret;
1823 /* If we're relocating, and this an external symbol, we don't want
1824 to change anything. */
1825 if (output_bfd != (bfd *) NULL
1826 && (symbol->flags & BSF_SECTION_SYM) == 0
1827 && reloc_entry->addend == 0)
1829 reloc_entry->address += input_section->output_offset;
1830 return bfd_reloc_ok;
1834 if (symbol->section == &bfd_und_section
1835 && output_bfd == (bfd *) NULL)
1836 ret = bfd_reloc_undefined;
1838 if (bfd_is_com_section (symbol->section))
1841 relocation = symbol->value;
1843 relocation += symbol->section->output_section->vma;
1844 relocation += symbol->section->output_offset;
1845 relocation += reloc_entry->addend;
1847 if (reloc_entry->address > input_section->_cooked_size)
1848 return bfd_reloc_outofrange;
1850 /* Save the information, and let REFLO do the actual relocation. */
1851 ecoff_refhi_addr = (bfd_byte *) data + reloc_entry->address;
1852 ecoff_refhi_addend = relocation;
1854 if (output_bfd != (bfd *) NULL)
1855 reloc_entry->address += input_section->output_offset;
1860 /* Do a REFLO relocation. This is a straightforward 16 bit inplace
1861 relocation; this function exists in order to do the REFHI
1862 relocation described above. */
1864 static bfd_reloc_status_type
1865 ecoff_reflo_reloc (abfd,
1872 arelent *reloc_entry;
1875 asection *input_section;
1878 if (ecoff_refhi_addr != (bfd_byte *) NULL)
1882 unsigned long vallo;
1884 /* Do the REFHI relocation. Note that we actually don't need to
1885 know anything about the REFLO itself, except where to find
1886 the low 16 bits of the addend needed by the REFHI. */
1887 insn = bfd_get_32 (abfd, ecoff_refhi_addr);
1888 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1890 val = ((insn & 0xffff) << 16) + vallo;
1891 val += ecoff_refhi_addend;
1893 /* The low order 16 bits are always treated as a signed value.
1894 Therefore, a negative value in the low order bits requires an
1895 adjustment in the high order bits. We need to make this
1896 adjustment in two ways: once for the bits we took from the
1897 data, and once for the bits we are putting back in to the
1899 if ((vallo & 0x8000) != 0)
1901 if ((val & 0x8000) != 0)
1904 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1905 bfd_put_32 (abfd, insn, ecoff_refhi_addr);
1907 ecoff_refhi_addr = (bfd_byte *) NULL;
1910 /* Now do the REFLO reloc in the usual way. */
1911 return ecoff_generic_reloc (abfd, reloc_entry, symbol, data,
1912 input_section, output_bfd);
1915 /* Do a GPREL relocation. This is a 16 bit value which must become
1916 the offset from the gp register. */
1918 static bfd_reloc_status_type
1919 ecoff_gprel_reloc (abfd,
1926 arelent *reloc_entry;
1929 asection *input_section;
1932 boolean relocateable;
1937 /* If we're relocating, and this is an external symbol with no
1938 addend, we don't want to change anything. We will only have an
1939 addend if this is a newly created reloc, not read from an ECOFF
1941 if (output_bfd != (bfd *) NULL
1942 && (symbol->flags & BSF_SECTION_SYM) == 0
1943 && reloc_entry->addend == 0)
1945 reloc_entry->address += input_section->output_offset;
1946 return bfd_reloc_ok;
1949 if (output_bfd != (bfd *) NULL)
1950 relocateable = true;
1953 relocateable = false;
1954 output_bfd = symbol->section->output_section->owner;
1957 if (symbol->section == &bfd_und_section
1958 && relocateable == false)
1959 return bfd_reloc_undefined;
1961 /* We have to figure out the gp value, so that we can adjust the
1962 symbol value correctly. We look up the symbol _gp in the output
1963 BFD. If we can't find it, we're stuck. We cache it in the ECOFF
1964 target data. We don't need to adjust the symbol value for an
1965 external symbol if we are producing relocateable output. */
1966 if (ecoff_data (output_bfd)->gp == 0
1967 && (relocateable == false
1968 || (symbol->flags & BSF_SECTION_SYM) != 0))
1970 if (relocateable != false)
1972 /* Make up a value. */
1973 ecoff_data (output_bfd)->gp =
1974 symbol->section->output_section->vma + 0x4000;
1982 count = bfd_get_symcount (output_bfd);
1983 sym = bfd_get_outsymbols (output_bfd);
1985 /* We should do something more friendly here, but we don't
1986 have a good reloc status to return. */
1987 if (sym == (asymbol **) NULL)
1990 for (i = 0; i < count; i++, sym++)
1992 register CONST char *name;
1994 name = bfd_asymbol_name (*sym);
1995 if (*name == '_' && strcmp (name, "_gp") == 0)
1997 ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
2002 /* We should do something more friendly here, but we don't have
2003 a good reloc status to return. */
2009 if (bfd_is_com_section (symbol->section))
2012 relocation = symbol->value;
2014 relocation += symbol->section->output_section->vma;
2015 relocation += symbol->section->output_offset;
2017 if (reloc_entry->address > input_section->_cooked_size)
2018 return bfd_reloc_outofrange;
2020 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
2022 /* Set val to the offset into the section or symbol. */
2023 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
2027 /* Adjust val for the final section location and GP value. If we
2028 are producing relocateable output, we don't want to do this for
2029 an external symbol. */
2030 if (relocateable == false
2031 || (symbol->flags & BSF_SECTION_SYM) != 0)
2032 val += relocation - ecoff_data (output_bfd)->gp;
2034 insn = (insn &~ 0xffff) | (val & 0xffff);
2035 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
2037 if (relocateable != false)
2038 reloc_entry->address += input_section->output_offset;
2040 /* Make sure it fit in 16 bits. */
2041 if (val >= 0x8000 && val < 0xffff8000)
2042 return bfd_reloc_outofrange;
2044 return bfd_reloc_ok;
2047 /* Read in the relocs for a section. */
2050 ecoff_slurp_reloc_table (abfd, section, symbols)
2055 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2056 arelent *internal_relocs;
2057 bfd_size_type external_reloc_size;
2058 bfd_size_type external_relocs_size;
2059 char *external_relocs;
2063 if (section->relocation != (arelent *) NULL
2064 || section->reloc_count == 0
2065 || (section->flags & SEC_CONSTRUCTOR) != 0)
2068 if (ecoff_slurp_symbol_table (abfd) == false)
2071 internal_relocs = (arelent *) bfd_alloc (abfd,
2073 * section->reloc_count));
2074 external_reloc_size = backend->external_reloc_size;
2075 external_relocs_size = external_reloc_size * section->reloc_count;
2076 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
2077 if (internal_relocs == (arelent *) NULL
2078 || external_relocs == (char *) NULL)
2080 bfd_error = no_memory;
2083 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
2085 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
2086 != external_relocs_size)
2088 bfd_error = system_call_error;
2092 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
2094 struct internal_reloc intern;
2096 (*backend->swap_reloc_in) (abfd,
2097 external_relocs + i * external_reloc_size,
2100 if (intern.r_type > ECOFF_R_LITERAL)
2103 if (intern.r_extern)
2105 /* r_symndx is an index into the external symbols. */
2106 BFD_ASSERT (intern.r_symndx >= 0
2108 < ecoff_data (abfd)->symbolic_header.iextMax));
2109 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
2114 CONST char *sec_name;
2117 /* r_symndx is a section key. */
2118 switch (intern.r_symndx)
2120 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
2121 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
2122 case RELOC_SECTION_DATA: sec_name = ".data"; break;
2123 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
2124 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
2125 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
2126 case RELOC_SECTION_INIT: sec_name = ".init"; break;
2127 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
2128 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
2132 sec = bfd_get_section_by_name (abfd, sec_name);
2133 if (sec == (asection *) NULL)
2135 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
2137 rptr->addend = - bfd_get_section_vma (abfd, sec);
2138 if (intern.r_type == ECOFF_R_GPREL
2139 || intern.r_type == ECOFF_R_LITERAL)
2140 rptr->addend += ecoff_data (abfd)->gp;
2143 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
2144 rptr->howto = &ecoff_howto_table[intern.r_type];
2146 /* If the type is ECOFF_R_IGNORE, make sure this is a reference
2147 to the absolute section so that the reloc is ignored. */
2148 if (intern.r_type == ECOFF_R_IGNORE)
2149 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2152 bfd_release (abfd, external_relocs);
2154 section->relocation = internal_relocs;
2159 /* Get a canonical list of relocs. */
2162 ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
2170 if (section->flags & SEC_CONSTRUCTOR)
2172 arelent_chain *chain;
2174 /* This section has relocs made up by us, not the file, so take
2175 them out of their chain and place them into the data area
2177 for (count = 0, chain = section->constructor_chain;
2178 count < section->reloc_count;
2179 count++, chain = chain->next)
2180 *relptr++ = &chain->relent;
2186 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
2189 tblptr = section->relocation;
2190 if (tblptr == (arelent *) NULL)
2193 for (count = 0; count < section->reloc_count; count++)
2194 *relptr++ = tblptr++;
2197 *relptr = (arelent *) NULL;
2199 return section->reloc_count;
2202 /* Get the howto structure for a generic reloc type. */
2204 CONST struct reloc_howto_struct *
2205 ecoff_bfd_reloc_type_lookup (abfd, code)
2207 bfd_reloc_code_real_type code;
2214 ecoff_type = ECOFF_R_REFHALF;
2217 ecoff_type = ECOFF_R_REFWORD;
2219 case BFD_RELOC_MIPS_JMP:
2220 ecoff_type = ECOFF_R_JMPADDR;
2222 case BFD_RELOC_HI16_S:
2223 ecoff_type = ECOFF_R_REFHI;
2225 case BFD_RELOC_LO16:
2226 ecoff_type = ECOFF_R_REFLO;
2228 case BFD_RELOC_MIPS_GPREL:
2229 ecoff_type = ECOFF_R_GPREL;
2232 return (CONST struct reloc_howto_struct *) NULL;
2235 return &ecoff_howto_table[ecoff_type];
2238 /* Provided a BFD, a section and an offset into the section, calculate
2239 and return the name of the source file and the line nearest to the
2243 ecoff_find_nearest_line (abfd,
2252 asymbol **ignore_symbols;
2254 CONST char **filename_ptr;
2255 CONST char **functionname_ptr;
2256 unsigned int *retline_ptr;
2258 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2263 bfd_size_type external_pdr_size;
2267 unsigned char *line_ptr;
2268 unsigned char *line_end;
2271 /* If we're not in the .text section, we don't have any line
2273 if (strcmp (section->name, _TEXT) != 0
2274 || offset < ecoff_data (abfd)->text_start
2275 || offset >= ecoff_data (abfd)->text_end)
2278 /* Make sure we have the FDR's. */
2279 if (ecoff_slurp_symbolic_info (abfd) == false
2280 || bfd_get_symcount (abfd) == 0)
2283 /* Each file descriptor (FDR) has a memory address. Here we track
2284 down which FDR we want. The FDR's are stored in increasing
2285 memory order. If speed is ever important, this can become a
2286 binary search. We must ignore FDR's with no PDR entries; they
2287 will have the adr of the FDR before or after them. */
2288 fdr_start = ecoff_data (abfd)->fdr;
2289 fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
2290 fdr_hold = (FDR *) NULL;
2291 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2293 if (fdr_ptr->cpd == 0)
2295 if (offset < fdr_ptr->adr)
2299 if (fdr_hold == (FDR *) NULL)
2303 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2304 have an address, which is relative to the FDR address, and are
2305 also stored in increasing memory order. */
2306 offset -= fdr_ptr->adr;
2307 external_pdr_size = backend->external_pdr_size;
2308 pdr_ptr = ((char *) ecoff_data (abfd)->external_pdr
2309 + fdr_ptr->ipdFirst * external_pdr_size);
2310 pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2311 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2313 /* The address of the first PDR is an offset which applies to the
2314 addresses of all the PDR's. */
2317 for (pdr_ptr += external_pdr_size;
2319 pdr_ptr += external_pdr_size)
2321 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2322 if (offset < pdr.adr)
2326 /* Now we can look for the actual line number. The line numbers are
2327 stored in a very funky format, which I won't try to describe.
2328 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2329 we want; we need this to compute line_end. */
2330 line_end = ecoff_data (abfd)->line;
2331 if (pdr_ptr == pdr_end)
2332 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2334 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2336 /* Now change pdr and pdr_ptr to the one we want. */
2337 pdr_ptr -= external_pdr_size;
2338 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2342 line_ptr = (ecoff_data (abfd)->line
2343 + fdr_ptr->cbLineOffset
2344 + pdr.cbLineOffset);
2345 while (line_ptr < line_end)
2350 delta = *line_ptr >> 4;
2353 count = (*line_ptr & 0xf) + 1;
2357 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2358 if (delta >= 0x8000)
2363 if (offset < count * 4)
2365 offset -= count * 4;
2368 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2369 at least according to gdb/mipsread.c. */
2370 if (fdr_ptr->rss == -1)
2372 *filename_ptr = NULL;
2374 *functionname_ptr = NULL;
2379 (*backend->swap_ext_in) (abfd,
2380 ((char *) ecoff_data (abfd)->external_ext
2381 + pdr.isym * backend->external_ext_size),
2383 *functionname_ptr = ecoff_data (abfd)->ssext + proc_ext.asym.iss;
2390 *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
2391 (*backend->swap_sym_in) (abfd,
2392 ((char *) ecoff_data (abfd)->external_sym
2393 + ((fdr_ptr->isymBase + pdr.isym)
2394 * backend->external_sym_size)),
2396 *functionname_ptr = (ecoff_data (abfd)->ss
2400 if (lineno == ilineNil)
2402 *retline_ptr = lineno;
2406 /* We can't use the generic linking routines for ECOFF, because we
2407 have to handle all the debugging information. The generic link
2408 routine just works out the section contents and attaches a list of
2411 We link by looping over all the seclets. We make two passes. On
2412 the first we set the actual section contents and determine the size
2413 of the debugging information. On the second we accumulate the
2414 debugging information and write it out.
2416 This currently always accumulates the debugging information, which
2417 is incorrect, because it ignores the -s and -S options of the
2418 linker. The linker needs to be modified to give us that
2419 information in a more useful format (currently it just provides a
2420 list of symbols which should appear in the output file). */
2422 /* Clear the output_has_begun flag for all the input BFD's. We use it
2423 to avoid linking in the debugging information for a BFD more than
2427 ecoff_clear_output_flags (abfd)
2430 register asection *o;
2431 register bfd_seclet_type *p;
2433 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2434 for (p = o->seclets_head;
2435 p != (bfd_seclet_type *) NULL;
2437 if (p->type == bfd_indirect_seclet)
2438 p->u.indirect.section->owner->output_has_begun = false;
2441 /* Handle an indirect seclet on the first pass. Set the contents of
2442 the output section, and accumulate the debugging information if
2446 ecoff_rel (output_bfd, seclet, output_section, data, relocateable)
2448 bfd_seclet_type *seclet;
2449 asection *output_section;
2451 boolean relocateable;
2454 HDRR *output_symhdr;
2457 if ((output_section->flags & SEC_HAS_CONTENTS)
2458 && !(output_section->flags & SEC_NEVER_LOAD)
2459 && (output_section->flags & SEC_LOAD)
2462 data = (PTR) bfd_get_relocated_section_contents (output_bfd,
2466 if (bfd_set_section_contents (output_bfd,
2477 input_bfd = seclet->u.indirect.section->owner;
2479 /* We want to figure out how much space will be required to
2480 incorporate all the debugging information from input_bfd. We use
2481 the output_has_begun field to avoid adding it in more than once.
2482 The actual incorporation is done in the second pass, in
2483 ecoff_get_debug. The code has to parallel that code in its
2484 manipulations of output_symhdr. */
2486 if (input_bfd->output_has_begun)
2488 input_bfd->output_has_begun = true;
2490 output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
2492 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2498 /* We just accumulate local symbols from a non-ECOFF BFD. The
2499 external symbols are handled separately. */
2501 symbols = (asymbol **) bfd_alloc (output_bfd,
2502 get_symtab_upper_bound (input_bfd));
2503 if (symbols == (asymbol **) NULL)
2505 bfd_error = no_memory;
2508 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2510 for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
2514 len = strlen ((*sym_ptr)->name);
2515 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2517 ++output_symhdr->isymMax;
2518 output_symhdr->issMax += len + 1;
2522 bfd_release (output_bfd, (PTR) symbols);
2524 ++output_symhdr->ifdMax;
2529 /* We simply add in the information from another ECOFF BFD. First
2530 we make sure we have the symbolic information. */
2531 if (ecoff_slurp_symbol_table (input_bfd) == false)
2533 if (bfd_get_symcount (input_bfd) == 0)
2536 input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2538 /* Figure out how much information we are going to be putting in.
2539 The external symbols are handled separately. */
2540 output_symhdr->ilineMax += input_symhdr->ilineMax;
2541 output_symhdr->cbLine += input_symhdr->cbLine;
2542 output_symhdr->idnMax += input_symhdr->idnMax;
2543 output_symhdr->ipdMax += input_symhdr->ipdMax;
2544 output_symhdr->isymMax += input_symhdr->isymMax;
2545 output_symhdr->ioptMax += input_symhdr->ioptMax;
2546 output_symhdr->iauxMax += input_symhdr->iauxMax;
2547 output_symhdr->issMax += input_symhdr->issMax;
2548 output_symhdr->ifdMax += input_symhdr->ifdMax;
2550 /* The RFD's are special, since we create them if needed. */
2551 if (input_symhdr->crfd > 0)
2552 output_symhdr->crfd += input_symhdr->crfd;
2554 output_symhdr->crfd += input_symhdr->ifdMax;
2559 /* Handle an arbitrary seclet on the first pass. */
2562 ecoff_dump_seclet (abfd, seclet, section, data, relocateable)
2564 bfd_seclet_type *seclet;
2567 boolean relocateable;
2569 switch (seclet->type)
2571 case bfd_indirect_seclet:
2572 /* The contents of this section come from another one somewhere
2574 return ecoff_rel (abfd, seclet, section, data, relocateable);
2576 case bfd_fill_seclet:
2577 /* Fill in the section with fill.value. This is used to pad out
2578 sections, but we must avoid padding the .bss section. */
2579 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2581 if (seclet->u.fill.value != 0)
2586 char *d = (char *) bfd_alloc (abfd, seclet->size);
2590 for (i = 0; i < seclet->size; i+=2)
2591 d[i] = seclet->u.fill.value >> 8;
2592 for (i = 1; i < seclet->size; i+=2)
2593 d[i] = seclet->u.fill.value;
2594 ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2596 bfd_release (abfd, (PTR) d);
2608 /* Add a string to the debugging information we are accumulating for a
2609 file. Return the offset from the fdr string base or from the
2610 external string base. */
2613 ecoff_add_string (output_bfd, fdr, string, external)
2623 symhdr = &ecoff_data (output_bfd)->symbolic_header;
2624 len = strlen (string);
2627 strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2628 ret = symhdr->issExtMax;
2629 symhdr->issExtMax += len + 1;
2633 strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2635 symhdr->issMax += len + 1;
2636 fdr->cbSs += len + 1;
2641 /* Accumulate the debugging information from an input section. */
2644 ecoff_get_debug (output_bfd, seclet, section, relocateable)
2646 bfd_seclet_type *seclet;
2648 boolean relocateable;
2650 const struct ecoff_backend_data * const backend = ecoff_backend (output_bfd);
2651 const bfd_size_type external_sym_size = backend->external_sym_size;
2652 const bfd_size_type external_pdr_size = backend->external_pdr_size;
2653 const bfd_size_type external_fdr_size = backend->external_fdr_size;
2654 const bfd_size_type external_rfd_size = backend->external_rfd_size;
2655 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
2656 = backend->swap_sym_in;
2657 void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
2658 = backend->swap_sym_out;
2659 void (* const swap_pdr_in) PARAMS ((bfd *, PTR, PDR *))
2660 = backend->swap_pdr_in;
2661 void (* const swap_fdr_out) PARAMS ((bfd *, const FDR *, PTR))
2662 = backend->swap_fdr_out;
2663 void (* const swap_rfd_out) PARAMS ((bfd *, const RFDT *, PTR))
2664 = backend->swap_rfd_out;
2666 HDRR *output_symhdr;
2668 ecoff_data_type *output_ecoff;
2669 ecoff_data_type *input_ecoff;
2672 ecoff_symbol_type *esym_ptr;
2673 ecoff_symbol_type *esym_end;
2678 input_bfd = seclet->u.indirect.section->owner;
2680 /* Don't get the information more than once. */
2681 if (input_bfd->output_has_begun)
2683 input_bfd->output_has_begun = true;
2685 output_ecoff = ecoff_data (output_bfd);
2686 output_symhdr = &output_ecoff->symbolic_header;
2688 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2695 /* This is not an ECOFF BFD. Just gather the symbols. */
2697 memset (&fdr, 0, sizeof fdr);
2699 fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2700 fdr.issBase = output_symhdr->issMax;
2702 fdr.rss = ecoff_add_string (output_bfd,
2704 bfd_get_filename (input_bfd),
2706 fdr.isymBase = output_symhdr->isymMax;
2708 /* Get the local symbols from the input BFD. */
2709 symbols = (asymbol **) bfd_alloc (output_bfd,
2710 get_symtab_upper_bound (input_bfd));
2711 if (symbols == (asymbol **) NULL)
2713 bfd_error = no_memory;
2716 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2718 /* Handle the local symbols. Any external symbols are handled
2721 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2725 if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2727 memset (&internal_sym, 0, sizeof internal_sym);
2728 internal_sym.iss = ecoff_add_string (output_bfd,
2733 if (bfd_is_com_section ((*sym_ptr)->section)
2734 || (*sym_ptr)->section == &bfd_und_section)
2735 internal_sym.value = (*sym_ptr)->value;
2737 internal_sym.value = ((*sym_ptr)->value
2738 + (*sym_ptr)->section->output_offset
2739 + (*sym_ptr)->section->output_section->vma);
2740 internal_sym.st = stNil;
2741 internal_sym.sc = scUndefined;
2742 internal_sym.index = indexNil;
2743 (*swap_sym_out) (output_bfd, &internal_sym,
2744 ((char *) output_ecoff->external_sym
2745 + output_symhdr->isymMax * external_sym_size));
2747 ++output_symhdr->isymMax;
2750 bfd_release (output_bfd, (PTR) symbols);
2752 /* Leave everything else in the FDR zeroed out. This will cause
2753 the lang field to be langC. The fBigendian field will
2754 indicate little endian format, but it doesn't matter because
2755 it only applies to aux fields and there are none. */
2757 (*swap_fdr_out) (output_bfd, &fdr,
2758 ((char *) output_ecoff->external_fdr
2759 + output_symhdr->ifdMax * external_fdr_size));
2760 ++output_symhdr->ifdMax;
2764 /* This is an ECOFF BFD. We want to grab the information from
2765 input_bfd and attach it to output_bfd. */
2766 count = bfd_get_symcount (input_bfd);
2769 input_ecoff = ecoff_data (input_bfd);
2770 input_symhdr = &input_ecoff->symbolic_header;
2772 /* I think that it is more efficient to simply copy the debugging
2773 information from the input BFD to the output BFD. Because ECOFF
2774 uses relative pointers for most of the debugging information,
2775 only a little of it has to be changed at all. */
2777 /* Swap in the local symbols, adjust their values, and swap them out
2778 again. The external symbols are handled separately. */
2779 sym_out = ((char *) output_ecoff->external_sym
2780 + output_symhdr->isymMax * external_sym_size);
2782 esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
2783 esym_end = esym_ptr + count;
2784 for (; esym_ptr < esym_end; esym_ptr++)
2786 if (esym_ptr->local)
2790 (*swap_sym_in) (input_bfd, esym_ptr->native, &sym);
2792 /* If we're producing an executable, move common symbols
2794 if (relocateable == false)
2796 if (sym.sc == scCommon)
2798 else if (sym.sc == scSCommon)
2802 if (! bfd_is_com_section (esym_ptr->symbol.section)
2803 && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
2804 && esym_ptr->symbol.section != &bfd_und_section)
2805 sym.value = (esym_ptr->symbol.value
2806 + esym_ptr->symbol.section->output_offset
2807 + esym_ptr->symbol.section->output_section->vma);
2808 (*swap_sym_out) (output_bfd, &sym, sym_out);
2809 sym_out += external_sym_size;
2813 /* That should have accounted for all the local symbols in
2816 /* Copy the information that does not need swapping. */
2817 memcpy (output_ecoff->line + output_symhdr->cbLine,
2819 input_symhdr->cbLine * sizeof (unsigned char));
2820 memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2821 input_ecoff->external_aux,
2822 input_symhdr->iauxMax * sizeof (union aux_ext));
2823 memcpy (output_ecoff->ss + output_symhdr->issMax,
2825 input_symhdr->issMax * sizeof (char));
2827 /* Some of the information may need to be swapped. */
2828 if (output_bfd->xvec->header_byteorder_big_p
2829 == input_bfd->xvec->header_byteorder_big_p)
2831 /* The two BFD's have the same endianness, so memcpy will
2833 if (input_symhdr->idnMax > 0)
2834 memcpy (((char *) output_ecoff->external_dnr
2835 + output_symhdr->idnMax * backend->external_dnr_size),
2836 input_ecoff->external_dnr,
2837 input_symhdr->idnMax * backend->external_dnr_size);
2838 if (input_symhdr->ipdMax > 0)
2839 memcpy (((char *) output_ecoff->external_pdr
2840 + output_symhdr->ipdMax * external_pdr_size),
2841 input_ecoff->external_pdr,
2842 input_symhdr->ipdMax * external_pdr_size);
2843 if (input_symhdr->ioptMax > 0)
2844 memcpy (((char *) output_ecoff->external_opt
2845 + output_symhdr->ioptMax * backend->external_opt_size),
2846 input_ecoff->external_opt,
2847 input_symhdr->ioptMax * backend->external_opt_size);
2856 /* The two BFD's have different endianness, so we must swap
2857 everything in and out. This code would always work, but it
2858 would be slow in the normal case. */
2859 sz = backend->external_dnr_size;
2860 in = (char *) input_ecoff->external_dnr;
2861 end = in + input_symhdr->idnMax * sz;
2862 out = (char *) output_ecoff->external_dnr + output_symhdr->idnMax * sz;
2863 for (; in < end; in += sz, out += sz)
2867 (*backend->swap_dnr_in) (input_bfd, in, &dnr);
2868 (*backend->swap_dnr_out) (output_bfd, &dnr, out);
2871 sz = external_pdr_size;
2872 in = (char *) input_ecoff->external_pdr;
2873 end = in + input_symhdr->ipdMax * sz;
2874 out = (char *) output_ecoff->external_pdr + output_symhdr->ipdMax * sz;
2875 for (; in < end; in += sz, out += sz)
2879 (*swap_pdr_in) (input_bfd, in, &pdr);
2880 (*backend->swap_pdr_out) (output_bfd, &pdr, out);
2883 sz = backend->external_opt_size;
2884 in = (char *) input_ecoff->external_opt;
2885 end = in + input_symhdr->ioptMax * sz;
2886 out = (char *) output_ecoff->external_opt + output_symhdr->ioptMax * sz;
2887 for (; in < end; in += sz, out += sz)
2891 (*backend->swap_opt_in) (input_bfd, in, &opt);
2892 (*backend->swap_opt_out) (output_bfd, &opt, out);
2896 /* Set ifdbase so that the external symbols know how to adjust their
2898 input_ecoff->ifdbase = output_symhdr->ifdMax;
2900 fdr_ptr = input_ecoff->fdr;
2901 fdr_end = fdr_ptr + input_symhdr->ifdMax;
2902 fdr_out = ((char *) output_ecoff->external_fdr
2903 + output_symhdr->ifdMax * external_fdr_size);
2904 for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out += external_fdr_size)
2907 unsigned long pdr_off;
2911 /* The memory address for this fdr is the address for the seclet
2912 plus the offset to this fdr within input_bfd. For some
2913 reason the offset of the first procedure pointer is also
2921 (*swap_pdr_in) (input_bfd,
2922 ((char *) input_ecoff->external_pdr
2923 + fdr.ipdFirst * external_pdr_size),
2927 fdr.adr = (bfd_get_section_vma (output_bfd, section)
2929 + (fdr_ptr->adr - input_ecoff->fdr->adr)
2932 fdr.issBase += output_symhdr->issMax;
2933 fdr.isymBase += output_symhdr->isymMax;
2934 fdr.ilineBase += output_symhdr->ilineMax;
2935 fdr.ioptBase += output_symhdr->ioptMax;
2936 fdr.ipdFirst += output_symhdr->ipdMax;
2937 fdr.iauxBase += output_symhdr->iauxMax;
2938 fdr.rfdBase += output_symhdr->crfd;
2940 /* If there are no RFD's, we are going to add some. We don't
2941 want to adjust irfd for this, so that all the FDR's can share
2943 if (input_symhdr->crfd == 0)
2944 fdr.crfd = input_symhdr->ifdMax;
2946 if (fdr.cbLine != 0)
2947 fdr.cbLineOffset += output_symhdr->cbLine;
2949 (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
2952 if (input_symhdr->crfd > 0)
2954 void (* const swap_rfd_in) PARAMS ((bfd *, PTR, RFDT *))
2955 = backend->swap_rfd_in;
2960 /* Swap and adjust the RFD's. RFD's are only created by the
2961 linker, so this will only be necessary if one of the input
2962 files is the result of a partial link. Presumably all
2963 necessary RFD's are present. */
2964 rfd_in = (char *) input_ecoff->external_rfd;
2965 rfd_end = rfd_in + input_symhdr->crfd * external_rfd_size;
2966 rfd_out = ((char *) output_ecoff->external_rfd
2967 + output_symhdr->crfd * external_rfd_size);
2970 rfd_in += external_rfd_size, rfd_out += external_rfd_size)
2974 (*swap_rfd_in) (input_bfd, rfd_in, &rfd);
2975 rfd += output_symhdr->ifdMax;
2976 (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
2978 output_symhdr->crfd += input_symhdr->crfd;
2986 /* Create RFD's. Some of the debugging information includes
2987 relative file indices. These indices are taken as indices to
2988 the RFD table if there is one, or to the global table if
2989 there is not. If we did not create RFD's, we would have to
2990 parse and adjust all the debugging information which contains
2992 rfd = output_symhdr->ifdMax;
2993 rfd_out = ((char *) output_ecoff->external_rfd
2994 + output_symhdr->crfd * external_rfd_size);
2995 rfd_end = rfd_out + input_symhdr->ifdMax * external_rfd_size;
2996 for (; rfd_out < rfd_end; rfd_out += external_rfd_size, rfd++)
2997 (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
2998 output_symhdr->crfd += input_symhdr->ifdMax;
3001 /* Combine the register masks. Not all of these are used on all
3002 targets, but that's OK because only the relevant ones will be
3003 swapped in and out. */
3007 output_ecoff->gprmask |= input_ecoff->gprmask;
3008 output_ecoff->fprmask |= input_ecoff->fprmask;
3009 for (i = 0; i < 4; i++)
3010 output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
3013 /* Update the counts. */
3014 output_symhdr->ilineMax += input_symhdr->ilineMax;
3015 output_symhdr->cbLine += input_symhdr->cbLine;
3016 output_symhdr->idnMax += input_symhdr->idnMax;
3017 output_symhdr->ipdMax += input_symhdr->ipdMax;
3018 output_symhdr->isymMax += input_symhdr->isymMax;
3019 output_symhdr->ioptMax += input_symhdr->ioptMax;
3020 output_symhdr->iauxMax += input_symhdr->iauxMax;
3021 output_symhdr->issMax += input_symhdr->issMax;
3022 output_symhdr->ifdMax += input_symhdr->ifdMax;
3027 /* This is the actual link routine. It makes two passes over all the
3031 ecoff_bfd_seclet_link (abfd, data, relocateable)
3034 boolean relocateable;
3036 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3039 register asection *o;
3040 register bfd_seclet_type *p;
3041 asymbol **sym_ptr_ptr;
3042 bfd_size_type debug_align;
3046 /* We accumulate the debugging information counts in the symbolic
3048 symhdr = &ecoff_data (abfd)->symbolic_header;
3049 symhdr->magic = backend->sym_magic;
3050 /* FIXME: What should the version stamp be? */
3052 symhdr->ilineMax = 0;
3056 symhdr->isymMax = 0;
3057 symhdr->ioptMax = 0;
3058 symhdr->iauxMax = 0;
3060 symhdr->issExtMax = 0;
3063 symhdr->iextMax = 0;
3065 /* We need to copy over the debugging symbols from each input BFD.
3066 When we do this copying, we have to adjust the text address in
3067 the FDR structures, so we have to know the text address used for
3068 the input BFD. Since we only want to copy the symbols once per
3069 input BFD, but we are going to look at each input BFD multiple
3070 times (once for each section it provides), we arrange to always
3071 look at the text section first. That means that when we copy the
3072 debugging information, we always know the text address. So we
3073 actually do each pass in two sub passes; first the text sections,
3074 then the non-text sections. We use the output_has_begun flag to
3075 determine whether we have copied over the debugging information
3078 /* Do the first pass: set the output section contents and count the
3079 debugging information. */
3080 ecoff_clear_output_flags (abfd);
3081 for (ipass = 0; ipass < 2; ipass++)
3083 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3085 /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
3086 so they are done on pass 0. For other sections the
3087 expression is true, so they are done on pass 1. */
3088 if (((o->flags & SEC_CODE) == 0) != ipass)
3091 for (p = o->seclets_head;
3092 p != (bfd_seclet_type *) NULL;
3095 if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
3102 /* We handle the external symbols differently. We use the ones
3103 attached to the output_bfd. The linker will have already
3104 determined which symbols are to be attached. Here we just
3105 determine how much space we will need for them. */
3106 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3107 if (sym_ptr_ptr != NULL)
3111 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
3112 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
3114 if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
3115 && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
3118 symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
3123 /* Adjust the counts so that structures are longword aligned. */
3124 debug_align = backend->debug_align;
3126 symhdr->cbLine = (symhdr->cbLine + debug_align) &~ debug_align;
3127 symhdr->issMax = (symhdr->issMax + debug_align) &~ debug_align;
3128 symhdr->issExtMax = (symhdr->issExtMax + debug_align) &~ debug_align;
3130 /* Now the counts in symhdr are the correct size for the debugging
3131 information. We allocate the right amount of space, and reset
3132 the counts so that the second pass can use them as indices. It
3133 would be possible to output the debugging information directly to
3134 the file in pass 2, rather than to build it in memory and then
3135 write it out. Outputting to the file would require a lot of
3136 seeks and small writes, though, and I think this approach is
3138 size = (symhdr->cbLine * sizeof (unsigned char)
3139 + symhdr->idnMax * backend->external_dnr_size
3140 + symhdr->ipdMax * backend->external_pdr_size
3141 + symhdr->isymMax * backend->external_sym_size
3142 + symhdr->ioptMax * backend->external_opt_size
3143 + symhdr->iauxMax * sizeof (union aux_ext)
3144 + symhdr->issMax * sizeof (char)
3145 + symhdr->issExtMax * sizeof (char)
3146 + symhdr->ifdMax * backend->external_fdr_size
3147 + symhdr->crfd * backend->external_rfd_size
3148 + symhdr->iextMax * backend->external_ext_size);
3149 raw = (char *) bfd_alloc (abfd, size);
3150 if (raw == (char *) NULL)
3152 bfd_error = no_memory;
3155 ecoff_data (abfd)->raw_size = size;
3156 ecoff_data (abfd)->raw_syments = (PTR) raw;
3158 /* Initialize the raw pointers. */
3159 #define SET(field, count, type, size) \
3160 ecoff_data (abfd)->field = (type) raw; \
3161 raw += symhdr->count * size
3163 SET (line, cbLine, unsigned char *, sizeof (unsigned char));
3164 SET (external_dnr, idnMax, PTR, backend->external_dnr_size);
3165 SET (external_pdr, ipdMax, PTR, backend->external_pdr_size);
3166 SET (external_sym, isymMax, PTR, backend->external_sym_size);
3167 SET (external_opt, ioptMax, PTR, backend->external_opt_size);
3168 SET (external_aux, iauxMax, union aux_ext *, sizeof (union aux_ext));
3169 SET (ss, issMax, char *, sizeof (char));
3170 SET (ssext, issExtMax, char *, sizeof (char));
3171 SET (external_fdr, ifdMax, PTR, backend->external_fdr_size);
3172 SET (external_rfd, crfd, PTR, backend->external_rfd_size);
3173 SET (external_ext, iextMax, PTR, backend->external_ext_size);
3176 /* Reset the counts so the second pass can use them to know how far
3178 symhdr->ilineMax = 0;
3182 symhdr->isymMax = 0;
3183 symhdr->ioptMax = 0;
3184 symhdr->iauxMax = 0;
3186 symhdr->issExtMax = 0;
3189 symhdr->iextMax = 0;
3191 /* Do the second pass: accumulate the debugging information. */
3192 ecoff_clear_output_flags (abfd);
3193 for (ipass = 0; ipass < 2; ipass++)
3195 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3197 if (((o->flags & SEC_CODE) == 0) != ipass)
3199 for (p = o->seclets_head;
3200 p != (bfd_seclet_type *) NULL;
3203 if (p->type == bfd_indirect_seclet)
3205 if (ecoff_get_debug (abfd, p, o, relocateable) == false)
3212 /* Put in the external symbols. */
3213 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3214 if (sym_ptr_ptr != NULL)
3216 const bfd_size_type external_ext_size = backend->external_ext_size;
3217 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3218 = backend->swap_ext_in;
3219 void (* const swap_ext_out) PARAMS ((bfd *, const EXTR *, PTR))
3220 = backend->swap_ext_out;
3224 ssext = ecoff_data (abfd)->ssext;
3225 external_ext = (char *) ecoff_data (abfd)->external_ext;
3226 for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
3231 sym_ptr = *sym_ptr_ptr;
3233 if ((sym_ptr->flags & BSF_DEBUGGING) != 0
3234 || (sym_ptr->flags & BSF_LOCAL) != 0)
3237 /* The native pointer can be NULL for a symbol created by
3238 the linker via ecoff_make_empty_symbol. */
3239 if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
3240 || ecoffsymbol (sym_ptr)->native == NULL)
3243 esym.cobol_main = 0;
3247 /* FIXME: we can do better than this for st and sc. */
3248 esym.asym.st = stGlobal;
3249 esym.asym.sc = scAbs;
3250 esym.asym.reserved = 0;
3251 esym.asym.index = indexNil;
3255 ecoff_symbol_type *ecoff_sym_ptr;
3257 ecoff_sym_ptr = ecoffsymbol (sym_ptr);
3258 if (ecoff_sym_ptr->local)
3260 (*swap_ext_in) (abfd, ecoff_sym_ptr->native, &esym);
3262 /* If we're producing an executable, move common symbols
3264 if (relocateable == false)
3266 if (esym.asym.sc == scCommon)
3267 esym.asym.sc = scBss;
3268 else if (esym.asym.sc == scSCommon)
3269 esym.asym.sc = scSBss;
3272 /* Adjust the FDR index for the symbol by that used for
3274 esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
3277 esym.asym.iss = symhdr->issExtMax;
3279 if (bfd_is_com_section (sym_ptr->section)
3280 || sym_ptr->section == &bfd_und_section)
3281 esym.asym.value = sym_ptr->value;
3283 esym.asym.value = (sym_ptr->value
3284 + sym_ptr->section->output_offset
3285 + sym_ptr->section->output_section->vma);
3287 (*swap_ext_out) (abfd, &esym, external_ext);
3289 ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
3291 external_ext += external_ext_size;
3294 strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
3295 symhdr->issExtMax += strlen (sym_ptr->name) + 1;
3299 /* Adjust the counts so that structures are longword aligned. */
3300 symhdr->cbLine = (symhdr->cbLine + debug_align) &~ debug_align;
3301 symhdr->issMax = (symhdr->issMax + debug_align) &~ debug_align;
3302 symhdr->issExtMax = (symhdr->issExtMax + debug_align) &~ debug_align;
3307 /* Set the architecture. The supported architecture is stored in the
3308 backend pointer. We always set the architecture anyhow, since many
3309 callers ignore the return value. */
3312 ecoff_set_arch_mach (abfd, arch, machine)
3314 enum bfd_architecture arch;
3315 unsigned long machine;
3317 bfd_default_set_arch_mach (abfd, arch, machine);
3318 return arch == ecoff_backend (abfd)->arch;
3321 /* Get the size of the section headers. We do not output the .scommon
3322 section which we created in ecoff_mkobject. */
3325 ecoff_sizeof_headers (abfd, reloc)
3329 return (bfd_coff_filhsz (abfd)
3330 + bfd_coff_aoutsz (abfd)
3331 + (abfd->section_count - 1) * bfd_coff_scnhsz (abfd));
3334 /* Calculate the file position for each section, and set
3338 ecoff_compute_section_file_positions (abfd)
3346 if (bfd_get_start_address (abfd))
3347 abfd->flags |= EXEC_P;
3349 sofar = ecoff_sizeof_headers (abfd, false);
3352 for (current = abfd->sections;
3353 current != (asection *) NULL;
3354 current = current->next)
3356 /* Only deal with sections which have contents */
3357 if (! (current->flags & SEC_HAS_CONTENTS)
3358 || strcmp (current->name, SCOMMON) == 0)
3361 /* On Ultrix, the data sections in an executable file must be
3362 aligned to a page boundary within the file. This does not
3363 affect the section size, though. FIXME: Does this work for
3365 if ((abfd->flags & EXEC_P) != 0
3366 && (abfd->flags & D_PAGED) != 0
3367 && first_data != false
3368 && (current->flags & SEC_CODE) == 0)
3370 const bfd_vma round = ecoff_backend (abfd)->round;
3372 sofar = (sofar + round - 1) &~ (round - 1);
3376 /* Align the sections in the file to the same boundary on
3377 which they are aligned in virtual memory. */
3379 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3381 current->filepos = sofar;
3383 sofar += current->_raw_size;
3385 /* make sure that this section is of the right size too */
3387 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3388 current->_raw_size += sofar - old_sofar;
3391 ecoff_data (abfd)->reloc_filepos = sofar;
3394 /* Set the contents of a section. */
3397 ecoff_set_section_contents (abfd, section, location, offset, count)
3402 bfd_size_type count;
3404 if (abfd->output_has_begun == false)
3405 ecoff_compute_section_file_positions (abfd);
3407 bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
3410 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
3415 /* Write out an ECOFF file. */
3418 ecoff_write_object_contents (abfd)
3421 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3422 const bfd_vma round = backend->round;
3423 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
3424 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
3425 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
3426 const bfd_size_type external_hdr_size = backend->external_hdr_size;
3427 const bfd_size_type external_reloc_size = backend->external_reloc_size;
3428 void (* const swap_reloc_out) PARAMS ((bfd *,
3429 const struct internal_reloc *,
3431 = backend->swap_reloc_out;
3435 file_ptr reloc_base;
3437 unsigned long reloc_size;
3438 unsigned long text_size;
3439 unsigned long text_start;
3440 unsigned long data_size;
3441 unsigned long data_start;
3442 unsigned long bss_size;
3444 struct internal_filehdr internal_f;
3445 struct internal_aouthdr internal_a;
3448 bfd_error = system_call_error;
3450 if(abfd->output_has_begun == false)
3451 ecoff_compute_section_file_positions(abfd);
3453 if (abfd->sections != (asection *) NULL)
3454 scn_base = abfd->sections->filepos;
3457 reloc_base = ecoff_data (abfd)->reloc_filepos;
3461 for (current = abfd->sections;
3462 current != (asection *)NULL;
3463 current = current->next)
3465 if (strcmp (current->name, SCOMMON) == 0)
3467 current->target_index = count;
3469 if (current->reloc_count != 0)
3471 bfd_size_type relsize;
3473 current->rel_filepos = reloc_base;
3474 relsize = current->reloc_count * external_reloc_size;
3475 reloc_size += relsize;
3476 reloc_base += relsize;
3479 current->rel_filepos = 0;
3482 sym_base = reloc_base + reloc_size;
3484 /* At least on Ultrix, the symbol table of an executable file must
3485 be aligned to a page boundary. FIXME: Is this true on other
3487 if ((abfd->flags & EXEC_P) != 0
3488 && (abfd->flags & D_PAGED) != 0)
3489 sym_base = (sym_base + round - 1) &~ (round - 1);
3491 ecoff_data (abfd)->sym_filepos = sym_base;
3493 if ((abfd->flags & D_PAGED) != 0)
3494 text_size = ecoff_sizeof_headers (abfd, false);
3502 /* Write section headers to the file. */
3504 buff = (PTR) alloca (scnhsz);
3505 internal_f.f_nscns = 0;
3506 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
3508 for (current = abfd->sections;
3509 current != (asection *) NULL;
3510 current = current->next)
3512 struct internal_scnhdr section;
3515 if (strcmp (current->name, SCOMMON) == 0)
3517 BFD_ASSERT (bfd_get_section_size_before_reloc (current) == 0
3518 && current->reloc_count == 0);
3522 ++internal_f.f_nscns;
3524 strncpy (section.s_name, current->name, sizeof section.s_name);
3526 /* FIXME: is this correct for shared libraries? I think it is
3527 but I have no platform to check. Ian Lance Taylor. */
3528 vma = bfd_get_section_vma (abfd, current);
3529 if (strcmp (current->name, _LIB) == 0)
3530 section.s_vaddr = 0;
3532 section.s_vaddr = vma;
3534 section.s_paddr = vma;
3535 section.s_size = bfd_get_section_size_before_reloc (current);
3537 /* If this section has no size or is unloadable then the scnptr
3539 if (current->_raw_size == 0
3540 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3541 section.s_scnptr = 0;
3543 section.s_scnptr = current->filepos;
3544 section.s_relptr = current->rel_filepos;
3546 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
3547 object file produced by the assembler is supposed to point to
3548 information about how much room is required by objects of
3549 various different sizes. I think this only matters if we
3550 want the linker to compute the best size to use, or
3551 something. I don't know what happens if the information is
3553 section.s_lnnoptr = 0;
3555 section.s_nreloc = current->reloc_count;
3556 section.s_nlnno = 0;
3557 section.s_flags = ecoff_sec_to_styp_flags (current->name,
3560 bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff);
3561 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
3564 if ((section.s_flags & STYP_TEXT) != 0)
3566 text_size += bfd_get_section_size_before_reloc (current);
3567 if (text_start == 0 || text_start > vma)
3570 else if ((section.s_flags & STYP_RDATA) != 0
3571 || (section.s_flags & STYP_DATA) != 0
3572 || (section.s_flags & STYP_LIT8) != 0
3573 || (section.s_flags & STYP_LIT4) != 0
3574 || (section.s_flags & STYP_SDATA) != 0)
3576 data_size += bfd_get_section_size_before_reloc (current);
3577 if (data_start == 0 || data_start > vma)
3580 else if ((section.s_flags & STYP_BSS) != 0
3581 || (section.s_flags & STYP_SBSS) != 0)
3582 bss_size += bfd_get_section_size_before_reloc (current);
3585 /* Set up the file header. */
3587 if (abfd->xvec->header_byteorder_big_p != false)
3588 internal_f.f_magic = backend->big_magic;
3590 internal_f.f_magic = backend->little_magic;
3592 /* We will NOT put a fucking timestamp in the header here. Every
3593 time you put it back, I will come in and take it out again. I'm
3594 sorry. This field does not belong here. We fill it with a 0 so
3595 it compares the same but is not a reasonable time. --
3597 internal_f.f_timdat = 0;
3599 if (bfd_get_symcount (abfd) != 0)
3601 /* The ECOFF f_nsyms field is not actually the number of
3602 symbols, it's the size of symbolic information header. */
3603 internal_f.f_nsyms = external_hdr_size;
3604 internal_f.f_symptr = sym_base;
3608 internal_f.f_nsyms = 0;
3609 internal_f.f_symptr = 0;
3612 internal_f.f_opthdr = aoutsz;
3614 internal_f.f_flags = F_LNNO;
3615 if (reloc_size == 0)
3616 internal_f.f_flags |= F_RELFLG;
3617 if (bfd_get_symcount (abfd) == 0)
3618 internal_f.f_flags |= F_LSYMS;
3619 if (abfd->flags & EXEC_P)
3620 internal_f.f_flags |= F_EXEC;
3622 if (! abfd->xvec->byteorder_big_p)
3623 internal_f.f_flags |= F_AR32WR;
3625 internal_f.f_flags |= F_AR32W;
3627 /* Set up the ``optional'' header. */
3628 if ((abfd->flags & D_PAGED) != 0)
3629 internal_a.magic = ECOFF_AOUT_ZMAGIC;
3631 internal_a.magic = ECOFF_AOUT_OMAGIC;
3633 /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
3634 linker happy. But, is it right? */
3635 internal_a.vstamp = 0x20a;
3637 /* At least on Ultrix, these have to be rounded to page boundaries.
3638 FIXME: Is this true on other platforms? */
3639 if ((abfd->flags & D_PAGED) != 0)
3641 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
3642 internal_a.text_start = text_start &~ (round - 1);
3643 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
3644 internal_a.data_start = data_start &~ (round - 1);
3648 internal_a.tsize = text_size;
3649 internal_a.text_start = text_start;
3650 internal_a.dsize = data_size;
3651 internal_a.data_start = data_start;
3654 /* On Ultrix, the initial portions of the .sbss and .bss segments
3655 are at the end of the data section. The bsize field in the
3656 optional header records how many bss bytes are required beyond
3657 those in the data section. The value is not rounded to a page
3659 if (bss_size < internal_a.dsize - data_size)
3662 bss_size -= internal_a.dsize - data_size;
3663 internal_a.bsize = bss_size;
3664 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
3666 internal_a.entry = bfd_get_start_address (abfd);
3668 internal_a.gp_value = ecoff_data (abfd)->gp;
3670 internal_a.gprmask = ecoff_data (abfd)->gprmask;
3671 internal_a.fprmask = ecoff_data (abfd)->fprmask;
3672 for (i = 0; i < 4; i++)
3673 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
3675 /* Write out the file header and the optional header. */
3677 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3680 buff = (PTR) alloca (filhsz);
3681 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
3682 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
3685 buff = (PTR) alloca (aoutsz);
3686 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
3687 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
3690 /* Write out the relocs. */
3691 for (current = abfd->sections;
3692 current != (asection *) NULL;
3693 current = current->next)
3695 arelent **reloc_ptr_ptr;
3696 arelent **reloc_end;
3699 if (current->reloc_count == 0)
3702 buff = bfd_alloc (abfd, current->reloc_count * external_reloc_size);
3705 bfd_error = no_memory;
3709 reloc_ptr_ptr = current->orelocation;
3710 reloc_end = reloc_ptr_ptr + current->reloc_count;
3711 out_ptr = (char *) buff;
3713 reloc_ptr_ptr < reloc_end;
3714 reloc_ptr_ptr++, out_ptr += external_reloc_size)
3718 struct internal_reloc in;
3720 memset (&in, 0, sizeof in);
3722 reloc = *reloc_ptr_ptr;
3723 sym = *reloc->sym_ptr_ptr;
3725 /* This must be an ECOFF reloc. */
3726 BFD_ASSERT (reloc->howto != (reloc_howto_type *) NULL
3727 && reloc->howto >= ecoff_howto_table
3729 < (ecoff_howto_table + ECOFF_HOWTO_COUNT)));
3731 in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
3732 in.r_type = reloc->howto->type;
3734 /* If this is a REFHI reloc, the next one must be a REFLO
3735 reloc for the same symbol. */
3736 BFD_ASSERT (in.r_type != ECOFF_R_REFHI
3737 || (reloc_ptr_ptr < reloc_end
3738 && (reloc_ptr_ptr[1]->howto
3739 != (reloc_howto_type *) NULL)
3740 && (reloc_ptr_ptr[1]->howto->type
3742 && (sym == *reloc_ptr_ptr[1]->sym_ptr_ptr)));
3744 if ((sym->flags & BSF_SECTION_SYM) == 0)
3746 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
3753 name = bfd_get_section_name (abfd, bfd_get_section (sym));
3754 if (strcmp (name, ".text") == 0)
3755 in.r_symndx = RELOC_SECTION_TEXT;
3756 else if (strcmp (name, ".rdata") == 0)
3757 in.r_symndx = RELOC_SECTION_RDATA;
3758 else if (strcmp (name, ".data") == 0)
3759 in.r_symndx = RELOC_SECTION_DATA;
3760 else if (strcmp (name, ".sdata") == 0)
3761 in.r_symndx = RELOC_SECTION_SDATA;
3762 else if (strcmp (name, ".sbss") == 0)
3763 in.r_symndx = RELOC_SECTION_SBSS;
3764 else if (strcmp (name, ".bss") == 0)
3765 in.r_symndx = RELOC_SECTION_BSS;
3766 else if (strcmp (name, ".init") == 0)
3767 in.r_symndx = RELOC_SECTION_INIT;
3768 else if (strcmp (name, ".lit8") == 0)
3769 in.r_symndx = RELOC_SECTION_LIT8;
3770 else if (strcmp (name, ".lit4") == 0)
3771 in.r_symndx = RELOC_SECTION_LIT4;
3777 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
3780 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
3782 if (bfd_write (buff, external_reloc_size, current->reloc_count, abfd)
3783 != external_reloc_size * current->reloc_count)
3785 bfd_release (abfd, buff);
3788 /* Write out the symbolic debugging information. */
3789 if (bfd_get_symcount (abfd) > 0)
3792 unsigned long sym_offset;
3794 /* Set up the offsets in the symbolic header. */
3795 symhdr = &ecoff_data (abfd)->symbolic_header;
3796 sym_offset = ecoff_data (abfd)->sym_filepos + external_hdr_size;
3798 #define SET(offset, size, ptr) \
3799 if (symhdr->size == 0) \
3800 symhdr->offset = 0; \
3802 symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
3803 - (char *) ecoff_data (abfd)->raw_syments) \
3806 SET (cbLineOffset, cbLine, line);
3807 SET (cbDnOffset, idnMax, external_dnr);
3808 SET (cbPdOffset, ipdMax, external_pdr);
3809 SET (cbSymOffset, isymMax, external_sym);
3810 SET (cbOptOffset, ioptMax, external_opt);
3811 SET (cbAuxOffset, iauxMax, external_aux);
3812 SET (cbSsOffset, issMax, ss);
3813 SET (cbSsExtOffset, issExtMax, ssext);
3814 SET (cbFdOffset, ifdMax, external_fdr);
3815 SET (cbRfdOffset, crfd, external_rfd);
3816 SET (cbExtOffset, iextMax, external_ext);
3819 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
3822 buff = (PTR) alloca (external_hdr_size);
3823 (*backend->swap_hdr_out) (abfd, &ecoff_data (abfd)->symbolic_header,
3825 if (bfd_write (buff, 1, external_hdr_size, abfd) != external_hdr_size)
3827 if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
3828 ecoff_data (abfd)->raw_size, abfd)
3829 != ecoff_data (abfd)->raw_size)
3832 else if ((abfd->flags & EXEC_P) != 0
3833 && (abfd->flags & D_PAGED) != 0)
3837 /* A demand paged executable must occupy an even number of
3839 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3842 if (bfd_read (&c, 1, 1, abfd) == 0)
3844 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3847 if (bfd_write (&c, 1, 1, abfd) != 1)
3854 /* Archive handling. ECOFF uses what appears to be a unique type of
3855 archive header (which I call an armap). The byte ordering of the
3856 armap and the contents are encoded in the name of the armap itself.
3857 At least for now, we only support archives with the same byte
3858 ordering in the armap and the contents.
3860 The first four bytes in the armap are the number of symbol
3861 definitions. This is always a power of two.
3863 This is followed by the symbol definitions. Each symbol definition
3864 occupies 8 bytes. The first four bytes are the offset from the
3865 start of the armap strings to the null-terminated string naming
3866 this symbol. The second four bytes are the file offset to the
3867 archive member which defines this symbol. If the second four bytes
3868 are 0, then this is not actually a symbol definition, and it should
3871 The symbols are hashed into the armap with a closed hashing scheme.
3872 See the functions below for the details of the algorithm.
3874 We could use the hash table when looking up symbols in a library.
3875 This would require a new BFD target entry point to replace the
3876 bfd_get_next_mapent function used by the linker.
3878 After the symbol definitions comes four bytes holding the size of
3879 the string table, followed by the string table itself. */
3881 /* The name of an archive headers looks like this:
3882 __________E[BL]E[BL]_ (with a trailing space).
3883 The trailing space is changed to an X if the archive is changed to
3884 indicate that the armap is out of date.
3886 The Alpha seems to use ________64E[BL]E[BL]_. */
3888 #define ARMAP_BIG_ENDIAN 'B'
3889 #define ARMAP_LITTLE_ENDIAN 'L'
3890 #define ARMAP_MARKER 'E'
3891 #define ARMAP_START_LENGTH 10
3892 #define ARMAP_HEADER_MARKER_INDEX 10
3893 #define ARMAP_HEADER_ENDIAN_INDEX 11
3894 #define ARMAP_OBJECT_MARKER_INDEX 12
3895 #define ARMAP_OBJECT_ENDIAN_INDEX 13
3896 #define ARMAP_END_INDEX 14
3897 #define ARMAP_END "_ "
3899 /* This is a magic number used in the hashing algorithm. */
3900 #define ARMAP_HASH_MAGIC 0x9dd68ab5
3902 /* This returns the hash value to use for a string. It also sets
3903 *REHASH to the rehash adjustment if the first slot is taken. SIZE
3904 is the number of entries in the hash table, and HLOG is the log
3908 ecoff_armap_hash (s, rehash, size, hlog)
3910 unsigned int *rehash;
3918 hash = ((hash >> 27) | (hash << 5)) + *s++;
3919 hash *= ARMAP_HASH_MAGIC;
3920 *rehash = (hash & (size - 1)) | 1;
3921 return hash >> (32 - hlog);
3924 /* Read in the armap. */
3927 ecoff_slurp_armap (abfd)
3932 struct areltdata *mapdata;
3933 bfd_size_type parsed_size;
3935 struct artdata *ardata;
3938 struct symdef *symdef_ptr;
3941 /* Get the name of the first element. */
3942 i = bfd_read ((PTR) nextname, 1, 16, abfd);
3948 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
3950 /* See if the first element is an armap. */
3951 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
3952 ARMAP_START_LENGTH) != 0
3953 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3954 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3955 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3956 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3957 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3958 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3959 || strncmp (nextname + ARMAP_END_INDEX,
3960 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3962 bfd_has_map (abfd) = false;
3966 /* Make sure we have the right byte ordering. */
3967 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3968 ^ (abfd->xvec->header_byteorder_big_p != false))
3969 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3970 ^ (abfd->xvec->byteorder_big_p != false)))
3972 bfd_error = wrong_format;
3976 /* Read in the armap. */
3977 ardata = bfd_ardata (abfd);
3978 mapdata = snarf_ar_hdr (abfd);
3979 if (mapdata == (struct areltdata *) NULL)
3981 parsed_size = mapdata->parsed_size;
3982 bfd_release (abfd, (PTR) mapdata);
3984 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3985 if (raw_armap == (char *) NULL)
3987 bfd_error = no_memory;
3991 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3993 bfd_error = malformed_archive;
3994 bfd_release (abfd, (PTR) raw_armap);
3998 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
4000 ardata->symdef_count = 0;
4001 ardata->cache = (struct ar_cache *) NULL;
4003 /* This code used to overlay the symdefs over the raw archive data,
4004 but that doesn't work on a 64 bit host. */
4006 stringbase = raw_ptr + count * (2 * LONG_SIZE) + LONG_SIZE;
4008 #ifdef CHECK_ARMAP_HASH
4012 /* Double check that I have the hashing algorithm right by making
4013 sure that every symbol can be looked up successfully. */
4015 for (i = 1; i < count; i <<= 1)
4017 BFD_ASSERT (i == count);
4019 raw_ptr = raw_armap + LONG_SIZE;
4020 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
4022 unsigned int name_offset, file_offset;
4023 unsigned int hash, rehash, srch;
4025 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
4026 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
4027 if (file_offset == 0)
4029 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
4034 /* See if we can rehash to this location. */
4035 for (srch = (hash + rehash) & (count - 1);
4036 srch != hash && srch != i;
4037 srch = (srch + rehash) & (count - 1))
4038 BFD_ASSERT (bfd_h_get_32 (abfd,
4041 + (srch * 2 * LONG_SIZE)
4044 BFD_ASSERT (srch == i);
4048 #endif /* CHECK_ARMAP_HASH */
4050 raw_ptr = raw_armap + LONG_SIZE;
4051 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
4052 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE)) != 0)
4053 ++ardata->symdef_count;
4055 symdef_ptr = ((struct symdef *)
4057 ardata->symdef_count * sizeof (struct symdef)));
4058 ardata->symdefs = (carsym *) symdef_ptr;
4060 raw_ptr = raw_armap + LONG_SIZE;
4061 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
4063 unsigned int name_offset, file_offset;
4065 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
4066 if (file_offset == 0)
4068 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
4069 symdef_ptr->s.name = stringbase + name_offset;
4070 symdef_ptr->file_offset = file_offset;
4074 ardata->first_file_filepos = bfd_tell (abfd);
4075 /* Pad to an even boundary. */
4076 ardata->first_file_filepos += ardata->first_file_filepos % 2;
4078 bfd_has_map (abfd) = true;
4083 /* Write out an armap. */
4086 ecoff_write_armap (abfd, elength, map, orl_count, stridx)
4088 unsigned int elength;
4090 unsigned int orl_count;
4093 unsigned int hashsize, hashlog;
4094 unsigned int symdefsize;
4096 unsigned int stringsize;
4097 unsigned int mapsize;
4100 struct stat statbuf;
4102 bfd_byte temp[LONG_SIZE];
4103 bfd_byte *hashtable;
4107 /* Ultrix appears to use as a hash table size the least power of two
4108 greater than twice the number of entries. */
4109 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
4111 hashsize = 1 << hashlog;
4113 symdefsize = hashsize * 2 * LONG_SIZE;
4115 stringsize = stridx + padit;
4117 /* Include 8 bytes to store symdefsize and stringsize in output. */
4118 mapsize = LONG_SIZE + symdefsize + stringsize + LONG_SIZE;
4120 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
4122 memset ((PTR) &hdr, 0, sizeof hdr);
4124 /* Work out the ECOFF armap name. */
4125 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
4126 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
4127 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
4128 (abfd->xvec->header_byteorder_big_p
4130 : ARMAP_LITTLE_ENDIAN);
4131 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
4132 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
4133 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
4134 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
4136 /* Write the timestamp of the archive header to be just a little bit
4137 later than the timestamp of the file, otherwise the linker will
4138 complain that the index is out of date. Actually, the Ultrix
4139 linker just checks the archive name; the GNU linker may check the
4141 stat (abfd->filename, &statbuf);
4142 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
4144 /* The DECstation uses zeroes for the uid, gid and mode of the
4146 hdr.ar_uid[0] = '0';
4147 hdr.ar_gid[0] = '0';
4148 hdr.ar_mode[0] = '0';
4150 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
4152 hdr.ar_fmag[0] = '`';
4153 hdr.ar_fmag[1] = '\n';
4155 /* Turn all null bytes in the header into spaces. */
4156 for (i = 0; i < sizeof (struct ar_hdr); i++)
4157 if (((char *)(&hdr))[i] == '\0')
4158 (((char *)(&hdr))[i]) = ' ';
4160 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
4161 != sizeof (struct ar_hdr))
4164 bfd_h_put_32 (abfd, hashsize, temp);
4165 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4168 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
4170 current = abfd->archive_head;
4172 for (i = 0; i < orl_count; i++)
4174 unsigned int hash, rehash;
4176 /* Advance firstreal to the file position of this archive
4178 if (((bfd *) map[i].pos) != last_elt)
4182 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
4183 firstreal += firstreal % 2;
4184 current = current->next;
4186 while (current != (bfd *) map[i].pos);
4191 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
4192 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4193 + (hash * 2 * LONG_SIZE)
4199 /* The desired slot is already taken. */
4200 for (srch = (hash + rehash) & (hashsize - 1);
4202 srch = (srch + rehash) & (hashsize - 1))
4203 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4204 + (srch * 2 * LONG_SIZE)
4209 BFD_ASSERT (srch != hash);
4214 bfd_h_put_32 (abfd, map[i].namidx,
4215 (PTR) (hashtable + hash * 2 * LONG_SIZE));
4216 bfd_h_put_32 (abfd, firstreal,
4217 (PTR) (hashtable + hash * 2 * LONG_SIZE + LONG_SIZE));
4220 if (bfd_write (hashtable, 1, symdefsize, abfd) != symdefsize)
4223 bfd_release (abfd, hashtable);
4225 /* Now write the strings. */
4226 bfd_h_put_32 (abfd, stringsize, temp);
4227 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4229 for (i = 0; i < orl_count; i++)
4233 len = strlen (*map[i].name) + 1;
4234 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
4238 /* The spec sez this should be a newline. But in order to be
4239 bug-compatible for DECstation ar we use a null. */
4242 if (bfd_write ("\0", 1, 1, abfd) != 1)
4249 /* See whether this BFD is an archive. If it is, read in the armap
4250 and the extended name table. */
4253 ecoff_archive_p (abfd)
4256 char armag[SARMAG + 1];
4258 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
4259 || strncmp (armag, ARMAG, SARMAG) != 0)
4261 bfd_error = wrong_format;
4262 return (bfd_target *) NULL;
4265 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
4266 involves a cast, we can't do it as the left operand of
4268 abfd->tdata.aout_ar_data =
4269 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
4271 if (bfd_ardata (abfd) == (struct artdata *) NULL)
4273 bfd_error = no_memory;
4274 return (bfd_target *) NULL;
4277 bfd_ardata (abfd)->first_file_filepos = SARMAG;
4279 if (ecoff_slurp_armap (abfd) == false
4280 || ecoff_slurp_extended_name_table (abfd) == false)
4282 bfd_release (abfd, bfd_ardata (abfd));
4283 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
4284 return (bfd_target *) NULL;