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