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