build: Use linker directly instead of going thru compiler; ensure we are buildable with an llvm toolchain

This commit is contained in:
mintsuki 2021-05-31 00:32:43 +02:00
parent 1e935e10d3
commit e69f7429ce
7 changed files with 135 additions and 31 deletions

View File

@ -7,16 +7,20 @@ endif
TOOLCHAIN = x86_64-elf TOOLCHAIN = x86_64-elf
TOOLCHAIN_CC = $(TOOLCHAIN)-gcc TOOLCHAIN_CC = $(TOOLCHAIN)-gcc
TOOLCHAIN_LD = $(TOOLCHAIN)-ld
TOOLCHAIN_OBJCOPY = $(TOOLCHAIN)-objcopy TOOLCHAIN_OBJCOPY = $(TOOLCHAIN)-objcopy
ifeq ($(shell which $(TOOLCHAIN_CC)), ) ifeq ($(shell which $(TOOLCHAIN_CC)), )
TOOLCHAIN_CC := gcc TOOLCHAIN_CC := gcc
endif endif
ifeq ($(shell which $(TOOLCHAIN_LD)), )
TOOLCHAIN_LD := ld
endif
ifeq ($(shell which $(TOOLCHAIN_OBJCOPY)), ) ifeq ($(shell which $(TOOLCHAIN_OBJCOPY)), )
TOOLCHAIN_OBJCOPY := objcopy TOOLCHAIN_OBJCOPY := objcopy
endif endif
CFLAGS = -flto -Os -pipe -Wall -Wextra -Werror CFLAGS = -Os -pipe -Wall -Wextra -Werror
INTERNAL_CFLAGS = \ INTERNAL_CFLAGS = \
-m32 \ -m32 \
@ -36,16 +40,11 @@ INTERNAL_CFLAGS = \
-MMD \ -MMD \
-I. -I.
LDFLAGS = -flto -Os LDFLAGS =
INTERNAL_LDFLAGS = \ INTERNAL_LDFLAGS = \
-m32 \ -melf_i386 \
-march=i386 \
-Wl,-melf_i386 \
-nostdlib \ -nostdlib \
-no-pie \
-fno-pic \
-fno-pie \
-z max-page-size=0x1000 \ -z max-page-size=0x1000 \
-static \ -static \
-Tlinker.ld -Tlinker.ld
@ -65,7 +64,7 @@ builddir:
for i in $(OBJ); do mkdir -p `dirname $$i`; done for i in $(OBJ); do mkdir -p `dirname $$i`; done
$(BUILDDIR)/decompressor.bin: $(OBJ) $(BUILDDIR)/decompressor.bin: $(OBJ)
$(TOOLCHAIN_CC) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $(BUILDDIR)/decompressor.elf $(TOOLCHAIN_LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $(BUILDDIR)/decompressor.elf
$(TOOLCHAIN_OBJCOPY) -O binary $(BUILDDIR)/decompressor.elf $@ $(TOOLCHAIN_OBJCOPY) -O binary $(BUILDDIR)/decompressor.elf $@
-include $(HEADER_DEPS) -include $(HEADER_DEPS)

View File

@ -25,6 +25,18 @@ SECTIONS
bss_end = .; bss_end = .;
} }
.symtab : {
*(.symtab)
}
.strtab : {
*(.strtab)
}
.shstrtab : {
*(.shstrtab)
}
/DISCARD/ : { /DISCARD/ : {
*(*) *(*)
} }

View File

