* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / bfd / ecoff.c
blobb76266d26f7de196ac081b1c28a0c126c00d28ca
1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Original version by Per Bothner.
6 Full support added by Ian Lance Taylor, ian@cygnus.com.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "aout/ar.h"
30 #include "aout/stab_gnu.h"
32 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
33 some other stuff which we don't want and which conflicts with stuff
34 we do want. */
35 #include "libaout.h"
36 #include "aout/aout64.h"
37 #undef N_ABS
38 #undef exec_hdr
39 #undef obj_sym_filepos
41 #include "coff/internal.h"
42 #include "coff/sym.h"
43 #include "coff/symconst.h"
44 #include "coff/ecoff.h"
45 #include "libcoff.h"
46 #include "libecoff.h"
47 #include "libiberty.h"
49 #define streq(a, b) (strcmp ((a), (b)) == 0)
50 #define strneq(a, b, n) (strncmp ((a), (b), (n)) == 0)
53 /* This stuff is somewhat copied from coffcode.h. */
54 static asection bfd_debug_section =
56 /* name, id, index, next, prev, flags, user_set_vma, */
57 "*DEBUG*", 0, 0, NULL, NULL, 0, 0,
58 /* linker_mark, linker_has_input, gc_mark, compress_status, */
59 0, 0, 1, 0,
60 /* segment_mark, sec_info_type, use_rela_p, */
61 0, 0, 0,
62 /* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5, */
63 0, 0, 0, 0, 0, 0,
64 /* vma, lma, size, rawsize, compressed_size, relax, relax_count, */
65 0, 0, 0, 0, 0, 0, 0,
66 /* output_offset, output_section, alignment_power, */
67 0, NULL, 0,
68 /* relocation, orelocation, reloc_count, filepos, rel_filepos, */
69 NULL, NULL, 0, 0, 0,
70 /* line_filepos, userdata, contents, lineno, lineno_count, */
71 0, NULL, NULL, NULL, 0,
72 /* entsize, kept_section, moving_line_filepos, */
73 0, NULL, 0,
74 /* target_index, used_by_bfd, constructor_chain, owner, */
75 0, NULL, NULL, NULL,
76 /* flag_info, */
77 NULL,
78 /* symbol, */
79 NULL,
80 /* symbol_ptr_ptr, */
81 NULL,
82 /* map_head, map_tail */
83 { NULL }, { NULL }
86 /* Create an ECOFF object. */
88 bfd_boolean
89 _bfd_ecoff_mkobject (bfd *abfd)
91 bfd_size_type amt = sizeof (ecoff_data_type);
93 abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
94 if (abfd->tdata.ecoff_obj_data == NULL)
95 return FALSE;
97 return TRUE;
100 /* This is a hook called by coff_real_object_p to create any backend
101 specific information. */
103 void *
104 _bfd_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr)
106 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
107 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
108 ecoff_data_type *ecoff;
110 if (! _bfd_ecoff_mkobject (abfd))
111 return NULL;
113 ecoff = ecoff_data (abfd);
114 ecoff->gp_size = 8;
115 ecoff->sym_filepos = internal_f->f_symptr;
117 if (internal_a != NULL)
119 int i;
121 ecoff->text_start = internal_a->text_start;
122 ecoff->text_end = internal_a->text_start + internal_a->tsize;
123 ecoff->gp = internal_a->gp_value;
124 ecoff->gprmask = internal_a->gprmask;
125 for (i = 0; i < 4; i++)
126 ecoff->cprmask[i] = internal_a->cprmask[i];
127 ecoff->fprmask = internal_a->fprmask;
128 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
129 abfd->flags |= D_PAGED;
130 else
131 abfd->flags &=~ D_PAGED;
134 /* It turns out that no special action is required by the MIPS or
135 Alpha ECOFF backends. They have different information in the
136 a.out header, but we just copy it all (e.g., gprmask, cprmask and
137 fprmask) and let the swapping routines ensure that only relevant
138 information is written out. */
140 return (void *) ecoff;
143 /* Initialize a new section. */
145 bfd_boolean
146 _bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
148 unsigned int i;
149 static struct
151 const char * name;
152 flagword flags;
154 section_flags [] =
156 { _TEXT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
157 { _INIT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
158 { _FINI, SEC_ALLOC | SEC_CODE | SEC_LOAD },
159 { _DATA, SEC_ALLOC | SEC_DATA | SEC_LOAD },
160 { _SDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD },
161 { _RDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
162 { _LIT8, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
163 { _LIT4, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
164 { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
165 { _PDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
166 { _BSS, SEC_ALLOC},
167 { _SBSS, SEC_ALLOC},
168 /* An Irix 4 shared libary. */
169 { _LIB, SEC_COFF_SHARED_LIBRARY}
172 section->alignment_power = 4;
174 for (i = 0; i < ARRAY_SIZE (section_flags); i++)
175 if (streq (section->name, section_flags[i].name))
177 section->flags |= section_flags[i].flags;
178 break;
182 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
183 uncertain about .init on some systems and I don't know how shared
184 libraries work. */
186 return _bfd_generic_new_section_hook (abfd, section);
189 /* Determine the machine architecture and type. This is called from
190 the generic COFF routines. It is the inverse of ecoff_get_magic,
191 below. This could be an ECOFF backend routine, with one version
192 for each target, but there aren't all that many ECOFF targets. */
194 bfd_boolean
195 _bfd_ecoff_set_arch_mach_hook (bfd *abfd, void * filehdr)
197 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
198 enum bfd_architecture arch;
199 unsigned long mach;
201 switch (internal_f->f_magic)
203 case MIPS_MAGIC_1:
204 case MIPS_MAGIC_LITTLE:
205 case MIPS_MAGIC_BIG:
206 arch = bfd_arch_mips;
207 mach = bfd_mach_mips3000;
208 break;
210 case MIPS_MAGIC_LITTLE2:
211 case MIPS_MAGIC_BIG2:
212 /* MIPS ISA level 2: the r6000. */
213 arch = bfd_arch_mips;
214 mach = bfd_mach_mips6000;
215 break;
217 case MIPS_MAGIC_LITTLE3:
218 case MIPS_MAGIC_BIG3:
219 /* MIPS ISA level 3: the r4000. */
220 arch = bfd_arch_mips;
221 mach = bfd_mach_mips4000;
222 break;
224 case ALPHA_MAGIC:
225 arch = bfd_arch_alpha;
226 mach = 0;
227 break;
229 default:
230 arch = bfd_arch_obscure;
231 mach = 0;
232 break;
235 return bfd_default_set_arch_mach (abfd, arch, mach);
238 bfd_boolean
239 _bfd_ecoff_no_long_sections (abfd, enable)
240 bfd *abfd;
241 int enable;
243 (void) abfd;
244 (void) enable;
245 return FALSE;
248 /* Get the magic number to use based on the architecture and machine.
249 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
251 static int
252 ecoff_get_magic (bfd *abfd)
254 int big, little;
256 switch (bfd_get_arch (abfd))
258 case bfd_arch_mips:
259 switch (bfd_get_mach (abfd))
261 default:
262 case 0:
263 case bfd_mach_mips3000:
264 big = MIPS_MAGIC_BIG;
265 little = MIPS_MAGIC_LITTLE;
266 break;
268 case bfd_mach_mips6000:
269 big = MIPS_MAGIC_BIG2;
270 little = MIPS_MAGIC_LITTLE2;
271 break;
273 case bfd_mach_mips4000:
274 big = MIPS_MAGIC_BIG3;
275 little = MIPS_MAGIC_LITTLE3;
276 break;
279 return bfd_big_endian (abfd) ? big : little;
281 case bfd_arch_alpha:
282 return ALPHA_MAGIC;
284 default:
285 abort ();
286 return 0;
290 /* Get the section s_flags to use for a section. */
292 static long
293 ecoff_sec_to_styp_flags (const char *name, flagword flags)
295 unsigned int i;
296 static struct
298 const char * name;
299 long flags;
301 styp_flags [] =
303 { _TEXT, STYP_TEXT },
304 { _DATA, STYP_DATA },
305 { _SDATA, STYP_SDATA },
306 { _RDATA, STYP_RDATA },
307 { _LITA, STYP_LITA },
308 { _LIT8, STYP_LIT8 },
309 { _LIT4, STYP_LIT4 },
310 { _BSS, STYP_BSS },
311 { _SBSS, STYP_SBSS },
312 { _INIT, STYP_ECOFF_INIT },
313 { _FINI, STYP_ECOFF_FINI },
314 { _PDATA, STYP_PDATA },
315 { _XDATA, STYP_XDATA },
316 { _LIB, STYP_ECOFF_LIB },
317 { _GOT, STYP_GOT },
318 { _HASH, STYP_HASH },
319 { _DYNAMIC, STYP_DYNAMIC },
320 { _LIBLIST, STYP_LIBLIST },
321 { _RELDYN, STYP_RELDYN },
322 { _CONFLIC, STYP_CONFLIC },
323 { _DYNSTR, STYP_DYNSTR },
324 { _DYNSYM, STYP_DYNSYM },
325 { _RCONST, STYP_RCONST }
327 long styp = 0;
329 for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
330 if (streq (name, styp_flags[i].name))
332 styp = styp_flags[i].flags;
333 break;
336 if (styp == 0)
338 if (streq (name, _COMMENT))
340 styp = STYP_COMMENT;
341 flags &=~ SEC_NEVER_LOAD;
343 else if (flags & SEC_CODE)
344 styp = STYP_TEXT;
345 else if (flags & SEC_DATA)
346 styp = STYP_DATA;
347 else if (flags & SEC_READONLY)
348 styp = STYP_RDATA;
349 else if (flags & SEC_LOAD)
350 styp = STYP_REG;
351 else
352 styp = STYP_BSS;
355 if (flags & SEC_NEVER_LOAD)
356 styp |= STYP_NOLOAD;
358 return styp;
361 /* Get the BFD flags to use for a section. */
363 bfd_boolean
364 _bfd_ecoff_styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
365 void * hdr,
366 const char *name ATTRIBUTE_UNUSED,
367 asection *section ATTRIBUTE_UNUSED,
368 flagword * flags_ptr)
370 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
371 long styp_flags = internal_s->s_flags;
372 flagword sec_flags = 0;
374 if (styp_flags & STYP_NOLOAD)
375 sec_flags |= SEC_NEVER_LOAD;
377 /* For 386 COFF, at least, an unloadable text or data section is
378 actually a shared library section. */
379 if ((styp_flags & STYP_TEXT)
380 || (styp_flags & STYP_ECOFF_INIT)
381 || (styp_flags & STYP_ECOFF_FINI)
382 || (styp_flags & STYP_DYNAMIC)
383 || (styp_flags & STYP_LIBLIST)
384 || (styp_flags & STYP_RELDYN)
385 || styp_flags == STYP_CONFLIC
386 || (styp_flags & STYP_DYNSTR)
387 || (styp_flags & STYP_DYNSYM)
388 || (styp_flags & STYP_HASH))
390 if (sec_flags & SEC_NEVER_LOAD)
391 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
392 else
393 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
395 else if ((styp_flags & STYP_DATA)
396 || (styp_flags & STYP_RDATA)
397 || (styp_flags & STYP_SDATA)
398 || styp_flags == STYP_PDATA
399 || styp_flags == STYP_XDATA
400 || (styp_flags & STYP_GOT)
401 || styp_flags == STYP_RCONST)
403 if (sec_flags & SEC_NEVER_LOAD)
404 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
405 else
406 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
407 if ((styp_flags & STYP_RDATA)
408 || styp_flags == STYP_PDATA
409 || styp_flags == STYP_RCONST)
410 sec_flags |= SEC_READONLY;
412 else if ((styp_flags & STYP_BSS)
413 || (styp_flags & STYP_SBSS))
414 sec_flags |= SEC_ALLOC;
415 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
416 sec_flags |= SEC_NEVER_LOAD;
417 else if ((styp_flags & STYP_LITA)
418 || (styp_flags & STYP_LIT8)
419 || (styp_flags & STYP_LIT4))
420 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
421 else if (styp_flags & STYP_ECOFF_LIB)
422 sec_flags |= SEC_COFF_SHARED_LIBRARY;
423 else
424 sec_flags |= SEC_ALLOC | SEC_LOAD;
426 * flags_ptr = sec_flags;
427 return TRUE;
430 /* Read in the symbolic header for an ECOFF object file. */
432 static bfd_boolean
433 ecoff_slurp_symbolic_header (bfd *abfd)
435 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
436 bfd_size_type external_hdr_size;
437 void * raw = NULL;
438 HDRR *internal_symhdr;
440 /* See if we've already read it in. */
441 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
442 backend->debug_swap.sym_magic)
443 return TRUE;
445 /* See whether there is a symbolic header. */
446 if (ecoff_data (abfd)->sym_filepos == 0)
448 bfd_get_symcount (abfd) = 0;
449 return TRUE;
452 /* At this point bfd_get_symcount (abfd) holds the number of symbols
453 as read from the file header, but on ECOFF this is always the
454 size of the symbolic information header. It would be cleaner to
455 handle this when we first read the file in coffgen.c. */
456 external_hdr_size = backend->debug_swap.external_hdr_size;
457 if (bfd_get_symcount (abfd) != external_hdr_size)
459 bfd_set_error (bfd_error_bad_value);
460 return FALSE;
463 /* Read the symbolic information header. */
464 raw = bfd_malloc (external_hdr_size);
465 if (raw == NULL)
466 goto error_return;
468 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0
469 || bfd_bread (raw, external_hdr_size, abfd) != external_hdr_size)
470 goto error_return;
471 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
472 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
474 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
476 bfd_set_error (bfd_error_bad_value);
477 goto error_return;
480 /* Now we can get the correct number of symbols. */
481 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
482 + internal_symhdr->iextMax);
484 if (raw != NULL)
485 free (raw);
486 return TRUE;
487 error_return:
488 if (raw != NULL)
489 free (raw);
490 return FALSE;
493 /* Read in and swap the important symbolic information for an ECOFF
494 object file. This is called by gdb via the read_debug_info entry
495 point in the backend structure. */
497 bfd_boolean
498 _bfd_ecoff_slurp_symbolic_info (bfd *abfd,
499 asection *ignore ATTRIBUTE_UNUSED,
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;
506 void * raw;
507 bfd_size_type external_fdr_size;
508 char *fraw_src;
509 char *fraw_end;
510 struct fdr *fdr_ptr;
511 bfd_size_type raw_end;
512 bfd_size_type cb_end;
513 bfd_size_type amt;
514 file_ptr pos;
516 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
518 /* Check whether we've already gotten it, and whether there's any to
519 get. */
520 if (ecoff_data (abfd)->raw_syments != NULL)
521 return TRUE;
522 if (ecoff_data (abfd)->sym_filepos == 0)
524 bfd_get_symcount (abfd) = 0;
525 return TRUE;
528 if (! ecoff_slurp_symbolic_header (abfd))
529 return FALSE;
531 internal_symhdr = &debug->symbolic_header;
533 /* Read all the symbolic information at once. */
534 raw_base = (ecoff_data (abfd)->sym_filepos
535 + backend->debug_swap.external_hdr_size);
537 /* Alpha ecoff makes the determination of raw_size difficult. It has
538 an undocumented debug data section between the symhdr and the first
539 documented section. And the ordering of the sections varies between
540 statically and dynamically linked executables.
541 If bfd supports SEEK_END someday, this code could be simplified. */
542 raw_end = 0;
544 #define UPDATE_RAW_END(start, count, size) \
545 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
546 if (cb_end > raw_end) \
547 raw_end = cb_end
549 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
550 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
551 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
552 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
553 /* eraxxon@alumni.rice.edu: ioptMax refers to the size of the
554 optimization symtab, not the number of entries. */
555 UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
556 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
557 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
558 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
559 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
560 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
561 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
563 #undef UPDATE_RAW_END
565 raw_size = raw_end - raw_base;
566 if (raw_size == 0)
568 ecoff_data (abfd)->sym_filepos = 0;
569 return TRUE;
571 raw = bfd_alloc (abfd, raw_size);
572 if (raw == NULL)
573 return FALSE;
575 pos = ecoff_data (abfd)->sym_filepos;
576 pos += backend->debug_swap.external_hdr_size;
577 if (bfd_seek (abfd, pos, SEEK_SET) != 0
578 || bfd_bread (raw, raw_size, abfd) != raw_size)
580 bfd_release (abfd, raw);
581 return FALSE;
584 ecoff_data (abfd)->raw_syments = raw;
586 /* Get pointers for the numeric offsets in the HDRR structure. */
587 #define FIX(off1, off2, type) \
588 if (internal_symhdr->off1 == 0) \
589 debug->off2 = NULL; \
590 else \
591 debug->off2 = (type) ((char *) raw \
592 + (internal_symhdr->off1 \
593 - raw_base))
595 FIX (cbLineOffset, line, unsigned char *);
596 FIX (cbDnOffset, external_dnr, void *);
597 FIX (cbPdOffset, external_pdr, void *);
598 FIX (cbSymOffset, external_sym, void *);
599 FIX (cbOptOffset, external_opt, void *);
600 FIX (cbAuxOffset, external_aux, union aux_ext *);
601 FIX (cbSsOffset, ss, char *);
602 FIX (cbSsExtOffset, ssext, char *);
603 FIX (cbFdOffset, external_fdr, void *);
604 FIX (cbRfdOffset, external_rfd, void *);
605 FIX (cbExtOffset, external_ext, void *);
606 #undef FIX
608 /* I don't want to always swap all the data, because it will just
609 waste time and most programs will never look at it. The only
610 time the linker needs most of the debugging information swapped
611 is when linking big-endian and little-endian MIPS object files
612 together, which is not a common occurrence.
614 We need to look at the fdr to deal with a lot of information in
615 the symbols, so we swap them here. */
616 amt = internal_symhdr->ifdMax;
617 amt *= sizeof (struct fdr);
618 debug->fdr = (FDR *) bfd_alloc (abfd, amt);
619 if (debug->fdr == NULL)
620 return FALSE;
621 external_fdr_size = backend->debug_swap.external_fdr_size;
622 fdr_ptr = debug->fdr;
623 fraw_src = (char *) debug->external_fdr;
624 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
625 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
626 (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
628 return TRUE;
631 /* ECOFF symbol table routines. The ECOFF symbol table is described
632 in gcc/mips-tfile.c. */
634 /* ECOFF uses two common sections. One is the usual one, and the
635 other is for small objects. All the small objects are kept
636 together, and then referenced via the gp pointer, which yields
637 faster assembler code. This is what we use for the small common
638 section. */
639 static asection ecoff_scom_section;
640 static asymbol ecoff_scom_symbol;
641 static asymbol *ecoff_scom_symbol_ptr;
643 /* Create an empty symbol. */
645 asymbol *
646 _bfd_ecoff_make_empty_symbol (bfd *abfd)
648 ecoff_symbol_type *new_symbol;
649 bfd_size_type amt = sizeof (ecoff_symbol_type);
651 new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
652 if (new_symbol == NULL)
653 return NULL;
654 new_symbol->symbol.section = NULL;
655 new_symbol->fdr = NULL;
656 new_symbol->local = FALSE;
657 new_symbol->native = NULL;
658 new_symbol->symbol.the_bfd = abfd;
659 return &new_symbol->symbol;
662 /* Set the BFD flags and section for an ECOFF symbol. */
664 static bfd_boolean
665 ecoff_set_symbol_info (bfd *abfd,
666 SYMR *ecoff_sym,
667 asymbol *asym,
668 int ext,
669 int weak)
671 asym->the_bfd = abfd;
672 asym->value = ecoff_sym->value;
673 asym->section = &bfd_debug_section;
674 asym->udata.i = 0;
676 /* Most symbol types are just for debugging. */
677 switch (ecoff_sym->st)
679 case stGlobal:
680 case stStatic:
681 case stLabel:
682 case stProc:
683 case stStaticProc:
684 break;
685 case stNil:
686 if (ECOFF_IS_STAB (ecoff_sym))
688 asym->flags = BSF_DEBUGGING;
689 return TRUE;
691 break;
692 default:
693 asym->flags = BSF_DEBUGGING;
694 return TRUE;
697 if (weak)
698 asym->flags = BSF_EXPORT | BSF_WEAK;
699 else if (ext)
700 asym->flags = BSF_EXPORT | BSF_GLOBAL;
701 else
703 asym->flags = BSF_LOCAL;
704 /* Normally, a local stProc symbol will have a corresponding
705 external symbol. We mark the local symbol as a debugging
706 symbol, in order to prevent nm from printing both out.
707 Similarly, we mark stLabel and stabs symbols as debugging
708 symbols. In both cases, we do want to set the value
709 correctly based on the symbol class. */
710 if (ecoff_sym->st == stProc
711 || ecoff_sym->st == stLabel
712 || ECOFF_IS_STAB (ecoff_sym))
713 asym->flags |= BSF_DEBUGGING;
716 if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
717 asym->flags |= BSF_FUNCTION;
719 switch (ecoff_sym->sc)
721 case scNil:
722 /* Used for compiler generated labels. Leave them in the
723 debugging section, and mark them as local. If BSF_DEBUGGING
724 is set, then nm does not display them for some reason. If no
725 flags are set then the linker whines about them. */
726 asym->flags = BSF_LOCAL;
727 break;
728 case scText:
729 asym->section = bfd_make_section_old_way (abfd, _TEXT);
730 asym->value -= asym->section->vma;
731 break;
732 case scData:
733 asym->section = bfd_make_section_old_way (abfd, _DATA);
734 asym->value -= asym->section->vma;
735 break;
736 case scBss:
737 asym->section = bfd_make_section_old_way (abfd, _BSS);
738 asym->value -= asym->section->vma;
739 break;
740 case scRegister:
741 asym->flags = BSF_DEBUGGING;
742 break;
743 case scAbs:
744 asym->section = bfd_abs_section_ptr;
745 break;
746 case scUndefined:
747 asym->section = bfd_und_section_ptr;
748 asym->flags = 0;
749 asym->value = 0;
750 break;
751 case scCdbLocal:
752 case scBits:
753 case scCdbSystem:
754 case scRegImage:
755 case scInfo:
756 case scUserStruct:
757 asym->flags = BSF_DEBUGGING;
758 break;
759 case scSData:
760 asym->section = bfd_make_section_old_way (abfd, ".sdata");
761 asym->value -= asym->section->vma;
762 break;
763 case scSBss:
764 asym->section = bfd_make_section_old_way (abfd, ".sbss");
765 asym->value -= asym->section->vma;
766 break;
767 case scRData:
768 asym->section = bfd_make_section_old_way (abfd, ".rdata");
769 asym->value -= asym->section->vma;
770 break;
771 case scVar:
772 asym->flags = BSF_DEBUGGING;
773 break;
774 case scCommon:
775 if (asym->value > ecoff_data (abfd)->gp_size)
777 asym->section = bfd_com_section_ptr;
778 asym->flags = 0;
779 break;
781 /* Fall through. */
782 case scSCommon:
783 if (ecoff_scom_section.name == NULL)
785 /* Initialize the small common section. */
786 ecoff_scom_section.name = SCOMMON;
787 ecoff_scom_section.flags = SEC_IS_COMMON;
788 ecoff_scom_section.output_section = &ecoff_scom_section;
789 ecoff_scom_section.symbol = &ecoff_scom_symbol;
790 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
791 ecoff_scom_symbol.name = SCOMMON;
792 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
793 ecoff_scom_symbol.section = &ecoff_scom_section;
794 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
796 asym->section = &ecoff_scom_section;
797 asym->flags = 0;
798 break;
799 case scVarRegister:
800 case scVariant:
801 asym->flags = BSF_DEBUGGING;
802 break;
803 case scSUndefined:
804 asym->section = bfd_und_section_ptr;
805 asym->flags = 0;
806 asym->value = 0;
807 break;
808 case scInit:
809 asym->section = bfd_make_section_old_way (abfd, ".init");
810 asym->value -= asym->section->vma;
811 break;
812 case scBasedVar:
813 case scXData:
814 case scPData:
815 asym->flags = BSF_DEBUGGING;
816 break;
817 case scFini:
818 asym->section = bfd_make_section_old_way (abfd, ".fini");
819 asym->value -= asym->section->vma;
820 break;
821 case scRConst:
822 asym->section = bfd_make_section_old_way (abfd, ".rconst");
823 asym->value -= asym->section->vma;
824 break;
825 default:
826 break;
829 /* Look for special constructors symbols and make relocation entries
830 in a special construction section. These are produced by the
831 -fgnu-linker argument to g++. */
832 if (ECOFF_IS_STAB (ecoff_sym))
834 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
836 default:
837 break;
839 case N_SETA:
840 case N_SETT:
841 case N_SETD:
842 case N_SETB:
843 /* Mark the symbol as a constructor. */
844 asym->flags |= BSF_CONSTRUCTOR;
845 break;
848 return TRUE;
851 /* Read an ECOFF symbol table. */
853 bfd_boolean
854 _bfd_ecoff_slurp_symbol_table (bfd *abfd)
856 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
857 const bfd_size_type external_ext_size
858 = backend->debug_swap.external_ext_size;
859 const bfd_size_type external_sym_size
860 = backend->debug_swap.external_sym_size;
861 void (* const swap_ext_in) (bfd *, void *, EXTR *)
862 = backend->debug_swap.swap_ext_in;
863 void (* const swap_sym_in) (bfd *, void *, SYMR *)
864 = backend->debug_swap.swap_sym_in;
865 bfd_size_type internal_size;
866 ecoff_symbol_type *internal;
867 ecoff_symbol_type *internal_ptr;
868 char *eraw_src;
869 char *eraw_end;
870 FDR *fdr_ptr;
871 FDR *fdr_end;
873 /* If we've already read in the symbol table, do nothing. */
874 if (ecoff_data (abfd)->canonical_symbols != NULL)
875 return TRUE;
877 /* Get the symbolic information. */
878 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
879 &ecoff_data (abfd)->debug_info))
880 return FALSE;
881 if (bfd_get_symcount (abfd) == 0)
882 return TRUE;
884 internal_size = bfd_get_symcount (abfd);
885 internal_size *= sizeof (ecoff_symbol_type);
886 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
887 if (internal == NULL)
888 return FALSE;
890 internal_ptr = internal;
891 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
892 eraw_end = (eraw_src
893 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
894 * external_ext_size));
895 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
897 EXTR internal_esym;
899 (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
900 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
901 + internal_esym.asym.iss);
902 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
903 &internal_ptr->symbol, 1,
904 internal_esym.weakext))
905 return FALSE;
906 /* The alpha uses a negative ifd field for section symbols. */
907 if (internal_esym.ifd >= 0)
908 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
909 + internal_esym.ifd);
910 else
911 internal_ptr->fdr = NULL;
912 internal_ptr->local = FALSE;
913 internal_ptr->native = (void *) eraw_src;
916 /* The local symbols must be accessed via the fdr's, because the
917 string and aux indices are relative to the fdr information. */
918 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
919 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
920 for (; fdr_ptr < fdr_end; fdr_ptr++)
922 char *lraw_src;
923 char *lraw_end;
925 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
926 + fdr_ptr->isymBase * external_sym_size);
927 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
928 for (;
929 lraw_src < lraw_end;
930 lraw_src += external_sym_size, internal_ptr++)
932 SYMR internal_sym;
934 (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
935 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
936 + fdr_ptr->issBase
937 + internal_sym.iss);
938 if (!ecoff_set_symbol_info (abfd, &internal_sym,
939 &internal_ptr->symbol, 0, 0))
940 return FALSE;
941 internal_ptr->fdr = fdr_ptr;
942 internal_ptr->local = TRUE;
943 internal_ptr->native = (void *) lraw_src;
947 ecoff_data (abfd)->canonical_symbols = internal;
949 return TRUE;
952 /* Return the amount of space needed for the canonical symbols. */
954 long
955 _bfd_ecoff_get_symtab_upper_bound (bfd *abfd)
957 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
958 &ecoff_data (abfd)->debug_info))
959 return -1;
961 if (bfd_get_symcount (abfd) == 0)
962 return 0;
964 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
967 /* Get the canonical symbols. */
969 long
970 _bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
972 unsigned int counter = 0;
973 ecoff_symbol_type *symbase;
974 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
976 if (! _bfd_ecoff_slurp_symbol_table (abfd))
977 return -1;
978 if (bfd_get_symcount (abfd) == 0)
979 return 0;
981 symbase = ecoff_data (abfd)->canonical_symbols;
982 while (counter < bfd_get_symcount (abfd))
984 *(location++) = symbase++;
985 counter++;
987 *location++ = NULL;
988 return bfd_get_symcount (abfd);
991 /* Turn ECOFF type information into a printable string.
992 ecoff_emit_aggregate and ecoff_type_to_string are from
993 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
995 /* Write aggregate information to a string. */
997 static void
998 ecoff_emit_aggregate (bfd *abfd,
999 FDR *fdr,
1000 char *string,
1001 RNDXR *rndx,
1002 long isym,
1003 const char *which)
1005 const struct ecoff_debug_swap * const debug_swap =
1006 &ecoff_backend (abfd)->debug_swap;
1007 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1008 unsigned int ifd = rndx->rfd;
1009 unsigned int indx = rndx->index;
1010 const char *name;
1012 if (ifd == 0xfff)
1013 ifd = isym;
1015 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1016 struct return type of a procedure compiled without -g. */
1017 if (ifd == 0xffffffff
1018 || (rndx->rfd == 0xfff && indx == 0))
1019 name = "<undefined>";
1020 else if (indx == indexNil)
1021 name = "<no name>";
1022 else
1024 SYMR sym;
1026 if (debug_info->external_rfd == NULL)
1027 fdr = debug_info->fdr + ifd;
1028 else
1030 RFDT rfd;
1032 (*debug_swap->swap_rfd_in) (abfd,
1033 ((char *) debug_info->external_rfd
1034 + ((fdr->rfdBase + ifd)
1035 * debug_swap->external_rfd_size)),
1036 &rfd);
1037 fdr = debug_info->fdr + rfd;
1040 indx += fdr->isymBase;
1042 (*debug_swap->swap_sym_in) (abfd,
1043 ((char *) debug_info->external_sym
1044 + indx * debug_swap->external_sym_size),
1045 &sym);
1047 name = debug_info->ss + fdr->issBase + sym.iss;
1050 sprintf (string,
1051 "%s %s { ifd = %u, index = %lu }",
1052 which, name, ifd,
1053 ((unsigned long) indx
1054 + debug_info->symbolic_header.iextMax));
1057 /* Convert the type information to string format. */
1059 static char *
1060 ecoff_type_to_string (bfd *abfd, FDR *fdr, unsigned int indx)
1062 union aux_ext *aux_ptr;
1063 int bigendian;
1064 AUXU u;
1065 struct qual
1067 unsigned int type;
1068 int low_bound;
1069 int high_bound;
1070 int stride;
1071 } qualifiers[7];
1072 unsigned int basic_type;
1073 int i;
1074 char buffer1[1024];
1075 static char buffer2[1024];
1076 char *p1 = buffer1;
1077 char *p2 = buffer2;
1078 RNDXR rndx;
1080 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1081 bigendian = fdr->fBigendian;
1083 for (i = 0; i < 7; i++)
1085 qualifiers[i].low_bound = 0;
1086 qualifiers[i].high_bound = 0;
1087 qualifiers[i].stride = 0;
1090 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1091 return "-1 (no type)";
1092 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1094 basic_type = u.ti.bt;
1095 qualifiers[0].type = u.ti.tq0;
1096 qualifiers[1].type = u.ti.tq1;
1097 qualifiers[2].type = u.ti.tq2;
1098 qualifiers[3].type = u.ti.tq3;
1099 qualifiers[4].type = u.ti.tq4;
1100 qualifiers[5].type = u.ti.tq5;
1101 qualifiers[6].type = tqNil;
1103 /* Go get the basic type. */
1104 switch (basic_type)
1106 case btNil: /* Undefined. */
1107 strcpy (p1, "nil");
1108 break;
1110 case btAdr: /* Address - integer same size as pointer. */
1111 strcpy (p1, "address");
1112 break;
1114 case btChar: /* Character. */
1115 strcpy (p1, "char");
1116 break;
1118 case btUChar: /* Unsigned character. */
1119 strcpy (p1, "unsigned char");
1120 break;
1122 case btShort: /* Short. */
1123 strcpy (p1, "short");
1124 break;
1126 case btUShort: /* Unsigned short. */
1127 strcpy (p1, "unsigned short");
1128 break;
1130 case btInt: /* Int. */
1131 strcpy (p1, "int");
1132 break;
1134 case btUInt: /* Unsigned int. */
1135 strcpy (p1, "unsigned int");
1136 break;
1138 case btLong: /* Long. */
1139 strcpy (p1, "long");
1140 break;
1142 case btULong: /* Unsigned long. */
1143 strcpy (p1, "unsigned long");
1144 break;
1146 case btFloat: /* Float (real). */
1147 strcpy (p1, "float");
1148 break;
1150 case btDouble: /* Double (real). */
1151 strcpy (p1, "double");
1152 break;
1154 /* Structures add 1-2 aux words:
1155 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1156 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1158 case btStruct: /* Structure (Record). */
1159 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1160 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1161 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1162 "struct");
1163 indx++; /* Skip aux words. */
1164 break;
1166 /* Unions add 1-2 aux words:
1167 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1168 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1170 case btUnion: /* Union. */
1171 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1172 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1173 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1174 "union");
1175 indx++; /* Skip aux words. */
1176 break;
1178 /* Enumerations add 1-2 aux words:
1179 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1180 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1182 case btEnum: /* Enumeration. */
1183 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1184 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1185 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1186 "enum");
1187 indx++; /* Skip aux words. */
1188 break;
1190 case btTypedef: /* Defined via a typedef, isymRef points. */
1191 strcpy (p1, "typedef");
1192 break;
1194 case btRange: /* Subrange of int. */
1195 strcpy (p1, "subrange");
1196 break;
1198 case btSet: /* Pascal sets. */
1199 strcpy (p1, "set");
1200 break;
1202 case btComplex: /* Fortran complex. */
1203 strcpy (p1, "complex");
1204 break;
1206 case btDComplex: /* Fortran double complex. */
1207 strcpy (p1, "double complex");
1208 break;
1210 case btIndirect: /* Forward or unnamed typedef. */
1211 strcpy (p1, "forward/unamed typedef");
1212 break;
1214 case btFixedDec: /* Fixed Decimal. */
1215 strcpy (p1, "fixed decimal");
1216 break;
1218 case btFloatDec: /* Float Decimal. */
1219 strcpy (p1, "float decimal");
1220 break;
1222 case btString: /* Varying Length Character String. */
1223 strcpy (p1, "string");
1224 break;
1226 case btBit: /* Aligned Bit String. */
1227 strcpy (p1, "bit");
1228 break;
1230 case btPicture: /* Picture. */
1231 strcpy (p1, "picture");
1232 break;
1234 case btVoid: /* Void. */
1235 strcpy (p1, "void");
1236 break;
1238 default:
1239 sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
1240 break;
1243 p1 += strlen (buffer1);
1245 /* If this is a bitfield, get the bitsize. */
1246 if (u.ti.fBitfield)
1248 int bitsize;
1250 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1251 sprintf (p1, " : %d", bitsize);
1252 p1 += strlen (buffer1);
1255 /* Deal with any qualifiers. */
1256 if (qualifiers[0].type != tqNil)
1258 /* Snarf up any array bounds in the correct order. Arrays
1259 store 5 successive words in the aux. table:
1260 word 0 RNDXR to type of the bounds (ie, int)
1261 word 1 Current file descriptor index
1262 word 2 low bound
1263 word 3 high bound (or -1 if [])
1264 word 4 stride size in bits. */
1265 for (i = 0; i < 7; i++)
1267 if (qualifiers[i].type == tqArray)
1269 qualifiers[i].low_bound =
1270 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1271 qualifiers[i].high_bound =
1272 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1273 qualifiers[i].stride =
1274 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1275 indx += 5;
1279 /* Now print out the qualifiers. */
1280 for (i = 0; i < 6; i++)
1282 switch (qualifiers[i].type)
1284 case tqNil:
1285 case tqMax:
1286 break;
1288 case tqPtr:
1289 strcpy (p2, "ptr to ");
1290 p2 += sizeof ("ptr to ")-1;
1291 break;
1293 case tqVol:
1294 strcpy (p2, "volatile ");
1295 p2 += sizeof ("volatile ")-1;
1296 break;
1298 case tqFar:
1299 strcpy (p2, "far ");
1300 p2 += sizeof ("far ")-1;
1301 break;
1303 case tqProc:
1304 strcpy (p2, "func. ret. ");
1305 p2 += sizeof ("func. ret. ");
1306 break;
1308 case tqArray:
1310 int first_array = i;
1311 int j;
1313 /* Print array bounds reversed (ie, in the order the C
1314 programmer writes them). C is such a fun language.... */
1315 while (i < 5 && qualifiers[i+1].type == tqArray)
1316 i++;
1318 for (j = i; j >= first_array; j--)
1320 strcpy (p2, "array [");
1321 p2 += sizeof ("array [")-1;
1322 if (qualifiers[j].low_bound != 0)
1323 sprintf (p2,
1324 "%ld:%ld {%ld bits}",
1325 (long) qualifiers[j].low_bound,
1326 (long) qualifiers[j].high_bound,
1327 (long) qualifiers[j].stride);
1329 else if (qualifiers[j].high_bound != -1)
1330 sprintf (p2,
1331 "%ld {%ld bits}",
1332 (long) (qualifiers[j].high_bound + 1),
1333 (long) (qualifiers[j].stride));
1335 else
1336 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1338 p2 += strlen (p2);
1339 strcpy (p2, "] of ");
1340 p2 += sizeof ("] of ")-1;
1343 break;
1348 strcpy (p2, buffer1);
1349 return buffer2;
1352 /* Return information about ECOFF symbol SYMBOL in RET. */
1354 void
1355 _bfd_ecoff_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1356 asymbol *symbol,
1357 symbol_info *ret)
1359 bfd_symbol_info (symbol, ret);
1362 /* Return whether this is a local label. */
1364 bfd_boolean
1365 _bfd_ecoff_bfd_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
1366 const char *name)
1368 return name[0] == '$';
1371 /* Print information about an ECOFF symbol. */
1373 void
1374 _bfd_ecoff_print_symbol (bfd *abfd,
1375 void * filep,
1376 asymbol *symbol,
1377 bfd_print_symbol_type how)
1379 const struct ecoff_debug_swap * const debug_swap
1380 = &ecoff_backend (abfd)->debug_swap;
1381 FILE *file = (FILE *)filep;
1383 switch (how)
1385 case bfd_print_symbol_name:
1386 fprintf (file, "%s", symbol->name);
1387 break;
1388 case bfd_print_symbol_more:
1389 if (ecoffsymbol (symbol)->local)
1391 SYMR ecoff_sym;
1393 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1394 &ecoff_sym);
1395 fprintf (file, "ecoff local ");
1396 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1397 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1398 (unsigned) ecoff_sym.sc);
1400 else
1402 EXTR ecoff_ext;
1404 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1405 &ecoff_ext);
1406 fprintf (file, "ecoff extern ");
1407 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1408 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1409 (unsigned) ecoff_ext.asym.sc);
1411 break;
1412 case bfd_print_symbol_all:
1413 /* Print out the symbols in a reasonable way. */
1415 char type;
1416 int pos;
1417 EXTR ecoff_ext;
1418 char jmptbl;
1419 char cobol_main;
1420 char weakext;
1422 if (ecoffsymbol (symbol)->local)
1424 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1425 &ecoff_ext.asym);
1426 type = 'l';
1427 pos = ((((char *) ecoffsymbol (symbol)->native
1428 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1429 / debug_swap->external_sym_size)
1430 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1431 jmptbl = ' ';
1432 cobol_main = ' ';
1433 weakext = ' ';
1435 else
1437 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1438 &ecoff_ext);
1439 type = 'e';
1440 pos = (((char *) ecoffsymbol (symbol)->native
1441 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1442 / debug_swap->external_ext_size);
1443 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1444 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1445 weakext = ecoff_ext.weakext ? 'w' : ' ';
1448 fprintf (file, "[%3d] %c ",
1449 pos, type);
1450 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1451 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1452 (unsigned) ecoff_ext.asym.st,
1453 (unsigned) ecoff_ext.asym.sc,
1454 (unsigned) ecoff_ext.asym.index,
1455 jmptbl, cobol_main, weakext,
1456 symbol->name);
1458 if (ecoffsymbol (symbol)->fdr != NULL
1459 && ecoff_ext.asym.index != indexNil)
1461 FDR *fdr;
1462 unsigned int indx;
1463 int bigendian;
1464 bfd_size_type sym_base;
1465 union aux_ext *aux_base;
1467 fdr = ecoffsymbol (symbol)->fdr;
1468 indx = ecoff_ext.asym.index;
1470 /* sym_base is used to map the fdr relative indices which
1471 appear in the file to the position number which we are
1472 using. */
1473 sym_base = fdr->isymBase;
1474 if (ecoffsymbol (symbol)->local)
1475 sym_base +=
1476 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1478 /* aux_base is the start of the aux entries for this file;
1479 asym.index is an offset from this. */
1480 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1481 + fdr->iauxBase);
1483 /* The aux entries are stored in host byte order; the
1484 order is indicated by a bit in the fdr. */
1485 bigendian = fdr->fBigendian;
1487 /* This switch is basically from gcc/mips-tdump.c. */
1488 switch (ecoff_ext.asym.st)
1490 case stNil:
1491 case stLabel:
1492 break;
1494 case stFile:
1495 case stBlock:
1496 fprintf (file, _("\n End+1 symbol: %ld"),
1497 (long) (indx + sym_base));
1498 break;
1500 case stEnd:
1501 if (ecoff_ext.asym.sc == scText
1502 || ecoff_ext.asym.sc == scInfo)
1503 fprintf (file, _("\n First symbol: %ld"),
1504 (long) (indx + sym_base));
1505 else
1506 fprintf (file, _("\n First symbol: %ld"),
1507 ((long)
1508 (AUX_GET_ISYM (bigendian,
1509 &aux_base[ecoff_ext.asym.index])
1510 + sym_base)));
1511 break;
1513 case stProc:
1514 case stStaticProc:
1515 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1517 else if (ecoffsymbol (symbol)->local)
1518 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
1519 ((long)
1520 (AUX_GET_ISYM (bigendian,
1521 &aux_base[ecoff_ext.asym.index])
1522 + sym_base)),
1523 ecoff_type_to_string (abfd, fdr, indx + 1));
1524 else
1525 fprintf (file, _("\n Local symbol: %ld"),
1526 ((long) indx
1527 + (long) sym_base
1528 + (ecoff_data (abfd)
1529 ->debug_info.symbolic_header.iextMax)));
1530 break;
1532 case stStruct:
1533 fprintf (file, _("\n struct; End+1 symbol: %ld"),
1534 (long) (indx + sym_base));
1535 break;
1537 case stUnion:
1538 fprintf (file, _("\n union; End+1 symbol: %ld"),
1539 (long) (indx + sym_base));
1540 break;
1542 case stEnum:
1543 fprintf (file, _("\n enum; End+1 symbol: %ld"),
1544 (long) (indx + sym_base));
1545 break;
1547 default:
1548 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1549 fprintf (file, _("\n Type: %s"),
1550 ecoff_type_to_string (abfd, fdr, indx));
1551 break;
1555 break;
1559 /* Read in the relocs for a section. */
1561 static bfd_boolean
1562 ecoff_slurp_reloc_table (bfd *abfd,
1563 asection *section,
1564 asymbol **symbols)
1566 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1567 arelent *internal_relocs;
1568 bfd_size_type external_reloc_size;
1569 bfd_size_type amt;
1570 char *external_relocs;
1571 arelent *rptr;
1572 unsigned int i;
1574 if (section->relocation != NULL
1575 || section->reloc_count == 0
1576 || (section->flags & SEC_CONSTRUCTOR) != 0)
1577 return TRUE;
1579 if (! _bfd_ecoff_slurp_symbol_table (abfd))
1580 return FALSE;
1582 amt = section->reloc_count;
1583 amt *= sizeof (arelent);
1584 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1586 external_reloc_size = backend->external_reloc_size;
1587 amt = external_reloc_size * section->reloc_count;
1588 external_relocs = (char *) bfd_alloc (abfd, amt);
1589 if (internal_relocs == NULL || external_relocs == NULL)
1590 return FALSE;
1591 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1592 return FALSE;
1593 if (bfd_bread (external_relocs, amt, abfd) != amt)
1594 return FALSE;
1596 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1598 struct internal_reloc intern;
1600 (*backend->swap_reloc_in) (abfd,
1601 external_relocs + i * external_reloc_size,
1602 &intern);
1604 if (intern.r_extern)
1606 /* r_symndx is an index into the external symbols. */
1607 BFD_ASSERT (intern.r_symndx >= 0
1608 && (intern.r_symndx
1609 < (ecoff_data (abfd)
1610 ->debug_info.symbolic_header.iextMax)));
1611 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1612 rptr->addend = 0;
1614 else if (intern.r_symndx == RELOC_SECTION_NONE
1615 || intern.r_symndx == RELOC_SECTION_ABS)
1617 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1618 rptr->addend = 0;
1620 else
1622 const char *sec_name;
1623 asection *sec;
1625 /* r_symndx is a section key. */
1626 switch (intern.r_symndx)
1628 case RELOC_SECTION_TEXT: sec_name = _TEXT; break;
1629 case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
1630 case RELOC_SECTION_DATA: sec_name = _DATA; break;
1631 case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
1632 case RELOC_SECTION_SBSS: sec_name = _SBSS; break;
1633 case RELOC_SECTION_BSS: sec_name = _BSS; break;
1634 case RELOC_SECTION_INIT: sec_name = _INIT; break;
1635 case RELOC_SECTION_LIT8: sec_name = _LIT8; break;
1636 case RELOC_SECTION_LIT4: sec_name = _LIT4; break;
1637 case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
1638 case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
1639 case RELOC_SECTION_FINI: sec_name = _FINI; break;
1640 case RELOC_SECTION_LITA: sec_name = _LITA; break;
1641 case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
1642 default: abort ();
1645 sec = bfd_get_section_by_name (abfd, sec_name);
1646 if (sec == NULL)
1647 abort ();
1648 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1650 rptr->addend = - bfd_get_section_vma (abfd, sec);
1653 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1655 /* Let the backend select the howto field and do any other
1656 required processing. */
1657 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1660 bfd_release (abfd, external_relocs);
1662 section->relocation = internal_relocs;
1664 return TRUE;
1667 /* Get a canonical list of relocs. */
1669 long
1670 _bfd_ecoff_canonicalize_reloc (bfd *abfd,
1671 asection *section,
1672 arelent **relptr,
1673 asymbol **symbols)
1675 unsigned int count;
1677 if (section->flags & SEC_CONSTRUCTOR)
1679 arelent_chain *chain;
1681 /* This section has relocs made up by us, not the file, so take
1682 them out of their chain and place them into the data area
1683 provided. */
1684 for (count = 0, chain = section->constructor_chain;
1685 count < section->reloc_count;
1686 count++, chain = chain->next)
1687 *relptr++ = &chain->relent;
1689 else
1691 arelent *tblptr;
1693 if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1694 return -1;
1696 tblptr = section->relocation;
1698 for (count = 0; count < section->reloc_count; count++)
1699 *relptr++ = tblptr++;
1702 *relptr = NULL;
1704 return section->reloc_count;
1707 /* Provided a BFD, a section and an offset into the section, calculate
1708 and return the name of the source file and the line nearest to the
1709 wanted location. */
1711 bfd_boolean
1712 _bfd_ecoff_find_nearest_line (bfd *abfd,
1713 asection *section,
1714 asymbol **ignore_symbols ATTRIBUTE_UNUSED,
1715 bfd_vma offset,
1716 const char **filename_ptr,
1717 const char **functionname_ptr,
1718 unsigned int *retline_ptr)
1720 const struct ecoff_debug_swap * const debug_swap
1721 = &ecoff_backend (abfd)->debug_swap;
1722 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1723 struct ecoff_find_line *line_info;
1725 /* Make sure we have the FDR's. */
1726 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
1727 || bfd_get_symcount (abfd) == 0)
1728 return FALSE;
1730 if (ecoff_data (abfd)->find_line_info == NULL)
1732 bfd_size_type amt = sizeof (struct ecoff_find_line);
1734 ecoff_data (abfd)->find_line_info =
1735 (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1736 if (ecoff_data (abfd)->find_line_info == NULL)
1737 return FALSE;
1739 line_info = ecoff_data (abfd)->find_line_info;
1741 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1742 debug_swap, line_info, filename_ptr,
1743 functionname_ptr, retline_ptr);
1746 /* Copy private BFD data. This is called by objcopy and strip. We
1747 use it to copy the ECOFF debugging information from one BFD to the
1748 other. It would be theoretically possible to represent the ECOFF
1749 debugging information in the symbol table. However, it would be a
1750 lot of work, and there would be little gain (gas, gdb, and ld
1751 already access the ECOFF debugging information via the
1752 ecoff_debug_info structure, and that structure would have to be
1753 retained in order to support ECOFF debugging in MIPS ELF).
1755 The debugging information for the ECOFF external symbols comes from
1756 the symbol table, so this function only handles the other debugging
1757 information. */
1759 bfd_boolean
1760 _bfd_ecoff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1762 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1763 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1764 int i;
1765 asymbol **sym_ptr_ptr;
1766 size_t c;
1767 bfd_boolean local;
1769 /* We only want to copy information over if both BFD's use ECOFF
1770 format. */
1771 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1772 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1773 return TRUE;
1775 /* Copy the GP value and the register masks. */
1776 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1777 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1778 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1779 for (i = 0; i < 3; i++)
1780 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1782 /* Copy the version stamp. */
1783 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1785 /* If there are no symbols, don't copy any debugging information. */
1786 c = bfd_get_symcount (obfd);
1787 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1788 if (c == 0 || sym_ptr_ptr == NULL)
1789 return TRUE;
1791 /* See if there are any local symbols. */
1792 local = FALSE;
1793 for (; c > 0; c--, sym_ptr_ptr++)
1795 if (ecoffsymbol (*sym_ptr_ptr)->local)
1797 local = TRUE;
1798 break;
1802 if (local)
1804 /* There are some local symbols. We just bring over all the
1805 debugging information. FIXME: This is not quite the right
1806 thing to do. If the user has asked us to discard all
1807 debugging information, then we are probably going to wind up
1808 keeping it because there will probably be some local symbol
1809 which objcopy did not discard. We should actually break
1810 apart the debugging information and only keep that which
1811 applies to the symbols we want to keep. */
1812 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1813 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1814 oinfo->line = iinfo->line;
1816 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1817 oinfo->external_dnr = iinfo->external_dnr;
1819 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1820 oinfo->external_pdr = iinfo->external_pdr;
1822 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1823 oinfo->external_sym = iinfo->external_sym;
1825 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1826 oinfo->external_opt = iinfo->external_opt;
1828 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1829 oinfo->external_aux = iinfo->external_aux;
1831 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1832 oinfo->ss = iinfo->ss;
1834 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1835 oinfo->external_fdr = iinfo->external_fdr;
1837 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1838 oinfo->external_rfd = iinfo->external_rfd;
1840 else
1842 /* We are discarding all the local symbol information. Look
1843 through the external symbols and remove all references to FDR
1844 or aux information. */
1845 c = bfd_get_symcount (obfd);
1846 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1847 for (; c > 0; c--, sym_ptr_ptr++)
1849 EXTR esym;
1851 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1852 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1853 esym.ifd = ifdNil;
1854 esym.asym.index = indexNil;
1855 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1856 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1860 return TRUE;
1863 /* Set the architecture. The supported architecture is stored in the
1864 backend pointer. We always set the architecture anyhow, since many
1865 callers ignore the return value. */
1867 bfd_boolean
1868 _bfd_ecoff_set_arch_mach (bfd *abfd,
1869 enum bfd_architecture arch,
1870 unsigned long machine)
1872 bfd_default_set_arch_mach (abfd, arch, machine);
1873 return arch == ecoff_backend (abfd)->arch;
1876 /* Get the size of the section headers. */
1879 _bfd_ecoff_sizeof_headers (bfd *abfd,
1880 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1882 asection *current;
1883 int c;
1884 int ret;
1886 c = 0;
1887 for (current = abfd->sections;
1888 current != NULL;
1889 current = current->next)
1890 ++c;
1892 ret = (bfd_coff_filhsz (abfd)
1893 + bfd_coff_aoutsz (abfd)
1894 + c * bfd_coff_scnhsz (abfd));
1895 return BFD_ALIGN (ret, 16);
1898 /* Get the contents of a section. */
1900 bfd_boolean
1901 _bfd_ecoff_get_section_contents (bfd *abfd,
1902 asection *section,
1903 void * location,
1904 file_ptr offset,
1905 bfd_size_type count)
1907 return _bfd_generic_get_section_contents (abfd, section, location,
1908 offset, count);
1911 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
1912 called via qsort. */
1914 static int
1915 ecoff_sort_hdrs (const void * arg1, const void * arg2)
1917 const asection *hdr1 = *(const asection **) arg1;
1918 const asection *hdr2 = *(const asection **) arg2;
1920 if ((hdr1->flags & SEC_ALLOC) != 0)
1922 if ((hdr2->flags & SEC_ALLOC) == 0)
1923 return -1;
1925 else
1927 if ((hdr2->flags & SEC_ALLOC) != 0)
1928 return 1;
1930 if (hdr1->vma < hdr2->vma)
1931 return -1;
1932 else if (hdr1->vma > hdr2->vma)
1933 return 1;
1934 else
1935 return 0;
1938 /* Calculate the file position for each section, and set
1939 reloc_filepos. */
1941 static bfd_boolean
1942 ecoff_compute_section_file_positions (bfd *abfd)
1944 file_ptr sofar, file_sofar;
1945 asection **sorted_hdrs;
1946 asection *current;
1947 unsigned int i;
1948 file_ptr old_sofar;
1949 bfd_boolean rdata_in_text;
1950 bfd_boolean first_data, first_nonalloc;
1951 const bfd_vma round = ecoff_backend (abfd)->round;
1952 bfd_size_type amt;
1954 sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
1955 file_sofar = sofar;
1957 /* Sort the sections by VMA. */
1958 amt = abfd->section_count;
1959 amt *= sizeof (asection *);
1960 sorted_hdrs = (asection **) bfd_malloc (amt);
1961 if (sorted_hdrs == NULL)
1962 return FALSE;
1963 for (current = abfd->sections, i = 0;
1964 current != NULL;
1965 current = current->next, i++)
1966 sorted_hdrs[i] = current;
1967 BFD_ASSERT (i == abfd->section_count);
1969 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
1970 ecoff_sort_hdrs);
1972 /* Some versions of the OSF linker put the .rdata section in the
1973 text segment, and some do not. */
1974 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
1975 if (rdata_in_text)
1977 for (i = 0; i < abfd->section_count; i++)
1979 current = sorted_hdrs[i];
1980 if (streq (current->name, _RDATA))
1981 break;
1982 if ((current->flags & SEC_CODE) == 0
1983 && ! streq (current->name, _PDATA)
1984 && ! streq (current->name, _RCONST))
1986 rdata_in_text = FALSE;
1987 break;
1991 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
1993 first_data = TRUE;
1994 first_nonalloc = TRUE;
1995 for (i = 0; i < abfd->section_count; i++)
1997 unsigned int alignment_power;
1999 current = sorted_hdrs[i];
2001 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2002 supposed to indicate the number of .pdata entries that are
2003 really in the section. Each entry is 8 bytes. We store this
2004 away in line_filepos before increasing the section size. */
2005 if (streq (current->name, _PDATA))
2006 current->line_filepos = current->size / 8;
2008 alignment_power = current->alignment_power;
2010 /* On Ultrix, the data sections in an executable file must be
2011 aligned to a page boundary within the file. This does not
2012 affect the section size, though. FIXME: Does this work for
2013 other platforms? It requires some modification for the
2014 Alpha, because .rdata on the Alpha goes with the text, not
2015 the data. */
2016 if ((abfd->flags & EXEC_P) != 0
2017 && (abfd->flags & D_PAGED) != 0
2018 && ! first_data
2019 && (current->flags & SEC_CODE) == 0
2020 && (! rdata_in_text
2021 || ! streq (current->name, _RDATA))
2022 && ! streq (current->name, _PDATA)
2023 && ! streq (current->name, _RCONST))
2025 sofar = (sofar + round - 1) &~ (round - 1);
2026 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2027 first_data = FALSE;
2029 else if (streq (current->name, _LIB))
2031 /* On Irix 4, the location of contents of the .lib section
2032 from a shared library section is also rounded up to a
2033 page boundary. */
2035 sofar = (sofar + round - 1) &~ (round - 1);
2036 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2038 else if (first_nonalloc
2039 && (current->flags & SEC_ALLOC) == 0
2040 && (abfd->flags & D_PAGED) != 0)
2042 /* Skip up to the next page for an unallocated section, such
2043 as the .comment section on the Alpha. This leaves room
2044 for the .bss section. */
2045 first_nonalloc = FALSE;
2046 sofar = (sofar + round - 1) &~ (round - 1);
2047 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2050 /* Align the sections in the file to the same boundary on
2051 which they are aligned in virtual memory. */
2052 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2053 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2054 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2056 if ((abfd->flags & D_PAGED) != 0
2057 && (current->flags & SEC_ALLOC) != 0)
2059 sofar += (current->vma - sofar) % round;
2060 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2061 file_sofar += (current->vma - file_sofar) % round;
2064 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2065 current->filepos = file_sofar;
2067 sofar += current->size;
2068 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2069 file_sofar += current->size;
2071 /* Make sure that this section is of the right size too. */
2072 old_sofar = sofar;
2073 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2074 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2075 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2076 current->size += sofar - old_sofar;
2079 free (sorted_hdrs);
2080 sorted_hdrs = NULL;
2082 ecoff_data (abfd)->reloc_filepos = file_sofar;
2084 return TRUE;
2087 /* Determine the location of the relocs for all the sections in the
2088 output file, as well as the location of the symbolic debugging
2089 information. */
2091 static bfd_size_type
2092 ecoff_compute_reloc_file_positions (bfd *abfd)
2094 const bfd_size_type external_reloc_size =
2095 ecoff_backend (abfd)->external_reloc_size;
2096 file_ptr reloc_base;
2097 bfd_size_type reloc_size;
2098 asection *current;
2099 file_ptr sym_base;
2101 if (! abfd->output_has_begun)
2103 if (! ecoff_compute_section_file_positions (abfd))
2104 abort ();
2105 abfd->output_has_begun = TRUE;
2108 reloc_base = ecoff_data (abfd)->reloc_filepos;
2110 reloc_size = 0;
2111 for (current = abfd->sections;
2112 current != NULL;
2113 current = current->next)
2115 if (current->reloc_count == 0)
2116 current->rel_filepos = 0;
2117 else
2119 bfd_size_type relsize;
2121 current->rel_filepos = reloc_base;
2122 relsize = current->reloc_count * external_reloc_size;
2123 reloc_size += relsize;
2124 reloc_base += relsize;
2128 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2130 /* At least on Ultrix, the symbol table of an executable file must
2131 be aligned to a page boundary. FIXME: Is this true on other
2132 platforms? */
2133 if ((abfd->flags & EXEC_P) != 0
2134 && (abfd->flags & D_PAGED) != 0)
2135 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2136 &~ (ecoff_backend (abfd)->round - 1));
2138 ecoff_data (abfd)->sym_filepos = sym_base;
2140 return reloc_size;
2143 /* Set the contents of a section. */
2145 bfd_boolean
2146 _bfd_ecoff_set_section_contents (bfd *abfd,
2147 asection *section,
2148 const void * location,
2149 file_ptr offset,
2150 bfd_size_type count)
2152 file_ptr pos;
2154 /* This must be done first, because bfd_set_section_contents is
2155 going to set output_has_begun to TRUE. */
2156 if (! abfd->output_has_begun
2157 && ! ecoff_compute_section_file_positions (abfd))
2158 return FALSE;
2160 /* Handle the .lib section specially so that Irix 4 shared libraries
2161 work out. See coff_set_section_contents in coffcode.h. */
2162 if (streq (section->name, _LIB))
2164 bfd_byte *rec, *recend;
2166 rec = (bfd_byte *) location;
2167 recend = rec + count;
2168 while (rec < recend)
2170 ++section->lma;
2171 rec += bfd_get_32 (abfd, rec) * 4;
2174 BFD_ASSERT (rec == recend);
2177 if (count == 0)
2178 return TRUE;
2180 pos = section->filepos + offset;
2181 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2182 || bfd_bwrite (location, count, abfd) != count)
2183 return FALSE;
2185 return TRUE;
2188 /* Get the GP value for an ECOFF file. This is a hook used by
2189 nlmconv. */
2191 bfd_vma
2192 bfd_ecoff_get_gp_value (bfd *abfd)
2194 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2195 || bfd_get_format (abfd) != bfd_object)
2197 bfd_set_error (bfd_error_invalid_operation);
2198 return 0;
2201 return ecoff_data (abfd)->gp;
2204 /* Set the GP value for an ECOFF file. This is a hook used by the
2205 assembler. */
2207 bfd_boolean
2208 bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value)
2210 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2211 || bfd_get_format (abfd) != bfd_object)
2213 bfd_set_error (bfd_error_invalid_operation);
2214 return FALSE;
2217 ecoff_data (abfd)->gp = gp_value;
2219 return TRUE;
2222 /* Set the register masks for an ECOFF file. This is a hook used by
2223 the assembler. */
2225 bfd_boolean
2226 bfd_ecoff_set_regmasks (bfd *abfd,
2227 unsigned long gprmask,
2228 unsigned long fprmask,
2229 unsigned long *cprmask)
2231 ecoff_data_type *tdata;
2233 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2234 || bfd_get_format (abfd) != bfd_object)
2236 bfd_set_error (bfd_error_invalid_operation);
2237 return FALSE;
2240 tdata = ecoff_data (abfd);
2241 tdata->gprmask = gprmask;
2242 tdata->fprmask = fprmask;
2243 if (cprmask != NULL)
2245 int i;
2247 for (i = 0; i < 3; i++)
2248 tdata->cprmask[i] = cprmask[i];
2251 return TRUE;
2254 /* Get ECOFF EXTR information for an external symbol. This function
2255 is passed to bfd_ecoff_debug_externals. */
2257 static bfd_boolean
2258 ecoff_get_extr (asymbol *sym, EXTR *esym)
2260 ecoff_symbol_type *ecoff_sym_ptr;
2261 bfd *input_bfd;
2263 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2264 || ecoffsymbol (sym)->native == NULL)
2266 /* Don't include debugging, local, or section symbols. */
2267 if ((sym->flags & BSF_DEBUGGING) != 0
2268 || (sym->flags & BSF_LOCAL) != 0
2269 || (sym->flags & BSF_SECTION_SYM) != 0)
2270 return FALSE;
2272 esym->jmptbl = 0;
2273 esym->cobol_main = 0;
2274 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2275 esym->reserved = 0;
2276 esym->ifd = ifdNil;
2277 /* FIXME: we can do better than this for st and sc. */
2278 esym->asym.st = stGlobal;
2279 esym->asym.sc = scAbs;
2280 esym->asym.reserved = 0;
2281 esym->asym.index = indexNil;
2282 return TRUE;
2285 ecoff_sym_ptr = ecoffsymbol (sym);
2287 if (ecoff_sym_ptr->local)
2288 return FALSE;
2290 input_bfd = bfd_asymbol_bfd (sym);
2291 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2292 (input_bfd, ecoff_sym_ptr->native, esym);
2294 /* If the symbol was defined by the linker, then esym will be
2295 undefined but sym will not be. Get a better class for such a
2296 symbol. */
2297 if ((esym->asym.sc == scUndefined
2298 || esym->asym.sc == scSUndefined)
2299 && ! bfd_is_und_section (bfd_get_section (sym)))
2300 esym->asym.sc = scAbs;
2302 /* Adjust the FDR index for the symbol by that used for the input
2303 BFD. */
2304 if (esym->ifd != -1)
2306 struct ecoff_debug_info *input_debug;
2308 input_debug = &ecoff_data (input_bfd)->debug_info;
2309 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2310 if (input_debug->ifdmap != NULL)
2311 esym->ifd = input_debug->ifdmap[esym->ifd];
2314 return TRUE;
2317 /* Set the external symbol index. This routine is passed to
2318 bfd_ecoff_debug_externals. */
2320 static void
2321 ecoff_set_index (asymbol *sym, bfd_size_type indx)
2323 ecoff_set_sym_index (sym, indx);
2326 /* Write out an ECOFF file. */
2328 bfd_boolean
2329 _bfd_ecoff_write_object_contents (bfd *abfd)
2331 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2332 const bfd_vma round = backend->round;
2333 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2334 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2335 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2336 const bfd_size_type external_hdr_size
2337 = backend->debug_swap.external_hdr_size;
2338 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2339 void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
2340 = backend->adjust_reloc_out;
2341 void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
2342 = backend->swap_reloc_out;
2343 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2344 HDRR * const symhdr = &debug->symbolic_header;
2345 asection *current;
2346 unsigned int count;
2347 bfd_size_type reloc_size;
2348 bfd_size_type text_size;
2349 bfd_vma text_start;
2350 bfd_boolean set_text_start;
2351 bfd_size_type data_size;
2352 bfd_vma data_start;
2353 bfd_boolean set_data_start;
2354 bfd_size_type bss_size;
2355 void * buff = NULL;
2356 void * reloc_buff = NULL;
2357 struct internal_filehdr internal_f;
2358 struct internal_aouthdr internal_a;
2359 int i;
2361 /* Determine where the sections and relocs will go in the output
2362 file. */
2363 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2365 count = 1;
2366 for (current = abfd->sections;
2367 current != NULL;
2368 current = current->next)
2370 current->target_index = count;
2371 ++count;
2374 if ((abfd->flags & D_PAGED) != 0)
2375 text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
2376 else
2377 text_size = 0;
2378 text_start = 0;
2379 set_text_start = FALSE;
2380 data_size = 0;
2381 data_start = 0;
2382 set_data_start = FALSE;
2383 bss_size = 0;
2385 /* Write section headers to the file. */
2387 /* Allocate buff big enough to hold a section header,
2388 file header, or a.out header. */
2390 bfd_size_type siz;
2392 siz = scnhsz;
2393 if (siz < filhsz)
2394 siz = filhsz;
2395 if (siz < aoutsz)
2396 siz = aoutsz;
2397 buff = bfd_malloc (siz);
2398 if (buff == NULL)
2399 goto error_return;
2402 internal_f.f_nscns = 0;
2403 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2404 goto error_return;
2406 for (current = abfd->sections;
2407 current != NULL;
2408 current = current->next)
2410 struct internal_scnhdr section;
2411 bfd_vma vma;
2413 ++internal_f.f_nscns;
2415 strncpy (section.s_name, current->name, sizeof section.s_name);
2417 /* This seems to be correct for Irix 4 shared libraries. */
2418 vma = bfd_get_section_vma (abfd, current);
2419 if (streq (current->name, _LIB))
2420 section.s_vaddr = 0;
2421 else
2422 section.s_vaddr = vma;
2424 section.s_paddr = current->lma;
2425 section.s_size = current->size;
2427 /* If this section is unloadable then the scnptr will be 0. */
2428 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2429 section.s_scnptr = 0;
2430 else
2431 section.s_scnptr = current->filepos;
2432 section.s_relptr = current->rel_filepos;
2434 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2435 object file produced by the assembler is supposed to point to
2436 information about how much room is required by objects of
2437 various different sizes. I think this only matters if we
2438 want the linker to compute the best size to use, or
2439 something. I don't know what happens if the information is
2440 not present. */
2441 if (! streq (current->name, _PDATA))
2442 section.s_lnnoptr = 0;
2443 else
2445 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2446 hold the number of entries in the section (each entry is
2447 8 bytes). We stored this in the line_filepos field in
2448 ecoff_compute_section_file_positions. */
2449 section.s_lnnoptr = current->line_filepos;
2452 section.s_nreloc = current->reloc_count;
2453 section.s_nlnno = 0;
2454 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2455 current->flags);
2457 if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff) == 0
2458 || bfd_bwrite (buff, scnhsz, abfd) != scnhsz)
2459 goto error_return;
2461 if ((section.s_flags & STYP_TEXT) != 0
2462 || ((section.s_flags & STYP_RDATA) != 0
2463 && ecoff_data (abfd)->rdata_in_text)
2464 || section.s_flags == STYP_PDATA
2465 || (section.s_flags & STYP_DYNAMIC) != 0
2466 || (section.s_flags & STYP_LIBLIST) != 0
2467 || (section.s_flags & STYP_RELDYN) != 0
2468 || section.s_flags == STYP_CONFLIC
2469 || (section.s_flags & STYP_DYNSTR) != 0
2470 || (section.s_flags & STYP_DYNSYM) != 0
2471 || (section.s_flags & STYP_HASH) != 0
2472 || (section.s_flags & STYP_ECOFF_INIT) != 0
2473 || (section.s_flags & STYP_ECOFF_FINI) != 0
2474 || section.s_flags == STYP_RCONST)
2476 text_size += current->size;
2477 if (! set_text_start || text_start > vma)
2479 text_start = vma;
2480 set_text_start = TRUE;
2483 else if ((section.s_flags & STYP_RDATA) != 0
2484 || (section.s_flags & STYP_DATA) != 0
2485 || (section.s_flags & STYP_LITA) != 0
2486 || (section.s_flags & STYP_LIT8) != 0
2487 || (section.s_flags & STYP_LIT4) != 0
2488 || (section.s_flags & STYP_SDATA) != 0
2489 || section.s_flags == STYP_XDATA
2490 || (section.s_flags & STYP_GOT) != 0)
2492 data_size += current->size;
2493 if (! set_data_start || data_start > vma)
2495 data_start = vma;
2496 set_data_start = TRUE;
2499 else if ((section.s_flags & STYP_BSS) != 0
2500 || (section.s_flags & STYP_SBSS) != 0)
2501 bss_size += current->size;
2502 else if (section.s_flags == 0
2503 || (section.s_flags & STYP_ECOFF_LIB) != 0
2504 || section.s_flags == STYP_COMMENT)
2505 /* Do nothing. */ ;
2506 else
2507 abort ();
2510 /* Set up the file header. */
2511 internal_f.f_magic = ecoff_get_magic (abfd);
2513 /* We will NOT put a fucking timestamp in the header here. Every
2514 time you put it back, I will come in and take it out again. I'm
2515 sorry. This field does not belong here. We fill it with a 0 so
2516 it compares the same but is not a reasonable time. --
2517 gnu@cygnus.com. */
2518 internal_f.f_timdat = 0;
2520 if (bfd_get_symcount (abfd) != 0)
2522 /* The ECOFF f_nsyms field is not actually the number of
2523 symbols, it's the size of symbolic information header. */
2524 internal_f.f_nsyms = external_hdr_size;
2525 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2527 else
2529 internal_f.f_nsyms = 0;
2530 internal_f.f_symptr = 0;
2533 internal_f.f_opthdr = aoutsz;
2535 internal_f.f_flags = F_LNNO;
2536 if (reloc_size == 0)
2537 internal_f.f_flags |= F_RELFLG;
2538 if (bfd_get_symcount (abfd) == 0)
2539 internal_f.f_flags |= F_LSYMS;
2540 if (abfd->flags & EXEC_P)
2541 internal_f.f_flags |= F_EXEC;
2543 if (bfd_little_endian (abfd))
2544 internal_f.f_flags |= F_AR32WR;
2545 else
2546 internal_f.f_flags |= F_AR32W;
2548 /* Set up the ``optional'' header. */
2549 if ((abfd->flags & D_PAGED) != 0)
2550 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2551 else
2552 internal_a.magic = ECOFF_AOUT_OMAGIC;
2554 /* FIXME: Is this really correct? */
2555 internal_a.vstamp = symhdr->vstamp;
2557 /* At least on Ultrix, these have to be rounded to page boundaries.
2558 FIXME: Is this true on other platforms? */
2559 if ((abfd->flags & D_PAGED) != 0)
2561 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2562 internal_a.text_start = text_start &~ (round - 1);
2563 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2564 internal_a.data_start = data_start &~ (round - 1);
2566 else
2568 internal_a.tsize = text_size;
2569 internal_a.text_start = text_start;
2570 internal_a.dsize = data_size;
2571 internal_a.data_start = data_start;
2574 /* On Ultrix, the initial portions of the .sbss and .bss segments
2575 are at the end of the data section. The bsize field in the
2576 optional header records how many bss bytes are required beyond
2577 those in the data section. The value is not rounded to a page
2578 boundary. */
2579 if (bss_size < internal_a.dsize - data_size)
2580 bss_size = 0;
2581 else
2582 bss_size -= internal_a.dsize - data_size;
2583 internal_a.bsize = bss_size;
2584 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2586 internal_a.entry = bfd_get_start_address (abfd);
2588 internal_a.gp_value = ecoff_data (abfd)->gp;
2590 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2591 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2592 for (i = 0; i < 4; i++)
2593 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2595 /* Let the backend adjust the headers if necessary. */
2596 if (backend->adjust_headers)
2598 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2599 goto error_return;
2602 /* Write out the file header and the optional header. */
2603 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2604 goto error_return;
2606 bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
2607 if (bfd_bwrite (buff, filhsz, abfd) != filhsz)
2608 goto error_return;
2610 bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
2611 if (bfd_bwrite (buff, aoutsz, abfd) != aoutsz)
2612 goto error_return;
2614 /* Build the external symbol information. This must be done before
2615 writing out the relocs so that we know the symbol indices. We
2616 don't do this if this BFD was created by the backend linker,
2617 since it will have already handled the symbols and relocs. */
2618 if (! ecoff_data (abfd)->linker)
2620 symhdr->iextMax = 0;
2621 symhdr->issExtMax = 0;
2622 debug->external_ext = debug->external_ext_end = NULL;
2623 debug->ssext = debug->ssext_end = NULL;
2624 if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2625 (abfd->flags & EXEC_P) == 0,
2626 ecoff_get_extr, ecoff_set_index))
2627 goto error_return;
2629 /* Write out the relocs. */
2630 for (current = abfd->sections;
2631 current != NULL;
2632 current = current->next)
2634 arelent **reloc_ptr_ptr;
2635 arelent **reloc_end;
2636 char *out_ptr;
2637 bfd_size_type amt;
2639 if (current->reloc_count == 0)
2640 continue;
2642 amt = current->reloc_count * external_reloc_size;
2643 reloc_buff = bfd_alloc (abfd, amt);
2644 if (reloc_buff == NULL)
2645 goto error_return;
2647 reloc_ptr_ptr = current->orelocation;
2648 reloc_end = reloc_ptr_ptr + current->reloc_count;
2649 out_ptr = (char *) reloc_buff;
2651 for (;
2652 reloc_ptr_ptr < reloc_end;
2653 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2655 arelent *reloc;
2656 asymbol *sym;
2657 struct internal_reloc in;
2659 memset ((void *) &in, 0, sizeof in);
2661 reloc = *reloc_ptr_ptr;
2662 sym = *reloc->sym_ptr_ptr;
2664 /* If the howto field has not been initialised then skip this reloc.
2665 This assumes that an error message has been issued elsewhere. */
2666 if (reloc->howto == NULL)
2667 continue;
2669 in.r_vaddr = (reloc->address
2670 + bfd_get_section_vma (abfd, current));
2671 in.r_type = reloc->howto->type;
2673 if ((sym->flags & BSF_SECTION_SYM) == 0)
2675 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2676 in.r_extern = 1;
2678 else
2680 const char *name;
2681 unsigned int j;
2682 static struct
2684 const char * name;
2685 long r_symndx;
2687 section_symndx [] =
2689 { _TEXT, RELOC_SECTION_TEXT },
2690 { _RDATA, RELOC_SECTION_RDATA },
2691 { _DATA, RELOC_SECTION_DATA },
2692 { _SDATA, RELOC_SECTION_SDATA },
2693 { _SBSS, RELOC_SECTION_SBSS },
2694 { _BSS, RELOC_SECTION_BSS },
2695 { _INIT, RELOC_SECTION_INIT },
2696 { _LIT8, RELOC_SECTION_LIT8 },
2697 { _LIT4, RELOC_SECTION_LIT4 },
2698 { _XDATA, RELOC_SECTION_XDATA },
2699 { _PDATA, RELOC_SECTION_PDATA },
2700 { _FINI, RELOC_SECTION_FINI },
2701 { _LITA, RELOC_SECTION_LITA },
2702 { "*ABS*", RELOC_SECTION_ABS },
2703 { _RCONST, RELOC_SECTION_RCONST }
2706 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2708 for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
2709 if (streq (name, section_symndx[j].name))
2711 in.r_symndx = section_symndx[j].r_symndx;
2712 break;
2715 if (j == ARRAY_SIZE (section_symndx))
2716 abort ();
2717 in.r_extern = 0;
2720 (*adjust_reloc_out) (abfd, reloc, &in);
2722 (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
2725 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2726 goto error_return;
2727 amt = current->reloc_count * external_reloc_size;
2728 if (bfd_bwrite (reloc_buff, amt, abfd) != amt)
2729 goto error_return;
2730 bfd_release (abfd, reloc_buff);
2731 reloc_buff = NULL;
2734 /* Write out the symbolic debugging information. */
2735 if (bfd_get_symcount (abfd) > 0)
2737 /* Write out the debugging information. */
2738 if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2739 ecoff_data (abfd)->sym_filepos))
2740 goto error_return;
2744 /* The .bss section of a demand paged executable must receive an
2745 entire page. If there are symbols, the symbols will start on the
2746 next page. If there are no symbols, we must fill out the page by
2747 hand. */
2748 if (bfd_get_symcount (abfd) == 0
2749 && (abfd->flags & EXEC_P) != 0
2750 && (abfd->flags & D_PAGED) != 0)
2752 char c;
2754 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2755 SEEK_SET) != 0)
2756 goto error_return;
2757 if (bfd_bread (&c, (bfd_size_type) 1, abfd) == 0)
2758 c = 0;
2759 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2760 SEEK_SET) != 0)
2761 goto error_return;
2762 if (bfd_bwrite (&c, (bfd_size_type) 1, abfd) != 1)
2763 goto error_return;
2766 if (reloc_buff != NULL)
2767 bfd_release (abfd, reloc_buff);
2768 if (buff != NULL)
2769 free (buff);
2770 return TRUE;
2771 error_return:
2772 if (reloc_buff != NULL)
2773 bfd_release (abfd, reloc_buff);
2774 if (buff != NULL)
2775 free (buff);
2776 return FALSE;
2779 /* Archive handling. ECOFF uses what appears to be a unique type of
2780 archive header (armap). The byte ordering of the armap and the
2781 contents are encoded in the name of the armap itself. At least for
2782 now, we only support archives with the same byte ordering in the
2783 armap and the contents.
2785 The first four bytes in the armap are the number of symbol
2786 definitions. This is always a power of two.
2788 This is followed by the symbol definitions. Each symbol definition
2789 occupies 8 bytes. The first four bytes are the offset from the
2790 start of the armap strings to the null-terminated string naming
2791 this symbol. The second four bytes are the file offset to the
2792 archive member which defines this symbol. If the second four bytes
2793 are 0, then this is not actually a symbol definition, and it should
2794 be ignored.
2796 The symbols are hashed into the armap with a closed hashing scheme.
2797 See the functions below for the details of the algorithm.
2799 After the symbol definitions comes four bytes holding the size of
2800 the string table, followed by the string table itself. */
2802 /* The name of an archive headers looks like this:
2803 __________E[BL]E[BL]_ (with a trailing space).
2804 The trailing space is changed to an X if the archive is changed to
2805 indicate that the armap is out of date.
2807 The Alpha seems to use ________64E[BL]E[BL]_. */
2809 #define ARMAP_BIG_ENDIAN 'B'
2810 #define ARMAP_LITTLE_ENDIAN 'L'
2811 #define ARMAP_MARKER 'E'
2812 #define ARMAP_START_LENGTH 10
2813 #define ARMAP_HEADER_MARKER_INDEX 10
2814 #define ARMAP_HEADER_ENDIAN_INDEX 11
2815 #define ARMAP_OBJECT_MARKER_INDEX 12
2816 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2817 #define ARMAP_END_INDEX 14
2818 #define ARMAP_END "_ "
2820 /* This is a magic number used in the hashing algorithm. */
2821 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2823 /* This returns the hash value to use for a string. It also sets
2824 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2825 is the number of entries in the hash table, and HLOG is the log
2826 base 2 of SIZE. */
2828 static unsigned int
2829 ecoff_armap_hash (const char *s,
2830 unsigned int *rehash,
2831 unsigned int size,
2832 unsigned int hlog)
2834 unsigned int hash;
2836 if (hlog == 0)
2837 return 0;
2838 hash = *s++;
2839 while (*s != '\0')
2840 hash = ((hash >> 27) | (hash << 5)) + *s++;
2841 hash *= ARMAP_HASH_MAGIC;
2842 *rehash = (hash & (size - 1)) | 1;
2843 return hash >> (32 - hlog);
2846 /* Read in the armap. */
2848 bfd_boolean
2849 _bfd_ecoff_slurp_armap (bfd *abfd)
2851 char nextname[17];
2852 unsigned int i;
2853 struct areltdata *mapdata;
2854 bfd_size_type parsed_size;
2855 char *raw_armap;
2856 struct artdata *ardata;
2857 unsigned int count;
2858 char *raw_ptr;
2859 carsym *symdef_ptr;
2860 char *stringbase;
2861 bfd_size_type amt;
2863 /* Get the name of the first element. */
2864 i = bfd_bread ((void *) nextname, (bfd_size_type) 16, abfd);
2865 if (i == 0)
2866 return TRUE;
2867 if (i != 16)
2868 return FALSE;
2870 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2871 return FALSE;
2873 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2874 standard COFF armap. We could move the ECOFF armap stuff into
2875 bfd_slurp_armap, but that seems inappropriate since no other
2876 target uses this format. Instead, we check directly for a COFF
2877 armap. */
2878 if (CONST_STRNEQ (nextname, "/ "))
2879 return bfd_slurp_armap (abfd);
2881 /* See if the first element is an armap. */
2882 if (! strneq (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH)
2883 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2884 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2885 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2886 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2887 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2888 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2889 || ! strneq (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1))
2891 bfd_has_map (abfd) = FALSE;
2892 return TRUE;
2895 /* Make sure we have the right byte ordering. */
2896 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2897 ^ (bfd_header_big_endian (abfd)))
2898 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2899 ^ (bfd_big_endian (abfd))))
2901 bfd_set_error (bfd_error_wrong_format);
2902 return FALSE;
2905 /* Read in the armap. */
2906 ardata = bfd_ardata (abfd);
2907 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2908 if (mapdata == NULL)
2909 return FALSE;
2910 parsed_size = mapdata->parsed_size;
2911 bfd_release (abfd, (void *) mapdata);
2913 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
2914 if (raw_armap == NULL)
2915 return FALSE;
2917 if (bfd_bread ((void *) raw_armap, parsed_size, abfd) != parsed_size)
2919 if (bfd_get_error () != bfd_error_system_call)
2920 bfd_set_error (bfd_error_malformed_archive);
2921 bfd_release (abfd, (void *) raw_armap);
2922 return FALSE;
2925 ardata->tdata = (void *) raw_armap;
2927 count = H_GET_32 (abfd, raw_armap);
2929 ardata->symdef_count = 0;
2930 ardata->cache = NULL;
2932 /* This code used to overlay the symdefs over the raw archive data,
2933 but that doesn't work on a 64 bit host. */
2934 stringbase = raw_armap + count * 8 + 8;
2936 #ifdef CHECK_ARMAP_HASH
2938 unsigned int hlog;
2940 /* Double check that I have the hashing algorithm right by making
2941 sure that every symbol can be looked up successfully. */
2942 hlog = 0;
2943 for (i = 1; i < count; i <<= 1)
2944 hlog++;
2945 BFD_ASSERT (i == count);
2947 raw_ptr = raw_armap + 4;
2948 for (i = 0; i < count; i++, raw_ptr += 8)
2950 unsigned int name_offset, file_offset;
2951 unsigned int hash, rehash, srch;
2953 name_offset = H_GET_32 (abfd, raw_ptr);
2954 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
2955 if (file_offset == 0)
2956 continue;
2957 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
2958 hlog);
2959 if (hash == i)
2960 continue;
2962 /* See if we can rehash to this location. */
2963 for (srch = (hash + rehash) & (count - 1);
2964 srch != hash && srch != i;
2965 srch = (srch + rehash) & (count - 1))
2966 BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
2967 BFD_ASSERT (srch == i);
2971 #endif /* CHECK_ARMAP_HASH */
2973 raw_ptr = raw_armap + 4;
2974 for (i = 0; i < count; i++, raw_ptr += 8)
2975 if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
2976 ++ardata->symdef_count;
2978 amt = ardata->symdef_count;
2979 amt *= sizeof (carsym);
2980 symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
2981 if (!symdef_ptr)
2982 return FALSE;
2984 ardata->symdefs = symdef_ptr;
2986 raw_ptr = raw_armap + 4;
2987 for (i = 0; i < count; i++, raw_ptr += 8)
2989 unsigned int name_offset, file_offset;
2991 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
2992 if (file_offset == 0)
2993 continue;
2994 name_offset = H_GET_32 (abfd, raw_ptr);
2995 symdef_ptr->name = stringbase + name_offset;
2996 symdef_ptr->file_offset = file_offset;
2997 ++symdef_ptr;
3000 ardata->first_file_filepos = bfd_tell (abfd);
3001 /* Pad to an even boundary. */
3002 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3004 bfd_has_map (abfd) = TRUE;
3006 return TRUE;
3009 /* Write out an armap. */
3011 bfd_boolean
3012 _bfd_ecoff_write_armap (bfd *abfd,
3013 unsigned int elength,
3014 struct orl *map,
3015 unsigned int orl_count,
3016 int stridx)
3018 unsigned int hashsize, hashlog;
3019 bfd_size_type symdefsize;
3020 int padit;
3021 unsigned int stringsize;
3022 unsigned int mapsize;
3023 file_ptr firstreal;
3024 struct ar_hdr hdr;
3025 struct stat statbuf;
3026 unsigned int i;
3027 bfd_byte temp[4];
3028 bfd_byte *hashtable;
3029 bfd *current;
3030 bfd *last_elt;
3032 /* Ultrix appears to use as a hash table size the least power of two
3033 greater than twice the number of entries. */
3034 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3036 hashsize = 1 << hashlog;
3038 symdefsize = hashsize * 8;
3039 padit = stridx % 2;
3040 stringsize = stridx + padit;
3042 /* Include 8 bytes to store symdefsize and stringsize in output. */
3043 mapsize = symdefsize + stringsize + 8;
3045 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3047 memset ((void *) &hdr, 0, sizeof hdr);
3049 /* Work out the ECOFF armap name. */
3050 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3051 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3052 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3053 (bfd_header_big_endian (abfd)
3054 ? ARMAP_BIG_ENDIAN
3055 : ARMAP_LITTLE_ENDIAN);
3056 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3057 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3058 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3059 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3061 /* Write the timestamp of the archive header to be just a little bit
3062 later than the timestamp of the file, otherwise the linker will
3063 complain that the index is out of date. Actually, the Ultrix
3064 linker just checks the archive name; the GNU linker may check the
3065 date. */
3066 stat (abfd->filename, &statbuf);
3067 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
3068 (long) (statbuf.st_mtime + 60));
3070 /* The DECstation uses zeroes for the uid, gid and mode of the
3071 armap. */
3072 hdr.ar_uid[0] = '0';
3073 hdr.ar_gid[0] = '0';
3074 /* Building gcc ends up extracting the armap as a file - twice. */
3075 hdr.ar_mode[0] = '6';
3076 hdr.ar_mode[1] = '4';
3077 hdr.ar_mode[2] = '4';
3079 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
3081 hdr.ar_fmag[0] = '`';
3082 hdr.ar_fmag[1] = '\012';
3084 /* Turn all null bytes in the header into spaces. */
3085 for (i = 0; i < sizeof (struct ar_hdr); i++)
3086 if (((char *) (&hdr))[i] == '\0')
3087 (((char *) (&hdr))[i]) = ' ';
3089 if (bfd_bwrite ((void *) &hdr, (bfd_size_type) sizeof (struct ar_hdr), abfd)
3090 != sizeof (struct ar_hdr))
3091 return FALSE;
3093 H_PUT_32 (abfd, hashsize, temp);
3094 if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4)
3095 return FALSE;
3097 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3098 if (!hashtable)
3099 return FALSE;
3101 current = abfd->archive_head;
3102 last_elt = current;
3103 for (i = 0; i < orl_count; i++)
3105 unsigned int hash, rehash = 0;
3107 /* Advance firstreal to the file position of this archive
3108 element. */
3109 if (map[i].u.abfd != last_elt)
3113 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3114 firstreal += firstreal % 2;
3115 current = current->archive_next;
3117 while (current != map[i].u.abfd);
3120 last_elt = current;
3122 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3123 if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
3125 unsigned int srch;
3127 /* The desired slot is already taken. */
3128 for (srch = (hash + rehash) & (hashsize - 1);
3129 srch != hash;
3130 srch = (srch + rehash) & (hashsize - 1))
3131 if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
3132 break;
3134 BFD_ASSERT (srch != hash);
3136 hash = srch;
3139 H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
3140 H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
3143 if (bfd_bwrite ((void *) hashtable, symdefsize, abfd) != symdefsize)
3144 return FALSE;
3146 bfd_release (abfd, hashtable);
3148 /* Now write the strings. */
3149 H_PUT_32 (abfd, stringsize, temp);
3150 if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4)
3151 return FALSE;
3152 for (i = 0; i < orl_count; i++)
3154 bfd_size_type len;
3156 len = strlen (*map[i].name) + 1;
3157 if (bfd_bwrite ((void *) (*map[i].name), len, abfd) != len)
3158 return FALSE;
3161 /* The spec sez this should be a newline. But in order to be
3162 bug-compatible for DECstation ar we use a null. */
3163 if (padit)
3165 if (bfd_bwrite ("", (bfd_size_type) 1, abfd) != 1)
3166 return FALSE;
3169 return TRUE;
3172 /* ECOFF linker code. */
3174 /* Routine to create an entry in an ECOFF link hash table. */
3176 static struct bfd_hash_entry *
3177 ecoff_link_hash_newfunc (struct bfd_hash_entry *entry,
3178 struct bfd_hash_table *table,
3179 const char *string)
3181 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3183 /* Allocate the structure if it has not already been allocated by a
3184 subclass. */
3185 if (ret == NULL)
3186 ret = ((struct ecoff_link_hash_entry *)
3187 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3188 if (ret == NULL)
3189 return NULL;
3191 /* Call the allocation method of the superclass. */
3192 ret = ((struct ecoff_link_hash_entry *)
3193 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3194 table, string));
3196 if (ret)
3198 /* Set local fields. */
3199 ret->indx = -1;
3200 ret->abfd = NULL;
3201 ret->written = 0;
3202 ret->small = 0;
3204 memset ((void *) &ret->esym, 0, sizeof ret->esym);
3206 return (struct bfd_hash_entry *) ret;
3209 /* Create an ECOFF link hash table. */
3211 struct bfd_link_hash_table *
3212 _bfd_ecoff_bfd_link_hash_table_create (bfd *abfd)
3214 struct ecoff_link_hash_table *ret;
3215 bfd_size_type amt = sizeof (struct ecoff_link_hash_table);
3217 ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3218 if (ret == NULL)
3219 return NULL;
3220 if (!_bfd_link_hash_table_init (&ret->root, abfd,
3221 ecoff_link_hash_newfunc,
3222 sizeof (struct ecoff_link_hash_entry)))
3224 free (ret);
3225 return NULL;
3227 return &ret->root;
3230 /* Look up an entry in an ECOFF link hash table. */
3232 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3233 ((struct ecoff_link_hash_entry *) \
3234 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3236 /* Get the ECOFF link hash table from the info structure. This is
3237 just a cast. */
3239 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3241 /* Add the external symbols of an object file to the global linker
3242 hash table. The external symbols and strings we are passed are
3243 just allocated on the stack, and will be discarded. We must
3244 explicitly save any information we may need later on in the link.
3245 We do not want to read the external symbol information again. */
3247 static bfd_boolean
3248 ecoff_link_add_externals (bfd *abfd,
3249 struct bfd_link_info *info,
3250 void * external_ext,
3251 char *ssext)
3253 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3254 void (* const swap_ext_in) (bfd *, void *, EXTR *)
3255 = backend->debug_swap.swap_ext_in;
3256 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3257 unsigned long ext_count;
3258 struct bfd_link_hash_entry **sym_hash;
3259 char *ext_ptr;
3260 char *ext_end;
3261 bfd_size_type amt;
3263 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3265 amt = ext_count;
3266 amt *= sizeof (struct bfd_link_hash_entry *);
3267 sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
3268 if (!sym_hash)
3269 return FALSE;
3270 ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
3272 ext_ptr = (char *) external_ext;
3273 ext_end = ext_ptr + ext_count * external_ext_size;
3274 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3276 EXTR esym;
3277 bfd_boolean skip;
3278 bfd_vma value;
3279 asection *section;
3280 const char *name;
3281 struct ecoff_link_hash_entry *h;
3283 *sym_hash = NULL;
3285 (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
3287 /* Skip debugging symbols. */
3288 skip = FALSE;
3289 switch (esym.asym.st)
3291 case stGlobal:
3292 case stStatic:
3293 case stLabel:
3294 case stProc:
3295 case stStaticProc:
3296 break;
3297 default:
3298 skip = TRUE;
3299 break;
3302 if (skip)
3303 continue;
3305 /* Get the information for this symbol. */
3306 value = esym.asym.value;
3307 switch (esym.asym.sc)
3309 default:
3310 case scNil:
3311 case scRegister:
3312 case scCdbLocal:
3313 case scBits:
3314 case scCdbSystem:
3315 case scRegImage:
3316 case scInfo:
3317 case scUserStruct:
3318 case scVar:
3319 case scVarRegister:
3320 case scVariant:
3321 case scBasedVar:
3322 case scXData:
3323 case scPData:
3324 section = NULL;
3325 break;
3326 case scText:
3327 section = bfd_make_section_old_way (abfd, _TEXT);
3328 value -= section->vma;
3329 break;
3330 case scData:
3331 section = bfd_make_section_old_way (abfd, _DATA);
3332 value -= section->vma;
3333 break;
3334 case scBss:
3335 section = bfd_make_section_old_way (abfd, _BSS);
3336 value -= section->vma;
3337 break;
3338 case scAbs:
3339 section = bfd_abs_section_ptr;
3340 break;
3341 case scUndefined:
3342 section = bfd_und_section_ptr;
3343 break;
3344 case scSData:
3345 section = bfd_make_section_old_way (abfd, _SDATA);
3346 value -= section->vma;
3347 break;
3348 case scSBss:
3349 section = bfd_make_section_old_way (abfd, _SBSS);
3350 value -= section->vma;
3351 break;
3352 case scRData:
3353 section = bfd_make_section_old_way (abfd, _RDATA);
3354 value -= section->vma;
3355 break;
3356 case scCommon:
3357 if (value > ecoff_data (abfd)->gp_size)
3359 section = bfd_com_section_ptr;
3360 break;
3362 /* Fall through. */
3363 case scSCommon:
3364 if (ecoff_scom_section.name == NULL)
3366 /* Initialize the small common section. */
3367 ecoff_scom_section.name = SCOMMON;
3368 ecoff_scom_section.flags = SEC_IS_COMMON;
3369 ecoff_scom_section.output_section = &ecoff_scom_section;
3370 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3371 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3372 ecoff_scom_symbol.name = SCOMMON;
3373 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3374 ecoff_scom_symbol.section = &ecoff_scom_section;
3375 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3377 section = &ecoff_scom_section;
3378 break;
3379 case scSUndefined:
3380 section = bfd_und_section_ptr;
3381 break;
3382 case scInit:
3383 section = bfd_make_section_old_way (abfd, _INIT);
3384 value -= section->vma;
3385 break;
3386 case scFini:
3387 section = bfd_make_section_old_way (abfd, _FINI);
3388 value -= section->vma;
3389 break;
3390 case scRConst:
3391 section = bfd_make_section_old_way (abfd, _RCONST);
3392 value -= section->vma;
3393 break;
3396 if (section == NULL)
3397 continue;
3399 name = ssext + esym.asym.iss;
3401 if (! (_bfd_generic_link_add_one_symbol
3402 (info, abfd, name,
3403 (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3404 section, value, NULL, TRUE, TRUE, sym_hash)))
3405 return FALSE;
3407 h = (struct ecoff_link_hash_entry *) *sym_hash;
3409 /* If we are building an ECOFF hash table, save the external
3410 symbol information. */
3411 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
3413 if (h->abfd == NULL
3414 || (! bfd_is_und_section (section)
3415 && (! bfd_is_com_section (section)
3416 || (h->root.type != bfd_link_hash_defined
3417 && h->root.type != bfd_link_hash_defweak))))
3419 h->abfd = abfd;
3420 h->esym = esym;
3423 /* Remember whether this symbol was small undefined. */
3424 if (esym.asym.sc == scSUndefined)
3425 h->small = 1;
3427 /* If this symbol was ever small undefined, it needs to wind
3428 up in a GP relative section. We can't control the
3429 section of a defined symbol, but we can control the
3430 section of a common symbol. This case is actually needed
3431 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
3432 if (h->small
3433 && h->root.type == bfd_link_hash_common
3434 && streq (h->root.u.c.p->section->name, SCOMMON))
3436 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3437 SCOMMON);
3438 h->root.u.c.p->section->flags = SEC_ALLOC;
3439 if (h->esym.asym.sc == scCommon)
3440 h->esym.asym.sc = scSCommon;
3445 return TRUE;
3448 /* Add symbols from an ECOFF object file to the global linker hash
3449 table. */
3451 static bfd_boolean
3452 ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3454 HDRR *symhdr;
3455 bfd_size_type external_ext_size;
3456 void * external_ext = NULL;
3457 bfd_size_type esize;
3458 char *ssext = NULL;
3459 bfd_boolean result;
3461 if (! ecoff_slurp_symbolic_header (abfd))
3462 return FALSE;
3464 /* If there are no symbols, we don't want it. */
3465 if (bfd_get_symcount (abfd) == 0)
3466 return TRUE;
3468 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3470 /* Read in the external symbols and external strings. */
3471 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3472 esize = symhdr->iextMax * external_ext_size;
3473 external_ext = bfd_malloc (esize);
3474 if (external_ext == NULL && esize != 0)
3475 goto error_return;
3477 if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
3478 || bfd_bread (external_ext, esize, abfd) != esize)
3479 goto error_return;
3481 ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
3482 if (ssext == NULL && symhdr->issExtMax != 0)
3483 goto error_return;
3485 if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
3486 || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
3487 != (bfd_size_type) symhdr->issExtMax))
3488 goto error_return;
3490 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3492 if (ssext != NULL)
3493 free (ssext);
3494 if (external_ext != NULL)
3495 free (external_ext);
3496 return result;
3498 error_return:
3499 if (ssext != NULL)
3500 free (ssext);
3501 if (external_ext != NULL)
3502 free (external_ext);
3503 return FALSE;
3506 /* Factored out from ecoff_link_check_archive_element. */
3508 static bfd_boolean
3509 read_ext_syms_and_strs (HDRR **symhdr, bfd_size_type *external_ext_size,
3510 bfd_size_type *esize, void **external_ext, char **ssext, bfd *abfd,
3511 const struct ecoff_backend_data * const backend)
3513 if (! ecoff_slurp_symbolic_header (abfd))
3514 return FALSE;
3516 /* If there are no symbols, we don't want it. */
3517 if (bfd_get_symcount (abfd) == 0)
3518 return TRUE;
3520 *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3522 *external_ext_size = backend->debug_swap.external_ext_size;
3523 *esize = (*symhdr)->iextMax * *external_ext_size;
3524 *external_ext = bfd_malloc (*esize);
3525 if (*external_ext == NULL && *esize != 0)
3526 return FALSE;
3528 if (bfd_seek (abfd, (file_ptr) (*symhdr)->cbExtOffset, SEEK_SET) != 0
3529 || bfd_bread (*external_ext, *esize, abfd) != *esize)
3530 return FALSE;
3532 *ssext = (char *) bfd_malloc ((bfd_size_type) (*symhdr)->issExtMax);
3533 if (*ssext == NULL && (*symhdr)->issExtMax != 0)
3534 return FALSE;
3536 if (bfd_seek (abfd, (file_ptr) (*symhdr)->cbSsExtOffset, SEEK_SET) != 0
3537 || (bfd_bread (*ssext, (bfd_size_type) (*symhdr)->issExtMax, abfd)
3538 != (bfd_size_type) (*symhdr)->issExtMax))
3539 return FALSE;
3540 return TRUE;
3543 static bfd_boolean
3544 reread_ext_syms_and_strs (HDRR **symhdr, bfd_size_type *external_ext_size,
3545 bfd_size_type *esize, void **external_ext, char **ssext, bfd *abfd,
3546 const struct ecoff_backend_data * const backend)
3548 if (*external_ext != NULL)
3549 free (*external_ext);
3550 *external_ext = NULL;
3551 if (*ssext != NULL)
3552 free (*ssext);
3553 *ssext = NULL;
3554 return read_ext_syms_and_strs (symhdr, external_ext_size, esize,
3555 external_ext, ssext, abfd, backend);
3558 /* This is called if we used _bfd_generic_link_add_archive_symbols
3559 because we were not dealing with an ECOFF archive. */
3561 static bfd_boolean
3562 ecoff_link_check_archive_element (bfd *abfd,
3563 struct bfd_link_info *info,
3564 bfd_boolean *pneeded)
3566 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3567 void (* const swap_ext_in) (bfd *, void *, EXTR *)
3568 = backend->debug_swap.swap_ext_in;
3569 HDRR *symhdr;
3570 bfd_size_type external_ext_size;
3571 void * external_ext = NULL;
3572 bfd_size_type esize;
3573 char *ssext = NULL;
3574 char *ext_ptr;
3575 char *ext_end;
3577 *pneeded = FALSE;
3579 /* Read in the external symbols and external strings. */
3580 if (!read_ext_syms_and_strs (&symhdr, &external_ext_size, &esize,
3581 &external_ext, &ssext, abfd, backend))
3582 goto error_return;
3584 /* If there are no symbols, we don't want it. */
3585 if (bfd_get_symcount (abfd) == 0)
3586 goto successful_return;
3588 /* Look through the external symbols to see if they define some
3589 symbol that is currently undefined. */
3590 ext_ptr = (char *) external_ext;
3591 ext_end = ext_ptr + esize;
3592 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3594 EXTR esym;
3595 bfd_boolean def;
3596 const char *name;
3597 bfd *oldbfd;
3598 struct bfd_link_hash_entry *h;
3600 (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
3602 /* See if this symbol defines something. */
3603 if (esym.asym.st != stGlobal
3604 && esym.asym.st != stLabel
3605 && esym.asym.st != stProc)
3606 continue;
3608 switch (esym.asym.sc)
3610 case scText:
3611 case scData:
3612 case scBss:
3613 case scAbs:
3614 case scSData:
3615 case scSBss:
3616 case scRData:
3617 case scCommon:
3618 case scSCommon:
3619 case scInit:
3620 case scFini:
3621 case scRConst:
3622 def = TRUE;
3623 break;
3624 default:
3625 def = FALSE;
3626 break;
3629 if (! def)
3630 continue;
3632 name = ssext + esym.asym.iss;
3633 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
3635 /* Unlike the generic linker, we do not pull in elements because
3636 of common symbols. */
3637 if (h == NULL
3638 || h->type != bfd_link_hash_undefined)
3639 continue;
3641 /* Include this element. */
3642 oldbfd = abfd;
3643 if (!(*info->callbacks
3644 ->add_archive_element) (info, abfd, name, &abfd))
3645 goto error_return;
3646 /* Potentially, the add_archive_element hook may have set a
3647 substitute BFD for us. */
3648 if (abfd != oldbfd
3649 && !reread_ext_syms_and_strs (&symhdr, &external_ext_size, &esize,
3650 &external_ext, &ssext, abfd, backend))
3651 goto error_return;
3652 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3653 goto error_return;
3655 *pneeded = TRUE;
3656 goto successful_return;
3659 successful_return:
3660 if (external_ext != NULL)
3661 free (external_ext);
3662 if (ssext != NULL)
3663 free (ssext);
3664 return TRUE;
3665 error_return:
3666 if (external_ext != NULL)
3667 free (external_ext);
3668 if (ssext != NULL)
3669 free (ssext);
3670 return FALSE;
3673 /* Add the symbols from an archive file to the global hash table.
3674 This looks through the undefined symbols, looks each one up in the
3675 archive hash table, and adds any associated object file. We do not
3676 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3677 already have a hash table, so there is no reason to construct
3678 another one. */
3680 static bfd_boolean
3681 ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
3683 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3684 const bfd_byte *raw_armap;
3685 struct bfd_link_hash_entry **pundef;
3686 unsigned int armap_count;
3687 unsigned int armap_log;
3688 unsigned int i;
3689 const bfd_byte *hashtable;
3690 const char *stringbase;
3692 if (! bfd_has_map (abfd))
3694 /* An empty archive is a special case. */
3695 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
3696 return TRUE;
3697 bfd_set_error (bfd_error_no_armap);
3698 return FALSE;
3701 /* If we don't have any raw data for this archive, as can happen on
3702 Irix 4.0.5F, we call the generic routine.
3703 FIXME: We should be more clever about this, since someday tdata
3704 may get to something for a generic archive. */
3705 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3706 if (raw_armap == NULL)
3707 return (_bfd_generic_link_add_archive_symbols
3708 (abfd, info, ecoff_link_check_archive_element));
3710 armap_count = H_GET_32 (abfd, raw_armap);
3712 armap_log = 0;
3713 for (i = 1; i < armap_count; i <<= 1)
3714 armap_log++;
3715 BFD_ASSERT (i == armap_count);
3717 hashtable = raw_armap + 4;
3718 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3720 /* Look through the list of undefined symbols. */
3721 pundef = &info->hash->undefs;
3722 while (*pundef != NULL)
3724 struct bfd_link_hash_entry *h;
3725 unsigned int hash, rehash = 0;
3726 unsigned int file_offset;
3727 const char *name;
3728 bfd *element;
3730 h = *pundef;
3732 /* When a symbol is defined, it is not necessarily removed from
3733 the list. */
3734 if (h->type != bfd_link_hash_undefined
3735 && h->type != bfd_link_hash_common)
3737 /* Remove this entry from the list, for general cleanliness
3738 and because we are going to look through the list again
3739 if we search any more libraries. We can't remove the
3740 entry if it is the tail, because that would lose any
3741 entries we add to the list later on. */
3742 if (*pundef != info->hash->undefs_tail)
3743 *pundef = (*pundef)->u.undef.next;
3744 else
3745 pundef = &(*pundef)->u.undef.next;
3746 continue;
3749 /* Native ECOFF linkers do not pull in archive elements merely
3750 to satisfy common definitions, so neither do we. We leave
3751 them on the list, though, in case we are linking against some
3752 other object format. */
3753 if (h->type != bfd_link_hash_undefined)
3755 pundef = &(*pundef)->u.undef.next;
3756 continue;
3759 /* Look for this symbol in the archive hash table. */
3760 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3761 armap_log);
3763 file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3764 if (file_offset == 0)
3766 /* Nothing in this slot. */
3767 pundef = &(*pundef)->u.undef.next;
3768 continue;
3771 name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3772 if (name[0] != h->root.string[0]
3773 || ! streq (name, h->root.string))
3775 unsigned int srch;
3776 bfd_boolean found;
3778 /* That was the wrong symbol. Try rehashing. */
3779 found = FALSE;
3780 for (srch = (hash + rehash) & (armap_count - 1);
3781 srch != hash;
3782 srch = (srch + rehash) & (armap_count - 1))
3784 file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3785 if (file_offset == 0)
3786 break;
3787 name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3788 if (name[0] == h->root.string[0]
3789 && streq (name, h->root.string))
3791 found = TRUE;
3792 break;
3796 if (! found)
3798 pundef = &(*pundef)->u.undef.next;
3799 continue;
3802 hash = srch;
3805 element = (*backend->get_elt_at_filepos) (abfd, (file_ptr) file_offset);
3806 if (element == NULL)
3807 return FALSE;
3809 if (! bfd_check_format (element, bfd_object))
3810 return FALSE;
3812 /* Unlike the generic linker, we know that this element provides
3813 a definition for an undefined symbol and we know that we want
3814 to include it. We don't need to check anything. */
3815 if (!(*info->callbacks
3816 ->add_archive_element) (info, element, name, &element))
3817 return FALSE;
3818 if (! ecoff_link_add_object_symbols (element, info))
3819 return FALSE;
3821 pundef = &(*pundef)->u.undef.next;
3824 return TRUE;
3827 /* Given an ECOFF BFD, add symbols to the global hash table as
3828 appropriate. */
3830 bfd_boolean
3831 _bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
3833 switch (bfd_get_format (abfd))
3835 case bfd_object:
3836 return ecoff_link_add_object_symbols (abfd, info);
3837 case bfd_archive:
3838 return ecoff_link_add_archive_symbols (abfd, info);
3839 default:
3840 bfd_set_error (bfd_error_wrong_format);
3841 return FALSE;
3846 /* ECOFF final link routines. */
3848 /* Structure used to pass information to ecoff_link_write_external. */
3850 struct extsym_info
3852 bfd *abfd;
3853 struct bfd_link_info *info;
3856 /* Accumulate the debugging information for an input BFD into the
3857 output BFD. This must read in the symbolic information of the
3858 input BFD. */
3860 static bfd_boolean
3861 ecoff_final_link_debug_accumulate (bfd *output_bfd,
3862 bfd *input_bfd,
3863 struct bfd_link_info *info,
3864 void * handle)
3866 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
3867 const struct ecoff_debug_swap * const swap =
3868 &ecoff_backend (input_bfd)->debug_swap;
3869 HDRR *symhdr = &debug->symbolic_header;
3870 bfd_boolean ret;
3872 #define READ(ptr, offset, count, size, type) \
3873 if (symhdr->count == 0) \
3874 debug->ptr = NULL; \
3875 else \
3877 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
3878 debug->ptr = (type) bfd_malloc (amt); \
3879 if (debug->ptr == NULL) \
3881 ret = FALSE; \
3882 goto return_something; \
3884 if (bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3885 || bfd_bread (debug->ptr, amt, input_bfd) != amt) \
3887 ret = FALSE; \
3888 goto return_something; \
3892 /* If raw_syments is not NULL, then the data was already by read by
3893 _bfd_ecoff_slurp_symbolic_info. */
3894 if (ecoff_data (input_bfd)->raw_syments == NULL)
3896 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
3897 unsigned char *);
3898 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
3899 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
3900 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
3901 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
3902 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3903 union aux_ext *);
3904 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3905 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
3906 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
3908 #undef READ
3910 /* We do not read the external strings or the external symbols. */
3912 ret = (bfd_ecoff_debug_accumulate
3913 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
3914 &ecoff_backend (output_bfd)->debug_swap,
3915 input_bfd, debug, swap, info));
3917 return_something:
3918 if (ecoff_data (input_bfd)->raw_syments == NULL)
3920 if (debug->line != NULL)
3921 free (debug->line);
3922 if (debug->external_dnr != NULL)
3923 free (debug->external_dnr);
3924 if (debug->external_pdr != NULL)
3925 free (debug->external_pdr);
3926 if (debug->external_sym != NULL)
3927 free (debug->external_sym);
3928 if (debug->external_opt != NULL)
3929 free (debug->external_opt);
3930 if (debug->external_aux != NULL)
3931 free (debug->external_aux);
3932 if (debug->ss != NULL)
3933 free (debug->ss);
3934 if (debug->external_fdr != NULL)
3935 free (debug->external_fdr);
3936 if (debug->external_rfd != NULL)
3937 free (debug->external_rfd);
3939 /* Make sure we don't accidentally follow one of these pointers
3940 into freed memory. */
3941 debug->line = NULL;
3942 debug->external_dnr = NULL;
3943 debug->external_pdr = NULL;
3944 debug->external_sym = NULL;
3945 debug->external_opt = NULL;
3946 debug->external_aux = NULL;
3947 debug->ss = NULL;
3948 debug->external_fdr = NULL;
3949 debug->external_rfd = NULL;
3952 return ret;
3955 /* Relocate and write an ECOFF section into an ECOFF output file. */
3957 static bfd_boolean
3958 ecoff_indirect_link_order (bfd *output_bfd,
3959 struct bfd_link_info *info,
3960 asection *output_section,
3961 struct bfd_link_order *link_order)
3963 asection *input_section;
3964 bfd *input_bfd;
3965 bfd_byte *contents = NULL;
3966 bfd_size_type external_reloc_size;
3967 bfd_size_type external_relocs_size;
3968 void * external_relocs = NULL;
3970 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
3972 input_section = link_order->u.indirect.section;
3973 input_bfd = input_section->owner;
3974 if (input_section->size == 0)
3975 return TRUE;
3977 BFD_ASSERT (input_section->output_section == output_section);
3978 BFD_ASSERT (input_section->output_offset == link_order->offset);
3979 BFD_ASSERT (input_section->size == link_order->size);
3981 /* Get the section contents. */
3982 if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
3983 goto error_return;
3985 /* Get the relocs. If we are relaxing MIPS code, they will already
3986 have been read in. Otherwise, we read them in now. */
3987 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
3988 external_relocs_size = external_reloc_size * input_section->reloc_count;
3990 external_relocs = bfd_malloc (external_relocs_size);
3991 if (external_relocs == NULL && external_relocs_size != 0)
3992 goto error_return;
3994 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
3995 || (bfd_bread (external_relocs, external_relocs_size, input_bfd)
3996 != external_relocs_size))
3997 goto error_return;
3999 /* Relocate the section contents. */
4000 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4001 (output_bfd, info, input_bfd, input_section, contents,
4002 external_relocs)))
4003 goto error_return;
4005 /* Write out the relocated section. */
4006 if (! bfd_set_section_contents (output_bfd,
4007 output_section,
4008 contents,
4009 input_section->output_offset,
4010 input_section->size))
4011 goto error_return;
4013 /* If we are producing relocatable output, the relocs were
4014 modified, and we write them out now. We use the reloc_count
4015 field of output_section to keep track of the number of relocs we
4016 have output so far. */
4017 if (info->relocatable)
4019 file_ptr pos = (output_section->rel_filepos
4020 + output_section->reloc_count * external_reloc_size);
4021 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4022 || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd)
4023 != external_relocs_size))
4024 goto error_return;
4025 output_section->reloc_count += input_section->reloc_count;
4028 if (contents != NULL)
4029 free (contents);
4030 if (external_relocs != NULL)
4031 free (external_relocs);
4032 return TRUE;
4034 error_return:
4035 if (contents != NULL)
4036 free (contents);
4037 if (external_relocs != NULL)
4038 free (external_relocs);
4039 return FALSE;
4042 /* Generate a reloc when linking an ECOFF file. This is a reloc
4043 requested by the linker, and does come from any input file. This
4044 is used to build constructor and destructor tables when linking
4045 with -Ur. */
4047 static bfd_boolean
4048 ecoff_reloc_link_order (bfd *output_bfd,
4049 struct bfd_link_info *info,
4050 asection *output_section,
4051 struct bfd_link_order *link_order)
4053 enum bfd_link_order_type type;
4054 asection *section;
4055 bfd_vma addend;
4056 arelent rel;
4057 struct internal_reloc in;
4058 bfd_size_type external_reloc_size;
4059 bfd_byte *rbuf;
4060 bfd_boolean ok;
4061 file_ptr pos;
4063 type = link_order->type;
4064 section = NULL;
4065 addend = link_order->u.reloc.p->addend;
4067 /* We set up an arelent to pass to the backend adjust_reloc_out
4068 routine. */
4069 rel.address = link_order->offset;
4071 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4072 if (rel.howto == 0)
4074 bfd_set_error (bfd_error_bad_value);
4075 return FALSE;
4078 if (type == bfd_section_reloc_link_order)
4080 section = link_order->u.reloc.p->u.section;
4081 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4083 else
4085 struct bfd_link_hash_entry *h;
4087 /* Treat a reloc against a defined symbol as though it were
4088 actually against the section. */
4089 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4090 link_order->u.reloc.p->u.name,
4091 FALSE, FALSE, FALSE);
4092 if (h != NULL
4093 && (h->type == bfd_link_hash_defined
4094 || h->type == bfd_link_hash_defweak))
4096 type = bfd_section_reloc_link_order;
4097 section = h->u.def.section->output_section;
4098 /* It seems that we ought to add the symbol value to the
4099 addend here, but in practice it has already been added
4100 because it was passed to constructor_callback. */
4101 addend += section->vma + h->u.def.section->output_offset;
4103 else
4105 /* We can't set up a reloc against a symbol correctly,
4106 because we have no asymbol structure. Currently no
4107 adjust_reloc_out routine cares. */
4108 rel.sym_ptr_ptr = NULL;
4112 /* All ECOFF relocs are in-place. Put the addend into the object
4113 file. */
4115 BFD_ASSERT (rel.howto->partial_inplace);
4116 if (addend != 0)
4118 bfd_size_type size;
4119 bfd_reloc_status_type rstat;
4120 bfd_byte *buf;
4122 size = bfd_get_reloc_size (rel.howto);
4123 buf = (bfd_byte *) bfd_zmalloc (size);
4124 if (buf == NULL)
4125 return FALSE;
4126 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4127 (bfd_vma) addend, buf);
4128 switch (rstat)
4130 case bfd_reloc_ok:
4131 break;
4132 default:
4133 case bfd_reloc_outofrange:
4134 abort ();
4135 case bfd_reloc_overflow:
4136 if (! ((*info->callbacks->reloc_overflow)
4137 (info, NULL,
4138 (link_order->type == bfd_section_reloc_link_order
4139 ? bfd_section_name (output_bfd, section)
4140 : link_order->u.reloc.p->u.name),
4141 rel.howto->name, addend, NULL,
4142 NULL, (bfd_vma) 0)))
4144 free (buf);
4145 return FALSE;
4147 break;
4149 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
4150 (file_ptr) link_order->offset, size);
4151 free (buf);
4152 if (! ok)
4153 return FALSE;
4156 rel.addend = 0;
4158 /* Move the information into an internal_reloc structure. */
4159 in.r_vaddr = (rel.address
4160 + bfd_get_section_vma (output_bfd, output_section));
4161 in.r_type = rel.howto->type;
4163 if (type == bfd_symbol_reloc_link_order)
4165 struct ecoff_link_hash_entry *h;
4167 h = ((struct ecoff_link_hash_entry *)
4168 bfd_wrapped_link_hash_lookup (output_bfd, info,
4169 link_order->u.reloc.p->u.name,
4170 FALSE, FALSE, TRUE));
4171 if (h != NULL
4172 && h->indx != -1)
4173 in.r_symndx = h->indx;
4174 else
4176 if (! ((*info->callbacks->unattached_reloc)
4177 (info, link_order->u.reloc.p->u.name, NULL,
4178 NULL, (bfd_vma) 0)))
4179 return FALSE;
4180 in.r_symndx = 0;
4182 in.r_extern = 1;
4184 else
4186 const char *name;
4187 unsigned int i;
4188 static struct
4190 const char * name;
4191 long r_symndx;
4193 section_symndx [] =
4195 { _TEXT, RELOC_SECTION_TEXT },
4196 { _RDATA, RELOC_SECTION_RDATA },
4197 { _DATA, RELOC_SECTION_DATA },
4198 { _SDATA, RELOC_SECTION_SDATA },
4199 { _SBSS, RELOC_SECTION_SBSS },
4200 { _BSS, RELOC_SECTION_BSS },
4201 { _INIT, RELOC_SECTION_INIT },
4202 { _LIT8, RELOC_SECTION_LIT8 },
4203 { _LIT4, RELOC_SECTION_LIT4 },
4204 { _XDATA, RELOC_SECTION_XDATA },
4205 { _PDATA, RELOC_SECTION_PDATA },
4206 { _FINI, RELOC_SECTION_FINI },
4207 { _LITA, RELOC_SECTION_LITA },
4208 { "*ABS*", RELOC_SECTION_ABS },
4209 { _RCONST, RELOC_SECTION_RCONST }
4212 name = bfd_get_section_name (output_bfd, section);
4214 for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
4215 if (streq (name, section_symndx[i].name))
4217 in.r_symndx = section_symndx[i].r_symndx;
4218 break;
4221 if (i == ARRAY_SIZE (section_symndx))
4222 abort ();
4224 in.r_extern = 0;
4227 /* Let the BFD backend adjust the reloc. */
4228 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4230 /* Get some memory and swap out the reloc. */
4231 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4232 rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4233 if (rbuf == NULL)
4234 return FALSE;
4236 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
4238 pos = (output_section->rel_filepos
4239 + output_section->reloc_count * external_reloc_size);
4240 ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4241 && (bfd_bwrite ((void *) rbuf, external_reloc_size, output_bfd)
4242 == external_reloc_size));
4244 if (ok)
4245 ++output_section->reloc_count;
4247 free (rbuf);
4249 return ok;
4252 /* Put out information for an external symbol. These come only from
4253 the hash table. */
4255 static bfd_boolean
4256 ecoff_link_write_external (struct bfd_hash_entry *bh, void * data)
4258 struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh;
4259 struct extsym_info *einfo = (struct extsym_info *) data;
4260 bfd *output_bfd = einfo->abfd;
4261 bfd_boolean strip;
4263 if (h->root.type == bfd_link_hash_warning)
4265 h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4266 if (h->root.type == bfd_link_hash_new)
4267 return TRUE;
4270 /* We need to check if this symbol is being stripped. */
4271 if (h->root.type == bfd_link_hash_undefined
4272 || h->root.type == bfd_link_hash_undefweak)
4273 strip = FALSE;
4274 else if (einfo->info->strip == strip_all
4275 || (einfo->info->strip == strip_some
4276 && bfd_hash_lookup (einfo->info->keep_hash,
4277 h->root.root.string,
4278 FALSE, FALSE) == NULL))
4279 strip = TRUE;
4280 else
4281 strip = FALSE;
4283 if (strip || h->written)
4284 return TRUE;
4286 if (h->abfd == NULL)
4288 h->esym.jmptbl = 0;
4289 h->esym.cobol_main = 0;
4290 h->esym.weakext = 0;
4291 h->esym.reserved = 0;
4292 h->esym.ifd = ifdNil;
4293 h->esym.asym.value = 0;
4294 h->esym.asym.st = stGlobal;
4296 if (h->root.type != bfd_link_hash_defined
4297 && h->root.type != bfd_link_hash_defweak)
4298 h->esym.asym.sc = scAbs;
4299 else
4301 asection *output_section;
4302 const char *name;
4303 unsigned int i;
4304 static struct
4306 const char * name;
4307 int sc;
4309 section_storage_classes [] =
4311 { _TEXT, scText },
4312 { _DATA, scData },
4313 { _SDATA, scSData },
4314 { _RDATA, scRData },
4315 { _BSS, scBss },
4316 { _SBSS, scSBss },
4317 { _INIT, scInit },
4318 { _FINI, scFini },
4319 { _PDATA, scPData },
4320 { _XDATA, scXData },
4321 { _RCONST, scRConst }
4324 output_section = h->root.u.def.section->output_section;
4325 name = bfd_section_name (output_section->owner, output_section);
4327 for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++)
4328 if (streq (name, section_storage_classes[i].name))
4330 h->esym.asym.sc = section_storage_classes[i].sc;
4331 break;
4334 if (i == ARRAY_SIZE (section_storage_classes))
4335 h->esym.asym.sc = scAbs;
4338 h->esym.asym.reserved = 0;
4339 h->esym.asym.index = indexNil;
4341 else if (h->esym.ifd != -1)
4343 struct ecoff_debug_info *debug;
4345 /* Adjust the FDR index for the symbol by that used for the
4346 input BFD. */
4347 debug = &ecoff_data (h->abfd)->debug_info;
4348 BFD_ASSERT (h->esym.ifd >= 0
4349 && h->esym.ifd < debug->symbolic_header.ifdMax);
4350 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4353 switch (h->root.type)
4355 default:
4356 case bfd_link_hash_warning:
4357 case bfd_link_hash_new:
4358 abort ();
4359 case bfd_link_hash_undefined:
4360 case bfd_link_hash_undefweak:
4361 if (h->esym.asym.sc != scUndefined
4362 && h->esym.asym.sc != scSUndefined)
4363 h->esym.asym.sc = scUndefined;
4364 break;
4365 case bfd_link_hash_defined:
4366 case bfd_link_hash_defweak:
4367 if (h->esym.asym.sc == scUndefined
4368 || h->esym.asym.sc == scSUndefined)
4369 h->esym.asym.sc = scAbs;
4370 else if (h->esym.asym.sc == scCommon)
4371 h->esym.asym.sc = scBss;
4372 else if (h->esym.asym.sc == scSCommon)
4373 h->esym.asym.sc = scSBss;
4374 h->esym.asym.value = (h->root.u.def.value
4375 + h->root.u.def.section->output_section->vma
4376 + h->root.u.def.section->output_offset);
4377 break;
4378 case bfd_link_hash_common:
4379 if (h->esym.asym.sc != scCommon
4380 && h->esym.asym.sc != scSCommon)
4381 h->esym.asym.sc = scCommon;
4382 h->esym.asym.value = h->root.u.c.size;
4383 break;
4384 case bfd_link_hash_indirect:
4385 /* We ignore these symbols, since the indirected symbol is
4386 already in the hash table. */
4387 return TRUE;
4390 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4391 symbol number. */
4392 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4393 h->written = 1;
4395 return (bfd_ecoff_debug_one_external
4396 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4397 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4398 &h->esym));
4401 /* ECOFF final link routine. This looks through all the input BFDs
4402 and gathers together all the debugging information, and then
4403 processes all the link order information. This may cause it to
4404 close and reopen some input BFDs; I'll see how bad this is. */
4406 bfd_boolean
4407 _bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
4409 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4410 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4411 HDRR *symhdr;
4412 void * handle;
4413 bfd *input_bfd;
4414 asection *o;
4415 struct bfd_link_order *p;
4416 struct extsym_info einfo;
4418 /* We accumulate the debugging information counts in the symbolic
4419 header. */
4420 symhdr = &debug->symbolic_header;
4421 symhdr->vstamp = 0;
4422 symhdr->ilineMax = 0;
4423 symhdr->cbLine = 0;
4424 symhdr->idnMax = 0;
4425 symhdr->ipdMax = 0;
4426 symhdr->isymMax = 0;
4427 symhdr->ioptMax = 0;
4428 symhdr->iauxMax = 0;
4429 symhdr->issMax = 0;
4430 symhdr->issExtMax = 0;
4431 symhdr->ifdMax = 0;
4432 symhdr->crfd = 0;
4433 symhdr->iextMax = 0;
4435 /* We accumulate the debugging information itself in the debug_info
4436 structure. */
4437 debug->line = NULL;
4438 debug->external_dnr = NULL;
4439 debug->external_pdr = NULL;
4440 debug->external_sym = NULL;
4441 debug->external_opt = NULL;
4442 debug->external_aux = NULL;
4443 debug->ss = NULL;
4444 debug->ssext = debug->ssext_end = NULL;
4445 debug->external_fdr = NULL;
4446 debug->external_rfd = NULL;
4447 debug->external_ext = debug->external_ext_end = NULL;
4449 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4450 if (handle == NULL)
4451 return FALSE;
4453 /* Accumulate the debugging symbols from each input BFD. */
4454 for (input_bfd = info->input_bfds;
4455 input_bfd != NULL;
4456 input_bfd = input_bfd->link_next)
4458 bfd_boolean ret;
4460 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4462 /* Arbitrarily set the symbolic header vstamp to the vstamp
4463 of the first object file in the link. */
4464 if (symhdr->vstamp == 0)
4465 symhdr->vstamp
4466 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4467 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4468 handle);
4470 else
4471 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4472 debug, &backend->debug_swap,
4473 input_bfd, info);
4474 if (! ret)
4475 return FALSE;
4477 /* Combine the register masks. */
4478 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4479 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4480 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4481 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4482 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4483 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4486 /* Write out the external symbols. */
4487 einfo.abfd = abfd;
4488 einfo.info = info;
4489 bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo);
4491 if (info->relocatable)
4493 /* We need to make a pass over the link_orders to count up the
4494 number of relocations we will need to output, so that we know
4495 how much space they will take up. */
4496 for (o = abfd->sections; o != NULL; o = o->next)
4498 o->reloc_count = 0;
4499 for (p = o->map_head.link_order;
4500 p != NULL;
4501 p = p->next)
4502 if (p->type == bfd_indirect_link_order)
4503 o->reloc_count += p->u.indirect.section->reloc_count;
4504 else if (p->type == bfd_section_reloc_link_order
4505 || p->type == bfd_symbol_reloc_link_order)
4506 ++o->reloc_count;
4510 /* Compute the reloc and symbol file positions. */
4511 ecoff_compute_reloc_file_positions (abfd);
4513 /* Write out the debugging information. */
4514 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4515 &backend->debug_swap, info,
4516 ecoff_data (abfd)->sym_filepos))
4517 return FALSE;
4519 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4521 if (info->relocatable)
4523 /* Now reset the reloc_count field of the sections in the output
4524 BFD to 0, so that we can use them to keep track of how many
4525 relocs we have output thus far. */
4526 for (o = abfd->sections; o != NULL; o = o->next)
4527 o->reloc_count = 0;
4530 /* Get a value for the GP register. */
4531 if (ecoff_data (abfd)->gp == 0)
4533 struct bfd_link_hash_entry *h;
4535 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
4536 if (h != NULL
4537 && h->type == bfd_link_hash_defined)
4538 ecoff_data (abfd)->gp = (h->u.def.value
4539 + h->u.def.section->output_section->vma
4540 + h->u.def.section->output_offset);
4541 else if (info->relocatable)
4543 bfd_vma lo;
4545 /* Make up a value. */
4546 lo = (bfd_vma) -1;
4547 for (o = abfd->sections; o != NULL; o = o->next)
4549 if (o->vma < lo
4550 && (streq (o->name, _SBSS)
4551 || streq (o->name, _SDATA)
4552 || streq (o->name, _LIT4)
4553 || streq (o->name, _LIT8)
4554 || streq (o->name, _LITA)))
4555 lo = o->vma;
4557 ecoff_data (abfd)->gp = lo + 0x8000;
4559 else
4561 /* If the relocate_section function needs to do a reloc
4562 involving the GP value, it should make a reloc_dangerous
4563 callback to warn that GP is not defined. */
4567 for (o = abfd->sections; o != NULL; o = o->next)
4569 for (p = o->map_head.link_order;
4570 p != NULL;
4571 p = p->next)
4573 if (p->type == bfd_indirect_link_order
4574 && (bfd_get_flavour (p->u.indirect.section->owner)
4575 == bfd_target_ecoff_flavour))
4577 if (! ecoff_indirect_link_order (abfd, info, o, p))
4578 return FALSE;
4580 else if (p->type == bfd_section_reloc_link_order
4581 || p->type == bfd_symbol_reloc_link_order)
4583 if (! ecoff_reloc_link_order (abfd, info, o, p))
4584 return FALSE;
4586 else
4588 if (! _bfd_default_link_order (abfd, info, o, p))
4589 return FALSE;
4594 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4596 ecoff_data (abfd)->linker = TRUE;
4598 return TRUE;