Make ELF constructors and destructors work

This makes setup and teardown functions defined with
__attribute__((__constructor__) and __attribute__((__destructor__)) work
in normal circumstances in EFI binaries.

A couple of notes:
- it implements both the old-style .ctors/.dtors methods and the newer
  style .init_array/.fini_array ELF constructor and destructor arrays,
  processed in the order:
    .init_array[]
    .ctors[]
    efi_main()
    .dtors[]
    .fini_array[]
- Destructors will only be called if efi_main() exits using "return";
  any call to Exit() will still longjmp() past them.
- InitializeLib() has already been called before constructors run, so
  they don't need to call it (and neither does anything else.)  For
  compatibility, it has been altered so calling it more than once is
  safe.
- No attempt is made to handle any constructor or destructor with a
  prototype other than "void func(void);", but note that InitializeLib
  has been called, so LibImageHandle, ST, BS, and RT are set.
- The init_array/ctor/dtor/fini_array lists aren't the using the GNU
  "CONSTRUCTOR" output section command, so they don't start with a size.
- The lists are individually sorted during the link stage via
  SORT_BY_NAME() in the linker script.
- The default (empty) init_array/ctor/dtor/fini_array lists are padded
  out to 8-byte alignment with ".p2align 3, 0", and each list always has
  at least one ".long 0" at the end of it (even if it's completely
  empty).  As a result, they can have NULLs that need to be skipped.
  The sections they're in are mergeable, so the NULLs don't have to be
  exclusively at the end.
- The ia64 and mips64el arches have not been tested.

Signed-off-by: Peter Jones <pjones@redhat.com>
This commit is contained in:
Peter Jones 2023-03-28 13:28:40 +01:00 committed by Nigel Croxon
parent 14899d899b
commit 4f8b339fac
21 changed files with 379 additions and 54 deletions

View file

@ -43,8 +43,8 @@ include $(SRCDIR)/../Make.defaults
TOPDIR = $(SRCDIR)/..
CDIR = $(TOPDIR)/..
FILES = boxdraw smbios console crc data debug dpath \
error event exit guid hand hw init lock \
FILES = boxdraw smbios console crc data debug dpath \
entry error event exit guid hand hw init lock \
misc pause print sread str cmdline\
runtime/rtlock runtime/efirtlib runtime/rtstr runtime/vm runtime/rtdata \
$(ARCH)/initplat $(ARCH)/math $(ARCH)/setjmp
@ -62,7 +62,7 @@ FILES += $(ARCH)/uldiv $(ARCH)/ldivmod $(ARCH)/div $(ARCH)/llsl $(ARCH)/llsr \
$(ARCH)/mullu
endif
OBJS = $(FILES:%=%.o)
OBJS = $(FILES:%=%.o) ctors.o
SUBDIRS = ia32 x86_64 ia64 aarch64 arm mips64el riscv64 loongarch64 runtime

47
lib/ctors.S Normal file
View file

@ -0,0 +1,47 @@
/*
* Try to define the minimal empty init/ctor/dtor/fini_arrays so building with
* older or out-of-tree linker scripts will still work.
*/
/*
* 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.
*/
.section .init_array, "aM", @init_array
.p2align 3, 0
.globl _init_array
_init_array:
.p2align 3, 0
.globl _init_array_end
_init_array_end:
.long 0
.section .ctors, "aM", @init_array
.p2align 3, 0
.globl __CTOR_LIST__
__CTOR_LIST__:
.p2align 3, 0
.globl __CTOR_END__
__CTOR_END__:
.long 0
.section .dtors, "aM", @fini_array
.p2align 3, 0
.globl __DTOR_LIST__
__DTOR_LIST__:
.p2align 3, 0
.globl __DTOR_END__
__DTOR_END__:
.long 0
.section .fini_array, "aM", @fini_array
.p2align 3, 0
.globl _fini_array
_fini_array:
.p2align 3, 0
.globl _fini_array_end
_fini_array_end:
.long 0
#if defined(__ELF__) && defined(__linux__)
.section .note.GNU-stack,"",%progbits
#endif

67
lib/entry.c Normal file
View file

@ -0,0 +1,67 @@
/*
* ctors.c
* Copyright 2019 Peter Jones <pjones@redhat.com>
*
*/
#include <efi.h>
#include <efilib.h>
/*
* 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);
static void ctors(void)
{
for (funcp *location = (void *)&_init_array; location < (funcp *)&_init_array_end; location++) {
funcp func = *location;
if (location != NULL)
func();
}
for (funcp *location = (void *)&__CTOR_LIST__; location < (funcp *)&__CTOR_END__; location++) {
funcp func = *location;
if (location != NULL)
func();
}
}
static void dtors(void)
{
for (funcp *location = (void *)&__DTOR_LIST__; location < (funcp *)&__DTOR_END__; location++) {
funcp func = *location;
if (location != NULL)
func();
}
for (funcp *location = (void *)&_fini_array; location < (funcp *)&_fini_array_end; location++) {
funcp func = *location;
if (location != NULL)
func();
}
}
extern EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab);
EFI_STATUS _entry(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
{
EFI_STATUS status;
InitializeLib(image, systab);
ctors();
status = efi_main(image, systab);
dtors();
return status;
}
// vim:fenc=utf-8:tw=75:noet

View file

@ -46,57 +46,52 @@ Returns:
EFI_STATUS Status;
CHAR8 *LangCode;
if (!LibInitialized) {
LibInitialized = TRUE;
LibFwInstance = FALSE;
LibImageHandle = ImageHandle;
if (LibInitialized)
return;
LibInitialized = TRUE;
LibFwInstance = FALSE;
LibImageHandle = ImageHandle;
//
// Set up global pointer to the system table, boot services table,
// and runtime services table
//
//
// Set up global pointer to the system table, boot services table,
// and runtime services table
//
ST = SystemTable;
BS = SystemTable->BootServices;
RT = SystemTable->RuntimeServices;
// ASSERT (CheckCrc(0, &ST->Hdr));
// ASSERT (CheckCrc(0, &BS->Hdr));
// ASSERT (CheckCrc(0, &RT->Hdr));
ST = SystemTable;
BS = SystemTable->BootServices;
RT = SystemTable->RuntimeServices;
// ASSERT (CheckCrc(0, &ST->Hdr));
// ASSERT (CheckCrc(0, &BS->Hdr));
// ASSERT (CheckCrc(0, &RT->Hdr));
//
// Initialize pool allocation type
//
//
// Initialize pool allocation type
//
if (ImageHandle) {
Status = uefi_call_wrapper(
BS->HandleProtocol,
3,
ImageHandle,
&LoadedImageProtocol,
(VOID*)&LoadedImage
);
if (ImageHandle) {
Status = uefi_call_wrapper(
BS->HandleProtocol,
3,
ImageHandle,
&LoadedImageProtocol,
(VOID*)&LoadedImage
);
if (!EFI_ERROR(Status)) {
PoolAllocationType = LoadedImage->ImageDataType;
}
EFIDebugVariable ();
}
//
// Initialize Guid table
//
InitializeGuid();
InitializeLibPlatform(ImageHandle,SystemTable);
if (!EFI_ERROR(Status)) {
PoolAllocationType = LoadedImage->ImageDataType;
}
EFIDebugVariable ();
}
//
//
// Initialize Guid table
//
InitializeGuid();
InitializeLibPlatform(ImageHandle,SystemTable);
if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) {
LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable);
InitializeUnicodeSupport (LangCode);