1 /* BFD back-end for Intel 960 b.out binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "aout/stab_gnu.h"
30 #include "libaout.h" /* BFD a.out internal data structures */
33 static int aligncode
PARAMS ((bfd
*abfd
, asection
*input_section
,
34 arelent
*r
, unsigned int shrink
));
35 static void perform_slip
PARAMS ((bfd
*abfd
, unsigned int slip
,
36 asection
*input_section
, bfd_vma value
));
37 static boolean b_out_squirt_out_relocs
PARAMS ((bfd
*abfd
, asection
*section
));
38 static const bfd_target
*b_out_callback
PARAMS ((bfd
*));
39 static bfd_reloc_status_type calljx_callback
40 PARAMS ((bfd
*, struct bfd_link_info
*, arelent
*, PTR src
, PTR dst
,
42 static bfd_reloc_status_type callj_callback
43 PARAMS ((bfd
*, struct bfd_link_info
*, arelent
*, PTR data
,
44 unsigned int srcidx
, unsigned int dstidx
, asection
*, boolean
));
45 static bfd_vma get_value
PARAMS ((arelent
*, struct bfd_link_info
*,
47 static int abs32code
PARAMS ((bfd
*, asection
*, arelent
*,
48 unsigned int, struct bfd_link_info
*));
49 static boolean b_out_bfd_relax_section
PARAMS ((bfd
*, asection
*,
50 struct bfd_link_info
*,
52 static bfd_byte
*b_out_bfd_get_relocated_section_contents
53 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
54 bfd_byte
*, boolean
, asymbol
**));
56 /* Swaps the information in an executable header taken from a raw byte
57 stream memory image, into the internal exec_header structure. */
60 bout_swap_exec_header_in (abfd
, raw_bytes
, execp
)
62 struct external_exec
*raw_bytes
;
63 struct internal_exec
*execp
;
65 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
67 /* Now fill in fields in the execp, from the bytes in the raw data. */
68 execp
->a_info
= bfd_h_get_32 (abfd
, bytes
->e_info
);
69 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
70 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
71 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
72 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
73 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
74 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
75 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
76 execp
->a_tload
= GET_WORD (abfd
, bytes
->e_tload
);
77 execp
->a_dload
= GET_WORD (abfd
, bytes
->e_dload
);
78 execp
->a_talign
= bytes
->e_talign
[0];
79 execp
->a_dalign
= bytes
->e_dalign
[0];
80 execp
->a_balign
= bytes
->e_balign
[0];
81 execp
->a_relaxable
= bytes
->e_relaxable
[0];
84 /* Swaps the information in an internal exec header structure into the
85 supplied buffer ready for writing to disk. */
87 PROTO(void, bout_swap_exec_header_out
,
89 struct internal_exec
*execp
,
90 struct external_exec
*raw_bytes
));
92 bout_swap_exec_header_out (abfd
, execp
, raw_bytes
)
94 struct internal_exec
*execp
;
95 struct external_exec
*raw_bytes
;
97 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
99 /* Now fill in fields in the raw data, from the fields in the exec struct. */
100 bfd_h_put_32 (abfd
, execp
->a_info
, bytes
->e_info
);
101 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
102 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
103 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
104 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
105 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
106 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
107 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
108 PUT_WORD (abfd
, execp
->a_tload
, bytes
->e_tload
);
109 PUT_WORD (abfd
, execp
->a_dload
, bytes
->e_dload
);
110 bytes
->e_talign
[0] = execp
->a_talign
;
111 bytes
->e_dalign
[0] = execp
->a_dalign
;
112 bytes
->e_balign
[0] = execp
->a_balign
;
113 bytes
->e_relaxable
[0] = execp
->a_relaxable
;
117 static const bfd_target
*
118 b_out_object_p (abfd
)
121 struct internal_exec anexec
;
122 struct external_exec exec_bytes
;
124 if (bfd_read ((PTR
) &exec_bytes
, 1, EXEC_BYTES_SIZE
, abfd
)
125 != EXEC_BYTES_SIZE
) {
126 if (bfd_get_error () != bfd_error_system_call
)
127 bfd_set_error (bfd_error_wrong_format
);
131 anexec
.a_info
= bfd_h_get_32 (abfd
, exec_bytes
.e_info
);
133 if (N_BADMAG (anexec
)) {
134 bfd_set_error (bfd_error_wrong_format
);
138 bout_swap_exec_header_in (abfd
, &exec_bytes
, &anexec
);
139 return aout_32_some_aout_object_p (abfd
, &anexec
, b_out_callback
);
143 /* Finish up the opening of a b.out file for reading. Fill in all the
144 fields that are not handled by common code. */
146 static const bfd_target
*
147 b_out_callback (abfd
)
150 struct internal_exec
*execp
= exec_hdr (abfd
);
151 unsigned long bss_start
;
153 /* Architecture and machine type */
154 bfd_set_arch_mach(abfd
,
155 bfd_arch_i960
, /* B.out only used on i960 */
156 bfd_mach_i960_core
/* Default */
159 /* The positions of the string table and symbol table. */
160 obj_str_filepos (abfd
) = N_STROFF (*execp
);
161 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
163 /* The alignments of the sections */
164 obj_textsec (abfd
)->alignment_power
= execp
->a_talign
;
165 obj_datasec (abfd
)->alignment_power
= execp
->a_dalign
;
166 obj_bsssec (abfd
)->alignment_power
= execp
->a_balign
;
168 /* The starting addresses of the sections. */
169 obj_textsec (abfd
)->vma
= execp
->a_tload
;
170 obj_datasec (abfd
)->vma
= execp
->a_dload
;
172 /* And reload the sizes, since the aout module zaps them */
173 obj_textsec (abfd
)->_raw_size
= execp
->a_text
;
175 bss_start
= execp
->a_dload
+ execp
->a_data
; /* BSS = end of data section */
176 obj_bsssec (abfd
)->vma
= align_power (bss_start
, execp
->a_balign
);
178 /* The file positions of the sections */
179 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
180 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
182 /* The file positions of the relocation info */
183 obj_textsec (abfd
)->rel_filepos
= N_TROFF(*execp
);
184 obj_datasec (abfd
)->rel_filepos
= N_DROFF(*execp
);
186 adata(abfd
).page_size
= 1; /* Not applicable. */
187 adata(abfd
).segment_size
= 1; /* Not applicable. */
188 adata(abfd
).exec_bytes_size
= EXEC_BYTES_SIZE
;
190 if (execp
->a_relaxable
)
191 abfd
->flags
|= BFD_IS_RELAXABLE
;
195 struct bout_data_struct
{
197 struct internal_exec e
;
201 b_out_mkobject (abfd
)
204 struct bout_data_struct
*rawptr
;
206 rawptr
= (struct bout_data_struct
*) bfd_zalloc (abfd
, sizeof (struct bout_data_struct
));
210 abfd
->tdata
.bout_data
= rawptr
;
211 exec_hdr (abfd
) = &rawptr
->e
;
213 obj_textsec (abfd
) = (asection
*)NULL
;
214 obj_datasec (abfd
) = (asection
*)NULL
;
215 obj_bsssec (abfd
) = (asection
*)NULL
;
221 b_out_write_object_contents (abfd
)
224 struct external_exec swapped_hdr
;
226 if (! aout_32_make_sections (abfd
))
229 exec_hdr (abfd
)->a_info
= BMAGIC
;
231 exec_hdr (abfd
)->a_text
= obj_textsec (abfd
)->_raw_size
;
232 exec_hdr (abfd
)->a_data
= obj_datasec (abfd
)->_raw_size
;
233 exec_hdr (abfd
)->a_bss
= obj_bsssec (abfd
)->_raw_size
;
234 exec_hdr (abfd
)->a_syms
= bfd_get_symcount (abfd
) * sizeof (struct nlist
);
235 exec_hdr (abfd
)->a_entry
= bfd_get_start_address (abfd
);
236 exec_hdr (abfd
)->a_trsize
= ((obj_textsec (abfd
)->reloc_count
) *
237 sizeof (struct relocation_info
));
238 exec_hdr (abfd
)->a_drsize
= ((obj_datasec (abfd
)->reloc_count
) *
239 sizeof (struct relocation_info
));
241 exec_hdr (abfd
)->a_talign
= obj_textsec (abfd
)->alignment_power
;
242 exec_hdr (abfd
)->a_dalign
= obj_datasec (abfd
)->alignment_power
;
243 exec_hdr (abfd
)->a_balign
= obj_bsssec (abfd
)->alignment_power
;
245 exec_hdr (abfd
)->a_tload
= obj_textsec (abfd
)->vma
;
246 exec_hdr (abfd
)->a_dload
= obj_datasec (abfd
)->vma
;
248 bout_swap_exec_header_out (abfd
, exec_hdr (abfd
), &swapped_hdr
);
250 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
251 || (bfd_write ((PTR
) &swapped_hdr
, 1, EXEC_BYTES_SIZE
, abfd
)
255 /* Now write out reloc info, followed by syms and strings */
256 if (bfd_get_symcount (abfd
) != 0)
258 if (bfd_seek (abfd
, (file_ptr
)(N_SYMOFF(*exec_hdr(abfd
))), SEEK_SET
)
262 if (! aout_32_write_syms (abfd
))
265 if (bfd_seek (abfd
, (file_ptr
)(N_TROFF(*exec_hdr(abfd
))), SEEK_SET
) != 0)
268 if (!b_out_squirt_out_relocs (abfd
, obj_textsec (abfd
))) return false;
269 if (bfd_seek (abfd
, (file_ptr
)(N_DROFF(*exec_hdr(abfd
))), SEEK_SET
)
273 if (!b_out_squirt_out_relocs (abfd
, obj_datasec (abfd
))) return false;
278 /** Some reloc hackery */
280 #define CALLS 0x66003800 /* Template for 'calls' instruction */
281 #define BAL 0x0b000000 /* Template for 'bal' instruction */
282 #define BALX 0x85000000 /* Template for 'balx' instruction */
283 #define BAL_MASK 0x00ffffff
284 #define CALL 0x09000000
285 #define PCREL13_MASK 0x1fff
288 #define output_addr(sec) ((sec)->output_offset+(sec)->output_section->vma)
290 /* Magic to turn callx into calljx */
291 static bfd_reloc_status_type
292 calljx_callback (abfd
, link_info
, reloc_entry
, src
, dst
, input_section
)
294 struct bfd_link_info
*link_info
;
295 arelent
*reloc_entry
;
298 asection
*input_section
;
300 int word
= bfd_get_32 (abfd
, src
);
301 asymbol
*symbol_in
= *(reloc_entry
->sym_ptr_ptr
);
302 aout_symbol_type
*symbol
= aout_symbol (symbol_in
);
305 value
= get_value (reloc_entry
, link_info
, input_section
);
307 if (IS_CALLNAME (symbol
->other
))
309 aout_symbol_type
*balsym
= symbol
+1;
310 int inst
= bfd_get_32 (abfd
, (bfd_byte
*) src
-4);
311 /* The next symbol should be an N_BALNAME */
312 BFD_ASSERT (IS_BALNAME (balsym
->other
));
315 bfd_put_32 (abfd
, inst
, (bfd_byte
*) dst
-4);
317 value
= (symbol
->symbol
.value
318 + output_addr (symbol
->symbol
.section
));
321 word
+= value
+ reloc_entry
->addend
;
323 bfd_put_32(abfd
, word
, dst
);
328 /* Magic to turn call into callj */
329 static bfd_reloc_status_type
330 callj_callback (abfd
, link_info
, reloc_entry
, data
, srcidx
, dstidx
,
331 input_section
, shrinking
)
333 struct bfd_link_info
*link_info
;
334 arelent
*reloc_entry
;
338 asection
*input_section
;
341 int word
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ srcidx
);
342 asymbol
*symbol_in
= *(reloc_entry
->sym_ptr_ptr
);
343 aout_symbol_type
*symbol
= aout_symbol (symbol_in
);
346 value
= get_value (reloc_entry
, link_info
, input_section
);
348 if (IS_OTHER(symbol
->other
))
350 /* Call to a system procedure - replace code with system
352 word
= CALLS
| (symbol
->other
- 1);
354 else if (IS_CALLNAME(symbol
->other
))
356 aout_symbol_type
*balsym
= symbol
+1;
358 /* The next symbol should be an N_BALNAME. */
359 BFD_ASSERT(IS_BALNAME(balsym
->other
));
361 /* We are calling a leaf, so replace the call instruction with a
364 + output_addr (balsym
->symbol
.section
)
365 + balsym
->symbol
.value
+ reloc_entry
->addend
367 - output_addr (input_section
))
370 else if ((symbol
->symbol
.flags
& BSF_SECTION_SYM
) != 0)
372 /* A callj against a symbol in the same section is a fully
373 resolved relative call. We don't need to do anything here.
374 If the symbol is not in the same section, I'm not sure what
375 to do; fortunately, this case will probably never arise. */
376 BFD_ASSERT (! shrinking
);
377 BFD_ASSERT (symbol
->symbol
.section
== input_section
);
381 word
= CALL
| (((word
& BAL_MASK
)
383 + reloc_entry
->addend
384 - (shrinking
? dstidx
: 0)
385 - output_addr (input_section
))
388 bfd_put_32(abfd
, word
, (bfd_byte
*) data
+ dstidx
);
392 /* type rshift size bitsize pcrel bitpos absolute overflow check*/
395 #define ABS32CODE_SHRUNK 1
400 #define ABS32_MAYBE_RELAXABLE 1
401 #define ABS32_WAS_RELAXABLE 2
405 static reloc_howto_type howto_reloc_callj
=
406 HOWTO(CALLJ
, 0, 2, 24, true, 0, complain_overflow_signed
, 0,"callj", true, 0x00ffffff, 0x00ffffff,false);
407 static reloc_howto_type howto_reloc_abs32
=
408 HOWTO(ABS32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"abs32", true, 0xffffffff,0xffffffff,false);
409 static reloc_howto_type howto_reloc_pcrel24
=
410 HOWTO(PCREL24
, 0, 2, 24, true, 0, complain_overflow_signed
,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);
412 static reloc_howto_type howto_reloc_pcrel13
=
413 HOWTO(PCREL13
, 0, 2, 13, true, 0, complain_overflow_signed
,0,"pcrel13", true, 0x00001fff,0x00001fff,false);
416 static reloc_howto_type howto_reloc_abs32codeshrunk
=
417 HOWTO(ABS32CODE_SHRUNK
, 0, 2, 24, true, 0, complain_overflow_signed
, 0,"callx->callj", true, 0x00ffffff, 0x00ffffff,false);
419 static reloc_howto_type howto_reloc_abs32code
=
420 HOWTO(ABS32CODE
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"callx", true, 0xffffffff,0xffffffff,false);
422 static reloc_howto_type howto_align_table
[] = {
423 HOWTO (ALIGNER
, 0, 0x1, 0, false, 0, complain_overflow_dont
, 0, "align16", false, 0, 0, false),
424 HOWTO (ALIGNER
, 0, 0x3, 0, false, 0, complain_overflow_dont
, 0, "align32", false, 0, 0, false),
425 HOWTO (ALIGNER
, 0, 0x7, 0, false, 0, complain_overflow_dont
, 0, "align64", false, 0, 0, false),
426 HOWTO (ALIGNER
, 0, 0xf, 0, false, 0, complain_overflow_dont
, 0, "align128", false, 0, 0, false),
429 static reloc_howto_type howto_done_align_table
[] = {
430 HOWTO (ALIGNDONE
, 0x1, 0x1, 0, false, 0, complain_overflow_dont
, 0, "donealign16", false, 0, 0, false),
431 HOWTO (ALIGNDONE
, 0x3, 0x3, 0, false, 0, complain_overflow_dont
, 0, "donealign32", false, 0, 0, false),
432 HOWTO (ALIGNDONE
, 0x7, 0x7, 0, false, 0, complain_overflow_dont
, 0, "donealign64", false, 0, 0, false),
433 HOWTO (ALIGNDONE
, 0xf, 0xf, 0, false, 0, complain_overflow_dont
, 0, "donealign128", false, 0, 0, false),
436 static reloc_howto_type
*
437 b_out_bfd_reloc_type_lookup (abfd
, code
)
439 bfd_reloc_code_real_type code
;
445 case BFD_RELOC_I960_CALLJ
:
446 return &howto_reloc_callj
;
449 return &howto_reloc_abs32
;
450 case BFD_RELOC_24_PCREL
:
451 return &howto_reloc_pcrel24
;
455 /* Allocate enough room for all the reloc entries, plus pointers to them all */
458 b_out_slurp_reloc_table (abfd
, asect
, symbols
)
463 register struct relocation_info
*rptr
;
464 unsigned int counter
;
466 int extern_mask
, pcrel_mask
, callj_mask
, length_shift
;
469 bfd_vma prev_addr
= 0;
472 struct relocation_info
*relocs
;
473 arelent
*reloc_cache
;
475 if (asect
->relocation
)
477 if (!aout_32_slurp_symbol_table (abfd
))
480 if (asect
== obj_datasec (abfd
)) {
481 reloc_size
= exec_hdr(abfd
)->a_drsize
;
485 if (asect
== obj_textsec (abfd
)) {
486 reloc_size
= exec_hdr(abfd
)->a_trsize
;
490 if (asect
== obj_bsssec (abfd
)) {
495 bfd_set_error (bfd_error_invalid_operation
);
499 if (bfd_seek (abfd
, (file_ptr
)(asect
->rel_filepos
), SEEK_SET
) != 0)
501 count
= reloc_size
/ sizeof (struct relocation_info
);
503 relocs
= (struct relocation_info
*) malloc (reloc_size
);
504 if (!relocs
&& reloc_size
!= 0) {
505 bfd_set_error (bfd_error_no_memory
);
508 reloc_cache
= (arelent
*) malloc ((count
+1) * sizeof (arelent
));
511 free ((char*)relocs
);
512 bfd_set_error (bfd_error_no_memory
);
516 if (bfd_read ((PTR
) relocs
, 1, reloc_size
, abfd
) != reloc_size
) {
525 if (abfd
->xvec
->header_byteorder_big_p
) {
526 /* big-endian bit field allocation order */
534 /* little-endian bit field allocation order */
543 for (rptr
= relocs
, cache_ptr
= reloc_cache
, counter
= 0;
545 counter
++, rptr
++, cache_ptr
++)
547 unsigned char *raw
= (unsigned char *)rptr
;
549 cache_ptr
->address
= bfd_h_get_32 (abfd
, raw
+ 0);
550 cache_ptr
->howto
= 0;
551 if (abfd
->xvec
->header_byteorder_big_p
)
553 symnum
= (raw
[4] << 16) | (raw
[5] << 8) | raw
[6];
557 symnum
= (raw
[6] << 16) | (raw
[5] << 8) | raw
[4];
560 if (raw
[7] & extern_mask
)
562 /* if this is set then the r_index is a index into the symbol table;
563 * if the bit is not set then r_index contains a section map.
564 * we either fill in the sym entry with a pointer to the symbol,
565 * or point to the correct section
567 cache_ptr
->sym_ptr_ptr
= symbols
+ symnum
;
568 cache_ptr
->addend
= 0;
571 /* in a.out symbols are relative to the beginning of the
572 * file rather than sections ?
573 * (look in translate_from_native_sym_flags)
574 * the reloc entry addend has added to it the offset into the
575 * file of the data, so subtract the base to make the reloc
576 * section relative */
579 /* sign-extend symnum from 24 bits to whatever host uses */
584 cache_ptr
->sym_ptr_ptr
= (asymbol
**)NULL
;
589 cache_ptr
->sym_ptr_ptr
= obj_textsec(abfd
)->symbol_ptr_ptr
;
590 cache_ptr
->addend
= - obj_textsec(abfd
)->vma
;
594 cache_ptr
->sym_ptr_ptr
= obj_datasec(abfd
)->symbol_ptr_ptr
;
595 cache_ptr
->addend
= - obj_datasec(abfd
)->vma
;
599 cache_ptr
->sym_ptr_ptr
= obj_bsssec(abfd
)->symbol_ptr_ptr
;
600 cache_ptr
->addend
= - obj_bsssec(abfd
)->vma
;
604 cache_ptr
->sym_ptr_ptr
= obj_bsssec(abfd
)->symbol_ptr_ptr
;
605 cache_ptr
->addend
= 0;
607 case -2: /* .align */
608 if (raw
[7] & pcrel_mask
)
610 cache_ptr
->howto
= &howto_align_table
[(raw
[7] >> length_shift
) & 3];
611 cache_ptr
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
618 cache_ptr
->addend
= 0;
627 /* the i960 only has a few relocation types:
628 abs 32-bit and pcrel 24bit. except for callj's! */
629 if (cache_ptr
->howto
!= 0)
631 else if (raw
[7] & callj_mask
)
633 cache_ptr
->howto
= &howto_reloc_callj
;
635 else if ( raw
[7] & pcrel_mask
)
637 if (raw
[7] & size_mask
)
638 cache_ptr
->howto
= &howto_reloc_pcrel13
;
640 cache_ptr
->howto
= &howto_reloc_pcrel24
;
644 if (raw
[7] & incode_mask
)
646 cache_ptr
->howto
= &howto_reloc_abs32code
;
650 cache_ptr
->howto
= &howto_reloc_abs32
;
653 if (cache_ptr
->address
< prev_addr
)
655 /* Ouch! this reloc is out of order, insert into the right place
658 arelent
*cursor
= cache_ptr
-1;
659 bfd_vma stop
= cache_ptr
->address
;
661 while (cursor
->address
> stop
&& cursor
>= reloc_cache
)
663 cursor
[1] = cursor
[0];
670 prev_addr
= cache_ptr
->address
;
677 asect
->relocation
= reloc_cache
;
678 asect
->reloc_count
= count
;
686 b_out_squirt_out_relocs (abfd
, section
)
695 unsigned int count
= section
->reloc_count
;
696 struct relocation_info
*native
, *natptr
;
697 size_t natsize
= count
* sizeof (struct relocation_info
);
698 int extern_mask
, pcrel_mask
, len_2
, callj_mask
;
699 if (count
== 0) return true;
700 generic
= section
->orelocation
;
701 native
= ((struct relocation_info
*) malloc (natsize
));
702 if (!native
&& natsize
!= 0) {
703 bfd_set_error (bfd_error_no_memory
);
707 if (abfd
->xvec
->header_byteorder_big_p
)
709 /* Big-endian bit field allocation order */
719 /* Little-endian bit field allocation order */
728 for (natptr
= native
; count
> 0; --count
, ++natptr
, ++generic
)
730 arelent
*g
= *generic
;
731 unsigned char *raw
= (unsigned char *)natptr
;
732 asymbol
*sym
= *(g
->sym_ptr_ptr
);
734 asection
*output_section
= sym
->section
->output_section
;
736 bfd_h_put_32(abfd
, g
->address
, raw
);
737 /* Find a type in the output format which matches the input howto -
738 * at the moment we assume input format == output format FIXME!!
741 /* FIXME: Need callj stuff here, and to check the howto entries to
742 be sure they are real for this architecture. */
743 if (g
->howto
== &howto_reloc_callj
)
745 raw
[7] = callj_mask
+ pcrel_mask
+ len_2
;
747 else if (g
->howto
== &howto_reloc_pcrel24
)
749 raw
[7] = pcrel_mask
+ len_2
;
751 else if (g
->howto
== &howto_reloc_pcrel13
)
753 raw
[7] = pcrel_mask
+ len_1
;
755 else if (g
->howto
== &howto_reloc_abs32code
)
757 raw
[7] = len_2
+ incode_mask
;
759 else if (g
->howto
>= howto_align_table
760 && g
->howto
<= (howto_align_table
761 + sizeof (howto_align_table
) / sizeof (howto_align_table
[0])
764 /* symnum == -2; extern_mask not set, pcrel_mask set */
768 | ((g
->howto
- howto_align_table
) << 1));
775 /* already mucked with r_extern, r_idx */;
776 else if (bfd_is_com_section (output_section
)
777 || bfd_is_abs_section (output_section
)
778 || bfd_is_und_section (output_section
))
781 if (bfd_abs_section_ptr
->symbol
== sym
)
783 /* Whoops, looked like an abs symbol, but is really an offset
784 from the abs section */
793 r_idx
= stoi((*(g
->sym_ptr_ptr
))->flags
);
798 /* Just an ordinary section */
800 r_idx
= output_section
->target_index
;
803 if (abfd
->xvec
->header_byteorder_big_p
) {
804 raw
[4] = (unsigned char) (r_idx
>> 16);
805 raw
[5] = (unsigned char) (r_idx
>> 8);
806 raw
[6] = (unsigned char) (r_idx
);
808 raw
[6] = (unsigned char) (r_idx
>> 16);
809 raw
[5] = (unsigned char) (r_idx
>> 8);
810 raw
[4] = (unsigned char) (r_idx
);
813 raw
[7] |= extern_mask
;
816 if (bfd_write ((PTR
) native
, 1, natsize
, abfd
) != natsize
) {
825 /* This is stupid. This function should be a boolean predicate */
827 b_out_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
836 if ((section
->flags
& SEC_CONSTRUCTOR
) != 0)
838 arelent_chain
*chain
= section
->constructor_chain
;
839 for (count
= 0; count
< section
->reloc_count
; count
++)
841 *relptr
++ = &chain
->relent
;
847 if (section
->relocation
== NULL
848 && ! b_out_slurp_reloc_table (abfd
, section
, symbols
))
851 tblptr
= section
->relocation
;
852 for (count
= 0; count
++ < section
->reloc_count
;)
853 *relptr
++ = tblptr
++;
858 return section
->reloc_count
;
862 b_out_get_reloc_upper_bound (abfd
, asect
)
866 if (bfd_get_format (abfd
) != bfd_object
) {
867 bfd_set_error (bfd_error_invalid_operation
);
871 if (asect
->flags
& SEC_CONSTRUCTOR
)
872 return sizeof (arelent
*) * (asect
->reloc_count
+ 1);
874 if (asect
== obj_datasec (abfd
))
875 return (sizeof (arelent
*) *
876 ((exec_hdr(abfd
)->a_drsize
/ sizeof (struct relocation_info
))
879 if (asect
== obj_textsec (abfd
))
880 return (sizeof (arelent
*) *
881 ((exec_hdr(abfd
)->a_trsize
/ sizeof (struct relocation_info
))
884 if (asect
== obj_bsssec (abfd
))
887 bfd_set_error (bfd_error_invalid_operation
);
892 b_out_set_section_contents (abfd
, section
, location
, offset
, count
)
900 if (abfd
->output_has_begun
== false) { /* set by bfd.c handler */
901 if (! aout_32_make_sections (abfd
))
904 obj_textsec (abfd
)->filepos
= sizeof(struct internal_exec
);
905 obj_datasec(abfd
)->filepos
= obj_textsec(abfd
)->filepos
906 + obj_textsec (abfd
)->_raw_size
;
909 /* regardless, once we know what we're doing, we might as well get going */
910 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0)
914 return (bfd_write ((PTR
)location
, 1, count
, abfd
) == count
) ?true:false;
920 b_out_set_arch_mach (abfd
, arch
, machine
)
922 enum bfd_architecture arch
;
923 unsigned long machine
;
925 bfd_default_set_arch_mach(abfd
, arch
, machine
);
927 if (arch
== bfd_arch_unknown
) /* Unknown machine arch is OK */
929 if (arch
== bfd_arch_i960
) /* i960 default is OK */
931 case bfd_mach_i960_core
:
932 case bfd_mach_i960_kb_sb
:
933 case bfd_mach_i960_mc
:
934 case bfd_mach_i960_xa
:
935 case bfd_mach_i960_ca
:
936 case bfd_mach_i960_ka_sa
:
947 b_out_sizeof_headers (ignore_abfd
, ignore
)
951 return sizeof(struct internal_exec
);
956 /************************************************************************/
958 get_value (reloc
, link_info
, input_section
)
960 struct bfd_link_info
*link_info
;
961 asection
*input_section
;
964 asymbol
*symbol
= *(reloc
->sym_ptr_ptr
);
966 /* A symbol holds a pointer to a section, and an offset from the
967 base of the section. To relocate, we find where the section will
968 live in the output and add that in */
970 if (bfd_is_und_section (symbol
->section
))
972 struct bfd_link_hash_entry
*h
;
974 /* The symbol is undefined in this BFD. Look it up in the
975 global linker hash table. FIXME: This should be changed when
976 we convert b.out to use a specific final_link function and
977 change the interface to bfd_relax_section to not require the
979 h
= bfd_link_hash_lookup (link_info
->hash
, bfd_asymbol_name (symbol
),
981 if (h
!= (struct bfd_link_hash_entry
*) NULL
982 && (h
->type
== bfd_link_hash_defined
983 || h
->type
== bfd_link_hash_defweak
))
984 value
= h
->u
.def
.value
+ output_addr (h
->u
.def
.section
);
985 else if (h
!= (struct bfd_link_hash_entry
*) NULL
986 && h
->type
== bfd_link_hash_common
)
990 if (! ((*link_info
->callbacks
->undefined_symbol
)
991 (link_info
, bfd_asymbol_name (symbol
),
992 input_section
->owner
, input_section
, reloc
->address
)))
999 value
= symbol
->value
+ output_addr (symbol
->section
);
1002 /* Add the value contained in the relocation */
1003 value
+= reloc
->addend
;
1009 perform_slip (abfd
, slip
, input_section
, value
)
1012 asection
*input_section
;
1017 s
= _bfd_generic_link_get_symbols (abfd
);
1018 BFD_ASSERT (s
!= (asymbol
**) NULL
);
1020 /* Find all symbols past this point, and make them know
1025 if (p
->section
== input_section
)
1027 /* This was pointing into this section, so mangle it */
1028 if (p
->value
> value
)
1031 if (p
->udata
.p
!= NULL
)
1033 struct generic_link_hash_entry
*h
;
1035 h
= (struct generic_link_hash_entry
*) p
->udata
.p
;
1036 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
);
1037 h
->root
.u
.def
.value
-= slip
;
1038 BFD_ASSERT (h
->root
.u
.def
.value
== p
->value
);
1047 /* This routine works out if the thing we want to get to can be
1048 reached with a 24bit offset instead of a 32 bit one.
1049 If it can, then it changes the amode */
1052 abs32code (abfd
, input_section
, r
, shrink
, link_info
)
1054 asection
*input_section
;
1056 unsigned int shrink
;
1057 struct bfd_link_info
*link_info
;
1059 bfd_vma value
= get_value (r
, link_info
, input_section
);
1060 bfd_vma dot
= output_addr (input_section
) + r
->address
;
1063 /* See if the address we're looking at within 2^23 bytes of where
1064 we are, if so then we can use a small branch rather than the
1065 jump we were going to */
1067 gap
= value
- (dot
- shrink
);
1070 if (-1<<23 < (long)gap
&& (long)gap
< 1<<23 )
1072 /* Change the reloc type from 32bitcode possible 24, to 24bit
1075 r
->howto
= &howto_reloc_abs32codeshrunk
;
1076 /* The place to relc moves back by four bytes */
1079 /* This will be four bytes smaller in the long run */
1081 perform_slip (abfd
, 4, input_section
, r
->address
-shrink
+ 4);
1087 aligncode (abfd
, input_section
, r
, shrink
)
1089 asection
*input_section
;
1091 unsigned int shrink
;
1093 bfd_vma dot
= output_addr (input_section
) + r
->address
;
1098 int size
= r
->howto
->size
;
1100 /* Reduce the size of the alignment so that it's still aligned but
1101 smaller - the current size is already the same size as or bigger
1102 than the alignment required. */
1104 /* calculate the first byte following the padding before we optimize */
1105 old_end
= ((dot
+ size
) & ~size
) + size
+1;
1106 /* work out where the new end will be - remember that we're smaller
1107 than we used to be */
1108 new_end
= ((dot
- shrink
+ size
) & ~size
);
1110 /* This is the new end */
1111 gap
= old_end
- ((dot
+ size
) & ~size
);
1113 shrink_delta
= (old_end
- new_end
) - shrink
;
1117 /* Change the reloc so that it knows how far to align to */
1118 r
->howto
= howto_done_align_table
+ (r
->howto
- howto_align_table
);
1120 /* Encode the stuff into the addend - for future use we need to
1121 know how big the reloc used to be */
1122 r
->addend
= old_end
- dot
+ r
->address
;
1124 /* This will be N bytes smaller in the long run, adjust all the symbols */
1125 perform_slip (abfd
, shrink_delta
, input_section
, r
->address
- shrink
);
1126 shrink
+= shrink_delta
;
1132 b_out_bfd_relax_section (abfd
, i
, link_info
, again
)
1135 struct bfd_link_info
*link_info
;
1138 /* Get enough memory to hold the stuff */
1139 bfd
*input_bfd
= i
->owner
;
1140 asection
*input_section
= i
;
1142 arelent
**reloc_vector
= NULL
;
1143 long reloc_size
= bfd_get_reloc_upper_bound(input_bfd
,
1149 /* We only run this relaxation once. It might work to run it
1150 multiple times, but it hasn't been tested. */
1157 reloc_vector
= (arelent
**) malloc (reloc_size
);
1158 if (reloc_vector
== NULL
&& reloc_size
!= 0)
1160 bfd_set_error (bfd_error_no_memory
);
1164 /* Get the relocs and think about them */
1166 bfd_canonicalize_reloc (input_bfd
, input_section
, reloc_vector
,
1167 _bfd_generic_link_get_symbols (input_bfd
));
1168 if (reloc_count
< 0)
1170 if (reloc_count
> 0)
1173 for (parent
= reloc_vector
; *parent
; parent
++)
1175 arelent
*r
= *parent
;
1176 switch (r
->howto
->type
)
1179 /* An alignment reloc */
1180 shrink
= aligncode (abfd
, input_section
, r
, shrink
);
1183 /* A 32bit reloc in an addressing mode */
1184 shrink
= abs32code (input_bfd
, input_section
, r
, shrink
,
1187 case ABS32CODE_SHRUNK
:
1194 input_section
->_cooked_size
= input_section
->_raw_size
- shrink
;
1196 if (reloc_vector
!= NULL
)
1197 free (reloc_vector
);
1200 if (reloc_vector
!= NULL
)
1201 free (reloc_vector
);
1206 b_out_bfd_get_relocated_section_contents (in_abfd
, link_info
, link_order
,
1207 data
, relocateable
, symbols
)
1209 struct bfd_link_info
*link_info
;
1210 struct bfd_link_order
*link_order
;
1212 boolean relocateable
;
1215 /* Get enough memory to hold the stuff */
1216 bfd
*input_bfd
= link_order
->u
.indirect
.section
->owner
;
1217 asection
*input_section
= link_order
->u
.indirect
.section
;
1218 long reloc_size
= bfd_get_reloc_upper_bound(input_bfd
,
1220 arelent
**reloc_vector
= NULL
;
1226 /* If producing relocateable output, don't bother to relax. */
1228 return bfd_generic_get_relocated_section_contents (in_abfd
, link_info
,
1233 reloc_vector
= (arelent
**) malloc (reloc_size
);
1234 if (reloc_vector
== NULL
&& reloc_size
!= 0)
1236 bfd_set_error (bfd_error_no_memory
);
1240 input_section
->reloc_done
= 1;
1242 /* read in the section */
1243 BFD_ASSERT (true == bfd_get_section_contents(input_bfd
,
1247 input_section
->_raw_size
));
1249 reloc_count
= bfd_canonicalize_reloc (input_bfd
,
1253 if (reloc_count
< 0)
1255 if (reloc_count
> 0)
1257 arelent
**parent
= reloc_vector
;
1260 unsigned int dst_address
= 0;
1261 unsigned int src_address
= 0;
1265 /* Find how long a run we can do */
1266 while (dst_address
< link_order
->size
)
1271 /* Note that the relaxing didn't tie up the addresses in the
1272 relocation, so we use the original address to work out the
1273 run of non-relocated data */
1274 BFD_ASSERT (reloc
->address
>= src_address
);
1275 run
= reloc
->address
- src_address
;
1280 run
= link_order
->size
- dst_address
;
1282 /* Copy the bytes */
1283 for (idx
= 0; idx
< run
; idx
++)
1285 data
[dst_address
++] = data
[src_address
++];
1288 /* Now do the relocation */
1292 switch (reloc
->howto
->type
)
1295 calljx_callback (in_abfd
, link_info
, reloc
,
1296 src_address
+ data
, dst_address
+ data
,
1303 (bfd_get_32 (in_abfd
, data
+src_address
)
1304 + get_value (reloc
, link_info
, input_section
)),
1310 callj_callback (in_abfd
, link_info
, reloc
, data
, src_address
,
1311 dst_address
, input_section
, false);
1316 BFD_ASSERT (reloc
->addend
>= src_address
);
1317 BFD_ASSERT (reloc
->addend
<= input_section
->_raw_size
);
1318 src_address
= reloc
->addend
;
1319 dst_address
= ((dst_address
+ reloc
->howto
->size
)
1320 & ~reloc
->howto
->size
);
1322 case ABS32CODE_SHRUNK
:
1323 /* This used to be a callx, but we've found out that a
1324 callj will reach, so do the right thing. */
1325 callj_callback (in_abfd
, link_info
, reloc
, data
,
1326 src_address
+ 4, dst_address
, input_section
,
1333 long int word
= bfd_get_32(in_abfd
, data
+src_address
);
1336 value
= get_value (reloc
, link_info
, input_section
);
1337 word
= ((word
& ~BAL_MASK
)
1338 | (((word
& BAL_MASK
)
1340 - output_addr (input_section
)
1344 bfd_put_32(in_abfd
,word
, data
+dst_address
);
1353 long int word
= bfd_get_32(in_abfd
, data
+src_address
);
1356 value
= get_value (reloc
, link_info
, input_section
);
1357 word
= ((word
& ~PCREL13_MASK
)
1358 | (((word
& PCREL13_MASK
)
1361 - output_addr (input_section
))
1364 bfd_put_32(in_abfd
,word
, data
+dst_address
);
1377 if (reloc_vector
!= NULL
)
1378 free (reloc_vector
);
1381 if (reloc_vector
!= NULL
)
1382 free (reloc_vector
);
1385 /***********************************************************************/
1387 /* Build the transfer vectors for Big and Little-Endian B.OUT files. */
1389 #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1390 #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
1392 #define b_out_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1393 #define b_out_bfd_link_add_symbols _bfd_generic_link_add_symbols
1394 #define b_out_bfd_final_link _bfd_generic_final_link
1395 #define b_out_bfd_link_split_section _bfd_generic_link_split_section
1397 #define aout_32_get_section_contents_in_window \
1398 _bfd_generic_get_section_contents_in_window
1400 const bfd_target b_out_vec_big_host
=
1402 "b.out.big", /* name */
1403 bfd_target_aout_flavour
,
1404 false, /* data byte order is little */
1405 true, /* hdr byte order is big */
1406 (HAS_RELOC
| EXEC_P
| /* object flags */
1407 HAS_LINENO
| HAS_DEBUG
|
1408 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
1409 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1410 '_', /* symbol leading char */
1411 ' ', /* ar_pad_char */
1412 16, /* ar_max_namelen */
1414 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1415 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1416 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
1417 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1418 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1419 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1420 {_bfd_dummy_target
, b_out_object_p
, /* bfd_check_format */
1421 bfd_generic_archive_p
, _bfd_dummy_target
},
1422 {bfd_false
, b_out_mkobject
, /* bfd_set_format */
1423 _bfd_generic_mkarchive
, bfd_false
},
1424 {bfd_false
, b_out_write_object_contents
, /* bfd_write_contents */
1425 _bfd_write_archive_contents
, bfd_false
},
1427 BFD_JUMP_TABLE_GENERIC (aout_32
),
1428 BFD_JUMP_TABLE_COPY (_bfd_generic
),
1429 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1430 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd
),
1431 BFD_JUMP_TABLE_SYMBOLS (aout_32
),
1432 BFD_JUMP_TABLE_RELOCS (b_out
),
1433 BFD_JUMP_TABLE_WRITE (b_out
),
1434 BFD_JUMP_TABLE_LINK (b_out
),
1435 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
1441 const bfd_target b_out_vec_little_host
=
1443 "b.out.little", /* name */
1444 bfd_target_aout_flavour
,
1445 false, /* data byte order is little */
1446 false, /* header byte order is little */
1447 (HAS_RELOC
| EXEC_P
| /* object flags */
1448 HAS_LINENO
| HAS_DEBUG
|
1449 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
1450 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1451 '_', /* symbol leading char */
1452 ' ', /* ar_pad_char */
1453 16, /* ar_max_namelen */
1454 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1455 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1456 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
1457 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
1458 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
1459 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
1461 {_bfd_dummy_target
, b_out_object_p
, /* bfd_check_format */
1462 bfd_generic_archive_p
, _bfd_dummy_target
},
1463 {bfd_false
, b_out_mkobject
, /* bfd_set_format */
1464 _bfd_generic_mkarchive
, bfd_false
},
1465 {bfd_false
, b_out_write_object_contents
, /* bfd_write_contents */
1466 _bfd_write_archive_contents
, bfd_false
},
1468 BFD_JUMP_TABLE_GENERIC (aout_32
),
1469 BFD_JUMP_TABLE_COPY (_bfd_generic
),
1470 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1471 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd
),
1472 BFD_JUMP_TABLE_SYMBOLS (aout_32
),
1473 BFD_JUMP_TABLE_RELOCS (b_out
),
1474 BFD_JUMP_TABLE_WRITE (b_out
),
1475 BFD_JUMP_TABLE_LINK (b_out
),
1476 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),