arrfab / rpms / shim

Forked from rpms/shim 4 years ago
Clone

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

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