Skip to content

Commit

Permalink
Move all PROGMEM to their own section (#5048)
Browse files Browse the repository at this point in the history
According to the GCC man page, __section__ attributes should only be used
for global variables.  However, the PROGMEM and ICACHE_RODATA macros use
this variable decorator even for local variables.  Most of the time it works,
but when a static or inlined function tries to use a PROGMEM/PSTR/etc.
variable the compiler can throw an error like:
  error: XXX causes a section type conflict with YYY

Change the PROGMEM macro to emit a section name that is unique (a combo
of the file, line, and counter variables to ensure uniqueness).  The
standard linker script will place them properly in .IROM without
any changes.

Fixes #5036 and others.
  • Loading branch information
earlephilhower authored and devyte committed Aug 15, 2018
1 parent adde93b commit 56b98fd
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cores/esp8266/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
size_t Print::write(const uint8_t *buffer, size_t size) {

#ifdef DEBUG_ESP_CORE
static char not_the_best_way [] ICACHE_RODATA_ATTR STORE_ATTR = "Print::write(data,len) should be overridden for better efficiency\r\n";
static char not_the_best_way [] PROGMEM STORE_ATTR = "Print::write(data,len) should be overridden for better efficiency\r\n";
static bool once = false;
if (!once) {
once = true;
Expand Down
6 changes: 3 additions & 3 deletions cores/esp8266/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ void* ICACHE_RAM_ATTR pvPortZalloc(size_t size, const char* file, int line)
#undef calloc
#undef realloc

static const char oom_fmt[] ICACHE_RODATA_ATTR STORE_ATTR = ":oom(%d)@?\n";
static const char oom_fmt_1[] ICACHE_RODATA_ATTR STORE_ATTR = ":oom(%d)@";
static const char oom_fmt_2[] ICACHE_RODATA_ATTR STORE_ATTR = ":%d\n";
static const char oom_fmt[] PROGMEM STORE_ATTR = ":oom(%d)@?\n";
static const char oom_fmt_1[] PROGMEM STORE_ATTR = ":oom(%d)@";
static const char oom_fmt_2[] PROGMEM STORE_ATTR = ":%d\n";

void* malloc (size_t s)
{
Expand Down
14 changes: 13 additions & 1 deletion cores/esp8266/pgmspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@
#include "ets_sys.h"
#include "osapi.h"

#define PROGMEM ICACHE_RODATA_ATTR
// Since __section__ is supposed to be only use for global variables,
// there could be conflicts when a static/inlined function has them in the
// same file as a non-static PROGMEM object.
// Ref: https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Variable-Attributes.html
// Place each progmem object into its own named section, avoiding conflicts

// The following two macros cause a parameter to be enclosed in quotes
// by the preopressor (i.e. for concatenating ints to strings)
#define __STRINGIZE_NX(A) #A
#define __STRINGIZE(A) __STRINGIZE_NX(A)

#define PROGMEM __attribute__((section( ".irom.text." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__))))

#define PGM_P const char *
#define PGM_VOID_P const void *
#define PSTR(s) (__extension__({static const char __c[] PROGMEM = (s); &__c[0];}))
Expand Down
3 changes: 2 additions & 1 deletion cores/esp8266/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@
*
*/
#include "Arduino.h"
#include <pgmspace.h>
#include "uart.h"
#include "esp8266_peri.h"
#include "user_interface.h"
#include "uart_register.h"

const char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n";
const char overrun_str [] PROGMEM STORE_ATTR = "uart input full!\r\n";
static int s_uart_debug_nr = UART0;


Expand Down
7 changes: 4 additions & 3 deletions cores/esp8266/umm_malloc/umm_malloc_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ extern char _heap_start;
// this must be outside from "#ifndef _UMM_MALLOC_CFG_H"
// because Arduino.h's <cstdlib> does #undef *alloc
// Arduino.h recall us to redefine them
#define malloc(s) ({ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; malloc_loc(s, mem_debug_file, __LINE__); })
#define calloc(n,s) ({ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; calloc_loc(n, s, mem_debug_file, __LINE__); })
#define realloc(p,s) ({ static const char mem_debug_file[] ICACHE_RODATA_ATTR STORE_ATTR = __FILE__; realloc_loc(p, s, mem_debug_file, __LINE__); })
#include <pgmspace.h>
#define malloc(s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; malloc_loc(s, mem_debug_file, __LINE__); })
#define calloc(n,s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; calloc_loc(n, s, mem_debug_file, __LINE__); })
#define realloc(p,s) ({ static const char mem_debug_file[] PROGMEM STORE_ATTR = __FILE__; realloc_loc(p, s, mem_debug_file, __LINE__); })
#endif /* DEBUG_ESP_OOM */
2 changes: 1 addition & 1 deletion libraries/ESP8266WebServer/src/detail/mimetable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace mime
{

// Table of extension->MIME strings stored in PROGMEM, needs to be global due to GCC section typing rules
const Entry mimeTable[maxType] ICACHE_RODATA_ATTR =
const Entry mimeTable[maxType] PROGMEM =
{
{ ".html", "text/html" },
{ ".htm", "text/html" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# Replace your-name-here with somethine appropriate before running and use
# the generated .H files in your code as follows:
#
# static const uint8_t rsakey[] ICACHE_RODATA_ATTR = {
# static const uint8_t rsakey[] PROGMEM = {
# #include "key.h"
# };
#
# static const uint8_t x509[] ICACHE_RODATA_ATTR = {
# static const uint8_t x509[] PROGMEM = {
# #include "x509.h"
# };
#
Expand Down

0 comments on commit 56b98fd

Please sign in to comment.