Blame SOURCES/0027-Make-ELF-constructors-and-destructors-work.patch

4c0d37
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4c0d37
From: Peter Jones <pjones@redhat.com>
4c0d37
Date: Fri, 23 Aug 2019 16:23:21 -0400
4c0d37
Subject: [PATCH] Make ELF constructors and destructors work.
4c0d37
4c0d37
This makes setup and teardown functions defined with
4c0d37
__attribute__((__constructor__) and __attribute__((__destructor__)) work
4c0d37
in normal circumstances in EFI binaries.
4c0d37
4c0d37
A couple of notes:
4c0d37
- it implements both the old-style .ctors/.dtors methods and the newer
4c0d37
  style .init_array/.fini_array ELF constructor and destructor arrays,
4c0d37
  processed in the order:
4c0d37
    .init_array[]
4c0d37
    .ctors[]
4c0d37
    efi_main()
4c0d37
    .dtors[]
4c0d37
    .fini_array[]
4c0d37
- Destructors will only be called if efi_main() exits using "return";
4c0d37
  any call to Exit() will still longjmp() past them.
4c0d37
- InitializeLib() has already been called before constructors run, so
4c0d37
  they don't need to call it (and neither does anything else.)  For
4c0d37
  compatibility, it has been altered so calling it more than once is
4c0d37
  safe.
4c0d37
- No attempt is made to handle any constructor or destructor with a
4c0d37
  prototype other than "void func(void);", but note that InitializeLib
4c0d37
  has been called, so LibImageHandle, ST, BS, and RT are set.
4c0d37
- The init_array/ctor/dtor/fini_array lists aren't the using the GNU
4c0d37
  "CONSTRUCTOR" output section command, so they don't start with a size.
4c0d37
- The lists are individually sorted during the link stage via
4c0d37
  SORT_BY_NAME() in the linker script.
4c0d37
- The default (empty) init_array/ctor/dtor/fini_array lists are padded
4c0d37
  out to 8-byte alignment with ".p2align 3, 0", and each list always has
4c0d37
  at least one ".long 0" at the end of it (even if it's completely
4c0d37
  empty).  As a result, they can have NULLs that need to be skipped.
4c0d37
  The sections they're in are mergeable, so the NULLs don't have to be
4c0d37
  exclusively at the end.
4c0d37
- The ia64 and mips64el arches have not been tested.
4c0d37
4c0d37
Signed-off-by: Peter Jones <pjones@redhat.com>
4c0d37
---
4c0d37
 apps/Makefile                |  5 ++-
4c0d37
 apps/ctors_fns.c             | 26 +++++++++++++
4c0d37
 apps/ctors_test.c            | 20 ++++++++++
4c0d37
 gnuefi/crt0-efi-aa64.S       |  2 +-
4c0d37
 gnuefi/crt0-efi-arm.S        |  2 +-
4c0d37
 gnuefi/crt0-efi-ia32.S       |  8 ++--
4c0d37
 gnuefi/crt0-efi-ia64.S       |  2 +-
4c0d37
 gnuefi/crt0-efi-mips64el.S   |  4 +-
4c0d37
 gnuefi/crt0-efi-x64.S        |  2 +-
4c0d37
 gnuefi/elf_aa64_efi.lds      | 21 +++++++++++
4c0d37
 gnuefi/elf_arm_efi.lds       | 20 ++++++++++
4c0d37
 gnuefi/elf_ia32_efi.lds      | 21 +++++++++++
4c0d37
 gnuefi/elf_ia32_fbsd_efi.lds | 21 +++++++++++
4c0d37
 gnuefi/elf_ia64_efi.lds      | 21 +++++++++++
4c0d37
 gnuefi/elf_mips64el_efi.lds  | 20 ++++++++++
4c0d37
 gnuefi/elf_x64_efi.lds       | 22 +++++++++++
4c0d37
 gnuefi/elf_x64_fbsd_efi.lds  | 21 +++++++++++
4c0d37
 lib/Makefile                 |  6 +--
4c0d37
 lib/ctors.S                  | 43 ++++++++++++++++++++++
4c0d37
 lib/entry.c                  | 67 ++++++++++++++++++++++++++++++++++
4c0d37
 lib/init.c                   | 87 +++++++++++++++++++++-----------------------
4c0d37
 21 files changed, 381 insertions(+), 60 deletions(-)
4c0d37
 create mode 100644 apps/ctors_fns.c
4c0d37
 create mode 100644 apps/ctors_test.c
4c0d37
 create mode 100644 lib/ctors.S
4c0d37
 create mode 100644 lib/entry.c
4c0d37
4c0d37
diff --git a/apps/Makefile b/apps/Makefile
4c0d37
index a95620cba2d..28088370da7 100644
4c0d37
--- a/apps/Makefile
4c0d37
+++ b/apps/Makefile
4c0d37
@@ -62,7 +62,8 @@ TARGET_APPS = t.efi t2.efi t3.efi t4.efi t5.efi t6.efi \
4c0d37
 	      printenv.efi t7.efi t8.efi tcc.efi modelist.efi \
4c0d37
 	      route80h.efi drv0_use.efi AllocPages.efi exit.efi \
4c0d37
 	      FreePages.efi setjmp.efi debughook.efi debughook.efi.debug \
4c0d37
-	      bltgrid.efi lfbgrid.efi setdbg.efi unsetdbg.efi
4c0d37
+	      bltgrid.efi lfbgrid.efi setdbg.efi unsetdbg.efi \
4c0d37
+	      ctors_test.efi
4c0d37
 TARGET_BSDRIVERS = drv0.efi
4c0d37
 TARGET_RTDRIVERS =
4c0d37
 
4c0d37
@@ -87,6 +88,8 @@ TARGETS = $(TARGET_APPS) $(TARGET_BSDRIVERS) $(TARGET_RTDRIVERS)
4c0d37
 
4c0d37
 all:	$(TARGETS)
4c0d37
 
4c0d37
+ctors_test.so : ctors_fns.o ctors_test.o
4c0d37
+
4c0d37
 clean:
4c0d37
 	rm -f $(TARGETS) *~ *.o *.so
4c0d37
 
4c0d37
diff --git a/apps/ctors_fns.c b/apps/ctors_fns.c
4c0d37
new file mode 100644
4c0d37
index 00000000000..624190462ea
4c0d37
--- /dev/null
4c0d37
+++ b/apps/ctors_fns.c
4c0d37
@@ -0,0 +1,26 @@
4c0d37
+/*
4c0d37
+ * ctors.c
4c0d37
+ * Copyright 2019 Peter Jones <pjones@redhat.com>
4c0d37
+ *
4c0d37
+ */
4c0d37
+
4c0d37
+#include <efi.h>
4c0d37
+#include <efilib.h>
4c0d37
+
4c0d37
+int constructed_value = 0;
4c0d37
+
4c0d37
+static void __attribute__((__constructor__)) ctor(void)
4c0d37
+{
4c0d37
+	Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
4c0d37
+	constructed_value = 1;
4c0d37
+	Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
4c0d37
+}
4c0d37
+
4c0d37
+static void __attribute__((__destructor__)) dtor(void)
4c0d37
+{
4c0d37
+	Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
4c0d37
+	constructed_value = 0;
4c0d37
+	Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
4c0d37
+}
4c0d37
+
4c0d37
+// vim:fenc=utf-8:tw=75:noet
4c0d37
diff --git a/apps/ctors_test.c b/apps/ctors_test.c
4c0d37
new file mode 100644
4c0d37
index 00000000000..7e48da8ef35
4c0d37
--- /dev/null
4c0d37
+++ b/apps/ctors_test.c
4c0d37
@@ -0,0 +1,20 @@
4c0d37
+/*
4c0d37
+ * ctors_test.c
4c0d37
+ * Copyright 2019 Peter Jones <pjones@redhat.com>
4c0d37
+ *
4c0d37
+ */
4c0d37
+
4c0d37
+#include <efi.h>
4c0d37
+#include <efilib.h>
4c0d37
+
4c0d37
+extern int constructed_value;
4c0d37
+
4c0d37
+EFI_STATUS
4c0d37
+efi_main (EFI_HANDLE image EFI_UNUSED, EFI_SYSTEM_TABLE *systab EFI_UNUSED)
4c0d37
+{
4c0d37
+	Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
4c0d37
+
4c0d37
+	return EFI_SUCCESS;
4c0d37
+}
4c0d37
+
4c0d37
+// vim:fenc=utf-8:tw=75:noet
4c0d37
diff --git a/gnuefi/crt0-efi-aa64.S b/gnuefi/crt0-efi-aa64.S
4c0d37
index d6e610b8c79..a9302588b71 100644
4c0d37
--- a/gnuefi/crt0-efi-aa64.S
4c0d37
+++ b/gnuefi/crt0-efi-aa64.S
4c0d37
@@ -124,7 +124,7 @@ _start:
4c0d37
 	cbnz		x0, 0f
4c0d37
 
4c0d37
 	ldp		x0, x1, [sp, #16]
4c0d37
-	bl		efi_main
4c0d37
+	bl		_entry
4c0d37
 
4c0d37
 0:	ldp		x29, x30, [sp], #32
4c0d37
 	ret
4c0d37
diff --git a/gnuefi/crt0-efi-arm.S b/gnuefi/crt0-efi-arm.S
4c0d37
index c5bb6d482da..04e75e9481a 100644
4c0d37
--- a/gnuefi/crt0-efi-arm.S
4c0d37
+++ b/gnuefi/crt0-efi-arm.S
4c0d37
@@ -136,7 +136,7 @@ _start:
4c0d37
 	bne		0f
4c0d37
 
4c0d37
 	ldmfd		sp, {r0-r1}
4c0d37
-	bl		efi_main
4c0d37
+	bl		_entry
4c0d37
 
4c0d37
 0:	add		sp, sp, #12
4c0d37
 	ldr		pc, [sp], #4
4c0d37
diff --git a/gnuefi/crt0-efi-ia32.S b/gnuefi/crt0-efi-ia32.S
4c0d37
index f9d5191ecb5..8e8e372f551 100644
4c0d37
--- a/gnuefi/crt0-efi-ia32.S
4c0d37
+++ b/gnuefi/crt0-efi-ia32.S
4c0d37
@@ -56,13 +56,13 @@ _start:
4c0d37
 	call _relocate
4c0d37
 	popl %ebx
4c0d37
 	popl %ebx
4c0d37
- 	testl %eax,%eax
4c0d37
- 	jne .exit
4c0d37
+	testl %eax,%eax
4c0d37
+	jne .exit
4c0d37
   
4c0d37
-  	call efi_main			# call app with "image" and "systab" argument
4c0d37
+	call _entry			# call app with "image" and "systab" argument
4c0d37
 
4c0d37
 .exit:	leave
4c0d37
-  	ret
4c0d37
+	ret
4c0d37
  
4c0d37
  	// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
4c0d37
  
4c0d37
diff --git a/gnuefi/crt0-efi-ia64.S b/gnuefi/crt0-efi-ia64.S
4c0d37
index 40c3c837a1c..dacb4c4c658 100644
4c0d37
--- a/gnuefi/crt0-efi-ia64.S
4c0d37
+++ b/gnuefi/crt0-efi-ia64.S
4c0d37
@@ -56,7 +56,7 @@ _start:
4c0d37
 
4c0d37
 	mov out0=in0			// image handle
4c0d37
 	mov out1=in1			// systab
4c0d37
-	br.call.sptk.few rp=efi_main
4c0d37
+	br.call.sptk.few rp=_entry
4c0d37
 .Lret2:
4c0d37
 .exit:
4c0d37
 	mov ar.pfs=loc0
4c0d37
diff --git a/gnuefi/crt0-efi-mips64el.S b/gnuefi/crt0-efi-mips64el.S
4c0d37
index 6a62aca98b4..5ad2503ca79 100644
4c0d37
--- a/gnuefi/crt0-efi-mips64el.S
4c0d37
+++ b/gnuefi/crt0-efi-mips64el.S
4c0d37
@@ -172,8 +172,8 @@ _pc:
4c0d37
 
4c0d37
 	// a0: ImageHandle
4c0d37
 	ld		$a0, 16($sp)
4c0d37
-	// call efi_main
4c0d37
-	dla		$t9, efi_main
4c0d37
+	// call _start
4c0d37
+	dla		$t9, _entry
4c0d37
 	jalr		$t9
4c0d37
 	// a1: SystemTable
4c0d37
 	ld		$a1, 24($sp)
4c0d37
diff --git a/gnuefi/crt0-efi-x64.S b/gnuefi/crt0-efi-x64.S
4c0d37
index 6533af7461f..3fe361b7ffd 100644
4c0d37
--- a/gnuefi/crt0-efi-x64.S
4c0d37
+++ b/gnuefi/crt0-efi-x64.S
4c0d37
@@ -56,7 +56,7 @@ _start:
4c0d37
 	popq %rdi
4c0d37
 	popq %rsi
4c0d37
 
4c0d37
-	call efi_main
4c0d37
+	call _entry
4c0d37
 	addq $8, %rsp
4c0d37
 
4c0d37
 .exit:	
4c0d37
diff --git a/gnuefi/elf_aa64_efi.lds b/gnuefi/elf_aa64_efi.lds
4c0d37
index 836d98255d8..161f5fc5641 100644
4c0d37
--- a/gnuefi/elf_aa64_efi.lds
4c0d37
+++ b/gnuefi/elf_aa64_efi.lds
4c0d37
@@ -26,6 +26,26 @@ SECTIONS
4c0d37
    *(.got.plt)
4c0d37
    *(.got)
4c0d37
 
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    . = ALIGN(16);
4c0d37
@@ -36,6 +56,7 @@ SECTIONS
4c0d37
    *(.bss)
4c0d37
    *(COMMON)
4c0d37
    . = ALIGN(16);
4c0d37
+
4c0d37
    _bss_end = .;
4c0d37
   }
4c0d37
 
4c0d37
diff --git a/gnuefi/elf_arm_efi.lds b/gnuefi/elf_arm_efi.lds
4c0d37
index 665bbdbf065..f93f39bc384 100644
4c0d37
--- a/gnuefi/elf_arm_efi.lds
4c0d37
+++ b/gnuefi/elf_arm_efi.lds
4c0d37
@@ -26,6 +26,26 @@ SECTIONS
4c0d37
    *(.got.plt)
4c0d37
    *(.got)
4c0d37
 
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    . = ALIGN(16);
4c0d37
diff --git a/gnuefi/elf_ia32_efi.lds b/gnuefi/elf_ia32_efi.lds
4c0d37
index f27fe5fc6e6..4b7e3f1df39 100644
4c0d37
--- a/gnuefi/elf_ia32_efi.lds
4c0d37
+++ b/gnuefi/elf_ia32_efi.lds
4c0d37
@@ -40,6 +40,27 @@ SECTIONS
4c0d37
    *(.sdata)
4c0d37
    *(.got.plt)
4c0d37
    *(.got)
4c0d37
+
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    *(.sbss)
4c0d37
diff --git a/gnuefi/elf_ia32_fbsd_efi.lds b/gnuefi/elf_ia32_fbsd_efi.lds
4c0d37
index cd309e24f7f..9e9baec2aca 100644
4c0d37
--- a/gnuefi/elf_ia32_fbsd_efi.lds
4c0d37
+++ b/gnuefi/elf_ia32_fbsd_efi.lds
4c0d37
@@ -40,6 +40,27 @@ SECTIONS
4c0d37
    *(.sdata)
4c0d37
    *(.got.plt)
4c0d37
    *(.got)
4c0d37
+
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    *(.sbss)
4c0d37
diff --git a/gnuefi/elf_ia64_efi.lds b/gnuefi/elf_ia64_efi.lds
4c0d37
index 190792a0c94..2cda0dd97c7 100644
4c0d37
--- a/gnuefi/elf_ia64_efi.lds
4c0d37
+++ b/gnuefi/elf_ia64_efi.lds
4c0d37
@@ -39,6 +39,27 @@ SECTIONS
4c0d37
    *(.data*)
4c0d37
    *(.gnu.linkonce.d*)
4c0d37
    *(.plabel)	/* data whose relocs we want to ignore */
4c0d37
+
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    *(.dynbss)
4c0d37
diff --git a/gnuefi/elf_mips64el_efi.lds b/gnuefi/elf_mips64el_efi.lds
4c0d37
index 4d1a077d8f8..0e68084d103 100644
4c0d37
--- a/gnuefi/elf_mips64el_efi.lds
4c0d37
+++ b/gnuefi/elf_mips64el_efi.lds
4c0d37
@@ -27,6 +27,26 @@ SECTIONS
4c0d37
    HIDDEN (_gp = ALIGN (16) + 0x7ff0);
4c0d37
    *(.got)
4c0d37
 
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    . = ALIGN(16);
4c0d37
diff --git a/gnuefi/elf_x64_efi.lds b/gnuefi/elf_x64_efi.lds
4c0d37
index c7a105898c8..cb2e3dc00aa 100644
4c0d37
--- a/gnuefi/elf_x64_efi.lds
4c0d37
+++ b/gnuefi/elf_x64_efi.lds
4c0d37
@@ -30,6 +30,7 @@ SECTIONS
4c0d37
   {
4c0d37
    *(.reloc)
4c0d37
   }
4c0d37
+
4c0d37
   . = ALIGN(4096);
4c0d37
   .data :
4c0d37
   {
4c0d37
@@ -39,6 +40,27 @@ SECTIONS
4c0d37
    *(.got)
4c0d37
    *(.data*)
4c0d37
    *(.sdata)
4c0d37
+
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    *(.sbss)
4c0d37
diff --git a/gnuefi/elf_x64_fbsd_efi.lds b/gnuefi/elf_x64_fbsd_efi.lds
4c0d37
index 705719bf68b..192aa065d8c 100644
4c0d37
--- a/gnuefi/elf_x64_fbsd_efi.lds
4c0d37
+++ b/gnuefi/elf_x64_fbsd_efi.lds
4c0d37
@@ -36,6 +36,27 @@ SECTIONS
4c0d37
    *(.got)
4c0d37
    *(.data*)
4c0d37
    *(.sdata)
4c0d37
+
4c0d37
+   /*
4c0d37
+    * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+    * command, so they don't start with a size.  Because of p2align and the
4c0d37
+    * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+    * have NULLs which aren't guaranteed to be at the end.
4c0d37
+    */
4c0d37
+   . = ALIGN(16);
4c0d37
+   _init_array = .;
4c0d37
+   *(SORT_BY_NAME(.init_array))
4c0d37
+   _init_array_end = .;
4c0d37
+   __CTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.ctors))
4c0d37
+   __CTOR_END__ = .;
4c0d37
+   __DTOR_LIST__ = .;
4c0d37
+   *(SORT_BY_NAME(.dtors))
4c0d37
+   __DTOR_END__ = .;
4c0d37
+   _fini_array = .;
4c0d37
+   *(SORT_BY_NAME(.fini_array))
4c0d37
+   _fini_array_end = .;
4c0d37
+
4c0d37
    /* the EFI loader doesn't seem to like a .bss section, so we stick
4c0d37
       it all into .data: */
