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