building lzma stubs is a headache
The recipe to build the stub de-compression code for lzma is a maintenance headache. The shell code in src/stub/src/arch/.../Makefile.extra contains a step such as
./arm64/v8/Makefile.extra: head -c-4 tmp/$T.bin > tmp/$T.outwhich assumes that the C compiler generates assembly language whose last instruction contains the only subroutine return instruction. Too often this assumption is false, partly because of possible improvements to compiler code generation.
The reason for trying to remove the return is to change the compiler-generated code from a closed subroutine to become an inline fall-through block. This saves 4 or 5 bytes of space for a CALL instruction, and makes the lzma code similar to the NRV/UCL code, which is generated by hand as an inline fall-through block. The i386 build recipe performs handsprings to deal with compiler-generated code for lzma de-compression. This is fragile and too cumbersome to implement for the other architectures, which now outnumber and are more important than i386.
So, the recipe for building the lzma de-compressor should be changed to generate an out-of-line subroutine in all cases, which is simpler and easier to maintain. The surrounding code will require the CALL, and a place to put the closed subroutine.
Here is an example of the problems involved in forcing an inline block by removing the last instruction:
## $ grep -sr 'head -c-[^0]' .
$ for file in ./arm64/v8/tmp/lzma_d_cs.o.disasm ./arm64/v8/tmp/lzma_d_cf.o.disasm; \
do tail -1 $file | sed -e "sX^X$file X"; done
./arm64/v8/tmp/lzma_d_cs.o.disasm 8d8: d65f03c0 ret
./arm64/v8/tmp/lzma_d_cf.o.disasm 968: 17fffe43 b 274 <LzmaDecode+0x274>The last instruciton is a return in lzma_d_cs, but a branch in lzma_d_cf, so applying the same recipe to both cases generates a bug in one of the cases.
Please tell us details about your environment.
- UPX version used (
upx --version): upx 5.2.0 - Host Operating System and version: Linux 6.X
- Host CPU architecture:x86_64
- Target Operating System and version: all
- Target CPU architecture: all
Source: upx/upx