2021-03-02 12:23:43 +03:00
|
|
|
TARGET = bios
|
2021-03-08 02:50:23 +03:00
|
|
|
BUILDDIR = .
|
2021-03-02 12:23:43 +03:00
|
|
|
|
|
|
|
ifeq ($(TARGET), bios)
|
|
|
|
TOOLCHAIN=i386-elf
|
|
|
|
else ifeq ($(TARGET), uefi)
|
|
|
|
TOOLCHAIN=x86_64-elf
|
|
|
|
else
|
2021-03-04 07:15:20 +03:00
|
|
|
$(error Invalid target)
|
2021-03-02 12:23:43 +03:00
|
|
|
endif
|
|
|
|
|
2021-03-07 02:52:25 +03:00
|
|
|
CC32 = i386-elf-gcc
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
CC = $(TOOLCHAIN)-gcc
|
2021-03-04 07:15:20 +03:00
|
|
|
LD = $(TOOLCHAIN)-ld
|
2021-03-02 12:23:43 +03:00
|
|
|
OBJCOPY = $(TOOLCHAIN)-objcopy
|
|
|
|
OBJDUMP = $(TOOLCHAIN)-objdump
|
|
|
|
READELF = $(TOOLCHAIN)-readelf
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2021-02-26 03:30:27 +03:00
|
|
|
COM_OUTPUT = false
|
|
|
|
E9_OUTPUT = false
|
|
|
|
|
2021-02-25 10:45:03 +03:00
|
|
|
BUILD_ID := $(shell dd if=/dev/urandom count=8 bs=1 | od -An -t x8 | sed 's/^ /0x/')
|
2021-02-23 03:16:12 +03:00
|
|
|
LIMINE_VERSION := $(shell git describe --exact-match --tags `git log -n1 --pretty='%h'` || git log -n1 --pretty='%h')
|
2020-10-18 07:23:39 +03:00
|
|
|
WERROR = -Werror
|
2021-03-05 01:12:40 +03:00
|
|
|
CFLAGS = -O3 -g -pipe -Wall -Wextra $(WERROR)
|
|
|
|
S2CFLAGS = -Os -g -pipe -Wall -Wextra $(WERROR)
|
2020-09-13 15:36:18 +03:00
|
|
|
|
2021-03-02 08:05:14 +03:00
|
|
|
INTERNAL_CFLAGS := \
|
2020-09-17 15:37:22 +03:00
|
|
|
-std=gnu11 \
|
|
|
|
-fplan9-extensions \
|
2020-01-22 01:45:13 +03:00
|
|
|
-ffreestanding \
|
2020-09-13 15:36:18 +03:00
|
|
|
-fno-stack-protector \
|
2020-11-15 19:56:10 +03:00
|
|
|
-fno-omit-frame-pointer \
|
2021-03-02 12:23:43 +03:00
|
|
|
-fno-lto \
|
2020-09-21 13:46:42 +03:00
|
|
|
-Wno-address-of-packed-member \
|
2020-09-16 18:22:05 +03:00
|
|
|
-masm=intel \
|
2021-03-02 08:05:14 +03:00
|
|
|
-mgeneral-regs-only \
|
2020-09-21 13:46:42 +03:00
|
|
|
-MMD \
|
2021-02-25 10:45:03 +03:00
|
|
|
-DBUILD_ID=$(BUILD_ID) \
|
2020-12-31 01:42:39 +03:00
|
|
|
-DLIMINE_VERSION='"$(LIMINE_VERSION)"' \
|
2021-02-26 03:30:27 +03:00
|
|
|
-DCOM_OUTPUT=$(COM_OUTPUT) \
|
|
|
|
-DE9_OUTPUT=$(E9_OUTPUT) \
|
2021-03-02 12:23:43 +03:00
|
|
|
-D$(TARGET)=1 \
|
2020-04-29 17:53:05 +03:00
|
|
|
-I. \
|
2020-09-21 13:46:42 +03:00
|
|
|
-I..
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2021-03-04 07:15:20 +03:00
|
|
|
ifeq ($(TARGET), bios)
|
|
|
|
INTERNAL_CFLAGS += \
|
|
|
|
-fno-pic
|
|
|
|
endif
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
ifeq ($(TARGET), uefi)
|
|
|
|
INTERNAL_CFLAGS += \
|
|
|
|
-I../gnu-efi/inc \
|
|
|
|
-I../gnu-efi/inc/x86_64 \
|
|
|
|
-fpic \
|
2021-03-04 07:15:20 +03:00
|
|
|
-mno-red-zone
|
2021-03-02 12:23:43 +03:00
|
|
|
endif
|
|
|
|
|
2021-03-05 01:12:40 +03:00
|
|
|
LDFLAGS = -O3 -g
|
2020-09-13 15:36:18 +03:00
|
|
|
|
2021-03-02 08:05:14 +03:00
|
|
|
INTERNAL_LDFLAGS := \
|
2021-03-02 12:23:43 +03:00
|
|
|
-fno-lto \
|
2020-01-22 01:45:13 +03:00
|
|
|
-nostdlib \
|
2021-03-02 12:23:43 +03:00
|
|
|
-z max-page-size=0x1000
|
|
|
|
|
|
|
|
ifeq ($(TARGET), bios)
|
|
|
|
INTERNAL_LDFLAGS += \
|
|
|
|
-static \
|
2021-03-04 07:15:20 +03:00
|
|
|
-fno-pie \
|
2021-03-02 12:23:43 +03:00
|
|
|
-lgcc \
|
|
|
|
-static-libgcc
|
|
|
|
endif
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
.PHONY: all clean builddir
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2021-02-21 07:01:18 +03:00
|
|
|
C_FILES := $(shell find -L ./ -type f -name '*.c' | sort)
|
2021-03-02 12:23:43 +03:00
|
|
|
ifeq ($(TARGET), bios)
|
2021-02-21 07:01:18 +03:00
|
|
|
ASM_FILES := $(shell find -L ./ -type f -name '*.asm' | sort)
|
2021-03-02 12:23:43 +03:00
|
|
|
endif
|
2021-03-07 02:52:25 +03:00
|
|
|
ifeq ($(TARGET), uefi)
|
|
|
|
ASM_FILES := lib/do_32.asm
|
|
|
|
endif
|
2021-03-08 02:50:23 +03:00
|
|
|
OBJ := $(addprefix $(BUILDDIR)/, $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o))
|
|
|
|
HEADER_DEPS := $(addprefix $(BUILDDIR)/, $(C_FILES:.c=.d))
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
ifeq ($(TARGET), bios)
|
2021-03-08 02:50:23 +03:00
|
|
|
all: | builddir everything
|
|
|
|
.PHONY: everything
|
|
|
|
everything: $(BUILDDIR)/limine_dbg.elf $(BUILDDIR)/limine.sys $(BUILDDIR)/stage2.bin $(BUILDDIR)/stage2.bin.gz
|
2021-03-02 12:23:43 +03:00
|
|
|
else ifeq ($(TARGET), uefi)
|
2021-03-08 02:50:23 +03:00
|
|
|
all: | builddir $(BUILDDIR)/BOOTX64.EFI
|
2021-03-02 12:23:43 +03:00
|
|
|
endif
|
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
builddir:
|
|
|
|
for i in $(OBJ); do mkdir -p `dirname $$i`; done
|
2021-02-22 22:43:51 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/sys/smp_trampoline.bin: sys/smp_trampoline.real
|
|
|
|
nasm $< -f bin -o $@
|
|
|
|
|
|
|
|
$(BUILDDIR)/sys/smp_trampoline.o: $(BUILDDIR)/sys/smp_trampoline.bin
|
|
|
|
cd "`dirname $<`" && \
|
|
|
|
$(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@
|
|
|
|
|
|
|
|
$(BUILDDIR)/font.o: font.bin
|
|
|
|
cd "`dirname $<`" && \
|
|
|
|
$(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@
|
|
|
|
|
|
|
|
ifeq ($(TARGET), bios)
|
|
|
|
|
|
|
|
$(BUILDDIR)/stage2.bin.gz: $(BUILDDIR)/stage2.bin
|
|
|
|
gzip -n -9 < $< > $@
|
2021-02-21 01:04:06 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/stage2.bin: $(BUILDDIR)/limine.sys
|
|
|
|
dd if=$< bs=$$(( 0x$$($(READELF) -S $(BUILDDIR)/limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
|
2021-02-21 01:04:06 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/limine.map.o: $(BUILDDIR)/limine_nomap.elf
|
|
|
|
GENSYMS="`pwd`/gensyms.sh" && \
|
|
|
|
cd "`dirname $<`" && \
|
|
|
|
"$$GENSYMS" $(OBJDUMP) $< limine
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/limine.sys: $(BUILDDIR)/limine.elf
|
2020-11-15 19:56:10 +03:00
|
|
|
$(OBJCOPY) -O binary $< $@
|
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o
|
|
|
|
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
|
|
|
|
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Wl,--gc-sections -Tlinker_stage2only.ld -o limine_stage2only.elf || \
|
2021-02-26 01:11:53 +03:00
|
|
|
( echo "This error means that stage 2 was trying to use stage 3 symbols before loading stage 3" && \
|
2021-02-25 13:28:14 +03:00
|
|
|
false )
|
2021-02-25 03:24:54 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/limine.map.o
|
|
|
|
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
|
2021-03-04 07:15:20 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o
|
|
|
|
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
|
2021-03-04 07:15:20 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
endif
|
2021-03-03 23:40:55 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
ifeq ($(TARGET), uefi)
|
2021-03-02 08:05:14 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/BOOTX64.EFI: $(BUILDDIR)/limine_efi.elf
|
|
|
|
$(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 $< $@
|
2021-03-04 07:15:20 +03:00
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(BUILDDIR)/limine_efi.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o
|
2021-03-04 07:15:20 +03:00
|
|
|
$(LD) -shared -Bsymbolic \
|
|
|
|
-T../gnu-efi/gnuefi/elf_x86_64_efi.lds \
|
2021-03-06 08:02:48 +03:00
|
|
|
../gnu-efi/gnuefi/crt0-efi-x86_64.o \
|
|
|
|
../gnu-efi/gnuefi/libgnuefi.a \
|
|
|
|
../gnu-efi/lib/x86_64/efi_stub.o \
|
2021-03-08 02:50:23 +03:00
|
|
|
$^ -o $@
|
|
|
|
|
|
|
|
endif
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2020-09-21 13:46:42 +03:00
|
|
|
-include $(HEADER_DEPS)
|
|
|
|
|
2021-03-08 02:50:23 +03:00
|
|
|
$(addprefix $(BUILDDIR)/, %.o): %.c
|
2020-09-13 15:36:18 +03:00
|
|
|
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
|
2020-01-22 01:45:13 +03:00
|
|
|
|
2021-03-05 01:12:40 +03:00
|
|
|
ifeq ($(TARGET), bios)
|
2021-03-08 02:50:23 +03:00
|
|
|
$(addprefix $(BUILDDIR)/, %.s2.o): %.s2.c
|
2021-03-05 01:12:40 +03:00
|
|
|
$(CC) $(S2CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
|
|
|
|
endif
|
|
|
|
|
2021-03-07 02:52:25 +03:00
|
|
|
ifeq ($(TARGET), uefi)
|
2021-03-08 02:50:23 +03:00
|
|
|
$(addprefix $(BUILDDIR)/, %.32.o): %.32.c
|
2021-03-07 02:52:25 +03:00
|
|
|
$(CC32) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@.32
|
|
|
|
$(OBJCOPY) -I elf32-i386 -O elf64-x86-64 $@.32 $@
|
|
|
|
rm $@.32
|
|
|
|
endif
|
|
|
|
|
|
|
|
ifeq ($(TARGET), bios)
|
2021-03-08 02:50:23 +03:00
|
|
|
$(addprefix $(BUILDDIR)/, %.o): %.asm
|
2021-03-04 07:15:20 +03:00
|
|
|
nasm $< -F dwarf -g -f elf32 -o $@
|
2021-03-07 02:52:25 +03:00
|
|
|
endif
|
|
|
|
ifeq ($(TARGET), uefi)
|
2021-03-08 02:50:23 +03:00
|
|
|
$(addprefix $(BUILDDIR)/, %.o): %.asm
|
2021-03-07 02:52:25 +03:00
|
|
|
nasm $< -F dwarf -g -f elf64 -o $@
|
|
|
|
endif
|
2020-09-04 02:37:24 +03:00
|
|
|
|
2020-01-22 01:45:13 +03:00
|
|
|
clean:
|
2021-03-08 02:50:23 +03:00
|
|
|
rm -rf $(BUILDDIR)
|