cpp rtti examples

This commit is contained in:
Màrius Montón 2020-06-11 10:23:35 +02:00
parent 03a228b020
commit 3cfbdcf6b8
4 changed files with 126 additions and 0 deletions

50
tests/CPP/rtti/Makefile Normal file
View File

@ -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!"

View File

@ -0,0 +1,51 @@
#include <string.h>
#include <stdio.h>
#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;
}
}

BIN
tests/CPP/rtti/rtti Executable file

Binary file not shown.

25
tests/CPP/rtti/rtti.cpp Normal file
View File

@ -0,0 +1,25 @@
/*
* RTTI example from https://www.geeksforgeeks.org/g-fact-33/
*/
#include<iostream>
using namespace std;
class B { virtual void fun() {} };
class D: public B { };
int main()
{
B *b = new D;
D *d = dynamic_cast<D*>(b);
if(d != NULL)
cout << "works" << endl;
else
cout << "cannot cast B* to D*" << endl;
asm volatile ("ecall");
return 0;
}