HACK: qemu: change e_machine for IAMCU binaries

For some strange reason IAMCU sets e_machine to 0x06 which causes
QEMU to freak out. This is just x86 code with a different C
calling convention, hack it back to 0x03 (EM_386) before running
under the emulator.

Change-Id: Ia5d51b771cad41f3013eb3d6a17912c8909c9bac
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
diff --git a/Makefile b/Makefile
index ee7ec53..b53eb51 100644
--- a/Makefile
+++ b/Makefile
@@ -1136,6 +1136,7 @@
 qemu: zephyr
 	$(if $(QEMU_PIPE),,@echo "To exit from QEMU enter: 'CTRL+a, x'")
 	@echo '[QEMU] CPU: $(QEMU_CPU_TYPE_$(ARCH))'
+	$(if $(CONFIG_X86_IAMCU),python $(ZEPHYR_BASE)/scripts/qemu-machine-hack.py $(KERNEL_ELF_NAME))
 	$(Q)$(QEMU) $(QEMU_FLAGS) $(QEMU_EXTRA_FLAGS) -kernel $(KERNEL_ELF_NAME)
 
 -include $(srctree)/boards/$(BOARD_NAME)/Makefile.board
diff --git a/scripts/qemu-machine-hack.py b/scripts/qemu-machine-hack.py
new file mode 100644
index 0000000..1412ed8
--- /dev/null
+++ b/scripts/qemu-machine-hack.py
@@ -0,0 +1,11 @@
+import sys
+
+# For some baffling reason IAMCU sets the instruction set architecture
+# in the ELF header to 0x06 instead of 0x03 even though it is just
+# 386 code. This gives QEMU fits. Hack it!
+fd = open(sys.argv[1], "r+b")
+fd.seek(0x12)
+# Write 0x03 which is EM_386 to e_machine
+fd.write(b'\x03')
+fd.close()
+