Skip to content

Commit

Permalink
Upgrade to GCC 10.1 toolchain (#6294)
Browse files Browse the repository at this point in the history
* Upgrade to GCC 9.1 toolchain

* Rebuilt using pure GNU binutils and GCC

Remove dependencies on earlier forked GNU utilities (gcc-xtensa,
binutils-gdb-xtensa) and just use GCC sources, unmodified (except for
patches in the esp-quick-toolchain directories).

* Rebuild bearssl using new toolchain

* Fix GDBstub linkage options

GDB works with pure GNU GCC and pure GNU binutils now.  Still warnings
galore, but tested with the example sketch in the docs.

* Fix digitalRead alias warning

* Remove gdb stub warnings w/a pragma

* Fix deprecated implicit copy ctors in IP code

Fix some warnings present in GCC8/9 in the IPAddress code

In AddressListIterator there was a copy constructor which simply copied
the structure bit-for-bit.  That's the default operation, so remove it
to avoid the warning there.

IPAddress, add a default copy constructor since the other copy
constructors are simply parsing from one format into a native ip_addr_t.

@d-a-v, can you give these a look over and see if they're good (since
IP stuff is really your domain).

* Fix AxTLS alias function defs to match real code

* Fix WiFiClientSecure implicit default copy ctor

These both use shared-ptrs to handle refcnts to allocated data, so using
the default copy constructor is fine (and has been in use for a long
time).

* Dummy size for heap to avoid GCC 8/9 warnings

Make GCC think _heap_start is large enough to avoid the basic (and
incorrect) bounds-checking warnings it produces.  The size chosen is
arbitrary and does not affect the actual size of the heap in any way.

* Make heap an undefined extend array

Instead of a bogus size, use an indefinite size for the heap to avoid
GCC warnings

* Trivial tab to space fix

* Update SDFat to remove FatFile warnings

* Fix ticker function cast warnings in GCC 9

The callback function is defined to take a (void*) as parameter, but our
templates let users use anything that fits inside sizeof(void*) to be
passed in.  Add pragmas to stop GCC warnings about this, since we
already check the size of the type will fit in the allocated space.

* Remove GCC support fcn that's in ROM

Manually delete the divdi3.so from the libgcc.a library by running the
updated EQT's 9.1-post script.

* Make exceptions work again, get std::regex up

Exceptions are broken on all builds (GCC4.8-9.1) due to the removal of
the PROGMEM non-32b read exception handler (added in the unstable
pre3.0.0).

Build the exception code with -mforce-l32 and patch
accordingly to avoid LoadStore errors.

Apply patches to select portions of the regex lib which use _stype_
(which is now in flash).

* Rebuild Bearssl using latest GCC push

* Automate building of BearSSL and LWIP w/new toolchain

* Workaround g++ template section problem for exception strings

G++ seems to throw out the section attributes for templates.  This means
that the __EXCSTR(a synonym for "PSTR()") is ignored and exception.what
strings are stored in RODATA, eating up RAM.

Workaround by using the linker to place the strings keying off their name
("*__exception_what__*").

* Rebuild moving exception.what to unique names

Exception.whats are now all in __exception_what__ and can be moved by
the linker to flash.  Works aroung G++ issue with segments being lost in
templates.

* Rebuild with new LWIP locking

* Update to latest libs, save iram

Move two GCC FP support routines out of iram since they are in ROM
already, saving some add'l IRAM.  Same list as gcc 4.8.

* Update BearSSL to latest release

* Fix umm_perf reference to ROM function

* Fix "reinterpret_case is not a constexpr" error

In GCC 9 (and 8 from what I read on SO), a cast of a const int to a
function pointer (via explicit or implicit reinterpret_cast) is not a
constexpr.
````
/home/earle/Arduino/hardware/esp8266com/esp8266/cores/esp8266/umm_malloc/umm_performance.cpp:45:36: error: a reinterpret_cast is not a constant expression
   45 | int constexpr (*_rom_putc1)(int) = (int (*)(int))(void*)0x40001dcc;
      |                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
````

Remove the constexpr, potentially increasing heap usage by 4 bytes in
debug mode.

* Update libc.a to latest rev

* Full rebuild of toolchain and libs

* Upgrade to GCC 9.2, released in August 2019

Add builds for all 5 cross-compiles to pass CI

* Move to --std=gnu++14 (C++14 + GNU extensions)

* Fix Ticker merge conflict

* Fix json merge conflict

* One more merge diff fix

* Reapply Ticker.h casting warning fixes for gcc9

* Update with fixes from Sming repo for PSTR and ASM

* Upgrade to -gnu4 toolchain

* Move to gnu5 build with add'l softFP from ROM

* Move add'l softFP from IRAM to flash

