Fix -Wstringop-overflow warning in ecoff_link_hash_newfunc
[binutils-gdb.git] / bfd / ecoff.c
blob5ee7ffaf4898c7c562590ca9fa6ea5d9de56da30
1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support added by Ian Lance Taylor, ian@cygnus.com.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "ecoff-bfd.h"
28 #include "aout/ar.h"
29 #include "aout/stab_gnu.h"
31 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32 some other stuff which we don't want and which conflicts with stuff
33 we do want. */
34 #include "libaout.h"
35 #include "aout/aout64.h"
36 #undef N_ABS
37 #undef exec_hdr
38 #undef obj_sym_filepos
40 #include "coff/internal.h"
41 #include "coff/sym.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "libcoff.h"
45 #include "libecoff.h"
46 #include "libiberty.h"
48 #define streq(a, b) (strcmp ((a), (b)) == 0)
51 /* This stuff is somewhat copied from coffcode.h. */
52 static asection bfd_debug_section =
53 BFD_FAKE_SECTION (bfd_debug_section, NULL, "*DEBUG*", 0, 0);
55 /* Create an ECOFF object. */
57 bool
58 _bfd_ecoff_mkobject (bfd *abfd)
60 size_t amt = sizeof (ecoff_data_type);
62 abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
63 if (abfd->tdata.ecoff_obj_data == NULL)
64 return false;
66 return true;
69 /* This is a hook called by coff_real_object_p to create any backend
70 specific information. */
72 void *
73 _bfd_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr)
75 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
76 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
77 ecoff_data_type *ecoff;
79 if (! _bfd_ecoff_mkobject (abfd))
80 return NULL;
82 ecoff = ecoff_data (abfd);
83 ecoff->gp_size = 8;
84 ecoff->sym_filepos = internal_f->f_symptr;
86 if (internal_a != NULL)
88 int i;
90 ecoff->text_start = internal_a->text_start;
91 ecoff->text_end = internal_a->text_start + internal_a->tsize;
92 ecoff->gp = internal_a->gp_value;
93 ecoff->gprmask = internal_a->gprmask;
94 for (i = 0; i < 4; i++)
95 ecoff->cprmask[i] = internal_a->cprmask[i];
96 ecoff->fprmask = internal_a->fprmask;
97 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
98 abfd->flags |= D_PAGED;
99 else
100 abfd->flags &=~ D_PAGED;
103 /* It turns out that no special action is required by the MIPS or
104 Alpha ECOFF backends. They have different information in the
105 a.out header, but we just copy it all (e.g., gprmask, cprmask and
106 fprmask) and let the swapping routines ensure that only relevant
107 information is written out. */
109 return (void *) ecoff;
112 bool
113 _bfd_ecoff_bfd_free_cached_info (bfd *abfd)
115 struct ecoff_tdata *tdata;
117 if ((bfd_get_format (abfd) == bfd_object
118 || bfd_get_format (abfd) == bfd_core)
119 && (tdata = ecoff_data (abfd)) != NULL)
121 while (tdata->mips_refhi_list != NULL)
123 struct mips_hi *ref = tdata->mips_refhi_list;
124 tdata->mips_refhi_list = ref->next;
125 free (ref);
127 _bfd_ecoff_free_ecoff_debug_info (&tdata->debug_info);
129 return _bfd_generic_bfd_free_cached_info (abfd);
132 /* Initialize a new section. */
134 bool
135 _bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
137 unsigned int i;
138 static struct
140 const char * name;
141 flagword flags;
143 section_flags [] =
145 { _TEXT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
146 { _INIT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
147 { _FINI, SEC_ALLOC | SEC_CODE | SEC_LOAD },
148 { _DATA, SEC_ALLOC | SEC_DATA | SEC_LOAD },
149 { _SDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_SMALL_DATA },
150 { _RDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
151 { _LIT8, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
152 { _LIT4, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
153 { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
154 { _PDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
155 { _BSS, SEC_ALLOC},
156 { _SBSS, SEC_ALLOC | SEC_SMALL_DATA},
157 /* An Irix 4 shared libary. */
158 { _LIB, SEC_COFF_SHARED_LIBRARY}
161 section->alignment_power = 4;
163 for (i = 0; i < ARRAY_SIZE (section_flags); i++)
164 if (streq (section->name, section_flags[i].name))
166 section->flags |= section_flags[i].flags;
167 break;
171 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
172 uncertain about .init on some systems and I don't know how shared
173 libraries work. */
175 return _bfd_generic_new_section_hook (abfd, section);
178 void
179 _bfd_ecoff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
180 asection *section ATTRIBUTE_UNUSED,
181 void *scnhdr ATTRIBUTE_UNUSED)
185 /* Determine the machine architecture and type. This is called from
186 the generic COFF routines. It is the inverse of ecoff_get_magic,
187 below. This could be an ECOFF backend routine, with one version
188 for each target, but there aren't all that many ECOFF targets. */
190 bool
191 _bfd_ecoff_set_arch_mach_hook (bfd *abfd, void * filehdr)
193 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
194 enum bfd_architecture arch;
195 unsigned long mach;
197 switch (internal_f->f_magic)
199 case MIPS_MAGIC_1:
200 case MIPS_MAGIC_LITTLE:
201 case MIPS_MAGIC_BIG:
202 arch = bfd_arch_mips;
203 mach = bfd_mach_mips3000;
204 break;
206 case MIPS_MAGIC_LITTLE2:
207 case MIPS_MAGIC_BIG2:
208 /* MIPS ISA level 2: the r6000. */
209 arch = bfd_arch_mips;
210 mach = bfd_mach_mips6000;
211 break;
213 case MIPS_MAGIC_LITTLE3:
214 case MIPS_MAGIC_BIG3:
215 /* MIPS ISA level 3: the r4000. */
216 arch = bfd_arch_mips;
217 mach = bfd_mach_mips4000;
218 break;
220 case ALPHA_MAGIC:
221 arch = bfd_arch_alpha;
222 mach = 0;
223 break;
225 default:
226 arch = bfd_arch_obscure;
227 mach = 0;
228 break;
231 return bfd_default_set_arch_mach (abfd, arch, mach);
234 bool
235 _bfd_ecoff_no_long_sections (bfd *abfd, int enable)
237 (void) abfd;
238 (void) enable;
239 return false;
242 /* Get the magic number to use based on the architecture and machine.
243 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
245 static int
246 ecoff_get_magic (bfd *abfd)
248 int big, little;
250 switch (bfd_get_arch (abfd))
252 case bfd_arch_mips:
253 switch (bfd_get_mach (abfd))
255 default:
256 case 0:
257 case bfd_mach_mips3000:
258 big = MIPS_MAGIC_BIG;
259 little = MIPS_MAGIC_LITTLE;
260 break;
262 case bfd_mach_mips6000:
263 big = MIPS_MAGIC_BIG2;
264 little = MIPS_MAGIC_LITTLE2;
265 break;
267 case bfd_mach_mips4000:
268 big = MIPS_MAGIC_BIG3;
269 little = MIPS_MAGIC_LITTLE3;
270 break;
273 return bfd_big_endian (abfd) ? big : little;
275 case bfd_arch_alpha:
276 return ALPHA_MAGIC;
278 default:
279 abort ();
280 return 0;
284 /* Get the section s_flags to use for a section. */
286 static long
287 ecoff_sec_to_styp_flags (const char *name, flagword flags)
289 unsigned int i;
290 static struct
292 const char * name;
293 long flags;
295 styp_flags [] =
297 { _TEXT, STYP_TEXT },
298 { _DATA, STYP_DATA },
299 { _SDATA, STYP_SDATA },
300 { _RDATA, STYP_RDATA },
301 { _LITA, STYP_LITA },
302 { _LIT8, STYP_LIT8 },
303 { _LIT4, STYP_LIT4 },
304 { _BSS, STYP_BSS },
305 { _SBSS, STYP_SBSS },
306 { _INIT, STYP_ECOFF_INIT },
307 { _FINI, STYP_ECOFF_FINI },
308 { _PDATA, STYP_PDATA },
309 { _XDATA, STYP_XDATA },
310 { _LIB, STYP_ECOFF_LIB },
311 { _GOT, STYP_GOT },
312 { _HASH, STYP_HASH },
313 { _DYNAMIC, STYP_DYNAMIC },
314 { _LIBLIST, STYP_LIBLIST },
315 { _RELDYN, STYP_RELDYN },
316 { _CONFLIC, STYP_CONFLIC },
317 { _DYNSTR, STYP_DYNSTR },
318 { _DYNSYM, STYP_DYNSYM },
319 { _RCONST, STYP_RCONST }
321 long styp = 0;
323 for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
324 if (streq (name, styp_flags[i].name))
326 styp = styp_flags[i].flags;
327 break;
330 if (styp == 0)
332 if (streq (name, _COMMENT))
334 styp = STYP_COMMENT;
335 flags &=~ SEC_NEVER_LOAD;
337 else if (flags & SEC_CODE)
338 styp = STYP_TEXT;
339 else if (flags & SEC_DATA)
340 styp = STYP_DATA;
341 else if (flags & SEC_READONLY)
342 styp = STYP_RDATA;
343 else if (flags & SEC_LOAD)
344 styp = STYP_REG;
345 else
346 styp = STYP_BSS;
349 if (flags & SEC_NEVER_LOAD)
350 styp |= STYP_NOLOAD;
352 return styp;
355 /* Get the BFD flags to use for a section. */
357 bool
358 _bfd_ecoff_styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
359 void * hdr,
360 const char *name ATTRIBUTE_UNUSED,
361 asection *section ATTRIBUTE_UNUSED,
362 flagword * flags_ptr)
364 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
365 long styp_flags = internal_s->s_flags;
366 flagword sec_flags = 0;
368 if (styp_flags & STYP_NOLOAD)
369 sec_flags |= SEC_NEVER_LOAD;
371 /* For 386 COFF, at least, an unloadable text or data section is
372 actually a shared library section. */
373 if ((styp_flags & STYP_TEXT)
374 || (styp_flags & STYP_ECOFF_INIT)
375 || (styp_flags & STYP_ECOFF_FINI)
376 || (styp_flags & STYP_DYNAMIC)
377 || (styp_flags & STYP_LIBLIST)
378 || (styp_flags & STYP_RELDYN)
379 || styp_flags == STYP_CONFLIC
380 || (styp_flags & STYP_DYNSTR)
381 || (styp_flags & STYP_DYNSYM)
382 || (styp_flags & STYP_HASH))
384 if (sec_flags & SEC_NEVER_LOAD)
385 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
386 else
387 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
389 else if ((styp_flags & STYP_DATA)
390 || (styp_flags & STYP_RDATA)
391 || (styp_flags & STYP_SDATA)
392 || styp_flags == STYP_PDATA
393 || styp_flags == STYP_XDATA
394 || (styp_flags & STYP_GOT)
395 || styp_flags == STYP_RCONST)
397 if (sec_flags & SEC_NEVER_LOAD)
398 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
399 else
400 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
401 if ((styp_flags & STYP_RDATA)
402 || styp_flags == STYP_PDATA
403 || styp_flags == STYP_RCONST)
404 sec_flags |= SEC_READONLY;
405 if (styp_flags & STYP_SDATA)
406 sec_flags |= SEC_SMALL_DATA;
408 else if (styp_flags & STYP_SBSS)
409 sec_flags |= SEC_ALLOC | SEC_SMALL_DATA;
410 else if (styp_flags & STYP_BSS)
411 sec_flags |= SEC_ALLOC;
412 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
413 sec_flags |= SEC_NEVER_LOAD;
414 else if ((styp_flags & STYP_LITA)
415 || (styp_flags & STYP_LIT8)
416 || (styp_flags & STYP_LIT4))
417 sec_flags |= SEC_DATA |SEC_SMALL_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
418 else if (styp_flags & STYP_ECOFF_LIB)
419 sec_flags |= SEC_COFF_SHARED_LIBRARY;
420 else
421 sec_flags |= SEC_ALLOC | SEC_LOAD;
423 * flags_ptr = sec_flags;
424 return true;
427 /* Read in the symbolic header for an ECOFF object file. */
429 static bool
430 ecoff_slurp_symbolic_header (bfd *abfd)
432 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
433 bfd_size_type external_hdr_size;
434 void * raw = NULL;
435 HDRR *internal_symhdr;
437 /* See if we've already read it in. */
438 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
439 backend->debug_swap.sym_magic)
440 return true;
442 /* See whether there is a symbolic header. */
443 if (ecoff_data (abfd)->sym_filepos == 0)
445 abfd->symcount = 0;
446 return true;
449 /* At this point bfd_get_symcount (abfd) holds the number of symbols
450 as read from the file header, but on ECOFF this is always the
451 size of the symbolic information header. It would be cleaner to
452 handle this when we first read the file in coffgen.c. */
453 external_hdr_size = backend->debug_swap.external_hdr_size;
454 if (bfd_get_symcount (abfd) != external_hdr_size)
456 bfd_set_error (bfd_error_bad_value);
457 return false;
460 /* Read the symbolic information header. */
461 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0)
462 goto error_return;
463 raw = _bfd_malloc_and_read (abfd, external_hdr_size, external_hdr_size);
464 if (raw == NULL)
465 goto error_return;
467 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
468 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
470 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
472 bfd_set_error (bfd_error_bad_value);
473 goto error_return;
476 #define FIX(start, count) \
477 if (internal_symhdr->start == 0) \
478 internal_symhdr->count = 0;
480 FIX (cbLineOffset, cbLine);
481 FIX (cbDnOffset, idnMax);
482 FIX (cbPdOffset, ipdMax);
483 FIX (cbSymOffset, isymMax);
484 FIX (cbOptOffset, ioptMax);
485 FIX (cbAuxOffset, iauxMax);
486 FIX (cbSsOffset, issMax);
487 FIX (cbSsExtOffset, issExtMax);
488 FIX (cbFdOffset, ifdMax);
489 FIX (cbRfdOffset, crfd);
490 FIX (cbExtOffset, iextMax);
491 #undef FIX
493 /* Now we can get the correct number of symbols. */
494 abfd->symcount = internal_symhdr->isymMax + internal_symhdr->iextMax;
496 free (raw);
497 return true;
498 error_return:
499 free (raw);
500 return false;
503 /* Read in and swap the important symbolic information for an ECOFF
504 object file. This is called by gdb via the read_debug_info entry
505 point in the backend structure. */
507 bool
508 _bfd_ecoff_slurp_symbolic_info (bfd *abfd,
509 asection *ignore ATTRIBUTE_UNUSED,
510 struct ecoff_debug_info *debug)
512 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
513 HDRR *internal_symhdr;
514 bfd_size_type raw_base;
515 bfd_size_type raw_size;
516 void * raw;
517 bfd_size_type external_fdr_size;
518 char *fraw_src;
519 char *fraw_end;
520 struct fdr *fdr_ptr;
521 bfd_size_type raw_end;
522 bfd_size_type cb_end;
523 file_ptr pos;
524 size_t amt;
526 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
528 /* Check whether we've already gotten it, and whether there's any to
529 get. */
530 if (debug->alloc_syments)
531 return true;
532 if (ecoff_data (abfd)->sym_filepos == 0)
534 abfd->symcount = 0;
535 return true;
538 if (! ecoff_slurp_symbolic_header (abfd))
539 return false;
541 internal_symhdr = &debug->symbolic_header;
543 /* Read all the symbolic information at once. */
544 raw_base = (ecoff_data (abfd)->sym_filepos
545 + backend->debug_swap.external_hdr_size);
547 /* Alpha ecoff makes the determination of raw_size difficult. It has
548 an undocumented debug data section between the symhdr and the first
549 documented section. And the ordering of the sections varies between
550 statically and dynamically linked executables.
551 If bfd supports SEEK_END someday, this code could be simplified. */
552 raw_end = raw_base;
554 #define UPDATE_RAW_END(start, count, size) \
555 do \
556 if (internal_symhdr->count != 0) \
558 if (internal_symhdr->start < raw_base) \
559 goto err; \
560 if (_bfd_mul_overflow ((unsigned long) internal_symhdr->count, \
561 (size), &amt)) \
562 goto err; \
563 cb_end = internal_symhdr->start + amt; \
564 if (cb_end < internal_symhdr->start) \
565 goto err; \
566 if (cb_end > raw_end) \
567 raw_end = cb_end; \
569 while (0)
571 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
572 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
573 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
574 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
575 /* eraxxon@alumni.rice.edu: ioptMax refers to the size of the
576 optimization symtab, not the number of entries. */
577 UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
578 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
579 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
580 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
581 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
582 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
583 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
585 #undef UPDATE_RAW_END
587 raw_size = raw_end - raw_base;
588 if (raw_size == 0)
590 ecoff_data (abfd)->sym_filepos = 0;
591 return true;
593 pos = ecoff_data (abfd)->sym_filepos;
594 pos += backend->debug_swap.external_hdr_size;
595 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
596 return false;
597 raw = _bfd_alloc_and_read (abfd, raw_size, raw_size);
598 if (raw == NULL)
599 return false;
601 debug->alloc_syments = true;
603 /* Get pointers for the numeric offsets in the HDRR structure. */
604 #define FIX(start, count, ptr, type) \
605 if (internal_symhdr->count == 0) \
606 debug->ptr = NULL; \
607 else \
608 debug->ptr = (type) ((char *) raw \
609 + (internal_symhdr->start - raw_base))
611 FIX (cbLineOffset, cbLine, line, unsigned char *);
612 FIX (cbDnOffset, idnMax, external_dnr, void *);
613 FIX (cbPdOffset, ipdMax, external_pdr, void *);
614 FIX (cbSymOffset, isymMax, external_sym, void *);
615 FIX (cbOptOffset, ioptMax, external_opt, void *);
616 FIX (cbAuxOffset, iauxMax, external_aux, union aux_ext *);
617 FIX (cbSsOffset, issMax, ss, char *);
618 FIX (cbSsExtOffset, issExtMax, ssext, char *);
619 FIX (cbFdOffset, ifdMax, external_fdr, void *);
620 FIX (cbRfdOffset, crfd, external_rfd, void *);
621 FIX (cbExtOffset, iextMax, external_ext, void *);
622 #undef FIX
624 /* Ensure string sections are zero terminated. */
625 if (debug->ss)
626 debug->ss[internal_symhdr->issMax - 1] = 0;
627 if (debug->ssext)
628 debug->ssext[internal_symhdr->issExtMax - 1] = 0;
630 /* I don't want to always swap all the data, because it will just
631 waste time and most programs will never look at it. The only
632 time the linker needs most of the debugging information swapped
633 is when linking big-endian and little-endian MIPS object files
634 together, which is not a common occurrence.
636 We need to look at the fdr to deal with a lot of information in
637 the symbols, so we swap them here. */
638 if (_bfd_mul_overflow ((unsigned long) internal_symhdr->ifdMax,
639 sizeof (struct fdr), &amt))
641 err:
642 bfd_set_error (bfd_error_file_too_big);
643 return false;
645 debug->fdr = (FDR *) bfd_alloc (abfd, amt);
646 if (debug->fdr == NULL)
647 return false;
648 external_fdr_size = backend->debug_swap.external_fdr_size;
649 fdr_ptr = debug->fdr;
650 fraw_src = (char *) debug->external_fdr;
651 /* PR 17512: file: 3372-1243-0.004. */
652 if (fraw_src == NULL && internal_symhdr->ifdMax > 0)
653 return false;
654 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
655 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
656 (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
658 return true;
661 /* ECOFF symbol table routines. The ECOFF symbol table is described
662 in gcc/mips-tfile.c. */
664 /* ECOFF uses two common sections. One is the usual one, and the
665 other is for small objects. All the small objects are kept
666 together, and then referenced via the gp pointer, which yields
667 faster assembler code. This is what we use for the small common
668 section. */
669 static asection ecoff_scom_section;
670 static const asymbol ecoff_scom_symbol =
671 GLOBAL_SYM_INIT (SCOMMON, &ecoff_scom_section);
672 static asection ecoff_scom_section =
673 BFD_FAKE_SECTION (ecoff_scom_section, &ecoff_scom_symbol,
674 SCOMMON, 0, SEC_IS_COMMON | SEC_SMALL_DATA);
676 /* Create an empty symbol. */
678 asymbol *
679 _bfd_ecoff_make_empty_symbol (bfd *abfd)
681 ecoff_symbol_type *new_symbol;
682 size_t amt = sizeof (ecoff_symbol_type);
684 new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
685 if (new_symbol == NULL)
686 return NULL;
687 new_symbol->symbol.section = NULL;
688 new_symbol->fdr = NULL;
689 new_symbol->local = false;
690 new_symbol->native = NULL;
691 new_symbol->symbol.the_bfd = abfd;
692 return &new_symbol->symbol;
695 /* Set the BFD flags and section for an ECOFF symbol. */
697 static bool
698 ecoff_set_symbol_info (bfd *abfd,
699 SYMR *ecoff_sym,
700 asymbol *asym,
701 int ext,
702 int weak)
704 asym->the_bfd = abfd;
705 asym->value = ecoff_sym->value;
706 asym->section = &bfd_debug_section;
707 asym->udata.i = 0;
709 /* Most symbol types are just for debugging. */
710 switch (ecoff_sym->st)
712 case stGlobal:
713 case stStatic:
714 case stLabel:
715 case stProc:
716 case stStaticProc:
717 break;
718 case stNil:
719 if (ECOFF_IS_STAB (ecoff_sym))
721 asym->flags = BSF_DEBUGGING;
722 return true;
724 break;
725 default:
726 asym->flags = BSF_DEBUGGING;
727 return true;
730 if (weak)
731 asym->flags = BSF_EXPORT | BSF_WEAK;
732 else if (ext)
733 asym->flags = BSF_EXPORT | BSF_GLOBAL;
734 else
736 asym->flags = BSF_LOCAL;
737 /* Normally, a local stProc symbol will have a corresponding
738 external symbol. We mark the local symbol as a debugging
739 symbol, in order to prevent nm from printing both out.
740 Similarly, we mark stLabel and stabs symbols as debugging
741 symbols. In both cases, we do want to set the value
742 correctly based on the symbol class. */
743 if (ecoff_sym->st == stProc
744 || ecoff_sym->st == stLabel
745 || ECOFF_IS_STAB (ecoff_sym))
746 asym->flags |= BSF_DEBUGGING;
749 if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
750 asym->flags |= BSF_FUNCTION;
752 switch (ecoff_sym->sc)
754 case scNil:
755 /* Used for compiler generated labels. Leave them in the
756 debugging section, and mark them as local. If BSF_DEBUGGING
757 is set, then nm does not display them for some reason. If no
758 flags are set then the linker whines about them. */
759 asym->flags = BSF_LOCAL;
760 break;
761 case scText:
762 asym->section = bfd_make_section_old_way (abfd, _TEXT);
763 asym->value -= asym->section->vma;
764 break;
765 case scData:
766 asym->section = bfd_make_section_old_way (abfd, _DATA);
767 asym->value -= asym->section->vma;
768 break;
769 case scBss:
770 asym->section = bfd_make_section_old_way (abfd, _BSS);
771 asym->value -= asym->section->vma;
772 break;
773 case scRegister:
774 asym->flags = BSF_DEBUGGING;
775 break;
776 case scAbs:
777 asym->section = bfd_abs_section_ptr;
778 break;
779 case scUndefined:
780 asym->section = bfd_und_section_ptr;
781 asym->flags = 0;
782 asym->value = 0;
783 break;
784 case scCdbLocal:
785 case scBits:
786 case scCdbSystem:
787 case scRegImage:
788 case scInfo:
789 case scUserStruct:
790 asym->flags = BSF_DEBUGGING;
791 break;
792 case scSData:
793 asym->section = bfd_make_section_old_way (abfd, ".sdata");
794 asym->value -= asym->section->vma;
795 break;
796 case scSBss:
797 asym->section = bfd_make_section_old_way (abfd, ".sbss");
798 asym->value -= asym->section->vma;
799 break;
800 case scRData:
801 asym->section = bfd_make_section_old_way (abfd, ".rdata");
802 asym->value -= asym->section->vma;
803 break;
804 case scVar:
805 asym->flags = BSF_DEBUGGING;
806 break;
807 case scCommon:
808 if (asym->value > ecoff_data (abfd)->gp_size)
810 asym->section = bfd_com_section_ptr;
811 asym->flags = 0;
812 break;
814 /* Fall through. */
815 case scSCommon:
816 asym->section = &ecoff_scom_section;
817 asym->flags = 0;
818 break;
819 case scVarRegister:
820 case scVariant:
821 asym->flags = BSF_DEBUGGING;
822 break;
823 case scSUndefined:
824 asym->section = bfd_und_section_ptr;
825 asym->flags = 0;
826 asym->value = 0;
827 break;
828 case scInit:
829 asym->section = bfd_make_section_old_way (abfd, ".init");
830 asym->value -= asym->section->vma;
831 break;
832 case scBasedVar:
833 case scXData:
834 case scPData:
835 asym->flags = BSF_DEBUGGING;
836 break;
837 case scFini:
838 asym->section = bfd_make_section_old_way (abfd, ".fini");
839 asym->value -= asym->section->vma;
840 break;
841 case scRConst:
842 asym->section = bfd_make_section_old_way (abfd, ".rconst");
843 asym->value -= asym->section->vma;
844 break;
845 default:
846 break;
849 /* Look for special constructors symbols and make relocation entries
850 in a special construction section. These are produced by the
851 -fgnu-linker argument to g++. */
852 if (ECOFF_IS_STAB (ecoff_sym))
854 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
856 default:
857 break;
859 case N_SETA:
860 case N_SETT:
861 case N_SETD:
862 case N_SETB:
863 /* Mark the symbol as a constructor. */
864 asym->flags |= BSF_CONSTRUCTOR;
865 break;
868 return true;
871 /* Read an ECOFF symbol table. */
873 bool
874 _bfd_ecoff_slurp_symbol_table (bfd *abfd)
876 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
877 const bfd_size_type external_ext_size
878 = backend->debug_swap.external_ext_size;
879 const bfd_size_type external_sym_size
880 = backend->debug_swap.external_sym_size;
881 void (* const swap_ext_in) (bfd *, void *, EXTR *)
882 = backend->debug_swap.swap_ext_in;
883 void (* const swap_sym_in) (bfd *, void *, SYMR *)
884 = backend->debug_swap.swap_sym_in;
885 ecoff_symbol_type *internal;
886 ecoff_symbol_type *internal_ptr;
887 char *eraw_src;
888 char *eraw_end;
889 FDR *fdr_ptr;
890 FDR *fdr_end;
891 size_t amt;
893 /* If we've already read in the symbol table, do nothing. */
894 if (ecoff_data (abfd)->canonical_symbols != NULL)
895 return true;
897 /* Get the symbolic information. */
898 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
899 &ecoff_data (abfd)->debug_info))
900 return false;
901 if (bfd_get_symcount (abfd) == 0)
902 return true;
904 if (_bfd_mul_overflow (bfd_get_symcount (abfd),
905 sizeof (ecoff_symbol_type), &amt))
907 bfd_set_error (bfd_error_file_too_big);
908 return false;
910 internal = (ecoff_symbol_type *) bfd_alloc (abfd, amt);
911 if (internal == NULL)
912 return false;
914 internal_ptr = internal;
915 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
916 eraw_end = (eraw_src
917 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
918 * external_ext_size));
919 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
921 EXTR internal_esym;
923 (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
925 /* PR 17512: file: 3372-1000-0.004. */
926 HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
927 if (internal_esym.asym.iss >= symhdr->issExtMax
928 || internal_esym.asym.iss < 0)
930 bfd_set_error (bfd_error_bad_value);
931 return false;
934 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
935 + internal_esym.asym.iss);
937 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
938 &internal_ptr->symbol, 1,
939 internal_esym.weakext))
940 return false;
942 /* The alpha uses a negative ifd field for section symbols. */
943 /* PR 17512: file: 3372-1983-0.004. */
944 if (internal_esym.ifd >= symhdr->ifdMax
945 || internal_esym.ifd < 0)
946 internal_ptr->fdr = NULL;
947 else
948 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
949 + internal_esym.ifd);
950 internal_ptr->local = false;
951 internal_ptr->native = (void *) eraw_src;
954 /* The local symbols must be accessed via the fdr's, because the
955 string and aux indices are relative to the fdr information. */
956 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
957 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
958 for (; fdr_ptr < fdr_end; fdr_ptr++)
960 char *lraw_src;
961 char *lraw_end;
962 HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
964 if (fdr_ptr->csym == 0)
965 continue;
966 if (fdr_ptr->isymBase < 0
967 || fdr_ptr->isymBase > symhdr->isymMax
968 || fdr_ptr->csym < 0
969 || fdr_ptr->csym > symhdr->isymMax - fdr_ptr->isymBase
970 || fdr_ptr->csym > ((long) bfd_get_symcount (abfd)
971 - (internal_ptr - internal))
972 || fdr_ptr->issBase < 0
973 || fdr_ptr->issBase > symhdr->issMax)
975 bfd_set_error (bfd_error_bad_value);
976 return false;
978 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
979 + fdr_ptr->isymBase * external_sym_size);
980 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
981 for (;
982 lraw_src < lraw_end;
983 lraw_src += external_sym_size, internal_ptr++)
985 SYMR internal_sym;
987 (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
989 if (internal_sym.iss >= symhdr->issMax - fdr_ptr->issBase
990 || internal_sym.iss < 0)
992 bfd_set_error (bfd_error_bad_value);
993 return false;
995 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
996 + fdr_ptr->issBase
997 + internal_sym.iss);
998 if (!ecoff_set_symbol_info (abfd, &internal_sym,
999 &internal_ptr->symbol, 0, 0))
1000 return false;
1001 internal_ptr->fdr = fdr_ptr;
1002 internal_ptr->local = true;
1003 internal_ptr->native = (void *) lraw_src;
1007 /* PR 17512: file: 3372-3080-0.004.
1008 A discrepancy between ecoff_data (abfd)->debug_info.symbolic_header.isymMax
1009 and ecoff_data (abfd)->debug_info.symbolic_header.ifdMax can mean that
1010 we have fewer symbols than we were expecting. Allow for this by updating
1011 the symbol count and warning the user. */
1012 if (internal_ptr - internal < (ptrdiff_t) bfd_get_symcount (abfd))
1014 abfd->symcount = internal_ptr - internal;
1015 _bfd_error_handler
1016 /* xgettext:c-format */
1017 (_("%pB: warning: isymMax (%ld) is greater than ifdMax (%ld)"),
1018 abfd, ecoff_data (abfd)->debug_info.symbolic_header.isymMax,
1019 ecoff_data (abfd)->debug_info.symbolic_header.ifdMax);
1022 ecoff_data (abfd)->canonical_symbols = internal;
1024 return true;
1027 /* Return the amount of space needed for the canonical symbols. */
1029 long
1030 _bfd_ecoff_get_symtab_upper_bound (bfd *abfd)
1032 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
1033 &ecoff_data (abfd)->debug_info))
1034 return -1;
1036 if (bfd_get_symcount (abfd) == 0)
1037 return 0;
1039 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1042 /* Get the canonical symbols. */
1044 long
1045 _bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
1047 unsigned int counter = 0;
1048 ecoff_symbol_type *symbase;
1049 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1051 if (! _bfd_ecoff_slurp_symbol_table (abfd))
1052 return -1;
1053 if (bfd_get_symcount (abfd) == 0)
1054 return 0;
1056 symbase = ecoff_data (abfd)->canonical_symbols;
1057 while (counter < bfd_get_symcount (abfd))
1059 *(location++) = symbase++;
1060 counter++;
1062 *location++ = NULL;
1063 return bfd_get_symcount (abfd);
1066 /* Turn ECOFF type information into a printable string.
1067 ecoff_emit_aggregate and ecoff_type_to_string are from
1068 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1070 /* Write aggregate information to a string. */
1072 static void
1073 ecoff_emit_aggregate (bfd *abfd,
1074 FDR *fdr,
1075 char *string,
1076 RNDXR *rndx,
1077 long isym,
1078 const char *which)
1080 const struct ecoff_debug_swap * const debug_swap =
1081 &ecoff_backend (abfd)->debug_swap;
1082 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1083 unsigned int ifd = rndx->rfd;
1084 unsigned int indx = rndx->index;
1085 const char *name;
1087 if (ifd == 0xfff)
1088 ifd = isym;
1090 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1091 struct return type of a procedure compiled without -g. */
1092 if (ifd == 0xffffffff
1093 || (rndx->rfd == 0xfff && indx == 0))
1094 name = "<undefined>";
1095 else if (indx == indexNil)
1096 name = "<no name>";
1097 else
1099 SYMR sym;
1101 if (debug_info->external_rfd == NULL)
1102 fdr = debug_info->fdr + ifd;
1103 else
1105 RFDT rfd;
1107 (*debug_swap->swap_rfd_in) (abfd,
1108 ((char *) debug_info->external_rfd
1109 + ((fdr->rfdBase + ifd)
1110 * debug_swap->external_rfd_size)),
1111 &rfd);
1112 fdr = debug_info->fdr + rfd;
1115 indx += fdr->isymBase;
1117 (*debug_swap->swap_sym_in) (abfd,
1118 ((char *) debug_info->external_sym
1119 + indx * debug_swap->external_sym_size),
1120 &sym);
1122 name = debug_info->ss + fdr->issBase + sym.iss;
1125 sprintf (string,
1126 "%s %s { ifd = %u, index = %lu }",
1127 which, name, ifd,
1128 ((unsigned long) indx
1129 + debug_info->symbolic_header.iextMax));
1132 /* Convert the type information to string format. */
1134 static char *
1135 ecoff_type_to_string (bfd *abfd, FDR *fdr, unsigned int indx, char *buff)
1137 union aux_ext *aux_ptr;
1138 int bigendian;
1139 AUXU u;
1140 struct qual
1142 unsigned int type;
1143 int low_bound;
1144 int high_bound;
1145 int stride;
1146 } qualifiers[7];
1147 unsigned int basic_type;
1148 int i;
1149 char buffer1[1024];
1150 char *p1 = buffer1;
1151 char *p2 = buff;
1152 RNDXR rndx;
1154 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1155 bigendian = fdr->fBigendian;
1157 for (i = 0; i < 7; i++)
1159 qualifiers[i].low_bound = 0;
1160 qualifiers[i].high_bound = 0;
1161 qualifiers[i].stride = 0;
1164 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1165 return "-1 (no type)";
1166 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1168 basic_type = u.ti.bt;
1169 qualifiers[0].type = u.ti.tq0;
1170 qualifiers[1].type = u.ti.tq1;
1171 qualifiers[2].type = u.ti.tq2;
1172 qualifiers[3].type = u.ti.tq3;
1173 qualifiers[4].type = u.ti.tq4;
1174 qualifiers[5].type = u.ti.tq5;
1175 qualifiers[6].type = tqNil;
1177 /* Go get the basic type. */
1178 switch (basic_type)
1180 case btNil: /* Undefined. */
1181 strcpy (p1, "nil");
1182 break;
1184 case btAdr: /* Address - integer same size as pointer. */
1185 strcpy (p1, "address");
1186 break;
1188 case btChar: /* Character. */
1189 strcpy (p1, "char");
1190 break;
1192 case btUChar: /* Unsigned character. */
1193 strcpy (p1, "unsigned char");
1194 break;
1196 case btShort: /* Short. */
1197 strcpy (p1, "short");
1198 break;
1200 case btUShort: /* Unsigned short. */
1201 strcpy (p1, "unsigned short");
1202 break;
1204 case btInt: /* Int. */
1205 strcpy (p1, "int");
1206 break;
1208 case btUInt: /* Unsigned int. */
1209 strcpy (p1, "unsigned int");
1210 break;
1212 case btLong: /* Long. */
1213 strcpy (p1, "long");
1214 break;
1216 case btULong: /* Unsigned long. */
1217 strcpy (p1, "unsigned long");
1218 break;
1220 case btFloat: /* Float (real). */
1221 strcpy (p1, "float");
1222 break;
1224 case btDouble: /* Double (real). */
1225 strcpy (p1, "double");
1226 break;
1228 /* Structures add 1-2 aux words:
1229 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1230 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1232 case btStruct: /* Structure (Record). */
1233 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1234 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1235 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1236 "struct");
1237 indx++; /* Skip aux words. */
1238 break;
1240 /* Unions add 1-2 aux words:
1241 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1242 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1244 case btUnion: /* Union. */
1245 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1246 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1247 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1248 "union");
1249 indx++; /* Skip aux words. */
1250 break;
1252 /* Enumerations add 1-2 aux words:
1253 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1254 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1256 case btEnum: /* Enumeration. */
1257 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1258 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1259 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1260 "enum");
1261 indx++; /* Skip aux words. */
1262 break;
1264 case btTypedef: /* Defined via a typedef, isymRef points. */
1265 strcpy (p1, "typedef");
1266 break;
1268 case btRange: /* Subrange of int. */
1269 strcpy (p1, "subrange");
1270 break;
1272 case btSet: /* Pascal sets. */
1273 strcpy (p1, "set");
1274 break;
1276 case btComplex: /* Fortran complex. */
1277 strcpy (p1, "complex");
1278 break;
1280 case btDComplex: /* Fortran double complex. */
1281 strcpy (p1, "double complex");
1282 break;
1284 case btIndirect: /* Forward or unnamed typedef. */
1285 strcpy (p1, "forward/unamed typedef");
1286 break;
1288 case btFixedDec: /* Fixed Decimal. */
1289 strcpy (p1, "fixed decimal");
1290 break;
1292 case btFloatDec: /* Float Decimal. */
1293 strcpy (p1, "float decimal");
1294 break;
1296 case btString: /* Varying Length Character String. */
1297 strcpy (p1, "string");
1298 break;
1300 case btBit: /* Aligned Bit String. */
1301 strcpy (p1, "bit");
1302 break;
1304 case btPicture: /* Picture. */
1305 strcpy (p1, "picture");
1306 break;
1308 case btVoid: /* Void. */
1309 strcpy (p1, "void");
1310 break;
1312 default:
1313 sprintf (p1, _("unknown basic type %d"), (int) basic_type);
1314 break;
1317 p1 += strlen (p1);
1319 /* If this is a bitfield, get the bitsize. */
1320 if (u.ti.fBitfield)
1322 int bitsize;
1324 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1325 sprintf (p1, " : %d", bitsize);
1328 /* Deal with any qualifiers. */
1329 if (qualifiers[0].type != tqNil)
1331 /* Snarf up any array bounds in the correct order. Arrays
1332 store 5 successive words in the aux. table:
1333 word 0 RNDXR to type of the bounds (ie, int)
1334 word 1 Current file descriptor index
1335 word 2 low bound
1336 word 3 high bound (or -1 if [])
1337 word 4 stride size in bits. */
1338 for (i = 0; i < 7; i++)
1340 if (qualifiers[i].type == tqArray)
1342 qualifiers[i].low_bound =
1343 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1344 qualifiers[i].high_bound =
1345 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1346 qualifiers[i].stride =
1347 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1348 indx += 5;
1352 /* Now print out the qualifiers. */
1353 for (i = 0; i < 6; i++)
1355 switch (qualifiers[i].type)
1357 case tqNil:
1358 case tqMax:
1359 break;
1361 case tqPtr:
1362 strcpy (p2, "ptr to ");
1363 p2 += sizeof ("ptr to ")-1;
1364 break;
1366 case tqVol:
1367 strcpy (p2, "volatile ");
1368 p2 += sizeof ("volatile ")-1;
1369 break;
1371 case tqFar:
1372 strcpy (p2, "far ");
1373 p2 += sizeof ("far ")-1;
1374 break;
1376 case tqProc:
1377 strcpy (p2, "func. ret. ");
1378 p2 += sizeof ("func. ret. ");
1379 break;
1381 case tqArray:
1383 int first_array = i;
1384 int j;
1386 /* Print array bounds reversed (ie, in the order the C
1387 programmer writes them). C is such a fun language.... */
1388 while (i < 5 && qualifiers[i+1].type == tqArray)
1389 i++;
1391 for (j = i; j >= first_array; j--)
1393 strcpy (p2, "array [");
1394 p2 += sizeof ("array [")-1;
1395 if (qualifiers[j].low_bound != 0)
1396 sprintf (p2,
1397 "%ld:%ld {%ld bits}",
1398 (long) qualifiers[j].low_bound,
1399 (long) qualifiers[j].high_bound,
1400 (long) qualifiers[j].stride);
1402 else if (qualifiers[j].high_bound != -1)
1403 sprintf (p2,
1404 "%ld {%ld bits}",
1405 (long) (qualifiers[j].high_bound + 1),
1406 (long) (qualifiers[j].stride));
1408 else
1409 sprintf (p2, " {%ld bits}", (long) qualifiers[j].stride);
1411 p2 += strlen (p2);
1412 strcpy (p2, "] of ");
1413 p2 += sizeof ("] of ")-1;
1416 break;
1421 strcpy (p2, buffer1);
1422 return buff;
1425 /* Return information about ECOFF symbol SYMBOL in RET. */
1427 void
1428 _bfd_ecoff_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1429 asymbol *symbol,
1430 symbol_info *ret)
1432 bfd_symbol_info (symbol, ret);
1435 /* Return whether this is a local label. */
1437 bool
1438 _bfd_ecoff_bfd_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
1439 const char *name)
1441 return name[0] == '$';
1444 /* Print information about an ECOFF symbol. */
1446 void
1447 _bfd_ecoff_print_symbol (bfd *abfd,
1448 void * filep,
1449 asymbol *symbol,
1450 bfd_print_symbol_type how)
1452 const struct ecoff_debug_swap * const debug_swap
1453 = &ecoff_backend (abfd)->debug_swap;
1454 FILE *file = (FILE *)filep;
1456 switch (how)
1458 case bfd_print_symbol_name:
1459 fprintf (file, "%s", symbol->name);
1460 break;
1461 case bfd_print_symbol_more:
1462 if (ecoffsymbol (symbol)->local)
1464 SYMR ecoff_sym;
1466 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1467 &ecoff_sym);
1468 fprintf (file, "ecoff local ");
1469 bfd_fprintf_vma (abfd, file, ecoff_sym.value);
1470 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1471 (unsigned) ecoff_sym.sc);
1473 else
1475 EXTR ecoff_ext;
1477 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1478 &ecoff_ext);
1479 fprintf (file, "ecoff extern ");
1480 bfd_fprintf_vma (abfd, file, ecoff_ext.asym.value);
1481 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1482 (unsigned) ecoff_ext.asym.sc);
1484 break;
1485 case bfd_print_symbol_all:
1486 /* Print out the symbols in a reasonable way. */
1488 char type;
1489 int pos;
1490 EXTR ecoff_ext;
1491 char jmptbl;
1492 char cobol_main;
1493 char weakext;
1495 if (ecoffsymbol (symbol)->local)
1497 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1498 &ecoff_ext.asym);
1499 type = 'l';
1500 pos = ((((char *) ecoffsymbol (symbol)->native
1501 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1502 / debug_swap->external_sym_size)
1503 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1504 jmptbl = ' ';
1505 cobol_main = ' ';
1506 weakext = ' ';
1508 else
1510 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1511 &ecoff_ext);
1512 type = 'e';
1513 pos = (((char *) ecoffsymbol (symbol)->native
1514 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1515 / debug_swap->external_ext_size);
1516 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1517 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1518 weakext = ecoff_ext.weakext ? 'w' : ' ';
1521 fprintf (file, "[%3d] %c ",
1522 pos, type);
1523 bfd_fprintf_vma (abfd, file, ecoff_ext.asym.value);
1524 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1525 (unsigned) ecoff_ext.asym.st,
1526 (unsigned) ecoff_ext.asym.sc,
1527 (unsigned) ecoff_ext.asym.index,
1528 jmptbl, cobol_main, weakext,
1529 symbol->name);
1531 if (ecoffsymbol (symbol)->fdr != NULL
1532 && ecoff_ext.asym.index != indexNil)
1534 FDR *fdr;
1535 unsigned int indx;
1536 int bigendian;
1537 bfd_size_type sym_base;
1538 union aux_ext *aux_base;
1540 fdr = ecoffsymbol (symbol)->fdr;
1541 indx = ecoff_ext.asym.index;
1543 /* sym_base is used to map the fdr relative indices which
1544 appear in the file to the position number which we are
1545 using. */
1546 sym_base = fdr->isymBase;
1547 if (ecoffsymbol (symbol)->local)
1548 sym_base +=
1549 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1551 /* aux_base is the start of the aux entries for this file;
1552 asym.index is an offset from this. */
1553 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1554 + fdr->iauxBase);
1556 /* The aux entries are stored in host byte order; the
1557 order is indicated by a bit in the fdr. */
1558 bigendian = fdr->fBigendian;
1560 /* This switch is basically from gcc/mips-tdump.c. */
1561 switch (ecoff_ext.asym.st)
1563 case stNil:
1564 case stLabel:
1565 break;
1567 case stFile:
1568 case stBlock:
1569 fprintf (file, _("\n End+1 symbol: %ld"),
1570 (long) (indx + sym_base));
1571 break;
1573 case stEnd:
1574 if (ecoff_ext.asym.sc == scText
1575 || ecoff_ext.asym.sc == scInfo)
1576 fprintf (file, _("\n First symbol: %ld"),
1577 (long) (indx + sym_base));
1578 else
1579 fprintf (file, _("\n First symbol: %ld"),
1580 ((long)
1581 (AUX_GET_ISYM (bigendian,
1582 &aux_base[ecoff_ext.asym.index])
1583 + sym_base)));
1584 break;
1586 case stProc:
1587 case stStaticProc:
1588 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1590 else if (ecoffsymbol (symbol)->local)
1592 char buff[1024];
1593 /* xgettext:c-format */
1594 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
1595 ((long)
1596 (AUX_GET_ISYM (bigendian,
1597 &aux_base[ecoff_ext.asym.index])
1598 + sym_base)),
1599 ecoff_type_to_string (abfd, fdr, indx + 1, buff));
1601 else
1602 fprintf (file, _("\n Local symbol: %ld"),
1603 ((long) indx
1604 + (long) sym_base
1605 + (ecoff_data (abfd)
1606 ->debug_info.symbolic_header.iextMax)));
1607 break;
1609 case stStruct:
1610 fprintf (file, _("\n struct; End+1 symbol: %ld"),
1611 (long) (indx + sym_base));
1612 break;
1614 case stUnion:
1615 fprintf (file, _("\n union; End+1 symbol: %ld"),
1616 (long) (indx + sym_base));
1617 break;
1619 case stEnum:
1620 fprintf (file, _("\n enum; End+1 symbol: %ld"),
1621 (long) (indx + sym_base));
1622 break;
1624 default:
1625 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1627 char buff[1024];
1628 fprintf (file, _("\n Type: %s"),
1629 ecoff_type_to_string (abfd, fdr, indx, buff));
1631 break;
1635 break;
1639 /* Read in the relocs for a section. */
1641 static bool
1642 ecoff_slurp_reloc_table (bfd *abfd,
1643 asection *section,
1644 asymbol **symbols)
1646 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1647 arelent *internal_relocs;
1648 bfd_size_type external_reloc_size;
1649 bfd_size_type amt;
1650 bfd_byte *external_relocs;
1651 arelent *rptr;
1652 unsigned int i;
1654 if (section->relocation != NULL
1655 || section->reloc_count == 0
1656 || (section->flags & SEC_CONSTRUCTOR) != 0)
1657 return true;
1659 if (! _bfd_ecoff_slurp_symbol_table (abfd))
1660 return false;
1662 external_reloc_size = backend->external_reloc_size;
1663 amt = external_reloc_size * section->reloc_count;
1664 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1665 return false;
1666 external_relocs = _bfd_malloc_and_read (abfd, amt, amt);
1667 if (external_relocs == NULL)
1668 return false;
1670 amt = section->reloc_count;
1671 amt *= sizeof (arelent);
1672 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1673 if (internal_relocs == NULL)
1675 free (external_relocs);
1676 return false;
1679 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1681 struct internal_reloc intern;
1683 (*backend->swap_reloc_in) (abfd,
1684 external_relocs + i * external_reloc_size,
1685 &intern);
1686 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1687 rptr->addend = 0;
1689 if (intern.r_extern)
1691 /* r_symndx is an index into the external symbols. */
1692 if (symbols != NULL
1693 && intern.r_symndx >= 0
1694 && (intern.r_symndx
1695 < (ecoff_data (abfd)->debug_info.symbolic_header.iextMax)))
1696 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1698 else
1700 const char *sec_name;
1701 asection *sec;
1703 /* r_symndx is a section key. */
1704 switch (intern.r_symndx)
1706 case RELOC_SECTION_TEXT: sec_name = _TEXT; break;
1707 case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
1708 case RELOC_SECTION_DATA: sec_name = _DATA; break;
1709 case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
1710 case RELOC_SECTION_SBSS: sec_name = _SBSS; break;
1711 case RELOC_SECTION_BSS: sec_name = _BSS; break;
1712 case RELOC_SECTION_INIT: sec_name = _INIT; break;
1713 case RELOC_SECTION_LIT8: sec_name = _LIT8; break;
1714 case RELOC_SECTION_LIT4: sec_name = _LIT4; break;
1715 case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
1716 case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
1717 case RELOC_SECTION_FINI: sec_name = _FINI; break;
1718 case RELOC_SECTION_LITA: sec_name = _LITA; break;
1719 case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
1720 default:
1721 sec_name = NULL;
1722 break;
1725 if (sec_name != NULL)
1727 sec = bfd_get_section_by_name (abfd, sec_name);
1728 if (sec != NULL)
1730 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1731 rptr->addend = - bfd_section_vma (sec);
1736 rptr->address = intern.r_vaddr - bfd_section_vma (section);
1738 /* Let the backend select the howto field and do any other
1739 required processing. */
1740 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1743 free (external_relocs);
1745 section->relocation = internal_relocs;
1747 return true;
1750 /* Get a canonical list of relocs. */
1752 long
1753 _bfd_ecoff_canonicalize_reloc (bfd *abfd,
1754 asection *section,
1755 arelent **relptr,
1756 asymbol **symbols)
1758 unsigned int count;
1760 if (section->flags & SEC_CONSTRUCTOR)
1762 arelent_chain *chain;
1764 /* This section has relocs made up by us, not the file, so take
1765 them out of their chain and place them into the data area
1766 provided. */
1767 for (count = 0, chain = section->constructor_chain;
1768 count < section->reloc_count;
1769 count++, chain = chain->next)
1770 *relptr++ = &chain->relent;
1772 else
1774 arelent *tblptr;
1776 if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1777 return -1;
1779 tblptr = section->relocation;
1781 for (count = 0; count < section->reloc_count; count++)
1782 *relptr++ = tblptr++;
1785 *relptr = NULL;
1787 return section->reloc_count;
1790 /* Provided a BFD, a section and an offset into the section, calculate
1791 and return the name of the source file and the line nearest to the
1792 wanted location. */
1794 bool
1795 _bfd_ecoff_find_nearest_line (bfd *abfd,
1796 asymbol **symbols ATTRIBUTE_UNUSED,
1797 asection *section,
1798 bfd_vma offset,
1799 const char **filename_ptr,
1800 const char **functionname_ptr,
1801 unsigned int *retline_ptr,
1802 unsigned int *discriminator_ptr)
1804 const struct ecoff_debug_swap * const debug_swap
1805 = &ecoff_backend (abfd)->debug_swap;
1806 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1807 struct ecoff_find_line *line_info;
1809 /* Make sure we have the FDR's. */
1810 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
1811 || bfd_get_symcount (abfd) == 0)
1812 return false;
1814 if (ecoff_data (abfd)->find_line_info == NULL)
1816 size_t amt = sizeof (struct ecoff_find_line);
1818 ecoff_data (abfd)->find_line_info =
1819 (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1820 if (ecoff_data (abfd)->find_line_info == NULL)
1821 return false;
1824 if (discriminator_ptr)
1825 *discriminator_ptr = 0;
1826 line_info = ecoff_data (abfd)->find_line_info;
1827 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1828 debug_swap, line_info, filename_ptr,
1829 functionname_ptr, retline_ptr);
1832 /* Copy private BFD data. This is called by objcopy and strip. We
1833 use it to copy the ECOFF debugging information from one BFD to the
1834 other. It would be theoretically possible to represent the ECOFF
1835 debugging information in the symbol table. However, it would be a
1836 lot of work, and there would be little gain (gas, gdb, and ld
1837 already access the ECOFF debugging information via the
1838 ecoff_debug_info structure, and that structure would have to be
1839 retained in order to support ECOFF debugging in MIPS ELF).
1841 The debugging information for the ECOFF external symbols comes from
1842 the symbol table, so this function only handles the other debugging
1843 information. */
1845 bool
1846 _bfd_ecoff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1848 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1849 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1850 int i;
1851 asymbol **sym_ptr_ptr;
1852 size_t c;
1853 bool local;
1855 /* We only want to copy information over if both BFD's use ECOFF
1856 format. */
1857 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1858 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1859 return true;
1861 /* Copy the GP value and the register masks. */
1862 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1863 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1864 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1865 for (i = 0; i < 3; i++)
1866 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1868 /* Copy the version stamp. */
1869 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1871 /* If there are no symbols, don't copy any debugging information. */
1872 c = bfd_get_symcount (obfd);
1873 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1874 if (c == 0 || sym_ptr_ptr == NULL)
1875 return true;
1877 /* See if there are any local symbols. */
1878 local = false;
1879 for (; c > 0; c--, sym_ptr_ptr++)
1881 if (ecoffsymbol (*sym_ptr_ptr)->local)
1883 local = true;
1884 break;
1888 if (local)
1890 /* There are some local symbols. We just bring over all the
1891 debugging information. FIXME: This is not quite the right
1892 thing to do. If the user has asked us to discard all
1893 debugging information, then we are probably going to wind up
1894 keeping it because there will probably be some local symbol
1895 which objcopy did not discard. We should actually break
1896 apart the debugging information and only keep that which
1897 applies to the symbols we want to keep. */
1898 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1899 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1900 oinfo->line = iinfo->line;
1902 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1903 oinfo->external_dnr = iinfo->external_dnr;
1905 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1906 oinfo->external_pdr = iinfo->external_pdr;
1908 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1909 oinfo->external_sym = iinfo->external_sym;
1911 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1912 oinfo->external_opt = iinfo->external_opt;
1914 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1915 oinfo->external_aux = iinfo->external_aux;
1917 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1918 oinfo->ss = iinfo->ss;
1920 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1921 oinfo->external_fdr = iinfo->external_fdr;
1923 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1924 oinfo->external_rfd = iinfo->external_rfd;
1926 /* Flag that oinfo entries should not be freed. */
1927 oinfo->alloc_syments = true;
1929 else
1931 /* We are discarding all the local symbol information. Look
1932 through the external symbols and remove all references to FDR
1933 or aux information. */
1934 c = bfd_get_symcount (obfd);
1935 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1936 for (; c > 0; c--, sym_ptr_ptr++)
1938 EXTR esym;
1940 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1941 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1942 esym.ifd = ifdNil;
1943 esym.asym.index = indexNil;
1944 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1945 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1949 return true;
1952 /* Set the architecture. The supported architecture is stored in the
1953 backend pointer. We always set the architecture anyhow, since many
1954 callers ignore the return value. */
1956 bool
1957 _bfd_ecoff_set_arch_mach (bfd *abfd,
1958 enum bfd_architecture arch,
1959 unsigned long machine)
1961 bfd_default_set_arch_mach (abfd, arch, machine);
1962 return arch == ecoff_backend (abfd)->arch;
1965 /* Get the size of the section headers. */
1968 _bfd_ecoff_sizeof_headers (bfd *abfd,
1969 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1971 asection *current;
1972 int c;
1973 int ret;
1975 c = 0;
1976 for (current = abfd->sections;
1977 current != NULL;
1978 current = current->next)
1979 ++c;
1981 ret = (bfd_coff_filhsz (abfd)
1982 + bfd_coff_aoutsz (abfd)
1983 + c * bfd_coff_scnhsz (abfd));
1984 return (int) BFD_ALIGN (ret, 16);
1987 /* Get the contents of a section. */
1989 bool
1990 _bfd_ecoff_get_section_contents (bfd *abfd,
1991 asection *section,
1992 void * location,
1993 file_ptr offset,
1994 bfd_size_type count)
1996 return _bfd_generic_get_section_contents (abfd, section, location,
1997 offset, count);
2000 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2001 called via qsort. */
2003 static int
2004 ecoff_sort_hdrs (const void * arg1, const void * arg2)
2006 const asection *hdr1 = *(const asection **) arg1;
2007 const asection *hdr2 = *(const asection **) arg2;
2009 if ((hdr1->flags & SEC_ALLOC) != 0)
2011 if ((hdr2->flags & SEC_ALLOC) == 0)
2012 return -1;
2014 else
2016 if ((hdr2->flags & SEC_ALLOC) != 0)
2017 return 1;
2019 if (hdr1->vma < hdr2->vma)
2020 return -1;
2021 else if (hdr1->vma > hdr2->vma)
2022 return 1;
2023 else
2024 return 0;
2027 /* Calculate the file position for each section, and set
2028 reloc_filepos. */
2030 static bool
2031 ecoff_compute_section_file_positions (bfd *abfd)
2033 file_ptr sofar, file_sofar;
2034 asection **sorted_hdrs;
2035 asection *current;
2036 unsigned int i;
2037 file_ptr old_sofar;
2038 bool rdata_in_text;
2039 bool first_data, first_nonalloc;
2040 const bfd_vma round = ecoff_backend (abfd)->round;
2041 bfd_size_type amt;
2043 sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
2044 file_sofar = sofar;
2046 /* Sort the sections by VMA. */
2047 amt = abfd->section_count;
2048 amt *= sizeof (asection *);
2049 sorted_hdrs = (asection **) bfd_malloc (amt);
2050 if (sorted_hdrs == NULL)
2051 return false;
2052 for (current = abfd->sections, i = 0;
2053 current != NULL;
2054 current = current->next, i++)
2055 sorted_hdrs[i] = current;
2056 BFD_ASSERT (i == abfd->section_count);
2058 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2059 ecoff_sort_hdrs);
2061 /* Some versions of the OSF linker put the .rdata section in the
2062 text segment, and some do not. */
2063 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2064 if (rdata_in_text)
2066 for (i = 0; i < abfd->section_count; i++)
2068 current = sorted_hdrs[i];
2069 if (streq (current->name, _RDATA))
2070 break;
2071 if ((current->flags & SEC_CODE) == 0
2072 && ! streq (current->name, _PDATA)
2073 && ! streq (current->name, _RCONST))
2075 rdata_in_text = false;
2076 break;
2080 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2082 first_data = true;
2083 first_nonalloc = true;
2084 for (i = 0; i < abfd->section_count; i++)
2086 unsigned int alignment_power;
2088 current = sorted_hdrs[i];
2090 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2091 supposed to indicate the number of .pdata entries that are
2092 really in the section. Each entry is 8 bytes. We store this
2093 away in line_filepos before increasing the section size. */
2094 if (streq (current->name, _PDATA))
2095 current->line_filepos = current->size / 8;
2097 alignment_power = current->alignment_power;
2099 /* On Ultrix, the data sections in an executable file must be
2100 aligned to a page boundary within the file. This does not
2101 affect the section size, though. FIXME: Does this work for
2102 other platforms? It requires some modification for the
2103 Alpha, because .rdata on the Alpha goes with the text, not
2104 the data. */
2105 if ((abfd->flags & EXEC_P) != 0
2106 && (abfd->flags & D_PAGED) != 0
2107 && ! first_data
2108 && (current->flags & SEC_CODE) == 0
2109 && (! rdata_in_text
2110 || ! streq (current->name, _RDATA))
2111 && ! streq (current->name, _PDATA)
2112 && ! streq (current->name, _RCONST))
2114 sofar = (sofar + round - 1) &~ (round - 1);
2115 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2116 first_data = false;
2118 else if (streq (current->name, _LIB))
2120 /* On Irix 4, the location of contents of the .lib section
2121 from a shared library section is also rounded up to a
2122 page boundary. */
2124 sofar = (sofar + round - 1) &~ (round - 1);
2125 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2127 else if (first_nonalloc
2128 && (current->flags & SEC_ALLOC) == 0
2129 && (abfd->flags & D_PAGED) != 0)
2131 /* Skip up to the next page for an unallocated section, such
2132 as the .comment section on the Alpha. This leaves room
2133 for the .bss section. */
2134 first_nonalloc = false;
2135 sofar = (sofar + round - 1) &~ (round - 1);
2136 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2139 /* Align the sections in the file to the same boundary on
2140 which they are aligned in virtual memory. */
2141 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2142 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2143 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2145 if ((abfd->flags & D_PAGED) != 0
2146 && (current->flags & SEC_ALLOC) != 0)
2148 sofar += (current->vma - sofar) % round;
2149 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2150 file_sofar += (current->vma - file_sofar) % round;
2153 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2154 current->filepos = file_sofar;
2156 sofar += current->size;
2157 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2158 file_sofar += current->size;
2160 /* Make sure that this section is of the right size too. */
2161 old_sofar = sofar;
2162 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2163 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2164 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2165 current->size += sofar - old_sofar;
2168 free (sorted_hdrs);
2169 sorted_hdrs = NULL;
2171 ecoff_data (abfd)->reloc_filepos = file_sofar;
2173 return true;
2176 /* Determine the location of the relocs for all the sections in the
2177 output file, as well as the location of the symbolic debugging
2178 information. */
2180 static bfd_size_type
2181 ecoff_compute_reloc_file_positions (bfd *abfd)
2183 const bfd_size_type external_reloc_size =
2184 ecoff_backend (abfd)->external_reloc_size;
2185 file_ptr reloc_base;
2186 bfd_size_type reloc_size;
2187 asection *current;
2188 file_ptr sym_base;
2190 if (! abfd->output_has_begun)
2192 if (! ecoff_compute_section_file_positions (abfd))
2193 abort ();
2194 abfd->output_has_begun = true;
2197 reloc_base = ecoff_data (abfd)->reloc_filepos;
2199 reloc_size = 0;
2200 for (current = abfd->sections;
2201 current != NULL;
2202 current = current->next)
2204 if (current->reloc_count == 0)
2205 current->rel_filepos = 0;
2206 else
2208 bfd_size_type relsize;
2210 current->rel_filepos = reloc_base;
2211 relsize = current->reloc_count * external_reloc_size;
2212 reloc_size += relsize;
2213 reloc_base += relsize;
2217 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2219 /* At least on Ultrix, the symbol table of an executable file must
2220 be aligned to a page boundary. FIXME: Is this true on other
2221 platforms? */
2222 if ((abfd->flags & EXEC_P) != 0
2223 && (abfd->flags & D_PAGED) != 0)
2224 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2225 &~ (ecoff_backend (abfd)->round - 1));
2227 ecoff_data (abfd)->sym_filepos = sym_base;
2229 return reloc_size;
2232 /* Set the contents of a section. */
2234 bool
2235 _bfd_ecoff_set_section_contents (bfd *abfd,
2236 asection *section,
2237 const void * location,
2238 file_ptr offset,
2239 bfd_size_type count)
2241 file_ptr pos;
2243 /* This must be done first, because bfd_set_section_contents is
2244 going to set output_has_begun to TRUE. */
2245 if (! abfd->output_has_begun
2246 && ! ecoff_compute_section_file_positions (abfd))
2247 return false;
2249 /* Handle the .lib section specially so that Irix 4 shared libraries
2250 work out. See coff_set_section_contents in coffcode.h. */
2251 if (streq (section->name, _LIB))
2253 bfd_byte *rec, *recend;
2255 rec = (bfd_byte *) location;
2256 recend = rec + count;
2257 while (rec < recend)
2259 ++section->lma;
2260 rec += bfd_get_32 (abfd, rec) * 4;
2263 BFD_ASSERT (rec == recend);
2266 if (count == 0)
2267 return true;
2269 pos = section->filepos + offset;
2270 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2271 || bfd_write (location, count, abfd) != count)
2272 return false;
2274 return true;
2277 /* Set the GP value for an ECOFF file. This is a hook used by the
2278 assembler. */
2280 bool
2281 bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value)
2283 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2284 || bfd_get_format (abfd) != bfd_object)
2286 bfd_set_error (bfd_error_invalid_operation);
2287 return false;
2290 ecoff_data (abfd)->gp = gp_value;
2292 return true;
2295 /* Set the register masks for an ECOFF file. This is a hook used by
2296 the assembler. */
2298 bool
2299 bfd_ecoff_set_regmasks (bfd *abfd,
2300 unsigned long gprmask,
2301 unsigned long fprmask,
2302 unsigned long *cprmask)
2304 ecoff_data_type *tdata;
2306 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2307 || bfd_get_format (abfd) != bfd_object)
2309 bfd_set_error (bfd_error_invalid_operation);
2310 return false;
2313 tdata = ecoff_data (abfd);
2314 tdata->gprmask = gprmask;
2315 tdata->fprmask = fprmask;
2316 if (cprmask != NULL)
2318 int i;
2320 for (i = 0; i < 3; i++)
2321 tdata->cprmask[i] = cprmask[i];
2324 return true;
2327 /* Get ECOFF EXTR information for an external symbol. This function
2328 is passed to bfd_ecoff_debug_externals. */
2330 static bool
2331 ecoff_get_extr (asymbol *sym, EXTR *esym)
2333 ecoff_symbol_type *ecoff_sym_ptr;
2334 bfd *input_bfd;
2336 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2337 || ecoffsymbol (sym)->native == NULL)
2339 /* Don't include debugging, local, or section symbols. */
2340 if ((sym->flags & BSF_DEBUGGING) != 0
2341 || (sym->flags & BSF_LOCAL) != 0
2342 || (sym->flags & BSF_SECTION_SYM) != 0)
2343 return false;
2345 esym->jmptbl = 0;
2346 esym->cobol_main = 0;
2347 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2348 esym->reserved = 0;
2349 esym->ifd = ifdNil;
2350 /* FIXME: we can do better than this for st and sc. */
2351 esym->asym.st = stGlobal;
2352 esym->asym.sc = scAbs;
2353 esym->asym.reserved = 0;
2354 esym->asym.index = indexNil;
2355 return true;
2358 ecoff_sym_ptr = ecoffsymbol (sym);
2360 if (ecoff_sym_ptr->local)
2361 return false;
2363 input_bfd = bfd_asymbol_bfd (sym);
2364 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2365 (input_bfd, ecoff_sym_ptr->native, esym);
2367 /* If the symbol was defined by the linker, then esym will be
2368 undefined but sym will not be. Get a better class for such a
2369 symbol. */
2370 if ((esym->asym.sc == scUndefined
2371 || esym->asym.sc == scSUndefined)
2372 && ! bfd_is_und_section (bfd_asymbol_section (sym)))
2373 esym->asym.sc = scAbs;
2375 /* Adjust the FDR index for the symbol by that used for the input
2376 BFD. */
2377 if (esym->ifd != -1)
2379 struct ecoff_debug_info *input_debug;
2381 input_debug = &ecoff_data (input_bfd)->debug_info;
2382 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2383 if (input_debug->ifdmap != NULL)
2384 esym->ifd = input_debug->ifdmap[esym->ifd];
2387 return true;
2390 /* Set the external symbol index. This routine is passed to
2391 bfd_ecoff_debug_externals. */
2393 static void
2394 ecoff_set_index (asymbol *sym, bfd_size_type indx)
2396 ecoff_set_sym_index (sym, indx);
2399 /* Write out an ECOFF file. */
2401 bool
2402 _bfd_ecoff_write_object_contents (bfd *abfd)
2404 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2405 const bfd_vma round = backend->round;
2406 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2407 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2408 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2409 const bfd_size_type external_hdr_size
2410 = backend->debug_swap.external_hdr_size;
2411 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2412 void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
2413 = backend->adjust_reloc_out;
2414 void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
2415 = backend->swap_reloc_out;
2416 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2417 HDRR * const symhdr = &debug->symbolic_header;
2418 asection *current;
2419 unsigned int count;
2420 bfd_size_type reloc_size;
2421 bfd_size_type text_size;
2422 bfd_vma text_start;
2423 bool set_text_start;
2424 bfd_size_type data_size;
2425 bfd_vma data_start;
2426 bool set_data_start;
2427 bfd_size_type bss_size;
2428 void * buff = NULL;
2429 void * reloc_buff = NULL;
2430 struct internal_filehdr internal_f;
2431 struct internal_aouthdr internal_a;
2432 int i;
2434 /* Determine where the sections and relocs will go in the output
2435 file. */
2436 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2438 count = 1;
2439 for (current = abfd->sections;
2440 current != NULL;
2441 current = current->next)
2443 current->target_index = count;
2444 ++count;
2447 if ((abfd->flags & D_PAGED) != 0)
2448 text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
2449 else
2450 text_size = 0;
2451 text_start = 0;
2452 set_text_start = false;
2453 data_size = 0;
2454 data_start = 0;
2455 set_data_start = false;
2456 bss_size = 0;
2458 /* Write section headers to the file. */
2460 /* Allocate buff big enough to hold a section header,
2461 file header, or a.out header. */
2463 bfd_size_type siz;
2465 siz = scnhsz;
2466 if (siz < filhsz)
2467 siz = filhsz;
2468 if (siz < aoutsz)
2469 siz = aoutsz;
2470 buff = bfd_malloc (siz);
2471 if (buff == NULL)
2472 goto error_return;
2475 internal_f.f_nscns = 0;
2476 if (bfd_seek (abfd, filhsz + aoutsz, SEEK_SET) != 0)
2477 goto error_return;
2479 for (current = abfd->sections;
2480 current != NULL;
2481 current = current->next)
2483 struct internal_scnhdr section;
2484 bfd_vma vma;
2486 ++internal_f.f_nscns;
2488 strncpy (section.s_name, current->name, sizeof section.s_name);
2490 /* This seems to be correct for Irix 4 shared libraries. */
2491 vma = bfd_section_vma (current);
2492 if (streq (current->name, _LIB))
2493 section.s_vaddr = 0;
2494 else
2495 section.s_vaddr = vma;
2497 section.s_paddr = current->lma;
2498 section.s_size = current->size;
2500 /* If this section is unloadable then the scnptr will be 0. */
2501 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2502 section.s_scnptr = 0;
2503 else
2504 section.s_scnptr = current->filepos;
2505 section.s_relptr = current->rel_filepos;
2507 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2508 object file produced by the assembler is supposed to point to
2509 information about how much room is required by objects of
2510 various different sizes. I think this only matters if we
2511 want the linker to compute the best size to use, or
2512 something. I don't know what happens if the information is
2513 not present. */
2514 if (! streq (current->name, _PDATA))
2515 section.s_lnnoptr = 0;
2516 else
2518 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2519 hold the number of entries in the section (each entry is
2520 8 bytes). We stored this in the line_filepos field in
2521 ecoff_compute_section_file_positions. */
2522 section.s_lnnoptr = current->line_filepos;
2525 section.s_nreloc = current->reloc_count;
2526 section.s_nlnno = 0;
2527 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2528 current->flags);
2530 if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff) == 0
2531 || bfd_write (buff, scnhsz, abfd) != scnhsz)
2532 goto error_return;
2534 if ((section.s_flags & STYP_TEXT) != 0
2535 || ((section.s_flags & STYP_RDATA) != 0
2536 && ecoff_data (abfd)->rdata_in_text)
2537 || section.s_flags == STYP_PDATA
2538 || (section.s_flags & STYP_DYNAMIC) != 0
2539 || (section.s_flags & STYP_LIBLIST) != 0
2540 || (section.s_flags & STYP_RELDYN) != 0
2541 || section.s_flags == STYP_CONFLIC
2542 || (section.s_flags & STYP_DYNSTR) != 0
2543 || (section.s_flags & STYP_DYNSYM) != 0
2544 || (section.s_flags & STYP_HASH) != 0
2545 || (section.s_flags & STYP_ECOFF_INIT) != 0
2546 || (section.s_flags & STYP_ECOFF_FINI) != 0
2547 || section.s_flags == STYP_RCONST)
2549 text_size += current->size;
2550 if (! set_text_start || text_start > vma)
2552 text_start = vma;
2553 set_text_start = true;
2556 else if ((section.s_flags & STYP_RDATA) != 0
2557 || (section.s_flags & STYP_DATA) != 0
2558 || (section.s_flags & STYP_LITA) != 0
2559 || (section.s_flags & STYP_LIT8) != 0
2560 || (section.s_flags & STYP_LIT4) != 0
2561 || (section.s_flags & STYP_SDATA) != 0
2562 || section.s_flags == STYP_XDATA
2563 || (section.s_flags & STYP_GOT) != 0)
2565 data_size += current->size;
2566 if (! set_data_start || data_start > vma)
2568 data_start = vma;
2569 set_data_start = true;
2572 else if ((section.s_flags & STYP_BSS) != 0
2573 || (section.s_flags & STYP_SBSS) != 0)
2574 bss_size += current->size;
2575 else if (section.s_flags == 0
2576 || (section.s_flags & STYP_ECOFF_LIB) != 0
2577 || section.s_flags == STYP_COMMENT)
2578 /* Do nothing. */ ;
2579 else
2580 abort ();
2583 /* Set up the file header. */
2584 internal_f.f_magic = ecoff_get_magic (abfd);
2586 /* We will NOT put a fucking timestamp in the header here. Every
2587 time you put it back, I will come in and take it out again. I'm
2588 sorry. This field does not belong here. We fill it with a 0 so
2589 it compares the same but is not a reasonable time. --
2590 gnu@cygnus.com. */
2591 internal_f.f_timdat = 0;
2593 if (bfd_get_symcount (abfd) != 0)
2595 /* The ECOFF f_nsyms field is not actually the number of
2596 symbols, it's the size of symbolic information header. */
2597 internal_f.f_nsyms = external_hdr_size;
2598 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2600 else
2602 internal_f.f_nsyms = 0;
2603 internal_f.f_symptr = 0;
2606 internal_f.f_opthdr = aoutsz;
2608 internal_f.f_flags = F_LNNO;
2609 if (reloc_size == 0)
2610 internal_f.f_flags |= F_RELFLG;
2611 if (bfd_get_symcount (abfd) == 0)
2612 internal_f.f_flags |= F_LSYMS;
2613 if (abfd->flags & EXEC_P)
2614 internal_f.f_flags |= F_EXEC;
2616 if (bfd_little_endian (abfd))
2617 internal_f.f_flags |= F_AR32WR;
2618 else
2619 internal_f.f_flags |= F_AR32W;
2621 /* Set up the ``optional'' header. */
2622 if ((abfd->flags & D_PAGED) != 0)
2623 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2624 else
2625 internal_a.magic = ECOFF_AOUT_OMAGIC;
2627 /* FIXME: Is this really correct? */
2628 internal_a.vstamp = symhdr->vstamp;
2630 /* At least on Ultrix, these have to be rounded to page boundaries.
2631 FIXME: Is this true on other platforms? */
2632 if ((abfd->flags & D_PAGED) != 0)
2634 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2635 internal_a.text_start = text_start &~ (round - 1);
2636 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2637 internal_a.data_start = data_start &~ (round - 1);
2639 else
2641 internal_a.tsize = text_size;
2642 internal_a.text_start = text_start;
2643 internal_a.dsize = data_size;
2644 internal_a.data_start = data_start;
2647 /* On Ultrix, the initial portions of the .sbss and .bss segments
2648 are at the end of the data section. The bsize field in the
2649 optional header records how many bss bytes are required beyond
2650 those in the data section. The value is not rounded to a page
2651 boundary. */
2652 if (bss_size < internal_a.dsize - data_size)
2653 bss_size = 0;
2654 else
2655 bss_size -= internal_a.dsize - data_size;
2656 internal_a.bsize = bss_size;
2657 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2659 internal_a.entry = bfd_get_start_address (abfd);
2661 internal_a.gp_value = ecoff_data (abfd)->gp;
2663 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2664 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2665 for (i = 0; i < 4; i++)
2666 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2668 /* Let the backend adjust the headers if necessary. */
2669 if (backend->adjust_headers)
2671 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2672 goto error_return;
2675 /* Write out the file header and the optional header. */
2676 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
2677 goto error_return;
2679 bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
2680 if (bfd_write (buff, filhsz, abfd) != filhsz)
2681 goto error_return;
2683 bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
2684 if (bfd_write (buff, aoutsz, abfd) != aoutsz)
2685 goto error_return;
2687 /* Build the external symbol information. This must be done before
2688 writing out the relocs so that we know the symbol indices. We
2689 don't do this if this BFD was created by the backend linker,
2690 since it will have already handled the symbols and relocs. */
2691 if (! ecoff_data (abfd)->linker)
2693 symhdr->iextMax = 0;
2694 symhdr->issExtMax = 0;
2695 debug->external_ext = debug->external_ext_end = NULL;
2696 debug->ssext = debug->ssext_end = NULL;
2697 if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2698 (abfd->flags & EXEC_P) == 0,
2699 ecoff_get_extr, ecoff_set_index))
2700 goto error_return;
2702 /* Write out the relocs. */
2703 for (current = abfd->sections;
2704 current != NULL;
2705 current = current->next)
2707 arelent **reloc_ptr_ptr;
2708 arelent **reloc_end;
2709 char *out_ptr;
2710 bfd_size_type amt;
2712 if (current->reloc_count == 0)
2713 continue;
2715 amt = current->reloc_count * external_reloc_size;
2716 reloc_buff = bfd_zalloc (abfd, amt);
2717 if (reloc_buff == NULL)
2718 goto error_return;
2720 reloc_ptr_ptr = current->orelocation;
2721 reloc_end = reloc_ptr_ptr + current->reloc_count;
2722 out_ptr = (char *) reloc_buff;
2724 for (;
2725 reloc_ptr_ptr < reloc_end;
2726 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2728 arelent *reloc;
2729 asymbol *sym;
2730 struct internal_reloc in;
2732 memset ((void *) &in, 0, sizeof in);
2734 reloc = *reloc_ptr_ptr;
2735 sym = *reloc->sym_ptr_ptr;
2737 /* If the howto field has not been initialised then skip this reloc.
2738 This assumes that an error message has been issued elsewhere. */
2739 if (reloc->howto == NULL)
2740 continue;
2742 in.r_vaddr = reloc->address + bfd_section_vma (current);
2743 in.r_type = reloc->howto->type;
2745 if ((sym->flags & BSF_SECTION_SYM) == 0)
2747 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2748 in.r_extern = 1;
2750 else
2752 const char *name;
2753 unsigned int j;
2754 static struct
2756 const char * name;
2757 long r_symndx;
2759 section_symndx [] =
2761 { _TEXT, RELOC_SECTION_TEXT },
2762 { _RDATA, RELOC_SECTION_RDATA },
2763 { _DATA, RELOC_SECTION_DATA },
2764 { _SDATA, RELOC_SECTION_SDATA },
2765 { _SBSS, RELOC_SECTION_SBSS },
2766 { _BSS, RELOC_SECTION_BSS },
2767 { _INIT, RELOC_SECTION_INIT },
2768 { _LIT8, RELOC_SECTION_LIT8 },
2769 { _LIT4, RELOC_SECTION_LIT4 },
2770 { _XDATA, RELOC_SECTION_XDATA },
2771 { _PDATA, RELOC_SECTION_PDATA },
2772 { _FINI, RELOC_SECTION_FINI },
2773 { _LITA, RELOC_SECTION_LITA },
2774 { "*ABS*", RELOC_SECTION_ABS },
2775 { _RCONST, RELOC_SECTION_RCONST }
2778 name = bfd_section_name (bfd_asymbol_section (sym));
2780 for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
2781 if (streq (name, section_symndx[j].name))
2783 in.r_symndx = section_symndx[j].r_symndx;
2784 break;
2787 if (j == ARRAY_SIZE (section_symndx))
2788 abort ();
2789 in.r_extern = 0;
2792 (*adjust_reloc_out) (abfd, reloc, &in);
2794 (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
2797 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2798 goto error_return;
2799 amt = current->reloc_count * external_reloc_size;
2800 if (bfd_write (reloc_buff, amt, abfd) != amt)
2801 goto error_return;
2802 bfd_release (abfd, reloc_buff);
2803 reloc_buff = NULL;
2806 /* Write out the symbolic debugging information. */
2807 if (bfd_get_symcount (abfd) > 0)
2809 /* Write out the debugging information. */
2810 if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2811 ecoff_data (abfd)->sym_filepos))
2812 goto error_return;
2816 /* The .bss section of a demand paged executable must receive an
2817 entire page. If there are symbols, the symbols will start on the
2818 next page. If there are no symbols, we must fill out the page by
2819 hand. */
2820 if (bfd_get_symcount (abfd) == 0
2821 && (abfd->flags & EXEC_P) != 0
2822 && (abfd->flags & D_PAGED) != 0)
2824 char c;
2826 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2827 goto error_return;
2828 if (bfd_read (&c, 1, abfd) == 0)
2829 c = 0;
2830 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2831 goto error_return;
2832 if (bfd_write (&c, 1, abfd) != 1)
2833 goto error_return;
2836 if (reloc_buff != NULL)
2837 bfd_release (abfd, reloc_buff);
2838 free (buff);
2839 return true;
2840 error_return:
2841 if (reloc_buff != NULL)
2842 bfd_release (abfd, reloc_buff);
2843 free (buff);
2844 return false;
2847 /* Archive handling. ECOFF uses what appears to be a unique type of
2848 archive header (armap). The byte ordering of the armap and the
2849 contents are encoded in the name of the armap itself. At least for
2850 now, we only support archives with the same byte ordering in the
2851 armap and the contents.
2853 The first four bytes in the armap are the number of symbol
2854 definitions. This is always a power of two.
2856 This is followed by the symbol definitions. Each symbol definition
2857 occupies 8 bytes. The first four bytes are the offset from the
2858 start of the armap strings to the null-terminated string naming
2859 this symbol. The second four bytes are the file offset to the
2860 archive member which defines this symbol. If the second four bytes
2861 are 0, then this is not actually a symbol definition, and it should
2862 be ignored.
2864 The symbols are hashed into the armap with a closed hashing scheme.
2865 See the functions below for the details of the algorithm.
2867 After the symbol definitions comes four bytes holding the size of
2868 the string table, followed by the string table itself. */
2870 /* The name of an archive headers looks like this:
2871 __________E[BL]E[BL]_ (with a trailing space).
2872 The trailing space is changed to an X if the archive is changed to
2873 indicate that the armap is out of date.
2875 The Alpha seems to use ________64E[BL]E[BL]_. */
2877 #define ARMAP_BIG_ENDIAN 'B'
2878 #define ARMAP_LITTLE_ENDIAN 'L'
2879 #define ARMAP_MARKER 'E'
2880 #define ARMAP_START_LENGTH 10
2881 #define ARMAP_HEADER_MARKER_INDEX 10
2882 #define ARMAP_HEADER_ENDIAN_INDEX 11
2883 #define ARMAP_OBJECT_MARKER_INDEX 12
2884 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2885 #define ARMAP_END_INDEX 14
2886 #define ARMAP_END "_ "
2888 /* This is a magic number used in the hashing algorithm. */
2889 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2891 /* This returns the hash value to use for a string. It also sets
2892 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2893 is the number of entries in the hash table, and HLOG is the log
2894 base 2 of SIZE. */
2896 static unsigned int
2897 ecoff_armap_hash (const char *s,
2898 unsigned int *rehash,
2899 unsigned int size,
2900 unsigned int hlog)
2902 unsigned int hash;
2904 if (hlog == 0)
2905 return 0;
2906 hash = *s++;
2907 while (*s != '\0')
2908 hash = ((hash >> 27) | (hash << 5)) + *s++;
2909 hash *= ARMAP_HASH_MAGIC;
2910 *rehash = (hash & (size - 1)) | 1;
2911 return hash >> (32 - hlog);
2914 /* Read in the armap. */
2916 bool
2917 _bfd_ecoff_slurp_armap (bfd *abfd)
2919 char nextname[17];
2920 unsigned int i;
2921 struct areltdata *mapdata;
2922 bfd_size_type parsed_size, stringsize;
2923 char *raw_armap;
2924 struct artdata *ardata;
2925 unsigned int count;
2926 char *raw_ptr;
2927 carsym *symdef_ptr;
2928 char *stringbase;
2929 bfd_size_type amt;
2931 /* Get the name of the first element. */
2932 i = bfd_read (nextname, 16, abfd);
2933 if (i == 0)
2934 return true;
2935 if (i != 16)
2936 return false;
2938 if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
2939 return false;
2941 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2942 standard COFF armap. We could move the ECOFF armap stuff into
2943 bfd_slurp_armap, but that seems inappropriate since no other
2944 target uses this format. Instead, we check directly for a COFF
2945 armap. */
2946 if (startswith (nextname, "/ "))
2947 return bfd_slurp_armap (abfd);
2949 /* See if the first element is an armap. */
2950 if (strncmp (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH) != 0
2951 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2952 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2953 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2954 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2955 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2956 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2957 || strncmp (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1) != 0)
2959 abfd->has_armap = false;
2960 return true;
2963 /* Make sure we have the right byte ordering. */
2964 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2965 ^ (bfd_header_big_endian (abfd)))
2966 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2967 ^ (bfd_big_endian (abfd))))
2969 bfd_set_error (bfd_error_wrong_format);
2970 return false;
2973 /* Read in the armap. */
2974 ardata = bfd_ardata (abfd);
2975 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2976 if (mapdata == NULL)
2977 return false;
2978 parsed_size = mapdata->parsed_size;
2979 free (mapdata);
2981 if (parsed_size + 1 < 9)
2983 bfd_set_error (bfd_error_malformed_archive);
2984 return false;
2987 raw_armap = (char *) _bfd_alloc_and_read (abfd, parsed_size + 1, parsed_size);
2988 if (raw_armap == NULL)
2989 return false;
2990 raw_armap[parsed_size] = 0;
2992 ardata->tdata = (void *) raw_armap;
2994 count = H_GET_32 (abfd, raw_armap);
2995 if ((parsed_size - 8) / 8 < count)
2996 goto error_malformed;
2998 ardata->symdef_count = 0;
2999 ardata->cache = NULL;
3001 /* This code used to overlay the symdefs over the raw archive data,
3002 but that doesn't work on a 64 bit host. */
3003 stringbase = raw_armap + count * 8 + 8;
3004 stringsize = parsed_size - (count * 8 + 8);
3006 #ifdef CHECK_ARMAP_HASH
3008 unsigned int hlog;
3010 /* Double check that I have the hashing algorithm right by making
3011 sure that every symbol can be looked up successfully. */
3012 hlog = 0;
3013 for (i = 1; i < count; i <<= 1)
3014 hlog++;
3015 BFD_ASSERT (i == count);
3017 raw_ptr = raw_armap + 4;
3018 for (i = 0; i < count; i++, raw_ptr += 8)
3020 unsigned int name_offset, file_offset;
3021 unsigned int hash, rehash, srch;
3023 name_offset = H_GET_32 (abfd, raw_ptr);
3024 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3025 if (file_offset == 0)
3026 continue;
3027 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3028 hlog);
3029 if (hash == i)
3030 continue;
3032 /* See if we can rehash to this location. */
3033 for (srch = (hash + rehash) & (count - 1);
3034 srch != hash && srch != i;
3035 srch = (srch + rehash) & (count - 1))
3036 BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
3037 BFD_ASSERT (srch == i);
3041 #endif /* CHECK_ARMAP_HASH */
3043 raw_ptr = raw_armap + 4;
3044 for (i = 0; i < count; i++, raw_ptr += 8)
3045 if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
3046 ++ardata->symdef_count;
3048 amt = ardata->symdef_count;
3049 amt *= sizeof (carsym);
3050 symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
3051 if (!symdef_ptr)
3052 goto error_exit;
3054 ardata->symdefs = symdef_ptr;
3056 raw_ptr = raw_armap + 4;
3057 for (i = 0; i < count; i++, raw_ptr += 8)
3059 unsigned int name_offset, file_offset;
3061 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3062 if (file_offset == 0)
3063 continue;
3064 name_offset = H_GET_32 (abfd, raw_ptr);
3065 if (name_offset > stringsize)
3066 goto error_malformed;
3067 symdef_ptr->name = stringbase + name_offset;
3068 symdef_ptr->file_offset = file_offset;
3069 ++symdef_ptr;
3072 ardata->first_file_filepos = bfd_tell (abfd);
3073 /* Pad to an even boundary. */
3074 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3075 abfd->has_armap = true;
3076 return true;
3078 error_malformed:
3079 bfd_set_error (bfd_error_malformed_archive);
3080 error_exit:
3081 ardata->symdef_count = 0;
3082 ardata->symdefs = NULL;
3083 ardata->tdata = NULL;
3084 bfd_release (abfd, raw_armap);
3085 return false;
3088 /* Write out an armap. */
3090 bool
3091 _bfd_ecoff_write_armap (bfd *abfd,
3092 unsigned int elength,
3093 struct orl *map,
3094 unsigned int orl_count,
3095 int stridx)
3097 unsigned int hashsize, hashlog;
3098 bfd_size_type symdefsize;
3099 int padit;
3100 unsigned int stringsize;
3101 unsigned int mapsize;
3102 file_ptr firstreal;
3103 struct ar_hdr hdr;
3104 struct stat statbuf;
3105 unsigned int i;
3106 bfd_byte temp[4];
3107 bfd_byte *hashtable;
3108 bfd *current;
3109 bfd *last_elt;
3111 /* Ultrix appears to use as a hash table size the least power of two
3112 greater than twice the number of entries. */
3113 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3115 hashsize = 1 << hashlog;
3117 symdefsize = hashsize * 8;
3118 padit = stridx % 2;
3119 stringsize = stridx + padit;
3121 /* Include 8 bytes to store symdefsize and stringsize in output. */
3122 mapsize = symdefsize + stringsize + 8;
3124 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3126 memset ((void *) &hdr, 0, sizeof hdr);
3128 /* Work out the ECOFF armap name. */
3129 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3130 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3131 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3132 (bfd_header_big_endian (abfd)
3133 ? ARMAP_BIG_ENDIAN
3134 : ARMAP_LITTLE_ENDIAN);
3135 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3136 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3137 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3138 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3140 /* Write the timestamp of the archive header to be just a little bit
3141 later than the timestamp of the file, otherwise the linker will
3142 complain that the index is out of date. Actually, the Ultrix
3143 linker just checks the archive name; the GNU linker may check the
3144 date. */
3145 stat (bfd_get_filename (abfd), &statbuf);
3146 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
3147 (long) (statbuf.st_mtime + 60));
3149 /* The DECstation uses zeroes for the uid, gid and mode of the
3150 armap. */
3151 hdr.ar_uid[0] = '0';
3152 hdr.ar_gid[0] = '0';
3153 /* Building gcc ends up extracting the armap as a file - twice. */
3154 hdr.ar_mode[0] = '6';
3155 hdr.ar_mode[1] = '4';
3156 hdr.ar_mode[2] = '4';
3158 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
3160 hdr.ar_fmag[0] = '`';
3161 hdr.ar_fmag[1] = '\012';
3163 /* Turn all null bytes in the header into spaces. */
3164 for (i = 0; i < sizeof (struct ar_hdr); i++)
3165 if (((char *) (&hdr))[i] == '\0')
3166 (((char *) (&hdr))[i]) = ' ';
3168 if (bfd_write (&hdr, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
3169 return false;
3171 H_PUT_32 (abfd, hashsize, temp);
3172 if (bfd_write (temp, 4, abfd) != 4)
3173 return false;
3175 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3176 if (!hashtable)
3177 return false;
3179 current = abfd->archive_head;
3180 last_elt = current;
3181 for (i = 0; i < orl_count; i++)
3183 unsigned int hash, rehash = 0;
3185 /* Advance firstreal to the file position of this archive
3186 element. */
3187 if (map[i].u.abfd != last_elt)
3191 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3192 firstreal += firstreal % 2;
3193 current = current->archive_next;
3195 while (current != map[i].u.abfd);
3198 last_elt = current;
3200 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3201 if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
3203 unsigned int srch;
3205 /* The desired slot is already taken. */
3206 for (srch = (hash + rehash) & (hashsize - 1);
3207 srch != hash;
3208 srch = (srch + rehash) & (hashsize - 1))
3209 if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
3210 break;
3212 BFD_ASSERT (srch != hash);
3214 hash = srch;
3217 H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
3218 H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
3221 if (bfd_write (hashtable, symdefsize, abfd) != symdefsize)
3222 return false;
3224 bfd_release (abfd, hashtable);
3226 /* Now write the strings. */
3227 H_PUT_32 (abfd, stringsize, temp);
3228 if (bfd_write (temp, 4, abfd) != 4)
3229 return false;
3230 for (i = 0; i < orl_count; i++)
3232 bfd_size_type len;
3234 len = strlen (*map[i].name) + 1;
3235 if (bfd_write (*map[i].name, len, abfd) != len)
3236 return false;
3239 /* The spec sez this should be a newline. But in order to be
3240 bug-compatible for DECstation ar we use a null. */
3241 if (padit)
3243 if (bfd_write ("", 1, abfd) != 1)
3244 return false;
3247 return true;
3250 /* ECOFF linker code. */
3252 /* Routine to create an entry in an ECOFF link hash table. */
3254 static struct bfd_hash_entry *
3255 ecoff_link_hash_newfunc (struct bfd_hash_entry *entry,
3256 struct bfd_hash_table *table,
3257 const char *string)
3259 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3261 /* Allocate the structure if it has not already been allocated by a
3262 subclass. */
3263 if (ret == NULL)
3264 ret = ((struct ecoff_link_hash_entry *)
3265 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3266 if (ret == NULL)
3267 return NULL;
3269 /* Call the allocation method of the superclass. */
3270 ret = ((struct ecoff_link_hash_entry *)
3271 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3272 table, string));
3274 if (ret)
3276 /* Set local fields. */
3277 ret->indx = -1;
3278 ret->abfd = NULL;
3279 ret->written = 0;
3280 ret->small = 0;
3281 memset ((void *) &ret->esym, 0, sizeof ret->esym);
3284 return (struct bfd_hash_entry *) ret;
3287 /* Create an ECOFF link hash table. */
3289 struct bfd_link_hash_table *
3290 _bfd_ecoff_bfd_link_hash_table_create (bfd *abfd)
3292 struct ecoff_link_hash_table *ret;
3293 size_t amt = sizeof (struct ecoff_link_hash_table);
3295 ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3296 if (ret == NULL)
3297 return NULL;
3298 if (!_bfd_link_hash_table_init (&ret->root, abfd,
3299 ecoff_link_hash_newfunc,
3300 sizeof (struct ecoff_link_hash_entry)))
3302 free (ret);
3303 return NULL;
3305 return &ret->root;
3308 /* Look up an entry in an ECOFF link hash table. */
3310 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3311 ((struct ecoff_link_hash_entry *) \
3312 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3314 /* Get the ECOFF link hash table from the info structure. This is
3315 just a cast. */
3317 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3319 /* Add the external symbols of an object file to the global linker
3320 hash table. The external symbols and strings we are passed are
3321 just allocated on the stack, and will be discarded. We must
3322 explicitly save any information we may need later on in the link.
3323 We do not want to read the external symbol information again. */
3325 static bool
3326 ecoff_link_add_externals (bfd *abfd,
3327 struct bfd_link_info *info,
3328 void * external_ext,
3329 char *ssext)
3331 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3332 void (* const swap_ext_in) (bfd *, void *, EXTR *)
3333 = backend->debug_swap.swap_ext_in;
3334 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3335 unsigned long ext_count;
3336 struct bfd_link_hash_entry **sym_hash;
3337 char *ext_ptr;
3338 char *ext_end;
3339 bfd_size_type amt;
3341 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3343 amt = ext_count;
3344 amt *= sizeof (struct bfd_link_hash_entry *);
3345 sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
3346 if (!sym_hash)
3347 return false;
3348 ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
3350 ext_ptr = (char *) external_ext;
3351 ext_end = ext_ptr + ext_count * external_ext_size;
3352 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3354 EXTR esym;
3355 bool skip;
3356 bfd_vma value;
3357 asection *section;
3358 const char *name;
3359 struct ecoff_link_hash_entry *h;
3361 *sym_hash = NULL;
3363 (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
3365 /* Skip debugging symbols. */
3366 skip = false;
3367 switch (esym.asym.st)
3369 case stGlobal:
3370 case stStatic:
3371 case stLabel:
3372 case stProc:
3373 case stStaticProc:
3374 break;
3375 default:
3376 skip = true;
3377 break;
3380 if (skip)
3381 continue;
3383 /* Get the information for this symbol. */
3384 value = esym.asym.value;
3385 switch (esym.asym.sc)
3387 default:
3388 case scNil:
3389 case scRegister:
3390 case scCdbLocal:
3391 case scBits:
3392 case scCdbSystem:
3393 case scRegImage:
3394 case scInfo:
3395 case scUserStruct:
3396 case scVar:
3397 case scVarRegister:
3398 case scVariant:
3399 case scBasedVar:
3400 case scXData:
3401 case scPData:
3402 section = NULL;
3403 break;
3404 case scText:
3405 section = bfd_make_section_old_way (abfd, _TEXT);
3406 value -= section->vma;
3407 break;
3408 case scData:
3409 section = bfd_make_section_old_way (abfd, _DATA);
3410 value -= section->vma;
3411 break;
3412 case scBss:
3413 section = bfd_make_section_old_way (abfd, _BSS);
3414 value -= section->vma;
3415 break;
3416 case scAbs:
3417 section = bfd_abs_section_ptr;
3418 break;
3419 case scUndefined:
3420 section = bfd_und_section_ptr;
3421 break;
3422 case scSData:
3423 section = bfd_make_section_old_way (abfd, _SDATA);
3424 value -= section->vma;
3425 break;
3426 case scSBss:
3427 section = bfd_make_section_old_way (abfd, _SBSS);
3428 value -= section->vma;
3429 break;
3430 case scRData:
3431 section = bfd_make_section_old_way (abfd, _RDATA);
3432 value -= section->vma;
3433 break;
3434 case scCommon:
3435 if (value > ecoff_data (abfd)->gp_size)
3437 section = bfd_com_section_ptr;
3438 break;
3440 /* Fall through. */
3441 case scSCommon:
3442 section = &ecoff_scom_section;
3443 break;
3444 case scSUndefined:
3445 section = bfd_und_section_ptr;
3446 break;
3447 case scInit:
3448 section = bfd_make_section_old_way (abfd, _INIT);
3449 value -= section->vma;
3450 break;
3451 case scFini:
3452 section = bfd_make_section_old_way (abfd, _FINI);
3453 value -= section->vma;
3454 break;
3455 case scRConst:
3456 section = bfd_make_section_old_way (abfd, _RCONST);
3457 value -= section->vma;
3458 break;
3461 if (section == NULL)
3462 continue;
3464 name = ssext + esym.asym.iss;
3466 if (! (_bfd_generic_link_add_one_symbol
3467 (info, abfd, name,
3468 (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3469 section, value, NULL, true, true, sym_hash)))
3470 return false;
3472 h = (struct ecoff_link_hash_entry *) *sym_hash;
3474 /* If we are building an ECOFF hash table, save the external
3475 symbol information. */
3476 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
3478 if (h->abfd == NULL
3479 || (! bfd_is_und_section (section)
3480 && (! bfd_is_com_section (section)
3481 || (h->root.type != bfd_link_hash_defined
3482 && h->root.type != bfd_link_hash_defweak))))
3484 h->abfd = abfd;
3485 h->esym = esym;
3488 /* Remember whether this symbol was small undefined. */
3489 if (esym.asym.sc == scSUndefined)
3490 h->small = 1;
3492 /* If this symbol was ever small undefined, it needs to wind
3493 up in a GP relative section. We can't control the
3494 section of a defined symbol, but we can control the
3495 section of a common symbol. This case is actually needed
3496 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
3497 if (h->small
3498 && h->root.type == bfd_link_hash_common
3499 && streq (h->root.u.c.p->section->name, SCOMMON))
3501 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3502 SCOMMON);
3503 h->root.u.c.p->section->flags = SEC_ALLOC;
3504 if (h->esym.asym.sc == scCommon)
3505 h->esym.asym.sc = scSCommon;
3510 return true;
3513 /* Add symbols from an ECOFF object file to the global linker hash
3514 table. */
3516 static bool
3517 ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3519 HDRR *symhdr;
3520 bfd_size_type external_ext_size;
3521 void * external_ext = NULL;
3522 bfd_size_type esize;
3523 char *ssext = NULL;
3524 bool result;
3526 if (! ecoff_slurp_symbolic_header (abfd))
3527 return false;
3529 /* If there are no symbols, we don't want it. */
3530 if (bfd_get_symcount (abfd) == 0)
3531 return true;
3533 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3535 /* Read in the external symbols and external strings. */
3536 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0)
3537 return false;
3538 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3539 esize = symhdr->iextMax * external_ext_size;
3540 external_ext = _bfd_malloc_and_read (abfd, esize, esize);
3541 if (external_ext == NULL && esize != 0)
3542 goto error_return;
3544 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0)
3545 goto error_return;
3546 ssext = (char *) _bfd_malloc_and_read (abfd, symhdr->issExtMax,
3547 symhdr->issExtMax);
3548 if (ssext == NULL && symhdr->issExtMax != 0)
3549 goto error_return;
3551 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3553 free (ssext);
3554 free (external_ext);
3555 return result;
3557 error_return:
3558 free (ssext);
3559 free (external_ext);
3560 return false;
3563 /* This is called if we used _bfd_generic_link_add_archive_symbols
3564 because we were not dealing with an ECOFF archive. */
3566 static bool
3567 ecoff_link_check_archive_element (bfd *abfd,
3568 struct bfd_link_info *info,
3569 struct bfd_link_hash_entry *h,
3570 const char *name,
3571 bool *pneeded)
3573 *pneeded = false;
3575 /* Unlike the generic linker, we do not pull in elements because
3576 of common symbols. */
3577 if (h->type != bfd_link_hash_undefined)
3578 return true;
3580 /* Include this element? */
3581 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
3582 return true;
3583 *pneeded = true;
3585 return ecoff_link_add_object_symbols (abfd, info);
3588 /* Add the symbols from an archive file to the global hash table.
3589 This looks through the undefined symbols, looks each one up in the
3590 archive hash table, and adds any associated object file. We do not
3591 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3592 already have a hash table, so there is no reason to construct
3593 another one. */
3595 static bool
3596 ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
3598 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3599 const bfd_byte *raw_armap;
3600 struct bfd_link_hash_entry **pundef;
3601 unsigned int armap_count;
3602 unsigned int armap_log;
3603 unsigned int i;
3604 const bfd_byte *hashtable;
3605 const char *stringbase;
3607 if (! bfd_has_map (abfd))
3609 /* An empty archive is a special case. */
3610 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
3611 return true;
3612 bfd_set_error (bfd_error_no_armap);
3613 return false;
3616 /* If we don't have any raw data for this archive, as can happen on
3617 Irix 4.0.5F, we call the generic routine.
3618 FIXME: We should be more clever about this, since someday tdata
3619 may get to something for a generic archive. */
3620 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3621 if (raw_armap == NULL)
3622 return (_bfd_generic_link_add_archive_symbols
3623 (abfd, info, ecoff_link_check_archive_element));
3625 armap_count = H_GET_32 (abfd, raw_armap);
3627 armap_log = 0;
3628 for (i = 1; i < armap_count; i <<= 1)
3629 armap_log++;
3630 BFD_ASSERT (i == armap_count);
3632 hashtable = raw_armap + 4;
3633 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3635 /* Look through the list of undefined symbols. */
3636 pundef = &info->hash->undefs;
3637 while (*pundef != NULL)
3639 struct bfd_link_hash_entry *h;
3640 unsigned int hash, rehash = 0;
3641 unsigned int file_offset;
3642 const char *name;
3643 bfd *element;
3645 h = *pundef;
3647 /* When a symbol is defined, it is not necessarily removed from
3648 the list. */
3649 if (h->type != bfd_link_hash_undefined
3650 && h->type != bfd_link_hash_common)
3652 /* Remove this entry from the list, for general cleanliness
3653 and because we are going to look through the list again
3654 if we search any more libraries. We can't remove the
3655 entry if it is the tail, because that would lose any
3656 entries we add to the list later on. */
3657 if (*pundef != info->hash->undefs_tail)
3658 *pundef = (*pundef)->u.undef.next;
3659 else
3660 pundef = &(*pundef)->u.undef.next;
3661 continue;
3664 /* Native ECOFF linkers do not pull in archive elements merely
3665 to satisfy common definitions, so neither do we. We leave
3666 them on the list, though, in case we are linking against some
3667 other object format. */
3668 if (h->type != bfd_link_hash_undefined)
3670 pundef = &(*pundef)->u.undef.next;
3671 continue;
3674 /* Look for this symbol in the archive hash table. */
3675 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3676 armap_log);
3678 file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3679 if (file_offset == 0)
3681 /* Nothing in this slot. */
3682 pundef = &(*pundef)->u.undef.next;
3683 continue;
3686 name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3687 if (name[0] != h->root.string[0]
3688 || ! streq (name, h->root.string))
3690 unsigned int srch;
3691 bool found;
3693 /* That was the wrong symbol. Try rehashing. */
3694 found = false;
3695 for (srch = (hash + rehash) & (armap_count - 1);
3696 srch != hash;
3697 srch = (srch + rehash) & (armap_count - 1))
3699 file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3700 if (file_offset == 0)
3701 break;
3702 name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3703 if (name[0] == h->root.string[0]
3704 && streq (name, h->root.string))
3706 found = true;
3707 break;
3711 if (! found)
3713 pundef = &(*pundef)->u.undef.next;
3714 continue;
3717 hash = srch;
3720 element = (*backend->get_elt_at_filepos) (abfd,
3721 (file_ptr) file_offset,
3722 info);
3723 if (element == NULL)
3724 return false;
3726 if (! bfd_check_format (element, bfd_object))
3727 return false;
3729 /* Unlike the generic linker, we know that this element provides
3730 a definition for an undefined symbol and we know that we want
3731 to include it. We don't need to check anything. */
3732 if (!(*info->callbacks
3733 ->add_archive_element) (info, element, name, &element))
3734 return false;
3735 if (! ecoff_link_add_object_symbols (element, info))
3736 return false;
3738 pundef = &(*pundef)->u.undef.next;
3741 return true;
3744 /* Given an ECOFF BFD, add symbols to the global hash table as
3745 appropriate. */
3747 bool
3748 _bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
3750 switch (bfd_get_format (abfd))
3752 case bfd_object:
3753 return ecoff_link_add_object_symbols (abfd, info);
3754 case bfd_archive:
3755 return ecoff_link_add_archive_symbols (abfd, info);
3756 default:
3757 bfd_set_error (bfd_error_wrong_format);
3758 return false;
3763 /* ECOFF final link routines. */
3765 /* Structure used to pass information to ecoff_link_write_external. */
3767 struct extsym_info
3769 bfd *abfd;
3770 struct bfd_link_info *info;
3773 /* Accumulate the debugging information for an input BFD into the
3774 output BFD. This must read in the symbolic information of the
3775 input BFD. */
3777 static bool
3778 ecoff_final_link_debug_accumulate (bfd *output_bfd,
3779 bfd *input_bfd,
3780 struct bfd_link_info *info,
3781 void * handle)
3783 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
3784 const struct ecoff_debug_swap * const swap =
3785 &ecoff_backend (input_bfd)->debug_swap;
3786 HDRR *symhdr = &debug->symbolic_header;
3787 bool ret;
3789 #define READ(ptr, offset, count, size) \
3790 do \
3792 size_t amt; \
3793 debug->ptr = NULL; \
3794 if (symhdr->count == 0) \
3795 break; \
3796 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
3798 bfd_set_error (bfd_error_file_too_big); \
3799 ret = false; \
3800 goto return_something; \
3802 if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0) \
3804 ret = false; \
3805 goto return_something; \
3807 debug->ptr = _bfd_malloc_and_read (input_bfd, amt + 1, amt); \
3808 if (debug->ptr == NULL) \
3810 ret = false; \
3811 goto return_something; \
3813 ((char *) debug->ptr)[amt] = 0; \
3814 } while (0)
3816 /* If alloc_syments is true, then the data was already by read by
3817 _bfd_ecoff_slurp_symbolic_info. */
3818 if (!debug->alloc_syments)
3820 READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
3821 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
3822 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
3823 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
3824 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
3825 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
3826 READ (ss, cbSsOffset, issMax, sizeof (char));
3827 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
3828 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
3830 #undef READ
3832 /* We do not read the external strings or the external symbols. */
3834 ret = (bfd_ecoff_debug_accumulate
3835 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
3836 &ecoff_backend (output_bfd)->debug_swap,
3837 input_bfd, debug, swap, info));
3839 return_something:
3840 _bfd_ecoff_free_ecoff_debug_info (debug);
3841 return ret;
3844 /* Relocate and write an ECOFF section into an ECOFF output file. */
3846 static bool
3847 ecoff_indirect_link_order (bfd *output_bfd,
3848 struct bfd_link_info *info,
3849 asection *output_section,
3850 struct bfd_link_order *link_order)
3852 asection *input_section;
3853 bfd *input_bfd;
3854 bfd_byte *contents = NULL;
3855 bfd_size_type external_reloc_size;
3856 bfd_size_type external_relocs_size;
3857 void * external_relocs = NULL;
3859 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
3861 input_section = link_order->u.indirect.section;
3862 input_bfd = input_section->owner;
3863 if (input_section->size == 0)
3864 return true;
3866 BFD_ASSERT (input_section->output_section == output_section);
3867 BFD_ASSERT (input_section->output_offset == link_order->offset);
3868 BFD_ASSERT (input_section->size == link_order->size);
3870 /* Get the section contents. */
3871 if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
3872 goto error_return;
3874 /* Get the relocs. If we are relaxing MIPS code, they will already
3875 have been read in. Otherwise, we read them in now. */
3876 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
3877 external_relocs_size = external_reloc_size * input_section->reloc_count;
3879 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0)
3880 goto error_return;
3881 external_relocs = _bfd_malloc_and_read (input_bfd, external_relocs_size,
3882 external_relocs_size);
3883 if (external_relocs == NULL && external_relocs_size != 0)
3884 goto error_return;
3886 /* Relocate the section contents. */
3887 if (! ((*ecoff_backend (input_bfd)->relocate_section)
3888 (output_bfd, info, input_bfd, input_section, contents,
3889 external_relocs)))
3890 goto error_return;
3892 /* Write out the relocated section. */
3893 if (! bfd_set_section_contents (output_bfd,
3894 output_section,
3895 contents,
3896 input_section->output_offset,
3897 input_section->size))
3898 goto error_return;
3900 /* If we are producing relocatable output, the relocs were
3901 modified, and we write them out now. We use the reloc_count
3902 field of output_section to keep track of the number of relocs we
3903 have output so far. */
3904 if (bfd_link_relocatable (info))
3906 file_ptr pos = (output_section->rel_filepos
3907 + output_section->reloc_count * external_reloc_size);
3908 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3909 || (bfd_write (external_relocs, external_relocs_size, output_bfd)
3910 != external_relocs_size))
3911 goto error_return;
3912 output_section->reloc_count += input_section->reloc_count;
3915 free (contents);
3916 free (external_relocs);
3917 return true;
3919 error_return:
3920 free (contents);
3921 free (external_relocs);
3922 return false;
3925 /* Generate a reloc when linking an ECOFF file. This is a reloc
3926 requested by the linker, and does come from any input file. This
3927 is used to build constructor and destructor tables when linking
3928 with -Ur. */
3930 static bool
3931 ecoff_reloc_link_order (bfd *output_bfd,
3932 struct bfd_link_info *info,
3933 asection *output_section,
3934 struct bfd_link_order *link_order)
3936 enum bfd_link_order_type type;
3937 asection *section;
3938 bfd_vma addend;
3939 arelent rel;
3940 struct internal_reloc in;
3941 bfd_size_type external_reloc_size;
3942 bfd_byte *rbuf;
3943 bool ok;
3944 file_ptr pos;
3946 type = link_order->type;
3947 section = NULL;
3948 addend = link_order->u.reloc.p->addend;
3950 /* We set up an arelent to pass to the backend adjust_reloc_out
3951 routine. */
3952 rel.address = link_order->offset;
3954 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
3955 if (rel.howto == 0)
3957 bfd_set_error (bfd_error_bad_value);
3958 return false;
3961 if (type == bfd_section_reloc_link_order)
3963 section = link_order->u.reloc.p->u.section;
3964 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
3966 else
3968 struct bfd_link_hash_entry *h;
3970 /* Treat a reloc against a defined symbol as though it were
3971 actually against the section. */
3972 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
3973 link_order->u.reloc.p->u.name,
3974 false, false, false);
3975 if (h != NULL
3976 && (h->type == bfd_link_hash_defined
3977 || h->type == bfd_link_hash_defweak))
3979 type = bfd_section_reloc_link_order;
3980 section = h->u.def.section->output_section;
3981 /* It seems that we ought to add the symbol value to the
3982 addend here, but in practice it has already been added
3983 because it was passed to constructor_callback. */
3984 addend += section->vma + h->u.def.section->output_offset;
3986 else
3988 /* We can't set up a reloc against a symbol correctly,
3989 because we have no asymbol structure. Currently no
3990 adjust_reloc_out routine cares. */
3991 rel.sym_ptr_ptr = NULL;
3995 /* All ECOFF relocs are in-place. Put the addend into the object
3996 file. */
3998 BFD_ASSERT (rel.howto->partial_inplace);
3999 if (addend != 0)
4001 bfd_size_type size;
4002 bfd_reloc_status_type rstat;
4003 bfd_byte *buf;
4005 size = bfd_get_reloc_size (rel.howto);
4006 buf = (bfd_byte *) bfd_zmalloc (size);
4007 if (buf == NULL && size != 0)
4008 return false;
4009 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4010 (bfd_vma) addend, buf);
4011 switch (rstat)
4013 case bfd_reloc_ok:
4014 break;
4015 default:
4016 case bfd_reloc_outofrange:
4017 abort ();
4018 case bfd_reloc_overflow:
4019 (*info->callbacks->reloc_overflow)
4020 (info, NULL,
4021 (link_order->type == bfd_section_reloc_link_order
4022 ? bfd_section_name (section)
4023 : link_order->u.reloc.p->u.name),
4024 rel.howto->name, addend, NULL, NULL, (bfd_vma) 0);
4025 break;
4027 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
4028 (file_ptr) link_order->offset, size);
4029 free (buf);
4030 if (! ok)
4031 return false;
4034 rel.addend = 0;
4036 /* Move the information into an internal_reloc structure. */
4037 in.r_vaddr = rel.address + bfd_section_vma (output_section);
4038 in.r_type = rel.howto->type;
4040 if (type == bfd_symbol_reloc_link_order)
4042 struct ecoff_link_hash_entry *h;
4044 h = ((struct ecoff_link_hash_entry *)
4045 bfd_wrapped_link_hash_lookup (output_bfd, info,
4046 link_order->u.reloc.p->u.name,
4047 false, false, true));
4048 if (h != NULL
4049 && h->indx != -1)
4050 in.r_symndx = h->indx;
4051 else
4053 (*info->callbacks->unattached_reloc)
4054 (info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
4055 in.r_symndx = 0;
4057 in.r_extern = 1;
4059 else
4061 const char *name;
4062 unsigned int i;
4063 static struct
4065 const char * name;
4066 long r_symndx;
4068 section_symndx [] =
4070 { _TEXT, RELOC_SECTION_TEXT },
4071 { _RDATA, RELOC_SECTION_RDATA },
4072 { _DATA, RELOC_SECTION_DATA },
4073 { _SDATA, RELOC_SECTION_SDATA },
4074 { _SBSS, RELOC_SECTION_SBSS },
4075 { _BSS, RELOC_SECTION_BSS },
4076 { _INIT, RELOC_SECTION_INIT },
4077 { _LIT8, RELOC_SECTION_LIT8 },
4078 { _LIT4, RELOC_SECTION_LIT4 },
4079 { _XDATA, RELOC_SECTION_XDATA },
4080 { _PDATA, RELOC_SECTION_PDATA },
4081 { _FINI, RELOC_SECTION_FINI },
4082 { _LITA, RELOC_SECTION_LITA },
4083 { "*ABS*", RELOC_SECTION_ABS },
4084 { _RCONST, RELOC_SECTION_RCONST }
4087 name = bfd_section_name (section);
4089 for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
4090 if (streq (name, section_symndx[i].name))
4092 in.r_symndx = section_symndx[i].r_symndx;
4093 break;
4096 if (i == ARRAY_SIZE (section_symndx))
4097 abort ();
4099 in.r_extern = 0;
4102 /* Let the BFD backend adjust the reloc. */
4103 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4105 /* Get some memory and swap out the reloc. */
4106 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4107 rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4108 if (rbuf == NULL)
4109 return false;
4111 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
4113 pos = (output_section->rel_filepos
4114 + output_section->reloc_count * external_reloc_size);
4115 ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4116 && (bfd_write (rbuf, external_reloc_size, output_bfd)
4117 == external_reloc_size));
4119 if (ok)
4120 ++output_section->reloc_count;
4122 free (rbuf);
4124 return ok;
4127 /* Put out information for an external symbol. These come only from
4128 the hash table. */
4130 static bool
4131 ecoff_link_write_external (struct bfd_hash_entry *bh, void * data)
4133 struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh;
4134 struct extsym_info *einfo = (struct extsym_info *) data;
4135 bfd *output_bfd = einfo->abfd;
4136 bool strip;
4138 if (h->root.type == bfd_link_hash_warning)
4140 h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4141 if (h->root.type == bfd_link_hash_new)
4142 return true;
4145 /* We need to check if this symbol is being stripped. */
4146 if (h->root.type == bfd_link_hash_undefined
4147 || h->root.type == bfd_link_hash_undefweak)
4148 strip = false;
4149 else if (einfo->info->strip == strip_all
4150 || (einfo->info->strip == strip_some
4151 && bfd_hash_lookup (einfo->info->keep_hash,
4152 h->root.root.string,
4153 false, false) == NULL))
4154 strip = true;
4155 else
4156 strip = false;
4158 if (strip || h->written)
4159 return true;
4161 if (h->abfd == NULL)
4163 h->esym.jmptbl = 0;
4164 h->esym.cobol_main = 0;
4165 h->esym.weakext = 0;
4166 h->esym.reserved = 0;
4167 h->esym.ifd = ifdNil;
4168 h->esym.asym.value = 0;
4169 h->esym.asym.st = stGlobal;
4171 if (h->root.type != bfd_link_hash_defined
4172 && h->root.type != bfd_link_hash_defweak)
4173 h->esym.asym.sc = scAbs;
4174 else
4176 asection *output_section;
4177 const char *name;
4178 unsigned int i;
4179 static struct
4181 const char * name;
4182 int sc;
4184 section_storage_classes [] =
4186 { _TEXT, scText },
4187 { _DATA, scData },
4188 { _SDATA, scSData },
4189 { _RDATA, scRData },
4190 { _BSS, scBss },
4191 { _SBSS, scSBss },
4192 { _INIT, scInit },
4193 { _FINI, scFini },
4194 { _PDATA, scPData },
4195 { _XDATA, scXData },
4196 { _RCONST, scRConst }
4199 output_section = h->root.u.def.section->output_section;
4200 name = bfd_section_name (output_section);
4202 for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++)
4203 if (streq (name, section_storage_classes[i].name))
4205 h->esym.asym.sc = section_storage_classes[i].sc;
4206 break;
4209 if (i == ARRAY_SIZE (section_storage_classes))
4210 h->esym.asym.sc = scAbs;
4213 h->esym.asym.reserved = 0;
4214 h->esym.asym.index = indexNil;
4216 else if (h->esym.ifd != -1)
4218 struct ecoff_debug_info *debug;
4220 /* Adjust the FDR index for the symbol by that used for the
4221 input BFD. */
4222 debug = &ecoff_data (h->abfd)->debug_info;
4223 BFD_ASSERT (h->esym.ifd >= 0
4224 && h->esym.ifd < debug->symbolic_header.ifdMax);
4225 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4228 switch (h->root.type)
4230 default:
4231 case bfd_link_hash_warning:
4232 case bfd_link_hash_new:
4233 abort ();
4234 case bfd_link_hash_undefined:
4235 case bfd_link_hash_undefweak:
4236 if (h->esym.asym.sc != scUndefined
4237 && h->esym.asym.sc != scSUndefined)
4238 h->esym.asym.sc = scUndefined;
4239 break;
4240 case bfd_link_hash_defined:
4241 case bfd_link_hash_defweak:
4242 if (h->esym.asym.sc == scUndefined
4243 || h->esym.asym.sc == scSUndefined)
4244 h->esym.asym.sc = scAbs;
4245 else if (h->esym.asym.sc == scCommon)
4246 h->esym.asym.sc = scBss;
4247 else if (h->esym.asym.sc == scSCommon)
4248 h->esym.asym.sc = scSBss;
4249 h->esym.asym.value = (h->root.u.def.value
4250 + h->root.u.def.section->output_section->vma
4251 + h->root.u.def.section->output_offset);
4252 break;
4253 case bfd_link_hash_common:
4254 if (h->esym.asym.sc != scCommon
4255 && h->esym.asym.sc != scSCommon)
4256 h->esym.asym.sc = scCommon;
4257 h->esym.asym.value = h->root.u.c.size;
4258 break;
4259 case bfd_link_hash_indirect:
4260 /* We ignore these symbols, since the indirected symbol is
4261 already in the hash table. */
4262 return true;
4265 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4266 symbol number. */
4267 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4268 h->written = 1;
4270 return (bfd_ecoff_debug_one_external
4271 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4272 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4273 &h->esym));
4276 /* ECOFF final link routine. This looks through all the input BFDs
4277 and gathers together all the debugging information, and then
4278 processes all the link order information. This may cause it to
4279 close and reopen some input BFDs; I'll see how bad this is. */
4281 bool
4282 _bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
4284 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4285 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4286 HDRR *symhdr;
4287 void * handle;
4288 bfd *input_bfd;
4289 asection *o;
4290 struct bfd_link_order *p;
4291 struct extsym_info einfo;
4293 /* We accumulate the debugging information counts in the symbolic
4294 header. */
4295 symhdr = &debug->symbolic_header;
4296 symhdr->vstamp = 0;
4297 symhdr->ilineMax = 0;
4298 symhdr->cbLine = 0;
4299 symhdr->idnMax = 0;
4300 symhdr->ipdMax = 0;
4301 symhdr->isymMax = 0;
4302 symhdr->ioptMax = 0;
4303 symhdr->iauxMax = 0;
4304 symhdr->issMax = 0;
4305 symhdr->issExtMax = 0;
4306 symhdr->ifdMax = 0;
4307 symhdr->crfd = 0;
4308 symhdr->iextMax = 0;
4310 /* We accumulate the debugging information itself in the debug_info
4311 structure. */
4312 debug->line = NULL;
4313 debug->external_dnr = NULL;
4314 debug->external_pdr = NULL;
4315 debug->external_sym = NULL;
4316 debug->external_opt = NULL;
4317 debug->external_aux = NULL;
4318 debug->ss = NULL;
4319 debug->ssext = debug->ssext_end = NULL;
4320 debug->external_fdr = NULL;
4321 debug->external_rfd = NULL;
4322 debug->external_ext = debug->external_ext_end = NULL;
4324 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4325 if (handle == NULL)
4326 return false;
4328 /* Accumulate the debugging symbols from each input BFD. */
4329 for (input_bfd = info->input_bfds;
4330 input_bfd != NULL;
4331 input_bfd = input_bfd->link.next)
4333 bool ret;
4335 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4337 /* Arbitrarily set the symbolic header vstamp to the vstamp
4338 of the first object file in the link. */
4339 if (symhdr->vstamp == 0)
4340 symhdr->vstamp
4341 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4342 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4343 handle);
4345 else
4346 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4347 debug, &backend->debug_swap,
4348 input_bfd, info);
4349 if (! ret)
4350 return false;
4352 /* Combine the register masks. */
4353 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4354 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4355 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4356 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4357 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4358 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4361 /* Write out the external symbols. */
4362 einfo.abfd = abfd;
4363 einfo.info = info;
4364 bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo);
4366 if (bfd_link_relocatable (info))
4368 /* We need to make a pass over the link_orders to count up the
4369 number of relocations we will need to output, so that we know
4370 how much space they will take up. */
4371 for (o = abfd->sections; o != NULL; o = o->next)
4373 o->reloc_count = 0;
4374 for (p = o->map_head.link_order;
4375 p != NULL;
4376 p = p->next)
4377 if (p->type == bfd_indirect_link_order)
4378 o->reloc_count += p->u.indirect.section->reloc_count;
4379 else if (p->type == bfd_section_reloc_link_order
4380 || p->type == bfd_symbol_reloc_link_order)
4381 ++o->reloc_count;
4385 /* Compute the reloc and symbol file positions. */
4386 ecoff_compute_reloc_file_positions (abfd);
4388 /* Write out the debugging information. */
4389 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4390 &backend->debug_swap, info,
4391 ecoff_data (abfd)->sym_filepos))
4392 return false;
4394 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4396 if (bfd_link_relocatable (info))
4398 /* Now reset the reloc_count field of the sections in the output
4399 BFD to 0, so that we can use them to keep track of how many
4400 relocs we have output thus far. */
4401 for (o = abfd->sections; o != NULL; o = o->next)
4402 o->reloc_count = 0;
4405 /* Get a value for the GP register. */
4406 if (ecoff_data (abfd)->gp == 0)
4408 struct bfd_link_hash_entry *h;
4410 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4411 if (h != NULL
4412 && h->type == bfd_link_hash_defined)
4413 ecoff_data (abfd)->gp = (h->u.def.value
4414 + h->u.def.section->output_section->vma
4415 + h->u.def.section->output_offset);
4416 else if (bfd_link_relocatable (info))
4418 bfd_vma lo;
4420 /* Make up a value. */
4421 lo = (bfd_vma) -1;
4422 for (o = abfd->sections; o != NULL; o = o->next)
4424 if (o->vma < lo
4425 && (streq (o->name, _SBSS)
4426 || streq (o->name, _SDATA)
4427 || streq (o->name, _LIT4)
4428 || streq (o->name, _LIT8)
4429 || streq (o->name, _LITA)))
4430 lo = o->vma;
4432 ecoff_data (abfd)->gp = lo + 0x8000;
4434 else
4436 /* If the relocate_section function needs to do a reloc
4437 involving the GP value, it should make a reloc_dangerous
4438 callback to warn that GP is not defined. */
4442 for (o = abfd->sections; o != NULL; o = o->next)
4444 for (p = o->map_head.link_order;
4445 p != NULL;
4446 p = p->next)
4448 if (p->type == bfd_indirect_link_order
4449 && (bfd_get_flavour (p->u.indirect.section->owner)
4450 == bfd_target_ecoff_flavour))
4452 if (! ecoff_indirect_link_order (abfd, info, o, p))
4453 return false;
4455 else if (p->type == bfd_section_reloc_link_order
4456 || p->type == bfd_symbol_reloc_link_order)
4458 if (! ecoff_reloc_link_order (abfd, info, o, p))
4459 return false;
4461 else
4463 if (! _bfd_default_link_order (abfd, info, o, p))
4464 return false;
4469 abfd->symcount = symhdr->iextMax + symhdr->isymMax;
4471 ecoff_data (abfd)->linker = true;
4473 return true;