1 /* BFD back-end for Intel 960 b.out binaries.
2 Copyright 1990, 1991, 1992, 1993 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. */
28 #include "aout/stab_gnu.h"
29 #include "libaout.h" /* BFD a.out internal data structures */
32 static boolean b_out_squirt_out_relocs
PARAMS ((bfd
*abfd
, asection
*section
));
33 static bfd_target
*b_out_callback
PARAMS ((bfd
*));
34 static bfd_reloc_status_type calljx_callback
35 PARAMS ((bfd
*, struct bfd_link_info
*, arelent
*, PTR src
, PTR dst
,
37 static bfd_reloc_status_type callj_callback
38 PARAMS ((bfd
*, struct bfd_link_info
*, arelent
*, PTR data
,
39 unsigned int srcidx
, unsigned int dstidx
, asection
*));
40 static bfd_vma get_value
PARAMS ((arelent
*, struct bfd_link_info
*,
42 static int abs32code
PARAMS ((asection
*, asymbol
**, arelent
*,
43 unsigned int, struct bfd_link_info
*));
44 static boolean b_out_relax_section
PARAMS ((bfd
*, asection
*,
45 struct bfd_link_info
*,
47 static bfd_byte
*b_out_get_relocated_section_contents
48 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
49 bfd_byte
*, boolean
, asymbol
**));
51 /* Swaps the information in an executable header taken from a raw byte
52 stream memory image, into the internal exec_header structure. */
55 DEFUN(bout_swap_exec_header_in
,(abfd
, raw_bytes
, execp
),
57 struct external_exec
*raw_bytes AND
58 struct internal_exec
*execp
)
60 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
62 /* Now fill in fields in the execp, from the bytes in the raw data. */
63 execp
->a_info
= bfd_h_get_32 (abfd
, bytes
->e_info
);
64 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
65 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
66 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
67 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
68 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
69 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
70 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
71 execp
->a_tload
= GET_WORD (abfd
, bytes
->e_tload
);
72 execp
->a_dload
= GET_WORD (abfd
, bytes
->e_dload
);
73 execp
->a_talign
= bytes
->e_talign
[0];
74 execp
->a_dalign
= bytes
->e_dalign
[0];
75 execp
->a_balign
= bytes
->e_balign
[0];
76 execp
->a_relaxable
= bytes
->e_relaxable
[0];
79 /* Swaps the information in an internal exec header structure into the
80 supplied buffer ready for writing to disk. */
82 PROTO(void, bout_swap_exec_header_out
,
84 struct internal_exec
*execp
,
85 struct external_exec
*raw_bytes
));
87 DEFUN(bout_swap_exec_header_out
,(abfd
, execp
, raw_bytes
),
89 struct internal_exec
*execp AND
90 struct external_exec
*raw_bytes
)
92 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
94 /* Now fill in fields in the raw data, from the fields in the exec struct. */
95 bfd_h_put_32 (abfd
, execp
->a_info
, bytes
->e_info
);
96 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
97 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
98 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
99 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
100 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
101 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
102 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
103 PUT_WORD (abfd
, execp
->a_tload
, bytes
->e_tload
);
104 PUT_WORD (abfd
, execp
->a_dload
, bytes
->e_dload
);
105 bytes
->e_talign
[0] = execp
->a_talign
;
106 bytes
->e_dalign
[0] = execp
->a_dalign
;
107 bytes
->e_balign
[0] = execp
->a_balign
;
108 bytes
->e_relaxable
[0] = execp
->a_relaxable
;
113 b_out_object_p (abfd
)
116 struct internal_exec anexec
;
117 struct external_exec exec_bytes
;
119 if (bfd_read ((PTR
) &exec_bytes
, 1, EXEC_BYTES_SIZE
, abfd
)
120 != EXEC_BYTES_SIZE
) {
121 bfd_set_error (bfd_error_wrong_format
);
125 anexec
.a_info
= bfd_h_get_32 (abfd
, exec_bytes
.e_info
);
127 if (N_BADMAG (anexec
)) {
128 bfd_set_error (bfd_error_wrong_format
);
132 bout_swap_exec_header_in (abfd
, &exec_bytes
, &anexec
);
133 return aout_32_some_aout_object_p (abfd
, &anexec
, b_out_callback
);
137 /* Finish up the opening of a b.out file for reading. Fill in all the
138 fields that are not handled by common code. */
141 b_out_callback (abfd
)
144 struct internal_exec
*execp
= exec_hdr (abfd
);
145 unsigned long bss_start
;
147 /* Architecture and machine type */
148 bfd_set_arch_mach(abfd
,
149 bfd_arch_i960
, /* B.out only used on i960 */
150 bfd_mach_i960_core
/* Default */
153 /* The positions of the string table and symbol table. */
154 obj_str_filepos (abfd
) = N_STROFF (*execp
);
155 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
157 /* The alignments of the sections */
158 obj_textsec (abfd
)->alignment_power
= execp
->a_talign
;
159 obj_datasec (abfd
)->alignment_power
= execp
->a_dalign
;
160 obj_bsssec (abfd
)->alignment_power
= execp
->a_balign
;
162 /* The starting addresses of the sections. */
163 obj_textsec (abfd
)->vma
= execp
->a_tload
;
164 obj_datasec (abfd
)->vma
= execp
->a_dload
;
166 /* And reload the sizes, since the aout module zaps them */
167 obj_textsec (abfd
)->_raw_size
= execp
->a_text
;
169 bss_start
= execp
->a_dload
+ execp
->a_data
; /* BSS = end of data section */
170 obj_bsssec (abfd
)->vma
= align_power (bss_start
, execp
->a_balign
);
172 /* The file positions of the sections */
173 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
174 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
176 /* The file positions of the relocation info */
177 obj_textsec (abfd
)->rel_filepos
= N_TROFF(*execp
);
178 obj_datasec (abfd
)->rel_filepos
= N_DROFF(*execp
);
180 adata(abfd
).page_size
= 1; /* Not applicable. */
181 adata(abfd
).segment_size
= 1; /* Not applicable. */
182 adata(abfd
).exec_bytes_size
= EXEC_BYTES_SIZE
;
184 if (execp
->a_relaxable
)
185 abfd
->flags
|= BFD_IS_RELAXABLE
;
189 struct bout_data_struct
{
191 struct internal_exec e
;
195 b_out_mkobject (abfd
)
198 struct bout_data_struct
*rawptr
;
200 rawptr
= (struct bout_data_struct
*) bfd_zalloc (abfd
, sizeof (struct bout_data_struct
));
201 if (rawptr
== NULL
) {
202 bfd_set_error (bfd_error_no_memory
);
206 abfd
->tdata
.bout_data
= rawptr
;
207 exec_hdr (abfd
) = &rawptr
->e
;
209 /* For simplicity's sake we just make all the sections right here. */
210 obj_textsec (abfd
) = (asection
*)NULL
;
211 obj_datasec (abfd
) = (asection
*)NULL
;
212 obj_bsssec (abfd
) = (asection
*)NULL
;
214 bfd_make_section (abfd
, ".text");
215 bfd_make_section (abfd
, ".data");
216 bfd_make_section (abfd
, ".bss");
222 b_out_write_object_contents (abfd
)
225 struct external_exec swapped_hdr
;
227 exec_hdr (abfd
)->a_info
= BMAGIC
;
229 exec_hdr (abfd
)->a_text
= obj_textsec (abfd
)->_raw_size
;
230 exec_hdr (abfd
)->a_data
= obj_datasec (abfd
)->_raw_size
;
231 exec_hdr (abfd
)->a_bss
= obj_bsssec (abfd
)->_raw_size
;
232 exec_hdr (abfd
)->a_syms
= bfd_get_symcount (abfd
) * sizeof (struct nlist
);
233 exec_hdr (abfd
)->a_entry
= bfd_get_start_address (abfd
);
234 exec_hdr (abfd
)->a_trsize
= ((obj_textsec (abfd
)->reloc_count
) *
235 sizeof (struct relocation_info
));
236 exec_hdr (abfd
)->a_drsize
= ((obj_datasec (abfd
)->reloc_count
) *
237 sizeof (struct relocation_info
));
239 exec_hdr (abfd
)->a_talign
= obj_textsec (abfd
)->alignment_power
;
240 exec_hdr (abfd
)->a_dalign
= obj_datasec (abfd
)->alignment_power
;
241 exec_hdr (abfd
)->a_balign
= obj_bsssec (abfd
)->alignment_power
;
243 exec_hdr (abfd
)->a_tload
= obj_textsec (abfd
)->vma
;
244 exec_hdr (abfd
)->a_dload
= obj_datasec (abfd
)->vma
;
246 bout_swap_exec_header_out (abfd
, exec_hdr (abfd
), &swapped_hdr
);
248 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
249 bfd_write ((PTR
) &swapped_hdr
, 1, EXEC_BYTES_SIZE
, abfd
);
251 /* Now write out reloc info, followed by syms and strings */
252 if (bfd_get_symcount (abfd
) != 0)
254 bfd_seek (abfd
, (file_ptr
)(N_SYMOFF(*exec_hdr(abfd
))), SEEK_SET
);
256 if (! aout_32_write_syms (abfd
))
259 bfd_seek (abfd
, (file_ptr
)(N_TROFF(*exec_hdr(abfd
))), SEEK_SET
);
261 if (!b_out_squirt_out_relocs (abfd
, obj_textsec (abfd
))) return false;
262 bfd_seek (abfd
, (file_ptr
)(N_DROFF(*exec_hdr(abfd
))), SEEK_SET
);
264 if (!b_out_squirt_out_relocs (abfd
, obj_datasec (abfd
))) return false;
269 /** Some reloc hackery */
271 #define CALLS 0x66003800 /* Template for 'calls' instruction */
272 #define BAL 0x0b000000 /* Template for 'bal' instruction */
273 #define BALX 0x85000000 /* Template for 'balx' instruction */
274 #define BAL_MASK 0x00ffffff
275 #define CALL 0x09000000
276 #define PCREL13_MASK 0x1fff
279 #define output_addr(sec) ((sec)->output_offset+(sec)->output_section->vma)
281 /* Magic to turn callx into calljx */
282 static bfd_reloc_status_type
283 calljx_callback (abfd
, link_info
, reloc_entry
, src
, dst
, input_section
)
285 struct bfd_link_info
*link_info
;
286 arelent
*reloc_entry
;
289 asection
*input_section
;
291 int word
= bfd_get_32 (abfd
, src
);
292 asymbol
*symbol_in
= *(reloc_entry
->sym_ptr_ptr
);
293 aout_symbol_type
*symbol
= aout_symbol (symbol_in
);
296 value
= get_value (reloc_entry
, link_info
, input_section
);
298 if (IS_CALLNAME (symbol
->other
))
300 aout_symbol_type
*balsym
= symbol
+1;
301 int inst
= bfd_get_32 (abfd
, (bfd_byte
*) src
-4);
302 /* The next symbol should be an N_BALNAME */
303 BFD_ASSERT (IS_BALNAME (balsym
->other
));
306 bfd_put_32 (abfd
, inst
, (bfd_byte
*) dst
-4);
308 value
= (symbol
->symbol
.value
309 + output_addr (symbol
->symbol
.section
));
312 word
+= value
+ reloc_entry
->addend
;
314 bfd_put_32(abfd
, word
, dst
);
319 /* Magic to turn call into callj */
320 static bfd_reloc_status_type
321 callj_callback (abfd
, link_info
, reloc_entry
, data
, srcidx
, dstidx
,
324 struct bfd_link_info
*link_info
;
325 arelent
*reloc_entry
;
329 asection
*input_section
;
331 int word
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ srcidx
);
332 asymbol
*symbol_in
= *(reloc_entry
->sym_ptr_ptr
);
333 aout_symbol_type
*symbol
= aout_symbol (symbol_in
);
336 value
= get_value (reloc_entry
, link_info
, input_section
);
338 if (IS_OTHER(symbol
->other
))
340 /* Call to a system procedure - replace code with system
342 word
= CALLS
| (symbol
->other
- 1);
344 else if (IS_CALLNAME(symbol
->other
))
346 aout_symbol_type
*balsym
= symbol
+1;
348 /* The next symbol should be an N_BALNAME. */
349 BFD_ASSERT(IS_BALNAME(balsym
->other
));
351 /* We are calling a leaf, so replace the call instruction with a
354 + output_addr (balsym
->symbol
.section
)
355 + balsym
->symbol
.value
+ reloc_entry
->addend
357 - output_addr (input_section
))
362 word
= CALL
| (((word
& BAL_MASK
)
364 + reloc_entry
->addend
366 - output_addr (input_section
))
369 bfd_put_32(abfd
, word
, (bfd_byte
*) data
+ dstidx
);
373 /* type rshift size bitsize pcrel bitpos absolute overflow check*/
376 #define ABS32CODE_SHRUNK 1
381 #define ABS32_MAYBE_RELAXABLE 1
382 #define ABS32_WAS_RELAXABLE 2
386 static reloc_howto_type howto_reloc_callj
=
387 HOWTO(CALLJ
, 0, 2, 24, true, 0, complain_overflow_signed
, 0,"callj", true, 0x00ffffff, 0x00ffffff,false);
388 static reloc_howto_type howto_reloc_abs32
=
389 HOWTO(ABS32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"abs32", true, 0xffffffff,0xffffffff,false);
390 static reloc_howto_type howto_reloc_pcrel24
=
391 HOWTO(PCREL24
, 0, 2, 24, true, 0, complain_overflow_signed
,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);
393 static reloc_howto_type howto_reloc_pcrel13
=
394 HOWTO(PCREL13
, 0, 2, 13, true, 0, complain_overflow_signed
,0,"pcrel13", true, 0x00001fff,0x00001fff,false);
397 static reloc_howto_type howto_reloc_abs32codeshrunk
=
398 HOWTO(ABS32CODE_SHRUNK
, 0, 2, 24, true, 0, complain_overflow_signed
, 0,"callx->callj", true, 0x00ffffff, 0x00ffffff,false);
400 static reloc_howto_type howto_reloc_abs32code
=
401 HOWTO(ABS32CODE
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"callx", true, 0xffffffff,0xffffffff,false);
403 static reloc_howto_type howto_align_table
[] = {
404 HOWTO (ALIGNER
, 0, 0x1, 0, false, 0, complain_overflow_dont
, 0, "align16", false, 0, 0, false),
405 HOWTO (ALIGNER
, 0, 0x3, 0, false, 0, complain_overflow_dont
, 0, "align32", false, 0, 0, false),
406 HOWTO (ALIGNER
, 0, 0x7, 0, false, 0, complain_overflow_dont
, 0, "align64", false, 0, 0, false),
407 HOWTO (ALIGNER
, 0, 0xf, 0, false, 0, complain_overflow_dont
, 0, "align128", false, 0, 0, false),
410 static reloc_howto_type howto_done_align_table
[] = {
411 HOWTO (ALIGNDONE
, 0x1, 0x1, 0, false, 0, complain_overflow_dont
, 0, "donealign16", false, 0, 0, false),
412 HOWTO (ALIGNDONE
, 0x3, 0x3, 0, false, 0, complain_overflow_dont
, 0, "donealign32", false, 0, 0, false),
413 HOWTO (ALIGNDONE
, 0x7, 0x7, 0, false, 0, complain_overflow_dont
, 0, "donealign64", false, 0, 0, false),
414 HOWTO (ALIGNDONE
, 0xf, 0xf, 0, false, 0, complain_overflow_dont
, 0, "donealign128", false, 0, 0, false),
417 static const reloc_howto_type
*
418 b_out_reloc_type_lookup (abfd
, code
)
420 bfd_reloc_code_real_type code
;
426 case BFD_RELOC_I960_CALLJ
:
427 return &howto_reloc_callj
;
429 return &howto_reloc_abs32
;
430 case BFD_RELOC_24_PCREL
:
431 return &howto_reloc_pcrel24
;
435 /* Allocate enough room for all the reloc entries, plus pointers to them all */
438 b_out_slurp_reloc_table (abfd
, asect
, symbols
)
443 register struct relocation_info
*rptr
;
444 unsigned int counter
;
446 int extern_mask
, pcrel_mask
, callj_mask
, length_shift
;
449 bfd_vma prev_addr
= 0;
452 struct relocation_info
*relocs
;
453 arelent
*reloc_cache
;
455 if (asect
->relocation
) return true;
456 if (!aout_32_slurp_symbol_table (abfd
)) return false;
458 if (asect
== obj_datasec (abfd
)) {
459 reloc_size
= exec_hdr(abfd
)->a_drsize
;
463 if (asect
== obj_textsec (abfd
)) {
464 reloc_size
= exec_hdr(abfd
)->a_trsize
;
468 bfd_set_error (bfd_error_invalid_operation
);
472 bfd_seek (abfd
, (file_ptr
)(asect
->rel_filepos
), SEEK_SET
);
473 count
= reloc_size
/ sizeof (struct relocation_info
);
475 relocs
= (struct relocation_info
*) malloc (reloc_size
);
477 bfd_set_error (bfd_error_no_memory
);
480 reloc_cache
= (arelent
*) malloc ((count
+1) * sizeof (arelent
));
482 free ((char*)relocs
);
483 bfd_set_error (bfd_error_no_memory
);
487 if (bfd_read ((PTR
) relocs
, 1, reloc_size
, abfd
) != reloc_size
) {
488 bfd_set_error (bfd_error_system_call
);
496 if (abfd
->xvec
->header_byteorder_big_p
) {
497 /* big-endian bit field allocation order */
505 /* little-endian bit field allocation order */
514 for (rptr
= relocs
, cache_ptr
= reloc_cache
, counter
= 0;
516 counter
++, rptr
++, cache_ptr
++)
518 unsigned char *raw
= (unsigned char *)rptr
;
520 cache_ptr
->address
= bfd_h_get_32 (abfd
, raw
+ 0);
521 cache_ptr
->howto
= 0;
522 if (abfd
->xvec
->header_byteorder_big_p
)
524 symnum
= (raw
[4] << 16) | (raw
[5] << 8) | raw
[6];
528 symnum
= (raw
[6] << 16) | (raw
[5] << 8) | raw
[4];
531 if (raw
[7] & extern_mask
)
533 /* if this is set then the r_index is a index into the symbol table;
534 * if the bit is not set then r_index contains a section map.
535 * we either fill in the sym entry with a pointer to the symbol,
536 * or point to the correct section
538 cache_ptr
->sym_ptr_ptr
= symbols
+ symnum
;
539 cache_ptr
->addend
= 0;
542 /* in a.out symbols are relative to the beginning of the
543 * file rather than sections ?
544 * (look in translate_from_native_sym_flags)
545 * the reloc entry addend has added to it the offset into the
546 * file of the data, so subtract the base to make the reloc
547 * section relative */
550 /* sign-extend symnum from 24 bits to whatever host uses */
555 cache_ptr
->sym_ptr_ptr
= (asymbol
**)NULL
;
560 cache_ptr
->sym_ptr_ptr
= obj_textsec(abfd
)->symbol_ptr_ptr
;
561 cache_ptr
->addend
= - obj_textsec(abfd
)->vma
;
565 cache_ptr
->sym_ptr_ptr
= obj_datasec(abfd
)->symbol_ptr_ptr
;
566 cache_ptr
->addend
= - obj_datasec(abfd
)->vma
;
570 cache_ptr
->sym_ptr_ptr
= obj_bsssec(abfd
)->symbol_ptr_ptr
;
571 cache_ptr
->addend
= - obj_bsssec(abfd
)->vma
;
575 cache_ptr
->sym_ptr_ptr
= obj_bsssec(abfd
)->symbol_ptr_ptr
;
576 cache_ptr
->addend
= 0;
578 case -2: /* .align */
579 if (raw
[7] & pcrel_mask
)
581 cache_ptr
->howto
= &howto_align_table
[(raw
[7] >> length_shift
) & 3];
582 cache_ptr
->sym_ptr_ptr
= &bfd_abs_symbol
;
589 cache_ptr
->addend
= 0;
598 /* the i960 only has a few relocation types:
599 abs 32-bit and pcrel 24bit. except for callj's! */
600 if (cache_ptr
->howto
!= 0)
602 else if (raw
[7] & callj_mask
)
604 cache_ptr
->howto
= &howto_reloc_callj
;
606 else if ( raw
[7] & pcrel_mask
)
608 if (raw
[7] & size_mask
)
609 cache_ptr
->howto
= &howto_reloc_pcrel13
;
611 cache_ptr
->howto
= &howto_reloc_pcrel24
;
615 if (raw
[7] & incode_mask
)
617 cache_ptr
->howto
= &howto_reloc_abs32code
;
621 cache_ptr
->howto
= &howto_reloc_abs32
;
624 if (cache_ptr
->address
< prev_addr
)
626 /* Ouch! this reloc is out of order, insert into the right place
629 arelent
*cursor
= cache_ptr
-1;
630 bfd_vma stop
= cache_ptr
->address
;
632 while (cursor
->address
> stop
&& cursor
>= reloc_cache
)
634 cursor
[1] = cursor
[0];
641 prev_addr
= cache_ptr
->address
;
647 asect
->relocation
= reloc_cache
;
648 asect
->reloc_count
= count
;
656 b_out_squirt_out_relocs (abfd
, section
)
665 unsigned int count
= section
->reloc_count
;
666 struct relocation_info
*native
, *natptr
;
667 size_t natsize
= count
* sizeof (struct relocation_info
);
668 int extern_mask
, pcrel_mask
, len_2
, callj_mask
;
669 if (count
== 0) return true;
670 generic
= section
->orelocation
;
671 native
= ((struct relocation_info
*) malloc (natsize
));
673 bfd_set_error (bfd_error_no_memory
);
677 if (abfd
->xvec
->header_byteorder_big_p
)
679 /* Big-endian bit field allocation order */
689 /* Little-endian bit field allocation order */
698 for (natptr
= native
; count
> 0; --count
, ++natptr
, ++generic
)
700 arelent
*g
= *generic
;
701 unsigned char *raw
= (unsigned char *)natptr
;
702 asymbol
*sym
= *(g
->sym_ptr_ptr
);
704 asection
*output_section
= sym
->section
->output_section
;
706 bfd_h_put_32(abfd
, g
->address
, raw
);
707 /* Find a type in the output format which matches the input howto -
708 * at the moment we assume input format == output format FIXME!!
711 /* FIXME: Need callj stuff here, and to check the howto entries to
712 be sure they are real for this architecture. */
713 if (g
->howto
== &howto_reloc_callj
)
715 raw
[7] = callj_mask
+ pcrel_mask
+ len_2
;
717 else if (g
->howto
== &howto_reloc_pcrel24
)
719 raw
[7] = pcrel_mask
+ len_2
;
721 else if (g
->howto
== &howto_reloc_pcrel13
)
723 raw
[7] = pcrel_mask
+ len_1
;
725 else if (g
->howto
== &howto_reloc_abs32code
)
727 raw
[7] = len_2
+ incode_mask
;
729 else if (g
->howto
>= howto_align_table
730 && g
->howto
<= (howto_align_table
731 + sizeof (howto_align_table
) / sizeof (howto_align_table
[0])
734 /* symnum == -2; extern_mask not set, pcrel_mask set */
738 | ((g
->howto
- howto_align_table
) << 1));
745 /* already mucked with r_extern, r_idx */;
746 else if (bfd_is_com_section (output_section
)
747 || output_section
== &bfd_abs_section
748 || output_section
== &bfd_und_section
)
751 if (bfd_abs_section
.symbol
== sym
)
753 /* Whoops, looked like an abs symbol, but is really an offset
754 from the abs section */
763 r_idx
= stoi((*(g
->sym_ptr_ptr
))->flags
);
768 /* Just an ordinary section */
770 r_idx
= output_section
->target_index
;
773 if (abfd
->xvec
->header_byteorder_big_p
) {
774 raw
[4] = (unsigned char) (r_idx
>> 16);
775 raw
[5] = (unsigned char) (r_idx
>> 8);
776 raw
[6] = (unsigned char) (r_idx
);
778 raw
[6] = (unsigned char) (r_idx
>> 16);
779 raw
[5] = (unsigned char) (r_idx
>> 8);
780 raw
[4] = (unsigned char) (r_idx
);
783 raw
[7] |= extern_mask
;
786 if (bfd_write ((PTR
) native
, 1, natsize
, abfd
) != natsize
) {
795 /* This is stupid. This function should be a boolean predicate */
797 b_out_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
803 arelent
*tblptr
= section
->relocation
;
804 unsigned int count
= 0;
806 if (!(tblptr
|| b_out_slurp_reloc_table (abfd
, section
, symbols
))) return 0;
807 tblptr
= section
->relocation
;
808 if (!tblptr
) return 0;
810 for (; count
++ < section
->reloc_count
;)
811 *relptr
++ = tblptr
++;
815 return section
->reloc_count
;
819 b_out_get_reloc_upper_bound (abfd
, asect
)
823 if (bfd_get_format (abfd
) != bfd_object
) {
824 bfd_set_error (bfd_error_invalid_operation
);
828 if (asect
== obj_datasec (abfd
))
829 return (sizeof (arelent
*) *
830 ((exec_hdr(abfd
)->a_drsize
/ sizeof (struct relocation_info
))
833 if (asect
== obj_textsec (abfd
))
834 return (sizeof (arelent
*) *
835 ((exec_hdr(abfd
)->a_trsize
/ sizeof (struct relocation_info
))
838 if (asect
== obj_bsssec (abfd
))
841 bfd_set_error (bfd_error_invalid_operation
);
846 b_out_set_section_contents (abfd
, section
, location
, offset
, count
)
849 unsigned char *location
;
854 if (abfd
->output_has_begun
== false) { /* set by bfd.c handler */
855 if ((obj_textsec (abfd
) == NULL
) || (obj_datasec (abfd
) == NULL
) /*||
856 (obj_textsec (abfd)->_cooked_size == 0) || (obj_datasec (abfd)->_cooked_size == 0)*/) {
857 bfd_set_error (bfd_error_invalid_operation
);
861 obj_textsec (abfd
)->filepos
= sizeof(struct internal_exec
);
862 obj_datasec(abfd
)->filepos
= obj_textsec(abfd
)->filepos
863 + obj_textsec (abfd
)->_raw_size
;
866 /* regardless, once we know what we're doing, we might as well get going */
867 bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
);
870 return (bfd_write ((PTR
)location
, 1, count
, abfd
) == count
) ?true:false;
876 b_out_set_arch_mach (abfd
, arch
, machine
)
878 enum bfd_architecture arch
;
879 unsigned long machine
;
881 bfd_default_set_arch_mach(abfd
, arch
, machine
);
883 if (arch
== bfd_arch_unknown
) /* Unknown machine arch is OK */
885 if (arch
== bfd_arch_i960
) /* i960 default is OK */
887 case bfd_mach_i960_core
:
888 case bfd_mach_i960_kb_sb
:
889 case bfd_mach_i960_mc
:
890 case bfd_mach_i960_xa
:
891 case bfd_mach_i960_ca
:
892 case bfd_mach_i960_ka_sa
:
903 DEFUN(b_out_sizeof_headers
,(ignore_abfd
, ignore
),
907 return sizeof(struct internal_exec
);
912 /************************************************************************/
914 get_value (reloc
, link_info
, input_section
)
916 struct bfd_link_info
*link_info
;
917 asection
*input_section
;
920 asymbol
*symbol
= *(reloc
->sym_ptr_ptr
);
922 /* A symbol holds a pointer to a section, and an offset from the
923 base of the section. To relocate, we find where the section will
924 live in the output and add that in */
926 if (symbol
->section
== &bfd_und_section
)
928 struct bfd_link_hash_entry
*h
;
930 /* The symbol is undefined in this BFD. Look it up in the
931 global linker hash table. FIXME: This should be changed when
932 we convert b.out to use a specific final_link function and
933 change the interface to bfd_relax_section to not require the
935 h
= bfd_link_hash_lookup (link_info
->hash
, bfd_asymbol_name (symbol
),
937 if (h
!= (struct bfd_link_hash_entry
*) NULL
938 && h
->type
== bfd_link_hash_defined
)
939 value
= h
->u
.def
.value
+ output_addr (h
->u
.def
.section
);
940 else if (h
!= (struct bfd_link_hash_entry
*) NULL
941 && h
->type
== bfd_link_hash_common
)
945 if (! ((*link_info
->callbacks
->undefined_symbol
)
946 (link_info
, bfd_asymbol_name (symbol
),
947 input_section
->owner
, input_section
, reloc
->address
)))
954 value
= symbol
->value
+ output_addr (symbol
->section
);
957 /* Add the value contained in the relocation */
958 value
+= reloc
->addend
;
964 DEFUN(perform_slip
,(s
, slip
, input_section
, value
),
966 unsigned int slip AND
967 asection
*input_section AND
971 /* Find all symbols past this point, and make them know
976 if (p
->section
== input_section
)
978 /* This was pointing into this section, so mangle it */
979 if (p
->value
> value
)
989 /* This routine works out if the thing we want to get to can be
990 reached with a 24bit offset instead of a 32 bit one.
991 If it can, then it changes the amode */
994 abs32code (input_section
, symbols
, r
, shrink
, link_info
)
995 asection
*input_section
;
999 struct bfd_link_info
*link_info
;
1001 bfd_vma value
= get_value (r
, link_info
, input_section
);
1002 bfd_vma dot
= output_addr (input_section
) + r
->address
;
1005 /* See if the address we're looking at within 2^23 bytes of where
1006 we are, if so then we can use a small branch rather than the
1007 jump we were going to */
1009 gap
= value
- (dot
- shrink
);
1012 if (-1<<23 < (long)gap
&& (long)gap
< 1<<23 )
1014 /* Change the reloc type from 32bitcode possible 24, to 24bit
1017 r
->howto
= &howto_reloc_abs32codeshrunk
;
1018 /* The place to relc moves back by four bytes */
1021 /* This will be four bytes smaller in the long run */
1023 perform_slip(symbols
, 4, input_section
, r
->address
-shrink
+4);
1029 DEFUN(aligncode
,(input_section
, symbols
, r
, shrink
),
1030 asection
*input_section AND
1031 asymbol
**symbols AND
1033 unsigned int shrink
)
1035 bfd_vma dot
= output_addr (input_section
) + r
->address
;
1040 int size
= r
->howto
->size
;
1042 /* Reduce the size of the alignment so that it's still aligned but
1043 smaller - the current size is already the same size as or bigger
1044 than the alignment required. */
1046 /* calculate the first byte following the padding before we optimize */
1047 old_end
= ((dot
+ size
) & ~size
) + size
+1;
1048 /* work out where the new end will be - remember that we're smaller
1049 than we used to be */
1050 new_end
= ((dot
- shrink
+ size
) & ~size
);
1052 /* This is the new end */
1053 gap
= old_end
- ((dot
+ size
) & ~size
);
1055 shrink_delta
= (old_end
- new_end
) - shrink
;
1059 /* Change the reloc so that it knows how far to align to */
1060 r
->howto
= howto_done_align_table
+ (r
->howto
- howto_align_table
);
1062 /* Encode the stuff into the addend - for future use we need to
1063 know how big the reloc used to be */
1064 r
->addend
= old_end
- dot
+ r
->address
;
1066 /* This will be N bytes smaller in the long run, adjust all the symbols */
1067 perform_slip(symbols
, shrink_delta
, input_section
, r
->address
- shrink
);
1068 shrink
+= shrink_delta
;
1074 b_out_relax_section (abfd
, i
, link_info
, symbols
)
1077 struct bfd_link_info
*link_info
;
1081 /* Get enough memory to hold the stuff */
1082 bfd
*input_bfd
= i
->owner
;
1083 asection
*input_section
= i
;
1085 boolean
new = false;
1086 arelent
**reloc_vector
= NULL
;
1088 bfd_size_type reloc_size
= bfd_get_reloc_upper_bound(input_bfd
,
1093 reloc_vector
= (arelent
**) malloc (reloc_size
);
1094 if (reloc_vector
== NULL
)
1096 bfd_set_error (bfd_error_no_memory
);
1100 /* Get the relocs and think about them */
1101 if (bfd_canonicalize_reloc(input_bfd
, input_section
, reloc_vector
,
1105 for (parent
= reloc_vector
; *parent
; parent
++)
1107 arelent
*r
= *parent
;
1108 switch (r
->howto
->type
)
1111 /* An alignment reloc */
1112 shrink
= aligncode(input_section
, symbols
, r
,shrink
);
1116 /* A 32bit reloc in an addressing mode */
1117 shrink
= abs32code (input_section
, symbols
, r
, shrink
, link_info
);
1120 case ABS32CODE_SHRUNK
:
1127 input_section
->_cooked_size
= input_section
->_raw_size
- shrink
;
1129 if (reloc_vector
!= NULL
)
1130 free (reloc_vector
);
1133 if (reloc_vector
!= NULL
)
1134 free (reloc_vector
);
1139 b_out_get_relocated_section_contents (in_abfd
, link_info
, link_order
, data
,
1140 relocateable
, symbols
)
1142 struct bfd_link_info
*link_info
;
1143 struct bfd_link_order
*link_order
;
1145 boolean relocateable
;
1148 /* Get enough memory to hold the stuff */
1149 bfd
*input_bfd
= link_order
->u
.indirect
.section
->owner
;
1150 asection
*input_section
= link_order
->u
.indirect
.section
;
1151 bfd_size_type reloc_size
= bfd_get_reloc_upper_bound(input_bfd
,
1153 arelent
**reloc_vector
= NULL
;
1155 /* If producing relocateable output, don't bother to relax. */
1157 return bfd_generic_get_relocated_section_contents (in_abfd
, link_info
,
1162 reloc_vector
= (arelent
**) malloc (reloc_size
);
1163 if (reloc_vector
== NULL
)
1165 bfd_set_error (bfd_error_no_memory
);
1169 input_section
->reloc_done
= 1;
1171 /* read in the section */
1172 BFD_ASSERT (true == bfd_get_section_contents(input_bfd
,
1176 input_section
->_raw_size
));
1178 if (bfd_canonicalize_reloc(input_bfd
,
1183 arelent
**parent
= reloc_vector
;
1186 unsigned int dst_address
= 0;
1187 unsigned int src_address
= 0;
1191 /* Find how long a run we can do */
1192 while (dst_address
< link_order
->size
)
1197 /* Note that the relaxing didn't tie up the addresses in the
1198 relocation, so we use the original address to work out the
1199 run of non-relocated data */
1200 BFD_ASSERT (reloc
->address
>= src_address
);
1201 run
= reloc
->address
- src_address
;
1206 run
= link_order
->size
- dst_address
;
1208 /* Copy the bytes */
1209 for (idx
= 0; idx
< run
; idx
++)
1211 data
[dst_address
++] = data
[src_address
++];
1214 /* Now do the relocation */
1218 switch (reloc
->howto
->type
)
1221 calljx_callback (in_abfd
, link_info
, reloc
,
1222 src_address
+ data
, dst_address
+ data
,
1229 (bfd_get_32 (in_abfd
, data
+src_address
)
1230 + get_value (reloc
, link_info
, input_section
)),
1236 callj_callback (in_abfd
, link_info
, reloc
, data
, src_address
,
1237 dst_address
, input_section
);
1242 BFD_ASSERT (reloc
->addend
>= src_address
);
1243 BFD_ASSERT (reloc
->addend
<= input_section
->_raw_size
);
1244 src_address
= reloc
->addend
;
1245 dst_address
= ((dst_address
+ reloc
->howto
->size
)
1246 & ~reloc
->howto
->size
);
1248 case ABS32CODE_SHRUNK
:
1249 /* This used to be a callx, but we've found out that a
1250 callj will reach, so do the right thing. */
1251 callj_callback (in_abfd
, link_info
, reloc
, data
,
1252 src_address
+ 4, dst_address
, input_section
);
1258 long int word
= bfd_get_32(in_abfd
, data
+src_address
);
1261 value
= get_value (reloc
, link_info
, input_section
);
1262 word
= ((word
& ~BAL_MASK
)
1263 | (((word
& BAL_MASK
)
1265 - output_addr (input_section
)
1269 bfd_put_32(in_abfd
,word
, data
+dst_address
);
1278 long int word
= bfd_get_32(in_abfd
, data
+src_address
);
1281 value
= get_value (reloc
, link_info
, input_section
);
1282 word
= ((word
& ~PCREL13_MASK
)
1283 | (((word
& PCREL13_MASK
)
1286 - output_addr (input_section
))
1289 bfd_put_32(in_abfd
,word
, data
+dst_address
);
1302 if (reloc_vector
!= NULL
)
1303 free (reloc_vector
);
1306 if (reloc_vector
!= NULL
)
1307 free (reloc_vector
);
1310 /***********************************************************************/
1312 /* Build the transfer vectors for Big and Little-Endian B.OUT files. */
1314 /* We don't have core files. */
1315 #define aout_32_core_file_failing_command _bfd_dummy_core_file_failing_command
1316 #define aout_32_core_file_failing_signal _bfd_dummy_core_file_failing_signal
1317 #define aout_32_core_file_matches_executable_p \
1318 _bfd_dummy_core_file_matches_executable_p
1320 /* We use BSD-Unix generic archive files. */
1321 #define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file
1322 #define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt
1323 #define aout_32_slurp_armap bfd_slurp_bsd_armap
1324 #define aout_32_slurp_extended_name_table _bfd_slurp_extended_name_table
1325 #define aout_32_write_armap bsd_write_armap
1326 #define aout_32_truncate_arname bfd_bsd_truncate_arname
1328 /* We override these routines from the usual a.out file routines. */
1329 #define aout_32_canonicalize_reloc b_out_canonicalize_reloc
1330 #define aout_32_get_reloc_upper_bound b_out_get_reloc_upper_bound
1331 #define aout_32_set_section_contents b_out_set_section_contents
1332 #define aout_32_set_arch_mach b_out_set_arch_mach
1333 #define aout_32_sizeof_headers b_out_sizeof_headers
1335 #define aout_32_bfd_debug_info_start bfd_void
1336 #define aout_32_bfd_debug_info_end bfd_void
1337 #define aout_32_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
1339 #define aout_32_bfd_get_relocated_section_contents b_out_get_relocated_section_contents
1340 #define aout_32_bfd_relax_section b_out_relax_section
1341 #define aout_32_bfd_reloc_type_lookup b_out_reloc_type_lookup
1342 #define aout_32_bfd_make_debug_symbol \
1343 ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
1344 #define aout_32_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1345 #define aout_32_bfd_link_add_symbols _bfd_generic_link_add_symbols
1346 #define aout_32_bfd_final_link _bfd_generic_final_link
1348 bfd_target b_out_vec_big_host
=
1350 "b.out.big", /* name */
1351 bfd_target_aout_flavour
,
1352 false, /* data byte order is little */
1353 true, /* hdr byte order is big */
1354 (HAS_RELOC
| EXEC_P
| /* object flags */
1355 HAS_LINENO
| HAS_DEBUG
|
1356 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
1357 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1358 '_', /* symbol leading char */
1359 ' ', /* ar_pad_char */
1360 16, /* ar_max_namelen */
1361 2, /* minumum alignment power */
1363 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1364 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1365 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
1366 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1367 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1368 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1369 {_bfd_dummy_target
, b_out_object_p
, /* bfd_check_format */
1370 bfd_generic_archive_p
, _bfd_dummy_target
},
1371 {bfd_false
, b_out_mkobject
, /* bfd_set_format */
1372 _bfd_generic_mkarchive
, bfd_false
},
1373 {bfd_false
, b_out_write_object_contents
, /* bfd_write_contents */
1374 _bfd_write_archive_contents
, bfd_false
},
1376 JUMP_TABLE(aout_32
),
1381 bfd_target b_out_vec_little_host
=
1383 "b.out.little", /* name */
1384 bfd_target_aout_flavour
,
1385 false, /* data byte order is little */
1386 false, /* header byte order is little */
1387 (HAS_RELOC
| EXEC_P
| /* object flags */
1388 HAS_LINENO
| HAS_DEBUG
|
1389 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
1390 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1391 '_', /* symbol leading char */
1392 ' ', /* ar_pad_char */
1393 16, /* ar_max_namelen */
1394 2, /* minum align */
1395 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1396 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1397 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
1398 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1399 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1400 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
1402 {_bfd_dummy_target
, b_out_object_p
, /* bfd_check_format */
1403 bfd_generic_archive_p
, _bfd_dummy_target
},
1404 {bfd_false
, b_out_mkobject
, /* bfd_set_format */
1405 _bfd_generic_mkarchive
, bfd_false
},
1406 {bfd_false
, b_out_write_object_contents
, /* bfd_write_contents */
1407 _bfd_write_archive_contents
, bfd_false
},
1408 JUMP_TABLE(aout_32
),