Per @mikee47, we miss sone add'l soft-FP routined in the linker which
makes them end up in IRAM.  Move them explicitly into flash, like a
couple others we have already done this for.

* Move to std=c++17/c17 in platform, remove abs/round

Move to C++17 and C17 standards on the compiler options.

Remove "register" from core since it is deprecated.

Remove the #define abs() and #define round() which a) overwrote the
C stdlib definitions, poorly, and b) broke the GCC core code which used
"abs" as an internal function name.

Now abs() returns an integer, and not whatever was being absoluted.
fabs() etc. can be used if users need fload/double suport.

round() returns a double now, which is basically what it was returning
in the original case since adding/subtracting by a FP.

* Use std::abs/round to replace the macro definitions

Per discussion w/@devyte, preserve the abs() and round() functionality
via the using statement.

* Remove using std::abs which conflicted with C lib headers

* Add 2nd arg (exception handler) to ets_isr_t

Disassembly of the ROM shows there are 2 params to the ets_isr_t
callback.  The first is the arg passed in, the second is a pointer to an
exception frame where you can get info about when the IRQ happened.

* Move the gdbstub example to a subdir

The Arduino IDE and the build CI don't build it without a subdir, so
make one for gdbstub's example so it's visible and tested.

* Fix ets_irq_arratch redefinition and core IRQ handlers

Remove a duplicated, different declaration for ets_irq_attach from
ets_sys.h.  It never really even matched the other declaration in the
same header.

Update the core to IRQ handlers to fix the prototype and include the
2nd, unused frame parameter.

* Actually rebuild the libc.a using GCC 9.2

* Fix SPISlave interrupt attach's 2nd parameter

* Rebuild eboot.elf with GCC 9

* Update to latest SoftwareSerial for Delegate fix

* Upgrade to GCC 9.3

* Rebuild all arch toolchains

* Move to GCC 10.1

* Merge master and fix eboot build

GCC10 now uses `-fno-common` so the eboot global variables were being
placed in IRAM.  Adjust the makefile and rebuild to fix.

* Built complete toolchain for all archs

* Pull in latest PSTR changes and fix GCC10.1 build

Somehow the prior GCC build's -mforce32 patch wasn't applying correctly,
but I was still able to get a binary.  Fixed.  Also pulled in latest
PSTR changes in progmem.h

* Update platform.io to platform C/C++ standards

* Use PR's toolchain in platformio build

* Fix several asm warnings in PIO build

* Optional stack smash protection -fstack-protector

Add a menu to enable GCC's built-in stack smash protection.  When a
subroutine goes past its end of stack, generate a crashdump on function
exit like:

````
GCC detected stack overrun
Stack corrupted, stack smash detected.

>>>stack>>>

ctx: cont
sp: 3fffff20 end: 3fffffc0 offset: 0000
3fffff20:  40202955 00000001 0000001c 4020287e
3fffff30:  feefeffe 000000fd 00000000 00000000
...
<<<stack<<<
````

Disabled by default because there is a small per-function code overhead
(and CPU time if the function is called very frequently and is very
small).

BearSSL and LWIP are not built using stack smash detection, yet.

* Fix duplicated stc=gnu99/c17 in build

* Dump faulting function PC in stack overflow

Report a fake exception to have the exception decoder print the actual
faulting function.  This won't tell you where in the function the issue
happened, but it will tell you the function name first and foremost.

* Rebuild with Platform.io JSON tag in release tgzs
  • Loading branch information
earlephilhower authored Jul 7, 2020
1 parent 7b48b9d commit d979b57
Show file tree
Hide file tree
Showing 44 changed files with 337 additions and 144 deletions.
137 changes: 137 additions & 0 deletions boards.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bootloaders/eboot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ INC += -I../../tools/sdk/include -I../../tools/sdk/uzlib/src

CFLAGS += -std=gnu99

CFLAGS += -Os -g -Wall -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mno-text-section-literals -ffunction-sections -fdata-sections
CFLAGS += -Os -fcommon -g -Wall -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mno-text-section-literals -ffunction-sections -fdata-sections

CFLAGS += $(INC)

Expand Down
Binary file modified bootloaders/eboot/eboot.elf
Binary file not shown.
9 changes: 2 additions & 7 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,8 @@ void timer0_isr_init(void);
void timer0_attachInterrupt(timercallback userFunc);
void timer0_detachInterrupt(void);

// undefine stdlib's abs if encountered
#ifdef abs
#undef abs
#endif

#define abs(x) ((x)>0?(x):-(x))
// Use stdlib abs() and round() to avoid issues with the C++ libraries
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
#define sq(x) ((x)*(x))
Expand Down Expand Up @@ -249,6 +243,7 @@ const int TIM_DIV265 __attribute__((deprecated, weak)) = TIM_DIV256;

