ia64.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file ia64.c
  5. /// \brief Filter for IA64 (Itanium) binaries
  6. ///
  7. // Authors: Igor Pavlov
  8. // Lasse Collin
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "simple_private.h"
  12. static size_t
  13. ia64_code(void *simple lzma_attribute((__unused__)),
  14. uint32_t now_pos, bool is_encoder,
  15. uint8_t *buffer, size_t size)
  16. {
  17. static const uint32_t BRANCH_TABLE[32] = {
  18. 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0, 0, 0, 0, 0, 0, 0, 0,
  20. 4, 4, 6, 6, 0, 0, 7, 7,
  21. 4, 4, 0, 0, 4, 4, 0, 0
  22. };
  23. size_t i;
  24. for (i = 0; i + 16 <= size; i += 16) {
  25. const uint32_t instr_template = buffer[i] & 0x1F;
  26. const uint32_t mask = BRANCH_TABLE[instr_template];
  27. uint32_t bit_pos = 5;
  28. for (size_t slot = 0; slot < 3; ++slot, bit_pos += 41) {
  29. if (((mask >> slot) & 1) == 0)
  30. continue;
  31. const size_t byte_pos = (bit_pos >> 3);
  32. const uint32_t bit_res = bit_pos & 0x7;
  33. uint64_t instruction = 0;
  34. for (size_t j = 0; j < 6; ++j)
  35. instruction += (uint64_t)(
  36. buffer[i + j + byte_pos])
  37. << (8 * j);
  38. uint64_t inst_norm = instruction >> bit_res;
  39. if (((inst_norm >> 37) & 0xF) == 0x5
  40. && ((inst_norm >> 9) & 0x7) == 0
  41. /* && (inst_norm & 0x3F)== 0 */
  42. ) {
  43. uint32_t src = (uint32_t)(
  44. (inst_norm >> 13) & 0xFFFFF);
  45. src |= ((inst_norm >> 36) & 1) << 20;
  46. src <<= 4;
  47. uint32_t dest;
  48. if (is_encoder)
  49. dest = now_pos + (uint32_t)(i) + src;
  50. else
  51. dest = src - (now_pos + (uint32_t)(i));
  52. dest >>= 4;
  53. inst_norm &= ~((uint64_t)(0x8FFFFF) << 13);
  54. inst_norm |= (uint64_t)(dest & 0xFFFFF) << 13;
  55. inst_norm |= (uint64_t)(dest & 0x100000)
  56. << (36 - 20);
  57. instruction &= (1U << bit_res) - 1;
  58. instruction |= (inst_norm << bit_res);
  59. for (size_t j = 0; j < 6; j++)
  60. buffer[i + j + byte_pos] = (uint8_t)(
  61. instruction
  62. >> (8 * j));
  63. }
  64. }
  65. }
  66. return i;
  67. }
  68. static lzma_ret
  69. ia64_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  70. const lzma_filter_info *filters, bool is_encoder)
  71. {
  72. return lzma_simple_coder_init(next, allocator, filters,
  73. &ia64_code, 0, 16, 16, is_encoder);
  74. }
  75. #ifdef HAVE_ENCODER_IA64
  76. extern lzma_ret
  77. lzma_simple_ia64_encoder_init(lzma_next_coder *next,
  78. const lzma_allocator *allocator,
  79. const lzma_filter_info *filters)
  80. {
  81. return ia64_coder_init(next, allocator, filters, true);
  82. }
  83. #endif
  84. #ifdef HAVE_DECODER_IA64
  85. extern lzma_ret
  86. lzma_simple_ia64_decoder_init(lzma_next_coder *next,
  87. const lzma_allocator *allocator,
  88. const lzma_filter_info *filters)
  89. {
  90. return ia64_coder_init(next, allocator, filters, false);
  91. }
  92. #endif