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))
376 if (sec_flags & SEC_NEVER_LOAD)
377 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
379 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
380 if (styp_flags & STYP_RDATA)
381 sec_flags |= SEC_READONLY;
383 else if ((styp_flags & STYP_BSS)
384 || (styp_flags & STYP_SBSS))
386 sec_flags |= SEC_ALLOC;
388 else if (styp_flags & STYP_INFO)
390 sec_flags |= SEC_NEVER_LOAD;
392 else if ((styp_flags & STYP_LITA)
393 || (styp_flags & STYP_LIT8)
394 || (styp_flags & STYP_LIT4))
396 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
400 sec_flags |= SEC_ALLOC | SEC_LOAD;
406 /* Routines to swap auxiliary information in and out. I am assuming
407 that the auxiliary information format is always going to be target
410 /* Swap in a type information record.
411 BIGEND says whether AUX symbols are big-endian or little-endian; this
412 info comes from the file header record (fh-fBigendian). */
415 ecoff_swap_tir_in (bigend, ext_copy, intern)
417 struct tir_ext *ext_copy;
420 struct tir_ext ext[1];
422 *ext = *ext_copy; /* Make it reasonable to do in-place. */
424 /* now the fun stuff... */
426 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
427 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
428 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
429 >> TIR_BITS1_BT_SH_BIG;
430 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
431 >> TIR_BITS_TQ4_SH_BIG;
432 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
433 >> TIR_BITS_TQ5_SH_BIG;
434 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
435 >> TIR_BITS_TQ0_SH_BIG;
436 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
437 >> TIR_BITS_TQ1_SH_BIG;
438 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
439 >> TIR_BITS_TQ2_SH_BIG;
440 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
441 >> TIR_BITS_TQ3_SH_BIG;
443 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
444 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
445 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
446 >> TIR_BITS1_BT_SH_LITTLE;
447 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
448 >> TIR_BITS_TQ4_SH_LITTLE;
449 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
450 >> TIR_BITS_TQ5_SH_LITTLE;
451 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
452 >> TIR_BITS_TQ0_SH_LITTLE;
453 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
454 >> TIR_BITS_TQ1_SH_LITTLE;
455 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
456 >> TIR_BITS_TQ2_SH_LITTLE;
457 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
458 >> TIR_BITS_TQ3_SH_LITTLE;
462 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
467 /* Swap out a type information record.
468 BIGEND says whether AUX symbols are big-endian or little-endian; this
469 info comes from the file header record (fh-fBigendian). */
472 ecoff_swap_tir_out (bigend, intern_copy, ext)
479 *intern = *intern_copy; /* Make it reasonable to do in-place. */
481 /* now the fun stuff... */
483 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
484 | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
485 | ((intern->bt << TIR_BITS1_BT_SH_BIG)
486 & TIR_BITS1_BT_BIG));
487 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
489 | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
490 & TIR_BITS_TQ5_BIG));
491 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
493 | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
494 & TIR_BITS_TQ1_BIG));
495 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
497 | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
498 & TIR_BITS_TQ3_BIG));
500 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
501 | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
502 | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
503 & TIR_BITS1_BT_LITTLE));
504 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
505 & TIR_BITS_TQ4_LITTLE)
506 | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
507 & TIR_BITS_TQ5_LITTLE));
508 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
509 & TIR_BITS_TQ0_LITTLE)
510 | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
511 & TIR_BITS_TQ1_LITTLE));
512 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
513 & TIR_BITS_TQ2_LITTLE)
514 | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
515 & TIR_BITS_TQ3_LITTLE));
519 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
524 /* Swap in a relative symbol record. BIGEND says whether it is in
525 big-endian or little-endian format.*/
528 ecoff_swap_rndx_in (bigend, ext_copy, intern)
530 struct rndx_ext *ext_copy;
533 struct rndx_ext ext[1];
535 *ext = *ext_copy; /* Make it reasonable to do in-place. */
537 /* now the fun stuff... */
539 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
540 | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
541 >> RNDX_BITS1_RFD_SH_BIG);
542 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
543 << RNDX_BITS1_INDEX_SH_LEFT_BIG)
544 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
545 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
547 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
548 | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
549 << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
550 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
551 >> RNDX_BITS1_INDEX_SH_LITTLE)
552 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
553 | ((unsigned int) ext->r_bits[3]
554 << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
558 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
563 /* Swap out a relative symbol record. BIGEND says whether it is in
564 big-endian or little-endian format.*/
567 ecoff_swap_rndx_out (bigend, intern_copy, ext)
570 struct rndx_ext *ext;
574 *intern = *intern_copy; /* Make it reasonable to do in-place. */
576 /* now the fun stuff... */
578 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
579 ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
580 & RNDX_BITS1_RFD_BIG)
581 | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
582 & RNDX_BITS1_INDEX_BIG));
583 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
584 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
586 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
587 ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
588 & RNDX_BITS1_RFD_LITTLE)
589 | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
590 & RNDX_BITS1_INDEX_LITTLE));
591 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
592 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
596 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
601 /* Read in the symbolic header for an ECOFF object file. */
604 ecoff_slurp_symbolic_header (abfd)
607 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
608 bfd_size_type external_hdr_size;
610 HDRR *internal_symhdr;
612 /* See if we've already read it in. */
613 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
614 backend->debug_swap.sym_magic)
617 /* See whether there is a symbolic header. */
618 if (ecoff_data (abfd)->sym_filepos == 0)
620 bfd_get_symcount (abfd) = 0;
624 /* At this point bfd_get_symcount (abfd) holds the number of symbols
625 as read from the file header, but on ECOFF this is always the
626 size of the symbolic information header. It would be cleaner to
627 handle this when we first read the file in coffgen.c. */
628 external_hdr_size = backend->debug_swap.external_hdr_size;
629 if (bfd_get_symcount (abfd) != external_hdr_size)
631 bfd_set_error (bfd_error_bad_value);
635 /* Read the symbolic information header. */
636 raw = (PTR) malloc ((size_t) external_hdr_size);
639 bfd_set_error (bfd_error_no_memory);
643 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
644 || (bfd_read (raw, external_hdr_size, 1, abfd)
645 != external_hdr_size))
647 bfd_set_error (bfd_error_system_call);
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_set_error (bfd_error_system_call);
756 bfd_release (abfd, raw);
760 ecoff_data (abfd)->raw_syments = raw;
762 /* Get pointers for the numeric offsets in the HDRR structure. */
763 #define FIX(off1, off2, type) \
764 if (internal_symhdr->off1 == 0) \
765 ecoff_data (abfd)->debug_info.off2 = (type) NULL; \
767 ecoff_data (abfd)->debug_info.off2 = (type) ((char *) raw \
768 + internal_symhdr->off1 \
770 FIX (cbLineOffset, line, unsigned char *);
771 FIX (cbDnOffset, external_dnr, PTR);
772 FIX (cbPdOffset, external_pdr, PTR);
773 FIX (cbSymOffset, external_sym, PTR);
774 FIX (cbOptOffset, external_opt, PTR);
775 FIX (cbAuxOffset, external_aux, union aux_ext *);
776 FIX (cbSsOffset, ss, char *);
777 FIX (cbSsExtOffset, ssext, char *);
778 FIX (cbFdOffset, external_fdr, PTR);
779 FIX (cbRfdOffset, external_rfd, PTR);
780 FIX (cbExtOffset, external_ext, PTR);
783 /* I don't want to always swap all the data, because it will just
784 waste time and most programs will never look at it. The only
785 time the linker needs most of the debugging information swapped
786 is when linking big-endian and little-endian MIPS object files
787 together, which is not a common occurrence.
789 We need to look at the fdr to deal with a lot of information in
790 the symbols, so we swap them here. */
791 ecoff_data (abfd)->debug_info.fdr =
792 (struct fdr *) bfd_alloc (abfd,
793 (internal_symhdr->ifdMax *
794 sizeof (struct fdr)));
795 if (ecoff_data (abfd)->debug_info.fdr == NULL)
797 bfd_set_error (bfd_error_no_memory);
800 external_fdr_size = backend->debug_swap.external_fdr_size;
801 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
802 fraw_src = (char *) ecoff_data (abfd)->debug_info.external_fdr;
803 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
804 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
805 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
810 /* ECOFF symbol table routines. The ECOFF symbol table is described
811 in gcc/mips-tfile.c. */
813 /* ECOFF uses two common sections. One is the usual one, and the
814 other is for small objects. All the small objects are kept
815 together, and then referenced via the gp pointer, which yields
816 faster assembler code. This is what we use for the small common
818 static asection ecoff_scom_section;
819 static asymbol ecoff_scom_symbol;
820 static asymbol *ecoff_scom_symbol_ptr;
822 /* Create an empty symbol. */
825 ecoff_make_empty_symbol (abfd)
828 ecoff_symbol_type *new;
830 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
831 if (new == (ecoff_symbol_type *) NULL)
833 bfd_set_error (bfd_error_no_memory);
834 return (asymbol *) NULL;
836 memset ((PTR) new, 0, sizeof *new);
837 new->symbol.section = (asection *) NULL;
838 new->fdr = (FDR *) NULL;
841 new->symbol.the_bfd = abfd;
845 /* Set the BFD flags and section for an ECOFF symbol. */
848 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, indirect_ptr_ptr)
853 asymbol **indirect_ptr_ptr;
855 asym->the_bfd = abfd;
856 asym->value = ecoff_sym->value;
857 asym->section = &bfd_debug_section;
860 /* An indirect symbol requires two consecutive stabs symbols. */
861 if (*indirect_ptr_ptr != (asymbol *) NULL)
863 BFD_ASSERT (ECOFF_IS_STAB (ecoff_sym));
865 /* @@ Stuffing pointers into integers is a no-no.
866 We can usually get away with it if the integer is
867 large enough though. */
868 if (sizeof (asym) > sizeof (bfd_vma))
870 (*indirect_ptr_ptr)->value = (bfd_vma) asym;
872 asym->flags = BSF_DEBUGGING;
873 asym->section = &bfd_und_section;
874 *indirect_ptr_ptr = NULL;
878 if (ECOFF_IS_STAB (ecoff_sym)
879 && (ECOFF_UNMARK_STAB (ecoff_sym->index) | N_EXT) == (N_INDR | N_EXT))
881 asym->flags = BSF_DEBUGGING | BSF_INDIRECT;
882 asym->section = &bfd_ind_section;
883 /* Pass this symbol on to the next call to this function. */
884 *indirect_ptr_ptr = asym;
888 /* Most symbol types are just for debugging. */
889 switch (ecoff_sym->st)
898 if (ECOFF_IS_STAB (ecoff_sym))
900 asym->flags = BSF_DEBUGGING;
905 asym->flags = BSF_DEBUGGING;
910 asym->flags = BSF_EXPORT | BSF_GLOBAL;
912 asym->flags = BSF_LOCAL;
913 switch (ecoff_sym->sc)
916 /* Used for compiler generated labels. Leave them in the
917 debugging section, and mark them as local. If BSF_DEBUGGING
918 is set, then nm does not display them for some reason. If no
919 flags are set then the linker whines about them. */
920 asym->flags = BSF_LOCAL;
923 asym->section = bfd_make_section_old_way (abfd, ".text");
924 asym->value -= asym->section->vma;
927 asym->section = bfd_make_section_old_way (abfd, ".data");
928 asym->value -= asym->section->vma;
931 asym->section = bfd_make_section_old_way (abfd, ".bss");
932 asym->value -= asym->section->vma;
935 asym->flags = BSF_DEBUGGING;
938 asym->section = &bfd_abs_section;
941 asym->section = &bfd_und_section;
951 asym->flags = BSF_DEBUGGING;
954 asym->section = bfd_make_section_old_way (abfd, ".sdata");
955 asym->value -= asym->section->vma;
958 asym->section = bfd_make_section_old_way (abfd, ".sbss");
959 asym->value -= asym->section->vma;
962 asym->section = bfd_make_section_old_way (abfd, ".rdata");
963 asym->value -= asym->section->vma;
966 asym->flags = BSF_DEBUGGING;
969 if (asym->value > ecoff_data (abfd)->gp_size)
971 asym->section = &bfd_com_section;
977 if (ecoff_scom_section.name == NULL)
979 /* Initialize the small common section. */
980 ecoff_scom_section.name = SCOMMON;
981 ecoff_scom_section.flags = SEC_IS_COMMON;
982 ecoff_scom_section.output_section = &ecoff_scom_section;
983 ecoff_scom_section.symbol = &ecoff_scom_symbol;
984 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
985 ecoff_scom_symbol.name = SCOMMON;
986 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
987 ecoff_scom_symbol.section = &ecoff_scom_section;
988 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
990 asym->section = &ecoff_scom_section;
995 asym->flags = BSF_DEBUGGING;
998 asym->section = &bfd_und_section;
1003 asym->section = bfd_make_section_old_way (abfd, ".init");
1004 asym->value -= asym->section->vma;
1009 asym->flags = BSF_DEBUGGING;
1012 asym->section = bfd_make_section_old_way (abfd, ".fini");
1013 asym->value -= asym->section->vma;
1019 /* Look for special constructors symbols and make relocation entries
1020 in a special construction section. These are produced by the
1021 -fgnu-linker argument to g++. */
1022 if (ECOFF_IS_STAB (ecoff_sym))
1024 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
1036 arelent_chain *reloc_chain;
1037 unsigned int bitsize;
1039 /* Get a section with the same name as the symbol (usually
1040 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
1041 name ___CTOR_LIST (three underscores). We need
1042 __CTOR_LIST (two underscores), since ECOFF doesn't use
1043 a leading underscore. This should be handled by gcc,
1044 but instead we do it here. Actually, this should all
1045 be done differently anyhow. */
1046 name = bfd_asymbol_name (asym);
1047 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
1052 section = bfd_get_section_by_name (abfd, name);
1053 if (section == (asection *) NULL)
1057 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
1060 bfd_set_error (bfd_error_no_memory);
1063 strcpy (copy, name);
1064 section = bfd_make_section (abfd, copy);
1067 /* Build a reloc pointing to this constructor. */
1069 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1072 bfd_set_error (bfd_error_no_memory);
1075 reloc_chain->relent.sym_ptr_ptr =
1076 bfd_get_section (asym)->symbol_ptr_ptr;
1077 reloc_chain->relent.address = section->_raw_size;
1078 reloc_chain->relent.addend = asym->value;
1079 reloc_chain->relent.howto =
1080 ecoff_backend (abfd)->constructor_reloc;
1082 /* Set up the constructor section to hold the reloc. */
1083 section->flags = SEC_CONSTRUCTOR;
1084 ++section->reloc_count;
1086 /* Constructor sections must be rounded to a boundary
1087 based on the bitsize. These are not real sections--
1088 they are handled specially by the linker--so the ECOFF
1089 16 byte alignment restriction does not apply. */
1090 bitsize = ecoff_backend (abfd)->constructor_bitsize;
1091 section->alignment_power = 1;
1092 while ((1 << section->alignment_power) < bitsize / 8)
1093 ++section->alignment_power;
1095 reloc_chain->next = section->constructor_chain;
1096 section->constructor_chain = reloc_chain;
1097 section->_raw_size += bitsize / 8;
1099 /* Mark the symbol as a constructor. */
1100 asym->flags |= BSF_CONSTRUCTOR;
1108 /* Read an ECOFF symbol table. */
1111 ecoff_slurp_symbol_table (abfd)
1114 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1115 const bfd_size_type external_ext_size
1116 = backend->debug_swap.external_ext_size;
1117 const bfd_size_type external_sym_size
1118 = backend->debug_swap.external_sym_size;
1119 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
1120 = backend->debug_swap.swap_ext_in;
1121 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
1122 = backend->debug_swap.swap_sym_in;
1123 bfd_size_type internal_size;
1124 ecoff_symbol_type *internal;
1125 ecoff_symbol_type *internal_ptr;
1126 asymbol *indirect_ptr;
1132 /* If we've already read in the symbol table, do nothing. */
1133 if (ecoff_data (abfd)->canonical_symbols != NULL)
1136 /* Get the symbolic information. */
1137 if (ecoff_slurp_symbolic_info (abfd) == false)
1139 if (bfd_get_symcount (abfd) == 0)
1142 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1143 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1144 if (internal == NULL)
1146 bfd_set_error (bfd_error_no_memory);
1150 internal_ptr = internal;
1151 indirect_ptr = NULL;
1152 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
1153 eraw_end = (eraw_src
1154 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
1155 * external_ext_size));
1156 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
1160 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
1161 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
1162 + internal_esym.asym.iss);
1163 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
1164 &internal_ptr->symbol, 1, &indirect_ptr))
1166 /* The alpha uses a negative ifd field for section symbols. */
1167 if (internal_esym.ifd >= 0)
1168 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
1169 + internal_esym.ifd);
1171 internal_ptr->fdr = NULL;
1172 internal_ptr->local = false;
1173 internal_ptr->native = (PTR) eraw_src;
1175 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1177 /* The local symbols must be accessed via the fdr's, because the
1178 string and aux indices are relative to the fdr information. */
1179 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
1180 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
1181 for (; fdr_ptr < fdr_end; fdr_ptr++)
1186 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
1187 + fdr_ptr->isymBase * external_sym_size);
1188 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1190 lraw_src < lraw_end;
1191 lraw_src += external_sym_size, internal_ptr++)
1195 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1196 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
1198 + internal_sym.iss);
1199 if (!ecoff_set_symbol_info (abfd, &internal_sym,
1200 &internal_ptr->symbol, 0, &indirect_ptr))
1202 internal_ptr->fdr = fdr_ptr;
1203 internal_ptr->local = true;
1204 internal_ptr->native = (PTR) lraw_src;
1207 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1209 ecoff_data (abfd)->canonical_symbols = internal;
1214 /* Return the amount of space needed for the canonical symbols. */
1217 ecoff_get_symtab_upper_bound (abfd)
1220 if (ecoff_slurp_symbolic_info (abfd) == false
1221 || bfd_get_symcount (abfd) == 0)
1224 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1227 /* Get the canonical symbols. */
1230 ecoff_get_symtab (abfd, alocation)
1232 asymbol **alocation;
1234 unsigned int counter = 0;
1235 ecoff_symbol_type *symbase;
1236 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1238 if (ecoff_slurp_symbol_table (abfd) == false
1239 || bfd_get_symcount (abfd) == 0)
1242 symbase = ecoff_data (abfd)->canonical_symbols;
1243 while (counter < bfd_get_symcount (abfd))
1245 *(location++) = symbase++;
1248 *location++ = (ecoff_symbol_type *) NULL;
1249 return bfd_get_symcount (abfd);
1252 /* Turn ECOFF type information into a printable string.
1253 ecoff_emit_aggregate and ecoff_type_to_string are from
1254 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1256 /* Write aggregate information to a string. */
1259 ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1266 int ifd = rndx->rfd;
1267 int indx = rndx->index;
1268 int sym_base, ss_base;
1274 sym_base = ecoff_data (abfd)->debug_info.fdr[ifd].isymBase;
1275 ss_base = ecoff_data (abfd)->debug_info.fdr[ifd].issBase;
1277 if (indx == indexNil)
1278 name = "/* no name */";
1281 const struct ecoff_debug_swap * const debug_swap
1282 = &ecoff_backend (abfd)->debug_swap;
1286 (*debug_swap->swap_sym_in)
1288 ((char *) ecoff_data (abfd)->debug_info.external_sym
1289 + indx * debug_swap->external_sym_size),
1291 name = ecoff_data (abfd)->debug_info.ss + ss_base + sym.iss;
1295 "%s %s { ifd = %d, index = %ld }",
1298 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax));
1301 /* Convert the type information to string format. */
1304 ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1306 union aux_ext *aux_ptr;
1318 unsigned int basic_type;
1320 static char buffer1[1024];
1321 static char buffer2[1024];
1326 for (i = 0; i < 7; i++)
1328 qualifiers[i].low_bound = 0;
1329 qualifiers[i].high_bound = 0;
1330 qualifiers[i].stride = 0;
1333 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1334 return "-1 (no type)";
1335 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1337 basic_type = u.ti.bt;
1338 qualifiers[0].type = u.ti.tq0;
1339 qualifiers[1].type = u.ti.tq1;
1340 qualifiers[2].type = u.ti.tq2;
1341 qualifiers[3].type = u.ti.tq3;
1342 qualifiers[4].type = u.ti.tq4;
1343 qualifiers[5].type = u.ti.tq5;
1344 qualifiers[6].type = tqNil;
1347 * Go get the basic type.
1351 case btNil: /* undefined */
1355 case btAdr: /* address - integer same size as pointer */
1356 strcpy (p1, "address");
1359 case btChar: /* character */
1360 strcpy (p1, "char");
1363 case btUChar: /* unsigned character */
1364 strcpy (p1, "unsigned char");
1367 case btShort: /* short */
1368 strcpy (p1, "short");
1371 case btUShort: /* unsigned short */
1372 strcpy (p1, "unsigned short");
1375 case btInt: /* int */
1379 case btUInt: /* unsigned int */
1380 strcpy (p1, "unsigned int");
1383 case btLong: /* long */
1384 strcpy (p1, "long");
1387 case btULong: /* unsigned long */
1388 strcpy (p1, "unsigned long");
1391 case btFloat: /* float (real) */
1392 strcpy (p1, "float");
1395 case btDouble: /* Double (real) */
1396 strcpy (p1, "double");
1399 /* Structures add 1-2 aux words:
1400 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1401 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1403 case btStruct: /* Structure (Record) */
1404 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1405 ecoff_emit_aggregate (abfd, p1, &rndx,
1406 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1408 indx++; /* skip aux words */
1411 /* Unions add 1-2 aux words:
1412 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1413 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1415 case btUnion: /* Union */
1416 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1417 ecoff_emit_aggregate (abfd, p1, &rndx,
1418 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1420 indx++; /* skip aux words */
1423 /* Enumerations add 1-2 aux words:
1424 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1425 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1427 case btEnum: /* Enumeration */
1428 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1429 ecoff_emit_aggregate (abfd, p1, &rndx,
1430 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1432 indx++; /* skip aux words */
1435 case btTypedef: /* defined via a typedef, isymRef points */
1436 strcpy (p1, "typedef");
1439 case btRange: /* subrange of int */
1440 strcpy (p1, "subrange");
1443 case btSet: /* pascal sets */
1447 case btComplex: /* fortran complex */
1448 strcpy (p1, "complex");
1451 case btDComplex: /* fortran double complex */
1452 strcpy (p1, "double complex");
1455 case btIndirect: /* forward or unnamed typedef */
1456 strcpy (p1, "forward/unamed typedef");
1459 case btFixedDec: /* Fixed Decimal */
1460 strcpy (p1, "fixed decimal");
1463 case btFloatDec: /* Float Decimal */
1464 strcpy (p1, "float decimal");
1467 case btString: /* Varying Length Character String */
1468 strcpy (p1, "string");
1471 case btBit: /* Aligned Bit String */
1475 case btPicture: /* Picture */
1476 strcpy (p1, "picture");
1479 case btVoid: /* Void */
1480 strcpy (p1, "void");
1484 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1488 p1 += strlen (buffer1);
1491 * If this is a bitfield, get the bitsize.
1497 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1498 sprintf (p1, " : %d", bitsize);
1499 p1 += strlen (buffer1);
1504 * Deal with any qualifiers.
1506 if (qualifiers[0].type != tqNil)
1509 * Snarf up any array bounds in the correct order. Arrays
1510 * store 5 successive words in the aux. table:
1511 * word 0 RNDXR to type of the bounds (ie, int)
1512 * word 1 Current file descriptor index
1514 * word 3 high bound (or -1 if [])
1515 * word 4 stride size in bits
1517 for (i = 0; i < 7; i++)
1519 if (qualifiers[i].type == tqArray)
1521 qualifiers[i].low_bound =
1522 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1523 qualifiers[i].high_bound =
1524 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1525 qualifiers[i].stride =
1526 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1532 * Now print out the qualifiers.
1534 for (i = 0; i < 6; i++)
1536 switch (qualifiers[i].type)
1543 strcpy (p2, "ptr to ");
1544 p2 += sizeof ("ptr to ")-1;
1548 strcpy (p2, "volatile ");
1549 p2 += sizeof ("volatile ")-1;
1553 strcpy (p2, "far ");
1554 p2 += sizeof ("far ")-1;
1558 strcpy (p2, "func. ret. ");
1559 p2 += sizeof ("func. ret. ");
1564 int first_array = i;
1567 /* Print array bounds reversed (ie, in the order the C
1568 programmer writes them). C is such a fun language.... */
1570 while (i < 5 && qualifiers[i+1].type == tqArray)
1573 for (j = i; j >= first_array; j--)
1575 strcpy (p2, "array [");
1576 p2 += sizeof ("array [")-1;
1577 if (qualifiers[j].low_bound != 0)
1579 "%ld:%ld {%ld bits}",
1580 (long) qualifiers[j].low_bound,
1581 (long) qualifiers[j].high_bound,
1582 (long) qualifiers[j].stride);
1584 else if (qualifiers[j].high_bound != -1)
1587 (long) (qualifiers[j].high_bound + 1),
1588 (long) (qualifiers[j].stride));
1591 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1594 strcpy (p2, "] of ");
1595 p2 += sizeof ("] of ")-1;
1603 strcpy (p2, buffer1);
1607 /* Return information about ECOFF symbol SYMBOL in RET. */
1611 ecoff_get_symbol_info (abfd, symbol, ret)
1612 bfd *abfd; /* Ignored. */
1616 bfd_symbol_info (symbol, ret);
1619 /* Print information about an ECOFF symbol. */
1622 ecoff_print_symbol (abfd, filep, symbol, how)
1626 bfd_print_symbol_type how;
1628 const struct ecoff_debug_swap * const debug_swap
1629 = &ecoff_backend (abfd)->debug_swap;
1630 FILE *file = (FILE *)filep;
1634 case bfd_print_symbol_name:
1635 fprintf (file, "%s", symbol->name);
1637 case bfd_print_symbol_more:
1638 if (ecoffsymbol (symbol)->local)
1642 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1644 fprintf (file, "ecoff local ");
1645 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1646 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1647 (unsigned) ecoff_sym.sc);
1653 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1655 fprintf (file, "ecoff extern ");
1656 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1657 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1658 (unsigned) ecoff_ext.asym.sc);
1661 case bfd_print_symbol_all:
1662 /* Print out the symbols in a reasonable way */
1671 if (ecoffsymbol (symbol)->local)
1673 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1676 pos = ((((char *) ecoffsymbol (symbol)->native
1677 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1678 / debug_swap->external_sym_size)
1679 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1686 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1689 pos = (((char *) ecoffsymbol (symbol)->native
1690 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1691 / debug_swap->external_ext_size);
1692 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1693 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1694 weakext = ecoff_ext.weakext ? 'w' : ' ';
1697 fprintf (file, "[%3d] %c ",
1699 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1700 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1701 (unsigned) ecoff_ext.asym.st,
1702 (unsigned) ecoff_ext.asym.sc,
1703 (unsigned) ecoff_ext.asym.index,
1704 jmptbl, cobol_main, weakext,
1707 if (ecoffsymbol (symbol)->fdr != NULL
1708 && ecoff_ext.asym.index != indexNil)
1712 bfd_size_type sym_base;
1713 union aux_ext *aux_base;
1715 indx = ecoff_ext.asym.index;
1717 /* sym_base is used to map the fdr relative indices which
1718 appear in the file to the position number which we are
1720 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1721 if (ecoffsymbol (symbol)->local)
1723 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1725 /* aux_base is the start of the aux entries for this file;
1726 asym.index is an offset from this. */
1727 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1728 + ecoffsymbol (symbol)->fdr->iauxBase);
1730 /* The aux entries are stored in host byte order; the
1731 order is indicated by a bit in the fdr. */
1732 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1734 /* This switch is basically from gcc/mips-tdump.c */
1735 switch (ecoff_ext.asym.st)
1743 fprintf (file, "\n End+1 symbol: %ld",
1744 (long) (indx + sym_base));
1748 if (ecoff_ext.asym.sc == scText
1749 || ecoff_ext.asym.sc == scInfo)
1750 fprintf (file, "\n First symbol: %ld",
1751 (long) (indx + sym_base));
1753 fprintf (file, "\n First symbol: %ld",
1754 (long) (AUX_GET_ISYM (bigendian,
1755 &aux_base[ecoff_ext.asym.index])
1761 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1763 else if (ecoffsymbol (symbol)->local)
1764 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1765 (long) (AUX_GET_ISYM (bigendian,
1766 &aux_base[ecoff_ext.asym.index])
1768 ecoff_type_to_string (abfd, aux_base, indx + 1,
1771 fprintf (file, "\n Local symbol: %ld",
1774 + (ecoff_data (abfd)
1775 ->debug_info.symbolic_header.iextMax)));
1779 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1780 fprintf (file, "\n Type: %s",
1781 ecoff_type_to_string (abfd, aux_base, indx,
1791 /* Read in the relocs for a section. */
1794 ecoff_slurp_reloc_table (abfd, section, symbols)
1799 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1800 arelent *internal_relocs;
1801 bfd_size_type external_reloc_size;
1802 bfd_size_type external_relocs_size;
1803 char *external_relocs;
1807 if (section->relocation != (arelent *) NULL
1808 || section->reloc_count == 0
1809 || (section->flags & SEC_CONSTRUCTOR) != 0)
1812 if (ecoff_slurp_symbol_table (abfd) == false)
1815 internal_relocs = (arelent *) bfd_alloc (abfd,
1817 * section->reloc_count));
1818 external_reloc_size = backend->external_reloc_size;
1819 external_relocs_size = external_reloc_size * section->reloc_count;
1820 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1821 if (internal_relocs == (arelent *) NULL
1822 || external_relocs == (char *) NULL)
1824 bfd_set_error (bfd_error_no_memory);
1827 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1829 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1830 != external_relocs_size)
1832 bfd_set_error (bfd_error_system_call);
1836 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1838 struct internal_reloc intern;
1840 (*backend->swap_reloc_in) (abfd,
1841 external_relocs + i * external_reloc_size,
1844 if (intern.r_extern)
1846 /* r_symndx is an index into the external symbols. */
1847 BFD_ASSERT (intern.r_symndx >= 0
1849 < (ecoff_data (abfd)
1850 ->debug_info.symbolic_header.iextMax)));
1851 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1854 else if (intern.r_symndx == RELOC_SECTION_NONE
1855 || intern.r_symndx == RELOC_SECTION_ABS)
1857 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1862 CONST char *sec_name;
1865 /* r_symndx is a section key. */
1866 switch (intern.r_symndx)
1868 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1869 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1870 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1871 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1872 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1873 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1874 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1875 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1876 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1877 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1878 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1879 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1880 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1884 sec = bfd_get_section_by_name (abfd, sec_name);
1885 if (sec == (asection *) NULL)
1887 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1889 rptr->addend = - bfd_get_section_vma (abfd, sec);
1892 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1894 /* Let the backend select the howto field and do any other
1895 required processing. */
1896 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1899 bfd_release (abfd, external_relocs);
1901 section->relocation = internal_relocs;
1906 /* Get a canonical list of relocs. */
1909 ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1917 if (section->flags & SEC_CONSTRUCTOR)
1919 arelent_chain *chain;
1921 /* This section has relocs made up by us, not the file, so take
1922 them out of their chain and place them into the data area
1924 for (count = 0, chain = section->constructor_chain;
1925 count < section->reloc_count;
1926 count++, chain = chain->next)
1927 *relptr++ = &chain->relent;
1933 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1936 tblptr = section->relocation;
1937 if (tblptr == (arelent *) NULL)
1940 for (count = 0; count < section->reloc_count; count++)
1941 *relptr++ = tblptr++;
1944 *relptr = (arelent *) NULL;
1946 return section->reloc_count;
1949 /* Provided a BFD, a section and an offset into the section, calculate
1950 and return the name of the source file and the line nearest to the
1955 ecoff_find_nearest_line (abfd,
1964 asymbol **ignore_symbols;
1966 CONST char **filename_ptr;
1967 CONST char **functionname_ptr;
1968 unsigned int *retline_ptr;
1970 const struct ecoff_debug_swap * const debug_swap
1971 = &ecoff_backend (abfd)->debug_swap;
1976 bfd_size_type external_pdr_size;
1980 unsigned char *line_ptr;
1981 unsigned char *line_end;
1984 /* If we're not in the .text section, we don't have any line
1986 if (strcmp (section->name, _TEXT) != 0
1987 || offset < ecoff_data (abfd)->text_start
1988 || offset >= ecoff_data (abfd)->text_end)
1991 /* Make sure we have the FDR's. */
1992 if (ecoff_slurp_symbolic_info (abfd) == false
1993 || bfd_get_symcount (abfd) == 0)
1996 /* Each file descriptor (FDR) has a memory address. Here we track
1997 down which FDR we want. The FDR's are stored in increasing
1998 memory order. If speed is ever important, this can become a
1999 binary search. We must ignore FDR's with no PDR entries; they
2000 will have the adr of the FDR before or after them. */
2001 fdr_start = ecoff_data (abfd)->debug_info.fdr;
2002 fdr_end = fdr_start + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
2003 fdr_hold = (FDR *) NULL;
2004 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2006 if (fdr_ptr->cpd == 0)
2008 if (offset < fdr_ptr->adr)
2012 if (fdr_hold == (FDR *) NULL)
2016 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2017 have an address, which is relative to the FDR address, and are
2018 also stored in increasing memory order. */
2019 offset -= fdr_ptr->adr;
2020 external_pdr_size = debug_swap->external_pdr_size;
2021 pdr_ptr = ((char *) ecoff_data (abfd)->debug_info.external_pdr
2022 + fdr_ptr->ipdFirst * external_pdr_size);
2023 pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2024 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2026 /* The address of the first PDR is an offset which applies to the
2027 addresses of all the PDR's. */
2030 for (pdr_ptr += external_pdr_size;
2032 pdr_ptr += external_pdr_size)
2034 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2035 if (offset < pdr.adr)
2039 /* Now we can look for the actual line number. The line numbers are
2040 stored in a very funky format, which I won't try to describe.
2041 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2042 we want; we need this to compute line_end. */
2043 line_end = ecoff_data (abfd)->debug_info.line;
2044 if (pdr_ptr == pdr_end)
2045 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2047 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2049 /* Now change pdr and pdr_ptr to the one we want. */
2050 pdr_ptr -= external_pdr_size;
2051 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2055 line_ptr = (ecoff_data (abfd)->debug_info.line
2056 + fdr_ptr->cbLineOffset
2057 + pdr.cbLineOffset);
2058 while (line_ptr < line_end)
2063 delta = *line_ptr >> 4;
2066 count = (*line_ptr & 0xf) + 1;
2070 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2071 if (delta >= 0x8000)
2076 if (offset < count * 4)
2078 offset -= count * 4;
2081 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2082 at least according to gdb/mipsread.c. */
2083 if (fdr_ptr->rss == -1)
2085 *filename_ptr = NULL;
2087 *functionname_ptr = NULL;
2092 (*debug_swap->swap_ext_in)
2094 ((char *) ecoff_data (abfd)->debug_info.external_ext
2095 + pdr.isym * debug_swap->external_ext_size),
2097 *functionname_ptr = (ecoff_data (abfd)->debug_info.ssext
2098 + proc_ext.asym.iss);
2105 *filename_ptr = (ecoff_data (abfd)->debug_info.ss
2108 (*debug_swap->swap_sym_in)
2110 ((char *) ecoff_data (abfd)->debug_info.external_sym
2111 + (fdr_ptr->isymBase + pdr.isym) * debug_swap->external_sym_size),
2113 *functionname_ptr = (ecoff_data (abfd)->debug_info.ss
2117 if (lineno == ilineNil)
2119 *retline_ptr = lineno;
2123 /* Set the architecture. The supported architecture is stored in the
2124 backend pointer. We always set the architecture anyhow, since many
2125 callers ignore the return value. */
2128 ecoff_set_arch_mach (abfd, arch, machine)
2130 enum bfd_architecture arch;
2131 unsigned long machine;
2133 bfd_default_set_arch_mach (abfd, arch, machine);
2134 return arch == ecoff_backend (abfd)->arch;
2137 /* Get the size of the section headers. We do not output the .reginfo
2142 ecoff_sizeof_headers (abfd, reloc)
2151 for (current = abfd->sections;
2152 current != (asection *)NULL;
2153 current = current->next)
2154 if (strcmp (current->name, REGINFO) != 0)
2157 ret = (bfd_coff_filhsz (abfd)
2158 + bfd_coff_aoutsz (abfd)
2159 + c * bfd_coff_scnhsz (abfd));
2160 return BFD_ALIGN (ret, 16);
2163 /* Get the contents of a section. This is where we handle reading the
2164 .reginfo section, which implicitly holds the contents of an
2165 ecoff_reginfo structure. */
2168 ecoff_get_section_contents (abfd, section, location, offset, count)
2173 bfd_size_type count;
2175 ecoff_data_type *tdata = ecoff_data (abfd);
2176 struct ecoff_reginfo s;
2179 if (strcmp (section->name, REGINFO) != 0)
2180 return bfd_generic_get_section_contents (abfd, section, location,
2183 s.gp_value = tdata->gp;
2184 s.gprmask = tdata->gprmask;
2185 for (i = 0; i < 4; i++)
2186 s.cprmask[i] = tdata->cprmask[i];
2187 s.fprmask = tdata->fprmask;
2189 /* bfd_get_section_contents has already checked that the offset and
2190 size is reasonable. We don't have to worry about swapping or any
2191 such thing; the .reginfo section is defined such that the
2192 contents are an ecoff_reginfo structure as seen on the host. */
2193 memcpy (location, ((char *) &s) + offset, (size_t) count);
2197 /* Calculate the file position for each section, and set
2201 ecoff_compute_section_file_positions (abfd)
2209 sofar = ecoff_sizeof_headers (abfd, false);
2212 for (current = abfd->sections;
2213 current != (asection *) NULL;
2214 current = current->next)
2216 unsigned int alignment_power;
2218 /* Only deal with sections which have contents */
2219 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) == 0
2220 || strcmp (current->name, REGINFO) == 0)
2223 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2224 supposed to indicate the number of .pdata entries that are
2225 really in the section. Each entry is 8 bytes. We store this
2226 away in line_filepos before increasing the section size. */
2227 if (strcmp (current->name, _PDATA) != 0)
2228 alignment_power = current->alignment_power;
2231 current->line_filepos = current->_raw_size / 8;
2232 alignment_power = 4;
2235 /* On Ultrix, the data sections in an executable file must be
2236 aligned to a page boundary within the file. This does not
2237 affect the section size, though. FIXME: Does this work for
2238 other platforms? It requires some modification for the
2239 Alpha, because .rdata on the Alpha goes with the text, not
2241 if ((abfd->flags & EXEC_P) != 0
2242 && (abfd->flags & D_PAGED) != 0
2243 && first_data != false
2244 && (current->flags & SEC_CODE) == 0
2245 && (! ecoff_backend (abfd)->rdata_in_text
2246 || strcmp (current->name, _RDATA) != 0)
2247 && strcmp (current->name, _PDATA) != 0)
2249 const bfd_vma round = ecoff_backend (abfd)->round;
2251 sofar = (sofar + round - 1) &~ (round - 1);
2255 /* Align the sections in the file to the same boundary on
2256 which they are aligned in virtual memory. */
2258 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2260 current->filepos = sofar;
2262 sofar += current->_raw_size;
2264 /* make sure that this section is of the right size too */
2266 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2267 current->_raw_size += sofar - old_sofar;
2270 ecoff_data (abfd)->reloc_filepos = sofar;
2273 /* Determine the location of the relocs for all the sections in the
2274 output file, as well as the location of the symbolic debugging
2277 static bfd_size_type
2278 ecoff_compute_reloc_file_positions (abfd)
2281 const bfd_size_type external_reloc_size =
2282 ecoff_backend (abfd)->external_reloc_size;
2283 file_ptr reloc_base;
2284 bfd_size_type reloc_size;
2288 if (! abfd->output_has_begun)
2290 ecoff_compute_section_file_positions (abfd);
2291 abfd->output_has_begun = true;
2294 reloc_base = ecoff_data (abfd)->reloc_filepos;
2297 for (current = abfd->sections;
2298 current != (asection *)NULL;
2299 current = current->next)
2301 if (strcmp (current->name, REGINFO) == 0)
2303 if (current->reloc_count == 0)
2304 current->rel_filepos = 0;
2307 bfd_size_type relsize;
2309 current->rel_filepos = reloc_base;
2310 relsize = current->reloc_count * external_reloc_size;
2311 reloc_size += relsize;
2312 reloc_base += relsize;
2316 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2318 /* At least on Ultrix, the symbol table of an executable file must
2319 be aligned to a page boundary. FIXME: Is this true on other
2321 if ((abfd->flags & EXEC_P) != 0
2322 && (abfd->flags & D_PAGED) != 0)
2323 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2324 &~ (ecoff_backend (abfd)->round - 1));
2326 ecoff_data (abfd)->sym_filepos = sym_base;
2331 /* Set the contents of a section. This is where we handle setting the
2332 contents of the .reginfo section, which implicitly holds a
2333 ecoff_reginfo structure. */
2336 ecoff_set_section_contents (abfd, section, location, offset, count)
2341 bfd_size_type count;
2343 /* This must be done first, because bfd_set_section_contents is
2344 going to set output_has_begun to true. */
2345 if (abfd->output_has_begun == false)
2346 ecoff_compute_section_file_positions (abfd);
2351 if (strcmp (section->name, REGINFO) == 0)
2353 ecoff_data_type *tdata = ecoff_data (abfd);
2354 struct ecoff_reginfo s;
2357 /* If the caller is only changing part of the structure, we must
2358 retrieve the current information before the memcpy. */
2359 if (offset != 0 || count != sizeof (struct ecoff_reginfo))
2361 s.gp_value = tdata->gp;
2362 s.gprmask = tdata->gprmask;
2363 for (i = 0; i < 4; i++)
2364 s.cprmask[i] = tdata->cprmask[i];
2365 s.fprmask = tdata->fprmask;
2368 /* bfd_set_section_contents has already checked that the offset
2369 and size is reasonable. We don't have to worry about
2370 swapping or any such thing; the .reginfo section is defined
2371 such that the contents are an ecoff_reginfo structure as seen
2373 memcpy (((char *) &s) + offset, location, (size_t) count);
2375 tdata->gp = s.gp_value;
2376 tdata->gprmask = s.gprmask;
2377 for (i = 0; i < 4; i++)
2378 tdata->cprmask[i] = s.cprmask[i];
2379 tdata->fprmask = s.fprmask;
2384 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2385 || bfd_write (location, 1, count, abfd) != count)
2391 /* Get ECOFF EXTR information for an external symbol. This function
2392 is passed to bfd_ecoff_debug_externals. */
2395 ecoff_get_extr (sym, esym)
2399 ecoff_symbol_type *ecoff_sym_ptr;
2402 /* Don't include debugging, local or section symbols. */
2403 if ((sym->flags & BSF_DEBUGGING) != 0
2404 || (sym->flags & BSF_LOCAL) != 0
2405 || (sym->flags & BSF_SECTION_SYM) != 0)
2408 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2409 || ecoffsymbol (sym)->native == NULL)
2412 esym->cobol_main = 0;
2416 /* FIXME: we can do better than this for st and sc. */
2417 esym->asym.st = stGlobal;
2418 esym->asym.sc = scAbs;
2419 esym->asym.reserved = 0;
2420 esym->asym.index = indexNil;
2424 ecoff_sym_ptr = ecoffsymbol (sym);
2426 if (ecoff_sym_ptr->local)
2429 input_bfd = bfd_asymbol_bfd (sym);
2430 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2431 (input_bfd, ecoff_sym_ptr->native, esym);
2433 /* If the symbol was defined by the linker, then esym will be
2434 undefined but sym will not be. Get a better class for such a
2436 if ((esym->asym.sc == scUndefined
2437 || esym->asym.sc == scSUndefined)
2438 && bfd_get_section (sym) != &bfd_und_section)
2439 esym->asym.sc = scAbs;
2441 /* Adjust the FDR index for the symbol by that used for the input
2443 if (esym->ifd != -1)
2445 struct ecoff_debug_info *input_debug;
2447 input_debug = &ecoff_data (input_bfd)->debug_info;
2448 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2449 if (input_debug->ifdmap != (RFDT *) NULL)
2450 esym->ifd = input_debug->ifdmap[esym->ifd];
2456 /* Set the external symbol index. This routine is passed to
2457 bfd_ecoff_debug_externals. */
2460 ecoff_set_index (sym, indx)
2464 ecoff_set_sym_index (sym, indx);
2467 /* Write out an ECOFF file. */
2470 ecoff_write_object_contents (abfd)
2473 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2474 const bfd_vma round = backend->round;
2475 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2476 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2477 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2478 const bfd_size_type external_hdr_size
2479 = backend->debug_swap.external_hdr_size;
2480 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2481 void (* const adjust_reloc_out) PARAMS ((bfd *,
2483 struct internal_reloc *))
2484 = backend->adjust_reloc_out;
2485 void (* const swap_reloc_out) PARAMS ((bfd *,
2486 const struct internal_reloc *,
2488 = backend->swap_reloc_out;
2489 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2490 HDRR * const symhdr = &debug->symbolic_header;
2493 bfd_size_type reloc_size;
2494 bfd_size_type text_size;
2496 bfd_size_type data_size;
2498 bfd_size_type bss_size;
2500 PTR reloc_buff = NULL;
2501 struct internal_filehdr internal_f;
2502 struct internal_aouthdr internal_a;
2505 bfd_set_error (bfd_error_system_call);
2507 /* Determine where the sections and relocs will go in the output
2509 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2512 for (current = abfd->sections;
2513 current != (asection *)NULL;
2514 current = current->next)
2516 if (strcmp (current->name, REGINFO) == 0)
2518 current->target_index = count;
2522 if ((abfd->flags & D_PAGED) != 0)
2523 text_size = ecoff_sizeof_headers (abfd, false);
2531 /* Write section headers to the file. */
2533 /* Allocate buff big enough to hold a section header,
2534 file header, or a.out header. */
2542 buff = (PTR) malloc (siz);
2545 bfd_set_error (bfd_error_no_memory);
2550 internal_f.f_nscns = 0;
2551 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2553 for (current = abfd->sections;
2554 current != (asection *) NULL;
2555 current = current->next)
2557 struct internal_scnhdr section;
2560 if (strcmp (current->name, REGINFO) == 0)
2562 BFD_ASSERT (current->reloc_count == 0);
2566 ++internal_f.f_nscns;
2568 strncpy (section.s_name, current->name, sizeof section.s_name);
2570 /* FIXME: is this correct for shared libraries? I think it is
2571 but I have no platform to check. Ian Lance Taylor. */
2572 vma = bfd_get_section_vma (abfd, current);
2573 if (strcmp (current->name, _LIB) == 0)
2574 section.s_vaddr = 0;
2576 section.s_vaddr = vma;
2578 section.s_paddr = vma;
2579 section.s_size = bfd_get_section_size_before_reloc (current);
2581 /* If this section is unloadable then the scnptr will be 0. */
2582 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2583 section.s_scnptr = 0;
2585 section.s_scnptr = current->filepos;
2586 section.s_relptr = current->rel_filepos;
2588 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2589 object file produced by the assembler is supposed to point to
2590 information about how much room is required by objects of
2591 various different sizes. I think this only matters if we
2592 want the linker to compute the best size to use, or
2593 something. I don't know what happens if the information is
2595 if (strcmp (current->name, _PDATA) != 0)
2596 section.s_lnnoptr = 0;
2599 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2600 hold the number of entries in the section (each entry is
2601 8 bytes). We stored this in the line_filepos field in
2602 ecoff_compute_section_file_positions. */
2603 section.s_lnnoptr = current->line_filepos;
2606 section.s_nreloc = current->reloc_count;
2607 section.s_nlnno = 0;
2608 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2611 bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff);
2612 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2615 if ((section.s_flags & STYP_TEXT) != 0
2616 || ((section.s_flags & STYP_RDATA) != 0
2617 && backend->rdata_in_text)
2618 || strcmp (current->name, _PDATA) == 0)
2620 text_size += bfd_get_section_size_before_reloc (current);
2621 if (text_start == 0 || text_start > vma)
2624 else if ((section.s_flags & STYP_RDATA) != 0
2625 || (section.s_flags & STYP_DATA) != 0
2626 || (section.s_flags & STYP_LITA) != 0
2627 || (section.s_flags & STYP_LIT8) != 0
2628 || (section.s_flags & STYP_LIT4) != 0
2629 || (section.s_flags & STYP_SDATA) != 0
2630 || strcmp (current->name, _XDATA) == 0)
2632 data_size += bfd_get_section_size_before_reloc (current);
2633 if (data_start == 0 || data_start > vma)
2636 else if ((section.s_flags & STYP_BSS) != 0
2637 || (section.s_flags & STYP_SBSS) != 0)
2638 bss_size += bfd_get_section_size_before_reloc (current);
2643 /* Set up the file header. */
2645 internal_f.f_magic = ecoff_get_magic (abfd);
2647 /* We will NOT put a fucking timestamp in the header here. Every
2648 time you put it back, I will come in and take it out again. I'm
2649 sorry. This field does not belong here. We fill it with a 0 so
2650 it compares the same but is not a reasonable time. --
2652 internal_f.f_timdat = 0;
2654 if (bfd_get_symcount (abfd) != 0)
2656 /* The ECOFF f_nsyms field is not actually the number of
2657 symbols, it's the size of symbolic information header. */
2658 internal_f.f_nsyms = external_hdr_size;
2659 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2663 internal_f.f_nsyms = 0;
2664 internal_f.f_symptr = 0;
2667 internal_f.f_opthdr = aoutsz;
2669 internal_f.f_flags = F_LNNO;
2670 if (reloc_size == 0)
2671 internal_f.f_flags |= F_RELFLG;
2672 if (bfd_get_symcount (abfd) == 0)
2673 internal_f.f_flags |= F_LSYMS;
2674 if (abfd->flags & EXEC_P)
2675 internal_f.f_flags |= F_EXEC;
2677 if (! abfd->xvec->byteorder_big_p)
2678 internal_f.f_flags |= F_AR32WR;
2680 internal_f.f_flags |= F_AR32W;
2682 /* Set up the ``optional'' header. */
2683 if ((abfd->flags & D_PAGED) != 0)
2684 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2686 internal_a.magic = ECOFF_AOUT_OMAGIC;
2688 /* FIXME: Is this really correct? */
2689 internal_a.vstamp = symhdr->vstamp;
2691 /* At least on Ultrix, these have to be rounded to page boundaries.
2692 FIXME: Is this true on other platforms? */
2693 if ((abfd->flags & D_PAGED) != 0)
2695 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2696 internal_a.text_start = text_start &~ (round - 1);
2697 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2698 internal_a.data_start = data_start &~ (round - 1);
2702 internal_a.tsize = text_size;
2703 internal_a.text_start = text_start;
2704 internal_a.dsize = data_size;
2705 internal_a.data_start = data_start;
2708 /* On Ultrix, the initial portions of the .sbss and .bss segments
2709 are at the end of the data section. The bsize field in the
2710 optional header records how many bss bytes are required beyond
2711 those in the data section. The value is not rounded to a page
2713 if (bss_size < internal_a.dsize - data_size)
2716 bss_size -= internal_a.dsize - data_size;
2717 internal_a.bsize = bss_size;
2718 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2720 internal_a.entry = bfd_get_start_address (abfd);
2722 internal_a.gp_value = ecoff_data (abfd)->gp;
2724 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2725 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2726 for (i = 0; i < 4; i++)
2727 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2729 /* Write out the file header and the optional header. */
2731 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2734 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2735 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2738 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2739 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2742 /* Build the external symbol information. This must be done before
2743 writing out the relocs so that we know the symbol indices. The
2744 condition checks makes sure this object was not created by
2745 ecoff_bfd_final_link, since if it was we do not want to tamper
2746 with the external symbols. */
2747 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL)
2749 symhdr->iextMax = 0;
2750 symhdr->issExtMax = 0;
2751 debug->external_ext = debug->external_ext_end = NULL;
2752 debug->ssext = debug->ssext_end = NULL;
2753 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2754 (((abfd->flags & EXEC_P) == 0)
2756 ecoff_get_extr, ecoff_set_index)
2760 /* Write out the relocs. */
2761 for (current = abfd->sections;
2762 current != (asection *) NULL;
2763 current = current->next)
2765 arelent **reloc_ptr_ptr;
2766 arelent **reloc_end;
2769 if (current->reloc_count == 0)
2773 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2774 if (reloc_buff == NULL)
2776 bfd_set_error (bfd_error_no_memory);
2780 reloc_ptr_ptr = current->orelocation;
2781 reloc_end = reloc_ptr_ptr + current->reloc_count;
2782 out_ptr = (char *) reloc_buff;
2784 reloc_ptr_ptr < reloc_end;
2785 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2789 struct internal_reloc in;
2791 memset ((PTR) &in, 0, sizeof in);
2793 reloc = *reloc_ptr_ptr;
2794 sym = *reloc->sym_ptr_ptr;
2796 in.r_vaddr = (reloc->address
2797 + bfd_get_section_vma (abfd, current));
2798 in.r_type = reloc->howto->type;
2800 if ((sym->flags & BSF_SECTION_SYM) == 0)
2802 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2809 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2810 if (strcmp (name, ".text") == 0)
2811 in.r_symndx = RELOC_SECTION_TEXT;
2812 else if (strcmp (name, ".rdata") == 0)
2813 in.r_symndx = RELOC_SECTION_RDATA;
2814 else if (strcmp (name, ".data") == 0)
2815 in.r_symndx = RELOC_SECTION_DATA;
2816 else if (strcmp (name, ".sdata") == 0)
2817 in.r_symndx = RELOC_SECTION_SDATA;
2818 else if (strcmp (name, ".sbss") == 0)
2819 in.r_symndx = RELOC_SECTION_SBSS;
2820 else if (strcmp (name, ".bss") == 0)
2821 in.r_symndx = RELOC_SECTION_BSS;
2822 else if (strcmp (name, ".init") == 0)
2823 in.r_symndx = RELOC_SECTION_INIT;
2824 else if (strcmp (name, ".lit8") == 0)
2825 in.r_symndx = RELOC_SECTION_LIT8;
2826 else if (strcmp (name, ".lit4") == 0)
2827 in.r_symndx = RELOC_SECTION_LIT4;
2828 else if (strcmp (name, ".xdata") == 0)
2829 in.r_symndx = RELOC_SECTION_XDATA;
2830 else if (strcmp (name, ".pdata") == 0)
2831 in.r_symndx = RELOC_SECTION_PDATA;
2832 else if (strcmp (name, ".fini") == 0)
2833 in.r_symndx = RELOC_SECTION_FINI;
2834 else if (strcmp (name, ".lita") == 0)
2835 in.r_symndx = RELOC_SECTION_LITA;
2836 else if (strcmp (name, "*ABS*") == 0)
2837 in.r_symndx = RELOC_SECTION_ABS;
2843 (*adjust_reloc_out) (abfd, reloc, &in);
2845 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2848 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2850 if (bfd_write (reloc_buff,
2851 external_reloc_size, current->reloc_count, abfd)
2852 != external_reloc_size * current->reloc_count)
2854 bfd_release (abfd, reloc_buff);
2858 /* Write out the symbolic debugging information. */
2859 if (bfd_get_symcount (abfd) > 0)
2861 /* Write out the debugging information. */
2862 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2863 ecoff_data (abfd)->sym_filepos)
2869 /* The .bss section of a demand paged executable must receive an
2870 entire page. If there are symbols, the symbols will start on the
2871 next page. If there are no symbols, we must fill out the page by
2873 if (bfd_get_symcount (abfd) == 0
2874 && (abfd->flags & EXEC_P) != 0
2875 && (abfd->flags & D_PAGED) != 0)
2879 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2882 if (bfd_read (&c, 1, 1, abfd) == 0)
2884 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2887 if (bfd_write (&c, 1, 1, abfd) != 1)
2891 if (reloc_buff != NULL)
2892 bfd_release (abfd, reloc_buff);
2897 if (reloc_buff != NULL)
2898 bfd_release (abfd, reloc_buff);
2904 /* Archive handling. ECOFF uses what appears to be a unique type of
2905 archive header (armap). The byte ordering of the armap and the
2906 contents are encoded in the name of the armap itself. At least for
2907 now, we only support archives with the same byte ordering in the
2908 armap and the contents.
2910 The first four bytes in the armap are the number of symbol
2911 definitions. This is always a power of two.
2913 This is followed by the symbol definitions. Each symbol definition
2914 occupies 8 bytes. The first four bytes are the offset from the
2915 start of the armap strings to the null-terminated string naming
2916 this symbol. The second four bytes are the file offset to the
2917 archive member which defines this symbol. If the second four bytes
2918 are 0, then this is not actually a symbol definition, and it should
2921 The symbols are hashed into the armap with a closed hashing scheme.
2922 See the functions below for the details of the algorithm.
2924 After the symbol definitions comes four bytes holding the size of
2925 the string table, followed by the string table itself. */
2927 /* The name of an archive headers looks like this:
2928 __________E[BL]E[BL]_ (with a trailing space).
2929 The trailing space is changed to an X if the archive is changed to
2930 indicate that the armap is out of date.
2932 The Alpha seems to use ________64E[BL]E[BL]_. */
2934 #define ARMAP_BIG_ENDIAN 'B'
2935 #define ARMAP_LITTLE_ENDIAN 'L'
2936 #define ARMAP_MARKER 'E'
2937 #define ARMAP_START_LENGTH 10
2938 #define ARMAP_HEADER_MARKER_INDEX 10
2939 #define ARMAP_HEADER_ENDIAN_INDEX 11
2940 #define ARMAP_OBJECT_MARKER_INDEX 12
2941 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2942 #define ARMAP_END_INDEX 14
2943 #define ARMAP_END "_ "
2945 /* This is a magic number used in the hashing algorithm. */
2946 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2948 /* This returns the hash value to use for a string. It also sets
2949 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2950 is the number of entries in the hash table, and HLOG is the log
2954 ecoff_armap_hash (s, rehash, size, hlog)
2956 unsigned int *rehash;
2964 hash = ((hash >> 27) | (hash << 5)) + *s++;
2965 hash *= ARMAP_HASH_MAGIC;
2966 *rehash = (hash & (size - 1)) | 1;
2967 return hash >> (32 - hlog);
2970 /* Read in the armap. */
2973 ecoff_slurp_armap (abfd)
2978 struct areltdata *mapdata;
2979 bfd_size_type parsed_size;
2981 struct artdata *ardata;
2984 struct symdef *symdef_ptr;
2987 /* Get the name of the first element. */
2988 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2994 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
2996 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2997 standard COFF armap. We could move the ECOFF armap stuff into
2998 bfd_slurp_armap, but that seems inappropriate since no other
2999 target uses this format. Instead, we check directly for a COFF
3001 if (strncmp (nextname, "/ ", 16) == 0)
3002 return bfd_slurp_armap (abfd);
3004 /* See if the first element is an armap. */
3005 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
3006 ARMAP_START_LENGTH) != 0
3007 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3008 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3009 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3010 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3011 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3012 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3013 || strncmp (nextname + ARMAP_END_INDEX,
3014 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3016 bfd_has_map (abfd) = false;
3020 /* Make sure we have the right byte ordering. */
3021 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3022 ^ (abfd->xvec->header_byteorder_big_p != false))
3023 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3024 ^ (abfd->xvec->byteorder_big_p != false)))
3026 bfd_set_error (bfd_error_wrong_format);
3030 /* Read in the armap. */
3031 ardata = bfd_ardata (abfd);
3032 mapdata = _bfd_snarf_ar_hdr (abfd);
3033 if (mapdata == (struct areltdata *) NULL)
3035 parsed_size = mapdata->parsed_size;
3036 bfd_release (abfd, (PTR) mapdata);
3038 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3039 if (raw_armap == (char *) NULL)
3041 bfd_set_error (bfd_error_no_memory);
3045 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3047 bfd_set_error (bfd_error_malformed_archive);
3048 bfd_release (abfd, (PTR) raw_armap);
3052 ardata->tdata = (PTR) raw_armap;
3054 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3056 ardata->symdef_count = 0;
3057 ardata->cache = (struct ar_cache *) NULL;
3059 /* This code used to overlay the symdefs over the raw archive data,
3060 but that doesn't work on a 64 bit host. */
3062 stringbase = raw_armap + count * 8 + 8;
3064 #ifdef CHECK_ARMAP_HASH
3068 /* Double check that I have the hashing algorithm right by making
3069 sure that every symbol can be looked up successfully. */
3071 for (i = 1; i < count; i <<= 1)
3073 BFD_ASSERT (i == count);
3075 raw_ptr = raw_armap + 4;
3076 for (i = 0; i < count; i++, raw_ptr += 8)
3078 unsigned int name_offset, file_offset;
3079 unsigned int hash, rehash, srch;
3081 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3082 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3083 if (file_offset == 0)
3085 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3090 /* See if we can rehash to this location. */
3091 for (srch = (hash + rehash) & (count - 1);
3092 srch != hash && srch != i;
3093 srch = (srch + rehash) & (count - 1))
3094 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3096 BFD_ASSERT (srch == i);
3100 #endif /* CHECK_ARMAP_HASH */
3102 raw_ptr = raw_armap + 4;
3103 for (i = 0; i < count; i++, raw_ptr += 8)
3104 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3105 ++ardata->symdef_count;
3107 symdef_ptr = ((struct symdef *)
3109 ardata->symdef_count * sizeof (struct symdef)));
3112 bfd_set_error (bfd_error_no_memory);
3116 ardata->symdefs = (carsym *) symdef_ptr;
3118 raw_ptr = raw_armap + 4;
3119 for (i = 0; i < count; i++, raw_ptr += 8)
3121 unsigned int name_offset, file_offset;
3123 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3124 if (file_offset == 0)
3126 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3127 symdef_ptr->s.name = stringbase + name_offset;
3128 symdef_ptr->file_offset = file_offset;
3132 ardata->first_file_filepos = bfd_tell (abfd);
3133 /* Pad to an even boundary. */
3134 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3136 bfd_has_map (abfd) = true;
3141 /* Write out an armap. */
3144 ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3146 unsigned int elength;
3148 unsigned int orl_count;
3151 unsigned int hashsize, hashlog;
3152 unsigned int symdefsize;
3154 unsigned int stringsize;
3155 unsigned int mapsize;
3158 struct stat statbuf;
3161 bfd_byte *hashtable;
3165 /* Ultrix appears to use as a hash table size the least power of two
3166 greater than twice the number of entries. */
3167 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3169 hashsize = 1 << hashlog;
3171 symdefsize = hashsize * 8;
3173 stringsize = stridx + padit;
3175 /* Include 8 bytes to store symdefsize and stringsize in output. */
3176 mapsize = symdefsize + stringsize + 8;
3178 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3180 memset ((PTR) &hdr, 0, sizeof hdr);
3182 /* Work out the ECOFF armap name. */
3183 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3184 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3185 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3186 (abfd->xvec->header_byteorder_big_p
3188 : ARMAP_LITTLE_ENDIAN);
3189 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3190 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3191 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3192 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3194 /* Write the timestamp of the archive header to be just a little bit
3195 later than the timestamp of the file, otherwise the linker will
3196 complain that the index is out of date. Actually, the Ultrix
3197 linker just checks the archive name; the GNU linker may check the
3199 stat (abfd->filename, &statbuf);
3200 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3202 /* The DECstation uses zeroes for the uid, gid and mode of the
3204 hdr.ar_uid[0] = '0';
3205 hdr.ar_gid[0] = '0';
3206 hdr.ar_mode[0] = '0';
3208 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3210 hdr.ar_fmag[0] = '`';
3211 hdr.ar_fmag[1] = '\012';
3213 /* Turn all null bytes in the header into spaces. */
3214 for (i = 0; i < sizeof (struct ar_hdr); i++)
3215 if (((char *)(&hdr))[i] == '\0')
3216 (((char *)(&hdr))[i]) = ' ';
3218 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3219 != sizeof (struct ar_hdr))
3222 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3223 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3226 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3229 bfd_set_error (bfd_error_no_memory);
3233 current = abfd->archive_head;
3235 for (i = 0; i < orl_count; i++)
3237 unsigned int hash, rehash;
3239 /* Advance firstreal to the file position of this archive
3241 if (((bfd *) map[i].pos) != last_elt)
3245 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3246 firstreal += firstreal % 2;
3247 current = current->next;
3249 while (current != (bfd *) map[i].pos);
3254 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3255 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3259 /* The desired slot is already taken. */
3260 for (srch = (hash + rehash) & (hashsize - 1);
3262 srch = (srch + rehash) & (hashsize - 1))
3263 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3266 BFD_ASSERT (srch != hash);
3271 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3272 (PTR) (hashtable + hash * 8));
3273 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3274 (PTR) (hashtable + hash * 8 + 4));
3277 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3280 bfd_release (abfd, hashtable);
3282 /* Now write the strings. */
3283 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3284 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3286 for (i = 0; i < orl_count; i++)
3290 len = strlen (*map[i].name) + 1;
3291 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3295 /* The spec sez this should be a newline. But in order to be
3296 bug-compatible for DECstation ar we use a null. */
3299 if (bfd_write ("", 1, 1, abfd) != 1)
3306 /* See whether this BFD is an archive. If it is, read in the armap
3307 and the extended name table. */
3310 ecoff_archive_p (abfd)
3313 char armag[SARMAG + 1];
3315 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3316 || strncmp (armag, ARMAG, SARMAG) != 0)
3318 bfd_set_error (bfd_error_wrong_format);
3319 return (bfd_target *) NULL;
3322 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3323 involves a cast, we can't do it as the left operand of
3325 abfd->tdata.aout_ar_data =
3326 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3328 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3330 bfd_set_error (bfd_error_no_memory);
3331 return (bfd_target *) NULL;
3334 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3335 bfd_ardata (abfd)->cache = NULL;
3336 bfd_ardata (abfd)->archive_head = NULL;
3337 bfd_ardata (abfd)->symdefs = NULL;
3338 bfd_ardata (abfd)->extended_names = NULL;
3339 bfd_ardata (abfd)->tdata = NULL;
3341 if (ecoff_slurp_armap (abfd) == false
3342 || ecoff_slurp_extended_name_table (abfd) == false)
3344 bfd_release (abfd, bfd_ardata (abfd));
3345 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3346 return (bfd_target *) NULL;
3352 /* ECOFF linker code. */
3354 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3355 PARAMS ((struct bfd_hash_entry *entry,
3356 struct bfd_hash_table *table,
3357 const char *string));
3358 static boolean ecoff_link_add_archive_symbols
3359 PARAMS ((bfd *, struct bfd_link_info *));
3360 static boolean ecoff_link_check_archive_element
3361 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3362 static boolean ecoff_link_add_object_symbols
3363 PARAMS ((bfd *, struct bfd_link_info *));
3364 static boolean ecoff_link_add_externals
3365 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3367 /* Routine to create an entry in an ECOFF link hash table. */
3369 static struct bfd_hash_entry *
3370 ecoff_link_hash_newfunc (entry, table, string)
3371 struct bfd_hash_entry *entry;
3372 struct bfd_hash_table *table;
3375 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3377 /* Allocate the structure if it has not already been allocated by a
3379 if (ret == (struct ecoff_link_hash_entry *) NULL)
3380 ret = ((struct ecoff_link_hash_entry *)
3381 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3382 if (ret == (struct ecoff_link_hash_entry *) NULL)
3384 bfd_set_error (bfd_error_no_memory);
3388 /* Call the allocation method of the superclass. */
3389 ret = ((struct ecoff_link_hash_entry *)
3390 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3395 /* Set local fields. */
3399 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3401 return (struct bfd_hash_entry *) ret;
3404 /* Create an ECOFF link hash table. */
3406 struct bfd_link_hash_table *
3407 ecoff_bfd_link_hash_table_create (abfd)
3410 struct ecoff_link_hash_table *ret;
3412 ret = ((struct ecoff_link_hash_table *)
3413 malloc (sizeof (struct ecoff_link_hash_table)));
3416 bfd_set_error (bfd_error_no_memory);
3419 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3420 ecoff_link_hash_newfunc))
3423 return (struct bfd_link_hash_table *) NULL;
3428 /* Look up an entry in an ECOFF link hash table. */
3430 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3431 ((struct ecoff_link_hash_entry *) \
3432 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3434 /* Traverse an ECOFF link hash table. */
3436 #define ecoff_link_hash_traverse(table, func, info) \
3437 (bfd_link_hash_traverse \
3439 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3442 /* Get the ECOFF link hash table from the info structure. This is
3445 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3447 /* Given an ECOFF BFD, add symbols to the global hash table as
3451 ecoff_bfd_link_add_symbols (abfd, info)
3453 struct bfd_link_info *info;
3455 switch (bfd_get_format (abfd))
3458 return ecoff_link_add_object_symbols (abfd, info);
3460 return ecoff_link_add_archive_symbols (abfd, info);
3462 bfd_set_error (bfd_error_wrong_format);
3467 /* Add the symbols from an archive file to the global hash table.
3468 This looks through the undefined symbols, looks each one up in the
3469 archive hash table, and adds any associated object file. We do not
3470 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3471 already have a hash table, so there is no reason to construct
3475 ecoff_link_add_archive_symbols (abfd, info)
3477 struct bfd_link_info *info;
3479 const bfd_byte *raw_armap;
3480 struct bfd_link_hash_entry **pundef;
3481 unsigned int armap_count;
3482 unsigned int armap_log;
3484 const bfd_byte *hashtable;
3485 const char *stringbase;
3487 if (! bfd_has_map (abfd))
3489 bfd_set_error (bfd_error_no_symbols);
3493 /* If we don't have any raw data for this archive, as can happen on
3494 Irix 4.0.5F, we call the generic routine.
3495 FIXME: We should be more clever about this, since someday tdata
3496 may get to something for a generic archive. */
3497 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3498 if (raw_armap == (bfd_byte *) NULL)
3499 return (_bfd_generic_link_add_archive_symbols
3500 (abfd, info, ecoff_link_check_archive_element));
3502 armap_count = bfd_h_get_32 (abfd, raw_armap);
3505 for (i = 1; i < armap_count; i <<= 1)
3507 BFD_ASSERT (i == armap_count);
3509 hashtable = raw_armap + 4;
3510 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3512 /* Look through the list of undefined symbols. */
3513 pundef = &info->hash->undefs;
3514 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3516 struct bfd_link_hash_entry *h;
3517 unsigned int hash, rehash;
3518 unsigned int file_offset;
3524 /* When a symbol is defined, it is not necessarily removed from
3526 if (h->type != bfd_link_hash_undefined
3527 && h->type != bfd_link_hash_common)
3529 /* Remove this entry from the list, for general cleanliness
3530 and because we are going to look through the list again
3531 if we search any more libraries. We can't remove the
3532 entry if it is the tail, because that would lose any
3533 entries we add to the list later on. */
3534 if (*pundef != info->hash->undefs_tail)
3535 *pundef = (*pundef)->next;
3537 pundef = &(*pundef)->next;
3541 /* Native ECOFF linkers do not pull in archive elements merely
3542 to satisfy common definitions, so neither do we. We leave
3543 them on the list, though, in case we are linking against some
3544 other object format. */
3545 if (h->type != bfd_link_hash_undefined)
3547 pundef = &(*pundef)->next;
3551 /* Look for this symbol in the archive hash table. */
3552 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3555 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3556 if (file_offset == 0)
3558 /* Nothing in this slot. */
3559 pundef = &(*pundef)->next;
3563 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3564 if (name[0] != h->root.string[0]
3565 || strcmp (name, h->root.string) != 0)
3570 /* That was the wrong symbol. Try rehashing. */
3572 for (srch = (hash + rehash) & (armap_count - 1);
3574 srch = (srch + rehash) & (armap_count - 1))
3576 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3577 if (file_offset == 0)
3579 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3580 if (name[0] == h->root.string[0]
3581 && strcmp (name, h->root.string) == 0)
3590 pundef = &(*pundef)->next;
3597 element = _bfd_get_elt_at_filepos (abfd, file_offset);
3598 if (element == (bfd *) NULL)
3601 if (! bfd_check_format (element, bfd_object))
3604 /* Unlike the generic linker, we know that this element provides
3605 a definition for an undefined symbol and we know that we want
3606 to include it. We don't need to check anything. */
3607 if (! (*info->callbacks->add_archive_element) (info, element, name))
3609 if (! ecoff_link_add_object_symbols (element, info))
3612 pundef = &(*pundef)->next;
3618 /* This is called if we used _bfd_generic_link_add_archive_symbols
3619 because we were not dealing with an ECOFF archive. */
3622 ecoff_link_check_archive_element (abfd, info, pneeded)
3624 struct bfd_link_info *info;
3627 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3628 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3629 = backend->debug_swap.swap_ext_in;
3631 bfd_size_type external_ext_size;
3632 PTR external_ext = NULL;
3640 if (! ecoff_slurp_symbolic_header (abfd))
3643 /* If there are no symbols, we don't want it. */
3644 if (bfd_get_symcount (abfd) == 0)
3645 goto successful_return;
3647 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3649 /* Read in the external symbols and external strings. */
3650 external_ext_size = backend->debug_swap.external_ext_size;
3651 esize = symhdr->iextMax * external_ext_size;
3652 external_ext = (PTR) malloc (esize);
3653 if (external_ext == NULL)
3655 bfd_set_error (bfd_error_no_memory);
3659 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3660 || bfd_read (external_ext, 1, esize, abfd) != esize)
3663 ssext = (char *) malloc (symhdr->issExtMax);
3666 bfd_set_error (bfd_error_no_memory);
3670 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3671 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
3674 /* Look through the external symbols to see if they define some
3675 symbol that is currently undefined. */
3676 ext_ptr = (char *) external_ext;
3677 ext_end = ext_ptr + esize;
3678 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3683 struct bfd_link_hash_entry *h;
3685 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3687 /* See if this symbol defines something. */
3688 if (esym.asym.st != stGlobal
3689 && esym.asym.st != stLabel
3690 && esym.asym.st != stProc)
3693 switch (esym.asym.sc)
3716 name = ssext + esym.asym.iss;
3717 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3719 /* Unlike the generic linker, we do not pull in elements because
3720 of common symbols. */
3721 if (h == (struct bfd_link_hash_entry *) NULL
3722 || h->type != bfd_link_hash_undefined)
3725 /* Include this element. */
3726 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3728 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3732 goto successful_return;
3736 if (external_ext != NULL)
3737 free (external_ext);
3742 if (external_ext != NULL)
3743 free (external_ext);
3749 /* Add symbols from an ECOFF object file to the global linker hash
3753 ecoff_link_add_object_symbols (abfd, info)
3755 struct bfd_link_info *info;
3758 bfd_size_type external_ext_size;
3759 PTR external_ext = NULL;
3764 if (! ecoff_slurp_symbolic_header (abfd))
3767 /* If there are no symbols, we don't want it. */
3768 if (bfd_get_symcount (abfd) == 0)
3771 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3773 /* Read in the external symbols and external strings. */
3774 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3775 esize = symhdr->iextMax * external_ext_size;
3776 external_ext = (PTR) malloc (esize);
3777 if (external_ext == NULL)
3779 bfd_set_error (bfd_error_no_memory);
3783 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3784 || bfd_read (external_ext, 1, esize, abfd) != esize)
3787 ssext = (char *) malloc (symhdr->issExtMax);
3790 bfd_set_error (bfd_error_no_memory);
3794 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3795 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
3798 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3802 if (external_ext != NULL)
3803 free (external_ext);
3809 if (external_ext != NULL)
3810 free (external_ext);
3814 /* Add the external symbols of an object file to the global linker
3815 hash table. The external symbols and strings we are passed are
3816 just allocated on the stack, and will be discarded. We must
3817 explicitly save any information we may need later on in the link.
3818 We do not want to read the external symbol information again. */
3821 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3823 struct bfd_link_info *info;
3827 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3828 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3829 = backend->debug_swap.swap_ext_in;
3830 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3831 unsigned long ext_count;
3832 struct ecoff_link_hash_entry **sym_hash;
3836 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3838 sym_hash = ((struct ecoff_link_hash_entry **)
3840 ext_count * sizeof (struct bfd_link_hash_entry *)));
3843 bfd_set_error (bfd_error_no_memory);
3846 ecoff_data (abfd)->sym_hashes = sym_hash;
3848 ext_ptr = (char *) external_ext;
3849 ext_end = ext_ptr + ext_count * external_ext_size;
3850 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3857 struct ecoff_link_hash_entry *h;
3861 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3863 /* Skip debugging symbols. */
3865 switch (esym.asym.st)
3881 /* Get the information for this symbol. */
3882 value = esym.asym.value;
3883 switch (esym.asym.sc)
3903 section = bfd_make_section_old_way (abfd, ".text");
3904 value -= section->vma;
3907 section = bfd_make_section_old_way (abfd, ".data");
3908 value -= section->vma;
3911 section = bfd_make_section_old_way (abfd, ".bss");
3912 value -= section->vma;
3915 section = &bfd_abs_section;
3918 section = &bfd_und_section;
3921 section = bfd_make_section_old_way (abfd, ".sdata");
3922 value -= section->vma;
3925 section = bfd_make_section_old_way (abfd, ".sbss");
3926 value -= section->vma;
3929 section = bfd_make_section_old_way (abfd, ".rdata");
3930 value -= section->vma;
3933 if (value > ecoff_data (abfd)->gp_size)
3935 section = &bfd_com_section;
3940 if (ecoff_scom_section.name == NULL)
3942 /* Initialize the small common section. */
3943 ecoff_scom_section.name = SCOMMON;
3944 ecoff_scom_section.flags = SEC_IS_COMMON;
3945 ecoff_scom_section.output_section = &ecoff_scom_section;
3946 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3947 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3948 ecoff_scom_symbol.name = SCOMMON;
3949 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3950 ecoff_scom_symbol.section = &ecoff_scom_section;
3951 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3953 section = &ecoff_scom_section;
3956 section = &bfd_und_section;
3959 section = bfd_make_section_old_way (abfd, ".init");
3960 value -= section->vma;
3963 section = bfd_make_section_old_way (abfd, ".fini");
3964 value -= section->vma;
3968 if (section == (asection *) NULL)
3971 name = ssext + esym.asym.iss;
3973 if (! (_bfd_generic_link_add_one_symbol
3974 (info, abfd, name, BSF_GLOBAL, section, value,
3975 (const char *) NULL, true, true, backend->constructor_bitsize,
3976 (struct bfd_link_hash_entry **) &h)))
3981 /* If we are building an ECOFF hash table, save the external
3982 symbol information. */
3983 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3985 if (h->abfd == (bfd *) NULL
3986 || (section != &bfd_und_section
3987 && (! bfd_is_com_section (section)
3988 || h->root.type != bfd_link_hash_defined)))
3999 /* ECOFF final link routines. */
4001 static boolean ecoff_final_link_debug_accumulate
4002 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4004 static boolean ecoff_link_write_external
4005 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4006 static boolean ecoff_indirect_link_order
4007 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4008 struct bfd_link_order *));
4010 /* ECOFF final link routine. This looks through all the input BFDs
4011 and gathers together all the debugging information, and then
4012 processes all the link order information. This may cause it to
4013 close and reopen some input BFDs; I'll see how bad this is. */
4016 ecoff_bfd_final_link (abfd, info)
4018 struct bfd_link_info *info;
4020 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4021 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4024 register bfd *input_bfd;
4026 struct bfd_link_order *p;
4028 /* We accumulate the debugging information counts in the symbolic
4030 symhdr = &debug->symbolic_header;
4032 symhdr->ilineMax = 0;
4036 symhdr->isymMax = 0;
4037 symhdr->ioptMax = 0;
4038 symhdr->iauxMax = 0;
4040 symhdr->issExtMax = 0;
4043 symhdr->iextMax = 0;
4045 /* We accumulate the debugging information itself in the debug_info
4048 debug->external_dnr = NULL;
4049 debug->external_pdr = NULL;
4050 debug->external_sym = NULL;
4051 debug->external_opt = NULL;
4052 debug->external_aux = NULL;
4054 debug->ssext = debug->ssext_end = NULL;
4055 debug->external_fdr = NULL;
4056 debug->external_rfd = NULL;
4057 debug->external_ext = debug->external_ext_end = NULL;
4059 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4060 if (handle == (PTR) NULL)
4063 /* Accumulate the debugging symbols from each input BFD. */
4064 for (input_bfd = info->input_bfds;
4065 input_bfd != (bfd *) NULL;
4066 input_bfd = input_bfd->link_next)
4070 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4072 /* Abitrarily set the symbolic header vstamp to the vstamp
4073 of the first object file in the link. */
4074 if (symhdr->vstamp == 0)
4076 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4077 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4081 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4082 debug, &backend->debug_swap,
4087 /* Combine the register masks. */
4088 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4089 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4090 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4091 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4092 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4093 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4096 /* Write out the external symbols. */
4097 ecoff_link_hash_traverse (ecoff_hash_table (info),
4098 ecoff_link_write_external,
4101 if (info->relocateable)
4103 /* We need to make a pass over the link_orders to count up the
4104 number of relocations we will need to output, so that we know
4105 how much space they will take up. */
4106 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4109 for (p = o->link_order_head;
4110 p != (struct bfd_link_order *) NULL;
4112 if (p->type == bfd_indirect_link_order)
4113 o->reloc_count += p->u.indirect.section->reloc_count;
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))
4194 if (! _bfd_default_link_order (abfd, info, o, p))
4200 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4205 /* Accumulate the debugging information for an input BFD into the
4206 output BFD. This must read in the symbolic information of the
4210 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4213 struct bfd_link_info *info;
4216 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4217 const struct ecoff_debug_swap * const swap =
4218 &ecoff_backend (input_bfd)->debug_swap;
4219 HDRR *symhdr = &debug->symbolic_header;
4222 #define READ(ptr, offset, count, size, type) \
4223 if (symhdr->count == 0) \
4224 debug->ptr = NULL; \
4227 debug->ptr = (type) malloc (size * symhdr->count); \
4228 if (debug->ptr == NULL) \
4230 bfd_set_error (bfd_error_no_memory); \
4232 goto return_something; \
4234 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4236 || (bfd_read (debug->ptr, size, symhdr->count, \
4237 input_bfd) != size * symhdr->count)) \
4240 goto return_something; \
4244 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
4245 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4246 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4247 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4248 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4249 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4251 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4252 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4253 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4256 /* We do not read the external strings or the external symbols. */
4258 ret = (bfd_ecoff_debug_accumulate
4259 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4260 &ecoff_backend (output_bfd)->debug_swap,
4261 input_bfd, debug, swap, info));
4264 if (debug->line != NULL)
4266 if (debug->external_dnr != NULL)
4267 free (debug->external_dnr);
4268 if (debug->external_pdr != NULL)
4269 free (debug->external_pdr);
4270 if (debug->external_sym != NULL)
4271 free (debug->external_sym);
4272 if (debug->external_opt != NULL)
4273 free (debug->external_opt);
4274 if (debug->external_aux != NULL)
4275 free (debug->external_aux);
4276 if (debug->ss != NULL)
4278 if (debug->external_fdr != NULL)
4279 free (debug->external_fdr);
4280 if (debug->external_rfd != NULL)
4281 free (debug->external_rfd);
4283 /* Make sure we don't accidentally follow one of these pointers on
4286 debug->external_dnr = NULL;
4287 debug->external_pdr = NULL;
4288 debug->external_sym = NULL;
4289 debug->external_opt = NULL;
4290 debug->external_aux = NULL;
4292 debug->external_fdr = NULL;
4293 debug->external_rfd = NULL;
4298 /* Put out information for an external symbol. These come only from
4302 ecoff_link_write_external (h, data)
4303 struct ecoff_link_hash_entry *h;
4306 bfd *output_bfd = (bfd *) data;
4308 /* FIXME: We should check if this symbol is being stripped. */
4310 if (h->root.written)
4313 if (h->abfd == (bfd *) NULL)
4316 h->esym.cobol_main = 0;
4317 h->esym.weakext = 0;
4318 h->esym.reserved = 0;
4319 h->esym.ifd = ifdNil;
4320 h->esym.asym.value = 0;
4321 /* FIXME: we can do better than this for st and sc. */
4322 h->esym.asym.st = stGlobal;
4323 h->esym.asym.sc = scAbs;
4324 h->esym.asym.reserved = 0;
4325 h->esym.asym.index = indexNil;
4327 else if (h->esym.ifd != -1)
4329 struct ecoff_debug_info *debug;
4331 /* Adjust the FDR index for the symbol by that used for the
4333 debug = &ecoff_data (h->abfd)->debug_info;
4334 BFD_ASSERT (h->esym.ifd >= 0
4335 && h->esym.ifd < debug->symbolic_header.ifdMax);
4336 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4339 switch (h->root.type)
4342 case bfd_link_hash_new:
4344 case bfd_link_hash_undefined:
4345 case bfd_link_hash_weak:
4346 if (h->esym.asym.st != scUndefined
4347 && h->esym.asym.st != scSUndefined)
4348 h->esym.asym.st = scUndefined;
4350 case bfd_link_hash_defined:
4351 if (h->esym.asym.sc == scUndefined
4352 || h->esym.asym.sc == scSUndefined)
4353 h->esym.asym.sc = scAbs;
4354 else if (h->esym.asym.sc == scCommon)
4355 h->esym.asym.sc = scBss;
4356 else if (h->esym.asym.sc == scSCommon)
4357 h->esym.asym.sc = scSBss;
4358 h->esym.asym.value = (h->root.u.def.value
4359 + h->root.u.def.section->output_section->vma
4360 + h->root.u.def.section->output_offset);
4362 case bfd_link_hash_common:
4363 if (h->esym.asym.sc != scCommon
4364 && h->esym.asym.sc != scSCommon)
4365 h->esym.asym.sc = scCommon;
4366 h->esym.asym.value = h->root.u.c.size;
4368 case bfd_link_hash_indirect:
4369 case bfd_link_hash_warning:
4370 /* FIXME: Ignore these for now. The circumstances under which
4371 they should be written out are not clear to me. */
4375 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4377 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4378 h->root.written = true;
4380 return (bfd_ecoff_debug_one_external
4381 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4382 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4386 /* Relocate and write an ECOFF section into an ECOFF output file. */
4389 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4391 struct bfd_link_info *info;
4392 asection *output_section;
4393 struct bfd_link_order *link_order;
4395 asection *input_section;
4397 bfd_size_type input_size;
4398 bfd_byte *contents = NULL;
4399 bfd_size_type external_reloc_size;
4400 bfd_size_type external_relocs_size;
4401 PTR external_relocs = NULL;
4403 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4405 if (link_order->size == 0)
4408 input_section = link_order->u.indirect.section;
4409 input_bfd = input_section->owner;
4411 BFD_ASSERT (input_section->output_section == output_section);
4412 BFD_ASSERT (input_section->output_offset == link_order->offset);
4413 BFD_ASSERT (bfd_section_size (input_bfd, input_section) == link_order->size);
4415 /* Get the section contents. */
4416 input_size = bfd_section_size (input_bfd, input_section);
4417 contents = (bfd_byte *) malloc (input_size);
4418 if (contents == NULL)
4420 bfd_set_error (bfd_error_no_memory);
4423 if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents,
4424 (file_ptr) 0, input_size))
4427 /* Get the relocs. */
4428 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4429 external_relocs_size = external_reloc_size * input_section->reloc_count;
4430 external_relocs = (PTR) malloc (external_relocs_size);
4431 if (external_relocs == NULL)
4433 bfd_set_error (bfd_error_no_memory);
4437 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4438 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4439 != external_relocs_size))
4442 /* Relocate the section contents. */
4443 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4444 (output_bfd, info, input_bfd, input_section, contents,
4448 /* Write out the relocated section. */
4449 if (! bfd_set_section_contents (output_bfd,
4452 input_section->output_offset,
4456 /* If we are producing relocateable output, the relocs were
4457 modified, and we write them out now. We use the reloc_count
4458 field of output_section to keep track of the number of relocs we
4459 have output so far. */
4460 if (info->relocateable)
4462 if (bfd_seek (output_bfd,
4463 (output_section->rel_filepos +
4464 output_section->reloc_count * external_reloc_size),
4466 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4467 != external_relocs_size))
4469 output_section->reloc_count += input_section->reloc_count;
4472 if (contents != NULL)
4474 if (external_relocs != NULL)
4475 free (external_relocs);
4479 if (contents != NULL)
4481 if (external_relocs != NULL)
4482 free (external_relocs);