tests: Fix up to work on arbitrary architectures

The current test infrastructure is tied to x86/amd64.  This means the
tests always fail on a non-x86 architecture (like aarch64).  Fix this
by generating the efi binary directly from C code and removing the
architectural restrictions in the Makefile.am.  One of the
consequences of this is that we no longer test ia32 on x86_64, but the
difficulty of detecting which architectures can support 32 bit
variants and generating them correctly from EFI c code is too great.

We also need to exclude tests involving objdump from aarch64 since its
bfd still doesn't have an efi_app_aarch64 target.

Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
diff --git a/configure.ac b/configure.ac
index cde2322..7f92979 100644
--- a/configure.ac
+++ b/configure.ac
@@ -65,6 +65,20 @@
 
 dnl gnu-efi headers require extra include dirs
 EFI_ARCH=$(uname -m | sed 's/i.86/ia32/;s/arm.*/arm/')
+AM_CONDITIONAL(TEST_BINARY_FORMAT, [ test "$EFI_ARCH" = "arm" -o "$EFI_ARCH" = "aarch64" ])
+
+##
+# no consistent view of where gnu-efi should dump the efi stuff, so find it
+##
+for path in /lib /lib64 /usr/lib /usr/lib64 /usr/lib32 /lib/efi /lib64/efi /usr/lib/efi /usr/lib64/efi; do
+    if test -e $path/crt0-efi-$EFI_ARCH.o; then
+       CRTPATH=$path
+    fi
+done
+if test -z "$CRTPATH"; then
+   AC_MSG_ERROR([cannot find the gnu-efi crt path])
+fi
+
 EFI_CPPFLAGS="-I/usr/include/efi -I/usr/include/efi/$EFI_ARCH \
  -DEFI_FUNCTION_WRAPPER"
 CPPFLAGS_save="$CPPFLAGS"
@@ -72,6 +86,8 @@
 AC_CHECK_HEADERS([efi.h], [], [], $EFI_INCLUDES)
 CPPFLAGS="$CPPFLAGS_save"
 AC_SUBST(EFI_CPPFLAGS, $EFI_CPPFLAGS)
+AC_SUBST(EFI_ARCH, $EFI_ARCH)
+AC_SUBST(CRTPATH, $CRTPATH)
 
 AC_CONFIG_FILES([Makefile src/Makefile lib/ccan/Makefile]
 		[docs/Makefile tests/Makefile])
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c5b008c..bc7f741 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -3,38 +3,33 @@
 
 test_key = private-key.rsa
 test_cert = public-cert.pem
-test_arches = x86_64 i386
-test_images = test-x86_64.pecoff test-i386.pecoff
+test_arches = $(EFI_ARCH)
 
-check_PROGRAMS = test-x86_64.pecoff test-i386.pecoff
+check_PROGRAMS = test.pecoff
+
+# override the automake rule to say we build from .elf files
+test.pecoff$(EXEEXT): test.elf
+
+if TEST_BINARY_FORMAT
+EFILDFLAGS = --defsym=EFI_SUBSYSTEM=0x0a
+FORMAT = -O binary
+else
+FORMAT = --target=efi-app-$(EFI_ARCH)
+endif
 check_DATA = $(test_key) $(test_cert)
 check_SCRIPTS = test-wrapper.sh
 
-test_i386_pecoff_SOURCES = test.S
-test_x86_64_pecoff_SOURCES = test.S
-
-test-%.pecoff: test-%.elf
+.elf.pecoff:
+	echo "TEST ARCHES $(test_arches) TEST_COMPAT=$(TEST_COMPAT_FALSE)"
 	$(OBJCOPY) -j .text -j .sdata -j .data \
 		-j .dynamic -j .dynsym  -j .rel \
 		-j .rela -j .reloc \
-		--target=$(test_lds) $^ $@
-	$(STRIP) $@
+		$(FORMAT) $^ $@
 
-test-x86_64.pecoff: test_image_arch = x86-64
-test-x86_64.pecoff: test_lds = efi-app-x86_64
-test-x86_64.pecoff: ASFLAGS += -m64
-test-x86_64.pecoff: LDFLAGS += -m64
-test-i386.pecoff: test_image_arch = i386
-test-i386.pecoff: test_lds = efi-app-ia32
-test-i386.pecoff: ASFLAGS += -m32
-test-i386.pecoff: LDFLAGS += -m32
+.$(OBJEXT).elf:
+	$(LD) $(EFILDFLAGS) -nostdlib -L $(CRTPATH) -shared -Bsymbolic $(CRTPATH)/crt0-efi-$(EFI_ARCH).o -T elf_$(EFI_ARCH)_efi.lds $< -o $@ -lefi -lgnuefi
 
-test-%.elf: LDFLAGS += -nostdlib
-test-%.elf: test-%.$(OBJEXT)
-	$(LINK) $<
-
-test-%.$(OBJEXT): $(srcdir)/test.S
-	$(COMPILE.S) -o $@ $^
+AM_CFLAGS=-fpic -I/usr/include/efi -I/usr/include/efi/$(EFI_ARCH)
 
 $(test_key): Makefile
 	openssl genrsa -out $@ 2048
@@ -52,14 +47,22 @@
 	verify-missing-image.sh \
 	verify-missing-cert.sh \
 	sign-invalidattach-verify.sh \
-	cert-table-header.sh \
 	resign-warning.sh \
-	reattach-warning.sh \
-	detach-remove.sh
+	reattach-warning.sh
+
+if !TEST_BINARY_FORMAT
+##
+# These tests involve objdump which will fail because the format
+# is not recognised. Someone needs to fix arm bfd to add efi
+##
+TESTS +=	cert-table-header.sh \
+		detach-remove.sh
+endif
+
 
 TEST_EXTENSIONS = .sh
 AM_TESTS_ENVIRONMENT = TEST_ARCHES='$(test_arches)'; export TEST_ARCHES;
 SH_LOG_COMPILER = $(srcdir)/test-wrapper.sh
 
-EXTRA_DIST = $(test_lds) test.S $(TESTS) $(check_SCRIPTS)
-CLEANFILES = $(test_key) $(test_cert) $(test_images)
+EXTRA_DIST = test.S $(TESTS) $(check_SCRIPTS)
+CLEANFILES = $(test_key) $(test_cert)
diff --git a/tests/test-wrapper.sh b/tests/test-wrapper.sh
index d8d78e2..b9c6cf1 100755
--- a/tests/test-wrapper.sh
+++ b/tests/test-wrapper.sh
@@ -24,7 +24,7 @@
 	test="$1"
 
 	# image depends on the test arch
-	image="$datadir/test-$arch.pecoff"
+	image="$datadir/test.pecoff"
 	export image
 
 	# create the temporary directory...
diff --git a/tests/test.S b/tests/test.S
deleted file mode 100644
index 67384a6..0000000
--- a/tests/test.S
+++ /dev/null
@@ -1,9 +0,0 @@
-
-.text
-.globl _start
-_start:
-	nop
-
-.data
-data:
-	.long	0x0
diff --git a/tests/test.c b/tests/test.c
new file mode 100644
index 0000000..d6a5b9f
--- /dev/null
+++ b/tests/test.c
@@ -0,0 +1,8 @@
+#include <efi.h>
+#include <efilib.h>
+
+EFI_STATUS
+efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
+{
+	return EFI_SUCCESS;
+}