|
|
|
|
#include <elf.h>
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <signal.h>
|
overhaul internally-public declarations using wrapper headers
commits leading up to this one have moved the vast majority of
libc-internal interface declarations to appropriate internal headers,
allowing them to be type-checked and setting the stage to limit their
visibility. the ones that have not yet been moved are mostly
namespace-protected aliases for standard/public interfaces, which
exist to facilitate implementing plain C functions in terms of POSIX
functionality, or C or POSIX functionality in terms of extensions that
are not standardized. some don't quite fit this description, but are
"internally public" interfacs between subsystems of libc.
rather than create a number of newly-named headers to declare these
functions, and having to add explicit include directives for them to
every source file where they're needed, I have introduced a method of
wrapping the corresponding public headers.
parallel to the public headers in $(srcdir)/include, we now have
wrappers in $(srcdir)/src/include that come earlier in the include
path order. they include the public header they're wrapping, then add
declarations for namespace-protected versions of the same interfaces
and any "internally public" interfaces for the subsystem they
correspond to.
along these lines, the wrapper for features.h is now responsible for
the definition of the hidden, weak, and weak_alias macros. this means
source files will no longer need to include any special headers to
access these features.
over time, it is my expectation that the scope of what is "internally
public" will expand, reducing the number of source files which need to
include *_impl.h and related headers down to those which are actually
implementing the corresponding subsystems, not just using them.
8 years ago
|
|
|
#include <unistd.h>
|
|
|
|
|
#include "syscall.h"
|
|
|
|
|
#include "atomic.h"
|
|
|
|
|
#include "libc.h"
|
|
|
|
|
|
remove undef weak refs to init/fini array symbols in libc.so
commit ad1cd43a86645ba2d4f7c8747240452a349d6bc1 eliminated
preprocessor-level omission of references to the init/fini array
symbols from object files going into libc.so. the references are weak,
and the intent was that the linker would resolve them to zero in
libc.so, but instead it leaves undefined references that could be
satisfied at runtime. normally these references would be harmless,
since the code using them does not even get executed, but some older
binutils versions produce a linking error: when linking a program
against libc.so, ld first tries to use the hidden init/fini array
symbols produced by the linker script to satisfy the references in
libc.so, then produces an error because the definitions are hidden.
ideally ld would have already provided definitions of these symbols
when linking libc.so, but the linker script for -shared omits them.
to avoid this situation, the dynamic linker now provides its own dummy
definitions of the init/fini array symbols for libc.so. since they are
hidden, everything binds at ld time and no references remain in the
dynamic symbol table. with modern binutils and --gc-sections, both
the dummy empty array objects and the code referencing them get
dropped at link time, anyway.
the _init and _fini symbols are also switched back to using weak
definitions rather than weak references since the latter behave
somewhat problematically in general, and the weak definition approach
was known to work well.
11 years ago
|
|
|
static void dummy(void) {}
|
|
|
|
|
weak_alias(dummy, _init);
|
|
|
|
|
|
|
|
|
|
extern weak hidden void (*const __init_array_start)(void), (*const __init_array_end)(void);
|
add support for init/fini array in main program, and greatly simplify
modern (4.7.x and later) gcc uses init/fini arrays, rather than the
legacy _init/_fini function pasting and crtbegin/crtend ctors/dtors
system, on most or all archs. some archs had already switched a long
time ago. without following this change, global ctors/dtors will cease
to work under musl when building with new gcc versions.
the most surprising part of this patch is that it actually reduces the
size of the init code, for both static and shared libc. this is
achieved by (1) unifying the handling main program and shared
libraries in the dynamic linker, and (2) eliminating the
glibc-inspired rube goldberg machine for passing around init and fini
function pointers. to clarify, some background:
the function signature for __libc_start_main was based on glibc, as
part of the original goal of being able to run some glibc-linked
binaries. it worked by having the crt1 code, which is linked into
every application, static or dynamic, obtain and pass pointers to the
init and fini functions, which __libc_start_main is then responsible
for using and recording for later use, as necessary. however, in
neither the static-linked nor dynamic-linked case do we actually need
crt1.o's help. with dynamic linking, all the pointers are available in
the _DYNAMIC block. with static linking, it's safe to simply access
the _init/_fini and __init_array_start, etc. symbols directly.
obviously changing the __libc_start_main function signature in an
incompatible way would break both old musl-linked programs and
glibc-linked programs, so let's not do that. instead, the function can
just ignore the information it doesn't need. new archs need not even
provide the useless args in their versions of crt1.o. existing archs
should continue to provide it as long as there is an interest in
having newly-linked applications be able to run on old versions of
musl; at some point in the future, this support can be removed.
13 years ago
|
|
|
|
|
|
|
|
static void dummy1(void *p) {}
|
|
|
|
|
weak_alias(dummy1, __init_ssp);
|
|
|
|
|
|
|
|
|
|
#define AUX_CNT 38
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
__attribute__((__noinline__))
|
|
|
|
|
#endif
|
|
|
|
|
void __init_libc(char **envp, char *pn)
|
|
|
|
|
{
|
|
|
|
|
size_t i, *auxv, aux[AUX_CNT] = { 0 };
|
|
|
|
|
__environ = envp;
|
|
|
|
|
for (i=0; envp[i]; i++);
|
|
|
|
|
libc.auxv = auxv = (void *)(envp+i+1);
|
|
|
|
|
for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT) aux[auxv[i]] = auxv[i+1];
|
|
|
|
|
__hwcap = aux[AT_HWCAP];
|
overhaul i386 syscall mechanism not to depend on external asm source
this is the first part of a series of patches intended to make
__syscall fully self-contained in the object file produced using
syscall.h, which will make it possible for crt1 code to perform
syscalls.
the (confusingly named) i386 __vsyscall mechanism, which this commit
removes, was introduced before the presence of a valid thread pointer
was mandatory; back then the thread pointer was setup lazily only if
threads were used. the intent was to be able to perform syscalls using
the kernel's fast entry point in the VDSO, which can use the sysenter
(Intel) or syscall (AMD) instruction instead of int $128, but without
inlining an access to the __syscall global at the point of each
syscall, which would incur a significant size cost from PIC setup
everywhere. the mechanism also shuffled registers/calling convention
around to avoid spills of call-saved registers, and to avoid
allocating ebx or ebp via asm constraints, since there are plenty of
broken-but-supported compiler versions which are incapable of
allocating ebx with -fPIC or ebp with -fno-omit-frame-pointer.
the new mechanism preserves the properties of avoiding spills and
avoiding allocation of ebx/ebp in constraints, but does it inline,
using some fairly simple register shuffling, and uses a field of the
thread structure rather than global data for the vdso-provided syscall
code address.
for now, the external __syscall function is refactored not to use the
old __vsyscall so it can be kept, but the intent is to remove it too.
7 years ago
|
|
|
if (aux[AT_SYSINFO]) __sysinfo = aux[AT_SYSINFO];
|
|
|
|
|
libc.page_size = aux[AT_PAGESZ];
|
|
|
|
|
|
|
|
|
|
if (!pn) pn = (void*)aux[AT_EXECFN];
|
|
|
|
|
if (!pn) pn = "";
|
|
|
|
|
__progname = __progname_full = pn;
|
|
|
|
|
for (i=0; pn[i]; i++) if (pn[i]=='/') __progname = pn+i+1;
|
|
|
|
|
|
|
|
|
|
__init_tls(aux);
|
|
|
|
|
__init_ssp((void *)aux[AT_RANDOM]);
|
|
|
|
|
|
|
|
|
|
if (aux[AT_UID]==aux[AT_EUID] && aux[AT_GID]==aux[AT_EGID]
|
|
|
|
|
&& !aux[AT_SECURE]) return;
|
|
|
|
|
|
|
|
|
|
struct pollfd pfd[3] = { {.fd=0}, {.fd=1}, {.fd=2} };
|
|
|
|
|
int r =
|
|
|
|
|
#ifdef SYS_poll
|
|
|
|
|
__syscall(SYS_poll, pfd, 3, 0);
|
|
|
|
|
#else
|
|
|
|
|
__syscall(SYS_ppoll, pfd, 3, &(struct timespec){0}, 0, _NSIG/8);
|
|
|
|
|
#endif
|
|
|
|
|
if (r<0) a_crash();
|
|
|
|
|
for (i=0; i<3; i++) if (pfd[i].revents&POLLNVAL)
|
|
|
|
|
if (__sys_open("/dev/null", O_RDWR)<0)
|
|
|
|
|
a_crash();
|
|
|
|
|
libc.secure = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void libc_start_init(void)
|
|
|
|
|
{
|
remove undef weak refs to init/fini array symbols in libc.so
commit ad1cd43a86645ba2d4f7c8747240452a349d6bc1 eliminated
preprocessor-level omission of references to the init/fini array
symbols from object files going into libc.so. the references are weak,
and the intent was that the linker would resolve them to zero in
libc.so, but instead it leaves undefined references that could be
satisfied at runtime. normally these references would be harmless,
since the code using them does not even get executed, but some older
binutils versions produce a linking error: when linking a program
against libc.so, ld first tries to use the hidden init/fini array
symbols produced by the linker script to satisfy the references in
libc.so, then produces an error because the definitions are hidden.
ideally ld would have already provided definitions of these symbols
when linking libc.so, but the linker script for -shared omits them.
to avoid this situation, the dynamic linker now provides its own dummy
definitions of the init/fini array symbols for libc.so. since they are
hidden, everything binds at ld time and no references remain in the
dynamic symbol table. with modern binutils and --gc-sections, both
the dummy empty array objects and the code referencing them get
dropped at link time, anyway.
the _init and _fini symbols are also switched back to using weak
definitions rather than weak references since the latter behave
somewhat problematically in general, and the weak definition approach
was known to work well.
11 years ago
|
|
|
_init();
|
|
|
|
|
uintptr_t a = (uintptr_t)&__init_array_start;
|
|
|
|
|
for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)()))
|
|
|
|
|
(*(void (**)(void))a)();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
weak_alias(libc_start_init, __libc_start_init);
|
|
|
|
|
|
|
|
|
|
typedef int lsm2_fn(int (*)(int,char **,char **), int, char **);
|
|
|
|
|
static lsm2_fn libc_start_main_stage2;
|
|
|
|
|
|
add support for init/fini array in main program, and greatly simplify
modern (4.7.x and later) gcc uses init/fini arrays, rather than the
legacy _init/_fini function pasting and crtbegin/crtend ctors/dtors
system, on most or all archs. some archs had already switched a long
time ago. without following this change, global ctors/dtors will cease
to work under musl when building with new gcc versions.
the most surprising part of this patch is that it actually reduces the
size of the init code, for both static and shared libc. this is
achieved by (1) unifying the handling main program and shared
libraries in the dynamic linker, and (2) eliminating the
glibc-inspired rube goldberg machine for passing around init and fini
function pointers. to clarify, some background:
the function signature for __libc_start_main was based on glibc, as
part of the original goal of being able to run some glibc-linked
binaries. it worked by having the crt1 code, which is linked into
every application, static or dynamic, obtain and pass pointers to the
init and fini functions, which __libc_start_main is then responsible
for using and recording for later use, as necessary. however, in
neither the static-linked nor dynamic-linked case do we actually need
crt1.o's help. with dynamic linking, all the pointers are available in
the _DYNAMIC block. with static linking, it's safe to simply access
the _init/_fini and __init_array_start, etc. symbols directly.
obviously changing the __libc_start_main function signature in an
incompatible way would break both old musl-linked programs and
glibc-linked programs, so let's not do that. instead, the function can
just ignore the information it doesn't need. new archs need not even
provide the useless args in their versions of crt1.o. existing archs
should continue to provide it as long as there is an interest in
having newly-linked applications be able to run on old versions of
musl; at some point in the future, this support can be removed.
13 years ago
|
|
|
int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
char **envp = argv+argc+1;
|
|
|
|
|
|
|
|
|
|
/* External linkage, and explicit noinline attribute if available,
|
|
|
|
|
* are used to prevent the stack frame used during init from
|
|
|
|
|
* persisting for the entire process lifetime. */
|
|
|
|
|
__init_libc(envp, argv[0]);
|
|
|
|
|
|
|
|
|
|
/* Barrier against hoisting application code or anything using ssp
|
|
|
|
|
* or thread pointer prior to its initialization above. */
|
|
|
|
|
lsm2_fn *stage2 = libc_start_main_stage2;
|
|
|
|
|
__asm__ ( "" : "+r"(stage2) : : "memory" );
|
|
|
|
|
return stage2(main, argc, argv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int libc_start_main_stage2(int (*main)(int,char **,char **), int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
char **envp = argv+argc+1;
|
|
|
|
|
__libc_start_init();
|
|
|
|
|
|
|
|
|
|
/* Pass control to the application */
|
|
|
|
|
exit(main(argc, argv, envp));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|