Fixes from Andrew Smith
[deliverable/binutils-gdb.git] / bfd / coff-a29k.c
CommitLineData
3c8a3c56
JG
1/* AMD 29000 COFF back-end for BFD.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Contributed by David Wood at New York University 7/8/91.
2013f9b4 4
3c8a3c56 5This file is part of BFD, the Binary File Descriptor library.
2013f9b4 6
3c8a3c56 7This program is free software; you can redistribute it and/or modify
2013f9b4 8it under the terms of the GNU General Public License as published by
3c8a3c56
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
2013f9b4 11
3c8a3c56 12This program is distributed in the hope that it will be useful,
2013f9b4
SC
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
3c8a3c56
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
2013f9b4
SC
20
21/* $Id$ */
22
23#define A29K 1
24
2013f9b4 25#include "bfd.h"
bbc8d484 26#include "sysdep.h"
2013f9b4
SC
27#include "libbfd.h"
28#include "obstack.h"
c3eb25fc
SC
29#include "coff/a29k.h"
30#include "coff/internal.h"
2013f9b4
SC
31#include "libcoff.h"
32
33#define INSERT_HWORD(WORD,HWORD) \
c2ce0738
SC
34 (((WORD) & 0xff00ff00) | (((HWORD) & 0xff00) << 8) | ((HWORD)& 0xff))
35#define EXTRACT_HWORD(WORD) \
36 (((WORD) & 0x00ff0000) >> 8) | ((WORD)& 0xff)
37#define SIGN_EXTEND_HWORD(HWORD) \
38 ((HWORD) & 0x8000 ? (HWORD)|0xffff0000 : (HWORD))
2013f9b4
SC
39
40/* Provided the symbol, returns the value reffed */
41static long
42get_symbol_value(symbol)
43asymbol *symbol;
44{
45 long relocation = 0;
46
859f11ff
SC
47 if (symbol->section == &bfd_com_section)
48 {
49 relocation = 0;
2013f9b4 50 }
859f11ff
SC
51 else
52 {
53 relocation = symbol->value +
54 symbol->section->output_section->vma +
55 symbol->section->output_offset;
56 }
57
2013f9b4
SC
58 return(relocation);
59}
60
c2ce0738
SC
61/* this function is in charge of performing all the 29k relocations */
62
9e2dad8e 63static bfd_reloc_status_type
2cfd0562 64DEFUN(a29k_reloc,(abfd, reloc_entry, symbol_in, data, input_section, output_bfd),
c2ce0738
SC
65 bfd *abfd AND
66 arelent *reloc_entry AND
67 asymbol *symbol_in AND
8b87cbae 68 PTR data AND
2cfd0562
SC
69 asection *input_section AND
70 bfd *output_bfd)
2013f9b4 71{
859f11ff
SC
72 /* the consth relocation comes in two parts, we have to remember
73 the state between calls, in these variables */
74 static boolean part1_consth_active = false;
75 static unsigned long part1_consth_value;
c2ce0738 76
859f11ff
SC
77 unsigned long insn;
78 unsigned long sym_value;
79 unsigned long unsigned_value;
80 unsigned short r_type;
81 long signed_value;
c2ce0738 82
2cfd0562 83 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
859f11ff 84 bfd_byte *hit_data =addr + (bfd_byte *)(data);
2013f9b4 85
859f11ff 86 r_type = reloc_entry->howto->type;
f58809fd 87
2cfd0562
SC
88 if (output_bfd) {
89 /* Partial linking - do nothing */
90 reloc_entry->address += input_section->output_offset;
91 return bfd_reloc_ok;
92
93 }
94
859f11ff
SC
95 if (symbol_in && (symbol_in->section == &bfd_und_section))
96 {
97 /* Keep the state machine happy in case we're called again */
98 if (r_type == R_IHIHALF)
c2ce0738 99 {
859f11ff
SC
100 part1_consth_active = true;
101 part1_consth_value = 0;
f58809fd 102 }
859f11ff
SC
103 return(bfd_reloc_undefined);
104 }
f58809fd 105
859f11ff
SC
106 if ((part1_consth_active) && (r_type != R_IHCONST))
107 {
108 fprintf(stderr,"Relocation problem : ");
109 fprintf(stderr,"Missing IHCONST in module %s\n",abfd->filename);
110 part1_consth_active = false;
111 return(bfd_reloc_dangerous);
112 }
f58809fd 113
f58809fd 114
859f11ff
SC
115 sym_value = get_symbol_value(symbol_in);
116
117 switch (r_type)
118 {
119 case R_IREL:
120 insn = bfd_get_32(abfd, hit_data);
121 /* Take the value in the field and sign extend it */
122 signed_value = EXTRACT_HWORD(insn) << 2;
123 signed_value = SIGN_EXTEND_HWORD(signed_value);
124 signed_value += sym_value + reloc_entry->addend;
125 if ((signed_value&~0x3ffff) == 0)
126 { /* Absolute jmp/call */
127 insn |= (1<<24); /* Make it absolute */
128 /* FIXME: Should we change r_type to R_IABS */
859f11ff
SC
129 }
130 else
c2ce0738 131 {
859f11ff
SC
132 /* Relative jmp/call, so subtract from the value the
133 address of the place we're coming from */
134 signed_value -= reloc_entry->address +
135 input_section->output_section->vma +
136 input_section->output_offset;
137 if (signed_value>0x1ffff || signed_value<-0x20000)
138 return(bfd_reloc_outofrange);
859f11ff 139 }
bbbd93b8 140 signed_value >>= 2;
859f11ff
SC
141 insn = INSERT_HWORD(insn, signed_value);
142 bfd_put_32(abfd, insn ,hit_data);
143 break;
144 case R_ILOHALF:
145 insn = bfd_get_32(abfd, hit_data);
146 unsigned_value = EXTRACT_HWORD(insn);
147 unsigned_value += sym_value + reloc_entry->addend;
148 insn = INSERT_HWORD(insn, unsigned_value);
149 bfd_put_32(abfd, insn, hit_data);
150 break;
151 case R_IHIHALF:
152 insn = bfd_get_32(abfd, hit_data);
153 /* consth, part 1
154 Just get the symbol value that is referenced */
155 part1_consth_active = true;
156 part1_consth_value = sym_value + reloc_entry->addend;
157 /* Don't modify insn until R_IHCONST */
158 break;
159 case R_IHCONST:
160 insn = bfd_get_32(abfd, hit_data);
161 /* consth, part 2
162 Now relocate the reference */
163 if (part1_consth_active == false) {
164 fprintf(stderr,"Relocation problem : ");
165 fprintf(stderr,"IHIHALF missing in module %s\n",
166 abfd->filename);
167 return(bfd_reloc_dangerous);
168 }
169 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
170 unsigned_value = 0; /*EXTRACT_HWORD(insn) << 16;*/
171 unsigned_value += reloc_entry->addend; /* r_symndx */
172 unsigned_value += part1_consth_value;
173 unsigned_value = unsigned_value >> 16;
174 insn = INSERT_HWORD(insn, unsigned_value);
175 part1_consth_active = false;
176 bfd_put_32(abfd, insn, hit_data);
177 break;
178 case R_BYTE:
179 insn = bfd_get_8(abfd, hit_data);
180 unsigned_value = insn + sym_value + reloc_entry->addend;
181 if (unsigned_value & 0xffffff00) {
182 fprintf(stderr,"Relocation problem : ");
183 fprintf(stderr,"byte value too large in module %s\n",
184 abfd->filename);
185 return(bfd_reloc_overflow);
186 }
187 bfd_put_8(abfd, insn, hit_data);
188 break;
189 case R_HWORD:
190 insn = bfd_get_16(abfd, hit_data);
191 unsigned_value = insn + sym_value + reloc_entry->addend;
192 if (unsigned_value & 0xffff0000) {
193 fprintf(stderr,"Relocation problem : ");
194 fprintf(stderr,"hword value too large in module %s\n",
195 abfd->filename);
196 return(bfd_reloc_overflow);
f58809fd
SC
197 }
198
2cfd0562 199 bfd_put_16(abfd, insn, hit_data);
859f11ff
SC
200 break;
201 case R_WORD:
202 insn = bfd_get_32(abfd, hit_data);
203 insn += sym_value + reloc_entry->addend;
204 bfd_put_32(abfd, insn, hit_data);
205 break;
206 default:
207 fprintf(stderr,"Relocation problem : ");
208 fprintf(stderr,"Unrecognized reloc type %d, in module %s\n",
209 r_type,abfd->filename);
210 return (bfd_reloc_dangerous);
211 }
212
213
214 return(bfd_reloc_ok);
2013f9b4
SC
215}
216
217/* type rightshift
218 size
219 bitsize
220 pc-relative
221 bitpos
222 absolute
223 complain_on_overflow
224 special_function
225 relocation name
226 partial_inplace
227 src_mask
228*/
229
230/*FIXME: I'm not real sure about this table */
231#define NA 0 /* Obsolete fields, via the documentation */
21a7f8b6 232#define NAB false
2013f9b4
SC
233static reloc_howto_type howto_table[] =
234{
21a7f8b6 235 {R_ABS, 0, 3, NA, false, NA, NAB, true,a29k_reloc,"ABS", true, 0xffffffff,0xffffffff, false},
2013f9b4
SC
236 {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10},
237 {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20},
238 {21}, {22}, {23},
21a7f8b6
SC
239 {R_IREL, 0, 3, NA, true, NA, NAB, true,a29k_reloc,"IREL", true, 0xffffffff,0xffffffff, false},
240 {R_IABS, 0, 3, NA, false, NA, NAB, true,a29k_reloc,"IABS", true, 0xffffffff,0xffffffff, false},
241 {R_ILOHALF, 0, 3, NA, true, NA, NAB, true,a29k_reloc,"ILOHALF", true, 0x0000ffff,0x0000ffff, false},
242 {R_IHIHALF, 0, 3, NA, true, NA, NAB, true,a29k_reloc,"IHIHALF", true, 0xffff0000,0xffff0000, false},
243 {R_IHCONST, 0, 3, NA, true, NA, NAB, true,a29k_reloc,"IHCONST", true, 0xffff0000,0xffff0000, false},
244 {R_BYTE, 0, 0, NA, false, NA, NAB, true,a29k_reloc,"BYTE", true, 0x000000ff,0x000000ff, false},
245 {R_HWORD, 0, 1, NA, false, NA, NAB, true,a29k_reloc,"HWORD", true, 0x0000ffff,0x0000ffff, false},
246 {R_WORD, 0, 2, NA, false, NA, NAB, true,a29k_reloc,"WORD", true, 0xffffffff,0xffffffff, false},
2013f9b4
SC
247};
248#undef NA
249
250#define BADMAG(x) A29KBADMAG(x)
251
6cba8f4b
SC
252#define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
253 reloc_processing(relent, reloc, symbols, abfd, section)
254
e98e6ec1 255static void DEFUN(reloc_processing,(relent,reloc, symbols, abfd, section) ,
6cba8f4b
SC
256 arelent *relent AND
257 struct internal_reloc *reloc AND
258 asymbol **symbols AND
259 bfd *abfd AND
260 asection *section)
261{
262 relent->address = reloc->r_vaddr;
263 relent->howto = howto_table + reloc->r_type;
264 if (reloc->r_type == R_IHCONST)
265 {
266 relent->addend = reloc->r_symndx;
b772312e 267 relent->sym_ptr_ptr= bfd_abs_section.symbol_ptr_ptr;
6cba8f4b
SC
268 }
269 else
270 {
271 asymbol *ptr;
272 relent->sym_ptr_ptr = symbols + obj_convert(abfd)[reloc->r_symndx];
273
274 ptr = *(relent->sym_ptr_ptr);
275
276 if (ptr
277 && ptr->the_bfd == abfd
859f11ff 278
6cba8f4b
SC
279 && ((ptr->flags & BSF_OLD_COMMON)== 0))
280 {
859f11ff 281 relent->addend = 0;
6cba8f4b
SC
282 }
283 else
284 {
285 relent->addend = 0;
286 }
287 relent->address-= section->vma;
6cba8f4b
SC
288 }
289}
3b4f1a5d 290
2013f9b4
SC
291#include "coffcode.h"
292
293bfd_target a29kcoff_big_vec =
294{
c2ce0738
SC
295 "coff-a29k-big", /* name */
296 bfd_target_coff_flavour,
297 true, /* data byte order is big */
298 true, /* header byte order is big */
299
300 (HAS_RELOC | EXEC_P | /* object flags */
301 HAS_LINENO | HAS_DEBUG |
302 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
303
304 (SEC_HAS_CONTENTS | SEC_ALLOC /* section flags */
305 | SEC_LOAD | SEC_RELOC
306 | SEC_READONLY ),
307 '/', /* ar_pad_char */
308 15, /* ar_max_namelen */
309 2, /* minimum section alignment */
310 /* data */
311 _do_getb64, _do_putb64, _do_getb32,
312 _do_putb32, _do_getb16, _do_putb16,
313 /* hdrs */
314 _do_getb64, _do_putb64, _do_getb32,
315 _do_putb32, _do_getb16, _do_putb16,
316
317 {
318
319 _bfd_dummy_target,
320 coff_object_p,
321 bfd_generic_archive_p,
322 _bfd_dummy_target
323 },
324 {
325 bfd_false,
326 coff_mkobject,
327 _bfd_generic_mkarchive,
328 bfd_false
329 },
330 {
331 bfd_false,
332 coff_write_object_contents,
333 _bfd_write_archive_contents,
334 bfd_false
335 },
336
337 JUMP_TABLE(coff),
338 COFF_SWAP_TABLE
339 };
2013f9b4 340
This page took 0.055791 seconds and 4 git commands to generate.