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