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