4c0d37
    *(.sbss)
4c0d37
diff --git a/lib/Makefile b/lib/Makefile
4c0d37
index 8bf94000e33..e7eafc01f1e 100644
4c0d37
--- a/lib/Makefile
4c0d37
+++ b/lib/Makefile
4c0d37
@@ -43,8 +43,8 @@ include $(SRCDIR)/../Make.defaults
4c0d37
 TOPDIR = $(SRCDIR)/..
4c0d37
 
4c0d37
 CDIR = $(TOPDIR)/..
4c0d37
-FILES = boxdraw smbios console crc data debug dpath  \
4c0d37
-        error event exit guid hand hw init lock   \
4c0d37
+FILES = boxdraw smbios console crc data debug dpath \
4c0d37
+        entry error event exit guid hand hw init lock \
4c0d37
         misc print sread str cmdline \
4c0d37
 	runtime/rtlock runtime/efirtlib runtime/rtstr runtime/vm runtime/rtdata  \
4c0d37
 	$(ARCH)/initplat $(ARCH)/math $(ARCH)/setjmp
4c0d37
@@ -62,7 +62,7 @@ FILES += $(ARCH)/uldiv $(ARCH)/ldivmod $(ARCH)/div $(ARCH)/llsl $(ARCH)/llsr \
4c0d37
 	 $(ARCH)/mullu
