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