XaviDCR92 / stm8-dce-example

Demonstration of link-time dead code elimination for the STM8 using SDCC and GNU binutils
11 stars 2 forks source link

Link-time dead code elimination for the STM8

Overview

This small code example demonstrates the new capabilities that have been implemented into the STM8 port of GNU ld that allows using --gc-sections and thus remove unused code at link-time. Feel free to comment out or create any symbols so you can see how final memory usage is affected.

Usage

make

This is the expected output from make:

sdcc src/main.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --gas --function-sections --data-sections -Iinc/ -MM > obj/main.d
sdcc src/main.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --gas --function-sections --data-sections -Iinc/ -o obj/main.o
sdcc src/bar.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --gas --function-sections --data-sections -Iinc/ -MM > obj/bar.d
sdcc src/bar.c -DSTM8S003 -mstm8 --out-fmt-elf -c --debug --opt-code-size --gas --function-sections --data-sections -Iinc/ -o obj/bar.o
stm8-ld obj/main.o obj/bar.o -o obj/STM8.elf -T./elf32stm8.x --print-memory-usage --gc-sections -Map obj/map_STM8.map 
Memory region         Used Size  Region Size  %age Used
      interrupts:           4 B        128 B      3.12%
             RAM:           5 B        894 B      0.56%
           STACK:          0 GB        128 B      0.00%
          eeprom:          0 GB        128 B      0.00%
            fuse:          0 GB         11 B      0.00%
            lock:          0 GB         1 KB      0.00%
             ROM:         100 B         8 KB      1.22%
       signature:          0 GB         1 KB      0.00%
 user_signatures:          0 GB         1 KB      0.00%
rm obj/bar.d obj/main.d

On the other hand, it demonstrates how SDCC can now output *.o files by generating GAS-compatible assembly code and calling stm8-as afterwards. This allows support for other tools from the GNU binutils package, such as objdump, size, readelf, etc.

Toolchain download links

GNU as-compatible SDCC

https://github.com/XaviDCR92/sdcc-gas

GNU binutils package modified to support the STM8

https://github.com/XaviDCR92/stm8-binutils-gdb (forked from https://stm8-binutils-gdb.sourceforge.io)

Background

As of SDCC 3.9.0, ELF and DWARF2 support were implemented via the in-house linker, sdld. Another package, stm8-binutils-gdb, provides a port of both GNU binutils and the GNU debugger for the STM8. However, this implementation worked partially. These were some of the issues I faced when using the SDCC toolchain:

Known issues