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)))
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 abfd->obj_arch = bfd_arch_i960; /* B.out only used on i960 */
119 abfd->obj_machine = bfd_mach_i960_core; /* Default */
120 /* FIXME: Set real machine type from file here */
122 /* The positions of the string table and symbol table. */
123 obj_str_filepos (abfd) = N_STROFF (anexec);
124 obj_sym_filepos (abfd) = N_SYMOFF (anexec);
126 /* The alignments of the sections */
127 obj_textsec (abfd)->alignment_power = execp->a_talign;
128 obj_datasec (abfd)->alignment_power = execp->a_dalign;
129 obj_bsssec (abfd)->alignment_power = execp->a_balign;
131 /* The starting addresses of the sections. */
132 obj_textsec (abfd)->vma = anexec.a_tload;
133 obj_datasec (abfd)->vma = anexec.a_dload;
134 bss_start = anexec.a_dload + anexec.a_data; /* BSS = end of data section */
135 obj_bsssec (abfd)->vma = i960_align (bss_start, anexec.a_balign);
137 /* The file positions of the sections */
138 obj_textsec (abfd)->filepos = N_TXTOFF(anexec);
139 obj_datasec (abfd)->filepos = N_DATOFF(anexec);
141 /* The file positions of the relocation info */
142 obj_textsec (abfd)->rel_filepos = N_TROFF(anexec);
143 obj_datasec (abfd)->rel_filepos = N_DROFF(anexec);
150 b_out_mkobject (abfd)
155 bfd_error = system_call_error;
157 /* Use an intermediate variable for clarity */
158 rawptr = (PTR) zalloc (sizeof (struct aoutdata) + sizeof (struct internal_exec));
160 if (rawptr == (PTR)NULL) {
161 bfd_error = no_memory;
165 set_tdata(abfd, (struct aoutdata *) rawptr);
166 exec_hdr (abfd) = (struct internal_exec *) ( (char*)rawptr + sizeof (struct aoutdata));
168 /* For simplicity's sake we just make all the sections right here. */
169 obj_textsec (abfd) = (asection *)NULL;
170 obj_datasec (abfd) = (asection *)NULL;
171 obj_bsssec (abfd) = (asection *)NULL;
173 bfd_make_section (abfd, ".text");
174 bfd_make_section (abfd, ".data");
175 bfd_make_section (abfd, ".bss");
181 b_out_write_object_contents (abfd)
184 struct internal_exec swapped_hdr;
186 exec_hdr (abfd)->a_magic = BMAGIC;
188 exec_hdr (abfd)->a_text = obj_textsec (abfd)->size;
189 exec_hdr (abfd)->a_data = obj_datasec (abfd)->size;
190 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->size;
191 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
192 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
193 exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
194 sizeof (struct relocation_info));
195 exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
196 sizeof (struct relocation_info));
198 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
199 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
200 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
202 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
203 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
205 /* FIXME, turn the header into bytes here, to avoid problems with
206 sizes and alignments of its fields. */
207 swapped_hdr = *exec_hdr(abfd);
208 swap_exec_header (abfd, &swapped_hdr);
210 bfd_seek (abfd, 0L, SEEK_SET);
211 bfd_write ((PTR) &swapped_hdr, 1, sizeof (struct internal_exec), abfd);
213 /* Now write out reloc info, followed by syms and strings */
214 if (bfd_get_symcount (abfd) != 0)
217 (long)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET);
219 aout_32_write_syms (abfd);
221 bfd_seek (abfd, (long)(N_TROFF(*exec_hdr(abfd))), SEEK_SET);
223 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
224 bfd_seek (abfd, (long)(N_DROFF(*exec_hdr(abfd))), SEEK_SET);
226 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
232 swap_exec_header (abfd, execp)
234 struct internal_exec *execp;
236 #define swapme(field) field = bfd_h_get_32 (abfd, (unsigned char *)&field);
237 swapme (execp->a_magic);
238 swapme (execp->a_text);
239 swapme (execp->a_data);
240 swapme (execp->a_bss);
241 swapme (execp->a_syms);
242 swapme (execp->a_entry);
243 swapme (execp->a_trsize);
244 swapme (execp->a_drsize);
245 swapme (execp->a_tload);
246 swapme (execp->a_dload);
247 /* talign, dalign, and balign are one-byte fields and don't swap. */
251 /** Some reloc hackery */
253 #define CALLS 0x66003800 /* Template for 'calls' instruction */
254 #define BAL 0x0b000000 /* Template for 'bal' instruction */
255 #define BAL_MASK 0x00ffffff
257 static bfd_reloc_status_enum_type
258 callj_callback(abfd, reloc_entry, symbol_in, data, input_section)
260 arelent *reloc_entry;
263 asection *input_section;
265 int word = bfd_get_32(abfd, data+reloc_entry->address);
266 aout_symbol_type *symbol = aout_symbol(symbol_in);
268 if (IS_OTHER(symbol->other)) {
269 /* Call to a system procedure - replace code with system
273 word = CALLS | (symbol->other - 1);
274 bfd_put_32(abfd, word, data+reloc_entry->address); /* replace */
278 if (IS_CALLNAME(symbol->other)) {
279 aout_symbol_type *balsym = symbol+1;
280 /* The next symbol should be an N_BALNAME */
281 BFD_ASSERT(IS_BALNAME(balsym->other));
283 /* We are calling a leaf - so replace the call instruction
287 (((word & BAL_MASK) +
288 balsym->symbol.section->output_offset +
289 balsym->symbol.section->output_section->vma+
290 balsym->symbol.value + reloc_entry->addend -
291 ( input_section->output_section->vma + input_section->output_offset))
294 bfd_put_32(abfd, word, data+reloc_entry->address); /* replace */
297 return bfd_reloc_continue;
300 /* type rshift size bitsize pcrel bitpos absolute overflow check*/
303 static reloc_howto_type howto_reloc_callj =
304 HOWTO( 3, 0, 2, 24, true, 0, true, true, callj_callback,"callj", true, 0x00ffffff, 0x00ffffff,false);
305 static reloc_howto_type howto_reloc_abs32 =
306 HOWTO(1, 0, 2, 32, false, 0, true, true,0,"abs32", true, 0xffffffff,0xffffffff,false);
307 static reloc_howto_type howto_reloc_pcrel24 =
308 HOWTO(2, 0, 2, 24, true, 0, true, true,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);
310 /* Allocate enough room for all the reloc entries, plus pointers to them all */
313 b_out_slurp_reloc_table (abfd, asect, symbols)
320 struct relocation_info *relocs;
321 arelent *reloc_cache;
323 if (asect->relocation) return true;
324 if (!aout_32_slurp_symbol_table (abfd)) return false;
326 if (asect == obj_datasec (abfd)) {
327 reloc_size = exec_hdr(abfd)->a_drsize;
331 if (asect == obj_textsec (abfd)) {
332 reloc_size = exec_hdr(abfd)->a_trsize;
336 bfd_error = invalid_operation;
340 bfd_seek (abfd, (long)(asect->rel_filepos), SEEK_SET);
341 count = reloc_size / sizeof (struct relocation_info);
343 relocs = (struct relocation_info *) malloc (reloc_size);
345 bfd_error = no_memory;
348 reloc_cache = (arelent *) malloc ((count+1) * sizeof (arelent));
350 free ((char*)relocs);
351 bfd_error = no_memory;
355 if (bfd_read ((PTR) relocs, 1, reloc_size, abfd) != reloc_size) {
356 bfd_error = system_call_error;
363 register struct relocation_info *rptr = relocs;
364 unsigned int counter = 0;
365 arelent *cache_ptr = reloc_cache;
366 int extern_mask, pcrel_mask, callj_mask;
368 if (abfd->xvec->header_byteorder_big_p) {
369 /* Big-endian bit field allocation order */
374 /* Little-endian bit field allocation order */
380 for (; counter < count; counter++, rptr++, cache_ptr++)
382 unsigned char *raw = (unsigned char *)rptr;
384 cache_ptr->address = bfd_h_get_32 (abfd, raw + 0);
385 if (abfd->xvec->header_byteorder_big_p) {
386 symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6];
388 symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4];
391 if (raw[7] & extern_mask) {
392 /* If this is set then the r_index is a index into the symbol table;
393 * if the bit is not set then r_index contains a section map.
394 * We either fill in the sym entry with a pointer to the symbol,
395 * or point to the correct section
397 cache_ptr->sym_ptr_ptr = symbols + symnum;
398 cache_ptr->addend = 0;
399 cache_ptr->section = (asection*)NULL;
401 /* In a.out symbols are relative to the beginning of the
402 * file rather than sections ?
403 * (look in translate_from_native_sym_flags)
404 * The reloc entry addend has added to it the offset into the
405 * file of the data, so subtract the base to make the reloc
406 * section relative */
407 cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
411 cache_ptr->section = obj_textsec(abfd);
412 cache_ptr->addend = -obj_textsec(abfd)->vma;
416 cache_ptr->section = obj_datasec(abfd);
417 cache_ptr->addend = - obj_datasec(abfd)->vma;
421 cache_ptr->section = obj_bsssec(abfd);
422 cache_ptr->addend = - obj_bsssec(abfd)->vma;
435 /* The i960 only has a few relocation types:
436 abs 32-bit and pcrel 24bit. Except for callj's! */
437 if (raw[7] & callj_mask)
438 cache_ptr->howto = &howto_reloc_callj;
439 else if ( raw[7] & pcrel_mask)
440 cache_ptr->howto = &howto_reloc_pcrel24;
442 cache_ptr->howto = &howto_reloc_abs32;
447 asect->relocation = reloc_cache;
448 asect->reloc_count = count;
454 b_out_squirt_out_relocs (abfd, section)
460 unsigned int count = section->reloc_count;
461 struct relocation_info *native, *natptr;
462 size_t natsize = count * sizeof (struct relocation_info);
463 int extern_mask, pcrel_mask, len_2, callj_mask;
464 if (count == 0) return true;
465 generic = section->orelocation;
466 native = ((struct relocation_info *) malloc (natsize));
468 bfd_error = no_memory;
472 if (abfd->xvec->header_byteorder_big_p) {
473 /* Big-endian bit field allocation order */
479 /* Little-endian bit field allocation order */
486 for (natptr = native; count > 0; --count, ++natptr, ++generic)
488 arelent *g = *generic;
489 unsigned char *raw = (unsigned char *)natptr;
492 bfd_h_put_32(abfd, g->address, raw);
493 /* Find a type in the output format which matches the input howto -
494 * at the moment we assume input format == output format FIXME!!
496 /* FIXME: Need callj stuff here, and to check the howto entries to
497 be sure they are real for this architecture. */
498 if (g->howto== &howto_reloc_callj) {
499 raw[7] = callj_mask + pcrel_mask + len_2;
501 else if (g->howto == &howto_reloc_pcrel24) {
502 raw[7] = pcrel_mask +len_2;
507 if (g->sym_ptr_ptr != (asymbol **)NULL)
509 /* name clobbered by aout_write_syms to be symbol index*/
510 if ((*(g->sym_ptr_ptr))->section) {
511 /* replace the section offset into the addent */
512 g->addend += (*(g->sym_ptr_ptr))->section->vma ;
514 symnum = stoi((*(g->sym_ptr_ptr))->name);
515 raw[7] |= extern_mask;
516 BFD_ASSERT(g->addend == 0);
519 if (g->section == (asection *)NULL) {
523 else if(g->section->output_section == obj_textsec(abfd)) {
525 BFD_ASSERT(g->addend + obj_textsec(abfd)->vma == 0);
527 else if (g->section->output_section == obj_datasec(abfd)) {
529 BFD_ASSERT(g->addend + obj_datasec(abfd)->vma == 0);
531 else if (g->section->output_section == obj_bsssec(abfd)) {
533 BFD_ASSERT(g->addend + obj_bsssec(abfd)->vma == 0);
540 if (abfd->xvec->header_byteorder_big_p) {
541 raw[4] = (unsigned char) (symnum >> 16);
542 raw[5] = (unsigned char) (symnum >> 8);
543 raw[6] = (unsigned char) (symnum );
545 raw[6] = (unsigned char) (symnum >> 16);
546 raw[5] = (unsigned char) (symnum >> 8);
547 raw[4] = (unsigned char) (symnum );
551 if (bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
560 /* This is stupid. This function should be a boolean predicate */
562 b_out_canonicalize_reloc (abfd, section, relptr, symbols)
568 arelent *tblptr = section->relocation;
569 unsigned int count = 0;
571 if (!(tblptr || b_out_slurp_reloc_table (abfd, section, symbols))) return 0;
572 tblptr = section->relocation;
573 if (!tblptr) return 0;
575 for (; count++ < section->reloc_count;)
576 *relptr++ = tblptr++;
580 return section->reloc_count;
584 b_out_get_reloc_upper_bound (abfd, asect)
588 if (bfd_get_format (abfd) != bfd_object) {
589 bfd_error = invalid_operation;
593 if (asect == obj_datasec (abfd))
594 return (sizeof (arelent *) *
595 ((exec_hdr(abfd)->a_drsize / sizeof (struct relocation_info))
598 if (asect == obj_textsec (abfd))
599 return (sizeof (arelent *) *
600 ((exec_hdr(abfd)->a_trsize / sizeof (struct relocation_info))
603 bfd_error = invalid_operation;
608 b_out_set_section_contents (abfd, section, location, offset, count)
611 unsigned char *location;
615 if (abfd->output_has_begun == false) { /* set by bfd.c handler */
616 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL) /*||
617 (obj_textsec (abfd)->size == 0) || (obj_datasec (abfd)->size == 0)*/) {
618 bfd_error = invalid_operation;
622 obj_textsec (abfd)->filepos = sizeof(struct internal_exec);
623 obj_datasec(abfd)->filepos = obj_textsec(abfd)->filepos
624 + obj_textsec (abfd)->size;
627 /* regardless, once we know what we're doing, we might as well get going */
628 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
631 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
637 b_out_set_arch_mach (abfd, arch, machine)
639 enum bfd_architecture arch;
640 unsigned long machine;
642 abfd->obj_arch = arch;
643 abfd->obj_machine = machine;
644 if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */
646 if (arch == bfd_arch_i960) /* i960 default is OK */
648 case bfd_mach_i960_core:
649 case bfd_mach_i960_kb_sb:
650 case bfd_mach_i960_mc:
651 case bfd_mach_i960_xa:
652 case bfd_mach_i960_ca:
653 case bfd_mach_i960_ka_sa:
664 DEFUN(b_out_sizeof_headers,(ignore_abfd, ignore),
668 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_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 */
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_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},