Blame SOURCES/0061-Do-the-same-for-ia32.patch

e97c83
From 6744a7ef8eca44948565c3d1244ec931ed3f6fee Mon Sep 17 00:00:00 2001
e97c83
From: Peter Jones <pjones@redhat.com>
e97c83
Date: Sun, 21 Sep 2014 13:11:11 -0400
e97c83
Subject: [PATCH 61/74] Do the same for ia32...
e97c83
e97c83
Once again, on ia32 this time, we see:
e97c83
e97c83
00000120  47 84 00 00 0a 00 00 00  00 00 00 00 00 00 00 00 |G...............|
e97c83
e97c83
Which is where the pointer on ia32 for the Base Relocation Table should
e97c83
be.  It points to 0x8447, which isn't a particularly reasonable address as
e97c83
numbers go, and happens to have this data there:
e97c83
e97c83
00008440  6f 00 6e 00 66 00 69 00  67 00 75 00 72 00 65 00 |o.n.f.i.g.u.r.e.|
e97c83
00008450  00 00 49 00 50 00 76 00  36 00 28 00 00 00 2c 00 |..I.P.v.6.(...,.|
e97c83
00008460  25 00 73 00 2c 00 00 00  29 00 00 00 25 00 64 00 |%.s.,...)...%.d.|
e97c83
00008470  2e 00 25 00 64 00 2e 00  25 00 64 00 2e 00 25 00 |..%.d...%.d...%.|
e97c83
00008480  64 00 00 00 44 00 48 00  43 00 50 00 00 00 49 00 |d...D.H.C.P...I.|
e97c83
00008490  50 00 76 00 34 00 28 00  00 00 2c 00 25 00 73 00 |P.v.4.(...,.%.s.|
e97c83
e97c83
And so that table is, in theory, this part:
e97c83
e97c83
00008447                       00  67 00 75 00 72 00 65 00 |       .g.u.r.e.|
e97c83
00008450  00                                               |.               |
e97c83
e97c83
Which is pretty clearly not a pointer table of any kind.
e97c83
e97c83
So give ia32 the same treatment as x86_64, and now all arches work basically
e97c83
the same.
e97c83
e97c83
Signed-off-by: Peter Jones <pjones@redhat.com>
e97c83
---
e97c83
 Makefile         |  22 +++++--
e97c83
 crt0-efi-ia32.S  | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
e97c83
 elf_ia32_efi.lds |  83 ++++++++++++-------------
e97c83
 3 files changed, 236 insertions(+), 49 deletions(-)
e97c83
 create mode 100644 crt0-efi-ia32.S
e97c83
e97c83
diff --git a/Makefile b/Makefile
e97c83
index d5fd55b..a52984f 100644
e97c83
--- a/Makefile
e97c83
+++ b/Makefile
e97c83
@@ -6,19 +6,25 @@ ARCH		= $(shell $(CC) -dumpmachine | cut -f1 -d- | sed s,i[3456789]86,ia32,)
e97c83
 
e97c83
 SUBDIRS		= Cryptlib lib
e97c83
 
e97c83
-LIB_PATH	= /usr/lib64
e97c83
-
e97c83
 EFI_INCLUDE	:= /usr/include/efi
e97c83
 EFI_INCLUDES	= -nostdinc -ICryptlib -ICryptlib/Include -I$(EFI_INCLUDE) -I$(EFI_INCLUDE)/$(ARCH) -I$(EFI_INCLUDE)/protocol -Iinclude
e97c83
-EFI_PATH	:= /usr/lib64/gnuefi
e97c83
+ifeq ($(ARCH),ia32)
e97c83
+LIB_PATH	:= /usr/lib
e97c83
+EFI_PATH	:= /usr/lib/gnuefi
e97c83
+endif
e97c83
+LIB_PATH	?= /usr/lib64
e97c83
+EFI_PATH	?= /usr/lib64/gnuefi
e97c83
 
e97c83
 LIB_GCC		= $(shell $(CC) -print-libgcc-file-name)
e97c83
 EFI_LIBS	= -lefi -lgnuefi --start-group Cryptlib/libcryptlib.a Cryptlib/OpenSSL/libopenssl.a --end-group $(LIB_GCC) 
e97c83
 
e97c83
 ifeq ($(ARCH),x86_64)
e97c83
 EFI_CRT_OBJS	:= crt0-efi-$(ARCH).o
e97c83
-endif
e97c83
+else ifeq ($(ARCH),ia32)
e97c83
+EFI_CRT_OBJS	:= crt0-efi-$(ARCH).o
e97c83
+else
e97c83
 EFI_CRT_OBJS 	?= $(EFI_PATH)/crt0-efi-$(ARCH).o
e97c83
+endif
e97c83
 EFI_LDS		= elf_$(ARCH)_efi.lds
e97c83
 
e97c83
 DEFAULT_LOADER	:= \\\\grub.efi
e97c83
@@ -137,9 +143,15 @@ SUBSYSTEM	:= 0xa
e97c83
 LDFLAGS		+= --defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
e97c83
 endif
e97c83
 
e97c83
+ifeq ($(ARCH),ia32)
e97c83
+FORMAT		:= -O binary
e97c83
+SUBSYSTEM	:= 0xa
e97c83
+LDFLAGS		+= --defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
e97c83
+endif
e97c83
+
e97c83
 FORMAT		?= --target efi-app-$(ARCH)
e97c83
 
e97c83
-crt0-efi-x86_64.o : crt0-efi-x86_64.S
e97c83
+crt0-efi-$(ARCH).o : crt0-efi-$(ARCH).S
e97c83
 	$(CC) $(CFLAGS) -DEFI_SUBSYSTEM=$(SUBSYSTEM) -c -o $@ $<
e97c83
 
e97c83
 %.efi: %.so
e97c83
diff --git a/crt0-efi-ia32.S b/crt0-efi-ia32.S
e97c83
new file mode 100644
e97c83
index 0000000..70b5b44
e97c83
--- /dev/null
e97c83
+++ b/crt0-efi-ia32.S
e97c83
@@ -0,0 +1,180 @@
e97c83
+/* crt0-efi-x86_64.S - x86_64 EFI startup code.
e97c83
+ *
e97c83
+ * Copyright 2014 Red Hat, Inc. <pjones@redhat.com>
e97c83
+ * Redistribution and use in source and binary forms, with or without
e97c83
+ * modification, are permitted provided that the following conditions
e97c83
+ * are met:
e97c83
+ *
e97c83
+ * Redistributions of source code must retain the above copyright
e97c83
+ * notice, this list of conditions and the following disclaimer.
e97c83
+ *
e97c83
+ * Redistributions in binary form must reproduce the above copyright
e97c83
+ * notice, this list of conditions and the following disclaimer in the
e97c83
+ * documentation and/or other materials provided with the
e97c83
+ * distribution.
e97c83
+ *
e97c83
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e97c83
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
e97c83
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
e97c83
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
e97c83
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
e97c83
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
e97c83
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
e97c83
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e97c83
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
e97c83
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e97c83
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
e97c83
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
e97c83
+ */
e97c83
+	.section	.text.head
e97c83
+
e97c83
+	/*
e97c83
+	 * Magic "MZ" signature for PE/COFF
e97c83
+	 */
e97c83
+	.globl	ImageBase
e97c83
+ImageBase:
e97c83
+	.ascii	"MZ"
e97c83
+	.skip	58				// 'MZ' + pad + offset == 64
e97c83
+	.long	pe_header - ImageBase		// Offset to the PE header.
e97c83
+	.long	0x0eba1f0e			/* terrifying code */
e97c83
+	.long	0xcd09b400			/* terrifying code */
e97c83
+	.long	0x4c01b821			/* terrifying code */
e97c83
+	.short	0x21cd				/* terrfiying code */
e97c83
+	.ascii	"The only winning move is not to play.\r\r\n$" /* DOS text */
e97c83
+	.skip	9
e97c83
+pe_header:
e97c83
+	.ascii	"PE"
e97c83
+	.short 	0
e97c83
+coff_header:
e97c83
+	.short	0x014c				// i386
e97c83
+	.short	1				// nr_sections
e97c83
+	.long	0 				// TimeDateStamp
e97c83
+	.long	0				// PointerToSymbolTable
e97c83
+	.long	0				// NumberOfSymbols
e97c83
+	.short	section_table - optional_header	// SizeOfOptionalHeader
e97c83
+	.short	0x306				// Characteristics.
e97c83
+						// IMAGE_FILE_DEBUG_STRIPPED |
e97c83
+						// IMAGE_FILE_EXECUTABLE_IMAGE |
e97c83
+						// IMAGE_FILE_LINE_NUMS_STRIPPED
e97c83
+						// | IMAGE_FILE_32BIT_MACHINE
e97c83
+optional_header:
e97c83
+	.short	0x10b				// PE32+ format
e97c83
+	.byte	0x02				// MajorLinkerVersion
e97c83
+	.byte	0x18				// MinorLinkerVersion
e97c83
+	.long	_edata - _start			// SizeOfCode
e97c83
+	.long	0				// SizeOfInitializedData
e97c83
+	.long	0				// SizeOfUninitializedData
e97c83
+	.long	_start - ImageBase		// AddressOfEntryPoint
e97c83
+	.long	_start - ImageBase		// BaseOfCode
e97c83
+	.long	0				// BaseOfData
e97c83
+
e97c83
+extra_header_fields:
e97c83
+	.long	0				// ImageBase
e97c83
+	.long	0x20				// SectionAlignment
e97c83
+	.long	0x8				// FileAlignment
e97c83
+	.short	0				// MajorOperatingSystemVersion
e97c83
+	.short	0				// MinorOperatingSystemVersion
e97c83
+	.short	0				// MajorImageVersion
e97c83
+	.short	0				// MinorImageVersion
e97c83
+	.short	0				// MajorSubsystemVersion
e97c83
+	.short	0				// MinorSubsystemVersion
e97c83
+	.long	0				// Win32VersionValue
e97c83
+
e97c83
+	.long	_edata - ImageBase		// SizeOfImage
e97c83
+
e97c83
+	// Everything before the kernel image is considered part of the header
e97c83
+	.long	_start - ImageBase		// SizeOfHeaders
e97c83
+	.long	0				// CheckSum
e97c83
+	.short	EFI_SUBSYSTEM			// Subsystem
e97c83
+	.short	0				// DllCharacteristics
e97c83
+	.long	0				// SizeOfStackReserve
e97c83
+	.long	0				// SizeOfStackCommit
e97c83
+	.long	0				// SizeOfHeapReserve
e97c83
+	.long	0				// SizeOfHeapCommit
e97c83
+	.long	0				// LoaderFlags
e97c83
+	.long	0x10				// NumberOfRvaAndSizes
e97c83
+
e97c83
+	.quad	0				// ExportTable
e97c83
+	.quad	0				// ImportTable
e97c83
+	.quad	0				// ResourceTable
e97c83
+	.quad	0				// ExceptionTable
e97c83
+	.quad	0				// CertificationTable
e97c83
+	.quad	0				// BaseRelocationTable
e97c83
+	.quad	0				// DebugTable
e97c83
+	.quad	0				// ArchTable
e97c83
+	.quad	0				// GlobalPointerTable
e97c83
+	.quad	0				// .tls
e97c83
+	.quad	0				// LoadConfigTable
e97c83
+	.quad	0				// BoundImportsTable
e97c83
+	.quad	0				// ImportAddressTable
e97c83
+	.quad	0				// DelayLoadImportTable
e97c83
+	.quad	0				// ClrRuntimeHeader (.cor)
e97c83
+	.quad	0				// Reserved
e97c83
+
e97c83
+	// Section table
e97c83
+section_table:
e97c83
+	.ascii	".text"
e97c83
+	.byte	0
e97c83
+	.byte	0
e97c83
+	.byte	0			// end of 0 padding of section name
e97c83
+
e97c83
+	.long	_edata - _start		// VirtualSize
e97c83
+	.long	_start - ImageBase	// VirtualAddress
e97c83
+	.long	_edata - _start		// SizeOfRawData
e97c83
+	.long	_start - ImageBase	// PointerToRawData
e97c83
+	.long	0		// PointerToRelocations (0 for executables)
e97c83
+	.long	0		// PointerToLineNumbers (0 for executables)
e97c83
+	.short	0		// NumberOfRelocations  (0 for executables)
e97c83
+	.short	0		// NumberOfLineNumbers  (0 for executables)
e97c83
+	.long	0x60500020	// Characteristics (section flags)
e97c83
+
e97c83
+	/*
e97c83
+	 * The EFI application loader requires a relocation section
e97c83
+	 * because EFI applications must be relocatable.  This is a
e97c83
+	 * dummy section as far as we are concerned.
e97c83
+	 */
e97c83
+	.ascii	".reloc"
e97c83
+	.byte	0
e97c83
+	.byte	0			// end of 0 padding of section name
e97c83
+
e97c83
+	.long	0			// VirtualSize
e97c83
+	.long	0			// VirtualAddress
e97c83
+	.long	0			// SizeOfRawData
e97c83
+	.long	0			// PointerToRawData
e97c83
+	.long	0			// PointerToRelocations
e97c83
+	.long	0			// PointerToLineNumbers
e97c83
+	.short	0			// NumberOfRelocations
e97c83
+	.short	0			// NumberOfLineNumbers
e97c83
+	.long	0x42100040		// Characteristics (section flags)
e97c83
+
e97c83
+	/* most if not all ia32 binaries binutils makes seem to have .text
e97c83
+	 * starting at 0x400; no reason to assume that's a bad idea. */
e97c83
+	.align 1024
e97c83
+
e97c83
+_start:
e97c83
+	pushl %ebp
e97c83
+	movl %esp,%ebp
e97c83
+
e97c83
+	pushl 12(%ebp)			# copy "image" argument
e97c83
+	pushl  8(%ebp)			# copy "systab" argument
e97c83
+
e97c83
+	call 0f
e97c83
+0:	popl %eax
e97c83
+	movl %eax,%ebx
e97c83
+
e97c83
+	addl $ImageBase-0b,%eax		# %eax = ldbase
e97c83
+	addl $_DYNAMIC-0b,%ebx		# %ebx = _DYNAMIC
e97c83
+
e97c83
+	pushl %ebx			# pass _DYNAMIC as second argument
e97c83
+	pushl %eax			# pass ldbase as first argument
e97c83
+	call _relocate
e97c83
+	popl %ebx
e97c83
+	popl %ebx
e97c83
+ 	testl %eax,%eax
e97c83
+ 	jne .exit
e97c83
+  
e97c83
+  	call efi_main			# call app with "image" and "systab" argument
e97c83
+
e97c83
+.exit:
e97c83
+	leave
e97c83
+  	ret
e97c83
diff --git a/elf_ia32_efi.lds b/elf_ia32_efi.lds
e97c83
index 12d4085..b649e15 100644
e97c83
--- a/elf_ia32_efi.lds
e97c83
+++ b/elf_ia32_efi.lds
e97c83
@@ -3,61 +3,56 @@ OUTPUT_ARCH(i386)
e97c83
 ENTRY(_start)
e97c83
 SECTIONS
e97c83
 {
e97c83
-  . = 0;
e97c83
-  ImageBase = .;
e97c83
-  .hash : { *(.hash) }	/* this MUST come first! */
e97c83
-  . = ALIGN(4096);
e97c83
-  .text :
e97c83
-  {
e97c83
-   *(.text)
e97c83
-   *(.text.*)
e97c83
-   *(.gnu.linkonce.t.*)
e97c83
-  }
e97c83
-  .reloc :
e97c83
-  {
e97c83
-   *(.reloc)
e97c83
+  .text 0x0 : {
e97c83
+    *(.text.head)
e97c83
+    *(.text)
e97c83
+    *(.text.*)
e97c83
+    *(.gnu.linkonce.t.*)
e97c83
+    *(.srodata)
e97c83
+    *(.rodata*)
e97c83
+    . = ALIGN(16);
e97c83
+    _etext = .;
e97c83
   }
e97c83
-  . = ALIGN(4096);
e97c83
+  .dynamic : { *(.dynamic) }
e97c83
   .data :
e97c83
   {
e97c83
-   *(.rodata*)
e97c83
-   *(.data)
e97c83
-   *(.data1)
e97c83
-   *(.data.*)
e97c83
-   *(.sdata)
e97c83
-   *(.got.plt)
e97c83
-   *(.got)
e97c83
-   /* the EFI loader doesn't seem to like a .bss section, so we stick
e97c83
-      it all into .data: */
e97c83
-   *(.sbss)
e97c83
-   *(.scommon)
e97c83
-   *(.dynbss)
e97c83
-   *(.bss)
e97c83
-   *(COMMON)
e97c83
+    *(.sdata)
e97c83
+    *(.data)
e97c83
+    *(.data1)
e97c83
+    *(.data.*)
e97c83
+    *(.got.plt)
e97c83
+    *(.got)
e97c83
+
e97c83
+    /* the EFI loader doesn't seem to like a .bss section, so we stick
e97c83
+     * it all into .data: */
e97c83
+    . = ALIGN(16);
e97c83
+    _bss = .;
e97c83
+    *(.sbss)
e97c83
+    *(.scommon)
e97c83
+    *(.dynbss)
e97c83
+    *(.bss)
e97c83
+    *(COMMON)
e97c83
+    . = ALIGN(16);
e97c83
+    _bss_end = .;
e97c83
   }
e97c83
   . = ALIGN(4096);
e97c83
   .vendor_cert :
e97c83
   {
e97c83
-   *(.vendor_cert)
e97c83
+    *(.vendor_cert)
e97c83
   }
e97c83
+
e97c83
   . = ALIGN(4096);
e97c83
-  .dynamic  : { *(.dynamic) }
e97c83
-  . = ALIGN(4096);
e97c83
-  .rel :
e97c83
-  {
e97c83
-    *(.rel.data)
e97c83
-    *(.rel.data.*)
e97c83
-    *(.rel.got)
e97c83
-    *(.rel.stab)
e97c83
-    *(.data.rel.ro.local)
e97c83
-    *(.data.rel.local)
e97c83
-    *(.data.rel.ro)
e97c83
-    *(.data.rel*)
e97c83
-  }
e97c83
+  .rel.dyn : { *(.rel.dyn) }
e97c83
+  .rel.plt : { *(.rel.plt) }
e97c83
+  .rel.got : { *(.rel.got) }
e97c83
+  .rel.data : { *(.rel.data) *(.rel.data*) }
e97c83
+  _edata = .;
e97c83
+  _data_size = . - _etext;
e97c83
+
e97c83
   . = ALIGN(4096);
e97c83
-  .dynsym   : { *(.dynsym) }
e97c83
+  .dynsym : { *(.dynsym) }
e97c83
   . = ALIGN(4096);
e97c83
-  .dynstr   : { *(.dynstr) }
e97c83
+  .dynstr : { *(.dynstr) }
e97c83
   . = ALIGN(4096);
e97c83
   /DISCARD/ :
e97c83
   {
e97c83
-- 
e97c83
1.9.3
e97c83