Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libc/allocator/allocator_simple.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.assume adl=1

.section .text._malloc
.section .text

.global _malloc
.type _malloc, @function
Expand Down
37 changes: 24 additions & 13 deletions src/makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,18 @@ LINKER_SCRIPT ?= $(call FORWARD_PATH,$(CEDEV_TOOLCHAIN)/meta/linker_script.ld)
# allocator (malloc/realloc/free)
ifeq ($(ALLOCATOR),STANDARD)
LIB_ALLOCATOR = $(call FORWARD_PATH,$(CEDEV_TOOLCHAIN)/lib/libc/allocator_standard.a)
endif
ifeq ($(ALLOCATOR),SIMPLE)
else ifeq ($(ALLOCATOR),SIMPLE)
LIB_ALLOCATOR = $(call FORWARD_PATH,$(CEDEV_TOOLCHAIN)/lib/libc/allocator_simple.a)
else ifeq ($(ALLOCATOR),CUSTOM)
LIB_ALLOCATOR =
else
$(error ALLOCATOR must be one of STANDARD, SIMPLE, or CUSTOM)
endif

ifeq ($(ALLOCATOR),CUSTOM)
CUSTOM_ALLOCATOR_SYMBOLS = --undefined _malloc --undefined _free --undefined _realloc
else
CUSTOM_ALLOCATOR_SYMBOLS =
endif

# ensure always a hexadecimal value
Expand Down Expand Up @@ -391,14 +400,14 @@ $(BINDIR)/$(TARGETOBJ): $(CRT0_OBJ) $(OBJDIR)/$(TARGETTMP) $(MAKEFILE_LIST) $(DE
$(EXTRA_LDFLAGS) \
$(OBJDIR)/$(TARGETTMP) \
$(CRT0_OBJ) \
$(LIB_ALLOCATOR) \
--start-group \
$(LIB_SOFTFLOAT) \
$(LIB_ALLOCATOR) \
$(LIB_CRT) \
--end-group \
$(LIB_CE) \
$(LIB_SOFTFLOAT) \
$(LIB_C) \
$(LIB_CXX) \
$(LIB_CE) \
--end-group \
-o $(call QUOTE_ARG,$@)

ifneq ($(ICON_SRC),)
Expand Down Expand Up @@ -481,7 +490,7 @@ $(OBJDIR)/%.$(CPP_EXTENSION).bc: $$(call UPDIR_RM,$$*).$(CPP_EXTENSION) $(EXTRA_
$(Q)$(CC) -MD -c -emit-llvm $(EZCXXFLAGS) $(call QUOTE_ARG,$<) -o $(call QUOTE_ARG,$@)

# crt
$(OBJDIR)/$(TARGETTMP): $(OBJECTS) $(LIB_ALLOCATOR) $(LIB_PRINTF) $(LIB_CXX) $(LIB_CE) $(LIB_SOFTFLOAT) $(LIB_CRT) $(LIB_C) $(ICON_OBJ) $(EXTRA_LIBS) $(MAKEFILE_LIST) $(DEPS)
$(OBJDIR)/$(TARGETTMP): $(OBJECTS) $(LIB_PRINTF) $(LIB_CXX) $(LIB_CE) $(LIB_SOFTFLOAT) $(LIB_CRT) $(LIB_C) $(ICON_OBJ) $(EXTRA_LIBS) $(MAKEFILE_LIST) $(DEPS)
$(Q)$(call MKDIR,$(@D))
$(Q)$(LD) \
-i \
Expand All @@ -491,19 +500,21 @@ $(OBJDIR)/$(TARGETTMP): $(OBJECTS) $(LIB_ALLOCATOR) $(LIB_PRINTF) $(LIB_CXX) $(L
--gc-sections \
--omagic \
--defsym __TICE__=1 \
$(CUSTOM_ALLOCATOR_SYMBOLS) \
$(SPRINTF_SYMBOL) \
$(LD_DEBUG) \
$(EXTRA_PRE_LDFLAGS) \
$(OBJECTS) \
$(ICON_OBJ) \
$(LIB_ALLOCATOR) \
$(LIB_PRINTF) \
$(LIB_CXX) \
$(LIB_CE) \
$(EXTRA_LIBS) \
--whole-archive $(LIB_PRINTF) --no-whole-archive \
--start-group \
$(LIB_CRT) \
$(LIB_C) \
$(LIB_CE) \
$(LIB_SOFTFLOAT) \
$(EXTRA_LIBS) \
$(LIB_C) \
$(LIB_CXX) \
--end-group \
-o $(call QUOTE_ARG,$@)
$(Q)$(STRIP_CMD) $(call QUOTE_ARG,$@)

Expand Down
39 changes: 39 additions & 0 deletions test/crt/terminate/autotest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"transfer_files": [
"bin/DEMO.8xp"
],
"target": {
"name": "DEMO",
"isASM": true
},
"sequence": [
"action|launch",
"delay|1000",
"hashWait|1",
"key|enter",
"delay|300",
"hashWait|2"
],
"hashes": {
"1": {
"description": "before std::terminate",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"0F1A678D"
]
},
"2": {
"description": "after std::terminate",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"FFAF89BA",
"101734A5",
"9DA19F44",
"A32840C8",
"349F4775"
]
}
}
}
19 changes: 19 additions & 0 deletions test/crt/terminate/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ----------------------------
# Makefile Options
# ----------------------------

NAME = DEMO
ICON = icon.png
DESCRIPTION = "CE C Toolchain Demo"
COMPRESSED = NO
ARCHIVED = NO

