1 /* HP PA-RISC SOM object file format: definitions internal to BFD.
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
4 Contributed by the Center for Software Science at the
5 University of Utah (pa-gdb-bugs@cs.utah.edu).
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
27 #define BYTES_IN_WORD 4
28 #define PA_PAGESIZE 0x1000
30 /* The PA instruction set variants. */
31 enum pa_arch
{pa10
= 10, pa11
= 11, pa20
= 20, pa20w
= 25};
33 /* HP PA-RISC relocation types */
35 enum hppa_reloc_field_selector_type
59 /* /usr/include/reloc.h defines these to constants. We want to use
60 them in enums, so #undef them before we start using them. We might
61 be able to fix this another way by simply managing not to include
62 /usr/include/reloc.h, but currently GDB picks up these defines
89 /* for compatibility */
90 enum hppa_reloc_field_selector_type_alt
93 e_lssel
= R_HPPA_LSSEL
,
94 e_rssel
= R_HPPA_RSSEL
,
97 e_ldsel
= R_HPPA_LDSEL
,
98 e_rdsel
= R_HPPA_RDSEL
,
99 e_lrsel
= R_HPPA_LRSEL
,
100 e_rrsel
= R_HPPA_RRSEL
,
101 e_nsel
= R_HPPA_NSEL
,
102 e_nlsel
= R_HPPA_NLSEL
,
103 e_nlrsel
= R_HPPA_NLRSEL
,
104 e_psel
= R_HPPA_PSEL
,
105 e_lpsel
= R_HPPA_LPSEL
,
106 e_rpsel
= R_HPPA_RPSEL
,
107 e_tsel
= R_HPPA_TSEL
,
108 e_ltsel
= R_HPPA_LTSEL
,
109 e_rtsel
= R_HPPA_RTSEL
,
110 e_ltpsel
= R_HPPA_LTPSEL
,
111 e_rtpsel
= R_HPPA_RTPSEL
114 enum hppa_reloc_expr_type
124 /* for compatibility */
125 enum hppa_reloc_expr_type_alt
127 e_one
= R_HPPA_E_ONE
,
128 e_two
= R_HPPA_E_TWO
,
129 e_pcrel
= R_HPPA_E_PCREL
,
130 e_con
= R_HPPA_E_CON
,
131 e_plabel
= R_HPPA_E_PLABEL
,
136 /* Relocations for function calls must be accompanied by parameter
137 relocation bits. These bits describe exactly where the caller has
138 placed the function's arguments and where it expects to find a return
141 Both ELF and SOM encode this information within the addend field
142 of the call relocation. (Note this could break very badly if one
143 was to make a call like bl foo + 0x12345678).
145 The high order 10 bits contain parameter relocation information,
146 the low order 22 bits contain the constant offset. */
148 #define HPPA_R_ARG_RELOC(a) \
149 (((a) >> 22) & 0x3ff)
150 #define HPPA_R_CONSTANT(a) \
151 ((((bfd_signed_vma) (a) & 0x3fffff) ^ 0x200000) - 0x200000)
152 #define HPPA_R_ADDEND(r, c) \
153 (((r) << 22) + ((c) & 0x3fffff))
156 /* Some functions to manipulate PA instructions. */
158 /* The *sign_extend functions are used to assemble various bitfields
159 taken from an instruction and return the resulting immediate
162 static inline unsigned ATTRIBUTE_UNUSED
163 sign_extend (unsigned x
, unsigned len
)
165 unsigned signbit
= (1u << (len
- 1));
166 unsigned mask
= (signbit
<< 1) - 1;
167 return ((x
& mask
) ^ signbit
) - signbit
;
170 static inline unsigned ATTRIBUTE_UNUSED
171 low_sign_extend (unsigned x
, unsigned len
)
173 return (x
>> 1) - ((x
& 1) << (len
- 1));
177 /* The re_assemble_* functions prepare an immediate value for
178 insertion into an opcode. pa-risc uses all sorts of weird bitfields
179 in the instruction to hold the value. */
181 static inline unsigned ATTRIBUTE_UNUSED
182 sign_unext (unsigned x
, unsigned len
)
186 len_ones
= (1 << len
) - 1;
191 static inline unsigned ATTRIBUTE_UNUSED
192 low_sign_unext (unsigned x
, unsigned len
)
197 sign
= (x
>> (len
-1)) & 1;
199 temp
= sign_unext (x
, len
-1);
201 return (temp
<< 1) | sign
;
204 static inline unsigned ATTRIBUTE_UNUSED
205 re_assemble_3 (unsigned as3
)
207 return (( (as3
& 4) << (13-2))
208 | ((as3
& 3) << (13+1)));
211 static inline unsigned ATTRIBUTE_UNUSED
212 re_assemble_12 (unsigned as12
)
214 return (( (as12
& 0x800) >> 11)
215 | ((as12
& 0x400) >> (10 - 2))
216 | ((as12
& 0x3ff) << (1 + 2)));
219 static inline unsigned ATTRIBUTE_UNUSED
220 re_assemble_14 (unsigned as14
)
222 return (( (as14
& 0x1fff) << 1)
223 | ((as14
& 0x2000) >> 13));
226 static inline unsigned ATTRIBUTE_UNUSED
227 re_assemble_16 (unsigned as16
)
231 /* Unusual 16-bit encoding, for wide mode only. */
232 t
= (as16
<< 1) & 0xffff;
234 return (t
^ s
^ (s
>> 1)) | (s
>> 15);
237 static inline unsigned ATTRIBUTE_UNUSED
238 re_assemble_17 (unsigned as17
)
240 return (( (as17
& 0x10000) >> 16)
241 | ((as17
& 0x0f800) << (16 - 11))
242 | ((as17
& 0x00400) >> (10 - 2))
243 | ((as17
& 0x003ff) << (1 + 2)));
246 static inline unsigned ATTRIBUTE_UNUSED
247 re_assemble_21 (unsigned as21
)
249 return (( (as21
& 0x100000) >> 20)
250 | ((as21
& 0x0ffe00) >> 8)
251 | ((as21
& 0x000180) << 7)
252 | ((as21
& 0x00007c) << 14)
253 | ((as21
& 0x000003) << 12));
256 static inline unsigned ATTRIBUTE_UNUSED
257 re_assemble_22 (unsigned as22
)
259 return (( (as22
& 0x200000) >> 21)
260 | ((as22
& 0x1f0000) << (21 - 16))
261 | ((as22
& 0x00f800) << (16 - 11))
262 | ((as22
& 0x000400) >> (10 - 2))
263 | ((as22
& 0x0003ff) << (1 + 2)));
267 /* Handle field selectors for PA instructions.
268 The L and R (and LS, RS etc.) selectors are used in pairs to form a
269 full 32 bit address. eg.
271 LDIL L'start,%r1 ; put left part into r1
272 LDW R'start(%r1),%r2 ; add r1 and right part to form address
274 This function returns sign extended values in all cases.
277 static inline bfd_signed_vma ATTRIBUTE_UNUSED
278 hppa_field_adjust (bfd_vma sym_val
,
279 bfd_signed_vma addend
,
280 enum hppa_reloc_field_selector_type_alt r_field
)
282 bfd_signed_vma value
;
284 value
= sym_val
+ addend
;
292 /* N: null selector. I don't really understand what this is all
293 about, but HP's documentation says "this indicates that zero
294 bits are to be used for the displacement on the instruction.
295 This fixup is used to identify three-instruction sequences to
296 access data (for importing shared library data)." */
302 /* L: Select top 21 bits. */
307 /* R: Select bottom 11 bits. */
308 value
= value
& 0x7ff;
312 /* LS: Round to nearest multiple of 2048 then select top 21 bits. */
313 value
= value
+ 0x400;
318 /* RS: Select bottom 11 bits for LS.
319 We need to return a value such that 2048 * LS'x + RS'x == x.
320 ie. RS'x = x - ((x + 0x400) & -0x800)
321 this is just a sign extension from bit 21. */
322 value
= ((value
& 0x7ff) ^ 0x400) - 0x400;
326 /* LD: Round to next multiple of 2048 then select top 21 bits.
327 Yes, if we are already on a multiple of 2048, we go up to the
328 next one. RD in this case will be -2048. */
329 value
= value
+ 0x800;
334 /* RD: Set bits 0-20 to one. */
335 value
= value
| -0x800;
340 /* LR: L with rounding of the addend to nearest 8k. */
341 value
= sym_val
+ ((addend
+ 0x1000) & -0x2000);
346 /* RR: R with rounding of the addend to nearest 8k.
347 We need to return a value such that 2048 * LR'x + RR'x == x
348 ie. RR'x = s+a - (s + (((a + 0x1000) & -0x2000) & -0x800))
349 . = s+a - ((s & -0x800) + ((a + 0x1000) & -0x2000))
350 . = (s & 0x7ff) + a - ((a + 0x1000) & -0x2000) */
351 value
= (sym_val
& 0x7ff) + (((addend
& 0x1fff) ^ 0x1000) - 0x1000);
360 /* PA-RISC OPCODES */
361 #define get_opcode(insn) (((insn) >> 26) & 0x3f)
363 enum hppa_opcode_type
365 /* None of the opcodes in the first group generate relocs, so we
366 aren't too concerned about them. */
441 /* Given a machine instruction, return its format. */
443 static inline unsigned ATTRIBUTE_UNUSED
444 bfd_hppa_insn2fmt (bfd
*abfd
, unsigned insn
)
446 enum hppa_opcode_type op
= (enum hppa_opcode_type
) get_opcode (insn
);
482 if (abfd
->arch_info
->mach
>= 25)
483 return 16; /* Wide mode, format 16. */
490 /* This is a hack. Unfortunately, format 11 is already taken
491 and we're using integers rather than an enum, so it's hard
492 to describe the 11a format. */
493 if (abfd
->arch_info
->mach
>= 25)
494 return -16; /* Wide mode, format 16a. */
499 if (abfd
->arch_info
->mach
>= 25)
500 return -10; /* Wide mode, format 10a. */
504 if ((insn
& 0x8000) != 0)
522 /* Insert VALUE into INSN using R_FORMAT to determine exactly what
525 static inline unsigned ATTRIBUTE_UNUSED
526 hppa_rebuild_insn (unsigned insn
, unsigned value
, int r_format
)
531 return (insn
& ~ 0x7ff) | low_sign_unext (value
, 11);
534 return (insn
& ~ 0x1ffd) | re_assemble_12 (value
);
538 return (insn
& ~ 0x3ff1) | re_assemble_14 (value
& -8);
541 return (insn
& ~ 0x3ff9) | re_assemble_14 (value
& -4);
544 return (insn
& ~ 0x3fff) | re_assemble_14 (value
);
548 return (insn
& ~ 0xfff1) | re_assemble_16 (value
& -8);
551 return (insn
& ~ 0xfff9) | re_assemble_16 (value
& -4);
554 return (insn
& ~ 0xffff) | re_assemble_16 (value
);
558 return (insn
& ~ 0x1f1ffd) | re_assemble_17 (value
);
561 return (insn
& ~ 0x1fffff) | re_assemble_21 (value
);
564 return (insn
& ~ 0x3ff1ffd) | re_assemble_22 (value
);
575 #endif /* _LIBHPPA_H */