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