Skip to content

Add support for multiple progmem sections #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spapadim opened this issue Aug 30, 2015 · 7 comments
Closed

Add support for multiple progmem sections #734

spapadim opened this issue Aug 30, 2015 · 7 comments
Milestone

Comments

@spapadim
Copy link

Current setup does not remove unused code and constants. Fixing this is a simple change to platforms.txt; I changed two lines, for compiler.c.flags and compiler.c.elf.flags to:

compiler.c.flags=-c -Os -g -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -falign-functions=4 -MMD -std=gnu99 -ffunction-sections -fdata-sections

compiler.c.elf.flags=-g -Os -nostdlib -Wl,--gc-sections -Wl,--no-check-sections -u call_user_start -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-T{build.flash_ld}" -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy

Let me know if you'd like a PR, but the changes are very simple: adding -ffunction-sections -fdata-section to the first and -Wl,--gc-sections.

I've tested this on a port of olikraus's excellent ucglib to the ESP; see https://github.com/spapadim/ucglib and olikraus/ucglib#61. The code compiles without issues with these simple changes (otherwise fails, since ucglib compiles all font data, several MB, putting each in a separate section, and relying on the linker to pick the ones actually used).

@asetyde
Copy link

asetyde commented Aug 30, 2015

+1

Sent from my iPhone

On 30 Aug 2015, at 21:01, Spiros Papadimitriou [email protected] wrote:

Current setup does not remove unused code and constants. Fixing this is a simple change to platforms.txt; I changed two lines, for compiler.c.flags and compiler.c.elf.flags to:

compiler.c.flags=-c -Os -g -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -falign-functions=4 -MMD -std=gnu99 -ffunction-sections -fdata-sections

compiler.c.elf.flags=-g -Os -nostdlib -Wl,--gc-sections -Wl,--no-check-sections -u call_user_start -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-T{build.flash_ld}" -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy

Let me know if you'd like a PR, but the changes are very simple: adding -ffunction-sections -fdata-section to the first and -Wl,--gc-sections.

I've tested this on a port of olikraus's excellent ucglib to the ESP; see https://github.com/spapadim/ucglib and olikraus/ucglib#61. The code compiles without issues with these simple changes (otherwise fails, since ucglib compiles all font data, several MB, putting each in a separate section, and relying on the linker to pick the ones actually used).


Reply to this email directly or view it on GitHub.

@spapadim spapadim changed the title Enable section garbage collection for linker (-Wl,--gc-sections) Add support for multiple progmem sections Sep 13, 2015
@spapadim
Copy link
Author

Got the ucglib port running (short demo: https://youtu.be/fafYB9CCGQ4), and successfully remove unused global font data automatically. This appears to need support for multiple progmem sections, which also requires a simple edit to the linker scripts -- changing issue title to better reflect this.

The current approach mirrors what olikraus did for the AVR arch.

If you can think of a better way to accomplish the same, please do let me know. Anything else I tried to get those global font vars removed by linker failed, but I'm not an expert in this stuff (just get by as needed :).

The patches are https://gist.github.com/spapadim/a4bc258df47f00831006 and the ucglib port that makes use of this feature is https://github.com/spapadim/ucglib -- note that one of the patches is in the gcc toolchain.

@igrr
Copy link
Member

igrr commented Sep 14, 2015

I understand the need for -fdata-sections to strip font data, but why does this require -ffunction-sections?

@spapadim
Copy link
Author

I basically copied avr-gcc flags Arduino uses on this :) -- can try w/o -ffunction-sections.

However, the essential bit seems to be support for explicit separate irom sections (and progmem sections on AVR), relevant defines are here:
https://github.com/spapadim/ucglib/blob/master/csrc/ucg.h#L105
and how they're used e.g., here: https://github.com/spapadim/ucglib/blob/master/csrc/ucg.h#L1292 -- which requres editing the linker scripts to include them in the irom output section.

I can confirm that setting UCG_FONT_SECTION to just .irom.text for all font data does not strip the unused ones, even with -fdata-sections and -Wl,-gc-sections flags.

@igrr
Copy link
Member

igrr commented Sep 14, 2015

That's right, -fdata-sections doesn't work when you specify section explicitly in an attribute.
Data gets placed into one of the 'common' sections which will not get garbage collected.
Same thing applies to functions as well. We need an additional objcopy --rename-section step in order for this to work.

I think for the change above you only need -Wl,-gc-sections plus the change to the linker script.

@spapadim
Copy link
Author

Yes, you are right, the -f flags are redundant -- just verified. Gist updated.

Now it makes sense btw, thanks -- unused code/data gc unit is always section, and if you want to remove unused data from progmem/irom (which necessarily have explicit section attribute), you need to play the tricks olikraus does (and I was wondering why he did it!).

So this would also imply that ICACHE_FLASH_ATTR functions also won't be removed, even if unused? I need to RTFM on objcopy, but it would be nice if removal of unused functions is also enabled. E.g., adding only #include <Wire.h> without ever using it adds all of the Wire library ( + soft-spi code) in the executable on the ESP, but not on AVR -- I think I got bitten by this a couple of weeks back.

@igrr
Copy link
Member

igrr commented Oct 6, 2015

@spapadim I have merged #857, so you can now remove ICACHE_FLASH_ATTR from your functions, as contents of .c files is now placed into flash automatically. You can use ICACHE_RAM_ATTR to override this, placing code into RAM.
gc-sections should also work now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants