From: Michael Brown <mcb30@ipxe.org>
Date: Sun, 14 Jan 2024 12:12:18 +0000
Subject: [build] Fix building with newer binutils (bsc#1219733, bsc#1219722)

Newer versions of the GNU assembler (observed with binutils 2.41) will
complain about the ".arch i386" in files assembled with "as --64",
with the message "Error: 64bit mode not supported on 'i386'".

In files such as stack.S that contain no instructions to be assembled,
the ".arch i386" is redundant and may be removed entirely.

In the remaining files, fix by moving ".arch i386" below the relevant
".code16" or ".code32" directive, so that the assembler is no longer
expecting 64-bit instructions to be used by the time that the ".arch
i386" directive is encountered.

Reported-by: Ali Mustakim <alim@forwardcomputers.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
(cherry picked from commit 6ca597eee9f95b846a3c2dc1231e63cfc02272c1)
References: bsc#1219733
References: bsc#1219722
Signed-off-by: Dario Faggioli <dfaggioli@suse.com>
---
 src/arch/i386/core/setjmp.S                 | 2 +-
 src/arch/i386/tests/gdbstub_test.S          | 1 +
 src/arch/x86/core/patch_cf.S                | 2 +-
 src/arch/x86/core/stack.S                   | 1 -
 src/arch/x86/core/stack16.S                 | 1 -
 src/arch/x86/drivers/net/undiisr.S          | 2 +-
 src/arch/x86/interface/pcbios/e820mangler.S | 2 +-
 src/arch/x86/interface/pxe/pxe_entry.S      | 1 +
 src/arch/x86/prefix/bootpart.S              | 2 +-
 src/arch/x86/prefix/dskprefix.S             | 2 +-
 src/arch/x86/prefix/exeprefix.S             | 2 +-
 src/arch/x86/prefix/hdprefix.S              | 2 +-
 src/arch/x86/prefix/libprefix.S             | 1 +
 src/arch/x86/prefix/lkrnprefix.S            | 2 +-
 src/arch/x86/prefix/mbr.S                   | 2 +-
 src/arch/x86/prefix/mromprefix.S            | 2 +-
 src/arch/x86/prefix/nbiprefix.S             | 2 +-
 src/arch/x86/prefix/nullprefix.S            | 2 +-
 src/arch/x86/prefix/pxeprefix.S             | 2 +-
 src/arch/x86/prefix/rawprefix.S             | 2 +-
 src/arch/x86/prefix/unlzma.S                | 1 +
 src/arch/x86/prefix/usbdisk.S               | 2 +-
 src/arch/x86/transitions/liba20.S           | 1 +
 src/arch/x86/transitions/libkir.S           | 2 +-
 24 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/roms/ipxe/src/arch/i386/core/setjmp.S b/roms/ipxe/src/arch/i386/core/setjmp.S
index e0bbb7ef8518dc6de32b14604816..cbb5e7138eeefe91aa5a14728f3b 100644
--- a/roms/ipxe/src/arch/i386/core/setjmp.S
+++ b/roms/ipxe/src/arch/i386/core/setjmp.S
@@ -2,8 +2,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
 	.text
-	.arch i386
 	.code32
+	.arch i386
 
 	/* Must match jmp_buf structure layout */
 	.struct	0
diff --git a/roms/ipxe/src/arch/i386/tests/gdbstub_test.S b/roms/ipxe/src/arch/i386/tests/gdbstub_test.S
index e0c9e6c9a513a9978dde6ac69e85..e44c13c2da01b168f8f85c903380 100644
--- a/roms/ipxe/src/arch/i386/tests/gdbstub_test.S
+++ b/roms/ipxe/src/arch/i386/tests/gdbstub_test.S
@@ -1,4 +1,5 @@
 	.section ".note.GNU-stack", "", @progbits
+	.code32
 	.arch i386
 
 	.section ".data", "aw", @progbits
diff --git a/roms/ipxe/src/arch/x86/core/patch_cf.S b/roms/ipxe/src/arch/x86/core/patch_cf.S
index ebf62876902c7b2abc5e96f967fa..62f19e45fe9535b7842d02db8f81 100644
--- a/roms/ipxe/src/arch/x86/core/patch_cf.S
+++ b/roms/ipxe/src/arch/x86/core/patch_cf.S
@@ -23,8 +23,8 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 	.code16
+	.arch i386
 
 /****************************************************************************
  * Set/clear CF on the stack as appropriate, assumes stack is as it should
diff --git a/roms/ipxe/src/arch/x86/core/stack.S b/roms/ipxe/src/arch/x86/core/stack.S
index 5aabab58cbe047b2972148d4f3fb..a03b40bd6a3033176f2dcb4d8a02 100644
--- a/roms/ipxe/src/arch/x86/core/stack.S
+++ b/roms/ipxe/src/arch/x86/core/stack.S
@@ -1,7 +1,6 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 
 #ifdef __x86_64__
 #define STACK_SIZE 8192
diff --git a/roms/ipxe/src/arch/x86/core/stack16.S b/roms/ipxe/src/arch/x86/core/stack16.S
index 85b683bc5f13bfa6d3fa814d04b1..61751bd9dde3fe124e598e8b8e66 100644
--- a/roms/ipxe/src/arch/x86/core/stack16.S
+++ b/roms/ipxe/src/arch/x86/core/stack16.S
@@ -1,7 +1,6 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 
 /****************************************************************************
  * Internal stack
diff --git a/roms/ipxe/src/arch/x86/drivers/net/undiisr.S b/roms/ipxe/src/arch/x86/drivers/net/undiisr.S
index aa8991d77bc492a5aced68e0af1a..8ba5c53549bece8f1f0c79373963 100644
--- a/roms/ipxe/src/arch/x86/drivers/net/undiisr.S
+++ b/roms/ipxe/src/arch/x86/drivers/net/undiisr.S
@@ -11,8 +11,8 @@ FILE_LICENCE ( GPL2_OR_LATER )
 #define PIC2_ICR 0xa0
 	
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 	.code16
+	.arch i386
 
 	.section ".text16", "ax", @progbits
 	.globl undiisr
diff --git a/roms/ipxe/src/arch/x86/interface/pcbios/e820mangler.S b/roms/ipxe/src/arch/x86/interface/pcbios/e820mangler.S
index 8d782dc4ba0652dd94bad2d3275b..cd8f0c70cdb2b5824c60be17668c 100644
--- a/roms/ipxe/src/arch/x86/interface/pcbios/e820mangler.S
+++ b/roms/ipxe/src/arch/x86/interface/pcbios/e820mangler.S
@@ -24,8 +24,8 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 	.code16
+	.arch i386
 
 #define SMAP 0x534d4150
 
diff --git a/roms/ipxe/src/arch/x86/interface/pxe/pxe_entry.S b/roms/ipxe/src/arch/x86/interface/pxe/pxe_entry.S
index 15ae82069201cba1192150aa23cb..22007b08aa208057243d78ad0b71 100644
--- a/roms/ipxe/src/arch/x86/interface/pxe/pxe_entry.S
+++ b/roms/ipxe/src/arch/x86/interface/pxe/pxe_entry.S
@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #include <librm.h>
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 
 /****************************************************************************
diff --git a/roms/ipxe/src/arch/x86/prefix/bootpart.S b/roms/ipxe/src/arch/x86/prefix/bootpart.S
index cf75ff79359cd6ee8f0b03abe49b..7b9920fdbe9bbcb35bdc99b2c3f4 100644
--- a/roms/ipxe/src/arch/x86/prefix/bootpart.S
+++ b/roms/ipxe/src/arch/x86/prefix/bootpart.S
@@ -6,9 +6,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #define STACK_SIZE	0x2000
 	
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.section ".prefix", "awx", @progbits
-	.code16
 
 /*
  * Find active partition
diff --git a/roms/ipxe/src/arch/x86/prefix/dskprefix.S b/roms/ipxe/src/arch/x86/prefix/dskprefix.S
index f9d301956ae2d6abb664a2adc9f8..e8e55ef1dc876a92642026ce923d 100644
--- a/roms/ipxe/src/arch/x86/prefix/dskprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/dskprefix.S
@@ -26,9 +26,9 @@ FILE_LICENCE ( GPL2_ONLY )
 
 	.section ".note.GNU-stack", "", @progbits
 	.org	0
+	.code16
 	.arch i386
 	.section ".prefix", "ax", @progbits
-	.code16
 	.globl	_dsk_start
 _dsk_start:
 
diff --git a/roms/ipxe/src/arch/x86/prefix/exeprefix.S b/roms/ipxe/src/arch/x86/prefix/exeprefix.S
index 773cccf3a982fa2ccbca5846072b..76ca39b4a26acc7201c07b7b7662 100644
--- a/roms/ipxe/src/arch/x86/prefix/exeprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/exeprefix.S
@@ -37,9 +37,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #define PSP_CMDLINE_START 0x81
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.org 0
-	.code16
 	.section ".prefix", "awx", @progbits
 
 signature:
diff --git a/roms/ipxe/src/arch/x86/prefix/hdprefix.S b/roms/ipxe/src/arch/x86/prefix/hdprefix.S
index 1bbf72dd283c4926feb21154f234..3133dec6c6d6e92c16043041cd2c 100644
--- a/roms/ipxe/src/arch/x86/prefix/hdprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/hdprefix.S
@@ -3,9 +3,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #include <librm.h>
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.section ".prefix", "awx", @progbits
-	.code16
 	.org 0
 	.globl	_hd_start
 _hd_start:
diff --git a/roms/ipxe/src/arch/x86/prefix/libprefix.S b/roms/ipxe/src/arch/x86/prefix/libprefix.S
index 0445a35d75a50adcac7139135e3a..4049eea2a6176dfacece4c839987 100644
--- a/roms/ipxe/src/arch/x86/prefix/libprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/libprefix.S
@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #include <librm.h>
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 
 /* Image compression enabled */
diff --git a/roms/ipxe/src/arch/x86/prefix/lkrnprefix.S b/roms/ipxe/src/arch/x86/prefix/lkrnprefix.S
index 9f1a2f09bf147834160297a46f87..c8a04c9d78eb73b7778d9b0e98a1 100644
--- a/roms/ipxe/src/arch/x86/prefix/lkrnprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/lkrnprefix.S
@@ -5,8 +5,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #define BZI_LOAD_HIGH_ADDR 0x100000
 
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 	.code16
+	.arch i386
 	.section ".prefix", "ax", @progbits
 	.globl	_lkrn_start
 _lkrn_start:
diff --git a/roms/ipxe/src/arch/x86/prefix/mbr.S b/roms/ipxe/src/arch/x86/prefix/mbr.S
index b37eed714bfb38d8f0408c9218da..5e0ed5ddb5c2ceca594f1f0bcbcb 100644
--- a/roms/ipxe/src/arch/x86/prefix/mbr.S
+++ b/roms/ipxe/src/arch/x86/prefix/mbr.S
@@ -1,9 +1,9 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.section ".prefix", "awx", @progbits
-	.code16
 	.org 0
 
 	.globl mbr
diff --git a/roms/ipxe/src/arch/x86/prefix/mromprefix.S b/roms/ipxe/src/arch/x86/prefix/mromprefix.S
index 0b2a0d2a64073477f60f32877023..df44643406f03ce9f4286a3b1bb0 100644
--- a/roms/ipxe/src/arch/x86/prefix/mromprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/mromprefix.S
@@ -42,8 +42,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #include "pciromprefix.S"
 
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 	.code16
+	.arch i386
 
 /* Obtain access to payload by exposing the expansion ROM BAR at the
  * address currently used by a suitably large memory BAR on the same
diff --git a/roms/ipxe/src/arch/x86/prefix/nbiprefix.S b/roms/ipxe/src/arch/x86/prefix/nbiprefix.S
index 0a74ca718e2a8a546558cfb96d5d..bbacd4b7b86c86a2f2c390d094ee 100644
--- a/roms/ipxe/src/arch/x86/prefix/nbiprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/nbiprefix.S
@@ -3,8 +3,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #include <librm.h>
 
 	.section ".note.GNU-stack", "", @progbits
-	.arch i386
 	.code16
+	.arch i386
 	.section ".prefix", "ax", @progbits
 	.org 0
 
diff --git a/roms/ipxe/src/arch/x86/prefix/nullprefix.S b/roms/ipxe/src/arch/x86/prefix/nullprefix.S
index bbc697fd292f0427fb4d8bd7e93b..426f1f2ce4e4d88c600b3dab3b54 100644
--- a/roms/ipxe/src/arch/x86/prefix/nullprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/nullprefix.S
@@ -2,10 +2,10 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
 	.org	0
+	.code16
 	.arch i386
 
 	.section ".prefix", "ax", @progbits
-	.code16
 _prefix:
 
 	.section ".text16", "ax", @progbits
diff --git a/roms/ipxe/src/arch/x86/prefix/pxeprefix.S b/roms/ipxe/src/arch/x86/prefix/pxeprefix.S
index c62a5011755ce6b4f8a790474ae0..5181ef6184516cc6773da0dc21c9 100644
--- a/roms/ipxe/src/arch/x86/prefix/pxeprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/pxeprefix.S
@@ -12,9 +12,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #define PXE_HACK_EB54			0x0001
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.org 0
-	.code16
 
 #include <librm.h>
 #include <undi.h>
diff --git a/roms/ipxe/src/arch/x86/prefix/rawprefix.S b/roms/ipxe/src/arch/x86/prefix/rawprefix.S
index d97b3b512bc743506a05a470ec64..962c97187806db65b86ea0e5ec91 100644
--- a/roms/ipxe/src/arch/x86/prefix/rawprefix.S
+++ b/roms/ipxe/src/arch/x86/prefix/rawprefix.S
@@ -9,9 +9,9 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.org 0
-	.code16
 
 #include <librm.h>
 
diff --git a/roms/ipxe/src/arch/x86/prefix/unlzma.S b/roms/ipxe/src/arch/x86/prefix/unlzma.S
index 9b3ecf8a64461ce074e43caef971..104842ed8050606b30cb4f652d2e 100644
--- a/roms/ipxe/src/arch/x86/prefix/unlzma.S
+++ b/roms/ipxe/src/arch/x86/prefix/unlzma.S
@@ -44,6 +44,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  */
 
 	.section ".note.GNU-stack", "", @progbits
+	.code32
 	.arch i486
 	.section ".prefix.lib", "ax", @progbits
 
diff --git a/roms/ipxe/src/arch/x86/prefix/usbdisk.S b/roms/ipxe/src/arch/x86/prefix/usbdisk.S
index 43f7632d54731edd7e84bccb6a3b..a4555881b531b5b9c3870a7a8103 100644
--- a/roms/ipxe/src/arch/x86/prefix/usbdisk.S
+++ b/roms/ipxe/src/arch/x86/prefix/usbdisk.S
@@ -1,9 +1,9 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.section ".prefix", "awx", @progbits
-	.code16
 	.org 0
 
 #include "mbr.S"
diff --git a/roms/ipxe/src/arch/x86/transitions/liba20.S b/roms/ipxe/src/arch/x86/transitions/liba20.S
index f7245e0baf3e985f18bab8c13128..cd7ebd6a60bb5101ee0b411ffad4 100644
--- a/roms/ipxe/src/arch/x86/transitions/liba20.S
+++ b/roms/ipxe/src/arch/x86/transitions/liba20.S
@@ -25,6 +25,7 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 
 /****************************************************************************
diff --git a/roms/ipxe/src/arch/x86/transitions/libkir.S b/roms/ipxe/src/arch/x86/transitions/libkir.S
index 5909654c2fa07c0ff20fafe00b6a..2c4dc948b477462c553735a2a7ad 100644
--- a/roms/ipxe/src/arch/x86/transitions/libkir.S
+++ b/roms/ipxe/src/arch/x86/transitions/libkir.S
@@ -32,9 +32,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
 #define BOCHSBP xchgw %bx, %bx
 
 	.section ".note.GNU-stack", "", @progbits
+	.code16
 	.arch i386
 	.section ".text16", "awx", @progbits
-	.code16
 	
 /****************************************************************************
  * init_libkir (real-mode or 16:xx protected-mode far call)
