2011-02-12 00:22:29 -05:00
|
|
|
#ifndef LIBC_H
|
|
|
|
#define LIBC_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-09-15 02:00:32 +00:00
|
|
|
#include <limits.h>
|
2011-02-12 00:22:29 -05:00
|
|
|
|
2011-02-20 22:30:06 -05:00
|
|
|
struct __libc {
|
2011-08-06 20:20:23 -04:00
|
|
|
void *main_thread;
|
2011-04-17 16:53:54 -04:00
|
|
|
int threaded;
|
2011-08-23 09:37:39 -04:00
|
|
|
int secure;
|
|
|
|
size_t *auxv;
|
2011-02-12 00:22:29 -05:00
|
|
|
volatile int threads_minus_1;
|
2011-08-23 09:37:39 -04:00
|
|
|
int canceldisable;
|
2011-03-12 21:55:45 -05:00
|
|
|
FILE *ofl_head;
|
2012-04-24 16:32:23 -04:00
|
|
|
int ofl_lock[2];
|
2012-10-05 11:51:50 -04:00
|
|
|
size_t tls_size;
|
2013-09-15 02:00:32 +00:00
|
|
|
size_t page_size;
|
2011-02-20 22:30:06 -05:00
|
|
|
};
|
|
|
|
|
2012-07-27 00:14:57 -04:00
|
|
|
extern size_t __hwcap;
|
2011-02-24 16:37:21 -05:00
|
|
|
|
2013-09-15 02:00:32 +00:00
|
|
|
#ifndef PAGE_SIZE
|
|
|
|
#define PAGE_SIZE libc.page_size
|
|
|
|
#endif
|
|
|
|
|
2012-10-13 23:53:18 -04:00
|
|
|
#if !defined(__PIC__) || (100*__GNUC__+__GNUC_MINOR__ >= 303 && !defined(__PCC__))
|
2011-02-24 16:37:21 -05:00
|
|
|
|
2012-02-23 21:24:56 -05:00
|
|
|
#ifdef __PIC__
|
2012-02-24 20:07:21 -05:00
|
|
|
#if __GNUC__ < 4
|
|
|
|
#define BROKEN_VISIBILITY 1
|
|
|
|
#endif
|
2012-12-07 16:16:44 -05:00
|
|
|
#define ATTR_LIBC_VISIBILITY __attribute__((visibility("hidden")))
|
2012-02-23 21:24:56 -05:00
|
|
|
#else
|
|
|
|
#define ATTR_LIBC_VISIBILITY
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern struct __libc __libc ATTR_LIBC_VISIBILITY;
|
2011-02-20 22:30:06 -05:00
|
|
|
#define libc __libc
|
2011-02-24 16:37:21 -05:00
|
|
|
|
|
|
|
#else
|
2012-02-23 21:24:56 -05:00
|
|
|
|
2011-02-24 16:37:21 -05:00
|
|
|
#define USE_LIBC_ACCESSOR
|
2012-02-23 21:24:56 -05:00
|
|
|
#define ATTR_LIBC_VISIBILITY
|
2011-02-24 16:37:21 -05:00
|
|
|
extern struct __libc *__libc_loc(void) __attribute__((const));
|
|
|
|
#define libc (*__libc_loc())
|
|
|
|
|
2011-02-20 22:30:06 -05:00
|
|
|
#endif
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* Designed to avoid any overhead in non-threaded processes */
|
use explicit visibility to optimize a few hot-path function calls
on x86 and some other archs, functions which make function calls which
might go through a PLT incur a significant overhead cost loading the
GOT register prior to making the call. this load is utterly useless in
musl, since all calls are bound at library-creation time using
-Bsymbolic-functions, but the compiler has no way of knowing this, and
attempts to set the default visibility to protected have failed due to
bugs in GCC and binutils.
this commit simply manually assigns hidden/protected visibility, as
appropriate, to a few internal-use-only functions which have many
callers, or which have callers that are hot paths like getc/putc. it
shaves about 5k off the i386 libc.so with -Os. many of the
improvements are in syscall wrappers, where the benefit is just size
and performance improvement is unmeasurable noise amid the syscall
overhead. however, stdio may be measurably faster.
if in the future there are toolchains that can do the same thing
globally without introducing linking bugs, it might be worth
considering removing these workarounds.
2012-10-25 15:40:58 -04:00
|
|
|
void __lock(volatile int *) ATTR_LIBC_VISIBILITY;
|
|
|
|
void __unlock(volatile int *) ATTR_LIBC_VISIBILITY;
|
|
|
|
int __lockfile(FILE *) ATTR_LIBC_VISIBILITY;
|
|
|
|
void __unlockfile(FILE *) ATTR_LIBC_VISIBILITY;
|
2013-09-20 02:00:27 -04:00
|
|
|
#define LOCK(x) __lock(x)
|
|
|
|
#define UNLOCK(x) __unlock(x)
|
2011-02-12 00:22:29 -05:00
|
|
|
|
new attempt at making set*id() safe and robust
changing credentials in a multi-threaded program is extremely
difficult on linux because it requires synchronizing the change
between all threads, which have their own thread-local credentials on
the kernel side. this is further complicated by the fact that changing
the real uid can fail due to exceeding RLIMIT_NPROC, making it
possible that the syscall will succeed in some threads but fail in
others.
the old __rsyscall approach being replaced was robust in that it would
report failure if any one thread failed, but in this case, the program
would be left in an inconsistent state where individual threads might
have different uid. (this was not as bad as glibc, which would
sometimes even fail to report the failure entirely!)
the new approach being committed refuses to change real user id when
it cannot temporarily set the rlimit to infinity. this is completely
POSIX conformant since POSIX does not require an implementation to
allow real-user-id changes for non-privileged processes whatsoever.
still, setting the real uid can fail due to memory allocation in the
kernel, but this can only happen if there is not already a cached
object for the target user. thus, we forcibly serialize the syscalls
attempts, and fail the entire operation on the first failure. this
*should* lead to an all-or-nothing success/failure result, but it's
still fragile and highly dependent on kernel developers not breaking
things worse than they're already broken.
ideally linux will eventually add a CLONE_USERCRED flag that would
give POSIX conformant credential changes without any hacks from
userspace, and all of this code would become redundant and could be
removed ~10 years down the line when everyone has abandoned the old
broken kernels. i'm not holding my breath...
2011-07-29 22:59:44 -04:00
|
|
|
void __synccall(void (*)(void *), void *);
|
|
|
|
int __setxid(int, int, int, int);
|
2011-04-06 20:27:07 -04:00
|
|
|
|
2011-02-12 00:22:29 -05:00
|
|
|
extern char **__environ;
|
|
|
|
|
|
|
|
#undef weak_alias
|
|
|
|
#define weak_alias(old, new) \
|
|
|
|
extern __typeof(old) new __attribute__((weak, alias(#old)))
|
|
|
|
|
|
|
|
#undef LFS64_2
|
2012-05-31 23:12:31 -04:00
|
|
|
#define LFS64_2(x, y) weak_alias(x, y)
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
#undef LFS64
|
|
|
|
#define LFS64(x) LFS64_2(x, x##64)
|
|
|
|
|
|
|
|
#endif
|