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 int ecoff_get_magic PARAMS ((bfd *abfd));
48 static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *abfd));
49 static boolean ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
50 asymbol *asym, int ext,
51 asymbol **indirect_ptr_ptr));
52 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
53 RNDXR *rndx, long isym,
55 static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
56 unsigned int indx, int bigendian));
57 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
59 static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
60 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
61 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
62 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
63 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
68 /* This stuff is somewhat copied from coffcode.h. */
70 static asection bfd_debug_section = { "*DEBUG*" };
72 /* Create an ECOFF object. */
78 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
79 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
80 if (abfd->tdata.ecoff_obj_data == NULL)
82 bfd_set_error (bfd_error_no_memory);
89 /* This is a hook called by coff_real_object_p to create any backend
90 specific information. */
93 ecoff_mkobject_hook (abfd, filehdr, aouthdr)
98 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
99 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
100 ecoff_data_type *ecoff;
103 if (ecoff_mkobject (abfd) == false)
106 ecoff = ecoff_data (abfd);
108 ecoff->sym_filepos = internal_f->f_symptr;
110 /* Create the .reginfo section to give programs outside BFD a way to
111 see the information stored in the a.out header. See the comment
113 regsec = bfd_make_section (abfd, REGINFO);
117 if (internal_a != (struct internal_aouthdr *) NULL)
121 ecoff->text_start = internal_a->text_start;
122 ecoff->text_end = internal_a->text_start + internal_a->tsize;
123 ecoff->gp = internal_a->gp_value;
124 ecoff->gprmask = internal_a->gprmask;
125 for (i = 0; i < 4; i++)
126 ecoff->cprmask[i] = internal_a->cprmask[i];
127 ecoff->fprmask = internal_a->fprmask;
128 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
129 abfd->flags |= D_PAGED;
132 /* It turns out that no special action is required by the MIPS or
133 Alpha ECOFF backends. They have different information in the
134 a.out header, but we just copy it all (e.g., gprmask, cprmask and
135 fprmask) and let the swapping routines ensure that only relevant
136 information is written out. */
141 /* This is a hook needed by SCO COFF, but we have nothing to do. */
145 ecoff_make_section_hook (abfd, name)
149 return (asection *) NULL;
152 /* Initialize a new section. */
155 ecoff_new_section_hook (abfd, section)
159 /* For the .pdata section, which has a special meaning on the Alpha,
160 we set the alignment to 8. We correct this later in
161 ecoff_compute_section_file_positions. We do this hackery because
162 we need to know the exact unaligned size of the .pdata section in
163 order to set the lnnoptr field correctly. */
164 if (strcmp (section->name, _PDATA) == 0)
165 section->alignment_power = 3;
167 section->alignment_power = abfd->xvec->align_power_min;
169 if (strcmp (section->name, _TEXT) == 0)
170 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
171 else if (strcmp (section->name, _DATA) == 0
172 || strcmp (section->name, _SDATA) == 0)
173 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
174 else if (strcmp (section->name, _RDATA) == 0
175 || strcmp (section->name, _LIT8) == 0
176 || strcmp (section->name, _LIT4) == 0)
177 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
178 else if (strcmp (section->name, _BSS) == 0
179 || strcmp (section->name, _SBSS) == 0)
180 section->flags |= SEC_ALLOC;
181 else if (strcmp (section->name, REGINFO) == 0)
183 /* Setting SEC_SHARED_LIBRARY should make the linker leave the
184 section completely alone. */
185 section->flags |= (SEC_SHARED_LIBRARY
188 section->_raw_size = sizeof (struct ecoff_reginfo);
191 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
192 uncertain about .init on some systems and I don't know how shared
198 /* Determine the machine architecture and type. This is called from
199 the generic COFF routines. It is the inverse of ecoff_get_magic,
200 below. This could be an ECOFF backend routine, with one version
201 for each target, but there aren't all that many ECOFF targets. */
204 ecoff_set_arch_mach_hook (abfd, filehdr)
208 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
209 enum bfd_architecture arch;
212 switch (internal_f->f_magic)
215 case MIPS_MAGIC_LITTLE:
217 arch = bfd_arch_mips;
221 case MIPS_MAGIC_LITTLE2:
222 case MIPS_MAGIC_BIG2:
223 /* MIPS ISA level 2: the r6000 */
224 arch = bfd_arch_mips;
228 case MIPS_MAGIC_LITTLE3:
229 case MIPS_MAGIC_BIG3:
230 /* MIPS ISA level 3: the r4000 */
231 arch = bfd_arch_mips;
236 arch = bfd_arch_alpha;
241 arch = bfd_arch_obscure;
246 return bfd_default_set_arch_mach (abfd, arch, mach);
249 /* Get the magic number to use based on the architecture and machine.
250 This is the inverse of ecoff_set_arch_mach_hook, above. */
253 ecoff_get_magic (abfd)
258 switch (bfd_get_arch (abfd))
261 switch (bfd_get_mach (abfd))
266 big = MIPS_MAGIC_BIG;
267 little = MIPS_MAGIC_LITTLE;
271 big = MIPS_MAGIC_BIG2;
272 little = MIPS_MAGIC_LITTLE2;
276 big = MIPS_MAGIC_BIG3;
277 little = MIPS_MAGIC_LITTLE3;
281 return abfd->xvec->byteorder_big_p ? big : little;
292 /* Get the section s_flags to use for a section. */
295 ecoff_sec_to_styp_flags (name, flags)
303 if (strcmp (name, _TEXT) == 0)
305 else if (strcmp (name, _DATA) == 0)
307 else if (strcmp (name, _SDATA) == 0)
309 else if (strcmp (name, _RDATA) == 0)
311 else if (strcmp (name, _LITA) == 0)
313 else if (strcmp (name, _LIT8) == 0)
315 else if (strcmp (name, _LIT4) == 0)
317 else if (strcmp (name, _BSS) == 0)
319 else if (strcmp (name, _SBSS) == 0)
321 else if (strcmp (name, _INIT) == 0)
322 styp = STYP_ECOFF_INIT;
323 else if (strcmp (name, _FINI) == 0)
324 styp = STYP_ECOFF_FINI;
325 else if (strcmp (name, _PDATA) == 0)
327 else if (strcmp (name, _XDATA) == 0)
329 else if (flags & SEC_CODE)
331 else if (flags & SEC_DATA)
333 else if (flags & SEC_READONLY)
335 else if (flags & SEC_LOAD)
340 if (flags & SEC_NEVER_LOAD)
346 /* Get the BFD flags to use for a section. */
350 ecoff_styp_to_sec_flags (abfd, hdr)
354 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
355 long styp_flags = internal_s->s_flags;
356 flagword sec_flags=0;
358 if (styp_flags & STYP_NOLOAD)
359 sec_flags |= SEC_NEVER_LOAD;
361 /* For 386 COFF, at least, an unloadable text or data section is
362 actually a shared library section. */
363 if ((styp_flags & STYP_TEXT)
364 || (styp_flags & STYP_ECOFF_INIT)
365 || (styp_flags & STYP_ECOFF_FINI))
367 if (sec_flags & SEC_NEVER_LOAD)
368 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
370 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
372 else if ((styp_flags & STYP_DATA)
373 || (styp_flags & STYP_RDATA)
374 || (styp_flags & STYP_SDATA)
375 || styp_flags == STYP_PDATA
376 || styp_flags == STYP_XDATA)
378 if (sec_flags & SEC_NEVER_LOAD)
379 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
381 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
382 if ((styp_flags & STYP_RDATA)
383 || styp_flags == STYP_PDATA)
384 sec_flags |= SEC_READONLY;
386 else if ((styp_flags & STYP_BSS)
387 || (styp_flags & STYP_SBSS))
389 sec_flags |= SEC_ALLOC;
391 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
393 sec_flags |= SEC_NEVER_LOAD;
395 else if ((styp_flags & STYP_LITA)
396 || (styp_flags & STYP_LIT8)
397 || (styp_flags & STYP_LIT4))
399 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
403 sec_flags |= SEC_ALLOC | SEC_LOAD;
409 /* Routines to swap auxiliary information in and out. I am assuming
410 that the auxiliary information format is always going to be target
413 /* Swap in a type information record.
414 BIGEND says whether AUX symbols are big-endian or little-endian; this
415 info comes from the file header record (fh-fBigendian). */
418 ecoff_swap_tir_in (bigend, ext_copy, intern)
420 struct tir_ext *ext_copy;
423 struct tir_ext ext[1];
425 *ext = *ext_copy; /* Make it reasonable to do in-place. */
427 /* now the fun stuff... */
429 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
430 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
431 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
432 >> TIR_BITS1_BT_SH_BIG;
433 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
434 >> TIR_BITS_TQ4_SH_BIG;
435 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
436 >> TIR_BITS_TQ5_SH_BIG;
437 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
438 >> TIR_BITS_TQ0_SH_BIG;
439 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
440 >> TIR_BITS_TQ1_SH_BIG;
441 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
442 >> TIR_BITS_TQ2_SH_BIG;
443 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
444 >> TIR_BITS_TQ3_SH_BIG;
446 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
447 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
448 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
449 >> TIR_BITS1_BT_SH_LITTLE;
450 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
451 >> TIR_BITS_TQ4_SH_LITTLE;
452 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
453 >> TIR_BITS_TQ5_SH_LITTLE;
454 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
455 >> TIR_BITS_TQ0_SH_LITTLE;
456 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
457 >> TIR_BITS_TQ1_SH_LITTLE;
458 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
459 >> TIR_BITS_TQ2_SH_LITTLE;
460 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
461 >> TIR_BITS_TQ3_SH_LITTLE;
465 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
470 /* Swap out a type information record.
471 BIGEND says whether AUX symbols are big-endian or little-endian; this
472 info comes from the file header record (fh-fBigendian). */
475 ecoff_swap_tir_out (bigend, intern_copy, ext)
482 *intern = *intern_copy; /* Make it reasonable to do in-place. */
484 /* now the fun stuff... */
486 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
487 | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
488 | ((intern->bt << TIR_BITS1_BT_SH_BIG)
489 & TIR_BITS1_BT_BIG));
490 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
492 | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
493 & TIR_BITS_TQ5_BIG));
494 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
496 | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
497 & TIR_BITS_TQ1_BIG));
498 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
500 | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
501 & TIR_BITS_TQ3_BIG));
503 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
504 | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
505 | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
506 & TIR_BITS1_BT_LITTLE));
507 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
508 & TIR_BITS_TQ4_LITTLE)
509 | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
510 & TIR_BITS_TQ5_LITTLE));
511 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
512 & TIR_BITS_TQ0_LITTLE)
513 | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
514 & TIR_BITS_TQ1_LITTLE));
515 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
516 & TIR_BITS_TQ2_LITTLE)
517 | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
518 & TIR_BITS_TQ3_LITTLE));
522 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
527 /* Swap in a relative symbol record. BIGEND says whether it is in
528 big-endian or little-endian format.*/
531 ecoff_swap_rndx_in (bigend, ext_copy, intern)
533 struct rndx_ext *ext_copy;
536 struct rndx_ext ext[1];
538 *ext = *ext_copy; /* Make it reasonable to do in-place. */
540 /* now the fun stuff... */
542 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
543 | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
544 >> RNDX_BITS1_RFD_SH_BIG);
545 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
546 << RNDX_BITS1_INDEX_SH_LEFT_BIG)
547 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
548 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
550 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
551 | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
552 << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
553 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
554 >> RNDX_BITS1_INDEX_SH_LITTLE)
555 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
556 | ((unsigned int) ext->r_bits[3]
557 << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
561 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
566 /* Swap out a relative symbol record. BIGEND says whether it is in
567 big-endian or little-endian format.*/
570 ecoff_swap_rndx_out (bigend, intern_copy, ext)
573 struct rndx_ext *ext;
577 *intern = *intern_copy; /* Make it reasonable to do in-place. */
579 /* now the fun stuff... */
581 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
582 ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
583 & RNDX_BITS1_RFD_BIG)
584 | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
585 & RNDX_BITS1_INDEX_BIG));
586 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
587 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
589 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
590 ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
591 & RNDX_BITS1_RFD_LITTLE)
592 | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
593 & RNDX_BITS1_INDEX_LITTLE));
594 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
595 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
599 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
604 /* Read in the symbolic header for an ECOFF object file. */
607 ecoff_slurp_symbolic_header (abfd)
610 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
611 bfd_size_type external_hdr_size;
613 HDRR *internal_symhdr;
615 /* See if we've already read it in. */
616 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
617 backend->debug_swap.sym_magic)
620 /* See whether there is a symbolic header. */
621 if (ecoff_data (abfd)->sym_filepos == 0)
623 bfd_get_symcount (abfd) = 0;
627 /* At this point bfd_get_symcount (abfd) holds the number of symbols
628 as read from the file header, but on ECOFF this is always the
629 size of the symbolic information header. It would be cleaner to
630 handle this when we first read the file in coffgen.c. */
631 external_hdr_size = backend->debug_swap.external_hdr_size;
632 if (bfd_get_symcount (abfd) != external_hdr_size)
634 bfd_set_error (bfd_error_bad_value);
638 /* Read the symbolic information header. */
639 raw = (PTR) malloc ((size_t) external_hdr_size);
642 bfd_set_error (bfd_error_no_memory);
646 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
647 || (bfd_read (raw, external_hdr_size, 1, abfd)
648 != external_hdr_size))
650 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
651 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
653 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
655 bfd_set_error (bfd_error_bad_value);
659 /* Now we can get the correct number of symbols. */
660 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
661 + internal_symhdr->iextMax);
672 /* Read in and swap the important symbolic information for an ECOFF
673 object file. This is called by gdb. */
676 ecoff_slurp_symbolic_info (abfd)
679 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
680 HDRR *internal_symhdr;
681 bfd_size_type raw_base;
682 bfd_size_type raw_size;
684 bfd_size_type external_fdr_size;
688 bfd_size_type raw_end;
689 bfd_size_type cb_end;
691 /* Check whether we've already gotten it, and whether there's any to
693 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
695 if (ecoff_data (abfd)->sym_filepos == 0)
697 bfd_get_symcount (abfd) = 0;
701 if (! ecoff_slurp_symbolic_header (abfd))
704 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
706 /* Read all the symbolic information at once. */
707 raw_base = (ecoff_data (abfd)->sym_filepos
708 + backend->debug_swap.external_hdr_size);
710 /* Alpha ecoff makes the determination of raw_size difficult. It has
711 an undocumented debug data section between the symhdr and the first
712 documented section. And the ordering of the sections varies between
713 statically and dynamically linked executables.
714 If bfd supports SEEK_END someday, this code could be simplified. */
718 #define UPDATE_RAW_END(start, count, size) \
719 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
720 if (cb_end > raw_end) \
723 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
724 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
725 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
726 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
727 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
728 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
729 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
730 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
731 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
732 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
733 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
735 #undef UPDATE_RAW_END
737 raw_size = raw_end - raw_base;
740 ecoff_data (abfd)->sym_filepos = 0;
743 raw = (PTR) bfd_alloc (abfd, raw_size);
746 bfd_set_error (bfd_error_no_memory);
750 (ecoff_data (abfd)->sym_filepos
751 + backend->debug_swap.external_hdr_size),
753 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
755 bfd_release (abfd, raw);
759 ecoff_data (abfd)->raw_syments = raw;
761 /* Get pointers for the numeric offsets in the HDRR structure. */
762 #define FIX(off1, off2, type) \
763 if (internal_symhdr->off1 == 0) \
764 ecoff_data (abfd)->debug_info.off2 = (type) NULL; \
766 ecoff_data (abfd)->debug_info.off2 = (type) ((char *) raw \
767 + internal_symhdr->off1 \
769 FIX (cbLineOffset, line, unsigned char *);
770 FIX (cbDnOffset, external_dnr, PTR);
771 FIX (cbPdOffset, external_pdr, PTR);
772 FIX (cbSymOffset, external_sym, PTR);
773 FIX (cbOptOffset, external_opt, PTR);
774 FIX (cbAuxOffset, external_aux, union aux_ext *);
775 FIX (cbSsOffset, ss, char *);
776 FIX (cbSsExtOffset, ssext, char *);
777 FIX (cbFdOffset, external_fdr, PTR);
778 FIX (cbRfdOffset, external_rfd, PTR);
779 FIX (cbExtOffset, external_ext, PTR);
782 /* I don't want to always swap all the data, because it will just
783 waste time and most programs will never look at it. The only
784 time the linker needs most of the debugging information swapped
785 is when linking big-endian and little-endian MIPS object files
786 together, which is not a common occurrence.
788 We need to look at the fdr to deal with a lot of information in
789 the symbols, so we swap them here. */
790 ecoff_data (abfd)->debug_info.fdr =
791 (struct fdr *) bfd_alloc (abfd,
792 (internal_symhdr->ifdMax *
793 sizeof (struct fdr)));
794 if (ecoff_data (abfd)->debug_info.fdr == NULL)
796 bfd_set_error (bfd_error_no_memory);
799 external_fdr_size = backend->debug_swap.external_fdr_size;
800 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
801 fraw_src = (char *) ecoff_data (abfd)->debug_info.external_fdr;
802 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
803 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
804 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
809 /* ECOFF symbol table routines. The ECOFF symbol table is described
810 in gcc/mips-tfile.c. */
812 /* ECOFF uses two common sections. One is the usual one, and the
813 other is for small objects. All the small objects are kept
814 together, and then referenced via the gp pointer, which yields
815 faster assembler code. This is what we use for the small common
817 static asection ecoff_scom_section;
818 static asymbol ecoff_scom_symbol;
819 static asymbol *ecoff_scom_symbol_ptr;
821 /* Create an empty symbol. */
824 ecoff_make_empty_symbol (abfd)
827 ecoff_symbol_type *new;
829 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
830 if (new == (ecoff_symbol_type *) NULL)
832 bfd_set_error (bfd_error_no_memory);
833 return (asymbol *) NULL;
835 memset ((PTR) new, 0, sizeof *new);
836 new->symbol.section = (asection *) NULL;
837 new->fdr = (FDR *) NULL;
840 new->symbol.the_bfd = abfd;
844 /* Set the BFD flags and section for an ECOFF symbol. */
847 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, indirect_ptr_ptr)
852 asymbol **indirect_ptr_ptr;
854 asym->the_bfd = abfd;
855 asym->value = ecoff_sym->value;
856 asym->section = &bfd_debug_section;
859 /* An indirect symbol requires two consecutive stabs symbols. */
860 if (*indirect_ptr_ptr != (asymbol *) NULL)
862 BFD_ASSERT (ECOFF_IS_STAB (ecoff_sym));
864 /* @@ Stuffing pointers into integers is a no-no.
865 We can usually get away with it if the integer is
866 large enough though. */
867 if (sizeof (asym) > sizeof (bfd_vma))
869 (*indirect_ptr_ptr)->value = (bfd_vma) asym;
871 asym->flags = BSF_DEBUGGING;
872 asym->section = &bfd_und_section;
873 *indirect_ptr_ptr = NULL;
877 if (ECOFF_IS_STAB (ecoff_sym)
878 && (ECOFF_UNMARK_STAB (ecoff_sym->index) | N_EXT) == (N_INDR | N_EXT))
880 asym->flags = BSF_DEBUGGING | BSF_INDIRECT;
881 asym->section = &bfd_ind_section;
882 /* Pass this symbol on to the next call to this function. */
883 *indirect_ptr_ptr = asym;
887 /* Most symbol types are just for debugging. */
888 switch (ecoff_sym->st)
897 if (ECOFF_IS_STAB (ecoff_sym))
899 asym->flags = BSF_DEBUGGING;
904 asym->flags = BSF_DEBUGGING;
909 asym->flags = BSF_EXPORT | BSF_GLOBAL;
911 asym->flags = BSF_LOCAL;
912 switch (ecoff_sym->sc)
915 /* Used for compiler generated labels. Leave them in the
916 debugging section, and mark them as local. If BSF_DEBUGGING
917 is set, then nm does not display them for some reason. If no
918 flags are set then the linker whines about them. */
919 asym->flags = BSF_LOCAL;
922 asym->section = bfd_make_section_old_way (abfd, ".text");
923 asym->value -= asym->section->vma;
926 asym->section = bfd_make_section_old_way (abfd, ".data");
927 asym->value -= asym->section->vma;
930 asym->section = bfd_make_section_old_way (abfd, ".bss");
931 asym->value -= asym->section->vma;
934 asym->flags = BSF_DEBUGGING;
937 asym->section = &bfd_abs_section;
940 asym->section = &bfd_und_section;
950 asym->flags = BSF_DEBUGGING;
953 asym->section = bfd_make_section_old_way (abfd, ".sdata");
954 asym->value -= asym->section->vma;
957 asym->section = bfd_make_section_old_way (abfd, ".sbss");
958 asym->value -= asym->section->vma;
961 asym->section = bfd_make_section_old_way (abfd, ".rdata");
962 asym->value -= asym->section->vma;
965 asym->flags = BSF_DEBUGGING;
968 if (asym->value > ecoff_data (abfd)->gp_size)
970 asym->section = &bfd_com_section;
976 if (ecoff_scom_section.name == NULL)
978 /* Initialize the small common section. */
979 ecoff_scom_section.name = SCOMMON;
980 ecoff_scom_section.flags = SEC_IS_COMMON;
981 ecoff_scom_section.output_section = &ecoff_scom_section;
982 ecoff_scom_section.symbol = &ecoff_scom_symbol;
983 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
984 ecoff_scom_symbol.name = SCOMMON;
985 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
986 ecoff_scom_symbol.section = &ecoff_scom_section;
987 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
989 asym->section = &ecoff_scom_section;
994 asym->flags = BSF_DEBUGGING;
997 asym->section = &bfd_und_section;
1002 asym->section = bfd_make_section_old_way (abfd, ".init");
1003 asym->value -= asym->section->vma;
1008 asym->flags = BSF_DEBUGGING;
1011 asym->section = bfd_make_section_old_way (abfd, ".fini");
1012 asym->value -= asym->section->vma;
1018 /* Look for special constructors symbols and make relocation entries
1019 in a special construction section. These are produced by the
1020 -fgnu-linker argument to g++. */
1021 if (ECOFF_IS_STAB (ecoff_sym))
1023 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
1035 arelent_chain *reloc_chain;
1036 unsigned int bitsize;
1038 /* Get a section with the same name as the symbol (usually
1039 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
1040 name ___CTOR_LIST (three underscores). We need
1041 __CTOR_LIST (two underscores), since ECOFF doesn't use
1042 a leading underscore. This should be handled by gcc,
1043 but instead we do it here. Actually, this should all
1044 be done differently anyhow. */
1045 name = bfd_asymbol_name (asym);
1046 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
1051 section = bfd_get_section_by_name (abfd, name);
1052 if (section == (asection *) NULL)
1056 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
1059 bfd_set_error (bfd_error_no_memory);
1062 strcpy (copy, name);
1063 section = bfd_make_section (abfd, copy);
1066 /* Build a reloc pointing to this constructor. */
1068 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1071 bfd_set_error (bfd_error_no_memory);
1074 reloc_chain->relent.sym_ptr_ptr =
1075 bfd_get_section (asym)->symbol_ptr_ptr;
1076 reloc_chain->relent.address = section->_raw_size;
1077 reloc_chain->relent.addend = asym->value;
1078 reloc_chain->relent.howto =
1079 ecoff_backend (abfd)->constructor_reloc;
1081 /* Set up the constructor section to hold the reloc. */
1082 section->flags = SEC_CONSTRUCTOR;
1083 ++section->reloc_count;
1085 /* Constructor sections must be rounded to a boundary
1086 based on the bitsize. These are not real sections--
1087 they are handled specially by the linker--so the ECOFF
1088 16 byte alignment restriction does not apply. */
1089 bitsize = ecoff_backend (abfd)->constructor_bitsize;
1090 section->alignment_power = 1;
1091 while ((1 << section->alignment_power) < bitsize / 8)
1092 ++section->alignment_power;
1094 reloc_chain->next = section->constructor_chain;
1095 section->constructor_chain = reloc_chain;
1096 section->_raw_size += bitsize / 8;
1098 /* Mark the symbol as a constructor. */
1099 asym->flags |= BSF_CONSTRUCTOR;
1107 /* Read an ECOFF symbol table. */
1110 ecoff_slurp_symbol_table (abfd)
1113 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1114 const bfd_size_type external_ext_size
1115 = backend->debug_swap.external_ext_size;
1116 const bfd_size_type external_sym_size
1117 = backend->debug_swap.external_sym_size;
1118 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
1119 = backend->debug_swap.swap_ext_in;
1120 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
1121 = backend->debug_swap.swap_sym_in;
1122 bfd_size_type internal_size;
1123 ecoff_symbol_type *internal;
1124 ecoff_symbol_type *internal_ptr;
1125 asymbol *indirect_ptr;
1131 /* If we've already read in the symbol table, do nothing. */
1132 if (ecoff_data (abfd)->canonical_symbols != NULL)
1135 /* Get the symbolic information. */
1136 if (ecoff_slurp_symbolic_info (abfd) == false)
1138 if (bfd_get_symcount (abfd) == 0)
1141 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1142 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1143 if (internal == NULL)
1145 bfd_set_error (bfd_error_no_memory);
1149 internal_ptr = internal;
1150 indirect_ptr = NULL;
1151 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
1152 eraw_end = (eraw_src
1153 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
1154 * external_ext_size));
1155 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
1159 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
1160 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
1161 + internal_esym.asym.iss);
1162 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
1163 &internal_ptr->symbol, 1, &indirect_ptr))
1165 /* The alpha uses a negative ifd field for section symbols. */
1166 if (internal_esym.ifd >= 0)
1167 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
1168 + internal_esym.ifd);
1170 internal_ptr->fdr = NULL;
1171 internal_ptr->local = false;
1172 internal_ptr->native = (PTR) eraw_src;
1174 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1176 /* The local symbols must be accessed via the fdr's, because the
1177 string and aux indices are relative to the fdr information. */
1178 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
1179 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
1180 for (; fdr_ptr < fdr_end; fdr_ptr++)
1185 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
1186 + fdr_ptr->isymBase * external_sym_size);
1187 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1189 lraw_src < lraw_end;
1190 lraw_src += external_sym_size, internal_ptr++)
1194 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1195 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
1197 + internal_sym.iss);
1198 if (!ecoff_set_symbol_info (abfd, &internal_sym,
1199 &internal_ptr->symbol, 0, &indirect_ptr))
1201 internal_ptr->fdr = fdr_ptr;
1202 internal_ptr->local = true;
1203 internal_ptr->native = (PTR) lraw_src;
1206 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1208 ecoff_data (abfd)->canonical_symbols = internal;
1213 /* Return the amount of space needed for the canonical symbols. */
1216 ecoff_get_symtab_upper_bound (abfd)
1219 if (ecoff_slurp_symbolic_info (abfd) == false
1220 || bfd_get_symcount (abfd) == 0)
1223 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1226 /* Get the canonical symbols. */
1229 ecoff_get_symtab (abfd, alocation)
1231 asymbol **alocation;
1233 unsigned int counter = 0;
1234 ecoff_symbol_type *symbase;
1235 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1237 if (ecoff_slurp_symbol_table (abfd) == false
1238 || bfd_get_symcount (abfd) == 0)
1241 symbase = ecoff_data (abfd)->canonical_symbols;
1242 while (counter < bfd_get_symcount (abfd))
1244 *(location++) = symbase++;
1247 *location++ = (ecoff_symbol_type *) NULL;
1248 return bfd_get_symcount (abfd);
1251 /* Turn ECOFF type information into a printable string.
1252 ecoff_emit_aggregate and ecoff_type_to_string are from
1253 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1255 /* Write aggregate information to a string. */
1258 ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1265 int ifd = rndx->rfd;
1266 int indx = rndx->index;
1267 int sym_base, ss_base;
1273 sym_base = ecoff_data (abfd)->debug_info.fdr[ifd].isymBase;
1274 ss_base = ecoff_data (abfd)->debug_info.fdr[ifd].issBase;
1276 if (indx == indexNil)
1277 name = "/* no name */";
1280 const struct ecoff_debug_swap * const debug_swap
1281 = &ecoff_backend (abfd)->debug_swap;
1285 (*debug_swap->swap_sym_in)
1287 ((char *) ecoff_data (abfd)->debug_info.external_sym
1288 + indx * debug_swap->external_sym_size),
1290 name = ecoff_data (abfd)->debug_info.ss + ss_base + sym.iss;
1294 "%s %s { ifd = %d, index = %ld }",
1297 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax));
1300 /* Convert the type information to string format. */
1303 ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1305 union aux_ext *aux_ptr;
1317 unsigned int basic_type;
1319 static char buffer1[1024];
1320 static char buffer2[1024];
1325 for (i = 0; i < 7; i++)
1327 qualifiers[i].low_bound = 0;
1328 qualifiers[i].high_bound = 0;
1329 qualifiers[i].stride = 0;
1332 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1333 return "-1 (no type)";
1334 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1336 basic_type = u.ti.bt;
1337 qualifiers[0].type = u.ti.tq0;
1338 qualifiers[1].type = u.ti.tq1;
1339 qualifiers[2].type = u.ti.tq2;
1340 qualifiers[3].type = u.ti.tq3;
1341 qualifiers[4].type = u.ti.tq4;
1342 qualifiers[5].type = u.ti.tq5;
1343 qualifiers[6].type = tqNil;
1346 * Go get the basic type.
1350 case btNil: /* undefined */
1354 case btAdr: /* address - integer same size as pointer */
1355 strcpy (p1, "address");
1358 case btChar: /* character */
1359 strcpy (p1, "char");
1362 case btUChar: /* unsigned character */
1363 strcpy (p1, "unsigned char");
1366 case btShort: /* short */
1367 strcpy (p1, "short");
1370 case btUShort: /* unsigned short */
1371 strcpy (p1, "unsigned short");
1374 case btInt: /* int */
1378 case btUInt: /* unsigned int */
1379 strcpy (p1, "unsigned int");
1382 case btLong: /* long */
1383 strcpy (p1, "long");
1386 case btULong: /* unsigned long */
1387 strcpy (p1, "unsigned long");
1390 case btFloat: /* float (real) */
1391 strcpy (p1, "float");
1394 case btDouble: /* Double (real) */
1395 strcpy (p1, "double");
1398 /* Structures add 1-2 aux words:
1399 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1400 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1402 case btStruct: /* Structure (Record) */
1403 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1404 ecoff_emit_aggregate (abfd, p1, &rndx,
1405 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1407 indx++; /* skip aux words */
1410 /* Unions add 1-2 aux words:
1411 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1412 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1414 case btUnion: /* Union */
1415 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1416 ecoff_emit_aggregate (abfd, p1, &rndx,
1417 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1419 indx++; /* skip aux words */
1422 /* Enumerations add 1-2 aux words:
1423 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1424 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1426 case btEnum: /* Enumeration */
1427 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1428 ecoff_emit_aggregate (abfd, p1, &rndx,
1429 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1431 indx++; /* skip aux words */
1434 case btTypedef: /* defined via a typedef, isymRef points */
1435 strcpy (p1, "typedef");
1438 case btRange: /* subrange of int */
1439 strcpy (p1, "subrange");
1442 case btSet: /* pascal sets */
1446 case btComplex: /* fortran complex */
1447 strcpy (p1, "complex");
1450 case btDComplex: /* fortran double complex */
1451 strcpy (p1, "double complex");
1454 case btIndirect: /* forward or unnamed typedef */
1455 strcpy (p1, "forward/unamed typedef");
1458 case btFixedDec: /* Fixed Decimal */
1459 strcpy (p1, "fixed decimal");
1462 case btFloatDec: /* Float Decimal */
1463 strcpy (p1, "float decimal");
1466 case btString: /* Varying Length Character String */
1467 strcpy (p1, "string");
1470 case btBit: /* Aligned Bit String */
1474 case btPicture: /* Picture */
1475 strcpy (p1, "picture");
1478 case btVoid: /* Void */
1479 strcpy (p1, "void");
1483 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1487 p1 += strlen (buffer1);
1490 * If this is a bitfield, get the bitsize.
1496 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1497 sprintf (p1, " : %d", bitsize);
1498 p1 += strlen (buffer1);
1503 * Deal with any qualifiers.
1505 if (qualifiers[0].type != tqNil)
1508 * Snarf up any array bounds in the correct order. Arrays
1509 * store 5 successive words in the aux. table:
1510 * word 0 RNDXR to type of the bounds (ie, int)
1511 * word 1 Current file descriptor index
1513 * word 3 high bound (or -1 if [])
1514 * word 4 stride size in bits
1516 for (i = 0; i < 7; i++)
1518 if (qualifiers[i].type == tqArray)
1520 qualifiers[i].low_bound =
1521 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1522 qualifiers[i].high_bound =
1523 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1524 qualifiers[i].stride =
1525 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1531 * Now print out the qualifiers.
1533 for (i = 0; i < 6; i++)
1535 switch (qualifiers[i].type)
1542 strcpy (p2, "ptr to ");
1543 p2 += sizeof ("ptr to ")-1;
1547 strcpy (p2, "volatile ");
1548 p2 += sizeof ("volatile ")-1;
1552 strcpy (p2, "far ");
1553 p2 += sizeof ("far ")-1;
1557 strcpy (p2, "func. ret. ");
1558 p2 += sizeof ("func. ret. ");
1563 int first_array = i;
1566 /* Print array bounds reversed (ie, in the order the C
1567 programmer writes them). C is such a fun language.... */
1569 while (i < 5 && qualifiers[i+1].type == tqArray)
1572 for (j = i; j >= first_array; j--)
1574 strcpy (p2, "array [");
1575 p2 += sizeof ("array [")-1;
1576 if (qualifiers[j].low_bound != 0)
1578 "%ld:%ld {%ld bits}",
1579 (long) qualifiers[j].low_bound,
1580 (long) qualifiers[j].high_bound,
1581 (long) qualifiers[j].stride);
1583 else if (qualifiers[j].high_bound != -1)
1586 (long) (qualifiers[j].high_bound + 1),
1587 (long) (qualifiers[j].stride));
1590 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1593 strcpy (p2, "] of ");
1594 p2 += sizeof ("] of ")-1;
1602 strcpy (p2, buffer1);
1606 /* Return information about ECOFF symbol SYMBOL in RET. */
1610 ecoff_get_symbol_info (abfd, symbol, ret)
1611 bfd *abfd; /* Ignored. */
1615 bfd_symbol_info (symbol, ret);
1618 /* Print information about an ECOFF symbol. */
1621 ecoff_print_symbol (abfd, filep, symbol, how)
1625 bfd_print_symbol_type how;
1627 const struct ecoff_debug_swap * const debug_swap
1628 = &ecoff_backend (abfd)->debug_swap;
1629 FILE *file = (FILE *)filep;
1633 case bfd_print_symbol_name:
1634 fprintf (file, "%s", symbol->name);
1636 case bfd_print_symbol_more:
1637 if (ecoffsymbol (symbol)->local)
1641 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1643 fprintf (file, "ecoff local ");
1644 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1645 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1646 (unsigned) ecoff_sym.sc);
1652 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1654 fprintf (file, "ecoff extern ");
1655 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1656 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1657 (unsigned) ecoff_ext.asym.sc);
1660 case bfd_print_symbol_all:
1661 /* Print out the symbols in a reasonable way */
1670 if (ecoffsymbol (symbol)->local)
1672 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1675 pos = ((((char *) ecoffsymbol (symbol)->native
1676 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1677 / debug_swap->external_sym_size)
1678 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1685 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1688 pos = (((char *) ecoffsymbol (symbol)->native
1689 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1690 / debug_swap->external_ext_size);
1691 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1692 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1693 weakext = ecoff_ext.weakext ? 'w' : ' ';
1696 fprintf (file, "[%3d] %c ",
1698 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1699 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1700 (unsigned) ecoff_ext.asym.st,
1701 (unsigned) ecoff_ext.asym.sc,
1702 (unsigned) ecoff_ext.asym.index,
1703 jmptbl, cobol_main, weakext,
1706 if (ecoffsymbol (symbol)->fdr != NULL
1707 && ecoff_ext.asym.index != indexNil)
1711 bfd_size_type sym_base;
1712 union aux_ext *aux_base;
1714 indx = ecoff_ext.asym.index;
1716 /* sym_base is used to map the fdr relative indices which
1717 appear in the file to the position number which we are
1719 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1720 if (ecoffsymbol (symbol)->local)
1722 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1724 /* aux_base is the start of the aux entries for this file;
1725 asym.index is an offset from this. */
1726 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1727 + ecoffsymbol (symbol)->fdr->iauxBase);
1729 /* The aux entries are stored in host byte order; the
1730 order is indicated by a bit in the fdr. */
1731 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1733 /* This switch is basically from gcc/mips-tdump.c */
1734 switch (ecoff_ext.asym.st)
1742 fprintf (file, "\n End+1 symbol: %ld",
1743 (long) (indx + sym_base));
1747 if (ecoff_ext.asym.sc == scText
1748 || ecoff_ext.asym.sc == scInfo)
1749 fprintf (file, "\n First symbol: %ld",
1750 (long) (indx + sym_base));
1752 fprintf (file, "\n First symbol: %ld",
1753 (long) (AUX_GET_ISYM (bigendian,
1754 &aux_base[ecoff_ext.asym.index])
1760 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1762 else if (ecoffsymbol (symbol)->local)
1763 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1764 (long) (AUX_GET_ISYM (bigendian,
1765 &aux_base[ecoff_ext.asym.index])
1767 ecoff_type_to_string (abfd, aux_base, indx + 1,
1770 fprintf (file, "\n Local symbol: %ld",
1773 + (ecoff_data (abfd)
1774 ->debug_info.symbolic_header.iextMax)));
1778 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1779 fprintf (file, "\n Type: %s",
1780 ecoff_type_to_string (abfd, aux_base, indx,
1790 /* Read in the relocs for a section. */
1793 ecoff_slurp_reloc_table (abfd, section, symbols)
1798 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1799 arelent *internal_relocs;
1800 bfd_size_type external_reloc_size;
1801 bfd_size_type external_relocs_size;
1802 char *external_relocs;
1806 if (section->relocation != (arelent *) NULL
1807 || section->reloc_count == 0
1808 || (section->flags & SEC_CONSTRUCTOR) != 0)
1811 if (ecoff_slurp_symbol_table (abfd) == false)
1814 internal_relocs = (arelent *) bfd_alloc (abfd,
1816 * section->reloc_count));
1817 external_reloc_size = backend->external_reloc_size;
1818 external_relocs_size = external_reloc_size * section->reloc_count;
1819 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1820 if (internal_relocs == (arelent *) NULL
1821 || external_relocs == (char *) NULL)
1823 bfd_set_error (bfd_error_no_memory);
1826 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1828 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1829 != external_relocs_size)
1832 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1834 struct internal_reloc intern;
1836 (*backend->swap_reloc_in) (abfd,
1837 external_relocs + i * external_reloc_size,
1840 if (intern.r_extern)
1842 /* r_symndx is an index into the external symbols. */
1843 BFD_ASSERT (intern.r_symndx >= 0
1845 < (ecoff_data (abfd)
1846 ->debug_info.symbolic_header.iextMax)));
1847 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1850 else if (intern.r_symndx == RELOC_SECTION_NONE
1851 || intern.r_symndx == RELOC_SECTION_ABS)
1853 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1858 CONST char *sec_name;
1861 /* r_symndx is a section key. */
1862 switch (intern.r_symndx)
1864 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1865 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1866 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1867 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1868 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1869 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1870 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1871 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1872 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1873 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1874 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1875 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1876 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1880 sec = bfd_get_section_by_name (abfd, sec_name);
1881 if (sec == (asection *) NULL)
1883 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1885 rptr->addend = - bfd_get_section_vma (abfd, sec);
1888 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1890 /* Let the backend select the howto field and do any other
1891 required processing. */
1892 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1895 bfd_release (abfd, external_relocs);
1897 section->relocation = internal_relocs;
1902 /* Get a canonical list of relocs. */
1905 ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1913 if (section->flags & SEC_CONSTRUCTOR)
1915 arelent_chain *chain;
1917 /* This section has relocs made up by us, not the file, so take
1918 them out of their chain and place them into the data area
1920 for (count = 0, chain = section->constructor_chain;
1921 count < section->reloc_count;
1922 count++, chain = chain->next)
1923 *relptr++ = &chain->relent;
1929 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1932 tblptr = section->relocation;
1933 if (tblptr == (arelent *) NULL)
1936 for (count = 0; count < section->reloc_count; count++)
1937 *relptr++ = tblptr++;
1940 *relptr = (arelent *) NULL;
1942 return section->reloc_count;
1945 /* Provided a BFD, a section and an offset into the section, calculate
1946 and return the name of the source file and the line nearest to the
1951 ecoff_find_nearest_line (abfd,
1960 asymbol **ignore_symbols;
1962 CONST char **filename_ptr;
1963 CONST char **functionname_ptr;
1964 unsigned int *retline_ptr;
1966 const struct ecoff_debug_swap * const debug_swap
1967 = &ecoff_backend (abfd)->debug_swap;
1972 bfd_size_type external_pdr_size;
1976 unsigned char *line_ptr;
1977 unsigned char *line_end;
1980 /* If we're not in the .text section, we don't have any line
1982 if (strcmp (section->name, _TEXT) != 0
1983 || offset < ecoff_data (abfd)->text_start
1984 || offset >= ecoff_data (abfd)->text_end)
1987 /* Make sure we have the FDR's. */
1988 if (ecoff_slurp_symbolic_info (abfd) == false
1989 || bfd_get_symcount (abfd) == 0)
1992 /* Each file descriptor (FDR) has a memory address. Here we track
1993 down which FDR we want. The FDR's are stored in increasing
1994 memory order. If speed is ever important, this can become a
1995 binary search. We must ignore FDR's with no PDR entries; they
1996 will have the adr of the FDR before or after them. */
1997 fdr_start = ecoff_data (abfd)->debug_info.fdr;
1998 fdr_end = fdr_start + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
1999 fdr_hold = (FDR *) NULL;
2000 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2002 if (fdr_ptr->cpd == 0)
2004 if (offset < fdr_ptr->adr)
2008 if (fdr_hold == (FDR *) NULL)
2012 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2013 have an address, which is relative to the FDR address, and are
2014 also stored in increasing memory order. */
2015 offset -= fdr_ptr->adr;
2016 external_pdr_size = debug_swap->external_pdr_size;
2017 pdr_ptr = ((char *) ecoff_data (abfd)->debug_info.external_pdr
2018 + fdr_ptr->ipdFirst * external_pdr_size);
2019 pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2020 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2022 /* The address of the first PDR is an offset which applies to the
2023 addresses of all the PDR's. */
2026 for (pdr_ptr += external_pdr_size;
2028 pdr_ptr += external_pdr_size)
2030 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2031 if (offset < pdr.adr)
2035 /* Now we can look for the actual line number. The line numbers are
2036 stored in a very funky format, which I won't try to describe.
2037 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2038 we want; we need this to compute line_end. */
2039 line_end = ecoff_data (abfd)->debug_info.line;
2040 if (pdr_ptr == pdr_end)
2041 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2043 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2045 /* Now change pdr and pdr_ptr to the one we want. */
2046 pdr_ptr -= external_pdr_size;
2047 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2051 line_ptr = (ecoff_data (abfd)->debug_info.line
2052 + fdr_ptr->cbLineOffset
2053 + pdr.cbLineOffset);
2054 while (line_ptr < line_end)
2059 delta = *line_ptr >> 4;
2062 count = (*line_ptr & 0xf) + 1;
2066 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2067 if (delta >= 0x8000)
2072 if (offset < count * 4)
2074 offset -= count * 4;
2077 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2078 at least according to gdb/mipsread.c. */
2079 if (fdr_ptr->rss == -1)
2081 *filename_ptr = NULL;
2083 *functionname_ptr = NULL;
2088 (*debug_swap->swap_ext_in)
2090 ((char *) ecoff_data (abfd)->debug_info.external_ext
2091 + pdr.isym * debug_swap->external_ext_size),
2093 *functionname_ptr = (ecoff_data (abfd)->debug_info.ssext
2094 + proc_ext.asym.iss);
2101 *filename_ptr = (ecoff_data (abfd)->debug_info.ss
2104 (*debug_swap->swap_sym_in)
2106 ((char *) ecoff_data (abfd)->debug_info.external_sym
2107 + (fdr_ptr->isymBase + pdr.isym) * debug_swap->external_sym_size),
2109 *functionname_ptr = (ecoff_data (abfd)->debug_info.ss
2113 if (lineno == ilineNil)
2115 *retline_ptr = lineno;
2119 /* Set the architecture. The supported architecture is stored in the
2120 backend pointer. We always set the architecture anyhow, since many
2121 callers ignore the return value. */
2124 ecoff_set_arch_mach (abfd, arch, machine)
2126 enum bfd_architecture arch;
2127 unsigned long machine;
2129 bfd_default_set_arch_mach (abfd, arch, machine);
2130 return arch == ecoff_backend (abfd)->arch;
2133 /* Get the size of the section headers. We do not output the .reginfo
2138 ecoff_sizeof_headers (abfd, reloc)
2147 for (current = abfd->sections;
2148 current != (asection *)NULL;
2149 current = current->next)
2150 if (strcmp (current->name, REGINFO) != 0)
2153 ret = (bfd_coff_filhsz (abfd)
2154 + bfd_coff_aoutsz (abfd)
2155 + c * bfd_coff_scnhsz (abfd));
2156 return BFD_ALIGN (ret, 16);
2159 /* Get the contents of a section. This is where we handle reading the
2160 .reginfo section, which implicitly holds the contents of an
2161 ecoff_reginfo structure. */
2164 ecoff_get_section_contents (abfd, section, location, offset, count)
2169 bfd_size_type count;
2171 ecoff_data_type *tdata = ecoff_data (abfd);
2172 struct ecoff_reginfo s;
2175 if (strcmp (section->name, REGINFO) != 0)
2176 return bfd_generic_get_section_contents (abfd, section, location,
2179 s.gp_value = tdata->gp;
2180 s.gprmask = tdata->gprmask;
2181 for (i = 0; i < 4; i++)
2182 s.cprmask[i] = tdata->cprmask[i];
2183 s.fprmask = tdata->fprmask;
2185 /* bfd_get_section_contents has already checked that the offset and
2186 size is reasonable. We don't have to worry about swapping or any
2187 such thing; the .reginfo section is defined such that the
2188 contents are an ecoff_reginfo structure as seen on the host. */
2189 memcpy (location, ((char *) &s) + offset, (size_t) count);
2193 /* Calculate the file position for each section, and set
2197 ecoff_compute_section_file_positions (abfd)
2205 sofar = ecoff_sizeof_headers (abfd, false);
2208 for (current = abfd->sections;
2209 current != (asection *) NULL;
2210 current = current->next)
2212 unsigned int alignment_power;
2214 /* Only deal with sections which have contents */
2215 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) == 0
2216 || strcmp (current->name, REGINFO) == 0)
2219 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2220 supposed to indicate the number of .pdata entries that are
2221 really in the section. Each entry is 8 bytes. We store this
2222 away in line_filepos before increasing the section size. */
2223 if (strcmp (current->name, _PDATA) != 0)
2224 alignment_power = current->alignment_power;
2227 current->line_filepos = current->_raw_size / 8;
2228 alignment_power = 4;
2231 /* On Ultrix, the data sections in an executable file must be
2232 aligned to a page boundary within the file. This does not
2233 affect the section size, though. FIXME: Does this work for
2234 other platforms? It requires some modification for the
2235 Alpha, because .rdata on the Alpha goes with the text, not
2237 if ((abfd->flags & EXEC_P) != 0
2238 && (abfd->flags & D_PAGED) != 0
2239 && first_data != false
2240 && (current->flags & SEC_CODE) == 0
2241 && (! ecoff_backend (abfd)->rdata_in_text
2242 || strcmp (current->name, _RDATA) != 0)
2243 && strcmp (current->name, _PDATA) != 0)
2245 const bfd_vma round = ecoff_backend (abfd)->round;
2247 sofar = (sofar + round - 1) &~ (round - 1);
2251 /* Align the sections in the file to the same boundary on
2252 which they are aligned in virtual memory. */
2254 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2256 current->filepos = sofar;
2258 sofar += current->_raw_size;
2260 /* make sure that this section is of the right size too */
2262 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2263 current->_raw_size += sofar - old_sofar;
2266 ecoff_data (abfd)->reloc_filepos = sofar;
2269 /* Determine the location of the relocs for all the sections in the
2270 output file, as well as the location of the symbolic debugging
2273 static bfd_size_type
2274 ecoff_compute_reloc_file_positions (abfd)
2277 const bfd_size_type external_reloc_size =
2278 ecoff_backend (abfd)->external_reloc_size;
2279 file_ptr reloc_base;
2280 bfd_size_type reloc_size;
2284 if (! abfd->output_has_begun)
2286 ecoff_compute_section_file_positions (abfd);
2287 abfd->output_has_begun = true;
2290 reloc_base = ecoff_data (abfd)->reloc_filepos;
2293 for (current = abfd->sections;
2294 current != (asection *)NULL;
2295 current = current->next)
2297 if (strcmp (current->name, REGINFO) == 0)
2299 if (current->reloc_count == 0)
2300 current->rel_filepos = 0;
2303 bfd_size_type relsize;
2305 current->rel_filepos = reloc_base;
2306 relsize = current->reloc_count * external_reloc_size;
2307 reloc_size += relsize;
2308 reloc_base += relsize;
2312 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2314 /* At least on Ultrix, the symbol table of an executable file must
2315 be aligned to a page boundary. FIXME: Is this true on other
2317 if ((abfd->flags & EXEC_P) != 0
2318 && (abfd->flags & D_PAGED) != 0)
2319 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2320 &~ (ecoff_backend (abfd)->round - 1));
2322 ecoff_data (abfd)->sym_filepos = sym_base;
2327 /* Set the contents of a section. This is where we handle setting the
2328 contents of the .reginfo section, which implicitly holds a
2329 ecoff_reginfo structure. */
2332 ecoff_set_section_contents (abfd, section, location, offset, count)
2337 bfd_size_type count;
2339 /* This must be done first, because bfd_set_section_contents is
2340 going to set output_has_begun to true. */
2341 if (abfd->output_has_begun == false)
2342 ecoff_compute_section_file_positions (abfd);
2347 if (strcmp (section->name, REGINFO) == 0)
2349 ecoff_data_type *tdata = ecoff_data (abfd);
2350 struct ecoff_reginfo s;
2353 /* If the caller is only changing part of the structure, we must
2354 retrieve the current information before the memcpy. */
2355 if (offset != 0 || count != sizeof (struct ecoff_reginfo))
2357 s.gp_value = tdata->gp;
2358 s.gprmask = tdata->gprmask;
2359 for (i = 0; i < 4; i++)
2360 s.cprmask[i] = tdata->cprmask[i];
2361 s.fprmask = tdata->fprmask;
2364 /* bfd_set_section_contents has already checked that the offset
2365 and size is reasonable. We don't have to worry about
2366 swapping or any such thing; the .reginfo section is defined
2367 such that the contents are an ecoff_reginfo structure as seen
2369 memcpy (((char *) &s) + offset, location, (size_t) count);
2371 tdata->gp = s.gp_value;
2372 tdata->gprmask = s.gprmask;
2373 for (i = 0; i < 4; i++)
2374 tdata->cprmask[i] = s.cprmask[i];
2375 tdata->fprmask = s.fprmask;
2380 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2381 || bfd_write (location, 1, count, abfd) != count)
2387 /* Get ECOFF EXTR information for an external symbol. This function
2388 is passed to bfd_ecoff_debug_externals. */
2391 ecoff_get_extr (sym, esym)
2395 ecoff_symbol_type *ecoff_sym_ptr;
2398 /* Don't include debugging, local or section symbols. */
2399 if ((sym->flags & BSF_DEBUGGING) != 0
2400 || (sym->flags & BSF_LOCAL) != 0
2401 || (sym->flags & BSF_SECTION_SYM) != 0)
2404 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2405 || ecoffsymbol (sym)->native == NULL)
2408 esym->cobol_main = 0;
2412 /* FIXME: we can do better than this for st and sc. */
2413 esym->asym.st = stGlobal;
2414 esym->asym.sc = scAbs;
2415 esym->asym.reserved = 0;
2416 esym->asym.index = indexNil;
2420 ecoff_sym_ptr = ecoffsymbol (sym);
2422 if (ecoff_sym_ptr->local)
2425 input_bfd = bfd_asymbol_bfd (sym);
2426 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2427 (input_bfd, ecoff_sym_ptr->native, esym);
2429 /* If the symbol was defined by the linker, then esym will be
2430 undefined but sym will not be. Get a better class for such a
2432 if ((esym->asym.sc == scUndefined
2433 || esym->asym.sc == scSUndefined)
2434 && bfd_get_section (sym) != &bfd_und_section)
2435 esym->asym.sc = scAbs;
2437 /* Adjust the FDR index for the symbol by that used for the input
2439 if (esym->ifd != -1)
2441 struct ecoff_debug_info *input_debug;
2443 input_debug = &ecoff_data (input_bfd)->debug_info;
2444 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2445 if (input_debug->ifdmap != (RFDT *) NULL)
2446 esym->ifd = input_debug->ifdmap[esym->ifd];
2452 /* Set the external symbol index. This routine is passed to
2453 bfd_ecoff_debug_externals. */
2456 ecoff_set_index (sym, indx)
2460 ecoff_set_sym_index (sym, indx);
2463 /* Write out an ECOFF file. */
2466 ecoff_write_object_contents (abfd)
2469 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2470 const bfd_vma round = backend->round;
2471 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2472 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2473 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2474 const bfd_size_type external_hdr_size
2475 = backend->debug_swap.external_hdr_size;
2476 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2477 void (* const adjust_reloc_out) PARAMS ((bfd *,
2479 struct internal_reloc *))
2480 = backend->adjust_reloc_out;
2481 void (* const swap_reloc_out) PARAMS ((bfd *,
2482 const struct internal_reloc *,
2484 = backend->swap_reloc_out;
2485 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2486 HDRR * const symhdr = &debug->symbolic_header;
2489 bfd_size_type reloc_size;
2490 bfd_size_type text_size;
2492 bfd_size_type data_size;
2494 bfd_size_type bss_size;
2496 PTR reloc_buff = NULL;
2497 struct internal_filehdr internal_f;
2498 struct internal_aouthdr internal_a;
2501 /* Determine where the sections and relocs will go in the output
2503 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2506 for (current = abfd->sections;
2507 current != (asection *)NULL;
2508 current = current->next)
2510 if (strcmp (current->name, REGINFO) == 0)
2512 current->target_index = count;
2516 if ((abfd->flags & D_PAGED) != 0)
2517 text_size = ecoff_sizeof_headers (abfd, false);
2525 /* Write section headers to the file. */
2527 /* Allocate buff big enough to hold a section header,
2528 file header, or a.out header. */
2536 buff = (PTR) malloc (siz);
2539 bfd_set_error (bfd_error_no_memory);
2544 internal_f.f_nscns = 0;
2545 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2547 for (current = abfd->sections;
2548 current != (asection *) NULL;
2549 current = current->next)
2551 struct internal_scnhdr section;
2554 if (strcmp (current->name, REGINFO) == 0)
2556 BFD_ASSERT (current->reloc_count == 0);
2560 ++internal_f.f_nscns;
2562 strncpy (section.s_name, current->name, sizeof section.s_name);
2564 /* FIXME: is this correct for shared libraries? I think it is
2565 but I have no platform to check. Ian Lance Taylor. */
2566 vma = bfd_get_section_vma (abfd, current);
2567 if (strcmp (current->name, _LIB) == 0)
2568 section.s_vaddr = 0;
2570 section.s_vaddr = vma;
2572 section.s_paddr = vma;
2573 section.s_size = bfd_get_section_size_before_reloc (current);
2575 /* If this section is unloadable then the scnptr will be 0. */
2576 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2577 section.s_scnptr = 0;
2579 section.s_scnptr = current->filepos;
2580 section.s_relptr = current->rel_filepos;
2582 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2583 object file produced by the assembler is supposed to point to
2584 information about how much room is required by objects of
2585 various different sizes. I think this only matters if we
2586 want the linker to compute the best size to use, or
2587 something. I don't know what happens if the information is
2589 if (strcmp (current->name, _PDATA) != 0)
2590 section.s_lnnoptr = 0;
2593 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2594 hold the number of entries in the section (each entry is
2595 8 bytes). We stored this in the line_filepos field in
2596 ecoff_compute_section_file_positions. */
2597 section.s_lnnoptr = current->line_filepos;
2600 section.s_nreloc = current->reloc_count;
2601 section.s_nlnno = 0;
2602 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2605 bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff);
2606 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2609 if ((section.s_flags & STYP_TEXT) != 0
2610 || ((section.s_flags & STYP_RDATA) != 0
2611 && backend->rdata_in_text)
2612 || strcmp (current->name, _PDATA) == 0)
2614 text_size += bfd_get_section_size_before_reloc (current);
2615 if (text_start == 0 || text_start > vma)
2618 else if ((section.s_flags & STYP_RDATA) != 0
2619 || (section.s_flags & STYP_DATA) != 0
2620 || (section.s_flags & STYP_LITA) != 0
2621 || (section.s_flags & STYP_LIT8) != 0
2622 || (section.s_flags & STYP_LIT4) != 0
2623 || (section.s_flags & STYP_SDATA) != 0
2624 || strcmp (current->name, _XDATA) == 0)
2626 data_size += bfd_get_section_size_before_reloc (current);
2627 if (data_start == 0 || data_start > vma)
2630 else if ((section.s_flags & STYP_BSS) != 0
2631 || (section.s_flags & STYP_SBSS) != 0)
2632 bss_size += bfd_get_section_size_before_reloc (current);
2637 /* Set up the file header. */
2639 internal_f.f_magic = ecoff_get_magic (abfd);
2641 /* We will NOT put a fucking timestamp in the header here. Every
2642 time you put it back, I will come in and take it out again. I'm
2643 sorry. This field does not belong here. We fill it with a 0 so
2644 it compares the same but is not a reasonable time. --
2646 internal_f.f_timdat = 0;
2648 if (bfd_get_symcount (abfd) != 0)
2650 /* The ECOFF f_nsyms field is not actually the number of
2651 symbols, it's the size of symbolic information header. */
2652 internal_f.f_nsyms = external_hdr_size;
2653 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2657 internal_f.f_nsyms = 0;
2658 internal_f.f_symptr = 0;
2661 internal_f.f_opthdr = aoutsz;
2663 internal_f.f_flags = F_LNNO;
2664 if (reloc_size == 0)
2665 internal_f.f_flags |= F_RELFLG;
2666 if (bfd_get_symcount (abfd) == 0)
2667 internal_f.f_flags |= F_LSYMS;
2668 if (abfd->flags & EXEC_P)
2669 internal_f.f_flags |= F_EXEC;
2671 if (! abfd->xvec->byteorder_big_p)
2672 internal_f.f_flags |= F_AR32WR;
2674 internal_f.f_flags |= F_AR32W;
2676 /* Set up the ``optional'' header. */
2677 if ((abfd->flags & D_PAGED) != 0)
2678 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2680 internal_a.magic = ECOFF_AOUT_OMAGIC;
2682 /* FIXME: Is this really correct? */
2683 internal_a.vstamp = symhdr->vstamp;
2685 /* At least on Ultrix, these have to be rounded to page boundaries.
2686 FIXME: Is this true on other platforms? */
2687 if ((abfd->flags & D_PAGED) != 0)
2689 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2690 internal_a.text_start = text_start &~ (round - 1);
2691 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2692 internal_a.data_start = data_start &~ (round - 1);
2696 internal_a.tsize = text_size;
2697 internal_a.text_start = text_start;
2698 internal_a.dsize = data_size;
2699 internal_a.data_start = data_start;
2702 /* On Ultrix, the initial portions of the .sbss and .bss segments
2703 are at the end of the data section. The bsize field in the
2704 optional header records how many bss bytes are required beyond
2705 those in the data section. The value is not rounded to a page
2707 if (bss_size < internal_a.dsize - data_size)
2710 bss_size -= internal_a.dsize - data_size;
2711 internal_a.bsize = bss_size;
2712 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2714 internal_a.entry = bfd_get_start_address (abfd);
2716 internal_a.gp_value = ecoff_data (abfd)->gp;
2718 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2719 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2720 for (i = 0; i < 4; i++)
2721 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2723 /* Write out the file header and the optional header. */
2725 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2728 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2729 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2732 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2733 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2736 /* Build the external symbol information. This must be done before
2737 writing out the relocs so that we know the symbol indices. The
2738 condition checks makes sure this object was not created by
2739 ecoff_bfd_final_link, since if it was we do not want to tamper
2740 with the external symbols. */
2741 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL)
2743 symhdr->iextMax = 0;
2744 symhdr->issExtMax = 0;
2745 debug->external_ext = debug->external_ext_end = NULL;
2746 debug->ssext = debug->ssext_end = NULL;
2747 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2748 (((abfd->flags & EXEC_P) == 0)
2750 ecoff_get_extr, ecoff_set_index)
2754 /* Write out the relocs. */
2755 for (current = abfd->sections;
2756 current != (asection *) NULL;
2757 current = current->next)
2759 arelent **reloc_ptr_ptr;
2760 arelent **reloc_end;
2763 if (current->reloc_count == 0)
2767 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2768 if (reloc_buff == NULL)
2770 bfd_set_error (bfd_error_no_memory);
2774 reloc_ptr_ptr = current->orelocation;
2775 reloc_end = reloc_ptr_ptr + current->reloc_count;
2776 out_ptr = (char *) reloc_buff;
2778 reloc_ptr_ptr < reloc_end;
2779 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2783 struct internal_reloc in;
2785 memset ((PTR) &in, 0, sizeof in);
2787 reloc = *reloc_ptr_ptr;
2788 sym = *reloc->sym_ptr_ptr;
2790 in.r_vaddr = (reloc->address
2791 + bfd_get_section_vma (abfd, current));
2792 in.r_type = reloc->howto->type;
2794 if ((sym->flags & BSF_SECTION_SYM) == 0)
2796 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2803 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2804 if (strcmp (name, ".text") == 0)
2805 in.r_symndx = RELOC_SECTION_TEXT;
2806 else if (strcmp (name, ".rdata") == 0)
2807 in.r_symndx = RELOC_SECTION_RDATA;
2808 else if (strcmp (name, ".data") == 0)
2809 in.r_symndx = RELOC_SECTION_DATA;
2810 else if (strcmp (name, ".sdata") == 0)
2811 in.r_symndx = RELOC_SECTION_SDATA;
2812 else if (strcmp (name, ".sbss") == 0)
2813 in.r_symndx = RELOC_SECTION_SBSS;
2814 else if (strcmp (name, ".bss") == 0)
2815 in.r_symndx = RELOC_SECTION_BSS;
2816 else if (strcmp (name, ".init") == 0)
2817 in.r_symndx = RELOC_SECTION_INIT;
2818 else if (strcmp (name, ".lit8") == 0)
2819 in.r_symndx = RELOC_SECTION_LIT8;
2820 else if (strcmp (name, ".lit4") == 0)
2821 in.r_symndx = RELOC_SECTION_LIT4;
2822 else if (strcmp (name, ".xdata") == 0)
2823 in.r_symndx = RELOC_SECTION_XDATA;
2824 else if (strcmp (name, ".pdata") == 0)
2825 in.r_symndx = RELOC_SECTION_PDATA;
2826 else if (strcmp (name, ".fini") == 0)
2827 in.r_symndx = RELOC_SECTION_FINI;
2828 else if (strcmp (name, ".lita") == 0)
2829 in.r_symndx = RELOC_SECTION_LITA;
2830 else if (strcmp (name, "*ABS*") == 0)
2831 in.r_symndx = RELOC_SECTION_ABS;
2837 (*adjust_reloc_out) (abfd, reloc, &in);
2839 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2842 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2844 if (bfd_write (reloc_buff,
2845 external_reloc_size, current->reloc_count, abfd)
2846 != external_reloc_size * current->reloc_count)
2848 bfd_release (abfd, reloc_buff);
2852 /* Write out the symbolic debugging information. */
2853 if (bfd_get_symcount (abfd) > 0)
2855 /* Write out the debugging information. */
2856 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2857 ecoff_data (abfd)->sym_filepos)
2863 /* The .bss section of a demand paged executable must receive an
2864 entire page. If there are symbols, the symbols will start on the
2865 next page. If there are no symbols, we must fill out the page by
2867 if (bfd_get_symcount (abfd) == 0
2868 && (abfd->flags & EXEC_P) != 0
2869 && (abfd->flags & D_PAGED) != 0)
2873 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2876 if (bfd_read (&c, 1, 1, abfd) == 0)
2878 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2881 if (bfd_write (&c, 1, 1, abfd) != 1)
2885 if (reloc_buff != NULL)
2886 bfd_release (abfd, reloc_buff);
2891 if (reloc_buff != NULL)
2892 bfd_release (abfd, reloc_buff);
2898 /* Archive handling. ECOFF uses what appears to be a unique type of
2899 archive header (armap). The byte ordering of the armap and the
2900 contents are encoded in the name of the armap itself. At least for
2901 now, we only support archives with the same byte ordering in the
2902 armap and the contents.
2904 The first four bytes in the armap are the number of symbol
2905 definitions. This is always a power of two.
2907 This is followed by the symbol definitions. Each symbol definition
2908 occupies 8 bytes. The first four bytes are the offset from the
2909 start of the armap strings to the null-terminated string naming
2910 this symbol. The second four bytes are the file offset to the
2911 archive member which defines this symbol. If the second four bytes
2912 are 0, then this is not actually a symbol definition, and it should
2915 The symbols are hashed into the armap with a closed hashing scheme.
2916 See the functions below for the details of the algorithm.
2918 After the symbol definitions comes four bytes holding the size of
2919 the string table, followed by the string table itself. */
2921 /* The name of an archive headers looks like this:
2922 __________E[BL]E[BL]_ (with a trailing space).
2923 The trailing space is changed to an X if the archive is changed to
2924 indicate that the armap is out of date.
2926 The Alpha seems to use ________64E[BL]E[BL]_. */
2928 #define ARMAP_BIG_ENDIAN 'B'
2929 #define ARMAP_LITTLE_ENDIAN 'L'
2930 #define ARMAP_MARKER 'E'
2931 #define ARMAP_START_LENGTH 10
2932 #define ARMAP_HEADER_MARKER_INDEX 10
2933 #define ARMAP_HEADER_ENDIAN_INDEX 11
2934 #define ARMAP_OBJECT_MARKER_INDEX 12
2935 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2936 #define ARMAP_END_INDEX 14
2937 #define ARMAP_END "_ "
2939 /* This is a magic number used in the hashing algorithm. */
2940 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2942 /* This returns the hash value to use for a string. It also sets
2943 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2944 is the number of entries in the hash table, and HLOG is the log
2948 ecoff_armap_hash (s, rehash, size, hlog)
2950 unsigned int *rehash;
2958 hash = ((hash >> 27) | (hash << 5)) + *s++;
2959 hash *= ARMAP_HASH_MAGIC;
2960 *rehash = (hash & (size - 1)) | 1;
2961 return hash >> (32 - hlog);
2964 /* Read in the armap. */
2967 ecoff_slurp_armap (abfd)
2972 struct areltdata *mapdata;
2973 bfd_size_type parsed_size;
2975 struct artdata *ardata;
2978 struct symdef *symdef_ptr;
2981 /* Get the name of the first element. */
2982 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2988 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
2990 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2991 standard COFF armap. We could move the ECOFF armap stuff into
2992 bfd_slurp_armap, but that seems inappropriate since no other
2993 target uses this format. Instead, we check directly for a COFF
2995 if (strncmp (nextname, "/ ", 16) == 0)
2996 return bfd_slurp_armap (abfd);
2998 /* See if the first element is an armap. */
2999 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
3000 ARMAP_START_LENGTH) != 0
3001 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3002 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3003 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3004 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3005 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3006 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3007 || strncmp (nextname + ARMAP_END_INDEX,
3008 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3010 bfd_has_map (abfd) = false;
3014 /* Make sure we have the right byte ordering. */
3015 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3016 ^ (abfd->xvec->header_byteorder_big_p != false))
3017 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3018 ^ (abfd->xvec->byteorder_big_p != false)))
3020 bfd_set_error (bfd_error_wrong_format);
3024 /* Read in the armap. */
3025 ardata = bfd_ardata (abfd);
3026 mapdata = _bfd_snarf_ar_hdr (abfd);
3027 if (mapdata == (struct areltdata *) NULL)
3029 parsed_size = mapdata->parsed_size;
3030 bfd_release (abfd, (PTR) mapdata);
3032 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3033 if (raw_armap == (char *) NULL)
3035 bfd_set_error (bfd_error_no_memory);
3039 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3041 bfd_set_error (bfd_error_malformed_archive);
3042 bfd_release (abfd, (PTR) raw_armap);
3046 ardata->tdata = (PTR) raw_armap;
3048 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3050 ardata->symdef_count = 0;
3051 ardata->cache = (struct ar_cache *) NULL;
3053 /* This code used to overlay the symdefs over the raw archive data,
3054 but that doesn't work on a 64 bit host. */
3056 stringbase = raw_armap + count * 8 + 8;
3058 #ifdef CHECK_ARMAP_HASH
3062 /* Double check that I have the hashing algorithm right by making
3063 sure that every symbol can be looked up successfully. */
3065 for (i = 1; i < count; i <<= 1)
3067 BFD_ASSERT (i == count);
3069 raw_ptr = raw_armap + 4;
3070 for (i = 0; i < count; i++, raw_ptr += 8)
3072 unsigned int name_offset, file_offset;
3073 unsigned int hash, rehash, srch;
3075 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3076 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3077 if (file_offset == 0)
3079 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3084 /* See if we can rehash to this location. */
3085 for (srch = (hash + rehash) & (count - 1);
3086 srch != hash && srch != i;
3087 srch = (srch + rehash) & (count - 1))
3088 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3090 BFD_ASSERT (srch == i);
3094 #endif /* CHECK_ARMAP_HASH */
3096 raw_ptr = raw_armap + 4;
3097 for (i = 0; i < count; i++, raw_ptr += 8)
3098 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3099 ++ardata->symdef_count;
3101 symdef_ptr = ((struct symdef *)
3103 ardata->symdef_count * sizeof (struct symdef)));
3106 bfd_set_error (bfd_error_no_memory);
3110 ardata->symdefs = (carsym *) symdef_ptr;
3112 raw_ptr = raw_armap + 4;
3113 for (i = 0; i < count; i++, raw_ptr += 8)
3115 unsigned int name_offset, file_offset;
3117 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3118 if (file_offset == 0)
3120 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3121 symdef_ptr->s.name = stringbase + name_offset;
3122 symdef_ptr->file_offset = file_offset;
3126 ardata->first_file_filepos = bfd_tell (abfd);
3127 /* Pad to an even boundary. */
3128 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3130 bfd_has_map (abfd) = true;
3135 /* Write out an armap. */
3138 ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3140 unsigned int elength;
3142 unsigned int orl_count;
3145 unsigned int hashsize, hashlog;
3146 unsigned int symdefsize;
3148 unsigned int stringsize;
3149 unsigned int mapsize;
3152 struct stat statbuf;
3155 bfd_byte *hashtable;
3159 /* Ultrix appears to use as a hash table size the least power of two
3160 greater than twice the number of entries. */
3161 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3163 hashsize = 1 << hashlog;
3165 symdefsize = hashsize * 8;
3167 stringsize = stridx + padit;
3169 /* Include 8 bytes to store symdefsize and stringsize in output. */
3170 mapsize = symdefsize + stringsize + 8;
3172 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3174 memset ((PTR) &hdr, 0, sizeof hdr);
3176 /* Work out the ECOFF armap name. */
3177 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3178 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3179 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3180 (abfd->xvec->header_byteorder_big_p
3182 : ARMAP_LITTLE_ENDIAN);
3183 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3184 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3185 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3186 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3188 /* Write the timestamp of the archive header to be just a little bit
3189 later than the timestamp of the file, otherwise the linker will
3190 complain that the index is out of date. Actually, the Ultrix
3191 linker just checks the archive name; the GNU linker may check the
3193 stat (abfd->filename, &statbuf);
3194 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3196 /* The DECstation uses zeroes for the uid, gid and mode of the
3198 hdr.ar_uid[0] = '0';
3199 hdr.ar_gid[0] = '0';
3200 hdr.ar_mode[0] = '0';
3202 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3204 hdr.ar_fmag[0] = '`';
3205 hdr.ar_fmag[1] = '\012';
3207 /* Turn all null bytes in the header into spaces. */
3208 for (i = 0; i < sizeof (struct ar_hdr); i++)
3209 if (((char *)(&hdr))[i] == '\0')
3210 (((char *)(&hdr))[i]) = ' ';
3212 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3213 != sizeof (struct ar_hdr))
3216 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3217 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3220 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3223 bfd_set_error (bfd_error_no_memory);
3227 current = abfd->archive_head;
3229 for (i = 0; i < orl_count; i++)
3231 unsigned int hash, rehash;
3233 /* Advance firstreal to the file position of this archive
3235 if (((bfd *) map[i].pos) != last_elt)
3239 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3240 firstreal += firstreal % 2;
3241 current = current->next;
3243 while (current != (bfd *) map[i].pos);
3248 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3249 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3253 /* The desired slot is already taken. */
3254 for (srch = (hash + rehash) & (hashsize - 1);
3256 srch = (srch + rehash) & (hashsize - 1))
3257 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3260 BFD_ASSERT (srch != hash);
3265 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3266 (PTR) (hashtable + hash * 8));
3267 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3268 (PTR) (hashtable + hash * 8 + 4));
3271 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3274 bfd_release (abfd, hashtable);
3276 /* Now write the strings. */
3277 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3278 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3280 for (i = 0; i < orl_count; i++)
3284 len = strlen (*map[i].name) + 1;
3285 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3289 /* The spec sez this should be a newline. But in order to be
3290 bug-compatible for DECstation ar we use a null. */
3293 if (bfd_write ("", 1, 1, abfd) != 1)
3300 /* See whether this BFD is an archive. If it is, read in the armap
3301 and the extended name table. */
3304 ecoff_archive_p (abfd)
3307 char armag[SARMAG + 1];
3309 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3310 || strncmp (armag, ARMAG, SARMAG) != 0)
3312 bfd_set_error (bfd_error_wrong_format);
3313 return (bfd_target *) NULL;
3316 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3317 involves a cast, we can't do it as the left operand of
3319 abfd->tdata.aout_ar_data =
3320 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3322 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3324 bfd_set_error (bfd_error_no_memory);
3325 return (bfd_target *) NULL;
3328 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3329 bfd_ardata (abfd)->cache = NULL;
3330 bfd_ardata (abfd)->archive_head = NULL;
3331 bfd_ardata (abfd)->symdefs = NULL;
3332 bfd_ardata (abfd)->extended_names = NULL;
3333 bfd_ardata (abfd)->tdata = NULL;
3335 if (ecoff_slurp_armap (abfd) == false
3336 || ecoff_slurp_extended_name_table (abfd) == false)
3338 bfd_release (abfd, bfd_ardata (abfd));
3339 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3340 return (bfd_target *) NULL;
3346 /* ECOFF linker code. */
3348 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3349 PARAMS ((struct bfd_hash_entry *entry,
3350 struct bfd_hash_table *table,
3351 const char *string));
3352 static boolean ecoff_link_add_archive_symbols
3353 PARAMS ((bfd *, struct bfd_link_info *));
3354 static boolean ecoff_link_check_archive_element
3355 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3356 static boolean ecoff_link_add_object_symbols
3357 PARAMS ((bfd *, struct bfd_link_info *));
3358 static boolean ecoff_link_add_externals
3359 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3361 /* Routine to create an entry in an ECOFF link hash table. */
3363 static struct bfd_hash_entry *
3364 ecoff_link_hash_newfunc (entry, table, string)
3365 struct bfd_hash_entry *entry;
3366 struct bfd_hash_table *table;
3369 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3371 /* Allocate the structure if it has not already been allocated by a
3373 if (ret == (struct ecoff_link_hash_entry *) NULL)
3374 ret = ((struct ecoff_link_hash_entry *)
3375 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3376 if (ret == (struct ecoff_link_hash_entry *) NULL)
3378 bfd_set_error (bfd_error_no_memory);
3382 /* Call the allocation method of the superclass. */
3383 ret = ((struct ecoff_link_hash_entry *)
3384 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3389 /* Set local fields. */
3393 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3395 return (struct bfd_hash_entry *) ret;
3398 /* Create an ECOFF link hash table. */
3400 struct bfd_link_hash_table *
3401 ecoff_bfd_link_hash_table_create (abfd)
3404 struct ecoff_link_hash_table *ret;
3406 ret = ((struct ecoff_link_hash_table *)
3407 malloc (sizeof (struct ecoff_link_hash_table)));
3410 bfd_set_error (bfd_error_no_memory);
3413 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3414 ecoff_link_hash_newfunc))
3417 return (struct bfd_link_hash_table *) NULL;
3422 /* Look up an entry in an ECOFF link hash table. */
3424 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3425 ((struct ecoff_link_hash_entry *) \
3426 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3428 /* Traverse an ECOFF link hash table. */
3430 #define ecoff_link_hash_traverse(table, func, info) \
3431 (bfd_link_hash_traverse \
3433 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3436 /* Get the ECOFF link hash table from the info structure. This is
3439 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3441 /* Given an ECOFF BFD, add symbols to the global hash table as
3445 ecoff_bfd_link_add_symbols (abfd, info)
3447 struct bfd_link_info *info;
3449 switch (bfd_get_format (abfd))
3452 return ecoff_link_add_object_symbols (abfd, info);
3454 return ecoff_link_add_archive_symbols (abfd, info);
3456 bfd_set_error (bfd_error_wrong_format);
3461 /* Add the symbols from an archive file to the global hash table.
3462 This looks through the undefined symbols, looks each one up in the
3463 archive hash table, and adds any associated object file. We do not
3464 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3465 already have a hash table, so there is no reason to construct
3469 ecoff_link_add_archive_symbols (abfd, info)
3471 struct bfd_link_info *info;
3473 const bfd_byte *raw_armap;
3474 struct bfd_link_hash_entry **pundef;
3475 unsigned int armap_count;
3476 unsigned int armap_log;
3478 const bfd_byte *hashtable;
3479 const char *stringbase;
3481 if (! bfd_has_map (abfd))
3483 bfd_set_error (bfd_error_no_symbols);
3487 /* If we don't have any raw data for this archive, as can happen on
3488 Irix 4.0.5F, we call the generic routine.
3489 FIXME: We should be more clever about this, since someday tdata
3490 may get to something for a generic archive. */
3491 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3492 if (raw_armap == (bfd_byte *) NULL)
3493 return (_bfd_generic_link_add_archive_symbols
3494 (abfd, info, ecoff_link_check_archive_element));
3496 armap_count = bfd_h_get_32 (abfd, raw_armap);
3499 for (i = 1; i < armap_count; i <<= 1)
3501 BFD_ASSERT (i == armap_count);
3503 hashtable = raw_armap + 4;
3504 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3506 /* Look through the list of undefined symbols. */
3507 pundef = &info->hash->undefs;
3508 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3510 struct bfd_link_hash_entry *h;
3511 unsigned int hash, rehash;
3512 unsigned int file_offset;
3518 /* When a symbol is defined, it is not necessarily removed from
3520 if (h->type != bfd_link_hash_undefined
3521 && h->type != bfd_link_hash_common)
3523 /* Remove this entry from the list, for general cleanliness
3524 and because we are going to look through the list again
3525 if we search any more libraries. We can't remove the
3526 entry if it is the tail, because that would lose any
3527 entries we add to the list later on. */
3528 if (*pundef != info->hash->undefs_tail)
3529 *pundef = (*pundef)->next;
3531 pundef = &(*pundef)->next;
3535 /* Native ECOFF linkers do not pull in archive elements merely
3536 to satisfy common definitions, so neither do we. We leave
3537 them on the list, though, in case we are linking against some
3538 other object format. */
3539 if (h->type != bfd_link_hash_undefined)
3541 pundef = &(*pundef)->next;
3545 /* Look for this symbol in the archive hash table. */
3546 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3549 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3550 if (file_offset == 0)
3552 /* Nothing in this slot. */
3553 pundef = &(*pundef)->next;
3557 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3558 if (name[0] != h->root.string[0]
3559 || strcmp (name, h->root.string) != 0)
3564 /* That was the wrong symbol. Try rehashing. */
3566 for (srch = (hash + rehash) & (armap_count - 1);
3568 srch = (srch + rehash) & (armap_count - 1))
3570 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3571 if (file_offset == 0)
3573 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3574 if (name[0] == h->root.string[0]
3575 && strcmp (name, h->root.string) == 0)
3584 pundef = &(*pundef)->next;
3591 element = _bfd_get_elt_at_filepos (abfd, file_offset);
3592 if (element == (bfd *) NULL)
3595 if (! bfd_check_format (element, bfd_object))
3598 /* Unlike the generic linker, we know that this element provides
3599 a definition for an undefined symbol and we know that we want
3600 to include it. We don't need to check anything. */
3601 if (! (*info->callbacks->add_archive_element) (info, element, name))
3603 if (! ecoff_link_add_object_symbols (element, info))
3606 pundef = &(*pundef)->next;
3612 /* This is called if we used _bfd_generic_link_add_archive_symbols
3613 because we were not dealing with an ECOFF archive. */
3616 ecoff_link_check_archive_element (abfd, info, pneeded)
3618 struct bfd_link_info *info;
3621 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3622 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3623 = backend->debug_swap.swap_ext_in;
3625 bfd_size_type external_ext_size;
3626 PTR external_ext = NULL;
3634 if (! ecoff_slurp_symbolic_header (abfd))
3637 /* If there are no symbols, we don't want it. */
3638 if (bfd_get_symcount (abfd) == 0)
3639 goto successful_return;
3641 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3643 /* Read in the external symbols and external strings. */
3644 external_ext_size = backend->debug_swap.external_ext_size;
3645 esize = symhdr->iextMax * external_ext_size;
3646 external_ext = (PTR) malloc (esize);
3647 if (external_ext == NULL && esize != 0)
3649 bfd_set_error (bfd_error_no_memory);
3653 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3654 || bfd_read (external_ext, 1, esize, abfd) != esize)
3657 ssext = (char *) malloc (symhdr->issExtMax);
3658 if (ssext == NULL && symhdr->issExtMax != 0)
3660 bfd_set_error (bfd_error_no_memory);
3664 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3665 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
3668 /* Look through the external symbols to see if they define some
3669 symbol that is currently undefined. */
3670 ext_ptr = (char *) external_ext;
3671 ext_end = ext_ptr + esize;
3672 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3677 struct bfd_link_hash_entry *h;
3679 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3681 /* See if this symbol defines something. */
3682 if (esym.asym.st != stGlobal
3683 && esym.asym.st != stLabel
3684 && esym.asym.st != stProc)
3687 switch (esym.asym.sc)
3710 name = ssext + esym.asym.iss;
3711 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3713 /* Unlike the generic linker, we do not pull in elements because
3714 of common symbols. */
3715 if (h == (struct bfd_link_hash_entry *) NULL
3716 || h->type != bfd_link_hash_undefined)
3719 /* Include this element. */
3720 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3722 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3726 goto successful_return;
3730 if (external_ext != NULL)
3731 free (external_ext);
3736 if (external_ext != NULL)
3737 free (external_ext);
3743 /* Add symbols from an ECOFF object file to the global linker hash
3747 ecoff_link_add_object_symbols (abfd, info)
3749 struct bfd_link_info *info;
3752 bfd_size_type external_ext_size;
3753 PTR external_ext = NULL;
3758 if (! ecoff_slurp_symbolic_header (abfd))
3761 /* If there are no symbols, we don't want it. */
3762 if (bfd_get_symcount (abfd) == 0)
3765 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3767 /* Read in the external symbols and external strings. */
3768 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3769 esize = symhdr->iextMax * external_ext_size;
3770 external_ext = (PTR) malloc (esize);
3771 if (external_ext == NULL && esize != 0)
3773 bfd_set_error (bfd_error_no_memory);
3777 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3778 || bfd_read (external_ext, 1, esize, abfd) != esize)
3781 ssext = (char *) malloc (symhdr->issExtMax);
3782 if (ssext == NULL && symhdr->issExtMax != 0)
3784 bfd_set_error (bfd_error_no_memory);
3788 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3789 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
3792 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3796 if (external_ext != NULL)
3797 free (external_ext);
3803 if (external_ext != NULL)
3804 free (external_ext);
3808 /* Add the external symbols of an object file to the global linker
3809 hash table. The external symbols and strings we are passed are
3810 just allocated on the stack, and will be discarded. We must
3811 explicitly save any information we may need later on in the link.
3812 We do not want to read the external symbol information again. */
3815 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3817 struct bfd_link_info *info;
3821 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3822 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3823 = backend->debug_swap.swap_ext_in;
3824 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3825 unsigned long ext_count;
3826 struct ecoff_link_hash_entry **sym_hash;
3830 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3832 sym_hash = ((struct ecoff_link_hash_entry **)
3834 ext_count * sizeof (struct bfd_link_hash_entry *)));
3837 bfd_set_error (bfd_error_no_memory);
3840 ecoff_data (abfd)->sym_hashes = sym_hash;
3842 ext_ptr = (char *) external_ext;
3843 ext_end = ext_ptr + ext_count * external_ext_size;
3844 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3851 struct ecoff_link_hash_entry *h;
3855 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3857 /* Skip debugging symbols. */
3859 switch (esym.asym.st)
3875 /* Get the information for this symbol. */
3876 value = esym.asym.value;
3877 switch (esym.asym.sc)
3897 section = bfd_make_section_old_way (abfd, ".text");
3898 value -= section->vma;
3901 section = bfd_make_section_old_way (abfd, ".data");
3902 value -= section->vma;
3905 section = bfd_make_section_old_way (abfd, ".bss");
3906 value -= section->vma;
3909 section = &bfd_abs_section;
3912 section = &bfd_und_section;
3915 section = bfd_make_section_old_way (abfd, ".sdata");
3916 value -= section->vma;
3919 section = bfd_make_section_old_way (abfd, ".sbss");
3920 value -= section->vma;
3923 section = bfd_make_section_old_way (abfd, ".rdata");
3924 value -= section->vma;
3927 if (value > ecoff_data (abfd)->gp_size)
3929 section = &bfd_com_section;
3934 if (ecoff_scom_section.name == NULL)
3936 /* Initialize the small common section. */
3937 ecoff_scom_section.name = SCOMMON;
3938 ecoff_scom_section.flags = SEC_IS_COMMON;
3939 ecoff_scom_section.output_section = &ecoff_scom_section;
3940 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3941 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3942 ecoff_scom_symbol.name = SCOMMON;
3943 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3944 ecoff_scom_symbol.section = &ecoff_scom_section;
3945 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3947 section = &ecoff_scom_section;
3950 section = &bfd_und_section;
3953 section = bfd_make_section_old_way (abfd, ".init");
3954 value -= section->vma;
3957 section = bfd_make_section_old_way (abfd, ".fini");
3958 value -= section->vma;
3962 if (section == (asection *) NULL)
3965 name = ssext + esym.asym.iss;
3967 if (! (_bfd_generic_link_add_one_symbol
3968 (info, abfd, name, BSF_GLOBAL, section, value,
3969 (const char *) NULL, true, true,
3970 (struct bfd_link_hash_entry **) &h)))
3975 /* If we are building an ECOFF hash table, save the external
3976 symbol information. */
3977 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3979 if (h->abfd == (bfd *) NULL
3980 || (section != &bfd_und_section
3981 && (! bfd_is_com_section (section)
3982 || h->root.type != bfd_link_hash_defined)))
3993 /* ECOFF final link routines. */
3995 static boolean ecoff_final_link_debug_accumulate
3996 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
3998 static boolean ecoff_link_write_external
3999 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4000 static boolean ecoff_indirect_link_order
4001 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4002 struct bfd_link_order *));
4003 static boolean ecoff_reloc_link_order
4004 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4005 struct bfd_link_order *));
4007 /* ECOFF final link routine. This looks through all the input BFDs
4008 and gathers together all the debugging information, and then
4009 processes all the link order information. This may cause it to
4010 close and reopen some input BFDs; I'll see how bad this is. */
4013 ecoff_bfd_final_link (abfd, info)
4015 struct bfd_link_info *info;
4017 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4018 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4021 register bfd *input_bfd;
4023 struct bfd_link_order *p;
4025 /* We accumulate the debugging information counts in the symbolic
4027 symhdr = &debug->symbolic_header;
4029 symhdr->ilineMax = 0;
4033 symhdr->isymMax = 0;
4034 symhdr->ioptMax = 0;
4035 symhdr->iauxMax = 0;
4037 symhdr->issExtMax = 0;
4040 symhdr->iextMax = 0;
4042 /* We accumulate the debugging information itself in the debug_info
4045 debug->external_dnr = NULL;
4046 debug->external_pdr = NULL;
4047 debug->external_sym = NULL;
4048 debug->external_opt = NULL;
4049 debug->external_aux = NULL;
4051 debug->ssext = debug->ssext_end = NULL;
4052 debug->external_fdr = NULL;
4053 debug->external_rfd = NULL;
4054 debug->external_ext = debug->external_ext_end = NULL;
4056 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4057 if (handle == (PTR) NULL)
4060 /* Accumulate the debugging symbols from each input BFD. */
4061 for (input_bfd = info->input_bfds;
4062 input_bfd != (bfd *) NULL;
4063 input_bfd = input_bfd->link_next)
4067 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4069 /* Abitrarily set the symbolic header vstamp to the vstamp
4070 of the first object file in the link. */
4071 if (symhdr->vstamp == 0)
4073 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4074 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4078 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4079 debug, &backend->debug_swap,
4084 /* Combine the register masks. */
4085 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4086 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4087 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4088 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4089 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4090 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4093 /* Write out the external symbols. */
4094 ecoff_link_hash_traverse (ecoff_hash_table (info),
4095 ecoff_link_write_external,
4098 if (info->relocateable)
4100 /* We need to make a pass over the link_orders to count up the
4101 number of relocations we will need to output, so that we know
4102 how much space they will take up. */
4103 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4106 for (p = o->link_order_head;
4107 p != (struct bfd_link_order *) NULL;
4109 if (p->type == bfd_indirect_link_order)
4110 o->reloc_count += p->u.indirect.section->reloc_count;
4111 else if (p->type == bfd_section_reloc_link_order
4112 || p->type == bfd_symbol_reloc_link_order)
4117 /* Compute the reloc and symbol file positions. */
4118 ecoff_compute_reloc_file_positions (abfd);
4120 /* Write out the debugging information. */
4121 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4122 &backend->debug_swap, info,
4123 ecoff_data (abfd)->sym_filepos))
4126 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4128 if (info->relocateable)
4130 /* Now reset the reloc_count field of the sections in the output
4131 BFD to 0, so that we can use them to keep track of how many
4132 relocs we have output thus far. */
4133 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4137 /* Get a value for the GP register. */
4138 if (ecoff_data (abfd)->gp == 0)
4140 struct bfd_link_hash_entry *h;
4142 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4143 if (h != (struct bfd_link_hash_entry *) NULL
4144 && h->type == bfd_link_hash_defined)
4145 ecoff_data (abfd)->gp = (h->u.def.value
4146 + h->u.def.section->output_section->vma
4147 + h->u.def.section->output_offset);
4148 else if (info->relocateable)
4152 /* Make up a value. */
4154 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4157 && (strcmp (o->name, _SBSS) == 0
4158 || strcmp (o->name, _SDATA) == 0
4159 || strcmp (o->name, _LIT4) == 0
4160 || strcmp (o->name, _LIT8) == 0
4161 || strcmp (o->name, _LITA) == 0))
4164 ecoff_data (abfd)->gp = lo + 0x8000;
4168 /* If the relocate_section function needs to do a reloc
4169 involving the GP value, it should make a reloc_dangerous
4170 callback to warn that GP is not defined. */
4174 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4176 /* Ignore any link_orders for the .reginfo section, which does
4177 not really exist. */
4178 if (strcmp (o->name, REGINFO) == 0)
4181 for (p = o->link_order_head;
4182 p != (struct bfd_link_order *) NULL;
4185 if (p->type == bfd_indirect_link_order
4186 && (bfd_get_flavour (p->u.indirect.section->owner)
4187 == bfd_target_ecoff_flavour))
4189 if (! ecoff_indirect_link_order (abfd, info, o, p))
4192 else if (p->type == bfd_section_reloc_link_order
4193 || p->type == bfd_symbol_reloc_link_order)
4195 if (! ecoff_reloc_link_order (abfd, info, o, p))
4200 if (! _bfd_default_link_order (abfd, info, o, p))
4206 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4211 /* Accumulate the debugging information for an input BFD into the
4212 output BFD. This must read in the symbolic information of the
4216 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4219 struct bfd_link_info *info;
4222 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4223 const struct ecoff_debug_swap * const swap =
4224 &ecoff_backend (input_bfd)->debug_swap;
4225 HDRR *symhdr = &debug->symbolic_header;
4228 #define READ(ptr, offset, count, size, type) \
4229 if (symhdr->count == 0) \
4230 debug->ptr = NULL; \
4233 debug->ptr = (type) malloc (size * symhdr->count); \
4234 if (debug->ptr == NULL) \
4236 bfd_set_error (bfd_error_no_memory); \
4238 goto return_something; \
4240 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4242 || (bfd_read (debug->ptr, size, symhdr->count, \
4243 input_bfd) != size * symhdr->count)) \
4246 goto return_something; \
4250 /* If raw_syments is not NULL, then the data was already by read by
4251 ecoff_slurp_symbolic_info. */
4252 if (ecoff_data (input_bfd)->raw_syments == NULL)
4254 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4256 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4257 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4258 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4259 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4260 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4262 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4263 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4264 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4268 /* We do not read the external strings or the external symbols. */
4270 ret = (bfd_ecoff_debug_accumulate
4271 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4272 &ecoff_backend (output_bfd)->debug_swap,
4273 input_bfd, debug, swap, info));
4276 if (ecoff_data (input_bfd)->raw_syments == NULL)
4278 if (debug->line != NULL)
4280 if (debug->external_dnr != NULL)
4281 free (debug->external_dnr);
4282 if (debug->external_pdr != NULL)
4283 free (debug->external_pdr);
4284 if (debug->external_sym != NULL)
4285 free (debug->external_sym);
4286 if (debug->external_opt != NULL)
4287 free (debug->external_opt);
4288 if (debug->external_aux != NULL)
4289 free (debug->external_aux);
4290 if (debug->ss != NULL)
4292 if (debug->external_fdr != NULL)
4293 free (debug->external_fdr);
4294 if (debug->external_rfd != NULL)
4295 free (debug->external_rfd);
4297 /* Make sure we don't accidentally follow one of these pointers
4298 into freed memory. */
4300 debug->external_dnr = NULL;
4301 debug->external_pdr = NULL;
4302 debug->external_sym = NULL;
4303 debug->external_opt = NULL;
4304 debug->external_aux = NULL;
4306 debug->external_fdr = NULL;
4307 debug->external_rfd = NULL;
4313 /* Put out information for an external symbol. These come only from
4317 ecoff_link_write_external (h, data)
4318 struct ecoff_link_hash_entry *h;
4321 bfd *output_bfd = (bfd *) data;
4323 /* FIXME: We should check if this symbol is being stripped. */
4325 if (h->root.written)
4328 if (h->abfd == (bfd *) NULL)
4331 h->esym.cobol_main = 0;
4332 h->esym.weakext = 0;
4333 h->esym.reserved = 0;
4334 h->esym.ifd = ifdNil;
4335 h->esym.asym.value = 0;
4336 h->esym.asym.st = stGlobal;
4338 if (h->root.type != bfd_link_hash_defined)
4339 h->esym.asym.sc = scAbs;
4342 asection *output_section;
4345 output_section = h->root.u.def.section->output_section;
4346 name = bfd_section_name (output_section->owner, output_section);
4348 if (strcmp (name, _TEXT) == 0)
4349 h->esym.asym.sc = scText;
4350 else if (strcmp (name, _DATA) == 0)
4351 h->esym.asym.sc = scData;
4352 else if (strcmp (name, _SDATA) == 0)
4353 h->esym.asym.sc = scSData;
4354 else if (strcmp (name, _RDATA) == 0)
4355 h->esym.asym.sc = scRData;
4356 else if (strcmp (name, _BSS) == 0)
4357 h->esym.asym.sc = scBss;
4358 else if (strcmp (name, _SBSS) == 0)
4359 h->esym.asym.sc = scSBss;
4360 else if (strcmp (name, _INIT) == 0)
4361 h->esym.asym.sc = scInit;
4362 else if (strcmp (name, _FINI) == 0)
4363 h->esym.asym.sc = scFini;
4364 else if (strcmp (name, _PDATA) == 0)
4365 h->esym.asym.sc = scPData;
4366 else if (strcmp (name, _XDATA) == 0)
4367 h->esym.asym.sc = scXData;
4369 h->esym.asym.sc = scAbs;
4372 h->esym.asym.reserved = 0;
4373 h->esym.asym.index = indexNil;
4375 else if (h->esym.ifd != -1)
4377 struct ecoff_debug_info *debug;
4379 /* Adjust the FDR index for the symbol by that used for the
4381 debug = &ecoff_data (h->abfd)->debug_info;
4382 BFD_ASSERT (h->esym.ifd >= 0
4383 && h->esym.ifd < debug->symbolic_header.ifdMax);
4384 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4387 switch (h->root.type)
4390 case bfd_link_hash_new:
4392 case bfd_link_hash_undefined:
4393 case bfd_link_hash_weak:
4394 if (h->esym.asym.sc != scUndefined
4395 && h->esym.asym.sc != scSUndefined)
4396 h->esym.asym.sc = scUndefined;
4398 case bfd_link_hash_defined:
4399 if (h->esym.asym.sc == scUndefined
4400 || h->esym.asym.sc == scSUndefined)
4401 h->esym.asym.sc = scAbs;
4402 else if (h->esym.asym.sc == scCommon)
4403 h->esym.asym.sc = scBss;
4404 else if (h->esym.asym.sc == scSCommon)
4405 h->esym.asym.sc = scSBss;
4406 h->esym.asym.value = (h->root.u.def.value
4407 + h->root.u.def.section->output_section->vma
4408 + h->root.u.def.section->output_offset);
4410 case bfd_link_hash_common:
4411 if (h->esym.asym.sc != scCommon
4412 && h->esym.asym.sc != scSCommon)
4413 h->esym.asym.sc = scCommon;
4414 h->esym.asym.value = h->root.u.c.size;
4416 case bfd_link_hash_indirect:
4417 case bfd_link_hash_warning:
4418 /* FIXME: Ignore these for now. The circumstances under which
4419 they should be written out are not clear to me. */
4423 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4425 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4426 h->root.written = true;
4428 return (bfd_ecoff_debug_one_external
4429 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4430 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4434 /* Relocate and write an ECOFF section into an ECOFF output file. */
4437 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4439 struct bfd_link_info *info;
4440 asection *output_section;
4441 struct bfd_link_order *link_order;
4443 asection *input_section;
4445 struct ecoff_section_tdata *section_tdata;
4446 bfd_size_type raw_size;
4447 bfd_size_type cooked_size;
4448 bfd_byte *contents = NULL;
4449 bfd_size_type external_reloc_size;
4450 bfd_size_type external_relocs_size;
4451 PTR external_relocs = NULL;
4453 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4455 if (link_order->size == 0)
4458 input_section = link_order->u.indirect.section;
4459 input_bfd = input_section->owner;
4460 section_tdata = ecoff_section_data (input_bfd, input_section);
4462 raw_size = input_section->_raw_size;
4463 cooked_size = input_section->_cooked_size;
4464 if (cooked_size == 0)
4465 cooked_size = raw_size;
4467 BFD_ASSERT (input_section->output_section == output_section);
4468 BFD_ASSERT (input_section->output_offset == link_order->offset);
4469 BFD_ASSERT (cooked_size == link_order->size);
4471 /* Get the section contents. We allocate memory for the larger of
4472 the size before relocating and the size after relocating. */
4473 contents = (bfd_byte *) malloc (raw_size >= cooked_size
4476 if (contents == NULL && raw_size != 0)
4478 bfd_set_error (bfd_error_no_memory);
4482 /* If we are relaxing, the contents may have already been read into
4483 memory, in which case we copy them into our new buffer. We don't
4484 simply reuse the old buffer in case cooked_size > raw_size. */
4485 if (section_tdata != (struct ecoff_section_tdata *) NULL
4486 && section_tdata->contents != (bfd_byte *) NULL)
4487 memcpy (contents, section_tdata->contents, raw_size);
4490 if (! bfd_get_section_contents (input_bfd, input_section,
4492 (file_ptr) 0, raw_size))
4496 /* Get the relocs. If we are relaxing MIPS code, they will already
4497 have been read in. Otherwise, we read them in now. */
4498 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4499 external_relocs_size = external_reloc_size * input_section->reloc_count;
4501 if (section_tdata != (struct ecoff_section_tdata *) NULL)
4502 external_relocs = section_tdata->external_relocs;
4505 external_relocs = (PTR) malloc (external_relocs_size);
4506 if (external_relocs == NULL && external_relocs_size != 0)
4508 bfd_set_error (bfd_error_no_memory);
4512 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4513 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4514 != external_relocs_size))
4518 /* Relocate the section contents. */
4519 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4520 (output_bfd, info, input_bfd, input_section, contents,
4524 /* Write out the relocated section. */
4525 if (! bfd_set_section_contents (output_bfd,
4528 input_section->output_offset,
4532 /* If we are producing relocateable output, the relocs were
4533 modified, and we write them out now. We use the reloc_count
4534 field of output_section to keep track of the number of relocs we
4535 have output so far. */
4536 if (info->relocateable)
4538 if (bfd_seek (output_bfd,
4539 (output_section->rel_filepos +
4540 output_section->reloc_count * external_reloc_size),
4542 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4543 != external_relocs_size))
4545 output_section->reloc_count += input_section->reloc_count;
4548 if (contents != NULL)
4550 if (external_relocs != NULL && section_tdata == NULL)
4551 free (external_relocs);
4555 if (contents != NULL)
4557 if (external_relocs != NULL && section_tdata == NULL)
4558 free (external_relocs);
4562 /* Generate a reloc when linking an ECOFF file. This is a reloc
4563 requested by the linker, and does come from any input file. This
4564 is used to build constructor and destructor tables when linking
4568 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4570 struct bfd_link_info *info;
4571 asection *output_section;
4572 struct bfd_link_order *link_order;
4575 struct internal_reloc in;
4576 bfd_size_type external_reloc_size;
4580 /* We set up an arelent to pass to the backend adjust_reloc_out
4582 rel.address = link_order->offset;
4584 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4585 if (rel.howto == (const reloc_howto_type *) NULL)
4587 bfd_set_error (bfd_error_bad_value);
4591 if (link_order->type == bfd_section_reloc_link_order)
4592 rel.sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
4595 /* We can't set up a reloc against a symbol correctly, because
4596 we have no asymbol structure. Currently no adjust_reloc_out
4598 rel.sym_ptr_ptr = (asymbol **) NULL;
4601 /* All ECOFF relocs are in-place. Put the addend into the object
4604 BFD_ASSERT (rel.howto->partial_inplace);
4605 if (link_order->u.reloc.p->addend != 0)
4608 bfd_reloc_status_type rstat;
4612 size = bfd_get_reloc_size (rel.howto);
4613 buf = (bfd_byte *) bfd_zmalloc (size);
4614 if (buf == (bfd_byte *) NULL)
4616 bfd_set_error (bfd_error_no_memory);
4619 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4620 link_order->u.reloc.p->addend, buf);
4626 case bfd_reloc_outofrange:
4628 case bfd_reloc_overflow:
4629 if (! ((*info->callbacks->reloc_overflow)
4631 (link_order->type == bfd_section_reloc_link_order
4632 ? bfd_section_name (output_bfd,
4633 link_order->u.reloc.p->u.section)
4634 : link_order->u.reloc.p->u.name),
4635 rel.howto->name, link_order->u.reloc.p->addend,
4636 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
4643 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4644 (file_ptr) link_order->offset, size);
4652 /* Move the information into a internal_reloc structure. */
4653 in.r_vaddr = (rel.address
4654 + bfd_get_section_vma (output_bfd, output_section));
4655 in.r_type = rel.howto->type;
4657 if (link_order->type == bfd_symbol_reloc_link_order)
4659 struct ecoff_link_hash_entry *h;
4661 h = ecoff_link_hash_lookup (ecoff_hash_table (info),
4662 link_order->u.reloc.p->u.name,
4663 false, false, true);
4664 if (h != (struct ecoff_link_hash_entry *) NULL
4666 in.r_symndx = h->indx;
4669 if (! ((*info->callbacks->unattached_reloc)
4670 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4671 (asection *) NULL, (bfd_vma) 0)))
4681 name = bfd_get_section_name (output_bfd,
4682 link_order->u.reloc.p->u.section);
4683 if (strcmp (name, ".text") == 0)
4684 in.r_symndx = RELOC_SECTION_TEXT;
4685 else if (strcmp (name, ".rdata") == 0)
4686 in.r_symndx = RELOC_SECTION_RDATA;
4687 else if (strcmp (name, ".data") == 0)
4688 in.r_symndx = RELOC_SECTION_DATA;
4689 else if (strcmp (name, ".sdata") == 0)
4690 in.r_symndx = RELOC_SECTION_SDATA;
4691 else if (strcmp (name, ".sbss") == 0)
4692 in.r_symndx = RELOC_SECTION_SBSS;
4693 else if (strcmp (name, ".bss") == 0)
4694 in.r_symndx = RELOC_SECTION_BSS;
4695 else if (strcmp (name, ".init") == 0)
4696 in.r_symndx = RELOC_SECTION_INIT;
4697 else if (strcmp (name, ".lit8") == 0)
4698 in.r_symndx = RELOC_SECTION_LIT8;
4699 else if (strcmp (name, ".lit4") == 0)
4700 in.r_symndx = RELOC_SECTION_LIT4;
4701 else if (strcmp (name, ".xdata") == 0)
4702 in.r_symndx = RELOC_SECTION_XDATA;
4703 else if (strcmp (name, ".pdata") == 0)
4704 in.r_symndx = RELOC_SECTION_PDATA;
4705 else if (strcmp (name, ".fini") == 0)
4706 in.r_symndx = RELOC_SECTION_FINI;
4707 else if (strcmp (name, ".lita") == 0)
4708 in.r_symndx = RELOC_SECTION_LITA;
4709 else if (strcmp (name, "*ABS*") == 0)
4710 in.r_symndx = RELOC_SECTION_ABS;
4716 /* Let the BFD backend adjust the reloc. */
4717 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4719 /* Get some memory and swap out the reloc. */
4720 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4721 rbuf = (bfd_byte *) malloc (external_reloc_size);
4722 if (rbuf == (bfd_byte *) NULL)
4724 bfd_set_error (bfd_error_no_memory);
4728 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4730 ok = (bfd_seek (output_bfd,
4731 (output_section->rel_filepos +
4732 output_section->reloc_count * external_reloc_size),
4734 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4735 == external_reloc_size));
4738 ++output_section->reloc_count;