* config/mips/tm-mips.h: Include bfd.h before coff/sym.h.
[deliverable/binutils-gdb.git] / bfd / ecoff.c
CommitLineData
dae31cf5
ILT
1/* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support added by Ian Lance Taylor, ian@cygnus.com.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22#include "bfd.h"
23#include "sysdep.h"
24#include "libbfd.h"
25#include "seclet.h"
26#include "aout/ar.h"
27#include "aout/ranlib.h"
28
29/* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
30 some other stuff which we don't want and which conflicts with stuff
31 we do want. */
32#include "libaout.h"
33#include "aout/aout64.h"
34#undef N_ABS
35#undef exec_hdr
36#undef obj_sym_filepos
37
38#include "coff/internal.h"
39#include "coff/sym.h"
40#include "coff/symconst.h"
41#include "coff/ecoff.h"
42#include "libcoff.h"
43#include "libecoff.h"
44\f
45/* Prototypes for static functions. */
46
47static void ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
48 asymbol *asym, int ext,
49 asymbol **indirect_ptr_ptr));
50static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
51 RNDXR *rndx, long isym,
52 CONST char *which));
53static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
54 unsigned int indx, int bigendian));
55static bfd_reloc_status_type ecoff_generic_reloc PARAMS ((bfd *abfd,
56 arelent *reloc,
57 asymbol *symbol,
58 PTR data,
59 asection *section,
60 bfd *output_bfd));
61static bfd_reloc_status_type ecoff_refhi_reloc PARAMS ((bfd *abfd,
62 arelent *reloc,
63 asymbol *symbol,
64 PTR data,
65 asection *section,
66 bfd *output_bfd));
67static bfd_reloc_status_type ecoff_reflo_reloc PARAMS ((bfd *abfd,
68 arelent *reloc,
69 asymbol *symbol,
70 PTR data,
71 asection *section,
72 bfd *output_bfd));
73static bfd_reloc_status_type ecoff_gprel_reloc PARAMS ((bfd *abfd,
74 arelent *reloc,
75 asymbol *symbol,
76 PTR data,
77 asection *section,
78 bfd *output_bfd));
79static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
80 asymbol **symbols));
81static void ecoff_clear_output_flags PARAMS ((bfd *abfd));
82static boolean ecoff_rel PARAMS ((bfd *output_bfd, bfd_seclet_type *seclet,
83 asection *output_section, PTR data,
84 boolean relocateable));
85static boolean ecoff_dump_seclet PARAMS ((bfd *abfd, bfd_seclet_type *seclet,
86 asection *section, PTR data,
87 boolean relocateable));
88static long ecoff_add_string PARAMS ((bfd *output_bfd, FDR *fdr,
89 CONST char *string, boolean external));
90static boolean ecoff_get_debug PARAMS ((bfd *output_bfd,
91 bfd_seclet_type *seclet,
92 asection *section,
93 boolean relocateable));
94static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
95static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
96 unsigned int *rehash,
97 unsigned int size,
98 unsigned int hlog));
99\f
100/* How to process the various relocs types. */
101
102static reloc_howto_type ecoff_howto_table[] =
103{
104 /* Reloc type 0 is ignored. The reloc reading code ensures that
105 this is a reference to the .abs section, which will cause
106 bfd_perform_relocation to do nothing. */
107 HOWTO (ECOFF_R_IGNORE, /* type */
108 0, /* rightshift */
109 0, /* size (0 = byte, 1 = short, 2 = long) */
110 8, /* bitsize */
111 false, /* pc_relative */
112 0, /* bitpos */
113 complain_overflow_dont, /* complain_on_overflow */
114 0, /* special_function */
115 "IGNORE", /* name */
116 false, /* partial_inplace */
117 0, /* src_mask */
118 0, /* dst_mask */
119 false), /* pcrel_offset */
120
121 /* A 16 bit reference to a symbol, normally from a data section. */
122 HOWTO (ECOFF_R_REFHALF, /* type */
123 0, /* rightshift */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
125 16, /* bitsize */
126 false, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_bitfield, /* complain_on_overflow */
129 ecoff_generic_reloc, /* special_function */
130 "REFHALF", /* name */
131 true, /* partial_inplace */
132 0xffff, /* src_mask */
133 0xffff, /* dst_mask */
134 false), /* pcrel_offset */
135
136 /* A 32 bit reference to a symbol, normally from a data section. */
137 HOWTO (ECOFF_R_REFWORD, /* type */
138 0, /* rightshift */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
140 32, /* bitsize */
141 false, /* pc_relative */
142 0, /* bitpos */
143 complain_overflow_bitfield, /* complain_on_overflow */
144 ecoff_generic_reloc, /* special_function */
145 "REFWORD", /* name */
146 true, /* partial_inplace */
147 0xffffffff, /* src_mask */
148 0xffffffff, /* dst_mask */
149 false), /* pcrel_offset */
150
151 /* A 26 bit absolute jump address. */
152 HOWTO (ECOFF_R_JMPADDR, /* type */
153 2, /* rightshift */
154 2, /* size (0 = byte, 1 = short, 2 = long) */
155 26, /* bitsize */
156 false, /* pc_relative */
157 0, /* bitpos */
158 complain_overflow_bitfield, /* complain_on_overflow */
159 ecoff_generic_reloc, /* special_function */
160 "JMPADDR", /* name */
161 true, /* partial_inplace */
162 0x3ffffff, /* src_mask */
163 0x3ffffff, /* dst_mask */
164 false), /* pcrel_offset */
165
166 /* The high 16 bits of a symbol value. Handled by the function
167 ecoff_refhi_reloc. */
168 HOWTO (ECOFF_R_REFHI, /* type */
169 16, /* rightshift */
170 2, /* size (0 = byte, 1 = short, 2 = long) */
171 16, /* bitsize */
172 false, /* pc_relative */
173 0, /* bitpos */
174 complain_overflow_bitfield, /* complain_on_overflow */
175 ecoff_refhi_reloc, /* special_function */
176 "REFHI", /* name */
177 true, /* partial_inplace */
178 0xffff, /* src_mask */
179 0xffff, /* dst_mask */
180 false), /* pcrel_offset */
181
182 /* The low 16 bits of a symbol value. */
183 HOWTO (ECOFF_R_REFLO, /* type */
184 0, /* rightshift */
185 2, /* size (0 = byte, 1 = short, 2 = long) */
186 16, /* bitsize */
187 false, /* pc_relative */
188 0, /* bitpos */
189 complain_overflow_dont, /* complain_on_overflow */
190 ecoff_reflo_reloc, /* special_function */
191 "REFLO", /* name */
192 true, /* partial_inplace */
193 0xffff, /* src_mask */
194 0xffff, /* dst_mask */
195 false), /* pcrel_offset */
196
197 /* A reference to an offset from the gp register. Handled by the
198 function ecoff_gprel_reloc. */
199 HOWTO (ECOFF_R_GPREL, /* type */
200 0, /* rightshift */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
202 16, /* bitsize */
203 false, /* pc_relative */
204 0, /* bitpos */
205 complain_overflow_signed, /* complain_on_overflow */
206 ecoff_gprel_reloc, /* special_function */
207 "GPREL", /* name */
208 true, /* partial_inplace */
209 0xffff, /* src_mask */
210 0xffff, /* dst_mask */
211 false), /* pcrel_offset */
212
213 /* A reference to a literal using an offset from the gp register.
214 Handled by the function ecoff_gprel_reloc. */
215 HOWTO (ECOFF_R_LITERAL, /* type */
216 0, /* rightshift */
217 2, /* size (0 = byte, 1 = short, 2 = long) */
218 16, /* bitsize */
219 false, /* pc_relative */
220 0, /* bitpos */
221 complain_overflow_signed, /* complain_on_overflow */
222 ecoff_gprel_reloc, /* special_function */
223 "LITERAL", /* name */
224 true, /* partial_inplace */
225 0xffff, /* src_mask */
226 0xffff, /* dst_mask */
227 false) /* pcrel_offset */
228};
229
230#define ECOFF_HOWTO_COUNT \
231 (sizeof ecoff_howto_table / sizeof ecoff_howto_table[0])
232\f
233/* This stuff is somewhat copied from coffcode.h. */
234
235static asection bfd_debug_section = { "*DEBUG*" };
236
237/* This is a hook needed by SCO COFF, but we have nothing to do. */
238
239asection *
240ecoff_make_section_hook (abfd, name)
241 bfd *abfd;
242 char *name;
243{
244 return (asection *) NULL;
245}
246
247/* Initialize a new section. */
248
249boolean
250ecoff_new_section_hook (abfd, section)
251 bfd *abfd;
252 asection *section;
253{
254 section->alignment_power = abfd->xvec->align_power_min;
255
256 if (strcmp (section->name, _TEXT) == 0)
257 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
258 else if (strcmp (section->name, _DATA) == 0
259 || strcmp (section->name, _SDATA) == 0)
260 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
261 else if (strcmp (section->name, _RDATA) == 0
262 || strcmp (section->name, _LIT8) == 0
263 || strcmp (section->name, _LIT4) == 0)
264 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
265 else if (strcmp (section->name, _BSS) == 0
266 || strcmp (section->name, _SBSS) == 0)
267 section->flags |= SEC_ALLOC;
268
269 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
270 uncertain about .init on some systems and I don't know how shared
271 libraries work. */
272
273 return true;
274}
275
276/* Determine the machine architecture and type. */
277
278boolean
279ecoff_set_arch_mach_hook (abfd, filehdr)
280 bfd *abfd;
281 PTR filehdr;
282{
283 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
284 enum bfd_architecture arch;
285
286 switch (internal_f->f_magic)
287 {
288 case MIPS_MAGIC_1:
289 case MIPS_MAGIC_LITTLE:
290 case MIPS_MAGIC_BIG:
291 arch = bfd_arch_mips;
292 break;
293
294 case ALPHA_MAGIC:
295 arch = bfd_arch_alpha;
296 break;
297
298 default:
299 arch = bfd_arch_obscure;
300 break;
301 }
302
303 bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
304
305 return true;
306}
307
308/* Get the section s_flags to use for a section. */
309
310long
311ecoff_sec_to_styp_flags (name, flags)
312 CONST char *name;
313 flagword flags;
314{
315 long styp;
316
317 styp = 0;
318
319 if (strcmp (name, _TEXT) == 0)
320 styp = STYP_TEXT;
321 else if (strcmp (name, _DATA) == 0)
322 styp = STYP_DATA;
323 else if (strcmp (name, _SDATA) == 0)
324 styp = STYP_SDATA;
325 else if (strcmp (name, _RDATA) == 0)
326 styp = STYP_RDATA;
327 else if (strcmp (name, _LIT8) == 0)
328 styp = STYP_LIT8;
329 else if (strcmp (name, _LIT4) == 0)
330 styp = STYP_LIT4;
331 else if (strcmp (name, _BSS) == 0)
332 styp = STYP_BSS;
333 else if (strcmp (name, _SBSS) == 0)
334 styp = STYP_SBSS;
335 else if (strcmp (name, _INIT) == 0)
336 styp = STYP_ECOFF_INIT;
337 else if (flags & SEC_CODE)
338 styp = STYP_TEXT;
339 else if (flags & SEC_DATA)
340 styp = STYP_DATA;
341 else if (flags & SEC_READONLY)
342 styp = STYP_RDATA;
343 else if (flags & SEC_LOAD)
344 styp = STYP_REG;
345 else
346 styp = STYP_BSS;
347
348 if (flags & SEC_NEVER_LOAD)
349 styp |= STYP_NOLOAD;
350
351 return styp;
352}
353
354/* Get the BFD flags to use for a section. */
355
356flagword
357ecoff_styp_to_sec_flags (abfd, hdr)
358 bfd *abfd;
359 PTR hdr;
360{
361 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
362 long styp_flags = internal_s->s_flags;
363 flagword sec_flags=0;
364
365 if (styp_flags & STYP_NOLOAD)
366 sec_flags |= SEC_NEVER_LOAD;
367
368 /* For 386 COFF, at least, an unloadable text or data section is
369 actually a shared library section. */
370 if ((styp_flags & STYP_TEXT)
371 || (styp_flags & STYP_ECOFF_INIT))
372 {
373 if (sec_flags & SEC_NEVER_LOAD)
374 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
375 else
376 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
377 }
378 else if ((styp_flags & STYP_DATA)
379 || (styp_flags & STYP_RDATA)
380 || (styp_flags & STYP_SDATA))
381 {
382 if (sec_flags & SEC_NEVER_LOAD)
383 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
384 else
385 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
386 if (styp_flags & STYP_RDATA)
387 sec_flags |= SEC_READONLY;
388 }
389 else if ((styp_flags & STYP_BSS)
390 || (styp_flags & STYP_SBSS))
391 {
392 sec_flags |= SEC_ALLOC;
393 }
394 else if (styp_flags & STYP_INFO)
395 {
396 sec_flags |= SEC_NEVER_LOAD;
397 }
398 else if ((styp_flags & STYP_LIT8)
399 || (styp_flags & STYP_LIT4))
400 {
401 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
402 }
403 else
404 {
405 sec_flags |= SEC_ALLOC | SEC_LOAD;
406 }
407
408 return sec_flags;
409}
410\f
411/* Routines to swap auxiliary information in and out. I am assuming
412 that the auxiliary information format is always going to be target
413 independent. */
414
415/* Swap in a type information record.
416 BIGEND says whether AUX symbols are big-endian or little-endian; this
417 info comes from the file header record (fh-fBigendian). */
418
419void
420ecoff_swap_tir_in (bigend, ext_copy, intern)
421 int bigend;
422 struct tir_ext *ext_copy;
423 TIR *intern;
424{
425 struct tir_ext ext[1];
426
427 *ext = *ext_copy; /* Make it reasonable to do in-place. */
428
429 /* now the fun stuff... */
430 if (bigend) {
431 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
432 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
433 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
434 >> TIR_BITS1_BT_SH_BIG;
435 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
436 >> TIR_BITS_TQ4_SH_BIG;
437 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
438 >> TIR_BITS_TQ5_SH_BIG;
439 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
440 >> TIR_BITS_TQ0_SH_BIG;
441 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
442 >> TIR_BITS_TQ1_SH_BIG;
443 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
444 >> TIR_BITS_TQ2_SH_BIG;
445 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
446 >> TIR_BITS_TQ3_SH_BIG;
447 } else {
448 intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
449 intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
450 intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
451 >> TIR_BITS1_BT_SH_LITTLE;
452 intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
453 >> TIR_BITS_TQ4_SH_LITTLE;
454 intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
455 >> TIR_BITS_TQ5_SH_LITTLE;
456 intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
457 >> TIR_BITS_TQ0_SH_LITTLE;
458 intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
459 >> TIR_BITS_TQ1_SH_LITTLE;
460 intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
461 >> TIR_BITS_TQ2_SH_LITTLE;
462 intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
463 >> TIR_BITS_TQ3_SH_LITTLE;
464 }
465
466#ifdef TEST
467 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
468 abort();
469#endif
470}
471
472/* Swap out a type information record.
473 BIGEND says whether AUX symbols are big-endian or little-endian; this
474 info comes from the file header record (fh-fBigendian). */
475
476void
477ecoff_swap_tir_out (bigend, intern_copy, ext)
478 int bigend;
479 TIR *intern_copy;
480 struct tir_ext *ext;
481{
482 TIR intern[1];
483
484 *intern = *intern_copy; /* Make it reasonable to do in-place. */
485
486 /* now the fun stuff... */
487 if (bigend) {
488 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
489 | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
490 | ((intern->bt << TIR_BITS1_BT_SH_BIG)
491 & TIR_BITS1_BT_BIG));
492 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
493 & TIR_BITS_TQ4_BIG)
494 | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
495 & TIR_BITS_TQ5_BIG));
496 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
497 & TIR_BITS_TQ0_BIG)
498 | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
499 & TIR_BITS_TQ1_BIG));
500 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
501 & TIR_BITS_TQ2_BIG)
502 | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
503 & TIR_BITS_TQ3_BIG));
504 } else {
505 ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
506 | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
507 | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
508 & TIR_BITS1_BT_LITTLE));
509 ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
510 & TIR_BITS_TQ4_LITTLE)
511 | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
512 & TIR_BITS_TQ5_LITTLE));
513 ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
514 & TIR_BITS_TQ0_LITTLE)
515 | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
516 & TIR_BITS_TQ1_LITTLE));
517 ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
518 & TIR_BITS_TQ2_LITTLE)
519 | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
520 & TIR_BITS_TQ3_LITTLE));
521 }
522
523#ifdef TEST
524 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
525 abort();
526#endif
527}
528
529/* Swap in a relative symbol record. BIGEND says whether it is in
530 big-endian or little-endian format.*/
531
532void
533ecoff_swap_rndx_in (bigend, ext_copy, intern)
534 int bigend;
535 struct rndx_ext *ext_copy;
536 RNDXR *intern;
537{
538 struct rndx_ext ext[1];
539
540 *ext = *ext_copy; /* Make it reasonable to do in-place. */
541
542 /* now the fun stuff... */
543 if (bigend) {
544 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
545 | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
546 >> RNDX_BITS1_RFD_SH_BIG);
547 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
548 << RNDX_BITS1_INDEX_SH_LEFT_BIG)
549 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
550 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
551 } else {
552 intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
553 | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
554 << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
555 intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
556 >> RNDX_BITS1_INDEX_SH_LITTLE)
557 | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
558 | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
559 }
560
561#ifdef TEST
562 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
563 abort();
564#endif
565}
566
567/* Swap out a relative symbol record. BIGEND says whether it is in
568 big-endian or little-endian format.*/
569
570void
571ecoff_swap_rndx_out (bigend, intern_copy, ext)
572 int bigend;
573 RNDXR *intern_copy;
574 struct rndx_ext *ext;
575{
576 RNDXR intern[1];
577
578 *intern = *intern_copy; /* Make it reasonable to do in-place. */
579
580 /* now the fun stuff... */
581 if (bigend) {
582 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
583 ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
584 & RNDX_BITS1_RFD_BIG)
585 | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
586 & RNDX_BITS1_INDEX_BIG));
587 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
588 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
589 } else {
590 ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
591 ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
592 & RNDX_BITS1_RFD_LITTLE)
593 | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
594 & RNDX_BITS1_INDEX_LITTLE));
595 ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
596 ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
597 }
598
599#ifdef TEST
600 if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
601 abort();
602#endif
603}
604\f
605/* Read in and swap the important symbolic information for an ECOFF
606 object file. This is called by gdb. */
607
608boolean
609ecoff_slurp_symbolic_info (abfd)
610 bfd *abfd;
611{
612 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
613 bfd_size_type external_hdr_size;
614 HDRR *internal_symhdr;
615 bfd_size_type raw_base;
616 bfd_size_type raw_size;
617 PTR raw;
618 bfd_size_type external_fdr_size;
619 char *fraw_src;
620 char *fraw_end;
621 struct fdr *fdr_ptr;
622
623 /* Check whether we've already gotten it, and whether there's any to
624 get. */
625 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
626 return true;
627 if (ecoff_data (abfd)->sym_filepos == 0)
628 {
629 bfd_get_symcount (abfd) = 0;
630 return true;
631 }
632
633 /* At this point bfd_get_symcount (abfd) holds the number of symbols
634 as read from the file header, but on ECOFF this is always the
635 size of the symbolic information header. It would be cleaner to
636 handle this when we first read the file in coffgen.c. */
637 external_hdr_size = backend->external_hdr_size;
638 if (bfd_get_symcount (abfd) != external_hdr_size)
639 {
640 bfd_error = bad_value;
641 return false;
642 }
643
644 /* Read the symbolic information header. */
645 raw = (PTR) alloca (external_hdr_size);
646 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
647 || (bfd_read (raw, external_hdr_size, 1, abfd)
648 != external_hdr_size))
649 {
650 bfd_error = system_call_error;
651 return false;
652 }
653 internal_symhdr = &ecoff_data (abfd)->symbolic_header;
654 (*backend->swap_hdr_in) (abfd, raw, internal_symhdr);
655
656 if (internal_symhdr->magic != magicSym)
657 {
658 bfd_error = bad_value;
659 return false;
660 }
661
662 /* Now we can get the correct number of symbols. */
663 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
664 + internal_symhdr->iextMax);
665
666 /* Read all the symbolic information at once. */
667 raw_base = ecoff_data (abfd)->sym_filepos + external_hdr_size;
668
669 if (internal_symhdr->cbExtOffset != 0)
670 raw_size = (internal_symhdr->cbExtOffset
671 - raw_base
672 + internal_symhdr->iextMax * backend->external_ext_size);
673 else
674 {
675 long cbline, issmax, issextmax;
676
677 cbline = (internal_symhdr->cbLine + 3) &~ 3;
678 issmax = (internal_symhdr->issMax + 3) &~ 3;
679 issextmax = (internal_symhdr->issExtMax + 3) &~ 3;
680 raw_size = (cbline * sizeof (unsigned char)
681 + internal_symhdr->idnMax * backend->external_dnr_size
682 + internal_symhdr->ipdMax * backend->external_pdr_size
683 + internal_symhdr->isymMax * backend->external_sym_size
684 + internal_symhdr->ioptMax * backend->external_opt_size
685 + internal_symhdr->iauxMax * sizeof (union aux_ext)
686 + issmax * sizeof (char)
687 + issextmax * sizeof (char)
688 + internal_symhdr->ifdMax * backend->external_fdr_size
689 + internal_symhdr->crfd * backend->external_rfd_size
690 + internal_symhdr->iextMax * backend->external_ext_size);
691 }
692
693 if (raw_size == 0)
694 {
695 ecoff_data (abfd)->sym_filepos = 0;
696 return true;
697 }
698 raw = (PTR) bfd_alloc (abfd, raw_size);
699 if (raw == NULL)
700 {
701 bfd_error = no_memory;
702 return false;
703 }
704 if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
705 {
706 bfd_error = system_call_error;
707 bfd_release (abfd, raw);
708 return false;
709 }
710
711 ecoff_data (abfd)->raw_size = raw_size;
712 ecoff_data (abfd)->raw_syments = raw;
713
714 /* Get pointers for the numeric offsets in the HDRR structure. */
715#define FIX(off1, off2, type) \
716 if (internal_symhdr->off1 == 0) \
717 ecoff_data (abfd)->off2 = (type) NULL; \
718 else \
719 ecoff_data (abfd)->off2 = (type) ((char *) raw \
720 + internal_symhdr->off1 \
721 - raw_base)
722 FIX (cbLineOffset, line, unsigned char *);
723 FIX (cbDnOffset, external_dnr, PTR);
724 FIX (cbPdOffset, external_pdr, PTR);
725 FIX (cbSymOffset, external_sym, PTR);
726 FIX (cbOptOffset, external_opt, PTR);
727 FIX (cbAuxOffset, external_aux, union aux_ext *);
728 FIX (cbSsOffset, ss, char *);
729 FIX (cbSsExtOffset, ssext, char *);
730 FIX (cbFdOffset, external_fdr, PTR);
731 FIX (cbRfdOffset, external_rfd, PTR);
732 FIX (cbExtOffset, external_ext, PTR);
733#undef FIX
734
735 /* I don't want to always swap all the data, because it will just
736 waste time and most programs will never look at it. The only
737 time the linker needs most of the debugging information swapped
738 is when linking big-endian and little-endian MIPS object files
739 together, which is not a common occurrence.
740
741 We need to look at the fdr to deal with a lot of information in
742 the symbols, so we swap them here. */
743 ecoff_data (abfd)->fdr =
744 (struct fdr *) bfd_alloc (abfd,
745 (internal_symhdr->ifdMax *
746 sizeof (struct fdr)));
747 if (ecoff_data (abfd)->fdr == NULL)
748 {
749 bfd_error = no_memory;
750 return false;
751 }
752 external_fdr_size = backend->external_fdr_size;
753 fdr_ptr = ecoff_data (abfd)->fdr;
754 fraw_src = (char *) ecoff_data (abfd)->external_fdr;
755 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
756 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
757 (*backend->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
758
759 return true;
760}
761\f
762/* ECOFF symbol table routines. The ECOFF symbol table is described
763 in gcc/mips-tfile.c. */
764
765/* ECOFF uses two common sections. One is the usual one, and the
766 other is for small objects. All the small objects are kept
767 together, and then referenced via the gp pointer, which yields
768 faster assembler code. This is what we use for the small common
769 section. */
770static asection ecoff_scom_section;
771static asymbol ecoff_scom_symbol;
772static asymbol *ecoff_scom_symbol_ptr;
773
774/* Create an empty symbol. */
775
776asymbol *
777ecoff_make_empty_symbol (abfd)
778 bfd *abfd;
779{
780 ecoff_symbol_type *new;
781
782 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
783 if (new == (ecoff_symbol_type *) NULL)
784 {
785 bfd_error = no_memory;
786 return (asymbol *) NULL;
787 }
788 memset (new, 0, sizeof *new);
789 new->symbol.section = (asection *) NULL;
790 new->fdr = (FDR *) NULL;
791 new->local = false;
792 new->native = NULL;
793 new->symbol.the_bfd = abfd;
794 return &new->symbol;
795}
796
797/* Set the BFD flags and section for an ECOFF symbol. */
798
799static void
800ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, indirect_ptr_ptr)
801 bfd *abfd;
802 SYMR *ecoff_sym;
803 asymbol *asym;
804 int ext;
805 asymbol **indirect_ptr_ptr;
806{
807 asym->the_bfd = abfd;
808 asym->value = ecoff_sym->value;
809 asym->section = &bfd_debug_section;
810 asym->udata = NULL;
811
812 /* An indirect symbol requires two consecutive stabs symbols. */
813 if (*indirect_ptr_ptr != (asymbol *) NULL)
814 {
815 BFD_ASSERT (ECOFF_IS_STAB (ecoff_sym));
816
817 /* @@ Stuffing pointers into integers is a no-no.
818 We can usually get away with it if the integer is
819 large enough though. */
820 if (sizeof (asym) > sizeof (bfd_vma))
821 abort ();
822 (*indirect_ptr_ptr)->value = (bfd_vma) asym;
823
824 asym->flags = BSF_DEBUGGING;
825 asym->section = &bfd_und_section;
826 *indirect_ptr_ptr = NULL;
827 return;
828 }
829
830 if (ECOFF_IS_STAB (ecoff_sym)
831 && (ECOFF_UNMARK_STAB (ecoff_sym->index) | N_EXT) == (N_INDR | N_EXT))
832 {
833 asym->flags = BSF_DEBUGGING | BSF_INDIRECT;
834 asym->section = &bfd_ind_section;
835 /* Pass this symbol on to the next call to this function. */
836 *indirect_ptr_ptr = asym;
837 return;
838 }
839
840 /* Most symbol types are just for debugging. */
841 switch (ecoff_sym->st)
842 {
843 case stGlobal:
844 case stStatic:
845 case stLabel:
846 case stProc:
847 case stStaticProc:
848 break;
849 case stNil:
850 if (ECOFF_IS_STAB (ecoff_sym))
851 {
852 asym->flags = BSF_DEBUGGING;
853 return;
854 }
855 break;
856 default:
857 asym->flags = BSF_DEBUGGING;
858 return;
859 }
860
861 if (ext)
862 asym->flags = BSF_EXPORT | BSF_GLOBAL;
863 else
864 asym->flags = BSF_LOCAL;
865 switch (ecoff_sym->sc)
866 {
867 case scNil:
868 /* Used for compiler generated labels. Leave them in the
869 debugging section, and mark them as local. If BSF_DEBUGGING
870 is set, then nm does not display them for some reason. If no
871 flags are set then the linker whines about them. */
872 asym->flags = BSF_LOCAL;
873 break;
874 case scText:
875 asym->section = bfd_make_section_old_way (abfd, ".text");
876 asym->value -= asym->section->vma;
877 break;
878 case scData:
879 asym->section = bfd_make_section_old_way (abfd, ".data");
880 asym->value -= asym->section->vma;
881 break;
882 case scBss:
883 if (ext)
884 {
885 asym->section = &bfd_com_section;
886 asym->flags = 0;
887 }
888 else
889 {
890 asym->section = bfd_make_section_old_way (abfd, ".bss");
891 asym->value -= asym->section->vma;
892 }
893 break;
894 case scRegister:
895 asym->flags = BSF_DEBUGGING;
896 break;
897 case scAbs:
898 asym->section = &bfd_abs_section;
899 break;
900 case scUndefined:
901 asym->section = &bfd_und_section;
902 asym->flags = 0;
903 asym->value = 0;
904 break;
905 case scCdbLocal:
906 case scBits:
907 case scCdbSystem:
908 case scRegImage:
909 case scInfo:
910 case scUserStruct:
911 asym->flags = BSF_DEBUGGING;
912 break;
913 case scSData:
914 asym->section = bfd_make_section_old_way (abfd, ".sdata");
915 asym->value -= asym->section->vma;
916 break;
917 case scSBss:
918 asym->section = bfd_make_section_old_way (abfd, ".sbss");
919 if (! ext)
920 asym->value -= asym->section->vma;
921 break;
922 case scRData:
923 asym->section = bfd_make_section_old_way (abfd, ".rdata");
924 asym->value -= asym->section->vma;
925 break;
926 case scVar:
927 asym->flags = BSF_DEBUGGING;
928 break;
929 case scCommon:
930 if (asym->value > ecoff_data (abfd)->gp_size)
931 {
932 asym->section = &bfd_com_section;
933 asym->flags = 0;
934 break;
935 }
936 /* Fall through. */
937 case scSCommon:
938 if (ecoff_scom_section.name == NULL)
939 {
940 /* Initialize the small common section. */
941 ecoff_scom_section.name = SCOMMON;
942 ecoff_scom_section.flags = SEC_IS_COMMON;
943 ecoff_scom_section.output_section = &ecoff_scom_section;
944 ecoff_scom_section.symbol = &ecoff_scom_symbol;
945 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
946 ecoff_scom_symbol.name = SCOMMON;
947 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
948 ecoff_scom_symbol.section = &ecoff_scom_section;
949 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
950 }
951 asym->section = &ecoff_scom_section;
952 asym->flags = 0;
953 break;
954 case scVarRegister:
955 case scVariant:
956 asym->flags = BSF_DEBUGGING;
957 break;
958 case scSUndefined:
959 asym->section = &bfd_und_section;
960 asym->flags = 0;
961 asym->value = 0;
962 break;
963 case scInit:
964 asym->section = bfd_make_section_old_way (abfd, ".init");
965 asym->value -= asym->section->vma;
966 break;
967 case scBasedVar:
968 case scXData:
969 case scPData:
970 asym->flags = BSF_DEBUGGING;
971 break;
972 case scFini:
973 asym->section = bfd_make_section_old_way (abfd, ".fini");
974 asym->value -= asym->section->vma;
975 break;
976 default:
977 break;
978 }
979
980 /* Look for special constructors symbols and make relocation entries
981 in a special construction section. These are produced by the
982 -fgnu-linker argument to g++. */
983 if (ECOFF_IS_STAB (ecoff_sym))
984 {
985 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
986 {
987 default:
988 break;
989
990 case N_SETA:
991 case N_SETT:
992 case N_SETD:
993 case N_SETB:
994 {
995 const char *name;
996 asection *section;
997 arelent_chain *reloc_chain;
998 unsigned int bitsize;
999 int reloc_index;
1000
1001 /* Get a section with the same name as the symbol (usually
1002 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
1003 name ___CTOR_LIST (three underscores). We need
1004 __CTOR_LIST (two underscores), since ECOFF doesn't use
1005 a leading underscore. This should be handled by gcc,
1006 but instead we do it here. Actually, this should all
1007 be done differently anyhow. */
1008 name = bfd_asymbol_name (asym);
1009 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
1010 {
1011 ++name;
1012 asym->name = name;
1013 }
1014 section = bfd_get_section_by_name (abfd, name);
1015 if (section == (asection *) NULL)
1016 {
1017 char *copy;
1018
1019 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
1020 strcpy (copy, name);
1021 section = bfd_make_section (abfd, copy);
1022 }
1023
1024 /* Build a reloc pointing to this constructor. */
1025 reloc_chain =
1026 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1027 reloc_chain->relent.sym_ptr_ptr =
1028 bfd_get_section (asym)->symbol_ptr_ptr;
1029 reloc_chain->relent.address = section->_raw_size;
1030 reloc_chain->relent.addend = asym->value;
1031
1032 bitsize = ecoff_backend (abfd)->constructor_bitsize;
1033 switch (bitsize)
1034 {
1035 case 32:
1036 reloc_index = ECOFF_R_REFWORD;
1037 break;
1038 case 64:
1039 abort ();
1040 default:
1041 abort ();
1042 }
1043 reloc_chain->relent.howto = ecoff_howto_table + reloc_index;
1044
1045 /* Set up the constructor section to hold the reloc. */
1046 section->flags = SEC_CONSTRUCTOR;
1047 ++section->reloc_count;
1048
1049 /* Constructor sections must be rounded to a boundary
1050 based on the bitsize. These are not real sections--
1051 they are handled specially by the linker--so the ECOFF
1052 16 byte alignment restriction does not apply. */
1053 section->alignment_power = 1;
1054 while ((1 << section->alignment_power) < bitsize / 8)
1055 ++section->alignment_power;
1056
1057 reloc_chain->next = section->constructor_chain;
1058 section->constructor_chain = reloc_chain;
1059 section->_raw_size += bitsize / 8;
1060
1061 /* Mark the symbol as a constructor. */
1062 asym->flags |= BSF_CONSTRUCTOR;
1063 }
1064 break;
1065 }
1066 }
1067}
1068
1069/* Read an ECOFF symbol table. */
1070
1071boolean
1072ecoff_slurp_symbol_table (abfd)
1073 bfd *abfd;
1074{
1075 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1076 const bfd_size_type external_ext_size = backend->external_ext_size;
1077 const bfd_size_type external_sym_size = backend->external_sym_size;
1078 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
1079 = backend->swap_ext_in;
1080 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
1081 = backend->swap_sym_in;
1082 bfd_size_type internal_size;
1083 ecoff_symbol_type *internal;
1084 ecoff_symbol_type *internal_ptr;
1085 asymbol *indirect_ptr;
1086 char *eraw_src;
1087 char *eraw_end;
1088 FDR *fdr_ptr;
1089 FDR *fdr_end;
1090
1091 /* If we've already read in the symbol table, do nothing. */
1092 if (ecoff_data (abfd)->canonical_symbols != NULL)
1093 return true;
1094
1095 /* Get the symbolic information. */
1096 if (ecoff_slurp_symbolic_info (abfd) == false)
1097 return false;
1098 if (bfd_get_symcount (abfd) == 0)
1099 return true;
1100
1101 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1102 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1103 if (internal == NULL)
1104 {
1105 bfd_error = no_memory;
1106 return false;
1107 }
1108
1109 internal_ptr = internal;
1110 indirect_ptr = NULL;
1111 eraw_src = (char *) ecoff_data (abfd)->external_ext;
1112 eraw_end = (eraw_src
1113 + (ecoff_data (abfd)->symbolic_header.iextMax
1114 * external_ext_size));
1115 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
1116 {
1117 EXTR internal_esym;
1118
1119 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
1120 internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
1121 + internal_esym.asym.iss);
1122 ecoff_set_symbol_info (abfd, &internal_esym.asym,
1123 &internal_ptr->symbol, 1, &indirect_ptr);
1124 internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
1125 internal_ptr->local = false;
1126 internal_ptr->native = (PTR) eraw_src;
1127 }
1128 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1129
1130 /* The local symbols must be accessed via the fdr's, because the
1131 string and aux indices are relative to the fdr information. */
1132 fdr_ptr = ecoff_data (abfd)->fdr;
1133 fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
1134 for (; fdr_ptr < fdr_end; fdr_ptr++)
1135 {
1136 char *lraw_src;
1137 char *lraw_end;
1138
1139 lraw_src = ((char *) ecoff_data (abfd)->external_sym
1140 + fdr_ptr->isymBase * external_sym_size);
1141 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1142 for (;
1143 lraw_src < lraw_end;
1144 lraw_src += external_sym_size, internal_ptr++)
1145 {
1146 SYMR internal_sym;
1147
1148 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1149 internal_ptr->symbol.name = (ecoff_data (abfd)->ss
1150 + fdr_ptr->issBase
1151 + internal_sym.iss);
1152 ecoff_set_symbol_info (abfd, &internal_sym,
1153 &internal_ptr->symbol, 0, &indirect_ptr);
1154 internal_ptr->fdr = fdr_ptr;
1155 internal_ptr->local = true;
1156 internal_ptr->native = (PTR) lraw_src;
1157 }
1158 }
1159 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1160
1161 ecoff_data (abfd)->canonical_symbols = internal;
1162
1163 return true;
1164}
1165
1166/* Return the amount of space needed for the canonical symbols. */
1167
1168unsigned int
1169ecoff_get_symtab_upper_bound (abfd)
1170 bfd *abfd;
1171{
1172 if (ecoff_slurp_symbolic_info (abfd) == false
1173 || bfd_get_symcount (abfd) == 0)
1174 return 0;
1175
1176 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1177}
1178
1179/* Get the canonicals symbols. */
1180
1181unsigned int
1182ecoff_get_symtab (abfd, alocation)
1183 bfd *abfd;
1184 asymbol **alocation;
1185{
1186 unsigned int counter = 0;
1187 ecoff_symbol_type *symbase;
1188 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1189
1190 if (ecoff_slurp_symbol_table (abfd) == false
1191 || bfd_get_symcount (abfd) == 0)
1192 return 0;
1193
1194 symbase = ecoff_data (abfd)->canonical_symbols;
1195 while (counter < bfd_get_symcount (abfd))
1196 {
1197 *(location++) = symbase++;
1198 counter++;
1199 }
1200 *location++ = (ecoff_symbol_type *) NULL;
1201 return bfd_get_symcount (abfd);
1202}
1203
1204/* Turn ECOFF type information into a printable string.
1205 ecoff_emit_aggregate and ecoff_type_to_string are from
1206 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1207
1208/* Write aggregate information to a string. */
1209
1210static void
1211ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1212 bfd *abfd;
1213 char *string;
1214 RNDXR *rndx;
1215 long isym;
1216 CONST char *which;
1217{
1218 int ifd = rndx->rfd;
1219 int indx = rndx->index;
1220 int sym_base, ss_base;
1221 CONST char *name;
1222
1223 if (ifd == 0xfff)
1224 ifd = isym;
1225
1226 sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
1227 ss_base = ecoff_data (abfd)->fdr[ifd].issBase;
1228
1229 if (indx == indexNil)
1230 name = "/* no name */";
1231 else
1232 {
1233 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1234 SYMR sym;
1235
1236 indx += sym_base;
1237 (*backend->swap_sym_in) (abfd,
1238 ((char *) ecoff_data (abfd)->external_sym
1239 + indx * backend->external_sym_size),
1240 &sym);
1241 name = ecoff_data (abfd)->ss + ss_base + sym.iss;
1242 }
1243
1244 sprintf (string,
1245 "%s %s { ifd = %d, index = %d }",
1246 which, name, ifd,
1247 indx + ecoff_data (abfd)->symbolic_header.iextMax);
1248}
1249
1250/* Convert the type information to string format. */
1251
1252static char *
1253ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1254 bfd *abfd;
1255 union aux_ext *aux_ptr;
1256 unsigned int indx;
1257 int bigendian;
1258{
1259 AUXU u;
1260 struct qual {
1261 unsigned int type;
1262 int low_bound;
1263 int high_bound;
1264 int stride;
1265 } qualifiers[7];
1266
1267 unsigned int basic_type;
1268 int i;
1269 static char buffer1[1024];
1270 static char buffer2[1024];
1271 char *p1 = buffer1;
1272 char *p2 = buffer2;
1273 RNDXR rndx;
1274
1275 for (i = 0; i < 7; i++)
1276 {
1277 qualifiers[i].low_bound = 0;
1278 qualifiers[i].high_bound = 0;
1279 qualifiers[i].stride = 0;
1280 }
1281
1282 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1283 return "-1 (no type)";
1284 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1285
1286 basic_type = u.ti.bt;
1287 qualifiers[0].type = u.ti.tq0;
1288 qualifiers[1].type = u.ti.tq1;
1289 qualifiers[2].type = u.ti.tq2;
1290 qualifiers[3].type = u.ti.tq3;
1291 qualifiers[4].type = u.ti.tq4;
1292 qualifiers[5].type = u.ti.tq5;
1293 qualifiers[6].type = tqNil;
1294
1295 /*
1296 * Go get the basic type.
1297 */
1298 switch (basic_type)
1299 {
1300 case btNil: /* undefined */
1301 strcpy (p1, "nil");
1302 break;
1303
1304 case btAdr: /* address - integer same size as pointer */
1305 strcpy (p1, "address");
1306 break;
1307
1308 case btChar: /* character */
1309 strcpy (p1, "char");
1310 break;
1311
1312 case btUChar: /* unsigned character */
1313 strcpy (p1, "unsigned char");
1314 break;
1315
1316 case btShort: /* short */
1317 strcpy (p1, "short");
1318 break;
1319
1320 case btUShort: /* unsigned short */
1321 strcpy (p1, "unsigned short");
1322 break;
1323
1324 case btInt: /* int */
1325 strcpy (p1, "int");
1326 break;
1327
1328 case btUInt: /* unsigned int */
1329 strcpy (p1, "unsigned int");
1330 break;
1331
1332 case btLong: /* long */
1333 strcpy (p1, "long");
1334 break;
1335
1336 case btULong: /* unsigned long */
1337 strcpy (p1, "unsigned long");
1338 break;
1339
1340 case btFloat: /* float (real) */
1341 strcpy (p1, "float");
1342 break;
1343
1344 case btDouble: /* Double (real) */
1345 strcpy (p1, "double");
1346 break;
1347
1348 /* Structures add 1-2 aux words:
1349 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1350 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1351
1352 case btStruct: /* Structure (Record) */
1353 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1354 ecoff_emit_aggregate (abfd, p1, &rndx,
1355 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1356 "struct");
1357 indx++; /* skip aux words */
1358 break;
1359
1360 /* Unions add 1-2 aux words:
1361 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1362 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1363
1364 case btUnion: /* Union */
1365 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1366 ecoff_emit_aggregate (abfd, p1, &rndx,
1367 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1368 "union");
1369 indx++; /* skip aux words */
1370 break;
1371
1372 /* Enumerations add 1-2 aux words:
1373 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1374 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1375
1376 case btEnum: /* Enumeration */
1377 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1378 ecoff_emit_aggregate (abfd, p1, &rndx,
1379 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1380 "enum");
1381 indx++; /* skip aux words */
1382 break;
1383
1384 case btTypedef: /* defined via a typedef, isymRef points */
1385 strcpy (p1, "typedef");
1386 break;
1387
1388 case btRange: /* subrange of int */
1389 strcpy (p1, "subrange");
1390 break;
1391
1392 case btSet: /* pascal sets */
1393 strcpy (p1, "set");
1394 break;
1395
1396 case btComplex: /* fortran complex */
1397 strcpy (p1, "complex");
1398 break;
1399
1400 case btDComplex: /* fortran double complex */
1401 strcpy (p1, "double complex");
1402 break;
1403
1404 case btIndirect: /* forward or unnamed typedef */
1405 strcpy (p1, "forward/unamed typedef");
1406 break;
1407
1408 case btFixedDec: /* Fixed Decimal */
1409 strcpy (p1, "fixed decimal");
1410 break;
1411
1412 case btFloatDec: /* Float Decimal */
1413 strcpy (p1, "float decimal");
1414 break;
1415
1416 case btString: /* Varying Length Character String */
1417 strcpy (p1, "string");
1418 break;
1419
1420 case btBit: /* Aligned Bit String */
1421 strcpy (p1, "bit");
1422 break;
1423
1424 case btPicture: /* Picture */
1425 strcpy (p1, "picture");
1426 break;
1427
1428 case btVoid: /* Void */
1429 strcpy (p1, "void");
1430 break;
1431
1432 default:
1433 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1434 break;
1435 }
1436
1437 p1 += strlen (buffer1);
1438
1439 /*
1440 * If this is a bitfield, get the bitsize.
1441 */
1442 if (u.ti.fBitfield)
1443 {
1444 int bitsize;
1445
1446 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1447 sprintf (p1, " : %d", bitsize);
1448 p1 += strlen (buffer1);
1449 }
1450
1451
1452 /*
1453 * Deal with any qualifiers.
1454 */
1455 if (qualifiers[0].type != tqNil)
1456 {
1457 /*
1458 * Snarf up any array bounds in the correct order. Arrays
1459 * store 5 successive words in the aux. table:
1460 * word 0 RNDXR to type of the bounds (ie, int)
1461 * word 1 Current file descriptor index
1462 * word 2 low bound
1463 * word 3 high bound (or -1 if [])
1464 * word 4 stride size in bits
1465 */
1466 for (i = 0; i < 7; i++)
1467 {
1468 if (qualifiers[i].type == tqArray)
1469 {
1470 qualifiers[i].low_bound =
1471 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1472 qualifiers[i].high_bound =
1473 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1474 qualifiers[i].stride =
1475 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1476 indx += 5;
1477 }
1478 }
1479
1480 /*
1481 * Now print out the qualifiers.
1482 */
1483 for (i = 0; i < 6; i++)
1484 {
1485 switch (qualifiers[i].type)
1486 {
1487 case tqNil:
1488 case tqMax:
1489 break;
1490
1491 case tqPtr:
1492 strcpy (p2, "ptr to ");
1493 p2 += sizeof ("ptr to ")-1;
1494 break;
1495
1496 case tqVol:
1497 strcpy (p2, "volatile ");
1498 p2 += sizeof ("volatile ")-1;
1499 break;
1500
1501 case tqFar:
1502 strcpy (p2, "far ");
1503 p2 += sizeof ("far ")-1;
1504 break;
1505
1506 case tqProc:
1507 strcpy (p2, "func. ret. ");
1508 p2 += sizeof ("func. ret. ");
1509 break;
1510
1511 case tqArray:
1512 {
1513 int first_array = i;
1514 int j;
1515
1516 /* Print array bounds reversed (ie, in the order the C
1517 programmer writes them). C is such a fun language.... */
1518
1519 while (i < 5 && qualifiers[i+1].type == tqArray)
1520 i++;
1521
1522 for (j = i; j >= first_array; j--)
1523 {
1524 strcpy (p2, "array [");
1525 p2 += sizeof ("array [")-1;
1526 if (qualifiers[j].low_bound != 0)
1527 sprintf (p2,
1528 "%ld:%ld {%ld bits}",
1529 (long) qualifiers[j].low_bound,
1530 (long) qualifiers[j].high_bound,
1531 (long) qualifiers[j].stride);
1532
1533 else if (qualifiers[j].high_bound != -1)
1534 sprintf (p2,
1535 "%ld {%ld bits}",
1536 (long) (qualifiers[j].high_bound + 1),
1537 (long) (qualifiers[j].stride));
1538
1539 else
1540 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1541
1542 p2 += strlen (p2);
1543 strcpy (p2, "] of ");
1544 p2 += sizeof ("] of ")-1;
1545 }
1546 }
1547 break;
1548 }
1549 }
1550 }
1551
1552 strcpy (p2, buffer1);
1553 return buffer2;
1554}
1555
1556/* Return information about ECOFF symbol SYMBOL in RET. */
1557
1558void
1559ecoff_get_symbol_info (abfd, symbol, ret)
1560 bfd *abfd; /* Ignored. */
1561 asymbol *symbol;
1562 symbol_info *ret;
1563{
1564 bfd_symbol_info (symbol, ret);
1565}
1566
1567/* Print information about an ECOFF symbol. */
1568
1569void
1570ecoff_print_symbol (abfd, filep, symbol, how)
1571 bfd *abfd;
1572 PTR filep;
1573 asymbol *symbol;
1574 bfd_print_symbol_type how;
1575{
1576 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1577 FILE *file = (FILE *)filep;
1578
1579 switch (how)
1580 {
1581 case bfd_print_symbol_name:
1582 fprintf (file, "%s", symbol->name);
1583 break;
1584 case bfd_print_symbol_more:
1585 if (ecoffsymbol (symbol)->local)
1586 {
1587 SYMR ecoff_sym;
1588
1589 (*backend->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1590 &ecoff_sym);
1591 fprintf (file, "ecoff local ");
1592 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1593 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1594 (unsigned) ecoff_sym.sc);
1595 }
1596 else
1597 {
1598 EXTR ecoff_ext;
1599
1600 (*backend->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1601 &ecoff_ext);
1602 fprintf (file, "ecoff extern ");
1603 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1604 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1605 (unsigned) ecoff_ext.asym.sc);
1606 }
1607 break;
1608 case bfd_print_symbol_all:
1609 /* Print out the symbols in a reasonable way */
1610 {
1611 char type;
1612 int pos;
1613 EXTR ecoff_ext;
1614 char jmptbl;
1615 char cobol_main;
1616 char weakext;
1617
1618 if (ecoffsymbol (symbol)->local)
1619 {
1620 (*backend->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1621 &ecoff_ext.asym);
1622 type = 'l';
1623 pos = ((((char *) ecoffsymbol (symbol)->native
1624 - (char *) ecoff_data (abfd)->external_sym)
1625 / backend->external_sym_size)
1626 + ecoff_data (abfd)->symbolic_header.iextMax);
1627 jmptbl = ' ';
1628 cobol_main = ' ';
1629 weakext = ' ';
1630 }
1631 else
1632 {
1633 (*backend->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1634 &ecoff_ext);
1635 type = 'e';
1636 pos = (((char *) ecoffsymbol (symbol)->native
1637 - (char *) ecoff_data (abfd)->external_ext)
1638 / backend->external_ext_size);
1639 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1640 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1641 weakext = ecoff_ext.weakext ? 'w' : ' ';
1642 }
1643
1644 fprintf (file, "[%3d] %c ",
1645 pos, type);
1646 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1647 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1648 (unsigned) ecoff_ext.asym.st,
1649 (unsigned) ecoff_ext.asym.sc,
1650 (unsigned) ecoff_ext.asym.index,
1651 jmptbl, cobol_main, weakext,
1652 symbol->name);
1653
1654 if (ecoffsymbol (symbol)->fdr != NULL
1655 && ecoff_ext.asym.index != indexNil)
1656 {
1657 unsigned int indx;
1658 int bigendian;
1659 bfd_size_type sym_base;
1660 union aux_ext *aux_base;
1661
1662 indx = ecoff_ext.asym.index;
1663
1664 /* sym_base is used to map the fdr relative indices which
1665 appear in the file to the position number which we are
1666 using. */
1667 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1668 if (ecoffsymbol (symbol)->local)
1669 sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1670
1671 /* aux_base is the start of the aux entries for this file;
1672 asym.index is an offset from this. */
1673 aux_base = (ecoff_data (abfd)->external_aux
1674 + ecoffsymbol (symbol)->fdr->iauxBase);
1675
1676 /* The aux entries are stored in host byte order; the
1677 order is indicated by a bit in the fdr. */
1678 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1679
1680 /* This switch is basically from gcc/mips-tdump.c */
1681 switch (ecoff_ext.asym.st)
1682 {
1683 case stNil:
1684 case stLabel:
1685 break;
1686
1687 case stFile:
1688 case stBlock:
1689 fprintf (file, "\n End+1 symbol: %ld",
1690 (long) (indx + sym_base));
1691 break;
1692
1693 case stEnd:
1694 if (ecoff_ext.asym.sc == scText
1695 || ecoff_ext.asym.sc == scInfo)
1696 fprintf (file, "\n First symbol: %ld",
1697 (long) (indx + sym_base));
1698 else
1699 fprintf (file, "\n First symbol: %ld",
1700 (long) (AUX_GET_ISYM (bigendian,
1701 &aux_base[ecoff_ext.asym.index])
1702 + sym_base));
1703 break;
1704
1705 case stProc:
1706 case stStaticProc:
1707 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1708 ;
1709 else if (ecoffsymbol (symbol)->local)
1710 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1711 (long) (AUX_GET_ISYM (bigendian,
1712 &aux_base[ecoff_ext.asym.index])
1713 + sym_base),
1714 ecoff_type_to_string (abfd, aux_base, indx + 1,
1715 bigendian));
1716 else
1717 fprintf (file, "\n Local symbol: %d",
1718 (indx
1719 + sym_base
1720 + ecoff_data (abfd)->symbolic_header.iextMax));
1721 break;
1722
1723 default:
1724 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1725 fprintf (file, "\n Type: %s",
1726 ecoff_type_to_string (abfd, aux_base, indx,
1727 bigendian));
1728 break;
1729 }
1730 }
1731 }
1732 break;
1733 }
1734}
1735\f
1736/* ECOFF relocs are either against external symbols, or against
1737 sections. If we are producing relocateable output, and the reloc
1738 is against an external symbol, and nothing has given us any
1739 additional addend, the resulting reloc will also be against the
1740 same symbol. In such a case, we don't want to change anything
1741 about the way the reloc is handled, since it will all be done at
1742 final link time. Rather than put special case code into
1743 bfd_perform_relocation, all the reloc types use this howto
1744 function. It just short circuits the reloc if producing
1745 relocateable output against an external symbol. */
1746
1747static bfd_reloc_status_type
1748ecoff_generic_reloc (abfd,
1749 reloc_entry,
1750 symbol,
1751 data,
1752 input_section,
1753 output_bfd)
1754 bfd *abfd;
1755 arelent *reloc_entry;
1756 asymbol *symbol;
1757 PTR data;
1758 asection *input_section;
1759 bfd *output_bfd;
1760{
1761 if (output_bfd != (bfd *) NULL
1762 && (symbol->flags & BSF_SECTION_SYM) == 0
1763 && reloc_entry->addend == 0)
1764 {
1765 reloc_entry->address += input_section->output_offset;
1766 return bfd_reloc_ok;
1767 }
1768
1769 return bfd_reloc_continue;
1770}
1771
1772/* Do a REFHI relocation. This has to be done in combination with a
1773 REFLO reloc, because there is a carry from the REFLO to the REFHI.
1774 Here we just save the information we need; we do the actual
1775 relocation when we see the REFLO. ECOFF requires that the REFLO
1776 immediately follow the REFHI, so this ought to work. */
1777
1778static bfd_byte *ecoff_refhi_addr;
1779static bfd_vma ecoff_refhi_addend;
1780
1781static bfd_reloc_status_type
1782ecoff_refhi_reloc (abfd,
1783 reloc_entry,
1784 symbol,
1785 data,
1786 input_section,
1787 output_bfd)
1788 bfd *abfd;
1789 arelent *reloc_entry;
1790 asymbol *symbol;
1791 PTR data;
1792 asection *input_section;
1793 bfd *output_bfd;
1794{
1795 bfd_reloc_status_type ret;
1796 bfd_vma relocation;
1797
1798 /* If we're relocating, and this an external symbol, we don't want
1799 to change anything. */
1800 if (output_bfd != (bfd *) NULL
1801 && (symbol->flags & BSF_SECTION_SYM) == 0
1802 && reloc_entry->addend == 0)
1803 {
1804 reloc_entry->address += input_section->output_offset;
1805 return bfd_reloc_ok;
1806 }
1807
1808 ret = bfd_reloc_ok;
1809 if (symbol->section == &bfd_und_section
1810 && output_bfd == (bfd *) NULL)
1811 ret = bfd_reloc_undefined;
1812
1813 if (bfd_is_com_section (symbol->section))
1814 relocation = 0;
1815 else
1816 relocation = symbol->value;
1817
1818 relocation += symbol->section->output_section->vma;
1819 relocation += symbol->section->output_offset;
1820 relocation += reloc_entry->addend;
1821
1822 if (reloc_entry->address > input_section->_cooked_size)
1823 return bfd_reloc_outofrange;
1824
1825 /* Save the information, and let REFLO do the actual relocation. */
1826 ecoff_refhi_addr = (bfd_byte *) data + reloc_entry->address;
1827 ecoff_refhi_addend = relocation;
1828
1829 if (output_bfd != (bfd *) NULL)
1830 reloc_entry->address += input_section->output_offset;
1831
1832 return ret;
1833}
1834
1835/* Do a REFLO relocation. This is a straightforward 16 bit inplace
1836 relocation; this function exists in order to do the REFHI
1837 relocation described above. */
1838
1839static bfd_reloc_status_type
1840ecoff_reflo_reloc (abfd,
1841 reloc_entry,
1842 symbol,
1843 data,
1844 input_section,
1845 output_bfd)
1846 bfd *abfd;
1847 arelent *reloc_entry;
1848 asymbol *symbol;
1849 PTR data;
1850 asection *input_section;
1851 bfd *output_bfd;
1852{
1853 if (ecoff_refhi_addr != (bfd_byte *) NULL)
1854 {
1855 unsigned long insn;
1856 unsigned long val;
1857 unsigned long vallo;
1858
1859 /* Do the REFHI relocation. Note that we actually don't need to
1860 know anything about the REFLO itself, except where to find
1861 the low 16 bits of the addend needed by the REFHI. */
1862 insn = bfd_get_32 (abfd, ecoff_refhi_addr);
1863 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1864 & 0xffff);
1865 val = ((insn & 0xffff) << 16) + vallo;
1866 val += ecoff_refhi_addend;
1867
1868 /* The low order 16 bits are always treated as a signed value.
1869 Therefore, a negative value in the low order bits requires an
1870 adjustment in the high order bits. We need to make this
1871 adjustment in two ways: once for the bits we took from the
1872 data, and once for the bits we are putting back in to the
1873 data. */
1874 if ((vallo & 0x8000) != 0)
1875 val -= 0x10000;
1876 if ((val & 0x8000) != 0)
1877 val += 0x10000;
1878
1879 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1880 bfd_put_32 (abfd, insn, ecoff_refhi_addr);
1881
1882 ecoff_refhi_addr = (bfd_byte *) NULL;
1883 }
1884
1885 /* Now do the REFLO reloc in the usual way. */
1886 return ecoff_generic_reloc (abfd, reloc_entry, symbol, data,
1887 input_section, output_bfd);
1888}
1889
1890/* Do a GPREL relocation. This is a 16 bit value which must become
1891 the offset from the gp register. */
1892
1893static bfd_reloc_status_type
1894ecoff_gprel_reloc (abfd,
1895 reloc_entry,
1896 symbol,
1897 data,
1898 input_section,
1899 output_bfd)
1900 bfd *abfd;
1901 arelent *reloc_entry;
1902 asymbol *symbol;
1903 PTR data;
1904 asection *input_section;
1905 bfd *output_bfd;
1906{
1907 boolean relocateable;
1908 bfd_vma relocation;
1909 unsigned long val;
1910 unsigned long insn;
1911
1912 /* If we're relocating, and this is an external symbol with no
1913 addend, we don't want to change anything. We will only have an
1914 addend if this is a newly created reloc, not read from an ECOFF
1915 file. */
1916 if (output_bfd != (bfd *) NULL
1917 && (symbol->flags & BSF_SECTION_SYM) == 0
1918 && reloc_entry->addend == 0)
1919 {
1920 reloc_entry->address += input_section->output_offset;
1921 return bfd_reloc_ok;
1922 }
1923
1924 if (output_bfd != (bfd *) NULL)
1925 relocateable = true;
1926 else
1927 {
1928 relocateable = false;
1929 output_bfd = symbol->section->output_section->owner;
1930 }
1931
1932 if (symbol->section == &bfd_und_section
1933 && relocateable == false)
1934 return bfd_reloc_undefined;
1935
1936 /* We have to figure out the gp value, so that we can adjust the
1937 symbol value correctly. We look up the symbol _gp in the output
1938 BFD. If we can't find it, we're stuck. We cache it in the ECOFF
1939 target data. We don't need to adjust the symbol value for an
1940 external symbol if we are producing relocateable output. */
1941 if (ecoff_data (output_bfd)->gp == 0
1942 && (relocateable == false
1943 || (symbol->flags & BSF_SECTION_SYM) != 0))
1944 {
1945 if (relocateable != false)
1946 {
1947 /* Make up a value. */
1948 ecoff_data (output_bfd)->gp =
1949 symbol->section->output_section->vma + 0x4000;
1950 }
1951 else
1952 {
1953 unsigned int count;
1954 asymbol **sym;
1955 unsigned int i;
1956
1957 count = bfd_get_symcount (output_bfd);
1958 sym = bfd_get_outsymbols (output_bfd);
1959
1960 /* We should do something more friendly here, but we don't
1961 have a good reloc status to return. */
1962 if (sym == (asymbol **) NULL)
1963 abort ();
1964
1965 for (i = 0; i < count; i++, sym++)
1966 {
1967 register CONST char *name;
1968
1969 name = bfd_asymbol_name (*sym);
1970 if (*name == '_' && strcmp (name, "_gp") == 0)
1971 {
1972 ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
1973 break;
1974 }
1975 }
1976
1977 /* We should do something more friendly here, but we don't have
1978 a good reloc status to return. */
1979 if (i >= count)
1980 abort ();
1981 }
1982 }
1983
1984 if (bfd_is_com_section (symbol->section))
1985 relocation = 0;
1986 else
1987 relocation = symbol->value;
1988
1989 relocation += symbol->section->output_section->vma;
1990 relocation += symbol->section->output_offset;
1991
1992 if (reloc_entry->address > input_section->_cooked_size)
1993 return bfd_reloc_outofrange;
1994
1995 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1996
1997 /* Set val to the offset into the section or symbol. */
1998 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1999 if (val & 0x8000)
2000 val -= 0x10000;
2001
2002 /* Adjust val for the final section location and GP value. If we
2003 are producing relocateable output, we don't want to do this for
2004 an external symbol. */
2005 if (relocateable == false
2006 || (symbol->flags & BSF_SECTION_SYM) != 0)
2007 val += relocation - ecoff_data (output_bfd)->gp;
2008
2009 insn = (insn &~ 0xffff) | (val & 0xffff);
2010 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
2011
2012 if (relocateable != false)
2013 reloc_entry->address += input_section->output_offset;
2014
2015 /* Make sure it fit in 16 bits. */
2016 if (val >= 0x8000 && val < 0xffff8000)
2017 return bfd_reloc_outofrange;
2018
2019 return bfd_reloc_ok;
2020}
2021
2022/* Read in the relocs for a section. */
2023
2024static boolean
2025ecoff_slurp_reloc_table (abfd, section, symbols)
2026 bfd *abfd;
2027 asection *section;
2028 asymbol **symbols;
2029{
2030 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2031 arelent *internal_relocs;
2032 bfd_size_type external_reloc_size;
2033 bfd_size_type external_relocs_size;
2034 char *external_relocs;
2035 arelent *rptr;
2036 unsigned int i;
2037
2038 if (section->relocation != (arelent *) NULL
2039 || section->reloc_count == 0
2040 || (section->flags & SEC_CONSTRUCTOR) != 0)
2041 return true;
2042
2043 if (ecoff_slurp_symbol_table (abfd) == false)
2044 return false;
2045
2046 internal_relocs = (arelent *) bfd_alloc (abfd,
2047 (sizeof (arelent)
2048 * section->reloc_count));
2049 external_reloc_size = backend->external_reloc_size;
2050 external_relocs_size = external_reloc_size * section->reloc_count;
2051 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
2052 if (internal_relocs == (arelent *) NULL
2053 || external_relocs == (char *) NULL)
2054 {
2055 bfd_error = no_memory;
2056 return false;
2057 }
2058 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
2059 return false;
2060 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
2061 != external_relocs_size)
2062 {
2063 bfd_error = system_call_error;
2064 return false;
2065 }
2066
2067 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
2068 {
2069 struct internal_reloc intern;
2070
2071 (*backend->swap_reloc_in) (abfd,
2072 external_relocs + i * external_reloc_size,
2073 &intern);
2074
2075 if (intern.r_type > ECOFF_R_LITERAL)
2076 abort ();
2077
2078 if (intern.r_extern)
2079 {
2080 /* r_symndx is an index into the external symbols. */
2081 BFD_ASSERT (intern.r_symndx >= 0
2082 && (intern.r_symndx
2083 < ecoff_data (abfd)->symbolic_header.iextMax));
2084 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
2085 rptr->addend = 0;
2086 }
2087 else
2088 {
2089 CONST char *sec_name;
2090 asection *sec;
2091
2092 /* r_symndx is a section key. */
2093 switch (intern.r_symndx)
2094 {
2095 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
2096 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
2097 case RELOC_SECTION_DATA: sec_name = ".data"; break;
2098 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
2099 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
2100 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
2101 case RELOC_SECTION_INIT: sec_name = ".init"; break;
2102 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
2103 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
2104 default: abort ();
2105 }
2106
2107 sec = bfd_get_section_by_name (abfd, sec_name);
2108 if (sec == (asection *) NULL)
2109 abort ();
2110 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
2111
2112 rptr->addend = - bfd_get_section_vma (abfd, sec);
2113 if (intern.r_type == ECOFF_R_GPREL
2114 || intern.r_type == ECOFF_R_LITERAL)
2115 rptr->addend += ecoff_data (abfd)->gp;
2116 }
2117
2118 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
2119 rptr->howto = &ecoff_howto_table[intern.r_type];
2120
2121 /* If the type is ECOFF_R_IGNORE, make sure this is a reference
2122 to the absolute section so that the reloc is ignored. */
2123 if (intern.r_type == ECOFF_R_IGNORE)
2124 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2125 }
2126
2127 bfd_release (abfd, external_relocs);
2128
2129 section->relocation = internal_relocs;
2130
2131 return true;
2132}
2133
2134/* Get a canonical list of relocs. */
2135
2136unsigned int
2137ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
2138 bfd *abfd;
2139 asection *section;
2140 arelent **relptr;
2141 asymbol **symbols;
2142{
2143 unsigned int count;
2144
2145 if (section->flags & SEC_CONSTRUCTOR)
2146 {
2147 arelent_chain *chain;
2148
2149 /* This section has relocs made up by us, not the file, so take
2150 them out of their chain and place them into the data area
2151 provided. */
2152 for (count = 0, chain = section->constructor_chain;
2153 count < section->reloc_count;
2154 count++, chain = chain->next)
2155 *relptr++ = &chain->relent;
2156 }
2157 else
2158 {
2159 arelent *tblptr;
2160
2161 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
2162 return 0;
2163
2164 tblptr = section->relocation;
2165 if (tblptr == (arelent *) NULL)
2166 return 0;
2167
2168 for (count = 0; count < section->reloc_count; count++)
2169 *relptr++ = tblptr++;
2170 }
2171
2172 *relptr = (arelent *) NULL;
2173
2174 return section->reloc_count;
2175}
2176
2177/* Get the howto structure for a generic reloc type. */
2178
2179CONST struct reloc_howto_struct *
2180ecoff_bfd_reloc_type_lookup (abfd, code)
2181 bfd *abfd;
2182 bfd_reloc_code_real_type code;
2183{
2184 int ecoff_type;
2185
2186 switch (code)
2187 {
2188 case BFD_RELOC_16:
2189 ecoff_type = ECOFF_R_REFHALF;
2190 break;
2191 case BFD_RELOC_32:
2192 ecoff_type = ECOFF_R_REFWORD;
2193 break;
2194 case BFD_RELOC_MIPS_JMP:
2195 ecoff_type = ECOFF_R_JMPADDR;
2196 break;
2197 case BFD_RELOC_HI16_S:
2198 ecoff_type = ECOFF_R_REFHI;
2199 break;
2200 case BFD_RELOC_LO16:
2201 ecoff_type = ECOFF_R_REFLO;
2202 break;
2203 case BFD_RELOC_MIPS_GPREL:
2204 ecoff_type = ECOFF_R_GPREL;
2205 break;
2206 default:
2207 return (CONST struct reloc_howto_struct *) NULL;
2208 }
2209
2210 return &ecoff_howto_table[ecoff_type];
2211}
2212\f
2213/* Provided a BFD, a section and an offset into the section, calculate
2214 and return the name of the source file and the line nearest to the
2215 wanted location. */
2216
2217boolean
2218ecoff_find_nearest_line (abfd,
2219 section,
2220 ignore_symbols,
2221 offset,
2222 filename_ptr,
2223 functionname_ptr,
2224 retline_ptr)
2225 bfd *abfd;
2226 asection *section;
2227 asymbol **ignore_symbols;
2228 bfd_vma offset;
2229 CONST char **filename_ptr;
2230 CONST char **functionname_ptr;
2231 unsigned int *retline_ptr;
2232{
2233 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2234 FDR *fdr_ptr;
2235 FDR *fdr_start;
2236 FDR *fdr_end;
2237 FDR *fdr_hold;
2238 bfd_size_type external_pdr_size;
2239 char *pdr_ptr;
2240 char *pdr_end;
2241 PDR pdr;
2242 unsigned char *line_ptr;
2243 unsigned char *line_end;
2244 int lineno;
2245
2246 /* If we're not in the .text section, we don't have any line
2247 numbers. */
2248 if (strcmp (section->name, _TEXT) != 0
2249 || offset < ecoff_data (abfd)->text_start
2250 || offset >= ecoff_data (abfd)->text_end)
2251 return false;
2252
2253 /* Make sure we have the FDR's. */
2254 if (ecoff_slurp_symbolic_info (abfd) == false
2255 || bfd_get_symcount (abfd) == 0)
2256 return false;
2257
2258 /* Each file descriptor (FDR) has a memory address. Here we track
2259 down which FDR we want. The FDR's are stored in increasing
2260 memory order. If speed is ever important, this can become a
2261 binary search. We must ignore FDR's with no PDR entries; they
2262 will have the adr of the FDR before or after them. */
2263 fdr_start = ecoff_data (abfd)->fdr;
2264 fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
2265 fdr_hold = (FDR *) NULL;
2266 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2267 {
2268 if (fdr_ptr->cpd == 0)
2269 continue;
2270 if (offset < fdr_ptr->adr)
2271 break;
2272 fdr_hold = fdr_ptr;
2273 }
2274 if (fdr_hold == (FDR *) NULL)
2275 return false;
2276 fdr_ptr = fdr_hold;
2277
2278 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2279 have an address, which is relative to the FDR address, and are
2280 also stored in increasing memory order. */
2281 offset -= fdr_ptr->adr;
2282 external_pdr_size = backend->external_pdr_size;
2283 pdr_ptr = ((char *) ecoff_data (abfd)->external_pdr
2284 + fdr_ptr->ipdFirst * external_pdr_size);
2285 pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2286 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2287
2288 /* The address of the first PDR is an offset which applies to the
2289 addresses of all the PDR's. */
2290 offset += pdr.adr;
2291
2292 for (pdr_ptr += external_pdr_size;
2293 pdr_ptr < pdr_end;
2294 pdr_ptr += external_pdr_size)
2295 {
2296 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2297 if (offset < pdr.adr)
2298 break;
2299 }
2300
2301 /* Now we can look for the actual line number. The line numbers are
2302 stored in a very funky format, which I won't try to describe.
2303 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2304 we want; we need this to compute line_end. */
2305 line_end = ecoff_data (abfd)->line;
2306 if (pdr_ptr == pdr_end)
2307 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2308 else
2309 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2310
2311 /* Now change pdr and pdr_ptr to the one we want. */
2312 pdr_ptr -= external_pdr_size;
2313 (*backend->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2314
2315 offset -= pdr.adr;
2316 lineno = pdr.lnLow;
2317 line_ptr = (ecoff_data (abfd)->line
2318 + fdr_ptr->cbLineOffset
2319 + pdr.cbLineOffset);
2320 while (line_ptr < line_end)
2321 {
2322 int delta;
2323 int count;
2324
2325 delta = *line_ptr >> 4;
2326 if (delta >= 0x8)
2327 delta -= 0x10;
2328 count = (*line_ptr & 0xf) + 1;
2329 ++line_ptr;
2330 if (delta == -8)
2331 {
2332 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2333 if (delta >= 0x8000)
2334 delta -= 0x10000;
2335 line_ptr += 2;
2336 }
2337 lineno += delta;
2338 if (offset < count * 4)
2339 break;
2340 offset -= count * 4;
2341 }
2342
2343 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2344 at least according to gdb/mipsread.c. */
2345 if (fdr_ptr->rss == -1)
2346 {
2347 *filename_ptr = NULL;
2348 if (pdr.isym == -1)
2349 *functionname_ptr = NULL;
2350 else
2351 {
2352 EXTR proc_ext;
2353
2354 (*backend->swap_ext_in) (abfd,
2355 ((char *) ecoff_data (abfd)->external_ext
2356 + pdr.isym * backend->external_ext_size),
2357 &proc_ext);
2358 *functionname_ptr = ecoff_data (abfd)->ssext + proc_ext.asym.iss;
2359 }
2360 }
2361 else
2362 {
2363 SYMR proc_sym;
2364
2365 *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
2366 (*backend->swap_sym_in) (abfd,
2367 ((char *) ecoff_data (abfd)->external_sym
2368 + ((fdr_ptr->isymBase + pdr.isym)
2369 * backend->external_sym_size)),
2370 &proc_sym);
2371 *functionname_ptr = (ecoff_data (abfd)->ss
2372 + fdr_ptr->issBase
2373 + proc_sym.iss);
2374 }
2375 if (lineno == ilineNil)
2376 lineno = 0;
2377 *retline_ptr = lineno;
2378 return true;
2379}
2380\f
2381/* We can't use the generic linking routines for ECOFF, because we
2382 have to handle all the debugging information. The generic link
2383 routine just works out the section contents and attaches a list of
2384 symbols.
2385
2386 We link by looping over all the seclets. We make two passes. On
2387 the first we set the actual section contents and determine the size
2388 of the debugging information. On the second we accumulate the
2389 debugging information and write it out.
2390
2391 This currently always accumulates the debugging information, which
2392 is incorrect, because it ignores the -s and -S options of the
2393 linker. The linker needs to be modified to give us that
2394 information in a more useful format (currently it just provides a
2395 list of symbols which should appear in the output file). */
2396
2397/* Clear the output_has_begun flag for all the input BFD's. We use it
2398 to avoid linking in the debugging information for a BFD more than
2399 once. */
2400
2401static void
2402ecoff_clear_output_flags (abfd)
2403 bfd *abfd;
2404{
2405 register asection *o;
2406 register bfd_seclet_type *p;
2407
2408 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2409 for (p = o->seclets_head;
2410 p != (bfd_seclet_type *) NULL;
2411 p = p->next)
2412 if (p->type == bfd_indirect_seclet)
2413 p->u.indirect.section->owner->output_has_begun = false;
2414}
2415
2416/* Handle an indirect seclet on the first pass. Set the contents of
2417 the output section, and accumulate the debugging information if
2418 any. */
2419
2420static boolean
2421ecoff_rel (output_bfd, seclet, output_section, data, relocateable)
2422 bfd *output_bfd;
2423 bfd_seclet_type *seclet;
2424 asection *output_section;
2425 PTR data;
2426 boolean relocateable;
2427{
2428 bfd *input_bfd;
2429 HDRR *output_symhdr;
2430 HDRR *input_symhdr;
2431
2432 if ((output_section->flags & SEC_HAS_CONTENTS)
2433 && !(output_section->flags & SEC_NEVER_LOAD)
2434 && (output_section->flags & SEC_LOAD)
2435 && seclet->size)
2436 {
2437 data = (PTR) bfd_get_relocated_section_contents (output_bfd,
2438 seclet,
2439 data,
2440 relocateable);
2441 if (bfd_set_section_contents (output_bfd,
2442 output_section,
2443 data,
2444 seclet->offset,
2445 seclet->size)
2446 == false)
2447 {
2448 abort();
2449 }
2450 }
2451
2452 input_bfd = seclet->u.indirect.section->owner;
2453
2454 /* We want to figure out how much space will be required to
2455 incorporate all the debugging information from input_bfd. We use
2456 the output_has_begun field to avoid adding it in more than once.
2457 The actual incorporation is done in the second pass, in
2458 ecoff_get_debug. The code has to parallel that code in its
2459 manipulations of output_symhdr. */
2460
2461 if (input_bfd->output_has_begun)
2462 return true;
2463 input_bfd->output_has_begun = true;
2464
2465 output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
2466
2467 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2468 {
2469 asymbol **symbols;
2470 asymbol **sym_ptr;
2471 asymbol **sym_end;
2472
2473 /* We just accumulate local symbols from a non-ECOFF BFD. The
2474 external symbols are handled separately. */
2475
2476 symbols = (asymbol **) bfd_alloc (output_bfd,
2477 get_symtab_upper_bound (input_bfd));
2478 if (symbols == (asymbol **) NULL)
2479 {
2480 bfd_error = no_memory;
2481 return false;
2482 }
2483 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2484
2485 for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
2486 {
2487 size_t len;
2488
2489 len = strlen ((*sym_ptr)->name);
2490 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2491 {
2492 ++output_symhdr->isymMax;
2493 output_symhdr->issMax += len + 1;
2494 }
2495 }
2496
2497 bfd_release (output_bfd, (PTR) symbols);
2498
2499 ++output_symhdr->ifdMax;
2500
2501 return true;
2502 }
2503
2504 /* We simply add in the information from another ECOFF BFD. First
2505 we make sure we have the symbolic information. */
2506 if (ecoff_slurp_symbol_table (input_bfd) == false)
2507 return false;
2508 if (bfd_get_symcount (input_bfd) == 0)
2509 return true;
2510
2511 input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2512
2513 /* Figure out how much information we are going to be putting in.
2514 The external symbols are handled separately. */
2515 output_symhdr->ilineMax += input_symhdr->ilineMax;
2516 output_symhdr->cbLine += input_symhdr->cbLine;
2517 output_symhdr->idnMax += input_symhdr->idnMax;
2518 output_symhdr->ipdMax += input_symhdr->ipdMax;
2519 output_symhdr->isymMax += input_symhdr->isymMax;
2520 output_symhdr->ioptMax += input_symhdr->ioptMax;
2521 output_symhdr->iauxMax += input_symhdr->iauxMax;
2522 output_symhdr->issMax += input_symhdr->issMax;
2523 output_symhdr->ifdMax += input_symhdr->ifdMax;
2524
2525 /* The RFD's are special, since we create them if needed. */
2526 if (input_symhdr->crfd > 0)
2527 output_symhdr->crfd += input_symhdr->crfd;
2528 else
2529 output_symhdr->crfd += input_symhdr->ifdMax;
2530
2531 return true;
2532}
2533
2534/* Handle an arbitrary seclet on the first pass. */
2535
2536static boolean
2537ecoff_dump_seclet (abfd, seclet, section, data, relocateable)
2538 bfd *abfd;
2539 bfd_seclet_type *seclet;
2540 asection *section;
2541 PTR data;
2542 boolean relocateable;
2543{
2544 switch (seclet->type)
2545 {
2546 case bfd_indirect_seclet:
2547 /* The contents of this section come from another one somewhere
2548 else. */
2549 return ecoff_rel (abfd, seclet, section, data, relocateable);
2550
2551 case bfd_fill_seclet:
2552 /* Fill in the section with fill.value. This is used to pad out
2553 sections, but we must avoid padding the .bss section. */
2554 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2555 {
2556 if (seclet->u.fill.value != 0)
2557 abort ();
2558 }
2559 else
2560 {
2561 char *d = (char *) bfd_alloc (abfd, seclet->size);
2562 unsigned int i;
2563 boolean ret;
2564
2565 for (i = 0; i < seclet->size; i+=2)
2566 d[i] = seclet->u.fill.value >> 8;
2567 for (i = 1; i < seclet->size; i+=2)
2568 d[i] = seclet->u.fill.value;
2569 ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2570 seclet->size);
2571 bfd_release (abfd, (PTR) d);
2572 return ret;
2573 }
2574 break;
2575
2576 default:
2577 abort();
2578 }
2579
2580 return true;
2581}
2582
2583/* Add a string to the debugging information we are accumulating for a
2584 file. Return the offset from the fdr string base or from the
2585 external string base. */
2586
2587static long
2588ecoff_add_string (output_bfd, fdr, string, external)
2589 bfd *output_bfd;
2590 FDR *fdr;
2591 CONST char *string;
2592 boolean external;
2593{
2594 HDRR *symhdr;
2595 size_t len;
2596 long ret;
2597
2598 symhdr = &ecoff_data (output_bfd)->symbolic_header;
2599 len = strlen (string);
2600 if (external)
2601 {
2602 strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2603 ret = symhdr->issExtMax;
2604 symhdr->issExtMax += len + 1;
2605 }
2606 else
2607 {
2608 strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2609 ret = fdr->cbSs;
2610 symhdr->issMax += len + 1;
2611 fdr->cbSs += len + 1;
2612 }
2613 return ret;
2614}
2615
2616/* Accumulate the debugging information from an input section. */
2617
2618static boolean
2619ecoff_get_debug (output_bfd, seclet, section, relocateable)
2620 bfd *output_bfd;
2621 bfd_seclet_type *seclet;
2622 asection *section;
2623 boolean relocateable;
2624{
2625 const struct ecoff_backend_data * const backend = ecoff_backend (output_bfd);
2626 const bfd_size_type external_sym_size = backend->external_sym_size;
2627 const bfd_size_type external_pdr_size = backend->external_pdr_size;
2628 const bfd_size_type external_fdr_size = backend->external_fdr_size;
2629 const bfd_size_type external_rfd_size = backend->external_rfd_size;
2630 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
2631 = backend->swap_sym_in;
2632 void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
2633 = backend->swap_sym_out;
2634 void (* const swap_pdr_in) PARAMS ((bfd *, PTR, PDR *))
2635 = backend->swap_pdr_in;
2636 void (* const swap_fdr_out) PARAMS ((bfd *, const FDR *, PTR))
2637 = backend->swap_fdr_out;
2638 void (* const swap_rfd_out) PARAMS ((bfd *, const RFDT *, PTR))
2639 = backend->swap_rfd_out;
2640 bfd *input_bfd;
2641 HDRR *output_symhdr;
2642 HDRR *input_symhdr;
2643 ecoff_data_type *output_ecoff;
2644 ecoff_data_type *input_ecoff;
2645 unsigned int count;
2646 char *sym_out;
2647 ecoff_symbol_type *esym_ptr;
2648 ecoff_symbol_type *esym_end;
2649 FDR *fdr_ptr;
2650 FDR *fdr_end;
2651 char *fdr_out;
2652
2653 input_bfd = seclet->u.indirect.section->owner;
2654
2655 /* Don't get the information more than once. */
2656 if (input_bfd->output_has_begun)
2657 return true;
2658 input_bfd->output_has_begun = true;
2659
2660 output_ecoff = ecoff_data (output_bfd);
2661 output_symhdr = &output_ecoff->symbolic_header;
2662
2663 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2664 {
2665 FDR fdr;
2666 asymbol **symbols;
2667 asymbol **sym_ptr;
2668 asymbol **sym_end;
2669
2670 /* This is not an ECOFF BFD. Just gather the symbols. */
2671
2672 memset (&fdr, 0, sizeof fdr);
2673
2674 fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2675 fdr.issBase = output_symhdr->issMax;
2676 fdr.cbSs = 0;
2677 fdr.rss = ecoff_add_string (output_bfd,
2678 &fdr,
2679 bfd_get_filename (input_bfd),
2680 false);
2681 fdr.isymBase = output_symhdr->isymMax;
2682
2683 /* Get the local symbols from the input BFD. */
2684 symbols = (asymbol **) bfd_alloc (output_bfd,
2685 get_symtab_upper_bound (input_bfd));
2686 if (symbols == (asymbol **) NULL)
2687 {
2688 bfd_error = no_memory;
2689 return false;
2690 }
2691 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2692
2693 /* Handle the local symbols. Any external symbols are handled
2694 separately. */
2695 fdr.csym = 0;
2696 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2697 {
2698 SYMR internal_sym;
2699
2700 if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2701 continue;
2702 memset (&internal_sym, 0, sizeof internal_sym);
2703 internal_sym.iss = ecoff_add_string (output_bfd,
2704 &fdr,
2705 (*sym_ptr)->name,
2706 false);
2707
2708 if (bfd_is_com_section ((*sym_ptr)->section)
2709 || (*sym_ptr)->section == &bfd_und_section)
2710 internal_sym.value = (*sym_ptr)->value;
2711 else
2712 internal_sym.value = ((*sym_ptr)->value
2713 + (*sym_ptr)->section->output_offset
2714 + (*sym_ptr)->section->output_section->vma);
2715 internal_sym.st = stNil;
2716 internal_sym.sc = scUndefined;
2717 internal_sym.index = indexNil;
2718 (*swap_sym_out) (output_bfd, &internal_sym,
2719 ((char *) output_ecoff->external_sym
2720 + output_symhdr->isymMax * external_sym_size));
2721 ++fdr.csym;
2722 ++output_symhdr->isymMax;
2723 }
2724
2725 bfd_release (output_bfd, (PTR) symbols);
2726
2727 /* Leave everything else in the FDR zeroed out. This will cause
2728 the lang field to be langC. The fBigendian field will
2729 indicate little endian format, but it doesn't matter because
2730 it only applies to aux fields and there are none. */
2731
2732 (*swap_fdr_out) (output_bfd, &fdr,
2733 ((char *) output_ecoff->external_fdr
2734 + output_symhdr->ifdMax * external_fdr_size));
2735 ++output_symhdr->ifdMax;
2736 return true;
2737 }
2738
2739 /* This is an ECOFF BFD. We want to grab the information from
2740 input_bfd and attach it to output_bfd. */
2741 count = bfd_get_symcount (input_bfd);
2742 if (count == 0)
2743 return true;
2744 input_ecoff = ecoff_data (input_bfd);
2745 input_symhdr = &input_ecoff->symbolic_header;
2746
2747 /* I think that it is more efficient to simply copy the debugging
2748 information from the input BFD to the output BFD. Because ECOFF
2749 uses relative pointers for most of the debugging information,
2750 only a little of it has to be changed at all. */
2751
2752 /* Swap in the local symbols, adjust their values, and swap them out
2753 again. The external symbols are handled separately. */
2754 sym_out = ((char *) output_ecoff->external_sym
2755 + output_symhdr->isymMax * external_sym_size);
2756
2757 esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
2758 esym_end = esym_ptr + count;
2759 for (; esym_ptr < esym_end; esym_ptr++)
2760 {
2761 if (esym_ptr->local)
2762 {
2763 SYMR sym;
2764
2765 (*swap_sym_in) (input_bfd, esym_ptr->native, &sym);
2766
2767 /* If we're producing an executable, move common symbols
2768 into bss. */
2769 if (relocateable == false)
2770 {
2771 if (sym.sc == scCommon)
2772 sym.sc = scBss;
2773 else if (sym.sc == scSCommon)
2774 sym.sc = scSBss;
2775 }
2776
2777 if (! bfd_is_com_section (esym_ptr->symbol.section)
2778 && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
2779 && esym_ptr->symbol.section != &bfd_und_section)
2780 sym.value = (esym_ptr->symbol.value
2781 + esym_ptr->symbol.section->output_offset
2782 + esym_ptr->symbol.section->output_section->vma);
2783 (*swap_sym_out) (output_bfd, &sym, sym_out);
2784 sym_out += external_sym_size;
2785 }
2786 }
2787
2788 /* That should have accounted for all the local symbols in
2789 input_bfd. */
2790
2791 /* Copy the information that does not need swapping. */
2792 memcpy (output_ecoff->line + output_symhdr->cbLine,
2793 input_ecoff->line,
2794 input_symhdr->cbLine * sizeof (unsigned char));
2795 memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2796 input_ecoff->external_aux,
2797 input_symhdr->iauxMax * sizeof (union aux_ext));
2798 memcpy (output_ecoff->ss + output_symhdr->issMax,
2799 input_ecoff->ss,
2800 input_symhdr->issMax * sizeof (char));
2801
2802 /* Some of the information may need to be swapped. */
2803 if (output_bfd->xvec->header_byteorder_big_p
2804 == input_bfd->xvec->header_byteorder_big_p)
2805 {
2806 /* The two BFD's have the same endianness, so memcpy will
2807 suffice. */
2808 if (input_symhdr->idnMax > 0)
2809 memcpy (((char *) output_ecoff->external_dnr
2810 + output_symhdr->idnMax * backend->external_dnr_size),
2811 input_ecoff->external_dnr,
2812 input_symhdr->idnMax * backend->external_dnr_size);
2813 if (input_symhdr->ipdMax > 0)
2814 memcpy (((char *) output_ecoff->external_pdr
2815 + output_symhdr->ipdMax * external_pdr_size),
2816 input_ecoff->external_pdr,
2817 input_symhdr->ipdMax * external_pdr_size);
2818 if (input_symhdr->ioptMax > 0)
2819 memcpy (((char *) output_ecoff->external_opt
2820 + output_symhdr->ioptMax * backend->external_opt_size),
2821 input_ecoff->external_opt,
2822 input_symhdr->ioptMax * backend->external_opt_size);
2823 }
2824 else
2825 {
2826 bfd_size_type sz;
2827 char *in;
2828 char *end;
2829 char *out;
2830
2831 /* The two BFD's have different endianness, so we must swap
2832 everything in and out. This code would always work, but it
2833 would be slow in the normal case. */
2834 sz = backend->external_dnr_size;
2835 in = (char *) input_ecoff->external_dnr;
2836 end = in + input_symhdr->idnMax * sz;
2837 out = (char *) output_ecoff->external_dnr + output_symhdr->idnMax * sz;
2838 for (; in < end; in += sz, out += sz)
2839 {
2840 DNR dnr;
2841
2842 (*backend->swap_dnr_in) (input_bfd, in, &dnr);
2843 (*backend->swap_dnr_out) (output_bfd, &dnr, out);
2844 }
2845
2846 sz = external_pdr_size;
2847 in = (char *) input_ecoff->external_pdr;
2848 end = in + input_symhdr->ipdMax * sz;
2849 out = (char *) output_ecoff->external_pdr + output_symhdr->ipdMax * sz;
2850 for (; in < end; in += sz, out += sz)
2851 {
2852 PDR pdr;
2853
2854 (*swap_pdr_in) (input_bfd, in, &pdr);
2855 (*backend->swap_pdr_out) (output_bfd, &pdr, out);
2856 }
2857
2858 sz = backend->external_opt_size;
2859 in = (char *) input_ecoff->external_opt;
2860 end = in + input_symhdr->ioptMax * sz;
2861 out = (char *) output_ecoff->external_opt + output_symhdr->ioptMax * sz;
2862 for (; in < end; in += sz, out += sz)
2863 {
2864 OPTR opt;
2865
2866 (*backend->swap_opt_in) (input_bfd, in, &opt);
2867 (*backend->swap_opt_out) (output_bfd, &opt, out);
2868 }
2869 }
2870
2871 /* Set ifdbase so that the external symbols know how to adjust their
2872 ifd values. */
2873 input_ecoff->ifdbase = output_symhdr->ifdMax;
2874
2875 fdr_ptr = input_ecoff->fdr;
2876 fdr_end = fdr_ptr + input_symhdr->ifdMax;
2877 fdr_out = ((char *) output_ecoff->external_fdr
2878 + output_symhdr->ifdMax * external_fdr_size);
2879 for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out += external_fdr_size)
2880 {
2881 FDR fdr;
2882 unsigned long pdr_off;
2883
2884 fdr = *fdr_ptr;
2885
2886 /* The memory address for this fdr is the address for the seclet
2887 plus the offset to this fdr within input_bfd. For some
2888 reason the offset of the first procedure pointer is also
2889 added in. */
2890 if (fdr.cpd == 0)
2891 pdr_off = 0;
2892 else
2893 {
2894 PDR pdr;
2895
2896 (*swap_pdr_in) (input_bfd,
2897 ((char *) input_ecoff->external_pdr
2898 + fdr.ipdFirst * external_pdr_size),
2899 &pdr);
2900 pdr_off = pdr.adr;
2901 }
2902 fdr.adr = (bfd_get_section_vma (output_bfd, section)
2903 + seclet->offset
2904 + (fdr_ptr->adr - input_ecoff->fdr->adr)
2905 + pdr_off);
2906
2907 fdr.issBase += output_symhdr->issMax;
2908 fdr.isymBase += output_symhdr->isymMax;
2909 fdr.ilineBase += output_symhdr->ilineMax;
2910 fdr.ioptBase += output_symhdr->ioptMax;
2911 fdr.ipdFirst += output_symhdr->ipdMax;
2912 fdr.iauxBase += output_symhdr->iauxMax;
2913 fdr.rfdBase += output_symhdr->crfd;
2914
2915 /* If there are no RFD's, we are going to add some. We don't
2916 want to adjust irfd for this, so that all the FDR's can share
2917 the RFD's. */
2918 if (input_symhdr->crfd == 0)
2919 fdr.crfd = input_symhdr->ifdMax;
2920
2921 if (fdr.cbLine != 0)
2922 fdr.cbLineOffset += output_symhdr->cbLine;
2923
2924 (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
2925 }
2926
2927 if (input_symhdr->crfd > 0)
2928 {
2929 void (* const swap_rfd_in) PARAMS ((bfd *, PTR, RFDT *))
2930 = backend->swap_rfd_in;
2931 char *rfd_in;
2932 char *rfd_end;
2933 char *rfd_out;
2934
2935 /* Swap and adjust the RFD's. RFD's are only created by the
2936 linker, so this will only be necessary if one of the input
2937 files is the result of a partial link. Presumably all
2938 necessary RFD's are present. */
2939 rfd_in = (char *) input_ecoff->external_rfd;
2940 rfd_end = rfd_in + input_symhdr->crfd * external_rfd_size;
2941 rfd_out = ((char *) output_ecoff->external_rfd
2942 + output_symhdr->crfd * external_rfd_size);
2943 for (;
2944 rfd_in < rfd_end;
2945 rfd_in += external_rfd_size, rfd_out += external_rfd_size)
2946 {
2947 RFDT rfd;
2948
2949 (*swap_rfd_in) (input_bfd, rfd_in, &rfd);
2950 rfd += output_symhdr->ifdMax;
2951 (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
2952 }
2953 output_symhdr->crfd += input_symhdr->crfd;
2954 }
2955 else
2956 {
2957 char *rfd_out;
2958 char *rfd_end;
2959 RFDT rfd;
2960
2961 /* Create RFD's. Some of the debugging information includes
2962 relative file indices. These indices are taken as indices to
2963 the RFD table if there is one, or to the global table if
2964 there is not. If we did not create RFD's, we would have to
2965 parse and adjust all the debugging information which contains
2966 file indices. */
2967 rfd = output_symhdr->ifdMax;
2968 rfd_out = ((char *) output_ecoff->external_rfd
2969 + output_symhdr->crfd * external_rfd_size);
2970 rfd_end = rfd_out + input_symhdr->ifdMax * external_rfd_size;
2971 for (; rfd_out < rfd_end; rfd_out += external_rfd_size, rfd++)
2972 (*swap_rfd_out) (output_bfd, &rfd, rfd_out);
2973 output_symhdr->crfd += input_symhdr->ifdMax;
2974 }
2975
2976 /* Combine the register masks. */
2977 {
2978 int i;
2979
2980 output_ecoff->gprmask |= input_ecoff->gprmask;
2981 for (i = 0; i < 4; i++)
2982 output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
2983 }
2984
2985 /* Update the counts. */
2986 output_symhdr->ilineMax += input_symhdr->ilineMax;
2987 output_symhdr->cbLine += input_symhdr->cbLine;
2988 output_symhdr->idnMax += input_symhdr->idnMax;
2989 output_symhdr->ipdMax += input_symhdr->ipdMax;
2990 output_symhdr->isymMax += input_symhdr->isymMax;
2991 output_symhdr->ioptMax += input_symhdr->ioptMax;
2992 output_symhdr->iauxMax += input_symhdr->iauxMax;
2993 output_symhdr->issMax += input_symhdr->issMax;
2994 output_symhdr->ifdMax += input_symhdr->ifdMax;
2995
2996 return true;
2997}
2998
2999/* This is the actual link routine. It makes two passes over all the
3000 seclets. */
3001
3002boolean
3003ecoff_bfd_seclet_link (abfd, data, relocateable)
3004 bfd *abfd;
3005 PTR data;
3006 boolean relocateable;
3007{
3008 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3009 HDRR *symhdr;
3010 int ipass;
3011 register asection *o;
3012 register bfd_seclet_type *p;
3013 asymbol **sym_ptr_ptr;
3014 bfd_size_type debug_align;
3015 bfd_size_type size;
3016 char *raw;
3017
3018 /* We accumulate the debugging information counts in the symbolic
3019 header. */
3020 symhdr = &ecoff_data (abfd)->symbolic_header;
3021 symhdr->magic = magicSym;
3022 /* FIXME: What should the version stamp be? */
3023 symhdr->vstamp = 0;
3024 symhdr->ilineMax = 0;
3025 symhdr->cbLine = 0;
3026 symhdr->idnMax = 0;
3027 symhdr->ipdMax = 0;
3028 symhdr->isymMax = 0;
3029 symhdr->ioptMax = 0;
3030 symhdr->iauxMax = 0;
3031 symhdr->issMax = 0;
3032 symhdr->issExtMax = 0;
3033 symhdr->ifdMax = 0;
3034 symhdr->crfd = 0;
3035 symhdr->iextMax = 0;
3036
3037 /* We need to copy over the debugging symbols from each input BFD.
3038 When we do this copying, we have to adjust the text address in
3039 the FDR structures, so we have to know the text address used for
3040 the input BFD. Since we only want to copy the symbols once per
3041 input BFD, but we are going to look at each input BFD multiple
3042 times (once for each section it provides), we arrange to always
3043 look at the text section first. That means that when we copy the
3044 debugging information, we always know the text address. So we
3045 actually do each pass in two sub passes; first the text sections,
3046 then the non-text sections. We use the output_has_begun flag to
3047 determine whether we have copied over the debugging information
3048 yet. */
3049
3050 /* Do the first pass: set the output section contents and count the
3051 debugging information. */
3052 ecoff_clear_output_flags (abfd);
3053 for (ipass = 0; ipass < 2; ipass++)
3054 {
3055 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3056 {
3057 /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
3058 so they are done on pass 0. For other sections the
3059 expression is true, so they are done on pass 1. */
3060 if (((o->flags & SEC_CODE) == 0) != ipass)
3061 continue;
3062
3063 for (p = o->seclets_head;
3064 p != (bfd_seclet_type *) NULL;
3065 p = p->next)
3066 {
3067 if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
3068 == false)
3069 return false;
3070 }
3071 }
3072 }
3073
3074 /* We handle the external symbols differently. We use the ones
3075 attached to the output_bfd. The linker will have already
3076 determined which symbols are to be attached. Here we just
3077 determine how much space we will need for them. */
3078 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3079 if (sym_ptr_ptr != NULL)
3080 {
3081 asymbol **sym_end;
3082
3083 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
3084 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
3085 {
3086 if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
3087 && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
3088 {
3089 ++symhdr->iextMax;
3090 symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
3091 }
3092 }
3093 }
3094
3095 /* Adjust the counts so that structures are longword aligned. */
3096 debug_align = backend->debug_align;
3097 --debug_align;
3098 symhdr->cbLine = (symhdr->cbLine + debug_align) &~ debug_align;
3099 symhdr->issMax = (symhdr->issMax + debug_align) &~ debug_align;
3100 symhdr->issExtMax = (symhdr->issExtMax + debug_align) &~ debug_align;
3101
3102 /* Now the counts in symhdr are the correct size for the debugging
3103 information. We allocate the right amount of space, and reset
3104 the counts so that the second pass can use them as indices. It
3105 would be possible to output the debugging information directly to
3106 the file in pass 2, rather than to build it in memory and then
3107 write it out. Outputting to the file would require a lot of
3108 seeks and small writes, though, and I think this approach is
3109 faster. */
3110 size = (symhdr->cbLine * sizeof (unsigned char)
3111 + symhdr->idnMax * backend->external_dnr_size
3112 + symhdr->ipdMax * backend->external_pdr_size
3113 + symhdr->isymMax * backend->external_sym_size
3114 + symhdr->ioptMax * backend->external_opt_size
3115 + symhdr->iauxMax * sizeof (union aux_ext)
3116 + symhdr->issMax * sizeof (char)
3117 + symhdr->issExtMax * sizeof (char)
3118 + symhdr->ifdMax * backend->external_fdr_size
3119 + symhdr->crfd * backend->external_rfd_size
3120 + symhdr->iextMax * backend->external_ext_size);
3121 raw = (char *) bfd_alloc (abfd, size);
3122 if (raw == (char *) NULL)
3123 {
3124 bfd_error = no_memory;
3125 return false;
3126 }
3127 ecoff_data (abfd)->raw_size = size;
3128 ecoff_data (abfd)->raw_syments = (PTR) raw;
3129
3130 /* Initialize the raw pointers. */
3131#define SET(field, count, type, size) \
3132 ecoff_data (abfd)->field = (type) raw; \
3133 raw += symhdr->count * size
3134
3135 SET (line, cbLine, unsigned char *, sizeof (unsigned char));
3136 SET (external_dnr, idnMax, PTR, backend->external_dnr_size);
3137 SET (external_pdr, ipdMax, PTR, backend->external_pdr_size);
3138 SET (external_sym, isymMax, PTR, backend->external_sym_size);
3139 SET (external_opt, ioptMax, PTR, backend->external_opt_size);
3140 SET (external_aux, iauxMax, union aux_ext *, sizeof (union aux_ext));
3141 SET (ss, issMax, char *, sizeof (char));
3142 SET (ssext, issExtMax, char *, sizeof (char));
3143 SET (external_fdr, ifdMax, PTR, backend->external_fdr_size);
3144 SET (external_rfd, crfd, PTR, backend->external_rfd_size);
3145 SET (external_ext, iextMax, PTR, backend->external_ext_size);
3146#undef SET
3147
3148 /* Reset the counts so the second pass can use them to know how far
3149 it has gotten. */
3150 symhdr->ilineMax = 0;
3151 symhdr->cbLine = 0;
3152 symhdr->idnMax = 0;
3153 symhdr->ipdMax = 0;
3154 symhdr->isymMax = 0;
3155 symhdr->ioptMax = 0;
3156 symhdr->iauxMax = 0;
3157 symhdr->issMax = 0;
3158 symhdr->issExtMax = 0;
3159 symhdr->ifdMax = 0;
3160 symhdr->crfd = 0;
3161 symhdr->iextMax = 0;
3162
3163 /* Do the second pass: accumulate the debugging information. */
3164 ecoff_clear_output_flags (abfd);
3165 for (ipass = 0; ipass < 2; ipass++)
3166 {
3167 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3168 {
3169 if (((o->flags & SEC_CODE) == 0) != ipass)
3170 continue;
3171 for (p = o->seclets_head;
3172 p != (bfd_seclet_type *) NULL;
3173 p = p->next)
3174 {
3175 if (p->type == bfd_indirect_seclet)
3176 {
3177 if (ecoff_get_debug (abfd, p, o, relocateable) == false)
3178 return false;
3179 }
3180 }
3181 }
3182 }
3183
3184 /* Put in the external symbols. */
3185 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3186 if (sym_ptr_ptr != NULL)
3187 {
3188 const bfd_size_type external_ext_size = backend->external_ext_size;
3189 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3190 = backend->swap_ext_in;
3191 void (* const swap_ext_out) PARAMS ((bfd *, const EXTR *, PTR))
3192 = backend->swap_ext_out;
3193 char *ssext;
3194 char *external_ext;
3195
3196 ssext = ecoff_data (abfd)->ssext;
3197 external_ext = (char *) ecoff_data (abfd)->external_ext;
3198 for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
3199 {
3200 asymbol *sym_ptr;
3201 EXTR esym;
3202
3203 sym_ptr = *sym_ptr_ptr;
3204
3205 if ((sym_ptr->flags & BSF_DEBUGGING) != 0
3206 || (sym_ptr->flags & BSF_LOCAL) != 0)
3207 continue;
3208
3209 /* The native pointer can be NULL for a symbol created by
3210 the linker via ecoff_make_empty_symbol. */
3211 if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
3212 || ecoffsymbol (sym_ptr)->native == NULL)
3213 {
3214 esym.jmptbl = 0;
3215 esym.cobol_main = 0;
3216 esym.weakext = 0;
3217 esym.reserved = 0;
3218 esym.ifd = ifdNil;
3219 /* FIXME: we can do better than this for st and sc. */
3220 esym.asym.st = stGlobal;
3221 esym.asym.sc = scAbs;
3222 esym.asym.reserved = 0;
3223 esym.asym.index = indexNil;
3224 }
3225 else
3226 {
3227 ecoff_symbol_type *ecoff_sym_ptr;
3228
3229 ecoff_sym_ptr = ecoffsymbol (sym_ptr);
3230 if (ecoff_sym_ptr->local)
3231 abort ();
3232 (*swap_ext_in) (abfd, ecoff_sym_ptr->native, &esym);
3233
3234 /* If we're producing an executable, move common symbols
3235 into bss. */
3236 if (relocateable == false)
3237 {
3238 if (esym.asym.sc == scCommon)
3239 esym.asym.sc = scBss;
3240 else if (esym.asym.sc == scSCommon)
3241 esym.asym.sc = scSBss;
3242 }
3243
3244 /* Adjust the FDR index for the symbol by that used for
3245 the input BFD. */
3246 esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
3247 }
3248
3249 esym.asym.iss = symhdr->issExtMax;
3250
3251 if (bfd_is_com_section (sym_ptr->section)
3252 || sym_ptr->section == &bfd_und_section)
3253 esym.asym.value = sym_ptr->value;
3254 else
3255 esym.asym.value = (sym_ptr->value
3256 + sym_ptr->section->output_offset
3257 + sym_ptr->section->output_section->vma);
3258
3259 (*swap_ext_out) (abfd, &esym, external_ext);
3260
3261 ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
3262
3263 external_ext += external_ext_size;
3264 ++symhdr->iextMax;
3265
3266 strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
3267 symhdr->issExtMax += strlen (sym_ptr->name) + 1;
3268 }
3269 }
3270
3271 /* Adjust the counts so that structures are longword aligned. */
3272 symhdr->cbLine = (symhdr->cbLine + debug_align) &~ debug_align;
3273 symhdr->issMax = (symhdr->issMax + debug_align) &~ debug_align;
3274 symhdr->issExtMax = (symhdr->issExtMax + debug_align) &~ debug_align;
3275
3276 return true;
3277}
3278\f
3279/* Set the architecture. The supported architecture is stored in the
3280 backend pointer. We always set the architecture anyhow, since many
3281 callers ignore the return value. */
3282
3283boolean
3284ecoff_set_arch_mach (abfd, arch, machine)
3285 bfd *abfd;
3286 enum bfd_architecture arch;
3287 unsigned long machine;
3288{
3289 bfd_default_set_arch_mach (abfd, arch, machine);
3290 return arch == ecoff_backend (abfd)->arch;
3291}
3292
3293/* Get the size of the section headers. We do not output the .scommon
3294 section which we created in ecoff_mkobject. */
3295
3296int
3297ecoff_sizeof_headers (abfd, reloc)
3298 bfd *abfd;
3299 boolean reloc;
3300{
3301 return (bfd_coff_filhsz (abfd)
3302 + bfd_coff_aoutsz (abfd)
3303 + (abfd->section_count - 1) * bfd_coff_scnhsz (abfd));
3304}
3305
3306/* Calculate the file position for each section, and set
3307 reloc_filepos. */
3308
3309static void
3310ecoff_compute_section_file_positions (abfd)
3311 bfd *abfd;
3312{
3313 asection *current;
3314 file_ptr sofar;
3315 file_ptr old_sofar;
3316 boolean first_data;
3317
3318 if (bfd_get_start_address (abfd))
3319 abfd->flags |= EXEC_P;
3320
3321 sofar = ecoff_sizeof_headers (abfd, false);
3322
3323 first_data = true;
3324 for (current = abfd->sections;
3325 current != (asection *) NULL;
3326 current = current->next)
3327 {
3328 /* Only deal with sections which have contents */
3329 if (! (current->flags & SEC_HAS_CONTENTS)
3330 || strcmp (current->name, SCOMMON) == 0)
3331 continue;
3332
3333 /* On Ultrix, the data sections in an executable file must be
3334 aligned to a page boundary within the file. This does not
3335 affect the section size, though. FIXME: Does this work for
3336 other platforms? */
3337 if ((abfd->flags & EXEC_P) != 0
3338 && (abfd->flags & D_PAGED) != 0
3339 && first_data != false
3340 && (current->flags & SEC_CODE) == 0)
3341 {
3342 const bfd_vma round = ecoff_backend (abfd)->round;
3343
3344 sofar = (sofar + round - 1) &~ (round - 1);
3345 first_data = false;
3346 }
3347
3348 /* Align the sections in the file to the same boundary on
3349 which they are aligned in virtual memory. */
3350 old_sofar = sofar;
3351 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3352
3353 current->filepos = sofar;
3354
3355 sofar += current->_raw_size;
3356
3357 /* make sure that this section is of the right size too */
3358 old_sofar = sofar;
3359 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3360 current->_raw_size += sofar - old_sofar;
3361 }
3362
3363 ecoff_data (abfd)->reloc_filepos = sofar;
3364}
3365
3366/* Set the contents of a section. */
3367
3368boolean
3369ecoff_set_section_contents (abfd, section, location, offset, count)
3370 bfd *abfd;
3371 asection *section;
3372 PTR location;
3373 file_ptr offset;
3374 bfd_size_type count;
3375{
3376 if (abfd->output_has_begun == false)
3377 ecoff_compute_section_file_positions (abfd);
3378
3379 bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
3380
3381 if (count != 0)
3382 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
3383
3384 return true;
3385}
3386
3387/* Write out an ECOFF file. */
3388
3389boolean
3390ecoff_write_object_contents (abfd)
3391 bfd *abfd;
3392{
3393 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3394 const bfd_vma round = backend->round;
3395 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
3396 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
3397 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
3398 const bfd_size_type external_hdr_size = backend->external_hdr_size;
3399 const bfd_size_type external_reloc_size = backend->external_reloc_size;
3400 void (* const swap_reloc_out) PARAMS ((bfd *,
3401 const struct internal_reloc *,
3402 PTR))
3403 = backend->swap_reloc_out;
3404 asection *current;
3405 unsigned int count;
3406 file_ptr scn_base;
3407 file_ptr reloc_base;
3408 file_ptr sym_base;
3409 unsigned long reloc_size;
3410 unsigned long text_size;
3411 unsigned long text_start;
3412 unsigned long data_size;
3413 unsigned long data_start;
3414 unsigned long bss_size;
3415 PTR buff;
3416 struct internal_filehdr internal_f;
3417 struct internal_aouthdr internal_a;
3418 int i;
3419
3420 bfd_error = system_call_error;
3421
3422 if(abfd->output_has_begun == false)
3423 ecoff_compute_section_file_positions(abfd);
3424
3425 if (abfd->sections != (asection *) NULL)
3426 scn_base = abfd->sections->filepos;
3427 else
3428 scn_base = 0;
3429 reloc_base = ecoff_data (abfd)->reloc_filepos;
3430
3431 count = 1;
3432 reloc_size = 0;
3433 for (current = abfd->sections;
3434 current != (asection *)NULL;
3435 current = current->next)
3436 {
3437 if (strcmp (current->name, SCOMMON) == 0)
3438 continue;
3439 current->target_index = count;
3440 ++count;
3441 if (current->reloc_count != 0)
3442 {
3443 bfd_size_type relsize;
3444
3445 current->rel_filepos = reloc_base;
3446 relsize = current->reloc_count * external_reloc_size;
3447 reloc_size += relsize;
3448 reloc_base += relsize;
3449 }
3450 else
3451 current->rel_filepos = 0;
3452 }
3453
3454 sym_base = reloc_base + reloc_size;
3455
3456 /* At least on Ultrix, the symbol table of an executable file must
3457 be aligned to a page boundary. FIXME: Is this true on other
3458 platforms? */
3459 if ((abfd->flags & EXEC_P) != 0
3460 && (abfd->flags & D_PAGED) != 0)
3461 sym_base = (sym_base + round - 1) &~ (round - 1);
3462
3463 ecoff_data (abfd)->sym_filepos = sym_base;
3464
3465 if ((abfd->flags & D_PAGED) != 0)
3466 text_size = ecoff_sizeof_headers (abfd, false);
3467 else
3468 text_size = 0;
3469 text_start = 0;
3470 data_size = 0;
3471 data_start = 0;
3472 bss_size = 0;
3473
3474 /* Write section headers to the file. */
3475
3476 buff = (PTR) alloca (scnhsz);
3477 internal_f.f_nscns = 0;
3478 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
3479 return false;
3480 for (current = abfd->sections;
3481 current != (asection *) NULL;
3482 current = current->next)
3483 {
3484 struct internal_scnhdr section;
3485 bfd_vma vma;
3486
3487 if (strcmp (current->name, SCOMMON) == 0)
3488 {
3489 BFD_ASSERT (bfd_get_section_size_before_reloc (current) == 0
3490 && current->reloc_count == 0);
3491 continue;
3492 }
3493
3494 ++internal_f.f_nscns;
3495
3496 strncpy (section.s_name, current->name, sizeof section.s_name);
3497
3498 /* FIXME: is this correct for shared libraries? I think it is
3499 but I have no platform to check. Ian Lance Taylor. */
3500 vma = bfd_get_section_vma (abfd, current);
3501 if (strcmp (current->name, _LIB) == 0)
3502 section.s_vaddr = 0;
3503 else
3504 section.s_vaddr = vma;
3505
3506 section.s_paddr = vma;
3507 section.s_size = bfd_get_section_size_before_reloc (current);
3508
3509 /* If this section has no size or is unloadable then the scnptr
3510 will be 0 too. */
3511 if (current->_raw_size == 0
3512 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3513 section.s_scnptr = 0;
3514 else
3515 section.s_scnptr = current->filepos;
3516 section.s_relptr = current->rel_filepos;
3517
3518 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
3519 object file produced by the assembler is supposed to point to
3520 information about how much room is required by objects of
3521 various different sizes. I think this only matters if we
3522 want the linker to compute the best size to use, or
3523 something. I don't know what happens if the information is
3524 not present. */
3525 section.s_lnnoptr = 0;
3526
3527 section.s_nreloc = current->reloc_count;
3528 section.s_nlnno = 0;
3529 section.s_flags = ecoff_sec_to_styp_flags (current->name,
3530 current->flags);
3531
3532 bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff);
3533 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
3534 return false;
3535
3536 if ((section.s_flags & STYP_TEXT) != 0)
3537 {
3538 text_size += bfd_get_section_size_before_reloc (current);
3539 if (text_start == 0 || text_start > vma)
3540 text_start = vma;
3541 }
3542 else if ((section.s_flags & STYP_RDATA) != 0
3543 || (section.s_flags & STYP_DATA) != 0
3544 || (section.s_flags & STYP_LIT8) != 0
3545 || (section.s_flags & STYP_LIT4) != 0
3546 || (section.s_flags & STYP_SDATA) != 0)
3547 {
3548 data_size += bfd_get_section_size_before_reloc (current);
3549 if (data_start == 0 || data_start > vma)
3550 data_start = vma;
3551 }
3552 else if ((section.s_flags & STYP_BSS) != 0
3553 || (section.s_flags & STYP_SBSS) != 0)
3554 bss_size += bfd_get_section_size_before_reloc (current);
3555 }
3556
3557 /* Set up the file header. */
3558
3559 if (abfd->xvec->header_byteorder_big_p != false)
3560 internal_f.f_magic = backend->big_magic;
3561 else
3562 internal_f.f_magic = backend->little_magic;
3563
3564 /* We will NOT put a fucking timestamp in the header here. Every
3565 time you put it back, I will come in and take it out again. I'm
3566 sorry. This field does not belong here. We fill it with a 0 so
3567 it compares the same but is not a reasonable time. --
3568 gnu@cygnus.com. */
3569 internal_f.f_timdat = 0;
3570
3571 if (bfd_get_symcount (abfd) != 0)
3572 {
3573 /* The ECOFF f_nsyms field is not actually the number of
3574 symbols, it's the size of symbolic information header. */
3575 internal_f.f_nsyms = external_hdr_size;
3576 internal_f.f_symptr = sym_base;
3577 }
3578 else
3579 {
3580 internal_f.f_nsyms = 0;
3581 internal_f.f_symptr = 0;
3582 }
3583
3584 internal_f.f_opthdr = aoutsz;
3585
3586 internal_f.f_flags = F_LNNO;
3587 if (reloc_size == 0)
3588 internal_f.f_flags |= F_RELFLG;
3589 if (bfd_get_symcount (abfd) == 0)
3590 internal_f.f_flags |= F_LSYMS;
3591 if (abfd->flags & EXEC_P)
3592 internal_f.f_flags |= F_EXEC;
3593
3594 if (! abfd->xvec->byteorder_big_p)
3595 internal_f.f_flags |= F_AR32WR;
3596 else
3597 internal_f.f_flags |= F_AR32W;
3598
3599 /* Set up the ``optional'' header. */
3600 if ((abfd->flags & D_PAGED) != 0)
3601 internal_a.magic = ECOFF_AOUT_ZMAGIC;
3602 else
3603 internal_a.magic = ECOFF_AOUT_OMAGIC;
3604
3605 /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
3606 linker happy. But, is it right? */
3607 internal_a.vstamp = 0x20a;
3608
3609 /* At least on Ultrix, these have to be rounded to page boundaries.
3610 FIXME: Is this true on other platforms? */
3611 if ((abfd->flags & D_PAGED) != 0)
3612 {
3613 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
3614 internal_a.text_start = text_start &~ (round - 1);
3615 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
3616 internal_a.data_start = data_start &~ (round - 1);
3617 }
3618 else
3619 {
3620 internal_a.tsize = text_size;
3621 internal_a.text_start = text_start;
3622 internal_a.dsize = data_size;
3623 internal_a.data_start = data_start;
3624 }
3625
3626 /* On Ultrix, the initial portions of the .sbss and .bss segments
3627 are at the end of the data section. The bsize field in the
3628 optional header records how many bss bytes are required beyond
3629 those in the data section. The value is not rounded to a page
3630 boundary. */
3631 if (bss_size < internal_a.dsize - data_size)
3632 bss_size = 0;
3633 else
3634 bss_size -= internal_a.dsize - data_size;
3635 internal_a.bsize = bss_size;
3636 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
3637
3638 internal_a.entry = bfd_get_start_address (abfd);
3639
3640 internal_a.gp_value = ecoff_data (abfd)->gp;
3641
3642 internal_a.gprmask = ecoff_data (abfd)->gprmask;
3643 for (i = 0; i < 4; i++)
3644 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
3645
3646 /* Write out the file header and the optional header. */
3647
3648 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3649 return false;
3650
3651 buff = (PTR) alloca (filhsz);
3652 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
3653 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
3654 return false;
3655
3656 buff = (PTR) alloca (aoutsz);
3657 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
3658 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
3659 return false;
3660
3661 /* Write out the relocs. */
3662 for (current = abfd->sections;
3663 current != (asection *) NULL;
3664 current = current->next)
3665 {
3666 arelent **reloc_ptr_ptr;
3667 arelent **reloc_end;
3668 char *out_ptr;
3669
3670 if (current->reloc_count == 0)
3671 continue;
3672
3673 buff = bfd_alloc (abfd, current->reloc_count * external_reloc_size);
3674 if (buff == NULL)
3675 {
3676 bfd_error = no_memory;
3677 return false;
3678 }
3679
3680 reloc_ptr_ptr = current->orelocation;
3681 reloc_end = reloc_ptr_ptr + current->reloc_count;
3682 out_ptr = (char *) buff;
3683 for (;
3684 reloc_ptr_ptr < reloc_end;
3685 reloc_ptr_ptr++, out_ptr += external_reloc_size)
3686 {
3687 arelent *reloc;
3688 asymbol *sym;
3689 struct internal_reloc in;
3690
3691 memset (&in, 0, sizeof in);
3692
3693 reloc = *reloc_ptr_ptr;
3694 sym = *reloc->sym_ptr_ptr;
3695
3696 /* This must be an ECOFF reloc. */
3697 BFD_ASSERT (reloc->howto != (reloc_howto_type *) NULL
3698 && reloc->howto >= ecoff_howto_table
3699 && (reloc->howto
3700 < (ecoff_howto_table + ECOFF_HOWTO_COUNT)));
3701
3702 in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
3703 in.r_type = reloc->howto->type;
3704
3705 /* If this is a REFHI reloc, the next one must be a REFLO
3706 reloc for the same symbol. */
3707 BFD_ASSERT (in.r_type != ECOFF_R_REFHI
3708 || (reloc_ptr_ptr < reloc_end
3709 && (reloc_ptr_ptr[1]->howto
3710 != (reloc_howto_type *) NULL)
3711 && (reloc_ptr_ptr[1]->howto->type
3712 == ECOFF_R_REFLO)
3713 && (sym == *reloc_ptr_ptr[1]->sym_ptr_ptr)));
3714
3715 if ((sym->flags & BSF_SECTION_SYM) == 0)
3716 {
3717 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
3718 in.r_extern = 1;
3719 }
3720 else
3721 {
3722 CONST char *name;
3723
3724 name = bfd_get_section_name (abfd, bfd_get_section (sym));
3725 if (strcmp (name, ".text") == 0)
3726 in.r_symndx = RELOC_SECTION_TEXT;
3727 else if (strcmp (name, ".rdata") == 0)
3728 in.r_symndx = RELOC_SECTION_RDATA;
3729 else if (strcmp (name, ".data") == 0)
3730 in.r_symndx = RELOC_SECTION_DATA;
3731 else if (strcmp (name, ".sdata") == 0)
3732 in.r_symndx = RELOC_SECTION_SDATA;
3733 else if (strcmp (name, ".sbss") == 0)
3734 in.r_symndx = RELOC_SECTION_SBSS;
3735 else if (strcmp (name, ".bss") == 0)
3736 in.r_symndx = RELOC_SECTION_BSS;
3737 else if (strcmp (name, ".init") == 0)
3738 in.r_symndx = RELOC_SECTION_INIT;
3739 else if (strcmp (name, ".lit8") == 0)
3740 in.r_symndx = RELOC_SECTION_LIT8;
3741 else if (strcmp (name, ".lit4") == 0)
3742 in.r_symndx = RELOC_SECTION_LIT4;
3743 else
3744 abort ();
3745 in.r_extern = 0;
3746 }
3747
3748 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
3749 }
3750
3751 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
3752 return false;
3753 if (bfd_write (buff, external_reloc_size, current->reloc_count, abfd)
3754 != external_reloc_size * current->reloc_count)
3755 return false;
3756 bfd_release (abfd, buff);
3757 }
3758
3759 /* Write out the symbolic debugging information. */
3760 if (bfd_get_symcount (abfd) > 0)
3761 {
3762 HDRR *symhdr;
3763 unsigned long sym_offset;
3764
3765 /* Set up the offsets in the symbolic header. */
3766 symhdr = &ecoff_data (abfd)->symbolic_header;
3767 sym_offset = ecoff_data (abfd)->sym_filepos + external_hdr_size;
3768
3769#define SET(offset, size, ptr) \
3770 if (symhdr->size == 0) \
3771 symhdr->offset = 0; \
3772 else \
3773 symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
3774 - (char *) ecoff_data (abfd)->raw_syments) \
3775 + sym_offset);
3776
3777 SET (cbLineOffset, cbLine, line);
3778 SET (cbDnOffset, idnMax, external_dnr);
3779 SET (cbPdOffset, ipdMax, external_pdr);
3780 SET (cbSymOffset, isymMax, external_sym);
3781 SET (cbOptOffset, ioptMax, external_opt);
3782 SET (cbAuxOffset, iauxMax, external_aux);
3783 SET (cbSsOffset, issMax, ss);
3784 SET (cbSsExtOffset, issExtMax, ssext);
3785 SET (cbFdOffset, ifdMax, external_fdr);
3786 SET (cbRfdOffset, crfd, external_rfd);
3787 SET (cbExtOffset, iextMax, external_ext);
3788#undef SET
3789
3790 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
3791 SEEK_SET) != 0)
3792 return false;
3793 buff = (PTR) alloca (external_hdr_size);
3794 (*backend->swap_hdr_out) (abfd, &ecoff_data (abfd)->symbolic_header,
3795 buff);
3796 if (bfd_write (buff, 1, external_hdr_size, abfd) != external_hdr_size)
3797 return false;
3798 if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
3799 ecoff_data (abfd)->raw_size, abfd)
3800 != ecoff_data (abfd)->raw_size)
3801 return false;
3802 }
3803 else if ((abfd->flags & EXEC_P) != 0
3804 && (abfd->flags & D_PAGED) != 0)
3805 {
3806 char c;
3807
3808 /* A demand paged executable must occupy an even number of
3809 pages. */
3810 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3811 SEEK_SET) != 0)
3812 return false;
3813 if (bfd_read (&c, 1, 1, abfd) == 0)
3814 c = 0;
3815 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3816 SEEK_SET) != 0)
3817 return false;
3818 if (bfd_write (&c, 1, 1, abfd) != 1)
3819 return false;
3820 }
3821
3822 return true;
3823}
3824\f
3825/* Archive handling. ECOFF uses what appears to be a unique type of
3826 archive header (which I call an armap). The byte ordering of the
3827 armap and the contents are encoded in the name of the armap itself.
3828 At least for now, we only support archives with the same byte
3829 ordering in the armap and the contents.
3830
3831 The first four bytes in the armap are the number of symbol
3832 definitions. This is always a power of two.
3833
3834 This is followed by the symbol definitions. Each symbol definition
3835 occupies 8 bytes. The first four bytes are the offset from the
3836 start of the armap strings to the null-terminated string naming
3837 this symbol. The second four bytes are the file offset to the
3838 archive member which defines this symbol. If the second four bytes
3839 are 0, then this is not actually a symbol definition, and it should
3840 be ignored.
3841
3842 The symbols are hashed into the armap with a closed hashing scheme.
3843 See the functions below for the details of the algorithm.
3844
3845 We could use the hash table when looking up symbols in a library.
3846 This would require a new BFD target entry point to replace the
3847 bfd_get_next_mapent function used by the linker.
3848
3849 After the symbol definitions comes four bytes holding the size of
3850 the string table, followed by the string table itself. */
3851
3852/* The name of an archive headers looks like this:
3853 __________E[BL]E[BL]_ (with a trailing space).
3854 The trailing space is changed to an X if the archive is changed to
3855 indicate that the armap is out of date. */
3856
3857#define ARMAP_BIG_ENDIAN 'B'
3858#define ARMAP_LITTLE_ENDIAN 'L'
3859#define ARMAP_MARKER 'E'
3860#define ARMAP_START "__________"
3861#define ARMAP_HEADER_MARKER_INDEX 10
3862#define ARMAP_HEADER_ENDIAN_INDEX 11
3863#define ARMAP_OBJECT_MARKER_INDEX 12
3864#define ARMAP_OBJECT_ENDIAN_INDEX 13
3865#define ARMAP_END_INDEX 14
3866#define ARMAP_END "_ "
3867
3868/* This is a magic number used in the hashing algorithm. */
3869#define ARMAP_HASH_MAGIC 0x9dd68ab5
3870
3871/* This returns the hash value to use for a string. It also sets
3872 *REHASH to the rehash adjustment if the first slot is taken. SIZE
3873 is the number of entries in the hash table, and HLOG is the log
3874 base 2 of SIZE. */
3875
3876static unsigned int
3877ecoff_armap_hash (s, rehash, size, hlog)
3878 CONST char *s;
3879 unsigned int *rehash;
3880 unsigned int size;
3881 unsigned int hlog;
3882{
3883 unsigned int hash;
3884
3885 hash = *s++;
3886 while (*s != '\0')
3887 hash = ((hash >> 27) | (hash << 5)) + *s++;
3888 hash *= ARMAP_HASH_MAGIC;
3889 *rehash = (hash & (size - 1)) | 1;
3890 return hash >> (32 - hlog);
3891}
3892
3893/* Read in the armap. */
3894
3895boolean
3896ecoff_slurp_armap (abfd)
3897 bfd *abfd;
3898{
3899 char nextname[17];
3900 unsigned int i;
3901 struct areltdata *mapdata;
3902 bfd_size_type parsed_size;
3903 char *raw_armap;
3904 struct artdata *ardata;
3905 unsigned int count;
3906 char *raw_ptr;
3907 struct symdef *symdef_ptr;
3908 char *stringbase;
3909
3910 /* Get the name of the first element. */
3911 i = bfd_read ((PTR) nextname, 1, 16, abfd);
3912 if (i == 0)
3913 return true;
3914 if (i != 16)
3915 return false;
3916
3917 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
3918
3919 /* See if the first element is an armap. */
3920 if (strncmp (nextname, ARMAP_START, sizeof ARMAP_START - 1) != 0
3921 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3922 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3923 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3924 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3925 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3926 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3927 || strncmp (nextname + ARMAP_END_INDEX,
3928 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3929 {
3930 bfd_has_map (abfd) = false;
3931 return true;
3932 }
3933
3934 /* Make sure we have the right byte ordering. */
3935 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3936 ^ (abfd->xvec->header_byteorder_big_p != false))
3937 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3938 ^ (abfd->xvec->byteorder_big_p != false)))
3939 {
3940 bfd_error = wrong_format;
3941 return false;
3942 }
3943
3944 /* Read in the armap. */
3945 ardata = bfd_ardata (abfd);
3946 mapdata = snarf_ar_hdr (abfd);
3947 if (mapdata == (struct areltdata *) NULL)
3948 return false;
3949 parsed_size = mapdata->parsed_size;
3950 bfd_release (abfd, (PTR) mapdata);
3951
3952 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3953 if (raw_armap == (char *) NULL)
3954 {
3955 bfd_error = no_memory;
3956 return false;
3957 }
3958
3959 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3960 {
3961 bfd_error = malformed_archive;
3962 bfd_release (abfd, (PTR) raw_armap);
3963 return false;
3964 }
3965
3966 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3967
3968 ardata->symdef_count = 0;
3969 ardata->cache = (struct ar_cache *) NULL;
3970
3971 /* Hack: overlay the symdefs on top of the raw archive data. This
3972 is the way do_slurp_bsd_armap works. */
3973 raw_ptr = raw_armap + LONG_SIZE;
3974 symdef_ptr = (struct symdef *) raw_ptr;
3975 ardata->symdefs = (carsym *) symdef_ptr;
3976 stringbase = raw_ptr + count * (2 * LONG_SIZE) + LONG_SIZE;
3977
3978#ifdef CHECK_ARMAP_HASH
3979 {
3980 unsigned int hlog;
3981
3982 /* Double check that I have the hashing algorithm right by making
3983 sure that every symbol can be looked up successfully. */
3984 hlog = 0;
3985 for (i = 1; i < count; i <<= 1)
3986 hlog++;
3987 BFD_ASSERT (i == count);
3988
3989 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
3990 {
3991 unsigned int name_offset, file_offset;
3992 unsigned int hash, rehash, srch;
3993
3994 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3995 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
3996 if (file_offset == 0)
3997 continue;
3998 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3999 hlog);
4000 if (hash == i)
4001 continue;
4002
4003 /* See if we can rehash to this location. */
4004 for (srch = (hash + rehash) & (count - 1);
4005 srch != hash && srch != i;
4006 srch = (srch + rehash) & (count - 1))
4007 BFD_ASSERT (bfd_h_get_32 (abfd,
4008 (PTR) (raw_armap
4009 + LONG_SIZE
4010 + (srch * 2 * LONG_SIZE)
4011 + LONG_SIZE))
4012 != 0);
4013 BFD_ASSERT (srch == i);
4014 }
4015 }
4016
4017 raw_ptr = raw_armap + LONG_SIZE;
4018#endif /* CHECK_ARMAP_HASH */
4019
4020 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
4021 {
4022 unsigned int name_offset, file_offset;
4023
4024 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
4025 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
4026 if (file_offset == 0)
4027 continue;
4028 symdef_ptr->s.name = stringbase + name_offset;
4029 symdef_ptr->file_offset = file_offset;
4030 ++symdef_ptr;
4031 ++ardata->symdef_count;
4032 }
4033
4034 ardata->first_file_filepos = bfd_tell (abfd);
4035 /* Pad to an even boundary. */
4036 ardata->first_file_filepos += ardata->first_file_filepos % 2;
4037
4038 bfd_has_map (abfd) = true;
4039
4040 return true;
4041}
4042
4043/* Write out an armap. */
4044
4045boolean
4046ecoff_write_armap (abfd, elength, map, orl_count, stridx)
4047 bfd *abfd;
4048 unsigned int elength;
4049 struct orl *map;
4050 unsigned int orl_count;
4051 int stridx;
4052{
4053 unsigned int hashsize, hashlog;
4054 unsigned int symdefsize;
4055 int padit;
4056 unsigned int stringsize;
4057 unsigned int mapsize;
4058 file_ptr firstreal;
4059 struct ar_hdr hdr;
4060 struct stat statbuf;
4061 unsigned int i;
4062 bfd_byte temp[LONG_SIZE];
4063 bfd_byte *hashtable;
4064 bfd *current;
4065 bfd *last_elt;
4066
4067 /* Ultrix appears to use as a hash table size the least power of two
4068 greater than twice the number of entries. */
4069 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
4070 ;
4071 hashsize = 1 << hashlog;
4072
4073 symdefsize = hashsize * 2 * LONG_SIZE;
4074 padit = stridx % 2;
4075 stringsize = stridx + padit;
4076
4077 /* Include 8 bytes to store symdefsize and stringsize in output. */
4078 mapsize = LONG_SIZE + symdefsize + stringsize + LONG_SIZE;
4079
4080 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
4081
4082 memset ((PTR) &hdr, 0, sizeof hdr);
4083
4084 /* Work out the ECOFF armap name. */
4085 strcpy (hdr.ar_name, ARMAP_START);
4086 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
4087 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
4088 (abfd->xvec->header_byteorder_big_p
4089 ? ARMAP_BIG_ENDIAN
4090 : ARMAP_LITTLE_ENDIAN);
4091 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
4092 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
4093 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
4094 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
4095
4096 /* Write the timestamp of the archive header to be just a little bit
4097 later than the timestamp of the file, otherwise the linker will
4098 complain that the index is out of date. Actually, the Ultrix
4099 linker just checks the archive name; the GNU linker may check the
4100 date. */
4101 stat (abfd->filename, &statbuf);
4102 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
4103
4104 /* The DECstation uses zeroes for the uid, gid and mode of the
4105 armap. */
4106 hdr.ar_uid[0] = '0';
4107 hdr.ar_gid[0] = '0';
4108 hdr.ar_mode[0] = '0';
4109
4110 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
4111
4112 hdr.ar_fmag[0] = '`';
4113 hdr.ar_fmag[1] = '\n';
4114
4115 /* Turn all null bytes in the header into spaces. */
4116 for (i = 0; i < sizeof (struct ar_hdr); i++)
4117 if (((char *)(&hdr))[i] == '\0')
4118 (((char *)(&hdr))[i]) = ' ';
4119
4120 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
4121 != sizeof (struct ar_hdr))
4122 return false;
4123
4124 bfd_h_put_32 (abfd, hashsize, temp);
4125 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4126 return false;
4127
4128 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
4129
4130 current = abfd->archive_head;
4131 last_elt = current;
4132 for (i = 0; i < orl_count; i++)
4133 {
4134 unsigned int hash, rehash;
4135
4136 /* Advance firstreal to the file position of this archive
4137 element. */
4138 if (((bfd *) map[i].pos) != last_elt)
4139 {
4140 do
4141 {
4142 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
4143 firstreal += firstreal % 2;
4144 current = current->next;
4145 }
4146 while (current != (bfd *) map[i].pos);
4147 }
4148
4149 last_elt = current;
4150
4151 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
4152 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4153 + (hash * 2 * LONG_SIZE)
4154 + LONG_SIZE))
4155 != 0)
4156 {
4157 unsigned int srch;
4158
4159 /* The desired slot is already taken. */
4160 for (srch = (hash + rehash) & (hashsize - 1);
4161 srch != hash;
4162 srch = (srch + rehash) & (hashsize - 1))
4163 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4164 + (srch * 2 * LONG_SIZE)
4165 + LONG_SIZE))
4166 == 0)
4167 break;
4168
4169 BFD_ASSERT (srch != hash);
4170
4171 hash = srch;
4172 }
4173
4174 bfd_h_put_32 (abfd, map[i].namidx,
4175 (PTR) (hashtable + hash * 2 * LONG_SIZE));
4176 bfd_h_put_32 (abfd, firstreal,
4177 (PTR) (hashtable + hash * 2 * LONG_SIZE + LONG_SIZE));
4178 }
4179
4180 if (bfd_write (hashtable, 1, symdefsize, abfd) != symdefsize)
4181 return false;
4182
4183 bfd_release (abfd, hashtable);
4184
4185 /* Now write the strings. */
4186 bfd_h_put_32 (abfd, stringsize, temp);
4187 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4188 return false;
4189 for (i = 0; i < orl_count; i++)
4190 {
4191 bfd_size_type len;
4192
4193 len = strlen (*map[i].name) + 1;
4194 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
4195 return false;
4196 }
4197
4198 /* The spec sez this should be a newline. But in order to be
4199 bug-compatible for DECstation ar we use a null. */
4200 if (padit)
4201 {
4202 if (bfd_write ("\0", 1, 1, abfd) != 1)
4203 return false;
4204 }
4205
4206 return true;
4207}
4208
4209/* See whether this BFD is an archive. If it is, read in the armap
4210 and the extended name table. */
4211
4212bfd_target *
4213ecoff_archive_p (abfd)
4214 bfd *abfd;
4215{
4216 char armag[SARMAG + 1];
4217
4218 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
4219 || strncmp (armag, ARMAG, SARMAG) != 0)
4220 {
4221 bfd_error = wrong_format;
4222 return (bfd_target *) NULL;
4223 }
4224
4225 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
4226 involves a cast, we can't do it as the left operand of
4227 assignment. */
4228 abfd->tdata.aout_ar_data =
4229 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
4230
4231 if (bfd_ardata (abfd) == (struct artdata *) NULL)
4232 {
4233 bfd_error = no_memory;
4234 return (bfd_target *) NULL;
4235 }
4236
4237 bfd_ardata (abfd)->first_file_filepos = SARMAG;
4238
4239 if (ecoff_slurp_armap (abfd) == false
4240 || ecoff_slurp_extended_name_table (abfd) == false)
4241 {
4242 bfd_release (abfd, bfd_ardata (abfd));
4243 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
4244 return (bfd_target *) NULL;
4245 }
4246
4247 return abfd->xvec;
4248}
This page took 0.174203 seconds and 4 git commands to generate.