Wed Mar 30 16:25:41 1994 Ian Lance Taylor (ian@tweedledumb.cygnus.com)
[deliverable/binutils-gdb.git] / bfd / ecoff.c
CommitLineData
dae31cf5
ILT
1/* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support added by Ian Lance Taylor, ian@cygnus.com.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22#include "bfd.h"
23#include "sysdep.h"
4c3721d5 24#include "bfdlink.h"
dae31cf5 25#include "libbfd.h"
dae31cf5
ILT
26#include "aout/ar.h"
27#include "aout/ranlib.h"
28
29/* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
30 some other stuff which we don't want and which conflicts with stuff
31 we do want. */
32#include "libaout.h"
33#include "aout/aout64.h"
34#undef N_ABS
35#undef exec_hdr
36#undef obj_sym_filepos
37
38#include "coff/internal.h"
39#include "coff/sym.h"
40#include "coff/symconst.h"
41#include "coff/ecoff.h"
42#include "libcoff.h"
43#include "libecoff.h"
44\f
45/* Prototypes for static functions. */
46
a7853216 47static int ecoff_get_magic PARAMS ((bfd *abfd));
966e0a16 48static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *abfd));
9783e04a 49static boolean ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
dae31cf5
ILT
50 asymbol *asym, int ext,
51 asymbol **indirect_ptr_ptr));
52static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
53 RNDXR *rndx, long isym,
54 CONST char *which));
55static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
56 unsigned int indx, int bigendian));
dae31cf5
ILT
57static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
58 asymbol **symbols));
dae31cf5 59static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
966e0a16 60static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
8d12f138
ILT
61static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
62static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
dae31cf5
ILT
63static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
64 unsigned int *rehash,
65 unsigned int size,
66 unsigned int hlog));
67\f
dae31cf5
ILT
68/* This stuff is somewhat copied from coffcode.h. */
69
70static asection bfd_debug_section = { "*DEBUG*" };
71
48edba81
ILT
72/* Create an ECOFF object. */
73
74boolean
75ecoff_mkobject (abfd)
76 bfd *abfd;
77{
78 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
79 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
80 if (abfd->tdata.ecoff_obj_data == NULL)
81 {
d1ad85a6 82 bfd_set_error (bfd_error_no_memory);
48edba81
ILT
83 return false;
84 }
85
48edba81
ILT
86 return true;
87}
88
a7853216
ILT
89/* This is a hook called by coff_real_object_p to create any backend
90 specific information. */
91
92PTR
93ecoff_mkobject_hook (abfd, filehdr, aouthdr)
94 bfd *abfd;
95 PTR filehdr;
96 PTR aouthdr;
97{
98 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
99 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
100 ecoff_data_type *ecoff;
101 asection *regsec;
102
103 if (ecoff_mkobject (abfd) == false)
104 return NULL;
105
106 ecoff = ecoff_data (abfd);
107 ecoff->gp_size = 8;
108 ecoff->sym_filepos = internal_f->f_symptr;
109
110 /* Create the .reginfo section to give programs outside BFD a way to
111 see the information stored in the a.out header. See the comment
112 in coff/ecoff.h. */
113 regsec = bfd_make_section (abfd, REGINFO);
114 if (regsec == NULL)
115 return NULL;
116
117 if (internal_a != (struct internal_aouthdr *) NULL)
118 {
119 int i;
120
121 ecoff->text_start = internal_a->text_start;
122 ecoff->text_end = internal_a->text_start + internal_a->tsize;
123 ecoff->gp = internal_a->gp_value;
124 ecoff->gprmask = internal_a->gprmask;
125 for (i = 0; i < 4; i++)
126 ecoff->cprmask[i] = internal_a->cprmask[i];
127 ecoff->fprmask = internal_a->fprmask;
128 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
129 abfd->flags |= D_PAGED;
130 }
131
132 /* It turns out that no special action is required by the MIPS or
133 Alpha ECOFF backends. They have different information in the
134 a.out header, but we just copy it all (e.g., gprmask, cprmask and
135 fprmask) and let the swapping routines ensure that only relevant
136 information is written out. */
137
138 return (PTR) ecoff;
139}
140
dae31cf5
ILT
141/* This is a hook needed by SCO COFF, but we have nothing to do. */
142
728472f1 143/*ARGSUSED*/
dae31cf5
ILT
144asection *
145ecoff_make_section_hook (abfd, name)
146 bfd *abfd;
147 char *name;
148{
149 return (asection *) NULL;
150}
151
152/* Initialize a new section. */
153
154boolean
155ecoff_new_section_hook (abfd, section)
156 bfd *abfd;
157 asection *section;
158{
3f048f7f
ILT
159 /* For the .pdata section, which has a special meaning on the Alpha,
160 we set the alignment to 8. We correct this later in
161 ecoff_compute_section_file_positions. We do this hackery because
162 we need to know the exact unaligned size of the .pdata section in
163 order to set the lnnoptr field correctly. */
164 if (strcmp (section->name, _PDATA) == 0)
165 section->alignment_power = 3;
166 else
167 section->alignment_power = abfd->xvec->align_power_min;
dae31cf5
ILT
168
169 if (strcmp (section->name, _TEXT) == 0)
170 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
171 else if (strcmp (section->name, _DATA) == 0
172 || strcmp (section->name, _SDATA) == 0)
173 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
174 else if (strcmp (section->name, _RDATA) == 0
175 || strcmp (section->name, _LIT8) == 0
176 || strcmp (section->name, _LIT4) == 0)
177 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
178 else if (strcmp (section->name, _BSS) == 0
179 || strcmp (section->name, _SBSS) == 0)
180 section->flags |= SEC_ALLOC;
a7853216
ILT
181 else if (strcmp (section->name, REGINFO) == 0)
182 {
a5a835ff
ILT
183 /* Setting SEC_SHARED_LIBRARY should make the linker leave the
184 section completely alone. */
185 section->flags |= (SEC_SHARED_LIBRARY
186 | SEC_HAS_CONTENTS
187 | SEC_NEVER_LOAD);
a7853216
ILT
188 section->_raw_size = sizeof (struct ecoff_reginfo);
189 }
dae31cf5
ILT
190
191 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
192 uncertain about .init on some systems and I don't know how shared
193 libraries work. */
194
195 return true;
196}
197
a7853216
ILT
198/* Determine the machine architecture and type. This is called from
199 the generic COFF routines. It is the inverse of ecoff_get_magic,
200 below. This could be an ECOFF backend routine, with one version
201 for each target, but there aren't all that many ECOFF targets. */
dae31cf5
ILT
202
203boolean
204ecoff_set_arch_mach_hook (abfd, filehdr)
205 bfd *abfd;
206 PTR filehdr;
207{
208 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
209 enum bfd_architecture arch;
a7853216 210 unsigned long mach;
dae31cf5
ILT
211
212 switch (internal_f->f_magic)
213 {
214 case MIPS_MAGIC_1:
215 case MIPS_MAGIC_LITTLE:
216 case MIPS_MAGIC_BIG:
217 arch = bfd_arch_mips;
a7853216
ILT
218 mach = 3000;
219 break;
220
221 case MIPS_MAGIC_LITTLE2:
222 case MIPS_MAGIC_BIG2:
223 /* MIPS ISA level 2: the r6000 */
224 arch = bfd_arch_mips;
225 mach = 6000;
226 break;
227
228 case MIPS_MAGIC_LITTLE3:
229 case MIPS_MAGIC_BIG3:
230 /* MIPS ISA level 3: the r4000 */
231 arch = bfd_arch_mips;
232 mach = 4000;
dae31cf5
ILT
233 break;
234
235 case ALPHA_MAGIC:
236 arch = bfd_arch_alpha;
a7853216 237 mach = 0;
dae31cf5
ILT
238 break;
239
240 default:
241 arch = bfd_arch_obscure;
a7853216 242 mach = 0;
dae31cf5
ILT
243 break;
244 }
245
a7853216
ILT
246 return bfd_default_set_arch_mach (abfd, arch, mach);
247}
dae31cf5 248
a7853216
ILT
249/* Get the magic number to use based on the architecture and machine.
250 This is the inverse of ecoff_set_arch_mach_hook, above. */
251
252static int
253ecoff_get_magic (abfd)
254 bfd *abfd;
255{
256 int big, little;
257
258 switch (bfd_get_arch (abfd))
259 {
260 case bfd_arch_mips:
261 switch (bfd_get_mach (abfd))
262 {
263 default:
264 case 0:
265 case 3000:
266 big = MIPS_MAGIC_BIG;
267 little = MIPS_MAGIC_LITTLE;
268 break;
269
270 case 6000:
271 big = MIPS_MAGIC_BIG2;
272 little = MIPS_MAGIC_LITTLE2;
273 break;
274
275 case 4000:
276 big = MIPS_MAGIC_BIG3;
277 little = MIPS_MAGIC_LITTLE3;
278 break;
279 }
280
281 return abfd->xvec->byteorder_big_p ? big : little;
282
283 case bfd_arch_alpha:
284 return ALPHA_MAGIC;
285
286 default:
287 abort ();
288 return 0;
289 }
dae31cf5
ILT
290}
291
292/* Get the section s_flags to use for a section. */
293
294long
295ecoff_sec_to_styp_flags (name, flags)
296 CONST char *name;
297 flagword flags;
298{
299 long styp;
300
301 styp = 0;
302
303 if (strcmp (name, _TEXT) == 0)
304 styp = STYP_TEXT;
305 else if (strcmp (name, _DATA) == 0)
306 styp = STYP_DATA;
307 else if (strcmp (name, _SDATA) == 0)
308 styp = STYP_SDATA;
309 else if (strcmp (name, _RDATA) == 0)
310 styp = STYP_RDATA;
c9668c58
ILT
311 else if (strcmp (name, _LITA) == 0)
312 styp = STYP_LITA;
dae31cf5
ILT
313 else if (strcmp (name, _LIT8) == 0)
314 styp = STYP_LIT8;
315 else if (strcmp (name, _LIT4) == 0)
316 styp = STYP_LIT4;
317 else if (strcmp (name, _BSS) == 0)
318 styp = STYP_BSS;
319 else if (strcmp (name, _SBSS) == 0)
320 styp = STYP_SBSS;
321 else if (strcmp (name, _INIT) == 0)
322 styp = STYP_ECOFF_INIT;
a7853216
ILT
323 else if (strcmp (name, _FINI) == 0)
324 styp = STYP_ECOFF_FINI;
966e0a16
ILT
325 else if (strcmp (name, _PDATA) == 0)
326 styp = STYP_PDATA;
327 else if (strcmp (name, _XDATA) == 0)
328 styp = STYP_XDATA;
dae31cf5
ILT
329 else if (flags & SEC_CODE)
330 styp = STYP_TEXT;
331 else if (flags & SEC_DATA)
332 styp = STYP_DATA;
333 else if (flags & SEC_READONLY)
334 styp = STYP_RDATA;
335 else if (flags & SEC_LOAD)
336 styp = STYP_REG;
337 else
338 styp = STYP_BSS;
339
340 if (flags & SEC_NEVER_LOAD)
341 styp |= STYP_NOLOAD;
342
343 return styp;
344}
345
346/* Get the BFD flags to use for a section. */
347
728472f1 348/*ARGSUSED*/
dae31cf5
ILT
349flagword
350ecoff_styp_to_sec_flags (abfd, hdr)
351 bfd *abfd;
352 PTR hdr;
353{
354 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
355 long styp_flags = internal_s->s_flags;
356 flagword sec_flags=0;
357
358 if (styp_flags & STYP_NOLOAD)
359 sec_flags |= SEC_NEVER_LOAD;
360
361 /* For 386 COFF, at least, an unloadable text or data section is
362 actually a shared library section. */
363 if ((styp_flags & STYP_TEXT)
a7853216
ILT
364 || (styp_flags & STYP_ECOFF_INIT)
365 || (styp_flags & STYP_ECOFF_FINI))
dae31cf5
ILT
366 {
367 if (sec_flags & SEC_NEVER_LOAD)
368 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
369 else
370 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
371 }
372 else if ((styp_flags & STYP_DATA)
373 || (styp_flags & STYP_RDATA)
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;
2495 bfd_size_type data_size;
2496 bfd_vma data_start;
2497 bfd_size_type bss_size;
80425e6c
JK
2498 PTR buff = NULL;
2499 PTR reloc_buff = NULL;
dae31cf5
ILT
2500 struct internal_filehdr internal_f;
2501 struct internal_aouthdr internal_a;
2502 int i;
2503
966e0a16
ILT
2504 /* Determine where the sections and relocs will go in the output
2505 file. */
2506 reloc_size = ecoff_compute_reloc_file_positions (abfd);
dae31cf5
ILT
2507
2508 count = 1;
dae31cf5
ILT
2509 for (current = abfd->sections;
2510 current != (asection *)NULL;
2511 current = current->next)
2512 {
8d12f138 2513 if (strcmp (current->name, REGINFO) == 0)
dae31cf5
ILT
2514 continue;
2515 current->target_index = count;
2516 ++count;
dae31cf5
ILT
2517 }
2518
dae31cf5
ILT
2519 if ((abfd->flags & D_PAGED) != 0)
2520 text_size = ecoff_sizeof_headers (abfd, false);
2521 else
2522 text_size = 0;
2523 text_start = 0;
2524 data_size = 0;
2525 data_start = 0;
2526 bss_size = 0;
2527
2528 /* Write section headers to the file. */
2529
80425e6c
JK
2530 /* Allocate buff big enough to hold a section header,
2531 file header, or a.out header. */
2532 {
2533 bfd_size_type siz;
2534 siz = scnhsz;
2535 if (siz < filhsz)
2536 siz = filhsz;
2537 if (siz < aoutsz)
2538 siz = aoutsz;
2539 buff = (PTR) malloc (siz);
2540 if (buff == NULL)
2541 {
2542 bfd_set_error (bfd_error_no_memory);
2543 goto error_return;
2544 }
2545 }
2546
dae31cf5
ILT
2547 internal_f.f_nscns = 0;
2548 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
80425e6c 2549 goto error_return;
dae31cf5
ILT
2550 for (current = abfd->sections;
2551 current != (asection *) NULL;
2552 current = current->next)
2553 {
2554 struct internal_scnhdr section;
2555 bfd_vma vma;
2556
a7853216
ILT
2557 if (strcmp (current->name, REGINFO) == 0)
2558 {
c9668c58 2559 BFD_ASSERT (current->reloc_count == 0);
a7853216
ILT
2560 continue;
2561 }
dae31cf5
ILT
2562
2563 ++internal_f.f_nscns;
2564
2565 strncpy (section.s_name, current->name, sizeof section.s_name);
2566
2567 /* FIXME: is this correct for shared libraries? I think it is
2568 but I have no platform to check. Ian Lance Taylor. */
2569 vma = bfd_get_section_vma (abfd, current);
2570 if (strcmp (current->name, _LIB) == 0)
2571 section.s_vaddr = 0;
2572 else
2573 section.s_vaddr = vma;
2574
2575 section.s_paddr = vma;
2576 section.s_size = bfd_get_section_size_before_reloc (current);
2577
e544ed4f
ILT
2578 /* If this section is unloadable then the scnptr will be 0. */
2579 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
dae31cf5
ILT
2580 section.s_scnptr = 0;
2581 else
2582 section.s_scnptr = current->filepos;
2583 section.s_relptr = current->rel_filepos;
2584
2585 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2586 object file produced by the assembler is supposed to point to
2587 information about how much room is required by objects of
2588 various different sizes. I think this only matters if we
2589 want the linker to compute the best size to use, or
2590 something. I don't know what happens if the information is
2591 not present. */
3f048f7f
ILT
2592 if (strcmp (current->name, _PDATA) != 0)
2593 section.s_lnnoptr = 0;
2594 else
2595 {
2596 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2597 hold the number of entries in the section (each entry is
2598 8 bytes). We stored this in the line_filepos field in
2599 ecoff_compute_section_file_positions. */
2600 section.s_lnnoptr = current->line_filepos;
2601 }
dae31cf5
ILT
2602
2603 section.s_nreloc = current->reloc_count;
2604 section.s_nlnno = 0;
2605 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2606 current->flags);
2607
2608 bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff);
2609 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
80425e6c 2610 goto error_return;
dae31cf5 2611
c9668c58
ILT
2612 if ((section.s_flags & STYP_TEXT) != 0
2613 || ((section.s_flags & STYP_RDATA) != 0
2614 && backend->rdata_in_text)
2615 || strcmp (current->name, _PDATA) == 0)
dae31cf5
ILT
2616 {
2617 text_size += bfd_get_section_size_before_reloc (current);
2618 if (text_start == 0 || text_start > vma)
2619 text_start = vma;
2620 }
2621 else if ((section.s_flags & STYP_RDATA) != 0
2622 || (section.s_flags & STYP_DATA) != 0
c9668c58 2623 || (section.s_flags & STYP_LITA) != 0
dae31cf5
ILT
2624 || (section.s_flags & STYP_LIT8) != 0
2625 || (section.s_flags & STYP_LIT4) != 0
966e0a16
ILT
2626 || (section.s_flags & STYP_SDATA) != 0
2627 || strcmp (current->name, _XDATA) == 0)
dae31cf5
ILT
2628 {
2629 data_size += bfd_get_section_size_before_reloc (current);
2630 if (data_start == 0 || data_start > vma)
2631 data_start = vma;
2632 }
2633 else if ((section.s_flags & STYP_BSS) != 0
2634 || (section.s_flags & STYP_SBSS) != 0)
2635 bss_size += bfd_get_section_size_before_reloc (current);
966e0a16
ILT
2636 else
2637 abort ();
dae31cf5
ILT
2638 }
2639
2640 /* Set up the file header. */
2641
a7853216 2642 internal_f.f_magic = ecoff_get_magic (abfd);
dae31cf5
ILT
2643
2644 /* We will NOT put a fucking timestamp in the header here. Every
2645 time you put it back, I will come in and take it out again. I'm
2646 sorry. This field does not belong here. We fill it with a 0 so
2647 it compares the same but is not a reasonable time. --
2648 gnu@cygnus.com. */
2649 internal_f.f_timdat = 0;
2650
2651 if (bfd_get_symcount (abfd) != 0)
2652 {
2653 /* The ECOFF f_nsyms field is not actually the number of
2654 symbols, it's the size of symbolic information header. */
2655 internal_f.f_nsyms = external_hdr_size;
3f048f7f 2656 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
dae31cf5
ILT
2657 }
2658 else
2659 {
2660 internal_f.f_nsyms = 0;
2661 internal_f.f_symptr = 0;
2662 }
2663
2664 internal_f.f_opthdr = aoutsz;
2665
2666 internal_f.f_flags = F_LNNO;
2667 if (reloc_size == 0)
2668 internal_f.f_flags |= F_RELFLG;
2669 if (bfd_get_symcount (abfd) == 0)
2670 internal_f.f_flags |= F_LSYMS;
2671 if (abfd->flags & EXEC_P)
2672 internal_f.f_flags |= F_EXEC;
2673
2674 if (! abfd->xvec->byteorder_big_p)
2675 internal_f.f_flags |= F_AR32WR;
2676 else
2677 internal_f.f_flags |= F_AR32W;
2678
2679 /* Set up the ``optional'' header. */
2680 if ((abfd->flags & D_PAGED) != 0)
2681 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2682 else
2683 internal_a.magic = ECOFF_AOUT_OMAGIC;
2684
3f048f7f
ILT
2685 /* FIXME: Is this really correct? */
2686 internal_a.vstamp = symhdr->vstamp;
dae31cf5
ILT
2687
2688 /* At least on Ultrix, these have to be rounded to page boundaries.
2689 FIXME: Is this true on other platforms? */
2690 if ((abfd->flags & D_PAGED) != 0)
2691 {
2692 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2693 internal_a.text_start = text_start &~ (round - 1);
2694 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2695 internal_a.data_start = data_start &~ (round - 1);
2696 }
2697 else
2698 {
2699 internal_a.tsize = text_size;
2700 internal_a.text_start = text_start;
2701 internal_a.dsize = data_size;
2702 internal_a.data_start = data_start;
2703 }
2704
2705 /* On Ultrix, the initial portions of the .sbss and .bss segments
2706 are at the end of the data section. The bsize field in the
2707 optional header records how many bss bytes are required beyond
2708 those in the data section. The value is not rounded to a page
2709 boundary. */
2710 if (bss_size < internal_a.dsize - data_size)
2711 bss_size = 0;
2712 else
2713 bss_size -= internal_a.dsize - data_size;
2714 internal_a.bsize = bss_size;
2715 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2716
2717 internal_a.entry = bfd_get_start_address (abfd);
2718
2719 internal_a.gp_value = ecoff_data (abfd)->gp;
2720
2721 internal_a.gprmask = ecoff_data (abfd)->gprmask;
48edba81 2722 internal_a.fprmask = ecoff_data (abfd)->fprmask;
dae31cf5
ILT
2723 for (i = 0; i < 4; i++)
2724 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2725
2726 /* Write out the file header and the optional header. */
2727
2728 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
80425e6c 2729 goto error_return;
dae31cf5 2730
dae31cf5
ILT
2731 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2732 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
80425e6c 2733 goto error_return;
dae31cf5 2734
dae31cf5
ILT
2735 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2736 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
80425e6c 2737 goto error_return;
dae31cf5 2738
8d12f138 2739 /* Build the external symbol information. This must be done before
966e0a16
ILT
2740 writing out the relocs so that we know the symbol indices. The
2741 condition checks makes sure this object was not created by
2742 ecoff_bfd_final_link, since if it was we do not want to tamper
2743 with the external symbols. */
3f048f7f 2744 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL)
dae31cf5 2745 {
966e0a16
ILT
2746 symhdr->iextMax = 0;
2747 symhdr->issExtMax = 0;
2748 debug->external_ext = debug->external_ext_end = NULL;
2749 debug->ssext = debug->ssext_end = NULL;
2750 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2751 (((abfd->flags & EXEC_P) == 0)
2752 ? true : false),
2753 ecoff_get_extr, ecoff_set_index)
2754 == false)
80425e6c 2755 goto error_return;
dae31cf5 2756
966e0a16
ILT
2757 /* Write out the relocs. */
2758 for (current = abfd->sections;
2759 current != (asection *) NULL;
2760 current = current->next)
dae31cf5 2761 {
966e0a16
ILT
2762 arelent **reloc_ptr_ptr;
2763 arelent **reloc_end;
2764 char *out_ptr;
dae31cf5 2765
966e0a16
ILT
2766 if (current->reloc_count == 0)
2767 continue;
dae31cf5 2768
80425e6c
JK
2769 reloc_buff =
2770 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2771 if (reloc_buff == NULL)
dae31cf5 2772 {
d1ad85a6 2773 bfd_set_error (bfd_error_no_memory);
80425e6c 2774 goto error_return;
dae31cf5 2775 }
966e0a16
ILT
2776
2777 reloc_ptr_ptr = current->orelocation;
2778 reloc_end = reloc_ptr_ptr + current->reloc_count;
80425e6c 2779 out_ptr = (char *) reloc_buff;
966e0a16
ILT
2780 for (;
2781 reloc_ptr_ptr < reloc_end;
2782 reloc_ptr_ptr++, out_ptr += external_reloc_size)
dae31cf5 2783 {
966e0a16
ILT
2784 arelent *reloc;
2785 asymbol *sym;
2786 struct internal_reloc in;
2787
68241b2b 2788 memset ((PTR) &in, 0, sizeof in);
966e0a16
ILT
2789
2790 reloc = *reloc_ptr_ptr;
2791 sym = *reloc->sym_ptr_ptr;
2792
2793 in.r_vaddr = (reloc->address
2794 + bfd_get_section_vma (abfd, current));
2795 in.r_type = reloc->howto->type;
2796
2797 if ((sym->flags & BSF_SECTION_SYM) == 0)
2798 {
2799 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2800 in.r_extern = 1;
2801 }
dae31cf5 2802 else
966e0a16
ILT
2803 {
2804 CONST char *name;
2805
2806 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2807 if (strcmp (name, ".text") == 0)
2808 in.r_symndx = RELOC_SECTION_TEXT;
2809 else if (strcmp (name, ".rdata") == 0)
2810 in.r_symndx = RELOC_SECTION_RDATA;
2811 else if (strcmp (name, ".data") == 0)
2812 in.r_symndx = RELOC_SECTION_DATA;
2813 else if (strcmp (name, ".sdata") == 0)
2814 in.r_symndx = RELOC_SECTION_SDATA;
2815 else if (strcmp (name, ".sbss") == 0)
2816 in.r_symndx = RELOC_SECTION_SBSS;
2817 else if (strcmp (name, ".bss") == 0)
2818 in.r_symndx = RELOC_SECTION_BSS;
2819 else if (strcmp (name, ".init") == 0)
2820 in.r_symndx = RELOC_SECTION_INIT;
2821 else if (strcmp (name, ".lit8") == 0)
2822 in.r_symndx = RELOC_SECTION_LIT8;
2823 else if (strcmp (name, ".lit4") == 0)
2824 in.r_symndx = RELOC_SECTION_LIT4;
2825 else if (strcmp (name, ".xdata") == 0)
2826 in.r_symndx = RELOC_SECTION_XDATA;
2827 else if (strcmp (name, ".pdata") == 0)
2828 in.r_symndx = RELOC_SECTION_PDATA;
2829 else if (strcmp (name, ".fini") == 0)
2830 in.r_symndx = RELOC_SECTION_FINI;
2831 else if (strcmp (name, ".lita") == 0)
2832 in.r_symndx = RELOC_SECTION_LITA;
2833 else if (strcmp (name, "*ABS*") == 0)
2834 in.r_symndx = RELOC_SECTION_ABS;
2835 else
2836 abort ();
2837 in.r_extern = 0;
2838 }
2839
2840 (*adjust_reloc_out) (abfd, reloc, &in);
2841
2842 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
dae31cf5
ILT
2843 }
2844
966e0a16 2845 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
80425e6c
JK
2846 goto error_return;
2847 if (bfd_write (reloc_buff,
2848 external_reloc_size, current->reloc_count, abfd)
966e0a16 2849 != external_reloc_size * current->reloc_count)
80425e6c
JK
2850 goto error_return;
2851 bfd_release (abfd, reloc_buff);
2852 reloc_buff = NULL;
dae31cf5 2853 }
dae31cf5 2854
3f048f7f
ILT
2855 /* Write out the symbolic debugging information. */
2856 if (bfd_get_symcount (abfd) > 0)
2857 {
2858 /* Write out the debugging information. */
2859 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2860 ecoff_data (abfd)->sym_filepos)
2861 == false)
80425e6c 2862 goto error_return;
3f048f7f 2863 }
626f883f 2864 }
dae31cf5 2865
626f883f
ILT
2866 /* The .bss section of a demand paged executable must receive an
2867 entire page. If there are symbols, the symbols will start on the
2868 next page. If there are no symbols, we must fill out the page by
2869 hand. */
2870 if (bfd_get_symcount (abfd) == 0
2871 && (abfd->flags & EXEC_P) != 0
2872 && (abfd->flags & D_PAGED) != 0)
2873 {
2874 char c;
2875
2876 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2877 SEEK_SET) != 0)
80425e6c 2878 goto error_return;
626f883f
ILT
2879 if (bfd_read (&c, 1, 1, abfd) == 0)
2880 c = 0;
2881 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2882 SEEK_SET) != 0)
80425e6c 2883 goto error_return;
626f883f 2884 if (bfd_write (&c, 1, 1, abfd) != 1)
80425e6c 2885 goto error_return;
dae31cf5
ILT
2886 }
2887
80425e6c
JK
2888 if (reloc_buff != NULL)
2889 bfd_release (abfd, reloc_buff);
2890 if (buff != NULL)
2891 free (buff);
dae31cf5 2892 return true;
80425e6c
JK
2893 error_return:
2894 if (reloc_buff != NULL)
2895 bfd_release (abfd, reloc_buff);
2896 if (buff != NULL)
2897 free (buff);
2898 return false;
dae31cf5
ILT
2899}
2900\f
2901/* Archive handling. ECOFF uses what appears to be a unique type of
b59f0276
ILT
2902 archive header (armap). The byte ordering of the armap and the
2903 contents are encoded in the name of the armap itself. At least for
2904 now, we only support archives with the same byte ordering in the
2905 armap and the contents.
dae31cf5
ILT
2906
2907 The first four bytes in the armap are the number of symbol
2908 definitions. This is always a power of two.
2909
2910 This is followed by the symbol definitions. Each symbol definition
2911 occupies 8 bytes. The first four bytes are the offset from the
2912 start of the armap strings to the null-terminated string naming
2913 this symbol. The second four bytes are the file offset to the
2914 archive member which defines this symbol. If the second four bytes
2915 are 0, then this is not actually a symbol definition, and it should
2916 be ignored.
2917
2918 The symbols are hashed into the armap with a closed hashing scheme.
2919 See the functions below for the details of the algorithm.
2920
dae31cf5
ILT
2921 After the symbol definitions comes four bytes holding the size of
2922 the string table, followed by the string table itself. */
2923
2924/* The name of an archive headers looks like this:
2925 __________E[BL]E[BL]_ (with a trailing space).
2926 The trailing space is changed to an X if the archive is changed to
48edba81
ILT
2927 indicate that the armap is out of date.
2928
2929 The Alpha seems to use ________64E[BL]E[BL]_. */
dae31cf5
ILT
2930
2931#define ARMAP_BIG_ENDIAN 'B'
2932#define ARMAP_LITTLE_ENDIAN 'L'
2933#define ARMAP_MARKER 'E'
48edba81 2934#define ARMAP_START_LENGTH 10
dae31cf5
ILT
2935#define ARMAP_HEADER_MARKER_INDEX 10
2936#define ARMAP_HEADER_ENDIAN_INDEX 11
2937#define ARMAP_OBJECT_MARKER_INDEX 12
2938#define ARMAP_OBJECT_ENDIAN_INDEX 13
2939#define ARMAP_END_INDEX 14
2940#define ARMAP_END "_ "
2941
2942/* This is a magic number used in the hashing algorithm. */
2943#define ARMAP_HASH_MAGIC 0x9dd68ab5
2944
2945/* This returns the hash value to use for a string. It also sets
2946 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2947 is the number of entries in the hash table, and HLOG is the log
2948 base 2 of SIZE. */
2949
2950static unsigned int
2951ecoff_armap_hash (s, rehash, size, hlog)
2952 CONST char *s;
2953 unsigned int *rehash;
2954 unsigned int size;
2955 unsigned int hlog;
2956{
2957 unsigned int hash;
2958
2959 hash = *s++;
2960 while (*s != '\0')
2961 hash = ((hash >> 27) | (hash << 5)) + *s++;
2962 hash *= ARMAP_HASH_MAGIC;
2963 *rehash = (hash & (size - 1)) | 1;
2964 return hash >> (32 - hlog);
2965}
2966
2967/* Read in the armap. */
2968
2969boolean
2970ecoff_slurp_armap (abfd)
2971 bfd *abfd;
2972{
2973 char nextname[17];
2974 unsigned int i;
2975 struct areltdata *mapdata;
2976 bfd_size_type parsed_size;
2977 char *raw_armap;
2978 struct artdata *ardata;
2979 unsigned int count;
2980 char *raw_ptr;
2981 struct symdef *symdef_ptr;
2982 char *stringbase;
2983
2984 /* Get the name of the first element. */
2985 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2986 if (i == 0)
2987 return true;
2988 if (i != 16)
2989 return false;
2990
2991 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
2992
8c11363a
ILT
2993 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2994 standard COFF armap. We could move the ECOFF armap stuff into
2995 bfd_slurp_armap, but that seems inappropriate since no other
2996 target uses this format. Instead, we check directly for a COFF
2997 armap. */
2998 if (strncmp (nextname, "/ ", 16) == 0)
2999 return bfd_slurp_armap (abfd);
3000
dae31cf5 3001 /* See if the first element is an armap. */
48edba81
ILT
3002 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
3003 ARMAP_START_LENGTH) != 0
dae31cf5
ILT
3004 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3005 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3006 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3007 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3008 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3009 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3010 || strncmp (nextname + ARMAP_END_INDEX,
3011 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3012 {
3013 bfd_has_map (abfd) = false;
3014 return true;
3015 }
3016
3017 /* Make sure we have the right byte ordering. */
3018 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3019 ^ (abfd->xvec->header_byteorder_big_p != false))
3020 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3021 ^ (abfd->xvec->byteorder_big_p != false)))
3022 {
d1ad85a6 3023 bfd_set_error (bfd_error_wrong_format);
dae31cf5
ILT
3024 return false;
3025 }
3026
3027 /* Read in the armap. */
3028 ardata = bfd_ardata (abfd);
b59f0276 3029 mapdata = _bfd_snarf_ar_hdr (abfd);
dae31cf5
ILT
3030 if (mapdata == (struct areltdata *) NULL)
3031 return false;
3032 parsed_size = mapdata->parsed_size;
3033 bfd_release (abfd, (PTR) mapdata);
3034
3035 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3036 if (raw_armap == (char *) NULL)
3037 {
d1ad85a6 3038 bfd_set_error (bfd_error_no_memory);
dae31cf5
ILT
3039 return false;
3040 }
3041
3042 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3043 {
d1ad85a6 3044 bfd_set_error (bfd_error_malformed_archive);
dae31cf5
ILT
3045 bfd_release (abfd, (PTR) raw_armap);
3046 return false;
3047 }
3048
b59f0276
ILT
3049 ardata->tdata = (PTR) raw_armap;
3050
dae31cf5
ILT
3051 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3052
3053 ardata->symdef_count = 0;
3054 ardata->cache = (struct ar_cache *) NULL;
3055
48edba81
ILT
3056 /* This code used to overlay the symdefs over the raw archive data,
3057 but that doesn't work on a 64 bit host. */
3058
e544ed4f 3059 stringbase = raw_armap + count * 8 + 8;
dae31cf5
ILT
3060
3061#ifdef CHECK_ARMAP_HASH
3062 {
3063 unsigned int hlog;
3064
3065 /* Double check that I have the hashing algorithm right by making
3066 sure that every symbol can be looked up successfully. */
3067 hlog = 0;
3068 for (i = 1; i < count; i <<= 1)
3069 hlog++;
3070 BFD_ASSERT (i == count);
3071
e544ed4f
ILT
3072 raw_ptr = raw_armap + 4;
3073 for (i = 0; i < count; i++, raw_ptr += 8)
dae31cf5
ILT
3074 {
3075 unsigned int name_offset, file_offset;
3076 unsigned int hash, rehash, srch;
3077
3078 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
e544ed4f 3079 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
dae31cf5
ILT
3080 if (file_offset == 0)
3081 continue;
3082 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3083 hlog);
3084 if (hash == i)
3085 continue;
3086
3087 /* See if we can rehash to this location. */
3088 for (srch = (hash + rehash) & (count - 1);
3089 srch != hash && srch != i;
3090 srch = (srch + rehash) & (count - 1))
e544ed4f 3091 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
dae31cf5
ILT
3092 != 0);
3093 BFD_ASSERT (srch == i);
3094 }
3095 }
3096
dae31cf5
ILT
3097#endif /* CHECK_ARMAP_HASH */
3098
e544ed4f
ILT
3099 raw_ptr = raw_armap + 4;
3100 for (i = 0; i < count; i++, raw_ptr += 8)
3101 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
48edba81
ILT
3102 ++ardata->symdef_count;
3103
3104 symdef_ptr = ((struct symdef *)
3105 bfd_alloc (abfd,
3106 ardata->symdef_count * sizeof (struct symdef)));
9783e04a
DM
3107 if (!symdef_ptr)
3108 {
d1ad85a6 3109 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3110 return false;
3111 }
3112
48edba81
ILT
3113 ardata->symdefs = (carsym *) symdef_ptr;
3114
e544ed4f
ILT
3115 raw_ptr = raw_armap + 4;
3116 for (i = 0; i < count; i++, raw_ptr += 8)
dae31cf5
ILT
3117 {
3118 unsigned int name_offset, file_offset;
3119
e544ed4f 3120 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
dae31cf5
ILT
3121 if (file_offset == 0)
3122 continue;
48edba81 3123 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
dae31cf5
ILT
3124 symdef_ptr->s.name = stringbase + name_offset;
3125 symdef_ptr->file_offset = file_offset;
3126 ++symdef_ptr;
dae31cf5
ILT
3127 }
3128
3129 ardata->first_file_filepos = bfd_tell (abfd);
3130 /* Pad to an even boundary. */
3131 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3132
3133 bfd_has_map (abfd) = true;
3134
3135 return true;
3136}
3137
3138/* Write out an armap. */
3139
3140boolean
3141ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3142 bfd *abfd;
3143 unsigned int elength;
3144 struct orl *map;
3145 unsigned int orl_count;
3146 int stridx;
3147{
3148 unsigned int hashsize, hashlog;
3149 unsigned int symdefsize;
3150 int padit;
3151 unsigned int stringsize;
3152 unsigned int mapsize;
3153 file_ptr firstreal;
3154 struct ar_hdr hdr;
3155 struct stat statbuf;
3156 unsigned int i;
e544ed4f 3157 bfd_byte temp[4];
dae31cf5
ILT
3158 bfd_byte *hashtable;
3159 bfd *current;
3160 bfd *last_elt;
3161
3162 /* Ultrix appears to use as a hash table size the least power of two
3163 greater than twice the number of entries. */
3164 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3165 ;
3166 hashsize = 1 << hashlog;
3167
e544ed4f 3168 symdefsize = hashsize * 8;
dae31cf5
ILT
3169 padit = stridx % 2;
3170 stringsize = stridx + padit;
3171
3172 /* Include 8 bytes to store symdefsize and stringsize in output. */
e544ed4f 3173 mapsize = symdefsize + stringsize + 8;
dae31cf5
ILT
3174
3175 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3176
3177 memset ((PTR) &hdr, 0, sizeof hdr);
3178
3179 /* Work out the ECOFF armap name. */
48edba81 3180 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
dae31cf5
ILT
3181 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3182 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3183 (abfd->xvec->header_byteorder_big_p
3184 ? ARMAP_BIG_ENDIAN
3185 : ARMAP_LITTLE_ENDIAN);
3186 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3187 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3188 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3189 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3190
3191 /* Write the timestamp of the archive header to be just a little bit
3192 later than the timestamp of the file, otherwise the linker will
3193 complain that the index is out of date. Actually, the Ultrix
3194 linker just checks the archive name; the GNU linker may check the
3195 date. */
3196 stat (abfd->filename, &statbuf);
3197 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3198
3199 /* The DECstation uses zeroes for the uid, gid and mode of the
3200 armap. */
3201 hdr.ar_uid[0] = '0';
3202 hdr.ar_gid[0] = '0';
3203 hdr.ar_mode[0] = '0';
3204
3205 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3206
3207 hdr.ar_fmag[0] = '`';
9a793780 3208 hdr.ar_fmag[1] = '\012';
dae31cf5
ILT
3209
3210 /* Turn all null bytes in the header into spaces. */
3211 for (i = 0; i < sizeof (struct ar_hdr); i++)
3212 if (((char *)(&hdr))[i] == '\0')
3213 (((char *)(&hdr))[i]) = ' ';
3214
3215 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3216 != sizeof (struct ar_hdr))
3217 return false;
3218
4c3721d5 3219 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
728472f1 3220 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
dae31cf5
ILT
3221 return false;
3222
3223 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
9783e04a
DM
3224 if (!hashtable)
3225 {
d1ad85a6 3226 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3227 return false;
3228 }
dae31cf5
ILT
3229
3230 current = abfd->archive_head;
3231 last_elt = current;
3232 for (i = 0; i < orl_count; i++)
3233 {
3234 unsigned int hash, rehash;
3235
3236 /* Advance firstreal to the file position of this archive
3237 element. */
3238 if (((bfd *) map[i].pos) != last_elt)
3239 {
3240 do
3241 {
3242 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3243 firstreal += firstreal % 2;
3244 current = current->next;
3245 }
3246 while (current != (bfd *) map[i].pos);
3247 }
3248
3249 last_elt = current;
3250
3251 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
e544ed4f 3252 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
dae31cf5
ILT
3253 {
3254 unsigned int srch;
3255
3256 /* The desired slot is already taken. */
3257 for (srch = (hash + rehash) & (hashsize - 1);
3258 srch != hash;
3259 srch = (srch + rehash) & (hashsize - 1))
e544ed4f 3260 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
dae31cf5
ILT
3261 break;
3262
3263 BFD_ASSERT (srch != hash);
3264
3265 hash = srch;
3266 }
3267
4c3721d5
ILT
3268 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3269 (PTR) (hashtable + hash * 8));
3270 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3271 (PTR) (hashtable + hash * 8 + 4));
dae31cf5
ILT
3272 }
3273
728472f1 3274 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
dae31cf5
ILT
3275 return false;
3276
3277 bfd_release (abfd, hashtable);
3278
3279 /* Now write the strings. */
4c3721d5 3280 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
728472f1 3281 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
dae31cf5
ILT
3282 return false;
3283 for (i = 0; i < orl_count; i++)
3284 {
3285 bfd_size_type len;
3286
3287 len = strlen (*map[i].name) + 1;
3288 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3289 return false;
3290 }
3291
3292 /* The spec sez this should be a newline. But in order to be
3293 bug-compatible for DECstation ar we use a null. */
3294 if (padit)
3295 {
728472f1 3296 if (bfd_write ("", 1, 1, abfd) != 1)
dae31cf5
ILT
3297 return false;
3298 }
3299
3300 return true;
3301}
3302
3303/* See whether this BFD is an archive. If it is, read in the armap
3304 and the extended name table. */
3305
3306bfd_target *
3307ecoff_archive_p (abfd)
3308 bfd *abfd;
3309{
3310 char armag[SARMAG + 1];
3311
3312 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3313 || strncmp (armag, ARMAG, SARMAG) != 0)
3314 {
d1ad85a6 3315 bfd_set_error (bfd_error_wrong_format);
dae31cf5
ILT
3316 return (bfd_target *) NULL;
3317 }
3318
3319 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3320 involves a cast, we can't do it as the left operand of
3321 assignment. */
3322 abfd->tdata.aout_ar_data =
3323 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3324
3325 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3326 {
d1ad85a6 3327 bfd_set_error (bfd_error_no_memory);
dae31cf5
ILT
3328 return (bfd_target *) NULL;
3329 }
3330
3331 bfd_ardata (abfd)->first_file_filepos = SARMAG;
b59f0276
ILT
3332 bfd_ardata (abfd)->cache = NULL;
3333 bfd_ardata (abfd)->archive_head = NULL;
3334 bfd_ardata (abfd)->symdefs = NULL;
3335 bfd_ardata (abfd)->extended_names = NULL;
3336 bfd_ardata (abfd)->tdata = NULL;
dae31cf5
ILT
3337
3338 if (ecoff_slurp_armap (abfd) == false
3339 || ecoff_slurp_extended_name_table (abfd) == false)
3340 {
3341 bfd_release (abfd, bfd_ardata (abfd));
3342 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3343 return (bfd_target *) NULL;
3344 }
3345
3346 return abfd->xvec;
3347}
966e0a16
ILT
3348\f
3349/* ECOFF linker code. */
3350
3351static struct bfd_hash_entry *ecoff_link_hash_newfunc
3352 PARAMS ((struct bfd_hash_entry *entry,
3353 struct bfd_hash_table *table,
3354 const char *string));
3355static boolean ecoff_link_add_archive_symbols
3356 PARAMS ((bfd *, struct bfd_link_info *));
3357static boolean ecoff_link_check_archive_element
3358 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3359static boolean ecoff_link_add_object_symbols
3360 PARAMS ((bfd *, struct bfd_link_info *));
3361static boolean ecoff_link_add_externals
3362 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3363
3364/* Routine to create an entry in an ECOFF link hash table. */
3365
3366static struct bfd_hash_entry *
3367ecoff_link_hash_newfunc (entry, table, string)
3368 struct bfd_hash_entry *entry;
3369 struct bfd_hash_table *table;
3370 const char *string;
3371{
3372 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3373
3374 /* Allocate the structure if it has not already been allocated by a
3375 subclass. */
3376 if (ret == (struct ecoff_link_hash_entry *) NULL)
3377 ret = ((struct ecoff_link_hash_entry *)
3378 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
9783e04a
DM
3379 if (ret == (struct ecoff_link_hash_entry *) NULL)
3380 {
d1ad85a6 3381 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3382 return NULL;
3383 }
966e0a16
ILT
3384
3385 /* Call the allocation method of the superclass. */
3386 ret = ((struct ecoff_link_hash_entry *)
3387 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3388 table, string));
3389
9783e04a
DM
3390 if (ret)
3391 {
3392 /* Set local fields. */
3393 ret->indx = -1;
3394 ret->abfd = NULL;
3395 }
68241b2b 3396 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
966e0a16
ILT
3397
3398 return (struct bfd_hash_entry *) ret;
3399}
3400
3401/* Create an ECOFF link hash table. */
3402
3403struct bfd_link_hash_table *
3404ecoff_bfd_link_hash_table_create (abfd)
3405 bfd *abfd;
3406{
3407 struct ecoff_link_hash_table *ret;
3408
3409 ret = ((struct ecoff_link_hash_table *)
9a793780
SS
3410 malloc (sizeof (struct ecoff_link_hash_table)));
3411 if (!ret)
3412 {
d1ad85a6 3413 bfd_set_error (bfd_error_no_memory);
9a793780
SS
3414 return NULL;
3415 }
966e0a16
ILT
3416 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3417 ecoff_link_hash_newfunc))
3418 {
3419 free (ret);
3420 return (struct bfd_link_hash_table *) NULL;
3421 }
3422 return &ret->root;
3423}
3424
3425/* Look up an entry in an ECOFF link hash table. */
3426
3427#define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3428 ((struct ecoff_link_hash_entry *) \
3429 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3430
3431/* Traverse an ECOFF link hash table. */
3432
3433#define ecoff_link_hash_traverse(table, func, info) \
3434 (bfd_link_hash_traverse \
3435 (&(table)->root, \
3436 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3437 (info)))
3438
3439/* Get the ECOFF link hash table from the info structure. This is
3440 just a cast. */
3441
3442#define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3443
3444/* Given an ECOFF BFD, add symbols to the global hash table as
3445 appropriate. */
3446
3447boolean
3448ecoff_bfd_link_add_symbols (abfd, info)
3449 bfd *abfd;
3450 struct bfd_link_info *info;
3451{
3452 switch (bfd_get_format (abfd))
3453 {
3454 case bfd_object:
3455 return ecoff_link_add_object_symbols (abfd, info);
3456 case bfd_archive:
3457 return ecoff_link_add_archive_symbols (abfd, info);
3458 default:
d1ad85a6 3459 bfd_set_error (bfd_error_wrong_format);
966e0a16
ILT
3460 return false;
3461 }
3462}
3463
3464/* Add the symbols from an archive file to the global hash table.
3465 This looks through the undefined symbols, looks each one up in the
3466 archive hash table, and adds any associated object file. We do not
3467 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3468 already have a hash table, so there is no reason to construct
3469 another one. */
3470
3471static boolean
3472ecoff_link_add_archive_symbols (abfd, info)
3473 bfd *abfd;
3474 struct bfd_link_info *info;
3475{
3476 const bfd_byte *raw_armap;
3477 struct bfd_link_hash_entry **pundef;
3478 unsigned int armap_count;
3479 unsigned int armap_log;
3480 unsigned int i;
3481 const bfd_byte *hashtable;
3482 const char *stringbase;
3483
3484 if (! bfd_has_map (abfd))
3485 {
d1ad85a6 3486 bfd_set_error (bfd_error_no_symbols);
966e0a16
ILT
3487 return false;
3488 }
3489
3490 /* If we don't have any raw data for this archive, as can happen on
3491 Irix 4.0.5F, we call the generic routine.
3492 FIXME: We should be more clever about this, since someday tdata
3493 may get to something for a generic archive. */
3494 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3495 if (raw_armap == (bfd_byte *) NULL)
3496 return (_bfd_generic_link_add_archive_symbols
3497 (abfd, info, ecoff_link_check_archive_element));
3498
3499 armap_count = bfd_h_get_32 (abfd, raw_armap);
3500
3501 armap_log = 0;
3502 for (i = 1; i < armap_count; i <<= 1)
3503 armap_log++;
3504 BFD_ASSERT (i == armap_count);
3505
3506 hashtable = raw_armap + 4;
3507 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3508
3509 /* Look through the list of undefined symbols. */
3510 pundef = &info->hash->undefs;
3511 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3512 {
3513 struct bfd_link_hash_entry *h;
3514 unsigned int hash, rehash;
3515 unsigned int file_offset;
3516 const char *name;
3517 bfd *element;
3518
3519 h = *pundef;
3520
3521 /* When a symbol is defined, it is not necessarily removed from
3522 the list. */
3523 if (h->type != bfd_link_hash_undefined
3524 && h->type != bfd_link_hash_common)
3525 {
3526 /* Remove this entry from the list, for general cleanliness
3527 and because we are going to look through the list again
3528 if we search any more libraries. We can't remove the
3529 entry if it is the tail, because that would lose any
3530 entries we add to the list later on. */
3531 if (*pundef != info->hash->undefs_tail)
3532 *pundef = (*pundef)->next;
3533 else
3534 pundef = &(*pundef)->next;
3535 continue;
3536 }
3537
3538 /* Native ECOFF linkers do not pull in archive elements merely
3539 to satisfy common definitions, so neither do we. We leave
3540 them on the list, though, in case we are linking against some
3541 other object format. */
3542 if (h->type != bfd_link_hash_undefined)
3543 {
3544 pundef = &(*pundef)->next;
3545 continue;
3546 }
3547
3548 /* Look for this symbol in the archive hash table. */
3549 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3550 armap_log);
3551
3552 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3553 if (file_offset == 0)
3554 {
3555 /* Nothing in this slot. */
3556 pundef = &(*pundef)->next;
3557 continue;
3558 }
3559
3560 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3561 if (name[0] != h->root.string[0]
3562 || strcmp (name, h->root.string) != 0)
3563 {
3564 unsigned int srch;
3565 boolean found;
3566
3567 /* That was the wrong symbol. Try rehashing. */
3568 found = false;
3569 for (srch = (hash + rehash) & (armap_count - 1);
3570 srch != hash;
3571 srch = (srch + rehash) & (armap_count - 1))
3572 {
3573 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3574 if (file_offset == 0)
3575 break;
3576 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3577 if (name[0] == h->root.string[0]
3578 && strcmp (name, h->root.string) == 0)
3579 {
3580 found = true;
3581 break;
3582 }
3583 }
3584
3585 if (! found)
3586 {
3587 pundef = &(*pundef)->next;
3588 continue;
3589 }
3590
3591 hash = srch;
3592 }
3593
3594 element = _bfd_get_elt_at_filepos (abfd, file_offset);
3595 if (element == (bfd *) NULL)
3596 return false;
3597
3598 if (! bfd_check_format (element, bfd_object))
3599 return false;
3600
3601 /* Unlike the generic linker, we know that this element provides
3602 a definition for an undefined symbol and we know that we want
3603 to include it. We don't need to check anything. */
3604 if (! (*info->callbacks->add_archive_element) (info, element, name))
3605 return false;
3606 if (! ecoff_link_add_object_symbols (element, info))
3607 return false;
3608
3609 pundef = &(*pundef)->next;
3610 }
3611
3612 return true;
3613}
3614
3615/* This is called if we used _bfd_generic_link_add_archive_symbols
3616 because we were not dealing with an ECOFF archive. */
3617
3618static boolean
3619ecoff_link_check_archive_element (abfd, info, pneeded)
3620 bfd *abfd;
3621 struct bfd_link_info *info;
3622 boolean *pneeded;
3623{
3624 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3625 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3626 = backend->debug_swap.swap_ext_in;
3627 HDRR *symhdr;
3628 bfd_size_type external_ext_size;
80425e6c 3629 PTR external_ext = NULL;
966e0a16 3630 size_t esize;
80425e6c 3631 char *ssext = NULL;
966e0a16
ILT
3632 char *ext_ptr;
3633 char *ext_end;
3634
3635 *pneeded = false;
3636
3637 if (! ecoff_slurp_symbolic_header (abfd))
80425e6c 3638 goto error_return;
966e0a16
ILT
3639
3640 /* If there are no symbols, we don't want it. */
3641 if (bfd_get_symcount (abfd) == 0)
80425e6c 3642 goto successful_return;
966e0a16
ILT
3643
3644 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3645
3646 /* Read in the external symbols and external strings. */
3647 external_ext_size = backend->debug_swap.external_ext_size;
3648 esize = symhdr->iextMax * external_ext_size;
80425e6c 3649 external_ext = (PTR) malloc (esize);
a3a33af3 3650 if (external_ext == NULL && esize != 0)
80425e6c
JK
3651 {
3652 bfd_set_error (bfd_error_no_memory);
3653 goto error_return;
3654 }
3655
966e0a16
ILT
3656 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3657 || bfd_read (external_ext, 1, esize, abfd) != esize)
80425e6c
JK
3658 goto error_return;
3659
3660 ssext = (char *) malloc (symhdr->issExtMax);
a3a33af3 3661 if (ssext == NULL && symhdr->issExtMax != 0)
80425e6c
JK
3662 {
3663 bfd_set_error (bfd_error_no_memory);
3664 goto error_return;
3665 }
966e0a16 3666
966e0a16
ILT
3667 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3668 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
80425e6c 3669 goto error_return;
966e0a16
ILT
3670
3671 /* Look through the external symbols to see if they define some
3672 symbol that is currently undefined. */
3673 ext_ptr = (char *) external_ext;
3674 ext_end = ext_ptr + esize;
3675 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3676 {
3677 EXTR esym;
3678 boolean def;
3679 const char *name;
3680 struct bfd_link_hash_entry *h;
3681
3682 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3683
3684 /* See if this symbol defines something. */
3685 if (esym.asym.st != stGlobal
3686 && esym.asym.st != stLabel
3687 && esym.asym.st != stProc)
3688 continue;
3689
3690 switch (esym.asym.sc)
3691 {
3692 case scText:
3693 case scData:
3694 case scBss:
3695 case scAbs:
3696 case scSData:
3697 case scSBss:
3698 case scRData:
3699 case scCommon:
3700 case scSCommon:
3701 case scInit:
3702 case scFini:
3703 def = true;
3704 break;
3705 default:
3706 def = false;
3707 break;
3708 }
3709
3710 if (! def)
3711 continue;
3712
3713 name = ssext + esym.asym.iss;
3714 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3715
3716 /* Unlike the generic linker, we do not pull in elements because
3717 of common symbols. */
3718 if (h == (struct bfd_link_hash_entry *) NULL
3719 || h->type != bfd_link_hash_undefined)
3720 continue;
3721
3722 /* Include this element. */
3723 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
80425e6c 3724 goto error_return;
966e0a16 3725 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
80425e6c 3726 goto error_return;
966e0a16
ILT
3727
3728 *pneeded = true;
80425e6c 3729 goto successful_return;
966e0a16
ILT
3730 }
3731
80425e6c
JK
3732 successful_return:
3733 if (external_ext != NULL)
3734 free (external_ext);
3735 if (ssext != NULL)
3736 free (ssext);
966e0a16 3737 return true;
80425e6c
JK
3738 error_return:
3739 if (external_ext != NULL)
3740 free (external_ext);
3741 if (ssext != NULL)
3742 free (ssext);
3743 return false;
966e0a16
ILT
3744}
3745
3746/* Add symbols from an ECOFF object file to the global linker hash
3747 table. */
3748
3749static boolean
3750ecoff_link_add_object_symbols (abfd, info)
3751 bfd *abfd;
3752 struct bfd_link_info *info;
3753{
3754 HDRR *symhdr;
3755 bfd_size_type external_ext_size;
80425e6c 3756 PTR external_ext = NULL;
966e0a16 3757 size_t esize;
80425e6c
JK
3758 char *ssext = NULL;
3759 boolean result;
966e0a16
ILT
3760
3761 if (! ecoff_slurp_symbolic_header (abfd))
3762 return false;
3763
3764 /* If there are no symbols, we don't want it. */
3765 if (bfd_get_symcount (abfd) == 0)
3766 return true;
3767
3768 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3769
3770 /* Read in the external symbols and external strings. */
3771 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3772 esize = symhdr->iextMax * external_ext_size;
80425e6c 3773 external_ext = (PTR) malloc (esize);
a3a33af3 3774 if (external_ext == NULL && esize != 0)
80425e6c
JK
3775 {
3776 bfd_set_error (bfd_error_no_memory);
3777 goto error_return;
3778 }
3779
966e0a16
ILT
3780 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3781 || bfd_read (external_ext, 1, esize, abfd) != esize)
80425e6c
JK
3782 goto error_return;
3783
3784 ssext = (char *) malloc (symhdr->issExtMax);
a3a33af3 3785 if (ssext == NULL && symhdr->issExtMax != 0)
80425e6c
JK
3786 {
3787 bfd_set_error (bfd_error_no_memory);
3788 goto error_return;
3789 }
966e0a16 3790
966e0a16
ILT
3791 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3792 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
80425e6c
JK
3793 goto error_return;
3794
3795 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3796
3797 if (ssext != NULL)
3798 free (ssext);
3799 if (external_ext != NULL)
3800 free (external_ext);
3801 return result;
3802
3803 error_return:
3804 if (ssext != NULL)
3805 free (ssext);
3806 if (external_ext != NULL)
3807 free (external_ext);
3808 return false;
966e0a16
ILT
3809}
3810
3811/* Add the external symbols of an object file to the global linker
3812 hash table. The external symbols and strings we are passed are
3813 just allocated on the stack, and will be discarded. We must
3814 explicitly save any information we may need later on in the link.
3815 We do not want to read the external symbol information again. */
3816
3817static boolean
3818ecoff_link_add_externals (abfd, info, external_ext, ssext)
3819 bfd *abfd;
3820 struct bfd_link_info *info;
3821 PTR external_ext;
3822 char *ssext;
3823{
3824 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3825 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3826 = backend->debug_swap.swap_ext_in;
3827 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3828 unsigned long ext_count;
3829 struct ecoff_link_hash_entry **sym_hash;
3830 char *ext_ptr;
3831 char *ext_end;
3832
3833 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3834
3835 sym_hash = ((struct ecoff_link_hash_entry **)
3836 bfd_alloc (abfd,
3837 ext_count * sizeof (struct bfd_link_hash_entry *)));
9783e04a
DM
3838 if (!sym_hash)
3839 {
d1ad85a6 3840 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3841 return false;
3842 }
966e0a16
ILT
3843 ecoff_data (abfd)->sym_hashes = sym_hash;
3844
3845 ext_ptr = (char *) external_ext;
3846 ext_end = ext_ptr + ext_count * external_ext_size;
3847 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3848 {
3849 EXTR esym;
3850 boolean skip;
3851 bfd_vma value;
3852 asection *section;
3853 const char *name;
3854 struct ecoff_link_hash_entry *h;
3855
3856 *sym_hash = NULL;
3857
3858 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3859
3860 /* Skip debugging symbols. */
3861 skip = false;
3862 switch (esym.asym.st)
3863 {
3864 case stGlobal:
3865 case stStatic:
3866 case stLabel:
3867 case stProc:
3868 case stStaticProc:
3869 break;
3870 default:
3871 skip = true;
3872 break;
3873 }
3874
3875 if (skip)
3876 continue;
3877
3878 /* Get the information for this symbol. */
3879 value = esym.asym.value;
3880 switch (esym.asym.sc)
3881 {
3882 default:
3883 case scNil:
3884 case scRegister:
3885 case scCdbLocal:
3886 case scBits:
3887 case scCdbSystem:
3888 case scRegImage:
3889 case scInfo:
3890 case scUserStruct:
3891 case scVar:
3892 case scVarRegister:
3893 case scVariant:
3894 case scBasedVar:
3895 case scXData:
3896 case scPData:
3897 section = NULL;
3898 break;
3899 case scText:
3900 section = bfd_make_section_old_way (abfd, ".text");
3901 value -= section->vma;
3902 break;
3903 case scData:
3904 section = bfd_make_section_old_way (abfd, ".data");
3905 value -= section->vma;
3906 break;
3907 case scBss:
3908 section = bfd_make_section_old_way (abfd, ".bss");
3909 value -= section->vma;
3910 break;
3911 case scAbs:
3912 section = &bfd_abs_section;
3913 break;
3914 case scUndefined:
3915 section = &bfd_und_section;
3916 break;
3917 case scSData:
3918 section = bfd_make_section_old_way (abfd, ".sdata");
3919 value -= section->vma;
3920 break;
3921 case scSBss:
3922 section = bfd_make_section_old_way (abfd, ".sbss");
3923 value -= section->vma;
3924 break;
3925 case scRData:
3926 section = bfd_make_section_old_way (abfd, ".rdata");
3927 value -= section->vma;
3928 break;
3929 case scCommon:
3930 if (value > ecoff_data (abfd)->gp_size)
3931 {
3932 section = &bfd_com_section;
3933 break;
3934 }
3935 /* Fall through. */
3936 case scSCommon:
3937 if (ecoff_scom_section.name == NULL)
3938 {
3939 /* Initialize the small common section. */
3940 ecoff_scom_section.name = SCOMMON;
3941 ecoff_scom_section.flags = SEC_IS_COMMON;
3942 ecoff_scom_section.output_section = &ecoff_scom_section;
3943 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3944 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3945 ecoff_scom_symbol.name = SCOMMON;
3946 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3947 ecoff_scom_symbol.section = &ecoff_scom_section;
3948 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3949 }
3950 section = &ecoff_scom_section;
3951 break;
3952 case scSUndefined:
3953 section = &bfd_und_section;
3954 break;
3955 case scInit:
3956 section = bfd_make_section_old_way (abfd, ".init");
3957 value -= section->vma;
3958 break;
3959 case scFini:
3960 section = bfd_make_section_old_way (abfd, ".fini");
3961 value -= section->vma;
3962 break;
3963 }
3964
3965 if (section == (asection *) NULL)
3966 continue;
3967
3968 name = ssext + esym.asym.iss;
3969
3970 if (! (_bfd_generic_link_add_one_symbol
3971 (info, abfd, name, BSF_GLOBAL, section, value,
a3a33af3 3972 (const char *) NULL, true, true,
966e0a16
ILT
3973 (struct bfd_link_hash_entry **) &h)))
3974 return false;
3975
3976 *sym_hash = h;
3977
3978 /* If we are building an ECOFF hash table, save the external
3979 symbol information. */
3980 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3981 {
3982 if (h->abfd == (bfd *) NULL
3983 || (section != &bfd_und_section
3984 && (! bfd_is_com_section (section)
3985 || h->root.type != bfd_link_hash_defined)))
3986 {
3987 h->abfd = abfd;
3988 h->esym = esym;
3989 }
3990 }
3991 }
3992
3993 return true;
3994}
3995\f
3996/* ECOFF final link routines. */
3997
3998static boolean ecoff_final_link_debug_accumulate
3f048f7f
ILT
3999 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4000 PTR handle));
966e0a16
ILT
4001static boolean ecoff_link_write_external
4002 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4003static boolean ecoff_indirect_link_order
4004 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4005 struct bfd_link_order *));
a3a33af3
ILT
4006static boolean ecoff_reloc_link_order
4007 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4008 struct bfd_link_order *));
966e0a16
ILT
4009
4010/* ECOFF final link routine. This looks through all the input BFDs
4011 and gathers together all the debugging information, and then
4012 processes all the link order information. This may cause it to
4013 close and reopen some input BFDs; I'll see how bad this is. */
4014
4015boolean
4016ecoff_bfd_final_link (abfd, info)
4017 bfd *abfd;
4018 struct bfd_link_info *info;
4019{
4020 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4021 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4022 HDRR *symhdr;
3f048f7f 4023 PTR handle;
966e0a16
ILT
4024 register bfd *input_bfd;
4025 asection *o;
4026 struct bfd_link_order *p;
4027
4028 /* We accumulate the debugging information counts in the symbolic
4029 header. */
4030 symhdr = &debug->symbolic_header;
966e0a16
ILT
4031 symhdr->vstamp = 0;
4032 symhdr->ilineMax = 0;
4033 symhdr->cbLine = 0;
4034 symhdr->idnMax = 0;
4035 symhdr->ipdMax = 0;
4036 symhdr->isymMax = 0;
4037 symhdr->ioptMax = 0;
4038 symhdr->iauxMax = 0;
4039 symhdr->issMax = 0;
4040 symhdr->issExtMax = 0;
4041 symhdr->ifdMax = 0;
4042 symhdr->crfd = 0;
4043 symhdr->iextMax = 0;
4044
4045 /* We accumulate the debugging information itself in the debug_info
4046 structure. */
3f048f7f
ILT
4047 debug->line = NULL;
4048 debug->external_dnr = NULL;
4049 debug->external_pdr = NULL;
4050 debug->external_sym = NULL;
4051 debug->external_opt = NULL;
4052 debug->external_aux = NULL;
4053 debug->ss = NULL;
966e0a16 4054 debug->ssext = debug->ssext_end = NULL;
3f048f7f
ILT
4055 debug->external_fdr = NULL;
4056 debug->external_rfd = NULL;
966e0a16
ILT
4057 debug->external_ext = debug->external_ext_end = NULL;
4058
3f048f7f
ILT
4059 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4060 if (handle == (PTR) NULL)
4061 return false;
4062
966e0a16
ILT
4063 /* Accumulate the debugging symbols from each input BFD. */
4064 for (input_bfd = info->input_bfds;
4065 input_bfd != (bfd *) NULL;
4066 input_bfd = input_bfd->link_next)
4067 {
4068 boolean ret;
4069
966e0a16 4070 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
3f048f7f
ILT
4071 {
4072 /* Abitrarily set the symbolic header vstamp to the vstamp
4073 of the first object file in the link. */
4074 if (symhdr->vstamp == 0)
4075 symhdr->vstamp
4076 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4077 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4078 handle);
4079 }
966e0a16 4080 else
3f048f7f
ILT
4081 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4082 debug, &backend->debug_swap,
4083 input_bfd, info);
966e0a16
ILT
4084 if (! ret)
4085 return false;
4086
4087 /* Combine the register masks. */
4088 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4089 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4090 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4091 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4092 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4093 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4094 }
4095
4096 /* Write out the external symbols. */
4097 ecoff_link_hash_traverse (ecoff_hash_table (info),
4098 ecoff_link_write_external,
4099 (PTR) abfd);
4100
4101 if (info->relocateable)
4102 {
4103 /* We need to make a pass over the link_orders to count up the
4104 number of relocations we will need to output, so that we know
4105 how much space they will take up. */
4106 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4107 {
4108 o->reloc_count = 0;
4109 for (p = o->link_order_head;
4110 p != (struct bfd_link_order *) NULL;
4111 p = p->next)
4112 if (p->type == bfd_indirect_link_order)
4113 o->reloc_count += p->u.indirect.section->reloc_count;
a3a33af3
ILT
4114 else if (p->type == bfd_section_reloc_link_order
4115 || p->type == bfd_symbol_reloc_link_order)
4116 ++o->reloc_count;
966e0a16 4117 }
3f048f7f
ILT
4118 }
4119
4120 /* Compute the reloc and symbol file positions. */
4121 ecoff_compute_reloc_file_positions (abfd);
4122
4123 /* Write out the debugging information. */
4124 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4125 &backend->debug_swap, info,
4126 ecoff_data (abfd)->sym_filepos))
4127 return false;
966e0a16 4128
3f048f7f 4129 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
966e0a16 4130
3f048f7f
ILT
4131 if (info->relocateable)
4132 {
966e0a16
ILT
4133 /* Now reset the reloc_count field of the sections in the output
4134 BFD to 0, so that we can use them to keep track of how many
4135 relocs we have output thus far. */
4136 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4137 o->reloc_count = 0;
4138 }
4139
4140 /* Get a value for the GP register. */
4141 if (ecoff_data (abfd)->gp == 0)
4142 {
4143 struct bfd_link_hash_entry *h;
4144
4145 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4146 if (h != (struct bfd_link_hash_entry *) NULL
4147 && h->type == bfd_link_hash_defined)
4148 ecoff_data (abfd)->gp = (h->u.def.value
4149 + h->u.def.section->output_section->vma
4150 + h->u.def.section->output_offset);
4151 else if (info->relocateable)
4152 {
4153 bfd_vma lo;
4154
4155 /* Make up a value. */
4156 lo = (bfd_vma) -1;
4157 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4158 {
4159 if (o->vma < lo
4160 && (strcmp (o->name, _SBSS) == 0
4161 || strcmp (o->name, _SDATA) == 0
4162 || strcmp (o->name, _LIT4) == 0
4163 || strcmp (o->name, _LIT8) == 0
4164 || strcmp (o->name, _LITA) == 0))
4165 lo = o->vma;
4166 }
4167 ecoff_data (abfd)->gp = lo + 0x8000;
4168 }
4169 else
4170 {
4171 /* If the relocate_section function needs to do a reloc
4172 involving the GP value, it should make a reloc_dangerous
4173 callback to warn that GP is not defined. */
4174 }
4175 }
4176
4177 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4178 {
4179 /* Ignore any link_orders for the .reginfo section, which does
4180 not really exist. */
4181 if (strcmp (o->name, REGINFO) == 0)
4182 continue;
4183
4184 for (p = o->link_order_head;
4185 p != (struct bfd_link_order *) NULL;
4186 p = p->next)
4187 {
966e0a16
ILT
4188 if (p->type == bfd_indirect_link_order
4189 && (bfd_get_flavour (p->u.indirect.section->owner)
4190 == bfd_target_ecoff_flavour))
4191 {
4192 if (! ecoff_indirect_link_order (abfd, info, o, p))
4193 return false;
4194 }
a3a33af3
ILT
4195 else if (p->type == bfd_section_reloc_link_order
4196 || p->type == bfd_symbol_reloc_link_order)
4197 {
4198 if (! ecoff_reloc_link_order (abfd, info, o, p))
4199 return false;
4200 }
966e0a16
ILT
4201 else
4202 {
4203 if (! _bfd_default_link_order (abfd, info, o, p))
4204 return false;
4205 }
4206 }
4207 }
4208
4209 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4210
4211 return true;
4212}
4213
4214/* Accumulate the debugging information for an input BFD into the
4215 output BFD. This must read in the symbolic information of the
4216 input BFD. */
4217
4218static boolean
3f048f7f 4219ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
966e0a16
ILT
4220 bfd *output_bfd;
4221 bfd *input_bfd;
4222 struct bfd_link_info *info;
3f048f7f 4223 PTR handle;
966e0a16
ILT
4224{
4225 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4226 const struct ecoff_debug_swap * const swap =
4227 &ecoff_backend (input_bfd)->debug_swap;
4228 HDRR *symhdr = &debug->symbolic_header;
4229 boolean ret;
4230
4231#define READ(ptr, offset, count, size, type) \
4232 if (symhdr->count == 0) \
4233 debug->ptr = NULL; \
4234 else \
4235 { \
80425e6c
JK
4236 debug->ptr = (type) malloc (size * symhdr->count); \
4237 if (debug->ptr == NULL) \
4238 { \
4239 bfd_set_error (bfd_error_no_memory); \
4240 ret = false; \
4241 goto return_something; \
4242 } \
966e0a16
ILT
4243 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4244 != 0) \
4245 || (bfd_read (debug->ptr, size, symhdr->count, \
4246 input_bfd) != size * symhdr->count)) \
80425e6c
JK
4247 { \
4248 ret = false; \
4249 goto return_something; \
4250 } \
966e0a16
ILT
4251 }
4252
a3a33af3
ILT
4253 /* If raw_syments is not NULL, then the data was already by read by
4254 ecoff_slurp_symbolic_info. */
4255 if (ecoff_data (input_bfd)->raw_syments == NULL)
4256 {
4257 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4258 unsigned char *);
4259 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4260 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4261 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4262 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4263 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4264 union aux_ext *);
4265 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4266 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4267 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4268 }
966e0a16
ILT
4269#undef READ
4270
4271 /* We do not read the external strings or the external symbols. */
4272
4273 ret = (bfd_ecoff_debug_accumulate
3f048f7f 4274 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
966e0a16 4275 &ecoff_backend (output_bfd)->debug_swap,
3f048f7f 4276 input_bfd, debug, swap, info));
966e0a16 4277
80425e6c 4278 return_something:
a3a33af3
ILT
4279 if (ecoff_data (input_bfd)->raw_syments == NULL)
4280 {
4281 if (debug->line != NULL)
4282 free (debug->line);
4283 if (debug->external_dnr != NULL)
4284 free (debug->external_dnr);
4285 if (debug->external_pdr != NULL)
4286 free (debug->external_pdr);
4287 if (debug->external_sym != NULL)
4288 free (debug->external_sym);
4289 if (debug->external_opt != NULL)
4290 free (debug->external_opt);
4291 if (debug->external_aux != NULL)
4292 free (debug->external_aux);
4293 if (debug->ss != NULL)
4294 free (debug->ss);
4295 if (debug->external_fdr != NULL)
4296 free (debug->external_fdr);
4297 if (debug->external_rfd != NULL)
4298 free (debug->external_rfd);
4299
4300 /* Make sure we don't accidentally follow one of these pointers
4301 into freed memory. */
4302 debug->line = NULL;
4303 debug->external_dnr = NULL;
4304 debug->external_pdr = NULL;
4305 debug->external_sym = NULL;
4306 debug->external_opt = NULL;
4307 debug->external_aux = NULL;
4308 debug->ss = NULL;
4309 debug->external_fdr = NULL;
4310 debug->external_rfd = NULL;
4311 }
966e0a16
ILT
4312
4313 return ret;
4314}
4315
4316/* Put out information for an external symbol. These come only from
4317 the hash table. */
4318
4319static boolean
4320ecoff_link_write_external (h, data)
4321 struct ecoff_link_hash_entry *h;
4322 PTR data;
4323{
4324 bfd *output_bfd = (bfd *) data;
4325
4326 /* FIXME: We should check if this symbol is being stripped. */
4327
4328 if (h->root.written)
4329 return true;
4330
4331 if (h->abfd == (bfd *) NULL)
4332 {
4333 h->esym.jmptbl = 0;
4334 h->esym.cobol_main = 0;
4335 h->esym.weakext = 0;
4336 h->esym.reserved = 0;
4337 h->esym.ifd = ifdNil;
4338 h->esym.asym.value = 0;
966e0a16 4339 h->esym.asym.st = stGlobal;
a3a33af3
ILT
4340
4341 if (h->root.type != bfd_link_hash_defined)
4342 h->esym.asym.sc = scAbs;
4343 else
4344 {
4345 asection *output_section;
4346 const char *name;
4347
4348 output_section = h->root.u.def.section->output_section;
4349 name = bfd_section_name (output_section->owner, output_section);
4350
4351 if (strcmp (name, _TEXT) == 0)
4352 h->esym.asym.sc = scText;
4353 else if (strcmp (name, _DATA) == 0)
4354 h->esym.asym.sc = scData;
4355 else if (strcmp (name, _SDATA) == 0)
4356 h->esym.asym.sc = scSData;
4357 else if (strcmp (name, _RDATA) == 0)
4358 h->esym.asym.sc = scRData;
4359 else if (strcmp (name, _BSS) == 0)
4360 h->esym.asym.sc = scBss;
4361 else if (strcmp (name, _SBSS) == 0)
4362 h->esym.asym.sc = scSBss;
4363 else if (strcmp (name, _INIT) == 0)
4364 h->esym.asym.sc = scInit;
4365 else if (strcmp (name, _FINI) == 0)
4366 h->esym.asym.sc = scFini;
4367 else if (strcmp (name, _PDATA) == 0)
4368 h->esym.asym.sc = scPData;
4369 else if (strcmp (name, _XDATA) == 0)
4370 h->esym.asym.sc = scXData;
4371 else
4372 h->esym.asym.sc = scAbs;
4373 }
4374
966e0a16
ILT
4375 h->esym.asym.reserved = 0;
4376 h->esym.asym.index = indexNil;
4377 }
3f048f7f 4378 else if (h->esym.ifd != -1)
966e0a16 4379 {
3f048f7f
ILT
4380 struct ecoff_debug_info *debug;
4381
966e0a16
ILT
4382 /* Adjust the FDR index for the symbol by that used for the
4383 input BFD. */
3f048f7f
ILT
4384 debug = &ecoff_data (h->abfd)->debug_info;
4385 BFD_ASSERT (h->esym.ifd >= 0
4386 && h->esym.ifd < debug->symbolic_header.ifdMax);
4387 h->esym.ifd = debug->ifdmap[h->esym.ifd];
966e0a16
ILT
4388 }
4389
4390 switch (h->root.type)
4391 {
4392 default:
4393 case bfd_link_hash_new:
4394 abort ();
4395 case bfd_link_hash_undefined:
4396 case bfd_link_hash_weak:
a3a33af3
ILT
4397 if (h->esym.asym.sc != scUndefined
4398 && h->esym.asym.sc != scSUndefined)
4399 h->esym.asym.sc = scUndefined;
966e0a16
ILT
4400 break;
4401 case bfd_link_hash_defined:
4402 if (h->esym.asym.sc == scUndefined
4403 || h->esym.asym.sc == scSUndefined)
4404 h->esym.asym.sc = scAbs;
4405 else if (h->esym.asym.sc == scCommon)
4406 h->esym.asym.sc = scBss;
4407 else if (h->esym.asym.sc == scSCommon)
4408 h->esym.asym.sc = scSBss;
4409 h->esym.asym.value = (h->root.u.def.value
4410 + h->root.u.def.section->output_section->vma
4411 + h->root.u.def.section->output_offset);
4412 break;
4413 case bfd_link_hash_common:
4414 if (h->esym.asym.sc != scCommon
4415 && h->esym.asym.sc != scSCommon)
4416 h->esym.asym.sc = scCommon;
4417 h->esym.asym.value = h->root.u.c.size;
4418 break;
4419 case bfd_link_hash_indirect:
4420 case bfd_link_hash_warning:
4421 /* FIXME: Ignore these for now. The circumstances under which
4422 they should be written out are not clear to me. */
4423 return true;
4424 }
4425
4426 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4427 symbol number. */
4428 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4429 h->root.written = true;
4430
4431 return (bfd_ecoff_debug_one_external
4432 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4433 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4434 &h->esym));
4435}
4436
4437/* Relocate and write an ECOFF section into an ECOFF output file. */
4438
4439static boolean
4440ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4441 bfd *output_bfd;
4442 struct bfd_link_info *info;
4443 asection *output_section;
4444 struct bfd_link_order *link_order;
4445{
4446 asection *input_section;
4447 bfd *input_bfd;
a3a33af3
ILT
4448 struct ecoff_section_tdata *section_tdata;
4449 bfd_size_type raw_size;
4450 bfd_size_type cooked_size;
80425e6c 4451 bfd_byte *contents = NULL;
966e0a16
ILT
4452 bfd_size_type external_reloc_size;
4453 bfd_size_type external_relocs_size;
80425e6c 4454 PTR external_relocs = NULL;
966e0a16
ILT
4455
4456 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4457
4458 if (link_order->size == 0)
4459 return true;
4460
4461 input_section = link_order->u.indirect.section;
4462 input_bfd = input_section->owner;
a3a33af3
ILT
4463 section_tdata = ecoff_section_data (input_bfd, input_section);
4464
4465 raw_size = input_section->_raw_size;
4466 cooked_size = input_section->_cooked_size;
4467 if (cooked_size == 0)
4468 cooked_size = raw_size;
966e0a16
ILT
4469
4470 BFD_ASSERT (input_section->output_section == output_section);
4471 BFD_ASSERT (input_section->output_offset == link_order->offset);
a3a33af3
ILT
4472 BFD_ASSERT (cooked_size == link_order->size);
4473
4474 /* Get the section contents. We allocate memory for the larger of
4475 the size before relocating and the size after relocating. */
4476 contents = (bfd_byte *) malloc (raw_size >= cooked_size
4477 ? raw_size
4478 : cooked_size);
4479 if (contents == NULL && raw_size != 0)
80425e6c
JK
4480 {
4481 bfd_set_error (bfd_error_no_memory);
4482 goto error_return;
4483 }
966e0a16 4484
a3a33af3
ILT
4485 /* If we are relaxing, the contents may have already been read into
4486 memory, in which case we copy them into our new buffer. We don't
4487 simply reuse the old buffer in case cooked_size > raw_size. */
4488 if (section_tdata != (struct ecoff_section_tdata *) NULL
4489 && section_tdata->contents != (bfd_byte *) NULL)
4490 memcpy (contents, section_tdata->contents, raw_size);
4491 else
4492 {
4493 if (! bfd_get_section_contents (input_bfd, input_section,
4494 (PTR) contents,
4495 (file_ptr) 0, raw_size))
4496 goto error_return;
4497 }
4498
4499 /* Get the relocs. If we are relaxing MIPS code, they will already
4500 have been read in. Otherwise, we read them in now. */
966e0a16
ILT
4501 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4502 external_relocs_size = external_reloc_size * input_section->reloc_count;
a3a33af3
ILT
4503
4504 if (section_tdata != (struct ecoff_section_tdata *) NULL)
4505 external_relocs = section_tdata->external_relocs;
4506 else
80425e6c 4507 {
a3a33af3
ILT
4508 external_relocs = (PTR) malloc (external_relocs_size);
4509 if (external_relocs == NULL && external_relocs_size != 0)
4510 {
4511 bfd_set_error (bfd_error_no_memory);
4512 goto error_return;
4513 }
80425e6c 4514
a3a33af3
ILT
4515 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4516 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4517 != external_relocs_size))
4518 goto error_return;
4519 }
966e0a16
ILT
4520
4521 /* Relocate the section contents. */
4522 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4523 (output_bfd, info, input_bfd, input_section, contents,
4524 external_relocs)))
80425e6c 4525 goto error_return;
966e0a16
ILT
4526
4527 /* Write out the relocated section. */
4528 if (! bfd_set_section_contents (output_bfd,
4529 output_section,
4530 (PTR) contents,
4531 input_section->output_offset,
a3a33af3 4532 cooked_size))
80425e6c 4533 goto error_return;
966e0a16
ILT
4534
4535 /* If we are producing relocateable output, the relocs were
4536 modified, and we write them out now. We use the reloc_count
4537 field of output_section to keep track of the number of relocs we
4538 have output so far. */
4539 if (info->relocateable)
4540 {
4541 if (bfd_seek (output_bfd,
4542 (output_section->rel_filepos +
4543 output_section->reloc_count * external_reloc_size),
4544 SEEK_SET) != 0
4545 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4546 != external_relocs_size))
80425e6c 4547 goto error_return;
966e0a16
ILT
4548 output_section->reloc_count += input_section->reloc_count;
4549 }
4550
80425e6c
JK
4551 if (contents != NULL)
4552 free (contents);
a3a33af3 4553 if (external_relocs != NULL && section_tdata == NULL)
80425e6c 4554 free (external_relocs);
966e0a16 4555 return true;
80425e6c
JK
4556
4557 error_return:
4558 if (contents != NULL)
4559 free (contents);
a3a33af3 4560 if (external_relocs != NULL && section_tdata == NULL)
80425e6c
JK
4561 free (external_relocs);
4562 return false;
966e0a16 4563}
a3a33af3
ILT
4564
4565/* Generate a reloc when linking an ECOFF file. This is a reloc
4566 requested by the linker, and does come from any input file. This
4567 is used to build constructor and destructor tables when linking
4568 with -Ur. */
4569
4570static boolean
4571ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4572 bfd *output_bfd;
4573 struct bfd_link_info *info;
4574 asection *output_section;
4575 struct bfd_link_order *link_order;
4576{
4577 arelent rel;
4578 struct internal_reloc in;
4579 bfd_size_type external_reloc_size;
4580 bfd_byte *rbuf;
4581 boolean ok;
4582
4583 /* We set up an arelent to pass to the backend adjust_reloc_out
4584 routine. */
4585 rel.address = link_order->offset;
4586
4587 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4588 if (rel.howto == (const reloc_howto_type *) NULL)
4589 {
4590 bfd_set_error (bfd_error_bad_value);
4591 return false;
4592 }
4593
4594 if (link_order->type == bfd_section_reloc_link_order)
4595 rel.sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
4596 else
4597 {
4598 /* We can't set up a reloc against a symbol correctly, because
4599 we have no asymbol structure. Currently no adjust_reloc_out
4600 routine cases. */
4601 rel.sym_ptr_ptr = (asymbol **) NULL;
4602 }
4603
4604 /* All ECOFF relocs are in-place. Put the addend into the object
4605 file. */
4606
4607 BFD_ASSERT (rel.howto->partial_inplace);
4608 if (link_order->u.reloc.p->addend != 0)
4609 {
4610 bfd_size_type size;
4611 bfd_reloc_status_type rstat;
4612 bfd_byte *buf;
4613 boolean ok;
4614
4615 size = bfd_get_reloc_size (rel.howto);
4616 buf = (bfd_byte *) bfd_zmalloc (size);
4617 if (buf == (bfd_byte *) NULL)
4618 {
4619 bfd_set_error (bfd_error_no_memory);
4620 return false;
4621 }
4622 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4623 link_order->u.reloc.p->addend, buf);
4624 switch (rstat)
4625 {
4626 case bfd_reloc_ok:
4627 break;
4628 default:
4629 case bfd_reloc_outofrange:
4630 abort ();
4631 case bfd_reloc_overflow:
4632 if (! ((*info->callbacks->reloc_overflow)
4633 (info,
4634 (link_order->type == bfd_section_reloc_link_order
4635 ? bfd_section_name (output_bfd,
4636 link_order->u.reloc.p->u.section)
4637 : link_order->u.reloc.p->u.name),
4638 rel.howto->name, link_order->u.reloc.p->addend,
4639 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
4640 {
4641 free (buf);
4642 return false;
4643 }
4644 break;
4645 }
4646 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4647 (file_ptr) link_order->offset, size);
4648 free (buf);
4649 if (! ok)
4650 return false;
4651 }
4652
4653 rel.addend = 0;
4654
4655 /* Move the information into a internal_reloc structure. */
4656 in.r_vaddr = (rel.address
4657 + bfd_get_section_vma (output_bfd, output_section));
4658 in.r_type = rel.howto->type;
4659
4660 if (link_order->type == bfd_symbol_reloc_link_order)
4661 {
4662 struct ecoff_link_hash_entry *h;
4663
4664 h = ecoff_link_hash_lookup (ecoff_hash_table (info),
4665 link_order->u.reloc.p->u.name,
4666 false, false, true);
4667 if (h != (struct ecoff_link_hash_entry *) NULL
4668 && h->indx != -1)
4669 in.r_symndx = h->indx;
4670 else
4671 {
4672 if (! ((*info->callbacks->unattached_reloc)
4673 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4674 (asection *) NULL, (bfd_vma) 0)))
4675 return false;
4676 in.r_symndx = 0;
4677 }
4678 in.r_extern = 1;
4679 }
4680 else
4681 {
4682 CONST char *name;
4683
4684 name = bfd_get_section_name (output_bfd,
4685 link_order->u.reloc.p->u.section);
4686 if (strcmp (name, ".text") == 0)
4687 in.r_symndx = RELOC_SECTION_TEXT;
4688 else if (strcmp (name, ".rdata") == 0)
4689 in.r_symndx = RELOC_SECTION_RDATA;
4690 else if (strcmp (name, ".data") == 0)
4691 in.r_symndx = RELOC_SECTION_DATA;
4692 else if (strcmp (name, ".sdata") == 0)
4693 in.r_symndx = RELOC_SECTION_SDATA;
4694 else if (strcmp (name, ".sbss") == 0)
4695 in.r_symndx = RELOC_SECTION_SBSS;
4696 else if (strcmp (name, ".bss") == 0)
4697 in.r_symndx = RELOC_SECTION_BSS;
4698 else if (strcmp (name, ".init") == 0)
4699 in.r_symndx = RELOC_SECTION_INIT;
4700 else if (strcmp (name, ".lit8") == 0)
4701 in.r_symndx = RELOC_SECTION_LIT8;
4702 else if (strcmp (name, ".lit4") == 0)
4703 in.r_symndx = RELOC_SECTION_LIT4;
4704 else if (strcmp (name, ".xdata") == 0)
4705 in.r_symndx = RELOC_SECTION_XDATA;
4706 else if (strcmp (name, ".pdata") == 0)
4707 in.r_symndx = RELOC_SECTION_PDATA;
4708 else if (strcmp (name, ".fini") == 0)
4709 in.r_symndx = RELOC_SECTION_FINI;
4710 else if (strcmp (name, ".lita") == 0)
4711 in.r_symndx = RELOC_SECTION_LITA;
4712 else if (strcmp (name, "*ABS*") == 0)
4713 in.r_symndx = RELOC_SECTION_ABS;
4714 else
4715 abort ();
4716 in.r_extern = 0;
4717 }
4718
4719 /* Let the BFD backend adjust the reloc. */
4720 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4721
4722 /* Get some memory and swap out the reloc. */
4723 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4724 rbuf = (bfd_byte *) malloc (external_reloc_size);
4725 if (rbuf == (bfd_byte *) NULL)
4726 {
4727 bfd_set_error (bfd_error_no_memory);
4728 return false;
4729 }
4730
4731 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4732
4733 ok = (bfd_seek (output_bfd,
4734 (output_section->rel_filepos +
4735 output_section->reloc_count * external_reloc_size),
4736 SEEK_SET) == 0
4737 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4738 == external_reloc_size));
4739
4740 if (ok)
4741 ++output_section->reloc_count;
4742
4743 free (rbuf);
4744
4745 return ok;
4746}
This page took 0.252959 seconds and 4 git commands to generate.