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