libhppa.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* HP PA-RISC SOM object file format: definitions internal to BFD.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Contributed by the Center for Software Science at the
  4. University of Utah (pa-gdb-bugs@cs.utah.edu).
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #ifndef _LIBHPPA_H
  19. #define _LIBHPPA_H
  20. #define BYTES_IN_WORD 4
  21. #define PA_PAGESIZE 0x1000
  22. /* The PA instruction set variants. */
  23. enum pa_arch {pa10 = 10, pa11 = 11, pa20 = 20, pa20w = 25};
  24. /* HP PA-RISC relocation types */
  25. enum hppa_reloc_field_selector_type
  26. {
  27. R_HPPA_FSEL = 0x0,
  28. R_HPPA_LSSEL = 0x1,
  29. R_HPPA_RSSEL = 0x2,
  30. R_HPPA_LSEL = 0x3,
  31. R_HPPA_RSEL = 0x4,
  32. R_HPPA_LDSEL = 0x5,
  33. R_HPPA_RDSEL = 0x6,
  34. R_HPPA_LRSEL = 0x7,
  35. R_HPPA_RRSEL = 0x8,
  36. R_HPPA_NSEL = 0x9,
  37. R_HPPA_NLSEL = 0xa,
  38. R_HPPA_NLRSEL = 0xb,
  39. R_HPPA_PSEL = 0xc,
  40. R_HPPA_LPSEL = 0xd,
  41. R_HPPA_RPSEL = 0xe,
  42. R_HPPA_TSEL = 0xf,
  43. R_HPPA_LTSEL = 0x10,
  44. R_HPPA_RTSEL = 0x11,
  45. R_HPPA_LTPSEL = 0x12,
  46. R_HPPA_RTPSEL = 0x13
  47. };
  48. /* /usr/include/reloc.h defines these to constants. We want to use
  49. them in enums, so #undef them before we start using them. We might
  50. be able to fix this another way by simply managing not to include
  51. /usr/include/reloc.h, but currently GDB picks up these defines
  52. somewhere. */
  53. #undef e_fsel
  54. #undef e_lssel
  55. #undef e_rssel
  56. #undef e_lsel
  57. #undef e_rsel
  58. #undef e_ldsel
  59. #undef e_rdsel
  60. #undef e_lrsel
  61. #undef e_rrsel
  62. #undef e_nsel
  63. #undef e_nlsel
  64. #undef e_nlrsel
  65. #undef e_psel
  66. #undef e_lpsel
  67. #undef e_rpsel
  68. #undef e_tsel
  69. #undef e_ltsel
  70. #undef e_rtsel
  71. #undef e_one
  72. #undef e_two
  73. #undef e_pcrel
  74. #undef e_con
  75. #undef e_plabel
  76. #undef e_abs
  77. /* for compatibility */
  78. enum hppa_reloc_field_selector_type_alt
  79. {
  80. e_fsel = R_HPPA_FSEL,
  81. e_lssel = R_HPPA_LSSEL,
  82. e_rssel = R_HPPA_RSSEL,
  83. e_lsel = R_HPPA_LSEL,
  84. e_rsel = R_HPPA_RSEL,
  85. e_ldsel = R_HPPA_LDSEL,
  86. e_rdsel = R_HPPA_RDSEL,
  87. e_lrsel = R_HPPA_LRSEL,
  88. e_rrsel = R_HPPA_RRSEL,
  89. e_nsel = R_HPPA_NSEL,
  90. e_nlsel = R_HPPA_NLSEL,
  91. e_nlrsel = R_HPPA_NLRSEL,
  92. e_psel = R_HPPA_PSEL,
  93. e_lpsel = R_HPPA_LPSEL,
  94. e_rpsel = R_HPPA_RPSEL,
  95. e_tsel = R_HPPA_TSEL,
  96. e_ltsel = R_HPPA_LTSEL,
  97. e_rtsel = R_HPPA_RTSEL,
  98. e_ltpsel = R_HPPA_LTPSEL,
  99. e_rtpsel = R_HPPA_RTPSEL
  100. };
  101. enum hppa_reloc_expr_type
  102. {
  103. R_HPPA_E_ONE = 0,
  104. R_HPPA_E_TWO = 1,
  105. R_HPPA_E_PCREL = 2,
  106. R_HPPA_E_CON = 3,
  107. R_HPPA_E_PLABEL = 7,
  108. R_HPPA_E_ABS = 18
  109. };
  110. /* for compatibility */
  111. enum hppa_reloc_expr_type_alt
  112. {
  113. e_one = R_HPPA_E_ONE,
  114. e_two = R_HPPA_E_TWO,
  115. e_pcrel = R_HPPA_E_PCREL,
  116. e_con = R_HPPA_E_CON,
  117. e_plabel = R_HPPA_E_PLABEL,
  118. e_abs = R_HPPA_E_ABS
  119. };
  120. /* Relocations for function calls must be accompanied by parameter
  121. relocation bits. These bits describe exactly where the caller has
  122. placed the function's arguments and where it expects to find a return
  123. value.
  124. Both ELF and SOM encode this information within the addend field
  125. of the call relocation. (Note this could break very badly if one
  126. was to make a call like bl foo + 0x12345678).
  127. The high order 10 bits contain parameter relocation information,
  128. the low order 22 bits contain the constant offset. */
  129. #define HPPA_R_ARG_RELOC(a) \
  130. (((a) >> 22) & 0x3ff)
  131. #define HPPA_R_CONSTANT(a) \
  132. ((((bfd_signed_vma) (a) & 0x3fffff) ^ 0x200000) - 0x200000)
  133. #define HPPA_R_ADDEND(r, c) \
  134. (((r) << 22) + ((c) & 0x3fffff))
  135. /* Some functions to manipulate PA instructions. */
  136. /* The *sign_extend functions are used to assemble various bitfields
  137. taken from an instruction and return the resulting immediate
  138. value. */
  139. static inline unsigned ATTRIBUTE_UNUSED
  140. sign_extend (unsigned x, unsigned len)
  141. {
  142. unsigned signbit = (1 << (len - 1));
  143. unsigned mask = (signbit << 1) - 1;
  144. return ((x & mask) ^ signbit) - signbit;
  145. }
  146. static inline unsigned ATTRIBUTE_UNUSED
  147. low_sign_extend (unsigned x, unsigned len)
  148. {
  149. return (x >> 1) - ((x & 1) << (len - 1));
  150. }
  151. /* The re_assemble_* functions prepare an immediate value for
  152. insertion into an opcode. pa-risc uses all sorts of weird bitfields
  153. in the instruction to hold the value. */
  154. static inline unsigned ATTRIBUTE_UNUSED
  155. sign_unext (unsigned x, unsigned len)
  156. {
  157. unsigned len_ones;
  158. len_ones = (1 << len) - 1;
  159. return x & len_ones;
  160. }
  161. static inline unsigned ATTRIBUTE_UNUSED
  162. low_sign_unext (unsigned x, unsigned len)
  163. {
  164. unsigned temp;
  165. unsigned sign;
  166. sign = (x >> (len-1)) & 1;
  167. temp = sign_unext (x, len-1);
  168. return (temp << 1) | sign;
  169. }
  170. static inline unsigned ATTRIBUTE_UNUSED
  171. re_assemble_3 (unsigned as3)
  172. {
  173. return (( (as3 & 4) << (13-2))
  174. | ((as3 & 3) << (13+1)));
  175. }
  176. static inline unsigned ATTRIBUTE_UNUSED
  177. re_assemble_12 (unsigned as12)
  178. {
  179. return (( (as12 & 0x800) >> 11)
  180. | ((as12 & 0x400) >> (10 - 2))
  181. | ((as12 & 0x3ff) << (1 + 2)));
  182. }
  183. static inline unsigned ATTRIBUTE_UNUSED
  184. re_assemble_14 (unsigned as14)
  185. {
  186. return (( (as14 & 0x1fff) << 1)
  187. | ((as14 & 0x2000) >> 13));
  188. }
  189. static inline unsigned ATTRIBUTE_UNUSED
  190. re_assemble_16 (unsigned as16)
  191. {
  192. unsigned s, t;
  193. /* Unusual 16-bit encoding, for wide mode only. */
  194. t = (as16 << 1) & 0xffff;
  195. s = (as16 & 0x8000);
  196. return (t ^ s ^ (s >> 1)) | (s >> 15);
  197. }
  198. static inline unsigned ATTRIBUTE_UNUSED
  199. re_assemble_17 (unsigned as17)
  200. {
  201. return (( (as17 & 0x10000) >> 16)
  202. | ((as17 & 0x0f800) << (16 - 11))
  203. | ((as17 & 0x00400) >> (10 - 2))
  204. | ((as17 & 0x003ff) << (1 + 2)));
  205. }
  206. static inline unsigned ATTRIBUTE_UNUSED
  207. re_assemble_21 (unsigned as21)
  208. {
  209. return (( (as21 & 0x100000) >> 20)
  210. | ((as21 & 0x0ffe00) >> 8)
  211. | ((as21 & 0x000180) << 7)
  212. | ((as21 & 0x00007c) << 14)
  213. | ((as21 & 0x000003) << 12));
  214. }
  215. static inline unsigned ATTRIBUTE_UNUSED
  216. re_assemble_22 (unsigned as22)
  217. {
  218. return (( (as22 & 0x200000) >> 21)
  219. | ((as22 & 0x1f0000) << (21 - 16))
  220. | ((as22 & 0x00f800) << (16 - 11))
  221. | ((as22 & 0x000400) >> (10 - 2))
  222. | ((as22 & 0x0003ff) << (1 + 2)));
  223. }
  224. /* Handle field selectors for PA instructions.
  225. The L and R (and LS, RS etc.) selectors are used in pairs to form a
  226. full 32 bit address. eg.
  227. LDIL L'start,%r1 ; put left part into r1
  228. LDW R'start(%r1),%r2 ; add r1 and right part to form address
  229. This function returns sign extended values in all cases.
  230. */
  231. static inline bfd_signed_vma ATTRIBUTE_UNUSED
  232. hppa_field_adjust (bfd_vma sym_val,
  233. bfd_signed_vma addend,
  234. enum hppa_reloc_field_selector_type_alt r_field)
  235. {
  236. bfd_signed_vma value;
  237. value = sym_val + addend;
  238. switch (r_field)
  239. {
  240. case e_fsel:
  241. /* F: No change. */
  242. break;
  243. case e_nsel:
  244. /* N: null selector. I don't really understand what this is all
  245. about, but HP's documentation says "this indicates that zero
  246. bits are to be used for the displacement on the instruction.
  247. This fixup is used to identify three-instruction sequences to
  248. access data (for importing shared library data)." */
  249. value = 0;
  250. break;
  251. case e_lsel:
  252. case e_nlsel:
  253. /* L: Select top 21 bits. */
  254. value = value >> 11;
  255. break;
  256. case e_rsel:
  257. /* R: Select bottom 11 bits. */
  258. value = value & 0x7ff;
  259. break;
  260. case e_lssel:
  261. /* LS: Round to nearest multiple of 2048 then select top 21 bits. */
  262. value = value + 0x400;
  263. value = value >> 11;
  264. break;
  265. case e_rssel:
  266. /* RS: Select bottom 11 bits for LS.
  267. We need to return a value such that 2048 * LS'x + RS'x == x.
  268. ie. RS'x = x - ((x + 0x400) & -0x800)
  269. this is just a sign extension from bit 21. */
  270. value = ((value & 0x7ff) ^ 0x400) - 0x400;
  271. break;
  272. case e_ldsel:
  273. /* LD: Round to next multiple of 2048 then select top 21 bits.
  274. Yes, if we are already on a multiple of 2048, we go up to the
  275. next one. RD in this case will be -2048. */
  276. value = value + 0x800;
  277. value = value >> 11;
  278. break;
  279. case e_rdsel:
  280. /* RD: Set bits 0-20 to one. */
  281. value = value | -0x800;
  282. break;
  283. case e_lrsel:
  284. case e_nlrsel:
  285. /* LR: L with rounding of the addend to nearest 8k. */
  286. value = sym_val + ((addend + 0x1000) & -0x2000);
  287. value = value >> 11;
  288. break;
  289. case e_rrsel:
  290. /* RR: R with rounding of the addend to nearest 8k.
  291. We need to return a value such that 2048 * LR'x + RR'x == x
  292. ie. RR'x = s+a - (s + (((a + 0x1000) & -0x2000) & -0x800))
  293. . = s+a - ((s & -0x800) + ((a + 0x1000) & -0x2000))
  294. . = (s & 0x7ff) + a - ((a + 0x1000) & -0x2000) */
  295. value = (sym_val & 0x7ff) + (((addend & 0x1fff) ^ 0x1000) - 0x1000);
  296. break;
  297. default:
  298. abort ();
  299. }
  300. return value;
  301. }
  302. /* PA-RISC OPCODES */
  303. #define get_opcode(insn) (((insn) >> 26) & 0x3f)
  304. enum hppa_opcode_type
  305. {
  306. /* None of the opcodes in the first group generate relocs, so we
  307. aren't too concerned about them. */
  308. OP_SYSOP = 0x00,
  309. OP_MEMMNG = 0x01,
  310. OP_ALU = 0x02,
  311. OP_NDXMEM = 0x03,
  312. OP_SPOP = 0x04,
  313. OP_DIAG = 0x05,
  314. OP_FMPYADD = 0x06,
  315. OP_UNDEF07 = 0x07,
  316. OP_COPRW = 0x09,
  317. OP_COPRDW = 0x0b,
  318. OP_COPR = 0x0c,
  319. OP_FLOAT = 0x0e,
  320. OP_PRDSPEC = 0x0f,
  321. OP_UNDEF15 = 0x15,
  322. OP_UNDEF1d = 0x1d,
  323. OP_FMPYSUB = 0x26,
  324. OP_FPFUSED = 0x2e,
  325. OP_SHEXDP0 = 0x34,
  326. OP_SHEXDP1 = 0x35,
  327. OP_SHEXDP2 = 0x36,
  328. OP_UNDEF37 = 0x37,
  329. OP_SHEXDP3 = 0x3c,
  330. OP_SHEXDP4 = 0x3d,
  331. OP_MULTMED = 0x3e,
  332. OP_UNDEF3f = 0x3f,
  333. OP_LDIL = 0x08,
  334. OP_ADDIL = 0x0a,
  335. OP_LDO = 0x0d,
  336. OP_LDB = 0x10,
  337. OP_LDH = 0x11,
  338. OP_LDW = 0x12,
  339. OP_LDWM = 0x13,
  340. OP_STB = 0x18,
  341. OP_STH = 0x19,
  342. OP_STW = 0x1a,
  343. OP_STWM = 0x1b,
  344. OP_LDD = 0x14,
  345. OP_STD = 0x1c,
  346. OP_FLDW = 0x16,
  347. OP_LDWL = 0x17,
  348. OP_FSTW = 0x1e,
  349. OP_STWL = 0x1f,
  350. OP_COMBT = 0x20,
  351. OP_COMIBT = 0x21,
  352. OP_COMBF = 0x22,
  353. OP_COMIBF = 0x23,
  354. OP_CMPBDT = 0x27,
  355. OP_ADDBT = 0x28,
  356. OP_ADDIBT = 0x29,
  357. OP_ADDBF = 0x2a,
  358. OP_ADDIBF = 0x2b,
  359. OP_CMPBDF = 0x2f,
  360. OP_BVB = 0x30,
  361. OP_BB = 0x31,
  362. OP_MOVB = 0x32,
  363. OP_MOVIB = 0x33,
  364. OP_CMPIBD = 0x3b,
  365. OP_COMICLR = 0x24,
  366. OP_SUBI = 0x25,
  367. OP_ADDIT = 0x2c,
  368. OP_ADDI = 0x2d,
  369. OP_BE = 0x38,
  370. OP_BLE = 0x39,
  371. OP_BL = 0x3a
  372. };
  373. /* Given a machine instruction, return its format. */
  374. static inline unsigned ATTRIBUTE_UNUSED
  375. bfd_hppa_insn2fmt (bfd *abfd, unsigned insn)
  376. {
  377. enum hppa_opcode_type op = (enum hppa_opcode_type) get_opcode (insn);
  378. switch (op)
  379. {
  380. case OP_COMICLR:
  381. case OP_SUBI:
  382. case OP_ADDIT:
  383. case OP_ADDI:
  384. return 11;
  385. case OP_COMBT:
  386. case OP_COMIBT:
  387. case OP_COMBF:
  388. case OP_COMIBF:
  389. case OP_CMPBDT:
  390. case OP_ADDBT:
  391. case OP_ADDIBT:
  392. case OP_ADDBF:
  393. case OP_ADDIBF:
  394. case OP_CMPBDF:
  395. case OP_BVB:
  396. case OP_BB:
  397. case OP_MOVB:
  398. case OP_MOVIB:
  399. case OP_CMPIBD:
  400. return 12;
  401. case OP_LDO:
  402. case OP_LDB:
  403. case OP_LDH:
  404. case OP_LDW:
  405. case OP_LDWM:
  406. case OP_STB:
  407. case OP_STH:
  408. case OP_STW:
  409. case OP_STWM:
  410. if (abfd->arch_info->mach >= 25)
  411. return 16; /* Wide mode, format 16. */
  412. return 14;
  413. case OP_FLDW:
  414. case OP_LDWL:
  415. case OP_FSTW:
  416. case OP_STWL:
  417. /* This is a hack. Unfortunately, format 11 is already taken
  418. and we're using integers rather than an enum, so it's hard
  419. to describe the 11a format. */
  420. if (abfd->arch_info->mach >= 25)
  421. return -16; /* Wide mode, format 16a. */
  422. return -11;
  423. case OP_LDD:
  424. case OP_STD:
  425. if (abfd->arch_info->mach >= 25)
  426. return -10; /* Wide mode, format 10a. */
  427. return 10;
  428. case OP_BL:
  429. if ((insn & 0x8000) != 0)
  430. return 22;
  431. /* fall thru */
  432. case OP_BE:
  433. case OP_BLE:
  434. return 17;
  435. case OP_LDIL:
  436. case OP_ADDIL:
  437. return 21;
  438. default:
  439. break;
  440. }
  441. return 32;
  442. }
  443. /* Insert VALUE into INSN using R_FORMAT to determine exactly what
  444. bits to change. */
  445. static inline unsigned ATTRIBUTE_UNUSED
  446. hppa_rebuild_insn (unsigned insn, unsigned value, int r_format)
  447. {
  448. switch (r_format)
  449. {
  450. case 11:
  451. return (insn & ~ 0x7ff) | low_sign_unext (value, 11);
  452. case 12:
  453. return (insn & ~ 0x1ffd) | re_assemble_12 (value);
  454. case 10:
  455. return (insn & ~ 0x3ff1) | re_assemble_14 (value & -8);
  456. case -11:
  457. return (insn & ~ 0x3ff9) | re_assemble_14 (value & -4);
  458. case 14:
  459. return (insn & ~ 0x3fff) | re_assemble_14 (value);
  460. case -10:
  461. return (insn & ~ 0xfff1) | re_assemble_16 (value & -8);
  462. case -16:
  463. return (insn & ~ 0xfff9) | re_assemble_16 (value & -4);
  464. case 16:
  465. return (insn & ~ 0xffff) | re_assemble_16 (value);
  466. case 17:
  467. return (insn & ~ 0x1f1ffd) | re_assemble_17 (value);
  468. case 21:
  469. return (insn & ~ 0x1fffff) | re_assemble_21 (value);
  470. case 22:
  471. return (insn & ~ 0x3ff1ffd) | re_assemble_22 (value);
  472. case 32:
  473. return value;
  474. default:
  475. abort ();
  476. }
  477. return insn;
  478. }
  479. #endif /* _LIBHPPA_H */