Skip to content

Commit 986404a

Browse files
committed
fix: use Hermit's sys malloc and fix uninitialised global data
1 parent 57d1f40 commit 986404a

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

newlib/configure.host

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ case "${host}" in
595595
syscall_dir=syscalls
596596
;;
597597
*-*-hermit*)
598-
newlib_cflags="${newlib_cflags} -DREENTRANT_SYSCALLS_PROVIDED -D__DYNAMIC_REENT__ -DSIGNAL_PROVIDED -DHAVE_NANOSLEEP"
598+
newlib_cflags="${newlib_cflags} -DREENTRANT_SYSCALLS_PROVIDED -D__DYNAMIC_REENT__ -DSIGNAL_PROVIDED -DHAVE_NANOSLEEP -DMALLOC_PROVIDED"
599599
+ ;;
600600
# RTEMS supplies its own versions of some routines:
601601
# malloc() (reentrant version)

newlib/libc/include/sys/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
#endif
138138
#endif
139139

140-
#if defined(__mips__) && !defined(__rtems__)
140+
#if (defined(__mips__) || defined(__hermit__)) && !defined(__rtems__)
141141
#define __ATTRIBUTE_IMPURE_PTR__ __attribute__((__section__(".sdata")))
142142
#endif
143143

newlib/libc/sys/hermit/crt0.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,74 @@
2828
#include <stdlib.h>
2929
#include <unistd.h>
3030
#include <getopt.h>
31+
#include <reent.h>
32+
#include "syscall.h"
3133

3234
extern int main(int argc, char** argv);
3335
extern void __libc_init_array(void);
3436
extern void __libc_fini_array (void);
3537
extern int _init_signal(void);
3638
extern char** environ;
39+
extern void __sinit (struct _reent *);
40+
extern struct _glue __sglue;
41+
extern __FILE __sf[3];
42+
43+
typedef struct layout {
44+
size_t size;
45+
} layout_t;
46+
47+
#define HERMIT_MALLOC_ALIGN ((size_t)8)
48+
49+
void* malloc(size_t s) {
50+
return _malloc_r(_REENT, s);
51+
}
52+
53+
void* _malloc_r(struct _reent* _r, size_t size) {
54+
uint8_t *ptr = sys_malloc(size + sizeof(layout_t), HERMIT_MALLOC_ALIGN);
55+
if (!ptr)
56+
return NULL;
57+
58+
((layout_t*)ptr)->size = size;
59+
return (void*)(ptr + sizeof(layout_t));
60+
}
61+
62+
void* calloc(size_t nmemb, size_t size) {
63+
return _calloc_r(_REENT, nmemb, size);
64+
}
65+
66+
void* _calloc_r(struct _reent* r, size_t nmemb, size_t size) {
67+
return NULL;
68+
}
69+
70+
void* realloc(void* ptr, size_t size) {
71+
return _realloc_r(_REENT, ptr, size);
72+
}
73+
74+
void* _realloc_r(struct _reent* r, void* ptr, size_t size) {
75+
return NULL;
76+
}
77+
78+
void free(void* ptr) {
79+
_free_r(_REENT, ptr);
80+
}
81+
82+
void _free_r(struct _reent* r, void* ptr) {
83+
if (ptr) {
84+
uint8_t* p = (uint8_t*)ptr - sizeof(layout_t);
85+
sys_free(p, ((layout_t*)p)->size + sizeof(layout_t), HERMIT_MALLOC_ALIGN);
86+
}
87+
}
3788

3889
void runtime_entry(int argc, char** argv, char** env)
3990
{
4091
int ret;
4192

93+
// For some reason, the newlib symbol for _impure_ptr is NULL.
94+
_impure_ptr = &_impure_data;
95+
_REENT_INIT_PTR(_impure_ptr);
96+
__sglue = (struct _glue){ NULL, 3, &__sf[0] };
97+
98+
4299
/* call init function */
43100
__libc_init_array();
44101

newlib/libc/sys/hermit/syscall.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ int sys_kill(tid_t dest, int signum);
146146
int sys_signal(signal_handler_t handler);
147147
unsigned int sys_rand();
148148

149+
uint8_t *sys_malloc(size_t size, size_t align);
150+
uint8_t *sys_alloc(size_t size, size_t align);
151+
uint8_t *sys_realloc(uint8_t *ptr, size_t size, size_t align, size_t new_size);
152+
void sys_free(uint8_t *ptr, size_t size, size_t align);
153+
149154
struct ucontext;
150155
typedef struct ucontext ucontext_t;
151156

0 commit comments

Comments
 (0)