LibC: Don't call initializers in crt0

The dynamic linker is already taking care of this for us. Now
that crt0 is statically linked into each executable and shared
library this breaks things because initializers are invoked twice.

Before this PR this didn't crash because crt0 and its _start()
function was contained in LibC and thus only LibC's initializers were
invoked several times which wasn't as much of a problem because
these initializers didn't have any side effects (such as malloc/free).

However, user programs are more likely to have constructors with side
effects, e.g.:

    std::string g_test("hello!");

This would allocate memory when the constructor is invoked. When it is
invoked again the original allocation would be leaked and another copy
of the string would get allocated. Worse still, when the destructors are
invoked twice the memory would get free'd twice which would likely
crash the program.
This commit is contained in:
Gunnar Beutner 2021-04-15 21:48:30 +02:00 committed by Andreas Kling
parent 50e4cad4a0
commit 92749d9a76
Notes: sideshowbarker 2024-07-18 20:16:57 +09:00

View File

@ -51,17 +51,8 @@ int _start(int argc, char** argv, char** env)
environ = env;
__environ_is_malloced = false;
__libc_init();
_init();
extern void (*__init_array_start[])(int, char**, char**) __attribute__((visibility("hidden")));
extern void (*__init_array_end[])(int, char**, char**) __attribute__((visibility("hidden")));
const size_t size = __init_array_end - __init_array_start;
for (size_t i = 0; i < size; i++)
(*__init_array_start[i])(argc, argv, env);
int status = main(argc, argv, environ);
exit(status);