Skip to content

Commit 97c926e

Browse files
earlephilhowerd-a-v
authored andcommitted
Move all PSTRs to own section, allow string dedup (#6565)
* Move all PSTRs to own section, allow string dedup GNU ld can deduplicate strings, and does so for most normal char *s automatically. However, for PSTRs we were using a unique section per string *and* the section was not flagges as containing dedupable 0-terminated strings. Modify the PSTR macro to emit assembly, which lets us set the section flags required (SM) for the variables, and use inline assembly to get the asm-block defined address. Should result in smaller compiled binaries if any strings are duplicated. * Give each PSTR its own segment * Allow disposing of unused strings before merging Add the "a" section flag to allow the linker to throw away unneeded strings. Without this flag, the linker will not discard unreferenced strings and ROMs will increase in size, possibly dramatically.
1 parent d475754 commit 97c926e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

tools/sdk/ld/eagle.app.v6.common.ld.h

+3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ SECTIONS
181181
*libwps.a:(.literal.* .text.*)
182182
*(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom0.text.* .irom.text .irom.text.*)
183183

184+
/* Constant strings in flash (PSTRs) */
185+
*(.irom0.pstr.*)
186+
184187
/* __FUNCTION__ locals */
185188
*(.rodata._ZZ*__FUNCTION__)
186189
*(.rodata._ZZ*__PRETTY_FUNCTION__)

tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
// Ref: https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Variable-Attributes.html
2424
// Place each progmem object into its own named section, avoiding conflicts
2525
#define PROGMEM __attribute__((section( "\".irom.text." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__) "\"")))
26+
#define PROGMEM_PSTR "\".irom0.pstr." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__) "\""
2627
#endif
2728
#ifndef PGM_P
2829
#define PGM_P const char *
@@ -34,7 +35,12 @@ extern "C" {
3435
// PSTR() macro modified to start on a 32-bit boundary. This adds on average
3536
// 1.5 bytes/string, but in return memcpy_P and strcpy_P will work 4~8x faster
3637
#ifndef PSTR
37-
#define PSTR(s) (__extension__({static const char __c[] __attribute__((__aligned__(4))) PROGMEM = (s); &__c[0];}))
38+
// Adapted from AVR-specific code at https://forum.arduino.cc/index.php?topic=194603.0
39+
#define PSTR(str) (__extension__({ \
40+
PGM_P ptr; \
41+
asm volatile ( ".pushsection " PROGMEM_PSTR ", \"aSM\", @progbits, 1 \n .align 4 \n 0: .string " __STRINGIZE(str) "\n .popsection \n" ); \
42+
asm volatile ( "movi %0, 0b" : "=r" (ptr) ); \
43+
ptr; }))
3844
#endif
3945

4046
// Flash memory must be read using 32 bit aligned addresses else a processor

0 commit comments

Comments
 (0)