1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Original version by Per Bothner.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "aout/ranlib.h"
28 #include "aout/stab_gnu.h"
30 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
31 some other stuff which we don't want and which conflicts with stuff
34 #include "aout/aout64.h"
37 #undef obj_sym_filepos
39 #include "coff/internal.h"
41 #include "coff/symconst.h"
42 #include "coff/ecoff.h"
46 /* Prototypes for static functions. */
48 static int ecoff_get_magic PARAMS ((bfd *abfd));
49 static long ecoff_sec_to_styp_flags PARAMS ((const char *name,
51 static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *abfd));
52 static boolean ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
53 asymbol *asym, int ext, int weak));
54 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, FDR *fdr,
56 RNDXR *rndx, long isym,
58 static char *ecoff_type_to_string PARAMS ((bfd *abfd, FDR *fdr,
60 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
62 static int ecoff_sort_hdrs PARAMS ((const PTR, const PTR));
63 static boolean ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
64 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
65 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
66 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
67 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
72 /* This stuff is somewhat copied from coffcode.h. */
74 static asection bfd_debug_section = { "*DEBUG*" };
76 /* Create an ECOFF object. */
79 _bfd_ecoff_mkobject (abfd)
82 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
83 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
84 if (abfd->tdata.ecoff_obj_data == NULL)
90 /* This is a hook called by coff_real_object_p to create any backend
91 specific information. */
94 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
99 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
100 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
101 ecoff_data_type *ecoff;
103 if (_bfd_ecoff_mkobject (abfd) == false)
106 ecoff = ecoff_data (abfd);
108 ecoff->sym_filepos = internal_f->f_symptr;
110 if (internal_a != (struct internal_aouthdr *) NULL)
114 ecoff->text_start = internal_a->text_start;
115 ecoff->text_end = internal_a->text_start + internal_a->tsize;
116 ecoff->gp = internal_a->gp_value;
117 ecoff->gprmask = internal_a->gprmask;
118 for (i = 0; i < 4; i++)
119 ecoff->cprmask[i] = internal_a->cprmask[i];
120 ecoff->fprmask = internal_a->fprmask;
121 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
122 abfd->flags |= D_PAGED;
124 abfd->flags &=~ D_PAGED;
127 /* It turns out that no special action is required by the MIPS or
128 Alpha ECOFF backends. They have different information in the
129 a.out header, but we just copy it all (e.g., gprmask, cprmask and
130 fprmask) and let the swapping routines ensure that only relevant
131 information is written out. */
136 /* Initialize a new section. */
139 _bfd_ecoff_new_section_hook (abfd, section)
143 section->alignment_power = 4;
145 if (strcmp (section->name, _TEXT) == 0
146 || strcmp (section->name, _INIT) == 0
147 || strcmp (section->name, _FINI) == 0)
148 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
149 else if (strcmp (section->name, _DATA) == 0
150 || strcmp (section->name, _SDATA) == 0)
151 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
152 else if (strcmp (section->name, _RDATA) == 0
153 || strcmp (section->name, _LIT8) == 0
154 || strcmp (section->name, _LIT4) == 0
155 || strcmp (section->name, _RCONST) == 0
156 || strcmp (section->name, _PDATA) == 0)
157 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
158 else if (strcmp (section->name, _BSS) == 0
159 || strcmp (section->name, _SBSS) == 0)
160 section->flags |= SEC_ALLOC;
161 else if (strcmp (section->name, _LIB) == 0)
163 /* An Irix 4 shared libary. */
164 section->flags |= SEC_COFF_SHARED_LIBRARY;
167 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
168 uncertain about .init on some systems and I don't know how shared
174 /* Determine the machine architecture and type. This is called from
175 the generic COFF routines. It is the inverse of ecoff_get_magic,
176 below. This could be an ECOFF backend routine, with one version
177 for each target, but there aren't all that many ECOFF targets. */
180 _bfd_ecoff_set_arch_mach_hook (abfd, filehdr)
184 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
185 enum bfd_architecture arch;
188 switch (internal_f->f_magic)
191 case MIPS_MAGIC_LITTLE:
193 arch = bfd_arch_mips;
197 case MIPS_MAGIC_LITTLE2:
198 case MIPS_MAGIC_BIG2:
199 /* MIPS ISA level 2: the r6000 */
200 arch = bfd_arch_mips;
204 case MIPS_MAGIC_LITTLE3:
205 case MIPS_MAGIC_BIG3:
206 /* MIPS ISA level 3: the r4000 */
207 arch = bfd_arch_mips;
212 arch = bfd_arch_alpha;
217 arch = bfd_arch_obscure;
222 return bfd_default_set_arch_mach (abfd, arch, mach);
225 /* Get the magic number to use based on the architecture and machine.
226 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
229 ecoff_get_magic (abfd)
234 switch (bfd_get_arch (abfd))
237 switch (bfd_get_mach (abfd))
242 big = MIPS_MAGIC_BIG;
243 little = MIPS_MAGIC_LITTLE;
247 big = MIPS_MAGIC_BIG2;
248 little = MIPS_MAGIC_LITTLE2;
252 big = MIPS_MAGIC_BIG3;
253 little = MIPS_MAGIC_LITTLE3;
257 return bfd_big_endian (abfd) ? big : little;
268 /* Get the section s_flags to use for a section. */
271 ecoff_sec_to_styp_flags (name, flags)
279 if (strcmp (name, _TEXT) == 0)
281 else if (strcmp (name, _DATA) == 0)
283 else if (strcmp (name, _SDATA) == 0)
285 else if (strcmp (name, _RDATA) == 0)
287 else if (strcmp (name, _LITA) == 0)
289 else if (strcmp (name, _LIT8) == 0)
291 else if (strcmp (name, _LIT4) == 0)
293 else if (strcmp (name, _BSS) == 0)
295 else if (strcmp (name, _SBSS) == 0)
297 else if (strcmp (name, _INIT) == 0)
298 styp = STYP_ECOFF_INIT;
299 else if (strcmp (name, _FINI) == 0)
300 styp = STYP_ECOFF_FINI;
301 else if (strcmp (name, _PDATA) == 0)
303 else if (strcmp (name, _XDATA) == 0)
305 else if (strcmp (name, _LIB) == 0)
306 styp = STYP_ECOFF_LIB;
307 else if (strcmp (name, _GOT) == 0)
309 else if (strcmp (name, _HASH) == 0)
311 else if (strcmp (name, _DYNAMIC) == 0)
313 else if (strcmp (name, _LIBLIST) == 0)
315 else if (strcmp (name, _RELDYN) == 0)
317 else if (strcmp (name, _CONFLIC) == 0)
319 else if (strcmp (name, _DYNSTR) == 0)
321 else if (strcmp (name, _DYNSYM) == 0)
323 else if (strcmp (name, _COMMENT) == 0)
326 flags &=~ SEC_NEVER_LOAD;
328 else if (strcmp (name, _RCONST) == 0)
330 else if (flags & SEC_CODE)
332 else if (flags & SEC_DATA)
334 else if (flags & SEC_READONLY)
336 else if (flags & SEC_LOAD)
341 if (flags & SEC_NEVER_LOAD)
347 /* Get the BFD flags to use for a section. */
351 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name)
356 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
357 long styp_flags = internal_s->s_flags;
358 flagword sec_flags=0;
360 if (styp_flags & STYP_NOLOAD)
361 sec_flags |= SEC_NEVER_LOAD;
363 /* For 386 COFF, at least, an unloadable text or data section is
364 actually a shared library section. */
365 if ((styp_flags & STYP_TEXT)
366 || (styp_flags & STYP_ECOFF_INIT)
367 || (styp_flags & STYP_ECOFF_FINI)
368 || (styp_flags & STYP_DYNAMIC)
369 || (styp_flags & STYP_LIBLIST)
370 || (styp_flags & STYP_RELDYN)
371 || styp_flags == STYP_CONFLIC
372 || (styp_flags & STYP_DYNSTR)
373 || (styp_flags & STYP_DYNSYM)
374 || (styp_flags & STYP_HASH))
376 if (sec_flags & SEC_NEVER_LOAD)
377 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
379 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
381 else if ((styp_flags & STYP_DATA)
382 || (styp_flags & STYP_RDATA)
383 || (styp_flags & STYP_SDATA)
384 || styp_flags == STYP_PDATA
385 || styp_flags == STYP_XDATA
386 || (styp_flags & STYP_GOT)
387 || styp_flags == STYP_RCONST)
389 if (sec_flags & SEC_NEVER_LOAD)
390 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
392 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
393 if ((styp_flags & STYP_RDATA)
394 || styp_flags == STYP_PDATA
395 || styp_flags == STYP_RCONST)
396 sec_flags |= SEC_READONLY;
398 else if ((styp_flags & STYP_BSS)
399 || (styp_flags & STYP_SBSS))
401 sec_flags |= SEC_ALLOC;
403 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
405 sec_flags |= SEC_NEVER_LOAD;
407 else if ((styp_flags & STYP_LITA)
408 || (styp_flags & STYP_LIT8)
409 || (styp_flags & STYP_LIT4))
411 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
413 else if (styp_flags & STYP_ECOFF_LIB)
415 sec_flags |= SEC_COFF_SHARED_LIBRARY;
419 sec_flags |= SEC_ALLOC | SEC_LOAD;
425 /* Read in the symbolic header for an ECOFF object file. */
428 ecoff_slurp_symbolic_header (abfd)
431 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
432 bfd_size_type external_hdr_size;
434 HDRR *internal_symhdr;
436 /* See if we've already read it in. */
437 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
438 backend->debug_swap.sym_magic)
441 /* See whether there is a symbolic header. */
442 if (ecoff_data (abfd)->sym_filepos == 0)
444 bfd_get_symcount (abfd) = 0;
448 /* At this point bfd_get_symcount (abfd) holds the number of symbols
449 as read from the file header, but on ECOFF this is always the
450 size of the symbolic information header. It would be cleaner to
451 handle this when we first read the file in coffgen.c. */
452 external_hdr_size = backend->debug_swap.external_hdr_size;
453 if (bfd_get_symcount (abfd) != external_hdr_size)
455 bfd_set_error (bfd_error_bad_value);
459 /* Read the symbolic information header. */
460 raw = (PTR) bfd_malloc ((size_t) external_hdr_size);
464 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
465 || (bfd_read (raw, external_hdr_size, 1, abfd)
466 != external_hdr_size))
468 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
469 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
471 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
473 bfd_set_error (bfd_error_bad_value);
477 /* Now we can get the correct number of symbols. */
478 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
479 + internal_symhdr->iextMax);
490 /* Read in and swap the important symbolic information for an ECOFF
491 object file. This is called by gdb via the read_debug_info entry
492 point in the backend structure. */
496 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
499 struct ecoff_debug_info *debug;
501 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
502 HDRR *internal_symhdr;
503 bfd_size_type raw_base;
504 bfd_size_type raw_size;
506 bfd_size_type external_fdr_size;
510 bfd_size_type raw_end;
511 bfd_size_type cb_end;
513 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
515 /* Check whether we've already gotten it, and whether there's any to
517 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
519 if (ecoff_data (abfd)->sym_filepos == 0)
521 bfd_get_symcount (abfd) = 0;
525 if (! ecoff_slurp_symbolic_header (abfd))
528 internal_symhdr = &debug->symbolic_header;
530 /* Read all the symbolic information at once. */
531 raw_base = (ecoff_data (abfd)->sym_filepos
532 + backend->debug_swap.external_hdr_size);
534 /* Alpha ecoff makes the determination of raw_size difficult. It has
535 an undocumented debug data section between the symhdr and the first
536 documented section. And the ordering of the sections varies between
537 statically and dynamically linked executables.
538 If bfd supports SEEK_END someday, this code could be simplified. */
542 #define UPDATE_RAW_END(start, count, size) \
543 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
544 if (cb_end > raw_end) \
547 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
548 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
549 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
550 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
551 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
552 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
553 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
554 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
555 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
556 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
557 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
559 #undef UPDATE_RAW_END
561 raw_size = raw_end - raw_base;
564 ecoff_data (abfd)->sym_filepos = 0;
567 raw = (PTR) bfd_alloc (abfd, raw_size);
571 (ecoff_data (abfd)->sym_filepos
572 + backend->debug_swap.external_hdr_size),
574 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
576 bfd_release (abfd, raw);
580 ecoff_data (abfd)->raw_syments = raw;
582 /* Get pointers for the numeric offsets in the HDRR structure. */
583 #define FIX(off1, off2, type) \
584 if (internal_symhdr->off1 == 0) \
585 debug->off2 = (type) NULL; \
587 debug->off2 = (type) ((char *) raw \
588 + (internal_symhdr->off1 \
590 FIX (cbLineOffset, line, unsigned char *);
591 FIX (cbDnOffset, external_dnr, PTR);
592 FIX (cbPdOffset, external_pdr, PTR);
593 FIX (cbSymOffset, external_sym, PTR);
594 FIX (cbOptOffset, external_opt, PTR);
595 FIX (cbAuxOffset, external_aux, union aux_ext *);
596 FIX (cbSsOffset, ss, char *);
597 FIX (cbSsExtOffset, ssext, char *);
598 FIX (cbFdOffset, external_fdr, PTR);
599 FIX (cbRfdOffset, external_rfd, PTR);
600 FIX (cbExtOffset, external_ext, PTR);
603 /* I don't want to always swap all the data, because it will just
604 waste time and most programs will never look at it. The only
605 time the linker needs most of the debugging information swapped
606 is when linking big-endian and little-endian MIPS object files
607 together, which is not a common occurrence.
609 We need to look at the fdr to deal with a lot of information in
610 the symbols, so we swap them here. */
611 debug->fdr = (struct fdr *) bfd_alloc (abfd,
612 (internal_symhdr->ifdMax *
613 sizeof (struct fdr)));
614 if (debug->fdr == NULL)
616 external_fdr_size = backend->debug_swap.external_fdr_size;
617 fdr_ptr = debug->fdr;
618 fraw_src = (char *) debug->external_fdr;
619 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
620 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
621 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
626 /* ECOFF symbol table routines. The ECOFF symbol table is described
627 in gcc/mips-tfile.c. */
629 /* ECOFF uses two common sections. One is the usual one, and the
630 other is for small objects. All the small objects are kept
631 together, and then referenced via the gp pointer, which yields
632 faster assembler code. This is what we use for the small common
634 static asection ecoff_scom_section;
635 static asymbol ecoff_scom_symbol;
636 static asymbol *ecoff_scom_symbol_ptr;
638 /* Create an empty symbol. */
641 _bfd_ecoff_make_empty_symbol (abfd)
644 ecoff_symbol_type *new;
646 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
647 if (new == (ecoff_symbol_type *) NULL)
648 return (asymbol *) NULL;
649 memset ((PTR) new, 0, sizeof *new);
650 new->symbol.section = (asection *) NULL;
651 new->fdr = (FDR *) NULL;
654 new->symbol.the_bfd = abfd;
658 /* Set the BFD flags and section for an ECOFF symbol. */
661 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, weak)
668 asym->the_bfd = abfd;
669 asym->value = ecoff_sym->value;
670 asym->section = &bfd_debug_section;
673 /* Most symbol types are just for debugging. */
674 switch (ecoff_sym->st)
683 if (ECOFF_IS_STAB (ecoff_sym))
685 asym->flags = BSF_DEBUGGING;
690 asym->flags = BSF_DEBUGGING;
695 asym->flags = BSF_EXPORT | BSF_WEAK;
697 asym->flags = BSF_EXPORT | BSF_GLOBAL;
700 asym->flags = BSF_LOCAL;
701 /* Normally, a local stProc symbol will have a corresponding
702 external symbol. We mark the local symbol as a debugging
703 symbol, in order to prevent nm from printing both out.
704 Similarly, we mark stLabel and stabs symbols as debugging
705 symbols. In both cases, we do want to set the value
706 correctly based on the symbol class. */
707 if (ecoff_sym->st == stProc
708 || ecoff_sym->st == stLabel
709 || ECOFF_IS_STAB (ecoff_sym))
710 asym->flags |= BSF_DEBUGGING;
712 switch (ecoff_sym->sc)
715 /* Used for compiler generated labels. Leave them in the
716 debugging section, and mark them as local. If BSF_DEBUGGING
717 is set, then nm does not display them for some reason. If no
718 flags are set then the linker whines about them. */
719 asym->flags = BSF_LOCAL;
722 asym->section = bfd_make_section_old_way (abfd, ".text");
723 asym->value -= asym->section->vma;
726 asym->section = bfd_make_section_old_way (abfd, ".data");
727 asym->value -= asym->section->vma;
730 asym->section = bfd_make_section_old_way (abfd, ".bss");
731 asym->value -= asym->section->vma;
734 asym->flags = BSF_DEBUGGING;
737 asym->section = bfd_abs_section_ptr;
740 asym->section = bfd_und_section_ptr;
750 asym->flags = BSF_DEBUGGING;
753 asym->section = bfd_make_section_old_way (abfd, ".sdata");
754 asym->value -= asym->section->vma;
757 asym->section = bfd_make_section_old_way (abfd, ".sbss");
758 asym->value -= asym->section->vma;
761 asym->section = bfd_make_section_old_way (abfd, ".rdata");
762 asym->value -= asym->section->vma;
765 asym->flags = BSF_DEBUGGING;
768 if (asym->value > ecoff_data (abfd)->gp_size)
770 asym->section = bfd_com_section_ptr;
776 if (ecoff_scom_section.name == NULL)
778 /* Initialize the small common section. */
779 ecoff_scom_section.name = SCOMMON;
780 ecoff_scom_section.flags = SEC_IS_COMMON;
781 ecoff_scom_section.output_section = &ecoff_scom_section;
782 ecoff_scom_section.symbol = &ecoff_scom_symbol;
783 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
784 ecoff_scom_symbol.name = SCOMMON;
785 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
786 ecoff_scom_symbol.section = &ecoff_scom_section;
787 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
789 asym->section = &ecoff_scom_section;
794 asym->flags = BSF_DEBUGGING;
797 asym->section = bfd_und_section_ptr;
802 asym->section = bfd_make_section_old_way (abfd, ".init");
803 asym->value -= asym->section->vma;
808 asym->flags = BSF_DEBUGGING;
811 asym->section = bfd_make_section_old_way (abfd, ".fini");
812 asym->value -= asym->section->vma;
815 asym->section = bfd_make_section_old_way (abfd, ".rconst");
816 asym->value -= asym->section->vma;
822 /* Look for special constructors symbols and make relocation entries
823 in a special construction section. These are produced by the
824 -fgnu-linker argument to g++. */
825 if (ECOFF_IS_STAB (ecoff_sym))
827 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
837 /* This code is no longer needed. It used to be used to
838 make the linker handle set symbols, but they are now
839 handled in the add_symbols routine instead. */
843 arelent_chain *reloc_chain;
844 unsigned int bitsize;
846 /* Get a section with the same name as the symbol (usually
847 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
848 name ___CTOR_LIST (three underscores). We need
849 __CTOR_LIST (two underscores), since ECOFF doesn't use
850 a leading underscore. This should be handled by gcc,
851 but instead we do it here. Actually, this should all
852 be done differently anyhow. */
853 name = bfd_asymbol_name (asym);
854 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
859 section = bfd_get_section_by_name (abfd, name);
860 if (section == (asection *) NULL)
864 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
868 section = bfd_make_section (abfd, copy);
871 /* Build a reloc pointing to this constructor. */
873 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
876 reloc_chain->relent.sym_ptr_ptr =
877 bfd_get_section (asym)->symbol_ptr_ptr;
878 reloc_chain->relent.address = section->_raw_size;
879 reloc_chain->relent.addend = asym->value;
880 reloc_chain->relent.howto =
881 ecoff_backend (abfd)->constructor_reloc;
883 /* Set up the constructor section to hold the reloc. */
884 section->flags = SEC_CONSTRUCTOR;
885 ++section->reloc_count;
887 /* Constructor sections must be rounded to a boundary
888 based on the bitsize. These are not real sections--
889 they are handled specially by the linker--so the ECOFF
890 16 byte alignment restriction does not apply. */
891 bitsize = ecoff_backend (abfd)->constructor_bitsize;
892 section->alignment_power = 1;
893 while ((1 << section->alignment_power) < bitsize / 8)
894 ++section->alignment_power;
896 reloc_chain->next = section->constructor_chain;
897 section->constructor_chain = reloc_chain;
898 section->_raw_size += bitsize / 8;
902 /* Mark the symbol as a constructor. */
903 asym->flags |= BSF_CONSTRUCTOR;
911 /* Read an ECOFF symbol table. */
914 _bfd_ecoff_slurp_symbol_table (abfd)
917 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
918 const bfd_size_type external_ext_size
919 = backend->debug_swap.external_ext_size;
920 const bfd_size_type external_sym_size
921 = backend->debug_swap.external_sym_size;
922 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
923 = backend->debug_swap.swap_ext_in;
924 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
925 = backend->debug_swap.swap_sym_in;
926 bfd_size_type internal_size;
927 ecoff_symbol_type *internal;
928 ecoff_symbol_type *internal_ptr;
934 /* If we've already read in the symbol table, do nothing. */
935 if (ecoff_data (abfd)->canonical_symbols != NULL)
938 /* Get the symbolic information. */
939 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
940 &ecoff_data (abfd)->debug_info))
942 if (bfd_get_symcount (abfd) == 0)
945 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
946 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
947 if (internal == NULL)
950 internal_ptr = internal;
951 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
953 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
954 * external_ext_size));
955 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
959 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
960 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
961 + internal_esym.asym.iss);
962 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
963 &internal_ptr->symbol, 1,
964 internal_esym.weakext))
966 /* The alpha uses a negative ifd field for section symbols. */
967 if (internal_esym.ifd >= 0)
968 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
969 + internal_esym.ifd);
971 internal_ptr->fdr = NULL;
972 internal_ptr->local = false;
973 internal_ptr->native = (PTR) eraw_src;
976 /* The local symbols must be accessed via the fdr's, because the
977 string and aux indices are relative to the fdr information. */
978 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
979 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
980 for (; fdr_ptr < fdr_end; fdr_ptr++)
985 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
986 + fdr_ptr->isymBase * external_sym_size);
987 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
990 lraw_src += external_sym_size, internal_ptr++)
994 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
995 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
998 if (!ecoff_set_symbol_info (abfd, &internal_sym,
999 &internal_ptr->symbol, 0, 0))
1001 internal_ptr->fdr = fdr_ptr;
1002 internal_ptr->local = true;
1003 internal_ptr->native = (PTR) lraw_src;
1007 ecoff_data (abfd)->canonical_symbols = internal;
1012 /* Return the amount of space needed for the canonical symbols. */
1015 _bfd_ecoff_get_symtab_upper_bound (abfd)
1018 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
1019 &ecoff_data (abfd)->debug_info))
1022 if (bfd_get_symcount (abfd) == 0)
1025 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1028 /* Get the canonical symbols. */
1031 _bfd_ecoff_get_symtab (abfd, alocation)
1033 asymbol **alocation;
1035 unsigned int counter = 0;
1036 ecoff_symbol_type *symbase;
1037 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1039 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1041 if (bfd_get_symcount (abfd) == 0)
1044 symbase = ecoff_data (abfd)->canonical_symbols;
1045 while (counter < bfd_get_symcount (abfd))
1047 *(location++) = symbase++;
1050 *location++ = (ecoff_symbol_type *) NULL;
1051 return bfd_get_symcount (abfd);
1054 /* Turn ECOFF type information into a printable string.
1055 ecoff_emit_aggregate and ecoff_type_to_string are from
1056 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1058 /* Write aggregate information to a string. */
1061 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1069 const struct ecoff_debug_swap * const debug_swap =
1070 &ecoff_backend (abfd)->debug_swap;
1071 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1072 unsigned int ifd = rndx->rfd;
1073 unsigned int indx = rndx->index;
1079 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1080 struct return type of a procedure compiled without -g. */
1081 if (ifd == 0xffffffff
1082 || (rndx->rfd == 0xfff && indx == 0))
1083 name = "<undefined>";
1084 else if (indx == indexNil)
1090 if (debug_info->external_rfd == NULL)
1091 fdr = debug_info->fdr + ifd;
1096 (*debug_swap->swap_rfd_in) (abfd,
1097 ((char *) debug_info->external_rfd
1098 + ((fdr->rfdBase + ifd)
1099 * debug_swap->external_rfd_size)),
1101 fdr = debug_info->fdr + rfd;
1104 indx += fdr->isymBase;
1106 (*debug_swap->swap_sym_in) (abfd,
1107 ((char *) debug_info->external_sym
1108 + indx * debug_swap->external_sym_size),
1111 name = debug_info->ss + fdr->issBase + sym.iss;
1115 "%s %s { ifd = %u, index = %lu }",
1118 + debug_info->symbolic_header.iextMax));
1121 /* Convert the type information to string format. */
1124 ecoff_type_to_string (abfd, fdr, indx)
1129 union aux_ext *aux_ptr;
1138 unsigned int basic_type;
1141 static char buffer2[1024];
1146 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1147 bigendian = fdr->fBigendian;
1149 for (i = 0; i < 7; i++)
1151 qualifiers[i].low_bound = 0;
1152 qualifiers[i].high_bound = 0;
1153 qualifiers[i].stride = 0;
1156 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1157 return "-1 (no type)";
1158 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1160 basic_type = u.ti.bt;
1161 qualifiers[0].type = u.ti.tq0;
1162 qualifiers[1].type = u.ti.tq1;
1163 qualifiers[2].type = u.ti.tq2;
1164 qualifiers[3].type = u.ti.tq3;
1165 qualifiers[4].type = u.ti.tq4;
1166 qualifiers[5].type = u.ti.tq5;
1167 qualifiers[6].type = tqNil;
1170 * Go get the basic type.
1174 case btNil: /* undefined */
1178 case btAdr: /* address - integer same size as pointer */
1179 strcpy (p1, "address");
1182 case btChar: /* character */
1183 strcpy (p1, "char");
1186 case btUChar: /* unsigned character */
1187 strcpy (p1, "unsigned char");
1190 case btShort: /* short */
1191 strcpy (p1, "short");
1194 case btUShort: /* unsigned short */
1195 strcpy (p1, "unsigned short");
1198 case btInt: /* int */
1202 case btUInt: /* unsigned int */
1203 strcpy (p1, "unsigned int");
1206 case btLong: /* long */
1207 strcpy (p1, "long");
1210 case btULong: /* unsigned long */
1211 strcpy (p1, "unsigned long");
1214 case btFloat: /* float (real) */
1215 strcpy (p1, "float");
1218 case btDouble: /* Double (real) */
1219 strcpy (p1, "double");
1222 /* Structures add 1-2 aux words:
1223 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1224 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1226 case btStruct: /* Structure (Record) */
1227 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1228 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1229 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1231 indx++; /* skip aux words */
1234 /* Unions add 1-2 aux words:
1235 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1236 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1238 case btUnion: /* Union */
1239 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1240 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1241 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1243 indx++; /* skip aux words */
1246 /* Enumerations add 1-2 aux words:
1247 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1248 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1250 case btEnum: /* Enumeration */
1251 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1252 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1253 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1255 indx++; /* skip aux words */
1258 case btTypedef: /* defined via a typedef, isymRef points */
1259 strcpy (p1, "typedef");
1262 case btRange: /* subrange of int */
1263 strcpy (p1, "subrange");
1266 case btSet: /* pascal sets */
1270 case btComplex: /* fortran complex */
1271 strcpy (p1, "complex");
1274 case btDComplex: /* fortran double complex */
1275 strcpy (p1, "double complex");
1278 case btIndirect: /* forward or unnamed typedef */
1279 strcpy (p1, "forward/unamed typedef");
1282 case btFixedDec: /* Fixed Decimal */
1283 strcpy (p1, "fixed decimal");
1286 case btFloatDec: /* Float Decimal */
1287 strcpy (p1, "float decimal");
1290 case btString: /* Varying Length Character String */
1291 strcpy (p1, "string");
1294 case btBit: /* Aligned Bit String */
1298 case btPicture: /* Picture */
1299 strcpy (p1, "picture");
1302 case btVoid: /* Void */
1303 strcpy (p1, "void");
1307 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1311 p1 += strlen (buffer1);
1314 * If this is a bitfield, get the bitsize.
1320 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1321 sprintf (p1, " : %d", bitsize);
1322 p1 += strlen (buffer1);
1327 * Deal with any qualifiers.
1329 if (qualifiers[0].type != tqNil)
1332 * Snarf up any array bounds in the correct order. Arrays
1333 * store 5 successive words in the aux. table:
1334 * word 0 RNDXR to type of the bounds (ie, int)
1335 * word 1 Current file descriptor index
1337 * word 3 high bound (or -1 if [])
1338 * word 4 stride size in bits
1340 for (i = 0; i < 7; i++)
1342 if (qualifiers[i].type == tqArray)
1344 qualifiers[i].low_bound =
1345 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1346 qualifiers[i].high_bound =
1347 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1348 qualifiers[i].stride =
1349 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1355 * Now print out the qualifiers.
1357 for (i = 0; i < 6; i++)
1359 switch (qualifiers[i].type)
1366 strcpy (p2, "ptr to ");
1367 p2 += sizeof ("ptr to ")-1;
1371 strcpy (p2, "volatile ");
1372 p2 += sizeof ("volatile ")-1;
1376 strcpy (p2, "far ");
1377 p2 += sizeof ("far ")-1;
1381 strcpy (p2, "func. ret. ");
1382 p2 += sizeof ("func. ret. ");
1387 int first_array = i;
1390 /* Print array bounds reversed (ie, in the order the C
1391 programmer writes them). C is such a fun language.... */
1393 while (i < 5 && qualifiers[i+1].type == tqArray)
1396 for (j = i; j >= first_array; j--)
1398 strcpy (p2, "array [");
1399 p2 += sizeof ("array [")-1;
1400 if (qualifiers[j].low_bound != 0)
1402 "%ld:%ld {%ld bits}",
1403 (long) qualifiers[j].low_bound,
1404 (long) qualifiers[j].high_bound,
1405 (long) qualifiers[j].stride);
1407 else if (qualifiers[j].high_bound != -1)
1410 (long) (qualifiers[j].high_bound + 1),
1411 (long) (qualifiers[j].stride));
1414 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1417 strcpy (p2, "] of ");
1418 p2 += sizeof ("] of ")-1;
1426 strcpy (p2, buffer1);
1430 /* Return information about ECOFF symbol SYMBOL in RET. */
1434 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1435 bfd *abfd; /* Ignored. */
1439 bfd_symbol_info (symbol, ret);
1442 /* Return whether this is a local label. */
1446 _bfd_ecoff_bfd_is_local_label (abfd, symbol)
1450 return symbol->name[0] == '$';
1453 /* Print information about an ECOFF symbol. */
1456 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1460 bfd_print_symbol_type how;
1462 const struct ecoff_debug_swap * const debug_swap
1463 = &ecoff_backend (abfd)->debug_swap;
1464 FILE *file = (FILE *)filep;
1468 case bfd_print_symbol_name:
1469 fprintf (file, "%s", symbol->name);
1471 case bfd_print_symbol_more:
1472 if (ecoffsymbol (symbol)->local)
1476 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1478 fprintf (file, "ecoff local ");
1479 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1480 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1481 (unsigned) ecoff_sym.sc);
1487 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1489 fprintf (file, "ecoff extern ");
1490 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1491 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1492 (unsigned) ecoff_ext.asym.sc);
1495 case bfd_print_symbol_all:
1496 /* Print out the symbols in a reasonable way */
1505 if (ecoffsymbol (symbol)->local)
1507 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1510 pos = ((((char *) ecoffsymbol (symbol)->native
1511 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1512 / debug_swap->external_sym_size)
1513 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1520 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1523 pos = (((char *) ecoffsymbol (symbol)->native
1524 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1525 / debug_swap->external_ext_size);
1526 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1527 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1528 weakext = ecoff_ext.weakext ? 'w' : ' ';
1531 fprintf (file, "[%3d] %c ",
1533 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1534 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1535 (unsigned) ecoff_ext.asym.st,
1536 (unsigned) ecoff_ext.asym.sc,
1537 (unsigned) ecoff_ext.asym.index,
1538 jmptbl, cobol_main, weakext,
1541 if (ecoffsymbol (symbol)->fdr != NULL
1542 && ecoff_ext.asym.index != indexNil)
1547 bfd_size_type sym_base;
1548 union aux_ext *aux_base;
1550 fdr = ecoffsymbol (symbol)->fdr;
1551 indx = ecoff_ext.asym.index;
1553 /* sym_base is used to map the fdr relative indices which
1554 appear in the file to the position number which we are
1556 sym_base = fdr->isymBase;
1557 if (ecoffsymbol (symbol)->local)
1559 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1561 /* aux_base is the start of the aux entries for this file;
1562 asym.index is an offset from this. */
1563 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1566 /* The aux entries are stored in host byte order; the
1567 order is indicated by a bit in the fdr. */
1568 bigendian = fdr->fBigendian;
1570 /* This switch is basically from gcc/mips-tdump.c */
1571 switch (ecoff_ext.asym.st)
1579 fprintf (file, "\n End+1 symbol: %ld",
1580 (long) (indx + sym_base));
1584 if (ecoff_ext.asym.sc == scText
1585 || ecoff_ext.asym.sc == scInfo)
1586 fprintf (file, "\n First symbol: %ld",
1587 (long) (indx + sym_base));
1589 fprintf (file, "\n First symbol: %ld",
1591 (AUX_GET_ISYM (bigendian,
1592 &aux_base[ecoff_ext.asym.index])
1598 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1600 else if (ecoffsymbol (symbol)->local)
1601 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1603 (AUX_GET_ISYM (bigendian,
1604 &aux_base[ecoff_ext.asym.index])
1606 ecoff_type_to_string (abfd, fdr, indx + 1));
1608 fprintf (file, "\n Local symbol: %ld",
1611 + (ecoff_data (abfd)
1612 ->debug_info.symbolic_header.iextMax)));
1616 fprintf (file, "\n struct; End+1 symbol: %ld",
1617 (long) (indx + sym_base));
1621 fprintf (file, "\n union; End+1 symbol: %ld",
1622 (long) (indx + sym_base));
1626 fprintf (file, "\n enum; End+1 symbol: %ld",
1627 (long) (indx + sym_base));
1631 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1632 fprintf (file, "\n Type: %s",
1633 ecoff_type_to_string (abfd, fdr, indx));
1642 /* Read in the relocs for a section. */
1645 ecoff_slurp_reloc_table (abfd, section, symbols)
1650 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1651 arelent *internal_relocs;
1652 bfd_size_type external_reloc_size;
1653 bfd_size_type external_relocs_size;
1654 char *external_relocs;
1658 if (section->relocation != (arelent *) NULL
1659 || section->reloc_count == 0
1660 || (section->flags & SEC_CONSTRUCTOR) != 0)
1663 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1666 internal_relocs = (arelent *) bfd_alloc (abfd,
1668 * section->reloc_count));
1669 external_reloc_size = backend->external_reloc_size;
1670 external_relocs_size = external_reloc_size * section->reloc_count;
1671 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
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_read (external_relocs, 1, external_relocs_size, abfd)
1678 != external_relocs_size)
1681 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1683 struct internal_reloc intern;
1685 (*backend->swap_reloc_in) (abfd,
1686 external_relocs + i * external_reloc_size,
1689 if (intern.r_extern)
1691 /* r_symndx is an index into the external symbols. */
1692 BFD_ASSERT (intern.r_symndx >= 0
1694 < (ecoff_data (abfd)
1695 ->debug_info.symbolic_header.iextMax)));
1696 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1699 else if (intern.r_symndx == RELOC_SECTION_NONE
1700 || intern.r_symndx == RELOC_SECTION_ABS)
1702 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1707 CONST char *sec_name;
1710 /* r_symndx is a section key. */
1711 switch (intern.r_symndx)
1713 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1714 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1715 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1716 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1717 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1718 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1719 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1720 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1721 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1722 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1723 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1724 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1725 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1726 case RELOC_SECTION_RCONST: sec_name = ".rconst"; break;
1730 sec = bfd_get_section_by_name (abfd, sec_name);
1731 if (sec == (asection *) NULL)
1733 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1735 rptr->addend = - bfd_get_section_vma (abfd, sec);
1738 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1740 /* Let the backend select the howto field and do any other
1741 required processing. */
1742 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1745 bfd_release (abfd, external_relocs);
1747 section->relocation = internal_relocs;
1752 /* Get a canonical list of relocs. */
1755 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1763 if (section->flags & SEC_CONSTRUCTOR)
1765 arelent_chain *chain;
1767 /* This section has relocs made up by us, not the file, so take
1768 them out of their chain and place them into the data area
1770 for (count = 0, chain = section->constructor_chain;
1771 count < section->reloc_count;
1772 count++, chain = chain->next)
1773 *relptr++ = &chain->relent;
1779 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1782 tblptr = section->relocation;
1784 for (count = 0; count < section->reloc_count; count++)
1785 *relptr++ = tblptr++;
1788 *relptr = (arelent *) NULL;
1790 return section->reloc_count;
1793 /* Provided a BFD, a section and an offset into the section, calculate
1794 and return the name of the source file and the line nearest to the
1799 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1800 filename_ptr, functionname_ptr, retline_ptr)
1803 asymbol **ignore_symbols;
1805 CONST char **filename_ptr;
1806 CONST char **functionname_ptr;
1807 unsigned int *retline_ptr;
1809 const struct ecoff_debug_swap * const debug_swap
1810 = &ecoff_backend (abfd)->debug_swap;
1811 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1812 struct ecoff_find_line *line_info;
1814 /* Make sure we have the FDR's. */
1815 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1816 || bfd_get_symcount (abfd) == 0)
1819 if (ecoff_data (abfd)->find_line_info == NULL)
1821 ecoff_data (abfd)->find_line_info =
1822 ((struct ecoff_find_line *)
1823 bfd_zalloc (abfd, sizeof (struct ecoff_find_line)));
1824 if (ecoff_data (abfd)->find_line_info == NULL)
1827 line_info = ecoff_data (abfd)->find_line_info;
1829 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1830 debug_swap, line_info, filename_ptr,
1831 functionname_ptr, retline_ptr);
1834 /* Copy private BFD data. This is called by objcopy and strip. We
1835 use it to copy the ECOFF debugging information from one BFD to the
1836 other. It would be theoretically possible to represent the ECOFF
1837 debugging information in the symbol table. However, it would be a
1838 lot of work, and there would be little gain (gas, gdb, and ld
1839 already access the ECOFF debugging information via the
1840 ecoff_debug_info structure, and that structure would have to be
1841 retained in order to support ECOFF debugging in MIPS ELF).
1843 The debugging information for the ECOFF external symbols comes from
1844 the symbol table, so this function only handles the other debugging
1848 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1852 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1853 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1855 asymbol **sym_ptr_ptr;
1859 /* This function is selected based on the input vector. We only
1860 want to copy information over if the output BFD also uses ECOFF
1862 if (bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1865 /* Copy the GP value and the register masks. */
1866 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1867 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1868 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1869 for (i = 0; i < 3; i++)
1870 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1872 /* Copy the version stamp. */
1873 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1875 /* If there are no symbols, don't copy any debugging information. */
1876 c = bfd_get_symcount (obfd);
1877 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1878 if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1881 /* See if there are any local symbols. */
1883 for (; c > 0; c--, sym_ptr_ptr++)
1885 if (ecoffsymbol (*sym_ptr_ptr)->local)
1894 /* There are some local symbols. We just bring over all the
1895 debugging information. FIXME: This is not quite the right
1896 thing to do. If the user has asked us to discard all
1897 debugging information, then we are probably going to wind up
1898 keeping it because there will probably be some local symbol
1899 which objcopy did not discard. We should actually break
1900 apart the debugging information and only keep that which
1901 applies to the symbols we want to keep. */
1902 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1903 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1904 oinfo->line = iinfo->line;
1906 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1907 oinfo->external_dnr = iinfo->external_dnr;
1909 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1910 oinfo->external_pdr = iinfo->external_pdr;
1912 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1913 oinfo->external_sym = iinfo->external_sym;
1915 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1916 oinfo->external_opt = iinfo->external_opt;
1918 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1919 oinfo->external_aux = iinfo->external_aux;
1921 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1922 oinfo->ss = iinfo->ss;
1924 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1925 oinfo->external_fdr = iinfo->external_fdr;
1927 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1928 oinfo->external_rfd = iinfo->external_rfd;
1932 /* We are discarding all the local symbol information. Look
1933 through the external symbols and remove all references to FDR
1934 or aux information. */
1935 c = bfd_get_symcount (obfd);
1936 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1937 for (; c > 0; c--, sym_ptr_ptr++)
1941 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1942 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1944 esym.asym.index = indexNil;
1945 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1946 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1953 /* Set the architecture. The supported architecture is stored in the
1954 backend pointer. We always set the architecture anyhow, since many
1955 callers ignore the return value. */
1958 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1960 enum bfd_architecture arch;
1961 unsigned long machine;
1963 bfd_default_set_arch_mach (abfd, arch, machine);
1964 return arch == ecoff_backend (abfd)->arch;
1967 /* Get the size of the section headers. */
1971 _bfd_ecoff_sizeof_headers (abfd, reloc)
1980 for (current = abfd->sections;
1981 current != (asection *)NULL;
1982 current = current->next)
1985 ret = (bfd_coff_filhsz (abfd)
1986 + bfd_coff_aoutsz (abfd)
1987 + c * bfd_coff_scnhsz (abfd));
1988 return BFD_ALIGN (ret, 16);
1991 /* Get the contents of a section. */
1994 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
1999 bfd_size_type count;
2001 return _bfd_generic_get_section_contents (abfd, section, location,
2005 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2006 called via qsort. */
2009 ecoff_sort_hdrs (arg1, arg2)
2013 const asection *hdr1 = *(const asection **) arg1;
2014 const asection *hdr2 = *(const asection **) arg2;
2016 if ((hdr1->flags & SEC_ALLOC) != 0)
2018 if ((hdr2->flags & SEC_ALLOC) == 0)
2023 if ((hdr2->flags & SEC_ALLOC) != 0)
2026 if (hdr1->vma < hdr2->vma)
2028 else if (hdr1->vma > hdr2->vma)
2034 /* Calculate the file position for each section, and set
2038 ecoff_compute_section_file_positions (abfd)
2041 file_ptr sofar, file_sofar;
2042 asection **sorted_hdrs;
2046 boolean first_data, first_nonalloc;
2047 const bfd_vma round = ecoff_backend (abfd)->round;
2049 sofar = _bfd_ecoff_sizeof_headers (abfd, false);
2052 /* Sort the sections by VMA. */
2053 sorted_hdrs = (asection **) bfd_malloc (abfd->section_count
2054 * sizeof (asection *));
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 *),
2067 first_nonalloc = true;
2068 for (i = 0; i < abfd->section_count; i++)
2070 unsigned int alignment_power;
2072 current = sorted_hdrs[i];
2074 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2075 supposed to indicate the number of .pdata entries that are
2076 really in the section. Each entry is 8 bytes. We store this
2077 away in line_filepos before increasing the section size. */
2078 if (strcmp (current->name, _PDATA) == 0)
2079 current->line_filepos = current->_raw_size / 8;
2081 alignment_power = current->alignment_power;
2083 /* On Ultrix, the data sections in an executable file must be
2084 aligned to a page boundary within the file. This does not
2085 affect the section size, though. FIXME: Does this work for
2086 other platforms? It requires some modification for the
2087 Alpha, because .rdata on the Alpha goes with the text, not
2089 if ((abfd->flags & EXEC_P) != 0
2090 && (abfd->flags & D_PAGED) != 0
2092 && (current->flags & SEC_CODE) == 0
2093 && (! ecoff_backend (abfd)->rdata_in_text
2094 || strcmp (current->name, _RDATA) != 0)
2095 && strcmp (current->name, _PDATA) != 0
2096 && strcmp (current->name, _RCONST) != 0)
2098 sofar = (sofar + round - 1) &~ (round - 1);
2099 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2102 else if (strcmp (current->name, _LIB) == 0)
2104 /* On Irix 4, the location of contents of the .lib section
2105 from a shared library section is also rounded up to a
2108 sofar = (sofar + round - 1) &~ (round - 1);
2109 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2111 else if (first_nonalloc
2112 && (current->flags & SEC_ALLOC) == 0
2113 && (abfd->flags & D_PAGED) != 0)
2115 /* Skip up to the next page for an unallocated section, such
2116 as the .comment section on the Alpha. This leaves room
2117 for the .bss section. */
2118 first_nonalloc = false;
2119 sofar = (sofar + round - 1) &~ (round - 1);
2120 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2123 /* Align the sections in the file to the same boundary on
2124 which they are aligned in virtual memory. */
2125 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2126 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2127 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2129 if ((abfd->flags & D_PAGED) != 0
2130 && (current->flags & SEC_ALLOC) != 0)
2132 sofar += (current->vma - sofar) % round;
2133 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2134 file_sofar += (current->vma - file_sofar) % round;
2137 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2138 current->filepos = file_sofar;
2140 sofar += current->_raw_size;
2141 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2142 file_sofar += current->_raw_size;
2144 /* make sure that this section is of the right size too */
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);
2149 current->_raw_size += sofar - old_sofar;
2155 ecoff_data (abfd)->reloc_filepos = file_sofar;
2160 /* Determine the location of the relocs for all the sections in the
2161 output file, as well as the location of the symbolic debugging
2164 static bfd_size_type
2165 ecoff_compute_reloc_file_positions (abfd)
2168 const bfd_size_type external_reloc_size =
2169 ecoff_backend (abfd)->external_reloc_size;
2170 file_ptr reloc_base;
2171 bfd_size_type reloc_size;
2175 if (! abfd->output_has_begun)
2177 if (! ecoff_compute_section_file_positions (abfd))
2179 abfd->output_has_begun = true;
2182 reloc_base = ecoff_data (abfd)->reloc_filepos;
2185 for (current = abfd->sections;
2186 current != (asection *)NULL;
2187 current = current->next)
2189 if (current->reloc_count == 0)
2190 current->rel_filepos = 0;
2193 bfd_size_type relsize;
2195 current->rel_filepos = reloc_base;
2196 relsize = current->reloc_count * external_reloc_size;
2197 reloc_size += relsize;
2198 reloc_base += relsize;
2202 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2204 /* At least on Ultrix, the symbol table of an executable file must
2205 be aligned to a page boundary. FIXME: Is this true on other
2207 if ((abfd->flags & EXEC_P) != 0
2208 && (abfd->flags & D_PAGED) != 0)
2209 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2210 &~ (ecoff_backend (abfd)->round - 1));
2212 ecoff_data (abfd)->sym_filepos = sym_base;
2217 /* Set the contents of a section. */
2220 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2225 bfd_size_type count;
2227 /* This must be done first, because bfd_set_section_contents is
2228 going to set output_has_begun to true. */
2229 if (abfd->output_has_begun == false)
2231 if (! ecoff_compute_section_file_positions (abfd))
2235 /* Handle the .lib section specially so that Irix 4 shared libraries
2236 work out. See coff_set_section_contents in coffcode.h. */
2237 if (strcmp (section->name, _LIB) == 0)
2239 bfd_byte *rec, *recend;
2241 rec = (bfd_byte *) location;
2242 recend = rec + count;
2243 while (rec < recend)
2246 rec += bfd_get_32 (abfd, rec) * 4;
2249 BFD_ASSERT (rec == recend);
2255 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2256 || bfd_write (location, 1, count, abfd) != count)
2262 /* Get the GP value for an ECOFF file. This is a hook used by
2266 bfd_ecoff_get_gp_value (abfd)
2269 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2270 || bfd_get_format (abfd) != bfd_object)
2272 bfd_set_error (bfd_error_invalid_operation);
2276 return ecoff_data (abfd)->gp;
2279 /* Set the GP value for an ECOFF file. This is a hook used by the
2283 bfd_ecoff_set_gp_value (abfd, gp_value)
2287 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2288 || bfd_get_format (abfd) != bfd_object)
2290 bfd_set_error (bfd_error_invalid_operation);
2294 ecoff_data (abfd)->gp = gp_value;
2299 /* Set the register masks for an ECOFF file. This is a hook used by
2303 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2305 unsigned long gprmask;
2306 unsigned long fprmask;
2307 unsigned long *cprmask;
2309 ecoff_data_type *tdata;
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 tdata = ecoff_data (abfd);
2319 tdata->gprmask = gprmask;
2320 tdata->fprmask = fprmask;
2321 if (cprmask != (unsigned long *) NULL)
2325 for (i = 0; i < 3; i++)
2326 tdata->cprmask[i] = cprmask[i];
2332 /* Get ECOFF EXTR information for an external symbol. This function
2333 is passed to bfd_ecoff_debug_externals. */
2336 ecoff_get_extr (sym, esym)
2340 ecoff_symbol_type *ecoff_sym_ptr;
2343 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2344 || ecoffsymbol (sym)->native == NULL)
2346 /* Don't include debugging, local, or section symbols. */
2347 if ((sym->flags & BSF_DEBUGGING) != 0
2348 || (sym->flags & BSF_LOCAL) != 0
2349 || (sym->flags & BSF_SECTION_SYM) != 0)
2353 esym->cobol_main = 0;
2354 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2357 /* FIXME: we can do better than this for st and sc. */
2358 esym->asym.st = stGlobal;
2359 esym->asym.sc = scAbs;
2360 esym->asym.reserved = 0;
2361 esym->asym.index = indexNil;
2365 ecoff_sym_ptr = ecoffsymbol (sym);
2367 if (ecoff_sym_ptr->local)
2370 input_bfd = bfd_asymbol_bfd (sym);
2371 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2372 (input_bfd, ecoff_sym_ptr->native, esym);
2374 /* If the symbol was defined by the linker, then esym will be
2375 undefined but sym will not be. Get a better class for such a
2377 if ((esym->asym.sc == scUndefined
2378 || esym->asym.sc == scSUndefined)
2379 && ! bfd_is_und_section (bfd_get_section (sym)))
2380 esym->asym.sc = scAbs;
2382 /* Adjust the FDR index for the symbol by that used for the input
2384 if (esym->ifd != -1)
2386 struct ecoff_debug_info *input_debug;
2388 input_debug = &ecoff_data (input_bfd)->debug_info;
2389 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2390 if (input_debug->ifdmap != (RFDT *) NULL)
2391 esym->ifd = input_debug->ifdmap[esym->ifd];
2397 /* Set the external symbol index. This routine is passed to
2398 bfd_ecoff_debug_externals. */
2401 ecoff_set_index (sym, indx)
2405 ecoff_set_sym_index (sym, indx);
2408 /* Write out an ECOFF file. */
2411 _bfd_ecoff_write_object_contents (abfd)
2414 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2415 const bfd_vma round = backend->round;
2416 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2417 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2418 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2419 const bfd_size_type external_hdr_size
2420 = backend->debug_swap.external_hdr_size;
2421 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2422 void (* const adjust_reloc_out) PARAMS ((bfd *,
2424 struct internal_reloc *))
2425 = backend->adjust_reloc_out;
2426 void (* const swap_reloc_out) PARAMS ((bfd *,
2427 const struct internal_reloc *,
2429 = backend->swap_reloc_out;
2430 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2431 HDRR * const symhdr = &debug->symbolic_header;
2434 bfd_size_type reloc_size;
2435 bfd_size_type text_size;
2437 boolean set_text_start;
2438 bfd_size_type data_size;
2440 boolean set_data_start;
2441 bfd_size_type bss_size;
2443 PTR reloc_buff = NULL;
2444 struct internal_filehdr internal_f;
2445 struct internal_aouthdr internal_a;
2448 /* Determine where the sections and relocs will go in the output
2450 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2453 for (current = abfd->sections;
2454 current != (asection *)NULL;
2455 current = current->next)
2457 current->target_index = count;
2461 if ((abfd->flags & D_PAGED) != 0)
2462 text_size = _bfd_ecoff_sizeof_headers (abfd, false);
2466 set_text_start = false;
2469 set_data_start = false;
2472 /* Write section headers to the file. */
2474 /* Allocate buff big enough to hold a section header,
2475 file header, or a.out header. */
2483 buff = (PTR) bfd_malloc ((size_t) siz);
2488 internal_f.f_nscns = 0;
2489 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2491 for (current = abfd->sections;
2492 current != (asection *) NULL;
2493 current = current->next)
2495 struct internal_scnhdr section;
2498 ++internal_f.f_nscns;
2500 strncpy (section.s_name, current->name, sizeof section.s_name);
2502 /* This seems to be correct for Irix 4 shared libraries. */
2503 vma = bfd_get_section_vma (abfd, current);
2504 if (strcmp (current->name, _LIB) == 0)
2505 section.s_vaddr = 0;
2507 section.s_vaddr = vma;
2509 section.s_paddr = current->lma;
2510 section.s_size = bfd_get_section_size_before_reloc (current);
2512 /* If this section is unloadable then the scnptr will be 0. */
2513 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2514 section.s_scnptr = 0;
2516 section.s_scnptr = current->filepos;
2517 section.s_relptr = current->rel_filepos;
2519 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2520 object file produced by the assembler is supposed to point to
2521 information about how much room is required by objects of
2522 various different sizes. I think this only matters if we
2523 want the linker to compute the best size to use, or
2524 something. I don't know what happens if the information is
2526 if (strcmp (current->name, _PDATA) != 0)
2527 section.s_lnnoptr = 0;
2530 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2531 hold the number of entries in the section (each entry is
2532 8 bytes). We stored this in the line_filepos field in
2533 ecoff_compute_section_file_positions. */
2534 section.s_lnnoptr = current->line_filepos;
2537 section.s_nreloc = current->reloc_count;
2538 section.s_nlnno = 0;
2539 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2542 if (bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff) == 0
2543 || bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2546 if ((section.s_flags & STYP_TEXT) != 0
2547 || ((section.s_flags & STYP_RDATA) != 0
2548 && backend->rdata_in_text)
2549 || section.s_flags == STYP_PDATA
2550 || (section.s_flags & STYP_DYNAMIC) != 0
2551 || (section.s_flags & STYP_LIBLIST) != 0
2552 || (section.s_flags & STYP_RELDYN) != 0
2553 || section.s_flags == STYP_CONFLIC
2554 || (section.s_flags & STYP_DYNSTR) != 0
2555 || (section.s_flags & STYP_DYNSYM) != 0
2556 || (section.s_flags & STYP_HASH) != 0
2557 || (section.s_flags & STYP_ECOFF_INIT) != 0
2558 || (section.s_flags & STYP_ECOFF_FINI) != 0
2559 || section.s_flags == STYP_RCONST)
2561 text_size += bfd_get_section_size_before_reloc (current);
2562 if (! set_text_start || text_start > vma)
2565 set_text_start = true;
2568 else if ((section.s_flags & STYP_RDATA) != 0
2569 || (section.s_flags & STYP_DATA) != 0
2570 || (section.s_flags & STYP_LITA) != 0
2571 || (section.s_flags & STYP_LIT8) != 0
2572 || (section.s_flags & STYP_LIT4) != 0
2573 || (section.s_flags & STYP_SDATA) != 0
2574 || section.s_flags == STYP_XDATA
2575 || (section.s_flags & STYP_GOT) != 0)
2577 data_size += bfd_get_section_size_before_reloc (current);
2578 if (! set_data_start || data_start > vma)
2581 set_data_start = true;
2584 else if ((section.s_flags & STYP_BSS) != 0
2585 || (section.s_flags & STYP_SBSS) != 0)
2586 bss_size += bfd_get_section_size_before_reloc (current);
2587 else if (section.s_flags == 0
2588 || (section.s_flags & STYP_ECOFF_LIB) != 0
2589 || section.s_flags == STYP_COMMENT)
2595 /* Set up the file header. */
2597 internal_f.f_magic = ecoff_get_magic (abfd);
2599 /* We will NOT put a fucking timestamp in the header here. Every
2600 time you put it back, I will come in and take it out again. I'm
2601 sorry. This field does not belong here. We fill it with a 0 so
2602 it compares the same but is not a reasonable time. --
2604 internal_f.f_timdat = 0;
2606 if (bfd_get_symcount (abfd) != 0)
2608 /* The ECOFF f_nsyms field is not actually the number of
2609 symbols, it's the size of symbolic information header. */
2610 internal_f.f_nsyms = external_hdr_size;
2611 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2615 internal_f.f_nsyms = 0;
2616 internal_f.f_symptr = 0;
2619 internal_f.f_opthdr = aoutsz;
2621 internal_f.f_flags = F_LNNO;
2622 if (reloc_size == 0)
2623 internal_f.f_flags |= F_RELFLG;
2624 if (bfd_get_symcount (abfd) == 0)
2625 internal_f.f_flags |= F_LSYMS;
2626 if (abfd->flags & EXEC_P)
2627 internal_f.f_flags |= F_EXEC;
2629 if (bfd_little_endian (abfd))
2630 internal_f.f_flags |= F_AR32WR;
2632 internal_f.f_flags |= F_AR32W;
2634 /* Set up the ``optional'' header. */
2635 if ((abfd->flags & D_PAGED) != 0)
2636 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2638 internal_a.magic = ECOFF_AOUT_OMAGIC;
2640 /* FIXME: Is this really correct? */
2641 internal_a.vstamp = symhdr->vstamp;
2643 /* At least on Ultrix, these have to be rounded to page boundaries.
2644 FIXME: Is this true on other platforms? */
2645 if ((abfd->flags & D_PAGED) != 0)
2647 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2648 internal_a.text_start = text_start &~ (round - 1);
2649 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2650 internal_a.data_start = data_start &~ (round - 1);
2654 internal_a.tsize = text_size;
2655 internal_a.text_start = text_start;
2656 internal_a.dsize = data_size;
2657 internal_a.data_start = data_start;
2660 /* On Ultrix, the initial portions of the .sbss and .bss segments
2661 are at the end of the data section. The bsize field in the
2662 optional header records how many bss bytes are required beyond
2663 those in the data section. The value is not rounded to a page
2665 if (bss_size < internal_a.dsize - data_size)
2668 bss_size -= internal_a.dsize - data_size;
2669 internal_a.bsize = bss_size;
2670 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2672 internal_a.entry = bfd_get_start_address (abfd);
2674 internal_a.gp_value = ecoff_data (abfd)->gp;
2676 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2677 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2678 for (i = 0; i < 4; i++)
2679 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2681 /* Let the backend adjust the headers if necessary. */
2682 if (backend->adjust_headers)
2684 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2688 /* Write out the file header and the optional header. */
2690 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2693 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2694 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2697 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2698 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2701 /* Build the external symbol information. This must be done before
2702 writing out the relocs so that we know the symbol indices. We
2703 don't do this if this BFD was created by the backend linker,
2704 since it will have already handled the symbols and relocs. */
2705 if (! ecoff_data (abfd)->linker)
2707 symhdr->iextMax = 0;
2708 symhdr->issExtMax = 0;
2709 debug->external_ext = debug->external_ext_end = NULL;
2710 debug->ssext = debug->ssext_end = NULL;
2711 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2712 (((abfd->flags & EXEC_P) == 0)
2714 ecoff_get_extr, ecoff_set_index)
2718 /* Write out the relocs. */
2719 for (current = abfd->sections;
2720 current != (asection *) NULL;
2721 current = current->next)
2723 arelent **reloc_ptr_ptr;
2724 arelent **reloc_end;
2727 if (current->reloc_count == 0)
2731 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2732 if (reloc_buff == NULL)
2735 reloc_ptr_ptr = current->orelocation;
2736 reloc_end = reloc_ptr_ptr + current->reloc_count;
2737 out_ptr = (char *) reloc_buff;
2739 reloc_ptr_ptr < reloc_end;
2740 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2744 struct internal_reloc in;
2746 memset ((PTR) &in, 0, sizeof in);
2748 reloc = *reloc_ptr_ptr;
2749 sym = *reloc->sym_ptr_ptr;
2751 in.r_vaddr = (reloc->address
2752 + bfd_get_section_vma (abfd, current));
2753 in.r_type = reloc->howto->type;
2755 if ((sym->flags & BSF_SECTION_SYM) == 0)
2757 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2764 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2765 if (strcmp (name, ".text") == 0)
2766 in.r_symndx = RELOC_SECTION_TEXT;
2767 else if (strcmp (name, ".rdata") == 0)
2768 in.r_symndx = RELOC_SECTION_RDATA;
2769 else if (strcmp (name, ".data") == 0)
2770 in.r_symndx = RELOC_SECTION_DATA;
2771 else if (strcmp (name, ".sdata") == 0)
2772 in.r_symndx = RELOC_SECTION_SDATA;
2773 else if (strcmp (name, ".sbss") == 0)
2774 in.r_symndx = RELOC_SECTION_SBSS;
2775 else if (strcmp (name, ".bss") == 0)
2776 in.r_symndx = RELOC_SECTION_BSS;
2777 else if (strcmp (name, ".init") == 0)
2778 in.r_symndx = RELOC_SECTION_INIT;
2779 else if (strcmp (name, ".lit8") == 0)
2780 in.r_symndx = RELOC_SECTION_LIT8;
2781 else if (strcmp (name, ".lit4") == 0)
2782 in.r_symndx = RELOC_SECTION_LIT4;
2783 else if (strcmp (name, ".xdata") == 0)
2784 in.r_symndx = RELOC_SECTION_XDATA;
2785 else if (strcmp (name, ".pdata") == 0)
2786 in.r_symndx = RELOC_SECTION_PDATA;
2787 else if (strcmp (name, ".fini") == 0)
2788 in.r_symndx = RELOC_SECTION_FINI;
2789 else if (strcmp (name, ".lita") == 0)
2790 in.r_symndx = RELOC_SECTION_LITA;
2791 else if (strcmp (name, "*ABS*") == 0)
2792 in.r_symndx = RELOC_SECTION_ABS;
2793 else if (strcmp (name, ".rconst") == 0)
2794 in.r_symndx = RELOC_SECTION_RCONST;
2800 (*adjust_reloc_out) (abfd, reloc, &in);
2802 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2805 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2807 if (bfd_write (reloc_buff,
2808 external_reloc_size, current->reloc_count, abfd)
2809 != external_reloc_size * current->reloc_count)
2811 bfd_release (abfd, reloc_buff);
2815 /* Write out the symbolic debugging information. */
2816 if (bfd_get_symcount (abfd) > 0)
2818 /* Write out the debugging information. */
2819 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2820 ecoff_data (abfd)->sym_filepos)
2826 /* The .bss section of a demand paged executable must receive an
2827 entire page. If there are symbols, the symbols will start on the
2828 next page. If there are no symbols, we must fill out the page by
2830 if (bfd_get_symcount (abfd) == 0
2831 && (abfd->flags & EXEC_P) != 0
2832 && (abfd->flags & D_PAGED) != 0)
2836 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2839 if (bfd_read (&c, 1, 1, abfd) == 0)
2841 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2844 if (bfd_write (&c, 1, 1, abfd) != 1)
2848 if (reloc_buff != NULL)
2849 bfd_release (abfd, reloc_buff);
2854 if (reloc_buff != NULL)
2855 bfd_release (abfd, reloc_buff);
2861 /* Archive handling. ECOFF uses what appears to be a unique type of
2862 archive header (armap). The byte ordering of the armap and the
2863 contents are encoded in the name of the armap itself. At least for
2864 now, we only support archives with the same byte ordering in the
2865 armap and the contents.
2867 The first four bytes in the armap are the number of symbol
2868 definitions. This is always a power of two.
2870 This is followed by the symbol definitions. Each symbol definition
2871 occupies 8 bytes. The first four bytes are the offset from the
2872 start of the armap strings to the null-terminated string naming
2873 this symbol. The second four bytes are the file offset to the
2874 archive member which defines this symbol. If the second four bytes
2875 are 0, then this is not actually a symbol definition, and it should
2878 The symbols are hashed into the armap with a closed hashing scheme.
2879 See the functions below for the details of the algorithm.
2881 After the symbol definitions comes four bytes holding the size of
2882 the string table, followed by the string table itself. */
2884 /* The name of an archive headers looks like this:
2885 __________E[BL]E[BL]_ (with a trailing space).
2886 The trailing space is changed to an X if the archive is changed to
2887 indicate that the armap is out of date.
2889 The Alpha seems to use ________64E[BL]E[BL]_. */
2891 #define ARMAP_BIG_ENDIAN 'B'
2892 #define ARMAP_LITTLE_ENDIAN 'L'
2893 #define ARMAP_MARKER 'E'
2894 #define ARMAP_START_LENGTH 10
2895 #define ARMAP_HEADER_MARKER_INDEX 10
2896 #define ARMAP_HEADER_ENDIAN_INDEX 11
2897 #define ARMAP_OBJECT_MARKER_INDEX 12
2898 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2899 #define ARMAP_END_INDEX 14
2900 #define ARMAP_END "_ "
2902 /* This is a magic number used in the hashing algorithm. */
2903 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2905 /* This returns the hash value to use for a string. It also sets
2906 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2907 is the number of entries in the hash table, and HLOG is the log
2911 ecoff_armap_hash (s, rehash, size, hlog)
2913 unsigned int *rehash;
2923 hash = ((hash >> 27) | (hash << 5)) + *s++;
2924 hash *= ARMAP_HASH_MAGIC;
2925 *rehash = (hash & (size - 1)) | 1;
2926 return hash >> (32 - hlog);
2929 /* Read in the armap. */
2932 _bfd_ecoff_slurp_armap (abfd)
2937 struct areltdata *mapdata;
2938 bfd_size_type parsed_size;
2940 struct artdata *ardata;
2943 struct symdef *symdef_ptr;
2946 /* Get the name of the first element. */
2947 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2953 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2956 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2957 standard COFF armap. We could move the ECOFF armap stuff into
2958 bfd_slurp_armap, but that seems inappropriate since no other
2959 target uses this format. Instead, we check directly for a COFF
2961 if (strncmp (nextname, "/ ", 16) == 0)
2962 return bfd_slurp_armap (abfd);
2964 /* See if the first element is an armap. */
2965 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2966 ARMAP_START_LENGTH) != 0
2967 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2968 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2969 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2970 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2971 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2972 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2973 || strncmp (nextname + ARMAP_END_INDEX,
2974 ARMAP_END, sizeof ARMAP_END - 1) != 0)
2976 bfd_has_map (abfd) = false;
2980 /* Make sure we have the right byte ordering. */
2981 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2982 ^ (bfd_header_big_endian (abfd)))
2983 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2984 ^ (bfd_big_endian (abfd))))
2986 bfd_set_error (bfd_error_wrong_format);
2990 /* Read in the armap. */
2991 ardata = bfd_ardata (abfd);
2992 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2993 if (mapdata == (struct areltdata *) NULL)
2995 parsed_size = mapdata->parsed_size;
2996 bfd_release (abfd, (PTR) mapdata);
2998 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
2999 if (raw_armap == (char *) NULL)
3002 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3004 if (bfd_get_error () != bfd_error_system_call)
3005 bfd_set_error (bfd_error_malformed_archive);
3006 bfd_release (abfd, (PTR) raw_armap);
3010 ardata->tdata = (PTR) raw_armap;
3012 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3014 ardata->symdef_count = 0;
3015 ardata->cache = (struct ar_cache *) NULL;
3017 /* This code used to overlay the symdefs over the raw archive data,
3018 but that doesn't work on a 64 bit host. */
3020 stringbase = raw_armap + count * 8 + 8;
3022 #ifdef CHECK_ARMAP_HASH
3026 /* Double check that I have the hashing algorithm right by making
3027 sure that every symbol can be looked up successfully. */
3029 for (i = 1; i < count; i <<= 1)
3031 BFD_ASSERT (i == count);
3033 raw_ptr = raw_armap + 4;
3034 for (i = 0; i < count; i++, raw_ptr += 8)
3036 unsigned int name_offset, file_offset;
3037 unsigned int hash, rehash, srch;
3039 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3040 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3041 if (file_offset == 0)
3043 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3048 /* See if we can rehash to this location. */
3049 for (srch = (hash + rehash) & (count - 1);
3050 srch != hash && srch != i;
3051 srch = (srch + rehash) & (count - 1))
3052 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3054 BFD_ASSERT (srch == i);
3058 #endif /* CHECK_ARMAP_HASH */
3060 raw_ptr = raw_armap + 4;
3061 for (i = 0; i < count; i++, raw_ptr += 8)
3062 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3063 ++ardata->symdef_count;
3065 symdef_ptr = ((struct symdef *)
3067 ardata->symdef_count * sizeof (struct symdef)));
3071 ardata->symdefs = (carsym *) symdef_ptr;
3073 raw_ptr = raw_armap + 4;
3074 for (i = 0; i < count; i++, raw_ptr += 8)
3076 unsigned int name_offset, file_offset;
3078 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3079 if (file_offset == 0)
3081 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3082 symdef_ptr->s.name = stringbase + name_offset;
3083 symdef_ptr->file_offset = file_offset;
3087 ardata->first_file_filepos = bfd_tell (abfd);
3088 /* Pad to an even boundary. */
3089 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3091 bfd_has_map (abfd) = true;
3096 /* Write out an armap. */
3099 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3101 unsigned int elength;
3103 unsigned int orl_count;
3106 unsigned int hashsize, hashlog;
3107 unsigned int symdefsize;
3109 unsigned int stringsize;
3110 unsigned int mapsize;
3113 struct stat statbuf;
3116 bfd_byte *hashtable;
3120 /* Ultrix appears to use as a hash table size the least power of two
3121 greater than twice the number of entries. */
3122 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3124 hashsize = 1 << hashlog;
3126 symdefsize = hashsize * 8;
3128 stringsize = stridx + padit;
3130 /* Include 8 bytes to store symdefsize and stringsize in output. */
3131 mapsize = symdefsize + stringsize + 8;
3133 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3135 memset ((PTR) &hdr, 0, sizeof hdr);
3137 /* Work out the ECOFF armap name. */
3138 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3139 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3140 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3141 (bfd_header_big_endian (abfd)
3143 : ARMAP_LITTLE_ENDIAN);
3144 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3145 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3146 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3147 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3149 /* Write the timestamp of the archive header to be just a little bit
3150 later than the timestamp of the file, otherwise the linker will
3151 complain that the index is out of date. Actually, the Ultrix
3152 linker just checks the archive name; the GNU linker may check the
3154 stat (abfd->filename, &statbuf);
3155 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3157 /* The DECstation uses zeroes for the uid, gid and mode of the
3159 hdr.ar_uid[0] = '0';
3160 hdr.ar_gid[0] = '0';
3161 hdr.ar_mode[0] = '0';
3163 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3165 hdr.ar_fmag[0] = '`';
3166 hdr.ar_fmag[1] = '\012';
3168 /* Turn all null bytes in the header into spaces. */
3169 for (i = 0; i < sizeof (struct ar_hdr); i++)
3170 if (((char *)(&hdr))[i] == '\0')
3171 (((char *)(&hdr))[i]) = ' ';
3173 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3174 != sizeof (struct ar_hdr))
3177 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3178 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3181 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3185 current = abfd->archive_head;
3187 for (i = 0; i < orl_count; i++)
3189 unsigned int hash, rehash;
3191 /* Advance firstreal to the file position of this archive
3193 if (((bfd *) map[i].pos) != last_elt)
3197 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3198 firstreal += firstreal % 2;
3199 current = current->next;
3201 while (current != (bfd *) map[i].pos);
3206 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3207 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3211 /* The desired slot is already taken. */
3212 for (srch = (hash + rehash) & (hashsize - 1);
3214 srch = (srch + rehash) & (hashsize - 1))
3215 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3218 BFD_ASSERT (srch != hash);
3223 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3224 (PTR) (hashtable + hash * 8));
3225 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3226 (PTR) (hashtable + hash * 8 + 4));
3229 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3232 bfd_release (abfd, hashtable);
3234 /* Now write the strings. */
3235 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3236 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3238 for (i = 0; i < orl_count; i++)
3242 len = strlen (*map[i].name) + 1;
3243 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3247 /* The spec sez this should be a newline. But in order to be
3248 bug-compatible for DECstation ar we use a null. */
3251 if (bfd_write ("", 1, 1, abfd) != 1)
3258 /* See whether this BFD is an archive. If it is, read in the armap
3259 and the extended name table. */
3262 _bfd_ecoff_archive_p (abfd)
3265 struct artdata *tdata_hold;
3266 char armag[SARMAG + 1];
3268 tdata_hold = abfd->tdata.aout_ar_data;
3270 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG)
3272 if (bfd_get_error () != bfd_error_system_call)
3273 bfd_set_error (bfd_error_wrong_format);
3274 return (const bfd_target *) NULL;
3277 if (strncmp (armag, ARMAG, SARMAG) != 0)
3279 bfd_set_error (bfd_error_wrong_format);
3283 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3284 involves a cast, we can't do it as the left operand of
3286 abfd->tdata.aout_ar_data =
3287 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3289 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3291 abfd->tdata.aout_ar_data = tdata_hold;
3292 return (const bfd_target *) NULL;
3295 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3296 bfd_ardata (abfd)->cache = NULL;
3297 bfd_ardata (abfd)->archive_head = NULL;
3298 bfd_ardata (abfd)->symdefs = NULL;
3299 bfd_ardata (abfd)->extended_names = NULL;
3300 bfd_ardata (abfd)->tdata = NULL;
3302 if (_bfd_ecoff_slurp_armap (abfd) == false
3303 || _bfd_ecoff_slurp_extended_name_table (abfd) == false)
3305 bfd_release (abfd, bfd_ardata (abfd));
3306 abfd->tdata.aout_ar_data = tdata_hold;
3307 return (const bfd_target *) NULL;
3310 if (bfd_has_map (abfd))
3314 /* This archive has a map, so we may presume that the contents
3315 are object files. Make sure that if the first file in the
3316 archive can be recognized as an object file, it is for this
3317 target. If not, assume that this is the wrong format. If
3318 the first file is not an object file, somebody is doing
3319 something weird, and we permit it so that ar -t will work. */
3321 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3326 first->target_defaulted = false;
3328 if (bfd_check_format (first, bfd_object)
3329 && first->xvec != abfd->xvec)
3331 (void) bfd_close (first);
3332 bfd_release (abfd, bfd_ardata (abfd));
3333 abfd->tdata.aout_ar_data = tdata_hold;
3334 bfd_set_error (bfd_error_wrong_format);
3338 /* We ought to close first here, but we can't, because we
3339 have no way to remove it from the archive cache. FIXME. */
3346 /* ECOFF linker code. */
3348 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3349 PARAMS ((struct bfd_hash_entry *entry,
3350 struct bfd_hash_table *table,
3351 const char *string));
3352 static boolean ecoff_link_add_archive_symbols
3353 PARAMS ((bfd *, struct bfd_link_info *));
3354 static boolean ecoff_link_check_archive_element
3355 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3356 static boolean ecoff_link_add_object_symbols
3357 PARAMS ((bfd *, struct bfd_link_info *));
3358 static boolean ecoff_link_add_externals
3359 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3361 /* Routine to create an entry in an ECOFF link hash table. */
3363 static struct bfd_hash_entry *
3364 ecoff_link_hash_newfunc (entry, table, string)
3365 struct bfd_hash_entry *entry;
3366 struct bfd_hash_table *table;
3369 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3371 /* Allocate the structure if it has not already been allocated by a
3373 if (ret == (struct ecoff_link_hash_entry *) NULL)
3374 ret = ((struct ecoff_link_hash_entry *)
3375 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3376 if (ret == (struct ecoff_link_hash_entry *) NULL)
3379 /* Call the allocation method of the superclass. */
3380 ret = ((struct ecoff_link_hash_entry *)
3381 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3386 /* Set local fields. */
3392 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3394 return (struct bfd_hash_entry *) ret;
3397 /* Create an ECOFF link hash table. */
3399 struct bfd_link_hash_table *
3400 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3403 struct ecoff_link_hash_table *ret;
3405 ret = ((struct ecoff_link_hash_table *)
3406 bfd_alloc (abfd, sizeof (struct ecoff_link_hash_table)));
3409 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3410 ecoff_link_hash_newfunc))
3413 return (struct bfd_link_hash_table *) NULL;
3418 /* Look up an entry in an ECOFF link hash table. */
3420 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3421 ((struct ecoff_link_hash_entry *) \
3422 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3424 /* Traverse an ECOFF link hash table. */
3426 #define ecoff_link_hash_traverse(table, func, info) \
3427 (bfd_link_hash_traverse \
3429 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3432 /* Get the ECOFF link hash table from the info structure. This is
3435 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3437 /* Given an ECOFF BFD, add symbols to the global hash table as
3441 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3443 struct bfd_link_info *info;
3445 switch (bfd_get_format (abfd))
3448 return ecoff_link_add_object_symbols (abfd, info);
3450 return ecoff_link_add_archive_symbols (abfd, info);
3452 bfd_set_error (bfd_error_wrong_format);
3457 /* Add the symbols from an archive file to the global hash table.
3458 This looks through the undefined symbols, looks each one up in the
3459 archive hash table, and adds any associated object file. We do not
3460 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3461 already have a hash table, so there is no reason to construct
3465 ecoff_link_add_archive_symbols (abfd, info)
3467 struct bfd_link_info *info;
3469 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3470 const bfd_byte *raw_armap;
3471 struct bfd_link_hash_entry **pundef;
3472 unsigned int armap_count;
3473 unsigned int armap_log;
3475 const bfd_byte *hashtable;
3476 const char *stringbase;
3478 if (! bfd_has_map (abfd))
3480 /* An empty archive is a special case. */
3481 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3483 bfd_set_error (bfd_error_no_armap);
3487 /* If we don't have any raw data for this archive, as can happen on
3488 Irix 4.0.5F, we call the generic routine.
3489 FIXME: We should be more clever about this, since someday tdata
3490 may get to something for a generic archive. */
3491 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3492 if (raw_armap == (bfd_byte *) NULL)
3493 return (_bfd_generic_link_add_archive_symbols
3494 (abfd, info, ecoff_link_check_archive_element));
3496 armap_count = bfd_h_get_32 (abfd, raw_armap);
3499 for (i = 1; i < armap_count; i <<= 1)
3501 BFD_ASSERT (i == armap_count);
3503 hashtable = raw_armap + 4;
3504 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3506 /* Look through the list of undefined symbols. */
3507 pundef = &info->hash->undefs;
3508 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3510 struct bfd_link_hash_entry *h;
3511 unsigned int hash, rehash;
3512 unsigned int file_offset;
3518 /* When a symbol is defined, it is not necessarily removed from
3520 if (h->type != bfd_link_hash_undefined
3521 && h->type != bfd_link_hash_common)
3523 /* Remove this entry from the list, for general cleanliness
3524 and because we are going to look through the list again
3525 if we search any more libraries. We can't remove the
3526 entry if it is the tail, because that would lose any
3527 entries we add to the list later on. */
3528 if (*pundef != info->hash->undefs_tail)
3529 *pundef = (*pundef)->next;
3531 pundef = &(*pundef)->next;
3535 /* Native ECOFF linkers do not pull in archive elements merely
3536 to satisfy common definitions, so neither do we. We leave
3537 them on the list, though, in case we are linking against some
3538 other object format. */
3539 if (h->type != bfd_link_hash_undefined)
3541 pundef = &(*pundef)->next;
3545 /* Look for this symbol in the archive hash table. */
3546 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3549 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3550 if (file_offset == 0)
3552 /* Nothing in this slot. */
3553 pundef = &(*pundef)->next;
3557 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3558 if (name[0] != h->root.string[0]
3559 || strcmp (name, h->root.string) != 0)
3564 /* That was the wrong symbol. Try rehashing. */
3566 for (srch = (hash + rehash) & (armap_count - 1);
3568 srch = (srch + rehash) & (armap_count - 1))
3570 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3571 if (file_offset == 0)
3573 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3574 if (name[0] == h->root.string[0]
3575 && strcmp (name, h->root.string) == 0)
3584 pundef = &(*pundef)->next;
3591 element = (*backend->get_elt_at_filepos) (abfd, file_offset);
3592 if (element == (bfd *) NULL)
3595 if (! bfd_check_format (element, bfd_object))
3598 /* Unlike the generic linker, we know that this element provides
3599 a definition for an undefined symbol and we know that we want
3600 to include it. We don't need to check anything. */
3601 if (! (*info->callbacks->add_archive_element) (info, element, name))
3603 if (! ecoff_link_add_object_symbols (element, info))
3606 pundef = &(*pundef)->next;
3612 /* This is called if we used _bfd_generic_link_add_archive_symbols
3613 because we were not dealing with an ECOFF archive. */
3616 ecoff_link_check_archive_element (abfd, info, pneeded)
3618 struct bfd_link_info *info;
3621 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3622 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3623 = backend->debug_swap.swap_ext_in;
3625 bfd_size_type external_ext_size;
3626 PTR external_ext = NULL;
3634 if (! ecoff_slurp_symbolic_header (abfd))
3637 /* If there are no symbols, we don't want it. */
3638 if (bfd_get_symcount (abfd) == 0)
3639 goto successful_return;
3641 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3643 /* Read in the external symbols and external strings. */
3644 external_ext_size = backend->debug_swap.external_ext_size;
3645 esize = symhdr->iextMax * external_ext_size;
3646 external_ext = (PTR) bfd_malloc (esize);
3647 if (external_ext == NULL && esize != 0)
3650 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3651 || bfd_read (external_ext, 1, esize, abfd) != esize)
3654 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3655 if (ssext == NULL && symhdr->issExtMax != 0)
3658 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3659 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd) !=
3660 (bfd_size_type) symhdr->issExtMax))
3663 /* Look through the external symbols to see if they define some
3664 symbol that is currently undefined. */
3665 ext_ptr = (char *) external_ext;
3666 ext_end = ext_ptr + esize;
3667 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3672 struct bfd_link_hash_entry *h;
3674 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3676 /* See if this symbol defines something. */
3677 if (esym.asym.st != stGlobal
3678 && esym.asym.st != stLabel
3679 && esym.asym.st != stProc)
3682 switch (esym.asym.sc)
3706 name = ssext + esym.asym.iss;
3707 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3709 /* Unlike the generic linker, we do not pull in elements because
3710 of common symbols. */
3711 if (h == (struct bfd_link_hash_entry *) NULL
3712 || h->type != bfd_link_hash_undefined)
3715 /* Include this element. */
3716 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3718 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3722 goto successful_return;
3726 if (external_ext != NULL)
3727 free (external_ext);
3732 if (external_ext != NULL)
3733 free (external_ext);
3739 /* Add symbols from an ECOFF object file to the global linker hash
3743 ecoff_link_add_object_symbols (abfd, info)
3745 struct bfd_link_info *info;
3748 bfd_size_type external_ext_size;
3749 PTR external_ext = NULL;
3754 if (! ecoff_slurp_symbolic_header (abfd))
3757 /* If there are no symbols, we don't want it. */
3758 if (bfd_get_symcount (abfd) == 0)
3761 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3763 /* Read in the external symbols and external strings. */
3764 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3765 esize = symhdr->iextMax * external_ext_size;
3766 external_ext = (PTR) bfd_malloc (esize);
3767 if (external_ext == NULL && esize != 0)
3770 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3771 || bfd_read (external_ext, 1, esize, abfd) != esize)
3774 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3775 if (ssext == NULL && symhdr->issExtMax != 0)
3778 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3779 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd)
3780 != (bfd_size_type) symhdr->issExtMax))
3783 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3787 if (external_ext != NULL)
3788 free (external_ext);
3794 if (external_ext != NULL)
3795 free (external_ext);
3799 /* Add the external symbols of an object file to the global linker
3800 hash table. The external symbols and strings we are passed are
3801 just allocated on the stack, and will be discarded. We must
3802 explicitly save any information we may need later on in the link.
3803 We do not want to read the external symbol information again. */
3806 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3808 struct bfd_link_info *info;
3812 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3813 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3814 = backend->debug_swap.swap_ext_in;
3815 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3816 unsigned long ext_count;
3817 struct ecoff_link_hash_entry **sym_hash;
3821 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3823 sym_hash = ((struct ecoff_link_hash_entry **)
3825 ext_count * sizeof (struct bfd_link_hash_entry *)));
3828 ecoff_data (abfd)->sym_hashes = sym_hash;
3830 ext_ptr = (char *) external_ext;
3831 ext_end = ext_ptr + ext_count * external_ext_size;
3832 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3839 struct ecoff_link_hash_entry *h;
3843 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3845 /* Skip debugging symbols. */
3847 switch (esym.asym.st)
3863 /* Get the information for this symbol. */
3864 value = esym.asym.value;
3865 switch (esym.asym.sc)
3885 section = bfd_make_section_old_way (abfd, ".text");
3886 value -= section->vma;
3889 section = bfd_make_section_old_way (abfd, ".data");
3890 value -= section->vma;
3893 section = bfd_make_section_old_way (abfd, ".bss");
3894 value -= section->vma;
3897 section = bfd_abs_section_ptr;
3900 section = bfd_und_section_ptr;
3903 section = bfd_make_section_old_way (abfd, ".sdata");
3904 value -= section->vma;
3907 section = bfd_make_section_old_way (abfd, ".sbss");
3908 value -= section->vma;
3911 section = bfd_make_section_old_way (abfd, ".rdata");
3912 value -= section->vma;
3915 if (value > ecoff_data (abfd)->gp_size)
3917 section = bfd_com_section_ptr;
3922 if (ecoff_scom_section.name == NULL)
3924 /* Initialize the small common section. */
3925 ecoff_scom_section.name = SCOMMON;
3926 ecoff_scom_section.flags = SEC_IS_COMMON;
3927 ecoff_scom_section.output_section = &ecoff_scom_section;
3928 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3929 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3930 ecoff_scom_symbol.name = SCOMMON;
3931 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3932 ecoff_scom_symbol.section = &ecoff_scom_section;
3933 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3935 section = &ecoff_scom_section;
3938 section = bfd_und_section_ptr;
3941 section = bfd_make_section_old_way (abfd, ".init");
3942 value -= section->vma;
3945 section = bfd_make_section_old_way (abfd, ".fini");
3946 value -= section->vma;
3949 section = bfd_make_section_old_way (abfd, ".rconst");
3950 value -= section->vma;
3954 if (section == (asection *) NULL)
3957 name = ssext + esym.asym.iss;
3960 if (! (_bfd_generic_link_add_one_symbol
3962 esym.weakext ? BSF_WEAK : BSF_GLOBAL,
3963 section, value, (const char *) NULL, true, true,
3964 (struct bfd_link_hash_entry **) &h)))
3969 /* If we are building an ECOFF hash table, save the external
3970 symbol information. */
3971 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3973 if (h->abfd == (bfd *) NULL
3974 || (! bfd_is_und_section (section)
3975 && (! bfd_is_com_section (section)
3976 || (h->root.type != bfd_link_hash_defined
3977 && h->root.type != bfd_link_hash_defweak))))
3983 /* Remember whether this symbol was small undefined. */
3984 if (esym.asym.sc == scSUndefined)
3987 /* If this symbol was ever small undefined, it needs to wind
3988 up in a GP relative section. We can't control the
3989 section of a defined symbol, but we can control the
3990 section of a common symbol. This case is actually needed
3991 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
3993 && h->root.type == bfd_link_hash_common
3994 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
3996 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3998 h->root.u.c.p->section->flags = SEC_ALLOC;
3999 if (h->esym.asym.sc == scCommon)
4000 h->esym.asym.sc = scSCommon;
4008 /* ECOFF final link routines. */
4010 static boolean ecoff_final_link_debug_accumulate
4011 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4013 static boolean ecoff_link_write_external
4014 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4015 static boolean ecoff_indirect_link_order
4016 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4017 struct bfd_link_order *));
4018 static boolean ecoff_reloc_link_order
4019 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4020 struct bfd_link_order *));
4022 /* Structure used to pass information to ecoff_link_write_external. */
4027 struct bfd_link_info *info;
4030 /* ECOFF final link routine. This looks through all the input BFDs
4031 and gathers together all the debugging information, and then
4032 processes all the link order information. This may cause it to
4033 close and reopen some input BFDs; I'll see how bad this is. */
4036 _bfd_ecoff_bfd_final_link (abfd, info)
4038 struct bfd_link_info *info;
4040 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4041 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4044 register bfd *input_bfd;
4046 struct bfd_link_order *p;
4047 struct extsym_info einfo;
4049 /* We accumulate the debugging information counts in the symbolic
4051 symhdr = &debug->symbolic_header;
4053 symhdr->ilineMax = 0;
4057 symhdr->isymMax = 0;
4058 symhdr->ioptMax = 0;
4059 symhdr->iauxMax = 0;
4061 symhdr->issExtMax = 0;
4064 symhdr->iextMax = 0;
4066 /* We accumulate the debugging information itself in the debug_info
4069 debug->external_dnr = NULL;
4070 debug->external_pdr = NULL;
4071 debug->external_sym = NULL;
4072 debug->external_opt = NULL;
4073 debug->external_aux = NULL;
4075 debug->ssext = debug->ssext_end = NULL;
4076 debug->external_fdr = NULL;
4077 debug->external_rfd = NULL;
4078 debug->external_ext = debug->external_ext_end = NULL;
4080 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4081 if (handle == (PTR) NULL)
4084 /* Accumulate the debugging symbols from each input BFD. */
4085 for (input_bfd = info->input_bfds;
4086 input_bfd != (bfd *) NULL;
4087 input_bfd = input_bfd->link_next)
4091 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4093 /* Abitrarily set the symbolic header vstamp to the vstamp
4094 of the first object file in the link. */
4095 if (symhdr->vstamp == 0)
4097 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4098 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4102 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4103 debug, &backend->debug_swap,
4108 /* Combine the register masks. */
4109 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4110 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4111 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4112 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4113 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4114 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4117 /* Write out the external symbols. */
4120 ecoff_link_hash_traverse (ecoff_hash_table (info),
4121 ecoff_link_write_external,
4124 if (info->relocateable)
4126 /* We need to make a pass over the link_orders to count up the
4127 number of relocations we will need to output, so that we know
4128 how much space they will take up. */
4129 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4132 for (p = o->link_order_head;
4133 p != (struct bfd_link_order *) NULL;
4135 if (p->type == bfd_indirect_link_order)
4136 o->reloc_count += p->u.indirect.section->reloc_count;
4137 else if (p->type == bfd_section_reloc_link_order
4138 || p->type == bfd_symbol_reloc_link_order)
4143 /* Compute the reloc and symbol file positions. */
4144 ecoff_compute_reloc_file_positions (abfd);
4146 /* Write out the debugging information. */
4147 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4148 &backend->debug_swap, info,
4149 ecoff_data (abfd)->sym_filepos))
4152 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4154 if (info->relocateable)
4156 /* Now reset the reloc_count field of the sections in the output
4157 BFD to 0, so that we can use them to keep track of how many
4158 relocs we have output thus far. */
4159 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4163 /* Get a value for the GP register. */
4164 if (ecoff_data (abfd)->gp == 0)
4166 struct bfd_link_hash_entry *h;
4168 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4169 if (h != (struct bfd_link_hash_entry *) NULL
4170 && h->type == bfd_link_hash_defined)
4171 ecoff_data (abfd)->gp = (h->u.def.value
4172 + h->u.def.section->output_section->vma
4173 + h->u.def.section->output_offset);
4174 else if (info->relocateable)
4178 /* Make up a value. */
4180 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4183 && (strcmp (o->name, _SBSS) == 0
4184 || strcmp (o->name, _SDATA) == 0
4185 || strcmp (o->name, _LIT4) == 0
4186 || strcmp (o->name, _LIT8) == 0
4187 || strcmp (o->name, _LITA) == 0))
4190 ecoff_data (abfd)->gp = lo + 0x8000;
4194 /* If the relocate_section function needs to do a reloc
4195 involving the GP value, it should make a reloc_dangerous
4196 callback to warn that GP is not defined. */
4200 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4202 for (p = o->link_order_head;
4203 p != (struct bfd_link_order *) NULL;
4206 if (p->type == bfd_indirect_link_order
4207 && (bfd_get_flavour (p->u.indirect.section->owner)
4208 == bfd_target_ecoff_flavour))
4210 if (! ecoff_indirect_link_order (abfd, info, o, p))
4213 else if (p->type == bfd_section_reloc_link_order
4214 || p->type == bfd_symbol_reloc_link_order)
4216 if (! ecoff_reloc_link_order (abfd, info, o, p))
4221 if (! _bfd_default_link_order (abfd, info, o, p))
4227 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4229 ecoff_data (abfd)->linker = true;
4234 /* Accumulate the debugging information for an input BFD into the
4235 output BFD. This must read in the symbolic information of the
4239 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4242 struct bfd_link_info *info;
4245 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4246 const struct ecoff_debug_swap * const swap =
4247 &ecoff_backend (input_bfd)->debug_swap;
4248 HDRR *symhdr = &debug->symbolic_header;
4251 #define READ(ptr, offset, count, size, type) \
4252 if (symhdr->count == 0) \
4253 debug->ptr = NULL; \
4256 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
4257 if (debug->ptr == NULL) \
4260 goto return_something; \
4262 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4264 || (bfd_read (debug->ptr, size, symhdr->count, \
4265 input_bfd) != size * symhdr->count)) \
4268 goto return_something; \
4272 /* If raw_syments is not NULL, then the data was already by read by
4273 _bfd_ecoff_slurp_symbolic_info. */
4274 if (ecoff_data (input_bfd)->raw_syments == NULL)
4276 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4278 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4279 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4280 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4281 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4282 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4284 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4285 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4286 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4290 /* We do not read the external strings or the external symbols. */
4292 ret = (bfd_ecoff_debug_accumulate
4293 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4294 &ecoff_backend (output_bfd)->debug_swap,
4295 input_bfd, debug, swap, info));
4298 if (ecoff_data (input_bfd)->raw_syments == NULL)
4300 if (debug->line != NULL)
4302 if (debug->external_dnr != NULL)
4303 free (debug->external_dnr);
4304 if (debug->external_pdr != NULL)
4305 free (debug->external_pdr);
4306 if (debug->external_sym != NULL)
4307 free (debug->external_sym);
4308 if (debug->external_opt != NULL)
4309 free (debug->external_opt);
4310 if (debug->external_aux != NULL)
4311 free (debug->external_aux);
4312 if (debug->ss != NULL)
4314 if (debug->external_fdr != NULL)
4315 free (debug->external_fdr);
4316 if (debug->external_rfd != NULL)
4317 free (debug->external_rfd);
4319 /* Make sure we don't accidentally follow one of these pointers
4320 into freed memory. */
4322 debug->external_dnr = NULL;
4323 debug->external_pdr = NULL;
4324 debug->external_sym = NULL;
4325 debug->external_opt = NULL;
4326 debug->external_aux = NULL;
4328 debug->external_fdr = NULL;
4329 debug->external_rfd = NULL;
4335 /* Put out information for an external symbol. These come only from
4339 ecoff_link_write_external (h, data)
4340 struct ecoff_link_hash_entry *h;
4343 struct extsym_info *einfo = (struct extsym_info *) data;
4344 bfd *output_bfd = einfo->abfd;
4347 /* We need to check if this symbol is being stripped. */
4348 if (h->root.type == bfd_link_hash_undefined
4349 || h->root.type == bfd_link_hash_undefweak)
4351 else if (einfo->info->strip == strip_all
4352 || (einfo->info->strip == strip_some
4353 && bfd_hash_lookup (einfo->info->keep_hash,
4354 h->root.root.string,
4355 false, false) == NULL))
4360 if (strip || h->written)
4363 if (h->abfd == (bfd *) NULL)
4366 h->esym.cobol_main = 0;
4367 h->esym.weakext = 0;
4368 h->esym.reserved = 0;
4369 h->esym.ifd = ifdNil;
4370 h->esym.asym.value = 0;
4371 h->esym.asym.st = stGlobal;
4373 if (h->root.type != bfd_link_hash_defined
4374 && h->root.type != bfd_link_hash_defweak)
4375 h->esym.asym.sc = scAbs;
4378 asection *output_section;
4381 output_section = h->root.u.def.section->output_section;
4382 name = bfd_section_name (output_section->owner, output_section);
4384 if (strcmp (name, _TEXT) == 0)
4385 h->esym.asym.sc = scText;
4386 else if (strcmp (name, _DATA) == 0)
4387 h->esym.asym.sc = scData;
4388 else if (strcmp (name, _SDATA) == 0)
4389 h->esym.asym.sc = scSData;
4390 else if (strcmp (name, _RDATA) == 0)
4391 h->esym.asym.sc = scRData;
4392 else if (strcmp (name, _BSS) == 0)
4393 h->esym.asym.sc = scBss;
4394 else if (strcmp (name, _SBSS) == 0)
4395 h->esym.asym.sc = scSBss;
4396 else if (strcmp (name, _INIT) == 0)
4397 h->esym.asym.sc = scInit;
4398 else if (strcmp (name, _FINI) == 0)
4399 h->esym.asym.sc = scFini;
4400 else if (strcmp (name, _PDATA) == 0)
4401 h->esym.asym.sc = scPData;
4402 else if (strcmp (name, _XDATA) == 0)
4403 h->esym.asym.sc = scXData;
4404 else if (strcmp (name, _RCONST) == 0)
4405 h->esym.asym.sc = scRConst;
4407 h->esym.asym.sc = scAbs;
4410 h->esym.asym.reserved = 0;
4411 h->esym.asym.index = indexNil;
4413 else if (h->esym.ifd != -1)
4415 struct ecoff_debug_info *debug;
4417 /* Adjust the FDR index for the symbol by that used for the
4419 debug = &ecoff_data (h->abfd)->debug_info;
4420 BFD_ASSERT (h->esym.ifd >= 0
4421 && h->esym.ifd < debug->symbolic_header.ifdMax);
4422 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4425 switch (h->root.type)
4428 case bfd_link_hash_new:
4430 case bfd_link_hash_undefined:
4431 case bfd_link_hash_undefweak:
4432 if (h->esym.asym.sc != scUndefined
4433 && h->esym.asym.sc != scSUndefined)
4434 h->esym.asym.sc = scUndefined;
4436 case bfd_link_hash_defined:
4437 case bfd_link_hash_defweak:
4438 if (h->esym.asym.sc == scUndefined
4439 || h->esym.asym.sc == scSUndefined)
4440 h->esym.asym.sc = scAbs;
4441 else if (h->esym.asym.sc == scCommon)
4442 h->esym.asym.sc = scBss;
4443 else if (h->esym.asym.sc == scSCommon)
4444 h->esym.asym.sc = scSBss;
4445 h->esym.asym.value = (h->root.u.def.value
4446 + h->root.u.def.section->output_section->vma
4447 + h->root.u.def.section->output_offset);
4449 case bfd_link_hash_common:
4450 if (h->esym.asym.sc != scCommon
4451 && h->esym.asym.sc != scSCommon)
4452 h->esym.asym.sc = scCommon;
4453 h->esym.asym.value = h->root.u.c.size;
4455 case bfd_link_hash_indirect:
4456 case bfd_link_hash_warning:
4457 /* FIXME: Ignore these for now. The circumstances under which
4458 they should be written out are not clear to me. */
4462 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4464 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4467 return (bfd_ecoff_debug_one_external
4468 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4469 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4473 /* Relocate and write an ECOFF section into an ECOFF output file. */
4476 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4478 struct bfd_link_info *info;
4479 asection *output_section;
4480 struct bfd_link_order *link_order;
4482 asection *input_section;
4484 struct ecoff_section_tdata *section_tdata;
4485 bfd_size_type raw_size;
4486 bfd_size_type cooked_size;
4487 bfd_byte *contents = NULL;
4488 bfd_size_type external_reloc_size;
4489 bfd_size_type external_relocs_size;
4490 PTR external_relocs = NULL;
4492 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4494 if (link_order->size == 0)
4497 input_section = link_order->u.indirect.section;
4498 input_bfd = input_section->owner;
4499 section_tdata = ecoff_section_data (input_bfd, input_section);
4501 raw_size = input_section->_raw_size;
4502 cooked_size = input_section->_cooked_size;
4503 if (cooked_size == 0)
4504 cooked_size = raw_size;
4506 BFD_ASSERT (input_section->output_section == output_section);
4507 BFD_ASSERT (input_section->output_offset == link_order->offset);
4508 BFD_ASSERT (cooked_size == link_order->size);
4510 /* Get the section contents. We allocate memory for the larger of
4511 the size before relocating and the size after relocating. */
4512 contents = (bfd_byte *) bfd_malloc (raw_size >= cooked_size
4514 : (size_t) cooked_size);
4515 if (contents == NULL && raw_size != 0)
4518 /* If we are relaxing, the contents may have already been read into
4519 memory, in which case we copy them into our new buffer. We don't
4520 simply reuse the old buffer in case cooked_size > raw_size. */
4521 if (section_tdata != (struct ecoff_section_tdata *) NULL
4522 && section_tdata->contents != (bfd_byte *) NULL)
4523 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4526 if (! bfd_get_section_contents (input_bfd, input_section,
4528 (file_ptr) 0, raw_size))
4532 /* Get the relocs. If we are relaxing MIPS code, they will already
4533 have been read in. Otherwise, we read them in now. */
4534 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4535 external_relocs_size = external_reloc_size * input_section->reloc_count;
4537 if (section_tdata != (struct ecoff_section_tdata *) NULL
4538 && section_tdata->external_relocs != NULL)
4539 external_relocs = section_tdata->external_relocs;
4542 external_relocs = (PTR) bfd_malloc ((size_t) external_relocs_size);
4543 if (external_relocs == NULL && external_relocs_size != 0)
4546 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4547 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4548 != external_relocs_size))
4552 /* Relocate the section contents. */
4553 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4554 (output_bfd, info, input_bfd, input_section, contents,
4558 /* Write out the relocated section. */
4559 if (! bfd_set_section_contents (output_bfd,
4562 input_section->output_offset,
4566 /* If we are producing relocateable output, the relocs were
4567 modified, and we write them out now. We use the reloc_count
4568 field of output_section to keep track of the number of relocs we
4569 have output so far. */
4570 if (info->relocateable)
4572 if (bfd_seek (output_bfd,
4573 (output_section->rel_filepos +
4574 output_section->reloc_count * external_reloc_size),
4576 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4577 != external_relocs_size))
4579 output_section->reloc_count += input_section->reloc_count;
4582 if (contents != NULL)
4584 if (external_relocs != NULL && section_tdata == NULL)
4585 free (external_relocs);
4589 if (contents != NULL)
4591 if (external_relocs != NULL && section_tdata == NULL)
4592 free (external_relocs);
4596 /* Generate a reloc when linking an ECOFF file. This is a reloc
4597 requested by the linker, and does come from any input file. This
4598 is used to build constructor and destructor tables when linking
4602 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4604 struct bfd_link_info *info;
4605 asection *output_section;
4606 struct bfd_link_order *link_order;
4608 enum bfd_link_order_type type;
4612 struct internal_reloc in;
4613 bfd_size_type external_reloc_size;
4617 type = link_order->type;
4619 addend = link_order->u.reloc.p->addend;
4621 /* We set up an arelent to pass to the backend adjust_reloc_out
4623 rel.address = link_order->offset;
4625 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4628 bfd_set_error (bfd_error_bad_value);
4632 if (type == bfd_section_reloc_link_order)
4634 section = link_order->u.reloc.p->u.section;
4635 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4639 struct bfd_link_hash_entry *h;
4641 /* Treat a reloc against a defined symbol as though it were
4642 actually against the section. */
4643 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4644 link_order->u.reloc.p->u.name,
4645 false, false, false);
4647 && (h->type == bfd_link_hash_defined
4648 || h->type == bfd_link_hash_defweak))
4650 type = bfd_section_reloc_link_order;
4651 section = h->u.def.section->output_section;
4652 /* It seems that we ought to add the symbol value to the
4653 addend here, but in practice it has already been added
4654 because it was passed to constructor_callback. */
4655 addend += section->vma + h->u.def.section->output_offset;
4659 /* We can't set up a reloc against a symbol correctly,
4660 because we have no asymbol structure. Currently no
4661 adjust_reloc_out routine cares. */
4662 rel.sym_ptr_ptr = (asymbol **) NULL;
4666 /* All ECOFF relocs are in-place. Put the addend into the object
4669 BFD_ASSERT (rel.howto->partial_inplace);
4673 bfd_reloc_status_type rstat;
4677 size = bfd_get_reloc_size (rel.howto);
4678 buf = (bfd_byte *) bfd_zmalloc (size);
4679 if (buf == (bfd_byte *) NULL)
4681 rstat = _bfd_relocate_contents (rel.howto, output_bfd, addend, buf);
4687 case bfd_reloc_outofrange:
4689 case bfd_reloc_overflow:
4690 if (! ((*info->callbacks->reloc_overflow)
4692 (link_order->type == bfd_section_reloc_link_order
4693 ? bfd_section_name (output_bfd, section)
4694 : link_order->u.reloc.p->u.name),
4695 rel.howto->name, addend, (bfd *) NULL,
4696 (asection *) NULL, (bfd_vma) 0)))
4703 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4704 (file_ptr) link_order->offset, size);
4712 /* Move the information into a internal_reloc structure. */
4713 in.r_vaddr = (rel.address
4714 + bfd_get_section_vma (output_bfd, output_section));
4715 in.r_type = rel.howto->type;
4717 if (type == bfd_symbol_reloc_link_order)
4719 struct ecoff_link_hash_entry *h;
4721 h = ((struct ecoff_link_hash_entry *)
4722 bfd_wrapped_link_hash_lookup (output_bfd, info,
4723 link_order->u.reloc.p->u.name,
4724 false, false, true));
4725 if (h != (struct ecoff_link_hash_entry *) NULL
4727 in.r_symndx = h->indx;
4730 if (! ((*info->callbacks->unattached_reloc)
4731 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4732 (asection *) NULL, (bfd_vma) 0)))
4742 name = bfd_get_section_name (output_bfd, section);
4743 if (strcmp (name, ".text") == 0)
4744 in.r_symndx = RELOC_SECTION_TEXT;
4745 else if (strcmp (name, ".rdata") == 0)
4746 in.r_symndx = RELOC_SECTION_RDATA;
4747 else if (strcmp (name, ".data") == 0)
4748 in.r_symndx = RELOC_SECTION_DATA;
4749 else if (strcmp (name, ".sdata") == 0)
4750 in.r_symndx = RELOC_SECTION_SDATA;
4751 else if (strcmp (name, ".sbss") == 0)
4752 in.r_symndx = RELOC_SECTION_SBSS;
4753 else if (strcmp (name, ".bss") == 0)
4754 in.r_symndx = RELOC_SECTION_BSS;
4755 else if (strcmp (name, ".init") == 0)
4756 in.r_symndx = RELOC_SECTION_INIT;
4757 else if (strcmp (name, ".lit8") == 0)
4758 in.r_symndx = RELOC_SECTION_LIT8;
4759 else if (strcmp (name, ".lit4") == 0)
4760 in.r_symndx = RELOC_SECTION_LIT4;
4761 else if (strcmp (name, ".xdata") == 0)
4762 in.r_symndx = RELOC_SECTION_XDATA;
4763 else if (strcmp (name, ".pdata") == 0)
4764 in.r_symndx = RELOC_SECTION_PDATA;
4765 else if (strcmp (name, ".fini") == 0)
4766 in.r_symndx = RELOC_SECTION_FINI;
4767 else if (strcmp (name, ".lita") == 0)
4768 in.r_symndx = RELOC_SECTION_LITA;
4769 else if (strcmp (name, "*ABS*") == 0)
4770 in.r_symndx = RELOC_SECTION_ABS;
4771 else if (strcmp (name, ".rconst") == 0)
4772 in.r_symndx = RELOC_SECTION_RCONST;
4778 /* Let the BFD backend adjust the reloc. */
4779 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4781 /* Get some memory and swap out the reloc. */
4782 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4783 rbuf = (bfd_byte *) bfd_malloc ((size_t) external_reloc_size);
4784 if (rbuf == (bfd_byte *) NULL)
4787 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4789 ok = (bfd_seek (output_bfd,
4790 (output_section->rel_filepos +
4791 output_section->reloc_count * external_reloc_size),
4793 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4794 == external_reloc_size));
4797 ++output_section->reloc_count;