git: 4c8ea3efe73c - main - stand: Go back to a.out format for /boot/loader

From: Warner Losh <imp_at_FreeBSD.org>
Date: Thu, 11 Aug 2022 23:30:00 UTC
The branch main has been updated by imp:

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

commit 4c8ea3efe73c12d595472a7fc37177432a77858e
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-08-11 23:29:10 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-08-11 23:29:10 +0000

    stand: Go back to a.out format for /boot/loader
    
    Turns out there's two hidden a.out dependencies. pxeldr.S assumes it has
    access to the a.out header from /boot/loader and cdboot.S assumes that
    /boot/loader is also a.out and doesn't use boot2.
    
    So, go back to making a.out files for these and adjust the size checks
    to use ls, but we only need to check loader.bin. Trim the size we check
    against by 2,000. The difference in size between loader and loader.bin
    is about 3000 bytes, but clang15 produces binaries that are a smidge
    bigger so we need to relax the check just a little and accept some
    additional risk for the moment.
    
    Add some comments to loader's Makefile about this.
    
    Sponsored by:           Netflix
    Reviewed by:            emaste
    Differential Revision:  https://reviews.freebsd.org/D36142
---
 stand/i386/loader/Makefile | 12 ++++++------
 stand/i386/pxeldr/Makefile |  7 +++----
 2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/stand/i386/loader/Makefile b/stand/i386/loader/Makefile
index 8b027b2edd61..539c736af38f 100644
--- a/stand/i386/loader/Makefile
+++ b/stand/i386/loader/Makefile
@@ -19,7 +19,7 @@ PROG=		${LOADER}.sym
 INTERNALPROG=
 NEWVERSWHAT?=	"bootstrap loader" x86
 VERSION_FILE=	${.CURDIR}/../loader/version
-LOADERSIZE?=	510000		# Largest known safe size
+LOADERSIZE?=	508000		# Largest known safe size for loader.bin
 
 
 .PATH:		${BOOTSRC}/i386/loader
@@ -83,13 +83,13 @@ CFLAGS+=	-I${BOOTSRC}/i386
 
 
 ${LOADER}: ${LOADER}.bin ${BTXLDR} ${BTXKERN}
-	btxld -v -f elf -e ${LOADER_ADDRESS} -o ${.TARGET} -l ${BTXLDR} \
+	btxld -v -f aout -e ${LOADER_ADDRESS} -o ${.TARGET} -l ${BTXLDR} \
 		-b ${BTXKERN} ${LOADER}.bin
-	@set -- `${SIZE} ${.TARGET} | tail -1` ; x=$$((${LOADERSIZE}-$$4)); \
-	    echo "$$x bytes available"; test $$x -ge 0
 
 ${LOADER}.bin: ${LOADER}.sym
 	${STRIPBIN} -R .comment -R .note -o ${.TARGET} ${.ALLSRC}
+	@set -- `ls -l ${.TARGET}` ; x=$$((${LOADERSIZE}-$$5)); \
+	    echo "$$x bytes available"; test $$x -ge 0
 
 .if ${MK_LOADER_ZFS} == "yes" && ${LOADER_INTERP} == ${LOADER_DEFAULT_INTERP}
 LINKS+=		${BINDIR}/${LOADER} ${BINDIR}/zfsloader
@@ -103,8 +103,8 @@ FILESMODE_${LOADER}= ${BINMODE} -b
 # Note: crt0.o needs to be first for pxeboot(8) to work. It assumes that the
 # startup code is located at the start of the loader and will jump
 # there. Although btx is more flexible than this, the emulated boot2 environment
-# that pxeloader provides has none of that flexibility because it lacks access
-# to the a.out/elf headers and assumes an entry point of 0.
+# that pxeldr provides has none of that flexibility assumes an entry point of
+# 0. In addition, pxeldr and cdboot assume that it is loading an a.out file.
 #
 # We must add it to the LDFLAGS instead of the OBJS because the former won't try
 # to clean it. When it is in OBJS, this cleaning can lead to races where
diff --git a/stand/i386/pxeldr/Makefile b/stand/i386/pxeldr/Makefile
index 3d27285968ce..790f5ed8f0e9 100644
--- a/stand/i386/pxeldr/Makefile
+++ b/stand/i386/pxeldr/Makefile
@@ -13,7 +13,6 @@ BOOT=	pxeboot
 LDR=	pxeldr
 ORG=	0x7c00
 LOADER=	loader
-PXELDRSIZE?= 510000		# Largest known safe size
 
 .if defined(BOOT_PXELDR_PROBE_KEYBOARD)
 CFLAGS+=-DPROBE_KEYBOARD
@@ -39,10 +38,10 @@ LDFLAGS+=${LDFLAGS_BIN}
 
 CLEANFILES+= ${LOADER}
 
+# Note: pxeldr.S assumes a.out and will require changes for ELF if the output
+# format is changed to ELF.
 ${LOADER}: ${LOADERBIN} ${BTXLDR} ${BTXKERN}
-	btxld -v -f elf -e ${LOADER_ADDRESS} -o ${.TARGET} -l ${BTXLDR} \
+	btxld -v -f aout -e ${LOADER_ADDRESS} -o ${.TARGET} -l ${BTXLDR} \
 	    -b ${BTXKERN} ${LOADERBIN}
-	@set -- `${SIZE} ${.TARGET} | tail -1` ; x=$$((${PXELDRSIZE}-$$4)); \
-	    echo "$$x bytes available"; test $$x -ge 0
 
 .include <bsd.prog.mk>