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