* ldmain.c (main): Rename BufferSize to ld_bufsz because HPUX
[binutils.git] / bfd / ecoff.c
blob86a12228e7febf5a3f538e3a33e05ed032b37c52
1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Original version by Per Bothner.
5 Full support added by Ian Lance Taylor, ian@cygnus.com.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "aout/ar.h"
28 #include "aout/ranlib.h"
29 #include "aout/stab_gnu.h"
31 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32 some other stuff which we don't want and which conflicts with stuff
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"
47 /* Prototypes for static functions. */
49 static int ecoff_get_magic PARAMS ((bfd *abfd));
50 static long ecoff_sec_to_styp_flags PARAMS ((const char *name,
51 flagword flags));
52 static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *abfd));
53 static boolean ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
54 asymbol *asym, int ext, int weak));
55 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, FDR *fdr,
56 char *string,
57 RNDXR *rndx, long isym,
58 const char *which));
59 static char *ecoff_type_to_string PARAMS ((bfd *abfd, FDR *fdr,
60 unsigned int indx));
61 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
62 asymbol **symbols));
63 static int ecoff_sort_hdrs PARAMS ((const PTR, const PTR));
64 static boolean ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
65 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
66 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
67 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
68 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
69 unsigned int *rehash,
70 unsigned int size,
71 unsigned int hlog));
73 /* This stuff is somewhat copied from coffcode.h. */
75 static asection bfd_debug_section =
77 /* name, id, index, next, flags, user_set_vma, reloc_done, */
78 "*DEBUG*", 0, 0, NULL, 0, 0, 0,
79 /* linker_mark, linker_has_input, gc_mark, segment_mark, */
80 0, 0, 0, 0,
81 /* vma, lma, _cooked_size, _raw_size, */
82 0, 0, 0, 0,
83 /* output_offset, output_section, alignment_power, */
84 0, NULL, 0,
85 /* relocation, orelocation, reloc_count, filepos, rel_filepos, */
86 NULL, NULL, 0, 0, 0,
87 /* line_filepos, userdata, contents, lineno, lineno_count, */
88 0, NULL, NULL, NULL, 0,
89 /* entsize, comdat, kept_section, moving_line_filepos, */
90 0, NULL, NULL, 0,
91 /* target_index, used_by_bfd, constructor_chain, owner, */
92 0, NULL, NULL, NULL,
93 /* symbol, */
94 (struct symbol_cache_entry *) NULL,
95 /* symbol_ptr_ptr, */
96 (struct symbol_cache_entry **) NULL,
97 /* link_order_head, link_order_tail */
98 NULL, NULL
101 /* Create an ECOFF object. */
103 boolean
104 _bfd_ecoff_mkobject (abfd)
105 bfd *abfd;
107 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
108 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
109 if (abfd->tdata.ecoff_obj_data == NULL)
110 return false;
112 return true;
115 /* This is a hook called by coff_real_object_p to create any backend
116 specific information. */
119 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
120 bfd *abfd;
121 PTR filehdr;
122 PTR aouthdr;
124 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
125 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
126 ecoff_data_type *ecoff;
128 if (_bfd_ecoff_mkobject (abfd) == false)
129 return NULL;
131 ecoff = ecoff_data (abfd);
132 ecoff->gp_size = 8;
133 ecoff->sym_filepos = internal_f->f_symptr;
135 if (internal_a != (struct internal_aouthdr *) NULL)
137 int i;
139 ecoff->text_start = internal_a->text_start;
140 ecoff->text_end = internal_a->text_start + internal_a->tsize;
141 ecoff->gp = internal_a->gp_value;
142 ecoff->gprmask = internal_a->gprmask;
143 for (i = 0; i < 4; i++)
144 ecoff->cprmask[i] = internal_a->cprmask[i];
145 ecoff->fprmask = internal_a->fprmask;
146 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
147 abfd->flags |= D_PAGED;
148 else
149 abfd->flags &=~ D_PAGED;
152 /* It turns out that no special action is required by the MIPS or
153 Alpha ECOFF backends. They have different information in the
154 a.out header, but we just copy it all (e.g., gprmask, cprmask and
155 fprmask) and let the swapping routines ensure that only relevant
156 information is written out. */
158 return (PTR) ecoff;
161 /* Initialize a new section. */
163 boolean
164 _bfd_ecoff_new_section_hook (abfd, section)
165 bfd *abfd ATTRIBUTE_UNUSED;
166 asection *section;
168 section->alignment_power = 4;
170 if (strcmp (section->name, _TEXT) == 0
171 || strcmp (section->name, _INIT) == 0
172 || strcmp (section->name, _FINI) == 0)
173 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
174 else if (strcmp (section->name, _DATA) == 0
175 || strcmp (section->name, _SDATA) == 0)
176 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
177 else if (strcmp (section->name, _RDATA) == 0
178 || strcmp (section->name, _LIT8) == 0
179 || strcmp (section->name, _LIT4) == 0
180 || strcmp (section->name, _RCONST) == 0
181 || strcmp (section->name, _PDATA) == 0)
182 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
183 else if (strcmp (section->name, _BSS) == 0
184 || strcmp (section->name, _SBSS) == 0)
185 section->flags |= SEC_ALLOC;
186 else if (strcmp (section->name, _LIB) == 0)
188 /* An Irix 4 shared libary. */
189 section->flags |= SEC_COFF_SHARED_LIBRARY;
192 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
193 uncertain about .init on some systems and I don't know how shared
194 libraries work. */
196 return true;
199 /* Determine the machine architecture and type. This is called from
200 the generic COFF routines. It is the inverse of ecoff_get_magic,
201 below. This could be an ECOFF backend routine, with one version
202 for each target, but there aren't all that many ECOFF targets. */
204 boolean
205 _bfd_ecoff_set_arch_mach_hook (abfd, filehdr)
206 bfd *abfd;
207 PTR filehdr;
209 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
210 enum bfd_architecture arch;
211 unsigned long mach;
213 switch (internal_f->f_magic)
215 case MIPS_MAGIC_1:
216 case MIPS_MAGIC_LITTLE:
217 case MIPS_MAGIC_BIG:
218 arch = bfd_arch_mips;
219 mach = 3000;
220 break;
222 case MIPS_MAGIC_LITTLE2:
223 case MIPS_MAGIC_BIG2:
224 /* MIPS ISA level 2: the r6000 */
225 arch = bfd_arch_mips;
226 mach = 6000;
227 break;
229 case MIPS_MAGIC_LITTLE3:
230 case MIPS_MAGIC_BIG3:
231 /* MIPS ISA level 3: the r4000 */
232 arch = bfd_arch_mips;
233 mach = 4000;
234 break;
236 case ALPHA_MAGIC:
237 arch = bfd_arch_alpha;
238 mach = 0;
239 break;
241 default:
242 arch = bfd_arch_obscure;
243 mach = 0;
244 break;
247 return bfd_default_set_arch_mach (abfd, arch, mach);
250 /* Get the magic number to use based on the architecture and machine.
251 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
253 static int
254 ecoff_get_magic (abfd)
255 bfd *abfd;
257 int big, little;
259 switch (bfd_get_arch (abfd))
261 case bfd_arch_mips:
262 switch (bfd_get_mach (abfd))
264 default:
265 case 0:
266 case 3000:
267 big = MIPS_MAGIC_BIG;
268 little = MIPS_MAGIC_LITTLE;
269 break;
271 case 6000:
272 big = MIPS_MAGIC_BIG2;
273 little = MIPS_MAGIC_LITTLE2;
274 break;
276 case 4000:
277 big = MIPS_MAGIC_BIG3;
278 little = MIPS_MAGIC_LITTLE3;
279 break;
282 return bfd_big_endian (abfd) ? big : little;
284 case bfd_arch_alpha:
285 return ALPHA_MAGIC;
287 default:
288 abort ();
289 return 0;
293 /* Get the section s_flags to use for a section. */
295 static long
296 ecoff_sec_to_styp_flags (name, flags)
297 const char *name;
298 flagword flags;
300 long styp;
302 styp = 0;
304 if (strcmp (name, _TEXT) == 0)
305 styp = STYP_TEXT;
306 else if (strcmp (name, _DATA) == 0)
307 styp = STYP_DATA;
308 else if (strcmp (name, _SDATA) == 0)
309 styp = STYP_SDATA;
310 else if (strcmp (name, _RDATA) == 0)
311 styp = STYP_RDATA;
312 else if (strcmp (name, _LITA) == 0)
313 styp = STYP_LITA;
314 else if (strcmp (name, _LIT8) == 0)
315 styp = STYP_LIT8;
316 else if (strcmp (name, _LIT4) == 0)
317 styp = STYP_LIT4;
318 else if (strcmp (name, _BSS) == 0)
319 styp = STYP_BSS;
320 else if (strcmp (name, _SBSS) == 0)
321 styp = STYP_SBSS;
322 else if (strcmp (name, _INIT) == 0)
323 styp = STYP_ECOFF_INIT;
324 else if (strcmp (name, _FINI) == 0)
325 styp = STYP_ECOFF_FINI;
326 else if (strcmp (name, _PDATA) == 0)
327 styp = STYP_PDATA;
328 else if (strcmp (name, _XDATA) == 0)
329 styp = STYP_XDATA;
330 else if (strcmp (name, _LIB) == 0)
331 styp = STYP_ECOFF_LIB;
332 else if (strcmp (name, _GOT) == 0)
333 styp = STYP_GOT;
334 else if (strcmp (name, _HASH) == 0)
335 styp = STYP_HASH;
336 else if (strcmp (name, _DYNAMIC) == 0)
337 styp = STYP_DYNAMIC;
338 else if (strcmp (name, _LIBLIST) == 0)
339 styp = STYP_LIBLIST;
340 else if (strcmp (name, _RELDYN) == 0)
341 styp = STYP_RELDYN;
342 else if (strcmp (name, _CONFLIC) == 0)
343 styp = STYP_CONFLIC;
344 else if (strcmp (name, _DYNSTR) == 0)
345 styp = STYP_DYNSTR;
346 else if (strcmp (name, _DYNSYM) == 0)
347 styp = STYP_DYNSYM;
348 else if (strcmp (name, _COMMENT) == 0)
350 styp = STYP_COMMENT;
351 flags &=~ SEC_NEVER_LOAD;
353 else if (strcmp (name, _RCONST) == 0)
354 styp = STYP_RCONST;
355 else if (flags & SEC_CODE)
356 styp = STYP_TEXT;
357 else if (flags & SEC_DATA)
358 styp = STYP_DATA;
359 else if (flags & SEC_READONLY)
360 styp = STYP_RDATA;
361 else if (flags & SEC_LOAD)
362 styp = STYP_REG;
363 else
364 styp = STYP_BSS;
366 if (flags & SEC_NEVER_LOAD)
367 styp |= STYP_NOLOAD;
369 return styp;
372 /* Get the BFD flags to use for a section. */
374 boolean
375 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name, section, flags_ptr)
376 bfd *abfd ATTRIBUTE_UNUSED;
377 PTR hdr;
378 const char *name ATTRIBUTE_UNUSED;
379 asection *section ATTRIBUTE_UNUSED;
380 flagword * flags_ptr;
382 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
383 long styp_flags = internal_s->s_flags;
384 flagword sec_flags = 0;
386 if (styp_flags & STYP_NOLOAD)
387 sec_flags |= SEC_NEVER_LOAD;
389 /* For 386 COFF, at least, an unloadable text or data section is
390 actually a shared library section. */
391 if ((styp_flags & STYP_TEXT)
392 || (styp_flags & STYP_ECOFF_INIT)
393 || (styp_flags & STYP_ECOFF_FINI)
394 || (styp_flags & STYP_DYNAMIC)
395 || (styp_flags & STYP_LIBLIST)
396 || (styp_flags & STYP_RELDYN)
397 || styp_flags == STYP_CONFLIC
398 || (styp_flags & STYP_DYNSTR)
399 || (styp_flags & STYP_DYNSYM)
400 || (styp_flags & STYP_HASH))
402 if (sec_flags & SEC_NEVER_LOAD)
403 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
404 else
405 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
407 else if ((styp_flags & STYP_DATA)
408 || (styp_flags & STYP_RDATA)
409 || (styp_flags & STYP_SDATA)
410 || styp_flags == STYP_PDATA
411 || styp_flags == STYP_XDATA
412 || (styp_flags & STYP_GOT)
413 || styp_flags == STYP_RCONST)
415 if (sec_flags & SEC_NEVER_LOAD)
416 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
417 else
418 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
419 if ((styp_flags & STYP_RDATA)
420 || styp_flags == STYP_PDATA
421 || styp_flags == STYP_RCONST)
422 sec_flags |= SEC_READONLY;
424 else if ((styp_flags & STYP_BSS)
425 || (styp_flags & STYP_SBSS))
426 sec_flags |= SEC_ALLOC;
427 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
428 sec_flags |= SEC_NEVER_LOAD;
429 else if ((styp_flags & STYP_LITA)
430 || (styp_flags & STYP_LIT8)
431 || (styp_flags & STYP_LIT4))
432 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
433 else if (styp_flags & STYP_ECOFF_LIB)
434 sec_flags |= SEC_COFF_SHARED_LIBRARY;
435 else
436 sec_flags |= SEC_ALLOC | SEC_LOAD;
438 * flags_ptr = sec_flags;
439 return true;
442 /* Read in the symbolic header for an ECOFF object file. */
444 static boolean
445 ecoff_slurp_symbolic_header (abfd)
446 bfd *abfd;
448 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
449 bfd_size_type external_hdr_size;
450 PTR raw = NULL;
451 HDRR *internal_symhdr;
453 /* See if we've already read it in. */
454 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
455 backend->debug_swap.sym_magic)
456 return true;
458 /* See whether there is a symbolic header. */
459 if (ecoff_data (abfd)->sym_filepos == 0)
461 bfd_get_symcount (abfd) = 0;
462 return true;
465 /* At this point bfd_get_symcount (abfd) holds the number of symbols
466 as read from the file header, but on ECOFF this is always the
467 size of the symbolic information header. It would be cleaner to
468 handle this when we first read the file in coffgen.c. */
469 external_hdr_size = backend->debug_swap.external_hdr_size;
470 if (bfd_get_symcount (abfd) != external_hdr_size)
472 bfd_set_error (bfd_error_bad_value);
473 return false;
476 /* Read the symbolic information header. */
477 raw = (PTR) bfd_malloc ((size_t) external_hdr_size);
478 if (raw == NULL)
479 goto error_return;
481 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
482 || (bfd_read (raw, external_hdr_size, 1, abfd)
483 != external_hdr_size))
484 goto error_return;
485 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
486 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
488 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
490 bfd_set_error (bfd_error_bad_value);
491 goto error_return;
494 /* Now we can get the correct number of symbols. */
495 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
496 + internal_symhdr->iextMax);
498 if (raw != NULL)
499 free (raw);
500 return true;
501 error_return:
502 if (raw != NULL)
503 free (raw);
504 return false;
507 /* Read in and swap the important symbolic information for an ECOFF
508 object file. This is called by gdb via the read_debug_info entry
509 point in the backend structure. */
511 boolean
512 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
513 bfd *abfd;
514 asection *ignore ATTRIBUTE_UNUSED;
515 struct ecoff_debug_info *debug;
517 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
518 HDRR *internal_symhdr;
519 bfd_size_type raw_base;
520 bfd_size_type raw_size;
521 PTR raw;
522 bfd_size_type external_fdr_size;
523 char *fraw_src;
524 char *fraw_end;
525 struct fdr *fdr_ptr;
526 bfd_size_type raw_end;
527 bfd_size_type cb_end;
529 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
531 /* Check whether we've already gotten it, and whether there's any to
532 get. */
533 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
534 return true;
535 if (ecoff_data (abfd)->sym_filepos == 0)
537 bfd_get_symcount (abfd) = 0;
538 return true;
541 if (! ecoff_slurp_symbolic_header (abfd))
542 return false;
544 internal_symhdr = &debug->symbolic_header;
546 /* Read all the symbolic information at once. */
547 raw_base = (ecoff_data (abfd)->sym_filepos
548 + backend->debug_swap.external_hdr_size);
550 /* Alpha ecoff makes the determination of raw_size difficult. It has
551 an undocumented debug data section between the symhdr and the first
552 documented section. And the ordering of the sections varies between
553 statically and dynamically linked executables.
554 If bfd supports SEEK_END someday, this code could be simplified. */
556 raw_end = 0;
558 #define UPDATE_RAW_END(start, count, size) \
559 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
560 if (cb_end > raw_end) \
561 raw_end = cb_end
563 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
564 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
565 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
566 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
567 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
568 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
569 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
570 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
571 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
572 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
573 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
575 #undef UPDATE_RAW_END
577 raw_size = raw_end - raw_base;
578 if (raw_size == 0)
580 ecoff_data (abfd)->sym_filepos = 0;
581 return true;
583 raw = (PTR) bfd_alloc (abfd, raw_size);
584 if (raw == NULL)
585 return false;
586 if (bfd_seek (abfd,
587 (ecoff_data (abfd)->sym_filepos
588 + backend->debug_swap.external_hdr_size),
589 SEEK_SET) != 0
590 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
592 bfd_release (abfd, raw);
593 return false;
596 ecoff_data (abfd)->raw_syments = raw;
598 /* Get pointers for the numeric offsets in the HDRR structure. */
599 #define FIX(off1, off2, type) \
600 if (internal_symhdr->off1 == 0) \
601 debug->off2 = (type) NULL; \
602 else \
603 debug->off2 = (type) ((char *) raw \
604 + (internal_symhdr->off1 \
605 - raw_base))
606 FIX (cbLineOffset, line, unsigned char *);
607 FIX (cbDnOffset, external_dnr, PTR);
608 FIX (cbPdOffset, external_pdr, PTR);
609 FIX (cbSymOffset, external_sym, PTR);
610 FIX (cbOptOffset, external_opt, PTR);
611 FIX (cbAuxOffset, external_aux, union aux_ext *);
612 FIX (cbSsOffset, ss, char *);
613 FIX (cbSsExtOffset, ssext, char *);
614 FIX (cbFdOffset, external_fdr, PTR);
615 FIX (cbRfdOffset, external_rfd, PTR);
616 FIX (cbExtOffset, external_ext, PTR);
617 #undef FIX
619 /* I don't want to always swap all the data, because it will just
620 waste time and most programs will never look at it. The only
621 time the linker needs most of the debugging information swapped
622 is when linking big-endian and little-endian MIPS object files
623 together, which is not a common occurrence.
625 We need to look at the fdr to deal with a lot of information in
626 the symbols, so we swap them here. */
627 debug->fdr = (struct fdr *) bfd_alloc (abfd,
628 (internal_symhdr->ifdMax *
629 sizeof (struct fdr)));
630 if (debug->fdr == NULL)
631 return false;
632 external_fdr_size = backend->debug_swap.external_fdr_size;
633 fdr_ptr = debug->fdr;
634 fraw_src = (char *) debug->external_fdr;
635 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
636 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
637 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
639 return true;
642 /* ECOFF symbol table routines. The ECOFF symbol table is described
643 in gcc/mips-tfile.c. */
645 /* ECOFF uses two common sections. One is the usual one, and the
646 other is for small objects. All the small objects are kept
647 together, and then referenced via the gp pointer, which yields
648 faster assembler code. This is what we use for the small common
649 section. */
650 static asection ecoff_scom_section;
651 static asymbol ecoff_scom_symbol;
652 static asymbol *ecoff_scom_symbol_ptr;
654 /* Create an empty symbol. */
656 asymbol *
657 _bfd_ecoff_make_empty_symbol (abfd)
658 bfd *abfd;
660 ecoff_symbol_type *new;
662 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
663 if (new == (ecoff_symbol_type *) NULL)
664 return (asymbol *) NULL;
665 memset ((PTR) new, 0, sizeof *new);
666 new->symbol.section = (asection *) NULL;
667 new->fdr = (FDR *) NULL;
668 new->local = false;
669 new->native = NULL;
670 new->symbol.the_bfd = abfd;
671 return &new->symbol;
674 /* Set the BFD flags and section for an ECOFF symbol. */
676 static boolean
677 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, weak)
678 bfd *abfd;
679 SYMR *ecoff_sym;
680 asymbol *asym;
681 int ext;
682 int weak;
684 asym->the_bfd = abfd;
685 asym->value = ecoff_sym->value;
686 asym->section = &bfd_debug_section;
687 asym->udata.i = 0;
689 /* Most symbol types are just for debugging. */
690 switch (ecoff_sym->st)
692 case stGlobal:
693 case stStatic:
694 case stLabel:
695 case stProc:
696 case stStaticProc:
697 break;
698 case stNil:
699 if (ECOFF_IS_STAB (ecoff_sym))
701 asym->flags = BSF_DEBUGGING;
702 return true;
704 break;
705 default:
706 asym->flags = BSF_DEBUGGING;
707 return true;
710 if (weak)
711 asym->flags = BSF_EXPORT | BSF_WEAK;
712 else if (ext)
713 asym->flags = BSF_EXPORT | BSF_GLOBAL;
714 else
716 asym->flags = BSF_LOCAL;
717 /* Normally, a local stProc symbol will have a corresponding
718 external symbol. We mark the local symbol as a debugging
719 symbol, in order to prevent nm from printing both out.
720 Similarly, we mark stLabel and stabs symbols as debugging
721 symbols. In both cases, we do want to set the value
722 correctly based on the symbol class. */
723 if (ecoff_sym->st == stProc
724 || ecoff_sym->st == stLabel
725 || ECOFF_IS_STAB (ecoff_sym))
726 asym->flags |= BSF_DEBUGGING;
728 switch (ecoff_sym->sc)
730 case scNil:
731 /* Used for compiler generated labels. Leave them in the
732 debugging section, and mark them as local. If BSF_DEBUGGING
733 is set, then nm does not display them for some reason. If no
734 flags are set then the linker whines about them. */
735 asym->flags = BSF_LOCAL;
736 break;
737 case scText:
738 asym->section = bfd_make_section_old_way (abfd, ".text");
739 asym->value -= asym->section->vma;
740 break;
741 case scData:
742 asym->section = bfd_make_section_old_way (abfd, ".data");
743 asym->value -= asym->section->vma;
744 break;
745 case scBss:
746 asym->section = bfd_make_section_old_way (abfd, ".bss");
747 asym->value -= asym->section->vma;
748 break;
749 case scRegister:
750 asym->flags = BSF_DEBUGGING;
751 break;
752 case scAbs:
753 asym->section = bfd_abs_section_ptr;
754 break;
755 case scUndefined:
756 asym->section = bfd_und_section_ptr;
757 asym->flags = 0;
758 asym->value = 0;
759 break;
760 case scCdbLocal:
761 case scBits:
762 case scCdbSystem:
763 case scRegImage:
764 case scInfo:
765 case scUserStruct:
766 asym->flags = BSF_DEBUGGING;
767 break;
768 case scSData:
769 asym->section = bfd_make_section_old_way (abfd, ".sdata");
770 asym->value -= asym->section->vma;
771 break;
772 case scSBss:
773 asym->section = bfd_make_section_old_way (abfd, ".sbss");
774 asym->value -= asym->section->vma;
775 break;
776 case scRData:
777 asym->section = bfd_make_section_old_way (abfd, ".rdata");
778 asym->value -= asym->section->vma;
779 break;
780 case scVar:
781 asym->flags = BSF_DEBUGGING;
782 break;
783 case scCommon:
784 if (asym->value > ecoff_data (abfd)->gp_size)
786 asym->section = bfd_com_section_ptr;
787 asym->flags = 0;
788 break;
790 /* Fall through. */
791 case scSCommon:
792 if (ecoff_scom_section.name == NULL)
794 /* Initialize the small common section. */
795 ecoff_scom_section.name = SCOMMON;
796 ecoff_scom_section.flags = SEC_IS_COMMON;
797 ecoff_scom_section.output_section = &ecoff_scom_section;
798 ecoff_scom_section.symbol = &ecoff_scom_symbol;
799 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
800 ecoff_scom_symbol.name = SCOMMON;
801 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
802 ecoff_scom_symbol.section = &ecoff_scom_section;
803 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
805 asym->section = &ecoff_scom_section;
806 asym->flags = 0;
807 break;
808 case scVarRegister:
809 case scVariant:
810 asym->flags = BSF_DEBUGGING;
811 break;
812 case scSUndefined:
813 asym->section = bfd_und_section_ptr;
814 asym->flags = 0;
815 asym->value = 0;
816 break;
817 case scInit:
818 asym->section = bfd_make_section_old_way (abfd, ".init");
819 asym->value -= asym->section->vma;
820 break;
821 case scBasedVar:
822 case scXData:
823 case scPData:
824 asym->flags = BSF_DEBUGGING;
825 break;
826 case scFini:
827 asym->section = bfd_make_section_old_way (abfd, ".fini");
828 asym->value -= asym->section->vma;
829 break;
830 case scRConst:
831 asym->section = bfd_make_section_old_way (abfd, ".rconst");
832 asym->value -= asym->section->vma;
833 break;
834 default:
835 break;
838 /* Look for special constructors symbols and make relocation entries
839 in a special construction section. These are produced by the
840 -fgnu-linker argument to g++. */
841 if (ECOFF_IS_STAB (ecoff_sym))
843 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
845 default:
846 break;
848 case N_SETA:
849 case N_SETT:
850 case N_SETD:
851 case N_SETB:
853 /* This code is no longer needed. It used to be used to
854 make the linker handle set symbols, but they are now
855 handled in the add_symbols routine instead. */
856 #if 0
857 const char *name;
858 asection *section;
859 arelent_chain *reloc_chain;
860 unsigned int bitsize;
862 /* Get a section with the same name as the symbol (usually
863 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
864 name ___CTOR_LIST (three underscores). We need
865 __CTOR_LIST (two underscores), since ECOFF doesn't use
866 a leading underscore. This should be handled by gcc,
867 but instead we do it here. Actually, this should all
868 be done differently anyhow. */
869 name = bfd_asymbol_name (asym);
870 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
872 ++name;
873 asym->name = name;
875 section = bfd_get_section_by_name (abfd, name);
876 if (section == (asection *) NULL)
878 char *copy;
880 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
881 if (!copy)
882 return false;
883 strcpy (copy, name);
884 section = bfd_make_section (abfd, copy);
887 /* Build a reloc pointing to this constructor. */
888 reloc_chain =
889 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
890 if (!reloc_chain)
891 return false;
892 reloc_chain->relent.sym_ptr_ptr =
893 bfd_get_section (asym)->symbol_ptr_ptr;
894 reloc_chain->relent.address = section->_raw_size;
895 reloc_chain->relent.addend = asym->value;
896 reloc_chain->relent.howto =
897 ecoff_backend (abfd)->constructor_reloc;
899 /* Set up the constructor section to hold the reloc. */
900 section->flags = SEC_CONSTRUCTOR;
901 ++section->reloc_count;
903 /* Constructor sections must be rounded to a boundary
904 based on the bitsize. These are not real sections--
905 they are handled specially by the linker--so the ECOFF
906 16 byte alignment restriction does not apply. */
907 bitsize = ecoff_backend (abfd)->constructor_bitsize;
908 section->alignment_power = 1;
909 while ((1 << section->alignment_power) < bitsize / 8)
910 ++section->alignment_power;
912 reloc_chain->next = section->constructor_chain;
913 section->constructor_chain = reloc_chain;
914 section->_raw_size += bitsize / 8;
916 #endif /* 0 */
918 /* Mark the symbol as a constructor. */
919 asym->flags |= BSF_CONSTRUCTOR;
921 break;
924 return true;
927 /* Read an ECOFF symbol table. */
929 boolean
930 _bfd_ecoff_slurp_symbol_table (abfd)
931 bfd *abfd;
933 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
934 const bfd_size_type external_ext_size
935 = backend->debug_swap.external_ext_size;
936 const bfd_size_type external_sym_size
937 = backend->debug_swap.external_sym_size;
938 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
939 = backend->debug_swap.swap_ext_in;
940 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
941 = backend->debug_swap.swap_sym_in;
942 bfd_size_type internal_size;
943 ecoff_symbol_type *internal;
944 ecoff_symbol_type *internal_ptr;
945 char *eraw_src;
946 char *eraw_end;
947 FDR *fdr_ptr;
948 FDR *fdr_end;
950 /* If we've already read in the symbol table, do nothing. */
951 if (ecoff_data (abfd)->canonical_symbols != NULL)
952 return true;
954 /* Get the symbolic information. */
955 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
956 &ecoff_data (abfd)->debug_info))
957 return false;
958 if (bfd_get_symcount (abfd) == 0)
959 return true;
961 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
962 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
963 if (internal == NULL)
964 return false;
966 internal_ptr = internal;
967 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
968 eraw_end = (eraw_src
969 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
970 * external_ext_size));
971 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
973 EXTR internal_esym;
975 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
976 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
977 + internal_esym.asym.iss);
978 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
979 &internal_ptr->symbol, 1,
980 internal_esym.weakext))
981 return false;
982 /* The alpha uses a negative ifd field for section symbols. */
983 if (internal_esym.ifd >= 0)
984 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
985 + internal_esym.ifd);
986 else
987 internal_ptr->fdr = NULL;
988 internal_ptr->local = false;
989 internal_ptr->native = (PTR) eraw_src;
992 /* The local symbols must be accessed via the fdr's, because the
993 string and aux indices are relative to the fdr information. */
994 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
995 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
996 for (; fdr_ptr < fdr_end; fdr_ptr++)
998 char *lraw_src;
999 char *lraw_end;
1001 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
1002 + fdr_ptr->isymBase * external_sym_size);
1003 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1004 for (;
1005 lraw_src < lraw_end;
1006 lraw_src += external_sym_size, internal_ptr++)
1008 SYMR internal_sym;
1010 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1011 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
1012 + fdr_ptr->issBase
1013 + internal_sym.iss);
1014 if (!ecoff_set_symbol_info (abfd, &internal_sym,
1015 &internal_ptr->symbol, 0, 0))
1016 return false;
1017 internal_ptr->fdr = fdr_ptr;
1018 internal_ptr->local = true;
1019 internal_ptr->native = (PTR) lraw_src;
1023 ecoff_data (abfd)->canonical_symbols = internal;
1025 return true;
1028 /* Return the amount of space needed for the canonical symbols. */
1030 long
1031 _bfd_ecoff_get_symtab_upper_bound (abfd)
1032 bfd *abfd;
1034 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
1035 &ecoff_data (abfd)->debug_info))
1036 return -1;
1038 if (bfd_get_symcount (abfd) == 0)
1039 return 0;
1041 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1044 /* Get the canonical symbols. */
1046 long
1047 _bfd_ecoff_get_symtab (abfd, alocation)
1048 bfd *abfd;
1049 asymbol **alocation;
1051 unsigned int counter = 0;
1052 ecoff_symbol_type *symbase;
1053 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1055 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1056 return -1;
1057 if (bfd_get_symcount (abfd) == 0)
1058 return 0;
1060 symbase = ecoff_data (abfd)->canonical_symbols;
1061 while (counter < bfd_get_symcount (abfd))
1063 *(location++) = symbase++;
1064 counter++;
1066 *location++ = (ecoff_symbol_type *) NULL;
1067 return bfd_get_symcount (abfd);
1070 /* Turn ECOFF type information into a printable string.
1071 ecoff_emit_aggregate and ecoff_type_to_string are from
1072 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1074 /* Write aggregate information to a string. */
1076 static void
1077 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1078 bfd *abfd;
1079 FDR *fdr;
1080 char *string;
1081 RNDXR *rndx;
1082 long isym;
1083 const char *which;
1085 const struct ecoff_debug_swap * const debug_swap =
1086 &ecoff_backend (abfd)->debug_swap;
1087 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1088 unsigned int ifd = rndx->rfd;
1089 unsigned int indx = rndx->index;
1090 const char *name;
1092 if (ifd == 0xfff)
1093 ifd = isym;
1095 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1096 struct return type of a procedure compiled without -g. */
1097 if (ifd == 0xffffffff
1098 || (rndx->rfd == 0xfff && indx == 0))
1099 name = "<undefined>";
1100 else if (indx == indexNil)
1101 name = "<no name>";
1102 else
1104 SYMR sym;
1106 if (debug_info->external_rfd == NULL)
1107 fdr = debug_info->fdr + ifd;
1108 else
1110 RFDT rfd;
1112 (*debug_swap->swap_rfd_in) (abfd,
1113 ((char *) debug_info->external_rfd
1114 + ((fdr->rfdBase + ifd)
1115 * debug_swap->external_rfd_size)),
1116 &rfd);
1117 fdr = debug_info->fdr + rfd;
1120 indx += fdr->isymBase;
1122 (*debug_swap->swap_sym_in) (abfd,
1123 ((char *) debug_info->external_sym
1124 + indx * debug_swap->external_sym_size),
1125 &sym);
1127 name = debug_info->ss + fdr->issBase + sym.iss;
1130 sprintf (string,
1131 "%s %s { ifd = %u, index = %lu }",
1132 which, name, ifd,
1133 ((long) indx
1134 + debug_info->symbolic_header.iextMax));
1137 /* Convert the type information to string format. */
1139 static char *
1140 ecoff_type_to_string (abfd, fdr, indx)
1141 bfd *abfd;
1142 FDR *fdr;
1143 unsigned int indx;
1145 union aux_ext *aux_ptr;
1146 int bigendian;
1147 AUXU u;
1148 struct qual {
1149 unsigned int type;
1150 int low_bound;
1151 int high_bound;
1152 int stride;
1153 } qualifiers[7];
1154 unsigned int basic_type;
1155 int i;
1156 char buffer1[1024];
1157 static char buffer2[1024];
1158 char *p1 = buffer1;
1159 char *p2 = buffer2;
1160 RNDXR rndx;
1162 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1163 bigendian = fdr->fBigendian;
1165 for (i = 0; i < 7; i++)
1167 qualifiers[i].low_bound = 0;
1168 qualifiers[i].high_bound = 0;
1169 qualifiers[i].stride = 0;
1172 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1173 return "-1 (no type)";
1174 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1176 basic_type = u.ti.bt;
1177 qualifiers[0].type = u.ti.tq0;
1178 qualifiers[1].type = u.ti.tq1;
1179 qualifiers[2].type = u.ti.tq2;
1180 qualifiers[3].type = u.ti.tq3;
1181 qualifiers[4].type = u.ti.tq4;
1182 qualifiers[5].type = u.ti.tq5;
1183 qualifiers[6].type = tqNil;
1186 * Go get the basic type.
1188 switch (basic_type)
1190 case btNil: /* undefined */
1191 strcpy (p1, "nil");
1192 break;
1194 case btAdr: /* address - integer same size as pointer */
1195 strcpy (p1, "address");
1196 break;
1198 case btChar: /* character */
1199 strcpy (p1, "char");
1200 break;
1202 case btUChar: /* unsigned character */
1203 strcpy (p1, "unsigned char");
1204 break;
1206 case btShort: /* short */
1207 strcpy (p1, "short");
1208 break;
1210 case btUShort: /* unsigned short */
1211 strcpy (p1, "unsigned short");
1212 break;
1214 case btInt: /* int */
1215 strcpy (p1, "int");
1216 break;
1218 case btUInt: /* unsigned int */
1219 strcpy (p1, "unsigned int");
1220 break;
1222 case btLong: /* long */
1223 strcpy (p1, "long");
1224 break;
1226 case btULong: /* unsigned long */
1227 strcpy (p1, "unsigned long");
1228 break;
1230 case btFloat: /* float (real) */
1231 strcpy (p1, "float");
1232 break;
1234 case btDouble: /* Double (real) */
1235 strcpy (p1, "double");
1236 break;
1238 /* Structures add 1-2 aux words:
1239 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1240 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1242 case btStruct: /* Structure (Record) */
1243 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1244 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1245 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1246 "struct");
1247 indx++; /* skip aux words */
1248 break;
1250 /* Unions add 1-2 aux words:
1251 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1252 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1254 case btUnion: /* Union */
1255 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1256 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1257 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1258 "union");
1259 indx++; /* skip aux words */
1260 break;
1262 /* Enumerations add 1-2 aux words:
1263 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1264 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1266 case btEnum: /* Enumeration */
1267 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1268 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1269 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1270 "enum");
1271 indx++; /* skip aux words */
1272 break;
1274 case btTypedef: /* defined via a typedef, isymRef points */
1275 strcpy (p1, "typedef");
1276 break;
1278 case btRange: /* subrange of int */
1279 strcpy (p1, "subrange");
1280 break;
1282 case btSet: /* pascal sets */
1283 strcpy (p1, "set");
1284 break;
1286 case btComplex: /* fortran complex */
1287 strcpy (p1, "complex");
1288 break;
1290 case btDComplex: /* fortran double complex */
1291 strcpy (p1, "double complex");
1292 break;
1294 case btIndirect: /* forward or unnamed typedef */
1295 strcpy (p1, "forward/unamed typedef");
1296 break;
1298 case btFixedDec: /* Fixed Decimal */
1299 strcpy (p1, "fixed decimal");
1300 break;
1302 case btFloatDec: /* Float Decimal */
1303 strcpy (p1, "float decimal");
1304 break;
1306 case btString: /* Varying Length Character String */
1307 strcpy (p1, "string");
1308 break;
1310 case btBit: /* Aligned Bit String */
1311 strcpy (p1, "bit");
1312 break;
1314 case btPicture: /* Picture */
1315 strcpy (p1, "picture");
1316 break;
1318 case btVoid: /* Void */
1319 strcpy (p1, "void");
1320 break;
1322 default:
1323 sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
1324 break;
1327 p1 += strlen (buffer1);
1330 * If this is a bitfield, get the bitsize.
1332 if (u.ti.fBitfield)
1334 int bitsize;
1336 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1337 sprintf (p1, " : %d", bitsize);
1338 p1 += strlen (buffer1);
1342 * Deal with any qualifiers.
1344 if (qualifiers[0].type != tqNil)
1347 * Snarf up any array bounds in the correct order. Arrays
1348 * store 5 successive words in the aux. table:
1349 * word 0 RNDXR to type of the bounds (ie, int)
1350 * word 1 Current file descriptor index
1351 * word 2 low bound
1352 * word 3 high bound (or -1 if [])
1353 * word 4 stride size in bits
1355 for (i = 0; i < 7; i++)
1357 if (qualifiers[i].type == tqArray)
1359 qualifiers[i].low_bound =
1360 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1361 qualifiers[i].high_bound =
1362 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1363 qualifiers[i].stride =
1364 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1365 indx += 5;
1370 * Now print out the qualifiers.
1372 for (i = 0; i < 6; i++)
1374 switch (qualifiers[i].type)
1376 case tqNil:
1377 case tqMax:
1378 break;
1380 case tqPtr:
1381 strcpy (p2, "ptr to ");
1382 p2 += sizeof ("ptr to ")-1;
1383 break;
1385 case tqVol:
1386 strcpy (p2, "volatile ");
1387 p2 += sizeof ("volatile ")-1;
1388 break;
1390 case tqFar:
1391 strcpy (p2, "far ");
1392 p2 += sizeof ("far ")-1;
1393 break;
1395 case tqProc:
1396 strcpy (p2, "func. ret. ");
1397 p2 += sizeof ("func. ret. ");
1398 break;
1400 case tqArray:
1402 int first_array = i;
1403 int j;
1405 /* Print array bounds reversed (ie, in the order the C
1406 programmer writes them). C is such a fun language.... */
1408 while (i < 5 && qualifiers[i+1].type == tqArray)
1409 i++;
1411 for (j = i; j >= first_array; j--)
1413 strcpy (p2, "array [");
1414 p2 += sizeof ("array [")-1;
1415 if (qualifiers[j].low_bound != 0)
1416 sprintf (p2,
1417 "%ld:%ld {%ld bits}",
1418 (long) qualifiers[j].low_bound,
1419 (long) qualifiers[j].high_bound,
1420 (long) qualifiers[j].stride);
1422 else if (qualifiers[j].high_bound != -1)
1423 sprintf (p2,
1424 "%ld {%ld bits}",
1425 (long) (qualifiers[j].high_bound + 1),
1426 (long) (qualifiers[j].stride));
1428 else
1429 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1431 p2 += strlen (p2);
1432 strcpy (p2, "] of ");
1433 p2 += sizeof ("] of ")-1;
1436 break;
1441 strcpy (p2, buffer1);
1442 return buffer2;
1445 /* Return information about ECOFF symbol SYMBOL in RET. */
1447 void
1448 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1449 bfd *abfd ATTRIBUTE_UNUSED;
1450 asymbol *symbol;
1451 symbol_info *ret;
1453 bfd_symbol_info (symbol, ret);
1456 /* Return whether this is a local label. */
1458 boolean
1459 _bfd_ecoff_bfd_is_local_label_name (abfd, name)
1460 bfd *abfd ATTRIBUTE_UNUSED;
1461 const char *name;
1463 return name[0] == '$';
1466 /* Print information about an ECOFF symbol. */
1468 void
1469 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1470 bfd *abfd;
1471 PTR filep;
1472 asymbol *symbol;
1473 bfd_print_symbol_type how;
1475 const struct ecoff_debug_swap * const debug_swap
1476 = &ecoff_backend (abfd)->debug_swap;
1477 FILE *file = (FILE *)filep;
1479 switch (how)
1481 case bfd_print_symbol_name:
1482 fprintf (file, "%s", symbol->name);
1483 break;
1484 case bfd_print_symbol_more:
1485 if (ecoffsymbol (symbol)->local)
1487 SYMR ecoff_sym;
1489 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1490 &ecoff_sym);
1491 fprintf (file, "ecoff local ");
1492 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1493 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1494 (unsigned) ecoff_sym.sc);
1496 else
1498 EXTR ecoff_ext;
1500 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1501 &ecoff_ext);
1502 fprintf (file, "ecoff extern ");
1503 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1504 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1505 (unsigned) ecoff_ext.asym.sc);
1507 break;
1508 case bfd_print_symbol_all:
1509 /* Print out the symbols in a reasonable way */
1511 char type;
1512 int pos;
1513 EXTR ecoff_ext;
1514 char jmptbl;
1515 char cobol_main;
1516 char weakext;
1518 if (ecoffsymbol (symbol)->local)
1520 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1521 &ecoff_ext.asym);
1522 type = 'l';
1523 pos = ((((char *) ecoffsymbol (symbol)->native
1524 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1525 / debug_swap->external_sym_size)
1526 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1527 jmptbl = ' ';
1528 cobol_main = ' ';
1529 weakext = ' ';
1531 else
1533 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1534 &ecoff_ext);
1535 type = 'e';
1536 pos = (((char *) ecoffsymbol (symbol)->native
1537 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1538 / debug_swap->external_ext_size);
1539 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1540 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1541 weakext = ecoff_ext.weakext ? 'w' : ' ';
1544 fprintf (file, "[%3d] %c ",
1545 pos, type);
1546 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1547 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1548 (unsigned) ecoff_ext.asym.st,
1549 (unsigned) ecoff_ext.asym.sc,
1550 (unsigned) ecoff_ext.asym.index,
1551 jmptbl, cobol_main, weakext,
1552 symbol->name);
1554 if (ecoffsymbol (symbol)->fdr != NULL
1555 && ecoff_ext.asym.index != indexNil)
1557 FDR *fdr;
1558 unsigned int indx;
1559 int bigendian;
1560 bfd_size_type sym_base;
1561 union aux_ext *aux_base;
1563 fdr = ecoffsymbol (symbol)->fdr;
1564 indx = ecoff_ext.asym.index;
1566 /* sym_base is used to map the fdr relative indices which
1567 appear in the file to the position number which we are
1568 using. */
1569 sym_base = fdr->isymBase;
1570 if (ecoffsymbol (symbol)->local)
1571 sym_base +=
1572 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1574 /* aux_base is the start of the aux entries for this file;
1575 asym.index is an offset from this. */
1576 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1577 + fdr->iauxBase);
1579 /* The aux entries are stored in host byte order; the
1580 order is indicated by a bit in the fdr. */
1581 bigendian = fdr->fBigendian;
1583 /* This switch is basically from gcc/mips-tdump.c */
1584 switch (ecoff_ext.asym.st)
1586 case stNil:
1587 case stLabel:
1588 break;
1590 case stFile:
1591 case stBlock:
1592 fprintf (file, _("\n End+1 symbol: %ld"),
1593 (long) (indx + sym_base));
1594 break;
1596 case stEnd:
1597 if (ecoff_ext.asym.sc == scText
1598 || ecoff_ext.asym.sc == scInfo)
1599 fprintf (file, _("\n First symbol: %ld"),
1600 (long) (indx + sym_base));
1601 else
1602 fprintf (file, _("\n First symbol: %ld"),
1603 ((long)
1604 (AUX_GET_ISYM (bigendian,
1605 &aux_base[ecoff_ext.asym.index])
1606 + sym_base)));
1607 break;
1609 case stProc:
1610 case stStaticProc:
1611 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1613 else if (ecoffsymbol (symbol)->local)
1614 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
1615 ((long)
1616 (AUX_GET_ISYM (bigendian,
1617 &aux_base[ecoff_ext.asym.index])
1618 + sym_base)),
1619 ecoff_type_to_string (abfd, fdr, indx + 1));
1620 else
1621 fprintf (file, _("\n Local symbol: %ld"),
1622 ((long) indx
1623 + (long) sym_base
1624 + (ecoff_data (abfd)
1625 ->debug_info.symbolic_header.iextMax)));
1626 break;
1628 case stStruct:
1629 fprintf (file, _("\n struct; End+1 symbol: %ld"),
1630 (long) (indx + sym_base));
1631 break;
1633 case stUnion:
1634 fprintf (file, _("\n union; End+1 symbol: %ld"),
1635 (long) (indx + sym_base));
1636 break;
1638 case stEnum:
1639 fprintf (file, _("\n enum; End+1 symbol: %ld"),
1640 (long) (indx + sym_base));
1641 break;
1643 default:
1644 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1645 fprintf (file, _("\n Type: %s"),
1646 ecoff_type_to_string (abfd, fdr, indx));
1647 break;
1651 break;
1655 /* Read in the relocs for a section. */
1657 static boolean
1658 ecoff_slurp_reloc_table (abfd, section, symbols)
1659 bfd *abfd;
1660 asection *section;
1661 asymbol **symbols;
1663 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1664 arelent *internal_relocs;
1665 bfd_size_type external_reloc_size;
1666 bfd_size_type external_relocs_size;
1667 char *external_relocs;
1668 arelent *rptr;
1669 unsigned int i;
1671 if (section->relocation != (arelent *) NULL
1672 || section->reloc_count == 0
1673 || (section->flags & SEC_CONSTRUCTOR) != 0)
1674 return true;
1676 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1677 return false;
1679 internal_relocs = (arelent *) bfd_alloc (abfd,
1680 (sizeof (arelent)
1681 * section->reloc_count));
1682 external_reloc_size = backend->external_reloc_size;
1683 external_relocs_size = external_reloc_size * section->reloc_count;
1684 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1685 if (internal_relocs == (arelent *) NULL
1686 || external_relocs == (char *) NULL)
1687 return false;
1688 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1689 return false;
1690 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1691 != external_relocs_size)
1692 return false;
1694 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1696 struct internal_reloc intern;
1698 (*backend->swap_reloc_in) (abfd,
1699 external_relocs + i * external_reloc_size,
1700 &intern);
1702 if (intern.r_extern)
1704 /* r_symndx is an index into the external symbols. */
1705 BFD_ASSERT (intern.r_symndx >= 0
1706 && (intern.r_symndx
1707 < (ecoff_data (abfd)
1708 ->debug_info.symbolic_header.iextMax)));
1709 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1710 rptr->addend = 0;
1712 else if (intern.r_symndx == RELOC_SECTION_NONE
1713 || intern.r_symndx == RELOC_SECTION_ABS)
1715 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1716 rptr->addend = 0;
1718 else
1720 CONST char *sec_name;
1721 asection *sec;
1723 /* r_symndx is a section key. */
1724 switch (intern.r_symndx)
1726 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1727 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1728 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1729 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1730 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1731 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1732 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1733 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1734 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1735 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1736 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1737 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1738 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1739 case RELOC_SECTION_RCONST: sec_name = ".rconst"; break;
1740 default: abort ();
1743 sec = bfd_get_section_by_name (abfd, sec_name);
1744 if (sec == (asection *) NULL)
1745 abort ();
1746 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1748 rptr->addend = - bfd_get_section_vma (abfd, sec);
1751 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1753 /* Let the backend select the howto field and do any other
1754 required processing. */
1755 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1758 bfd_release (abfd, external_relocs);
1760 section->relocation = internal_relocs;
1762 return true;
1765 /* Get a canonical list of relocs. */
1767 long
1768 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1769 bfd *abfd;
1770 asection *section;
1771 arelent **relptr;
1772 asymbol **symbols;
1774 unsigned int count;
1776 if (section->flags & SEC_CONSTRUCTOR)
1778 arelent_chain *chain;
1780 /* This section has relocs made up by us, not the file, so take
1781 them out of their chain and place them into the data area
1782 provided. */
1783 for (count = 0, chain = section->constructor_chain;
1784 count < section->reloc_count;
1785 count++, chain = chain->next)
1786 *relptr++ = &chain->relent;
1788 else
1790 arelent *tblptr;
1792 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1793 return -1;
1795 tblptr = section->relocation;
1797 for (count = 0; count < section->reloc_count; count++)
1798 *relptr++ = tblptr++;
1801 *relptr = (arelent *) NULL;
1803 return section->reloc_count;
1806 /* Provided a BFD, a section and an offset into the section, calculate
1807 and return the name of the source file and the line nearest to the
1808 wanted location. */
1810 boolean
1811 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1812 filename_ptr, functionname_ptr, retline_ptr)
1813 bfd *abfd;
1814 asection *section;
1815 asymbol **ignore_symbols ATTRIBUTE_UNUSED;
1816 bfd_vma offset;
1817 CONST char **filename_ptr;
1818 CONST char **functionname_ptr;
1819 unsigned int *retline_ptr;
1821 const struct ecoff_debug_swap * const debug_swap
1822 = &ecoff_backend (abfd)->debug_swap;
1823 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1824 struct ecoff_find_line *line_info;
1826 /* Make sure we have the FDR's. */
1827 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1828 || bfd_get_symcount (abfd) == 0)
1829 return false;
1831 if (ecoff_data (abfd)->find_line_info == NULL)
1833 ecoff_data (abfd)->find_line_info =
1834 ((struct ecoff_find_line *)
1835 bfd_zalloc (abfd, sizeof (struct ecoff_find_line)));
1836 if (ecoff_data (abfd)->find_line_info == NULL)
1837 return false;
1839 line_info = ecoff_data (abfd)->find_line_info;
1841 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1842 debug_swap, line_info, filename_ptr,
1843 functionname_ptr, retline_ptr);
1846 /* Copy private BFD data. This is called by objcopy and strip. We
1847 use it to copy the ECOFF debugging information from one BFD to the
1848 other. It would be theoretically possible to represent the ECOFF
1849 debugging information in the symbol table. However, it would be a
1850 lot of work, and there would be little gain (gas, gdb, and ld
1851 already access the ECOFF debugging information via the
1852 ecoff_debug_info structure, and that structure would have to be
1853 retained in order to support ECOFF debugging in MIPS ELF).
1855 The debugging information for the ECOFF external symbols comes from
1856 the symbol table, so this function only handles the other debugging
1857 information. */
1859 boolean
1860 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1861 bfd *ibfd;
1862 bfd *obfd;
1864 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1865 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1866 register int i;
1867 asymbol **sym_ptr_ptr;
1868 size_t c;
1869 boolean local;
1871 /* We only want to copy information over if both BFD's use ECOFF
1872 format. */
1873 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1874 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1875 return true;
1877 /* Copy the GP value and the register masks. */
1878 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1879 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1880 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1881 for (i = 0; i < 3; i++)
1882 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1884 /* Copy the version stamp. */
1885 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1887 /* If there are no symbols, don't copy any debugging information. */
1888 c = bfd_get_symcount (obfd);
1889 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1890 if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1891 return true;
1893 /* See if there are any local symbols. */
1894 local = false;
1895 for (; c > 0; c--, sym_ptr_ptr++)
1897 if (ecoffsymbol (*sym_ptr_ptr)->local)
1899 local = true;
1900 break;
1904 if (local)
1906 /* There are some local symbols. We just bring over all the
1907 debugging information. FIXME: This is not quite the right
1908 thing to do. If the user has asked us to discard all
1909 debugging information, then we are probably going to wind up
1910 keeping it because there will probably be some local symbol
1911 which objcopy did not discard. We should actually break
1912 apart the debugging information and only keep that which
1913 applies to the symbols we want to keep. */
1914 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1915 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1916 oinfo->line = iinfo->line;
1918 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1919 oinfo->external_dnr = iinfo->external_dnr;
1921 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1922 oinfo->external_pdr = iinfo->external_pdr;
1924 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1925 oinfo->external_sym = iinfo->external_sym;
1927 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1928 oinfo->external_opt = iinfo->external_opt;
1930 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1931 oinfo->external_aux = iinfo->external_aux;
1933 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1934 oinfo->ss = iinfo->ss;
1936 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1937 oinfo->external_fdr = iinfo->external_fdr;
1939 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1940 oinfo->external_rfd = iinfo->external_rfd;
1942 else
1944 /* We are discarding all the local symbol information. Look
1945 through the external symbols and remove all references to FDR
1946 or aux information. */
1947 c = bfd_get_symcount (obfd);
1948 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1949 for (; c > 0; c--, sym_ptr_ptr++)
1951 EXTR esym;
1953 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1954 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1955 esym.ifd = ifdNil;
1956 esym.asym.index = indexNil;
1957 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1958 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1962 return true;
1965 /* Set the architecture. The supported architecture is stored in the
1966 backend pointer. We always set the architecture anyhow, since many
1967 callers ignore the return value. */
1969 boolean
1970 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1971 bfd *abfd;
1972 enum bfd_architecture arch;
1973 unsigned long machine;
1975 bfd_default_set_arch_mach (abfd, arch, machine);
1976 return arch == ecoff_backend (abfd)->arch;
1979 /* Get the size of the section headers. */
1982 _bfd_ecoff_sizeof_headers (abfd, reloc)
1983 bfd *abfd;
1984 boolean reloc ATTRIBUTE_UNUSED;
1986 asection *current;
1987 int c;
1988 int ret;
1990 c = 0;
1991 for (current = abfd->sections;
1992 current != (asection *)NULL;
1993 current = current->next)
1994 ++c;
1996 ret = (bfd_coff_filhsz (abfd)
1997 + bfd_coff_aoutsz (abfd)
1998 + c * bfd_coff_scnhsz (abfd));
1999 return BFD_ALIGN (ret, 16);
2002 /* Get the contents of a section. */
2004 boolean
2005 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
2006 bfd *abfd;
2007 asection *section;
2008 PTR location;
2009 file_ptr offset;
2010 bfd_size_type count;
2012 return _bfd_generic_get_section_contents (abfd, section, location,
2013 offset, count);
2016 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2017 called via qsort. */
2019 static int
2020 ecoff_sort_hdrs (arg1, arg2)
2021 const PTR arg1;
2022 const PTR arg2;
2024 const asection *hdr1 = *(const asection **) arg1;
2025 const asection *hdr2 = *(const asection **) arg2;
2027 if ((hdr1->flags & SEC_ALLOC) != 0)
2029 if ((hdr2->flags & SEC_ALLOC) == 0)
2030 return -1;
2032 else
2034 if ((hdr2->flags & SEC_ALLOC) != 0)
2035 return 1;
2037 if (hdr1->vma < hdr2->vma)
2038 return -1;
2039 else if (hdr1->vma > hdr2->vma)
2040 return 1;
2041 else
2042 return 0;
2045 /* Calculate the file position for each section, and set
2046 reloc_filepos. */
2048 static boolean
2049 ecoff_compute_section_file_positions (abfd)
2050 bfd *abfd;
2052 file_ptr sofar, file_sofar;
2053 asection **sorted_hdrs;
2054 asection *current;
2055 unsigned int i;
2056 file_ptr old_sofar;
2057 boolean rdata_in_text;
2058 boolean first_data, first_nonalloc;
2059 const bfd_vma round = ecoff_backend (abfd)->round;
2061 sofar = _bfd_ecoff_sizeof_headers (abfd, false);
2062 file_sofar = sofar;
2064 /* Sort the sections by VMA. */
2065 sorted_hdrs = (asection **) bfd_malloc (abfd->section_count
2066 * sizeof (asection *));
2067 if (sorted_hdrs == NULL)
2068 return false;
2069 for (current = abfd->sections, i = 0;
2070 current != NULL;
2071 current = current->next, i++)
2072 sorted_hdrs[i] = current;
2073 BFD_ASSERT (i == abfd->section_count);
2075 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2076 ecoff_sort_hdrs);
2078 /* Some versions of the OSF linker put the .rdata section in the
2079 text segment, and some do not. */
2080 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2081 if (rdata_in_text)
2083 for (i = 0; i < abfd->section_count; i++)
2085 current = sorted_hdrs[i];
2086 if (strcmp (current->name, _RDATA) == 0)
2087 break;
2088 if ((current->flags & SEC_CODE) == 0
2089 && strcmp (current->name, _PDATA) != 0
2090 && strcmp (current->name, _RCONST) != 0)
2092 rdata_in_text = false;
2093 break;
2097 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2099 first_data = true;
2100 first_nonalloc = true;
2101 for (i = 0; i < abfd->section_count; i++)
2103 unsigned int alignment_power;
2105 current = sorted_hdrs[i];
2107 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2108 supposed to indicate the number of .pdata entries that are
2109 really in the section. Each entry is 8 bytes. We store this
2110 away in line_filepos before increasing the section size. */
2111 if (strcmp (current->name, _PDATA) == 0)
2112 current->line_filepos = current->_raw_size / 8;
2114 alignment_power = current->alignment_power;
2116 /* On Ultrix, the data sections in an executable file must be
2117 aligned to a page boundary within the file. This does not
2118 affect the section size, though. FIXME: Does this work for
2119 other platforms? It requires some modification for the
2120 Alpha, because .rdata on the Alpha goes with the text, not
2121 the data. */
2122 if ((abfd->flags & EXEC_P) != 0
2123 && (abfd->flags & D_PAGED) != 0
2124 && ! first_data
2125 && (current->flags & SEC_CODE) == 0
2126 && (! rdata_in_text
2127 || strcmp (current->name, _RDATA) != 0)
2128 && strcmp (current->name, _PDATA) != 0
2129 && strcmp (current->name, _RCONST) != 0)
2131 sofar = (sofar + round - 1) &~ (round - 1);
2132 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2133 first_data = false;
2135 else if (strcmp (current->name, _LIB) == 0)
2137 /* On Irix 4, the location of contents of the .lib section
2138 from a shared library section is also rounded up to a
2139 page boundary. */
2141 sofar = (sofar + round - 1) &~ (round - 1);
2142 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2144 else if (first_nonalloc
2145 && (current->flags & SEC_ALLOC) == 0
2146 && (abfd->flags & D_PAGED) != 0)
2148 /* Skip up to the next page for an unallocated section, such
2149 as the .comment section on the Alpha. This leaves room
2150 for the .bss section. */
2151 first_nonalloc = false;
2152 sofar = (sofar + round - 1) &~ (round - 1);
2153 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2156 /* Align the sections in the file to the same boundary on
2157 which they are aligned in virtual memory. */
2158 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2159 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2160 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2162 if ((abfd->flags & D_PAGED) != 0
2163 && (current->flags & SEC_ALLOC) != 0)
2165 sofar += (current->vma - sofar) % round;
2166 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2167 file_sofar += (current->vma - file_sofar) % round;
2170 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2171 current->filepos = file_sofar;
2173 sofar += current->_raw_size;
2174 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2175 file_sofar += current->_raw_size;
2177 /* make sure that this section is of the right size too */
2178 old_sofar = sofar;
2179 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2180 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2181 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2182 current->_raw_size += sofar - old_sofar;
2185 free (sorted_hdrs);
2186 sorted_hdrs = NULL;
2188 ecoff_data (abfd)->reloc_filepos = file_sofar;
2190 return true;
2193 /* Determine the location of the relocs for all the sections in the
2194 output file, as well as the location of the symbolic debugging
2195 information. */
2197 static bfd_size_type
2198 ecoff_compute_reloc_file_positions (abfd)
2199 bfd *abfd;
2201 const bfd_size_type external_reloc_size =
2202 ecoff_backend (abfd)->external_reloc_size;
2203 file_ptr reloc_base;
2204 bfd_size_type reloc_size;
2205 asection *current;
2206 file_ptr sym_base;
2208 if (! abfd->output_has_begun)
2210 if (! ecoff_compute_section_file_positions (abfd))
2211 abort ();
2212 abfd->output_has_begun = true;
2215 reloc_base = ecoff_data (abfd)->reloc_filepos;
2217 reloc_size = 0;
2218 for (current = abfd->sections;
2219 current != (asection *)NULL;
2220 current = current->next)
2222 if (current->reloc_count == 0)
2223 current->rel_filepos = 0;
2224 else
2226 bfd_size_type relsize;
2228 current->rel_filepos = reloc_base;
2229 relsize = current->reloc_count * external_reloc_size;
2230 reloc_size += relsize;
2231 reloc_base += relsize;
2235 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2237 /* At least on Ultrix, the symbol table of an executable file must
2238 be aligned to a page boundary. FIXME: Is this true on other
2239 platforms? */
2240 if ((abfd->flags & EXEC_P) != 0
2241 && (abfd->flags & D_PAGED) != 0)
2242 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2243 &~ (ecoff_backend (abfd)->round - 1));
2245 ecoff_data (abfd)->sym_filepos = sym_base;
2247 return reloc_size;
2250 /* Set the contents of a section. */
2252 boolean
2253 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2254 bfd *abfd;
2255 asection *section;
2256 PTR location;
2257 file_ptr offset;
2258 bfd_size_type count;
2260 /* This must be done first, because bfd_set_section_contents is
2261 going to set output_has_begun to true. */
2262 if (abfd->output_has_begun == false)
2264 if (! ecoff_compute_section_file_positions (abfd))
2265 return false;
2268 /* Handle the .lib section specially so that Irix 4 shared libraries
2269 work out. See coff_set_section_contents in coffcode.h. */
2270 if (strcmp (section->name, _LIB) == 0)
2272 bfd_byte *rec, *recend;
2274 rec = (bfd_byte *) location;
2275 recend = rec + count;
2276 while (rec < recend)
2278 ++section->lma;
2279 rec += bfd_get_32 (abfd, rec) * 4;
2282 BFD_ASSERT (rec == recend);
2285 if (count == 0)
2286 return true;
2288 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2289 || bfd_write (location, 1, count, abfd) != count)
2290 return false;
2292 return true;
2295 /* Get the GP value for an ECOFF file. This is a hook used by
2296 nlmconv. */
2298 bfd_vma
2299 bfd_ecoff_get_gp_value (abfd)
2300 bfd *abfd;
2302 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2303 || bfd_get_format (abfd) != bfd_object)
2305 bfd_set_error (bfd_error_invalid_operation);
2306 return 0;
2309 return ecoff_data (abfd)->gp;
2312 /* Set the GP value for an ECOFF file. This is a hook used by the
2313 assembler. */
2315 boolean
2316 bfd_ecoff_set_gp_value (abfd, gp_value)
2317 bfd *abfd;
2318 bfd_vma gp_value;
2320 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2321 || bfd_get_format (abfd) != bfd_object)
2323 bfd_set_error (bfd_error_invalid_operation);
2324 return false;
2327 ecoff_data (abfd)->gp = gp_value;
2329 return true;
2332 /* Set the register masks for an ECOFF file. This is a hook used by
2333 the assembler. */
2335 boolean
2336 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2337 bfd *abfd;
2338 unsigned long gprmask;
2339 unsigned long fprmask;
2340 unsigned long *cprmask;
2342 ecoff_data_type *tdata;
2344 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2345 || bfd_get_format (abfd) != bfd_object)
2347 bfd_set_error (bfd_error_invalid_operation);
2348 return false;
2351 tdata = ecoff_data (abfd);
2352 tdata->gprmask = gprmask;
2353 tdata->fprmask = fprmask;
2354 if (cprmask != (unsigned long *) NULL)
2356 register int i;
2358 for (i = 0; i < 3; i++)
2359 tdata->cprmask[i] = cprmask[i];
2362 return true;
2365 /* Get ECOFF EXTR information for an external symbol. This function
2366 is passed to bfd_ecoff_debug_externals. */
2368 static boolean
2369 ecoff_get_extr (sym, esym)
2370 asymbol *sym;
2371 EXTR *esym;
2373 ecoff_symbol_type *ecoff_sym_ptr;
2374 bfd *input_bfd;
2376 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2377 || ecoffsymbol (sym)->native == NULL)
2379 /* Don't include debugging, local, or section symbols. */
2380 if ((sym->flags & BSF_DEBUGGING) != 0
2381 || (sym->flags & BSF_LOCAL) != 0
2382 || (sym->flags & BSF_SECTION_SYM) != 0)
2383 return false;
2385 esym->jmptbl = 0;
2386 esym->cobol_main = 0;
2387 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2388 esym->reserved = 0;
2389 esym->ifd = ifdNil;
2390 /* FIXME: we can do better than this for st and sc. */
2391 esym->asym.st = stGlobal;
2392 esym->asym.sc = scAbs;
2393 esym->asym.reserved = 0;
2394 esym->asym.index = indexNil;
2395 return true;
2398 ecoff_sym_ptr = ecoffsymbol (sym);
2400 if (ecoff_sym_ptr->local)
2401 return false;
2403 input_bfd = bfd_asymbol_bfd (sym);
2404 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2405 (input_bfd, ecoff_sym_ptr->native, esym);
2407 /* If the symbol was defined by the linker, then esym will be
2408 undefined but sym will not be. Get a better class for such a
2409 symbol. */
2410 if ((esym->asym.sc == scUndefined
2411 || esym->asym.sc == scSUndefined)
2412 && ! bfd_is_und_section (bfd_get_section (sym)))
2413 esym->asym.sc = scAbs;
2415 /* Adjust the FDR index for the symbol by that used for the input
2416 BFD. */
2417 if (esym->ifd != -1)
2419 struct ecoff_debug_info *input_debug;
2421 input_debug = &ecoff_data (input_bfd)->debug_info;
2422 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2423 if (input_debug->ifdmap != (RFDT *) NULL)
2424 esym->ifd = input_debug->ifdmap[esym->ifd];
2427 return true;
2430 /* Set the external symbol index. This routine is passed to
2431 bfd_ecoff_debug_externals. */
2433 static void
2434 ecoff_set_index (sym, indx)
2435 asymbol *sym;
2436 bfd_size_type indx;
2438 ecoff_set_sym_index (sym, indx);
2441 /* Write out an ECOFF file. */
2443 boolean
2444 _bfd_ecoff_write_object_contents (abfd)
2445 bfd *abfd;
2447 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2448 const bfd_vma round = backend->round;
2449 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2450 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2451 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2452 const bfd_size_type external_hdr_size
2453 = backend->debug_swap.external_hdr_size;
2454 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2455 void (* const adjust_reloc_out) PARAMS ((bfd *,
2456 const arelent *,
2457 struct internal_reloc *))
2458 = backend->adjust_reloc_out;
2459 void (* const swap_reloc_out) PARAMS ((bfd *,
2460 const struct internal_reloc *,
2461 PTR))
2462 = backend->swap_reloc_out;
2463 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2464 HDRR * const symhdr = &debug->symbolic_header;
2465 asection *current;
2466 unsigned int count;
2467 bfd_size_type reloc_size;
2468 bfd_size_type text_size;
2469 bfd_vma text_start;
2470 boolean set_text_start;
2471 bfd_size_type data_size;
2472 bfd_vma data_start;
2473 boolean set_data_start;
2474 bfd_size_type bss_size;
2475 PTR buff = NULL;
2476 PTR reloc_buff = NULL;
2477 struct internal_filehdr internal_f;
2478 struct internal_aouthdr internal_a;
2479 int i;
2481 /* Determine where the sections and relocs will go in the output
2482 file. */
2483 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2485 count = 1;
2486 for (current = abfd->sections;
2487 current != (asection *)NULL;
2488 current = current->next)
2490 current->target_index = count;
2491 ++count;
2494 if ((abfd->flags & D_PAGED) != 0)
2495 text_size = _bfd_ecoff_sizeof_headers (abfd, false);
2496 else
2497 text_size = 0;
2498 text_start = 0;
2499 set_text_start = false;
2500 data_size = 0;
2501 data_start = 0;
2502 set_data_start = false;
2503 bss_size = 0;
2505 /* Write section headers to the file. */
2507 /* Allocate buff big enough to hold a section header,
2508 file header, or a.out header. */
2510 bfd_size_type siz;
2511 siz = scnhsz;
2512 if (siz < filhsz)
2513 siz = filhsz;
2514 if (siz < aoutsz)
2515 siz = aoutsz;
2516 buff = (PTR) bfd_malloc ((size_t) siz);
2517 if (buff == NULL)
2518 goto error_return;
2521 internal_f.f_nscns = 0;
2522 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2523 goto error_return;
2524 for (current = abfd->sections;
2525 current != (asection *) NULL;
2526 current = current->next)
2528 struct internal_scnhdr section;
2529 bfd_vma vma;
2531 ++internal_f.f_nscns;
2533 strncpy (section.s_name, current->name, sizeof section.s_name);
2535 /* This seems to be correct for Irix 4 shared libraries. */
2536 vma = bfd_get_section_vma (abfd, current);
2537 if (strcmp (current->name, _LIB) == 0)
2538 section.s_vaddr = 0;
2539 else
2540 section.s_vaddr = vma;
2542 section.s_paddr = current->lma;
2543 section.s_size = bfd_get_section_size_before_reloc (current);
2545 /* If this section is unloadable then the scnptr will be 0. */
2546 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2547 section.s_scnptr = 0;
2548 else
2549 section.s_scnptr = current->filepos;
2550 section.s_relptr = current->rel_filepos;
2552 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2553 object file produced by the assembler is supposed to point to
2554 information about how much room is required by objects of
2555 various different sizes. I think this only matters if we
2556 want the linker to compute the best size to use, or
2557 something. I don't know what happens if the information is
2558 not present. */
2559 if (strcmp (current->name, _PDATA) != 0)
2560 section.s_lnnoptr = 0;
2561 else
2563 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2564 hold the number of entries in the section (each entry is
2565 8 bytes). We stored this in the line_filepos field in
2566 ecoff_compute_section_file_positions. */
2567 section.s_lnnoptr = current->line_filepos;
2570 section.s_nreloc = current->reloc_count;
2571 section.s_nlnno = 0;
2572 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2573 current->flags);
2575 if (bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff) == 0
2576 || bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2577 goto error_return;
2579 if ((section.s_flags & STYP_TEXT) != 0
2580 || ((section.s_flags & STYP_RDATA) != 0
2581 && ecoff_data (abfd)->rdata_in_text)
2582 || section.s_flags == STYP_PDATA
2583 || (section.s_flags & STYP_DYNAMIC) != 0
2584 || (section.s_flags & STYP_LIBLIST) != 0
2585 || (section.s_flags & STYP_RELDYN) != 0
2586 || section.s_flags == STYP_CONFLIC
2587 || (section.s_flags & STYP_DYNSTR) != 0
2588 || (section.s_flags & STYP_DYNSYM) != 0
2589 || (section.s_flags & STYP_HASH) != 0
2590 || (section.s_flags & STYP_ECOFF_INIT) != 0
2591 || (section.s_flags & STYP_ECOFF_FINI) != 0
2592 || section.s_flags == STYP_RCONST)
2594 text_size += bfd_get_section_size_before_reloc (current);
2595 if (! set_text_start || text_start > vma)
2597 text_start = vma;
2598 set_text_start = true;
2601 else if ((section.s_flags & STYP_RDATA) != 0
2602 || (section.s_flags & STYP_DATA) != 0
2603 || (section.s_flags & STYP_LITA) != 0
2604 || (section.s_flags & STYP_LIT8) != 0
2605 || (section.s_flags & STYP_LIT4) != 0
2606 || (section.s_flags & STYP_SDATA) != 0
2607 || section.s_flags == STYP_XDATA
2608 || (section.s_flags & STYP_GOT) != 0)
2610 data_size += bfd_get_section_size_before_reloc (current);
2611 if (! set_data_start || data_start > vma)
2613 data_start = vma;
2614 set_data_start = true;
2617 else if ((section.s_flags & STYP_BSS) != 0
2618 || (section.s_flags & STYP_SBSS) != 0)
2619 bss_size += bfd_get_section_size_before_reloc (current);
2620 else if (section.s_flags == 0
2621 || (section.s_flags & STYP_ECOFF_LIB) != 0
2622 || section.s_flags == STYP_COMMENT)
2623 /* Do nothing */ ;
2624 else
2625 abort ();
2628 /* Set up the file header. */
2630 internal_f.f_magic = ecoff_get_magic (abfd);
2632 /* We will NOT put a fucking timestamp in the header here. Every
2633 time you put it back, I will come in and take it out again. I'm
2634 sorry. This field does not belong here. We fill it with a 0 so
2635 it compares the same but is not a reasonable time. --
2636 gnu@cygnus.com. */
2637 internal_f.f_timdat = 0;
2639 if (bfd_get_symcount (abfd) != 0)
2641 /* The ECOFF f_nsyms field is not actually the number of
2642 symbols, it's the size of symbolic information header. */
2643 internal_f.f_nsyms = external_hdr_size;
2644 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2646 else
2648 internal_f.f_nsyms = 0;
2649 internal_f.f_symptr = 0;
2652 internal_f.f_opthdr = aoutsz;
2654 internal_f.f_flags = F_LNNO;
2655 if (reloc_size == 0)
2656 internal_f.f_flags |= F_RELFLG;
2657 if (bfd_get_symcount (abfd) == 0)
2658 internal_f.f_flags |= F_LSYMS;
2659 if (abfd->flags & EXEC_P)
2660 internal_f.f_flags |= F_EXEC;
2662 if (bfd_little_endian (abfd))
2663 internal_f.f_flags |= F_AR32WR;
2664 else
2665 internal_f.f_flags |= F_AR32W;
2667 /* Set up the ``optional'' header. */
2668 if ((abfd->flags & D_PAGED) != 0)
2669 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2670 else
2671 internal_a.magic = ECOFF_AOUT_OMAGIC;
2673 /* FIXME: Is this really correct? */
2674 internal_a.vstamp = symhdr->vstamp;
2676 /* At least on Ultrix, these have to be rounded to page boundaries.
2677 FIXME: Is this true on other platforms? */
2678 if ((abfd->flags & D_PAGED) != 0)
2680 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2681 internal_a.text_start = text_start &~ (round - 1);
2682 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2683 internal_a.data_start = data_start &~ (round - 1);
2685 else
2687 internal_a.tsize = text_size;
2688 internal_a.text_start = text_start;
2689 internal_a.dsize = data_size;
2690 internal_a.data_start = data_start;
2693 /* On Ultrix, the initial portions of the .sbss and .bss segments
2694 are at the end of the data section. The bsize field in the
2695 optional header records how many bss bytes are required beyond
2696 those in the data section. The value is not rounded to a page
2697 boundary. */
2698 if (bss_size < internal_a.dsize - data_size)
2699 bss_size = 0;
2700 else
2701 bss_size -= internal_a.dsize - data_size;
2702 internal_a.bsize = bss_size;
2703 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2705 internal_a.entry = bfd_get_start_address (abfd);
2707 internal_a.gp_value = ecoff_data (abfd)->gp;
2709 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2710 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2711 for (i = 0; i < 4; i++)
2712 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2714 /* Let the backend adjust the headers if necessary. */
2715 if (backend->adjust_headers)
2717 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2718 goto error_return;
2721 /* Write out the file header and the optional header. */
2723 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2724 goto error_return;
2726 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2727 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2728 goto error_return;
2730 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2731 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2732 goto error_return;
2734 /* Build the external symbol information. This must be done before
2735 writing out the relocs so that we know the symbol indices. We
2736 don't do this if this BFD was created by the backend linker,
2737 since it will have already handled the symbols and relocs. */
2738 if (! ecoff_data (abfd)->linker)
2740 symhdr->iextMax = 0;
2741 symhdr->issExtMax = 0;
2742 debug->external_ext = debug->external_ext_end = NULL;
2743 debug->ssext = debug->ssext_end = NULL;
2744 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2745 (((abfd->flags & EXEC_P) == 0)
2746 ? true : false),
2747 ecoff_get_extr, ecoff_set_index)
2748 == false)
2749 goto error_return;
2751 /* Write out the relocs. */
2752 for (current = abfd->sections;
2753 current != (asection *) NULL;
2754 current = current->next)
2756 arelent **reloc_ptr_ptr;
2757 arelent **reloc_end;
2758 char *out_ptr;
2760 if (current->reloc_count == 0)
2761 continue;
2763 reloc_buff =
2764 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2765 if (reloc_buff == NULL)
2766 goto error_return;
2768 reloc_ptr_ptr = current->orelocation;
2769 reloc_end = reloc_ptr_ptr + current->reloc_count;
2770 out_ptr = (char *) reloc_buff;
2771 for (;
2772 reloc_ptr_ptr < reloc_end;
2773 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2775 arelent *reloc;
2776 asymbol *sym;
2777 struct internal_reloc in;
2779 memset ((PTR) &in, 0, sizeof in);
2781 reloc = *reloc_ptr_ptr;
2782 sym = *reloc->sym_ptr_ptr;
2784 in.r_vaddr = (reloc->address
2785 + bfd_get_section_vma (abfd, current));
2786 in.r_type = reloc->howto->type;
2788 if ((sym->flags & BSF_SECTION_SYM) == 0)
2790 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2791 in.r_extern = 1;
2793 else
2795 CONST char *name;
2797 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2798 if (strcmp (name, ".text") == 0)
2799 in.r_symndx = RELOC_SECTION_TEXT;
2800 else if (strcmp (name, ".rdata") == 0)
2801 in.r_symndx = RELOC_SECTION_RDATA;
2802 else if (strcmp (name, ".data") == 0)
2803 in.r_symndx = RELOC_SECTION_DATA;
2804 else if (strcmp (name, ".sdata") == 0)
2805 in.r_symndx = RELOC_SECTION_SDATA;
2806 else if (strcmp (name, ".sbss") == 0)
2807 in.r_symndx = RELOC_SECTION_SBSS;
2808 else if (strcmp (name, ".bss") == 0)
2809 in.r_symndx = RELOC_SECTION_BSS;
2810 else if (strcmp (name, ".init") == 0)
2811 in.r_symndx = RELOC_SECTION_INIT;
2812 else if (strcmp (name, ".lit8") == 0)
2813 in.r_symndx = RELOC_SECTION_LIT8;
2814 else if (strcmp (name, ".lit4") == 0)
2815 in.r_symndx = RELOC_SECTION_LIT4;
2816 else if (strcmp (name, ".xdata") == 0)
2817 in.r_symndx = RELOC_SECTION_XDATA;
2818 else if (strcmp (name, ".pdata") == 0)
2819 in.r_symndx = RELOC_SECTION_PDATA;
2820 else if (strcmp (name, ".fini") == 0)
2821 in.r_symndx = RELOC_SECTION_FINI;
2822 else if (strcmp (name, ".lita") == 0)
2823 in.r_symndx = RELOC_SECTION_LITA;
2824 else if (strcmp (name, "*ABS*") == 0)
2825 in.r_symndx = RELOC_SECTION_ABS;
2826 else if (strcmp (name, ".rconst") == 0)
2827 in.r_symndx = RELOC_SECTION_RCONST;
2828 else
2829 abort ();
2830 in.r_extern = 0;
2833 (*adjust_reloc_out) (abfd, reloc, &in);
2835 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2838 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2839 goto error_return;
2840 if (bfd_write (reloc_buff,
2841 external_reloc_size, current->reloc_count, abfd)
2842 != external_reloc_size * current->reloc_count)
2843 goto error_return;
2844 bfd_release (abfd, reloc_buff);
2845 reloc_buff = NULL;
2848 /* Write out the symbolic debugging information. */
2849 if (bfd_get_symcount (abfd) > 0)
2851 /* Write out the debugging information. */
2852 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2853 ecoff_data (abfd)->sym_filepos)
2854 == false)
2855 goto error_return;
2859 /* The .bss section of a demand paged executable must receive an
2860 entire page. If there are symbols, the symbols will start on the
2861 next page. If there are no symbols, we must fill out the page by
2862 hand. */
2863 if (bfd_get_symcount (abfd) == 0
2864 && (abfd->flags & EXEC_P) != 0
2865 && (abfd->flags & D_PAGED) != 0)
2867 char c;
2869 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2870 SEEK_SET) != 0)
2871 goto error_return;
2872 if (bfd_read (&c, 1, 1, abfd) == 0)
2873 c = 0;
2874 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2875 SEEK_SET) != 0)
2876 goto error_return;
2877 if (bfd_write (&c, 1, 1, abfd) != 1)
2878 goto error_return;
2881 if (reloc_buff != NULL)
2882 bfd_release (abfd, reloc_buff);
2883 if (buff != NULL)
2884 free (buff);
2885 return true;
2886 error_return:
2887 if (reloc_buff != NULL)
2888 bfd_release (abfd, reloc_buff);
2889 if (buff != NULL)
2890 free (buff);
2891 return false;
2894 /* Archive handling. ECOFF uses what appears to be a unique type of
2895 archive header (armap). The byte ordering of the armap and the
2896 contents are encoded in the name of the armap itself. At least for
2897 now, we only support archives with the same byte ordering in the
2898 armap and the contents.
2900 The first four bytes in the armap are the number of symbol
2901 definitions. This is always a power of two.
2903 This is followed by the symbol definitions. Each symbol definition
2904 occupies 8 bytes. The first four bytes are the offset from the
2905 start of the armap strings to the null-terminated string naming
2906 this symbol. The second four bytes are the file offset to the
2907 archive member which defines this symbol. If the second four bytes
2908 are 0, then this is not actually a symbol definition, and it should
2909 be ignored.
2911 The symbols are hashed into the armap with a closed hashing scheme.
2912 See the functions below for the details of the algorithm.
2914 After the symbol definitions comes four bytes holding the size of
2915 the string table, followed by the string table itself. */
2917 /* The name of an archive headers looks like this:
2918 __________E[BL]E[BL]_ (with a trailing space).
2919 The trailing space is changed to an X if the archive is changed to
2920 indicate that the armap is out of date.
2922 The Alpha seems to use ________64E[BL]E[BL]_. */
2924 #define ARMAP_BIG_ENDIAN 'B'
2925 #define ARMAP_LITTLE_ENDIAN 'L'
2926 #define ARMAP_MARKER 'E'
2927 #define ARMAP_START_LENGTH 10
2928 #define ARMAP_HEADER_MARKER_INDEX 10
2929 #define ARMAP_HEADER_ENDIAN_INDEX 11
2930 #define ARMAP_OBJECT_MARKER_INDEX 12
2931 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2932 #define ARMAP_END_INDEX 14
2933 #define ARMAP_END "_ "
2935 /* This is a magic number used in the hashing algorithm. */
2936 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2938 /* This returns the hash value to use for a string. It also sets
2939 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2940 is the number of entries in the hash table, and HLOG is the log
2941 base 2 of SIZE. */
2943 static unsigned int
2944 ecoff_armap_hash (s, rehash, size, hlog)
2945 CONST char *s;
2946 unsigned int *rehash;
2947 unsigned int size;
2948 unsigned int hlog;
2950 unsigned int hash;
2952 if (hlog == 0)
2953 return 0;
2954 hash = *s++;
2955 while (*s != '\0')
2956 hash = ((hash >> 27) | (hash << 5)) + *s++;
2957 hash *= ARMAP_HASH_MAGIC;
2958 *rehash = (hash & (size - 1)) | 1;
2959 return hash >> (32 - hlog);
2962 /* Read in the armap. */
2964 boolean
2965 _bfd_ecoff_slurp_armap (abfd)
2966 bfd *abfd;
2968 char nextname[17];
2969 unsigned int i;
2970 struct areltdata *mapdata;
2971 bfd_size_type parsed_size;
2972 char *raw_armap;
2973 struct artdata *ardata;
2974 unsigned int count;
2975 char *raw_ptr;
2976 struct symdef *symdef_ptr;
2977 char *stringbase;
2979 /* Get the name of the first element. */
2980 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2981 if (i == 0)
2982 return true;
2983 if (i != 16)
2984 return false;
2986 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2987 return false;
2989 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2990 standard COFF armap. We could move the ECOFF armap stuff into
2991 bfd_slurp_armap, but that seems inappropriate since no other
2992 target uses this format. Instead, we check directly for a COFF
2993 armap. */
2994 if (strncmp (nextname, "/ ", 16) == 0)
2995 return bfd_slurp_armap (abfd);
2997 /* See if the first element is an armap. */
2998 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2999 ARMAP_START_LENGTH) != 0
3000 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3001 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3002 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3003 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3004 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3005 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3006 || strncmp (nextname + ARMAP_END_INDEX,
3007 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3009 bfd_has_map (abfd) = false;
3010 return true;
3013 /* Make sure we have the right byte ordering. */
3014 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3015 ^ (bfd_header_big_endian (abfd)))
3016 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3017 ^ (bfd_big_endian (abfd))))
3019 bfd_set_error (bfd_error_wrong_format);
3020 return false;
3023 /* Read in the armap. */
3024 ardata = bfd_ardata (abfd);
3025 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
3026 if (mapdata == (struct areltdata *) NULL)
3027 return false;
3028 parsed_size = mapdata->parsed_size;
3029 bfd_release (abfd, (PTR) mapdata);
3031 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3032 if (raw_armap == (char *) NULL)
3033 return false;
3035 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3037 if (bfd_get_error () != bfd_error_system_call)
3038 bfd_set_error (bfd_error_malformed_archive);
3039 bfd_release (abfd, (PTR) raw_armap);
3040 return false;
3043 ardata->tdata = (PTR) raw_armap;
3045 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3047 ardata->symdef_count = 0;
3048 ardata->cache = (struct ar_cache *) NULL;
3050 /* This code used to overlay the symdefs over the raw archive data,
3051 but that doesn't work on a 64 bit host. */
3053 stringbase = raw_armap + count * 8 + 8;
3055 #ifdef CHECK_ARMAP_HASH
3057 unsigned int hlog;
3059 /* Double check that I have the hashing algorithm right by making
3060 sure that every symbol can be looked up successfully. */
3061 hlog = 0;
3062 for (i = 1; i < count; i <<= 1)
3063 hlog++;
3064 BFD_ASSERT (i == count);
3066 raw_ptr = raw_armap + 4;
3067 for (i = 0; i < count; i++, raw_ptr += 8)
3069 unsigned int name_offset, file_offset;
3070 unsigned int hash, rehash, srch;
3072 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3073 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3074 if (file_offset == 0)
3075 continue;
3076 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3077 hlog);
3078 if (hash == i)
3079 continue;
3081 /* See if we can rehash to this location. */
3082 for (srch = (hash + rehash) & (count - 1);
3083 srch != hash && srch != i;
3084 srch = (srch + rehash) & (count - 1))
3085 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3086 != 0);
3087 BFD_ASSERT (srch == i);
3091 #endif /* CHECK_ARMAP_HASH */
3093 raw_ptr = raw_armap + 4;
3094 for (i = 0; i < count; i++, raw_ptr += 8)
3095 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3096 ++ardata->symdef_count;
3098 symdef_ptr = ((struct symdef *)
3099 bfd_alloc (abfd,
3100 ardata->symdef_count * sizeof (struct symdef)));
3101 if (!symdef_ptr)
3102 return false;
3104 ardata->symdefs = (carsym *) symdef_ptr;
3106 raw_ptr = raw_armap + 4;
3107 for (i = 0; i < count; i++, raw_ptr += 8)
3109 unsigned int name_offset, file_offset;
3111 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3112 if (file_offset == 0)
3113 continue;
3114 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3115 symdef_ptr->s.name = stringbase + name_offset;
3116 symdef_ptr->file_offset = file_offset;
3117 ++symdef_ptr;
3120 ardata->first_file_filepos = bfd_tell (abfd);
3121 /* Pad to an even boundary. */
3122 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3124 bfd_has_map (abfd) = true;
3126 return true;
3129 /* Write out an armap. */
3131 boolean
3132 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3133 bfd *abfd;
3134 unsigned int elength;
3135 struct orl *map;
3136 unsigned int orl_count;
3137 int stridx;
3139 unsigned int hashsize, hashlog;
3140 unsigned int symdefsize;
3141 int padit;
3142 unsigned int stringsize;
3143 unsigned int mapsize;
3144 file_ptr firstreal;
3145 struct ar_hdr hdr;
3146 struct stat statbuf;
3147 unsigned int i;
3148 bfd_byte temp[4];
3149 bfd_byte *hashtable;
3150 bfd *current;
3151 bfd *last_elt;
3153 /* Ultrix appears to use as a hash table size the least power of two
3154 greater than twice the number of entries. */
3155 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3157 hashsize = 1 << hashlog;
3159 symdefsize = hashsize * 8;
3160 padit = stridx % 2;
3161 stringsize = stridx + padit;
3163 /* Include 8 bytes to store symdefsize and stringsize in output. */
3164 mapsize = symdefsize + stringsize + 8;
3166 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3168 memset ((PTR) &hdr, 0, sizeof hdr);
3170 /* Work out the ECOFF armap name. */
3171 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3172 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3173 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3174 (bfd_header_big_endian (abfd)
3175 ? ARMAP_BIG_ENDIAN
3176 : ARMAP_LITTLE_ENDIAN);
3177 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3178 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3179 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3180 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3182 /* Write the timestamp of the archive header to be just a little bit
3183 later than the timestamp of the file, otherwise the linker will
3184 complain that the index is out of date. Actually, the Ultrix
3185 linker just checks the archive name; the GNU linker may check the
3186 date. */
3187 stat (abfd->filename, &statbuf);
3188 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3190 /* The DECstation uses zeroes for the uid, gid and mode of the
3191 armap. */
3192 hdr.ar_uid[0] = '0';
3193 hdr.ar_gid[0] = '0';
3194 #if 0
3195 hdr.ar_mode[0] = '0';
3196 #else
3197 /* Building gcc ends up extracting the armap as a file - twice. */
3198 hdr.ar_mode[0] = '6';
3199 hdr.ar_mode[1] = '4';
3200 hdr.ar_mode[2] = '4';
3201 #endif
3203 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3205 hdr.ar_fmag[0] = '`';
3206 hdr.ar_fmag[1] = '\012';
3208 /* Turn all null bytes in the header into spaces. */
3209 for (i = 0; i < sizeof (struct ar_hdr); i++)
3210 if (((char *) (&hdr))[i] == '\0')
3211 (((char *) (&hdr))[i]) = ' ';
3213 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3214 != sizeof (struct ar_hdr))
3215 return false;
3217 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3218 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3219 return false;
3221 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3222 if (!hashtable)
3223 return false;
3225 current = abfd->archive_head;
3226 last_elt = current;
3227 for (i = 0; i < orl_count; i++)
3229 unsigned int hash, rehash;
3231 /* Advance firstreal to the file position of this archive
3232 element. */
3233 if (((bfd *) map[i].pos) != last_elt)
3237 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3238 firstreal += firstreal % 2;
3239 current = current->next;
3241 while (current != (bfd *) map[i].pos);
3244 last_elt = current;
3246 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3247 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3249 unsigned int srch;
3251 /* The desired slot is already taken. */
3252 for (srch = (hash + rehash) & (hashsize - 1);
3253 srch != hash;
3254 srch = (srch + rehash) & (hashsize - 1))
3255 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3256 break;
3258 BFD_ASSERT (srch != hash);
3260 hash = srch;
3263 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3264 (PTR) (hashtable + hash * 8));
3265 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3266 (PTR) (hashtable + hash * 8 + 4));
3269 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3270 return false;
3272 bfd_release (abfd, hashtable);
3274 /* Now write the strings. */
3275 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3276 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3277 return false;
3278 for (i = 0; i < orl_count; i++)
3280 bfd_size_type len;
3282 len = strlen (*map[i].name) + 1;
3283 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3284 return false;
3287 /* The spec sez this should be a newline. But in order to be
3288 bug-compatible for DECstation ar we use a null. */
3289 if (padit)
3291 if (bfd_write ("", 1, 1, abfd) != 1)
3292 return false;
3295 return true;
3298 /* See whether this BFD is an archive. If it is, read in the armap
3299 and the extended name table. */
3301 const bfd_target *
3302 _bfd_ecoff_archive_p (abfd)
3303 bfd *abfd;
3305 struct artdata *tdata_hold;
3306 char armag[SARMAG + 1];
3308 tdata_hold = abfd->tdata.aout_ar_data;
3310 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG)
3312 if (bfd_get_error () != bfd_error_system_call)
3313 bfd_set_error (bfd_error_wrong_format);
3314 return (const bfd_target *) NULL;
3317 if (strncmp (armag, ARMAG, SARMAG) != 0)
3319 bfd_set_error (bfd_error_wrong_format);
3320 return NULL;
3323 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3324 involves a cast, we can't do it as the left operand of
3325 assignment. */
3326 abfd->tdata.aout_ar_data =
3327 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3329 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3331 abfd->tdata.aout_ar_data = tdata_hold;
3332 return (const bfd_target *) NULL;
3335 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3336 bfd_ardata (abfd)->cache = NULL;
3337 bfd_ardata (abfd)->archive_head = NULL;
3338 bfd_ardata (abfd)->symdefs = NULL;
3339 bfd_ardata (abfd)->extended_names = NULL;
3340 bfd_ardata (abfd)->tdata = NULL;
3342 if (_bfd_ecoff_slurp_armap (abfd) == false
3343 || _bfd_ecoff_slurp_extended_name_table (abfd) == false)
3345 bfd_release (abfd, bfd_ardata (abfd));
3346 abfd->tdata.aout_ar_data = tdata_hold;
3347 return (const bfd_target *) NULL;
3350 if (bfd_has_map (abfd))
3352 bfd *first;
3354 /* This archive has a map, so we may presume that the contents
3355 are object files. Make sure that if the first file in the
3356 archive can be recognized as an object file, it is for this
3357 target. If not, assume that this is the wrong format. If
3358 the first file is not an object file, somebody is doing
3359 something weird, and we permit it so that ar -t will work. */
3361 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3362 if (first != NULL)
3364 boolean fail;
3366 first->target_defaulted = false;
3367 fail = false;
3368 if (bfd_check_format (first, bfd_object)
3369 && first->xvec != abfd->xvec)
3371 (void) bfd_close (first);
3372 bfd_release (abfd, bfd_ardata (abfd));
3373 abfd->tdata.aout_ar_data = tdata_hold;
3374 bfd_set_error (bfd_error_wrong_format);
3375 return NULL;
3378 /* We ought to close first here, but we can't, because we
3379 have no way to remove it from the archive cache. FIXME. */
3383 return abfd->xvec;
3386 /* ECOFF linker code. */
3388 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3389 PARAMS ((struct bfd_hash_entry *entry,
3390 struct bfd_hash_table *table,
3391 const char *string));
3392 static boolean ecoff_link_add_archive_symbols
3393 PARAMS ((bfd *, struct bfd_link_info *));
3394 static boolean ecoff_link_check_archive_element
3395 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3396 static boolean ecoff_link_add_object_symbols
3397 PARAMS ((bfd *, struct bfd_link_info *));
3398 static boolean ecoff_link_add_externals
3399 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3401 /* Routine to create an entry in an ECOFF link hash table. */
3403 static struct bfd_hash_entry *
3404 ecoff_link_hash_newfunc (entry, table, string)
3405 struct bfd_hash_entry *entry;
3406 struct bfd_hash_table *table;
3407 const char *string;
3409 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3411 /* Allocate the structure if it has not already been allocated by a
3412 subclass. */
3413 if (ret == (struct ecoff_link_hash_entry *) NULL)
3414 ret = ((struct ecoff_link_hash_entry *)
3415 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3416 if (ret == (struct ecoff_link_hash_entry *) NULL)
3417 return NULL;
3419 /* Call the allocation method of the superclass. */
3420 ret = ((struct ecoff_link_hash_entry *)
3421 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3422 table, string));
3424 if (ret)
3426 /* Set local fields. */
3427 ret->indx = -1;
3428 ret->abfd = NULL;
3429 ret->written = 0;
3430 ret->small = 0;
3432 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3434 return (struct bfd_hash_entry *) ret;
3437 /* Create an ECOFF link hash table. */
3439 struct bfd_link_hash_table *
3440 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3441 bfd *abfd;
3443 struct ecoff_link_hash_table *ret;
3445 ret = ((struct ecoff_link_hash_table *)
3446 bfd_alloc (abfd, sizeof (struct ecoff_link_hash_table)));
3447 if (ret == NULL)
3448 return NULL;
3449 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3450 ecoff_link_hash_newfunc))
3452 free (ret);
3453 return (struct bfd_link_hash_table *) NULL;
3455 return &ret->root;
3458 /* Look up an entry in an ECOFF link hash table. */
3460 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3461 ((struct ecoff_link_hash_entry *) \
3462 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3464 /* Traverse an ECOFF link hash table. */
3466 #define ecoff_link_hash_traverse(table, func, info) \
3467 (bfd_link_hash_traverse \
3468 (&(table)->root, \
3469 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3470 (info)))
3472 /* Get the ECOFF link hash table from the info structure. This is
3473 just a cast. */
3475 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3477 /* Given an ECOFF BFD, add symbols to the global hash table as
3478 appropriate. */
3480 boolean
3481 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3482 bfd *abfd;
3483 struct bfd_link_info *info;
3485 switch (bfd_get_format (abfd))
3487 case bfd_object:
3488 return ecoff_link_add_object_symbols (abfd, info);
3489 case bfd_archive:
3490 return ecoff_link_add_archive_symbols (abfd, info);
3491 default:
3492 bfd_set_error (bfd_error_wrong_format);
3493 return false;
3497 /* Add the symbols from an archive file to the global hash table.
3498 This looks through the undefined symbols, looks each one up in the
3499 archive hash table, and adds any associated object file. We do not
3500 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3501 already have a hash table, so there is no reason to construct
3502 another one. */
3504 static boolean
3505 ecoff_link_add_archive_symbols (abfd, info)
3506 bfd *abfd;
3507 struct bfd_link_info *info;
3509 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3510 const bfd_byte *raw_armap;
3511 struct bfd_link_hash_entry **pundef;
3512 unsigned int armap_count;
3513 unsigned int armap_log;
3514 unsigned int i;
3515 const bfd_byte *hashtable;
3516 const char *stringbase;
3518 if (! bfd_has_map (abfd))
3520 /* An empty archive is a special case. */
3521 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3522 return true;
3523 bfd_set_error (bfd_error_no_armap);
3524 return false;
3527 /* If we don't have any raw data for this archive, as can happen on
3528 Irix 4.0.5F, we call the generic routine.
3529 FIXME: We should be more clever about this, since someday tdata
3530 may get to something for a generic archive. */
3531 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3532 if (raw_armap == (bfd_byte *) NULL)
3533 return (_bfd_generic_link_add_archive_symbols
3534 (abfd, info, ecoff_link_check_archive_element));
3536 armap_count = bfd_h_get_32 (abfd, raw_armap);
3538 armap_log = 0;
3539 for (i = 1; i < armap_count; i <<= 1)
3540 armap_log++;
3541 BFD_ASSERT (i == armap_count);
3543 hashtable = raw_armap + 4;
3544 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3546 /* Look through the list of undefined symbols. */
3547 pundef = &info->hash->undefs;
3548 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3550 struct bfd_link_hash_entry *h;
3551 unsigned int hash, rehash;
3552 unsigned int file_offset;
3553 const char *name;
3554 bfd *element;
3556 h = *pundef;
3558 /* When a symbol is defined, it is not necessarily removed from
3559 the list. */
3560 if (h->type != bfd_link_hash_undefined
3561 && h->type != bfd_link_hash_common)
3563 /* Remove this entry from the list, for general cleanliness
3564 and because we are going to look through the list again
3565 if we search any more libraries. We can't remove the
3566 entry if it is the tail, because that would lose any
3567 entries we add to the list later on. */
3568 if (*pundef != info->hash->undefs_tail)
3569 *pundef = (*pundef)->next;
3570 else
3571 pundef = &(*pundef)->next;
3572 continue;
3575 /* Native ECOFF linkers do not pull in archive elements merely
3576 to satisfy common definitions, so neither do we. We leave
3577 them on the list, though, in case we are linking against some
3578 other object format. */
3579 if (h->type != bfd_link_hash_undefined)
3581 pundef = &(*pundef)->next;
3582 continue;
3585 /* Look for this symbol in the archive hash table. */
3586 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3587 armap_log);
3589 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3590 if (file_offset == 0)
3592 /* Nothing in this slot. */
3593 pundef = &(*pundef)->next;
3594 continue;
3597 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3598 if (name[0] != h->root.string[0]
3599 || strcmp (name, h->root.string) != 0)
3601 unsigned int srch;
3602 boolean found;
3604 /* That was the wrong symbol. Try rehashing. */
3605 found = false;
3606 for (srch = (hash + rehash) & (armap_count - 1);
3607 srch != hash;
3608 srch = (srch + rehash) & (armap_count - 1))
3610 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3611 if (file_offset == 0)
3612 break;
3613 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3614 if (name[0] == h->root.string[0]
3615 && strcmp (name, h->root.string) == 0)
3617 found = true;
3618 break;
3622 if (! found)
3624 pundef = &(*pundef)->next;
3625 continue;
3628 hash = srch;
3631 element = (*backend->get_elt_at_filepos) (abfd, file_offset);
3632 if (element == (bfd *) NULL)
3633 return false;
3635 if (! bfd_check_format (element, bfd_object))
3636 return false;
3638 /* Unlike the generic linker, we know that this element provides
3639 a definition for an undefined symbol and we know that we want
3640 to include it. We don't need to check anything. */
3641 if (! (*info->callbacks->add_archive_element) (info, element, name))
3642 return false;
3643 if (! ecoff_link_add_object_symbols (element, info))
3644 return false;
3646 pundef = &(*pundef)->next;
3649 return true;
3652 /* This is called if we used _bfd_generic_link_add_archive_symbols
3653 because we were not dealing with an ECOFF archive. */
3655 static boolean
3656 ecoff_link_check_archive_element (abfd, info, pneeded)
3657 bfd *abfd;
3658 struct bfd_link_info *info;
3659 boolean *pneeded;
3661 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3662 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3663 = backend->debug_swap.swap_ext_in;
3664 HDRR *symhdr;
3665 bfd_size_type external_ext_size;
3666 PTR external_ext = NULL;
3667 size_t esize;
3668 char *ssext = NULL;
3669 char *ext_ptr;
3670 char *ext_end;
3672 *pneeded = false;
3674 if (! ecoff_slurp_symbolic_header (abfd))
3675 goto error_return;
3677 /* If there are no symbols, we don't want it. */
3678 if (bfd_get_symcount (abfd) == 0)
3679 goto successful_return;
3681 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3683 /* Read in the external symbols and external strings. */
3684 external_ext_size = backend->debug_swap.external_ext_size;
3685 esize = symhdr->iextMax * external_ext_size;
3686 external_ext = (PTR) bfd_malloc (esize);
3687 if (external_ext == NULL && esize != 0)
3688 goto error_return;
3690 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3691 || bfd_read (external_ext, 1, esize, abfd) != esize)
3692 goto error_return;
3694 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3695 if (ssext == NULL && symhdr->issExtMax != 0)
3696 goto error_return;
3698 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3699 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd) !=
3700 (bfd_size_type) symhdr->issExtMax))
3701 goto error_return;
3703 /* Look through the external symbols to see if they define some
3704 symbol that is currently undefined. */
3705 ext_ptr = (char *) external_ext;
3706 ext_end = ext_ptr + esize;
3707 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3709 EXTR esym;
3710 boolean def;
3711 const char *name;
3712 struct bfd_link_hash_entry *h;
3714 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3716 /* See if this symbol defines something. */
3717 if (esym.asym.st != stGlobal
3718 && esym.asym.st != stLabel
3719 && esym.asym.st != stProc)
3720 continue;
3722 switch (esym.asym.sc)
3724 case scText:
3725 case scData:
3726 case scBss:
3727 case scAbs:
3728 case scSData:
3729 case scSBss:
3730 case scRData:
3731 case scCommon:
3732 case scSCommon:
3733 case scInit:
3734 case scFini:
3735 case scRConst:
3736 def = true;
3737 break;
3738 default:
3739 def = false;
3740 break;
3743 if (! def)
3744 continue;
3746 name = ssext + esym.asym.iss;
3747 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3749 /* Unlike the generic linker, we do not pull in elements because
3750 of common symbols. */
3751 if (h == (struct bfd_link_hash_entry *) NULL
3752 || h->type != bfd_link_hash_undefined)
3753 continue;
3755 /* Include this element. */
3756 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3757 goto error_return;
3758 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3759 goto error_return;
3761 *pneeded = true;
3762 goto successful_return;
3765 successful_return:
3766 if (external_ext != NULL)
3767 free (external_ext);
3768 if (ssext != NULL)
3769 free (ssext);
3770 return true;
3771 error_return:
3772 if (external_ext != NULL)
3773 free (external_ext);
3774 if (ssext != NULL)
3775 free (ssext);
3776 return false;
3779 /* Add symbols from an ECOFF object file to the global linker hash
3780 table. */
3782 static boolean
3783 ecoff_link_add_object_symbols (abfd, info)
3784 bfd *abfd;
3785 struct bfd_link_info *info;
3787 HDRR *symhdr;
3788 bfd_size_type external_ext_size;
3789 PTR external_ext = NULL;
3790 size_t esize;
3791 char *ssext = NULL;
3792 boolean result;
3794 if (! ecoff_slurp_symbolic_header (abfd))
3795 return false;
3797 /* If there are no symbols, we don't want it. */
3798 if (bfd_get_symcount (abfd) == 0)
3799 return true;
3801 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3803 /* Read in the external symbols and external strings. */
3804 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3805 esize = symhdr->iextMax * external_ext_size;
3806 external_ext = (PTR) bfd_malloc (esize);
3807 if (external_ext == NULL && esize != 0)
3808 goto error_return;
3810 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3811 || bfd_read (external_ext, 1, esize, abfd) != esize)
3812 goto error_return;
3814 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3815 if (ssext == NULL && symhdr->issExtMax != 0)
3816 goto error_return;
3818 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3819 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd)
3820 != (bfd_size_type) symhdr->issExtMax))
3821 goto error_return;
3823 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3825 if (ssext != NULL)
3826 free (ssext);
3827 if (external_ext != NULL)
3828 free (external_ext);
3829 return result;
3831 error_return:
3832 if (ssext != NULL)
3833 free (ssext);
3834 if (external_ext != NULL)
3835 free (external_ext);
3836 return false;
3839 /* Add the external symbols of an object file to the global linker
3840 hash table. The external symbols and strings we are passed are
3841 just allocated on the stack, and will be discarded. We must
3842 explicitly save any information we may need later on in the link.
3843 We do not want to read the external symbol information again. */
3845 static boolean
3846 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3847 bfd *abfd;
3848 struct bfd_link_info *info;
3849 PTR external_ext;
3850 char *ssext;
3852 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3853 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3854 = backend->debug_swap.swap_ext_in;
3855 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3856 unsigned long ext_count;
3857 struct ecoff_link_hash_entry **sym_hash;
3858 char *ext_ptr;
3859 char *ext_end;
3861 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3863 sym_hash = ((struct ecoff_link_hash_entry **)
3864 bfd_alloc (abfd,
3865 ext_count * sizeof (struct bfd_link_hash_entry *)));
3866 if (!sym_hash)
3867 return false;
3868 ecoff_data (abfd)->sym_hashes = sym_hash;
3870 ext_ptr = (char *) external_ext;
3871 ext_end = ext_ptr + ext_count * external_ext_size;
3872 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3874 EXTR esym;
3875 boolean skip;
3876 bfd_vma value;
3877 asection *section;
3878 const char *name;
3879 struct ecoff_link_hash_entry *h;
3881 *sym_hash = NULL;
3883 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3885 /* Skip debugging symbols. */
3886 skip = false;
3887 switch (esym.asym.st)
3889 case stGlobal:
3890 case stStatic:
3891 case stLabel:
3892 case stProc:
3893 case stStaticProc:
3894 break;
3895 default:
3896 skip = true;
3897 break;
3900 if (skip)
3901 continue;
3903 /* Get the information for this symbol. */
3904 value = esym.asym.value;
3905 switch (esym.asym.sc)
3907 default:
3908 case scNil:
3909 case scRegister:
3910 case scCdbLocal:
3911 case scBits:
3912 case scCdbSystem:
3913 case scRegImage:
3914 case scInfo:
3915 case scUserStruct:
3916 case scVar:
3917 case scVarRegister:
3918 case scVariant:
3919 case scBasedVar:
3920 case scXData:
3921 case scPData:
3922 section = NULL;
3923 break;
3924 case scText:
3925 section = bfd_make_section_old_way (abfd, ".text");
3926 value -= section->vma;
3927 break;
3928 case scData:
3929 section = bfd_make_section_old_way (abfd, ".data");
3930 value -= section->vma;
3931 break;
3932 case scBss:
3933 section = bfd_make_section_old_way (abfd, ".bss");
3934 value -= section->vma;
3935 break;
3936 case scAbs:
3937 section = bfd_abs_section_ptr;
3938 break;
3939 case scUndefined:
3940 section = bfd_und_section_ptr;
3941 break;
3942 case scSData:
3943 section = bfd_make_section_old_way (abfd, ".sdata");
3944 value -= section->vma;
3945 break;
3946 case scSBss:
3947 section = bfd_make_section_old_way (abfd, ".sbss");
3948 value -= section->vma;
3949 break;
3950 case scRData:
3951 section = bfd_make_section_old_way (abfd, ".rdata");
3952 value -= section->vma;
3953 break;
3954 case scCommon:
3955 if (value > ecoff_data (abfd)->gp_size)
3957 section = bfd_com_section_ptr;
3958 break;
3960 /* Fall through. */
3961 case scSCommon:
3962 if (ecoff_scom_section.name == NULL)
3964 /* Initialize the small common section. */
3965 ecoff_scom_section.name = SCOMMON;
3966 ecoff_scom_section.flags = SEC_IS_COMMON;
3967 ecoff_scom_section.output_section = &ecoff_scom_section;
3968 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3969 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3970 ecoff_scom_symbol.name = SCOMMON;
3971 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3972 ecoff_scom_symbol.section = &ecoff_scom_section;
3973 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3975 section = &ecoff_scom_section;
3976 break;
3977 case scSUndefined:
3978 section = bfd_und_section_ptr;
3979 break;
3980 case scInit:
3981 section = bfd_make_section_old_way (abfd, ".init");
3982 value -= section->vma;
3983 break;
3984 case scFini:
3985 section = bfd_make_section_old_way (abfd, ".fini");
3986 value -= section->vma;
3987 break;
3988 case scRConst:
3989 section = bfd_make_section_old_way (abfd, ".rconst");
3990 value -= section->vma;
3991 break;
3994 if (section == (asection *) NULL)
3995 continue;
3997 name = ssext + esym.asym.iss;
3999 h = NULL;
4000 if (! (_bfd_generic_link_add_one_symbol
4001 (info, abfd, name,
4002 esym.weakext ? BSF_WEAK : BSF_GLOBAL,
4003 section, value, (const char *) NULL, true, true,
4004 (struct bfd_link_hash_entry **) &h)))
4005 return false;
4007 *sym_hash = h;
4009 /* If we are building an ECOFF hash table, save the external
4010 symbol information. */
4011 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
4013 if (h->abfd == (bfd *) NULL
4014 || (! bfd_is_und_section (section)
4015 && (! bfd_is_com_section (section)
4016 || (h->root.type != bfd_link_hash_defined
4017 && h->root.type != bfd_link_hash_defweak))))
4019 h->abfd = abfd;
4020 h->esym = esym;
4023 /* Remember whether this symbol was small undefined. */
4024 if (esym.asym.sc == scSUndefined)
4025 h->small = 1;
4027 /* If this symbol was ever small undefined, it needs to wind
4028 up in a GP relative section. We can't control the
4029 section of a defined symbol, but we can control the
4030 section of a common symbol. This case is actually needed
4031 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
4032 if (h->small
4033 && h->root.type == bfd_link_hash_common
4034 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
4036 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
4037 SCOMMON);
4038 h->root.u.c.p->section->flags = SEC_ALLOC;
4039 if (h->esym.asym.sc == scCommon)
4040 h->esym.asym.sc = scSCommon;
4045 return true;
4048 /* ECOFF final link routines. */
4050 static boolean ecoff_final_link_debug_accumulate
4051 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4052 PTR handle));
4053 static boolean ecoff_link_write_external
4054 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4055 static boolean ecoff_indirect_link_order
4056 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4057 struct bfd_link_order *));
4058 static boolean ecoff_reloc_link_order
4059 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4060 struct bfd_link_order *));
4062 /* Structure used to pass information to ecoff_link_write_external. */
4064 struct extsym_info
4066 bfd *abfd;
4067 struct bfd_link_info *info;
4070 /* ECOFF final link routine. This looks through all the input BFDs
4071 and gathers together all the debugging information, and then
4072 processes all the link order information. This may cause it to
4073 close and reopen some input BFDs; I'll see how bad this is. */
4075 boolean
4076 _bfd_ecoff_bfd_final_link (abfd, info)
4077 bfd *abfd;
4078 struct bfd_link_info *info;
4080 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4081 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4082 HDRR *symhdr;
4083 PTR handle;
4084 register bfd *input_bfd;
4085 asection *o;
4086 struct bfd_link_order *p;
4087 struct extsym_info einfo;
4089 /* We accumulate the debugging information counts in the symbolic
4090 header. */
4091 symhdr = &debug->symbolic_header;
4092 symhdr->vstamp = 0;
4093 symhdr->ilineMax = 0;
4094 symhdr->cbLine = 0;
4095 symhdr->idnMax = 0;
4096 symhdr->ipdMax = 0;
4097 symhdr->isymMax = 0;
4098 symhdr->ioptMax = 0;
4099 symhdr->iauxMax = 0;
4100 symhdr->issMax = 0;
4101 symhdr->issExtMax = 0;
4102 symhdr->ifdMax = 0;
4103 symhdr->crfd = 0;
4104 symhdr->iextMax = 0;
4106 /* We accumulate the debugging information itself in the debug_info
4107 structure. */
4108 debug->line = NULL;
4109 debug->external_dnr = NULL;
4110 debug->external_pdr = NULL;
4111 debug->external_sym = NULL;
4112 debug->external_opt = NULL;
4113 debug->external_aux = NULL;
4114 debug->ss = NULL;
4115 debug->ssext = debug->ssext_end = NULL;
4116 debug->external_fdr = NULL;
4117 debug->external_rfd = NULL;
4118 debug->external_ext = debug->external_ext_end = NULL;
4120 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4121 if (handle == (PTR) NULL)
4122 return false;
4124 /* Accumulate the debugging symbols from each input BFD. */
4125 for (input_bfd = info->input_bfds;
4126 input_bfd != (bfd *) NULL;
4127 input_bfd = input_bfd->link_next)
4129 boolean ret;
4131 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4133 /* Abitrarily set the symbolic header vstamp to the vstamp
4134 of the first object file in the link. */
4135 if (symhdr->vstamp == 0)
4136 symhdr->vstamp
4137 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4138 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4139 handle);
4141 else
4142 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4143 debug, &backend->debug_swap,
4144 input_bfd, info);
4145 if (! ret)
4146 return false;
4148 /* Combine the register masks. */
4149 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4150 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4151 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4152 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4153 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4154 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4157 /* Write out the external symbols. */
4158 einfo.abfd = abfd;
4159 einfo.info = info;
4160 ecoff_link_hash_traverse (ecoff_hash_table (info),
4161 ecoff_link_write_external,
4162 (PTR) &einfo);
4164 if (info->relocateable)
4166 /* We need to make a pass over the link_orders to count up the
4167 number of relocations we will need to output, so that we know
4168 how much space they will take up. */
4169 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4171 o->reloc_count = 0;
4172 for (p = o->link_order_head;
4173 p != (struct bfd_link_order *) NULL;
4174 p = p->next)
4175 if (p->type == bfd_indirect_link_order)
4176 o->reloc_count += p->u.indirect.section->reloc_count;
4177 else if (p->type == bfd_section_reloc_link_order
4178 || p->type == bfd_symbol_reloc_link_order)
4179 ++o->reloc_count;
4183 /* Compute the reloc and symbol file positions. */
4184 ecoff_compute_reloc_file_positions (abfd);
4186 /* Write out the debugging information. */
4187 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4188 &backend->debug_swap, info,
4189 ecoff_data (abfd)->sym_filepos))
4190 return false;
4192 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4194 if (info->relocateable)
4196 /* Now reset the reloc_count field of the sections in the output
4197 BFD to 0, so that we can use them to keep track of how many
4198 relocs we have output thus far. */
4199 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4200 o->reloc_count = 0;
4203 /* Get a value for the GP register. */
4204 if (ecoff_data (abfd)->gp == 0)
4206 struct bfd_link_hash_entry *h;
4208 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4209 if (h != (struct bfd_link_hash_entry *) NULL
4210 && h->type == bfd_link_hash_defined)
4211 ecoff_data (abfd)->gp = (h->u.def.value
4212 + h->u.def.section->output_section->vma
4213 + h->u.def.section->output_offset);
4214 else if (info->relocateable)
4216 bfd_vma lo;
4218 /* Make up a value. */
4219 lo = (bfd_vma) -1;
4220 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4222 if (o->vma < lo
4223 && (strcmp (o->name, _SBSS) == 0
4224 || strcmp (o->name, _SDATA) == 0
4225 || strcmp (o->name, _LIT4) == 0
4226 || strcmp (o->name, _LIT8) == 0
4227 || strcmp (o->name, _LITA) == 0))
4228 lo = o->vma;
4230 ecoff_data (abfd)->gp = lo + 0x8000;
4232 else
4234 /* If the relocate_section function needs to do a reloc
4235 involving the GP value, it should make a reloc_dangerous
4236 callback to warn that GP is not defined. */
4240 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4242 for (p = o->link_order_head;
4243 p != (struct bfd_link_order *) NULL;
4244 p = p->next)
4246 if (p->type == bfd_indirect_link_order
4247 && (bfd_get_flavour (p->u.indirect.section->owner)
4248 == bfd_target_ecoff_flavour))
4250 if (! ecoff_indirect_link_order (abfd, info, o, p))
4251 return false;
4253 else if (p->type == bfd_section_reloc_link_order
4254 || p->type == bfd_symbol_reloc_link_order)
4256 if (! ecoff_reloc_link_order (abfd, info, o, p))
4257 return false;
4259 else
4261 if (! _bfd_default_link_order (abfd, info, o, p))
4262 return false;
4267 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4269 ecoff_data (abfd)->linker = true;
4271 return true;
4274 /* Accumulate the debugging information for an input BFD into the
4275 output BFD. This must read in the symbolic information of the
4276 input BFD. */
4278 static boolean
4279 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4280 bfd *output_bfd;
4281 bfd *input_bfd;
4282 struct bfd_link_info *info;
4283 PTR handle;
4285 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4286 const struct ecoff_debug_swap * const swap =
4287 &ecoff_backend (input_bfd)->debug_swap;
4288 HDRR *symhdr = &debug->symbolic_header;
4289 boolean ret;
4291 #define READ(ptr, offset, count, size, type) \
4292 if (symhdr->count == 0) \
4293 debug->ptr = NULL; \
4294 else \
4296 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
4297 if (debug->ptr == NULL) \
4299 ret = false; \
4300 goto return_something; \
4302 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4303 != 0) \
4304 || (bfd_read (debug->ptr, size, symhdr->count, \
4305 input_bfd) != size * symhdr->count)) \
4307 ret = false; \
4308 goto return_something; \
4312 /* If raw_syments is not NULL, then the data was already by read by
4313 _bfd_ecoff_slurp_symbolic_info. */
4314 if (ecoff_data (input_bfd)->raw_syments == NULL)
4316 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4317 unsigned char *);
4318 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4319 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4320 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4321 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4322 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4323 union aux_ext *);
4324 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4325 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4326 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4328 #undef READ
4330 /* We do not read the external strings or the external symbols. */
4332 ret = (bfd_ecoff_debug_accumulate
4333 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4334 &ecoff_backend (output_bfd)->debug_swap,
4335 input_bfd, debug, swap, info));
4337 return_something:
4338 if (ecoff_data (input_bfd)->raw_syments == NULL)
4340 if (debug->line != NULL)
4341 free (debug->line);
4342 if (debug->external_dnr != NULL)
4343 free (debug->external_dnr);
4344 if (debug->external_pdr != NULL)
4345 free (debug->external_pdr);
4346 if (debug->external_sym != NULL)
4347 free (debug->external_sym);
4348 if (debug->external_opt != NULL)
4349 free (debug->external_opt);
4350 if (debug->external_aux != NULL)
4351 free (debug->external_aux);
4352 if (debug->ss != NULL)
4353 free (debug->ss);
4354 if (debug->external_fdr != NULL)
4355 free (debug->external_fdr);
4356 if (debug->external_rfd != NULL)
4357 free (debug->external_rfd);
4359 /* Make sure we don't accidentally follow one of these pointers
4360 into freed memory. */
4361 debug->line = NULL;
4362 debug->external_dnr = NULL;
4363 debug->external_pdr = NULL;
4364 debug->external_sym = NULL;
4365 debug->external_opt = NULL;
4366 debug->external_aux = NULL;
4367 debug->ss = NULL;
4368 debug->external_fdr = NULL;
4369 debug->external_rfd = NULL;
4372 return ret;
4375 /* Put out information for an external symbol. These come only from
4376 the hash table. */
4378 static boolean
4379 ecoff_link_write_external (h, data)
4380 struct ecoff_link_hash_entry *h;
4381 PTR data;
4383 struct extsym_info *einfo = (struct extsym_info *) data;
4384 bfd *output_bfd = einfo->abfd;
4385 boolean strip;
4387 /* We need to check if this symbol is being stripped. */
4388 if (h->root.type == bfd_link_hash_undefined
4389 || h->root.type == bfd_link_hash_undefweak)
4390 strip = false;
4391 else if (einfo->info->strip == strip_all
4392 || (einfo->info->strip == strip_some
4393 && bfd_hash_lookup (einfo->info->keep_hash,
4394 h->root.root.string,
4395 false, false) == NULL))
4396 strip = true;
4397 else
4398 strip = false;
4400 if (strip || h->written)
4401 return true;
4403 if (h->abfd == (bfd *) NULL)
4405 h->esym.jmptbl = 0;
4406 h->esym.cobol_main = 0;
4407 h->esym.weakext = 0;
4408 h->esym.reserved = 0;
4409 h->esym.ifd = ifdNil;
4410 h->esym.asym.value = 0;
4411 h->esym.asym.st = stGlobal;
4413 if (h->root.type != bfd_link_hash_defined
4414 && h->root.type != bfd_link_hash_defweak)
4415 h->esym.asym.sc = scAbs;
4416 else
4418 asection *output_section;
4419 const char *name;
4421 output_section = h->root.u.def.section->output_section;
4422 name = bfd_section_name (output_section->owner, output_section);
4424 if (strcmp (name, _TEXT) == 0)
4425 h->esym.asym.sc = scText;
4426 else if (strcmp (name, _DATA) == 0)
4427 h->esym.asym.sc = scData;
4428 else if (strcmp (name, _SDATA) == 0)
4429 h->esym.asym.sc = scSData;
4430 else if (strcmp (name, _RDATA) == 0)
4431 h->esym.asym.sc = scRData;
4432 else if (strcmp (name, _BSS) == 0)
4433 h->esym.asym.sc = scBss;
4434 else if (strcmp (name, _SBSS) == 0)
4435 h->esym.asym.sc = scSBss;
4436 else if (strcmp (name, _INIT) == 0)
4437 h->esym.asym.sc = scInit;
4438 else if (strcmp (name, _FINI) == 0)
4439 h->esym.asym.sc = scFini;
4440 else if (strcmp (name, _PDATA) == 0)
4441 h->esym.asym.sc = scPData;
4442 else if (strcmp (name, _XDATA) == 0)
4443 h->esym.asym.sc = scXData;
4444 else if (strcmp (name, _RCONST) == 0)
4445 h->esym.asym.sc = scRConst;
4446 else
4447 h->esym.asym.sc = scAbs;
4450 h->esym.asym.reserved = 0;
4451 h->esym.asym.index = indexNil;
4453 else if (h->esym.ifd != -1)
4455 struct ecoff_debug_info *debug;
4457 /* Adjust the FDR index for the symbol by that used for the
4458 input BFD. */
4459 debug = &ecoff_data (h->abfd)->debug_info;
4460 BFD_ASSERT (h->esym.ifd >= 0
4461 && h->esym.ifd < debug->symbolic_header.ifdMax);
4462 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4465 switch (h->root.type)
4467 default:
4468 case bfd_link_hash_new:
4469 abort ();
4470 case bfd_link_hash_undefined:
4471 case bfd_link_hash_undefweak:
4472 if (h->esym.asym.sc != scUndefined
4473 && h->esym.asym.sc != scSUndefined)
4474 h->esym.asym.sc = scUndefined;
4475 break;
4476 case bfd_link_hash_defined:
4477 case bfd_link_hash_defweak:
4478 if (h->esym.asym.sc == scUndefined
4479 || h->esym.asym.sc == scSUndefined)
4480 h->esym.asym.sc = scAbs;
4481 else if (h->esym.asym.sc == scCommon)
4482 h->esym.asym.sc = scBss;
4483 else if (h->esym.asym.sc == scSCommon)
4484 h->esym.asym.sc = scSBss;
4485 h->esym.asym.value = (h->root.u.def.value
4486 + h->root.u.def.section->output_section->vma
4487 + h->root.u.def.section->output_offset);
4488 break;
4489 case bfd_link_hash_common:
4490 if (h->esym.asym.sc != scCommon
4491 && h->esym.asym.sc != scSCommon)
4492 h->esym.asym.sc = scCommon;
4493 h->esym.asym.value = h->root.u.c.size;
4494 break;
4495 case bfd_link_hash_indirect:
4496 case bfd_link_hash_warning:
4497 /* FIXME: Ignore these for now. The circumstances under which
4498 they should be written out are not clear to me. */
4499 return true;
4502 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4503 symbol number. */
4504 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4505 h->written = 1;
4507 return (bfd_ecoff_debug_one_external
4508 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4509 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4510 &h->esym));
4513 /* Relocate and write an ECOFF section into an ECOFF output file. */
4515 static boolean
4516 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4517 bfd *output_bfd;
4518 struct bfd_link_info *info;
4519 asection *output_section;
4520 struct bfd_link_order *link_order;
4522 asection *input_section;
4523 bfd *input_bfd;
4524 struct ecoff_section_tdata *section_tdata;
4525 bfd_size_type raw_size;
4526 bfd_size_type cooked_size;
4527 bfd_byte *contents = NULL;
4528 bfd_size_type external_reloc_size;
4529 bfd_size_type external_relocs_size;
4530 PTR external_relocs = NULL;
4532 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4534 if (link_order->size == 0)
4535 return true;
4537 input_section = link_order->u.indirect.section;
4538 input_bfd = input_section->owner;
4539 section_tdata = ecoff_section_data (input_bfd, input_section);
4541 raw_size = input_section->_raw_size;
4542 cooked_size = input_section->_cooked_size;
4543 if (cooked_size == 0)
4544 cooked_size = raw_size;
4546 BFD_ASSERT (input_section->output_section == output_section);
4547 BFD_ASSERT (input_section->output_offset == link_order->offset);
4548 BFD_ASSERT (cooked_size == link_order->size);
4550 /* Get the section contents. We allocate memory for the larger of
4551 the size before relocating and the size after relocating. */
4552 contents = (bfd_byte *) bfd_malloc (raw_size >= cooked_size
4553 ? (size_t) raw_size
4554 : (size_t) cooked_size);
4555 if (contents == NULL && raw_size != 0)
4556 goto error_return;
4558 /* If we are relaxing, the contents may have already been read into
4559 memory, in which case we copy them into our new buffer. We don't
4560 simply reuse the old buffer in case cooked_size > raw_size. */
4561 if (section_tdata != (struct ecoff_section_tdata *) NULL
4562 && section_tdata->contents != (bfd_byte *) NULL)
4563 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4564 else
4566 if (! bfd_get_section_contents (input_bfd, input_section,
4567 (PTR) contents,
4568 (file_ptr) 0, raw_size))
4569 goto error_return;
4572 /* Get the relocs. If we are relaxing MIPS code, they will already
4573 have been read in. Otherwise, we read them in now. */
4574 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4575 external_relocs_size = external_reloc_size * input_section->reloc_count;
4577 if (section_tdata != (struct ecoff_section_tdata *) NULL
4578 && section_tdata->external_relocs != NULL)
4579 external_relocs = section_tdata->external_relocs;
4580 else
4582 external_relocs = (PTR) bfd_malloc ((size_t) external_relocs_size);
4583 if (external_relocs == NULL && external_relocs_size != 0)
4584 goto error_return;
4586 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4587 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4588 != external_relocs_size))
4589 goto error_return;
4592 /* Relocate the section contents. */
4593 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4594 (output_bfd, info, input_bfd, input_section, contents,
4595 external_relocs)))
4596 goto error_return;
4598 /* Write out the relocated section. */
4599 if (! bfd_set_section_contents (output_bfd,
4600 output_section,
4601 (PTR) contents,
4602 input_section->output_offset,
4603 cooked_size))
4604 goto error_return;
4606 /* If we are producing relocateable output, the relocs were
4607 modified, and we write them out now. We use the reloc_count
4608 field of output_section to keep track of the number of relocs we
4609 have output so far. */
4610 if (info->relocateable)
4612 if (bfd_seek (output_bfd,
4613 (output_section->rel_filepos +
4614 output_section->reloc_count * external_reloc_size),
4615 SEEK_SET) != 0
4616 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4617 != external_relocs_size))
4618 goto error_return;
4619 output_section->reloc_count += input_section->reloc_count;
4622 if (contents != NULL)
4623 free (contents);
4624 if (external_relocs != NULL && section_tdata == NULL)
4625 free (external_relocs);
4626 return true;
4628 error_return:
4629 if (contents != NULL)
4630 free (contents);
4631 if (external_relocs != NULL && section_tdata == NULL)
4632 free (external_relocs);
4633 return false;
4636 /* Generate a reloc when linking an ECOFF file. This is a reloc
4637 requested by the linker, and does come from any input file. This
4638 is used to build constructor and destructor tables when linking
4639 with -Ur. */
4641 static boolean
4642 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4643 bfd *output_bfd;
4644 struct bfd_link_info *info;
4645 asection *output_section;
4646 struct bfd_link_order *link_order;
4648 enum bfd_link_order_type type;
4649 asection *section;
4650 bfd_vma addend;
4651 arelent rel;
4652 struct internal_reloc in;
4653 bfd_size_type external_reloc_size;
4654 bfd_byte *rbuf;
4655 boolean ok;
4657 type = link_order->type;
4658 section = NULL;
4659 addend = link_order->u.reloc.p->addend;
4661 /* We set up an arelent to pass to the backend adjust_reloc_out
4662 routine. */
4663 rel.address = link_order->offset;
4665 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4666 if (rel.howto == 0)
4668 bfd_set_error (bfd_error_bad_value);
4669 return false;
4672 if (type == bfd_section_reloc_link_order)
4674 section = link_order->u.reloc.p->u.section;
4675 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4677 else
4679 struct bfd_link_hash_entry *h;
4681 /* Treat a reloc against a defined symbol as though it were
4682 actually against the section. */
4683 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4684 link_order->u.reloc.p->u.name,
4685 false, false, false);
4686 if (h != NULL
4687 && (h->type == bfd_link_hash_defined
4688 || h->type == bfd_link_hash_defweak))
4690 type = bfd_section_reloc_link_order;
4691 section = h->u.def.section->output_section;
4692 /* It seems that we ought to add the symbol value to the
4693 addend here, but in practice it has already been added
4694 because it was passed to constructor_callback. */
4695 addend += section->vma + h->u.def.section->output_offset;
4697 else
4699 /* We can't set up a reloc against a symbol correctly,
4700 because we have no asymbol structure. Currently no
4701 adjust_reloc_out routine cares. */
4702 rel.sym_ptr_ptr = (asymbol **) NULL;
4706 /* All ECOFF relocs are in-place. Put the addend into the object
4707 file. */
4709 BFD_ASSERT (rel.howto->partial_inplace);
4710 if (addend != 0)
4712 bfd_size_type size;
4713 bfd_reloc_status_type rstat;
4714 bfd_byte *buf;
4715 boolean ok;
4717 size = bfd_get_reloc_size (rel.howto);
4718 buf = (bfd_byte *) bfd_zmalloc (size);
4719 if (buf == (bfd_byte *) NULL)
4720 return false;
4721 rstat = _bfd_relocate_contents (rel.howto, output_bfd, addend, buf);
4722 switch (rstat)
4724 case bfd_reloc_ok:
4725 break;
4726 default:
4727 case bfd_reloc_outofrange:
4728 abort ();
4729 case bfd_reloc_overflow:
4730 if (! ((*info->callbacks->reloc_overflow)
4731 (info,
4732 (link_order->type == bfd_section_reloc_link_order
4733 ? bfd_section_name (output_bfd, section)
4734 : link_order->u.reloc.p->u.name),
4735 rel.howto->name, addend, (bfd *) NULL,
4736 (asection *) NULL, (bfd_vma) 0)))
4738 free (buf);
4739 return false;
4741 break;
4743 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4744 (file_ptr) link_order->offset, size);
4745 free (buf);
4746 if (! ok)
4747 return false;
4750 rel.addend = 0;
4752 /* Move the information into a internal_reloc structure. */
4753 in.r_vaddr = (rel.address
4754 + bfd_get_section_vma (output_bfd, output_section));
4755 in.r_type = rel.howto->type;
4757 if (type == bfd_symbol_reloc_link_order)
4759 struct ecoff_link_hash_entry *h;
4761 h = ((struct ecoff_link_hash_entry *)
4762 bfd_wrapped_link_hash_lookup (output_bfd, info,
4763 link_order->u.reloc.p->u.name,
4764 false, false, true));
4765 if (h != (struct ecoff_link_hash_entry *) NULL
4766 && h->indx != -1)
4767 in.r_symndx = h->indx;
4768 else
4770 if (! ((*info->callbacks->unattached_reloc)
4771 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4772 (asection *) NULL, (bfd_vma) 0)))
4773 return false;
4774 in.r_symndx = 0;
4776 in.r_extern = 1;
4778 else
4780 CONST char *name;
4782 name = bfd_get_section_name (output_bfd, section);
4783 if (strcmp (name, ".text") == 0)
4784 in.r_symndx = RELOC_SECTION_TEXT;
4785 else if (strcmp (name, ".rdata") == 0)
4786 in.r_symndx = RELOC_SECTION_RDATA;
4787 else if (strcmp (name, ".data") == 0)
4788 in.r_symndx = RELOC_SECTION_DATA;
4789 else if (strcmp (name, ".sdata") == 0)
4790 in.r_symndx = RELOC_SECTION_SDATA;
4791 else if (strcmp (name, ".sbss") == 0)
4792 in.r_symndx = RELOC_SECTION_SBSS;
4793 else if (strcmp (name, ".bss") == 0)
4794 in.r_symndx = RELOC_SECTION_BSS;
4795 else if (strcmp (name, ".init") == 0)
4796 in.r_symndx = RELOC_SECTION_INIT;
4797 else if (strcmp (name, ".lit8") == 0)
4798 in.r_symndx = RELOC_SECTION_LIT8;
4799 else if (strcmp (name, ".lit4") == 0)
4800 in.r_symndx = RELOC_SECTION_LIT4;
4801 else if (strcmp (name, ".xdata") == 0)
4802 in.r_symndx = RELOC_SECTION_XDATA;
4803 else if (strcmp (name, ".pdata") == 0)
4804 in.r_symndx = RELOC_SECTION_PDATA;
4805 else if (strcmp (name, ".fini") == 0)
4806 in.r_symndx = RELOC_SECTION_FINI;
4807 else if (strcmp (name, ".lita") == 0)
4808 in.r_symndx = RELOC_SECTION_LITA;
4809 else if (strcmp (name, "*ABS*") == 0)
4810 in.r_symndx = RELOC_SECTION_ABS;
4811 else if (strcmp (name, ".rconst") == 0)
4812 in.r_symndx = RELOC_SECTION_RCONST;
4813 else
4814 abort ();
4815 in.r_extern = 0;
4818 /* Let the BFD backend adjust the reloc. */
4819 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4821 /* Get some memory and swap out the reloc. */
4822 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4823 rbuf = (bfd_byte *) bfd_malloc ((size_t) external_reloc_size);
4824 if (rbuf == (bfd_byte *) NULL)
4825 return false;
4827 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4829 ok = (bfd_seek (output_bfd,
4830 (output_section->rel_filepos +
4831 output_section->reloc_count * external_reloc_size),
4832 SEEK_SET) == 0
4833 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4834 == external_reloc_size));
4836 if (ok)
4837 ++output_section->reloc_count;
4839 free (rbuf);
4841 return ok;