2012-03-11 20:04:09 +00:00
|
|
|
/*-
|
2018-11-20 21:04:20 +00:00
|
|
|
* SPDX-License-Identifier: BSD-1-Clause
|
2017-11-26 02:00:33 +00:00
|
|
|
*
|
2012-03-11 20:04:09 +00:00
|
|
|
* Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
|
csu: move common code to libc
Why? Most trivial point, it shaves around 600 bytes from the dynamic
binaries on amd64. Less trivial, the removed code is no longer part of
the ABI, and we can ship updates to it with libc updates. Right now most
of the csu is linked into the binaries and require us to do somewhat
tricky ABI compat when it needs to change. For instance, the init_array
change would be much simpler and does not require note tagging if we
have init calling code in libc.
This could be improved more, by splitting dynamic and static
initialization. For instance, &_DYNAMIC tests can be removed then.
Such change, nonetheless, would require building libc three times.
I left this for later, after this change stabilizes, if ever.
Reviewed by: markj
Discussed with: jrtc27 (some objections, see the review), imp
Tested by: markj (aarch64)
Sponsored by: The FreeBSD Foundation
MFC after: 3 weeks
Differential revision: https://reviews.freebsd.org/D37220
2022-10-30 23:47:44 +00:00
|
|
|
* Copyright (c) 2018, 2023 The FreeBSD Foundation
|
2018-10-13 23:52:55 +00:00
|
|
|
*
|
|
|
|
* Parts of this software was developed by Konstantin Belousov
|
|
|
|
* <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
|
2012-03-11 20:04:09 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-04 17:35:32 +00:00
|
|
|
#include <sys/param.h>
|
2018-10-13 23:52:55 +00:00
|
|
|
#include <sys/elf.h>
|
2018-10-04 17:35:32 +00:00
|
|
|
#include <sys/elf_common.h>
|
csu: move common code to libc
Why? Most trivial point, it shaves around 600 bytes from the dynamic
binaries on amd64. Less trivial, the removed code is no longer part of
the ABI, and we can ship updates to it with libc updates. Right now most
of the csu is linked into the binaries and require us to do somewhat
tricky ABI compat when it needs to change. For instance, the init_array
change would be much simpler and does not require note tagging if we
have init calling code in libc.
This could be improved more, by splitting dynamic and static
initialization. For instance, &_DYNAMIC tests can be removed then.
Such change, nonetheless, would require building libc three times.
I left this for later, after this change stabilizes, if ever.
Reviewed by: markj
Discussed with: jrtc27 (some objections, see the review), imp
Tested by: markj (aarch64)
Sponsored by: The FreeBSD Foundation
MFC after: 3 weeks
Differential revision: https://reviews.freebsd.org/D37220
2022-10-30 23:47:44 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "libc_private.h"
|
2012-03-11 20:04:09 +00:00
|
|
|
|
|
|
|
extern void (*__preinit_array_start[])(int, char **, char **) __hidden;
|
|
|
|
extern void (*__preinit_array_end[])(int, char **, char **) __hidden;
|
|
|
|
extern void (*__init_array_start[])(int, char **, char **) __hidden;
|
|
|
|
extern void (*__init_array_end[])(int, char **, char **) __hidden;
|
|
|
|
extern void (*__fini_array_start[])(void) __hidden;
|
|
|
|
extern void (*__fini_array_end[])(void) __hidden;
|
|
|
|
extern void _fini(void) __hidden;
|
|
|
|
extern void _init(void) __hidden;
|
|
|
|
|
|
|
|
extern int _DYNAMIC;
|
|
|
|
#pragma weak _DYNAMIC
|
|
|
|
|
2018-10-13 23:52:55 +00:00
|
|
|
#if defined(CRT_IRELOC_RELA)
|
|
|
|
extern const Elf_Rela __rela_iplt_start[] __weak_symbol __hidden;
|
|
|
|
extern const Elf_Rela __rela_iplt_end[] __weak_symbol __hidden;
|
|
|
|
|
|
|
|
#include "reloc.c"
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_irelocs(void)
|
|
|
|
{
|
|
|
|
const Elf_Rela *r;
|
|
|
|
|
|
|
|
for (r = &__rela_iplt_start[0]; r < &__rela_iplt_end[0]; r++)
|
|
|
|
crt1_handle_rela(r);
|
|
|
|
}
|
|
|
|
#elif defined(CRT_IRELOC_REL)
|
|
|
|
extern const Elf_Rel __rel_iplt_start[] __weak_symbol __hidden;
|
|
|
|
extern const Elf_Rel __rel_iplt_end[] __weak_symbol __hidden;
|
|
|
|
|
|
|
|
#include "reloc.c"
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_irelocs(void)
|
|
|
|
{
|
|
|
|
const Elf_Rel *r;
|
|
|
|
|
|
|
|
for (r = &__rel_iplt_start[0]; r < &__rel_iplt_end[0]; r++)
|
|
|
|
crt1_handle_rel(r);
|
|
|
|
}
|
|
|
|
#elif defined(CRT_IRELOC_SUPPRESS)
|
|
|
|
#else
|
|
|
|
#error "Define platform reloc type"
|
|
|
|
#endif
|
|
|
|
|
2023-07-22 08:56:06 +00:00
|
|
|
#ifndef PIC
|
2012-03-11 20:04:09 +00:00
|
|
|
static void
|
|
|
|
finalizer(void)
|
|
|
|
{
|
|
|
|
void (*fn)(void);
|
|
|
|
size_t array_size, n;
|
|
|
|
|
|
|
|
array_size = __fini_array_end - __fini_array_start;
|
|
|
|
for (n = array_size; n > 0; n--) {
|
|
|
|
fn = __fini_array_start[n - 1];
|
|
|
|
if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
|
|
|
|
(fn)();
|
|
|
|
}
|
|
|
|
_fini();
|
|
|
|
}
|
2023-07-22 08:56:06 +00:00
|
|
|
#endif
|
2012-03-11 20:04:09 +00:00
|
|
|
|
csu: move common code to libc
Why? Most trivial point, it shaves around 600 bytes from the dynamic
binaries on amd64. Less trivial, the removed code is no longer part of
the ABI, and we can ship updates to it with libc updates. Right now most
of the csu is linked into the binaries and require us to do somewhat
tricky ABI compat when it needs to change. For instance, the init_array
change would be much simpler and does not require note tagging if we
have init calling code in libc.
This could be improved more, by splitting dynamic and static
initialization. For instance, &_DYNAMIC tests can be removed then.
Such change, nonetheless, would require building libc three times.
I left this for later, after this change stabilizes, if ever.
Reviewed by: markj
Discussed with: jrtc27 (some objections, see the review), imp
Tested by: markj (aarch64)
Sponsored by: The FreeBSD Foundation
MFC after: 3 weeks
Differential revision: https://reviews.freebsd.org/D37220
2022-10-30 23:47:44 +00:00
|
|
|
static void
|
2012-03-11 20:04:09 +00:00
|
|
|
handle_static_init(int argc, char **argv, char **env)
|
|
|
|
{
|
2023-03-12 01:45:37 +00:00
|
|
|
#ifndef PIC
|
2012-03-11 20:04:09 +00:00
|
|
|
void (*fn)(int, char **, char **);
|
|
|
|
size_t array_size, n;
|
|
|
|
|
|
|
|
if (&_DYNAMIC != NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
atexit(finalizer);
|
|
|
|
|
|
|
|
array_size = __preinit_array_end - __preinit_array_start;
|
|
|
|
for (n = 0; n < array_size; n++) {
|
|
|
|
fn = __preinit_array_start[n];
|
|
|
|
if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
|
|
|
|
fn(argc, argv, env);
|
|
|
|
}
|
|
|
|
_init();
|
|
|
|
array_size = __init_array_end - __init_array_start;
|
|
|
|
for (n = 0; n < array_size; n++) {
|
|
|
|
fn = __init_array_start[n];
|
|
|
|
if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
|
|
|
|
fn(argc, argv, env);
|
|
|
|
}
|
2023-03-12 01:45:37 +00:00
|
|
|
#endif
|
2012-03-11 20:04:09 +00:00
|
|
|
}
|
|
|
|
|
csu: move common code to libc
Why? Most trivial point, it shaves around 600 bytes from the dynamic
binaries on amd64. Less trivial, the removed code is no longer part of
the ABI, and we can ship updates to it with libc updates. Right now most
of the csu is linked into the binaries and require us to do somewhat
tricky ABI compat when it needs to change. For instance, the init_array
change would be much simpler and does not require note tagging if we
have init calling code in libc.
This could be improved more, by splitting dynamic and static
initialization. For instance, &_DYNAMIC tests can be removed then.
Such change, nonetheless, would require building libc three times.
I left this for later, after this change stabilizes, if ever.
Reviewed by: markj
Discussed with: jrtc27 (some objections, see the review), imp
Tested by: markj (aarch64)
Sponsored by: The FreeBSD Foundation
MFC after: 3 weeks
Differential revision: https://reviews.freebsd.org/D37220
2022-10-30 23:47:44 +00:00
|
|
|
static void
|
2013-01-07 17:58:27 +00:00
|
|
|
handle_argv(int argc, char *argv[], char **env)
|
2012-03-11 20:04:09 +00:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
|
2013-01-07 17:58:27 +00:00
|
|
|
if (environ == NULL)
|
|
|
|
environ = env;
|
|
|
|
if (argc > 0 && argv[0] != NULL) {
|
|
|
|
__progname = argv[0];
|
|
|
|
for (s = __progname; *s != '\0'; s++) {
|
|
|
|
if (*s == '/')
|
|
|
|
__progname = s + 1;
|
|
|
|
}
|
2012-03-11 20:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
csu: move common code to libc
Why? Most trivial point, it shaves around 600 bytes from the dynamic
binaries on amd64. Less trivial, the removed code is no longer part of
the ABI, and we can ship updates to it with libc updates. Right now most
of the csu is linked into the binaries and require us to do somewhat
tricky ABI compat when it needs to change. For instance, the init_array
change would be much simpler and does not require note tagging if we
have init calling code in libc.
This could be improved more, by splitting dynamic and static
initialization. For instance, &_DYNAMIC tests can be removed then.
Such change, nonetheless, would require building libc three times.
I left this for later, after this change stabilizes, if ever.
Reviewed by: markj
Discussed with: jrtc27 (some objections, see the review), imp
Tested by: markj (aarch64)
Sponsored by: The FreeBSD Foundation
MFC after: 3 weeks
Differential revision: https://reviews.freebsd.org/D37220
2022-10-30 23:47:44 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
__libc_start1(int argc, char *argv[], char *env[], void (*cleanup)(void),
|
|
|
|
int (*mainX)(int, char *[], char *[]))
|
|
|
|
{
|
|
|
|
handle_argv(argc, argv, env);
|
|
|
|
|
|
|
|
if (&_DYNAMIC != NULL) {
|
|
|
|
atexit(cleanup);
|
|
|
|
} else {
|
|
|
|
#ifndef CRT_IRELOC_SUPPRESS
|
|
|
|
INIT_IRELOCS;
|
|
|
|
process_irelocs();
|
|
|
|
#endif
|
|
|
|
_init_tls();
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_static_init(argc, argv, env);
|
|
|
|
exit(mainX(argc, argv, env));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXXKIB _mcleanup and monstartup defs */
|
|
|
|
extern void _mcleanup(void);
|
|
|
|
extern void monstartup(void *, void *);
|
|
|
|
|
|
|
|
void
|
|
|
|
__libc_start1_gcrt(int argc, char *argv[], char *env[],
|
|
|
|
void (*cleanup)(void), int (*mainX)(int, char *[], char *[]),
|
|
|
|
int *eprolp, int *etextp)
|
|
|
|
{
|
|
|
|
handle_argv(argc, argv, env);
|
|
|
|
|
|
|
|
if (&_DYNAMIC != NULL) {
|
|
|
|
atexit(cleanup);
|
|
|
|
} else {
|
|
|
|
#ifndef CRT_IRELOC_SUPPRESS
|
|
|
|
INIT_IRELOCS;
|
|
|
|
process_irelocs();
|
|
|
|
#endif
|
|
|
|
_init_tls();
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit(_mcleanup);
|
|
|
|
monstartup(eprolp, etextp);
|
|
|
|
|
|
|
|
handle_static_init(argc, argv, env);
|
|
|
|
exit(mainX(argc, argv, env));
|
|
|
|
}
|