__weak void _exit(int status) not overridden despite trying

I'm using Windows 11, WSL2, NCS 2.

I had a program crash due to using an uninitialized function<> object, and end up at _exit in libc-hooks.c

__weak void _exit(int status)
{
	_write(1, "exit\n", 5);
	while (1) {
		;
	}
}

Unfortunately this locks up the main thread and doesn't reset the whole system, which is a problem.

I tried to implement my own _exit() function, but doing so doesn't seem to override the __weak nature of the libc function.

My code looks like this:

.h file
...
extern void _exit(int status);
...


.cpp file
...
void _exit(int status)
{
    // do stuff
}
...

Any thoughts on what I can do to make this work correctly?

Thanks.

  • C vs. C++? The override would only work in a .c file and not if the compiler thinks its C++ (via .C or .cpp file extension).

  • It is C++ as you noticed.

    I googled how to handle this and found an SO article

    https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c

    I changed my implementation to this

    .h file
    ...
    extern "C" {
    extern void _exit(int);
    }
    ...
    
    .cpp file
    ...
    extern "C" {
    void _exit(int status)
    {
        // do stuff
    }
    }
    ...

    But now I'm getting a system crash saying BUS FAULT.  Any thoughts on what might be going wrong here?

    [00:00:10.730,529] <err> os: bus_fault: ***** BUS FAULT *****
    [00:00:10.807,891] <err> os: bus_fault:   Precise data bus error
    [00:00:10.888,427] <err> os: bus_fault:   BFAR Address: 0xfffffff5
    [00:00:10.971,069] <err> os: esf_dump: r0/a1:  0x00000001  r1/a2:  0xfffffff5  r2/a3:  0x00000000
    [00:00:11.086,090] <err> os: esf_dump: r3/a4:  0xfffffff5 r12/ip:  0x20007325 r14/lr:  0x000214a5
    [00:00:11.201,110] <err> os: esf_dump:  xpsr:  0x21000000
    [00:00:11.274,353] <err> os: esf_dump: Faulting instruction address (r15/pc): 0x000214b2
    [00:00:11.379,913] <err> os: z_fatal_error: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
    [00:00:11.489,624] <err> os: z_fatal_error: Current thread: 0x200026c0 (shell_uart)
    

    Thanks.

Related