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