diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..71c9880 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.elf \ No newline at end of file diff --git a/makefile b/makefile index c5ab1b6..1f81cfc 100644 --- a/makefile +++ b/makefile @@ -1,16 +1,40 @@ # Compiler and assembler -CC = riscv64-linux-gnu-gcc -AS = riscv64-linux-gnu-as -LD = riscv64-linux-gnu-ld +CROSS=riscv64-linux-gnu +CC = ${CROSS}-gcc +AS = ${CROSS}-as +LD = ${CROSS}-ld +OBJDUMP = ${CROSS}-objdump +SIZE = ${CROSS}-size -# Flags for compiler and assembler -CFLAGS = -static -nostartfiles -nostdlib -ASFLAGS = +# Specify the target binary (call it whatever) +TARGET = bin.elf # QEMU command and flags QEMU = qemu-riscv64-static QEMU_FLAGS = +# Flags for compiler and assembler +CFLAGS += -static# # Use static linking +CFLAGS += -nostartfiles# # Do not use standard startup files +CFLAGS += -nostdlib# # Do not use standard libraries +CFLAGS += -fno-builtin# # Do not use built-in functions +CFLAGS += -fno-common# # Do not use common sections +CFLAGS += -march=rv64i # Use RV64I ISA, i.e., integer only +CFLAGS += -mabi=lp64 # Use LP64 ABI, i.e., 64-bit longs and pointers, 32-bit ints +CFLAGS += -Os# # Optimize for size + +# Use GC=0 to disable garbage collection +ifneq ($(GC), 0) + CFLAGS += -ffreestanding + CFLAGS += -ffunction-sections -fdata-sections + CFLAGS += -Wl,--gc-sections +endif + +# Debugging flags, passed as 'make DEBUG=1 ' +ifeq ($(DEBUG), 1) + CFLAGS += -g -O0 +endif + # Assembly source files AS_SRCS := $(wildcard *.s) @@ -19,17 +43,34 @@ OBJS := $(AS_SRCS:.s=.o) # Default target all: $(OBJS) - $(LD) -o program $(OBJS) -# $(CC) $(CFLAGS) -o program $(OBJS) + @$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) + @echo "LD $(OBJS)" # Compile assembly sources %.o: %.s - $(AS) $(ASFLAGS) -o $@ $< + @$(CC) $(CFLAGS) -c -o $@ $< + @echo "CC $<" -# Run the program in QEMU +# Run the binary in QEMU run: all - $(QEMU) $(QEMU_FLAGS) ./program + $(QEMU) $(QEMU_FLAGS) ./$(TARGET) + +# View the text section of the binary +inspect: all + $(OBJDUMP) -d $(TARGET) + +# View the disassembly +raw: all + $(OBJDUMP) -D $(TARGET) + +# View the data section +data: all + $(OBJDUMP) -s $(TARGET) --section=.data + +# Check the size of the compiled binary +size: all + $(SIZE) $(TARGET) # Clean up clean: - rm -f program $(OBJS) + rm -f $(TARGET) $(OBJS)