Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back-end for ns32k a.out-ish binaries. |
1725a96e | 2 | Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1998, 2000, 2001 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | Contributed by Ian Dall (idall@eleceng.adelaide.edu.au). |
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | #define BYTES_IN_WORD 4 | |
23 | ||
24 | #include "bfd.h" | |
25 | #include "aout/aout64.h" | |
26 | #include "ns32k.h" | |
27 | ||
28 | #define MYNS(OP) CAT(ns32kaout_,OP) | |
29 | reloc_howto_type * | |
30 | MYNS(bfd_reloc_type_lookup) | |
31 | PARAMS((bfd *abfd AND | |
32 | bfd_reloc_code_real_type code)); | |
33 | ||
34 | boolean | |
35 | MYNS(write_object_contents) | |
36 | PARAMS((bfd *abfd)); | |
37 | ||
1725a96e NC |
38 | /* Avoid multiple definitions from aoutx if supporting |
39 | standard a.out format(s) as well as this one. */ | |
252b5132 RH |
40 | #define NAME(x,y) CAT3(ns32kaout,_32_,y) |
41 | ||
42 | void bfd_ns32k_arch PARAMS ((void)); | |
43 | ||
44 | #include "libaout.h" | |
45 | ||
46 | #define MY(OP) MYNS(OP) | |
47 | ||
48 | #define MY_swap_std_reloc_in MY(swap_std_reloc_in) | |
49 | #define MY_swap_std_reloc_out MY(swap_std_reloc_out) | |
50 | ||
51 | static void | |
52 | MY_swap_std_reloc_in PARAMS ((bfd *abfd, struct reloc_std_external *bytes, | |
53 | arelent *cache_ptr, asymbol **symbols, | |
54 | bfd_size_type symcount)); | |
252b5132 RH |
55 | static void |
56 | MY_swap_std_reloc_out PARAMS ((bfd *abfd, arelent *g, | |
57 | struct reloc_std_external *natptr)); | |
1725a96e NC |
58 | reloc_howto_type * |
59 | MY(reloc_howto) PARAMS ((bfd *, struct reloc_std_external *, | |
60 | int *, int *, int *)); | |
61 | void | |
dc810e39 | 62 | MY(put_reloc) PARAMS ((bfd *, int, int, bfd_vma, reloc_howto_type *, |
1725a96e | 63 | struct reloc_std_external *)); |
252b5132 RH |
64 | |
65 | /* The ns32k series is ah, unusual, when it comes to relocation. | |
1725a96e NC |
66 | There are three storage methods for relocateable objects. There |
67 | are displacements, immediate operands and ordinary twos complement | |
68 | data. Of these, only the last fits into the standard relocation | |
69 | scheme. Immediate operands are stored huffman encoded and | |
70 | immediate operands are stored big endian (where as the natural byte | |
71 | order is little endian for this achitecture). | |
72 | ||
73 | Note that the ns32k displacement storage method is orthogonal to | |
74 | whether the relocation is pc relative or not. The "displacement" | |
75 | storage scheme is used for essentially all address constants. The | |
76 | displacement can be relative to zero (absolute displacement), | |
77 | relative to the pc (pc relative), the stack pointer, the frame | |
78 | pointer, the static base register and general purpose register etc. | |
79 | ||
80 | For example: | |
dc810e39 | 81 | |
1725a96e NC |
82 | sym1: .long . # pc relative 2's complement |
83 | sym1: .long foo # 2's complement not pc relative | |
dc810e39 | 84 | |
1725a96e NC |
85 | self: movd @self, r0 # pc relative displacement |
86 | movd foo, r0 # non pc relative displacement | |
87 | ||
88 | self: movd self, r0 # pc relative immediate | |
89 | movd foo, r0 # non pc relative immediate | |
dc810e39 | 90 | |
1725a96e NC |
91 | In addition, for historical reasons the encoding of the relocation types |
92 | in the a.out format relocation entries is such that even the relocation | |
93 | methods which are standard are not encoded the standard way. */ | |
252b5132 | 94 | |
c4dfa77f | 95 | reloc_howto_type MY(howto_table)[] = |
1725a96e NC |
96 | { |
97 | /* ns32k immediate operands. */ | |
98 | HOWTO (BFD_RELOC_NS32K_IMM_8, 0, 0, 8, false, 0, true, | |
99 | _bfd_ns32k_reloc_imm, "NS32K_IMM_8", | |
100 | true, 0x000000ff,0x000000ff, false), | |
101 | HOWTO (BFD_RELOC_NS32K_IMM_16, 0, 1, 16, false, 0, true, | |
102 | _bfd_ns32k_reloc_imm, "NS32K_IMM_16", | |
103 | true, 0x0000ffff,0x0000ffff, false), | |
104 | HOWTO (BFD_RELOC_NS32K_IMM_32, 0, 2, 32, false, 0, true, | |
105 | _bfd_ns32k_reloc_imm, "NS32K_IMM_32", | |
106 | true, 0xffffffff,0xffffffff, false), | |
107 | HOWTO (BFD_RELOC_NS32K_IMM_8_PCREL, 0, 0, 8, true, 0, false, | |
108 | _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_8", | |
109 | true, 0x000000ff, 0x000000ff, false), | |
110 | HOWTO (BFD_RELOC_NS32K_IMM_16_PCREL, 0, 1, 16, true, 0, false, | |
111 | _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_16", | |
112 | true, 0x0000ffff,0x0000ffff, false), | |
113 | HOWTO (BFD_RELOC_NS32K_IMM_32_PCREL, 0, 2, 32, true, 0, false, | |
114 | _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_32", | |
115 | true, 0xffffffff,0xffffffff, false), | |
116 | ||
117 | /* ns32k displacements. */ | |
118 | HOWTO (BFD_RELOC_NS32K_DISP_8, 0, 0, 8, false, 0, true, | |
119 | _bfd_ns32k_reloc_disp, "NS32K_DISP_8", | |
120 | true, 0x000000ff,0x000000ff, false), | |
121 | HOWTO (BFD_RELOC_NS32K_DISP_16, 0, 1, 16, false, 0, true, | |
122 | _bfd_ns32k_reloc_disp, "NS32K_DISP_16", | |
123 | true, 0x0000ffff, 0x0000ffff, false), | |
124 | HOWTO (BFD_RELOC_NS32K_DISP_32, 0, 2, 32, false, 0, true, | |
125 | _bfd_ns32k_reloc_disp, "NS32K_DISP_32", | |
126 | true, 0xffffffff, 0xffffffff, false), | |
127 | HOWTO (BFD_RELOC_NS32K_DISP_8_PCREL, 0, 0, 8, true, 0, false, | |
128 | _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_8", | |
129 | true, 0x000000ff,0x000000ff, false), | |
130 | HOWTO (BFD_RELOC_NS32K_DISP_16_PCREL, 0, 1, 16, true, 0, false, | |
131 | _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_16", | |
132 | true, 0x0000ffff,0x0000ffff, false), | |
133 | HOWTO (BFD_RELOC_NS32K_DISP_32_PCREL, 0, 2, 32, true, 0, false, | |
134 | _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_32", | |
135 | true, 0xffffffff,0xffffffff, false), | |
136 | ||
137 | /* Normal 2's complement. */ | |
138 | HOWTO (BFD_RELOC_8, 0, 0, 8, false, 0, complain_overflow_bitfield,0, | |
139 | "8", true, 0x000000ff,0x000000ff, false), | |
140 | HOWTO (BFD_RELOC_16, 0, 1, 16, false, 0, complain_overflow_bitfield,0, | |
141 | "16", true, 0x0000ffff,0x0000ffff, false), | |
142 | HOWTO (BFD_RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield,0, | |
143 | "32", true, 0xffffffff,0xffffffff, false), | |
144 | HOWTO (BFD_RELOC_8_PCREL, 0, 0, 8, true, 0, complain_overflow_signed, 0, | |
145 | "PCREL_8", true, 0x000000ff,0x000000ff, false), | |
146 | HOWTO (BFD_RELOC_16_PCREL, 0, 1, 16, true, 0, complain_overflow_signed, 0, | |
147 | "PCREL_16", true, 0x0000ffff,0x0000ffff, false), | |
148 | HOWTO (BFD_RELOC_32_PCREL, 0, 2, 32, true, 0, complain_overflow_signed, 0, | |
149 | "PCREL_32", true, 0xffffffff,0xffffffff, false), | |
150 | }; | |
252b5132 | 151 | |
252b5132 RH |
152 | #define CTOR_TABLE_RELOC_HOWTO(BFD) (MY(howto_table) + 14) |
153 | ||
154 | #define RELOC_STD_BITS_NS32K_TYPE_BIG 0x06 | |
155 | #define RELOC_STD_BITS_NS32K_TYPE_LITTLE 0x60 | |
156 | #define RELOC_STD_BITS_NS32K_TYPE_SH_BIG 1 | |
157 | #define RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE 5 | |
158 | ||
159 | reloc_howto_type * | |
beb0d161 | 160 | MY(reloc_howto) (abfd, rel, r_index, r_extern, r_pcrel) |
5f771d47 | 161 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
162 | struct reloc_std_external *rel; |
163 | int *r_index; | |
164 | int *r_extern; | |
165 | int *r_pcrel; | |
166 | { | |
167 | unsigned int r_length; | |
168 | int r_ns32k_type; | |
1725a96e NC |
169 | |
170 | /* BFD_ASSERT(bfd_header_little_endian (abfd)); */ | |
252b5132 RH |
171 | *r_index = ((rel->r_index[2] << 16) |
172 | | (rel->r_index[1] << 8) | |
173 | | rel->r_index[0] ); | |
174 | *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE)); | |
175 | *r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE)); | |
176 | r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE) | |
177 | >> RELOC_STD_BITS_LENGTH_SH_LITTLE); | |
178 | r_ns32k_type = ((rel->r_type[0] & RELOC_STD_BITS_NS32K_TYPE_LITTLE) | |
179 | >> RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE); | |
180 | return (MY(howto_table) + r_length + 3 * (*r_pcrel) + 6 * r_ns32k_type); | |
181 | } | |
182 | ||
dc810e39 AM |
183 | #define MY_reloc_howto(BFD, REL, IN, EX, PC) \ |
184 | MY(reloc_howto) (BFD, REL, &IN, &EX, &PC) | |
252b5132 RH |
185 | |
186 | void | |
beb0d161 | 187 | MY(put_reloc) (abfd, r_extern, r_index, value, howto, reloc) |
252b5132 RH |
188 | bfd *abfd; |
189 | int r_extern; | |
190 | int r_index; | |
dc810e39 | 191 | bfd_vma value; |
252b5132 RH |
192 | reloc_howto_type *howto; |
193 | struct reloc_std_external *reloc; | |
194 | { | |
195 | unsigned int r_length; | |
196 | int r_pcrel; | |
197 | int r_ns32k_type; | |
1725a96e | 198 | |
252b5132 | 199 | PUT_WORD (abfd, value, reloc->r_address); |
1725a96e NC |
200 | r_length = howto->size ; /* Size as a power of two. */ |
201 | r_pcrel = (int) howto->pc_relative; /* Relative to PC? */ | |
252b5132 | 202 | r_ns32k_type = (howto - MY(howto_table) )/6; |
1725a96e NC |
203 | |
204 | /* BFD_ASSERT (bfd_header_little_endian (abfd)); */ | |
252b5132 RH |
205 | reloc->r_index[2] = r_index >> 16; |
206 | reloc->r_index[1] = r_index >> 8; | |
207 | reloc->r_index[0] = r_index; | |
208 | reloc->r_type[0] = | |
209 | (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0) | |
210 | | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0) | |
211 | | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE) | |
212 | | (r_ns32k_type << RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE); | |
213 | } | |
214 | ||
215 | #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \ | |
beb0d161 | 216 | MY(put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC) |
252b5132 RH |
217 | |
218 | #define STAT_FOR_EXEC | |
219 | ||
220 | #define MY_final_link_relocate _bfd_ns32k_final_link_relocate | |
221 | #define MY_relocate_contents _bfd_ns32k_relocate_contents | |
222 | ||
dc810e39 | 223 | #include "aoutx.h" |
252b5132 RH |
224 | |
225 | reloc_howto_type * | |
beb0d161 | 226 | MY(bfd_reloc_type_lookup) (abfd,code) |
252b5132 RH |
227 | bfd *abfd; |
228 | bfd_reloc_code_real_type code; | |
229 | { | |
230 | ||
231 | #define ENTRY(i,j) case i: return &MY(howto_table)[j] | |
232 | ||
233 | int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE; | |
234 | ||
235 | BFD_ASSERT(ext == 0); | |
236 | if (code == BFD_RELOC_CTOR) | |
237 | switch (bfd_get_arch_info (abfd)->bits_per_address) | |
238 | { | |
239 | case 32: | |
240 | code = BFD_RELOC_32; | |
241 | break; | |
1725a96e NC |
242 | default: |
243 | break; | |
252b5132 RH |
244 | } |
245 | switch (code) | |
246 | { | |
247 | ENTRY(BFD_RELOC_NS32K_IMM_8, 0); | |
248 | ENTRY(BFD_RELOC_NS32K_IMM_16, 1); | |
249 | ENTRY(BFD_RELOC_NS32K_IMM_32, 2); | |
250 | ENTRY(BFD_RELOC_NS32K_IMM_8_PCREL, 3); | |
251 | ENTRY(BFD_RELOC_NS32K_IMM_16_PCREL, 4); | |
252 | ENTRY(BFD_RELOC_NS32K_IMM_32_PCREL, 5); | |
253 | ENTRY(BFD_RELOC_NS32K_DISP_8, 6); | |
254 | ENTRY(BFD_RELOC_NS32K_DISP_16, 7); | |
255 | ENTRY(BFD_RELOC_NS32K_DISP_32, 8); | |
256 | ENTRY(BFD_RELOC_NS32K_DISP_8_PCREL, 9); | |
257 | ENTRY(BFD_RELOC_NS32K_DISP_16_PCREL, 10); | |
258 | ENTRY(BFD_RELOC_NS32K_DISP_32_PCREL, 11); | |
259 | ENTRY(BFD_RELOC_8, 12); | |
260 | ENTRY(BFD_RELOC_16, 13); | |
261 | ENTRY(BFD_RELOC_32, 14); | |
262 | ENTRY(BFD_RELOC_8_PCREL, 15); | |
263 | ENTRY(BFD_RELOC_16_PCREL, 16); | |
264 | ENTRY(BFD_RELOC_32_PCREL, 17); | |
1725a96e NC |
265 | default: |
266 | return (reloc_howto_type *) NULL; | |
252b5132 RH |
267 | } |
268 | #undef ENTRY | |
269 | } | |
270 | ||
252b5132 RH |
271 | static void |
272 | MY_swap_std_reloc_in (abfd, bytes, cache_ptr, symbols, symcount) | |
273 | bfd *abfd; | |
274 | struct reloc_std_external *bytes; | |
275 | arelent *cache_ptr; | |
276 | asymbol **symbols; | |
5f771d47 | 277 | bfd_size_type symcount ATTRIBUTE_UNUSED; |
252b5132 RH |
278 | { |
279 | int r_index; | |
280 | int r_extern; | |
281 | int r_pcrel; | |
282 | struct aoutdata *su = &(abfd->tdata.aout_data->a); | |
283 | ||
dc810e39 | 284 | cache_ptr->address = H_GET_32 (abfd, bytes->r_address); |
252b5132 | 285 | |
1725a96e | 286 | /* Now the fun stuff. */ |
252b5132 RH |
287 | cache_ptr->howto = MY_reloc_howto(abfd, bytes, r_index, r_extern, r_pcrel); |
288 | ||
1725a96e | 289 | MOVE_ADDRESS (0); |
252b5132 RH |
290 | } |
291 | ||
292 | static void | |
293 | MY_swap_std_reloc_out (abfd, g, natptr) | |
294 | bfd *abfd; | |
295 | arelent *g; | |
296 | struct reloc_std_external *natptr; | |
297 | { | |
298 | int r_index; | |
299 | asymbol *sym = *(g->sym_ptr_ptr); | |
300 | int r_extern; | |
301 | unsigned int r_addend; | |
302 | asection *output_section = sym->section->output_section; | |
303 | ||
304 | r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma; | |
c4dfa77f | 305 | |
1725a96e | 306 | /* Name was clobbered by aout_write_syms to be symbol index. */ |
252b5132 | 307 | |
c4dfa77f | 308 | /* If this relocation is relative to a symbol then set the |
252b5132 RH |
309 | r_index to the symbols index, and the r_extern bit. |
310 | ||
311 | Absolute symbols can come in in two ways, either as an offset | |
312 | from the abs section, or as a symbol which has an abs value. | |
313 | Check for that here. */ | |
252b5132 RH |
314 | if (bfd_is_com_section (output_section) |
315 | || output_section == &bfd_abs_section | |
c4dfa77f | 316 | || output_section == &bfd_und_section) |
252b5132 RH |
317 | { |
318 | if (bfd_abs_section.symbol == sym) | |
319 | { | |
320 | /* Whoops, looked like an abs symbol, but is really an offset | |
1725a96e | 321 | from the abs section. */ |
252b5132 RH |
322 | r_index = 0; |
323 | r_extern = 0; | |
324 | } | |
c4dfa77f | 325 | else |
252b5132 | 326 | { |
1725a96e | 327 | /* Fill in symbol. */ |
252b5132 RH |
328 | r_extern = 1; |
329 | #undef KEEPIT | |
330 | #define KEEPIT udata.i | |
331 | r_index = (*(g->sym_ptr_ptr))->KEEPIT; | |
c4dfa77f | 332 | #undef KEEPIT |
252b5132 RH |
333 | } |
334 | } | |
c4dfa77f | 335 | else |
252b5132 | 336 | { |
1725a96e | 337 | /* Just an ordinary section. */ |
252b5132 | 338 | r_extern = 0; |
c4dfa77f | 339 | r_index = output_section->target_index; |
252b5132 RH |
340 | } |
341 | ||
342 | MY_put_reloc (abfd, r_extern, r_index, g->address, g->howto, natptr); | |
343 | } | |
344 | ||
345 | bfd_reloc_status_type | |
346 | _bfd_ns32k_relocate_contents (howto, input_bfd, relocation, location) | |
347 | reloc_howto_type *howto; | |
348 | bfd *input_bfd; | |
349 | bfd_vma relocation; | |
350 | bfd_byte *location; | |
351 | { | |
352 | int r_ns32k_type = (howto - MY(howto_table)) / 6; | |
dc810e39 AM |
353 | bfd_vma (*get_data) PARAMS ((bfd_byte *, int)); |
354 | int (*put_data) PARAMS ((bfd_vma, bfd_byte *, int)); | |
252b5132 RH |
355 | |
356 | switch (r_ns32k_type) | |
357 | { | |
358 | case 0: | |
359 | get_data = _bfd_ns32k_get_immediate; | |
360 | put_data = _bfd_ns32k_put_immediate; | |
361 | break; | |
362 | case 1: | |
363 | get_data = _bfd_ns32k_get_displacement; | |
364 | put_data = _bfd_ns32k_put_displacement; | |
365 | break; | |
366 | case 2: | |
367 | return _bfd_relocate_contents (howto, input_bfd, relocation, | |
368 | location); | |
369 | /* NOT REACHED */ | |
370 | break; | |
371 | default: | |
372 | return bfd_reloc_notsupported; | |
373 | } | |
374 | return _bfd_do_ns32k_reloc_contents (howto, input_bfd, relocation, | |
375 | location, get_data, put_data); | |
376 | } |