-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] C/ELF/Unix constructors/destructors cannot work #208
Comments
hi @stefanct per my test, it requires:
BTW, my mcuxpresso version is 11.9.0, and default REDLIB is used. what is your steps to make it work? |
I am still working on a suitable solution but what you write is about what I have. Alternatively to implementing the system call functions ( redlib is not an option for us since it's closed-source and we need C++ support. Since my project's deadline is in a few weeks I don't have time to update and port all my workarounds... but AFAICT nothing relevant has changed between the versions anyway (at least in the SDK). |
You are referring to the |
first of all, with internal evaluation, we decide to not support this feature(attribute((constructor))) out of the box, because:
for your questions: |
Describe the bug
In the MCUXpresso linker scripts the
.init
,.fini
(and.preinit
) sections are only enabled bycpp_info.ldt
(i.e., for C++ projects) and almost(?) all startup functions within this SDK do not call__libc_init_array()
unless they get compiled for C++.However, these sections are not C++-specific at all but usable in C as well (and predate C++ by quite some time). One can set respective attributes on the functions (e.g.,
__attribute__((constructor))
) to enable execution before/aftermain()
.To Reproduce
Add a constructor to a C program, e.g.,
Set a break point on it and start debugging.
Expected behavior
Execution stops at the function or a build error. However, neither happens.
I have changed the startup code for my rt1021 to always call
__libc_init_array()
and the linker scripts to make it work as intended.Alternatively, I think, GCC could be built with
--disable-initfini-array
but I am not entirely sure if that would immediately throw a build error for such examples... and if it would affect C++.Additional context
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-constructor-function-attribute
https://refspecs.linuxbase.org/elf/gabi4+/ch4.sheader.html#special_sections
https://www.ibm.com/docs/en/xl-c-and-cpp-linux/16.1.1?topic=attributes-constructor-destructor
Apart from fixing the linker scripts, this is approximately what's needed for the startup code:
fix.txt (yes, uploading .patch files is broken, thanks m$)
The text was updated successfully, but these errors were encountered: