ia64.c 2.7 KB

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