Fri Sep 25 11:11:57 1992 Steve Chamberlain (sac@thepub.cygnus.com)
[binutils-gdb.git] / bfd / coff-z8k.c
1 /* Zilog Z800n COFF back-end for BFD.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3 Written by Steve Chamberlain
4 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
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "obstack.h"
27 #include "coff/z8k.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30 #include "seclet.h"
31 extern bfd_error_vector_type bfd_error_vector;
32 func_da() {}
33 func_jr() {}
34
35 static reloc_howto_type r_da =
36 HOWTO(R_DA , 0, 1, 16, false, 0, true,
37 true,func_da,"r_da", true, 0x0000ffff,0x0000ffff, false);
38
39 static reloc_howto_type r_jr =
40 HOWTO(R_JR, 0,1,8,true,0,true,true,func_jr,"r_jr", true,0,0,true);
41
42
43
44
45
46 /* Turn a howto into a reloc number */
47
48 static int coff_z8k_select_reloc(howto)
49 reloc_howto_type *howto;
50 {
51 return howto->type;
52 }
53 #define SELECT_RELOC(x,howto) x= coff_z8k_select_reloc(howto)
54
55
56 #define BADMAG(x) Z8KBADMAG(x)
57 #define Z8K 1 /* Customize coffcode.h */
58 #define __A_MAGIC_SET__
59
60
61
62 /* Code to swap in the reloc */
63 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
64 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
65 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
66 dst->r_stuff[0] = 'S'; \
67 dst->r_stuff[1] = 'C';
68
69 /* Code to turn a r_type into a howto ptr, uses the above howto table
70 */
71
72 static void
73 DEFUN(rtype2howto,(internal, dst),
74 arelent *internal AND
75 struct internal_reloc *dst)
76 {
77 switch (dst->r_type) {
78 default:
79 printf("BAD %x\n", dst->r_type);
80 case R_DA:
81 internal->howto = &r_da;
82 break;
83 case R_JR:
84 internal->howto = &r_jr;
85 }
86 }
87
88 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
89
90
91 /* Perform any necessaru magic to the addend in a reloc entry */
92
93
94 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
95 cache_ptr->addend = ext_reloc.r_offset;
96
97
98 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
99 reloc_processing(relent, reloc, symbols, abfd, section)
100
101 static void DEFUN(reloc_processing,(relent,reloc, symbols, abfd, section) ,
102 arelent *relent AND
103 struct internal_reloc *reloc AND
104 asymbol **symbols AND
105 bfd *abfd AND
106 asection *section)
107 {
108 relent->address = reloc->r_vaddr;
109 rtype2howto(relent,reloc);
110
111 if (reloc->r_symndx > 0)
112 {
113
114 relent->sym_ptr_ptr = symbols + obj_convert(abfd)[reloc->r_symndx];
115
116 }
117 else
118 {
119 relent->sym_ptr_ptr = &(bfd_abs_symbol);
120 }
121
122
123
124 relent->addend = reloc->r_offset;
125
126 relent->address-= section->vma;
127 /* relent->section = 0;*/
128 }
129
130 static bfd_vma get_value();
131 static
132 extra_case(in_abfd, seclet, reloc, data, src_ptr, dst_ptr)
133 bfd *in_abfd;
134 bfd_seclet_type *seclet;
135 arelent *reloc;
136 bfd_byte *data;
137 unsigned int *src_ptr;
138 unsigned int *dst_ptr;
139 {
140 switch (reloc->howto->type)
141 {
142 case R_DA:
143 bfd_put_16(in_abfd, get_value(reloc, seclet), data+*dst_ptr);
144 (*dst_ptr)+=2;
145 ( *src_ptr)+=2;
146 break;
147 case R_JR:
148 {
149 bfd_vma dst = get_value(reloc,seclet);
150 bfd_vma dot = seclet->offset
151 + *dst_ptr
152 + seclet->u.indirect.section->output_section->vma;
153 int gap = dst - dot - 1 ; /* -1 since were in the odd byte of the
154 word */
155 if (gap & 1)
156 abort();
157 gap/=2;
158 if (gap > 128 || gap< -128)
159 {
160 bfd_error_vector.reloc_value_truncated(reloc, seclet);
161 }
162 bfd_put_8(in_abfd, gap, data+*dst_ptr);
163 (*dst_ptr)++;
164 (*src_ptr)++;
165 break;
166 }
167 default:
168 abort();
169 }
170 }
171 #define EXTRA_CASES default: extra_case(in_abfd,seclet,reloc, data, &src_address, &dst_address);
172
173
174
175
176 #include "coffcode.h"
177
178
179 #undef coff_bfd_get_relocated_section_contents
180 #undef coff_bfd_relax_section
181 #define coff_bfd_get_relocated_section_contents bfd_coff_get_relocated_section_contents
182 #define coff_bfd_relax_section bfd_coff_relax_section
183
184 bfd_target z8kcoff_vec =
185 {
186 "coff-z8k", /* name */
187 bfd_target_coff_flavour,
188 true, /* data byte order is big */
189 true, /* header byte order is big */
190
191 (HAS_RELOC | EXEC_P | /* object flags */
192 HAS_LINENO | HAS_DEBUG |
193 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
194
195 ( SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
196 '/', /* ar_pad_char */
197 15, /* ar_max_namelen */
198 1, /* minimum section alignment */
199 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
200 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
201
202 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
203 bfd_generic_archive_p, _bfd_dummy_target},
204 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
205 bfd_false},
206 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
207 _bfd_write_archive_contents, bfd_false},
208
209 JUMP_TABLE(coff),
210 COFF_SWAP_TABLE
211 };