1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4 Original version by Per Bothner.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "aout/ranlib.h"
29 #include "aout/stab_gnu.h"
31 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32 some other stuff which we don't want and which conflicts with stuff
35 #include "aout/aout64.h"
38 #undef obj_sym_filepos
40 #include "coff/internal.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
47 /* Prototypes for static functions. */
49 static int ecoff_get_magic PARAMS ((bfd *abfd));
50 static long ecoff_sec_to_styp_flags PARAMS ((const char *name,
52 static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *abfd));
53 static boolean ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
54 asymbol *asym, int ext, int weak));
55 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, FDR *fdr,
57 RNDXR *rndx, long isym,
59 static char *ecoff_type_to_string PARAMS ((bfd *abfd, FDR *fdr,
61 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
63 static int ecoff_sort_hdrs PARAMS ((const PTR, const PTR));
64 static boolean ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
65 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
66 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
67 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
68 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
73 /* This stuff is somewhat copied from coffcode.h. */
75 static asection bfd_debug_section = { "*DEBUG*" };
77 /* Create an ECOFF object. */
80 _bfd_ecoff_mkobject (abfd)
83 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
84 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
85 if (abfd->tdata.ecoff_obj_data == NULL)
91 /* This is a hook called by coff_real_object_p to create any backend
92 specific information. */
95 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
100 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
101 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
102 ecoff_data_type *ecoff;
104 if (_bfd_ecoff_mkobject (abfd) == false)
107 ecoff = ecoff_data (abfd);
109 ecoff->sym_filepos = internal_f->f_symptr;
111 if (internal_a != (struct internal_aouthdr *) NULL)
115 ecoff->text_start = internal_a->text_start;
116 ecoff->text_end = internal_a->text_start + internal_a->tsize;
117 ecoff->gp = internal_a->gp_value;
118 ecoff->gprmask = internal_a->gprmask;
119 for (i = 0; i < 4; i++)
120 ecoff->cprmask[i] = internal_a->cprmask[i];
121 ecoff->fprmask = internal_a->fprmask;
122 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
123 abfd->flags |= D_PAGED;
125 abfd->flags &=~ D_PAGED;
128 /* It turns out that no special action is required by the MIPS or
129 Alpha ECOFF backends. They have different information in the
130 a.out header, but we just copy it all (e.g., gprmask, cprmask and
131 fprmask) and let the swapping routines ensure that only relevant
132 information is written out. */
137 /* Initialize a new section. */
140 _bfd_ecoff_new_section_hook (abfd, section)
144 section->alignment_power = 4;
146 if (strcmp (section->name, _TEXT) == 0
147 || strcmp (section->name, _INIT) == 0
148 || strcmp (section->name, _FINI) == 0)
149 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
150 else if (strcmp (section->name, _DATA) == 0
151 || strcmp (section->name, _SDATA) == 0)
152 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
153 else if (strcmp (section->name, _RDATA) == 0
154 || strcmp (section->name, _LIT8) == 0
155 || strcmp (section->name, _LIT4) == 0
156 || strcmp (section->name, _RCONST) == 0
157 || strcmp (section->name, _PDATA) == 0)
158 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
159 else if (strcmp (section->name, _BSS) == 0
160 || strcmp (section->name, _SBSS) == 0)
161 section->flags |= SEC_ALLOC;
162 else if (strcmp (section->name, _LIB) == 0)
164 /* An Irix 4 shared libary. */
165 section->flags |= SEC_COFF_SHARED_LIBRARY;
168 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
169 uncertain about .init on some systems and I don't know how shared
175 /* Determine the machine architecture and type. This is called from
176 the generic COFF routines. It is the inverse of ecoff_get_magic,
177 below. This could be an ECOFF backend routine, with one version
178 for each target, but there aren't all that many ECOFF targets. */
181 _bfd_ecoff_set_arch_mach_hook (abfd, filehdr)
185 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
186 enum bfd_architecture arch;
189 switch (internal_f->f_magic)
192 case MIPS_MAGIC_LITTLE:
194 arch = bfd_arch_mips;
198 case MIPS_MAGIC_LITTLE2:
199 case MIPS_MAGIC_BIG2:
200 /* MIPS ISA level 2: the r6000 */
201 arch = bfd_arch_mips;
205 case MIPS_MAGIC_LITTLE3:
206 case MIPS_MAGIC_BIG3:
207 /* MIPS ISA level 3: the r4000 */
208 arch = bfd_arch_mips;
213 arch = bfd_arch_alpha;
218 arch = bfd_arch_obscure;
223 return bfd_default_set_arch_mach (abfd, arch, mach);
226 /* Get the magic number to use based on the architecture and machine.
227 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
230 ecoff_get_magic (abfd)
235 switch (bfd_get_arch (abfd))
238 switch (bfd_get_mach (abfd))
243 big = MIPS_MAGIC_BIG;
244 little = MIPS_MAGIC_LITTLE;
248 big = MIPS_MAGIC_BIG2;
249 little = MIPS_MAGIC_LITTLE2;
253 big = MIPS_MAGIC_BIG3;
254 little = MIPS_MAGIC_LITTLE3;
258 return bfd_big_endian (abfd) ? big : little;
269 /* Get the section s_flags to use for a section. */
272 ecoff_sec_to_styp_flags (name, flags)
280 if (strcmp (name, _TEXT) == 0)
282 else if (strcmp (name, _DATA) == 0)
284 else if (strcmp (name, _SDATA) == 0)
286 else if (strcmp (name, _RDATA) == 0)
288 else if (strcmp (name, _LITA) == 0)
290 else if (strcmp (name, _LIT8) == 0)
292 else if (strcmp (name, _LIT4) == 0)
294 else if (strcmp (name, _BSS) == 0)
296 else if (strcmp (name, _SBSS) == 0)
298 else if (strcmp (name, _INIT) == 0)
299 styp = STYP_ECOFF_INIT;
300 else if (strcmp (name, _FINI) == 0)
301 styp = STYP_ECOFF_FINI;
302 else if (strcmp (name, _PDATA) == 0)
304 else if (strcmp (name, _XDATA) == 0)
306 else if (strcmp (name, _LIB) == 0)
307 styp = STYP_ECOFF_LIB;
308 else if (strcmp (name, _GOT) == 0)
310 else if (strcmp (name, _HASH) == 0)
312 else if (strcmp (name, _DYNAMIC) == 0)
314 else if (strcmp (name, _LIBLIST) == 0)
316 else if (strcmp (name, _RELDYN) == 0)
318 else if (strcmp (name, _CONFLIC) == 0)
320 else if (strcmp (name, _DYNSTR) == 0)
322 else if (strcmp (name, _DYNSYM) == 0)
324 else if (strcmp (name, _COMMENT) == 0)
327 flags &=~ SEC_NEVER_LOAD;
329 else if (strcmp (name, _RCONST) == 0)
331 else if (flags & SEC_CODE)
333 else if (flags & SEC_DATA)
335 else if (flags & SEC_READONLY)
337 else if (flags & SEC_LOAD)
342 if (flags & SEC_NEVER_LOAD)
348 /* Get the BFD flags to use for a section. */
352 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name)
357 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
358 long styp_flags = internal_s->s_flags;
359 flagword sec_flags=0;
361 if (styp_flags & STYP_NOLOAD)
362 sec_flags |= SEC_NEVER_LOAD;
364 /* For 386 COFF, at least, an unloadable text or data section is
365 actually a shared library section. */
366 if ((styp_flags & STYP_TEXT)
367 || (styp_flags & STYP_ECOFF_INIT)
368 || (styp_flags & STYP_ECOFF_FINI)
369 || (styp_flags & STYP_DYNAMIC)
370 || (styp_flags & STYP_LIBLIST)
371 || (styp_flags & STYP_RELDYN)
372 || styp_flags == STYP_CONFLIC
373 || (styp_flags & STYP_DYNSTR)
374 || (styp_flags & STYP_DYNSYM)
375 || (styp_flags & STYP_HASH))
377 if (sec_flags & SEC_NEVER_LOAD)
378 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
380 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
382 else if ((styp_flags & STYP_DATA)
383 || (styp_flags & STYP_RDATA)
384 || (styp_flags & STYP_SDATA)
385 || styp_flags == STYP_PDATA
386 || styp_flags == STYP_XDATA
387 || (styp_flags & STYP_GOT)
388 || styp_flags == STYP_RCONST)
390 if (sec_flags & SEC_NEVER_LOAD)
391 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
393 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
394 if ((styp_flags & STYP_RDATA)
395 || styp_flags == STYP_PDATA
396 || styp_flags == STYP_RCONST)
397 sec_flags |= SEC_READONLY;
399 else if ((styp_flags & STYP_BSS)
400 || (styp_flags & STYP_SBSS))
402 sec_flags |= SEC_ALLOC;
404 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
406 sec_flags |= SEC_NEVER_LOAD;
408 else if ((styp_flags & STYP_LITA)
409 || (styp_flags & STYP_LIT8)
410 || (styp_flags & STYP_LIT4))
412 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
414 else if (styp_flags & STYP_ECOFF_LIB)
416 sec_flags |= SEC_COFF_SHARED_LIBRARY;
420 sec_flags |= SEC_ALLOC | SEC_LOAD;
426 /* Read in the symbolic header for an ECOFF object file. */
429 ecoff_slurp_symbolic_header (abfd)
432 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
433 bfd_size_type external_hdr_size;
435 HDRR *internal_symhdr;
437 /* See if we've already read it in. */
438 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
439 backend->debug_swap.sym_magic)
442 /* See whether there is a symbolic header. */
443 if (ecoff_data (abfd)->sym_filepos == 0)
445 bfd_get_symcount (abfd) = 0;
449 /* At this point bfd_get_symcount (abfd) holds the number of symbols
450 as read from the file header, but on ECOFF this is always the
451 size of the symbolic information header. It would be cleaner to
452 handle this when we first read the file in coffgen.c. */
453 external_hdr_size = backend->debug_swap.external_hdr_size;
454 if (bfd_get_symcount (abfd) != external_hdr_size)
456 bfd_set_error (bfd_error_bad_value);
460 /* Read the symbolic information header. */
461 raw = (PTR) bfd_malloc ((size_t) external_hdr_size);
465 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
466 || (bfd_read (raw, external_hdr_size, 1, abfd)
467 != external_hdr_size))
469 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
470 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
472 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
474 bfd_set_error (bfd_error_bad_value);
478 /* Now we can get the correct number of symbols. */
479 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
480 + internal_symhdr->iextMax);
491 /* Read in and swap the important symbolic information for an ECOFF
492 object file. This is called by gdb via the read_debug_info entry
493 point in the backend structure. */
497 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
500 struct ecoff_debug_info *debug;
502 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
503 HDRR *internal_symhdr;
504 bfd_size_type raw_base;
505 bfd_size_type raw_size;
507 bfd_size_type external_fdr_size;
511 bfd_size_type raw_end;
512 bfd_size_type cb_end;
514 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
516 /* Check whether we've already gotten it, and whether there's any to
518 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
520 if (ecoff_data (abfd)->sym_filepos == 0)
522 bfd_get_symcount (abfd) = 0;
526 if (! ecoff_slurp_symbolic_header (abfd))
529 internal_symhdr = &debug->symbolic_header;
531 /* Read all the symbolic information at once. */
532 raw_base = (ecoff_data (abfd)->sym_filepos
533 + backend->debug_swap.external_hdr_size);
535 /* Alpha ecoff makes the determination of raw_size difficult. It has
536 an undocumented debug data section between the symhdr and the first
537 documented section. And the ordering of the sections varies between
538 statically and dynamically linked executables.
539 If bfd supports SEEK_END someday, this code could be simplified. */
543 #define UPDATE_RAW_END(start, count, size) \
544 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
545 if (cb_end > raw_end) \
548 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
549 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
550 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
551 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
552 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
553 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
554 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
555 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
556 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
557 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
558 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
560 #undef UPDATE_RAW_END
562 raw_size = raw_end - raw_base;
565 ecoff_data (abfd)->sym_filepos = 0;
568 raw = (PTR) bfd_alloc (abfd, raw_size);
572 (ecoff_data (abfd)->sym_filepos
573 + backend->debug_swap.external_hdr_size),
575 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
577 bfd_release (abfd, raw);
581 ecoff_data (abfd)->raw_syments = raw;
583 /* Get pointers for the numeric offsets in the HDRR structure. */
584 #define FIX(off1, off2, type) \
585 if (internal_symhdr->off1 == 0) \
586 debug->off2 = (type) NULL; \
588 debug->off2 = (type) ((char *) raw \
589 + (internal_symhdr->off1 \
591 FIX (cbLineOffset, line, unsigned char *);
592 FIX (cbDnOffset, external_dnr, PTR);
593 FIX (cbPdOffset, external_pdr, PTR);
594 FIX (cbSymOffset, external_sym, PTR);
595 FIX (cbOptOffset, external_opt, PTR);
596 FIX (cbAuxOffset, external_aux, union aux_ext *);
597 FIX (cbSsOffset, ss, char *);
598 FIX (cbSsExtOffset, ssext, char *);
599 FIX (cbFdOffset, external_fdr, PTR);
600 FIX (cbRfdOffset, external_rfd, PTR);
601 FIX (cbExtOffset, external_ext, PTR);
604 /* I don't want to always swap all the data, because it will just
605 waste time and most programs will never look at it. The only
606 time the linker needs most of the debugging information swapped
607 is when linking big-endian and little-endian MIPS object files
608 together, which is not a common occurrence.
610 We need to look at the fdr to deal with a lot of information in
611 the symbols, so we swap them here. */
612 debug->fdr = (struct fdr *) bfd_alloc (abfd,
613 (internal_symhdr->ifdMax *
614 sizeof (struct fdr)));
615 if (debug->fdr == NULL)
617 external_fdr_size = backend->debug_swap.external_fdr_size;
618 fdr_ptr = debug->fdr;
619 fraw_src = (char *) debug->external_fdr;
620 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
621 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
622 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
627 /* ECOFF symbol table routines. The ECOFF symbol table is described
628 in gcc/mips-tfile.c. */
630 /* ECOFF uses two common sections. One is the usual one, and the
631 other is for small objects. All the small objects are kept
632 together, and then referenced via the gp pointer, which yields
633 faster assembler code. This is what we use for the small common
635 static asection ecoff_scom_section;
636 static asymbol ecoff_scom_symbol;
637 static asymbol *ecoff_scom_symbol_ptr;
639 /* Create an empty symbol. */
642 _bfd_ecoff_make_empty_symbol (abfd)
645 ecoff_symbol_type *new;
647 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
648 if (new == (ecoff_symbol_type *) NULL)
649 return (asymbol *) NULL;
650 memset ((PTR) new, 0, sizeof *new);
651 new->symbol.section = (asection *) NULL;
652 new->fdr = (FDR *) NULL;
655 new->symbol.the_bfd = abfd;
659 /* Set the BFD flags and section for an ECOFF symbol. */
662 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, weak)
669 asym->the_bfd = abfd;
670 asym->value = ecoff_sym->value;
671 asym->section = &bfd_debug_section;
674 /* Most symbol types are just for debugging. */
675 switch (ecoff_sym->st)
684 if (ECOFF_IS_STAB (ecoff_sym))
686 asym->flags = BSF_DEBUGGING;
691 asym->flags = BSF_DEBUGGING;
696 asym->flags = BSF_EXPORT | BSF_WEAK;
698 asym->flags = BSF_EXPORT | BSF_GLOBAL;
701 asym->flags = BSF_LOCAL;
702 /* Normally, a local stProc symbol will have a corresponding
703 external symbol. We mark the local symbol as a debugging
704 symbol, in order to prevent nm from printing both out.
705 Similarly, we mark stLabel and stabs symbols as debugging
706 symbols. In both cases, we do want to set the value
707 correctly based on the symbol class. */
708 if (ecoff_sym->st == stProc
709 || ecoff_sym->st == stLabel
710 || ECOFF_IS_STAB (ecoff_sym))
711 asym->flags |= BSF_DEBUGGING;
713 switch (ecoff_sym->sc)
716 /* Used for compiler generated labels. Leave them in the
717 debugging section, and mark them as local. If BSF_DEBUGGING
718 is set, then nm does not display them for some reason. If no
719 flags are set then the linker whines about them. */
720 asym->flags = BSF_LOCAL;
723 asym->section = bfd_make_section_old_way (abfd, ".text");
724 asym->value -= asym->section->vma;
727 asym->section = bfd_make_section_old_way (abfd, ".data");
728 asym->value -= asym->section->vma;
731 asym->section = bfd_make_section_old_way (abfd, ".bss");
732 asym->value -= asym->section->vma;
735 asym->flags = BSF_DEBUGGING;
738 asym->section = bfd_abs_section_ptr;
741 asym->section = bfd_und_section_ptr;
751 asym->flags = BSF_DEBUGGING;
754 asym->section = bfd_make_section_old_way (abfd, ".sdata");
755 asym->value -= asym->section->vma;
758 asym->section = bfd_make_section_old_way (abfd, ".sbss");
759 asym->value -= asym->section->vma;
762 asym->section = bfd_make_section_old_way (abfd, ".rdata");
763 asym->value -= asym->section->vma;
766 asym->flags = BSF_DEBUGGING;
769 if (asym->value > ecoff_data (abfd)->gp_size)
771 asym->section = bfd_com_section_ptr;
777 if (ecoff_scom_section.name == NULL)
779 /* Initialize the small common section. */
780 ecoff_scom_section.name = SCOMMON;
781 ecoff_scom_section.flags = SEC_IS_COMMON;
782 ecoff_scom_section.output_section = &ecoff_scom_section;
783 ecoff_scom_section.symbol = &ecoff_scom_symbol;
784 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
785 ecoff_scom_symbol.name = SCOMMON;
786 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
787 ecoff_scom_symbol.section = &ecoff_scom_section;
788 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
790 asym->section = &ecoff_scom_section;
795 asym->flags = BSF_DEBUGGING;
798 asym->section = bfd_und_section_ptr;
803 asym->section = bfd_make_section_old_way (abfd, ".init");
804 asym->value -= asym->section->vma;
809 asym->flags = BSF_DEBUGGING;
812 asym->section = bfd_make_section_old_way (abfd, ".fini");
813 asym->value -= asym->section->vma;
816 asym->section = bfd_make_section_old_way (abfd, ".rconst");
817 asym->value -= asym->section->vma;
823 /* Look for special constructors symbols and make relocation entries
824 in a special construction section. These are produced by the
825 -fgnu-linker argument to g++. */
826 if (ECOFF_IS_STAB (ecoff_sym))
828 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
838 /* This code is no longer needed. It used to be used to
839 make the linker handle set symbols, but they are now
840 handled in the add_symbols routine instead. */
844 arelent_chain *reloc_chain;
845 unsigned int bitsize;
847 /* Get a section with the same name as the symbol (usually
848 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
849 name ___CTOR_LIST (three underscores). We need
850 __CTOR_LIST (two underscores), since ECOFF doesn't use
851 a leading underscore. This should be handled by gcc,
852 but instead we do it here. Actually, this should all
853 be done differently anyhow. */
854 name = bfd_asymbol_name (asym);
855 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
860 section = bfd_get_section_by_name (abfd, name);
861 if (section == (asection *) NULL)
865 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
869 section = bfd_make_section (abfd, copy);
872 /* Build a reloc pointing to this constructor. */
874 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
877 reloc_chain->relent.sym_ptr_ptr =
878 bfd_get_section (asym)->symbol_ptr_ptr;
879 reloc_chain->relent.address = section->_raw_size;
880 reloc_chain->relent.addend = asym->value;
881 reloc_chain->relent.howto =
882 ecoff_backend (abfd)->constructor_reloc;
884 /* Set up the constructor section to hold the reloc. */
885 section->flags = SEC_CONSTRUCTOR;
886 ++section->reloc_count;
888 /* Constructor sections must be rounded to a boundary
889 based on the bitsize. These are not real sections--
890 they are handled specially by the linker--so the ECOFF
891 16 byte alignment restriction does not apply. */
892 bitsize = ecoff_backend (abfd)->constructor_bitsize;
893 section->alignment_power = 1;
894 while ((1 << section->alignment_power) < bitsize / 8)
895 ++section->alignment_power;
897 reloc_chain->next = section->constructor_chain;
898 section->constructor_chain = reloc_chain;
899 section->_raw_size += bitsize / 8;
903 /* Mark the symbol as a constructor. */
904 asym->flags |= BSF_CONSTRUCTOR;
912 /* Read an ECOFF symbol table. */
915 _bfd_ecoff_slurp_symbol_table (abfd)
918 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
919 const bfd_size_type external_ext_size
920 = backend->debug_swap.external_ext_size;
921 const bfd_size_type external_sym_size
922 = backend->debug_swap.external_sym_size;
923 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
924 = backend->debug_swap.swap_ext_in;
925 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
926 = backend->debug_swap.swap_sym_in;
927 bfd_size_type internal_size;
928 ecoff_symbol_type *internal;
929 ecoff_symbol_type *internal_ptr;
935 /* If we've already read in the symbol table, do nothing. */
936 if (ecoff_data (abfd)->canonical_symbols != NULL)
939 /* Get the symbolic information. */
940 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
941 &ecoff_data (abfd)->debug_info))
943 if (bfd_get_symcount (abfd) == 0)
946 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
947 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
948 if (internal == NULL)
951 internal_ptr = internal;
952 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
954 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
955 * external_ext_size));
956 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
960 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
961 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
962 + internal_esym.asym.iss);
963 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
964 &internal_ptr->symbol, 1,
965 internal_esym.weakext))
967 /* The alpha uses a negative ifd field for section symbols. */
968 if (internal_esym.ifd >= 0)
969 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
970 + internal_esym.ifd);
972 internal_ptr->fdr = NULL;
973 internal_ptr->local = false;
974 internal_ptr->native = (PTR) eraw_src;
977 /* The local symbols must be accessed via the fdr's, because the
978 string and aux indices are relative to the fdr information. */
979 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
980 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
981 for (; fdr_ptr < fdr_end; fdr_ptr++)
986 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
987 + fdr_ptr->isymBase * external_sym_size);
988 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
991 lraw_src += external_sym_size, internal_ptr++)
995 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
996 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
999 if (!ecoff_set_symbol_info (abfd, &internal_sym,
1000 &internal_ptr->symbol, 0, 0))
1002 internal_ptr->fdr = fdr_ptr;
1003 internal_ptr->local = true;
1004 internal_ptr->native = (PTR) lraw_src;
1008 ecoff_data (abfd)->canonical_symbols = internal;
1013 /* Return the amount of space needed for the canonical symbols. */
1016 _bfd_ecoff_get_symtab_upper_bound (abfd)
1019 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
1020 &ecoff_data (abfd)->debug_info))
1023 if (bfd_get_symcount (abfd) == 0)
1026 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1029 /* Get the canonical symbols. */
1032 _bfd_ecoff_get_symtab (abfd, alocation)
1034 asymbol **alocation;
1036 unsigned int counter = 0;
1037 ecoff_symbol_type *symbase;
1038 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1040 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1042 if (bfd_get_symcount (abfd) == 0)
1045 symbase = ecoff_data (abfd)->canonical_symbols;
1046 while (counter < bfd_get_symcount (abfd))
1048 *(location++) = symbase++;
1051 *location++ = (ecoff_symbol_type *) NULL;
1052 return bfd_get_symcount (abfd);
1055 /* Turn ECOFF type information into a printable string.
1056 ecoff_emit_aggregate and ecoff_type_to_string are from
1057 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1059 /* Write aggregate information to a string. */
1062 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1070 const struct ecoff_debug_swap * const debug_swap =
1071 &ecoff_backend (abfd)->debug_swap;
1072 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1073 unsigned int ifd = rndx->rfd;
1074 unsigned int indx = rndx->index;
1080 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1081 struct return type of a procedure compiled without -g. */
1082 if (ifd == 0xffffffff
1083 || (rndx->rfd == 0xfff && indx == 0))
1084 name = "<undefined>";
1085 else if (indx == indexNil)
1091 if (debug_info->external_rfd == NULL)
1092 fdr = debug_info->fdr + ifd;
1097 (*debug_swap->swap_rfd_in) (abfd,
1098 ((char *) debug_info->external_rfd
1099 + ((fdr->rfdBase + ifd)
1100 * debug_swap->external_rfd_size)),
1102 fdr = debug_info->fdr + rfd;
1105 indx += fdr->isymBase;
1107 (*debug_swap->swap_sym_in) (abfd,
1108 ((char *) debug_info->external_sym
1109 + indx * debug_swap->external_sym_size),
1112 name = debug_info->ss + fdr->issBase + sym.iss;
1116 "%s %s { ifd = %u, index = %lu }",
1119 + debug_info->symbolic_header.iextMax));
1122 /* Convert the type information to string format. */
1125 ecoff_type_to_string (abfd, fdr, indx)
1130 union aux_ext *aux_ptr;
1139 unsigned int basic_type;
1142 static char buffer2[1024];
1147 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1148 bigendian = fdr->fBigendian;
1150 for (i = 0; i < 7; i++)
1152 qualifiers[i].low_bound = 0;
1153 qualifiers[i].high_bound = 0;
1154 qualifiers[i].stride = 0;
1157 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1158 return "-1 (no type)";
1159 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1161 basic_type = u.ti.bt;
1162 qualifiers[0].type = u.ti.tq0;
1163 qualifiers[1].type = u.ti.tq1;
1164 qualifiers[2].type = u.ti.tq2;
1165 qualifiers[3].type = u.ti.tq3;
1166 qualifiers[4].type = u.ti.tq4;
1167 qualifiers[5].type = u.ti.tq5;
1168 qualifiers[6].type = tqNil;
1171 * Go get the basic type.
1175 case btNil: /* undefined */
1179 case btAdr: /* address - integer same size as pointer */
1180 strcpy (p1, "address");
1183 case btChar: /* character */
1184 strcpy (p1, "char");
1187 case btUChar: /* unsigned character */
1188 strcpy (p1, "unsigned char");
1191 case btShort: /* short */
1192 strcpy (p1, "short");
1195 case btUShort: /* unsigned short */
1196 strcpy (p1, "unsigned short");
1199 case btInt: /* int */
1203 case btUInt: /* unsigned int */
1204 strcpy (p1, "unsigned int");
1207 case btLong: /* long */
1208 strcpy (p1, "long");
1211 case btULong: /* unsigned long */
1212 strcpy (p1, "unsigned long");
1215 case btFloat: /* float (real) */
1216 strcpy (p1, "float");
1219 case btDouble: /* Double (real) */
1220 strcpy (p1, "double");
1223 /* Structures add 1-2 aux words:
1224 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1225 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1227 case btStruct: /* Structure (Record) */
1228 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1229 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1230 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1232 indx++; /* skip aux words */
1235 /* Unions add 1-2 aux words:
1236 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1237 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1239 case btUnion: /* Union */
1240 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1241 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1242 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1244 indx++; /* skip aux words */
1247 /* Enumerations add 1-2 aux words:
1248 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1249 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1251 case btEnum: /* Enumeration */
1252 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1253 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1254 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1256 indx++; /* skip aux words */
1259 case btTypedef: /* defined via a typedef, isymRef points */
1260 strcpy (p1, "typedef");
1263 case btRange: /* subrange of int */
1264 strcpy (p1, "subrange");
1267 case btSet: /* pascal sets */
1271 case btComplex: /* fortran complex */
1272 strcpy (p1, "complex");
1275 case btDComplex: /* fortran double complex */
1276 strcpy (p1, "double complex");
1279 case btIndirect: /* forward or unnamed typedef */
1280 strcpy (p1, "forward/unamed typedef");
1283 case btFixedDec: /* Fixed Decimal */
1284 strcpy (p1, "fixed decimal");
1287 case btFloatDec: /* Float Decimal */
1288 strcpy (p1, "float decimal");
1291 case btString: /* Varying Length Character String */
1292 strcpy (p1, "string");
1295 case btBit: /* Aligned Bit String */
1299 case btPicture: /* Picture */
1300 strcpy (p1, "picture");
1303 case btVoid: /* Void */
1304 strcpy (p1, "void");
1308 sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
1312 p1 += strlen (buffer1);
1315 * If this is a bitfield, get the bitsize.
1321 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1322 sprintf (p1, " : %d", bitsize);
1323 p1 += strlen (buffer1);
1328 * Deal with any qualifiers.
1330 if (qualifiers[0].type != tqNil)
1333 * Snarf up any array bounds in the correct order. Arrays
1334 * store 5 successive words in the aux. table:
1335 * word 0 RNDXR to type of the bounds (ie, int)
1336 * word 1 Current file descriptor index
1338 * word 3 high bound (or -1 if [])
1339 * word 4 stride size in bits
1341 for (i = 0; i < 7; i++)
1343 if (qualifiers[i].type == tqArray)
1345 qualifiers[i].low_bound =
1346 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1347 qualifiers[i].high_bound =
1348 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1349 qualifiers[i].stride =
1350 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1356 * Now print out the qualifiers.
1358 for (i = 0; i < 6; i++)
1360 switch (qualifiers[i].type)
1367 strcpy (p2, "ptr to ");
1368 p2 += sizeof ("ptr to ")-1;
1372 strcpy (p2, "volatile ");
1373 p2 += sizeof ("volatile ")-1;
1377 strcpy (p2, "far ");
1378 p2 += sizeof ("far ")-1;
1382 strcpy (p2, "func. ret. ");
1383 p2 += sizeof ("func. ret. ");
1388 int first_array = i;
1391 /* Print array bounds reversed (ie, in the order the C
1392 programmer writes them). C is such a fun language.... */
1394 while (i < 5 && qualifiers[i+1].type == tqArray)
1397 for (j = i; j >= first_array; j--)
1399 strcpy (p2, "array [");
1400 p2 += sizeof ("array [")-1;
1401 if (qualifiers[j].low_bound != 0)
1403 "%ld:%ld {%ld bits}",
1404 (long) qualifiers[j].low_bound,
1405 (long) qualifiers[j].high_bound,
1406 (long) qualifiers[j].stride);
1408 else if (qualifiers[j].high_bound != -1)
1411 (long) (qualifiers[j].high_bound + 1),
1412 (long) (qualifiers[j].stride));
1415 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1418 strcpy (p2, "] of ");
1419 p2 += sizeof ("] of ")-1;
1427 strcpy (p2, buffer1);
1431 /* Return information about ECOFF symbol SYMBOL in RET. */
1435 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1436 bfd *abfd; /* Ignored. */
1440 bfd_symbol_info (symbol, ret);
1443 /* Return whether this is a local label. */
1447 _bfd_ecoff_bfd_is_local_label_name (abfd, name)
1451 return name[0] == '$';
1454 /* Print information about an ECOFF symbol. */
1457 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1461 bfd_print_symbol_type how;
1463 const struct ecoff_debug_swap * const debug_swap
1464 = &ecoff_backend (abfd)->debug_swap;
1465 FILE *file = (FILE *)filep;
1469 case bfd_print_symbol_name:
1470 fprintf (file, "%s", symbol->name);
1472 case bfd_print_symbol_more:
1473 if (ecoffsymbol (symbol)->local)
1477 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1479 fprintf (file, "ecoff local ");
1480 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1481 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1482 (unsigned) ecoff_sym.sc);
1488 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1490 fprintf (file, "ecoff extern ");
1491 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1492 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1493 (unsigned) ecoff_ext.asym.sc);
1496 case bfd_print_symbol_all:
1497 /* Print out the symbols in a reasonable way */
1506 if (ecoffsymbol (symbol)->local)
1508 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1511 pos = ((((char *) ecoffsymbol (symbol)->native
1512 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1513 / debug_swap->external_sym_size)
1514 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1521 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1524 pos = (((char *) ecoffsymbol (symbol)->native
1525 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1526 / debug_swap->external_ext_size);
1527 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1528 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1529 weakext = ecoff_ext.weakext ? 'w' : ' ';
1532 fprintf (file, "[%3d] %c ",
1534 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1535 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1536 (unsigned) ecoff_ext.asym.st,
1537 (unsigned) ecoff_ext.asym.sc,
1538 (unsigned) ecoff_ext.asym.index,
1539 jmptbl, cobol_main, weakext,
1542 if (ecoffsymbol (symbol)->fdr != NULL
1543 && ecoff_ext.asym.index != indexNil)
1548 bfd_size_type sym_base;
1549 union aux_ext *aux_base;
1551 fdr = ecoffsymbol (symbol)->fdr;
1552 indx = ecoff_ext.asym.index;
1554 /* sym_base is used to map the fdr relative indices which
1555 appear in the file to the position number which we are
1557 sym_base = fdr->isymBase;
1558 if (ecoffsymbol (symbol)->local)
1560 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1562 /* aux_base is the start of the aux entries for this file;
1563 asym.index is an offset from this. */
1564 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1567 /* The aux entries are stored in host byte order; the
1568 order is indicated by a bit in the fdr. */
1569 bigendian = fdr->fBigendian;
1571 /* This switch is basically from gcc/mips-tdump.c */
1572 switch (ecoff_ext.asym.st)
1580 fprintf (file, _("\n End+1 symbol: %ld"),
1581 (long) (indx + sym_base));
1585 if (ecoff_ext.asym.sc == scText
1586 || ecoff_ext.asym.sc == scInfo)
1587 fprintf (file, _("\n First symbol: %ld"),
1588 (long) (indx + sym_base));
1590 fprintf (file, _("\n First symbol: %ld"),
1592 (AUX_GET_ISYM (bigendian,
1593 &aux_base[ecoff_ext.asym.index])
1599 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1601 else if (ecoffsymbol (symbol)->local)
1602 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
1604 (AUX_GET_ISYM (bigendian,
1605 &aux_base[ecoff_ext.asym.index])
1607 ecoff_type_to_string (abfd, fdr, indx + 1));
1609 fprintf (file, _("\n Local symbol: %ld"),
1612 + (ecoff_data (abfd)
1613 ->debug_info.symbolic_header.iextMax)));
1617 fprintf (file, _("\n struct; End+1 symbol: %ld"),
1618 (long) (indx + sym_base));
1622 fprintf (file, _("\n union; End+1 symbol: %ld"),
1623 (long) (indx + sym_base));
1627 fprintf (file, _("\n enum; End+1 symbol: %ld"),
1628 (long) (indx + sym_base));
1632 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1633 fprintf (file, _("\n Type: %s"),
1634 ecoff_type_to_string (abfd, fdr, indx));
1643 /* Read in the relocs for a section. */
1646 ecoff_slurp_reloc_table (abfd, section, symbols)
1651 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1652 arelent *internal_relocs;
1653 bfd_size_type external_reloc_size;
1654 bfd_size_type external_relocs_size;
1655 char *external_relocs;
1659 if (section->relocation != (arelent *) NULL
1660 || section->reloc_count == 0
1661 || (section->flags & SEC_CONSTRUCTOR) != 0)
1664 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1667 internal_relocs = (arelent *) bfd_alloc (abfd,
1669 * section->reloc_count));
1670 external_reloc_size = backend->external_reloc_size;
1671 external_relocs_size = external_reloc_size * section->reloc_count;
1672 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1673 if (internal_relocs == (arelent *) NULL
1674 || external_relocs == (char *) NULL)
1676 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1678 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1679 != external_relocs_size)
1682 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1684 struct internal_reloc intern;
1686 (*backend->swap_reloc_in) (abfd,
1687 external_relocs + i * external_reloc_size,
1690 if (intern.r_extern)
1692 /* r_symndx is an index into the external symbols. */
1693 BFD_ASSERT (intern.r_symndx >= 0
1695 < (ecoff_data (abfd)
1696 ->debug_info.symbolic_header.iextMax)));
1697 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1700 else if (intern.r_symndx == RELOC_SECTION_NONE
1701 || intern.r_symndx == RELOC_SECTION_ABS)
1703 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1708 CONST char *sec_name;
1711 /* r_symndx is a section key. */
1712 switch (intern.r_symndx)
1714 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1715 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1716 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1717 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1718 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1719 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1720 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1721 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1722 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1723 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1724 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1725 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1726 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1727 case RELOC_SECTION_RCONST: sec_name = ".rconst"; break;
1731 sec = bfd_get_section_by_name (abfd, sec_name);
1732 if (sec == (asection *) NULL)
1734 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1736 rptr->addend = - bfd_get_section_vma (abfd, sec);
1739 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1741 /* Let the backend select the howto field and do any other
1742 required processing. */
1743 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1746 bfd_release (abfd, external_relocs);
1748 section->relocation = internal_relocs;
1753 /* Get a canonical list of relocs. */
1756 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1764 if (section->flags & SEC_CONSTRUCTOR)
1766 arelent_chain *chain;
1768 /* This section has relocs made up by us, not the file, so take
1769 them out of their chain and place them into the data area
1771 for (count = 0, chain = section->constructor_chain;
1772 count < section->reloc_count;
1773 count++, chain = chain->next)
1774 *relptr++ = &chain->relent;
1780 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1783 tblptr = section->relocation;
1785 for (count = 0; count < section->reloc_count; count++)
1786 *relptr++ = tblptr++;
1789 *relptr = (arelent *) NULL;
1791 return section->reloc_count;
1794 /* Provided a BFD, a section and an offset into the section, calculate
1795 and return the name of the source file and the line nearest to the
1800 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1801 filename_ptr, functionname_ptr, retline_ptr)
1804 asymbol **ignore_symbols;
1806 CONST char **filename_ptr;
1807 CONST char **functionname_ptr;
1808 unsigned int *retline_ptr;
1810 const struct ecoff_debug_swap * const debug_swap
1811 = &ecoff_backend (abfd)->debug_swap;
1812 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1813 struct ecoff_find_line *line_info;
1815 /* Make sure we have the FDR's. */
1816 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1817 || bfd_get_symcount (abfd) == 0)
1820 if (ecoff_data (abfd)->find_line_info == NULL)
1822 ecoff_data (abfd)->find_line_info =
1823 ((struct ecoff_find_line *)
1824 bfd_zalloc (abfd, sizeof (struct ecoff_find_line)));
1825 if (ecoff_data (abfd)->find_line_info == NULL)
1828 line_info = ecoff_data (abfd)->find_line_info;
1830 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1831 debug_swap, line_info, filename_ptr,
1832 functionname_ptr, retline_ptr);
1835 /* Copy private BFD data. This is called by objcopy and strip. We
1836 use it to copy the ECOFF debugging information from one BFD to the
1837 other. It would be theoretically possible to represent the ECOFF
1838 debugging information in the symbol table. However, it would be a
1839 lot of work, and there would be little gain (gas, gdb, and ld
1840 already access the ECOFF debugging information via the
1841 ecoff_debug_info structure, and that structure would have to be
1842 retained in order to support ECOFF debugging in MIPS ELF).
1844 The debugging information for the ECOFF external symbols comes from
1845 the symbol table, so this function only handles the other debugging
1849 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1853 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1854 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1856 asymbol **sym_ptr_ptr;
1860 /* We only want to copy information over if both BFD's use ECOFF
1862 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1863 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1866 /* Copy the GP value and the register masks. */
1867 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1868 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1869 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1870 for (i = 0; i < 3; i++)
1871 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1873 /* Copy the version stamp. */
1874 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1876 /* If there are no symbols, don't copy any debugging information. */
1877 c = bfd_get_symcount (obfd);
1878 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1879 if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1882 /* See if there are any local symbols. */
1884 for (; c > 0; c--, sym_ptr_ptr++)
1886 if (ecoffsymbol (*sym_ptr_ptr)->local)
1895 /* There are some local symbols. We just bring over all the
1896 debugging information. FIXME: This is not quite the right
1897 thing to do. If the user has asked us to discard all
1898 debugging information, then we are probably going to wind up
1899 keeping it because there will probably be some local symbol
1900 which objcopy did not discard. We should actually break
1901 apart the debugging information and only keep that which
1902 applies to the symbols we want to keep. */
1903 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1904 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1905 oinfo->line = iinfo->line;
1907 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1908 oinfo->external_dnr = iinfo->external_dnr;
1910 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1911 oinfo->external_pdr = iinfo->external_pdr;
1913 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1914 oinfo->external_sym = iinfo->external_sym;
1916 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1917 oinfo->external_opt = iinfo->external_opt;
1919 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1920 oinfo->external_aux = iinfo->external_aux;
1922 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1923 oinfo->ss = iinfo->ss;
1925 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1926 oinfo->external_fdr = iinfo->external_fdr;
1928 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1929 oinfo->external_rfd = iinfo->external_rfd;
1933 /* We are discarding all the local symbol information. Look
1934 through the external symbols and remove all references to FDR
1935 or aux information. */
1936 c = bfd_get_symcount (obfd);
1937 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1938 for (; c > 0; c--, sym_ptr_ptr++)
1942 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1943 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1945 esym.asym.index = indexNil;
1946 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1947 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1954 /* Set the architecture. The supported architecture is stored in the
1955 backend pointer. We always set the architecture anyhow, since many
1956 callers ignore the return value. */
1959 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1961 enum bfd_architecture arch;
1962 unsigned long machine;
1964 bfd_default_set_arch_mach (abfd, arch, machine);
1965 return arch == ecoff_backend (abfd)->arch;
1968 /* Get the size of the section headers. */
1972 _bfd_ecoff_sizeof_headers (abfd, reloc)
1981 for (current = abfd->sections;
1982 current != (asection *)NULL;
1983 current = current->next)
1986 ret = (bfd_coff_filhsz (abfd)
1987 + bfd_coff_aoutsz (abfd)
1988 + c * bfd_coff_scnhsz (abfd));
1989 return BFD_ALIGN (ret, 16);
1992 /* Get the contents of a section. */
1995 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
2000 bfd_size_type count;
2002 return _bfd_generic_get_section_contents (abfd, section, location,
2006 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2007 called via qsort. */
2010 ecoff_sort_hdrs (arg1, arg2)
2014 const asection *hdr1 = *(const asection **) arg1;
2015 const asection *hdr2 = *(const asection **) arg2;
2017 if ((hdr1->flags & SEC_ALLOC) != 0)
2019 if ((hdr2->flags & SEC_ALLOC) == 0)
2024 if ((hdr2->flags & SEC_ALLOC) != 0)
2027 if (hdr1->vma < hdr2->vma)
2029 else if (hdr1->vma > hdr2->vma)
2035 /* Calculate the file position for each section, and set
2039 ecoff_compute_section_file_positions (abfd)
2042 file_ptr sofar, file_sofar;
2043 asection **sorted_hdrs;
2047 boolean rdata_in_text;
2048 boolean first_data, first_nonalloc;
2049 const bfd_vma round = ecoff_backend (abfd)->round;
2051 sofar = _bfd_ecoff_sizeof_headers (abfd, false);
2054 /* Sort the sections by VMA. */
2055 sorted_hdrs = (asection **) bfd_malloc (abfd->section_count
2056 * sizeof (asection *));
2057 if (sorted_hdrs == NULL)
2059 for (current = abfd->sections, i = 0;
2061 current = current->next, i++)
2062 sorted_hdrs[i] = current;
2063 BFD_ASSERT (i == abfd->section_count);
2065 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2068 /* Some versions of the OSF linker put the .rdata section in the
2069 text segment, and some do not. */
2070 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2073 for (i = 0; i < abfd->section_count; i++)
2075 current = sorted_hdrs[i];
2076 if (strcmp (current->name, _RDATA) == 0)
2078 if ((current->flags & SEC_CODE) == 0
2079 && strcmp (current->name, _PDATA) != 0
2080 && strcmp (current->name, _RCONST) != 0)
2082 rdata_in_text = false;
2087 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2090 first_nonalloc = true;
2091 for (i = 0; i < abfd->section_count; i++)
2093 unsigned int alignment_power;
2095 current = sorted_hdrs[i];
2097 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2098 supposed to indicate the number of .pdata entries that are
2099 really in the section. Each entry is 8 bytes. We store this
2100 away in line_filepos before increasing the section size. */
2101 if (strcmp (current->name, _PDATA) == 0)
2102 current->line_filepos = current->_raw_size / 8;
2104 alignment_power = current->alignment_power;
2106 /* On Ultrix, the data sections in an executable file must be
2107 aligned to a page boundary within the file. This does not
2108 affect the section size, though. FIXME: Does this work for
2109 other platforms? It requires some modification for the
2110 Alpha, because .rdata on the Alpha goes with the text, not
2112 if ((abfd->flags & EXEC_P) != 0
2113 && (abfd->flags & D_PAGED) != 0
2115 && (current->flags & SEC_CODE) == 0
2117 || strcmp (current->name, _RDATA) != 0)
2118 && strcmp (current->name, _PDATA) != 0
2119 && strcmp (current->name, _RCONST) != 0)
2121 sofar = (sofar + round - 1) &~ (round - 1);
2122 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2125 else if (strcmp (current->name, _LIB) == 0)
2127 /* On Irix 4, the location of contents of the .lib section
2128 from a shared library section is also rounded up to a
2131 sofar = (sofar + round - 1) &~ (round - 1);
2132 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2134 else if (first_nonalloc
2135 && (current->flags & SEC_ALLOC) == 0
2136 && (abfd->flags & D_PAGED) != 0)
2138 /* Skip up to the next page for an unallocated section, such
2139 as the .comment section on the Alpha. This leaves room
2140 for the .bss section. */
2141 first_nonalloc = false;
2142 sofar = (sofar + round - 1) &~ (round - 1);
2143 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2146 /* Align the sections in the file to the same boundary on
2147 which they are aligned in virtual memory. */
2148 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2149 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2150 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2152 if ((abfd->flags & D_PAGED) != 0
2153 && (current->flags & SEC_ALLOC) != 0)
2155 sofar += (current->vma - sofar) % round;
2156 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2157 file_sofar += (current->vma - file_sofar) % round;
2160 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2161 current->filepos = file_sofar;
2163 sofar += current->_raw_size;
2164 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2165 file_sofar += current->_raw_size;
2167 /* make sure that this section is of the right size too */
2169 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2170 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2171 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2172 current->_raw_size += sofar - old_sofar;
2178 ecoff_data (abfd)->reloc_filepos = file_sofar;
2183 /* Determine the location of the relocs for all the sections in the
2184 output file, as well as the location of the symbolic debugging
2187 static bfd_size_type
2188 ecoff_compute_reloc_file_positions (abfd)
2191 const bfd_size_type external_reloc_size =
2192 ecoff_backend (abfd)->external_reloc_size;
2193 file_ptr reloc_base;
2194 bfd_size_type reloc_size;
2198 if (! abfd->output_has_begun)
2200 if (! ecoff_compute_section_file_positions (abfd))
2202 abfd->output_has_begun = true;
2205 reloc_base = ecoff_data (abfd)->reloc_filepos;
2208 for (current = abfd->sections;
2209 current != (asection *)NULL;
2210 current = current->next)
2212 if (current->reloc_count == 0)
2213 current->rel_filepos = 0;
2216 bfd_size_type relsize;
2218 current->rel_filepos = reloc_base;
2219 relsize = current->reloc_count * external_reloc_size;
2220 reloc_size += relsize;
2221 reloc_base += relsize;
2225 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2227 /* At least on Ultrix, the symbol table of an executable file must
2228 be aligned to a page boundary. FIXME: Is this true on other
2230 if ((abfd->flags & EXEC_P) != 0
2231 && (abfd->flags & D_PAGED) != 0)
2232 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2233 &~ (ecoff_backend (abfd)->round - 1));
2235 ecoff_data (abfd)->sym_filepos = sym_base;
2240 /* Set the contents of a section. */
2243 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2248 bfd_size_type count;
2250 /* This must be done first, because bfd_set_section_contents is
2251 going to set output_has_begun to true. */
2252 if (abfd->output_has_begun == false)
2254 if (! ecoff_compute_section_file_positions (abfd))
2258 /* Handle the .lib section specially so that Irix 4 shared libraries
2259 work out. See coff_set_section_contents in coffcode.h. */
2260 if (strcmp (section->name, _LIB) == 0)
2262 bfd_byte *rec, *recend;
2264 rec = (bfd_byte *) location;
2265 recend = rec + count;
2266 while (rec < recend)
2269 rec += bfd_get_32 (abfd, rec) * 4;
2272 BFD_ASSERT (rec == recend);
2278 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2279 || bfd_write (location, 1, count, abfd) != count)
2285 /* Get the GP value for an ECOFF file. This is a hook used by
2289 bfd_ecoff_get_gp_value (abfd)
2292 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2293 || bfd_get_format (abfd) != bfd_object)
2295 bfd_set_error (bfd_error_invalid_operation);
2299 return ecoff_data (abfd)->gp;
2302 /* Set the GP value for an ECOFF file. This is a hook used by the
2306 bfd_ecoff_set_gp_value (abfd, gp_value)
2310 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2311 || bfd_get_format (abfd) != bfd_object)
2313 bfd_set_error (bfd_error_invalid_operation);
2317 ecoff_data (abfd)->gp = gp_value;
2322 /* Set the register masks for an ECOFF file. This is a hook used by
2326 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2328 unsigned long gprmask;
2329 unsigned long fprmask;
2330 unsigned long *cprmask;
2332 ecoff_data_type *tdata;
2334 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2335 || bfd_get_format (abfd) != bfd_object)
2337 bfd_set_error (bfd_error_invalid_operation);
2341 tdata = ecoff_data (abfd);
2342 tdata->gprmask = gprmask;
2343 tdata->fprmask = fprmask;
2344 if (cprmask != (unsigned long *) NULL)
2348 for (i = 0; i < 3; i++)
2349 tdata->cprmask[i] = cprmask[i];
2355 /* Get ECOFF EXTR information for an external symbol. This function
2356 is passed to bfd_ecoff_debug_externals. */
2359 ecoff_get_extr (sym, esym)
2363 ecoff_symbol_type *ecoff_sym_ptr;
2366 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2367 || ecoffsymbol (sym)->native == NULL)
2369 /* Don't include debugging, local, or section symbols. */
2370 if ((sym->flags & BSF_DEBUGGING) != 0
2371 || (sym->flags & BSF_LOCAL) != 0
2372 || (sym->flags & BSF_SECTION_SYM) != 0)
2376 esym->cobol_main = 0;
2377 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2380 /* FIXME: we can do better than this for st and sc. */
2381 esym->asym.st = stGlobal;
2382 esym->asym.sc = scAbs;
2383 esym->asym.reserved = 0;
2384 esym->asym.index = indexNil;
2388 ecoff_sym_ptr = ecoffsymbol (sym);
2390 if (ecoff_sym_ptr->local)
2393 input_bfd = bfd_asymbol_bfd (sym);
2394 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2395 (input_bfd, ecoff_sym_ptr->native, esym);
2397 /* If the symbol was defined by the linker, then esym will be
2398 undefined but sym will not be. Get a better class for such a
2400 if ((esym->asym.sc == scUndefined
2401 || esym->asym.sc == scSUndefined)
2402 && ! bfd_is_und_section (bfd_get_section (sym)))
2403 esym->asym.sc = scAbs;
2405 /* Adjust the FDR index for the symbol by that used for the input
2407 if (esym->ifd != -1)
2409 struct ecoff_debug_info *input_debug;
2411 input_debug = &ecoff_data (input_bfd)->debug_info;
2412 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2413 if (input_debug->ifdmap != (RFDT *) NULL)
2414 esym->ifd = input_debug->ifdmap[esym->ifd];
2420 /* Set the external symbol index. This routine is passed to
2421 bfd_ecoff_debug_externals. */
2424 ecoff_set_index (sym, indx)
2428 ecoff_set_sym_index (sym, indx);
2431 /* Write out an ECOFF file. */
2434 _bfd_ecoff_write_object_contents (abfd)
2437 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2438 const bfd_vma round = backend->round;
2439 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2440 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2441 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2442 const bfd_size_type external_hdr_size
2443 = backend->debug_swap.external_hdr_size;
2444 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2445 void (* const adjust_reloc_out) PARAMS ((bfd *,
2447 struct internal_reloc *))
2448 = backend->adjust_reloc_out;
2449 void (* const swap_reloc_out) PARAMS ((bfd *,
2450 const struct internal_reloc *,
2452 = backend->swap_reloc_out;
2453 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2454 HDRR * const symhdr = &debug->symbolic_header;
2457 bfd_size_type reloc_size;
2458 bfd_size_type text_size;
2460 boolean set_text_start;
2461 bfd_size_type data_size;
2463 boolean set_data_start;
2464 bfd_size_type bss_size;
2466 PTR reloc_buff = NULL;
2467 struct internal_filehdr internal_f;
2468 struct internal_aouthdr internal_a;
2471 /* Determine where the sections and relocs will go in the output
2473 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2476 for (current = abfd->sections;
2477 current != (asection *)NULL;
2478 current = current->next)
2480 current->target_index = count;
2484 if ((abfd->flags & D_PAGED) != 0)
2485 text_size = _bfd_ecoff_sizeof_headers (abfd, false);
2489 set_text_start = false;
2492 set_data_start = false;
2495 /* Write section headers to the file. */
2497 /* Allocate buff big enough to hold a section header,
2498 file header, or a.out header. */
2506 buff = (PTR) bfd_malloc ((size_t) siz);
2511 internal_f.f_nscns = 0;
2512 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2514 for (current = abfd->sections;
2515 current != (asection *) NULL;
2516 current = current->next)
2518 struct internal_scnhdr section;
2521 ++internal_f.f_nscns;
2523 strncpy (section.s_name, current->name, sizeof section.s_name);
2525 /* This seems to be correct for Irix 4 shared libraries. */
2526 vma = bfd_get_section_vma (abfd, current);
2527 if (strcmp (current->name, _LIB) == 0)
2528 section.s_vaddr = 0;
2530 section.s_vaddr = vma;
2532 section.s_paddr = current->lma;
2533 section.s_size = bfd_get_section_size_before_reloc (current);
2535 /* If this section is unloadable then the scnptr will be 0. */
2536 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2537 section.s_scnptr = 0;
2539 section.s_scnptr = current->filepos;
2540 section.s_relptr = current->rel_filepos;
2542 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2543 object file produced by the assembler is supposed to point to
2544 information about how much room is required by objects of
2545 various different sizes. I think this only matters if we
2546 want the linker to compute the best size to use, or
2547 something. I don't know what happens if the information is
2549 if (strcmp (current->name, _PDATA) != 0)
2550 section.s_lnnoptr = 0;
2553 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2554 hold the number of entries in the section (each entry is
2555 8 bytes). We stored this in the line_filepos field in
2556 ecoff_compute_section_file_positions. */
2557 section.s_lnnoptr = current->line_filepos;
2560 section.s_nreloc = current->reloc_count;
2561 section.s_nlnno = 0;
2562 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2565 if (bfd_coff_swap_scnhdr_out (abfd, (PTR) §ion, buff) == 0
2566 || bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2569 if ((section.s_flags & STYP_TEXT) != 0
2570 || ((section.s_flags & STYP_RDATA) != 0
2571 && ecoff_data (abfd)->rdata_in_text)
2572 || section.s_flags == STYP_PDATA
2573 || (section.s_flags & STYP_DYNAMIC) != 0
2574 || (section.s_flags & STYP_LIBLIST) != 0
2575 || (section.s_flags & STYP_RELDYN) != 0
2576 || section.s_flags == STYP_CONFLIC
2577 || (section.s_flags & STYP_DYNSTR) != 0
2578 || (section.s_flags & STYP_DYNSYM) != 0
2579 || (section.s_flags & STYP_HASH) != 0
2580 || (section.s_flags & STYP_ECOFF_INIT) != 0
2581 || (section.s_flags & STYP_ECOFF_FINI) != 0
2582 || section.s_flags == STYP_RCONST)
2584 text_size += bfd_get_section_size_before_reloc (current);
2585 if (! set_text_start || text_start > vma)
2588 set_text_start = true;
2591 else if ((section.s_flags & STYP_RDATA) != 0
2592 || (section.s_flags & STYP_DATA) != 0
2593 || (section.s_flags & STYP_LITA) != 0
2594 || (section.s_flags & STYP_LIT8) != 0
2595 || (section.s_flags & STYP_LIT4) != 0
2596 || (section.s_flags & STYP_SDATA) != 0
2597 || section.s_flags == STYP_XDATA
2598 || (section.s_flags & STYP_GOT) != 0)
2600 data_size += bfd_get_section_size_before_reloc (current);
2601 if (! set_data_start || data_start > vma)
2604 set_data_start = true;
2607 else if ((section.s_flags & STYP_BSS) != 0
2608 || (section.s_flags & STYP_SBSS) != 0)
2609 bss_size += bfd_get_section_size_before_reloc (current);
2610 else if (section.s_flags == 0
2611 || (section.s_flags & STYP_ECOFF_LIB) != 0
2612 || section.s_flags == STYP_COMMENT)
2618 /* Set up the file header. */
2620 internal_f.f_magic = ecoff_get_magic (abfd);
2622 /* We will NOT put a fucking timestamp in the header here. Every
2623 time you put it back, I will come in and take it out again. I'm
2624 sorry. This field does not belong here. We fill it with a 0 so
2625 it compares the same but is not a reasonable time. --
2627 internal_f.f_timdat = 0;
2629 if (bfd_get_symcount (abfd) != 0)
2631 /* The ECOFF f_nsyms field is not actually the number of
2632 symbols, it's the size of symbolic information header. */
2633 internal_f.f_nsyms = external_hdr_size;
2634 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2638 internal_f.f_nsyms = 0;
2639 internal_f.f_symptr = 0;
2642 internal_f.f_opthdr = aoutsz;
2644 internal_f.f_flags = F_LNNO;
2645 if (reloc_size == 0)
2646 internal_f.f_flags |= F_RELFLG;
2647 if (bfd_get_symcount (abfd) == 0)
2648 internal_f.f_flags |= F_LSYMS;
2649 if (abfd->flags & EXEC_P)
2650 internal_f.f_flags |= F_EXEC;
2652 if (bfd_little_endian (abfd))
2653 internal_f.f_flags |= F_AR32WR;
2655 internal_f.f_flags |= F_AR32W;
2657 /* Set up the ``optional'' header. */
2658 if ((abfd->flags & D_PAGED) != 0)
2659 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2661 internal_a.magic = ECOFF_AOUT_OMAGIC;
2663 /* FIXME: Is this really correct? */
2664 internal_a.vstamp = symhdr->vstamp;
2666 /* At least on Ultrix, these have to be rounded to page boundaries.
2667 FIXME: Is this true on other platforms? */
2668 if ((abfd->flags & D_PAGED) != 0)
2670 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2671 internal_a.text_start = text_start &~ (round - 1);
2672 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2673 internal_a.data_start = data_start &~ (round - 1);
2677 internal_a.tsize = text_size;
2678 internal_a.text_start = text_start;
2679 internal_a.dsize = data_size;
2680 internal_a.data_start = data_start;
2683 /* On Ultrix, the initial portions of the .sbss and .bss segments
2684 are at the end of the data section. The bsize field in the
2685 optional header records how many bss bytes are required beyond
2686 those in the data section. The value is not rounded to a page
2688 if (bss_size < internal_a.dsize - data_size)
2691 bss_size -= internal_a.dsize - data_size;
2692 internal_a.bsize = bss_size;
2693 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2695 internal_a.entry = bfd_get_start_address (abfd);
2697 internal_a.gp_value = ecoff_data (abfd)->gp;
2699 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2700 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2701 for (i = 0; i < 4; i++)
2702 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2704 /* Let the backend adjust the headers if necessary. */
2705 if (backend->adjust_headers)
2707 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2711 /* Write out the file header and the optional header. */
2713 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2716 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2717 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2720 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2721 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2724 /* Build the external symbol information. This must be done before
2725 writing out the relocs so that we know the symbol indices. We
2726 don't do this if this BFD was created by the backend linker,
2727 since it will have already handled the symbols and relocs. */
2728 if (! ecoff_data (abfd)->linker)
2730 symhdr->iextMax = 0;
2731 symhdr->issExtMax = 0;
2732 debug->external_ext = debug->external_ext_end = NULL;
2733 debug->ssext = debug->ssext_end = NULL;
2734 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2735 (((abfd->flags & EXEC_P) == 0)
2737 ecoff_get_extr, ecoff_set_index)
2741 /* Write out the relocs. */
2742 for (current = abfd->sections;
2743 current != (asection *) NULL;
2744 current = current->next)
2746 arelent **reloc_ptr_ptr;
2747 arelent **reloc_end;
2750 if (current->reloc_count == 0)
2754 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2755 if (reloc_buff == NULL)
2758 reloc_ptr_ptr = current->orelocation;
2759 reloc_end = reloc_ptr_ptr + current->reloc_count;
2760 out_ptr = (char *) reloc_buff;
2762 reloc_ptr_ptr < reloc_end;
2763 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2767 struct internal_reloc in;
2769 memset ((PTR) &in, 0, sizeof in);
2771 reloc = *reloc_ptr_ptr;
2772 sym = *reloc->sym_ptr_ptr;
2774 in.r_vaddr = (reloc->address
2775 + bfd_get_section_vma (abfd, current));
2776 in.r_type = reloc->howto->type;
2778 if ((sym->flags & BSF_SECTION_SYM) == 0)
2780 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2787 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2788 if (strcmp (name, ".text") == 0)
2789 in.r_symndx = RELOC_SECTION_TEXT;
2790 else if (strcmp (name, ".rdata") == 0)
2791 in.r_symndx = RELOC_SECTION_RDATA;
2792 else if (strcmp (name, ".data") == 0)
2793 in.r_symndx = RELOC_SECTION_DATA;
2794 else if (strcmp (name, ".sdata") == 0)
2795 in.r_symndx = RELOC_SECTION_SDATA;
2796 else if (strcmp (name, ".sbss") == 0)
2797 in.r_symndx = RELOC_SECTION_SBSS;
2798 else if (strcmp (name, ".bss") == 0)
2799 in.r_symndx = RELOC_SECTION_BSS;
2800 else if (strcmp (name, ".init") == 0)
2801 in.r_symndx = RELOC_SECTION_INIT;
2802 else if (strcmp (name, ".lit8") == 0)
2803 in.r_symndx = RELOC_SECTION_LIT8;
2804 else if (strcmp (name, ".lit4") == 0)
2805 in.r_symndx = RELOC_SECTION_LIT4;
2806 else if (strcmp (name, ".xdata") == 0)
2807 in.r_symndx = RELOC_SECTION_XDATA;
2808 else if (strcmp (name, ".pdata") == 0)
2809 in.r_symndx = RELOC_SECTION_PDATA;
2810 else if (strcmp (name, ".fini") == 0)
2811 in.r_symndx = RELOC_SECTION_FINI;
2812 else if (strcmp (name, ".lita") == 0)
2813 in.r_symndx = RELOC_SECTION_LITA;
2814 else if (strcmp (name, "*ABS*") == 0)
2815 in.r_symndx = RELOC_SECTION_ABS;
2816 else if (strcmp (name, ".rconst") == 0)
2817 in.r_symndx = RELOC_SECTION_RCONST;
2823 (*adjust_reloc_out) (abfd, reloc, &in);
2825 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2828 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2830 if (bfd_write (reloc_buff,
2831 external_reloc_size, current->reloc_count, abfd)
2832 != external_reloc_size * current->reloc_count)
2834 bfd_release (abfd, reloc_buff);
2838 /* Write out the symbolic debugging information. */
2839 if (bfd_get_symcount (abfd) > 0)
2841 /* Write out the debugging information. */
2842 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2843 ecoff_data (abfd)->sym_filepos)
2849 /* The .bss section of a demand paged executable must receive an
2850 entire page. If there are symbols, the symbols will start on the
2851 next page. If there are no symbols, we must fill out the page by
2853 if (bfd_get_symcount (abfd) == 0
2854 && (abfd->flags & EXEC_P) != 0
2855 && (abfd->flags & D_PAGED) != 0)
2859 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2862 if (bfd_read (&c, 1, 1, abfd) == 0)
2864 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2867 if (bfd_write (&c, 1, 1, abfd) != 1)
2871 if (reloc_buff != NULL)
2872 bfd_release (abfd, reloc_buff);
2877 if (reloc_buff != NULL)
2878 bfd_release (abfd, reloc_buff);
2884 /* Archive handling. ECOFF uses what appears to be a unique type of
2885 archive header (armap). The byte ordering of the armap and the
2886 contents are encoded in the name of the armap itself. At least for
2887 now, we only support archives with the same byte ordering in the
2888 armap and the contents.
2890 The first four bytes in the armap are the number of symbol
2891 definitions. This is always a power of two.
2893 This is followed by the symbol definitions. Each symbol definition
2894 occupies 8 bytes. The first four bytes are the offset from the
2895 start of the armap strings to the null-terminated string naming
2896 this symbol. The second four bytes are the file offset to the
2897 archive member which defines this symbol. If the second four bytes
2898 are 0, then this is not actually a symbol definition, and it should
2901 The symbols are hashed into the armap with a closed hashing scheme.
2902 See the functions below for the details of the algorithm.
2904 After the symbol definitions comes four bytes holding the size of
2905 the string table, followed by the string table itself. */
2907 /* The name of an archive headers looks like this:
2908 __________E[BL]E[BL]_ (with a trailing space).
2909 The trailing space is changed to an X if the archive is changed to
2910 indicate that the armap is out of date.
2912 The Alpha seems to use ________64E[BL]E[BL]_. */
2914 #define ARMAP_BIG_ENDIAN 'B'
2915 #define ARMAP_LITTLE_ENDIAN 'L'
2916 #define ARMAP_MARKER 'E'
2917 #define ARMAP_START_LENGTH 10
2918 #define ARMAP_HEADER_MARKER_INDEX 10
2919 #define ARMAP_HEADER_ENDIAN_INDEX 11
2920 #define ARMAP_OBJECT_MARKER_INDEX 12
2921 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2922 #define ARMAP_END_INDEX 14
2923 #define ARMAP_END "_ "
2925 /* This is a magic number used in the hashing algorithm. */
2926 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2928 /* This returns the hash value to use for a string. It also sets
2929 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2930 is the number of entries in the hash table, and HLOG is the log
2934 ecoff_armap_hash (s, rehash, size, hlog)
2936 unsigned int *rehash;
2946 hash = ((hash >> 27) | (hash << 5)) + *s++;
2947 hash *= ARMAP_HASH_MAGIC;
2948 *rehash = (hash & (size - 1)) | 1;
2949 return hash >> (32 - hlog);
2952 /* Read in the armap. */
2955 _bfd_ecoff_slurp_armap (abfd)
2960 struct areltdata *mapdata;
2961 bfd_size_type parsed_size;
2963 struct artdata *ardata;
2966 struct symdef *symdef_ptr;
2969 /* Get the name of the first element. */
2970 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2976 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2979 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2980 standard COFF armap. We could move the ECOFF armap stuff into
2981 bfd_slurp_armap, but that seems inappropriate since no other
2982 target uses this format. Instead, we check directly for a COFF
2984 if (strncmp (nextname, "/ ", 16) == 0)
2985 return bfd_slurp_armap (abfd);
2987 /* See if the first element is an armap. */
2988 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2989 ARMAP_START_LENGTH) != 0
2990 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2991 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2992 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2993 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2994 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2995 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2996 || strncmp (nextname + ARMAP_END_INDEX,
2997 ARMAP_END, sizeof ARMAP_END - 1) != 0)
2999 bfd_has_map (abfd) = false;
3003 /* Make sure we have the right byte ordering. */
3004 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3005 ^ (bfd_header_big_endian (abfd)))
3006 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3007 ^ (bfd_big_endian (abfd))))
3009 bfd_set_error (bfd_error_wrong_format);
3013 /* Read in the armap. */
3014 ardata = bfd_ardata (abfd);
3015 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
3016 if (mapdata == (struct areltdata *) NULL)
3018 parsed_size = mapdata->parsed_size;
3019 bfd_release (abfd, (PTR) mapdata);
3021 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3022 if (raw_armap == (char *) NULL)
3025 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3027 if (bfd_get_error () != bfd_error_system_call)
3028 bfd_set_error (bfd_error_malformed_archive);
3029 bfd_release (abfd, (PTR) raw_armap);
3033 ardata->tdata = (PTR) raw_armap;
3035 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3037 ardata->symdef_count = 0;
3038 ardata->cache = (struct ar_cache *) NULL;
3040 /* This code used to overlay the symdefs over the raw archive data,
3041 but that doesn't work on a 64 bit host. */
3043 stringbase = raw_armap + count * 8 + 8;
3045 #ifdef CHECK_ARMAP_HASH
3049 /* Double check that I have the hashing algorithm right by making
3050 sure that every symbol can be looked up successfully. */
3052 for (i = 1; i < count; i <<= 1)
3054 BFD_ASSERT (i == count);
3056 raw_ptr = raw_armap + 4;
3057 for (i = 0; i < count; i++, raw_ptr += 8)
3059 unsigned int name_offset, file_offset;
3060 unsigned int hash, rehash, srch;
3062 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3063 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3064 if (file_offset == 0)
3066 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3071 /* See if we can rehash to this location. */
3072 for (srch = (hash + rehash) & (count - 1);
3073 srch != hash && srch != i;
3074 srch = (srch + rehash) & (count - 1))
3075 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3077 BFD_ASSERT (srch == i);
3081 #endif /* CHECK_ARMAP_HASH */
3083 raw_ptr = raw_armap + 4;
3084 for (i = 0; i < count; i++, raw_ptr += 8)
3085 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3086 ++ardata->symdef_count;
3088 symdef_ptr = ((struct symdef *)
3090 ardata->symdef_count * sizeof (struct symdef)));
3094 ardata->symdefs = (carsym *) symdef_ptr;
3096 raw_ptr = raw_armap + 4;
3097 for (i = 0; i < count; i++, raw_ptr += 8)
3099 unsigned int name_offset, file_offset;
3101 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3102 if (file_offset == 0)
3104 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3105 symdef_ptr->s.name = stringbase + name_offset;
3106 symdef_ptr->file_offset = file_offset;
3110 ardata->first_file_filepos = bfd_tell (abfd);
3111 /* Pad to an even boundary. */
3112 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3114 bfd_has_map (abfd) = true;
3119 /* Write out an armap. */
3122 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3124 unsigned int elength;
3126 unsigned int orl_count;
3129 unsigned int hashsize, hashlog;
3130 unsigned int symdefsize;
3132 unsigned int stringsize;
3133 unsigned int mapsize;
3136 struct stat statbuf;
3139 bfd_byte *hashtable;
3143 /* Ultrix appears to use as a hash table size the least power of two
3144 greater than twice the number of entries. */
3145 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3147 hashsize = 1 << hashlog;
3149 symdefsize = hashsize * 8;
3151 stringsize = stridx + padit;
3153 /* Include 8 bytes to store symdefsize and stringsize in output. */
3154 mapsize = symdefsize + stringsize + 8;
3156 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3158 memset ((PTR) &hdr, 0, sizeof hdr);
3160 /* Work out the ECOFF armap name. */
3161 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3162 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3163 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3164 (bfd_header_big_endian (abfd)
3166 : ARMAP_LITTLE_ENDIAN);
3167 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3168 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3169 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3170 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3172 /* Write the timestamp of the archive header to be just a little bit
3173 later than the timestamp of the file, otherwise the linker will
3174 complain that the index is out of date. Actually, the Ultrix
3175 linker just checks the archive name; the GNU linker may check the
3177 stat (abfd->filename, &statbuf);
3178 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3180 /* The DECstation uses zeroes for the uid, gid and mode of the
3182 hdr.ar_uid[0] = '0';
3183 hdr.ar_gid[0] = '0';
3184 hdr.ar_mode[0] = '0';
3186 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3188 hdr.ar_fmag[0] = '`';
3189 hdr.ar_fmag[1] = '\012';
3191 /* Turn all null bytes in the header into spaces. */
3192 for (i = 0; i < sizeof (struct ar_hdr); i++)
3193 if (((char *)(&hdr))[i] == '\0')
3194 (((char *)(&hdr))[i]) = ' ';
3196 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3197 != sizeof (struct ar_hdr))
3200 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3201 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3204 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3208 current = abfd->archive_head;
3210 for (i = 0; i < orl_count; i++)
3212 unsigned int hash, rehash;
3214 /* Advance firstreal to the file position of this archive
3216 if (((bfd *) map[i].pos) != last_elt)
3220 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3221 firstreal += firstreal % 2;
3222 current = current->next;
3224 while (current != (bfd *) map[i].pos);
3229 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3230 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3234 /* The desired slot is already taken. */
3235 for (srch = (hash + rehash) & (hashsize - 1);
3237 srch = (srch + rehash) & (hashsize - 1))
3238 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3241 BFD_ASSERT (srch != hash);
3246 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3247 (PTR) (hashtable + hash * 8));
3248 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3249 (PTR) (hashtable + hash * 8 + 4));
3252 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3255 bfd_release (abfd, hashtable);
3257 /* Now write the strings. */
3258 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3259 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3261 for (i = 0; i < orl_count; i++)
3265 len = strlen (*map[i].name) + 1;
3266 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3270 /* The spec sez this should be a newline. But in order to be
3271 bug-compatible for DECstation ar we use a null. */
3274 if (bfd_write ("", 1, 1, abfd) != 1)
3281 /* See whether this BFD is an archive. If it is, read in the armap
3282 and the extended name table. */
3285 _bfd_ecoff_archive_p (abfd)
3288 struct artdata *tdata_hold;
3289 char armag[SARMAG + 1];
3291 tdata_hold = abfd->tdata.aout_ar_data;
3293 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG)
3295 if (bfd_get_error () != bfd_error_system_call)
3296 bfd_set_error (bfd_error_wrong_format);
3297 return (const bfd_target *) NULL;
3300 if (strncmp (armag, ARMAG, SARMAG) != 0)
3302 bfd_set_error (bfd_error_wrong_format);
3306 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3307 involves a cast, we can't do it as the left operand of
3309 abfd->tdata.aout_ar_data =
3310 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3312 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3314 abfd->tdata.aout_ar_data = tdata_hold;
3315 return (const bfd_target *) NULL;
3318 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3319 bfd_ardata (abfd)->cache = NULL;
3320 bfd_ardata (abfd)->archive_head = NULL;
3321 bfd_ardata (abfd)->symdefs = NULL;
3322 bfd_ardata (abfd)->extended_names = NULL;
3323 bfd_ardata (abfd)->tdata = NULL;
3325 if (_bfd_ecoff_slurp_armap (abfd) == false
3326 || _bfd_ecoff_slurp_extended_name_table (abfd) == false)
3328 bfd_release (abfd, bfd_ardata (abfd));
3329 abfd->tdata.aout_ar_data = tdata_hold;
3330 return (const bfd_target *) NULL;
3333 if (bfd_has_map (abfd))
3337 /* This archive has a map, so we may presume that the contents
3338 are object files. Make sure that if the first file in the
3339 archive can be recognized as an object file, it is for this
3340 target. If not, assume that this is the wrong format. If
3341 the first file is not an object file, somebody is doing
3342 something weird, and we permit it so that ar -t will work. */
3344 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3349 first->target_defaulted = false;
3351 if (bfd_check_format (first, bfd_object)
3352 && first->xvec != abfd->xvec)
3354 (void) bfd_close (first);
3355 bfd_release (abfd, bfd_ardata (abfd));
3356 abfd->tdata.aout_ar_data = tdata_hold;
3357 bfd_set_error (bfd_error_wrong_format);
3361 /* We ought to close first here, but we can't, because we
3362 have no way to remove it from the archive cache. FIXME. */
3369 /* ECOFF linker code. */
3371 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3372 PARAMS ((struct bfd_hash_entry *entry,
3373 struct bfd_hash_table *table,
3374 const char *string));
3375 static boolean ecoff_link_add_archive_symbols
3376 PARAMS ((bfd *, struct bfd_link_info *));
3377 static boolean ecoff_link_check_archive_element
3378 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3379 static boolean ecoff_link_add_object_symbols
3380 PARAMS ((bfd *, struct bfd_link_info *));
3381 static boolean ecoff_link_add_externals
3382 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3384 /* Routine to create an entry in an ECOFF link hash table. */
3386 static struct bfd_hash_entry *
3387 ecoff_link_hash_newfunc (entry, table, string)
3388 struct bfd_hash_entry *entry;
3389 struct bfd_hash_table *table;
3392 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3394 /* Allocate the structure if it has not already been allocated by a
3396 if (ret == (struct ecoff_link_hash_entry *) NULL)
3397 ret = ((struct ecoff_link_hash_entry *)
3398 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3399 if (ret == (struct ecoff_link_hash_entry *) NULL)
3402 /* Call the allocation method of the superclass. */
3403 ret = ((struct ecoff_link_hash_entry *)
3404 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3409 /* Set local fields. */
3415 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3417 return (struct bfd_hash_entry *) ret;
3420 /* Create an ECOFF link hash table. */
3422 struct bfd_link_hash_table *
3423 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3426 struct ecoff_link_hash_table *ret;
3428 ret = ((struct ecoff_link_hash_table *)
3429 bfd_alloc (abfd, sizeof (struct ecoff_link_hash_table)));
3432 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3433 ecoff_link_hash_newfunc))
3436 return (struct bfd_link_hash_table *) NULL;
3441 /* Look up an entry in an ECOFF link hash table. */
3443 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3444 ((struct ecoff_link_hash_entry *) \
3445 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3447 /* Traverse an ECOFF link hash table. */
3449 #define ecoff_link_hash_traverse(table, func, info) \
3450 (bfd_link_hash_traverse \
3452 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3455 /* Get the ECOFF link hash table from the info structure. This is
3458 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3460 /* Given an ECOFF BFD, add symbols to the global hash table as
3464 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3466 struct bfd_link_info *info;
3468 switch (bfd_get_format (abfd))
3471 return ecoff_link_add_object_symbols (abfd, info);
3473 return ecoff_link_add_archive_symbols (abfd, info);
3475 bfd_set_error (bfd_error_wrong_format);
3480 /* Add the symbols from an archive file to the global hash table.
3481 This looks through the undefined symbols, looks each one up in the
3482 archive hash table, and adds any associated object file. We do not
3483 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3484 already have a hash table, so there is no reason to construct
3488 ecoff_link_add_archive_symbols (abfd, info)
3490 struct bfd_link_info *info;
3492 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3493 const bfd_byte *raw_armap;
3494 struct bfd_link_hash_entry **pundef;
3495 unsigned int armap_count;
3496 unsigned int armap_log;
3498 const bfd_byte *hashtable;
3499 const char *stringbase;
3501 if (! bfd_has_map (abfd))
3503 /* An empty archive is a special case. */
3504 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3506 bfd_set_error (bfd_error_no_armap);
3510 /* If we don't have any raw data for this archive, as can happen on
3511 Irix 4.0.5F, we call the generic routine.
3512 FIXME: We should be more clever about this, since someday tdata
3513 may get to something for a generic archive. */
3514 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3515 if (raw_armap == (bfd_byte *) NULL)
3516 return (_bfd_generic_link_add_archive_symbols
3517 (abfd, info, ecoff_link_check_archive_element));
3519 armap_count = bfd_h_get_32 (abfd, raw_armap);
3522 for (i = 1; i < armap_count; i <<= 1)
3524 BFD_ASSERT (i == armap_count);
3526 hashtable = raw_armap + 4;
3527 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3529 /* Look through the list of undefined symbols. */
3530 pundef = &info->hash->undefs;
3531 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3533 struct bfd_link_hash_entry *h;
3534 unsigned int hash, rehash;
3535 unsigned int file_offset;
3541 /* When a symbol is defined, it is not necessarily removed from
3543 if (h->type != bfd_link_hash_undefined
3544 && h->type != bfd_link_hash_common)
3546 /* Remove this entry from the list, for general cleanliness
3547 and because we are going to look through the list again
3548 if we search any more libraries. We can't remove the
3549 entry if it is the tail, because that would lose any
3550 entries we add to the list later on. */
3551 if (*pundef != info->hash->undefs_tail)
3552 *pundef = (*pundef)->next;
3554 pundef = &(*pundef)->next;
3558 /* Native ECOFF linkers do not pull in archive elements merely
3559 to satisfy common definitions, so neither do we. We leave
3560 them on the list, though, in case we are linking against some
3561 other object format. */
3562 if (h->type != bfd_link_hash_undefined)
3564 pundef = &(*pundef)->next;
3568 /* Look for this symbol in the archive hash table. */
3569 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3572 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3573 if (file_offset == 0)
3575 /* Nothing in this slot. */
3576 pundef = &(*pundef)->next;
3580 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3581 if (name[0] != h->root.string[0]
3582 || strcmp (name, h->root.string) != 0)
3587 /* That was the wrong symbol. Try rehashing. */
3589 for (srch = (hash + rehash) & (armap_count - 1);
3591 srch = (srch + rehash) & (armap_count - 1))
3593 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3594 if (file_offset == 0)
3596 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3597 if (name[0] == h->root.string[0]
3598 && strcmp (name, h->root.string) == 0)
3607 pundef = &(*pundef)->next;
3614 element = (*backend->get_elt_at_filepos) (abfd, file_offset);
3615 if (element == (bfd *) NULL)
3618 if (! bfd_check_format (element, bfd_object))
3621 /* Unlike the generic linker, we know that this element provides
3622 a definition for an undefined symbol and we know that we want
3623 to include it. We don't need to check anything. */
3624 if (! (*info->callbacks->add_archive_element) (info, element, name))
3626 if (! ecoff_link_add_object_symbols (element, info))
3629 pundef = &(*pundef)->next;
3635 /* This is called if we used _bfd_generic_link_add_archive_symbols
3636 because we were not dealing with an ECOFF archive. */
3639 ecoff_link_check_archive_element (abfd, info, pneeded)
3641 struct bfd_link_info *info;
3644 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3645 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3646 = backend->debug_swap.swap_ext_in;
3648 bfd_size_type external_ext_size;
3649 PTR external_ext = NULL;
3657 if (! ecoff_slurp_symbolic_header (abfd))
3660 /* If there are no symbols, we don't want it. */
3661 if (bfd_get_symcount (abfd) == 0)
3662 goto successful_return;
3664 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3666 /* Read in the external symbols and external strings. */
3667 external_ext_size = backend->debug_swap.external_ext_size;
3668 esize = symhdr->iextMax * external_ext_size;
3669 external_ext = (PTR) bfd_malloc (esize);
3670 if (external_ext == NULL && esize != 0)
3673 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3674 || bfd_read (external_ext, 1, esize, abfd) != esize)
3677 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3678 if (ssext == NULL && symhdr->issExtMax != 0)
3681 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3682 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd) !=
3683 (bfd_size_type) symhdr->issExtMax))
3686 /* Look through the external symbols to see if they define some
3687 symbol that is currently undefined. */
3688 ext_ptr = (char *) external_ext;
3689 ext_end = ext_ptr + esize;
3690 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3695 struct bfd_link_hash_entry *h;
3697 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3699 /* See if this symbol defines something. */
3700 if (esym.asym.st != stGlobal
3701 && esym.asym.st != stLabel
3702 && esym.asym.st != stProc)
3705 switch (esym.asym.sc)
3729 name = ssext + esym.asym.iss;
3730 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3732 /* Unlike the generic linker, we do not pull in elements because
3733 of common symbols. */
3734 if (h == (struct bfd_link_hash_entry *) NULL
3735 || h->type != bfd_link_hash_undefined)
3738 /* Include this element. */
3739 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3741 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3745 goto successful_return;
3749 if (external_ext != NULL)
3750 free (external_ext);
3755 if (external_ext != NULL)
3756 free (external_ext);
3762 /* Add symbols from an ECOFF object file to the global linker hash
3766 ecoff_link_add_object_symbols (abfd, info)
3768 struct bfd_link_info *info;
3771 bfd_size_type external_ext_size;
3772 PTR external_ext = NULL;
3777 if (! ecoff_slurp_symbolic_header (abfd))
3780 /* If there are no symbols, we don't want it. */
3781 if (bfd_get_symcount (abfd) == 0)
3784 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3786 /* Read in the external symbols and external strings. */
3787 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3788 esize = symhdr->iextMax * external_ext_size;
3789 external_ext = (PTR) bfd_malloc (esize);
3790 if (external_ext == NULL && esize != 0)
3793 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3794 || bfd_read (external_ext, 1, esize, abfd) != esize)
3797 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3798 if (ssext == NULL && symhdr->issExtMax != 0)
3801 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3802 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd)
3803 != (bfd_size_type) symhdr->issExtMax))
3806 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3810 if (external_ext != NULL)
3811 free (external_ext);
3817 if (external_ext != NULL)
3818 free (external_ext);
3822 /* Add the external symbols of an object file to the global linker
3823 hash table. The external symbols and strings we are passed are
3824 just allocated on the stack, and will be discarded. We must
3825 explicitly save any information we may need later on in the link.
3826 We do not want to read the external symbol information again. */
3829 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3831 struct bfd_link_info *info;
3835 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3836 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3837 = backend->debug_swap.swap_ext_in;
3838 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3839 unsigned long ext_count;
3840 struct ecoff_link_hash_entry **sym_hash;
3844 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3846 sym_hash = ((struct ecoff_link_hash_entry **)
3848 ext_count * sizeof (struct bfd_link_hash_entry *)));
3851 ecoff_data (abfd)->sym_hashes = sym_hash;
3853 ext_ptr = (char *) external_ext;
3854 ext_end = ext_ptr + ext_count * external_ext_size;
3855 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3862 struct ecoff_link_hash_entry *h;
3866 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3868 /* Skip debugging symbols. */
3870 switch (esym.asym.st)
3886 /* Get the information for this symbol. */
3887 value = esym.asym.value;
3888 switch (esym.asym.sc)
3908 section = bfd_make_section_old_way (abfd, ".text");
3909 value -= section->vma;
3912 section = bfd_make_section_old_way (abfd, ".data");
3913 value -= section->vma;
3916 section = bfd_make_section_old_way (abfd, ".bss");
3917 value -= section->vma;
3920 section = bfd_abs_section_ptr;
3923 section = bfd_und_section_ptr;
3926 section = bfd_make_section_old_way (abfd, ".sdata");
3927 value -= section->vma;
3930 section = bfd_make_section_old_way (abfd, ".sbss");
3931 value -= section->vma;
3934 section = bfd_make_section_old_way (abfd, ".rdata");
3935 value -= section->vma;
3938 if (value > ecoff_data (abfd)->gp_size)
3940 section = bfd_com_section_ptr;
3945 if (ecoff_scom_section.name == NULL)
3947 /* Initialize the small common section. */
3948 ecoff_scom_section.name = SCOMMON;
3949 ecoff_scom_section.flags = SEC_IS_COMMON;
3950 ecoff_scom_section.output_section = &ecoff_scom_section;
3951 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3952 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3953 ecoff_scom_symbol.name = SCOMMON;
3954 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3955 ecoff_scom_symbol.section = &ecoff_scom_section;
3956 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3958 section = &ecoff_scom_section;
3961 section = bfd_und_section_ptr;
3964 section = bfd_make_section_old_way (abfd, ".init");
3965 value -= section->vma;
3968 section = bfd_make_section_old_way (abfd, ".fini");
3969 value -= section->vma;
3972 section = bfd_make_section_old_way (abfd, ".rconst");
3973 value -= section->vma;
3977 if (section == (asection *) NULL)
3980 name = ssext + esym.asym.iss;
3983 if (! (_bfd_generic_link_add_one_symbol
3985 esym.weakext ? BSF_WEAK : BSF_GLOBAL,
3986 section, value, (const char *) NULL, true, true,
3987 (struct bfd_link_hash_entry **) &h)))
3992 /* If we are building an ECOFF hash table, save the external
3993 symbol information. */
3994 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3996 if (h->abfd == (bfd *) NULL
3997 || (! bfd_is_und_section (section)
3998 && (! bfd_is_com_section (section)
3999 || (h->root.type != bfd_link_hash_defined
4000 && h->root.type != bfd_link_hash_defweak))))
4006 /* Remember whether this symbol was small undefined. */
4007 if (esym.asym.sc == scSUndefined)
4010 /* If this symbol was ever small undefined, it needs to wind
4011 up in a GP relative section. We can't control the
4012 section of a defined symbol, but we can control the
4013 section of a common symbol. This case is actually needed
4014 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
4016 && h->root.type == bfd_link_hash_common
4017 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
4019 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
4021 h->root.u.c.p->section->flags = SEC_ALLOC;
4022 if (h->esym.asym.sc == scCommon)
4023 h->esym.asym.sc = scSCommon;
4031 /* ECOFF final link routines. */
4033 static boolean ecoff_final_link_debug_accumulate
4034 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4036 static boolean ecoff_link_write_external
4037 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4038 static boolean ecoff_indirect_link_order
4039 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4040 struct bfd_link_order *));
4041 static boolean ecoff_reloc_link_order
4042 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4043 struct bfd_link_order *));
4045 /* Structure used to pass information to ecoff_link_write_external. */
4050 struct bfd_link_info *info;
4053 /* ECOFF final link routine. This looks through all the input BFDs
4054 and gathers together all the debugging information, and then
4055 processes all the link order information. This may cause it to
4056 close and reopen some input BFDs; I'll see how bad this is. */
4059 _bfd_ecoff_bfd_final_link (abfd, info)
4061 struct bfd_link_info *info;
4063 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4064 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4067 register bfd *input_bfd;
4069 struct bfd_link_order *p;
4070 struct extsym_info einfo;
4072 /* We accumulate the debugging information counts in the symbolic
4074 symhdr = &debug->symbolic_header;
4076 symhdr->ilineMax = 0;
4080 symhdr->isymMax = 0;
4081 symhdr->ioptMax = 0;
4082 symhdr->iauxMax = 0;
4084 symhdr->issExtMax = 0;
4087 symhdr->iextMax = 0;
4089 /* We accumulate the debugging information itself in the debug_info
4092 debug->external_dnr = NULL;
4093 debug->external_pdr = NULL;
4094 debug->external_sym = NULL;
4095 debug->external_opt = NULL;
4096 debug->external_aux = NULL;
4098 debug->ssext = debug->ssext_end = NULL;
4099 debug->external_fdr = NULL;
4100 debug->external_rfd = NULL;
4101 debug->external_ext = debug->external_ext_end = NULL;
4103 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4104 if (handle == (PTR) NULL)
4107 /* Accumulate the debugging symbols from each input BFD. */
4108 for (input_bfd = info->input_bfds;
4109 input_bfd != (bfd *) NULL;
4110 input_bfd = input_bfd->link_next)
4114 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4116 /* Abitrarily set the symbolic header vstamp to the vstamp
4117 of the first object file in the link. */
4118 if (symhdr->vstamp == 0)
4120 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4121 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4125 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4126 debug, &backend->debug_swap,
4131 /* Combine the register masks. */
4132 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4133 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4134 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4135 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4136 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4137 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4140 /* Write out the external symbols. */
4143 ecoff_link_hash_traverse (ecoff_hash_table (info),
4144 ecoff_link_write_external,
4147 if (info->relocateable)
4149 /* We need to make a pass over the link_orders to count up the
4150 number of relocations we will need to output, so that we know
4151 how much space they will take up. */
4152 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4155 for (p = o->link_order_head;
4156 p != (struct bfd_link_order *) NULL;
4158 if (p->type == bfd_indirect_link_order)
4159 o->reloc_count += p->u.indirect.section->reloc_count;
4160 else if (p->type == bfd_section_reloc_link_order
4161 || p->type == bfd_symbol_reloc_link_order)
4166 /* Compute the reloc and symbol file positions. */
4167 ecoff_compute_reloc_file_positions (abfd);
4169 /* Write out the debugging information. */
4170 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4171 &backend->debug_swap, info,
4172 ecoff_data (abfd)->sym_filepos))
4175 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4177 if (info->relocateable)
4179 /* Now reset the reloc_count field of the sections in the output
4180 BFD to 0, so that we can use them to keep track of how many
4181 relocs we have output thus far. */
4182 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4186 /* Get a value for the GP register. */
4187 if (ecoff_data (abfd)->gp == 0)
4189 struct bfd_link_hash_entry *h;
4191 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4192 if (h != (struct bfd_link_hash_entry *) NULL
4193 && h->type == bfd_link_hash_defined)
4194 ecoff_data (abfd)->gp = (h->u.def.value
4195 + h->u.def.section->output_section->vma
4196 + h->u.def.section->output_offset);
4197 else if (info->relocateable)
4201 /* Make up a value. */
4203 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4206 && (strcmp (o->name, _SBSS) == 0
4207 || strcmp (o->name, _SDATA) == 0
4208 || strcmp (o->name, _LIT4) == 0
4209 || strcmp (o->name, _LIT8) == 0
4210 || strcmp (o->name, _LITA) == 0))
4213 ecoff_data (abfd)->gp = lo + 0x8000;
4217 /* If the relocate_section function needs to do a reloc
4218 involving the GP value, it should make a reloc_dangerous
4219 callback to warn that GP is not defined. */
4223 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4225 for (p = o->link_order_head;
4226 p != (struct bfd_link_order *) NULL;
4229 if (p->type == bfd_indirect_link_order
4230 && (bfd_get_flavour (p->u.indirect.section->owner)
4231 == bfd_target_ecoff_flavour))
4233 if (! ecoff_indirect_link_order (abfd, info, o, p))
4236 else if (p->type == bfd_section_reloc_link_order
4237 || p->type == bfd_symbol_reloc_link_order)
4239 if (! ecoff_reloc_link_order (abfd, info, o, p))
4244 if (! _bfd_default_link_order (abfd, info, o, p))
4250 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4252 ecoff_data (abfd)->linker = true;
4257 /* Accumulate the debugging information for an input BFD into the
4258 output BFD. This must read in the symbolic information of the
4262 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4265 struct bfd_link_info *info;
4268 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4269 const struct ecoff_debug_swap * const swap =
4270 &ecoff_backend (input_bfd)->debug_swap;
4271 HDRR *symhdr = &debug->symbolic_header;
4274 #define READ(ptr, offset, count, size, type) \
4275 if (symhdr->count == 0) \
4276 debug->ptr = NULL; \
4279 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
4280 if (debug->ptr == NULL) \
4283 goto return_something; \
4285 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4287 || (bfd_read (debug->ptr, size, symhdr->count, \
4288 input_bfd) != size * symhdr->count)) \
4291 goto return_something; \
4295 /* If raw_syments is not NULL, then the data was already by read by
4296 _bfd_ecoff_slurp_symbolic_info. */
4297 if (ecoff_data (input_bfd)->raw_syments == NULL)
4299 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4301 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4302 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4303 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4304 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4305 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4307 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4308 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4309 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4313 /* We do not read the external strings or the external symbols. */
4315 ret = (bfd_ecoff_debug_accumulate
4316 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4317 &ecoff_backend (output_bfd)->debug_swap,
4318 input_bfd, debug, swap, info));
4321 if (ecoff_data (input_bfd)->raw_syments == NULL)
4323 if (debug->line != NULL)
4325 if (debug->external_dnr != NULL)
4326 free (debug->external_dnr);
4327 if (debug->external_pdr != NULL)
4328 free (debug->external_pdr);
4329 if (debug->external_sym != NULL)
4330 free (debug->external_sym);
4331 if (debug->external_opt != NULL)
4332 free (debug->external_opt);
4333 if (debug->external_aux != NULL)
4334 free (debug->external_aux);
4335 if (debug->ss != NULL)
4337 if (debug->external_fdr != NULL)
4338 free (debug->external_fdr);
4339 if (debug->external_rfd != NULL)
4340 free (debug->external_rfd);
4342 /* Make sure we don't accidentally follow one of these pointers
4343 into freed memory. */
4345 debug->external_dnr = NULL;
4346 debug->external_pdr = NULL;
4347 debug->external_sym = NULL;
4348 debug->external_opt = NULL;
4349 debug->external_aux = NULL;
4351 debug->external_fdr = NULL;
4352 debug->external_rfd = NULL;
4358 /* Put out information for an external symbol. These come only from
4362 ecoff_link_write_external (h, data)
4363 struct ecoff_link_hash_entry *h;
4366 struct extsym_info *einfo = (struct extsym_info *) data;
4367 bfd *output_bfd = einfo->abfd;
4370 /* We need to check if this symbol is being stripped. */
4371 if (h->root.type == bfd_link_hash_undefined
4372 || h->root.type == bfd_link_hash_undefweak)
4374 else if (einfo->info->strip == strip_all
4375 || (einfo->info->strip == strip_some
4376 && bfd_hash_lookup (einfo->info->keep_hash,
4377 h->root.root.string,
4378 false, false) == NULL))
4383 if (strip || h->written)
4386 if (h->abfd == (bfd *) NULL)
4389 h->esym.cobol_main = 0;
4390 h->esym.weakext = 0;
4391 h->esym.reserved = 0;
4392 h->esym.ifd = ifdNil;
4393 h->esym.asym.value = 0;
4394 h->esym.asym.st = stGlobal;
4396 if (h->root.type != bfd_link_hash_defined
4397 && h->root.type != bfd_link_hash_defweak)
4398 h->esym.asym.sc = scAbs;
4401 asection *output_section;
4404 output_section = h->root.u.def.section->output_section;
4405 name = bfd_section_name (output_section->owner, output_section);
4407 if (strcmp (name, _TEXT) == 0)
4408 h->esym.asym.sc = scText;
4409 else if (strcmp (name, _DATA) == 0)
4410 h->esym.asym.sc = scData;
4411 else if (strcmp (name, _SDATA) == 0)
4412 h->esym.asym.sc = scSData;
4413 else if (strcmp (name, _RDATA) == 0)
4414 h->esym.asym.sc = scRData;
4415 else if (strcmp (name, _BSS) == 0)
4416 h->esym.asym.sc = scBss;
4417 else if (strcmp (name, _SBSS) == 0)
4418 h->esym.asym.sc = scSBss;
4419 else if (strcmp (name, _INIT) == 0)
4420 h->esym.asym.sc = scInit;
4421 else if (strcmp (name, _FINI) == 0)
4422 h->esym.asym.sc = scFini;
4423 else if (strcmp (name, _PDATA) == 0)
4424 h->esym.asym.sc = scPData;
4425 else if (strcmp (name, _XDATA) == 0)
4426 h->esym.asym.sc = scXData;
4427 else if (strcmp (name, _RCONST) == 0)
4428 h->esym.asym.sc = scRConst;
4430 h->esym.asym.sc = scAbs;
4433 h->esym.asym.reserved = 0;
4434 h->esym.asym.index = indexNil;
4436 else if (h->esym.ifd != -1)
4438 struct ecoff_debug_info *debug;
4440 /* Adjust the FDR index for the symbol by that used for the
4442 debug = &ecoff_data (h->abfd)->debug_info;
4443 BFD_ASSERT (h->esym.ifd >= 0
4444 && h->esym.ifd < debug->symbolic_header.ifdMax);
4445 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4448 switch (h->root.type)
4451 case bfd_link_hash_new:
4453 case bfd_link_hash_undefined:
4454 case bfd_link_hash_undefweak:
4455 if (h->esym.asym.sc != scUndefined
4456 && h->esym.asym.sc != scSUndefined)
4457 h->esym.asym.sc = scUndefined;
4459 case bfd_link_hash_defined:
4460 case bfd_link_hash_defweak:
4461 if (h->esym.asym.sc == scUndefined
4462 || h->esym.asym.sc == scSUndefined)
4463 h->esym.asym.sc = scAbs;
4464 else if (h->esym.asym.sc == scCommon)
4465 h->esym.asym.sc = scBss;
4466 else if (h->esym.asym.sc == scSCommon)
4467 h->esym.asym.sc = scSBss;
4468 h->esym.asym.value = (h->root.u.def.value
4469 + h->root.u.def.section->output_section->vma
4470 + h->root.u.def.section->output_offset);
4472 case bfd_link_hash_common:
4473 if (h->esym.asym.sc != scCommon
4474 && h->esym.asym.sc != scSCommon)
4475 h->esym.asym.sc = scCommon;
4476 h->esym.asym.value = h->root.u.c.size;
4478 case bfd_link_hash_indirect:
4479 case bfd_link_hash_warning:
4480 /* FIXME: Ignore these for now. The circumstances under which
4481 they should be written out are not clear to me. */
4485 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4487 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4490 return (bfd_ecoff_debug_one_external
4491 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4492 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4496 /* Relocate and write an ECOFF section into an ECOFF output file. */
4499 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4501 struct bfd_link_info *info;
4502 asection *output_section;
4503 struct bfd_link_order *link_order;
4505 asection *input_section;
4507 struct ecoff_section_tdata *section_tdata;
4508 bfd_size_type raw_size;
4509 bfd_size_type cooked_size;
4510 bfd_byte *contents = NULL;
4511 bfd_size_type external_reloc_size;
4512 bfd_size_type external_relocs_size;
4513 PTR external_relocs = NULL;
4515 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4517 if (link_order->size == 0)
4520 input_section = link_order->u.indirect.section;
4521 input_bfd = input_section->owner;
4522 section_tdata = ecoff_section_data (input_bfd, input_section);
4524 raw_size = input_section->_raw_size;
4525 cooked_size = input_section->_cooked_size;
4526 if (cooked_size == 0)
4527 cooked_size = raw_size;
4529 BFD_ASSERT (input_section->output_section == output_section);
4530 BFD_ASSERT (input_section->output_offset == link_order->offset);
4531 BFD_ASSERT (cooked_size == link_order->size);
4533 /* Get the section contents. We allocate memory for the larger of
4534 the size before relocating and the size after relocating. */
4535 contents = (bfd_byte *) bfd_malloc (raw_size >= cooked_size
4537 : (size_t) cooked_size);
4538 if (contents == NULL && raw_size != 0)
4541 /* If we are relaxing, the contents may have already been read into
4542 memory, in which case we copy them into our new buffer. We don't
4543 simply reuse the old buffer in case cooked_size > raw_size. */
4544 if (section_tdata != (struct ecoff_section_tdata *) NULL
4545 && section_tdata->contents != (bfd_byte *) NULL)
4546 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4549 if (! bfd_get_section_contents (input_bfd, input_section,
4551 (file_ptr) 0, raw_size))
4555 /* Get the relocs. If we are relaxing MIPS code, they will already
4556 have been read in. Otherwise, we read them in now. */
4557 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4558 external_relocs_size = external_reloc_size * input_section->reloc_count;
4560 if (section_tdata != (struct ecoff_section_tdata *) NULL
4561 && section_tdata->external_relocs != NULL)
4562 external_relocs = section_tdata->external_relocs;
4565 external_relocs = (PTR) bfd_malloc ((size_t) external_relocs_size);
4566 if (external_relocs == NULL && external_relocs_size != 0)
4569 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4570 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4571 != external_relocs_size))
4575 /* Relocate the section contents. */
4576 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4577 (output_bfd, info, input_bfd, input_section, contents,
4581 /* Write out the relocated section. */
4582 if (! bfd_set_section_contents (output_bfd,
4585 input_section->output_offset,
4589 /* If we are producing relocateable output, the relocs were
4590 modified, and we write them out now. We use the reloc_count
4591 field of output_section to keep track of the number of relocs we
4592 have output so far. */
4593 if (info->relocateable)
4595 if (bfd_seek (output_bfd,
4596 (output_section->rel_filepos +
4597 output_section->reloc_count * external_reloc_size),
4599 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4600 != external_relocs_size))
4602 output_section->reloc_count += input_section->reloc_count;
4605 if (contents != NULL)
4607 if (external_relocs != NULL && section_tdata == NULL)
4608 free (external_relocs);
4612 if (contents != NULL)
4614 if (external_relocs != NULL && section_tdata == NULL)
4615 free (external_relocs);
4619 /* Generate a reloc when linking an ECOFF file. This is a reloc
4620 requested by the linker, and does come from any input file. This
4621 is used to build constructor and destructor tables when linking
4625 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4627 struct bfd_link_info *info;
4628 asection *output_section;
4629 struct bfd_link_order *link_order;
4631 enum bfd_link_order_type type;
4635 struct internal_reloc in;
4636 bfd_size_type external_reloc_size;
4640 type = link_order->type;
4642 addend = link_order->u.reloc.p->addend;
4644 /* We set up an arelent to pass to the backend adjust_reloc_out
4646 rel.address = link_order->offset;
4648 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4651 bfd_set_error (bfd_error_bad_value);
4655 if (type == bfd_section_reloc_link_order)
4657 section = link_order->u.reloc.p->u.section;
4658 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4662 struct bfd_link_hash_entry *h;
4664 /* Treat a reloc against a defined symbol as though it were
4665 actually against the section. */
4666 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4667 link_order->u.reloc.p->u.name,
4668 false, false, false);
4670 && (h->type == bfd_link_hash_defined
4671 || h->type == bfd_link_hash_defweak))
4673 type = bfd_section_reloc_link_order;
4674 section = h->u.def.section->output_section;
4675 /* It seems that we ought to add the symbol value to the
4676 addend here, but in practice it has already been added
4677 because it was passed to constructor_callback. */
4678 addend += section->vma + h->u.def.section->output_offset;
4682 /* We can't set up a reloc against a symbol correctly,
4683 because we have no asymbol structure. Currently no
4684 adjust_reloc_out routine cares. */
4685 rel.sym_ptr_ptr = (asymbol **) NULL;
4689 /* All ECOFF relocs are in-place. Put the addend into the object
4692 BFD_ASSERT (rel.howto->partial_inplace);
4696 bfd_reloc_status_type rstat;
4700 size = bfd_get_reloc_size (rel.howto);
4701 buf = (bfd_byte *) bfd_zmalloc (size);
4702 if (buf == (bfd_byte *) NULL)
4704 rstat = _bfd_relocate_contents (rel.howto, output_bfd, addend, buf);
4710 case bfd_reloc_outofrange:
4712 case bfd_reloc_overflow:
4713 if (! ((*info->callbacks->reloc_overflow)
4715 (link_order->type == bfd_section_reloc_link_order
4716 ? bfd_section_name (output_bfd, section)
4717 : link_order->u.reloc.p->u.name),
4718 rel.howto->name, addend, (bfd *) NULL,
4719 (asection *) NULL, (bfd_vma) 0)))
4726 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4727 (file_ptr) link_order->offset, size);
4735 /* Move the information into a internal_reloc structure. */
4736 in.r_vaddr = (rel.address
4737 + bfd_get_section_vma (output_bfd, output_section));
4738 in.r_type = rel.howto->type;
4740 if (type == bfd_symbol_reloc_link_order)
4742 struct ecoff_link_hash_entry *h;
4744 h = ((struct ecoff_link_hash_entry *)
4745 bfd_wrapped_link_hash_lookup (output_bfd, info,
4746 link_order->u.reloc.p->u.name,
4747 false, false, true));
4748 if (h != (struct ecoff_link_hash_entry *) NULL
4750 in.r_symndx = h->indx;
4753 if (! ((*info->callbacks->unattached_reloc)
4754 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4755 (asection *) NULL, (bfd_vma) 0)))
4765 name = bfd_get_section_name (output_bfd, section);
4766 if (strcmp (name, ".text") == 0)
4767 in.r_symndx = RELOC_SECTION_TEXT;
4768 else if (strcmp (name, ".rdata") == 0)
4769 in.r_symndx = RELOC_SECTION_RDATA;
4770 else if (strcmp (name, ".data") == 0)
4771 in.r_symndx = RELOC_SECTION_DATA;
4772 else if (strcmp (name, ".sdata") == 0)
4773 in.r_symndx = RELOC_SECTION_SDATA;
4774 else if (strcmp (name, ".sbss") == 0)
4775 in.r_symndx = RELOC_SECTION_SBSS;
4776 else if (strcmp (name, ".bss") == 0)
4777 in.r_symndx = RELOC_SECTION_BSS;
4778 else if (strcmp (name, ".init") == 0)
4779 in.r_symndx = RELOC_SECTION_INIT;
4780 else if (strcmp (name, ".lit8") == 0)
4781 in.r_symndx = RELOC_SECTION_LIT8;
4782 else if (strcmp (name, ".lit4") == 0)
4783 in.r_symndx = RELOC_SECTION_LIT4;
4784 else if (strcmp (name, ".xdata") == 0)
4785 in.r_symndx = RELOC_SECTION_XDATA;
4786 else if (strcmp (name, ".pdata") == 0)
4787 in.r_symndx = RELOC_SECTION_PDATA;
4788 else if (strcmp (name, ".fini") == 0)
4789 in.r_symndx = RELOC_SECTION_FINI;
4790 else if (strcmp (name, ".lita") == 0)
4791 in.r_symndx = RELOC_SECTION_LITA;
4792 else if (strcmp (name, "*ABS*") == 0)
4793 in.r_symndx = RELOC_SECTION_ABS;
4794 else if (strcmp (name, ".rconst") == 0)
4795 in.r_symndx = RELOC_SECTION_RCONST;
4801 /* Let the BFD backend adjust the reloc. */
4802 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4804 /* Get some memory and swap out the reloc. */
4805 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4806 rbuf = (bfd_byte *) bfd_malloc ((size_t) external_reloc_size);
4807 if (rbuf == (bfd_byte *) NULL)
4810 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4812 ok = (bfd_seek (output_bfd,
4813 (output_section->rel_filepos +
4814 output_section->reloc_count * external_reloc_size),
4816 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4817 == external_reloc_size));
4820 ++output_section->reloc_count;