git: ba045ba49ded - main - arm: Improve the creation of kernel.bin

From: Michal Meloun <mmel_at_FreeBSD.org>
Date: Sat, 09 Nov 2024 07:16:31 UTC
The branch main has been updated by mmel:

URL: https://cgit.FreeBSD.org/src/commit/?id=ba045ba49dedb068ba2f0e13fbb1c1a91fde8333

commit ba045ba49dedb068ba2f0e13fbb1c1a91fde8333
Author:     Michal Meloun <mmel@FreeBSD.org>
AuthorDate: 2024-11-03 12:15:33 +0000
Commit:     Michal Meloun <mmel@FreeBSD.org>
CommitDate: 2024-11-09 07:15:23 +0000

    arm: Improve the creation of kernel.bin
    
    Instead of relinking the kernel without elf headers, replace them with
    a binary blob of the same length starting with a jump to the kernel's start
    address. This ensures that all symbols stay at the same offsets as in
    the original kernel, so kernel.debug remains valid also for kernel.bin.
    
    Also ensure that the awk script used runs with  the neutral locale defined.
    Awk 'write' is a locale compilant, and  script is used to write binary data
    to a file, so it needs to ensure that  bytes > 127 are written unchanged.
    
    While I'm on, set the neutral locale also for arm64, where the same script is
    used to generate the kernel booti image.
    
    Reviewed by:   emaste, imp
    Differential Revision:  https://reviews.freebsd.org/D47488
---
 sys/conf/Makefile.arm   | 36 ++++++++++++++++++++----------------
 sys/conf/Makefile.arm64 |  1 +
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/sys/conf/Makefile.arm b/sys/conf/Makefile.arm
index 5416aff76666..8e98e5b07648 100644
--- a/sys/conf/Makefile.arm
+++ b/sys/conf/Makefile.arm
@@ -57,6 +57,7 @@ KERNVIRTADDR= 0xc0000000
 # "ELF for the ARM architecture" for more info on the mapping symbols.
 SYSTEM_LD= \
 	${SYSTEM_LD_BASECMD} \
+	    --Map=/usr2/Meloun/ld.map \
 	    --defsym='text_start=kernbase + SIZEOF_HEADERS' \
 	    -o ${.TARGET} ${SYSTEM_OBJS} vers.o; \
 	$(OBJCOPY) \
@@ -64,22 +65,6 @@ SYSTEM_LD= \
 	    --strip-symbol='$$[adt]*' \
 	    ${.TARGET}
 
-# Generate the .bin (no elf headers) kernel as an extra build output.
-# We must relink to generate the .bin kernel, because without headers the
-# location of everything changes.  We also strip the ARM marker symbols.
-${KERNEL_KO}.bin: ${SYSTEM_DEP} vers.o
-	@echo "linking ${.TARGET}"
-	@${SYSTEM_LD_BASECMD} \
-	    --defsym='text_start=kernbase' \
-	    -o ${.TARGET} ${SYSTEM_OBJS} vers.o
-	${SIZE} ${.TARGET}
-	@${OBJCOPY} \
-	    --wildcard \
-	    --strip-symbol='$$[adt]*' \
-	    --output-target=binary \
-	    ${.TARGET}
-	@chmod 755 ${.TARGET}
-
 # hack because genassym.c includes sys/bus.h which includes these.
 genassym.o: bus_if.h device_if.h
 
@@ -98,3 +83,22 @@ genassym.o: bus_if.h device_if.h
 %RULES
 	
 .include "$S/conf/kern.post.mk"
+
+# Generate the .bin (no elf headers) kernel
+# Copy the kernel to directly executable format (the elf headers are
+# stripped and a binary head blob with jump to kernel start address is
+# prepended), saving the output in a temp file.
+# We also strip arm "marker" symbols which are used only by elf toolchains. Read
+# the symbols from kernel.full and pass  them to arm_kernel_boothdr.awk, which
+# generates a binary header blob that goes on the front of the stripped kernel.
+# Cat the header blob and the temp file together to make the kernel.bin file.
+${KERNEL_KO}.bin: ${FULLKERNEL}
+	@${OBJCOPY} --wildcard --strip-symbol='$$[adtx]*' \
+	    --output-target=binary ${FULLKERNEL} ${.TARGET}.temp
+	@{ ${NM} ${FULLKERNEL} | \
+	    LC_ALL=C \
+	    ${AWK} -f $S/tools/arm_kernel_boothdr.awk -v hdrtype=v7jump && \
+	    cat ${.TARGET}.temp; \
+	 } > ${.TARGET}
+	@rm ${.TARGET}.temp
+	@echo "created ${.TARGET} from ${.ALLSRC}"
diff --git a/sys/conf/Makefile.arm64 b/sys/conf/Makefile.arm64
index 52f0f9052db3..ed9624ee5446 100644
--- a/sys/conf/Makefile.arm64
+++ b/sys/conf/Makefile.arm64
@@ -88,6 +88,7 @@ ${KERNEL_KO}.bin: ${FULLKERNEL}
 	@${OBJCOPY} --wildcard --strip-symbol='$$[adtx]*' \
 	    --output-target=binary ${FULLKERNEL} ${.TARGET}.temp
 	@{ ${NM} ${FULLKERNEL} | \
+	    LC_ALL=C \
 	    ${AWK} -f $S/tools/arm_kernel_boothdr.awk -v hdrtype=v8booti && \
 	    cat ${.TARGET}.temp; \
 	 } > ${.TARGET}