1 /* BFD back-end for Intel 960 b.out binaries.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #include "libaout.h" /* BFD a.out internal data structures */
32 /* Align an address by rounding it up to a power of two. It leaves the
33 address unchanged if align == 0 (2^0 = alignment of 1 byte) */
34 #define i960_align(addr, align) \
35 ( ((addr) + ((1<<(align))-1)) & (-1 << (align)))
38 #define EXEC_BYTES_SIZE (sizeof (struct exec))
39 PROTO (static boolean, b_out_squirt_out_relocs,(bfd *abfd, asection *section));
40 PROTO (static bfd_target *, b_out_callback, (bfd *));
42 PROTO (boolean, aout_32_slurp_symbol_table, (bfd *abfd));
43 PROTO (void , aout_32_write_syms, ());
45 PROTO (static void, swap_exec_header, (bfd *abfd, struct internal_exec *execp));
49 b_out_little_object_p (abfd)
52 unsigned char magicbytes[LONG_SIZE];
53 struct internal_exec anexec;
55 if (bfd_read ((PTR)magicbytes, 1, LONG_SIZE, abfd) != LONG_SIZE) {
56 bfd_error = system_call_error;
59 anexec.a_magic = _do_getl32 (magicbytes);
61 if (N_BADMAG (anexec)) {
62 bfd_error = wrong_format;
65 return aout_32_some_aout_object_p (abfd, b_out_callback);
69 b_out_big_object_p (abfd)
72 unsigned char magicbytes[LONG_SIZE];
73 struct internal_exec anexec;
75 if (bfd_read ((PTR)magicbytes, 1, LONG_SIZE, abfd) != LONG_SIZE) {
76 bfd_error = system_call_error;
80 anexec.a_magic = _do_getb32 (magicbytes);
82 if (N_BADMAG (anexec)) {
83 bfd_error = wrong_format;
86 return aout_32_some_aout_object_p (abfd, b_out_callback);
89 /* Finish up the opening of a b.out file for reading. Fill in all the
90 fields that are not handled by common code. */
96 struct internal_exec anexec;
97 struct internal_exec *execp = &anexec;
98 unsigned long bss_start;
100 /* Reread the exec header, because the common code didn't get all of
101 our extended header. */
103 if (bfd_seek (abfd, 0L, SEEK_SET) < 0) {
104 bfd_error = system_call_error;
108 /* FIXME, needs to be hacked for character array read-in ala sunos.c. */
109 if (bfd_read ((PTR) execp, 1, sizeof (struct internal_exec), abfd)
110 != sizeof (struct internal_exec)) {
111 bfd_error = wrong_format;
115 swap_exec_header (abfd, execp);
117 /* Architecture and machine type */
118 bfd_set_arch_mach(abfd,
119 bfd_arch_i960, /* B.out only used on i960 */
120 bfd_mach_i960_core /* Default */
123 /* The positions of the string table and symbol table. */
124 obj_str_filepos (abfd) = N_STROFF (anexec);
125 obj_sym_filepos (abfd) = N_SYMOFF (anexec);
127 /* The alignments of the sections */
128 obj_textsec (abfd)->alignment_power = execp->a_talign;
129 obj_datasec (abfd)->alignment_power = execp->a_dalign;
130 obj_bsssec (abfd)->alignment_power = execp->a_balign;
132 /* The starting addresses of the sections. */
133 obj_textsec (abfd)->vma = anexec.a_tload;
134 obj_datasec (abfd)->vma = anexec.a_dload;
135 bss_start = anexec.a_dload + anexec.a_data; /* BSS = end of data section */
136 obj_bsssec (abfd)->vma = i960_align (bss_start, anexec.a_balign);
138 /* The file positions of the sections */
139 obj_textsec (abfd)->filepos = N_TXTOFF(anexec);
140 obj_datasec (abfd)->filepos = N_DATOFF(anexec);
142 /* The file positions of the relocation info */
143 obj_textsec (abfd)->rel_filepos = N_TROFF(anexec);
144 obj_datasec (abfd)->rel_filepos = N_DROFF(anexec);
151 b_out_mkobject (abfd)
156 bfd_error = system_call_error;
158 /* Use an intermediate variable for clarity */
159 rawptr = (PTR) zalloc (sizeof (struct aoutdata) + sizeof (struct internal_exec));
161 if (rawptr == (PTR)NULL) {
162 bfd_error = no_memory;
166 set_tdata(abfd, (struct aoutdata *) rawptr);
167 exec_hdr (abfd) = (struct internal_exec *) ( (char*)rawptr + sizeof (struct aoutdata));
169 /* For simplicity's sake we just make all the sections right here. */
170 obj_textsec (abfd) = (asection *)NULL;
171 obj_datasec (abfd) = (asection *)NULL;
172 obj_bsssec (abfd) = (asection *)NULL;
174 bfd_make_section (abfd, ".text");
175 bfd_make_section (abfd, ".data");
176 bfd_make_section (abfd, ".bss");
182 b_out_write_object_contents (abfd)
185 struct internal_exec swapped_hdr;
187 exec_hdr (abfd)->a_magic = BMAGIC;
189 exec_hdr (abfd)->a_text = obj_textsec (abfd)->size;
190 exec_hdr (abfd)->a_data = obj_datasec (abfd)->size;
191 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->size;
192 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
193 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
194 exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
195 sizeof (struct relocation_info));
196 exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
197 sizeof (struct relocation_info));
199 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
200 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
201 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
203 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
204 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
206 /* FIXME, turn the header into bytes here, to avoid problems with
207 sizes and alignments of its fields. */
208 swapped_hdr = *exec_hdr(abfd);
209 swap_exec_header (abfd, &swapped_hdr);
211 bfd_seek (abfd, 0L, SEEK_SET);
212 bfd_write ((PTR) &swapped_hdr, 1, sizeof (struct internal_exec), abfd);
214 /* Now write out reloc info, followed by syms and strings */
215 if (bfd_get_symcount (abfd) != 0)
218 (long)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET);
220 aout_32_write_syms (abfd);
222 bfd_seek (abfd, (long)(N_TROFF(*exec_hdr(abfd))), SEEK_SET);
224 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
225 bfd_seek (abfd, (long)(N_DROFF(*exec_hdr(abfd))), SEEK_SET);
227 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
233 swap_exec_header (abfd, execp)
235 struct internal_exec *execp;
237 #define swapme(field) field = bfd_h_get_32 (abfd, (unsigned char *)&field);
238 swapme (execp->a_magic);
239 swapme (execp->a_text);
240 swapme (execp->a_data);
241 swapme (execp->a_bss);
242 swapme (execp->a_syms);
243 swapme (execp->a_entry);
244 swapme (execp->a_trsize);
245 swapme (execp->a_drsize);
246 swapme (execp->a_tload);
247 swapme (execp->a_dload);
248 /* talign, dalign, and balign are one-byte fields and don't swap. */
252 /** Some reloc hackery */
254 #define CALLS 0x66003800 /* Template for 'calls' instruction */
255 #define BAL 0x0b000000 /* Template for 'bal' instruction */
256 #define BAL_MASK 0x00ffffff
258 static bfd_reloc_status_type
259 callj_callback(abfd, reloc_entry, symbol_in, data, input_section)
261 arelent *reloc_entry;
264 asection *input_section;
266 int word = bfd_get_32(abfd, data+reloc_entry->address);
267 aout_symbol_type *symbol = aout_symbol(symbol_in);
269 if (IS_OTHER(symbol->other)) {
270 /* Call to a system procedure - replace code with system
274 word = CALLS | (symbol->other - 1);
275 bfd_put_32(abfd, word, data+reloc_entry->address); /* replace */
279 if (IS_CALLNAME(symbol->other)) {
280 aout_symbol_type *balsym = symbol+1;
281 /* The next symbol should be an N_BALNAME */
282 BFD_ASSERT(IS_BALNAME(balsym->other));
284 /* We are calling a leaf - so replace the call instruction
288 (((word & BAL_MASK) +
289 balsym->symbol.section->output_offset +
290 balsym->symbol.section->output_section->vma+
291 balsym->symbol.value + reloc_entry->addend -
292 ( input_section->output_section->vma + input_section->output_offset))
295 bfd_put_32(abfd, word, data+reloc_entry->address); /* replace */
298 return bfd_reloc_continue;
301 /* type rshift size bitsize pcrel bitpos absolute overflow check*/
304 static reloc_howto_type howto_reloc_callj =
305 HOWTO( 3, 0, 2, 24, true, 0, true, true, callj_callback,"callj", true, 0x00ffffff, 0x00ffffff,false);
306 static reloc_howto_type howto_reloc_abs32 =
307 HOWTO(1, 0, 2, 32, false, 0, true, true,0,"abs32", true, 0xffffffff,0xffffffff,false);
308 static reloc_howto_type howto_reloc_pcrel24 =
309 HOWTO(2, 0, 2, 24, true, 0, true, true,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);
311 /* Allocate enough room for all the reloc entries, plus pointers to them all */
314 b_out_slurp_reloc_table (abfd, asect, symbols)
321 struct relocation_info *relocs;
322 arelent *reloc_cache;
324 if (asect->relocation) return true;
325 if (!aout_32_slurp_symbol_table (abfd)) return false;
327 if (asect == obj_datasec (abfd)) {
328 reloc_size = exec_hdr(abfd)->a_drsize;
332 if (asect == obj_textsec (abfd)) {
333 reloc_size = exec_hdr(abfd)->a_trsize;
337 bfd_error = invalid_operation;
341 bfd_seek (abfd, (long)(asect->rel_filepos), SEEK_SET);
342 count = reloc_size / sizeof (struct relocation_info);
344 relocs = (struct relocation_info *) malloc (reloc_size);
346 bfd_error = no_memory;
349 reloc_cache = (arelent *) malloc ((count+1) * sizeof (arelent));
351 free ((char*)relocs);
352 bfd_error = no_memory;
356 if (bfd_read ((PTR) relocs, 1, reloc_size, abfd) != reloc_size) {
357 bfd_error = system_call_error;
364 register struct relocation_info *rptr = relocs;
365 unsigned int counter = 0;
366 arelent *cache_ptr = reloc_cache;
367 int extern_mask, pcrel_mask, callj_mask;
369 if (abfd->xvec->header_byteorder_big_p) {
370 /* Big-endian bit field allocation order */
375 /* Little-endian bit field allocation order */
381 for (; counter < count; counter++, rptr++, cache_ptr++)
383 unsigned char *raw = (unsigned char *)rptr;
385 cache_ptr->address = bfd_h_get_32 (abfd, raw + 0);
386 if (abfd->xvec->header_byteorder_big_p) {
387 symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6];
389 symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4];
392 if (raw[7] & extern_mask) {
393 /* If this is set then the r_index is a index into the symbol table;
394 * if the bit is not set then r_index contains a section map.
395 * We either fill in the sym entry with a pointer to the symbol,
396 * or point to the correct section
398 cache_ptr->sym_ptr_ptr = symbols + symnum;
399 cache_ptr->addend = 0;
400 cache_ptr->section = (asection*)NULL;
402 /* In a.out symbols are relative to the beginning of the
403 * file rather than sections ?
404 * (look in translate_from_native_sym_flags)
405 * The reloc entry addend has added to it the offset into the
406 * file of the data, so subtract the base to make the reloc
407 * section relative */
408 cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
412 cache_ptr->section = obj_textsec(abfd);
413 cache_ptr->addend = -obj_textsec(abfd)->vma;
417 cache_ptr->section = obj_datasec(abfd);
418 cache_ptr->addend = - obj_datasec(abfd)->vma;
422 cache_ptr->section = obj_bsssec(abfd);
423 cache_ptr->addend = - obj_bsssec(abfd)->vma;
436 /* The i960 only has a few relocation types:
437 abs 32-bit and pcrel 24bit. Except for callj's! */
438 if (raw[7] & callj_mask)
439 cache_ptr->howto = &howto_reloc_callj;
440 else if ( raw[7] & pcrel_mask)
441 cache_ptr->howto = &howto_reloc_pcrel24;
443 cache_ptr->howto = &howto_reloc_abs32;
448 asect->relocation = reloc_cache;
449 asect->reloc_count = count;
455 b_out_squirt_out_relocs (abfd, section)
461 unsigned int count = section->reloc_count;
462 struct relocation_info *native, *natptr;
463 size_t natsize = count * sizeof (struct relocation_info);
464 int extern_mask, pcrel_mask, len_2, callj_mask;
465 if (count == 0) return true;
466 generic = section->orelocation;
467 native = ((struct relocation_info *) malloc (natsize));
469 bfd_error = no_memory;
473 if (abfd->xvec->header_byteorder_big_p) {
474 /* Big-endian bit field allocation order */
480 /* Little-endian bit field allocation order */
487 for (natptr = native; count > 0; --count, ++natptr, ++generic)
489 arelent *g = *generic;
490 unsigned char *raw = (unsigned char *)natptr;
493 bfd_h_put_32(abfd, g->address, raw);
494 /* Find a type in the output format which matches the input howto -
495 * at the moment we assume input format == output format FIXME!!
497 /* FIXME: Need callj stuff here, and to check the howto entries to
498 be sure they are real for this architecture. */
499 if (g->howto== &howto_reloc_callj) {
500 raw[7] = callj_mask + pcrel_mask + len_2;
502 else if (g->howto == &howto_reloc_pcrel24) {
503 raw[7] = pcrel_mask +len_2;
508 if (g->sym_ptr_ptr != (asymbol **)NULL)
510 /* name clobbered by aout_write_syms to be symbol index*/
511 if ((*(g->sym_ptr_ptr))->section) {
512 /* replace the section offset into the addent */
513 g->addend += (*(g->sym_ptr_ptr))->section->vma ;
515 symnum = stoi((*(g->sym_ptr_ptr))->name);
516 raw[7] |= extern_mask;
517 BFD_ASSERT(g->addend == 0);
520 if (g->section == (asection *)NULL) {
524 else if(g->section->output_section == obj_textsec(abfd)) {
526 BFD_ASSERT(g->addend + obj_textsec(abfd)->vma == 0);
528 else if (g->section->output_section == obj_datasec(abfd)) {
530 BFD_ASSERT(g->addend + obj_datasec(abfd)->vma == 0);
532 else if (g->section->output_section == obj_bsssec(abfd)) {
534 BFD_ASSERT(g->addend + obj_bsssec(abfd)->vma == 0);
541 if (abfd->xvec->header_byteorder_big_p) {
542 raw[4] = (unsigned char) (symnum >> 16);
543 raw[5] = (unsigned char) (symnum >> 8);
544 raw[6] = (unsigned char) (symnum );
546 raw[6] = (unsigned char) (symnum >> 16);
547 raw[5] = (unsigned char) (symnum >> 8);
548 raw[4] = (unsigned char) (symnum );
552 if (bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
561 /* This is stupid. This function should be a boolean predicate */
563 b_out_canonicalize_reloc (abfd, section, relptr, symbols)
569 arelent *tblptr = section->relocation;
570 unsigned int count = 0;
572 if (!(tblptr || b_out_slurp_reloc_table (abfd, section, symbols))) return 0;
573 tblptr = section->relocation;
574 if (!tblptr) return 0;
576 for (; count++ < section->reloc_count;)
577 *relptr++ = tblptr++;
581 return section->reloc_count;
585 b_out_get_reloc_upper_bound (abfd, asect)
589 if (bfd_get_format (abfd) != bfd_object) {
590 bfd_error = invalid_operation;
594 if (asect == obj_datasec (abfd))
595 return (sizeof (arelent *) *
596 ((exec_hdr(abfd)->a_drsize / sizeof (struct relocation_info))
599 if (asect == obj_textsec (abfd))
600 return (sizeof (arelent *) *
601 ((exec_hdr(abfd)->a_trsize / sizeof (struct relocation_info))
604 bfd_error = invalid_operation;
609 b_out_set_section_contents (abfd, section, location, offset, count)
612 unsigned char *location;
616 if (abfd->output_has_begun == false) { /* set by bfd.c handler */
617 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL) /*||
618 (obj_textsec (abfd)->size == 0) || (obj_datasec (abfd)->size == 0)*/) {
619 bfd_error = invalid_operation;
623 obj_textsec (abfd)->filepos = sizeof(struct internal_exec);
624 obj_datasec(abfd)->filepos = obj_textsec(abfd)->filepos
625 + obj_textsec (abfd)->size;
628 /* regardless, once we know what we're doing, we might as well get going */
629 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
632 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
638 b_out_set_arch_mach (abfd, arch, machine)
640 enum bfd_architecture arch;
641 unsigned long machine;
643 bfd_default_set_arch_mach(abfd, arch, machine);
645 if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */
647 if (arch == bfd_arch_i960) /* i960 default is OK */
649 case bfd_mach_i960_core:
650 case bfd_mach_i960_kb_sb:
651 case bfd_mach_i960_mc:
652 case bfd_mach_i960_xa:
653 case bfd_mach_i960_ca:
654 case bfd_mach_i960_ka_sa:
665 DEFUN(b_out_sizeof_headers,(ignore_abfd, ignore),
669 return sizeof(struct internal_exec);
675 /* Build the transfer vectors for Big and Little-Endian B.OUT files. */
677 /* We don't have core files. */
678 #define aout_32_core_file_failing_command _bfd_dummy_core_file_failing_command
679 #define aout_32_core_file_failing_signal _bfd_dummy_core_file_failing_signal
680 #define aout_32_core_file_matches_executable_p \
681 _bfd_dummy_core_file_matches_executable_p
683 /* We use BSD-Unix generic archive files. */
684 #define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file
685 #define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt
686 #define aout_32_slurp_armap bfd_slurp_bsd_armap
687 #define aout_32_slurp_extended_name_table bfd_true
688 #define aout_32_write_armap bsd_write_armap
689 #define aout_32_truncate_arname bfd_bsd_truncate_arname
691 /* We override these routines from the usual a.out file routines. */
692 #define aout_32_canonicalize_reloc b_out_canonicalize_reloc
693 #define aout_32_get_reloc_upper_bound b_out_get_reloc_upper_bound
694 #define aout_32_set_section_contents b_out_set_section_contents
695 #define aout_32_set_arch_mach b_out_set_arch_mach
696 #define aout_32_sizeof_headers b_out_sizeof_headers
698 #define aout_32_bfd_debug_info_start bfd_void
699 #define aout_32_bfd_debug_info_end bfd_void
700 #define aout_32_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
703 bfd_target b_out_vec_big_host =
705 "b.out.big", /* name */
706 bfd_target_aout_flavour,
707 false, /* data byte order is little */
708 true, /* hdr byte order is big */
709 (HAS_RELOC | EXEC_P | /* object flags */
710 HAS_LINENO | HAS_DEBUG |
711 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT ),
712 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
713 ' ', /* ar_pad_char */
714 16, /* ar_max_namelen */
715 2, /* minumum alignment power */
717 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
718 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
719 {_bfd_dummy_target, b_out_big_object_p, /* bfd_check_format */
720 bfd_generic_archive_p, _bfd_dummy_target},
721 {bfd_false, b_out_mkobject, /* bfd_set_format */
722 _bfd_generic_mkarchive, bfd_false},
723 {bfd_false, b_out_write_object_contents, /* bfd_write_contents */
724 _bfd_write_archive_contents, bfd_false},
730 bfd_target b_out_vec_little_host =
732 "b.out.little", /* name */
733 bfd_target_aout_flavour,
734 false, /* data byte order is little */
735 false, /* header byte order is little */
736 (HAS_RELOC | EXEC_P | /* object flags */
737 HAS_LINENO | HAS_DEBUG |
738 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT ),
739 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
740 ' ', /* ar_pad_char */
741 16, /* ar_max_namelen */
743 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
744 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* hdrs */
746 {_bfd_dummy_target, b_out_little_object_p, /* bfd_check_format */
747 bfd_generic_archive_p, _bfd_dummy_target},
748 {bfd_false, b_out_mkobject, /* bfd_set_format */
749 _bfd_generic_mkarchive, bfd_false},
750 {bfd_false, b_out_write_object_contents, /* bfd_write_contents */
751 _bfd_write_archive_contents, bfd_false},