CFLAGS = -Wall -Wextra -Wshadow -Oz
CXXFLAGS = -Wall -Wextra -Wshadow -Oz

PREFER_OS_LIBC = NO
PREFER_OS_CRT = NO

# ----------------------------

include $(shell cedev-config --makefile)
11 changes: 11 additions & 0 deletions test/crt/terminate/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <ti/screen.h>
#include <ti/getcsc.h>
#include <exception>

int main(void) {
os_ClrHome();
os_PutStrFull("before std::terminate");
while (!os_GetCSC());

std::terminate();
}
61 changes: 61 additions & 0 deletions test/standalone/custom_allocator/autotest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"transfer_files": [
"bin/DEMO.8xp"
],
"target": {
"name": "DEMO",
"isASM": true
},
"sequence": [
"action|launch",
"delay|1000",
"hashWait|1",
"key|enter",
"delay|400",
"hashWait|2",
"key|enter",
"delay|400",
"hashWait|3",
"key|enter",
"delay|400",
"hashWait|4"
],
"hashes": {
"1": {
"description": "calloc should clear the memory from custom malloc",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"8CC9FD0C"
]
},
"2": {
"description": "we expect malloc to be called twice (for calloc and atexit)",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"C1BC5A5C"
]
},
"3": {
"description": "custom free should print a custom message",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"BD82EEA3"
]
},
"4": {
"description": "Exit",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"FFAF89BA",
"101734A5",
"9DA19F44",
"A32840C8",
"349F4775"
]
}
}
}
21 changes: 21 additions & 0 deletions test/standalone/custom_allocator/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ----------------------------
# Makefile Options
# ----------------------------

NAME = DEMO
ICON = icon.png
DESCRIPTION = "CE C Toolchain Demo"
COMPRESSED = NO
ARCHIVED = NO

CFLAGS = -Wall -Wextra -Wshadow -Oz
CXXFLAGS = -Wall -Wextra -Wshadow -Oz

PREFER_OS_LIBC = NO
PREFER_OS_CRT = NO
HAS_PRINTF = NO
ALLOCATOR = CUSTOM

# ----------------------------

include $(shell cedev-config --makefile)
23 changes: 23 additions & 0 deletions test/standalone/custom_allocator/src/custom_free.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.assume adl=1

.section .rodata._free_string
.local _free_string
_free_string:
.asciz "called free"

.section .text._free
.global _free
.type _free, @function
_free:
ld hl, _free_string
push hl
call _puts
pop hl
.L.wait_loop:
call _os_GetCSC
or a, a
jr z, .L.wait_loop
ret

.extern _puts
.extern _os_GetCSC
19 changes: 19 additions & 0 deletions test/standalone/custom_allocator/src/custom_malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string.h>
#include <stdlib.h>

static unsigned char arr[4096];

static unsigned char *arr_ptr = &arr[0];

int malloc_calls = 0;

void *malloc(size_t size) {
if (size >= sizeof(arr)) {
return NULL;
}
unsigned char *ret = arr_ptr;
arr_ptr += size;
memset(ret, 0xAA, size);
malloc_calls++;
return ret;
}
62 changes: 62 additions & 0 deletions test/standalone/custom_allocator/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <ti/screen.h>
#include <ti/getcsc.h>
#include <ti/sprintf.h>
#include <stdio.h>
#include <stdlib.h>

extern int malloc_calls;

void atexit_func(void) {
/*
* The custom free function is supposed to print a string if it is called,
* however, it is unspecified if atexit_func or free will be called first.
* This means that we cannot print anything in this function since we
* cannot guarantee the order. However, this function must be non-empty so
* Clang does not optimize away the call to atexit. Now some of this could
* be fixed with -ffreestanding, but that may also hide the assumptions
* that Clang makes.
*/
if (*(const char*)-1 != 0) {
puts("0xFFFFFF should be zero");
while (!os_GetCSC());
}
}

int main(void) {
char buf[50];
os_ClrHome();

// CRT0 may use malloc/free so we cannot assume this is zero
int malloc_calls_before = malloc_calls;

// calloc should call the custom malloc
int *ptr = calloc(1, sizeof(int[3]));

if (ptr == NULL) {
puts("calloc failed");
while (!os_GetCSC());
return 0;
}

for (int n = 0; n < 3; ++n) {
boot_sprintf(buf, "ptr(%d) == %d", n, ptr[n]);
puts(buf);
}

while (!os_GetCSC());

if (atexit(atexit_func) != 0) {
puts("atexit(atexit_func) failed");
while (!os_GetCSC());
return 0;
}

// we expect malloc to be called twice (for calloc and atexit)
int malloc_call_count = malloc_calls - malloc_calls_before;
boot_sprintf(buf, "malloc calls: %d", malloc_call_count);
puts(buf);

while (!os_GetCSC());

exit(EXIT_SUCCESS);
}
39 changes: 39 additions & 0 deletions test/standalone/custom_printf/autotest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"transfer_files": [
"bin/DEMO.8xp"
],
"target": {
"name": "DEMO",
"isASM": true
},
"sequence": [
"action|launch",
"delay|1000",
"hashWait|1",
"key|enter",
"delay|400",
"hashWait|2"
],
"hashes": {
"1": {
"description": "%s should print ALL CAPS with the custom printf",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"F449EF83"
]
},
"2": {
"description": "Exit",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"FFAF89BA",
"101734A5",
"9DA19F44",
"A32840C8",
"349F4775"
]
}
}
}
Loading
Loading