cnlohr / ch32v003fun

Open source minimal stack for the ch32 line of WCH processors, including the ch32v003, a 10ยข 48 MHz RISC-V Microcontroller - as well as many other chips within the ch32v/x line.
MIT License
960 stars 147 forks source link

Why no `-fdata-sections`? #265

Closed TommyMurphyTM1234 closed 9 months ago

TommyMurphyTM1234 commented 10 months ago

Just curious - is there any specific reason that you don't use -fdata-sections along with -ffunction-sections:

and, ultimately, -gc-sections:

to further optimise linked executables for size?

Are there, perhaps, some "magic" data sections that would be impacted by this?

By the way, thanks for this work. Very interesting. ๐Ÿ‘

cnlohr commented 9 months ago

If you make a PR for this, I will accept it.

TommyMurphyTM1234 commented 9 months ago

If you make a PR for this, I will accept it.

Thanks @cnlohr - I'll have a look and submit a PR if it looks worthwhile.

TommyMurphyTM1234 commented 9 months ago

Before I prepare a PR, and in case it influences the decision on whether or not to make the change, here are the results of building the example programs without and with -fdata-sections.

riscv64-unknown-elf-size blink_raw.elf text data bss dec hex filename 568 0 0 568 238 blink_raw.elf 568 0 0 568 238 blink_raw.elf (-fdata-sections)

riscv64-unknown-elf-size bootload.elf text data bss dec hex filename 612 0 4 616 268 bootload.elf 612 0 4 616 268 bootload.elf (-fdata-sections)

riscv64-unknown-elf-size cap_touch_adc.elf text data bss dec hex filename 2352 0 0 2352 930 cap_touch_adc.elf 2352 0 0 2352 930 cap_touch_adc.elf (-fdata-sections)

riscv64-unknown-elf-size cpp_virtual_methods.elf text data bss dec hex filename 4184 4 12 4200 1068 cpp_virtual_methods.elf 4184 4 12 4200 1068 cpp_virtual_methods.elf (-fdata-sections)

riscv64-unknown-elf-size direct_gpio.elf text data bss dec hex filename 560 0 0 560 230 direct_gpio.elf 560 0 0 560 230 direct_gpio.elf (-fdata-sections)

riscv64-unknown-elf-size external_crystal.elf text data bss dec hex filename 540 0 0 540 21c external_crystal.elf 540 0 0 540 21c external_crystal.elf (-fdata-sections)

riscv64-unknown-elf-size exti_pin_change_isr.elf text data bss dec hex filename 520 0 0 520 208 exti_pin_change_isr.elf 520 0 0 520 208 exti_pin_change_isr.elf (-fdata-sections)

riscv64-unknown-elf-size GPIO.elf text data bss dec hex filename 384 0 0 384 180 GPIO.elf 384 0 0 384 180 GPIO.elf (-fdata-sections)

riscv64-unknown-elf-size iwdg.elf text data bss dec hex filename 660 0 0 660 294 iwdg.elf 660 0 0 660 294 iwdg.elf (-fdata-sections)

riscv64-unknown-elf-size run_from_ram.elf text data bss dec hex filename 424 0 4 428 1ac run_from_ram.elf 424 0 4 428 1ac run_from_ram.elf (-fdata-sections)

riscv64-unknown-elf-size spi_24L01_rx.elf text data bss dec hex filename 3780 8 24 3812 ee4 spi_24L01_rx.elf 3780 8 24 3812 ee4 spi_24L01_rx.elf (-fdata-sections)

riscv64-unknown-elf-size spi_max7219.elf text data bss dec hex filename 5808 0 0 5808 16b0 spi_max7219.elf 5808 0 0 5808 16b0 spi_max7219.elf (-fdata-sections)

riscv64-unknown-elf-size standby_btn.elf text data bss dec hex filename 2036 0 0 2036 7f4 standby_btn.elf 2036 0 0 2036 7f4 standby_btn.elf (-fdata-sections)

riscv64-unknown-elf-size struct_direct_gpio.elf text data bss dec hex filename 560 0 0 560 230 struct_direct_gpio.elf 560 0 0 560 230 struct_direct_gpio.elf (-fdata-sections)

* Decrease in size - many marginal (-4, -8, -12 bytes) but some a bit more significant (-28 to -76 bytes)

