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