summaryrefslogtreecommitdiff
path: root/src/libc.c
blob: 66b0d8dbe4fcccb7820bddc184ad38ab171a7686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <math.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/user.h> // PAGE_SIZE, PAGE_SHIFT
#include <netdb.h> // h_errno
#include  "wrapper/verbose.h"

#ifdef VERBOSE_FUNCTIONS
#  include "libc-verbose.h"
#endif

struct bionic_dirent {
   uint64_t d_ino;
   int64_t d_off;
   unsigned short d_reclen;
   unsigned char d_type;
   char d_name[256];
};

#ifdef ANDROID_X86_LINKER
struct bionic_sigaction {
   union {
      __sighandler_t _sa_handler;
      void(* _sa_sigaction) (int, siginfo_t *, void *);
   } _u;
   sigset_t sa_mask;
   unsigned long sa_flags;
   void(* sa_restorer) (void);
};
#else
#  error "not implemented for this platform"
#endif

// Stuff that doesn't exist in glibc

void
__assert2(const char* file, int line, const char* function, const char* failed_expression)
{
   fprintf(stderr"%s:%d%s: assertion \"%s\" failed\n", file, line, function, failed_expression);
   abort();
}

pid_t
gettid(void)
{
   return syscall(SYS_gettid);
}

int
tgkill(int tgid, int tid, int sig)
{
   verbose("%d%d%d", tgid, tid, sig);
   return syscall(SYS_tgkill, tgid, tid, sig);
}

int
tkill(int tid, int sig)
{
   verbose("%d%d", tid, sig);
   return syscall(SYS_tkill, tid, sig);
}

#include "libc-sha1.h"

// Stuff needed for runtime compatibility, but not neccessary for linking
// Also stuff that exists in glibc, but needs to be wrapped for runtime compatibility

const char *bionic__ctype_, *bionic__tolower_tab_, *bionic__toupper_tab_;
char bionic___sF[0x54] = {0};
unsigned int bionic___page_size = PAGE_SIZE;

__attribute_const__
int*
bionic___errno(void)
{
   return __errno_location();
}

__attribute_const__
int*
bionic___get_h_errno(void)
{
   return &h_errno;
}

int
bionic_stat(const char *restrict path, struct stat *restrict buf)
{
   verbose("%s", path);
   return stat(path, buf);
}

int
bionic_lstat(const char *restrict path, struct stat *restrict buf)
{
   verbose("%s", path);
   return lstat(path, buf);
}

int
bionic_fstat(int fd, struct stat *buf)
{
   verbose("%d", fd);
   return fstat(fd, buf);
}

int
bionic_fstatat(int dirfd, const char *pathname, void *buf, int flags)
{
   verbose("%d%s", dirfd, pathname);
   return fstatat(dirfd, pathname, buf, flags);
}

static void
glibc_dirent_to_bionic_dirent(const struct dirent *de, struct bionic_dirent *bde)
{
   assert(bde && de);
   *bde = (struct bionic_dirent){
      .d_ino = de->d_ino,
      .d_off = de->d_off,
      .d_reclen = de->d_reclen,
      .d_type = de->d_type,
   };
   _Static_assert(sizeof(bde->d_name) >= sizeof(de->d_name), "bionic_dirent can't hold dirent's d_name");
   memcpy(bde->d_name, de->d_name, sizeof(bde->d_name));
}

struct bionic_dirent*
bionic_readdir(DIR *dirp)
{
   assert(dirp);
   static struct bionic_dirent bde;
   struct dirent *de;
   if (!(de = readdir(dirp)))
      return NULL;
   glibc_dirent_to_bionic_dirent(de, &bde);
   return &bde;
}

int
bionic_readdir_r(DIR *dirp, struct bionic_dirent *entry, struct bionic_dirent **result)
{
   assert(dirp && entry && result);
   struct dirent de, *der = NULL;

   int ret;
   if ((ret = readdir_r(dirp, &de, &der)) != 0 || !der) {
      *result = NULL;
      return ret;
   }

   glibc_dirent_to_bionic_dirent(der, entry);
   *result = entry;
   return 0;
}

int
bionic_sigaction(int sig, const struct bionic_sigaction *restrict act, struct bionic_sigaction *restrict oact)
{
   assert(act);

   struct sigaction goact, gact = {
      .sa_handler = act->_u._sa_handler,
      .sa_mask = act->sa_mask,
      .sa_flags = act->sa_flags,
      .sa_restorer = act->sa_restorer,
   };

   const int ret = sigaction(sig, &gact, &goact);

   if (oact) {
      oact->_u._sa_handler = goact.sa_handler;
      oact->sa_mask = goact.sa_mask;
      oact->sa_flags = goact.sa_flags;
      oact->sa_restorer = goact.sa_restorer;
   }

   return ret;
}

int
bionic___isfinitef(float f)
{
   return isfinite(f);
}

uintptr_t bionic___stack_chk_guard = 4;

__attribute__((noreturn))
void bionic___stack_chk_fail(void)
{
   abort();
}

