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
This commit is contained in:
Michal Meloun 2024-11-03 13:15:33 +01:00
parent bab5e3d468
commit ba045ba49d
2 changed files with 21 additions and 16 deletions

View File

@ -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}"

View File

@ -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}