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