* bfd/aoutx.h (aout_link_input_section_std,
[platform/upstream/binutils.git] / bfd / coff-z8k.c
1 /* BFD back-end for Zilog Z800n COFF binaries.
2    Copyright 1992, 1993 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4    Written by Steve Chamberlain, <sac@cygnus.com>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "obstack.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "coff/z8k.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 static reloc_howto_type r_imm32 =
32 HOWTO (R_IMM32, 0, 1, 32, false, 0,
33        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff,
34        0xffffffff, false);
35
36 static reloc_howto_type r_imm4l =
37 HOWTO (R_IMM4L, 0, 1, 4, false, 0,
38        complain_overflow_bitfield, 0, "r_imm4l", true, 0xf, 0xf, false);
39
40 static reloc_howto_type r_da =
41 HOWTO (R_IMM16, 0, 1, 16, false, 0,
42        complain_overflow_bitfield, 0, "r_da", true, 0x0000ffff, 0x0000ffff,
43        false);
44
45 static reloc_howto_type r_imm8 =
46 HOWTO (R_IMM8, 0, 1, 8, false, 0,
47        complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff,
48        false);
49
50 static reloc_howto_type r_jr =
51 HOWTO (R_JR, 0, 1, 8, true, 0, complain_overflow_signed, 0,
52        "r_jr", true, 0, 0, true);
53
54 /* Turn a howto into a reloc number */
55
56 static int 
57 coff_z8k_select_reloc (howto)
58      reloc_howto_type *howto;
59 {
60   return howto->type;
61 }
62
63 #define SELECT_RELOC(x,howto) x= coff_z8k_select_reloc(howto)
64
65
66 #define BADMAG(x) Z8KBADMAG(x)
67 #define Z8K 1                   /* Customize coffcode.h */
68 #define __A_MAGIC_SET__
69
70
71
72 /* Code to swap in the reloc */
73 #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
74 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
75 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
76   dst->r_stuff[0] = 'S'; \
77   dst->r_stuff[1] = 'C';
78
79 /* Code to turn a r_type into a howto ptr, uses the above howto table
80    */
81
82 static void
83 DEFUN (rtype2howto, (internal, dst),
84        arelent * internal AND
85        struct internal_reloc *dst)
86 {
87   switch (dst->r_type)
88     {
89       default:
90       fprintf (stderr, "BAD 0x%x\n", dst->r_type);
91     case R_IMM8:
92       internal->howto = &r_imm8;
93       break;
94      case R_IMM16:
95       internal->howto = &r_da;
96       break;
97     case R_JR:
98       internal->howto = &r_jr;
99       break;
100     case R_IMM32:
101       internal->howto = &r_imm32;
102       break;
103     case R_IMM4L:
104       internal->howto = &r_imm4l;
105       break;
106     }
107 }
108
109 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
110
111
112 /* Perform any necessaru magic to the addend in a reloc entry */
113
114
115 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
116  cache_ptr->addend =  ext_reloc.r_offset;
117
118
119 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
120  reloc_processing(relent, reloc, symbols, abfd, section)
121
122 static void 
123 DEFUN (reloc_processing, (relent, reloc, symbols, abfd, section),
124        arelent * relent AND
125        struct internal_reloc *reloc AND
126        asymbol ** symbols AND
127        bfd * abfd AND
128        asection * section)
129 {
130   relent->address = reloc->r_vaddr;
131   rtype2howto (relent, reloc);
132
133   if (reloc->r_symndx > 0)
134     {
135       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
136     }
137   else
138     {
139       relent->sym_ptr_ptr = &(bfd_abs_symbol);
140     }
141
142
143   relent->addend = reloc->r_offset;
144   relent->address -= section->vma;
145 }
146
147 static void
148 extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
149      bfd *in_abfd;
150      struct bfd_link_info *link_info;
151      struct bfd_link_order *link_order;
152      arelent *reloc;
153      bfd_byte *data;
154      unsigned int *src_ptr;
155      unsigned int *dst_ptr;
156 {
157   asection *input_section = link_order->u.indirect.section;
158
159   switch (reloc->howto->type)
160     {
161     case R_IMM8:
162       bfd_put_8 (in_abfd,
163                  bfd_coff_reloc16_get_value (reloc, link_info, input_section),
164                  data + *dst_ptr);
165       (*dst_ptr) += 1;
166       (*src_ptr) += 1;
167       break;
168
169     case R_IMM32:
170       bfd_put_32 (in_abfd,
171                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
172                   data + *dst_ptr);
173       (*dst_ptr) += 4;
174       (*src_ptr) += 4;
175       break;
176
177     case R_IMM4L:
178       bfd_put_8 (in_abfd,
179                  ((bfd_get_8 (in_abfd, data + *dst_ptr) & 0xf0) 
180                   | (0x0f
181                      & bfd_coff_reloc16_get_value (reloc, link_info,
182                                                    input_section))),
183                  data + *dst_ptr);
184       (*dst_ptr) += 1;
185       (*src_ptr) += 1;
186       break;
187
188     case R_IMM16:
189       bfd_put_16 (in_abfd,
190                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
191                   data + *dst_ptr);
192       (*dst_ptr) += 2;
193       (*src_ptr) += 2;
194       break;
195
196     case R_JR:
197       {
198         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
199                                                   input_section);
200         bfd_vma dot = (link_order->offset
201                        + *dst_ptr
202                        + input_section->output_section->vma);
203         int gap = dst - dot - 1;/* -1 since were in the odd byte of the
204                                     word and the pc's been incremented */
205
206         if (gap & 1)
207           abort ();
208         gap /= 2;
209         if (gap > 128 || gap < -128)
210           {
211             if (! ((*link_info->callbacks->reloc_overflow)
212                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
213                     reloc->howto->name, reloc->addend, input_section->owner,
214                     input_section, reloc->address)))
215               abort ();
216           }
217         bfd_put_8 (in_abfd, gap, data + *dst_ptr);
218         (*dst_ptr)++;
219         (*src_ptr)++;
220         break;
221       }
222     default:
223       abort ();
224     }
225 }
226
227 #define coff_reloc16_extra_cases extra_case
228
229 #include "coffcode.h"
230
231
232 #undef  coff_bfd_get_relocated_section_contents
233 #undef coff_bfd_relax_section
234 #define coff_bfd_get_relocated_section_contents \
235   bfd_coff_reloc16_get_relocated_section_contents
236 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
237
238 bfd_target z8kcoff_vec =
239 {
240   "coff-z8k",                   /* name */
241   bfd_target_coff_flavour,
242   true,                         /* data byte order is big */
243   true,                         /* header byte order is big */
244
245   (HAS_RELOC | EXEC_P |         /* object flags */
246    HAS_LINENO | HAS_DEBUG |
247    HAS_SYMS | HAS_LOCALS | WP_TEXT),
248
249   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),        /* section flags */
250   '_',                          /* leading symbol underscore */
251   '/',                          /* ar_pad_char */
252   15,                           /* ar_max_namelen */
253   1,                            /* minimum section alignment */
254   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
255      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
256      bfd_getb16, bfd_getb_signed_16, bfd_putb16,        /* data */
257   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
258      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
259      bfd_getb16, bfd_getb_signed_16, bfd_putb16,        /* hdrs */
260
261   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
262    bfd_generic_archive_p, _bfd_dummy_target},
263   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
264    bfd_false},
265   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
266    _bfd_write_archive_contents, bfd_false},
267
268   JUMP_TABLE (coff),
269   COFF_SWAP_TABLE,
270 };