Skip to content

Commit 6280e98

Browse files
earlephilhowerdevyte
authored andcommitted
Enable exceptions, update to optimized newlib, migrate to new toolchain (#5376)
* Move to PROGMEM aware libc, allow PSTR in printf() A Newlib (libc) patch is in progress to move the _P functions from inside Arduino into first-class citizens in libc. This Arduino patch cleans up code that's been migrated there. Binaries for the new libs are included because it seems they're part of the Arduino git tree, and should be replaced with @igrr built ones when/if the Newlib changes are accepted. Notable changes/additions for Arduino: Allow for use of PROGMEM based format and parameter strings in all *printf functions. No need for copying PSTR()s into RAM before printing them out (transparently saves heap space when using _P functions) and makes it easier to print out constant strings for applications. Add "%S" (capital-S) format that I've been told, but cannot verify, is used in Arduino to specify a PROGMEM string parameter in printfs, as an alias for "%s" since plain "%s" can now handle PROGMEM. Optimized the memcpy_P, strnlen_P, and strncpy_P functions to use 32-bit direct reads whenver possible (source and dest alignment mediated), but there is still room for improvement in others. Finally, move several constant arrays from RODATA into PROGMEM and update their accessors. Among these are the ctype array, ~260 bytes, mprec* arrays, ~300 bytes, and strings/daycounts in the time formatting functions, ~200 bytes. All told, sketches will see from 300 to 800 additional RAM heap free on startup (depending on their use of these routines). * Fix merge error in #ifdef/#endif * Fix host test using the newlib generic pgmspace.h Host tests now use the sys/pgmspace.h for compiles instead of the ESP8266-specific version. * Update with rebuilt libraries using latest newlib * Include binaries built directly from @igrr repo Rebuild the binaries using a git clone of https://github.com/igrr/newlib-xtensa Build commands for posterity: ```` rm -rf ./xtensa-lx106-elf/ ./configure --prefix=<DIR>/esp8266/tools/sdk/libc --with-newlib \ --enable-multilib --disable-newlib-io-c99-formats \ --disable-newlib-supplied-syscalls \ --enable-newlib-nano-formatted-io --enable-newlib-reent-small \ --enable-target-optspace \ --program-transform-name="s&^&xtensa-lx106-elf-&" \ --disable-option-checking --with-target-subdir=xtensa-lx106-elf \ --target=xtensa-lx106-elf rm -f etc/config.cache CROSS_CFLAGS="-fno-omit-frame-pointer -DSIGNAL_PROVIDED -DABORT_PROVIDED"\ " -DMALLOC_PROVIDED" \ PATH=<DIR>/esp8266/tools/xtensa-lx106-elf/bin/:$PATH \ make all install ```` * Fix merge define conflict in c_types.h * Fix strlen_P misaligned source error Include fix from newlib-xtensa/fix-strlen branch cleaning up misaligned access on a non-aligned source string. * Fix strlen_P and strcpy_P edge cases Ran the included test suite on ESP8266 tstring.c with the following defines: #define MAX_1 50 #define memcmp memcmp_P #define memcpy memcpy_P #define memmem memmem_P #define memchr memchr_P #define strcat strcat_P #define strncat strncat_P #define strcpy strcpy_P #define strlen strlen_P #define strnlen strnlen_P #define strcmp strcmp_P #define strncmp strncmp_P Uncovered edge case and return value problems in the optimized versions of the strnlen_P and strncpy_P functions. Corrected. * Fix memcpy_P return value memcpy-1.c test suite showed error in return value of memcpy_P. Correct it. * Fix strnlen_P/strlen_P off-by-4 error Random crashes, often on String constructors using a PSTR, would occur due to the accelerated strnlen_P going past the end of the string. Would make debug builds fail, too (ESP.getVersionString() failure). Fix to fall through to normal copy on a word that's got a 0 byte anywhere in it. * Add device tests for libc functional verification Add test suite used to debug libc optimized _P functions to the device tests. * Rebuild from igrr's repo (same source as prior) Rebuild .a from igrr's repo at 347260af117b4177389e69fd4d04169b11d87a97 * WIP - add exceptions * Fix exception to have 0-terminator * Move some exception constants to TEXT from RODATA * Remove throw stubs * Move more exception stuff to ROM * Enable exceptions in platform.io * Remove atexit, is duplicated in rebuilt lib Need to look at the quick-toolchain options, there seems to be a definition for atexit defined there (libgcc?) that needs to be excised. For now, remove our local do-nothing copy. * Update libgcc to remove soft-fp functions The esp-quick-toolchain generated libgcc.a needed to have the soft-FP routines that are in ROM removed from it. Remove them in the new esp-quick-toolchain and update. * Fix merge typos in Makefile * Add unhandled exception handler to postmortem * Return our atexit() handler * Latest stdc++, minimize exception emercengy area * Remove atexit from newlib atexit was defined in newlib strongly, but we also define a noop atexit in core. Since we never exit, use the core's noop and delete the atexit from libc.a Updated in esp-quick-toolchain as well. * Move __FUNCTION__ static strings to PROGMEM __FUNCTION__ is unlikely to be a timing sensitive variable, so move it to PROGMEM and not RODATA (RAM) using linker magic. asserts() now should take no RAM for any strings. * Clean up linker file, update to latest stdc++ * Update to latest stdc++ which doesn't call strerror * Update to GCC5.1 exception emergency allocator Using GCC 5.1's emergency memory allocator for exceptions, much less space is required in programs which do not use exceptions and when space is allocated it is managed more efficiently. * Initial try with new compiler toolchain * Include newlib built from esp-quick-toolchain * Update JSON with all new esp-quick-toolchain builds * Use 64bit Windows compiler on 64bit Windows * Dump std::exception.what() when possible When doing the panic on unhandled exceptions, try and grab the .what() pointer and dump it as part of the termination info. Makes it easy to see mem errors (std::bad_alloc) or std::runtime_error strings. * Use scripted install from esp-quick-toolchain Makes sure proper libraries and includes are present by using a scripted installation from esp-quick-install instead of a manual one. * Update eqk to remove atexit, fix packaging diff
1 parent 4941711 commit 6280e98

37 files changed

+2279
-817
lines changed

cores/esp8266/Arduino.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ const int TIM_DIV265 __attribute__((deprecated, weak)) = TIM_DIV256;
254254
#ifdef __cplusplus
255255

256256
#include <algorithm>
257-
#include "pgmspace.h"
257+
#include <pgmspace.h>
258258

259259
#include "WCharacter.h"
260260
#include "WString.h"

cores/esp8266/abi.cpp

-78
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,6 @@ using __cxxabiv1::__guard;
2828
extern void *umm_last_fail_alloc_addr;
2929
extern int umm_last_fail_alloc_size;
3030

31-
void *operator new(size_t size)
32-
{
33-
void *ret = malloc(size);
34-
if (0 != size && 0 == ret) {
35-
umm_last_fail_alloc_addr = __builtin_return_address(0);
36-
umm_last_fail_alloc_size = size;
37-
}
38-
return ret;
39-
}
40-
41-
void *operator new[](size_t size)
42-
{
43-
void *ret = malloc(size);
44-
if (0 != size && 0 == ret) {
45-
umm_last_fail_alloc_addr = __builtin_return_address(0);
46-
umm_last_fail_alloc_size = size;
47-
}
48-
return ret;
49-
}
50-
51-
void operator delete(void * ptr)
52-
{
53-
free(ptr);
54-
}
55-
56-
void operator delete[](void * ptr)
57-
{
58-
free(ptr);
59-
}
60-
6131
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
6232
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
6333

@@ -98,53 +68,5 @@ extern "C" void __cxa_guard_abort(__guard* pg)
9868
xt_wsr_ps(reinterpret_cast<guard_t*>(pg)->ps);
9969
}
10070

