| 1 | // SPDX-License-Identifier: 0BSD |
| 2 | // SPDX-FileCopyrightText: The XZ for Java authors and contributors |
| 3 | // SPDX-FileContributor: Lasse Collin <[email protected]> |
| 4 | // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/> |
| 5 | |
| 6 | package org.tukaani.xz.simple; |
| 7 | |
| 8 | import org.tukaani.xz.common.ByteArrayView; |
| 9 | |
| 10 | // BCJ filter for ARM64 (AArch64) instructions |
| 11 | public final class ARM64 implements SimpleFilter { |
| 12 | private final boolean isEncoder; |
| 13 | private int pos; |
| 14 | |
| 15 | public ARM64(boolean isEncoder, int startPos) { |
| 16 | this.isEncoder = isEncoder; |
| 17 | pos = startPos; |
| 18 | } |
| 19 | |
| 20 | @Override |
| 21 | public int code(byte[] buf, int off, int len) { |
| 22 | int end = off + len - 4; |
| 23 | int i; |
| 24 | |
| 25 | for (i = off; i <= end; i += 4) { |
| 26 | // Only the highest byte is needed to identify the BL and ADRP |
| 27 | // instructions. |
| 28 | int instr = buf[i + 3]; |
| 29 | |
| 30 | if ((instr & 0xFC) == 0x94) { |
| 31 | // BL instruction: |
| 32 | // The full 26-bit immediate is converted. |
| 33 | // The range is +/-128 MiB. |
| 34 | // |
| 35 | // Using the full range helps quite a lot with big |
| 36 | // executables. Smaller range would reduce false positives |
| 37 | // in non-code sections of the input though so this is |
| 38 | // a compromise that slightly favors big files. With the |
| 39 | // full range, only six bits of the 32 need to match to |
| 40 | // trigger a conversion. |
| 41 | instr = ByteArrayView.getIntLE(buf, i); |
| 42 | |
| 43 | int pc = (pos + i - off) >>> 2; |
| 44 | if (!isEncoder) |
| 45 | pc = -pc; |
| 46 | |
| 47 | instr = 0x94000000 | ((instr + pc) & 0x03FFFFFF); |
| 48 | ByteArrayView.setIntLE(buf, i, instr); |
| 49 | |
| 50 | } else if ((instr & 0x9F) == 0x90) { |
| 51 | // ADRP instruction: |
| 52 | // Only values in the range +/-512 MiB are converted. |
| 53 | // |
| 54 | // Using less than the full +/-4 GiB range reduces false |
| 55 | // positives on non-code sections of the input while being |
| 56 | // excellent for executables up to 512 MiB. The positive |
| 57 | // effect of ADRP conversion is smaller than that of BL |
| 58 | // but it also doesn't hurt so much in non-code sections |
| 59 | // of input because, with +/-512 MiB range, nine bits of 32 |
| 60 | // need to match to trigger a conversion (two 10-bit match |
| 61 | // choices = 9 bits). |
| 62 | instr = ByteArrayView.getIntLE(buf, i); |
| 63 | int src = ((instr >>> 29) & 3) | ((instr >>> 3) & 0x001FFFFC); |
| 64 | |
| 65 | // With the addition only one branch is needed to |
| 66 | // check the +/- range. This is usually false when |
| 67 | // processing ARM64 code so branch prediction will |
| 68 | // handle it well in terms of performance. |
| 69 | // |
| 70 | // if ((src & 0x001E0000) != 0 |
| 71 | // && (src & 0x001E0000) != 0x001E0000) |
| 72 | if (((src + 0x00020000) & 0x001C0000) != 0) |
| 73 | continue; |
| 74 | |
| 75 | int pc = (pos + i - off) >>> 12; |
| 76 | if (!isEncoder) |
| 77 | pc = -pc; |
| 78 | |
| 79 | int dest = src + pc; |
| 80 | |
| 81 | instr &= 0x9000001F; |
| 82 | instr |= (dest & 3) << 29; |
| 83 | instr |= (dest & 0x0003FFFC) << 3; |
| 84 | instr |= (-(dest & 0x00020000)) & 0x00E00000; |
| 85 | ByteArrayView.setIntLE(buf, i, instr); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | i -= off; |
| 90 | pos += i; |
| 91 | return i; |
| 92 | } |
| 93 | } |