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