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