This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / bfd / coff-w65.c
1 /* BFD back-end for WDC 65816 COFF binaries.
2    Copyright 1995 Free Software Foundation, Inc.
3    Written by Steve Chamberlain, <sac@cygnus.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "obstack.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "coff/w65.h"
27 #include "coff/internal.h"
28 #include "libcoff.h"
29
30 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
31 static reloc_howto_type howto_table[] =
32 {
33   HOWTO (R_W65_ABS8,    0,  0, 8, false, 0, complain_overflow_bitfield, 0, "abs8", true, 0x000000ff, 0x000000ff, false),
34   HOWTO (R_W65_ABS16,   1,  0, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
35   HOWTO (R_W65_ABS24,   0,  2, 32, false, 0, complain_overflow_bitfield, 0, "abs24", true, 0x00ffffff, 0x00ffffff, false),
36   HOWTO (R_W65_ABS8S8,  0,  0, 8, false, 0, complain_overflow_bitfield, 0, ">abs8", true, 0x000000ff, 0x000000ff, false),
37   HOWTO (R_W65_ABS8S16, 0,  0, 8, false, 0, complain_overflow_bitfield, 0, "^abs8", true, 0x000000ff, 0x000000ff, false),
38   HOWTO (R_W65_ABS16S8, 1,  0, 16, false, 0, complain_overflow_bitfield, 0, ">abs16", true, 0x0000ffff, 0x0000ffff, false),
39   HOWTO (R_W65_ABS16S16,1,  0, 16, false, 0, complain_overflow_bitfield, 0, "^abs16", true, 0x0000ffff, 0x0000ffff, false),
40   HOWTO (R_W65_PCR8,    0,  0, 8, false, 0, complain_overflow_bitfield, 0, "pcrel8", true, 0x000000ff, 0x000000ff, true),
41   HOWTO (R_W65_PCR16,   1,  0, 16, false, 0, complain_overflow_bitfield, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, true),
42   HOWTO (R_W65_DP,    0,  0, 8, false, 0, complain_overflow_bitfield, 0, "dp", true, 0x000000ff, 0x000000ff, false),
43
44 };
45
46
47 /* Turn a howto into a reloc number */
48
49 #define SELECT_RELOC(x,howto) \
50   { x.r_type = select_reloc(howto); }
51
52 #define BADMAG(x) (W65BADMAG(x))
53 #define W65 1                   /* Customize coffcode.h */
54 #define __A_MAGIC_SET__
55
56
57 /* Code to swap in the reloc */
58 #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
59 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
60 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
61   dst->r_stuff[0] = 'S'; \
62   dst->r_stuff[1] = 'C';
63
64
65 static int
66 select_reloc (howto)
67      reloc_howto_type *howto;
68 {
69   return howto->type ;
70 }
71
72 /* Code to turn a r_type into a howto ptr, uses the above howto table
73    */
74
75 static void
76 rtype2howto (internal, dst)
77      arelent *internal;
78      struct internal_reloc *dst;
79 {
80       internal->howto = howto_table + dst->r_type - 1;
81 }
82
83 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
84
85
86 /* Perform any necessaru magic to the addend in a reloc entry */
87
88
89 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
90  cache_ptr->addend =  ext_reloc.r_offset;
91
92
93 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
94  reloc_processing(relent, reloc, symbols, abfd, section)
95
96 static void
97 reloc_processing (relent, reloc, symbols, abfd, section)
98      arelent * relent;
99      struct internal_reloc *reloc;
100      asymbol ** symbols;
101      bfd * abfd;
102      asection * section;
103 {
104   relent->address = reloc->r_vaddr;
105   rtype2howto (relent, reloc);
106
107   if (((int) reloc->r_symndx) > 0)
108     {
109       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
110     }
111   else
112     {
113       relent->sym_ptr_ptr = (asymbol **)&(bfd_abs_symbol);
114     }
115
116
117
118   relent->addend = reloc->r_offset;
119
120   relent->address -= section->vma;
121   /*  relent->section = 0;*/
122 }
123
124
125 static int
126 h8300_reloc16_estimate(abfd, input_section, reloc, shrink, link_info)
127      bfd *abfd;
128      asection *input_section;
129      arelent *reloc;
130      unsigned int shrink;
131      struct bfd_link_info *link_info;
132 {
133   bfd_vma value;  
134   bfd_vma dot;
135   bfd_vma gap;
136
137   /* The address of the thing to be relocated will have moved back by 
138    the size of the shrink  - but we don't change reloc->address here,
139    since we need it to know where the relocation lives in the source
140    uncooked section */
141
142   /*  reloc->address -= shrink;   conceptual */
143
144   bfd_vma address = reloc->address - shrink;
145   
146
147   switch (reloc->howto->type)
148     {     
149     case R_MOVB2:
150     case R_JMP2:
151       shrink+=2;
152       break;
153
154       /* Thing is a move one byte */
155     case R_MOVB1:
156       value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
157
158       if (value >= 0xff00)
159         { 
160
161           /* Change the reloc type from 16bit, possible 8 to 8bit
162              possible 16 */
163           reloc->howto = reloc->howto + 1;        
164           /* The place to relc moves back by one */
165           /* This will be two bytes smaller in the long run */
166           shrink +=2 ;
167           bfd_perform_slip(abfd, 2, input_section, address);
168         }      
169
170       break;
171       /* This is the 24 bit branch which could become an 8 bitter, 
172        the relocation points to the first byte of the insn, not the
173        actual data */
174
175     case R_JMPL1:
176       value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
177         
178       dot = input_section->output_section->vma +
179         input_section->output_offset + address;
180   
181       /* See if the address we're looking at within 127 bytes of where
182          we are, if so then we can use a small branch rather than the
183          jump we were going to */
184
185       gap = value - dot ;
186   
187       if (-120 < (long)gap && (long)gap < 120 )
188         { 
189
190           /* Change the reloc type from 24bit, possible 8 to 8bit
191              possible 32 */
192           reloc->howto = reloc->howto + 1;        
193           /* This will be two bytes smaller in the long run */
194           shrink +=2 ;
195           bfd_perform_slip(abfd, 2, input_section, address);
196         }
197       break;
198
199     case R_JMP1:
200
201       value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
202         
203       dot = input_section->output_section->vma +
204         input_section->output_offset + address;
205   
206       /* See if the address we're looking at within 127 bytes of where
207          we are, if so then we can use a small branch rather than the
208          jump we were going to */
209
210       gap = value - (dot - shrink);
211   
212
213       if (-120 < (long)gap && (long)gap < 120 )
214         { 
215
216           /* Change the reloc type from 16bit, possible 8 to 8bit
217              possible 16 */
218           reloc->howto = reloc->howto + 1;        
219           /* The place to relc moves back by one */
220
221           /* This will be two bytes smaller in the long run */
222           shrink +=2 ;
223           bfd_perform_slip(abfd, 2, input_section, address);
224         }
225       break;
226     }
227
228   
229   return shrink;
230 }
231
232
233 /* First phase of a relaxing link */
234
235 /* Reloc types
236    large                small
237    R_MOVB1              R_MOVB2         mov.b with 16bit or 8 bit address
238    R_JMP1               R_JMP2          jmp or pcrel branch
239    R_JMPL1              R_JMPL_B8       24jmp or pcrel branch
240    R_MOVLB1             R_MOVLB2        24 or 8 bit reloc for mov.b
241
242 */
243
244 static void
245 h8300_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
246                            dst_ptr)
247      bfd *abfd;
248      struct bfd_link_info *link_info;
249      struct bfd_link_order *link_order;
250      arelent *reloc;
251      bfd_byte *data;
252      unsigned int *src_ptr;
253      unsigned int *dst_ptr;
254 {
255   unsigned int src_address = *src_ptr;
256   unsigned int dst_address = *dst_ptr;
257   asection *input_section = link_order->u.indirect.section;
258
259   switch (reloc->howto->type)
260     {
261     case R_W65_ABS8:
262     case R_W65_DP:
263       {
264         unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
265                                                        input_section);
266         bfd_put_8 (abfd, gap, data + dst_address);
267         dst_address += 1;
268         src_address += 1;
269       }
270       break;
271
272     case R_W65_ABS8S8:
273       {
274         unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
275                                                        input_section);
276         gap >>= 8;
277         bfd_put_8 (abfd, gap, data + dst_address);
278         dst_address += 1;
279         src_address += 1;
280       }
281       break;
282
283     case R_W65_ABS8S16:
284       {
285         unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
286                                                        input_section);
287         gap >>=16;
288         bfd_put_8 (abfd, gap, data + dst_address);
289         dst_address += 1;
290         src_address += 1;
291       }
292       break;
293
294     case R_W65_ABS16:
295       {
296         unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
297                                                        input_section);
298
299         bfd_put_16 (abfd, gap, data + dst_address);
300         dst_address += 2;
301         src_address += 2;
302       }
303       break;
304     case R_W65_ABS16S8:
305       {
306         unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
307                                                        input_section);
308         gap >>= 8;
309         bfd_put_16 (abfd, gap, data + dst_address);
310         dst_address += 2;
311         src_address += 2;
312       }
313       break;
314     case R_W65_ABS16S16:
315       {
316         unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
317                                                        input_section);
318         gap >>= 16;
319         bfd_put_16 (abfd, gap, data + dst_address);
320         dst_address += 2;
321         src_address += 2;
322       }
323       break;
324
325     case R_W65_ABS24:
326       {
327         unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
328                                                        input_section);
329         bfd_put_16 (abfd, gap, data + dst_address);
330         bfd_put_8 (abfd, gap>>16, data+dst_address+2);
331         dst_address += 3;
332         src_address += 3;
333       }
334       break;
335
336     case R_W65_PCR8:
337       {
338         int gap = bfd_coff_reloc16_get_value (reloc, link_info,
339                                               input_section);
340         bfd_vma dot = link_order->offset 
341           + dst_address 
342             + link_order->u.indirect.section->output_section->vma;
343
344         gap -= dot + 1;
345         if (gap < -128 || gap > 127) {
346           if (! ((*link_info->callbacks->reloc_overflow)
347                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
348                   reloc->howto->name, reloc->addend, input_section->owner,
349                   input_section, reloc->address)))
350             abort();
351         }
352         bfd_put_8 (abfd, gap, data + dst_address);
353         dst_address += 1;
354         src_address += 1;
355       }
356       break;
357
358     case R_W65_PCR16:
359       {
360         bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
361                                                   input_section);
362         bfd_vma dot = link_order->offset 
363           + dst_address 
364             + link_order->u.indirect.section->output_section->vma;
365
366
367         /* This wraps within the page, so ignore the relativeness, look at the
368            high part */
369         if ((gap & 0xf0000) != (dot & 0xf0000)) {
370           if (! ((*link_info->callbacks->reloc_overflow)
371                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
372                   reloc->howto->name, reloc->addend, input_section->owner,
373                   input_section, reloc->address)))
374             abort();
375         }
376
377         gap -= dot + 2;
378         bfd_put_16 (abfd, gap, data + dst_address);
379         dst_address += 2;
380         src_address += 2;
381       }
382       break;
383     default:
384       printf("ignoring reloc %s\n", reloc->howto->name);
385       break;
386
387     }
388   *src_ptr = src_address;
389   *dst_ptr = dst_address;
390
391 }
392
393 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
394 #define coff_reloc16_estimate h8300_reloc16_estimate
395
396 #include "coffcode.h"
397
398
399 #undef coff_bfd_get_relocated_section_contents
400 #undef coff_bfd_relax_section
401 #define coff_bfd_get_relocated_section_contents \
402   bfd_coff_reloc16_get_relocated_section_contents
403 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
404
405
406
407 bfd_target w65_vec =
408 {
409   "coff-w65",                   /* name */
410   bfd_target_coff_flavour,
411   false,                        /* data byte order is big */
412   false,                        /* header byte order is big */
413
414   (HAS_RELOC | EXEC_P |         /* object flags */
415    HAS_LINENO | HAS_DEBUG |
416    HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
417   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),        /* section flags */
418   '_',                          /* leading char */
419   '/',                          /* ar_pad_char */
420   15,                           /* ar_max_namelen */
421   1,                            /* minimum section alignment */
422   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
423   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
424   bfd_getl16, bfd_getl_signed_16, bfd_putl16,   /* data */
425   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
426   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
427   bfd_getl16, bfd_getl_signed_16, bfd_putl16,   /* hdrs */
428
429   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
430    bfd_generic_archive_p, _bfd_dummy_target},
431   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
432    bfd_false},
433   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
434    _bfd_write_archive_contents, bfd_false},
435
436      BFD_JUMP_TABLE_GENERIC (coff),
437      BFD_JUMP_TABLE_COPY (coff),
438      BFD_JUMP_TABLE_CORE (_bfd_nocore),
439      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
440      BFD_JUMP_TABLE_SYMBOLS (coff),
441      BFD_JUMP_TABLE_RELOCS (coff),
442      BFD_JUMP_TABLE_WRITE (coff),
443      BFD_JUMP_TABLE_LINK (coff),
444      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
445
446   COFF_SWAP_TABLE,
447 };