using std::min;
using std::max;
using std::round;
using std::isinf;
using std::isnan;

Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/StackThunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern uint32_t stack_thunk_refcnt;

// Thunking macro
#define make_stack_thunk(fcnToThunk) \
__asm("\n\
__asm__ ("\n\
.text\n\
.literal_position\n\
.literal .LC_STACK_VALUE"#fcnToThunk", 0xdeadbeef\n\
Expand Down
5 changes: 3 additions & 2 deletions cores/esp8266/core_esp8266_features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ void precache(void *f, uint32_t bytes) {
// page (ie 1 word in 8) for this to work.
#define CACHE_PAGE_SIZE 32

register uint32_t a0 asm("a0");
register uint32_t lines = (bytes/CACHE_PAGE_SIZE)+2;
uint32_t a0;
__asm__("mov.n %0, a0" : "=r"(a0));
uint32_t lines = (bytes/CACHE_PAGE_SIZE)+2;
volatile uint32_t *p = (uint32_t*)((f ? (uint32_t)f : a0) & ~0x03);
uint32_t x;
for (uint32_t i=0; i<lines; i++, p+=CACHE_PAGE_SIZE/sizeof(uint32_t)) x=*p;
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ extern "C" bool ets_post_rom(uint8 prio, ETSSignal sig, ETSParam par);

extern "C" bool IRAM_ATTR ets_post(uint8 prio, ETSSignal sig, ETSParam par) {
uint32_t saved;
asm volatile ("rsr %0,ps":"=a" (saved));
__asm__ __volatile__ ("rsr %0,ps":"=a" (saved));
bool rc=ets_post_rom(prio, sig, par);
xt_wsr_ps(saved);
return rc;
Expand Down
24 changes: 24 additions & 0 deletions cores/esp8266/core_esp8266_postmortem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ static const char* s_panic_what = 0;
static bool s_abort_called = false;
static const char* s_unhandled_exception = NULL;

static uint32_t s_stacksmash_addr = 0;

void abort() __attribute__((noreturn));
static void uart_write_char_d(char c);
static void uart0_write_char_d(char c);
Expand All @@ -54,6 +56,7 @@ static void print_stack(uint32_t start, uint32_t end);
// using numbers different from "REASON_" in user_interface.h (=0..6)
enum rst_reason_sw
{
REASON_USER_STACK_SMASH = 253,
REASON_USER_SWEXCEPTION_RST = 254
};
static int s_user_reset_reason = REASON_DEFAULT_RST;
Expand Down Expand Up @@ -147,6 +150,11 @@ void __wrap_system_restart_local() {
else if (rst_info.reason == REASON_SOFT_WDT_RST) {
ets_printf_P(PSTR("\nSoft WDT reset\n"));
}
else if (rst_info.reason == REASON_USER_STACK_SMASH) {
ets_printf_P(PSTR("\nStack overflow detected.\n"));
ets_printf_P(PSTR("\nException (%d):\nepc1=0x%08x epc2=0x%08x epc3=0x%08x excvaddr=0x%08x depc=0x%08x\n"),
5 /* Alloca exception, closest thing to stack fault*/, s_stacksmash_addr, 0, 0, 0, 0);
}
else {
ets_printf_P(PSTR("\nGeneric Reset\n"));
}
Expand Down Expand Up @@ -290,4 +298,20 @@ void __panic_func(const char* file, int line, const char* func) {
raise_exception();
}

uintptr_t __stack_chk_guard = 0x08675309 ^ RANDOM_REG32;
void __stack_chk_fail(void) {
s_user_reset_reason = REASON_USER_STACK_SMASH;
ets_printf_P(PSTR("\nPANIC: Stack overrun"));

s_stacksmash_addr = (uint32_t)__builtin_return_address(0);

if (gdb_present())
__asm__ __volatile__ ("syscall"); // triggers GDB when enabled

__wrap_system_restart_local();

while (1); // never reached, needed to satisfy "noreturn" attribute
}


};
2 changes: 1 addition & 1 deletion cores/esp8266/core_esp8266_si2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void ICACHE_RAM_ATTR Twi::busywait(unsigned int v)
unsigned int i;
for (i = 0; i < v; i++) // loop time is 5 machine cycles: 31.25ns @ 160MHz, 62.5ns @ 80MHz
{
asm("nop"); // minimum element to keep GCC from optimizing this function out.
__asm__ __volatile__("nop"); // minimum element to keep GCC from optimizing this function out.
}
}

Expand Down
6 changes: 4 additions & 2 deletions cores/esp8266/core_esp8266_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ extern "C" {

static volatile timercallback timer1_user_cb = NULL;

void ICACHE_RAM_ATTR timer1_isr_handler(void *para){
void ICACHE_RAM_ATTR timer1_isr_handler(void *para, void *frame) {
(void) para;
(void) frame;
if ((T1C & ((1 << TCAR) | (1 << TCIT))) == 0) TEIE &= ~TEIE1;//edge int disable
T1I = 0;
if (timer1_user_cb) {
Expand Down Expand Up @@ -79,8 +80,9 @@ void ICACHE_RAM_ATTR timer1_disable(){

static volatile timercallback timer0_user_cb = NULL;

void ICACHE_RAM_ATTR timer0_isr_handler(void* para){
void ICACHE_RAM_ATTR timer0_isr_handler(void *para, void *frame) {
(void) para;
(void) frame;
if (timer0_user_cb) {
// to make ISR compatible to Arduino AVR model where interrupts are disabled
// we disable them before we call the client ISR
Expand Down
4 changes: 3 additions & 1 deletion cores/esp8266/core_esp8266_wiring_digital.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ typedef struct {
static interrupt_handler_t interrupt_handlers[16] = { {0, 0, 0, 0}, };
static uint32_t interrupt_reg = 0;

void ICACHE_RAM_ATTR interrupt_handler(void*)
void ICACHE_RAM_ATTR interrupt_handler(void *arg, void *frame)
{
(void) arg;
(void) frame;
uint32_t status = GPIE;
GPIEC = status;//clear them interrupts
uint32_t levels = GPI;
Expand Down
6 changes: 6 additions & 0 deletions cores/esp8266/gdb_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ static bool ICACHE_RAM_ATTR __gdb_no_op()
return false;
}

// To save space, don't create a dummy no-op for each GCC, just point to the no-op
// Need to turn off GCC's checking of parameter types or we'll get many warnings
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattribute-alias"
#pragma GCC diagnostic ignored "-Wmissing-attributes"
void gdb_init(void) __attribute__ ((weak, alias("__gdb_no_op")));
void gdb_do_break(void) __attribute__ ((weak, alias("__gdb_no_op")));
bool gdb_present(void) __attribute__ ((weak, alias("__gdb_no_op")));
Expand All @@ -40,5 +45,6 @@ bool gdbstub_has_uart_isr_control(void) __attribute__ ((weak, alias("__gdb_no_op
void gdbstub_set_uart_isr_callback(void (*func)(void*, uint8_t), void* arg) __attribute__ ((weak, alias("__gdb_no_op")));
void gdbstub_write_char(char c) __attribute__ ((weak, alias("__gdb_no_op")));
void gdbstub_write(const char* buf, size_t size) __attribute__ ((weak, alias("__gdb_no_op")));
#pragma GCC diagnostic pop

};
3 changes: 2 additions & 1 deletion cores/esp8266/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,9 @@ uart_get_rx_buffer_size(uart_t* uart)

// The default ISR handler called when GDB is not enabled
void ICACHE_RAM_ATTR
uart_isr(void * arg)
uart_isr(void * arg, void * frame)
{
(void) frame;
uart_t* uart = (uart_t*)arg;
uint32_t usis = USIS(uart->uart_nr);

Expand Down
2 changes: 1 addition & 1 deletion libraries/GDBStub/src/internal/gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ static inline int gdbHandleCommand() {
writeByte(i, gdbGetHexVal(&data, 8));
}
//Make sure caches are up-to-date. Procedure according to Xtensa ISA document, ISYNC inst desc.
asm volatile("ISYNC\nISYNC\n");
__asm__ __volatile__ ("ISYNC\nISYNC\n");
gdbSendPacketOK();
} else {
//Trying to do a software breakpoint on a flash proc, perhaps?
Expand Down
3 changes: 2 additions & 1 deletion libraries/SPISlave/src/hspi_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ static void (*_hspi_slave_rx_status_cb)(void * arg, uint32_t data) = NULL;
static void (*_hspi_slave_tx_status_cb)(void * arg) = NULL;
static uint8_t _hspi_slave_buffer[33];

void ICACHE_RAM_ATTR _hspi_slave_isr_handler(void *arg)
void ICACHE_RAM_ATTR _hspi_slave_isr_handler(void *arg, void *frame)
{
(void) frame;
uint32_t status;
uint32_t istatus;

Expand Down
6 changes: 6 additions & 0 deletions libraries/Ticker/src/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,22 @@ class Ticker
template<typename TArg>
void attach(float seconds, void (*callback)(TArg), TArg arg)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
_attach_ms(1000UL * seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
#pragma GCC diagnostic pop
}

// callback will be called in SYS ctx when ticker fires
template<typename TArg>
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
#pragma GCC diagnostic pop
}

// callback will be called at following loop() after ticker fires
Expand Down
Loading

0 comments on commit d979b57

Please sign in to comment.