1#pragma once
2
3#include <limits.h>
4#include <stdatomic.h>
5#include <stdbool.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9
10#include <zircon/compiler.h>
11#include <zircon/types.h>
12
13struct __locale_map;
14
15struct __locale_struct {
16    const struct __locale_map* volatile cat[6];
17};
18
19struct tls_module {
20    struct tls_module* next;
21    void* image;
22    size_t len, size, align, offset;
23};
24
25struct __libc {
26    atomic_int thread_count;
27    struct tls_module* tls_head;
28    size_t tls_size, tls_align, tls_cnt;
29    size_t stack_size;
30    size_t page_size;
31    struct __locale_struct global_locale;
32};
33
34#ifdef __PIC__
35#define ATTR_LIBC_VISIBILITY __attribute__((visibility("hidden")))
36#else
37#define ATTR_LIBC_VISIBILITY
38#endif
39
40// Put this on things that are touched only during dynamic linker startup.
41#define ATTR_RELRO __SECTION(".data.rel.ro")
42
43extern struct __libc __libc ATTR_LIBC_VISIBILITY;
44#define libc __libc
45
46extern size_t __hwcap ATTR_LIBC_VISIBILITY;
47extern char *__progname, *__progname_full;
48
49void __libc_start_init(void) ATTR_LIBC_VISIBILITY;
50
51void __funcs_on_exit(void) ATTR_LIBC_VISIBILITY;
52void __funcs_on_quick_exit(void) ATTR_LIBC_VISIBILITY;
53void __libc_exit_fini(void) ATTR_LIBC_VISIBILITY;
54
55void __dl_thread_cleanup(void) ATTR_LIBC_VISIBILITY;
56
57void __tls_run_dtors(void) ATTR_LIBC_VISIBILITY;
58
59// Registers the handles that zx_take_startup_handle() will return.
60//
61// This function takes ownership of the data, but not the memory: it assumes
62// that the arrays are valid as long as the process is alive.
63//
64// |handles| and |handle_info| are parallel arrays and must have |nhandles|
65//     entries.
66// |handles| contains the actual handle values, or ZX_HANDLE_INVALID if a
67//     handle has already been claimed.
68// |handle_info| contains the PA_HND value associated with the
69//     corresponding element of |handles|, or zero if the handle has already
70//     been claimed.
71void __libc_startup_handles_init(uint32_t nhandles,
72                                 zx_handle_t handles[],
73                                 uint32_t handle_info[]) ATTR_LIBC_VISIBILITY;
74
75_Noreturn void __libc_start_main(void* arg, int (*main)(int, char**, char**));
76
77
78// Hook for extension libraries to init. Extensions must zero out
79// handle[i] and handle_info[i] for any handles they claim.
80void __libc_extensions_init(uint32_t handle_count,
81                            zx_handle_t handle[],
82                            uint32_t handle_info[],
83                            uint32_t name_count,
84                            char** names) __attribute__((weak));
85
86// Hook for extension libraries to clean up. This is run after exit
87// and quick_exit handlers.
88void __libc_extensions_fini(void) __attribute__((weak));
89
90extern uintptr_t __stack_chk_guard;
91void __stack_chk_fail(void);
92
93int __lockfile(FILE*) ATTR_LIBC_VISIBILITY;
94void __unlockfile(FILE*) ATTR_LIBC_VISIBILITY;
95
96// Hook for extension libraries to return the maximum number of files that
97// a process can have open at any time. Used to answer sysconf(_SC_OPEN_MAX).
98// Returns -1 if the value is unknown.
99int _fd_open_max(void);
100
101extern char** __environ;
102
103#undef weak_alias
104#define weak_alias(old, new) extern __typeof(old) new __attribute__((weak, alias(#old)))
105
106#ifdef __clang__
107#define NO_ASAN __attribute__((no_sanitize("address")))
108#else
109#define NO_ASAN
110#endif
111