1 /* HP PA-RISC SOM object file format: definitions internal to BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
3 2003, 2005, 2007 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
28 #define BYTES_IN_WORD 4
29 #define PA_PAGESIZE 0x1000
31 /* The PA instruction set variants. */
32 enum pa_arch {pa10 = 10, pa11 = 11, pa20 = 20, pa20w = 25};
34 /* HP PA-RISC relocation types */
36 enum hppa_reloc_field_selector_type
60 /* /usr/include/reloc.h defines these to constants. We want to use
61 them in enums, so #undef them before we start using them. We might
62 be able to fix this another way by simply managing not to include
63 /usr/include/reloc.h, but currently GDB picks up these defines
90 /* for compatibility */
91 enum hppa_reloc_field_selector_type_alt
94 e_lssel = R_HPPA_LSSEL,
95 e_rssel = R_HPPA_RSSEL,
98 e_ldsel = R_HPPA_LDSEL,
99 e_rdsel = R_HPPA_RDSEL,
100 e_lrsel = R_HPPA_LRSEL,
101 e_rrsel = R_HPPA_RRSEL,
102 e_nsel = R_HPPA_NSEL,
103 e_nlsel = R_HPPA_NLSEL,
104 e_nlrsel = R_HPPA_NLRSEL,
105 e_psel = R_HPPA_PSEL,
106 e_lpsel = R_HPPA_LPSEL,
107 e_rpsel = R_HPPA_RPSEL,
108 e_tsel = R_HPPA_TSEL,
109 e_ltsel = R_HPPA_LTSEL,
110 e_rtsel = R_HPPA_RTSEL,
111 e_ltpsel = R_HPPA_LTPSEL,
112 e_rtpsel = R_HPPA_RTPSEL
115 enum hppa_reloc_expr_type
125 /* for compatibility */
126 enum hppa_reloc_expr_type_alt
128 e_one = R_HPPA_E_ONE,
129 e_two = R_HPPA_E_TWO,
130 e_pcrel = R_HPPA_E_PCREL,
131 e_con = R_HPPA_E_CON,
132 e_plabel = R_HPPA_E_PLABEL,
137 /* Relocations for function calls must be accompanied by parameter
138 relocation bits. These bits describe exactly where the caller has
139 placed the function's arguments and where it expects to find a return
142 Both ELF and SOM encode this information within the addend field
143 of the call relocation. (Note this could break very badly if one
144 was to make a call like bl foo + 0x12345678).
146 The high order 10 bits contain parameter relocation information,
147 the low order 22 bits contain the constant offset. */
149 #define HPPA_R_ARG_RELOC(a) \
150 (((a) >> 22) & 0x3ff)
151 #define HPPA_R_CONSTANT(a) \
152 ((((bfd_signed_vma)(a)) << (BFD_ARCH_SIZE-22)) >> (BFD_ARCH_SIZE-22))
153 #define HPPA_R_ADDEND(r, c) \
154 (((r) << 22) + ((c) & 0x3fffff))
157 /* Some functions to manipulate PA instructions. */
159 /* Declare the functions with the unused attribute to avoid warnings. */
160 static inline int sign_extend (int, int) ATTRIBUTE_UNUSED;
161 static inline int low_sign_extend (int, int) ATTRIBUTE_UNUSED;
162 static inline int sign_unext (int, int) ATTRIBUTE_UNUSED;
163 static inline int low_sign_unext (int, int) ATTRIBUTE_UNUSED;
164 static inline int re_assemble_3 (int) ATTRIBUTE_UNUSED;
165 static inline int re_assemble_12 (int) ATTRIBUTE_UNUSED;
166 static inline int re_assemble_14 (int) ATTRIBUTE_UNUSED;
167 static inline int re_assemble_16 (int) ATTRIBUTE_UNUSED;
168 static inline int re_assemble_17 (int) ATTRIBUTE_UNUSED;
169 static inline int re_assemble_21 (int) ATTRIBUTE_UNUSED;
170 static inline int re_assemble_22 (int) ATTRIBUTE_UNUSED;
171 static inline bfd_signed_vma hppa_field_adjust
172 (bfd_vma, bfd_signed_vma, enum hppa_reloc_field_selector_type_alt)
174 static inline int bfd_hppa_insn2fmt (bfd *, int) ATTRIBUTE_UNUSED;
175 static inline int hppa_rebuild_insn (int, int, int) ATTRIBUTE_UNUSED;
178 /* The *sign_extend functions are used to assemble various bitfields
179 taken from an instruction and return the resulting immediate
183 sign_extend (int x, int len)
185 int signbit = (1 << (len - 1));
186 int mask = (signbit << 1) - 1;
187 return ((x & mask) ^ signbit) - signbit;
191 low_sign_extend (int x, int len)
193 return (x >> 1) - ((x & 1) << (len - 1));
197 /* The re_assemble_* functions prepare an immediate value for
198 insertion into an opcode. pa-risc uses all sorts of weird bitfields
199 in the instruction to hold the value. */
202 sign_unext (int x, int len)
206 len_ones = (1 << len) - 1;
212 low_sign_unext (int x, int len)
217 sign = (x >> (len-1)) & 1;
219 temp = sign_unext (x, len-1);
221 return (temp << 1) | sign;
225 re_assemble_3 (int as3)
227 return (( (as3 & 4) << (13-2))
228 | ((as3 & 3) << (13+1)));
232 re_assemble_12 (int as12)
234 return (( (as12 & 0x800) >> 11)
235 | ((as12 & 0x400) >> (10 - 2))
236 | ((as12 & 0x3ff) << (1 + 2)));
240 re_assemble_14 (int as14)
242 return (( (as14 & 0x1fff) << 1)
243 | ((as14 & 0x2000) >> 13));
247 re_assemble_16 (int as16)
251 /* Unusual 16-bit encoding, for wide mode only. */
252 t = (as16 << 1) & 0xffff;
254 return (t ^ s ^ (s >> 1)) | (s >> 15);
258 re_assemble_17 (int as17)
260 return (( (as17 & 0x10000) >> 16)
261 | ((as17 & 0x0f800) << (16 - 11))
262 | ((as17 & 0x00400) >> (10 - 2))
263 | ((as17 & 0x003ff) << (1 + 2)));
267 re_assemble_21 (int as21)
269 return (( (as21 & 0x100000) >> 20)
270 | ((as21 & 0x0ffe00) >> 8)
271 | ((as21 & 0x000180) << 7)
272 | ((as21 & 0x00007c) << 14)
273 | ((as21 & 0x000003) << 12));
277 re_assemble_22 (int as22)
279 return (( (as22 & 0x200000) >> 21)
280 | ((as22 & 0x1f0000) << (21 - 16))
281 | ((as22 & 0x00f800) << (16 - 11))
282 | ((as22 & 0x000400) >> (10 - 2))
283 | ((as22 & 0x0003ff) << (1 + 2)));
287 /* Handle field selectors for PA instructions.
288 The L and R (and LS, RS etc.) selectors are used in pairs to form a
289 full 32 bit address. eg.
291 LDIL L'start,%r1 ; put left part into r1
292 LDW R'start(%r1),%r2 ; add r1 and right part to form address
294 This function returns sign extended values in all cases.
297 static inline bfd_signed_vma
298 hppa_field_adjust (bfd_vma sym_val,
299 bfd_signed_vma addend,
300 enum hppa_reloc_field_selector_type_alt r_field)
302 bfd_signed_vma value;
304 value = sym_val + addend;
312 /* N: null selector. I don't really understand what this is all
313 about, but HP's documentation says "this indicates that zero
314 bits are to be used for the displacement on the instruction.
315 This fixup is used to identify three-instruction sequences to
316 access data (for importing shared library data)." */
322 /* L: Select top 21 bits. */
327 /* R: Select bottom 11 bits. */
328 value = value & 0x7ff;
332 /* LS: Round to nearest multiple of 2048 then select top 21 bits. */
333 value = value + 0x400;
338 /* RS: Select bottom 11 bits for LS.
339 We need to return a value such that 2048 * LS'x + RS'x == x.
340 ie. RS'x = x - ((x + 0x400) & -0x800)
341 this is just a sign extension from bit 21. */
342 value = ((value & 0x7ff) ^ 0x400) - 0x400;
346 /* LD: Round to next multiple of 2048 then select top 21 bits.
347 Yes, if we are already on a multiple of 2048, we go up to the
348 next one. RD in this case will be -2048. */
349 value = value + 0x800;
354 /* RD: Set bits 0-20 to one. */
355 value = value | -0x800;
360 /* LR: L with rounding of the addend to nearest 8k. */
361 value = sym_val + ((addend + 0x1000) & -0x2000);
366 /* RR: R with rounding of the addend to nearest 8k.
367 We need to return a value such that 2048 * LR'x + RR'x == x
368 ie. RR'x = s+a - (s + (((a + 0x1000) & -0x2000) & -0x800))
369 . = s+a - ((s & -0x800) + ((a + 0x1000) & -0x2000))
370 . = (s & 0x7ff) + a - ((a + 0x1000) & -0x2000) */
371 value = (sym_val & 0x7ff) + (((addend & 0x1fff) ^ 0x1000) - 0x1000);
380 /* PA-RISC OPCODES */
381 #define get_opcode(insn) (((insn) >> 26) & 0x3f)
383 enum hppa_opcode_type
385 /* None of the opcodes in the first group generate relocs, so we
386 aren't too concerned about them. */
461 /* Given a machine instruction, return its format. */
464 bfd_hppa_insn2fmt (bfd *abfd, int insn)
466 enum hppa_opcode_type op = get_opcode (insn);
502 if (abfd->arch_info->mach >= 25)
503 return 16; /* Wide mode, format 16. */
510 /* This is a hack. Unfortunately, format 11 is already taken
511 and we're using integers rather than an enum, so it's hard
512 to describe the 11a format. */
513 if (abfd->arch_info->mach >= 25)
514 return -16; /* Wide mode, format 16a. */
519 if (abfd->arch_info->mach >= 25)
520 return -10; /* Wide mode, format 10a. */
524 if ((insn & 0x8000) != 0)
542 /* Insert VALUE into INSN using R_FORMAT to determine exactly what
546 hppa_rebuild_insn (int insn, int value, int r_format)
551 return (insn & ~ 0x7ff) | low_sign_unext (value, 11);
554 return (insn & ~ 0x1ffd) | re_assemble_12 (value);
558 return (insn & ~ 0x3ff1) | re_assemble_14 (value & -8);
561 return (insn & ~ 0x3ff9) | re_assemble_14 (value & -4);
564 return (insn & ~ 0x3fff) | re_assemble_14 (value);
568 return (insn & ~ 0xfff1) | re_assemble_16 (value & -8);
571 return (insn & ~ 0xfff9) | re_assemble_16 (value & -4);
574 return (insn & ~ 0xffff) | re_assemble_16 (value);
578 return (insn & ~ 0x1f1ffd) | re_assemble_17 (value);
581 return (insn & ~ 0x1fffff) | re_assemble_21 (value);
584 return (insn & ~ 0x3ff1ffd) | re_assemble_22 (value);
595 #endif /* _LIBHPPA_H */