13b35e7eeSXin LI /* SPDX-License-Identifier: 0BSD */ 23b35e7eeSXin LI 381ad8388SMartin Matuska /** 481ad8388SMartin Matuska * \file lzma/bcj.h 581ad8388SMartin Matuska * \brief Branch/Call/Jump conversion filters 6c917796cSXin LI * \note Never include this file directly. Use <lzma.h> instead. 781ad8388SMartin Matuska */ 881ad8388SMartin Matuska 981ad8388SMartin Matuska /* 1081ad8388SMartin Matuska * Author: Lasse Collin 1181ad8388SMartin Matuska */ 1281ad8388SMartin Matuska 1381ad8388SMartin Matuska #ifndef LZMA_H_INTERNAL 1481ad8388SMartin Matuska # error Never include this file directly. Use <lzma.h> instead. 1581ad8388SMartin Matuska #endif 1681ad8388SMartin Matuska 1781ad8388SMartin Matuska 1881ad8388SMartin Matuska /* Filter IDs for lzma_filter.id */ 1981ad8388SMartin Matuska 20c917796cSXin LI /** 21c917796cSXin LI * \brief Filter for x86 binaries 22c917796cSXin LI */ 2381ad8388SMartin Matuska #define LZMA_FILTER_X86 LZMA_VLI_C(0x04) 2481ad8388SMartin Matuska 2581ad8388SMartin Matuska /** 26c917796cSXin LI * \brief Filter for Big endian PowerPC binaries 27c917796cSXin LI */ 28c917796cSXin LI #define LZMA_FILTER_POWERPC LZMA_VLI_C(0x05) 29c917796cSXin LI 30c917796cSXin LI /** 31c917796cSXin LI * \brief Filter for IA-64 (Itanium) binaries 32c917796cSXin LI */ 33c917796cSXin LI #define LZMA_FILTER_IA64 LZMA_VLI_C(0x06) 34c917796cSXin LI 35c917796cSXin LI /** 36c917796cSXin LI * \brief Filter for ARM binaries 37c917796cSXin LI */ 38c917796cSXin LI #define LZMA_FILTER_ARM LZMA_VLI_C(0x07) 39c917796cSXin LI 40c917796cSXin LI /** 41c917796cSXin LI * \brief Filter for ARM-Thumb binaries 42c917796cSXin LI */ 43c917796cSXin LI #define LZMA_FILTER_ARMTHUMB LZMA_VLI_C(0x08) 44c917796cSXin LI 45c917796cSXin LI /** 46c917796cSXin LI * \brief Filter for SPARC binaries 47c917796cSXin LI */ 48c917796cSXin LI #define LZMA_FILTER_SPARC LZMA_VLI_C(0x09) 49c917796cSXin LI 50c917796cSXin LI /** 51c917796cSXin LI * \brief Filter for ARM64 binaries 52c917796cSXin LI */ 53c917796cSXin LI #define LZMA_FILTER_ARM64 LZMA_VLI_C(0x0A) 54c917796cSXin LI 553b35e7eeSXin LI /** 563b35e7eeSXin LI * \brief Filter for RISC-V binaries 573b35e7eeSXin LI */ 583b35e7eeSXin LI #define LZMA_FILTER_RISCV LZMA_VLI_C(0x0B) 593b35e7eeSXin LI 60c917796cSXin LI 61c917796cSXin LI /** 62c917796cSXin LI * \brief Options for BCJ filters 6381ad8388SMartin Matuska * 6481ad8388SMartin Matuska * The BCJ filters never change the size of the data. Specifying options 6581ad8388SMartin Matuska * for them is optional: if pointer to options is NULL, default value is 6681ad8388SMartin Matuska * used. You probably never need to specify options to BCJ filters, so just 6781ad8388SMartin Matuska * set the options pointer to NULL and be happy. 6881ad8388SMartin Matuska * 6981ad8388SMartin Matuska * If options with non-default values have been specified when encoding, 7081ad8388SMartin Matuska * the same options must also be specified when decoding. 7181ad8388SMartin Matuska * 7281ad8388SMartin Matuska * \note At the moment, none of the BCJ filters support 7381ad8388SMartin Matuska * LZMA_SYNC_FLUSH. If LZMA_SYNC_FLUSH is specified, 7481ad8388SMartin Matuska * LZMA_OPTIONS_ERROR will be returned. If there is need, 7581ad8388SMartin Matuska * partial support for LZMA_SYNC_FLUSH can be added in future. 7681ad8388SMartin Matuska * Partial means that flushing would be possible only at 7781ad8388SMartin Matuska * offsets that are multiple of 2, 4, or 16 depending on 7881ad8388SMartin Matuska * the filter, except x86 which cannot be made to support 7981ad8388SMartin Matuska * LZMA_SYNC_FLUSH predictably. 8081ad8388SMartin Matuska */ 8181ad8388SMartin Matuska typedef struct { 8281ad8388SMartin Matuska /** 8381ad8388SMartin Matuska * \brief Start offset for conversions 8481ad8388SMartin Matuska * 8581ad8388SMartin Matuska * This setting is useful only when the same filter is used 8681ad8388SMartin Matuska * _separately_ for multiple sections of the same executable file, 8781ad8388SMartin Matuska * and the sections contain cross-section branch/call/jump 8881ad8388SMartin Matuska * instructions. In that case it is beneficial to set the start 8981ad8388SMartin Matuska * offset of the non-first sections so that the relative addresses 9081ad8388SMartin Matuska * of the cross-section branch/call/jump instructions will use the 9181ad8388SMartin Matuska * same absolute addresses as in the first section. 9281ad8388SMartin Matuska * 9381ad8388SMartin Matuska * When the pointer to options is NULL, the default value (zero) 9481ad8388SMartin Matuska * is used. 9581ad8388SMartin Matuska */ 9681ad8388SMartin Matuska uint32_t start_offset; 9781ad8388SMartin Matuska 9881ad8388SMartin Matuska } lzma_options_bcj; 99*128836d3SXin LI 100*128836d3SXin LI 101*128836d3SXin LI /** 102*128836d3SXin LI * \brief Raw ARM64 BCJ encoder 103*128836d3SXin LI * 104*128836d3SXin LI * This is for special use cases only. 105*128836d3SXin LI * 106*128836d3SXin LI * \param start_offset The lowest 32 bits of the offset in the 107*128836d3SXin LI * executable being filtered. For the ARM64 108*128836d3SXin LI * filter, this must be a multiple of four. 109*128836d3SXin LI * For the very best results, this should also 110*128836d3SXin LI * be in sync with 4096-byte page boundaries 111*128836d3SXin LI * in the executable due to how ARM64's ADRP 112*128836d3SXin LI * instruction works. 113*128836d3SXin LI * \param buf Buffer to be filtered in place 114*128836d3SXin LI * \param size Size of the buffer 115*128836d3SXin LI * 116*128836d3SXin LI * \return Number of bytes that were processed in `buf`. This is at most 117*128836d3SXin LI * `size`. With the ARM64 filter, the return value is always 118*128836d3SXin LI * a multiple of 4, and at most 3 bytes are left unfiltered. 119*128836d3SXin LI * 120*128836d3SXin LI * \since 5.7.1alpha 121*128836d3SXin LI */ 122*128836d3SXin LI extern LZMA_API(size_t) lzma_bcj_arm64_encode( 123*128836d3SXin LI uint32_t start_offset, uint8_t *buf, size_t size) lzma_nothrow; 124*128836d3SXin LI 125*128836d3SXin LI /** 126*128836d3SXin LI * \brief Raw ARM64 BCJ decoder 127*128836d3SXin LI * 128*128836d3SXin LI * See lzma_bcj_arm64_encode(). 129*128836d3SXin LI * 130*128836d3SXin LI * \since 5.7.1alpha 131*128836d3SXin LI */ 132*128836d3SXin LI extern LZMA_API(size_t) lzma_bcj_arm64_decode( 133*128836d3SXin LI uint32_t start_offset, uint8_t *buf, size_t size) lzma_nothrow; 134*128836d3SXin LI 135*128836d3SXin LI 136*128836d3SXin LI /** 137*128836d3SXin LI * \brief Raw RISC-V BCJ encoder 138*128836d3SXin LI * 139*128836d3SXin LI * This is for special use cases only. 140*128836d3SXin LI * 141*128836d3SXin LI * \param start_offset The lowest 32 bits of the offset in the 142*128836d3SXin LI * executable being filtered. For the RISC-V 143*128836d3SXin LI * filter, this must be a multiple of 2. 144*128836d3SXin LI * \param buf Buffer to be filtered in place 145*128836d3SXin LI * \param size Size of the buffer 146*128836d3SXin LI * 147*128836d3SXin LI * \return Number of bytes that were processed in `buf`. This is at most 148*128836d3SXin LI * `size`. With the RISC-V filter, the return value is always 149*128836d3SXin LI * a multiple of 2, and at most 7 bytes are left unfiltered. 150*128836d3SXin LI * 151*128836d3SXin LI * \since 5.7.1alpha 152*128836d3SXin LI */ 153*128836d3SXin LI extern LZMA_API(size_t) lzma_bcj_riscv_encode( 154*128836d3SXin LI uint32_t start_offset, uint8_t *buf, size_t size) lzma_nothrow; 155*128836d3SXin LI 156*128836d3SXin LI /** 157*128836d3SXin LI * \brief Raw RISC-V BCJ decoder 158*128836d3SXin LI * 159*128836d3SXin LI * See lzma_bcj_riscv_encode(). 160*128836d3SXin LI * 161*128836d3SXin LI * \since 5.7.1alpha 162*128836d3SXin LI */ 163*128836d3SXin LI extern LZMA_API(size_t) lzma_bcj_riscv_decode( 164*128836d3SXin LI uint32_t start_offset, uint8_t *buf, size_t size) lzma_nothrow; 165*128836d3SXin LI 166*128836d3SXin LI 167*128836d3SXin LI /** 168*128836d3SXin LI * \brief Raw x86 BCJ encoder 169*128836d3SXin LI * 170*128836d3SXin LI * This is for special use cases only. 171*128836d3SXin LI * 172*128836d3SXin LI * \param start_offset The lowest 32 bits of the offset in the 173*128836d3SXin LI * executable being filtered. For the x86 174*128836d3SXin LI * filter, all values are valid. 175*128836d3SXin LI * \param buf Buffer to be filtered in place 176*128836d3SXin LI * \param size Size of the buffer 177*128836d3SXin LI * 178*128836d3SXin LI * \return Number of bytes that were processed in `buf`. This is at most 179*128836d3SXin LI * `size`. For the x86 filter, the return value is always 180*128836d3SXin LI * a multiple of 1, and at most 4 bytes are left unfiltered. 181*128836d3SXin LI * 182*128836d3SXin LI * \since 5.7.1alpha 183*128836d3SXin LI */ 184*128836d3SXin LI extern LZMA_API(size_t) lzma_bcj_x86_encode( 185*128836d3SXin LI uint32_t start_offset, uint8_t *buf, size_t size) lzma_nothrow; 186*128836d3SXin LI 187*128836d3SXin LI /** 188*128836d3SXin LI * \brief Raw x86 BCJ decoder 189*128836d3SXin LI * 190*128836d3SXin LI * See lzma_bcj_x86_encode(). 191*128836d3SXin LI * 192*128836d3SXin LI * \since 5.7.1alpha 193*128836d3SXin LI */ 194*128836d3SXin LI extern LZMA_API(size_t) lzma_bcj_x86_decode( 195*128836d3SXin LI uint32_t start_offset, uint8_t *buf, size_t size) lzma_nothrow; 196