1 /* BFD back-end for Zilog Z80 COFF binaries.
2 Copyright (C) 2005-2023 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 3 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"
29 #include "libiberty.h"
31 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 0
33 typedef const struct {
34 bfd_reloc_code_real_type r_type
;
35 reloc_howto_type howto
;
38 #define BFD_EMPTY_HOWTO(rt,x) {rt, EMPTY_HOWTO(x)}
39 #define BFD_HOWTO(rt,a,b,c,d,e,f,g,h,i,j,k,l,m) {rt, HOWTO(a,b,c,d,e,f,g,h,i,j,k,l,m)}
41 static bfd_howto_type howto_table
[] =
43 BFD_EMPTY_HOWTO (BFD_RELOC_NONE
, 0),
45 BFD_HOWTO (BFD_RELOC_32
,
50 false, /* pc_relative */
52 complain_overflow_bitfield
, /* complain_on_overflow */
53 0, /* special_function */
55 false, /* partial_inplace */
56 0xffffffff, /* src_mask */
57 0xffffffff, /* dst_mask */
58 false), /* pcrel_offset */
60 BFD_HOWTO (BFD_RELOC_24
,
65 false, /* pc_relative */
67 complain_overflow_bitfield
, /* complain_on_overflow */
68 0, /* special_function */
70 false, /* partial_inplace */
71 0x00ffffff, /* src_mask */
72 0x00ffffff, /* dst_mask */
73 false), /* pcrel_offset */
75 BFD_HOWTO (BFD_RELOC_16
,
80 false, /* pc_relative */
82 complain_overflow_bitfield
, /* complain_on_overflow */
83 0, /* special_function */
85 false, /* partial_inplace */
86 0x0000ffff, /* src_mask */
87 0x0000ffff, /* dst_mask */
88 false), /* pcrel_offset */
90 BFD_HOWTO (BFD_RELOC_8
,
95 false, /* pc_relative */
97 complain_overflow_bitfield
, /* complain_on_overflow */
98 0, /* special_function */
100 false, /* partial_inplace */
101 0x000000ff, /* src_mask */
102 0x000000ff, /* dst_mask */
103 false), /* pcrel_offset */
105 BFD_HOWTO (BFD_RELOC_8_PCREL
,
110 true, /* pc_relative */
112 complain_overflow_signed
, /* complain_on_overflow */
113 0, /* special_function */
115 false, /* partial_inplace */
118 true), /* pcrel_offset */
120 BFD_HOWTO (BFD_RELOC_Z80_DISP8
,
125 false, /* pc_relative */
127 complain_overflow_signed
, /* complain_on_overflow */
128 0, /* special_function */
130 false, /* partial_inplace */
133 false), /* pcrel_offset */
135 BFD_HOWTO (BFD_RELOC_Z80_BYTE0
,
140 false, /* pc_relative */
142 complain_overflow_dont
, /* complain_on_overflow */
143 0, /* special_function */
144 "r_byte0", /* name */
145 false, /* partial_inplace */
148 false), /* pcrel_offset */
150 BFD_HOWTO (BFD_RELOC_Z80_BYTE1
,
155 false, /* pc_relative */
157 complain_overflow_dont
, /* complain_on_overflow */
158 0, /* special_function */
159 "r_byte1", /* name */
160 false, /* partial_inplace */
163 false), /* pcrel_offset */
165 BFD_HOWTO (BFD_RELOC_Z80_BYTE2
,
170 false, /* pc_relative */
172 complain_overflow_dont
, /* complain_on_overflow */
173 0, /* special_function */
174 "r_byte2", /* name */
175 false, /* partial_inplace */
178 false), /* pcrel_offset */
180 BFD_HOWTO (BFD_RELOC_Z80_BYTE3
,
185 false, /* pc_relative */
187 complain_overflow_dont
, /* complain_on_overflow */
188 0, /* special_function */
189 "r_byte3", /* name */
190 false, /* partial_inplace */
193 false), /* pcrel_offset */
195 BFD_HOWTO (BFD_RELOC_Z80_WORD0
,
200 false, /* pc_relative */
202 complain_overflow_dont
, /* complain_on_overflow */
203 0, /* special_function */
204 "r_word0", /* name */
205 false, /* partial_inplace */
207 0xffff, /* dst_mask */
208 false), /* pcrel_offset */
210 BFD_HOWTO (BFD_RELOC_Z80_WORD1
,
215 false, /* pc_relative */
217 complain_overflow_dont
, /* complain_on_overflow */
218 0, /* special_function */
219 "r_word1", /* name */
220 false, /* partial_inplace */
222 0xffff, /* dst_mask */
223 false), /* pcrel_offset */
225 BFD_HOWTO (BFD_RELOC_Z80_16_BE
,
226 R_IMM16BE
, /* type */
230 false, /* pc_relative */
232 complain_overflow_bitfield
, /* complain_on_overflow */
233 0, /* special_function */
234 "r_imm16be", /* name */
235 false, /* partial_inplace */
236 0x0000ffff, /* src_mask */
237 0x0000ffff, /* dst_mask */
238 false), /* pcrel_offset */
241 #define NUM_HOWTOS ARRAY_SIZE (howto_table)
243 #define BADMAG(x) Z80BADMAG(x)
244 #define Z80 1 /* Customize coffcode.h. */
245 #define __A_MAGIC_SET__
247 /* Code to swap in the reloc. */
249 #define SWAP_IN_RELOC_OFFSET H_GET_32
250 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
252 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
253 dst->r_stuff[0] = 'S'; \
254 dst->r_stuff[1] = 'C';
256 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
258 rtype2howto (arelent
*internal
, struct internal_reloc
*dst
)
261 for (i
= 0; i
< NUM_HOWTOS
; i
++)
263 if (howto_table
[i
].howto
.type
== dst
->r_type
)
265 internal
->howto
= &howto_table
[i
].howto
;
269 internal
->howto
= NULL
;
272 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
274 static reloc_howto_type
*
275 coff_z80_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
276 bfd_reloc_code_real_type code
)
279 for (i
= 0; i
< NUM_HOWTOS
; i
++)
280 if (howto_table
[i
].r_type
== code
)
281 return &howto_table
[i
].howto
;
287 static reloc_howto_type
*
288 coff_z80_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
292 for (i
= 0; i
< NUM_HOWTOS
; i
++)
293 if (strcasecmp(howto_table
[i
].howto
.name
, r_name
) == 0)
294 return &howto_table
[i
].howto
;
299 /* Perform any necessary magic to the addend in a reloc entry. */
301 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
302 cache_ptr->addend = ext_reloc.r_offset;
304 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
305 reloc_processing(relent, reloc, symbols, abfd, section)
308 reloc_processing (arelent
*relent
,
309 struct internal_reloc
*reloc
,
314 relent
->address
= reloc
->r_vaddr
;
315 rtype2howto (relent
, reloc
);
317 if (reloc
->r_symndx
== -1 || symbols
== NULL
)
318 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
319 else if (reloc
->r_symndx
>= 0 && reloc
->r_symndx
< obj_conv_table_size (abfd
))
320 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
324 /* xgettext:c-format */
325 (_("%pB: warning: illegal symbol index %ld in relocs"),
326 abfd
, reloc
->r_symndx
);
327 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
329 relent
->addend
= reloc
->r_offset
;
330 relent
->address
-= section
->vma
;
334 extra_case (bfd
*in_abfd
,
335 struct bfd_link_info
*link_info
,
336 struct bfd_link_order
*link_order
,
342 asection
* input_section
= link_order
->u
.indirect
.section
;
343 bfd_size_type end
= bfd_get_section_limit_octets (in_abfd
, input_section
);
344 bfd_size_type reloc_size
= bfd_get_reloc_size (reloc
->howto
);
347 || reloc_size
> end
- *src_ptr
)
349 link_info
->callbacks
->einfo
350 /* xgettext:c-format */
351 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
352 in_abfd
, input_section
, reloc
);
356 int val
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
357 switch (reloc
->howto
->type
)
360 if (reloc
->howto
->partial_inplace
)
361 val
+= (signed char) (bfd_get_8 (in_abfd
, data
+ *src_ptr
)
362 & reloc
->howto
->src_mask
);
363 if (val
> 127 || val
< -128)
365 link_info
->callbacks
->reloc_overflow
366 (link_info
, NULL
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
367 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
368 input_section
, reloc
->address
);
372 bfd_put_8 (in_abfd
, val
, data
+ *dst_ptr
);
378 bfd_put_8 (in_abfd
, val
>> 24, data
+ *dst_ptr
);
384 bfd_put_8 (in_abfd
, val
>> 16, data
+ *dst_ptr
);
390 bfd_put_8 (in_abfd
, val
>> 8, data
+ *dst_ptr
);
396 if (reloc
->howto
->partial_inplace
)
397 val
+= bfd_get_8 (in_abfd
, data
+ *src_ptr
) & reloc
->howto
->src_mask
;
400 bfd_put_8 (in_abfd
, val
, data
+ *dst_ptr
);
406 bfd_put_16 (in_abfd
, val
>> 16, data
+ *dst_ptr
);
412 if (reloc
->howto
->partial_inplace
)
413 val
+= bfd_get_16 (in_abfd
, data
+ *src_ptr
) & reloc
->howto
->src_mask
;
416 bfd_put_16 (in_abfd
, val
, data
+ *dst_ptr
);
422 if (reloc
->howto
->partial_inplace
)
423 val
+= (bfd_get_24 (in_abfd
, data
+ *src_ptr
)
424 & reloc
->howto
->src_mask
);
425 bfd_put_24 (in_abfd
, val
, data
+ *dst_ptr
);
431 if (reloc
->howto
->partial_inplace
)
432 val
+= bfd_get_32 (in_abfd
, data
+ *src_ptr
) & reloc
->howto
->src_mask
;
433 bfd_put_32 (in_abfd
, val
, data
+ *dst_ptr
);
440 if (reloc
->howto
->partial_inplace
)
441 val
+= (signed char) (bfd_get_8 (in_abfd
, data
+ *src_ptr
)
442 & reloc
->howto
->src_mask
);
443 bfd_vma dot
= (*dst_ptr
444 + input_section
->output_offset
445 + input_section
->output_section
->vma
);
446 bfd_signed_vma gap
= val
- dot
;
447 if (gap
>= 128 || gap
< -128)
449 link_info
->callbacks
->reloc_overflow
450 (link_info
, NULL
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
451 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
452 input_section
, reloc
->address
);
456 bfd_put_8 (in_abfd
, gap
, data
+ *dst_ptr
);
463 if (reloc
->howto
->partial_inplace
)
464 val
+= ((bfd_get_8 (in_abfd
, data
+ *src_ptr
+ 0) * 0x100
465 + bfd_get_8 (in_abfd
, data
+ *src_ptr
+ 1))
466 & reloc
->howto
->src_mask
);
468 bfd_put_8 (in_abfd
, val
>> 8, data
+ *dst_ptr
+ 0);
469 bfd_put_8 (in_abfd
, val
, data
+ *dst_ptr
+ 1);
475 link_info
->callbacks
->einfo
476 /* xgettext:c-format */
477 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
478 in_abfd
, input_section
, reloc
);
485 z80_is_local_label_name (bfd
* abfd ATTRIBUTE_UNUSED
,
488 return (name
[0] == '.' && name
[1] == 'L') ||
489 _bfd_coff_is_local_label_name (abfd
, name
);
492 #define coff_bfd_is_local_label_name z80_is_local_label_name
494 #define coff_reloc16_extra_cases extra_case
495 #define coff_bfd_reloc_type_lookup coff_z80_reloc_type_lookup
496 #define coff_bfd_reloc_name_lookup coff_z80_reloc_name_lookup
498 #ifndef bfd_pe_print_pdata
499 #define bfd_pe_print_pdata NULL
502 #include "coffcode.h"
504 #undef coff_bfd_get_relocated_section_contents
505 #define coff_bfd_get_relocated_section_contents \
506 bfd_coff_reloc16_get_relocated_section_contents
508 #undef coff_bfd_relax_section
509 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
511 CREATE_LITTLE_COFF_TARGET_VEC (z80_coff_vec
, "coff-z80", 0,
512 SEC_CODE
| SEC_DATA
, '\0', NULL
,