diff options
-rwxr-xr-x | Make.defaults | 6 | ||||
-rw-r--r-- | Make.rules | 2 | ||||
-rw-r--r-- | apps/Makefile | 2 | ||||
-rw-r--r-- | apps/ctors_dtors_priority_test.c | 29 | ||||
-rw-r--r-- | gnuefi/elf_aarch64_efi.lds | 28 | ||||
-rw-r--r-- | gnuefi/elf_arm_efi.lds | 28 | ||||
-rw-r--r-- | gnuefi/elf_ia32_efi.lds | 27 | ||||
-rw-r--r-- | gnuefi/elf_ia32_fbsd_efi.lds | 27 | ||||
-rw-r--r-- | gnuefi/elf_ia64_efi.lds | 28 | ||||
-rw-r--r-- | gnuefi/elf_mips64el_efi.lds | 27 | ||||
-rw-r--r-- | gnuefi/elf_riscv64_efi.lds | 4 | ||||
-rw-r--r-- | gnuefi/elf_x86_64_efi.lds | 27 | ||||
-rw-r--r-- | gnuefi/elf_x86_64_fbsd_efi.lds | 27 | ||||
-rw-r--r-- | lib/ctors.S | 29 | ||||
-rw-r--r-- | lib/dpath.c | 5 | ||||
-rw-r--r-- | lib/entry.c | 42 | ||||
-rw-r--r-- | lib/error.c | 9 |
17 files changed, 238 insertions, 109 deletions
diff --git a/Make.defaults b/Make.defaults index 1277d14..a4ecac0 100755 --- a/Make.defaults +++ b/Make.defaults @@ -125,9 +125,9 @@ ifeq ($(ARCH),x86_64) && [ $(GCCMINOR) -ge "7" ] ) ) \ && echo 1) ifeq ($(GCCNEWENOUGH),1) - CPPFLAGS += -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args --std=c11 + CPPFLAGS += -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args -std=c11 else ifeq ($(USING_CLANG),clang) - CPPFLAGS += -DGNU_EFI_USE_MS_ABI --std=c11 + CPPFLAGS += -DGNU_EFI_USE_MS_ABI -std=c11 endif CFLAGS += -mno-red-zone @@ -197,7 +197,7 @@ endif ARFLAGS := rDv ASFLAGS += $(ARCH3264) LDFLAGS += -nostdlib --warn-common --no-undefined --fatal-warnings \ - --build-id=sha1 + --build-id=sha1 -z nocombreloc ifneq ($(ARCH),arm) export LIBGCC=$(shell $(CC) $(CFLAGS) $(ARCH3264) -print-libgcc-file-name) @@ -34,6 +34,8 @@ # SUCH DAMAGE. # +.SECONDARY: + %.efi: %.so $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel \ -j .rela -j .rel.* -j .rela.* -j .rel* -j .rela* \ diff --git a/apps/Makefile b/apps/Makefile index 6ebd438..424ee9d 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -64,7 +64,7 @@ TARGET_APPS = t.efi t2.efi t3.efi t4.efi t5.efi t6.efi \ route80h.efi drv0_use.efi AllocPages.efi exit.efi \ FreePages.efi setjmp.efi debughook.efi debughook.efi.debug \ bltgrid.efi lfbgrid.efi setdbg.efi unsetdbg.efi \ - ctors_test.efi + ctors_test.efi ctors_dtors_priority_test.efi TARGET_BSDRIVERS = drv0.efi TARGET_RTDRIVERS = diff --git a/apps/ctors_dtors_priority_test.c b/apps/ctors_dtors_priority_test.c new file mode 100644 index 0000000..0458c08 --- /dev/null +++ b/apps/ctors_dtors_priority_test.c @@ -0,0 +1,29 @@ +#include <efi.h>
+#include <efilib.h>
+
+// 101 in init_array, 65434 in ctors
+static void __attribute__((constructor(101))) ctors101() {
+ Print(L"1) ctor with lower numbered priority \r\n");
+}
+
+// 65434 in init_array, 101 in ctors
+static void __attribute__((constructor(65434))) ctors65434() {
+ Print(L"2) ctor with higher numbered priority \r\n");
+}
+
+// 101 in fini_array, 65434 in dtors
+static void __attribute__((destructor(101))) dtors101() {
+ Print(L"4) dtor with lower numbered priority \r\n");
+}
+
+// 65434 in fini_array, 101 in dtors
+static void __attribute__((destructor(65434))) dtors65434() {
+ Print(L"3) dtor with higher numbered priority \r\n");
+}
+
+EFI_STATUS
+efi_main (EFI_HANDLE image EFI_UNUSED, EFI_SYSTEM_TABLE *systab EFI_UNUSED)
+{
+ Print(L"Main function \r\n");
+ return EFI_SUCCESS;
+}
\ No newline at end of file diff --git a/gnuefi/elf_aarch64_efi.lds b/gnuefi/elf_aarch64_efi.lds index 563e22d..c487d8d 100644 --- a/gnuefi/elf_aarch64_efi.lds +++ b/gnuefi/elf_aarch64_efi.lds @@ -48,18 +48,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -84,6 +91,11 @@ SECTIONS *(.rela.got) *(.rela.dyn) *(.rela.stab) + *(.rela.init_array*) + *(.rela.fini_array*) + *(.rela.ctors*) + *(.rela.dtors*) + } . = ALIGN(4096); .rela.plt : { *(.rela.plt) } diff --git a/gnuefi/elf_arm_efi.lds b/gnuefi/elf_arm_efi.lds index 3023ebc..b656440 100644 --- a/gnuefi/elf_arm_efi.lds +++ b/gnuefi/elf_arm_efi.lds @@ -35,18 +35,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -70,6 +77,11 @@ SECTIONS *(.rel.got) *(.rel.dyn) *(.rel.stab) + *(.rel.init_array*) + *(.rel.fini_array*) + *(.rel.ctors*) + *(.rel.dtors*) + } . = ALIGN(4096); .rel.plt : { *(.rel.plt) } diff --git a/gnuefi/elf_ia32_efi.lds b/gnuefi/elf_ia32_efi.lds index 5eec529..a84c673 100644 --- a/gnuefi/elf_ia32_efi.lds +++ b/gnuefi/elf_ia32_efi.lds @@ -39,18 +39,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -70,6 +77,10 @@ SECTIONS *(.rel.got) *(.rel.dyn) *(.rel.stab) + *(.rel.init_array*) + *(.rel.fini_array*) + *(.rel.ctors*) + *(.rel.dtors*) *(.data.rel.ro.local) *(.data.rel.local) *(.data.rel.ro) diff --git a/gnuefi/elf_ia32_fbsd_efi.lds b/gnuefi/elf_ia32_fbsd_efi.lds index 9e9baec..61ae5c5 100644 --- a/gnuefi/elf_ia32_fbsd_efi.lds +++ b/gnuefi/elf_ia32_fbsd_efi.lds @@ -48,18 +48,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -80,6 +87,10 @@ SECTIONS *(.rel.data.*) *(.rel.got) *(.rel.stab) + *(.rel.init_array*) + *(.rel.fini_array*) + *(.rel.ctors*) + *(.rel.dtors*) *(.data.rel.ro.local) *(.data.rel.local) *(.data.rel.ro) diff --git a/gnuefi/elf_ia64_efi.lds b/gnuefi/elf_ia64_efi.lds index e45799c..74014ce 100644 --- a/gnuefi/elf_ia64_efi.lds +++ b/gnuefi/elf_ia64_efi.lds @@ -49,18 +49,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -82,7 +89,10 @@ SECTIONS *(.rela.dyn) *(.rela.gnu.linkonce.d*) *(.rela.stab) - *(.rela.ctors) + *(.rela.init_array*) + *(.rela.fini_array*) + *(.rela.ctors*) + *(.rela.dtors*) } . = ALIGN(4096); .rela.plt : { *(.rela.plt) } diff --git a/gnuefi/elf_mips64el_efi.lds b/gnuefi/elf_mips64el_efi.lds index bf96841..a9fe787 100644 --- a/gnuefi/elf_mips64el_efi.lds +++ b/gnuefi/elf_mips64el_efi.lds @@ -36,18 +36,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -70,6 +77,10 @@ SECTIONS *(.rel.got) *(.rel.dyn) *(.rel.stab) + *(.rel.init_array*) + *(.rel.fini_array*) + *(.rel.ctors*) + *(.rel.dtors*) } . = ALIGN(4096); .rel.plt : { *(.rel.plt) } diff --git a/gnuefi/elf_riscv64_efi.lds b/gnuefi/elf_riscv64_efi.lds index 3b47247..d5153e9 100644 --- a/gnuefi/elf_riscv64_efi.lds +++ b/gnuefi/elf_riscv64_efi.lds @@ -52,6 +52,10 @@ SECTIONS { *(.rela.got) *(.rela.dyn) *(.rela.stab) + *(.rela.init_array) + *(.rela.fini_array) + *(.rela.ctors) + *(.rela.dtors) } . = ALIGN(4096); .rela.plt : { *(.rela.plt) } diff --git a/gnuefi/elf_x86_64_efi.lds b/gnuefi/elf_x86_64_efi.lds index 2e0e0cb..0f4b63b 100644 --- a/gnuefi/elf_x86_64_efi.lds +++ b/gnuefi/elf_x86_64_efi.lds @@ -46,18 +46,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -82,6 +89,10 @@ SECTIONS *(.rela.got) *(.rela.dyn) *(.rela.stab) + *(.rela.init_array*) + *(.rela.fini_array*) + *(.rela.ctors*) + *(.rela.dtors*) } . = ALIGN(4096); .rela.plt : { *(.rela.plt) } diff --git a/gnuefi/elf_x86_64_fbsd_efi.lds b/gnuefi/elf_x86_64_fbsd_efi.lds index 721ce9a..dd10d88 100644 --- a/gnuefi/elf_x86_64_fbsd_efi.lds +++ b/gnuefi/elf_x86_64_fbsd_efi.lds @@ -44,18 +44,25 @@ SECTIONS * have NULLs which aren't guaranteed to be at the end. */ . = ALIGN(16); - _init_array = .; - *(SORT_BY_NAME(.init_array)) - _init_array_end = .; + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); __CTOR_LIST__ = .; - *(SORT_BY_NAME(.ctors)) + *(SORT(.ctors.*)) + *(.ctors) __CTOR_END__ = .; + . = ALIGN(16); __DTOR_LIST__ = .; - *(SORT_BY_NAME(.dtors)) + *(SORT(.dtors.*)) + *(.dtors) __DTOR_END__ = .; - _fini_array = .; - *(SORT_BY_NAME(.fini_array)) - _fini_array_end = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; /* the EFI loader doesn't seem to like a .bss section, so we stick it all into .data: */ @@ -76,6 +83,10 @@ SECTIONS *(.rela.data*) *(.rela.got) *(.rela.stab) + *(.rela.init_array*) + *(.rela.fini_array*) + *(.rela.ctors*) + *(.rela.dtors*) } _edata = .; _data_size = . - _etext; diff --git a/lib/ctors.S b/lib/ctors.S index 4a0c6ab..de3f231 100644 --- a/lib/ctors.S +++ b/lib/ctors.S @@ -8,38 +8,35 @@ * end/END definitions, and the fact that they're mergeable, they can also * have NULLs which aren't guaranteed to be at the end. */ - .section .init_array, "aM", @init_array + .section .init_array, "aw", @init_array .p2align 3, 0 - .globl _init_array -_init_array: + .globl __init_array_start +__init_array_start: .p2align 3, 0 - .globl _init_array_end -_init_array_end: - .long 0 - .section .ctors, "aM", @init_array + .globl __init_array_end +__init_array_end: + .section .ctors, "aw", @progbits .p2align 3, 0 .globl __CTOR_LIST__ __CTOR_LIST__: .p2align 3, 0 .globl __CTOR_END__ __CTOR_END__: - .long 0 - .section .dtors, "aM", @fini_array + .section .dtors, "aw", @progbits .p2align 3, 0 .globl __DTOR_LIST__ __DTOR_LIST__: .p2align 3, 0 .globl __DTOR_END__ __DTOR_END__: - .long 0 - .section .fini_array, "aM", @fini_array + .section .fini_array, "aw", @fini_array .p2align 3, 0 - .globl _fini_array -_fini_array: + .globl __fini_array_start +__fini_array_start: + .p2align 3, 0 + .globl __fini_array_end +__fini_array_end: .p2align 3, 0 - .globl _fini_array_end -_fini_array_end: - .long 0 #if defined(__ELF__) && defined(__linux__) .section .note.GNU-stack,"",%progbits diff --git a/lib/dpath.c b/lib/dpath.c index 5e079d6..63e4e70 100644 --- a/lib/dpath.c +++ b/lib/dpath.c @@ -1083,11 +1083,12 @@ _DevPathNodeUnknown ( * Entries hold "Type" and "SubType" for know values. * Special "SubType" 0 is used as default for known type with unknown subtype. */ -struct { +typedef struct { UINT8 Type; UINT8 SubType; VOID (*Function)(POOL_PRINT *, VOID *); -} DevPathTable[] = { +} DevPathTable_Type; +DevPathTable_Type DevPathTable[] = { { HARDWARE_DEVICE_PATH, HW_PCI_DP, _DevPathPci}, { HARDWARE_DEVICE_PATH, HW_PCCARD_DP, _DevPathPccard}, { HARDWARE_DEVICE_PATH, HW_MEMMAP_DP, _DevPathMemMap}, diff --git a/lib/entry.c b/lib/entry.c index d852608..3baa91d 100644 --- a/lib/entry.c +++ b/lib/entry.c @@ -7,45 +7,51 @@ #include <efi.h> #include <efilib.h> +typedef void (*funcp)(void); + /* * Note that these aren't the using the GNU "CONSTRUCTOR" output section * command, so they don't start with a size. Because of p2align and the * end/END definitions, and the fact that they're mergeable, they can also * have NULLs which aren't guaranteed to be at the end. */ -extern UINTN _init_array, _init_array_end; -extern UINTN __CTOR_LIST__, __CTOR_END__; -extern UINTN _fini_array, _fini_array_end; -extern UINTN __DTOR_LIST__, __DTOR_END__; - -typedef void (*funcp)(void); +extern funcp __init_array_start[], __init_array_end[]; +extern funcp __CTOR_LIST__[], __CTOR_END__[]; +extern funcp __fini_array_start[], __fini_array_end[]; +extern funcp __DTOR_LIST__[], __DTOR_END__[]; static void ctors(void) { - for (funcp *location = (void *)&_init_array; location < (funcp *)&_init_array_end; location++) { - funcp func = *location; - if (location != NULL) + size_t __init_array_length = __init_array_end - __init_array_start; + for (size_t i = 0; i < __init_array_length; i++) { + funcp func = __init_array_start[i]; + if (func != NULL) func(); } - for (funcp *location = (void *)&__CTOR_LIST__; location < (funcp *)&__CTOR_END__; location++) { - funcp func = *location; - if (location != NULL) + size_t __CTOR_length = __CTOR_END__ - __CTOR_LIST__; + for (size_t i = 0; i < __CTOR_length; i++) { + size_t current = __CTOR_length - i - 1; + funcp func = __CTOR_LIST__[current]; + if (func != NULL) func(); } } static void dtors(void) { - for (funcp *location = (void *)&__DTOR_LIST__; location < (funcp *)&__DTOR_END__; location++) { - funcp func = *location; - if (location != NULL) + size_t __DTOR_length = __DTOR_END__ - __DTOR_LIST__; + for (size_t i = 0; i < __DTOR_length; i++) { + funcp func = __DTOR_LIST__[i]; + if (func != NULL) func(); } - for (funcp *location = (void *)&_fini_array; location < (funcp *)&_fini_array_end; location++) { - funcp func = *location; - if (location != NULL) + size_t __fini_array_length = __fini_array_end - __fini_array_start; + for (size_t i = 0; i < __fini_array_length; i++) { + size_t current = __fini_array_length - i - 1; + funcp func = __fini_array_start[current]; + if (func != NULL) func(); } } diff --git a/lib/error.c b/lib/error.c index 2399a06..9f3b230 100644 --- a/lib/error.c +++ b/lib/error.c @@ -18,10 +18,11 @@ Revision History #include "lib.h" -struct { - EFI_STATUS Code; - WCHAR *Desc; -} ErrorCodeTable[] = { +typedef struct { + EFI_STATUS Code; + WCHAR *Desc; +} ErrorCodeTable_Type; +ErrorCodeTable_Type ErrorCodeTable[] = { { EFI_SUCCESS, L"Success"}, { EFI_LOAD_ERROR, L"Load Error"}, { EFI_INVALID_PARAMETER, L"Invalid Parameter"}, |