* libbfd.c (bfd_read): Set bfd_error as appropriate for a short
[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
1215unsigned int
1216ecoff_get_symtab_upper_bound (abfd)
1217 bfd *abfd;
1218{
1219 if (ecoff_slurp_symbolic_info (abfd) == false
1220 || bfd_get_symcount (abfd) == 0)
1221 return 0;
1222
1223 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1224}
1225
b59f0276 1226/* Get the canonical symbols. */
dae31cf5
ILT
1227
1228unsigned int
1229ecoff_get_symtab (abfd, alocation)
1230 bfd *abfd;
1231 asymbol **alocation;
1232{
1233 unsigned int counter = 0;
1234 ecoff_symbol_type *symbase;
1235 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1236
1237 if (ecoff_slurp_symbol_table (abfd) == false
1238 || bfd_get_symcount (abfd) == 0)
1239 return 0;
1240
1241 symbase = ecoff_data (abfd)->canonical_symbols;
1242 while (counter < bfd_get_symcount (abfd))
1243 {
1244 *(location++) = symbase++;
1245 counter++;
1246 }
1247 *location++ = (ecoff_symbol_type *) NULL;
1248 return bfd_get_symcount (abfd);
1249}
1250
1251/* Turn ECOFF type information into a printable string.
1252 ecoff_emit_aggregate and ecoff_type_to_string are from
1253 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1254
1255/* Write aggregate information to a string. */
1256
1257static void
1258ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1259 bfd *abfd;
1260 char *string;
1261 RNDXR *rndx;
1262 long isym;
1263 CONST char *which;
1264{
1265 int ifd = rndx->rfd;
1266 int indx = rndx->index;
1267 int sym_base, ss_base;
1268 CONST char *name;
1269
1270 if (ifd == 0xfff)
1271 ifd = isym;
1272
c9668c58
ILT
1273 sym_base = ecoff_data (abfd)->debug_info.fdr[ifd].isymBase;
1274 ss_base = ecoff_data (abfd)->debug_info.fdr[ifd].issBase;
dae31cf5
ILT
1275
1276 if (indx == indexNil)
1277 name = "/* no name */";
1278 else
1279 {
c9668c58
ILT
1280 const struct ecoff_debug_swap * const debug_swap
1281 = &ecoff_backend (abfd)->debug_swap;
dae31cf5
ILT
1282 SYMR sym;
1283
1284 indx += sym_base;
c9668c58
ILT
1285 (*debug_swap->swap_sym_in)
1286 (abfd,
1287 ((char *) ecoff_data (abfd)->debug_info.external_sym
1288 + indx * debug_swap->external_sym_size),
1289 &sym);
1290 name = ecoff_data (abfd)->debug_info.ss + ss_base + sym.iss;
dae31cf5
ILT
1291 }
1292
1293 sprintf (string,
c9668c58 1294 "%s %s { ifd = %d, index = %ld }",
dae31cf5 1295 which, name, ifd,
c9668c58
ILT
1296 ((long) indx
1297 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax));
dae31cf5
ILT
1298}
1299
1300/* Convert the type information to string format. */
1301
1302static char *
1303ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1304 bfd *abfd;
1305 union aux_ext *aux_ptr;
1306 unsigned int indx;
1307 int bigendian;
1308{
1309 AUXU u;
1310 struct qual {
1311 unsigned int type;
1312 int low_bound;
1313 int high_bound;
1314 int stride;
1315 } qualifiers[7];
1316
1317 unsigned int basic_type;
1318 int i;
1319 static char buffer1[1024];
1320 static char buffer2[1024];
1321 char *p1 = buffer1;
1322 char *p2 = buffer2;
1323 RNDXR rndx;
1324
1325 for (i = 0; i < 7; i++)
1326 {
1327 qualifiers[i].low_bound = 0;
1328 qualifiers[i].high_bound = 0;
1329 qualifiers[i].stride = 0;
1330 }
1331
1332 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1333 return "-1 (no type)";
1334 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1335
1336 basic_type = u.ti.bt;
1337 qualifiers[0].type = u.ti.tq0;
1338 qualifiers[1].type = u.ti.tq1;
1339 qualifiers[2].type = u.ti.tq2;
1340 qualifiers[3].type = u.ti.tq3;
1341 qualifiers[4].type = u.ti.tq4;
1342 qualifiers[5].type = u.ti.tq5;
1343 qualifiers[6].type = tqNil;
1344
1345 /*
1346 * Go get the basic type.
1347 */
1348 switch (basic_type)
1349 {
1350 case btNil: /* undefined */
1351 strcpy (p1, "nil");
1352 break;
1353
1354 case btAdr: /* address - integer same size as pointer */
1355 strcpy (p1, "address");
1356 break;
1357
1358 case btChar: /* character */
1359 strcpy (p1, "char");
1360 break;
1361
1362 case btUChar: /* unsigned character */
1363 strcpy (p1, "unsigned char");
1364 break;
1365
1366 case btShort: /* short */
1367 strcpy (p1, "short");
1368 break;
1369
1370 case btUShort: /* unsigned short */
1371 strcpy (p1, "unsigned short");
1372 break;
1373
1374 case btInt: /* int */
1375 strcpy (p1, "int");
1376 break;
1377
1378 case btUInt: /* unsigned int */
1379 strcpy (p1, "unsigned int");
1380 break;
1381
1382 case btLong: /* long */
1383 strcpy (p1, "long");
1384 break;
1385
1386 case btULong: /* unsigned long */
1387 strcpy (p1, "unsigned long");
1388 break;
1389
1390 case btFloat: /* float (real) */
1391 strcpy (p1, "float");
1392 break;
1393
1394 case btDouble: /* Double (real) */
1395 strcpy (p1, "double");
1396 break;
1397
1398 /* Structures add 1-2 aux words:
1399 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1400 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1401
1402 case btStruct: /* Structure (Record) */
1403 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1404 ecoff_emit_aggregate (abfd, p1, &rndx,
4c3721d5 1405 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
dae31cf5
ILT
1406 "struct");
1407 indx++; /* skip aux words */
1408 break;
1409
1410 /* Unions add 1-2 aux words:
1411 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1412 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1413
1414 case btUnion: /* Union */
1415 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1416 ecoff_emit_aggregate (abfd, p1, &rndx,
4c3721d5 1417 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
dae31cf5
ILT
1418 "union");
1419 indx++; /* skip aux words */
1420 break;
1421
1422 /* Enumerations add 1-2 aux words:
1423 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1424 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1425
1426 case btEnum: /* Enumeration */
1427 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1428 ecoff_emit_aggregate (abfd, p1, &rndx,
4c3721d5 1429 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
dae31cf5
ILT
1430 "enum");
1431 indx++; /* skip aux words */
1432 break;
1433
1434 case btTypedef: /* defined via a typedef, isymRef points */
1435 strcpy (p1, "typedef");
1436 break;
1437
1438 case btRange: /* subrange of int */
1439 strcpy (p1, "subrange");
1440 break;
1441
1442 case btSet: /* pascal sets */
1443 strcpy (p1, "set");
1444 break;
1445
1446 case btComplex: /* fortran complex */
1447 strcpy (p1, "complex");
1448 break;
1449
1450 case btDComplex: /* fortran double complex */
1451 strcpy (p1, "double complex");
1452 break;
1453
1454 case btIndirect: /* forward or unnamed typedef */
1455 strcpy (p1, "forward/unamed typedef");
1456 break;
1457
1458 case btFixedDec: /* Fixed Decimal */
1459 strcpy (p1, "fixed decimal");
1460 break;
1461
1462 case btFloatDec: /* Float Decimal */
1463 strcpy (p1, "float decimal");
1464 break;
1465
1466 case btString: /* Varying Length Character String */
1467 strcpy (p1, "string");
1468 break;
1469
1470 case btBit: /* Aligned Bit String */
1471 strcpy (p1, "bit");
1472 break;
1473
1474 case btPicture: /* Picture */
1475 strcpy (p1, "picture");
1476 break;
1477
1478 case btVoid: /* Void */
1479 strcpy (p1, "void");
1480 break;
1481
1482 default:
1483 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1484 break;
1485 }
1486
1487 p1 += strlen (buffer1);
1488
1489 /*
1490 * If this is a bitfield, get the bitsize.
1491 */
1492 if (u.ti.fBitfield)
1493 {
1494 int bitsize;
1495
1496 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1497 sprintf (p1, " : %d", bitsize);
1498 p1 += strlen (buffer1);
1499 }
1500
1501
1502 /*
1503 * Deal with any qualifiers.
1504 */
1505 if (qualifiers[0].type != tqNil)
1506 {
1507 /*
1508 * Snarf up any array bounds in the correct order. Arrays
1509 * store 5 successive words in the aux. table:
1510 * word 0 RNDXR to type of the bounds (ie, int)
1511 * word 1 Current file descriptor index
1512 * word 2 low bound
1513 * word 3 high bound (or -1 if [])
1514 * word 4 stride size in bits
1515 */
1516 for (i = 0; i < 7; i++)
1517 {
1518 if (qualifiers[i].type == tqArray)
1519 {
1520 qualifiers[i].low_bound =
1521 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1522 qualifiers[i].high_bound =
1523 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1524 qualifiers[i].stride =
1525 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1526 indx += 5;
1527 }
1528 }
1529
1530 /*
1531 * Now print out the qualifiers.
1532 */
1533 for (i = 0; i < 6; i++)
1534 {
1535 switch (qualifiers[i].type)
1536 {
1537 case tqNil:
1538 case tqMax:
1539 break;
1540
1541 case tqPtr:
1542 strcpy (p2, "ptr to ");
1543 p2 += sizeof ("ptr to ")-1;
1544 break;
1545
1546 case tqVol:
1547 strcpy (p2, "volatile ");
1548 p2 += sizeof ("volatile ")-1;
1549 break;
1550
1551 case tqFar:
1552 strcpy (p2, "far ");
1553 p2 += sizeof ("far ")-1;
1554 break;
1555
1556 case tqProc:
1557 strcpy (p2, "func. ret. ");
1558 p2 += sizeof ("func. ret. ");
1559 break;
1560
1561 case tqArray:
1562 {
1563 int first_array = i;
1564 int j;
1565
1566 /* Print array bounds reversed (ie, in the order the C
1567 programmer writes them). C is such a fun language.... */
1568
1569 while (i < 5 && qualifiers[i+1].type == tqArray)
1570 i++;
1571
1572 for (j = i; j >= first_array; j--)
1573 {
1574 strcpy (p2, "array [");
1575 p2 += sizeof ("array [")-1;
1576 if (qualifiers[j].low_bound != 0)
1577 sprintf (p2,
1578 "%ld:%ld {%ld bits}",
1579 (long) qualifiers[j].low_bound,
1580 (long) qualifiers[j].high_bound,
1581 (long) qualifiers[j].stride);
1582
1583 else if (qualifiers[j].high_bound != -1)
1584 sprintf (p2,
1585 "%ld {%ld bits}",
1586 (long) (qualifiers[j].high_bound + 1),
1587 (long) (qualifiers[j].stride));
1588
1589 else
1590 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1591
1592 p2 += strlen (p2);
1593 strcpy (p2, "] of ");
1594 p2 += sizeof ("] of ")-1;
1595 }
1596 }
1597 break;
1598 }
1599 }
1600 }
1601
1602 strcpy (p2, buffer1);
1603 return buffer2;
1604}
1605
1606/* Return information about ECOFF symbol SYMBOL in RET. */
1607
728472f1 1608/*ARGSUSED*/
dae31cf5
ILT
1609void
1610ecoff_get_symbol_info (abfd, symbol, ret)
1611 bfd *abfd; /* Ignored. */
1612 asymbol *symbol;
1613 symbol_info *ret;
1614{
1615 bfd_symbol_info (symbol, ret);
1616}
1617
1618/* Print information about an ECOFF symbol. */
1619
1620void
1621ecoff_print_symbol (abfd, filep, symbol, how)
1622 bfd *abfd;
1623 PTR filep;
1624 asymbol *symbol;
1625 bfd_print_symbol_type how;
1626{
c9668c58
ILT
1627 const struct ecoff_debug_swap * const debug_swap
1628 = &ecoff_backend (abfd)->debug_swap;
dae31cf5
ILT
1629 FILE *file = (FILE *)filep;
1630
1631 switch (how)
1632 {
1633 case bfd_print_symbol_name:
1634 fprintf (file, "%s", symbol->name);
1635 break;
1636 case bfd_print_symbol_more:
1637 if (ecoffsymbol (symbol)->local)
1638 {
1639 SYMR ecoff_sym;
1640
c9668c58
ILT
1641 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1642 &ecoff_sym);
dae31cf5
ILT
1643 fprintf (file, "ecoff local ");
1644 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1645 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1646 (unsigned) ecoff_sym.sc);
1647 }
1648 else
1649 {
1650 EXTR ecoff_ext;
1651
c9668c58
ILT
1652 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1653 &ecoff_ext);
dae31cf5
ILT
1654 fprintf (file, "ecoff extern ");
1655 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1656 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1657 (unsigned) ecoff_ext.asym.sc);
1658 }
1659 break;
1660 case bfd_print_symbol_all:
1661 /* Print out the symbols in a reasonable way */
1662 {
1663 char type;
1664 int pos;
1665 EXTR ecoff_ext;
1666 char jmptbl;
1667 char cobol_main;
1668 char weakext;
1669
1670 if (ecoffsymbol (symbol)->local)
1671 {
c9668c58
ILT
1672 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1673 &ecoff_ext.asym);
dae31cf5
ILT
1674 type = 'l';
1675 pos = ((((char *) ecoffsymbol (symbol)->native
c9668c58
ILT
1676 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1677 / debug_swap->external_sym_size)
1678 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
dae31cf5
ILT
1679 jmptbl = ' ';
1680 cobol_main = ' ';
1681 weakext = ' ';
1682 }
1683 else
1684 {
c9668c58
ILT
1685 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1686 &ecoff_ext);
dae31cf5
ILT
1687 type = 'e';
1688 pos = (((char *) ecoffsymbol (symbol)->native
c9668c58
ILT
1689 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1690 / debug_swap->external_ext_size);
dae31cf5
ILT
1691 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1692 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1693 weakext = ecoff_ext.weakext ? 'w' : ' ';
1694 }
1695
1696 fprintf (file, "[%3d] %c ",
1697 pos, type);
1698 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1699 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1700 (unsigned) ecoff_ext.asym.st,
1701 (unsigned) ecoff_ext.asym.sc,
1702 (unsigned) ecoff_ext.asym.index,
1703 jmptbl, cobol_main, weakext,
1704 symbol->name);
1705
1706 if (ecoffsymbol (symbol)->fdr != NULL
1707 && ecoff_ext.asym.index != indexNil)
1708 {
1709 unsigned int indx;
1710 int bigendian;
1711 bfd_size_type sym_base;
1712 union aux_ext *aux_base;
1713
1714 indx = ecoff_ext.asym.index;
1715
1716 /* sym_base is used to map the fdr relative indices which
1717 appear in the file to the position number which we are
1718 using. */
1719 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1720 if (ecoffsymbol (symbol)->local)
c9668c58
ILT
1721 sym_base +=
1722 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
dae31cf5
ILT
1723
1724 /* aux_base is the start of the aux entries for this file;
1725 asym.index is an offset from this. */
c9668c58 1726 aux_base = (ecoff_data (abfd)->debug_info.external_aux
dae31cf5
ILT
1727 + ecoffsymbol (symbol)->fdr->iauxBase);
1728
1729 /* The aux entries are stored in host byte order; the
1730 order is indicated by a bit in the fdr. */
1731 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1732
1733 /* This switch is basically from gcc/mips-tdump.c */
1734 switch (ecoff_ext.asym.st)
1735 {
1736 case stNil:
1737 case stLabel:
1738 break;
1739
1740 case stFile:
1741 case stBlock:
1742 fprintf (file, "\n End+1 symbol: %ld",
1743 (long) (indx + sym_base));
1744 break;
1745
1746 case stEnd:
1747 if (ecoff_ext.asym.sc == scText
1748 || ecoff_ext.asym.sc == scInfo)
1749 fprintf (file, "\n First symbol: %ld",
1750 (long) (indx + sym_base));
1751 else
1752 fprintf (file, "\n First symbol: %ld",
1753 (long) (AUX_GET_ISYM (bigendian,
1754 &aux_base[ecoff_ext.asym.index])
1755 + sym_base));
1756 break;
1757
1758 case stProc:
1759 case stStaticProc:
1760 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1761 ;
1762 else if (ecoffsymbol (symbol)->local)
1763 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1764 (long) (AUX_GET_ISYM (bigendian,
1765 &aux_base[ecoff_ext.asym.index])
1766 + sym_base),
1767 ecoff_type_to_string (abfd, aux_base, indx + 1,
1768 bigendian));
1769 else
c9668c58
ILT
1770 fprintf (file, "\n Local symbol: %ld",
1771 ((long) indx
1772 + (long) sym_base
1773 + (ecoff_data (abfd)
1774 ->debug_info.symbolic_header.iextMax)));
dae31cf5
ILT
1775 break;
1776
1777 default:
1778 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1779 fprintf (file, "\n Type: %s",
1780 ecoff_type_to_string (abfd, aux_base, indx,
1781 bigendian));
1782 break;
1783 }
1784 }
1785 }
1786 break;
1787 }
1788}
1789\f
dae31cf5
ILT
1790/* Read in the relocs for a section. */
1791
1792static boolean
1793ecoff_slurp_reloc_table (abfd, section, symbols)
1794 bfd *abfd;
1795 asection *section;
1796 asymbol **symbols;
1797{
1798 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1799 arelent *internal_relocs;
1800 bfd_size_type external_reloc_size;
1801 bfd_size_type external_relocs_size;
1802 char *external_relocs;
1803 arelent *rptr;
1804 unsigned int i;
1805
1806 if (section->relocation != (arelent *) NULL
1807 || section->reloc_count == 0
1808 || (section->flags & SEC_CONSTRUCTOR) != 0)
1809 return true;
1810
1811 if (ecoff_slurp_symbol_table (abfd) == false)
1812 return false;
1813
1814 internal_relocs = (arelent *) bfd_alloc (abfd,
1815 (sizeof (arelent)
1816 * section->reloc_count));
1817 external_reloc_size = backend->external_reloc_size;
1818 external_relocs_size = external_reloc_size * section->reloc_count;
1819 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1820 if (internal_relocs == (arelent *) NULL
1821 || external_relocs == (char *) NULL)
1822 {
d1ad85a6 1823 bfd_set_error (bfd_error_no_memory);
dae31cf5
ILT
1824 return false;
1825 }
1826 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1827 return false;
1828 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1829 != external_relocs_size)
25057836 1830 return false;
dae31cf5
ILT
1831
1832 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1833 {
1834 struct internal_reloc intern;
1835
1836 (*backend->swap_reloc_in) (abfd,
1837 external_relocs + i * external_reloc_size,
1838 &intern);
1839
dae31cf5
ILT
1840 if (intern.r_extern)
1841 {
1842 /* r_symndx is an index into the external symbols. */
1843 BFD_ASSERT (intern.r_symndx >= 0
1844 && (intern.r_symndx
c9668c58
ILT
1845 < (ecoff_data (abfd)
1846 ->debug_info.symbolic_header.iextMax)));
dae31cf5
ILT
1847 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1848 rptr->addend = 0;
1849 }
e544ed4f
ILT
1850 else if (intern.r_symndx == RELOC_SECTION_NONE
1851 || intern.r_symndx == RELOC_SECTION_ABS)
1852 {
1853 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1854 rptr->addend = 0;
1855 }
dae31cf5
ILT
1856 else
1857 {
1858 CONST char *sec_name;
1859 asection *sec;
1860
1861 /* r_symndx is a section key. */
1862 switch (intern.r_symndx)
1863 {
1864 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1865 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1866 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1867 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1868 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1869 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1870 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1871 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1872 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
8f46bac8
ILT
1873 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1874 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
c9668c58 1875 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
8f46bac8 1876 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
dae31cf5
ILT
1877 default: abort ();
1878 }
1879
1880 sec = bfd_get_section_by_name (abfd, sec_name);
1881 if (sec == (asection *) NULL)
e544ed4f 1882 abort ();
dae31cf5
ILT
1883 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1884
1885 rptr->addend = - bfd_get_section_vma (abfd, sec);
dae31cf5
ILT
1886 }
1887
1888 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
dae31cf5 1889
8f46bac8
ILT
1890 /* Let the backend select the howto field and do any other
1891 required processing. */
c9668c58 1892 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
dae31cf5
ILT
1893 }
1894
1895 bfd_release (abfd, external_relocs);
1896
1897 section->relocation = internal_relocs;
1898
1899 return true;
1900}
1901
1902/* Get a canonical list of relocs. */
1903
1904unsigned int
1905ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1906 bfd *abfd;
1907 asection *section;
1908 arelent **relptr;
1909 asymbol **symbols;
1910{
1911 unsigned int count;
1912
1913 if (section->flags & SEC_CONSTRUCTOR)
1914 {
1915 arelent_chain *chain;
1916
1917 /* This section has relocs made up by us, not the file, so take
1918 them out of their chain and place them into the data area
1919 provided. */
1920 for (count = 0, chain = section->constructor_chain;
1921 count < section->reloc_count;
1922 count++, chain = chain->next)
1923 *relptr++ = &chain->relent;
1924 }
1925 else
1926 {
1927 arelent *tblptr;
1928
1929 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1930 return 0;
1931
1932 tblptr = section->relocation;
1933 if (tblptr == (arelent *) NULL)
1934 return 0;
1935
1936 for (count = 0; count < section->reloc_count; count++)
1937 *relptr++ = tblptr++;
1938 }
1939
1940 *relptr = (arelent *) NULL;
1941
1942 return section->reloc_count;
1943}
dae31cf5
ILT
1944\f
1945/* Provided a BFD, a section and an offset into the section, calculate
1946 and return the name of the source file and the line nearest to the
1947 wanted location. */
1948
728472f1 1949/*ARGSUSED*/
dae31cf5
ILT
1950boolean
1951ecoff_find_nearest_line (abfd,
1952 section,
1953 ignore_symbols,
1954 offset,
1955 filename_ptr,
1956 functionname_ptr,
1957 retline_ptr)
1958 bfd *abfd;
1959 asection *section;
1960 asymbol **ignore_symbols;
1961 bfd_vma offset;
1962 CONST char **filename_ptr;
1963 CONST char **functionname_ptr;
1964 unsigned int *retline_ptr;
1965{
c9668c58
ILT
1966 const struct ecoff_debug_swap * const debug_swap
1967 = &ecoff_backend (abfd)->debug_swap;
dae31cf5
ILT
1968 FDR *fdr_ptr;
1969 FDR *fdr_start;
1970 FDR *fdr_end;
1971 FDR *fdr_hold;
1972 bfd_size_type external_pdr_size;
1973 char *pdr_ptr;
1974 char *pdr_end;
1975 PDR pdr;
1976 unsigned char *line_ptr;
1977 unsigned char *line_end;
1978 int lineno;
1979
1980 /* If we're not in the .text section, we don't have any line
1981 numbers. */
1982 if (strcmp (section->name, _TEXT) != 0
1983 || offset < ecoff_data (abfd)->text_start
1984 || offset >= ecoff_data (abfd)->text_end)
1985 return false;
1986
1987 /* Make sure we have the FDR's. */
1988 if (ecoff_slurp_symbolic_info (abfd) == false
1989 || bfd_get_symcount (abfd) == 0)
1990 return false;
1991
1992 /* Each file descriptor (FDR) has a memory address. Here we track
1993 down which FDR we want. The FDR's are stored in increasing
1994 memory order. If speed is ever important, this can become a
1995 binary search. We must ignore FDR's with no PDR entries; they
1996 will have the adr of the FDR before or after them. */
c9668c58
ILT
1997 fdr_start = ecoff_data (abfd)->debug_info.fdr;
1998 fdr_end = fdr_start + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
dae31cf5
ILT
1999 fdr_hold = (FDR *) NULL;
2000 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2001 {
2002 if (fdr_ptr->cpd == 0)
2003 continue;
2004 if (offset < fdr_ptr->adr)
2005 break;
2006 fdr_hold = fdr_ptr;
2007 }
2008 if (fdr_hold == (FDR *) NULL)
2009 return false;
2010 fdr_ptr = fdr_hold;
2011
2012 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2013 have an address, which is relative to the FDR address, and are
2014 also stored in increasing memory order. */
2015 offset -= fdr_ptr->adr;
c9668c58
ILT
2016 external_pdr_size = debug_swap->external_pdr_size;
2017 pdr_ptr = ((char *) ecoff_data (abfd)->debug_info.external_pdr
dae31cf5
ILT
2018 + fdr_ptr->ipdFirst * external_pdr_size);
2019 pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
c9668c58 2020 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
dae31cf5
ILT
2021
2022 /* The address of the first PDR is an offset which applies to the
2023 addresses of all the PDR's. */
2024 offset += pdr.adr;
2025
2026 for (pdr_ptr += external_pdr_size;
2027 pdr_ptr < pdr_end;
2028 pdr_ptr += external_pdr_size)
2029 {
c9668c58 2030 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
dae31cf5
ILT
2031 if (offset < pdr.adr)
2032 break;
2033 }
2034
2035 /* Now we can look for the actual line number. The line numbers are
2036 stored in a very funky format, which I won't try to describe.
2037 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2038 we want; we need this to compute line_end. */
c9668c58 2039 line_end = ecoff_data (abfd)->debug_info.line;
dae31cf5
ILT
2040 if (pdr_ptr == pdr_end)
2041 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2042 else
2043 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2044
2045 /* Now change pdr and pdr_ptr to the one we want. */
2046 pdr_ptr -= external_pdr_size;
c9668c58 2047 (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
dae31cf5
ILT
2048
2049 offset -= pdr.adr;
2050 lineno = pdr.lnLow;
c9668c58 2051 line_ptr = (ecoff_data (abfd)->debug_info.line
dae31cf5
ILT
2052 + fdr_ptr->cbLineOffset
2053 + pdr.cbLineOffset);
2054 while (line_ptr < line_end)
2055 {
2056 int delta;
2057 int count;
2058
2059 delta = *line_ptr >> 4;
2060 if (delta >= 0x8)
2061 delta -= 0x10;
2062 count = (*line_ptr & 0xf) + 1;
2063 ++line_ptr;
2064 if (delta == -8)
2065 {
2066 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2067 if (delta >= 0x8000)
2068 delta -= 0x10000;
2069 line_ptr += 2;
2070 }
2071 lineno += delta;
2072 if (offset < count * 4)
2073 break;
2074 offset -= count * 4;
2075 }
2076
2077 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2078 at least according to gdb/mipsread.c. */
2079 if (fdr_ptr->rss == -1)
2080 {
2081 *filename_ptr = NULL;
2082 if (pdr.isym == -1)
2083 *functionname_ptr = NULL;
2084 else
2085 {
2086 EXTR proc_ext;
2087
c9668c58
ILT
2088 (*debug_swap->swap_ext_in)
2089 (abfd,
2090 ((char *) ecoff_data (abfd)->debug_info.external_ext
2091 + pdr.isym * debug_swap->external_ext_size),
2092 &proc_ext);
2093 *functionname_ptr = (ecoff_data (abfd)->debug_info.ssext
2094 + proc_ext.asym.iss);
dae31cf5
ILT
2095 }
2096 }
2097 else
2098 {
2099 SYMR proc_sym;
2100
c9668c58
ILT
2101 *filename_ptr = (ecoff_data (abfd)->debug_info.ss
2102 + fdr_ptr->issBase
2103 + fdr_ptr->rss);
2104 (*debug_swap->swap_sym_in)
2105 (abfd,
2106 ((char *) ecoff_data (abfd)->debug_info.external_sym
2107 + (fdr_ptr->isymBase + pdr.isym) * debug_swap->external_sym_size),
2108 &proc_sym);
2109 *functionname_ptr = (ecoff_data (abfd)->debug_info.ss
dae31cf5
ILT
2110 + fdr_ptr->issBase
2111 + proc_sym.iss);
2112 }
2113 if (lineno == ilineNil)
2114 lineno = 0;
2115 *retline_ptr = lineno;
2116 return true;
2117}
2118\f
dae31cf5
ILT
2119/* Set the architecture. The supported architecture is stored in the
2120 backend pointer. We always set the architecture anyhow, since many
2121 callers ignore the return value. */
2122
2123boolean
2124ecoff_set_arch_mach (abfd, arch, machine)
2125 bfd *abfd;
2126 enum bfd_architecture arch;
2127 unsigned long machine;
2128{
2129 bfd_default_set_arch_mach (abfd, arch, machine);
2130 return arch == ecoff_backend (abfd)->arch;
2131}
2132
8d12f138
ILT
2133/* Get the size of the section headers. We do not output the .reginfo
2134 section. */
dae31cf5 2135
728472f1 2136/*ARGSUSED*/
dae31cf5
ILT
2137int
2138ecoff_sizeof_headers (abfd, reloc)
2139 bfd *abfd;
2140 boolean reloc;
2141{
a7853216
ILT
2142 asection *current;
2143 int c;
d6074cdd 2144 int ret;
a7853216
ILT
2145
2146 c = 0;
2147 for (current = abfd->sections;
2148 current != (asection *)NULL;
2149 current = current->next)
8d12f138 2150 if (strcmp (current->name, REGINFO) != 0)
a7853216
ILT
2151 ++c;
2152
626f883f
ILT
2153 ret = (bfd_coff_filhsz (abfd)
2154 + bfd_coff_aoutsz (abfd)
2155 + c * bfd_coff_scnhsz (abfd));
2156 return BFD_ALIGN (ret, 16);
a7853216
ILT
2157}
2158
a7853216
ILT
2159/* Get the contents of a section. This is where we handle reading the
2160 .reginfo section, which implicitly holds the contents of an
2161 ecoff_reginfo structure. */
2162
2163boolean
2164ecoff_get_section_contents (abfd, section, location, offset, count)
2165 bfd *abfd;
2166 asection *section;
2167 PTR location;
2168 file_ptr offset;
2169 bfd_size_type count;
2170{
2171 ecoff_data_type *tdata = ecoff_data (abfd);
2172 struct ecoff_reginfo s;
2173 int i;
2174
2175 if (strcmp (section->name, REGINFO) != 0)
2176 return bfd_generic_get_section_contents (abfd, section, location,
2177 offset, count);
2178
2179 s.gp_value = tdata->gp;
2180 s.gprmask = tdata->gprmask;
2181 for (i = 0; i < 4; i++)
2182 s.cprmask[i] = tdata->cprmask[i];
2183 s.fprmask = tdata->fprmask;
2184
2185 /* bfd_get_section_contents has already checked that the offset and
2186 size is reasonable. We don't have to worry about swapping or any
2187 such thing; the .reginfo section is defined such that the
2188 contents are an ecoff_reginfo structure as seen on the host. */
4c3721d5 2189 memcpy (location, ((char *) &s) + offset, (size_t) count);
a7853216 2190 return true;
dae31cf5
ILT
2191}
2192
2193/* Calculate the file position for each section, and set
2194 reloc_filepos. */
2195
2196static void
2197ecoff_compute_section_file_positions (abfd)
2198 bfd *abfd;
2199{
2200 asection *current;
2201 file_ptr sofar;
2202 file_ptr old_sofar;
2203 boolean first_data;
2204
dae31cf5
ILT
2205 sofar = ecoff_sizeof_headers (abfd, false);
2206
2207 first_data = true;
2208 for (current = abfd->sections;
2209 current != (asection *) NULL;
2210 current = current->next)
2211 {
3f048f7f
ILT
2212 unsigned int alignment_power;
2213
dae31cf5 2214 /* Only deal with sections which have contents */
e544ed4f 2215 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) == 0
a7853216 2216 || strcmp (current->name, REGINFO) == 0)
dae31cf5
ILT
2217 continue;
2218
3f048f7f
ILT
2219 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2220 supposed to indicate the number of .pdata entries that are
2221 really in the section. Each entry is 8 bytes. We store this
2222 away in line_filepos before increasing the section size. */
2223 if (strcmp (current->name, _PDATA) != 0)
2224 alignment_power = current->alignment_power;
2225 else
2226 {
2227 current->line_filepos = current->_raw_size / 8;
2228 alignment_power = 4;
2229 }
2230
dae31cf5
ILT
2231 /* On Ultrix, the data sections in an executable file must be
2232 aligned to a page boundary within the file. This does not
2233 affect the section size, though. FIXME: Does this work for
c9668c58
ILT
2234 other platforms? It requires some modification for the
2235 Alpha, because .rdata on the Alpha goes with the text, not
2236 the data. */
dae31cf5
ILT
2237 if ((abfd->flags & EXEC_P) != 0
2238 && (abfd->flags & D_PAGED) != 0
2239 && first_data != false
c9668c58
ILT
2240 && (current->flags & SEC_CODE) == 0
2241 && (! ecoff_backend (abfd)->rdata_in_text
2242 || strcmp (current->name, _RDATA) != 0)
2243 && strcmp (current->name, _PDATA) != 0)
dae31cf5
ILT
2244 {
2245 const bfd_vma round = ecoff_backend (abfd)->round;
2246
2247 sofar = (sofar + round - 1) &~ (round - 1);
2248 first_data = false;
2249 }
2250
2251 /* Align the sections in the file to the same boundary on
2252 which they are aligned in virtual memory. */
2253 old_sofar = sofar;
3f048f7f 2254 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
dae31cf5
ILT
2255
2256 current->filepos = sofar;
2257
2258 sofar += current->_raw_size;
2259
2260 /* make sure that this section is of the right size too */
2261 old_sofar = sofar;
3f048f7f 2262 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
dae31cf5
ILT
2263 current->_raw_size += sofar - old_sofar;
2264 }
2265
2266 ecoff_data (abfd)->reloc_filepos = sofar;
2267}
2268
966e0a16 2269/* Determine the location of the relocs for all the sections in the
3f048f7f
ILT
2270 output file, as well as the location of the symbolic debugging
2271 information. */
966e0a16
ILT
2272
2273static bfd_size_type
2274ecoff_compute_reloc_file_positions (abfd)
2275 bfd *abfd;
2276{
2277 const bfd_size_type external_reloc_size =
2278 ecoff_backend (abfd)->external_reloc_size;
2279 file_ptr reloc_base;
2280 bfd_size_type reloc_size;
2281 asection *current;
3f048f7f 2282 file_ptr sym_base;
966e0a16
ILT
2283
2284 if (! abfd->output_has_begun)
3f048f7f
ILT
2285 {
2286 ecoff_compute_section_file_positions (abfd);
2287 abfd->output_has_begun = true;
2288 }
966e0a16
ILT
2289
2290 reloc_base = ecoff_data (abfd)->reloc_filepos;
2291
2292 reloc_size = 0;
2293 for (current = abfd->sections;
2294 current != (asection *)NULL;
2295 current = current->next)
2296 {
2297 if (strcmp (current->name, REGINFO) == 0)
2298 continue;
2299 if (current->reloc_count == 0)
2300 current->rel_filepos = 0;
2301 else
2302 {
2303 bfd_size_type relsize;
2304
2305 current->rel_filepos = reloc_base;
2306 relsize = current->reloc_count * external_reloc_size;
2307 reloc_size += relsize;
2308 reloc_base += relsize;
2309 }
2310 }
2311
3f048f7f
ILT
2312 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2313
2314 /* At least on Ultrix, the symbol table of an executable file must
2315 be aligned to a page boundary. FIXME: Is this true on other
2316 platforms? */
2317 if ((abfd->flags & EXEC_P) != 0
2318 && (abfd->flags & D_PAGED) != 0)
2319 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2320 &~ (ecoff_backend (abfd)->round - 1));
2321
2322 ecoff_data (abfd)->sym_filepos = sym_base;
2323
966e0a16
ILT
2324 return reloc_size;
2325}
2326
a7853216
ILT
2327/* Set the contents of a section. This is where we handle setting the
2328 contents of the .reginfo section, which implicitly holds a
2329 ecoff_reginfo structure. */
dae31cf5
ILT
2330
2331boolean
2332ecoff_set_section_contents (abfd, section, location, offset, count)
2333 bfd *abfd;
2334 asection *section;
2335 PTR location;
2336 file_ptr offset;
2337 bfd_size_type count;
2338{
966e0a16
ILT
2339 /* This must be done first, because bfd_set_section_contents is
2340 going to set output_has_begun to true. */
dae31cf5
ILT
2341 if (abfd->output_has_begun == false)
2342 ecoff_compute_section_file_positions (abfd);
2343
966e0a16
ILT
2344 if (count == 0)
2345 return true;
2346
a7853216
ILT
2347 if (strcmp (section->name, REGINFO) == 0)
2348 {
2349 ecoff_data_type *tdata = ecoff_data (abfd);
2350 struct ecoff_reginfo s;
2351 int i;
2352
2353 /* If the caller is only changing part of the structure, we must
2354 retrieve the current information before the memcpy. */
2355 if (offset != 0 || count != sizeof (struct ecoff_reginfo))
2356 {
2357 s.gp_value = tdata->gp;
2358 s.gprmask = tdata->gprmask;
2359 for (i = 0; i < 4; i++)
2360 s.cprmask[i] = tdata->cprmask[i];
2361 s.fprmask = tdata->fprmask;
2362 }
2363
2364 /* bfd_set_section_contents has already checked that the offset
2365 and size is reasonable. We don't have to worry about
2366 swapping or any such thing; the .reginfo section is defined
2367 such that the contents are an ecoff_reginfo structure as seen
2368 on the host. */
4c3721d5 2369 memcpy (((char *) &s) + offset, location, (size_t) count);
a7853216
ILT
2370
2371 tdata->gp = s.gp_value;
2372 tdata->gprmask = s.gprmask;
2373 for (i = 0; i < 4; i++)
2374 tdata->cprmask[i] = s.cprmask[i];
2375 tdata->fprmask = s.fprmask;
2376
2377 return true;
a7853216
ILT
2378 }
2379
966e0a16
ILT
2380 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2381 || bfd_write (location, 1, count, abfd) != count)
2382 return false;
dae31cf5
ILT
2383
2384 return true;
2385}
2386
966e0a16
ILT
2387/* Get ECOFF EXTR information for an external symbol. This function
2388 is passed to bfd_ecoff_debug_externals. */
dae31cf5 2389
966e0a16
ILT
2390static boolean
2391ecoff_get_extr (sym, esym)
2392 asymbol *sym;
2393 EXTR *esym;
dae31cf5 2394{
966e0a16
ILT
2395 ecoff_symbol_type *ecoff_sym_ptr;
2396 bfd *input_bfd;
2397
2398 /* Don't include debugging, local or section symbols. */
2399 if ((sym->flags & BSF_DEBUGGING) != 0
2400 || (sym->flags & BSF_LOCAL) != 0
2401 || (sym->flags & BSF_SECTION_SYM) != 0)
2402 return false;
2403
2404 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2405 || ecoffsymbol (sym)->native == NULL)
2406 {
2407 esym->jmptbl = 0;
2408 esym->cobol_main = 0;
2409 esym->weakext = 0;
2410 esym->reserved = 0;
2411 esym->ifd = ifdNil;
2412 /* FIXME: we can do better than this for st and sc. */
2413 esym->asym.st = stGlobal;
2414 esym->asym.sc = scAbs;
2415 esym->asym.reserved = 0;
2416 esym->asym.index = indexNil;
2417 return true;
2418 }
2419
2420 ecoff_sym_ptr = ecoffsymbol (sym);
2421
2422 if (ecoff_sym_ptr->local)
2423 abort ();
2424
2425 input_bfd = bfd_asymbol_bfd (sym);
2426 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2427 (input_bfd, ecoff_sym_ptr->native, esym);
2428
2429 /* If the symbol was defined by the linker, then esym will be
2430 undefined but sym will not be. Get a better class for such a
2431 symbol. */
2432 if ((esym->asym.sc == scUndefined
2433 || esym->asym.sc == scSUndefined)
2434 && bfd_get_section (sym) != &bfd_und_section)
2435 esym->asym.sc = scAbs;
2436
2437 /* Adjust the FDR index for the symbol by that used for the input
2438 BFD. */
3f048f7f
ILT
2439 if (esym->ifd != -1)
2440 {
2441 struct ecoff_debug_info *input_debug;
2442
2443 input_debug = &ecoff_data (input_bfd)->debug_info;
2444 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2445 if (input_debug->ifdmap != (RFDT *) NULL)
2446 esym->ifd = input_debug->ifdmap[esym->ifd];
2447 }
966e0a16
ILT
2448
2449 return true;
2450}
2451
2452/* Set the external symbol index. This routine is passed to
2453 bfd_ecoff_debug_externals. */
2454
2455static void
2456ecoff_set_index (sym, indx)
2457 asymbol *sym;
2458 bfd_size_type indx;
2459{
2460 ecoff_set_sym_index (sym, indx);
2461}
2462
2463/* Write out an ECOFF file. */
2464
2465boolean
2466ecoff_write_object_contents (abfd)
2467 bfd *abfd;
2468{
2469 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2470 const bfd_vma round = backend->round;
2471 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2472 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2473 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2474 const bfd_size_type external_hdr_size
2475 = backend->debug_swap.external_hdr_size;
2476 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2477 void (* const adjust_reloc_out) PARAMS ((bfd *,
2478 const arelent *,
2479 struct internal_reloc *))
c9668c58 2480 = backend->adjust_reloc_out;
dae31cf5
ILT
2481 void (* const swap_reloc_out) PARAMS ((bfd *,
2482 const struct internal_reloc *,
2483 PTR))
2484 = backend->swap_reloc_out;
8d12f138
ILT
2485 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2486 HDRR * const symhdr = &debug->symbolic_header;
dae31cf5
ILT
2487 asection *current;
2488 unsigned int count;
966e0a16 2489 bfd_size_type reloc_size;
22a71fef
ILT
2490 bfd_size_type text_size;
2491 bfd_vma text_start;
2492 bfd_size_type data_size;
2493 bfd_vma data_start;
2494 bfd_size_type bss_size;
80425e6c
JK
2495 PTR buff = NULL;
2496 PTR reloc_buff = NULL;
dae31cf5
ILT
2497 struct internal_filehdr internal_f;
2498 struct internal_aouthdr internal_a;
2499 int i;
2500
966e0a16
ILT
2501 /* Determine where the sections and relocs will go in the output
2502 file. */
2503 reloc_size = ecoff_compute_reloc_file_positions (abfd);
dae31cf5
ILT
2504
2505 count = 1;
dae31cf5
ILT
2506 for (current = abfd->sections;
2507 current != (asection *)NULL;
2508 current = current->next)
2509 {
8d12f138 2510 if (strcmp (current->name, REGINFO) == 0)
dae31cf5
ILT
2511 continue;
2512 current->target_index = count;
2513 ++count;
dae31cf5
ILT
2514 }
2515
dae31cf5
ILT
2516 if ((abfd->flags & D_PAGED) != 0)
2517 text_size = ecoff_sizeof_headers (abfd, false);
2518 else
2519 text_size = 0;
2520 text_start = 0;
2521 data_size = 0;
2522 data_start = 0;
2523 bss_size = 0;
2524
2525 /* Write section headers to the file. */
2526
80425e6c
JK
2527 /* Allocate buff big enough to hold a section header,
2528 file header, or a.out header. */
2529 {
2530 bfd_size_type siz;
2531 siz = scnhsz;
2532 if (siz < filhsz)
2533 siz = filhsz;
2534 if (siz < aoutsz)
2535 siz = aoutsz;
2536 buff = (PTR) malloc (siz);
2537 if (buff == NULL)
2538 {
2539 bfd_set_error (bfd_error_no_memory);
2540 goto error_return;
2541 }
2542 }
2543
dae31cf5
ILT
2544 internal_f.f_nscns = 0;
2545 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
80425e6c 2546 goto error_return;
dae31cf5
ILT
2547 for (current = abfd->sections;
2548 current != (asection *) NULL;
2549 current = current->next)
2550 {
2551 struct internal_scnhdr section;
2552 bfd_vma vma;
2553
a7853216
ILT
2554 if (strcmp (current->name, REGINFO) == 0)
2555 {
c9668c58 2556 BFD_ASSERT (current->reloc_count == 0);
a7853216
ILT
2557 continue;
2558 }
dae31cf5
ILT
2559
2560 ++internal_f.f_nscns;
2561
2562 strncpy (section.s_name, current->name, sizeof section.s_name);
2563
2564 /* FIXME: is this correct for shared libraries? I think it is
2565 but I have no platform to check. Ian Lance Taylor. */
2566 vma = bfd_get_section_vma (abfd, current);
2567 if (strcmp (current->name, _LIB) == 0)
2568 section.s_vaddr = 0;
2569 else
2570 section.s_vaddr = vma;
2571
2572 section.s_paddr = vma;
2573 section.s_size = bfd_get_section_size_before_reloc (current);
2574
e544ed4f
ILT
2575 /* If this section is unloadable then the scnptr will be 0. */
2576 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
dae31cf5
ILT
2577 section.s_scnptr = 0;
2578 else
2579 section.s_scnptr = current->filepos;
2580 section.s_relptr = current->rel_filepos;
2581
2582 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2583 object file produced by the assembler is supposed to point to
2584 information about how much room is required by objects of
2585 various different sizes. I think this only matters if we
2586 want the linker to compute the best size to use, or
2587 something. I don't know what happens if the information is
2588 not present. */
3f048f7f
ILT
2589 if (strcmp (current->name, _PDATA) != 0)
2590 section.s_lnnoptr = 0;
2591 else
2592 {
2593 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2594 hold the number of entries in the section (each entry is
2595 8 bytes). We stored this in the line_filepos field in
2596 ecoff_compute_section_file_positions. */
2597 section.s_lnnoptr = current->line_filepos;
2598 }
dae31cf5
ILT
2599
2600 section.s_nreloc = current->reloc_count;
2601 section.s_nlnno = 0;
2602 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2603 current->flags);
2604
2605 bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff);
2606 if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
80425e6c 2607 goto error_return;
dae31cf5 2608
c9668c58
ILT
2609 if ((section.s_flags & STYP_TEXT) != 0
2610 || ((section.s_flags & STYP_RDATA) != 0
2611 && backend->rdata_in_text)
2612 || strcmp (current->name, _PDATA) == 0)
dae31cf5
ILT
2613 {
2614 text_size += bfd_get_section_size_before_reloc (current);
2615 if (text_start == 0 || text_start > vma)
2616 text_start = vma;
2617 }
2618 else if ((section.s_flags & STYP_RDATA) != 0
2619 || (section.s_flags & STYP_DATA) != 0
c9668c58 2620 || (section.s_flags & STYP_LITA) != 0
dae31cf5
ILT
2621 || (section.s_flags & STYP_LIT8) != 0
2622 || (section.s_flags & STYP_LIT4) != 0
966e0a16
ILT
2623 || (section.s_flags & STYP_SDATA) != 0
2624 || strcmp (current->name, _XDATA) == 0)
dae31cf5
ILT
2625 {
2626 data_size += bfd_get_section_size_before_reloc (current);
2627 if (data_start == 0 || data_start > vma)
2628 data_start = vma;
2629 }
2630 else if ((section.s_flags & STYP_BSS) != 0
2631 || (section.s_flags & STYP_SBSS) != 0)
2632 bss_size += bfd_get_section_size_before_reloc (current);
966e0a16
ILT
2633 else
2634 abort ();
dae31cf5
ILT
2635 }
2636
2637 /* Set up the file header. */
2638
a7853216 2639 internal_f.f_magic = ecoff_get_magic (abfd);
dae31cf5
ILT
2640
2641 /* We will NOT put a fucking timestamp in the header here. Every
2642 time you put it back, I will come in and take it out again. I'm
2643 sorry. This field does not belong here. We fill it with a 0 so
2644 it compares the same but is not a reasonable time. --
2645 gnu@cygnus.com. */
2646 internal_f.f_timdat = 0;
2647
2648 if (bfd_get_symcount (abfd) != 0)
2649 {
2650 /* The ECOFF f_nsyms field is not actually the number of
2651 symbols, it's the size of symbolic information header. */
2652 internal_f.f_nsyms = external_hdr_size;
3f048f7f 2653 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
dae31cf5
ILT
2654 }
2655 else
2656 {
2657 internal_f.f_nsyms = 0;
2658 internal_f.f_symptr = 0;
2659 }
2660
2661 internal_f.f_opthdr = aoutsz;
2662
2663 internal_f.f_flags = F_LNNO;
2664 if (reloc_size == 0)
2665 internal_f.f_flags |= F_RELFLG;
2666 if (bfd_get_symcount (abfd) == 0)
2667 internal_f.f_flags |= F_LSYMS;
2668 if (abfd->flags & EXEC_P)
2669 internal_f.f_flags |= F_EXEC;
2670
2671 if (! abfd->xvec->byteorder_big_p)
2672 internal_f.f_flags |= F_AR32WR;
2673 else
2674 internal_f.f_flags |= F_AR32W;
2675
2676 /* Set up the ``optional'' header. */
2677 if ((abfd->flags & D_PAGED) != 0)
2678 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2679 else
2680 internal_a.magic = ECOFF_AOUT_OMAGIC;
2681
3f048f7f
ILT
2682 /* FIXME: Is this really correct? */
2683 internal_a.vstamp = symhdr->vstamp;
dae31cf5
ILT
2684
2685 /* At least on Ultrix, these have to be rounded to page boundaries.
2686 FIXME: Is this true on other platforms? */
2687 if ((abfd->flags & D_PAGED) != 0)
2688 {
2689 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2690 internal_a.text_start = text_start &~ (round - 1);
2691 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2692 internal_a.data_start = data_start &~ (round - 1);
2693 }
2694 else
2695 {
2696 internal_a.tsize = text_size;
2697 internal_a.text_start = text_start;
2698 internal_a.dsize = data_size;
2699 internal_a.data_start = data_start;
2700 }
2701
2702 /* On Ultrix, the initial portions of the .sbss and .bss segments
2703 are at the end of the data section. The bsize field in the
2704 optional header records how many bss bytes are required beyond
2705 those in the data section. The value is not rounded to a page
2706 boundary. */
2707 if (bss_size < internal_a.dsize - data_size)
2708 bss_size = 0;
2709 else
2710 bss_size -= internal_a.dsize - data_size;
2711 internal_a.bsize = bss_size;
2712 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2713
2714 internal_a.entry = bfd_get_start_address (abfd);
2715
2716 internal_a.gp_value = ecoff_data (abfd)->gp;
2717
2718 internal_a.gprmask = ecoff_data (abfd)->gprmask;
48edba81 2719 internal_a.fprmask = ecoff_data (abfd)->fprmask;
dae31cf5
ILT
2720 for (i = 0; i < 4; i++)
2721 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2722
2723 /* Write out the file header and the optional header. */
2724
2725 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
80425e6c 2726 goto error_return;
dae31cf5 2727
dae31cf5
ILT
2728 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2729 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
80425e6c 2730 goto error_return;
dae31cf5 2731
dae31cf5
ILT
2732 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2733 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
80425e6c 2734 goto error_return;
dae31cf5 2735
8d12f138 2736 /* Build the external symbol information. This must be done before
966e0a16
ILT
2737 writing out the relocs so that we know the symbol indices. The
2738 condition checks makes sure this object was not created by
2739 ecoff_bfd_final_link, since if it was we do not want to tamper
2740 with the external symbols. */
3f048f7f 2741 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL)
dae31cf5 2742 {
966e0a16
ILT
2743 symhdr->iextMax = 0;
2744 symhdr->issExtMax = 0;
2745 debug->external_ext = debug->external_ext_end = NULL;
2746 debug->ssext = debug->ssext_end = NULL;
2747 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2748 (((abfd->flags & EXEC_P) == 0)
2749 ? true : false),
2750 ecoff_get_extr, ecoff_set_index)
2751 == false)
80425e6c 2752 goto error_return;
dae31cf5 2753
966e0a16
ILT
2754 /* Write out the relocs. */
2755 for (current = abfd->sections;
2756 current != (asection *) NULL;
2757 current = current->next)
dae31cf5 2758 {
966e0a16
ILT
2759 arelent **reloc_ptr_ptr;
2760 arelent **reloc_end;
2761 char *out_ptr;
dae31cf5 2762
966e0a16
ILT
2763 if (current->reloc_count == 0)
2764 continue;
dae31cf5 2765
80425e6c
JK
2766 reloc_buff =
2767 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2768 if (reloc_buff == NULL)
dae31cf5 2769 {
d1ad85a6 2770 bfd_set_error (bfd_error_no_memory);
80425e6c 2771 goto error_return;
dae31cf5 2772 }
966e0a16
ILT
2773
2774 reloc_ptr_ptr = current->orelocation;
2775 reloc_end = reloc_ptr_ptr + current->reloc_count;
80425e6c 2776 out_ptr = (char *) reloc_buff;
966e0a16
ILT
2777 for (;
2778 reloc_ptr_ptr < reloc_end;
2779 reloc_ptr_ptr++, out_ptr += external_reloc_size)
dae31cf5 2780 {
966e0a16
ILT
2781 arelent *reloc;
2782 asymbol *sym;
2783 struct internal_reloc in;
2784
68241b2b 2785 memset ((PTR) &in, 0, sizeof in);
966e0a16
ILT
2786
2787 reloc = *reloc_ptr_ptr;
2788 sym = *reloc->sym_ptr_ptr;
2789
2790 in.r_vaddr = (reloc->address
2791 + bfd_get_section_vma (abfd, current));
2792 in.r_type = reloc->howto->type;
2793
2794 if ((sym->flags & BSF_SECTION_SYM) == 0)
2795 {
2796 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2797 in.r_extern = 1;
2798 }
dae31cf5 2799 else
966e0a16
ILT
2800 {
2801 CONST char *name;
2802
2803 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2804 if (strcmp (name, ".text") == 0)
2805 in.r_symndx = RELOC_SECTION_TEXT;
2806 else if (strcmp (name, ".rdata") == 0)
2807 in.r_symndx = RELOC_SECTION_RDATA;
2808 else if (strcmp (name, ".data") == 0)
2809 in.r_symndx = RELOC_SECTION_DATA;
2810 else if (strcmp (name, ".sdata") == 0)
2811 in.r_symndx = RELOC_SECTION_SDATA;
2812 else if (strcmp (name, ".sbss") == 0)
2813 in.r_symndx = RELOC_SECTION_SBSS;
2814 else if (strcmp (name, ".bss") == 0)
2815 in.r_symndx = RELOC_SECTION_BSS;
2816 else if (strcmp (name, ".init") == 0)
2817 in.r_symndx = RELOC_SECTION_INIT;
2818 else if (strcmp (name, ".lit8") == 0)
2819 in.r_symndx = RELOC_SECTION_LIT8;
2820 else if (strcmp (name, ".lit4") == 0)
2821 in.r_symndx = RELOC_SECTION_LIT4;
2822 else if (strcmp (name, ".xdata") == 0)
2823 in.r_symndx = RELOC_SECTION_XDATA;
2824 else if (strcmp (name, ".pdata") == 0)
2825 in.r_symndx = RELOC_SECTION_PDATA;
2826 else if (strcmp (name, ".fini") == 0)
2827 in.r_symndx = RELOC_SECTION_FINI;
2828 else if (strcmp (name, ".lita") == 0)
2829 in.r_symndx = RELOC_SECTION_LITA;
2830 else if (strcmp (name, "*ABS*") == 0)
2831 in.r_symndx = RELOC_SECTION_ABS;
2832 else
2833 abort ();
2834 in.r_extern = 0;
2835 }
2836
2837 (*adjust_reloc_out) (abfd, reloc, &in);
2838
2839 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
dae31cf5
ILT
2840 }
2841
966e0a16 2842 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
80425e6c
JK
2843 goto error_return;
2844 if (bfd_write (reloc_buff,
2845 external_reloc_size, current->reloc_count, abfd)
966e0a16 2846 != external_reloc_size * current->reloc_count)
80425e6c
JK
2847 goto error_return;
2848 bfd_release (abfd, reloc_buff);
2849 reloc_buff = NULL;
dae31cf5 2850 }
dae31cf5 2851
3f048f7f
ILT
2852 /* Write out the symbolic debugging information. */
2853 if (bfd_get_symcount (abfd) > 0)
2854 {
2855 /* Write out the debugging information. */
2856 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2857 ecoff_data (abfd)->sym_filepos)
2858 == false)
80425e6c 2859 goto error_return;
3f048f7f 2860 }
626f883f 2861 }
dae31cf5 2862
626f883f
ILT
2863 /* The .bss section of a demand paged executable must receive an
2864 entire page. If there are symbols, the symbols will start on the
2865 next page. If there are no symbols, we must fill out the page by
2866 hand. */
2867 if (bfd_get_symcount (abfd) == 0
2868 && (abfd->flags & EXEC_P) != 0
2869 && (abfd->flags & D_PAGED) != 0)
2870 {
2871 char c;
2872
2873 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2874 SEEK_SET) != 0)
80425e6c 2875 goto error_return;
626f883f
ILT
2876 if (bfd_read (&c, 1, 1, abfd) == 0)
2877 c = 0;
2878 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2879 SEEK_SET) != 0)
80425e6c 2880 goto error_return;
626f883f 2881 if (bfd_write (&c, 1, 1, abfd) != 1)
80425e6c 2882 goto error_return;
dae31cf5
ILT
2883 }
2884
80425e6c
JK
2885 if (reloc_buff != NULL)
2886 bfd_release (abfd, reloc_buff);
2887 if (buff != NULL)
2888 free (buff);
dae31cf5 2889 return true;
80425e6c
JK
2890 error_return:
2891 if (reloc_buff != NULL)
2892 bfd_release (abfd, reloc_buff);
2893 if (buff != NULL)
2894 free (buff);
2895 return false;
dae31cf5
ILT
2896}
2897\f
2898/* Archive handling. ECOFF uses what appears to be a unique type of
b59f0276
ILT
2899 archive header (armap). The byte ordering of the armap and the
2900 contents are encoded in the name of the armap itself. At least for
2901 now, we only support archives with the same byte ordering in the
2902 armap and the contents.
dae31cf5
ILT
2903
2904 The first four bytes in the armap are the number of symbol
2905 definitions. This is always a power of two.
2906
2907 This is followed by the symbol definitions. Each symbol definition
2908 occupies 8 bytes. The first four bytes are the offset from the
2909 start of the armap strings to the null-terminated string naming
2910 this symbol. The second four bytes are the file offset to the
2911 archive member which defines this symbol. If the second four bytes
2912 are 0, then this is not actually a symbol definition, and it should
2913 be ignored.
2914
2915 The symbols are hashed into the armap with a closed hashing scheme.
2916 See the functions below for the details of the algorithm.
2917
dae31cf5
ILT
2918 After the symbol definitions comes four bytes holding the size of
2919 the string table, followed by the string table itself. */
2920
2921/* The name of an archive headers looks like this:
2922 __________E[BL]E[BL]_ (with a trailing space).
2923 The trailing space is changed to an X if the archive is changed to
48edba81
ILT
2924 indicate that the armap is out of date.
2925
2926 The Alpha seems to use ________64E[BL]E[BL]_. */
dae31cf5
ILT
2927
2928#define ARMAP_BIG_ENDIAN 'B'
2929#define ARMAP_LITTLE_ENDIAN 'L'
2930#define ARMAP_MARKER 'E'
48edba81 2931#define ARMAP_START_LENGTH 10
dae31cf5
ILT
2932#define ARMAP_HEADER_MARKER_INDEX 10
2933#define ARMAP_HEADER_ENDIAN_INDEX 11
2934#define ARMAP_OBJECT_MARKER_INDEX 12
2935#define ARMAP_OBJECT_ENDIAN_INDEX 13
2936#define ARMAP_END_INDEX 14
2937#define ARMAP_END "_ "
2938
2939/* This is a magic number used in the hashing algorithm. */
2940#define ARMAP_HASH_MAGIC 0x9dd68ab5
2941
2942/* This returns the hash value to use for a string. It also sets
2943 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2944 is the number of entries in the hash table, and HLOG is the log
2945 base 2 of SIZE. */
2946
2947static unsigned int
2948ecoff_armap_hash (s, rehash, size, hlog)
2949 CONST char *s;
2950 unsigned int *rehash;
2951 unsigned int size;
2952 unsigned int hlog;
2953{
2954 unsigned int hash;
2955
2956 hash = *s++;
2957 while (*s != '\0')
2958 hash = ((hash >> 27) | (hash << 5)) + *s++;
2959 hash *= ARMAP_HASH_MAGIC;
2960 *rehash = (hash & (size - 1)) | 1;
2961 return hash >> (32 - hlog);
2962}
2963
2964/* Read in the armap. */
2965
2966boolean
2967ecoff_slurp_armap (abfd)
2968 bfd *abfd;
2969{
2970 char nextname[17];
2971 unsigned int i;
2972 struct areltdata *mapdata;
2973 bfd_size_type parsed_size;
2974 char *raw_armap;
2975 struct artdata *ardata;
2976 unsigned int count;
2977 char *raw_ptr;
2978 struct symdef *symdef_ptr;
2979 char *stringbase;
2980
2981 /* Get the name of the first element. */
2982 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2983 if (i == 0)
2984 return true;
2985 if (i != 16)
2986 return false;
2987
2988 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
2989
8c11363a
ILT
2990 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2991 standard COFF armap. We could move the ECOFF armap stuff into
2992 bfd_slurp_armap, but that seems inappropriate since no other
2993 target uses this format. Instead, we check directly for a COFF
2994 armap. */
2995 if (strncmp (nextname, "/ ", 16) == 0)
2996 return bfd_slurp_armap (abfd);
2997
dae31cf5 2998 /* See if the first element is an armap. */
48edba81
ILT
2999 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
3000 ARMAP_START_LENGTH) != 0
dae31cf5
ILT
3001 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3002 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3003 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3004 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3005 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3006 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3007 || strncmp (nextname + ARMAP_END_INDEX,
3008 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3009 {
3010 bfd_has_map (abfd) = false;
3011 return true;
3012 }
3013
3014 /* Make sure we have the right byte ordering. */
3015 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3016 ^ (abfd->xvec->header_byteorder_big_p != false))
3017 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3018 ^ (abfd->xvec->byteorder_big_p != false)))
3019 {
d1ad85a6 3020 bfd_set_error (bfd_error_wrong_format);
dae31cf5
ILT
3021 return false;
3022 }
3023
3024 /* Read in the armap. */
3025 ardata = bfd_ardata (abfd);
b59f0276 3026 mapdata = _bfd_snarf_ar_hdr (abfd);
dae31cf5
ILT
3027 if (mapdata == (struct areltdata *) NULL)
3028 return false;
3029 parsed_size = mapdata->parsed_size;
3030 bfd_release (abfd, (PTR) mapdata);
3031
3032 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3033 if (raw_armap == (char *) NULL)
3034 {
d1ad85a6 3035 bfd_set_error (bfd_error_no_memory);
dae31cf5
ILT
3036 return false;
3037 }
3038
3039 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3040 {
d1ad85a6 3041 bfd_set_error (bfd_error_malformed_archive);
dae31cf5
ILT
3042 bfd_release (abfd, (PTR) raw_armap);
3043 return false;
3044 }
3045
b59f0276
ILT
3046 ardata->tdata = (PTR) raw_armap;
3047
dae31cf5
ILT
3048 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3049
3050 ardata->symdef_count = 0;
3051 ardata->cache = (struct ar_cache *) NULL;
3052
48edba81
ILT
3053 /* This code used to overlay the symdefs over the raw archive data,
3054 but that doesn't work on a 64 bit host. */
3055
e544ed4f 3056 stringbase = raw_armap + count * 8 + 8;
dae31cf5
ILT
3057
3058#ifdef CHECK_ARMAP_HASH
3059 {
3060 unsigned int hlog;
3061
3062 /* Double check that I have the hashing algorithm right by making
3063 sure that every symbol can be looked up successfully. */
3064 hlog = 0;
3065 for (i = 1; i < count; i <<= 1)
3066 hlog++;
3067 BFD_ASSERT (i == count);
3068
e544ed4f
ILT
3069 raw_ptr = raw_armap + 4;
3070 for (i = 0; i < count; i++, raw_ptr += 8)
dae31cf5
ILT
3071 {
3072 unsigned int name_offset, file_offset;
3073 unsigned int hash, rehash, srch;
3074
3075 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
e544ed4f 3076 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
dae31cf5
ILT
3077 if (file_offset == 0)
3078 continue;
3079 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3080 hlog);
3081 if (hash == i)
3082 continue;
3083
3084 /* See if we can rehash to this location. */
3085 for (srch = (hash + rehash) & (count - 1);
3086 srch != hash && srch != i;
3087 srch = (srch + rehash) & (count - 1))
e544ed4f 3088 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
dae31cf5
ILT
3089 != 0);
3090 BFD_ASSERT (srch == i);
3091 }
3092 }
3093
dae31cf5
ILT
3094#endif /* CHECK_ARMAP_HASH */
3095
e544ed4f
ILT
3096 raw_ptr = raw_armap + 4;
3097 for (i = 0; i < count; i++, raw_ptr += 8)
3098 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
48edba81
ILT
3099 ++ardata->symdef_count;
3100
3101 symdef_ptr = ((struct symdef *)
3102 bfd_alloc (abfd,
3103 ardata->symdef_count * sizeof (struct symdef)));
9783e04a
DM
3104 if (!symdef_ptr)
3105 {
d1ad85a6 3106 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3107 return false;
3108 }
3109
48edba81
ILT
3110 ardata->symdefs = (carsym *) symdef_ptr;
3111
e544ed4f
ILT
3112 raw_ptr = raw_armap + 4;
3113 for (i = 0; i < count; i++, raw_ptr += 8)
dae31cf5
ILT
3114 {
3115 unsigned int name_offset, file_offset;
3116
e544ed4f 3117 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
dae31cf5
ILT
3118 if (file_offset == 0)
3119 continue;
48edba81 3120 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
dae31cf5
ILT
3121 symdef_ptr->s.name = stringbase + name_offset;
3122 symdef_ptr->file_offset = file_offset;
3123 ++symdef_ptr;
dae31cf5
ILT
3124 }
3125
3126 ardata->first_file_filepos = bfd_tell (abfd);
3127 /* Pad to an even boundary. */
3128 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3129
3130 bfd_has_map (abfd) = true;
3131
3132 return true;
3133}
3134
3135/* Write out an armap. */
3136
3137boolean
3138ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3139 bfd *abfd;
3140 unsigned int elength;
3141 struct orl *map;
3142 unsigned int orl_count;
3143 int stridx;
3144{
3145 unsigned int hashsize, hashlog;
3146 unsigned int symdefsize;
3147 int padit;
3148 unsigned int stringsize;
3149 unsigned int mapsize;
3150 file_ptr firstreal;
3151 struct ar_hdr hdr;
3152 struct stat statbuf;
3153 unsigned int i;
e544ed4f 3154 bfd_byte temp[4];
dae31cf5
ILT
3155 bfd_byte *hashtable;
3156 bfd *current;
3157 bfd *last_elt;
3158
3159 /* Ultrix appears to use as a hash table size the least power of two
3160 greater than twice the number of entries. */
3161 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3162 ;
3163 hashsize = 1 << hashlog;
3164
e544ed4f 3165 symdefsize = hashsize * 8;
dae31cf5
ILT
3166 padit = stridx % 2;
3167 stringsize = stridx + padit;
3168
3169 /* Include 8 bytes to store symdefsize and stringsize in output. */
e544ed4f 3170 mapsize = symdefsize + stringsize + 8;
dae31cf5
ILT
3171
3172 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3173
3174 memset ((PTR) &hdr, 0, sizeof hdr);
3175
3176 /* Work out the ECOFF armap name. */
48edba81 3177 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
dae31cf5
ILT
3178 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3179 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3180 (abfd->xvec->header_byteorder_big_p
3181 ? ARMAP_BIG_ENDIAN
3182 : ARMAP_LITTLE_ENDIAN);
3183 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3184 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3185 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3186 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3187
3188 /* Write the timestamp of the archive header to be just a little bit
3189 later than the timestamp of the file, otherwise the linker will
3190 complain that the index is out of date. Actually, the Ultrix
3191 linker just checks the archive name; the GNU linker may check the
3192 date. */
3193 stat (abfd->filename, &statbuf);
3194 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3195
3196 /* The DECstation uses zeroes for the uid, gid and mode of the
3197 armap. */
3198 hdr.ar_uid[0] = '0';
3199 hdr.ar_gid[0] = '0';
3200 hdr.ar_mode[0] = '0';
3201
3202 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3203
3204 hdr.ar_fmag[0] = '`';
9a793780 3205 hdr.ar_fmag[1] = '\012';
dae31cf5
ILT
3206
3207 /* Turn all null bytes in the header into spaces. */
3208 for (i = 0; i < sizeof (struct ar_hdr); i++)
3209 if (((char *)(&hdr))[i] == '\0')
3210 (((char *)(&hdr))[i]) = ' ';
3211
3212 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3213 != sizeof (struct ar_hdr))
3214 return false;
3215
4c3721d5 3216 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
728472f1 3217 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
dae31cf5
ILT
3218 return false;
3219
3220 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
9783e04a
DM
3221 if (!hashtable)
3222 {
d1ad85a6 3223 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3224 return false;
3225 }
dae31cf5
ILT
3226
3227 current = abfd->archive_head;
3228 last_elt = current;
3229 for (i = 0; i < orl_count; i++)
3230 {
3231 unsigned int hash, rehash;
3232
3233 /* Advance firstreal to the file position of this archive
3234 element. */
3235 if (((bfd *) map[i].pos) != last_elt)
3236 {
3237 do
3238 {
3239 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3240 firstreal += firstreal % 2;
3241 current = current->next;
3242 }
3243 while (current != (bfd *) map[i].pos);
3244 }
3245
3246 last_elt = current;
3247
3248 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
e544ed4f 3249 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
dae31cf5
ILT
3250 {
3251 unsigned int srch;
3252
3253 /* The desired slot is already taken. */
3254 for (srch = (hash + rehash) & (hashsize - 1);
3255 srch != hash;
3256 srch = (srch + rehash) & (hashsize - 1))
e544ed4f 3257 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
dae31cf5
ILT
3258 break;
3259
3260 BFD_ASSERT (srch != hash);
3261
3262 hash = srch;
3263 }
3264
4c3721d5
ILT
3265 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3266 (PTR) (hashtable + hash * 8));
3267 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3268 (PTR) (hashtable + hash * 8 + 4));
dae31cf5
ILT
3269 }
3270
728472f1 3271 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
dae31cf5
ILT
3272 return false;
3273
3274 bfd_release (abfd, hashtable);
3275
3276 /* Now write the strings. */
4c3721d5 3277 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
728472f1 3278 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
dae31cf5
ILT
3279 return false;
3280 for (i = 0; i < orl_count; i++)
3281 {
3282 bfd_size_type len;
3283
3284 len = strlen (*map[i].name) + 1;
3285 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3286 return false;
3287 }
3288
3289 /* The spec sez this should be a newline. But in order to be
3290 bug-compatible for DECstation ar we use a null. */
3291 if (padit)
3292 {
728472f1 3293 if (bfd_write ("", 1, 1, abfd) != 1)
dae31cf5
ILT
3294 return false;
3295 }
3296
3297 return true;
3298}
3299
3300/* See whether this BFD is an archive. If it is, read in the armap
3301 and the extended name table. */
3302
3303bfd_target *
3304ecoff_archive_p (abfd)
3305 bfd *abfd;
3306{
3307 char armag[SARMAG + 1];
3308
3309 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3310 || strncmp (armag, ARMAG, SARMAG) != 0)
3311 {
d1ad85a6 3312 bfd_set_error (bfd_error_wrong_format);
dae31cf5
ILT
3313 return (bfd_target *) NULL;
3314 }
3315
3316 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3317 involves a cast, we can't do it as the left operand of
3318 assignment. */
3319 abfd->tdata.aout_ar_data =
3320 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3321
3322 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3323 {
d1ad85a6 3324 bfd_set_error (bfd_error_no_memory);
dae31cf5
ILT
3325 return (bfd_target *) NULL;
3326 }
3327
3328 bfd_ardata (abfd)->first_file_filepos = SARMAG;
b59f0276
ILT
3329 bfd_ardata (abfd)->cache = NULL;
3330 bfd_ardata (abfd)->archive_head = NULL;
3331 bfd_ardata (abfd)->symdefs = NULL;
3332 bfd_ardata (abfd)->extended_names = NULL;
3333 bfd_ardata (abfd)->tdata = NULL;
dae31cf5
ILT
3334
3335 if (ecoff_slurp_armap (abfd) == false
3336 || ecoff_slurp_extended_name_table (abfd) == false)
3337 {
3338 bfd_release (abfd, bfd_ardata (abfd));
3339 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3340 return (bfd_target *) NULL;
3341 }
3342
3343 return abfd->xvec;
3344}
966e0a16
ILT
3345\f
3346/* ECOFF linker code. */
3347
3348static struct bfd_hash_entry *ecoff_link_hash_newfunc
3349 PARAMS ((struct bfd_hash_entry *entry,
3350 struct bfd_hash_table *table,
3351 const char *string));
3352static boolean ecoff_link_add_archive_symbols
3353 PARAMS ((bfd *, struct bfd_link_info *));
3354static boolean ecoff_link_check_archive_element
3355 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3356static boolean ecoff_link_add_object_symbols
3357 PARAMS ((bfd *, struct bfd_link_info *));
3358static boolean ecoff_link_add_externals
3359 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3360
3361/* Routine to create an entry in an ECOFF link hash table. */
3362
3363static struct bfd_hash_entry *
3364ecoff_link_hash_newfunc (entry, table, string)
3365 struct bfd_hash_entry *entry;
3366 struct bfd_hash_table *table;
3367 const char *string;
3368{
3369 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3370
3371 /* Allocate the structure if it has not already been allocated by a
3372 subclass. */
3373 if (ret == (struct ecoff_link_hash_entry *) NULL)
3374 ret = ((struct ecoff_link_hash_entry *)
3375 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
9783e04a
DM
3376 if (ret == (struct ecoff_link_hash_entry *) NULL)
3377 {
d1ad85a6 3378 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3379 return NULL;
3380 }
966e0a16
ILT
3381
3382 /* Call the allocation method of the superclass. */
3383 ret = ((struct ecoff_link_hash_entry *)
3384 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3385 table, string));
3386
9783e04a
DM
3387 if (ret)
3388 {
3389 /* Set local fields. */
3390 ret->indx = -1;
3391 ret->abfd = NULL;
3392 }
68241b2b 3393 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
966e0a16
ILT
3394
3395 return (struct bfd_hash_entry *) ret;
3396}
3397
3398/* Create an ECOFF link hash table. */
3399
3400struct bfd_link_hash_table *
3401ecoff_bfd_link_hash_table_create (abfd)
3402 bfd *abfd;
3403{
3404 struct ecoff_link_hash_table *ret;
3405
3406 ret = ((struct ecoff_link_hash_table *)
9a793780
SS
3407 malloc (sizeof (struct ecoff_link_hash_table)));
3408 if (!ret)
3409 {
d1ad85a6 3410 bfd_set_error (bfd_error_no_memory);
9a793780
SS
3411 return NULL;
3412 }
966e0a16
ILT
3413 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3414 ecoff_link_hash_newfunc))
3415 {
3416 free (ret);
3417 return (struct bfd_link_hash_table *) NULL;
3418 }
3419 return &ret->root;
3420}
3421
3422/* Look up an entry in an ECOFF link hash table. */
3423
3424#define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3425 ((struct ecoff_link_hash_entry *) \
3426 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3427
3428/* Traverse an ECOFF link hash table. */
3429
3430#define ecoff_link_hash_traverse(table, func, info) \
3431 (bfd_link_hash_traverse \
3432 (&(table)->root, \
3433 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3434 (info)))
3435
3436/* Get the ECOFF link hash table from the info structure. This is
3437 just a cast. */
3438
3439#define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3440
3441/* Given an ECOFF BFD, add symbols to the global hash table as
3442 appropriate. */
3443
3444boolean
3445ecoff_bfd_link_add_symbols (abfd, info)
3446 bfd *abfd;
3447 struct bfd_link_info *info;
3448{
3449 switch (bfd_get_format (abfd))
3450 {
3451 case bfd_object:
3452 return ecoff_link_add_object_symbols (abfd, info);
3453 case bfd_archive:
3454 return ecoff_link_add_archive_symbols (abfd, info);
3455 default:
d1ad85a6 3456 bfd_set_error (bfd_error_wrong_format);
966e0a16
ILT
3457 return false;
3458 }
3459}
3460
3461/* Add the symbols from an archive file to the global hash table.
3462 This looks through the undefined symbols, looks each one up in the
3463 archive hash table, and adds any associated object file. We do not
3464 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3465 already have a hash table, so there is no reason to construct
3466 another one. */
3467
3468static boolean
3469ecoff_link_add_archive_symbols (abfd, info)
3470 bfd *abfd;
3471 struct bfd_link_info *info;
3472{
3473 const bfd_byte *raw_armap;
3474 struct bfd_link_hash_entry **pundef;
3475 unsigned int armap_count;
3476 unsigned int armap_log;
3477 unsigned int i;
3478 const bfd_byte *hashtable;
3479 const char *stringbase;
3480
3481 if (! bfd_has_map (abfd))
3482 {
d1ad85a6 3483 bfd_set_error (bfd_error_no_symbols);
966e0a16
ILT
3484 return false;
3485 }
3486
3487 /* If we don't have any raw data for this archive, as can happen on
3488 Irix 4.0.5F, we call the generic routine.
3489 FIXME: We should be more clever about this, since someday tdata
3490 may get to something for a generic archive. */
3491 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3492 if (raw_armap == (bfd_byte *) NULL)
3493 return (_bfd_generic_link_add_archive_symbols
3494 (abfd, info, ecoff_link_check_archive_element));
3495
3496 armap_count = bfd_h_get_32 (abfd, raw_armap);
3497
3498 armap_log = 0;
3499 for (i = 1; i < armap_count; i <<= 1)
3500 armap_log++;
3501 BFD_ASSERT (i == armap_count);
3502
3503 hashtable = raw_armap + 4;
3504 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3505
3506 /* Look through the list of undefined symbols. */
3507 pundef = &info->hash->undefs;
3508 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3509 {
3510 struct bfd_link_hash_entry *h;
3511 unsigned int hash, rehash;
3512 unsigned int file_offset;
3513 const char *name;
3514 bfd *element;
3515
3516 h = *pundef;
3517
3518 /* When a symbol is defined, it is not necessarily removed from
3519 the list. */
3520 if (h->type != bfd_link_hash_undefined
3521 && h->type != bfd_link_hash_common)
3522 {
3523 /* Remove this entry from the list, for general cleanliness
3524 and because we are going to look through the list again
3525 if we search any more libraries. We can't remove the
3526 entry if it is the tail, because that would lose any
3527 entries we add to the list later on. */
3528 if (*pundef != info->hash->undefs_tail)
3529 *pundef = (*pundef)->next;
3530 else
3531 pundef = &(*pundef)->next;
3532 continue;
3533 }
3534
3535 /* Native ECOFF linkers do not pull in archive elements merely
3536 to satisfy common definitions, so neither do we. We leave
3537 them on the list, though, in case we are linking against some
3538 other object format. */
3539 if (h->type != bfd_link_hash_undefined)
3540 {
3541 pundef = &(*pundef)->next;
3542 continue;
3543 }
3544
3545 /* Look for this symbol in the archive hash table. */
3546 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3547 armap_log);
3548
3549 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3550 if (file_offset == 0)
3551 {
3552 /* Nothing in this slot. */
3553 pundef = &(*pundef)->next;
3554 continue;
3555 }
3556
3557 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3558 if (name[0] != h->root.string[0]
3559 || strcmp (name, h->root.string) != 0)
3560 {
3561 unsigned int srch;
3562 boolean found;
3563
3564 /* That was the wrong symbol. Try rehashing. */
3565 found = false;
3566 for (srch = (hash + rehash) & (armap_count - 1);
3567 srch != hash;
3568 srch = (srch + rehash) & (armap_count - 1))
3569 {
3570 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3571 if (file_offset == 0)
3572 break;
3573 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3574 if (name[0] == h->root.string[0]
3575 && strcmp (name, h->root.string) == 0)
3576 {
3577 found = true;
3578 break;
3579 }
3580 }
3581
3582 if (! found)
3583 {
3584 pundef = &(*pundef)->next;
3585 continue;
3586 }
3587
3588 hash = srch;
3589 }
3590
3591 element = _bfd_get_elt_at_filepos (abfd, file_offset);
3592 if (element == (bfd *) NULL)
3593 return false;
3594
3595 if (! bfd_check_format (element, bfd_object))
3596 return false;
3597
3598 /* Unlike the generic linker, we know that this element provides
3599 a definition for an undefined symbol and we know that we want
3600 to include it. We don't need to check anything. */
3601 if (! (*info->callbacks->add_archive_element) (info, element, name))
3602 return false;
3603 if (! ecoff_link_add_object_symbols (element, info))
3604 return false;
3605
3606 pundef = &(*pundef)->next;
3607 }
3608
3609 return true;
3610}
3611
3612/* This is called if we used _bfd_generic_link_add_archive_symbols
3613 because we were not dealing with an ECOFF archive. */
3614
3615static boolean
3616ecoff_link_check_archive_element (abfd, info, pneeded)
3617 bfd *abfd;
3618 struct bfd_link_info *info;
3619 boolean *pneeded;
3620{
3621 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3622 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3623 = backend->debug_swap.swap_ext_in;
3624 HDRR *symhdr;
3625 bfd_size_type external_ext_size;
80425e6c 3626 PTR external_ext = NULL;
966e0a16 3627 size_t esize;
80425e6c 3628 char *ssext = NULL;
966e0a16
ILT
3629 char *ext_ptr;
3630 char *ext_end;
3631
3632 *pneeded = false;
3633
3634 if (! ecoff_slurp_symbolic_header (abfd))
80425e6c 3635 goto error_return;
966e0a16
ILT
3636
3637 /* If there are no symbols, we don't want it. */
3638 if (bfd_get_symcount (abfd) == 0)
80425e6c 3639 goto successful_return;
966e0a16
ILT
3640
3641 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3642
3643 /* Read in the external symbols and external strings. */
3644 external_ext_size = backend->debug_swap.external_ext_size;
3645 esize = symhdr->iextMax * external_ext_size;
80425e6c 3646 external_ext = (PTR) malloc (esize);
a3a33af3 3647 if (external_ext == NULL && esize != 0)
80425e6c
JK
3648 {
3649 bfd_set_error (bfd_error_no_memory);
3650 goto error_return;
3651 }
3652
966e0a16
ILT
3653 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3654 || bfd_read (external_ext, 1, esize, abfd) != esize)
80425e6c
JK
3655 goto error_return;
3656
3657 ssext = (char *) malloc (symhdr->issExtMax);
a3a33af3 3658 if (ssext == NULL && symhdr->issExtMax != 0)
80425e6c
JK
3659 {
3660 bfd_set_error (bfd_error_no_memory);
3661 goto error_return;
3662 }
966e0a16 3663
966e0a16
ILT
3664 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3665 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
80425e6c 3666 goto error_return;
966e0a16
ILT
3667
3668 /* Look through the external symbols to see if they define some
3669 symbol that is currently undefined. */
3670 ext_ptr = (char *) external_ext;
3671 ext_end = ext_ptr + esize;
3672 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3673 {
3674 EXTR esym;
3675 boolean def;
3676 const char *name;
3677 struct bfd_link_hash_entry *h;
3678
3679 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3680
3681 /* See if this symbol defines something. */
3682 if (esym.asym.st != stGlobal
3683 && esym.asym.st != stLabel
3684 && esym.asym.st != stProc)
3685 continue;
3686
3687 switch (esym.asym.sc)
3688 {
3689 case scText:
3690 case scData:
3691 case scBss:
3692 case scAbs:
3693 case scSData:
3694 case scSBss:
3695 case scRData:
3696 case scCommon:
3697 case scSCommon:
3698 case scInit:
3699 case scFini:
3700 def = true;
3701 break;
3702 default:
3703 def = false;
3704 break;
3705 }
3706
3707 if (! def)
3708 continue;
3709
3710 name = ssext + esym.asym.iss;
3711 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3712
3713 /* Unlike the generic linker, we do not pull in elements because
3714 of common symbols. */
3715 if (h == (struct bfd_link_hash_entry *) NULL
3716 || h->type != bfd_link_hash_undefined)
3717 continue;
3718
3719 /* Include this element. */
3720 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
80425e6c 3721 goto error_return;
966e0a16 3722 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
80425e6c 3723 goto error_return;
966e0a16
ILT
3724
3725 *pneeded = true;
80425e6c 3726 goto successful_return;
966e0a16
ILT
3727 }
3728
80425e6c
JK
3729 successful_return:
3730 if (external_ext != NULL)
3731 free (external_ext);
3732 if (ssext != NULL)
3733 free (ssext);
966e0a16 3734 return true;
80425e6c
JK
3735 error_return:
3736 if (external_ext != NULL)
3737 free (external_ext);
3738 if (ssext != NULL)
3739 free (ssext);
3740 return false;
966e0a16
ILT
3741}
3742
3743/* Add symbols from an ECOFF object file to the global linker hash
3744 table. */
3745
3746static boolean
3747ecoff_link_add_object_symbols (abfd, info)
3748 bfd *abfd;
3749 struct bfd_link_info *info;
3750{
3751 HDRR *symhdr;
3752 bfd_size_type external_ext_size;
80425e6c 3753 PTR external_ext = NULL;
966e0a16 3754 size_t esize;
80425e6c
JK
3755 char *ssext = NULL;
3756 boolean result;
966e0a16
ILT
3757
3758 if (! ecoff_slurp_symbolic_header (abfd))
3759 return false;
3760
3761 /* If there are no symbols, we don't want it. */
3762 if (bfd_get_symcount (abfd) == 0)
3763 return true;
3764
3765 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3766
3767 /* Read in the external symbols and external strings. */
3768 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3769 esize = symhdr->iextMax * external_ext_size;
80425e6c 3770 external_ext = (PTR) malloc (esize);
a3a33af3 3771 if (external_ext == NULL && esize != 0)
80425e6c
JK
3772 {
3773 bfd_set_error (bfd_error_no_memory);
3774 goto error_return;
3775 }
3776
966e0a16
ILT
3777 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3778 || bfd_read (external_ext, 1, esize, abfd) != esize)
80425e6c
JK
3779 goto error_return;
3780
3781 ssext = (char *) malloc (symhdr->issExtMax);
a3a33af3 3782 if (ssext == NULL && symhdr->issExtMax != 0)
80425e6c
JK
3783 {
3784 bfd_set_error (bfd_error_no_memory);
3785 goto error_return;
3786 }
966e0a16 3787
966e0a16
ILT
3788 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3789 || bfd_read (ssext, 1, symhdr->issExtMax, abfd) != symhdr->issExtMax)
80425e6c
JK
3790 goto error_return;
3791
3792 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3793
3794 if (ssext != NULL)
3795 free (ssext);
3796 if (external_ext != NULL)
3797 free (external_ext);
3798 return result;
3799
3800 error_return:
3801 if (ssext != NULL)
3802 free (ssext);
3803 if (external_ext != NULL)
3804 free (external_ext);
3805 return false;
966e0a16
ILT
3806}
3807
3808/* Add the external symbols of an object file to the global linker
3809 hash table. The external symbols and strings we are passed are
3810 just allocated on the stack, and will be discarded. We must
3811 explicitly save any information we may need later on in the link.
3812 We do not want to read the external symbol information again. */
3813
3814static boolean
3815ecoff_link_add_externals (abfd, info, external_ext, ssext)
3816 bfd *abfd;
3817 struct bfd_link_info *info;
3818 PTR external_ext;
3819 char *ssext;
3820{
3821 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3822 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3823 = backend->debug_swap.swap_ext_in;
3824 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3825 unsigned long ext_count;
3826 struct ecoff_link_hash_entry **sym_hash;
3827 char *ext_ptr;
3828 char *ext_end;
3829
3830 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3831
3832 sym_hash = ((struct ecoff_link_hash_entry **)
3833 bfd_alloc (abfd,
3834 ext_count * sizeof (struct bfd_link_hash_entry *)));
9783e04a
DM
3835 if (!sym_hash)
3836 {
d1ad85a6 3837 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
3838 return false;
3839 }
966e0a16
ILT
3840 ecoff_data (abfd)->sym_hashes = sym_hash;
3841
3842 ext_ptr = (char *) external_ext;
3843 ext_end = ext_ptr + ext_count * external_ext_size;
3844 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3845 {
3846 EXTR esym;
3847 boolean skip;
3848 bfd_vma value;
3849 asection *section;
3850 const char *name;
3851 struct ecoff_link_hash_entry *h;
3852
3853 *sym_hash = NULL;
3854
3855 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3856
3857 /* Skip debugging symbols. */
3858 skip = false;
3859 switch (esym.asym.st)
3860 {
3861 case stGlobal:
3862 case stStatic:
3863 case stLabel:
3864 case stProc:
3865 case stStaticProc:
3866 break;
3867 default:
3868 skip = true;
3869 break;
3870 }
3871
3872 if (skip)
3873 continue;
3874
3875 /* Get the information for this symbol. */
3876 value = esym.asym.value;
3877 switch (esym.asym.sc)
3878 {
3879 default:
3880 case scNil:
3881 case scRegister:
3882 case scCdbLocal:
3883 case scBits:
3884 case scCdbSystem:
3885 case scRegImage:
3886 case scInfo:
3887 case scUserStruct:
3888 case scVar:
3889 case scVarRegister:
3890 case scVariant:
3891 case scBasedVar:
3892 case scXData:
3893 case scPData:
3894 section = NULL;
3895 break;
3896 case scText:
3897 section = bfd_make_section_old_way (abfd, ".text");
3898 value -= section->vma;
3899 break;
3900 case scData:
3901 section = bfd_make_section_old_way (abfd, ".data");
3902 value -= section->vma;
3903 break;
3904 case scBss:
3905 section = bfd_make_section_old_way (abfd, ".bss");
3906 value -= section->vma;
3907 break;
3908 case scAbs:
3909 section = &bfd_abs_section;
3910 break;
3911 case scUndefined:
3912 section = &bfd_und_section;
3913 break;
3914 case scSData:
3915 section = bfd_make_section_old_way (abfd, ".sdata");
3916 value -= section->vma;
3917 break;
3918 case scSBss:
3919 section = bfd_make_section_old_way (abfd, ".sbss");
3920 value -= section->vma;
3921 break;
3922 case scRData:
3923 section = bfd_make_section_old_way (abfd, ".rdata");
3924 value -= section->vma;
3925 break;
3926 case scCommon:
3927 if (value > ecoff_data (abfd)->gp_size)
3928 {
3929 section = &bfd_com_section;
3930 break;
3931 }
3932 /* Fall through. */
3933 case scSCommon:
3934 if (ecoff_scom_section.name == NULL)
3935 {
3936 /* Initialize the small common section. */
3937 ecoff_scom_section.name = SCOMMON;
3938 ecoff_scom_section.flags = SEC_IS_COMMON;
3939 ecoff_scom_section.output_section = &ecoff_scom_section;
3940 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3941 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3942 ecoff_scom_symbol.name = SCOMMON;
3943 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3944 ecoff_scom_symbol.section = &ecoff_scom_section;
3945 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3946 }
3947 section = &ecoff_scom_section;
3948 break;
3949 case scSUndefined:
3950 section = &bfd_und_section;
3951 break;
3952 case scInit:
3953 section = bfd_make_section_old_way (abfd, ".init");
3954 value -= section->vma;
3955 break;
3956 case scFini:
3957 section = bfd_make_section_old_way (abfd, ".fini");
3958 value -= section->vma;
3959 break;
3960 }
3961
3962 if (section == (asection *) NULL)
3963 continue;
3964
3965 name = ssext + esym.asym.iss;
3966
3967 if (! (_bfd_generic_link_add_one_symbol
3968 (info, abfd, name, BSF_GLOBAL, section, value,
a3a33af3 3969 (const char *) NULL, true, true,
966e0a16
ILT
3970 (struct bfd_link_hash_entry **) &h)))
3971 return false;
3972
3973 *sym_hash = h;
3974
3975 /* If we are building an ECOFF hash table, save the external
3976 symbol information. */
3977 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3978 {
3979 if (h->abfd == (bfd *) NULL
3980 || (section != &bfd_und_section
3981 && (! bfd_is_com_section (section)
3982 || h->root.type != bfd_link_hash_defined)))
3983 {
3984 h->abfd = abfd;
3985 h->esym = esym;
3986 }
3987 }
3988 }
3989
3990 return true;
3991}
3992\f
3993/* ECOFF final link routines. */
3994
3995static boolean ecoff_final_link_debug_accumulate
3f048f7f
ILT
3996 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
3997 PTR handle));
966e0a16
ILT
3998static boolean ecoff_link_write_external
3999 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4000static boolean ecoff_indirect_link_order
4001 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4002 struct bfd_link_order *));
a3a33af3
ILT
4003static boolean ecoff_reloc_link_order
4004 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4005 struct bfd_link_order *));
966e0a16
ILT
4006
4007/* ECOFF final link routine. This looks through all the input BFDs
4008 and gathers together all the debugging information, and then
4009 processes all the link order information. This may cause it to
4010 close and reopen some input BFDs; I'll see how bad this is. */
4011
4012boolean
4013ecoff_bfd_final_link (abfd, info)
4014 bfd *abfd;
4015 struct bfd_link_info *info;
4016{
4017 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4018 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4019 HDRR *symhdr;
3f048f7f 4020 PTR handle;
966e0a16
ILT
4021 register bfd *input_bfd;
4022 asection *o;
4023 struct bfd_link_order *p;
4024
4025 /* We accumulate the debugging information counts in the symbolic
4026 header. */
4027 symhdr = &debug->symbolic_header;
966e0a16
ILT
4028 symhdr->vstamp = 0;
4029 symhdr->ilineMax = 0;
4030 symhdr->cbLine = 0;
4031 symhdr->idnMax = 0;
4032 symhdr->ipdMax = 0;
4033 symhdr->isymMax = 0;
4034 symhdr->ioptMax = 0;
4035 symhdr->iauxMax = 0;
4036 symhdr->issMax = 0;
4037 symhdr->issExtMax = 0;
4038 symhdr->ifdMax = 0;
4039 symhdr->crfd = 0;
4040 symhdr->iextMax = 0;
4041
4042 /* We accumulate the debugging information itself in the debug_info
4043 structure. */
3f048f7f
ILT
4044 debug->line = NULL;
4045 debug->external_dnr = NULL;
4046 debug->external_pdr = NULL;
4047 debug->external_sym = NULL;
4048 debug->external_opt = NULL;
4049 debug->external_aux = NULL;
4050 debug->ss = NULL;
966e0a16 4051 debug->ssext = debug->ssext_end = NULL;
3f048f7f
ILT
4052 debug->external_fdr = NULL;
4053 debug->external_rfd = NULL;
966e0a16
ILT
4054 debug->external_ext = debug->external_ext_end = NULL;
4055
3f048f7f
ILT
4056 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4057 if (handle == (PTR) NULL)
4058 return false;
4059
966e0a16
ILT
4060 /* Accumulate the debugging symbols from each input BFD. */
4061 for (input_bfd = info->input_bfds;
4062 input_bfd != (bfd *) NULL;
4063 input_bfd = input_bfd->link_next)
4064 {
4065 boolean ret;
4066
966e0a16 4067 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
3f048f7f
ILT
4068 {
4069 /* Abitrarily set the symbolic header vstamp to the vstamp
4070 of the first object file in the link. */
4071 if (symhdr->vstamp == 0)
4072 symhdr->vstamp
4073 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4074 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4075 handle);
4076 }
966e0a16 4077 else
3f048f7f
ILT
4078 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4079 debug, &backend->debug_swap,
4080 input_bfd, info);
966e0a16
ILT
4081 if (! ret)
4082 return false;
4083
4084 /* Combine the register masks. */
4085 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4086 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4087 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4088 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4089 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4090 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4091 }
4092
4093 /* Write out the external symbols. */
4094 ecoff_link_hash_traverse (ecoff_hash_table (info),
4095 ecoff_link_write_external,
4096 (PTR) abfd);
4097
4098 if (info->relocateable)
4099 {
4100 /* We need to make a pass over the link_orders to count up the
4101 number of relocations we will need to output, so that we know
4102 how much space they will take up. */
4103 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4104 {
4105 o->reloc_count = 0;
4106 for (p = o->link_order_head;
4107 p != (struct bfd_link_order *) NULL;
4108 p = p->next)
4109 if (p->type == bfd_indirect_link_order)
4110 o->reloc_count += p->u.indirect.section->reloc_count;
a3a33af3
ILT
4111 else if (p->type == bfd_section_reloc_link_order
4112 || p->type == bfd_symbol_reloc_link_order)
4113 ++o->reloc_count;
966e0a16 4114 }
3f048f7f
ILT
4115 }
4116
4117 /* Compute the reloc and symbol file positions. */
4118 ecoff_compute_reloc_file_positions (abfd);
4119
4120 /* Write out the debugging information. */
4121 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4122 &backend->debug_swap, info,
4123 ecoff_data (abfd)->sym_filepos))
4124 return false;
966e0a16 4125
3f048f7f 4126 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
966e0a16 4127
3f048f7f
ILT
4128 if (info->relocateable)
4129 {
966e0a16
ILT
4130 /* Now reset the reloc_count field of the sections in the output
4131 BFD to 0, so that we can use them to keep track of how many
4132 relocs we have output thus far. */
4133 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4134 o->reloc_count = 0;
4135 }
4136
4137 /* Get a value for the GP register. */
4138 if (ecoff_data (abfd)->gp == 0)
4139 {
4140 struct bfd_link_hash_entry *h;
4141
4142 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4143 if (h != (struct bfd_link_hash_entry *) NULL
4144 && h->type == bfd_link_hash_defined)
4145 ecoff_data (abfd)->gp = (h->u.def.value
4146 + h->u.def.section->output_section->vma
4147 + h->u.def.section->output_offset);
4148 else if (info->relocateable)
4149 {
4150 bfd_vma lo;
4151
4152 /* Make up a value. */
4153 lo = (bfd_vma) -1;
4154 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4155 {
4156 if (o->vma < lo
4157 && (strcmp (o->name, _SBSS) == 0
4158 || strcmp (o->name, _SDATA) == 0
4159 || strcmp (o->name, _LIT4) == 0
4160 || strcmp (o->name, _LIT8) == 0
4161 || strcmp (o->name, _LITA) == 0))
4162 lo = o->vma;
4163 }
4164 ecoff_data (abfd)->gp = lo + 0x8000;
4165 }
4166 else
4167 {
4168 /* If the relocate_section function needs to do a reloc
4169 involving the GP value, it should make a reloc_dangerous
4170 callback to warn that GP is not defined. */
4171 }
4172 }
4173
4174 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4175 {
4176 /* Ignore any link_orders for the .reginfo section, which does
4177 not really exist. */
4178 if (strcmp (o->name, REGINFO) == 0)
4179 continue;
4180
4181 for (p = o->link_order_head;
4182 p != (struct bfd_link_order *) NULL;
4183 p = p->next)
4184 {
966e0a16
ILT
4185 if (p->type == bfd_indirect_link_order
4186 && (bfd_get_flavour (p->u.indirect.section->owner)
4187 == bfd_target_ecoff_flavour))
4188 {
4189 if (! ecoff_indirect_link_order (abfd, info, o, p))
4190 return false;
4191 }
a3a33af3
ILT
4192 else if (p->type == bfd_section_reloc_link_order
4193 || p->type == bfd_symbol_reloc_link_order)
4194 {
4195 if (! ecoff_reloc_link_order (abfd, info, o, p))
4196 return false;
4197 }
966e0a16
ILT
4198 else
4199 {
4200 if (! _bfd_default_link_order (abfd, info, o, p))
4201 return false;
4202 }
4203 }
4204 }
4205
4206 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4207
4208 return true;
4209}
4210
4211/* Accumulate the debugging information for an input BFD into the
4212 output BFD. This must read in the symbolic information of the
4213 input BFD. */
4214
4215static boolean
3f048f7f 4216ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
966e0a16
ILT
4217 bfd *output_bfd;
4218 bfd *input_bfd;
4219 struct bfd_link_info *info;
3f048f7f 4220 PTR handle;
966e0a16
ILT
4221{
4222 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4223 const struct ecoff_debug_swap * const swap =
4224 &ecoff_backend (input_bfd)->debug_swap;
4225 HDRR *symhdr = &debug->symbolic_header;
4226 boolean ret;
4227
4228#define READ(ptr, offset, count, size, type) \
4229 if (symhdr->count == 0) \
4230 debug->ptr = NULL; \
4231 else \
4232 { \
80425e6c
JK
4233 debug->ptr = (type) malloc (size * symhdr->count); \
4234 if (debug->ptr == NULL) \
4235 { \
4236 bfd_set_error (bfd_error_no_memory); \
4237 ret = false; \
4238 goto return_something; \
4239 } \
966e0a16
ILT
4240 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4241 != 0) \
4242 || (bfd_read (debug->ptr, size, symhdr->count, \
4243 input_bfd) != size * symhdr->count)) \
80425e6c
JK
4244 { \
4245 ret = false; \
4246 goto return_something; \
4247 } \
966e0a16
ILT
4248 }
4249
a3a33af3
ILT
4250 /* If raw_syments is not NULL, then the data was already by read by
4251 ecoff_slurp_symbolic_info. */
4252 if (ecoff_data (input_bfd)->raw_syments == NULL)
4253 {
4254 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4255 unsigned char *);
4256 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4257 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4258 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4259 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4260 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4261 union aux_ext *);
4262 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4263 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4264 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4265 }
966e0a16
ILT
4266#undef READ
4267
4268 /* We do not read the external strings or the external symbols. */
4269
4270 ret = (bfd_ecoff_debug_accumulate
3f048f7f 4271 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
966e0a16 4272 &ecoff_backend (output_bfd)->debug_swap,
3f048f7f 4273 input_bfd, debug, swap, info));
966e0a16 4274
80425e6c 4275 return_something:
a3a33af3
ILT
4276 if (ecoff_data (input_bfd)->raw_syments == NULL)
4277 {
4278 if (debug->line != NULL)
4279 free (debug->line);
4280 if (debug->external_dnr != NULL)
4281 free (debug->external_dnr);
4282 if (debug->external_pdr != NULL)
4283 free (debug->external_pdr);
4284 if (debug->external_sym != NULL)
4285 free (debug->external_sym);
4286 if (debug->external_opt != NULL)
4287 free (debug->external_opt);
4288 if (debug->external_aux != NULL)
4289 free (debug->external_aux);
4290 if (debug->ss != NULL)
4291 free (debug->ss);
4292 if (debug->external_fdr != NULL)
4293 free (debug->external_fdr);
4294 if (debug->external_rfd != NULL)
4295 free (debug->external_rfd);
4296
4297 /* Make sure we don't accidentally follow one of these pointers
4298 into freed memory. */
4299 debug->line = NULL;
4300 debug->external_dnr = NULL;
4301 debug->external_pdr = NULL;
4302 debug->external_sym = NULL;
4303 debug->external_opt = NULL;
4304 debug->external_aux = NULL;
4305 debug->ss = NULL;
4306 debug->external_fdr = NULL;
4307 debug->external_rfd = NULL;
4308 }
966e0a16
ILT
4309
4310 return ret;
4311}
4312
4313/* Put out information for an external symbol. These come only from
4314 the hash table. */
4315
4316static boolean
4317ecoff_link_write_external (h, data)
4318 struct ecoff_link_hash_entry *h;
4319 PTR data;
4320{
4321 bfd *output_bfd = (bfd *) data;
4322
4323 /* FIXME: We should check if this symbol is being stripped. */
4324
4325 if (h->root.written)
4326 return true;
4327
4328 if (h->abfd == (bfd *) NULL)
4329 {
4330 h->esym.jmptbl = 0;
4331 h->esym.cobol_main = 0;
4332 h->esym.weakext = 0;
4333 h->esym.reserved = 0;
4334 h->esym.ifd = ifdNil;
4335 h->esym.asym.value = 0;
966e0a16 4336 h->esym.asym.st = stGlobal;
a3a33af3
ILT
4337
4338 if (h->root.type != bfd_link_hash_defined)
4339 h->esym.asym.sc = scAbs;
4340 else
4341 {
4342 asection *output_section;
4343 const char *name;
4344
4345 output_section = h->root.u.def.section->output_section;
4346 name = bfd_section_name (output_section->owner, output_section);
4347
4348 if (strcmp (name, _TEXT) == 0)
4349 h->esym.asym.sc = scText;
4350 else if (strcmp (name, _DATA) == 0)
4351 h->esym.asym.sc = scData;
4352 else if (strcmp (name, _SDATA) == 0)
4353 h->esym.asym.sc = scSData;
4354 else if (strcmp (name, _RDATA) == 0)
4355 h->esym.asym.sc = scRData;
4356 else if (strcmp (name, _BSS) == 0)
4357 h->esym.asym.sc = scBss;
4358 else if (strcmp (name, _SBSS) == 0)
4359 h->esym.asym.sc = scSBss;
4360 else if (strcmp (name, _INIT) == 0)
4361 h->esym.asym.sc = scInit;
4362 else if (strcmp (name, _FINI) == 0)
4363 h->esym.asym.sc = scFini;
4364 else if (strcmp (name, _PDATA) == 0)
4365 h->esym.asym.sc = scPData;
4366 else if (strcmp (name, _XDATA) == 0)
4367 h->esym.asym.sc = scXData;
4368 else
4369 h->esym.asym.sc = scAbs;
4370 }
4371
966e0a16
ILT
4372 h->esym.asym.reserved = 0;
4373 h->esym.asym.index = indexNil;
4374 }
3f048f7f 4375 else if (h->esym.ifd != -1)
966e0a16 4376 {
3f048f7f
ILT
4377 struct ecoff_debug_info *debug;
4378
966e0a16
ILT
4379 /* Adjust the FDR index for the symbol by that used for the
4380 input BFD. */
3f048f7f
ILT
4381 debug = &ecoff_data (h->abfd)->debug_info;
4382 BFD_ASSERT (h->esym.ifd >= 0
4383 && h->esym.ifd < debug->symbolic_header.ifdMax);
4384 h->esym.ifd = debug->ifdmap[h->esym.ifd];
966e0a16
ILT
4385 }
4386
4387 switch (h->root.type)
4388 {
4389 default:
4390 case bfd_link_hash_new:
4391 abort ();
4392 case bfd_link_hash_undefined:
4393 case bfd_link_hash_weak:
a3a33af3
ILT
4394 if (h->esym.asym.sc != scUndefined
4395 && h->esym.asym.sc != scSUndefined)
4396 h->esym.asym.sc = scUndefined;
966e0a16
ILT
4397 break;
4398 case bfd_link_hash_defined:
4399 if (h->esym.asym.sc == scUndefined
4400 || h->esym.asym.sc == scSUndefined)
4401 h->esym.asym.sc = scAbs;
4402 else if (h->esym.asym.sc == scCommon)
4403 h->esym.asym.sc = scBss;
4404 else if (h->esym.asym.sc == scSCommon)
4405 h->esym.asym.sc = scSBss;
4406 h->esym.asym.value = (h->root.u.def.value
4407 + h->root.u.def.section->output_section->vma
4408 + h->root.u.def.section->output_offset);
4409 break;
4410 case bfd_link_hash_common:
4411 if (h->esym.asym.sc != scCommon
4412 && h->esym.asym.sc != scSCommon)
4413 h->esym.asym.sc = scCommon;
4414 h->esym.asym.value = h->root.u.c.size;
4415 break;
4416 case bfd_link_hash_indirect:
4417 case bfd_link_hash_warning:
4418 /* FIXME: Ignore these for now. The circumstances under which
4419 they should be written out are not clear to me. */
4420 return true;
4421 }
4422
4423 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4424 symbol number. */
4425 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4426 h->root.written = true;
4427
4428 return (bfd_ecoff_debug_one_external
4429 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4430 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4431 &h->esym));
4432}
4433
4434/* Relocate and write an ECOFF section into an ECOFF output file. */
4435
4436static boolean
4437ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4438 bfd *output_bfd;
4439 struct bfd_link_info *info;
4440 asection *output_section;
4441 struct bfd_link_order *link_order;
4442{
4443 asection *input_section;
4444 bfd *input_bfd;
a3a33af3
ILT
4445 struct ecoff_section_tdata *section_tdata;
4446 bfd_size_type raw_size;
4447 bfd_size_type cooked_size;
80425e6c 4448 bfd_byte *contents = NULL;
966e0a16
ILT
4449 bfd_size_type external_reloc_size;
4450 bfd_size_type external_relocs_size;
80425e6c 4451 PTR external_relocs = NULL;
966e0a16
ILT
4452
4453 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4454
4455 if (link_order->size == 0)
4456 return true;
4457
4458 input_section = link_order->u.indirect.section;
4459 input_bfd = input_section->owner;
a3a33af3
ILT
4460 section_tdata = ecoff_section_data (input_bfd, input_section);
4461
4462 raw_size = input_section->_raw_size;
4463 cooked_size = input_section->_cooked_size;
4464 if (cooked_size == 0)
4465 cooked_size = raw_size;
966e0a16
ILT
4466
4467 BFD_ASSERT (input_section->output_section == output_section);
4468 BFD_ASSERT (input_section->output_offset == link_order->offset);
a3a33af3
ILT
4469 BFD_ASSERT (cooked_size == link_order->size);
4470
4471 /* Get the section contents. We allocate memory for the larger of
4472 the size before relocating and the size after relocating. */
4473 contents = (bfd_byte *) malloc (raw_size >= cooked_size
4474 ? raw_size
4475 : cooked_size);
4476 if (contents == NULL && raw_size != 0)
80425e6c
JK
4477 {
4478 bfd_set_error (bfd_error_no_memory);
4479 goto error_return;
4480 }
966e0a16 4481
a3a33af3
ILT
4482 /* If we are relaxing, the contents may have already been read into
4483 memory, in which case we copy them into our new buffer. We don't
4484 simply reuse the old buffer in case cooked_size > raw_size. */
4485 if (section_tdata != (struct ecoff_section_tdata *) NULL
4486 && section_tdata->contents != (bfd_byte *) NULL)
4487 memcpy (contents, section_tdata->contents, raw_size);
4488 else
4489 {
4490 if (! bfd_get_section_contents (input_bfd, input_section,
4491 (PTR) contents,
4492 (file_ptr) 0, raw_size))
4493 goto error_return;
4494 }
4495
4496 /* Get the relocs. If we are relaxing MIPS code, they will already
4497 have been read in. Otherwise, we read them in now. */
966e0a16
ILT
4498 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4499 external_relocs_size = external_reloc_size * input_section->reloc_count;
a3a33af3
ILT
4500
4501 if (section_tdata != (struct ecoff_section_tdata *) NULL)
4502 external_relocs = section_tdata->external_relocs;
4503 else
80425e6c 4504 {
a3a33af3
ILT
4505 external_relocs = (PTR) malloc (external_relocs_size);
4506 if (external_relocs == NULL && external_relocs_size != 0)
4507 {
4508 bfd_set_error (bfd_error_no_memory);
4509 goto error_return;
4510 }
80425e6c 4511
a3a33af3
ILT
4512 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4513 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4514 != external_relocs_size))
4515 goto error_return;
4516 }
966e0a16
ILT
4517
4518 /* Relocate the section contents. */
4519 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4520 (output_bfd, info, input_bfd, input_section, contents,
4521 external_relocs)))
80425e6c 4522 goto error_return;
966e0a16
ILT
4523
4524 /* Write out the relocated section. */
4525 if (! bfd_set_section_contents (output_bfd,
4526 output_section,
4527 (PTR) contents,
4528 input_section->output_offset,
a3a33af3 4529 cooked_size))
80425e6c 4530 goto error_return;
966e0a16
ILT
4531
4532 /* If we are producing relocateable output, the relocs were
4533 modified, and we write them out now. We use the reloc_count
4534 field of output_section to keep track of the number of relocs we
4535 have output so far. */
4536 if (info->relocateable)
4537 {
4538 if (bfd_seek (output_bfd,
4539 (output_section->rel_filepos +
4540 output_section->reloc_count * external_reloc_size),
4541 SEEK_SET) != 0
4542 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4543 != external_relocs_size))
80425e6c 4544 goto error_return;
966e0a16
ILT
4545 output_section->reloc_count += input_section->reloc_count;
4546 }
4547
80425e6c
JK
4548 if (contents != NULL)
4549 free (contents);
a3a33af3 4550 if (external_relocs != NULL && section_tdata == NULL)
80425e6c 4551 free (external_relocs);
966e0a16 4552 return true;
80425e6c
JK
4553
4554 error_return:
4555 if (contents != NULL)
4556 free (contents);
a3a33af3 4557 if (external_relocs != NULL && section_tdata == NULL)
80425e6c
JK
4558 free (external_relocs);
4559 return false;
966e0a16 4560}
a3a33af3
ILT
4561
4562/* Generate a reloc when linking an ECOFF file. This is a reloc
4563 requested by the linker, and does come from any input file. This
4564 is used to build constructor and destructor tables when linking
4565 with -Ur. */
4566
4567static boolean
4568ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4569 bfd *output_bfd;
4570 struct bfd_link_info *info;
4571 asection *output_section;
4572 struct bfd_link_order *link_order;
4573{
4574 arelent rel;
4575 struct internal_reloc in;
4576 bfd_size_type external_reloc_size;
4577 bfd_byte *rbuf;
4578 boolean ok;
4579
4580 /* We set up an arelent to pass to the backend adjust_reloc_out
4581 routine. */
4582 rel.address = link_order->offset;
4583
4584 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4585 if (rel.howto == (const reloc_howto_type *) NULL)
4586 {
4587 bfd_set_error (bfd_error_bad_value);
4588 return false;
4589 }
4590
4591 if (link_order->type == bfd_section_reloc_link_order)
4592 rel.sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
4593 else
4594 {
4595 /* We can't set up a reloc against a symbol correctly, because
4596 we have no asymbol structure. Currently no adjust_reloc_out
4597 routine cases. */
4598 rel.sym_ptr_ptr = (asymbol **) NULL;
4599 }
4600
4601 /* All ECOFF relocs are in-place. Put the addend into the object
4602 file. */
4603
4604 BFD_ASSERT (rel.howto->partial_inplace);
4605 if (link_order->u.reloc.p->addend != 0)
4606 {
4607 bfd_size_type size;
4608 bfd_reloc_status_type rstat;
4609 bfd_byte *buf;
4610 boolean ok;
4611
4612 size = bfd_get_reloc_size (rel.howto);
4613 buf = (bfd_byte *) bfd_zmalloc (size);
4614 if (buf == (bfd_byte *) NULL)
4615 {
4616 bfd_set_error (bfd_error_no_memory);
4617 return false;
4618 }
4619 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4620 link_order->u.reloc.p->addend, buf);
4621 switch (rstat)
4622 {
4623 case bfd_reloc_ok:
4624 break;
4625 default:
4626 case bfd_reloc_outofrange:
4627 abort ();
4628 case bfd_reloc_overflow:
4629 if (! ((*info->callbacks->reloc_overflow)
4630 (info,
4631 (link_order->type == bfd_section_reloc_link_order
4632 ? bfd_section_name (output_bfd,
4633 link_order->u.reloc.p->u.section)
4634 : link_order->u.reloc.p->u.name),
4635 rel.howto->name, link_order->u.reloc.p->addend,
4636 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
4637 {
4638 free (buf);
4639 return false;
4640 }
4641 break;
4642 }
4643 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4644 (file_ptr) link_order->offset, size);
4645 free (buf);
4646 if (! ok)
4647 return false;
4648 }
4649
4650 rel.addend = 0;
4651
4652 /* Move the information into a internal_reloc structure. */
4653 in.r_vaddr = (rel.address
4654 + bfd_get_section_vma (output_bfd, output_section));
4655 in.r_type = rel.howto->type;
4656
4657 if (link_order->type == bfd_symbol_reloc_link_order)
4658 {
4659 struct ecoff_link_hash_entry *h;
4660
4661 h = ecoff_link_hash_lookup (ecoff_hash_table (info),
4662 link_order->u.reloc.p->u.name,
4663 false, false, true);
4664 if (h != (struct ecoff_link_hash_entry *) NULL
4665 && h->indx != -1)
4666 in.r_symndx = h->indx;
4667 else
4668 {
4669 if (! ((*info->callbacks->unattached_reloc)
4670 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4671 (asection *) NULL, (bfd_vma) 0)))
4672 return false;
4673 in.r_symndx = 0;
4674 }
4675 in.r_extern = 1;
4676 }
4677 else
4678 {
4679 CONST char *name;
4680
4681 name = bfd_get_section_name (output_bfd,
4682 link_order->u.reloc.p->u.section);
4683 if (strcmp (name, ".text") == 0)
4684 in.r_symndx = RELOC_SECTION_TEXT;
4685 else if (strcmp (name, ".rdata") == 0)
4686 in.r_symndx = RELOC_SECTION_RDATA;
4687 else if (strcmp (name, ".data") == 0)
4688 in.r_symndx = RELOC_SECTION_DATA;
4689 else if (strcmp (name, ".sdata") == 0)
4690 in.r_symndx = RELOC_SECTION_SDATA;
4691 else if (strcmp (name, ".sbss") == 0)
4692 in.r_symndx = RELOC_SECTION_SBSS;
4693 else if (strcmp (name, ".bss") == 0)
4694 in.r_symndx = RELOC_SECTION_BSS;
4695 else if (strcmp (name, ".init") == 0)
4696 in.r_symndx = RELOC_SECTION_INIT;
4697 else if (strcmp (name, ".lit8") == 0)
4698 in.r_symndx = RELOC_SECTION_LIT8;
4699 else if (strcmp (name, ".lit4") == 0)
4700 in.r_symndx = RELOC_SECTION_LIT4;
4701 else if (strcmp (name, ".xdata") == 0)
4702 in.r_symndx = RELOC_SECTION_XDATA;
4703 else if (strcmp (name, ".pdata") == 0)
4704 in.r_symndx = RELOC_SECTION_PDATA;
4705 else if (strcmp (name, ".fini") == 0)
4706 in.r_symndx = RELOC_SECTION_FINI;
4707 else if (strcmp (name, ".lita") == 0)
4708 in.r_symndx = RELOC_SECTION_LITA;
4709 else if (strcmp (name, "*ABS*") == 0)
4710 in.r_symndx = RELOC_SECTION_ABS;
4711 else
4712 abort ();
4713 in.r_extern = 0;
4714 }
4715
4716 /* Let the BFD backend adjust the reloc. */
4717 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4718
4719 /* Get some memory and swap out the reloc. */
4720 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4721 rbuf = (bfd_byte *) malloc (external_reloc_size);
4722 if (rbuf == (bfd_byte *) NULL)
4723 {
4724 bfd_set_error (bfd_error_no_memory);
4725 return false;
4726 }
4727
4728 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4729
4730 ok = (bfd_seek (output_bfd,
4731 (output_section->rel_filepos +
4732 output_section->reloc_count * external_reloc_size),
4733 SEEK_SET) == 0
4734 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4735 == external_reloc_size));
4736
4737 if (ok)
4738 ++output_section->reloc_count;
4739
4740 free (rbuf);
4741
4742 return ok;
4743}
This page took 0.276828 seconds and 4 git commands to generate.