:: commit 2cfe54ee99a44af0246e72870984d4b5306ab3c8

mintsuki <mintsuki@protonmail.com> — 2021-11-08 08:42

parents: ec92f62faf

build: Get build-id from linker instead of generating a random one

diff --git a/stage23/Makefile b/stage23/Makefile
index 03d85e3f..73a4a193 100644
--- a/stage23/Makefile
+++ b/stage23/Makefile
@@ -50,8 +50,6 @@ endif
 COM_OUTPUT = false
 E9_OUTPUT = false
 
-BUILD_ID := $(shell dd if=/dev/urandom count=8 bs=1 2>/dev/null | od -An -t x4 | sed 's/^ /0x/g;s/ //g')
-
 WERROR = -Werror
 CFLAGS ?= -O3 -g -pipe -Wall -Wextra $(WERROR)
 
@@ -71,7 +69,6 @@ INTERNAL_CFLAGS := \
 	-mno-sse \
 	-mno-sse2 \
 	-MMD \
-	-DBUILD_ID=$(BUILD_ID) \
 	-DLIMINE_VERSION='"$(LIMINE_VERSION)"' \
 	-DLIMINE_COPYRIGHT='"$(LIMINE_COPYRIGHT)"' \
 	-DCOM_OUTPUT=$(COM_OUTPUT) \
@@ -141,7 +138,8 @@ INTERNAL_LDFLAGS := \
 ifeq ($(TARGET), bios)
 	INTERNAL_LDFLAGS += \
 		-melf_i386 \
-		-static
+		-static \
+		--build-id=sha1
 endif
 
 ifeq ($(TARGET), uefi)