4c0d37
 endif
4c0d37
 
4c0d37
-OBJS  = $(FILES:%=%.o)
4c0d37
+OBJS  = $(FILES:%=%.o) ctors.o
4c0d37
 
4c0d37
 SUBDIRS = ia32 x64 ia64 aa64 arm mips64el runtime
4c0d37
 
4c0d37
diff --git a/lib/ctors.S b/lib/ctors.S
4c0d37
new file mode 100644
4c0d37
index 00000000000..522d31b90d2
4c0d37
--- /dev/null
4c0d37
+++ b/lib/ctors.S
4c0d37
@@ -0,0 +1,43 @@
4c0d37
+/*
4c0d37
+ * Try to define the minimal empty init/ctor/dtor/fini_arrays so building with
4c0d37
+ * older or out-of-tree linker scripts will still work.
4c0d37
+ */
4c0d37
+/*
4c0d37
+ * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+ * command, so they don't start with a size.  Because of p2align and the
4c0d37
+ * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+ * have NULLs which aren't guaranteed to be at the end.
4c0d37
+ */
4c0d37
+	.section .init_array, "aM", @init_array
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl _init_array
4c0d37
+_init_array:
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl _init_array_end
4c0d37
+_init_array_end:
4c0d37
+	.long 0
4c0d37
+	.section .ctors, "aM", @init_array
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl __CTOR_LIST__
4c0d37
+__CTOR_LIST__:
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl __CTOR_END__
4c0d37
+__CTOR_END__:
4c0d37
+	.long 0
4c0d37
+	.section .dtors, "aM", @fini_array
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl __DTOR_LIST__
4c0d37
+__DTOR_LIST__:
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl __DTOR_END__
4c0d37
+__DTOR_END__:
4c0d37
+	.long 0
4c0d37
+	.section .fini_array, "aM", @fini_array
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl _fini_array
4c0d37
+_fini_array:
4c0d37
+	.p2align 3, 0
4c0d37
+	.globl _fini_array_end
4c0d37
+_fini_array_end:
4c0d37
+	.long 0
4c0d37
+
4c0d37
diff --git a/lib/entry.c b/lib/entry.c
4c0d37
new file mode 100644
4c0d37
index 00000000000..d8526084602
4c0d37
--- /dev/null
4c0d37
+++ b/lib/entry.c
4c0d37
@@ -0,0 +1,67 @@
4c0d37
+/*
4c0d37
+ * ctors.c
4c0d37
+ * Copyright 2019 Peter Jones <pjones@redhat.com>
4c0d37
+ *
4c0d37
+ */
4c0d37
+
4c0d37
+#include <efi.h>
4c0d37
+#include <efilib.h>
4c0d37
+
4c0d37
+/*
4c0d37
+ * Note that these aren't the using the GNU "CONSTRUCTOR" output section
4c0d37
+ * command, so they don't start with a size.  Because of p2align and the
4c0d37
+ * end/END definitions, and the fact that they're mergeable, they can also
4c0d37
+ * have NULLs which aren't guaranteed to be at the end.
4c0d37
+ */
4c0d37
+extern UINTN _init_array, _init_array_end;
4c0d37
+extern UINTN __CTOR_LIST__, __CTOR_END__;
4c0d37
+extern UINTN _fini_array, _fini_array_end;
4c0d37
+extern UINTN __DTOR_LIST__, __DTOR_END__;
4c0d37
+
4c0d37
+typedef void (*funcp)(void);
4c0d37
+
4c0d37
+static void ctors(void)
4c0d37
+{
4c0d37
+	for (funcp *location = (void *)&_init_array; location < (funcp *)&_init_array_end; location++) {
4c0d37
+		funcp func = *location;
4c0d37
+		if (location != NULL)
4c0d37
+			func();
4c0d37
+	}
4c0d37
+
4c0d37
+	for (funcp *location = (void *)&__CTOR_LIST__; location < (funcp *)&__CTOR_END__; location++) {
4c0d37
+		funcp func = *location;
4c0d37
+		if (location != NULL)
4c0d37
+			func();
4c0d37
+	}
4c0d37
+}
4c0d37
+
4c0d37
+static void dtors(void)
4c0d37
+{
4c0d37
+	for (funcp *location = (void *)&__DTOR_LIST__; location < (funcp *)&__DTOR_END__; location++) {
4c0d37
+		funcp func = *location;
4c0d37
+		if (location != NULL)
4c0d37
+			func();
4c0d37
+	}
4c0d37
+
4c0d37
+	for (funcp *location = (void *)&_fini_array; location < (funcp *)&_fini_array_end; location++) {
4c0d37
+		funcp func = *location;
4c0d37
+		if (location != NULL)
4c0d37
+			func();
4c0d37
+	}
4c0d37
+}
4c0d37
+
4c0d37
+extern EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab);
4c0d37
+
4c0d37
+EFI_STATUS _entry(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
4c0d37
+{
4c0d37
+	EFI_STATUS status;
4c0d37
+	InitializeLib(image, systab);
4c0d37
+
4c0d37
+	ctors();
4c0d37
+	status = efi_main(image, systab);
4c0d37
+	dtors();
4c0d37
+
4c0d37
+	return status;
4c0d37
+}
4c0d37
+
4c0d37
+// vim:fenc=utf-8:tw=75:noet
4c0d37
diff --git a/lib/init.c b/lib/init.c
4c0d37
index 4f238c0a2cc..726e493919a 100644
4c0d37
--- a/lib/init.c
4c0d37
+++ b/lib/init.c
4c0d37
@@ -46,57 +46,52 @@ Returns:
4c0d37
     EFI_STATUS              Status;
4c0d37
     CHAR8                   *LangCode;
4c0d37
 
4c0d37
-    if (!LibInitialized) {
4c0d37
-        LibInitialized = TRUE;
4c0d37
-        LibFwInstance = FALSE;
4c0d37
-        LibImageHandle = ImageHandle;
4c0d37
-
4c0d37
-
4c0d37
-        //
4c0d37
-        // Set up global pointer to the system table, boot services table,
4c0d37
-        // and runtime services table
4c0d37
-        //
4c0d37
-
4c0d37
-        ST = SystemTable;
4c0d37
-        BS = SystemTable->BootServices;
4c0d37
-        RT = SystemTable->RuntimeServices;
4c0d37
-//        ASSERT (CheckCrc(0, &ST->Hdr));
4c0d37
-//        ASSERT (CheckCrc(0, &BS->Hdr));
4c0d37
-//        ASSERT (CheckCrc(0, &RT->Hdr));
4c0d37
-
4c0d37
-
4c0d37
-        //
4c0d37
-        // Initialize pool allocation type
4c0d37
-        //
4c0d37
-
4c0d37
-        if (ImageHandle) {
4c0d37
-            Status = uefi_call_wrapper(
4c0d37
-                BS->HandleProtocol,
4c0d37
-                3,
4c0d37
-                ImageHandle, 
4c0d37
-                &LoadedImageProtocol,
4c0d37
-                (VOID*)&LoadedImage
4c0d37
-            );
4c0d37
-
4c0d37
-            if (!EFI_ERROR(Status)) {
4c0d37
-                PoolAllocationType = LoadedImage->ImageDataType;
4c0d37
-            }
4c0d37
-            EFIDebugVariable ();
4c0d37
-        }
4c0d37
-
4c0d37
-        //
4c0d37
-        // Initialize Guid table
4c0d37
-        //
4c0d37
-
4c0d37
-        InitializeGuid();
4c0d37
-
4c0d37
-        InitializeLibPlatform(ImageHandle,SystemTable);
4c0d37
+    if (LibInitialized)
4c0d37
+	return;
4c0d37
+
4c0d37
+    LibInitialized = TRUE;
4c0d37
+    LibFwInstance = FALSE;
4c0d37
+    LibImageHandle = ImageHandle;
4c0d37
+
4c0d37
+    //
4c0d37
+    // Set up global pointer to the system table, boot services table,
4c0d37
+    // and runtime services table
4c0d37
+    //
4c0d37
+
4c0d37
+    ST = SystemTable;
4c0d37
+    BS = SystemTable->BootServices;
4c0d37
+    RT = SystemTable->RuntimeServices;
4c0d37
+    // ASSERT (CheckCrc(0, &ST->Hdr));
4c0d37
+    // ASSERT (CheckCrc(0, &BS->Hdr));
4c0d37
+    // ASSERT (CheckCrc(0, &RT->Hdr));
4c0d37
+
4c0d37
+    //
4c0d37
+    // Initialize pool allocation type
4c0d37
+    //
4c0d37
+
4c0d37
+    if (ImageHandle) {
4c0d37
+	Status = uefi_call_wrapper(
4c0d37
+	    BS->HandleProtocol,
4c0d37
+	    3,
4c0d37
+	    ImageHandle,
4c0d37
+	    &LoadedImageProtocol,
4c0d37
+	    (VOID*)&LoadedImage
4c0d37
+	);
4c0d37
+
4c0d37
+	if (!EFI_ERROR(Status)) {
4c0d37
+	    PoolAllocationType = LoadedImage->ImageDataType;
4c0d37
+	}
4c0d37
+	EFIDebugVariable ();
4c0d37
     }
4c0d37
 
4c0d37
     //
4c0d37
-    // 
4c0d37
+    // Initialize Guid table
4c0d37
     //
4c0d37
 
4c0d37
+    InitializeGuid();
4c0d37
+
4c0d37
+    InitializeLibPlatform(ImageHandle,SystemTable);
4c0d37
+
4c0d37
     if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) {
4c0d37
         LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable);
4c0d37
         InitializeUnicodeSupport (LangCode);