101-
102-
namespace std
103-
{
104-
void __throw_bad_function_call()
105-
{
106-
panic();
107-
}
108-
109-
void __throw_length_error(char const*)
110-
{
111-
panic();
112-
}
113-
114-
void __throw_bad_alloc()
115-
{
116-
panic();
117-
}
118-
119-
void __throw_logic_error(const char* str)
120-
{
121-
(void) str;
122-
panic();
123-
}
124-
125-
void __throw_out_of_range(const char* str)
126-
{
127-
(void) str;
128-
panic();
129-
}
130-
131-
void __throw_bad_cast(void)
132-
{
133-
panic();
134-
}
135-
136-
void __throw_ios_failure(const char* str)
137-
{
138-
(void) str;
139-
panic();
140-
}
141-
142-
void __throw_runtime_error(const char* str)
143-
{
144-
(void) str;
145-
panic();
146-
}
147-
} // namespace std
148-
14971
// TODO: rebuild windows toolchain to make this unnecessary:
15072
void* __dso_handle;

cores/esp8266/core_esp8266_main.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,47 @@ static void loop_task(os_event_t *events) {
134134
panic();
135135
}
136136
}
137+
extern "C" {
138+
139+
struct object { long placeholder[ 10 ]; };
140+
void __register_frame_info (const void *begin, struct object *ob);
141+
extern char __eh_frame[];
142+
}
137143