riscv64-unknown-elf-size adc_dma_opamp.elf (-4 bytes) text data bss dec hex filename 2216 0 8 2224 8b0 adc_dma_opamp.elf 2212 0 8 2220 8ac adc_dma_opamp.elf (-fdata-sections)

riscv64-unknown-elf-size adc_polled.elf (-4 bytes) text data bss dec hex filename 2076 0 0 2076 81c adc_polled.elf 2072 0 0 2072 818 adc_polled.elf (-fdata-sections)

riscv64-unknown-elf-size color_lcd.elf (-76 bytes) text data bss dec hex filename 4360 16 1320 5696 1640 color_lcd.elf 4284 16 1320 5620 15f4 color_lcd.elf (-fdata-sections)

riscv64-unknown-elf-size debugprintfdemo.elf (-8 bytes) text data bss dec hex filename 1928 0 8 1936 790 debugprintfdemo.elf 1920 0 8 1928 788 debugprintfdemo.elf (-fdata-sections)

riscv64-unknown-elf-size flashtest.elf (-4 bytes) text data bss dec hex filename 2700 0 0 2700 a8c flashtest.elf 2696 0 0 2696 a88 flashtest.elf (-fdata-sections)

riscv64-unknown-elf-size hsitrim.elf (-4 bytes) text data bss dec hex filename 2320 0 4 2324 914 hsitrim.elf 2316 0 4 2320 910 hsitrim.elf (-fdata-sections)

riscv64-unknown-elf-size i2c_oled.elf (-72 bytes) text data bss dec hex filename 7244 0 512 7756 1e4c i2c_oled.elf 7172 0 512 7684 1e04 i2c_oled.elf (-fdata-sections)

riscv64-unknown-elf-size i2c_slave.elf (-12 bytes) text data bss dec hex filename 1072 0 44 1116 45c i2c_slave.elf 1060 0 44 1104 450 i2c_slave.elf (-fdata-sections)

riscv64-unknown-elf-size MCOtest.elf (-76 bytes) text data bss dec hex filename 2192 0 0 2192 890 MCOtest.elf 2116 0 0 2116 844 MCOtest.elf (-fdata-sections)

riscv64-unknown-elf-size optionbytes.elf (-4 bytes) text data bss dec hex filename 1900 0 0 1900 76c optionbytes.elf 1896 0 0 1896 768 optionbytes.elf (-fdata-sections)

riscv64-unknown-elf-size optiondata.elf (-4 bytes) text data bss dec hex filename 1864 0 0 1864 748 optiondata.elf 1860 0 0 1860 744 optiondata.elf (-fdata-sections)

riscv64-unknown-elf-size self_modify_code.elf (-4 bytes) text data bss dec hex filename 1652 32 0 1684 694 self_modify_code.elf 1648 32 0 1680 690 self_modify_code.elf (-fdata-sections)

riscv64-unknown-elf-size spi_dac.elf (-68 bytes) text data bss dec hex filename 3068 0 80 3148 c4c spi_dac.elf 3000 0 80 3080 c08 spi_dac.elf (-fdata-sections)

riscv64-unknown-elf-size spi_oled.elf (-64 bytes) text data bss dec hex filename 2932 8 1028 3968 f80 spi_oled.elf 2868 8 1028 3904 f40 spi_oled.elf (-fdata-sections)

riscv64-unknown-elf-size standby_autowake.elf (-4 bytes) text data bss dec hex filename 2068 0 0 2068 814 standby_autowake.elf 2064 0 0 2064 810 standby_autowake.elf (-fdata-sections)

riscv64-unknown-elf-size struct_gpio.elf (-4 bytes) text data bss dec hex filename 1792 0 0 1792 700 struct_gpio.elf 1788 0 0 1788 6fc struct_gpio.elf (-fdata-sections)

riscv64-unknown-elf-size sysclk_config.elf (-32 bytes) text data bss dec hex filename 3640 0 0 3640 e38 sysclk_config.elf 3608 0 0 3608 e18 sysclk_config.elf (-fdata-sections)

riscv64-unknown-elf-size systick_irq.elf (-4 bytes) text data bss dec hex filename 2100 0 4 2104 838 systick_irq.elf 2096 0 4 2100 834 systick_irq.elf (-fdata-sections)

