Tail calls break ctors/dtors

Add macro EFI_NO_TAIL_CALL which disables tail call
optimization on a per-function basis

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
This commit is contained in:
Callum Farmer 2023-06-21 20:27:43 +01:00
parent 708f66acfe
commit 6f333cfc91
3 changed files with 16 additions and 6 deletions

View file

@ -2,22 +2,22 @@
#include <efilib.h>
// 101 in init_array, 65434 in ctors
static void __attribute__((constructor(101))) ctors101() {
static void __attribute__((constructor(101))) EFI_NO_TAIL_CALL ctors101() {
Print(L"1) ctor with lower numbered priority \r\n");
}
// 65434 in init_array, 101 in ctors
static void __attribute__((constructor(65434))) ctors65434() {
static void __attribute__((constructor(65434))) EFI_NO_TAIL_CALL ctors65434() {
Print(L"2) ctor with higher numbered priority \r\n");
}
// 101 in fini_array, 65434 in dtors
static void __attribute__((destructor(101))) dtors101() {
static void __attribute__((destructor(101))) EFI_NO_TAIL_CALL dtors101() {
Print(L"4) dtor with lower numbered priority \r\n");
}
// 65434 in fini_array, 101 in dtors
static void __attribute__((destructor(65434))) dtors65434() {
static void __attribute__((destructor(65434))) EFI_NO_TAIL_CALL dtors65434() {
Print(L"3) dtor with higher numbered priority \r\n");
}

View file

@ -9,14 +9,14 @@
int constructed_value = 0;
static void __attribute__((__constructor__)) ctor(void)
static void __attribute__((__constructor__)) EFI_NO_TAIL_CALL ctor(void)
{
Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
constructed_value = 1;
Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
}
static void __attribute__((__destructor__)) dtor(void)
static void __attribute__((__destructor__)) EFI_NO_TAIL_CALL dtor(void)
{
Print(L"%a:%d:%a() constructed_value:%d\n", __FILE__, __LINE__, __func__, constructed_value);
constructed_value = 0;

View file

@ -18,6 +18,16 @@ Abstract:
#define EFI_UNUSED __attribute__((__unused__))
#endif
#ifdef _MSC_EXTENSIONS
#define EFI_NO_TAIL_CALL
#else
#ifdef __clang__
#define EFI_NO_TAIL_CALL __attribute__((disable_tail_calls))
#else
#define EFI_NO_TAIL_CALL __attribute__((optimize("no-optimize-sibling-calls")))
#endif
#endif
#ifdef _MSC_EXTENSIONS
#define ALIGN(x) __declspec(align(x))
#else