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