1 /* HP PA-RISC SOM object file format: definitions internal to BFD.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 98, 99, 2000
3 Free Software Foundation, Inc.
5 Contributed by the Center for Software Science at the
6 University of Utah (pa-gdb-bugs@cs.utah.edu).
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #define BYTES_IN_WORD 4
28 #define PA_PAGESIZE 0x1000
38 /* The PA instruction set variants. */
39 enum pa_arch
{pa10
= 10, pa11
= 11, pa20
= 20, pa20w
= 25};
41 /* HP PA-RISC relocation types */
43 enum hppa_reloc_field_selector_type
67 /* /usr/include/reloc.h defines these to constants. We want to use
68 them in enums, so #undef them before we start using them. We might
69 be able to fix this another way by simply managing not to include
70 /usr/include/reloc.h, but currently GDB picks up these defines
97 /* for compatibility */
98 enum hppa_reloc_field_selector_type_alt
100 e_fsel
= R_HPPA_FSEL
,
101 e_lssel
= R_HPPA_LSSEL
,
102 e_rssel
= R_HPPA_RSSEL
,
103 e_lsel
= R_HPPA_LSEL
,
104 e_rsel
= R_HPPA_RSEL
,
105 e_ldsel
= R_HPPA_LDSEL
,
106 e_rdsel
= R_HPPA_RDSEL
,
107 e_lrsel
= R_HPPA_LRSEL
,
108 e_rrsel
= R_HPPA_RRSEL
,
109 e_nsel
= R_HPPA_NSEL
,
110 e_nlsel
= R_HPPA_NLSEL
,
111 e_nlrsel
= R_HPPA_NLRSEL
,
112 e_psel
= R_HPPA_PSEL
,
113 e_lpsel
= R_HPPA_LPSEL
,
114 e_rpsel
= R_HPPA_RPSEL
,
115 e_tsel
= R_HPPA_TSEL
,
116 e_ltsel
= R_HPPA_LTSEL
,
117 e_rtsel
= R_HPPA_RTSEL
,
118 e_ltpsel
= R_HPPA_LTPSEL
,
119 e_rtpsel
= R_HPPA_RTPSEL
122 enum hppa_reloc_expr_type
132 /* for compatibility */
133 enum hppa_reloc_expr_type_alt
135 e_one
= R_HPPA_E_ONE
,
136 e_two
= R_HPPA_E_TWO
,
137 e_pcrel
= R_HPPA_E_PCREL
,
138 e_con
= R_HPPA_E_CON
,
139 e_plabel
= R_HPPA_E_PLABEL
,
144 /* Relocations for function calls must be accompanied by parameter
145 relocation bits. These bits describe exactly where the caller has
146 placed the function's arguments and where it expects to find a return
149 Both ELF and SOM encode this information within the addend field
150 of the call relocation. (Note this could break very badly if one
151 was to make a call like bl foo + 0x12345678).
153 The high order 10 bits contain parameter relocation information,
154 the low order 22 bits contain the constant offset. */
156 #define HPPA_R_ARG_RELOC(a) \
157 (((a) >> 22) & 0x3ff)
158 #define HPPA_R_CONSTANT(a) \
159 ((((bfd_signed_vma)(a)) << (BFD_ARCH_SIZE-22)) >> (BFD_ARCH_SIZE-22))
160 #define HPPA_R_ADDEND(r, c) \
161 (((r) << 22) + ((c) & 0x3fffff))
164 /* Some functions to manipulate PA instructions. */
166 /* Declare the functions with the unused attribute to avoid warnings. */
167 static INLINE
int sign_extend
PARAMS ((int, int)) ATTRIBUTE_UNUSED
;
168 static INLINE
int low_sign_extend
PARAMS ((int, int)) ATTRIBUTE_UNUSED
;
169 static INLINE
int sign_unext
PARAMS ((int, int)) ATTRIBUTE_UNUSED
;
170 static INLINE
int low_sign_unext
PARAMS ((int, int)) ATTRIBUTE_UNUSED
;
171 static INLINE
int re_assemble_3
PARAMS ((int)) ATTRIBUTE_UNUSED
;
172 static INLINE
int re_assemble_12
PARAMS ((int)) ATTRIBUTE_UNUSED
;
173 static INLINE
int re_assemble_14
PARAMS ((int)) ATTRIBUTE_UNUSED
;
174 static INLINE
int re_assemble_16
PARAMS ((int)) ATTRIBUTE_UNUSED
;
175 static INLINE
int re_assemble_17
PARAMS ((int)) ATTRIBUTE_UNUSED
;
176 static INLINE
int re_assemble_21
PARAMS ((int)) ATTRIBUTE_UNUSED
;
177 static INLINE
int re_assemble_22
PARAMS ((int)) ATTRIBUTE_UNUSED
;
178 static INLINE bfd_signed_vma hppa_field_adjust
179 PARAMS ((bfd_vma
, bfd_signed_vma
,
180 enum hppa_reloc_field_selector_type_alt
)) ATTRIBUTE_UNUSED
;
181 static INLINE
int bfd_hppa_insn2fmt
PARAMS ((bfd
*, int)) ATTRIBUTE_UNUSED
;
182 static INLINE
int hppa_rebuild_insn
PARAMS ((int, int, int)) ATTRIBUTE_UNUSED
;
185 /* The *sign_extend functions are used to assemble various bitfields
186 taken from an instruction and return the resulting immediate
193 int signbit
= (1 << (len
- 1));
194 int mask
= (signbit
<< 1) - 1;
195 return ((x
& mask
) ^ signbit
) - signbit
;
199 low_sign_extend (x
, len
)
202 return (x
>> 1) - ((x
& 1) << (len
- 1));
206 /* The re_assemble_* functions prepare an immediate value for
207 insertion into an opcode. pa-risc uses all sorts of weird bitfields
208 in the instruction to hold the value. */
216 len_ones
= (1 << len
) - 1;
222 low_sign_unext (x
, len
)
228 sign
= (x
>> (len
-1)) & 1;
230 temp
= sign_unext (x
, len
-1);
232 return (temp
<< 1) | sign
;
239 return (( (as3
& 4) << (13-2))
240 | ((as3
& 3) << (13+1)));
244 re_assemble_12 (as12
)
247 return (( (as12
& 0x800) >> 11)
248 | ((as12
& 0x400) >> (10 - 2))
249 | ((as12
& 0x3ff) << (1 + 2)));
253 re_assemble_14 (as14
)
256 return (( (as14
& 0x1fff) << 1)
257 | ((as14
& 0x2000) >> 13));
261 re_assemble_16 (as16
)
266 /* Unusual 16-bit encoding, for wide mode only. */
267 t
= (as16
<< 1) & 0xffff;
269 return (t
^ s
^ (s
>> 1)) | (s
>> 15);
273 re_assemble_17 (as17
)
276 return (( (as17
& 0x10000) >> 16)
277 | ((as17
& 0x0f800) << (16 - 11))
278 | ((as17
& 0x00400) >> (10 - 2))
279 | ((as17
& 0x003ff) << (1 + 2)));
283 re_assemble_21 (as21
)
286 return (( (as21
& 0x100000) >> 20)
287 | ((as21
& 0x0ffe00) >> 8)
288 | ((as21
& 0x000180) << 7)
289 | ((as21
& 0x00007c) << 14)
290 | ((as21
& 0x000003) << 12));
294 re_assemble_22 (as22
)
297 return (( (as22
& 0x200000) >> 21)
298 | ((as22
& 0x1f0000) << (21 - 16))
299 | ((as22
& 0x00f800) << (16 - 11))
300 | ((as22
& 0x000400) >> (10 - 2))
301 | ((as22
& 0x0003ff) << (1 + 2)));
305 /* Handle field selectors for PA instructions.
306 The L and R (and LS, RS etc.) selectors are used in pairs to form a
307 full 32 bit address. eg.
309 LDIL L'start,%r1 ; put left part into r1
310 LDW R'start(%r1),%r2 ; add r1 and right part to form address
312 This function returns sign extended values in all cases.
315 static INLINE bfd_signed_vma
316 hppa_field_adjust (sym_val
, addend
, r_field
)
318 bfd_signed_vma addend
;
319 enum hppa_reloc_field_selector_type_alt r_field
;
321 bfd_signed_vma value
;
323 value
= sym_val
+ addend
;
331 /* N: null selector. I don't really understand what this is all
332 about, but HP's documentation says "this indicates that zero
333 bits are to be used for the displacement on the instruction.
334 This fixup is used to identify three-instruction sequences to
335 access data (for importing shared library data)." */
341 /* L: Select top 21 bits. */
346 /* R: Select bottom 11 bits. */
347 value
= value
& 0x7ff;
351 /* LS: Round to nearest multiple of 2048 then select top 21 bits. */
352 value
= value
+ 0x400;
357 /* RS: Select bottom 11 bits for LS.
358 We need to return a value such that 2048 * LS'x + RS'x == x.
359 ie. RS'x = x - ((x + 0x400) & -0x800)
360 this is just a sign extension from bit 21. */
361 value
= ((value
& 0x7ff) ^ 0x400) - 0x400;
365 /* LD: Round to next multiple of 2048 then select top 21 bits.
366 Yes, if we are already on a multiple of 2048, we go up to the
367 next one. RD in this case will be -2048. */
368 value
= value
+ 0x800;
373 /* RD: Set bits 0-20 to one. */
374 value
= value
| -0x800;
379 /* LR: L with rounding of the addend to nearest 8k. */
380 value
= sym_val
+ ((addend
+ 0x1000) & -0x2000);
385 /* RR: R with rounding of the addend to nearest 8k.
386 We need to return a value such that 2048 * LR'x + RR'x == x
387 ie. RR'x = s+a - (s + (((a + 0x1000) & -0x2000) & -0x800))
388 . = s+a - ((s & -0x800) + ((a + 0x1000) & -0x2000))
389 . = (s & 0x7ff) + a - ((a + 0x1000) & -0x2000) */
390 value
= (sym_val
& 0x7ff) + (((addend
& 0x1fff) ^ 0x1000) - 0x1000);
399 /* PA-RISC OPCODES */
400 #define get_opcode(insn) (((insn) >> 26) & 0x3f)
402 enum hppa_opcode_type
404 /* None of the opcodes in the first group generate relocs, so we
405 aren't too concerned about them. */
480 /* Given a machine instruction, return its format. */
483 bfd_hppa_insn2fmt (abfd
, insn
)
487 enum hppa_opcode_type op
= get_opcode (insn
);
523 if (abfd
->arch_info
->mach
>= 25)
524 return 16; /* Wide mode, format 16. */
531 /* This is a hack. Unfortunately, format 11 is already taken
532 and we're using integers rather than an enum, so it's hard
533 to describe the 11a format. */
534 if (abfd
->arch_info
->mach
>= 25)
535 return -16; /* Wide mode, format 16a. */
540 if (abfd
->arch_info
->mach
>= 25)
541 return -10; /* Wide mode, format 10a. */
545 if ((insn
& 0x8000) != 0)
563 /* Insert VALUE into INSN using R_FORMAT to determine exactly what
567 hppa_rebuild_insn (insn
, value
, r_format
)
575 return (insn
& ~ 0x7ff) | low_sign_unext (value
, 11);
578 return (insn
& ~ 0x1ffd) | re_assemble_12 (value
);
582 return (insn
& ~ 0x3ff1) | re_assemble_14 (value
& -8);
585 return (insn
& ~ 0x3ff9) | re_assemble_14 (value
& -4);
588 return (insn
& ~ 0x3fff) | re_assemble_14 (value
);
592 return (insn
& ~ 0xfff1) | re_assemble_16 (value
& -8);
595 return (insn
& ~ 0xfff9) | re_assemble_16 (value
& -4);
598 return (insn
& ~ 0xffff) | re_assemble_16 (value
);
602 return (insn
& ~ 0x1f1ffd) | re_assemble_17 (value
);
605 return (insn
& ~ 0x1fffff) | re_assemble_21 (value
);
608 return (insn
& ~ 0x3ff1ffd) | re_assemble_22 (value
);
619 #endif /* _LIBHPPA_H */