@@ -246,15 +244,43 @@ $(BUILDDIR)/limine_stage2only.elf: $(OBJ)
 	$(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" && \
 		  false )
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s2.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s2.bin build-id.s2.o
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s3.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s3.bin build-id.s3.o
+	$(TOOLCHAIN_LD) $(BUILDDIR)/build-id.s2.o $(BUILDDIR)/build-id.s3.o $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_stage2only.ld -o $@
 
 $(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/tinf/tinfgzip.o $(BUILDDIR)/tinf/tinflate.o $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o
 	$(TOOLCHAIN_LD) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s2.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s2.bin build-id.s2.o
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s3.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s3.bin build-id.s3.o
+	$(TOOLCHAIN_LD) $(BUILDDIR)/build-id.s2.o $(BUILDDIR)/build-id.s3.o $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
 
 $(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/tinf/tinfgzip.o $(BUILDDIR)/tinf/tinflate.o $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
 	$(TOOLCHAIN_LD) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s2.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s2.bin build-id.s2.o
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s3.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s3.bin build-id.s3.o
+	$(TOOLCHAIN_LD) $(BUILDDIR)/build-id.s2.o $(BUILDDIR)/build-id.s3.o $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
 
 $(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/tinf/tinfgzip.o $(BUILDDIR)/tinf/tinflate.o $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
 	$(TOOLCHAIN_LD) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s2.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s2.bin build-id.s2.o
+	$(TOOLCHAIN_OBJCOPY) -O binary --only-section=.note.gnu.build-id $@ $(BUILDDIR)/build-id.s3.bin
+	cd "$(BUILDDIR)" && \
+		$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) build-id.s3.bin build-id.s3.o
+	$(TOOLCHAIN_LD) $(BUILDDIR)/build-id.s2.o $(BUILDDIR)/build-id.s3.o $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
 
 endif
 
diff --git a/stage23/entry.s2.c b/stage23/entry.s2.c
index 663ea65f..65921f54 100644
--- a/stage23/entry.s2.c
+++ b/stage23/entry.s2.c
@@ -21,8 +21,6 @@
 #include <drivers/disk.h>
 #include <sys/idt.h>
 
-extern uint64_t stage3_build_id;
-
 struct volume *boot_volume;
 
 #if bios == 1
@@ -32,6 +30,8 @@ static bool stage3_found = false;
 
 extern symbol stage3_addr;
 extern symbol limine_sys_size;
+extern symbol build_id_s2;
+extern symbol build_id_s3;
 
 static bool stage3_init(struct volume *part) {
     struct file_handle *stage3;
@@ -54,7 +54,7 @@ static bool stage3_init(struct volume *part) {
 
     fclose(stage3);
 
-    if (BUILD_ID != stage3_build_id) {
+    if (memcmp(build_id_s2 + 16, build_id_s3 + 16, 20) != 0) {
         print("limine.sys build ID mismatch.\n");
         return false;
     }
diff --git a/stage23/entry.s3.c b/stage23/entry.s3.c
index f5c91104..7b6afeaf 100644
--- a/stage23/entry.s3.c
+++ b/stage23/entry.s3.c
@@ -126,9 +126,6 @@ void uefi_entry(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
 #endif
 
 #if bios == 1
-__attribute__((section(".stage3_build_id")))
-uint64_t stage3_build_id = BUILD_ID;
-
 __attribute__((section(".stage3_entry")))
 #endif
 __attribute__((noreturn))
diff --git a/stage23/linker.ld b/stage23/linker.ld
index 02308faa..398f627e 100644
--- a/stage23/linker.ld
+++ b/stage23/linker.ld
@@ -17,6 +17,11 @@ SECTIONS
         *.s2.o(.text*)
     }
 
+    .stage2.build-id : {
+        build_id_s2 = .;
+        *build-id.s2.o(*)
+    }
+
     .stage2.data : {
         *.s2.o(.data*)
         *.s2.o(.rodata*)
@@ -26,10 +31,14 @@ SECTIONS
     .stage3.text : {
         stage3_addr = .;
         *(.stage3_entry*)
-        *(.stage3_build_id*)
         *(.text*)
     }
 
+    .stage3.build-id : {
+        build_id_s3 = .;
+        *build-id.s3.o(*)
+    }
+
     .stage3.data : {
         *(.data*)
         *(.rodata*)
@@ -37,6 +46,10 @@ SECTIONS
         limine_sys_size = . - 0x8000;
     }
 
+    .note.gnu.build-id : {
+        *(.note.gnu.build-id)
+    }
+
     .bss : {
         bss_begin = .;
         *(COMMON)
diff --git a/stage23/linker_dbg.ld b/stage23/linker_dbg.ld
index f4e99048..62d5c27c 100644
--- a/stage23/linker_dbg.ld
+++ b/stage23/linker_dbg.ld
@@ -17,6 +17,11 @@ SECTIONS
         *.s2.o(.text*)
     }
 
+    .stage2.build-id : {
+        build_id_s2 = .;
+        *build-id.s2.o(*)
+    }
+
     .stage2.data : {
         *.s2.o(.data*)
         *.s2.o(.rodata*)
@@ -26,10 +31,14 @@ SECTIONS
     .stage3.text : {
         stage3_addr = .;
         *(.stage3_entry*)
-        *(.stage3_build_id*)
         *(.text*)
     }
 
+    .stage3.build-id : {
+        build_id_s3 = .;
+        *build-id.s3.o(*)
+    }
+
     .stage3.data : {
         *(.data*)
         *(.rodata*)
@@ -37,6 +46,10 @@ SECTIONS
         limine_sys_size = . - 0x8000;
     }
 
+    .note.gnu.build-id : {
+        *(.note.gnu.build-id)
+    }
+
     .bss : {
         bss_begin = .;
         *(COMMON)
diff --git a/stage23/linker_nomap.ld b/stage23/linker_nomap.ld
index 6c62b069..cd33c4e8 100644
--- a/stage23/linker_nomap.ld
+++ b/stage23/linker_nomap.ld
@@ -17,6 +17,11 @@ SECTIONS
         *.s2.o(.text*)
     }
 
+    .stage2.build-id : {
+        build_id_s2 = .;
+        *build-id.s2.o(*)
+    }
+
     .stage2.data : {
         *.s2.o(.data*)
         *.s2.o(.rodata*)
@@ -26,10 +31,14 @@ SECTIONS
     .stage3.text : {
         stage3_addr = .;
         *(.stage3_entry*)
-        *(.stage3_build_id*)
         *(.text*)
     }
 
+    .stage3.build-id : {
+        build_id_s3 = .;
+        *build-id.s3.o(*)
+    }
+
     .stage3.data : {
         *(.data*)
         *(.rodata*)
@@ -37,6 +46,10 @@ SECTIONS
         limine_sys_size = . - 0x8000;
     }
 
+    .note.gnu.build-id : {
+        *(.note.gnu.build-id)
+    }
+
     .bss : {
         bss_begin = .;
         *(COMMON)
diff --git a/stage23/linker_stage2only.ld b/stage23/linker_stage2only.ld
index ccd38277..2163dfff 100644
--- a/stage23/linker_stage2only.ld
+++ b/stage23/linker_stage2only.ld
@@ -17,17 +17,30 @@ SECTIONS
         *.s2.o(.text*)
     }
 
+    .stage2.build-id : {
+        build_id_s2 = .;
+        *build-id.s2.o(*)
+    }
+
     .stage2.data : {
         *.s2.o(.data*)
         *.s2.o(.rodata*)
         stage2_map = .;
         stage3_addr = .;
-        *(.stage3_build_id*)
         full_map = .;
         limine_sys_size = .;
         getchar_internal = .;
     }
 
+    .stage3.build-id : {
+        build_id_s3 = .;
+        *build-id.s3.o(*)
+    }
+
+    .note.gnu.build-id : {
+        *(.note.gnu.build-id)
+    }
+
     .bss : {
         bss_begin = .;
         *(COMMON)
tab: 248 wrap: offon