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