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