1# 2# riscv makefile 3# 4# Authors: Andrew Jones <ajones@ventanamicro.com> 5# 6 7ifeq ($(CONFIG_EFI),y) 8exe = efi 9else 10exe = flat 11endif 12 13tests = 14tests += $(TEST_DIR)/sbi.$(exe) 15tests += $(TEST_DIR)/selftest.$(exe) 16tests += $(TEST_DIR)/sieve.$(exe) 17tests += $(TEST_DIR)/isa-dbltrp.$(exe) 18 19all: $(tests) 20 21$(TEST_DIR)/sbi-deps += $(TEST_DIR)/sbi-asm.o 22$(TEST_DIR)/sbi-deps += $(TEST_DIR)/sbi-dbtr.o 23$(TEST_DIR)/sbi-deps += $(TEST_DIR)/sbi-fwft.o 24$(TEST_DIR)/sbi-deps += $(TEST_DIR)/sbi-sse.o 25 26all_deps += $($(TEST_DIR)/sbi-deps) 27 28# When built for EFI sieve needs extra memory, run with e.g. '-m 256' on QEMU 29$(TEST_DIR)/sieve.$(exe): AUXFLAGS = 0x1 30 31cstart.o = $(TEST_DIR)/cstart.o 32 33cflatobjs += lib/alloc.o 34cflatobjs += lib/alloc_page.o 35cflatobjs += lib/alloc_phys.o 36cflatobjs += lib/devicetree.o 37cflatobjs += lib/memregions.o 38cflatobjs += lib/on-cpus.o 39cflatobjs += lib/vmalloc.o 40cflatobjs += lib/riscv/bitops.o 41cflatobjs += lib/riscv/delay.o 42cflatobjs += lib/riscv/io.o 43cflatobjs += lib/riscv/isa.o 44cflatobjs += lib/riscv/mmu.o 45cflatobjs += lib/riscv/processor.o 46cflatobjs += lib/riscv/sbi.o 47cflatobjs += lib/riscv/setjmp.o 48cflatobjs += lib/riscv/setup.o 49cflatobjs += lib/riscv/smp.o 50cflatobjs += lib/riscv/stack.o 51cflatobjs += lib/riscv/timer.o 52cflatobjs += lib/riscv/sbi-sse-asm.o 53ifeq ($(ARCH),riscv32) 54cflatobjs += lib/ldiv32.o 55endif 56 57######################################## 58 59OBJDIRS += lib/riscv 60FLATLIBS = $(libcflat) $(LIBFDT_archive) 61 62AUXFLAGS ?= 0x0 63 64# stack.o relies on frame pointers. 65KEEP_FRAME_POINTER := y 66 67# We want to keep intermediate files 68.PRECIOUS: %.elf %.o 69 70define arch_elf_check = 71 $(if $(shell ! $(READELF) -rW $(1) >&/dev/null && echo "nok"), 72 $(error $(shell $(READELF) -rW $(1) 2>&1))) 73 $(if $(shell $(READELF) -rW $(1) | grep R_ | grep -v R_RISCV_RELATIVE), 74 $(error $(1) has unsupported reloc types)) 75endef 76 77ISA_COMMON = imac_zicsr_zifencei_zihintpause 78 79ifeq ($(ARCH),riscv64) 80CFLAGS += -DCONFIG_64BIT 81CFLAGS += -mabi=lp64 -march=rv64$(ISA_COMMON) 82LDFLAGS += -melf64lriscv 83else ifeq ($(ARCH),riscv32) 84CFLAGS += -mabi=ilp32 -march=rv32$(ISA_COMMON) 85LDFLAGS += -melf32lriscv 86endif 87CFLAGS += -DCONFIG_RELOC 88CFLAGS += -mcmodel=medany 89# Unaligned accesses are allowed, but may be emulated by M-mode. 90# Enable -mstrict-align if that's troublesome (only supported by gcc). 91#CFLAGS += -mstrict-align 92CFLAGS += -std=gnu99 93CFLAGS += -ffreestanding 94CFLAGS += -O2 95CFLAGS += -I $(SRCDIR)/lib -I $(SRCDIR)/lib/libfdt -I lib -I $(SRCDIR)/riscv 96 97asm-offsets = lib/riscv/asm-offsets.h riscv/sbi-asm-offsets.h 98include $(SRCDIR)/scripts/asm-offsets.mak 99 100.PRECIOUS: %.aux.o 101%.aux.o: $(SRCDIR)/lib/auxinfo.c 102 $(CC) $(CFLAGS) -c -o $@ $< \ 103 -DPROGNAME=\"$(notdir $(@:.aux.o=.$(exe)))\" -DAUXFLAGS=$(AUXFLAGS) 104 105.SECONDEXPANSION: 106ifeq ($(CONFIG_EFI),y) 107# avoid jump tables before all relocations have been processed 108riscv/efi/reloc_riscv64.o: CFLAGS += -fno-jump-tables 109cflatobjs += riscv/efi/reloc_riscv64.o 110cflatobjs += lib/acpi.o 111cflatobjs += lib/efi.o 112 113.PRECIOUS: %.so 114 115%.so: EFI_LDFLAGS += -defsym=EFI_SUBSYSTEM=0xa --no-undefined 116%.so: %.o $(FLATLIBS) $(SRCDIR)/riscv/efi/elf_riscv64_efi.lds $(cstart.o) %.aux.o $$($$*-deps) 117 $(LD) $(EFI_LDFLAGS) -o $@ -T $(SRCDIR)/riscv/efi/elf_riscv64_efi.lds \ 118 $(filter %.o, $^) $(FLATLIBS) $(EFI_LIBS) 119 120%.efi: %.so 121 $(call arch_elf_check, $^) 122 $(OBJCOPY) --only-keep-debug $^ $@.debug 123 $(OBJCOPY) --strip-debug $^ 124 $(OBJCOPY) --add-gnu-debuglink=$@.debug $^ 125 $(OBJCOPY) \ 126 -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym \ 127 -j .rel -j .rela -j .rel.* -j .rela.* -j .rel* -j .rela* \ 128 -j .reloc \ 129 -O binary $^ $@ 130else 131%.elf: LDFLAGS += -pie -n -z notext 132%.elf: %.o $(FLATLIBS) $(SRCDIR)/riscv/flat.lds $(cstart.o) %.aux.o $$($$*-deps) 133 $(LD) $(LDFLAGS) -o $@ -T $(SRCDIR)/riscv/flat.lds \ 134 $(filter %.o, $^) $(FLATLIBS) 135 @chmod a-x $@ 136 137%.flat: %.elf 138 $(call arch_elf_check, $^) 139 $(OBJCOPY) -O binary $^ $@ 140 @chmod a-x $@ 141endif 142 143generated-files = $(asm-offsets) 144$(tests:.$(exe)=.o) $(cstart.o) $(cflatobjs) $(all_deps): $(generated-files) 145 146arch_clean: asm_offsets_clean 147 $(RM) $(TEST_DIR)/*.{o,flat,elf,so,efi,debug} \ 148 $(TEST_DIR)/.*.d $(TEST_DIR)/efi/.*.d lib/riscv/.*.d 149