Skip to content

Commit 7785949

Browse files
authored
Merge pull request #263 from Vogtinator/newliboptimization
Some newlib-related optimizations
2 parents 052f0ac + f435971 commit 7785949

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

ndless-sdk/libsyscalls/stdlib.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <sys/stat.h>
99
#include <sys/time.h>
1010
#include <sys/times.h>
11+
#include <sys/reent.h>
1112
#include <limits.h>
1213
#include <nucleus.h>
1314
#include <libndls.h>
@@ -24,6 +25,10 @@ extern int errno;
2425
#include <syscall-list.h>
2526
#include "syscall.h"
2627

28+
namespace __gnu_cxx {
29+
extern __attribute__((weak)) void __freeres();
30+
}
31+
2732
extern "C" {
2833

2934
// The only macro, I swear
@@ -197,6 +202,17 @@ void _exit(int ret)
197202
nio_free(&csl);
198203
#endif
199204

205+
// Free memory allocated by libstdc++
206+
if(__gnu_cxx::__freeres)
207+
__gnu_cxx::__freeres();
208+
209+
// Newlib doesn't reclaim data from the statically allocated reent
210+
// itself, so do it here. It needs a bit of "convincing".
211+
// See https://sourceware.org/pipermail/newlib/2020/018173.html
212+
struct _reent *global_reent = _impure_ptr;
213+
_impure_ptr = nullptr;
214+
_reclaim_reent(global_reent);
215+
200216
__crt0_exit(ret);
201217

202218
__builtin_unreachable();

ndless-sdk/system/crt0.S

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ _start: .global _start
55
str sp, __crt0_savedsp
66
push {r0, r1}
77
bl initialise_monitor_handles /* Grab stdin, -out and -err, required for newlib */
8-
bl _init_signal /* Required for newlibs atexit */
98
bl __cpp_init /* C++ static initializers */
109
pop {r0, r1}
1110
bl main

ndless-sdk/system/crtn.S

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
exit: .weak exit /* If linked without newlib, used by crt0. Can't be defined in crt0, as newlibs exit is also weak */
77
bl __crt0_exit
88

9-
_init_signal: .weak _init_signal /* Same here */
10-
bx lr
11-
129
.section .init_array
1310
.long -1 /* Terminating character for ctor list */
1411

0 commit comments

Comments
 (0)