diff --git a/tests/CPP/rtti/Makefile b/tests/CPP/rtti/Makefile new file mode 100644 index 0000000..4b0ce75 --- /dev/null +++ b/tests/CPP/rtti/Makefile @@ -0,0 +1,50 @@ +TARGET = rtti + +TARGET_ARCH = riscv32 + +CC = riscv32-unknown-elf-g++ + +# compiling flags here +CFLAGS = -Wall -I. -O0 -static -march=rv32imac -mabi=ilp32 --specs=nosys.specs + +LINKER = riscv32-unknown-elf-g++ +# linking flags here +LDFLAGS = -I. -static +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.cpp) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) + +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) + $(LINKER) $(CFLAGS) $(LDFLAGS) $(LIBS) $(LIBDIR) $(OBJECTS) -o $@ + riscv32-unknown-elf-objdump -d $@ > dump + riscv32-unknown-elf-objcopy -Oihex $@ $(TARGET).hex + @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp + @echo "Compiling "$<" ..." + $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) *.hex dump + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/tests/CPP/rtti/helper_functions.cpp b/tests/CPP/rtti/helper_functions.cpp new file mode 100644 index 0000000..a137bfc --- /dev/null +++ b/tests/CPP/rtti/helper_functions.cpp @@ -0,0 +1,51 @@ +#include +#include + + +#define TRACE (*(unsigned char *)0x40000000) +extern "C" { + +int _read(int file, char* ptr, int len) { + return 0; +} + +int _open(int fd) { + return 0; +} + +int _close(int fd){ + return 0; +} + +int _fstat_r(int fd) { + return 0; +} + +int _lseek_r(struct _reent *ptr, FILE *fp, long offset, int whence){ + return 0; +} + +int _isatty_r(struct _reent *ptr, int fd) { + return 0; +} + + +int _write(int file, const char *ptr, int len) { + int x; + + for (x = 0; x < len; x++) { + TRACE = *ptr++; + } + + return (len); +} + +int _getpid(void) { + return 0; +} + +void _kill(int pid) { + return; +} + +} diff --git a/tests/CPP/rtti/rtti b/tests/CPP/rtti/rtti new file mode 100755 index 0000000..d3f2e7d Binary files /dev/null and b/tests/CPP/rtti/rtti differ diff --git a/tests/CPP/rtti/rtti.cpp b/tests/CPP/rtti/rtti.cpp new file mode 100644 index 0000000..8b92611 --- /dev/null +++ b/tests/CPP/rtti/rtti.cpp @@ -0,0 +1,25 @@ +/* + * RTTI example from https://www.geeksforgeeks.org/g-fact-33/ + */ + +#include +using namespace std; +class B { virtual void fun() {} }; +class D: public B { }; + +int main() +{ + B *b = new D; + D *d = dynamic_cast(b); + if(d != NULL) + cout << "works" << endl; + else + cout << "cannot cast B* to D*" << endl; + + + asm volatile ("ecall"); + + return 0; + + +}