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