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 void 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_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);
116 /* Tell the linker to leave this section completely alone. */
117 regsec->flags = SEC_SHARED_LIBRARY;
119 if (internal_a != (struct internal_aouthdr *) NULL)
123 ecoff->text_start = internal_a->text_start;
124 ecoff->text_end = internal_a->text_start + internal_a->tsize;
125 ecoff->gp = internal_a->gp_value;
126 ecoff->gprmask = internal_a->gprmask;
127 for (i = 0; i < 4; i++)
128 ecoff->cprmask[i] = internal_a->cprmask[i];
129 ecoff->fprmask = internal_a->fprmask;
130 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
131 abfd->flags |= D_PAGED;
134 /* It turns out that no special action is required by the MIPS or
135 Alpha ECOFF backends. They have different information in the
136 a.out header, but we just copy it all (e.g., gprmask, cprmask and
137 fprmask) and let the swapping routines ensure that only relevant
138 information is written out. */
143 /* This is a hook needed by SCO COFF, but we have nothing to do. */
147 ecoff_make_section_hook (abfd, name)
151 return (asection *) NULL;
154 /* Initialize a new section. */
157 ecoff_new_section_hook (abfd, section)
161 section->alignment_power = abfd->xvec->align_power_min;
163 if (strcmp (section->name, _TEXT) == 0)
164 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
165 else if (strcmp (section->name, _DATA) == 0
166 || strcmp (section->name, _SDATA) == 0)
167 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
168 else if (strcmp (section->name, _RDATA) == 0
169 || strcmp (section->name, _LIT8) == 0
170 || strcmp (section->name, _LIT4) == 0)
171 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
172 else if (strcmp (section->name, _BSS) == 0
173 || strcmp (section->name, _SBSS) == 0)
174 section->flags |= SEC_ALLOC;
175 else if (strcmp (section->name, REGINFO) == 0)
177 section->flags |= SEC_HAS_CONTENTS | SEC_NEVER_LOAD;
178 section->_raw_size = sizeof (struct ecoff_reginfo);
181 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
182 uncertain about .init on some systems and I don't know how shared
188 /* Determine the machine architecture and type. This is called from
189 the generic COFF routines. It is the inverse of ecoff_get_magic,
190 below. This could be an ECOFF backend routine, with one version
191 for each target, but there aren't all that many ECOFF targets. */
194 ecoff_set_arch_mach_hook (abfd, filehdr)
198 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
199 enum bfd_architecture arch;
202 switch (internal_f->f_magic)
205 case MIPS_MAGIC_LITTLE:
207 arch = bfd_arch_mips;
211 case MIPS_MAGIC_LITTLE2:
212 case MIPS_MAGIC_BIG2:
213 /* MIPS ISA level 2: the r6000 */
214 arch = bfd_arch_mips;
218 case MIPS_MAGIC_LITTLE3:
219 case MIPS_MAGIC_BIG3:
220 /* MIPS ISA level 3: the r4000 */
221 arch = bfd_arch_mips;
226 arch = bfd_arch_alpha;
231 arch = bfd_arch_obscure;
236 return bfd_default_set_arch_mach (abfd, arch, mach);
239 /* Get the magic number to use based on the architecture and machine.
240 This is the inverse of ecoff_set_arch_mach_hook, above. */
243 ecoff_get_magic (abfd)
248 switch (bfd_get_arch (abfd))
251 switch (bfd_get_mach (abfd))
256 big = MIPS_MAGIC_BIG;
257 little = MIPS_MAGIC_LITTLE;
261 big = MIPS_MAGIC_BIG2;
262 little = MIPS_MAGIC_LITTLE2;
266 big = MIPS_MAGIC_BIG3;
267 little = MIPS_MAGIC_LITTLE3;
271 return abfd->xvec->byteorder_big_p ? big : little;
282 /* Get the section s_flags to use for a section. */
285 ecoff_sec_to_styp_flags (name, flags)
293 if (strcmp (name, _TEXT) == 0)
295 else if (strcmp (name, _DATA) == 0)
297 else if (strcmp (name, _SDATA) == 0)
299 else if (strcmp (name, _RDATA) == 0)
301 else if (strcmp (name, _LITA) == 0)
303 else if (strcmp (name, _LIT8) == 0)
305 else if (strcmp (name, _LIT4) == 0)
307 else if (strcmp (name, _BSS) == 0)
309 else if (strcmp (name, _SBSS) == 0)
311 else if (strcmp (name, _INIT) == 0)
312 styp = STYP_ECOFF_INIT;
313 else if (strcmp (name, _FINI) == 0)
314 styp = STYP_ECOFF_FINI;
315 else if (strcmp (name, _PDATA) == 0)
317 else if (strcmp (name, _XDATA) == 0)
319 else if (flags & SEC_CODE)
321 else if (flags & SEC_DATA)
323 else if (flags & SEC_READONLY)
325 else if (flags & SEC_LOAD)
330 if (flags & SEC_NEVER_LOAD)
336 /* Get the BFD flags to use for a section. */
340 ecoff_styp_to_sec_flags (abfd, hdr)
344 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
345 long styp_flags = internal_s->s_flags;
346 flagword sec_flags=0;
348 if (styp_flags & STYP_NOLOAD)
349 sec_flags |= SEC_NEVER_LOAD;
351 /* For 386 COFF, at least, an unloadable text or data section is
352 actually a shared library section. */
353 if ((styp_flags & STYP_TEXT)
354 || (styp_flags & STYP_ECOFF_INIT)
355 || (styp_flags & STYP_ECOFF_FINI))
357 if (sec_flags & SEC_NEVER_LOAD)
358 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
360 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
362 else if ((styp_flags & STYP_DATA)
363 || (styp_flags & STYP_RDATA)
364 || (styp_flags & STYP_SDATA))
366 if (sec_flags & SEC_NEVER_LOAD)
367 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
369 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
370 if (styp_flags & STYP_RDATA)
371 sec_flags |= SEC_READONLY;
373 else if ((styp_flags & STYP_BSS)
374 || (styp_flags & STYP_SBSS))
376 sec_flags |= SEC_ALLOC;
378 else if (styp_flags & STYP_INFO)
380 sec_flags |= SEC_NEVER_LOAD;
382 else if ((styp_flags & STYP_LITA)
383 || (styp_flags & STYP_LIT8)
384 || (styp_flags & STYP_LIT4))
386 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
390 sec_flags |= SEC_ALLOC | SEC_LOAD;
396 /* Routines to swap auxiliary information in and out. I am assuming
397 that the auxiliary information format is always going to be target
400 /* Swap in a type information record.
401 BIGEND says whether AUX symbols are big-endian or little-endian; this
402 info comes from the file header record (fh-fBigendian). */
405 ecoff_swap_tir_in (bigend, ext_copy, intern)
407 struct tir_ext *ext_copy;
410 struct tir_ext ext[1];
412 *ext = *ext_copy; /* Make it reasonable to do in-place. */
414 /* now the fun stuff... */
416 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
417 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
418 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
419 >> TIR_BITS1_BT_SH_BIG;
420 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
421 >> TIR_BITS_TQ4_SH_BIG;
422 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
423 >> TIR_BITS_TQ5_SH_BIG;
424 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
425 >> TIR_BITS_TQ0_SH_BIG;
426 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
427 >> TIR_BITS_TQ1_SH_BIG;
428 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
429 >> TIR_BITS_TQ2_SH_BIG;
430 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
431 >> TIR_BITS_TQ3_SH_BIG;
433 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
434 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
435 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
436 >> TIR_BITS1_BT_SH_LITTLE;
437 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
438 >> TIR_BITS_TQ4_SH_LITTLE;
439 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
440 >> TIR_BITS_TQ5_SH_LITTLE;
441 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
442 >> TIR_BITS_TQ0_SH_LITTLE;
443 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
444 >> TIR_BITS_TQ1_SH_LITTLE;
445 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
446 >> TIR_BITS_TQ2_SH_LITTLE;
447 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
448 >> TIR_BITS_TQ3_SH_LITTLE;
452 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
457 /* Swap out a type information record.
458 BIGEND says whether AUX symbols are big-endian or little-endian; this
459 info comes from the file header record (fh-fBigendian). */
462 ecoff_swap_tir_out (bigend, intern_copy, ext)
469 *intern = *intern_copy; /* Make it reasonable to do in-place. */
471 /* now the fun stuff... */
473 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
474 | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
475 | ((intern->bt << TIR_BITS1_BT_SH_BIG)
476 & TIR_BITS1_BT_BIG));
477 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
479 | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
480 & TIR_BITS_TQ5_BIG));
481 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
483 | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
484 & TIR_BITS_TQ1_BIG));
485 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
487 | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
488 & TIR_BITS_TQ3_BIG));
490 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
491 | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
492 | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
493 & TIR_BITS1_BT_LITTLE));
494 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
495 & TIR_BITS_TQ4_LITTLE)
496 | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
497 & TIR_BITS_TQ5_LITTLE));
498 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
499 & TIR_BITS_TQ0_LITTLE)
500 | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
501 & TIR_BITS_TQ1_LITTLE));
502 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
503 & TIR_BITS_TQ2_LITTLE)
504 | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
505 & TIR_BITS_TQ3_LITTLE));
509 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
514 /* Swap in a relative symbol record. BIGEND says whether it is in
515 big-endian or little-endian format.*/
518 ecoff_swap_rndx_in (bigend, ext_copy, intern)
520 struct rndx_ext *ext_copy;
523 struct rndx_ext ext[1];
525 *ext = *ext_copy; /* Make it reasonable to do in-place. */
527 /* now the fun stuff... */
529 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
530 | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
531 >> RNDX_BITS1_RFD_SH_BIG);
532 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
533 << RNDX_BITS1_INDEX_SH_LEFT_BIG)
534 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
535 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
537 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
538 | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
539 << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
540 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
541 >> RNDX_BITS1_INDEX_SH_LITTLE)
542 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
543 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
547 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
552 /* Swap out a relative symbol record. BIGEND says whether it is in
553 big-endian or little-endian format.*/
556 ecoff_swap_rndx_out (bigend, intern_copy, ext)
559 struct rndx_ext *ext;
563 *intern = *intern_copy; /* Make it reasonable to do in-place. */
565 /* now the fun stuff... */
567 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
568 ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
569 & RNDX_BITS1_RFD_BIG)
570 | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
571 & RNDX_BITS1_INDEX_BIG));
572 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
573 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
575 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
576 ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
577 & RNDX_BITS1_RFD_LITTLE)
578 | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
579 & RNDX_BITS1_INDEX_LITTLE));
580 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
581 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
585 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
590 /* Read in the symbolic header for an ECOFF object file. */
593 ecoff_slurp_symbolic_header (abfd)
596 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
597 bfd_size_type external_hdr_size;
599 HDRR *internal_symhdr;
601 /* See if we've already read it in. */
602 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
603 backend->debug_swap.sym_magic)
606 /* See whether there is a symbolic header. */
607 if (ecoff_data (abfd)->sym_filepos == 0)
609 bfd_get_symcount (abfd) = 0;
613 /* At this point bfd_get_symcount (abfd) holds the number of symbols
614 as read from the file header, but on ECOFF this is always the
615 size of the symbolic information header. It would be cleaner to
616 handle this when we first read the file in coffgen.c. */
617 external_hdr_size = backend->debug_swap.external_hdr_size;
618 if (bfd_get_symcount (abfd) != external_hdr_size)
620 bfd_error = bad_value;
624 /* Read the symbolic information header. */
625 raw = (PTR) alloca ((size_t) external_hdr_size);
626 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
627 || (bfd_read (raw, external_hdr_size, 1, abfd)
628 != external_hdr_size))
630 bfd_error = system_call_error;
633 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
634 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
636 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
638 bfd_error = bad_value;
642 /* Now we can get the correct number of symbols. */
643 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
644 + internal_symhdr->iextMax);
649 /* Read in and swap the important symbolic information for an ECOFF
650 object file. This is called by gdb. */
653 ecoff_slurp_symbolic_info (abfd)
656 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
657 HDRR *internal_symhdr;
658 bfd_size_type raw_base;
659 bfd_size_type raw_size;
661 bfd_size_type external_fdr_size;
665 bfd_size_type raw_end;
666 bfd_size_type cb_end;
668 /* Check whether we've already gotten it, and whether there's any to
670 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
672 if (ecoff_data (abfd)->sym_filepos == 0)
674 bfd_get_symcount (abfd) = 0;
678 if (! ecoff_slurp_symbolic_header (abfd))
681 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
683 /* Read all the symbolic information at once. */
684 raw_base = (ecoff_data (abfd)->sym_filepos
685 + backend->debug_swap.external_hdr_size);
687 /* Alpha ecoff makes the determination of raw_size difficult. It has
688 an undocumented debug data section between the symhdr and the first
689 documented section. And the ordering of the sections varies between
690 statically and dynamically linked executables.
691 If bfd supports SEEK_END someday, this code could be simplified. */
695 #define UPDATE_RAW_END(start, count, size) \
696 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
697 if (cb_end > raw_end) \
700 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
701 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
702 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
703 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
704 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
705 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
706 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
707 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
708 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
709 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
710 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
712 #undef UPDATE_RAW_END
714 raw_size = raw_end - raw_base;
717 ecoff_data (abfd)->sym_filepos = 0;
720 raw = (PTR) bfd_alloc (abfd, raw_size);
723 bfd_error = no_memory;
727 (ecoff_data (abfd)->sym_filepos
728 + backend->debug_swap.external_hdr_size),
730 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
732 bfd_error = system_call_error;
733 bfd_release (abfd, raw);
737 ecoff_data (abfd)->raw_syments = raw;
739 /* Get pointers for the numeric offsets in the HDRR structure. */
740 #define FIX(off1, off2, type) \
741 if (internal_symhdr->off1 == 0) \
742 ecoff_data (abfd)->debug_info.off2 = (type) NULL; \
744 ecoff_data (abfd)->debug_info.off2 = (type) ((char *) raw \
745 + internal_symhdr->off1 \
747 FIX (cbLineOffset, line, unsigned char *);
748 FIX (cbDnOffset, external_dnr, PTR);
749 FIX (cbPdOffset, external_pdr, PTR);
750 FIX (cbSymOffset, external_sym, PTR);
751 FIX (cbOptOffset, external_opt, PTR);
752 FIX (cbAuxOffset, external_aux, union aux_ext *);
753 FIX (cbSsOffset, ss, char *);
754 FIX (cbSsExtOffset, ssext, char *);
755 FIX (cbFdOffset, external_fdr, PTR);
756 FIX (cbRfdOffset, external_rfd, PTR);
757 FIX (cbExtOffset, external_ext, PTR);
760 /* I don't want to always swap all the data, because it will just
761 waste time and most programs will never look at it. The only
762 time the linker needs most of the debugging information swapped
763 is when linking big-endian and little-endian MIPS object files
764 together, which is not a common occurrence.
766 We need to look at the fdr to deal with a lot of information in
767 the symbols, so we swap them here. */
768 ecoff_data (abfd)->debug_info.fdr =
769 (struct fdr *) bfd_alloc (abfd,
770 (internal_symhdr->ifdMax *
771 sizeof (struct fdr)));
772 if (ecoff_data (abfd)->debug_info.fdr == NULL)
774 bfd_error = no_memory;
777 external_fdr_size = backend->debug_swap.external_fdr_size;
778 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
779 fraw_src = (char *) ecoff_data (abfd)->debug_info.external_fdr;
780 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
781 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
782 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
787 /* ECOFF symbol table routines. The ECOFF symbol table is described
788 in gcc/mips-tfile.c. */
790 /* ECOFF uses two common sections. One is the usual one, and the
791 other is for small objects. All the small objects are kept
792 together, and then referenced via the gp pointer, which yields
793 faster assembler code. This is what we use for the small common
795 static asection ecoff_scom_section;
796 static asymbol ecoff_scom_symbol;
797 static asymbol *ecoff_scom_symbol_ptr;
799 /* Create an empty symbol. */
802 ecoff_make_empty_symbol (abfd)
805 ecoff_symbol_type *new;
807 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
808 if (new == (ecoff_symbol_type *) NULL)
810 bfd_error = no_memory;
811 return (asymbol *) NULL;
813 memset (new, 0, sizeof *new);
814 new->symbol.section = (asection *) NULL;
815 new->fdr = (FDR *) NULL;
818 new->symbol.the_bfd = abfd;
822 /* Set the BFD flags and section for an ECOFF symbol. */
825 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, indirect_ptr_ptr)
830 asymbol **indirect_ptr_ptr;
832 asym->the_bfd = abfd;
833 asym->value = ecoff_sym->value;
834 asym->section = &bfd_debug_section;
837 /* An indirect symbol requires two consecutive stabs symbols. */
838 if (*indirect_ptr_ptr != (asymbol *) NULL)
840 BFD_ASSERT (ECOFF_IS_STAB (ecoff_sym));
842 /* @@ Stuffing pointers into integers is a no-no.
843 We can usually get away with it if the integer is
844 large enough though. */
845 if (sizeof (asym) > sizeof (bfd_vma))
847 (*indirect_ptr_ptr)->value = (bfd_vma) asym;
849 asym->flags = BSF_DEBUGGING;
850 asym->section = &bfd_und_section;
851 *indirect_ptr_ptr = NULL;
855 if (ECOFF_IS_STAB (ecoff_sym)
856 && (ECOFF_UNMARK_STAB (ecoff_sym->index) | N_EXT) == (N_INDR | N_EXT))
858 asym->flags = BSF_DEBUGGING | BSF_INDIRECT;
859 asym->section = &bfd_ind_section;
860 /* Pass this symbol on to the next call to this function. */
861 *indirect_ptr_ptr = asym;
865 /* Most symbol types are just for debugging. */
866 switch (ecoff_sym->st)
875 if (ECOFF_IS_STAB (ecoff_sym))
877 asym->flags = BSF_DEBUGGING;
882 asym->flags = BSF_DEBUGGING;
887 asym->flags = BSF_EXPORT | BSF_GLOBAL;
889 asym->flags = BSF_LOCAL;
890 switch (ecoff_sym->sc)
893 /* Used for compiler generated labels. Leave them in the
894 debugging section, and mark them as local. If BSF_DEBUGGING
895 is set, then nm does not display them for some reason. If no
896 flags are set then the linker whines about them. */
897 asym->flags = BSF_LOCAL;
900 asym->section = bfd_make_section_old_way (abfd, ".text");
901 asym->value -= asym->section->vma;
904 asym->section = bfd_make_section_old_way (abfd, ".data");
905 asym->value -= asym->section->vma;
908 asym->section = bfd_make_section_old_way (abfd, ".bss");
909 asym->value -= asym->section->vma;
912 asym->flags = BSF_DEBUGGING;
915 asym->section = &bfd_abs_section;
918 asym->section = &bfd_und_section;
928 asym->flags = BSF_DEBUGGING;
931 asym->section = bfd_make_section_old_way (abfd, ".sdata");
932 asym->value -= asym->section->vma;
935 asym->section = bfd_make_section_old_way (abfd, ".sbss");
936 asym->value -= asym->section->vma;
939 asym->section = bfd_make_section_old_way (abfd, ".rdata");
940 asym->value -= asym->section->vma;
943 asym->flags = BSF_DEBUGGING;
946 if (asym->value > ecoff_data (abfd)->gp_size)
948 asym->section = &bfd_com_section;
954 if (ecoff_scom_section.name == NULL)
956 /* Initialize the small common section. */
957 ecoff_scom_section.name = SCOMMON;
958 ecoff_scom_section.flags = SEC_IS_COMMON;
959 ecoff_scom_section.output_section = &ecoff_scom_section;
960 ecoff_scom_section.symbol = &ecoff_scom_symbol;
961 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
962 ecoff_scom_symbol.name = SCOMMON;
963 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
964 ecoff_scom_symbol.section = &ecoff_scom_section;
965 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
967 asym->section = &ecoff_scom_section;
972 asym->flags = BSF_DEBUGGING;
975 asym->section = &bfd_und_section;
980 asym->section = bfd_make_section_old_way (abfd, ".init");
981 asym->value -= asym->section->vma;
986 asym->flags = BSF_DEBUGGING;
989 asym->section = bfd_make_section_old_way (abfd, ".fini");
990 asym->value -= asym->section->vma;
996 /* Look for special constructors symbols and make relocation entries
997 in a special construction section. These are produced by the
998 -fgnu-linker argument to g++. */
999 if (ECOFF_IS_STAB (ecoff_sym))
1001 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
1013 arelent_chain *reloc_chain;
1014 unsigned int bitsize;
1016 /* Get a section with the same name as the symbol (usually
1017 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
1018 name ___CTOR_LIST (three underscores). We need
1019 __CTOR_LIST (two underscores), since ECOFF doesn't use
1020 a leading underscore. This should be handled by gcc,
1021 but instead we do it here. Actually, this should all
1022 be done differently anyhow. */
1023 name = bfd_asymbol_name (asym);
1024 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
1029 section = bfd_get_section_by_name (abfd, name);
1030 if (section == (asection *) NULL)
1034 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
1035 strcpy (copy, name);
1036 section = bfd_make_section (abfd, copy);
1039 /* Build a reloc pointing to this constructor. */
1041 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1042 reloc_chain->relent.sym_ptr_ptr =
1043 bfd_get_section (asym)->symbol_ptr_ptr;
1044 reloc_chain->relent.address = section->_raw_size;
1045 reloc_chain->relent.addend = asym->value;
1046 reloc_chain->relent.howto =
1047 ecoff_backend (abfd)->constructor_reloc;
1049 /* Set up the constructor section to hold the reloc. */
1050 section->flags = SEC_CONSTRUCTOR;
1051 ++section->reloc_count;
1053 /* Constructor sections must be rounded to a boundary
1054 based on the bitsize. These are not real sections--
1055 they are handled specially by the linker--so the ECOFF
1056 16 byte alignment restriction does not apply. */
1057 bitsize = ecoff_backend (abfd)->constructor_bitsize;
1058 section->alignment_power = 1;
1059 while ((1 << section->alignment_power) < bitsize / 8)
1060 ++section->alignment_power;
1062 reloc_chain->next = section->constructor_chain;
1063 section->constructor_chain = reloc_chain;
1064 section->_raw_size += bitsize / 8;
1066 /* Mark the symbol as a constructor. */
1067 asym->flags |= BSF_CONSTRUCTOR;
1074 /* Read an ECOFF symbol table. */
1077 ecoff_slurp_symbol_table (abfd)
1080 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1081 const bfd_size_type external_ext_size
1082 = backend->debug_swap.external_ext_size;
1083 const bfd_size_type external_sym_size
1084 = backend->debug_swap.external_sym_size;
1085 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
1086 = backend->debug_swap.swap_ext_in;
1087 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
1088 = backend->debug_swap.swap_sym_in;
1089 bfd_size_type internal_size;
1090 ecoff_symbol_type *internal;
1091 ecoff_symbol_type *internal_ptr;
1092 asymbol *indirect_ptr;
1098 /* If we've already read in the symbol table, do nothing. */
1099 if (ecoff_data (abfd)->canonical_symbols != NULL)
1102 /* Get the symbolic information. */
1103 if (ecoff_slurp_symbolic_info (abfd) == false)
1105 if (bfd_get_symcount (abfd) == 0)
1108 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1109 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1110 if (internal == NULL)
1112 bfd_error = no_memory;
1116 internal_ptr = internal;
1117 indirect_ptr = NULL;
1118 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
1119 eraw_end = (eraw_src
1120 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
1121 * external_ext_size));
1122 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
1126 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
1127 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
1128 + internal_esym.asym.iss);
1129 ecoff_set_symbol_info (abfd, &internal_esym.asym,
1130 &internal_ptr->symbol, 1, &indirect_ptr);
1131 /* The alpha uses a negative ifd field for section symbols. */
1132 if (internal_esym.ifd >= 0)
1133 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
1134 + internal_esym.ifd);
1136 internal_ptr->fdr = NULL;
1137 internal_ptr->local = false;
1138 internal_ptr->native = (PTR) eraw_src;
1140 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1142 /* The local symbols must be accessed via the fdr's, because the
1143 string and aux indices are relative to the fdr information. */
1144 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
1145 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
1146 for (; fdr_ptr < fdr_end; fdr_ptr++)
1151 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
1152 + fdr_ptr->isymBase * external_sym_size);
1153 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1155 lraw_src < lraw_end;
1156 lraw_src += external_sym_size, internal_ptr++)
1160 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1161 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
1163 + internal_sym.iss);
1164 ecoff_set_symbol_info (abfd, &internal_sym,
1165 &internal_ptr->symbol, 0, &indirect_ptr);
1166 internal_ptr->fdr = fdr_ptr;
1167 internal_ptr->local = true;
1168 internal_ptr->native = (PTR) lraw_src;
1171 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1173 ecoff_data (abfd)->canonical_symbols = internal;
1178 /* Return the amount of space needed for the canonical symbols. */
1181 ecoff_get_symtab_upper_bound (abfd)
1184 if (ecoff_slurp_symbolic_info (abfd) == false
1185 || bfd_get_symcount (abfd) == 0)
1188 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1191 /* Get the canonical symbols. */
1194 ecoff_get_symtab (abfd, alocation)
1196 asymbol **alocation;
1198 unsigned int counter = 0;
1199 ecoff_symbol_type *symbase;
1200 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1202 if (ecoff_slurp_symbol_table (abfd) == false
1203 || bfd_get_symcount (abfd) == 0)
1206 symbase = ecoff_data (abfd)->canonical_symbols;
1207 while (counter < bfd_get_symcount (abfd))
1209 *(location++) = symbase++;
1212 *location++ = (ecoff_symbol_type *) NULL;
1213 return bfd_get_symcount (abfd);
1216 /* Turn ECOFF type information into a printable string.
1217 ecoff_emit_aggregate and ecoff_type_to_string are from
1218 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1220 /* Write aggregate information to a string. */
1223 ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1230 int ifd = rndx->rfd;
1231 int indx = rndx->index;
1232 int sym_base, ss_base;
1238 sym_base = ecoff_data (abfd)->debug_info.fdr[ifd].isymBase;
1239 ss_base = ecoff_data (abfd)->debug_info.fdr[ifd].issBase;
1241 if (indx == indexNil)
1242 name = "/* no name */";
1245 const struct ecoff_debug_swap * const debug_swap
1246 = &ecoff_backend (abfd)->debug_swap;
1250 (*debug_swap->swap_sym_in)
1252 ((char *) ecoff_data (abfd)->debug_info.external_sym
1253 + indx * debug_swap->external_sym_size),
1255 name = ecoff_data (abfd)->debug_info.ss + ss_base + sym.iss;
1259 "%s %s { ifd = %d, index = %ld }",
1262 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax));
1265 /* Convert the type information to string format. */
1268 ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1270 union aux_ext *aux_ptr;
1282 unsigned int basic_type;
1284 static char buffer1[1024];
1285 static char buffer2[1024];
1290 for (i = 0; i < 7; i++)
1292 qualifiers[i].low_bound = 0;
1293 qualifiers[i].high_bound = 0;
1294 qualifiers[i].stride = 0;
1297 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1298 return "-1 (no type)";
1299 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1301 basic_type = u.ti.bt;
1302 qualifiers[0].type = u.ti.tq0;
1303 qualifiers[1].type = u.ti.tq1;
1304 qualifiers[2].type = u.ti.tq2;
1305 qualifiers[3].type = u.ti.tq3;
1306 qualifiers[4].type = u.ti.tq4;
1307 qualifiers[5].type = u.ti.tq5;
1308 qualifiers[6].type = tqNil;
1311 * Go get the basic type.
1315 case btNil: /* undefined */
1319 case btAdr: /* address - integer same size as pointer */
1320 strcpy (p1, "address");
1323 case btChar: /* character */
1324 strcpy (p1, "char");
1327 case btUChar: /* unsigned character */
1328 strcpy (p1, "unsigned char");
1331 case btShort: /* short */
1332 strcpy (p1, "short");
1335 case btUShort: /* unsigned short */
1336 strcpy (p1, "unsigned short");
1339 case btInt: /* int */
1343 case btUInt: /* unsigned int */
1344 strcpy (p1, "unsigned int");
1347 case btLong: /* long */
1348 strcpy (p1, "long");
1351 case btULong: /* unsigned long */
1352 strcpy (p1, "unsigned long");
1355 case btFloat: /* float (real) */
1356 strcpy (p1, "float");
1359 case btDouble: /* Double (real) */
1360 strcpy (p1, "double");
1363 /* Structures add 1-2 aux words:
1364 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1365 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1367 case btStruct: /* Structure (Record) */
1368 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1369 ecoff_emit_aggregate (abfd, p1, &rndx,
1370 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1372 indx++; /* skip aux words */
1375 /* Unions add 1-2 aux words:
1376 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1377 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1379 case btUnion: /* Union */
1380 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1381 ecoff_emit_aggregate (abfd, p1, &rndx,
1382 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1384 indx++; /* skip aux words */
1387 /* Enumerations add 1-2 aux words:
1388 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1389 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1391 case btEnum: /* Enumeration */
1392 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1393 ecoff_emit_aggregate (abfd, p1, &rndx,
1394 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1396 indx++; /* skip aux words */
1399 case btTypedef: /* defined via a typedef, isymRef points */
1400 strcpy (p1, "typedef");
1403 case btRange: /* subrange of int */
1404 strcpy (p1, "subrange");
1407 case btSet: /* pascal sets */
1411 case btComplex: /* fortran complex */
1412 strcpy (p1, "complex");
1415 case btDComplex: /* fortran double complex */
1416 strcpy (p1, "double complex");
1419 case btIndirect: /* forward or unnamed typedef */
1420 strcpy (p1, "forward/unamed typedef");
1423 case btFixedDec: /* Fixed Decimal */
1424 strcpy (p1, "fixed decimal");
1427 case btFloatDec: /* Float Decimal */
1428 strcpy (p1, "float decimal");
1431 case btString: /* Varying Length Character String */
1432 strcpy (p1, "string");
1435 case btBit: /* Aligned Bit String */
1439 case btPicture: /* Picture */
1440 strcpy (p1, "picture");
1443 case btVoid: /* Void */
1444 strcpy (p1, "void");
1448 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1452 p1 += strlen (buffer1);
1455 * If this is a bitfield, get the bitsize.
1461 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1462 sprintf (p1, " : %d", bitsize);
1463 p1 += strlen (buffer1);
1468 * Deal with any qualifiers.
1470 if (qualifiers[0].type != tqNil)
1473 * Snarf up any array bounds in the correct order. Arrays
1474 * store 5 successive words in the aux. table:
1475 * word 0 RNDXR to type of the bounds (ie, int)
1476 * word 1 Current file descriptor index
1478 * word 3 high bound (or -1 if [])
1479 * word 4 stride size in bits
1481 for (i = 0; i < 7; i++)
1483 if (qualifiers[i].type == tqArray)
1485 qualifiers[i].low_bound =
1486 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1487 qualifiers[i].high_bound =
1488 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1489 qualifiers[i].stride =
1490 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1496 * Now print out the qualifiers.
1498 for (i = 0; i < 6; i++)
1500 switch (qualifiers[i].type)
1507 strcpy (p2, "ptr to ");
1508 p2 += sizeof ("ptr to ")-1;
1512 strcpy (p2, "volatile ");
1513 p2 += sizeof ("volatile ")-1;
1517 strcpy (p2, "far ");
1518 p2 += sizeof ("far ")-1;
1522 strcpy (p2, "func. ret. ");
1523 p2 += sizeof ("func. ret. ");
1528 int first_array = i;
1531 /* Print array bounds reversed (ie, in the order the C
1532 programmer writes them). C is such a fun language.... */
1534 while (i < 5 && qualifiers[i+1].type == tqArray)
1537 for (j = i; j >= first_array; j--)
1539 strcpy (p2, "array [");
1540 p2 += sizeof ("array [")-1;
1541 if (qualifiers[j].low_bound != 0)
1543 "%ld:%ld {%ld bits}",
1544 (long) qualifiers[j].low_bound,
1545 (long) qualifiers[j].high_bound,
1546 (long) qualifiers[j].stride);
1548 else if (qualifiers[j].high_bound != -1)
1551 (long) (qualifiers[j].high_bound + 1),
1552 (long) (qualifiers[j].stride));
1555 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1558 strcpy (p2, "] of ");
1559 p2 += sizeof ("] of ")-1;
1567 strcpy (p2, buffer1);
1571 /* Return information about ECOFF symbol SYMBOL in RET. */
1575 ecoff_get_symbol_info (abfd, symbol, ret)
1576 bfd *abfd; /* Ignored. */
1580 bfd_symbol_info (symbol, ret);
1583 /* Print information about an ECOFF symbol. */
1586 ecoff_print_symbol (abfd, filep, symbol, how)
1590 bfd_print_symbol_type how;
1592 const struct ecoff_debug_swap * const debug_swap
1593 = &ecoff_backend (abfd)->debug_swap;
1594 FILE *file = (FILE *)filep;
1598 case bfd_print_symbol_name:
1599 fprintf (file, "%s", symbol->name);
1601 case bfd_print_symbol_more:
1602 if (ecoffsymbol (symbol)->local)
1606 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1608 fprintf (file, "ecoff local ");
1609 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1610 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1611 (unsigned) ecoff_sym.sc);
1617 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1619 fprintf (file, "ecoff extern ");
1620 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1621 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1622 (unsigned) ecoff_ext.asym.sc);
1625 case bfd_print_symbol_all:
1626 /* Print out the symbols in a reasonable way */
1635 if (ecoffsymbol (symbol)->local)
1637 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1640 pos = ((((char *) ecoffsymbol (symbol)->native
1641 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1642 / debug_swap->external_sym_size)
1643 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1650 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1653 pos = (((char *) ecoffsymbol (symbol)->native
1654 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1655 / debug_swap->external_ext_size);
1656 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1657 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1658 weakext = ecoff_ext.weakext ? 'w' : ' ';
1661 fprintf (file, "[%3d] %c ",
1663 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1664 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1665 (unsigned) ecoff_ext.asym.st,
1666 (unsigned) ecoff_ext.asym.sc,
1667 (unsigned) ecoff_ext.asym.index,
1668 jmptbl, cobol_main, weakext,
1671 if (ecoffsymbol (symbol)->fdr != NULL
1672 && ecoff_ext.asym.index != indexNil)
1676 bfd_size_type sym_base;
1677 union aux_ext *aux_base;
1679 indx = ecoff_ext.asym.index;
1681 /* sym_base is used to map the fdr relative indices which
1682 appear in the file to the position number which we are
1684 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1685 if (ecoffsymbol (symbol)->local)
1687 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1689 /* aux_base is the start of the aux entries for this file;
1690 asym.index is an offset from this. */
1691 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1692 + ecoffsymbol (symbol)->fdr->iauxBase);
1694 /* The aux entries are stored in host byte order; the
1695 order is indicated by a bit in the fdr. */
1696 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1698 /* This switch is basically from gcc/mips-tdump.c */
1699 switch (ecoff_ext.asym.st)
1707 fprintf (file, "\n End+1 symbol: %ld",
1708 (long) (indx + sym_base));
1712 if (ecoff_ext.asym.sc == scText
1713 || ecoff_ext.asym.sc == scInfo)
1714 fprintf (file, "\n First symbol: %ld",
1715 (long) (indx + sym_base));
1717 fprintf (file, "\n First symbol: %ld",
1718 (long) (AUX_GET_ISYM (bigendian,
1719 &aux_base[ecoff_ext.asym.index])
1725 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1727 else if (ecoffsymbol (symbol)->local)
1728 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1729 (long) (AUX_GET_ISYM (bigendian,
1730 &aux_base[ecoff_ext.asym.index])
1732 ecoff_type_to_string (abfd, aux_base, indx + 1,
1735 fprintf (file, "\n Local symbol: %ld",
1738 + (ecoff_data (abfd)
1739 ->debug_info.symbolic_header.iextMax)));
1743 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1744 fprintf (file, "\n Type: %s",
1745 ecoff_type_to_string (abfd, aux_base, indx,
1755 /* Read in the relocs for a section. */
1758 ecoff_slurp_reloc_table (abfd, section, symbols)
1763 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1764 arelent *internal_relocs;
1765 bfd_size_type external_reloc_size;
1766 bfd_size_type external_relocs_size;
1767 char *external_relocs;
1771 if (section->relocation != (arelent *) NULL
1772 || section->reloc_count == 0
1773 || (section->flags & SEC_CONSTRUCTOR) != 0)
1776 if (ecoff_slurp_symbol_table (abfd) == false)
1779 internal_relocs = (arelent *) bfd_alloc (abfd,
1781 * section->reloc_count));
1782 external_reloc_size = backend->external_reloc_size;
1783 external_relocs_size = external_reloc_size * section->reloc_count;
1784 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1785 if (internal_relocs == (arelent *) NULL
1786 || external_relocs == (char *) NULL)
1788 bfd_error = no_memory;
1791 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1793 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1794 != external_relocs_size)
1796 bfd_error = system_call_error;
1800 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1802 struct internal_reloc intern;
1804 (*backend->swap_reloc_in) (abfd,
1805 external_relocs + i * external_reloc_size,
1808 if (intern.r_extern)
1810 /* r_symndx is an index into the external symbols. */
1811 BFD_ASSERT (intern.r_symndx >= 0
1813 < (ecoff_data (abfd)
1814 ->debug_info.symbolic_header.iextMax)));
1815 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1818 else if (intern.r_symndx == RELOC_SECTION_NONE
1819 || intern.r_symndx == RELOC_SECTION_ABS)
1821 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1826 CONST char *sec_name;
1829 /* r_symndx is a section key. */
1830 switch (intern.r_symndx)
1832 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1833 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1834 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1835 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1836 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1837 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1838 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1839 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1840 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1841 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1842 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1843 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1844 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1848 sec = bfd_get_section_by_name (abfd, sec_name);
1849 if (sec == (asection *) NULL)
1851 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1853 rptr->addend = - bfd_get_section_vma (abfd, sec);
1856 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1858 /* Let the backend select the howto field and do any other
1859 required processing. */
1860 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1863 bfd_release (abfd, external_relocs);
1865 section->relocation = internal_relocs;
1870 /* Get a canonical list of relocs. */
1873 ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1881 if (section->flags & SEC_CONSTRUCTOR)
1883 arelent_chain *chain;
1885 /* This section has relocs made up by us, not the file, so take
1886 them out of their chain and place them into the data area
1888 for (count = 0, chain = section->constructor_chain;
1889 count < section->reloc_count;
1890 count++, chain = chain->next)
1891 *relptr++ = &chain->relent;
1897 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1900 tblptr = section->relocation;
1901 if (tblptr == (arelent *) NULL)
1904 for (count = 0; count < section->reloc_count; count++)
1905 *relptr++ = tblptr++;
1908 *relptr = (arelent *) NULL;
1910 return section->reloc_count;
1913 /* Provided a BFD, a section and an offset into the section, calculate
1914 and return the name of the source file and the line nearest to the
1919 ecoff_find_nearest_line (abfd,
1928 asymbol **ignore_symbols;
1930 CONST char **filename_ptr;
1931 CONST char **functionname_ptr;
1932 unsigned int *retline_ptr;
1934 const struct ecoff_debug_swap * const debug_swap
1935 = &ecoff_backend (abfd)->debug_swap;
1940 bfd_size_type external_pdr_size;
1944 unsigned char *line_ptr;
1945 unsigned char *line_end;
1948 /* If we're not in the .text section, we don't have any line
1950 if (strcmp (section->name, _TEXT) != 0
1951 || offset < ecoff_data (abfd)->text_start
1952 || offset >= ecoff_data (abfd)->text_end)
1955 /* Make sure we have the FDR's. */
1956 if (ecoff_slurp_symbolic_info (abfd) == false
1957 || bfd_get_symcount (abfd) == 0)
1960 /* Each file descriptor (FDR) has a memory address. Here we track
1961 down which FDR we want. The FDR's are stored in increasing
1962 memory order. If speed is ever important, this can become a
1963 binary search. We must ignore FDR's with no PDR entries; they
1964 will have the adr of the FDR before or after them. */
1965 fdr_start = ecoff_data (abfd)->debug_info.fdr;
1966 fdr_end = fdr_start + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
1967 fdr_hold = (FDR *) NULL;
1968 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1970 if (fdr_ptr->cpd == 0)
1972 if (offset < fdr_ptr->adr)
1976 if (fdr_hold == (FDR *) NULL)
1980 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
1981 have an address, which is relative to the FDR address, and are
1982 also stored in increasing memory order. */
1983 offset -= fdr_ptr->adr;
1984 external_pdr_size = debug_swap->external_pdr_size;
1985 pdr_ptr = ((char *) ecoff_data (abfd)->debug_info.external_pdr
1986 + fdr_ptr->ipdFirst * external_pdr_size);
1987 pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
1988 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
1990 /* The address of the first PDR is an offset which applies to the
1991 addresses of all the PDR's. */
1994 for (pdr_ptr += external_pdr_size;
1996 pdr_ptr += external_pdr_size)
1998 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
1999 if (offset < pdr.adr)
2003 /* Now we can look for the actual line number. The line numbers are
2004 stored in a very funky format, which I won't try to describe.
2005 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2006 we want; we need this to compute line_end. */
2007 line_end = ecoff_data (abfd)->debug_info.line;
2008 if (pdr_ptr == pdr_end)
2009 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2011 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2013 /* Now change pdr and pdr_ptr to the one we want. */
2014 pdr_ptr -= external_pdr_size;
2015 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2019 line_ptr = (ecoff_data (abfd)->debug_info.line
2020 + fdr_ptr->cbLineOffset
2021 + pdr.cbLineOffset);
2022 while (line_ptr < line_end)
2027 delta = *line_ptr >> 4;
2030 count = (*line_ptr & 0xf) + 1;
2034 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2035 if (delta >= 0x8000)
2040 if (offset < count * 4)
2042 offset -= count * 4;
2045 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2046 at least according to gdb/mipsread.c. */
2047 if (fdr_ptr->rss == -1)
2049 *filename_ptr = NULL;
2051 *functionname_ptr = NULL;
2056 (*debug_swap->swap_ext_in)
2058 ((char *) ecoff_data (abfd)->debug_info.external_ext
2059 + pdr.isym * debug_swap->external_ext_size),
2061 *functionname_ptr = (ecoff_data (abfd)->debug_info.ssext
2062 + proc_ext.asym.iss);
2069 *filename_ptr = (ecoff_data (abfd)->debug_info.ss
2072 (*debug_swap->swap_sym_in)
2074 ((char *) ecoff_data (abfd)->debug_info.external_sym
2075 + (fdr_ptr->isymBase + pdr.isym) * debug_swap->external_sym_size),
2077 *functionname_ptr = (ecoff_data (abfd)->debug_info.ss
2081 if (lineno == ilineNil)
2083 *retline_ptr = lineno;
2087 /* Set the architecture. The supported architecture is stored in the
2088 backend pointer. We always set the architecture anyhow, since many
2089 callers ignore the return value. */
2092 ecoff_set_arch_mach (abfd, arch, machine)
2094 enum bfd_architecture arch;
2095 unsigned long machine;
2097 bfd_default_set_arch_mach (abfd, arch, machine);
2098 return arch == ecoff_backend (abfd)->arch;
2101 /* Get the size of the section headers. We do not output the .reginfo
2106 ecoff_sizeof_headers (abfd, reloc)
2114 for (current = abfd->sections;
2115 current != (asection *)NULL;
2116 current = current->next)
2117 if (strcmp (current->name, REGINFO) != 0)
2120 return (bfd_coff_filhsz (abfd)
2121 + bfd_coff_aoutsz (abfd)
2122 + c * bfd_coff_scnhsz (abfd));
2125 /* Get the contents of a section. This is where we handle reading the
2126 .reginfo section, which implicitly holds the contents of an
2127 ecoff_reginfo structure. */
2130 ecoff_get_section_contents (abfd, section, location, offset, count)
2135 bfd_size_type count;
2137 ecoff_data_type *tdata = ecoff_data (abfd);
2138 struct ecoff_reginfo s;
2141 if (strcmp (section->name, REGINFO) != 0)
2142 return bfd_generic_get_section_contents (abfd, section, location,
2145 s.gp_value = tdata->gp;
2146 s.gprmask = tdata->gprmask;
2147 for (i = 0; i < 4; i++)
2148 s.cprmask[i] = tdata->cprmask[i];
2149 s.fprmask = tdata->fprmask;
2151 /* bfd_get_section_contents has already checked that the offset and
2152 size is reasonable. We don't have to worry about swapping or any
2153 such thing; the .reginfo section is defined such that the
2154 contents are an ecoff_reginfo structure as seen on the host. */
2155 memcpy (location, ((char *) &s) + offset, (size_t) count);
2159 /* Calculate the file position for each section, and set
2163 ecoff_compute_section_file_positions (abfd)
2171 sofar = ecoff_sizeof_headers (abfd, false);
2174 for (current = abfd->sections;
2175 current != (asection *) NULL;
2176 current = current->next)
2178 /* Only deal with sections which have contents */
2179 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) == 0
2180 || strcmp (current->name, REGINFO) == 0)
2183 /* On Ultrix, the data sections in an executable file must be
2184 aligned to a page boundary within the file. This does not
2185 affect the section size, though. FIXME: Does this work for
2186 other platforms? It requires some modification for the
2187 Alpha, because .rdata on the Alpha goes with the text, not
2189 if ((abfd->flags & EXEC_P) != 0
2190 && (abfd->flags & D_PAGED) != 0
2191 && first_data != false
2192 && (current->flags & SEC_CODE) == 0
2193 && (! ecoff_backend (abfd)->rdata_in_text
2194 || strcmp (current->name, _RDATA) != 0)
2195 && strcmp (current->name, _PDATA) != 0)
2197 const bfd_vma round = ecoff_backend (abfd)->round;
2199 sofar = (sofar + round - 1) &~ (round - 1);
2203 /* Align the sections in the file to the same boundary on
2204 which they are aligned in virtual memory. */
2206 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2208 current->filepos = sofar;
2210 sofar += current->_raw_size;
2212 /* make sure that this section is of the right size too */
2214 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2215 current->_raw_size += sofar - old_sofar;
2218 ecoff_data (abfd)->reloc_filepos = sofar;
2221 /* Determine the location of the relocs for all the sections in the
2224 static bfd_size_type
2225 ecoff_compute_reloc_file_positions (abfd)
2228 const bfd_size_type external_reloc_size =
2229 ecoff_backend (abfd)->external_reloc_size;
2230 file_ptr reloc_base;
2231 bfd_size_type reloc_size;
2234 if (! abfd->output_has_begun)
2235 ecoff_compute_section_file_positions (abfd);
2237 reloc_base = ecoff_data (abfd)->reloc_filepos;
2240 for (current = abfd->sections;
2241 current != (asection *)NULL;
2242 current = current->next)
2244 if (strcmp (current->name, REGINFO) == 0)
2246 if (current->reloc_count == 0)
2247 current->rel_filepos = 0;
2250 bfd_size_type relsize;
2252 current->rel_filepos = reloc_base;
2253 relsize = current->reloc_count * external_reloc_size;
2254 reloc_size += relsize;
2255 reloc_base += relsize;
2262 /* Set the contents of a section. This is where we handle setting the
2263 contents of the .reginfo section, which implicitly holds a
2264 ecoff_reginfo structure. */
2267 ecoff_set_section_contents (abfd, section, location, offset, count)
2272 bfd_size_type count;
2274 /* This must be done first, because bfd_set_section_contents is
2275 going to set output_has_begun to true. */
2276 if (abfd->output_has_begun == false)
2277 ecoff_compute_section_file_positions (abfd);
2282 if (strcmp (section->name, REGINFO) == 0)
2284 ecoff_data_type *tdata = ecoff_data (abfd);
2285 struct ecoff_reginfo s;
2288 /* If the caller is only changing part of the structure, we must
2289 retrieve the current information before the memcpy. */
2290 if (offset != 0 || count != sizeof (struct ecoff_reginfo))
2292 s.gp_value = tdata->gp;
2293 s.gprmask = tdata->gprmask;
2294 for (i = 0; i < 4; i++)
2295 s.cprmask[i] = tdata->cprmask[i];
2296 s.fprmask = tdata->fprmask;
2299 /* bfd_set_section_contents has already checked that the offset
2300 and size is reasonable. We don't have to worry about
2301 swapping or any such thing; the .reginfo section is defined
2302 such that the contents are an ecoff_reginfo structure as seen
2304 memcpy (((char *) &s) + offset, location, (size_t) count);
2306 tdata->gp = s.gp_value;
2307 tdata->gprmask = s.gprmask;
2308 for (i = 0; i < 4; i++)
2309 tdata->cprmask[i] = s.cprmask[i];
2310 tdata->fprmask = s.fprmask;
2315 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2316 || bfd_write (location, 1, count, abfd) != count)
2322 /* Get ECOFF EXTR information for an external symbol. This function
2323 is passed to bfd_ecoff_debug_externals. */
2326 ecoff_get_extr (sym, esym)
2330 ecoff_symbol_type *ecoff_sym_ptr;
2333 /* Don't include debugging, local or section symbols. */
2334 if ((sym->flags & BSF_DEBUGGING) != 0
2335 || (sym->flags & BSF_LOCAL) != 0
2336 || (sym->flags & BSF_SECTION_SYM) != 0)
2339 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2340 || ecoffsymbol (sym)->native == NULL)
2343 esym->cobol_main = 0;
2347 /* FIXME: we can do better than this for st and sc. */
2348 esym->asym.st = stGlobal;
2349 esym->asym.sc = scAbs;
2350 esym->asym.reserved = 0;
2351 esym->asym.index = indexNil;
2355 ecoff_sym_ptr = ecoffsymbol (sym);
2357 if (ecoff_sym_ptr->local)
2360 input_bfd = bfd_asymbol_bfd (sym);
2361 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2362 (input_bfd, ecoff_sym_ptr->native, esym);
2364 /* If the symbol was defined by the linker, then esym will be
2365 undefined but sym will not be. Get a better class for such a
2367 if ((esym->asym.sc == scUndefined
2368 || esym->asym.sc == scSUndefined)
2369 && bfd_get_section (sym) != &bfd_und_section)
2370 esym->asym.sc = scAbs;
2372 /* Adjust the FDR index for the symbol by that used for the input
2374 esym->ifd += ecoff_data (input_bfd)->debug_info.ifdbase;
2379 /* Set the external symbol index. This routine is passed to
2380 bfd_ecoff_debug_externals. */
2383 ecoff_set_index (sym, indx)
2387 ecoff_set_sym_index (sym, indx);
2390 /* Write out an ECOFF file. */
2393 ecoff_write_object_contents (abfd)
2396 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2397 const bfd_vma round = backend->round;
2398 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2399 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2400 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2401 const bfd_size_type external_hdr_size
2402 = backend->debug_swap.external_hdr_size;
2403 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2404 void (* const adjust_reloc_out) PARAMS ((bfd *,
2406 struct internal_reloc *))
2407 = backend->adjust_reloc_out;
2408 void (* const swap_reloc_out) PARAMS ((bfd *,
2409 const struct internal_reloc *,
2411 = backend->swap_reloc_out;
2412 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2413 HDRR * const symhdr = &debug->symbolic_header;
2417 bfd_size_type reloc_size;
2418 unsigned long text_size;
2419 unsigned long text_start;
2420 unsigned long data_size;
2421 unsigned long data_start;
2422 unsigned long bss_size;
2424 struct internal_filehdr internal_f;
2425 struct internal_aouthdr internal_a;
2428 bfd_error = system_call_error;
2430 /* Determine where the sections and relocs will go in the output
2432 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2435 for (current = abfd->sections;
2436 current != (asection *)NULL;
2437 current = current->next)
2439 if (strcmp (current->name, REGINFO) == 0)
2441 current->target_index = count;
2445 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2447 /* At least on Ultrix, the symbol table of an executable file must
2448 be aligned to a page boundary. FIXME: Is this true on other
2450 if ((abfd->flags & EXEC_P) != 0
2451 && (abfd->flags & D_PAGED) != 0)
2452 sym_base = (sym_base + round - 1) &~ (round - 1);
2454 ecoff_data (abfd)->sym_filepos = sym_base;
2456 if ((abfd->flags & D_PAGED) != 0)
2457 text_size = ecoff_sizeof_headers (abfd, false);
2465 /* Write section headers to the file. */
2467 buff = (PTR) alloca (scnhsz);
2468 internal_f.f_nscns = 0;
2469 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2471 for (current = abfd->sections;
2472 current != (asection *) NULL;
2473 current = current->next)
2475 struct internal_scnhdr section;
2478 if (strcmp (current->name, REGINFO) == 0)
2480 BFD_ASSERT (current->reloc_count == 0);
2484 ++internal_f.f_nscns;
2486 strncpy (section.s_name, current->name, sizeof section.s_name);
2488 /* FIXME: is this correct for shared libraries? I think it is
2489 but I have no platform to check. Ian Lance Taylor. */
2490 vma = bfd_get_section_vma (abfd, current);
2491 if (strcmp (current->name, _LIB) == 0)
2492 section.s_vaddr = 0;
2494 section.s_vaddr = vma;
2496 section.s_paddr = vma;
2497 section.s_size = bfd_get_section_size_before_reloc (current);
2499 /* If this section is unloadable then the scnptr will be 0. */
2500 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2501 section.s_scnptr = 0;
2503 section.s_scnptr = current->filepos;
2504 section.s_relptr = current->rel_filepos;
2506 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2507 object file produced by the assembler is supposed to point to
2508 information about how much room is required by objects of
2509 various different sizes. I think this only matters if we
2510 want the linker to compute the best size to use, or
2511 something. I don't know what happens if the information is
2513 section.s_lnnoptr = 0;
2515 section.s_nreloc = current->reloc_count;
2516 section.s_nlnno = 0;
2517 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2520 bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff);
2521 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2524 if ((section.s_flags & STYP_TEXT) != 0
2525 || ((section.s_flags & STYP_RDATA) != 0
2526 && backend->rdata_in_text)
2527 || strcmp (current->name, _PDATA) == 0)
2529 text_size += bfd_get_section_size_before_reloc (current);
2530 if (text_start == 0 || text_start > vma)
2533 else if ((section.s_flags & STYP_RDATA) != 0
2534 || (section.s_flags & STYP_DATA) != 0
2535 || (section.s_flags & STYP_LITA) != 0
2536 || (section.s_flags & STYP_LIT8) != 0
2537 || (section.s_flags & STYP_LIT4) != 0
2538 || (section.s_flags & STYP_SDATA) != 0
2539 || strcmp (current->name, _XDATA) == 0)
2541 data_size += bfd_get_section_size_before_reloc (current);
2542 if (data_start == 0 || data_start > vma)
2545 else if ((section.s_flags & STYP_BSS) != 0
2546 || (section.s_flags & STYP_SBSS) != 0)
2547 bss_size += bfd_get_section_size_before_reloc (current);
2552 /* Set up the file header. */
2554 internal_f.f_magic = ecoff_get_magic (abfd);
2556 /* We will NOT put a fucking timestamp in the header here. Every
2557 time you put it back, I will come in and take it out again. I'm
2558 sorry. This field does not belong here. We fill it with a 0 so
2559 it compares the same but is not a reasonable time. --
2561 internal_f.f_timdat = 0;
2563 if (bfd_get_symcount (abfd) != 0)
2565 /* The ECOFF f_nsyms field is not actually the number of
2566 symbols, it's the size of symbolic information header. */
2567 internal_f.f_nsyms = external_hdr_size;
2568 internal_f.f_symptr = sym_base;
2572 internal_f.f_nsyms = 0;
2573 internal_f.f_symptr = 0;
2576 internal_f.f_opthdr = aoutsz;
2578 internal_f.f_flags = F_LNNO;
2579 if (reloc_size == 0)
2580 internal_f.f_flags |= F_RELFLG;
2581 if (bfd_get_symcount (abfd) == 0)
2582 internal_f.f_flags |= F_LSYMS;
2583 if (abfd->flags & EXEC_P)
2584 internal_f.f_flags |= F_EXEC;
2586 if (! abfd->xvec->byteorder_big_p)
2587 internal_f.f_flags |= F_AR32WR;
2589 internal_f.f_flags |= F_AR32W;
2591 /* Set up the ``optional'' header. */
2592 if ((abfd->flags & D_PAGED) != 0)
2593 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2595 internal_a.magic = ECOFF_AOUT_OMAGIC;
2597 /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
2598 linker happy. But, is it right? */
2599 internal_a.vstamp = 0x20a;
2601 /* At least on Ultrix, these have to be rounded to page boundaries.
2602 FIXME: Is this true on other platforms? */
2603 if ((abfd->flags & D_PAGED) != 0)
2605 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2606 internal_a.text_start = text_start &~ (round - 1);
2607 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2608 internal_a.data_start = data_start &~ (round - 1);
2612 internal_a.tsize = text_size;
2613 internal_a.text_start = text_start;
2614 internal_a.dsize = data_size;
2615 internal_a.data_start = data_start;
2618 /* On Ultrix, the initial portions of the .sbss and .bss segments
2619 are at the end of the data section. The bsize field in the
2620 optional header records how many bss bytes are required beyond
2621 those in the data section. The value is not rounded to a page
2623 if (bss_size < internal_a.dsize - data_size)
2626 bss_size -= internal_a.dsize - data_size;
2627 internal_a.bsize = bss_size;
2628 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2630 internal_a.entry = bfd_get_start_address (abfd);
2632 internal_a.gp_value = ecoff_data (abfd)->gp;
2634 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2635 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2636 for (i = 0; i < 4; i++)
2637 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2639 /* Write out the file header and the optional header. */
2641 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2644 buff = (PTR) alloca (filhsz);
2645 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2646 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2649 buff = (PTR) alloca (aoutsz);
2650 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2651 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2654 /* Build the external symbol information. This must be done before
2655 writing out the relocs so that we know the symbol indices. The
2656 condition checks makes sure this object was not created by
2657 ecoff_bfd_final_link, since if it was we do not want to tamper
2658 with the external symbols. */
2659 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL
2660 || bfd_get_symcount (abfd) == 0)
2662 symhdr->iextMax = 0;
2663 symhdr->issExtMax = 0;
2664 debug->external_ext = debug->external_ext_end = NULL;
2665 debug->ssext = debug->ssext_end = NULL;
2666 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2667 (((abfd->flags & EXEC_P) == 0)
2669 ecoff_get_extr, ecoff_set_index)
2673 /* Write out the relocs. */
2674 for (current = abfd->sections;
2675 current != (asection *) NULL;
2676 current = current->next)
2678 arelent **reloc_ptr_ptr;
2679 arelent **reloc_end;
2682 if (current->reloc_count == 0)
2685 buff = bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2688 bfd_error = no_memory;
2692 reloc_ptr_ptr = current->orelocation;
2693 reloc_end = reloc_ptr_ptr + current->reloc_count;
2694 out_ptr = (char *) buff;
2696 reloc_ptr_ptr < reloc_end;
2697 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2701 struct internal_reloc in;
2703 memset (&in, 0, sizeof in);
2705 reloc = *reloc_ptr_ptr;
2706 sym = *reloc->sym_ptr_ptr;
2708 in.r_vaddr = (reloc->address
2709 + bfd_get_section_vma (abfd, current));
2710 in.r_type = reloc->howto->type;
2712 if ((sym->flags & BSF_SECTION_SYM) == 0)
2714 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2721 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2722 if (strcmp (name, ".text") == 0)
2723 in.r_symndx = RELOC_SECTION_TEXT;
2724 else if (strcmp (name, ".rdata") == 0)
2725 in.r_symndx = RELOC_SECTION_RDATA;
2726 else if (strcmp (name, ".data") == 0)
2727 in.r_symndx = RELOC_SECTION_DATA;
2728 else if (strcmp (name, ".sdata") == 0)
2729 in.r_symndx = RELOC_SECTION_SDATA;
2730 else if (strcmp (name, ".sbss") == 0)
2731 in.r_symndx = RELOC_SECTION_SBSS;
2732 else if (strcmp (name, ".bss") == 0)
2733 in.r_symndx = RELOC_SECTION_BSS;
2734 else if (strcmp (name, ".init") == 0)
2735 in.r_symndx = RELOC_SECTION_INIT;
2736 else if (strcmp (name, ".lit8") == 0)
2737 in.r_symndx = RELOC_SECTION_LIT8;
2738 else if (strcmp (name, ".lit4") == 0)
2739 in.r_symndx = RELOC_SECTION_LIT4;
2740 else if (strcmp (name, ".xdata") == 0)
2741 in.r_symndx = RELOC_SECTION_XDATA;
2742 else if (strcmp (name, ".pdata") == 0)
2743 in.r_symndx = RELOC_SECTION_PDATA;
2744 else if (strcmp (name, ".fini") == 0)
2745 in.r_symndx = RELOC_SECTION_FINI;
2746 else if (strcmp (name, ".lita") == 0)
2747 in.r_symndx = RELOC_SECTION_LITA;
2748 else if (strcmp (name, "*ABS*") == 0)
2749 in.r_symndx = RELOC_SECTION_ABS;
2755 (*adjust_reloc_out) (abfd, reloc, &in);
2757 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2760 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2762 if (bfd_write (buff, external_reloc_size, current->reloc_count, abfd)
2763 != external_reloc_size * current->reloc_count)
2765 bfd_release (abfd, buff);
2769 /* Write out the symbolic debugging information. */
2770 if (bfd_get_symcount (abfd) > 0)
2772 /* Write out the debugging information. */
2773 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2774 ecoff_data (abfd)->sym_filepos)
2778 else if ((abfd->flags & EXEC_P) != 0
2779 && (abfd->flags & D_PAGED) != 0)
2783 /* A demand paged executable must occupy an even number of
2785 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2788 if (bfd_read (&c, 1, 1, abfd) == 0)
2790 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2793 if (bfd_write (&c, 1, 1, abfd) != 1)
2800 /* Archive handling. ECOFF uses what appears to be a unique type of
2801 archive header (armap). The byte ordering of the armap and the
2802 contents are encoded in the name of the armap itself. At least for
2803 now, we only support archives with the same byte ordering in the
2804 armap and the contents.
2806 The first four bytes in the armap are the number of symbol
2807 definitions. This is always a power of two.
2809 This is followed by the symbol definitions. Each symbol definition
2810 occupies 8 bytes. The first four bytes are the offset from the
2811 start of the armap strings to the null-terminated string naming
2812 this symbol. The second four bytes are the file offset to the
2813 archive member which defines this symbol. If the second four bytes
2814 are 0, then this is not actually a symbol definition, and it should
2817 The symbols are hashed into the armap with a closed hashing scheme.
2818 See the functions below for the details of the algorithm.
2820 After the symbol definitions comes four bytes holding the size of
2821 the string table, followed by the string table itself. */
2823 /* The name of an archive headers looks like this:
2824 __________E[BL]E[BL]_ (with a trailing space).
2825 The trailing space is changed to an X if the archive is changed to
2826 indicate that the armap is out of date.
2828 The Alpha seems to use ________64E[BL]E[BL]_. */
2830 #define ARMAP_BIG_ENDIAN 'B'
2831 #define ARMAP_LITTLE_ENDIAN 'L'
2832 #define ARMAP_MARKER 'E'
2833 #define ARMAP_START_LENGTH 10
2834 #define ARMAP_HEADER_MARKER_INDEX 10
2835 #define ARMAP_HEADER_ENDIAN_INDEX 11
2836 #define ARMAP_OBJECT_MARKER_INDEX 12
2837 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2838 #define ARMAP_END_INDEX 14
2839 #define ARMAP_END "_ "
2841 /* This is a magic number used in the hashing algorithm. */
2842 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2844 /* This returns the hash value to use for a string. It also sets
2845 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2846 is the number of entries in the hash table, and HLOG is the log
2850 ecoff_armap_hash (s, rehash, size, hlog)
2852 unsigned int *rehash;
2860 hash = ((hash >> 27) | (hash << 5)) + *s++;
2861 hash *= ARMAP_HASH_MAGIC;
2862 *rehash = (hash & (size - 1)) | 1;
2863 return hash >> (32 - hlog);
2866 /* Read in the armap. */
2869 ecoff_slurp_armap (abfd)
2874 struct areltdata *mapdata;
2875 bfd_size_type parsed_size;
2877 struct artdata *ardata;
2880 struct symdef *symdef_ptr;
2883 /* Get the name of the first element. */
2884 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2890 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
2892 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2893 standard COFF armap. We could move the ECOFF armap stuff into
2894 bfd_slurp_armap, but that seems inappropriate since no other
2895 target uses this format. Instead, we check directly for a COFF
2897 if (strncmp (nextname, "/ ", 16) == 0)
2898 return bfd_slurp_armap (abfd);
2900 /* See if the first element is an armap. */
2901 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2902 ARMAP_START_LENGTH) != 0
2903 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2904 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2905 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2906 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2907 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2908 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2909 || strncmp (nextname + ARMAP_END_INDEX,
2910 ARMAP_END, sizeof ARMAP_END - 1) != 0)
2912 bfd_has_map (abfd) = false;
2916 /* Make sure we have the right byte ordering. */
2917 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2918 ^ (abfd->xvec->header_byteorder_big_p != false))
2919 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2920 ^ (abfd->xvec->byteorder_big_p != false)))
2922 bfd_error = wrong_format;
2926 /* Read in the armap. */
2927 ardata = bfd_ardata (abfd);
2928 mapdata = _bfd_snarf_ar_hdr (abfd);
2929 if (mapdata == (struct areltdata *) NULL)
2931 parsed_size = mapdata->parsed_size;
2932 bfd_release (abfd, (PTR) mapdata);
2934 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
2935 if (raw_armap == (char *) NULL)
2937 bfd_error = no_memory;
2941 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
2943 bfd_error = malformed_archive;
2944 bfd_release (abfd, (PTR) raw_armap);
2948 ardata->tdata = (PTR) raw_armap;
2950 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
2952 ardata->symdef_count = 0;
2953 ardata->cache = (struct ar_cache *) NULL;
2955 /* This code used to overlay the symdefs over the raw archive data,
2956 but that doesn't work on a 64 bit host. */
2958 stringbase = raw_armap + count * 8 + 8;
2960 #ifdef CHECK_ARMAP_HASH
2964 /* Double check that I have the hashing algorithm right by making
2965 sure that every symbol can be looked up successfully. */
2967 for (i = 1; i < count; i <<= 1)
2969 BFD_ASSERT (i == count);
2971 raw_ptr = raw_armap + 4;
2972 for (i = 0; i < count; i++, raw_ptr += 8)
2974 unsigned int name_offset, file_offset;
2975 unsigned int hash, rehash, srch;
2977 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
2978 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
2979 if (file_offset == 0)
2981 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
2986 /* See if we can rehash to this location. */
2987 for (srch = (hash + rehash) & (count - 1);
2988 srch != hash && srch != i;
2989 srch = (srch + rehash) & (count - 1))
2990 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
2992 BFD_ASSERT (srch == i);
2996 #endif /* CHECK_ARMAP_HASH */
2998 raw_ptr = raw_armap + 4;
2999 for (i = 0; i < count; i++, raw_ptr += 8)
3000 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3001 ++ardata->symdef_count;
3003 symdef_ptr = ((struct symdef *)
3005 ardata->symdef_count * sizeof (struct symdef)));
3006 ardata->symdefs = (carsym *) symdef_ptr;
3008 raw_ptr = raw_armap + 4;
3009 for (i = 0; i < count; i++, raw_ptr += 8)
3011 unsigned int name_offset, file_offset;
3013 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3014 if (file_offset == 0)
3016 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3017 symdef_ptr->s.name = stringbase + name_offset;
3018 symdef_ptr->file_offset = file_offset;
3022 ardata->first_file_filepos = bfd_tell (abfd);
3023 /* Pad to an even boundary. */
3024 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3026 bfd_has_map (abfd) = true;
3031 /* Write out an armap. */
3034 ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3036 unsigned int elength;
3038 unsigned int orl_count;
3041 unsigned int hashsize, hashlog;
3042 unsigned int symdefsize;
3044 unsigned int stringsize;
3045 unsigned int mapsize;
3048 struct stat statbuf;
3051 bfd_byte *hashtable;
3055 /* Ultrix appears to use as a hash table size the least power of two
3056 greater than twice the number of entries. */
3057 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3059 hashsize = 1 << hashlog;
3061 symdefsize = hashsize * 8;
3063 stringsize = stridx + padit;
3065 /* Include 8 bytes to store symdefsize and stringsize in output. */
3066 mapsize = symdefsize + stringsize + 8;
3068 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3070 memset ((PTR) &hdr, 0, sizeof hdr);
3072 /* Work out the ECOFF armap name. */
3073 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3074 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3075 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3076 (abfd->xvec->header_byteorder_big_p
3078 : ARMAP_LITTLE_ENDIAN);
3079 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3080 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3081 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3082 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3084 /* Write the timestamp of the archive header to be just a little bit
3085 later than the timestamp of the file, otherwise the linker will
3086 complain that the index is out of date. Actually, the Ultrix
3087 linker just checks the archive name; the GNU linker may check the
3089 stat (abfd->filename, &statbuf);
3090 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3092 /* The DECstation uses zeroes for the uid, gid and mode of the
3094 hdr.ar_uid[0] = '0';
3095 hdr.ar_gid[0] = '0';
3096 hdr.ar_mode[0] = '0';
3098 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3100 hdr.ar_fmag[0] = '`';
3101 hdr.ar_fmag[1] = '\n';
3103 /* Turn all null bytes in the header into spaces. */
3104 for (i = 0; i < sizeof (struct ar_hdr); i++)
3105 if (((char *)(&hdr))[i] == '\0')
3106 (((char *)(&hdr))[i]) = ' ';
3108 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3109 != sizeof (struct ar_hdr))
3112 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3113 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3116 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3118 current = abfd->archive_head;
3120 for (i = 0; i < orl_count; i++)
3122 unsigned int hash, rehash;
3124 /* Advance firstreal to the file position of this archive
3126 if (((bfd *) map[i].pos) != last_elt)
3130 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3131 firstreal += firstreal % 2;
3132 current = current->next;
3134 while (current != (bfd *) map[i].pos);
3139 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3140 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3144 /* The desired slot is already taken. */
3145 for (srch = (hash + rehash) & (hashsize - 1);
3147 srch = (srch + rehash) & (hashsize - 1))
3148 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3151 BFD_ASSERT (srch != hash);
3156 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3157 (PTR) (hashtable + hash * 8));
3158 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3159 (PTR) (hashtable + hash * 8 + 4));
3162 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3165 bfd_release (abfd, hashtable);
3167 /* Now write the strings. */
3168 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3169 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3171 for (i = 0; i < orl_count; i++)
3175 len = strlen (*map[i].name) + 1;
3176 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3180 /* The spec sez this should be a newline. But in order to be
3181 bug-compatible for DECstation ar we use a null. */
3184 if (bfd_write ("", 1, 1, abfd) != 1)
3191 /* See whether this BFD is an archive. If it is, read in the armap
3192 and the extended name table. */
3195 ecoff_archive_p (abfd)
3198 char armag[SARMAG + 1];
3200 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3201 || strncmp (armag, ARMAG, SARMAG) != 0)
3203 bfd_error = wrong_format;
3204 return (bfd_target *) NULL;
3207 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3208 involves a cast, we can't do it as the left operand of
3210 abfd->tdata.aout_ar_data =
3211 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3213 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3215 bfd_error = no_memory;
3216 return (bfd_target *) NULL;
3219 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3220 bfd_ardata (abfd)->cache = NULL;
3221 bfd_ardata (abfd)->archive_head = NULL;
3222 bfd_ardata (abfd)->symdefs = NULL;
3223 bfd_ardata (abfd)->extended_names = NULL;
3224 bfd_ardata (abfd)->tdata = NULL;
3226 if (ecoff_slurp_armap (abfd) == false
3227 || ecoff_slurp_extended_name_table (abfd) == false)
3229 bfd_release (abfd, bfd_ardata (abfd));
3230 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3231 return (bfd_target *) NULL;
3237 /* ECOFF linker code. */
3239 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3240 PARAMS ((struct bfd_hash_entry *entry,
3241 struct bfd_hash_table *table,
3242 const char *string));
3243 static boolean ecoff_link_add_archive_symbols
3244 PARAMS ((bfd *, struct bfd_link_info *));
3245 static boolean ecoff_link_check_archive_element
3246 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3247 static boolean ecoff_link_add_object_symbols
3248 PARAMS ((bfd *, struct bfd_link_info *));
3249 static boolean ecoff_link_add_externals
3250 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3252 /* Routine to create an entry in an ECOFF link hash table. */
3254 static struct bfd_hash_entry *
3255 ecoff_link_hash_newfunc (entry, table, string)
3256 struct bfd_hash_entry *entry;
3257 struct bfd_hash_table *table;
3260 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3262 /* Allocate the structure if it has not already been allocated by a
3264 if (ret == (struct ecoff_link_hash_entry *) NULL)
3265 ret = ((struct ecoff_link_hash_entry *)
3266 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3268 /* Call the allocation method of the superclass. */
3269 ret = ((struct ecoff_link_hash_entry *)
3270 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3273 /* Set local fields. */
3276 memset (&ret->esym, 0, sizeof ret->esym);
3278 return (struct bfd_hash_entry *) ret;
3281 /* Create an ECOFF link hash table. */
3283 struct bfd_link_hash_table *
3284 ecoff_bfd_link_hash_table_create (abfd)
3287 struct ecoff_link_hash_table *ret;
3289 ret = ((struct ecoff_link_hash_table *)
3290 bfd_xmalloc (sizeof (struct ecoff_link_hash_table)));
3291 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3292 ecoff_link_hash_newfunc))
3295 return (struct bfd_link_hash_table *) NULL;
3300 /* Look up an entry in an ECOFF link hash table. */
3302 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3303 ((struct ecoff_link_hash_entry *) \
3304 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3306 /* Traverse an ECOFF link hash table. */
3308 #define ecoff_link_hash_traverse(table, func, info) \
3309 (bfd_link_hash_traverse \
3311 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3314 /* Get the ECOFF link hash table from the info structure. This is
3317 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3319 /* Given an ECOFF BFD, add symbols to the global hash table as
3323 ecoff_bfd_link_add_symbols (abfd, info)
3325 struct bfd_link_info *info;
3327 switch (bfd_get_format (abfd))
3330 return ecoff_link_add_object_symbols (abfd, info);
3332 return ecoff_link_add_archive_symbols (abfd, info);
3334 bfd_error = wrong_format;
3339 /* Add the symbols from an archive file to the global hash table.
3340 This looks through the undefined symbols, looks each one up in the
3341 archive hash table, and adds any associated object file. We do not
3342 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3343 already have a hash table, so there is no reason to construct
3347 ecoff_link_add_archive_symbols (abfd, info)
3349 struct bfd_link_info *info;
3351 const bfd_byte *raw_armap;
3352 struct bfd_link_hash_entry **pundef;
3353 unsigned int armap_count;
3354 unsigned int armap_log;
3356 const bfd_byte *hashtable;
3357 const char *stringbase;
3359 if (! bfd_has_map (abfd))
3361 bfd_error = no_symbols;
3365 /* If we don't have any raw data for this archive, as can happen on
3366 Irix 4.0.5F, we call the generic routine.
3367 FIXME: We should be more clever about this, since someday tdata
3368 may get to something for a generic archive. */
3369 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3370 if (raw_armap == (bfd_byte *) NULL)
3371 return (_bfd_generic_link_add_archive_symbols
3372 (abfd, info, ecoff_link_check_archive_element));
3374 armap_count = bfd_h_get_32 (abfd, raw_armap);
3377 for (i = 1; i < armap_count; i <<= 1)
3379 BFD_ASSERT (i == armap_count);
3381 hashtable = raw_armap + 4;
3382 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3384 /* Look through the list of undefined symbols. */
3385 pundef = &info->hash->undefs;
3386 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3388 struct bfd_link_hash_entry *h;
3389 unsigned int hash, rehash;
3390 unsigned int file_offset;
3396 /* When a symbol is defined, it is not necessarily removed from
3398 if (h->type != bfd_link_hash_undefined
3399 && h->type != bfd_link_hash_common)
3401 /* Remove this entry from the list, for general cleanliness
3402 and because we are going to look through the list again
3403 if we search any more libraries. We can't remove the
3404 entry if it is the tail, because that would lose any
3405 entries we add to the list later on. */
3406 if (*pundef != info->hash->undefs_tail)
3407 *pundef = (*pundef)->next;
3409 pundef = &(*pundef)->next;
3413 /* Native ECOFF linkers do not pull in archive elements merely
3414 to satisfy common definitions, so neither do we. We leave
3415 them on the list, though, in case we are linking against some
3416 other object format. */
3417 if (h->type != bfd_link_hash_undefined)
3419 pundef = &(*pundef)->next;
3423 /* Look for this symbol in the archive hash table. */
3424 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3427 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3428 if (file_offset == 0)
3430 /* Nothing in this slot. */
3431 pundef = &(*pundef)->next;
3435 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3436 if (name[0] != h->root.string[0]
3437 || strcmp (name, h->root.string) != 0)
3442 /* That was the wrong symbol. Try rehashing. */
3444 for (srch = (hash + rehash) & (armap_count - 1);
3446 srch = (srch + rehash) & (armap_count - 1))
3448 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3449 if (file_offset == 0)
3451 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3452 if (name[0] == h->root.string[0]
3453 && strcmp (name, h->root.string) == 0)
3462 pundef = &(*pundef)->next;
3469 element = _bfd_get_elt_at_filepos (abfd, file_offset);
3470 if (element == (bfd *) NULL)
3473 if (! bfd_check_format (element, bfd_object))
3476 /* Unlike the generic linker, we know that this element provides
3477 a definition for an undefined symbol and we know that we want
3478 to include it. We don't need to check anything. */
3479 if (! (*info->callbacks->add_archive_element) (info, element, name))
3481 if (! ecoff_link_add_object_symbols (element, info))
3484 pundef = &(*pundef)->next;
3490 /* This is called if we used _bfd_generic_link_add_archive_symbols
3491 because we were not dealing with an ECOFF archive. */
3494 ecoff_link_check_archive_element (abfd, info, pneeded)
3496 struct bfd_link_info *info;
3499 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3500 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3501 = backend->debug_swap.swap_ext_in;
3503 bfd_size_type external_ext_size;
3512 if (! ecoff_slurp_symbolic_header (abfd))
3515 /* If there are no symbols, we don't want it. */
3516 if (bfd_get_symcount (abfd) == 0)
3519 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3521 /* Read in the external symbols and external strings. */
3522 external_ext_size = backend->debug_swap.external_ext_size;
3523 esize = symhdr->iextMax * external_ext_size;
3524 external_ext = (PTR) alloca (esize);
3525 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3526 || bfd_read (external_ext, 1, esize, abfd) != esize)
3529 ssext = (char *) alloca (symhdr->issExtMax);
3530 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3531 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
3534 /* Look through the external symbols to see if they define some
3535 symbol that is currently undefined. */
3536 ext_ptr = (char *) external_ext;
3537 ext_end = ext_ptr + esize;
3538 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3543 struct bfd_link_hash_entry *h;
3545 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3547 /* See if this symbol defines something. */
3548 if (esym.asym.st != stGlobal
3549 && esym.asym.st != stLabel
3550 && esym.asym.st != stProc)
3553 switch (esym.asym.sc)
3576 name = ssext + esym.asym.iss;
3577 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3579 /* Unlike the generic linker, we do not pull in elements because
3580 of common symbols. */
3581 if (h == (struct bfd_link_hash_entry *) NULL
3582 || h->type != bfd_link_hash_undefined)
3585 /* Include this element. */
3586 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3588 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3598 /* Add symbols from an ECOFF object file to the global linker hash
3602 ecoff_link_add_object_symbols (abfd, info)
3604 struct bfd_link_info *info;
3607 bfd_size_type external_ext_size;
3612 if (! ecoff_slurp_symbolic_header (abfd))
3615 /* If there are no symbols, we don't want it. */
3616 if (bfd_get_symcount (abfd) == 0)
3619 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3621 /* Read in the external symbols and external strings. */
3622 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3623 esize = symhdr->iextMax * external_ext_size;
3624 external_ext = (PTR) alloca (esize);
3625 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3626 || bfd_read (external_ext, 1, esize, abfd) != esize)
3629 ssext = (char *) alloca (symhdr->issExtMax);
3630 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3631 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
3634 return ecoff_link_add_externals (abfd, info, external_ext, ssext);
3637 /* Add the external symbols of an object file to the global linker
3638 hash table. The external symbols and strings we are passed are
3639 just allocated on the stack, and will be discarded. We must
3640 explicitly save any information we may need later on in the link.
3641 We do not want to read the external symbol information again. */
3644 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3646 struct bfd_link_info *info;
3650 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3651 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3652 = backend->debug_swap.swap_ext_in;
3653 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3654 unsigned long ext_count;
3655 struct ecoff_link_hash_entry **sym_hash;
3659 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3661 sym_hash = ((struct ecoff_link_hash_entry **)
3663 ext_count * sizeof (struct bfd_link_hash_entry *)));
3664 ecoff_data (abfd)->sym_hashes = sym_hash;
3666 ext_ptr = (char *) external_ext;
3667 ext_end = ext_ptr + ext_count * external_ext_size;
3668 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3675 struct ecoff_link_hash_entry *h;
3679 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3681 /* Skip debugging symbols. */
3683 switch (esym.asym.st)
3699 /* Get the information for this symbol. */
3700 value = esym.asym.value;
3701 switch (esym.asym.sc)
3721 section = bfd_make_section_old_way (abfd, ".text");
3722 value -= section->vma;
3725 section = bfd_make_section_old_way (abfd, ".data");
3726 value -= section->vma;
3729 section = bfd_make_section_old_way (abfd, ".bss");
3730 value -= section->vma;
3733 section = &bfd_abs_section;
3736 section = &bfd_und_section;
3739 section = bfd_make_section_old_way (abfd, ".sdata");
3740 value -= section->vma;
3743 section = bfd_make_section_old_way (abfd, ".sbss");
3744 value -= section->vma;
3747 section = bfd_make_section_old_way (abfd, ".rdata");
3748 value -= section->vma;
3751 if (value > ecoff_data (abfd)->gp_size)
3753 section = &bfd_com_section;
3758 if (ecoff_scom_section.name == NULL)
3760 /* Initialize the small common section. */
3761 ecoff_scom_section.name = SCOMMON;
3762 ecoff_scom_section.flags = SEC_IS_COMMON;
3763 ecoff_scom_section.output_section = &ecoff_scom_section;
3764 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3765 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3766 ecoff_scom_symbol.name = SCOMMON;
3767 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3768 ecoff_scom_symbol.section = &ecoff_scom_section;
3769 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3771 section = &ecoff_scom_section;
3774 section = &bfd_und_section;
3777 section = bfd_make_section_old_way (abfd, ".init");
3778 value -= section->vma;
3781 section = bfd_make_section_old_way (abfd, ".fini");
3782 value -= section->vma;
3786 if (section == (asection *) NULL)
3789 name = ssext + esym.asym.iss;
3791 if (! (_bfd_generic_link_add_one_symbol
3792 (info, abfd, name, BSF_GLOBAL, section, value,
3793 (const char *) NULL, true, true, backend->constructor_bitsize,
3794 (struct bfd_link_hash_entry **) &h)))
3799 /* If we are building an ECOFF hash table, save the external
3800 symbol information. */
3801 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3803 if (h->abfd == (bfd *) NULL
3804 || (section != &bfd_und_section
3805 && (! bfd_is_com_section (section)
3806 || h->root.type != bfd_link_hash_defined)))
3817 /* ECOFF final link routines. */
3819 static boolean ecoff_final_link_debug_accumulate
3820 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *));
3821 static boolean ecoff_link_write_external
3822 PARAMS ((struct ecoff_link_hash_entry *, PTR));
3823 static boolean ecoff_indirect_link_order
3824 PARAMS ((bfd *, struct bfd_link_info *, asection *,
3825 struct bfd_link_order *));
3827 /* ECOFF final link routine. This looks through all the input BFDs
3828 and gathers together all the debugging information, and then
3829 processes all the link order information. This may cause it to
3830 close and reopen some input BFDs; I'll see how bad this is. */
3833 ecoff_bfd_final_link (abfd, info)
3835 struct bfd_link_info *info;
3837 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3838 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
3840 register bfd *input_bfd;
3842 struct bfd_link_order *p;
3844 /* We accumulate the debugging information counts in the symbolic
3846 symhdr = &debug->symbolic_header;
3847 symhdr->magic = backend->debug_swap.sym_magic;
3848 /* FIXME: What should the version stamp be? */
3850 symhdr->ilineMax = 0;
3854 symhdr->isymMax = 0;
3855 symhdr->ioptMax = 0;
3856 symhdr->iauxMax = 0;
3858 symhdr->issExtMax = 0;
3861 symhdr->iextMax = 0;
3863 /* We accumulate the debugging information itself in the debug_info
3865 debug->line = debug->line_end = NULL;
3866 debug->external_dnr = debug->external_dnr_end = NULL;
3867 debug->external_pdr = debug->external_pdr_end = NULL;
3868 debug->external_sym = debug->external_sym_end = NULL;
3869 debug->external_opt = debug->external_opt_end = NULL;
3870 debug->external_aux = debug->external_aux_end = NULL;
3871 debug->ss = debug->ss_end = NULL;
3872 debug->ssext = debug->ssext_end = NULL;
3873 debug->external_fdr = debug->external_fdr_end = NULL;
3874 debug->external_rfd = debug->external_rfd_end = NULL;
3875 debug->external_ext = debug->external_ext_end = NULL;
3877 /* Accumulate the debugging symbols from each input BFD. */
3878 for (input_bfd = info->input_bfds;
3879 input_bfd != (bfd *) NULL;
3880 input_bfd = input_bfd->link_next)
3884 /* If we might be using the C based alloca function, dump memory
3885 allocated by ecoff_final_link_debug_accumulate. */
3892 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
3893 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info);
3895 ret = bfd_ecoff_debug_link_other (abfd,
3897 &backend->debug_swap,
3902 /* Combine the register masks. */
3903 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
3904 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
3905 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
3906 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
3907 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
3908 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
3911 /* Write out the external symbols. */
3912 ecoff_link_hash_traverse (ecoff_hash_table (info),
3913 ecoff_link_write_external,
3916 if (info->relocateable)
3918 /* We need to make a pass over the link_orders to count up the
3919 number of relocations we will need to output, so that we know
3920 how much space they will take up. */
3921 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3924 for (p = o->link_order_head;
3925 p != (struct bfd_link_order *) NULL;
3927 if (p->type == bfd_indirect_link_order)
3928 o->reloc_count += p->u.indirect.section->reloc_count;
3931 ecoff_compute_reloc_file_positions (abfd);
3933 /* Now reset the reloc_count field of the sections in the output
3934 BFD to 0, so that we can use them to keep track of how many
3935 relocs we have output thus far. */
3936 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3940 /* Get a value for the GP register. */
3941 if (ecoff_data (abfd)->gp == 0)
3943 struct bfd_link_hash_entry *h;
3945 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
3946 if (h != (struct bfd_link_hash_entry *) NULL
3947 && h->type == bfd_link_hash_defined)
3948 ecoff_data (abfd)->gp = (h->u.def.value
3949 + h->u.def.section->output_section->vma
3950 + h->u.def.section->output_offset);
3951 else if (info->relocateable)
3955 /* Make up a value. */
3957 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3960 && (strcmp (o->name, _SBSS) == 0
3961 || strcmp (o->name, _SDATA) == 0
3962 || strcmp (o->name, _LIT4) == 0
3963 || strcmp (o->name, _LIT8) == 0
3964 || strcmp (o->name, _LITA) == 0))
3967 ecoff_data (abfd)->gp = lo + 0x8000;
3971 /* If the relocate_section function needs to do a reloc
3972 involving the GP value, it should make a reloc_dangerous
3973 callback to warn that GP is not defined. */
3977 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3979 /* Ignore any link_orders for the .reginfo section, which does
3980 not really exist. */
3981 if (strcmp (o->name, REGINFO) == 0)
3984 for (p = o->link_order_head;
3985 p != (struct bfd_link_order *) NULL;
3988 /* If we might be using the C based alloca function, we need
3989 to dump the memory allocated by the function
3990 ecoff_indirect_link_order. */
3996 if (p->type == bfd_indirect_link_order
3997 && (bfd_get_flavour (p->u.indirect.section->owner)
3998 == bfd_target_ecoff_flavour))
4000 if (! ecoff_indirect_link_order (abfd, info, o, p))
4005 if (! _bfd_default_link_order (abfd, info, o, p))
4011 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4016 /* Accumulate the debugging information for an input BFD into the
4017 output BFD. This must read in the symbolic information of the
4021 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info)
4024 struct bfd_link_info *info;
4026 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4027 const struct ecoff_debug_swap * const swap =
4028 &ecoff_backend (input_bfd)->debug_swap;
4029 HDRR *symhdr = &debug->symbolic_header;
4032 #define READ(ptr, offset, count, size, type) \
4033 if (symhdr->count == 0) \
4034 debug->ptr = NULL; \
4037 debug->ptr = (type) alloca (size * symhdr->count); \
4038 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4040 || (bfd_read (debug->ptr, size, symhdr->count, \
4041 input_bfd) != size * symhdr->count)) \
4045 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
4046 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4047 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4048 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4049 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4050 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4052 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4053 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4054 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4057 /* We do not read the external strings or the external symbols. */
4059 ret = (bfd_ecoff_debug_accumulate
4060 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4061 &ecoff_backend (output_bfd)->debug_swap,
4062 input_bfd, debug, swap, info->relocateable));
4064 /* Make sure we don't accidentally follow one of these pointers on
4067 debug->external_dnr = NULL;
4068 debug->external_pdr = NULL;
4069 debug->external_sym = NULL;
4070 debug->external_opt = NULL;
4071 debug->external_aux = NULL;
4073 debug->external_fdr = NULL;
4074 debug->external_rfd = NULL;
4079 /* Put out information for an external symbol. These come only from
4083 ecoff_link_write_external (h, data)
4084 struct ecoff_link_hash_entry *h;
4087 bfd *output_bfd = (bfd *) data;
4089 /* FIXME: We should check if this symbol is being stripped. */
4091 if (h->root.written)
4094 if (h->abfd == (bfd *) NULL)
4097 h->esym.cobol_main = 0;
4098 h->esym.weakext = 0;
4099 h->esym.reserved = 0;
4100 h->esym.ifd = ifdNil;
4101 h->esym.asym.value = 0;
4102 /* FIXME: we can do better than this for st and sc. */
4103 h->esym.asym.st = stGlobal;
4104 h->esym.asym.sc = scAbs;
4105 h->esym.asym.reserved = 0;
4106 h->esym.asym.index = indexNil;
4110 /* Adjust the FDR index for the symbol by that used for the
4112 h->esym.ifd += ecoff_data (h->abfd)->debug_info.ifdbase;
4115 switch (h->root.type)
4118 case bfd_link_hash_new:
4120 case bfd_link_hash_undefined:
4121 case bfd_link_hash_weak:
4122 if (h->esym.asym.st != scUndefined
4123 && h->esym.asym.st != scSUndefined)
4124 h->esym.asym.st = scUndefined;
4126 case bfd_link_hash_defined:
4127 if (h->esym.asym.sc == scUndefined
4128 || h->esym.asym.sc == scSUndefined)
4129 h->esym.asym.sc = scAbs;
4130 else if (h->esym.asym.sc == scCommon)
4131 h->esym.asym.sc = scBss;
4132 else if (h->esym.asym.sc == scSCommon)
4133 h->esym.asym.sc = scSBss;
4134 h->esym.asym.value = (h->root.u.def.value
4135 + h->root.u.def.section->output_section->vma
4136 + h->root.u.def.section->output_offset);
4138 case bfd_link_hash_common:
4139 if (h->esym.asym.sc != scCommon
4140 && h->esym.asym.sc != scSCommon)
4141 h->esym.asym.sc = scCommon;
4142 h->esym.asym.value = h->root.u.c.size;
4144 case bfd_link_hash_indirect:
4145 case bfd_link_hash_warning:
4146 /* FIXME: Ignore these for now. The circumstances under which
4147 they should be written out are not clear to me. */
4151 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4153 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4154 h->root.written = true;
4156 return (bfd_ecoff_debug_one_external
4157 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4158 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4162 /* Relocate and write an ECOFF section into an ECOFF output file. */
4165 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4167 struct bfd_link_info *info;
4168 asection *output_section;
4169 struct bfd_link_order *link_order;
4171 asection *input_section;
4173 bfd_size_type input_size;
4175 bfd_size_type external_reloc_size;
4176 bfd_size_type external_relocs_size;
4177 PTR external_relocs;
4179 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4181 if (link_order->size == 0)
4184 input_section = link_order->u.indirect.section;
4185 input_bfd = input_section->owner;
4187 BFD_ASSERT (input_section->output_section == output_section);
4188 BFD_ASSERT (input_section->output_offset == link_order->offset);
4189 BFD_ASSERT (bfd_section_size (input_bfd, input_section) == link_order->size);
4191 /* Get the section contents. */
4192 input_size = bfd_section_size (input_bfd, input_section);
4193 contents = (bfd_byte *) alloca (input_size);
4194 if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents,
4195 (file_ptr) 0, input_size))
4198 /* Get the relocs. */
4199 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4200 external_relocs_size = external_reloc_size * input_section->reloc_count;
4201 external_relocs = (PTR) alloca (external_relocs_size);
4202 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4203 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4204 != external_relocs_size))
4207 /* Relocate the section contents. */
4208 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4209 (output_bfd, info, input_bfd, input_section, contents,
4213 /* Write out the relocated section. */
4214 if (! bfd_set_section_contents (output_bfd,
4217 input_section->output_offset,
4221 /* If we are producing relocateable output, the relocs were
4222 modified, and we write them out now. We use the reloc_count
4223 field of output_section to keep track of the number of relocs we
4224 have output so far. */
4225 if (info->relocateable)
4227 if (bfd_seek (output_bfd,
4228 (output_section->rel_filepos +
4229 output_section->reloc_count * external_reloc_size),
4231 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4232 != external_relocs_size))
4234 output_section->reloc_count += input_section->reloc_count;