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>
2023-03-28 15:28:40 +03:00
|
|
|
/*
|
|
|
|
* Try to define the minimal empty init/ctor/dtor/fini_arrays so building with
|
|
|
|
* older or out-of-tree linker scripts will still work.
|
|
|
|
*/
|
|
|
|
/*
|
2024-05-09 13:52:46 +03:00
|
|
|
* Note that these aren't using the GNU "CONSTRUCTOR" output section
|
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>
2023-03-28 15:28:40 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2024-05-09 13:52:46 +03:00
|
|
|
#if defined(__ELF__)
|
2023-05-23 17:37:42 +03:00
|
|
|
.section .init_array,"aw",%init_array
|
2024-05-09 13:52:46 +03:00
|
|
|
#else
|
|
|
|
.section .init_array,"aw"
|
|
|
|
#endif
|
2023-11-26 17:28:45 +03:00
|
|
|
.p2align 4, 0
|
2023-04-30 15:51:32 +03:00
|
|
|
.globl __init_array_start
|
|
|
|
__init_array_start:
|
|
|
|
.globl __init_array_end
|
|
|
|
__init_array_end:
|
2024-05-09 13:52:46 +03:00
|
|
|
#if defined(__ELF__)
|
2023-05-23 17:37:42 +03:00
|
|
|
.section .ctors,"aw",%progbits
|
2024-05-09 13:52:46 +03:00
|
|
|
#else
|
|
|
|
.section .ctors,"aw"
|
|
|
|
#endif
|
2023-11-26 17:28:45 +03:00
|
|
|
.p2align 4, 0
|
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>
2023-03-28 15:28:40 +03:00
|
|
|
.globl __CTOR_LIST__
|
|
|
|
__CTOR_LIST__:
|
|
|
|
.globl __CTOR_END__
|
|
|
|
__CTOR_END__:
|
2024-05-09 13:52:46 +03:00
|
|
|
#if defined(__ELF__)
|
2023-05-23 17:37:42 +03:00
|
|
|
.section .dtors,"aw",%progbits
|
2024-05-09 13:52:46 +03:00
|
|
|
#else
|
|
|
|
.section .dtors,"aw"
|
|
|
|
#endif
|
2023-11-26 17:28:45 +03:00
|
|
|
.p2align 4, 0
|
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>
2023-03-28 15:28:40 +03:00
|
|
|
.globl __DTOR_LIST__
|
|
|
|
__DTOR_LIST__:
|
|
|
|
.globl __DTOR_END__
|
|
|
|
__DTOR_END__:
|
2024-05-09 13:52:46 +03:00
|
|
|
#if defined(__ELF__)
|
2023-05-23 17:37:42 +03:00
|
|
|
.section .fini_array,"aw",%fini_array
|
2024-05-09 13:52:46 +03:00
|
|
|
#else
|
|
|
|
.section .fini_array,"aw"
|
|
|
|
#endif
|
2023-11-26 17:28:45 +03:00
|
|
|
.p2align 4, 0
|
2023-04-30 15:51:32 +03:00
|
|
|
.globl __fini_array_start
|
|
|
|
__fini_array_start:
|
|
|
|
.globl __fini_array_end
|
|
|
|
__fini_array_end:
|
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>
2023-03-28 15:28:40 +03:00
|
|
|
|
|
|
|
#if defined(__ELF__) && defined(__linux__)
|
|
|
|
.section .note.GNU-stack,"",%progbits
|
|
|
|
#endif
|
|
|
|
|