1 /* BFD back-end for Zilog Z80 COFF binaries.
2 Copyright 2005 Free Software Foundation, Inc.
3 Contributed by Arnold Metselaar <arnold_m@operamail.com>
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "coff/internal.h"
30 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 0
32 static reloc_howto_type r_imm32
=
33 HOWTO (R_IMM32
, 0, 1, 32, FALSE
, 0,
34 complain_overflow_dont
, 0, "r_imm32", TRUE
, 0xffffffff, 0xffffffff,
37 static reloc_howto_type r_imm24
=
38 HOWTO (R_IMM24
, 0, 1, 24, FALSE
, 0,
39 complain_overflow_dont
, 0, "r_imm24", TRUE
, 0x00ffffff, 0x00ffffff,
42 static reloc_howto_type r_imm16
=
43 HOWTO (R_IMM16
, 0, 1, 16, FALSE
, 0,
44 complain_overflow_dont
, 0, "r_imm16", TRUE
, 0x0000ffff, 0x0000ffff,
47 static reloc_howto_type r_imm8
=
48 HOWTO (R_IMM8
, 0, 0, 8, FALSE
, 0,
49 complain_overflow_bitfield
, 0, "r_imm8", TRUE
, 0x000000ff, 0x000000ff,
52 static reloc_howto_type r_jr
=
53 HOWTO (R_JR
, 0, 0, 8, TRUE
, 0,
54 complain_overflow_signed
, 0, "r_jr", FALSE
, 0, 0xFF,
57 static reloc_howto_type r_off8
=
58 HOWTO (R_OFF8
, 0, 0, 8, FALSE
, 0,
59 complain_overflow_signed
, 0,"r_off8", FALSE
, 0, 0xff,
63 #define BADMAG(x) Z80BADMAG(x)
64 #define Z80 1 /* Customize coffcode.h. */
65 #define __A_MAGIC_SET__
67 /* Code to swap in the reloc. */
69 #define SWAP_IN_RELOC_OFFSET H_GET_32
70 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
72 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
73 dst->r_stuff[0] = 'S'; \
74 dst->r_stuff[1] = 'C';
76 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
79 rtype2howto (arelent
*internal
, struct internal_reloc
*dst
)
87 internal
->howto
= &r_imm8
;
90 internal
->howto
= &r_imm16
;
93 internal
->howto
= &r_imm24
;
96 internal
->howto
= &r_imm32
;
99 internal
->howto
= &r_jr
;
102 internal
->howto
= &r_off8
;
107 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
109 static reloc_howto_type
*
110 coff_z80_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
111 bfd_reloc_code_real_type code
)
115 case BFD_RELOC_8
: return & r_imm8
;
116 case BFD_RELOC_16
: return & r_imm16
;
117 case BFD_RELOC_24
: return & r_imm24
;
118 case BFD_RELOC_32
: return & r_imm32
;
119 case BFD_RELOC_8_PCREL
: return & r_jr
;
120 case BFD_RELOC_Z80_DISP8
: return & r_off8
;
121 default: BFD_FAIL ();
126 /* Perform any necessary magic to the addend in a reloc entry. */
128 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
129 cache_ptr->addend = ext_reloc.r_offset;
131 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
132 reloc_processing(relent, reloc, symbols, abfd, section)
135 reloc_processing (arelent
*relent
,
136 struct internal_reloc
*reloc
,
141 relent
->address
= reloc
->r_vaddr
;
142 rtype2howto (relent
, reloc
);
144 if (reloc
->r_symndx
> 0)
145 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
147 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
149 relent
->addend
= reloc
->r_offset
;
150 relent
->address
-= section
->vma
;
154 extra_case (bfd
*in_abfd
,
155 struct bfd_link_info
*link_info
,
156 struct bfd_link_order
*link_order
,
159 unsigned int *src_ptr
,
160 unsigned int *dst_ptr
)
162 asection
* input_section
= link_order
->u
.indirect
.section
;
165 switch (reloc
->howto
->type
)
168 val
= bfd_coff_reloc16_get_value (reloc
, link_info
,
170 if (val
>127 || val
<-128) /* Test for overflow. */
172 if (! ((*link_info
->callbacks
->reloc_overflow
)
174 bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
175 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
176 input_section
, reloc
->address
)))
179 bfd_put_8 (in_abfd
, val
, data
+ *dst_ptr
);
185 val
= bfd_get_8 ( in_abfd
, data
+*src_ptr
)
186 + bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
187 bfd_put_8 (in_abfd
, val
, data
+ *dst_ptr
);
193 val
= bfd_get_16 ( in_abfd
, data
+*src_ptr
)
194 + bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
195 bfd_put_16 (in_abfd
, val
, data
+ *dst_ptr
);
201 val
= bfd_get_16 ( in_abfd
, data
+*src_ptr
)
202 + (bfd_get_8 ( in_abfd
, data
+*src_ptr
+2) << 16)
203 + bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
204 bfd_put_16 (in_abfd
, val
, data
+ *dst_ptr
);
205 bfd_put_8 (in_abfd
, val
>> 16, data
+ *dst_ptr
+2);
211 val
= bfd_get_32 ( in_abfd
, data
+*src_ptr
)
212 + bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
213 bfd_put_32 (in_abfd
, val
, data
+ *dst_ptr
);
220 bfd_vma dst
= bfd_coff_reloc16_get_value (reloc
, link_info
,
222 bfd_vma dot
= (*dst_ptr
223 + input_section
->output_offset
224 + input_section
->output_section
->vma
);
225 int gap
= dst
- dot
- 1; /* -1, Since the offset is relative
226 to the value of PC after reading
229 if (gap
>= 128 || gap
< -128)
231 if (! ((*link_info
->callbacks
->reloc_overflow
)
233 bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
234 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
235 input_section
, reloc
->address
)))
238 bfd_put_8 (in_abfd
, gap
, data
+ *dst_ptr
);
249 #define coff_reloc16_extra_cases extra_case
250 #define coff_bfd_reloc_type_lookup coff_z80_reloc_type_lookup
252 #include "coffcode.h"
254 #undef coff_bfd_get_relocated_section_contents
255 #define coff_bfd_get_relocated_section_contents \
256 bfd_coff_reloc16_get_relocated_section_contents
258 #undef coff_bfd_relax_section
259 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
261 CREATE_LITTLE_COFF_TARGET_VEC (z80coff_vec
, "coff-z80", 0, 0, '\0', NULL
,