xfangfang / PPPwn_cpp

C++ rewrite of PPPwn (PlayStation 4 PPPoE RCE)
GNU General Public License v3.0
389 stars 57 forks source link

How can I reduce the size of the compiled file? #87

Closed EAScript closed 2 months ago

EAScript commented 2 months ago

The compiled files with a direct download link are tiny (under 1MB), but when I compile them myself, they are about 20MB!

EAScript commented 2 months ago

What I noticed was that CMAKE_BUILD_TYPE should be equal to release and I think it should be in debug mode right now, but since Cmake is managed by Zig, I didn't really understand its function.

xfangfang commented 2 months ago

zig cc/c++ is like gcc/g++, it's just convenient for cross compilation.

As stated in the readme:

Please refer to the workflow file .github/workflows/ci.yaml for more information.

-DCMAKE_BUILD_TYPE=MinSizeRel


There are also some platform related methods, and the content listed below may not be suitable for all platforms

EAScript commented 2 months ago

zig cc/c++ is like gcc/g++, it's just convenient for cross compilation.

As stated in the readme:

Please refer to the workflow file .github/workflows/ci.yaml for more information.

-DCMAKE_BUILD_TYPE=MinSizeRel

There are also some platform related methods, and the content listed below may not be suitable for all platforms

  • If you are using zig, you can: -DZIG_COMPILE_OPTION='-ffunction-sections'
  • strip the binary
  • using upx to compress the binary

The Zig Compile option is currently equal to "-mcpu=cortex_a7". I didn't know exactly how to add two options, so I added the second option with a space, which received errors and had no effect as follows:

-DZIG_COMPILE_OPTION="-mcpu=cortex_a7 -ffunction-sections" //Error Not Found CPU

and

-DZIG_COMPILE_OPTION="-ffunction-sections -mcpu=cortex_a7" //Another Errors but compiled


I wrote them separately, again, no effect, but no error was received, as follows:

-DZIG_COMPILE_OPTION='-ffunction-sections' -DZIG_COMPILE_OPTION="-mcpu=cortex_a7" //compiled but not effective


I used upx to compress, the file size was reduced from 21MB to 6MB, but it is still far from 417KB, on the other hand, according to what I found, using upx is not a good thing, because at the time of execution, the same amount of RAM or Swap will be used in your device.

EAScript commented 2 months ago

I think it's better to compile natively with g++ on the a7 processor.

xfangfang commented 2 months ago

@EAScript

  1. cmake uses ; to write a list:

    -DCMAKE_BUILD_TYPE=MinSizeRel -DZIG_COMPILE_OPTION='-ffunction-sections;-mcpu=cortex_a7'
  2. All the built binary is compiled through the previous script(.github/workflows/ci.yaml). As long as you can understand the workflow file of GitHub Actions, you should be able to reproduce the same result locally.

I think it's better to compile natively with g++ on the a7 processor.

  1. I don't think there will be too much difference between cross compilation and native compilation. But if your goal is a smaller binary size, it is indeed more suitable to compile natively. Because native compilation usually uses glibc in the system. And to avoid dependency issues with glibc, the prebuilt binary is statically linked with musl. You can refer to readme to compile natively (or modify -DZIG_TARGET=arm-linux-musleabi to -DZIG_TARGET=arm-linux-gnueabi or -DZIG_TARGET=arm-linux-gnueabihf This will do similar things, but it may not work properly on all devices).

  2. If your goal is a smaller binary size, I think upx is born for this goal, because some people use pppwn_cpp in devices with limited space such as routers. Even if there is no difference at runtime, reducing the file size is also one of the requirements.

  3. I think the most effective way to reduce file size is to use -DCMAKE_BUILD_TYPE=MinSizeRel and strip your binary

xfangfang commented 2 months ago

If you still cannot reduce the file size, please start from a new directory and record all shell commands, then send them here.

EAScript commented 2 months ago

@xfangfang

Thank you for your help and attention🙏