1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 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));
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 void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
63 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
64 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
65 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
66 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
71 /* This stuff is somewhat copied from coffcode.h. */
73 static asection bfd_debug_section = { "*DEBUG*" };
75 /* Create an ECOFF object. */
78 _bfd_ecoff_mkobject (abfd)
81 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
82 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
83 if (abfd->tdata.ecoff_obj_data == NULL)
85 bfd_set_error (bfd_error_no_memory);
92 /* This is a hook called by coff_real_object_p to create any backend
93 specific information. */
96 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
101 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
102 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
103 ecoff_data_type *ecoff;
105 if (_bfd_ecoff_mkobject (abfd) == false)
108 ecoff = ecoff_data (abfd);
110 ecoff->sym_filepos = internal_f->f_symptr;
112 if (internal_a != (struct internal_aouthdr *) NULL)
116 ecoff->text_start = internal_a->text_start;
117 ecoff->text_end = internal_a->text_start + internal_a->tsize;
118 ecoff->gp = internal_a->gp_value;
119 ecoff->gprmask = internal_a->gprmask;
120 for (i = 0; i < 4; i++)
121 ecoff->cprmask[i] = internal_a->cprmask[i];
122 ecoff->fprmask = internal_a->fprmask;
123 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
124 abfd->flags |= D_PAGED;
126 abfd->flags &=~ D_PAGED;
129 /* It turns out that no special action is required by the MIPS or
130 Alpha ECOFF backends. They have different information in the
131 a.out header, but we just copy it all (e.g., gprmask, cprmask and
132 fprmask) and let the swapping routines ensure that only relevant
133 information is written out. */
138 /* This is a hook needed by SCO COFF, but we have nothing to do. */
142 _bfd_ecoff_make_section_hook (abfd, name)
146 return (asection *) NULL;
149 /* Initialize a new section. */
152 _bfd_ecoff_new_section_hook (abfd, section)
156 /* For the .pdata section, which has a special meaning on the Alpha,
157 we set the alignment power to 3. We correct this later in
158 ecoff_compute_section_file_positions. We do this hackery because
159 we need to know the exact unaligned size of the .pdata section in
160 order to set the lnnoptr field correctly. For every other
161 section we use an alignment power of 4; this could be made target
162 dependent by adding a field to ecoff_backend_data, but 4 appears
163 to be correct for both the MIPS and the Alpha. */
164 if (strcmp (section->name, _PDATA) == 0)
165 section->alignment_power = 3;
167 section->alignment_power = 4;
169 if (strcmp (section->name, _TEXT) == 0)
170 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
171 else if (strcmp (section->name, _DATA) == 0
172 || strcmp (section->name, _SDATA) == 0)
173 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
174 else if (strcmp (section->name, _RDATA) == 0
175 || strcmp (section->name, _LIT8) == 0
176 || strcmp (section->name, _LIT4) == 0)
177 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
178 else if (strcmp (section->name, _BSS) == 0
179 || strcmp (section->name, _SBSS) == 0)
180 section->flags |= SEC_ALLOC;
181 else if (strcmp (section->name, _LIB) == 0)
183 /* An Irix 4 shared libary. */
184 section->flags |= SEC_COFF_SHARED_LIBRARY;
187 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
188 uncertain about .init on some systems and I don't know how shared
194 /* Determine the machine architecture and type. This is called from
195 the generic COFF routines. It is the inverse of ecoff_get_magic,
196 below. This could be an ECOFF backend routine, with one version
197 for each target, but there aren't all that many ECOFF targets. */
200 _bfd_ecoff_set_arch_mach_hook (abfd, filehdr)
204 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
205 enum bfd_architecture arch;
208 switch (internal_f->f_magic)
211 case MIPS_MAGIC_LITTLE:
213 arch = bfd_arch_mips;
217 case MIPS_MAGIC_LITTLE2:
218 case MIPS_MAGIC_BIG2:
219 /* MIPS ISA level 2: the r6000 */
220 arch = bfd_arch_mips;
224 case MIPS_MAGIC_LITTLE3:
225 case MIPS_MAGIC_BIG3:
226 /* MIPS ISA level 3: the r4000 */
227 arch = bfd_arch_mips;
232 arch = bfd_arch_alpha;
237 arch = bfd_arch_obscure;
242 return bfd_default_set_arch_mach (abfd, arch, mach);
245 /* Get the magic number to use based on the architecture and machine.
246 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
249 ecoff_get_magic (abfd)
254 switch (bfd_get_arch (abfd))
257 switch (bfd_get_mach (abfd))
262 big = MIPS_MAGIC_BIG;
263 little = MIPS_MAGIC_LITTLE;
267 big = MIPS_MAGIC_BIG2;
268 little = MIPS_MAGIC_LITTLE2;
272 big = MIPS_MAGIC_BIG3;
273 little = MIPS_MAGIC_LITTLE3;
277 return abfd->xvec->byteorder_big_p ? big : little;
288 /* Get the section s_flags to use for a section. */
291 ecoff_sec_to_styp_flags (name, flags)
299 if (strcmp (name, _TEXT) == 0)
301 else if (strcmp (name, _DATA) == 0)
303 else if (strcmp (name, _SDATA) == 0)
305 else if (strcmp (name, _RDATA) == 0)
307 else if (strcmp (name, _LITA) == 0)
309 else if (strcmp (name, _LIT8) == 0)
311 else if (strcmp (name, _LIT4) == 0)
313 else if (strcmp (name, _BSS) == 0)
315 else if (strcmp (name, _SBSS) == 0)
317 else if (strcmp (name, _INIT) == 0)
318 styp = STYP_ECOFF_INIT;
319 else if (strcmp (name, _FINI) == 0)
320 styp = STYP_ECOFF_FINI;
321 else if (strcmp (name, _PDATA) == 0)
323 else if (strcmp (name, _XDATA) == 0)
325 else if (strcmp (name, _LIB) == 0)
326 styp = STYP_ECOFF_LIB;
327 else if (flags & SEC_CODE)
329 else if (flags & SEC_DATA)
331 else if (flags & SEC_READONLY)
333 else if (flags & SEC_LOAD)
338 if (flags & SEC_NEVER_LOAD)
344 /* Get the BFD flags to use for a section. */
348 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name)
353 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
354 long styp_flags = internal_s->s_flags;
355 flagword sec_flags=0;
357 if (styp_flags & STYP_NOLOAD)
358 sec_flags |= SEC_NEVER_LOAD;
360 /* For 386 COFF, at least, an unloadable text or data section is
361 actually a shared library section. */
362 if ((styp_flags & STYP_TEXT)
363 || (styp_flags & STYP_ECOFF_INIT)
364 || (styp_flags & STYP_ECOFF_FINI))
366 if (sec_flags & SEC_NEVER_LOAD)
367 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
369 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
371 else if ((styp_flags & STYP_DATA)
372 || (styp_flags & STYP_RDATA)
373 || (styp_flags & STYP_SDATA)
374 || styp_flags == STYP_PDATA
375 || styp_flags == STYP_XDATA)
377 if (sec_flags & SEC_NEVER_LOAD)
378 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
380 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
381 if ((styp_flags & STYP_RDATA)
382 || styp_flags == STYP_PDATA)
383 sec_flags |= SEC_READONLY;
385 else if ((styp_flags & STYP_BSS)
386 || (styp_flags & STYP_SBSS))
388 sec_flags |= SEC_ALLOC;
390 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
392 sec_flags |= SEC_NEVER_LOAD;
394 else if ((styp_flags & STYP_LITA)
395 || (styp_flags & STYP_LIT8)
396 || (styp_flags & STYP_LIT4))
398 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
400 else if (styp_flags & STYP_ECOFF_LIB)
402 sec_flags |= SEC_COFF_SHARED_LIBRARY;
406 sec_flags |= SEC_ALLOC | SEC_LOAD;
412 /* Read in the symbolic header for an ECOFF object file. */
415 ecoff_slurp_symbolic_header (abfd)
418 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
419 bfd_size_type external_hdr_size;
421 HDRR *internal_symhdr;
423 /* See if we've already read it in. */
424 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
425 backend->debug_swap.sym_magic)
428 /* See whether there is a symbolic header. */
429 if (ecoff_data (abfd)->sym_filepos == 0)
431 bfd_get_symcount (abfd) = 0;
435 /* At this point bfd_get_symcount (abfd) holds the number of symbols
436 as read from the file header, but on ECOFF this is always the
437 size of the symbolic information header. It would be cleaner to
438 handle this when we first read the file in coffgen.c. */
439 external_hdr_size = backend->debug_swap.external_hdr_size;
440 if (bfd_get_symcount (abfd) != external_hdr_size)
442 bfd_set_error (bfd_error_bad_value);
446 /* Read the symbolic information header. */
447 raw = (PTR) malloc ((size_t) external_hdr_size);
450 bfd_set_error (bfd_error_no_memory);
454 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
455 || (bfd_read (raw, external_hdr_size, 1, abfd)
456 != external_hdr_size))
458 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
459 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
461 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
463 bfd_set_error (bfd_error_bad_value);
467 /* Now we can get the correct number of symbols. */
468 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
469 + internal_symhdr->iextMax);
480 /* Read in and swap the important symbolic information for an ECOFF
481 object file. This is called by gdb via the read_debug_info entry
482 point in the backend structure. */
486 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
489 struct ecoff_debug_info *debug;
491 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
492 HDRR *internal_symhdr;
493 bfd_size_type raw_base;
494 bfd_size_type raw_size;
496 bfd_size_type external_fdr_size;
500 bfd_size_type raw_end;
501 bfd_size_type cb_end;
503 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
505 /* Check whether we've already gotten it, and whether there's any to
507 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
509 if (ecoff_data (abfd)->sym_filepos == 0)
511 bfd_get_symcount (abfd) = 0;
515 if (! ecoff_slurp_symbolic_header (abfd))
518 internal_symhdr = &debug->symbolic_header;
520 /* Read all the symbolic information at once. */
521 raw_base = (ecoff_data (abfd)->sym_filepos
522 + backend->debug_swap.external_hdr_size);
524 /* Alpha ecoff makes the determination of raw_size difficult. It has
525 an undocumented debug data section between the symhdr and the first
526 documented section. And the ordering of the sections varies between
527 statically and dynamically linked executables.
528 If bfd supports SEEK_END someday, this code could be simplified. */
532 #define UPDATE_RAW_END(start, count, size) \
533 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
534 if (cb_end > raw_end) \
537 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
538 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
539 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
540 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
541 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
542 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
543 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
544 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
545 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
546 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
547 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
549 #undef UPDATE_RAW_END
551 raw_size = raw_end - raw_base;
554 ecoff_data (abfd)->sym_filepos = 0;
557 raw = (PTR) bfd_alloc (abfd, raw_size);
560 bfd_set_error (bfd_error_no_memory);
564 (ecoff_data (abfd)->sym_filepos
565 + backend->debug_swap.external_hdr_size),
567 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
569 bfd_release (abfd, raw);
573 ecoff_data (abfd)->raw_syments = raw;
575 /* Get pointers for the numeric offsets in the HDRR structure. */
576 #define FIX(off1, off2, type) \
577 if (internal_symhdr->off1 == 0) \
578 debug->off2 = (type) NULL; \
580 debug->off2 = (type) ((char *) raw \
581 + (internal_symhdr->off1 \
583 FIX (cbLineOffset, line, unsigned char *);
584 FIX (cbDnOffset, external_dnr, PTR);
585 FIX (cbPdOffset, external_pdr, PTR);
586 FIX (cbSymOffset, external_sym, PTR);
587 FIX (cbOptOffset, external_opt, PTR);
588 FIX (cbAuxOffset, external_aux, union aux_ext *);
589 FIX (cbSsOffset, ss, char *);
590 FIX (cbSsExtOffset, ssext, char *);
591 FIX (cbFdOffset, external_fdr, PTR);
592 FIX (cbRfdOffset, external_rfd, PTR);
593 FIX (cbExtOffset, external_ext, PTR);
596 /* I don't want to always swap all the data, because it will just
597 waste time and most programs will never look at it. The only
598 time the linker needs most of the debugging information swapped
599 is when linking big-endian and little-endian MIPS object files
600 together, which is not a common occurrence.
602 We need to look at the fdr to deal with a lot of information in
603 the symbols, so we swap them here. */
604 debug->fdr = (struct fdr *) bfd_alloc (abfd,
605 (internal_symhdr->ifdMax *
606 sizeof (struct fdr)));
607 if (debug->fdr == NULL)
609 bfd_set_error (bfd_error_no_memory);
612 external_fdr_size = backend->debug_swap.external_fdr_size;
613 fdr_ptr = debug->fdr;
614 fraw_src = (char *) debug->external_fdr;
615 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
616 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
617 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
622 /* ECOFF symbol table routines. The ECOFF symbol table is described
623 in gcc/mips-tfile.c. */
625 /* ECOFF uses two common sections. One is the usual one, and the
626 other is for small objects. All the small objects are kept
627 together, and then referenced via the gp pointer, which yields
628 faster assembler code. This is what we use for the small common
630 static asection ecoff_scom_section;
631 static asymbol ecoff_scom_symbol;
632 static asymbol *ecoff_scom_symbol_ptr;
634 /* Create an empty symbol. */
637 _bfd_ecoff_make_empty_symbol (abfd)
640 ecoff_symbol_type *new;
642 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
643 if (new == (ecoff_symbol_type *) NULL)
645 bfd_set_error (bfd_error_no_memory);
646 return (asymbol *) NULL;
648 memset ((PTR) new, 0, sizeof *new);
649 new->symbol.section = (asection *) NULL;
650 new->fdr = (FDR *) NULL;
653 new->symbol.the_bfd = abfd;
657 /* Set the BFD flags and section for an ECOFF symbol. */
660 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext)
666 asym->the_bfd = abfd;
667 asym->value = ecoff_sym->value;
668 asym->section = &bfd_debug_section;
671 /* Most symbol types are just for debugging. */
672 switch (ecoff_sym->st)
681 if (ECOFF_IS_STAB (ecoff_sym))
683 asym->flags = BSF_DEBUGGING;
688 asym->flags = BSF_DEBUGGING;
693 asym->flags = BSF_EXPORT | BSF_GLOBAL;
696 asym->flags = BSF_LOCAL;
697 /* Normally, a local stProc symbol will have a corresponding
698 external symbol. We mark the local symbol as a debugging
699 symbol, in order to prevent nm from printing both out.
700 Similarly, we mark stLabel and stabs symbols as debugging
701 symbols. In both cases, we do want to set the value
702 correctly based on the symbol class. */
703 if (ecoff_sym->st == stProc
704 || ecoff_sym->st == stLabel
705 || ECOFF_IS_STAB (ecoff_sym))
706 asym->flags |= BSF_DEBUGGING;
708 switch (ecoff_sym->sc)
711 /* Used for compiler generated labels. Leave them in the
712 debugging section, and mark them as local. If BSF_DEBUGGING
713 is set, then nm does not display them for some reason. If no
714 flags are set then the linker whines about them. */
715 asym->flags = BSF_LOCAL;
718 asym->section = bfd_make_section_old_way (abfd, ".text");
719 asym->value -= asym->section->vma;
722 asym->section = bfd_make_section_old_way (abfd, ".data");
723 asym->value -= asym->section->vma;
726 asym->section = bfd_make_section_old_way (abfd, ".bss");
727 asym->value -= asym->section->vma;
730 asym->flags = BSF_DEBUGGING;
733 asym->section = bfd_abs_section_ptr;
736 asym->section = bfd_und_section_ptr;
746 asym->flags = BSF_DEBUGGING;
749 asym->section = bfd_make_section_old_way (abfd, ".sdata");
750 asym->value -= asym->section->vma;
753 asym->section = bfd_make_section_old_way (abfd, ".sbss");
754 asym->value -= asym->section->vma;
757 asym->section = bfd_make_section_old_way (abfd, ".rdata");
758 asym->value -= asym->section->vma;
761 asym->flags = BSF_DEBUGGING;
764 if (asym->value > ecoff_data (abfd)->gp_size)
766 asym->section = bfd_com_section_ptr;
772 if (ecoff_scom_section.name == NULL)
774 /* Initialize the small common section. */
775 ecoff_scom_section.name = SCOMMON;
776 ecoff_scom_section.flags = SEC_IS_COMMON;
777 ecoff_scom_section.output_section = &ecoff_scom_section;
778 ecoff_scom_section.symbol = &ecoff_scom_symbol;
779 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
780 ecoff_scom_symbol.name = SCOMMON;
781 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
782 ecoff_scom_symbol.section = &ecoff_scom_section;
783 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
785 asym->section = &ecoff_scom_section;
790 asym->flags = BSF_DEBUGGING;
793 asym->section = bfd_und_section_ptr;
798 asym->section = bfd_make_section_old_way (abfd, ".init");
799 asym->value -= asym->section->vma;
804 asym->flags = BSF_DEBUGGING;
807 asym->section = bfd_make_section_old_way (abfd, ".fini");
808 asym->value -= asym->section->vma;
814 /* Look for special constructors symbols and make relocation entries
815 in a special construction section. These are produced by the
816 -fgnu-linker argument to g++. */
817 if (ECOFF_IS_STAB (ecoff_sym))
819 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
831 arelent_chain *reloc_chain;
832 unsigned int bitsize;
834 /* Get a section with the same name as the symbol (usually
835 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
836 name ___CTOR_LIST (three underscores). We need
837 __CTOR_LIST (two underscores), since ECOFF doesn't use
838 a leading underscore. This should be handled by gcc,
839 but instead we do it here. Actually, this should all
840 be done differently anyhow. */
841 name = bfd_asymbol_name (asym);
842 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
847 section = bfd_get_section_by_name (abfd, name);
848 if (section == (asection *) NULL)
852 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
855 bfd_set_error (bfd_error_no_memory);
859 section = bfd_make_section (abfd, copy);
862 /* Build a reloc pointing to this constructor. */
864 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
867 bfd_set_error (bfd_error_no_memory);
870 reloc_chain->relent.sym_ptr_ptr =
871 bfd_get_section (asym)->symbol_ptr_ptr;
872 reloc_chain->relent.address = section->_raw_size;
873 reloc_chain->relent.addend = asym->value;
874 reloc_chain->relent.howto =
875 ecoff_backend (abfd)->constructor_reloc;
877 /* Set up the constructor section to hold the reloc. */
878 section->flags = SEC_CONSTRUCTOR;
879 ++section->reloc_count;
881 /* Constructor sections must be rounded to a boundary
882 based on the bitsize. These are not real sections--
883 they are handled specially by the linker--so the ECOFF
884 16 byte alignment restriction does not apply. */
885 bitsize = ecoff_backend (abfd)->constructor_bitsize;
886 section->alignment_power = 1;
887 while ((1 << section->alignment_power) < bitsize / 8)
888 ++section->alignment_power;
890 reloc_chain->next = section->constructor_chain;
891 section->constructor_chain = reloc_chain;
892 section->_raw_size += bitsize / 8;
894 /* Mark the symbol as a constructor. */
895 asym->flags |= BSF_CONSTRUCTOR;
903 /* Read an ECOFF symbol table. */
906 _bfd_ecoff_slurp_symbol_table (abfd)
909 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
910 const bfd_size_type external_ext_size
911 = backend->debug_swap.external_ext_size;
912 const bfd_size_type external_sym_size
913 = backend->debug_swap.external_sym_size;
914 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
915 = backend->debug_swap.swap_ext_in;
916 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
917 = backend->debug_swap.swap_sym_in;
918 bfd_size_type internal_size;
919 ecoff_symbol_type *internal;
920 ecoff_symbol_type *internal_ptr;
926 /* If we've already read in the symbol table, do nothing. */
927 if (ecoff_data (abfd)->canonical_symbols != NULL)
930 /* Get the symbolic information. */
931 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
932 &ecoff_data (abfd)->debug_info))
934 if (bfd_get_symcount (abfd) == 0)
937 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
938 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
939 if (internal == NULL)
941 bfd_set_error (bfd_error_no_memory);
945 internal_ptr = internal;
946 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
948 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
949 * external_ext_size));
950 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
954 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
955 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
956 + internal_esym.asym.iss);
957 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
958 &internal_ptr->symbol, 1))
960 /* The alpha uses a negative ifd field for section symbols. */
961 if (internal_esym.ifd >= 0)
962 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
963 + internal_esym.ifd);
965 internal_ptr->fdr = NULL;
966 internal_ptr->local = false;
967 internal_ptr->native = (PTR) eraw_src;
970 /* The local symbols must be accessed via the fdr's, because the
971 string and aux indices are relative to the fdr information. */
972 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
973 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
974 for (; fdr_ptr < fdr_end; fdr_ptr++)
979 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
980 + fdr_ptr->isymBase * external_sym_size);
981 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
984 lraw_src += external_sym_size, internal_ptr++)
988 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
989 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
992 if (!ecoff_set_symbol_info (abfd, &internal_sym,
993 &internal_ptr->symbol, 0))
995 internal_ptr->fdr = fdr_ptr;
996 internal_ptr->local = true;
997 internal_ptr->native = (PTR) lraw_src;
1001 ecoff_data (abfd)->canonical_symbols = internal;
1006 /* Return the amount of space needed for the canonical symbols. */
1009 _bfd_ecoff_get_symtab_upper_bound (abfd)
1012 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
1013 &ecoff_data (abfd)->debug_info))
1016 if (bfd_get_symcount (abfd) == 0)
1019 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1022 /* Get the canonical symbols. */
1025 _bfd_ecoff_get_symtab (abfd, alocation)
1027 asymbol **alocation;
1029 unsigned int counter = 0;
1030 ecoff_symbol_type *symbase;
1031 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1033 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1035 if (bfd_get_symcount (abfd) == 0)
1038 symbase = ecoff_data (abfd)->canonical_symbols;
1039 while (counter < bfd_get_symcount (abfd))
1041 *(location++) = symbase++;
1044 *location++ = (ecoff_symbol_type *) NULL;
1045 return bfd_get_symcount (abfd);
1048 /* Turn ECOFF type information into a printable string.
1049 ecoff_emit_aggregate and ecoff_type_to_string are from
1050 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1052 /* Write aggregate information to a string. */
1055 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1063 const struct ecoff_debug_swap * const debug_swap =
1064 &ecoff_backend (abfd)->debug_swap;
1065 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1066 unsigned int ifd = rndx->rfd;
1067 unsigned int indx = rndx->index;
1073 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1074 struct return type of a procedure compiled without -g. */
1075 if (ifd == 0xffffffff
1076 || (rndx->rfd == 0xfff && indx == 0))
1077 name = "<undefined>";
1078 else if (indx == indexNil)
1084 if (debug_info->external_rfd == NULL)
1085 fdr = debug_info->fdr + ifd;
1090 (*debug_swap->swap_rfd_in) (abfd,
1091 ((char *) debug_info->external_rfd
1092 + ((fdr->rfdBase + ifd)
1093 * debug_swap->external_rfd_size)),
1095 fdr = debug_info->fdr + rfd;
1098 indx += fdr->isymBase;
1100 (*debug_swap->swap_sym_in) (abfd,
1101 ((char *) debug_info->external_sym
1102 + indx * debug_swap->external_sym_size),
1105 name = debug_info->ss + fdr->issBase + sym.iss;
1109 "%s %s { ifd = %u, index = %lu }",
1112 + debug_info->symbolic_header.iextMax));
1115 /* Convert the type information to string format. */
1118 ecoff_type_to_string (abfd, fdr, indx)
1123 union aux_ext *aux_ptr;
1132 unsigned int basic_type;
1135 static char buffer2[1024];
1140 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1141 bigendian = fdr->fBigendian;
1143 for (i = 0; i < 7; i++)
1145 qualifiers[i].low_bound = 0;
1146 qualifiers[i].high_bound = 0;
1147 qualifiers[i].stride = 0;
1150 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1151 return "-1 (no type)";
1152 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1154 basic_type = u.ti.bt;
1155 qualifiers[0].type = u.ti.tq0;
1156 qualifiers[1].type = u.ti.tq1;
1157 qualifiers[2].type = u.ti.tq2;
1158 qualifiers[3].type = u.ti.tq3;
1159 qualifiers[4].type = u.ti.tq4;
1160 qualifiers[5].type = u.ti.tq5;
1161 qualifiers[6].type = tqNil;
1164 * Go get the basic type.
1168 case btNil: /* undefined */
1172 case btAdr: /* address - integer same size as pointer */
1173 strcpy (p1, "address");
1176 case btChar: /* character */
1177 strcpy (p1, "char");
1180 case btUChar: /* unsigned character */
1181 strcpy (p1, "unsigned char");
1184 case btShort: /* short */
1185 strcpy (p1, "short");
1188 case btUShort: /* unsigned short */
1189 strcpy (p1, "unsigned short");
1192 case btInt: /* int */
1196 case btUInt: /* unsigned int */
1197 strcpy (p1, "unsigned int");
1200 case btLong: /* long */
1201 strcpy (p1, "long");
1204 case btULong: /* unsigned long */
1205 strcpy (p1, "unsigned long");
1208 case btFloat: /* float (real) */
1209 strcpy (p1, "float");
1212 case btDouble: /* Double (real) */
1213 strcpy (p1, "double");
1216 /* Structures add 1-2 aux words:
1217 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1218 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1220 case btStruct: /* Structure (Record) */
1221 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1222 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1223 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1225 indx++; /* skip aux words */
1228 /* Unions add 1-2 aux words:
1229 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1230 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1232 case btUnion: /* Union */
1233 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1234 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1235 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1237 indx++; /* skip aux words */
1240 /* Enumerations add 1-2 aux words:
1241 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1242 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1244 case btEnum: /* Enumeration */
1245 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1246 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1247 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1249 indx++; /* skip aux words */
1252 case btTypedef: /* defined via a typedef, isymRef points */
1253 strcpy (p1, "typedef");
1256 case btRange: /* subrange of int */
1257 strcpy (p1, "subrange");
1260 case btSet: /* pascal sets */
1264 case btComplex: /* fortran complex */
1265 strcpy (p1, "complex");
1268 case btDComplex: /* fortran double complex */
1269 strcpy (p1, "double complex");
1272 case btIndirect: /* forward or unnamed typedef */
1273 strcpy (p1, "forward/unamed typedef");
1276 case btFixedDec: /* Fixed Decimal */
1277 strcpy (p1, "fixed decimal");
1280 case btFloatDec: /* Float Decimal */
1281 strcpy (p1, "float decimal");
1284 case btString: /* Varying Length Character String */
1285 strcpy (p1, "string");
1288 case btBit: /* Aligned Bit String */
1292 case btPicture: /* Picture */
1293 strcpy (p1, "picture");
1296 case btVoid: /* Void */
1297 strcpy (p1, "void");
1301 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1305 p1 += strlen (buffer1);
1308 * If this is a bitfield, get the bitsize.
1314 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1315 sprintf (p1, " : %d", bitsize);
1316 p1 += strlen (buffer1);
1321 * Deal with any qualifiers.
1323 if (qualifiers[0].type != tqNil)
1326 * Snarf up any array bounds in the correct order. Arrays
1327 * store 5 successive words in the aux. table:
1328 * word 0 RNDXR to type of the bounds (ie, int)
1329 * word 1 Current file descriptor index
1331 * word 3 high bound (or -1 if [])
1332 * word 4 stride size in bits
1334 for (i = 0; i < 7; i++)
1336 if (qualifiers[i].type == tqArray)
1338 qualifiers[i].low_bound =
1339 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1340 qualifiers[i].high_bound =
1341 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1342 qualifiers[i].stride =
1343 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1349 * Now print out the qualifiers.
1351 for (i = 0; i < 6; i++)
1353 switch (qualifiers[i].type)
1360 strcpy (p2, "ptr to ");
1361 p2 += sizeof ("ptr to ")-1;
1365 strcpy (p2, "volatile ");
1366 p2 += sizeof ("volatile ")-1;
1370 strcpy (p2, "far ");
1371 p2 += sizeof ("far ")-1;
1375 strcpy (p2, "func. ret. ");
1376 p2 += sizeof ("func. ret. ");
1381 int first_array = i;
1384 /* Print array bounds reversed (ie, in the order the C
1385 programmer writes them). C is such a fun language.... */
1387 while (i < 5 && qualifiers[i+1].type == tqArray)
1390 for (j = i; j >= first_array; j--)
1392 strcpy (p2, "array [");
1393 p2 += sizeof ("array [")-1;
1394 if (qualifiers[j].low_bound != 0)
1396 "%ld:%ld {%ld bits}",
1397 (long) qualifiers[j].low_bound,
1398 (long) qualifiers[j].high_bound,
1399 (long) qualifiers[j].stride);
1401 else if (qualifiers[j].high_bound != -1)
1404 (long) (qualifiers[j].high_bound + 1),
1405 (long) (qualifiers[j].stride));
1408 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1411 strcpy (p2, "] of ");
1412 p2 += sizeof ("] of ")-1;
1420 strcpy (p2, buffer1);
1424 /* Return information about ECOFF symbol SYMBOL in RET. */
1428 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1429 bfd *abfd; /* Ignored. */
1433 bfd_symbol_info (symbol, ret);
1436 /* Return whether this is a local label. */
1440 _bfd_ecoff_bfd_is_local_label (abfd, symbol)
1444 return symbol->name[0] == '$';
1447 /* Print information about an ECOFF symbol. */
1450 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1454 bfd_print_symbol_type how;
1456 const struct ecoff_debug_swap * const debug_swap
1457 = &ecoff_backend (abfd)->debug_swap;
1458 FILE *file = (FILE *)filep;
1462 case bfd_print_symbol_name:
1463 fprintf (file, "%s", symbol->name);
1465 case bfd_print_symbol_more:
1466 if (ecoffsymbol (symbol)->local)
1470 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1472 fprintf (file, "ecoff local ");
1473 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1474 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1475 (unsigned) ecoff_sym.sc);
1481 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1483 fprintf (file, "ecoff extern ");
1484 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1485 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1486 (unsigned) ecoff_ext.asym.sc);
1489 case bfd_print_symbol_all:
1490 /* Print out the symbols in a reasonable way */
1499 if (ecoffsymbol (symbol)->local)
1501 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1504 pos = ((((char *) ecoffsymbol (symbol)->native
1505 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1506 / debug_swap->external_sym_size)
1507 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1514 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1517 pos = (((char *) ecoffsymbol (symbol)->native
1518 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1519 / debug_swap->external_ext_size);
1520 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1521 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1522 weakext = ecoff_ext.weakext ? 'w' : ' ';
1525 fprintf (file, "[%3d] %c ",
1527 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1528 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1529 (unsigned) ecoff_ext.asym.st,
1530 (unsigned) ecoff_ext.asym.sc,
1531 (unsigned) ecoff_ext.asym.index,
1532 jmptbl, cobol_main, weakext,
1535 if (ecoffsymbol (symbol)->fdr != NULL
1536 && ecoff_ext.asym.index != indexNil)
1541 bfd_size_type sym_base;
1542 union aux_ext *aux_base;
1544 fdr = ecoffsymbol (symbol)->fdr;
1545 indx = ecoff_ext.asym.index;
1547 /* sym_base is used to map the fdr relative indices which
1548 appear in the file to the position number which we are
1550 sym_base = fdr->isymBase;
1551 if (ecoffsymbol (symbol)->local)
1553 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1555 /* aux_base is the start of the aux entries for this file;
1556 asym.index is an offset from this. */
1557 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1560 /* The aux entries are stored in host byte order; the
1561 order is indicated by a bit in the fdr. */
1562 bigendian = fdr->fBigendian;
1564 /* This switch is basically from gcc/mips-tdump.c */
1565 switch (ecoff_ext.asym.st)
1573 fprintf (file, "\n End+1 symbol: %ld",
1574 (long) (indx + sym_base));
1578 if (ecoff_ext.asym.sc == scText
1579 || ecoff_ext.asym.sc == scInfo)
1580 fprintf (file, "\n First symbol: %ld",
1581 (long) (indx + sym_base));
1583 fprintf (file, "\n First symbol: %ld",
1585 (AUX_GET_ISYM (bigendian,
1586 &aux_base[ecoff_ext.asym.index])
1592 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1594 else if (ecoffsymbol (symbol)->local)
1595 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1597 (AUX_GET_ISYM (bigendian,
1598 &aux_base[ecoff_ext.asym.index])
1600 ecoff_type_to_string (abfd, fdr, indx + 1));
1602 fprintf (file, "\n Local symbol: %ld",
1605 + (ecoff_data (abfd)
1606 ->debug_info.symbolic_header.iextMax)));
1610 fprintf (file, "\n struct; End+1 symbol: %ld",
1611 (long) (indx + sym_base));
1615 fprintf (file, "\n union; End+1 symbol: %ld",
1616 (long) (indx + sym_base));
1620 fprintf (file, "\n enum; End+1 symbol: %ld",
1621 (long) (indx + sym_base));
1625 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1626 fprintf (file, "\n Type: %s",
1627 ecoff_type_to_string (abfd, fdr, indx));
1636 /* Read in the relocs for a section. */
1639 ecoff_slurp_reloc_table (abfd, section, symbols)
1644 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1645 arelent *internal_relocs;
1646 bfd_size_type external_reloc_size;
1647 bfd_size_type external_relocs_size;
1648 char *external_relocs;
1652 if (section->relocation != (arelent *) NULL
1653 || section->reloc_count == 0
1654 || (section->flags & SEC_CONSTRUCTOR) != 0)
1657 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1660 internal_relocs = (arelent *) bfd_alloc (abfd,
1662 * section->reloc_count));
1663 external_reloc_size = backend->external_reloc_size;
1664 external_relocs_size = external_reloc_size * section->reloc_count;
1665 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1666 if (internal_relocs == (arelent *) NULL
1667 || external_relocs == (char *) NULL)
1669 bfd_set_error (bfd_error_no_memory);
1672 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1674 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1675 != external_relocs_size)
1678 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1680 struct internal_reloc intern;
1682 (*backend->swap_reloc_in) (abfd,
1683 external_relocs + i * external_reloc_size,
1686 if (intern.r_extern)
1688 /* r_symndx is an index into the external symbols. */
1689 BFD_ASSERT (intern.r_symndx >= 0
1691 < (ecoff_data (abfd)
1692 ->debug_info.symbolic_header.iextMax)));
1693 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1696 else if (intern.r_symndx == RELOC_SECTION_NONE
1697 || intern.r_symndx == RELOC_SECTION_ABS)
1699 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1704 CONST char *sec_name;
1707 /* r_symndx is a section key. */
1708 switch (intern.r_symndx)
1710 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1711 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1712 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1713 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1714 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1715 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1716 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1717 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1718 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1719 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1720 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1721 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1722 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1726 sec = bfd_get_section_by_name (abfd, sec_name);
1727 if (sec == (asection *) NULL)
1729 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1731 rptr->addend = - bfd_get_section_vma (abfd, sec);
1734 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1736 /* Let the backend select the howto field and do any other
1737 required processing. */
1738 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1741 bfd_release (abfd, external_relocs);
1743 section->relocation = internal_relocs;
1748 /* Get a canonical list of relocs. */
1751 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1759 if (section->flags & SEC_CONSTRUCTOR)
1761 arelent_chain *chain;
1763 /* This section has relocs made up by us, not the file, so take
1764 them out of their chain and place them into the data area
1766 for (count = 0, chain = section->constructor_chain;
1767 count < section->reloc_count;
1768 count++, chain = chain->next)
1769 *relptr++ = &chain->relent;
1775 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1778 tblptr = section->relocation;
1780 for (count = 0; count < section->reloc_count; count++)
1781 *relptr++ = tblptr++;
1784 *relptr = (arelent *) NULL;
1786 return section->reloc_count;
1789 /* Provided a BFD, a section and an offset into the section, calculate
1790 and return the name of the source file and the line nearest to the
1795 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1796 filename_ptr, functionname_ptr, retline_ptr)
1799 asymbol **ignore_symbols;
1801 CONST char **filename_ptr;
1802 CONST char **functionname_ptr;
1803 unsigned int *retline_ptr;
1805 const struct ecoff_debug_swap * const debug_swap
1806 = &ecoff_backend (abfd)->debug_swap;
1807 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1808 struct ecoff_find_line *line_info;
1810 /* If we're not in the .text section, we don't have any line
1812 if (strcmp (section->name, _TEXT) != 0
1813 || offset < ecoff_data (abfd)->text_start
1814 || offset >= ecoff_data (abfd)->text_end)
1817 /* Make sure we have the FDR's. */
1818 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1819 || bfd_get_symcount (abfd) == 0)
1822 if (ecoff_data (abfd)->find_line_info == NULL)
1824 ecoff_data (abfd)->find_line_info =
1825 ((struct ecoff_find_line *)
1826 bfd_alloc (abfd, sizeof (struct ecoff_find_line)));
1827 if (ecoff_data (abfd)->find_line_info == NULL)
1829 bfd_set_error (bfd_error_no_memory);
1832 ecoff_data (abfd)->find_line_info->find_buffer = NULL;
1833 ecoff_data (abfd)->find_line_info->fdrtab_len = 0;
1834 ecoff_data (abfd)->find_line_info->fdrtab = NULL;
1836 line_info = ecoff_data (abfd)->find_line_info;
1838 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1839 debug_swap, line_info, filename_ptr,
1840 functionname_ptr, retline_ptr);
1843 /* Copy private BFD data. This is called by objcopy and strip. We
1844 use it to copy the ECOFF debugging information from one BFD to the
1845 other. It would be theoretically possible to represent the ECOFF
1846 debugging information in the symbol table. However, it would be a
1847 lot of work, and there would be little gain (gas, gdb, and ld
1848 already access the ECOFF debugging information via the
1849 ecoff_debug_info structure, and that structure would have to be
1850 retained in order to support ECOFF debugging in MIPS ELF).
1852 The debugging information for the ECOFF external symbols comes from
1853 the symbol table, so this function only handles the other debugging
1857 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1861 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1862 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1864 asymbol **sym_ptr_ptr;
1868 /* This function is selected based on the input vector. We only
1869 want to copy information over if the output BFD also uses ECOFF
1871 if (bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1874 /* Copy the GP value and the register masks. */
1875 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1876 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1877 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1878 for (i = 0; i < 3; i++)
1879 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1881 /* Copy the version stamp. */
1882 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1884 /* If there are no symbols, don't copy any debugging information. */
1885 c = bfd_get_symcount (obfd);
1886 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1887 if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1890 /* See if there are any local symbols. */
1892 for (; c > 0; c--, sym_ptr_ptr++)
1894 if (ecoffsymbol (*sym_ptr_ptr)->local)
1903 /* There are some local symbols. We just bring over all the
1904 debugging information. FIXME: This is not quite the right
1905 thing to do. If the user has asked us to discard all
1906 debugging information, then we are probably going to wind up
1907 keeping it because there will probably be some local symbol
1908 which objcopy did not discard. We should actually break
1909 apart the debugging information and only keep that which
1910 applies to the symbols we want to keep. */
1911 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1912 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1913 oinfo->line = iinfo->line;
1915 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1916 oinfo->external_dnr = iinfo->external_dnr;
1918 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1919 oinfo->external_pdr = iinfo->external_pdr;
1921 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1922 oinfo->external_sym = iinfo->external_sym;
1924 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1925 oinfo->external_opt = iinfo->external_opt;
1927 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1928 oinfo->external_aux = iinfo->external_aux;
1930 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1931 oinfo->ss = iinfo->ss;
1933 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1934 oinfo->external_fdr = iinfo->external_fdr;
1936 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1937 oinfo->external_rfd = iinfo->external_rfd;
1941 /* We are discarding all the local symbol information. Look
1942 through the external symbols and remove all references to FDR
1943 or aux information. */
1944 c = bfd_get_symcount (obfd);
1945 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1946 for (; c > 0; c--, sym_ptr_ptr++)
1950 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1951 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1953 esym.asym.index = indexNil;
1954 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1955 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1962 /* Set the architecture. The supported architecture is stored in the
1963 backend pointer. We always set the architecture anyhow, since many
1964 callers ignore the return value. */
1967 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1969 enum bfd_architecture arch;
1970 unsigned long machine;
1972 bfd_default_set_arch_mach (abfd, arch, machine);
1973 return arch == ecoff_backend (abfd)->arch;
1976 /* Get the size of the section headers. */
1980 _bfd_ecoff_sizeof_headers (abfd, reloc)
1989 for (current = abfd->sections;
1990 current != (asection *)NULL;
1991 current = current->next)
1994 ret = (bfd_coff_filhsz (abfd)
1995 + bfd_coff_aoutsz (abfd)
1996 + c * bfd_coff_scnhsz (abfd));
1997 return BFD_ALIGN (ret, 16);
2000 /* Get the contents of a section. */
2003 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
2008 bfd_size_type count;
2010 return _bfd_generic_get_section_contents (abfd, section, location,
2014 /* Calculate the file position for each section, and set
2018 ecoff_compute_section_file_positions (abfd)
2026 sofar = _bfd_ecoff_sizeof_headers (abfd, false);
2029 for (current = abfd->sections;
2030 current != (asection *) NULL;
2031 current = current->next)
2033 unsigned int alignment_power;
2035 /* Only deal with sections which have contents */
2036 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) == 0)
2039 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2040 supposed to indicate the number of .pdata entries that are
2041 really in the section. Each entry is 8 bytes. We store this
2042 away in line_filepos before increasing the section size. */
2043 if (strcmp (current->name, _PDATA) != 0)
2044 alignment_power = current->alignment_power;
2047 current->line_filepos = current->_raw_size / 8;
2048 alignment_power = 4;
2051 /* On Ultrix, the data sections in an executable file must be
2052 aligned to a page boundary within the file. This does not
2053 affect the section size, though. FIXME: Does this work for
2054 other platforms? It requires some modification for the
2055 Alpha, because .rdata on the Alpha goes with the text, not
2057 if ((abfd->flags & EXEC_P) != 0
2058 && (abfd->flags & D_PAGED) != 0
2059 && first_data != false
2060 && (current->flags & SEC_CODE) == 0
2061 && (! ecoff_backend (abfd)->rdata_in_text
2062 || strcmp (current->name, _RDATA) != 0)
2063 && strcmp (current->name, _PDATA) != 0)
2065 const bfd_vma round = ecoff_backend (abfd)->round;
2067 sofar = (sofar + round - 1) &~ (round - 1);
2070 else if (strcmp (current->name, _LIB) == 0)
2072 const bfd_vma round = ecoff_backend (abfd)->round;
2073 /* On Irix 4, the location of contents of the .lib section
2074 from a shared library section is also rounded up to a
2077 sofar = (sofar + round - 1) &~ (round - 1);
2080 /* Align the sections in the file to the same boundary on
2081 which they are aligned in virtual memory. */
2083 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2085 current->filepos = sofar;
2087 sofar += current->_raw_size;
2089 /* make sure that this section is of the right size too */
2091 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2092 current->_raw_size += sofar - old_sofar;
2095 ecoff_data (abfd)->reloc_filepos = sofar;
2098 /* Determine the location of the relocs for all the sections in the
2099 output file, as well as the location of the symbolic debugging
2102 static bfd_size_type
2103 ecoff_compute_reloc_file_positions (abfd)
2106 const bfd_size_type external_reloc_size =
2107 ecoff_backend (abfd)->external_reloc_size;
2108 file_ptr reloc_base;
2109 bfd_size_type reloc_size;
2113 if (! abfd->output_has_begun)
2115 ecoff_compute_section_file_positions (abfd);
2116 abfd->output_has_begun = true;
2119 reloc_base = ecoff_data (abfd)->reloc_filepos;
2122 for (current = abfd->sections;
2123 current != (asection *)NULL;
2124 current = current->next)
2126 if (current->reloc_count == 0)
2127 current->rel_filepos = 0;
2130 bfd_size_type relsize;
2132 current->rel_filepos = reloc_base;
2133 relsize = current->reloc_count * external_reloc_size;
2134 reloc_size += relsize;
2135 reloc_base += relsize;
2139 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2141 /* At least on Ultrix, the symbol table of an executable file must
2142 be aligned to a page boundary. FIXME: Is this true on other
2144 if ((abfd->flags & EXEC_P) != 0
2145 && (abfd->flags & D_PAGED) != 0)
2146 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2147 &~ (ecoff_backend (abfd)->round - 1));
2149 ecoff_data (abfd)->sym_filepos = sym_base;
2154 /* Set the contents of a section. */
2157 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2162 bfd_size_type count;
2164 /* This must be done first, because bfd_set_section_contents is
2165 going to set output_has_begun to true. */
2166 if (abfd->output_has_begun == false)
2167 ecoff_compute_section_file_positions (abfd);
2169 /* If this is a .lib section, bump the vma address so that it winds
2170 up being the number of .lib sections output. This is right for
2172 if (strcmp (section->name, _LIB) == 0)
2178 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2179 || bfd_write (location, 1, count, abfd) != count)
2185 /* Get the GP value for an ECOFF file. This is a hook used by
2189 bfd_ecoff_get_gp_value (abfd)
2192 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2193 || bfd_get_format (abfd) != bfd_object)
2195 bfd_set_error (bfd_error_invalid_operation);
2199 return ecoff_data (abfd)->gp;
2202 /* Set the GP value for an ECOFF file. This is a hook used by the
2206 bfd_ecoff_set_gp_value (abfd, gp_value)
2210 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2211 || bfd_get_format (abfd) != bfd_object)
2213 bfd_set_error (bfd_error_invalid_operation);
2217 ecoff_data (abfd)->gp = gp_value;
2222 /* Set the register masks for an ECOFF file. This is a hook used by
2226 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2228 unsigned long gprmask;
2229 unsigned long fprmask;
2230 unsigned long *cprmask;
2232 ecoff_data_type *tdata;
2234 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2235 || bfd_get_format (abfd) != bfd_object)
2237 bfd_set_error (bfd_error_invalid_operation);
2241 tdata = ecoff_data (abfd);
2242 tdata->gprmask = gprmask;
2243 tdata->fprmask = fprmask;
2244 if (cprmask != (unsigned long *) NULL)
2248 for (i = 0; i < 3; i++)
2249 tdata->cprmask[i] = cprmask[i];
2255 /* Get ECOFF EXTR information for an external symbol. This function
2256 is passed to bfd_ecoff_debug_externals. */
2259 ecoff_get_extr (sym, esym)
2263 ecoff_symbol_type *ecoff_sym_ptr;
2266 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2267 || ecoffsymbol (sym)->native == NULL)
2269 /* Don't include debugging, local, or section symbols. */
2270 if ((sym->flags & BSF_DEBUGGING) != 0
2271 || (sym->flags & BSF_LOCAL) != 0
2272 || (sym->flags & BSF_SECTION_SYM) != 0)
2276 esym->cobol_main = 0;
2280 /* FIXME: we can do better than this for st and sc. */
2281 esym->asym.st = stGlobal;
2282 esym->asym.sc = scAbs;
2283 esym->asym.reserved = 0;
2284 esym->asym.index = indexNil;
2288 ecoff_sym_ptr = ecoffsymbol (sym);
2290 if (ecoff_sym_ptr->local)
2293 input_bfd = bfd_asymbol_bfd (sym);
2294 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2295 (input_bfd, ecoff_sym_ptr->native, esym);
2297 /* If the symbol was defined by the linker, then esym will be
2298 undefined but sym will not be. Get a better class for such a
2300 if ((esym->asym.sc == scUndefined
2301 || esym->asym.sc == scSUndefined)
2302 && ! bfd_is_und_section (bfd_get_section (sym)))
2303 esym->asym.sc = scAbs;
2305 /* Adjust the FDR index for the symbol by that used for the input
2307 if (esym->ifd != -1)
2309 struct ecoff_debug_info *input_debug;
2311 input_debug = &ecoff_data (input_bfd)->debug_info;
2312 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2313 if (input_debug->ifdmap != (RFDT *) NULL)
2314 esym->ifd = input_debug->ifdmap[esym->ifd];
2320 /* Set the external symbol index. This routine is passed to
2321 bfd_ecoff_debug_externals. */
2324 ecoff_set_index (sym, indx)
2328 ecoff_set_sym_index (sym, indx);
2331 /* Write out an ECOFF file. */
2334 _bfd_ecoff_write_object_contents (abfd)
2337 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2338 const bfd_vma round = backend->round;
2339 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2340 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2341 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2342 const bfd_size_type external_hdr_size
2343 = backend->debug_swap.external_hdr_size;
2344 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2345 void (* const adjust_reloc_out) PARAMS ((bfd *,
2347 struct internal_reloc *))
2348 = backend->adjust_reloc_out;
2349 void (* const swap_reloc_out) PARAMS ((bfd *,
2350 const struct internal_reloc *,
2352 = backend->swap_reloc_out;
2353 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2354 HDRR * const symhdr = &debug->symbolic_header;
2357 bfd_size_type reloc_size;
2358 bfd_size_type text_size;
2360 boolean set_text_start;
2361 bfd_size_type data_size;
2363 boolean set_data_start;
2364 bfd_size_type bss_size;
2366 PTR reloc_buff = NULL;
2367 struct internal_filehdr internal_f;
2368 struct internal_aouthdr internal_a;
2371 /* Determine where the sections and relocs will go in the output
2373 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2376 for (current = abfd->sections;
2377 current != (asection *)NULL;
2378 current = current->next)
2380 current->target_index = count;
2384 if ((abfd->flags & D_PAGED) != 0)
2385 text_size = _bfd_ecoff_sizeof_headers (abfd, false);
2389 set_text_start = false;
2392 set_data_start = false;
2395 /* Write section headers to the file. */
2397 /* Allocate buff big enough to hold a section header,
2398 file header, or a.out header. */
2406 buff = (PTR) malloc ((size_t) siz);
2409 bfd_set_error (bfd_error_no_memory);
2414 internal_f.f_nscns = 0;
2415 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2417 for (current = abfd->sections;
2418 current != (asection *) NULL;
2419 current = current->next)
2421 struct internal_scnhdr section;
2424 ++internal_f.f_nscns;
2426 strncpy (section.s_name, current->name, sizeof section.s_name);
2428 /* This seems to be correct for Irix 4 shared libraries. */
2429 vma = bfd_get_section_vma (abfd, current);
2430 if (strcmp (current->name, _LIB) == 0)
2431 section.s_vaddr = 0;
2433 section.s_vaddr = vma;
2435 section.s_paddr = vma;
2436 section.s_size = bfd_get_section_size_before_reloc (current);
2438 /* If this section is unloadable then the scnptr will be 0. */
2439 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2440 section.s_scnptr = 0;
2442 section.s_scnptr = current->filepos;
2443 section.s_relptr = current->rel_filepos;
2445 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2446 object file produced by the assembler is supposed to point to
2447 information about how much room is required by objects of
2448 various different sizes. I think this only matters if we
2449 want the linker to compute the best size to use, or
2450 something. I don't know what happens if the information is
2452 if (strcmp (current->name, _PDATA) != 0)
2453 section.s_lnnoptr = 0;
2456 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2457 hold the number of entries in the section (each entry is
2458 8 bytes). We stored this in the line_filepos field in
2459 ecoff_compute_section_file_positions. */
2460 section.s_lnnoptr = current->line_filepos;
2463 section.s_nreloc = current->reloc_count;
2464 section.s_nlnno = 0;
2465 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2468 if (bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff) == 0
2469 || bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2472 if ((section.s_flags & STYP_TEXT) != 0
2473 || ((section.s_flags & STYP_RDATA) != 0
2474 && backend->rdata_in_text)
2475 || strcmp (current->name, _PDATA) == 0)
2477 text_size += bfd_get_section_size_before_reloc (current);
2478 if (! set_text_start || text_start > vma)
2481 set_text_start = true;
2484 else if ((section.s_flags & STYP_RDATA) != 0
2485 || (section.s_flags & STYP_DATA) != 0
2486 || (section.s_flags & STYP_LITA) != 0
2487 || (section.s_flags & STYP_LIT8) != 0
2488 || (section.s_flags & STYP_LIT4) != 0
2489 || (section.s_flags & STYP_SDATA) != 0
2490 || strcmp (current->name, _XDATA) == 0)
2492 data_size += bfd_get_section_size_before_reloc (current);
2493 if (! set_data_start || data_start > vma)
2496 set_data_start = true;
2499 else if ((section.s_flags & STYP_BSS) != 0
2500 || (section.s_flags & STYP_SBSS) != 0)
2501 bss_size += bfd_get_section_size_before_reloc (current);
2502 else if ((section.s_flags & STYP_ECOFF_LIB) != 0)
2508 /* Set up the file header. */
2510 internal_f.f_magic = ecoff_get_magic (abfd);
2512 /* We will NOT put a fucking timestamp in the header here. Every
2513 time you put it back, I will come in and take it out again. I'm
2514 sorry. This field does not belong here. We fill it with a 0 so
2515 it compares the same but is not a reasonable time. --
2517 internal_f.f_timdat = 0;
2519 if (bfd_get_symcount (abfd) != 0)
2521 /* The ECOFF f_nsyms field is not actually the number of
2522 symbols, it's the size of symbolic information header. */
2523 internal_f.f_nsyms = external_hdr_size;
2524 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2528 internal_f.f_nsyms = 0;
2529 internal_f.f_symptr = 0;
2532 internal_f.f_opthdr = aoutsz;
2534 internal_f.f_flags = F_LNNO;
2535 if (reloc_size == 0)
2536 internal_f.f_flags |= F_RELFLG;
2537 if (bfd_get_symcount (abfd) == 0)
2538 internal_f.f_flags |= F_LSYMS;
2539 if (abfd->flags & EXEC_P)
2540 internal_f.f_flags |= F_EXEC;
2542 if (! abfd->xvec->byteorder_big_p)
2543 internal_f.f_flags |= F_AR32WR;
2545 internal_f.f_flags |= F_AR32W;
2547 /* Set up the ``optional'' header. */
2548 if ((abfd->flags & D_PAGED) != 0)
2549 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2551 internal_a.magic = ECOFF_AOUT_OMAGIC;
2553 /* FIXME: Is this really correct? */
2554 internal_a.vstamp = symhdr->vstamp;
2556 /* At least on Ultrix, these have to be rounded to page boundaries.
2557 FIXME: Is this true on other platforms? */
2558 if ((abfd->flags & D_PAGED) != 0)
2560 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2561 internal_a.text_start = text_start &~ (round - 1);
2562 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2563 internal_a.data_start = data_start &~ (round - 1);
2567 internal_a.tsize = text_size;
2568 internal_a.text_start = text_start;
2569 internal_a.dsize = data_size;
2570 internal_a.data_start = data_start;
2573 /* On Ultrix, the initial portions of the .sbss and .bss segments
2574 are at the end of the data section. The bsize field in the
2575 optional header records how many bss bytes are required beyond
2576 those in the data section. The value is not rounded to a page
2578 if (bss_size < internal_a.dsize - data_size)
2581 bss_size -= internal_a.dsize - data_size;
2582 internal_a.bsize = bss_size;
2583 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2585 internal_a.entry = bfd_get_start_address (abfd);
2587 internal_a.gp_value = ecoff_data (abfd)->gp;
2589 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2590 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2591 for (i = 0; i < 4; i++)
2592 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2594 /* Write out the file header and the optional header. */
2596 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2599 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2600 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2603 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2604 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2607 /* Build the external symbol information. This must be done before
2608 writing out the relocs so that we know the symbol indices. We
2609 don't do this if this BFD was created by the backend linker,
2610 since it will have already handled the symbols and relocs. */
2611 if (! ecoff_data (abfd)->linker)
2613 symhdr->iextMax = 0;
2614 symhdr->issExtMax = 0;
2615 debug->external_ext = debug->external_ext_end = NULL;
2616 debug->ssext = debug->ssext_end = NULL;
2617 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2618 (((abfd->flags & EXEC_P) == 0)
2620 ecoff_get_extr, ecoff_set_index)
2624 /* Write out the relocs. */
2625 for (current = abfd->sections;
2626 current != (asection *) NULL;
2627 current = current->next)
2629 arelent **reloc_ptr_ptr;
2630 arelent **reloc_end;
2633 if (current->reloc_count == 0)
2637 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2638 if (reloc_buff == NULL)
2640 bfd_set_error (bfd_error_no_memory);
2644 reloc_ptr_ptr = current->orelocation;
2645 reloc_end = reloc_ptr_ptr + current->reloc_count;
2646 out_ptr = (char *) reloc_buff;
2648 reloc_ptr_ptr < reloc_end;
2649 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2653 struct internal_reloc in;
2655 memset ((PTR) &in, 0, sizeof in);
2657 reloc = *reloc_ptr_ptr;
2658 sym = *reloc->sym_ptr_ptr;
2660 in.r_vaddr = (reloc->address
2661 + bfd_get_section_vma (abfd, current));
2662 in.r_type = reloc->howto->type;
2664 if ((sym->flags & BSF_SECTION_SYM) == 0)
2666 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2673 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2674 if (strcmp (name, ".text") == 0)
2675 in.r_symndx = RELOC_SECTION_TEXT;
2676 else if (strcmp (name, ".rdata") == 0)
2677 in.r_symndx = RELOC_SECTION_RDATA;
2678 else if (strcmp (name, ".data") == 0)
2679 in.r_symndx = RELOC_SECTION_DATA;
2680 else if (strcmp (name, ".sdata") == 0)
2681 in.r_symndx = RELOC_SECTION_SDATA;
2682 else if (strcmp (name, ".sbss") == 0)
2683 in.r_symndx = RELOC_SECTION_SBSS;
2684 else if (strcmp (name, ".bss") == 0)
2685 in.r_symndx = RELOC_SECTION_BSS;
2686 else if (strcmp (name, ".init") == 0)
2687 in.r_symndx = RELOC_SECTION_INIT;
2688 else if (strcmp (name, ".lit8") == 0)
2689 in.r_symndx = RELOC_SECTION_LIT8;
2690 else if (strcmp (name, ".lit4") == 0)
2691 in.r_symndx = RELOC_SECTION_LIT4;
2692 else if (strcmp (name, ".xdata") == 0)
2693 in.r_symndx = RELOC_SECTION_XDATA;
2694 else if (strcmp (name, ".pdata") == 0)
2695 in.r_symndx = RELOC_SECTION_PDATA;
2696 else if (strcmp (name, ".fini") == 0)
2697 in.r_symndx = RELOC_SECTION_FINI;
2698 else if (strcmp (name, ".lita") == 0)
2699 in.r_symndx = RELOC_SECTION_LITA;
2700 else if (strcmp (name, "*ABS*") == 0)
2701 in.r_symndx = RELOC_SECTION_ABS;
2707 (*adjust_reloc_out) (abfd, reloc, &in);
2709 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2712 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2714 if (bfd_write (reloc_buff,
2715 external_reloc_size, current->reloc_count, abfd)
2716 != external_reloc_size * current->reloc_count)
2718 bfd_release (abfd, reloc_buff);
2722 /* Write out the symbolic debugging information. */
2723 if (bfd_get_symcount (abfd) > 0)
2725 /* Write out the debugging information. */
2726 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2727 ecoff_data (abfd)->sym_filepos)
2733 /* The .bss section of a demand paged executable must receive an
2734 entire page. If there are symbols, the symbols will start on the
2735 next page. If there are no symbols, we must fill out the page by
2737 if (bfd_get_symcount (abfd) == 0
2738 && (abfd->flags & EXEC_P) != 0
2739 && (abfd->flags & D_PAGED) != 0)
2743 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2746 if (bfd_read (&c, 1, 1, abfd) == 0)
2748 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2751 if (bfd_write (&c, 1, 1, abfd) != 1)
2755 if (reloc_buff != NULL)
2756 bfd_release (abfd, reloc_buff);
2761 if (reloc_buff != NULL)
2762 bfd_release (abfd, reloc_buff);
2768 /* Archive handling. ECOFF uses what appears to be a unique type of
2769 archive header (armap). The byte ordering of the armap and the
2770 contents are encoded in the name of the armap itself. At least for
2771 now, we only support archives with the same byte ordering in the
2772 armap and the contents.
2774 The first four bytes in the armap are the number of symbol
2775 definitions. This is always a power of two.
2777 This is followed by the symbol definitions. Each symbol definition
2778 occupies 8 bytes. The first four bytes are the offset from the
2779 start of the armap strings to the null-terminated string naming
2780 this symbol. The second four bytes are the file offset to the
2781 archive member which defines this symbol. If the second four bytes
2782 are 0, then this is not actually a symbol definition, and it should
2785 The symbols are hashed into the armap with a closed hashing scheme.
2786 See the functions below for the details of the algorithm.
2788 After the symbol definitions comes four bytes holding the size of
2789 the string table, followed by the string table itself. */
2791 /* The name of an archive headers looks like this:
2792 __________E[BL]E[BL]_ (with a trailing space).
2793 The trailing space is changed to an X if the archive is changed to
2794 indicate that the armap is out of date.
2796 The Alpha seems to use ________64E[BL]E[BL]_. */
2798 #define ARMAP_BIG_ENDIAN 'B'
2799 #define ARMAP_LITTLE_ENDIAN 'L'
2800 #define ARMAP_MARKER 'E'
2801 #define ARMAP_START_LENGTH 10
2802 #define ARMAP_HEADER_MARKER_INDEX 10
2803 #define ARMAP_HEADER_ENDIAN_INDEX 11
2804 #define ARMAP_OBJECT_MARKER_INDEX 12
2805 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2806 #define ARMAP_END_INDEX 14
2807 #define ARMAP_END "_ "
2809 /* This is a magic number used in the hashing algorithm. */
2810 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2812 /* This returns the hash value to use for a string. It also sets
2813 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2814 is the number of entries in the hash table, and HLOG is the log
2818 ecoff_armap_hash (s, rehash, size, hlog)
2820 unsigned int *rehash;
2828 hash = ((hash >> 27) | (hash << 5)) + *s++;
2829 hash *= ARMAP_HASH_MAGIC;
2830 *rehash = (hash & (size - 1)) | 1;
2831 return hash >> (32 - hlog);
2834 /* Read in the armap. */
2837 _bfd_ecoff_slurp_armap (abfd)
2842 struct areltdata *mapdata;
2843 bfd_size_type parsed_size;
2845 struct artdata *ardata;
2848 struct symdef *symdef_ptr;
2851 /* Get the name of the first element. */
2852 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2858 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2861 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2862 standard COFF armap. We could move the ECOFF armap stuff into
2863 bfd_slurp_armap, but that seems inappropriate since no other
2864 target uses this format. Instead, we check directly for a COFF
2866 if (strncmp (nextname, "/ ", 16) == 0)
2867 return bfd_slurp_armap (abfd);
2869 /* See if the first element is an armap. */
2870 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2871 ARMAP_START_LENGTH) != 0
2872 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2873 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2874 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2875 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2876 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2877 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2878 || strncmp (nextname + ARMAP_END_INDEX,
2879 ARMAP_END, sizeof ARMAP_END - 1) != 0)
2881 bfd_has_map (abfd) = false;
2885 /* Make sure we have the right byte ordering. */
2886 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2887 ^ (abfd->xvec->header_byteorder_big_p != false))
2888 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2889 ^ (abfd->xvec->byteorder_big_p != false)))
2891 bfd_set_error (bfd_error_wrong_format);
2895 /* Read in the armap. */
2896 ardata = bfd_ardata (abfd);
2897 mapdata = _bfd_snarf_ar_hdr (abfd);
2898 if (mapdata == (struct areltdata *) NULL)
2900 parsed_size = mapdata->parsed_size;
2901 bfd_release (abfd, (PTR) mapdata);
2903 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
2904 if (raw_armap == (char *) NULL)
2906 bfd_set_error (bfd_error_no_memory);
2910 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
2912 if (bfd_get_error () != bfd_error_system_call)
2913 bfd_set_error (bfd_error_malformed_archive);
2914 bfd_release (abfd, (PTR) raw_armap);
2918 ardata->tdata = (PTR) raw_armap;
2920 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
2922 ardata->symdef_count = 0;
2923 ardata->cache = (struct ar_cache *) NULL;
2925 /* This code used to overlay the symdefs over the raw archive data,
2926 but that doesn't work on a 64 bit host. */
2928 stringbase = raw_armap + count * 8 + 8;
2930 #ifdef CHECK_ARMAP_HASH
2934 /* Double check that I have the hashing algorithm right by making
2935 sure that every symbol can be looked up successfully. */
2937 for (i = 1; i < count; i <<= 1)
2939 BFD_ASSERT (i == count);
2941 raw_ptr = raw_armap + 4;
2942 for (i = 0; i < count; i++, raw_ptr += 8)
2944 unsigned int name_offset, file_offset;
2945 unsigned int hash, rehash, srch;
2947 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
2948 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
2949 if (file_offset == 0)
2951 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
2956 /* See if we can rehash to this location. */
2957 for (srch = (hash + rehash) & (count - 1);
2958 srch != hash && srch != i;
2959 srch = (srch + rehash) & (count - 1))
2960 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
2962 BFD_ASSERT (srch == i);
2966 #endif /* CHECK_ARMAP_HASH */
2968 raw_ptr = raw_armap + 4;
2969 for (i = 0; i < count; i++, raw_ptr += 8)
2970 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
2971 ++ardata->symdef_count;
2973 symdef_ptr = ((struct symdef *)
2975 ardata->symdef_count * sizeof (struct symdef)));
2978 bfd_set_error (bfd_error_no_memory);
2982 ardata->symdefs = (carsym *) symdef_ptr;
2984 raw_ptr = raw_armap + 4;
2985 for (i = 0; i < count; i++, raw_ptr += 8)
2987 unsigned int name_offset, file_offset;
2989 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
2990 if (file_offset == 0)
2992 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
2993 symdef_ptr->s.name = stringbase + name_offset;
2994 symdef_ptr->file_offset = file_offset;
2998 ardata->first_file_filepos = bfd_tell (abfd);
2999 /* Pad to an even boundary. */
3000 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3002 bfd_has_map (abfd) = true;
3007 /* Write out an armap. */
3010 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3012 unsigned int elength;
3014 unsigned int orl_count;
3017 unsigned int hashsize, hashlog;
3018 unsigned int symdefsize;
3020 unsigned int stringsize;
3021 unsigned int mapsize;
3024 struct stat statbuf;
3027 bfd_byte *hashtable;
3031 /* Ultrix appears to use as a hash table size the least power of two
3032 greater than twice the number of entries. */
3033 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3035 hashsize = 1 << hashlog;
3037 symdefsize = hashsize * 8;
3039 stringsize = stridx + padit;
3041 /* Include 8 bytes to store symdefsize and stringsize in output. */
3042 mapsize = symdefsize + stringsize + 8;
3044 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3046 memset ((PTR) &hdr, 0, sizeof hdr);
3048 /* Work out the ECOFF armap name. */
3049 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3050 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3051 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3052 (abfd->xvec->header_byteorder_big_p
3054 : ARMAP_LITTLE_ENDIAN);
3055 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3056 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3057 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3058 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3060 /* Write the timestamp of the archive header to be just a little bit
3061 later than the timestamp of the file, otherwise the linker will
3062 complain that the index is out of date. Actually, the Ultrix
3063 linker just checks the archive name; the GNU linker may check the
3065 stat (abfd->filename, &statbuf);
3066 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3068 /* The DECstation uses zeroes for the uid, gid and mode of the
3070 hdr.ar_uid[0] = '0';
3071 hdr.ar_gid[0] = '0';
3072 hdr.ar_mode[0] = '0';
3074 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3076 hdr.ar_fmag[0] = '`';
3077 hdr.ar_fmag[1] = '\012';
3079 /* Turn all null bytes in the header into spaces. */
3080 for (i = 0; i < sizeof (struct ar_hdr); i++)
3081 if (((char *)(&hdr))[i] == '\0')
3082 (((char *)(&hdr))[i]) = ' ';
3084 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3085 != sizeof (struct ar_hdr))
3088 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3089 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3092 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3095 bfd_set_error (bfd_error_no_memory);
3099 current = abfd->archive_head;
3101 for (i = 0; i < orl_count; i++)
3103 unsigned int hash, rehash;
3105 /* Advance firstreal to the file position of this archive
3107 if (((bfd *) map[i].pos) != last_elt)
3111 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3112 firstreal += firstreal % 2;
3113 current = current->next;
3115 while (current != (bfd *) map[i].pos);
3120 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3121 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3125 /* The desired slot is already taken. */
3126 for (srch = (hash + rehash) & (hashsize - 1);
3128 srch = (srch + rehash) & (hashsize - 1))
3129 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3132 BFD_ASSERT (srch != hash);
3137 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3138 (PTR) (hashtable + hash * 8));
3139 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3140 (PTR) (hashtable + hash * 8 + 4));
3143 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3146 bfd_release (abfd, hashtable);
3148 /* Now write the strings. */
3149 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3150 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3152 for (i = 0; i < orl_count; i++)
3156 len = strlen (*map[i].name) + 1;
3157 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3161 /* The spec sez this should be a newline. But in order to be
3162 bug-compatible for DECstation ar we use a null. */
3165 if (bfd_write ("", 1, 1, abfd) != 1)
3172 /* See whether this BFD is an archive. If it is, read in the armap
3173 and the extended name table. */
3176 _bfd_ecoff_archive_p (abfd)
3179 char armag[SARMAG + 1];
3181 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3182 || strncmp (armag, ARMAG, SARMAG) != 0)
3184 if (bfd_get_error () != bfd_error_system_call)
3185 bfd_set_error (bfd_error_wrong_format);
3186 return (const bfd_target *) NULL;
3189 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3190 involves a cast, we can't do it as the left operand of
3192 abfd->tdata.aout_ar_data =
3193 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3195 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3197 bfd_set_error (bfd_error_no_memory);
3198 return (const bfd_target *) NULL;
3201 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3202 bfd_ardata (abfd)->cache = NULL;
3203 bfd_ardata (abfd)->archive_head = NULL;
3204 bfd_ardata (abfd)->symdefs = NULL;
3205 bfd_ardata (abfd)->extended_names = NULL;
3206 bfd_ardata (abfd)->tdata = NULL;
3208 if (_bfd_ecoff_slurp_armap (abfd) == false
3209 || _bfd_ecoff_slurp_extended_name_table (abfd) == false)
3211 bfd_release (abfd, bfd_ardata (abfd));
3212 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3213 return (const bfd_target *) NULL;
3219 /* ECOFF linker code. */
3221 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3222 PARAMS ((struct bfd_hash_entry *entry,
3223 struct bfd_hash_table *table,
3224 const char *string));
3225 static boolean ecoff_link_add_archive_symbols
3226 PARAMS ((bfd *, struct bfd_link_info *));
3227 static boolean ecoff_link_check_archive_element
3228 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3229 static boolean ecoff_link_add_object_symbols
3230 PARAMS ((bfd *, struct bfd_link_info *));
3231 static boolean ecoff_link_add_externals
3232 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3234 /* Routine to create an entry in an ECOFF link hash table. */
3236 static struct bfd_hash_entry *
3237 ecoff_link_hash_newfunc (entry, table, string)
3238 struct bfd_hash_entry *entry;
3239 struct bfd_hash_table *table;
3242 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3244 /* Allocate the structure if it has not already been allocated by a
3246 if (ret == (struct ecoff_link_hash_entry *) NULL)
3247 ret = ((struct ecoff_link_hash_entry *)
3248 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3249 if (ret == (struct ecoff_link_hash_entry *) NULL)
3251 bfd_set_error (bfd_error_no_memory);
3255 /* Call the allocation method of the superclass. */
3256 ret = ((struct ecoff_link_hash_entry *)
3257 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3262 /* Set local fields. */
3268 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3270 return (struct bfd_hash_entry *) ret;
3273 /* Create an ECOFF link hash table. */
3275 struct bfd_link_hash_table *
3276 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3279 struct ecoff_link_hash_table *ret;
3281 ret = ((struct ecoff_link_hash_table *)
3282 bfd_alloc (abfd, sizeof (struct ecoff_link_hash_table)));
3285 bfd_set_error (bfd_error_no_memory);
3288 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3289 ecoff_link_hash_newfunc))
3292 return (struct bfd_link_hash_table *) NULL;
3297 /* Look up an entry in an ECOFF link hash table. */
3299 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3300 ((struct ecoff_link_hash_entry *) \
3301 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3303 /* Traverse an ECOFF link hash table. */
3305 #define ecoff_link_hash_traverse(table, func, info) \
3306 (bfd_link_hash_traverse \
3308 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3311 /* Get the ECOFF link hash table from the info structure. This is
3314 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3316 /* Given an ECOFF BFD, add symbols to the global hash table as
3320 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3322 struct bfd_link_info *info;
3324 switch (bfd_get_format (abfd))
3327 return ecoff_link_add_object_symbols (abfd, info);
3329 return ecoff_link_add_archive_symbols (abfd, info);
3331 bfd_set_error (bfd_error_wrong_format);
3336 /* Add the symbols from an archive file to the global hash table.
3337 This looks through the undefined symbols, looks each one up in the
3338 archive hash table, and adds any associated object file. We do not
3339 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3340 already have a hash table, so there is no reason to construct
3344 ecoff_link_add_archive_symbols (abfd, info)
3346 struct bfd_link_info *info;
3348 const bfd_byte *raw_armap;
3349 struct bfd_link_hash_entry **pundef;
3350 unsigned int armap_count;
3351 unsigned int armap_log;
3353 const bfd_byte *hashtable;
3354 const char *stringbase;
3356 if (! bfd_has_map (abfd))
3358 /* An empty archive is a special case. */
3359 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3361 bfd_set_error (bfd_error_no_armap);
3365 /* If we don't have any raw data for this archive, as can happen on
3366 Irix 4.0.5F, we call the generic routine.
3367 FIXME: We should be more clever about this, since someday tdata
3368 may get to something for a generic archive. */
3369 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3370 if (raw_armap == (bfd_byte *) NULL)
3371 return (_bfd_generic_link_add_archive_symbols
3372 (abfd, info, ecoff_link_check_archive_element));
3374 armap_count = bfd_h_get_32 (abfd, raw_armap);
3377 for (i = 1; i < armap_count; i <<= 1)
3379 BFD_ASSERT (i == armap_count);
3381 hashtable = raw_armap + 4;
3382 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3384 /* Look through the list of undefined symbols. */
3385 pundef = &info->hash->undefs;
3386 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3388 struct bfd_link_hash_entry *h;
3389 unsigned int hash, rehash;
3390 unsigned int file_offset;
3396 /* When a symbol is defined, it is not necessarily removed from
3398 if (h->type != bfd_link_hash_undefined
3399 && h->type != bfd_link_hash_common)
3401 /* Remove this entry from the list, for general cleanliness
3402 and because we are going to look through the list again
3403 if we search any more libraries. We can't remove the
3404 entry if it is the tail, because that would lose any
3405 entries we add to the list later on. */
3406 if (*pundef != info->hash->undefs_tail)
3407 *pundef = (*pundef)->next;
3409 pundef = &(*pundef)->next;
3413 /* Native ECOFF linkers do not pull in archive elements merely
3414 to satisfy common definitions, so neither do we. We leave
3415 them on the list, though, in case we are linking against some
3416 other object format. */
3417 if (h->type != bfd_link_hash_undefined)
3419 pundef = &(*pundef)->next;
3423 /* Look for this symbol in the archive hash table. */
3424 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3427 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3428 if (file_offset == 0)
3430 /* Nothing in this slot. */
3431 pundef = &(*pundef)->next;
3435 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3436 if (name[0] != h->root.string[0]
3437 || strcmp (name, h->root.string) != 0)
3442 /* That was the wrong symbol. Try rehashing. */
3444 for (srch = (hash + rehash) & (armap_count - 1);
3446 srch = (srch + rehash) & (armap_count - 1))
3448 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3449 if (file_offset == 0)
3451 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3452 if (name[0] == h->root.string[0]
3453 && strcmp (name, h->root.string) == 0)
3462 pundef = &(*pundef)->next;
3469 element = _bfd_get_elt_at_filepos (abfd, file_offset);
3470 if (element == (bfd *) NULL)
3473 if (! bfd_check_format (element, bfd_object))
3476 /* Unlike the generic linker, we know that this element provides
3477 a definition for an undefined symbol and we know that we want
3478 to include it. We don't need to check anything. */
3479 if (! (*info->callbacks->add_archive_element) (info, element, name))
3481 if (! ecoff_link_add_object_symbols (element, info))
3484 pundef = &(*pundef)->next;
3490 /* This is called if we used _bfd_generic_link_add_archive_symbols
3491 because we were not dealing with an ECOFF archive. */
3494 ecoff_link_check_archive_element (abfd, info, pneeded)
3496 struct bfd_link_info *info;
3499 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3500 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3501 = backend->debug_swap.swap_ext_in;
3503 bfd_size_type external_ext_size;
3504 PTR external_ext = NULL;
3512 if (! ecoff_slurp_symbolic_header (abfd))
3515 /* If there are no symbols, we don't want it. */
3516 if (bfd_get_symcount (abfd) == 0)
3517 goto successful_return;
3519 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3521 /* Read in the external symbols and external strings. */
3522 external_ext_size = backend->debug_swap.external_ext_size;
3523 esize = symhdr->iextMax * external_ext_size;
3524 external_ext = (PTR) malloc (esize);
3525 if (external_ext == NULL && esize != 0)
3527 bfd_set_error (bfd_error_no_memory);
3531 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3532 || bfd_read (external_ext, 1, esize, abfd) != esize)
3535 ssext = (char *) malloc (symhdr->issExtMax);
3536 if (ssext == NULL && symhdr->issExtMax != 0)
3538 bfd_set_error (bfd_error_no_memory);
3542 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3543 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd) !=
3544 (bfd_size_type) symhdr->issExtMax))
3547 /* Look through the external symbols to see if they define some
3548 symbol that is currently undefined. */
3549 ext_ptr = (char *) external_ext;
3550 ext_end = ext_ptr + esize;
3551 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3556 struct bfd_link_hash_entry *h;
3558 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3560 /* See if this symbol defines something. */
3561 if (esym.asym.st != stGlobal
3562 && esym.asym.st != stLabel
3563 && esym.asym.st != stProc)
3566 switch (esym.asym.sc)
3589 name = ssext + esym.asym.iss;
3590 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3592 /* Unlike the generic linker, we do not pull in elements because
3593 of common symbols. */
3594 if (h == (struct bfd_link_hash_entry *) NULL
3595 || h->type != bfd_link_hash_undefined)
3598 /* Include this element. */
3599 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3601 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3605 goto successful_return;
3609 if (external_ext != NULL)
3610 free (external_ext);
3615 if (external_ext != NULL)
3616 free (external_ext);
3622 /* Add symbols from an ECOFF object file to the global linker hash
3626 ecoff_link_add_object_symbols (abfd, info)
3628 struct bfd_link_info *info;
3631 bfd_size_type external_ext_size;
3632 PTR external_ext = NULL;
3637 if (! ecoff_slurp_symbolic_header (abfd))
3640 /* If there are no symbols, we don't want it. */
3641 if (bfd_get_symcount (abfd) == 0)
3644 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3646 /* Read in the external symbols and external strings. */
3647 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3648 esize = symhdr->iextMax * external_ext_size;
3649 external_ext = (PTR) malloc (esize);
3650 if (external_ext == NULL && esize != 0)
3652 bfd_set_error (bfd_error_no_memory);
3656 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3657 || bfd_read (external_ext, 1, esize, abfd) != esize)
3660 ssext = (char *) malloc (symhdr->issExtMax);
3661 if (ssext == NULL && symhdr->issExtMax != 0)
3663 bfd_set_error (bfd_error_no_memory);
3667 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3668 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd)
3669 != (bfd_size_type) symhdr->issExtMax))
3672 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3676 if (external_ext != NULL)
3677 free (external_ext);
3683 if (external_ext != NULL)
3684 free (external_ext);
3688 /* Add the external symbols of an object file to the global linker
3689 hash table. The external symbols and strings we are passed are
3690 just allocated on the stack, and will be discarded. We must
3691 explicitly save any information we may need later on in the link.
3692 We do not want to read the external symbol information again. */
3695 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3697 struct bfd_link_info *info;
3701 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3702 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3703 = backend->debug_swap.swap_ext_in;
3704 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3705 unsigned long ext_count;
3706 struct ecoff_link_hash_entry **sym_hash;
3710 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3712 sym_hash = ((struct ecoff_link_hash_entry **)
3714 ext_count * sizeof (struct bfd_link_hash_entry *)));
3717 bfd_set_error (bfd_error_no_memory);
3720 ecoff_data (abfd)->sym_hashes = sym_hash;
3722 ext_ptr = (char *) external_ext;
3723 ext_end = ext_ptr + ext_count * external_ext_size;
3724 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3731 struct ecoff_link_hash_entry *h;
3735 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3737 /* Skip debugging symbols. */
3739 switch (esym.asym.st)
3755 /* Get the information for this symbol. */
3756 value = esym.asym.value;
3757 switch (esym.asym.sc)
3777 section = bfd_make_section_old_way (abfd, ".text");
3778 value -= section->vma;
3781 section = bfd_make_section_old_way (abfd, ".data");
3782 value -= section->vma;
3785 section = bfd_make_section_old_way (abfd, ".bss");
3786 value -= section->vma;
3789 section = bfd_abs_section_ptr;
3792 section = bfd_und_section_ptr;
3795 section = bfd_make_section_old_way (abfd, ".sdata");
3796 value -= section->vma;
3799 section = bfd_make_section_old_way (abfd, ".sbss");
3800 value -= section->vma;
3803 section = bfd_make_section_old_way (abfd, ".rdata");
3804 value -= section->vma;
3807 if (value > ecoff_data (abfd)->gp_size)
3809 section = bfd_com_section_ptr;
3814 if (ecoff_scom_section.name == NULL)
3816 /* Initialize the small common section. */
3817 ecoff_scom_section.name = SCOMMON;
3818 ecoff_scom_section.flags = SEC_IS_COMMON;
3819 ecoff_scom_section.output_section = &ecoff_scom_section;
3820 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3821 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3822 ecoff_scom_symbol.name = SCOMMON;
3823 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3824 ecoff_scom_symbol.section = &ecoff_scom_section;
3825 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3827 section = &ecoff_scom_section;
3830 section = bfd_und_section_ptr;
3833 section = bfd_make_section_old_way (abfd, ".init");
3834 value -= section->vma;
3837 section = bfd_make_section_old_way (abfd, ".fini");
3838 value -= section->vma;
3842 if (section == (asection *) NULL)
3845 name = ssext + esym.asym.iss;
3848 if (! (_bfd_generic_link_add_one_symbol
3849 (info, abfd, name, BSF_GLOBAL, section, value,
3850 (const char *) NULL, true, true,
3851 (struct bfd_link_hash_entry **) &h)))
3856 /* If we are building an ECOFF hash table, save the external
3857 symbol information. */
3858 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3860 if (h->abfd == (bfd *) NULL
3861 || (! bfd_is_und_section (section)
3862 && (! bfd_is_com_section (section)
3863 || (h->root.type != bfd_link_hash_defined
3864 && h->root.type != bfd_link_hash_defweak))))
3870 /* Remember whether this symbol was small undefined. */
3871 if (esym.asym.sc == scSUndefined)
3874 /* If this symbol was ever small undefined, it needs to wind
3875 up in a GP relative section. We can't control the
3876 section of a defined symbol, but we can control the
3877 section of a common symbol. This case is actually needed
3878 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
3880 && h->root.type == bfd_link_hash_common
3881 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
3883 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3885 h->root.u.c.p->section->flags = SEC_ALLOC;
3886 if (h->esym.asym.sc == scCommon)
3887 h->esym.asym.sc = scSCommon;
3895 /* ECOFF final link routines. */
3897 static boolean ecoff_final_link_debug_accumulate
3898 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
3900 static boolean ecoff_link_write_external
3901 PARAMS ((struct ecoff_link_hash_entry *, PTR));
3902 static boolean ecoff_indirect_link_order
3903 PARAMS ((bfd *, struct bfd_link_info *, asection *,
3904 struct bfd_link_order *));
3905 static boolean ecoff_reloc_link_order
3906 PARAMS ((bfd *, struct bfd_link_info *, asection *,
3907 struct bfd_link_order *));
3909 /* ECOFF final link routine. This looks through all the input BFDs
3910 and gathers together all the debugging information, and then
3911 processes all the link order information. This may cause it to
3912 close and reopen some input BFDs; I'll see how bad this is. */
3915 _bfd_ecoff_bfd_final_link (abfd, info)
3917 struct bfd_link_info *info;
3919 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3920 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
3923 register bfd *input_bfd;
3925 struct bfd_link_order *p;
3927 /* We accumulate the debugging information counts in the symbolic
3929 symhdr = &debug->symbolic_header;
3931 symhdr->ilineMax = 0;
3935 symhdr->isymMax = 0;
3936 symhdr->ioptMax = 0;
3937 symhdr->iauxMax = 0;
3939 symhdr->issExtMax = 0;
3942 symhdr->iextMax = 0;
3944 /* We accumulate the debugging information itself in the debug_info
3947 debug->external_dnr = NULL;
3948 debug->external_pdr = NULL;
3949 debug->external_sym = NULL;
3950 debug->external_opt = NULL;
3951 debug->external_aux = NULL;
3953 debug->ssext = debug->ssext_end = NULL;
3954 debug->external_fdr = NULL;
3955 debug->external_rfd = NULL;
3956 debug->external_ext = debug->external_ext_end = NULL;
3958 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
3959 if (handle == (PTR) NULL)
3962 /* Accumulate the debugging symbols from each input BFD. */
3963 for (input_bfd = info->input_bfds;
3964 input_bfd != (bfd *) NULL;
3965 input_bfd = input_bfd->link_next)
3969 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
3971 /* Abitrarily set the symbolic header vstamp to the vstamp
3972 of the first object file in the link. */
3973 if (symhdr->vstamp == 0)
3975 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
3976 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
3980 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
3981 debug, &backend->debug_swap,
3986 /* Combine the register masks. */
3987 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
3988 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
3989 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
3990 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
3991 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
3992 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
3995 /* Write out the external symbols. */
3996 ecoff_link_hash_traverse (ecoff_hash_table (info),
3997 ecoff_link_write_external,
4000 if (info->relocateable)
4002 /* We need to make a pass over the link_orders to count up the
4003 number of relocations we will need to output, so that we know
4004 how much space they will take up. */
4005 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4008 for (p = o->link_order_head;
4009 p != (struct bfd_link_order *) NULL;
4011 if (p->type == bfd_indirect_link_order)
4012 o->reloc_count += p->u.indirect.section->reloc_count;
4013 else if (p->type == bfd_section_reloc_link_order
4014 || p->type == bfd_symbol_reloc_link_order)
4019 /* Compute the reloc and symbol file positions. */
4020 ecoff_compute_reloc_file_positions (abfd);
4022 /* Write out the debugging information. */
4023 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4024 &backend->debug_swap, info,
4025 ecoff_data (abfd)->sym_filepos))
4028 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4030 if (info->relocateable)
4032 /* Now reset the reloc_count field of the sections in the output
4033 BFD to 0, so that we can use them to keep track of how many
4034 relocs we have output thus far. */
4035 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4039 /* Get a value for the GP register. */
4040 if (ecoff_data (abfd)->gp == 0)
4042 struct bfd_link_hash_entry *h;
4044 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4045 if (h != (struct bfd_link_hash_entry *) NULL
4046 && h->type == bfd_link_hash_defined)
4047 ecoff_data (abfd)->gp = (h->u.def.value
4048 + h->u.def.section->output_section->vma
4049 + h->u.def.section->output_offset);
4050 else if (info->relocateable)
4054 /* Make up a value. */
4056 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4059 && (strcmp (o->name, _SBSS) == 0
4060 || strcmp (o->name, _SDATA) == 0
4061 || strcmp (o->name, _LIT4) == 0
4062 || strcmp (o->name, _LIT8) == 0
4063 || strcmp (o->name, _LITA) == 0))
4066 ecoff_data (abfd)->gp = lo + 0x8000;
4070 /* If the relocate_section function needs to do a reloc
4071 involving the GP value, it should make a reloc_dangerous
4072 callback to warn that GP is not defined. */
4076 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4078 for (p = o->link_order_head;
4079 p != (struct bfd_link_order *) NULL;
4082 if (p->type == bfd_indirect_link_order
4083 && (bfd_get_flavour (p->u.indirect.section->owner)
4084 == bfd_target_ecoff_flavour))
4086 if (! ecoff_indirect_link_order (abfd, info, o, p))
4089 else if (p->type == bfd_section_reloc_link_order
4090 || p->type == bfd_symbol_reloc_link_order)
4092 if (! ecoff_reloc_link_order (abfd, info, o, p))
4097 if (! _bfd_default_link_order (abfd, info, o, p))
4103 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4105 ecoff_data (abfd)->linker = true;
4110 /* Accumulate the debugging information for an input BFD into the
4111 output BFD. This must read in the symbolic information of the
4115 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4118 struct bfd_link_info *info;
4121 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4122 const struct ecoff_debug_swap * const swap =
4123 &ecoff_backend (input_bfd)->debug_swap;
4124 HDRR *symhdr = &debug->symbolic_header;
4127 #define READ(ptr, offset, count, size, type) \
4128 if (symhdr->count == 0) \
4129 debug->ptr = NULL; \
4132 debug->ptr = (type) malloc ((size_t) (size * symhdr->count)); \
4133 if (debug->ptr == NULL) \
4135 bfd_set_error (bfd_error_no_memory); \
4137 goto return_something; \
4139 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4141 || (bfd_read (debug->ptr, size, symhdr->count, \
4142 input_bfd) != size * symhdr->count)) \
4145 goto return_something; \
4149 /* If raw_syments is not NULL, then the data was already by read by
4150 _bfd_ecoff_slurp_symbolic_info. */
4151 if (ecoff_data (input_bfd)->raw_syments == NULL)
4153 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4155 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4156 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4157 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4158 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4159 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4161 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4162 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4163 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4167 /* We do not read the external strings or the external symbols. */
4169 ret = (bfd_ecoff_debug_accumulate
4170 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4171 &ecoff_backend (output_bfd)->debug_swap,
4172 input_bfd, debug, swap, info));
4175 if (ecoff_data (input_bfd)->raw_syments == NULL)
4177 if (debug->line != NULL)
4179 if (debug->external_dnr != NULL)
4180 free (debug->external_dnr);
4181 if (debug->external_pdr != NULL)
4182 free (debug->external_pdr);
4183 if (debug->external_sym != NULL)
4184 free (debug->external_sym);
4185 if (debug->external_opt != NULL)
4186 free (debug->external_opt);
4187 if (debug->external_aux != NULL)
4188 free (debug->external_aux);
4189 if (debug->ss != NULL)
4191 if (debug->external_fdr != NULL)
4192 free (debug->external_fdr);
4193 if (debug->external_rfd != NULL)
4194 free (debug->external_rfd);
4196 /* Make sure we don't accidentally follow one of these pointers
4197 into freed memory. */
4199 debug->external_dnr = NULL;
4200 debug->external_pdr = NULL;
4201 debug->external_sym = NULL;
4202 debug->external_opt = NULL;
4203 debug->external_aux = NULL;
4205 debug->external_fdr = NULL;
4206 debug->external_rfd = NULL;
4212 /* Put out information for an external symbol. These come only from
4216 ecoff_link_write_external (h, data)
4217 struct ecoff_link_hash_entry *h;
4220 bfd *output_bfd = (bfd *) data;
4222 /* FIXME: We should check if this symbol is being stripped. */
4227 if (h->abfd == (bfd *) NULL)
4230 h->esym.cobol_main = 0;
4231 h->esym.weakext = 0;
4232 h->esym.reserved = 0;
4233 h->esym.ifd = ifdNil;
4234 h->esym.asym.value = 0;
4235 h->esym.asym.st = stGlobal;
4237 if (h->root.type != bfd_link_hash_defined
4238 && h->root.type != bfd_link_hash_defweak)
4239 h->esym.asym.sc = scAbs;
4242 asection *output_section;
4245 output_section = h->root.u.def.section->output_section;
4246 name = bfd_section_name (output_section->owner, output_section);
4248 if (strcmp (name, _TEXT) == 0)
4249 h->esym.asym.sc = scText;
4250 else if (strcmp (name, _DATA) == 0)
4251 h->esym.asym.sc = scData;
4252 else if (strcmp (name, _SDATA) == 0)
4253 h->esym.asym.sc = scSData;
4254 else if (strcmp (name, _RDATA) == 0)
4255 h->esym.asym.sc = scRData;
4256 else if (strcmp (name, _BSS) == 0)
4257 h->esym.asym.sc = scBss;
4258 else if (strcmp (name, _SBSS) == 0)
4259 h->esym.asym.sc = scSBss;
4260 else if (strcmp (name, _INIT) == 0)
4261 h->esym.asym.sc = scInit;
4262 else if (strcmp (name, _FINI) == 0)
4263 h->esym.asym.sc = scFini;
4264 else if (strcmp (name, _PDATA) == 0)
4265 h->esym.asym.sc = scPData;
4266 else if (strcmp (name, _XDATA) == 0)
4267 h->esym.asym.sc = scXData;
4269 h->esym.asym.sc = scAbs;
4272 h->esym.asym.reserved = 0;
4273 h->esym.asym.index = indexNil;
4275 else if (h->esym.ifd != -1)
4277 struct ecoff_debug_info *debug;
4279 /* Adjust the FDR index for the symbol by that used for the
4281 debug = &ecoff_data (h->abfd)->debug_info;
4282 BFD_ASSERT (h->esym.ifd >= 0
4283 && h->esym.ifd < debug->symbolic_header.ifdMax);
4284 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4287 switch (h->root.type)
4290 case bfd_link_hash_new:
4292 case bfd_link_hash_undefined:
4293 case bfd_link_hash_undefweak:
4294 if (h->esym.asym.sc != scUndefined
4295 && h->esym.asym.sc != scSUndefined)
4296 h->esym.asym.sc = scUndefined;
4298 case bfd_link_hash_defined:
4299 case bfd_link_hash_defweak:
4300 if (h->esym.asym.sc == scUndefined
4301 || h->esym.asym.sc == scSUndefined)
4302 h->esym.asym.sc = scAbs;
4303 else if (h->esym.asym.sc == scCommon)
4304 h->esym.asym.sc = scBss;
4305 else if (h->esym.asym.sc == scSCommon)
4306 h->esym.asym.sc = scSBss;
4307 h->esym.asym.value = (h->root.u.def.value
4308 + h->root.u.def.section->output_section->vma
4309 + h->root.u.def.section->output_offset);
4311 case bfd_link_hash_common:
4312 if (h->esym.asym.sc != scCommon
4313 && h->esym.asym.sc != scSCommon)
4314 h->esym.asym.sc = scCommon;
4315 h->esym.asym.value = h->root.u.c.size;
4317 case bfd_link_hash_indirect:
4318 case bfd_link_hash_warning:
4319 /* FIXME: Ignore these for now. The circumstances under which
4320 they should be written out are not clear to me. */
4324 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4326 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4329 return (bfd_ecoff_debug_one_external
4330 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4331 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4335 /* Relocate and write an ECOFF section into an ECOFF output file. */
4338 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4340 struct bfd_link_info *info;
4341 asection *output_section;
4342 struct bfd_link_order *link_order;
4344 asection *input_section;
4346 struct ecoff_section_tdata *section_tdata;
4347 bfd_size_type raw_size;
4348 bfd_size_type cooked_size;
4349 bfd_byte *contents = NULL;
4350 bfd_size_type external_reloc_size;
4351 bfd_size_type external_relocs_size;
4352 PTR external_relocs = NULL;
4354 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4356 if (link_order->size == 0)
4359 input_section = link_order->u.indirect.section;
4360 input_bfd = input_section->owner;
4361 section_tdata = ecoff_section_data (input_bfd, input_section);
4363 raw_size = input_section->_raw_size;
4364 cooked_size = input_section->_cooked_size;
4365 if (cooked_size == 0)
4366 cooked_size = raw_size;
4368 BFD_ASSERT (input_section->output_section == output_section);
4369 BFD_ASSERT (input_section->output_offset == link_order->offset);
4370 BFD_ASSERT (cooked_size == link_order->size);
4372 /* Get the section contents. We allocate memory for the larger of
4373 the size before relocating and the size after relocating. */
4374 contents = (bfd_byte *) malloc (raw_size >= cooked_size
4376 : (size_t) cooked_size);
4377 if (contents == NULL && raw_size != 0)
4379 bfd_set_error (bfd_error_no_memory);
4383 /* If we are relaxing, the contents may have already been read into
4384 memory, in which case we copy them into our new buffer. We don't
4385 simply reuse the old buffer in case cooked_size > raw_size. */
4386 if (section_tdata != (struct ecoff_section_tdata *) NULL
4387 && section_tdata->contents != (bfd_byte *) NULL)
4388 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4391 if (! bfd_get_section_contents (input_bfd, input_section,
4393 (file_ptr) 0, raw_size))
4397 /* Get the relocs. If we are relaxing MIPS code, they will already
4398 have been read in. Otherwise, we read them in now. */
4399 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4400 external_relocs_size = external_reloc_size * input_section->reloc_count;
4402 if (section_tdata != (struct ecoff_section_tdata *) NULL)
4403 external_relocs = section_tdata->external_relocs;
4406 external_relocs = (PTR) malloc ((size_t) external_relocs_size);
4407 if (external_relocs == NULL && external_relocs_size != 0)
4409 bfd_set_error (bfd_error_no_memory);
4413 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4414 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4415 != external_relocs_size))
4419 /* Relocate the section contents. */
4420 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4421 (output_bfd, info, input_bfd, input_section, contents,
4425 /* Write out the relocated section. */
4426 if (! bfd_set_section_contents (output_bfd,
4429 input_section->output_offset,
4433 /* If we are producing relocateable output, the relocs were
4434 modified, and we write them out now. We use the reloc_count
4435 field of output_section to keep track of the number of relocs we
4436 have output so far. */
4437 if (info->relocateable)
4439 if (bfd_seek (output_bfd,
4440 (output_section->rel_filepos +
4441 output_section->reloc_count * external_reloc_size),
4443 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4444 != external_relocs_size))
4446 output_section->reloc_count += input_section->reloc_count;
4449 if (contents != NULL)
4451 if (external_relocs != NULL && section_tdata == NULL)
4452 free (external_relocs);
4456 if (contents != NULL)
4458 if (external_relocs != NULL && section_tdata == NULL)
4459 free (external_relocs);
4463 /* Generate a reloc when linking an ECOFF file. This is a reloc
4464 requested by the linker, and does come from any input file. This
4465 is used to build constructor and destructor tables when linking
4469 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4471 struct bfd_link_info *info;
4472 asection *output_section;
4473 struct bfd_link_order *link_order;
4476 struct internal_reloc in;
4477 bfd_size_type external_reloc_size;
4481 /* We set up an arelent to pass to the backend adjust_reloc_out
4483 rel.address = link_order->offset;
4485 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4488 bfd_set_error (bfd_error_bad_value);
4492 if (link_order->type == bfd_section_reloc_link_order)
4493 rel.sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
4496 /* We can't set up a reloc against a symbol correctly, because
4497 we have no asymbol structure. Currently no adjust_reloc_out
4499 rel.sym_ptr_ptr = (asymbol **) NULL;
4502 /* All ECOFF relocs are in-place. Put the addend into the object
4505 BFD_ASSERT (rel.howto->partial_inplace);
4506 if (link_order->u.reloc.p->addend != 0)
4509 bfd_reloc_status_type rstat;
4513 size = bfd_get_reloc_size (rel.howto);
4514 buf = (bfd_byte *) bfd_zmalloc (size);
4515 if (buf == (bfd_byte *) NULL)
4517 bfd_set_error (bfd_error_no_memory);
4520 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4521 link_order->u.reloc.p->addend, buf);
4527 case bfd_reloc_outofrange:
4529 case bfd_reloc_overflow:
4530 if (! ((*info->callbacks->reloc_overflow)
4532 (link_order->type == bfd_section_reloc_link_order
4533 ? bfd_section_name (output_bfd,
4534 link_order->u.reloc.p->u.section)
4535 : link_order->u.reloc.p->u.name),
4536 rel.howto->name, link_order->u.reloc.p->addend,
4537 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
4544 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4545 (file_ptr) link_order->offset, size);
4553 /* Move the information into a internal_reloc structure. */
4554 in.r_vaddr = (rel.address
4555 + bfd_get_section_vma (output_bfd, output_section));
4556 in.r_type = rel.howto->type;
4558 if (link_order->type == bfd_symbol_reloc_link_order)
4560 struct ecoff_link_hash_entry *h;
4562 h = ecoff_link_hash_lookup (ecoff_hash_table (info),
4563 link_order->u.reloc.p->u.name,
4564 false, false, true);
4565 if (h != (struct ecoff_link_hash_entry *) NULL
4567 in.r_symndx = h->indx;
4570 if (! ((*info->callbacks->unattached_reloc)
4571 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4572 (asection *) NULL, (bfd_vma) 0)))
4582 name = bfd_get_section_name (output_bfd,
4583 link_order->u.reloc.p->u.section);
4584 if (strcmp (name, ".text") == 0)
4585 in.r_symndx = RELOC_SECTION_TEXT;
4586 else if (strcmp (name, ".rdata") == 0)
4587 in.r_symndx = RELOC_SECTION_RDATA;
4588 else if (strcmp (name, ".data") == 0)
4589 in.r_symndx = RELOC_SECTION_DATA;
4590 else if (strcmp (name, ".sdata") == 0)
4591 in.r_symndx = RELOC_SECTION_SDATA;
4592 else if (strcmp (name, ".sbss") == 0)
4593 in.r_symndx = RELOC_SECTION_SBSS;
4594 else if (strcmp (name, ".bss") == 0)
4595 in.r_symndx = RELOC_SECTION_BSS;
4596 else if (strcmp (name, ".init") == 0)
4597 in.r_symndx = RELOC_SECTION_INIT;
4598 else if (strcmp (name, ".lit8") == 0)
4599 in.r_symndx = RELOC_SECTION_LIT8;
4600 else if (strcmp (name, ".lit4") == 0)
4601 in.r_symndx = RELOC_SECTION_LIT4;
4602 else if (strcmp (name, ".xdata") == 0)
4603 in.r_symndx = RELOC_SECTION_XDATA;
4604 else if (strcmp (name, ".pdata") == 0)
4605 in.r_symndx = RELOC_SECTION_PDATA;
4606 else if (strcmp (name, ".fini") == 0)
4607 in.r_symndx = RELOC_SECTION_FINI;
4608 else if (strcmp (name, ".lita") == 0)
4609 in.r_symndx = RELOC_SECTION_LITA;
4610 else if (strcmp (name, "*ABS*") == 0)
4611 in.r_symndx = RELOC_SECTION_ABS;
4617 /* Let the BFD backend adjust the reloc. */
4618 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4620 /* Get some memory and swap out the reloc. */
4621 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4622 rbuf = (bfd_byte *) malloc ((size_t) external_reloc_size);
4623 if (rbuf == (bfd_byte *) NULL)
4625 bfd_set_error (bfd_error_no_memory);
4629 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4631 ok = (bfd_seek (output_bfd,
4632 (output_section->rel_filepos +
4633 output_section->reloc_count * external_reloc_size),
4635 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4636 == external_reloc_size));
4639 ++output_section->reloc_count;