@ -16,6 +16,7 @@ endif
TOOLCHAIN = x86_64-elf TOOLCHAIN = x86_64-elf
TOOLCHAIN_CC = $(TOOLCHAIN)-gcc TOOLCHAIN_CC = $(TOOLCHAIN)-gcc
TOOLCHAIN_LD = $(TOOLCHAIN)-ld
TOOLCHAIN_OBJCOPY = $(TOOLCHAIN)-objcopy TOOLCHAIN_OBJCOPY = $(TOOLCHAIN)-objcopy
TOOLCHAIN_OBJDUMP = $(TOOLCHAIN)-objdump TOOLCHAIN_OBJDUMP = $(TOOLCHAIN)-objdump
TOOLCHAIN_READELF = $(TOOLCHAIN)-readelf TOOLCHAIN_READELF = $(TOOLCHAIN)-readelf
@ -23,6 +24,9 @@ TOOLCHAIN_READELF = $(TOOLCHAIN)-readelf
ifeq ($(shell which $(TOOLCHAIN_CC)), ) ifeq ($(shell which $(TOOLCHAIN_CC)), )
TOOLCHAIN_CC := gcc TOOLCHAIN_CC := gcc
endif endif
ifeq ($(shell which $(TOOLCHAIN_LD)), )
TOOLCHAIN_LD := ld
endif
ifeq ($(shell which $(TOOLCHAIN_OBJCOPY)), ) ifeq ($(shell which $(TOOLCHAIN_OBJCOPY)), )
TOOLCHAIN_OBJCOPY := objcopy TOOLCHAIN_OBJCOPY := objcopy
endif endif
@ -88,32 +92,25 @@ ifeq ($(TARGET), uefi)
-mno-red-zone -mno-red-zone
endif endif
LDFLAGS = -O3 -g LDFLAGS =
INTERNAL_LDFLAGS := \ INTERNAL_LDFLAGS := \
-fno-pic \
-fno-lto \
-nostdlib \ -nostdlib \
-z max-page-size=0x1000 -z max-page-size=0x1000
ifeq ($(TARGET), bios) ifeq ($(TARGET), bios)
INTERNAL_LDFLAGS += \ INTERNAL_LDFLAGS += \
-m32 \ -melf_i386 \
-march=i386 \ -static
-fno-pie \
-Wl,-melf_i386 \
-static \
-no-pie
endif endif
ifeq ($(TARGET), uefi) ifeq ($(TARGET), uefi)
INTERNAL_LDFLAGS += \ INTERNAL_LDFLAGS += \
-m64 \ -melf_x86_64 \
-march=x86-64 \ -static \
-fpie \ -pie \
-Wl,-melf_x86_64 \ --no-dynamic-linker \
-Wl,-static,-pie,--no-dynamic-linker,-ztext \ -ztext
-static-pie
endif endif
.PHONY: all clean builddir .PHONY: all clean builddir
@ -175,18 +172,18 @@ $(BUILDDIR)/limine.sys: $(BUILDDIR)/limine.elf
$(TOOLCHAIN_OBJCOPY) -O binary $< $@ $(TOOLCHAIN_OBJCOPY) -O binary $< $@
$(BUILDDIR)/limine_stage2only.elf: $(OBJ) $(BUILDDIR)/limine_stage2only.elf: $(OBJ)
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_stage2only.ld -o $@ || \ $(TOOLCHAIN_LD) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_stage2only.ld -o $@ || \
( echo "This error may mean that stage 2 was trying to use stage 3 symbols before loading stage 3" && \ ( echo "This error may mean that stage 2 was trying to use stage 3 symbols before loading stage 3" && \
false ) false )
$(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@ $(TOOLCHAIN_LD) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
$(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o $(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@ $(TOOLCHAIN_LD) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
$(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o $(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@ $(TOOLCHAIN_LD) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
endif endif
@ -201,7 +198,7 @@ $(BUILDDIR)/BOOTX64.EFI: $(BUILDDIR)/limine_efi.elf
$(TOOLCHAIN_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 $< $@ $(TOOLCHAIN_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 $< $@
$(BUILDDIR)/limine_efi_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/limine_efi_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o
$(TOOLCHAIN_CC) \ $(TOOLCHAIN_LD) \
-Tlinker_uefi_nomap.ld \ -Tlinker_uefi_nomap.ld \
../gnu-efi/gnuefi/crt0-efi-x86_64.o \ ../gnu-efi/gnuefi/crt0-efi-x86_64.o \
../gnu-efi/gnuefi/libgnuefi.a \ ../gnu-efi/gnuefi/libgnuefi.a \
@ -209,7 +206,7 @@ $(BUILDDIR)/limine_efi_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_
$^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $@ $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $@
$(BUILDDIR)/limine_efi.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/full.map.o $(BUILDDIR)/limine_efi.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/full.map.o
$(TOOLCHAIN_CC) \ $(TOOLCHAIN_LD) \
-Tlinker_uefi.ld \ -Tlinker_uefi.ld \
../gnu-efi/gnuefi/crt0-efi-x86_64.o \ ../gnu-efi/gnuefi/crt0-efi-x86_64.o \
../gnu-efi/gnuefi/libgnuefi.a \ ../gnu-efi/gnuefi/libgnuefi.a \

View File

@ -44,6 +44,18 @@ SECTIONS
bss_end = .; bss_end = .;
} }
.symtab : {
*(.symtab)
}
.strtab : {
*(.strtab)
}
.shstrtab : {
*(.shstrtab)
}
/DISCARD/ : { /DISCARD/ : {
*(*) *(*)
} }

View File

@ -44,7 +44,67 @@ SECTIONS
bss_end = .; bss_end = .;
} }
.symtab : {
*(.symtab)
}
.strtab : {
*(.strtab)
}
.shstrtab : {
*(.shstrtab)
}
.debug_aranges : {
*(.debug_aranges)
}
.debug_pubnames : {
*(.debug_pubnames)
}
.debug_info : {
*(.debug_info)
}
.debug_abbrev : {
*(.debug_abbrev)
}
.debug_line : {
*(.debug_line)
}
.debug_loclists : {
*(.debug_loclists)
}
.debug_rnglists : {
*(.debug_rnglists)
}
.debug_frame : {
*(.debug_frame)
}
.debug_loc : {
*(.debug_loc)
}
.debug_ranges : {
*(.debug_ranges)
}
.debug_str : {
*(.debug_str)
}
.debug_line_str : {
*(.debug_line_str)
}
/DISCARD/ : { /DISCARD/ : {
*(.eh_frame*) *(*)
} }
} }

View File

@ -44,6 +44,18 @@ SECTIONS
bss_end = .; bss_end = .;
} }
.symtab : {
*(.symtab)
}
.strtab : {
*(.strtab)
}
.shstrtab : {
*(.shstrtab)
}
/DISCARD/ : { /DISCARD/ : {
*(*) *(*)
} }

View File

@ -35,6 +35,18 @@ SECTIONS
bss_end = .; bss_end = .;
} }
.symtab : {
*(.symtab)
}
.strtab : {
*(.strtab)
}
.shstrtab : {
*(.shstrtab)
}
/DISCARD/ : { /DISCARD/ : {
*(*) *(*)
} }