1 /* BFD back-end for i960 b.out binaries */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD 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 1, or (at your option)
12 BFD 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 BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
32 #include "libaout.h" /* BFD a.out internal data structures */
35 /* Align an address by rounding it up to a power of two. It leaves the
36 address unchanged if align == 0 (2^0 = alignment of 1 byte) */
37 #define i960_align(addr, align) \
38 ( ((addr) + ((1<<(align))-1)) & (-1 << (align)))
40 extern uint64_type _do_getl64 (), _do_getb64 ();
41 extern uint32_type _do_getl32 (), _do_getb32 ();
42 extern uint16_type _do_getl16 (), _do_getb16 ();
43 extern void _do_putl64 (), _do_putb64 ();
44 extern void _do_putl32 (), _do_putb32 ();
45 extern void _do_putl16 (), _do_putb16 ();
48 PROTO (static boolean, b_out_squirt_out_relocs,(bfd *abfd, asection *section));
49 PROTO (static bfd_target *, b_out_callback, (bfd *));
51 PROTO (boolean, aout_slurp_symbol_table, (bfd *abfd));
52 PROTO (void , aout_write_syms, ());
54 PROTO (static void, swap_exec_header, (bfd *abfd, struct exec *execp));
57 b_out_little_object_p (abfd)
60 unsigned char magicbytes[LONG_SIZE];
63 if (bfd_read ((PTR)magicbytes, 1, LONG_SIZE, abfd) != LONG_SIZE) {
64 bfd_error = system_call_error;
67 anexec.a_magic = _do_getl32 (magicbytes);
69 if (N_BADMAG (anexec)) {
70 bfd_error = wrong_format;
73 return some_aout_object_p (abfd, b_out_callback);
77 b_out_big_object_p (abfd)
80 unsigned char magicbytes[LONG_SIZE];
83 if (bfd_read ((PTR)magicbytes, 1, LONG_SIZE, abfd) != LONG_SIZE) {
84 bfd_error = system_call_error;
88 anexec.a_magic = _do_getb32 (magicbytes);
90 if (N_BADMAG (anexec)) {
91 bfd_error = wrong_format;
94 return some_aout_object_p (abfd, b_out_callback);
97 /* Finish up the opening of a b.out file for reading. Fill in all the
98 fields that are not handled by common code. */
101 b_out_callback (abfd)
105 struct exec *execp = &anexec;
106 unsigned long bss_start;
108 /* Reread the exec header, because the common code didn't get all of
109 our extended header. */
111 if (bfd_seek (abfd, 0L, SEEK_SET) < 0) {
112 bfd_error = system_call_error;
116 /* FIXME, needs to be hacked for character array read-in ala sunos.c. */
117 if (bfd_read ((PTR) execp, 1, sizeof (struct exec), abfd)
118 != sizeof (struct exec)) {
119 bfd_error = wrong_format;
123 swap_exec_header (abfd, execp);
125 /* Architecture and machine type */
126 abfd->obj_arch = bfd_arch_i960; /* B.out only used on i960 */
127 abfd->obj_machine = bfd_mach_i960_core; /* Default */
128 /* FIXME: Set real machine type from file here */
130 /* The positions of the string table and symbol table. */
131 obj_str_filepos (abfd) = N_STROFF (anexec);
132 obj_sym_filepos (abfd) = N_SYMOFF (anexec);
134 /* The alignments of the sections */
135 obj_textsec (abfd)->alignment_power = execp->a_talign;
136 obj_datasec (abfd)->alignment_power = execp->a_dalign;
137 obj_bsssec (abfd)->alignment_power = execp->a_balign;
139 /* The starting addresses of the sections. */
140 obj_textsec (abfd)->vma = anexec.a_tload;
141 obj_datasec (abfd)->vma = anexec.a_dload;
142 bss_start = anexec.a_dload + anexec.a_data; /* BSS = end of data section */
143 obj_bsssec (abfd)->vma = i960_align (bss_start, anexec.a_balign);
145 /* The file positions of the sections */
146 obj_textsec (abfd)->filepos = N_TXTOFF(anexec);
147 obj_datasec (abfd)->filepos = N_DATOFF(anexec);
149 /* The file positions of the relocation info */
150 obj_textsec (abfd)->rel_filepos = N_TROFF(anexec);
151 obj_datasec (abfd)->rel_filepos = N_DROFF(anexec);
158 b_out_mkobject (abfd)
163 bfd_error = system_call_error;
165 /* Use an intermediate variable for clarity */
166 rawptr = (PTR) zalloc (sizeof (struct aoutdata) + sizeof (struct exec));
168 if (rawptr == (PTR)NULL) {
169 bfd_error = no_memory;
173 set_tdata(abfd, (struct aoutdata *) rawptr);
174 exec_hdr (abfd) = (struct exec *) ( (char*)rawptr + sizeof (struct aoutdata));
176 /* For simplicity's sake we just make all the sections right here. */
177 obj_textsec (abfd) = (asection *)NULL;
178 obj_datasec (abfd) = (asection *)NULL;
179 obj_bsssec (abfd) = (asection *)NULL;
181 bfd_make_section (abfd, ".text");
182 bfd_make_section (abfd, ".data");
183 bfd_make_section (abfd, ".bss");
189 b_out_write_object_contents (abfd)
192 struct exec swapped_hdr;
194 exec_hdr (abfd)->a_magic = BMAGIC;
196 exec_hdr (abfd)->a_text = obj_textsec (abfd)->size;
197 exec_hdr (abfd)->a_data = obj_datasec (abfd)->size;
198 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->size;
199 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
200 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
201 exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
202 sizeof (struct relocation_info));
203 exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
204 sizeof (struct relocation_info));
206 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
207 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
208 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
210 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
211 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
213 /* FIXME, turn the header into bytes here, to avoid problems with
214 sizes and alignments of its fields. */
215 swapped_hdr = *exec_hdr(abfd);
216 swap_exec_header (abfd, &swapped_hdr);
218 bfd_seek (abfd, 0L, SEEK_SET);
219 bfd_write ((PTR) &swapped_hdr, 1, sizeof (struct exec), abfd);
221 /* Now write out reloc info, followed by syms and strings */
222 if (bfd_get_symcount (abfd) != 0)
225 (long)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET);
227 aout_write_syms (abfd);
229 bfd_seek (abfd, (long)(N_TROFF(*exec_hdr(abfd))), SEEK_SET);
231 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
232 bfd_seek (abfd, (long)(N_DROFF(*exec_hdr(abfd))), SEEK_SET);
234 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
240 swap_exec_header (abfd, execp)
244 #define swapme(field) field = bfd_h_get_32 (abfd, (unsigned char *)&field);
245 swapme (execp->a_magic);
246 swapme (execp->a_text);
247 swapme (execp->a_data);
248 swapme (execp->a_bss);
249 swapme (execp->a_syms);
250 swapme (execp->a_entry);
251 swapme (execp->a_trsize);
252 swapme (execp->a_drsize);
253 swapme (execp->a_tload);
254 swapme (execp->a_dload);
255 /* talign, dalign, and balign are one-byte fields and don't swap. */
259 /** Some reloc hackery */
261 #define CALLS 0x66003800 /* Template for 'calls' instruction */
262 #define BAL 0x0b000000 /* Template for 'bal' instruction */
263 #define BAL_MASK 0x00ffffff
265 static bfd_reloc_status_enum_type
266 callj_callback(abfd, reloc_entry, symbol_in, data, input_section)
268 arelent *reloc_entry;
271 asection *input_section;
273 int word = bfd_get_32(abfd, data+reloc_entry->address);
274 aout_symbol_type *symbol = aout_symbol(symbol_in);
276 if (IS_OTHER(symbol->other)) {
277 /* Call to a system procedure - replace code with system
281 word = CALLS | (symbol->other - 1);
282 bfd_put_32(abfd, word, data+reloc_entry->address); /* replace */
286 if (IS_CALLNAME(symbol->other)) {
287 aout_symbol_type *balsym = symbol+1;
288 /* The next symbol should be an N_BALNAME */
289 BFD_ASSERT(IS_BALNAME(balsym->other));
291 /* We are calling a leaf - so replace the call instruction
295 (((word & BAL_MASK) +
296 balsym->symbol.section->output_offset +
297 balsym->symbol.section->output_section->vma+
298 balsym->symbol.value + reloc_entry->addend -
299 ( input_section->output_section->vma + input_section->output_offset))
302 bfd_put_32(abfd, word, data+reloc_entry->address); /* replace */
305 return bfd_reloc_continue;
308 /* type rshift size bitsize pcrel bitpos absolute overflow check*/
311 static reloc_howto_type howto_reloc_callj =
312 HOWTO( 3, 0, 2, 24, true, 0, true, true, callj_callback,"callj", true, 0x00ffffff, 0x00ffffff,false);
313 static reloc_howto_type howto_reloc_abs32 =
314 HOWTO(1, 0, 2, 32, false, 0, true, true,0,"abs32", true, 0xffffffff,0xffffffff,false);
315 static reloc_howto_type howto_reloc_pcrel24 =
316 HOWTO(2, 0, 2, 24, true, 0, true, true,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);
318 /* Allocate enough room for all the reloc entries, plus pointers to them all */
321 b_out_slurp_reloc_table (abfd, asect, symbols)
328 struct relocation_info *relocs;
329 arelent *reloc_cache;
331 if (asect->relocation) return true;
332 if (!aout_slurp_symbol_table (abfd)) return false;
334 if (asect == obj_datasec (abfd)) {
335 reloc_size = exec_hdr(abfd)->a_drsize;
339 if (asect == obj_textsec (abfd)) {
340 reloc_size = exec_hdr(abfd)->a_trsize;
344 bfd_error = invalid_operation;
348 bfd_seek (abfd, (long)(asect->rel_filepos), SEEK_SET);
349 count = reloc_size / sizeof (struct relocation_info);
351 relocs = (struct relocation_info *) malloc (reloc_size);
353 bfd_error = no_memory;
356 reloc_cache = (arelent *) malloc ((count+1) * sizeof (arelent));
358 free ((char*)relocs);
359 bfd_error = no_memory;
363 if (bfd_read ((PTR) relocs, 1, reloc_size, abfd) != reloc_size) {
364 bfd_error = system_call_error;
371 register struct relocation_info *rptr = relocs;
372 unsigned int counter = 0;
373 arelent *cache_ptr = reloc_cache;
374 int extern_mask, pcrel_mask, callj_mask;
376 if (abfd->xvec->header_byteorder_big_p) {
377 /* Big-endian bit field allocation order */
382 /* Little-endian bit field allocation order */
388 for (; counter < count; counter++, rptr++, cache_ptr++)
390 unsigned char *raw = (unsigned char *)rptr;
392 cache_ptr->address = bfd_h_get_32 (abfd, raw + 0);
393 if (abfd->xvec->header_byteorder_big_p) {
394 symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6];
396 symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4];
399 if (raw[7] & extern_mask) {
400 /* If this is set then the r_index is a index into the symbol table;
401 * if the bit is not set then r_index contains a section map.
402 * We either fill in the sym entry with a pointer to the symbol,
403 * or point to the correct section
405 cache_ptr->sym_ptr_ptr = symbols + symnum;
406 cache_ptr->addend = 0;
407 cache_ptr->section = (asection*)NULL;
409 /* In a.out symbols are relative to the beginning of the
410 * file rather than sections ?
411 * (look in translate_from_native_sym_flags)
412 * The reloc entry addend has added to it the offset into the
413 * file of the data, so subtract the base to make the reloc
414 * section relative */
415 cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
419 cache_ptr->section = obj_textsec(abfd);
420 cache_ptr->addend = -obj_textsec(abfd)->vma;
424 cache_ptr->section = obj_datasec(abfd);
425 cache_ptr->addend = - obj_datasec(abfd)->vma;
429 cache_ptr->section = obj_bsssec(abfd);
430 cache_ptr->addend = - obj_bsssec(abfd)->vma;
443 /* The i960 only has a few relocation types:
444 abs 32-bit and pcrel 24bit. Except for callj's! */
445 if (raw[7] & callj_mask)
446 cache_ptr->howto = &howto_reloc_callj;
447 else if ( raw[7] & pcrel_mask)
448 cache_ptr->howto = &howto_reloc_pcrel24;
450 cache_ptr->howto = &howto_reloc_abs32;
455 asect->relocation = reloc_cache;
456 asect->reloc_count = count;
462 b_out_squirt_out_relocs (abfd, section)
468 unsigned int count = section->reloc_count;
469 struct relocation_info *native, *natptr;
470 size_t natsize = count * sizeof (struct relocation_info);
471 int extern_mask, pcrel_mask, len_2, callj_mask;
472 if (count == 0) return true;
473 generic = section->orelocation;
474 native = ((struct relocation_info *) malloc (natsize));
476 bfd_error = no_memory;
480 if (abfd->xvec->header_byteorder_big_p) {
481 /* Big-endian bit field allocation order */
487 /* Little-endian bit field allocation order */
494 for (natptr = native; count > 0; --count, ++natptr, ++generic)
496 arelent *g = *generic;
497 unsigned char *raw = (unsigned char *)natptr;
500 bfd_h_put_32(abfd, g->address, raw);
501 /* Find a type in the output format which matches the input howto -
502 * at the moment we assume input format == output format FIXME!!
504 /* FIXME: Need callj stuff here, and to check the howto entries to
505 be sure they are real for this architecture. */
506 if (g->howto== &howto_reloc_callj) {
507 raw[7] = callj_mask + pcrel_mask + len_2;
509 else if (g->howto == &howto_reloc_pcrel24) {
510 raw[7] = pcrel_mask +len_2;
515 if (g->sym_ptr_ptr != (asymbol **)NULL)
517 /* name clobbered by aout_write_syms to be symbol index*/
518 if ((*(g->sym_ptr_ptr))->section) {
519 /* replace the section offset into the addent */
520 g->addend += (*(g->sym_ptr_ptr))->section->vma ;
522 symnum = stoi((*(g->sym_ptr_ptr))->name);
523 raw[7] |= extern_mask;
524 BFD_ASSERT(g->addend == 0);
527 if (g->section == (asection *)NULL) {
531 else if(g->section->output_section == obj_textsec(abfd)) {
533 BFD_ASSERT(g->addend == 0);
535 else if (g->section->output_section == obj_datasec(abfd)) {
537 BFD_ASSERT(g->addend + obj_textsec(abfd)->size == 0);
539 else if (g->section->output_section == obj_bsssec(abfd)) {
541 BFD_ASSERT(g->addend + obj_textsec(abfd)->size
542 + obj_datasec(abfd)->size == 0);
549 if (abfd->xvec->header_byteorder_big_p) {
550 raw[4] = (unsigned char) (symnum >> 16);
551 raw[5] = (unsigned char) (symnum >> 8);
552 raw[6] = (unsigned char) (symnum );
554 raw[6] = (unsigned char) (symnum >> 16);
555 raw[5] = (unsigned char) (symnum >> 8);
556 raw[4] = (unsigned char) (symnum );
560 if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
569 /* This is stupid. This function should be a boolean predicate */
571 b_out_canonicalize_reloc (abfd, section, relptr, symbols)
577 arelent *tblptr = section->relocation;
578 unsigned int count = 0;
580 if (!(tblptr || b_out_slurp_reloc_table (abfd, section, symbols))) return 0;
581 tblptr = section->relocation;
582 if (!tblptr) return 0;
584 for (; count++ < section->reloc_count;)
585 *relptr++ = tblptr++;
589 return section->reloc_count;
593 b_out_get_reloc_upper_bound (abfd, asect)
597 if (bfd_get_format (abfd) != bfd_object) {
598 bfd_error = invalid_operation;
602 if (asect == obj_datasec (abfd))
603 return (sizeof (arelent *) *
604 ((exec_hdr(abfd)->a_drsize / sizeof (struct relocation_info))
607 if (asect == obj_textsec (abfd))
608 return (sizeof (arelent *) *
609 ((exec_hdr(abfd)->a_trsize / sizeof (struct relocation_info))
612 bfd_error = invalid_operation;
617 b_out_set_section_contents (abfd, section, location, offset, count)
620 unsigned char *location;
624 if (abfd->output_has_begun == false) { /* set by bfd.c handler */
625 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL) /*||
626 (obj_textsec (abfd)->size == 0) || (obj_datasec (abfd)->size == 0)*/) {
627 bfd_error = invalid_operation;
631 obj_textsec (abfd)->filepos = sizeof(struct exec);
632 obj_datasec(abfd)->filepos = obj_textsec(abfd)->filepos
633 + obj_textsec (abfd)->size;
636 /* regardless, once we know what we're doing, we might as well get going */
637 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
640 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
646 b_out_set_arch_mach (abfd, arch, machine)
648 enum bfd_architecture arch;
649 unsigned long machine;
651 abfd->obj_arch = arch;
652 abfd->obj_machine = machine;
653 if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */
655 if (arch == bfd_arch_i960) /* i960 default is OK */
657 case bfd_mach_i960_core:
658 case bfd_mach_i960_kb_sb:
659 case bfd_mach_i960_mc:
660 case bfd_mach_i960_xa:
661 case bfd_mach_i960_ca:
662 case bfd_mach_i960_ka_sa:
672 DEFUN(b_out_sizeof_headers,(abfd, exec),
676 return sizeof(struct exec);
679 /* Build the transfer vectors for Big and Little-Endian B.OUT files. */
681 /* We don't have core files. */
682 #define aout_core_file_failing_command _bfd_dummy_core_file_failing_command
683 #define aout_core_file_failing_signal _bfd_dummy_core_file_failing_signal
684 #define aout_core_file_matches_executable_p \
685 _bfd_dummy_core_file_matches_executable_p
687 /* We use BSD-Unix generic archive files. */
688 #define aout_openr_next_archived_file bfd_generic_openr_next_archived_file
689 #define aout_generic_stat_arch_elt bfd_generic_stat_arch_elt
690 #define aout_slurp_armap bfd_slurp_bsd_armap
691 #define aout_slurp_extended_name_table bfd_true
692 #define aout_write_armap bsd_write_armap
693 #define aout_truncate_arname bfd_bsd_truncate_arname
695 /* We override these routines from the usual a.out file routines. */
696 #define aout_canonicalize_reloc b_out_canonicalize_reloc
697 #define aout_get_reloc_upper_bound b_out_get_reloc_upper_bound
698 #define aout_set_section_contents b_out_set_section_contents
699 #define aout_set_arch_mach b_out_set_arch_mach
700 #define aout_sizeof_headers b_out_sizeof_headers
703 bfd_target b_out_vec_big_host =
705 "b.out.big", /* name */
706 bfd_target_aout_flavour_enum,
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 */
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_enum,
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},