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