1 /* BFD back-end for ns32k a.out-ish binaries.
2 Copyright (C) 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
3 Contributed by Ian Dall (idall@eleceng.adelaide.edu.au).
5 This file is part of BFD, the Binary File Descriptor library.
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
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
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #define BYTES_IN_WORD 4
24 #include "aout/aout64.h"
26 #define MYNS(OP) CAT(ns32kaout_,OP)
27 CONST
struct reloc_howto_struct
*
28 MYNS(bfd_reloc_type_lookup
)
30 bfd_reloc_code_real_type code
));
33 MYNS(write_object_contents
)
36 /* Avoid multiple definitions from aoutx if supporting standard a.out format(s)
39 #define NAME(x,y) CAT3(ns32kaout,_32_,y)
41 void bfd_ns32k_arch
PARAMS ((void));
42 long ns32k_get_displacement
PARAMS ((bfd_byte
*buffer
, long offset
, long size
));
43 int ns32k_put_displacement
PARAMS ((long value
, bfd_byte
*buffer
, long offset
, long size
));
44 long ns32k_get_immediate
PARAMS ((bfd_byte
*buffer
, long offset
, long size
));
45 int ns32k_put_immediate
PARAMS ((long value
, bfd_byte
*buffer
, long offset
, long size
));
47 ns32k_reloc_disp
PARAMS ((bfd
*abfd
, arelent
*reloc_entry
,
48 struct symbol_cache_entry
*symbol
,
50 asection
*input_section
,
52 char **error_message
));
54 ns32k_reloc_imm
PARAMS ((bfd
*abfd
,
56 struct symbol_cache_entry
*symbol
,
58 asection
*input_section
,
60 char **error_message
));
62 ns32k_final_link_relocate
PARAMS ((reloc_howto_type
*howto
,
64 asection
*input_section
,
70 ns32k_relocate_contents
PARAMS ((reloc_howto_type
*howto
,
77 #define MY(OP) MYNS(OP)
79 #define MY_swap_std_reloc_in MY(swap_std_reloc_in)
80 #define MY_swap_std_reloc_out MY(swap_std_reloc_out)
83 MY_swap_std_reloc_in
PARAMS ((bfd
*abfd
, struct reloc_std_external
*bytes
,
84 arelent
*cache_ptr
, asymbol
**symbols
));
87 MY_swap_std_reloc_out
PARAMS ((bfd
*abfd
, arelent
*g
,
88 struct reloc_std_external
*natptr
));
90 /* The ns32k series is ah, unusual, when it comes to relocation.
91 * There are three storage methods for relocateable objects. There
92 * are displacements, immediate operands and ordinary twos complement
93 * data. Of these, only the last fits into the standard relocation
94 * scheme. Immediate operands are stored huffman encoded and
95 * immediate operands are stored big endian (where as the natural byte
96 * order is little endian for this achitecture).
98 * Note that the ns32k displacement storage method is orthogonal to
99 * whether the relocation is pc relative or not. The "displacement"
100 * storage scheme is used for essentially all address constants. The
101 * displacement can be relative to zero (absolute displacement),
102 * relative to the pc (pc relative), the stack pointer, the frame
103 * pointer, the static base register and general purpose register etc.
107 * sym1: .long . # pc relative 2's complement
108 * sym1: .long foo # 2's complement not pc relative
110 * self: movd @self, r0 # pc relative displacement
111 * movd foo, r0 # non pc relative displacement
113 * self: movd self, r0 # pc relative immediate
114 * movd foo, r0 # non pc relative immediate
116 * In addition, for historical reasons the encoding of the relocation types
117 * in the a.out format relocation entries is such that even the relocation
118 * methods which are standard are not encoded the standard way.
122 reloc_howto_type
MY(howto_table
)[] =
124 /* ns32k immediate operands */
125 HOWTO(BFD_RELOC_NS32K_IMM_8
, 0, 0, 8, false, 0, true,
126 ns32k_reloc_imm
, "NS32K_IMM_8",
127 true, 0x000000ff,0x000000ff, false),
128 HOWTO(BFD_RELOC_NS32K_IMM_16
, 0, 1, 16, false, 0, true,
129 ns32k_reloc_imm
, "NS32K_IMM_16",
130 true, 0x0000ffff,0x0000ffff, false),
131 HOWTO(BFD_RELOC_NS32K_IMM_32
, 0, 2, 32, false, 0, true,
132 ns32k_reloc_imm
, "NS32K_IMM_32",
133 true, 0xffffffff,0xffffffff, false),
134 HOWTO(BFD_RELOC_NS32K_IMM_8_PCREL
, 0, 0, 8, true, 0, false,
135 ns32k_reloc_imm
, "PCREL_NS32K_IMM_8",
136 true, 0x000000ff, 0x000000ff, false),
137 HOWTO(BFD_RELOC_NS32K_IMM_16_PCREL
, 0, 1, 16, true, 0, false,
138 ns32k_reloc_imm
, "PCREL_NS32K_IMM_16",
139 true, 0x0000ffff,0x0000ffff, false),
140 HOWTO(BFD_RELOC_NS32K_IMM_32_PCREL
, 0, 2, 32, true, 0, false,
141 ns32k_reloc_imm
, "PCREL_NS32K_IMM_32",
142 true, 0xffffffff,0xffffffff, false),
144 /* ns32k displacements */
145 HOWTO(BFD_RELOC_NS32K_DISP_8
, 0, 0, 8, false, 0, true,
146 ns32k_reloc_disp
, "NS32K_DISP_8",
147 true, 0x000000ff,0x000000ff, false),
148 HOWTO(BFD_RELOC_NS32K_DISP_16
, 0, 1, 16, false, 0, true,
149 ns32k_reloc_disp
, "NS32K_DISP_16",
150 true, 0x0000ffff, 0x0000ffff, false),
151 HOWTO(BFD_RELOC_NS32K_DISP_32
, 0, 2, 32, false, 0, true,
152 ns32k_reloc_disp
, "NS32K_DISP_32",
153 true, 0xffffffff, 0xffffffff, false),
154 HOWTO(BFD_RELOC_NS32K_DISP_8_PCREL
, 0, 0, 8, true, 0, false,
155 ns32k_reloc_disp
, "PCREL_NS32K_DISP_8",
156 true, 0x000000ff,0x000000ff, false),
157 HOWTO(BFD_RELOC_NS32K_DISP_16_PCREL
, 0, 1, 16, true, 0, false,
158 ns32k_reloc_disp
, "PCREL_NS32K_DISP_16",
159 true, 0x0000ffff,0x0000ffff, false),
160 HOWTO(BFD_RELOC_NS32K_DISP_32_PCREL
, 0, 2, 32, true, 0, false,
161 ns32k_reloc_disp
, "PCREL_NS32K_DISP_32",
162 true, 0xffffffff,0xffffffff, false),
164 /* Normal 2's complement */
165 HOWTO(BFD_RELOC_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,
166 "8", true, 0x000000ff,0x000000ff, false),
167 HOWTO(BFD_RELOC_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,
168 "16", true, 0x0000ffff,0x0000ffff, false),
169 HOWTO(BFD_RELOC_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,
170 "32", true, 0xffffffff,0xffffffff, false),
171 HOWTO(BFD_RELOC_8_PCREL
, 0, 0, 8, true, 0, complain_overflow_signed
, 0,
172 "PCREL_8", true, 0x000000ff,0x000000ff, false),
173 HOWTO(BFD_RELOC_16_PCREL
, 0, 1, 16, true, 0, complain_overflow_signed
, 0,
174 "PCREL_16", true, 0x0000ffff,0x0000ffff, false),
175 HOWTO(BFD_RELOC_32_PCREL
, 0, 2, 32, true, 0, complain_overflow_signed
, 0,
176 "PCREL_32", true, 0xffffffff,0xffffffff, false),
179 #define RELOC_STD_BITS_NS32K_TYPE_BIG 0x06
180 #define RELOC_STD_BITS_NS32K_TYPE_LITTLE 0x60
181 #define RELOC_STD_BITS_NS32K_TYPE_SH_BIG 1
182 #define RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE 5
185 MY(reloc_howto
)(abfd
, rel
, r_index
, r_extern
, r_pcrel
)
187 struct reloc_std_external
*rel
;
192 unsigned int r_length
;
194 /* BFD_ASSERT(abfd->xvec->header_byteorder_big_p == false); */
195 *r_index
= ((rel
->r_index
[2] << 16)
196 | (rel
->r_index
[1] << 8)
198 *r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
199 *r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
200 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
201 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
202 r_ns32k_type
= ((rel
->r_type
[0] & RELOC_STD_BITS_NS32K_TYPE_LITTLE
)
203 >> RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE
);
204 return (MY(howto_table
) + r_length
+ 3 * (*r_pcrel
) + 6 * r_ns32k_type
);
207 #define MY_reloc_howto(BFD,REL,IN,EX,PC) MY(reloc_howto)(BFD, REL, &IN, &EX, &PC)
210 MY(put_reloc
)(abfd
, r_extern
, r_index
, value
, howto
, reloc
)
215 reloc_howto_type
*howto
;
216 struct reloc_std_external
*reloc
;
218 unsigned int r_length
;
221 PUT_WORD (abfd
, value
, reloc
->r_address
);
222 r_length
= howto
->size
; /* Size as a power of two */
223 r_pcrel
= (int) howto
->pc_relative
; /* Relative to PC? */
224 r_ns32k_type
= (howto
- MY(howto_table
) )/6;
225 /* BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false); */
226 reloc
->r_index
[2] = r_index
>> 16;
227 reloc
->r_index
[1] = r_index
>> 8;
228 reloc
->r_index
[0] = r_index
;
230 (r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
231 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
232 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
)
233 | (r_ns32k_type
<< RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE
);
236 #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
237 MY(put_reloc)(BFD, EXT, IDX, VAL, HOWTO, RELOC)
239 #define STAT_FOR_EXEC
241 #define MY_final_link_relocate ns32k_final_link_relocate
242 #define MY_relocate_contents ns32k_relocate_contents
246 CONST
struct reloc_howto_struct
*
247 MY(bfd_reloc_type_lookup
)(abfd
,code
)
249 bfd_reloc_code_real_type code
;
252 #define ENTRY(i,j) case i: return &MY(howto_table)[j]
254 int ext
= obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
;
256 BFD_ASSERT(ext
== 0);
257 if (code
== BFD_RELOC_CTOR
)
258 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
266 ENTRY(BFD_RELOC_NS32K_IMM_8
, 0);
267 ENTRY(BFD_RELOC_NS32K_IMM_16
, 1);
268 ENTRY(BFD_RELOC_NS32K_IMM_32
, 2);
269 ENTRY(BFD_RELOC_NS32K_IMM_8_PCREL
, 3);
270 ENTRY(BFD_RELOC_NS32K_IMM_16_PCREL
, 4);
271 ENTRY(BFD_RELOC_NS32K_IMM_32_PCREL
, 5);
272 ENTRY(BFD_RELOC_NS32K_DISP_8
, 6);
273 ENTRY(BFD_RELOC_NS32K_DISP_16
, 7);
274 ENTRY(BFD_RELOC_NS32K_DISP_32
, 8);
275 ENTRY(BFD_RELOC_NS32K_DISP_8_PCREL
, 9);
276 ENTRY(BFD_RELOC_NS32K_DISP_16_PCREL
, 10);
277 ENTRY(BFD_RELOC_NS32K_DISP_32_PCREL
, 11);
278 ENTRY(BFD_RELOC_8
, 12);
279 ENTRY(BFD_RELOC_16
, 13);
280 ENTRY(BFD_RELOC_32
, 14);
281 ENTRY(BFD_RELOC_8_PCREL
, 15);
282 ENTRY(BFD_RELOC_16_PCREL
, 16);
283 ENTRY(BFD_RELOC_32_PCREL
, 17);
284 default: return (CONST
struct reloc_howto_struct
*) 0;
291 MY_swap_std_reloc_in (abfd
, bytes
, cache_ptr
, symbols
)
293 struct reloc_std_external
*bytes
;
299 unsigned int r_length
;
302 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
304 cache_ptr
->address
= bfd_h_get_32 (abfd
, bytes
->r_address
);
306 /* now the fun stuff */
308 cache_ptr
->howto
= MY_reloc_howto(abfd
, bytes
, r_index
, r_extern
, r_pcrel
);
314 MY_swap_std_reloc_out (abfd
, g
, natptr
)
317 struct reloc_std_external
*natptr
;
320 asymbol
*sym
= *(g
->sym_ptr_ptr
);
322 unsigned int r_length
;
325 unsigned int r_addend
;
326 asection
*output_section
= sym
->section
->output_section
;
328 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
330 /* name was clobbered by aout_write_syms to be symbol index */
332 /* If this relocation is relative to a symbol then set the
333 r_index to the symbols index, and the r_extern bit.
335 Absolute symbols can come in in two ways, either as an offset
336 from the abs section, or as a symbol which has an abs value.
337 Check for that here. */
339 if (bfd_is_com_section (output_section
)
340 || output_section
== &bfd_abs_section
341 || output_section
== &bfd_und_section
)
343 if (bfd_abs_section
.symbol
== sym
)
345 /* Whoops, looked like an abs symbol, but is really an offset
346 from the abs section */
355 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
361 /* Just an ordinary section */
363 r_index
= output_section
->target_index
;
366 MY_put_reloc (abfd
, r_extern
, r_index
, g
->address
, g
->howto
, natptr
);
369 bfd_reloc_status_type
370 ns32k_relocate_contents (howto
, input_bfd
, relocation
, location
)
371 reloc_howto_type
*howto
;
376 int r_ns32k_type
= (howto
- MY(howto_table
)) / 6;
380 switch (r_ns32k_type
)
383 get_data
= ns32k_get_immediate
;
384 put_data
= ns32k_put_immediate
;
387 get_data
= ns32k_get_displacement
;
388 put_data
= ns32k_put_displacement
;
391 return _bfd_relocate_contents (howto
, input_bfd
, relocation
,
396 return do_ns32k_reloc_contents (howto
, input_bfd
, relocation
,
397 location
, get_data
, put_data
);