riscv64-unknown-elf-size template.elf (-4 bytes) text data bss dec hex filename 1860 0 4 1864 748 template.elf 1856 0 4 1860 744 template.elf (-fdata-sections)

riscv64-unknown-elf-size tim1_pwm.elf (-4 bytes) text data bss dec hex filename 2020 0 0 2020 7e4 tim1_pwm.elf 2016 0 0 2016 7e0 tim1_pwm.elf (-fdata-sections)

riscv64-unknown-elf-size tim2_encoder.elf (-4 bytes) text data bss dec hex filename 1968 0 0 1968 7b0 tim2_encoder.elf 1964 0 0 1964 7ac tim2_encoder.elf (-fdata-sections)

riscv64-unknown-elf-size tim2_pwm.elf (-8 bytes) text data bss dec hex filename 2048 0 0 2048 800 tim2_pwm.elf 2040 0 0 2040 7f8 tim2_pwm.elf (-fdata-sections)

riscv64-unknown-elf-size uartdemo.elf (-4 bytes) text data bss dec hex filename 1608 0 4 1612 64c uartdemo.elf 1604 0 4 1608 648 uartdemo.elf (-fdata-sections)

riscv64-unknown-elf-size ws2812bdemo.elf (-28 bytes) text data bss dec hex filename 2460 32 496 2988 bac ws2812bdemo.elf 2432 32 496 2960 b90 ws2812bdemo.elf (-fdata-sections)

* Increase in size - all marginal (+4 or +8 bytes)

riscv64-unknown-elf-size adc_fixed_fs.elf (+8 bytes) text data bss dec hex filename 1296 0 516 1812 714 adc_fixed_fs.elf 1304 0 516 1820 71c adc_fixed_fs.elf (-fdata-sections)

riscv64-unknown-elf-size dma_gpio.elf (+4 bytes) text data bss dec hex filename 2216 0 1032 3248 cb0 dma_gpio.elf 2224 0 1028 3252 cb4 dma_gpio.elf (-fdata-sections)

riscv64-unknown-elf-size input_capture.elf (+8 bytes) text data bss dec hex filename 2232 0 72 2304 900 input_capture.elf 2240 0 72 2312 908 input_capture.elf (-fdata-sections)

riscv64-unknown-elf-size tim2_pwm_remap.elf (+4 bytes) text data bss dec hex filename 2236 0 0 2236 8bc tim2_pwm_remap.elf 2240 0 0 2240 8c0 tim2_pwm_remap.elf (-fdata-sections)

riscv64-unknown-elf-size spi_24L01_tx.elf (+4 bytes) text data bss dec hex filename 4120 8 24 4152 1038 spi_24L01_tx.elf 4124 8 24 4156 103c spi_24L01_tx.elf (-fdata-sections)

* Toolchain used (built from the `riscv-gnu-toolchain` [repo](https://github.com/riscv-collab/riscv-gnu-toolchain))

riscv64-unknown-elf-gcc -v Using built-in specs. COLLECT_GCC=riscv64-unknown-elf-gcc COLLECT_LTO_WRAPPER=/home/user/Downloads/riscv-tools/libexec/gcc/riscv64-unknown-elf/13.2.0/lto-wrapper Target: riscv64-unknown-elf Configured with: /home/user/Downloads/riscv-gnu-toolchain/gcc/configure --target=riscv64-unknown-elf --prefix=/home/user/Downloads/riscv-tools --disable-shared --disable-threads --enable-languages=c,c++ --with-pkgversion=gc891d8dc23e --with-system-zlib --enable-tls --with-newlib --with-sysroot=/home/user/Downloads/riscv-tools/riscv64-unknown-elf --with-native-system-header-dir=/include --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=.././gcc --disable-multilib --with-abi=lp64d --with-arch=rv64imafdc --with-tune=rocket --with-isa-spec=20191213 'CFLAGS_FOR_TARGET=-Os -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-Os -mcmodel=medlow' Thread model: single Supported LTO compression algorithms: zlib gcc version 13.2.0 (gc891d8dc23e)


Any opinions @cnlohr  or others?
cnlohr commented 9 months ago

I have a preference to include -fdata-sections regardless of the marginal increase in size.

TommyMurphyTM1234 commented 9 months ago

I have a preference to include -fdata-sections regardless of the marginal increase in size.

Thanks a lot @cnlohr. ๐Ÿ‘