* coff-h8300.c (h8300_reloc16_extra_cases): Use input section
[external/binutils.git] / bfd / coff-z80.c
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>
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "coff/z80.h"
27 #include "coff/internal.h"
28 #include "libcoff.h"
29
30 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 0
31
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,
35        FALSE);
36
37 static reloc_howto_type r_imm16 =
38 HOWTO (R_IMM16, 0, 1, 16, FALSE, 0,
39        complain_overflow_dont, 0, "r_imm16", TRUE, 0x0000ffff, 0x0000ffff,
40        FALSE);
41
42 static reloc_howto_type r_imm8 =
43 HOWTO (R_IMM8, 0, 0, 8, FALSE, 0,
44        complain_overflow_bitfield, 0, "r_imm8", TRUE, 0x000000ff, 0x000000ff,
45        FALSE);
46
47 static reloc_howto_type r_jr =
48 HOWTO (R_JR, 0, 0, 8, TRUE, 0, 
49        complain_overflow_signed, 0, "r_jr", FALSE, 0, 0xFF,
50        FALSE);
51
52 static reloc_howto_type r_off8 =
53 HOWTO (R_OFF8, 0, 0, 8, FALSE, 0, 
54        complain_overflow_signed, 0,"r_off8", FALSE, 0, 0xff,
55        FALSE);
56
57
58 #define BADMAG(x) Z80BADMAG(x)
59 #define Z80 1                   /* Customize coffcode.h.  */
60 #define __A_MAGIC_SET__
61
62 /* Code to swap in the reloc.  */
63
64 #define SWAP_IN_RELOC_OFFSET    H_GET_32
65 #define SWAP_OUT_RELOC_OFFSET   H_PUT_32
66
67 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
68   dst->r_stuff[0] = 'S'; \
69   dst->r_stuff[1] = 'C';
70
71 /* Code to turn a r_type into a howto ptr, uses the above howto table.  */
72
73 static void
74 rtype2howto (arelent *internal, struct internal_reloc *dst)
75 {
76   switch (dst->r_type)
77     {
78     default:
79       abort ();
80       break;
81     case R_IMM8:
82       internal->howto = &r_imm8;
83       break;
84     case R_IMM16:
85       internal->howto = &r_imm16;
86       break;
87     case R_IMM32:
88       internal->howto = &r_imm32;
89       break;
90     case R_JR:
91       internal->howto = &r_jr;
92       break;
93     case R_OFF8:
94       internal->howto = &r_off8;
95       break;
96     }
97 }
98
99 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
100
101 static reloc_howto_type *
102 coff_z80_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
103                             bfd_reloc_code_real_type code)
104 {
105   switch (code)
106     {
107     case BFD_RELOC_8:           return & r_imm8;
108     case BFD_RELOC_16:          return & r_imm16;
109     case BFD_RELOC_32:          return & r_imm32;
110     case BFD_RELOC_8_PCREL:     return & r_jr;
111     case BFD_RELOC_Z80_DISP8:   return & r_off8;
112     default:                    BFD_FAIL ();
113       return NULL;
114     }
115 }
116
117 /* Perform any necessary magic to the addend in a reloc entry.  */
118
119 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
120  cache_ptr->addend =  ext_reloc.r_offset;
121
122 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
123  reloc_processing(relent, reloc, symbols, abfd, section)
124
125 static void
126 reloc_processing (arelent *relent,
127                   struct internal_reloc *reloc,
128                   asymbol **symbols,
129                   bfd *abfd,
130                   asection *section)
131 {
132   relent->address = reloc->r_vaddr;
133   rtype2howto (relent, reloc);
134
135   if (reloc->r_symndx > 0)
136     relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
137   else
138     relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
139
140   relent->addend = reloc->r_offset;
141   relent->address -= section->vma;
142 }
143
144 static void
145 extra_case (bfd *in_abfd,
146             struct bfd_link_info *link_info,
147             struct bfd_link_order *link_order,
148             arelent *reloc,
149             bfd_byte *data,
150             unsigned int *src_ptr,
151             unsigned int *dst_ptr)
152 {
153   asection * input_section = link_order->u.indirect.section;
154   int val;
155
156   switch (reloc->howto->type)
157     {
158     case R_OFF8:
159         val = bfd_coff_reloc16_get_value (reloc, link_info,
160                                            input_section);
161         if (val>127 || val<-128) /* Test for overflow.  */
162           {
163             if (! ((*link_info->callbacks->reloc_overflow)
164                    (link_info, NULL,
165                     bfd_asymbol_name (*reloc->sym_ptr_ptr),
166                     reloc->howto->name, reloc->addend, input_section->owner,
167                     input_section, reloc->address)))
168               abort ();
169           }
170         bfd_put_8 (in_abfd, val, data + *dst_ptr);
171         (*dst_ptr) += 1;
172         (*src_ptr) += 1;
173       break;
174
175     case R_IMM8:
176       val = bfd_get_16 ( in_abfd, data+*src_ptr)
177         + bfd_coff_reloc16_get_value (reloc, link_info, input_section);
178       bfd_put_8 (in_abfd, val, data + *dst_ptr);
179       (*dst_ptr) += 1;
180       (*src_ptr) += 1;
181       break;
182
183     case R_IMM16:
184       val = bfd_get_16 ( in_abfd, data+*src_ptr)
185         + bfd_coff_reloc16_get_value (reloc, link_info, input_section);
186       bfd_put_16 (in_abfd, val, data + *dst_ptr);
187       (*dst_ptr) += 2;
188       (*src_ptr) += 2;
189       break;
190
191     case R_IMM32:
192       val = bfd_get_32 ( in_abfd, data+*src_ptr)
193         + bfd_coff_reloc16_get_value (reloc, link_info, input_section);
194       bfd_put_32 (in_abfd, val, data + *dst_ptr);
195       (*dst_ptr) += 4;
196       (*src_ptr) += 4;
197       break;
198
199     case R_JR:
200       {
201         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
202                                                   input_section);
203         bfd_vma dot = (*dst_ptr
204                        + input_section->output_offset
205                        + input_section->output_section->vma);
206         int gap = dst - dot - 1;  /* -1, Since the offset is relative
207                                      to the value of PC after reading
208                                      the offset.  */
209
210         if (gap >= 128 || gap < -128)
211           {
212             if (! ((*link_info->callbacks->reloc_overflow)
213                    (link_info, NULL,
214                     bfd_asymbol_name (*reloc->sym_ptr_ptr),
215                     reloc->howto->name, reloc->addend, input_section->owner,
216                     input_section, reloc->address)))
217               abort ();
218           }
219         bfd_put_8 (in_abfd, gap, data + *dst_ptr);
220         (*dst_ptr)++;
221         (*src_ptr)++;
222         break;
223       }
224
225     default:
226       abort ();
227     }
228 }
229
230 #define coff_reloc16_extra_cases    extra_case
231 #define coff_bfd_reloc_type_lookup  coff_z80_reloc_type_lookup
232
233 #include "coffcode.h"
234
235 #undef  coff_bfd_get_relocated_section_contents
236 #define coff_bfd_get_relocated_section_contents \
237   bfd_coff_reloc16_get_relocated_section_contents
238
239 #undef  coff_bfd_relax_section
240 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
241
242 CREATE_LITTLE_COFF_TARGET_VEC (z80coff_vec, "coff-z80", 0, 0, '\0', NULL, 
243                                COFF_SWAP_TABLE)
244