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