1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Original version by Per Bothner.
5 Full support added by Ian Lance Taylor, ian@cygnus.com.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "aout/ranlib.h"
29 #include "aout/stab_gnu.h"
31 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32 some other stuff which we don't want and which conflicts with stuff
35 #include "aout/aout64.h"
38 #undef obj_sym_filepos
40 #include "coff/internal.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
47 /* Prototypes for static functions. */
49 static int ecoff_get_magic PARAMS ((bfd *));
50 static long ecoff_sec_to_styp_flags PARAMS ((const char *, flagword));
51 static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *));
52 static boolean ecoff_set_symbol_info PARAMS ((bfd *, SYMR *, asymbol *, int, int));
53 static void ecoff_emit_aggregate PARAMS ((bfd *, FDR *, char *, RNDXR *, long, const char *));
54 static char *ecoff_type_to_string PARAMS ((bfd *, FDR *, unsigned int));
55 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *, asection *, asymbol **));
56 static int ecoff_sort_hdrs PARAMS ((const PTR, const PTR));
57 static boolean ecoff_compute_section_file_positions PARAMS ((bfd *));
58 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *));
59 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
60 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
61 static unsigned int ecoff_armap_hash PARAMS ((const char *, unsigned int *, unsigned int, unsigned int));
63 /* This stuff is somewhat copied from coffcode.h. */
65 static asection bfd_debug_section =
67 /* name, id, index, next, flags, user_set_vma, reloc_done, */
68 "*DEBUG*", 0, 0, NULL, 0, 0, 0,
69 /* linker_mark, linker_has_input, gc_mark, segment_mark, */
71 /* vma, lma, _cooked_size, _raw_size, */
73 /* output_offset, output_section, alignment_power, */
75 /* relocation, orelocation, reloc_count, filepos, rel_filepos, */
77 /* line_filepos, userdata, contents, lineno, lineno_count, */
78 0, NULL, NULL, NULL, 0,
79 /* entsize, comdat, moving_line_filepos, */
81 /* target_index, used_by_bfd, constructor_chain, owner, */
84 (struct symbol_cache_entry *) NULL,
86 (struct symbol_cache_entry **) NULL,
87 /* link_order_head, link_order_tail */
91 /* Create an ECOFF object. */
94 _bfd_ecoff_mkobject (abfd)
97 bfd_size_type amt = sizeof (ecoff_data_type);
99 abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
100 if (abfd->tdata.ecoff_obj_data == NULL)
106 /* This is a hook called by coff_real_object_p to create any backend
107 specific information. */
110 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
115 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
116 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
117 ecoff_data_type *ecoff;
119 if (! _bfd_ecoff_mkobject (abfd))
122 ecoff = ecoff_data (abfd);
124 ecoff->sym_filepos = internal_f->f_symptr;
126 if (internal_a != (struct internal_aouthdr *) NULL)
130 ecoff->text_start = internal_a->text_start;
131 ecoff->text_end = internal_a->text_start + internal_a->tsize;
132 ecoff->gp = internal_a->gp_value;
133 ecoff->gprmask = internal_a->gprmask;
134 for (i = 0; i < 4; i++)
135 ecoff->cprmask[i] = internal_a->cprmask[i];
136 ecoff->fprmask = internal_a->fprmask;
137 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
138 abfd->flags |= D_PAGED;
140 abfd->flags &=~ D_PAGED;
143 /* It turns out that no special action is required by the MIPS or
144 Alpha ECOFF backends. They have different information in the
145 a.out header, but we just copy it all (e.g., gprmask, cprmask and
146 fprmask) and let the swapping routines ensure that only relevant
147 information is written out. */
152 /* Initialize a new section. */
155 _bfd_ecoff_new_section_hook (abfd, section)
156 bfd *abfd ATTRIBUTE_UNUSED;
159 section->alignment_power = 4;
161 if (strcmp (section->name, _TEXT) == 0
162 || strcmp (section->name, _INIT) == 0
163 || strcmp (section->name, _FINI) == 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 || strcmp (section->name, _RCONST) == 0
172 || strcmp (section->name, _PDATA) == 0)
173 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
174 else if (strcmp (section->name, _BSS) == 0
175 || strcmp (section->name, _SBSS) == 0)
176 section->flags |= SEC_ALLOC;
177 else if (strcmp (section->name, _LIB) == 0)
178 /* An Irix 4 shared libary. */
179 section->flags |= SEC_COFF_SHARED_LIBRARY;
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 _bfd_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 _bfd_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 bfd_big_endian (abfd) ? 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 (strcmp (name, _LIB) == 0)
320 styp = STYP_ECOFF_LIB;
321 else if (strcmp (name, _GOT) == 0)
323 else if (strcmp (name, _HASH) == 0)
325 else if (strcmp (name, _DYNAMIC) == 0)
327 else if (strcmp (name, _LIBLIST) == 0)
329 else if (strcmp (name, _RELDYN) == 0)
331 else if (strcmp (name, _CONFLIC) == 0)
333 else if (strcmp (name, _DYNSTR) == 0)
335 else if (strcmp (name, _DYNSYM) == 0)
337 else if (strcmp (name, _COMMENT) == 0)
340 flags &=~ SEC_NEVER_LOAD;
342 else if (strcmp (name, _RCONST) == 0)
344 else if (flags & SEC_CODE)
346 else if (flags & SEC_DATA)
348 else if (flags & SEC_READONLY)
350 else if (flags & SEC_LOAD)
355 if (flags & SEC_NEVER_LOAD)
361 /* Get the BFD flags to use for a section. */
364 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name, section, flags_ptr)
365 bfd *abfd ATTRIBUTE_UNUSED;
367 const char *name ATTRIBUTE_UNUSED;
368 asection *section ATTRIBUTE_UNUSED;
369 flagword * flags_ptr;
371 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
372 long styp_flags = internal_s->s_flags;
373 flagword sec_flags = 0;
375 if (styp_flags & STYP_NOLOAD)
376 sec_flags |= SEC_NEVER_LOAD;
378 /* For 386 COFF, at least, an unloadable text or data section is
379 actually a shared library section. */
380 if ((styp_flags & STYP_TEXT)
381 || (styp_flags & STYP_ECOFF_INIT)
382 || (styp_flags & STYP_ECOFF_FINI)
383 || (styp_flags & STYP_DYNAMIC)
384 || (styp_flags & STYP_LIBLIST)
385 || (styp_flags & STYP_RELDYN)
386 || styp_flags == STYP_CONFLIC
387 || (styp_flags & STYP_DYNSTR)
388 || (styp_flags & STYP_DYNSYM)
389 || (styp_flags & STYP_HASH))
391 if (sec_flags & SEC_NEVER_LOAD)
392 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
394 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
396 else if ((styp_flags & STYP_DATA)
397 || (styp_flags & STYP_RDATA)
398 || (styp_flags & STYP_SDATA)
399 || styp_flags == STYP_PDATA
400 || styp_flags == STYP_XDATA
401 || (styp_flags & STYP_GOT)
402 || styp_flags == STYP_RCONST)
404 if (sec_flags & SEC_NEVER_LOAD)
405 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
407 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
408 if ((styp_flags & STYP_RDATA)
409 || styp_flags == STYP_PDATA
410 || styp_flags == STYP_RCONST)
411 sec_flags |= SEC_READONLY;
413 else if ((styp_flags & STYP_BSS)
414 || (styp_flags & STYP_SBSS))
415 sec_flags |= SEC_ALLOC;
416 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
417 sec_flags |= SEC_NEVER_LOAD;
418 else if ((styp_flags & STYP_LITA)
419 || (styp_flags & STYP_LIT8)
420 || (styp_flags & STYP_LIT4))
421 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
422 else if (styp_flags & STYP_ECOFF_LIB)
423 sec_flags |= SEC_COFF_SHARED_LIBRARY;
425 sec_flags |= SEC_ALLOC | SEC_LOAD;
427 * flags_ptr = sec_flags;
431 /* Read in the symbolic header for an ECOFF object file. */
434 ecoff_slurp_symbolic_header (abfd)
437 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
438 bfd_size_type external_hdr_size;
440 HDRR *internal_symhdr;
442 /* See if we've already read it in. */
443 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
444 backend->debug_swap.sym_magic)
447 /* See whether there is a symbolic header. */
448 if (ecoff_data (abfd)->sym_filepos == 0)
450 bfd_get_symcount (abfd) = 0;
454 /* At this point bfd_get_symcount (abfd) holds the number of symbols
455 as read from the file header, but on ECOFF this is always the
456 size of the symbolic information header. It would be cleaner to
457 handle this when we first read the file in coffgen.c. */
458 external_hdr_size = backend->debug_swap.external_hdr_size;
459 if (bfd_get_symcount (abfd) != external_hdr_size)
461 bfd_set_error (bfd_error_bad_value);
465 /* Read the symbolic information header. */
466 raw = (PTR) bfd_malloc (external_hdr_size);
470 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0
471 || bfd_bread (raw, external_hdr_size, abfd) != external_hdr_size)
473 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
474 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
476 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
478 bfd_set_error (bfd_error_bad_value);
482 /* Now we can get the correct number of symbols. */
483 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
484 + internal_symhdr->iextMax);
495 /* Read in and swap the important symbolic information for an ECOFF
496 object file. This is called by gdb via the read_debug_info entry
497 point in the backend structure. */
500 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
502 asection *ignore ATTRIBUTE_UNUSED;
503 struct ecoff_debug_info *debug;
505 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
506 HDRR *internal_symhdr;
507 bfd_size_type raw_base;
508 bfd_size_type raw_size;
510 bfd_size_type external_fdr_size;
514 bfd_size_type raw_end;
515 bfd_size_type cb_end;
519 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
521 /* Check whether we've already gotten it, and whether there's any to
523 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
525 if (ecoff_data (abfd)->sym_filepos == 0)
527 bfd_get_symcount (abfd) = 0;
531 if (! ecoff_slurp_symbolic_header (abfd))
534 internal_symhdr = &debug->symbolic_header;
536 /* Read all the symbolic information at once. */
537 raw_base = (ecoff_data (abfd)->sym_filepos
538 + backend->debug_swap.external_hdr_size);
540 /* Alpha ecoff makes the determination of raw_size difficult. It has
541 an undocumented debug data section between the symhdr and the first
542 documented section. And the ordering of the sections varies between
543 statically and dynamically linked executables.
544 If bfd supports SEEK_END someday, this code could be simplified. */
547 #define UPDATE_RAW_END(start, count, size) \
548 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
549 if (cb_end > raw_end) \
552 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
553 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
554 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
555 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
556 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
557 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
558 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
559 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
560 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
561 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
562 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
564 #undef UPDATE_RAW_END
566 raw_size = raw_end - raw_base;
569 ecoff_data (abfd)->sym_filepos = 0;
572 raw = (PTR) bfd_alloc (abfd, raw_size);
576 pos = ecoff_data (abfd)->sym_filepos;
577 pos += backend->debug_swap.external_hdr_size;
578 if (bfd_seek (abfd, pos, SEEK_SET) != 0
579 || bfd_bread (raw, raw_size, abfd) != raw_size)
581 bfd_release (abfd, raw);
585 ecoff_data (abfd)->raw_syments = raw;
587 /* Get pointers for the numeric offsets in the HDRR structure. */
588 #define FIX(off1, off2, type) \
589 if (internal_symhdr->off1 == 0) \
590 debug->off2 = (type) NULL; \
592 debug->off2 = (type) ((char *) raw \
593 + (internal_symhdr->off1 \
596 FIX (cbLineOffset, line, unsigned char *);
597 FIX (cbDnOffset, external_dnr, PTR);
598 FIX (cbPdOffset, external_pdr, PTR);
599 FIX (cbSymOffset, external_sym, PTR);
600 FIX (cbOptOffset, external_opt, PTR);
601 FIX (cbAuxOffset, external_aux, union aux_ext *);
602 FIX (cbSsOffset, ss, char *);
603 FIX (cbSsExtOffset, ssext, char *);
604 FIX (cbFdOffset, external_fdr, PTR);
605 FIX (cbRfdOffset, external_rfd, PTR);
606 FIX (cbExtOffset, external_ext, PTR);
609 /* I don't want to always swap all the data, because it will just
610 waste time and most programs will never look at it. The only
611 time the linker needs most of the debugging information swapped
612 is when linking big-endian and little-endian MIPS object files
613 together, which is not a common occurrence.
615 We need to look at the fdr to deal with a lot of information in
616 the symbols, so we swap them here. */
617 amt = internal_symhdr->ifdMax;
618 amt *= sizeof (struct fdr);
619 debug->fdr = (struct fdr *) bfd_alloc (abfd, amt);
620 if (debug->fdr == NULL)
622 external_fdr_size = backend->debug_swap.external_fdr_size;
623 fdr_ptr = debug->fdr;
624 fraw_src = (char *) debug->external_fdr;
625 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
626 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
627 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
632 /* ECOFF symbol table routines. The ECOFF symbol table is described
633 in gcc/mips-tfile.c. */
635 /* ECOFF uses two common sections. One is the usual one, and the
636 other is for small objects. All the small objects are kept
637 together, and then referenced via the gp pointer, which yields
638 faster assembler code. This is what we use for the small common
640 static asection ecoff_scom_section;
641 static asymbol ecoff_scom_symbol;
642 static asymbol *ecoff_scom_symbol_ptr;
644 /* Create an empty symbol. */
647 _bfd_ecoff_make_empty_symbol (abfd)
650 ecoff_symbol_type *new;
651 bfd_size_type amt = sizeof (ecoff_symbol_type);
653 new = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
654 if (new == (ecoff_symbol_type *) NULL)
655 return (asymbol *) NULL;
656 new->symbol.section = (asection *) NULL;
657 new->fdr = (FDR *) NULL;
660 new->symbol.the_bfd = abfd;
664 /* Set the BFD flags and section for an ECOFF symbol. */
667 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, weak)
674 asym->the_bfd = abfd;
675 asym->value = ecoff_sym->value;
676 asym->section = &bfd_debug_section;
679 /* Most symbol types are just for debugging. */
680 switch (ecoff_sym->st)
689 if (ECOFF_IS_STAB (ecoff_sym))
691 asym->flags = BSF_DEBUGGING;
696 asym->flags = BSF_DEBUGGING;
701 asym->flags = BSF_EXPORT | BSF_WEAK;
703 asym->flags = BSF_EXPORT | BSF_GLOBAL;
706 asym->flags = BSF_LOCAL;
707 /* Normally, a local stProc symbol will have a corresponding
708 external symbol. We mark the local symbol as a debugging
709 symbol, in order to prevent nm from printing both out.
710 Similarly, we mark stLabel and stabs symbols as debugging
711 symbols. In both cases, we do want to set the value
712 correctly based on the symbol class. */
713 if (ecoff_sym->st == stProc
714 || ecoff_sym->st == stLabel
715 || ECOFF_IS_STAB (ecoff_sym))
716 asym->flags |= BSF_DEBUGGING;
719 if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
720 asym->flags |= BSF_FUNCTION;
722 switch (ecoff_sym->sc)
725 /* Used for compiler generated labels. Leave them in the
726 debugging section, and mark them as local. If BSF_DEBUGGING
727 is set, then nm does not display them for some reason. If no
728 flags are set then the linker whines about them. */
729 asym->flags = BSF_LOCAL;
732 asym->section = bfd_make_section_old_way (abfd, ".text");
733 asym->value -= asym->section->vma;
736 asym->section = bfd_make_section_old_way (abfd, ".data");
737 asym->value -= asym->section->vma;
740 asym->section = bfd_make_section_old_way (abfd, ".bss");
741 asym->value -= asym->section->vma;
744 asym->flags = BSF_DEBUGGING;
747 asym->section = bfd_abs_section_ptr;
750 asym->section = bfd_und_section_ptr;
760 asym->flags = BSF_DEBUGGING;
763 asym->section = bfd_make_section_old_way (abfd, ".sdata");
764 asym->value -= asym->section->vma;
767 asym->section = bfd_make_section_old_way (abfd, ".sbss");
768 asym->value -= asym->section->vma;
771 asym->section = bfd_make_section_old_way (abfd, ".rdata");
772 asym->value -= asym->section->vma;
775 asym->flags = BSF_DEBUGGING;
778 if (asym->value > ecoff_data (abfd)->gp_size)
780 asym->section = bfd_com_section_ptr;
786 if (ecoff_scom_section.name == NULL)
788 /* Initialize the small common section. */
789 ecoff_scom_section.name = SCOMMON;
790 ecoff_scom_section.flags = SEC_IS_COMMON;
791 ecoff_scom_section.output_section = &ecoff_scom_section;
792 ecoff_scom_section.symbol = &ecoff_scom_symbol;
793 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
794 ecoff_scom_symbol.name = SCOMMON;
795 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
796 ecoff_scom_symbol.section = &ecoff_scom_section;
797 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
799 asym->section = &ecoff_scom_section;
804 asym->flags = BSF_DEBUGGING;
807 asym->section = bfd_und_section_ptr;
812 asym->section = bfd_make_section_old_way (abfd, ".init");
813 asym->value -= asym->section->vma;
818 asym->flags = BSF_DEBUGGING;
821 asym->section = bfd_make_section_old_way (abfd, ".fini");
822 asym->value -= asym->section->vma;
825 asym->section = bfd_make_section_old_way (abfd, ".rconst");
826 asym->value -= asym->section->vma;
832 /* Look for special constructors symbols and make relocation entries
833 in a special construction section. These are produced by the
834 -fgnu-linker argument to g++. */
835 if (ECOFF_IS_STAB (ecoff_sym))
837 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
847 /* This code is no longer needed. It used to be used to
848 make the linker handle set symbols, but they are now
849 handled in the add_symbols routine instead. */
853 arelent_chain *reloc_chain;
854 unsigned int bitsize;
857 /* Get a section with the same name as the symbol (usually
858 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
859 name ___CTOR_LIST (three underscores). We need
860 __CTOR_LIST (two underscores), since ECOFF doesn't use
861 a leading underscore. This should be handled by gcc,
862 but instead we do it here. Actually, this should all
863 be done differently anyhow. */
864 name = bfd_asymbol_name (asym);
865 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
870 section = bfd_get_section_by_name (abfd, name);
871 if (section == (asection *) NULL)
875 amt = strlen (name) + 1;
876 copy = (char *) bfd_alloc (abfd, amt);
880 section = bfd_make_section (abfd, copy);
883 /* Build a reloc pointing to this constructor. */
884 amt = sizeof (arelent_chain);
885 reloc_chain = (arelent_chain *) bfd_alloc (abfd, amt);
888 reloc_chain->relent.sym_ptr_ptr =
889 bfd_get_section (asym)->symbol_ptr_ptr;
890 reloc_chain->relent.address = section->_raw_size;
891 reloc_chain->relent.addend = asym->value;
892 reloc_chain->relent.howto =
893 ecoff_backend (abfd)->constructor_reloc;
895 /* Set up the constructor section to hold the reloc. */
896 section->flags = SEC_CONSTRUCTOR;
897 ++section->reloc_count;
899 /* Constructor sections must be rounded to a boundary
900 based on the bitsize. These are not real sections--
901 they are handled specially by the linker--so the ECOFF
902 16 byte alignment restriction does not apply. */
903 bitsize = ecoff_backend (abfd)->constructor_bitsize;
904 section->alignment_power = 1;
905 while ((1 << section->alignment_power) < bitsize / 8)
906 ++section->alignment_power;
908 reloc_chain->next = section->constructor_chain;
909 section->constructor_chain = reloc_chain;
910 section->_raw_size += bitsize / 8;
914 /* Mark the symbol as a constructor. */
915 asym->flags |= BSF_CONSTRUCTOR;
923 /* Read an ECOFF symbol table. */
926 _bfd_ecoff_slurp_symbol_table (abfd)
929 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
930 const bfd_size_type external_ext_size
931 = backend->debug_swap.external_ext_size;
932 const bfd_size_type external_sym_size
933 = backend->debug_swap.external_sym_size;
934 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
935 = backend->debug_swap.swap_ext_in;
936 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
937 = backend->debug_swap.swap_sym_in;
938 bfd_size_type internal_size;
939 ecoff_symbol_type *internal;
940 ecoff_symbol_type *internal_ptr;
946 /* If we've already read in the symbol table, do nothing. */
947 if (ecoff_data (abfd)->canonical_symbols != NULL)
950 /* Get the symbolic information. */
951 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
952 &ecoff_data (abfd)->debug_info))
954 if (bfd_get_symcount (abfd) == 0)
957 internal_size = bfd_get_symcount (abfd);
958 internal_size *= sizeof (ecoff_symbol_type);
959 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
960 if (internal == NULL)
963 internal_ptr = internal;
964 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
966 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
967 * external_ext_size));
968 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
972 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
973 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
974 + internal_esym.asym.iss);
975 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
976 &internal_ptr->symbol, 1,
977 internal_esym.weakext))
979 /* The alpha uses a negative ifd field for section symbols. */
980 if (internal_esym.ifd >= 0)
981 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
982 + internal_esym.ifd);
984 internal_ptr->fdr = NULL;
985 internal_ptr->local = false;
986 internal_ptr->native = (PTR) eraw_src;
989 /* The local symbols must be accessed via the fdr's, because the
990 string and aux indices are relative to the fdr information. */
991 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
992 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
993 for (; fdr_ptr < fdr_end; fdr_ptr++)
998 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
999 + fdr_ptr->isymBase * external_sym_size);
1000 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1002 lraw_src < lraw_end;
1003 lraw_src += external_sym_size, internal_ptr++)
1007 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1008 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
1010 + internal_sym.iss);
1011 if (!ecoff_set_symbol_info (abfd, &internal_sym,
1012 &internal_ptr->symbol, 0, 0))
1014 internal_ptr->fdr = fdr_ptr;
1015 internal_ptr->local = true;
1016 internal_ptr->native = (PTR) lraw_src;
1020 ecoff_data (abfd)->canonical_symbols = internal;
1025 /* Return the amount of space needed for the canonical symbols. */
1028 _bfd_ecoff_get_symtab_upper_bound (abfd)
1031 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
1032 &ecoff_data (abfd)->debug_info))
1035 if (bfd_get_symcount (abfd) == 0)
1038 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1041 /* Get the canonical symbols. */
1044 _bfd_ecoff_get_symtab (abfd, alocation)
1046 asymbol **alocation;
1048 unsigned int counter = 0;
1049 ecoff_symbol_type *symbase;
1050 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1052 if (! _bfd_ecoff_slurp_symbol_table (abfd))
1054 if (bfd_get_symcount (abfd) == 0)
1057 symbase = ecoff_data (abfd)->canonical_symbols;
1058 while (counter < bfd_get_symcount (abfd))
1060 *(location++) = symbase++;
1063 *location++ = (ecoff_symbol_type *) NULL;
1064 return bfd_get_symcount (abfd);
1067 /* Turn ECOFF type information into a printable string.
1068 ecoff_emit_aggregate and ecoff_type_to_string are from
1069 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1071 /* Write aggregate information to a string. */
1074 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1082 const struct ecoff_debug_swap * const debug_swap =
1083 &ecoff_backend (abfd)->debug_swap;
1084 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1085 unsigned int ifd = rndx->rfd;
1086 unsigned int indx = rndx->index;
1092 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1093 struct return type of a procedure compiled without -g. */
1094 if (ifd == 0xffffffff
1095 || (rndx->rfd == 0xfff && indx == 0))
1096 name = "<undefined>";
1097 else if (indx == indexNil)
1103 if (debug_info->external_rfd == NULL)
1104 fdr = debug_info->fdr + ifd;
1109 (*debug_swap->swap_rfd_in) (abfd,
1110 ((char *) debug_info->external_rfd
1111 + ((fdr->rfdBase + ifd)
1112 * debug_swap->external_rfd_size)),
1114 fdr = debug_info->fdr + rfd;
1117 indx += fdr->isymBase;
1119 (*debug_swap->swap_sym_in) (abfd,
1120 ((char *) debug_info->external_sym
1121 + indx * debug_swap->external_sym_size),
1124 name = debug_info->ss + fdr->issBase + sym.iss;
1128 "%s %s { ifd = %u, index = %lu }",
1131 + debug_info->symbolic_header.iextMax));
1134 /* Convert the type information to string format. */
1137 ecoff_type_to_string (abfd, fdr, indx)
1142 union aux_ext *aux_ptr;
1151 unsigned int basic_type;
1154 static char buffer2[1024];
1159 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1160 bigendian = fdr->fBigendian;
1162 for (i = 0; i < 7; i++)
1164 qualifiers[i].low_bound = 0;
1165 qualifiers[i].high_bound = 0;
1166 qualifiers[i].stride = 0;
1169 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1170 return "-1 (no type)";
1171 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1173 basic_type = u.ti.bt;
1174 qualifiers[0].type = u.ti.tq0;
1175 qualifiers[1].type = u.ti.tq1;
1176 qualifiers[2].type = u.ti.tq2;
1177 qualifiers[3].type = u.ti.tq3;
1178 qualifiers[4].type = u.ti.tq4;
1179 qualifiers[5].type = u.ti.tq5;
1180 qualifiers[6].type = tqNil;
1182 /* Go get the basic type. */
1185 case btNil: /* Undefined. */
1189 case btAdr: /* Address - integer same size as pointer. */
1190 strcpy (p1, "address");
1193 case btChar: /* Character. */
1194 strcpy (p1, "char");
1197 case btUChar: /* Unsigned character. */
1198 strcpy (p1, "unsigned char");
1201 case btShort: /* Short. */
1202 strcpy (p1, "short");
1205 case btUShort: /* Unsigned short. */
1206 strcpy (p1, "unsigned short");
1209 case btInt: /* Int. */
1213 case btUInt: /* Unsigned int. */
1214 strcpy (p1, "unsigned int");
1217 case btLong: /* Long. */
1218 strcpy (p1, "long");
1221 case btULong: /* Unsigned long. */
1222 strcpy (p1, "unsigned long");
1225 case btFloat: /* Float (real). */
1226 strcpy (p1, "float");
1229 case btDouble: /* Double (real). */
1230 strcpy (p1, "double");
1233 /* Structures add 1-2 aux words:
1234 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1235 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1237 case btStruct: /* Structure (Record). */
1238 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1239 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1240 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1242 indx++; /* Skip aux words. */
1245 /* Unions add 1-2 aux words:
1246 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1247 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1249 case btUnion: /* Union. */
1250 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1251 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1252 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1254 indx++; /* Skip aux words. */
1257 /* Enumerations add 1-2 aux words:
1258 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1259 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1261 case btEnum: /* Enumeration. */
1262 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1263 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1264 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1266 indx++; /* Skip aux words. */
1269 case btTypedef: /* Defined via a typedef, isymRef points. */
1270 strcpy (p1, "typedef");
1273 case btRange: /* Subrange of int. */
1274 strcpy (p1, "subrange");
1277 case btSet: /* Pascal sets. */
1281 case btComplex: /* Fortran complex. */
1282 strcpy (p1, "complex");
1285 case btDComplex: /* Fortran double complex. */
1286 strcpy (p1, "double complex");
1289 case btIndirect: /* Forward or unnamed typedef. */
1290 strcpy (p1, "forward/unamed typedef");
1293 case btFixedDec: /* Fixed Decimal. */
1294 strcpy (p1, "fixed decimal");
1297 case btFloatDec: /* Float Decimal. */
1298 strcpy (p1, "float decimal");
1301 case btString: /* Varying Length Character String. */
1302 strcpy (p1, "string");
1305 case btBit: /* Aligned Bit String. */
1309 case btPicture: /* Picture. */
1310 strcpy (p1, "picture");
1313 case btVoid: /* Void. */
1314 strcpy (p1, "void");
1318 sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
1322 p1 += strlen (buffer1);
1324 /* If this is a bitfield, get the bitsize. */
1329 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1330 sprintf (p1, " : %d", bitsize);
1331 p1 += strlen (buffer1);
1334 /* Deal with any qualifiers. */
1335 if (qualifiers[0].type != tqNil)
1337 /* Snarf up any array bounds in the correct order. Arrays
1338 store 5 successive words in the aux. table:
1339 word 0 RNDXR to type of the bounds (ie, int)
1340 word 1 Current file descriptor index
1342 word 3 high bound (or -1 if [])
1343 word 4 stride size in bits. */
1344 for (i = 0; i < 7; i++)
1346 if (qualifiers[i].type == tqArray)
1348 qualifiers[i].low_bound =
1349 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1350 qualifiers[i].high_bound =
1351 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1352 qualifiers[i].stride =
1353 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1358 /* Now print out the qualifiers. */
1359 for (i = 0; i < 6; i++)
1361 switch (qualifiers[i].type)
1368 strcpy (p2, "ptr to ");
1369 p2 += sizeof ("ptr to ")-1;
1373 strcpy (p2, "volatile ");
1374 p2 += sizeof ("volatile ")-1;
1378 strcpy (p2, "far ");
1379 p2 += sizeof ("far ")-1;
1383 strcpy (p2, "func. ret. ");
1384 p2 += sizeof ("func. ret. ");
1389 int first_array = i;
1392 /* Print array bounds reversed (ie, in the order the C
1393 programmer writes them). C is such a fun language.... */
1394 while (i < 5 && qualifiers[i+1].type == tqArray)
1397 for (j = i; j >= first_array; j--)
1399 strcpy (p2, "array [");
1400 p2 += sizeof ("array [")-1;
1401 if (qualifiers[j].low_bound != 0)
1403 "%ld:%ld {%ld bits}",
1404 (long) qualifiers[j].low_bound,
1405 (long) qualifiers[j].high_bound,
1406 (long) qualifiers[j].stride);
1408 else if (qualifiers[j].high_bound != -1)
1411 (long) (qualifiers[j].high_bound + 1),
1412 (long) (qualifiers[j].stride));
1415 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1418 strcpy (p2, "] of ");
1419 p2 += sizeof ("] of ")-1;
1427 strcpy (p2, buffer1);
1431 /* Return information about ECOFF symbol SYMBOL in RET. */
1434 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1435 bfd *abfd ATTRIBUTE_UNUSED;
1439 bfd_symbol_info (symbol, ret);
1442 /* Return whether this is a local label. */
1445 _bfd_ecoff_bfd_is_local_label_name (abfd, name)
1446 bfd *abfd ATTRIBUTE_UNUSED;
1449 return name[0] == '$';
1452 /* Print information about an ECOFF symbol. */
1455 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1459 bfd_print_symbol_type how;
1461 const struct ecoff_debug_swap * const debug_swap
1462 = &ecoff_backend (abfd)->debug_swap;
1463 FILE *file = (FILE *)filep;
1467 case bfd_print_symbol_name:
1468 fprintf (file, "%s", symbol->name);
1470 case bfd_print_symbol_more:
1471 if (ecoffsymbol (symbol)->local)
1475 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1477 fprintf (file, "ecoff local ");
1478 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1479 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1480 (unsigned) ecoff_sym.sc);
1486 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1488 fprintf (file, "ecoff extern ");
1489 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1490 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1491 (unsigned) ecoff_ext.asym.sc);
1494 case bfd_print_symbol_all:
1495 /* Print out the symbols in a reasonable way. */
1504 if (ecoffsymbol (symbol)->local)
1506 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1509 pos = ((((char *) ecoffsymbol (symbol)->native
1510 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1511 / debug_swap->external_sym_size)
1512 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1519 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1522 pos = (((char *) ecoffsymbol (symbol)->native
1523 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1524 / debug_swap->external_ext_size);
1525 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1526 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1527 weakext = ecoff_ext.weakext ? 'w' : ' ';
1530 fprintf (file, "[%3d] %c ",
1532 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1533 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1534 (unsigned) ecoff_ext.asym.st,
1535 (unsigned) ecoff_ext.asym.sc,
1536 (unsigned) ecoff_ext.asym.index,
1537 jmptbl, cobol_main, weakext,
1540 if (ecoffsymbol (symbol)->fdr != NULL
1541 && ecoff_ext.asym.index != indexNil)
1546 bfd_size_type sym_base;
1547 union aux_ext *aux_base;
1549 fdr = ecoffsymbol (symbol)->fdr;
1550 indx = ecoff_ext.asym.index;
1552 /* sym_base is used to map the fdr relative indices which
1553 appear in the file to the position number which we are
1555 sym_base = fdr->isymBase;
1556 if (ecoffsymbol (symbol)->local)
1558 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1560 /* aux_base is the start of the aux entries for this file;
1561 asym.index is an offset from this. */
1562 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1565 /* The aux entries are stored in host byte order; the
1566 order is indicated by a bit in the fdr. */
1567 bigendian = fdr->fBigendian;
1569 /* This switch is basically from gcc/mips-tdump.c. */
1570 switch (ecoff_ext.asym.st)
1578 fprintf (file, _("\n End+1 symbol: %ld"),
1579 (long) (indx + sym_base));
1583 if (ecoff_ext.asym.sc == scText
1584 || ecoff_ext.asym.sc == scInfo)
1585 fprintf (file, _("\n First symbol: %ld"),
1586 (long) (indx + sym_base));
1588 fprintf (file, _("\n First symbol: %ld"),
1590 (AUX_GET_ISYM (bigendian,
1591 &aux_base[ecoff_ext.asym.index])
1597 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1599 else if (ecoffsymbol (symbol)->local)
1600 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
1602 (AUX_GET_ISYM (bigendian,
1603 &aux_base[ecoff_ext.asym.index])
1605 ecoff_type_to_string (abfd, fdr, indx + 1));
1607 fprintf (file, _("\n Local symbol: %ld"),
1610 + (ecoff_data (abfd)
1611 ->debug_info.symbolic_header.iextMax)));
1615 fprintf (file, _("\n struct; End+1 symbol: %ld"),
1616 (long) (indx + sym_base));
1620 fprintf (file, _("\n union; End+1 symbol: %ld"),
1621 (long) (indx + sym_base));
1625 fprintf (file, _("\n enum; End+1 symbol: %ld"),
1626 (long) (indx + sym_base));
1630 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1631 fprintf (file, _("\n Type: %s"),
1632 ecoff_type_to_string (abfd, fdr, indx));
1641 /* Read in the relocs for a section. */
1644 ecoff_slurp_reloc_table (abfd, section, symbols)
1649 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1650 arelent *internal_relocs;
1651 bfd_size_type external_reloc_size;
1653 char *external_relocs;
1657 if (section->relocation != (arelent *) NULL
1658 || section->reloc_count == 0
1659 || (section->flags & SEC_CONSTRUCTOR) != 0)
1662 if (! _bfd_ecoff_slurp_symbol_table (abfd))
1665 amt = section->reloc_count;
1666 amt *= sizeof (arelent);
1667 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1669 external_reloc_size = backend->external_reloc_size;
1670 amt = external_reloc_size * section->reloc_count;
1671 external_relocs = (char *) bfd_alloc (abfd, amt);
1672 if (internal_relocs == (arelent *) NULL
1673 || external_relocs == (char *) NULL)
1675 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1677 if (bfd_bread (external_relocs, amt, abfd) != amt)
1680 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1682 struct internal_reloc intern;
1684 (*backend->swap_reloc_in) (abfd,
1685 external_relocs + i * external_reloc_size,
1688 if (intern.r_extern)
1690 /* r_symndx is an index into the external symbols. */
1691 BFD_ASSERT (intern.r_symndx >= 0
1693 < (ecoff_data (abfd)
1694 ->debug_info.symbolic_header.iextMax)));
1695 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1698 else if (intern.r_symndx == RELOC_SECTION_NONE
1699 || intern.r_symndx == RELOC_SECTION_ABS)
1701 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1706 const char *sec_name;
1709 /* r_symndx is a section key. */
1710 switch (intern.r_symndx)
1712 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1713 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1714 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1715 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1716 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1717 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1718 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1719 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1720 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1721 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1722 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1723 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1724 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1725 case RELOC_SECTION_RCONST: sec_name = ".rconst"; break;
1729 sec = bfd_get_section_by_name (abfd, sec_name);
1730 if (sec == (asection *) NULL)
1732 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1734 rptr->addend = - bfd_get_section_vma (abfd, sec);
1737 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1739 /* Let the backend select the howto field and do any other
1740 required processing. */
1741 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1744 bfd_release (abfd, external_relocs);
1746 section->relocation = internal_relocs;
1751 /* Get a canonical list of relocs. */
1754 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1762 if (section->flags & SEC_CONSTRUCTOR)
1764 arelent_chain *chain;
1766 /* This section has relocs made up by us, not the file, so take
1767 them out of their chain and place them into the data area
1769 for (count = 0, chain = section->constructor_chain;
1770 count < section->reloc_count;
1771 count++, chain = chain->next)
1772 *relptr++ = &chain->relent;
1778 if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1781 tblptr = section->relocation;
1783 for (count = 0; count < section->reloc_count; count++)
1784 *relptr++ = tblptr++;
1787 *relptr = (arelent *) NULL;
1789 return section->reloc_count;
1792 /* Provided a BFD, a section and an offset into the section, calculate
1793 and return the name of the source file and the line nearest to the
1797 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1798 filename_ptr, functionname_ptr, retline_ptr)
1801 asymbol **ignore_symbols ATTRIBUTE_UNUSED;
1803 const char **filename_ptr;
1804 const char **functionname_ptr;
1805 unsigned int *retline_ptr;
1807 const struct ecoff_debug_swap * const debug_swap
1808 = &ecoff_backend (abfd)->debug_swap;
1809 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1810 struct ecoff_find_line *line_info;
1812 /* Make sure we have the FDR's. */
1813 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1814 || bfd_get_symcount (abfd) == 0)
1817 if (ecoff_data (abfd)->find_line_info == NULL)
1819 bfd_size_type amt = sizeof (struct ecoff_find_line);
1820 ecoff_data (abfd)->find_line_info
1821 = (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1822 if (ecoff_data (abfd)->find_line_info == NULL)
1825 line_info = ecoff_data (abfd)->find_line_info;
1827 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1828 debug_swap, line_info, filename_ptr,
1829 functionname_ptr, retline_ptr);
1832 /* Copy private BFD data. This is called by objcopy and strip. We
1833 use it to copy the ECOFF debugging information from one BFD to the
1834 other. It would be theoretically possible to represent the ECOFF
1835 debugging information in the symbol table. However, it would be a
1836 lot of work, and there would be little gain (gas, gdb, and ld
1837 already access the ECOFF debugging information via the
1838 ecoff_debug_info structure, and that structure would have to be
1839 retained in order to support ECOFF debugging in MIPS ELF).
1841 The debugging information for the ECOFF external symbols comes from
1842 the symbol table, so this function only handles the other debugging
1846 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1850 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1851 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1853 asymbol **sym_ptr_ptr;
1857 /* We only want to copy information over if both BFD's use ECOFF
1859 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1860 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1863 /* Copy the GP value and the register masks. */
1864 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1865 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1866 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1867 for (i = 0; i < 3; i++)
1868 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1870 /* Copy the version stamp. */
1871 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1873 /* If there are no symbols, don't copy any debugging information. */
1874 c = bfd_get_symcount (obfd);
1875 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1876 if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1879 /* See if there are any local symbols. */
1881 for (; c > 0; c--, sym_ptr_ptr++)
1883 if (ecoffsymbol (*sym_ptr_ptr)->local)
1892 /* There are some local symbols. We just bring over all the
1893 debugging information. FIXME: This is not quite the right
1894 thing to do. If the user has asked us to discard all
1895 debugging information, then we are probably going to wind up
1896 keeping it because there will probably be some local symbol
1897 which objcopy did not discard. We should actually break
1898 apart the debugging information and only keep that which
1899 applies to the symbols we want to keep. */
1900 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1901 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1902 oinfo->line = iinfo->line;
1904 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1905 oinfo->external_dnr = iinfo->external_dnr;
1907 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1908 oinfo->external_pdr = iinfo->external_pdr;
1910 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1911 oinfo->external_sym = iinfo->external_sym;
1913 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1914 oinfo->external_opt = iinfo->external_opt;
1916 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1917 oinfo->external_aux = iinfo->external_aux;
1919 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1920 oinfo->ss = iinfo->ss;
1922 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1923 oinfo->external_fdr = iinfo->external_fdr;
1925 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1926 oinfo->external_rfd = iinfo->external_rfd;
1930 /* We are discarding all the local symbol information. Look
1931 through the external symbols and remove all references to FDR
1932 or aux information. */
1933 c = bfd_get_symcount (obfd);
1934 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1935 for (; c > 0; c--, sym_ptr_ptr++)
1939 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1940 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1942 esym.asym.index = indexNil;
1943 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1944 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1951 /* Set the architecture. The supported architecture is stored in the
1952 backend pointer. We always set the architecture anyhow, since many
1953 callers ignore the return value. */
1956 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1958 enum bfd_architecture arch;
1959 unsigned long machine;
1961 bfd_default_set_arch_mach (abfd, arch, machine);
1962 return arch == ecoff_backend (abfd)->arch;
1965 /* Get the size of the section headers. */
1968 _bfd_ecoff_sizeof_headers (abfd, reloc)
1970 boolean reloc ATTRIBUTE_UNUSED;
1977 for (current = abfd->sections;
1978 current != (asection *)NULL;
1979 current = current->next)
1982 ret = (bfd_coff_filhsz (abfd)
1983 + bfd_coff_aoutsz (abfd)
1984 + c * bfd_coff_scnhsz (abfd));
1985 return BFD_ALIGN (ret, 16);
1988 /* Get the contents of a section. */
1991 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
1996 bfd_size_type count;
1998 return _bfd_generic_get_section_contents (abfd, section, location,
2002 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2003 called via qsort. */
2006 ecoff_sort_hdrs (arg1, arg2)
2010 const asection *hdr1 = *(const asection **) arg1;
2011 const asection *hdr2 = *(const asection **) arg2;
2013 if ((hdr1->flags & SEC_ALLOC) != 0)
2015 if ((hdr2->flags & SEC_ALLOC) == 0)
2020 if ((hdr2->flags & SEC_ALLOC) != 0)
2023 if (hdr1->vma < hdr2->vma)
2025 else if (hdr1->vma > hdr2->vma)
2031 /* Calculate the file position for each section, and set
2035 ecoff_compute_section_file_positions (abfd)
2038 file_ptr sofar, file_sofar;
2039 asection **sorted_hdrs;
2043 boolean rdata_in_text;
2044 boolean first_data, first_nonalloc;
2045 const bfd_vma round = ecoff_backend (abfd)->round;
2048 sofar = _bfd_ecoff_sizeof_headers (abfd, false);
2051 /* Sort the sections by VMA. */
2052 amt = abfd->section_count;
2053 amt *= sizeof (asection *);
2054 sorted_hdrs = (asection **) bfd_malloc (amt);
2055 if (sorted_hdrs == NULL)
2057 for (current = abfd->sections, i = 0;
2059 current = current->next, i++)
2060 sorted_hdrs[i] = current;
2061 BFD_ASSERT (i == abfd->section_count);
2063 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2066 /* Some versions of the OSF linker put the .rdata section in the
2067 text segment, and some do not. */
2068 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2071 for (i = 0; i < abfd->section_count; i++)
2073 current = sorted_hdrs[i];
2074 if (strcmp (current->name, _RDATA) == 0)
2076 if ((current->flags & SEC_CODE) == 0
2077 && strcmp (current->name, _PDATA) != 0
2078 && strcmp (current->name, _RCONST) != 0)
2080 rdata_in_text = false;
2085 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2088 first_nonalloc = true;
2089 for (i = 0; i < abfd->section_count; i++)
2091 unsigned int alignment_power;
2093 current = sorted_hdrs[i];
2095 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2096 supposed to indicate the number of .pdata entries that are
2097 really in the section. Each entry is 8 bytes. We store this
2098 away in line_filepos before increasing the section size. */
2099 if (strcmp (current->name, _PDATA) == 0)
2100 current->line_filepos = current->_raw_size / 8;
2102 alignment_power = current->alignment_power;
2104 /* On Ultrix, the data sections in an executable file must be
2105 aligned to a page boundary within the file. This does not
2106 affect the section size, though. FIXME: Does this work for
2107 other platforms? It requires some modification for the
2108 Alpha, because .rdata on the Alpha goes with the text, not
2110 if ((abfd->flags & EXEC_P) != 0
2111 && (abfd->flags & D_PAGED) != 0
2113 && (current->flags & SEC_CODE) == 0
2115 || strcmp (current->name, _RDATA) != 0)
2116 && strcmp (current->name, _PDATA) != 0
2117 && strcmp (current->name, _RCONST) != 0)
2119 sofar = (sofar + round - 1) &~ (round - 1);
2120 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2123 else if (strcmp (current->name, _LIB) == 0)
2125 /* On Irix 4, the location of contents of the .lib section
2126 from a shared library section is also rounded up to a
2129 sofar = (sofar + round - 1) &~ (round - 1);
2130 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2132 else if (first_nonalloc
2133 && (current->flags & SEC_ALLOC) == 0
2134 && (abfd->flags & D_PAGED) != 0)
2136 /* Skip up to the next page for an unallocated section, such
2137 as the .comment section on the Alpha. This leaves room
2138 for the .bss section. */
2139 first_nonalloc = false;
2140 sofar = (sofar + round - 1) &~ (round - 1);
2141 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2144 /* Align the sections in the file to the same boundary on
2145 which they are aligned in virtual memory. */
2146 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2147 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2148 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2150 if ((abfd->flags & D_PAGED) != 0
2151 && (current->flags & SEC_ALLOC) != 0)
2153 sofar += (current->vma - sofar) % round;
2154 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2155 file_sofar += (current->vma - file_sofar) % round;
2158 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2159 current->filepos = file_sofar;
2161 sofar += current->_raw_size;
2162 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2163 file_sofar += current->_raw_size;
2165 /* Make sure that this section is of the right size too. */
2167 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2168 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2169 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2170 current->_raw_size += sofar - old_sofar;
2176 ecoff_data (abfd)->reloc_filepos = file_sofar;
2181 /* Determine the location of the relocs for all the sections in the
2182 output file, as well as the location of the symbolic debugging
2185 static bfd_size_type
2186 ecoff_compute_reloc_file_positions (abfd)
2189 const bfd_size_type external_reloc_size =
2190 ecoff_backend (abfd)->external_reloc_size;
2191 file_ptr reloc_base;
2192 bfd_size_type reloc_size;
2196 if (! abfd->output_has_begun)
2198 if (! ecoff_compute_section_file_positions (abfd))
2200 abfd->output_has_begun = true;
2203 reloc_base = ecoff_data (abfd)->reloc_filepos;
2206 for (current = abfd->sections;
2207 current != (asection *)NULL;
2208 current = current->next)
2210 if (current->reloc_count == 0)
2211 current->rel_filepos = 0;
2214 bfd_size_type relsize;
2216 current->rel_filepos = reloc_base;
2217 relsize = current->reloc_count * external_reloc_size;
2218 reloc_size += relsize;
2219 reloc_base += relsize;
2223 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2225 /* At least on Ultrix, the symbol table of an executable file must
2226 be aligned to a page boundary. FIXME: Is this true on other
2228 if ((abfd->flags & EXEC_P) != 0
2229 && (abfd->flags & D_PAGED) != 0)
2230 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2231 &~ (ecoff_backend (abfd)->round - 1));
2233 ecoff_data (abfd)->sym_filepos = sym_base;
2238 /* Set the contents of a section. */
2241 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2246 bfd_size_type count;
2250 /* This must be done first, because bfd_set_section_contents is
2251 going to set output_has_begun to true. */
2252 if (! abfd->output_has_begun)
2254 if (! ecoff_compute_section_file_positions (abfd))
2258 /* Handle the .lib section specially so that Irix 4 shared libraries
2259 work out. See coff_set_section_contents in coffcode.h. */
2260 if (strcmp (section->name, _LIB) == 0)
2262 bfd_byte *rec, *recend;
2264 rec = (bfd_byte *) location;
2265 recend = rec + count;
2266 while (rec < recend)
2269 rec += bfd_get_32 (abfd, rec) * 4;
2272 BFD_ASSERT (rec == recend);
2278 pos = section->filepos + offset;
2279 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2280 || bfd_bwrite (location, count, abfd) != count)
2286 /* Get the GP value for an ECOFF file. This is a hook used by
2290 bfd_ecoff_get_gp_value (abfd)
2293 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2294 || bfd_get_format (abfd) != bfd_object)
2296 bfd_set_error (bfd_error_invalid_operation);
2300 return ecoff_data (abfd)->gp;
2303 /* Set the GP value for an ECOFF file. This is a hook used by the
2307 bfd_ecoff_set_gp_value (abfd, gp_value)
2311 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2312 || bfd_get_format (abfd) != bfd_object)
2314 bfd_set_error (bfd_error_invalid_operation);
2318 ecoff_data (abfd)->gp = gp_value;
2323 /* Set the register masks for an ECOFF file. This is a hook used by
2327 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2329 unsigned long gprmask;
2330 unsigned long fprmask;
2331 unsigned long *cprmask;
2333 ecoff_data_type *tdata;
2335 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2336 || bfd_get_format (abfd) != bfd_object)
2338 bfd_set_error (bfd_error_invalid_operation);
2342 tdata = ecoff_data (abfd);
2343 tdata->gprmask = gprmask;
2344 tdata->fprmask = fprmask;
2345 if (cprmask != (unsigned long *) NULL)
2349 for (i = 0; i < 3; i++)
2350 tdata->cprmask[i] = cprmask[i];
2356 /* Get ECOFF EXTR information for an external symbol. This function
2357 is passed to bfd_ecoff_debug_externals. */
2360 ecoff_get_extr (sym, esym)
2364 ecoff_symbol_type *ecoff_sym_ptr;
2367 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2368 || ecoffsymbol (sym)->native == NULL)
2370 /* Don't include debugging, local, or section symbols. */
2371 if ((sym->flags & BSF_DEBUGGING) != 0
2372 || (sym->flags & BSF_LOCAL) != 0
2373 || (sym->flags & BSF_SECTION_SYM) != 0)
2377 esym->cobol_main = 0;
2378 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2381 /* FIXME: we can do better than this for st and sc. */
2382 esym->asym.st = stGlobal;
2383 esym->asym.sc = scAbs;
2384 esym->asym.reserved = 0;
2385 esym->asym.index = indexNil;
2389 ecoff_sym_ptr = ecoffsymbol (sym);
2391 if (ecoff_sym_ptr->local)
2394 input_bfd = bfd_asymbol_bfd (sym);
2395 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2396 (input_bfd, ecoff_sym_ptr->native, esym);
2398 /* If the symbol was defined by the linker, then esym will be
2399 undefined but sym will not be. Get a better class for such a
2401 if ((esym->asym.sc == scUndefined
2402 || esym->asym.sc == scSUndefined)
2403 && ! bfd_is_und_section (bfd_get_section (sym)))
2404 esym->asym.sc = scAbs;
2406 /* Adjust the FDR index for the symbol by that used for the input
2408 if (esym->ifd != -1)
2410 struct ecoff_debug_info *input_debug;
2412 input_debug = &ecoff_data (input_bfd)->debug_info;
2413 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2414 if (input_debug->ifdmap != (RFDT *) NULL)
2415 esym->ifd = input_debug->ifdmap[esym->ifd];
2421 /* Set the external symbol index. This routine is passed to
2422 bfd_ecoff_debug_externals. */
2425 ecoff_set_index (sym, indx)
2429 ecoff_set_sym_index (sym, indx);
2432 /* Write out an ECOFF file. */
2435 _bfd_ecoff_write_object_contents (abfd)
2438 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2439 const bfd_vma round = backend->round;
2440 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2441 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2442 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2443 const bfd_size_type external_hdr_size
2444 = backend->debug_swap.external_hdr_size;
2445 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2446 void (* const adjust_reloc_out) PARAMS ((bfd *,
2448 struct internal_reloc *))
2449 = backend->adjust_reloc_out;
2450 void (* const swap_reloc_out) PARAMS ((bfd *,
2451 const struct internal_reloc *,
2453 = backend->swap_reloc_out;
2454 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2455 HDRR * const symhdr = &debug->symbolic_header;
2458 bfd_size_type reloc_size;
2459 bfd_size_type text_size;
2461 boolean set_text_start;
2462 bfd_size_type data_size;
2464 boolean set_data_start;
2465 bfd_size_type bss_size;
2467 PTR reloc_buff = NULL;
2468 struct internal_filehdr internal_f;
2469 struct internal_aouthdr internal_a;
2472 /* Determine where the sections and relocs will go in the output
2474 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2477 for (current = abfd->sections;
2478 current != (asection *)NULL;
2479 current = current->next)
2481 current->target_index = count;
2485 if ((abfd->flags & D_PAGED) != 0)
2486 text_size = _bfd_ecoff_sizeof_headers (abfd, false);
2490 set_text_start = false;
2493 set_data_start = false;
2496 /* Write section headers to the file. */
2498 /* Allocate buff big enough to hold a section header,
2499 file header, or a.out header. */
2507 buff = (PTR) bfd_malloc (siz);
2512 internal_f.f_nscns = 0;
2513 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2515 for (current = abfd->sections;
2516 current != (asection *) NULL;
2517 current = current->next)
2519 struct internal_scnhdr section;
2522 ++internal_f.f_nscns;
2524 strncpy (section.s_name, current->name, sizeof section.s_name);
2526 /* This seems to be correct for Irix 4 shared libraries. */
2527 vma = bfd_get_section_vma (abfd, current);
2528 if (strcmp (current->name, _LIB) == 0)
2529 section.s_vaddr = 0;
2531 section.s_vaddr = vma;
2533 section.s_paddr = current->lma;
2534 section.s_size = bfd_get_section_size_before_reloc (current);
2536 /* If this section is unloadable then the scnptr will be 0. */
2537 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2538 section.s_scnptr = 0;
2540 section.s_scnptr = current->filepos;
2541 section.s_relptr = current->rel_filepos;
2543 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2544 object file produced by the assembler is supposed to point to
2545 information about how much room is required by objects of
2546 various different sizes. I think this only matters if we
2547 want the linker to compute the best size to use, or
2548 something. I don't know what happens if the information is
2550 if (strcmp (current->name, _PDATA) != 0)
2551 section.s_lnnoptr = 0;
2554 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2555 hold the number of entries in the section (each entry is
2556 8 bytes). We stored this in the line_filepos field in
2557 ecoff_compute_section_file_positions. */
2558 section.s_lnnoptr = current->line_filepos;
2561 section.s_nreloc = current->reloc_count;
2562 section.s_nlnno = 0;
2563 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2566 if (bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff) == 0
2567 || bfd_bwrite (buff, scnhsz, abfd) != scnhsz)
2570 if ((section.s_flags & STYP_TEXT) != 0
2571 || ((section.s_flags & STYP_RDATA) != 0
2572 && ecoff_data (abfd)->rdata_in_text)
2573 || section.s_flags == STYP_PDATA
2574 || (section.s_flags & STYP_DYNAMIC) != 0
2575 || (section.s_flags & STYP_LIBLIST) != 0
2576 || (section.s_flags & STYP_RELDYN) != 0
2577 || section.s_flags == STYP_CONFLIC
2578 || (section.s_flags & STYP_DYNSTR) != 0
2579 || (section.s_flags & STYP_DYNSYM) != 0
2580 || (section.s_flags & STYP_HASH) != 0
2581 || (section.s_flags & STYP_ECOFF_INIT) != 0
2582 || (section.s_flags & STYP_ECOFF_FINI) != 0
2583 || section.s_flags == STYP_RCONST)
2585 text_size += bfd_get_section_size_before_reloc (current);
2586 if (! set_text_start || text_start > vma)
2589 set_text_start = true;
2592 else if ((section.s_flags & STYP_RDATA) != 0
2593 || (section.s_flags & STYP_DATA) != 0
2594 || (section.s_flags & STYP_LITA) != 0
2595 || (section.s_flags & STYP_LIT8) != 0
2596 || (section.s_flags & STYP_LIT4) != 0
2597 || (section.s_flags & STYP_SDATA) != 0
2598 || section.s_flags == STYP_XDATA
2599 || (section.s_flags & STYP_GOT) != 0)
2601 data_size += bfd_get_section_size_before_reloc (current);
2602 if (! set_data_start || data_start > vma)
2605 set_data_start = true;
2608 else if ((section.s_flags & STYP_BSS) != 0
2609 || (section.s_flags & STYP_SBSS) != 0)
2610 bss_size += bfd_get_section_size_before_reloc (current);
2611 else if (section.s_flags == 0
2612 || (section.s_flags & STYP_ECOFF_LIB) != 0
2613 || section.s_flags == STYP_COMMENT)
2619 /* Set up the file header. */
2620 internal_f.f_magic = ecoff_get_magic (abfd);
2622 /* We will NOT put a fucking timestamp in the header here. Every
2623 time you put it back, I will come in and take it out again. I'm
2624 sorry. This field does not belong here. We fill it with a 0 so
2625 it compares the same but is not a reasonable time. --
2627 internal_f.f_timdat = 0;
2629 if (bfd_get_symcount (abfd) != 0)
2631 /* The ECOFF f_nsyms field is not actually the number of
2632 symbols, it's the size of symbolic information header. */
2633 internal_f.f_nsyms = external_hdr_size;
2634 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2638 internal_f.f_nsyms = 0;
2639 internal_f.f_symptr = 0;
2642 internal_f.f_opthdr = aoutsz;
2644 internal_f.f_flags = F_LNNO;
2645 if (reloc_size == 0)
2646 internal_f.f_flags |= F_RELFLG;
2647 if (bfd_get_symcount (abfd) == 0)
2648 internal_f.f_flags |= F_LSYMS;
2649 if (abfd->flags & EXEC_P)
2650 internal_f.f_flags |= F_EXEC;
2652 if (bfd_little_endian (abfd))
2653 internal_f.f_flags |= F_AR32WR;
2655 internal_f.f_flags |= F_AR32W;
2657 /* Set up the ``optional'' header. */
2658 if ((abfd->flags & D_PAGED) != 0)
2659 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2661 internal_a.magic = ECOFF_AOUT_OMAGIC;
2663 /* FIXME: Is this really correct? */
2664 internal_a.vstamp = symhdr->vstamp;
2666 /* At least on Ultrix, these have to be rounded to page boundaries.
2667 FIXME: Is this true on other platforms? */
2668 if ((abfd->flags & D_PAGED) != 0)
2670 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2671 internal_a.text_start = text_start &~ (round - 1);
2672 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2673 internal_a.data_start = data_start &~ (round - 1);
2677 internal_a.tsize = text_size;
2678 internal_a.text_start = text_start;
2679 internal_a.dsize = data_size;
2680 internal_a.data_start = data_start;
2683 /* On Ultrix, the initial portions of the .sbss and .bss segments
2684 are at the end of the data section. The bsize field in the
2685 optional header records how many bss bytes are required beyond
2686 those in the data section. The value is not rounded to a page
2688 if (bss_size < internal_a.dsize - data_size)
2691 bss_size -= internal_a.dsize - data_size;
2692 internal_a.bsize = bss_size;
2693 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2695 internal_a.entry = bfd_get_start_address (abfd);
2697 internal_a.gp_value = ecoff_data (abfd)->gp;
2699 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2700 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2701 for (i = 0; i < 4; i++)
2702 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2704 /* Let the backend adjust the headers if necessary. */
2705 if (backend->adjust_headers)
2707 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2711 /* Write out the file header and the optional header. */
2712 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2715 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2716 if (bfd_bwrite (buff, filhsz, abfd) != filhsz)
2719 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2720 if (bfd_bwrite (buff, aoutsz, abfd) != aoutsz)
2723 /* Build the external symbol information. This must be done before
2724 writing out the relocs so that we know the symbol indices. We
2725 don't do this if this BFD was created by the backend linker,
2726 since it will have already handled the symbols and relocs. */
2727 if (! ecoff_data (abfd)->linker)
2729 symhdr->iextMax = 0;
2730 symhdr->issExtMax = 0;
2731 debug->external_ext = debug->external_ext_end = NULL;
2732 debug->ssext = debug->ssext_end = NULL;
2733 if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2734 (abfd->flags & EXEC_P) == 0,
2735 ecoff_get_extr, ecoff_set_index))
2738 /* Write out the relocs. */
2739 for (current = abfd->sections;
2740 current != (asection *) NULL;
2741 current = current->next)
2743 arelent **reloc_ptr_ptr;
2744 arelent **reloc_end;
2748 if (current->reloc_count == 0)
2751 amt = current->reloc_count * external_reloc_size;
2752 reloc_buff = bfd_alloc (abfd, amt);
2753 if (reloc_buff == NULL)
2756 reloc_ptr_ptr = current->orelocation;
2757 reloc_end = reloc_ptr_ptr + current->reloc_count;
2758 out_ptr = (char *) reloc_buff;
2760 reloc_ptr_ptr < reloc_end;
2761 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2765 struct internal_reloc in;
2767 memset ((PTR) &in, 0, sizeof in);
2769 reloc = *reloc_ptr_ptr;
2770 sym = *reloc->sym_ptr_ptr;
2772 in.r_vaddr = (reloc->address
2773 + bfd_get_section_vma (abfd, current));
2774 in.r_type = reloc->howto->type;
2776 if ((sym->flags & BSF_SECTION_SYM) == 0)
2778 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2785 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2786 if (strcmp (name, ".text") == 0)
2787 in.r_symndx = RELOC_SECTION_TEXT;
2788 else if (strcmp (name, ".rdata") == 0)
2789 in.r_symndx = RELOC_SECTION_RDATA;
2790 else if (strcmp (name, ".data") == 0)
2791 in.r_symndx = RELOC_SECTION_DATA;
2792 else if (strcmp (name, ".sdata") == 0)
2793 in.r_symndx = RELOC_SECTION_SDATA;
2794 else if (strcmp (name, ".sbss") == 0)
2795 in.r_symndx = RELOC_SECTION_SBSS;
2796 else if (strcmp (name, ".bss") == 0)
2797 in.r_symndx = RELOC_SECTION_BSS;
2798 else if (strcmp (name, ".init") == 0)
2799 in.r_symndx = RELOC_SECTION_INIT;
2800 else if (strcmp (name, ".lit8") == 0)
2801 in.r_symndx = RELOC_SECTION_LIT8;
2802 else if (strcmp (name, ".lit4") == 0)
2803 in.r_symndx = RELOC_SECTION_LIT4;
2804 else if (strcmp (name, ".xdata") == 0)
2805 in.r_symndx = RELOC_SECTION_XDATA;
2806 else if (strcmp (name, ".pdata") == 0)
2807 in.r_symndx = RELOC_SECTION_PDATA;
2808 else if (strcmp (name, ".fini") == 0)
2809 in.r_symndx = RELOC_SECTION_FINI;
2810 else if (strcmp (name, ".lita") == 0)
2811 in.r_symndx = RELOC_SECTION_LITA;
2812 else if (strcmp (name, "*ABS*") == 0)
2813 in.r_symndx = RELOC_SECTION_ABS;
2814 else if (strcmp (name, ".rconst") == 0)
2815 in.r_symndx = RELOC_SECTION_RCONST;
2821 (*adjust_reloc_out) (abfd, reloc, &in);
2823 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2826 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2828 amt = current->reloc_count * external_reloc_size;
2829 if (bfd_bwrite (reloc_buff, amt, abfd) != amt)
2831 bfd_release (abfd, reloc_buff);
2835 /* Write out the symbolic debugging information. */
2836 if (bfd_get_symcount (abfd) > 0)
2838 /* Write out the debugging information. */
2839 if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2840 ecoff_data (abfd)->sym_filepos))
2845 /* The .bss section of a demand paged executable must receive an
2846 entire page. If there are symbols, the symbols will start on the
2847 next page. If there are no symbols, we must fill out the page by
2849 if (bfd_get_symcount (abfd) == 0
2850 && (abfd->flags & EXEC_P) != 0
2851 && (abfd->flags & D_PAGED) != 0)
2855 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2858 if (bfd_bread (&c, (bfd_size_type) 1, abfd) == 0)
2860 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2863 if (bfd_bwrite (&c, (bfd_size_type) 1, abfd) != 1)
2867 if (reloc_buff != NULL)
2868 bfd_release (abfd, reloc_buff);
2873 if (reloc_buff != NULL)
2874 bfd_release (abfd, reloc_buff);
2880 /* Archive handling. ECOFF uses what appears to be a unique type of
2881 archive header (armap). The byte ordering of the armap and the
2882 contents are encoded in the name of the armap itself. At least for
2883 now, we only support archives with the same byte ordering in the
2884 armap and the contents.
2886 The first four bytes in the armap are the number of symbol
2887 definitions. This is always a power of two.
2889 This is followed by the symbol definitions. Each symbol definition
2890 occupies 8 bytes. The first four bytes are the offset from the
2891 start of the armap strings to the null-terminated string naming
2892 this symbol. The second four bytes are the file offset to the
2893 archive member which defines this symbol. If the second four bytes
2894 are 0, then this is not actually a symbol definition, and it should
2897 The symbols are hashed into the armap with a closed hashing scheme.
2898 See the functions below for the details of the algorithm.
2900 After the symbol definitions comes four bytes holding the size of
2901 the string table, followed by the string table itself. */
2903 /* The name of an archive headers looks like this:
2904 __________E[BL]E[BL]_ (with a trailing space).
2905 The trailing space is changed to an X if the archive is changed to
2906 indicate that the armap is out of date.
2908 The Alpha seems to use ________64E[BL]E[BL]_. */
2910 #define ARMAP_BIG_ENDIAN 'B'
2911 #define ARMAP_LITTLE_ENDIAN 'L'
2912 #define ARMAP_MARKER 'E'
2913 #define ARMAP_START_LENGTH 10
2914 #define ARMAP_HEADER_MARKER_INDEX 10
2915 #define ARMAP_HEADER_ENDIAN_INDEX 11
2916 #define ARMAP_OBJECT_MARKER_INDEX 12
2917 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2918 #define ARMAP_END_INDEX 14
2919 #define ARMAP_END "_ "
2921 /* This is a magic number used in the hashing algorithm. */
2922 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2924 /* This returns the hash value to use for a string. It also sets
2925 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2926 is the number of entries in the hash table, and HLOG is the log
2930 ecoff_armap_hash (s, rehash, size, hlog)
2932 unsigned int *rehash;
2942 hash = ((hash >> 27) | (hash << 5)) + *s++;
2943 hash *= ARMAP_HASH_MAGIC;
2944 *rehash = (hash & (size - 1)) | 1;
2945 return hash >> (32 - hlog);
2948 /* Read in the armap. */
2951 _bfd_ecoff_slurp_armap (abfd)
2956 struct areltdata *mapdata;
2957 bfd_size_type parsed_size;
2959 struct artdata *ardata;
2962 struct symdef *symdef_ptr;
2966 /* Get the name of the first element. */
2967 i = bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd);
2973 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2976 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2977 standard COFF armap. We could move the ECOFF armap stuff into
2978 bfd_slurp_armap, but that seems inappropriate since no other
2979 target uses this format. Instead, we check directly for a COFF
2981 if (strncmp (nextname, "/ ", 16) == 0)
2982 return bfd_slurp_armap (abfd);
2984 /* See if the first element is an armap. */
2985 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2986 ARMAP_START_LENGTH) != 0
2987 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2988 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2989 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2990 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2991 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2992 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2993 || strncmp (nextname + ARMAP_END_INDEX,
2994 ARMAP_END, sizeof ARMAP_END - 1) != 0)
2996 bfd_has_map (abfd) = false;
3000 /* Make sure we have the right byte ordering. */
3001 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3002 ^ (bfd_header_big_endian (abfd)))
3003 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3004 ^ (bfd_big_endian (abfd))))
3006 bfd_set_error (bfd_error_wrong_format);
3010 /* Read in the armap. */
3011 ardata = bfd_ardata (abfd);
3012 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
3013 if (mapdata == (struct areltdata *) NULL)
3015 parsed_size = mapdata->parsed_size;
3016 bfd_release (abfd, (PTR) mapdata);
3018 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3019 if (raw_armap == (char *) NULL)
3022 if (bfd_bread ((PTR) raw_armap, parsed_size, abfd) != parsed_size)
3024 if (bfd_get_error () != bfd_error_system_call)
3025 bfd_set_error (bfd_error_malformed_archive);
3026 bfd_release (abfd, (PTR) raw_armap);
3030 ardata->tdata = (PTR) raw_armap;
3032 count = H_GET_32 (abfd, raw_armap);
3034 ardata->symdef_count = 0;
3035 ardata->cache = (struct ar_cache *) NULL;
3037 /* This code used to overlay the symdefs over the raw archive data,
3038 but that doesn't work on a 64 bit host. */
3039 stringbase = raw_armap + count * 8 + 8;
3041 #ifdef CHECK_ARMAP_HASH
3045 /* Double check that I have the hashing algorithm right by making
3046 sure that every symbol can be looked up successfully. */
3048 for (i = 1; i < count; i <<= 1)
3050 BFD_ASSERT (i == count);
3052 raw_ptr = raw_armap + 4;
3053 for (i = 0; i < count; i++, raw_ptr += 8)
3055 unsigned int name_offset, file_offset;
3056 unsigned int hash, rehash, srch;
3058 name_offset = H_GET_32 (abfd, raw_ptr);
3059 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3060 if (file_offset == 0)
3062 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3067 /* See if we can rehash to this location. */
3068 for (srch = (hash + rehash) & (count - 1);
3069 srch != hash && srch != i;
3070 srch = (srch + rehash) & (count - 1))
3071 BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
3072 BFD_ASSERT (srch == i);
3076 #endif /* CHECK_ARMAP_HASH */
3078 raw_ptr = raw_armap + 4;
3079 for (i = 0; i < count; i++, raw_ptr += 8)
3080 if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
3081 ++ardata->symdef_count;
3083 amt = ardata->symdef_count;
3084 amt *= sizeof (struct symdef);
3085 symdef_ptr = (struct symdef *) bfd_alloc (abfd, amt);
3089 ardata->symdefs = (carsym *) symdef_ptr;
3091 raw_ptr = raw_armap + 4;
3092 for (i = 0; i < count; i++, raw_ptr += 8)
3094 unsigned int name_offset, file_offset;
3096 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3097 if (file_offset == 0)
3099 name_offset = H_GET_32 (abfd, raw_ptr);
3100 symdef_ptr->s.name = stringbase + name_offset;
3101 symdef_ptr->file_offset = file_offset;
3105 ardata->first_file_filepos = bfd_tell (abfd);
3106 /* Pad to an even boundary. */
3107 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3109 bfd_has_map (abfd) = true;
3114 /* Write out an armap. */
3117 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3119 unsigned int elength;
3121 unsigned int orl_count;
3124 unsigned int hashsize, hashlog;
3125 bfd_size_type symdefsize;
3127 unsigned int stringsize;
3128 unsigned int mapsize;
3131 struct stat statbuf;
3134 bfd_byte *hashtable;
3138 /* Ultrix appears to use as a hash table size the least power of two
3139 greater than twice the number of entries. */
3140 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3142 hashsize = 1 << hashlog;
3144 symdefsize = hashsize * 8;
3146 stringsize = stridx + padit;
3148 /* Include 8 bytes to store symdefsize and stringsize in output. */
3149 mapsize = symdefsize + stringsize + 8;
3151 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3153 memset ((PTR) &hdr, 0, sizeof hdr);
3155 /* Work out the ECOFF armap name. */
3156 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3157 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3158 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3159 (bfd_header_big_endian (abfd)
3161 : ARMAP_LITTLE_ENDIAN);
3162 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3163 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3164 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3165 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3167 /* Write the timestamp of the archive header to be just a little bit
3168 later than the timestamp of the file, otherwise the linker will
3169 complain that the index is out of date. Actually, the Ultrix
3170 linker just checks the archive name; the GNU linker may check the
3172 stat (abfd->filename, &statbuf);
3173 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3175 /* The DECstation uses zeroes for the uid, gid and mode of the
3177 hdr.ar_uid[0] = '0';
3178 hdr.ar_gid[0] = '0';
3180 hdr.ar_mode[0] = '0';
3182 /* Building gcc ends up extracting the armap as a file - twice. */
3183 hdr.ar_mode[0] = '6';
3184 hdr.ar_mode[1] = '4';
3185 hdr.ar_mode[2] = '4';
3188 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3190 hdr.ar_fmag[0] = '`';
3191 hdr.ar_fmag[1] = '\012';
3193 /* Turn all null bytes in the header into spaces. */
3194 for (i = 0; i < sizeof (struct ar_hdr); i++)
3195 if (((char *) (&hdr))[i] == '\0')
3196 (((char *) (&hdr))[i]) = ' ';
3198 if (bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), abfd)
3199 != sizeof (struct ar_hdr))
3202 H_PUT_32 (abfd, hashsize, temp);
3203 if (bfd_bwrite ((PTR) temp, (bfd_size_type) 4, abfd) != 4)
3206 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3210 current = abfd->archive_head;
3212 for (i = 0; i < orl_count; i++)
3214 unsigned int hash, rehash;
3216 /* Advance firstreal to the file position of this archive
3218 if (map[i].u.abfd != last_elt)
3222 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3223 firstreal += firstreal % 2;
3224 current = current->next;
3226 while (current != map[i].u.abfd);
3231 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3232 if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
3236 /* The desired slot is already taken. */
3237 for (srch = (hash + rehash) & (hashsize - 1);
3239 srch = (srch + rehash) & (hashsize - 1))
3240 if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
3243 BFD_ASSERT (srch != hash);
3248 H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
3249 H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
3252 if (bfd_bwrite ((PTR) hashtable, symdefsize, abfd) != symdefsize)
3255 bfd_release (abfd, hashtable);
3257 /* Now write the strings. */
3258 H_PUT_32 (abfd, stringsize, temp);
3259 if (bfd_bwrite ((PTR) temp, (bfd_size_type) 4, abfd) != 4)
3261 for (i = 0; i < orl_count; i++)
3265 len = strlen (*map[i].name) + 1;
3266 if (bfd_bwrite ((PTR) (*map[i].name), len, abfd) != len)
3270 /* The spec sez this should be a newline. But in order to be
3271 bug-compatible for DECstation ar we use a null. */
3274 if (bfd_bwrite ("", (bfd_size_type) 1, abfd) != 1)
3281 /* See whether this BFD is an archive. If it is, read in the armap
3282 and the extended name table. */
3285 _bfd_ecoff_archive_p (abfd)
3288 struct artdata *tdata_hold;
3289 char armag[SARMAG + 1];
3292 tdata_hold = abfd->tdata.aout_ar_data;
3294 if (bfd_bread ((PTR) armag, (bfd_size_type) SARMAG, abfd) != SARMAG)
3296 if (bfd_get_error () != bfd_error_system_call)
3297 bfd_set_error (bfd_error_wrong_format);
3298 return (const bfd_target *) NULL;
3301 if (strncmp (armag, ARMAG, SARMAG) != 0)
3303 bfd_set_error (bfd_error_wrong_format);
3307 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3308 involves a cast, we can't do it as the left operand of
3310 amt = sizeof (struct artdata);
3311 abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
3313 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3315 abfd->tdata.aout_ar_data = tdata_hold;
3316 return (const bfd_target *) NULL;
3319 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3320 bfd_ardata (abfd)->cache = NULL;
3321 bfd_ardata (abfd)->archive_head = NULL;
3322 bfd_ardata (abfd)->symdefs = NULL;
3323 bfd_ardata (abfd)->extended_names = NULL;
3324 bfd_ardata (abfd)->tdata = NULL;
3326 if (! _bfd_ecoff_slurp_armap (abfd)
3327 || ! _bfd_ecoff_slurp_extended_name_table (abfd))
3329 bfd_release (abfd, bfd_ardata (abfd));
3330 abfd->tdata.aout_ar_data = tdata_hold;
3331 return (const bfd_target *) NULL;
3334 if (bfd_has_map (abfd))
3338 /* This archive has a map, so we may presume that the contents
3339 are object files. Make sure that if the first file in the
3340 archive can be recognized as an object file, it is for this
3341 target. If not, assume that this is the wrong format. If
3342 the first file is not an object file, somebody is doing
3343 something weird, and we permit it so that ar -t will work. */
3345 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3350 first->target_defaulted = false;
3352 if (bfd_check_format (first, bfd_object)
3353 && first->xvec != abfd->xvec)
3355 (void) bfd_close (first);
3356 bfd_release (abfd, bfd_ardata (abfd));
3357 abfd->tdata.aout_ar_data = tdata_hold;
3358 bfd_set_error (bfd_error_wrong_format);
3362 /* We ought to close first here, but we can't, because we
3363 have no way to remove it from the archive cache. FIXME. */
3370 /* ECOFF linker code. */
3372 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3373 PARAMS ((struct bfd_hash_entry *entry,
3374 struct bfd_hash_table *table,
3375 const char *string));
3376 static boolean ecoff_link_add_archive_symbols
3377 PARAMS ((bfd *, struct bfd_link_info *));
3378 static boolean ecoff_link_check_archive_element
3379 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3380 static boolean ecoff_link_add_object_symbols
3381 PARAMS ((bfd *, struct bfd_link_info *));
3382 static boolean ecoff_link_add_externals
3383 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3385 /* Routine to create an entry in an ECOFF link hash table. */
3387 static struct bfd_hash_entry *
3388 ecoff_link_hash_newfunc (entry, table, string)
3389 struct bfd_hash_entry *entry;
3390 struct bfd_hash_table *table;
3393 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3395 /* Allocate the structure if it has not already been allocated by a
3397 if (ret == (struct ecoff_link_hash_entry *) NULL)
3398 ret = ((struct ecoff_link_hash_entry *)
3399 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3400 if (ret == (struct ecoff_link_hash_entry *) NULL)
3403 /* Call the allocation method of the superclass. */
3404 ret = ((struct ecoff_link_hash_entry *)
3405 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3410 /* Set local fields. */
3416 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3418 return (struct bfd_hash_entry *) ret;
3421 /* Create an ECOFF link hash table. */
3423 struct bfd_link_hash_table *
3424 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3427 struct ecoff_link_hash_table *ret;
3428 bfd_size_type amt = sizeof (struct ecoff_link_hash_table);
3430 ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3433 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3434 ecoff_link_hash_newfunc))
3437 return (struct bfd_link_hash_table *) NULL;
3442 /* Look up an entry in an ECOFF link hash table. */
3444 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3445 ((struct ecoff_link_hash_entry *) \
3446 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3448 /* Traverse an ECOFF link hash table. */
3450 #define ecoff_link_hash_traverse(table, func, info) \
3451 (bfd_link_hash_traverse \
3453 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3456 /* Get the ECOFF link hash table from the info structure. This is
3459 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3461 /* Given an ECOFF BFD, add symbols to the global hash table as
3465 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3467 struct bfd_link_info *info;
3469 switch (bfd_get_format (abfd))
3472 return ecoff_link_add_object_symbols (abfd, info);
3474 return ecoff_link_add_archive_symbols (abfd, info);
3476 bfd_set_error (bfd_error_wrong_format);
3481 /* Add the symbols from an archive file to the global hash table.
3482 This looks through the undefined symbols, looks each one up in the
3483 archive hash table, and adds any associated object file. We do not
3484 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3485 already have a hash table, so there is no reason to construct
3489 ecoff_link_add_archive_symbols (abfd, info)
3491 struct bfd_link_info *info;
3493 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3494 const bfd_byte *raw_armap;
3495 struct bfd_link_hash_entry **pundef;
3496 unsigned int armap_count;
3497 unsigned int armap_log;
3499 const bfd_byte *hashtable;
3500 const char *stringbase;
3502 if (! bfd_has_map (abfd))
3504 /* An empty archive is a special case. */
3505 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3507 bfd_set_error (bfd_error_no_armap);
3511 /* If we don't have any raw data for this archive, as can happen on
3512 Irix 4.0.5F, we call the generic routine.
3513 FIXME: We should be more clever about this, since someday tdata
3514 may get to something for a generic archive. */
3515 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3516 if (raw_armap == (bfd_byte *) NULL)
3517 return (_bfd_generic_link_add_archive_symbols
3518 (abfd, info, ecoff_link_check_archive_element));
3520 armap_count = H_GET_32 (abfd, raw_armap);
3523 for (i = 1; i < armap_count; i <<= 1)
3525 BFD_ASSERT (i == armap_count);
3527 hashtable = raw_armap + 4;
3528 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3530 /* Look through the list of undefined symbols. */
3531 pundef = &info->hash->undefs;
3532 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3534 struct bfd_link_hash_entry *h;
3535 unsigned int hash, rehash;
3536 unsigned int file_offset;
3542 /* When a symbol is defined, it is not necessarily removed from
3544 if (h->type != bfd_link_hash_undefined
3545 && h->type != bfd_link_hash_common)
3547 /* Remove this entry from the list, for general cleanliness
3548 and because we are going to look through the list again
3549 if we search any more libraries. We can't remove the
3550 entry if it is the tail, because that would lose any
3551 entries we add to the list later on. */
3552 if (*pundef != info->hash->undefs_tail)
3553 *pundef = (*pundef)->next;
3555 pundef = &(*pundef)->next;
3559 /* Native ECOFF linkers do not pull in archive elements merely
3560 to satisfy common definitions, so neither do we. We leave
3561 them on the list, though, in case we are linking against some
3562 other object format. */
3563 if (h->type != bfd_link_hash_undefined)
3565 pundef = &(*pundef)->next;
3569 /* Look for this symbol in the archive hash table. */
3570 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3573 file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3574 if (file_offset == 0)
3576 /* Nothing in this slot. */
3577 pundef = &(*pundef)->next;
3581 name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3582 if (name[0] != h->root.string[0]
3583 || strcmp (name, h->root.string) != 0)
3588 /* That was the wrong symbol. Try rehashing. */
3590 for (srch = (hash + rehash) & (armap_count - 1);
3592 srch = (srch + rehash) & (armap_count - 1))
3594 file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3595 if (file_offset == 0)
3597 name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3598 if (name[0] == h->root.string[0]
3599 && strcmp (name, h->root.string) == 0)
3608 pundef = &(*pundef)->next;
3615 element = (*backend->get_elt_at_filepos) (abfd, (file_ptr) file_offset);
3616 if (element == (bfd *) NULL)
3619 if (! bfd_check_format (element, bfd_object))
3622 /* Unlike the generic linker, we know that this element provides
3623 a definition for an undefined symbol and we know that we want
3624 to include it. We don't need to check anything. */
3625 if (! (*info->callbacks->add_archive_element) (info, element, name))
3627 if (! ecoff_link_add_object_symbols (element, info))
3630 pundef = &(*pundef)->next;
3636 /* This is called if we used _bfd_generic_link_add_archive_symbols
3637 because we were not dealing with an ECOFF archive. */
3640 ecoff_link_check_archive_element (abfd, info, pneeded)
3642 struct bfd_link_info *info;
3645 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3646 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3647 = backend->debug_swap.swap_ext_in;
3649 bfd_size_type external_ext_size;
3650 PTR external_ext = NULL;
3651 bfd_size_type esize;
3658 if (! ecoff_slurp_symbolic_header (abfd))
3661 /* If there are no symbols, we don't want it. */
3662 if (bfd_get_symcount (abfd) == 0)
3663 goto successful_return;
3665 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3667 /* Read in the external symbols and external strings. */
3668 external_ext_size = backend->debug_swap.external_ext_size;
3669 esize = symhdr->iextMax * external_ext_size;
3670 external_ext = (PTR) bfd_malloc (esize);
3671 if (external_ext == NULL && esize != 0)
3674 if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
3675 || bfd_bread (external_ext, esize, abfd) != esize)
3678 ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
3679 if (ssext == NULL && symhdr->issExtMax != 0)
3682 if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
3683 || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
3684 != (bfd_size_type) symhdr->issExtMax))
3687 /* Look through the external symbols to see if they define some
3688 symbol that is currently undefined. */
3689 ext_ptr = (char *) external_ext;
3690 ext_end = ext_ptr + esize;
3691 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3696 struct bfd_link_hash_entry *h;
3698 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3700 /* See if this symbol defines something. */
3701 if (esym.asym.st != stGlobal
3702 && esym.asym.st != stLabel
3703 && esym.asym.st != stProc)
3706 switch (esym.asym.sc)
3730 name = ssext + esym.asym.iss;
3731 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3733 /* Unlike the generic linker, we do not pull in elements because
3734 of common symbols. */
3735 if (h == (struct bfd_link_hash_entry *) NULL
3736 || h->type != bfd_link_hash_undefined)
3739 /* Include this element. */
3740 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3742 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3746 goto successful_return;
3750 if (external_ext != NULL)
3751 free (external_ext);
3756 if (external_ext != NULL)
3757 free (external_ext);
3763 /* Add symbols from an ECOFF object file to the global linker hash
3767 ecoff_link_add_object_symbols (abfd, info)
3769 struct bfd_link_info *info;
3772 bfd_size_type external_ext_size;
3773 PTR external_ext = NULL;
3774 bfd_size_type esize;
3778 if (! ecoff_slurp_symbolic_header (abfd))
3781 /* If there are no symbols, we don't want it. */
3782 if (bfd_get_symcount (abfd) == 0)
3785 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3787 /* Read in the external symbols and external strings. */
3788 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3789 esize = symhdr->iextMax * external_ext_size;
3790 external_ext = (PTR) bfd_malloc (esize);
3791 if (external_ext == NULL && esize != 0)
3794 if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
3795 || bfd_bread (external_ext, esize, abfd) != esize)
3798 ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
3799 if (ssext == NULL && symhdr->issExtMax != 0)
3802 if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
3803 || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
3804 != (bfd_size_type) symhdr->issExtMax))
3807 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3811 if (external_ext != NULL)
3812 free (external_ext);
3818 if (external_ext != NULL)
3819 free (external_ext);
3823 /* Add the external symbols of an object file to the global linker
3824 hash table. The external symbols and strings we are passed are
3825 just allocated on the stack, and will be discarded. We must
3826 explicitly save any information we may need later on in the link.
3827 We do not want to read the external symbol information again. */
3830 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3832 struct bfd_link_info *info;
3836 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3837 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3838 = backend->debug_swap.swap_ext_in;
3839 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3840 unsigned long ext_count;
3841 struct ecoff_link_hash_entry **sym_hash;
3846 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3849 amt *= sizeof (struct bfd_link_hash_entry *);
3850 sym_hash = (struct ecoff_link_hash_entry **) bfd_alloc (abfd, amt);
3853 ecoff_data (abfd)->sym_hashes = sym_hash;
3855 ext_ptr = (char *) external_ext;
3856 ext_end = ext_ptr + ext_count * external_ext_size;
3857 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3864 struct ecoff_link_hash_entry *h;
3868 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3870 /* Skip debugging symbols. */
3872 switch (esym.asym.st)
3888 /* Get the information for this symbol. */
3889 value = esym.asym.value;
3890 switch (esym.asym.sc)
3910 section = bfd_make_section_old_way (abfd, ".text");
3911 value -= section->vma;
3914 section = bfd_make_section_old_way (abfd, ".data");
3915 value -= section->vma;
3918 section = bfd_make_section_old_way (abfd, ".bss");
3919 value -= section->vma;
3922 section = bfd_abs_section_ptr;
3925 section = bfd_und_section_ptr;
3928 section = bfd_make_section_old_way (abfd, ".sdata");
3929 value -= section->vma;
3932 section = bfd_make_section_old_way (abfd, ".sbss");
3933 value -= section->vma;
3936 section = bfd_make_section_old_way (abfd, ".rdata");
3937 value -= section->vma;
3940 if (value > ecoff_data (abfd)->gp_size)
3942 section = bfd_com_section_ptr;
3947 if (ecoff_scom_section.name == NULL)
3949 /* Initialize the small common section. */
3950 ecoff_scom_section.name = SCOMMON;
3951 ecoff_scom_section.flags = SEC_IS_COMMON;
3952 ecoff_scom_section.output_section = &ecoff_scom_section;
3953 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3954 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3955 ecoff_scom_symbol.name = SCOMMON;
3956 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3957 ecoff_scom_symbol.section = &ecoff_scom_section;
3958 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3960 section = &ecoff_scom_section;
3963 section = bfd_und_section_ptr;
3966 section = bfd_make_section_old_way (abfd, ".init");
3967 value -= section->vma;
3970 section = bfd_make_section_old_way (abfd, ".fini");
3971 value -= section->vma;
3974 section = bfd_make_section_old_way (abfd, ".rconst");
3975 value -= section->vma;
3979 if (section == (asection *) NULL)
3982 name = ssext + esym.asym.iss;
3985 if (! (_bfd_generic_link_add_one_symbol
3987 (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3988 section, value, (const char *) NULL, true, true,
3989 (struct bfd_link_hash_entry **) &h)))
3994 /* If we are building an ECOFF hash table, save the external
3995 symbol information. */
3996 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3998 if (h->abfd == (bfd *) NULL
3999 || (! bfd_is_und_section (section)
4000 && (! bfd_is_com_section (section)
4001 || (h->root.type != bfd_link_hash_defined
4002 && h->root.type != bfd_link_hash_defweak))))
4008 /* Remember whether this symbol was small undefined. */
4009 if (esym.asym.sc == scSUndefined)
4012 /* If this symbol was ever small undefined, it needs to wind
4013 up in a GP relative section. We can't control the
4014 section of a defined symbol, but we can control the
4015 section of a common symbol. This case is actually needed
4016 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
4018 && h->root.type == bfd_link_hash_common
4019 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
4021 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
4023 h->root.u.c.p->section->flags = SEC_ALLOC;
4024 if (h->esym.asym.sc == scCommon)
4025 h->esym.asym.sc = scSCommon;
4033 /* ECOFF final link routines. */
4035 static boolean ecoff_final_link_debug_accumulate
4036 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4038 static boolean ecoff_link_write_external
4039 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4040 static boolean ecoff_indirect_link_order
4041 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4042 struct bfd_link_order *));
4043 static boolean ecoff_reloc_link_order
4044 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4045 struct bfd_link_order *));
4047 /* Structure used to pass information to ecoff_link_write_external. */
4052 struct bfd_link_info *info;
4055 /* ECOFF final link routine. This looks through all the input BFDs
4056 and gathers together all the debugging information, and then
4057 processes all the link order information. This may cause it to
4058 close and reopen some input BFDs; I'll see how bad this is. */
4061 _bfd_ecoff_bfd_final_link (abfd, info)
4063 struct bfd_link_info *info;
4065 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4066 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4069 register bfd *input_bfd;
4071 struct bfd_link_order *p;
4072 struct extsym_info einfo;
4074 /* We accumulate the debugging information counts in the symbolic
4076 symhdr = &debug->symbolic_header;
4078 symhdr->ilineMax = 0;
4082 symhdr->isymMax = 0;
4083 symhdr->ioptMax = 0;
4084 symhdr->iauxMax = 0;
4086 symhdr->issExtMax = 0;
4089 symhdr->iextMax = 0;
4091 /* We accumulate the debugging information itself in the debug_info
4094 debug->external_dnr = NULL;
4095 debug->external_pdr = NULL;
4096 debug->external_sym = NULL;
4097 debug->external_opt = NULL;
4098 debug->external_aux = NULL;
4100 debug->ssext = debug->ssext_end = NULL;
4101 debug->external_fdr = NULL;
4102 debug->external_rfd = NULL;
4103 debug->external_ext = debug->external_ext_end = NULL;
4105 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4106 if (handle == (PTR) NULL)
4109 /* Accumulate the debugging symbols from each input BFD. */
4110 for (input_bfd = info->input_bfds;
4111 input_bfd != (bfd *) NULL;
4112 input_bfd = input_bfd->link_next)
4116 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4118 /* Abitrarily set the symbolic header vstamp to the vstamp
4119 of the first object file in the link. */
4120 if (symhdr->vstamp == 0)
4122 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4123 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4127 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4128 debug, &backend->debug_swap,
4133 /* Combine the register masks. */
4134 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4135 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4136 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4137 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4138 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4139 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4142 /* Write out the external symbols. */
4145 ecoff_link_hash_traverse (ecoff_hash_table (info),
4146 ecoff_link_write_external,
4149 if (info->relocateable)
4151 /* We need to make a pass over the link_orders to count up the
4152 number of relocations we will need to output, so that we know
4153 how much space they will take up. */
4154 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4157 for (p = o->link_order_head;
4158 p != (struct bfd_link_order *) NULL;
4160 if (p->type == bfd_indirect_link_order)
4161 o->reloc_count += p->u.indirect.section->reloc_count;
4162 else if (p->type == bfd_section_reloc_link_order
4163 || p->type == bfd_symbol_reloc_link_order)
4168 /* Compute the reloc and symbol file positions. */
4169 ecoff_compute_reloc_file_positions (abfd);
4171 /* Write out the debugging information. */
4172 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4173 &backend->debug_swap, info,
4174 ecoff_data (abfd)->sym_filepos))
4177 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4179 if (info->relocateable)
4181 /* Now reset the reloc_count field of the sections in the output
4182 BFD to 0, so that we can use them to keep track of how many
4183 relocs we have output thus far. */
4184 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4188 /* Get a value for the GP register. */
4189 if (ecoff_data (abfd)->gp == 0)
4191 struct bfd_link_hash_entry *h;
4193 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4194 if (h != (struct bfd_link_hash_entry *) NULL
4195 && h->type == bfd_link_hash_defined)
4196 ecoff_data (abfd)->gp = (h->u.def.value
4197 + h->u.def.section->output_section->vma
4198 + h->u.def.section->output_offset);
4199 else if (info->relocateable)
4203 /* Make up a value. */
4205 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4208 && (strcmp (o->name, _SBSS) == 0
4209 || strcmp (o->name, _SDATA) == 0
4210 || strcmp (o->name, _LIT4) == 0
4211 || strcmp (o->name, _LIT8) == 0
4212 || strcmp (o->name, _LITA) == 0))
4215 ecoff_data (abfd)->gp = lo + 0x8000;
4219 /* If the relocate_section function needs to do a reloc
4220 involving the GP value, it should make a reloc_dangerous
4221 callback to warn that GP is not defined. */
4225 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4227 for (p = o->link_order_head;
4228 p != (struct bfd_link_order *) NULL;
4231 if (p->type == bfd_indirect_link_order
4232 && (bfd_get_flavour (p->u.indirect.section->owner)
4233 == bfd_target_ecoff_flavour))
4235 if (! ecoff_indirect_link_order (abfd, info, o, p))
4238 else if (p->type == bfd_section_reloc_link_order
4239 || p->type == bfd_symbol_reloc_link_order)
4241 if (! ecoff_reloc_link_order (abfd, info, o, p))
4246 if (! _bfd_default_link_order (abfd, info, o, p))
4252 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4254 ecoff_data (abfd)->linker = true;
4259 /* Accumulate the debugging information for an input BFD into the
4260 output BFD. This must read in the symbolic information of the
4264 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4267 struct bfd_link_info *info;
4270 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4271 const struct ecoff_debug_swap * const swap =
4272 &ecoff_backend (input_bfd)->debug_swap;
4273 HDRR *symhdr = &debug->symbolic_header;
4276 #define READ(ptr, offset, count, size, type) \
4277 if (symhdr->count == 0) \
4278 debug->ptr = NULL; \
4281 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
4282 debug->ptr = (type) bfd_malloc (amt); \
4283 if (debug->ptr == NULL) \
4286 goto return_something; \
4288 if (bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
4289 || bfd_bread (debug->ptr, amt, input_bfd) != amt) \
4292 goto return_something; \
4296 /* If raw_syments is not NULL, then the data was already by read by
4297 _bfd_ecoff_slurp_symbolic_info. */
4298 if (ecoff_data (input_bfd)->raw_syments == NULL)
4300 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4302 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4303 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4304 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4305 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4306 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4308 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4309 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4310 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4314 /* We do not read the external strings or the external symbols. */
4316 ret = (bfd_ecoff_debug_accumulate
4317 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4318 &ecoff_backend (output_bfd)->debug_swap,
4319 input_bfd, debug, swap, info));
4322 if (ecoff_data (input_bfd)->raw_syments == NULL)
4324 if (debug->line != NULL)
4326 if (debug->external_dnr != NULL)
4327 free (debug->external_dnr);
4328 if (debug->external_pdr != NULL)
4329 free (debug->external_pdr);
4330 if (debug->external_sym != NULL)
4331 free (debug->external_sym);
4332 if (debug->external_opt != NULL)
4333 free (debug->external_opt);
4334 if (debug->external_aux != NULL)
4335 free (debug->external_aux);
4336 if (debug->ss != NULL)
4338 if (debug->external_fdr != NULL)
4339 free (debug->external_fdr);
4340 if (debug->external_rfd != NULL)
4341 free (debug->external_rfd);
4343 /* Make sure we don't accidentally follow one of these pointers
4344 into freed memory. */
4346 debug->external_dnr = NULL;
4347 debug->external_pdr = NULL;
4348 debug->external_sym = NULL;
4349 debug->external_opt = NULL;
4350 debug->external_aux = NULL;
4352 debug->external_fdr = NULL;
4353 debug->external_rfd = NULL;
4359 /* Put out information for an external symbol. These come only from
4363 ecoff_link_write_external (h, data)
4364 struct ecoff_link_hash_entry *h;
4367 struct extsym_info *einfo = (struct extsym_info *) data;
4368 bfd *output_bfd = einfo->abfd;
4371 if (h->root.type == bfd_link_hash_warning)
4373 h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4374 if (h->root.type == bfd_link_hash_new)
4378 /* We need to check if this symbol is being stripped. */
4379 if (h->root.type == bfd_link_hash_undefined
4380 || h->root.type == bfd_link_hash_undefweak)
4382 else if (einfo->info->strip == strip_all
4383 || (einfo->info->strip == strip_some
4384 && bfd_hash_lookup (einfo->info->keep_hash,
4385 h->root.root.string,
4386 false, false) == NULL))
4391 if (strip || h->written)
4394 if (h->abfd == (bfd *) NULL)
4397 h->esym.cobol_main = 0;
4398 h->esym.weakext = 0;
4399 h->esym.reserved = 0;
4400 h->esym.ifd = ifdNil;
4401 h->esym.asym.value = 0;
4402 h->esym.asym.st = stGlobal;
4404 if (h->root.type != bfd_link_hash_defined
4405 && h->root.type != bfd_link_hash_defweak)
4406 h->esym.asym.sc = scAbs;
4409 asection *output_section;
4412 output_section = h->root.u.def.section->output_section;
4413 name = bfd_section_name (output_section->owner, output_section);
4415 if (strcmp (name, _TEXT) == 0)
4416 h->esym.asym.sc = scText;
4417 else if (strcmp (name, _DATA) == 0)
4418 h->esym.asym.sc = scData;
4419 else if (strcmp (name, _SDATA) == 0)
4420 h->esym.asym.sc = scSData;
4421 else if (strcmp (name, _RDATA) == 0)
4422 h->esym.asym.sc = scRData;
4423 else if (strcmp (name, _BSS) == 0)
4424 h->esym.asym.sc = scBss;
4425 else if (strcmp (name, _SBSS) == 0)
4426 h->esym.asym.sc = scSBss;
4427 else if (strcmp (name, _INIT) == 0)
4428 h->esym.asym.sc = scInit;
4429 else if (strcmp (name, _FINI) == 0)
4430 h->esym.asym.sc = scFini;
4431 else if (strcmp (name, _PDATA) == 0)
4432 h->esym.asym.sc = scPData;
4433 else if (strcmp (name, _XDATA) == 0)
4434 h->esym.asym.sc = scXData;
4435 else if (strcmp (name, _RCONST) == 0)
4436 h->esym.asym.sc = scRConst;
4438 h->esym.asym.sc = scAbs;
4441 h->esym.asym.reserved = 0;
4442 h->esym.asym.index = indexNil;
4444 else if (h->esym.ifd != -1)
4446 struct ecoff_debug_info *debug;
4448 /* Adjust the FDR index for the symbol by that used for the
4450 debug = &ecoff_data (h->abfd)->debug_info;
4451 BFD_ASSERT (h->esym.ifd >= 0
4452 && h->esym.ifd < debug->symbolic_header.ifdMax);
4453 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4456 switch (h->root.type)
4459 case bfd_link_hash_warning:
4460 case bfd_link_hash_new:
4462 case bfd_link_hash_undefined:
4463 case bfd_link_hash_undefweak:
4464 if (h->esym.asym.sc != scUndefined
4465 && h->esym.asym.sc != scSUndefined)
4466 h->esym.asym.sc = scUndefined;
4468 case bfd_link_hash_defined:
4469 case bfd_link_hash_defweak:
4470 if (h->esym.asym.sc == scUndefined
4471 || h->esym.asym.sc == scSUndefined)
4472 h->esym.asym.sc = scAbs;
4473 else if (h->esym.asym.sc == scCommon)
4474 h->esym.asym.sc = scBss;
4475 else if (h->esym.asym.sc == scSCommon)
4476 h->esym.asym.sc = scSBss;
4477 h->esym.asym.value = (h->root.u.def.value
4478 + h->root.u.def.section->output_section->vma
4479 + h->root.u.def.section->output_offset);
4481 case bfd_link_hash_common:
4482 if (h->esym.asym.sc != scCommon
4483 && h->esym.asym.sc != scSCommon)
4484 h->esym.asym.sc = scCommon;
4485 h->esym.asym.value = h->root.u.c.size;
4487 case bfd_link_hash_indirect:
4488 /* We ignore these symbols, since the indirected symbol is
4489 already in the hash table. */
4493 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4495 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4498 return (bfd_ecoff_debug_one_external
4499 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4500 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4504 /* Relocate and write an ECOFF section into an ECOFF output file. */
4507 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4509 struct bfd_link_info *info;
4510 asection *output_section;
4511 struct bfd_link_order *link_order;
4513 asection *input_section;
4515 struct ecoff_section_tdata *section_tdata;
4516 bfd_size_type raw_size;
4517 bfd_size_type cooked_size;
4518 bfd_byte *contents = NULL;
4519 bfd_size_type external_reloc_size;
4520 bfd_size_type external_relocs_size;
4521 PTR external_relocs = NULL;
4524 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4526 if (link_order->size == 0)
4529 input_section = link_order->u.indirect.section;
4530 input_bfd = input_section->owner;
4531 section_tdata = ecoff_section_data (input_bfd, input_section);
4533 raw_size = input_section->_raw_size;
4534 cooked_size = input_section->_cooked_size;
4535 if (cooked_size == 0)
4536 cooked_size = raw_size;
4538 BFD_ASSERT (input_section->output_section == output_section);
4539 BFD_ASSERT (input_section->output_offset == link_order->offset);
4540 BFD_ASSERT (cooked_size == link_order->size);
4542 /* Get the section contents. We allocate memory for the larger of
4543 the size before relocating and the size after relocating. */
4544 amt = raw_size >= cooked_size ? raw_size : cooked_size;
4545 contents = (bfd_byte *) bfd_malloc (amt);
4546 if (contents == NULL && amt != 0)
4549 /* If we are relaxing, the contents may have already been read into
4550 memory, in which case we copy them into our new buffer. We don't
4551 simply reuse the old buffer in case cooked_size > raw_size. */
4552 if (section_tdata != (struct ecoff_section_tdata *) NULL
4553 && section_tdata->contents != (bfd_byte *) NULL)
4554 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4557 if (! bfd_get_section_contents (input_bfd, input_section,
4559 (file_ptr) 0, raw_size))
4563 /* Get the relocs. If we are relaxing MIPS code, they will already
4564 have been read in. Otherwise, we read them in now. */
4565 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4566 external_relocs_size = external_reloc_size * input_section->reloc_count;
4568 if (section_tdata != (struct ecoff_section_tdata *) NULL
4569 && section_tdata->external_relocs != NULL)
4570 external_relocs = section_tdata->external_relocs;
4573 external_relocs = (PTR) bfd_malloc (external_relocs_size);
4574 if (external_relocs == NULL && external_relocs_size != 0)
4577 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4578 || (bfd_bread (external_relocs, external_relocs_size, input_bfd)
4579 != external_relocs_size))
4583 /* Relocate the section contents. */
4584 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4585 (output_bfd, info, input_bfd, input_section, contents,
4589 /* Write out the relocated section. */
4590 if (! bfd_set_section_contents (output_bfd,
4593 (file_ptr) input_section->output_offset,
4597 /* If we are producing relocateable output, the relocs were
4598 modified, and we write them out now. We use the reloc_count
4599 field of output_section to keep track of the number of relocs we
4600 have output so far. */
4601 if (info->relocateable)
4603 file_ptr pos = (output_section->rel_filepos
4604 + output_section->reloc_count * external_reloc_size);
4605 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4606 || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd)
4607 != external_relocs_size))
4609 output_section->reloc_count += input_section->reloc_count;
4612 if (contents != NULL)
4614 if (external_relocs != NULL && section_tdata == NULL)
4615 free (external_relocs);
4619 if (contents != NULL)
4621 if (external_relocs != NULL && section_tdata == NULL)
4622 free (external_relocs);
4626 /* Generate a reloc when linking an ECOFF file. This is a reloc
4627 requested by the linker, and does come from any input file. This
4628 is used to build constructor and destructor tables when linking
4632 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4634 struct bfd_link_info *info;
4635 asection *output_section;
4636 struct bfd_link_order *link_order;
4638 enum bfd_link_order_type type;
4642 struct internal_reloc in;
4643 bfd_size_type external_reloc_size;
4648 type = link_order->type;
4650 addend = link_order->u.reloc.p->addend;
4652 /* We set up an arelent to pass to the backend adjust_reloc_out
4654 rel.address = link_order->offset;
4656 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4659 bfd_set_error (bfd_error_bad_value);
4663 if (type == bfd_section_reloc_link_order)
4665 section = link_order->u.reloc.p->u.section;
4666 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4670 struct bfd_link_hash_entry *h;
4672 /* Treat a reloc against a defined symbol as though it were
4673 actually against the section. */
4674 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4675 link_order->u.reloc.p->u.name,
4676 false, false, false);
4678 && (h->type == bfd_link_hash_defined
4679 || h->type == bfd_link_hash_defweak))
4681 type = bfd_section_reloc_link_order;
4682 section = h->u.def.section->output_section;
4683 /* It seems that we ought to add the symbol value to the
4684 addend here, but in practice it has already been added
4685 because it was passed to constructor_callback. */
4686 addend += section->vma + h->u.def.section->output_offset;
4690 /* We can't set up a reloc against a symbol correctly,
4691 because we have no asymbol structure. Currently no
4692 adjust_reloc_out routine cares. */
4693 rel.sym_ptr_ptr = (asymbol **) NULL;
4697 /* All ECOFF relocs are in-place. Put the addend into the object
4700 BFD_ASSERT (rel.howto->partial_inplace);
4704 bfd_reloc_status_type rstat;
4707 size = bfd_get_reloc_size (rel.howto);
4708 buf = (bfd_byte *) bfd_zmalloc (size);
4709 if (buf == (bfd_byte *) NULL)
4711 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4712 (bfd_vma) addend, buf);
4718 case bfd_reloc_outofrange:
4720 case bfd_reloc_overflow:
4721 if (! ((*info->callbacks->reloc_overflow)
4723 (link_order->type == bfd_section_reloc_link_order
4724 ? bfd_section_name (output_bfd, section)
4725 : link_order->u.reloc.p->u.name),
4726 rel.howto->name, addend, (bfd *) NULL,
4727 (asection *) NULL, (bfd_vma) 0)))
4734 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4735 (file_ptr) link_order->offset, size);
4743 /* Move the information into an internal_reloc structure. */
4744 in.r_vaddr = (rel.address
4745 + bfd_get_section_vma (output_bfd, output_section));
4746 in.r_type = rel.howto->type;
4748 if (type == bfd_symbol_reloc_link_order)
4750 struct ecoff_link_hash_entry *h;
4752 h = ((struct ecoff_link_hash_entry *)
4753 bfd_wrapped_link_hash_lookup (output_bfd, info,
4754 link_order->u.reloc.p->u.name,
4755 false, false, true));
4756 if (h != (struct ecoff_link_hash_entry *) NULL
4758 in.r_symndx = h->indx;
4761 if (! ((*info->callbacks->unattached_reloc)
4762 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4763 (asection *) NULL, (bfd_vma) 0)))
4773 name = bfd_get_section_name (output_bfd, section);
4774 if (strcmp (name, ".text") == 0)
4775 in.r_symndx = RELOC_SECTION_TEXT;
4776 else if (strcmp (name, ".rdata") == 0)
4777 in.r_symndx = RELOC_SECTION_RDATA;
4778 else if (strcmp (name, ".data") == 0)
4779 in.r_symndx = RELOC_SECTION_DATA;
4780 else if (strcmp (name, ".sdata") == 0)
4781 in.r_symndx = RELOC_SECTION_SDATA;
4782 else if (strcmp (name, ".sbss") == 0)
4783 in.r_symndx = RELOC_SECTION_SBSS;
4784 else if (strcmp (name, ".bss") == 0)
4785 in.r_symndx = RELOC_SECTION_BSS;
4786 else if (strcmp (name, ".init") == 0)
4787 in.r_symndx = RELOC_SECTION_INIT;
4788 else if (strcmp (name, ".lit8") == 0)
4789 in.r_symndx = RELOC_SECTION_LIT8;
4790 else if (strcmp (name, ".lit4") == 0)
4791 in.r_symndx = RELOC_SECTION_LIT4;
4792 else if (strcmp (name, ".xdata") == 0)
4793 in.r_symndx = RELOC_SECTION_XDATA;
4794 else if (strcmp (name, ".pdata") == 0)
4795 in.r_symndx = RELOC_SECTION_PDATA;
4796 else if (strcmp (name, ".fini") == 0)
4797 in.r_symndx = RELOC_SECTION_FINI;
4798 else if (strcmp (name, ".lita") == 0)
4799 in.r_symndx = RELOC_SECTION_LITA;
4800 else if (strcmp (name, "*ABS*") == 0)
4801 in.r_symndx = RELOC_SECTION_ABS;
4802 else if (strcmp (name, ".rconst") == 0)
4803 in.r_symndx = RELOC_SECTION_RCONST;
4809 /* Let the BFD backend adjust the reloc. */
4810 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4812 /* Get some memory and swap out the reloc. */
4813 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4814 rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4815 if (rbuf == (bfd_byte *) NULL)
4818 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4820 pos = (output_section->rel_filepos
4821 + output_section->reloc_count * external_reloc_size);
4822 ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4823 && (bfd_bwrite ((PTR) rbuf, external_reloc_size, output_bfd)
4824 == external_reloc_size));
4827 ++output_section->reloc_count;