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