arm64.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file arm64.c
  5. /// \brief Filter for ARM64 binaries
  6. ///
  7. /// This converts ARM64 relative addresses in the BL and ADRP immediates
  8. /// to absolute values to increase redundancy of ARM64 code.
  9. ///
  10. /// Converting B or ADR instructions was also tested but it's not useful.
  11. /// A majority of the jumps for the B instruction are very small (+/- 0xFF).
  12. /// These are typical for loops and if-statements. Encoding them to their
  13. /// absolute address reduces redundancy since many of the small relative
  14. /// jump values are repeated, but very few of the absolute addresses are.
  15. //
  16. // Authors: Lasse Collin
  17. // Jia Tan
  18. // Igor Pavlov
  19. //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. #include "simple_private.h"
  22. static size_t
  23. arm64_code(void *simple lzma_attribute((__unused__)),
  24. uint32_t now_pos, bool is_encoder,
  25. uint8_t *buffer, size_t size)
  26. {
  27. size_t i;
  28. // Clang 14.0.6 on x86-64 makes this four times bigger and 40 % slower
  29. // with auto-vectorization that is enabled by default with -O2.
  30. // Such vectorization bloat happens with -O2 when targeting ARM64 too
  31. // but performance hasn't been tested.
  32. #ifdef __clang__
  33. # pragma clang loop vectorize(disable)
  34. #endif
  35. for (i = 0; i + 4 <= size; i += 4) {
  36. uint32_t pc = (uint32_t)(now_pos + i);
  37. uint32_t instr = read32le(buffer + i);
  38. if ((instr >> 26) == 0x25) {
  39. // BL instruction:
  40. // The full 26-bit immediate is converted.
  41. // The range is +/-128 MiB.
  42. //
  43. // Using the full range helps quite a lot with
  44. // big executables. Smaller range would reduce false
  45. // positives in non-code sections of the input though
  46. // so this is a compromise that slightly favors big
  47. // files. With the full range, only six bits of the 32
  48. // need to match to trigger a conversion.
  49. const uint32_t src = instr;
  50. instr = 0x94000000;
  51. pc >>= 2;
  52. if (!is_encoder)
  53. pc = 0U - pc;
  54. instr |= (src + pc) & 0x03FFFFFF;
  55. write32le(buffer + i, instr);
  56. } else if ((instr & 0x9F000000) == 0x90000000) {
  57. // ADRP instruction:
  58. // Only values in the range +/-512 MiB are converted.
  59. //
  60. // Using less than the full +/-4 GiB range reduces
  61. // false positives on non-code sections of the input
  62. // while being excellent for executables up to 512 MiB.
  63. // The positive effect of ADRP conversion is smaller
  64. // than that of BL but it also doesn't hurt so much in
  65. // non-code sections of input because, with +/-512 MiB
  66. // range, nine bits of 32 need to match to trigger a
  67. // conversion (two 10-bit match choices = 9 bits).
  68. const uint32_t src = ((instr >> 29) & 3)
  69. | ((instr >> 3) & 0x001FFFFC);
  70. // With the addition only one branch is needed to
  71. // check the +/- range. This is usually false when
  72. // processing ARM64 code so branch prediction will
  73. // handle it well in terms of performance.
  74. //
  75. //if ((src & 0x001E0000) != 0
  76. // && (src & 0x001E0000) != 0x001E0000)
  77. if ((src + 0x00020000) & 0x001C0000)
  78. continue;
  79. instr &= 0x9000001F;
  80. pc >>= 12;
  81. if (!is_encoder)
  82. pc = 0U - pc;
  83. const uint32_t dest = src + pc;
  84. instr |= (dest & 3) << 29;
  85. instr |= (dest & 0x0003FFFC) << 3;
  86. instr |= (0U - (dest & 0x00020000)) & 0x00E00000;
  87. write32le(buffer + i, instr);
  88. }
  89. }
  90. return i;
  91. }
  92. static lzma_ret
  93. arm64_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  94. const lzma_filter_info *filters, bool is_encoder)
  95. {
  96. return lzma_simple_coder_init(next, allocator, filters,
  97. &arm64_code, 0, 4, 4, is_encoder);
  98. }
  99. #ifdef HAVE_ENCODER_ARM64
  100. extern lzma_ret
  101. lzma_simple_arm64_encoder_init(lzma_next_coder *next,
  102. const lzma_allocator *allocator,
  103. const lzma_filter_info *filters)
  104. {
  105. return arm64_coder_init(next, allocator, filters, true);
  106. }
  107. #endif
  108. #ifdef HAVE_DECODER_ARM64
  109. extern lzma_ret
  110. lzma_simple_arm64_decoder_init(lzma_next_coder *next,
  111. const lzma_allocator *allocator,
  112. const lzma_filter_info *filters)
  113. {
  114. return arm64_coder_init(next, allocator, filters, false);
  115. }
  116. #endif