static inline int
bionic_sysconf_to_glibc_sysconf(int name)
{
   switch(name) {
      case 0x0000return _SC_ARG_MAX;
      case 0x0001return _SC_BC_BASE_MAX;
      case 0x0002return _SC_BC_DIM_MAX;
      case 0x0003return _SC_BC_SCALE_MAX;
      case 0x0004return _SC_BC_STRING_MAX;
      case 0x0005return _SC_CHILD_MAX;
      case 0x0006return _SC_CLK_TCK;
      case 0x0007return _SC_COLL_WEIGHTS_MAX;
      case 0x0008return _SC_EXPR_NEST_MAX;
      case 0x0009return _SC_LINE_MAX;
      case 0x000areturn _SC_NGROUPS_MAX;
      case 0x000breturn _SC_OPEN_MAX;
      case 0x000creturn _SC_PASS_MAX;
      case 0x000dreturn _SC_2_C_BIND;
      case 0x000ereturn _SC_2_C_DEV;
      case 0x000freturn _SC_2_C_VERSION;
      case 0x0010return _SC_2_CHAR_TERM;
      case 0x001dreturn _SC_XOPEN_CRYPT;
      case 0x001ereturn _SC_XOPEN_ENH_I18N;
      case 0x001freturn _SC_XOPEN_SHM;
      case 0x0020return _SC_XOPEN_VERSION;
      case 0x0021return _SC_XOPEN_XCU_VERSION;
      case 0x0022return _SC_XOPEN_REALTIME;
      case 0x0023return _SC_XOPEN_REALTIME_THREADS;
      case 0x0024return _SC_XOPEN_LEGACY;
      case 0x0025return _SC_ATEXIT_MAX;
      case 0x0026return _SC_IOV_MAX;
      case 0x0027return _SC_PAGESIZE;
      case 0x0028return _SC_PAGE_SIZE;
      case 0x0029return _SC_XOPEN_UNIX;
      case 0x002areturn _SC_XBS5_ILP32_OFF32;
      case 0x002breturn _SC_XBS5_ILP32_OFFBIG;
      case 0x002creturn _SC_XBS5_LP64_OFF64;
      case 0x002dreturn _SC_XBS5_LPBIG_OFFBIG;
      case 0x002ereturn _SC_AIO_LISTIO_MAX;
      case 0x002freturn _SC_AIO_MAX;
      case 0x0030return _SC_AIO_PRIO_DELTA_MAX;
      case 0x0031return _SC_DELAYTIMER_MAX;
      case 0x0032return _SC_MQ_OPEN_MAX;
      case 0x0033return _SC_MQ_PRIO_MAX;
      case 0x0034return _SC_RTSIG_MAX;
      case 0x0035return _SC_SEM_NSEMS_MAX;
      case 0x0036return _SC_SEM_VALUE_MAX;
      case 0x0037return _SC_SIGQUEUE_MAX;
      case 0x0038return _SC_TIMER_MAX;
      case 0x0039return _SC_ASYNCHRONOUS_IO;
      case 0x003areturn _SC_FSYNC;
      case 0x003breturn _SC_MAPPED_FILES;
      case 0x003creturn _SC_MEMLOCK;
      case 0x003dreturn _SC_MEMLOCK_RANGE;
      case 0x003ereturn _SC_MEMORY_PROTECTION;
      case 0x003freturn _SC_MESSAGE_PASSING;
      case 0x0040return _SC_PRIORITIZED_IO;
      case 0x0041return _SC_PRIORITY_SCHEDULING;
      case 0x0042return _SC_REALTIME_SIGNALS;
      case 0x0043return _SC_SEMAPHORES;
      case 0x0044return _SC_SHARED_MEMORY_OBJECTS;
      case 0x0045return _SC_SYNCHRONIZED_IO;
      case 0x0046return _SC_TIMERS;
      case 0x0047return _SC_GETGR_R_SIZE_MAX;
      case 0x0048return _SC_GETPW_R_SIZE_MAX;
      case 0x0049return _SC_LOGIN_NAME_MAX;
      case 0x004areturn _SC_THREAD_DESTRUCTOR_ITERATIONS;
      case 0x004breturn _SC_THREAD_KEYS_MAX;
      case 0x004creturn _SC_THREAD_STACK_MIN;
      case 0x004dreturn _SC_THREAD_THREADS_MAX;
      case 0x004ereturn _SC_TTY_NAME_MAX;
      case 0x004freturn _SC_THREADS;
      case 0x0050return _SC_THREAD_ATTR_STACKADDR;
      case 0x0051return _SC_THREAD_ATTR_STACKSIZE;
      case 0x0052return _SC_THREAD_PRIORITY_SCHEDULING;
      case 0x0053return _SC_THREAD_PRIO_INHERIT;
      case 0x0054return _SC_THREAD_PRIO_PROTECT;
      case 0x0055return _SC_THREAD_SAFE_FUNCTIONS;
      case 0x0060return _SC_NPROCESSORS_CONF;
      case 0x0061return _SC_NPROCESSORS_ONLN;
      case 0x0062return _SC_PHYS_PAGES;
      case 0x0063return _SC_AVPHYS_PAGES;
      case 0x0064return _SC_MONOTONIC_CLOCK;
      case 0x0065return _SC_2_PBS;
      case 0x0066return _SC_2_PBS_ACCOUNTING;
      case 0x0067return _SC_2_PBS_CHECKPOINT;
      case 0x0068return _SC_2_PBS_LOCATE;
      case 0x0069return _SC_2_PBS_MESSAGE;
      case 0x006areturn _SC_2_PBS_TRACK;
      case 0x006breturn _SC_ADVISORY_INFO;
      case 0x006creturn _SC_BARRIERS;
      case 0x006dreturn _SC_CLOCK_SELECTION;
      case 0x006ereturn _SC_CPUTIME;
      case 0x006freturn _SC_HOST_NAME_MAX;
      case 0x0070return _SC_IPV6;
      case 0x0071return _SC_RAW_SOCKETS;
      case 0x0072return _SC_READER_WRITER_LOCKS;
      case 0x0073return _SC_REGEXP;
      case 0x0074return _SC_SHELL;
      case 0x0075return _SC_SPAWN;
      case 0x0076return _SC_SPIN_LOCKS;
      case 0x0077return _SC_SPORADIC_SERVER;
      case 0x0078return _SC_SS_REPL_MAX;
      case 0x0079return _SC_SYMLOOP_MAX;
      case 0x007areturn _SC_THREAD_CPUTIME;
      case 0x007breturn _SC_THREAD_PROCESS_SHARED;
      case 0x007creturn _SC_THREAD_ROBUST_PRIO_INHERIT;
      case 0x007dreturn _SC_THREAD_ROBUST_PRIO_PROTECT;
      case 0x007ereturn _SC_THREAD_SPORADIC_SERVER;
      case 0x007freturn _SC_TIMEOUTS;
      case 0x0080return _SC_TRACE;
      case 0x0081return _SC_TRACE_EVENT_FILTER;
      case 0x0082return _SC_TRACE_EVENT_NAME_MAX;
      case 0x0083return _SC_TRACE_INHERIT;
      case 0x0084return _SC_TRACE_LOG;
      case 0x0085return _SC_TRACE_NAME_MAX;
      case 0x0086return _SC_TRACE_SYS_MAX;
      case 0x0087return _SC_TRACE_USER_EVENT_MAX;
      case 0x0088return _SC_TYPED_MEMORY_OBJECTS;
      case 0x0089return _SC_V7_ILP32_OFF32;
      case 0x008areturn _SC_V7_ILP32_OFFBIG;
      case 0x008breturn _SC_V7_LP64_OFF64;
      case 0x008creturn _SC_V7_LPBIG_OFFBIG;
      case 0x008dreturn _SC_XOPEN_STREAMS;
      // case 0x008e: return _SC_XOPEN_UUCP;
      case 0x008freturn _SC_LEVEL1_ICACHE_SIZE;
      case 0x0090return _SC_LEVEL1_ICACHE_ASSOC;
      case 0x0091return _SC_LEVEL1_ICACHE_LINESIZE;
      case 0x0092return _SC_LEVEL1_DCACHE_SIZE;
      case 0x0093return _SC_LEVEL1_DCACHE_ASSOC;
      case 0x0094return _SC_LEVEL1_DCACHE_LINESIZE;
      case 0x0095return _SC_LEVEL2_CACHE_SIZE;
      case 0x0096return _SC_LEVEL2_CACHE_ASSOC;
      case 0x0097return _SC_LEVEL2_CACHE_LINESIZE;
      case 0x0098return _SC_LEVEL3_CACHE_SIZE;
      case 0x0099return _SC_LEVEL3_CACHE_ASSOC;
      case 0x009areturn _SC_LEVEL3_CACHE_LINESIZE;
      case 0x009breturn _SC_LEVEL4_CACHE_SIZE;
      case 0x009creturn _SC_LEVEL4_CACHE_ASSOC;
      case 0x009dreturn _SC_LEVEL4_CACHE_LINESIZE;
      default:
         assert(0 && "sysconf constant (%d) is not mapped");
   }
   return 0xFFFF;
}

long
bionic_sysconf(int name)
{
   verbose("0x%x", name);
   return sysconf(bionic_sysconf_to_glibc_sysconf(name));
}

#if 1
// gcc's libstdc++
void*
bionic___dynamic_cast(const void *src_ptr, const void *src_type, const void *dst_type, intptr_t src2dst)
{
   return (void*)src_ptr;
}
#else
// android's libstdc++
#  include <sys/time.h>
#  include <linux/futex.h>

int
bionic___futex_wait(volatile void* ftx, int value, const struct timespec* timeout)
{
   return syscall(SYS_futex, ftx, FUTEX_WAIT, value, timeout, 0);
}

int
bionic___futex_wake(volatile void* ftx, int count)
{
   return syscall(SYS_futex, ftx, FUTEX_WAKE, count, NULL, 0);
}
#endif