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