138144
static void do_global_ctors(void) {
145+
static struct object ob;
146+
__register_frame_info( __eh_frame, &ob );
147+
139148
void (**p)(void) = &__init_array_end;
140149
while (p != &__init_array_start)
141150
(*--p)();
142151
}
143152

153+
extern "C" {
154+
extern void __unhandled_exception(const char *str);
155+
156+
static void __unhandled_exception_cpp()
157+
{
158+
static bool terminating;
159+
if (terminating)
160+
abort();
161+
terminating = true;
162+
/* Use a trick from vterminate.cc to get any std::exception what() */
163+
try {
164+
__throw_exception_again;
165+
} catch (const std::exception& e) {
166+
__unhandled_exception( e.what() );
167+
} catch (...) {
168+
__unhandled_exception( "" );
169+
}
170+
}
171+
172+
}
173+
144174
void init_done() {
145175
system_set_os_print(1);
146176
gdb_init();
177+
std::set_terminate(__unhandled_exception_cpp);
147178
do_global_ctors();
148179
esp_schedule();
149180
}

cores/esp8266/core_esp8266_postmortem.c

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static const char* s_panic_func = 0;
4040
static const char* s_panic_what = 0;
4141

4242
static bool s_abort_called = false;
43+
static const char* s_unhandled_exception = NULL;
4344

4445
void abort() __attribute__((noreturn));
4546
static void uart_write_char_d(char c);
@@ -119,6 +120,9 @@ void __wrap_system_restart_local() {
119120
}
120121
ets_putc('\n');
121122
}
123+
else if (s_unhandled_exception) {
124+
ets_printf_P("\nUnhandled exception: %s\n", s_unhandled_exception);
125+
}
122126
else if (s_abort_called) {
123127
ets_printf_P("\nAbort called\n");
124128
}
@@ -233,6 +237,11 @@ void abort() {
233237
raise_exception();
234238
}
235239

240+
void __unhandled_exception(const char *str) {
241+
s_unhandled_exception = str;
242+
raise_exception();
243+
}
244+
236245
void __assert_func(const char *file, int line, const char *func, const char *what) {
237246
s_panic_file = file;
238247
s_panic_line = line;

cores/esp8266/libc_replacements.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,4 @@ void _exit(int status) {
128128
int atexit(void (*func)()) {
129129
(void) func;
130130
return 0;
131-
}
131+
}

0 commit comments

Comments
 (0)