From 35b077791a7f640b5dc98593b26c6e79569381e3 Mon Sep 17 00:00:00 2001 From: liujia178 Date: Thu, 25 Jan 2024 19:58:20 +0800 Subject: [PATCH] merge porting files Signed-off-by: liujia178 --- BUILD.gn | 19 +- arch/aarch64/syscall_arch.h | 45 + arch/arm/bits/fenv.h | 6 + .../arm/bits/liteos_a}/syscall.h.in | 0 arch/arm/crt_arch.h | 5 + .../arm => arch/arm/liteos_m}/atomic_arch.h | 0 .../arm => arch/arm/liteos_m}/pthread_arch.h | 0 .../arm => arch/arm/liteos_m}/syscall_arch.h | 0 arch/arm/pthread_arch.h | 3 + arch/arm/reloc.h | 6 +- arch/arm/syscall_arch.h | 7 + arch/generic/bits/shm.h | 4 + .../user/arch => arch}/generic/crtbrand.s | 0 arch/x86_64/bits/float.h | 17 +- .../linux/user/config => config}/README_zh.md | 0 .../ld-musl-namespace-aarch64-test.ini | 0 .../ld-musl-namespace-aarch64.ini | 0 .../ld-musl-namespace-arm-test.ini | 0 .../ld-musl-namespace-arm.ini | 0 .../ld-musl-namespace-riscv64-test.ini | 0 .../ld-musl-namespace-riscv64.ini | 0 .../ld-musl-namespace-x86_64-test.ini | 0 .../ld-musl-namespace-x86_64.ini | 0 crt/aarch64/crti.s | 10 + crt/arm/crti.s | 10 + .../linux/user/crt => crt/linux}/crtplus.c | 0 crt/x86_64/crti.s | 4 + {porting/linux/user/etc => etc}/musl.para | 0 {porting/linux/user/etc => etc}/musl.para.dac | 0 include/ctype.h | 4 + include/dlfcn.h | 143 +- include/elf.h | 16 +- include/fcntl.h | 3 + .../fortify => include/fortify/linux}/fcntl.h | 0 .../fortify/linux}/fortify.h | 0 .../fortify => include/fortify/linux}/poll.h | 0 .../fortify/linux}/socket.h | 0 .../fortify => include/fortify/linux}/stat.h | 0 .../fortify => include/fortify/linux}/stdio.h | 0 .../fortify/linux}/stdlib.h | 0 .../fortify/linux}/string.h | 0 .../fortify/linux}/unistd.h | 0 .../linux}/application_target_sdk_version.h | 0 .../info/linux}/device_api_version.h | 0 .../info/linux}/fatal_message.h | 0 include/langinfo.h | 3 + include/limits.h | 11 +- .../include => include/linux}/dlfcn_ext.h | 0 .../user/include => include/linux}/sigchain.h | 0 .../liteos_a/user_debug}/debug.h | 0 .../include => include/liteos_m}/alloca.h | 0 .../liteos_m}/bits/alltypes.h | 0 .../include => include/liteos_m}/bits/errno.h | 0 .../include => include/liteos_m}/bits/fcntl.h | 0 .../liteos_m}/bits/limits.h | 0 .../include => include/liteos_m}/bits/posix.h | 0 .../liteos_m}/bits/stdint.h | 0 .../liteos_m}/bits/syscall.h | 0 .../user/include => include/liteos_m}/errno.h | 0 .../user/include => include/liteos_m}/fcntl.h | 0 .../include => include/liteos_m}/features.h | 0 .../include => include/liteos_m}/limits.h | 0 .../include => include/liteos_m}/pthread.h | 0 .../user/include => include/liteos_m}/sched.h | 0 .../include => include/liteos_m}/semaphore.h | 0 .../include => include/liteos_m}/stdbool.h | 0 .../include => include/liteos_m}/stdint.h | 0 .../include => include/liteos_m}/stdlib.h | 0 .../include => include/liteos_m}/string.h | 0 .../include => include/liteos_m}/strings.h | 0 .../liteos_m}/sys/syscall.h | 0 .../include => include/liteos_m}/syscall.h | 0 .../user/include => include/liteos_m}/time.h | 0 .../include => include/liteos_m}/unistd.h | 0 .../include => include/liteos_m}/usr_lib.h | 0 .../liteos_m}/usr_lib_define.h | 0 .../liteos_m}/usr_lib_ops.h | 0 include/locale.h | 18 + include/malloc.h | 29 + include/poll.h | 3 + include/pthread.h | 152 +- include/sched.h | 13 + include/signal.h | 11 + include/stdio.h | 5 + include/stdlib.h | 7 + include/string.h | 3 + .../user/include => include}/sys/capability.h | 0 .../sys => include/sys/linux}/capability.h | 0 .../include/sys => include/sys/linux}/cdefs.h | 0 .../sys => include/sys/linux}/sspret.h | 0 include/sys/reboot.h | 17 + include/sys/socket.h | 9 + include/sys/stat.h | 3 + include/sys/syscall.h | 40 + include/sys/sysinfo.h | 4 + .../user/include => include}/sys/tgkill.h | 4 +- .../trace/linux}/trace_marker.h | 0 include/unistd.h | 76 + ldso/dynlink.c | 68 + .../linux/user/ldso => ldso/linux}/README.md | 36 +- {porting/linux/user/ldso => ldso/linux}/cfi.c | 0 {porting/linux/user/ldso => ldso/linux}/cfi.h | 0 .../linux/user/ldso => ldso/linux}/dynlink.c | 0 .../user/ldso => ldso/linux}/dynlink_rand.c | 0 .../user/ldso => ldso/linux}/dynlink_rand.h | 0 .../linux/user/ldso => ldso/linux}/ld_log.c | 0 .../linux/user/ldso => ldso/linux}/ld_log.h | 0 .../user/ldso => ldso/linux}/namespace.c | 0 .../user/ldso => ldso/linux}/namespace.h | 0 .../user/ldso => ldso/linux}/ns_config.c | 0 .../user/ldso => ldso/linux}/ns_config.h | 0 .../linux/user/ldso => ldso/linux}/strops.c | 0 .../linux/user/ldso => ldso/linux}/strops.h | 0 .../user/ldso => ldso/linux}/zip_archive.h | 0 libc-test/src/common/BUILD.gn | 6 + .../src/functionalext/fortify/string_ext.c | 2 +- libc-test/src/functionalext/unittest/BUILD.gn | 23 +- libc-test/src/gwp_asan/BUILD.gn | 2 + libc-test/test_template.gni | 2 + musl_src.gni | 366 +- musl_template.gni | 26 +- .../linux/user/arch/aarch64/syscall_arch.h | 105 - porting/linux/user/arch/arm/bits/fenv.h | 23 - porting/linux/user/arch/generic/bits/shm.h | 24 - porting/linux/user/arch/x86_64/bits/float.h | 20 - porting/linux/user/crt/aarch64/crti.s | 17 - porting/linux/user/crt/arm/crti.s | 17 - porting/linux/user/crt/x86_64/crti.s | 11 - porting/linux/user/include/ctype.h | 82 - porting/linux/user/include/dlfcn.h | 185 - porting/linux/user/include/elf.h | 3300 ----------------- porting/linux/user/include/fcntl.h | 217 -- porting/linux/user/include/langinfo.h | 101 - porting/linux/user/include/locale.h | 95 - porting/linux/user/include/malloc.h | 76 - porting/linux/user/include/poll.h | 59 - porting/linux/user/include/pthread.h | 406 -- porting/linux/user/include/sched.h | 153 - porting/linux/user/include/signal.h | 316 -- porting/linux/user/include/stdio.h | 226 -- porting/linux/user/include/stdlib.h | 185 - porting/linux/user/include/string.h | 109 - porting/linux/user/include/sys/reboot.h | 38 - porting/linux/user/include/sys/socket.h | 423 --- porting/linux/user/include/sys/stat.h | 129 - porting/linux/user/include/sys/syscall.h | 47 - porting/linux/user/include/sys/sysinfo.h | 40 - porting/linux/user/include/unistd.h | 511 --- porting/linux/user/src/aio/aio.c | 424 --- porting/linux/user/src/conf/legacy.c | 78 - porting/linux/user/src/conf/sysconf.c | 215 -- porting/linux/user/src/ctype/towctrans.c | 109 - porting/linux/user/src/env/__init_tls.c | 158 - .../linux/user/src/env/__libc_start_main.c | 113 - porting/linux/user/src/env/getenv.c | 24 - porting/linux/user/src/fcntl/open.c | 20 - porting/linux/user/src/include/pthread.h | 29 - porting/linux/user/src/internal/dynlink.h | 269 -- porting/linux/user/src/internal/libc.h | 78 - porting/linux/user/src/internal/locale_impl.h | 50 - .../linux/user/src/internal/pthread_impl.h | 289 -- porting/linux/user/src/internal/stdio_impl.h | 181 - porting/linux/user/src/internal/syscall.h | 402 -- porting/linux/user/src/ldso/dlclose.c | 14 - porting/linux/user/src/ldso/dlerror.c | 87 - porting/linux/user/src/legacy/ulimit.c | 20 - porting/linux/user/src/linux/cache.c | 65 - porting/linux/user/src/linux/clone.c | 101 - porting/linux/user/src/linux/flock.c | 8 - porting/linux/user/src/linux/prctl.c | 22 - porting/linux/user/src/linux/settimeofday.c | 10 - porting/linux/user/src/locale/c_locale.c | 16 - porting/linux/user/src/locale/dcngettext.c | 348 -- porting/linux/user/src/locale/langinfo.c | 103 - porting/linux/user/src/locale/locale_map.c | 162 - porting/linux/user/src/locale/newlocale.c | 70 - porting/linux/user/src/locale/setlocale.c | 95 - porting/linux/user/src/malloc/free.c | 15 - porting/linux/user/src/malloc/libc_calloc.c | 61 - porting/linux/user/src/malloc/lite_malloc.c | 125 - .../user/src/malloc/mallocng/aligned_alloc.c | 67 - .../linux/user/src/malloc/mallocng/donate.c | 39 - .../linux/user/src/malloc/mallocng/malloc.c | 499 --- .../src/malloc/mallocng/malloc_usable_size.c | 23 - porting/linux/user/src/malloc/mallocng/meta.h | 303 -- porting/linux/user/src/malloc/realloc.c | 15 - porting/linux/user/src/mq/mq_notify.c | 78 - porting/linux/user/src/network/getaddrinfo.c | 240 -- porting/linux/user/src/network/getnameinfo.c | 225 -- porting/linux/user/src/network/inet_legacy.c | 32 - porting/linux/user/src/network/lookup.h | 170 - porting/linux/user/src/network/lookup_name.c | 913 ----- porting/linux/user/src/network/lookup_serv.c | 110 - porting/linux/user/src/network/res_cache.c | 188 - porting/linux/user/src/network/res_msend.c | 203 - porting/linux/user/src/network/resolvconf.c | 285 -- porting/linux/user/src/network/socket.c | 69 - porting/linux/user/src/passwd/getspnam_r.c | 132 - porting/linux/user/src/process/_Fork.c | 42 - porting/linux/user/src/process/vfork.c | 31 - porting/linux/user/src/sched/sched_getcpu.c | 57 - porting/linux/user/src/sched/sched_getparam.c | 20 - .../linux/user/src/sched/sched_getscheduler.c | 9 - porting/linux/user/src/sched/sched_setparam.c | 15 - .../linux/user/src/sched/sched_setscheduler.c | 15 - porting/linux/user/src/signal/sigaction.c | 111 - porting/linux/user/src/signal/signal.c | 12 - porting/linux/user/src/stat/fstat.c | 15 - porting/linux/user/src/stdio/__fdopen.c | 139 - porting/linux/user/src/stdio/__fmodeflags.c | 55 - porting/linux/user/src/stdio/__stdio_read.c | 36 - porting/linux/user/src/stdio/__stdio_write.c | 64 - porting/linux/user/src/stdio/__stdout_write.c | 18 - porting/linux/user/src/stdio/__toread.c | 27 - porting/linux/user/src/stdio/__towrite.c | 28 - porting/linux/user/src/stdio/fclose.c | 46 - porting/linux/user/src/stdio/fgets.c | 48 - porting/linux/user/src/stdio/fileno.c | 20 - porting/linux/user/src/stdio/fmemopen.c | 128 - porting/linux/user/src/stdio/fopen.c | 33 - porting/linux/user/src/stdio/fread.c | 82 - porting/linux/user/src/stdio/freopen.c | 60 - porting/linux/user/src/stdio/ofl.c | 76 - porting/linux/user/src/stdio/ofl_add.c | 9 - porting/linux/user/src/stdio/setvbuf.c | 31 - porting/linux/user/src/stdio/stderr.c | 18 - porting/linux/user/src/stdio/stdin.c | 18 - porting/linux/user/src/stdio/vfprintf.c | 721 ---- porting/linux/user/src/stdio/vfscanf.c | 371 -- porting/linux/user/src/stdio/vsnprintf.c | 47 - porting/linux/user/src/stdio/vsscanf.c | 21 - porting/linux/user/src/stdlib/strtod.c | 32 - porting/linux/user/src/stdlib/strtol.c | 59 - porting/linux/user/src/string/stpncpy.c | 20 - porting/linux/user/src/string/strcasecmp.c | 17 - porting/linux/user/src/string/strcspn.c | 20 - porting/linux/user/src/string/strncasecmp.c | 18 - porting/linux/user/src/string/strncat.c | 10 - porting/linux/user/src/string/strncpy.c | 14 - porting/linux/user/src/thread/arm/clone.s | 36 - .../linux/user/src/thread/arm/syscall_cp.s | 37 - .../linux/user/src/thread/pthread_cancel.c | 111 - .../linux/user/src/thread/pthread_cond_init.c | 11 - .../user/src/thread/pthread_cond_timedwait.c | 222 -- .../linux/user/src/thread/pthread_create.c | 562 --- porting/linux/user/src/thread/pthread_kill.c | 19 - .../user/src/thread/pthread_mutex_init.c | 8 - .../user/src/thread/pthread_mutex_lock.c | 12 - .../user/src/thread/pthread_mutex_timedlock.c | 116 - porting/linux/user/src/thread/pthread_once.c | 62 - .../user/src/thread/pthread_rwlock_init.c | 8 - .../src/thread/pthread_rwlock_timedrdlock.c | 41 - .../src/thread/pthread_rwlock_timedwrlock.c | 44 - .../user/src/thread/pthread_setcancelstate.c | 14 - .../user/src/thread/pthread_setcanceltype.c | 13 - .../linux/user/src/thread/pthread_sigmask.c | 74 - porting/linux/user/src/thread/sem_timedwait.c | 34 - porting/linux/user/src/time/__tz.c | 527 --- porting/linux/user/src/time/clock_getres.c | 68 - porting/linux/user/src/time/clock_gettime.c | 108 - porting/linux/user/src/time/gettimeofday.c | 61 - porting/linux/user/src/time/gmtime_r.c | 31 - porting/linux/user/src/time/strftime.c | 370 -- porting/linux/user/src/time/strptime.c | 450 --- porting/linux/user/src/time/time_impl.h | 32 - porting/linux/user/src/time/timer_create.c | 131 - porting/linux/user/src/unistd/getpid.c | 17 - porting/liteos_a/user/arch/arm/crt_arch.h | 18 - porting/liteos_a/user/arch/arm/pthread_arch.h | 34 - porting/liteos_a/user/arch/arm/reloc.h | 32 - porting/liteos_a/user/arch/arm/syscall_arch.h | 109 - porting/liteos_a/user/include/sched.h | 143 - porting/liteos_a/user/ldso/dynlink.c | 2395 ------------ porting/liteos_a/user/src/aio/aio.c | 424 --- porting/liteos_a/user/src/aio/aio_suspend.c | 81 - porting/liteos_a/user/src/aio/lio_listio.c | 146 - porting/liteos_a/user/src/conf/confstr.c | 20 - porting/liteos_a/user/src/conf/sysconf.c | 28 - porting/liteos_a/user/src/crypt/encrypt.c | 55 - porting/liteos_a/user/src/ctype/wcswidth.c | 10 - porting/liteos_a/user/src/dirent/__dirent.h | 12 - porting/liteos_a/user/src/env/__init_tls.c | 185 - .../liteos_a/user/src/env/__libc_start_main.c | 98 - porting/liteos_a/user/src/exit/abort.c | 36 - porting/liteos_a/user/src/exit/exit.c | 49 - porting/liteos_a/user/src/fcntl/fcntl.c | 47 - .../liteos_a/user/src/fcntl/posix_fadvise.c | 21 - porting/liteos_a/user/src/internal/libc.h | 61 - .../liteos_a/user/src/internal/pthread_impl.h | 213 -- .../user/src/internal/unsupported_api.h | 11 - porting/liteos_a/user/src/ipc/msgctl.c | 54 - porting/liteos_a/user/src/ipc/msgget.c | 15 - porting/liteos_a/user/src/ipc/msgrcv.c | 15 - porting/liteos_a/user/src/ipc/msgsnd.c | 14 - porting/liteos_a/user/src/ipc/semctl.c | 72 - porting/liteos_a/user/src/ipc/semget.c | 22 - porting/liteos_a/user/src/ipc/semop.c | 15 - porting/liteos_a/user/src/legacy/daemon.c | 35 - .../liteos_a/user/src/legacy/getdtablesize.c | 13 - .../liteos_a/user/src/legacy/getusershell.c | 34 - porting/liteos_a/user/src/legacy/isastream.c | 8 - porting/liteos_a/user/src/legacy/ulimit.c | 21 - porting/liteos_a/user/src/legacy/utmpx.c | 61 - porting/liteos_a/user/src/linux/adjtime.c | 29 - porting/liteos_a/user/src/linux/clone.c | 80 - porting/liteos_a/user/src/linux/utimes.c | 10 - porting/liteos_a/user/src/locale/freelocale.c | 16 - porting/liteos_a/user/src/locale/strcoll.c | 15 - .../user/src/malloc/oldmalloc/malloc.c | 556 --- porting/liteos_a/user/src/misc/gethostid.c | 31 - porting/liteos_a/user/src/misc/getpriority.c | 7 - porting/liteos_a/user/src/misc/getrlimit.c | 20 - porting/liteos_a/user/src/misc/lockf.c | 34 - porting/liteos_a/user/src/misc/nftw.c | 128 - porting/liteos_a/user/src/misc/openpty.c | 42 - porting/liteos_a/user/src/misc/pty.c | 38 - porting/liteos_a/user/src/misc/setrlimit.c | 49 - porting/liteos_a/user/src/misc/syslog.c | 151 - porting/liteos_a/user/src/misc/wordexp.c | 189 - porting/liteos_a/user/src/mman/mlock.c | 13 - porting/liteos_a/user/src/mman/mlockall.c | 9 - porting/liteos_a/user/src/mman/msync.c | 9 - porting/liteos_a/user/src/mman/munlock.c | 9 - porting/liteos_a/user/src/mman/munlockall.c | 9 - .../liteos_a/user/src/mman/posix_madvise.c | 11 - porting/liteos_a/user/src/mman/shm_open.c | 46 - porting/liteos_a/user/src/mq/mq_close.c | 7 - porting/liteos_a/user/src/mq/mq_notify.c | 16 - .../liteos_a/user/src/multibyte/wcsnrtombs.c | 35 - porting/liteos_a/user/src/network/ent.c | 219 -- porting/liteos_a/user/src/network/ether.c | 143 - .../liteos_a/user/src/network/gethostbyaddr.c | 24 - .../user/src/network/gethostbyaddr_r.c | 70 - .../user/src/network/gethostbyname_r.c | 11 - .../liteos_a/user/src/network/getifaddrs.c | 299 -- .../liteos_a/user/src/network/getnameinfo.c | 200 - .../liteos_a/user/src/network/getservbyname.c | 12 - .../user/src/network/getservbyname_r.c | 55 - .../liteos_a/user/src/network/getservbyport.c | 12 - .../user/src/network/getservbyport_r.c | 59 - .../user/src/network/if_indextoname.c | 22 - .../liteos_a/user/src/network/if_nameindex.c | 115 - .../user/src/network/if_nametoindex.c | 18 - .../liteos_a/user/src/network/inet_legacy.c | 32 - porting/liteos_a/user/src/network/netname.c | 41 - porting/liteos_a/user/src/network/proto.c | 84 - porting/liteos_a/user/src/network/recvmsg.c | 73 - porting/liteos_a/user/src/network/serv.c | 110 - .../liteos_a/user/src/network/sockatmark.c | 12 - porting/liteos_a/user/src/network/socket.c | 62 - porting/liteos_a/user/src/passwd/nscd_query.c | 115 - porting/liteos_a/user/src/process/_Fork.c | 40 - porting/liteos_a/user/src/process/fexecve.c | 18 - .../liteos_a/user/src/process/posix_spawn.c | 217 -- .../posix_spawn_file_actions_addclose.c | 19 - .../posix_spawn_file_actions_adddup2.c | 20 - .../posix_spawn_file_actions_addopen.c | 23 - .../src/process/posix_spawnattr_destroy.c | 8 - .../src/process/posix_spawnattr_getflags.c | 9 - .../src/process/posix_spawnattr_getpgroup.c | 9 - .../process/posix_spawnattr_getsigdefault.c | 9 - .../src/process/posix_spawnattr_getsigmask.c | 9 - .../user/src/process/posix_spawnattr_init.c | 9 - .../user/src/process/posix_spawnattr_sched.c | 30 - .../src/process/posix_spawnattr_setflags.c | 20 - .../src/process/posix_spawnattr_setpgroup.c | 9 - .../process/posix_spawnattr_setsigdefault.c | 10 - .../src/process/posix_spawnattr_setsigmask.c | 10 - .../liteos_a/user/src/process/posix_spawnp.c | 15 - porting/liteos_a/user/src/process/system.c | 49 - porting/liteos_a/user/src/sched/affinity.c | 46 - .../liteos_a/user/src/sched/sched_getparam.c | 19 - .../user/src/sched/sched_getscheduler.c | 10 - .../liteos_a/user/src/sched/sched_setparam.c | 17 - .../user/src/sched/sched_setscheduler.c | 17 - porting/liteos_a/user/src/sched/sched_yield.c | 7 - porting/liteos_a/user/src/select/select.c | 44 - porting/liteos_a/user/src/signal/getitimer.c | 7 - porting/liteos_a/user/src/signal/psiginfo.c | 8 - porting/liteos_a/user/src/signal/setitimer.c | 10 - porting/liteos_a/user/src/signal/sigaction.c | 355 -- .../liteos_a/user/src/signal/sigaltstack.c | 21 - .../liteos_a/user/src/signal/siginterrupt.c | 13 - porting/liteos_a/user/src/signal/siglongjmp.c | 12 - porting/liteos_a/user/src/signal/sigqueue.c | 25 - porting/liteos_a/user/src/signal/sigsuspend.c | 24 - porting/liteos_a/user/src/stat/mkfifo.c | 7 - porting/liteos_a/user/src/stat/mkfifoat.c | 8 - porting/liteos_a/user/src/stat/mknod.c | 14 - porting/liteos_a/user/src/stat/mknodat.c | 9 - .../liteos_a/user/src/stdio/__stdio_exit.c | 24 - .../liteos_a/user/src/stdio/open_wmemstream.c | 104 - porting/liteos_a/user/src/stdio/popen.c | 63 - porting/liteos_a/user/src/stdio/tmpfile.c | 34 - porting/liteos_a/user/src/stdio/tmpnam.c | 32 - .../liteos_a/user/src/termios/cfsetospeed.c | 25 - .../liteos_a/user/src/thread/__timedwait.c | 82 - porting/liteos_a/user/src/thread/__wait.c | 17 - .../user/src/thread/arm/__set_thread_area.c | 56 - .../user/src/thread/pthread_attr_get.c | 100 - .../user/src/thread/pthread_attr_init.c | 13 - .../src/thread/pthread_attr_setschedparam.c | 13 - .../src/thread/pthread_attr_setschedpolicy.c | 10 - .../user/src/thread/pthread_attr_setscope.c | 13 - .../user/src/thread/pthread_barrier_wait.c | 111 - .../liteos_a/user/src/thread/pthread_cancel.c | 89 - .../liteos_a/user/src/thread/pthread_create.c | 379 -- .../liteos_a/user/src/thread/pthread_detach.c | 19 - .../user/src/thread/pthread_getschedparam.c | 29 - .../liteos_a/user/src/thread/pthread_join.c | 58 - .../src/thread/pthread_mutex_getprioceiling.c | 8 - .../user/src/thread/pthread_mutex_timedlock.c | 90 - .../user/src/thread/pthread_mutex_trylock.c | 59 - .../user/src/thread/pthread_mutex_unlock.c | 45 - .../thread/pthread_mutexattr_setprotocol.c | 30 - .../src/thread/pthread_mutexattr_setrobust.c | 13 - .../liteos_a/user/src/thread/pthread_self.c | 17 - .../user/src/thread/pthread_setschedparam.c | 14 - .../user/src/thread/pthread_setschedprio.c | 18 - porting/liteos_a/user/src/thread/sem_open.c | 181 - porting/liteos_a/user/src/thread/thrd_yield.c | 7 - porting/liteos_a/user/src/time/__map_file.c | 21 - porting/liteos_a/user/src/time/clock.c | 18 - porting/liteos_a/user/src/time/times.c | 7 - porting/liteos_a/user/src/time/utime.c | 13 - porting/liteos_a/user/src/unistd/ctermid.c | 9 - porting/liteos_a/user/src/unistd/fdatasync.c | 10 - porting/liteos_a/user/src/unistd/lchown.c | 14 - porting/liteos_a/user/src/unistd/pipe2.c | 24 - porting/liteos_a/user/src/unistd/setegid.c | 13 - porting/liteos_a/user/src/unistd/seteuid.c | 13 - porting/liteos_a/user/src/unistd/setxid.c | 34 - porting/liteos_a/user/src/unistd/tcgetpgrp.c | 13 - porting/liteos_a/user/src/unistd/ttyname.c | 16 - porting/liteos_a/user_debug/Makefile | 237 -- porting/liteos_a/user_debug/ldso/dynlink.c | 2394 ------------ .../user_debug/src/env/__libc_start_main.c | 100 - porting/liteos_a/user_debug/src/exit/exit.c | 60 - .../liteos_a/user_debug/src/malloc/memalign.c | 88 - .../src/malloc/oldmalloc/aligned_alloc.c | 54 - .../user_debug/src/malloc/oldmalloc/malloc.c | 647 ---- .../src/malloc/oldmalloc/malloc_impl.h | 105 - porting/liteos_m/user/BUILD.gn | 34 - porting/liteos_m/user/include/limits.h | 166 - porting/liteos_m/user/include/unistd.h | 493 --- porting/liteos_m/user/src/string/memcmp.c | 9 - porting/liteos_m/user/src/string/memcpy.c | 125 - porting/liteos_m/user/src/string/memset.c | 91 - porting/liteos_m/user/src/string/strcmp.c | 8 - porting/liteos_m/user/src/string/strlen.c | 23 - porting/liteos_m/user/src/thread/__lock.c | 60 - .../user/src/thread/pthread_attr_destroy.c | 7 - .../user/src/thread/pthread_attr_get.c | 52 - .../user/src/thread/pthread_attr_init.c | 11 - .../src/thread/pthread_attr_setdetachstate.c | 9 - .../src/thread/pthread_attr_setguardsize.c | 7 - .../src/thread/pthread_attr_setinheritsched.c | 10 - .../src/thread/pthread_attr_setschedparam.c | 12 - .../src/thread/pthread_attr_setschedpolicy.c | 12 - .../user/src/thread/pthread_attr_setscope.c | 14 - .../user/src/thread/pthread_attr_setstack.c | 9 - .../src/thread/pthread_attr_setstacksize.c | 10 - .../liteos_m/user/src/thread/pthread_cancel.c | 10 - .../liteos_m/user/src/thread/pthread_detach.c | 19 - .../user/src/thread/pthread_getschedparam.c | 28 - .../liteos_m/user/src/thread/pthread_join.c | 41 - .../liteos_m/user/src/thread/pthread_self.c | 12 - .../user/src/thread/pthread_setschedparam.c | 21 - scripts/build_lite/Makefile | 51 +- .../linux/user => scripts/linux}/Makefile | 0 .../user => scripts/linux}/tools/install.sh | 0 .../user/src => scripts/liteos_m}/BUILD.gn | 70 +- .../user => scripts/liteos_m}/hook/BUILD.gn | 4 +- scripts/porting.sh | 2 +- scripts/runtest_Windows.bat | 2 +- scripts/runtest_linux.sh | 2 +- src/aio/aio.c | 13 + src/aio/aio_suspend.c | 2 + src/aio/lio_listio.c | 2 + src/conf/confstr.c | 2 + src/conf/legacy.c | 60 +- src/conf/sysconf.c | 14 +- src/crypt/encrypt.c | 3 + src/ctype/towctrans.c | 35 + src/ctype/wcswidth.c | 2 + src/dirent/__dirent.h | 5 + src/env/__init_tls.c | 60 +- src/env/__libc_start_main.c | 26 +- src/env/getenv.c | 21 +- src/exit/abort.c | 16 + src/exit/exit.c | 30 + .../user/src/exit => src/exit/linux}/atexit.c | 0 src/fcntl/fcntl.c | 2 + src/fcntl/open.c | 3 +- src/fcntl/posix_fadvise.c | 2 + .../fortify => src/fortify/linux}/fortify.c | 0 .../gwp_asan/linux}/gwp_asan.c | 0 .../gwp_asan/linux}/gwp_asan.h | 0 .../hilog => src/hilog/linux}/hilog_adapter.c | 0 .../hilog => src/hilog/linux}/hilog_common.h | 0 .../hilog => src/hilog/linux}/output_p.inl | 0 .../hilog => src/hilog/linux}/vsnprintf_s_p.c | 0 .../hilog => src/hilog/linux}/vsnprintf_s_p.h | 0 .../src/hook => src/hook/linux}/common_def.h | 0 .../hook => src/hook/linux}/malloc_common.c | 0 .../hook => src/hook/linux}/memory_trace.c | 0 .../hook => src/hook/linux}/memory_trace.h | 0 .../src/hook => src/hook/linux}/musl_malloc.h | 0 .../hook/linux}/musl_malloc_dispatch.h | 0 .../hook/linux}/musl_malloc_dispatch_table.h | 0 .../hook => src/hook/linux}/musl_preinit.c | 0 .../hook/linux}/musl_preinit_common.c | 0 .../hook/linux}/musl_preinit_common.h | 0 .../src/hook => src/hook/linux}/musl_socket.h | 0 .../hook/linux}/musl_socket_dispatch.h | 0 .../hook/linux}/musl_socket_preinit.c | 0 .../hook/linux}/musl_socket_preinit_common.c | 0 .../hook/linux}/musl_socket_preinit_common.h | 0 .../hook => src/hook/linux}/socket_common.c | 0 .../hook => src/hook/liteos_m}/los_usr_libc.c | 0 .../include/liteos_m}/features.h | 0 .../linux}/application_target_sdk_version.c | 2 +- .../linux/application_target_sdk_version.h | 56 +- .../info/linux}/device_api_version.c | 4 +- .../info/linux/device_api_version.h | 24 +- .../info => src/info/linux}/fatal_message.c | 2 +- .../info/linux/fatal_message.h | 43 +- src/internal/dynlink.h | 176 +- src/internal/libc.h | 15 +- .../internal/linux}/hilog_adapter.h | 2 +- .../internal/linux}/malloc_config.h | 6 +- .../internal/linux}/malloc_random.h | 40 +- .../internal/linux}/musl_log.h | 2 +- .../internal/linux}/network_conf_function.c | 0 .../internal/linux}/network_conf_function.h | 0 .../internal/linux}/proc_xid_impl.h | 0 .../internal/linux}/services.h | 0 .../internal => src/internal/linux}/vdso.c | 2 +- .../internal/liteos_m}/atomic.h | 0 .../internal/liteos_m}/futex.h | 0 .../src => src/internal/liteos_m}/libc_init.c | 0 .../internal/liteos_m}/libc_start.c | 0 .../internal => src/internal/liteos_m}/lock.h | 0 .../internal/liteos_m}/pthread_impl.h | 0 .../internal/liteos_m}/syscall.h | 0 src/internal/liteos_m/unsupported_api.h | 33 + src/internal/locale_impl.h | 3 + src/internal/pthread_impl.h | 154 +- src/internal/stdio_impl.h | 75 +- src/internal/syscall.h | 11 + src/internal/unsupported_api.h | 33 + src/ipc/msgctl.c | 2 + src/ipc/msgget.c | 2 + src/ipc/msgrcv.c | 2 + src/ipc/msgsnd.c | 2 + src/ipc/semctl.c | 2 + src/ipc/semget.c | 2 + src/ipc/semop.c | 2 + .../ldso/aarch64/linux}/dlvsym.s | 0 .../ldso/arm => src/ldso/arm/linux}/dlvsym.s | 0 src/ldso/dlclose.c | 12 + src/ldso/dlerror.c | 12 + .../ldso/riscv64/linux}/dlvsym.s | 0 .../x86_64 => src/ldso/x86_64/linux}/dlvsym.s | 0 src/legacy/daemon.c | 2 + src/legacy/getdtablesize.c | 4 + src/legacy/getusershell.c | 2 + src/legacy/isastream.c | 2 + src/legacy/ulimit.c | 10 + src/legacy/utmpx.c | 9 + .../linux/aarch64/linux}/flock.s | 0 src/linux/adjtime.c | 2 + .../linux/arm => src/linux/arm/linux}/flock.s | 0 src/linux/cache.c | 5 + src/linux/clone.c | 141 +- src/linux/flock.c | 8 + .../user/src => src/linux}/linux/getprocpid.c | 0 .../src/linux => src/linux/liteos_a}/cap.c | 31 - src/linux/prctl.c | 7 + src/linux/reboot.c | 5 +- src/linux/settimeofday.c | 4 + .../linux/user/src => src}/linux/tgkill.c | 4 +- src/linux/utimes.c | 2 + .../x86_64 => src/linux/x86_64/linux}/flock.s | 0 src/locale/c_locale.c | 3 +- src/locale/dcngettext.c | 19 + src/locale/freelocale.c | 2 + src/locale/langinfo.c | 7 +- src/locale/locale_map.c | 40 + src/locale/newlocale.c | 2 +- src/locale/setlocale.c | 27 +- src/locale/strcoll.c | 4 + src/malloc/free.c | 9 + .../malloc/jemalloc/linux}/jemalloc.c | 0 src/malloc/libc_calloc.c | 68 + .../src/malloc => src/malloc/linux}/calloc.c | 0 .../src/malloc => src/malloc/linux}/stats.c | 0 src/malloc/lite_malloc.c | 7 + .../malloc/liteos_a/user_debug}/backtrace.c | 0 .../liteos_a/user_debug}/memory_check.c | 0 src/malloc/mallocng/aligned_alloc.c | 7 + src/malloc/mallocng/donate.c | 2 +- src/malloc/mallocng/malloc.c | 123 +- src/malloc/mallocng/malloc_usable_size.c | 11 + src/malloc/mallocng/meta.h | 17 +- src/malloc/memalign.c | 91 + src/malloc/oldmalloc/aligned_alloc.c | 3 + src/malloc/oldmalloc/malloc.c | 167 +- src/malloc/oldmalloc/malloc_impl.h | 87 + src/malloc/realloc.c | 8 + .../misc/aarch64/linux}/fstat.s | 0 .../misc/aarch64/linux}/syscall.s | 0 src/misc/gethostid.c | 33 + src/misc/getpriority.c | 4 + src/misc/getrlimit.c | 12 + .../src/misc => src/misc/liteos_a}/realpath.c | 0 src/misc/lockf.c | 3 + src/misc/nftw.c | 24 + src/misc/openpty.c | 2 + src/misc/pty.c | 3 + src/misc/setrlimit.c | 30 + src/misc/syslog.c | 5 + src/misc/wordexp.c | 2 + src/mman/mlock.c | 2 + src/mman/mlockall.c | 2 + src/mman/msync.c | 2 + src/mman/munlock.c | 2 + src/mman/munlockall.c | 2 + src/mman/posix_madvise.c | 2 + src/mman/shm_open.c | 3 + src/mq/mq_close.c | 4 + src/mq/mq_notify.c | 18 + src/network/ent.c | 229 +- src/network/ether.c | 108 +- src/network/getaddrinfo.c | 126 + src/network/getifaddrs.c | 104 + src/network/getnameinfo.c | 50 + src/network/if_indextoname.c | 4 + src/network/if_nametoindex.c | 5 +- src/network/inet_legacy.c | 6 +- src/network/lookup.h | 42 + src/network/lookup_name.c | 551 ++- src/network/lookup_serv.c | 14 + src/network/netname.c | 44 +- src/network/recvmsg.c | 9 + src/network/res_cache.c | 113 +- src/network/res_msend.c | 16 + src/network/resolvconf.c | 156 +- src/network/serv.c | 116 +- src/network/sockatmark.c | 2 + src/network/socket.c | 4 + src/passwd/getspnam_r.c | 8 + src/process/_Fork.c | 16 + .../user/src => src}/process/arm/__vfork.s | 0 src/process/fexecve.c | 2 + src/process/posix_spawn.c | 4 +- .../posix_spawn_file_actions_addclose.c | 2 + .../posix_spawn_file_actions_adddup2.c | 2 + .../posix_spawn_file_actions_addopen.c | 2 + src/process/posix_spawnattr_destroy.c | 2 + src/process/posix_spawnattr_getflags.c | 2 + src/process/posix_spawnattr_getpgroup.c | 2 + src/process/posix_spawnattr_getsigdefault.c | 2 + src/process/posix_spawnattr_getsigmask.c | 2 + src/process/posix_spawnattr_init.c | 2 + src/process/posix_spawnattr_sched.c | 5 + src/process/posix_spawnattr_setflags.c | 2 + src/process/posix_spawnattr_setpgroup.c | 2 + src/process/posix_spawnattr_setsigdefault.c | 2 + src/process/posix_spawnattr_setsigmask.c | 2 + src/process/posix_spawnp.c | 2 + src/process/system.c | 2 + src/process/vfork.c | 23 +- .../user/src => src}/process/x86_64/__vfork.s | 0 src/sched/affinity.c | 39 + .../user/src => src}/sched/sched_cpualloc.c | 22 +- src/sched/sched_getcpu.c | 5 + src/sched/sched_getparam.c | 21 +- src/sched/sched_getscheduler.c | 11 +- src/sched/sched_setparam.c | 16 +- src/sched/sched_setscheduler.c | 16 +- src/sched/sched_yield.c | 4 + src/select/select.c | 8 + .../sigchain/linux}/sigchain.c | 0 .../arm => src/signal/arm/liteos_a}/restore.s | 2 +- src/signal/getitimer.c | 2 + src/signal/psiginfo.c | 2 + src/signal/setitimer.c | 2 + src/signal/sigaction.c | 333 +- src/signal/sigaltstack.c | 2 + src/signal/siginterrupt.c | 20 + src/signal/siglongjmp.c | 2 + src/signal/signal.c | 1 - src/signal/sigqueue.c | 2 + src/signal/sigsuspend.c | 21 + src/stat/fstat.c | 11 + src/stat/mkfifo.c | 7 + src/stat/mkfifoat.c | 2 + src/stat/mknod.c | 2 + src/stat/mknodat.c | 2 + src/stdio/__fdopen.c | 124 +- src/stdio/__fmodeflags.c | 61 +- src/stdio/__stdio_exit.c | 2 + src/stdio/__stdio_read.c | 22 +- src/stdio/__stdio_write.c | 66 +- src/stdio/__stdout_write.c | 9 +- src/stdio/__toread.c | 8 + src/stdio/__towrite.c | 7 +- src/stdio/fclose.c | 22 +- src/stdio/fgets.c | 4 +- src/stdio/fileno.c | 12 +- src/stdio/fmemopen.c | 1 + src/stdio/fopen.c | 28 +- src/stdio/fread.c | 72 +- src/stdio/freopen.c | 4 +- src/stdio/ofl.c | 128 +- src/stdio/ofl_add.c | 4 + src/stdio/open_wmemstream.c | 2 + src/stdio/popen.c | 2 + src/stdio/setvbuf.c | 2 + src/stdio/stderr.c | 2 +- src/stdio/stdin.c | 1 + src/stdio/tmpfile.c | 2 + src/stdio/tmpnam.c | 2 + src/stdio/vfprintf.c | 48 +- src/stdio/vfscanf.c | 34 +- src/stdio/vsnprintf.c | 28 +- src/stdio/vsscanf.c | 16 +- .../stdlib => src/stdlib/liteos_a}/qsort.c | 0 src/stdlib/strtod.c | 2 + src/stdlib/strtol.c | 19 + .../arm => src/string/arm/liteos_a}/memcmp.S | 0 .../user/src => src}/string/arm/memset.S | 0 src/string/memcmp.c | 6 + src/string/memcpy.c | 6 + src/string/memset.c | 6 + src/string/stpncpy.c | 38 +- src/string/strcasecmp.c | 3 +- src/string/strcmp.c | 6 + src/string/strcspn.c | 27 +- src/string/strlen.c | 6 + src/string/strncasecmp.c | 3 +- src/string/strncat.c | 2 +- src/string/strncpy.c | 13 +- src/termios/cfsetospeed.c | 3 + src/thread/__lock.c | 14 +- src/thread/__timedwait.c | 14 + src/thread/__wait.c | 5 + src/thread/arm/__set_thread_area.c | 4 + src/thread/arm/clone.s | 16 + src/thread/arm/syscall_cp.s | 21 + .../thread/liteos_a}/pthread_clone.c | 0 .../thread/liteos_m}/pthread_create.c | 0 src/thread/pthread_attr_destroy.c | 6 + src/thread/pthread_attr_get.c | 40 + src/thread/pthread_attr_init.c | 16 + src/thread/pthread_attr_setdetachstate.c | 6 + src/thread/pthread_attr_setguardsize.c | 10 + src/thread/pthread_attr_setinheritsched.c | 6 + src/thread/pthread_attr_setschedparam.c | 18 + src/thread/pthread_attr_setschedpolicy.c | 15 + src/thread/pthread_attr_setscope.c | 13 + src/thread/pthread_attr_setstack.c | 8 + src/thread/pthread_attr_setstacksize.c | 6 + src/thread/pthread_barrier_wait.c | 5 + src/thread/pthread_cancel.c | 29 +- .../thread/pthread_cond_clockwait.c | 0 src/thread/pthread_cond_init.c | 2 +- .../pthread_cond_timedwait_monotonic_np.c | 0 .../thread/pthread_cond_timeout_np.c | 0 src/thread/pthread_create.c | 271 +- src/thread/pthread_detach.c | 19 + src/thread/pthread_getschedparam.c | 32 + src/thread/pthread_join.c | 49 + src/thread/pthread_kill.c | 6 + .../thread/pthread_mutex_clocklock.c | 0 src/thread/pthread_mutex_getprioceiling.c | 2 + src/thread/pthread_mutex_init.c | 2 +- src/thread/pthread_mutex_lock.c | 7 + .../thread/pthread_mutex_lock_timeout_np.c | 0 src/thread/pthread_mutex_timedlock.c | 37 +- .../pthread_mutex_timedlock_monotonic_np.c | 0 src/thread/pthread_mutex_trylock.c | 16 + src/thread/pthread_mutex_unlock.c | 9 + src/thread/pthread_mutexattr_setprotocol.c | 3 + src/thread/pthread_mutexattr_setrobust.c | 4 + src/thread/pthread_once.c | 12 + .../thread/pthread_rwlock_clockrdlock.c | 0 .../thread/pthread_rwlock_clockwrlock.c | 0 src/thread/pthread_rwlock_init.c | 2 +- src/thread/pthread_rwlock_timedrdlock.c | 3 +- .../pthread_rwlock_timedrdlock_monotonic_np.c | 0 src/thread/pthread_rwlock_timedwrlock.c | 6 +- .../pthread_rwlock_timedwrlock_monotonic_np.c | 0 src/thread/pthread_self.c | 23 + src/thread/pthread_setcancelstate.c | 2 + src/thread/pthread_setcanceltype.c | 2 + src/thread/pthread_setschedparam.c | 24 + src/thread/pthread_setschedprio.c | 10 + src/thread/pthread_sigmask.c | 48 + src/thread/sem_open.c | 8 + src/thread/sem_timedwait.c | 15 +- src/thread/thrd_yield.c | 4 + src/time/__map_file.c | 7 + src/time/__tz.c | 85 +- src/time/clock.c | 3 + src/time/clock_getres.c | 37 +- src/time/clock_gettime.c | 9 + src/time/gettimeofday.c | 36 + src/time/gmtime_r.c | 2 +- src/time/strftime.c | 19 + src/time/strptime.c | 164 + src/time/time.c | 31 + src/time/time_impl.h | 6 + src/time/timer_create.c | 4 +- src/time/times.c | 4 + src/time/utime.c | 2 + .../trace => src/trace/linux}/trace_marker.c | 4 +- src/unistd/ctermid.c | 2 + src/unistd/fdatasync.c | 2 + src/unistd/getpid.c | 10 + src/unistd/lchown.c | 2 + .../unistd => src/unistd/liteos_a}/format.c | 0 src/unistd/pipe2.c | 3 + src/unistd/setegid.c | 9 + src/unistd/seteuid.c | 9 + src/unistd/setxid.c | 4 + src/unistd/tcgetpgrp.c | 2 + src/unistd/ttyname.c | 2 + 829 files changed, 7174 insertions(+), 37032 deletions(-) rename {porting/liteos_a/user/arch/arm/bits => arch/arm/bits/liteos_a}/syscall.h.in (100%) rename {porting/liteos_m/user/arch/arm => arch/arm/liteos_m}/atomic_arch.h (100%) rename {porting/liteos_m/user/arch/arm => arch/arm/liteos_m}/pthread_arch.h (100%) rename {porting/liteos_m/user/arch/arm => arch/arm/liteos_m}/syscall_arch.h (100%) rename {porting/linux/user/arch => arch}/generic/crtbrand.s (100%) rename {porting/linux/user/config => config}/README_zh.md (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-aarch64-test.ini (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-aarch64.ini (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-arm-test.ini (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-arm.ini (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-riscv64-test.ini (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-riscv64.ini (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-x86_64-test.ini (100%) rename {porting/linux/user/config => config}/ld-musl-namespace-x86_64.ini (100%) rename {porting/linux/user/crt => crt/linux}/crtplus.c (100%) rename {porting/linux/user/etc => etc}/musl.para (100%) rename {porting/linux/user/etc => etc}/musl.para.dac (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/fcntl.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/fortify.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/poll.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/socket.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/stat.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/stdio.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/stdlib.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/string.h (100%) rename {porting/linux/user/include/fortify => include/fortify/linux}/unistd.h (100%) rename {porting/linux/user/include/info => include/info/linux}/application_target_sdk_version.h (100%) rename {porting/linux/user/include/info => include/info/linux}/device_api_version.h (100%) rename {porting/linux/user/include/info => include/info/linux}/fatal_message.h (100%) rename {porting/linux/user/include => include/linux}/dlfcn_ext.h (100%) rename {porting/linux/user/include => include/linux}/sigchain.h (100%) rename {porting/liteos_a/user_debug/include => include/liteos_a/user_debug}/debug.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/alloca.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/bits/alltypes.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/bits/errno.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/bits/fcntl.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/bits/limits.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/bits/posix.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/bits/stdint.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/bits/syscall.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/errno.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/fcntl.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/features.h (100%) rename {porting/liteos_a/user/include => include/liteos_m}/limits.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/pthread.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/sched.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/semaphore.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/stdbool.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/stdint.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/stdlib.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/string.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/strings.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/sys/syscall.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/syscall.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/time.h (100%) rename {porting/liteos_a/user/include => include/liteos_m}/unistd.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/usr_lib.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/usr_lib_define.h (100%) rename {porting/liteos_m/user/include => include/liteos_m}/usr_lib_ops.h (100%) rename {porting/liteos_a/user/include => include}/sys/capability.h (100%) rename {porting/linux/user/include/sys => include/sys/linux}/capability.h (100%) rename {porting/linux/user/include/sys => include/sys/linux}/cdefs.h (100%) rename {porting/linux/user/include/sys => include/sys/linux}/sspret.h (100%) rename {porting/linux/user/include => include}/sys/tgkill.h (95%) rename {porting/linux/user/include/trace => include/trace/linux}/trace_marker.h (100%) rename {porting/linux/user/ldso => ldso/linux}/README.md (98%) rename {porting/linux/user/ldso => ldso/linux}/cfi.c (100%) rename {porting/linux/user/ldso => ldso/linux}/cfi.h (100%) rename {porting/linux/user/ldso => ldso/linux}/dynlink.c (100%) rename {porting/linux/user/ldso => ldso/linux}/dynlink_rand.c (100%) rename {porting/linux/user/ldso => ldso/linux}/dynlink_rand.h (100%) rename {porting/linux/user/ldso => ldso/linux}/ld_log.c (100%) rename {porting/linux/user/ldso => ldso/linux}/ld_log.h (100%) rename {porting/linux/user/ldso => ldso/linux}/namespace.c (100%) rename {porting/linux/user/ldso => ldso/linux}/namespace.h (100%) rename {porting/linux/user/ldso => ldso/linux}/ns_config.c (100%) rename {porting/linux/user/ldso => ldso/linux}/ns_config.h (100%) rename {porting/linux/user/ldso => ldso/linux}/strops.c (100%) rename {porting/linux/user/ldso => ldso/linux}/strops.h (100%) rename {porting/linux/user/ldso => ldso/linux}/zip_archive.h (100%) delete mode 100644 porting/linux/user/arch/aarch64/syscall_arch.h delete mode 100644 porting/linux/user/arch/arm/bits/fenv.h delete mode 100644 porting/linux/user/arch/generic/bits/shm.h delete mode 100644 porting/linux/user/arch/x86_64/bits/float.h delete mode 100644 porting/linux/user/crt/aarch64/crti.s delete mode 100644 porting/linux/user/crt/arm/crti.s delete mode 100644 porting/linux/user/crt/x86_64/crti.s delete mode 100644 porting/linux/user/include/ctype.h delete mode 100644 porting/linux/user/include/dlfcn.h delete mode 100644 porting/linux/user/include/elf.h delete mode 100644 porting/linux/user/include/fcntl.h delete mode 100644 porting/linux/user/include/langinfo.h delete mode 100755 porting/linux/user/include/locale.h delete mode 100644 porting/linux/user/include/malloc.h delete mode 100644 porting/linux/user/include/poll.h delete mode 100644 porting/linux/user/include/pthread.h delete mode 100644 porting/linux/user/include/sched.h delete mode 100644 porting/linux/user/include/signal.h delete mode 100644 porting/linux/user/include/stdio.h delete mode 100644 porting/linux/user/include/stdlib.h delete mode 100644 porting/linux/user/include/string.h delete mode 100644 porting/linux/user/include/sys/reboot.h delete mode 100644 porting/linux/user/include/sys/socket.h delete mode 100644 porting/linux/user/include/sys/stat.h delete mode 100644 porting/linux/user/include/sys/syscall.h delete mode 100644 porting/linux/user/include/sys/sysinfo.h delete mode 100644 porting/linux/user/include/unistd.h delete mode 100644 porting/linux/user/src/aio/aio.c delete mode 100644 porting/linux/user/src/conf/legacy.c delete mode 100644 porting/linux/user/src/conf/sysconf.c delete mode 100644 porting/linux/user/src/ctype/towctrans.c delete mode 100644 porting/linux/user/src/env/__init_tls.c delete mode 100644 porting/linux/user/src/env/__libc_start_main.c delete mode 100644 porting/linux/user/src/env/getenv.c delete mode 100644 porting/linux/user/src/fcntl/open.c delete mode 100644 porting/linux/user/src/include/pthread.h delete mode 100644 porting/linux/user/src/internal/dynlink.h delete mode 100644 porting/linux/user/src/internal/libc.h delete mode 100644 porting/linux/user/src/internal/locale_impl.h delete mode 100644 porting/linux/user/src/internal/pthread_impl.h delete mode 100644 porting/linux/user/src/internal/stdio_impl.h delete mode 100644 porting/linux/user/src/internal/syscall.h delete mode 100644 porting/linux/user/src/ldso/dlclose.c delete mode 100644 porting/linux/user/src/ldso/dlerror.c delete mode 100644 porting/linux/user/src/legacy/ulimit.c delete mode 100644 porting/linux/user/src/linux/cache.c delete mode 100644 porting/linux/user/src/linux/clone.c delete mode 100644 porting/linux/user/src/linux/flock.c delete mode 100644 porting/linux/user/src/linux/prctl.c delete mode 100644 porting/linux/user/src/linux/settimeofday.c delete mode 100644 porting/linux/user/src/locale/c_locale.c delete mode 100755 porting/linux/user/src/locale/dcngettext.c delete mode 100644 porting/linux/user/src/locale/langinfo.c delete mode 100755 porting/linux/user/src/locale/locale_map.c delete mode 100644 porting/linux/user/src/locale/newlocale.c delete mode 100644 porting/linux/user/src/locale/setlocale.c delete mode 100644 porting/linux/user/src/malloc/free.c delete mode 100644 porting/linux/user/src/malloc/libc_calloc.c delete mode 100644 porting/linux/user/src/malloc/lite_malloc.c delete mode 100644 porting/linux/user/src/malloc/mallocng/aligned_alloc.c delete mode 100644 porting/linux/user/src/malloc/mallocng/donate.c delete mode 100644 porting/linux/user/src/malloc/mallocng/malloc.c delete mode 100644 porting/linux/user/src/malloc/mallocng/malloc_usable_size.c delete mode 100644 porting/linux/user/src/malloc/mallocng/meta.h delete mode 100644 porting/linux/user/src/malloc/realloc.c delete mode 100644 porting/linux/user/src/mq/mq_notify.c delete mode 100644 porting/linux/user/src/network/getaddrinfo.c delete mode 100644 porting/linux/user/src/network/getnameinfo.c delete mode 100644 porting/linux/user/src/network/inet_legacy.c delete mode 100644 porting/linux/user/src/network/lookup.h delete mode 100644 porting/linux/user/src/network/lookup_name.c delete mode 100644 porting/linux/user/src/network/lookup_serv.c delete mode 100644 porting/linux/user/src/network/res_cache.c delete mode 100644 porting/linux/user/src/network/res_msend.c delete mode 100644 porting/linux/user/src/network/resolvconf.c delete mode 100644 porting/linux/user/src/network/socket.c delete mode 100644 porting/linux/user/src/passwd/getspnam_r.c delete mode 100644 porting/linux/user/src/process/_Fork.c delete mode 100644 porting/linux/user/src/process/vfork.c delete mode 100644 porting/linux/user/src/sched/sched_getcpu.c delete mode 100644 porting/linux/user/src/sched/sched_getparam.c delete mode 100644 porting/linux/user/src/sched/sched_getscheduler.c delete mode 100644 porting/linux/user/src/sched/sched_setparam.c delete mode 100644 porting/linux/user/src/sched/sched_setscheduler.c delete mode 100644 porting/linux/user/src/signal/sigaction.c delete mode 100644 porting/linux/user/src/signal/signal.c delete mode 100644 porting/linux/user/src/stat/fstat.c delete mode 100644 porting/linux/user/src/stdio/__fdopen.c delete mode 100644 porting/linux/user/src/stdio/__fmodeflags.c delete mode 100644 porting/linux/user/src/stdio/__stdio_read.c delete mode 100644 porting/linux/user/src/stdio/__stdio_write.c delete mode 100644 porting/linux/user/src/stdio/__stdout_write.c delete mode 100644 porting/linux/user/src/stdio/__toread.c delete mode 100644 porting/linux/user/src/stdio/__towrite.c delete mode 100644 porting/linux/user/src/stdio/fclose.c delete mode 100644 porting/linux/user/src/stdio/fgets.c delete mode 100644 porting/linux/user/src/stdio/fileno.c delete mode 100644 porting/linux/user/src/stdio/fmemopen.c delete mode 100644 porting/linux/user/src/stdio/fopen.c delete mode 100644 porting/linux/user/src/stdio/fread.c delete mode 100644 porting/linux/user/src/stdio/freopen.c delete mode 100644 porting/linux/user/src/stdio/ofl.c delete mode 100644 porting/linux/user/src/stdio/ofl_add.c delete mode 100644 porting/linux/user/src/stdio/setvbuf.c delete mode 100644 porting/linux/user/src/stdio/stderr.c delete mode 100644 porting/linux/user/src/stdio/stdin.c delete mode 100644 porting/linux/user/src/stdio/vfprintf.c delete mode 100644 porting/linux/user/src/stdio/vfscanf.c delete mode 100644 porting/linux/user/src/stdio/vsnprintf.c delete mode 100644 porting/linux/user/src/stdio/vsscanf.c delete mode 100644 porting/linux/user/src/stdlib/strtod.c delete mode 100644 porting/linux/user/src/stdlib/strtol.c delete mode 100644 porting/linux/user/src/string/stpncpy.c delete mode 100644 porting/linux/user/src/string/strcasecmp.c delete mode 100644 porting/linux/user/src/string/strcspn.c delete mode 100644 porting/linux/user/src/string/strncasecmp.c delete mode 100644 porting/linux/user/src/string/strncat.c delete mode 100644 porting/linux/user/src/string/strncpy.c delete mode 100644 porting/linux/user/src/thread/arm/clone.s delete mode 100644 porting/linux/user/src/thread/arm/syscall_cp.s delete mode 100644 porting/linux/user/src/thread/pthread_cancel.c delete mode 100644 porting/linux/user/src/thread/pthread_cond_init.c delete mode 100644 porting/linux/user/src/thread/pthread_cond_timedwait.c delete mode 100644 porting/linux/user/src/thread/pthread_create.c delete mode 100644 porting/linux/user/src/thread/pthread_kill.c delete mode 100644 porting/linux/user/src/thread/pthread_mutex_init.c delete mode 100644 porting/linux/user/src/thread/pthread_mutex_lock.c delete mode 100644 porting/linux/user/src/thread/pthread_mutex_timedlock.c delete mode 100644 porting/linux/user/src/thread/pthread_once.c delete mode 100644 porting/linux/user/src/thread/pthread_rwlock_init.c delete mode 100644 porting/linux/user/src/thread/pthread_rwlock_timedrdlock.c delete mode 100644 porting/linux/user/src/thread/pthread_rwlock_timedwrlock.c delete mode 100644 porting/linux/user/src/thread/pthread_setcancelstate.c delete mode 100644 porting/linux/user/src/thread/pthread_setcanceltype.c delete mode 100644 porting/linux/user/src/thread/pthread_sigmask.c delete mode 100644 porting/linux/user/src/thread/sem_timedwait.c delete mode 100755 porting/linux/user/src/time/__tz.c delete mode 100644 porting/linux/user/src/time/clock_getres.c delete mode 100644 porting/linux/user/src/time/clock_gettime.c delete mode 100644 porting/linux/user/src/time/gettimeofday.c delete mode 100755 porting/linux/user/src/time/gmtime_r.c delete mode 100644 porting/linux/user/src/time/strftime.c delete mode 100755 porting/linux/user/src/time/strptime.c delete mode 100644 porting/linux/user/src/time/time_impl.h delete mode 100644 porting/linux/user/src/time/timer_create.c delete mode 100644 porting/linux/user/src/unistd/getpid.c delete mode 100644 porting/liteos_a/user/arch/arm/crt_arch.h delete mode 100644 porting/liteos_a/user/arch/arm/pthread_arch.h delete mode 100644 porting/liteos_a/user/arch/arm/reloc.h delete mode 100644 porting/liteos_a/user/arch/arm/syscall_arch.h delete mode 100644 porting/liteos_a/user/include/sched.h delete mode 100644 porting/liteos_a/user/ldso/dynlink.c delete mode 100644 porting/liteos_a/user/src/aio/aio.c delete mode 100644 porting/liteos_a/user/src/aio/aio_suspend.c delete mode 100644 porting/liteos_a/user/src/aio/lio_listio.c delete mode 100644 porting/liteos_a/user/src/conf/confstr.c delete mode 100644 porting/liteos_a/user/src/conf/sysconf.c delete mode 100644 porting/liteos_a/user/src/crypt/encrypt.c delete mode 100644 porting/liteos_a/user/src/ctype/wcswidth.c delete mode 100644 porting/liteos_a/user/src/dirent/__dirent.h delete mode 100644 porting/liteos_a/user/src/env/__init_tls.c delete mode 100644 porting/liteos_a/user/src/env/__libc_start_main.c delete mode 100644 porting/liteos_a/user/src/exit/abort.c delete mode 100644 porting/liteos_a/user/src/exit/exit.c delete mode 100644 porting/liteos_a/user/src/fcntl/fcntl.c delete mode 100644 porting/liteos_a/user/src/fcntl/posix_fadvise.c delete mode 100644 porting/liteos_a/user/src/internal/libc.h delete mode 100644 porting/liteos_a/user/src/internal/pthread_impl.h delete mode 100644 porting/liteos_a/user/src/internal/unsupported_api.h delete mode 100644 porting/liteos_a/user/src/ipc/msgctl.c delete mode 100644 porting/liteos_a/user/src/ipc/msgget.c delete mode 100644 porting/liteos_a/user/src/ipc/msgrcv.c delete mode 100644 porting/liteos_a/user/src/ipc/msgsnd.c delete mode 100644 porting/liteos_a/user/src/ipc/semctl.c delete mode 100644 porting/liteos_a/user/src/ipc/semget.c delete mode 100644 porting/liteos_a/user/src/ipc/semop.c delete mode 100644 porting/liteos_a/user/src/legacy/daemon.c delete mode 100644 porting/liteos_a/user/src/legacy/getdtablesize.c delete mode 100644 porting/liteos_a/user/src/legacy/getusershell.c delete mode 100644 porting/liteos_a/user/src/legacy/isastream.c delete mode 100644 porting/liteos_a/user/src/legacy/ulimit.c delete mode 100644 porting/liteos_a/user/src/legacy/utmpx.c delete mode 100644 porting/liteos_a/user/src/linux/adjtime.c delete mode 100644 porting/liteos_a/user/src/linux/clone.c delete mode 100644 porting/liteos_a/user/src/linux/utimes.c delete mode 100644 porting/liteos_a/user/src/locale/freelocale.c delete mode 100644 porting/liteos_a/user/src/locale/strcoll.c delete mode 100644 porting/liteos_a/user/src/malloc/oldmalloc/malloc.c delete mode 100644 porting/liteos_a/user/src/misc/gethostid.c delete mode 100644 porting/liteos_a/user/src/misc/getpriority.c delete mode 100644 porting/liteos_a/user/src/misc/getrlimit.c delete mode 100644 porting/liteos_a/user/src/misc/lockf.c delete mode 100644 porting/liteos_a/user/src/misc/nftw.c delete mode 100644 porting/liteos_a/user/src/misc/openpty.c delete mode 100644 porting/liteos_a/user/src/misc/pty.c delete mode 100644 porting/liteos_a/user/src/misc/setrlimit.c delete mode 100644 porting/liteos_a/user/src/misc/syslog.c delete mode 100644 porting/liteos_a/user/src/misc/wordexp.c delete mode 100644 porting/liteos_a/user/src/mman/mlock.c delete mode 100644 porting/liteos_a/user/src/mman/mlockall.c delete mode 100644 porting/liteos_a/user/src/mman/msync.c delete mode 100644 porting/liteos_a/user/src/mman/munlock.c delete mode 100644 porting/liteos_a/user/src/mman/munlockall.c delete mode 100644 porting/liteos_a/user/src/mman/posix_madvise.c delete mode 100644 porting/liteos_a/user/src/mman/shm_open.c delete mode 100644 porting/liteos_a/user/src/mq/mq_close.c delete mode 100644 porting/liteos_a/user/src/mq/mq_notify.c delete mode 100644 porting/liteos_a/user/src/multibyte/wcsnrtombs.c delete mode 100644 porting/liteos_a/user/src/network/ent.c delete mode 100644 porting/liteos_a/user/src/network/ether.c delete mode 100644 porting/liteos_a/user/src/network/gethostbyaddr.c delete mode 100644 porting/liteos_a/user/src/network/gethostbyaddr_r.c delete mode 100644 porting/liteos_a/user/src/network/gethostbyname_r.c delete mode 100644 porting/liteos_a/user/src/network/getifaddrs.c delete mode 100644 porting/liteos_a/user/src/network/getnameinfo.c delete mode 100644 porting/liteos_a/user/src/network/getservbyname.c delete mode 100644 porting/liteos_a/user/src/network/getservbyname_r.c delete mode 100644 porting/liteos_a/user/src/network/getservbyport.c delete mode 100644 porting/liteos_a/user/src/network/getservbyport_r.c delete mode 100644 porting/liteos_a/user/src/network/if_indextoname.c delete mode 100644 porting/liteos_a/user/src/network/if_nameindex.c delete mode 100644 porting/liteos_a/user/src/network/if_nametoindex.c delete mode 100644 porting/liteos_a/user/src/network/inet_legacy.c delete mode 100644 porting/liteos_a/user/src/network/netname.c delete mode 100644 porting/liteos_a/user/src/network/proto.c delete mode 100644 porting/liteos_a/user/src/network/recvmsg.c delete mode 100644 porting/liteos_a/user/src/network/serv.c delete mode 100644 porting/liteos_a/user/src/network/sockatmark.c delete mode 100644 porting/liteos_a/user/src/network/socket.c delete mode 100644 porting/liteos_a/user/src/passwd/nscd_query.c delete mode 100644 porting/liteos_a/user/src/process/_Fork.c delete mode 100644 porting/liteos_a/user/src/process/fexecve.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawn.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawn_file_actions_addclose.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawn_file_actions_adddup2.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawn_file_actions_addopen.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_destroy.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_getflags.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_getpgroup.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_getsigdefault.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_getsigmask.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_init.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_sched.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_setflags.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_setpgroup.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_setsigdefault.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnattr_setsigmask.c delete mode 100644 porting/liteos_a/user/src/process/posix_spawnp.c delete mode 100644 porting/liteos_a/user/src/process/system.c delete mode 100644 porting/liteos_a/user/src/sched/affinity.c delete mode 100644 porting/liteos_a/user/src/sched/sched_getparam.c delete mode 100644 porting/liteos_a/user/src/sched/sched_getscheduler.c delete mode 100644 porting/liteos_a/user/src/sched/sched_setparam.c delete mode 100644 porting/liteos_a/user/src/sched/sched_setscheduler.c delete mode 100644 porting/liteos_a/user/src/sched/sched_yield.c delete mode 100644 porting/liteos_a/user/src/select/select.c delete mode 100644 porting/liteos_a/user/src/signal/getitimer.c delete mode 100644 porting/liteos_a/user/src/signal/psiginfo.c delete mode 100644 porting/liteos_a/user/src/signal/setitimer.c delete mode 100644 porting/liteos_a/user/src/signal/sigaction.c delete mode 100644 porting/liteos_a/user/src/signal/sigaltstack.c delete mode 100644 porting/liteos_a/user/src/signal/siginterrupt.c delete mode 100644 porting/liteos_a/user/src/signal/siglongjmp.c delete mode 100644 porting/liteos_a/user/src/signal/sigqueue.c delete mode 100644 porting/liteos_a/user/src/signal/sigsuspend.c delete mode 100644 porting/liteos_a/user/src/stat/mkfifo.c delete mode 100644 porting/liteos_a/user/src/stat/mkfifoat.c delete mode 100644 porting/liteos_a/user/src/stat/mknod.c delete mode 100644 porting/liteos_a/user/src/stat/mknodat.c delete mode 100644 porting/liteos_a/user/src/stdio/__stdio_exit.c delete mode 100644 porting/liteos_a/user/src/stdio/open_wmemstream.c delete mode 100644 porting/liteos_a/user/src/stdio/popen.c delete mode 100644 porting/liteos_a/user/src/stdio/tmpfile.c delete mode 100644 porting/liteos_a/user/src/stdio/tmpnam.c delete mode 100644 porting/liteos_a/user/src/termios/cfsetospeed.c delete mode 100644 porting/liteos_a/user/src/thread/__timedwait.c delete mode 100644 porting/liteos_a/user/src/thread/__wait.c delete mode 100644 porting/liteos_a/user/src/thread/arm/__set_thread_area.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_attr_get.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_attr_init.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_attr_setschedparam.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_attr_setschedpolicy.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_attr_setscope.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_barrier_wait.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_cancel.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_create.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_detach.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_getschedparam.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_join.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_mutex_getprioceiling.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_mutex_timedlock.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_mutex_trylock.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_mutex_unlock.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_mutexattr_setprotocol.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_mutexattr_setrobust.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_self.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_setschedparam.c delete mode 100644 porting/liteos_a/user/src/thread/pthread_setschedprio.c delete mode 100644 porting/liteos_a/user/src/thread/sem_open.c delete mode 100644 porting/liteos_a/user/src/thread/thrd_yield.c delete mode 100644 porting/liteos_a/user/src/time/__map_file.c delete mode 100644 porting/liteos_a/user/src/time/clock.c delete mode 100644 porting/liteos_a/user/src/time/times.c delete mode 100644 porting/liteos_a/user/src/time/utime.c delete mode 100644 porting/liteos_a/user/src/unistd/ctermid.c delete mode 100644 porting/liteos_a/user/src/unistd/fdatasync.c delete mode 100644 porting/liteos_a/user/src/unistd/lchown.c delete mode 100644 porting/liteos_a/user/src/unistd/pipe2.c delete mode 100644 porting/liteos_a/user/src/unistd/setegid.c delete mode 100644 porting/liteos_a/user/src/unistd/seteuid.c delete mode 100644 porting/liteos_a/user/src/unistd/setxid.c delete mode 100644 porting/liteos_a/user/src/unistd/tcgetpgrp.c delete mode 100644 porting/liteos_a/user/src/unistd/ttyname.c delete mode 100644 porting/liteos_a/user_debug/Makefile delete mode 100644 porting/liteos_a/user_debug/ldso/dynlink.c delete mode 100644 porting/liteos_a/user_debug/src/env/__libc_start_main.c delete mode 100644 porting/liteos_a/user_debug/src/exit/exit.c delete mode 100644 porting/liteos_a/user_debug/src/malloc/memalign.c delete mode 100644 porting/liteos_a/user_debug/src/malloc/oldmalloc/aligned_alloc.c delete mode 100644 porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc.c delete mode 100644 porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc_impl.h delete mode 100644 porting/liteos_m/user/BUILD.gn delete mode 100644 porting/liteos_m/user/include/limits.h delete mode 100644 porting/liteos_m/user/include/unistd.h delete mode 100644 porting/liteos_m/user/src/string/memcmp.c delete mode 100644 porting/liteos_m/user/src/string/memcpy.c delete mode 100644 porting/liteos_m/user/src/string/memset.c delete mode 100644 porting/liteos_m/user/src/string/strcmp.c delete mode 100644 porting/liteos_m/user/src/string/strlen.c delete mode 100644 porting/liteos_m/user/src/thread/__lock.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_destroy.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_get.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_init.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setdetachstate.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setguardsize.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setinheritsched.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setschedparam.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setschedpolicy.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setscope.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setstack.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_attr_setstacksize.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_cancel.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_detach.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_getschedparam.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_join.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_self.c delete mode 100644 porting/liteos_m/user/src/thread/pthread_setschedparam.c rename {porting/linux/user => scripts/linux}/Makefile (100%) rename {porting/linux/user => scripts/linux}/tools/install.sh (100%) rename {porting/liteos_m/user/src => scripts/liteos_m}/BUILD.gn (52%) rename {porting/liteos_m/user => scripts/liteos_m}/hook/BUILD.gn (93%) rename {porting/linux/user/src/exit => src/exit/linux}/atexit.c (100%) rename {porting/linux/user/src/fortify => src/fortify/linux}/fortify.c (100%) rename {porting/linux/user/src/gwp_asan => src/gwp_asan/linux}/gwp_asan.c (100%) rename {porting/linux/user/src/gwp_asan => src/gwp_asan/linux}/gwp_asan.h (100%) rename {porting/linux/user/src/hilog => src/hilog/linux}/hilog_adapter.c (100%) rename {porting/linux/user/src/hilog => src/hilog/linux}/hilog_common.h (100%) rename {porting/linux/user/src/hilog => src/hilog/linux}/output_p.inl (100%) rename {porting/linux/user/src/hilog => src/hilog/linux}/vsnprintf_s_p.c (100%) rename {porting/linux/user/src/hilog => src/hilog/linux}/vsnprintf_s_p.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/common_def.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/malloc_common.c (100%) rename {porting/linux/user/src/hook => src/hook/linux}/memory_trace.c (100%) rename {porting/linux/user/src/hook => src/hook/linux}/memory_trace.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_malloc.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_malloc_dispatch.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_malloc_dispatch_table.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_preinit.c (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_preinit_common.c (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_preinit_common.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_socket.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_socket_dispatch.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_socket_preinit.c (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_socket_preinit_common.c (100%) rename {porting/linux/user/src/hook => src/hook/linux}/musl_socket_preinit_common.h (100%) rename {porting/linux/user/src/hook => src/hook/linux}/socket_common.c (100%) rename {porting/liteos_m/user/hook => src/hook/liteos_m}/los_usr_libc.c (100%) rename {porting/liteos_m/user/src/include => src/include/liteos_m}/features.h (100%) rename {porting/linux/user/src/info => src/info/linux}/application_target_sdk_version.c (95%) rename porting/linux/user/src/time/time.c => src/info/linux/application_target_sdk_version.h (44%) rename {porting/linux/user/src/info => src/info/linux}/device_api_version.c (94%) rename porting/linux/user/src/linux/reboot.c => src/info/linux/device_api_version.h (60%) rename {porting/linux/user/src/info => src/info/linux}/fatal_message.c (97%) rename porting/linux/user/src/signal/siginterrupt.c => src/info/linux/fatal_message.h (50%) rename {porting/linux/user/src/internal => src/internal/linux}/hilog_adapter.h (97%) rename {porting/linux/user/src/internal => src/internal/linux}/malloc_config.h (92%) rename {porting/linux/user/src/internal => src/internal/linux}/malloc_random.h (91%) rename {porting/linux/user/src/internal => src/internal/linux}/musl_log.h (96%) rename {porting/linux/user/src/internal => src/internal/linux}/network_conf_function.c (100%) rename {porting/linux/user/src/internal => src/internal/linux}/network_conf_function.h (100%) rename {porting/linux/user/src/internal => src/internal/linux}/proc_xid_impl.h (100%) rename {porting/linux/user/src/internal => src/internal/linux}/services.h (100%) rename {porting/linux/user/src/internal => src/internal/linux}/vdso.c (98%) rename {porting/liteos_m/user/src/internal => src/internal/liteos_m}/atomic.h (100%) rename {porting/liteos_m/user/src/internal => src/internal/liteos_m}/futex.h (100%) rename {porting/liteos_m/user/src => src/internal/liteos_m}/libc_init.c (100%) rename {porting/liteos_m/user/src => src/internal/liteos_m}/libc_start.c (100%) rename {porting/liteos_m/user/src/internal => src/internal/liteos_m}/lock.h (100%) rename {porting/liteos_m/user/src/internal => src/internal/liteos_m}/pthread_impl.h (100%) rename {porting/liteos_m/user/src/internal => src/internal/liteos_m}/syscall.h (100%) create mode 100644 src/internal/liteos_m/unsupported_api.h create mode 100644 src/internal/unsupported_api.h rename {porting/linux/user/src/ldso/aarch64 => src/ldso/aarch64/linux}/dlvsym.s (100%) rename {porting/linux/user/src/ldso/arm => src/ldso/arm/linux}/dlvsym.s (100%) rename {porting/linux/user/src/ldso/riscv64 => src/ldso/riscv64/linux}/dlvsym.s (100%) rename {porting/linux/user/src/ldso/x86_64 => src/ldso/x86_64/linux}/dlvsym.s (100%) rename {porting/linux/user/src/linux/aarch64 => src/linux/aarch64/linux}/flock.s (100%) rename {porting/linux/user/src/linux/arm => src/linux/arm/linux}/flock.s (100%) rename {porting/linux/user/src => src/linux}/linux/getprocpid.c (100%) rename {porting/liteos_a/user/src/linux => src/linux/liteos_a}/cap.c (77%) rename {porting/linux/user/src => src}/linux/tgkill.c (93%) rename {porting/linux/user/src/linux/x86_64 => src/linux/x86_64/linux}/flock.s (100%) rename {porting/linux/user/src/malloc/jemalloc => src/malloc/jemalloc/linux}/jemalloc.c (100%) rename {porting/linux/user/src/malloc => src/malloc/linux}/calloc.c (100%) rename {porting/linux/user/src/malloc => src/malloc/linux}/stats.c (100%) rename {porting/liteos_a/user_debug/src/malloc => src/malloc/liteos_a/user_debug}/backtrace.c (100%) rename {porting/liteos_a/user_debug/src/malloc => src/malloc/liteos_a/user_debug}/memory_check.c (100%) rename {porting/linux/user/src/misc/aarch64 => src/misc/aarch64/linux}/fstat.s (100%) rename {porting/linux/user/src/misc/aarch64 => src/misc/aarch64/linux}/syscall.s (100%) rename {porting/liteos_a/user/src/misc => src/misc/liteos_a}/realpath.c (100%) rename {porting/linux/user/src => src}/process/arm/__vfork.s (100%) rename {porting/linux/user/src => src}/process/x86_64/__vfork.s (100%) rename {porting/linux/user/src => src}/sched/sched_cpualloc.c (96%) rename {porting/linux/user/src/sigchain => src/sigchain/linux}/sigchain.c (100%) rename {porting/liteos_a/user/src/signal/arm => src/signal/arm/liteos_a}/restore.s (98%) rename {porting/liteos_a/user/src/stdlib => src/stdlib/liteos_a}/qsort.c (100%) rename {porting/liteos_a/user/src/string/arm => src/string/arm/liteos_a}/memcmp.S (100%) rename {porting/liteos_a/user/src => src}/string/arm/memset.S (100%) rename {porting/liteos_a/user/src/thread => src/thread/liteos_a}/pthread_clone.c (100%) rename {porting/liteos_m/user/src/thread => src/thread/liteos_m}/pthread_create.c (100%) rename {porting/linux/user/src => src}/thread/pthread_cond_clockwait.c (100%) rename {porting/linux/user/src => src}/thread/pthread_cond_timedwait_monotonic_np.c (100%) rename {porting/linux/user/src => src}/thread/pthread_cond_timeout_np.c (100%) rename {porting/linux/user/src => src}/thread/pthread_mutex_clocklock.c (100%) rename {porting/linux/user/src => src}/thread/pthread_mutex_lock_timeout_np.c (100%) rename {porting/linux/user/src => src}/thread/pthread_mutex_timedlock_monotonic_np.c (100%) rename {porting/linux/user/src => src}/thread/pthread_rwlock_clockrdlock.c (100%) rename {porting/linux/user/src => src}/thread/pthread_rwlock_clockwrlock.c (100%) rename {porting/linux/user/src => src}/thread/pthread_rwlock_timedrdlock_monotonic_np.c (100%) rename {porting/linux/user/src => src}/thread/pthread_rwlock_timedwrlock_monotonic_np.c (100%) rename {porting/linux/user/src/trace => src/trace/linux}/trace_marker.c (99%) rename {porting/liteos_a/user/src/unistd => src/unistd/liteos_a}/format.c (100%) diff --git a/BUILD.gn b/BUILD.gn index 5c8b6a39b..64f50ec0b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -33,12 +33,11 @@ if (is_lite_system && current_os == "ohos") { if (musl_arch != "mips" && musl_arch != "riscv64") { ohos_prebuilt_etc("ld-musl-namespace-${musl_arch}-test.ini") { - source = - "${musl_porting_dir}/config/ld-musl-namespace-${musl_arch}-test.ini" + source = "${musl_dir}/config/ld-musl-namespace-${musl_arch}-test.ini" } ohos_prebuilt_etc("ld-musl-namespace-${musl_arch}.ini") { - source = "${musl_porting_dir}/config/ld-musl-namespace-${musl_arch}.ini" + source = "${musl_dir}/config/ld-musl-namespace-${musl_arch}.ini" install_images = [ "system", "updater", @@ -68,12 +67,12 @@ if (is_lite_system && current_os == "ohos") { } ohos_prebuilt_etc("musl_sysparam") { - source = "${musl_porting_dir}/etc/musl.para" + source = "${musl_dir}/etc/musl.para" relative_install_dir = "param" } ohos_prebuilt_etc("musl_sysparam_dac") { - source = "${musl_porting_dir}/etc/musl.para.dac" + source = "${musl_dir}/etc/musl.para.dac" relative_install_dir = "param" } @@ -122,6 +121,8 @@ if (is_lite_system && current_os == "ohos") { src_files += musl_inc_sys_files src_files += musl_inc_fortify_files src_files += musl_inc_root_files + src_files += musl_inc_info_files + src_files += musl_inc_trace_files src_files += [ "crt/Scrt1.c", @@ -156,7 +157,7 @@ if (is_lite_system && current_os == "ohos") { args += [ "-p" ] + [ "${musl_target_os}" ] foreach(i, src_files) { - foreach(s, musl_src_porting_file) { + foreach(s, musl_src_files_ext) { if (i == s) { src_files -= [ "${s}" ] } @@ -168,7 +169,7 @@ if (is_lite_system && current_os == "ohos") { outputs += [ "${target_out_dir}/${musl_ported_dir}/${s}" ] } - foreach(s, musl_src_porting_file) { + foreach(s, musl_src_files_ext) { outputs += [ "${target_out_dir}/${musl_ported_dir}/${s}" ] } @@ -177,8 +178,8 @@ if (is_lite_system && current_os == "ohos") { inputs += [ "${musl_dir}/${s}" ] } - foreach(s, musl_src_porting_file) { - inputs += [ "${musl_porting_dir}/${s}" ] + foreach(s, musl_src_files_ext) { + inputs += [ "${musl_dir}/${s}" ] } } diff --git a/arch/aarch64/syscall_arch.h b/arch/aarch64/syscall_arch.h index 504983aa2..3bb2dc6b0 100644 --- a/arch/aarch64/syscall_arch.h +++ b/arch/aarch64/syscall_arch.h @@ -1,3 +1,7 @@ +#ifdef LINUX_MUSL_SUPPORTED +#include "syscall_hooks.h" +#endif + #define __SYSCALL_LL_E(x) (x) #define __SYSCALL_LL_O(x) (x) @@ -9,6 +13,11 @@ static inline long __syscall0(long n) { +#ifdef LINUX_MUSL_SUPPORTED + if (is_syscall_hooked(n)) { + return __syscall_hooks_entry0(n); + } +#endif register long x8 __asm__("x8") = n; register long x0 __asm__("x0"); __asm_syscall("r"(x8)); @@ -16,6 +25,11 @@ static inline long __syscall0(long n) static inline long __syscall1(long n, long a) { +#ifdef LINUX_MUSL_SUPPORTED + if (is_syscall_hooked(n)) { + return __syscall_hooks_entry1(n, a); + } +#endif register long x8 __asm__("x8") = n; register long x0 __asm__("x0") = a; __asm_syscall("r"(x8), "0"(x0)); @@ -23,6 +37,11 @@ static inline long __syscall1(long n, long a) static inline long __syscall2(long n, long a, long b) { +#ifdef LINUX_MUSL_SUPPORTED + if (is_syscall_hooked(n)) { + return __syscall_hooks_entry2(n, a, b); + } +#endif register long x8 __asm__("x8") = n; register long x0 __asm__("x0") = a; register long x1 __asm__("x1") = b; @@ -31,6 +50,11 @@ static inline long __syscall2(long n, long a, long b) static inline long __syscall3(long n, long a, long b, long c) { +#ifdef LINUX_MUSL_SUPPORTED + if (is_syscall_hooked(n)) { + return __syscall_hooks_entry3(n, a, b, c); + } +#endif register long x8 __asm__("x8") = n; register long x0 __asm__("x0") = a; register long x1 __asm__("x1") = b; @@ -40,6 +64,11 @@ static inline long __syscall3(long n, long a, long b, long c) static inline long __syscall4(long n, long a, long b, long c, long d) { +#ifdef LINUX_MUSL_SUPPORTED + if (is_syscall_hooked(n)) { + return __syscall_hooks_entry4(n, a, b, c, d); + } +#endif register long x8 __asm__("x8") = n; register long x0 __asm__("x0") = a; register long x1 __asm__("x1") = b; @@ -50,6 +79,11 @@ static inline long __syscall4(long n, long a, long b, long c, long d) static inline long __syscall5(long n, long a, long b, long c, long d, long e) { +#ifdef LINUX_MUSL_SUPPORTED + if (is_syscall_hooked(n)) { + return __syscall_hooks_entry5(n, a, b, c, d, e); + } +#endif register long x8 __asm__("x8") = n; register long x0 __asm__("x0") = a; register long x1 __asm__("x1") = b; @@ -61,6 +95,11 @@ static inline long __syscall5(long n, long a, long b, long c, long d, long e) static inline long __syscall6(long n, long a, long b, long c, long d, long e, long f) { +#ifdef LINUX_MUSL_SUPPORTED + if (is_syscall_hooked(n)) { + return __syscall_hooks_entry6(n, a, b, c, d, e, f); + } +#endif register long x8 __asm__("x8") = n; register long x0 __asm__("x0") = a; register long x1 __asm__("x1") = b; @@ -74,5 +113,11 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo #define VDSO_USEFUL #define VDSO_CGT_SYM "__kernel_clock_gettime" #define VDSO_CGT_VER "LINUX_2.6.39" +#ifdef LINUX_MUSL_SUPPORTED +#define VDSO_CGR_SYM "__kernel_clock_getres" +#define VDSO_CGR_VER "LINUX_2.6.39" +#define VDSO_GTD_SYM "__kernel_gettimeofday" +#define VDSO_GTD_VER "LINUX_2.6.39" +#endif #define IPC_64 0 diff --git a/arch/arm/bits/fenv.h b/arch/arm/bits/fenv.h index d85fc86d7..303b2c352 100644 --- a/arch/arm/bits/fenv.h +++ b/arch/arm/bits/fenv.h @@ -9,10 +9,16 @@ #define FE_INEXACT 16 #define FE_ALL_EXCEPT 31 #define FE_TONEAREST 0 +#ifndef LINUX_MUSL_SUPPORTED #define FE_DOWNWARD 0x800000 #define FE_UPWARD 0x400000 +#endif #define FE_TOWARDZERO 0xc00000 #endif +#ifdef LINUX_MUSL_SUPPORTED +#define FE_DOWNWARD 0x800000 +#define FE_UPWARD 0x400000 +#endif typedef unsigned long fexcept_t; diff --git a/porting/liteos_a/user/arch/arm/bits/syscall.h.in b/arch/arm/bits/liteos_a/syscall.h.in similarity index 100% rename from porting/liteos_a/user/arch/arm/bits/syscall.h.in rename to arch/arm/bits/liteos_a/syscall.h.in diff --git a/arch/arm/crt_arch.h b/arch/arm/crt_arch.h index 99508b1db..1196d7cb7 100644 --- a/arch/arm/crt_arch.h +++ b/arch/arm/crt_arch.h @@ -7,8 +7,13 @@ START ": \n" " mov lr, #0 \n" " ldr a2, 1f \n" " add a2, pc, a2 \n" +#ifdef __LITEOS_A__ +" mov a3, sp \n" +"2: and ip, a3, #-16 \n" +#else " mov a1, sp \n" "2: and ip, a1, #-16 \n" +#endif " mov sp, ip \n" " bl " START "_c \n" ".weak _DYNAMIC \n" diff --git a/porting/liteos_m/user/arch/arm/atomic_arch.h b/arch/arm/liteos_m/atomic_arch.h similarity index 100% rename from porting/liteos_m/user/arch/arm/atomic_arch.h rename to arch/arm/liteos_m/atomic_arch.h diff --git a/porting/liteos_m/user/arch/arm/pthread_arch.h b/arch/arm/liteos_m/pthread_arch.h similarity index 100% rename from porting/liteos_m/user/arch/arm/pthread_arch.h rename to arch/arm/liteos_m/pthread_arch.h diff --git a/porting/liteos_m/user/arch/arm/syscall_arch.h b/arch/arm/liteos_m/syscall_arch.h similarity index 100% rename from porting/liteos_m/user/arch/arm/syscall_arch.h rename to arch/arm/liteos_m/syscall_arch.h diff --git a/arch/arm/pthread_arch.h b/arch/arm/pthread_arch.h index 157e2eae6..192f71a26 100644 --- a/arch/arm/pthread_arch.h +++ b/arch/arm/pthread_arch.h @@ -26,6 +26,9 @@ static inline uintptr_t __get_tp() #endif +#ifdef __LITEOS_A__ +extern pthread_t __pthread_self(); +#endif #define TLS_ABOVE_TP #define GAP_ABOVE_TP 8 diff --git a/arch/arm/reloc.h b/arch/arm/reloc.h index d091d2ad9..3f86825a1 100644 --- a/arch/arm/reloc.h +++ b/arch/arm/reloc.h @@ -27,6 +27,10 @@ #define REL_TLSDESC R_ARM_TLS_DESC #define TLSDESC_BACKWARDS - +#ifdef __LITEOS_A__ +#define CRTJMP(pc,sp) __asm__ __volatile__( \ + "mov r0,%1 ; bx %0" : : "r"(pc), "r"(sp) : "memory", "r0" ) +#else #define CRTJMP(pc,sp) __asm__ __volatile__( \ "mov sp,%1 ; bx %0" : : "r"(pc), "r"(sp) : "memory" ) +#endif diff --git a/arch/arm/syscall_arch.h b/arch/arm/syscall_arch.h index a877b2cff..b47f66d24 100644 --- a/arch/arm/syscall_arch.h +++ b/arch/arm/syscall_arch.h @@ -98,6 +98,13 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo __asm_syscall(R7_OPERAND, "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5)); } +#ifdef __LITEOS_A__ +#define VDSO_USEFUL +#define VDSO_CGT32_SYM "VdsoClockGettime" +#define VDSO_CGT32_VER "OHOS" +#define VDSO_CGT_SYM "VdsoClockGettime" +#define VDSO_CGT_VER "OHOS" +#endif #define SYSCALL_FADVISE_6_ARG #define SYSCALL_IPC_BROKEN_MODE diff --git a/arch/generic/bits/shm.h b/arch/generic/bits/shm.h index 8d1937819..49a5b6a48 100644 --- a/arch/generic/bits/shm.h +++ b/arch/generic/bits/shm.h @@ -1,4 +1,8 @@ +#ifdef LINUX_MUSL_SUPPORTED +#define SHMLBA (4*4096) +#else #define SHMLBA 4096 +#endif struct shmid_ds { struct ipc_perm shm_perm; diff --git a/porting/linux/user/arch/generic/crtbrand.s b/arch/generic/crtbrand.s similarity index 100% rename from porting/linux/user/arch/generic/crtbrand.s rename to arch/generic/crtbrand.s diff --git a/arch/x86_64/bits/float.h b/arch/x86_64/bits/float.h index 4d8e78641..78b53875f 100644 --- a/arch/x86_64/bits/float.h +++ b/arch/x86_64/bits/float.h @@ -4,17 +4,28 @@ #define FLT_EVAL_METHOD 0 #endif +#ifdef LINUX_MUSL_SUPPORTED +#define LDBL_TRUE_MIN 6.47517511943802511092443895822764655e-4966L +#define LDBL_MIN 3.36210314311209350626267781732175260e-4932L +#define LDBL_MAX 1.18973149535723176508575932662800702e+4932L +#define LDBL_EPSILON 1.92592994438723585305597794258492732e-34L + +#define LDBL_MANT_DIG 113 +#define LDBL_DIG 33 +#define DECIMAL_DIG 36 +#else #define LDBL_TRUE_MIN 3.6451995318824746025e-4951L #define LDBL_MIN 3.3621031431120935063e-4932L #define LDBL_MAX 1.1897314953572317650e+4932L #define LDBL_EPSILON 1.0842021724855044340e-19L #define LDBL_MANT_DIG 64 +#define LDBL_DIG 18 +#define DECIMAL_DIG 21 +#endif + #define LDBL_MIN_EXP (-16381) #define LDBL_MAX_EXP 16384 -#define LDBL_DIG 18 #define LDBL_MIN_10_EXP (-4931) #define LDBL_MAX_10_EXP 4932 - -#define DECIMAL_DIG 21 diff --git a/porting/linux/user/config/README_zh.md b/config/README_zh.md similarity index 100% rename from porting/linux/user/config/README_zh.md rename to config/README_zh.md diff --git a/porting/linux/user/config/ld-musl-namespace-aarch64-test.ini b/config/ld-musl-namespace-aarch64-test.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-aarch64-test.ini rename to config/ld-musl-namespace-aarch64-test.ini diff --git a/porting/linux/user/config/ld-musl-namespace-aarch64.ini b/config/ld-musl-namespace-aarch64.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-aarch64.ini rename to config/ld-musl-namespace-aarch64.ini diff --git a/porting/linux/user/config/ld-musl-namespace-arm-test.ini b/config/ld-musl-namespace-arm-test.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-arm-test.ini rename to config/ld-musl-namespace-arm-test.ini diff --git a/porting/linux/user/config/ld-musl-namespace-arm.ini b/config/ld-musl-namespace-arm.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-arm.ini rename to config/ld-musl-namespace-arm.ini diff --git a/porting/linux/user/config/ld-musl-namespace-riscv64-test.ini b/config/ld-musl-namespace-riscv64-test.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-riscv64-test.ini rename to config/ld-musl-namespace-riscv64-test.ini diff --git a/porting/linux/user/config/ld-musl-namespace-riscv64.ini b/config/ld-musl-namespace-riscv64.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-riscv64.ini rename to config/ld-musl-namespace-riscv64.ini diff --git a/porting/linux/user/config/ld-musl-namespace-x86_64-test.ini b/config/ld-musl-namespace-x86_64-test.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-x86_64-test.ini rename to config/ld-musl-namespace-x86_64-test.ini diff --git a/porting/linux/user/config/ld-musl-namespace-x86_64.ini b/config/ld-musl-namespace-x86_64.ini similarity index 100% rename from porting/linux/user/config/ld-musl-namespace-x86_64.ini rename to config/ld-musl-namespace-x86_64.ini diff --git a/crt/aarch64/crti.s b/crt/aarch64/crti.s index 775df0ac0..46d8dec4e 100644 --- a/crt/aarch64/crti.s +++ b/crt/aarch64/crti.s @@ -1,6 +1,13 @@ +.ifdef LINUX_MUSL_SUPPORTED +.include "crtbrand.s" +.endif + .section .init .global _init .type _init,%function +.ifdef LINUX_MUSL_SUPPORTED +.balign 4 +.endif _init: stp x29,x30,[sp,-16]! mov x29,sp @@ -8,6 +15,9 @@ _init: .section .fini .global _fini .type _fini,%function +.ifdef LINUX_MUSL_SUPPORTED +.balign 4 +.endif _fini: stp x29,x30,[sp,-16]! mov x29,sp diff --git a/crt/arm/crti.s b/crt/arm/crti.s index 18dc1e419..ad14eea27 100644 --- a/crt/arm/crti.s +++ b/crt/arm/crti.s @@ -1,13 +1,23 @@ +.ifdef LINUX_MUSL_SUPPORTED +.include "crtbrand.s" +.endif + .syntax unified .section .init .global _init .type _init,%function +.ifdef LINUX_MUSL_SUPPORTED +.balign 4 +.endif _init: push {r0,lr} .section .fini .global _fini .type _fini,%function +.ifdef LINUX_MUSL_SUPPORTED +.balign 4 +.endif _fini: push {r0,lr} diff --git a/porting/linux/user/crt/crtplus.c b/crt/linux/crtplus.c similarity index 100% rename from porting/linux/user/crt/crtplus.c rename to crt/linux/crtplus.c diff --git a/crt/x86_64/crti.s b/crt/x86_64/crti.s index 4788968b2..e5b7442da 100644 --- a/crt/x86_64/crti.s +++ b/crt/x86_64/crti.s @@ -1,3 +1,7 @@ +.ifdef LINUX_MUSL_SUPPORTED +.include "crtbrand.s" +.endif + .section .init .global _init _init: diff --git a/porting/linux/user/etc/musl.para b/etc/musl.para similarity index 100% rename from porting/linux/user/etc/musl.para rename to etc/musl.para diff --git a/porting/linux/user/etc/musl.para.dac b/etc/musl.para.dac similarity index 100% rename from porting/linux/user/etc/musl.para.dac rename to etc/musl.para.dac diff --git a/include/ctype.h b/include/ctype.h index 32bcef4da..2feae0614 100644 --- a/include/ctype.h +++ b/include/ctype.h @@ -68,6 +68,10 @@ int toascii(int); #define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128) #endif +#include + +const int32_t **__ctype_tolower_loc(void); +const int32_t **__ctype_toupper_loc(void); #endif #ifdef __cplusplus diff --git a/include/dlfcn.h b/include/dlfcn.h index 13ab71dd0..ac561f8f4 100644 --- a/include/dlfcn.h +++ b/include/dlfcn.h @@ -1,12 +1,13 @@ #ifndef _DLFCN_H #define _DLFCN_H +#include +#include + #ifdef __cplusplus extern "C" { #endif -#include - #define RTLD_LAZY 1 #define RTLD_NOW 2 #define RTLD_NOLOAD 4 @@ -19,11 +20,149 @@ extern "C" { #define RTLD_DI_LINKMAP 2 +/* create flags for dlns_create */ +#define CREATE_INHERIT_DEFAULT 0x1 +#define CREATE_INHERIT_CURRENT 0x2 +#define LOCAL_NS_PREFERED 0x4 /* Use app's library when app's library has same name as system library. */ + int dlclose(void *); char *dlerror(void); void *dlopen(const char *, int); void *dlsym(void *__restrict, const char *__restrict); +/* namespace apis */ +#define NS_NAME_MAX 255 +typedef struct { + char name[NS_NAME_MAX+1]; +} Dl_namespace; + +void *dlvsym(void *__restrict, const char *__restrict, const char *__restrict); + +/** + * @brief Initialize a namespace structure for operating namespaces through related functional interfaces. + * @param Dl_namespace * namespace handle. + * @param char * namespace name. + * @return void. + * @retval none. + */ +void dlns_init(Dl_namespace *, const char *); + +/** + * @brief Gets the current namespace handle, or verifies that the given name namespace exists. + * @param char * Namespace name.Gets the current caller namespace handle when name is null. + * @param Dl_namespace * namespace handle. + * @return return 0 on success,fail other values. + * @retval + * EINVAL(22) Invalid argument. + * ENOKEY(126) Required key not available. + */ +int dlns_get(const char *, Dl_namespace *); + +/** + * @brief open dso in given namespace which has own lib search paths, when namespace is null, it's same to dlopen(). + * avoid using "default" as namespace, which is the default namespace. + * @param Dl_namespace * namespace handle. + * @param char * the name of the so file you want to open. + * @param int open file mode. + * -- RTLD_LAZY. + * -- RTLD_NOW. + * -- RTLD_NOLOAD. + * -- RTLD_NODELETE. + * -- RTLD_GLOBAL. + * -- RTLD_LOCAL. + * @return success: dynamic library handleoid,failed: NULL. + * @retval none. + */ +void *dlopen_ns(Dl_namespace *, const char *, int); + +/** + * @brief create the namespace and set lib search paths of namespace, + * the paths should be splited by ':'. When namespace already exist,return error. + * avoid using "default" as namespace, which is the default namespace. + * @param Dl_namespace * namespace handle. + * @param char * lib path library that can be specified. + * @return return 0 on success,fail other values. + * @retval + * EINVAL(22) Invalid argument. + * EEXIST(17) File exists. + * ENOMEM(12) Out of memory. + */ +int dlns_create(Dl_namespace *, const char *); + +/** + * @brief create the namespace and set lib search paths of namespace, + * like dlns_create, except can use flags to set parent ns. + * @param Dl_namespace * namespace handle. + * @param char * lib path library that can be specified. + * #param int flags for create namespace, CREATE_INHERIT_CURRENT or CREATE_INHERIT_DEFAULT. + * @return return 0 on success,fail other values. + * @retval + * EINVAL(22) Invalid argument. + * EEXIST(17) File exists. + * ENOMEM(12) Out of memory. + */ +int dlns_create2(Dl_namespace *, const char *, int); + +/** + * @brief make one namespace inherit another, and so it can use shared libs by the inherited one. + * param1: namespace, param2: inherited namespace, param3: shared libs. + * the shared libs should be splited by ':'. when it is null or empty, all libs can be shared. + * one namespace can inherit or be inherited by multiple ones. + * When namespaces do not exist, return error. + * @param Dl_namespace * The first parameter is the namespace to inherit from. + * @param Dl_namespace * The second parameter is the inherited namespace. + * @param char * some library names to inherit. + * @return return 0 on success,fail other values. + * @retval + * EINVAL(22) Invalid argument. + * ENOKEY(126) Required key not available. + */ +int dlns_inherit(Dl_namespace *, Dl_namespace *, const char *); + +/** + * @brief Set namespace lib_path. + * @param name namespace name. + * @param lib_path The lib path name that needs to be reset, it can be multiple, link with ":". + * @return Returns 0 on success, other on failure. + * @retval + * EINVAL(22) Invalid argument. + * ENOKEY(126) Required key not available. + */ +int dlns_set_namespace_lib_path(const char *name, const char *lib_path); + +/** + * @brief Set namespace separated. + * @param name namespace name. + * @param separated separated. + * @return Returns 0 on success, other on failure. + * @retval + * EINVAL(22) Invalid argument. + * ENOKEY(126) Required key not available. + */ +int dlns_set_namespace_separated(const char *name, const bool separated); + +/** + * @brief Set namespace permitted_paths. + * @param name namespace name. + * @param permitted_paths set new permitted_paths. + * @return Returns 0 on success, other on failure. + * @retval + * EINVAL(22) Invalid argument. + * ENOKEY(126) Required key not available. + */ +int dlns_set_namespace_permitted_paths(const char *name, const char *permitted_paths); + +/** + * @brief Set namespace allowed_libs. + * @param name namespace name. + * @param allowed_libs set new allowed_libs. + * @return Returns 0 on success, other on failure. + * @retval + * EINVAL(22) Invalid argument. + * ENOKEY(126) Required key not available. + */ +int dlns_set_namespace_allowed_libs(const char *name, const char *allowed_libs); + #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) typedef struct { const char *dli_fname; diff --git a/include/elf.h b/include/elf.h index 86e2f0bb7..91ac0b33d 100644 --- a/include/elf.h +++ b/include/elf.h @@ -385,7 +385,12 @@ typedef struct { #define SHT_PREINIT_ARRAY 16 #define SHT_GROUP 17 #define SHT_SYMTAB_SHNDX 18 +#ifdef LINUX_MUSL_SUPPORTED +#define SHT_RELR 19 +#define SHT_NUM 20 +#else #define SHT_NUM 19 +#endif #define SHT_LOOS 0x60000000 #define SHT_GNU_ATTRIBUTES 0x6ffffff5 #define SHT_GNU_HASH 0x6ffffff6 @@ -612,7 +617,9 @@ typedef struct { #define PT_LOPROC 0x70000000 #define PT_HIPROC 0x7fffffff - +#ifdef LINUX_MUSL_SUPPORTED +#define PT_OHOS_RANDOMDATA 0x6788fc60 +#endif #define PN_XNUM 0xffff @@ -754,7 +761,14 @@ typedef struct { #define DT_PREINIT_ARRAY 32 #define DT_PREINIT_ARRAYSZ 33 #define DT_SYMTAB_SHNDX 34 +#ifdef LINUX_MUSL_SUPPORTED +#define DT_RELRSZ 35 +#define DT_RELR 36 +#define DT_RELRENT 37 +#define DT_NUM 38 +#else #define DT_NUM 35 +#endif #define DT_LOOS 0x6000000d #define DT_HIOS 0x6ffff000 #define DT_LOPROC 0x70000000 diff --git a/include/fcntl.h b/include/fcntl.h index b664cdc44..13f0e7d48 100644 --- a/include/fcntl.h +++ b/include/fcntl.h @@ -20,6 +20,9 @@ extern "C" { #include #include +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif struct flock { short l_type; diff --git a/porting/linux/user/include/fortify/fcntl.h b/include/fortify/linux/fcntl.h similarity index 100% rename from porting/linux/user/include/fortify/fcntl.h rename to include/fortify/linux/fcntl.h diff --git a/porting/linux/user/include/fortify/fortify.h b/include/fortify/linux/fortify.h similarity index 100% rename from porting/linux/user/include/fortify/fortify.h rename to include/fortify/linux/fortify.h diff --git a/porting/linux/user/include/fortify/poll.h b/include/fortify/linux/poll.h similarity index 100% rename from porting/linux/user/include/fortify/poll.h rename to include/fortify/linux/poll.h diff --git a/porting/linux/user/include/fortify/socket.h b/include/fortify/linux/socket.h similarity index 100% rename from porting/linux/user/include/fortify/socket.h rename to include/fortify/linux/socket.h diff --git a/porting/linux/user/include/fortify/stat.h b/include/fortify/linux/stat.h similarity index 100% rename from porting/linux/user/include/fortify/stat.h rename to include/fortify/linux/stat.h diff --git a/porting/linux/user/include/fortify/stdio.h b/include/fortify/linux/stdio.h similarity index 100% rename from porting/linux/user/include/fortify/stdio.h rename to include/fortify/linux/stdio.h diff --git a/porting/linux/user/include/fortify/stdlib.h b/include/fortify/linux/stdlib.h similarity index 100% rename from porting/linux/user/include/fortify/stdlib.h rename to include/fortify/linux/stdlib.h diff --git a/porting/linux/user/include/fortify/string.h b/include/fortify/linux/string.h similarity index 100% rename from porting/linux/user/include/fortify/string.h rename to include/fortify/linux/string.h diff --git a/porting/linux/user/include/fortify/unistd.h b/include/fortify/linux/unistd.h similarity index 100% rename from porting/linux/user/include/fortify/unistd.h rename to include/fortify/linux/unistd.h diff --git a/porting/linux/user/include/info/application_target_sdk_version.h b/include/info/linux/application_target_sdk_version.h similarity index 100% rename from porting/linux/user/include/info/application_target_sdk_version.h rename to include/info/linux/application_target_sdk_version.h diff --git a/porting/linux/user/include/info/device_api_version.h b/include/info/linux/device_api_version.h similarity index 100% rename from porting/linux/user/include/info/device_api_version.h rename to include/info/linux/device_api_version.h diff --git a/porting/linux/user/include/info/fatal_message.h b/include/info/linux/fatal_message.h similarity index 100% rename from porting/linux/user/include/info/fatal_message.h rename to include/info/linux/fatal_message.h diff --git a/include/langinfo.h b/include/langinfo.h index 519c06129..290741489 100644 --- a/include/langinfo.h +++ b/include/langinfo.h @@ -68,6 +68,9 @@ extern "C" { #define ERA_D_T_FMT 0x20030 #define ERA_T_FMT 0x20031 +#define AM_STR_LOWER 0x20032 +#define PM_STR_LOWER 0x20033 + #define CODESET 14 #define CRNCYSTR 0x4000F diff --git a/include/limits.h b/include/limits.h index 53a27b9de..27f3c34a7 100644 --- a/include/limits.h +++ b/include/limits.h @@ -44,9 +44,14 @@ #ifndef NAME_MAX #define NAME_MAX 255 #endif +#ifdef __LITEOS_A__ +#define PATH_MAX 256 +#define ARG_MAX 4096 +#else #define PATH_MAX 4096 -#define NGROUPS_MAX 32 #define ARG_MAX 131072 +#endif +#define NGROUPS_MAX 32 #define IOV_MAX 1024 #define SYMLOOP_MAX 40 #define WORD_BIT 32 @@ -69,7 +74,11 @@ #define SEM_VALUE_MAX 0x7fffffff #define SEM_NSEMS_MAX 256 #define DELAYTIMER_MAX 0x7fffffff +#ifdef __LITEOS_A__ +#define MQ_PRIO_MAX 1 +#else #define MQ_PRIO_MAX 32768 +#endif #define LOGIN_NAME_MAX 256 /* Arbitrary numbers... */ diff --git a/porting/linux/user/include/dlfcn_ext.h b/include/linux/dlfcn_ext.h similarity index 100% rename from porting/linux/user/include/dlfcn_ext.h rename to include/linux/dlfcn_ext.h diff --git a/porting/linux/user/include/sigchain.h b/include/linux/sigchain.h similarity index 100% rename from porting/linux/user/include/sigchain.h rename to include/linux/sigchain.h diff --git a/porting/liteos_a/user_debug/include/debug.h b/include/liteos_a/user_debug/debug.h similarity index 100% rename from porting/liteos_a/user_debug/include/debug.h rename to include/liteos_a/user_debug/debug.h diff --git a/porting/liteos_m/user/include/alloca.h b/include/liteos_m/alloca.h similarity index 100% rename from porting/liteos_m/user/include/alloca.h rename to include/liteos_m/alloca.h diff --git a/porting/liteos_m/user/include/bits/alltypes.h b/include/liteos_m/bits/alltypes.h similarity index 100% rename from porting/liteos_m/user/include/bits/alltypes.h rename to include/liteos_m/bits/alltypes.h diff --git a/porting/liteos_m/user/include/bits/errno.h b/include/liteos_m/bits/errno.h similarity index 100% rename from porting/liteos_m/user/include/bits/errno.h rename to include/liteos_m/bits/errno.h diff --git a/porting/liteos_m/user/include/bits/fcntl.h b/include/liteos_m/bits/fcntl.h similarity index 100% rename from porting/liteos_m/user/include/bits/fcntl.h rename to include/liteos_m/bits/fcntl.h diff --git a/porting/liteos_m/user/include/bits/limits.h b/include/liteos_m/bits/limits.h similarity index 100% rename from porting/liteos_m/user/include/bits/limits.h rename to include/liteos_m/bits/limits.h diff --git a/porting/liteos_m/user/include/bits/posix.h b/include/liteos_m/bits/posix.h similarity index 100% rename from porting/liteos_m/user/include/bits/posix.h rename to include/liteos_m/bits/posix.h diff --git a/porting/liteos_m/user/include/bits/stdint.h b/include/liteos_m/bits/stdint.h similarity index 100% rename from porting/liteos_m/user/include/bits/stdint.h rename to include/liteos_m/bits/stdint.h diff --git a/porting/liteos_m/user/include/bits/syscall.h b/include/liteos_m/bits/syscall.h similarity index 100% rename from porting/liteos_m/user/include/bits/syscall.h rename to include/liteos_m/bits/syscall.h diff --git a/porting/liteos_m/user/include/errno.h b/include/liteos_m/errno.h similarity index 100% rename from porting/liteos_m/user/include/errno.h rename to include/liteos_m/errno.h diff --git a/porting/liteos_m/user/include/fcntl.h b/include/liteos_m/fcntl.h similarity index 100% rename from porting/liteos_m/user/include/fcntl.h rename to include/liteos_m/fcntl.h diff --git a/porting/liteos_m/user/include/features.h b/include/liteos_m/features.h similarity index 100% rename from porting/liteos_m/user/include/features.h rename to include/liteos_m/features.h diff --git a/porting/liteos_a/user/include/limits.h b/include/liteos_m/limits.h similarity index 100% rename from porting/liteos_a/user/include/limits.h rename to include/liteos_m/limits.h diff --git a/porting/liteos_m/user/include/pthread.h b/include/liteos_m/pthread.h similarity index 100% rename from porting/liteos_m/user/include/pthread.h rename to include/liteos_m/pthread.h diff --git a/porting/liteos_m/user/include/sched.h b/include/liteos_m/sched.h similarity index 100% rename from porting/liteos_m/user/include/sched.h rename to include/liteos_m/sched.h diff --git a/porting/liteos_m/user/include/semaphore.h b/include/liteos_m/semaphore.h similarity index 100% rename from porting/liteos_m/user/include/semaphore.h rename to include/liteos_m/semaphore.h diff --git a/porting/liteos_m/user/include/stdbool.h b/include/liteos_m/stdbool.h similarity index 100% rename from porting/liteos_m/user/include/stdbool.h rename to include/liteos_m/stdbool.h diff --git a/porting/liteos_m/user/include/stdint.h b/include/liteos_m/stdint.h similarity index 100% rename from porting/liteos_m/user/include/stdint.h rename to include/liteos_m/stdint.h diff --git a/porting/liteos_m/user/include/stdlib.h b/include/liteos_m/stdlib.h similarity index 100% rename from porting/liteos_m/user/include/stdlib.h rename to include/liteos_m/stdlib.h diff --git a/porting/liteos_m/user/include/string.h b/include/liteos_m/string.h similarity index 100% rename from porting/liteos_m/user/include/string.h rename to include/liteos_m/string.h diff --git a/porting/liteos_m/user/include/strings.h b/include/liteos_m/strings.h similarity index 100% rename from porting/liteos_m/user/include/strings.h rename to include/liteos_m/strings.h diff --git a/porting/liteos_m/user/include/sys/syscall.h b/include/liteos_m/sys/syscall.h similarity index 100% rename from porting/liteos_m/user/include/sys/syscall.h rename to include/liteos_m/sys/syscall.h diff --git a/porting/liteos_m/user/include/syscall.h b/include/liteos_m/syscall.h similarity index 100% rename from porting/liteos_m/user/include/syscall.h rename to include/liteos_m/syscall.h diff --git a/porting/liteos_m/user/include/time.h b/include/liteos_m/time.h similarity index 100% rename from porting/liteos_m/user/include/time.h rename to include/liteos_m/time.h diff --git a/porting/liteos_a/user/include/unistd.h b/include/liteos_m/unistd.h similarity index 100% rename from porting/liteos_a/user/include/unistd.h rename to include/liteos_m/unistd.h diff --git a/porting/liteos_m/user/include/usr_lib.h b/include/liteos_m/usr_lib.h similarity index 100% rename from porting/liteos_m/user/include/usr_lib.h rename to include/liteos_m/usr_lib.h diff --git a/porting/liteos_m/user/include/usr_lib_define.h b/include/liteos_m/usr_lib_define.h similarity index 100% rename from porting/liteos_m/user/include/usr_lib_define.h rename to include/liteos_m/usr_lib_define.h diff --git a/porting/liteos_m/user/include/usr_lib_ops.h b/include/liteos_m/usr_lib_ops.h similarity index 100% rename from porting/liteos_m/user/include/usr_lib_ops.h rename to include/liteos_m/usr_lib_ops.h diff --git a/include/locale.h b/include/locale.h index 11106fea8..a5a2802cb 100644 --- a/include/locale.h +++ b/include/locale.h @@ -21,7 +21,17 @@ extern "C" { #define LC_COLLATE 3 #define LC_MONETARY 4 #define LC_MESSAGES 5 +#ifdef LINUX_MUSL_SUPPORTED +#define LC_PAPER 6 +#define LC_NAME 7 +#define LC_ADDRESS 8 +#define LC_TELEPHONE 9 +#define LC_MEASUREMENT 10 +#define LC_IDENTIFICATION 11 +#define LC_ALL 12 +#else #define LC_ALL 6 +#endif struct lconv { char *decimal_point; @@ -71,6 +81,14 @@ struct lconv *localeconv(void); #define LC_COLLATE_MASK (1< #ifdef __cplusplus extern "C" { #endif #define __NEED_size_t +#define __NEED_ssize_t +#define __NEED_uintptr_t #include @@ -38,6 +41,32 @@ struct mallinfo2 { __MALLINFO_BODY }; struct mallinfo2 mallinfo2(void); +int malloc_iterate(void* base, size_t size, void (*callback)(void* base, size_t size, void* arg), void* arg); +void malloc_disable(void); +void malloc_enable(void); + +int malloc_info(int options, FILE* fp); +void malloc_stats_print(void (*write_cb) (void *, const char *), void *cbopaque, const char *opts); + + +#define M_SET_THREAD_CACHE -1001 +#define M_THREAD_CACHE_ENABLE 1 +#define M_THREAD_CACHE_DISABLE 0 + +#define M_FLUSH_THREAD_CACHE -1002 + +#define M_DELAYED_FREE -1003 +#define M_DELAYED_FREE_ENABLE 1 +#define M_DELAYED_FREE_DISABLE 0 + +#define M_OHOS_CONFIG -1004 +#define M_DISABLE_OPT_TCACHE 100 +#define M_ENABLE_OPT_TCACHE 101 +#define M_TCACHE_PERFORMANCE_MODE 102 +#define M_TCACHE_NORMAL_MODE 103 + +int mallopt(int param, int value); +ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); #ifdef __cplusplus } #endif diff --git a/include/poll.h b/include/poll.h index 472e4b847..ba17ae911 100644 --- a/include/poll.h +++ b/include/poll.h @@ -50,6 +50,9 @@ __REDIR(ppoll, __ppoll_time64); #endif #endif +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif #ifdef __cplusplus } #endif diff --git a/include/pthread.h b/include/pthread.h index 89fd9ff7c..cc644be31 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -81,6 +81,7 @@ int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void int pthread_detach(pthread_t); _Noreturn void pthread_exit(void *); int pthread_join(pthread_t, void **); +pid_t pthread_gettid_np(pthread_t); #ifdef __GNUC__ __attribute__((const)) @@ -110,7 +111,156 @@ int pthread_mutex_trylock(pthread_mutex_t *); int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict); int pthread_mutex_destroy(pthread_mutex_t *); int pthread_mutex_consistent(pthread_mutex_t *); - +/** + * @brief lock the mutex object referenced by mutex. If the mutex is already locked, + * the calling thread shall block until the mutex becomes available as in the + * pthread_mutex_lock() function. If the mutex cannot be locked without waiting for + * another thread to unlock the mutex, this wait shall be terminated when the specified + * timeout expires. The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock. + * The resolution of the timeout shall be the resolution of the clock on which it is based. + * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock. + * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock. + * @param timespec the timeout shall expire specified by abstime passes. + * @return clocklock result. + * @retval 0 is returned on success. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ +int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict); +/** + * @brief lock the mutex object referenced by mutex. If the mutex is already locked, + * the calling thread shall block until the mutex becomes available as in the + * pthread_mutex_lock() function. If the mutex cannot be locked without waiting for + * another thread to unlock the mutex, this wait shall be terminated when the specified + * timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock. + * The resolution of the timeout shall be the resolution of the clock on which it is based. + * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock. + * @param timespec the timeout shall expire specified by abstime passes. + * @return clocklock result. + * @retval 0 is returned on success. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ +int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t *__restrict, const struct timespec *__restrict); +/** + * @brief lock the mutex object referenced by mutex. If the mutex is already locked, + * the calling thread shall block until the mutex becomes available as in the + * pthread_mutex_lock() function. If the mutex cannot be locked without waiting for + * another thread to unlock the mutex, this wait shall be terminated when the specified + * timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock. + * The resolution of the timeout shall be the resolution of the clock on which it is based. + * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock. + * @param ms the timeout shall expire specified by relative time(ms) passes. + * @return clocklock result. + * @retval 0 is returned on success. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ +int pthread_mutex_lock_timeout_np(pthread_mutex_t *__restrict, unsigned int); +/** + * @brief The thread waits for a signal to trigger, and if timeout or signal is triggered, + * the thread wakes up. + * @param pthread_cond_t Condition variables for multithreading. + * @param pthread_mutex_t Thread mutex variable. + * @param clockid_t Clock ID used in clock and timer functions. + * @param timespec The timeout shall expire specified by abstime passes. + * @return pthread_cond_clockwait result. + * @retval 0 pthread_cond_clockwait successful. + * @retval ETIMEDOUT pthread_cond_clockwait Connection timed out. + * @retval EINVAL pthread_cond_clockwait error. + */ +int pthread_cond_clockwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, + clockid_t, const struct timespec *__restrict); + +/** + * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC. + * The thread waits for a signal to trigger, and if timeout or signal is triggered, + * the thread wakes up. + * @param pthread_cond_t Condition variables for multithreading. + * @param pthread_mutex_t Thread mutex variable. + * @param timespec The timeout shall expire specified by abstime passes. + * @return pthread_cond_timedwait_monotonic_np result. + * @retval 0 pthread_cond_timedwait_monotonic_np successful. + * @retval ETIMEDOUT pthread_cond_timedwait_monotonic_np Connection timed out. + * @retval EINVAL pthread_cond_timedwait_monotonic_np error. + */ +int pthread_cond_timedwait_monotonic_np(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, + const struct timespec *__restrict); + +/** + * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC and The time + * parameter is in milliseconds. The thread waits for a signal to trigger, and if timeout or + * signal is triggered, the thread wakes up. + * @param pthread_cond_t Condition variables for multithreading. + * @param pthread_mutex_t Thread mutex variable. + * @param unsigned Timeout, in milliseconds. + * @return pthread_cond_timeout_np result. + * @retval 0 pthread_cond_timeout_np successful. + * @retval ETIMEDOUT pthread_cond_timeout_np Connection timed out. + * @retval EINVAL pthread_cond_timeout_np error. + */ +int pthread_cond_timeout_np(pthread_cond_t* __restrict, pthread_mutex_t* __restrict, unsigned int); +/** + * @brief Apply a read lock to the read-write lock referenced by rwlock as in the + * pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without + * waiting for other threads to unlock the lock, this wait shall be terminated when + * the specified timeout expires. The timeout shall expire when the absolute time specified by + * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time + * specified by abstime has already been passed at the time of the call. + * The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock. + * @param rw a read lock to the read-write lock referenced. + * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock. + * @param timespec the timeout shall expire specified by abstime passes. + * @return clockrdlock result. + * @retval 0 is returned on success. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ +int pthread_rwlock_clockrdlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict); +/** + * @brief Apply a read lock to the read-write lock referenced by rwlock as in the + * pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without + * waiting for other threads to unlock the lock, this wait shall be terminated when + * the specified timeout expires. The timeout shall expire when the absolute time specified by + * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time + * specified by abstime has already been passed at the time of the call. + * The timeout shall be based on the CLOCK_MONOTONIC clock. + * @param rw a read lock to the read-write lock referenced. + * @param timespec the timeout shall expire specified by abstime passes. + * @return clockrdlock result. + * @retval 0 is returned on success. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ +int pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict); +/** + * @brief Read-write lock variables have an initialization option to use CLOCK_MONOTONIC. + * apply a read lock to the read-write lock referenced by rwlock as in the + * pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without + * waiting for other threads to unlock the lock, this wait shall be terminated when + * the specified timeout expires. The timeout shall expire when the absolute time specified by + * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time + * specified by abstime has already been passed at the time of the call. + * The timeout shall be based on the CLOCK_MONOTONIC clock. + * @param rw a read lock to the read-write lock referenced. + * @param timespec the timeout shall expire specified by abstime passes. + * @return clockrdlock result. + * @retval 0 is returned on success. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ +int pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict); + +/** + * @brief Apply a read lock to the read-write lock referenced by rwlock as in the + * pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without + * waiting for other threads to unlock the lock, this wait shall be terminated when + * the specified timeout expires. The timeout shall expire when the absolute time specified by + * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time + * specified by abstime has already been passed at the time of the call. + * The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock. + * @param rw a read lock to the read-write lock referenced. + * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock. + * @param timespec the timeout shall expire specified by abstime passes. + * @return clockrdlock result. + * @retval 0 is returned on success. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ +int pthread_rwlock_clockwrlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict); int pthread_mutex_getprioceiling(const pthread_mutex_t *__restrict, int *__restrict); int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restrict); diff --git a/include/sched.h b/include/sched.h index fda4b4846..205ad736f 100644 --- a/include/sched.h +++ b/include/sched.h @@ -15,7 +15,18 @@ extern "C" { #endif #include +#include +#ifdef __LITEOS_A__ +struct sched_param { + union { + int sched_priority; + int sched_runtime; + }; + int sched_deadline; + int sched_period; +}; +#else struct sched_param { int sched_priority; int __reserved1; @@ -29,6 +40,7 @@ struct sched_param { #endif int __reserved3; }; +#endif int sched_get_priority_max(int); int sched_get_priority_min(int); @@ -85,6 +97,7 @@ void *calloc(size_t, size_t); void free(void *); typedef struct cpu_set_t { unsigned long __bits[128/sizeof(long)]; } cpu_set_t; +cpu_set_t* __sched_cpualloc(size_t __count); int __sched_cpucount(size_t, const cpu_set_t *); int sched_getcpu(void); int sched_getaffinity(pid_t, size_t, cpu_set_t *); diff --git a/include/signal.h b/include/signal.h index c347f8610..9c0bd5a0a 100644 --- a/include/signal.h +++ b/include/signal.h @@ -209,6 +209,17 @@ int __libc_current_sigrtmax(void); #define SIGRTMIN (__libc_current_sigrtmin()) #define SIGRTMAX (__libc_current_sigrtmax()) +#define MUSL_SIGNAL_NATIVE_REMOTE (SIGRTMIN + 0) +#define MUSL_SIGNAL_HOOK (SIGRTMIN + 1) +#define MUSL_SIGNAL_UNHOOK (SIGRTMIN + 2) +#define MUSL_SIGNAL_NATIVE_LOCAL (SIGRTMIN + 3) +#define MUSL_SIGNAL_JSHEAP (SIGRTMIN + 4) +#define MUSL_SIGNAL_JSHEAP_PRIV (SIGRTMIN + 5) +#define MUSL_SIGNAL_LEAK_STACK (SIGRTMIN + 7) +#ifdef USE_JEMALLOC_RECYCLE_FUNC +#define MUSL_SIGNAL_RECYCLE_JEMALLOC (SIGRTMIN + 8) +#endif +#define MUSL_SIGNAL_MEMCHECK (SIGRTMIN + 9) int kill(pid_t, int); diff --git a/include/stdio.h b/include/stdio.h index d1ed01f03..3017b296d 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -217,6 +217,11 @@ FILE *fopencookie(void *, const char *, cookie_io_functions_t); #define off64_t off_t #endif +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif + +#include #ifdef __cplusplus } #endif diff --git a/include/stdlib.h b/include/stdlib.h index b507ca33b..ffb3606a0 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -75,6 +75,10 @@ int wctomb (char *, wchar_t); size_t mbstowcs (wchar_t *__restrict, const char *__restrict, size_t); size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t); +unsigned int arc4random(void); +unsigned int arc4random_uniform(unsigned int); +void arc4random_buf(void *, size_t); + #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 @@ -172,6 +176,9 @@ long double strtold_l(const char *__restrict, char **__restrict, struct __locale #endif #endif +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif #ifdef __cplusplus } #endif diff --git a/include/string.h b/include/string.h index 43ad0942e..48eae75bd 100644 --- a/include/string.h +++ b/include/string.h @@ -100,6 +100,9 @@ char *basename(); #endif #endif +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif #ifdef __cplusplus } #endif diff --git a/porting/liteos_a/user/include/sys/capability.h b/include/sys/capability.h similarity index 100% rename from porting/liteos_a/user/include/sys/capability.h rename to include/sys/capability.h diff --git a/porting/linux/user/include/sys/capability.h b/include/sys/linux/capability.h similarity index 100% rename from porting/linux/user/include/sys/capability.h rename to include/sys/linux/capability.h diff --git a/porting/linux/user/include/sys/cdefs.h b/include/sys/linux/cdefs.h similarity index 100% rename from porting/linux/user/include/sys/cdefs.h rename to include/sys/linux/cdefs.h diff --git a/porting/linux/user/include/sys/sspret.h b/include/sys/linux/sspret.h similarity index 100% rename from porting/linux/user/include/sys/sspret.h rename to include/sys/linux/sspret.h diff --git a/include/sys/reboot.h b/include/sys/reboot.h index 9702eddba..c920bf16e 100644 --- a/include/sys/reboot.h +++ b/include/sys/reboot.h @@ -11,7 +11,24 @@ extern "C" { #define RB_POWER_OFF 0x4321fedc #define RB_SW_SUSPEND 0xd000fce2 #define RB_KEXEC 0x45584543 +#define RB_MAGIC1 0xfee1dead +#define RB_MAGIC2 672274793 +/** + * @brief reboots the device, or enables/disables the reboot keystroke. + * @param type commands accepted by the reboot() system call. + * -- RESTART Restart system using default command and mode. + * -- HALT Stop OS and give system control to ROM monitor, if any. + * -- CAD_ON Ctrl-Alt-Del sequence causes RESTART command. + * -- CAD_OFF Ctrl-Alt-Del sequence sends SIGINT to init task. + * -- POWER_OFF Stop OS and remove all power from system, if possible. + * -- RESTART2 Restart system using given command string. + * -- SW_SUSPEND Suspend system using software suspend if compiled in. + * -- KEXEC Restart system using a previously loaded Linux kernel. + * @return reboot result. + * @retval 0 is returned on success, if CAD was successfully enabled/disabled. + * @retval -1 is returned on failure, and errno is set to indicate the error. + */ int reboot(int); #ifdef __cplusplus diff --git a/include/sys/socket.h b/include/sys/socket.h index 6dc1e40ad..8b23b16a3 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h @@ -19,6 +19,10 @@ extern "C" { #include +#if (defined(__MUSL__)) && (defined(LINUX_MUSL_SUPPORTED)) +#include +#endif + struct msghdr { void *msg_name; socklen_t msg_namelen; @@ -371,11 +375,13 @@ struct sockaddr { char sa_data[14]; }; +#if (!defined(__MUSL__)) || (!defined(LINUX_MUSL_SUPPORTED)) struct sockaddr_storage { sa_family_t ss_family; char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)]; unsigned long __ss_align; }; +#endif int socket (int, int, int); int socketpair (int, int, int, int [2]); @@ -409,6 +415,9 @@ __REDIR(recvmmsg, __recvmmsg_time64); #endif #endif +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif #ifdef __cplusplus } #endif diff --git a/include/sys/stat.h b/include/sys/stat.h index 10d446c46..0aa29def5 100644 --- a/include/sys/stat.h +++ b/include/sys/stat.h @@ -119,6 +119,9 @@ __REDIR(futimens, __futimens_time64); __REDIR(utimensat, __utimensat_time64); #endif +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif #ifdef __cplusplus } #endif diff --git a/include/sys/syscall.h b/include/sys/syscall.h index 24987ddf2..8654e0c4b 100644 --- a/include/sys/syscall.h +++ b/include/sys/syscall.h @@ -3,4 +3,44 @@ #include +#if defined(SYS_getuid32) && defined(LINUX_MUSL_SUPPORTED) +#undef SYS_lchown +#undef SYS_getuid +#undef SYS_getgid +#undef SYS_geteuid +#undef SYS_getegid +#undef SYS_setreuid +#undef SYS_setregid +#undef SYS_getgroups +#undef SYS_setgroups +#undef SYS_fchown +#undef SYS_setresuid +#undef SYS_getresuid +#undef SYS_setresgid +#undef SYS_getresgid +#undef SYS_chown +#undef SYS_setuid +#undef SYS_setgid +#undef SYS_setfsuid +#undef SYS_setfsgid +#define SYS_lchown SYS_lchown32 +#define SYS_getuid SYS_getuid32 +#define SYS_getgid SYS_getgid32 +#define SYS_geteuid SYS_geteuid32 +#define SYS_getegid SYS_getegid32 +#define SYS_setreuid SYS_setreuid32 +#define SYS_setregid SYS_setregid32 +#define SYS_getgroups SYS_getgroups32 +#define SYS_setgroups SYS_setgroups32 +#define SYS_fchown SYS_fchown32 +#define SYS_setresuid SYS_setresuid32 +#define SYS_getresuid SYS_getresuid32 +#define SYS_setresgid SYS_setresgid32 +#define SYS_getresgid SYS_getresgid32 +#define SYS_chown SYS_chown32 +#define SYS_setuid SYS_setuid32 +#define SYS_setgid SYS_setgid32 +#define SYS_setfsuid SYS_setfsuid32 +#define SYS_setfsgid SYS_setfsgid32 +#endif #endif diff --git a/include/sys/sysinfo.h b/include/sys/sysinfo.h index 6a3931e52..b55bb2a97 100644 --- a/include/sys/sysinfo.h +++ b/include/sys/sysinfo.h @@ -5,6 +5,7 @@ extern "C" { #endif +#if !defined (__linux__) || !defined(STANDARD_SYSTEM) #define SI_LOAD_SHIFT 16 struct sysinfo { @@ -22,6 +23,9 @@ struct sysinfo { unsigned mem_unit; char __reserved[256]; }; +#else +#include +#endif int sysinfo (struct sysinfo *); int get_nprocs_conf (void); diff --git a/porting/linux/user/include/sys/tgkill.h b/include/sys/tgkill.h similarity index 95% rename from porting/linux/user/include/sys/tgkill.h rename to include/sys/tgkill.h index 0dd46ec8f..98fd458f6 100644 --- a/porting/linux/user/include/sys/tgkill.h +++ b/include/sys/tgkill.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -33,4 +33,4 @@ int tgkill(int tgid, int tid, int sig); #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/porting/linux/user/include/trace/trace_marker.h b/include/trace/linux/trace_marker.h similarity index 100% rename from porting/linux/user/include/trace/trace_marker.h rename to include/trace/linux/trace_marker.h diff --git a/include/unistd.h b/include/unistd.h index 212263a7e..7426694e2 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -14,9 +14,18 @@ extern "C" { #define SEEK_SET 0 #define SEEK_CUR 1 #define SEEK_END 2 +#ifndef __LITEOS_A__ #define SEEK_DATA 3 #define SEEK_HOLE 4 +#endif +#ifdef __LITEOS_A__ +#ifdef __cplusplus +#define NULL 0L +#else +#define NULL ((void*)0) +#endif +#else #if __cplusplus >= 201103L #define NULL nullptr #elif defined(__cplusplus) @@ -24,6 +33,7 @@ extern "C" { #else #define NULL ((void*)0) #endif +#endif #define __NEED_size_t #define __NEED_ssize_t @@ -77,6 +87,31 @@ int ftruncate(int, off_t); int access(const char *, int); int faccessat(int, const char *, int, int); +#ifdef __LITEOS_A__ +/* Format options */ +#define FMT_FAT 0x01 +#define FMT_FAT32 0x02 +#define FMT_ANY 0x07 +#define FMT_ERASE 0x08 + +/** + * @brief format FAT device (SD card, U disk, and MMC card), this function is OHOS-specific + * @param dev device name. + * @param sectors sectors per cluster, can be 0 OR power of 2. The sector size for standard FAT volumes is 512 bytes. + * -- sector number is 0 OR >128: automatically choose the appropriate cluster size. + * -- sector number is 1 ~ 128: cluster size = sectors per cluster * 512B. + * @param option file system type. + * -- FMT_FAT + * -- FMT_FAT32 + * -- FMT_ANY + * -- FMT_ERASE (USB not supported) + * @return format result + * @retval -1 format error + * @retval 0 format successful + */ +int format(const char *dev, int sectors, int option); +#endif + int chdir(const char *); int fchdir(int); char *getcwd(char *, size_t); @@ -86,7 +121,9 @@ unsigned sleep(unsigned); int pause(void); pid_t fork(void); +#ifndef __LITEOS_A__ pid_t _Fork(void); +#endif int execve(const char *, char *const [], char *const []); int execv(const char *, char *const []); int execle(const char *, const char *, ...); @@ -96,6 +133,27 @@ int execlp(const char *, const char *, ...); int fexecve(int, char *const [], char *const []); _Noreturn void _exit(int); +/** + * @brief Get the pid in the system namespace + * + * get the pid in the system namespace, used to globally mark processes in the pid sandbox.\n + * + * @param NA + * @return pid + * @since 4.1 + */ +pid_t getprocpid(void); + +/** + * @brief Get the tid in the system namespace + * + * get the tid in the system namespace, used to globally mark processes in the pid sandbox.\n + * + * @param NA + * @return pid + * @since 4.1 + */ +pid_t getproctid(void); pid_t getpid(void); pid_t getppid(void); pid_t getpgrp(void); @@ -195,8 +253,10 @@ int syncfs(int); int euidaccess(const char *, int); int eaccess(const char *, int); ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned); +#ifndef __LITEOS_A__ pid_t gettid(void); #endif +#endif #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) #define lseek64 lseek @@ -468,6 +528,22 @@ pid_t gettid(void); #define _CS_V6_ENV 1148 #define _CS_V7_ENV 1149 +#ifdef _GNU_SOURCE +#ifndef TEMP_FAILURE_RETRY +#define MUSL_TEMP_FAILURE_RETRY(expression) \ + (__extension__ \ + ({ long int __result; \ + do __result = (long int)(expression); \ + while(__result == -1L&& errno == EINTR); \ + __result;})) + +#define TEMP_FAILURE_RETRY(expression) MUSL_TEMP_FAILURE_RETRY(expression) +#endif +#endif + +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif #ifdef __cplusplus } #endif diff --git a/ldso/dynlink.c b/ldso/dynlink.c index 5b9c8be42..ea838826a 100644 --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -72,8 +72,10 @@ struct dso { size_t *lazy, lazy_cnt; unsigned char *map; size_t map_len; +#ifndef __LITEOS_A__ dev_t dev; ino_t ino; +#endif char relocated; char constructed; char kernel_mapped; @@ -773,6 +775,22 @@ static void *map_library(int fd, struct dso *dso) prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); +#ifdef __LITEOS_A__ + if ((ph->p_flags & PF_R) && (ph->p_flags & PF_X) && (!(ph->p_flags & PF_W))) { + Phdr *next_ph = ph; + for (int j = i - 1; j > 0; j--) { + next_ph = (void *)((char *)next_ph+eh->e_phentsize); + if (next_ph->p_type != PT_LOAD) { + continue; + } + size_t p_vaddr = (next_ph->p_vaddr & -(PAGE_SIZE)); + if (p_vaddr > this_max) { + mprotect(base + this_max, p_vaddr - this_max , PROT_READ); + } + break; + } + } +#endif /* Reuse the existing mapping for the lowest-address LOAD */ if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT) if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) @@ -991,6 +1009,9 @@ static void makefuncdescs(struct dso *p) static struct dso *load_library(const char *name, struct dso *needed_by) { char buf[2*NAME_MAX+2]; +#ifdef __LITEOS_A__ + char fullpath[2*NAME_MAX+2]; +#endif const char *pathname; unsigned char *map; struct dso *p, temp_dso = {0}; @@ -1095,18 +1116,38 @@ static struct dso *load_library(const char *name, struct dso *needed_by) sys_path = ""; } } +#ifdef __LITEOS_A__ + if (!sys_path || sys_path[0] == 0) { + sys_path = "/usr/lib:/lib:/usr/local/lib"; + } +#else if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib"; +#endif fd = path_open(name, sys_path, buf, sizeof buf); } pathname = buf; } if (fd < 0) return 0; +#ifdef __LITEOS_A__ + if (pathname[0] != '/') { + if (!realpath(pathname, fullpath)) { + close(fd); + return 0; + } + pathname = fullpath; + } +#else if (fstat(fd, &st) < 0) { close(fd); return 0; } +#endif for (p=head->next; p; p=p->next) { +#ifdef __LITEOS_A__ + if (!strcmp(p->name, pathname)) { +#else if (p->dev == st.st_dev && p->ino == st.st_ino) { +#endif /* If this library was previously loaded with a * pathname but a search found the same inode, * setup its shortname so it can be found by name. */ @@ -1154,8 +1195,10 @@ static struct dso *load_library(const char *name, struct dso *needed_by) return 0; } memcpy(p, &temp_dso, sizeof temp_dso); +#ifndef __LITEOS_A__ p->dev = st.st_dev; p->ino = st.st_ino; +#endif p->needed_by = needed_by; p->name = p->buf; p->runtime_loaded = runtime; @@ -1780,12 +1823,23 @@ void __dls3(size_t *sp, size_t *auxv) } if (DL_FDPIC) app.loadmap = app_loadmap; if (app.tls.size) app.tls.image = laddr(&app, tls_image); +#ifdef __LITEOS_A__ + if (interp_off) ldso.name = "/lib/libc.so"; +#else if (interp_off) ldso.name = laddr(&app, interp_off); +#endif +#ifdef __LITEOS_A__ + if (argv[0]) + app.name = argv[0]; + else + app.name = "none"; +#else if ((aux[0] & (1UL<p_vaddr + phdr->p_offset); } vdso.name = ""; +#ifdef __LITEOS_A__ + vdso.shortname = "OHOS-vdso.so"; +#else vdso.shortname = "linux-gate.so.1"; +#endif vdso.relocated = 1; vdso.deps = (struct dso **)no_deps; decode_dyn(&vdso); @@ -2031,7 +2089,17 @@ void *dlopen(const char *file, int mode) int cs; jmp_buf jb; struct dso **volatile ctor_queue = 0; +#ifdef __LITEOS_A__ + if (mode & ~(RTLD_LAZY | RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL | RTLD_LOCAL | RTLD_NODELETE)) { + error("invalid mode parameter for dlopen()."); + return NULL; + } + if ((mode & (RTLD_LAZY | RTLD_NOW)) == 0) { + error("invalid mode, one of RTLD_LAZY and RTLD_NOW must be set."); + return NULL; + } +#endif if (!file) return head; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); diff --git a/porting/linux/user/ldso/README.md b/ldso/linux/README.md similarity index 98% rename from porting/linux/user/ldso/README.md rename to ldso/linux/README.md index ea607086b..ac9cb724f 100644 --- a/porting/linux/user/ldso/README.md +++ b/ldso/linux/README.md @@ -1,18 +1,18 @@ -## Dynamic Linker Namespace - -The dynamic linker provides the namespace mechanism which is based on configures and apis. - -Shared libraries can be isolated in different namespaces so that libraries with same library name but with different symbols won't conflict. On the other hand, the namespace mechanism provides the flexibility so that some shared libraries can be exported by a linker namespace and used by another linker namespace. - - -### How does it work - -There are two ways to use the linker namespace: - -1. Modify and save the namespace configure file or base on architecture. When it does not exist or is filled in, a default namespace will be created. - -2. Use the apis of namespace in head file , to create a namespace, to set search paths of library, or to use exported libraries of another namespace by inheriting it. - -### Usage example - -Test usages show some examples of using the linker namespace. The file or in config directory is configures for test. The file gives usages of how apis and configures work. +## Dynamic Linker Namespace + +The dynamic linker provides the namespace mechanism which is based on configures and apis. + +Shared libraries can be isolated in different namespaces so that libraries with same library name but with different symbols won't conflict. On the other hand, the namespace mechanism provides the flexibility so that some shared libraries can be exported by a linker namespace and used by another linker namespace. + + +### How does it work + +There are two ways to use the linker namespace: + +1. Modify and save the namespace configure file or base on architecture. When it does not exist or is filled in, a default namespace will be created. + +2. Use the apis of namespace in head file , to create a namespace, to set search paths of library, or to use exported libraries of another namespace by inheriting it. + +### Usage example + +Test usages show some examples of using the linker namespace. The file or in config directory is configures for test. The file gives usages of how apis and configures work. diff --git a/porting/linux/user/ldso/cfi.c b/ldso/linux/cfi.c similarity index 100% rename from porting/linux/user/ldso/cfi.c rename to ldso/linux/cfi.c diff --git a/porting/linux/user/ldso/cfi.h b/ldso/linux/cfi.h similarity index 100% rename from porting/linux/user/ldso/cfi.h rename to ldso/linux/cfi.h diff --git a/porting/linux/user/ldso/dynlink.c b/ldso/linux/dynlink.c similarity index 100% rename from porting/linux/user/ldso/dynlink.c rename to ldso/linux/dynlink.c diff --git a/porting/linux/user/ldso/dynlink_rand.c b/ldso/linux/dynlink_rand.c similarity index 100% rename from porting/linux/user/ldso/dynlink_rand.c rename to ldso/linux/dynlink_rand.c diff --git a/porting/linux/user/ldso/dynlink_rand.h b/ldso/linux/dynlink_rand.h similarity index 100% rename from porting/linux/user/ldso/dynlink_rand.h rename to ldso/linux/dynlink_rand.h diff --git a/porting/linux/user/ldso/ld_log.c b/ldso/linux/ld_log.c similarity index 100% rename from porting/linux/user/ldso/ld_log.c rename to ldso/linux/ld_log.c diff --git a/porting/linux/user/ldso/ld_log.h b/ldso/linux/ld_log.h similarity index 100% rename from porting/linux/user/ldso/ld_log.h rename to ldso/linux/ld_log.h diff --git a/porting/linux/user/ldso/namespace.c b/ldso/linux/namespace.c similarity index 100% rename from porting/linux/user/ldso/namespace.c rename to ldso/linux/namespace.c diff --git a/porting/linux/user/ldso/namespace.h b/ldso/linux/namespace.h similarity index 100% rename from porting/linux/user/ldso/namespace.h rename to ldso/linux/namespace.h diff --git a/porting/linux/user/ldso/ns_config.c b/ldso/linux/ns_config.c similarity index 100% rename from porting/linux/user/ldso/ns_config.c rename to ldso/linux/ns_config.c diff --git a/porting/linux/user/ldso/ns_config.h b/ldso/linux/ns_config.h similarity index 100% rename from porting/linux/user/ldso/ns_config.h rename to ldso/linux/ns_config.h diff --git a/porting/linux/user/ldso/strops.c b/ldso/linux/strops.c similarity index 100% rename from porting/linux/user/ldso/strops.c rename to ldso/linux/strops.c diff --git a/porting/linux/user/ldso/strops.h b/ldso/linux/strops.h similarity index 100% rename from porting/linux/user/ldso/strops.h rename to ldso/linux/strops.h diff --git a/porting/linux/user/ldso/zip_archive.h b/ldso/linux/zip_archive.h similarity index 100% rename from porting/linux/user/ldso/zip_archive.h rename to ldso/linux/zip_archive.h diff --git a/libc-test/src/common/BUILD.gn b/libc-test/src/common/BUILD.gn index 53f8268cc..d2a5ebc2f 100644 --- a/libc-test/src/common/BUILD.gn +++ b/libc-test/src/common/BUILD.gn @@ -38,6 +38,8 @@ ohos_static_library("libtest") { "-D_FILE_OFFSET_BITS=64", ] + cflags = [ "-DLINUX_MUSL_SUPPORTED" ] + output_name = "libtest" } @@ -75,6 +77,8 @@ config("config_runtest") { "-D_FILE_OFFSET_BITS=64", ] + cflags = [ "-DLINUX_MUSL_SUPPORTED" ] + ldflags = [ "-nostdlib" ] libs = [ "//${target_out_dir}/libtest.a" ] @@ -109,6 +113,8 @@ config("config_unittest") { "-Wno-constant-conversion", ] + cflags = [ "-DLINUX_MUSL_SUPPORTED" ] + ldflags = [ "-nostdlib" ] libs = [ "//${target_out_dir}/libtest.a" ] diff --git a/libc-test/src/functionalext/fortify/string_ext.c b/libc-test/src/functionalext/fortify/string_ext.c index 6cebe91c8..4a04cab4e 100644 --- a/libc-test/src/functionalext/fortify/string_ext.c +++ b/libc-test/src/functionalext/fortify/string_ext.c @@ -23,7 +23,7 @@ #include "fortify_test.h" #include "functionalext.h" #include "test.h" -#include "../../../../porting/linux/user/include/fortify/fortify.h" +#include "../../../..//include/fortify/linux/fortify.h" #define SIZE_1 1 #define SIZE_5 5 diff --git a/libc-test/src/functionalext/unittest/BUILD.gn b/libc-test/src/functionalext/unittest/BUILD.gn index f2f444174..02ca243c8 100644 --- a/libc-test/src/functionalext/unittest/BUILD.gn +++ b/libc-test/src/functionalext/unittest/BUILD.gn @@ -22,11 +22,13 @@ ohos_executable("unittest_ldso_ns_config") { include_dirs = [ "${musl_src_base}/ldso", + "${musl_src_base}/ldso/linux", "//${test_dir}/src/common", "//${test_dir}/src/functionalext/common", "${musl_src_base}/src/include", "${musl_src_base}/include", "${musl_src_base}/src/internal", + "${musl_src_base}/src/internal/linux", ] configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] @@ -45,7 +47,7 @@ ohos_executable("unittest_ldso_namesapce") { part_name = "libc-test" sources = [ - "${musl_src_base}/ldso/namespace.c", + "${musl_src_base}/ldso/linux/namespace.c", "unit_test_ldso_namespace.c", "unit_test_mock_molloc.c", "unit_test_mock_strops.c", @@ -53,11 +55,13 @@ ohos_executable("unittest_ldso_namesapce") { include_dirs = [ "${musl_src_base}/ldso", + "${musl_src_base}/ldso/linux", "//${test_dir}/src/common", "//${test_dir}/src/functionalext/common", "${musl_src_base}/src/include", "${musl_src_base}/include", "${musl_src_base}/src/internal", + "${musl_src_base}/src/internal/linux", ] configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] @@ -74,18 +78,20 @@ ohos_executable("unittest_ldso_ld_log") { part_name = "libc-test" sources = [ - "${musl_src_base}/ldso/ld_log.c", + "${musl_src_base}/ldso/linux/ld_log.c", "unit_test_ldso_ld_log.c", "unit_test_mock_hilog_adapter.c", ] include_dirs = [ "${musl_src_base}/ldso", + "${musl_src_base}/ldso/linux", "//${test_dir}/src/common", "//${test_dir}/src/functionalext/common", "${musl_src_base}/src/include", "${musl_src_base}/include", "${musl_src_base}/src/internal", + "${musl_src_base}/src/internal/linux", "//${test_dir}/src/functionalext/unittest", ] @@ -109,11 +115,13 @@ ohos_executable("unittest_ldso_dynlink") { include_dirs = [ "${musl_src_base}/ldso", + "${musl_src_base}/ldso/linux", "//${test_dir}/src/common", "//${test_dir}/src/functionalext/common", "${musl_src_base}/src/include", "${musl_src_base}/include", "${musl_src_base}/src/internal", + "${musl_src_base}/src/internal/linux", "${musl_src_base}/arch/${musl_arch}", ] @@ -144,6 +152,7 @@ ohos_executable("unittest_hilog_adapter") { "//${test_dir}/src/functionalext/common", "//${test_dir}/src/common", "${musl_src_base}/src/internal", + "${musl_src_base}/src/internal/linux", ] configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] @@ -158,18 +167,20 @@ ohos_executable("unittest_ldso_strops") { part_name = "libc-test" sources = [ - "${musl_src_base}/ldso/strops.c", + "${musl_src_base}/ldso/linux/strops.c", "unit_test_ldso_strops.c", "unit_test_mock_molloc.c", ] include_dirs = [ "${musl_src_base}/ldso", + "${musl_src_base}/ldso/linux", "//${test_dir}/src/common", "//${test_dir}/src/functionalext/common", "${musl_src_base}/src/include", "${musl_src_base}/include", "${musl_src_base}/src/internal", + "${musl_src_base}/src/internal/linux", ] configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] @@ -186,7 +197,7 @@ ohos_executable("unittest_hilog_vsnprint") { include_dirs = [ "//${test_dir}/src/functionalext/common", "//${test_dir}/src/common", - "${musl_src_base}/src/hilog", + "${musl_src_base}/src/hilog/linux", ] configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] @@ -201,18 +212,20 @@ ohos_executable("unittest_fatal_message") { part_name = "libc-test" sources = [ - "${musl_src_base}/src/info/fatal_message.c", + "${musl_src_base}/src/info/linux/fatal_message.c", "unit_test_fatal_message.c", "unit_test_mock_fatal_message.c", ] include_dirs = [ "${musl_src_base}/src/info", + "${musl_src_base}/src/info/linux", "//${test_dir}/src/common", "//${test_dir}/src/functionalext/common", "${musl_src_base}/src/include", "${musl_src_base}/include", "${musl_src_base}/src/internal", + "${musl_src_base}/src/internal/linux", ] configs = [ "//third_party/musl/libc-test/src/common:config_unittest" ] diff --git a/libc-test/src/gwp_asan/BUILD.gn b/libc-test/src/gwp_asan/BUILD.gn index acf181c52..4cf8685a6 100644 --- a/libc-test/src/gwp_asan/BUILD.gn +++ b/libc-test/src/gwp_asan/BUILD.gn @@ -1,5 +1,6 @@ import("//build/test.gni") import("../../../musl_config.gni") +import("../../test_template.gni") if (use_gwp_asan) { config("gwp_asan_test_config") { @@ -10,6 +11,7 @@ if (use_gwp_asan) { ] include_dirs = [ "//third_party/musl/porting/linux/user/src/gwp_asan", + "${musl_src_base}/src/gwp_asan/linux", "//third_party/musl/libc-test/src/common", ] defines = [ "USE_GWP_ASAN" ] diff --git a/libc-test/test_template.gni b/libc-test/test_template.gni index 4e0642f23..267ca2e2f 100644 --- a/libc-test/test_template.gni +++ b/libc-test/test_template.gni @@ -74,6 +74,7 @@ template("test_unittest") { "-o", ] + cflags += [ "-DLINUX_MUSL_SUPPORTED" ] if (use_pthread_cancel) { cflags += [ "-DFEATURE_PTHREAD_CANCEL" ] } @@ -314,6 +315,7 @@ template("test_sharedlib") { "-o", ] + cflags += [ "-DLINUX_MUSL_SUPPORTED" ] cflags_c = cflags cflags_c += cflags_common diff --git a/musl_src.gni b/musl_src.gni index 2430f5a83..03c139f1d 100644 --- a/musl_src.gni +++ b/musl_src.gni @@ -8,7 +8,6 @@ if (musl_arch == "arm") { "src/fenv/arm/fenv.c", "src/ldso/arm/dlsym.s", "src/ldso/arm/dlsym_time64.S", - "src/ldso/arm/dlvsym.s", "src/ldso/arm/find_exidx.c", "src/ldso/arm/tlsdesc.S", "src/math/arm/fabs.c", @@ -31,7 +30,7 @@ if (musl_arch == "arm") { "src/thread/arm/atomics.s", "src/thread/arm/clone.s", "src/thread/arm/syscall_cp.s", - "src/linux/arm/flock.s", + "src/linux/arm/linux/flock.s", "compat/time32/adjtime32.c", "compat/time32/adjtimex_time32.c", "compat/time32/aio_suspend_time32.c", @@ -101,7 +100,6 @@ if (musl_arch == "arm") { musl_src_arch_file = [ "src/fenv/aarch64/fenv.s", "src/ldso/aarch64/dlsym.s", - "src/ldso/aarch64/dlvsym.s", "src/ldso/aarch64/tlsdesc.s", "src/math/aarch64/ceil.c", "src/math/aarch64/ceilf.c", @@ -140,10 +138,8 @@ if (musl_arch == "arm") { "src/thread/aarch64/__set_thread_area.s", "src/thread/aarch64/__unmapself.s", "src/thread/aarch64/clone.s", - "src/misc/aarch64/fstat.s", "src/thread/aarch64/syscall_cp.s", - "src/misc/aarch64/syscall.s", - "src/linux/aarch64/flock.s", + "src/linux/aarch64/linux/flock.s", ] } else if (musl_arch == "mips") { musl_src_arch_file = [ @@ -194,7 +190,6 @@ if (musl_arch == "arm") { musl_src_arch_file = [ "src/fenv/x86_64/fenv.s", "src/ldso/x86_64/dlsym.s", - "src/ldso/x86_64/dlvsym.s", "src/ldso/x86_64/tlsdesc.s", "src/math/x86_64/__invtrigl.s", "src/math/x86_64/acosl.s", @@ -239,22 +234,17 @@ if (musl_arch == "arm") { "src/thread/x86_64/__unmapself.s", "src/thread/x86_64/clone.s", "src/thread/x86_64/syscall_cp.s", - "src/linux/x86_64/flock.s", + "src/linux/x86_64/linux/flock.s", ] } musl_src_file = [ - "src/thread/pthread_cond_clockwait.c", - "src/thread/pthread_cond_timedwait_monotonic_np.c", - "src/thread/pthread_cond_timeout_np.c", "src/internal/pthread_impl.h", "src/internal/locale_impl.h", + "src/internal/stdio_impl.h", + "src/internal/unsupported_api.h", "src/internal/syscall_hooks.h", - "src/thread/pthread_rwlock_clockrdlock.c", "src/thread/pthread_rwlock_timedrdlock.c", - "src/thread/pthread_rwlock_timedrdlock_monotonic_np.c", - "src/thread/pthread_rwlock_clockwrlock.c", - "src/thread/pthread_rwlock_timedwrlock_monotonic_np.c", "src/thread/pthread_rwlock_timedwrlock.c", "src/aio/aio.c", "src/aio/aio_suspend.c", @@ -407,7 +397,7 @@ musl_src_file = [ "src/exit/abort.c", "src/exit/assert.c", "src/exit/at_quick_exit.c", - "src/exit/atexit.c", + "src/exit/linux/atexit.c", "src/exit/exit.c", "src/exit/quick_exit.c", "src/fcntl/creat.c", @@ -423,9 +413,9 @@ musl_src_file = [ "src/fenv/fesetexceptflag.c", "src/fenv/fesetround.c", "src/fenv/feupdateenv.c", - "src/fortify/fortify.c", - "src/gwp_asan/gwp_asan.c", - "src/gwp_asan/gwp_asan.h", + "src/fortify/linux/fortify.c", + "src/gwp_asan/linux/gwp_asan.c", + "src/gwp_asan/linux/gwp_asan.h", "src/internal/defsysinfo.c", "src/internal/floatscan.c", "src/internal/intscan.c", @@ -433,11 +423,9 @@ musl_src_file = [ "src/internal/procfdname.c", "src/internal/shgetc.c", "src/internal/syscall_ret.c", - "src/internal/vdso.c", "src/internal/version.c", - "src/internal/network_conf_function.c", - "src/hilog/hilog_adapter.c", - "src/hilog/vsnprintf_s_p.c", + "src/hilog/linux/hilog_adapter.c", + "src/hilog/linux/vsnprintf_s_p.c", "src/ipc/ftok.c", "src/ipc/msgctl.c", "src/ipc/msgget.c", @@ -485,7 +473,7 @@ musl_src_file = [ "src/linux/chroot.c", "src/linux/clock_adjtime.c", "src/linux/clone.c", - "src/linux/getprocpid.c", + "src/linux/linux/getprocpid.c", "src/linux/copy_file_range.c", "src/linux/epoll.c", "src/linux/eventfd.c", @@ -516,7 +504,6 @@ musl_src_file = [ "src/linux/quotactl.c", "src/linux/readahead.c", "src/linux/reboot.c", - "src/linux/tgkill.c", "src/linux/remap_file_pages.c", "src/linux/sbrk.c", "src/linux/sendfile.c", @@ -576,8 +563,6 @@ musl_src_file = [ "src/malloc/mallocng/malloc.c", "src/malloc/mallocng/meta.h", "src/malloc/mallocng/realloc.c", - "src/malloc/stats.c", - "src/malloc/calloc.c", "src/malloc/free.c", "src/malloc/libc_calloc.c", "src/malloc/lite_malloc.c", @@ -1112,7 +1097,6 @@ musl_src_file = [ "src/signal/sigtimedwait.c", "src/signal/sigwait.c", "src/signal/sigwaitinfo.c", - "src/sigchain/sigchain.c", "src/stat/__xstat.c", "src/stat/chmod.c", "src/stat/fchmod.c", @@ -1435,9 +1419,6 @@ musl_src_file = [ "src/thread/pthread_mutex_lock.c", "src/thread/pthread_mutex_setprioceiling.c", "src/thread/pthread_mutex_timedlock.c", - "src/thread/pthread_mutex_clocklock.c", - "src/thread/pthread_mutex_timedlock_monotonic_np.c", - "src/thread/pthread_mutex_lock_timeout_np.c", "src/thread/pthread_mutex_trylock.c", "src/thread/pthread_mutex_unlock.c", "src/thread/pthread_mutexattr_destroy.c", @@ -1612,10 +1593,10 @@ musl_src_file = [ "src/unistd/usleep.c", "src/unistd/write.c", "src/unistd/writev.c", - "src/trace/trace_marker.c", - "src/info/application_target_sdk_version.c", - "src/info/device_api_version.c", - "src/info/fatal_message.c", + "src/trace/linux/trace_marker.c", + "src/info/linux/application_target_sdk_version.c", + "src/info/linux/device_api_version.c", + "src/info/linux/fatal_message.c", ] if (musl_arch == "arm") { @@ -1780,13 +1761,13 @@ if (musl_arch == "arm") { musl_src_ldso = [ "ldso/dlstart.c", - "ldso/dynlink.c", - "ldso/namespace.c", - "ldso/ns_config.c", - "ldso/strops.c", - "ldso/dynlink_rand.c", - "ldso/cfi.c", - "ldso/ld_log.c", + "ldso/linux/dynlink.c", + "ldso/linux/namespace.c", + "ldso/linux/ns_config.c", + "ldso/linux/strops.c", + "ldso/linux/dynlink_rand.c", + "ldso/linux/cfi.c", + "ldso/linux/ld_log.c", ] if (musl_arch == "arm") { @@ -2017,7 +1998,7 @@ musl_inc_sys_files = [ "include/sys/acct.h", "include/sys/auxv.h", "include/sys/cachectl.h", - "include/sys/capability.h", + "include/sys/linux/capability.h", "include/sys/dir.h", "include/sys/epoll.h", "include/sys/errno.h", @@ -2057,7 +2038,7 @@ musl_inc_sys_files = [ "include/sys/signal.h", "include/sys/socket.h", "include/sys/soundcard.h", - "include/sys/sspret.h", + "include/sys/linux/sspret.h", "include/sys/statfs.h", "include/sys/stat.h", "include/sys/statvfs.h", @@ -2084,27 +2065,27 @@ musl_inc_sys_files = [ "include/sys/vt.h", "include/sys/wait.h", "include/sys/xattr.h", - "include/sys/cdefs.h", + "include/sys/linux/cdefs.h", ] -musl_inc_trace_files = [ "include/trace/trace_marker.h" ] +musl_inc_trace_files = [ "include/trace/linux/trace_marker.h" ] musl_inc_info_files = [ - "include/info/application_target_sdk_version.h", - "include/info/device_api_version.h", - "include/info/fatal_message.h", + "include/info/linux/application_target_sdk_version.h", + "include/info/linux/device_api_version.h", + "include/info/linux/fatal_message.h", ] musl_inc_fortify_files = [ - "include/fortify/fcntl.h", - "include/fortify/fortify.h", - "include/fortify/poll.h", - "include/fortify/socket.h", - "include/fortify/stat.h", - "include/fortify/stdlib.h", - "include/fortify/string.h", - "include/fortify/unistd.h", - "include/fortify/stdio.h", + "include/fortify/linux/fcntl.h", + "include/fortify/linux/fortify.h", + "include/fortify/linux/poll.h", + "include/fortify/linux/socket.h", + "include/fortify/linux/stat.h", + "include/fortify/linux/stdlib.h", + "include/fortify/linux/string.h", + "include/fortify/linux/unistd.h", + "include/fortify/linux/stdio.h", ] musl_inc_root_files = [ @@ -2119,7 +2100,7 @@ musl_inc_root_files = [ "include/ctype.h", "include/dirent.h", "include/dlfcn.h", - "include/dlfcn_ext.h", + "include/linux/dlfcn_ext.h", "include/elf.h", "include/endian.h", "include/err.h", @@ -2166,7 +2147,7 @@ musl_inc_root_files = [ "include/setjmp.h", "include/shadow.h", "include/signal.h", - "include/sigchain.h", + "include/linux/sigchain.h", "include/spawn.h", "include/stdalign.h", "include/stdarg.h", @@ -2204,233 +2185,68 @@ musl_inc_root_files = [ "include/syscall_hooks.h", ] -musl_src_porting_file = [ - "arch/aarch64/syscall_arch.h", - "arch/arm/bits/fenv.h", - "arch/generic/bits/shm.h", +musl_src_files_ext = [ "arch/generic/crtbrand.s", - "arch/x86_64/bits/float.h", - "include/ctype.h", - "include/info/application_target_sdk_version.h", - "include/info/device_api_version.h", - "include/info/fatal_message.h", - "include/malloc.h", - "include/pthread.h", - "include/fcntl.h", - "include/poll.h", - "include/stdio.h", - "include/stdlib.h", - "include/string.h", - "include/sys/stat.h", - "include/sys/sspret.h", - "include/fortify/fcntl.h", - "include/fortify/fortify.h", - "include/fortify/poll.h", - "include/fortify/socket.h", - "include/fortify/stat.h", - "include/fortify/stdlib.h", - "include/fortify/string.h", - "include/fortify/unistd.h", - "include/fortify/stdio.h", - "src/fortify/fortify.c", - "include/sys/capability.h", - "include/sys/cdefs.h", - "include/sys/reboot.h", - "include/sys/socket.h", - "include/sys/sysinfo.h", - "include/signal.h", - "include/sigchain.h", - "include/sched.h", - "src/internal/dynlink.h", - "include/sys/tgkill.h", - "include/trace/trace_marker.h", - "include/unistd.h", - "include/dlfcn.h", - "include/dlfcn_ext.h", - "src/fcntl/open.c", - "src/hilog/hilog_common.h", - "src/hilog/vsnprintf_s_p.h", - "src/internal/hilog_adapter.h", - "src/internal/musl_log.h", - "src/info/application_target_sdk_version.c", - "src/info/device_api_version.c", - "src/info/fatal_message.c", - "src/env/__init_tls.c", - "src/env/__libc_start_main.c", - "src/internal/pthread_impl.h", - "src/internal/locale_impl.h", - "src/internal/syscall.h", - "src/internal/services.h", - "src/internal/network_conf_function.h", - "src/internal/network_conf_function.c", - "src/internal/proc_xid_impl.h", - "ldso/namespace.h", - "ldso/ns_config.h", - "ldso/strops.h", - "src/legacy/ulimit.c", - "src/linux/clone.c", - "src/linux/getprocpid.c", - "src/linux/reboot.c", + "crt/linux/crtplus.c", + "ldso/linux/namespace.h", + "ldso/linux/ns_config.h", + "ldso/linux/strops.h", + "ldso/linux/cfi.h", + "ldso/linux/ld_log.h", + "ldso/linux/dynlink_rand.h", + "src/internal/linux/vdso.c", + "src/internal/linux/malloc_config.h", + "src/internal/linux/malloc_random.h", + "src/internal/linux/hilog_adapter.h", + "src/internal/linux/musl_log.h", + "src/internal/linux/network_conf_function.c", + "src/internal/linux/network_conf_function.h", + "src/internal/linux/services.h", + "src/internal/linux/proc_xid_impl.h", "src/linux/tgkill.c", - "src/malloc/mallocng/malloc_usable_size.c", - "src/malloc/mallocng/malloc.c", - "src/malloc/mallocng/meta.h", - "src/malloc/mallocng/donate.c", - "src/malloc/mallocng/aligned_alloc.c", - "src/malloc/lite_malloc.c", - "src/malloc/stats.c", - "src/malloc/calloc.c", - "src/malloc/free.c", - "src/network/getaddrinfo.c", - "src/network/getnameinfo.c", - "src/malloc/libc_calloc.c", - "src/malloc/realloc.c", - "src/gwp_asan/gwp_asan.c", - "src/gwp_asan/gwp_asan.h", - "src/network/inet_legacy.c", - "src/network/lookup.h", - "src/network/lookup_name.c", - "src/network/lookup_serv.c", - "src/network/res_cache.c", - "src/network/res_msend.c", - "src/network/resolvconf.c", - "src/network/socket.c", - "src/passwd/getspnam_r.c", - "src/sched/sched_setparam.c", - "src/sched/sched_getparam.c", - "src/sched/sched_setscheduler.c", - "src/sched/sched_getscheduler.c", - "src/thread/arm/clone.s", - "src/thread/arm/syscall_cp.s", + "src/malloc/linux/calloc.c", + "src/malloc/linux/stats.c", + "src/sigchain/linux/sigchain.c", + "src/thread/pthread_cond_clockwait.c", + "src/thread/pthread_cond_timedwait_monotonic_np.c", + "src/thread/pthread_cond_timeout_np.c", "src/thread/pthread_mutex_clocklock.c", - "src/thread/pthread_mutex_timedlock.c", - "src/thread/pthread_mutex_lock.c", "src/thread/pthread_mutex_timedlock_monotonic_np.c", "src/thread/pthread_mutex_lock_timeout_np.c", - "src/thread/pthread_mutex_init.c", "src/thread/pthread_rwlock_clockrdlock.c", - "src/thread/pthread_rwlock_timedrdlock.c", "src/thread/pthread_rwlock_timedrdlock_monotonic_np.c", - "src/thread/pthread_rwlock_init.c", - "src/thread/pthread_cond_timedwait.c", - "src/thread/pthread_cond_timedwait_monotonic_np.c", - "src/thread/pthread_cond_timeout_np.c", - "src/thread/pthread_cond_clockwait.c", - "src/thread/pthread_cond_init.c", - "src/thread/pthread_create.c", - "src/thread/pthread_kill.c", "src/thread/pthread_rwlock_clockwrlock.c", "src/thread/pthread_rwlock_timedwrlock_monotonic_np.c", - "src/thread/pthread_rwlock_timedwrlock.c", - "src/thread/pthread_sigmask.c", - "src/trace/trace_marker.c", - "src/ldso/dlclose.c", - "src/ldso/dlerror.c", - "ldso/dynlink.c", - "src/exit/atexit.c", - "crt/arm/crti.s", - "crt/aarch64/crti.s", - "crt/crtplus.c", - "ldso/cfi.h", - "ldso/ld_log.h", - "ldso/ld_log.c", - "ldso/namespace.c", - "ldso/ns_config.c", - "ldso/strops.c", - "ldso/cfi.c", - "ldso/dynlink_rand.c", - "ldso/dynlink_rand.h", - "src/sched/sched_cpualloc.c", - "src/signal/signal.c", - "src/signal/sigaction.c", - "src/signal/siginterrupt.c", - "include/langinfo.h", - "include/locale.h", - "src/hilog/hilog_adapter.c", - "src/hilog/vsnprintf_s_p.c", - "src/internal/libc.h", - "src/locale/dcngettext.c", - "src/locale/locale_map.c", - "src/locale/langinfo.c", - "src/locale/newlocale.c", - "src/time/__tz.c", - "src/time/gmtime_r.c", - "src/time/strftime.c", - "src/time/strptime.c", - "src/time/time_impl.h", - "src/ldso/aarch64/dlvsym.s", - "src/ldso/arm/dlvsym.s", - "src/ldso/riscv64/dlvsym.s", - "src/ldso/x86_64/dlvsym.s", - "src/stdio/__fdopen.c", - "src/stdio/vfprintf.c", - "src/stdio/__stdio_read.c", - "src/stdio/__stdio_write.c", - "src/stdio/__stdout_write.c", - "src/stdio/fread.c", - "src/stdio/fmemopen.c", - "src/stdio/freopen.c", - "src/stdio/stdin.c", - "src/stdio/__fmodeflags.c", - "src/stdio/fopen.c", - "src/stdio/ofl.c", - "src/stdio/fclose.c", - "src/stdio/__toread.c", - "src/stdio/__towrite.c", - "src/stdio/stderr.c", - "src/stdio/fgets.c", - "src/stdio/vsnprintf.c", - "src/stdio/vsscanf.c", - "src/internal/stdio_impl.h", - "src/internal/vdso.c", - "src/time/clock_gettime.c", - "src/time/clock_getres.c", - "src/time/gettimeofday.c", - "src/time/time.c", - "src/unistd/getpid.c", - "src/process/_Fork.c", - "src/process/vfork.c", - "src/process/arm/__vfork.s", - "src/process/x86_64/__vfork.s", - "src/linux/cache.c", - "src/sched/sched_getcpu.c", - "src/sigchain/sigchain.c", - "src/conf/legacy.c", - "src/conf/sysconf.c", - "src/env/getenv.c", - "src/string/strcasecmp.c", - "src/string/strncasecmp.c", - "src/string/strncat.c", - "src/string/stpncpy.c", - "src/stat/fstat.c", - "src/string/strcspn.c", - "src/thread/pthread_once.c", - "src/thread/pthread_cancel.c", - "src/mq/mq_notify.c", - "src/aio/aio.c", - "src/misc/aarch64/fstat.s", - "src/misc/aarch64/syscall.s", - "src/stdlib/strtol.c", - "src/stdlib/strtod.c", - "src/thread/sem_timedwait.c", - "src/stdio/vfscanf.c", - "src/stdio/fileno.c", - "src/linux/arm/flock.s", - "src/linux/aarch64/flock.s", - "src/linux/x86_64/flock.s", - "src/linux/flock.c", - "src/ctype/towctrans.c", - "src/stdio/ofl_add.c", - "src/linux/settimeofday.c", ] +if (musl_arch == "arm") { + musl_src_files_ext += [ + "src/process/arm/__vfork.s", + "src/ldso/arm/linux/dlvsym.s", + ] +} else if (musl_arch == "aarch64") { + musl_src_files_ext += [ + "arch/aarch64/syscall_arch.h", + "src/misc/aarch64/linux/fstat.s", + "src/misc/aarch64/linux/syscall.s", + "src/ldso/aarch64/linux/dlvsym.s", + ] +} else if (musl_arch == "x86_64") { + musl_src_files_ext += [ + "src/process/x86_64/__vfork.s", + "src/ldso/x86_64/linux/dlvsym.s", + "arch/x86_64/bits/float.h", + ] +} + musl_inc_hook_files = [ - "porting/linux/user/src/internal/hilog_adapter.h", - "porting/linux/user/src/internal/musl_log.h", - "porting/linux/user/src/hook/memory_trace.h", - "porting/linux/user/src/hook/musl_malloc_dispatch_table.h", - "porting/linux/user/src/hook/musl_malloc_dispatch.h", - "porting/linux/user/src/hook/musl_preinit_common.h", + "src/internal/linux/hilog_adapter.h", + "src/internal/linux/musl_log.h", + "src/hook/linux/memory_trace.h", + "src/hook/linux/musl_malloc_dispatch_table.h", + "src/hook/linux/musl_malloc_dispatch.h", + "src/hook/linux/musl_preinit_common.h", + "src/hook/linux/musl_socket_dispatch.h", ] added_freebsd_files = [ diff --git a/musl_template.gni b/musl_template.gni index f4148e23a..976f068c9 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -116,9 +116,11 @@ template("musl_libs") { "${target_out_dir}/${musl_ported_dir}/arch/${musl_arch}", "${target_out_dir}/${musl_ported_dir}/arch/generic", "${target_out_dir}/${musl_ported_dir}/src/internal", + "${target_out_dir}/${musl_ported_dir}/src/internal/linux", "${target_out_dir}/${musl_ported_dir}/src/include", "${target_out_dir}/${musl_ported_dir}/src/gwp_asan", "${target_out_dir}/${musl_ported_dir}/src/hook", + "${target_out_dir}/${musl_ported_dir}/src/hook/linux", "${target_out_dir}/${musl_ported_dir}/include", "${target_out_dir}/${musl_inc_out_dir}", ] @@ -182,6 +184,7 @@ template("musl_libs") { cflags_auto += [ "-DRESERVE_SIGNAL_STACK" ] cflags_auto += [ "-DDFX_SIGNAL_LIBC" ] + cflags_auto += [ "-DLINUX_MUSL_SUPPORTED" ] cflags_c99fse = [ "-std=c99", @@ -262,6 +265,7 @@ template("musl_libs") { "__MUSL__", "_LIBCPP_HAS_MUSL_LIBC", "__BUILD_LINUX_WITH_CLANG", + "LINUX_MUSL_SUPPORTED", ] if (!is_asan && musl_arch != "mips") { @@ -369,7 +373,7 @@ template("musl_libs") { sources = [ "${target_out_dir}/${musl_ported_dir}/crt/Scrt1.c", "${target_out_dir}/${musl_ported_dir}/crt/crt1.c", - "${target_out_dir}/${musl_ported_dir}/crt/crtplus.c", + "${target_out_dir}/${musl_ported_dir}/crt/linux/crtplus.c", "${target_out_dir}/${musl_ported_dir}/crt/rcrt1.c", ] if (musl_arch == "riscv64") { @@ -553,6 +557,10 @@ template("musl_libs") { } } + foreach(s, musl_src_files_ext) { + sources_orig += [ "${s}" ] + } + defines = [] if (musl_arch == "arm") { defines += [ "MUSL_ARM_ARCH" ] @@ -934,13 +942,13 @@ template("musl_libs") { source_set("soft_musl_hook") { sources = [ - "./porting/linux/user/src/hook/malloc_common.c", - "./porting/linux/user/src/hook/memory_trace.c", - "./porting/linux/user/src/hook/musl_preinit.c", - "./porting/linux/user/src/hook/musl_preinit_common.c", - "./porting/linux/user/src/hook/musl_socket_preinit.c", - "./porting/linux/user/src/hook/musl_socket_preinit_common.c", - "./porting/linux/user/src/hook/socket_common.c", + "./src/hook/linux/malloc_common.c", + "./src/hook/linux/memory_trace.c", + "./src/hook/linux/musl_preinit.c", + "./src/hook/linux/musl_preinit_common.c", + "./src/hook/linux/musl_socket_preinit.c", + "./src/hook/linux/musl_socket_preinit_common.c", + "./src/hook/linux/socket_common.c", ] deps = [ @@ -1249,7 +1257,7 @@ template("musl_libs") { args += [ "--output" ] + rebase_path(outputs, root_build_dir) args += [ "--ldpath" ] + rebase_path(ldpath, root_build_dir) args += [ "--crtplus" ] + rebase_path( - [ "${redir}/${target_out_dir}/${musl_ported_dir}/crt/soft_musl_crt/crtplus.o" ], + [ "${redir}/${target_out_dir}/${musl_ported_dir}/crt/linux/soft_musl_crt/crtplus.o" ], root_build_dir) deps = [ ":soft_musl_crt" ] diff --git a/porting/linux/user/arch/aarch64/syscall_arch.h b/porting/linux/user/arch/aarch64/syscall_arch.h deleted file mode 100644 index 2e4047380..000000000 --- a/porting/linux/user/arch/aarch64/syscall_arch.h +++ /dev/null @@ -1,105 +0,0 @@ -#include "syscall_hooks.h" - -#define __SYSCALL_LL_E(x) (x) -#define __SYSCALL_LL_O(x) (x) - -#define __asm_syscall(...) do { \ - __asm__ __volatile__ ( "svc 0" \ - : "=r"(x0) : __VA_ARGS__ : "memory", "cc"); \ - return x0; \ - } while (0) - -static inline long __syscall0(long n) -{ - if (is_syscall_hooked(n)) { - return __syscall_hooks_entry0(n); - } - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0"); - __asm_syscall("r"(x8)); -} - -static inline long __syscall1(long n, long a) -{ - if (is_syscall_hooked(n)) { - return __syscall_hooks_entry1(n, a); - } - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - __asm_syscall("r"(x8), "0"(x0)); -} - -static inline long __syscall2(long n, long a, long b) -{ - if (is_syscall_hooked(n)) { - return __syscall_hooks_entry2(n, a, b); - } - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - __asm_syscall("r"(x8), "0"(x0), "r"(x1)); -} - -static inline long __syscall3(long n, long a, long b, long c) -{ - if (is_syscall_hooked(n)) { - return __syscall_hooks_entry3(n, a, b, c); - } - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - register long x2 __asm__("x2") = c; - __asm_syscall("r"(x8), "0"(x0), "r"(x1), "r"(x2)); -} - -static inline long __syscall4(long n, long a, long b, long c, long d) -{ - if (is_syscall_hooked(n)) { - return __syscall_hooks_entry4(n, a, b, c, d); - } - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - register long x2 __asm__("x2") = c; - register long x3 __asm__("x3") = d; - __asm_syscall("r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3)); -} - -static inline long __syscall5(long n, long a, long b, long c, long d, long e) -{ - if (is_syscall_hooked(n)) { - return __syscall_hooks_entry5(n, a, b, c, d, e); - } - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - register long x2 __asm__("x2") = c; - register long x3 __asm__("x3") = d; - register long x4 __asm__("x4") = e; - __asm_syscall("r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4)); -} - -static inline long __syscall6(long n, long a, long b, long c, long d, long e, long f) -{ - if (is_syscall_hooked(n)) { - return __syscall_hooks_entry6(n, a, b, c, d, e, f); - } - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - register long x2 __asm__("x2") = c; - register long x3 __asm__("x3") = d; - register long x4 __asm__("x4") = e; - register long x5 __asm__("x5") = f; - __asm_syscall("r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)); -} - -#define VDSO_USEFUL -#define VDSO_CGT_SYM "__kernel_clock_gettime" -#define VDSO_CGT_VER "LINUX_2.6.39" -#define VDSO_CGR_SYM "__kernel_clock_getres" -#define VDSO_CGR_VER "LINUX_2.6.39" -#define VDSO_GTD_SYM "__kernel_gettimeofday" -#define VDSO_GTD_VER "LINUX_2.6.39" - -#define IPC_64 0 diff --git a/porting/linux/user/arch/arm/bits/fenv.h b/porting/linux/user/arch/arm/bits/fenv.h deleted file mode 100644 index b497a9878..000000000 --- a/porting/linux/user/arch/arm/bits/fenv.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __ARM_PCS_VFP -#define FE_ALL_EXCEPT 0 -#define FE_TONEAREST 0 -#else -#define FE_INVALID 1 -#define FE_DIVBYZERO 2 -#define FE_OVERFLOW 4 -#define FE_UNDERFLOW 8 -#define FE_INEXACT 16 -#define FE_ALL_EXCEPT 31 -#define FE_TONEAREST 0 -#define FE_TOWARDZERO 0xc00000 -#endif -#define FE_DOWNWARD 0x800000 -#define FE_UPWARD 0x400000 - -typedef unsigned long fexcept_t; - -typedef struct { - unsigned long __cw; -} fenv_t; - -#define FE_DFL_ENV ((const fenv_t *) -1) diff --git a/porting/linux/user/arch/generic/bits/shm.h b/porting/linux/user/arch/generic/bits/shm.h deleted file mode 100644 index da07a2a0c..000000000 --- a/porting/linux/user/arch/generic/bits/shm.h +++ /dev/null @@ -1,24 +0,0 @@ -#define SHMLBA (4*4096) - -struct shmid_ds { - struct ipc_perm shm_perm; - size_t shm_segsz; - time_t shm_atime; - time_t shm_dtime; - time_t shm_ctime; - pid_t shm_cpid; - pid_t shm_lpid; - unsigned long shm_nattch; - unsigned long __pad1; - unsigned long __pad2; -}; - -struct shminfo { - unsigned long shmmax, shmmin, shmmni, shmseg, shmall, __unused[4]; -}; - -struct shm_info { - int __used_ids; - unsigned long shm_tot, shm_rss, shm_swp; - unsigned long __swap_attempts, __swap_successes; -}; diff --git a/porting/linux/user/arch/x86_64/bits/float.h b/porting/linux/user/arch/x86_64/bits/float.h deleted file mode 100644 index e188cb619..000000000 --- a/porting/linux/user/arch/x86_64/bits/float.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifdef __FLT_EVAL_METHOD__ -#define FLT_EVAL_METHOD __FLT_EVAL_METHOD__ -#else -#define FLT_EVAL_METHOD 0 -#endif - -#define LDBL_TRUE_MIN 6.47517511943802511092443895822764655e-4966L -#define LDBL_MIN 3.36210314311209350626267781732175260e-4932L -#define LDBL_MAX 1.18973149535723176508575932662800702e+4932L -#define LDBL_EPSILON 1.92592994438723585305597794258492732e-34L - -#define LDBL_MANT_DIG 113 -#define LDBL_MIN_EXP (-16381) -#define LDBL_MAX_EXP 16384 - -#define LDBL_DIG 33 -#define LDBL_MIN_10_EXP (-4931) -#define LDBL_MAX_10_EXP 4932 - -#define DECIMAL_DIG 36 diff --git a/porting/linux/user/crt/aarch64/crti.s b/porting/linux/user/crt/aarch64/crti.s deleted file mode 100644 index d82bcb319..000000000 --- a/porting/linux/user/crt/aarch64/crti.s +++ /dev/null @@ -1,17 +0,0 @@ -.include "crtbrand.s" - -.section .init -.global _init -.type _init,%function -.balign 4 -_init: - stp x29,x30,[sp,-16]! - mov x29,sp - -.section .fini -.global _fini -.type _fini,%function -.balign 4 -_fini: - stp x29,x30,[sp,-16]! - mov x29,sp diff --git a/porting/linux/user/crt/arm/crti.s b/porting/linux/user/crt/arm/crti.s deleted file mode 100644 index aae0ddcaa..000000000 --- a/porting/linux/user/crt/arm/crti.s +++ /dev/null @@ -1,17 +0,0 @@ -.include "crtbrand.s" - -.syntax unified - -.section .init -.global _init -.type _init,%function -.balign 4 -_init: - push {r0,lr} - -.section .fini -.global _fini -.type _fini,%function -.balign 4 -_fini: - push {r0,lr} diff --git a/porting/linux/user/crt/x86_64/crti.s b/porting/linux/user/crt/x86_64/crti.s deleted file mode 100644 index 104d91839..000000000 --- a/porting/linux/user/crt/x86_64/crti.s +++ /dev/null @@ -1,11 +0,0 @@ -.include "crtbrand.s" - -.section .init -.global _init -_init: - push %rax - -.section .fini -.global _fini -_fini: - push %rax diff --git a/porting/linux/user/include/ctype.h b/porting/linux/user/include/ctype.h deleted file mode 100644 index fe72b3ff6..000000000 --- a/porting/linux/user/include/ctype.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef _CTYPE_H -#define _CTYPE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -int isalnum(int); -int isalpha(int); -int isblank(int); -int iscntrl(int); -int isdigit(int); -int isgraph(int); -int islower(int); -int isprint(int); -int ispunct(int); -int isspace(int); -int isupper(int); -int isxdigit(int); -int tolower(int); -int toupper(int); - -#ifndef __cplusplus -static __inline int __isspace(int _c) -{ - return _c == ' ' || (unsigned)_c-'\t' < 5; -} - -#define isalpha(a) (0 ? isalpha(a) : (((unsigned)(a)|32)-'a') < 26) -#define isdigit(a) (0 ? isdigit(a) : ((unsigned)(a)-'0') < 10) -#define islower(a) (0 ? islower(a) : ((unsigned)(a)-'a') < 26) -#define isupper(a) (0 ? isupper(a) : ((unsigned)(a)-'A') < 26) -#define isprint(a) (0 ? isprint(a) : ((unsigned)(a)-0x20) < 0x5f) -#define isgraph(a) (0 ? isgraph(a) : ((unsigned)(a)-0x21) < 0x5e) -#define isspace(a) __isspace(a) -#endif - - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) - -#define __NEED_locale_t -#include - -int isalnum_l(int, locale_t); -int isalpha_l(int, locale_t); -int isblank_l(int, locale_t); -int iscntrl_l(int, locale_t); -int isdigit_l(int, locale_t); -int isgraph_l(int, locale_t); -int islower_l(int, locale_t); -int isprint_l(int, locale_t); -int ispunct_l(int, locale_t); -int isspace_l(int, locale_t); -int isupper_l(int, locale_t); -int isxdigit_l(int, locale_t); -int tolower_l(int, locale_t); -int toupper_l(int, locale_t); - -int isascii(int); -int toascii(int); -#define _tolower(a) ((a)|0x20) -#define _toupper(a) ((a)&0x5f) -#ifndef __cplusplus -#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128) -#endif - -#include - -const int32_t **__ctype_tolower_loc(void); -const int32_t **__ctype_toupper_loc(void); - -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/dlfcn.h b/porting/linux/user/include/dlfcn.h deleted file mode 100644 index 07f810dbf..000000000 --- a/porting/linux/user/include/dlfcn.h +++ /dev/null @@ -1,185 +0,0 @@ -#ifndef _DLFCN_H -#define _DLFCN_H - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define RTLD_LAZY 1 -#define RTLD_NOW 2 -#define RTLD_NOLOAD 4 -#define RTLD_NODELETE 4096 -#define RTLD_GLOBAL 256 -#define RTLD_LOCAL 0 - -#define RTLD_NEXT ((void *)-1) -#define RTLD_DEFAULT ((void *)0) - -#define RTLD_DI_LINKMAP 2 - - -/* create flags for dlns_create */ -#define CREATE_INHERIT_DEFAULT 0x1 -#define CREATE_INHERIT_CURRENT 0x2 -#define LOCAL_NS_PREFERED 0x4 /* Use app's library when app's library has same name as system library. */ - -int dlclose(void *); -char *dlerror(void); -void *dlopen(const char *, int); -void *dlsym(void *__restrict, const char *__restrict); -void *dlvsym(void *__restrict, const char *__restrict, const char *__restrict); - -/* namespace apis */ -#define NS_NAME_MAX 255 -typedef struct { - char name[NS_NAME_MAX+1]; -} Dl_namespace; - -/** - * @brief Initialize a namespace structure for operating namespaces through related functional interfaces. - * @param Dl_namespace * namespace handle. - * @param char * namespace name. - * @return void. - * @retval none. - */ -void dlns_init(Dl_namespace *, const char *); - -/** - * @brief Gets the current namespace handle, or verifies that the given name namespace exists. - * @param char * Namespace name.Gets the current caller namespace handle when name is null. - * @param Dl_namespace * namespace handle. - * @return return 0 on success,fail other values. - * @retval - * EINVAL(22) Invalid argument. - * ENOKEY(126) Required key not available. - */ -int dlns_get(const char *, Dl_namespace *); - -/** - * @brief open dso in given namespace which has own lib search paths, when namespace is null, it's same to dlopen(). - * avoid using "default" as namespace, which is the default namespace. - * @param Dl_namespace * namespace handle. - * @param char * the name of the so file you want to open. - * @param int open file mode. - * -- RTLD_LAZY. - * -- RTLD_NOW. - * -- RTLD_NOLOAD. - * -- RTLD_NODELETE. - * -- RTLD_GLOBAL. - * -- RTLD_LOCAL. - * @return success: dynamic library handleoid,failed: NULL. - * @retval none. - */ -void *dlopen_ns(Dl_namespace *, const char *, int); - -/** - * @brief create the namespace and set lib search paths of namespace, - * the paths should be splited by ':'. When namespace already exist,return error. - * avoid using "default" as namespace, which is the default namespace. - * @param Dl_namespace * namespace handle. - * @param char * lib path library that can be specified. - * @return return 0 on success,fail other values. - * @retval - * EINVAL(22) Invalid argument. - * EEXIST(17) File exists. - * ENOMEM(12) Out of memory. - */ -int dlns_create(Dl_namespace *, const char *); - -/** - * @brief create the namespace and set lib search paths of namespace, - * like dlns_create, except can use flags to set parent ns. - * @param Dl_namespace * namespace handle. - * @param char * lib path library that can be specified. - * #param int flags for create namespace, CREATE_INHERIT_CURRENT or CREATE_INHERIT_DEFAULT. - * @return return 0 on success,fail other values. - * @retval - * EINVAL(22) Invalid argument. - * EEXIST(17) File exists. - * ENOMEM(12) Out of memory. - */ -int dlns_create2(Dl_namespace *, const char *, int); - -/** - * @brief make one namespace inherit another, and so it can use shared libs by the inherited one. - * param1: namespace, param2: inherited namespace, param3: shared libs. - * the shared libs should be splited by ':'. when it is null or empty, all libs can be shared. - * one namespace can inherit or be inherited by multiple ones. - * When namespaces do not exist, return error. - * @param Dl_namespace * The first parameter is the namespace to inherit from. - * @param Dl_namespace * The second parameter is the inherited namespace. - * @param char * some library names to inherit. - * @return return 0 on success,fail other values. - * @retval - * EINVAL(22) Invalid argument. - * ENOKEY(126) Required key not available. - */ -int dlns_inherit(Dl_namespace *, Dl_namespace *, const char *); - -/** - * @brief Set namespace lib_path. - * @param name namespace name. - * @param lib_path The lib path name that needs to be reset, it can be multiple, link with ":". - * @return Returns 0 on success, other on failure. - * @retval - * EINVAL(22) Invalid argument. - * ENOKEY(126) Required key not available. - */ -int dlns_set_namespace_lib_path(const char *name, const char *lib_path); - -/** - * @brief Set namespace separated. - * @param name namespace name. - * @param separated separated. - * @return Returns 0 on success, other on failure. - * @retval - * EINVAL(22) Invalid argument. - * ENOKEY(126) Required key not available. - */ -int dlns_set_namespace_separated(const char *name, const bool separated); - -/** - * @brief Set namespace permitted_paths. - * @param name namespace name. - * @param permitted_paths set new permitted_paths. - * @return Returns 0 on success, other on failure. - * @retval - * EINVAL(22) Invalid argument. - * ENOKEY(126) Required key not available. - */ -int dlns_set_namespace_permitted_paths(const char *name, const char *permitted_paths); - -/** - * @brief Set namespace allowed_libs. - * @param name namespace name. - * @param allowed_libs set new allowed_libs. - * @return Returns 0 on success, other on failure. - * @retval - * EINVAL(22) Invalid argument. - * ENOKEY(126) Required key not available. - */ -int dlns_set_namespace_allowed_libs(const char *name, const char *allowed_libs); - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -typedef struct { - const char *dli_fname; - void *dli_fbase; - const char *dli_sname; - void *dli_saddr; -} Dl_info; -int dladdr(const void *, Dl_info *); -int dlinfo(void *, int, void *); -#endif - -#if _REDIR_TIME64 -__REDIR(dlsym, __dlsym_time64); -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/elf.h b/porting/linux/user/include/elf.h deleted file mode 100644 index d3020899e..000000000 --- a/porting/linux/user/include/elf.h +++ /dev/null @@ -1,3300 +0,0 @@ -#ifndef _ELF_H -#define _ELF_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -typedef uint16_t Elf32_Half; -typedef uint16_t Elf64_Half; - -typedef uint32_t Elf32_Word; -typedef int32_t Elf32_Sword; -typedef uint32_t Elf64_Word; -typedef int32_t Elf64_Sword; - -typedef uint64_t Elf32_Xword; -typedef int64_t Elf32_Sxword; -typedef uint64_t Elf64_Xword; -typedef int64_t Elf64_Sxword; - -typedef uint32_t Elf32_Addr; -typedef uint64_t Elf64_Addr; - -typedef uint32_t Elf32_Off; -typedef uint64_t Elf64_Off; - -typedef uint16_t Elf32_Section; -typedef uint16_t Elf64_Section; - -typedef Elf32_Half Elf32_Versym; -typedef Elf64_Half Elf64_Versym; - -#define EI_NIDENT (16) - -typedef struct { - unsigned char e_ident[EI_NIDENT]; - Elf32_Half e_type; - Elf32_Half e_machine; - Elf32_Word e_version; - Elf32_Addr e_entry; - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf32_Word e_flags; - Elf32_Half e_ehsize; - Elf32_Half e_phentsize; - Elf32_Half e_phnum; - Elf32_Half e_shentsize; - Elf32_Half e_shnum; - Elf32_Half e_shstrndx; -} Elf32_Ehdr; - -typedef struct { - unsigned char e_ident[EI_NIDENT]; - Elf64_Half e_type; - Elf64_Half e_machine; - Elf64_Word e_version; - Elf64_Addr e_entry; - Elf64_Off e_phoff; - Elf64_Off e_shoff; - Elf64_Word e_flags; - Elf64_Half e_ehsize; - Elf64_Half e_phentsize; - Elf64_Half e_phnum; - Elf64_Half e_shentsize; - Elf64_Half e_shnum; - Elf64_Half e_shstrndx; -} Elf64_Ehdr; - -#define EI_MAG0 0 -#define ELFMAG0 0x7f - -#define EI_MAG1 1 -#define ELFMAG1 'E' - -#define EI_MAG2 2 -#define ELFMAG2 'L' - -#define EI_MAG3 3 -#define ELFMAG3 'F' - - -#define ELFMAG "\177ELF" -#define SELFMAG 4 - -#define EI_CLASS 4 -#define ELFCLASSNONE 0 -#define ELFCLASS32 1 -#define ELFCLASS64 2 -#define ELFCLASSNUM 3 - -#define EI_DATA 5 -#define ELFDATANONE 0 -#define ELFDATA2LSB 1 -#define ELFDATA2MSB 2 -#define ELFDATANUM 3 - -#define EI_VERSION 6 - - -#define EI_OSABI 7 -#define ELFOSABI_NONE 0 -#define ELFOSABI_SYSV 0 -#define ELFOSABI_HPUX 1 -#define ELFOSABI_NETBSD 2 -#define ELFOSABI_LINUX 3 -#define ELFOSABI_GNU 3 -#define ELFOSABI_SOLARIS 6 -#define ELFOSABI_AIX 7 -#define ELFOSABI_IRIX 8 -#define ELFOSABI_FREEBSD 9 -#define ELFOSABI_TRU64 10 -#define ELFOSABI_MODESTO 11 -#define ELFOSABI_OPENBSD 12 -#define ELFOSABI_ARM 97 -#define ELFOSABI_STANDALONE 255 - -#define EI_ABIVERSION 8 - -#define EI_PAD 9 - - - -#define ET_NONE 0 -#define ET_REL 1 -#define ET_EXEC 2 -#define ET_DYN 3 -#define ET_CORE 4 -#define ET_NUM 5 -#define ET_LOOS 0xfe00 -#define ET_HIOS 0xfeff -#define ET_LOPROC 0xff00 -#define ET_HIPROC 0xffff - - - -#define EM_NONE 0 -#define EM_M32 1 -#define EM_SPARC 2 -#define EM_386 3 -#define EM_68K 4 -#define EM_88K 5 -#define EM_860 7 -#define EM_MIPS 8 -#define EM_S370 9 -#define EM_MIPS_RS3_LE 10 - -#define EM_PARISC 15 -#define EM_VPP500 17 -#define EM_SPARC32PLUS 18 -#define EM_960 19 -#define EM_PPC 20 -#define EM_PPC64 21 -#define EM_S390 22 - -#define EM_V800 36 -#define EM_FR20 37 -#define EM_RH32 38 -#define EM_RCE 39 -#define EM_ARM 40 -#define EM_FAKE_ALPHA 41 -#define EM_SH 42 -#define EM_SPARCV9 43 -#define EM_TRICORE 44 -#define EM_ARC 45 -#define EM_H8_300 46 -#define EM_H8_300H 47 -#define EM_H8S 48 -#define EM_H8_500 49 -#define EM_IA_64 50 -#define EM_MIPS_X 51 -#define EM_COLDFIRE 52 -#define EM_68HC12 53 -#define EM_MMA 54 -#define EM_PCP 55 -#define EM_NCPU 56 -#define EM_NDR1 57 -#define EM_STARCORE 58 -#define EM_ME16 59 -#define EM_ST100 60 -#define EM_TINYJ 61 -#define EM_X86_64 62 -#define EM_PDSP 63 - -#define EM_FX66 66 -#define EM_ST9PLUS 67 -#define EM_ST7 68 -#define EM_68HC16 69 -#define EM_68HC11 70 -#define EM_68HC08 71 -#define EM_68HC05 72 -#define EM_SVX 73 -#define EM_ST19 74 -#define EM_VAX 75 -#define EM_CRIS 76 -#define EM_JAVELIN 77 -#define EM_FIREPATH 78 -#define EM_ZSP 79 -#define EM_MMIX 80 -#define EM_HUANY 81 -#define EM_PRISM 82 -#define EM_AVR 83 -#define EM_FR30 84 -#define EM_D10V 85 -#define EM_D30V 86 -#define EM_V850 87 -#define EM_M32R 88 -#define EM_MN10300 89 -#define EM_MN10200 90 -#define EM_PJ 91 -#define EM_OR1K 92 -#define EM_OPENRISC 92 -#define EM_ARC_A5 93 -#define EM_ARC_COMPACT 93 -#define EM_XTENSA 94 -#define EM_VIDEOCORE 95 -#define EM_TMM_GPP 96 -#define EM_NS32K 97 -#define EM_TPC 98 -#define EM_SNP1K 99 -#define EM_ST200 100 -#define EM_IP2K 101 -#define EM_MAX 102 -#define EM_CR 103 -#define EM_F2MC16 104 -#define EM_MSP430 105 -#define EM_BLACKFIN 106 -#define EM_SE_C33 107 -#define EM_SEP 108 -#define EM_ARCA 109 -#define EM_UNICORE 110 -#define EM_EXCESS 111 -#define EM_DXP 112 -#define EM_ALTERA_NIOS2 113 -#define EM_CRX 114 -#define EM_XGATE 115 -#define EM_C166 116 -#define EM_M16C 117 -#define EM_DSPIC30F 118 -#define EM_CE 119 -#define EM_M32C 120 -#define EM_TSK3000 131 -#define EM_RS08 132 -#define EM_SHARC 133 -#define EM_ECOG2 134 -#define EM_SCORE7 135 -#define EM_DSP24 136 -#define EM_VIDEOCORE3 137 -#define EM_LATTICEMICO32 138 -#define EM_SE_C17 139 -#define EM_TI_C6000 140 -#define EM_TI_C2000 141 -#define EM_TI_C5500 142 -#define EM_TI_ARP32 143 -#define EM_TI_PRU 144 -#define EM_MMDSP_PLUS 160 -#define EM_CYPRESS_M8C 161 -#define EM_R32C 162 -#define EM_TRIMEDIA 163 -#define EM_QDSP6 164 -#define EM_8051 165 -#define EM_STXP7X 166 -#define EM_NDS32 167 -#define EM_ECOG1X 168 -#define EM_MAXQ30 169 -#define EM_XIMO16 170 -#define EM_MANIK 171 -#define EM_CRAYNV2 172 -#define EM_RX 173 -#define EM_METAG 174 -#define EM_MCST_ELBRUS 175 -#define EM_ECOG16 176 -#define EM_CR16 177 -#define EM_ETPU 178 -#define EM_SLE9X 179 -#define EM_L10M 180 -#define EM_K10M 181 -#define EM_AARCH64 183 -#define EM_AVR32 185 -#define EM_STM8 186 -#define EM_TILE64 187 -#define EM_TILEPRO 188 -#define EM_MICROBLAZE 189 -#define EM_CUDA 190 -#define EM_TILEGX 191 -#define EM_CLOUDSHIELD 192 -#define EM_COREA_1ST 193 -#define EM_COREA_2ND 194 -#define EM_ARC_COMPACT2 195 -#define EM_OPEN8 196 -#define EM_RL78 197 -#define EM_VIDEOCORE5 198 -#define EM_78KOR 199 -#define EM_56800EX 200 -#define EM_BA1 201 -#define EM_BA2 202 -#define EM_XCORE 203 -#define EM_MCHP_PIC 204 -#define EM_KM32 210 -#define EM_KMX32 211 -#define EM_EMX16 212 -#define EM_EMX8 213 -#define EM_KVARC 214 -#define EM_CDP 215 -#define EM_COGE 216 -#define EM_COOL 217 -#define EM_NORC 218 -#define EM_CSR_KALIMBA 219 -#define EM_Z80 220 -#define EM_VISIUM 221 -#define EM_FT32 222 -#define EM_MOXIE 223 -#define EM_AMDGPU 224 -#define EM_RISCV 243 -#define EM_BPF 247 -#define EM_CSKY 252 -#define EM_NUM 253 - -#define EM_ALPHA 0x9026 - -#define EV_NONE 0 -#define EV_CURRENT 1 -#define EV_NUM 2 - -typedef struct { - Elf32_Word sh_name; - Elf32_Word sh_type; - Elf32_Word sh_flags; - Elf32_Addr sh_addr; - Elf32_Off sh_offset; - Elf32_Word sh_size; - Elf32_Word sh_link; - Elf32_Word sh_info; - Elf32_Word sh_addralign; - Elf32_Word sh_entsize; -} Elf32_Shdr; - -typedef struct { - Elf64_Word sh_name; - Elf64_Word sh_type; - Elf64_Xword sh_flags; - Elf64_Addr sh_addr; - Elf64_Off sh_offset; - Elf64_Xword sh_size; - Elf64_Word sh_link; - Elf64_Word sh_info; - Elf64_Xword sh_addralign; - Elf64_Xword sh_entsize; -} Elf64_Shdr; - - - -#define SHN_UNDEF 0 -#define SHN_LORESERVE 0xff00 -#define SHN_LOPROC 0xff00 -#define SHN_BEFORE 0xff00 - -#define SHN_AFTER 0xff01 - -#define SHN_HIPROC 0xff1f -#define SHN_LOOS 0xff20 -#define SHN_HIOS 0xff3f -#define SHN_ABS 0xfff1 -#define SHN_COMMON 0xfff2 -#define SHN_XINDEX 0xffff -#define SHN_HIRESERVE 0xffff - - - -#define SHT_NULL 0 -#define SHT_PROGBITS 1 -#define SHT_SYMTAB 2 -#define SHT_STRTAB 3 -#define SHT_RELA 4 -#define SHT_HASH 5 -#define SHT_DYNAMIC 6 -#define SHT_NOTE 7 -#define SHT_NOBITS 8 -#define SHT_REL 9 -#define SHT_SHLIB 10 -#define SHT_DYNSYM 11 -#define SHT_INIT_ARRAY 14 -#define SHT_FINI_ARRAY 15 -#define SHT_PREINIT_ARRAY 16 -#define SHT_GROUP 17 -#define SHT_SYMTAB_SHNDX 18 -#define SHT_RELR 19 -#define SHT_NUM 20 -#define SHT_LOOS 0x60000000 -#define SHT_GNU_ATTRIBUTES 0x6ffffff5 -#define SHT_GNU_HASH 0x6ffffff6 -#define SHT_GNU_LIBLIST 0x6ffffff7 -#define SHT_CHECKSUM 0x6ffffff8 -#define SHT_LOSUNW 0x6ffffffa -#define SHT_SUNW_move 0x6ffffffa -#define SHT_SUNW_COMDAT 0x6ffffffb -#define SHT_SUNW_syminfo 0x6ffffffc -#define SHT_GNU_verdef 0x6ffffffd -#define SHT_GNU_verneed 0x6ffffffe -#define SHT_GNU_versym 0x6fffffff -#define SHT_HISUNW 0x6fffffff -#define SHT_HIOS 0x6fffffff -#define SHT_LOPROC 0x70000000 -#define SHT_HIPROC 0x7fffffff -#define SHT_LOUSER 0x80000000 -#define SHT_HIUSER 0x8fffffff - -#define SHF_WRITE (1 << 0) -#define SHF_ALLOC (1 << 1) -#define SHF_EXECINSTR (1 << 2) -#define SHF_MERGE (1 << 4) -#define SHF_STRINGS (1 << 5) -#define SHF_INFO_LINK (1 << 6) -#define SHF_LINK_ORDER (1 << 7) -#define SHF_OS_NONCONFORMING (1 << 8) - -#define SHF_GROUP (1 << 9) -#define SHF_TLS (1 << 10) -#define SHF_COMPRESSED (1 << 11) -#define SHF_MASKOS 0x0ff00000 -#define SHF_MASKPROC 0xf0000000 -#define SHF_ORDERED (1 << 30) -#define SHF_EXCLUDE (1U << 31) - -typedef struct { - Elf32_Word ch_type; - Elf32_Word ch_size; - Elf32_Word ch_addralign; -} Elf32_Chdr; - -typedef struct { - Elf64_Word ch_type; - Elf64_Word ch_reserved; - Elf64_Xword ch_size; - Elf64_Xword ch_addralign; -} Elf64_Chdr; - -#define ELFCOMPRESS_ZLIB 1 -#define ELFCOMPRESS_LOOS 0x60000000 -#define ELFCOMPRESS_HIOS 0x6fffffff -#define ELFCOMPRESS_LOPROC 0x70000000 -#define ELFCOMPRESS_HIPROC 0x7fffffff - - -#define GRP_COMDAT 0x1 - -typedef struct { - Elf32_Word st_name; - Elf32_Addr st_value; - Elf32_Word st_size; - unsigned char st_info; - unsigned char st_other; - Elf32_Section st_shndx; -} Elf32_Sym; - -typedef struct { - Elf64_Word st_name; - unsigned char st_info; - unsigned char st_other; - Elf64_Section st_shndx; - Elf64_Addr st_value; - Elf64_Xword st_size; -} Elf64_Sym; - -typedef struct { - Elf32_Half si_boundto; - Elf32_Half si_flags; -} Elf32_Syminfo; - -typedef struct { - Elf64_Half si_boundto; - Elf64_Half si_flags; -} Elf64_Syminfo; - -#define SYMINFO_BT_SELF 0xffff -#define SYMINFO_BT_PARENT 0xfffe -#define SYMINFO_BT_LOWRESERVE 0xff00 - -#define SYMINFO_FLG_DIRECT 0x0001 -#define SYMINFO_FLG_PASSTHRU 0x0002 -#define SYMINFO_FLG_COPY 0x0004 -#define SYMINFO_FLG_LAZYLOAD 0x0008 - -#define SYMINFO_NONE 0 -#define SYMINFO_CURRENT 1 -#define SYMINFO_NUM 2 - -#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4) -#define ELF32_ST_TYPE(val) ((val) & 0xf) -#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) - -#define ELF64_ST_BIND(val) ELF32_ST_BIND (val) -#define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val) -#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) - -#define STB_LOCAL 0 -#define STB_GLOBAL 1 -#define STB_WEAK 2 -#define STB_NUM 3 -#define STB_LOOS 10 -#define STB_GNU_UNIQUE 10 -#define STB_HIOS 12 -#define STB_LOPROC 13 -#define STB_HIPROC 15 - -#define STT_NOTYPE 0 -#define STT_OBJECT 1 -#define STT_FUNC 2 -#define STT_SECTION 3 -#define STT_FILE 4 -#define STT_COMMON 5 -#define STT_TLS 6 -#define STT_NUM 7 -#define STT_LOOS 10 -#define STT_GNU_IFUNC 10 -#define STT_HIOS 12 -#define STT_LOPROC 13 -#define STT_HIPROC 15 - -#define STN_UNDEF 0 - -#define ELF32_ST_VISIBILITY(o) ((o) & 0x03) -#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) - -#define STV_DEFAULT 0 -#define STV_INTERNAL 1 -#define STV_HIDDEN 2 -#define STV_PROTECTED 3 - - - - -typedef struct { - Elf32_Addr r_offset; - Elf32_Word r_info; -} Elf32_Rel; - -typedef struct { - Elf64_Addr r_offset; - Elf64_Xword r_info; -} Elf64_Rel; - - - -typedef struct { - Elf32_Addr r_offset; - Elf32_Word r_info; - Elf32_Sword r_addend; -} Elf32_Rela; - -typedef struct { - Elf64_Addr r_offset; - Elf64_Xword r_info; - Elf64_Sxword r_addend; -} Elf64_Rela; - - - -#define ELF32_R_SYM(val) ((val) >> 8) -#define ELF32_R_TYPE(val) ((val) & 0xff) -#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) - -#define ELF64_R_SYM(i) ((i) >> 32) -#define ELF64_R_TYPE(i) ((i) & 0xffffffff) -#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type)) - - - -typedef struct { - Elf32_Word p_type; - Elf32_Off p_offset; - Elf32_Addr p_vaddr; - Elf32_Addr p_paddr; - Elf32_Word p_filesz; - Elf32_Word p_memsz; - Elf32_Word p_flags; - Elf32_Word p_align; -} Elf32_Phdr; - -typedef struct { - Elf64_Word p_type; - Elf64_Word p_flags; - Elf64_Off p_offset; - Elf64_Addr p_vaddr; - Elf64_Addr p_paddr; - Elf64_Xword p_filesz; - Elf64_Xword p_memsz; - Elf64_Xword p_align; -} Elf64_Phdr; - - - -#define PT_NULL 0 -#define PT_LOAD 1 -#define PT_DYNAMIC 2 -#define PT_INTERP 3 -#define PT_NOTE 4 -#define PT_SHLIB 5 -#define PT_PHDR 6 -#define PT_TLS 7 -#define PT_NUM 8 -#define PT_LOOS 0x60000000 -#define PT_GNU_EH_FRAME 0x6474e550 -#define PT_GNU_STACK 0x6474e551 -#define PT_GNU_RELRO 0x6474e552 -#define PT_GNU_PROPERTY 0x6474e553 -#define PT_LOSUNW 0x6ffffffa -#define PT_SUNWBSS 0x6ffffffa -#define PT_SUNWSTACK 0x6ffffffb -#define PT_HISUNW 0x6fffffff -#define PT_HIOS 0x6fffffff -#define PT_LOPROC 0x70000000 -#define PT_HIPROC 0x7fffffff - -#define PT_OHOS_RANDOMDATA 0x6788fc60 - -#define PN_XNUM 0xffff - - -#define PF_X (1 << 0) -#define PF_W (1 << 1) -#define PF_R (1 << 2) -#define PF_MASKOS 0x0ff00000 -#define PF_MASKPROC 0xf0000000 - - - -#define NT_PRSTATUS 1 -#define NT_PRFPREG 2 -#define NT_FPREGSET 2 -#define NT_PRPSINFO 3 -#define NT_PRXREG 4 -#define NT_TASKSTRUCT 4 -#define NT_PLATFORM 5 -#define NT_AUXV 6 -#define NT_GWINDOWS 7 -#define NT_ASRS 8 -#define NT_PSTATUS 10 -#define NT_PSINFO 13 -#define NT_PRCRED 14 -#define NT_UTSNAME 15 -#define NT_LWPSTATUS 16 -#define NT_LWPSINFO 17 -#define NT_PRFPXREG 20 -#define NT_SIGINFO 0x53494749 -#define NT_FILE 0x46494c45 -#define NT_PRXFPREG 0x46e62b7f -#define NT_PPC_VMX 0x100 -#define NT_PPC_SPE 0x101 -#define NT_PPC_VSX 0x102 -#define NT_PPC_TAR 0x103 -#define NT_PPC_PPR 0x104 -#define NT_PPC_DSCR 0x105 -#define NT_PPC_EBB 0x106 -#define NT_PPC_PMU 0x107 -#define NT_PPC_TM_CGPR 0x108 -#define NT_PPC_TM_CFPR 0x109 -#define NT_PPC_TM_CVMX 0x10a -#define NT_PPC_TM_CVSX 0x10b -#define NT_PPC_TM_SPR 0x10c -#define NT_PPC_TM_CTAR 0x10d -#define NT_PPC_TM_CPPR 0x10e -#define NT_PPC_TM_CDSCR 0x10f -#define NT_386_TLS 0x200 -#define NT_386_IOPERM 0x201 -#define NT_X86_XSTATE 0x202 -#define NT_S390_HIGH_GPRS 0x300 -#define NT_S390_TIMER 0x301 -#define NT_S390_TODCMP 0x302 -#define NT_S390_TODPREG 0x303 -#define NT_S390_CTRS 0x304 -#define NT_S390_PREFIX 0x305 -#define NT_S390_LAST_BREAK 0x306 -#define NT_S390_SYSTEM_CALL 0x307 -#define NT_S390_TDB 0x308 -#define NT_S390_VXRS_LOW 0x309 -#define NT_S390_VXRS_HIGH 0x30a -#define NT_S390_GS_CB 0x30b -#define NT_S390_GS_BC 0x30c -#define NT_S390_RI_CB 0x30d -#define NT_ARM_VFP 0x400 -#define NT_ARM_TLS 0x401 -#define NT_ARM_HW_BREAK 0x402 -#define NT_ARM_HW_WATCH 0x403 -#define NT_ARM_SYSTEM_CALL 0x404 -#define NT_ARM_SVE 0x405 -#define NT_ARM_PAC_MASK 0x406 -#define NT_ARM_PACA_KEYS 0x407 -#define NT_ARM_PACG_KEYS 0x408 -#define NT_ARM_TAGGED_ADDR_CTRL 0x409 -#define NT_ARM_PAC_ENABLED_KEYS 0x40a -#define NT_METAG_CBUF 0x500 -#define NT_METAG_RPIPE 0x501 -#define NT_METAG_TLS 0x502 -#define NT_ARC_V2 0x600 -#define NT_VMCOREDD 0x700 -#define NT_MIPS_DSP 0x800 -#define NT_MIPS_FP_MODE 0x801 -#define NT_MIPS_MSA 0x802 -#define NT_VERSION 1 - - - - -typedef struct { - Elf32_Sword d_tag; - union { - Elf32_Word d_val; - Elf32_Addr d_ptr; - } d_un; -} Elf32_Dyn; - -typedef struct { - Elf64_Sxword d_tag; - union { - Elf64_Xword d_val; - Elf64_Addr d_ptr; - } d_un; -} Elf64_Dyn; - - - -#define DT_NULL 0 -#define DT_NEEDED 1 -#define DT_PLTRELSZ 2 -#define DT_PLTGOT 3 -#define DT_HASH 4 -#define DT_STRTAB 5 -#define DT_SYMTAB 6 -#define DT_RELA 7 -#define DT_RELASZ 8 -#define DT_RELAENT 9 -#define DT_STRSZ 10 -#define DT_SYMENT 11 -#define DT_INIT 12 -#define DT_FINI 13 -#define DT_SONAME 14 -#define DT_RPATH 15 -#define DT_SYMBOLIC 16 -#define DT_REL 17 -#define DT_RELSZ 18 -#define DT_RELENT 19 -#define DT_PLTREL 20 -#define DT_DEBUG 21 -#define DT_TEXTREL 22 -#define DT_JMPREL 23 -#define DT_BIND_NOW 24 -#define DT_INIT_ARRAY 25 -#define DT_FINI_ARRAY 26 -#define DT_INIT_ARRAYSZ 27 -#define DT_FINI_ARRAYSZ 28 -#define DT_RUNPATH 29 -#define DT_FLAGS 30 -#define DT_ENCODING 32 -#define DT_PREINIT_ARRAY 32 -#define DT_PREINIT_ARRAYSZ 33 -#define DT_SYMTAB_SHNDX 34 -#define DT_RELRSZ 35 -#define DT_RELR 36 -#define DT_RELRENT 37 -#define DT_NUM 38 -#define DT_LOOS 0x6000000d -#define DT_HIOS 0x6ffff000 -#define DT_LOPROC 0x70000000 -#define DT_HIPROC 0x7fffffff -#define DT_PROCNUM DT_MIPS_NUM - -#define DT_VALRNGLO 0x6ffffd00 -#define DT_GNU_PRELINKED 0x6ffffdf5 -#define DT_GNU_CONFLICTSZ 0x6ffffdf6 -#define DT_GNU_LIBLISTSZ 0x6ffffdf7 -#define DT_CHECKSUM 0x6ffffdf8 -#define DT_PLTPADSZ 0x6ffffdf9 -#define DT_MOVEENT 0x6ffffdfa -#define DT_MOVESZ 0x6ffffdfb -#define DT_FEATURE_1 0x6ffffdfc -#define DT_POSFLAG_1 0x6ffffdfd - -#define DT_SYMINSZ 0x6ffffdfe -#define DT_SYMINENT 0x6ffffdff -#define DT_VALRNGHI 0x6ffffdff -#define DT_VALTAGIDX(tag) (DT_VALRNGHI - (tag)) -#define DT_VALNUM 12 - -#define DT_ADDRRNGLO 0x6ffffe00 -#define DT_GNU_HASH 0x6ffffef5 -#define DT_TLSDESC_PLT 0x6ffffef6 -#define DT_TLSDESC_GOT 0x6ffffef7 -#define DT_GNU_CONFLICT 0x6ffffef8 -#define DT_GNU_LIBLIST 0x6ffffef9 -#define DT_CONFIG 0x6ffffefa -#define DT_DEPAUDIT 0x6ffffefb -#define DT_AUDIT 0x6ffffefc -#define DT_PLTPAD 0x6ffffefd -#define DT_MOVETAB 0x6ffffefe -#define DT_SYMINFO 0x6ffffeff -#define DT_ADDRRNGHI 0x6ffffeff -#define DT_ADDRTAGIDX(tag) (DT_ADDRRNGHI - (tag)) -#define DT_ADDRNUM 11 - - - -#define DT_VERSYM 0x6ffffff0 - -#define DT_RELACOUNT 0x6ffffff9 -#define DT_RELCOUNT 0x6ffffffa - - -#define DT_FLAGS_1 0x6ffffffb -#define DT_VERDEF 0x6ffffffc - -#define DT_VERDEFNUM 0x6ffffffd -#define DT_VERNEED 0x6ffffffe - -#define DT_VERNEEDNUM 0x6fffffff -#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) -#define DT_VERSIONTAGNUM 16 - - - -#define DT_AUXILIARY 0x7ffffffd -#define DT_FILTER 0x7fffffff -#define DT_EXTRATAGIDX(tag) ((Elf32_Word)-((Elf32_Sword) (tag) <<1>>1)-1) -#define DT_EXTRANUM 3 - - -#define DF_ORIGIN 0x00000001 -#define DF_SYMBOLIC 0x00000002 -#define DF_TEXTREL 0x00000004 -#define DF_BIND_NOW 0x00000008 -#define DF_STATIC_TLS 0x00000010 - - - -#define DF_1_NOW 0x00000001 -#define DF_1_GLOBAL 0x00000002 -#define DF_1_GROUP 0x00000004 -#define DF_1_NODELETE 0x00000008 -#define DF_1_LOADFLTR 0x00000010 -#define DF_1_INITFIRST 0x00000020 -#define DF_1_NOOPEN 0x00000040 -#define DF_1_ORIGIN 0x00000080 -#define DF_1_DIRECT 0x00000100 -#define DF_1_TRANS 0x00000200 -#define DF_1_INTERPOSE 0x00000400 -#define DF_1_NODEFLIB 0x00000800 -#define DF_1_NODUMP 0x00001000 -#define DF_1_CONFALT 0x00002000 -#define DF_1_ENDFILTEE 0x00004000 -#define DF_1_DISPRELDNE 0x00008000 -#define DF_1_DISPRELPND 0x00010000 -#define DF_1_NODIRECT 0x00020000 -#define DF_1_IGNMULDEF 0x00040000 -#define DF_1_NOKSYMS 0x00080000 -#define DF_1_NOHDR 0x00100000 -#define DF_1_EDITED 0x00200000 -#define DF_1_NORELOC 0x00400000 -#define DF_1_SYMINTPOSE 0x00800000 -#define DF_1_GLOBAUDIT 0x01000000 -#define DF_1_SINGLETON 0x02000000 -#define DF_1_STUB 0x04000000 -#define DF_1_PIE 0x08000000 - -#define DTF_1_PARINIT 0x00000001 -#define DTF_1_CONFEXP 0x00000002 - - -#define DF_P1_LAZYLOAD 0x00000001 -#define DF_P1_GROUPPERM 0x00000002 - - - - -typedef struct { - Elf32_Half vd_version; - Elf32_Half vd_flags; - Elf32_Half vd_ndx; - Elf32_Half vd_cnt; - Elf32_Word vd_hash; - Elf32_Word vd_aux; - Elf32_Word vd_next; -} Elf32_Verdef; - -typedef struct { - Elf64_Half vd_version; - Elf64_Half vd_flags; - Elf64_Half vd_ndx; - Elf64_Half vd_cnt; - Elf64_Word vd_hash; - Elf64_Word vd_aux; - Elf64_Word vd_next; -} Elf64_Verdef; - - - -#define VER_DEF_NONE 0 -#define VER_DEF_CURRENT 1 -#define VER_DEF_NUM 2 - - -#define VER_FLG_BASE 0x1 -#define VER_FLG_WEAK 0x2 - - -#define VER_NDX_LOCAL 0 -#define VER_NDX_GLOBAL 1 -#define VER_NDX_LORESERVE 0xff00 -#define VER_NDX_ELIMINATE 0xff01 - - - -typedef struct { - Elf32_Word vda_name; - Elf32_Word vda_next; -} Elf32_Verdaux; - -typedef struct { - Elf64_Word vda_name; - Elf64_Word vda_next; -} Elf64_Verdaux; - - - - -typedef struct { - Elf32_Half vn_version; - Elf32_Half vn_cnt; - Elf32_Word vn_file; - Elf32_Word vn_aux; - Elf32_Word vn_next; -} Elf32_Verneed; - -typedef struct { - Elf64_Half vn_version; - Elf64_Half vn_cnt; - Elf64_Word vn_file; - Elf64_Word vn_aux; - Elf64_Word vn_next; -} Elf64_Verneed; - - - -#define VER_NEED_NONE 0 -#define VER_NEED_CURRENT 1 -#define VER_NEED_NUM 2 - - - -typedef struct { - Elf32_Word vna_hash; - Elf32_Half vna_flags; - Elf32_Half vna_other; - Elf32_Word vna_name; - Elf32_Word vna_next; -} Elf32_Vernaux; - -typedef struct { - Elf64_Word vna_hash; - Elf64_Half vna_flags; - Elf64_Half vna_other; - Elf64_Word vna_name; - Elf64_Word vna_next; -} Elf64_Vernaux; - - - -#define VER_FLG_WEAK 0x2 - - - -typedef struct { - uint32_t a_type; - union { - uint32_t a_val; - } a_un; -} Elf32_auxv_t; - -typedef struct { - uint64_t a_type; - union { - uint64_t a_val; - } a_un; -} Elf64_auxv_t; - - - -#define AT_NULL 0 -#define AT_IGNORE 1 -#define AT_EXECFD 2 -#define AT_PHDR 3 -#define AT_PHENT 4 -#define AT_PHNUM 5 -#define AT_PAGESZ 6 -#define AT_BASE 7 -#define AT_FLAGS 8 -#define AT_ENTRY 9 -#define AT_NOTELF 10 -#define AT_UID 11 -#define AT_EUID 12 -#define AT_GID 13 -#define AT_EGID 14 -#define AT_CLKTCK 17 - - -#define AT_PLATFORM 15 -#define AT_HWCAP 16 - - - - -#define AT_FPUCW 18 - - -#define AT_DCACHEBSIZE 19 -#define AT_ICACHEBSIZE 20 -#define AT_UCACHEBSIZE 21 - - - -#define AT_IGNOREPPC 22 - -#define AT_SECURE 23 - -#define AT_BASE_PLATFORM 24 - -#define AT_RANDOM 25 - -#define AT_HWCAP2 26 - -#define AT_EXECFN 31 - - - -#define AT_SYSINFO 32 -#define AT_SYSINFO_EHDR 33 - - - -#define AT_L1I_CACHESHAPE 34 -#define AT_L1D_CACHESHAPE 35 -#define AT_L2_CACHESHAPE 36 -#define AT_L3_CACHESHAPE 37 - -#define AT_L1I_CACHESIZE 40 -#define AT_L1I_CACHEGEOMETRY 41 -#define AT_L1D_CACHESIZE 42 -#define AT_L1D_CACHEGEOMETRY 43 -#define AT_L2_CACHESIZE 44 -#define AT_L2_CACHEGEOMETRY 45 -#define AT_L3_CACHESIZE 46 -#define AT_L3_CACHEGEOMETRY 47 - -#define AT_MINSIGSTKSZ 51 - - -typedef struct { - Elf32_Word n_namesz; - Elf32_Word n_descsz; - Elf32_Word n_type; -} Elf32_Nhdr; - -typedef struct { - Elf64_Word n_namesz; - Elf64_Word n_descsz; - Elf64_Word n_type; -} Elf64_Nhdr; - - - - -#define ELF_NOTE_SOLARIS "SUNW Solaris" - - -#define ELF_NOTE_GNU "GNU" - - - - - -#define ELF_NOTE_PAGESIZE_HINT 1 - - -#define NT_GNU_ABI_TAG 1 -#define ELF_NOTE_ABI NT_GNU_ABI_TAG - - - -#define ELF_NOTE_OS_LINUX 0 -#define ELF_NOTE_OS_GNU 1 -#define ELF_NOTE_OS_SOLARIS2 2 -#define ELF_NOTE_OS_FREEBSD 3 - -#define NT_GNU_BUILD_ID 3 -#define NT_GNU_GOLD_VERSION 4 -#define NT_GNU_PROPERTY_TYPE_0 5 - - -typedef struct { - Elf32_Xword m_value; - Elf32_Word m_info; - Elf32_Word m_poffset; - Elf32_Half m_repeat; - Elf32_Half m_stride; -} Elf32_Move; - -typedef struct { - Elf64_Xword m_value; - Elf64_Xword m_info; - Elf64_Xword m_poffset; - Elf64_Half m_repeat; - Elf64_Half m_stride; -} Elf64_Move; - - -#define ELF32_M_SYM(info) ((info) >> 8) -#define ELF32_M_SIZE(info) ((unsigned char) (info)) -#define ELF32_M_INFO(sym, size) (((sym) << 8) + (unsigned char) (size)) - -#define ELF64_M_SYM(info) ELF32_M_SYM (info) -#define ELF64_M_SIZE(info) ELF32_M_SIZE (info) -#define ELF64_M_INFO(sym, size) ELF32_M_INFO (sym, size) - -#define EF_CPU32 0x00810000 - -#define R_68K_NONE 0 -#define R_68K_32 1 -#define R_68K_16 2 -#define R_68K_8 3 -#define R_68K_PC32 4 -#define R_68K_PC16 5 -#define R_68K_PC8 6 -#define R_68K_GOT32 7 -#define R_68K_GOT16 8 -#define R_68K_GOT8 9 -#define R_68K_GOT32O 10 -#define R_68K_GOT16O 11 -#define R_68K_GOT8O 12 -#define R_68K_PLT32 13 -#define R_68K_PLT16 14 -#define R_68K_PLT8 15 -#define R_68K_PLT32O 16 -#define R_68K_PLT16O 17 -#define R_68K_PLT8O 18 -#define R_68K_COPY 19 -#define R_68K_GLOB_DAT 20 -#define R_68K_JMP_SLOT 21 -#define R_68K_RELATIVE 22 -#define R_68K_TLS_GD32 25 -#define R_68K_TLS_GD16 26 -#define R_68K_TLS_GD8 27 -#define R_68K_TLS_LDM32 28 -#define R_68K_TLS_LDM16 29 -#define R_68K_TLS_LDM8 30 -#define R_68K_TLS_LDO32 31 -#define R_68K_TLS_LDO16 32 -#define R_68K_TLS_LDO8 33 -#define R_68K_TLS_IE32 34 -#define R_68K_TLS_IE16 35 -#define R_68K_TLS_IE8 36 -#define R_68K_TLS_LE32 37 -#define R_68K_TLS_LE16 38 -#define R_68K_TLS_LE8 39 -#define R_68K_TLS_DTPMOD32 40 -#define R_68K_TLS_DTPREL32 41 -#define R_68K_TLS_TPREL32 42 -#define R_68K_NUM 43 - -#define R_386_NONE 0 -#define R_386_32 1 -#define R_386_PC32 2 -#define R_386_GOT32 3 -#define R_386_PLT32 4 -#define R_386_COPY 5 -#define R_386_GLOB_DAT 6 -#define R_386_JMP_SLOT 7 -#define R_386_RELATIVE 8 -#define R_386_GOTOFF 9 -#define R_386_GOTPC 10 -#define R_386_32PLT 11 -#define R_386_TLS_TPOFF 14 -#define R_386_TLS_IE 15 -#define R_386_TLS_GOTIE 16 -#define R_386_TLS_LE 17 -#define R_386_TLS_GD 18 -#define R_386_TLS_LDM 19 -#define R_386_16 20 -#define R_386_PC16 21 -#define R_386_8 22 -#define R_386_PC8 23 -#define R_386_TLS_GD_32 24 -#define R_386_TLS_GD_PUSH 25 -#define R_386_TLS_GD_CALL 26 -#define R_386_TLS_GD_POP 27 -#define R_386_TLS_LDM_32 28 -#define R_386_TLS_LDM_PUSH 29 -#define R_386_TLS_LDM_CALL 30 -#define R_386_TLS_LDM_POP 31 -#define R_386_TLS_LDO_32 32 -#define R_386_TLS_IE_32 33 -#define R_386_TLS_LE_32 34 -#define R_386_TLS_DTPMOD32 35 -#define R_386_TLS_DTPOFF32 36 -#define R_386_TLS_TPOFF32 37 -#define R_386_SIZE32 38 -#define R_386_TLS_GOTDESC 39 -#define R_386_TLS_DESC_CALL 40 -#define R_386_TLS_DESC 41 -#define R_386_IRELATIVE 42 -#define R_386_GOT32X 43 -#define R_386_NUM 44 - - - - - -#define STT_SPARC_REGISTER 13 - - - -#define EF_SPARCV9_MM 3 -#define EF_SPARCV9_TSO 0 -#define EF_SPARCV9_PSO 1 -#define EF_SPARCV9_RMO 2 -#define EF_SPARC_LEDATA 0x800000 -#define EF_SPARC_EXT_MASK 0xFFFF00 -#define EF_SPARC_32PLUS 0x000100 -#define EF_SPARC_SUN_US1 0x000200 -#define EF_SPARC_HAL_R1 0x000400 -#define EF_SPARC_SUN_US3 0x000800 - - - -#define R_SPARC_NONE 0 -#define R_SPARC_8 1 -#define R_SPARC_16 2 -#define R_SPARC_32 3 -#define R_SPARC_DISP8 4 -#define R_SPARC_DISP16 5 -#define R_SPARC_DISP32 6 -#define R_SPARC_WDISP30 7 -#define R_SPARC_WDISP22 8 -#define R_SPARC_HI22 9 -#define R_SPARC_22 10 -#define R_SPARC_13 11 -#define R_SPARC_LO10 12 -#define R_SPARC_GOT10 13 -#define R_SPARC_GOT13 14 -#define R_SPARC_GOT22 15 -#define R_SPARC_PC10 16 -#define R_SPARC_PC22 17 -#define R_SPARC_WPLT30 18 -#define R_SPARC_COPY 19 -#define R_SPARC_GLOB_DAT 20 -#define R_SPARC_JMP_SLOT 21 -#define R_SPARC_RELATIVE 22 -#define R_SPARC_UA32 23 - - - -#define R_SPARC_PLT32 24 -#define R_SPARC_HIPLT22 25 -#define R_SPARC_LOPLT10 26 -#define R_SPARC_PCPLT32 27 -#define R_SPARC_PCPLT22 28 -#define R_SPARC_PCPLT10 29 -#define R_SPARC_10 30 -#define R_SPARC_11 31 -#define R_SPARC_64 32 -#define R_SPARC_OLO10 33 -#define R_SPARC_HH22 34 -#define R_SPARC_HM10 35 -#define R_SPARC_LM22 36 -#define R_SPARC_PC_HH22 37 -#define R_SPARC_PC_HM10 38 -#define R_SPARC_PC_LM22 39 -#define R_SPARC_WDISP16 40 -#define R_SPARC_WDISP19 41 -#define R_SPARC_GLOB_JMP 42 -#define R_SPARC_7 43 -#define R_SPARC_5 44 -#define R_SPARC_6 45 -#define R_SPARC_DISP64 46 -#define R_SPARC_PLT64 47 -#define R_SPARC_HIX22 48 -#define R_SPARC_LOX10 49 -#define R_SPARC_H44 50 -#define R_SPARC_M44 51 -#define R_SPARC_L44 52 -#define R_SPARC_REGISTER 53 -#define R_SPARC_UA64 54 -#define R_SPARC_UA16 55 -#define R_SPARC_TLS_GD_HI22 56 -#define R_SPARC_TLS_GD_LO10 57 -#define R_SPARC_TLS_GD_ADD 58 -#define R_SPARC_TLS_GD_CALL 59 -#define R_SPARC_TLS_LDM_HI22 60 -#define R_SPARC_TLS_LDM_LO10 61 -#define R_SPARC_TLS_LDM_ADD 62 -#define R_SPARC_TLS_LDM_CALL 63 -#define R_SPARC_TLS_LDO_HIX22 64 -#define R_SPARC_TLS_LDO_LOX10 65 -#define R_SPARC_TLS_LDO_ADD 66 -#define R_SPARC_TLS_IE_HI22 67 -#define R_SPARC_TLS_IE_LO10 68 -#define R_SPARC_TLS_IE_LD 69 -#define R_SPARC_TLS_IE_LDX 70 -#define R_SPARC_TLS_IE_ADD 71 -#define R_SPARC_TLS_LE_HIX22 72 -#define R_SPARC_TLS_LE_LOX10 73 -#define R_SPARC_TLS_DTPMOD32 74 -#define R_SPARC_TLS_DTPMOD64 75 -#define R_SPARC_TLS_DTPOFF32 76 -#define R_SPARC_TLS_DTPOFF64 77 -#define R_SPARC_TLS_TPOFF32 78 -#define R_SPARC_TLS_TPOFF64 79 -#define R_SPARC_GOTDATA_HIX22 80 -#define R_SPARC_GOTDATA_LOX10 81 -#define R_SPARC_GOTDATA_OP_HIX22 82 -#define R_SPARC_GOTDATA_OP_LOX10 83 -#define R_SPARC_GOTDATA_OP 84 -#define R_SPARC_H34 85 -#define R_SPARC_SIZE32 86 -#define R_SPARC_SIZE64 87 -#define R_SPARC_GNU_VTINHERIT 250 -#define R_SPARC_GNU_VTENTRY 251 -#define R_SPARC_REV32 252 - -#define R_SPARC_NUM 253 - - - -#define DT_SPARC_REGISTER 0x70000001 -#define DT_SPARC_NUM 2 - - -#define EF_MIPS_NOREORDER 1 -#define EF_MIPS_PIC 2 -#define EF_MIPS_CPIC 4 -#define EF_MIPS_XGOT 8 -#define EF_MIPS_64BIT_WHIRL 16 -#define EF_MIPS_ABI2 32 -#define EF_MIPS_ABI_ON32 64 -#define EF_MIPS_FP64 512 -#define EF_MIPS_NAN2008 1024 -#define EF_MIPS_ARCH 0xf0000000 - - - -#define EF_MIPS_ARCH_1 0x00000000 -#define EF_MIPS_ARCH_2 0x10000000 -#define EF_MIPS_ARCH_3 0x20000000 -#define EF_MIPS_ARCH_4 0x30000000 -#define EF_MIPS_ARCH_5 0x40000000 -#define EF_MIPS_ARCH_32 0x50000000 -#define EF_MIPS_ARCH_64 0x60000000 -#define EF_MIPS_ARCH_32R2 0x70000000 -#define EF_MIPS_ARCH_64R2 0x80000000 - - -#define E_MIPS_ARCH_1 0x00000000 -#define E_MIPS_ARCH_2 0x10000000 -#define E_MIPS_ARCH_3 0x20000000 -#define E_MIPS_ARCH_4 0x30000000 -#define E_MIPS_ARCH_5 0x40000000 -#define E_MIPS_ARCH_32 0x50000000 -#define E_MIPS_ARCH_64 0x60000000 - - - -#define SHN_MIPS_ACOMMON 0xff00 -#define SHN_MIPS_TEXT 0xff01 -#define SHN_MIPS_DATA 0xff02 -#define SHN_MIPS_SCOMMON 0xff03 -#define SHN_MIPS_SUNDEFINED 0xff04 - - - -#define SHT_MIPS_LIBLIST 0x70000000 -#define SHT_MIPS_MSYM 0x70000001 -#define SHT_MIPS_CONFLICT 0x70000002 -#define SHT_MIPS_GPTAB 0x70000003 -#define SHT_MIPS_UCODE 0x70000004 -#define SHT_MIPS_DEBUG 0x70000005 -#define SHT_MIPS_REGINFO 0x70000006 -#define SHT_MIPS_PACKAGE 0x70000007 -#define SHT_MIPS_PACKSYM 0x70000008 -#define SHT_MIPS_RELD 0x70000009 -#define SHT_MIPS_IFACE 0x7000000b -#define SHT_MIPS_CONTENT 0x7000000c -#define SHT_MIPS_OPTIONS 0x7000000d -#define SHT_MIPS_SHDR 0x70000010 -#define SHT_MIPS_FDESC 0x70000011 -#define SHT_MIPS_EXTSYM 0x70000012 -#define SHT_MIPS_DENSE 0x70000013 -#define SHT_MIPS_PDESC 0x70000014 -#define SHT_MIPS_LOCSYM 0x70000015 -#define SHT_MIPS_AUXSYM 0x70000016 -#define SHT_MIPS_OPTSYM 0x70000017 -#define SHT_MIPS_LOCSTR 0x70000018 -#define SHT_MIPS_LINE 0x70000019 -#define SHT_MIPS_RFDESC 0x7000001a -#define SHT_MIPS_DELTASYM 0x7000001b -#define SHT_MIPS_DELTAINST 0x7000001c -#define SHT_MIPS_DELTACLASS 0x7000001d -#define SHT_MIPS_DWARF 0x7000001e -#define SHT_MIPS_DELTADECL 0x7000001f -#define SHT_MIPS_SYMBOL_LIB 0x70000020 -#define SHT_MIPS_EVENTS 0x70000021 -#define SHT_MIPS_TRANSLATE 0x70000022 -#define SHT_MIPS_PIXIE 0x70000023 -#define SHT_MIPS_XLATE 0x70000024 -#define SHT_MIPS_XLATE_DEBUG 0x70000025 -#define SHT_MIPS_WHIRL 0x70000026 -#define SHT_MIPS_EH_REGION 0x70000027 -#define SHT_MIPS_XLATE_OLD 0x70000028 -#define SHT_MIPS_PDR_EXCEPTION 0x70000029 - - - -#define SHF_MIPS_GPREL 0x10000000 -#define SHF_MIPS_MERGE 0x20000000 -#define SHF_MIPS_ADDR 0x40000000 -#define SHF_MIPS_STRINGS 0x80000000 -#define SHF_MIPS_NOSTRIP 0x08000000 -#define SHF_MIPS_LOCAL 0x04000000 -#define SHF_MIPS_NAMES 0x02000000 -#define SHF_MIPS_NODUPE 0x01000000 - - - - - -#define STO_MIPS_DEFAULT 0x0 -#define STO_MIPS_INTERNAL 0x1 -#define STO_MIPS_HIDDEN 0x2 -#define STO_MIPS_PROTECTED 0x3 -#define STO_MIPS_PLT 0x8 -#define STO_MIPS_SC_ALIGN_UNUSED 0xff - - -#define STB_MIPS_SPLIT_COMMON 13 - - - -typedef union { - struct { - Elf32_Word gt_current_g_value; - Elf32_Word gt_unused; - } gt_header; - struct { - Elf32_Word gt_g_value; - Elf32_Word gt_bytes; - } gt_entry; -} Elf32_gptab; - - - -typedef struct { - Elf32_Word ri_gprmask; - Elf32_Word ri_cprmask[4]; - Elf32_Sword ri_gp_value; -} Elf32_RegInfo; - - - -typedef struct { - unsigned char kind; - - unsigned char size; - Elf32_Section section; - - Elf32_Word info; -} Elf_Options; - - - -#define ODK_NULL 0 -#define ODK_REGINFO 1 -#define ODK_EXCEPTIONS 2 -#define ODK_PAD 3 -#define ODK_HWPATCH 4 -#define ODK_FILL 5 -#define ODK_TAGS 6 -#define ODK_HWAND 7 -#define ODK_HWOR 8 - - - -#define OEX_FPU_MIN 0x1f -#define OEX_FPU_MAX 0x1f00 -#define OEX_PAGE0 0x10000 -#define OEX_SMM 0x20000 -#define OEX_FPDBUG 0x40000 -#define OEX_PRECISEFP OEX_FPDBUG -#define OEX_DISMISS 0x80000 - -#define OEX_FPU_INVAL 0x10 -#define OEX_FPU_DIV0 0x08 -#define OEX_FPU_OFLO 0x04 -#define OEX_FPU_UFLO 0x02 -#define OEX_FPU_INEX 0x01 - - - -#define OHW_R4KEOP 0x1 -#define OHW_R8KPFETCH 0x2 -#define OHW_R5KEOP 0x4 -#define OHW_R5KCVTL 0x8 - -#define OPAD_PREFIX 0x1 -#define OPAD_POSTFIX 0x2 -#define OPAD_SYMBOL 0x4 - - - -typedef struct { - Elf32_Word hwp_flags1; - Elf32_Word hwp_flags2; -} Elf_Options_Hw; - - - -#define OHWA0_R4KEOP_CHECKED 0x00000001 -#define OHWA1_R4KEOP_CLEAN 0x00000002 - - - -#define R_MIPS_NONE 0 -#define R_MIPS_16 1 -#define R_MIPS_32 2 -#define R_MIPS_REL32 3 -#define R_MIPS_26 4 -#define R_MIPS_HI16 5 -#define R_MIPS_LO16 6 -#define R_MIPS_GPREL16 7 -#define R_MIPS_LITERAL 8 -#define R_MIPS_GOT16 9 -#define R_MIPS_PC16 10 -#define R_MIPS_CALL16 11 -#define R_MIPS_GPREL32 12 - -#define R_MIPS_SHIFT5 16 -#define R_MIPS_SHIFT6 17 -#define R_MIPS_64 18 -#define R_MIPS_GOT_DISP 19 -#define R_MIPS_GOT_PAGE 20 -#define R_MIPS_GOT_OFST 21 -#define R_MIPS_GOT_HI16 22 -#define R_MIPS_GOT_LO16 23 -#define R_MIPS_SUB 24 -#define R_MIPS_INSERT_A 25 -#define R_MIPS_INSERT_B 26 -#define R_MIPS_DELETE 27 -#define R_MIPS_HIGHER 28 -#define R_MIPS_HIGHEST 29 -#define R_MIPS_CALL_HI16 30 -#define R_MIPS_CALL_LO16 31 -#define R_MIPS_SCN_DISP 32 -#define R_MIPS_REL16 33 -#define R_MIPS_ADD_IMMEDIATE 34 -#define R_MIPS_PJUMP 35 -#define R_MIPS_RELGOT 36 -#define R_MIPS_JALR 37 -#define R_MIPS_TLS_DTPMOD32 38 -#define R_MIPS_TLS_DTPREL32 39 -#define R_MIPS_TLS_DTPMOD64 40 -#define R_MIPS_TLS_DTPREL64 41 -#define R_MIPS_TLS_GD 42 -#define R_MIPS_TLS_LDM 43 -#define R_MIPS_TLS_DTPREL_HI16 44 -#define R_MIPS_TLS_DTPREL_LO16 45 -#define R_MIPS_TLS_GOTTPREL 46 -#define R_MIPS_TLS_TPREL32 47 -#define R_MIPS_TLS_TPREL64 48 -#define R_MIPS_TLS_TPREL_HI16 49 -#define R_MIPS_TLS_TPREL_LO16 50 -#define R_MIPS_GLOB_DAT 51 -#define R_MIPS_COPY 126 -#define R_MIPS_JUMP_SLOT 127 - -#define R_MIPS_NUM 128 - - - -#define PT_MIPS_REGINFO 0x70000000 -#define PT_MIPS_RTPROC 0x70000001 -#define PT_MIPS_OPTIONS 0x70000002 -#define PT_MIPS_ABIFLAGS 0x70000003 - - - -#define PF_MIPS_LOCAL 0x10000000 - - - -#define DT_MIPS_RLD_VERSION 0x70000001 -#define DT_MIPS_TIME_STAMP 0x70000002 -#define DT_MIPS_ICHECKSUM 0x70000003 -#define DT_MIPS_IVERSION 0x70000004 -#define DT_MIPS_FLAGS 0x70000005 -#define DT_MIPS_BASE_ADDRESS 0x70000006 -#define DT_MIPS_MSYM 0x70000007 -#define DT_MIPS_CONFLICT 0x70000008 -#define DT_MIPS_LIBLIST 0x70000009 -#define DT_MIPS_LOCAL_GOTNO 0x7000000a -#define DT_MIPS_CONFLICTNO 0x7000000b -#define DT_MIPS_LIBLISTNO 0x70000010 -#define DT_MIPS_SYMTABNO 0x70000011 -#define DT_MIPS_UNREFEXTNO 0x70000012 -#define DT_MIPS_GOTSYM 0x70000013 -#define DT_MIPS_HIPAGENO 0x70000014 -#define DT_MIPS_RLD_MAP 0x70000016 -#define DT_MIPS_DELTA_CLASS 0x70000017 -#define DT_MIPS_DELTA_CLASS_NO 0x70000018 - -#define DT_MIPS_DELTA_INSTANCE 0x70000019 -#define DT_MIPS_DELTA_INSTANCE_NO 0x7000001a - -#define DT_MIPS_DELTA_RELOC 0x7000001b -#define DT_MIPS_DELTA_RELOC_NO 0x7000001c - -#define DT_MIPS_DELTA_SYM 0x7000001d - -#define DT_MIPS_DELTA_SYM_NO 0x7000001e - -#define DT_MIPS_DELTA_CLASSSYM 0x70000020 - -#define DT_MIPS_DELTA_CLASSSYM_NO 0x70000021 - -#define DT_MIPS_CXX_FLAGS 0x70000022 -#define DT_MIPS_PIXIE_INIT 0x70000023 -#define DT_MIPS_SYMBOL_LIB 0x70000024 -#define DT_MIPS_LOCALPAGE_GOTIDX 0x70000025 -#define DT_MIPS_LOCAL_GOTIDX 0x70000026 -#define DT_MIPS_HIDDEN_GOTIDX 0x70000027 -#define DT_MIPS_PROTECTED_GOTIDX 0x70000028 -#define DT_MIPS_OPTIONS 0x70000029 -#define DT_MIPS_INTERFACE 0x7000002a -#define DT_MIPS_DYNSTR_ALIGN 0x7000002b -#define DT_MIPS_INTERFACE_SIZE 0x7000002c -#define DT_MIPS_RLD_TEXT_RESOLVE_ADDR 0x7000002d - -#define DT_MIPS_PERF_SUFFIX 0x7000002e - -#define DT_MIPS_COMPACT_SIZE 0x7000002f -#define DT_MIPS_GP_VALUE 0x70000030 -#define DT_MIPS_AUX_DYNAMIC 0x70000031 - -#define DT_MIPS_PLTGOT 0x70000032 - -#define DT_MIPS_RWPLT 0x70000034 -#define DT_MIPS_RLD_MAP_REL 0x70000035 -#define DT_MIPS_NUM 0x36 - - - -#define RHF_NONE 0 -#define RHF_QUICKSTART (1 << 0) -#define RHF_NOTPOT (1 << 1) -#define RHF_NO_LIBRARY_REPLACEMENT (1 << 2) -#define RHF_NO_MOVE (1 << 3) -#define RHF_SGI_ONLY (1 << 4) -#define RHF_GUARANTEE_INIT (1 << 5) -#define RHF_DELTA_C_PLUS_PLUS (1 << 6) -#define RHF_GUARANTEE_START_INIT (1 << 7) -#define RHF_PIXIE (1 << 8) -#define RHF_DEFAULT_DELAY_LOAD (1 << 9) -#define RHF_REQUICKSTART (1 << 10) -#define RHF_REQUICKSTARTED (1 << 11) -#define RHF_CORD (1 << 12) -#define RHF_NO_UNRES_UNDEF (1 << 13) -#define RHF_RLD_ORDER_SAFE (1 << 14) - - - -typedef struct { - Elf32_Word l_name; - Elf32_Word l_time_stamp; - Elf32_Word l_checksum; - Elf32_Word l_version; - Elf32_Word l_flags; -} Elf32_Lib; - -typedef struct { - Elf64_Word l_name; - Elf64_Word l_time_stamp; - Elf64_Word l_checksum; - Elf64_Word l_version; - Elf64_Word l_flags; -} Elf64_Lib; - - - - -#define LL_NONE 0 -#define LL_EXACT_MATCH (1 << 0) -#define LL_IGNORE_INT_VER (1 << 1) -#define LL_REQUIRE_MINOR (1 << 2) -#define LL_EXPORTS (1 << 3) -#define LL_DELAY_LOAD (1 << 4) -#define LL_DELTA (1 << 5) - - - -typedef Elf32_Addr Elf32_Conflict; - -typedef struct { - Elf32_Half version; - unsigned char isa_level; - unsigned char isa_rev; - unsigned char gpr_size; - unsigned char cpr1_size; - unsigned char cpr2_size; - unsigned char fp_abi; - Elf32_Word isa_ext; - Elf32_Word ases; - Elf32_Word flags1; - Elf32_Word flags2; -} Elf_MIPS_ABIFlags_v0; - -#define MIPS_AFL_REG_NONE 0x00 -#define MIPS_AFL_REG_32 0x01 -#define MIPS_AFL_REG_64 0x02 -#define MIPS_AFL_REG_128 0x03 - -#define MIPS_AFL_ASE_DSP 0x00000001 -#define MIPS_AFL_ASE_DSPR2 0x00000002 -#define MIPS_AFL_ASE_EVA 0x00000004 -#define MIPS_AFL_ASE_MCU 0x00000008 -#define MIPS_AFL_ASE_MDMX 0x00000010 -#define MIPS_AFL_ASE_MIPS3D 0x00000020 -#define MIPS_AFL_ASE_MT 0x00000040 -#define MIPS_AFL_ASE_SMARTMIPS 0x00000080 -#define MIPS_AFL_ASE_VIRT 0x00000100 -#define MIPS_AFL_ASE_MSA 0x00000200 -#define MIPS_AFL_ASE_MIPS16 0x00000400 -#define MIPS_AFL_ASE_MICROMIPS 0x00000800 -#define MIPS_AFL_ASE_XPA 0x00001000 -#define MIPS_AFL_ASE_MASK 0x00001fff - -#define MIPS_AFL_EXT_XLR 1 -#define MIPS_AFL_EXT_OCTEON2 2 -#define MIPS_AFL_EXT_OCTEONP 3 -#define MIPS_AFL_EXT_LOONGSON_3A 4 -#define MIPS_AFL_EXT_OCTEON 5 -#define MIPS_AFL_EXT_5900 6 -#define MIPS_AFL_EXT_4650 7 -#define MIPS_AFL_EXT_4010 8 -#define MIPS_AFL_EXT_4100 9 -#define MIPS_AFL_EXT_3900 10 -#define MIPS_AFL_EXT_10000 11 -#define MIPS_AFL_EXT_SB1 12 -#define MIPS_AFL_EXT_4111 13 -#define MIPS_AFL_EXT_4120 14 -#define MIPS_AFL_EXT_5400 15 -#define MIPS_AFL_EXT_5500 16 -#define MIPS_AFL_EXT_LOONGSON_2E 17 -#define MIPS_AFL_EXT_LOONGSON_2F 18 - -#define MIPS_AFL_FLAGS1_ODDSPREG 1 - -enum -{ - Val_GNU_MIPS_ABI_FP_ANY = 0, - Val_GNU_MIPS_ABI_FP_DOUBLE = 1, - Val_GNU_MIPS_ABI_FP_SINGLE = 2, - Val_GNU_MIPS_ABI_FP_SOFT = 3, - Val_GNU_MIPS_ABI_FP_OLD_64 = 4, - Val_GNU_MIPS_ABI_FP_XX = 5, - Val_GNU_MIPS_ABI_FP_64 = 6, - Val_GNU_MIPS_ABI_FP_64A = 7, - Val_GNU_MIPS_ABI_FP_MAX = 7 -}; - - - - -#define EF_PARISC_TRAPNIL 0x00010000 -#define EF_PARISC_EXT 0x00020000 -#define EF_PARISC_LSB 0x00040000 -#define EF_PARISC_WIDE 0x00080000 -#define EF_PARISC_NO_KABP 0x00100000 - -#define EF_PARISC_LAZYSWAP 0x00400000 -#define EF_PARISC_ARCH 0x0000ffff - - - -#define EFA_PARISC_1_0 0x020b -#define EFA_PARISC_1_1 0x0210 -#define EFA_PARISC_2_0 0x0214 - - - -#define SHN_PARISC_ANSI_COMMON 0xff00 - -#define SHN_PARISC_HUGE_COMMON 0xff01 - - - -#define SHT_PARISC_EXT 0x70000000 -#define SHT_PARISC_UNWIND 0x70000001 -#define SHT_PARISC_DOC 0x70000002 - - - -#define SHF_PARISC_SHORT 0x20000000 -#define SHF_PARISC_HUGE 0x40000000 -#define SHF_PARISC_SBP 0x80000000 - - - -#define STT_PARISC_MILLICODE 13 - -#define STT_HP_OPAQUE (STT_LOOS + 0x1) -#define STT_HP_STUB (STT_LOOS + 0x2) - - - -#define R_PARISC_NONE 0 -#define R_PARISC_DIR32 1 -#define R_PARISC_DIR21L 2 -#define R_PARISC_DIR17R 3 -#define R_PARISC_DIR17F 4 -#define R_PARISC_DIR14R 6 -#define R_PARISC_PCREL32 9 -#define R_PARISC_PCREL21L 10 -#define R_PARISC_PCREL17R 11 -#define R_PARISC_PCREL17F 12 -#define R_PARISC_PCREL14R 14 -#define R_PARISC_DPREL21L 18 -#define R_PARISC_DPREL14R 22 -#define R_PARISC_GPREL21L 26 -#define R_PARISC_GPREL14R 30 -#define R_PARISC_LTOFF21L 34 -#define R_PARISC_LTOFF14R 38 -#define R_PARISC_SECREL32 41 -#define R_PARISC_SEGBASE 48 -#define R_PARISC_SEGREL32 49 -#define R_PARISC_PLTOFF21L 50 -#define R_PARISC_PLTOFF14R 54 -#define R_PARISC_LTOFF_FPTR32 57 -#define R_PARISC_LTOFF_FPTR21L 58 -#define R_PARISC_LTOFF_FPTR14R 62 -#define R_PARISC_FPTR64 64 -#define R_PARISC_PLABEL32 65 -#define R_PARISC_PLABEL21L 66 -#define R_PARISC_PLABEL14R 70 -#define R_PARISC_PCREL64 72 -#define R_PARISC_PCREL22F 74 -#define R_PARISC_PCREL14WR 75 -#define R_PARISC_PCREL14DR 76 -#define R_PARISC_PCREL16F 77 -#define R_PARISC_PCREL16WF 78 -#define R_PARISC_PCREL16DF 79 -#define R_PARISC_DIR64 80 -#define R_PARISC_DIR14WR 83 -#define R_PARISC_DIR14DR 84 -#define R_PARISC_DIR16F 85 -#define R_PARISC_DIR16WF 86 -#define R_PARISC_DIR16DF 87 -#define R_PARISC_GPREL64 88 -#define R_PARISC_GPREL14WR 91 -#define R_PARISC_GPREL14DR 92 -#define R_PARISC_GPREL16F 93 -#define R_PARISC_GPREL16WF 94 -#define R_PARISC_GPREL16DF 95 -#define R_PARISC_LTOFF64 96 -#define R_PARISC_LTOFF14WR 99 -#define R_PARISC_LTOFF14DR 100 -#define R_PARISC_LTOFF16F 101 -#define R_PARISC_LTOFF16WF 102 -#define R_PARISC_LTOFF16DF 103 -#define R_PARISC_SECREL64 104 -#define R_PARISC_SEGREL64 112 -#define R_PARISC_PLTOFF14WR 115 -#define R_PARISC_PLTOFF14DR 116 -#define R_PARISC_PLTOFF16F 117 -#define R_PARISC_PLTOFF16WF 118 -#define R_PARISC_PLTOFF16DF 119 -#define R_PARISC_LTOFF_FPTR64 120 -#define R_PARISC_LTOFF_FPTR14WR 123 -#define R_PARISC_LTOFF_FPTR14DR 124 -#define R_PARISC_LTOFF_FPTR16F 125 -#define R_PARISC_LTOFF_FPTR16WF 126 -#define R_PARISC_LTOFF_FPTR16DF 127 -#define R_PARISC_LORESERVE 128 -#define R_PARISC_COPY 128 -#define R_PARISC_IPLT 129 -#define R_PARISC_EPLT 130 -#define R_PARISC_TPREL32 153 -#define R_PARISC_TPREL21L 154 -#define R_PARISC_TPREL14R 158 -#define R_PARISC_LTOFF_TP21L 162 -#define R_PARISC_LTOFF_TP14R 166 -#define R_PARISC_LTOFF_TP14F 167 -#define R_PARISC_TPREL64 216 -#define R_PARISC_TPREL14WR 219 -#define R_PARISC_TPREL14DR 220 -#define R_PARISC_TPREL16F 221 -#define R_PARISC_TPREL16WF 222 -#define R_PARISC_TPREL16DF 223 -#define R_PARISC_LTOFF_TP64 224 -#define R_PARISC_LTOFF_TP14WR 227 -#define R_PARISC_LTOFF_TP14DR 228 -#define R_PARISC_LTOFF_TP16F 229 -#define R_PARISC_LTOFF_TP16WF 230 -#define R_PARISC_LTOFF_TP16DF 231 -#define R_PARISC_GNU_VTENTRY 232 -#define R_PARISC_GNU_VTINHERIT 233 -#define R_PARISC_TLS_GD21L 234 -#define R_PARISC_TLS_GD14R 235 -#define R_PARISC_TLS_GDCALL 236 -#define R_PARISC_TLS_LDM21L 237 -#define R_PARISC_TLS_LDM14R 238 -#define R_PARISC_TLS_LDMCALL 239 -#define R_PARISC_TLS_LDO21L 240 -#define R_PARISC_TLS_LDO14R 241 -#define R_PARISC_TLS_DTPMOD32 242 -#define R_PARISC_TLS_DTPMOD64 243 -#define R_PARISC_TLS_DTPOFF32 244 -#define R_PARISC_TLS_DTPOFF64 245 -#define R_PARISC_TLS_LE21L R_PARISC_TPREL21L -#define R_PARISC_TLS_LE14R R_PARISC_TPREL14R -#define R_PARISC_TLS_IE21L R_PARISC_LTOFF_TP21L -#define R_PARISC_TLS_IE14R R_PARISC_LTOFF_TP14R -#define R_PARISC_TLS_TPREL32 R_PARISC_TPREL32 -#define R_PARISC_TLS_TPREL64 R_PARISC_TPREL64 -#define R_PARISC_HIRESERVE 255 - - - -#define PT_HP_TLS (PT_LOOS + 0x0) -#define PT_HP_CORE_NONE (PT_LOOS + 0x1) -#define PT_HP_CORE_VERSION (PT_LOOS + 0x2) -#define PT_HP_CORE_KERNEL (PT_LOOS + 0x3) -#define PT_HP_CORE_COMM (PT_LOOS + 0x4) -#define PT_HP_CORE_PROC (PT_LOOS + 0x5) -#define PT_HP_CORE_LOADABLE (PT_LOOS + 0x6) -#define PT_HP_CORE_STACK (PT_LOOS + 0x7) -#define PT_HP_CORE_SHM (PT_LOOS + 0x8) -#define PT_HP_CORE_MMF (PT_LOOS + 0x9) -#define PT_HP_PARALLEL (PT_LOOS + 0x10) -#define PT_HP_FASTBIND (PT_LOOS + 0x11) -#define PT_HP_OPT_ANNOT (PT_LOOS + 0x12) -#define PT_HP_HSL_ANNOT (PT_LOOS + 0x13) -#define PT_HP_STACK (PT_LOOS + 0x14) - -#define PT_PARISC_ARCHEXT 0x70000000 -#define PT_PARISC_UNWIND 0x70000001 - - - -#define PF_PARISC_SBP 0x08000000 - -#define PF_HP_PAGE_SIZE 0x00100000 -#define PF_HP_FAR_SHARED 0x00200000 -#define PF_HP_NEAR_SHARED 0x00400000 -#define PF_HP_CODE 0x01000000 -#define PF_HP_MODIFY 0x02000000 -#define PF_HP_LAZYSWAP 0x04000000 -#define PF_HP_SBP 0x08000000 - - - - - - -#define EF_ALPHA_32BIT 1 -#define EF_ALPHA_CANRELAX 2 - - - - -#define SHT_ALPHA_DEBUG 0x70000001 -#define SHT_ALPHA_REGINFO 0x70000002 - - - -#define SHF_ALPHA_GPREL 0x10000000 - - -#define STO_ALPHA_NOPV 0x80 -#define STO_ALPHA_STD_GPLOAD 0x88 - - - -#define R_ALPHA_NONE 0 -#define R_ALPHA_REFLONG 1 -#define R_ALPHA_REFQUAD 2 -#define R_ALPHA_GPREL32 3 -#define R_ALPHA_LITERAL 4 -#define R_ALPHA_LITUSE 5 -#define R_ALPHA_GPDISP 6 -#define R_ALPHA_BRADDR 7 -#define R_ALPHA_HINT 8 -#define R_ALPHA_SREL16 9 -#define R_ALPHA_SREL32 10 -#define R_ALPHA_SREL64 11 -#define R_ALPHA_GPRELHIGH 17 -#define R_ALPHA_GPRELLOW 18 -#define R_ALPHA_GPREL16 19 -#define R_ALPHA_COPY 24 -#define R_ALPHA_GLOB_DAT 25 -#define R_ALPHA_JMP_SLOT 26 -#define R_ALPHA_RELATIVE 27 -#define R_ALPHA_TLS_GD_HI 28 -#define R_ALPHA_TLSGD 29 -#define R_ALPHA_TLS_LDM 30 -#define R_ALPHA_DTPMOD64 31 -#define R_ALPHA_GOTDTPREL 32 -#define R_ALPHA_DTPREL64 33 -#define R_ALPHA_DTPRELHI 34 -#define R_ALPHA_DTPRELLO 35 -#define R_ALPHA_DTPREL16 36 -#define R_ALPHA_GOTTPREL 37 -#define R_ALPHA_TPREL64 38 -#define R_ALPHA_TPRELHI 39 -#define R_ALPHA_TPRELLO 40 -#define R_ALPHA_TPREL16 41 - -#define R_ALPHA_NUM 46 - - -#define LITUSE_ALPHA_ADDR 0 -#define LITUSE_ALPHA_BASE 1 -#define LITUSE_ALPHA_BYTOFF 2 -#define LITUSE_ALPHA_JSR 3 -#define LITUSE_ALPHA_TLS_GD 4 -#define LITUSE_ALPHA_TLS_LDM 5 - - -#define DT_ALPHA_PLTRO (DT_LOPROC + 0) -#define DT_ALPHA_NUM 1 - - - - -#define EF_PPC_EMB 0x80000000 - - -#define EF_PPC_RELOCATABLE 0x00010000 -#define EF_PPC_RELOCATABLE_LIB 0x00008000 - - - -#define R_PPC_NONE 0 -#define R_PPC_ADDR32 1 -#define R_PPC_ADDR24 2 -#define R_PPC_ADDR16 3 -#define R_PPC_ADDR16_LO 4 -#define R_PPC_ADDR16_HI 5 -#define R_PPC_ADDR16_HA 6 -#define R_PPC_ADDR14 7 -#define R_PPC_ADDR14_BRTAKEN 8 -#define R_PPC_ADDR14_BRNTAKEN 9 -#define R_PPC_REL24 10 -#define R_PPC_REL14 11 -#define R_PPC_REL14_BRTAKEN 12 -#define R_PPC_REL14_BRNTAKEN 13 -#define R_PPC_GOT16 14 -#define R_PPC_GOT16_LO 15 -#define R_PPC_GOT16_HI 16 -#define R_PPC_GOT16_HA 17 -#define R_PPC_PLTREL24 18 -#define R_PPC_COPY 19 -#define R_PPC_GLOB_DAT 20 -#define R_PPC_JMP_SLOT 21 -#define R_PPC_RELATIVE 22 -#define R_PPC_LOCAL24PC 23 -#define R_PPC_UADDR32 24 -#define R_PPC_UADDR16 25 -#define R_PPC_REL32 26 -#define R_PPC_PLT32 27 -#define R_PPC_PLTREL32 28 -#define R_PPC_PLT16_LO 29 -#define R_PPC_PLT16_HI 30 -#define R_PPC_PLT16_HA 31 -#define R_PPC_SDAREL16 32 -#define R_PPC_SECTOFF 33 -#define R_PPC_SECTOFF_LO 34 -#define R_PPC_SECTOFF_HI 35 -#define R_PPC_SECTOFF_HA 36 - - -#define R_PPC_TLS 67 -#define R_PPC_DTPMOD32 68 -#define R_PPC_TPREL16 69 -#define R_PPC_TPREL16_LO 70 -#define R_PPC_TPREL16_HI 71 -#define R_PPC_TPREL16_HA 72 -#define R_PPC_TPREL32 73 -#define R_PPC_DTPREL16 74 -#define R_PPC_DTPREL16_LO 75 -#define R_PPC_DTPREL16_HI 76 -#define R_PPC_DTPREL16_HA 77 -#define R_PPC_DTPREL32 78 -#define R_PPC_GOT_TLSGD16 79 -#define R_PPC_GOT_TLSGD16_LO 80 -#define R_PPC_GOT_TLSGD16_HI 81 -#define R_PPC_GOT_TLSGD16_HA 82 -#define R_PPC_GOT_TLSLD16 83 -#define R_PPC_GOT_TLSLD16_LO 84 -#define R_PPC_GOT_TLSLD16_HI 85 -#define R_PPC_GOT_TLSLD16_HA 86 -#define R_PPC_GOT_TPREL16 87 -#define R_PPC_GOT_TPREL16_LO 88 -#define R_PPC_GOT_TPREL16_HI 89 -#define R_PPC_GOT_TPREL16_HA 90 -#define R_PPC_GOT_DTPREL16 91 -#define R_PPC_GOT_DTPREL16_LO 92 -#define R_PPC_GOT_DTPREL16_HI 93 -#define R_PPC_GOT_DTPREL16_HA 94 -#define R_PPC_TLSGD 95 -#define R_PPC_TLSLD 96 - - -#define R_PPC_EMB_NADDR32 101 -#define R_PPC_EMB_NADDR16 102 -#define R_PPC_EMB_NADDR16_LO 103 -#define R_PPC_EMB_NADDR16_HI 104 -#define R_PPC_EMB_NADDR16_HA 105 -#define R_PPC_EMB_SDAI16 106 -#define R_PPC_EMB_SDA2I16 107 -#define R_PPC_EMB_SDA2REL 108 -#define R_PPC_EMB_SDA21 109 -#define R_PPC_EMB_MRKREF 110 -#define R_PPC_EMB_RELSEC16 111 -#define R_PPC_EMB_RELST_LO 112 -#define R_PPC_EMB_RELST_HI 113 -#define R_PPC_EMB_RELST_HA 114 -#define R_PPC_EMB_BIT_FLD 115 -#define R_PPC_EMB_RELSDA 116 - - -#define R_PPC_DIAB_SDA21_LO 180 -#define R_PPC_DIAB_SDA21_HI 181 -#define R_PPC_DIAB_SDA21_HA 182 -#define R_PPC_DIAB_RELSDA_LO 183 -#define R_PPC_DIAB_RELSDA_HI 184 -#define R_PPC_DIAB_RELSDA_HA 185 - - -#define R_PPC_IRELATIVE 248 - - -#define R_PPC_REL16 249 -#define R_PPC_REL16_LO 250 -#define R_PPC_REL16_HI 251 -#define R_PPC_REL16_HA 252 - - - -#define R_PPC_TOC16 255 - - -#define DT_PPC_GOT (DT_LOPROC + 0) -#define DT_PPC_OPT (DT_LOPROC + 1) -#define DT_PPC_NUM 2 - -#define PPC_OPT_TLS 1 - - -#define R_PPC64_NONE R_PPC_NONE -#define R_PPC64_ADDR32 R_PPC_ADDR32 -#define R_PPC64_ADDR24 R_PPC_ADDR24 -#define R_PPC64_ADDR16 R_PPC_ADDR16 -#define R_PPC64_ADDR16_LO R_PPC_ADDR16_LO -#define R_PPC64_ADDR16_HI R_PPC_ADDR16_HI -#define R_PPC64_ADDR16_HA R_PPC_ADDR16_HA -#define R_PPC64_ADDR14 R_PPC_ADDR14 -#define R_PPC64_ADDR14_BRTAKEN R_PPC_ADDR14_BRTAKEN -#define R_PPC64_ADDR14_BRNTAKEN R_PPC_ADDR14_BRNTAKEN -#define R_PPC64_REL24 R_PPC_REL24 -#define R_PPC64_REL14 R_PPC_REL14 -#define R_PPC64_REL14_BRTAKEN R_PPC_REL14_BRTAKEN -#define R_PPC64_REL14_BRNTAKEN R_PPC_REL14_BRNTAKEN -#define R_PPC64_GOT16 R_PPC_GOT16 -#define R_PPC64_GOT16_LO R_PPC_GOT16_LO -#define R_PPC64_GOT16_HI R_PPC_GOT16_HI -#define R_PPC64_GOT16_HA R_PPC_GOT16_HA - -#define R_PPC64_COPY R_PPC_COPY -#define R_PPC64_GLOB_DAT R_PPC_GLOB_DAT -#define R_PPC64_JMP_SLOT R_PPC_JMP_SLOT -#define R_PPC64_RELATIVE R_PPC_RELATIVE - -#define R_PPC64_UADDR32 R_PPC_UADDR32 -#define R_PPC64_UADDR16 R_PPC_UADDR16 -#define R_PPC64_REL32 R_PPC_REL32 -#define R_PPC64_PLT32 R_PPC_PLT32 -#define R_PPC64_PLTREL32 R_PPC_PLTREL32 -#define R_PPC64_PLT16_LO R_PPC_PLT16_LO -#define R_PPC64_PLT16_HI R_PPC_PLT16_HI -#define R_PPC64_PLT16_HA R_PPC_PLT16_HA - -#define R_PPC64_SECTOFF R_PPC_SECTOFF -#define R_PPC64_SECTOFF_LO R_PPC_SECTOFF_LO -#define R_PPC64_SECTOFF_HI R_PPC_SECTOFF_HI -#define R_PPC64_SECTOFF_HA R_PPC_SECTOFF_HA -#define R_PPC64_ADDR30 37 -#define R_PPC64_ADDR64 38 -#define R_PPC64_ADDR16_HIGHER 39 -#define R_PPC64_ADDR16_HIGHERA 40 -#define R_PPC64_ADDR16_HIGHEST 41 -#define R_PPC64_ADDR16_HIGHESTA 42 -#define R_PPC64_UADDR64 43 -#define R_PPC64_REL64 44 -#define R_PPC64_PLT64 45 -#define R_PPC64_PLTREL64 46 -#define R_PPC64_TOC16 47 -#define R_PPC64_TOC16_LO 48 -#define R_PPC64_TOC16_HI 49 -#define R_PPC64_TOC16_HA 50 -#define R_PPC64_TOC 51 -#define R_PPC64_PLTGOT16 52 -#define R_PPC64_PLTGOT16_LO 53 -#define R_PPC64_PLTGOT16_HI 54 -#define R_PPC64_PLTGOT16_HA 55 - -#define R_PPC64_ADDR16_DS 56 -#define R_PPC64_ADDR16_LO_DS 57 -#define R_PPC64_GOT16_DS 58 -#define R_PPC64_GOT16_LO_DS 59 -#define R_PPC64_PLT16_LO_DS 60 -#define R_PPC64_SECTOFF_DS 61 -#define R_PPC64_SECTOFF_LO_DS 62 -#define R_PPC64_TOC16_DS 63 -#define R_PPC64_TOC16_LO_DS 64 -#define R_PPC64_PLTGOT16_DS 65 -#define R_PPC64_PLTGOT16_LO_DS 66 - - -#define R_PPC64_TLS 67 -#define R_PPC64_DTPMOD64 68 -#define R_PPC64_TPREL16 69 -#define R_PPC64_TPREL16_LO 70 -#define R_PPC64_TPREL16_HI 71 -#define R_PPC64_TPREL16_HA 72 -#define R_PPC64_TPREL64 73 -#define R_PPC64_DTPREL16 74 -#define R_PPC64_DTPREL16_LO 75 -#define R_PPC64_DTPREL16_HI 76 -#define R_PPC64_DTPREL16_HA 77 -#define R_PPC64_DTPREL64 78 -#define R_PPC64_GOT_TLSGD16 79 -#define R_PPC64_GOT_TLSGD16_LO 80 -#define R_PPC64_GOT_TLSGD16_HI 81 -#define R_PPC64_GOT_TLSGD16_HA 82 -#define R_PPC64_GOT_TLSLD16 83 -#define R_PPC64_GOT_TLSLD16_LO 84 -#define R_PPC64_GOT_TLSLD16_HI 85 -#define R_PPC64_GOT_TLSLD16_HA 86 -#define R_PPC64_GOT_TPREL16_DS 87 -#define R_PPC64_GOT_TPREL16_LO_DS 88 -#define R_PPC64_GOT_TPREL16_HI 89 -#define R_PPC64_GOT_TPREL16_HA 90 -#define R_PPC64_GOT_DTPREL16_DS 91 -#define R_PPC64_GOT_DTPREL16_LO_DS 92 -#define R_PPC64_GOT_DTPREL16_HI 93 -#define R_PPC64_GOT_DTPREL16_HA 94 -#define R_PPC64_TPREL16_DS 95 -#define R_PPC64_TPREL16_LO_DS 96 -#define R_PPC64_TPREL16_HIGHER 97 -#define R_PPC64_TPREL16_HIGHERA 98 -#define R_PPC64_TPREL16_HIGHEST 99 -#define R_PPC64_TPREL16_HIGHESTA 100 -#define R_PPC64_DTPREL16_DS 101 -#define R_PPC64_DTPREL16_LO_DS 102 -#define R_PPC64_DTPREL16_HIGHER 103 -#define R_PPC64_DTPREL16_HIGHERA 104 -#define R_PPC64_DTPREL16_HIGHEST 105 -#define R_PPC64_DTPREL16_HIGHESTA 106 -#define R_PPC64_TLSGD 107 -#define R_PPC64_TLSLD 108 -#define R_PPC64_TOCSAVE 109 -#define R_PPC64_ADDR16_HIGH 110 -#define R_PPC64_ADDR16_HIGHA 111 -#define R_PPC64_TPREL16_HIGH 112 -#define R_PPC64_TPREL16_HIGHA 113 -#define R_PPC64_DTPREL16_HIGH 114 -#define R_PPC64_DTPREL16_HIGHA 115 - - -#define R_PPC64_JMP_IREL 247 -#define R_PPC64_IRELATIVE 248 -#define R_PPC64_REL16 249 -#define R_PPC64_REL16_LO 250 -#define R_PPC64_REL16_HI 251 -#define R_PPC64_REL16_HA 252 - -#define EF_PPC64_ABI 3 - -#define DT_PPC64_GLINK (DT_LOPROC + 0) -#define DT_PPC64_OPD (DT_LOPROC + 1) -#define DT_PPC64_OPDSZ (DT_LOPROC + 2) -#define DT_PPC64_OPT (DT_LOPROC + 3) -#define DT_PPC64_NUM 4 - -#define PPC64_OPT_TLS 1 -#define PPC64_OPT_MULTI_TOC 2 -#define PPC64_OPT_LOCALENTRY 4 - -#define STO_PPC64_LOCAL_BIT 5 -#define STO_PPC64_LOCAL_MASK 0xe0 -#define PPC64_LOCAL_ENTRY_OFFSET(x) (1 << (((x)&0xe0)>>5) & 0xfc) - - -#define EF_ARM_RELEXEC 0x01 -#define EF_ARM_HASENTRY 0x02 -#define EF_ARM_INTERWORK 0x04 -#define EF_ARM_APCS_26 0x08 -#define EF_ARM_APCS_FLOAT 0x10 -#define EF_ARM_PIC 0x20 -#define EF_ARM_ALIGN8 0x40 -#define EF_ARM_NEW_ABI 0x80 -#define EF_ARM_OLD_ABI 0x100 -#define EF_ARM_SOFT_FLOAT 0x200 -#define EF_ARM_VFP_FLOAT 0x400 -#define EF_ARM_MAVERICK_FLOAT 0x800 - -#define EF_ARM_ABI_FLOAT_SOFT 0x200 -#define EF_ARM_ABI_FLOAT_HARD 0x400 - - -#define EF_ARM_SYMSARESORTED 0x04 -#define EF_ARM_DYNSYMSUSESEGIDX 0x08 -#define EF_ARM_MAPSYMSFIRST 0x10 -#define EF_ARM_EABIMASK 0XFF000000 - - -#define EF_ARM_BE8 0x00800000 -#define EF_ARM_LE8 0x00400000 - -#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK) -#define EF_ARM_EABI_UNKNOWN 0x00000000 -#define EF_ARM_EABI_VER1 0x01000000 -#define EF_ARM_EABI_VER2 0x02000000 -#define EF_ARM_EABI_VER3 0x03000000 -#define EF_ARM_EABI_VER4 0x04000000 -#define EF_ARM_EABI_VER5 0x05000000 - - -#define STT_ARM_TFUNC STT_LOPROC -#define STT_ARM_16BIT STT_HIPROC - - -#define SHF_ARM_ENTRYSECT 0x10000000 -#define SHF_ARM_COMDEF 0x80000000 - - - -#define PF_ARM_SB 0x10000000 - -#define PF_ARM_PI 0x20000000 -#define PF_ARM_ABS 0x40000000 - - -#define PT_ARM_EXIDX (PT_LOPROC + 1) - - -#define SHT_ARM_EXIDX (SHT_LOPROC + 1) -#define SHT_ARM_PREEMPTMAP (SHT_LOPROC + 2) -#define SHT_ARM_ATTRIBUTES (SHT_LOPROC + 3) - -#define R_AARCH64_NONE 0 -#define R_AARCH64_P32_ABS32 1 -#define R_AARCH64_P32_COPY 180 -#define R_AARCH64_P32_GLOB_DAT 181 -#define R_AARCH64_P32_JUMP_SLOT 182 -#define R_AARCH64_P32_RELATIVE 183 -#define R_AARCH64_P32_TLS_DTPMOD 184 -#define R_AARCH64_P32_TLS_DTPREL 185 -#define R_AARCH64_P32_TLS_TPREL 186 -#define R_AARCH64_P32_TLSDESC 187 -#define R_AARCH64_P32_IRELATIVE 188 -#define R_AARCH64_ABS64 257 -#define R_AARCH64_ABS32 258 -#define R_AARCH64_ABS16 259 -#define R_AARCH64_PREL64 260 -#define R_AARCH64_PREL32 261 -#define R_AARCH64_PREL16 262 -#define R_AARCH64_MOVW_UABS_G0 263 -#define R_AARCH64_MOVW_UABS_G0_NC 264 -#define R_AARCH64_MOVW_UABS_G1 265 -#define R_AARCH64_MOVW_UABS_G1_NC 266 -#define R_AARCH64_MOVW_UABS_G2 267 -#define R_AARCH64_MOVW_UABS_G2_NC 268 -#define R_AARCH64_MOVW_UABS_G3 269 -#define R_AARCH64_MOVW_SABS_G0 270 -#define R_AARCH64_MOVW_SABS_G1 271 -#define R_AARCH64_MOVW_SABS_G2 272 -#define R_AARCH64_LD_PREL_LO19 273 -#define R_AARCH64_ADR_PREL_LO21 274 -#define R_AARCH64_ADR_PREL_PG_HI21 275 -#define R_AARCH64_ADR_PREL_PG_HI21_NC 276 -#define R_AARCH64_ADD_ABS_LO12_NC 277 -#define R_AARCH64_LDST8_ABS_LO12_NC 278 -#define R_AARCH64_TSTBR14 279 -#define R_AARCH64_CONDBR19 280 -#define R_AARCH64_JUMP26 282 -#define R_AARCH64_CALL26 283 -#define R_AARCH64_LDST16_ABS_LO12_NC 284 -#define R_AARCH64_LDST32_ABS_LO12_NC 285 -#define R_AARCH64_LDST64_ABS_LO12_NC 286 -#define R_AARCH64_MOVW_PREL_G0 287 -#define R_AARCH64_MOVW_PREL_G0_NC 288 -#define R_AARCH64_MOVW_PREL_G1 289 -#define R_AARCH64_MOVW_PREL_G1_NC 290 -#define R_AARCH64_MOVW_PREL_G2 291 -#define R_AARCH64_MOVW_PREL_G2_NC 292 -#define R_AARCH64_MOVW_PREL_G3 293 -#define R_AARCH64_LDST128_ABS_LO12_NC 299 -#define R_AARCH64_MOVW_GOTOFF_G0 300 -#define R_AARCH64_MOVW_GOTOFF_G0_NC 301 -#define R_AARCH64_MOVW_GOTOFF_G1 302 -#define R_AARCH64_MOVW_GOTOFF_G1_NC 303 -#define R_AARCH64_MOVW_GOTOFF_G2 304 -#define R_AARCH64_MOVW_GOTOFF_G2_NC 305 -#define R_AARCH64_MOVW_GOTOFF_G3 306 -#define R_AARCH64_GOTREL64 307 -#define R_AARCH64_GOTREL32 308 -#define R_AARCH64_GOT_LD_PREL19 309 -#define R_AARCH64_LD64_GOTOFF_LO15 310 -#define R_AARCH64_ADR_GOT_PAGE 311 -#define R_AARCH64_LD64_GOT_LO12_NC 312 -#define R_AARCH64_LD64_GOTPAGE_LO15 313 -#define R_AARCH64_TLSGD_ADR_PREL21 512 -#define R_AARCH64_TLSGD_ADR_PAGE21 513 -#define R_AARCH64_TLSGD_ADD_LO12_NC 514 -#define R_AARCH64_TLSGD_MOVW_G1 515 -#define R_AARCH64_TLSGD_MOVW_G0_NC 516 -#define R_AARCH64_TLSLD_ADR_PREL21 517 -#define R_AARCH64_TLSLD_ADR_PAGE21 518 -#define R_AARCH64_TLSLD_ADD_LO12_NC 519 -#define R_AARCH64_TLSLD_MOVW_G1 520 -#define R_AARCH64_TLSLD_MOVW_G0_NC 521 -#define R_AARCH64_TLSLD_LD_PREL19 522 -#define R_AARCH64_TLSLD_MOVW_DTPREL_G2 523 -#define R_AARCH64_TLSLD_MOVW_DTPREL_G1 524 -#define R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC 525 -#define R_AARCH64_TLSLD_MOVW_DTPREL_G0 526 -#define R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC 527 -#define R_AARCH64_TLSLD_ADD_DTPREL_HI12 528 -#define R_AARCH64_TLSLD_ADD_DTPREL_LO12 529 -#define R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC 530 -#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12 531 -#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC 532 -#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12 533 -#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC 534 -#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12 535 -#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC 536 -#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12 537 -#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC 538 -#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G1 539 -#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC 540 -#define R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 541 -#define R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC 542 -#define R_AARCH64_TLSIE_LD_GOTTPREL_PREL19 543 -#define R_AARCH64_TLSLE_MOVW_TPREL_G2 544 -#define R_AARCH64_TLSLE_MOVW_TPREL_G1 545 -#define R_AARCH64_TLSLE_MOVW_TPREL_G1_NC 546 -#define R_AARCH64_TLSLE_MOVW_TPREL_G0 547 -#define R_AARCH64_TLSLE_MOVW_TPREL_G0_NC 548 -#define R_AARCH64_TLSLE_ADD_TPREL_HI12 549 -#define R_AARCH64_TLSLE_ADD_TPREL_LO12 550 -#define R_AARCH64_TLSLE_ADD_TPREL_LO12_NC 551 -#define R_AARCH64_TLSLE_LDST8_TPREL_LO12 552 -#define R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC 553 -#define R_AARCH64_TLSLE_LDST16_TPREL_LO12 554 -#define R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC 555 -#define R_AARCH64_TLSLE_LDST32_TPREL_LO12 556 -#define R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC 557 -#define R_AARCH64_TLSLE_LDST64_TPREL_LO12 558 -#define R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC 559 -#define R_AARCH64_TLSDESC_LD_PREL19 560 -#define R_AARCH64_TLSDESC_ADR_PREL21 561 -#define R_AARCH64_TLSDESC_ADR_PAGE21 562 -#define R_AARCH64_TLSDESC_LD64_LO12 563 -#define R_AARCH64_TLSDESC_ADD_LO12 564 -#define R_AARCH64_TLSDESC_OFF_G1 565 -#define R_AARCH64_TLSDESC_OFF_G0_NC 566 -#define R_AARCH64_TLSDESC_LDR 567 -#define R_AARCH64_TLSDESC_ADD 568 -#define R_AARCH64_TLSDESC_CALL 569 -#define R_AARCH64_TLSLE_LDST128_TPREL_LO12 570 -#define R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC 571 -#define R_AARCH64_TLSLD_LDST128_DTPREL_LO12 572 -#define R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC 573 -#define R_AARCH64_COPY 1024 -#define R_AARCH64_GLOB_DAT 1025 -#define R_AARCH64_JUMP_SLOT 1026 -#define R_AARCH64_RELATIVE 1027 -#define R_AARCH64_TLS_DTPMOD 1028 -#define R_AARCH64_TLS_DTPMOD64 1028 -#define R_AARCH64_TLS_DTPREL 1029 -#define R_AARCH64_TLS_DTPREL64 1029 -#define R_AARCH64_TLS_TPREL 1030 -#define R_AARCH64_TLS_TPREL64 1030 -#define R_AARCH64_TLSDESC 1031 - - -#define R_ARM_NONE 0 -#define R_ARM_PC24 1 -#define R_ARM_ABS32 2 -#define R_ARM_REL32 3 -#define R_ARM_PC13 4 -#define R_ARM_ABS16 5 -#define R_ARM_ABS12 6 -#define R_ARM_THM_ABS5 7 -#define R_ARM_ABS8 8 -#define R_ARM_SBREL32 9 -#define R_ARM_THM_PC22 10 -#define R_ARM_THM_PC8 11 -#define R_ARM_AMP_VCALL9 12 -#define R_ARM_TLS_DESC 13 -#define R_ARM_THM_SWI8 14 -#define R_ARM_XPC25 15 -#define R_ARM_THM_XPC22 16 -#define R_ARM_TLS_DTPMOD32 17 -#define R_ARM_TLS_DTPOFF32 18 -#define R_ARM_TLS_TPOFF32 19 -#define R_ARM_COPY 20 -#define R_ARM_GLOB_DAT 21 -#define R_ARM_JUMP_SLOT 22 -#define R_ARM_RELATIVE 23 -#define R_ARM_GOTOFF 24 -#define R_ARM_GOTPC 25 -#define R_ARM_GOT32 26 -#define R_ARM_PLT32 27 -#define R_ARM_CALL 28 -#define R_ARM_JUMP24 29 -#define R_ARM_THM_JUMP24 30 -#define R_ARM_BASE_ABS 31 -#define R_ARM_ALU_PCREL_7_0 32 -#define R_ARM_ALU_PCREL_15_8 33 -#define R_ARM_ALU_PCREL_23_15 34 -#define R_ARM_LDR_SBREL_11_0 35 -#define R_ARM_ALU_SBREL_19_12 36 -#define R_ARM_ALU_SBREL_27_20 37 -#define R_ARM_TARGET1 38 -#define R_ARM_SBREL31 39 -#define R_ARM_V4BX 40 -#define R_ARM_TARGET2 41 -#define R_ARM_PREL31 42 -#define R_ARM_MOVW_ABS_NC 43 -#define R_ARM_MOVT_ABS 44 -#define R_ARM_MOVW_PREL_NC 45 -#define R_ARM_MOVT_PREL 46 -#define R_ARM_THM_MOVW_ABS_NC 47 -#define R_ARM_THM_MOVT_ABS 48 -#define R_ARM_THM_MOVW_PREL_NC 49 -#define R_ARM_THM_MOVT_PREL 50 -#define R_ARM_THM_JUMP19 51 -#define R_ARM_THM_JUMP6 52 -#define R_ARM_THM_ALU_PREL_11_0 53 -#define R_ARM_THM_PC12 54 -#define R_ARM_ABS32_NOI 55 -#define R_ARM_REL32_NOI 56 -#define R_ARM_ALU_PC_G0_NC 57 -#define R_ARM_ALU_PC_G0 58 -#define R_ARM_ALU_PC_G1_NC 59 -#define R_ARM_ALU_PC_G1 60 -#define R_ARM_ALU_PC_G2 61 -#define R_ARM_LDR_PC_G1 62 -#define R_ARM_LDR_PC_G2 63 -#define R_ARM_LDRS_PC_G0 64 -#define R_ARM_LDRS_PC_G1 65 -#define R_ARM_LDRS_PC_G2 66 -#define R_ARM_LDC_PC_G0 67 -#define R_ARM_LDC_PC_G1 68 -#define R_ARM_LDC_PC_G2 69 -#define R_ARM_ALU_SB_G0_NC 70 -#define R_ARM_ALU_SB_G0 71 -#define R_ARM_ALU_SB_G1_NC 72 -#define R_ARM_ALU_SB_G1 73 -#define R_ARM_ALU_SB_G2 74 -#define R_ARM_LDR_SB_G0 75 -#define R_ARM_LDR_SB_G1 76 -#define R_ARM_LDR_SB_G2 77 -#define R_ARM_LDRS_SB_G0 78 -#define R_ARM_LDRS_SB_G1 79 -#define R_ARM_LDRS_SB_G2 80 -#define R_ARM_LDC_SB_G0 81 -#define R_ARM_LDC_SB_G1 82 -#define R_ARM_LDC_SB_G2 83 -#define R_ARM_MOVW_BREL_NC 84 -#define R_ARM_MOVT_BREL 85 -#define R_ARM_MOVW_BREL 86 -#define R_ARM_THM_MOVW_BREL_NC 87 -#define R_ARM_THM_MOVT_BREL 88 -#define R_ARM_THM_MOVW_BREL 89 -#define R_ARM_TLS_GOTDESC 90 -#define R_ARM_TLS_CALL 91 -#define R_ARM_TLS_DESCSEQ 92 -#define R_ARM_THM_TLS_CALL 93 -#define R_ARM_PLT32_ABS 94 -#define R_ARM_GOT_ABS 95 -#define R_ARM_GOT_PREL 96 -#define R_ARM_GOT_BREL12 97 -#define R_ARM_GOTOFF12 98 -#define R_ARM_GOTRELAX 99 -#define R_ARM_GNU_VTENTRY 100 -#define R_ARM_GNU_VTINHERIT 101 -#define R_ARM_THM_PC11 102 -#define R_ARM_THM_PC9 103 -#define R_ARM_TLS_GD32 104 - -#define R_ARM_TLS_LDM32 105 - -#define R_ARM_TLS_LDO32 106 - -#define R_ARM_TLS_IE32 107 - -#define R_ARM_TLS_LE32 108 -#define R_ARM_TLS_LDO12 109 -#define R_ARM_TLS_LE12 110 -#define R_ARM_TLS_IE12GP 111 -#define R_ARM_ME_TOO 128 -#define R_ARM_THM_TLS_DESCSEQ 129 -#define R_ARM_THM_TLS_DESCSEQ16 129 -#define R_ARM_THM_TLS_DESCSEQ32 130 -#define R_ARM_THM_GOT_BREL12 131 -#define R_ARM_IRELATIVE 160 -#define R_ARM_RXPC25 249 -#define R_ARM_RSBREL32 250 -#define R_ARM_THM_RPC22 251 -#define R_ARM_RREL32 252 -#define R_ARM_RABS22 253 -#define R_ARM_RPC24 254 -#define R_ARM_RBASE 255 - -#define R_ARM_NUM 256 - - -#define R_CKCORE_NONE 0 -#define R_CKCORE_ADDR32 1 -#define R_CKCORE_PCRELIMM8BY4 2 -#define R_CKCORE_PCRELIMM11BY2 3 -#define R_CKCORE_PCREL32 5 -#define R_CKCORE_PCRELJSR_IMM11BY2 6 -#define R_CKCORE_RELATIVE 9 -#define R_CKCORE_COPY 10 -#define R_CKCORE_GLOB_DAT 11 -#define R_CKCORE_JUMP_SLOT 12 -#define R_CKCORE_GOTOFF 13 -#define R_CKCORE_GOTPC 14 -#define R_CKCORE_GOT32 15 -#define R_CKCORE_PLT32 16 -#define R_CKCORE_ADDRGOT 17 -#define R_CKCORE_ADDRPLT 18 -#define R_CKCORE_PCREL_IMM26BY2 19 -#define R_CKCORE_PCREL_IMM16BY2 20 -#define R_CKCORE_PCREL_IMM16BY4 21 -#define R_CKCORE_PCREL_IMM10BY2 22 -#define R_CKCORE_PCREL_IMM10BY4 23 -#define R_CKCORE_ADDR_HI16 24 -#define R_CKCORE_ADDR_LO16 25 -#define R_CKCORE_GOTPC_HI16 26 -#define R_CKCORE_GOTPC_LO16 27 -#define R_CKCORE_GOTOFF_HI16 28 -#define R_CKCORE_GOTOFF_LO16 29 -#define R_CKCORE_GOT12 30 -#define R_CKCORE_GOT_HI16 31 -#define R_CKCORE_GOT_LO16 32 -#define R_CKCORE_PLT12 33 -#define R_CKCORE_PLT_HI16 34 -#define R_CKCORE_PLT_LO16 35 -#define R_CKCORE_ADDRGOT_HI16 36 -#define R_CKCORE_ADDRGOT_LO16 37 -#define R_CKCORE_ADDRPLT_HI16 38 -#define R_CKCORE_ADDRPLT_LO16 39 -#define R_CKCORE_PCREL_JSR_IMM26BY2 40 -#define R_CKCORE_TOFFSET_LO16 41 -#define R_CKCORE_DOFFSET_LO16 42 -#define R_CKCORE_PCREL_IMM18BY2 43 -#define R_CKCORE_DOFFSET_IMM18 44 -#define R_CKCORE_DOFFSET_IMM18BY2 45 -#define R_CKCORE_DOFFSET_IMM18BY4 46 -#define R_CKCORE_GOT_IMM18BY4 48 -#define R_CKCORE_PLT_IMM18BY4 49 -#define R_CKCORE_PCREL_IMM7BY4 50 -#define R_CKCORE_TLS_LE32 51 -#define R_CKCORE_TLS_IE32 52 -#define R_CKCORE_TLS_GD32 53 -#define R_CKCORE_TLS_LDM32 54 -#define R_CKCORE_TLS_LDO32 55 -#define R_CKCORE_TLS_DTPMOD32 56 -#define R_CKCORE_TLS_DTPOFF32 57 -#define R_CKCORE_TLS_TPOFF32 58 - - -#define EF_IA_64_MASKOS 0x0000000f -#define EF_IA_64_ABI64 0x00000010 -#define EF_IA_64_ARCH 0xff000000 - - -#define PT_IA_64_ARCHEXT (PT_LOPROC + 0) -#define PT_IA_64_UNWIND (PT_LOPROC + 1) -#define PT_IA_64_HP_OPT_ANOT (PT_LOOS + 0x12) -#define PT_IA_64_HP_HSL_ANOT (PT_LOOS + 0x13) -#define PT_IA_64_HP_STACK (PT_LOOS + 0x14) - - -#define PF_IA_64_NORECOV 0x80000000 - - -#define SHT_IA_64_EXT (SHT_LOPROC + 0) -#define SHT_IA_64_UNWIND (SHT_LOPROC + 1) - - -#define SHF_IA_64_SHORT 0x10000000 -#define SHF_IA_64_NORECOV 0x20000000 - - -#define DT_IA_64_PLT_RESERVE (DT_LOPROC + 0) -#define DT_IA_64_NUM 1 - - -#define R_IA64_NONE 0x00 -#define R_IA64_IMM14 0x21 -#define R_IA64_IMM22 0x22 -#define R_IA64_IMM64 0x23 -#define R_IA64_DIR32MSB 0x24 -#define R_IA64_DIR32LSB 0x25 -#define R_IA64_DIR64MSB 0x26 -#define R_IA64_DIR64LSB 0x27 -#define R_IA64_GPREL22 0x2a -#define R_IA64_GPREL64I 0x2b -#define R_IA64_GPREL32MSB 0x2c -#define R_IA64_GPREL32LSB 0x2d -#define R_IA64_GPREL64MSB 0x2e -#define R_IA64_GPREL64LSB 0x2f -#define R_IA64_LTOFF22 0x32 -#define R_IA64_LTOFF64I 0x33 -#define R_IA64_PLTOFF22 0x3a -#define R_IA64_PLTOFF64I 0x3b -#define R_IA64_PLTOFF64MSB 0x3e -#define R_IA64_PLTOFF64LSB 0x3f -#define R_IA64_FPTR64I 0x43 -#define R_IA64_FPTR32MSB 0x44 -#define R_IA64_FPTR32LSB 0x45 -#define R_IA64_FPTR64MSB 0x46 -#define R_IA64_FPTR64LSB 0x47 -#define R_IA64_PCREL60B 0x48 -#define R_IA64_PCREL21B 0x49 -#define R_IA64_PCREL21M 0x4a -#define R_IA64_PCREL21F 0x4b -#define R_IA64_PCREL32MSB 0x4c -#define R_IA64_PCREL32LSB 0x4d -#define R_IA64_PCREL64MSB 0x4e -#define R_IA64_PCREL64LSB 0x4f -#define R_IA64_LTOFF_FPTR22 0x52 -#define R_IA64_LTOFF_FPTR64I 0x53 -#define R_IA64_LTOFF_FPTR32MSB 0x54 -#define R_IA64_LTOFF_FPTR32LSB 0x55 -#define R_IA64_LTOFF_FPTR64MSB 0x56 -#define R_IA64_LTOFF_FPTR64LSB 0x57 -#define R_IA64_SEGREL32MSB 0x5c -#define R_IA64_SEGREL32LSB 0x5d -#define R_IA64_SEGREL64MSB 0x5e -#define R_IA64_SEGREL64LSB 0x5f -#define R_IA64_SECREL32MSB 0x64 -#define R_IA64_SECREL32LSB 0x65 -#define R_IA64_SECREL64MSB 0x66 -#define R_IA64_SECREL64LSB 0x67 -#define R_IA64_REL32MSB 0x6c -#define R_IA64_REL32LSB 0x6d -#define R_IA64_REL64MSB 0x6e -#define R_IA64_REL64LSB 0x6f -#define R_IA64_LTV32MSB 0x74 -#define R_IA64_LTV32LSB 0x75 -#define R_IA64_LTV64MSB 0x76 -#define R_IA64_LTV64LSB 0x77 -#define R_IA64_PCREL21BI 0x79 -#define R_IA64_PCREL22 0x7a -#define R_IA64_PCREL64I 0x7b -#define R_IA64_IPLTMSB 0x80 -#define R_IA64_IPLTLSB 0x81 -#define R_IA64_COPY 0x84 -#define R_IA64_SUB 0x85 -#define R_IA64_LTOFF22X 0x86 -#define R_IA64_LDXMOV 0x87 -#define R_IA64_TPREL14 0x91 -#define R_IA64_TPREL22 0x92 -#define R_IA64_TPREL64I 0x93 -#define R_IA64_TPREL64MSB 0x96 -#define R_IA64_TPREL64LSB 0x97 -#define R_IA64_LTOFF_TPREL22 0x9a -#define R_IA64_DTPMOD64MSB 0xa6 -#define R_IA64_DTPMOD64LSB 0xa7 -#define R_IA64_LTOFF_DTPMOD22 0xaa -#define R_IA64_DTPREL14 0xb1 -#define R_IA64_DTPREL22 0xb2 -#define R_IA64_DTPREL64I 0xb3 -#define R_IA64_DTPREL32MSB 0xb4 -#define R_IA64_DTPREL32LSB 0xb5 -#define R_IA64_DTPREL64MSB 0xb6 -#define R_IA64_DTPREL64LSB 0xb7 -#define R_IA64_LTOFF_DTPREL22 0xba - - -#define EF_SH_MACH_MASK 0x1f -#define EF_SH_UNKNOWN 0x0 -#define EF_SH1 0x1 -#define EF_SH2 0x2 -#define EF_SH3 0x3 -#define EF_SH_DSP 0x4 -#define EF_SH3_DSP 0x5 -#define EF_SH4AL_DSP 0x6 -#define EF_SH3E 0x8 -#define EF_SH4 0x9 -#define EF_SH2E 0xb -#define EF_SH4A 0xc -#define EF_SH2A 0xd -#define EF_SH4_NOFPU 0x10 -#define EF_SH4A_NOFPU 0x11 -#define EF_SH4_NOMMU_NOFPU 0x12 -#define EF_SH2A_NOFPU 0x13 -#define EF_SH3_NOMMU 0x14 -#define EF_SH2A_SH4_NOFPU 0x15 -#define EF_SH2A_SH3_NOFPU 0x16 -#define EF_SH2A_SH4 0x17 -#define EF_SH2A_SH3E 0x18 - -#define R_SH_NONE 0 -#define R_SH_DIR32 1 -#define R_SH_REL32 2 -#define R_SH_DIR8WPN 3 -#define R_SH_IND12W 4 -#define R_SH_DIR8WPL 5 -#define R_SH_DIR8WPZ 6 -#define R_SH_DIR8BP 7 -#define R_SH_DIR8W 8 -#define R_SH_DIR8L 9 -#define R_SH_SWITCH16 25 -#define R_SH_SWITCH32 26 -#define R_SH_USES 27 -#define R_SH_COUNT 28 -#define R_SH_ALIGN 29 -#define R_SH_CODE 30 -#define R_SH_DATA 31 -#define R_SH_LABEL 32 -#define R_SH_SWITCH8 33 -#define R_SH_GNU_VTINHERIT 34 -#define R_SH_GNU_VTENTRY 35 -#define R_SH_TLS_GD_32 144 -#define R_SH_TLS_LD_32 145 -#define R_SH_TLS_LDO_32 146 -#define R_SH_TLS_IE_32 147 -#define R_SH_TLS_LE_32 148 -#define R_SH_TLS_DTPMOD32 149 -#define R_SH_TLS_DTPOFF32 150 -#define R_SH_TLS_TPOFF32 151 -#define R_SH_GOT32 160 -#define R_SH_PLT32 161 -#define R_SH_COPY 162 -#define R_SH_GLOB_DAT 163 -#define R_SH_JMP_SLOT 164 -#define R_SH_RELATIVE 165 -#define R_SH_GOTOFF 166 -#define R_SH_GOTPC 167 -#define R_SH_GOT20 201 -#define R_SH_GOTOFF20 202 -#define R_SH_GOTFUNCDESC 203 -#define R_SH_GOTFUNCDEST20 204 -#define R_SH_GOTOFFFUNCDESC 205 -#define R_SH_GOTOFFFUNCDEST20 206 -#define R_SH_FUNCDESC 207 -#define R_SH_FUNCDESC_VALUE 208 - -#define R_SH_NUM 256 - - - -#define R_390_NONE 0 -#define R_390_8 1 -#define R_390_12 2 -#define R_390_16 3 -#define R_390_32 4 -#define R_390_PC32 5 -#define R_390_GOT12 6 -#define R_390_GOT32 7 -#define R_390_PLT32 8 -#define R_390_COPY 9 -#define R_390_GLOB_DAT 10 -#define R_390_JMP_SLOT 11 -#define R_390_RELATIVE 12 -#define R_390_GOTOFF32 13 -#define R_390_GOTPC 14 -#define R_390_GOT16 15 -#define R_390_PC16 16 -#define R_390_PC16DBL 17 -#define R_390_PLT16DBL 18 -#define R_390_PC32DBL 19 -#define R_390_PLT32DBL 20 -#define R_390_GOTPCDBL 21 -#define R_390_64 22 -#define R_390_PC64 23 -#define R_390_GOT64 24 -#define R_390_PLT64 25 -#define R_390_GOTENT 26 -#define R_390_GOTOFF16 27 -#define R_390_GOTOFF64 28 -#define R_390_GOTPLT12 29 -#define R_390_GOTPLT16 30 -#define R_390_GOTPLT32 31 -#define R_390_GOTPLT64 32 -#define R_390_GOTPLTENT 33 -#define R_390_PLTOFF16 34 -#define R_390_PLTOFF32 35 -#define R_390_PLTOFF64 36 -#define R_390_TLS_LOAD 37 -#define R_390_TLS_GDCALL 38 - -#define R_390_TLS_LDCALL 39 - -#define R_390_TLS_GD32 40 - -#define R_390_TLS_GD64 41 - -#define R_390_TLS_GOTIE12 42 - -#define R_390_TLS_GOTIE32 43 - -#define R_390_TLS_GOTIE64 44 - -#define R_390_TLS_LDM32 45 - -#define R_390_TLS_LDM64 46 - -#define R_390_TLS_IE32 47 - -#define R_390_TLS_IE64 48 - -#define R_390_TLS_IEENT 49 - -#define R_390_TLS_LE32 50 - -#define R_390_TLS_LE64 51 - -#define R_390_TLS_LDO32 52 - -#define R_390_TLS_LDO64 53 - -#define R_390_TLS_DTPMOD 54 -#define R_390_TLS_DTPOFF 55 -#define R_390_TLS_TPOFF 56 - -#define R_390_20 57 -#define R_390_GOT20 58 -#define R_390_GOTPLT20 59 -#define R_390_TLS_GOTIE20 60 - - -#define R_390_NUM 61 - - - -#define R_CRIS_NONE 0 -#define R_CRIS_8 1 -#define R_CRIS_16 2 -#define R_CRIS_32 3 -#define R_CRIS_8_PCREL 4 -#define R_CRIS_16_PCREL 5 -#define R_CRIS_32_PCREL 6 -#define R_CRIS_GNU_VTINHERIT 7 -#define R_CRIS_GNU_VTENTRY 8 -#define R_CRIS_COPY 9 -#define R_CRIS_GLOB_DAT 10 -#define R_CRIS_JUMP_SLOT 11 -#define R_CRIS_RELATIVE 12 -#define R_CRIS_16_GOT 13 -#define R_CRIS_32_GOT 14 -#define R_CRIS_16_GOTPLT 15 -#define R_CRIS_32_GOTPLT 16 -#define R_CRIS_32_GOTREL 17 -#define R_CRIS_32_PLT_GOTREL 18 -#define R_CRIS_32_PLT_PCREL 19 - -#define R_CRIS_NUM 20 - - - -#define R_X86_64_NONE 0 -#define R_X86_64_64 1 -#define R_X86_64_PC32 2 -#define R_X86_64_GOT32 3 -#define R_X86_64_PLT32 4 -#define R_X86_64_COPY 5 -#define R_X86_64_GLOB_DAT 6 -#define R_X86_64_JUMP_SLOT 7 -#define R_X86_64_RELATIVE 8 -#define R_X86_64_GOTPCREL 9 - -#define R_X86_64_32 10 -#define R_X86_64_32S 11 -#define R_X86_64_16 12 -#define R_X86_64_PC16 13 -#define R_X86_64_8 14 -#define R_X86_64_PC8 15 -#define R_X86_64_DTPMOD64 16 -#define R_X86_64_DTPOFF64 17 -#define R_X86_64_TPOFF64 18 -#define R_X86_64_TLSGD 19 - -#define R_X86_64_TLSLD 20 - -#define R_X86_64_DTPOFF32 21 -#define R_X86_64_GOTTPOFF 22 - -#define R_X86_64_TPOFF32 23 -#define R_X86_64_PC64 24 -#define R_X86_64_GOTOFF64 25 -#define R_X86_64_GOTPC32 26 -#define R_X86_64_GOT64 27 -#define R_X86_64_GOTPCREL64 28 -#define R_X86_64_GOTPC64 29 -#define R_X86_64_GOTPLT64 30 -#define R_X86_64_PLTOFF64 31 -#define R_X86_64_SIZE32 32 -#define R_X86_64_SIZE64 33 - -#define R_X86_64_GOTPC32_TLSDESC 34 -#define R_X86_64_TLSDESC_CALL 35 - -#define R_X86_64_TLSDESC 36 -#define R_X86_64_IRELATIVE 37 -#define R_X86_64_RELATIVE64 38 -#define R_X86_64_GOTPCRELX 41 -#define R_X86_64_REX_GOTPCRELX 42 -#define R_X86_64_NUM 43 - - - -#define R_MN10300_NONE 0 -#define R_MN10300_32 1 -#define R_MN10300_16 2 -#define R_MN10300_8 3 -#define R_MN10300_PCREL32 4 -#define R_MN10300_PCREL16 5 -#define R_MN10300_PCREL8 6 -#define R_MN10300_GNU_VTINHERIT 7 -#define R_MN10300_GNU_VTENTRY 8 -#define R_MN10300_24 9 -#define R_MN10300_GOTPC32 10 -#define R_MN10300_GOTPC16 11 -#define R_MN10300_GOTOFF32 12 -#define R_MN10300_GOTOFF24 13 -#define R_MN10300_GOTOFF16 14 -#define R_MN10300_PLT32 15 -#define R_MN10300_PLT16 16 -#define R_MN10300_GOT32 17 -#define R_MN10300_GOT24 18 -#define R_MN10300_GOT16 19 -#define R_MN10300_COPY 20 -#define R_MN10300_GLOB_DAT 21 -#define R_MN10300_JMP_SLOT 22 -#define R_MN10300_RELATIVE 23 - -#define R_MN10300_NUM 24 - - - -#define R_M32R_NONE 0 -#define R_M32R_16 1 -#define R_M32R_32 2 -#define R_M32R_24 3 -#define R_M32R_10_PCREL 4 -#define R_M32R_18_PCREL 5 -#define R_M32R_26_PCREL 6 -#define R_M32R_HI16_ULO 7 -#define R_M32R_HI16_SLO 8 -#define R_M32R_LO16 9 -#define R_M32R_SDA16 10 -#define R_M32R_GNU_VTINHERIT 11 -#define R_M32R_GNU_VTENTRY 12 - -#define R_M32R_16_RELA 33 -#define R_M32R_32_RELA 34 -#define R_M32R_24_RELA 35 -#define R_M32R_10_PCREL_RELA 36 -#define R_M32R_18_PCREL_RELA 37 -#define R_M32R_26_PCREL_RELA 38 -#define R_M32R_HI16_ULO_RELA 39 -#define R_M32R_HI16_SLO_RELA 40 -#define R_M32R_LO16_RELA 41 -#define R_M32R_SDA16_RELA 42 -#define R_M32R_RELA_GNU_VTINHERIT 43 -#define R_M32R_RELA_GNU_VTENTRY 44 -#define R_M32R_REL32 45 - -#define R_M32R_GOT24 48 -#define R_M32R_26_PLTREL 49 -#define R_M32R_COPY 50 -#define R_M32R_GLOB_DAT 51 -#define R_M32R_JMP_SLOT 52 -#define R_M32R_RELATIVE 53 -#define R_M32R_GOTOFF 54 -#define R_M32R_GOTPC24 55 -#define R_M32R_GOT16_HI_ULO 56 - -#define R_M32R_GOT16_HI_SLO 57 - -#define R_M32R_GOT16_LO 58 -#define R_M32R_GOTPC_HI_ULO 59 - -#define R_M32R_GOTPC_HI_SLO 60 - -#define R_M32R_GOTPC_LO 61 - -#define R_M32R_GOTOFF_HI_ULO 62 - -#define R_M32R_GOTOFF_HI_SLO 63 - -#define R_M32R_GOTOFF_LO 64 -#define R_M32R_NUM 256 - -#define R_MICROBLAZE_NONE 0 -#define R_MICROBLAZE_32 1 -#define R_MICROBLAZE_32_PCREL 2 -#define R_MICROBLAZE_64_PCREL 3 -#define R_MICROBLAZE_32_PCREL_LO 4 -#define R_MICROBLAZE_64 5 -#define R_MICROBLAZE_32_LO 6 -#define R_MICROBLAZE_SRO32 7 -#define R_MICROBLAZE_SRW32 8 -#define R_MICROBLAZE_64_NONE 9 -#define R_MICROBLAZE_32_SYM_OP_SYM 10 -#define R_MICROBLAZE_GNU_VTINHERIT 11 -#define R_MICROBLAZE_GNU_VTENTRY 12 -#define R_MICROBLAZE_GOTPC_64 13 -#define R_MICROBLAZE_GOT_64 14 -#define R_MICROBLAZE_PLT_64 15 -#define R_MICROBLAZE_REL 16 -#define R_MICROBLAZE_JUMP_SLOT 17 -#define R_MICROBLAZE_GLOB_DAT 18 -#define R_MICROBLAZE_GOTOFF_64 19 -#define R_MICROBLAZE_GOTOFF_32 20 -#define R_MICROBLAZE_COPY 21 -#define R_MICROBLAZE_TLS 22 -#define R_MICROBLAZE_TLSGD 23 -#define R_MICROBLAZE_TLSLD 24 -#define R_MICROBLAZE_TLSDTPMOD32 25 -#define R_MICROBLAZE_TLSDTPREL32 26 -#define R_MICROBLAZE_TLSDTPREL64 27 -#define R_MICROBLAZE_TLSGOTTPREL32 28 -#define R_MICROBLAZE_TLSTPREL32 29 - -#define DT_NIOS2_GP 0x70000002 - -#define R_NIOS2_NONE 0 -#define R_NIOS2_S16 1 -#define R_NIOS2_U16 2 -#define R_NIOS2_PCREL16 3 -#define R_NIOS2_CALL26 4 -#define R_NIOS2_IMM5 5 -#define R_NIOS2_CACHE_OPX 6 -#define R_NIOS2_IMM6 7 -#define R_NIOS2_IMM8 8 -#define R_NIOS2_HI16 9 -#define R_NIOS2_LO16 10 -#define R_NIOS2_HIADJ16 11 -#define R_NIOS2_BFD_RELOC_32 12 -#define R_NIOS2_BFD_RELOC_16 13 -#define R_NIOS2_BFD_RELOC_8 14 -#define R_NIOS2_GPREL 15 -#define R_NIOS2_GNU_VTINHERIT 16 -#define R_NIOS2_GNU_VTENTRY 17 -#define R_NIOS2_UJMP 18 -#define R_NIOS2_CJMP 19 -#define R_NIOS2_CALLR 20 -#define R_NIOS2_ALIGN 21 -#define R_NIOS2_GOT16 22 -#define R_NIOS2_CALL16 23 -#define R_NIOS2_GOTOFF_LO 24 -#define R_NIOS2_GOTOFF_HA 25 -#define R_NIOS2_PCREL_LO 26 -#define R_NIOS2_PCREL_HA 27 -#define R_NIOS2_TLS_GD16 28 -#define R_NIOS2_TLS_LDM16 29 -#define R_NIOS2_TLS_LDO16 30 -#define R_NIOS2_TLS_IE16 31 -#define R_NIOS2_TLS_LE16 32 -#define R_NIOS2_TLS_DTPMOD 33 -#define R_NIOS2_TLS_DTPREL 34 -#define R_NIOS2_TLS_TPREL 35 -#define R_NIOS2_COPY 36 -#define R_NIOS2_GLOB_DAT 37 -#define R_NIOS2_JUMP_SLOT 38 -#define R_NIOS2_RELATIVE 39 -#define R_NIOS2_GOTOFF 40 -#define R_NIOS2_CALL26_NOAT 41 -#define R_NIOS2_GOT_LO 42 -#define R_NIOS2_GOT_HA 43 -#define R_NIOS2_CALL_LO 44 -#define R_NIOS2_CALL_HA 45 - -#define R_OR1K_NONE 0 -#define R_OR1K_32 1 -#define R_OR1K_16 2 -#define R_OR1K_8 3 -#define R_OR1K_LO_16_IN_INSN 4 -#define R_OR1K_HI_16_IN_INSN 5 -#define R_OR1K_INSN_REL_26 6 -#define R_OR1K_GNU_VTENTRY 7 -#define R_OR1K_GNU_VTINHERIT 8 -#define R_OR1K_32_PCREL 9 -#define R_OR1K_16_PCREL 10 -#define R_OR1K_8_PCREL 11 -#define R_OR1K_GOTPC_HI16 12 -#define R_OR1K_GOTPC_LO16 13 -#define R_OR1K_GOT16 14 -#define R_OR1K_PLT26 15 -#define R_OR1K_GOTOFF_HI16 16 -#define R_OR1K_GOTOFF_LO16 17 -#define R_OR1K_COPY 18 -#define R_OR1K_GLOB_DAT 19 -#define R_OR1K_JMP_SLOT 20 -#define R_OR1K_RELATIVE 21 -#define R_OR1K_TLS_GD_HI16 22 -#define R_OR1K_TLS_GD_LO16 23 -#define R_OR1K_TLS_LDM_HI16 24 -#define R_OR1K_TLS_LDM_LO16 25 -#define R_OR1K_TLS_LDO_HI16 26 -#define R_OR1K_TLS_LDO_LO16 27 -#define R_OR1K_TLS_IE_HI16 28 -#define R_OR1K_TLS_IE_LO16 29 -#define R_OR1K_TLS_LE_HI16 30 -#define R_OR1K_TLS_LE_LO16 31 -#define R_OR1K_TLS_TPOFF 32 -#define R_OR1K_TLS_DTPOFF 33 -#define R_OR1K_TLS_DTPMOD 34 - -#define R_BPF_NONE 0 -#define R_BPF_MAP_FD 1 - -#define R_RISCV_NONE 0 -#define R_RISCV_32 1 -#define R_RISCV_64 2 -#define R_RISCV_RELATIVE 3 -#define R_RISCV_COPY 4 -#define R_RISCV_JUMP_SLOT 5 -#define R_RISCV_TLS_DTPMOD32 6 -#define R_RISCV_TLS_DTPMOD64 7 -#define R_RISCV_TLS_DTPREL32 8 -#define R_RISCV_TLS_DTPREL64 9 -#define R_RISCV_TLS_TPREL32 10 -#define R_RISCV_TLS_TPREL64 11 - -#define R_RISCV_BRANCH 16 -#define R_RISCV_JAL 17 -#define R_RISCV_CALL 18 -#define R_RISCV_CALL_PLT 19 -#define R_RISCV_GOT_HI20 20 -#define R_RISCV_TLS_GOT_HI20 21 -#define R_RISCV_TLS_GD_HI20 22 -#define R_RISCV_PCREL_HI20 23 -#define R_RISCV_PCREL_LO12_I 24 -#define R_RISCV_PCREL_LO12_S 25 -#define R_RISCV_HI20 26 -#define R_RISCV_LO12_I 27 -#define R_RISCV_LO12_S 28 -#define R_RISCV_TPREL_HI20 29 -#define R_RISCV_TPREL_LO12_I 30 -#define R_RISCV_TPREL_LO12_S 31 -#define R_RISCV_TPREL_ADD 32 -#define R_RISCV_ADD8 33 -#define R_RISCV_ADD16 34 -#define R_RISCV_ADD32 35 -#define R_RISCV_ADD64 36 -#define R_RISCV_SUB8 37 -#define R_RISCV_SUB16 38 -#define R_RISCV_SUB32 39 -#define R_RISCV_SUB64 40 -#define R_RISCV_GNU_VTINHERIT 41 -#define R_RISCV_GNU_VTENTRY 42 -#define R_RISCV_ALIGN 43 -#define R_RISCV_RVC_BRANCH 44 -#define R_RISCV_RVC_JUMP 45 -#define R_RISCV_RVC_LUI 46 -#define R_RISCV_GPREL_I 47 -#define R_RISCV_GPREL_S 48 -#define R_RISCV_TPREL_I 49 -#define R_RISCV_TPREL_S 50 -#define R_RISCV_RELAX 51 -#define R_RISCV_SUB6 52 -#define R_RISCV_SET6 53 -#define R_RISCV_SET8 54 -#define R_RISCV_SET16 55 -#define R_RISCV_SET32 56 -#define R_RISCV_32_PCREL 57 - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/porting/linux/user/include/fcntl.h b/porting/linux/user/include/fcntl.h deleted file mode 100644 index 98c165f1c..000000000 --- a/porting/linux/user/include/fcntl.h +++ /dev/null @@ -1,217 +0,0 @@ -#ifndef _FCNTL_H -#define _FCNTL_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define __NEED_off_t -#define __NEED_pid_t -#define __NEED_mode_t - -#ifdef _GNU_SOURCE -#define __NEED_size_t -#define __NEED_ssize_t -#define __NEED_struct_iovec -#endif - -#include - -#include -#include - -struct flock { - short l_type; - short l_whence; - off_t l_start; - off_t l_len; - pid_t l_pid; -}; - -int creat(const char *, mode_t); -int fcntl(int, int, ...); -int open(const char *, int, ...); -int openat(int, const char *, int, ...); -int posix_fadvise(int, off_t, off_t, int); -int posix_fallocate(int, off_t, off_t); - -#define O_SEARCH O_PATH -#define O_EXEC O_PATH -#define O_TTY_INIT 0 - -#define O_ACCMODE (03|O_SEARCH) -#define O_RDONLY 00 -#define O_WRONLY 01 -#define O_RDWR 02 - -#define F_OFD_GETLK 36 -#define F_OFD_SETLK 37 -#define F_OFD_SETLKW 38 - -#define F_DUPFD_CLOEXEC 1030 - -#define F_RDLCK 0 -#define F_WRLCK 1 -#define F_UNLCK 2 - -#define FD_CLOEXEC 1 - -#define AT_FDCWD (-100) -#define AT_SYMLINK_NOFOLLOW 0x100 -#define AT_REMOVEDIR 0x200 -#define AT_SYMLINK_FOLLOW 0x400 -#define AT_EACCESS 0x200 - -#define POSIX_FADV_NORMAL 0 -#define POSIX_FADV_RANDOM 1 -#define POSIX_FADV_SEQUENTIAL 2 -#define POSIX_FADV_WILLNEED 3 -#ifndef POSIX_FADV_DONTNEED -#define POSIX_FADV_DONTNEED 4 -#define POSIX_FADV_NOREUSE 5 -#endif - -#undef SEEK_SET -#undef SEEK_CUR -#undef SEEK_END -#define SEEK_SET 0 -#define SEEK_CUR 1 -#define SEEK_END 2 - -#ifndef S_IRUSR -#define S_ISUID 04000 -#define S_ISGID 02000 -#define S_ISVTX 01000 -#define S_IRUSR 0400 -#define S_IWUSR 0200 -#define S_IXUSR 0100 -#define S_IRWXU 0700 -#define S_IRGRP 0040 -#define S_IWGRP 0020 -#define S_IXGRP 0010 -#define S_IRWXG 0070 -#define S_IROTH 0004 -#define S_IWOTH 0002 -#define S_IXOTH 0001 -#define S_IRWXO 0007 -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define AT_NO_AUTOMOUNT 0x800 -#define AT_EMPTY_PATH 0x1000 -#define AT_STATX_SYNC_TYPE 0x6000 -#define AT_STATX_SYNC_AS_STAT 0x0000 -#define AT_STATX_FORCE_SYNC 0x2000 -#define AT_STATX_DONT_SYNC 0x4000 -#define AT_RECURSIVE 0x8000 - -#define FAPPEND O_APPEND -#define FFSYNC O_SYNC -#define FASYNC O_ASYNC -#define FNONBLOCK O_NONBLOCK -#define FNDELAY O_NDELAY - -#define F_OK 0 -#define R_OK 4 -#define W_OK 2 -#define X_OK 1 -#define F_ULOCK 0 -#define F_LOCK 1 -#define F_TLOCK 2 -#define F_TEST 3 - -#define F_SETLEASE 1024 -#define F_GETLEASE 1025 -#define F_NOTIFY 1026 -#define F_CANCELLK 1029 -#define F_SETPIPE_SZ 1031 -#define F_GETPIPE_SZ 1032 -#define F_ADD_SEALS 1033 -#define F_GET_SEALS 1034 - -#define F_SEAL_SEAL 0x0001 -#define F_SEAL_SHRINK 0x0002 -#define F_SEAL_GROW 0x0004 -#define F_SEAL_WRITE 0x0008 -#define F_SEAL_FUTURE_WRITE 0x0010 - -#define F_GET_RW_HINT 1035 -#define F_SET_RW_HINT 1036 -#define F_GET_FILE_RW_HINT 1037 -#define F_SET_FILE_RW_HINT 1038 - -#define RWF_WRITE_LIFE_NOT_SET 0 -#define RWH_WRITE_LIFE_NONE 1 -#define RWH_WRITE_LIFE_SHORT 2 -#define RWH_WRITE_LIFE_MEDIUM 3 -#define RWH_WRITE_LIFE_LONG 4 -#define RWH_WRITE_LIFE_EXTREME 5 - -#define DN_ACCESS 0x00000001 -#define DN_MODIFY 0x00000002 -#define DN_CREATE 0x00000004 -#define DN_DELETE 0x00000008 -#define DN_RENAME 0x00000010 -#define DN_ATTRIB 0x00000020 -#define DN_MULTISHOT 0x80000000 - -int lockf(int, int, off_t); -#endif - -#if defined(_GNU_SOURCE) -#define F_OWNER_TID 0 -#define F_OWNER_PID 1 -#define F_OWNER_PGRP 2 -#define F_OWNER_GID 2 -struct file_handle { - unsigned handle_bytes; - int handle_type; - unsigned char f_handle[]; -}; -struct f_owner_ex { - int type; - pid_t pid; -}; -#define FALLOC_FL_KEEP_SIZE 1 -#define FALLOC_FL_PUNCH_HOLE 2 -#define MAX_HANDLE_SZ 128 -#define SYNC_FILE_RANGE_WAIT_BEFORE 1 -#define SYNC_FILE_RANGE_WRITE 2 -#define SYNC_FILE_RANGE_WAIT_AFTER 4 -#define SPLICE_F_MOVE 1 -#define SPLICE_F_NONBLOCK 2 -#define SPLICE_F_MORE 4 -#define SPLICE_F_GIFT 8 -int fallocate(int, int, off_t, off_t); -#define fallocate64 fallocate -int name_to_handle_at(int, const char *, struct file_handle *, int *, int); -int open_by_handle_at(int, struct file_handle *, int); -ssize_t readahead(int, off_t, size_t); -int sync_file_range(int, off_t, off_t, unsigned); -ssize_t vmsplice(int, const struct iovec *, size_t, unsigned); -ssize_t splice(int, off_t *, int, off_t *, size_t, unsigned); -ssize_t tee(int, int, size_t, unsigned); -#define loff_t off_t -#endif - -#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) -#define F_GETLK64 F_GETLK -#define F_SETLK64 F_SETLK -#define F_SETLKW64 F_SETLKW -#define flock64 flock -#define open64 open -#define openat64 openat -#define creat64 creat -#define lockf64 lockf -#define posix_fadvise64 posix_fadvise -#define posix_fallocate64 posix_fallocate -#define off64_t off_t -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/langinfo.h b/porting/linux/user/include/langinfo.h deleted file mode 100644 index 290741489..000000000 --- a/porting/linux/user/include/langinfo.h +++ /dev/null @@ -1,101 +0,0 @@ -#ifndef _LANGINFO_H -#define _LANGINFO_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -#define __NEED_locale_t - -#include - -#define ABDAY_1 0x20000 -#define ABDAY_2 0x20001 -#define ABDAY_3 0x20002 -#define ABDAY_4 0x20003 -#define ABDAY_5 0x20004 -#define ABDAY_6 0x20005 -#define ABDAY_7 0x20006 - -#define DAY_1 0x20007 -#define DAY_2 0x20008 -#define DAY_3 0x20009 -#define DAY_4 0x2000A -#define DAY_5 0x2000B -#define DAY_6 0x2000C -#define DAY_7 0x2000D - -#define ABMON_1 0x2000E -#define ABMON_2 0x2000F -#define ABMON_3 0x20010 -#define ABMON_4 0x20011 -#define ABMON_5 0x20012 -#define ABMON_6 0x20013 -#define ABMON_7 0x20014 -#define ABMON_8 0x20015 -#define ABMON_9 0x20016 -#define ABMON_10 0x20017 -#define ABMON_11 0x20018 -#define ABMON_12 0x20019 - -#define MON_1 0x2001A -#define MON_2 0x2001B -#define MON_3 0x2001C -#define MON_4 0x2001D -#define MON_5 0x2001E -#define MON_6 0x2001F -#define MON_7 0x20020 -#define MON_8 0x20021 -#define MON_9 0x20022 -#define MON_10 0x20023 -#define MON_11 0x20024 -#define MON_12 0x20025 - -#define AM_STR 0x20026 -#define PM_STR 0x20027 - -#define D_T_FMT 0x20028 -#define D_FMT 0x20029 -#define T_FMT 0x2002A -#define T_FMT_AMPM 0x2002B - -#define ERA 0x2002C -#define ERA_D_FMT 0x2002E -#define ALT_DIGITS 0x2002F -#define ERA_D_T_FMT 0x20030 -#define ERA_T_FMT 0x20031 - -#define AM_STR_LOWER 0x20032 -#define PM_STR_LOWER 0x20033 - -#define CODESET 14 - -#define CRNCYSTR 0x4000F - -#define RADIXCHAR 0x10000 -#define THOUSEP 0x10001 -#define YESEXPR 0x50000 -#define NOEXPR 0x50001 - -#define _NL_LOCALE_NAME(cat) (((cat)<<16) | 0xffff) - -#if defined(_GNU_SOURCE) -#define NL_LOCALE_NAME(cat) _NL_LOCALE_NAME(cat) -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define YESSTR 0x50002 -#define NOSTR 0x50003 -#endif - -char *nl_langinfo(nl_item); -char *nl_langinfo_l(nl_item, locale_t); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/locale.h b/porting/linux/user/include/locale.h deleted file mode 100755 index 5728a3ead..000000000 --- a/porting/linux/user/include/locale.h +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef _LOCALE_H -#define _LOCALE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#if __cplusplus >= 201103L -#define NULL nullptr -#elif defined(__cplusplus) -#define NULL 0L -#else -#define NULL ((void*)0) -#endif - -#define LC_CTYPE 0 -#define LC_NUMERIC 1 -#define LC_TIME 2 -#define LC_COLLATE 3 -#define LC_MONETARY 4 -#define LC_MESSAGES 5 -#define LC_PAPER 6 -#define LC_NAME 7 -#define LC_ADDRESS 8 -#define LC_TELEPHONE 9 -#define LC_MEASUREMENT 10 -#define LC_IDENTIFICATION 11 -#define LC_ALL 12 - -struct lconv { - char *decimal_point; - char *thousands_sep; - char *grouping; - - char *int_curr_symbol; - char *currency_symbol; - char *mon_decimal_point; - char *mon_thousands_sep; - char *mon_grouping; - char *positive_sign; - char *negative_sign; - char int_frac_digits; - char frac_digits; - char p_cs_precedes; - char p_sep_by_space; - char n_cs_precedes; - char n_sep_by_space; - char p_sign_posn; - char n_sign_posn; - char int_p_cs_precedes; - char int_p_sep_by_space; - char int_n_cs_precedes; - char int_n_sep_by_space; - char int_p_sign_posn; - char int_n_sign_posn; -}; - -char *setlocale (int, const char *); -struct lconv *localeconv(void); - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) - -#define __NEED_locale_t - -#include - -#define LC_GLOBAL_LOCALE ((locale_t)-1) - -#define LC_CTYPE_MASK (1< - -#ifdef __cplusplus -extern "C" { -#endif - -#define __NEED_size_t -#define __NEED_ssize_t -#define __NEED_uintptr_t - -#include - -void *malloc (size_t); -void *calloc (size_t, size_t); -void *realloc (void *, size_t); -void free (void *); -void *valloc (size_t); -void *memalign(size_t, size_t); - -size_t malloc_usable_size(void *); - -#define __MALLINFO_BODY \ - size_t arena; \ - size_t ordblks; \ - size_t smblks; \ - size_t hblks; \ - size_t hblkhd; \ - size_t usmblks; \ - size_t fsmblks; \ - size_t uordblks; \ - size_t fordblks; \ - size_t keepcost; - -struct mallinfo { __MALLINFO_BODY }; - -struct mallinfo mallinfo(void); - -struct mallinfo2 { __MALLINFO_BODY }; - -struct mallinfo2 mallinfo2(void); - -int malloc_iterate(void* base, size_t size, void (*callback)(void* base, size_t size, void* arg), void* arg); -void malloc_disable(void); -void malloc_enable(void); - -int malloc_info(int options, FILE* fp); -void malloc_stats_print(void (*write_cb) (void *, const char *), void *cbopaque, const char *opts); - - -#define M_SET_THREAD_CACHE (-1001) -#define M_THREAD_CACHE_ENABLE 1 -#define M_THREAD_CACHE_DISABLE 0 - -#define M_FLUSH_THREAD_CACHE (-1002) - -#define M_DELAYED_FREE (-1003) -#define M_DELAYED_FREE_ENABLE 1 -#define M_DELAYED_FREE_DISABLE 0 - -#define M_OHOS_CONFIG (-1004) -#define M_DISABLE_OPT_TCACHE 100 -#define M_ENABLE_OPT_TCACHE 101 -#define M_TCACHE_PERFORMANCE_MODE 102 -#define M_TCACHE_NORMAL_MODE 103 - -int mallopt(int param, int value); -ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/poll.h b/porting/linux/user/include/poll.h deleted file mode 100644 index b7df3dba8..000000000 --- a/porting/linux/user/include/poll.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef _POLL_H -#define _POLL_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include - -#define POLLIN 0x001 -#define POLLPRI 0x002 -#define POLLOUT 0x004 -#define POLLERR 0x008 -#define POLLHUP 0x010 -#define POLLNVAL 0x020 -#define POLLRDNORM 0x040 -#define POLLRDBAND 0x080 -#ifndef POLLWRNORM -#define POLLWRNORM 0x100 -#define POLLWRBAND 0x200 -#endif -#ifndef POLLMSG -#define POLLMSG 0x400 -#define POLLRDHUP 0x2000 -#endif - -typedef unsigned long nfds_t; - -struct pollfd { - int fd; - short events; - short revents; -}; - -int poll (struct pollfd *, nfds_t, int); - -#ifdef _GNU_SOURCE -#define __NEED_time_t -#define __NEED_struct_timespec -#define __NEED_sigset_t -#include -int ppoll(struct pollfd *, nfds_t, const struct timespec *, const sigset_t *); -#endif - -#if _REDIR_TIME64 -#ifdef _GNU_SOURCE -__REDIR(ppoll, __ppoll_time64); -#endif -#endif - -#include - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/pthread.h b/porting/linux/user/include/pthread.h deleted file mode 100644 index ac5b8618b..000000000 --- a/porting/linux/user/include/pthread.h +++ /dev/null @@ -1,406 +0,0 @@ -#ifndef _PTHREAD_H -#define _PTHREAD_H -#ifdef __cplusplus -extern "C" { -#endif - -#include - -/* Musl did not provide the "owner" macro directly, - * so users can not access the mutex-ower-ID. - * Thus we added this macro for getting the owner-ID - * of the mutex. */ - -/* These macros provides macros for accessing inner - * attributes of the pthread_mutex_t struct. - * It is intended for solving the coompiling failure - * of Dopra codes which claims that .__data.* realm - * can not be found in pthread_mutex_t. */ - -#define __NEED_time_t -#define __NEED_clockid_t -#define __NEED_struct_timespec -#define __NEED_sigset_t -#define __NEED_pthread_t -#define __NEED_pthread_attr_t -#define __NEED_pthread_mutexattr_t -#define __NEED_pthread_condattr_t -#define __NEED_pthread_rwlockattr_t -#define __NEED_pthread_barrierattr_t -#define __NEED_pthread_mutex_t -#define __NEED_pthread_cond_t -#define __NEED_pthread_rwlock_t -#define __NEED_pthread_barrier_t -#define __NEED_pthread_spinlock_t -#define __NEED_pthread_key_t -#define __NEED_pthread_once_t -#define __NEED_size_t - -#include - -#include -#include - -#define PTHREAD_CREATE_JOINABLE 0 -#define PTHREAD_CREATE_DETACHED 1 - -#define PTHREAD_MUTEX_NORMAL 0 -#define PTHREAD_MUTEX_DEFAULT 0 -#define PTHREAD_MUTEX_RECURSIVE 1 -#define PTHREAD_MUTEX_ERRORCHECK 2 - -#define PTHREAD_MUTEX_STALLED 0 -#define PTHREAD_MUTEX_ROBUST 1 - -#define PTHREAD_PRIO_NONE 0 -#define PTHREAD_PRIO_INHERIT 1 -#define PTHREAD_PRIO_PROTECT 2 - -#define PTHREAD_INHERIT_SCHED 0 -#define PTHREAD_EXPLICIT_SCHED 1 - -#define PTHREAD_SCOPE_SYSTEM 0 -#define PTHREAD_SCOPE_PROCESS 1 - -#define PTHREAD_PROCESS_PRIVATE 0 -#define PTHREAD_PROCESS_SHARED 1 - - -#define PTHREAD_MUTEX_INITIALIZER {{{0}}} -#define PTHREAD_RWLOCK_INITIALIZER {{{0}}} -#define PTHREAD_COND_INITIALIZER {{{0}}} -#define PTHREAD_ONCE_INIT 0 - - -#define PTHREAD_CANCEL_ENABLE 0 -#define PTHREAD_CANCEL_DISABLE 1 -#define PTHREAD_CANCEL_MASKED 2 - -#define PTHREAD_CANCEL_DEFERRED 0 -#define PTHREAD_CANCEL_ASYNCHRONOUS 1 - -#define PTHREAD_CANCELED ((void *)-1) - - -#define PTHREAD_BARRIER_SERIAL_THREAD (-1) - - -#define PTHREAD_NULL ((pthread_t)0) - - -int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict); -int pthread_detach(pthread_t); -_Noreturn void pthread_exit(void *); -int pthread_join(pthread_t, void **); -pid_t pthread_gettid_np(pthread_t); - -#ifdef __GNUC__ -__attribute__((const)) -#endif -pthread_t pthread_self(void); - -int pthread_equal(pthread_t, pthread_t); -#ifndef __cplusplus -#define pthread_equal(x,y) ((x)==(y)) -#endif - -int pthread_setcancelstate(int, int *); -int pthread_setcanceltype(int, int *); -void pthread_testcancel(void); -int pthread_cancel(pthread_t); - -int pthread_getschedparam(pthread_t, int *__restrict, struct sched_param *__restrict); -int pthread_setschedparam(pthread_t, int, const struct sched_param *); -int pthread_setschedprio(pthread_t, int); - -int pthread_once(pthread_once_t *, void (*)(void)); - -int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *__restrict); -int pthread_mutex_lock(pthread_mutex_t *); -int pthread_mutex_unlock(pthread_mutex_t *); -int pthread_mutex_trylock(pthread_mutex_t *); -int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict); -int pthread_mutex_destroy(pthread_mutex_t *); -int pthread_mutex_consistent(pthread_mutex_t *); -/** - * @brief lock the mutex object referenced by mutex. If the mutex is already locked, - * the calling thread shall block until the mutex becomes available as in the - * pthread_mutex_lock() function. If the mutex cannot be locked without waiting for - * another thread to unlock the mutex, this wait shall be terminated when the specified - * timeout expires. The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock. - * The resolution of the timeout shall be the resolution of the clock on which it is based. - * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock. - * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock. - * @param timespec the timeout shall expire specified by abstime passes. - * @return clocklock result. - * @retval 0 is returned on success. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict); -/** - * @brief lock the mutex object referenced by mutex. If the mutex is already locked, - * the calling thread shall block until the mutex becomes available as in the - * pthread_mutex_lock() function. If the mutex cannot be locked without waiting for - * another thread to unlock the mutex, this wait shall be terminated when the specified - * timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock. - * The resolution of the timeout shall be the resolution of the clock on which it is based. - * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock. - * @param timespec the timeout shall expire specified by abstime passes. - * @return clocklock result. - * @retval 0 is returned on success. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t *__restrict, const struct timespec *__restrict); -/** - * @brief lock the mutex object referenced by mutex. If the mutex is already locked, - * the calling thread shall block until the mutex becomes available as in the - * pthread_mutex_lock() function. If the mutex cannot be locked without waiting for - * another thread to unlock the mutex, this wait shall be terminated when the specified - * timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock. - * The resolution of the timeout shall be the resolution of the clock on which it is based. - * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock. - * @param ms the timeout shall expire specified by relative time(ms) passes. - * @return clocklock result. - * @retval 0 is returned on success. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int pthread_mutex_lock_timeout_np(pthread_mutex_t *__restrict, unsigned int); - -int pthread_mutex_getprioceiling(const pthread_mutex_t *__restrict, int *__restrict); -int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restrict); - -int pthread_cond_init(pthread_cond_t *__restrict, const pthread_condattr_t *__restrict); -int pthread_cond_destroy(pthread_cond_t *); -int pthread_cond_wait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict); -int pthread_cond_timedwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, const struct timespec *__restrict); -/** - * @brief The thread waits for a signal to trigger, and if timeout or signal is triggered, - * the thread wakes up. - * @param pthread_cond_t Condition variables for multithreading. - * @param pthread_mutex_t Thread mutex variable. - * @param clockid_t Clock ID used in clock and timer functions. - * @param timespec The timeout shall expire specified by abstime passes. - * @return pthread_cond_clockwait result. - * @retval 0 pthread_cond_clockwait successful. - * @retval ETIMEDOUT pthread_cond_clockwait Connection timed out. - * @retval EINVAL pthread_cond_clockwait error. - */ -int pthread_cond_clockwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, - clockid_t, const struct timespec *__restrict); - -/** - * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC. - * The thread waits for a signal to trigger, and if timeout or signal is triggered, - * the thread wakes up. - * @param pthread_cond_t Condition variables for multithreading. - * @param pthread_mutex_t Thread mutex variable. - * @param timespec The timeout shall expire specified by abstime passes. - * @return pthread_cond_timedwait_monotonic_np result. - * @retval 0 pthread_cond_timedwait_monotonic_np successful. - * @retval ETIMEDOUT pthread_cond_timedwait_monotonic_np Connection timed out. - * @retval EINVAL pthread_cond_timedwait_monotonic_np error. - */ -int pthread_cond_timedwait_monotonic_np(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, - const struct timespec *__restrict); - -/** - * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC and The time - * parameter is in milliseconds. The thread waits for a signal to trigger, and if timeout or - * signal is triggered, the thread wakes up. - * @param pthread_cond_t Condition variables for multithreading. - * @param pthread_mutex_t Thread mutex variable. - * @param unsigned Timeout, in milliseconds. - * @return pthread_cond_timeout_np result. - * @retval 0 pthread_cond_timeout_np successful. - * @retval ETIMEDOUT pthread_cond_timeout_np Connection timed out. - * @retval EINVAL pthread_cond_timeout_np error. - */ -int pthread_cond_timeout_np(pthread_cond_t* __restrict, pthread_mutex_t* __restrict, unsigned int); -int pthread_cond_broadcast(pthread_cond_t *); -int pthread_cond_signal(pthread_cond_t *); - -int pthread_rwlock_init(pthread_rwlock_t *__restrict, const pthread_rwlockattr_t *__restrict); -int pthread_rwlock_destroy(pthread_rwlock_t *); -int pthread_rwlock_rdlock(pthread_rwlock_t *); -int pthread_rwlock_tryrdlock(pthread_rwlock_t *); -int pthread_rwlock_timedrdlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict); -/** - * @brief Apply a read lock to the read-write lock referenced by rwlock as in the - * pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without - * waiting for other threads to unlock the lock, this wait shall be terminated when - * the specified timeout expires. The timeout shall expire when the absolute time specified by - * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time - * specified by abstime has already been passed at the time of the call. - * The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock. - * @param rw a read lock to the read-write lock referenced. - * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock. - * @param timespec the timeout shall expire specified by abstime passes. - * @return clockrdlock result. - * @retval 0 is returned on success. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int pthread_rwlock_clockrdlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict); -/** - * @brief Apply a read lock to the read-write lock referenced by rwlock as in the - * pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without - * waiting for other threads to unlock the lock, this wait shall be terminated when - * the specified timeout expires. The timeout shall expire when the absolute time specified by - * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time - * specified by abstime has already been passed at the time of the call. - * The timeout shall be based on the CLOCK_MONOTONIC clock. - * @param rw a read lock to the read-write lock referenced. - * @param timespec the timeout shall expire specified by abstime passes. - * @return clockrdlock result. - * @retval 0 is returned on success. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict); -int pthread_rwlock_wrlock(pthread_rwlock_t *); -int pthread_rwlock_trywrlock(pthread_rwlock_t *); -int pthread_rwlock_timedwrlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict); -int pthread_rwlock_unlock(pthread_rwlock_t *); -/** - * @brief Read-write lock variables have an initialization option to use CLOCK_MONOTONIC. - * apply a read lock to the read-write lock referenced by rwlock as in the - * pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without - * waiting for other threads to unlock the lock, this wait shall be terminated when - * the specified timeout expires. The timeout shall expire when the absolute time specified by - * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time - * specified by abstime has already been passed at the time of the call. - * The timeout shall be based on the CLOCK_MONOTONIC clock. - * @param rw a read lock to the read-write lock referenced. - * @param timespec the timeout shall expire specified by abstime passes. - * @return clockrdlock result. - * @retval 0 is returned on success. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict); - -/** - * @brief Apply a read lock to the read-write lock referenced by rwlock as in the - * pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without - * waiting for other threads to unlock the lock, this wait shall be terminated when - * the specified timeout expires. The timeout shall expire when the absolute time specified by - * abstime passes, as measured by the clock on which timeouts are based, or if the absolute time - * specified by abstime has already been passed at the time of the call. - * The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock. - * @param rw a read lock to the read-write lock referenced. - * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock. - * @param timespec the timeout shall expire specified by abstime passes. - * @return clockrdlock result. - * @retval 0 is returned on success. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int pthread_rwlock_clockwrlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict); - -int pthread_spin_init(pthread_spinlock_t *, int); -int pthread_spin_destroy(pthread_spinlock_t *); -int pthread_spin_lock(pthread_spinlock_t *); -int pthread_spin_trylock(pthread_spinlock_t *); -int pthread_spin_unlock(pthread_spinlock_t *); - -int pthread_barrier_init(pthread_barrier_t *__restrict, const pthread_barrierattr_t *__restrict, unsigned); -int pthread_barrier_destroy(pthread_barrier_t *); -int pthread_barrier_wait(pthread_barrier_t *); - -int pthread_key_create(pthread_key_t *, void (*)(void *)); -int pthread_key_delete(pthread_key_t); -void *pthread_getspecific(pthread_key_t); -int pthread_setspecific(pthread_key_t, const void *); - -int pthread_attr_init(pthread_attr_t *); -int pthread_attr_destroy(pthread_attr_t *); - -int pthread_attr_getguardsize(const pthread_attr_t *__restrict, size_t *__restrict); -int pthread_attr_setguardsize(pthread_attr_t *, size_t); -int pthread_attr_getstacksize(const pthread_attr_t *__restrict, size_t *__restrict); -int pthread_attr_setstacksize(pthread_attr_t *, size_t); -int pthread_attr_getdetachstate(const pthread_attr_t *, int *); -int pthread_attr_setdetachstate(pthread_attr_t *, int); -int pthread_attr_getstack(const pthread_attr_t *__restrict, void **__restrict, size_t *__restrict); -int pthread_attr_setstack(pthread_attr_t *, void *, size_t); -int pthread_attr_getscope(const pthread_attr_t *__restrict, int *__restrict); -int pthread_attr_setscope(pthread_attr_t *, int); -int pthread_attr_getschedpolicy(const pthread_attr_t *__restrict, int *__restrict); -int pthread_attr_setschedpolicy(pthread_attr_t *, int); -int pthread_attr_getschedparam(const pthread_attr_t *__restrict, struct sched_param *__restrict); -int pthread_attr_setschedparam(pthread_attr_t *__restrict, const struct sched_param *__restrict); -int pthread_attr_getinheritsched(const pthread_attr_t *__restrict, int *__restrict); -int pthread_attr_setinheritsched(pthread_attr_t *, int); - -int pthread_mutexattr_destroy(pthread_mutexattr_t *); -int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *__restrict, int *__restrict); -int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *__restrict, int *__restrict); -int pthread_mutexattr_getpshared(const pthread_mutexattr_t *__restrict, int *__restrict); -int pthread_mutexattr_getrobust(const pthread_mutexattr_t *__restrict, int *__restrict); -int pthread_mutexattr_gettype(const pthread_mutexattr_t *__restrict, int *__restrict); -int pthread_mutexattr_init(pthread_mutexattr_t *); -int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int); -int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int); -int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int); -int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int); -int pthread_mutexattr_settype(pthread_mutexattr_t *, int); - -int pthread_condattr_init(pthread_condattr_t *); -int pthread_condattr_destroy(pthread_condattr_t *); -int pthread_condattr_setclock(pthread_condattr_t *, clockid_t); -int pthread_condattr_setpshared(pthread_condattr_t *, int); -int pthread_condattr_getclock(const pthread_condattr_t *__restrict, clockid_t *__restrict); -int pthread_condattr_getpshared(const pthread_condattr_t *__restrict, int *__restrict); - -int pthread_rwlockattr_init(pthread_rwlockattr_t *); -int pthread_rwlockattr_destroy(pthread_rwlockattr_t *); -int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int); -int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *__restrict, int *__restrict); - -int pthread_barrierattr_destroy(pthread_barrierattr_t *); -int pthread_barrierattr_getpshared(const pthread_barrierattr_t *__restrict, int *__restrict); -int pthread_barrierattr_init(pthread_barrierattr_t *); -int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int); - -int pthread_atfork(void (*)(void), void (*)(void), void (*)(void)); - -int pthread_getconcurrency(void); -int pthread_setconcurrency(int); - -int pthread_getcpuclockid(pthread_t, clockid_t *); - -struct __ptcb { - void (*__f)(void *); - void *__x; - struct __ptcb *__next; -}; - -void _pthread_cleanup_push(struct __ptcb *, void (*)(void *), void *); -void _pthread_cleanup_pop(struct __ptcb *, int); - -#define pthread_cleanup_push(f, x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x); -#define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0) - -#ifdef _GNU_SOURCE -struct cpu_set_t; -int pthread_getaffinity_np(pthread_t, size_t, struct cpu_set_t *); -int pthread_setaffinity_np(pthread_t, size_t, const struct cpu_set_t *); -int pthread_getattr_np(pthread_t, pthread_attr_t *); -int pthread_setname_np(pthread_t, const char *); -int pthread_getname_np(pthread_t, char *, size_t); -int pthread_getattr_default_np(pthread_attr_t *); -int pthread_setattr_default_np(const pthread_attr_t *); -int pthread_tryjoin_np(pthread_t, void **); -int pthread_timedjoin_np(pthread_t, void **, const struct timespec *); -#endif - -#if _REDIR_TIME64 -__REDIR(pthread_mutex_timedlock, __pthread_mutex_timedlock_time64); -__REDIR(pthread_cond_timedwait, __pthread_cond_timedwait_time64); -__REDIR(pthread_rwlock_timedrdlock, __pthread_rwlock_timedrdlock_time64); -__REDIR(pthread_rwlock_timedwrlock, __pthread_rwlock_timedwrlock_time64); -#ifdef _GNU_SOURCE -__REDIR(pthread_timedjoin_np, __pthread_timedjoin_np_time64); -#endif -#endif - -#ifdef __cplusplus -} -#endif -#endif diff --git a/porting/linux/user/include/sched.h b/porting/linux/user/include/sched.h deleted file mode 100644 index a11fe2cfe..000000000 --- a/porting/linux/user/include/sched.h +++ /dev/null @@ -1,153 +0,0 @@ -#ifndef _SCHED_H -#define _SCHED_H -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define __NEED_struct_timespec -#define __NEED_pid_t -#define __NEED_time_t - -#ifdef _GNU_SOURCE -#define __NEED_size_t -#endif - -#include -#include -struct sched_param { - int sched_priority; - int __reserved1; -#if _REDIR_TIME64 - long __reserved2[4]; -#else - struct { - time_t __reserved1; - long __reserved2; - } __reserved2[2]; -#endif - int __reserved3; -}; - -int sched_get_priority_max(int); -int sched_get_priority_min(int); -int sched_getparam(pid_t, struct sched_param *); -int sched_getscheduler(pid_t); -int sched_rr_get_interval(pid_t, struct timespec *); -int sched_setparam(pid_t, const struct sched_param *); -int sched_setscheduler(pid_t, int, const struct sched_param *); -int sched_yield(void); - - -#define SCHED_OTHER 0 -#define SCHED_FIFO 1 -#define SCHED_RR 2 -#define SCHED_BATCH 3 -#define SCHED_IDLE 5 -#define SCHED_DEADLINE 6 -#define SCHED_RESET_ON_FORK 0x40000000 - -#ifdef _GNU_SOURCE -#define CSIGNAL 0x000000ff -#define CLONE_NEWTIME 0x00000080 -#define CLONE_VM 0x00000100 -#define CLONE_FS 0x00000200 -#define CLONE_FILES 0x00000400 -#define CLONE_SIGHAND 0x00000800 -#define CLONE_PIDFD 0x00001000 -#define CLONE_PTRACE 0x00002000 -#define CLONE_VFORK 0x00004000 -#define CLONE_PARENT 0x00008000 -#define CLONE_THREAD 0x00010000 -#define CLONE_NEWNS 0x00020000 -#define CLONE_SYSVSEM 0x00040000 -#define CLONE_SETTLS 0x00080000 -#define CLONE_PARENT_SETTID 0x00100000 -#define CLONE_CHILD_CLEARTID 0x00200000 -#define CLONE_DETACHED 0x00400000 -#define CLONE_UNTRACED 0x00800000 -#define CLONE_CHILD_SETTID 0x01000000 -#define CLONE_NEWCGROUP 0x02000000 -#define CLONE_NEWUTS 0x04000000 -#define CLONE_NEWIPC 0x08000000 -#define CLONE_NEWUSER 0x10000000 -#define CLONE_NEWPID 0x20000000 -#define CLONE_NEWNET 0x40000000 -#define CLONE_IO 0x80000000 -int clone (int (*)(void *), void *, int, void *, ...); -int unshare(int); -int setns(int, int); - -void *memcpy(void *__restrict, const void *__restrict, size_t); -int memcmp(const void *, const void *, size_t); -void *memset (void *, int, size_t); -void *calloc(size_t, size_t); -void free(void *); - -typedef struct cpu_set_t { unsigned long __bits[128/sizeof(long)]; } cpu_set_t; -cpu_set_t* __sched_cpualloc(size_t __count); -int __sched_cpucount(size_t, const cpu_set_t *); -int sched_getcpu(void); -int sched_getaffinity(pid_t, size_t, cpu_set_t *); -int sched_setaffinity(pid_t, size_t, const cpu_set_t *); - -#define __CPU_op_S(i, size, set, op) ( (i)/8U >= (size) ? 0 : \ - (((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) ) - -#define CPU_SET_S(i, size, set) __CPU_op_S(i, size, set, |=) -#define CPU_CLR_S(i, size, set) __CPU_op_S(i, size, set, &=~) -#define CPU_ISSET_S(i, size, set) __CPU_op_S(i, size, set, &) - -#define __CPU_op_func_S(func, op) \ -static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, \ - const cpu_set_t *__src1, const cpu_set_t *__src2) \ -{ \ - size_t __i; \ - for (__i=0; __i<__size/sizeof(long); __i++) \ - ((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] \ - op ((unsigned long *)__src2)[__i] ; \ -} - -__CPU_op_func_S(AND, &) -__CPU_op_func_S(OR, |) -__CPU_op_func_S(XOR, ^) - -#define CPU_AND_S(a,b,c,d) __CPU_AND_S(a,b,c,d) -#define CPU_OR_S(a,b,c,d) __CPU_OR_S(a,b,c,d) -#define CPU_XOR_S(a,b,c,d) __CPU_XOR_S(a,b,c,d) - -#define CPU_COUNT_S(size,set) __sched_cpucount(size,set) -#define CPU_ZERO_S(size,set) memset(set,0,size) -#define CPU_EQUAL_S(size,set1,set2) (!memcmp(set1,set2,size)) - -#define CPU_ALLOC_SIZE(n) (sizeof(long) * ( (n)/(8*sizeof(long)) \ - + ((n)%(8*sizeof(long)) + 8*sizeof(long)-1)/(8*sizeof(long)) ) ) -#define CPU_ALLOC(n) ((cpu_set_t *)calloc(1,CPU_ALLOC_SIZE(n))) -#define CPU_FREE(set) free(set) - -#define CPU_SETSIZE 128 - -#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set) -#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set) -#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set) -#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2) -#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2) -#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2) -#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set) -#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set) -#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2) - - -#endif - -#if _REDIR_TIME64 -__REDIR(sched_rr_get_interval, __sched_rr_get_interval_time64); -#endif - -#ifdef __cplusplus -} -#endif -#endif - - diff --git a/porting/linux/user/include/signal.h b/porting/linux/user/include/signal.h deleted file mode 100644 index 4e4dc686b..000000000 --- a/porting/linux/user/include/signal.h +++ /dev/null @@ -1,316 +0,0 @@ -#ifndef _SIGNAL_H -#define _SIGNAL_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) - -#ifdef _GNU_SOURCE -#define __ucontext ucontext -#endif - -#define __NEED_size_t -#define __NEED_pid_t -#define __NEED_uid_t -#define __NEED_struct_timespec -#define __NEED_pthread_t -#define __NEED_pthread_attr_t -#define __NEED_time_t -#define __NEED_clock_t -#define __NEED_sigset_t - -#include - -#define SIG_BLOCK 0 -#define SIG_UNBLOCK 1 -#define SIG_SETMASK 2 - -#define SI_ASYNCNL (-60) -#define SI_TKILL (-6) -#define SI_SIGIO (-5) -#define SI_ASYNCIO (-4) -#define SI_MESGQ (-3) -#define SI_TIMER (-2) -#define SI_QUEUE (-1) -#define SI_USER 0 -#define SI_KERNEL 128 - - -typedef struct sigaltstack stack_t; - -#endif - -#include - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) - -#define SIG_HOLD ((void (*)(int)) 2) - -#define FPE_INTDIV 1 -#define FPE_INTOVF 2 -#define FPE_FLTDIV 3 -#define FPE_FLTOVF 4 -#define FPE_FLTUND 5 -#define FPE_FLTRES 6 -#define FPE_FLTINV 7 -#define FPE_FLTSUB 8 - -#define ILL_ILLOPC 1 -#define ILL_ILLOPN 2 -#define ILL_ILLADR 3 -#define ILL_ILLTRP 4 -#define ILL_PRVOPC 5 -#define ILL_PRVREG 6 -#define ILL_COPROC 7 -#define ILL_BADSTK 8 - -#define SEGV_MAPERR 1 -#define SEGV_ACCERR 2 -#define SEGV_BNDERR 3 -#define SEGV_PKUERR 4 -#define SEGV_MTEAERR 8 -#define SEGV_MTESERR 9 - -#define BUS_ADRALN 1 -#define BUS_ADRERR 2 -#define BUS_OBJERR 3 -#define BUS_MCEERR_AR 4 -#define BUS_MCEERR_AO 5 - -#define CLD_EXITED 1 -#define CLD_KILLED 2 -#define CLD_DUMPED 3 -#define CLD_TRAPPED 4 -#define CLD_STOPPED 5 -#define CLD_CONTINUED 6 - -union sigval { - int sival_int; - void *sival_ptr; -}; - -typedef struct { -#ifdef __SI_SWAP_ERRNO_CODE - int si_signo, si_code, si_errno; -#else - int si_signo, si_errno, si_code; -#endif - union { - char __pad[128 - 2*sizeof(int) - sizeof(long)]; - struct { - union { - struct { - pid_t si_pid; - uid_t si_uid; - } __piduid; - struct { - int si_timerid; - int si_overrun; - } __timer; - } __first; - union { - union sigval si_value; - struct { - int si_status; - clock_t si_utime, si_stime; - } __sigchld; - } __second; - } __si_common; - struct { - void *si_addr; - short si_addr_lsb; - union { - struct { - void *si_lower; - void *si_upper; - } __addr_bnd; - unsigned si_pkey; - } __first; - } __sigfault; - struct { - long si_band; - int si_fd; - } __sigpoll; - struct { - void *si_call_addr; - int si_syscall; - unsigned si_arch; - } __sigsys; - } __si_fields; -} siginfo_t; -#define si_pid __si_fields.__si_common.__first.__piduid.si_pid -#define si_uid __si_fields.__si_common.__first.__piduid.si_uid -#define si_status __si_fields.__si_common.__second.__sigchld.si_status -#define si_utime __si_fields.__si_common.__second.__sigchld.si_utime -#define si_stime __si_fields.__si_common.__second.__sigchld.si_stime -#define si_value __si_fields.__si_common.__second.si_value -#define si_addr __si_fields.__sigfault.si_addr -#define si_addr_lsb __si_fields.__sigfault.si_addr_lsb -#define si_lower __si_fields.__sigfault.__first.__addr_bnd.si_lower -#define si_upper __si_fields.__sigfault.__first.__addr_bnd.si_upper -#define si_pkey __si_fields.__sigfault.__first.si_pkey -#define si_band __si_fields.__sigpoll.si_band -#define si_fd __si_fields.__sigpoll.si_fd -#define si_timerid __si_fields.__si_common.__first.__timer.si_timerid -#define si_overrun __si_fields.__si_common.__first.__timer.si_overrun -#define si_ptr si_value.sival_ptr -#define si_int si_value.sival_int -#define si_call_addr __si_fields.__sigsys.si_call_addr -#define si_syscall __si_fields.__sigsys.si_syscall -#define si_arch __si_fields.__sigsys.si_arch - -struct sigaction { - union { - void (*sa_handler)(int); - void (*sa_sigaction)(int, siginfo_t *, void *); - } __sa_handler; - sigset_t sa_mask; - int sa_flags; - void (*sa_restorer)(void); -}; -#define sa_handler __sa_handler.sa_handler -#define sa_sigaction __sa_handler.sa_sigaction - -#define SA_UNSUPPORTED 0x00000400 -#define SA_EXPOSE_TAGBITS 0x00000800 - -struct sigevent { - union sigval sigev_value; - int sigev_signo; - int sigev_notify; - union { - char __pad[64 - 2*sizeof(int) - sizeof(union sigval)]; - pid_t sigev_notify_thread_id; - struct { - void (*sigev_notify_function)(union sigval); - pthread_attr_t *sigev_notify_attributes; - } __sev_thread; - } __sev_fields; -}; - -#define sigev_notify_thread_id __sev_fields.sigev_notify_thread_id -#define sigev_notify_function __sev_fields.__sev_thread.sigev_notify_function -#define sigev_notify_attributes __sev_fields.__sev_thread.sigev_notify_attributes - -#define SIGEV_SIGNAL 0 -#define SIGEV_NONE 1 -#define SIGEV_THREAD 2 -#define SIGEV_THREAD_ID 4 - -int __libc_current_sigrtmin(void); -int __libc_current_sigrtmax(void); - -#define SIGRTMIN (__libc_current_sigrtmin()) -#define SIGRTMAX (__libc_current_sigrtmax()) - -#define MUSL_SIGNAL_NATIVE_REMOTE (SIGRTMIN + 0) -#define MUSL_SIGNAL_HOOK (SIGRTMIN + 1) -#define MUSL_SIGNAL_UNHOOK (SIGRTMIN + 2) -#define MUSL_SIGNAL_NATIVE_LOCAL (SIGRTMIN + 3) -#define MUSL_SIGNAL_JSHEAP (SIGRTMIN + 4) -#define MUSL_SIGNAL_JSHEAP_PRIV (SIGRTMIN + 5) -#define MUSL_SIGNAL_LEAK_STACK (SIGRTMIN + 7) -#ifdef USE_JEMALLOC_RECYCLE_FUNC -#define MUSL_SIGNAL_RECYCLE_JEMALLOC (SIGRTMIN + 8) -#endif -#define MUSL_SIGNAL_MEMCHECK (SIGRTMIN + 9) - -int kill(pid_t, int); -int sigemptyset(sigset_t *); -int sigfillset(sigset_t *); -int sigaddset(sigset_t *, int); -int sigdelset(sigset_t *, int); -int sigismember(const sigset_t *, int); - -int sigprocmask(int, const sigset_t *__restrict, sigset_t *__restrict); -int sigsuspend(const sigset_t *); -int sigaction(int, const struct sigaction *__restrict, struct sigaction *__restrict); -int sigpending(sigset_t *); -int sigwait(const sigset_t *__restrict, int *__restrict); -int sigwaitinfo(const sigset_t *__restrict, siginfo_t *__restrict); -int sigtimedwait(const sigset_t *__restrict, siginfo_t *__restrict, const struct timespec *__restrict); -int sigqueue(pid_t, int, union sigval); - -int pthread_sigmask(int, const sigset_t *__restrict, sigset_t *__restrict); -int pthread_kill(pthread_t, int); - -void psiginfo(const siginfo_t *, const char *); -void psignal(int, const char *); - -#endif - -#if defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE) -int killpg(pid_t, int); -int sigaltstack(const stack_t *__restrict, stack_t *__restrict); -int sighold(int); -int sigignore(int); -int siginterrupt(int, int); -int sigpause(int); -int sigrelse(int); -void (*sigset(int, void (*)(int)))(int); -#define TRAP_BRKPT 1 -#define TRAP_TRACE 2 -#define TRAP_BRANCH 3 -#define TRAP_HWBKPT 4 -#define TRAP_UNK 5 -#define POLL_IN 1 -#define POLL_OUT 2 -#define POLL_MSG 3 -#define POLL_ERR 4 -#define POLL_PRI 5 -#define POLL_HUP 6 -#define SS_ONSTACK 1 -#define SS_DISABLE 2 -#define SS_AUTODISARM (1U << 31) -#define SS_FLAG_BITS SS_AUTODISARM -#endif - -#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE) -#define NSIG _NSIG -typedef void (*sig_t)(int); - -#define SYS_SECCOMP 1 -#define SYS_USER_DISPATCH 2 -#endif - -#ifdef _GNU_SOURCE -typedef void (*sighandler_t)(int); -void (*bsd_signal(int, void (*)(int)))(int); -int sigisemptyset(const sigset_t *); -int sigorset (sigset_t *, const sigset_t *, const sigset_t *); -int sigandset(sigset_t *, const sigset_t *, const sigset_t *); - -#define SA_NOMASK SA_NODEFER -#define SA_ONESHOT SA_RESETHAND -#endif - -#define SIG_ERR ((void (*)(int))-1) -#define SIG_DFL ((void (*)(int)) 0) -#define SIG_IGN ((void (*)(int)) 1) - -typedef int sig_atomic_t; - -void (*signal(int, void (*)(int)))(int); -int raise(int); - -#if _REDIR_TIME64 -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -__REDIR(sigtimedwait, __sigtimedwait_time64); -#endif -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/stdio.h b/porting/linux/user/include/stdio.h deleted file mode 100644 index 8692c596f..000000000 --- a/porting/linux/user/include/stdio.h +++ /dev/null @@ -1,226 +0,0 @@ -#ifndef _STDIO_H -#define _STDIO_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define __NEED_FILE -#define __NEED___isoc_va_list -#define __NEED_size_t - -#if __STDC_VERSION__ < 201112L -#define __NEED_struct__IO_FILE -#endif - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -#define __NEED_ssize_t -#define __NEED_off_t -#define __NEED_va_list -#endif - -#include - -#if __cplusplus >= 201103L -#define NULL nullptr -#elif defined(__cplusplus) -#define NULL 0L -#else -#define NULL ((void*)0) -#endif - -#undef EOF -#define EOF (-1) - -#undef SEEK_SET -#undef SEEK_CUR -#undef SEEK_END -#define SEEK_SET 0 -#define SEEK_CUR 1 -#define SEEK_END 2 - -#define _IOFBF 0 -#define _IOLBF 1 -#define _IONBF 2 - -#define BUFSIZ 1024 -#define FILENAME_MAX 4096 -#define FOPEN_MAX 1000 -#define TMP_MAX 10000 -#define L_tmpnam 20 - -typedef union _G_fpos64_t { - char __opaque[16]; - long long __lldata; - double __align; -} fpos_t; - -extern FILE *const stdin; -extern FILE *const stdout; -extern FILE *const stderr; - -#define stdin (stdin) -#define stdout (stdout) -#define stderr (stderr) - -FILE *fopen(const char *__restrict, const char *__restrict); -FILE *freopen(const char *__restrict, const char *__restrict, FILE *__restrict); -int fclose(FILE *); - -int remove(const char *); -int rename(const char *, const char *); - -int feof(FILE *); -int ferror(FILE *); -int fflush(FILE *); -void clearerr(FILE *); - -int fseek(FILE *, long, int); -long ftell(FILE *); -void rewind(FILE *); - -int fgetpos(FILE *__restrict, fpos_t *__restrict); -int fsetpos(FILE *, const fpos_t *); - -size_t fread(void *__restrict, size_t, size_t, FILE *__restrict); -size_t fwrite(const void *__restrict, size_t, size_t, FILE *__restrict); - -int fgetc(FILE *); -int getc(FILE *); -int getchar(void); -int ungetc(int, FILE *); - -int fputc(int, FILE *); -int putc(int, FILE *); -int putchar(int); - -char *fgets(char *__restrict, int, FILE *__restrict); -#if __STDC_VERSION__ < 201112L -char *gets(char *); -#endif - -int fputs(const char *__restrict, FILE *__restrict); -int puts(const char *); - -int printf(const char *__restrict, ...); -int fprintf(FILE *__restrict, const char *__restrict, ...); -int sprintf(char *__restrict, const char *__restrict, ...); -int snprintf(char *__restrict, size_t, const char *__restrict, ...); - -int vprintf(const char *__restrict, __isoc_va_list); -int vfprintf(FILE *__restrict, const char *__restrict, __isoc_va_list); -int vsprintf(char *__restrict, const char *__restrict, __isoc_va_list); -int vsnprintf(char *__restrict, size_t, const char *__restrict, __isoc_va_list); - -int scanf(const char *__restrict, ...); -int fscanf(FILE *__restrict, const char *__restrict, ...); -int sscanf(const char *__restrict, const char *__restrict, ...); -int vscanf(const char *__restrict, __isoc_va_list); -int vfscanf(FILE *__restrict, const char *__restrict, __isoc_va_list); -int vsscanf(const char *__restrict, const char *__restrict, __isoc_va_list); - -void perror(const char *); - -int setvbuf(FILE *__restrict, char *__restrict, int, size_t); -void setbuf(FILE *__restrict, char *__restrict); - -char *tmpnam(char *); -FILE *tmpfile(void); - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -FILE *fmemopen(void *__restrict, size_t, const char *__restrict); -FILE *open_memstream(char **, size_t *); -FILE *fdopen(int, const char *); -FILE *popen(const char *, const char *); -int pclose(FILE *); -int fileno(FILE *); -int fseeko(FILE *, off_t, int); -off_t ftello(FILE *); -int dprintf(int, const char *__restrict, ...); -int vdprintf(int, const char *__restrict, __isoc_va_list); -void flockfile(FILE *); -int ftrylockfile(FILE *); -void funlockfile(FILE *); -int getc_unlocked(FILE *); -int getchar_unlocked(void); -int putc_unlocked(int, FILE *); -int putchar_unlocked(int); -ssize_t getdelim(char **__restrict, size_t *__restrict, int, FILE *__restrict); -ssize_t getline(char **__restrict, size_t *__restrict, FILE *__restrict); -int renameat(int, const char *, int, const char *); -char *ctermid(char *); -#define L_ctermid 20 -#endif - - -#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -#define P_tmpdir "/tmp" -char *tempnam(const char *, const char *); -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define L_cuserid 20 -char *cuserid(char *); -void setlinebuf(FILE *); -void setbuffer(FILE *, char *, size_t); -int fgetc_unlocked(FILE *); -int fputc_unlocked(int, FILE *); -int fflush_unlocked(FILE *); -size_t fread_unlocked(void *, size_t, size_t, FILE *); -size_t fwrite_unlocked(const void *, size_t, size_t, FILE *); -void clearerr_unlocked(FILE *); -int feof_unlocked(FILE *); -int ferror_unlocked(FILE *); -int fileno_unlocked(FILE *); -int getw(FILE *); -int putw(int, FILE *); -char *fgetln(FILE *, size_t *); -int asprintf(char **, const char *, ...); -int vasprintf(char **, const char *, __isoc_va_list); -#endif - -#ifdef _GNU_SOURCE -char *fgets_unlocked(char *, int, FILE *); -int fputs_unlocked(const char *, FILE *); - -typedef ssize_t (cookie_read_function_t)(void *, char *, size_t); -typedef ssize_t (cookie_write_function_t)(void *, const char *, size_t); -typedef int (cookie_seek_function_t)(void *, off_t *, int); -typedef int (cookie_close_function_t)(void *); - -typedef struct _IO_cookie_io_functions_t { - cookie_read_function_t *read; - cookie_write_function_t *write; - cookie_seek_function_t *seek; - cookie_close_function_t *close; -} cookie_io_functions_t; - -FILE *fopencookie(void *, const char *, cookie_io_functions_t); -#endif - -#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) -#define tmpfile64 tmpfile -#define fopen64 fopen -#define freopen64 freopen -#define fseeko64 fseeko -#define ftello64 ftello -#define fgetpos64 fgetpos -#define fsetpos64 fsetpos -#define fpos64_t fpos_t -#define off64_t off_t -#endif - -#include - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/stdlib.h b/porting/linux/user/include/stdlib.h deleted file mode 100644 index a944bd5d6..000000000 --- a/porting/linux/user/include/stdlib.h +++ /dev/null @@ -1,185 +0,0 @@ -#ifndef _STDLIB_H -#define _STDLIB_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#if __cplusplus >= 201103L -#define NULL nullptr -#elif defined(__cplusplus) -#define NULL 0L -#else -#define NULL ((void*)0) -#endif - -#define __NEED_size_t -#define __NEED_wchar_t - -#include - -int atoi (const char *); -long atol (const char *); -long long atoll (const char *); -double atof (const char *); - -float strtof (const char *__restrict, char **__restrict); -double strtod (const char *__restrict, char **__restrict); -long double strtold (const char *__restrict, char **__restrict); - -long strtol (const char *__restrict, char **__restrict, int); -unsigned long strtoul (const char *__restrict, char **__restrict, int); -long long strtoll (const char *__restrict, char **__restrict, int); -unsigned long long strtoull (const char *__restrict, char **__restrict, int); - -int rand (void); -void srand (unsigned); - -void *malloc (size_t); -void *calloc (size_t, size_t); -void *realloc (void *, size_t); -void free (void *); -void *aligned_alloc(size_t, size_t); - -_Noreturn void abort (void); -int atexit (void (*) (void)); -_Noreturn void exit (int); -_Noreturn void _Exit (int); -int at_quick_exit (void (*) (void)); -_Noreturn void quick_exit (int); - -char *getenv (const char *); - -int system (const char *); - -void *bsearch (const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); -void qsort (void *, size_t, size_t, int (*)(const void *, const void *)); - -int abs (int); -long labs (long); -long long llabs (long long); - -typedef struct { int quot, rem; } div_t; -typedef struct { long quot, rem; } ldiv_t; -typedef struct { long long quot, rem; } lldiv_t; - -div_t div (int, int); -ldiv_t ldiv (long, long); -lldiv_t lldiv (long long, long long); - -int mblen (const char *, size_t); -int mbtowc (wchar_t *__restrict, const char *__restrict, size_t); -int wctomb (char *, wchar_t); -size_t mbstowcs (wchar_t *__restrict, const char *__restrict, size_t); -size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t); - -unsigned int arc4random(void); -unsigned int arc4random_uniform(unsigned int); -void arc4random_buf(void *, size_t); - -#define EXIT_FAILURE 1 -#define EXIT_SUCCESS 0 - -size_t __ctype_get_mb_cur_max(void); -#define MB_CUR_MAX (__ctype_get_mb_cur_max()) - -#define RAND_MAX (0x7fffffff) - - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) - -#define WNOHANG 1 -#define WUNTRACED 2 - -#define WEXITSTATUS(s) (((s) & 0xff00) >> 8) -#define WTERMSIG(s) ((s) & 0x7f) -#define WSTOPSIG(s) WEXITSTATUS(s) -#define WIFEXITED(s) (!WTERMSIG(s)) -#define WIFSTOPPED(s) ((short)((((s)&0xffff)*0x10001)>>8) > 0x7f00) -#define WIFSIGNALED(s) (((s)&0xffff)-1U < 0xffu) - -int posix_memalign (void **, size_t, size_t); -int setenv (const char *, const char *, int); -int unsetenv (const char *); -int mkstemp (char *); -int mkostemp (char *, int); -char *mkdtemp (char *); -int getsubopt (char **, char *const *, char **); -int rand_r (unsigned *); - -#endif - - -#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -char *realpath (const char *__restrict, char *__restrict); -long int random (void); -void srandom (unsigned int); -char *initstate (unsigned int, char *, size_t); -char *setstate (char *); -int putenv (char *); -int posix_openpt (int); -int grantpt (int); -int unlockpt (int); -char *ptsname (int); -char *l64a (long); -long a64l (const char *); -void setkey (const char *); -double drand48 (void); -double erand48 (unsigned short [3]); -long int lrand48 (void); -long int nrand48 (unsigned short [3]); -long mrand48 (void); -long jrand48 (unsigned short [3]); -void srand48 (long); -unsigned short *seed48 (unsigned short [3]); -void lcong48 (unsigned short [7]); -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#include -char *mktemp (char *); -int mkstemps (char *, int); -int mkostemps (char *, int, int); -void *valloc (size_t); -void *memalign(size_t, size_t); -int getloadavg(double *, int); -int clearenv(void); -#define WCOREDUMP(s) ((s) & 0x80) -#define WIFCONTINUED(s) ((s) == 0xffff) -void *reallocarray (void *, size_t, size_t); -void qsort_r (void *, size_t, size_t, int (*)(const void *, const void *, void *), void *); -#endif - -#ifdef _GNU_SOURCE -int ptsname_r(int, char *, size_t); -char *ecvt(double, int, int *, int *); -char *fcvt(double, int, int *, int *); -char *gcvt(double, int, char *); -char *secure_getenv(const char *); -struct __locale_struct; -float strtof_l(const char *__restrict, char **__restrict, struct __locale_struct *); -double strtod_l(const char *__restrict, char **__restrict, struct __locale_struct *); -long double strtold_l(const char *__restrict, char **__restrict, struct __locale_struct *); -#endif - -#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) -#define mkstemp64 mkstemp -#define mkostemp64 mkostemp -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define mkstemps64 mkstemps -#define mkostemps64 mkostemps -#endif -#endif - -#include - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/string.h b/porting/linux/user/include/string.h deleted file mode 100644 index 15776855b..000000000 --- a/porting/linux/user/include/string.h +++ /dev/null @@ -1,109 +0,0 @@ -#ifndef _STRING_H -#define _STRING_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#if __cplusplus >= 201103L -#define NULL nullptr -#elif defined(__cplusplus) -#define NULL 0L -#else -#define NULL ((void*)0) -#endif - -#define __NEED_size_t -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -#define __NEED_locale_t -#endif - -#include - -void *memcpy (void *__restrict, const void *__restrict, size_t); -void *memmove (void *, const void *, size_t); -void *memset (void *, int, size_t); -int memcmp (const void *, const void *, size_t); -void *memchr (const void *, int, size_t); - -char *strcpy (char *__restrict, const char *__restrict); -char *strncpy (char *__restrict, const char *__restrict, size_t); - -char *strcat (char *__restrict, const char *__restrict); -char *strncat (char *__restrict, const char *__restrict, size_t); - -int strcmp (const char *, const char *); -int strncmp (const char *, const char *, size_t); - -int strcoll (const char *, const char *); -size_t strxfrm (char *__restrict, const char *__restrict, size_t); - -char *strchr (const char *, int); -char *strrchr (const char *, int); - -size_t strcspn (const char *, const char *); -size_t strspn (const char *, const char *); -char *strpbrk (const char *, const char *); -char *strstr (const char *, const char *); -char *strtok (char *__restrict, const char *__restrict); - -size_t strlen (const char *); - -char *strerror (int); - -#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE) -#include -#endif - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -char *strtok_r (char *__restrict, const char *__restrict, char **__restrict); -int strerror_r (int, char *, size_t); -char *stpcpy(char *__restrict, const char *__restrict); -char *stpncpy(char *__restrict, const char *__restrict, size_t); -size_t strnlen (const char *, size_t); -char *strdup (const char *); -char *strndup (const char *, size_t); -char *strsignal(int); -char *strerror_l (int, locale_t); -int strcoll_l (const char *, const char *, locale_t); -size_t strxfrm_l (char *__restrict, const char *__restrict, size_t, locale_t); -#endif - -#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ - || defined(_BSD_SOURCE) -void *memccpy (void *__restrict, const void *__restrict, int, size_t); -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -char *strsep(char **, const char *); -size_t strlcat (char *, const char *, size_t); -size_t strlcpy (char *, const char *, size_t); -void explicit_bzero (void *, size_t); -#endif - -#ifdef _GNU_SOURCE -#define strdupa(x) strcpy(alloca(strlen(x)+1),x) -int strverscmp (const char *, const char *); -char *strchrnul(const char *, int); -char *strcasestr(const char *, const char *); -void *memmem(const void *, size_t, const void *, size_t); -void *memrchr(const void *, int, size_t); -void *mempcpy(void *, const void *, size_t); -#ifndef __cplusplus -char *basename(); -#endif -#endif - -#include - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/sys/reboot.h b/porting/linux/user/include/sys/reboot.h deleted file mode 100644 index 330d5f5ab..000000000 --- a/porting/linux/user/include/sys/reboot.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef _SYS_REBOOT_H -#define _SYS_REBOOT_H -#ifdef __cplusplus -extern "C" -{ -#endif - -#define RB_AUTOBOOT 0x01234567 -#define RB_HALT_SYSTEM 0xcdef0123 -#define RB_ENABLE_CAD 0x89abcdef -#define RB_DISABLE_CAD 0 -#define RB_POWER_OFF 0x4321fedc -#define RB_SW_SUSPEND 0xd000fce2 -#define RB_KEXEC 0x45584543 -#define RB_MAGIC1 0xfee1dead -#define RB_MAGIC2 672274793 - -/** - * @brief reboots the device, or enables/disables the reboot keystroke. - * @param type commands accepted by the reboot() system call. - * -- RESTART Restart system using default command and mode. - * -- HALT Stop OS and give system control to ROM monitor, if any. - * -- CAD_ON Ctrl-Alt-Del sequence causes RESTART command. - * -- CAD_OFF Ctrl-Alt-Del sequence sends SIGINT to init task. - * -- POWER_OFF Stop OS and remove all power from system, if possible. - * -- RESTART2 Restart system using given command string. - * -- SW_SUSPEND Suspend system using software suspend if compiled in. - * -- KEXEC Restart system using a previously loaded Linux kernel. - * @return reboot result. - * @retval 0 is returned on success, if CAD was successfully enabled/disabled. - * @retval -1 is returned on failure, and errno is set to indicate the error. - */ -int reboot(int); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/porting/linux/user/include/sys/socket.h b/porting/linux/user/include/sys/socket.h deleted file mode 100644 index 111a90458..000000000 --- a/porting/linux/user/include/sys/socket.h +++ /dev/null @@ -1,423 +0,0 @@ -#ifndef _SYS_SOCKET_H -#define _SYS_SOCKET_H -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define __NEED_socklen_t -#define __NEED_sa_family_t -#define __NEED_size_t -#define __NEED_ssize_t -#define __NEED_uid_t -#define __NEED_pid_t -#define __NEED_gid_t -#define __NEED_struct_iovec - -#include - -#include - -#ifdef __MUSL__ -#include -#endif - -struct msghdr { - void *msg_name; - socklen_t msg_namelen; - struct iovec *msg_iov; -#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __BIG_ENDIAN - int __pad1; -#endif - int msg_iovlen; -#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __LITTLE_ENDIAN - int __pad1; -#endif - void *msg_control; -#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __BIG_ENDIAN - int __pad2; -#endif - socklen_t msg_controllen; -#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __LITTLE_ENDIAN - int __pad2; -#endif - int msg_flags; -}; - -struct cmsghdr { -#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __BIG_ENDIAN - int __pad1; -#endif - socklen_t cmsg_len; -#if __LONG_MAX > 0x7fffffff && __BYTE_ORDER == __LITTLE_ENDIAN - int __pad1; -#endif - int cmsg_level; - int cmsg_type; -}; - -#ifdef _GNU_SOURCE -struct ucred { - pid_t pid; - uid_t uid; - gid_t gid; -}; - -struct mmsghdr { - struct msghdr msg_hdr; - unsigned int msg_len; -}; - -struct timespec; - -int sendmmsg (int, struct mmsghdr *, unsigned int, unsigned int); -int recvmmsg (int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *); -#endif - -struct linger { - int l_onoff; - int l_linger; -}; - -#define SHUT_RD 0 -#define SHUT_WR 1 -#define SHUT_RDWR 2 - -#ifndef SOCK_STREAM -#define SOCK_STREAM 1 -#define SOCK_DGRAM 2 -#endif - -#define SOCK_RAW 3 -#define SOCK_RDM 4 -#define SOCK_SEQPACKET 5 -#define SOCK_DCCP 6 -#define SOCK_PACKET 10 - -#ifndef SOCK_CLOEXEC -#define SOCK_CLOEXEC 02000000 -#define SOCK_NONBLOCK 04000 -#endif - -#define PF_UNSPEC 0 -#define PF_LOCAL 1 -#define PF_UNIX PF_LOCAL -#define PF_FILE PF_LOCAL -#define PF_INET 2 -#define PF_AX25 3 -#define PF_IPX 4 -#define PF_APPLETALK 5 -#define PF_NETROM 6 -#define PF_BRIDGE 7 -#define PF_ATMPVC 8 -#define PF_X25 9 -#define PF_INET6 10 -#define PF_ROSE 11 -#define PF_DECnet 12 -#define PF_NETBEUI 13 -#define PF_SECURITY 14 -#define PF_KEY 15 -#define PF_NETLINK 16 -#define PF_ROUTE PF_NETLINK -#define PF_PACKET 17 -#define PF_ASH 18 -#define PF_ECONET 19 -#define PF_ATMSVC 20 -#define PF_RDS 21 -#define PF_SNA 22 -#define PF_IRDA 23 -#define PF_PPPOX 24 -#define PF_WANPIPE 25 -#define PF_LLC 26 -#define PF_IB 27 -#define PF_MPLS 28 -#define PF_CAN 29 -#define PF_TIPC 30 -#define PF_BLUETOOTH 31 -#define PF_IUCV 32 -#define PF_RXRPC 33 -#define PF_ISDN 34 -#define PF_PHONET 35 -#define PF_IEEE802154 36 -#define PF_CAIF 37 -#define PF_ALG 38 -#define PF_NFC 39 -#define PF_VSOCK 40 -#define PF_KCM 41 -#define PF_QIPCRTR 42 -#define PF_SMC 43 -#define PF_XDP 44 -#define PF_MAX 45 - -#define AF_UNSPEC PF_UNSPEC -#define AF_LOCAL PF_LOCAL -#define AF_UNIX AF_LOCAL -#define AF_FILE AF_LOCAL -#define AF_INET PF_INET -#define AF_AX25 PF_AX25 -#define AF_IPX PF_IPX -#define AF_APPLETALK PF_APPLETALK -#define AF_NETROM PF_NETROM -#define AF_BRIDGE PF_BRIDGE -#define AF_ATMPVC PF_ATMPVC -#define AF_X25 PF_X25 -#define AF_INET6 PF_INET6 -#define AF_ROSE PF_ROSE -#define AF_DECnet PF_DECnet -#define AF_NETBEUI PF_NETBEUI -#define AF_SECURITY PF_SECURITY -#define AF_KEY PF_KEY -#define AF_NETLINK PF_NETLINK -#define AF_ROUTE PF_ROUTE -#define AF_PACKET PF_PACKET -#define AF_ASH PF_ASH -#define AF_ECONET PF_ECONET -#define AF_ATMSVC PF_ATMSVC -#define AF_RDS PF_RDS -#define AF_SNA PF_SNA -#define AF_IRDA PF_IRDA -#define AF_PPPOX PF_PPPOX -#define AF_WANPIPE PF_WANPIPE -#define AF_LLC PF_LLC -#define AF_IB PF_IB -#define AF_MPLS PF_MPLS -#define AF_CAN PF_CAN -#define AF_TIPC PF_TIPC -#define AF_BLUETOOTH PF_BLUETOOTH -#define AF_IUCV PF_IUCV -#define AF_RXRPC PF_RXRPC -#define AF_ISDN PF_ISDN -#define AF_PHONET PF_PHONET -#define AF_IEEE802154 PF_IEEE802154 -#define AF_CAIF PF_CAIF -#define AF_ALG PF_ALG -#define AF_NFC PF_NFC -#define AF_VSOCK PF_VSOCK -#define AF_KCM PF_KCM -#define AF_QIPCRTR PF_QIPCRTR -#define AF_SMC PF_SMC -#define AF_XDP PF_XDP -#define AF_MAX PF_MAX - -#ifndef SO_DEBUG -#define SO_DEBUG 1 -#define SO_REUSEADDR 2 -#define SO_TYPE 3 -#define SO_ERROR 4 -#define SO_DONTROUTE 5 -#define SO_BROADCAST 6 -#define SO_SNDBUF 7 -#define SO_RCVBUF 8 -#define SO_KEEPALIVE 9 -#define SO_OOBINLINE 10 -#define SO_NO_CHECK 11 -#define SO_PRIORITY 12 -#define SO_LINGER 13 -#define SO_BSDCOMPAT 14 -#define SO_REUSEPORT 15 -#define SO_PASSCRED 16 -#define SO_PEERCRED 17 -#define SO_RCVLOWAT 18 -#define SO_SNDLOWAT 19 -#define SO_ACCEPTCONN 30 -#define SO_PEERSEC 31 -#define SO_SNDBUFFORCE 32 -#define SO_RCVBUFFORCE 33 -#define SO_PROTOCOL 38 -#define SO_DOMAIN 39 -#endif - -#ifndef SO_RCVTIMEO -#if __LONG_MAX == 0x7fffffff -#define SO_RCVTIMEO 66 -#define SO_SNDTIMEO 67 -#else -#define SO_RCVTIMEO 20 -#define SO_SNDTIMEO 21 -#endif -#endif - -#ifndef SO_TIMESTAMP -#if __LONG_MAX == 0x7fffffff -#define SO_TIMESTAMP 63 -#define SO_TIMESTAMPNS 64 -#define SO_TIMESTAMPING 65 -#else -#define SO_TIMESTAMP 29 -#define SO_TIMESTAMPNS 35 -#define SO_TIMESTAMPING 37 -#endif -#endif - -#define SO_SECURITY_AUTHENTICATION 22 -#define SO_SECURITY_ENCRYPTION_TRANSPORT 23 -#define SO_SECURITY_ENCRYPTION_NETWORK 24 - -#define SO_BINDTODEVICE 25 - -#define SO_ATTACH_FILTER 26 -#define SO_DETACH_FILTER 27 -#define SO_GET_FILTER SO_ATTACH_FILTER - -#define SO_PEERNAME 28 -#define SCM_TIMESTAMP SO_TIMESTAMP -#define SO_PASSSEC 34 -#define SCM_TIMESTAMPNS SO_TIMESTAMPNS -#define SO_MARK 36 -#define SCM_TIMESTAMPING SO_TIMESTAMPING -#define SO_RXQ_OVFL 40 -#define SO_WIFI_STATUS 41 -#define SCM_WIFI_STATUS SO_WIFI_STATUS -#define SO_PEEK_OFF 42 -#define SO_NOFCS 43 -#define SO_LOCK_FILTER 44 -#define SO_SELECT_ERR_QUEUE 45 -#define SO_BUSY_POLL 46 -#define SO_MAX_PACING_RATE 47 -#define SO_BPF_EXTENSIONS 48 -#define SO_INCOMING_CPU 49 -#define SO_ATTACH_BPF 50 -#define SO_DETACH_BPF SO_DETACH_FILTER -#define SO_ATTACH_REUSEPORT_CBPF 51 -#define SO_ATTACH_REUSEPORT_EBPF 52 -#define SO_CNX_ADVICE 53 -#define SCM_TIMESTAMPING_OPT_STATS 54 -#define SO_MEMINFO 55 -#define SO_INCOMING_NAPI_ID 56 -#define SO_COOKIE 57 -#define SCM_TIMESTAMPING_PKTINFO 58 -#define SO_PEERGROUPS 59 -#define SO_ZEROCOPY 60 -#define SO_TXTIME 61 -#define SCM_TXTIME SO_TXTIME -#define SO_BINDTOIFINDEX 62 -#define SO_DETACH_REUSEPORT_BPF 68 -#define SO_PREFER_BUSY_POLL 69 -#define SO_BUSY_POLL_BUDGET 70 - -#ifndef SOL_SOCKET -#define SOL_SOCKET 1 -#endif - -#define SOL_IP 0 -#define SOL_IPV6 41 -#define SOL_ICMPV6 58 - -#define SOL_RAW 255 -#define SOL_DECNET 261 -#define SOL_X25 262 -#define SOL_PACKET 263 -#define SOL_ATM 264 -#define SOL_AAL 265 -#define SOL_IRDA 266 -#define SOL_NETBEUI 267 -#define SOL_LLC 268 -#define SOL_DCCP 269 -#define SOL_NETLINK 270 -#define SOL_TIPC 271 -#define SOL_RXRPC 272 -#define SOL_PPPOL2TP 273 -#define SOL_BLUETOOTH 274 -#define SOL_PNPIPE 275 -#define SOL_RDS 276 -#define SOL_IUCV 277 -#define SOL_CAIF 278 -#define SOL_ALG 279 -#define SOL_NFC 280 -#define SOL_KCM 281 -#define SOL_TLS 282 -#define SOL_XDP 283 - -#define SOMAXCONN 128 - -#define MSG_OOB 0x0001 -#define MSG_PEEK 0x0002 -#define MSG_DONTROUTE 0x0004 -#define MSG_CTRUNC 0x0008 -#define MSG_PROXY 0x0010 -#define MSG_TRUNC 0x0020 -#define MSG_DONTWAIT 0x0040 -#define MSG_EOR 0x0080 -#define MSG_WAITALL 0x0100 -#define MSG_FIN 0x0200 -#define MSG_SYN 0x0400 -#define MSG_CONFIRM 0x0800 -#define MSG_RST 0x1000 -#define MSG_ERRQUEUE 0x2000 -#define MSG_NOSIGNAL 0x4000 -#define MSG_MORE 0x8000 -#define MSG_WAITFORONE 0x10000 -#define MSG_BATCH 0x40000 -#define MSG_ZEROCOPY 0x4000000 -#define MSG_FASTOPEN 0x20000000 -#define MSG_CMSG_CLOEXEC 0x40000000 - -#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) -#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) -#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) - -#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) -#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ - __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ - ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) -#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) - -#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) -#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) -#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) - -#define SCM_RIGHTS 0x01 -#define SCM_CREDENTIALS 0x02 - -struct sockaddr { - sa_family_t sa_family; - char sa_data[14]; -}; - -#ifndef __MUSL__ -struct sockaddr_storage { - sa_family_t ss_family; - char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)]; - unsigned long __ss_align; -}; -#endif - -int socket (int, int, int); -int socketpair (int, int, int, int [2]); - -int shutdown (int, int); - -int bind (int, const struct sockaddr *, socklen_t); -int connect (int, const struct sockaddr *, socklen_t); -int listen (int, int); -int accept (int, struct sockaddr *__restrict, socklen_t *__restrict); -int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); - -int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict); -int getpeername (int, struct sockaddr *__restrict, socklen_t *__restrict); - -ssize_t send (int, const void *, size_t, int); -ssize_t recv (int, void *, size_t, int); -ssize_t sendto (int, const void *, size_t, int, const struct sockaddr *, socklen_t); -ssize_t recvfrom (int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict); -ssize_t sendmsg (int, const struct msghdr *, int); -ssize_t recvmsg (int, struct msghdr *, int); - -int getsockopt (int, int, int, void *__restrict, socklen_t *__restrict); -int setsockopt (int, int, int, const void *, socklen_t); - -int sockatmark (int); - -#if _REDIR_TIME64 -#ifdef _GNU_SOURCE -__REDIR(recvmmsg, __recvmmsg_time64); -#endif -#endif - -#include - -#ifdef __cplusplus -} -#endif -#endif diff --git a/porting/linux/user/include/sys/stat.h b/porting/linux/user/include/sys/stat.h deleted file mode 100644 index 450ee53c0..000000000 --- a/porting/linux/user/include/sys/stat.h +++ /dev/null @@ -1,129 +0,0 @@ -#ifndef _SYS_STAT_H -#define _SYS_STAT_H -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define __NEED_dev_t -#define __NEED_ino_t -#define __NEED_mode_t -#define __NEED_nlink_t -#define __NEED_uid_t -#define __NEED_gid_t -#define __NEED_off_t -#define __NEED_time_t -#define __NEED_blksize_t -#define __NEED_blkcnt_t -#define __NEED_struct_timespec - -#include - -#include - -#define st_atime st_atim.tv_sec -#define st_mtime st_mtim.tv_sec -#define st_ctime st_ctim.tv_sec - -#define S_IFMT 0170000 - -#define S_IFDIR 0040000 -#define S_IFCHR 0020000 -#define S_IFBLK 0060000 -#define S_IFREG 0100000 -#define S_IFIFO 0010000 -#define S_IFLNK 0120000 -#define S_IFSOCK 0140000 - -#define S_TYPEISMQ(buf) 0 -#define S_TYPEISSEM(buf) 0 -#define S_TYPEISSHM(buf) 0 -#define S_TYPEISTMO(buf) 0 - -#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) -#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) -#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) -#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) -#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) -#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) -#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) - -#ifndef S_IRUSR -#define S_ISUID 04000 -#define S_ISGID 02000 -#define S_ISVTX 01000 -#define S_IRUSR 0400 -#define S_IWUSR 0200 -#define S_IXUSR 0100 -#define S_IRWXU 0700 -#define S_IRGRP 0040 -#define S_IWGRP 0020 -#define S_IXGRP 0010 -#define S_IRWXG 0070 -#define S_IROTH 0004 -#define S_IWOTH 0002 -#define S_IXOTH 0001 -#define S_IRWXO 0007 -#endif - -#define UTIME_NOW 0x3fffffff -#define UTIME_OMIT 0x3ffffffe - -int stat(const char *__restrict, struct stat *__restrict); -int fstat(int, struct stat *); -int lstat(const char *__restrict, struct stat *__restrict); -int fstatat(int, const char *__restrict, struct stat *__restrict, int); -int chmod(const char *, mode_t); -int fchmod(int, mode_t); -int fchmodat(int, const char *, mode_t, int); -mode_t umask(mode_t); -int mkdir(const char *, mode_t); -int mkfifo(const char *, mode_t); -int mkdirat(int, const char *, mode_t); -int mkfifoat(int, const char *, mode_t); - -#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -int mknod(const char *, mode_t, dev_t); -int mknodat(int, const char *, mode_t, dev_t); -#endif - -int futimens(int, const struct timespec [2]); -int utimensat(int, const char *, const struct timespec [2], int); - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -int lchmod(const char *, mode_t); -#define S_IREAD S_IRUSR -#define S_IWRITE S_IWUSR -#define S_IEXEC S_IXUSR -#endif - -#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) -#define stat64 stat -#define fstat64 fstat -#define lstat64 lstat -#define fstatat64 fstatat -#define blkcnt64_t blkcnt_t -#define fsblkcnt64_t fsblkcnt_t -#define fsfilcnt64_t fsfilcnt_t -#define ino64_t ino_t -#define off64_t off_t -#endif - -#if _REDIR_TIME64 -__REDIR(stat, __stat_time64); -__REDIR(fstat, __fstat_time64); -__REDIR(lstat, __lstat_time64); -__REDIR(fstatat, __fstatat_time64); -__REDIR(futimens, __futimens_time64); -__REDIR(utimensat, __utimensat_time64); -#endif - -#include - -#ifdef __cplusplus -} -#endif -#endif - - diff --git a/porting/linux/user/include/sys/syscall.h b/porting/linux/user/include/sys/syscall.h deleted file mode 100644 index f5e822944..000000000 --- a/porting/linux/user/include/sys/syscall.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef _SYS_SYSCALL_H -#define _SYS_SYSCALL_H - -#include - -/* fixup legacy 16-bit junk */ -#ifdef SYS_getuid32 -#undef SYS_lchown -#undef SYS_getuid -#undef SYS_getgid -#undef SYS_geteuid -#undef SYS_getegid -#undef SYS_setreuid -#undef SYS_setregid -#undef SYS_getgroups -#undef SYS_setgroups -#undef SYS_fchown -#undef SYS_setresuid -#undef SYS_getresuid -#undef SYS_setresgid -#undef SYS_getresgid -#undef SYS_chown -#undef SYS_setuid -#undef SYS_setgid -#undef SYS_setfsuid -#undef SYS_setfsgid -#define SYS_lchown SYS_lchown32 -#define SYS_getuid SYS_getuid32 -#define SYS_getgid SYS_getgid32 -#define SYS_geteuid SYS_geteuid32 -#define SYS_getegid SYS_getegid32 -#define SYS_setreuid SYS_setreuid32 -#define SYS_setregid SYS_setregid32 -#define SYS_getgroups SYS_getgroups32 -#define SYS_setgroups SYS_setgroups32 -#define SYS_fchown SYS_fchown32 -#define SYS_setresuid SYS_setresuid32 -#define SYS_getresuid SYS_getresuid32 -#define SYS_setresgid SYS_setresgid32 -#define SYS_getresgid SYS_getresgid32 -#define SYS_chown SYS_chown32 -#define SYS_setuid SYS_setuid32 -#define SYS_setgid SYS_setgid32 -#define SYS_setfsuid SYS_setfsuid32 -#define SYS_setfsgid SYS_setfsgid32 -#endif -#endif diff --git a/porting/linux/user/include/sys/sysinfo.h b/porting/linux/user/include/sys/sysinfo.h deleted file mode 100644 index b55bb2a97..000000000 --- a/porting/linux/user/include/sys/sysinfo.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef _SYS_SYSINFO_H -#define _SYS_SYSINFO_H - -#ifdef __cplusplus -extern "C" { -#endif - -#if !defined (__linux__) || !defined(STANDARD_SYSTEM) -#define SI_LOAD_SHIFT 16 - -struct sysinfo { - unsigned long uptime; - unsigned long loads[3]; - unsigned long totalram; - unsigned long freeram; - unsigned long sharedram; - unsigned long bufferram; - unsigned long totalswap; - unsigned long freeswap; - unsigned short procs, pad; - unsigned long totalhigh; - unsigned long freehigh; - unsigned mem_unit; - char __reserved[256]; -}; -#else -#include -#endif - -int sysinfo (struct sysinfo *); -int get_nprocs_conf (void); -int get_nprocs (void); -long get_phys_pages (void); -long get_avphys_pages (void); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/include/unistd.h b/porting/linux/user/include/unistd.h deleted file mode 100644 index 089d7ee55..000000000 --- a/porting/linux/user/include/unistd.h +++ /dev/null @@ -1,511 +0,0 @@ -#ifndef _UNISTD_H -#define _UNISTD_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define STDIN_FILENO 0 -#define STDOUT_FILENO 1 -#define STDERR_FILENO 2 - -#define SEEK_SET 0 -#define SEEK_CUR 1 -#define SEEK_END 2 -#define SEEK_DATA 3 -#define SEEK_HOLE 4 - -#if __cplusplus >= 201103L -#define NULL nullptr -#elif defined(__cplusplus) -#define NULL 0L -#else -#define NULL ((void*)0) -#endif - -#define __NEED_size_t -#define __NEED_ssize_t -#define __NEED_uid_t -#define __NEED_gid_t -#define __NEED_off_t -#define __NEED_pid_t -#define __NEED_intptr_t -#define __NEED_useconds_t - -#include - -int pipe(int [2]); -int pipe2(int [2], int); -int close(int); -int posix_close(int, int); -int dup(int); -int dup2(int, int); -int dup3(int, int, int); -off_t lseek(int, off_t, int); -int fsync(int); -int fdatasync(int); - -ssize_t read(int, void *, size_t); -ssize_t write(int, const void *, size_t); -ssize_t pread(int, void *, size_t, off_t); -ssize_t pwrite(int, const void *, size_t, off_t); - -int chown(const char *, uid_t, gid_t); -int fchown(int, uid_t, gid_t); -int lchown(const char *, uid_t, gid_t); -int fchownat(int, const char *, uid_t, gid_t, int); - -int link(const char *, const char *); -int linkat(int, const char *, int, const char *, int); -int symlink(const char *, const char *); -int symlinkat(const char *, int, const char *); -ssize_t readlink(const char *__restrict, char *__restrict, size_t); -ssize_t readlinkat(int, const char *__restrict, char *__restrict, size_t); -int unlink(const char *); -int unlinkat(int, const char *, int); -int rmdir(const char *); -int truncate(const char *, off_t); -int ftruncate(int, off_t); - -#define F_OK 0 -#define R_OK 4 -#define W_OK 2 -#define X_OK 1 - -int access(const char *, int); -int faccessat(int, const char *, int, int); - -int chdir(const char *); -int fchdir(int); -char *getcwd(char *, size_t); - -unsigned alarm(unsigned); -unsigned sleep(unsigned); -int pause(void); - -pid_t fork(void); -pid_t _Fork(void); -int execve(const char *, char *const [], char *const []); -int execv(const char *, char *const []); -int execle(const char *, const char *, ...); -int execl(const char *, const char *, ...); -int execvp(const char *, char *const []); -int execlp(const char *, const char *, ...); -int fexecve(int, char *const [], char *const []); -_Noreturn void _exit(int); - -/** - * @brief Get the pid in the system namespace - * - * get the pid in the system namespace, used to globally mark processes in the pid sandbox.\n - * - * @param NA - * @return pid - * @since 4.1 - */ -pid_t getprocpid(void); - -/** - * @brief Get the tid in the system namespace - * - * get the tid in the system namespace, used to globally mark processes in the pid sandbox.\n - * - * @param NA - * @return pid - * @since 4.1 - */ -pid_t getproctid(void); -pid_t getpid(void); -pid_t getppid(void); -pid_t getpgrp(void); -pid_t getpgid(pid_t); -int setpgid(pid_t, pid_t); -pid_t setsid(void); -pid_t getsid(pid_t); -char *ttyname(int); -int ttyname_r(int, char *, size_t); -int isatty(int); -pid_t tcgetpgrp(int); -int tcsetpgrp(int, pid_t); - -uid_t getuid(void); -uid_t geteuid(void); -gid_t getgid(void); -gid_t getegid(void); -int getgroups(int, gid_t []); -int setuid(uid_t); -int seteuid(uid_t); -int setgid(gid_t); -int setegid(gid_t); - -char *getlogin(void); -int getlogin_r(char *, size_t); -int gethostname(char *, size_t); -char *ctermid(char *); - -int getopt(int, char * const [], const char *); -extern char *optarg; -extern int optind, opterr, optopt; - -long pathconf(const char *, int); -long fpathconf(int, int); -long sysconf(int); -size_t confstr(int, char *, size_t); - -#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define F_ULOCK 0 -#define F_LOCK 1 -#define F_TLOCK 2 -#define F_TEST 3 -int setreuid(uid_t, uid_t); -int setregid(gid_t, gid_t); -int lockf(int, int, off_t); -long gethostid(void); -int nice(int); -void sync(void); -pid_t setpgrp(void); -char *crypt(const char *, const char *); -void encrypt(char *, int); -void swab(const void *__restrict, void *__restrict, ssize_t); -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) \ - || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) -int usleep(unsigned); -unsigned ualarm(unsigned, unsigned); -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define L_SET 0 -#define L_INCR 1 -#define L_XTND 2 -int brk(void *); -void *sbrk(intptr_t); -pid_t vfork(void); -int vhangup(void); -int chroot(const char *); -int getpagesize(void); -int getdtablesize(void); -int sethostname(const char *, size_t); -int getdomainname(char *, size_t); -int setdomainname(const char *, size_t); -int setgroups(size_t, const gid_t *); -char *getpass(const char *); -int daemon(int, int); -void setusershell(void); -void endusershell(void); -char *getusershell(void); -int acct(const char *); -long syscall(long, ...); -int execvpe(const char *, char *const [], char *const []); -int issetugid(void); -int getentropy(void *, size_t); -extern int optreset; -#endif - -#ifdef _GNU_SOURCE -extern char **environ; -int setresuid(uid_t, uid_t, uid_t); -int setresgid(gid_t, gid_t, gid_t); -int getresuid(uid_t *, uid_t *, uid_t *); -int getresgid(gid_t *, gid_t *, gid_t *); -char *get_current_dir_name(void); -int syncfs(int); -int euidaccess(const char *, int); -int eaccess(const char *, int); -ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned); -pid_t gettid(void); -#endif - -#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) -#define lseek64 lseek -#define pread64 pread -#define pwrite64 pwrite -#define truncate64 truncate -#define ftruncate64 ftruncate -#define lockf64 lockf -#define off64_t off_t -#endif - -#define POSIX_CLOSE_RESTART 0 - -#define _XOPEN_VERSION 700 -#define _XOPEN_UNIX 1 -#define _XOPEN_ENH_I18N 1 - -#define _POSIX_VERSION 200809L -#define _POSIX2_VERSION _POSIX_VERSION - -#define _POSIX_ADVISORY_INFO _POSIX_VERSION -#define _POSIX_CHOWN_RESTRICTED 1 -#define _POSIX_IPV6 _POSIX_VERSION -#define _POSIX_JOB_CONTROL 1 -#define _POSIX_MAPPED_FILES _POSIX_VERSION -#define _POSIX_MEMLOCK _POSIX_VERSION -#define _POSIX_MEMLOCK_RANGE _POSIX_VERSION -#define _POSIX_MEMORY_PROTECTION _POSIX_VERSION -#define _POSIX_MESSAGE_PASSING _POSIX_VERSION -#define _POSIX_FSYNC _POSIX_VERSION -#define _POSIX_NO_TRUNC 1 -#define _POSIX_RAW_SOCKETS _POSIX_VERSION -#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION -#define _POSIX_REGEXP 1 -#define _POSIX_SAVED_IDS 1 -#define _POSIX_SHELL 1 -#define _POSIX_SPAWN _POSIX_VERSION -#define _POSIX_VDISABLE 0 - -#define _POSIX_THREADS _POSIX_VERSION -#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION -#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION -#define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION -#define _POSIX_THREAD_ATTR_STACKSIZE _POSIX_VERSION -#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION -#define _POSIX_THREAD_CPUTIME _POSIX_VERSION -#define _POSIX_TIMERS _POSIX_VERSION -#define _POSIX_TIMEOUTS _POSIX_VERSION -#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION -#define _POSIX_CPUTIME _POSIX_VERSION -#define _POSIX_CLOCK_SELECTION _POSIX_VERSION -#define _POSIX_BARRIERS _POSIX_VERSION -#define _POSIX_SPIN_LOCKS _POSIX_VERSION -#define _POSIX_READER_WRITER_LOCKS _POSIX_VERSION -#define _POSIX_ASYNCHRONOUS_IO _POSIX_VERSION -#define _POSIX_SEMAPHORES _POSIX_VERSION -#define _POSIX_SHARED_MEMORY_OBJECTS _POSIX_VERSION - -#define _POSIX2_C_BIND _POSIX_VERSION - -#include - - - -#define _PC_LINK_MAX 0 -#define _PC_MAX_CANON 1 -#define _PC_MAX_INPUT 2 -#define _PC_NAME_MAX 3 -#define _PC_PATH_MAX 4 -#define _PC_PIPE_BUF 5 -#define _PC_CHOWN_RESTRICTED 6 -#define _PC_NO_TRUNC 7 -#define _PC_VDISABLE 8 -#define _PC_SYNC_IO 9 -#define _PC_ASYNC_IO 10 -#define _PC_PRIO_IO 11 -#define _PC_SOCK_MAXBUF 12 -#define _PC_FILESIZEBITS 13 -#define _PC_REC_INCR_XFER_SIZE 14 -#define _PC_REC_MAX_XFER_SIZE 15 -#define _PC_REC_MIN_XFER_SIZE 16 -#define _PC_REC_XFER_ALIGN 17 -#define _PC_ALLOC_SIZE_MIN 18 -#define _PC_SYMLINK_MAX 19 -#define _PC_2_SYMLINKS 20 - -#define _SC_ARG_MAX 0 -#define _SC_CHILD_MAX 1 -#define _SC_CLK_TCK 2 -#define _SC_NGROUPS_MAX 3 -#define _SC_OPEN_MAX 4 -#define _SC_STREAM_MAX 5 -#define _SC_TZNAME_MAX 6 -#define _SC_JOB_CONTROL 7 -#define _SC_SAVED_IDS 8 -#define _SC_REALTIME_SIGNALS 9 -#define _SC_PRIORITY_SCHEDULING 10 -#define _SC_TIMERS 11 -#define _SC_ASYNCHRONOUS_IO 12 -#define _SC_PRIORITIZED_IO 13 -#define _SC_SYNCHRONIZED_IO 14 -#define _SC_FSYNC 15 -#define _SC_MAPPED_FILES 16 -#define _SC_MEMLOCK 17 -#define _SC_MEMLOCK_RANGE 18 -#define _SC_MEMORY_PROTECTION 19 -#define _SC_MESSAGE_PASSING 20 -#define _SC_SEMAPHORES 21 -#define _SC_SHARED_MEMORY_OBJECTS 22 -#define _SC_AIO_LISTIO_MAX 23 -#define _SC_AIO_MAX 24 -#define _SC_AIO_PRIO_DELTA_MAX 25 -#define _SC_DELAYTIMER_MAX 26 -#define _SC_MQ_OPEN_MAX 27 -#define _SC_MQ_PRIO_MAX 28 -#define _SC_VERSION 29 -#define _SC_PAGE_SIZE 30 -#define _SC_PAGESIZE 30 /* !! */ -#define _SC_RTSIG_MAX 31 -#define _SC_SEM_NSEMS_MAX 32 -#define _SC_SEM_VALUE_MAX 33 -#define _SC_SIGQUEUE_MAX 34 -#define _SC_TIMER_MAX 35 -#define _SC_BC_BASE_MAX 36 -#define _SC_BC_DIM_MAX 37 -#define _SC_BC_SCALE_MAX 38 -#define _SC_BC_STRING_MAX 39 -#define _SC_COLL_WEIGHTS_MAX 40 -#define _SC_EXPR_NEST_MAX 42 -#define _SC_LINE_MAX 43 -#define _SC_RE_DUP_MAX 44 -#define _SC_2_VERSION 46 -#define _SC_2_C_BIND 47 -#define _SC_2_C_DEV 48 -#define _SC_2_FORT_DEV 49 -#define _SC_2_FORT_RUN 50 -#define _SC_2_SW_DEV 51 -#define _SC_2_LOCALEDEF 52 -#define _SC_UIO_MAXIOV 60 /* !! */ -#define _SC_IOV_MAX 60 -#define _SC_THREADS 67 -#define _SC_THREAD_SAFE_FUNCTIONS 68 -#define _SC_GETGR_R_SIZE_MAX 69 -#define _SC_GETPW_R_SIZE_MAX 70 -#define _SC_LOGIN_NAME_MAX 71 -#define _SC_TTY_NAME_MAX 72 -#define _SC_THREAD_DESTRUCTOR_ITERATIONS 73 -#define _SC_THREAD_KEYS_MAX 74 -#define _SC_THREAD_STACK_MIN 75 -#define _SC_THREAD_THREADS_MAX 76 -#define _SC_THREAD_ATTR_STACKADDR 77 -#define _SC_THREAD_ATTR_STACKSIZE 78 -#define _SC_THREAD_PRIORITY_SCHEDULING 79 -#define _SC_THREAD_PRIO_INHERIT 80 -#define _SC_THREAD_PRIO_PROTECT 81 -#define _SC_THREAD_PROCESS_SHARED 82 -#define _SC_NPROCESSORS_CONF 83 -#define _SC_NPROCESSORS_ONLN 84 -#define _SC_PHYS_PAGES 85 -#define _SC_AVPHYS_PAGES 86 -#define _SC_ATEXIT_MAX 87 -#define _SC_PASS_MAX 88 -#define _SC_XOPEN_VERSION 89 -#define _SC_XOPEN_XCU_VERSION 90 -#define _SC_XOPEN_UNIX 91 -#define _SC_XOPEN_CRYPT 92 -#define _SC_XOPEN_ENH_I18N 93 -#define _SC_XOPEN_SHM 94 -#define _SC_2_CHAR_TERM 95 -#define _SC_2_UPE 97 -#define _SC_XOPEN_XPG2 98 -#define _SC_XOPEN_XPG3 99 -#define _SC_XOPEN_XPG4 100 -#define _SC_NZERO 109 -#define _SC_XBS5_ILP32_OFF32 125 -#define _SC_XBS5_ILP32_OFFBIG 126 -#define _SC_XBS5_LP64_OFF64 127 -#define _SC_XBS5_LPBIG_OFFBIG 128 -#define _SC_XOPEN_LEGACY 129 -#define _SC_XOPEN_REALTIME 130 -#define _SC_XOPEN_REALTIME_THREADS 131 -#define _SC_ADVISORY_INFO 132 -#define _SC_BARRIERS 133 -#define _SC_CLOCK_SELECTION 137 -#define _SC_CPUTIME 138 -#define _SC_THREAD_CPUTIME 139 -#define _SC_MONOTONIC_CLOCK 149 -#define _SC_READER_WRITER_LOCKS 153 -#define _SC_SPIN_LOCKS 154 -#define _SC_REGEXP 155 -#define _SC_SHELL 157 -#define _SC_SPAWN 159 -#define _SC_SPORADIC_SERVER 160 -#define _SC_THREAD_SPORADIC_SERVER 161 -#define _SC_TIMEOUTS 164 -#define _SC_TYPED_MEMORY_OBJECTS 165 -#define _SC_2_PBS 168 -#define _SC_2_PBS_ACCOUNTING 169 -#define _SC_2_PBS_LOCATE 170 -#define _SC_2_PBS_MESSAGE 171 -#define _SC_2_PBS_TRACK 172 -#define _SC_SYMLOOP_MAX 173 -#define _SC_STREAMS 174 -#define _SC_2_PBS_CHECKPOINT 175 -#define _SC_V6_ILP32_OFF32 176 -#define _SC_V6_ILP32_OFFBIG 177 -#define _SC_V6_LP64_OFF64 178 -#define _SC_V6_LPBIG_OFFBIG 179 -#define _SC_HOST_NAME_MAX 180 -#define _SC_TRACE 181 -#define _SC_TRACE_EVENT_FILTER 182 -#define _SC_TRACE_INHERIT 183 -#define _SC_TRACE_LOG 184 - -#define _SC_IPV6 235 -#define _SC_RAW_SOCKETS 236 -#define _SC_V7_ILP32_OFF32 237 -#define _SC_V7_ILP32_OFFBIG 238 -#define _SC_V7_LP64_OFF64 239 -#define _SC_V7_LPBIG_OFFBIG 240 -#define _SC_SS_REPL_MAX 241 -#define _SC_TRACE_EVENT_NAME_MAX 242 -#define _SC_TRACE_NAME_MAX 243 -#define _SC_TRACE_SYS_MAX 244 -#define _SC_TRACE_USER_EVENT_MAX 245 -#define _SC_XOPEN_STREAMS 246 -#define _SC_THREAD_ROBUST_PRIO_INHERIT 247 -#define _SC_THREAD_ROBUST_PRIO_PROTECT 248 - -#define _CS_PATH 0 -#define _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS 1 -#define _CS_GNU_LIBC_VERSION 2 -#define _CS_GNU_LIBPTHREAD_VERSION 3 -#define _CS_POSIX_V5_WIDTH_RESTRICTED_ENVS 4 -#define _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS 5 - -#define _CS_POSIX_V6_ILP32_OFF32_CFLAGS 1116 -#define _CS_POSIX_V6_ILP32_OFF32_LDFLAGS 1117 -#define _CS_POSIX_V6_ILP32_OFF32_LIBS 1118 -#define _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS 1119 -#define _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS 1120 -#define _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS 1121 -#define _CS_POSIX_V6_ILP32_OFFBIG_LIBS 1122 -#define _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS 1123 -#define _CS_POSIX_V6_LP64_OFF64_CFLAGS 1124 -#define _CS_POSIX_V6_LP64_OFF64_LDFLAGS 1125 -#define _CS_POSIX_V6_LP64_OFF64_LIBS 1126 -#define _CS_POSIX_V6_LP64_OFF64_LINTFLAGS 1127 -#define _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS 1128 -#define _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS 1129 -#define _CS_POSIX_V6_LPBIG_OFFBIG_LIBS 1130 -#define _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS 1131 -#define _CS_POSIX_V7_ILP32_OFF32_CFLAGS 1132 -#define _CS_POSIX_V7_ILP32_OFF32_LDFLAGS 1133 -#define _CS_POSIX_V7_ILP32_OFF32_LIBS 1134 -#define _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS 1135 -#define _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS 1136 -#define _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS 1137 -#define _CS_POSIX_V7_ILP32_OFFBIG_LIBS 1138 -#define _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS 1139 -#define _CS_POSIX_V7_LP64_OFF64_CFLAGS 1140 -#define _CS_POSIX_V7_LP64_OFF64_LDFLAGS 1141 -#define _CS_POSIX_V7_LP64_OFF64_LIBS 1142 -#define _CS_POSIX_V7_LP64_OFF64_LINTFLAGS 1143 -#define _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS 1144 -#define _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS 1145 -#define _CS_POSIX_V7_LPBIG_OFFBIG_LIBS 1146 -#define _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS 1147 -#define _CS_V6_ENV 1148 -#define _CS_V7_ENV 1149 - -#ifdef _GNU_SOURCE -#ifndef TEMP_FAILURE_RETRY -#define MUSL_TEMP_FAILURE_RETRY(expression) \ - (__extension__ \ - ({ long int __result; \ - do __result = (long int)(expression); \ - while(__result == -1L&& errno == EINTR); \ - __result;})) - -#define TEMP_FAILURE_RETRY(expression) MUSL_TEMP_FAILURE_RETRY(expression) -#endif -#endif - -#include - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/linux/user/src/aio/aio.c b/porting/linux/user/src/aio/aio.c deleted file mode 100644 index 6e632f356..000000000 --- a/porting/linux/user/src/aio/aio.c +++ /dev/null @@ -1,424 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#include "atomic.h" -#include "pthread_impl.h" -#include "aio_impl.h" - -#define malloc __libc_malloc -#define calloc __libc_calloc -#define realloc __libc_realloc -#define free __libc_free - -/* The following is a threads-based implementation of AIO with minimal - * dependence on implementation details. Most synchronization is - * performed with pthread primitives, but atomics and futex operations - * are used for notification in a couple places where the pthread - * primitives would be inefficient or impractical. - * - * For each fd with outstanding aio operations, an aio_queue structure - * is maintained. These are reference-counted and destroyed by the last - * aio worker thread to exit. Accessing any member of the aio_queue - * structure requires a lock on the aio_queue. Adding and removing aio - * queues themselves requires a write lock on the global map object, - * a 4-level table mapping file descriptor numbers to aio queues. A - * read lock on the map is used to obtain locks on existing queues by - * excluding destruction of the queue by a different thread while it is - * being locked. - * - * Each aio queue has a list of active threads/operations. Presently there - * is a one to one relationship between threads and operations. The only - * members of the aio_thread structure which are accessed by other threads - * are the linked list pointers, op (which is immutable), running (which - * is updated atomically), and err (which is synchronized via running), - * so no locking is necessary. Most of the other other members are used - * for sharing data between the main flow of execution and cancellation - * cleanup handler. - * - * Taking any aio locks requires having all signals blocked. This is - * necessary because aio_cancel is needed by close, and close is required - * to be async-signal safe. All aio worker threads run with all signals - * blocked permanently. - */ - -struct aio_thread { - pthread_t td; - struct aiocb *cb; - struct aio_thread *next, *prev; - struct aio_queue *q; - volatile int running; - int err, op; - ssize_t ret; -}; - -struct aio_queue { - int fd, seekable, append, ref, init; - pthread_mutex_t lock; - pthread_cond_t cond; - struct aio_thread *head; -}; - -struct aio_args { - struct aiocb *cb; - struct aio_queue *q; - int op; - sem_t sem; -}; - -static pthread_rwlock_t maplock = PTHREAD_RWLOCK_INITIALIZER; -static struct aio_queue *****map; -static volatile int aio_fd_cnt; -volatile int __aio_fut; - -static size_t io_thread_stack_size; - -#define MAX(a,b) ((a)>(b) ? (a) : (b)) - -static struct aio_queue *__aio_get_queue(int fd, int need) -{ - if (fd < 0) { - errno = EBADF; - return 0; - } - int a=fd>>24; - unsigned char b=fd>>16, c=fd>>8, d=fd; - struct aio_queue *q = 0; - pthread_rwlock_rdlock(&maplock); - if ((!map || !map[a] || !map[a][b] || !map[a][b][c] || !(q=map[a][b][c][d])) && need) { - pthread_rwlock_unlock(&maplock); - if (fcntl(fd, F_GETFD) < 0) return 0; - pthread_rwlock_wrlock(&maplock); - if (!io_thread_stack_size) { - unsigned long val = __getauxval(AT_MINSIGSTKSZ); - io_thread_stack_size = MAX(MINSIGSTKSZ+2048, val+512); - } - if (!map) map = calloc(sizeof *map, (-1U/2+1)>>24); - if (!map) goto out; - if (!map[a]) map[a] = calloc(sizeof **map, 256); - if (!map[a]) goto out; - if (!map[a][b]) map[a][b] = calloc(sizeof ***map, 256); - if (!map[a][b]) goto out; - if (!map[a][b][c]) map[a][b][c] = calloc(sizeof ****map, 256); - if (!map[a][b][c]) goto out; - if (!(q = map[a][b][c][d])) { - map[a][b][c][d] = q = calloc(sizeof *****map, 1); - if (q) { - q->fd = fd; - pthread_mutex_init(&q->lock, 0); - pthread_cond_init(&q->cond, 0); - a_inc(&aio_fd_cnt); - } - } - } - if (q) pthread_mutex_lock(&q->lock); -out: - pthread_rwlock_unlock(&maplock); - return q; -} - -static void __aio_unref_queue(struct aio_queue *q) -{ - if (q->ref > 1) { - q->ref--; - pthread_mutex_unlock(&q->lock); - return; - } - - /* This is potentially the last reference, but a new reference - * may arrive since we cannot free the queue object without first - * taking the maplock, which requires releasing the queue lock. */ - pthread_mutex_unlock(&q->lock); - pthread_rwlock_wrlock(&maplock); - pthread_mutex_lock(&q->lock); - if (q->ref == 1) { - int fd=q->fd; - int a=fd>>24; - unsigned char b=fd>>16, c=fd>>8, d=fd; - map[a][b][c][d] = 0; - a_dec(&aio_fd_cnt); - pthread_rwlock_unlock(&maplock); - pthread_mutex_unlock(&q->lock); - free(q); - } else { - q->ref--; - pthread_rwlock_unlock(&maplock); - pthread_mutex_unlock(&q->lock); - } -} - -static void cleanup(void *ctx) -{ - struct aio_thread *at = ctx; - struct aio_queue *q = at->q; - struct aiocb *cb = at->cb; - struct sigevent sev = cb->aio_sigevent; - - /* There are four potential types of waiters we could need to wake: - * 1. Callers of aio_cancel/close. - * 2. Callers of aio_suspend with a single aiocb. - * 3. Callers of aio_suspend with a list. - * 4. AIO worker threads waiting for sequenced operations. - * Types 1-3 are notified via atomics/futexes, mainly for AS-safety - * considerations. Type 4 is notified later via a cond var. */ - - cb->__ret = at->ret; - if (a_swap(&at->running, 0) < 0) - __wake(&at->running, -1, 1); - if (a_swap(&cb->__err, at->err) != EINPROGRESS) - __wake(&cb->__err, -1, 1); - if (a_swap(&__aio_fut, 0)) - __wake(&__aio_fut, -1, 1); - - pthread_mutex_lock(&q->lock); - - if (at->next) at->next->prev = at->prev; - if (at->prev) at->prev->next = at->next; - else q->head = at->next; - - /* Signal aio worker threads waiting for sequenced operations. */ - pthread_cond_broadcast(&q->cond); - - __aio_unref_queue(q); - - if (sev.sigev_notify == SIGEV_SIGNAL) { - siginfo_t si = { - .si_signo = sev.sigev_signo, - .si_value = sev.sigev_value, - .si_code = SI_ASYNCIO, - .si_pid = getpid(), - .si_uid = getuid() - }; - __syscall(SYS_rt_sigqueueinfo, si.si_pid, si.si_signo, &si); - } - if (sev.sigev_notify == SIGEV_THREAD) { -#ifdef FEATURE_PTHREAD_CANCEL - a_store(&__pthread_self()->cancel, 0); -#endif - sev.sigev_notify_function(sev.sigev_value); - } -} - -static void *io_thread_func(void *ctx) -{ - struct aio_thread at, *p; - - struct aio_args *args = ctx; - struct aiocb *cb = args->cb; - int fd = cb->aio_fildes; - int op = args->op; - void *buf = (void *)cb->aio_buf; - size_t len = cb->aio_nbytes; - off_t off = cb->aio_offset; - - struct aio_queue *q = args->q; - ssize_t ret; - - pthread_mutex_lock(&q->lock); - sem_post(&args->sem); - - at.op = op; - at.running = 1; - at.ret = -1; - at.err = ECANCELED; - at.q = q; - at.td = __pthread_self(); - at.cb = cb; - at.prev = 0; - if ((at.next = q->head)) at.next->prev = &at; - q->head = &at; - - if (!q->init) { - int seekable = lseek(fd, 0, SEEK_CUR) >= 0; - q->seekable = seekable; - q->append = !seekable || (fcntl(fd, F_GETFL) & O_APPEND); - q->init = 1; - } - - pthread_cleanup_push(cleanup, &at); - - /* Wait for sequenced operations. */ - if (op!=LIO_READ && (op!=LIO_WRITE || q->append)) { - for (;;) { - for (p=at.next; p && p->op!=LIO_WRITE; p=p->next); - if (!p) break; - pthread_cond_wait(&q->cond, &q->lock); - } - } - - pthread_mutex_unlock(&q->lock); - - switch (op) { - case LIO_WRITE: - ret = q->append ? write(fd, buf, len) : pwrite(fd, buf, len, off); - break; - case LIO_READ: - ret = !q->seekable ? read(fd, buf, len) : pread(fd, buf, len, off); - break; - case O_SYNC: - ret = fsync(fd); - break; - case O_DSYNC: - ret = fdatasync(fd); - break; - } - at.ret = ret; - at.err = ret<0 ? errno : 0; - - pthread_cleanup_pop(1); - - return 0; -} - -static int submit(struct aiocb *cb, int op) -{ - int ret = 0; - pthread_attr_t a; - sigset_t allmask, origmask; - pthread_t td; - struct aio_queue *q = __aio_get_queue(cb->aio_fildes, 1); - struct aio_args args = { .cb = cb, .op = op, .q = q }; - sem_init(&args.sem, 0, 0); - - if (!q) { - if (errno != EBADF) errno = EAGAIN; - cb->__ret = -1; - cb->__err = errno; - return -1; - } - q->ref++; - pthread_mutex_unlock(&q->lock); - - if (cb->aio_sigevent.sigev_notify == SIGEV_THREAD) { - if (cb->aio_sigevent.sigev_notify_attributes) - a = *cb->aio_sigevent.sigev_notify_attributes; - else - pthread_attr_init(&a); - } else { - pthread_attr_init(&a); - pthread_attr_setstacksize(&a, io_thread_stack_size); - pthread_attr_setguardsize(&a, 0); - } - pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); - sigfillset(&allmask); - pthread_sigmask(SIG_BLOCK, &allmask, &origmask); - cb->__err = EINPROGRESS; - if (pthread_create(&td, &a, io_thread_func, &args)) { - pthread_mutex_lock(&q->lock); - __aio_unref_queue(q); - cb->__err = errno = EAGAIN; - cb->__ret = ret = -1; - } - pthread_sigmask(SIG_SETMASK, &origmask, 0); - - if (!ret) { - while (sem_wait(&args.sem)); - } - - return ret; -} - -int aio_read(struct aiocb *cb) -{ - return submit(cb, LIO_READ); -} - -int aio_write(struct aiocb *cb) -{ - return submit(cb, LIO_WRITE); -} - -int aio_fsync(int op, struct aiocb *cb) -{ - if (op != O_SYNC && op != O_DSYNC) { - errno = EINVAL; - return -1; - } - return submit(cb, op); -} - -ssize_t aio_return(struct aiocb *cb) -{ - return cb->__ret; -} - -int aio_error(const struct aiocb *cb) -{ - a_barrier(); - return cb->__err & 0x7fffffff; -} - -int aio_cancel(int fd, struct aiocb *cb) -{ - sigset_t allmask, origmask; - int ret = AIO_ALLDONE; - struct aio_thread *p; - struct aio_queue *q; - - /* Unspecified behavior case. Report an error. */ - if (cb && fd != cb->aio_fildes) { - errno = EINVAL; - return -1; - } - - sigfillset(&allmask); - pthread_sigmask(SIG_BLOCK, &allmask, &origmask); - - errno = ENOENT; - if (!(q = __aio_get_queue(fd, 0))) { - if (errno == EBADF) ret = -1; - goto done; - } - - for (p = q->head; p; p = p->next) { - if (cb && cb != p->cb) continue; - /* Transition target from running to running-with-waiters */ - if (a_cas(&p->running, 1, -1)) { -#ifdef FEATURE_PTHREAD_CANCEL - pthread_cancel(p->td); -#else - __syscall(SYS_tkill, p->td->tid, SIGCANCEL); -#endif - __wait(&p->running, 0, -1, 1); - if (p->err == ECANCELED) ret = AIO_CANCELED; - } - } - - pthread_mutex_unlock(&q->lock); -done: - pthread_sigmask(SIG_SETMASK, &origmask, 0); - return ret; -} - -int __aio_close(int fd) -{ - a_barrier(); - if (aio_fd_cnt) aio_cancel(fd, 0); - return fd; -} - -void __aio_atfork(int who) -{ - if (who<0) { - pthread_rwlock_rdlock(&maplock); - return; - } - if (who>0 && map) for (int a=0; a<(-1U/2+1)>>24; a++) - if (map[a]) for (int b=0; b<256; b++) - if (map[a][b]) for (int c=0; c<256; c++) - if (map[a][b][c]) for (int d=0; d<256; d++) - map[a][b][c][d] = 0; - pthread_rwlock_unlock(&maplock); -} - -weak_alias(aio_cancel, aio_cancel64); -weak_alias(aio_error, aio_error64); -weak_alias(aio_fsync, aio_fsync64); -weak_alias(aio_read, aio_read64); -weak_alias(aio_write, aio_write64); -weak_alias(aio_return, aio_return64); diff --git a/porting/linux/user/src/conf/legacy.c b/porting/linux/user/src/conf/legacy.c deleted file mode 100644 index ddc863427..000000000 --- a/porting/linux/user/src/conf/legacy.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" - -#define _GNU_SOURCE -#include - -int get_cpu_default() { - int i, cnt; - unsigned char set[128] = {1}; - __syscall(SYS_sched_getaffinity, 0, sizeof(set), set); - for ((i=cnt=0); (i < sizeof(set)); i++) - for (; set[i]; (set[i]&=set[i]-1), cnt++); - return cnt; -} - -int get_nprocs_conf() -{ - struct dirent *de; - int cnt = 0; - DIR *d = opendir("/sys/devices/system/cpu"); - if (!d) - return get_cpu_default(); - while ((de = readdir(d))) { - if ((de->d_type == DT_DIR) && - (strlen(de->d_name) > 3) && - (de->d_name[0] == 'c') && - (de->d_name[1] == 'p') && - (de->d_name[2] == 'u') && - (isdigit(de->d_name[3]))) - cnt++; - } - closedir(d); - return cnt; -} - -int get_nprocs() -{ - int cnt; - FILE* fp = fopen("/sys/devices/system/cpu/online", "re"); - if (fp != NULL) { - char buf[128]; - memset(buf, 0, sizeof(buf)); - if (!fgets(buf, sizeof(buf), fp)) { - return 1; - } - cnt = 0; - char *tmp, *p; - tmp = strtok_r(buf, ",", &p); - while (tmp != NULL) { - char *tok = strstr(tmp, "-"); - if (tok) { - tok++; - cnt += atoi(tok) - atoi(tmp) + 1; - } else { - cnt += 1; - } - tmp = strtok_r(NULL, ",", &p); - } - fclose(fp); - return cnt; - } - return get_cpu_default(); -} - -long get_phys_pages() -{ - return sysconf(_SC_PHYS_PAGES); -} - -long get_avphys_pages() -{ - return sysconf(_SC_AVPHYS_PAGES); -} diff --git a/porting/linux/user/src/conf/sysconf.c b/porting/linux/user/src/conf/sysconf.c deleted file mode 100644 index 0578599ef..000000000 --- a/porting/linux/user/src/conf/sysconf.c +++ /dev/null @@ -1,215 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#include "libc.h" - -#define JT(x) (-256|(x)) -#define VER JT(1) -#define JT_ARG_MAX JT(2) -#define JT_MQ_PRIO_MAX JT(3) -#define JT_PAGE_SIZE JT(4) -#define JT_SEM_VALUE_MAX JT(5) -#define JT_NPROCESSORS_CONF JT(6) -#define JT_NPROCESSORS_ONLN JT(7) -#define JT_PHYS_PAGES JT(8) -#define JT_AVPHYS_PAGES JT(9) -#define JT_ZERO JT(10) -#define JT_DELAYTIMER_MAX JT(11) - -#define RLIM(x) (-32768|(RLIMIT_ ## x)) - -long sysconf(int name) -{ - static const short values[] = { - [_SC_ARG_MAX] = JT_ARG_MAX, - [_SC_CHILD_MAX] = RLIM(NPROC), - [_SC_CLK_TCK] = 100, - [_SC_NGROUPS_MAX] = 32, - [_SC_OPEN_MAX] = RLIM(NOFILE), - [_SC_STREAM_MAX] = -1, - [_SC_TZNAME_MAX] = TZNAME_MAX, - [_SC_JOB_CONTROL] = 1, - [_SC_SAVED_IDS] = 1, - [_SC_REALTIME_SIGNALS] = VER, - [_SC_PRIORITY_SCHEDULING] = -1, - [_SC_TIMERS] = VER, - [_SC_ASYNCHRONOUS_IO] = VER, - [_SC_PRIORITIZED_IO] = -1, - [_SC_SYNCHRONIZED_IO] = -1, - [_SC_FSYNC] = VER, - [_SC_MAPPED_FILES] = VER, - [_SC_MEMLOCK] = VER, - [_SC_MEMLOCK_RANGE] = VER, - [_SC_MEMORY_PROTECTION] = VER, - [_SC_MESSAGE_PASSING] = VER, - [_SC_SEMAPHORES] = VER, - [_SC_SHARED_MEMORY_OBJECTS] = VER, - [_SC_AIO_LISTIO_MAX] = -1, - [_SC_AIO_MAX] = -1, - [_SC_AIO_PRIO_DELTA_MAX] = JT_ZERO, /* ?? */ - [_SC_DELAYTIMER_MAX] = JT_DELAYTIMER_MAX, - [_SC_MQ_OPEN_MAX] = -1, - [_SC_MQ_PRIO_MAX] = JT_MQ_PRIO_MAX, - [_SC_VERSION] = VER, - [_SC_PAGE_SIZE] = JT_PAGE_SIZE, - [_SC_RTSIG_MAX] = _NSIG - 1 - 31 - 3, - [_SC_SEM_NSEMS_MAX] = SEM_NSEMS_MAX, - [_SC_SEM_VALUE_MAX] = JT_SEM_VALUE_MAX, - [_SC_SIGQUEUE_MAX] = -1, - [_SC_TIMER_MAX] = -1, - [_SC_BC_BASE_MAX] = _POSIX2_BC_BASE_MAX, - [_SC_BC_DIM_MAX] = _POSIX2_BC_DIM_MAX, - [_SC_BC_SCALE_MAX] = _POSIX2_BC_SCALE_MAX, - [_SC_BC_STRING_MAX] = _POSIX2_BC_STRING_MAX, - [_SC_COLL_WEIGHTS_MAX] = COLL_WEIGHTS_MAX, - [_SC_EXPR_NEST_MAX] = -1, - [_SC_LINE_MAX] = -1, - [_SC_RE_DUP_MAX] = RE_DUP_MAX, - [_SC_2_VERSION] = VER, - [_SC_2_C_BIND] = VER, - [_SC_2_C_DEV] = -1, - [_SC_2_FORT_DEV] = -1, - [_SC_2_FORT_RUN] = -1, - [_SC_2_SW_DEV] = -1, - [_SC_2_LOCALEDEF] = -1, - [_SC_IOV_MAX] = IOV_MAX, - [_SC_THREADS] = VER, - [_SC_THREAD_SAFE_FUNCTIONS] = VER, - [_SC_GETGR_R_SIZE_MAX] = -1, - [_SC_GETPW_R_SIZE_MAX] = -1, - [_SC_LOGIN_NAME_MAX] = 256, - [_SC_TTY_NAME_MAX] = TTY_NAME_MAX, - [_SC_THREAD_DESTRUCTOR_ITERATIONS] = PTHREAD_DESTRUCTOR_ITERATIONS, - [_SC_THREAD_KEYS_MAX] = PTHREAD_KEYS_MAX, - [_SC_THREAD_STACK_MIN] = PTHREAD_STACK_MIN, - [_SC_THREAD_THREADS_MAX] = -1, - [_SC_THREAD_ATTR_STACKADDR] = VER, - [_SC_THREAD_ATTR_STACKSIZE] = VER, - [_SC_THREAD_PRIORITY_SCHEDULING] = VER, - [_SC_THREAD_PRIO_INHERIT] = -1, - [_SC_THREAD_PRIO_PROTECT] = -1, - [_SC_THREAD_PROCESS_SHARED] = VER, - [_SC_NPROCESSORS_CONF] = JT_NPROCESSORS_CONF, - [_SC_NPROCESSORS_ONLN] = JT_NPROCESSORS_ONLN, - [_SC_PHYS_PAGES] = JT_PHYS_PAGES, - [_SC_AVPHYS_PAGES] = JT_AVPHYS_PAGES, - [_SC_ATEXIT_MAX] = -1, - [_SC_PASS_MAX] = -1, - [_SC_XOPEN_VERSION] = _XOPEN_VERSION, - [_SC_XOPEN_XCU_VERSION] = _XOPEN_VERSION, - [_SC_XOPEN_UNIX] = 1, - [_SC_XOPEN_CRYPT] = -1, - [_SC_XOPEN_ENH_I18N] = 1, - [_SC_XOPEN_SHM] = 1, - [_SC_2_CHAR_TERM] = -1, - [_SC_2_UPE] = -1, - [_SC_XOPEN_XPG2] = -1, - [_SC_XOPEN_XPG3] = -1, - [_SC_XOPEN_XPG4] = -1, - [_SC_NZERO] = NZERO, - [_SC_XBS5_ILP32_OFF32] = -1, - [_SC_XBS5_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1, - [_SC_XBS5_LP64_OFF64] = sizeof(long)==8 ? 1 : -1, - [_SC_XBS5_LPBIG_OFFBIG] = -1, - [_SC_XOPEN_LEGACY] = -1, - [_SC_XOPEN_REALTIME] = -1, - [_SC_XOPEN_REALTIME_THREADS] = -1, - [_SC_ADVISORY_INFO] = VER, - [_SC_BARRIERS] = VER, - [_SC_CLOCK_SELECTION] = VER, - [_SC_CPUTIME] = VER, - [_SC_THREAD_CPUTIME] = VER, - [_SC_MONOTONIC_CLOCK] = VER, - [_SC_READER_WRITER_LOCKS] = VER, - [_SC_SPIN_LOCKS] = VER, - [_SC_REGEXP] = 1, - [_SC_SHELL] = 1, - [_SC_SPAWN] = VER, - [_SC_SPORADIC_SERVER] = -1, - [_SC_THREAD_SPORADIC_SERVER] = -1, - [_SC_TIMEOUTS] = VER, - [_SC_TYPED_MEMORY_OBJECTS] = -1, - [_SC_2_PBS] = -1, - [_SC_2_PBS_ACCOUNTING] = -1, - [_SC_2_PBS_LOCATE] = -1, - [_SC_2_PBS_MESSAGE] = -1, - [_SC_2_PBS_TRACK] = -1, - [_SC_SYMLOOP_MAX] = SYMLOOP_MAX, - [_SC_STREAMS] = JT_ZERO, - [_SC_2_PBS_CHECKPOINT] = -1, - [_SC_V6_ILP32_OFF32] = -1, - [_SC_V6_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1, - [_SC_V6_LP64_OFF64] = sizeof(long)==8 ? 1 : -1, - [_SC_V6_LPBIG_OFFBIG] = -1, - [_SC_HOST_NAME_MAX] = HOST_NAME_MAX, - [_SC_TRACE] = -1, - [_SC_TRACE_EVENT_FILTER] = -1, - [_SC_TRACE_INHERIT] = -1, - [_SC_TRACE_LOG] = -1, - - [_SC_IPV6] = VER, - [_SC_RAW_SOCKETS] = VER, - [_SC_V7_ILP32_OFF32] = -1, - [_SC_V7_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1, - [_SC_V7_LP64_OFF64] = sizeof(long)==8 ? 1 : -1, - [_SC_V7_LPBIG_OFFBIG] = -1, - [_SC_SS_REPL_MAX] = -1, - [_SC_TRACE_EVENT_NAME_MAX] = -1, - [_SC_TRACE_NAME_MAX] = -1, - [_SC_TRACE_SYS_MAX] = -1, - [_SC_TRACE_USER_EVENT_MAX] = -1, - [_SC_XOPEN_STREAMS] = JT_ZERO, - [_SC_THREAD_ROBUST_PRIO_INHERIT] = -1, - [_SC_THREAD_ROBUST_PRIO_PROTECT] = -1, - }; - - if (name >= sizeof(values)/sizeof(values[0]) || !values[name]) { - errno = EINVAL; - return -1; - } else if (values[name] >= -1) { - return values[name]; - } else if (values[name] < -256) { - struct rlimit lim; - getrlimit(values[name]&16383, &lim); - if (lim.rlim_cur == RLIM_INFINITY) - return -1; - return lim.rlim_cur > LONG_MAX ? LONG_MAX : lim.rlim_cur; - } - - switch ((unsigned char)values[name]) { - case VER & 255: - return _POSIX_VERSION; - case JT_ARG_MAX & 255: - return ARG_MAX; - case JT_MQ_PRIO_MAX & 255: - return MQ_PRIO_MAX; - case JT_PAGE_SIZE & 255: - return PAGE_SIZE; - case JT_SEM_VALUE_MAX & 255: - return SEM_VALUE_MAX; - case JT_DELAYTIMER_MAX & 255: - return DELAYTIMER_MAX; - case JT_NPROCESSORS_CONF & 255: - return get_nprocs_conf(); - case JT_NPROCESSORS_ONLN & 255: - return get_nprocs(); - case JT_PHYS_PAGES & 255: - case JT_AVPHYS_PAGES & 255: ; - unsigned long long mem; - struct sysinfo si; - __lsysinfo(&si); - if (!si.mem_unit) si.mem_unit = 1; - if (name==_SC_PHYS_PAGES) mem = si.totalram; - else mem = si.freeram + si.bufferram; - mem *= si.mem_unit; - mem /= PAGE_SIZE; - return (mem > LONG_MAX) ? LONG_MAX : mem; - case JT_ZERO & 255: - return 0; - } - return values[name]; -} diff --git a/porting/linux/user/src/ctype/towctrans.c b/porting/linux/user/src/ctype/towctrans.c deleted file mode 100644 index cb0d9e6bb..000000000 --- a/porting/linux/user/src/ctype/towctrans.c +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -#include -#include - -static const unsigned char tab[]; - -static const unsigned char rulebases[512]; -static const int rules[]; - -static const unsigned char exceptions[][2]; - -#include "casemap.h" - -static int casemap(unsigned c, int dir) -{ - unsigned b, x, y, v, rt, xb, xn; - int r, rd, c0 = c; - - if (c >= 0x20000) return c; - - b = c>>8; - c &= 255; - x = c/3; - y = c%3; - - /* lookup entry in two-level base-6 table */ - v = tab[tab[b]*86+x]; - static const int mt[] = { 2048, 342, 57 }; - v = (v*mt[y]>>11)%6; - - /* use the bit vector out of the tables as an index into - * a block-specific set of rules and decode the rule into - * a type and a case-mapping delta. */ - r = rules[rulebases[b]+v]; - rt = r & 255; - rd = r >> 8; - - /* rules 0/1 are simple lower/upper case with a delta. - * apply according to desired mapping direction. */ - if (rt < 2) return c0 + (rd & -(rt^dir)); - - /* binary search. endpoints of the binary search for - * this block are stored in the rule delta field. */ - xn = rd & 0xff; - xb = (unsigned)rd >> 8; - while (xn) { - unsigned try = exceptions[xb+xn/2][0]; - if (try == c) { - r = rules[exceptions[xb+xn/2][1]]; - rt = r & 255; - rd = r >> 8; - if (rt < 2) return c0 + (rd & -(rt^dir)); - /* Hard-coded for the four exceptional titlecase */ - return c0 + (dir ? -1 : 1); - } else if (try > c) { - xn /= 2; - } else { - xb += xn/2; - xn -= xn/2; - } - } - return c0; -} - -static void* g_hmicu_handle = NULL; -static wint_t (*g_hm_ucase_toupper)(wint_t); - -static void* find_hmicu_symbol(const char* symbol_name) { - if (!g_hmicu_handle) { - g_hmicu_handle = dlopen("libhmicuuc.z.so", RTLD_LOCAL); - } - return g_hmicu_handle ? dlsym(g_hmicu_handle, symbol_name) : NULL; -} - -wint_t towlower(wint_t wc) -{ - if (wc < 0x80) { - if (wc >= 'A' && wc <= 'Z') return wc | 0x20; - return wc; - } - return casemap(wc, 0); -} - -wint_t towupper(wint_t wc) -{ - if (wc < 0x80) { - if (wc >= 'a' && wc <= 'z') return wc ^ 0x20; - return wc; - } - if (!g_hm_ucase_toupper) { - typedef wint_t (*f)(wint_t); - g_hm_ucase_toupper = (f)find_hmicu_symbol("ucase_toupper_72"); - } - return g_hm_ucase_toupper ? g_hm_ucase_toupper(wc) : casemap(wc, 1); -} - -wint_t __towupper_l(wint_t c, locale_t l) -{ - return towupper(c); -} - -wint_t __towlower_l(wint_t c, locale_t l) -{ - return towlower(c); -} - -weak_alias(__towupper_l, towupper_l); -weak_alias(__towlower_l, towlower_l); \ No newline at end of file diff --git a/porting/linux/user/src/env/__init_tls.c b/porting/linux/user/src/env/__init_tls.c deleted file mode 100644 index 665c82242..000000000 --- a/porting/linux/user/src/env/__init_tls.c +++ /dev/null @@ -1,158 +0,0 @@ -#define SYSCALL_NO_TLS 1 -#include -#include -#include -#include -#include -#include "pthread_impl.h" -#include "libc.h" -#include "atomic.h" -#include "syscall.h" - -volatile int __thread_list_lock; - -int __init_tp(void *p) -{ - pthread_t td = p; - td->self = td; - int r = __set_thread_area(TP_ADJ(p)); - if (r < 0) return -1; - if (!r) libc.can_do_threads = 1; - td->detach_state = DT_JOINABLE; - td->tid = td->pid = __syscall(SYS_set_tid_address, &__thread_list_lock); - td->proc_tid = -1; - td->locale = &libc.global_locale; - td->robust_list.head = &td->robust_list.head; - td->sysinfo = __sysinfo; - td->next = td->prev = td; - return 0; -} - -static struct builtin_tls { - char c[8]; - struct pthread pt; - void *space[16]; -} builtin_tls[1]; -#define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt) - -static struct tls_module main_tls; - -void *__copy_tls(unsigned char *mem) -{ - pthread_t td; - struct tls_module *p; - size_t i; - uintptr_t *dtv; - -#ifdef TLS_ABOVE_TP - dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1); - - mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1); - td = (pthread_t)mem; - mem += sizeof(struct pthread); - - for (i=1, p=libc.tls_head; p; i++, p=p->next) { - dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET; - if (p->image) { - memcpy(mem + p->offset, p->image, p->len); - } - } -#else - dtv = (uintptr_t *)mem; - - mem += libc.tls_size - sizeof(struct pthread); - mem -= (uintptr_t)mem & (libc.tls_align-1); - td = (pthread_t)mem; - - for (i=1, p=libc.tls_head; p; i++, p=p->next) { - dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET; - if (p->image) { - memcpy(mem - p->offset, p->image, p->len); - } - } -#endif - dtv[0] = libc.tls_cnt; - td->dtv = dtv; - return td; -} - -#if ULONG_MAX == 0xffffffff -typedef Elf32_Phdr Phdr; -#else -typedef Elf64_Phdr Phdr; -#endif - -extern weak hidden const size_t _DYNAMIC[]; - -static void static_init_tls(size_t *aux) -{ - unsigned char *p; - size_t n; - Phdr *phdr, *tls_phdr=0; - size_t base = 0; - void *mem; - - for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) { - phdr = (void *)p; - if (phdr->p_type == PT_PHDR) - base = aux[AT_PHDR] - phdr->p_vaddr; - if (phdr->p_type == PT_DYNAMIC && _DYNAMIC) - base = (size_t)_DYNAMIC - phdr->p_vaddr; - if (phdr->p_type == PT_TLS) - tls_phdr = phdr; - if (phdr->p_type == PT_GNU_STACK && - phdr->p_memsz > __default_stacksize) - __default_stacksize = - phdr->p_memsz < DEFAULT_STACK_MAX ? - phdr->p_memsz : DEFAULT_STACK_MAX; - } - - if (tls_phdr) { - main_tls.image = (void *)(base + tls_phdr->p_vaddr); - main_tls.len = tls_phdr->p_filesz; - main_tls.size = tls_phdr->p_memsz; - main_tls.align = tls_phdr->p_align; - libc.tls_cnt = 1; - libc.tls_head = &main_tls; - } - - main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image) - & (main_tls.align-1); -#ifdef TLS_ABOVE_TP - main_tls.offset = GAP_ABOVE_TP; - main_tls.offset += (-GAP_ABOVE_TP + (uintptr_t)main_tls.image) - & (main_tls.align-1); -#else - main_tls.offset = main_tls.size; -#endif - if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN; - - libc.tls_align = main_tls.align; - libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread) -#ifdef TLS_ABOVE_TP - + main_tls.offset -#endif - + main_tls.size + main_tls.align - + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN; - - if (libc.tls_size > sizeof builtin_tls) { -#ifndef SYS_mmap2 -#define SYS_mmap2 SYS_mmap -#endif - mem = (void *)__syscall( - SYS_mmap2, - 0, libc.tls_size, PROT_READ|PROT_WRITE, - MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); - /* -4095...-1 cast to void * will crash on dereference anyway, - * so don't bloat the init code checking for error codes and - * explicitly calling a_crash(). */ - } else { - mem = builtin_tls; - } - - /* Failure to initialize thread pointer is always fatal. */ - if (__init_tp(__copy_tls(mem)) < 0) - a_crash(); -} - -weak_alias(static_init_tls, __init_tls); diff --git a/porting/linux/user/src/env/__libc_start_main.c b/porting/linux/user/src/env/__libc_start_main.c deleted file mode 100644 index 6dd2eb61f..000000000 --- a/porting/linux/user/src/env/__libc_start_main.c +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include -#include -#include -#include "syscall.h" -#include "atomic.h" -#include "libc.h" -#include "pthread_impl.h" - -static void dummy(void) {} -weak_alias(dummy, _init); - -extern weak hidden void (*const __init_array_start)(void), (*const __init_array_end)(void); - -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] -#include -#include - -char *getenv(const char *name) -{ - if (name == NULL || __environ == NULL) - return 0; - size_t i, l = 0; - const char *np; - char **p, *ep; - for (; *(name + l) && *(name + l) != '='; ++l); - for (p = __environ; (ep = *p) != NULL; ++p) { - for (np = name, i = l; i && *ep; i--) { - if (*ep++ != *np++) { - break; - } - } - if (i == 0 && *ep++ == '=') { - return (ep); - } - } - return 0; -} diff --git a/porting/linux/user/src/fcntl/open.c b/porting/linux/user/src/fcntl/open.c deleted file mode 100644 index a5ce534bd..000000000 --- a/porting/linux/user/src/fcntl/open.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include "syscall.h" - -int open(const char *filename, int flags, ...) -{ - mode_t mode = 0; - - if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) { - va_list ap; - va_start(ap, flags); - mode = va_arg(ap, mode_t); - va_end(ap); - } - - int fd = __sys_open_cp(filename, flags, mode); - return __syscall_ret(fd); -} - -weak_alias(open, open64); diff --git a/porting/linux/user/src/include/pthread.h b/porting/linux/user/src/include/pthread.h deleted file mode 100644 index 1c4d66276..000000000 --- a/porting/linux/user/src/include/pthread.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef PTHREAD_H -#define PTHREAD_H - -#include "../../include/pthread.h" - -hidden int __pthread_once(pthread_once_t *, void (*)(void)); -hidden void __pthread_testcancel(void); -hidden int __pthread_setcancelstate(int, int *); -hidden int __pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict); -hidden _Noreturn void __pthread_exit(void *); -hidden int __pthread_join(pthread_t, void **); -hidden int __pthread_mutex_lock(pthread_mutex_t *); -hidden int __pthread_mutex_trylock(pthread_mutex_t *); -hidden int __pthread_mutex_trylock_owner(pthread_mutex_t *); -hidden int __pthread_mutex_timedlock(pthread_mutex_t *restrict, const struct timespec *restrict); -hidden int __pthread_mutex_unlock(pthread_mutex_t *); -hidden int __private_cond_signal(pthread_cond_t *, int) __attribute__((no_stack_protector)); -hidden int __pthread_cond_timedwait(pthread_cond_t *restrict, pthread_mutex_t *restrict, const struct timespec *restrict); -hidden int __pthread_key_create(pthread_key_t *, void (*)(void *)); -hidden int __pthread_key_delete(pthread_key_t); -hidden int __pthread_rwlock_rdlock(pthread_rwlock_t *); -hidden int __pthread_rwlock_tryrdlock(pthread_rwlock_t *); -hidden int __pthread_rwlock_timedrdlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict); -hidden int __pthread_rwlock_wrlock(pthread_rwlock_t *); -hidden int __pthread_rwlock_trywrlock(pthread_rwlock_t *); -hidden int __pthread_rwlock_timedwrlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict); -hidden int __pthread_rwlock_unlock(pthread_rwlock_t *); - -#endif diff --git a/porting/linux/user/src/internal/dynlink.h b/porting/linux/user/src/internal/dynlink.h deleted file mode 100644 index 6bb0c6c9d..000000000 --- a/porting/linux/user/src/internal/dynlink.h +++ /dev/null @@ -1,269 +0,0 @@ -#ifndef _INTERNAL_RELOC_H -#define _INTERNAL_RELOC_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include "libc.h" -#include "../../ldso/namespace.h" - -#if UINTPTR_MAX == 0xffffffff -typedef Elf32_Ehdr Ehdr; -typedef Elf32_Phdr Phdr; -typedef Elf32_Shdr Shdr; -typedef Elf32_Sym Sym; -typedef Elf32_Verdaux Verdaux; -typedef Elf32_Verdef Verdef; -typedef Elf32_Vernaux Vernaux; -typedef Elf32_Verneed Verneed; -#define R_TYPE(x) ((x)&255) -#define R_SYM(x) ((x)>>8) -#define R_INFO ELF32_R_INFO -#else -typedef Elf64_Ehdr Ehdr; -typedef Elf64_Phdr Phdr; -typedef Elf64_Shdr Shdr; -typedef Elf64_Sym Sym; -typedef Elf64_Verdaux Verdaux; -typedef Elf64_Verdef Verdef; -typedef Elf64_Vernaux Vernaux; -typedef Elf64_Verneed Verneed; -#define R_TYPE(x) ((x)&0x7fffffff) -#define R_SYM(x) ((x)>>32) -#define R_INFO ELF64_R_INFO -#endif - -/* These enum constants provide unmatchable default values for - * any relocation type the arch does not use. */ -enum { - REL_NONE = 0, - REL_SYMBOLIC = -100, - REL_USYMBOLIC, - REL_GOT, - REL_PLT, - REL_RELATIVE, - REL_OFFSET, - REL_OFFSET32, - REL_COPY, - REL_SYM_OR_REL, - REL_DTPMOD, - REL_DTPOFF, - REL_TPOFF, - REL_TPOFF_NEG, - REL_TLSDESC, - REL_FUNCDESC, - REL_FUNCDESC_VAL, -}; - -struct td_index { - size_t args[2]; - struct td_index *next; -}; - -struct verinfo { - const char *s; - const char *v; - bool use_vna_hash; - uint32_t vna_hash; -}; - -struct sym_info_pair { - uint_fast32_t sym_h; - uint32_t sym_l; -}; - -struct dso { -#if DL_FDPIC - struct fdpic_loadmap *loadmap; -#else - unsigned char *base; -#endif - char *name; - size_t *dynv; - struct dso *next, *prev; - /* add namespace */ - ns_t *namespace; - int cache_sym_index; - struct dso *cache_dso; - Sym *cache_sym; - Phdr *phdr; - int phnum; - size_t phentsize; - Sym *syms; - Elf_Symndx *hashtab; - uint32_t *ghashtab; - int16_t *versym; - Verdef *verdef; - Verneed *verneed; - char *strings; - struct dso *syms_next, *lazy_next; - size_t *lazy, lazy_cnt; - unsigned char *map; - size_t map_len; - dev_t dev; - ino_t ino; - uint64_t file_offset; - pthread_t ctor_visitor; - char *rpath_orig, *rpath; - struct tls_module tls; - size_t tls_id; - size_t relro_start, relro_end; - uintptr_t *new_dtv; - unsigned char *new_tls; - struct td_index *td_index; - struct dso *fini_next; - char *shortname; -#if DL_FDPIC - unsigned char *base; -#else - struct fdpic_loadmap *loadmap; -#endif - struct funcdesc { - void *addr; - size_t *got; - } *funcdescs; - size_t *got; - struct dso **deps, *needed_by; - uint16_t ndeps_direct; - uint16_t next_dep; - uint16_t parents_count; - uint16_t parents_capacity; - struct dso **parents; - struct dso **reloc_can_search_dso_list; - uint16_t reloc_can_search_dso_count; - uint16_t reloc_can_search_dso_capacity; - /* mark the dso status */ - uint32_t flags; - uint8_t nr_dlopen; - bool is_global; - bool is_reloc_head_so_dep; - char relocated; - char constructed; - char kernel_mapped; - char mark; - char bfs_built; - char runtime_loaded; - char by_dlopen; - bool is_mapped_to_shadow; - struct dso_debug_info *debug_info; - char buf[]; -}; - -struct dso_debug_info { -#if DL_FDPIC - struct fdpic_loadmap *loadmap; -#else - unsigned char *base; -#endif - char *name; - size_t *dynv; - struct dso_debug_info *next, *prev; -}; - -struct symdef { - Sym *sym; - struct dso *dso; -}; - -struct fdpic_loadseg { - uintptr_t addr, p_vaddr, p_memsz; -}; - -struct fdpic_loadmap { - unsigned short version, nsegs; - struct fdpic_loadseg segs[]; -}; - -struct fdpic_dummy_loadmap { - unsigned short version, nsegs; - struct fdpic_loadseg segs[1]; -}; - -#include "reloc.h" - -#ifndef FDPIC_CONSTDISP_FLAG -#define FDPIC_CONSTDISP_FLAG 0 -#endif - -#ifndef DL_FDPIC -#define DL_FDPIC 0 -#endif - -#ifndef DL_NOMMU_SUPPORT -#define DL_NOMMU_SUPPORT 0 -#endif - -#if !DL_FDPIC -#define IS_RELATIVE(x,s) ( \ - (R_TYPE(x) == REL_RELATIVE) || \ - (R_TYPE(x) == REL_SYM_OR_REL && !R_SYM(x)) ) -#else -#define IS_RELATIVE(x,s) ( ( \ - (R_TYPE(x) == REL_FUNCDESC_VAL) || \ - (R_TYPE(x) == REL_SYMBOLIC) ) \ - && (((s)[R_SYM(x)].st_info & 0xf) == STT_SECTION) ) -#endif - -#ifndef NEED_MIPS_GOT_RELOCS -#define NEED_MIPS_GOT_RELOCS 0 -#endif - -#ifndef DT_DEBUG_INDIRECT -#define DT_DEBUG_INDIRECT 0 -#endif - -#define AUX_CNT 32 -#define DYN_CNT 37 - -#define DT_ANDROID_REL (DT_LOOS + 2) -#define DT_ANDROID_RELSZ (DT_LOOS + 3) - -#define DT_ANDROID_RELA (DT_LOOS + 4) -#define DT_ANDROID_RELASZ (DT_LOOS + 5) - -#define ANDROID_REL_SIGN_SIZE 4 - -#define RELOCATION_GROUPED_BY_INFO_FLAG 1 -#define RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG 2 -#define RELOCATION_GROUPED_BY_ADDEND_FLAG 4 -#define RELOCATION_GROUP_HAS_ADDEND_FLAG 8 - -typedef void (*stage2_func)(unsigned char *, size_t *); - -#if DL_FDPIC -void *laddr(const struct dso *p, size_t v); -#endif - -#ifdef UNIT_TEST_STATIC - #define UT_STATIC -#else - #define UT_STATIC static -#endif - -void *addr2dso(size_t a); -UT_STATIC size_t count_syms(struct dso *p); -struct sym_info_pair gnu_hash(const char *s0); -struct symdef find_sym_impl( - struct dso *dso, struct verinfo *verinfo, struct sym_info_pair s_info_p, int need_def, ns_t *ns); - -hidden void *__dlsym(void *restrict, const char *restrict, void *restrict); -hidden void *__dlvsym(void *restrict, const char *restrict, const char *restrict, void *restrict); -hidden int __dlclose(void *p); - -hidden void __dl_seterr(const char *, ...); -hidden int __dl_invalid_handle(void *); -hidden void __dl_vseterr(const char *, va_list); - -hidden ptrdiff_t __tlsdesc_static(), __tlsdesc_dynamic(); - -hidden extern int __malloc_replaced; -hidden extern int __aligned_alloc_replaced; -hidden void __malloc_donate(char *, char *); -hidden int __malloc_allzerop(void *); - -#endif diff --git a/porting/linux/user/src/internal/libc.h b/porting/linux/user/src/internal/libc.h deleted file mode 100644 index 2f1d24795..000000000 --- a/porting/linux/user/src/internal/libc.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LIBC_H -#define LIBC_H - -#include -#include -#include -#include -#include -struct __locale_map; - -struct __locale_struct { - const struct __locale_map *cat[LC_ALL]; -}; - -struct tls_module { - struct tls_module *next; - void *image; - size_t len, size, align, offset; -}; - -struct __libc { - char can_do_threads; - char threaded; - char secure; - volatile signed char need_locks; - int threads_minus_1; - size_t *auxv; - struct tls_module *tls_head; - size_t tls_size, tls_align, tls_cnt; - size_t page_size; - struct __locale_struct global_locale; -#ifdef ENABLE_HWASAN - void (*load_hook)(unsigned long int base, const Elf64_Phdr* phdr, int phnum); - void (*unload_hook)(unsigned long int base, const Elf64_Phdr* phdr, int phnum); -#endif -}; - -#ifndef PAGE_SIZE -#define PAGE_SIZE libc.page_size -#endif - -extern hidden struct __libc __libc; -#define libc __libc - -hidden void __init_libc(char **, char *); -hidden void __init_tls(size_t *); -hidden void __init_ssp(void *); -hidden void __libc_start_init(void); -hidden void __funcs_on_exit(void); -hidden void __funcs_on_quick_exit(void); -hidden void __libc_exit_fini(void); -hidden void __fork_handler(int); - -extern hidden size_t __hwcap; -extern hidden size_t __sysinfo; -extern char *__progname, *__progname_full; - -extern hidden const char __libc_version[]; - -hidden void __synccall(void (*)(void *), void *); -hidden int __setxid(int, int, int, int); - -#endif diff --git a/porting/linux/user/src/internal/locale_impl.h b/porting/linux/user/src/internal/locale_impl.h deleted file mode 100644 index a843f0e73..000000000 --- a/porting/linux/user/src/internal/locale_impl.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef _LOCALE_IMPL_H -#define _LOCALE_IMPL_H - -#include -#include -#include "libc.h" -#include "pthread_impl.h" - -#define LOCALE_NAME_MAX 23 -#define VALID 2 -#define INVALID 1 - -struct __locale_map { - const void *map; - size_t map_size; - char name[LOCALE_NAME_MAX+1]; - const struct __locale_map *next; - char flag; -}; - -extern hidden volatile int __locale_lock[1]; - -extern hidden const struct __locale_map __c_dot_utf8; -extern hidden const struct __locale_struct __c_locale; -extern hidden const struct __locale_struct __c_dot_utf8_locale; - -hidden const struct __locale_map *__get_locale(int, const char *); -hidden const char *__mo_lookup(const void *, size_t, const char *); -hidden const char *__lctrans(const char *, const struct __locale_map *); -hidden const char *__lctrans_cur(const char *); -hidden const char *__lctrans_impl(const char *, const struct __locale_map *); -hidden int __loc_is_allocated(locale_t); -hidden char *__gettextdomain(void); - -#define LOC_MAP_FAILED ((const struct __locale_map *)-1) - -#define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)]) -#define LCTRANS_CUR(msg) __lctrans_cur(msg) - -#define C_LOCALE ((locale_t)&__c_locale) -#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale) - -#define CURRENT_LOCALE (__pthread_self()->locale) - -#define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE]) - -#undef MB_CUR_MAX -#define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1) - -#endif diff --git a/porting/linux/user/src/internal/pthread_impl.h b/porting/linux/user/src/internal/pthread_impl.h deleted file mode 100644 index d8aece14c..000000000 --- a/porting/linux/user/src/internal/pthread_impl.h +++ /dev/null @@ -1,289 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _INTERNAL_PTHREAD_IMPL_H -#define _INTERNAL_PTHREAD_IMPL_H - -#include -#include -#include -#include -#include -#include "libc.h" -#include "syscall.h" -#include "atomic.h" -#include "futex.h" - -#include "pthread_arch.h" - -#define pthread __pthread -#define TLS_RESERVE_SLOT 15 - -struct pthread { - /* Part 1 -- these fields may be external or - * internal (accessed via asm) ABI. Do not change. */ - struct pthread *self; -#ifndef TLS_ABOVE_TP - uintptr_t *dtv; -#endif - struct pthread *prev, *next; /* non-ABI */ - uintptr_t sysinfo; -#ifndef TLS_ABOVE_TP -#ifdef CANARY_PAD - uintptr_t canary_pad; -#endif - uintptr_t canary; -#endif - - /* Part 2 -- implementation details, non-ABI. */ - int tid; - int pid; - int proc_tid; - int errno_val; - volatile int detach_state; -#ifdef FEATURE_PTHREAD_CANCEL - volatile int cancel; - volatile unsigned char canceldisable, cancelasync; -#endif - unsigned char tsd_used:1; - unsigned char dlerror_flag:1; - unsigned char *map_base; - size_t map_size; - void *stack; - size_t stack_size; - size_t guard_size; - void *result; - struct __ptcb *cancelbuf; - void **tsd; - struct { - volatile void *volatile head; - long off; - volatile void *volatile pending; - } robust_list; - int h_errno_val; - volatile int timer_id; - locale_t locale; - volatile int killlock[1]; - char *dlerror_buf; - void *stdio_locks; -#ifdef RESERVE_SIGNAL_STACK - void *signal_stack; -#endif - - /* Part 3 -- the positions of these fields relative to - * the end of the structure is external and internal ABI. */ -#ifdef TLS_ABOVE_TP - /* The tls_slots will be accessed by kernel, so don't use it. - * To solve the problem that the kernel isn't synchronized with the musl, - * so make pre/post reserved slots for musl. - * pre-reserved : tls_slots[0-4] - * kernel used : tls_slots[5-9] - * post-reserved : tls_slot[10-14] */ - void *tls_slots[TLS_RESERVE_SLOT]; - uintptr_t canary; - uintptr_t *dtv; -#endif -}; - -enum { - DT_EXITED = 0, - DT_EXITING, - DT_JOINABLE, - DT_DETACHED, -}; - -#define __SU (sizeof(size_t)/sizeof(int)) - -#define _a_stacksize __u.__s[0] -#define _a_guardsize __u.__s[1] -#define _a_stackaddr __u.__s[2] -#define _a_detach __u.__i[3*__SU+0] -#define _a_sched __u.__i[3*__SU+1] -#define _a_policy __u.__i[3*__SU+2] -#define _a_prio __u.__i[3*__SU+3] - -/* we define the original value of _m_* in include/pthread.h - * as macros MUTEX_* to make the user be able to - * access the inner attribute of the mutex struct. - * Then, we modify the value of _m_* macros to MUTEX_* here, - * so that we can immediately be aware of the changes that - * the open source society has made to these original macros, - * because patching will fail if the value of the _m_* are - * changed by musl society */ -#define _m_type __u.__i[0] -#define _m_lock __u.__vi[1] -#define _m_waiters __u.__vi[2] -#define _m_prev __u.__p[3] -#define _m_next __u.__p[4] -#define _m_clock __u.__i[4] -#define _m_count __u.__i[5] - -#define _c_shared __u.__p[0] -#define _c_seq __u.__vi[2] -#define _c_waiters __u.__vi[3] -#define _c_clock __u.__i[4] -#define _c_lock __u.__vi[8] -#define _c_head __u.__p[1] -#define _c_tail __u.__p[5] -#define _rw_lock __u.__vi[0] -#define _rw_waiters __u.__vi[1] -#define _rw_shared __u.__i[2] -#define _rw_clock __u.__i[4] -#define _b_lock __u.__vi[0] -#define _b_waiters __u.__vi[1] -#define _b_limit __u.__i[2] -#define _b_count __u.__vi[3] -#define _b_waiters2 __u.__vi[4] -#define _b_inst __u.__p[3] - -#ifndef TP_OFFSET -#define TP_OFFSET 0 -#endif - -#ifndef DTP_OFFSET -#define DTP_OFFSET 0 -#endif - -#ifdef TLS_ABOVE_TP -#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + TP_OFFSET) -#define __pthread_self() ((pthread_t)(__get_tp() - sizeof(struct __pthread) - TP_OFFSET)) -#else -#define TP_ADJ(p) (p) -#define __pthread_self() ((pthread_t)__get_tp()) -#endif - -#ifndef tls_mod_off_t -#define tls_mod_off_t size_t -#endif - -#define SIGTIMER 32 -#define SIGCANCEL 33 -#define SIGSYNCCALL 34 - -#define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 }) -#define SIGPT_SET \ - ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ - [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) }) -#define SIGTIMER_SET \ - ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ - 0x80000000 }) - -void *__tls_get_addr(tls_mod_off_t *); -hidden int __init_tp(void *); -hidden void *__copy_tls(unsigned char *); -hidden void __reset_tls(); - -hidden void __membarrier_init(void); -hidden void __dl_thread_cleanup(void); -hidden void __testcancel(); -hidden void __do_cleanup_push(struct __ptcb *); -hidden void __do_cleanup_pop(struct __ptcb *); -hidden void __pthread_tsd_run_dtors(); - -hidden void __pthread_key_delete_synccall(void (*)(void *), void *); -hidden int __pthread_key_delete_impl(pthread_key_t); - -extern hidden volatile size_t __pthread_tsd_size; -extern hidden void *__pthread_tsd_main[]; -extern hidden volatile int __eintr_valid_flag; - -hidden int __clone(int (*)(void *), void *, int, void *, ...); -hidden int __set_thread_area(void *); -hidden int __libc_sigaction(int, const struct sigaction *, struct sigaction *); -hidden void __unmapself(void *, size_t); - -hidden int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int); -hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int); -hidden void __wait(volatile int *, volatile int *, int, int); -static inline void __wake(volatile void *addr, int cnt, int priv) -{ - if (priv) priv = FUTEX_PRIVATE; - if (cnt<0) cnt = INT_MAX; - __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS || - __syscall(SYS_futex, addr, FUTEX_WAKE, cnt); -} -static inline void __futexwait(volatile void *addr, int val, int priv) -{ - if (priv) priv = FUTEX_PRIVATE; - __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS || - __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0); -} - -#define MS_PER_S 1000 -#define US_PER_S 1000000 -static inline void __timespec_from_ms(struct timespec* ts, const unsigned ms) -{ - if (ts == NULL) { - return; - } - ts->tv_sec = ms / MS_PER_S; - ts->tv_nsec = (ms % MS_PER_S) * US_PER_S; -} - -#define NS_PER_S 1000000000 -static inline void __absolute_timespec_from_timespec(struct timespec *abs_ts, - const struct timespec *ts, clockid_t clock) -{ - if (abs_ts == NULL || ts == NULL) { - return; - } - clock_gettime(clock, abs_ts); - abs_ts->tv_sec += ts->tv_sec; - abs_ts->tv_nsec += ts->tv_nsec; - if (abs_ts->tv_nsec >= NS_PER_S) { - abs_ts->tv_nsec -= NS_PER_S; - abs_ts->tv_sec++; - } -} - -#ifdef RESERVE_SIGNAL_STACK -hidden void pthread_reserve_signal_stack(); -hidden void pthread_release_signal_stack(); -#endif - -hidden void __acquire_ptc(void); -hidden void __release_ptc(void); -hidden void __inhibit_ptc(void); - -hidden void __tl_lock(void); -hidden void __tl_unlock(void); -hidden void __tl_sync(pthread_t); -hidden struct pthread* __pthread_list_find(pthread_t, const char*); - -extern hidden volatile int __thread_list_lock; - -extern hidden volatile int __abort_lock[1]; - -extern hidden unsigned __default_stacksize; -extern hidden unsigned __default_guardsize; - -#ifdef TARGET_STACK_SIZE -#define DEFAULT_STACK_SIZE TARGET_STACK_SIZE -#else -#define DEFAULT_STACK_SIZE 131072 -#endif - -#ifdef TARGET_GUARD_SIZE -#define DEFAULT_GUARD_SIZE TARGET_GUARD_SIZE -#else -#define DEFAULT_GUARD_SIZE 8192 -#endif - -#define DEFAULT_STACK_MAX (8<<20) -#define DEFAULT_GUARD_MAX (1<<20) - -#define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1) - -#endif diff --git a/porting/linux/user/src/internal/stdio_impl.h b/porting/linux/user/src/internal/stdio_impl.h deleted file mode 100644 index 6610c8899..000000000 --- a/porting/linux/user/src/internal/stdio_impl.h +++ /dev/null @@ -1,181 +0,0 @@ -#ifndef _STDIO_IMPL_H -#define _STDIO_IMPL_H - -#include -#include -#include "syscall.h" - -#define UNGET 8 - -#define FFINALLOCK(f) ((f)->lock>=0 ? __lockfile((f)) : 0) -#define FLOCK(f) int __need_unlock = ((f)->lock>=0 ? __lockfile((f)) : 0) -#define FUNLOCK(f) do { if (__need_unlock) __unlockfile((f)); } while (0) - -#define F_PERM 1 -#define F_NORD 4 -#define F_NOWR 8 -#define F_EOF 16 -#define F_ERR 32 -#define F_SVB 64 -#define F_APP 128 -#define F_NOBUF 256 -#define F_PBUF 512 - - -#define FILE_LIST_NEXT(fl) (fl->next) -#define FILE_LIST_HEAD(head) (head) -#define FILE_LIST_EMPTY(head) ((head) == NULL) -#define FILE_SAVELINK(name, link) void **name = (void *)&(link) -#define INVALID_LINK(x) do {(x) = (void *)-1;} while (0) - -#define FILE_LIST_CHECK_NEXT(fl) do { \ - if (FILE_LIST_NEXT(fl) != NULL && FILE_LIST_NEXT(fl)->prev != &fl->next) { \ - abort(); \ - } \ -} while(0) - -#define FILE_LIST_CHECK_PREV(fl) do { \ - if (*fl->prev != fl) { \ - abort();\ - } \ -} while(0) - -#define FILE_LIST_CHECK_HEAD(head) do { \ - if (FILE_LIST_HEAD(head) != NULL && \ - FILE_LIST_HEAD(head)->prev != &FILE_LIST_HEAD(head)) {\ - abort();\ - } \ -} while(0) - - -#define FILE_LIST_INSERT_HEAD(head, fl) do { \ - FILE_LIST_CHECK_HEAD((head)); \ - if ((FILE_LIST_NEXT((fl)) = FILE_LIST_HEAD((head))) != NULL) { \ - FILE_LIST_HEAD((head))->prev = &FILE_LIST_NEXT((fl)); \ - } \ - FILE_LIST_HEAD(head) = (fl); \ - (fl)->prev = &FILE_LIST_HEAD((head)); \ -} while(0) - -#define FILE_LIST_REMOVE(fl) do { \ - FILE_SAVELINK(oldnext, (fl)->next); \ - FILE_SAVELINK(oldprev, (fl)->prev); \ - FILE_LIST_CHECK_NEXT(fl); \ - FILE_LIST_CHECK_PREV(fl); \ - if (FILE_LIST_NEXT(fl) != NULL) { \ - FILE_LIST_NEXT(fl)->prev = fl->prev; \ - } \ - *fl->prev = FILE_LIST_NEXT(fl); \ - INVALID_LINK(*oldnext); \ - INVALID_LINK(*oldprev); \ -} while(0) - -struct _IO_FILE { - unsigned flags; - unsigned char *rpos, *rend; - int (*close)(FILE *); - unsigned char *wend, *wpos; - unsigned char *mustbezero_1; - unsigned char *wbase; - size_t (*read)(FILE *, unsigned char *, size_t); - size_t (*readx)(FILE *, unsigned char *, size_t); - size_t (*write)(FILE *, const unsigned char *, size_t); - off_t (*seek)(FILE *, off_t, int); - unsigned char *buf; - size_t buf_size; - /* when allocating buffer dynamically, base == buf - UNGET, - * free base when calling fclose. - * otherwise, base == NULL, cases: - * 1. in stdout, stdin, stdout, base is static array. - * 2. call setvbuf to set buffer or non-buffer. - * 3. call fmemopen, base == NULL && buf_size != 0. - */ - unsigned char *base; - FILE **prev, *next; - int fd; - int pipe_pid; - long lockcount; - int mode; - volatile int lock; - int lbf; - void *cookie; - off_t off; - char *getln_buf; - void *mustbezero_2; - unsigned char *shend; - off_t shlim, shcnt; - FILE *prev_locked, *next_locked; - struct __locale_struct *locale; -}; - -extern hidden FILE *volatile __stdin_used; -extern hidden FILE *volatile __stdout_used; -extern hidden FILE *volatile __stderr_used; - -hidden int __lockfile(FILE *); -hidden void __unlockfile(FILE *); - -hidden size_t __stdio_read(FILE *, unsigned char *, size_t); -hidden size_t __stdio_readx(FILE *, unsigned char *, size_t); -hidden size_t __stdio_write(FILE *, const unsigned char *, size_t); -hidden size_t __stdout_write(FILE *, const unsigned char *, size_t); -hidden off_t __stdio_seek(FILE *, off_t, int); -hidden int __stdio_close(FILE *); - -hidden int __fill_buffer(FILE *f); -hidden ssize_t __flush_buffer(FILE *f); - -hidden int __toread(FILE *); -hidden int __towrite(FILE *); -hidden int __falloc_buf(FILE *); - -hidden void __stdio_exit(void); -hidden void __stdio_exit_needed(void); - -#if defined(__PIC__) && (100*__GNUC__+__GNUC_MINOR__ >= 303) -__attribute__((visibility("protected"))) -#endif -int __overflow(FILE *, int), __uflow(FILE *); - -hidden int __fseeko(FILE *, off_t, int); -hidden int __fseeko_unlocked(FILE *, off_t, int); -hidden off_t __ftello(FILE *); -hidden off_t __ftello_unlocked(FILE *); -hidden size_t __fwritex(const unsigned char *, size_t, FILE *); -hidden int __putc_unlocked(int, FILE *); - -hidden FILE *__fdopen(int, const char *); -hidden FILE *__fdopenx(int, int); -hidden int __fmodeflags(const char *, int *); - -hidden FILE *__ofl_add(FILE *f); -hidden FILE **__ofl_lock(void); -hidden void __ofl_unlock(void); -hidden void __ofl_free(FILE *f); -hidden FILE *__ofl_alloc(); - -struct __pthread; -hidden void __register_locked_file(FILE *, struct __pthread *); -hidden void __unlist_locked_file(FILE *); -hidden void __do_orphaned_stdio_locks(void); - -#define MAYBE_WAITERS 0x40000000 - -hidden void __getopt_msg(const char *, const char *, const char *, size_t); - -#define feof(f) ((f)->flags & F_EOF) -#define ferror(f) ((f)->flags & F_ERR) - -#define getc_unlocked(f) \ - ( ((f)->rpos != (f)->rend) ? *(f)->rpos++ : __uflow((f)) ) - -#define putc_unlocked(c, f) \ - ( (((unsigned char)(c)!=(f)->lbf && (f)->wpos!=(f)->wend)) \ - ? *(f)->wpos++ = (unsigned char)(c) \ - : __overflow((f),(unsigned char)(c)) ) - -/* Caller-allocated FILE * operations */ -hidden FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t); -hidden int __fclose_ca(FILE *); - -#endif diff --git a/porting/linux/user/src/internal/syscall.h b/porting/linux/user/src/internal/syscall.h deleted file mode 100644 index 5c6fbf83c..000000000 --- a/porting/linux/user/src/internal/syscall.h +++ /dev/null @@ -1,402 +0,0 @@ -#ifndef _INTERNAL_SYSCALL_H -#define _INTERNAL_SYSCALL_H - -#include -#include -#include -#include "syscall_arch.h" - -#ifndef SYSCALL_RLIM_INFINITY -#define SYSCALL_RLIM_INFINITY (~0ULL) -#endif - -#ifndef SYSCALL_MMAP2_UNIT -#define SYSCALL_MMAP2_UNIT 4096ULL -#endif - -#ifndef __SYSCALL_LL_PRW -#define __SYSCALL_LL_PRW(x) __SYSCALL_LL_O(x) -#endif - -#ifndef __scc -#define __scc(X) ((long) (X)) -typedef long syscall_arg_t; -#endif - -hidden long __syscall_ret(unsigned long), - __syscall_cp(syscall_arg_t, syscall_arg_t, syscall_arg_t, syscall_arg_t, - syscall_arg_t, syscall_arg_t, syscall_arg_t); - -#define __syscall1(n,a) __syscall1(n,__scc(a)) -#define __syscall2(n,a,b) __syscall2(n,__scc(a),__scc(b)) -#define __syscall3(n,a,b,c) __syscall3(n,__scc(a),__scc(b),__scc(c)) -#define __syscall4(n,a,b,c,d) __syscall4(n,__scc(a),__scc(b),__scc(c),__scc(d)) -#define __syscall5(n,a,b,c,d,e) __syscall5(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e)) -#define __syscall6(n,a,b,c,d,e,f) __syscall6(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f)) -#define __syscall7(n,a,b,c,d,e,f,g) __syscall7(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f),__scc(g)) - -#define __SYSCALL_NARGS_X(a,b,c,d,e,f,g,h,n,...) n -#define __SYSCALL_NARGS(...) __SYSCALL_NARGS_X(__VA_ARGS__,7,6,5,4,3,2,1,0,) -#define __SYSCALL_CONCAT_X(a,b) a##b -#define __SYSCALL_CONCAT(a,b) __SYSCALL_CONCAT_X(a,b) -#define __SYSCALL_DISP(b,...) __SYSCALL_CONCAT(b,__SYSCALL_NARGS(__VA_ARGS__))(__VA_ARGS__) - -#define __syscall(...) __SYSCALL_DISP(__syscall,__VA_ARGS__) -#define syscall(...) __syscall_ret(__syscall(__VA_ARGS__)) - -#define socketcall(nm,a,b,c,d,e,f) __syscall_ret(__socketcall(nm,a,b,c,d,e,f)) -#define socketcall_cp(nm,a,b,c,d,e,f) __syscall_ret(__socketcall_cp(nm,a,b,c,d,e,f)) - -#define __syscall_cp0(n) (__syscall_cp)(n,0,0,0,0,0,0) -#define __syscall_cp1(n,a) (__syscall_cp)(n,__scc(a),0,0,0,0,0) -#define __syscall_cp2(n,a,b) (__syscall_cp)(n,__scc(a),__scc(b),0,0,0,0) -#define __syscall_cp3(n,a,b,c) (__syscall_cp)(n,__scc(a),__scc(b),__scc(c),0,0,0) -#define __syscall_cp4(n,a,b,c,d) (__syscall_cp)(n,__scc(a),__scc(b),__scc(c),__scc(d),0,0) -#define __syscall_cp5(n,a,b,c,d,e) (__syscall_cp)(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),0) -#define __syscall_cp6(n,a,b,c,d,e,f) (__syscall_cp)(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f)) - -#ifdef FEATURE_PTHREAD_CANCEL -#define __syscall_cp(...) __SYSCALL_DISP(__syscall_cp,__VA_ARGS__) -#define syscall_cp(...) __syscall_ret(__syscall_cp(__VA_ARGS__)) -#else -#define __syscall_cp(...) __SYSCALL_DISP(__syscall,__VA_ARGS__) -#define syscall_cp(...) __syscall_ret(__syscall(__VA_ARGS__)) -#endif - -static inline long __alt_socketcall(int sys, int sock, int cp, long a, long b, long c, long d, long e, long f) -{ - long r; - if (cp) r = __syscall_cp(sys, a, b, c, d, e, f); - else r = __syscall(sys, a, b, c, d, e, f); - if (r != -ENOSYS) return r; -#ifdef SYS_socketcall - if (cp) r = __syscall_cp(SYS_socketcall, sock, ((long[6]){a, b, c, d, e, f})); - else r = __syscall(SYS_socketcall, sock, ((long[6]){a, b, c, d, e, f})); -#endif - return r; -} -#define __socketcall(nm, a, b, c, d, e, f) __alt_socketcall(SYS_##nm, __SC_##nm, 0, \ - (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), (long)(f)) -#define __socketcall_cp(nm, a, b, c, d, e, f) __alt_socketcall(SYS_##nm, __SC_##nm, 1, \ - (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), (long)(f)) - -/* fixup legacy 16-bit junk */ - -#ifdef SYS_getuid32 -#undef SYS_lchown -#undef SYS_getuid -#undef SYS_getgid -#undef SYS_geteuid -#undef SYS_getegid -#undef SYS_setreuid -#undef SYS_setregid -#undef SYS_getgroups -#undef SYS_setgroups -#undef SYS_fchown -#undef SYS_setresuid -#undef SYS_getresuid -#undef SYS_setresgid -#undef SYS_getresgid -#undef SYS_chown -#undef SYS_setuid -#undef SYS_setgid -#undef SYS_setfsuid -#undef SYS_setfsgid -#define SYS_lchown SYS_lchown32 -#define SYS_getuid SYS_getuid32 -#define SYS_getgid SYS_getgid32 -#define SYS_geteuid SYS_geteuid32 -#define SYS_getegid SYS_getegid32 -#define SYS_setreuid SYS_setreuid32 -#define SYS_setregid SYS_setregid32 -#define SYS_getgroups SYS_getgroups32 -#define SYS_setgroups SYS_setgroups32 -#define SYS_fchown SYS_fchown32 -#define SYS_setresuid SYS_setresuid32 -#define SYS_getresuid SYS_getresuid32 -#define SYS_setresgid SYS_setresgid32 -#define SYS_getresgid SYS_getresgid32 -#define SYS_chown SYS_chown32 -#define SYS_setuid SYS_setuid32 -#define SYS_setgid SYS_setgid32 -#define SYS_setfsuid SYS_setfsuid32 -#define SYS_setfsgid SYS_setfsgid32 -#endif - - -/* fixup legacy 32-bit-vs-lfs64 junk */ - -#ifdef SYS_fcntl64 -#undef SYS_fcntl -#define SYS_fcntl SYS_fcntl64 -#endif - -#ifdef SYS_getdents64 -#undef SYS_getdents -#define SYS_getdents SYS_getdents64 -#endif - -#ifdef SYS_ftruncate64 -#undef SYS_ftruncate -#undef SYS_truncate -#define SYS_ftruncate SYS_ftruncate64 -#define SYS_truncate SYS_truncate64 -#endif - -#ifdef SYS_stat64 -#undef SYS_stat -#define SYS_stat SYS_stat64 -#endif - -#ifdef SYS_fstat64 -#undef SYS_fstat -#define SYS_fstat SYS_fstat64 -#endif - -#ifdef SYS_lstat64 -#undef SYS_lstat -#define SYS_lstat SYS_lstat64 -#endif - -#ifdef SYS_statfs64 -#undef SYS_statfs -#define SYS_statfs SYS_statfs64 -#endif - -#ifdef SYS_fstatfs64 -#undef SYS_fstatfs -#define SYS_fstatfs SYS_fstatfs64 -#endif - -#if defined(SYS_newfstatat) -#undef SYS_fstatat -#define SYS_fstatat SYS_newfstatat -#elif defined(SYS_fstatat64) -#undef SYS_fstatat -#define SYS_fstatat SYS_fstatat64 -#endif - -#ifdef SYS_ugetrlimit -#undef SYS_getrlimit -#define SYS_getrlimit SYS_ugetrlimit -#endif - -#ifdef SYS__newselect -#undef SYS_select -#define SYS_select SYS__newselect -#endif - -#ifdef SYS_pread64 -#undef SYS_pread -#undef SYS_pwrite -#define SYS_pread SYS_pread64 -#define SYS_pwrite SYS_pwrite64 -#endif - -#ifdef SYS_fadvise64_64 -#undef SYS_fadvise -#define SYS_fadvise SYS_fadvise64_64 -#elif defined(SYS_fadvise64) -#undef SYS_fadvise -#define SYS_fadvise SYS_fadvise64 -#endif - -#ifdef SYS_sendfile64 -#undef SYS_sendfile -#define SYS_sendfile SYS_sendfile64 -#endif - -#ifndef SYS_timer_settime -#define SYS_timer_settime SYS_timer_settime32 -#endif - -#ifndef SYS_timer_gettime -#define SYS_timer_gettime SYS_timer_gettime32 -#endif - -#ifndef SYS_timerfd_settime -#define SYS_timerfd_settime SYS_timerfd_settime32 -#endif - -#ifndef SYS_timerfd_gettime -#define SYS_timerfd_gettime SYS_timerfd_gettime32 -#endif - -#ifndef SYS_clock_settime -#define SYS_clock_settime SYS_clock_settime32 -#endif - -#ifndef SYS_clock_gettime -#define SYS_clock_gettime SYS_clock_gettime32 -#endif - -#ifndef SYS_clock_getres -#define SYS_clock_getres SYS_clock_getres_time32 -#endif - -#ifndef SYS_clock_nanosleep -#define SYS_clock_nanosleep SYS_clock_nanosleep_time32 -#endif - -#ifndef SYS_gettimeofday -#define SYS_gettimeofday SYS_gettimeofday_time32 -#endif - -#ifndef SYS_settimeofday -#define SYS_settimeofday SYS_settimeofday_time32 -#endif - -/* Ensure that the plain syscall names are defined even for "time64-only" - * archs. These facilitate callers passing null time arguments, and make - * tests for establishing which to use/fallback-to more consistent when - * they do need to be called with time arguments. */ - -#ifndef SYS_clock_gettime -#define SYS_clock_gettime SYS_clock_gettime64 -#endif - -#ifndef SYS_clock_settime -#define SYS_clock_settime SYS_clock_settime64 -#endif - -#ifndef SYS_clock_adjtime -#define SYS_clock_adjtime SYS_clock_adjtime64 -#endif - -#ifndef SYS_clock_getres -#define SYS_clock_getres SYS_clock_getres_time64 -#endif - -#ifndef SYS_clock_nanosleep -#define SYS_clock_nanosleep SYS_clock_nanosleep_time64 -#endif - -#ifndef SYS_timer_gettime -#define SYS_timer_gettime SYS_timer_gettime64 -#endif - -#ifndef SYS_timer_settime -#define SYS_timer_settime SYS_timer_settime64 -#endif - -#ifndef SYS_timerfd_gettime -#define SYS_timerfd_gettime SYS_timerfd_gettime64 -#endif - -#ifndef SYS_timerfd_settime -#define SYS_timerfd_settime SYS_timerfd_settime64 -#endif - -#ifndef SYS_utimensat -#define SYS_utimensat SYS_utimensat_time64 -#endif - -#ifndef SYS_ppoll -#define SYS_ppoll SYS_ppoll_time64 -#endif - -#ifndef SYS_recvmmsg -#define SYS_recvmmsg SYS_recvmmsg_time64 -#endif - -#ifndef SYS_mq_timedsend -#define SYS_mq_timedsend SYS_mq_timedsend_time64 -#endif - -#ifndef SYS_mq_timedreceive -#define SYS_mq_timedreceive SYS_mq_timedreceive_time64 -#endif - -/* SYS_semtimedop omitted because SYS_ipc may provide it */ - -#ifndef SYS_rt_sigtimedwait -#define SYS_rt_sigtimedwait SYS_rt_sigtimedwait_time64 -#endif - -#ifndef SYS_futex -#define SYS_futex SYS_futex_time64 -#endif - -#ifndef SYS_sched_rr_get_interval -#define SYS_sched_rr_get_interval SYS_sched_rr_get_interval_time64 -#endif - - - - -/* socketcall calls */ - -#define __SC_socket 1 -#define __SC_bind 2 -#define __SC_connect 3 -#define __SC_listen 4 -#define __SC_accept 5 -#define __SC_getsockname 6 -#define __SC_getpeername 7 -#define __SC_socketpair 8 -#define __SC_send 9 -#define __SC_recv 10 -#define __SC_sendto 11 -#define __SC_recvfrom 12 -#define __SC_shutdown 13 -#define __SC_setsockopt 14 -#define __SC_getsockopt 15 -#define __SC_sendmsg 16 -#define __SC_recvmsg 17 -#define __SC_accept4 18 -#define __SC_recvmmsg 19 -#define __SC_sendmmsg 20 - -/* This is valid only because all socket syscalls are made via - * socketcall, which always fills unused argument slots with zeros. */ -#ifndef SYS_accept -#define SYS_accept SYS_accept4 -#endif - -#ifndef SO_RCVTIMEO_OLD -#define SO_RCVTIMEO_OLD 20 -#endif -#ifndef SO_SNDTIMEO_OLD -#define SO_SNDTIMEO_OLD 21 -#endif - -#define SO_TIMESTAMP_OLD 29 -#define SO_TIMESTAMPNS_OLD 35 -#define SO_TIMESTAMPING_OLD 37 -#define SCM_TIMESTAMP_OLD SO_TIMESTAMP_OLD -#define SCM_TIMESTAMPNS_OLD SO_TIMESTAMPNS_OLD -#define SCM_TIMESTAMPING_OLD SO_TIMESTAMPING_OLD - -#ifndef SIOCGSTAMP_OLD -#define SIOCGSTAMP_OLD 0x8906 -#endif -#ifndef SIOCGSTAMPNS_OLD -#define SIOCGSTAMPNS_OLD 0x8907 -#endif - -#ifdef SYS_open -#define __sys_open2(x,pn,fl) __syscall2(SYS_open, pn, (fl)|O_LARGEFILE) -#define __sys_open3(x,pn,fl,mo) __syscall3(SYS_open, pn, (fl)|O_LARGEFILE, mo) -#define __sys_open_cp2(x,pn,fl) __syscall_cp2(SYS_open, pn, (fl)|O_LARGEFILE) -#define __sys_open_cp3(x,pn,fl,mo) __syscall_cp3(SYS_open, pn, (fl)|O_LARGEFILE, mo) -#else -#define __sys_open2(x,pn,fl) __syscall3(SYS_openat, AT_FDCWD, pn, (fl)|O_LARGEFILE) -#define __sys_open3(x,pn,fl,mo) __syscall4(SYS_openat, AT_FDCWD, pn, (fl)|O_LARGEFILE, mo) -#define __sys_open_cp2(x,pn,fl) __syscall_cp3(SYS_openat, AT_FDCWD, pn, (fl)|O_LARGEFILE) -#define __sys_open_cp3(x,pn,fl,mo) __syscall_cp4(SYS_openat, AT_FDCWD, pn, (fl)|O_LARGEFILE, mo) -#endif - -#define __sys_open(...) __SYSCALL_DISP(__sys_open,,__VA_ARGS__) -#define sys_open(...) __syscall_ret(__sys_open(__VA_ARGS__)) - -#define __sys_open_cp(...) __SYSCALL_DISP(__sys_open_cp,,__VA_ARGS__) -#define sys_open_cp(...) __syscall_ret(__sys_open_cp(__VA_ARGS__)) - -hidden void __procfdname(char __buf[static 15+3*sizeof(int)], unsigned); - -hidden void *__vdsosym(const char *, const char *); - -hidden void __get_vdso_info(); -hidden void *__get_vdso_addr(const char *, const char *); - -#endif diff --git a/porting/linux/user/src/ldso/dlclose.c b/porting/linux/user/src/ldso/dlclose.c deleted file mode 100644 index 566c8c3b6..000000000 --- a/porting/linux/user/src/ldso/dlclose.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "dynlink.h" - -static int dummy(void *p) -{ - return __dl_invalid_handle(p); -} -weak_alias(dummy, __dlclose); - -int dlclose(void *p) -{ - return __dlclose(p); -} - diff --git a/porting/linux/user/src/ldso/dlerror.c b/porting/linux/user/src/ldso/dlerror.c deleted file mode 100644 index 4ff6285d5..000000000 --- a/porting/linux/user/src/ldso/dlerror.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include -#include "pthread_impl.h" -#include "dynlink.h" -#include "lock.h" -#include "fork_impl.h" - -#define malloc __libc_malloc -#define calloc __libc_calloc -#define realloc __libc_realloc -#define free __libc_free - -char *dlerror() -{ - pthread_t self = __pthread_self(); - if (!self->dlerror_flag) return 0; - self->dlerror_flag = 0; - char *s = self->dlerror_buf; - if (s == (void *)-1) - return "Dynamic linker failed to allocate memory for error message"; - else - return s; -} - -static volatile int freebuf_queue_lock[1]; -static void **freebuf_queue; -volatile int *const __dlerror_lockptr = freebuf_queue_lock; - -void __dl_thread_cleanup(void) -{ - pthread_t self = __pthread_self(); - if (self->dlerror_buf && self->dlerror_buf != (void *)-1) { - LOCK(freebuf_queue_lock); - void **p = (void **)self->dlerror_buf; - *p = freebuf_queue; - freebuf_queue = p; - UNLOCK(freebuf_queue_lock); - } -} - -hidden void __dl_vseterr(const char *fmt, va_list ap) -{ - LOCK(freebuf_queue_lock); - void **q = freebuf_queue; - freebuf_queue = 0; - UNLOCK(freebuf_queue_lock); - - while (q) { - void **p = *q; - __libc_free(q); - q = p; - } - - va_list ap2; - va_copy(ap2, ap); - pthread_t self = __pthread_self(); - if (self->dlerror_buf != (void *)-1) - __libc_free(self->dlerror_buf); - size_t len = vsnprintf(0, 0, fmt, ap2); - if (len < sizeof(void *)) len = sizeof(void *); - va_end(ap2); - char *buf = __libc_malloc(len+1); - if (buf) { - vsnprintf(buf, len+1, fmt, ap); - } else { - buf = (void *)-1; - } - self->dlerror_buf = buf; - self->dlerror_flag = 1; -} - -hidden void __dl_seterr(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - __dl_vseterr(fmt, ap); - va_end(ap); -} - -static int stub_invalid_handle(void *h) -{ - __dl_seterr("Invalid library handle %p", (void *)h); - return 1; -} - -weak_alias(stub_invalid_handle, __dl_invalid_handle); diff --git a/porting/linux/user/src/legacy/ulimit.c b/porting/linux/user/src/legacy/ulimit.c deleted file mode 100644 index d1620e6e2..000000000 --- a/porting/linux/user/src/legacy/ulimit.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include - -long ulimit(int cmd, ...) -{ - struct rlimit rl; - getrlimit(RLIMIT_FSIZE, &rl); - if (cmd == UL_SETFSIZE) { - long val; - va_list ap; - va_start(ap, cmd); - val = va_arg(ap, long); - va_end(ap); - rl.rlim_cur = 512ULL * val; - if (setrlimit(RLIMIT_FSIZE, &rl)) return -1; - } - return rl.rlim_cur == RLIM_INFINITY? LONG_MAX : rl.rlim_cur / 512; -} diff --git a/porting/linux/user/src/linux/cache.c b/porting/linux/user/src/linux/cache.c deleted file mode 100644 index c27820257..000000000 --- a/porting/linux/user/src/linux/cache.c +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "syscall.h" -#include "atomic.h" - -#ifdef SYS_cacheflush -int _flush_cache(void *addr, int len, int op) -{ - return syscall(SYS_cacheflush, addr, len, op); -} -weak_alias(_flush_cache, cacheflush); -#endif - -#ifdef SYS_cachectl -int __cachectl(void *addr, int len, int op) -{ - return syscall(SYS_cachectl, addr, len, op); -} -weak_alias(__cachectl, cachectl); -#endif - -#ifdef SYS_riscv_flush_icache - -#define VDSO_FLUSH_ICACHE_SYM "__vdso_flush_icache" -#define VDSO_FLUSH_ICACHE_VER "LINUX_4.5" - -static void *volatile vdso_func; - -static int flush_icache_init(void *start, void *end, unsigned long int flags) -{ - __get_vdso_info(); - void *p = __get_vdso_addr(VDSO_FLUSH_ICACHE_VER, VDSO_FLUSH_ICACHE_SYM); - int (*f)(void *, void *, unsigned long int) = - (int (*)(void *, void *, unsigned long int))p; - a_cas_p(&vdso_func, (void *)flush_icache_init, p); - return f ? f(start, end, flags) : -ENOSYS; -} - -static void *volatile vdso_func = (void *)flush_icache_init; - -int __riscv_flush_icache(void *start, void *end, unsigned long int flags) -{ - int (*f)(void *, void *, unsigned long int) = - (int (*)(void *, void *, unsigned long int))vdso_func; - if (f) { - int r = f(start, end, flags); - if (!r) return r; - if (r != -ENOSYS) return __syscall_ret(r); - } -} -weak_alias(__riscv_flush_icache, riscv_flush_icache); -#endif diff --git a/porting/linux/user/src/linux/clone.c b/porting/linux/user/src/linux/clone.c deleted file mode 100644 index c20833e18..000000000 --- a/porting/linux/user/src/linux/clone.c +++ /dev/null @@ -1,101 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include "pthread_impl.h" -#include "syscall.h" -#include "libc.h" -#include "proc_xid_impl.h" - -struct __clone_args { - int (*func)(void *); - void *arg; -}; - -#if defined(__arm__) || defined(__aarch64__) -#define GET_SP_REG(stack) __asm__ __volatile__ ("mov %0, sp" : "=r"(stack)) -#elif defined(__x86_64__) -#define GET_SP_REG(stack) do { \ - __asm__ __volatile__ ("mov %%rsp, %0;" : "=r"(stack) :); \ - stack = (void *)((uintptr_t)stack - 16); \ -} while (0) -#else -#define GET_SP_REG(stack) -#endif - -static int __start_child(void *clone_args) -{ - int status; - sigset_t set; - int (*func)(void *) = ((struct __clone_args *)clone_args)->func; - void *arg = ((struct __clone_args *)clone_args)->arg; - free(clone_args); - - __block_all_sigs(&set); - pthread_t self = __pthread_self(); - self->tid = __syscall(SYS_gettid); - self->pid = self->tid; - self->proc_tid = -1; - self->robust_list.off = 0; - self->robust_list.pending = 0; - self->next = self->prev = self; - __thread_list_lock = 0; - libc.threads_minus_1 = 0; - __clear_proc_pid(); - __restore_sigs(&set); - - status = func(arg); - exit(status); -} - -int clone(int (*func)(void *), void *stack, int flags, void *arg, ...) -{ - int ret; - va_list ap; - pid_t *ptid, *ctid; - void *tls; - int (*clone_func)(void *) = func; - struct __clone_args *clone_args = (struct __clone_args *)arg; - - va_start(ap, arg); - ptid = va_arg(ap, pid_t *); - tls = va_arg(ap, void *); - ctid = va_arg(ap, pid_t *); - va_end(ap); - - pthread_t self = __pthread_self(); - pid_t parent_pid = self->pid; - self->pid = 0; - pid_t caller_tid = self->tid; - - if (!(flags & (CLONE_VM | CLONE_VFORK))) { - self->tid = -1; - if (func) { - clone_args = (struct __clone_args *)malloc(sizeof(struct __clone_args)); - if (clone_args == NULL) { - errno = ENOMEM; - return -1; - } - clone_args->func = clone_func; - clone_args->arg = arg; - clone_func = __start_child; - } - } - if (!stack) { - GET_SP_REG(stack); - } - ret = __syscall_ret(__clone(clone_func, stack, flags, (void *)clone_args, ptid, tls, ctid)); - if (!(flags & (CLONE_VM | CLONE_VFORK)) && func) { - free(clone_args); - } - - if (ret != 0) { - self->pid = parent_pid; - self->tid = caller_tid; - } else if (self->tid == -1) { - self->tid = __syscall(SYS_gettid); - self->pid = self->tid; - } - return ret; -} diff --git a/porting/linux/user/src/linux/flock.c b/porting/linux/user/src/linux/flock.c deleted file mode 100644 index 3cf786b51..000000000 --- a/porting/linux/user/src/linux/flock.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include "syscall.h" - -int __flock(int fd, int op) -{ - return syscall(SYS_flock, fd, op); -} -weak_alias(__flock, flock); diff --git a/porting/linux/user/src/linux/prctl.c b/porting/linux/user/src/linux/prctl.c deleted file mode 100644 index 9da27bb47..000000000 --- a/porting/linux/user/src/linux/prctl.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include "syscall.h" - -#ifdef HOOK_ENABLE -int __libc_prctl(int op, ...); -#endif - -#ifdef HOOK_ENABLE -int __libc_prctl(int op, ...) -#else -int prctl(int op, ...) -#endif -{ - unsigned long x[4]; - int i; - va_list ap; - va_start(ap, op); - for (i=0; i<4; i++) x[i] = va_arg(ap, unsigned long); - va_end(ap); - return syscall(SYS_prctl, op, x[0], x[1], x[2], x[3]); -} diff --git a/porting/linux/user/src/linux/settimeofday.c b/porting/linux/user/src/linux/settimeofday.c deleted file mode 100644 index 2806f16c8..000000000 --- a/porting/linux/user/src/linux/settimeofday.c +++ /dev/null @@ -1,10 +0,0 @@ -#define _BSD_SOURCE -#include -#include -#include -#include "syscall.h" - -int settimeofday(const struct timeval *tv, const struct timezone *tz) -{ - return syscall(SYS_settimeofday, tv, tz); -} diff --git a/porting/linux/user/src/locale/c_locale.c b/porting/linux/user/src/locale/c_locale.c deleted file mode 100644 index 582d0305f..000000000 --- a/porting/linux/user/src/locale/c_locale.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "locale_impl.h" -#include - -static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 }; - -const struct __locale_map __c_dot_utf8 = { - .map = empty_mo, - .map_size = sizeof empty_mo, - .name = "C.UTF-8", - .flag = VALID -}; - -const struct __locale_struct __c_locale = { 0 }; -const struct __locale_struct __c_dot_utf8_locale = { - .cat[LC_CTYPE] = &__c_dot_utf8 -}; diff --git a/porting/linux/user/src/locale/dcngettext.c b/porting/linux/user/src/locale/dcngettext.c deleted file mode 100755 index 68d2f10b0..000000000 --- a/porting/linux/user/src/locale/dcngettext.c +++ /dev/null @@ -1,348 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include "locale_impl.h" -#include "atomic.h" -#include "pleval.h" -#include "lock.h" -#include "fork_impl.h" - -#define malloc __libc_malloc -#define calloc __libc_calloc -#define realloc undef -#define free undef - -#define __DIGIT_SEVEN__ 7 -#define __DIGIT_NINE__ 9 -#define __DIGIT_TEN__ 10 -#define __DIGIT_THIRTEEN__ 13 - -struct binding { - struct binding *next; - int dirlen; - volatile int active; - char *domainname; - char *dirname; - char buf[]; -}; - -static void *volatile bindings; - -static char *gettextdir(const char *domainname, size_t *dirlen) -{ - struct binding *p; - for (p=bindings; p; p=p->next) { - if (!strcmp(p->domainname, domainname) && p->active) { - *dirlen = p->dirlen; - return (char *)p->dirname; - } - } - return 0; -} - -static volatile int lock[1]; -volatile int *const __gettext_lockptr = lock; - -char *bindtextdomain(const char *domainname, const char *dirname) -{ - struct binding *p, *q; - - if (!domainname) { - return 0; - } - if (!dirname) { - return gettextdir(domainname, &(size_t){0}); - } - - size_t domlen = strnlen(domainname, NAME_MAX+1); - size_t dirlen = strnlen(dirname, PATH_MAX); - if (domlen > NAME_MAX || dirlen >= PATH_MAX) { - errno = EINVAL; - return 0; - } - - LOCK(lock); - - for (p=bindings; p; p=p->next) { - if (!strcmp(p->domainname, domainname) && - !strcmp(p->dirname, dirname)) { - break; - } - } - - if (!p) { - p = calloc(sizeof *p + domlen + dirlen + 2, 1); - if (!p) { - UNLOCK(lock); - return 0; - } - p->next = bindings; - p->dirlen = dirlen; - p->domainname = p->buf; - p->dirname = p->buf + domlen + 1; - memcpy(p->domainname, domainname, domlen+1); - memcpy(p->dirname, dirname, dirlen+1); - a_cas_p(&bindings, bindings, p); - } - - a_store(&p->active, 1); - - for (q=bindings; q; q=q->next) { - if (!strcmp(q->domainname, domainname) && q != p) { - a_store(&q->active, 0); - } - } - - UNLOCK(lock); - - return (char *)p->dirname; -} - -static const char catnames[][18] = { - "LC_CTYPE", - "LC_NUMERIC", - "LC_TIME", - "LC_COLLATE", - "LC_MONETARY", - "LC_MESSAGES", - "LC_PAPER", - "LC_NAME", - "LC_ADDRESS", - "LC_TELEPHONE", - "LC_MEASUREMENT", - "LC_IDENTIFICATION", -}; - -static const char catlens[] = { 8, 10, 7, 10, 11, 11, 8, 7, 10, 12, 14, 17 }; - -struct msgcat { - struct msgcat *next; - const void *map; - size_t map_size; - const char *plural_rule; - int nplurals; - struct binding *binding; - const struct __locale_map *lm; - int cat; -}; - -static char *dummy_gettextdomain() -{ - return "messages"; -} - -weak_alias(dummy_gettextdomain, __gettextdomain); - -char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n, int category) -{ - static struct msgcat *volatile cats; - struct msgcat *p; - struct __locale_struct *loc = CURRENT_LOCALE; - const struct __locale_map *lm; - size_t domlen; - struct binding *q; - int old_errno = errno; - - /* match gnu gettext behaviour */ - if (!msgid1) { - goto notrans; - } - - if ((unsigned)category >= LC_ALL) { - goto notrans; - } - - if (!domainname) { - domainname = __gettextdomain(); - } - - domlen = strnlen(domainname, NAME_MAX+1); - if (domlen > NAME_MAX) { - goto notrans; - } - - for (q=bindings; q; q=q->next) { - if (!strcmp(q->domainname, domainname) && q->active) { - break; - } - } - if (!q) { - goto notrans; - } - - lm = loc->cat[category]; - if (!lm) { -notrans: - errno = old_errno; - return (char *) ((n == 1) ? msgid1 : msgid2); - } - - for (p=cats; p; p=p->next) { - if (p->binding == q && p->lm == lm && p->cat == category) { - break; - } - } - - if (!p) { - const char *dirname, *locname, *catname, *modname, *locp; - size_t dirlen, loclen, catlen, modlen, alt_modlen; - void *old_cats; - size_t map_size; - - dirname = q->dirname; - locname = lm->name; - catname = catnames[category]; - - dirlen = q->dirlen; - loclen = strlen(locname); - catlen = catlens[category]; - - /* Logically split @mod suffix from locale name. */ - modname = memchr(locname, '@', loclen); - if (!modname) { - modname = locname + loclen; - } - alt_modlen = modlen = loclen - (modname-locname); - loclen = modname-locname; - - /* Drop .charset identifier; it is not used. */ - const char *csp = memchr(locname, '.', loclen); - if (csp) { - loclen = csp-locname; - } - - char name[dirlen+1 + loclen+modlen+1 + catlen+1 + domlen+3 + 1]; - const void *map; - - for (;;) { - snprintf(name, sizeof name, "%s/%.*s%.*s/%s/%s.mo\0", - dirname, (int)loclen, locname, - (int)alt_modlen, modname, catname, domainname); - if (map = __map_file(name, &map_size)) { - break; - } - - /* Try dropping @mod, _YY, then both. */ - if (alt_modlen) { - alt_modlen = 0; - } else if ((locp = memchr(locname, '_', loclen))) { - loclen = locp-locname; - alt_modlen = modlen; - } else { - break; - } - } - if (!map) { - goto notrans; - } - - p = calloc(sizeof *p, 1); - if (!p) { - __munmap((void *)map, map_size); - goto notrans; - } - p->cat = category; - p->binding = q; - p->lm = lm; - p->map = map; - p->map_size = map_size; - - const char *rule = "n!=1;"; - unsigned long np = 2; - const char *r = __mo_lookup(p->map, p->map_size, ""); - char *z; - while (r && strncmp(r, "Plural-Forms:", __DIGIT_THIRTEEN__)) { - z = strchr(r, '\n'); - r = z ? z+1 : 0; - } - if (r) { - r += __DIGIT_THIRTEEN__; - while (isspace(*r)) { - r++; - } - if (!strncmp(r, "nplurals=", __DIGIT_NINE__)) { - np = strtoul(r+__DIGIT_NINE__, &z, __DIGIT_TEN__); - r = z; - } - while (*r && *r != ';') { - r++; - } - if (*r) { - r++; - while (isspace(*r)) { - r++; - } - if (!strncmp(r, "plural=", __DIGIT_SEVEN__)) { - rule = r+__DIGIT_SEVEN__; - } - } - } - p->nplurals = np; - p->plural_rule = rule; - - do { - old_cats = cats; - p->next = old_cats; - } while (a_cas_p(&cats, old_cats, p) != old_cats); - } - - const char *trans = __mo_lookup(p->map, p->map_size, msgid1); - if (!trans) { - goto notrans; - } - - /* Non-plural-processing gettext forms pass a null pointer as - * msgid2 to request that dcngettext suppress plural processing. */ - - if (msgid2 && p->nplurals) { - unsigned long plural = __pleval(p->plural_rule, n); - if (plural > p->nplurals) { - goto notrans; - } - while (plural--) { - size_t rem = p->map_size - (trans - (char *)p->map); - size_t l = strnlen(trans, rem); - if (l+1 >= rem) { - goto notrans; - } - trans += l+1; - } - } - errno = old_errno; - return (char *)trans; -} - -char *dcgettext(const char *domainname, const char *msgid, int category) -{ - return dcngettext(domainname, msgid, 0, 1, category); -} - -char *dngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n) -{ - return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES); -} - -char *dgettext(const char *domainname, const char *msgid) -{ - return dcngettext(domainname, msgid, 0, 1, LC_MESSAGES); -} diff --git a/porting/linux/user/src/locale/langinfo.c b/porting/linux/user/src/locale/langinfo.c deleted file mode 100644 index 71711492d..000000000 --- a/porting/linux/user/src/locale/langinfo.c +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "locale_impl.h" - -#define __INDEX_THREE__ 3 - -static const char c_time[] = - "Sun\0" "Mon\0" "Tue\0" "Wed\0" "Thu\0" "Fri\0" "Sat\0" - "Sunday\0" "Monday\0" "Tuesday\0" "Wednesday\0" - "Thursday\0" "Friday\0" "Saturday\0" - "Jan\0" "Feb\0" "Mar\0" "Apr\0" "May\0" "Jun\0" - "Jul\0" "Aug\0" "Sep\0" "Oct\0" "Nov\0" "Dec\0" - "January\0" "February\0" "March\0" "April\0" - "May\0" "June\0" "July\0" "August\0" - "September\0" "October\0" "November\0" "December\0" - "AM\0" "PM\0" - "%a %b %e %T %Y\0" - "%m/%d/%y\0" - "%H:%M:%S\0" - "%I:%M:%S %p\0" - "\0" - "\0" - "%m/%d/%y\0" - "0123456789\0" - "%a %b %e %T %Y\0" - "%H:%M:%S\0" - "am\0" "pm"; - -static const char c_messages[] = "^[yY]\0" "^[nN]\0" "yes\0" "no"; -static const char c_numeric[] = ".\0" ""; - -char *__nl_langinfo_l(nl_item item, locale_t loc) -{ - int cat = item >> 16; - int idx = item & 65535; - const char *str; - - if (item == CODESET) return loc->cat[LC_CTYPE] ? "UTF-8" : "ASCII"; - - /* _NL_LOCALE_NAME extension */ - if (idx == 65535 && cat < LC_ALL) - return loc->cat[cat] ? (char *)loc->cat[cat]->name : "C"; - - switch (cat) { - case LC_NUMERIC: - if (idx > 1) { - return ""; - } - str = c_numeric; - break; - case LC_TIME: - if (idx > 0x33) { - return ""; - } - str = c_time; - break; - case LC_MONETARY: - if (idx > 0) { - return ""; - } - str = ""; - break; - case LC_MESSAGES: - if (idx > __INDEX_THREE__) { - return ""; - } - str = c_messages; - break; - default: - return ""; - } - - for (; idx; idx--, str++) { - for (; *str; str++); - } - if (cat != LC_NUMERIC && *str) { - str = LCTRANS(str, cat, loc); - } - return (char *)str; -} - -char *__nl_langinfo(nl_item item) -{ - return __nl_langinfo_l(item, CURRENT_LOCALE); -} - -weak_alias(__nl_langinfo, nl_langinfo); -weak_alias(__nl_langinfo_l, nl_langinfo_l); diff --git a/porting/linux/user/src/locale/locale_map.c b/porting/linux/user/src/locale/locale_map.c deleted file mode 100755 index b2f9fc288..000000000 --- a/porting/linux/user/src/locale/locale_map.c +++ /dev/null @@ -1,162 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include "locale_impl.h" -#include "libc.h" -#include "lock.h" -#include "fork_impl.h" - -#define malloc __libc_malloc -#define calloc undef -#define realloc undef -#define free undef - -const char *__lctrans_impl(const char *msg, const struct __locale_map *lm) -{ - const char *trans = 0; - if (lm) trans = __mo_lookup(lm->map, lm->map_size, msg); - return trans ? trans : msg; -} - -static const char envvars[][18] = { - "LC_CTYPE", - "LC_NUMERIC", - "LC_TIME", - "LC_COLLATE", - "LC_MONETARY", - "LC_MESSAGES", - "LC_PAPER", - "LC_NAME", - "LC_ADDRESS", - "LC_TELEPHONE", - "LC_MEASUREMENT", - "LC_IDENTIFICATION", -}; - -volatile int __locale_lock[1]; -volatile int *const __locale_lockptr = __locale_lock; - -const struct __locale_map *__get_locale(int cat, const char *val) -{ - static void *volatile loc_head; - const struct __locale_map *p; - struct __locale_map *new = 0; - const char *path = 0, *z; - char buf[256]; - size_t l, n; - - if (!*val) { - (val = getenv("LC_ALL")) && *val || - (val = getenv(envvars[cat])) && *val || - (val = getenv("LANG")) && *val || - (val = "C.UTF-8"); - } - - /* Limit name length and forbid leading dot or any slashes. */ - for (n=0; nnext) { - if (!strcmp(val, p->name)) { - return p; - } - } - - if (!strcmp(val, "en_US.UTF-8") || !strcmp(val, "C.UTF-8")) { - /* If no locale definition was found, make a locale map - * object anyway to store the name, which is kept for the - * sake of being able to do message translations at the - * application level. */ - if (!new && (new = malloc(sizeof *new))) { - new->map = __c_dot_utf8.map; - new->map_size = __c_dot_utf8.map_size; - memcpy(new->name, val, n); - new->name[n] = 0; - new->next = loc_head; - new->flag = VALID; - loc_head = new; - } - } - - if (!libc.secure) { - path = getenv("MUSL_LOCPATH"); - } - - if (path) for (; *path; path=z+!!*z) { - z = __strchrnul(path, ':'); - l = z - path; - if (l >= sizeof buf - n - 2) { - continue; - } - memcpy(buf, path, l); - buf[l] = '/'; - memcpy(buf+l+1, val, n); - buf[l+1+n] = 0; - size_t map_size; - const void *map = __map_file(buf, &map_size); - if (map) { - new = malloc(sizeof *new); - if (!new) { - __munmap((void *)map, map_size); - break; - } - new->map = map; - new->map_size = map_size; - memcpy(new->name, val, n); - new->name[n] = 0; - new->next = loc_head; - new->flag = VALID; - loc_head = new; - break; - } - } - - - /* If no locale definition was found, make a locale map - * object anyway to store the name, which is kept for the - * sake of being able to do message translations at the - * application level. */ - if (!new && (new = malloc(sizeof *new))) { - new->map = __c_dot_utf8.map; - new->map_size = __c_dot_utf8.map_size; - memcpy(new->name, val, n); - new->name[n] = 0; - new->next = loc_head; - new->flag = INVALID; - loc_head = new; - } - - /* For LC_CTYPE, never return a null pointer unless the - * requested name was "C" or "POSIX". */ - if (!new && cat == LC_CTYPE) new = (void *)&__c_dot_utf8; - - return new; -} - diff --git a/porting/linux/user/src/locale/newlocale.c b/porting/linux/user/src/locale/newlocale.c deleted file mode 100644 index 451e593f8..000000000 --- a/porting/linux/user/src/locale/newlocale.c +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include "locale_impl.h" -#include "lock.h" - -#define malloc __libc_malloc -#define calloc undef -#define realloc undef -#define free undef - -static int default_locale_init_done; -static struct __locale_struct default_locale, default_ctype_locale; - -int __loc_is_allocated(locale_t loc) -{ - return loc && loc != C_LOCALE && loc != UTF8_LOCALE - && loc != &default_locale && loc != &default_ctype_locale; -} - -static locale_t do_newlocale(int mask, const char *name, locale_t loc) -{ - struct __locale_struct tmp; - - for (int i=0; icat[i] : - __get_locale(i, (mask & (1<flag == INVALID) - return 0; - } - - /* For locales with allocated storage, modify in-place. */ - if (__loc_is_allocated(loc)) { - *loc = tmp; - return loc; - } - - /* Otherwise, first see if we can use one of the builtin locales. - * This makes the common usage case for newlocale, getting a C locale - * with predictable behavior, very fast, and more importantly, fail-safe. */ - if (!memcmp(&tmp, C_LOCALE, sizeof tmp)) return C_LOCALE; - if (!memcmp(&tmp, UTF8_LOCALE, sizeof tmp)) return UTF8_LOCALE; - - /* And provide builtins for the initial default locale, and a - * variant of the C locale honoring the default locale's encoding. */ - if (!default_locale_init_done) { - for (int i=0; i -#include -#include -#include "locale_impl.h" -#include "libc.h" -#include "lock.h" - -static char buf[LC_ALL*(LOCALE_NAME_MAX+1)]; - -static inline int Fresh(struct __locale_map *lm) -{ - if (lm != NULL) { - return lm->flag; - } - return VALID; -} - -char *setlocale(int cat, const char *name) -{ - const struct __locale_map *lm; - char flag = VALID; - if ((unsigned)cat > LC_ALL) return 0; - - LOCK(__locale_lock); - - /* For LC_ALL, setlocale is required to return a string which - * encodes the current setting for all categories. The format of - * this string is unspecified, and only the following code, which - * performs both the serialization and deserialization, depends - * on the format, so it can easily be changed if needed. */ - if (cat == LC_ALL) { - int i; - if (name) { - struct __locale_struct tmp_locale; - char part[LOCALE_NAME_MAX+1] = "C.UTF-8"; - const char *p = name; - for (i=0; iname : "C"; - size_t l = strlen(part); - memcpy(s, part, l); - s[l] = ';'; - s += l+1; - } - *--s = 0; - UNLOCK(__locale_lock); - if (flag == INVALID) { - return 0; - } - return same==LC_ALL ? (char *)part : buf; - } - - if (name) { - lm = __get_locale(cat, name); - if (lm == LOC_MAP_FAILED) { - UNLOCK(__locale_lock); - return 0; - } - flag = Fresh(lm); - libc.global_locale.cat[cat] = lm; - } else { - lm = libc.global_locale.cat[cat]; - } - char *ret = lm ? (char *)lm->name : "C"; - - UNLOCK(__locale_lock); - if (flag == INVALID) { - return 0; - } - return ret; -} diff --git a/porting/linux/user/src/malloc/free.c b/porting/linux/user/src/malloc/free.c deleted file mode 100644 index 18ba5ad5d..000000000 --- a/porting/linux/user/src/malloc/free.c +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HOOK_ENABLE -#include - -#ifdef USE_JEMALLOC -extern void je_free(void* p); -#endif - -void free(void *p) -{ -#ifdef USE_JEMALLOC - return je_free(p); -#endif - __libc_free(p); -} -#endif \ No newline at end of file diff --git a/porting/linux/user/src/malloc/libc_calloc.c b/porting/linux/user/src/malloc/libc_calloc.c deleted file mode 100644 index 4925914d0..000000000 --- a/porting/linux/user/src/malloc/libc_calloc.c +++ /dev/null @@ -1,61 +0,0 @@ -#include -#include -#include -#include -#include "dynlink.h" - -static size_t mal0_clear(char *p, size_t n) -{ - const size_t pagesz = 4096; /* arbitrary */ - if (n < pagesz) return n; -#ifdef __GNUC__ - typedef uint64_t __attribute__((__may_alias__)) T; -#else - typedef unsigned char T; -#endif - char *pp = p + n; - size_t i = (uintptr_t)pp & (pagesz - 1); - for (;;) { - pp = memset(pp - i, 0, i); - if (pp - p < pagesz) return pp - p; - for (i = pagesz; i; i -= 2*sizeof(T), pp -= 2*sizeof(T)) - if (((T *)pp)[-1] | ((T *)pp)[-2]) - break; - } -} - -static int allzerop(void *p) -{ - return 0; -} -weak_alias(allzerop, __malloc_allzerop); - -void *__libc_calloc(size_t m, size_t n) -{ - if (n && m > (size_t)-1/n) { - errno = ENOMEM; - return 0; - } - n *= m; - void *p = __libc_malloc(n); - if (!p || (!__malloc_replaced && __malloc_allzerop(p))) - return p; - n = mal0_clear(p, n); - return memset(p, 0, n); -} - -#ifdef HOOK_ENABLE -void *hook_calloc(size_t m, size_t n) -{ - if (n && m > (size_t)-1/n) { - errno = ENOMEM; - return 0; - } - n *= m; - void *p = __libc_malloc(n); - if (!p || (!__malloc_replaced && __malloc_allzerop(p))) - return p; - n = mal0_clear(p, n); - return memset(p, 0, n); -} -#endif diff --git a/porting/linux/user/src/malloc/lite_malloc.c b/porting/linux/user/src/malloc/lite_malloc.c deleted file mode 100644 index fcb5b6434..000000000 --- a/porting/linux/user/src/malloc/lite_malloc.c +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include -#include -#include -#include "libc.h" -#include "lock.h" -#include "syscall.h" -#include "fork_impl.h" - -#ifdef USE_JEMALLOC -extern void* je_malloc(size_t size); -#endif - -#define ALIGN 16 - -/* This function returns true if the interval [old,new] - * intersects the 'len'-sized interval below &libc.auxv - * (interpreted as the main-thread stack) or below &b - * (the current stack). It is used to defend against - * buggy brk implementations that can cross the stack. */ - -static int traverses_stack_p(uintptr_t old, uintptr_t new) -{ - const uintptr_t len = 8<<20; - uintptr_t a, b; - - b = (uintptr_t)libc.auxv; - a = b > len ? b-len : 0; - if (new>a && old len ? b-len : 0; - if (new>a && old SIZE_MAX/2) { - errno = ENOMEM; - return 0; - } - - if (!n) n++; - while (align end-cur) { - size_t req = n - (end-cur) + PAGE_SIZE-1 & -PAGE_SIZE; - - if (!cur) { - brk = __syscall(SYS_brk, 0); - brk += -brk & PAGE_SIZE-1; - cur = end = brk; - } - - if (brk == end && req < SIZE_MAX-brk - && !traverses_stack_p(brk, brk+req) - && __syscall(SYS_brk, brk+req)==brk+req) { - brk = end += req; - } else { - int new_area = 0; - req = n + PAGE_SIZE-1 & -PAGE_SIZE; - /* Only make a new area rather than individual mmap - * if wasted space would be over 1/8 of the map. */ - if (req-n > req/8) { - /* Geometric area size growth up to 64 pages, - * bounding waste by 1/8 of the area. */ - size_t min = PAGE_SIZE<<(mmap_step/2); - if (min-n > end-cur) { - if (req < min) { - req = min; - if (mmap_step < 12) - mmap_step++; - } - new_area = 1; - } - } - void *mem = __mmap(0, req, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (mem == MAP_FAILED || !new_area) { - UNLOCK(lock); - return mem==MAP_FAILED ? 0 : mem; - } - cur = (uintptr_t)mem; - end = cur + req; - } - } - - p = (void *)cur; - cur += n; - UNLOCK(lock); - return p; -} - -weak_alias(__simple_malloc, __libc_malloc_impl); - -void *__libc_malloc(size_t n) -{ - return __libc_malloc_impl(n); -} - -static void *default_malloc(size_t n) -{ -#ifdef USE_JEMALLOC - return je_malloc(n); -#endif - return __libc_malloc_impl(n); -} - -weak_alias(default_malloc, malloc); diff --git a/porting/linux/user/src/malloc/mallocng/aligned_alloc.c b/porting/linux/user/src/malloc/mallocng/aligned_alloc.c deleted file mode 100644 index ac5847e1c..000000000 --- a/porting/linux/user/src/malloc/mallocng/aligned_alloc.c +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include "meta.h" - -#ifdef USE_JEMALLOC -extern void* je_memalign(size_t align, size_t len); -#endif - -void *aligned_alloc(size_t align, size_t len) -{ -#ifdef USE_JEMALLOC - return je_memalign(align, len); -#endif - if ((align & -align) != align) { - errno = EINVAL; - return 0; - } - - if (len > SIZE_MAX - align || align >= (1ULL<<31)*UNIT) { - errno = ENOMEM; - return 0; - } - - if (DISABLE_ALIGNED_ALLOC) { - errno = ENOMEM; - return 0; - } - - if (align <= UNIT) align = UNIT; - - unsigned char *p = malloc(len + align - UNIT); - if (!p) - return 0; - - struct meta *g = get_meta(p); - int idx = get_slot_index(p); - size_t stride = get_stride(g); - unsigned char *start = g->mem->storage + stride*idx; - unsigned char *end = g->mem->storage + stride*(idx+1) - IB; - size_t adj = -(uintptr_t)p & (align-1); - - if (!adj) { - set_size(p, end, len); - return p; - } - p += adj; - uint32_t offset = (size_t)(p-g->mem->storage)/UNIT; - if (offset <= 0xffff) { - *(uint16_t *)(p-2) = offset; - p[-4] = 0; - } else { - // use a 32-bit offset if 16-bit doesn't fit. for this, - // 16-bit field must be zero, [-4] byte nonzero. - *(uint16_t *)(p-2) = 0; - *(uint32_t *)(p-8) = offset; - p[-4] = 1; - } - p[-3] = idx; - set_size(p, end, len); - // store offset to aligned enframing. this facilitates cycling - // offset and also iteration of heap for debugging/measurement. - // for extreme overalignment it won't fit but these are classless - // allocations anyway. - *(uint16_t *)(start - 2) = (size_t)(p-start)/UNIT; - start[-3] = 7<<5; - return p; -} diff --git a/porting/linux/user/src/malloc/mallocng/donate.c b/porting/linux/user/src/malloc/mallocng/donate.c deleted file mode 100644 index 7c5a28473..000000000 --- a/porting/linux/user/src/malloc/mallocng/donate.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "meta.h" - -static void donate(unsigned char *base, size_t len) -{ - uintptr_t a = (uintptr_t)base; - uintptr_t b = a + len; - a += -a & (UNIT-1); - b -= b & (UNIT-1); - memset(base, 0, len); - for (int sc=47; sc>0 && b>a; sc-=4) { - if (b-a < (size_classes[sc]+1)*UNIT) continue; - struct meta *m = alloc_meta(); - m->avail_mask = 0; - m->freed_mask = 1; - m->mem = (void *)a; - m->mem->meta = encode_ptr(m, ctx.secret); - m->last_idx = 0; - m->freeable = 0; - m->sizeclass = sc; - m->maplen = 0; - *((unsigned char *)m->mem+UNIT-4) = 0; - *((unsigned char *)m->mem+UNIT-3) = 255; - m->mem->storage[size_classes[sc]*UNIT-4] = 0; - queue(&ctx.active[sc], m); - a += (size_classes[sc]+1)*UNIT; - } -} - -void __malloc_donate(char *start, char *end) -{ - donate((void *)start, end-start); -} diff --git a/porting/linux/user/src/malloc/mallocng/malloc.c b/porting/linux/user/src/malloc/mallocng/malloc.c deleted file mode 100644 index e79e3cbd2..000000000 --- a/porting/linux/user/src/malloc/mallocng/malloc.c +++ /dev/null @@ -1,499 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "meta.h" - -#ifdef USE_JEMALLOC -#ifdef USE_JEMALLOC_DFX_INTF -extern void je_malloc_disable(); -extern void je_malloc_enable(); -extern int je_iterate(uintptr_t base, size_t size, - void (*callback)(uintptr_t ptr, size_t size, void* arg), void* arg); -extern int je_mallopt(int param, int value); -#endif -#endif - -#ifdef MALLOC_SECURE_ALL -#include -#define RANDOM_BUFFER_LEN 512 -static uint8_t buffer[RANDOM_BUFFER_LEN] = { 0 }; -static size_t ri = RANDOM_BUFFER_LEN; - -static uint8_t get_random8() -{ - uint8_t num; - if ((ri >= RANDOM_BUFFER_LEN) || (buffer[0] == 0)) { - int fd = open("/dev/urandom", O_RDONLY); - if (fd < 0) { - num = (uint8_t)get_random_secret(); - return num; - } - - read(fd, buffer, RANDOM_BUFFER_LEN); - close(fd); - ri = 0; - } - num = buffer[ri]; - ri++; - return num; -} - -static int get_randomIdx(int avail_mask, int last_idx) -{ - uint32_t mask; - uint32_t r; - uint32_t cmask; - int idx; - - mask = avail_mask; - r = get_random8() % last_idx; - cmask = ~((2u << (last_idx - r)) - 1); - - if (mask & cmask) { - idx = 31 - a_clz_32(mask & cmask); - } else { - idx = a_ctz_32(mask); - } - - return idx; -} -#endif - -LOCK_OBJ_DEF; - -const uint16_t size_classes[] = { - 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10, 12, 15, - 18, 20, 25, 31, - 36, 42, 50, 63, - 72, 84, 102, 127, - 146, 170, 204, 255, - 292, 340, 409, 511, - 584, 682, 818, 1023, - 1169, 1364, 1637, 2047, - 2340, 2730, 3276, 4095, - 4680, 5460, 6552, 8191, -}; - -static const uint8_t small_cnt_tab[][3] = { - { 30, 30, 30 }, - { 31, 15, 15 }, - { 20, 10, 10 }, - { 31, 15, 7 }, - { 25, 12, 6 }, - { 21, 10, 5 }, - { 18, 8, 4 }, - { 31, 15, 7 }, - { 28, 14, 6 }, -}; - -static const uint8_t med_cnt_tab[4] = { 28, 24, 20, 32 }; - -struct malloc_context ctx = { 0 }; - -struct meta *alloc_meta(void) -{ - struct meta *m; - unsigned char *p; - if (!ctx.init_done) { -#ifndef PAGESIZE - ctx.pagesize = get_page_size(); -#endif - ctx.secret = get_random_secret(); - ctx.init_done = 1; - } - size_t pagesize = PGSZ; - if (pagesize < 4096) pagesize = 4096; - if ((m = dequeue_head(&ctx.free_meta_head))) return m; - if (!ctx.avail_meta_count) { - int need_unprotect = 1; - if (!ctx.avail_meta_area_count && ctx.brk!=-1) { - uintptr_t new = ctx.brk + pagesize; - int need_guard = 0; - if (!ctx.brk) { - need_guard = 1; - ctx.brk = brk(0); - // some ancient kernels returned _ebss - // instead of next page as initial brk. - ctx.brk += -ctx.brk & (pagesize-1); - new = ctx.brk + 2*pagesize; - } - if (brk(new) != new) { - ctx.brk = -1; - } else { - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ctx.brk, new - ctx.brk, "native_heap:meta"); - if (need_guard) mmap((void *)ctx.brk, pagesize, - PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0); - ctx.brk = new; - ctx.avail_meta_areas = (void *)(new - pagesize); - ctx.avail_meta_area_count = pagesize>>12; - need_unprotect = 0; - } - } - if (!ctx.avail_meta_area_count) { - size_t n = 2UL << ctx.meta_alloc_shift; - p = mmap(0, n*pagesize, PROT_NONE, - MAP_PRIVATE|MAP_ANON, -1, 0); - if (p==MAP_FAILED) return 0; - ctx.avail_meta_areas = p + pagesize; - ctx.avail_meta_area_count = (n-1)*(pagesize>>12); - ctx.meta_alloc_shift++; - } - p = ctx.avail_meta_areas; - if ((uintptr_t)p & (pagesize-1)) need_unprotect = 0; - if (need_unprotect) - if (mprotect(p, pagesize, PROT_READ|PROT_WRITE) - && errno != ENOSYS) - return 0; - ctx.avail_meta_area_count--; - ctx.avail_meta_areas = p + 4096; - if (ctx.meta_area_tail) { - ctx.meta_area_tail->next = (void *)p; - } else { - ctx.meta_area_head = (void *)p; - } - ctx.meta_area_tail = (void *)p; - ctx.meta_area_tail->check = ctx.secret; - ctx.avail_meta_count = ctx.meta_area_tail->nslots - = (4096-sizeof(struct meta_area))/sizeof *m; - ctx.avail_meta = ctx.meta_area_tail->slots; - } - ctx.avail_meta_count--; - m = ctx.avail_meta++; - m->prev = m->next = 0; - return m; -} - -static uint32_t try_avail(struct meta **pm) -{ - struct meta *m = *pm; - uint32_t first; - if (!m) return 0; - uint32_t mask = m->avail_mask; - if (!mask) { - if (!m) return 0; - if (!m->freed_mask) { - dequeue(pm, m); - m = *pm; - if (!m) return 0; - } else { - m = m->next; - *pm = m; - } - - mask = m->freed_mask; - - // skip fully-free group unless it's the only one - // or it's a permanently non-freeable group - if (mask == (2u<last_idx)-1 && m->freeable) { - m = m->next; - *pm = m; - mask = m->freed_mask; - } - - // activate more slots in a not-fully-active group - // if needed, but only as a last resort. prefer using - // any other group with free slots. this avoids - // touching & dirtying as-yet-unused pages. - if (!(mask & ((2u<mem->active_idx)-1))) { - if (m->next != m) { - m = m->next; - *pm = m; - } else { - int cnt = m->mem->active_idx + 2; - int size = size_classes[m->sizeclass]*UNIT; - int span = UNIT + size*cnt; - // activate up to next 4k boundary - while ((span^(span+size-1)) < 4096) { - cnt++; - span += size; - } - if (cnt > m->last_idx+1) - cnt = m->last_idx+1; - m->mem->active_idx = cnt-1; - } - } - mask = activate_group(m); - assert(mask); - decay_bounces(m->sizeclass); - } - -#ifdef MALLOC_SECURE_ALL - int idx = get_randomIdx(mask, m->last_idx); - first = 1 << idx; -#else - first = mask&-mask; -#endif - m->avail_mask = mask-first; - return first; -} - -static int alloc_slot(int, size_t); - -static struct meta *alloc_group(int sc, size_t req) -{ - size_t size = UNIT*size_classes[sc]; - int i = 0, cnt; - unsigned char *p; - struct meta *m = alloc_meta(); - if (!m) return 0; - size_t usage = ctx.usage_by_class[sc]; - size_t pagesize = PGSZ; - int active_idx; - if (sc < 9) { - while (i<2 && 4*small_cnt_tab[sc][i] > usage) - i++; - cnt = small_cnt_tab[sc][i]; - } else { - // lookup max number of slots fitting in power-of-two size - // from a table, along with number of factors of two we - // can divide out without a remainder or reaching 1. - cnt = med_cnt_tab[sc&3]; - - // reduce cnt to avoid excessive eagar allocation. - while (!(cnt&1) && 4*cnt > usage) - cnt >>= 1; - - // data structures don't support groups whose slot offsets - // in units don't fit in 16 bits. - while (size*cnt >= 65536*UNIT) - cnt >>= 1; - } - - // If we selected a count of 1 above but it's not sufficient to use - // mmap, increase to 2. Then it might be; if not it will nest. - if (cnt==1 && size*cnt+UNIT <= pagesize/2) cnt = 2; - - // All choices of size*cnt are "just below" a power of two, so anything - // larger than half the page size should be allocated as whole pages. - if (size*cnt+UNIT > pagesize/2) { - // check/update bounce counter to start/increase retention - // of freed maps, and inhibit use of low-count, odd-size - // small mappings and single-slot groups if activated. - int nosmall = is_bouncing(sc); - account_bounce(sc); - step_seq(); - - // since the following count reduction opportunities have - // an absolute memory usage cost, don't overdo them. count - // coarse usage as part of usage. - if (!(sc&1) && sc<32) usage += ctx.usage_by_class[sc+1]; - - // try to drop to a lower count if the one found above - // increases usage by more than 25%. these reduced counts - // roughly fill an integral number of pages, just not a - // power of two, limiting amount of unusable space. - if (4*cnt > usage && !nosmall) { - if (0); - else if ((sc&3)==1 && size*cnt>8*pagesize) cnt = 2; - else if ((sc&3)==2 && size*cnt>4*pagesize) cnt = 3; - else if ((sc&3)==0 && size*cnt>8*pagesize) cnt = 3; - else if ((sc&3)==0 && size*cnt>2*pagesize) cnt = 5; - } - size_t needed = size*cnt + UNIT; - needed += -needed & (pagesize-1); - - // produce an individually-mmapped allocation if usage is low, - // bounce counter hasn't triggered, and either it saves memory - // or it avoids eagar slot allocation without wasting too much. - if (!nosmall && cnt<=7) { - req += IB + UNIT; - req += -req & (pagesize-1); - if (req=4*pagesize && 2*cnt>usage)) { - cnt = 1; - needed = req; - } - } - - p = mmap(0, needed, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (p==MAP_FAILED) { - free_meta(m); - return 0; - } - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, needed, "native_heap:brk"); - m->maplen = needed>>12; - ctx.mmap_counter++; - active_idx = (4096-UNIT)/size-1; - if (active_idx > cnt-1) active_idx = cnt-1; - if (active_idx < 0) active_idx = 0; - } else { - int j = size_to_class(UNIT+cnt*size-IB); - int idx = alloc_slot(j, UNIT+cnt*size-IB); - if (idx < 0) { - free_meta(m); - return 0; - } - struct meta *g = ctx.active[j]; - p = enframe(g, idx, UNIT*size_classes[j]-IB, ctx.mmap_counter); - m->maplen = 0; - p[-3] = (p[-3]&31) | (6<<5); - for (int i=0; i<=cnt; i++) - p[UNIT+i*size-4] = 0; - active_idx = cnt-1; - } - ctx.usage_by_class[sc] += cnt; - m->avail_mask = (2u<freed_mask = (2u<<(cnt-1))-1 - m->avail_mask; - m->mem = (void *)p; - m->mem->meta = encode_ptr(m, ctx.secret); - m->mem->active_idx = active_idx; - m->last_idx = cnt-1; - m->freeable = 1; - m->sizeclass = sc; - return m; -} - -static int alloc_slot(int sc, size_t req) -{ - uint32_t first = try_avail(&ctx.active[sc]); - if (first) return a_ctz_32(first); - - struct meta *g = alloc_group(sc, req); - if (!g) return -1; - - g->avail_mask--; - queue(&ctx.active[sc], g); - return 0; -} - -void *malloc(size_t n) -{ - if (size_overflows(n)) return 0; - struct meta *g; - uint32_t mask, first; - int sc; - int idx; - int ctr; - - if (n >= MMAP_THRESHOLD) { - size_t needed = n + IB + UNIT; - void *p = mmap(0, needed, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANON, -1, 0); - if (p==MAP_FAILED) return 0; - wrlock(); - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, needed, "native_heap:mmap"); - step_seq(); - g = alloc_meta(); - if (!g) { - unlock(); - munmap(p, needed); - return 0; - } - g->mem = p; - g->mem->meta = encode_ptr(g, ctx.secret); - g->last_idx = 0; - g->freeable = 1; - g->sizeclass = 63; - g->maplen = (needed+4095)/4096; - g->avail_mask = g->freed_mask = 0; - // use a global counter to cycle offset in - // individually-mmapped allocations. - ctx.mmap_counter++; - idx = 0; - goto success; - } - - sc = size_to_class(n); - - rdlock(); - g = ctx.active[sc]; - - // use coarse size classes initially when there are not yet - // any groups of desired size. this allows counts of 2 or 3 - // to be allocated at first rather than having to start with - // 7 or 5, the min counts for even size classes. - if (!g && sc>=4 && sc<32 && sc!=6 && !(sc&1) && !ctx.usage_by_class[sc]) { - size_t usage = ctx.usage_by_class[sc|1]; - // if a new group may be allocated, count it toward - // usage in deciding if we can use coarse class. - if (!ctx.active[sc|1] || (!ctx.active[sc|1]->avail_mask - && !ctx.active[sc|1]->freed_mask)) - usage += 3; - if (usage <= 12) - sc |= 1; - g = ctx.active[sc]; - } - - for (;;) { - mask = g ? g->avail_mask : 0; -#ifdef MALLOC_SECURE_ALL - if (!mask) break; - idx = get_randomIdx(mask, g->last_idx); - first = 1u << idx; - - if (RDLOCK_IS_EXCLUSIVE || !MT) - g->avail_mask = mask-first; - else if (a_cas(&g->avail_mask, mask, mask-first)!=mask) - continue; -#else - first = mask&-mask; - if (!first) break; - if (RDLOCK_IS_EXCLUSIVE || !MT) - g->avail_mask = mask-first; - else if (a_cas(&g->avail_mask, mask, mask-first)!=mask) - continue; - idx = a_ctz_32(first); -#endif - goto success; - } - upgradelock(); - - idx = alloc_slot(sc, n); - if (idx < 0) { - unlock(); - return 0; - } - g = ctx.active[sc]; - -success: - ctr = ctx.mmap_counter; - unlock(); - return enframe(g, idx, n, ctr); -} - -int is_allzero(void *p) -{ - struct meta *g = get_meta(p); - return g->sizeclass >= 48 || - get_stride(g) < UNIT*size_classes[g->sizeclass]; -} - -int mallopt(int param, int value) -{ -#ifdef USE_JEMALLOC_DFX_INTF - return je_mallopt(param, value); -#endif - return 0; -} - -void malloc_disable(void) -{ -#ifdef USE_JEMALLOC_DFX_INTF - je_malloc_disable(); -#endif -} - -void malloc_enable(void) -{ -#ifdef USE_JEMALLOC_DFX_INTF - je_malloc_enable(); -#endif -} - -int malloc_iterate(void* base, size_t size, void (*callback)(void* base, size_t size, void* arg), void* arg) -{ -#ifdef USE_JEMALLOC_DFX_INTF - return je_iterate(base, size, callback, arg); -#endif - return 0; -} - -ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) -{ - return 0; -} diff --git a/porting/linux/user/src/malloc/mallocng/malloc_usable_size.c b/porting/linux/user/src/malloc/mallocng/malloc_usable_size.c deleted file mode 100644 index 04ba97908..000000000 --- a/porting/linux/user/src/malloc/mallocng/malloc_usable_size.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include "meta.h" - -#ifdef USE_JEMALLOC -extern size_t je_malloc_usable_size(void *p); -#endif -#ifndef HOOK_ENABLE -size_t malloc_usable_size(void *p) -#else -size_t __libc_malloc_usable_size(void *p) -#endif -{ -#ifdef USE_JEMALLOC - return je_malloc_usable_size(p); -#endif - if (!p) return 0; - struct meta *g = get_meta(p); - int idx = get_slot_index(p); - size_t stride = get_stride(g); - unsigned char *start = g->mem->storage + stride*idx; - unsigned char *end = start + stride - IB; - return get_nominal_size(p, end); -} diff --git a/porting/linux/user/src/malloc/mallocng/meta.h b/porting/linux/user/src/malloc/mallocng/meta.h deleted file mode 100644 index 1eb0e320f..000000000 --- a/porting/linux/user/src/malloc/mallocng/meta.h +++ /dev/null @@ -1,303 +0,0 @@ -#ifndef MALLOC_META_H -#define MALLOC_META_H - -#include -#include -#include -#include "glue.h" - -__attribute__((__visibility__("hidden"))) -extern const uint16_t size_classes[]; - -#define MMAP_THRESHOLD 131052 - -#define UNIT 16 -#define IB 4 - -struct group { - struct meta *meta; - unsigned char active_idx:5; - char pad[UNIT - sizeof(struct meta *) - 1]; - unsigned char storage[]; -}; - -struct meta { - struct meta *prev, *next; - struct group *mem; - volatile int avail_mask, freed_mask; - uintptr_t last_idx:5; - uintptr_t freeable:1; - uintptr_t sizeclass:6; - uintptr_t maplen:8*sizeof(uintptr_t)-12; -}; - -struct meta_area { - uint64_t check; - struct meta_area *next; - int nslots; - struct meta slots[]; -}; - -struct malloc_context { - uint64_t secret; -#ifndef PAGESIZE - size_t pagesize; -#endif - int init_done; - unsigned mmap_counter; - struct meta *free_meta_head; - struct meta *avail_meta; - size_t avail_meta_count, avail_meta_area_count, meta_alloc_shift; - struct meta_area *meta_area_head, *meta_area_tail; - unsigned char *avail_meta_areas; - struct meta *active[48]; - size_t usage_by_class[48]; - uint8_t unmap_seq[32], bounces[32]; - uint8_t seq; - uintptr_t brk; -}; - -__attribute__((__visibility__("hidden"))) -extern struct malloc_context ctx; - -#ifdef PAGESIZE -#define PGSZ PAGESIZE -#else -#define PGSZ ctx.pagesize -#endif - -__attribute__((__visibility__("hidden"))) -struct meta *alloc_meta(void); - -__attribute__((__visibility__("hidden"))) -int is_allzero(void *); - -static inline void *encode_ptr(void *ptr, uint64_t key) -{ -#ifdef MALLOC_FREELIST_HARDENED - return (void *)((uintptr_t)ptr ^ key); -#else - (void)key; - return ptr; -#endif -} - -static inline void queue(struct meta **phead, struct meta *m) -{ - assert(!m->next); - assert(!m->prev); - if (*phead) { - struct meta *head = *phead; -#ifdef MALLOC_FREELIST_HARDENED - if (head->next->prev != head || head->prev->next != head) { - a_crash(); - } -#endif - m->next = head; - m->prev = head->prev; - m->next->prev = m->prev->next = m; - } else { - m->prev = m->next = m; - *phead = m; - } -} - -static inline void dequeue(struct meta **phead, struct meta *m) -{ - if (m->next != m) { - m->prev->next = m->next; - m->next->prev = m->prev; - if (*phead == m) *phead = m->next; - } else { - *phead = 0; - } - m->prev = m->next = 0; -} - -static inline struct meta *dequeue_head(struct meta **phead) -{ - struct meta *m = *phead; - if (m) dequeue(phead, m); - return m; -} - -static inline void free_meta(struct meta *m) -{ - *m = (struct meta){0}; - queue(&ctx.free_meta_head, m); -} - -static inline uint32_t activate_group(struct meta *m) -{ - assert(!m->avail_mask); - uint32_t mask, act = (2u<mem->active_idx)-1; - do mask = m->freed_mask; - while (a_cas(&m->freed_mask, mask, mask&~act)!=mask); - return m->avail_mask = mask & act; -} - -static inline int get_slot_index(const unsigned char *p) -{ - return p[-3] & 31; -} - -static inline struct meta *get_meta(const unsigned char *p) -{ - assert(!((uintptr_t)p & 15)); - int offset = *(const uint16_t *)(p - 2); - int index = get_slot_index(p); - if (p[-4]) { - assert(!offset); - offset = *(uint32_t *)(p - 8); - assert(offset > 0xffff); - } - const struct group *base = (const void *)(p - UNIT*offset - UNIT); - const struct meta *meta = encode_ptr(base->meta, ctx.secret); - assert(meta->mem == base); - assert(index <= meta->last_idx); - assert(!(meta->avail_mask & (1u<freed_mask & (1u<check == ctx.secret); - if (meta->sizeclass < 48) { - assert(offset >= size_classes[meta->sizeclass]*index); - assert(offset < size_classes[meta->sizeclass]*(index+1)); - } else { - assert(meta->sizeclass == 63); - } - if (meta->maplen) { - assert(offset <= meta->maplen*4096UL/UNIT - 1); - } - return (struct meta *)meta; -} - -static inline size_t get_nominal_size(const unsigned char *p, const unsigned char *end) -{ - size_t reserved = p[-3] >> 5; - if (reserved >= 5) { - assert(reserved == 5); - reserved = *(const uint32_t *)(end-4); - assert(reserved >= 5); - assert(!end[-5]); - } - assert(reserved <= end-p); - assert(!*(end-reserved)); - // also check the slot's overflow byte - assert(!*end); - return end-reserved-p; -} - -static inline size_t get_stride(const struct meta *g) -{ - if (!g->last_idx && g->maplen) { - return g->maplen*4096UL - UNIT; - } else { - return UNIT*size_classes[g->sizeclass]; - } -} - -static inline void set_size(unsigned char *p, unsigned char *end, size_t n) -{ - int reserved = end-p-n; - if (reserved) end[-reserved] = 0; - if (reserved >= 5) { - *(uint32_t *)(end-4) = reserved; - end[-5] = 0; - reserved = 5; - } - p[-3] = (p[-3]&31) + (reserved<<5); -} - -static inline void *enframe(struct meta *g, int idx, size_t n, int ctr) -{ - size_t stride = get_stride(g); - size_t slack = (stride-IB-n)/UNIT; - unsigned char *p = g->mem->storage + stride*idx; - unsigned char *end = p+stride-IB; - // cycle offset within slot to increase interval to address - // reuse, facilitate trapping double-free. - int off = (p[-3] ? *(uint16_t *)(p-2) + 1 : ctr) & 255; - assert(!p[-4]); - if (off > slack) { - size_t m = slack; - m |= m>>1; m |= m>>2; m |= m>>4; - off &= m; - if (off > slack) off -= slack+1; - assert(off <= slack); - } - if (off) { - // store offset in unused header at offset zero - // if enframing at non-zero offset. - *(uint16_t *)(p-2) = off; - p[-3] = 7<<5; - p += UNIT*off; - // for nonzero offset there is no permanent check - // byte, so make one. - p[-4] = 0; - } - *(uint16_t *)(p-2) = (size_t)(p-g->mem->storage)/UNIT; - p[-3] = idx; - set_size(p, end, n); - return p; -} - -static inline int size_to_class(size_t n) -{ - n = (n+IB-1)>>4; - if (n<10) return n; - n++; - int i = (28-a_clz_32(n))*4 + 8; - if (n>size_classes[i+1]) i+=2; - if (n>size_classes[i]) i++; - return i; -} - -static inline int size_overflows(size_t n) -{ - if (n >= SIZE_MAX/2 - 4096) { - errno = ENOMEM; - return 1; - } - return 0; -} - -static inline void step_seq(void) -{ - if (ctx.seq==255) { - for (int i=0; i<32; i++) ctx.unmap_seq[i] = 0; - ctx.seq = 1; - } else { - ctx.seq++; - } -} - -static inline void record_seq(int sc) -{ - if (sc-7U < 32) ctx.unmap_seq[sc-7] = ctx.seq; -} - -static inline void account_bounce(int sc) -{ - if (sc-7U < 32) { - int seq = ctx.unmap_seq[sc-7]; - if (seq && ctx.seq-seq < 10) { - if (ctx.bounces[sc-7]+1 < 100) - ctx.bounces[sc-7]++; - else - ctx.bounces[sc-7] = 150; - } - } -} - -static inline void decay_bounces(int sc) -{ - if (sc-7U < 32 && ctx.bounces[sc-7]) - ctx.bounces[sc-7]--; -} - -static inline int is_bouncing(int sc) -{ - return (sc-7U < 32 && ctx.bounces[sc-7] >= 100); -} - -#endif diff --git a/porting/linux/user/src/malloc/realloc.c b/porting/linux/user/src/malloc/realloc.c deleted file mode 100644 index 74b32be84..000000000 --- a/porting/linux/user/src/malloc/realloc.c +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HOOK_ENABLE -#include - -#ifdef USE_JEMALLOC -extern void* je_realloc(void* p, size_t newsize); -#endif - -void *realloc(void *p, size_t n) -{ -#ifdef USE_JEMALLOC - return je_realloc(p, n); -#endif - return __libc_realloc(p, n); -} -#endif diff --git a/porting/linux/user/src/mq/mq_notify.c b/porting/linux/user/src/mq/mq_notify.c deleted file mode 100644 index aca2f9cef..000000000 --- a/porting/linux/user/src/mq/mq_notify.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#include "pthread_impl.h" - -struct args { - pthread_barrier_t barrier; - int sock; - const struct sigevent *sev; -}; - -static void *start(void *p) -{ - struct args *args = p; - char buf[32]; - ssize_t n; - int s = args->sock; - void (*func)(union sigval) = args->sev->sigev_notify_function; - union sigval val = args->sev->sigev_value; - - pthread_barrier_wait(&args->barrier); - n = recv(s, buf, sizeof(buf), MSG_NOSIGNAL|MSG_WAITALL); - close(s); - if (n==sizeof buf && buf[sizeof buf - 1] == 1) - func(val); - return 0; -} - -int mq_notify(mqd_t mqd, const struct sigevent *sev) -{ - struct args args = { .sev = sev }; - pthread_attr_t attr; - pthread_t td; - int s; - struct sigevent sev2; - static const char zeros[32]; - - if (!sev || sev->sigev_notify != SIGEV_THREAD) - return syscall(SYS_mq_notify, mqd, sev); - - s = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, 0); - if (s < 0) return -1; - args.sock = s; - - if (sev->sigev_notify_attributes) attr = *sev->sigev_notify_attributes; - else pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_barrier_init(&args.barrier, 0, 2); - - if (pthread_create(&td, &attr, start, &args)) { - __syscall(SYS_close, s); - errno = EAGAIN; - return -1; - } - - pthread_barrier_wait(&args.barrier); - pthread_barrier_destroy(&args.barrier); - - sev2.sigev_notify = SIGEV_THREAD; - sev2.sigev_signo = s; - sev2.sigev_value.sival_ptr = (void *)&zeros; - - if (syscall(SYS_mq_notify, mqd, &sev2) < 0) { -#ifdef FEATURE_PTHREAD_CANCEL - pthread_cancel(td); -#else - __syscall(SYS_tkill, td->tid, SIGCANCEL); -#endif - __syscall(SYS_close, s); - return -1; - } - - return 0; -} diff --git a/porting/linux/user/src/network/getaddrinfo.c b/porting/linux/user/src/network/getaddrinfo.c deleted file mode 100644 index d18a9ca21..000000000 --- a/porting/linux/user/src/network/getaddrinfo.c +++ /dev/null @@ -1,240 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lookup.h" - -#define DNS_QUERY_SUCCESS 0 -#define DNS_QUERY_COMMOM_FAIL (-1) -#define GETADDRINFO_PRINT_DEBUG(...) - -int reportdnsresult(int netid, char* name, int usedtime, int queryret, struct addrinfo *res, struct queryparam *param) -{ -#if OHOS_DNS_PROXY_BY_NETSYS - if (dns_post_result_to_netsys_cache(netid, name, usedtime, queryret, res, param) == 0) { - GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext reportdnsresult fail\n"); - } -#endif - return 0; -} - -static custom_dns_resolver g_customdnsresolvehook; -static pthread_key_t g_recursiveKey; -static int* g_recursive; - -int setdnsresolvehook(custom_dns_resolver hookfunc) -{ - int ret = -1; - if (g_customdnsresolvehook) { - return ret; - } - if (hookfunc) { - g_customdnsresolvehook = hookfunc; - pthread_key_create(&g_recursiveKey, NULL); - ret = 0; - } - return ret; -} - -int removednsresolvehook() -{ - g_customdnsresolvehook = NULL; - if (g_recursive) { - free(g_recursive); - g_recursive = NULL; - } - if (g_recursiveKey) { - pthread_key_delete(g_recursiveKey); - g_recursiveKey = NULL; - } - return 0; -} - -int getaddrinfo(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res) -{ - struct queryparam param = {0, 0, 0, 0, NULL}; - return getaddrinfo_ext(host, serv, hint, res, ¶m); -} - -int getaddrinfo_ext(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, - struct addrinfo **restrict res, struct queryparam *restrict param) -{ - int netid = 0; - int type = 0; - int usedtime = 0; - time_t t_start, t_end; - - if (!host && !serv) return EAI_NONAME; - if (!param) { - netid = 0; - type = 0; - } else { - netid = param->qp_netid; - type = param->qp_type; - } - - if (g_customdnsresolvehook) { - g_recursive = pthread_getspecific(g_recursiveKey); - if (g_recursive == NULL) { - int *newRecursive = malloc(sizeof(int)); - *newRecursive = 0; - pthread_setspecific(g_recursiveKey, newRecursive); - g_recursive = newRecursive; - } - if (*g_recursive == 0) { - ++(*g_recursive); - int ret = g_customdnsresolvehook(host, serv, hint, res); - --(*g_recursive); - return ret; - } - } - -#if OHOS_DNS_PROXY_BY_NETSYS - GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext netid:%{public}d type:%{public}d \n", netid, type); - if (type == QEURY_TYPE_NORMAL) { - if (dns_get_addr_info_from_netsys_cache2(netid, host, serv, hint, res) == 0) { - GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext get from netsys cache OK\n"); - reportdnsresult(netid, host, 0, DNS_QUERY_SUCCESS, *res, ¶m); - return 0; - } - } -#endif - - struct service ports[MAXSERVS]; - struct address addrs[MAXADDRS]; - char canon[256], *outcanon; - int nservs, naddrs, nais, canon_len, i, j, k; - int family = AF_UNSPEC, flags = 0, proto = 0, socktype = 0; - struct aibuf *out; - - if (hint) { - family = hint->ai_family; - flags = hint->ai_flags; - proto = hint->ai_protocol; - socktype = hint->ai_socktype; - - const int mask = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | - AI_V4MAPPED | AI_ALL | AI_ADDRCONFIG | AI_NUMERICSERV; - if ((flags & mask) != flags) - return EAI_BADFLAGS; - - switch (family) { - case AF_INET: - case AF_INET6: - case AF_UNSPEC: - break; - default: - return EAI_FAMILY; - } - } - - if (flags & AI_ADDRCONFIG) { - /* Define the "an address is configured" condition for address - * families via ability to create a socket for the family plus - * routability of the loopback address for the family. */ - static const struct sockaddr_in lo4 = { - .sin_family = AF_INET, .sin_port = 65535, - .sin_addr.s_addr = __BYTE_ORDER == __BIG_ENDIAN - ? 0x7f000001 : 0x0100007f - }; - static const struct sockaddr_in6 lo6 = { - .sin6_family = AF_INET6, .sin6_port = 65535, - .sin6_addr = IN6ADDR_LOOPBACK_INIT - }; - int tf[2] = { AF_INET, AF_INET6 }; - const void *ta[2] = { &lo4, &lo6 }; - socklen_t tl[2] = { sizeof lo4, sizeof lo6 }; - for (i=0; i<2; i++) { - if (family==tf[1-i]) continue; - int s = socket(tf[i], SOCK_CLOEXEC|SOCK_DGRAM, - IPPROTO_UDP); - if (s>=0) { - int cs; - pthread_setcancelstate( - PTHREAD_CANCEL_DISABLE, &cs); - int r = connect(s, ta[i], tl[i]); - pthread_setcancelstate(cs, 0); - close(s); - if (!r) continue; - } - switch (errno) { - case EADDRNOTAVAIL: - case EAFNOSUPPORT: - case EHOSTUNREACH: - case ENETDOWN: - case ENETUNREACH: - break; - default: - return EAI_SYSTEM; - } - if (family == tf[i]) return EAI_NONAME; - family = tf[1-i]; - } - } - - t_start = time(NULL); - nservs = __lookup_serv(ports, serv, proto, socktype, flags); - if (nservs < 0) return nservs; - - naddrs = lookup_name_ext(addrs, canon, host, family, flags, netid); - t_end = time(NULL); - if (naddrs < 0) { - reportdnsresult(netid, host, difftime(t_end, t_start), DNS_QUERY_COMMOM_FAIL, NULL, ¶m); - return naddrs; - } - - nais = nservs * naddrs; - canon_len = strlen(canon); - out = calloc(1, nais * sizeof(*out) + canon_len + 1); - if (!out) return EAI_MEMORY; - - if (canon_len) { - outcanon = (void *)&out[nais]; - memcpy(outcanon, canon, canon_len+1); - } else { - outcanon = 0; - } - - for (k=i=0; iai; - - reportdnsresult(netid, host, difftime(t_end, t_start), DNS_QUERY_SUCCESS, *res, ¶m); - int cnt = predefined_host_is_contain_host(host); -#if OHOS_DNS_PROXY_BY_NETSYS - if (type == QEURY_TYPE_NORMAL && cnt == 0) { - dns_set_addr_info_to_netsys_cache2(netid, host, serv, hint, *res); - } -#endif - return 0; -} diff --git a/porting/linux/user/src/network/getnameinfo.c b/porting/linux/user/src/network/getnameinfo.c deleted file mode 100644 index 75657659d..000000000 --- a/porting/linux/user/src/network/getnameinfo.c +++ /dev/null @@ -1,225 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lookup.h" -#include "stdio_impl.h" -#include "network_conf_function.h" - -#define PTR_MAX (64 + sizeof ".in-addr.arpa") -#define RR_PTR 12 -#define BREAK 0 -#define CONTINUE 1 -#define FIXED_HOSTS_MAX_LENGTH 2 -#define FIXED_HOSTS_STR_MAX_LENGTH 23 - -extern char fixed_hosts[FIXED_HOSTS_MAX_LENGTH][FIXED_HOSTS_STR_MAX_LENGTH]; - -static char *itoa(char *p, unsigned x) { - p += 3*sizeof(int); - *--p = 0; - do { - *--p = '0' + x % 10; - x /= 10; - } while (x); - return p; -} - -static void mkptr4(char *s, const unsigned char *ip) -{ - sprintf(s, "%d.%d.%d.%d.in-addr.arpa", - ip[3], ip[2], ip[1], ip[0]); -} - -static void mkptr6(char *s, const unsigned char *ip) -{ - static const char xdigits[] = "0123456789abcdef"; - int i; - for (i=15; i>=0; i--) { - *s++ = xdigits[ip[i]&15]; *s++ = '.'; - *s++ = xdigits[ip[i]>>4]; *s++ = '.'; - } - strcpy(s, "ip6.arpa"); -} - -static inline int get_hosts_str(char *line, int length, FILE *f, int *i) -{ - if (f) { - return fgets(line, sizeof line, f); - } - if (*i < FIXED_HOSTS_MAX_LENGTH) { - memcpy(line, fixed_hosts[*i], strlen(fixed_hosts[*i])); - (*i)++; - return 1; - } - return NULL; -} - -static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family) -{ - char line[512], *p, *z; - unsigned char _buf[1032], atmp[16]; - struct address iplit; - FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); - if (family == AF_INET) { - memcpy(atmp+12, a, 4); - memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); - a = atmp; - } - int i = 0; - while (i < FIXED_HOSTS_MAX_LENGTH && get_hosts_str(line, sizeof line, f, &i)) { - if ((p=strchr(line, '#'))) *p++='\n', *p=0; - - for (p=line; *p && !isspace(*p); p++); - *p++ = 0; - if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0) - continue; - - if (iplit.family == AF_INET) { - memcpy(iplit.addr+12, iplit.addr, 4); - memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); - iplit.scopeid = 0; - } - - if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid) - continue; - - for (; *p && isspace(*p); p++); - for (z=p; *z && !isspace(*z); z++); - *z = 0; - if (z-p < 256) { - memcpy(buf, p, z-p+1); - break; - } - } - if (f) { - __fclose_ca(f); - } -} - -static void reverse_services(char *buf, int port, int dgram) -{ - unsigned long svport; - char line[128], *p, *z; - unsigned char _buf[1032]; - FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf); - int indexPtr = 0; - while (get_services_str(line, f, &indexPtr)) { - if ((p=strchr(line, '#'))) *p++='\n', *p=0; - - for (p=line; *p && !isspace(*p); p++); - if (!*p) continue; - *p++ = 0; - svport = strtoul(p, &z, 10); - - if (svport != port || z==p) continue; - if (dgram && strncmp(z, "/udp", 4)) continue; - if (!dgram && strncmp(z, "/tcp", 4)) continue; - if (p-line > 32) continue; - - memcpy(buf, line, p-line); - break; - } - if (f) { - __fclose_ca(f); - } -} - -static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet) -{ - if (rr != RR_PTR) return 0; - if (__dn_expand(packet, (const unsigned char *)packet + 512, - data, c, 256) <= 0) - *(char *)c = 0; - return 0; - -} - -int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, - char *restrict node, socklen_t nodelen, - char *restrict serv, socklen_t servlen, - int flags) -{ - char ptr[PTR_MAX]; - char buf[256], num[3*sizeof(int)+1]; - int af = sa->sa_family; - unsigned char *a; - unsigned scopeid; - - switch (af) { - case AF_INET: - a = (void *)&((struct sockaddr_in *)sa)->sin_addr; - if (sl < sizeof(struct sockaddr_in)) return EAI_FAMILY; - mkptr4(ptr, a); - scopeid = 0; - break; - case AF_INET6: - a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr; - if (sl < sizeof(struct sockaddr_in6)) return EAI_FAMILY; - if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12)) - mkptr6(ptr, a); - else - mkptr4(ptr, a+12); - scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id; - break; - default: - return EAI_FAMILY; - } - - if (node && nodelen) { - buf[0] = 0; - if (!(flags & NI_NUMERICHOST)) { - reverse_hosts(buf, a, scopeid, af); - } - if (!*buf && !(flags & NI_NUMERICHOST)) { - unsigned char query[18+PTR_MAX], reply[512]; - int qlen = __res_mkquery(0, ptr, 1, RR_PTR, - 0, 0, 0, query, sizeof query); - query[3] = 0; /* don't need AD flag */ - int rlen = __res_send(query, qlen, reply, sizeof reply); - buf[0] = 0; - if (rlen > 0) - __dns_parse(reply, rlen, dns_parse_callback, buf); - } - if (!*buf) { - if (flags & NI_NAMEREQD) return EAI_NONAME; - inet_ntop(af, a, buf, sizeof buf); - if (scopeid) { - char *p = 0, tmp[IF_NAMESIZE+1]; - if (!(flags & NI_NUMERICSCOPE) && - (IN6_IS_ADDR_LINKLOCAL(a) || - IN6_IS_ADDR_MC_LINKLOCAL(a))) - p = if_indextoname(scopeid, tmp+1); - if (!p) - p = itoa(num, scopeid); - *--p = '%'; - strcat(buf, p); - } - } - if (strlen(buf) >= nodelen) return EAI_OVERFLOW; - strcpy(node, buf); - } - - if (serv && servlen) { - char *p = buf; - int port = ntohs(((struct sockaddr_in *)sa)->sin_port); - buf[0] = 0; - if (!(flags & NI_NUMERICSERV)) - reverse_services(buf, port, flags & NI_DGRAM); - if (!*p) - p = itoa(num, port); - if (strlen(p) >= servlen) - return EAI_OVERFLOW; - strcpy(serv, p); - } - - return 0; -} - diff --git a/porting/linux/user/src/network/inet_legacy.c b/porting/linux/user/src/network/inet_legacy.c deleted file mode 100644 index 6d91b6e17..000000000 --- a/porting/linux/user/src/network/inet_legacy.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include - -in_addr_t inet_network(const char *p) -{ - return ntohl(inet_addr(p)); -} - -struct in_addr inet_makeaddr(in_addr_t n, in_addr_t h) -{ - if (n < 256) h |= n<<24; - else if (n < 65536) h |= n<<16; - else h |= n<<8; - return (struct in_addr){ htonl(h) }; -} - -in_addr_t inet_lnaof(struct in_addr in) -{ - uint32_t h = ntohl(in.s_addr); - if (h>>24 < 128) return h & 0xffffff; - if (h>>24 < 192) return h & 0xffff; - return h & 0xff; -} - -in_addr_t inet_netof(struct in_addr in) -{ - uint32_t h = ntohl(in.s_addr); - if (h>>24 < 128) return h >> 24; - if (h>>24 < 192) return h >> 16; - return h >> 8; -} diff --git a/porting/linux/user/src/network/lookup.h b/porting/linux/user/src/network/lookup.h deleted file mode 100644 index bb4d67731..000000000 --- a/porting/linux/user/src/network/lookup.h +++ /dev/null @@ -1,170 +0,0 @@ -#ifndef LOOKUP_H -#define LOOKUP_H - -#include -#include -#include -#include -#include - -#if OHOS_DNS_PROXY_BY_NETSYS - -#include - -#if DNS_CONFIG_DEBUG -#ifndef DNS_CONFIG_PRINT -#define DNS_CONFIG_PRINT(fmt, ...) printf("DNS " fmt "\n", ##__VA_ARGS__) -#endif -#else -#define DNS_CONFIG_PRINT(fmt, ...) -#endif -#endif - -struct aibuf { - struct addrinfo ai; - union sa { - struct sockaddr_in sin; - struct sockaddr_in6 sin6; - } sa; - volatile int lock[1]; - short slot, ref; -}; - -struct address { - int family; - unsigned scopeid; - uint8_t addr[16]; - int sortkey; -}; - -struct service { - uint16_t port; - unsigned char proto, socktype; -}; - -#define MAXNS 3 - -struct resolvconf { - struct address ns[MAXNS]; - unsigned nns, attempts, ndots; - unsigned timeout; -}; - -/* The limit of 48 results is a non-sharp bound on the number of addresses - * that can fit in one 512-byte DNS packet full of v4 results and a second - * packet full of v6 results. Due to headers, the actual limit is lower. */ -#define MAXADDRS 48 -#define MAXSERVS 2 - -hidden int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int proto, int socktype, int flags); -hidden int lookup_name_ext(struct address buf[static MAXADDRS], char canon[static 256], const char *name, - int family, int flags, int netid); -hidden int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags); -hidden int __lookup_ipliteral(struct address buf[static 1], const char *name, int family); - -hidden int __get_resolv_conf(struct resolvconf *, char *, size_t); -hidden int get_resolv_conf_ext(struct resolvconf *, char *, size_t, int netid); -hidden int __res_msend_rc(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int, const struct resolvconf *); -hidden int res_msend_rc_ext(int, int, const unsigned char *const *, const int *, unsigned char *const *, - int *, int, const struct resolvconf *); - -hidden int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *); -hidden int predefined_host_name_from_hosts(struct address buf[static MAXADDRS], - char canon[static 256], const char *name, int family); -hidden int predefined_host_is_contain_host(const char *host); -hidden int res_bind_socket(int, int); - -#if OHOS_DNS_PROXY_BY_NETSYS -#define DNS_SO_PATH "libnetsys_client.z.so" -#define MAX_SERVER_NUM 4 -#define MAX_SERVER_LENGTH 50 -#define OHOS_GET_CONFIG_FUNC_NAME "NetSysGetResolvConf" -#define OHOS_GET_CACHE_FUNC_NAME "NetSysGetResolvCache" -#define OHOS_SET_CACHE_FUNC_NAME "NetSysSetResolvCache" -#define OHOS_JUDGE_IPV6_FUNC_NAME "NetSysIsIpv6Enable" -#define OHOS_POST_DNS_RESULT_FUNC_NAME "NetSysPostDnsResult" -#define OHOS_GET_DEFAULT_NET_FUNC_NAME "NetSysGetDefaultNetwork" -#define MAX_RESULTS 32 -#define MAX_CANON_NAME 256 -#define MACRO_MIN(a, b) ((a) < (b) ? (a) : (b)) - -struct resolv_config { - int32_t error; - int32_t timeout_ms; - uint32_t retry_count; - char nameservers[MAX_SERVER_NUM][MAX_SERVER_LENGTH + 1]; -}; - -typedef union { - struct sockaddr sa; - struct sockaddr_in6 sin6; - struct sockaddr_in sin; -} aligned_sockAddr; - -struct addr_info_wrapper { - uint32_t ai_flags; - uint32_t ai_family; - uint32_t ai_sockType; - uint32_t ai_protocol; - uint32_t ai_addrLen; - aligned_sockAddr ai_addr; - char ai_canonName[MAX_CANON_NAME + 1]; -}; - -struct param_wrapper { - char *host; - char *serv; - struct addrinfo *hint; -}; - -typedef int32_t (*GetConfig)(uint16_t netId, struct resolv_config *config); - -typedef int32_t (*GetCache)(uint16_t netId, struct param_wrapper param, - struct addr_info_wrapper addr_info[static MAX_RESULTS], - uint32_t *num); - -typedef int32_t (*SetCache)(uint16_t netId, struct param_wrapper param, struct addrinfo *res); - -typedef int (*JudgeIpv6)(uint16_t netId); - -typedef int (*PostDnsResult)(int netid, char* name, int usedtime, int queryfail, - struct addrinfo *res, struct queryparam *param); - -typedef int (*GetDefaultNet)(uint16_t netId, int32_t *currentnetid); - -/* If the memory holder points to stores NULL value, try to load symbol from the - * dns lib into holder; otherwise, it does nothing. */ -hidden void resolve_dns_sym(void **holder, const char *symbol); - -void -dns_set_addr_info_to_netsys_cache(const char *__restrict host, const char *__restrict serv, - const struct addrinfo *__restrict - hint, struct addrinfo *res); - -void dns_set_addr_info_to_netsys_cache2(const int netid, const char *__restrict host, const char *__restrict serv, - const struct addrinfo *__restrict hint, struct addrinfo *res); - -int dns_get_addr_info_from_netsys_cache(const char *__restrict host, const char *__restrict serv, - const struct addrinfo *__restrict hint, struct addrinfo **__restrict res); - -int dns_get_addr_info_from_netsys_cache2(const int netid, const char *__restrict host, const char *__restrict serv, - const struct addrinfo *__restrict hint, struct addrinfo **__restrict res); - -int dns_post_result_to_netsys_cache(int netid, char* name, int usedtime, int querypass, - struct addrinfo *res, struct queryparam *param); - -int dns_get_default_network(int *currentnetid); -#endif - -#if OHOS_FWMARK_CLIENT_BY_NETSYS -#define FWMARKCLIENT_SO_PATH "libfwmark_client.z.so" -#define OHOS_BIND_SOCKET_FUNC_NAME "BindSocket" - -typedef int32_t (*BindSocket)(int32_t fd, uint32_t netId); - -#define OHOS_NETSYS_BIND_SOCKET_FUNC_NAME "NetSysBindSocket" - -typedef int32_t (*BindSocket_Ext)(int32_t fd, uint32_t netId); - -#endif -#endif diff --git a/porting/linux/user/src/network/lookup_name.c b/porting/linux/user/src/network/lookup_name.c deleted file mode 100644 index a445a49b5..000000000 --- a/porting/linux/user/src/network/lookup_name.c +++ /dev/null @@ -1,913 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lookup.h" -#include "stdio_impl.h" -#include "syscall.h" -#include -#define BREAK 0 -#define CONTINUE 1 -#if OHOS_PERMISSION_INTERNET -uint8_t is_allow_internet(void); -#endif -#define FIXED_HOSTS_MAX_LENGTH 2 -#define FIXED_HOSTS_STR_MAX_LENGTH 23 - -char fixed_hosts[][FIXED_HOSTS_STR_MAX_LENGTH] = { - "127.0.0.1 localhost\r\n\0", - "::1 ip6-localhost\r\n\0" -}; - -static int is_valid_hostname(const char *host) -{ - const unsigned char *s; - if (strnlen(host, 255)-1 >= 254 || mbstowcs(0, host, 0) == -1) return 0; - for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++); - return !*s; -} - -static int name_from_null(struct address buf[static 2], const char *name, int family, int flags) -{ - int cnt = 0; - if (name) return 0; - if (flags & AI_PASSIVE) { - if (family != AF_INET6) - buf[cnt++] = (struct address){ .family = AF_INET }; - if (family != AF_INET) - buf[cnt++] = (struct address){ .family = AF_INET6 }; - } else { - if (family != AF_INET6) - buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } }; - if (family != AF_INET) - buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } }; - } - return cnt; -} - -static int name_from_numeric(struct address buf[static 1], const char *name, int family) -{ - return __lookup_ipliteral(buf, name, family); -} - -static inline int get_hosts_str(char *line, int length, FILE *f, int *i) -{ - if (f) { - return fgets(line, sizeof line, f); - } - if (*i < FIXED_HOSTS_MAX_LENGTH) { - memcpy(line, fixed_hosts[*i], strlen(fixed_hosts[*i])); - (*i)++; - return 1; - } - return NULL; -} - -static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family) -{ - char line[512]; - size_t l = strlen(name); - int cnt = 0, badfam = 0, have_canon = 0; - unsigned char _buf[1032]; - FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); - int i = 0; - while (i < FIXED_HOSTS_MAX_LENGTH && get_hosts_str(line, sizeof line, f, &i) && cnt < MAXADDRS) { - char *p, *z; - - if ((p=strchr(line, '#'))) *p++='\n', *p=0; - for(p=line+1; (p=strstr(p, name)) && - (!isspace(p[-1]) || !isspace(p[l])); p++); - if (!p) continue; - - /* Isolate IP address to parse */ - for (p=line; *p && !isspace(*p); p++); - *p++ = 0; - switch (name_from_numeric(buf+cnt, line, family)) { - case 1: - cnt++; - break; - case 0: - continue; - default: - badfam = EAI_NONAME; - break; - } - - if (have_canon) continue; - - /* Extract first name as canonical name */ - for (; *p && isspace(*p); p++); - for (z=p; *z && !isspace(*z); z++); - *z = 0; - if (is_valid_hostname(p)) { - have_canon = 1; - memcpy(canon, p, z-p+1); - } - } - if (f) { - __fclose_ca(f); - } - return cnt ? cnt : badfam; -} - -struct dpc_ctx { - struct address *addrs; - char *canon; - int cnt; -}; - -#define RR_A 1 -#define RR_CNAME 5 -#define RR_AAAA 28 - -static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet) -{ - char tmp[256]; - struct dpc_ctx *ctx = c; - if (ctx->cnt >= MAXADDRS) return -1; - switch (rr) { - case RR_A: - if (len != 4) return -1; - ctx->addrs[ctx->cnt].family = AF_INET; - ctx->addrs[ctx->cnt].scopeid = 0; - memcpy(ctx->addrs[ctx->cnt++].addr, data, 4); - break; - case RR_AAAA: - if (len != 16) return -1; - ctx->addrs[ctx->cnt].family = AF_INET6; - ctx->addrs[ctx->cnt].scopeid = 0; - memcpy(ctx->addrs[ctx->cnt++].addr, data, 16); - break; - case RR_CNAME: - if (__dn_expand(packet, (const unsigned char *)packet + 512, - data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp)) - strcpy(ctx->canon, tmp); - break; - } - return 0; -} - -#if OHOS_DNS_PROXY_BY_NETSYS -static JudgeIpv6 load_ipv6_judger(void) -{ - static JudgeIpv6 ipv6_judger = NULL; - resolve_dns_sym((void **) &ipv6_judger, OHOS_JUDGE_IPV6_FUNC_NAME); - return ipv6_judger; -} -#endif - -static int IsIpv6Enable(int netid) -{ - int ret = 0; -#if OHOS_DNS_PROXY_BY_NETSYS - JudgeIpv6 func = load_ipv6_judger(); - if (!func) { - return -1; - } - - ret = func(netid); - if (ret < 0) { - return -1; - } -#endif - return ret; -} - -static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, const struct resolvconf *conf, int netid) -{ - unsigned char qbuf[2][280], abuf[2][512]; - const unsigned char *qp[2] = { qbuf[0], qbuf[1] }; - unsigned char *ap[2] = { abuf[0], abuf[1] }; - int qlens[2], alens[2]; - int i, nq = 0; - int queryNum = 2; - struct dpc_ctx ctx = { .addrs = buf, .canon = canon }; - static const struct { int af; int rr; } afrr_ipv6_enable[2] = { - { .af = AF_INET, .rr = RR_AAAA }, - { .af = AF_INET6, .rr = RR_A }, - }; - static const struct { int af; int rr; } afrr_ipv4_only[1] = { - { .af = AF_INET6, .rr = RR_A }, - }; - struct {int af; int rr;} *afrr = afrr_ipv6_enable; - - if (!IsIpv6Enable(netid) || (family == AF_INET)) { - if (family == AF_INET6) { - return EAI_SYSTEM; - } - queryNum = 1; - afrr = afrr_ipv4_only; - } else { - queryNum = 2; - afrr = afrr_ipv6_enable; - } - for (i = 0; i < queryNum; i++) { - if (family != afrr[i].af) { - qlens[nq] = __res_mkquery(0, name, 1, afrr[i].rr, - 0, 0, 0, qbuf[nq], sizeof *qbuf); - if (qlens[nq] == -1) - return EAI_NONAME; - qbuf[nq][3] = 0; /* don't need AD flag */ - nq++; - } - } - - if (res_msend_rc_ext(netid, nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0) - return EAI_SYSTEM; - - for (i=0; i=ndots or name ends in - * a dot, which is an explicit request for global scope. */ - for (dots=l=0; name[l]; l++) if (name[l]=='.') dots++; - if (dots >= conf.ndots || name[l-1]=='.') *search = 0; - - /* Strip final dot for canon, fail if multiple trailing dots. */ - if (name[l-1]=='.') l--; - if (!l || name[l-1]=='.') return EAI_NONAME; - - /* This can never happen; the caller already checked length. */ - if (l >= 256) return EAI_NONAME; - - /* Name with search domain appended is setup in canon[]. This both - * provides the desired default canonical name (if the requested - * name is not a CNAME record) and serves as a buffer for passing - * the full requested name to name_from_dns. */ - memcpy(canon, name, l); - canon[l] = '.'; - - for (p=search; *p; p=z) { - for (; isspace(*p); p++); - for (z=p; *z && !isspace(*z); z++); - if (z==p) break; - if (z-p < 256 - l - 1) { - memcpy(canon+l+1, p, z-p); - canon[z-p+1+l] = 0; - int cnt = name_from_dns(buf, canon, canon, family, &conf, netid); - if (cnt) return cnt; - } - } - - canon[l] = 0; - return name_from_dns(buf, canon, name, family, &conf, netid); -} - -static const struct policy { - unsigned char addr[16]; - unsigned char len, mask; - unsigned char prec, label; -} defpolicy[] = { - { "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1", 15, 0xff, 50, 0 }, - { "\0\0\0\0\0\0\0\0\0\0\xff\xff", 11, 0xff, 35, 4 }, - { "\x20\2", 1, 0xff, 30, 2 }, - { "\x20\1", 3, 0xff, 5, 5 }, - { "\xfc", 0, 0xfe, 3, 13 }, -#if 0 - /* These are deprecated and/or returned to the address - * pool, so despite the RFC, treating them as special - * is probably wrong. */ - { "", 11, 0xff, 1, 3 }, - { "\xfe\xc0", 1, 0xc0, 1, 11 }, - { "\x3f\xfe", 1, 0xff, 1, 12 }, -#endif - /* Last rule must match all addresses to stop loop. */ - { "", 0, 0, 40, 1 }, -}; - -static const struct policy *policyof(const struct in6_addr *a) -{ - int i; - for (i=0; ; i++) { - if (memcmp(a->s6_addr, defpolicy[i].addr, defpolicy[i].len)) - continue; - if ((a->s6_addr[defpolicy[i].len] & defpolicy[i].mask) - != defpolicy[i].addr[defpolicy[i].len]) - continue; - return defpolicy+i; - } -} - -static int labelof(const struct in6_addr *a) -{ - return policyof(a)->label; -} - -static int scopeof(const struct in6_addr *a) -{ - if (IN6_IS_ADDR_MULTICAST(a)) return a->s6_addr[1] & 15; - if (IN6_IS_ADDR_LINKLOCAL(a)) return 2; - if (IN6_IS_ADDR_LOOPBACK(a)) return 2; - if (IN6_IS_ADDR_SITELOCAL(a)) return 5; - return 14; -} - -static int prefixmatch(const struct in6_addr *s, const struct in6_addr *d) -{ - /* FIXME: The common prefix length should be limited to no greater - * than the nominal length of the prefix portion of the source - * address. However the definition of the source prefix length is - * not clear and thus this limiting is not yet implemented. */ - unsigned i; - for (i=0; i<128 && !((s->s6_addr[i/8]^d->s6_addr[i/8])&(128>>(i%8))); i++); - return i; -} - -#define DAS_USABLE 0x40000000 -#define DAS_MATCHINGSCOPE 0x20000000 -#define DAS_MATCHINGLABEL 0x10000000 -#define DAS_PREC_SHIFT 20 -#define DAS_SCOPE_SHIFT 16 -#define DAS_PREFIX_SHIFT 8 -#define DAS_ORDER_SHIFT 0 - -static int addrcmp(const void *_a, const void *_b) -{ - const struct address *a = _a, *b = _b; - return b->sortkey - a->sortkey; -} - -int lookup_name_ext(struct address buf[static MAXADDRS], char canon[static 256], const char *name, - int family, int flags, int netid) -{ - int cnt = 0, i, j; - -#if OHOS_DNS_PROXY_BY_NETSYS - DNS_CONFIG_PRINT("lookup_name_ext \n"); -#endif - - *canon = 0; - if (name) { - /* reject empty name and check len so it fits into temp bufs */ - size_t l = strnlen(name, 255); - if (l-1 >= 254) - return EAI_NONAME; - memcpy(canon, name, l+1); - } - - /* Procedurally, a request for v6 addresses with the v4-mapped - * flag set is like a request for unspecified family, followed - * by filtering of the results. */ - if (flags & AI_V4MAPPED) { - if (family == AF_INET6) family = AF_UNSPEC; - else flags -= AI_V4MAPPED; - } - - /* Try each backend until there's at least one result. */ - cnt = name_from_null(buf, name, family, flags); - if (!cnt) cnt = name_from_numeric(buf, name, family); - if (!cnt && !(flags & AI_NUMERICHOST)) { - cnt = predefined_host_name_from_hosts(buf, canon, name, family); - if (!cnt) cnt = name_from_hosts(buf, canon, name, family); - if (!cnt) cnt = name_from_dns_search(buf, canon, name, family, netid); - } - if (cnt<=0) return cnt ? cnt : EAI_NONAME; - - /* Filter/transform results for v4-mapped lookup, if requested. */ - if (flags & AI_V4MAPPED) { - if (!(flags & AI_ALL)) { - /* If any v6 results exist, remove v4 results. */ - for (i=0; ilabel; - int dprec = dpolicy->prec; - int prefixlen = 0; - int fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP); - if (fd >= 0) { - if (!connect(fd, da, dalen)) { - key |= DAS_USABLE; - if (!getsockname(fd, sa, &salen)) { - if (family == AF_INET) memcpy( - sa6.sin6_addr.s6_addr+12, - &sa4.sin_addr, 4); - if (dscope == scopeof(&sa6.sin6_addr)) - key |= DAS_MATCHINGSCOPE; - if (dlabel == labelof(&sa6.sin6_addr)) - key |= DAS_MATCHINGLABEL; - prefixlen = prefixmatch(&sa6.sin6_addr, - &da6.sin6_addr); - } - } - close(fd); - } - key |= dprec << DAS_PREC_SHIFT; - key |= (15-dscope) << DAS_SCOPE_SHIFT; - key |= prefixlen << DAS_PREFIX_SHIFT; - key |= (MAXADDRS-i) << DAS_ORDER_SHIFT; - buf[i].sortkey = key; - } - qsort(buf, cnt, sizeof *buf, addrcmp); - - pthread_setcancelstate(cs, 0); - - return cnt; -} - -int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags) -{ - return lookup_name_ext(buf, canon, name, family, flags, 0); -} - -typedef struct _linknode { - struct _linknode *_next; - void *_data; -}linknode; - -typedef struct _linkList { - linknode *_phead; - int _count; -}linkList; - -static linkList *create_linklist(void); -static int destory_linklist(linkList *plist); -static int linklist_size(linkList *plist); -static linknode *get_linknode(linkList *plist, int index); -static int linklist_append_last(linkList *plist, void *pdata); -static int linklist_delete(linkList *plist, int index); -static int linklist_delete_first(linkList *plist); -static int linklist_delete_last(linkList *plist); - -typedef struct { - char* host; - char* ip; -}host_ip_pair; - -static const char GROUP_SEPARATOR[] = "|"; -static const char SEPARATOR[] = ","; - -static linkList *host_ips_list_ = NULL; - -static char *strsep(char **s, const char *ct) -{ - char *sbegin = *s; - char *end; - if (sbegin == NULL) { - return NULL; - } - end = strpbrk(sbegin, ct); - if (end) { - *end++ = '\0'; - } - *s = end; - return sbegin; -} - -int predefined_host_clear_all_hosts(void) -{ - if (host_ips_list_ != NULL) { - linknode *pnode = host_ips_list_->_phead; - while (pnode != NULL) { - free(pnode->_data); - pnode->_data = NULL; - pnode = pnode->_next; - } - - destory_linklist(host_ips_list_); - free(host_ips_list_); - host_ips_list_ = NULL; - return 0; - } - return -1; -} - -int predefined_host_remove_host(const char *host) -{ - int remove_cnt = 0; - if (host_ips_list_ != NULL) { - linknode *pnode = NULL; - host_ip_pair *pinfo = NULL; - int cnt = linklist_size(host_ips_list_); - - for (int i = cnt - 1; i >= 0; i--) { - pnode = get_linknode(host_ips_list_, i); - if (pnode != NULL) { - pinfo = (host_ip_pair*)pnode->_data; - if (strcmp(pinfo->host, host) == 0) { - free(pinfo); - linklist_delete(host_ips_list_, i); - remove_cnt++; - } - } - } - } - - return remove_cnt == 0 ? -1 : 0; -} - -static int _predefined_host_is_contain_host_ip(const char* host, const char* ip, int is_check_ip) -{ - if (host_ips_list_ != NULL) { - linknode *pnode = host_ips_list_->_phead; - - while (pnode != NULL) { - host_ip_pair *pinfo = (host_ip_pair*)pnode->_data; - if (strcmp(pinfo->host, host) == 0) { - if (is_check_ip) { - if (strcmp(pinfo->ip, ip) == 0) { - return 1; - } - } else { - return 1; - } - } - pnode = pnode->_next; - } - } - return 0; -} - -int predefined_host_is_contain_host(const char *host) -{ - return _predefined_host_is_contain_host_ip(host, NULL, 0); -} - -static int _predefined_host_add_record(char* host, char* ip) -{ - int head_len = sizeof(host_ip_pair); - int host_len = strlen(host); - int ip_len = strlen(ip); - int total = host_len + 1 + ip_len + 1 + head_len; - char *pdata = calloc(1, total); - host_ip_pair *pinfo = (host_ip_pair*)pdata; - - if (pdata == NULL) { - return EAI_NONAME; - } - - char* i_host = (char*)(pdata + head_len); - char* i_ip = (char*)(pdata + head_len + host_len + 1); - - memcpy(i_host, host, host_len + 1); - memcpy(i_ip, ip, ip_len + 1); - - pinfo->host = i_host; - pinfo->ip = i_ip; - - linklist_append_last(host_ips_list_, pdata); - return 0; -} - -static int _predefined_host_parse_host_ips(char* params) -{ - char* cmd = NULL; - int ret = 0; - int cnt = 0; - - while ((cmd = strsep(¶ms, GROUP_SEPARATOR)) != NULL) { - char* host = strsep(&cmd, SEPARATOR); - char* ip = NULL; - - while ((ip = strsep(&cmd, SEPARATOR)) != NULL) { - cnt++; - if (!_predefined_host_is_contain_host_ip(host, ip, 1)) { - ret = _predefined_host_add_record(host, ip); - if (ret != 0) { - return ret; - } - } - } - } - - return cnt > 0 ? 0 : -1; -} - -int predefined_host_set_hosts(const char* host_ips) -{ - if (host_ips == NULL) { - return EAI_NONAME; - } - - int len = strlen(host_ips); - if (len == 0) { - return EAI_NONAME; - } - - if (host_ips_list_ == NULL) { - host_ips_list_ = create_linklist(); - if (host_ips_list_ == NULL) { - return EAI_MEMORY; - } - } - - char *host_ips_str = calloc(1, len + 1); - if (host_ips_str == NULL) { - return EAI_MEMORY; - } - - memcpy(host_ips_str, host_ips, len + 1); - - int ret = _predefined_host_parse_host_ips(host_ips_str); - - free(host_ips_str); - - return ret; -} - -int predefined_host_set_host(const char* host, const char* ip) -{ - if (host == NULL || strlen(host) == 0 || ip == NULL || strlen(ip) == 0) { - return -1; - } - - if (host_ips_list_ == NULL) { - host_ips_list_ = create_linklist(); - if (host_ips_list_ == NULL) { - return EAI_NONAME; - } - } - - if (_predefined_host_is_contain_host_ip(host, ip, 1)) { - return 0; - } - - return _predefined_host_add_record(host, ip); -} - -int predefined_host_name_from_hosts( - struct address buf[static MAXADDRS], - char canon[static 256], const char *name, int family) -{ - int size = 256; - int cnt = 0; - if (host_ips_list_ != NULL) { - linknode *pnode = host_ips_list_->_phead; - - while (pnode != NULL && cnt < MAXADDRS) { - host_ip_pair *pinfo = (host_ip_pair*)pnode->_data; - if (strcmp(pinfo->host, name) == 0) { - if (__lookup_ipliteral(buf+cnt, pinfo->ip, family) == 1) { - cnt++; - } - } - pnode = pnode->_next; - } - } - - if (cnt > 0) { - memcpy(canon, name, size); - } - return cnt; -} - -static inline void free_linknodedata(linknode *pnode) -{ - if (NULL != pnode) { - free(pnode); - } -} - -static linknode *create_linknode(void *data) -{ - linknode *pnode = (linknode *)calloc(1, sizeof(linknode)); - if (NULL == pnode) { - return NULL; - } - pnode->_data = data; - pnode->_next = NULL; - - return pnode; -} - -static linkList *create_linklist(void) -{ - linkList *plist = (linkList *)calloc(1, sizeof(linkList)); - if (NULL == plist) { - return NULL; - } - plist->_phead = NULL; - plist->_count = 0; - - return plist; -} - -static int destory_linklist(linkList *plist) -{ - if (NULL == plist) { - return -1; - } - - linknode *pnode = plist->_phead; - linknode *ptmp = NULL; - while (pnode != NULL) { - ptmp = pnode; - pnode = pnode->_next; - free_linknodedata(ptmp); - } - - plist->_phead = NULL; - plist->_count = 0; - - return 0; -} - -static int linklist_size(linkList *plist) -{ - if (NULL == plist) { - return 0; - } - - return plist->_count; -} - -static linknode *get_linknode(linkList *plist, int index) -{ - if (index < 0 || index >= plist->_count) { - return NULL; - } - - int i = 0; - linknode *pnode = plist->_phead; - while ((i++) < index) { - pnode = pnode->_next; - } - - return pnode; -} - -static int linklist_append_last(linkList *plist, void *pdata) -{ - if (NULL == plist) { - return -1; - } - - linknode *pnode = create_linknode(pdata); - if (NULL == pnode) { - return -1; - } - - if (NULL == plist->_phead) { - plist->_phead = pnode; - plist->_count++; - } else { - linknode *plastnode = get_linknode(plist, plist->_count - 1); - plastnode->_next = pnode; - plist->_count++; - } - - return 0; -} - -static int linklist_delete(linkList *plist, int index) -{ - if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { - return -1; - } - - if (index == 0) { - return linklist_delete_first(plist); - } else if (index == (plist->_count - 1)) { - return linklist_delete_last(plist); - } else { - linknode *pindex = get_linknode(plist, index); - if (NULL == pindex) { - return -1; - } - linknode *preindex = get_linknode(plist, index - 1); - if (NULL == preindex) { - return -1; - } - - preindex->_next = pindex->_next; - - free_linknodedata(pindex); - plist->_count--; - } - - return 0; -} - -static int linklist_delete_first(linkList *plist) -{ - if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { - return -1; - } - - linknode *phead = plist->_phead; - plist->_phead = plist->_phead->_next; - - free_linknodedata(phead); - plist->_count--; - - return 0; -} - -static int linklist_delete_last(linkList *plist) -{ - if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { - return -1; - } - - linknode *plastsecondnode = get_linknode(plist, plist->_count - 2); - if (NULL != plastsecondnode) { - linknode *plastnode = plastsecondnode->_next; - plastsecondnode->_next = NULL; - - free_linknodedata(plastnode); - plist->_count--; - } else { - linknode *plastnode = get_linknode(plist, plist->_count - 1); - plist->_phead = NULL; - plist->_count = 0; - - free_linknodedata(plastnode); - } - - return 0; -} diff --git a/porting/linux/user/src/network/lookup_serv.c b/porting/linux/user/src/network/lookup_serv.c deleted file mode 100644 index 944d70764..000000000 --- a/porting/linux/user/src/network/lookup_serv.c +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "lookup.h" -#include "stdio_impl.h" -#include "network_conf_function.h" - -int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int proto, int socktype, int flags) -{ - char line[128]; - int cnt = 0; - char *p, *z = ""; - unsigned long port = 0; - - switch (socktype) { - case SOCK_STREAM: - switch (proto) { - case 0: - proto = IPPROTO_TCP; - case IPPROTO_TCP: - break; - default: - return EAI_SERVICE; - } - break; - case SOCK_DGRAM: - switch (proto) { - case 0: - proto = IPPROTO_UDP; - case IPPROTO_UDP: - break; - default: - return EAI_SERVICE; - } - case 0: - break; - default: - if (name) return EAI_SERVICE; - buf[0].port = 0; - buf[0].proto = proto; - buf[0].socktype = socktype; - return 1; - } - - if (name) { - if (!*name) return EAI_SERVICE; - port = strtoul(name, &z, 10); - } - if (!*z) { - if (port > 65535) return EAI_SERVICE; - if (proto != IPPROTO_UDP) { - buf[cnt].port = port; - buf[cnt].socktype = SOCK_STREAM; - buf[cnt++].proto = IPPROTO_TCP; - } - if (proto != IPPROTO_TCP) { - buf[cnt].port = port; - buf[cnt].socktype = SOCK_DGRAM; - buf[cnt++].proto = IPPROTO_UDP; - } - return cnt; - } - - if (flags & AI_NUMERICSERV) return EAI_NONAME; - - size_t l = strlen(name); - - unsigned char _buf[1032]; - FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf); - int indexPtr = 0; - while (get_services_str(line, f, &indexPtr)) { - if ((p=strchr(line, '#'))) *p++='\n', *p=0; - - /* Find service name */ - for(p=line; (p=strstr(p, name)); p++) { - if (p>line && !isspace(p[-1])) continue; - if (p[l] && !isspace(p[l])) continue; - break; - } - if (!p) continue; - - /* Skip past canonical name at beginning of line */ - for (p=line; *p && !isspace(*p); p++); - - port = strtoul(p, &z, 10); - if (port > 65535 || z==p) continue; - if (!strncmp(z, "/udp", 4)) { - if (proto == IPPROTO_TCP) continue; - buf[cnt].port = port; - buf[cnt].socktype = SOCK_DGRAM; - buf[cnt++].proto = IPPROTO_UDP; - } - if (!strncmp(z, "/tcp", 4)) { - if (proto == IPPROTO_UDP) continue; - buf[cnt].port = port; - buf[cnt].socktype = SOCK_STREAM; - buf[cnt++].proto = IPPROTO_TCP; - } - } - if (f) { - __fclose_ca(f); - } - return cnt > 0 ? cnt : EAI_SERVICE; -} - diff --git a/porting/linux/user/src/network/res_cache.c b/porting/linux/user/src/network/res_cache.c deleted file mode 100644 index 947ede4bc..000000000 --- a/porting/linux/user/src/network/res_cache.c +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include "lookup.h" - -#if OHOS_DNS_PROXY_BY_NETSYS - -#include "atomic.h" - -#define GETADDRINFO_PRINT_DEBUG(...) - -static GetCache load_cache_getter(void) -{ - static GetCache cache_getter = NULL; - resolve_dns_sym((void **) &cache_getter, OHOS_GET_CACHE_FUNC_NAME); - return cache_getter; -} - -static SetCache load_cache_setter(void) -{ - static SetCache cache_setter = NULL; - resolve_dns_sym((void **) &cache_setter, OHOS_SET_CACHE_FUNC_NAME); - return cache_setter; -} - -static PostDnsResult load_result_poster(void) -{ - static PostDnsResult result_poster = NULL; - resolve_dns_sym((void **) &result_poster, OHOS_POST_DNS_RESULT_FUNC_NAME); - return result_poster; -} - -void -dns_set_addr_info_to_netsys_cache2(const int netid, const char *restrict host, const char *restrict serv, - const struct addrinfo *restrict hint, struct addrinfo *res) -{ - SetCache func = load_cache_setter(); - if (!func) { - DNS_CONFIG_PRINT("%s: loading %s failed", __func__, OHOS_SET_CACHE_FUNC_NAME); - return; - } - - struct param_wrapper param = {(char *) host, (char *) serv, (struct addrinfo *) hint}; - int ret = func(netid, param, res); - if (ret < 0) { - GETADDRINFO_PRINT_DEBUG("dns_set_addr_info_to_netsys_cache OHOS_SET_CACHE_FUNC_NAME err %d\n", ret); - return; - } - - GETADDRINFO_PRINT_DEBUG("set to netsys cache OK\n"); -} - -void dns_set_addr_info_to_netsys_cache(const char *restrict host, const char *restrict serv, - const struct addrinfo *restrict hint, struct addrinfo *res) -{ - dns_set_addr_info_to_netsys_cache2(0, host, serv, hint, res); -} - -int dns_get_addr_info_from_netsys_cache2(const int netid, const char *restrict host, const char *restrict serv, - const struct addrinfo *restrict hint, struct addrinfo **restrict res) -{ - GetCache func = load_cache_getter(); - if (!func) { - DNS_CONFIG_PRINT("%s: loading %s failed", __func__, OHOS_GET_CACHE_FUNC_NAME); - return -1; - } - - struct addr_info_wrapper addr_info[MAX_RESULTS] = {0}; - uint32_t num = 0; - struct param_wrapper param = {(char *) host, (char *) serv, (struct addrinfo *) hint}; - int ret = func(netid, param, addr_info, &num); - if (ret < 0) { - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache OHOS_GET_CACHE_FUNC_NAME err %d\n", ret); - return -1; - } - - num = MACRO_MIN(num, MACRO_MIN(MAX_RESULTS, MAXADDRS)); - if (num == 0) { - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache num is invalid err %u", num); - return -1; - } - - int canon_len = (int) strlen(addr_info[0].ai_canonName); - struct aibuf *out = calloc(1, num * sizeof(*out) + canon_len + 1); - if (!out) { - return -1; - } - char *outcanon = NULL; - if (canon_len) { - outcanon = (char *) &out[num]; - memcpy(outcanon, addr_info[0].ai_canonName, canon_len + 1); - } - - for (int i = 0; i < num; i++) { - out[i].slot = (short) i; - out[i].ai = (struct addrinfo) { - .ai_flags = (int) addr_info[i].ai_flags, - .ai_family = (int) addr_info[i].ai_family, - .ai_socktype = (int) addr_info[i].ai_sockType, - .ai_protocol = (int) addr_info[i].ai_protocol, - .ai_addrlen = (socklen_t) addr_info[i].ai_addrLen, - .ai_addr = (void *) &out[i].sa, - .ai_canonname = outcanon, - }; - memcpy(&out[i].sa, &addr_info[i].ai_addr, addr_info[i].ai_addrLen); - if (i > 0) { - out[i - 1].ai.ai_next = &out[i].ai; - } - } - - out[0].ref = (short) num; - *res = &out->ai; - - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache end\n"); - return 0; -} - -int dns_get_addr_info_from_netsys_cache(const char *restrict host, const char *restrict serv, - const struct addrinfo *restrict hint, struct addrinfo **restrict res) -{ - return dns_get_addr_info_from_netsys_cache2(0, host, serv, hint, res); -} - -int dns_post_result_to_netsys_cache(int netid, char* name, int usedtime, int queryret, - struct addrinfo *res, struct queryparam *param) -{ - PostDnsResult func = load_result_poster(); - if (!func) { - GETADDRINFO_PRINT_DEBUG("%s: loading %s failed", __func__, OHOS_POST_DNS_RESULT_FUNC_NAME); - return -1; - } - - int ret = func(netid, name, usedtime, queryret, res, param); - if (ret < 0) { - GETADDRINFO_PRINT_DEBUG("dns_set_result_to_netsys_cache OHOS_POST_DNS_RESULT_FUNC_NAME err %d\n", ret); - return -1; - } - - GETADDRINFO_PRINT_DEBUG("dns_post_result_to_netsys_cache OK\n"); - return 0; -} - -int dns_get_default_network(int *currentnetid) -{ - void *handle = dlopen(DNS_SO_PATH, RTLD_LAZY); - if (handle == NULL) { - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlopen err %s\n", dlerror()); - return -1; - } - - GetDefaultNet func = dlsym(handle, OHOS_GET_DEFAULT_NET_FUNC_NAME); - if (func == NULL) { - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlsym err %s\n", dlerror()); - dlclose(handle); - return -1; - } - - int ret = func(0, currentnetid); - dlclose(handle); - - if (ret < 0) { - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache OHOS_GET_DEFAULT_NET_FUNC_NAME err %d\n", ret); - return -1; - } - - GETADDRINFO_PRINT_DEBUG("dns_post_result_to_netsys_cache OK %d\n", currentnetid); - return 0; -} - -#endif diff --git a/porting/linux/user/src/network/res_msend.c b/porting/linux/user/src/network/res_msend.c deleted file mode 100644 index 7ed9bce36..000000000 --- a/porting/linux/user/src/network/res_msend.c +++ /dev/null @@ -1,203 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "stdio_impl.h" -#include "syscall.h" -#include "lookup.h" - - -static void cleanup(void *p) -{ - __syscall(SYS_close, (intptr_t)p); -} - -static unsigned long mtime() -{ - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - return (unsigned long)ts.tv_sec * 1000 - + ts.tv_nsec / 1000000; -} - -int __res_msend_rc(int nqueries, const unsigned char *const *queries, - const int *qlens, unsigned char *const *answers, int *alens, int asize, - const struct resolvconf *conf) -{ - return res_msend_rc_ext(0, nqueries, queries, qlens, answers, alens, asize, conf); -} - -int res_msend_rc_ext(int netid, int nqueries, const unsigned char *const *queries, - const int *qlens, unsigned char *const *answers, int *alens, int asize, - const struct resolvconf *conf) -{ - int fd; - int timeout, attempts, retry_interval, servfail_retry; - union { - struct sockaddr_in sin; - struct sockaddr_in6 sin6; - } sa = {0}, ns[MAXNS] = {{0}}; - socklen_t sl = sizeof sa.sin; - int nns = 0; - int family = AF_INET; - int rlen; - int next; - int i, j; - int cs; - struct pollfd pfd; - unsigned long t0, t1, t2; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - - timeout = 1000*conf->timeout; - attempts = conf->attempts; - - for (nns=0; nnsnns; nns++) { - const struct address *iplit = &conf->ns[nns]; - if (iplit->family == AF_INET) { - memcpy(&ns[nns].sin.sin_addr, iplit->addr, 4); - ns[nns].sin.sin_port = htons(53); - ns[nns].sin.sin_family = AF_INET; - } else { - sl = sizeof sa.sin6; - memcpy(&ns[nns].sin6.sin6_addr, iplit->addr, 16); - ns[nns].sin6.sin6_port = htons(53); - ns[nns].sin6.sin6_scope_id = iplit->scopeid; - ns[nns].sin6.sin6_family = family = AF_INET6; - } - } - - /* Get local address and open/bind a socket */ - sa.sin.sin_family = family; - fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - - /* Handle case where system lacks IPv6 support */ - if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) { - fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - family = AF_INET; - } - if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) { - if (fd >= 0) close(fd); - pthread_setcancelstate(cs, 0); - return -1; - } - - /* Past this point, there are no errors. Each individual query will - * yield either no reply (indicated by zero length) or an answer - * packet which is up to the caller to interpret. */ - - pthread_cleanup_push(cleanup, (void *)(intptr_t)fd); - pthread_setcancelstate(cs, 0); - - /** - * Todo FwmarkClient::BindSocket - */ - if (netid > 0) { - res_bind_socket(fd, netid); - } - - /* Convert any IPv4 addresses in a mixed environment to v4-mapped */ - if (family == AF_INET6) { - setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0); - for (i=0; i= retry_interval) { - /* Query all configured namservers in parallel */ - for (i=0; i= 0) { - - /* Ignore non-identifiable packets */ - if (rlen < 4) continue; - - /* Ignore replies from addresses we didn't send to */ - for (j=0; j -#include -#include -#include -#include - -#include "hilog_adapter.h" - -#if 1 -#define SIGCHAIN_LOG_DOMAIN 0xD003F00 -#define SIGCHAIN_LOG_TAG "ressolvconf" -#define GETADDRINFO_PRINT_DEBUG(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \ - SIGCHAIN_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__)) -#else -#define GETADDRINFO_PRINT_DEBUG(...) -#endif - -#define DNS_RESOLV_CONF_PATH "/etc/resolv.conf" - -#if OHOS_DNS_PROXY_BY_NETSYS | OHOS_FWMARK_CLIENT_BY_NETSYS -#include "atomic.h" - -#include - -static void *open_dns_lib(void) -{ - static void *dns_lib_handle = NULL; - if (dns_lib_handle != NULL) { - a_barrier(); - return dns_lib_handle; - } - - void *lib = dlopen(DNS_SO_PATH, RTLD_LAZY); - if (lib == NULL) { - DNS_CONFIG_PRINT("%s: dlopen %s failed: %s", - __func__, DNS_SO_PATH, dlerror()); - return NULL; - } - - void *old_lib = a_cas_p(&dns_lib_handle, NULL, lib); - if (old_lib == NULL) { - DNS_CONFIG_PRINT("%s: %s loaded", __func__, DNS_SO_PATH); - return lib; - } else { - /* Another thread has already loaded the library, - * dlclose is invoked to make refcount correct */ - DNS_CONFIG_PRINT("%s: %s has been loaded by another thread", - __func__, DNS_SO_PATH); - if (dlclose(lib)) { - DNS_CONFIG_PRINT("%s: dlclose %s failed: %s", - __func__, DNS_SO_PATH, dlerror()); - } - return old_lib; - } -} - -static void *load_from_dns_lib(const char *symbol) -{ - void *lib_handle = open_dns_lib(); - if (lib_handle == NULL) { - return NULL; - } - - void *sym_addr = dlsym(lib_handle, symbol); - if (sym_addr == NULL) { - DNS_CONFIG_PRINT("%s: loading symbol %s with dlsym failed: %s", - __func__, symbol, dlerror()); - } - return sym_addr; -} - -void resolve_dns_sym(void **holder, const char *symbol) -{ - if (*holder != NULL) { - a_barrier(); - return; - } - - void *ptr = load_from_dns_lib(symbol); - if (ptr == NULL) { - return; - } - - void *old_ptr = a_cas_p(holder, NULL, ptr); - if (old_ptr != NULL) { - DNS_CONFIG_PRINT("%s: %s has been found by another thread", - __func__, symbol); - } else { - DNS_CONFIG_PRINT("%s: %s found", __func__, symbol); - } -} - -static GetConfig load_config_getter(void) -{ - static GetConfig config_getter = NULL; - resolve_dns_sym((void **) &config_getter, OHOS_GET_CONFIG_FUNC_NAME); - return config_getter; -} - -#endif - -int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz) -{ - return get_resolv_conf_ext(conf, search, search_sz, 0); -} - -int get_resolv_conf_ext(struct resolvconf *conf, char *search, size_t search_sz, int netid) -{ - char line[256]; - unsigned char _buf[256]; - FILE *f, _f; - int nns = 0; - - conf->ndots = 1; - conf->timeout = 5; - conf->attempts = 2; - if (search) *search = 0; - -#if OHOS_DNS_PROXY_BY_NETSYS - GetConfig func = load_config_getter(); - if (!func) { - DNS_CONFIG_PRINT("%s: loading %s failed, use %s as a fallback", - __func__, OHOS_GET_CONFIG_FUNC_NAME, DNS_RESOLV_CONF_PATH); - goto etc_resolv_conf; - } - - struct resolv_config config = {0}; - int ret = func(netid, &config); - if (ret < 0) { - DNS_CONFIG_PRINT("__get_resolv_conf OHOS_GET_CONFIG_FUNC_NAME err %d\n", ret); - return EAI_NONAME; - } - int32_t timeout_second = config.timeout_ms / 1000; - -netsys_conf: - if (timeout_second > 0) { - if (timeout_second >= 60) { - conf->timeout = 60; - } else { - conf->timeout = timeout_second; - } - } - if (config.retry_count > 0) { - if (config.retry_count >= 10) { - conf->attempts = 10; - } else { - conf->attempts = config.retry_count; - } - } - for (int i = 0; i < MAX_SERVER_NUM; ++i) { - if (config.nameservers[i] == NULL || config.nameservers[i][0] == 0 || nns >= MAXNS) { - continue; - } - if (__lookup_ipliteral(conf->ns + nns, config.nameservers[i], AF_UNSPEC) > 0) { - nns++; - } - } - - if (nns != 0) { - goto get_conf_ok; - } - -etc_resolv_conf: -#endif - f = __fopen_rb_ca(DNS_RESOLV_CONF_PATH, &_f, _buf, sizeof _buf); - if (!f) switch (errno) { - case ENOENT: - case ENOTDIR: - case EACCES: - goto no_resolv_conf; - default: - return -1; - } - - while (fgets(line, sizeof line, f)) { - char *p, *z; - if (!strchr(line, '\n') && !feof(f)) { - /* Ignore lines that get truncated rather than - * potentially misinterpreting them. */ - int c; - do c = getc(f); - while (c != '\n' && c != EOF); - continue; - } - if (!strncmp(line, "options", 7) && isspace(line[7])) { - p = strstr(line, "ndots:"); - if (p && isdigit(p[6])) { - p += 6; - unsigned long x = strtoul(p, &z, 10); - if (z != p) conf->ndots = x > 15 ? 15 : x; - } - p = strstr(line, "attempts:"); - if (p && isdigit(p[9])) { - p += 9; - unsigned long x = strtoul(p, &z, 10); - if (z != p) conf->attempts = x > 10 ? 10 : x; - } - p = strstr(line, "timeout:"); - if (p && (isdigit(p[8]) || p[8]=='.')) { - p += 8; - unsigned long x = strtoul(p, &z, 10); - if (z != p) conf->timeout = x > 60 ? 60 : x; - } - continue; - } - if (!strncmp(line, "nameserver", 10) && isspace(line[10])) { - if (nns >= MAXNS) continue; - for (p=line+11; isspace(*p); p++); - for (z=p; *z && !isspace(*z); z++); - *z=0; - if (__lookup_ipliteral(conf->ns+nns, p, AF_UNSPEC) > 0) - nns++; - continue; - } - - if (!search) continue; - if ((strncmp(line, "domain", 6) && strncmp(line, "search", 6)) - || !isspace(line[6])) - continue; - for (p=line+7; isspace(*p); p++); - size_t l = strlen(p); - /* This can never happen anyway with chosen buffer sizes. */ - if (l >= search_sz) continue; - memcpy(search, p, l+1); - } - - __fclose_ca(f); - -no_resolv_conf: - if (!nns) { - __lookup_ipliteral(conf->ns, "127.0.0.1", AF_UNSPEC); - nns = 1; - } - -get_conf_ok: - conf->nns = nns; - - return 0; -} - -int res_bind_socket(int fd, int netid) -{ - int ret = -1; - void* libhandler; - - GETADDRINFO_PRINT_DEBUG("res_bind_socket netid:%{public}d \n", netid); - -#ifdef OHOS_FWMARK_CLIENT_BY_NETSYS - libhandler = dlopen(FWMARKCLIENT_SO_PATH, RTLD_LAZY); - if (libhandler == NULL) { - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlopen err %s\n", dlerror()); - return -1; - } - - BindSocket_Ext func = dlsym(libhandler, OHOS_BIND_SOCKET_FUNC_NAME); - if (func == NULL) { - GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlsym err %s\n", dlerror()); - dlclose(libhandler); - return -1; - } else { - ret = func(fd, netid); - GETADDRINFO_PRINT_DEBUG("res_bind_socket ret %{public}d \n", ret); - dlclose(libhandler); - } -#endif - return ret; -} - diff --git a/porting/linux/user/src/network/socket.c b/porting/linux/user/src/network/socket.c deleted file mode 100644 index 21dd8fed4..000000000 --- a/porting/linux/user/src/network/socket.c +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" - -#if OHOS_PERMISSION_INTERNET -typedef uint8_t (*AllowFunc)(void); -static const char *LIB_NETSYS_CLIENT_NAME = "libnetsys_client.z.so"; -static const char *ALLOW_SOCKET_FUNC_NAME = "IsAllowInternet"; - -/* - * Read a flag from netsys_client, there is only one place to set this flag, is the - * founction named DoStartup in startup_appspawn. - * */ -uint8_t is_allow_internet(void) -{ - static uint8_t first_time = 1; - static uint8_t allow = 1; - - if (!first_time) { - return allow; - } - - void *handler = dlopen(LIB_NETSYS_CLIENT_NAME, RTLD_LAZY); - if (handler != NULL) { - AllowFunc func = (AllowFunc)dlsym(handler, ALLOW_SOCKET_FUNC_NAME); - if (func != NULL && func() == 0) { - allow = 0; - } - dlclose(handler); - } - first_time = 0; - return allow; -} -#endif - -#ifdef OHOS_SOCKET_HOOK_ENABLE -int __libc_socket(int domain, int type, int protocol) -#else -int socket(int domain, int type, int protocol) -#endif -{ -#if OHOS_PERMISSION_INTERNET - if ((domain == AF_INET || domain == AF_INET6) && is_allow_internet() == 0) { - errno = EPERM; - return -1; - } -#endif - int s = __socketcall(socket, domain, type, protocol, 0, 0, 0); - if ((s==-EINVAL || s==-EPROTONOSUPPORT) - && (type & (SOCK_CLOEXEC | SOCK_NONBLOCK))) { - s = __socketcall(socket, domain, - type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK), - protocol, 0, 0, 0); - if (s < 0) { - return __syscall_ret(s); - } - if (type & SOCK_CLOEXEC) { - __syscall(SYS_fcntl, s, F_SETFD, FD_CLOEXEC); - } - if (type & SOCK_NONBLOCK) { - __syscall(SYS_fcntl, s, F_SETFL, O_NONBLOCK); - } - } - return __syscall_ret(s); -} \ No newline at end of file diff --git a/porting/linux/user/src/passwd/getspnam_r.c b/porting/linux/user/src/passwd/getspnam_r.c deleted file mode 100644 index e738d656c..000000000 --- a/porting/linux/user/src/passwd/getspnam_r.c +++ /dev/null @@ -1,132 +0,0 @@ -#include -#include -#include -#include -#include -#include "pwf.h" - -/* This implementation support Openwall-style TCB passwords in place of - * traditional shadow, if the appropriate directories and files exist. - * Thus, it is careful to avoid following symlinks or blocking on fifos - * which a malicious user might create in place of his or her TCB shadow - * file. It also avoids any allocation to prevent memory-exhaustion - * attacks via huge TCB shadow files. */ - -static long xatol(char **s) -{ - long x; - int sign; - if (**s == ':' || **s == '\n') return -1; - - sign = (int)(unsigned char)**s; - if (sign == '-' || sign == '+') ++*s; - - for (x=0; **s-'0'<10U; ++*s) x=10*x+(**s-'0'); - - if (sign == '-') return -x; - return x; -} - -int __parsespent(char *s, struct spwd *sp) -{ - sp->sp_namp = s; - if (!(s = strchr(s, ':'))) return -1; - *s = 0; - - sp->sp_pwdp = ++s; - if (!(s = strchr(s, ':'))) return -1; - *s = 0; - - s++; sp->sp_lstchg = xatol(&s); - if (*s != ':') return -1; - - s++; sp->sp_min = xatol(&s); - if (*s != ':') return -1; - - s++; sp->sp_max = xatol(&s); - if (*s != ':') return -1; - - s++; sp->sp_warn = xatol(&s); - if (*s != ':') return -1; - - s++; sp->sp_inact = xatol(&s); - if (*s != ':') return -1; - - s++; sp->sp_expire = xatol(&s); - if (*s != ':') return -1; - - s++; sp->sp_flag = xatol(&s); - if (*s != '\n') return -1; - return 0; -} - -static void cleanup(void *p) -{ - fclose(p); -} - -int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct spwd **res) -{ - char path[20+NAME_MAX]; - FILE *f = 0; - int rv = 0; - int fd; - size_t k, l = strlen(name); - int skip = 0; - int cs; - int orig_errno = errno; - - *res = 0; - - /* Disallow potentially-malicious user names */ - if (*name=='.' || strchr(name, '/') || !l) - return errno = EINVAL; - - /* Buffer size must at least be able to hold name, plus some.. */ - if (size < l+100) - return errno = ERANGE; - - /* Protect against truncation */ - if (snprintf(path, sizeof path, "/etc/tcb/%s/shadow", name) >= sizeof path) - return errno = EINVAL; - - fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC); - if (fd >= 0) { - struct stat st = { 0 }; - errno = EINVAL; - if (fstat(fd, &st) || !S_ISREG(st.st_mode) || !(f = fdopen(fd, "rb"))) { - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - close(fd); - pthread_setcancelstate(cs, 0); - return errno; - } - } else { - if (errno != ENOENT && errno != ENOTDIR) - return errno; - f = fopen("/etc/shadow", "rbe"); - if (!f) { - if (errno != ENOENT && errno != ENOTDIR) - return errno; - return 0; - } - } - - pthread_cleanup_push(cleanup, f); - while (fgets(buf, size, f) && (k=strlen(buf))>0) { - if (skip || strncmp(name, buf, l) || buf[l]!=':') { - skip = buf[k-1] != '\n'; - continue; - } - if (buf[k-1] != '\n') { - rv = ERANGE; - break; - } - - if (__parsespent(buf, sp) < 0) continue; - *res = sp; - break; - } - pthread_cleanup_pop(1); - errno = rv ? rv : orig_errno; - return rv; -} diff --git a/porting/linux/user/src/process/_Fork.c b/porting/linux/user/src/process/_Fork.c deleted file mode 100644 index 710a7a7bc..000000000 --- a/porting/linux/user/src/process/_Fork.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include "syscall.h" -#include "libc.h" -#include "lock.h" -#include "pthread_impl.h" -#include "aio_impl.h" -#include "proc_xid_impl.h" - -static void dummy(int x) { } -weak_alias(dummy, __aio_atfork); - -pid_t _Fork(void) -{ - pid_t ret; - sigset_t set; - __block_all_sigs(&set); - __aio_atfork(-1); - LOCK(__abort_lock); -#ifdef SYS_fork - ret = __syscall(SYS_fork); -#else - ret = __syscall(SYS_clone, SIGCHLD, 0); -#endif - if (!ret) { - pthread_t self = __pthread_self(); - self->tid = __syscall(SYS_gettid); - self->pid = self->tid; - self->proc_tid = -1; - self->robust_list.off = 0; - self->robust_list.pending = 0; - self->next = self->prev = self; - __thread_list_lock = 0; - libc.threads_minus_1 = 0; - __clear_proc_pid(); - if (libc.need_locks) libc.need_locks = -1; - } - UNLOCK(__abort_lock); - __aio_atfork(!ret); - __restore_sigs(&set); - return __syscall_ret(ret); -} diff --git a/porting/linux/user/src/process/vfork.c b/porting/linux/user/src/process/vfork.c deleted file mode 100644 index 0f4fba94d..000000000 --- a/porting/linux/user/src/process/vfork.c +++ /dev/null @@ -1,31 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include "syscall.h" -#include "pthread_impl.h" -#include "proc_xid_impl.h" - -hidden pid_t __vfork(void) -{ - /* vfork syscall cannot be made from C code */ -#ifdef SYS_fork - return syscall(SYS_fork); -#else - return syscall(SYS_clone, SIGCHLD, 0); -#endif -} - -pid_t vfork(void) -{ - pthread_t self = __pthread_self(); - pid_t parent_pid = self->pid; - self->pid = 0; - pid_t ret = __vfork(); - if (ret != 0) { - self->pid = parent_pid; - } else { - self->proc_tid = -1; - __clear_proc_pid(); - } - return ret; -} diff --git a/porting/linux/user/src/sched/sched_getcpu.c b/porting/linux/user/src/sched/sched_getcpu.c deleted file mode 100644 index 72597b2d1..000000000 --- a/porting/linux/user/src/sched/sched_getcpu.c +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define _GNU_SOURCE -#include -#include -#include "syscall.h" -#include "atomic.h" - -#ifdef VDSO_GETCPU_SYM - -static void *volatile vdso_func; - -typedef long (*getcpu_f)(unsigned *, unsigned *, void *); - -static long getcpu_init(unsigned *cpu, unsigned *node, void *unused) -{ - __get_vdso_info(); - void *p = __get_vdso_addr(VDSO_GETCPU_VER, VDSO_GETCPU_SYM); - getcpu_f f = (getcpu_f)p; - a_cas_p(&vdso_func, (void *)getcpu_init, p); - return f ? f(cpu, node, unused) : -ENOSYS; -} - -static void *volatile vdso_func = (void *)getcpu_init; - -#endif - -int sched_getcpu(void) -{ - int r; - unsigned cpu; - -#ifdef VDSO_GETCPU_SYM - getcpu_f f = (getcpu_f)vdso_func; - if (f) { - r = f(&cpu, 0, 0); - if (!r) return cpu; - if (r != -ENOSYS) return __syscall_ret(r); - } -#endif - - r = __syscall(SYS_getcpu, &cpu, 0, 0); - if (!r) return cpu; - return __syscall_ret(r); -} diff --git a/porting/linux/user/src/sched/sched_getparam.c b/porting/linux/user/src/sched/sched_getparam.c deleted file mode 100644 index b31f00892..000000000 --- a/porting/linux/user/src/sched/sched_getparam.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include "syscall.h" -#include - -int sched_getparam(pid_t pid, struct sched_param *param) -{ - int r; - if (!param) { - r = -EINVAL; - goto exit; - } - memset(param, 0, sizeof(struct sched_param)); - r = __syscall(SYS_sched_getparam, pid , param); - if (r >= 0) { - r = 0; - } -exit: - return __syscall_ret(r); -} diff --git a/porting/linux/user/src/sched/sched_getscheduler.c b/porting/linux/user/src/sched/sched_getscheduler.c deleted file mode 100644 index 454ac8bce..000000000 --- a/porting/linux/user/src/sched/sched_getscheduler.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include "syscall.h" - -int sched_getscheduler(pid_t pid) -{ - int r = __syscall(SYS_sched_getscheduler, pid); - return __syscall_ret(r); -} \ No newline at end of file diff --git a/porting/linux/user/src/sched/sched_setparam.c b/porting/linux/user/src/sched/sched_setparam.c deleted file mode 100644 index 2e0f79bff..000000000 --- a/porting/linux/user/src/sched/sched_setparam.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include "syscall.h" - -int sched_setparam(pid_t pid, const struct sched_param *param) -{ - int r; - if (!param) { - r = -EINVAL; - goto exit; - } - r = __syscall(SYS_sched_setparam, pid, param); -exit: - return __syscall_ret(r); -} \ No newline at end of file diff --git a/porting/linux/user/src/sched/sched_setscheduler.c b/porting/linux/user/src/sched/sched_setscheduler.c deleted file mode 100644 index fcb5873f5..000000000 --- a/porting/linux/user/src/sched/sched_setscheduler.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include "syscall.h" - -int sched_setscheduler(pid_t pid, int sched, const struct sched_param *param) -{ - int r; - if (!param) { - r = -EINVAL; - goto exit; - } - r = __syscall(SYS_sched_setscheduler, pid , sched , param); -exit: - return __syscall_ret(r); -} \ No newline at end of file diff --git a/porting/linux/user/src/signal/sigaction.c b/porting/linux/user/src/signal/sigaction.c deleted file mode 100644 index 3f0dc522c..000000000 --- a/porting/linux/user/src/signal/sigaction.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include "syscall.h" -#include "pthread_impl.h" -#include "libc.h" -#include "lock.h" -#include "ksigaction.h" - -extern bool intercept_sigaction(int signo, const struct sigaction *restrict sa, struct sigaction *restrict old); - -static volatile int dummy_lock[1] = { 0 }; - -extern hidden volatile int __abort_lock[1]; - -weak_alias(dummy_lock, __abort_lock); - -static int unmask_done; -static unsigned long handler_set[_NSIG/(8*sizeof(long))]; - -void __get_handler_set(sigset_t *set) -{ - memcpy(set, handler_set, sizeof handler_set); -} - -volatile int __eintr_valid_flag; - -int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) -{ - struct k_sigaction ksa, ksa_old; - unsigned long set[_NSIG/(8*sizeof(long))]; - if (sa) { - if ((uintptr_t)sa->sa_handler > 1UL) { - a_or_l(handler_set+(sig-1)/(8*sizeof(long)), - 1UL<<(sig-1)%(8*sizeof(long))); - - /* If pthread_create has not yet been called, - * implementation-internal signals might not - * yet have been unblocked. They must be - * unblocked before any signal handler is - * installed, so that an application cannot - * receive an illegal sigset_t (with them - * blocked) as part of the ucontext_t passed - * to the signal handler. */ - if (!libc.threaded && !unmask_done) { - __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, - SIGPT_SET, 0, _NSIG/8); - unmask_done = 1; - } - - if (!(sa->sa_flags & SA_RESTART)) { - a_store(&__eintr_valid_flag, 1); - } - } - /* Changing the disposition of SIGABRT to anything but - * SIG_DFL requires a lock, so that it cannot be changed - * while abort is terminating the process after simply - * calling raise(SIGABRT) failed to do so. */ - if (sa->sa_handler != SIG_DFL && sig == SIGABRT) { - __block_all_sigs(&set); - LOCK(__abort_lock); - } - ksa.handler = sa->sa_handler; - ksa.flags = sa->sa_flags | SA_RESTORER; - ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore; - memcpy(&ksa.mask, &sa->sa_mask, _NSIG/8); - } - int r = __syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8); - if (sig == SIGABRT && sa && sa->sa_handler != SIG_DFL) { - UNLOCK(__abort_lock); - __restore_sigs(&set); - } - if (old && !r) { - old->sa_handler = ksa_old.handler; - old->sa_flags = ksa_old.flags; - memcpy(&old->sa_mask, &ksa_old.mask, _NSIG/8); - } - return __syscall_ret(r); -} - -int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) -{ - if (sig-32U < 3 || sig-1U >= _NSIG-1) { - errno = EINVAL; - return -1; - } - /* sigchain intercepts sigaction */ - if (intercept_sigaction(sig, sa, old)) { - return 0; - } - - return __libc_sigaction(sig, sa, old); -} - -weak_alias(__sigaction, sigaction); diff --git a/porting/linux/user/src/signal/signal.c b/porting/linux/user/src/signal/signal.c deleted file mode 100644 index 0dbd3f6a8..000000000 --- a/porting/linux/user/src/signal/signal.c +++ /dev/null @@ -1,12 +0,0 @@ -#include - -void (*signal(int sig, void (*func)(int)))(int) -{ - struct sigaction sa_old, sa = { .sa_handler = func, .sa_flags = SA_RESTART }; - if (__sigaction(sig, &sa, &sa_old) < 0) - return SIG_ERR; - return sa_old.sa_handler; -} - -weak_alias(signal, bsd_signal); -weak_alias(signal, __sysv_signal); diff --git a/porting/linux/user/src/stat/fstat.c b/porting/linux/user/src/stat/fstat.c deleted file mode 100644 index 65084e6c3..000000000 --- a/porting/linux/user/src/stat/fstat.c +++ /dev/null @@ -1,15 +0,0 @@ -#define _BSD_SOURCE -#include -#include -#include -#include "syscall.h" - -int __fstat(int fd, struct stat *st) -{ - if (fd<0) return __syscall_ret(-EBADF); - return fstatat(fd, "", st, AT_EMPTY_PATH); -} -weak_alias(__fstat, fstat); -#if !_REDIR_TIME64 -weak_alias(__fstat, fstat64); -#endif diff --git a/porting/linux/user/src/stdio/__fdopen.c b/porting/linux/user/src/stdio/__fdopen.c deleted file mode 100644 index 4eb342104..000000000 --- a/porting/linux/user/src/stdio/__fdopen.c +++ /dev/null @@ -1,139 +0,0 @@ -/* Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "stdio_impl.h" -#include -#include -#include -#include -#include -#include -#include "libc.h" - -static size_t __get_bufsize(int fd) -{ - struct stat st; - size_t buf_size = 0; - - if (fstat(fd, &st) < 0) { - buf_size = BUFSIZ; - } else if (st.st_blksize == 0) { - buf_size = BUFSIZ; - } else { - buf_size = st.st_blksize; - } - - return buf_size; -} - -int __falloc_buf(FILE *f) -{ - /* return if already allocated, or F_NOBUF set */ - if (f->buf != NULL || f->buf_size != 0 || f->flags & F_NOBUF) { - return 0; - } - - /* Default, base and buf are NULL,and buf_size = 0 */ - size_t buf_size = 0; - - /* get buffer size via file stat */ - buf_size = __get_bufsize(f->fd); - - /* alloc R/W buffer */ - f->base = (unsigned char *)malloc(UNGET + buf_size * sizeof(unsigned char)); - if (!f->base) { - errno = -ENOMEM; - return errno; - } - - /* reserve UNGET buffer */ - f->buf = f->base + UNGET; - f->buf_size = buf_size; - - return 0; -} - -FILE *__fdopen(int fd, const char *mode) -{ - FILE *f = NULL; - int file_flags = 0; - int mode_flags = 0; - - int flags = syscall(SYS_fcntl, fd, F_GETFL, 0); - if (flags == -1) { - return NULL; - } - - /* Compute the flags to pass to open() */ - mode_flags = __fmodeflags(mode, &file_flags); - if (mode_flags < 0) { - return NULL; - } - - if (mode_flags & O_CLOEXEC) { - __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); - } - - if (mode_flags & O_APPEND) { - flags = __syscall(SYS_fcntl, fd, F_GETFL); - if (!(flags & O_APPEND)) - __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND); - } - - f = __fdopenx(fd, file_flags); - if (f) { - return f; - } - - return NULL; -} -weak_alias(__fdopen, fdopen); - -FILE *__fdopenx(int fd, int flags) -{ - FILE *f = 0; - struct winsize wsz; - - /* Allocate FILE or fail */ - if (!(f = __ofl_alloc())) { - return NULL; - } - - /* Zero-fill only the struct, not the buffer */ - memset(f, 0, sizeof *f); - - f->flags = flags; - f->fd = fd; - - /* Activate line buffered mode for terminals */ - f->lbf = EOF; - if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) { - f->lbf = '\n'; - } - - /* Initialize op ptrs. No problem if some are unneeded. */ - f->read = __stdio_read; - f->write = __stdio_write; - f->seek = __stdio_seek; - f->close = __stdio_close; - f->readx = __stdio_readx; - - if (!libc.threaded) { - f->lock = -1; - } - - /* Add new FILE to open file list */ - return __ofl_add(f); -} - diff --git a/porting/linux/user/src/stdio/__fmodeflags.c b/porting/linux/user/src/stdio/__fmodeflags.c deleted file mode 100644 index f738b270a..000000000 --- a/porting/linux/user/src/stdio/__fmodeflags.c +++ /dev/null @@ -1,55 +0,0 @@ -#include "stdio_impl.h" -#include -#include -#include - -int __fmodeflags(const char *mode, int *flags) -{ - int mode_flags = 0; - int options = 0; - - switch (*mode) { - case 'r': - mode_flags = O_RDONLY; - *flags = F_NOWR; - break; - case 'w': - mode_flags = O_WRONLY; - options = O_TRUNC | O_CREAT; - *flags = F_NORD; - break; - case 'a': - mode_flags = O_WRONLY; - options = O_APPEND | O_CREAT; - *flags = F_NORD | F_APP; - break; - default: - errno = EINVAL; - return -EINVAL; - } - - mode++; - while (*mode != '\0') { - switch (*mode) { - case '+': - mode_flags = O_RDWR; - *flags &= ~(F_NORD | F_NOWR); - break; - case 'x': - /* need O_CREAT check */ - options |= O_EXCL; - break; - case 'e': - options |= O_CLOEXEC; - break; - case 'b': - break; - default: - /* only accept "+xeb" */ - break; - } - mode++; - } - - return mode_flags | options; -} diff --git a/porting/linux/user/src/stdio/__stdio_read.c b/porting/linux/user/src/stdio/__stdio_read.c deleted file mode 100644 index dd681e0b8..000000000 --- a/porting/linux/user/src/stdio/__stdio_read.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "stdio_impl.h" -#include - -size_t __stdio_readx(FILE *f, unsigned char *buf, size_t len) -{ - ssize_t cnt = syscall(SYS_read, f->fd, buf, len); - if (cnt <= 0) { - f->flags |= cnt ? F_ERR : F_EOF; - return 0; - } - return cnt; -} - -size_t __stdio_read(FILE *f, unsigned char *buf, size_t len) -{ - struct iovec iov_buf[2] = { - { .iov_base = buf, .iov_len = len - !!f->buf_size }, - { .iov_base = f->buf, .iov_len = f->buf_size } - }; - ssize_t cnt; - - cnt = iov_buf[0].iov_len ? syscall(SYS_readv, f->fd, iov_buf, 2) - : syscall(SYS_read, f->fd, iov_buf[1].iov_base, iov_buf[1].iov_len); - if (cnt <= 0) { - f->flags |= cnt ? F_ERR : F_EOF; - return 0; - } - if (cnt <= iov_buf[0].iov_len) { - return cnt; - } - cnt -= iov_buf[0].iov_len; - f->rpos = f->buf; - f->rend = f->buf + cnt; - if (f->buf_size) buf[len-1] = *f->rpos++; - return len; -} diff --git a/porting/linux/user/src/stdio/__stdio_write.c b/porting/linux/user/src/stdio/__stdio_write.c deleted file mode 100644 index 6774a2946..000000000 --- a/porting/linux/user/src/stdio/__stdio_write.c +++ /dev/null @@ -1,64 +0,0 @@ -#include "stdio_impl.h" -#include -#include - -ssize_t __flush_buffer(FILE *f) -{ - ssize_t cnt = 0; - char *wbase = (char *)f->wbase; - size_t rem = f->wpos - f->wbase; - while (rem > 0) { - cnt = syscall(SYS_write, f->fd, wbase, rem); - if (cnt < 0) { - f->wpos = f->wbase = f->wend = 0; - f->flags |= F_ERR; - return cnt; - } - wbase += cnt; - rem -= cnt; - } - - /* reset file buffer */ - f->wend = f->buf + f->buf_size; - f->wpos = f->wbase = f->buf; - return cnt; -} - -size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) -{ - size_t rem = len; - unsigned char *wbuf = (unsigned char *)buf; - - /* flush buffer first */ - ssize_t cnt = __flush_buffer(f); - if (cnt < 0) { - return 0; - } - - for (;;) { - if (f->lbf < 0 && rem <= f->wend - f->wpos) { - memcpy(f->wpos, wbuf, rem); - f->wpos += rem; - return len; - } - - /* write directly if - * 1. file buffer < rem - * 2. line buffer mode - */ - cnt = syscall(SYS_write, f->fd, wbuf, rem); - if (cnt < 0) { - f->wpos = f->wbase = f->wend = 0; - f->flags |= F_ERR; - return len - rem; - } - - rem -= cnt; - wbuf += cnt; - if (rem == 0) { - break; - } - } - - return len; -} diff --git a/porting/linux/user/src/stdio/__stdout_write.c b/porting/linux/user/src/stdio/__stdout_write.c deleted file mode 100644 index cd78374dd..000000000 --- a/porting/linux/user/src/stdio/__stdout_write.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "stdio_impl.h" -#include - -size_t __stdout_write(FILE *f, const unsigned char *buf, size_t len) -{ - struct winsize wsz; - f->write = __stdio_write; - /* - * write directly at first time. - * check f->flags & tty, and take effect later. - */ - size_t cnt = __stdio_write(f, buf, len); - - if (!(f->flags & F_SVB) && __syscall(SYS_ioctl, f->fd, TIOCGWINSZ, &wsz)) - f->lbf = -1; - - return cnt; -} diff --git a/porting/linux/user/src/stdio/__toread.c b/porting/linux/user/src/stdio/__toread.c deleted file mode 100644 index b03f878d3..000000000 --- a/porting/linux/user/src/stdio/__toread.c +++ /dev/null @@ -1,27 +0,0 @@ -#include - -int __toread(FILE *f) -{ - f->mode |= f->mode-1; - if (f->wpos != f->wbase) f->write(f, 0, 0); - f->wpos = f->wbase = f->wend = 0; - if (f->flags & F_NORD) { - f->flags |= F_ERR; - return EOF; - } - - /* Alloc file buffer if needed */ - if (__falloc_buf(f) < 0) { - f->flags |= F_ERR; - return EOF; - } - - f->rpos = f->rend = f->buf + f->buf_size; - - return (f->flags & F_EOF) ? EOF : 0; -} - -hidden void __toread_needs_stdio_exit() -{ - __stdio_exit_needed(); -} diff --git a/porting/linux/user/src/stdio/__towrite.c b/porting/linux/user/src/stdio/__towrite.c deleted file mode 100644 index 382a3abd5..000000000 --- a/porting/linux/user/src/stdio/__towrite.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "stdio_impl.h" - -int __towrite(FILE *f) -{ - f->mode |= f->mode-1; - if (f->flags & F_NOWR) { - f->flags |= F_ERR; - return EOF; - } - /* Clear read buffer (easier than summoning nasal demons) */ - f->rpos = f->rend = 0; - - /* Alloc file buffer if needed */ - if (__falloc_buf(f) < 0) { - f->flags |= F_ERR; - return EOF; - } - - /* Activate write through the buffer. */ - f->wpos = f->wbase = f->buf; - f->wend = f->buf + f->buf_size; - return 0; -} - -hidden void __towrite_needs_stdio_exit() -{ - __stdio_exit_needed(); -} diff --git a/porting/linux/user/src/stdio/fclose.c b/porting/linux/user/src/stdio/fclose.c deleted file mode 100644 index 4f3ab1133..000000000 --- a/porting/linux/user/src/stdio/fclose.c +++ /dev/null @@ -1,46 +0,0 @@ -#include "stdio_impl.h" -#include -#include - -static void dummy(FILE *f) { } -weak_alias(dummy, __unlist_locked_file); - -int fclose(FILE *f) -{ - int r; - - FLOCK(f); - if (!f || f->fd < 0) { - errno = EBADF; - FUNLOCK(f); - return -EBADF; - } - - r = fflush(f); - r |= f->close(f); - FUNLOCK(f); - - /* Past this point, f is closed and any further explict access - * to it is undefined. However, it still exists as an entry in - * the open file list and possibly in the thread's locked files - * list, if it was closed while explicitly locked. Functions - * which process these lists must tolerate dead FILE objects - * (which necessarily have inactive buffer pointers) without - * producing any side effects. */ - - if (f->flags & F_PERM) return r; - - __unlist_locked_file(f); - - free(f->getln_buf); - /* release base instead of buf which may be modified by setvbuf - * or iniitalize by local variable */ - free(f->base); - - /* set file to invalid descriptor */ - f->fd = -EBADF; - - __ofl_free(f); - - return r; -} diff --git a/porting/linux/user/src/stdio/fgets.c b/porting/linux/user/src/stdio/fgets.c deleted file mode 100644 index 4a2e81fd9..000000000 --- a/porting/linux/user/src/stdio/fgets.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "stdio_impl.h" -#include - -#define MIN(a,b) ((a)<(b) ? (a) : (b)) -char *fgets(char *restrict s, int n, FILE *restrict f) -{ - char *p = s; - unsigned char *z; - size_t k; - int c; - - FLOCK(f); - - if (n--<=1) { - f->mode |= f->mode-1; - FUNLOCK(f); - if (n) return 0; - *s = 0; - return s; - } - - while (n) { - if (f->rpos != f->rend) { - z = memchr(f->rpos, '\n', MIN(f->rend - f->rpos, n)); - k = z ? z - f->rpos + 1 : f->rend - f->rpos; - k = MIN(k, n); - memcpy(p, f->rpos, k); - f->rpos += k; - p += k; - n -= k; - if (z || !n) break; - } - if ((c = getc_unlocked(f)) < 0) { - if (p==s || !feof(f)) s = 0; - break; - } - n--; - if ((*p++ = c) == '\n') break; - } - if (s) *p = 0; - - FUNLOCK(f); - - return s; -} - - -weak_alias(fgets, fgets_unlocked); diff --git a/porting/linux/user/src/stdio/fileno.c b/porting/linux/user/src/stdio/fileno.c deleted file mode 100644 index 1eea733cb..000000000 --- a/porting/linux/user/src/stdio/fileno.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "stdio_impl.h" -#include - -int fileno_unlocked(FILE *f) -{ - int fd = f->fd; - if (fd < 0) { - errno = EBADF; - return -1; - } - return fd; -} - -int fileno(FILE *f) -{ - FLOCK(f); - int fd = fileno_unlocked(f); - FUNLOCK(f); - return fd; -} diff --git a/porting/linux/user/src/stdio/fmemopen.c b/porting/linux/user/src/stdio/fmemopen.c deleted file mode 100644 index bb857e5a8..000000000 --- a/porting/linux/user/src/stdio/fmemopen.c +++ /dev/null @@ -1,128 +0,0 @@ -#include "stdio_impl.h" -#include -#include -#include -#include -#include -#include "libc.h" - -struct cookie { - size_t pos, len, size; - unsigned char *buf; - int mode; -}; - -struct mem_FILE { - FILE f; - struct cookie c; - unsigned char buf[UNGET+BUFSIZ], buf2[]; -}; - -static off_t mseek(FILE *f, off_t off, int whence) -{ - ssize_t base; - struct cookie *c = f->cookie; - if (whence>2U) { -fail: - errno = EINVAL; - return -1; - } - base = (size_t [3]){0, c->pos, c->len}[whence]; - if (off < -base || off > (ssize_t)c->size-base) goto fail; - return c->pos = base+off; -} - -static size_t mread(FILE *f, unsigned char *buf, size_t len) -{ - struct cookie *c = f->cookie; - size_t rem = c->len - c->pos; - if (c->pos > c->len) rem = 0; - if (len > rem) { - len = rem; - f->flags |= F_EOF; - } - memcpy(buf, c->buf+c->pos, len); - c->pos += len; - rem -= len; - if (rem > f->buf_size) rem = f->buf_size; - f->rpos = f->buf; - f->rend = f->buf + rem; - memcpy(f->rpos, c->buf+c->pos, rem); - c->pos += rem; - return len; -} - -static size_t mwrite(FILE *f, const unsigned char *buf, size_t len) -{ - struct cookie *c = f->cookie; - size_t rem; - size_t len2 = f->wpos - f->wbase; - if (len2) { - f->wpos = f->wbase; - if (mwrite(f, f->wpos, len2) < len2) return 0; - } - if (c->mode == 'a') c->pos = c->len; - rem = c->size - c->pos; - if (len > rem) len = rem; - memcpy(c->buf+c->pos, buf, len); - c->pos += len; - if (c->pos > c->len) { - c->len = c->pos; - if (c->len < c->size) c->buf[c->len] = 0; - else if ((f->flags&F_NORD) && c->size) c->buf[c->size-1] = 0; - } - return len; -} - -static int mclose(FILE *m) -{ - return 0; -} - -FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode) -{ - struct mem_FILE *f; - int plus = !!strchr(mode, '+'); - - if (!strchr("rwa", *mode)) { - errno = EINVAL; - return 0; - } - - if (!buf && size > PTRDIFF_MAX) { - errno = ENOMEM; - return 0; - } - - f = malloc(sizeof *f + (buf?0:size)); - if (!f) return 0; - memset(f, 0, offsetof(struct mem_FILE, buf)); - f->f.cookie = &f->c; - f->f.fd = -1; - f->f.lbf = EOF; - f->f.buf = f->buf + UNGET; - f->f.buf_size = sizeof f->buf - UNGET; - if (!buf) { - buf = f->buf2; - memset(buf, 0, size); - } - - f->c.buf = buf; - f->c.size = size; - f->c.mode = *mode; - - if (!plus) f->f.flags = (*mode == 'r') ? F_NOWR : F_NORD; - if (*mode == 'r') f->c.len = size; - else if (*mode == 'a') f->c.len = f->c.pos = strnlen(buf, size); - else if (plus) *f->c.buf = 0; - - f->f.read = mread; - f->f.readx = mread; - f->f.write = mwrite; - f->f.seek = mseek; - f->f.close = mclose; - - if (!libc.threaded) f->f.lock = -1; - - return __ofl_add(&f->f); -} diff --git a/porting/linux/user/src/stdio/fopen.c b/porting/linux/user/src/stdio/fopen.c deleted file mode 100644 index b0e66aefd..000000000 --- a/porting/linux/user/src/stdio/fopen.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "stdio_impl.h" -#include -#include -#include - -FILE *fopen(const char *restrict filename, const char *restrict mode) -{ - FILE *f = NULL; - int fd = -1; - int file_flags = 0; - int mode_flags = 0; - - /* Compute the flags to pass to open() */ - mode_flags = __fmodeflags(mode, &file_flags); - if (mode_flags < 0) { - return NULL; - } - - fd = sys_open(filename, mode_flags, 0666); - if (fd < 0) return 0; - if (mode_flags & O_CLOEXEC) - __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); - - f = __fdopenx(fd, file_flags); - if (f) { - return f; - } - - __syscall(SYS_close, fd); - return 0; -} - -weak_alias(fopen, fopen64); diff --git a/porting/linux/user/src/stdio/fread.c b/porting/linux/user/src/stdio/fread.c deleted file mode 100644 index 38b685c1a..000000000 --- a/porting/linux/user/src/stdio/fread.c +++ /dev/null @@ -1,82 +0,0 @@ -#include "stdio_impl.h" -#include - -#define MIN(a,b) ((a)<(b) ? (a) : (b)) - -int __fill_buffer(FILE *f) -{ - int r = __toread(f); - if (r != 0) { - return r; - } - - int k = f->readx(f, f->buf, f->buf_size); - if (k <= 0) { - f->flags |= (k == 0) ? F_EOF : F_ERR; - f->rpos = f->rend; - return k; - } - - f->rpos = f->buf; - f->rend = f->rpos + k; - - return 0; -} - -size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f) -{ - unsigned char *dest = destv; - size_t len = size * nmemb, l = len, k; - if (!size) { - nmemb = 0; - } - - FLOCK(f); - - /* allocate file buffer if needed */ - if (__falloc_buf(f) < 0) { - f->flags |= F_ERR; - goto exit; - } - - f->mode |= f->mode-1; - - while (l > 0) { - if (f->rpos != f->rend) { - /* First exhaust the buffer. */ - k = MIN(f->rend - f->rpos, l); - memcpy(dest, f->rpos, k); - f->rpos += k; - dest += k; - l -= k; - } - /* done */ - if (l == 0) { - goto exit; - } - /* if user buffer is longer than file buffer, - * maybe buffer size is 0, non-buffer mode, - * read directly */ - if (l > f->buf_size) { - break; - } - - if (__fill_buffer(f)) { - goto exit; - } - } - - /* Read the remainder directly */ - for (; l; l-=k, dest+=k) { - k = f->readx(f, dest, l); - if (!k) { - break; - } - } - -exit: - FUNLOCK(f); - return (len - l) / size; -} - -weak_alias(fread, fread_unlocked); diff --git a/porting/linux/user/src/stdio/freopen.c b/porting/linux/user/src/stdio/freopen.c deleted file mode 100644 index 0f4ea987a..000000000 --- a/porting/linux/user/src/stdio/freopen.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "stdio_impl.h" -#include -#include - -/* The basic idea of this implementation is to open a new FILE, - * hack the necessary parts of the new FILE into the old one, then - * close the new FILE. */ - -/* Locking IS necessary because another thread may provably hold the - * lock, via flockfile or otherwise, when freopen is called, and in that - * case, freopen cannot act until the lock is released. */ - -FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f) -{ - int file_flags = 0; - int fl = __fmodeflags(mode, &file_flags); - FILE *f2; - - FLOCK(f); - - fflush(f); - - if (!filename) { - if (fl&O_CLOEXEC) - __syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC); - fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC); - if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0) { - goto fail; - } - } else { - f2 = fopen(filename, mode); - if (!f2) goto fail; - if (f2->fd == f->fd) { - f2->fd = -1; /* avoid closing in fclose */ - } - else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) { - goto fail2; - } - - f->flags = (f->flags & F_PERM) | f2->flags; - f->read = f2->read; - f->readx = f2->readx; - f->write = f2->write; - f->seek = f2->seek; - f->close = f2->close; - - fclose(f2); - } - - FUNLOCK(f); - return f; - -fail2: - fclose(f2); -fail: - fclose(f); - return NULL; -} - -weak_alias(freopen, freopen64); diff --git a/porting/linux/user/src/stdio/ofl.c b/porting/linux/user/src/stdio/ofl.c deleted file mode 100644 index eb2464799..000000000 --- a/porting/linux/user/src/stdio/ofl.c +++ /dev/null @@ -1,76 +0,0 @@ -#include "stdio_impl.h" -#include "lock.h" -#include "fork_impl.h" -#include - -#define DEFAULT_ALLOC_FILE (8) - -static FILE *ofl_head = NULL; -static FILE *ofl_free = NULL; - -static volatile int ofl_lock[1]; -volatile int *const __stdio_ofl_lockptr = ofl_lock; - -FILE **__ofl_lock() -{ - LOCK(ofl_lock); - return &FILE_LIST_HEAD(ofl_head); -} - -void __ofl_unlock() -{ - UNLOCK(ofl_lock); -} - -FILE *__ofl_alloc() -{ - FILE *fsb = NULL; - size_t cnt = 0; - FILE *f = NULL; - - LOCK(ofl_lock); - if (!FILE_LIST_EMPTY(ofl_free)) { - f = FILE_LIST_HEAD(ofl_free); - FILE_LIST_REMOVE(ofl_free); - UNLOCK(ofl_lock); - - return f; - } - UNLOCK(ofl_lock); - - /* alloc new FILEs(8) */ - fsb = (FILE *)malloc(DEFAULT_ALLOC_FILE * sizeof(FILE)); - if (!fsb) { - return NULL; - } - - LOCK(ofl_lock); - - for (cnt = 0; cnt < DEFAULT_ALLOC_FILE; cnt++) { - FILE_LIST_INSERT_HEAD(ofl_free, &fsb[cnt]); - } - - /* retrieve fist and move to next free FILE */ - f = FILE_LIST_HEAD(ofl_free); - FILE_LIST_REMOVE(ofl_free); - - UNLOCK(ofl_lock); - return f; -} - -void __ofl_free(FILE *f) -{ - LOCK(ofl_lock); - if (!f) { - UNLOCK(ofl_lock); - return; - } - - /* remove from head list */ - FILE_LIST_REMOVE(f); - - /* push to free list */ - FILE_LIST_INSERT_HEAD(ofl_free, f); - - UNLOCK(ofl_lock); -} diff --git a/porting/linux/user/src/stdio/ofl_add.c b/porting/linux/user/src/stdio/ofl_add.c deleted file mode 100644 index 7f94d9ca6..000000000 --- a/porting/linux/user/src/stdio/ofl_add.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "stdio_impl.h" - -FILE *__ofl_add(FILE *f) -{ - FILE **head = __ofl_lock(); - FILE_LIST_INSERT_HEAD(*head, f); - __ofl_unlock(); - return f; -} diff --git a/porting/linux/user/src/stdio/setvbuf.c b/porting/linux/user/src/stdio/setvbuf.c deleted file mode 100644 index 2fcd41db3..000000000 --- a/porting/linux/user/src/stdio/setvbuf.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "stdio_impl.h" - -/* The behavior of this function is undefined except when it is the first - * operation on the stream, so the presence or absence of locking is not - * observable in a program whose behavior is defined. Thus no locking is - * performed here. No allocation of buffers is performed, but a buffer - * provided by the caller is used as long as it is suitably sized. */ - -int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size) -{ - f->lbf = EOF; - - if (type == _IONBF) { - f->buf_size = 0; - f->flags |= F_NOBUF; - } else if (type == _IOLBF || type == _IOFBF) { - if (buf && size >= UNGET) { - f->buf = (void *)(buf + UNGET); - f->buf_size = size - UNGET; - } - if (type == _IOLBF && f->buf_size) - f->lbf = '\n'; - f->flags &= ~F_NOBUF; - } else { - return -1; - } - - f->flags |= F_SVB; - - return 0; -} diff --git a/porting/linux/user/src/stdio/stderr.c b/porting/linux/user/src/stdio/stderr.c deleted file mode 100644 index 4dd73a5bb..000000000 --- a/porting/linux/user/src/stdio/stderr.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "stdio_impl.h" - -#undef stderr - -static unsigned char buf[UNGET]; -hidden FILE __stderr_FILE = { - .buf = buf+UNGET, - .buf_size = 0, - .fd = 2, - .flags = F_PERM | F_NORD | F_NOBUF, - .lbf = -1, - .write = __stdio_write, - .seek = __stdio_seek, - .close = __stdio_close, - .lock = -1, -}; -FILE *const stderr = &__stderr_FILE; -FILE *volatile __stderr_used = &__stderr_FILE; diff --git a/porting/linux/user/src/stdio/stdin.c b/porting/linux/user/src/stdio/stdin.c deleted file mode 100644 index ca7b27c33..000000000 --- a/porting/linux/user/src/stdio/stdin.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "stdio_impl.h" - -#undef stdin - -static unsigned char buf[BUFSIZ+UNGET]; -hidden FILE __stdin_FILE = { - .buf = buf+UNGET, - .buf_size = sizeof buf-UNGET, - .fd = 0, - .flags = F_PERM | F_NOWR, - .read = __stdio_read, - .readx = __stdio_readx, - .seek = __stdio_seek, - .close = __stdio_close, - .lock = -1, -}; -FILE *const stdin = &__stdin_FILE; -FILE *volatile __stdin_used = &__stdin_FILE; diff --git a/porting/linux/user/src/stdio/vfprintf.c b/porting/linux/user/src/stdio/vfprintf.c deleted file mode 100644 index d4cdad575..000000000 --- a/porting/linux/user/src/stdio/vfprintf.c +++ /dev/null @@ -1,721 +0,0 @@ -#include "stdio_impl.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* Some useful macros */ - -#define MAX(a,b) ((a)>(b) ? (a) : (b)) -#define MIN(a,b) ((a)<(b) ? (a) : (b)) - -/* Convenient bit representation for modifier flags, which all fall - * within 31 codepoints of the space character. */ - -#define ALT_FORM (1U<<'#'-' ') -#define ZERO_PAD (1U<<'0'-' ') -#define LEFT_ADJ (1U<<'-'-' ') -#define PAD_POS (1U<<' '-' ') -#define MARK_POS (1U<<'+'-' ') -#define GROUPED (1U<<'\''-' ') - -#define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED) - -/* State machine to accept length modifiers + conversion specifiers. - * Result is 0 on failure, or an argument type to pop on success. */ - -enum { - BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE, - ZTPRE, JPRE, - STOP, - PTR, INT, UINT, ULLONG, - LONG, ULONG, - SHORT, USHORT, CHAR, UCHAR, - LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR, - DBL, LDBL, - NOARG, - MAXSTATE -}; - -#define S(x) [(x)-'A'] - -static const unsigned char states[]['z'-'A'+1] = { - { /* 0: bare types */ - S('d') = INT, S('i') = INT, - S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, - S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, - S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, - S('c') = CHAR, S('C') = INT, - S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR, - S('m') = NOARG, - S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE, - S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE, - }, { /* 1: l-prefixed */ - S('d') = LONG, S('i') = LONG, - S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, - S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, - S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, - S('c') = INT, S('s') = PTR, S('n') = PTR, - S('l') = LLPRE, - }, { /* 2: ll-prefixed */ - S('d') = LLONG, S('i') = LLONG, - S('o') = ULLONG, S('u') = ULLONG, - S('x') = ULLONG, S('X') = ULLONG, - S('n') = PTR, - }, { /* 3: h-prefixed */ - S('d') = SHORT, S('i') = SHORT, - S('o') = USHORT, S('u') = USHORT, - S('x') = USHORT, S('X') = USHORT, - S('n') = PTR, - S('h') = HHPRE, - }, { /* 4: hh-prefixed */ - S('d') = CHAR, S('i') = CHAR, - S('o') = UCHAR, S('u') = UCHAR, - S('x') = UCHAR, S('X') = UCHAR, - S('n') = PTR, - }, { /* 5: L-prefixed */ - S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL, - S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL, - S('n') = PTR, - }, { /* 6: z- or t-prefixed (assumed to be same size) */ - S('d') = PDIFF, S('i') = PDIFF, - S('o') = SIZET, S('u') = SIZET, - S('x') = SIZET, S('X') = SIZET, - S('n') = PTR, - }, { /* 7: j-prefixed */ - S('d') = IMAX, S('i') = IMAX, - S('o') = UMAX, S('u') = UMAX, - S('x') = UMAX, S('X') = UMAX, - S('n') = PTR, - } -}; - -#define OOB(x) ((unsigned)(x)-'A' > 'z'-'A') - -union arg -{ - uintmax_t i; - long double f; - void *p; -}; - -static void pop_arg(union arg *arg, int type, va_list *ap) -{ - switch (type) { - case PTR: arg->p = va_arg(*ap, void *); - break; case INT: arg->i = va_arg(*ap, int); - break; case UINT: arg->i = va_arg(*ap, unsigned int); - break; case LONG: arg->i = va_arg(*ap, long); - break; case ULONG: arg->i = va_arg(*ap, unsigned long); - break; case ULLONG: arg->i = va_arg(*ap, unsigned long long); - break; case SHORT: arg->i = (short)va_arg(*ap, int); - break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int); - break; case CHAR: arg->i = (signed char)va_arg(*ap, int); - break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int); - break; case LLONG: arg->i = va_arg(*ap, long long); - break; case SIZET: arg->i = va_arg(*ap, size_t); - break; case IMAX: arg->i = va_arg(*ap, intmax_t); - break; case UMAX: arg->i = va_arg(*ap, uintmax_t); - break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t); - break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *); - break; case DBL: arg->f = va_arg(*ap, double); - break; case LDBL: arg->f = va_arg(*ap, long double); - } -} - -static void out(FILE *f, const char *s, size_t l) -{ - if (!l) return; - - /* write to file buffer if flag F_PBUF is available */ - if (!(f->flags & F_ERR) && !(f->flags & F_PBUF)) { - __fwritex((void *)s, l, f); - return; - } - - /* otherwise, copy to buffer directly */ - f->write(f, (void *)s, l); -} - -static void pad(FILE *f, char c, int w, int l, int fl) -{ - char pad[16]; - if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return; - l = w - l; - __builtin_memset(pad, c, sizeof pad); - for (; l >= sizeof pad; l -= sizeof pad) - out(f, pad, sizeof pad); - out(f, pad, l); -} - -static const char xdigits[16] = { - "0123456789ABCDEF" -}; - -static char *fmt_x(uintmax_t x, char *s, int lower) -{ - for (; x; x>>=4) *--s = xdigits[(x&15)]|lower; - return s; -} - -static char *fmt_o(uintmax_t x, char *s) -{ - for (; x; x>>=3) *--s = '0' + (x&7); - return s; -} - -static char *fmt_u(uintmax_t x, char *s) -{ - unsigned long y; - for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10; - for (y=x; y; y/=10) *--s = '0' + y%10; - return s; -} - -/* Do not override this check. The floating point printing code below - * depends on the float.h constants being right. If they are wrong, it - * may overflow the stack. */ -#if LDBL_MANT_DIG == 53 -typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)]; -#endif - -static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t) -{ - uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion - + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion - uint32_t *a, *d, *r, *z; - int e2=0, e, i, j, l; - char buf[9+LDBL_MANT_DIG/4], *s; - const char *prefix="-0X+0X 0X-0x+0x 0x"; - int pl; - char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr; - - pl=1; - if (signbit(y)) { - y=-y; - } else if (fl & MARK_POS) { - prefix+=3; - } else if (fl & PAD_POS) { - prefix+=6; - } else prefix++, pl=0; - - if (!isfinite(y)) { - char *s = (t&32)?"inf":"INF"; - if (y!=y) s=(t&32)?"nan":"NAN"; - pad(f, ' ', w, 3+pl, fl&~ZERO_PAD); - out(f, prefix, pl); - out(f, s, 3); - pad(f, ' ', w, 3+pl, fl^LEFT_ADJ); - return MAX(w, 3+pl); - } - - y = frexpl(y, &e2) * 2; - if (y) e2--; - - if ((t|32)=='a') { - long double round = 8.0; - int re; - - if (t&32) prefix += 9; - pl += 2; - - if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0; - else re=LDBL_MANT_DIG/4-1-p; - - if (re) { - round *= 1<<(LDBL_MANT_DIG%4); - while (re--) round*=16; - if (*prefix=='-') { - y=-y; - y-=round; - y+=round; - y=-y; - } else { - y+=round; - y-=round; - } - } - - estr=fmt_u(e2<0 ? -e2 : e2, ebuf); - if (estr==ebuf) *--estr='0'; - *--estr = (e2<0 ? '-' : '+'); - *--estr = t+('p'-'a'); - - s=buf; - do { - int x=y; - *s++=xdigits[x]|(t&32); - y=16*(y-x); - if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.'; - } while (y); - - if (p > INT_MAX-2-(ebuf-estr)-pl) - return -1; - if (p && s-buf-2 < p) - l = (p+2) + (ebuf-estr); - else - l = (s-buf) + (ebuf-estr); - - pad(f, ' ', w, pl+l, fl); - out(f, prefix, pl); - pad(f, '0', w, pl+l, fl^ZERO_PAD); - out(f, buf, s-buf); - pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0); - out(f, estr, ebuf-estr); - pad(f, ' ', w, pl+l, fl^LEFT_ADJ); - return MAX(w, pl+l); - } - if (p<0) p=6; - - if (y) y *= 0x1p28, e2-=28; - - if (e2<0) a=r=z=big; - else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1; - - do { - *z = y; - y = 1000000000*(y-*z++); - } while (y); - - while (e2>0) { - uint32_t carry=0; - int sh=MIN(29,e2); - for (d=z-1; d>=a; d--) { - uint64_t x = ((uint64_t)*d<a && !z[-1]) z--; - e2-=sh; - } - while (e2<0) { - uint32_t carry=0, *b; - int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9; - for (d=a; d>sh) + carry; - carry = (1000000000>>sh) * rm; - } - if (!*a) a++; - if (carry) *z++ = carry; - /* Avoid (slow!) computation past requested precision */ - b = (t|32)=='f' ? r : a; - if (z-b > need) z = b+need; - e2+=sh; - } - - if (a=i; i*=10, e++); - else e=0; - - /* Perform rounding: j is precision after the radix (possibly neg) */ - j = p - ((t|32)!='f')*e - ((t|32)=='g' && p); - if (j < 9*(z-r-1)) { - uint32_t x; - /* We avoid C's broken division of negative numbers */ - d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP); - j += 9*LDBL_MAX_EXP; - j %= 9; - for (i=10, j++; j<9; i*=10, j++); - x = *d % i; - /* Are there any significant digits past j? */ - if (x || d+1!=z) { - long double round = 2/LDBL_EPSILON; - long double small; - if ((*d/i & 1) || (i==1000000000 && d>a && (d[-1]&1))) - round += 2; - if (x 999999999) { - *d--=0; - if (d=i; i*=10, e++); - } - } - if (z>d+1) z=d+1; - } - for (; z>a && !z[-1]; z--); - - if ((t|32)=='g') { - if (!p) p++; - if (p>e && e>=-4) { - t--; - p-=e+1; - } else { - t-=2; - p--; - } - if (!(fl&ALT_FORM)) { - /* Count trailing zeros in last place */ - if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++); - else j=9; - if ((t|32)=='f') - p = MIN(p,MAX(0,9*(z-r-1)-j)); - else - p = MIN(p,MAX(0,9*(z-r-1)+e-j)); - } - } - if (p > INT_MAX-1-(p || (fl&ALT_FORM))) - return -1; - l = 1 + p + (p || (fl&ALT_FORM)); - if ((t|32)=='f') { - if (e > INT_MAX-l) return -1; - if (e>0) l+=e; - } else { - estr=fmt_u(e<0 ? -e : e, ebuf); - while(ebuf-estr<2) *--estr='0'; - *--estr = (e<0 ? '-' : '+'); - *--estr = t; - if (ebuf-estr > INT_MAX-l) return -1; - l += ebuf-estr; - } - - if (l > INT_MAX-pl) return -1; - pad(f, ' ', w, pl+l, fl); - out(f, prefix, pl); - pad(f, '0', w, pl+l, fl^ZERO_PAD); - - if ((t|32)=='f') { - if (a>r) a=r; - for (d=a; d<=r; d++) { - char *s = fmt_u(*d, buf+9); - if (d!=a) while (s>buf) *--s='0'; - else if (s==buf+9) *--s='0'; - out(f, s, buf+9-s); - } - if (p || (fl&ALT_FORM)) out(f, ".", 1); - for (; d0; d++, p-=9) { - char *s = fmt_u(*d, buf+9); - while (s>buf) *--s='0'; - out(f, s, MIN(9,p)); - } - pad(f, '0', p+9, 9, 0); - } else { - if (z<=a) z=a+1; - for (d=a; d=0; d++) { - char *s = fmt_u(*d, buf+9); - if (s==buf+9) *--s='0'; - if (d!=a) while (s>buf) *--s='0'; - else { - out(f, s++, 1); - if (p>0||(fl&ALT_FORM)) out(f, ".", 1); - } - out(f, s, MIN(buf+9-s, p)); - p -= buf+9-s; - } - pad(f, '0', p+18, 18, 0); - out(f, estr, ebuf-estr); - } - - pad(f, ' ', w, pl+l, fl^LEFT_ADJ); - - return MAX(w, pl+l); -} - -static int getint(char **s) { - int i; - for (i=0; isdigit(**s); (*s)++) { - if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1; - else i = 10*i + (**s-'0'); - } - return i; -} - -static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type, char nl_arg_filled) -{ - char *a, *z, *s=(char *)fmt; - unsigned l10n=0, fl; - int w, p, xp; - union arg arg; - int argpos; - unsigned st, ps; - int cnt=0, l=0; - size_t i; - const char *prefix; - int t, pl; - wchar_t wc[2], *ws; - char mb[4]; - - for (;;) { - /* This error is only specified for snprintf, but since it's - * unspecified for other forms, do the same. Stop immediately - * on overflow; otherwise %n could produce wrong results. */ - if (l > INT_MAX - cnt) goto overflow; - - /* Update output count, end loop when fmt is exhausted */ - cnt += l; - if (!*s) break; - - /* Handle literal text and %% format specifiers */ - for (a=s; *s && *s!='%'; s++); - for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2); - if (z-a > INT_MAX-cnt) goto overflow; - l = z-a; - if (f) out(f, a, l); - if (l) continue; - - if (isdigit(s[1]) && s[2]=='$') { - if (!nl_arg_filled) { - va_list ap_copy; - va_copy(ap_copy, *ap); - if (printf_core(0, fmt, &ap_copy, nl_arg, nl_type, 1) < 0) { - return -1; - } - va_end(ap_copy); - } - l10n=1; - argpos = s[1]-'0'; - s+=3; - } else { - argpos = -1; - s++; - } - - /* Read modifier flags */ - for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++) - fl |= 1U<<*s-' '; - - /* Read field width */ - if (*s=='*') { - if (isdigit(s[1]) && s[2]=='$') { - l10n=1; - nl_type[s[1]-'0'] = INT; - w = nl_arg[s[1]-'0'].i; - s+=3; - } else if (!l10n) { - w = f ? va_arg(*ap, int) : 0; - s++; - } else goto inval; - if (w<0) fl|=LEFT_ADJ, w=-w; - } else if ((w=getint(&s))<0) goto overflow; - - /* Read precision */ - if (*s=='.' && s[1]=='*') { - if (isdigit(s[2]) && s[3]=='$') { - nl_type[s[2]-'0'] = INT; - p = nl_arg[s[2]-'0'].i; - s+=4; - } else if (!l10n) { - p = f ? va_arg(*ap, int) : 0; - s+=2; - } else goto inval; - xp = (p>=0); - } else if (*s=='.') { - s++; - p = getint(&s); - xp = 1; - } else { - p = -1; - xp = 0; - } - - /* Format specifier state machine */ - st=0; - do { - if (OOB(*s)) goto inval; - ps=st; - st=states[st]S(*s++); - } while (st-1=0) goto inval; - } else { - if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos]; - else if (f) pop_arg(&arg, st, ap); - else return 0; - } - - if (!f) continue; - - char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4]; - z = buf + sizeof(buf); - prefix = "-+ 0X0x"; - pl = 0; - t = s[-1]; - - /* Transform ls,lc -> S,C */ - if (ps && (t&15)==3) t&=~32; - - /* - and 0 flags are mutually exclusive */ - if (fl & LEFT_ADJ) fl &= ~ZERO_PAD; - - switch(t) { - case 'n': - switch(ps) { - case BARE: *(int *)arg.p = cnt; break; - case LPRE: *(long *)arg.p = cnt; break; - case LLPRE: *(long long *)arg.p = cnt; break; - case HPRE: *(unsigned short *)arg.p = cnt; break; - case HHPRE: *(unsigned char *)arg.p = cnt; break; - case ZTPRE: *(size_t *)arg.p = cnt; break; - case JPRE: *(uintmax_t *)arg.p = cnt; break; - } - continue; - case 'p': - p = MAX(p, 2*sizeof(void*)); - t = 'x'; - fl |= ALT_FORM; - case 'x': case 'X': - a = fmt_x(arg.i, z, t&32); - if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2; - if (0) { - case 'o': - a = fmt_o(arg.i, z); - if ((fl&ALT_FORM) && pINTMAX_MAX) { - arg.i=-arg.i; - } else if (fl & MARK_POS) { - prefix++; - } else if (fl & PAD_POS) { - prefix+=2; - } else pl=0; - case 'u': - a = fmt_u(arg.i, z); - } - if (xp && p<0) goto overflow; - if (xp) fl &= ~ZERO_PAD; - if (!arg.i && !p) { - a=z; - break; - } - p = MAX(p, z-a + !arg.i); - break; - case 'c': - *(a=z-(p=1))=arg.i; - fl &= ~ZERO_PAD; - break; - case 'm': - if (1) a = strerror(errno); else - case 's': - a = arg.p ? arg.p : "(null)"; - z = a + strnlen(a, p<0 ? INT_MAX : p); - if (p<0 && *z) goto overflow; - p = z-a; - fl &= ~ZERO_PAD; - break; - case 'C': - wc[0] = arg.i; - wc[1] = 0; - arg.p = wc; - p = -1; - case 'S': - ws = arg.p; - for (i=l=0; i

=0 && l<=p-i; i+=l); - if (l<0) return -1; - if (i > INT_MAX) goto overflow; - p = i; - pad(f, ' ', w, p, fl); - ws = arg.p; - for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l) - out(f, mb, l); - pad(f, ' ', w, p, fl^LEFT_ADJ); - l = w>p ? w : p; - continue; - case 'e': case 'f': case 'g': case 'a': - case 'E': case 'F': case 'G': case 'A': - if (xp && p<0) goto overflow; - l = fmt_fp(f, arg.f, w, p, fl, t); - if (l<0) goto overflow; - continue; - } - - if (p < z-a) p = z-a; - if (p > INT_MAX-pl) goto overflow; - if (w < pl+p) w = pl+p; - if (w > INT_MAX-cnt) goto overflow; - - pad(f, ' ', w, pl+p, fl); - out(f, prefix, pl); - pad(f, '0', w, pl+p, fl^ZERO_PAD); - pad(f, '0', p, z-a, 0); - out(f, a, z-a); - pad(f, ' ', w, pl+p, fl^LEFT_ADJ); - - l = w; - } - - if (f) return cnt; - if (!l10n) return 0; - - for (i=1; i<=NL_ARGMAX && nl_type[i]; i++) - pop_arg(nl_arg+i, nl_type[i], ap); - for (; i<=NL_ARGMAX && !nl_type[i]; i++); - if (i<=NL_ARGMAX) goto inval; - return 1; - -inval: - errno = EINVAL; - return -1; -overflow: - errno = EOVERFLOW; - return -1; -} - -int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) -{ - va_list ap2; - int nl_type[NL_ARGMAX+1] = {0}; - union arg nl_arg[NL_ARGMAX+1]; - unsigned char internal_buf[80], *saved_buf = 0; - int olderr; - int ret; - - /* the copy allows passing va_list* even if va_list is an array */ - va_copy(ap2, ap); - - FLOCK(f); - olderr = f->flags & F_ERR; - if (f->mode < 1) f->flags &= ~F_ERR; - - if (!f->buf_size && f->buf != NULL) { - saved_buf = f->buf; - f->buf = internal_buf; - f->buf_size = sizeof internal_buf; - f->wpos = f->wbase = f->wend = 0; - } - if (!f->wend && __towrite(f)) ret = -1; - else ret = printf_core(f, fmt, &ap2, nl_arg, nl_type, 0); - if (saved_buf) { - if (!(f->flags & F_PBUF)) { - f->write(f, 0, 0); - } else { - *saved_buf = '\0'; - } - if (!f->wpos) ret = -1; - f->buf = saved_buf; - f->buf_size = 0; - f->wpos = f->wbase = f->wend = 0; - } - else - { - if (f->flags & F_PBUF) { - *f->wpos = '\0'; - } - } - - if (f->flags & F_ERR) ret = -1; - f->flags |= olderr; - FUNLOCK(f); - va_end(ap2); - return ret; -} diff --git a/porting/linux/user/src/stdio/vfscanf.c b/porting/linux/user/src/stdio/vfscanf.c deleted file mode 100644 index 67b48eea4..000000000 --- a/porting/linux/user/src/stdio/vfscanf.c +++ /dev/null @@ -1,371 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "stdio_impl.h" -#include "shgetc.h" -#include "intscan.h" -#include "floatscan.h" - -#define SIZE_hh -2 -#define SIZE_h -1 -#define SIZE_def 0 -#define SIZE_l 1 -#define SIZE_L 2 -#define SIZE_ll 3 -#define BUF_LEN 513 - -#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH)) -extern int parsefloat(FILE *f, char *buf, char *end); -#endif - -static void store_int(void *dest, int size, unsigned long long i) -{ - if (!dest) return; - switch (size) { - case SIZE_hh: - *(char *)dest = i; - break; - case SIZE_h: - *(short *)dest = i; - break; - case SIZE_def: - *(int *)dest = i; - break; - case SIZE_l: - *(long *)dest = i; - break; - case SIZE_ll: - *(long long *)dest = i; - break; - } -} - -static void *arg_n(va_list ap, unsigned int n) -{ - void *p; - unsigned int i; - va_list ap2; - va_copy(ap2, ap); - for (i=n; i>1; i--) va_arg(ap2, void *); - p = va_arg(ap2, void *); - va_end(ap2); - return p; -} - -int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap) -{ -#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH)) - char buf[BUF_LEN]; - char *endptr; -#endif - int width; - int size; - int alloc = 0; - int base; - const unsigned char *p; - int c, t; - char *s; - wchar_t *wcs; - mbstate_t st; - void *dest=NULL; - int invert; - int matches=0; - unsigned long long x; - long double y; - off_t pos = 0; - unsigned char scanset[257]; - size_t i, k; - wchar_t wc; - - FLOCK(f); - - if (!f->rpos) __toread(f); - if (!f->rpos) goto input_fail; - - for (p=(const unsigned char *)fmt; *p; p++) { - - alloc = 0; - - if (isspace(*p)) { - while (isspace(p[1])) p++; - shlim(f, 0); - while (isspace(shgetc(f))); - shunget(f); - pos += shcnt(f); - continue; - } - if (*p != '%' || p[1] == '%') { - shlim(f, 0); - if (*p == '%') { - p++; - while (isspace((c=shgetc(f)))); - } else { - c = shgetc(f); - } - if (c!=*p) { - shunget(f); - if (c<0) goto input_fail; - goto match_fail; - } - pos += shcnt(f); - continue; - } - - p++; - if (*p=='*') { - dest = 0; p++; - } else if (isdigit(*p) && p[1]=='$') { - dest = arg_n(ap, *p-'0'); p+=2; - } else { - dest = va_arg(ap, void *); - } - - for (width=0; isdigit(*p); p++) { - width = 10*width + *p - '0'; - } - - if (*p=='m') { - wcs = 0; - s = 0; - alloc = !!dest; - p++; - } else { - alloc = 0; - } - - size = SIZE_def; - switch (*p++) { - case 'h': - if (*p == 'h') p++, size = SIZE_hh; - else size = SIZE_h; - break; - case 'l': - if (*p == 'l') p++, size = SIZE_ll; - else size = SIZE_l; - break; - case 'j': - size = SIZE_ll; - break; - case 'z': - case 't': - size = SIZE_l; - break; - case 'L': - size = SIZE_L; - break; - case 'd': case 'i': case 'o': case 'u': case 'x': - case 'a': case 'e': case 'f': case 'g': - case 'A': case 'E': case 'F': case 'G': case 'X': - case 's': case 'c': case '[': - case 'S': case 'C': - case 'p': case 'n': - p--; - break; - default: - goto fmt_fail; - } - - t = *p; - - /* C or S */ - if ((t&0x2f) == 3) { - t |= 32; - size = SIZE_l; - } - - switch (t) { - case 'c': - if (width < 1) width = 1; - case '[': - break; - case 'n': - store_int(dest, size, pos); - /* do not increment match count, etc! */ - continue; - default: - shlim(f, 0); - while (isspace(shgetc(f))); - shunget(f); - pos += shcnt(f); - } - - shlim(f, width); - if (shgetc(f) < 0) goto input_fail; - shunget(f); - - switch (t) { - case 's': - case 'c': - case '[': - if (t == 'c' || t == 's') { - memset(scanset, -1, sizeof scanset); - scanset[0] = 0; - if (t == 's') { - scanset[1+'\t'] = 0; - scanset[1+'\n'] = 0; - scanset[1+'\v'] = 0; - scanset[1+'\f'] = 0; - scanset[1+'\r'] = 0; - scanset[1+' '] = 0; - } - } else { - if (*++p == '^') p++, invert = 1; - else invert = 0; - memset(scanset, invert, sizeof scanset); - scanset[0] = 0; - if (*p == '-') p++, scanset[1+'-'] = 1-invert; - else if (*p == ']') p++, scanset[1+']'] = 1-invert; - for (; *p != ']'; p++) { - if (!*p) goto fmt_fail; - if (*p=='-' && p[1] && p[1] != ']') - for (c=p++[-1]; c<*p; c++) - scanset[1+c] = 1-invert; - scanset[1+*p] = 1-invert; - } - } - wcs = 0; - s = 0; - i = 0; - k = t=='c' ? width+1U : 31; - if (size == SIZE_l) { - if (alloc) { - wcs = malloc(k*sizeof(wchar_t)); - if (!wcs) goto alloc_fail; - } else { - wcs = dest; - } - st = (mbstate_t){0}; - while (scanset[(c=shgetc(f))+1]) { - switch (mbrtowc(&wc, &(char){c}, 1, &st)) { - case -1: - goto input_fail; - case -2: - continue; - } - if (wcs) wcs[i++] = wc; - if (alloc && i==k) { - k+=k+1; - wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t)); - if (!tmp) goto alloc_fail; - wcs = tmp; - } - } - if (!mbsinit(&st)) goto input_fail; - } else if (alloc) { - s = malloc(k); - if (!s) goto alloc_fail; - while (scanset[(c=shgetc(f))+1]) { - s[i++] = c; - if (i==k) { - k+=k+1; - char *tmp = realloc(s, k); - if (!tmp) goto alloc_fail; - s = tmp; - } - } - } else if ((s = dest)) { - while (scanset[(c=shgetc(f))+1]) - s[i++] = c; - } else { - while (scanset[(c=shgetc(f))+1]); - } - shunget(f); - if (!shcnt(f)) goto match_fail; - if (t == 'c' && shcnt(f) != width) goto match_fail; - if (alloc) { - if (size == SIZE_l) *(wchar_t **)dest = wcs; - else *(char **)dest = s; - } - if (t != 'c') { - if (wcs) wcs[i] = 0; - if (s) s[i] = 0; - } - break; - case 'p': - case 'X': - case 'x': - base = 16; - goto int_common; - case 'o': - base = 8; - goto int_common; - case 'd': - case 'u': - base = 10; - goto int_common; - case 'i': - base = 0; - int_common: - x = __intscan(f, base, 0, ULLONG_MAX); - if (!shcnt(f)) goto match_fail; - if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x; - else store_int(dest, size, x); - break; - case 'a': case 'A': - case 'e': case 'E': - case 'f': case 'F': - case 'g': case 'G': -#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH)) - if (width == 0 || width > sizeof(buf) - 1) - width = sizeof(buf) - 1; - int count = parsefloat(f, buf, buf + width); - if (count == 0) - goto match_fail; - if (dest) switch (size) { - case SIZE_def: - y = strtof(buf, &endptr); - *(float *)dest = y; - break; - case SIZE_l: - y = strtod(buf, &endptr); - *(double *)dest = y; - break; - case SIZE_L: - y = strtold(buf, &endptr); - *(long double *)dest = y; - break; - } - break; - } -#else - y = __floatscan(f, size, 0); - if (!shcnt(f)) goto match_fail; - if (dest) switch (size) { - case SIZE_def: - *(float *)dest = y; - break; - case SIZE_l: - *(double *)dest = y; - break; - case SIZE_L: - *(long double *)dest = y; - break; - } - break; - } -#endif - pos += shcnt(f); - if (dest) matches++; - } - if (0) { -fmt_fail: -alloc_fail: -input_fail: - if (!matches) matches--; -match_fail: - if (alloc) { - free(s); - free(wcs); - } - } - FUNLOCK(f); - return matches; -} - -weak_alias(vfscanf,__isoc99_vfscanf); diff --git a/porting/linux/user/src/stdio/vsnprintf.c b/porting/linux/user/src/stdio/vsnprintf.c deleted file mode 100644 index 0e7d7f365..000000000 --- a/porting/linux/user/src/stdio/vsnprintf.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "stdio_impl.h" -#include -#include -#include -#include - -struct cookie { - char *s; - size_t n; -}; - -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -static size_t sn_write(FILE *f, const unsigned char *s, size_t l) -{ - struct cookie *c = f->cookie; - size_t already_size = f->wpos - f->wbase; - if (already_size <= c->n) { - size_t k = MIN(l, c->n - already_size); - memcpy(f->wpos, s, k); - f->wpos += k; - } - /* pretend to succeed, even if we discarded extra data */ - return l; -} - -int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list ap) -{ - unsigned char dummy[1]; - struct cookie c = { .s = n ? s : dummy, .n = n ? n-1 : 0 }; - FILE f = { - .lbf = EOF, - .lock = -1, - .buf = (unsigned char *)(n ? s: dummy), - .buf_size = n ? n - 1 : 0, - .flags = F_PBUF, - .cookie = &c, - .write = sn_write, - }; - - if (n > INT_MAX) { - errno = EOVERFLOW; - return -1; - } - - return vfprintf(&f, fmt, ap); -} diff --git a/porting/linux/user/src/stdio/vsscanf.c b/porting/linux/user/src/stdio/vsscanf.c deleted file mode 100644 index f0c4a7d01..000000000 --- a/porting/linux/user/src/stdio/vsscanf.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "stdio_impl.h" -#include - -// Empty implementation because vfscanf operates rpos directly. -static size_t string_read(FILE *f, unsigned char *buf, size_t len) -{ - return 0; -} - -int vsscanf(const char *restrict s, const char *restrict fmt, va_list ap) -{ - size_t s_len = strlen(s); - FILE f = { - .buf = (void *)s, .cookie = (void *)s, - .read = string_read, .lock = -1, - .buf_size = s_len, .rpos = s, .rend = s + s_len, - }; - return vfscanf(&f, fmt, ap); -} - -weak_alias(vsscanf,__isoc99_vsscanf); diff --git a/porting/linux/user/src/stdlib/strtod.c b/porting/linux/user/src/stdlib/strtod.c deleted file mode 100644 index b4af23353..000000000 --- a/porting/linux/user/src/stdlib/strtod.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include "shgetc.h" -#include "floatscan.h" -#include "stdio_impl.h" - - -static long double strtox(const char *s, char **p, int prec) -{ - FILE f; - sh_fromstring(&f, s); - shlim(&f, 0); - long double y = __floatscan(&f, prec, 1); - off_t cnt = shcnt(&f); - if (p) *p = cnt ? (char *)s + cnt : (char *)s; - return y; -} -#if (!defined(MUSL_AARCH64_ARCH)) && (!defined(MUSL_ARM_ARCH)) -float strtof(const char *restrict s, char **restrict p) -{ - return strtox(s, p, 0); -} - -double strtod(const char *restrict s, char **restrict p) -{ - return strtox(s, p, 1); -} -#endif - -long double strtold(const char *restrict s, char **restrict p) -{ - return strtox(s, p, 2); -} diff --git a/porting/linux/user/src/stdlib/strtol.c b/porting/linux/user/src/stdlib/strtol.c deleted file mode 100644 index 900e957ab..000000000 --- a/porting/linux/user/src/stdlib/strtol.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "stdio_impl.h" -#include "intscan.h" -#include "shgetc.h" -#include -#include -#include - -static unsigned long long strtox(const char *s, char **p, int base, unsigned long long lim) -{ - FILE f; - sh_fromstring(&f, s); - shlim(&f, 0); - unsigned long long y = __intscan(&f, base, 1, lim); - if (p) { - size_t cnt = shcnt(&f); - *p = (char *)s + cnt; - } - return y; -} - -unsigned long long strtoull(const char *restrict s, char **restrict p, int base) -{ - return strtox(s, p, base, ULLONG_MAX); -} - -long long strtoll(const char *restrict s, char **restrict p, int base) -{ - return strtox(s, p, base, LLONG_MIN); -} - -unsigned long strtoul_weak(const char *restrict s, char **restrict p, int base) -{ - return strtox(s, p, base, ULONG_MAX); -} - -long strtol(const char *restrict s, char **restrict p, int base) -{ - return strtox(s, p, base, 0UL+LONG_MIN); -} - -intmax_t strtoimax_weak(const char *restrict s, char **restrict p, int base) -{ - return strtoll(s, p, base); -} - -uintmax_t strtoumax_weak(const char *restrict s, char **restrict p, int base) -{ - return strtoull(s, p, base); -} - -weak_alias(strtol, __strtol_internal); -weak_alias(strtoul_weak, strtoul); -weak_alias(strtoul, __strtoul_internal); -weak_alias(strtoll, __strtoll_internal); -weak_alias(strtoull, __strtoull_internal); -weak_alias(strtoimax_weak, strtoimax); -weak_alias(strtoumax_weak, strtoumax); -weak_alias(strtoimax, __strtoimax_internal); -weak_alias(strtoumax, __strtoumax_internal); diff --git a/porting/linux/user/src/string/stpncpy.c b/porting/linux/user/src/string/stpncpy.c deleted file mode 100644 index 63dcfd9b0..000000000 --- a/porting/linux/user/src/string/stpncpy.c +++ /dev/null @@ -1,20 +0,0 @@ -#include - -char *__stpncpy(char *dst, const char *src, size_t n) -{ - char *d = dst, *s = src; - dst = &dst[n]; - while (n > 0){ - if((*d++ = *src++) == 0){ - dst = d - 1; - while(--n > 0){ - *d++ = 0; - } - break; - } - n--; - } - return (dst); -} - -weak_alias(__stpncpy, stpncpy); diff --git a/porting/linux/user/src/string/strcasecmp.c b/porting/linux/user/src/string/strcasecmp.c deleted file mode 100644 index 92c2c89f5..000000000 --- a/porting/linux/user/src/string/strcasecmp.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -int __strcasecmp(const char *_l, const char *_r) -{ - const unsigned char *l=(void *)_l, *r=(void *)_r; - for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++); - return tolower(*l) - tolower(*r); -} -weak_alias(__strcasecmp, strcasecmp); - -int __strcasecmp_l(const char *l, const char *r, locale_t loc) -{ - return strcasecmp(l, r); -} - -weak_alias(__strcasecmp_l, strcasecmp_l); \ No newline at end of file diff --git a/porting/linux/user/src/string/strcspn.c b/porting/linux/user/src/string/strcspn.c deleted file mode 100644 index 23b84cdf5..000000000 --- a/porting/linux/user/src/string/strcspn.c +++ /dev/null @@ -1,20 +0,0 @@ -// from openbsd, will be rectified later - -#include - -size_t strcspn(const char *string1, const char *string2) -{ - const char *a, *p; - char sc, c; - - for (a = string1;;) { - sc = *a++; - p = string2; - do { - if ((c = *p++) == sc) { - return (a - 1 - string1); - } - } while (c != 0); - } - /* NOTREACHED */ -} \ No newline at end of file diff --git a/porting/linux/user/src/string/strncasecmp.c b/porting/linux/user/src/string/strncasecmp.c deleted file mode 100644 index 03d13a970..000000000 --- a/porting/linux/user/src/string/strncasecmp.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -int __strncasecmp(const char *_l, const char *_r, size_t n) -{ - const unsigned char *l=(void *)_l, *r=(void *)_r; - if (!n--) return 0; - for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--); - return tolower(*l) - tolower(*r); -} -weak_alias(__strncasecmp, strncasecmp); - -int __strncasecmp_l(const char *l, const char *r, size_t n, locale_t loc) -{ - return strncasecmp(l, r, n); -} - -weak_alias(__strncasecmp_l, strncasecmp_l); \ No newline at end of file diff --git a/porting/linux/user/src/string/strncat.c b/porting/linux/user/src/string/strncat.c deleted file mode 100644 index 01fc0a022..000000000 --- a/porting/linux/user/src/string/strncat.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -char *strncat(char *restrict d, const char *restrict s, size_t n) -{ - char *a = d; - while (*d != 0) d++; - while (n && *s) n--, *d++ = *s++; - *d++ = 0; - return a; -} diff --git a/porting/linux/user/src/string/strncpy.c b/porting/linux/user/src/string/strncpy.c deleted file mode 100644 index 54838a6e8..000000000 --- a/porting/linux/user/src/string/strncpy.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -char *strncpy(char *dst, const char *src, size_t n) -{ - char *d = dst, *s = src; - while (n > 0){ - if((*d = *s) != 0){ - s++; - } - d++; - n--; - } - return (dst); -} \ No newline at end of file diff --git a/porting/linux/user/src/thread/arm/clone.s b/porting/linux/user/src/thread/arm/clone.s deleted file mode 100644 index 2070dd51b..000000000 --- a/porting/linux/user/src/thread/arm/clone.s +++ /dev/null @@ -1,36 +0,0 @@ -.syntax unified -.text -.global __clone -.hidden __clone -.type __clone,%function -.cfi_startproc -__clone: - stmfd sp!,{r4,r5,r6,r7} - .cfi_def_cfa_offset 16 - .cfi_rel_offset r4, 0 - .cfi_rel_offset r5, 4 - .cfi_rel_offset r6, 8 - .cfi_rel_offset r7, 12 - mov r7,#120 - mov r6,r3 - mov r5,r0 - mov r0,r2 - and r1,r1,#-16 - ldr r2,[sp,#16] - ldr r3,[sp,#20] - ldr r4,[sp,#24] - svc 0 - tst r0,r0 - beq 1f - ldmfd sp!,{r4,r5,r6,r7} - .cfi_def_cfa_offset 0 - bx lr - -1: mov r0,r6 - bl 3f -2: mov r7,#1 - svc 0 - b 2b - -3: bx r5 -.cfi_endproc \ No newline at end of file diff --git a/porting/linux/user/src/thread/arm/syscall_cp.s b/porting/linux/user/src/thread/arm/syscall_cp.s deleted file mode 100644 index 03a83dc95..000000000 --- a/porting/linux/user/src/thread/arm/syscall_cp.s +++ /dev/null @@ -1,37 +0,0 @@ -.syntax unified -.global __cp_begin -.hidden __cp_begin -.global __cp_end -.hidden __cp_end -.global __cp_cancel -.hidden __cp_cancel -.hidden __cancel -.global __syscall_cp_asm -.type __syscall_cp_asm,%function -.cfi_startproc -__syscall_cp_asm: - mov ip,sp - stmfd sp!,{r4,r5,r6,r7} - .cfi_def_cfa_offset 16 - .cfi_rel_offset r4, 0 - .cfi_rel_offset r5, 4 - .cfi_rel_offset r6, 8 - .cfi_rel_offset r7, 12 -__cp_begin: - ldr r0,[r0] - cmp r0,#0 - bne __cp_cancel - mov r7,r1 - mov r0,r2 - mov r1,r3 - ldmfd ip,{r2,r3,r4,r5,r6} - svc 0 -__cp_end: - ldmfd sp!,{r4,r5,r6,r7} - .cfi_def_cfa_offset 0 - bx lr -__cp_cancel: - ldmfd sp!,{r4,r5,r6,r7} - .cfi_def_cfa_offset 0 - b __cancel -.cfi_endproc diff --git a/porting/linux/user/src/thread/pthread_cancel.c b/porting/linux/user/src/thread/pthread_cancel.c deleted file mode 100644 index f9e5c6113..000000000 --- a/porting/linux/user/src/thread/pthread_cancel.c +++ /dev/null @@ -1,111 +0,0 @@ -#define _GNU_SOURCE -#include -#include "pthread_impl.h" -#include "syscall.h" - -#ifdef FEATURE_PTHREAD_CANCEL -hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c(); - -long __cancel() -{ - pthread_t self = __pthread_self(); - if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync) - pthread_exit(PTHREAD_CANCELED); - self->canceldisable = PTHREAD_CANCEL_DISABLE; - return -ECANCELED; -} - -long __syscall_cp_asm(volatile void *, syscall_arg_t, - syscall_arg_t, syscall_arg_t, syscall_arg_t, - syscall_arg_t, syscall_arg_t, syscall_arg_t); - -long __syscall_cp_c(syscall_arg_t nr, - syscall_arg_t u, syscall_arg_t v, syscall_arg_t w, - syscall_arg_t x, syscall_arg_t y, syscall_arg_t z) -{ - pthread_t self; - long r; - int st; - - if ((st=(self=__pthread_self())->canceldisable) - && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close)) - return __syscall(nr, u, v, w, x, y, z); - - r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z); - if (r==-EINTR && nr!=SYS_close && self->cancel && - self->canceldisable != PTHREAD_CANCEL_DISABLE) - r = __cancel(); - return r; -} - -static void _sigaddset(sigset_t *set, int sig) -{ - unsigned s = sig-1; - set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1); -} - -extern hidden const char __cp_begin[1], __cp_end[1], __cp_cancel[1]; - -static void cancel_handler(int sig, siginfo_t *si, void *ctx) -{ - pthread_t self = __pthread_self(); - ucontext_t *uc = ctx; - uintptr_t pc = uc->uc_mcontext.MC_PC; - - a_barrier(); - if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return; - - _sigaddset(&uc->uc_sigmask, SIGCANCEL); - - if (self->cancelasync || pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) { - uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel; -#ifdef CANCEL_GOT - uc->uc_mcontext.MC_GOT = CANCEL_GOT; -#endif - return; - } - - __syscall(SYS_tkill, self->tid, SIGCANCEL); -} - -void __testcancel() -{ - pthread_t self = __pthread_self(); - if (self->cancel && !self->canceldisable) - __cancel(); -} - -static void init_cancellation() -{ - struct sigaction sa = { - .sa_flags = SA_SIGINFO | SA_RESTART, - .sa_sigaction = cancel_handler - }; - memset(&sa.sa_mask, -1, _NSIG/8); - __libc_sigaction(SIGCANCEL, &sa, 0); -} - -int pthread_cancel(pthread_t t) -{ - static int init; - if (!init) { - init_cancellation(); - init = 1; - } - a_store(&t->cancel, 1); - if (t == pthread_self()) { - if (t->canceldisable == PTHREAD_CANCEL_ENABLE && t->cancelasync) - pthread_exit(PTHREAD_CANCELED); - return 0; - } - return pthread_kill(t, SIGCANCEL); -} -#else -hidden long __cancel() -{ -} - -int pthread_cancel(pthread_t t) -{ -} -#endif diff --git a/porting/linux/user/src/thread/pthread_cond_init.c b/porting/linux/user/src/thread/pthread_cond_init.c deleted file mode 100644 index bd2fc6a14..000000000 --- a/porting/linux/user/src/thread/pthread_cond_init.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "pthread_impl.h" - -int pthread_cond_init(pthread_cond_t *restrict c, const pthread_condattr_t *restrict a) -{ - __builtin_memset(c, 0, sizeof(pthread_cond_t)); - if (a) { - c->_c_clock = a->__attr & 0x7fffffff; - if (a->__attr>>31) c->_c_shared = (void *)-1; - } - return 0; -} diff --git a/porting/linux/user/src/thread/pthread_cond_timedwait.c b/porting/linux/user/src/thread/pthread_cond_timedwait.c deleted file mode 100644 index 4ef63ebdf..000000000 --- a/porting/linux/user/src/thread/pthread_cond_timedwait.c +++ /dev/null @@ -1,222 +0,0 @@ -#include "pthread_impl.h" - -/* - * struct waiter - * - * Waiter objects have automatic storage on the waiting thread, and - * are used in building a linked list representing waiters currently - * waiting on the condition variable or a group of waiters woken - * together by a broadcast or signal; in the case of signal, this is a - * degenerate list of one member. - * - * Waiter lists attached to the condition variable itself are - * protected by the lock on the cv. Detached waiter lists are never - * modified again, but can only be traversed in reverse order, and are - * protected by the "barrier" locks in each node, which are unlocked - * in turn to control wake order. - * - * Since process-shared cond var semantics do not necessarily allow - * one thread to see another's automatic storage (they may be in - * different processes), the waiter list is not used for the - * process-shared case, but the structure is still used to store data - * needed by the cancellation cleanup handler. - */ - -struct waiter { - struct waiter *prev, *next; - volatile int state, barrier; - volatile int *notify; -}; - -/* Self-synchronized-destruction-safe lock functions */ - -static inline void lock(volatile int *l) -{ - if (a_cas(l, 0, 1)) { - a_cas(l, 1, 2); - do __wait(l, 0, 2, 1); - while (a_cas(l, 0, 2)); - } -} - -static inline void unlock(volatile int *l) -{ - if (a_swap(l, 0)==2) - __wake(l, 1, 1); -} - -static inline void unlock_requeue(volatile int *l, volatile int *r, int w) -{ - a_store(l, 0); - if (w) __wake(l, 1, 1); - else __syscall(SYS_futex, l, FUTEX_REQUEUE|FUTEX_PRIVATE, 0, 1, r) != -ENOSYS - || __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r); -} - -enum { - WAITING, - SIGNALED, - LEAVING, -}; - -int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts) -{ - struct waiter node = { 0 }; - int e, seq, clock = c->_c_clock, cs, shared=0, oldstate, tmp; - volatile int *fut; - - if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid) - return EPERM; - - if (ts && ts->tv_nsec >= 1000000000UL) - return EINVAL; - - __pthread_testcancel(); - - if (c->_c_shared) { - shared = 1; - fut = &c->_c_seq; - seq = c->_c_seq; - a_inc(&c->_c_waiters); - } else { - lock(&c->_c_lock); - - seq = node.barrier = 2; - fut = &node.barrier; - node.state = WAITING; - node.next = c->_c_head; - c->_c_head = &node; - if (!c->_c_tail) c->_c_tail = &node; - else node.next->prev = &node; - - unlock(&c->_c_lock); - } - - __pthread_mutex_unlock(m); - - __pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs); - if (cs == PTHREAD_CANCEL_DISABLE) __pthread_setcancelstate(cs, 0); - - do e = __timedwait_cp(fut, seq, clock, ts, !shared); - while (*fut==seq && (!e || e==EINTR)); - if (e == EINTR) e = 0; - - if (shared) { - /* Suppress cancellation if a signal was potentially - * consumed; this is a legitimate form of spurious - * wake even if not. */ - if (e == ECANCELED && c->_c_seq != seq) e = 0; - if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff) - __wake(&c->_c_waiters, 1, 0); - oldstate = WAITING; - goto relock; - } - - oldstate = a_cas(&node.state, WAITING, LEAVING); - - if (oldstate == WAITING) { - /* Access to cv object is valid because this waiter was not - * yet signaled and a new signal/broadcast cannot return - * after seeing a LEAVING waiter without getting notified - * via the futex notify below. */ - - lock(&c->_c_lock); - - if (c->_c_head == &node) c->_c_head = node.next; - else if (node.prev) node.prev->next = node.next; - if (c->_c_tail == &node) c->_c_tail = node.prev; - else if (node.next) node.next->prev = node.prev; - - unlock(&c->_c_lock); - - if (node.notify) { - if (a_fetch_add(node.notify, -1)==1) - __wake(node.notify, 1, 1); - } - } else { - /* Lock barrier first to control wake order. */ - lock(&node.barrier); - } - -relock: - /* Errors locking the mutex override any existing error or - * cancellation, since the caller must see them to know the - * state of the mutex. */ - if ((tmp = pthread_mutex_lock(m))) e = tmp; - - if (oldstate == WAITING) goto done; - - if (!node.next && !(m->_m_type & 8)) - a_inc(&m->_m_waiters); - - /* Unlock the barrier that's holding back the next waiter, and - * either wake it or requeue it to the mutex. */ - if (node.prev) { - int val = m->_m_lock; - if (val>0) a_cas(&m->_m_lock, val, val|0x80000000); - unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & (8|128)); - } else if (!(m->_m_type & 8)) { - a_dec(&m->_m_waiters); - } - - /* Since a signal was consumed, cancellation is not permitted. */ - if (e == ECANCELED) e = 0; - -done: - __pthread_setcancelstate(cs, 0); - - if (e == ECANCELED) { - __pthread_testcancel(); - __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0); - } - - return e; -} - -int __private_cond_signal(pthread_cond_t *c, int n) -{ - struct waiter *p, *first=0; - volatile int ref = 0; - int cur; - #ifdef a_ll_p - if (a_ll_p(&c->_c_tail) == NULL) { - /* Wait for any waiters in the LEAVING state to remove - * themselves from the list before returning or allowing - * signaled threads to proceed. */ - while ((cur = ref)) __wait(&ref, 0, cur, 1); - - return 0; - } - #endif - lock(&c->_c_lock); - for (p=c->_c_tail; n && p; p=p->prev) { - if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) { - ref++; - p->notify = &ref; - } else { - n--; - if (!first) first=p; - } - } - /* Split the list, leaving any remainder on the cv. */ - if (p) { - if (p->next) p->next->prev = 0; - p->next = 0; - } else { - c->_c_head = 0; - } - c->_c_tail = p; - unlock(&c->_c_lock); - - /* Wait for any waiters in the LEAVING state to remove - * themselves from the list before returning or allowing - * signaled threads to proceed. */ - while ((cur = ref)) __wait(&ref, 0, cur, 1); - - /* Allow first signaled waiter, if any, to proceed. */ - if (first) unlock(&first->barrier); - - return 0; -} - -weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait); diff --git a/porting/linux/user/src/thread/pthread_create.c b/porting/linux/user/src/thread/pthread_create.c deleted file mode 100644 index c0fc725f5..000000000 --- a/porting/linux/user/src/thread/pthread_create.c +++ /dev/null @@ -1,562 +0,0 @@ -#define _GNU_SOURCE -#define ANON_STACK_NAME_SIZE 50 -#include "musl_log.h" -#include "pthread_impl.h" -#include "stdio_impl.h" -#include "libc.h" -#include "lock.h" -#include -#include -#include -#include -#include - -pid_t getpid(void); - -void log_print(const char* info,...) -{ - va_list ap; - va_start(ap, info); - vfprintf(stdout,info, ap); - va_end(ap); -} - -void stack_naming(struct pthread *new){ - size_t size_len; - unsigned char *start_addr; - char name[ANON_STACK_NAME_SIZE]; - if (new->guard_size) { - snprintf(name, ANON_STACK_NAME_SIZE, "guard:%d", new->tid); - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, new->map_base, new->guard_size, name); - start_addr = new->map_base + new->guard_size; - size_len = new->map_size - new->guard_size; - memset(name, 0, ANON_STACK_NAME_SIZE); - } else { - start_addr = new->map_base; - size_len = new->map_size; - } - snprintf(name, ANON_STACK_NAME_SIZE, "stack:%d", new->tid); - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start_addr, size_len, name); -}; - -#ifdef RESERVE_SIGNAL_STACK -#if defined (__LP64__) -#define RESERVE_SIGNAL_STACK_SIZE (32 * 1024) -#else -#define RESERVE_SIGNAL_STACK_SIZE (20 * 1024) -#endif -void __pthread_reserve_signal_stack() -{ - void* stack = mmap(NULL, RESERVE_SIGNAL_STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0); - if (stack != MAP_FAILED) { - if (mprotect(stack, __default_guardsize, PROT_NONE) == -1) { - munmap(stack, RESERVE_SIGNAL_STACK_SIZE); - return; - } - } - - stack_t signal_stack; - signal_stack.ss_sp = (uint8_t*)stack + __default_guardsize; - signal_stack.ss_size = RESERVE_SIGNAL_STACK_SIZE - __default_guardsize; - signal_stack.ss_flags = 0; - sigaltstack(&signal_stack, NULL); - - pthread_t self = __pthread_self(); - self->signal_stack = stack; - char name[ANON_STACK_NAME_SIZE]; - snprintf(name, ANON_STACK_NAME_SIZE, "signal_stack:%d", __pthread_self()->tid); - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, signal_stack.ss_sp, signal_stack.ss_size, name); - return; -} - -void __pthread_release_signal_stack() -{ - pthread_t self = __pthread_self(); - if (self->signal_stack == NULL) { - return; - } - - stack_t signal_stack, old_stack; - memset(&signal_stack, 0, sizeof(signal_stack)); - signal_stack.ss_flags = SS_DISABLE; - sigaltstack(&signal_stack, &old_stack); - munmap(self->signal_stack, __default_guardsize); - if (old_stack.ss_flags != SS_DISABLE) { - munmap(old_stack.ss_sp, old_stack.ss_size); - } - self->signal_stack = NULL; -} - -weak_alias(__pthread_reserve_signal_stack, pthread_reserve_signal_stack); -weak_alias(__pthread_release_signal_stack, pthread_release_signal_stack); -#endif - -static void dummy_0() -{ -} -weak_alias(dummy_0, __acquire_ptc); -weak_alias(dummy_0, __release_ptc); -weak_alias(dummy_0, __pthread_tsd_run_dtors); -weak_alias(dummy_0, __do_orphaned_stdio_locks); -weak_alias(dummy_0, __dl_thread_cleanup); -weak_alias(dummy_0, __membarrier_init); - -static int tl_lock_count; -static int tl_lock_waiters; - -void __tl_lock(void) -{ - int tid = __pthread_self()->tid; - int val = __thread_list_lock; - if (val == tid) { - tl_lock_count++; - return; - } - while ((val = a_cas(&__thread_list_lock, 0, tid))) - __wait(&__thread_list_lock, &tl_lock_waiters, val, 0); -} - -void __tl_unlock(void) -{ - if (tl_lock_count) { - tl_lock_count--; - return; - } - a_store(&__thread_list_lock, 0); - if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0); -} - -void __tl_sync(pthread_t td) -{ - a_barrier(); - int val = __thread_list_lock; - if (!val) return; - __wait(&__thread_list_lock, &tl_lock_waiters, val, 0); - if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0); -} - -#ifdef ENABLE_HWASAN -weak void __hwasan_thread_enter(); -weak void __hwasan_thread_exit(); - -__attribute__((no_sanitize("hwaddress"))) -#endif -_Noreturn void __pthread_exit(void *result) -{ - pthread_t self = __pthread_self(); - sigset_t set; - -#ifdef FEATURE_PTHREAD_CANCEL - self->canceldisable = 1; - self->cancelasync = 0; -#endif - self->result = result; - - while (self->cancelbuf) { - void (*f)(void *) = self->cancelbuf->__f; - void *x = self->cancelbuf->__x; - self->cancelbuf = self->cancelbuf->__next; - f(x); - } - - __pthread_tsd_run_dtors(); - - __block_app_sigs(&set); - - /* This atomic potentially competes with a concurrent pthread_detach - * call; the loser is responsible for freeing thread resources. */ - int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING); - - if (state==DT_DETACHED && self->map_base) { - /* Since __unmapself bypasses the normal munmap code path, - * explicitly wait for vmlock holders first. This must be - * done before any locks are taken, to avoid lock ordering - * issues that could lead to deadlock. */ - __vm_wait(); - } - - /* Access to target the exiting thread with syscalls that use - * its kernel tid is controlled by killlock. For detached threads, - * any use past this point would have undefined behavior, but for - * joinable threads it's a valid usage that must be handled. - * Signals must be blocked since pthread_kill must be AS-safe. */ - LOCK(self->killlock); - - /* The thread list lock must be AS-safe, and thus depends on - * application signals being blocked above. */ - __tl_lock(); - -#ifdef RESERVE_SIGNAL_STACK - __pthread_release_signal_stack(); -#endif - /* If this is the only thread in the list, don't proceed with - * termination of the thread, but restore the previous lock and - * signal state to prepare for exit to call atexit handlers. */ - if (self->next == self) { - __tl_unlock(); - UNLOCK(self->killlock); - self->detach_state = state; - __restore_sigs(&set); -#ifdef ENABLE_HWASAN - __hwasan_thread_exit(); -#endif - exit(0); - } - - /* At this point we are committed to thread termination. */ - - /* Process robust list in userspace to handle non-pshared mutexes - * and the detached thread case where the robust list head will - * be invalid when the kernel would process it. */ - __vm_lock(); - volatile void *volatile *rp; - while ((rp=self->robust_list.head) && rp != &self->robust_list.head) { - pthread_mutex_t *m = (void *)((char *)rp - - offsetof(pthread_mutex_t, _m_next)); - int waiters = m->_m_waiters; - int priv = (m->_m_type & 128) ^ 128; - self->robust_list.pending = rp; - self->robust_list.head = *rp; - int cont = a_swap(&m->_m_lock, 0x40000000); - self->robust_list.pending = 0; - if (cont < 0 || waiters) - __wake(&m->_m_lock, 1, priv); - } - __vm_unlock(); - - __do_orphaned_stdio_locks(); - __dl_thread_cleanup(); - - /* Last, unlink thread from the list. This change will not be visible - * until the lock is released, which only happens after SYS_exit - * has been called, via the exit futex address pointing at the lock. - * This needs to happen after any possible calls to LOCK() that might - * skip locking if process appears single-threaded. */ - if (!--libc.threads_minus_1) libc.need_locks = -1; - self->next->prev = self->prev; - self->prev->next = self->next; - self->prev = self->next = self; - - if (state==DT_DETACHED && self->map_base) { - /* Detached threads must block even implementation-internal - * signals, since they will not have a stack in their last - * moments of existence. */ - __block_all_sigs(&set); - - /* Robust list will no longer be valid, and was already - * processed above, so unregister it with the kernel. */ - if (self->robust_list.off) - __syscall(SYS_set_robust_list, 0, 3*sizeof(long)); - - /* The following call unmaps the thread's stack mapping - * and then exits without touching the stack. */ - __unmapself(self->map_base, self->map_size); - } - - /* Wake any joiner. */ - a_store(&self->detach_state, DT_EXITED); - __wake(&self->detach_state, 1, 1); - - /* After the kernel thread exits, its tid may be reused. Clear it - * to prevent inadvertent use and inform functions that would use - * it that it's no longer available. */ - self->tid = 0; - UNLOCK(self->killlock); - -#ifdef ENABLE_HWASAN - __hwasan_thread_exit(); -#endif - - for (;;) __syscall(SYS_exit, 0); -} - -void __do_cleanup_push(struct __ptcb *cb) -{ - struct pthread *self = __pthread_self(); - cb->__next = self->cancelbuf; - self->cancelbuf = cb; -} - -void __do_cleanup_pop(struct __ptcb *cb) -{ - __pthread_self()->cancelbuf = cb->__next; -} - -struct start_args { - void *(*start_func)(void *); - void *start_arg; - volatile int control; - unsigned long sig_mask[_NSIG/8/sizeof(long)]; -}; - -#ifdef ENABLE_HWASAN -__attribute__((no_sanitize("hwaddress"))) -#endif -static int start(void *p) -{ -#ifdef ENABLE_HWASAN - __hwasan_thread_enter(); -#endif - struct start_args *args = p; - int state = args->control; - if (state) { - if (a_cas(&args->control, 1, 2)==1) - __wait(&args->control, 0, 2, 1); - if (args->control) { - __syscall(SYS_set_tid_address, &args->control); - for (;;) __syscall(SYS_exit, 0); - } - } - __syscall(SYS_rt_sigprocmask, SIG_SETMASK, &args->sig_mask, 0, _NSIG/8); -#ifdef RESERVE_SIGNAL_STACK - __pthread_reserve_signal_stack(); -#endif - __pthread_exit(args->start_func(args->start_arg)); - return 0; -} - -#ifdef ENABLE_HWASAN -__attribute__((no_sanitize("hwaddress"))) -#endif -static int start_c11(void *p) -{ -#ifdef RESERVE_SIGNAL_STACK - __pthread_reserve_signal_stack(); -#endif -#ifdef ENABLE_HWASAN - __hwasan_thread_enter(); -#endif - struct start_args *args = p; - int (*start)(void*) = (int(*)(void*)) args->start_func; - __pthread_exit((void *)(uintptr_t)start(args->start_arg)); - return 0; -} - -#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE) - -/* pthread_key_create.c overrides this */ -static volatile size_t dummy = 0; -weak_alias(dummy, __pthread_tsd_size); -static void *dummy_tsd[1] = { 0 }; -weak_alias(dummy_tsd, __pthread_tsd_main); - -static FILE *volatile dummy_file = 0; -weak_alias(dummy_file, __stdin_used); -weak_alias(dummy_file, __stdout_used); -weak_alias(dummy_file, __stderr_used); - -static void init_file_lock(FILE *f) -{ - if (f && f->lock<0) f->lock = 0; -} - -#ifdef ENABLE_HWASAN -__attribute__((no_sanitize("hwaddress"))) -#endif -int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg) -{ - int ret, c11 = (attrp == __ATTRP_C11_THREAD); - size_t size, guard, size_len; - struct pthread *self, *new; - unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit, *start_addr; - unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND - | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS - | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED; - pthread_attr_t attr = { 0 }; - sigset_t set; - - if (!libc.can_do_threads) { - MUSL_LOGE("pthread_create: can't do threads, err: %{public}s", strerror(errno)); - return ENOSYS; - } - self = __pthread_self(); - if (!libc.threaded) { - for (FILE *f=*__ofl_lock(); f; f=f->next) - init_file_lock(f); - __ofl_unlock(); - init_file_lock(__stdin_used); - init_file_lock(__stdout_used); - init_file_lock(__stderr_used); - __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8); - self->tsd = (void **)__pthread_tsd_main; - __membarrier_init(); - libc.threaded = 1; - } - if (attrp && !c11) attr = *attrp; - - __acquire_ptc(); - if (!attrp || c11) { - attr._a_stacksize = __default_stacksize; - attr._a_guardsize = __default_guardsize; - } - - if (attr._a_stackaddr) { - size_t need = libc.tls_size + __pthread_tsd_size; - size = attr._a_stacksize; - stack = (void *)(attr._a_stackaddr & -16); - stack_limit = (void *)(attr._a_stackaddr - size); - /* Use application-provided stack for TLS only when - * it does not take more than ~12% or 2k of the - * application's stack space. */ - if (need < size/8 && need < 2048) { - tsd = stack - __pthread_tsd_size; - stack = tsd - libc.tls_size; - memset(stack, 0, need); - } else { - size = ROUND(need); - } - guard = 0; - } else { - guard = ROUND(attr._a_guardsize); - size = guard + ROUND(attr._a_stacksize - + libc.tls_size + __pthread_tsd_size); - } - - if (!tsd) { - if (guard) { - map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (map == MAP_FAILED) { - MUSL_LOGE("pthread_create: mmap PROT_NONE failed, err:%{public}s", strerror(errno)); - goto fail; - } - if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE) - && errno != ENOSYS) { - MUSL_LOGE("pthread_create: mprotect failed, err:%{public}s", strerror(errno)); - __munmap(map, size); - goto fail; - } - } else { - map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (map == MAP_FAILED) { - MUSL_LOGE("pthread_create: mmap PROT_READ|PROT_WRITE failed, err:%{public}s", strerror(errno)); - goto fail; - } - } - tsd = map + size - __pthread_tsd_size; - if (!stack) { - stack = tsd - libc.tls_size; - stack_limit = map + guard; - } - } - - new = __copy_tls(tsd - libc.tls_size); - new->map_base = map; - new->map_size = size; - new->stack = stack; - new->stack_size = stack - stack_limit; - new->guard_size = guard; - new->self = new; - new->pid = getpid(); - new->proc_tid = -1; - new->tsd = (void *)tsd; - new->locale = &libc.global_locale; - if (attr._a_detach) { - new->detach_state = DT_DETACHED; - } else { - new->detach_state = DT_JOINABLE; - } - new->robust_list.head = &new->robust_list.head; - new->canary = self->canary; - new->sysinfo = self->sysinfo; - - /* Setup argument structure for the new thread on its stack. - * It's safe to access from the caller only until the thread - * list is unlocked. */ - stack -= (uintptr_t)stack % sizeof(uintptr_t); - stack -= sizeof(struct start_args); - struct start_args *args = (void *)stack; - args->start_func = entry; - args->start_arg = arg; - args->control = attr._a_sched ? 1 : 0; - - /* Application signals (but not the synccall signal) must be - * blocked before the thread list lock can be taken, to ensure - * that the lock is AS-safe. */ - __block_app_sigs(&set); - - /* Ensure SIGCANCEL is unblocked in new thread. This requires - * working with a copy of the set so we can restore the - * original mask in the calling thread. */ - memcpy(&args->sig_mask, &set, sizeof args->sig_mask); - args->sig_mask[(SIGCANCEL-1)/8/sizeof(long)] &= - ~(1UL<<((SIGCANCEL-1)%(8*sizeof(long)))); - - __tl_lock(); - if (!libc.threads_minus_1++) libc.need_locks = 1; - ret = __clone((c11 ? start_c11 : start), stack, flags, args, &new->tid, TP_ADJ(new), &__thread_list_lock); - - /* All clone failures translate to EAGAIN. If explicit scheduling - * was requested, attempt it before unlocking the thread list so - * that the failed thread is never exposed and so that we can - * clean up all transient resource usage before returning. */ - if (ret < 0) { - ret = -EAGAIN; - } else if (attr._a_sched) { - ret = __syscall(SYS_sched_setscheduler, - new->tid, attr._a_policy, &attr._a_prio); - if (a_swap(&args->control, ret ? 3 : 0)==2) - __wake(&args->control, 1, 1); - if (ret) - __wait(&args->control, 0, 3, 0); - } - - if (ret >= 0) { - stack_naming(new); - - new->next = self->next; - new->prev = self; - new->next->prev = new; - new->prev->next = new; - } else { - if (!--libc.threads_minus_1) libc.need_locks = 0; - } - __tl_unlock(); - __restore_sigs(&set); - __release_ptc(); - - if (ret < 0) { - if (map) __munmap(map, size); - MUSL_LOGE("pthread_create: ret:%{public}d, err:%{public}s", ret, strerror(errno)); - return -ret; - } - - *res = new; - return 0; -fail: - __release_ptc(); - return EAGAIN; -} - -weak_alias(__pthread_exit, pthread_exit); -weak_alias(__pthread_create, pthread_create); - -struct pthread* __pthread_list_find(pthread_t thread_id, const char* info) -{ - struct pthread *thread = (struct pthread *)thread_id; - if (NULL == thread) { - log_print("invalid pthread_t (0) passed to %s\n", info); - return NULL; - } - - struct pthread *self = __pthread_self(); - if (thread == self) { - return thread; - } - struct pthread *t = self; - t = t->next ; - while (t != self) { - if (t == thread) return thread; - t = t->next ; - } - log_print("invalid pthread_t %p passed to %s\n", thread, info); - return NULL; -} - -pid_t __pthread_gettid_np(pthread_t t) -{ - __tl_lock(); - struct pthread* thread = __pthread_list_find(t, "pthread_gettid_np"); - __tl_unlock(); - return thread ? thread->tid : -1; -} -weak_alias(__pthread_gettid_np, pthread_gettid_np); diff --git a/porting/linux/user/src/thread/pthread_kill.c b/porting/linux/user/src/thread/pthread_kill.c deleted file mode 100644 index 9770ea66d..000000000 --- a/porting/linux/user/src/thread/pthread_kill.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include "pthread_impl.h" -#include "lock.h" - -int pthread_kill(pthread_t t, int sig) -{ - int r; - sigset_t set; - /* Block not just app signals, but internal ones too, since - * pthread_kill is used to implement pthread_cancel, which - * must be async-cancel-safe. */ - __block_all_sigs(&set); - LOCK(t->killlock); - r = t->tid ? -__syscall(__NR_tgkill, getpid(), t->tid, sig) - : (sig+0U >= _NSIG ? EINVAL : 0); - UNLOCK(t->killlock); - __restore_sigs(&set); - return r; -} diff --git a/porting/linux/user/src/thread/pthread_mutex_init.c b/porting/linux/user/src/thread/pthread_mutex_init.c deleted file mode 100644 index f606428db..000000000 --- a/porting/linux/user/src/thread/pthread_mutex_init.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "pthread_impl.h" - -int pthread_mutex_init(pthread_mutex_t *restrict m, const pthread_mutexattr_t *restrict a) -{ - __builtin_memset(m, 0, sizeof(pthread_mutex_t)); - if (a) m->_m_type = a->__attr; - return 0; -} diff --git a/porting/linux/user/src/thread/pthread_mutex_lock.c b/porting/linux/user/src/thread/pthread_mutex_lock.c deleted file mode 100644 index 4049a8733..000000000 --- a/porting/linux/user/src/thread/pthread_mutex_lock.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "pthread_impl.h" -extern int __pthread_mutex_timedlock_inner(pthread_mutex_t *restrict m, const struct timespec *restrict at); -int __pthread_mutex_lock(pthread_mutex_t *m) -{ - if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL - && !a_cas(&m->_m_lock, 0, EBUSY)) - return 0; - - return __pthread_mutex_timedlock_inner(m, 0); -} - -weak_alias(__pthread_mutex_lock, pthread_mutex_lock); diff --git a/porting/linux/user/src/thread/pthread_mutex_timedlock.c b/porting/linux/user/src/thread/pthread_mutex_timedlock.c deleted file mode 100644 index c71f7fc06..000000000 --- a/porting/linux/user/src/thread/pthread_mutex_timedlock.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "pthread_impl.h" - -#define IS32BIT(x) !((x)+0x80000000ULL>>32) -#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) - -static int __futex4(volatile void *addr, int op, int val, const struct timespec *to) -{ -#ifdef SYS_futex_time64 - time_t s = to ? to->tv_sec : 0; - long ns = to ? to->tv_nsec : 0; - int r = -ENOSYS; - if (SYS_futex == SYS_futex_time64 || !IS32BIT(s)) - r = __syscall(SYS_futex_time64, addr, op, val, - to ? ((long long[]){s, ns}) : 0); - if (SYS_futex == SYS_futex_time64 || r!=-ENOSYS) return r; - to = to ? (void *)(long[]){CLAMP(s), ns} : 0; -#endif - return __syscall(SYS_futex, addr, op, val, to); -} - -static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct timespec *restrict at) -{ - int clock = m->_m_clock; - int type = m->_m_type; - int priv = (type & 128) ^ 128; - pthread_t self = __pthread_self(); - int e; - - if (!priv) self->robust_list.pending = &m->_m_next; - - do e = -__futex4(&m->_m_lock, FUTEX_LOCK_PI|priv, 0, at); - while (e==EINTR); - if (e) self->robust_list.pending = 0; - - switch (e) { - case 0: - /* Catch spurious success for non-robust mutexes. */ - if (!(type&4) && ((m->_m_lock & 0x40000000) || m->_m_waiters)) { - a_store(&m->_m_waiters, -1); - __syscall(SYS_futex, &m->_m_lock, FUTEX_UNLOCK_PI|priv); - self->robust_list.pending = 0; - break; - } - /* Signal to trylock that we already have the lock. */ - m->_m_count = -1; - return __pthread_mutex_trylock_owner(m); - case ETIMEDOUT: - return e; - case EDEADLK: - if ((type&3) == PTHREAD_MUTEX_ERRORCHECK) return e; - } - do e = __timedwait(&(int){0}, 0, clock, at, 1); - while (e != ETIMEDOUT); - return e; -} - -int __pthread_mutex_timedlock_inner(pthread_mutex_t *restrict m, const struct timespec *restrict at) -{ - int type = m->_m_type; - int r; - // PI - if (type&8) { - r = __pthread_mutex_trylock_owner(m); - if (r != EBUSY) return r; - return pthread_mutex_timedlock_pi(m, at); - } - r = __pthread_mutex_trylock(m); - if (r != EBUSY) return r; - int clock = (m->_m_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME; - int t, priv = (type & 128) ^ 128; - int spins = 100; - while (spins-- && m->_m_lock && !m->_m_waiters) a_spin(); - - while ((r=__pthread_mutex_trylock(m)) == EBUSY) { - r = m->_m_lock; - int own = r & 0x3fffffff; - if (!own && (!r || (type&4))) - continue; - if ((type&3) == PTHREAD_MUTEX_ERRORCHECK - && own == __pthread_self()->tid) - return EDEADLK; - - a_inc(&m->_m_waiters); - t = r | 0x80000000; - a_cas(&m->_m_lock, r, t); - r = __timedwait(&m->_m_lock, t, clock, at, priv); - a_dec(&m->_m_waiters); - if (r && r != EINTR) break; - } - return r; -} - -int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at) -{ - if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL - && !a_cas(&m->_m_lock, 0, EBUSY)) - return 0; - return __pthread_mutex_timedlock_inner(m, at); -} - -weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock); \ No newline at end of file diff --git a/porting/linux/user/src/thread/pthread_once.c b/porting/linux/user/src/thread/pthread_once.c deleted file mode 100644 index 69f77f186..000000000 --- a/porting/linux/user/src/thread/pthread_once.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "pthread_impl.h" - -static void undo(void *control) -{ - /* Wake all waiters, since the waiter status is lost when - * resetting control to the initial state. */ - if (a_swap(control, 0) == 3) - __wake(control, -1, 1); -} - -hidden int __pthread_once_full(pthread_once_t *control, void (*init)(void)) -{ - /* Try to enter initializing state. Four possibilities: - * 0 - we're the first or the other cancelled; run init - * 1 - another thread is running init; wait - * 2 - another thread finished running init; just return - * 3 - another thread is running init, waiters present; wait */ - - for (;;) switch (a_cas(control, 0, 1)) { - case 0: - pthread_cleanup_push(undo, control); - init(); - pthread_cleanup_pop(0); - - if (a_swap(control, 2) == 3) - __wake(control, -1, 1); - return 0; - case 1: - /* If this fails, so will __wait. */ - a_cas(control, 1, 3); - case 3: - __wait(control, 0, 3, 1); - continue; - case 2: - return 0; - } -} - -#if defined(MUSL_AARCH64_ARCH) -int __pthread_once(pthread_once_t *control, void (*init)(void)) -{ - /* Return immediately if init finished before, use load aquire to ensure that - * effects of the init routine are visible to the caller. */ - if (a_ldar((volatile int *)control) == 2) { - return 0; - } - return __pthread_once_full(control, init); -} -#else -int __pthread_once(pthread_once_t *control, void (*init)(void)) -{ - /* Return immediately if init finished before, but ensure that - * effects of the init routine are visible to the caller. */ - if (*(volatile int *)control == 2) { - a_barrier(); - return 0; - } - return __pthread_once_full(control, init); -} -#endif - -weak_alias(__pthread_once, pthread_once); diff --git a/porting/linux/user/src/thread/pthread_rwlock_init.c b/porting/linux/user/src/thread/pthread_rwlock_init.c deleted file mode 100644 index 27cf8723e..000000000 --- a/porting/linux/user/src/thread/pthread_rwlock_init.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "pthread_impl.h" - -int pthread_rwlock_init(pthread_rwlock_t *restrict rw, const pthread_rwlockattr_t *restrict a) -{ - __builtin_memset(rw, 0, sizeof(pthread_rwlock_t)); - if (a) rw->_rw_shared = a->__attr[0]*128; - return 0; -} diff --git a/porting/linux/user/src/thread/pthread_rwlock_timedrdlock.c b/porting/linux/user/src/thread/pthread_rwlock_timedrdlock.c deleted file mode 100644 index f4adab2f9..000000000 --- a/porting/linux/user/src/thread/pthread_rwlock_timedrdlock.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "pthread_impl.h" - -int __pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at) -{ - int r, t; - int clock = (rw->_rw_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME; - - r = pthread_rwlock_tryrdlock(rw); - if (r != EBUSY) return r; - - int spins = 100; - while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin(); - - while ((r=__pthread_rwlock_tryrdlock(rw))==EBUSY) { - if (!(r=rw->_rw_lock) || (r&0x7fffffff)!=0x7fffffff) continue; - t = r | 0x80000000; - a_inc(&rw->_rw_waiters); - a_cas(&rw->_rw_lock, r, t); - r = __timedwait(&rw->_rw_lock, t, clock, at, rw->_rw_shared^128); - a_dec(&rw->_rw_waiters); - if (r && r != EINTR) return r; - } - return r; -} - -weak_alias(__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock); \ No newline at end of file diff --git a/porting/linux/user/src/thread/pthread_rwlock_timedwrlock.c b/porting/linux/user/src/thread/pthread_rwlock_timedwrlock.c deleted file mode 100644 index fead64916..000000000 --- a/porting/linux/user/src/thread/pthread_rwlock_timedwrlock.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "pthread_impl.h" - -int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at) -{ - if (rw == NULL) { - return EINVAL; - } - int r, t; - int clock = (rw->_rw_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME; - - r = pthread_rwlock_trywrlock(rw); - if (r != EBUSY) return r; - - int spins = 100; - while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin(); - - while ((r=__pthread_rwlock_trywrlock(rw))==EBUSY) { - if (!(r=rw->_rw_lock)) continue; - t = r | 0x80000000; - a_inc(&rw->_rw_waiters); - a_cas(&rw->_rw_lock, r, t); - r = __timedwait(&rw->_rw_lock, t, clock, at, rw->_rw_shared^128); - a_dec(&rw->_rw_waiters); - if (r && r != EINTR) return r; - } - return r; -} - -weak_alias(__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock); diff --git a/porting/linux/user/src/thread/pthread_setcancelstate.c b/porting/linux/user/src/thread/pthread_setcancelstate.c deleted file mode 100644 index bbb22abdd..000000000 --- a/porting/linux/user/src/thread/pthread_setcancelstate.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "pthread_impl.h" - -int __pthread_setcancelstate(int new, int *old) -{ -#ifdef FEATURE_PTHREAD_CANCEL - if (new > 2U) return EINVAL; - struct pthread *self = __pthread_self(); - if (old) *old = self->canceldisable; - self->canceldisable = new; - return 0; -#endif -} - -weak_alias(__pthread_setcancelstate, pthread_setcancelstate); diff --git a/porting/linux/user/src/thread/pthread_setcanceltype.c b/porting/linux/user/src/thread/pthread_setcanceltype.c deleted file mode 100644 index 851485869..000000000 --- a/porting/linux/user/src/thread/pthread_setcanceltype.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "pthread_impl.h" - -int pthread_setcanceltype(int new, int *old) -{ -#ifdef FEATURE_PTHREAD_CANCEL - struct pthread *self = __pthread_self(); - if (new > 1U) return EINVAL; - if (old) *old = self->cancelasync; - self->cancelasync = new; - if (new) pthread_testcancel(); - return 0; -#endif -} diff --git a/porting/linux/user/src/thread/pthread_sigmask.c b/porting/linux/user/src/thread/pthread_sigmask.c deleted file mode 100644 index dc3be454e..000000000 --- a/porting/linux/user/src/thread/pthread_sigmask.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#ifdef OHOS_ENABLE_PARAMETER -#include "sys_param.h" -#endif - -extern void intercept_pthread_sigmask(int how, sigset_t *restrict set); -static const char *param_name = "musl.sigchain.procmask"; - -/** - * @brief Get whether sigchain mask is enabled - * @retval True if the sigchain mask is enable, or false. - */ -bool get_sigchain_mask_enable() -{ -#ifdef OHOS_ENABLE_PARAMETER - static CachedHandle sigchain_procmask_handle = NULL; - if (sigchain_procmask_handle == NULL) { - sigchain_procmask_handle = CachedParameterCreate(param_name, "false"); - } - char *param_value = CachedParameterGet(sigchain_procmask_handle); - if (param_value != NULL) { - if (strcmp(param_value, "true") == 0) { - return true; - } - } -#endif - return false; -} - -int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict old) -{ - int ret; - if (set && (unsigned)how - SIG_BLOCK > 2U) return EINVAL; - /* sigchain intercepts pthread_sigmask */ - if (set && get_sigchain_mask_enable()) { - sigset_t tmpset = *set; - intercept_pthread_sigmask(how, &tmpset); - const sigset_t *new_set_ptr = &tmpset; - ret = -__syscall(SYS_rt_sigprocmask, how, new_set_ptr, old, _NSIG/8); - } else { - ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8); - } - - if (!ret && old) { - if (sizeof old->__bits[0] == 8) { - old->__bits[0] &= ~0x380000000ULL; - } else { - old->__bits[0] &= ~0x80000000UL; - old->__bits[1] &= ~0x3UL; - } - } - return ret; -} diff --git a/porting/linux/user/src/thread/sem_timedwait.c b/porting/linux/user/src/thread/sem_timedwait.c deleted file mode 100644 index a17dd96bd..000000000 --- a/porting/linux/user/src/thread/sem_timedwait.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include "pthread_impl.h" - -static void cleanup(void *p) -{ - a_dec(p); -} -int __sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at) -{ - int spins = 100; - while (spins-- && sem->__val[0] <= 0 && !sem->__val[1]) a_spin(); - - while (sem_trywait(sem)) { - int r; - a_inc(sem->__val+1); - a_cas(sem->__val, 0, -1); - pthread_cleanup_push(cleanup, (void *)(sem->__val+1)); - r = __timedwait_cp(sem->__val, -1, CLOCK_REALTIME, at, sem->__val[2]); - pthread_cleanup_pop(1); - if (r) { - errno = r; - return -1; - } - } - return 0; -} -int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at) -{ - pthread_testcancel(); - - if (!sem_trywait(sem)) return 0; - - return __sem_timedwait(sem, at); -} diff --git a/porting/linux/user/src/time/__tz.c b/porting/linux/user/src/time/__tz.c deleted file mode 100755 index a20e1b67d..000000000 --- a/porting/linux/user/src/time/__tz.c +++ /dev/null @@ -1,527 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include "libc.h" -#include "lock.h" -#include "fork_impl.h" -#include "time_impl.h" -#ifdef OHOS_ENABLE_PARAMETER -#include "sys_param.h" -#define SYSPARAM_LENGTH 40 -#endif - -#define malloc __libc_malloc -#define calloc undef -#define realloc undef -#define free undef -#define __TZ_VERSION__ '2' - -long __timezone = 0; -int __daylight = 0; -char *__tzname[2] = { 0, 0 }; - -weak_alias(__timezone, timezone); -weak_alias(__daylight, daylight); -weak_alias(__tzname, tzname); - -static char std_name[TZNAME_MAX+1]; -static char dst_name[TZNAME_MAX+1]; -const char __utc[] = "UTC"; -const char __gmt[] = "GMT"; - -static int dst_off; -static int r0[5], r1[5]; - -static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end; -static size_t map_size; - -static char old_tz_buf[32]; -static char *old_tz = old_tz_buf; -static size_t old_tz_size = sizeof old_tz_buf; - -static volatile int lock[1]; -volatile int *const __timezone_lockptr = lock; - -static int getint(const char **p) -{ - unsigned x; - for (x=0; **p-'0'<10U; (*p)++) { - x = **p-'0' + 10*x; - } - return x; -} - -static int getoff(const char **p) -{ - int neg = 0; - if (**p == '-') { - ++*p; - neg = 1; - } else if (**p == '+') { - ++*p; - } - int off = 3600*getint(p); - if (**p == ':') { - ++*p; - off += 60*getint(p); - if (**p == ':') { - ++*p; - off += getint(p); - } - } - return neg ? -off : off; -} - -static void getrule(const char **p, int rule[5]) -{ - int r = rule[0] = **p; - - if (r!='M') { - if (r=='J') { - ++*p; - } else { - rule[0] = 0; - } - rule[1] = getint(p); - } else { - ++*p; - rule[1] = getint(p); - ++*p; - rule[2] = getint(p); - ++*p; - rule[3] = getint(p); - } - - if (**p=='/') { - ++*p; - rule[4] = getoff(p); - } else { - rule[4] = 7200; - } -} - -static void getname(char *d, const char **p) -{ - int i; - if (**p == '<') { - ++*p; - for (i=0; (*p)[i] && (*p)[i]!='>'; i++) - if (i PATH_MAX+1) s = __utc, i = 3; - if (i >= old_tz_size) { - old_tz_size *= 2; - if (i >= old_tz_size) old_tz_size = i+1; - if (old_tz_size > PATH_MAX+2) old_tz_size = PATH_MAX+2; - old_tz = malloc(old_tz_size); - } - if (old_tz) memcpy(old_tz, s, i+1); - - int posix_form = 0; - if (*s != ':') { - p = s; - char dummy_name[TZNAME_MAX+1]; - getname(dummy_name, &p); - if (p!=s && (*p == '+' || *p == '-' || isdigit(*p) - || !strcmp(dummy_name, "UTC") - || !strcmp(dummy_name, "GMT"))) - posix_form = 1; - } - /* Non-suid can use an absolute tzfile pathname or a relative - * pathame beginning with "."; in secure mode, only the - * standard path will be searched. */ - int flag = 1; - if (!posix_form) { - if (*s == ':') s++; - if (*s == '/' || *s == '.') { - /* The path is invalid, use the default value. */ - flag = 0; - if (!libc.secure || !strcmp(s, "/etc/localtime")) { - map = __map_file(s, &map_size); - } - } - } - - if (flag) { - /* Adapt to time zone names, such as Asia/Shanghai or Shanghai*/ - size_t l = strlen(s); - if (l <= NAME_MAX && !strchr(s, '.')) { - memcpy(pathname, s, l+1); - pathname[l] = 0; - for (try=search; !map && *try; try+=l+1) { - l = strlen(try); - memcpy(pathname-l, try, l); - map = __map_file(pathname-l, &map_size); - } - } - } - - if (map && (map_size < 44 || memcmp(map, "TZif", 4))) { - __munmap((void *)map, map_size); - map = 0; - s = __utc; - } - - zi = map; - if (map) { - int scale = 2; - - /* - * map[0]-map[3]: magic, it is TZif - * map[4]: version, '\0' or '2' or '3' as of 2013 - * map[5]-map[19]: reserved; must be zero - * map[20]-map[23]: The number of UT/local indicators stored in the file. - * map[24]-map[27]: The number of standard/wall indicators stored in the file. - * map[24]-map[31]: The number of leap seconds for which data entries are stored in the file. - * map[32]-map[35]: The number of transition times for which data entries are stored in the file. - * map[36]-map[39]: The number of local time types for which data entries are - * stored in the file (must not be zero). - * map[40]-map[43]: The number of bytes of time zone abbreviation strings stored in the file. - - * If map[4] is '2' or greater, the above is followed by a second instance - * of tzhead and a second instance of the data in which each coded transition - * time uses 8 rather than 4 chars, - * then a POSIX-TZ-environment-variable-style string for use in handling - * instants after the last transition time stored in the file - * (with nothing between the newlines if there is no POSIX representation for - * such instants). - - * If map[4] is '3' or greater, the above is extended as follows. - * First, the POSIX TZ string's hour offset may range from -167 - * through 167 as compared to the POSIX-required 0 through 24. - * Second, its DST start time may be January 1 at 00:00 and its stop - * time December 31 at 24:00 plus the difference between DST and - * standard time, indicating DST all year. */ - if (map[4]!='1') { - size_t skip = zi_dotprod(zi+20, VEC(1, 1, 8, 5, 6, 1), 6); - trans = zi+skip+44+44; - scale++; - } else { - trans = zi+44; - } - index = trans + (zi_read32(trans-12) << scale); - types = index + zi_read32(trans-12); - abbrevs = types + 6*zi_read32(trans-8); - abbrevs_end = abbrevs + zi_read32(trans-4); - if (zi[map_size-1] == '\n') { - for (s = (const char *)zi+map_size-2; *s!='\n'; s--); - s++; - } else { - const unsigned char *p; - __tzname[0] = __tzname[1] = 0; - __daylight = __timezone = dst_off = 0; - for (p=types; p>scale, m; - - if (!n) { - if (alt) *alt = 0; - return 0; - } - - /* Binary search for 'most-recent rule before t'. */ - while (n > 1) { - m = a + n/2; - x = zi_read32(trans + (m<>scale; - if (a == n-1) return -1; - if (a == 0) { - x = zi_read32(trans); - if (scale == 3) x = (x<<32) | zi_read32(trans + 4); - else x = (int32_t)x; - /* Find the lowest non-DST type, or 0 if none. */ - size_t j = 0; - for (size_t i=abbrevs-types; i; i-=6) { - if (!types[i-6+4]) j = i-6; - } - if (local) off = (int32_t)zi_read32(types + j); - /* If t is before first transition, use the above-found type - * and the index-zero (after transition) type as the alt. */ - if (t - off < (int64_t)x) { - if (alt) *alt = index[0]; - return j/6; - } - } - - /* Try to find a neighboring opposite-DST-status rule. */ - if (alt) { - if (a && types[6*index[a-1]+4] != types[6*index[a]+4]) - *alt = index[a-1]; - else if (a+1>(m-1))&1); -} - -/* Convert a POSIX DST rule plus year to seconds since epoch. */ - -static long long rule_to_secs(const int *rule, int year) -{ - int is_leap; - long long t = __year_to_secs(year, &is_leap); - int x, m, n, d; - if (rule[0]!='M') { - x = rule[1]; - if (rule[0]=='J' && (x < 60 || !is_leap)) x--; - t += 86400 * x; - } else { - m = rule[1]; - n = rule[2]; - d = rule[3]; - t += __month_to_secs(m-1, is_leap); - int wday = (int)((t + 4*86400) % (7*86400)) / 86400; - int days = d - wday; - if (days < 0) days += 7; - if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4; - t += 86400 * (days + 7*(n-1)); - } - t += rule[4]; - return t; -} - -/* Determine the time zone in effect for a given time in seconds since the - * epoch. It can be given in local or universal time. The results will - * indicate whether DST is in effect at the queried time, and will give both - * the GMT offset for the active zone/DST rule and the opposite DST. This - * enables a caller to efficiently adjust for the case where an explicit - * DST specification mismatches what would be in effect at the time. */ - -void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename) -{ - LOCK(lock); - - do_tzset(); - - if (zi) { - size_t alt, i = scan_trans(t, local, &alt); - if (i != -1) { - *isdst = types[6*i+4]; - *offset = (int32_t)zi_read32(types+6*i); - *zonename = (const char *)abbrevs + types[6*i+5]; - if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt); - UNLOCK(lock); - return; - } - } - - if (!__daylight) goto std; - - long long y = t / 31556952 + 70; - while (__year_to_secs(y, 0) > t) y--; - while (__year_to_secs(y+1, 0) < t) y++; - - long long t0 = rule_to_secs(r0, y); - long long t1 = rule_to_secs(r1, y); - - if (!local) { - t0 += __timezone; - t1 += dst_off; - } - if (t0 < t1) { - if (t >= t0 && t < t1) goto dst; - goto std; - } else { - if (t >= t1 && t < t0) goto std; - goto dst; - } -std: - *isdst = 0; - *offset = -__timezone; - if (oppoff) { - *oppoff = -dst_off; - } - *zonename = __tzname[0]; - UNLOCK(lock); - return; -dst: - *isdst = 1; - *offset = -dst_off; - if (oppoff) { - *oppoff = -__timezone; - } - *zonename = __tzname[1]; - UNLOCK(lock); -} - -static void __tzset() -{ - LOCK(lock); - do_tzset(); - UNLOCK(lock); -} - -weak_alias(__tzset, tzset); - -const char *__tm_to_tzname(const struct tm *tm) -{ - const void *p = tm->__tm_zone; - LOCK(lock); - do_tzset(); - if (p != __utc && p != __tzname[0] && p != __tzname[1] && - (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs)) { - p = ""; - } - UNLOCK(lock); - return p; -} diff --git a/porting/linux/user/src/time/clock_getres.c b/porting/linux/user/src/time/clock_getres.c deleted file mode 100644 index d594b6466..000000000 --- a/porting/linux/user/src/time/clock_getres.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include "syscall.h" -#include "atomic.h" - -#ifdef VDSO_CGR_SYM - -static void *volatile vdso_cgr; - -static int cgr_init(clockid_t clk, struct timespec *ts) -{ - __get_vdso_info(); - void *p = __get_vdso_addr(VDSO_CGR_VER, VDSO_CGR_SYM); - int (*f)(clockid_t, struct timespec *) = - (int (*)(clockid_t, struct timespec *))p; - a_cas_p(&vdso_cgr, (void *)cgr_init, p); - return f ? f(clk, ts) : -ENOSYS; -} - -static void *volatile vdso_cgr = (void *)cgr_init; - -#endif - -int clock_getres(clockid_t clk, struct timespec *ts) -{ - int r; -#ifdef VDSO_CGR_SYM - int (*f)(clockid_t, struct timespec *) = - (int (*)(clockid_t, struct timespec *))vdso_cgr; - if (f) { - r = f(clk, ts); - if (!r) return r; - if (r == -EINVAL) return __syscall_ret(r); - } -#endif - -#ifdef SYS_clock_getres_time64 - /* On a 32-bit arch, use the old syscall if it exists. */ - if (SYS_clock_getres != SYS_clock_getres_time64) { - long ts32[2]; - r = __syscall(SYS_clock_getres, clk, ts32); - if (!r && ts) { - ts->tv_sec = ts32[0]; - ts->tv_nsec = ts32[1]; - } - return __syscall_ret(r); - } -#endif - /* If reaching this point, it's a 64-bit arch or time64-only - * 32-bit arch and we can get result directly into timespec. */ - return syscall(SYS_clock_getres, clk, ts); -} diff --git a/porting/linux/user/src/time/clock_gettime.c b/porting/linux/user/src/time/clock_gettime.c deleted file mode 100644 index 684ba2aea..000000000 --- a/porting/linux/user/src/time/clock_gettime.c +++ /dev/null @@ -1,108 +0,0 @@ -#include -#include -#include -#include "syscall.h" -#include "atomic.h" - -#ifdef VDSO_CGT_SYM - -static void *volatile vdso_func; - -#ifdef VDSO_CGT32_SYM -static void *volatile vdso_func_32; -static int cgt_time32_wrap(clockid_t clk, struct timespec *ts) -{ - long ts32[2]; - int (*f)(clockid_t, long[2]) = - (int (*)(clockid_t, long[2]))vdso_func_32; - int r = f(clk, ts32); - if (!r) { - /* Fallback to syscalls if time32 overflowed. Maybe - * we lucked out and somehow migrated to a kernel with - * time64 syscalls available. */ - if (ts32[0] < 0) { - a_cas_p(&vdso_func, (void *)cgt_time32_wrap, 0); - return -ENOSYS; - } - ts->tv_sec = ts32[0]; - ts->tv_nsec = ts32[1]; - } - return r; -} -#endif - -static int cgt_init(clockid_t clk, struct timespec *ts) -{ - __get_vdso_info(); - void *p = __get_vdso_addr(VDSO_CGT_VER, VDSO_CGT_SYM); -#ifdef VDSO_CGT32_SYM - if (!p) { - void *q = __get_vdso_addr(VDSO_CGT32_VER, VDSO_CGT32_SYM); - if (q) { - a_cas_p(&vdso_func_32, 0, q); - p = cgt_time32_wrap; - } - } -#endif - int (*f)(clockid_t, struct timespec *) = - (int (*)(clockid_t, struct timespec *))p; - a_cas_p(&vdso_func, (void *)cgt_init, p); - return f ? f(clk, ts) : -ENOSYS; -} - -static void *volatile vdso_func = (void *)cgt_init; - -#endif - -int __clock_gettime(clockid_t clk, struct timespec *ts) -{ - int r; - -#ifdef VDSO_CGT_SYM - int (*f)(clockid_t, struct timespec *) = - (int (*)(clockid_t, struct timespec *))vdso_func; - if (f) { - r = f(clk, ts); - if (!r) return r; - if (r == -EINVAL) return __syscall_ret(r); - /* Fall through on errors other than EINVAL. Some buggy - * vdso implementations return ENOSYS for clocks they - * can't handle, rather than making the syscall. This - * also handles the case where cgt_init fails to find - * a vdso function to use. */ - } -#endif - -#ifdef SYS_clock_gettime64 - r = -ENOSYS; - if (sizeof(time_t) > 4) - r = __syscall(SYS_clock_gettime64, clk, ts); - if (SYS_clock_gettime == SYS_clock_gettime64 || r!=-ENOSYS) - return __syscall_ret(r); - long ts32[2]; - r = __syscall(SYS_clock_gettime, clk, ts32); - if (r==-ENOSYS && clk==CLOCK_REALTIME) { - r = __syscall(SYS_gettimeofday, ts32, 0); - ts32[1] *= 1000; - } - if (!r) { - ts->tv_sec = ts32[0]; - ts->tv_nsec = ts32[1]; - return r; - } - return __syscall_ret(r); -#else - r = __syscall(SYS_clock_gettime, clk, ts); - if (r == -ENOSYS) { - if (clk == CLOCK_REALTIME) { - __syscall(SYS_gettimeofday, ts, 0); - ts->tv_nsec = (int)ts->tv_nsec * 1000; - return 0; - } - r = -EINVAL; - } - return __syscall_ret(r); -#endif -} - -weak_alias(__clock_gettime, clock_gettime); diff --git a/porting/linux/user/src/time/gettimeofday.c b/porting/linux/user/src/time/gettimeofday.c deleted file mode 100644 index 836655ac3..000000000 --- a/porting/linux/user/src/time/gettimeofday.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include "syscall.h" -#include "atomic.h" - -#ifdef VDSO_GTD_SYM - -static void *volatile vdso_gtd; - -static int gtd_init(struct timeval *tv, void *tz) -{ - __get_vdso_info(); - void *p = __get_vdso_addr(VDSO_GTD_VER, VDSO_GTD_SYM); - int (*f)(struct timeval *, void *) = - (int (*)(struct timval *, void *))p; - a_cas_p(&vdso_gtd, (void *)gtd_init, p); - return f ? f(tv, tz) : -ENOSYS; -} - -static void *volatile vdso_gtd = (void *)gtd_init; - -#endif - -int gettimeofday(struct timeval *restrict tv, void *restrict tz) -{ -#ifdef VDSO_GTD_SYM - int r; - int (*f)(struct timeval *, void *) = - (int (*)(struct timeval *, void *))vdso_gtd; - if (f) { - r = f(tv, tz); - if (!r) return r; - if (r == -EINVAL) return __syscall_ret(r); - } -#endif - - struct timespec ts; - if (!tv) return 0; - clock_gettime(CLOCK_REALTIME, &ts); - tv->tv_sec = ts.tv_sec; - tv->tv_usec = (int)ts.tv_nsec / 1000; - return 0; -} diff --git a/porting/linux/user/src/time/gmtime_r.c b/porting/linux/user/src/time/gmtime_r.c deleted file mode 100755 index fe2eb9437..000000000 --- a/porting/linux/user/src/time/gmtime_r.c +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "time_impl.h" - -struct tm *__gmtime_r(const time_t *restrict t, struct tm *restrict tm) -{ - if (__secs_to_tm(*t, tm) < 0) { - errno = EOVERFLOW; - return 0; - } - tm->tm_isdst = 0; - tm->__tm_gmtoff = 0; - tm->__tm_zone = __gmt; - return tm; -} - -weak_alias(__gmtime_r, gmtime_r); \ No newline at end of file diff --git a/porting/linux/user/src/time/strftime.c b/porting/linux/user/src/time/strftime.c deleted file mode 100644 index 6a0f5bfd7..000000000 --- a/porting/linux/user/src/time/strftime.c +++ /dev/null @@ -1,370 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include "locale_impl.h" -#include "time_impl.h" - -#define __YEAR_BASE__ 1900 -#define __WEEKS_IN_YEAR__ 52 -#define __WEEKS_IN_YEAR2__ 53 -#define __DAY_LAST_WEEK__ 4 -#define __DAY_LAST_WEEK2__ 3 -#define __YEARS_PER_CENTURY__ 100 -#define __DAYS_IN_YEAR__ 360 -#define __LEN_YEAR__ 4 -#define __HALF_HOUR__ 12 -#define __LEN_DAY__ 3 - -static int is_leap(int y) -{ - /* Avoid overflow */ - if (y > INT_MAX - __YEAR_BASE__) { - y -= 2000; - } - y += __YEAR_BASE__; - return !(y%4) && ((y%100) || !(y%400)); -} - -static int week_num(const struct tm *tm) -{ - int val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7; - /* If 1 Jan is just 1-3 days past Monday, - * the previous week is also in this year. */ - if ((tm->tm_wday + 371U - tm->tm_yday - 2) % 7 <= 2) - val++; - if (!val) { - val = __WEEKS_IN_YEAR__; - /* If 31 December of prev year a Thursday, - * or Friday of a leap year, then the - * prev year has 53 weeks. */ - int dec31 = (tm->tm_wday + 7U - tm->tm_yday - 1) % 7; - if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1))) - val++; - } else if (val == __WEEKS_IN_YEAR2__) { - /* If 1 January is not a Thursday, and not - * a Wednesday of a leap year, then this - * year has only 52 weeks. */ - int jan1 = (tm->tm_wday + 371U - tm->tm_yday) % 7; - if (jan1 != __DAY_LAST_WEEK__ && (jan1 != 3 || !is_leap(tm->tm_year))) - val = 1; - } - return val; -} - -const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc, int pad) -{ - nl_item item; - long long val; - const char *fmt = "-"; - int width = 2, def_pad = '0'; - - switch (f) { - case 'a': - if (tm->tm_wday > 6U) goto string; - item = ABDAY_1 + tm->tm_wday; - goto nl_strcat; - case 'A': - if (tm->tm_wday > 6U) goto string; - item = DAY_1 + tm->tm_wday; - goto nl_strcat; - case 'h': - case 'b': - if (tm->tm_mon > 11U) { - goto string; - } - item = ABMON_1 + tm->tm_mon; - goto nl_strcat; - case 'B': - if (tm->tm_mon > 11U) { - goto string; - } - item = MON_1 + tm->tm_mon; - goto nl_strcat; - case 'c': - item = D_T_FMT; - goto nl_strftime; - case 'C': - val = (1900LL+tm->tm_year) / __YEARS_PER_CENTURY__; - goto number; - case 'e': - def_pad = '_'; - case 'd': - val = tm->tm_mday; - goto number; - case 'D': - fmt = "%m/%d/%y"; - goto recu_strftime; - case 'F': - fmt = "%Y-%m-%d"; - goto recu_strftime; - case 'g': - case 'G': - val = tm->tm_year + 1900LL; - if (tm->tm_yday < __DAY_LAST_WEEK2__ && week_num(tm) != 1) { - val--; - } else if (tm->tm_yday > __DAYS_IN_YEAR__ && week_num(tm) == 1) { - val++; - } - if (f=='g') { - val %= __YEARS_PER_CENTURY__; - } else { - width = __LEN_YEAR__; - } - goto number; - case 'H': - val = tm->tm_hour; - goto number; - case 'I': - val = tm->tm_hour; - if (!val) { - val = __HALF_HOUR__; - } else if (val > __HALF_HOUR__) { - val -= __HALF_HOUR__; - } - goto number; - case 'j': - val = tm->tm_yday+1; - width = __LEN_DAY__; - goto number; - case 'k': - def_pad = '_'; - val = tm->tm_hour; - goto number; - case 'l': - def_pad = '_'; - val = tm->tm_hour; - if (!val) { - val = __HALF_HOUR__; - } else if (val > __HALF_HOUR__) { - val -= __HALF_HOUR__; - } - goto number; - case 'm': - val = tm->tm_mon+1; - goto number; - case 'M': - val = tm->tm_min; - goto number; - case 'n': - *l = 1; - return "\n"; - case 'p': - item = tm->tm_hour >= __HALF_HOUR__ ? PM_STR : AM_STR; - goto nl_strcat; - case 'P': - item = tm->tm_hour >= __HALF_HOUR__ ? PM_STR_LOWER : AM_STR_LOWER; - goto nl_strcat; - case 'r': - item = T_FMT_AMPM; - goto nl_strftime; - case 'R': - fmt = "%H:%M"; - goto recu_strftime; - case 's': - val = __tm_to_secs(tm) - tm->__tm_gmtoff; - width = 1; - goto number; - case 'S': - val = tm->tm_sec; - goto number; - case 't': - *l = 1; - return "\t"; - case 'T': - fmt = "%H:%M:%S"; - goto recu_strftime; - case 'u': - val = tm->tm_wday ? tm->tm_wday : 7; - width = 1; - goto number; - case 'U': - val = (tm->tm_yday + 7U - tm->tm_wday) / 7; - goto number; - case 'W': - val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7; - goto number; - case 'V': - val = week_num(tm); - goto number; - case 'v': - fmt = "%e-%b-%Y"; - goto recu_strftime; - case 'w': - val = tm->tm_wday; - width = 1; - goto number; - case 'x': - item = D_FMT; - goto nl_strftime; - case 'X': - item = T_FMT; - goto nl_strftime; - case 'y': - val = (tm->tm_year + 1900LL) % 100; - if (val < 0) { - val = -val; - } - goto number; - case 'Y': - val = tm->tm_year + 1900LL; - if (val >= 10000) { - *l = snprintf(*s, sizeof *s, "+%lld", val); - return *s; - } - width = __LEN_YEAR__; - goto number; - case 'z': - if (tm->tm_isdst < 0) { - *l = 0; - return ""; - } - *l = snprintf(*s, sizeof *s, "%+.4ld", - tm->__tm_gmtoff/3600*100 + tm->__tm_gmtoff%3600/60); - return *s; - case 'Z': - if (tm->tm_isdst < 0) { - *l = 0; - return ""; - } - fmt = __tm_to_tzname(tm); - goto string; - case '%': - *l = 1; - return "%"; - default: - return 0; - } -number: - switch (pad ? pad : def_pad) { - case '-': - *l = snprintf(*s, sizeof *s, "%lld", val); - break; - case '_': - *l = snprintf(*s, sizeof *s, "%*lld", width, val); - break; - case '0': - default: - *l = snprintf(*s, sizeof *s, "%0*lld", width, val); - break; - } - return *s; -nl_strcat: - fmt = __nl_langinfo_l(item, loc); -string: - *l = strlen(fmt); - return fmt; -nl_strftime: - fmt = __nl_langinfo_l(item, loc); -recu_strftime: - *l = __strftime_l(*s, sizeof *s, fmt, tm, loc); - if (!*l) { - return 0; - } - return *s; -} - -size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc) -{ - size_t l, k; - char buf[100]; - char *p; - const char *t; - int pad, plus; - unsigned long width; - for (l=0; ltm_year < -1900) { - s[l++] = '-'; - width--; - } else if (plus && d+(width-k) >= (*p=='C'?3:5)) { - s[l++] = '+'; - width--; - } - for (; width > k && l < n; width--) { - s[l++] = '0'; - } - } - if (k > n-l) { - k = n-l; - } - memcpy(s+l, t, k); - l += k; - } - if (n) { - if (l==n) { - l=n-1; - } - s[l] = 0; - } - return 0; -} - -size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm) -{ - return __strftime_l(s, n, f, tm, CURRENT_LOCALE); -} - -weak_alias(__strftime_l, strftime_l); \ No newline at end of file diff --git a/porting/linux/user/src/time/strptime.c b/porting/linux/user/src/time/strptime.c deleted file mode 100755 index fd357d906..000000000 --- a/porting/linux/user/src/time/strptime.c +++ /dev/null @@ -1,450 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -/* seconds per hour */ -#define __STRPTIME_SECOND_IN_HOUR 3600 -/* character-to-number base */ -#define __STRPTIME_NUMBER_BASE 10 -/* epoch time */ -#define __STRPTIME_EPOCH 1900 -/* receive data buffer size */ -#define __STRPTIME_BUFFER_SIZE 16 -/* Width of hours and minutes when formatting %z */ -#define __STRPTIME_ZONE_WIDTH 2 -/* time base */ -#define __STRPTIME_TIME_BASE 60 -/* number of weeks per year */ -#define __STRPTIME_WEEKS_IN_YEAR 53 -/* days of the week */ -#define __STRPTIME_DAYS_IN_WEEK 7 -/* 12 hour clock */ -#define __STRPTIME_HOUR_CLOCK_12 12 -/* 24 hour clock */ -#define __STRPTIME_HOUR_CLOCK_24 24 -/* days per year */ -#define __STRPTIME_DAYS_PER_YEAR 366 -/* Years in each century */ -#define __STRPTIME_YEARS_PER_CENTURY 100 - -int __getzonename(const char *restrict s, struct tm *restrict tm) -{ - const char *p = s; - struct tm old; - memcpy(&old, tm, sizeof(struct tm)); - /* Possible time zone names like +XXX or -XXX */ - if (*p == '+' || *p == '-') { - p++; - } - - /* The time zone name is adjacent to the offset second data, - * and the following symbol belongs to the offset second */ - while (*p && (*p != '+' && *p != '-' && *p != ' ')) { - p++; - } - - /* In the structure struct tm, tm_zone is declared as const char * type, so use static */ - static char buf[__STRPTIME_BUFFER_SIZE] = {0}; - memset(buf, 0x0, sizeof(buf)); - int len = p - s; - memcpy(buf, s, len); - tm->__tm_zone = buf; - - /* Re-fetch local data, extract tm_isdst flag. */ - time_t t = mktime(&old); - struct tm *tmp = localtime(&t); - if (tmp) { - tm->tm_isdst = tmp->tm_isdst; - } - return len; -} - -int __getgmtoff(const char *restrict s, struct tm *restrict tm) -{ - const char *p = s; - int sign = 1; - int i; - int isexit = 0; - long m = 0; - long h = 0; - - /* The possible formats for time offset are HHMM(-HHMM) or HH:MM(-HH:MM) */ - if (*p == '-') { - sign = -1; - } - p++; - tm->__tm_gmtoff = 0; - - /* get hours */ - for (i=0; i<__STRPTIME_ZONE_WIDTH && *p; i++, p++) { - if (isdigit(*p)) { - h = h * __STRPTIME_NUMBER_BASE + (*p - 0x30); - } else { - p--; - isexit = 1; - break; - } - } - - if (!isexit) { - /* Possible time zone formats are HH:MM. */ - if (*p == ':') { - *p++; - } - - /* get minutes */ - for (i=0; i<__STRPTIME_ZONE_WIDTH && *p; i++, p++) { - if (isdigit(*p)) { - m = m * __STRPTIME_NUMBER_BASE + (*p - 0x30); - } else { - p--; - isexit = 1; - break; - } - } - } - - /* Convert hours and minutes to seconds */ - tm->__tm_gmtoff = sign * (h * __STRPTIME_SECOND_IN_HOUR + m * __STRPTIME_TIME_BASE); - - return p - s; -} - -char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm) -{ - int i, w, neg, adj, min, range, *dest, dummy; - const char *ex; - size_t len; - int want_century = 0, century = 0, relyear = 0; - while (*f) { - if (*f != '%') { - if (isspace(*f)) { - for (; *s && isspace(*s); s++); - } else if (*s != *f) { - return 0; - } else { - s++; - } - f++; - continue; - } - f++; - if (*f == '+') { - f++; - } - if (isdigit(*f)) { - char *new_f; - w=strtoul(f, &new_f, __STRPTIME_NUMBER_BASE); - f = new_f; - } else { - w=-1; - } - adj=0; - switch (*f++) { - case 'a': case 'A': - dest = &tm->tm_wday; - min = ABDAY_1; - range = __STRPTIME_DAYS_IN_WEEK; - goto symbolic_range; - case 'b': case 'B': case 'h': - dest = &tm->tm_mon; - min = ABMON_1; - range = __STRPTIME_HOUR_CLOCK_12; - goto symbolic_range; - case 'c': - s = strptime(s, nl_langinfo(D_T_FMT), tm); - if (!s) { - return 0; - } - break; - case 'C': - dest = ¢ury; - if (w<0) { - w=__STRPTIME_ZONE_WIDTH; - } - want_century |= __STRPTIME_ZONE_WIDTH; - goto numeric_digits; - case 'd': case 'e': - dest = &tm->tm_mday; - min = 1; - range = 31; - goto numeric_range; - case 'D': - s = strptime(s, "%m/%d/%y", tm); - if (!s) { - return 0; - } - break; - case 'F': - s = strptime(s, "%Y-%m-%d", tm); - if (!s) { - return 0; - } - break; - case 'g': - dest = &tm->tm_year; - min = 0; - range = 99; - w = __STRPTIME_ZONE_WIDTH; - want_century = 0; - goto numeric_digits; - case 'G': - do { - ++s; - } while (isdigit(*s)); - continue; - case 'k': - case 'H': - dest = &tm->tm_hour; - min = 0; - range = __STRPTIME_HOUR_CLOCK_24; - goto numeric_range; - case 'l': - case 'I': - dest = &tm->tm_hour; - min = 1; - range = __STRPTIME_HOUR_CLOCK_12; - goto numeric_range; - case 'j': - dest = &tm->tm_yday; - min = 1; - range = __STRPTIME_DAYS_PER_YEAR; - adj = 1; - goto numeric_range; - case 'm': - dest = &tm->tm_mon; - min = 1; - range = __STRPTIME_HOUR_CLOCK_12; - adj = 1; - goto numeric_range; - case 'M': - dest = &tm->tm_min; - min = 0; - range = __STRPTIME_TIME_BASE; - goto numeric_range; - case 'n': case 't': - for (; *s && isspace(*s); s++) {} - break; - case 'p': - case 'P': - ex = nl_langinfo(AM_STR); - len = strlen(ex); - if (!strncasecmp(s, ex, len)) { - tm->tm_hour %= __STRPTIME_HOUR_CLOCK_12; - s += len; - break; - } - ex = nl_langinfo(PM_STR); - len = strlen(ex); - if (!strncasecmp(s, ex, len)) { - tm->tm_hour %= __STRPTIME_HOUR_CLOCK_12; - tm->tm_hour += __STRPTIME_HOUR_CLOCK_12; - s += len; - break; - } - return 0; - case 'r': - s = strptime(s, nl_langinfo(T_FMT_AMPM), tm); - if (!s) { - return 0; - } - break; - case 'R': - s = strptime(s, "%H:%M", tm); - if (!s) { - return 0; - } - break; - case 's': { - time_t secs = 0; - if (!isdigit(*s)) { - return 0; - } - do { - secs *= __STRPTIME_NUMBER_BASE; - secs += *s - '0'; - s++; - } while (isdigit(*s)); - if (localtime_r(&secs, tm) == NULL) { - return 0; - } - break; - } - case 'S': - dest = &tm->tm_sec; - min = 0; - range = 61; - goto numeric_range; - case 'T': - s = strptime(s, "%H:%M:%S", tm); - if (!s) { - return 0; - } - break; - case 'u': { - if (!isdigit(*s)) { - return 0; - } - int wday = 0; - int rulim = __STRPTIME_DAYS_IN_WEEK; - do { - wday *= __STRPTIME_NUMBER_BASE; - wday += *s++ - '0'; - rulim /= __STRPTIME_NUMBER_BASE; - } while ((wday * __STRPTIME_NUMBER_BASE < __STRPTIME_DAYS_IN_WEEK) && rulim && isdigit(*s)); - if (wday < 1 || wday > __STRPTIME_DAYS_IN_WEEK) { - return 0; - } - tm->tm_wday = wday % __STRPTIME_DAYS_IN_WEEK; - continue; - } - case 'U': - case 'W': - dest = &dummy; - min = 0; - range = __STRPTIME_WEEKS_IN_YEAR + 1; - goto numeric_range; - case 'w': - dest = &tm->tm_wday; - min = 0; - range = __STRPTIME_DAYS_IN_WEEK; - goto numeric_range; - case 'v': - if (!(s = strptime(s, "%e-%b-%Y", tm))) { - return 0; - } - break; - case 'V': { - int r = 0; - int rulim = __STRPTIME_WEEKS_IN_YEAR; - if (!isdigit(*s)) { - return 0; - } - do { - r *= __STRPTIME_NUMBER_BASE; - r += *s++ - '0'; - rulim /= __STRPTIME_NUMBER_BASE; - } while ((r * __STRPTIME_NUMBER_BASE < __STRPTIME_WEEKS_IN_YEAR) && rulim && isdigit(*s)); - if (r < 0 || r > __STRPTIME_WEEKS_IN_YEAR) { - return 0; - } - continue; - } - case 'x': - s = strptime(s, nl_langinfo(D_FMT), tm); - if (!s) { - return 0; - } - break; - case 'X': - s = strptime(s, nl_langinfo(T_FMT), tm); - if (!s) { - return 0; - } - break; - case 'y': - dest = &relyear; - w = __STRPTIME_ZONE_WIDTH; - want_century |= 1; - goto numeric_digits; - case 'Y': - dest = &tm->tm_year; - if (w<0) { - w=4; - } - adj = __STRPTIME_EPOCH; - want_century = 0; - goto numeric_digits; - case 'Z': - tzset(); - s += __getzonename((const char *)s, tm); - continue; - case 'z': - s += __getgmtoff((const char *)s, tm); - continue; - case '%': - if (*s++ != '%') { - return 0; - } - break; - default: - return 0; - numeric_range: - if (!isdigit(*s)) { - return 0; - } - *dest = 0; - for (i=1; i<=min+range && isdigit(*s); i*=__STRPTIME_NUMBER_BASE) { - *dest = *dest * __STRPTIME_NUMBER_BASE + *s++ - '0'; - } - if (*dest - min >= (unsigned)range) { - return 0; - } - *dest -= adj; - switch ((char *)dest - (char *)tm) { - case offsetof(struct tm, tm_yday): - ; - } - goto update; - numeric_digits: - neg = 0; - if (*s == '+') { - s++; - } else if (*s == '-') { - neg=1, s++; - } - if (!isdigit(*s)) { - return 0; - } - for (*dest=i=0; i=0; i--) { - ex = nl_langinfo(min+i); - len = strlen(ex); - if (strncasecmp(s, ex, len)) continue; - s += len; - *dest = i % range; - break; - } - if (i<0) { - return 0; - } - goto update; - update: - ; - } - } - if (want_century) { - tm->tm_year = relyear; - if (want_century & __STRPTIME_ZONE_WIDTH) { - tm->tm_year += century * __STRPTIME_YEARS_PER_CENTURY - __STRPTIME_EPOCH; - } - else if (tm->tm_year <= 68) tm->tm_year += __STRPTIME_YEARS_PER_CENTURY; - } - return (char *)s; -} \ No newline at end of file diff --git a/porting/linux/user/src/time/time_impl.h b/porting/linux/user/src/time/time_impl.h deleted file mode 100644 index 5474e41eb..000000000 --- a/porting/linux/user/src/time/time_impl.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __TIME_IMPL_H__ -#define __TIME_IMPL_H__ - -#include - -hidden int __days_in_month(int, int); -hidden int __month_to_secs(int, int); -hidden long long __year_to_secs(long long, int *); -hidden long long __tm_to_secs(const struct tm *); -hidden const char *__tm_to_tzname(const struct tm *); -hidden int __secs_to_tm(long long, struct tm *); -hidden void __secs_to_zone(long long, int, int *, long *, long *, const char **); -hidden const char *__strftime_fmt_1(char (*)[100], size_t *, int, const struct tm *, locale_t, int); -extern hidden const char __utc[]; -extern hidden const char __gmt[]; - -#endif \ No newline at end of file diff --git a/porting/linux/user/src/time/timer_create.c b/porting/linux/user/src/time/timer_create.c deleted file mode 100644 index 09d3d1341..000000000 --- a/porting/linux/user/src/time/timer_create.c +++ /dev/null @@ -1,131 +0,0 @@ -#include -#include -#include -#include "pthread_impl.h" -#include "atomic.h" - -struct ksigevent { - union sigval sigev_value; - int sigev_signo; - int sigev_notify; - int sigev_tid; -}; - -struct start_args { - pthread_barrier_t b; - struct sigevent *sev; -}; - -static void dummy_0() -{ -} -weak_alias(dummy_0, __pthread_tsd_run_dtors); - -static void cleanup_fromsig(void *p) -{ - pthread_t self = __pthread_self(); - __pthread_tsd_run_dtors(); -#ifdef FEATURE_PTHREAD_CANCEL - self->cancel = 0; - self->canceldisable = 0; - self->cancelasync = 0; -#endif - self->cancelbuf = 0; - __reset_tls(); - longjmp(p, 1); -} - -static void *start(void *arg) -{ - pthread_t self = __pthread_self(); - struct start_args *args = arg; - jmp_buf jb; - - void (*notify)(union sigval) = args->sev->sigev_notify_function; - union sigval val = args->sev->sigev_value; - - pthread_barrier_wait(&args->b); - for (;;) { - siginfo_t si; - while (sigwaitinfo(SIGTIMER_SET, &si) < 0); - if (si.si_code == SI_TIMER && !setjmp(jb)) { - pthread_cleanup_push(cleanup_fromsig, jb); - notify(val); - pthread_cleanup_pop(1); - } - if (self->timer_id < 0) break; - } - __syscall(SYS_timer_delete, self->timer_id & INT_MAX); - return 0; -} - -int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res) -{ - volatile static int init = 0; - pthread_t td; - pthread_attr_t attr; - int r; - struct start_args args; - struct ksigevent ksev, *ksevp=0; - int timerid; - sigset_t set; - - switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) { - case SIGEV_NONE: - case SIGEV_SIGNAL: - case SIGEV_THREAD_ID: - if (evp) { - ksev.sigev_value = evp->sigev_value; - ksev.sigev_signo = evp->sigev_signo; - ksev.sigev_notify = evp->sigev_notify; - if (evp->sigev_notify == SIGEV_THREAD_ID) - ksev.sigev_tid = evp->sigev_notify_thread_id; - else - ksev.sigev_tid = 0; - ksevp = &ksev; - } - if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0) - return -1; - *res = (void *)(intptr_t)timerid; - break; - case SIGEV_THREAD: - if (!init) { - struct sigaction sa = { .sa_handler = SIG_DFL }; - __libc_sigaction(SIGTIMER, &sa, 0); - a_store(&init, 1); - } - if (evp->sigev_notify_attributes) - attr = *evp->sigev_notify_attributes; - else - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_barrier_init(&args.b, 0, 2); - args.sev = evp; - - __block_app_sigs(&set); - __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGTIMER_SET, 0, _NSIG/8); - r = pthread_create(&td, &attr, start, &args); - __restore_sigs(&set); - if (r) { - errno = r; - return -1; - } - - ksev.sigev_value.sival_ptr = 0; - ksev.sigev_signo = SIGTIMER; - ksev.sigev_notify = SIGEV_THREAD_ID; - ksev.sigev_tid = td->tid; - if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0) - timerid = -1; - td->timer_id = timerid; - pthread_barrier_wait(&args.b); - if (timerid < 0) return -1; - *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1); - break; - default: - errno = EINVAL; - return -1; - } - - return 0; -} diff --git a/porting/linux/user/src/unistd/getpid.c b/porting/linux/user/src/unistd/getpid.c deleted file mode 100644 index ccc62e770..000000000 --- a/porting/linux/user/src/unistd/getpid.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include "pthread_impl.h" -#include "syscall.h" - -static pid_t __get_cached_pid() -{ - return __pthread_self()->pid; -} - -pid_t getpid(void) -{ - pid_t pid = __get_cached_pid(); - if (pid != 0) { - return pid; - } - return __syscall(SYS_getpid); -} diff --git a/porting/liteos_a/user/arch/arm/crt_arch.h b/porting/liteos_a/user/arch/arm/crt_arch.h deleted file mode 100644 index 219464e07..000000000 --- a/porting/liteos_a/user/arch/arm/crt_arch.h +++ /dev/null @@ -1,18 +0,0 @@ -__asm__( -".text \n" -".global " START " \n" -".type " START ",%function \n" -START ": \n" -" mov fp, #0 \n" -" mov lr, #0 \n" -" ldr a2, 1f \n" -" add a2, pc, a2 \n" -" mov a3, sp \n" -"2: and ip, a3, #-16 \n" -" mov sp, ip \n" -" bl " START "_c \n" -".weak _DYNAMIC \n" -".hidden _DYNAMIC \n" -".align 2 \n" -"1: .word _DYNAMIC-2b \n" -); diff --git a/porting/liteos_a/user/arch/arm/pthread_arch.h b/porting/liteos_a/user/arch/arm/pthread_arch.h deleted file mode 100644 index 70b8e260d..000000000 --- a/porting/liteos_a/user/arch/arm/pthread_arch.h +++ /dev/null @@ -1,34 +0,0 @@ -#if ((__ARM_ARCH_6K__ || __ARM_ARCH_6KZ__ || __ARM_ARCH_6ZK__) && !__thumb__) \ - || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH >= 7 - -static inline uintptr_t __get_tp() -{ - uintptr_t tp; - __asm__ ( "mrc p15,0,%0,c13,c0,3" : "=r"(tp) ); - return tp; -} - -#else - -#if __ARM_ARCH_4__ || __ARM_ARCH_4T__ || __ARM_ARCH == 4 -#define BLX "mov lr,pc\n\tbx" -#else -#define BLX "blx" -#endif - -static inline uintptr_t __get_tp() -{ - extern hidden uintptr_t __a_gettp_ptr; - register uintptr_t tp __asm__("r0"); - __asm__ ( BLX " %1" : "=r"(tp) : "r"(__a_gettp_ptr) : "cc", "lr" ); - return tp; -} - -#endif - -extern pthread_t __pthread_self(); - -#define TLS_ABOVE_TP -#define GAP_ABOVE_TP 8 - -#define MC_PC arm_pc diff --git a/porting/liteos_a/user/arch/arm/reloc.h b/porting/liteos_a/user/arch/arm/reloc.h deleted file mode 100644 index 843dd6211..000000000 --- a/porting/liteos_a/user/arch/arm/reloc.h +++ /dev/null @@ -1,32 +0,0 @@ -#if __BYTE_ORDER == __BIG_ENDIAN -#define ENDIAN_SUFFIX "eb" -#else -#define ENDIAN_SUFFIX "" -#endif - -#if __ARM_PCS_VFP -#define FP_SUFFIX "hf" -#else -#define FP_SUFFIX "" -#endif - -#define LDSO_ARCH "arm" ENDIAN_SUFFIX FP_SUFFIX - -#define NO_LEGACY_INITFINI - -#define TPOFF_K 0 - -#define REL_SYMBOLIC R_ARM_ABS32 -#define REL_GOT R_ARM_GLOB_DAT -#define REL_PLT R_ARM_JUMP_SLOT -#define REL_RELATIVE R_ARM_RELATIVE -#define REL_COPY R_ARM_COPY -#define REL_DTPMOD R_ARM_TLS_DTPMOD32 -#define REL_DTPOFF R_ARM_TLS_DTPOFF32 -#define REL_TPOFF R_ARM_TLS_TPOFF32 -#define REL_TLSDESC R_ARM_TLS_DESC - -#define TLSDESC_BACKWARDS - -#define CRTJMP(pc,sp) __asm__ __volatile__( \ - "mov r0,%1 ; bx %0" : : "r"(pc), "r"(sp) : "memory", "r0" ) diff --git a/porting/liteos_a/user/arch/arm/syscall_arch.h b/porting/liteos_a/user/arch/arm/syscall_arch.h deleted file mode 100644 index 9b5a52491..000000000 --- a/porting/liteos_a/user/arch/arm/syscall_arch.h +++ /dev/null @@ -1,109 +0,0 @@ -#define __SYSCALL_LL_E(x) \ -((union { long long ll; long l[2]; }){ .ll = x }).l[0], \ -((union { long long ll; long l[2]; }){ .ll = x }).l[1] -#define __SYSCALL_LL_O(x) 0, __SYSCALL_LL_E((x)) - -#ifdef __thumb__ - -/* Avoid use of r7 in asm constraints when producing thumb code, - * since it's reserved as frame pointer and might not be supported. */ -#define __ASM____R7__ -#define __asm_syscall(...) do { \ - __asm__ __volatile__ ( "mov %1,r7 ; mov r7,%2 ; svc 0 ; mov r7,%1" \ - : "=r"(r0), "=&r"((int){0}) : __VA_ARGS__ : "memory"); \ - return r0; \ - } while (0) - -#else - -#define __ASM____R7__ __asm__("r7") -#define __asm_syscall(...) do { \ - __asm__ __volatile__ ( "svc 0" \ - : "=r"(r0) : __VA_ARGS__ : "memory"); \ - return r0; \ - } while (0) -#endif - -/* For thumb2, we can allow 8-bit immediate syscall numbers, saving a - * register in the above dance around r7. Does not work for thumb1 where - * only movs, not mov, supports immediates, and we can't use movs because - * it doesn't support high regs. */ -#ifdef __thumb2__ -#define R7_OPERAND "rI"(r7) -#else -#define R7_OPERAND "r"(r7) -#endif - -static inline long __syscall0(long n) -{ - register long r7 __ASM____R7__ = n; - register long r0 __asm__("r0"); - __asm_syscall(R7_OPERAND); -} - -static inline long __syscall1(long n, long a) -{ - register long r7 __ASM____R7__ = n; - register long r0 __asm__("r0") = a; - __asm_syscall(R7_OPERAND, "0"(r0)); -} - -static inline long __syscall2(long n, long a, long b) -{ - register long r7 __ASM____R7__ = n; - register long r0 __asm__("r0") = a; - register long r1 __asm__("r1") = b; - __asm_syscall(R7_OPERAND, "0"(r0), "r"(r1)); -} - -static inline long __syscall3(long n, long a, long b, long c) -{ - register long r7 __ASM____R7__ = n; - register long r0 __asm__("r0") = a; - register long r1 __asm__("r1") = b; - register long r2 __asm__("r2") = c; - __asm_syscall(R7_OPERAND, "0"(r0), "r"(r1), "r"(r2)); -} - -static inline long __syscall4(long n, long a, long b, long c, long d) -{ - register long r7 __ASM____R7__ = n; - register long r0 __asm__("r0") = a; - register long r1 __asm__("r1") = b; - register long r2 __asm__("r2") = c; - register long r3 __asm__("r3") = d; - __asm_syscall(R7_OPERAND, "0"(r0), "r"(r1), "r"(r2), "r"(r3)); -} - -static inline long __syscall5(long n, long a, long b, long c, long d, long e) -{ - register long r7 __ASM____R7__ = n; - register long r0 __asm__("r0") = a; - register long r1 __asm__("r1") = b; - register long r2 __asm__("r2") = c; - register long r3 __asm__("r3") = d; - register long r4 __asm__("r4") = e; - __asm_syscall(R7_OPERAND, "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4)); -} - -static inline long __syscall6(long n, long a, long b, long c, long d, long e, long f) -{ - register long r7 __ASM____R7__ = n; - register long r0 __asm__("r0") = a; - register long r1 __asm__("r1") = b; - register long r2 __asm__("r2") = c; - register long r3 __asm__("r3") = d; - register long r4 __asm__("r4") = e; - register long r5 __asm__("r5") = f; - __asm_syscall(R7_OPERAND, "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5)); -} - -#define VDSO_USEFUL -#define VDSO_CGT32_SYM "VdsoClockGettime" -#define VDSO_CGT32_VER "OHOS" -#define VDSO_CGT_SYM "VdsoClockGettime" -#define VDSO_CGT_VER "OHOS" - -#define SYSCALL_FADVISE_6_ARG - -#define SYSCALL_IPC_BROKEN_MODE diff --git a/porting/liteos_a/user/include/sched.h b/porting/liteos_a/user/include/sched.h deleted file mode 100644 index 6c5710e9e..000000000 --- a/porting/liteos_a/user/include/sched.h +++ /dev/null @@ -1,143 +0,0 @@ -#ifndef _SCHED_H -#define _SCHED_H -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define __NEED_struct_timespec -#define __NEED_pid_t -#define __NEED_time_t - -#ifdef _GNU_SOURCE -#define __NEED_size_t -#endif - -#include - -struct sched_param { - union { - int sched_priority; - int sched_runtime; - }; - int sched_deadline; - int sched_period; -}; - -int sched_get_priority_max(int); -int sched_get_priority_min(int); -int sched_getparam(pid_t, struct sched_param *); -int sched_getscheduler(pid_t); -int sched_rr_get_interval(pid_t, struct timespec *); -int sched_setparam(pid_t, const struct sched_param *); -int sched_setscheduler(pid_t, int, const struct sched_param *); -int sched_yield(void); - -#define SCHED_OTHER 0 -#define SCHED_FIFO 1 -#define SCHED_RR 2 -#define SCHED_BATCH 3 -#define SCHED_IDLE 5 -#define SCHED_DEADLINE 6 -#define SCHED_RESET_ON_FORK 0x40000000 - -#ifdef _GNU_SOURCE -#define CSIGNAL 0x000000ff -#define CLONE_NEWTIME 0x00000080 -#define CLONE_VM 0x00000100 -#define CLONE_FS 0x00000200 -#define CLONE_FILES 0x00000400 -#define CLONE_SIGHAND 0x00000800 -#define CLONE_PIDFD 0x00001000 -#define CLONE_PTRACE 0x00002000 -#define CLONE_VFORK 0x00004000 -#define CLONE_PARENT 0x00008000 -#define CLONE_THREAD 0x00010000 -#define CLONE_NEWNS 0x00020000 -#define CLONE_SYSVSEM 0x00040000 -#define CLONE_SETTLS 0x00080000 -#define CLONE_PARENT_SETTID 0x00100000 -#define CLONE_CHILD_CLEARTID 0x00200000 -#define CLONE_DETACHED 0x00400000 -#define CLONE_UNTRACED 0x00800000 -#define CLONE_CHILD_SETTID 0x01000000 -#define CLONE_NEWCGROUP 0x02000000 -#define CLONE_NEWUTS 0x04000000 -#define CLONE_NEWIPC 0x08000000 -#define CLONE_NEWUSER 0x10000000 -#define CLONE_NEWPID 0x20000000 -#define CLONE_NEWNET 0x40000000 -#define CLONE_IO 0x80000000 -int clone (int (*)(void *), void *, int, void *, ...); -int unshare(int); -int setns(int, int); - -void *memcpy(void *__restrict, const void *__restrict, size_t); -int memcmp(const void *, const void *, size_t); -void *memset (void *, int, size_t); -void *calloc(size_t, size_t); -void free(void *); - -typedef struct cpu_set_t { unsigned long __bits[128/sizeof(long)]; } cpu_set_t; -int __sched_cpucount(size_t, const cpu_set_t *); -int sched_getcpu(void); -int sched_getaffinity(pid_t, size_t, cpu_set_t *); -int sched_setaffinity(pid_t, size_t, const cpu_set_t *); - -#define __CPU_op_S(i, size, set, op) ( (i)/8U >= (size) ? 0 : \ - (((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) ) - -#define CPU_SET_S(i, size, set) __CPU_op_S(i, size, set, |=) -#define CPU_CLR_S(i, size, set) __CPU_op_S(i, size, set, &=~) -#define CPU_ISSET_S(i, size, set) __CPU_op_S(i, size, set, &) - -#define __CPU_op_func_S(func, op) \ -static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, \ - const cpu_set_t *__src1, const cpu_set_t *__src2) \ -{ \ - size_t __i; \ - for (__i=0; __i<__size/sizeof(long); __i++) \ - ((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] \ - op ((unsigned long *)__src2)[__i] ; \ -} - -__CPU_op_func_S(AND, &) -__CPU_op_func_S(OR, |) -__CPU_op_func_S(XOR, ^) - -#define CPU_AND_S(a,b,c,d) __CPU_AND_S(a,b,c,d) -#define CPU_OR_S(a,b,c,d) __CPU_OR_S(a,b,c,d) -#define CPU_XOR_S(a,b,c,d) __CPU_XOR_S(a,b,c,d) - -#define CPU_COUNT_S(size,set) __sched_cpucount(size,set) -#define CPU_ZERO_S(size,set) memset(set,0,size) -#define CPU_EQUAL_S(size,set1,set2) (!memcmp(set1,set2,size)) - -#define CPU_ALLOC_SIZE(n) (sizeof(long) * ( (n)/(8*sizeof(long)) \ - + ((n)%(8*sizeof(long)) + 8*sizeof(long)-1)/(8*sizeof(long)) ) ) -#define CPU_ALLOC(n) ((cpu_set_t *)calloc(1,CPU_ALLOC_SIZE(n))) -#define CPU_FREE(set) free(set) - -#define CPU_SETSIZE 128 - -#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set) -#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set) -#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set) -#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2) -#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2) -#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2) -#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set) -#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set) -#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2) - -#endif - -#if _REDIR_TIME64 -__REDIR(sched_rr_get_interval, __sched_rr_get_interval_time64); -#endif - -#ifdef __cplusplus -} -#endif -#endif diff --git a/porting/liteos_a/user/ldso/dynlink.c b/porting/liteos_a/user/ldso/dynlink.c deleted file mode 100644 index 555a7fe98..000000000 --- a/porting/liteos_a/user/ldso/dynlink.c +++ /dev/null @@ -1,2395 +0,0 @@ -#define _GNU_SOURCE -#define SYSCALL_NO_TLS 1 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "pthread_impl.h" -#include "fork_impl.h" -#include "libc.h" -#include "dynlink.h" - -#define malloc __libc_malloc -#define calloc __libc_calloc -#define realloc __libc_realloc -#define free __libc_free - -static void error(const char *, ...); - -#define MAXP2(a,b) (-(-(a)&-(b))) -#define ALIGN(x,y) ((x)+(y)-1 & -(y)) - -#define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m))) -#define countof(a) ((sizeof (a))/(sizeof (a)[0])) - -struct debug { - int ver; - void *head; - void (*bp)(void); - int state; - void *base; -}; - -struct td_index { - size_t args[2]; - struct td_index *next; -}; - -struct dso { -#if DL_FDPIC - struct fdpic_loadmap *loadmap; -#else - unsigned char *base; -#endif - char *name; - size_t *dynv; - struct dso *next, *prev; - - Phdr *phdr; - int phnum; - size_t phentsize; - Sym *syms; - Elf_Symndx *hashtab; - uint32_t *ghashtab; - int16_t *versym; - char *strings; - struct dso *syms_next, *lazy_next; - size_t *lazy, lazy_cnt; - unsigned char *map; - size_t map_len; - char relocated; - char constructed; - char kernel_mapped; - char mark; - char bfs_built; - char runtime_loaded; - struct dso **deps, *needed_by; - size_t ndeps_direct; - size_t next_dep; - pthread_t ctor_visitor; - char *rpath_orig, *rpath; - struct tls_module tls; - size_t tls_id; - size_t relro_start, relro_end; - uintptr_t *new_dtv; - unsigned char *new_tls; - struct td_index *td_index; - struct dso *fini_next; - char *shortname; -#if DL_FDPIC - unsigned char *base; -#else - struct fdpic_loadmap *loadmap; -#endif - struct funcdesc { - void *addr; - size_t *got; - } *funcdescs; - size_t *got; - char buf[]; -}; - -struct symdef { - Sym *sym; - struct dso *dso; -}; - -typedef void (*stage3_func)(size_t *, size_t *); - -static struct builtin_tls { - char c; - struct pthread pt; - void *space[16]; -} builtin_tls[1]; -#define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt) - -#define ADDEND_LIMIT 4096 -static size_t *saved_addends, *apply_addends_to; - -static struct dso ldso; -static struct dso *head, *tail, *fini_head, *syms_tail, *lazy_head; -static char *env_path, *sys_path; -static unsigned long long gencnt; -static int runtime; -static int ldd_mode; -static int ldso_fail; -static int noload; -static int shutting_down; -static jmp_buf *rtld_fail; -static pthread_rwlock_t lock; -static struct debug debug; -static struct tls_module *tls_tail; -static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN; -static size_t static_tls_cnt; -static pthread_mutex_t init_fini_lock; -static pthread_cond_t ctor_cond; -static struct dso *builtin_deps[2]; -static struct dso *const no_deps[1]; -static struct dso *builtin_ctor_queue[4]; -static struct dso **main_ctor_queue; -static struct fdpic_loadmap *app_loadmap; -static struct fdpic_dummy_loadmap app_dummy_loadmap; - -struct debug *_dl_debug_addr = &debug; - -extern hidden int __malloc_replaced; - -hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0; - -extern hidden void (*const __init_array_end)(void), (*const __fini_array_end)(void); - -weak_alias(__init_array_start, __init_array_end); -weak_alias(__fini_array_start, __fini_array_end); - -static int dl_strcmp(const char *l, const char *r) -{ - for (; *l==*r && *l; l++, r++); - return *(unsigned char *)l - *(unsigned char *)r; -} -#define strcmp(l,r) dl_strcmp(l,r) - -/* Compute load address for a virtual address in a given dso. */ -#if DL_FDPIC -static void *laddr(const struct dso *p, size_t v) -{ - size_t j=0; - if (!p->loadmap) return p->base + v; - for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++); - return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr); -} -static void *laddr_pg(const struct dso *p, size_t v) -{ - size_t j=0; - size_t pgsz = PAGE_SIZE; - if (!p->loadmap) return p->base + v; - for (j=0; ; j++) { - size_t a = p->loadmap->segs[j].p_vaddr; - size_t b = a + p->loadmap->segs[j].p_memsz; - a &= -pgsz; - b += pgsz-1; - b &= -pgsz; - if (v-aloadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr); -} -static void (*fdbarrier(void *p))() -{ - void (*fd)(); - __asm__("" : "=r"(fd) : "0"(p)); - return fd; -} -#define fpaddr(p, v) fdbarrier((&(struct funcdesc){ \ - laddr(p, v), (p)->got })) -#else -#define laddr(p, v) (void *)((p)->base + (v)) -#define laddr_pg(p, v) laddr(p, v) -#define fpaddr(p, v) ((void (*)())laddr(p, v)) -#endif - -static void decode_vec(size_t *v, size_t *a, size_t cnt) -{ - size_t i; - for (i=0; i>24 & 0xf0; - } - return h & 0xfffffff; -} - -static uint32_t gnu_hash(const char *s0) -{ - const unsigned char *s = (void *)s0; - uint_fast32_t h = 5381; - for (; *s; s++) - h += h*32 + *s; - return h; -} - -static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso) -{ - size_t i; - Sym *syms = dso->syms; - Elf_Symndx *hashtab = dso->hashtab; - char *strings = dso->strings; - for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { - if ((!dso->versym || dso->versym[i] >= 0) - && (!strcmp(s, strings+syms[i].st_name))) - return syms+i; - } - return 0; -} - -static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s) -{ - uint32_t nbuckets = hashtab[0]; - uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4); - uint32_t i = buckets[h1 % nbuckets]; - - if (!i) return 0; - - uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]); - - for (h1 |= 1; ; i++) { - uint32_t h2 = *hashval++; - if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0) - && !strcmp(s, dso->strings + dso->syms[i].st_name)) - return dso->syms+i; - if (h2 & 1) break; - } - - return 0; -} - -static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask) -{ - const size_t *bloomwords = (const void *)(hashtab+4); - size_t f = bloomwords[fofs & (hashtab[2]-1)]; - if (!(f & fmask)) return 0; - - f >>= (h1 >> hashtab[3]) % (8 * sizeof f); - if (!(f & 1)) return 0; - - return gnu_lookup(h1, hashtab, dso, s); -} - -#define OK_TYPES (1<deps : 0; - for (; dso; dso=use_deps ? *deps++ : dso->syms_next) { - Sym *sym; - if ((ght = dso->ghashtab)) { - sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm); - } else { - if (!h) h = sysv_hash(s); - sym = sysv_lookup(s, h, dso); - } - if (!sym) continue; - if (!sym->st_shndx) - if (need_def || (sym->st_info&0xf) == STT_TLS - || ARCH_SYM_REJECT_UND(sym)) - continue; - if (!sym->st_value) - if ((sym->st_info&0xf) != STT_TLS) - continue; - if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue; - if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue; - def.sym = sym; - def.dso = dso; - break; - } - return def; -} - -static struct symdef find_sym(struct dso *dso, const char *s, int need_def) -{ - return find_sym2(dso, s, need_def, 0); -} - -static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride) -{ - unsigned char *base = dso->base; - Sym *syms = dso->syms; - char *strings = dso->strings; - Sym *sym; - const char *name; - void *ctx; - int type; - int sym_index; - struct symdef def; - size_t *reloc_addr; - size_t sym_val; - size_t tls_val; - size_t addend; - int skip_relative = 0, reuse_addends = 0, save_slot = 0; - - if (dso == &ldso) { - /* Only ldso's REL table needs addend saving/reuse. */ - if (rel == apply_addends_to) - reuse_addends = 1; - skip_relative = 1; - } - - for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { - if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue; - type = R_TYPE(rel[1]); - if (type == REL_NONE) continue; - reloc_addr = laddr(dso, rel[0]); - - if (stride > 2) { - addend = rel[2]; - } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) { - addend = 0; - } else if (reuse_addends) { - /* Save original addend in stage 2 where the dso - * chain consists of just ldso; otherwise read back - * saved addend since the inline one was clobbered. */ - if (head==&ldso) - saved_addends[save_slot] = *reloc_addr; - addend = saved_addends[save_slot++]; - } else { - addend = *reloc_addr; - } - - sym_index = R_SYM(rel[1]); - if (sym_index) { - sym = syms + sym_index; - name = strings + sym->st_name; - ctx = type==REL_COPY ? head->syms_next : head; - def = (sym->st_info>>4) == STB_LOCAL - ? (struct symdef){ .dso = dso, .sym = sym } - : find_sym(ctx, name, type==REL_PLT); - if (!def.sym && (sym->st_shndx != SHN_UNDEF - || sym->st_info>>4 != STB_WEAK)) { - if (dso->lazy && (type==REL_PLT || type==REL_GOT)) { - dso->lazy[3*dso->lazy_cnt+0] = rel[0]; - dso->lazy[3*dso->lazy_cnt+1] = rel[1]; - dso->lazy[3*dso->lazy_cnt+2] = addend; - dso->lazy_cnt++; - continue; - } - error("Error relocating %s: %s: symbol not found", - dso->name, name); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - } else { - sym = 0; - def.sym = 0; - def.dso = dso; - } - - sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0; - tls_val = def.sym ? def.sym->st_value : 0; - - if ((type == REL_TPOFF || type == REL_TPOFF_NEG) - && def.dso->tls_id > static_tls_cnt) { - error("Error relocating %s: %s: initial-exec TLS " - "resolves to dynamic definition in %s", - dso->name, name, def.dso->name); - longjmp(*rtld_fail, 1); - } - - switch(type) { - case REL_OFFSET: - addend -= (size_t)reloc_addr; - case REL_SYMBOLIC: - case REL_GOT: - case REL_PLT: - *reloc_addr = sym_val + addend; - break; - case REL_USYMBOLIC: - memcpy(reloc_addr, &(size_t){sym_val + addend}, sizeof(size_t)); - break; - case REL_RELATIVE: - *reloc_addr = (size_t)base + addend; - break; - case REL_SYM_OR_REL: - if (sym) *reloc_addr = sym_val + addend; - else *reloc_addr = (size_t)base + addend; - break; - case REL_COPY: - memcpy(reloc_addr, (void *)sym_val, sym->st_size); - break; - case REL_OFFSET32: - *(uint32_t *)reloc_addr = sym_val + addend - - (size_t)reloc_addr; - break; - case REL_FUNCDESC: - *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs - + (def.sym - def.dso->syms)) : 0; - break; - case REL_FUNCDESC_VAL: - if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val; - else *reloc_addr = sym_val; - reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0; - break; - case REL_DTPMOD: - *reloc_addr = def.dso->tls_id; - break; - case REL_DTPOFF: - *reloc_addr = tls_val + addend - DTP_OFFSET; - break; -#ifdef TLS_ABOVE_TP - case REL_TPOFF: - *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend; - break; -#else - case REL_TPOFF: - *reloc_addr = tls_val - def.dso->tls.offset + addend; - break; - case REL_TPOFF_NEG: - *reloc_addr = def.dso->tls.offset - tls_val + addend; - break; -#endif - case REL_TLSDESC: - if (stride<3) addend = reloc_addr[1]; - if (def.dso->tls_id > static_tls_cnt) { - struct td_index *new = malloc(sizeof *new); - if (!new) { - error( - "Error relocating %s: cannot allocate TLSDESC for %s", - dso->name, sym ? name : "(local)" ); - longjmp(*rtld_fail, 1); - } - new->next = dso->td_index; - dso->td_index = new; - new->args[0] = def.dso->tls_id; - new->args[1] = tls_val + addend - DTP_OFFSET; - reloc_addr[0] = (size_t)__tlsdesc_dynamic; - reloc_addr[1] = (size_t)new; - } else { - reloc_addr[0] = (size_t)__tlsdesc_static; -#ifdef TLS_ABOVE_TP - reloc_addr[1] = tls_val + def.dso->tls.offset - + TPOFF_K + addend; -#else - reloc_addr[1] = tls_val - def.dso->tls.offset - + addend; -#endif - } -#ifdef TLSDESC_BACKWARDS - /* Some archs (32-bit ARM at least) invert the order of - * the descriptor members. Fix them up here. */ - size_t tmp = reloc_addr[0]; - reloc_addr[0] = reloc_addr[1]; - reloc_addr[1] = tmp; -#endif - break; - default: - error("Error relocating %s: unsupported relocation type %d", - dso->name, type); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - } -} - -static void redo_lazy_relocs() -{ - struct dso *p = lazy_head, *next; - lazy_head = 0; - for (; p; p=next) { - next = p->lazy_next; - size_t size = p->lazy_cnt*3*sizeof(size_t); - p->lazy_cnt = 0; - do_relocs(p, p->lazy, size, 3); - if (p->lazy_cnt) { - p->lazy_next = lazy_head; - lazy_head = p; - } else { - free(p->lazy); - p->lazy = 0; - p->lazy_next = 0; - } - } -} - -/* A huge hack: to make up for the wastefulness of shared libraries - * needing at least a page of dirty memory even if they have no global - * data, we reclaim the gaps at the beginning and end of writable maps - * and "donate" them to the heap. */ - -static void reclaim(struct dso *dso, size_t start, size_t end) -{ - if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end; - if (end >= dso->relro_start && end < dso->relro_end) end = dso->relro_start; - if (start >= end) return; - char *base = laddr_pg(dso, start); - __malloc_donate(base, base+(end-start)); -} - -static void reclaim_gaps(struct dso *dso) -{ - Phdr *ph = dso->phdr; - size_t phcnt = dso->phnum; - - for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) { - if (ph->p_type!=PT_LOAD) continue; - if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue; - reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr); - reclaim(dso, ph->p_vaddr+ph->p_memsz, - ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE); - } -} - -static ssize_t read_loop(int fd, void *p, size_t n) -{ - for (size_t i=0; iloadmap) { - size_t i; - for (i=0; iloadmap->nsegs; i++) { - if (!dso->loadmap->segs[i].p_memsz) - continue; - munmap((void *)dso->loadmap->segs[i].addr, - dso->loadmap->segs[i].p_memsz); - } - free(dso->loadmap); - } else if (dso->map && dso->map_len) { - munmap(dso->map, dso->map_len); - } -} - -static void *map_library(int fd, struct dso *dso) -{ - Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; - void *allocated_buf=0; - size_t phsize; - size_t addr_min=SIZE_MAX, addr_max=0, map_len; - size_t this_min, this_max; - size_t nsegs = 0; - off_t off_start; - Ehdr *eh; - Phdr *ph, *ph0; - unsigned prot; - unsigned char *map=MAP_FAILED, *base; - size_t dyn=0; - size_t tls_image=0; - size_t i; - - ssize_t l = read(fd, buf, sizeof buf); - eh = buf; - if (l<0) return 0; - if (le_type != ET_DYN && eh->e_type != ET_EXEC)) - goto noexec; - phsize = eh->e_phentsize * eh->e_phnum; - if (phsize > sizeof buf - sizeof *eh) { - allocated_buf = malloc(phsize); - if (!allocated_buf) return 0; - l = pread(fd, allocated_buf, phsize, eh->e_phoff); - if (l < 0) goto error; - if (l != phsize) goto noexec; - ph = ph0 = allocated_buf; - } else if (eh->e_phoff + phsize > l) { - l = pread(fd, buf+1, phsize, eh->e_phoff); - if (l < 0) goto error; - if (l != phsize) goto noexec; - ph = ph0 = (void *)(buf + 1); - } else { - ph = ph0 = (void *)((char *)buf + eh->e_phoff); - } - for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { - if (ph->p_type == PT_DYNAMIC) { - dyn = ph->p_vaddr; - } else if (ph->p_type == PT_TLS) { - tls_image = ph->p_vaddr; - dso->tls.align = ph->p_align; - dso->tls.len = ph->p_filesz; - dso->tls.size = ph->p_memsz; - } else if (ph->p_type == PT_GNU_RELRO) { - dso->relro_start = ph->p_vaddr & -PAGE_SIZE; - dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; - } else if (ph->p_type == PT_GNU_STACK) { - if (!runtime && ph->p_memsz > __default_stacksize) { - __default_stacksize = - ph->p_memsz < DEFAULT_STACK_MAX ? - ph->p_memsz : DEFAULT_STACK_MAX; - } - } - if (ph->p_type != PT_LOAD) continue; - nsegs++; - if (ph->p_vaddr < addr_min) { - addr_min = ph->p_vaddr; - off_start = ph->p_offset; - prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | - ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | - ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); - } - if (ph->p_vaddr+ph->p_memsz > addr_max) { - addr_max = ph->p_vaddr+ph->p_memsz; - } - } - if (!dyn) goto noexec; - if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) { - dso->loadmap = calloc(1, sizeof *dso->loadmap - + nsegs * sizeof *dso->loadmap->segs); - if (!dso->loadmap) goto error; - dso->loadmap->nsegs = nsegs; - for (ph=ph0, i=0; ie_phentsize)) { - if (ph->p_type != PT_LOAD) continue; - prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | - ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | - ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); - map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1), - prot, MAP_PRIVATE, - fd, ph->p_offset & -PAGE_SIZE); - if (map == MAP_FAILED) { - unmap_library(dso); - goto error; - } - dso->loadmap->segs[i].addr = (size_t)map + - (ph->p_vaddr & PAGE_SIZE-1); - dso->loadmap->segs[i].p_vaddr = ph->p_vaddr; - dso->loadmap->segs[i].p_memsz = ph->p_memsz; - i++; - if (prot & PROT_WRITE) { - size_t brk = (ph->p_vaddr & PAGE_SIZE-1) - + ph->p_filesz; - size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE; - size_t pgend = brk + ph->p_memsz - ph->p_filesz - + PAGE_SIZE-1 & -PAGE_SIZE; - if (pgend > pgbrk && mmap_fixed(map+pgbrk, - pgend-pgbrk, prot, - MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, - -1, off_start) == MAP_FAILED) - goto error; - memset(map + brk, 0, pgbrk-brk); - } - } - map = (void *)dso->loadmap->segs[0].addr; - map_len = 0; - goto done_mapping; - } - addr_max += PAGE_SIZE-1; - addr_max &= -PAGE_SIZE; - addr_min &= -PAGE_SIZE; - off_start &= -PAGE_SIZE; - map_len = addr_max - addr_min + off_start; - /* The first time, we map too much, possibly even more than - * the length of the file. This is okay because we will not - * use the invalid part; we just need to reserve the right - * amount of virtual address space to map over later. */ - map = DL_NOMMU_SUPPORT - ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) - : mmap((void *)addr_min, map_len, prot, - MAP_PRIVATE, fd, off_start); - if (map==MAP_FAILED) goto error; - dso->map = map; - dso->map_len = map_len; - /* If the loaded file is not relocatable and the requested address is - * not available, then the load operation must fail. */ - if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) { - errno = EBUSY; - goto error; - } - base = map - addr_min; - dso->phdr = 0; - dso->phnum = 0; - for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { - if (ph->p_type != PT_LOAD) continue; - /* Check if the programs headers are in this load segment, and - * if so, record the address for use by dl_iterate_phdr. */ - if (!dso->phdr && eh->e_phoff >= ph->p_offset - && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) { - dso->phdr = (void *)(base + ph->p_vaddr - + (eh->e_phoff-ph->p_offset)); - dso->phnum = eh->e_phnum; - dso->phentsize = eh->e_phentsize; - } - this_min = ph->p_vaddr & -PAGE_SIZE; - this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; - off_start = ph->p_offset & -PAGE_SIZE; - prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | - ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | - ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); - if ((ph->p_flags & PF_R) && (ph->p_flags & PF_X) && (!(ph->p_flags & PF_W))) { - Phdr *next_ph = ph; - for (int j = i - 1; j > 0; j--) { - next_ph = (void *)((char *)next_ph+eh->e_phentsize); - if (next_ph->p_type != PT_LOAD) { - continue; - } - size_t p_vaddr = (next_ph->p_vaddr & -(PAGE_SIZE)); - if (p_vaddr > this_max) { - mprotect(base + this_max, p_vaddr - this_max , PROT_READ); - } - break; - } - } - /* Reuse the existing mapping for the lowest-address LOAD */ - if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT) - if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) - goto error; - if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) { - size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; - size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; - memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); - if (pgbrk-(size_t)base < this_max && mmap_fixed((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) - goto error; - } - } - for (i=0; ((size_t *)(base+dyn))[i]; i+=2) - if (((size_t *)(base+dyn))[i]==DT_TEXTREL) { - if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) - && errno != ENOSYS) - goto error; - break; - } -done_mapping: - dso->base = base; - dso->dynv = laddr(dso, dyn); - if (dso->tls.size) dso->tls.image = laddr(dso, tls_image); - free(allocated_buf); - return map; -noexec: - errno = ENOEXEC; -error: - if (map!=MAP_FAILED) unmap_library(dso); - free(allocated_buf); - return 0; -} - -static int path_open(const char *name, const char *s, char *buf, size_t buf_size) -{ - size_t l; - int fd; - for (;;) { - s += strspn(s, ":\n"); - l = strcspn(s, ":\n"); - if (l-1 >= INT_MAX) return -1; - if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) { - if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd; - switch (errno) { - case ENOENT: - case ENOTDIR: - case EACCES: - case ENAMETOOLONG: - break; - default: - /* Any negative value but -1 will inhibit - * futher path search. */ - return -2; - } - } - s += l; - } -} - -static int fixup_rpath(struct dso *p, char *buf, size_t buf_size) -{ - size_t n, l; - const char *s, *t, *origin; - char *d; - if (p->rpath || !p->rpath_orig) return 0; - if (!strchr(p->rpath_orig, '$')) { - p->rpath = p->rpath_orig; - return 0; - } - n = 0; - s = p->rpath_orig; - while ((t=strchr(s, '$'))) { - if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9)) - return 0; - s = t+1; - n++; - } - if (n > SSIZE_MAX/PATH_MAX) return 0; - - if (p->kernel_mapped) { - /* $ORIGIN searches cannot be performed for the main program - * when it is suid/sgid/AT_SECURE. This is because the - * pathname is under the control of the caller of execve. - * For libraries, however, $ORIGIN can be processed safely - * since the library's pathname came from a trusted source - * (either system paths or a call to dlopen). */ - if (libc.secure) - return 0; - l = readlink("/proc/self/exe", buf, buf_size); - if (l == -1) switch (errno) { - case ENOENT: - case ENOTDIR: - case EACCES: - break; - default: - return -1; - } - if (l >= buf_size) - return 0; - buf[l] = 0; - origin = buf; - } else { - origin = p->name; - } - t = strrchr(origin, '/'); - if (t) { - l = t-origin; - } else { - /* Normally p->name will always be an absolute or relative - * pathname containing at least one '/' character, but in the - * case where ldso was invoked as a command to execute a - * program in the working directory, app.name may not. Fix. */ - origin = "."; - l = 1; - } - /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */ - if (libc.secure && *origin != '/') - return 0; - p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1); - if (!p->rpath) return -1; - - d = p->rpath; - s = p->rpath_orig; - while ((t=strchr(s, '$'))) { - memcpy(d, s, t-s); - d += t-s; - memcpy(d, origin, l); - d += l; - /* It was determined previously that the '$' is followed - * either by "ORIGIN" or "{ORIGIN}". */ - s = t + 7 + 2*(t[1]=='{'); - } - strcpy(d, s); - return 0; -} - -static void decode_dyn(struct dso *p) -{ - size_t dyn[DYN_CNT]; - decode_vec(p->dynv, dyn, DYN_CNT); - p->syms = laddr(p, dyn[DT_SYMTAB]); - p->strings = laddr(p, dyn[DT_STRTAB]); - if (dyn[0]&(1<hashtab = laddr(p, dyn[DT_HASH]); - if (dyn[0]&(1<rpath_orig = p->strings + dyn[DT_RPATH]; - if (dyn[0]&(1<rpath_orig = p->strings + dyn[DT_RUNPATH]; - if (dyn[0]&(1<got = laddr(p, dyn[DT_PLTGOT]); - if (search_vec(p->dynv, dyn, DT_GNU_HASH)) - p->ghashtab = laddr(p, *dyn); - if (search_vec(p->dynv, dyn, DT_VERSYM)) - p->versym = laddr(p, *dyn); -} - -static size_t count_syms(struct dso *p) -{ - if (p->hashtab) return p->hashtab[1]; - - size_t nsym, i; - uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4); - uint32_t *hashval; - for (i = nsym = 0; i < p->ghashtab[0]; i++) { - if (buckets[i] > nsym) - nsym = buckets[i]; - } - if (nsym) { - hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]); - do nsym++; - while (!(*hashval++ & 1)); - } - return nsym; -} - -static void *dl_mmap(size_t n) -{ - void *p; - int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE; -#ifdef SYS_mmap2 - p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0); -#else - p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0); -#endif - return (unsigned long)p > -4096UL ? 0 : p; -} - -static void makefuncdescs(struct dso *p) -{ - static int self_done; - size_t nsym = count_syms(p); - size_t i, size = nsym * sizeof(*p->funcdescs); - - if (!self_done) { - p->funcdescs = dl_mmap(size); - self_done = 1; - } else { - p->funcdescs = malloc(size); - } - if (!p->funcdescs) { - if (!runtime) a_crash(); - error("Error allocating function descriptors for %s", p->name); - longjmp(*rtld_fail, 1); - } - for (i=0; isyms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) { - p->funcdescs[i].addr = laddr(p, p->syms[i].st_value); - p->funcdescs[i].got = p->got; - } else { - p->funcdescs[i].addr = 0; - p->funcdescs[i].got = 0; - } - } -} - -static struct dso *load_library(const char *name, struct dso *needed_by) -{ - char buf[2*NAME_MAX+2]; - char fullpath[2*NAME_MAX+2]; - const char *pathname; - unsigned char *map; - struct dso *p, temp_dso = {0}; - int fd; - struct stat st; - size_t alloc_size; - int n_th = 0; - int is_self = 0; - - if (!*name) { - errno = EINVAL; - return 0; - } - - /* Catch and block attempts to reload the implementation itself */ - if (name[0]=='l' && name[1]=='i' && name[2]=='b') { - static const char reserved[] = - "c.pthread.rt.m.dl.util.xnet."; - const char *rp, *next; - for (rp=reserved; *rp; rp=next) { - next = strchr(rp, '.') + 1; - if (strncmp(name+3, rp, next-rp) == 0) - break; - } - if (*rp) { - if (ldd_mode) { - /* Track which names have been resolved - * and only report each one once. */ - static unsigned reported; - unsigned mask = 1U<<(rp-reserved); - if (!(reported & mask)) { - reported |= mask; - dprintf(1, "\t%s => %s (%p)\n", - name, ldso.name, - ldso.base); - } - } - is_self = 1; - } - } - if (!strcmp(name, ldso.name)) is_self = 1; - if (is_self) { - if (!ldso.prev) { - tail->next = &ldso; - ldso.prev = tail; - tail = &ldso; - } - return &ldso; - } - if (strchr(name, '/')) { - pathname = name; - fd = open(name, O_RDONLY|O_CLOEXEC); - } else { - /* Search for the name to see if it's already loaded */ - for (p=head->next; p; p=p->next) { - if (p->shortname && !strcmp(p->shortname, name)) { - return p; - } - } - if (strlen(name) > NAME_MAX) return 0; - fd = -1; - if (env_path) fd = path_open(name, env_path, buf, sizeof buf); - for (p=needed_by; fd == -1 && p; p=p->needed_by) { - if (fixup_rpath(p, buf, sizeof buf) < 0) - fd = -2; /* Inhibit further search. */ - if (p->rpath) - fd = path_open(name, p->rpath, buf, sizeof buf); - } - if (fd == -1) { - if (!sys_path) { - char *prefix = 0; - size_t prefix_len; - if (ldso.name[0]=='/') { - char *s, *t, *z; - for (s=t=z=ldso.name; *s; s++) - if (*s=='/') z=t, t=s; - prefix_len = z-ldso.name; - if (prefix_len < PATH_MAX) - prefix = ldso.name; - } - if (!prefix) { - prefix = ""; - prefix_len = 0; - } - char etc_ldso_path[prefix_len + 1 - + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"]; - snprintf(etc_ldso_path, sizeof etc_ldso_path, - "%.*s/etc/ld-musl-" LDSO_ARCH ".path", - (int)prefix_len, prefix); - fd = open(etc_ldso_path, O_RDONLY|O_CLOEXEC); - if (fd>=0) { - size_t n = 0; - if (!fstat(fd, &st)) n = st.st_size; - if ((sys_path = malloc(n+1))) - sys_path[n] = 0; - if (!sys_path || read_loop(fd, sys_path, n)<0) { - - free(sys_path); - sys_path = ""; - } - close(fd); - } else if (errno != ENOENT) { - sys_path = ""; - } - } - if (!sys_path || sys_path[0] == 0) sys_path = "/usr/lib:/lib:/usr/local/lib"; - fd = path_open(name, sys_path, buf, sizeof buf); - } - pathname = buf; - } - if (fd < 0) return 0; - - if (pathname[0] != '/') { - if (!realpath(pathname, fullpath)) { - close(fd); - return 0; - } - pathname = fullpath; - } - - for (p=head->next; p; p=p->next) { - if (!strcmp(p->name, pathname)) { - /* If this library was previously loaded with a - * pathname but a search found the same inode, - * setup its shortname so it can be found by name. */ - if (!p->shortname && pathname != name) - p->shortname = strrchr(p->name, '/')+1; - close(fd); - return p; - } - } - map = noload ? 0 : map_library(fd, &temp_dso); - close(fd); - if (!map) return 0; - - /* Avoid the danger of getting two versions of libc mapped into the - * same process when an absolute pathname was used. The symbols - * checked are chosen to catch both musl and glibc, and to avoid - * false positives from interposition-hack libraries. */ - decode_dyn(&temp_dso); - if (find_sym(&temp_dso, "__libc_start_main", 1).sym && - find_sym(&temp_dso, "stdin", 1).sym) { - unmap_library(&temp_dso); - return load_library("libc.so", needed_by); - } - /* Past this point, if we haven't reached runtime yet, ldso has - * committed either to use the mapped library or to abort execution. - * Unmapping is not possible, so we can safely reclaim gaps. */ - if (!runtime) reclaim_gaps(&temp_dso); - - /* Allocate storage for the new DSO. When there is TLS, this - * storage must include a reservation for all pre-existing - * threads to obtain copies of both the new TLS, and an - * extended DTV capable of storing an additional slot for - * the newly-loaded DSO. */ - alloc_size = sizeof *p + strlen(pathname) + 1; - if (runtime && temp_dso.tls.image) { - size_t per_th = temp_dso.tls.size + temp_dso.tls.align - + sizeof(void *) * (tls_cnt+3); - n_th = libc.threads_minus_1 + 1; - if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX; - else alloc_size += n_th * per_th; - } - p = calloc(1, alloc_size); - if (!p) { - unmap_library(&temp_dso); - return 0; - } - memcpy(p, &temp_dso, sizeof temp_dso); - p->needed_by = needed_by; - p->name = p->buf; - p->runtime_loaded = runtime; - strcpy(p->name, pathname); - /* Add a shortname only if name arg was not an explicit pathname. */ - if (pathname != name) p->shortname = strrchr(p->name, '/')+1; - if (p->tls.image) { - p->tls_id = ++tls_cnt; - tls_align = MAXP2(tls_align, p->tls.align); -#ifdef TLS_ABOVE_TP - p->tls.offset = tls_offset + ( (p->tls.align-1) & - (-tls_offset + (uintptr_t)p->tls.image) ); - tls_offset = p->tls.offset + p->tls.size; -#else - tls_offset += p->tls.size + p->tls.align - 1; - tls_offset -= (tls_offset + (uintptr_t)p->tls.image) - & (p->tls.align-1); - p->tls.offset = tls_offset; -#endif - p->new_dtv = (void *)(-sizeof(size_t) & - (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t))); - p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1)); - if (tls_tail) tls_tail->next = &p->tls; - else libc.tls_head = &p->tls; - tls_tail = &p->tls; - } - - tail->next = p; - p->prev = tail; - tail = p; - - if (DL_FDPIC) makefuncdescs(p); - - if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); - - return p; -} - -static void load_direct_deps(struct dso *p) -{ - size_t i, cnt=0; - - if (p->deps) return; - /* For head, all preloads are direct pseudo-dependencies. - * Count and include them now to avoid realloc later. */ - if (p==head) for (struct dso *q=p->next; q; q=q->next) - cnt++; - for (i=0; p->dynv[i]; i+=2) - if (p->dynv[i] == DT_NEEDED) cnt++; - /* Use builtin buffer for apps with no external deps, to - * preserve property of no runtime failure paths. */ - p->deps = (p==head && cnt<2) ? builtin_deps : - calloc(cnt+1, sizeof *p->deps); - if (!p->deps) { - error("Error loading dependencies for %s", p->name); - if (runtime) longjmp(*rtld_fail, 1); - } - cnt=0; - if (p==head) for (struct dso *q=p->next; q; q=q->next) - p->deps[cnt++] = q; - for (i=0; p->dynv[i]; i+=2) { - if (p->dynv[i] != DT_NEEDED) continue; - struct dso *dep = load_library(p->strings + p->dynv[i+1], p); - if (!dep) { - error("Error loading shared library %s: %m (needed by %s)", - p->strings + p->dynv[i+1], p->name); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - p->deps[cnt++] = dep; - } - p->deps[cnt] = 0; - p->ndeps_direct = cnt; -} - -static void load_deps(struct dso *p) -{ - if (p->deps) return; - for (; p; p=p->next) - load_direct_deps(p); -} - -static void extend_bfs_deps(struct dso *p) -{ - size_t i, j, cnt, ndeps_all; - struct dso **tmp; - - /* Can't use realloc if the original p->deps was allocated at - * program entry and malloc has been replaced, or if it's - * the builtin non-allocated trivial main program deps array. */ - int no_realloc = (__malloc_replaced && !p->runtime_loaded) - || p->deps == builtin_deps; - - if (p->bfs_built) return; - ndeps_all = p->ndeps_direct; - - /* Mark existing (direct) deps so they won't be duplicated. */ - for (i=0; p->deps[i]; i++) - p->deps[i]->mark = 1; - - /* For each dependency already in the list, copy its list of direct - * dependencies to the list, excluding any items already in the - * list. Note that the list this loop iterates over will grow during - * the loop, but since duplicates are excluded, growth is bounded. */ - for (i=0; p->deps[i]; i++) { - struct dso *dep = p->deps[i]; - for (j=cnt=0; jndeps_direct; j++) - if (!dep->deps[j]->mark) cnt++; - tmp = no_realloc ? - malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) : - realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1)); - if (!tmp) { - error("Error recording dependencies for %s", p->name); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - if (no_realloc) { - memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1)); - no_realloc = 0; - } - p->deps = tmp; - for (j=0; jndeps_direct; j++) { - if (dep->deps[j]->mark) continue; - dep->deps[j]->mark = 1; - p->deps[ndeps_all++] = dep->deps[j]; - } - p->deps[ndeps_all] = 0; - } - p->bfs_built = 1; - for (p=head; p; p=p->next) - p->mark = 0; -} - -static void load_preload(char *s) -{ - int tmp; - char *z; - for (z=s; *z; s=z) { - for ( ; *s && (isspace(*s) || *s==':'); s++); - for (z=s; *z && !isspace(*z) && *z!=':'; z++); - tmp = *z; - *z = 0; - load_library(s, 0); - *z = tmp; - } -} - -static void add_syms(struct dso *p) -{ - if (!p->syms_next && syms_tail != p) { - syms_tail->syms_next = p; - syms_tail = p; - } -} - -static void revert_syms(struct dso *old_tail) -{ - struct dso *p, *next; - /* Chop off the tail of the list of dsos that participate in - * the global symbol table, reverting them to RTLD_LOCAL. */ - for (p=old_tail; p; p=next) { - next = p->syms_next; - p->syms_next = 0; - } - syms_tail = old_tail; -} - -static void do_mips_relocs(struct dso *p, size_t *got) -{ - size_t i, j, rel[2]; - unsigned char *base = p->base; - i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO); - if (p==&ldso) { - got += i; - } else { - while (i--) *got++ += (size_t)base; - } - j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM); - i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); - Sym *sym = p->syms + j; - rel[0] = (unsigned char *)got - base; - for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) { - rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT); - do_relocs(p, rel, sizeof rel, 2); - } -} - -static void reloc_all(struct dso *p) -{ - size_t dyn[DYN_CNT]; - for (; p; p=p->next) { - if (p->relocated) continue; - decode_vec(p->dynv, dyn, DYN_CNT); - if (NEED_MIPS_GOT_RELOCS) - do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT])); - do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], - 2+(dyn[DT_PLTREL]==DT_RELA)); - do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2); - do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3); - - if (head != &ldso && p->relro_start != p->relro_end && - mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ) - && errno != ENOSYS) { - error("Error relocating %s: RELRO protection failed: %m", - p->name); - if (runtime) longjmp(*rtld_fail, 1); - } - - p->relocated = 1; - } -} - -static void kernel_mapped_dso(struct dso *p) -{ - size_t min_addr = -1, max_addr = 0, cnt; - Phdr *ph = p->phdr; - for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) { - if (ph->p_type == PT_DYNAMIC) { - p->dynv = laddr(p, ph->p_vaddr); - } else if (ph->p_type == PT_GNU_RELRO) { - p->relro_start = ph->p_vaddr & -PAGE_SIZE; - p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; - } else if (ph->p_type == PT_GNU_STACK) { - if (!runtime && ph->p_memsz > __default_stacksize) { - __default_stacksize = - ph->p_memsz < DEFAULT_STACK_MAX ? - ph->p_memsz : DEFAULT_STACK_MAX; - } - } - if (ph->p_type != PT_LOAD) continue; - if (ph->p_vaddr < min_addr) - min_addr = ph->p_vaddr; - if (ph->p_vaddr+ph->p_memsz > max_addr) - max_addr = ph->p_vaddr+ph->p_memsz; - } - min_addr &= -PAGE_SIZE; - max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE; - p->map = p->base + min_addr; - p->map_len = max_addr - min_addr; - p->kernel_mapped = 1; -} - -void __libc_exit_fini() -{ - struct dso *p; - size_t dyn[DYN_CNT]; - pthread_t self = __pthread_self(); - - /* Take both locks before setting shutting_down, so that - * either lock is sufficient to read its value. The lock - * order matches that in dlopen to avoid deadlock. */ - pthread_rwlock_wrlock(&lock); - pthread_mutex_lock(&init_fini_lock); - shutting_down = 1; - pthread_rwlock_unlock(&lock); - for (p=fini_head; p; p=p->fini_next) { - while (p->ctor_visitor && p->ctor_visitor!=self) - pthread_cond_wait(&ctor_cond, &init_fini_lock); - if (!p->constructed) continue; - decode_vec(p->dynv, dyn, DYN_CNT); - if (dyn[0] & (1<bfs_built) { - for (cnt=0; dso->deps[cnt]; cnt++) - dso->deps[cnt]->mark = 0; - cnt++; /* self, not included in deps */ - } else { - for (cnt=0, p=head; p; cnt++, p=p->next) - p->mark = 0; - } - cnt++; /* termination slot */ - if (dso==head && cnt <= countof(builtin_ctor_queue)) - queue = builtin_ctor_queue; - else - queue = calloc(cnt, sizeof *queue); - - if (!queue) { - error("Error allocating constructor queue: %m\n"); - if (runtime) longjmp(*rtld_fail, 1); - return 0; - } - - /* Opposite ends of the allocated buffer serve as an output queue - * and a working stack. Setup initial stack with just the argument - * dso and initial queue empty... */ - stack = queue; - qpos = 0; - spos = cnt; - stack[--spos] = dso; - dso->next_dep = 0; - dso->mark = 1; - - /* Then perform pseudo-DFS sort, but ignoring circular deps. */ - while (sposnext_dep < p->ndeps_direct) { - if (p->deps[p->next_dep]->mark) { - p->next_dep++; - } else { - stack[--spos] = p; - p = p->deps[p->next_dep]; - p->next_dep = 0; - p->mark = 1; - } - } - queue[qpos++] = p; - } - queue[qpos] = 0; - for (i=0; imark = 0; - for (i=0; ictor_visitor && queue[i]->ctor_visitor->tid < 0) { - error("State of %s is inconsistent due to multithreaded fork\n", - queue[i]->name); - free(queue); - if (runtime) longjmp(*rtld_fail, 1); - } - - return queue; -} - -static void do_init_fini(struct dso **queue) -{ - struct dso *p; - size_t dyn[DYN_CNT], i; - pthread_t self = __pthread_self(); - - pthread_mutex_lock(&init_fini_lock); - for (i=0; (p=queue[i]); i++) { - while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down) - pthread_cond_wait(&ctor_cond, &init_fini_lock); - if (p->ctor_visitor || p->constructed) - continue; - p->ctor_visitor = self; - - decode_vec(p->dynv, dyn, DYN_CNT); - if (dyn[0] & ((1<fini_next = fini_head; - fini_head = p; - } - - pthread_mutex_unlock(&init_fini_lock); - -#ifndef NO_LEGACY_INITFINI - if ((dyn[0] & (1<ctor_visitor = 0; - p->constructed = 1; - pthread_cond_broadcast(&ctor_cond); - } - pthread_mutex_unlock(&init_fini_lock); -} - -void __libc_start_init(void) -{ - do_init_fini(main_ctor_queue); - if (!__malloc_replaced && main_ctor_queue != builtin_ctor_queue) - free(main_ctor_queue); - main_ctor_queue = 0; -} - -static void dl_debug_state(void) -{ -} - -weak_alias(dl_debug_state, _dl_debug_state); - -void __init_tls(size_t *auxv) -{ -} - -static void update_tls_size() -{ - libc.tls_cnt = tls_cnt; - libc.tls_align = tls_align; - libc.tls_size = ALIGN( - (1+tls_cnt) * sizeof(void *) + - tls_offset + - sizeof(struct pthread) + - tls_align * 2, - tls_align); -} - -static void install_new_tls(void) -{ - sigset_t set; - pthread_t self = __pthread_self(), td; - struct dso *dtv_provider = container_of(tls_tail, struct dso, tls); - uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv; - struct dso *p; - size_t i, j; - size_t old_cnt = self->dtv[0]; - - __block_app_sigs(&set); - __tl_lock(); - /* Copy existing dtv contents from all existing threads. */ - for (i=0, td=self; !i || td!=self; i++, td=td->next) { - memcpy(newdtv+i, td->dtv, - (old_cnt+1)*sizeof(uintptr_t)); - newdtv[i][0] = tls_cnt; - } - /* Install new dtls into the enlarged, uninstalled dtv copies. */ - for (p=head; ; p=p->next) { - if (p->tls_id <= old_cnt) continue; - unsigned char *mem = p->new_tls; - for (j=0; jtls.image - (uintptr_t)mem) - & (p->tls.align-1); - memcpy(new, p->tls.image, p->tls.len); - newdtv[j][p->tls_id] = - (uintptr_t)new + DTP_OFFSET; - mem += p->tls.size + p->tls.align; - } - if (p->tls_id == tls_cnt) break; - } - - /* Broadcast barrier to ensure contents of new dtv is visible - * if the new dtv pointer is. The __membarrier function has a - * fallback emulation using signals for kernels that lack the - * feature at the syscall level. */ - - __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0); - - /* Install new dtv for each thread. */ - for (j=0, td=self; !j || td!=self; j++, td=td->next) { - td->dtv = newdtv[j]; - } - - __tl_unlock(); - __restore_sigs(&set); -} - -/* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the - * following stage 2 and stage 3 functions via primitive symbolic lookup - * since it does not have access to their addresses to begin with. */ - -/* Stage 2 of the dynamic linker is called after relative relocations - * have been processed. It can make function calls to static functions - * and access string literals and static data, but cannot use extern - * symbols. Its job is to perform symbolic relocations on the dynamic - * linker itself, but some of the relocations performed may need to be - * replaced later due to copy relocations in the main program. */ - -hidden void __dls2(unsigned char *base, size_t *sp) -{ - size_t *auxv; - for (auxv=sp+1+*sp+1; *auxv; auxv++); - auxv++; - if (DL_FDPIC) { - void *p1 = (void *)sp[-2]; - void *p2 = (void *)sp[-1]; - if (!p1) { - size_t aux[AUX_CNT]; - decode_vec(auxv, aux, AUX_CNT); - if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE]; - else ldso.base = (void *)(aux[AT_PHDR] & -4096); - } - app_loadmap = p2 ? p1 : 0; - ldso.loadmap = p2 ? p2 : p1; - ldso.base = laddr(&ldso, 0); - } else { - ldso.base = base; - } - Ehdr *ehdr = (void *)ldso.base; - ldso.name = ldso.shortname = "libc.so"; - ldso.phnum = ehdr->e_phnum; - ldso.phdr = laddr(&ldso, ehdr->e_phoff); - ldso.phentsize = ehdr->e_phentsize; - kernel_mapped_dso(&ldso); - decode_dyn(&ldso); - - if (DL_FDPIC) makefuncdescs(&ldso); - - /* Prepare storage for to save clobbered REL addends so they - * can be reused in stage 3. There should be very few. If - * something goes wrong and there are a huge number, abort - * instead of risking stack overflow. */ - size_t dyn[DYN_CNT]; - decode_vec(ldso.dynv, dyn, DYN_CNT); - size_t *rel = laddr(&ldso, dyn[DT_REL]); - size_t rel_size = dyn[DT_RELSZ]; - size_t symbolic_rel_cnt = 0; - apply_addends_to = rel; - for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) - if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++; - if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash(); - size_t addends[symbolic_rel_cnt+1]; - saved_addends = addends; - - head = &ldso; - reloc_all(&ldso); - - ldso.relocated = 0; - - /* Call dynamic linker stage-2b, __dls2b, looking it up - * symbolically as a barrier against moving the address - * load across the above relocation processing. */ - struct symdef dls2b_def = find_sym(&ldso, "__dls2b", 0); - if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp, auxv); - else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp, auxv); -} - -/* Stage 2b sets up a valid thread pointer, which requires relocations - * completed in stage 2, and on which stage 3 is permitted to depend. - * This is done as a separate stage, with symbolic lookup as a barrier, - * so that loads of the thread pointer and &errno can be pure/const and - * thereby hoistable. */ - -void __dls2b(size_t *sp, size_t *auxv) -{ - /* Setup early thread pointer in builtin_tls for ldso/libc itself to - * use during dynamic linking. If possible it will also serve as the - * thread pointer at runtime. */ - search_vec(auxv, &__hwcap, AT_HWCAP); - libc.auxv = auxv; - libc.tls_size = sizeof builtin_tls; - libc.tls_align = tls_align; - if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) { - a_crash(); - } - - struct symdef dls3_def = find_sym(&ldso, "__dls3", 0); - if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp, auxv); - else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp, auxv); -} - -/* Stage 3 of the dynamic linker is called with the dynamic linker/libc - * fully functional. Its job is to load (if not already loaded) and - * process dependencies and relocations for the main application and - * transfer control to its entry point. */ - -void __dls3(size_t *sp, size_t *auxv) -{ - static struct dso app, vdso; - size_t aux[AUX_CNT]; - size_t i; - char *env_preload=0; - char *replace_argv0=0; - size_t vdso_base; - int argc = *sp; - char **argv = (void *)(sp+1); - char **argv_orig = argv; - char **envp = argv+argc+1; - - /* Find aux vector just past environ[] and use it to initialize - * global data that may be needed before we can make syscalls. */ - __environ = envp; - decode_vec(auxv, aux, AUX_CNT); - search_vec(auxv, &__sysinfo, AT_SYSINFO); - __pthread_self()->sysinfo = __sysinfo; - libc.page_size = aux[AT_PAGESZ]; - libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] - || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]); - - /* Only trust user/env if kernel says we're not suid/sgid */ - if (!libc.secure) { - env_path = getenv("LD_LIBRARY_PATH"); - env_preload = getenv("LD_PRELOAD"); - } - - /* If the main program was already loaded by the kernel, - * AT_PHDR will point to some location other than the dynamic - * linker's program headers. */ - if (aux[AT_PHDR] != (size_t)ldso.phdr) { - size_t interp_off = 0; - size_t tls_image = 0; - /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */ - Phdr *phdr = app.phdr = (void *)aux[AT_PHDR]; - app.phnum = aux[AT_PHNUM]; - app.phentsize = aux[AT_PHENT]; - for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) { - if (phdr->p_type == PT_PHDR) - app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr); - else if (phdr->p_type == PT_INTERP) - interp_off = (size_t)phdr->p_vaddr; - else if (phdr->p_type == PT_TLS) { - tls_image = phdr->p_vaddr; - app.tls.len = phdr->p_filesz; - app.tls.size = phdr->p_memsz; - app.tls.align = phdr->p_align; - } - } - if (DL_FDPIC) app.loadmap = app_loadmap; - if (app.tls.size) app.tls.image = laddr(&app, tls_image); - if (interp_off) ldso.name = laddr(&app, interp_off); -#if 0 - if ((aux[0] & (1UL<= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1; - argv++; - while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') { - char *opt = argv[0]+2; - *argv++ = (void *)-1; - if (!*opt) { - break; - } else if (!memcmp(opt, "list", 5)) { - ldd_mode = 1; - } else if (!memcmp(opt, "library-path", 12)) { - if (opt[12]=='=') env_path = opt+13; - else if (opt[12]) *argv = 0; - else if (*argv) env_path = *argv++; - } else if (!memcmp(opt, "preload", 7)) { - if (opt[7]=='=') env_preload = opt+8; - else if (opt[7]) *argv = 0; - else if (*argv) env_preload = *argv++; - } else if (!memcmp(opt, "argv0", 5)) { - if (opt[5]=='=') replace_argv0 = opt+6; - else if (opt[5]) *argv = 0; - else if (*argv) replace_argv0 = *argv++; - } else { - argv[0] = 0; - } - } - argv[-1] = (void *)(argc - (argv-argv_orig)); - if (!argv[0]) { - dprintf(2, "musl libc (" LDSO_ARCH ")\n" - "Version %s\n" - "Dynamic Program Loader\n" - "Usage: %s [options] [--] pathname%s\n", - __libc_version, ldname, - ldd_mode ? "" : " [args]"); - _exit(1); - } - fd = open(argv[0], O_RDONLY); - if (fd < 0) { - dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno)); - _exit(1); - } - Ehdr *ehdr = map_library(fd, &app); - if (!ehdr) { - dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]); - _exit(1); - } - close(fd); - ldso.name = ldname; - app.name = argv[0]; - aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry); - /* Find the name that would have been used for the dynamic - * linker had ldd not taken its place. */ - if (ldd_mode) { - for (i=0; insegs = 1; - app.loadmap->segs[0].addr = (size_t)app.map; - app.loadmap->segs[0].p_vaddr = (size_t)app.map - - (size_t)app.base; - app.loadmap->segs[0].p_memsz = app.map_len; - } - argv[-3] = (void *)app.loadmap; - } - - /* Initial dso chain consists only of the app. */ - head = tail = syms_tail = &app; - - /* Donate unused parts of app and library mapping to malloc */ - reclaim_gaps(&app); - reclaim_gaps(&ldso); - - /* Load preload/needed libraries, add symbols to global namespace. */ - ldso.deps = (struct dso **)no_deps; - if (env_preload) load_preload(env_preload); - load_deps(&app); - for (struct dso *p=head; p; p=p->next) - add_syms(p); - - /* Attach to vdso, if provided by the kernel, last so that it does - * not become part of the global namespace. */ - if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR) && vdso_base) { - Ehdr *ehdr = (void *)vdso_base; - Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff); - vdso.phnum = ehdr->e_phnum; - vdso.phentsize = ehdr->e_phentsize; - for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) { - if (phdr->p_type == PT_DYNAMIC) - vdso.dynv = (void *)(vdso_base + phdr->p_offset); - if (phdr->p_type == PT_LOAD) - vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset); - } - vdso.name = ""; - vdso.shortname = "OHOS-vdso.so"; - vdso.relocated = 1; - vdso.deps = (struct dso **)no_deps; - decode_dyn(&vdso); - vdso.prev = tail; - tail->next = &vdso; - tail = &vdso; - } - - for (i=0; app.dynv[i]; i+=2) { - if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG) - app.dynv[i+1] = (size_t)&debug; - if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) { - size_t *ptr = (size_t *) app.dynv[i+1]; - *ptr = (size_t)&debug; - } - } - - /* This must be done before final relocations, since it calls - * malloc, which may be provided by the application. Calling any - * application code prior to the jump to its entry point is not - * valid in our model and does not work with FDPIC, where there - * are additional relocation-like fixups that only the entry point - * code can see to perform. */ - main_ctor_queue = queue_ctors(&app); - - /* Initial TLS must also be allocated before final relocations - * might result in calloc being a call to application code. */ - update_tls_size(); - void *initial_tls = builtin_tls; - if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) { - initial_tls = calloc(libc.tls_size, 1); - if (!initial_tls) { - dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n", - argv[0], libc.tls_size); - _exit(127); - } - } - static_tls_cnt = tls_cnt; - - /* The main program must be relocated LAST since it may contain - * copy relocations which depend on libraries' relocations. */ - reloc_all(app.next); - reloc_all(&app); - - /* Actual copying to new TLS needs to happen after relocations, - * since the TLS images might have contained relocated addresses. */ - if (initial_tls != builtin_tls) { - if (__init_tp(__copy_tls(initial_tls)) < 0) { - a_crash(); - } - } else { - size_t tmp_tls_size = libc.tls_size; - pthread_t self = __pthread_self(); - /* Temporarily set the tls size to the full size of - * builtin_tls so that __copy_tls will use the same layout - * as it did for before. Then check, just to be safe. */ - libc.tls_size = sizeof builtin_tls; - if (__copy_tls((void*)builtin_tls) != self) a_crash(); - libc.tls_size = tmp_tls_size; - } - - if (ldso_fail) _exit(127); - if (ldd_mode) _exit(0); - - /* Determine if malloc was interposed by a replacement implementation - * so that calloc and the memalign family can harden against the - * possibility of incomplete replacement. */ - if (find_sym(head, "malloc", 1).dso != &ldso) - __malloc_replaced = 1; - if (find_sym(head, "aligned_alloc", 1).dso != &ldso) - __aligned_alloc_replaced = 1; - - /* Switch to runtime mode: any further failures in the dynamic - * linker are a reportable failure rather than a fatal startup - * error. */ - runtime = 1; - - debug.ver = 1; - debug.bp = dl_debug_state; - debug.head = head; - debug.base = ldso.base; - debug.state = RT_CONSISTENT; - _dl_debug_state(); - - if (replace_argv0) argv[0] = replace_argv0; - - errno = 0; - - CRTJMP((void *)aux[AT_ENTRY], argv-1); - for(;;); -} - -static void prepare_lazy(struct dso *p) -{ - size_t dyn[DYN_CNT], n, flags1=0; - decode_vec(p->dynv, dyn, DYN_CNT); - search_vec(p->dynv, &flags1, DT_FLAGS_1); - if (dyn[DT_BIND_NOW] || (dyn[DT_FLAGS] & DF_BIND_NOW) || (flags1 & DF_1_NOW)) - return; - n = dyn[DT_RELSZ]/2 + dyn[DT_RELASZ]/3 + dyn[DT_PLTRELSZ]/2 + 1; - if (NEED_MIPS_GOT_RELOCS) { - size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM); - size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); - n += i-j; - } - p->lazy = calloc(n, 3*sizeof(size_t)); - if (!p->lazy) { - error("Error preparing lazy relocation for %s: %m", p->name); - longjmp(*rtld_fail, 1); - } - p->lazy_next = lazy_head; - lazy_head = p; -} - -void *dlopen(const char *file, int mode) -{ - struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next; - struct tls_module *orig_tls_tail; - size_t orig_tls_cnt, orig_tls_offset, orig_tls_align; - size_t i; - int cs; - jmp_buf jb; - struct dso **volatile ctor_queue = 0; - - if (mode & ~(RTLD_LAZY | RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL | RTLD_LOCAL | RTLD_NODELETE)) { - error("invalid mode parameter for dlopen()."); - return NULL; - } - - if ((mode & (RTLD_LAZY | RTLD_NOW)) == 0) { - error("invalid mode, one of RTLD_LAZY and RTLD_NOW must be set."); - return NULL; - } - - if (!file) return head; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - pthread_rwlock_wrlock(&lock); - __inhibit_ptc(); - - debug.state = RT_ADD; - _dl_debug_state(); - - p = 0; - if (shutting_down) { - error("Cannot dlopen while program is exiting."); - goto end; - } - orig_tls_tail = tls_tail; - orig_tls_cnt = tls_cnt; - orig_tls_offset = tls_offset; - orig_tls_align = tls_align; - orig_lazy_head = lazy_head; - orig_syms_tail = syms_tail; - orig_tail = tail; - noload = mode & RTLD_NOLOAD; - - rtld_fail = &jb; - if (setjmp(*rtld_fail)) { - /* Clean up anything new that was (partially) loaded */ - revert_syms(orig_syms_tail); - for (p=orig_tail->next; p; p=next) { - next = p->next; - while (p->td_index) { - void *tmp = p->td_index->next; - free(p->td_index); - p->td_index = tmp; - } - free(p->funcdescs); - if (p->rpath != p->rpath_orig) - free(p->rpath); - free(p->deps); - unmap_library(p); - free(p); - } - free(ctor_queue); - ctor_queue = 0; - if (!orig_tls_tail) libc.tls_head = 0; - tls_tail = orig_tls_tail; - if (tls_tail) tls_tail->next = 0; - tls_cnt = orig_tls_cnt; - tls_offset = orig_tls_offset; - tls_align = orig_tls_align; - lazy_head = orig_lazy_head; - tail = orig_tail; - tail->next = 0; - p = 0; - goto end; - } else p = load_library(file, head); - - if (!p) { - error(noload ? - "Library %s is not already loaded" : - "Error loading shared library %s: %m", - file); - goto end; - } - - /* First load handling */ - load_deps(p); - extend_bfs_deps(p); - pthread_mutex_lock(&init_fini_lock); - int constructed = p->constructed; - pthread_mutex_unlock(&init_fini_lock); - if (!constructed) ctor_queue = queue_ctors(p); - if (!p->relocated && (mode & RTLD_LAZY)) { - prepare_lazy(p); - for (i=0; p->deps[i]; i++) - if (!p->deps[i]->relocated) - prepare_lazy(p->deps[i]); - } - if (!p->relocated || (mode & RTLD_GLOBAL)) { - /* Make new symbols global, at least temporarily, so we can do - * relocations. If not RTLD_GLOBAL, this is reverted below. */ - add_syms(p); - for (i=0; p->deps[i]; i++) - add_syms(p->deps[i]); - } - if (!p->relocated) { - reloc_all(p); - } - - /* If RTLD_GLOBAL was not specified, undo any new additions - * to the global symbol table. This is a nop if the library was - * previously loaded and already global. */ - if (!(mode & RTLD_GLOBAL)) - revert_syms(orig_syms_tail); - - /* Processing of deferred lazy relocations must not happen until - * the new libraries are committed; otherwise we could end up with - * relocations resolved to symbol definitions that get removed. */ - redo_lazy_relocs(); - - update_tls_size(); - if (tls_cnt != orig_tls_cnt) - install_new_tls(); - orig_tail = tail; -end: - debug.state = RT_CONSISTENT; - _dl_debug_state(); - __release_ptc(); - if (p) gencnt++; - pthread_rwlock_unlock(&lock); - if (ctor_queue) { - do_init_fini(ctor_queue); - free(ctor_queue); - } - pthread_setcancelstate(cs, 0); - return p; -} - -hidden int __dl_invalid_handle(void *h) -{ - struct dso *p; - for (p=head; p; p=p->next) if (h==p) return 0; - error("Invalid library handle %p", (void *)h); - return 1; -} - -static void *addr2dso(size_t a) -{ - struct dso *p; - size_t i; - if (DL_FDPIC) for (p=head; p; p=p->next) { - i = count_syms(p); - if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs)) - return p; - } - for (p=head; p; p=p->next) { - if (DL_FDPIC && p->loadmap) { - for (i=0; iloadmap->nsegs; i++) { - if (a-p->loadmap->segs[i].p_vaddr - < p->loadmap->segs[i].p_memsz) - return p; - } - } else { - Phdr *ph = p->phdr; - size_t phcnt = p->phnum; - size_t entsz = p->phentsize; - size_t base = (size_t)p->base; - for (; phcnt--; ph=(void *)((char *)ph+entsz)) { - if (ph->p_type != PT_LOAD) continue; - if (a-base-ph->p_vaddr < ph->p_memsz) - return p; - } - if (a-(size_t)p->map < p->map_len) - return 0; - } - } - return 0; -} - -static void *do_dlsym(struct dso *p, const char *s, void *ra) -{ - int use_deps = 0; - if (p == head || p == RTLD_DEFAULT) { - p = head; - } else if (p == RTLD_NEXT) { - p = addr2dso((size_t)ra); - if (!p) p=head; - p = p->next; - } else if (__dl_invalid_handle(p)) { - return 0; - } else - use_deps = 1; - struct symdef def = find_sym2(p, s, 0, use_deps); - if (!def.sym) { - error("Symbol not found: %s", s); - return 0; - } - if ((def.sym->st_info&0xf) == STT_TLS) - return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET}); - if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC) - return def.dso->funcdescs + (def.sym - def.dso->syms); - return laddr(def.dso, def.sym->st_value); -} - -int dladdr(const void *addr_arg, Dl_info *info) -{ - size_t addr = (size_t)addr_arg; - struct dso *p; - Sym *sym, *bestsym; - uint32_t nsym; - char *strings; - size_t best = 0; - size_t besterr = -1; - - pthread_rwlock_rdlock(&lock); - p = addr2dso(addr); - pthread_rwlock_unlock(&lock); - - if (!p) return 0; - - sym = p->syms; - strings = p->strings; - nsym = count_syms(p); - - if (DL_FDPIC) { - size_t idx = (addr-(size_t)p->funcdescs) - / sizeof(*p->funcdescs); - if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) { - best = (size_t)(p->funcdescs + idx); - bestsym = sym + idx; - besterr = 0; - } - } - - if (!best) for (; nsym; nsym--, sym++) { - if (sym->st_value - && (1<<(sym->st_info&0xf) & OK_TYPES) - && (1<<(sym->st_info>>4) & OK_BINDS)) { - size_t symaddr = (size_t)laddr(p, sym->st_value); - if (symaddr > addr || symaddr <= best) - continue; - best = symaddr; - bestsym = sym; - besterr = addr - symaddr; - if (addr == symaddr) - break; - } - } - - if (best && besterr > bestsym->st_size-1) { - best = 0; - bestsym = 0; - } - - info->dli_fname = p->name; - info->dli_fbase = p->map; - - if (!best) { - info->dli_sname = 0; - info->dli_saddr = 0; - return 1; - } - - if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC) - best = (size_t)(p->funcdescs + (bestsym - p->syms)); - info->dli_sname = strings + bestsym->st_name; - info->dli_saddr = (void *)best; - - return 1; -} - -hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra) -{ - void *res; - pthread_rwlock_rdlock(&lock); - res = do_dlsym(p, s, ra); - pthread_rwlock_unlock(&lock); - return res; -} - -hidden void *__dlsym_redir_time64(void *restrict p, const char *restrict s, void *restrict ra) -{ -#if _REDIR_TIME64 - const char *suffix, *suffix2 = ""; - char redir[36]; - - /* Map the symbol name to a time64 version of itself according to the - * pattern used for naming the redirected time64 symbols. */ - size_t l = strnlen(s, sizeof redir); - if (l<4 || l==sizeof redir) goto no_redir; - if (s[l-2]=='_' && s[l-1]=='r') { - l -= 2; - suffix2 = s+l; - } - if (l<4) goto no_redir; - if (!strcmp(s+l-4, "time")) suffix = "64"; - else suffix = "_time64"; - - /* Use the presence of the remapped symbol name in libc to determine - * whether it's one that requires time64 redirection; replace if so. */ - snprintf(redir, sizeof redir, "__%.*s%s%s", (int)l, s, suffix, suffix2); - if (find_sym(&ldso, redir, 1).sym) s = redir; -no_redir: -#endif - return __dlsym(p, s, ra); -} - -int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data) -{ - struct dso *current; - struct dl_phdr_info info; - int ret = 0; - for(current = head; current;) { - info.dlpi_addr = (uintptr_t)current->base; - info.dlpi_name = current->name; - info.dlpi_phdr = current->phdr; - info.dlpi_phnum = current->phnum; - info.dlpi_adds = gencnt; - info.dlpi_subs = 0; - info.dlpi_tls_modid = current->tls_id; - info.dlpi_tls_data = !current->tls_id ? 0 : - __tls_get_addr((tls_mod_off_t[]){current->tls_id,0}); - - ret = (callback)(&info, sizeof (info), data); - - if (ret != 0) break; - - pthread_rwlock_rdlock(&lock); - current = current->next; - pthread_rwlock_unlock(&lock); - } - return ret; -} - -static void error(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - if (!runtime) { - vdprintf(2, fmt, ap); - dprintf(2, "\n"); - ldso_fail = 1; - va_end(ap); - return; - } - __dl_vseterr(fmt, ap); - va_end(ap); -} diff --git a/porting/liteos_a/user/src/aio/aio.c b/porting/liteos_a/user/src/aio/aio.c deleted file mode 100644 index 7e8bc6add..000000000 --- a/porting/liteos_a/user/src/aio/aio.c +++ /dev/null @@ -1,424 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#include "atomic.h" -#include "pthread_impl.h" -#include "aio_impl.h" - -#define malloc __libc_malloc -#define calloc __libc_calloc -#define realloc __libc_realloc -#define free __libc_free - -/* The following is a threads-based implementation of AIO with minimal - * dependence on implementation details. Most synchronization is - * performed with pthread primitives, but atomics and futex operations - * are used for notification in a couple places where the pthread - * primitives would be inefficient or impractical. - * - * For each fd with outstanding aio operations, an aio_queue structure - * is maintained. These are reference-counted and destroyed by the last - * aio worker thread to exit. Accessing any member of the aio_queue - * structure requires a lock on the aio_queue. Adding and removing aio - * queues themselves requires a write lock on the global map object, - * a 4-level table mapping file descriptor numbers to aio queues. A - * read lock on the map is used to obtain locks on existing queues by - * excluding destruction of the queue by a different thread while it is - * being locked. - * - * Each aio queue has a list of active threads/operations. Presently there - * is a one to one relationship between threads and operations. The only - * members of the aio_thread structure which are accessed by other threads - * are the linked list pointers, op (which is immutable), running (which - * is updated atomically), and err (which is synchronized via running), - * so no locking is necessary. Most of the other other members are used - * for sharing data between the main flow of execution and cancellation - * cleanup handler. - * - * Taking any aio locks requires having all signals blocked. This is - * necessary because aio_cancel is needed by close, and close is required - * to be async-signal safe. All aio worker threads run with all signals - * blocked permanently. - */ - -struct aio_thread { - pthread_t td; - struct aiocb *cb; - struct aio_thread *next, *prev; - struct aio_queue *q; - volatile int running; - int err, op; - ssize_t ret; -}; - -struct aio_queue { - int fd, seekable, append, ref, init; - pthread_mutex_t lock; - pthread_cond_t cond; - struct aio_thread *head; -}; - -struct aio_args { - struct aiocb *cb; - struct aio_queue *q; - int op; - sem_t sem; -}; - -static pthread_rwlock_t maplock = PTHREAD_RWLOCK_INITIALIZER; -static struct aio_queue *****map; -static volatile int aio_fd_cnt; -volatile int __aio_fut; - -static size_t io_thread_stack_size; - -#define MAX(a,b) ((a)>(b) ? (a) : (b)) -static struct aio_queue *__aio_get_queue(int fd, int need) -{ - if (fd < 0) { - errno = EBADF; - return 0; - } - int a=fd>>24; - unsigned char b=fd>>16, c=fd>>8, d=fd; - struct aio_queue *q = 0; - pthread_rwlock_rdlock(&maplock); - if ((!map || !map[a] || !map[a][b] || !map[a][b][c] || !(q=map[a][b][c][d])) && need) { - pthread_rwlock_unlock(&maplock); - if (fcntl(fd, F_GETFD) < 0) return 0; - pthread_rwlock_wrlock(&maplock); - if (!io_thread_stack_size) { - unsigned long val = __getauxval(AT_MINSIGSTKSZ); - io_thread_stack_size = MAX(MINSIGSTKSZ+2048, val+512); - } - if (!map) map = calloc(sizeof *map, (-1U/2+1)>>24); - if (!map) goto out; - if (!map[a]) map[a] = calloc(sizeof **map, 256); - if (!map[a]) goto out; - if (!map[a][b]) map[a][b] = calloc(sizeof ***map, 256); - if (!map[a][b]) goto out; - if (!map[a][b][c]) map[a][b][c] = calloc(sizeof ****map, 256); - if (!map[a][b][c]) goto out; - if (!(q = map[a][b][c][d])) { - map[a][b][c][d] = q = calloc(sizeof *****map, 1); - if (q) { - q->fd = fd; - pthread_mutex_init(&q->lock, 0); - pthread_cond_init(&q->cond, 0); - a_inc(&aio_fd_cnt); - } - } - } - if (q) pthread_mutex_lock(&q->lock); -out: - pthread_rwlock_unlock(&maplock); - return q; -} - -static void __aio_unref_queue(struct aio_queue *q) -{ - if (q->ref > 1) { - q->ref--; - pthread_mutex_unlock(&q->lock); - return; - } - - /* This is potentially the last reference, but a new reference - * may arrive since we cannot free the queue object without first - * taking the maplock, which requires releasing the queue lock. */ - pthread_mutex_unlock(&q->lock); - pthread_rwlock_wrlock(&maplock); - pthread_mutex_lock(&q->lock); - if (q->ref == 1) { - int fd=q->fd; - int a=fd>>24; - unsigned char b=fd>>16, c=fd>>8, d=fd; - map[a][b][c][d] = 0; - a_dec(&aio_fd_cnt); - pthread_rwlock_unlock(&maplock); - pthread_mutex_unlock(&q->lock); - free(q); - } else { - q->ref--; - pthread_rwlock_unlock(&maplock); - pthread_mutex_unlock(&q->lock); - } -} - -static void cleanup(void *ctx) -{ - struct aio_thread *at = ctx; - struct aio_queue *q = at->q; - struct aiocb *cb = at->cb; - struct sigevent sev = cb->aio_sigevent; - - /* There are four potential types of waiters we could need to wake: - * 1. Callers of aio_cancel/close. - * 2. Callers of aio_suspend with a single aiocb. - * 3. Callers of aio_suspend with a list. - * 4. AIO worker threads waiting for sequenced operations. - * Types 1-3 are notified via atomics/futexes, mainly for AS-safety - * considerations. Type 4 is notified later via a cond var. */ - - cb->__ret = at->ret; - if (a_swap(&at->running, 0) < 0) - __wake(&at->running, -1, 1); - if (a_swap(&cb->__err, at->err) != EINPROGRESS) - __wake(&cb->__err, -1, 1); - if (a_swap(&__aio_fut, 0)) - __wake(&__aio_fut, -1, 1); - - pthread_mutex_lock(&q->lock); - - if (at->next) at->next->prev = at->prev; - if (at->prev) at->prev->next = at->next; - else q->head = at->next; - - /* Signal aio worker threads waiting for sequenced operations. */ - pthread_cond_broadcast(&q->cond); - - __aio_unref_queue(q); - - if (sev.sigev_notify == SIGEV_SIGNAL) { - siginfo_t si = { - .si_signo = sev.sigev_signo, - .si_value = sev.sigev_value, - .si_code = SI_ASYNCIO, - .si_pid = getpid(), - .si_uid = getuid() - }; - __syscall(SYS_rt_sigqueueinfo, si.si_pid, si.si_signo, &si); - } - if (sev.sigev_notify == SIGEV_THREAD) { - a_store(&__pthread_self()->cancel, 0); - sev.sigev_notify_function(sev.sigev_value); - } -} - -static void *io_thread_func(void *ctx) -{ - struct aio_thread at, *p; - - struct aio_args *args = ctx; - struct aiocb *cb = args->cb; - int fd = cb->aio_fildes; - int op = args->op; - void *buf = (void *)cb->aio_buf; - size_t len = cb->aio_nbytes; - off_t off = cb->aio_offset; - - struct aio_queue *q = args->q; - ssize_t ret; - - pthread_mutex_lock(&q->lock); - sem_post(&args->sem); - - at.op = op; - at.running = 1; - at.ret = -1; - at.err = ECANCELED; - at.q = q; - at.td = __pthread_self(); - at.cb = cb; - at.prev = 0; - if ((at.next = q->head)) at.next->prev = &at; - q->head = &at; - - if (!q->init) { - int seekable = lseek(fd, 0, SEEK_CUR) >= 0; - q->seekable = seekable; - q->append = !seekable || (fcntl(fd, F_GETFL) & O_APPEND); - q->init = 1; - } - - pthread_cleanup_push(cleanup, &at); - - /* Wait for sequenced operations. */ - if (op!=LIO_READ && (op!=LIO_WRITE || q->append)) { - for (;;) { - for (p=at.next; p && p->op!=LIO_WRITE; p=p->next); - if (!p) break; - pthread_cond_wait(&q->cond, &q->lock); - } - } - - pthread_mutex_unlock(&q->lock); - - switch (op) { - case LIO_WRITE: - ret = q->append ? write(fd, buf, len) : pwrite(fd, buf, len, off); - break; - case LIO_READ: - ret = !q->seekable ? read(fd, buf, len) : pread(fd, buf, len, off); - break; - case O_SYNC: - ret = fsync(fd); - break; - case O_DSYNC: - ret = fdatasync(fd); - break; - } - at.ret = ret; - at.err = ret<0 ? errno : 0; - - pthread_cleanup_pop(1); - - return 0; -} - -static int submit(struct aiocb *cb, int op) -{ - int ret = 0; - pthread_attr_t a; - sigset_t allmask, origmask; - pthread_t td; - struct aio_queue *q = __aio_get_queue(cb->aio_fildes, 1); - struct aio_args args = { .cb = cb, .op = op, .q = q }; - sem_init(&args.sem, 0, 0); - - if (!q) { - if (errno != EBADF) errno = EAGAIN; - cb->__ret = -1; - cb->__err = errno; - return -1; - } - q->ref++; - pthread_mutex_unlock(&q->lock); - - if (cb->aio_sigevent.sigev_notify == SIGEV_THREAD) { - if (cb->aio_sigevent.sigev_notify_attributes) - a = *cb->aio_sigevent.sigev_notify_attributes; - else - pthread_attr_init(&a); - } else { - pthread_attr_init(&a); - pthread_attr_setstacksize(&a, io_thread_stack_size); - pthread_attr_setguardsize(&a, 0); - } - pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); - sigfillset(&allmask); - pthread_sigmask(SIG_BLOCK, &allmask, &origmask); - cb->__err = EINPROGRESS; - if (pthread_create(&td, &a, io_thread_func, &args)) { - pthread_mutex_lock(&q->lock); - __aio_unref_queue(q); - cb->__err = errno = EAGAIN; - cb->__ret = ret = -1; - } - pthread_sigmask(SIG_SETMASK, &origmask, 0); - - if (!ret) { - while (sem_wait(&args.sem)); - } - - return ret; -} - -int aio_read(struct aiocb *cb) -{ - unsupported_api(__FUNCTION__); - return submit(cb, LIO_READ); -} - -int aio_write(struct aiocb *cb) -{ - unsupported_api(__FUNCTION__); - return submit(cb, LIO_WRITE); -} - -int aio_fsync(int op, struct aiocb *cb) -{ - unsupported_api(__FUNCTION__); - if (op != O_SYNC && op != O_DSYNC) { - errno = EINVAL; - return -1; - } - return submit(cb, op); -} - -ssize_t aio_return(struct aiocb *cb) -{ - unsupported_api(__FUNCTION__); - return cb->__ret; -} - -int aio_error(const struct aiocb *cb) -{ - unsupported_api(__FUNCTION__); - a_barrier(); - return cb->__err & 0x7fffffff; -} - -int aio_cancel(int fd, struct aiocb *cb) -{ - sigset_t allmask, origmask; - int ret = AIO_ALLDONE; - struct aio_thread *p; - struct aio_queue *q; - - unsupported_api(__FUNCTION__); - /* Unspecified behavior case. Report an error. */ - if (cb && fd != cb->aio_fildes) { - errno = EINVAL; - return -1; - } - - sigfillset(&allmask); - pthread_sigmask(SIG_BLOCK, &allmask, &origmask); - - errno = ENOENT; - if (!(q = __aio_get_queue(fd, 0))) { - if (errno == EBADF) ret = -1; - goto done; - } - - for (p = q->head; p; p = p->next) { - if (cb && cb != p->cb) continue; - /* Transition target from running to running-with-waiters */ - if (a_cas(&p->running, 1, -1)) { - pthread_cancel(p->td); - __wait(&p->running, 0, -1, 1); - if (p->err == ECANCELED) ret = AIO_CANCELED; - } - } - - pthread_mutex_unlock(&q->lock); -done: - pthread_sigmask(SIG_SETMASK, &origmask, 0); - return ret; -} - -int __aio_close(int fd) -{ - a_barrier(); - if (aio_fd_cnt) aio_cancel(fd, 0); - return fd; -} - -void __aio_atfork(int who) -{ - if (who<0) { - pthread_rwlock_rdlock(&maplock); - return; - } - if (who>0 && map) for (int a=0; a<(-1U/2+1)>>24; a++) - if (map[a]) for (int b=0; b<256; b++) - if (map[a][b]) for (int c=0; c<256; c++) - if (map[a][b][c]) for (int d=0; d<256; d++) - map[a][b][c][d] = 0; - pthread_rwlock_unlock(&maplock); -} - -weak_alias(aio_cancel, aio_cancel64); -weak_alias(aio_error, aio_error64); -weak_alias(aio_fsync, aio_fsync64); -weak_alias(aio_read, aio_read64); -weak_alias(aio_write, aio_write64); -weak_alias(aio_return, aio_return64); diff --git a/porting/liteos_a/user/src/aio/aio_suspend.c b/porting/liteos_a/user/src/aio/aio_suspend.c deleted file mode 100644 index cb41920c4..000000000 --- a/porting/liteos_a/user/src/aio/aio_suspend.c +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include -#include -#include -#include "atomic.h" -#include "pthread_impl.h" -#include "aio_impl.h" - -int aio_suspend(const struct aiocb *const cbs[], int cnt, const struct timespec *ts) -{ - int i, tid = 0, ret, expect = 0; - struct timespec at; - volatile int dummy_fut, *pfut; - int nzcnt = 0; - const struct aiocb *cb = 0; - - unsupported_api(__FUNCTION__); - pthread_testcancel(); - - if (cnt<0) { - errno = EINVAL; - return -1; - } - - for (i=0; itv_sec; - if ((at.tv_nsec += ts->tv_nsec) >= 1000000000) { - at.tv_nsec -= 1000000000; - at.tv_sec++; - } - } - - for (;;) { - for (i=0; i__err; - expect = EINPROGRESS | 0x80000000; - a_cas(pfut, EINPROGRESS, expect); - break; - default: - pfut = &__aio_fut; - if (!tid) tid = __pthread_self()->tid; - expect = a_cas(pfut, 0, tid); - if (!expect) expect = tid; - /* Need to recheck the predicate before waiting. */ - for (i=0; i -#include -#include -#include -#include "pthread_impl.h" -#include - -struct lio_state { - struct sigevent *sev; - int cnt; - struct aiocb *cbs[]; -}; - -static int lio_wait(struct lio_state *st) -{ - int i, err, got_err = 0; - int cnt = st->cnt; - struct aiocb **cbs = st->cbs; - - for (;;) { - for (i=0; isigev_signo, - .si_value = sev->sigev_value, - .si_code = SI_ASYNCIO, - .si_pid = getpid(), - .si_uid = getuid() - }; - __syscall(SYS_rt_sigqueueinfo, si.si_pid, si.si_signo, &si); -} - -static void *wait_thread(void *p) -{ - struct lio_state *st = p; - struct sigevent *sev = st->sev; - lio_wait(st); - free(st); - switch (sev->sigev_notify) { - case SIGEV_SIGNAL: - notify_signal(sev); - break; - case SIGEV_THREAD: - sev->sigev_notify_function(sev->sigev_value); - break; - } - return 0; -} - -int lio_listio(int mode, struct aiocb *restrict const *restrict cbs, int cnt, struct sigevent *restrict sev) -{ - unsupported_api(__FUNCTION__); - - int i, ret; - struct lio_state *st=0; - - if (cnt < 0) { - errno = EINVAL; - return -1; - } - - if (mode == LIO_WAIT || (sev && sev->sigev_notify != SIGEV_NONE)) { - if (!(st = malloc(sizeof *st + cnt*sizeof *cbs))) { - errno = EAGAIN; - return -1; - } - st->cnt = cnt; - st->sev = sev; - memcpy(st->cbs, (void*) cbs, cnt*sizeof *cbs); - } - - for (i=0; iaio_lio_opcode) { - case LIO_READ: - ret = aio_read(cbs[i]); - break; - case LIO_WRITE: - ret = aio_write(cbs[i]); - break; - default: - continue; - } - if (ret) { - free(st); - errno = EAGAIN; - return -1; - } - } - - if (mode == LIO_WAIT) { - ret = lio_wait(st); - free(st); - return ret; - } - - if (st) { - pthread_attr_t a; - sigset_t set, set_old; - pthread_t td; - - if (sev->sigev_notify == SIGEV_THREAD) { - if (sev->sigev_notify_attributes) - a = *sev->sigev_notify_attributes; - else - pthread_attr_init(&a); - } else { - pthread_attr_init(&a); - pthread_attr_setstacksize(&a, PAGE_SIZE); - pthread_attr_setguardsize(&a, 0); - } - pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); - sigfillset(&set); - pthread_sigmask(SIG_BLOCK, &set, &set_old); - if (pthread_create(&td, &a, wait_thread, st)) { - free(st); - errno = EAGAIN; - return -1; - } - pthread_sigmask(SIG_SETMASK, &set_old, 0); - } - - return 0; -} - -weak_alias(lio_listio, lio_listio64); diff --git a/porting/liteos_a/user/src/conf/confstr.c b/porting/liteos_a/user/src/conf/confstr.c deleted file mode 100644 index 3fa863c73..000000000 --- a/porting/liteos_a/user/src/conf/confstr.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include - -size_t confstr(int name, char *buf, size_t len) -{ - const char *s = ""; - - unsupported_api(__FUNCTION__); - if (!name) { - s = "/bin:/usr/bin"; - } else if ((name&~4U)!=1 && name-_CS_POSIX_V6_ILP32_OFF32_CFLAGS>33U) { - errno = EINVAL; - return 0; - } - // snprintf is overkill but avoid wasting code size to implement - // this completely useless function and its truncation semantics - return snprintf(buf, len, "%s", s) + 1; -} diff --git a/porting/liteos_a/user/src/conf/sysconf.c b/porting/liteos_a/user/src/conf/sysconf.c deleted file mode 100644 index 645f6487c..000000000 --- a/porting/liteos_a/user/src/conf/sysconf.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#include "libc.h" - -#define JT(x) (-256|(x)) -#define VER JT(1) -#define JT_ARG_MAX JT(2) -#define JT_MQ_PRIO_MAX JT(3) -#define JT_PAGE_SIZE JT(4) -#define JT_SEM_VALUE_MAX JT(5) -#define JT_NPROCESSORS_CONF JT(6) -#define JT_NPROCESSORS_ONLN JT(7) -#define JT_PHYS_PAGES JT(8) -#define JT_AVPHYS_PAGES JT(9) -#define JT_ZERO JT(10) -#define JT_DELAYTIMER_MAX JT(11) - -#define RLIM(x) (-32768|(RLIMIT_ ## x)) - -long sysconf(int name) -{ - return syscall(SYS_sysconf, name); -} diff --git a/porting/liteos_a/user/src/crypt/encrypt.c b/porting/liteos_a/user/src/crypt/encrypt.c deleted file mode 100644 index 5a3f6503a..000000000 --- a/porting/liteos_a/user/src/crypt/encrypt.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include -#include - -#include "crypt_des.h" - -static struct expanded_key __encrypt_key; - -void setkey(const char *key) -{ - unsigned char bkey[8]; - int i, j; - - unsupported_api(__FUNCTION__); - for (i = 0; i < 8; i++) { - bkey[i] = 0; - for (j = 7; j >= 0; j--, key++) - bkey[i] |= (uint32_t)(*key & 1) << j; - } - - __des_setkey(bkey, &__encrypt_key); -} - -void encrypt(char *block, int edflag) -{ - struct expanded_key decrypt_key, *key; - uint32_t b[2]; - int i, j; - char *p; - - unsupported_api(__FUNCTION__); - p = block; - for (i = 0; i < 2; i++) { - b[i] = 0; - for (j = 31; j >= 0; j--, p++) - b[i] |= (uint32_t)(*p & 1) << j; - } - - key = &__encrypt_key; - if (edflag) { - key = &decrypt_key; - for (i = 0; i < 16; i++) { - decrypt_key.l[i] = __encrypt_key.l[15-i]; - decrypt_key.r[i] = __encrypt_key.r[15-i]; - } - } - - __do_des(b[0], b[1], b, b + 1, 1, 0, key); - - p = block; - for (i = 0; i < 2; i++) - for (j = 31; j >= 0; j--) - *p++ = b[i]>>j & 1; -} diff --git a/porting/liteos_a/user/src/ctype/wcswidth.c b/porting/liteos_a/user/src/ctype/wcswidth.c deleted file mode 100644 index ed0f9f204..000000000 --- a/porting/liteos_a/user/src/ctype/wcswidth.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -int wcswidth(const wchar_t *wcs, size_t n) -{ - int l=0, k=0; - unsupported_api(__FUNCTION__); - for (; n-- && *wcs && (k = wcwidth(*wcs)) >= 0; l+=k, wcs++); - return (k < 0) ? k : l; -} diff --git a/porting/liteos_a/user/src/dirent/__dirent.h b/porting/liteos_a/user/src/dirent/__dirent.h deleted file mode 100644 index 5d79b9657..000000000 --- a/porting/liteos_a/user/src/dirent/__dirent.h +++ /dev/null @@ -1,12 +0,0 @@ -struct __dirstream -{ - off_t tell; - int fd; - int buf_pos; - int buf_end; - volatile int lock[1]; - /* Any changes to this struct must preserve the property: - * offsetof(struct __dirent, buf) % sizeof(off_t) == 0 */ - // change buf len from 2048 to 4096 to support read 14 dirs at one readdir syscall - char buf[4096]; -}; diff --git a/porting/liteos_a/user/src/env/__init_tls.c b/porting/liteos_a/user/src/env/__init_tls.c deleted file mode 100644 index c01a8ba8a..000000000 --- a/porting/liteos_a/user/src/env/__init_tls.c +++ /dev/null @@ -1,185 +0,0 @@ -#define SYSCALL_NO_TLS 1 -#include -#include -#include -#include -#include -#include "pthread_impl.h" -#include "libc.h" -#include "atomic.h" -#include "syscall.h" -#include "stdio_impl.h" -#include "lock.h" - -volatile int __thread_list_lock; - -static void *dummy_tsd[1] = { 0 }; -weak_alias(dummy_tsd, __pthread_tsd_main); -static FILE *volatile dummy_file = 0; -weak_alias(dummy_file, __stdin_used); -weak_alias(dummy_file, __stdout_used); -weak_alias(dummy_file, __stderr_used); - -static void dummy_0() -{ -} -weak_alias(dummy_0, __membarrier_init); - -static void init_file_lock(FILE *f) -{ - if (f && f->lock<0) f->lock = 0; -} - -int __init_tp(void *p) -{ - pthread_t td = p; - int r = __set_thread_area(TP_ADJ(p)); - if (r < 0) return -1; - if (!r) libc.can_do_threads = 1; - libc.threaded = 0; - td->self = td; - td->detach_state = DT_JOINABLE; - td->tid = __syscall(SYS_gettid); - td->locale = &libc.global_locale; - td->robust_list.head = &td->robust_list.head; - td->sysinfo = __sysinfo; - td->next = td->prev = td; - td->tsd = (void **)__pthread_tsd_main; - for (FILE *f=*__ofl_lock(); f; f=f->next) - init_file_lock(f); - __ofl_unlock(); - init_file_lock(__stdin_used); - init_file_lock(__stdout_used); - init_file_lock(__stderr_used); - __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8); -#if 0 - __membarrier_init(); -#endif - libc.threaded = 1; - return 0; -} - -static struct builtin_tls { - char c; - struct pthread pt; - void *space[16]; -} builtin_tls[1]; -#define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt) - -static struct tls_module main_tls; - -void *__copy_tls(unsigned char *mem) -{ - pthread_t td; - struct tls_module *p; - size_t i; - uintptr_t *dtv; - -#ifdef TLS_ABOVE_TP - dtv = (uintptr_t*)(mem + libc.tls_size) - (libc.tls_cnt + 1); - - mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1); - td = (pthread_t)mem; - mem += sizeof(struct pthread); - - for (i=1, p=libc.tls_head; p; i++, p=p->next) { - dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET; - memcpy(mem + p->offset, p->image, p->len); - } -#else - dtv = (uintptr_t *)mem; - - mem += libc.tls_size - sizeof(struct pthread); - mem -= (uintptr_t)mem & (libc.tls_align-1); - td = (pthread_t)mem; - - for (i=1, p=libc.tls_head; p; i++, p=p->next) { - dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET; - memcpy(mem - p->offset, p->image, p->len); - } -#endif - dtv[0] = libc.tls_cnt; - td->dtv = dtv; - return td; -} - -#if ULONG_MAX == 0xffffffff -typedef Elf32_Phdr Phdr; -#else -typedef Elf64_Phdr Phdr; -#endif - -extern weak hidden const size_t _DYNAMIC[]; - -static void static_init_tls(size_t *aux) -{ - unsigned char *p; - size_t n; - Phdr *phdr, *tls_phdr=0; - size_t base = 0; - void *mem; - - for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) { - phdr = (void *)p; - if (phdr->p_type == PT_PHDR) - base = aux[AT_PHDR] - phdr->p_vaddr; - if (phdr->p_type == PT_DYNAMIC && _DYNAMIC) - base = (size_t)_DYNAMIC - phdr->p_vaddr; - if (phdr->p_type == PT_TLS) - tls_phdr = phdr; - if (phdr->p_type == PT_GNU_STACK && - phdr->p_memsz > __default_stacksize) - __default_stacksize = - phdr->p_memsz < DEFAULT_STACK_MAX ? - phdr->p_memsz : DEFAULT_STACK_MAX; - } - - if (tls_phdr) { - main_tls.image = (void *)(base + tls_phdr->p_vaddr); - main_tls.len = tls_phdr->p_filesz; - main_tls.size = tls_phdr->p_memsz; - main_tls.align = tls_phdr->p_align; - libc.tls_cnt = 1; - libc.tls_head = &main_tls; - } - - main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image) - & (main_tls.align-1); -#ifdef TLS_ABOVE_TP - main_tls.offset = GAP_ABOVE_TP; - main_tls.offset += (-GAP_ABOVE_TP + (uintptr_t)main_tls.image) - & (main_tls.align-1); -#else - main_tls.offset = main_tls.size; -#endif - if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN; - - libc.tls_align = main_tls.align; - libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread) -#ifdef TLS_ABOVE_TP - + main_tls.offset -#endif - + main_tls.size + main_tls.align - + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN; - - if (libc.tls_size > sizeof builtin_tls) { -#ifndef SYS_mmap2 -#define SYS_mmap2 SYS_mmap -#endif - mem = (void *)__syscall( - SYS_mmap2, - 0, libc.tls_size, PROT_READ|PROT_WRITE, - MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); - /* -4095...-1 cast to void * will crash on dereference anyway, - * so don't bloat the init code checking for error codes and - * explicitly calling a_crash(). */ - } else { - mem = builtin_tls; - } - - /* Failure to initialize thread pointer is always fatal. */ - if (__init_tp(__copy_tls(mem)) < 0) - a_crash(); -} - -weak_alias(static_init_tls, __init_tls); diff --git a/porting/liteos_a/user/src/env/__libc_start_main.c b/porting/liteos_a/user/src/env/__libc_start_main.c deleted file mode 100644 index 78ce1249c..000000000 --- a/porting/liteos_a/user/src/env/__libc_start_main.c +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include -#include -#include -#include -#include "syscall.h" -#include "atomic.h" -#include "libc.h" - -static void dummy(void) {} -weak_alias(dummy, _init); - -extern weak hidden void (*const __init_array_start)(void), (*const __init_array_end)(void); - -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] -#include -#include "syscall.h" -#include "pthread_impl.h" -#include "atomic.h" -#include "lock.h" -#include "ksigaction.h" - -_Noreturn void abort(void) -{ - sigset_t set, pending; - sigemptyset(&set); - sigaddset(&set, SIGABRT); - - sigpending(&pending); - __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, &set, 0, _NSIG / 8); - if (!sigismember(&pending, SIGABRT)) { - raise(SIGABRT); - } - /* If there was a SIGABRT handler installed and it returned, or if - * SIGABRT was blocked or ignored, take an AS-safe lock to prevent - * sigaction from installing a new SIGABRT handler, uninstall any - * handler that may be present, and re-raise the signal to generate - * the default action of abnormal termination. */ - __block_all_sigs(0); - LOCK(__abort_lock); - signal(SIGABRT, SIG_DFL); - __syscall(SYS_tkill, __pthread_self()->tid, SIGABRT); - __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, - &(long[_NSIG/(8*sizeof(long))]){1UL<<(SIGABRT-1)}, 0, _NSIG/8); - - /* Beyond this point should be unreachable. */ - a_crash(); - raise(SIGKILL); - _Exit(127); -} diff --git a/porting/liteos_a/user/src/exit/exit.c b/porting/liteos_a/user/src/exit/exit.c deleted file mode 100644 index bee135dfc..000000000 --- a/porting/liteos_a/user/src/exit/exit.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include -#include -#include -#include "syscall.h" -#include "libc.h" -#include - -pthread_mutex_t __exit_mutex = PTHREAD_MUTEX_INITIALIZER; - -static void dummy() -{ -} - -/* atexit.c and __stdio_exit.c override these. the latter is linked - * as a consequence of linking either __toread.c or __towrite.c. */ -weak_alias(dummy, __funcs_on_exit); -weak_alias(dummy, __stdio_exit); -weak_alias(dummy, _fini); - -extern weak hidden void (*const __fini_array_start)(void), (*const __fini_array_end)(void); - -static void libc_exit_fini(void) -{ - uintptr_t a = (uintptr_t)&__fini_array_end; - for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)())) - (*(void (**)())(a-sizeof(void(*)())))(); - _fini(); -} - -weak_alias(libc_exit_fini, __libc_exit_fini); - -_Noreturn void exit(int code) -{ - sigset_t set; - - __block_app_sigs(&set); - - int ret = pthread_mutex_trylock(&__exit_mutex); - if (ret == EBUSY) { - pthread_exit(NULL); - } - - __funcs_on_exit(); - __libc_exit_fini(); - __stdio_exit(); - _Exit(code); -} diff --git a/porting/liteos_a/user/src/fcntl/fcntl.c b/porting/liteos_a/user/src/fcntl/fcntl.c deleted file mode 100644 index 37391e053..000000000 --- a/porting/liteos_a/user/src/fcntl/fcntl.c +++ /dev/null @@ -1,47 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include "syscall.h" - -int fcntl(int fd, int cmd, ...) -{ - unsigned long arg; - va_list ap; - va_start(ap, cmd); - arg = va_arg(ap, unsigned long); - va_end(ap); - if (cmd == F_SETLKW) return syscall_cp(SYS_fcntl, fd, cmd, (void *)arg); - if (cmd == F_GETOWN) { - struct f_owner_ex ex; - int ret = __syscall(SYS_fcntl, fd, F_GETOWN_EX, &ex); - if (ret == -EINVAL) return __syscall(SYS_fcntl, fd, cmd, (void *)arg); - if (ret) return __syscall_ret(ret); - return ex.type == F_OWNER_PGRP ? -ex.pid : ex.pid; - } - if (cmd == F_DUPFD_CLOEXEC) { - int ret = __syscall(SYS_fcntl, fd, F_DUPFD_CLOEXEC, arg); - if (ret != -EINVAL) { - if (ret >= 0) - __syscall(SYS_fcntl, ret, F_SETFD, FD_CLOEXEC); - return __syscall_ret(ret); - } - ret = __syscall(SYS_fcntl, fd, F_DUPFD_CLOEXEC, 0); - if (ret != -EINVAL) { - if (ret >= 0) __syscall(SYS_close, ret); - return __syscall_ret(-EINVAL); - } - ret = __syscall(SYS_fcntl, fd, F_DUPFD, arg); - if (ret >= 0) __syscall(SYS_fcntl, ret, F_SETFD, FD_CLOEXEC); - return __syscall_ret(ret); - } - switch (cmd) { - case F_SETLK: - case F_GETLK: - case F_GETOWN_EX: - case F_SETOWN_EX: - return syscall(SYS_fcntl, fd, cmd, (void *)arg); - default: - return syscall(SYS_fcntl, fd, cmd, arg); - } -} diff --git a/porting/liteos_a/user/src/fcntl/posix_fadvise.c b/porting/liteos_a/user/src/fcntl/posix_fadvise.c deleted file mode 100644 index 641dc56f7..000000000 --- a/porting/liteos_a/user/src/fcntl/posix_fadvise.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -#include "syscall.h" - -int posix_fadvise(int fd, off_t base, off_t len, int advice) -{ - unsupported_api(__FUNCTION__); -#if defined(SYSCALL_FADVISE_6_ARG) - /* Some archs, at least arm and powerpc, have the syscall - * arguments reordered to avoid needing 7 argument registers - * due to 64-bit argument alignment. */ - return -__syscall(SYS_fadvise, fd, advice, - __SYSCALL_LL_E(base), __SYSCALL_LL_E(len)); -#else - return -__syscall(SYS_fadvise, fd, __SYSCALL_LL_O(base), - __SYSCALL_LL_E(len), advice); -#endif -} - -weak_alias(posix_fadvise, posix_fadvise64); diff --git a/porting/liteos_a/user/src/internal/libc.h b/porting/liteos_a/user/src/internal/libc.h deleted file mode 100644 index 3a5eac946..000000000 --- a/porting/liteos_a/user/src/internal/libc.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef LIBC_H -#define LIBC_H - -#include -#include -#include - -struct __locale_map; - -struct __locale_struct { - const struct __locale_map *cat[6]; -}; - -struct tls_module { - struct tls_module *next; - void *image; - size_t len, size, align, offset; -}; - -struct __libc { - char can_do_threads; - char threaded; - char secure; - volatile signed char need_locks; - int threads_minus_1; - size_t *auxv; - struct tls_module *tls_head; - size_t tls_size, tls_align, tls_cnt; - size_t page_size; - struct __locale_struct global_locale; - int exit; -}; - -#ifndef PAGE_SIZE -#define PAGE_SIZE libc.page_size -#endif - -extern hidden struct __libc __libc; -#define libc __libc - -hidden void __init_libc(char **, char *); -hidden void __init_tls(size_t *); -hidden void __init_ssp(void *); -hidden void __libc_start_init(void); -hidden void __funcs_on_exit(void); -hidden void __funcs_on_quick_exit(void); -hidden void __libc_exit_fini(void); -hidden void __fork_handler(int); -hidden void __sig_init(void); -hidden void arm_do_signal(int); - -extern hidden size_t __hwcap; -extern hidden size_t __sysinfo; -extern char *__progname, *__progname_full; - -extern hidden const char __libc_version[]; - -hidden void __synccall(void (*)(void *), void *); -hidden int __setxid(int, int, int, int); - -#endif diff --git a/porting/liteos_a/user/src/internal/pthread_impl.h b/porting/liteos_a/user/src/internal/pthread_impl.h deleted file mode 100644 index ca0aa531b..000000000 --- a/porting/liteos_a/user/src/internal/pthread_impl.h +++ /dev/null @@ -1,213 +0,0 @@ -#ifndef _PTHREAD_IMPL_H -#define _PTHREAD_IMPL_H - -#include -#include -#include -#include -#include -#include "libc.h" -#include "syscall.h" -#include "atomic.h" -#include "futex.h" - -#include "pthread_arch.h" - -#define pthread __pthread - -struct pthread { - /* Part 1 -- these fields may be external or - * internal (accessed via asm) ABI. Do not change. */ - struct pthread *self; -#ifndef TLS_ABOVE_TP - uintptr_t *dtv; -#endif - struct pthread *prev, *next; /* non-ABI */ - uintptr_t sysinfo; -#ifndef TLS_ABOVE_TP -#ifdef CANARY_PAD - uintptr_t canary_pad; -#endif - uintptr_t canary; -#endif - - /* Part 2 -- implementation details, non-ABI. */ - int tid; - int errno_val; - volatile int detach_state; - volatile int cancel; - volatile unsigned char canceldisable, cancelasync; - unsigned char tsd_used:1; - unsigned char dlerror_flag:1; - unsigned char *map_base; - size_t map_size; - void *stack; - size_t stack_size; - size_t guard_size; - void *result; - struct __ptcb *cancelbuf; - void **tsd; - struct { - volatile void *volatile head; - long off; - volatile void *volatile pending; - } robust_list; - int h_errno_val; - volatile int timer_id; - locale_t locale; - volatile int killlock[1]; - char *dlerror_buf; - void *stdio_locks; - - /* Part 3 -- the positions of these fields relative to - * the end of the structure is external and internal ABI. */ -#ifdef TLS_ABOVE_TP - uintptr_t canary; - uintptr_t *dtv; -#endif -}; - -enum { - DT_EXITED = 0, - DT_EXITING, - DT_JOINABLE, - DT_DETACHED, -}; - -#define __SU (sizeof(size_t)/sizeof(int)) - -#define _a_stacksize __u.__s[0] -#define _a_guardsize __u.__s[1] -#define _a_stackaddr __u.__s[2] -#define _a_detach __u.__i[3*__SU+0] -#define _a_sched __u.__i[3*__SU+1] -#define _a_policy __u.__i[3*__SU+2] -#define _a_prio __u.__i[3*__SU+3] -#define _a_runtime __u.__i[3*__SU+3] -#define _a_deadline __u.__i[3*__SU+4] -#define _a_period __u.__i[3*__SU+5] -#define _m_type __u.__i[0] -#define _m_lock __u.__vi[1] -#define _m_waiters __u.__vi[2] -#define _m_prev __u.__p[3] -#define _m_next __u.__p[4] -#define _m_count __u.__i[5] -#define _c_shared __u.__p[0] -#define _c_seq __u.__vi[2] -#define _c_waiters __u.__vi[3] -#define _c_clock __u.__i[4] -#define _c_lock __u.__vi[8] -#define _c_head __u.__p[1] -#define _c_tail __u.__p[5] -#define _rw_lock __u.__vi[0] -#define _rw_waiters __u.__vi[1] -#define _rw_shared __u.__i[2] -#define _b_lock __u.__vi[0] -#define _b_waiters __u.__vi[1] -#define _b_limit __u.__i[2] -#define _b_count __u.__vi[3] -#define _b_waiters2 __u.__vi[4] -#define _b_inst __u.__p[3] - -#ifndef TP_OFFSET -#define TP_OFFSET 0 -#endif - -#ifndef DTP_OFFSET -#define DTP_OFFSET 0 -#endif - -#ifdef TLS_ABOVE_TP -#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + TP_OFFSET) -#else -#define TP_ADJ(p) (p) -#endif - -#ifndef tls_mod_off_t -#define tls_mod_off_t size_t -#endif - -#define SIGTIMER 32 -#define SIGCANCEL 33 -#define SIGSYNCCALL 34 - -#define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 }) -#define SIGPT_SET \ - ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ - [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) }) -#define SIGTIMER_SET \ - ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ - 0x80000000 }) - -void *__tls_get_addr(tls_mod_off_t *); -hidden int __init_tp(void *); -hidden void *__copy_tls(unsigned char *); -hidden void __reset_tls(); - -hidden void __membarrier_init(void); -hidden void __dl_thread_cleanup(void); -hidden void __testcancel(); -hidden void __do_cleanup_push(struct __ptcb *); -hidden void __do_cleanup_pop(struct __ptcb *); -hidden void __pthread_tsd_run_dtors(); - -hidden void __pthread_key_delete_synccall(void (*)(void *), void *); -hidden int __pthread_key_delete_impl(pthread_key_t); - -extern hidden volatile size_t __pthread_tsd_size; -extern hidden void *__pthread_tsd_main[]; -extern hidden volatile int __eintr_valid_flag; - -hidden int __clone(int (*)(void *), void *, int, void *, ...); -hidden int __thread_clone(int (*func)(void *), int flags, struct pthread *thread, unsigned char *sp); -hidden int __set_thread_area(void *); -hidden int __libc_sigaction(int, const struct sigaction *, struct sigaction *); -hidden void __unmapself(void *, size_t); - -hidden int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int); -hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int); -hidden void __wait(volatile int *, volatile int *, int, int); -static inline void __wake(volatile void *addr, int cnt, int priv) -{ - if (priv) priv = FUTEX_PRIVATE; - if (cnt<0) cnt = INT_MAX; - __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS || - __syscall(SYS_futex, addr, FUTEX_WAKE, cnt); -} -static inline void __futexwait(volatile void *addr, int val, int priv) -{ - if (priv) priv = FUTEX_PRIVATE; - __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0xffffffffu) != -ENOSYS || - __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0xffffffffu); -} - -hidden void __acquire_ptc(void); -hidden void __release_ptc(void); -hidden void __inhibit_ptc(void); - -hidden void __tl_lock(void); -hidden void __tl_unlock(void); -hidden void __tl_sync(pthread_t); - -extern hidden volatile int __thread_list_lock; - -extern hidden volatile int __abort_lock[1]; - -extern hidden unsigned __default_stacksize; -extern hidden unsigned __default_guardsize; - -#define DEFAULT_STACK_SIZE 131072 -#define DEFAULT_GUARD_SIZE 8192 - -#define DEFAULT_STACK_MAX (8<<20) -#define DEFAULT_GUARD_MAX (1<<20) - -#define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1) - -#define MUSL_TYPE_THREAD (-1) -#define MUSL_TYPE_PROCESS (0) - -#define PTHREAD_MUTEX_TYPE_MASK 3 -#define PTHREAD_PRIORITY_LOWEST 31 - -#endif diff --git a/porting/liteos_a/user/src/internal/unsupported_api.h b/porting/liteos_a/user/src/internal/unsupported_api.h deleted file mode 100644 index aa48b46f8..000000000 --- a/porting/liteos_a/user/src/internal/unsupported_api.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef UNSUPPORTED_API_H -#define UNSUPPORTED_API_H - -#include - -static inline void unsupported_api(const char *func) -{ - fprintf(stderr, "[ERR]Unsupported API %s\n", func); -} - -#endif diff --git a/porting/liteos_a/user/src/ipc/msgctl.c b/porting/liteos_a/user/src/ipc/msgctl.c deleted file mode 100644 index 971fb0ee0..000000000 --- a/porting/liteos_a/user/src/ipc/msgctl.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include - -#include "syscall.h" -#include "ipc.h" - -#if __BYTE_ORDER != __BIG_ENDIAN -#undef SYSCALL_IPC_BROKEN_MODE -#endif - -int msgctl(int q, int cmd, struct msqid_ds *buf) -{ -#if IPC_TIME64 - struct msqid_ds out, *orig; - if (cmd&IPC_TIME64) { - out = (struct msqid_ds){0}; - orig = buf; - buf = &out; - } -#endif - unsupported_api(__FUNCTION__); -#ifdef SYSCALL_IPC_BROKEN_MODE - struct msqid_ds tmp; - if (cmd == IPC_SET) { - tmp = *buf; - tmp.msg_perm.mode *= 0x10000U; - buf = &tmp; - } -#endif -#ifndef SYS_ipc - int r = __syscall(SYS_msgctl, q, IPC_CMD(cmd), buf); -#else - int r = __syscall(SYS_ipc, IPCOP_msgctl, q, IPC_CMD(cmd), 0, buf, 0); -#endif -#ifdef SYSCALL_IPC_BROKEN_MODE - if (r >= 0) switch (cmd | IPC_TIME64) { - case IPC_STAT: - case MSG_STAT: - case MSG_STAT_ANY: - buf->msg_perm.mode >>= 16; - } -#endif -#if IPC_TIME64 - if (r >= 0 && (cmd&IPC_TIME64)) { - buf = orig; - *buf = out; - IPC_HILO(buf, msg_stime); - IPC_HILO(buf, msg_rtime); - IPC_HILO(buf, msg_ctime); - } -#endif - return __syscall_ret(r); -} diff --git a/porting/liteos_a/user/src/ipc/msgget.c b/porting/liteos_a/user/src/ipc/msgget.c deleted file mode 100644 index 9ff7a053b..000000000 --- a/porting/liteos_a/user/src/ipc/msgget.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -#include "syscall.h" -#include "ipc.h" - -int msgget(key_t k, int flag) -{ - unsupported_api(__FUNCTION__); -#ifndef SYS_ipc - return syscall(SYS_msgget, k, flag); -#else - return syscall(SYS_ipc, IPCOP_msgget, k, flag); -#endif -} diff --git a/porting/liteos_a/user/src/ipc/msgrcv.c b/porting/liteos_a/user/src/ipc/msgrcv.c deleted file mode 100644 index c66eb52fa..000000000 --- a/porting/liteos_a/user/src/ipc/msgrcv.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -#include "syscall.h" -#include "ipc.h" - -ssize_t msgrcv(int q, void *m, size_t len, long type, int flag) -{ - unsupported_api(__FUNCTION__); -#ifndef SYS_ipc - return syscall_cp(SYS_msgrcv, q, m, len, type, flag); -#else - return syscall_cp(SYS_ipc, IPCOP_msgrcv, q, len, flag, ((long[]){ (long)m, type })); -#endif -} diff --git a/porting/liteos_a/user/src/ipc/msgsnd.c b/porting/liteos_a/user/src/ipc/msgsnd.c deleted file mode 100644 index 4db4d4ef4..000000000 --- a/porting/liteos_a/user/src/ipc/msgsnd.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include "syscall.h" -#include "ipc.h" - -int msgsnd(int q, const void *m, size_t len, int flag) -{ - unsupported_api(__FUNCTION__); -#ifndef SYS_ipc - return syscall_cp(SYS_msgsnd, q, m, len, flag); -#else - return syscall_cp(SYS_ipc, IPCOP_msgsnd, q, len, flag, m); -#endif -} diff --git a/porting/liteos_a/user/src/ipc/semctl.c b/porting/liteos_a/user/src/ipc/semctl.c deleted file mode 100644 index 953daf091..000000000 --- a/porting/liteos_a/user/src/ipc/semctl.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include - -#include "syscall.h" -#include "ipc.h" - -#if __BYTE_ORDER != __BIG_ENDIAN -#undef SYSCALL_IPC_BROKEN_MODE -#endif - -union semun { - int val; - struct semid_ds *buf; - unsigned short *array; -}; - -int semctl(int id, int num, int cmd, ...) -{ - unsupported_api(__FUNCTION__); - union semun arg = {0}; - va_list ap; - switch (cmd & ~IPC_TIME64) { - case SETVAL: case GETALL: case SETALL: case IPC_SET: - case IPC_INFO: case SEM_INFO: - case IPC_STAT & ~IPC_TIME64: - case SEM_STAT & ~IPC_TIME64: - case SEM_STAT_ANY & ~IPC_TIME64: - va_start(ap, cmd); - arg = va_arg(ap, union semun); - va_end(ap); - } -#if IPC_TIME64 - struct semid_ds out, *orig; - if (cmd&IPC_TIME64) { - out = (struct semid_ds){0}; - orig = arg.buf; - arg.buf = &out; - } -#endif -#ifdef SYSCALL_IPC_BROKEN_MODE - struct semid_ds tmp; - if (cmd == IPC_SET) { - tmp = *arg.buf; - tmp.sem_perm.mode *= 0x10000U; - arg.buf = &tmp; - } -#endif -#ifndef SYS_ipc - int r = __syscall(SYS_semctl, id, num, IPC_CMD(cmd), arg.buf); -#else - int r = __syscall(SYS_ipc, IPCOP_semctl, id, num, IPC_CMD(cmd), &arg.buf); -#endif -#ifdef SYSCALL_IPC_BROKEN_MODE - if (r >= 0) switch (cmd | IPC_TIME64) { - case IPC_STAT: - case SEM_STAT: - case SEM_STAT_ANY: - arg.buf->sem_perm.mode >>= 16; - } -#endif -#if IPC_TIME64 - if (r >= 0 && (cmd&IPC_TIME64)) { - arg.buf = orig; - *arg.buf = out; - IPC_HILO(arg.buf, sem_otime); - IPC_HILO(arg.buf, sem_ctime); - } -#endif - return __syscall_ret(r); -} diff --git a/porting/liteos_a/user/src/ipc/semget.c b/porting/liteos_a/user/src/ipc/semget.c deleted file mode 100644 index 858d5bd61..000000000 --- a/porting/liteos_a/user/src/ipc/semget.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include -#include - -#include "syscall.h" -#include "ipc.h" - -int semget(key_t key, int n, int fl) -{ - /* The kernel uses the wrong type for the sem_nsems member - * of struct semid_ds, and thus might not check that the - * n fits in the correct (per POSIX) userspace type, so - * we have to check here. */ - unsupported_api(__FUNCTION__); - if (n > USHRT_MAX) return __syscall_ret(-EINVAL); -#ifndef SYS_ipc - return syscall(SYS_semget, key, n, fl); -#else - return syscall(SYS_ipc, IPCOP_semget, key, n, fl); -#endif -} diff --git a/porting/liteos_a/user/src/ipc/semop.c b/porting/liteos_a/user/src/ipc/semop.c deleted file mode 100644 index 1c96614fb..000000000 --- a/porting/liteos_a/user/src/ipc/semop.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -#include "syscall.h" -#include "ipc.h" - -int semop(int id, struct sembuf *buf, size_t n) -{ - unsupported_api(__FUNCTION__); -#ifndef SYS_ipc - return syscall(SYS_semop, id, buf, n); -#else - return syscall(SYS_ipc, IPCOP_semop, id, n, 0, buf); -#endif -} diff --git a/porting/liteos_a/user/src/legacy/daemon.c b/porting/liteos_a/user/src/legacy/daemon.c deleted file mode 100644 index 57838e781..000000000 --- a/porting/liteos_a/user/src/legacy/daemon.c +++ /dev/null @@ -1,35 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include - -int daemon(int nochdir, int noclose) -{ - unsupported_api(__FUNCTION__); - if (!nochdir && chdir("/")) - return -1; - if (!noclose) { - int fd, failed = 0; - if ((fd = open("/dev/null", O_RDWR)) < 0) return -1; - if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0) - failed++; - if (fd > 2) close(fd); - if (failed) return -1; - } - - switch(fork()) { - case 0: break; - case -1: return -1; - default: _exit(0); - } - - if (setsid() < 0) return -1; - - switch(fork()) { - case 0: break; - case -1: return -1; - default: _exit(0); - } - - return 0; -} diff --git a/porting/liteos_a/user/src/legacy/getdtablesize.c b/porting/liteos_a/user/src/legacy/getdtablesize.c deleted file mode 100644 index 672e0556c..000000000 --- a/porting/liteos_a/user/src/legacy/getdtablesize.c +++ /dev/null @@ -1,13 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include - -int getdtablesize(void) -{ - struct rlimit rl; - unsupported_api(__FUNCTION__); - getrlimit(RLIMIT_NOFILE, &rl); - return rl.rlim_cur < INT_MAX ? rl.rlim_cur : INT_MAX; -} diff --git a/porting/liteos_a/user/src/legacy/getusershell.c b/porting/liteos_a/user/src/legacy/getusershell.c deleted file mode 100644 index c17ada773..000000000 --- a/porting/liteos_a/user/src/legacy/getusershell.c +++ /dev/null @@ -1,34 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include - -static const char defshells[] = "/bin/sh\n/bin/csh\n"; - -static char *line; -static size_t linesize; -static FILE *f; - -void endusershell(void) -{ - unsupported_api(__FUNCTION__); - if (f) fclose(f); - f = 0; -} - -void setusershell(void) -{ - if (!f) f = fopen("/etc/shells", "rbe"); - if (!f) f = fmemopen((void *)defshells, sizeof defshells - 1, "rb"); -} - -char *getusershell(void) -{ - ssize_t l; - if (!f) setusershell(); - if (!f) return 0; - l = getline(&line, &linesize, f); - if (l <= 0) return 0; - if (line[l-1]=='\n') line[l-1]=0; - return line; -} diff --git a/porting/liteos_a/user/src/legacy/isastream.c b/porting/liteos_a/user/src/legacy/isastream.c deleted file mode 100644 index 9398088e5..000000000 --- a/porting/liteos_a/user/src/legacy/isastream.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include -#include -int isastream(int fd) -{ - unsupported_api(__FUNCTION__); - return fcntl(fd, F_GETFD) < 0 ? -1 : 0; -} diff --git a/porting/liteos_a/user/src/legacy/ulimit.c b/porting/liteos_a/user/src/legacy/ulimit.c deleted file mode 100644 index 5580bf885..000000000 --- a/porting/liteos_a/user/src/legacy/ulimit.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include -#include - -long ulimit(int cmd, ...) -{ - struct rlimit rl; - unsupported_api(__FUNCTION__); - getrlimit(RLIMIT_FSIZE, &rl); - if (cmd == UL_SETFSIZE) { - long val; - va_list ap; - va_start(ap, cmd); - val = va_arg(ap, long); - va_end(ap); - rl.rlim_cur = 512ULL * val; - if (setrlimit(RLIMIT_FSIZE, &rl)) return -1; - } - return rl.rlim_cur / 512; -} diff --git a/porting/liteos_a/user/src/legacy/utmpx.c b/porting/liteos_a/user/src/legacy/utmpx.c deleted file mode 100644 index a9f05a2a4..000000000 --- a/porting/liteos_a/user/src/legacy/utmpx.c +++ /dev/null @@ -1,61 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include - -void endutxent(void) -{ - unsupported_api(__FUNCTION__); -} - -void setutxent(void) -{ - unsupported_api(__FUNCTION__); -} - -struct utmpx *getutxent(void) -{ - unsupported_api(__FUNCTION__); - return NULL; -} - -struct utmpx *getutxid(const struct utmpx *ut) -{ - unsupported_api(__FUNCTION__); - return NULL; -} - -struct utmpx *getutxline(const struct utmpx *ut) -{ - unsupported_api(__FUNCTION__); - return NULL; -} - -struct utmpx *pututxline(const struct utmpx *ut) -{ - unsupported_api(__FUNCTION__); - return NULL; -} - -void updwtmpx(const char *f, const struct utmpx *u) -{ - unsupported_api(__FUNCTION__); -} - -static int __utmpxname(const char *f) -{ - unsupported_api(__FUNCTION__); - errno = ENOTSUP; - return -1; -} - -weak_alias(endutxent, endutent); -weak_alias(setutxent, setutent); -weak_alias(getutxent, getutent); -weak_alias(getutxid, getutid); -weak_alias(getutxline, getutline); -weak_alias(pututxline, pututline); -weak_alias(updwtmpx, updwtmp); -weak_alias(__utmpxname, utmpname); -weak_alias(__utmpxname, utmpxname); diff --git a/porting/liteos_a/user/src/linux/adjtime.c b/porting/liteos_a/user/src/linux/adjtime.c deleted file mode 100644 index 871623a96..000000000 --- a/porting/liteos_a/user/src/linux/adjtime.c +++ /dev/null @@ -1,29 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include "syscall.h" -#include - -int adjtime(const struct timeval *in, struct timeval *out) -{ - struct timex tx = { 0 }; - unsupported_api(__FUNCTION__); - if (in) { - if (in->tv_sec > 1000 || in->tv_usec > 1000000000) { - errno = EINVAL; - return -1; - } - tx.offset = in->tv_sec*1000000 + in->tv_usec; - tx.modes = ADJ_OFFSET_SINGLESHOT; - } - if (adjtimex(&tx) < 0) return -1; - if (out) { - out->tv_sec = tx.offset / 1000000; - if ((out->tv_usec = tx.offset % 1000000) < 0) { - out->tv_sec--; - out->tv_usec += 1000000; - } - } - return 0; -} diff --git a/porting/liteos_a/user/src/linux/clone.c b/porting/liteos_a/user/src/linux/clone.c deleted file mode 100644 index c937f8fa5..000000000 --- a/porting/liteos_a/user/src/linux/clone.c +++ /dev/null @@ -1,80 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include "pthread_impl.h" -#include "syscall.h" -#include "libc.h" - -struct __clone_args { - int (*func)(void *); - void *arg; -}; - -static int __start_child(void *clone_args) -{ - int status; - sigset_t set; - int (*func)(void *) = ((struct __clone_args *)clone_args)->func; - void *arg = ((struct __clone_args *)clone_args)->arg; - - __block_all_sigs(&set); - pthread_t self = __pthread_self(); - self->tid = __syscall(SYS_gettid); - self->robust_list.off = 0; - self->robust_list.pending = 0; - self->next = self->prev = self; - __thread_list_lock = 0; - libc.threads_minus_1 = 0; - libc.exit = 0; - signal(SIGSYS, arm_do_signal); - __restore_sigs(&set); - - status = func(arg); - exit(status); -} - -int clone(int (*func)(void *), void *stack, int flags, void *arg, ...) -{ - int ret; - va_list ap; - pid_t *ptid, *ctid; - void *tls; - void *mmap_stack = NULL; - void *clone_stack = stack; - int (*clone_func)(void *) = func; - struct __clone_args *clone_args = (struct __clone_args *)arg; - - va_start(ap, arg); - ptid = va_arg(ap, pid_t *); - tls = va_arg(ap, void *); - ctid = va_arg(ap, pid_t *); - va_end(ap); - - if (!(flags & (CLONE_VM | CLONE_VFORK))) { - if (func) { - if (!stack) { - mmap_stack = __mmap(0, __default_stacksize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (mmap_stack == MAP_FAILED) { - errno = ENOMEM; - return -1; - } - clone_args = (struct __clone_args *)((char *)mmap_stack + __default_stacksize); - } else { - clone_args = (struct __clone_args *)stack; - } - clone_args = (struct __clone_args *)((uintptr_t)clone_args - ((uintptr_t)clone_args % sizeof(uintptr_t))); - clone_args = (struct __clone_args *)((char *)clone_args - sizeof(struct __clone_args)); - clone_args->func = clone_func; - clone_args->arg = arg; - clone_func = __start_child; - clone_stack = (void *)clone_args; - } - } - ret = __syscall_ret(__clone(clone_func, clone_stack, flags, (void *)clone_args, ptid, tls, ctid)); - if (mmap_stack) { - __munmap(mmap_stack, __default_stacksize); - } - return ret; -} diff --git a/porting/liteos_a/user/src/linux/utimes.c b/porting/liteos_a/user/src/linux/utimes.c deleted file mode 100644 index b6f6df064..000000000 --- a/porting/liteos_a/user/src/linux/utimes.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include "fcntl.h" -#include "syscall.h" - -int utimes(const char *path, const struct timeval times[2]) -{ - unsupported_api(__FUNCTION__); - return __futimesat(AT_FDCWD, path, times); -} diff --git a/porting/liteos_a/user/src/locale/freelocale.c b/porting/liteos_a/user/src/locale/freelocale.c deleted file mode 100644 index 183965585..000000000 --- a/porting/liteos_a/user/src/locale/freelocale.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include "locale_impl.h" - -#define malloc undef -#define calloc undef -#define realloc undef -#define free __libc_free - -void freelocale(locale_t l) -{ - unsupported_api(__FUNCTION__); - if (__loc_is_allocated(l)) free(l); -} - -weak_alias(freelocale, __freelocale); diff --git a/porting/liteos_a/user/src/locale/strcoll.c b/porting/liteos_a/user/src/locale/strcoll.c deleted file mode 100644 index 355a8aa5e..000000000 --- a/porting/liteos_a/user/src/locale/strcoll.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include "locale_impl.h" - -int __strcoll_l(const char *l, const char *r, locale_t loc) -{ - return strcmp(l, r); -} - -int strcoll(const char *l, const char *r) -{ - return strcmp(l, r); -} - -weak_alias(__strcoll_l, strcoll_l); diff --git a/porting/liteos_a/user/src/malloc/oldmalloc/malloc.c b/porting/liteos_a/user/src/malloc/oldmalloc/malloc.c deleted file mode 100644 index 5bd3de4d4..000000000 --- a/porting/liteos_a/user/src/malloc/oldmalloc/malloc.c +++ /dev/null @@ -1,556 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include "libc.h" -#include "atomic.h" -#include "pthread_impl.h" -#include "malloc_impl.h" -#include "fork_impl.h" - -#define malloc __libc_malloc_impl -#define realloc __libc_realloc -#define free __libc_free - -#if defined(__GNUC__) && defined(__PIC__) -#define inline inline __attribute__((always_inline)) -#endif - -static struct { - volatile uint64_t binmap; - struct bin bins[64]; - volatile int split_merge_lock[2]; -} mal; - -/* Synchronization tools */ - -static inline void lock(volatile int *lk) -{ - int need_locks = libc.need_locks; - if (need_locks) { - while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1); - if (need_locks < 0) libc.need_locks = 0; - } -} - -static inline void unlock(volatile int *lk) -{ - if (lk[0]) { - a_store(lk, 0); - if (lk[1]) __wake(lk, 1, 1); - } -} - -static inline void lock_bin(int i) -{ - lock(mal.bins[i].lock); - if (!mal.bins[i].head) - mal.bins[i].head = mal.bins[i].tail = BIN_TO_CHUNK(i); -} - -static inline void unlock_bin(int i) -{ - unlock(mal.bins[i].lock); -} - -static int first_set(uint64_t x) -{ -#if 1 - return a_ctz_64(x); -#else - static const char debruijn64[64] = { - 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, - 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, - 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, - 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 - }; - static const char debruijn32[32] = { - 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, - 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 - }; - if (sizeof(long) < 8) { - uint32_t y = x; - if (!y) { - y = x>>32; - return 32 + debruijn32[(y&-y)*0x076be629 >> 27]; - } - return debruijn32[(y&-y)*0x076be629 >> 27]; - } - return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58]; -#endif -} - -static const unsigned char bin_tab[60] = { - 32,33,34,35,36,36,37,37,38,38,39,39, - 40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43, - 44,44,44,44,44,44,44,44,45,45,45,45,45,45,45,45, - 46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47, -}; - -static int bin_index(size_t x) -{ - x = x / SIZE_ALIGN - 1; - if (x <= 32) return x; - if (x < 512) return bin_tab[x/8-4]; - if (x > 0x1c00) return 63; - return bin_tab[x/128-4] + 16; -} - -static int bin_index_up(size_t x) -{ - x = x / SIZE_ALIGN - 1; - if (x <= 32) return x; - x--; - if (x < 512) return bin_tab[x/8-4] + 1; - return bin_tab[x/128-4] + 17; -} - -#if 0 -void __dump_heap(int x) -{ - struct chunk *c; - int i; - for (c = (void *)mal.heap; CHUNK_SIZE(c); c = NEXT_CHUNK(c)) - fprintf(stderr, "base %p size %zu (%d) flags %d/%d\n", - c, CHUNK_SIZE(c), bin_index(CHUNK_SIZE(c)), - c->csize & 15, - NEXT_CHUNK(c)->psize & 15); - for (i=0; i<64; i++) { - if (mal.bins[i].head != BIN_TO_CHUNK(i) && mal.bins[i].head) { - fprintf(stderr, "bin %d: %p\n", i, mal.bins[i].head); - if (!(mal.binmap & 1ULL< len ? b-len : 0; - if (new>a && old len ? b-len : 0; - if (new>a && old SIZE_MAX/2 - PAGE_SIZE) { - errno = ENOMEM; - return 0; - } - n += -n & PAGE_SIZE-1; - - if (!brk) { - brk = __syscall(SYS_brk, 0); - brk += -brk & PAGE_SIZE-1; - } - - if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n) - && __syscall(SYS_brk, brk+n)==brk+n) { - *pn = n; - brk += n; - return (void *)(brk-n); - } - - size_t min = (size_t)PAGE_SIZE << mmap_step/2; - if (n < min) n = min; - void *area = __mmap(0, n, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (area == MAP_FAILED) return 0; - *pn = n; - mmap_step++; - return area; -} - -static struct chunk *expand_heap(size_t n) -{ - static void *end; - void *p; - struct chunk *w; - - /* The argument n already accounts for the caller's chunk - * overhead needs, but if the heap can't be extended in-place, - * we need room for an extra zero-sized sentinel chunk. */ - n += SIZE_ALIGN; - - p = __expand_heap(&n); - if (!p) return 0; - - /* If not just expanding existing space, we need to make a - * new sentinel chunk below the allocated space. */ - if (p != end) { - /* Valid/safe because of the prologue increment. */ - n -= SIZE_ALIGN; - p = (char *)p + SIZE_ALIGN; - w = MEM_TO_CHUNK(p); - w->psize = 0 | C_INUSE; - } - - /* Record new heap end and fill in footer. */ - end = (char *)p + n; - w = MEM_TO_CHUNK(end); - w->psize = n | C_INUSE; - w->csize = 0 | C_INUSE; - - /* Fill in header, which may be new or may be replacing a - * zero-size sentinel header at the old end-of-heap. */ - w = MEM_TO_CHUNK(p); - w->csize = n | C_INUSE; - - return w; -} - -static int adjust_size(size_t *n) -{ - /* Result of pointer difference must fit in ptrdiff_t. */ - if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) { - if (*n) { - errno = ENOMEM; - return -1; - } else { - *n = SIZE_ALIGN; - return 0; - } - } - *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK; - return 0; -} - -static void unbin(struct chunk *c, int i) -{ - if (c->prev == c->next) - a_and_64(&mal.binmap, ~(1ULL<prev->next = c->next; - c->next->prev = c->prev; - c->csize |= C_INUSE; - NEXT_CHUNK(c)->psize |= C_INUSE; -} - -static void bin_chunk(struct chunk *self, int i) -{ - self->next = BIN_TO_CHUNK(i); - self->prev = mal.bins[i].tail; - self->next->prev = self; - self->prev->next = self; - if (self->prev == BIN_TO_CHUNK(i)) - a_or_64(&mal.binmap, 1ULL<= n1 - DONTCARE) return; - - next = NEXT_CHUNK(self); - split = (void *)((char *)self + n); - - split->psize = n | C_INUSE; - split->csize = n1-n; - next->psize = n1-n; - self->csize = n | C_INUSE; - - int i = bin_index(n1-n); - lock_bin(i); - - bin_chunk(split, i); - - unlock_bin(i); -} - -void *malloc(size_t n) -{ - struct chunk *c; - int i, j; - uint64_t mask; - - if (adjust_size(&n) < 0) return 0; - - if (n > MMAP_THRESHOLD) { - size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE; - char *base = __mmap(0, len, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (base == (void *)-1) return 0; - c = (void *)(base + SIZE_ALIGN - OVERHEAD); - c->csize = len - (SIZE_ALIGN - OVERHEAD); - c->psize = SIZE_ALIGN - OVERHEAD; - return CHUNK_TO_MEM(c); - } - - i = bin_index_up(n); - if (i<63 && (mal.binmap & (1ULL<psize; - char *base = (char *)self - extra; - size_t oldlen = n0 + extra; - size_t newlen = n + extra; - /* Crash on realloc of freed chunk */ - if (extra & 1) a_crash(); - if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) { - n0 = n; - goto copy_free_ret; - } - newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; - if (oldlen == newlen) return p; - base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE); - if (base == (void *)-1) - goto copy_realloc; - self = (void *)(base + extra); - self->csize = newlen - extra; - return CHUNK_TO_MEM(self); - } - - next = NEXT_CHUNK(self); - - /* Crash on corrupted footer (likely from buffer overflow) */ - if (next->psize != self->csize) a_crash(); - - if (n < n0) { - int i = bin_index_up(n); - int j = bin_index(n0); - if (icsize = split->psize = n | C_INUSE; - split->csize = next->psize = n0-n | C_INUSE; - __bin_chunk(split); - return CHUNK_TO_MEM(self); - } - - lock(mal.split_merge_lock); - - size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next); - if (n0+nsize >= n) { - int i = bin_index(nsize); - lock_bin(i); - if (!(next->csize & C_INUSE)) { - unbin(next, i); - unlock_bin(i); - next = NEXT_CHUNK(next); - self->csize = next->psize = n0+nsize | C_INUSE; - trim(self, n); - unlock(mal.split_merge_lock); - return CHUNK_TO_MEM(self); - } - unlock_bin(i); - } - unlock(mal.split_merge_lock); - -copy_realloc: - /* As a last resort, allocate a new chunk and copy to it. */ - new = malloc(n-OVERHEAD); - if (!new) return 0; -copy_free_ret: - memcpy(new, p, (npsize != self->csize) a_crash(); - - lock(mal.split_merge_lock); - - size_t osize = CHUNK_SIZE(self), size = osize; - - /* Since we hold split_merge_lock, only transition from free to - * in-use can race; in-use to free is impossible */ - size_t psize = self->psize & C_INUSE ? 0 : CHUNK_PSIZE(self); - size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next); - - if (psize) { - int i = bin_index(psize); - lock_bin(i); - if (!(self->psize & C_INUSE)) { - struct chunk *prev = PREV_CHUNK(self); - unbin(prev, i); - self = prev; - size += psize; - } - unlock_bin(i); - } - if (nsize) { - int i = bin_index(nsize); - lock_bin(i); - if (!(next->csize & C_INUSE)) { - unbin(next, i); - next = NEXT_CHUNK(next); - size += nsize; - } - unlock_bin(i); - } - - int i = bin_index(size); - lock_bin(i); - - self->csize = size; - next->psize = size; - bin_chunk(self, i); - unlock(mal.split_merge_lock); - - /* Replace middle of large chunks with fresh zero pages */ - if (size > RECLAIM && (size^(size-osize)) > size-osize) { - uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE; - uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE; - int e = errno; -#if 0 - __madvise((void *)a, b-a, MADV_DONTNEED); -#else - __mmap((void *)a, b-a, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0); -#endif - errno = e; - } - - unlock_bin(i); -} - -static void unmap_chunk(struct chunk *self) -{ - size_t extra = self->psize; - char *base = (char *)self - extra; - size_t len = CHUNK_SIZE(self) + extra; - /* Crash on double free */ - if (extra & 1) a_crash(); - int e = errno; - __munmap(base, len); - errno = e; -} - -void free(void *p) -{ - if (!p) return; - - struct chunk *self = MEM_TO_CHUNK(p); - - if (IS_MMAPPED(self)) - unmap_chunk(self); - else - __bin_chunk(self); -} - -void __malloc_donate(char *start, char *end) -{ - size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - OVERHEAD); - size_t align_end_down = (SIZE_ALIGN-1) & (uintptr_t)end; - - /* Getting past this condition ensures that the padding for alignment - * and header overhead will not overflow and will leave a nonzero - * multiple of SIZE_ALIGN bytes between start and end. */ - if (end - start <= OVERHEAD + align_start_up + align_end_down) - return; - start += align_start_up + OVERHEAD; - end -= align_end_down; - - struct chunk *c = MEM_TO_CHUNK(start), *n = MEM_TO_CHUNK(end); - c->psize = n->csize = C_INUSE; - c->csize = n->psize = C_INUSE | (end-start); - __bin_chunk(c); -} - -void __malloc_atfork(int who) -{ - if (who<0) { - lock(mal.split_merge_lock); - for (int i=0; i<64; i++) - lock(mal.bins[i].lock); - } else if (!who) { - for (int i=0; i<64; i++) - unlock(mal.bins[i].lock); - unlock(mal.split_merge_lock); - } else { - for (int i=0; i<64; i++) - mal.bins[i].lock[0] = mal.bins[i].lock[1] = 0; - mal.split_merge_lock[1] = 0; - mal.split_merge_lock[0] = 0; - } -} diff --git a/porting/liteos_a/user/src/misc/gethostid.c b/porting/liteos_a/user/src/misc/gethostid.c deleted file mode 100644 index 66fa4f843..000000000 --- a/porting/liteos_a/user/src/misc/gethostid.c +++ /dev/null @@ -1,31 +0,0 @@ -#define _GNU_SOURCE - -#include -#include -#include - -#define HOSTID_NUM 16 - -long gethostid() -{ - int ret; - struct in_addr in; - struct hostent *hst = NULL; - char **p = NULL; - char host[128] = {0}; - - ret = gethostname(host, sizeof(host)); - if (ret || host[0] == '\0') { - return -1; - } - - hst = gethostbyname(host); - if (hst == NULL) { - return -1; - } - - p = hst->h_addr_list; - memcpy(&in.s_addr, *p, sizeof(in.s_addr)); - - return (long)((in.s_addr << HOSTID_NUM) | (in.s_addr >> HOSTID_NUM)); -} diff --git a/porting/liteos_a/user/src/misc/getpriority.c b/porting/liteos_a/user/src/misc/getpriority.c deleted file mode 100644 index 42382122c..000000000 --- a/porting/liteos_a/user/src/misc/getpriority.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "syscall.h" - -int getpriority(int which, id_t who) -{ - return syscall(SYS_getpriority, which, who); -} diff --git a/porting/liteos_a/user/src/misc/getrlimit.c b/porting/liteos_a/user/src/misc/getrlimit.c deleted file mode 100644 index 3611ee3b4..000000000 --- a/porting/liteos_a/user/src/misc/getrlimit.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include "syscall.h" - -#define FIX(x) do{ if ((x)>=SYSCALL_RLIM_INFINITY) (x)=RLIM_INFINITY; }while(0) - -int getrlimit(int resource, struct rlimit *rlim) -{ - unsigned long long k_rlim[2]; - - if (syscall(SYS_getrlimit, resource, k_rlim) < 0) - return -1; - - rlim->rlim_cur = k_rlim[0]; - rlim->rlim_max = k_rlim[1]; - - return 0; -} - -weak_alias(getrlimit, getrlimit64); diff --git a/porting/liteos_a/user/src/misc/lockf.c b/porting/liteos_a/user/src/misc/lockf.c deleted file mode 100644 index f6d9a0bbd..000000000 --- a/porting/liteos_a/user/src/misc/lockf.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -int lockf(int fd, int op, off_t size) -{ - struct flock l = { - .l_type = F_WRLCK, - .l_whence = SEEK_CUR, - .l_len = size, - }; - unsupported_api(__FUNCTION__); - switch (op) { - case F_TEST: - l.l_type = F_RDLCK; - if (fcntl(fd, F_GETLK, &l) < 0) - return -1; - if (l.l_type == F_UNLCK || l.l_pid == getpid()) - return 0; - errno = EACCES; - return -1; - case F_ULOCK: - l.l_type = F_UNLCK; - case F_TLOCK: - return fcntl(fd, F_SETLK, &l); - case F_LOCK: - return fcntl(fd, F_SETLKW, &l); - } - errno = EINVAL; - return -1; -} - -weak_alias(lockf, lockf64); diff --git a/porting/liteos_a/user/src/misc/nftw.c b/porting/liteos_a/user/src/misc/nftw.c deleted file mode 100644 index 4b26818b8..000000000 --- a/porting/liteos_a/user/src/misc/nftw.c +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -struct history -{ - struct history *chain; - dev_t dev; - ino_t ino; - int level; - int base; -}; - -#undef dirfd -#define dirfd(d) (*(int *)d) - -static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h) -{ - size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l; - struct stat st; - struct history new; - int type; - int r; - struct FTW lev; - - if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) { - if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st)) - type = FTW_SLN; - else if (errno != EACCES) return -1; - else type = FTW_NS; - } else if (S_ISDIR(st.st_mode)) { - if (access(path, R_OK) < 0) type = FTW_DNR; - else if (flags & FTW_DEPTH) type = FTW_DP; - else type = FTW_D; - } else if (S_ISLNK(st.st_mode)) { - if (flags & FTW_PHYS) type = FTW_SL; - else type = FTW_SLN; - } else { - type = FTW_F; - } - - if ((flags & FTW_MOUNT) && h && st.st_dev != h->dev) - return 0; - - new.chain = h; - new.dev = st.st_dev; - new.ino = st.st_ino; - new.level = h ? h->level+1 : 0; - new.base = j+1; - - lev.level = new.level; - if (h) { - lev.base = h->base; - } else { - size_t k; - for (k=j; k && path[k]=='/'; k--); - for (; k && path[k-1]!='/'; k--); - lev.base = k; - } - - if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) - return r; - - for (; h; h = h->chain) - if (h->dev == st.st_dev && h->ino == st.st_ino) - return 0; - - if ((type == FTW_D || type == FTW_DP) && fd_limit) { - DIR *d = opendir(path); - if (d) { - struct dirent *de; - while ((de = readdir(d))) { - if (de->d_name[0] == '.' - && (!de->d_name[1] - || (de->d_name[1]=='.' - && !de->d_name[2]))) continue; - if (strlen(de->d_name) >= PATH_MAX-l) { - errno = ENAMETOOLONG; - closedir(d); - return -1; - } - path[j]='/'; - strcpy(path+j+1, de->d_name); - if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) { - closedir(d); - return r; - } - } - closedir(d); - } else if (errno != EACCES) { - return -1; - } - } - - path[l] = 0; - if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) - return r; - - return 0; -} - -int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags) -{ - int r, cs; - size_t l; - char pathbuf[PATH_MAX+1]; - - if (fd_limit <= 0) return 0; - - l = strlen(path); - if (l > PATH_MAX) { - errno = ENAMETOOLONG; - return -1; - } - memcpy(pathbuf, path, l+1); - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - r = do_nftw(pathbuf, fn, fd_limit, flags, NULL); - pthread_setcancelstate(cs, 0); - return r; -} - -weak_alias(nftw, nftw64); diff --git a/porting/liteos_a/user/src/misc/openpty.c b/porting/liteos_a/user/src/misc/openpty.c deleted file mode 100644 index 9c1ed0369..000000000 --- a/porting/liteos_a/user/src/misc/openpty.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -/* Nonstandard, but vastly superior to the standard functions */ - -int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struct winsize *ws) -{ - int m, s, n=0, cs; - char buf[20]; - - unsupported_api(__FUNCTION__); - m = open("/dev/ptmx", O_RDWR|O_NOCTTY); - if (m < 0) return -1; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - - if (ioctl(m, TIOCSPTLCK, &n) || ioctl (m, TIOCGPTN, &n)) - goto fail; - - if (!name) name = buf; - snprintf(name, sizeof buf, "/dev/pts/%d", n); - if ((s = open(name, O_RDWR|O_NOCTTY)) < 0) - goto fail; - - if (tio) tcsetattr(s, TCSANOW, tio); - if (ws) ioctl(s, TIOCSWINSZ, ws); - - *pm = m; - *ps = s; - - pthread_setcancelstate(cs, 0); - return 0; -fail: - close(m); - pthread_setcancelstate(cs, 0); - return -1; -} diff --git a/porting/liteos_a/user/src/misc/pty.c b/porting/liteos_a/user/src/misc/pty.c deleted file mode 100644 index 8842b0e51..000000000 --- a/porting/liteos_a/user/src/misc/pty.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" - -int posix_openpt(int flags) -{ - unsupported_api(__FUNCTION__); - int r = open("/dev/ptmx", flags); - if (r < 0 && errno == ENOSPC) errno = EAGAIN; - return r; -} - -int grantpt(int fd) -{ - unsupported_api(__FUNCTION__); - return 0; -} - -int unlockpt(int fd) -{ - int unlock = 0; - return ioctl(fd, TIOCSPTLCK, &unlock); -} - -int __ptsname_r(int fd, char *buf, size_t len) -{ - int pty, err; - if (!buf) len = 0; - if ((err = __syscall(SYS_ioctl, fd, TIOCGPTN, &pty))) return -err; - if (snprintf(buf, len, "/dev/pts/%d", pty) >= len) return ERANGE; - return 0; -} - -weak_alias(__ptsname_r, ptsname_r); diff --git a/porting/liteos_a/user/src/misc/setrlimit.c b/porting/liteos_a/user/src/misc/setrlimit.c deleted file mode 100644 index 7645df0d0..000000000 --- a/porting/liteos_a/user/src/misc/setrlimit.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include "syscall.h" -#include "libc.h" - -#define MIN(a, b) ((a)<(b) ? (a) : (b)) -#define FIX(x) do{ if ((x)>=SYSCALL_RLIM_INFINITY) (x)=RLIM_INFINITY; }while(0) - -static int __setrlimit(int resource, const struct rlimit *rlim) -{ - unsigned long long k_rlim[2]; - struct rlimit tmp; - if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) { - tmp = *rlim; - FIX(tmp.rlim_cur); - FIX(tmp.rlim_max); - rlim = &tmp; - } - - k_rlim[0] = rlim->rlim_cur; - k_rlim[1] = rlim->rlim_max; - return __syscall(SYS_setrlimit, resource, k_rlim); -} - -struct ctx { - const struct rlimit *rlim; - int res; - int err; -}; - -static void do_setrlimit(void *p) -{ - struct ctx *c = p; - if (c->err>0) return; - c->err = -__setrlimit(c->res, c->rlim); -} - -int setrlimit(int resource, const struct rlimit *rlim) -{ - struct ctx c = { .res = resource, .rlim = rlim, .err = -1 }; - __synccall(do_setrlimit, &c); - if (c.err) { - if (c.err>0) errno = c.err; - return -1; - } - return 0; -} - -weak_alias(setrlimit, setrlimit64); diff --git a/porting/liteos_a/user/src/misc/syslog.c b/porting/liteos_a/user/src/misc/syslog.c deleted file mode 100644 index 370fd879b..000000000 --- a/porting/liteos_a/user/src/misc/syslog.c +++ /dev/null @@ -1,151 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lock.h" -#include "fork_impl.h" - -static volatile int lock[1]; -static char log_ident[32]; -static int log_opt; -static int log_facility = LOG_USER; -static int log_mask = 0xff; -static int log_fd = -1; -volatile int *const __syslog_lockptr = lock; - -int setlogmask(int maskpri) -{ - unsupported_api(__FUNCTION__); - LOCK(lock); - int ret = log_mask; - if (maskpri) log_mask = maskpri; - UNLOCK(lock); - return ret; -} - -static const struct { - short sun_family; - char sun_path[9]; -} log_addr = { - AF_UNIX, - "/dev/log" -}; - -void closelog(void) -{ - int cs; - unsupported_api(__FUNCTION__); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - LOCK(lock); - close(log_fd); - log_fd = -1; - UNLOCK(lock); - pthread_setcancelstate(cs, 0); -} - -static void __openlog() -{ - log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); - if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr); -} - -void openlog(const char *ident, int opt, int facility) -{ - int cs; - unsupported_api(__FUNCTION__); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - LOCK(lock); - - if (ident) { - size_t n = strnlen(ident, sizeof log_ident - 1); - memcpy(log_ident, ident, n); - log_ident[n] = 0; - } else { - log_ident[0] = 0; - } - log_opt = opt; - log_facility = facility; - - if ((opt & LOG_NDELAY) && log_fd<0) __openlog(); - - UNLOCK(lock); - pthread_setcancelstate(cs, 0); -} - -static int is_lost_conn(int e) -{ - return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE; -} - -static void _vsyslog(int priority, const char *message, va_list ap) -{ - char timebuf[16]; - time_t now; - struct tm tm; - char buf[1024]; - int errno_save = errno; - int pid; - int l, l2; - int hlen; - int fd; - - if (log_fd < 0) __openlog(); - - if (!(priority & LOG_FACMASK)) priority |= log_facility; - - now = time(NULL); - gmtime_r(&now, &tm); - strftime(timebuf, sizeof timebuf, "%b %e %T", &tm); - - pid = (log_opt & LOG_PID) ? getpid() : 0; - l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ", - priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid); - errno = errno_save; - l2 = vsnprintf(buf+l, sizeof buf - l, message, ap); - if (l2 >= 0) { - if (l2 >= sizeof buf - l) l = sizeof buf - 1; - else l += l2; - if (buf[l-1] != '\n') buf[l++] = '\n'; - if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno) - || connect(log_fd, (void *)&log_addr, sizeof log_addr) < 0 - || send(log_fd, buf, l, 0) < 0) - && (log_opt & LOG_CONS)) { - fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); - if (fd >= 0) { - dprintf(fd, "%.*s", l-hlen, buf+hlen); - close(fd); - } - } - if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen); - } -} - -static void __vsyslog(int priority, const char *message, va_list ap) -{ - int cs; - if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return; - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - LOCK(lock); - _vsyslog(priority, message, ap); - UNLOCK(lock); - pthread_setcancelstate(cs, 0); -} - -void syslog(int priority, const char *message, ...) -{ - unsupported_api(__FUNCTION__); - va_list ap; - va_start(ap, message); - __vsyslog(priority, message, ap); - va_end(ap); -} - -weak_alias(__vsyslog, vsyslog); diff --git a/porting/liteos_a/user/src/misc/wordexp.c b/porting/liteos_a/user/src/misc/wordexp.c deleted file mode 100644 index f8b680369..000000000 --- a/porting/liteos_a/user/src/misc/wordexp.c +++ /dev/null @@ -1,189 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "pthread_impl.h" - -static void reap(pid_t pid) -{ - int status; - while (waitpid(pid, &status, 0) < 0 && errno == EINTR); -} - -static char *getword(FILE *f) -{ - char *s = 0; - return getdelim(&s, (size_t [1]){0}, 0, f) < 0 ? 0 : s; -} - -static int do_wordexp(const char *s, wordexp_t *we, int flags) -{ - size_t i, l; - int sq=0, dq=0; - size_t np=0; - char *w, **tmp; - char *redir = (flags & WRDE_SHOWERR) ? "" : "2>/dev/null"; - int err = 0; - FILE *f; - size_t wc = 0; - char **wv = 0; - int p[2]; - pid_t pid; - sigset_t set; - - if (flags & WRDE_REUSE) wordfree(we); - - if (flags & WRDE_NOCMD) for (i=0; s[i]; i++) switch (s[i]) { - case '\\': - if (!sq && !s[++i]) return WRDE_SYNTAX; - break; - case '\'': - if (!dq) sq^=1; - break; - case '"': - if (!sq) dq^=1; - break; - case '(': - if (np) { - np++; - break; - } - case ')': - if (np) { - np--; - break; - } - case '\n': - case '|': - case '&': - case ';': - case '<': - case '>': - case '{': - case '}': - if (!(sq|dq|np)) return WRDE_BADCHAR; - break; - case '$': - if (sq) break; - if (s[i+1]=='(' && s[i+2]=='(') { - i += 2; - np += 2; - break; - } else if (s[i+1] != '(') break; - case '`': - if (sq) break; - return WRDE_CMDSUB; - } - - if (flags & WRDE_APPEND) { - wc = we->we_wordc; - wv = we->we_wordv; - } - - i = wc; - if (flags & WRDE_DOOFFS) { - if (we->we_offs > SIZE_MAX/sizeof(void *)/4) - goto nospace; - i += we->we_offs; - } else { - we->we_offs = 0; - } - - if (pipe2(p, O_CLOEXEC) < 0) goto nospace; - __block_all_sigs(&set); - pid = fork(); - __restore_sigs(&set); - if (pid < 0) { - close(p[0]); - close(p[1]); - goto nospace; - } - if (!pid) { - if (p[1] == 1) fcntl(1, F_SETFD, 0); - else dup2(p[1], 1); - execl("/bin/sh", "sh", "-c", - "eval \"printf %s\\\\\\\\0 x $1 $2\"", - "sh", s, redir, (char *)0); - _exit(1); - } - close(p[1]); - - f = fdopen(p[0], "r"); - if (!f) { - close(p[0]); - kill(pid, SIGKILL); - reap(pid); - goto nospace; - } - - l = wv ? i+1 : 0; - - free(getword(f)); - if (feof(f)) { - fclose(f); - reap(pid); - return WRDE_SYNTAX; - } - - while ((w = getword(f))) { - if (i+1 >= l) { - l += l/2+10; - tmp = realloc(wv, l*sizeof(char *)); - if (!tmp) break; - wv = tmp; - } - wv[i++] = w; - wv[i] = 0; - } - if (!feof(f)) err = WRDE_NOSPACE; - - fclose(f); - reap(pid); - - if (!wv) wv = calloc(i+1, sizeof *wv); - - we->we_wordv = wv; - we->we_wordc = i; - - if (flags & WRDE_DOOFFS) { - if (wv) for (i=we->we_offs; i; i--) - we->we_wordv[i-1] = 0; - we->we_wordc -= we->we_offs; - } - return err; - -nospace: - if (!(flags & WRDE_APPEND)) { - we->we_wordc = 0; - we->we_wordv = 0; - } - return WRDE_NOSPACE; -} - -int wordexp(const char *restrict s, wordexp_t *restrict we, int flags) -{ - int r, cs; - unsupported_api(__FUNCTION__); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - r = do_wordexp(s, we, flags); - pthread_setcancelstate(cs, 0); - return r; -} - -void wordfree(wordexp_t *we) -{ - size_t i; - if (!we->we_wordv) return; - for (i=0; iwe_wordc; i++) free(we->we_wordv[we->we_offs+i]); - free(we->we_wordv); - we->we_wordv = 0; - we->we_wordc = 0; -} diff --git a/porting/liteos_a/user/src/mman/mlock.c b/porting/liteos_a/user/src/mman/mlock.c deleted file mode 100644 index e56c3228e..000000000 --- a/porting/liteos_a/user/src/mman/mlock.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "syscall.h" -#include - -int mlock(const void *addr, size_t len) -{ - unsupported_api(__FUNCTION__); -#ifdef SYS_mlock - return syscall(SYS_mlock, addr, len); -#else - return syscall(SYS_mlock2, addr, len, 0); -#endif -} diff --git a/porting/liteos_a/user/src/mman/mlockall.c b/porting/liteos_a/user/src/mman/mlockall.c deleted file mode 100644 index bc2411e65..000000000 --- a/porting/liteos_a/user/src/mman/mlockall.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include "syscall.h" - -int mlockall(int flags) -{ - unsupported_api(__FUNCTION__); - return syscall(SYS_mlockall, flags); -} diff --git a/porting/liteos_a/user/src/mman/msync.c b/porting/liteos_a/user/src/mman/msync.c deleted file mode 100644 index 61cbf5f0d..000000000 --- a/porting/liteos_a/user/src/mman/msync.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include "syscall.h" - -int msync(void *start, size_t len, int flags) -{ - unsupported_api(__FUNCTION__); - return syscall_cp(SYS_msync, start, len, flags); -} diff --git a/porting/liteos_a/user/src/mman/munlock.c b/porting/liteos_a/user/src/mman/munlock.c deleted file mode 100644 index 8a6463a1e..000000000 --- a/porting/liteos_a/user/src/mman/munlock.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include "syscall.h" - -int munlock(const void *addr, size_t len) -{ - unsupported_api(__FUNCTION__); - return syscall(SYS_munlock, addr, len); -} diff --git a/porting/liteos_a/user/src/mman/munlockall.c b/porting/liteos_a/user/src/mman/munlockall.c deleted file mode 100644 index 432700742..000000000 --- a/porting/liteos_a/user/src/mman/munlockall.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include "syscall.h" - -int munlockall(void) -{ - unsupported_api(__FUNCTION__); - return syscall(SYS_munlockall); -} diff --git a/porting/liteos_a/user/src/mman/posix_madvise.c b/porting/liteos_a/user/src/mman/posix_madvise.c deleted file mode 100644 index 8498ec939..000000000 --- a/porting/liteos_a/user/src/mman/posix_madvise.c +++ /dev/null @@ -1,11 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include "syscall.h" - -int posix_madvise(void *addr, size_t len, int advice) -{ - unsupported_api(__FUNCTION__); - if (advice == MADV_DONTNEED) return 0; - return -__syscall(SYS_madvise, addr, len, advice); -} diff --git a/porting/liteos_a/user/src/mman/shm_open.c b/porting/liteos_a/user/src/mman/shm_open.c deleted file mode 100644 index ef8a8c946..000000000 --- a/porting/liteos_a/user/src/mman/shm_open.c +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -char *__shm_mapname(const char *name, char *buf) -{ - char *p; - while (*name == '/') name++; - if (*(p = __strchrnul(name, '/')) || p==name || - (p-name <= 2 && name[0]=='.' && p[-1]=='.')) { - errno = EINVAL; - return 0; - } - if (p-name > NAME_MAX) { - errno = ENAMETOOLONG; - return 0; - } - memcpy(buf, "/dev/shm/", 9); - memcpy(buf+9, name, p-name+1); - return buf; -} - -int shm_open(const char *name, int flag, mode_t mode) -{ - int cs; - char buf[NAME_MAX+10]; - unsupported_api(__FUNCTION__); - if (!(name = __shm_mapname(name, buf))) return -1; - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - int fd = open(name, flag|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, mode); - pthread_setcancelstate(cs, 0); - return fd; -} - -int shm_unlink(const char *name) -{ - char buf[NAME_MAX+10]; - unsupported_api(__FUNCTION__); - if (!(name = __shm_mapname(name, buf))) return -1; - return unlink(name); -} diff --git a/porting/liteos_a/user/src/mq/mq_close.c b/porting/liteos_a/user/src/mq/mq_close.c deleted file mode 100644 index 645220cbd..000000000 --- a/porting/liteos_a/user/src/mq/mq_close.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "syscall.h" - -int mq_close(mqd_t mqd) -{ - return syscall(SYS_mqclose, mqd); -} diff --git a/porting/liteos_a/user/src/mq/mq_notify.c b/porting/liteos_a/user/src/mq/mq_notify.c deleted file mode 100644 index c2bfddf5a..000000000 --- a/porting/liteos_a/user/src/mq/mq_notify.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" - -int mq_notify(mqd_t mqd, const struct sigevent *sev) -{ - if (!sev || sev->sigev_notify != SIGEV_THREAD) - return syscall(SYS_mq_notify, mqd, sev); - - errno = ENOTSUP; - return -1; -} diff --git a/porting/liteos_a/user/src/multibyte/wcsnrtombs.c b/porting/liteos_a/user/src/multibyte/wcsnrtombs.c deleted file mode 100644 index 95e25e708..000000000 --- a/porting/liteos_a/user/src/multibyte/wcsnrtombs.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include - -size_t wcsnrtombs(char *restrict dst, const wchar_t **restrict wcs, size_t wn, size_t n, mbstate_t *restrict st) -{ - const wchar_t *ws = *wcs; - size_t cnt = 0; - if (!dst) n=0; - while (ws && wn) { - char tmp[MB_LEN_MAX]; - size_t l = wcrtomb(nn) break; - memcpy(dst, tmp, l); - } - dst += l; - n -= l; - } - if (!*ws) { - ws = 0; - break; - } - ws++; - wn--; - cnt += l; - } - if (dst) *wcs = ws; - return cnt; -} diff --git a/porting/liteos_a/user/src/network/ent.c b/porting/liteos_a/user/src/network/ent.c deleted file mode 100644 index 65131e2d6..000000000 --- a/porting/liteos_a/user/src/network/ent.c +++ /dev/null @@ -1,219 +0,0 @@ -#define _BSD_SOURCE -#include -#include -#include -#include -#include -#include - -#define HOST_FILE_PATH "/etc/hosts" -#define NET_FILE_PATH "/etc/networks" -#define BUFFER_SIZE 200 -#define INET_ADDR_LEN 4 -#define INET6_ADDR_LEN 16 -#define MAX_ALIAS_NO 10 -#define MAX_ADDR_NO 10 -#define MAX_NAME_LEN 20 -#define MAX_ALIAS_LEN 20 - -struct hostdata { - FILE *hostf; - int stayopen; - struct hostent he; - char buf[BUFFER_SIZE]; - char *addr[MAX_ADDR_NO + 1]; - char addr_list[MAX_ADDR_NO + 1][INET6_ADDR_LEN + 1]; - char *aliases[MAX_ALIAS_NO + 1]; -}; - -struct netdata { - FILE *netf; - int stayopen; - struct netent ne; - char buf[BUFFER_SIZE]; - char *aliases[MAX_ALIAS_NO + 1]; -}; - -static struct hostdata *hd = NULL; -static struct netdata *nd = NULL; - -static int gethostent_resolv() -{ - char *ch, *ptr = NULL; - if (hd->buf[0] == '#') - return -1; - - ch = strchr(hd->buf, '#'); - if (ch) - *ch = '\0'; - ch = strtok_r(hd->buf, " \t\n", &ptr); - if (!ch) - return -1; - - if (inet_pton(AF_INET, ch, hd->addr_list[0]) == 1) { - hd->he.h_addrtype = AF_INET; - hd->he.h_length = INET_ADDR_LEN; - hd->addr_list[0][INET_ADDR_LEN] = '\0'; - } else if (inet_pton(AF_INET6, ch, hd->addr_list[0]) == 1) { - hd->he.h_addrtype = AF_INET6; - hd->he.h_length = INET6_ADDR_LEN; - hd->addr_list[0][INET6_ADDR_LEN] = '\0'; - } else { - return -1; - } - - hd->addr[0] = hd->addr_list[0]; - hd->addr[1] = NULL; - hd->he.h_addr_list = hd->addr; - - ch = strtok_r(NULL, " \t\n", &ptr); - if (!ch) - return -1; - hd->he.h_name = ch; - - int i = 0; - while (i < MAX_ALIAS_NO) { - ch = strtok_r(NULL, " \t\n", &ptr); - hd->aliases[i++] = ch; - } - - hd->aliases[MAX_ALIAS_NO] = NULL; - hd->he.h_aliases = hd->aliases; - return 0; -} - -struct hostent *gethostent(void) -{ - if (hd == NULL) { - if ((hd = malloc(sizeof(struct hostdata))) == NULL) { - h_errno = NO_RECOVERY; - return NULL; - } - hd->hostf = NULL; - hd->stayopen = 0; - } - if (!hd->hostf && !(hd->hostf = fopen(HOST_FILE_PATH, "r"))) { - h_errno = NO_RECOVERY; - free(hd); - hd =NULL; - return NULL; - } - do { - if (!fgets(hd->buf, BUFFER_SIZE, hd->hostf)) { - h_errno = HOST_NOT_FOUND; - return NULL; - } - } while (gethostent_resolv()); - - return &(hd->he); -} - -void sethostent(int stayopen) -{ - if (hd == NULL) { - if ((hd = malloc(sizeof(struct hostdata))) == NULL) { - h_errno = NO_RECOVERY; - return; - } - hd->hostf = NULL; - } - if (hd->hostf == NULL) - hd->hostf = fopen(NET_FILE_PATH, "r"); - else - rewind(hd->hostf); - hd->stayopen = stayopen; -} - -void endhostent(void) -{ - if (hd == NULL) - return; - if (hd->stayopen == 0) { - if (hd->hostf != NULL) - fclose(hd->hostf); - free(hd); - hd = NULL; - } -} - -static int getnetent_resolv() -{ - if (nd->buf[0] == '#') - return -1; - char *ch, *ptr = NULL; - ch = strchr(nd->buf, '#'); - if (ch) - *ch = '\0'; - ch = strtok_r(nd->buf, " \t\n", &ptr); - if (!ch) - return -1; - nd->ne.n_name = ch; - nd->ne.n_addrtype = AF_INET; - - ch = strtok_r(NULL, " \t\n", &ptr); - if (!ch) - return -1; - nd->ne.n_net = inet_network(ch); - - int i = 0; - while ((ch = strtok_r(NULL, " \t\n", &ptr)) != NULL && i < MAX_ALIAS_NO) { - nd->aliases[i++] = ch; - } - nd->aliases[i] = NULL; - nd->ne.n_aliases = nd->aliases; - return 0; -} - -struct netent *getnetent(void) -{ - if (nd == NULL) { - if ((nd = malloc(sizeof(struct netdata))) == NULL) { - errno = ENOMEM; - return NULL; - } - nd->netf = NULL; - nd->stayopen = 0; - } - if (nd->netf == NULL && (nd->netf = fopen(NET_FILE_PATH, "r")) == NULL) { - errno = ENOENT; - free(nd); - nd = NULL; - return NULL; - } - while (1) { - if (fgets(nd->buf, BUFFER_SIZE, nd->netf) == NULL) - break; - if (getnetent_resolv() == 0) - return &(nd->ne); - } - - return NULL; -} - -void setnetent(int stayopen) -{ - if (nd == NULL) { - if ((nd = malloc(sizeof(struct netdata))) == NULL) { - errno = ENOMEM; - return; - } - nd->netf = NULL; - } - if (nd->netf == NULL) - nd->netf = fopen(NET_FILE_PATH, "r"); - else - rewind(nd->netf); - nd->stayopen = stayopen; -} - -void endnetent(void) -{ - if (nd == NULL) - return; - if (!nd->stayopen) { - if (nd->netf != NULL) - fclose(nd->netf); - free(nd); - nd = NULL; - } -} \ No newline at end of file diff --git a/porting/liteos_a/user/src/network/ether.c b/porting/liteos_a/user/src/network/ether.c deleted file mode 100644 index 28cde3929..000000000 --- a/porting/liteos_a/user/src/network/ether.c +++ /dev/null @@ -1,143 +0,0 @@ -#include -#include -#include -#include -#include - -#define ETHER_FILE_PATH "/etc/ethers" -#define BUFFER_SIZE 200 - -struct ether_addr *ether_aton_r (const char *x, struct ether_addr *p_a) -{ - struct ether_addr a; - char *y; - for (int ii = 0; ii < 6; ii++) { - unsigned long int n; - if (ii != 0) { - if (x[0] != ':') return 0; /* bad format */ - else x++; - } - n = strtoul (x, &y, 16); - x = y; - if (n > 0xFF) return 0; /* bad byte */ - a.ether_addr_octet[ii] = n; - } - if (x[0] != 0) return 0; /* bad format */ - *p_a = a; - return p_a; -} - -struct ether_addr *ether_aton (const char *x) -{ - static struct ether_addr a; - return ether_aton_r (x, &a); -} - -char *ether_ntoa_r (const struct ether_addr *p_a, char *x) { - char *y; - y = x; - for (int ii = 0; ii < 6; ii++) { - x += sprintf (x, ii == 0 ? "%.2X" : ":%.2X", p_a->ether_addr_octet[ii]); - } - return y; -} - -char *ether_ntoa (const struct ether_addr *p_a) { - static char x[18]; - return ether_ntoa_r (p_a, x); -} - -int ether_line(const char *line, struct ether_addr *addr, char *hostname) -{ - char buf[BUFFER_SIZE], *ch, *ptr = NULL; - if (line[0] == '#') { - errno = EINVAL; - return -1; - } - strcpy(buf, line); - ch = strchr(buf, '#'); - if (ch) - *ch = '\0'; - ch = strtok_r(buf, " \t\n", &ptr); - ch = strtok_r(NULL, " \t\n", &ptr); - if (ch == NULL) { - errno = EINVAL; - return -1; - } - strcpy(hostname, ch); - - unsigned int ret; - ch = &buf[0]; - for (int i = 0; i < 6; i++) { - ch = strtok_r(ch, ":", &ptr); - if (ch == NULL || sscanf(ch, "%x", &ret) != 1) { - errno = EINVAL; - return -1; - } - addr->ether_addr_octet[i] = (uint8_t)ret; - ch = NULL; - } - - return 0; -} - -int ether_ntohost(char *hostname, const struct ether_addr *addr) -{ - FILE *f = NULL; - char buf[BUFFER_SIZE + 1]; - char temp_name[BUFFER_SIZE]; - struct ether_addr *temp_addr; - - if (!(f = fopen(ETHER_FILE_PATH, "r"))) - return ENOENT; - temp_addr = (struct ether_addr *)malloc(sizeof(struct ether_addr)); - if (temp_addr == NULL) { - fclose(f); - return ENOMEM; - } - while (fgets(buf, BUFFER_SIZE, f)) { - if (ether_line(buf, temp_addr, temp_name) != 0) - continue; - if (memcmp(addr, temp_addr, 6) == 0) { - strcpy(hostname, temp_name); - free(temp_addr); - fclose(f); - return 0; - } - } - - free(temp_addr); - fclose(f); - return -1; -} - - -int ether_hostton(const char *hostname, struct ether_addr *addr) -{ - FILE *f = NULL; - char buf[BUFFER_SIZE + 1]; - char temp_name[BUFFER_SIZE]; - struct ether_addr *temp_addr; - - temp_addr = (struct ether_addr *)malloc(sizeof(struct ether_addr)); - if (temp_addr == NULL) - return ENOMEM; - if (!(f = fopen(ETHER_FILE_PATH, "r"))) { - free(temp_addr); - return ENOENT; - } - while (fgets(buf, BUFFER_SIZE, f)) { - if (ether_line(buf, temp_addr, temp_name) != 0) - continue; - if (strcmp(hostname, temp_name) == 0) { - memcpy(addr, temp_addr, 6); - free(temp_addr); - fclose(f); - return 0; - } - } - - free(temp_addr); - fclose(f); - return -1; -} diff --git a/porting/liteos_a/user/src/network/gethostbyaddr.c b/porting/liteos_a/user/src/network/gethostbyaddr.c deleted file mode 100644 index 598e2241a..000000000 --- a/porting/liteos_a/user/src/network/gethostbyaddr.c +++ /dev/null @@ -1,24 +0,0 @@ -#define _GNU_SOURCE - -#include -#include -#include - -struct hostent *gethostbyaddr(const void *a, socklen_t l, int af) -{ - static struct hostent *h; - size_t size = 63; - struct hostent *res; - int err; - do { - free(h); - h = malloc(size+=size+1); - if (!h) { - h_errno = NO_RECOVERY; - return 0; - } - err = gethostbyaddr_r(a, l, af, h, - (void *)(h+1), size-sizeof *h, &res, &h_errno); - } while (err == ERANGE); - return err ? 0 : h; -} diff --git a/porting/liteos_a/user/src/network/gethostbyaddr_r.c b/porting/liteos_a/user/src/network/gethostbyaddr_r.c deleted file mode 100644 index 2c3cd7088..000000000 --- a/porting/liteos_a/user/src/network/gethostbyaddr_r.c +++ /dev/null @@ -1,70 +0,0 @@ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include - -int gethostbyaddr_r(const void *a, socklen_t l, int af, - struct hostent *h, char *buf, size_t buflen, - struct hostent **res, int *err) -{ - union { - struct sockaddr_in sin; - struct sockaddr_in6 sin6; - } sa = { .sin.sin_family = af }; - socklen_t sl = af==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin; - int i; - - *res = 0; - /* Load address argument into sockaddr structure */ - if (af==AF_INET6 && l==16) memcpy(&sa.sin6.sin6_addr, a, 16); - else if (af==AF_INET && l==4) memcpy(&sa.sin.sin_addr, a, 4); - else { - *err = NO_RECOVERY; - return EINVAL; - } - - /* Align buffer and check for space for pointers and ip address */ - i = (uintptr_t)buf & sizeof(char *)-1; - if (!i) i = sizeof(char *); - if (buflen <= 5*sizeof(char *)-i + l) return ERANGE; - buf += sizeof(char *)-i; - buflen -= 5*sizeof(char *)-i + l; - - h->h_addr_list = (void *)buf; - buf += 2*sizeof(char *); - h->h_aliases = (void *)buf; - buf += 2*sizeof(char *); - - h->h_addr_list[0] = buf; - memcpy(h->h_addr_list[0], a, l); - buf += l; - h->h_addr_list[1] = 0; - h->h_aliases[0] = buf; - h->h_aliases[1] = 0; - - switch (getnameinfo((void *)&sa, sl, buf, buflen, 0, 0, 0)) { - case EAI_AGAIN: - *err = TRY_AGAIN; - return EAGAIN; - case EAI_OVERFLOW: - return ERANGE; - default: - case EAI_MEMORY: - case EAI_SYSTEM: - case EAI_FAIL: - *err = NO_RECOVERY; - return errno; - case 0: - break; - } - - h->h_addrtype = af; - h->h_length = l; - h->h_name = h->h_aliases[0]; - *res = h; - return 0; -} diff --git a/porting/liteos_a/user/src/network/gethostbyname_r.c b/porting/liteos_a/user/src/network/gethostbyname_r.c deleted file mode 100644 index cd8725417..000000000 --- a/porting/liteos_a/user/src/network/gethostbyname_r.c +++ /dev/null @@ -1,11 +0,0 @@ -#define _GNU_SOURCE - -#include -#include - -int gethostbyname_r(const char *name, - struct hostent *h, char *buf, size_t buflen, - struct hostent **res, int *err) -{ - return gethostbyname2_r(name, AF_INET, h, buf, buflen, res, err); -} diff --git a/porting/liteos_a/user/src/network/getifaddrs.c b/porting/liteos_a/user/src/network/getifaddrs.c deleted file mode 100644 index ed9ad0089..000000000 --- a/porting/liteos_a/user/src/network/getifaddrs.c +++ /dev/null @@ -1,299 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "netlink.h" -#include - -#define IFADDRS_HASH_SIZE 64 -#define MAX_IF_NO 10 - -/* getifaddrs() reports hardware addresses with PF_PACKET that implies - * struct sockaddr_ll. But e.g. Infiniband socket address length is - * longer than sockaddr_ll.ssl_addr[8] can hold. Use this hack struct - * to extend ssl_addr - callers should be able to still use it. */ -struct sockaddr_ll_hack { - unsigned short sll_family, sll_protocol; - int sll_ifindex; - unsigned short sll_hatype; - unsigned char sll_pkttype, sll_halen; - unsigned char sll_addr[24]; -}; - -union sockany { - struct sockaddr sa; - struct sockaddr_ll_hack ll; - struct sockaddr_in v4; - struct sockaddr_in6 v6; -}; - -struct ifaddrs_storage { - struct ifaddrs ifa; - struct ifaddrs_storage *hash_next; - union sockany addr, netmask, ifu; - unsigned int index; - char name[IFNAMSIZ+1]; -}; - -struct ifaddrs_ctx { - struct ifaddrs_storage *first; - struct ifaddrs_storage *last; - struct ifaddrs_storage *hash[IFADDRS_HASH_SIZE]; -}; - -void freeifaddrs(struct ifaddrs *ifp) -{ - struct ifaddrs *tmp = NULL; - while (ifp) { - if (!ifp->ifa_name) - free(ifp); - if (!ifp->ifa_addr) - free(ifp->ifa_addr); - if (!ifp->ifa_netmask) - free(ifp->ifa_netmask); - tmp = ifp->ifa_next; - free(ifp); - ifp = tmp; - } -} - -static void copy_addr(struct sockaddr **r, int af, union sockany *sa, void *addr, size_t addrlen, int ifindex) -{ - uint8_t *dst; - int len; - - switch (af) { - case AF_INET: - dst = (uint8_t*) &sa->v4.sin_addr; - len = 4; - break; - case AF_INET6: - dst = (uint8_t*) &sa->v6.sin6_addr; - len = 16; - if (IN6_IS_ADDR_LINKLOCAL(addr) || IN6_IS_ADDR_MC_LINKLOCAL(addr)) - sa->v6.sin6_scope_id = ifindex; - break; - default: - return; - } - if (addrlen < len) return; - sa->sa.sa_family = af; - memcpy(dst, addr, len); - *r = &sa->sa; -} - -static void gen_netmask(struct sockaddr **r, int af, union sockany *sa, int prefixlen) -{ - uint8_t addr[16] = {0}; - int i; - - if (prefixlen > 8*sizeof(addr)) prefixlen = 8*sizeof(addr); - i = prefixlen / 8; - memset(addr, 0xff, i); - if (i < sizeof(addr)) addr[i++] = 0xff << (8 - (prefixlen % 8)); - copy_addr(r, af, sa, addr, sizeof(addr), 0); -} - -static void copy_lladdr(struct sockaddr **r, union sockany *sa, void *addr, size_t addrlen, int ifindex, unsigned short hatype) -{ - if (addrlen > sizeof(sa->ll.sll_addr)) return; - sa->ll.sll_family = AF_PACKET; - sa->ll.sll_ifindex = ifindex; - sa->ll.sll_hatype = hatype; - sa->ll.sll_halen = addrlen; - memcpy(sa->ll.sll_addr, addr, addrlen); - *r = &sa->sa; -} - -static int netlink_msg_to_ifaddr(void *pctx, struct nlmsghdr *h) -{ - struct ifaddrs_ctx *ctx = pctx; - struct ifaddrs_storage *ifs, *ifs0; - struct ifinfomsg *ifi = NLMSG_DATA(h); - struct ifaddrmsg *ifa = NLMSG_DATA(h); - struct rtattr *rta; - int stats_len = 0; - - if (h->nlmsg_type == RTM_NEWLINK) { - for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) { - if (rta->rta_type != IFLA_STATS) continue; - stats_len = RTA_DATALEN(rta); - break; - } - } else { - for (ifs0 = ctx->hash[ifa->ifa_index % IFADDRS_HASH_SIZE]; ifs0; ifs0 = ifs0->hash_next) - if (ifs0->index == ifa->ifa_index) - break; - if (!ifs0) return 0; - } - - ifs = calloc(1, sizeof(struct ifaddrs_storage) + stats_len); - if (ifs == 0) return -1; - - if (h->nlmsg_type == RTM_NEWLINK) { - ifs->index = ifi->ifi_index; - ifs->ifa.ifa_flags = ifi->ifi_flags; - - for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) { - switch (rta->rta_type) { - case IFLA_IFNAME: - if (RTA_DATALEN(rta) < sizeof(ifs->name)) { - memcpy(ifs->name, RTA_DATA(rta), RTA_DATALEN(rta)); - ifs->ifa.ifa_name = ifs->name; - } - break; - case IFLA_ADDRESS: - copy_lladdr(&ifs->ifa.ifa_addr, &ifs->addr, RTA_DATA(rta), RTA_DATALEN(rta), ifi->ifi_index, ifi->ifi_type); - break; - case IFLA_BROADCAST: - copy_lladdr(&ifs->ifa.ifa_broadaddr, &ifs->ifu, RTA_DATA(rta), RTA_DATALEN(rta), ifi->ifi_index, ifi->ifi_type); - break; - case IFLA_STATS: - ifs->ifa.ifa_data = (void*)(ifs+1); - memcpy(ifs->ifa.ifa_data, RTA_DATA(rta), RTA_DATALEN(rta)); - break; - } - } - if (ifs->ifa.ifa_name) { - unsigned int bucket = ifs->index % IFADDRS_HASH_SIZE; - ifs->hash_next = ctx->hash[bucket]; - ctx->hash[bucket] = ifs; - } - } else { - ifs->ifa.ifa_name = ifs0->ifa.ifa_name; - ifs->ifa.ifa_flags = ifs0->ifa.ifa_flags; - for (rta = NLMSG_RTA(h, sizeof(*ifa)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) { - switch (rta->rta_type) { - case IFA_ADDRESS: - /* If ifa_addr is already set we, received an IFA_LOCAL before - * so treat this as destination address */ - if (ifs->ifa.ifa_addr) - copy_addr(&ifs->ifa.ifa_dstaddr, ifa->ifa_family, &ifs->ifu, RTA_DATA(rta), RTA_DATALEN(rta), ifa->ifa_index); - else - copy_addr(&ifs->ifa.ifa_addr, ifa->ifa_family, &ifs->addr, RTA_DATA(rta), RTA_DATALEN(rta), ifa->ifa_index); - break; - case IFA_BROADCAST: - copy_addr(&ifs->ifa.ifa_broadaddr, ifa->ifa_family, &ifs->ifu, RTA_DATA(rta), RTA_DATALEN(rta), ifa->ifa_index); - break; - case IFA_LOCAL: - /* If ifa_addr is set and we get IFA_LOCAL, assume we have - * a point-to-point network. Move address to correct field. */ - if (ifs->ifa.ifa_addr) { - ifs->ifu = ifs->addr; - ifs->ifa.ifa_dstaddr = &ifs->ifu.sa; - memset(&ifs->addr, 0, sizeof(ifs->addr)); - } - copy_addr(&ifs->ifa.ifa_addr, ifa->ifa_family, &ifs->addr, RTA_DATA(rta), RTA_DATALEN(rta), ifa->ifa_index); - break; - case IFA_LABEL: - if (RTA_DATALEN(rta) < sizeof(ifs->name)) { - memcpy(ifs->name, RTA_DATA(rta), RTA_DATALEN(rta)); - ifs->ifa.ifa_name = ifs->name; - } - break; - } - } - if (ifs->ifa.ifa_addr) - gen_netmask(&ifs->ifa.ifa_netmask, ifa->ifa_family, &ifs->netmask, ifa->ifa_prefixlen); - } - - if (ifs->ifa.ifa_name) { - if (!ctx->first) ctx->first = ifs; - if (ctx->last) ctx->last->ifa.ifa_next = &ifs->ifa; - ctx->last = ifs; - } else { - free(ifs); - } - return 0; -} - - -static struct ifaddrs* ifaddrs_init() -{ - struct ifaddrs *ifa = NULL; - ifa = malloc(sizeof(struct ifaddrs)); - if (!ifa) - return NULL; - ifa->ifa_name = malloc(sizeof(char) *(IF_NAMESIZE + 1)); - ifa->ifa_addr = malloc(sizeof(struct sockaddr)); - ifa->ifa_netmask = malloc(sizeof(struct sockaddr)); - ifa->ifa_next = NULL; - if (!ifa->ifa_name || !ifa->ifa_addr || !ifa->ifa_netmask) { - free(ifa->ifa_name); - free(ifa->ifa_addr); - free(ifa->ifa_netmask); - free(ifa); - return NULL; - } - return ifa; -} - -int getifaddrs(struct ifaddrs **ifap) -{ - if (ifap == NULL) { - errno = EINVAL; - return -1; - } - - int fd, ifno, ret; - struct ifreq ifr[MAX_IF_NO]; - struct ifconf ifconfig; - struct ifaddrs *ifstart = NULL; - - ifconfig.ifc_buf = ifr; - ifconfig.ifc_len = sizeof(ifr); - if ((fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) - return -1; - if (ioctl(fd, SIOCGIFCONF, &ifconfig) < 0) - goto exit; - if (ifconfig.ifc_len % sizeof(struct ifreq)) { - errno = EINVAL; - goto exit; - } - ifno = ifconfig.ifc_len / sizeof(struct ifreq); - - if (!(ifstart = ifaddrs_init())) { - errno = ENOMEM; - goto exit; - } - struct ifaddrs *ifa = ifstart; - - for (int i = 0; i < ifno; i++) { - memcpy(ifa->ifa_name, ifr[i].ifr_name, IF_NAMESIZE); - ifa->ifa_name[IF_NAMESIZE] = '\0'; - memcpy(ifa->ifa_addr, &ifr[i].ifr_addr, sizeof(struct sockaddr)); - if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) < 0) - goto error; - memcpy(ifa->ifa_netmask, &ifr[i].ifr_netmask, sizeof(struct sockaddr)); - - if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) < 0) - goto error; - ifa->ifa_flags = ifr[i].ifr_flags; - - if (i < ifno - 1) { - ifa->ifa_next = ifaddrs_init(); - if (!ifa->ifa_next) { - errno = ENOMEM; - goto error; - } - ifa = ifa->ifa_next; - } - } - - *ifap = ifstart; - __syscall(SYS_close, fd); - return 0; - -error: - freeifaddrs(ifstart); -exit: - __syscall(SYS_close, fd); - return -1; -} - diff --git a/porting/liteos_a/user/src/network/getnameinfo.c b/porting/liteos_a/user/src/network/getnameinfo.c deleted file mode 100644 index 684b6eeb8..000000000 --- a/porting/liteos_a/user/src/network/getnameinfo.c +++ /dev/null @@ -1,200 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lookup.h" -#include "stdio_impl.h" - -#define PTR_MAX (64 + sizeof ".in-addr.arpa") -#define RR_PTR 12 - -static char *itoa(char *p, unsigned x) { - p += 3*sizeof(int); - *--p = 0; - do { - *--p = '0' + x % 10; - x /= 10; - } while (x); - return p; -} - -static void mkptr4(char *s, const unsigned char *ip) -{ - sprintf(s, "%d.%d.%d.%d.in-addr.arpa", - ip[3], ip[2], ip[1], ip[0]); -} - -static void mkptr6(char *s, const unsigned char *ip) -{ - static const char xdigits[] = "0123456789abcdef"; - int i; - for (i=15; i>=0; i--) { - *s++ = xdigits[ip[i]&15]; *s++ = '.'; - *s++ = xdigits[ip[i]>>4]; *s++ = '.'; - } - strcpy(s, "ip6.arpa"); -} - -static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family) -{ - char line[512], *p, *z; - unsigned char _buf[1032], atmp[16]; - struct address iplit; - FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); - if (!f) return; - if (family == AF_INET) { - memcpy(atmp+12, a, 4); - memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); - a = atmp; - } - while (fgets(line, sizeof line, f)) { - if ((p=strchr(line, '#'))) *p++='\n', *p=0; - - for (p=line; *p && !isspace(*p); p++); - *p++ = 0; - if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0) - continue; - - if (iplit.family == AF_INET) { - memcpy(iplit.addr+12, iplit.addr, 4); - memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); - iplit.scopeid = 0; - } - - if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid) - continue; - - for (; *p && isspace(*p); p++); - for (z=p; *z && !isspace(*z); z++); - *z = 0; - if (z-p < 256) { - memcpy(buf, p, z-p+1); - break; - } - } - __fclose_ca(f); -} - -static void reverse_services(char *buf, int port, int dgram) -{ - unsigned long svport; - char line[128], *p, *z; - unsigned char _buf[1032]; - FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf); - if (!f) return; - while (fgets(line, sizeof line, f)) { - if ((p=strchr(line, '#'))) *p++='\n', *p=0; - - for (p=line; *p && !isspace(*p); p++); - if (!*p) continue; - *p++ = 0; - svport = strtoul(p, &z, 10); - - if (svport != port || z==p) continue; - if (dgram && strncmp(z, "/udp", 4)) continue; - if (!dgram && strncmp(z, "/tcp", 4)) continue; - if (p-line > 32) continue; - - memcpy(buf, line, p-line); - break; - } - __fclose_ca(f); -} - -static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet) -{ - if (rr != RR_PTR) return 0; - if (__dn_expand(packet, (const unsigned char *)packet + 512, - data, c, 256) <= 0) - *(char *)c = 0; - return 0; - -} - -int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, - char *restrict node, socklen_t nodelen, - char *restrict serv, socklen_t servlen, - int flags) -{ - char ptr[PTR_MAX]; - char buf[256], num[3*sizeof(int)+1]; - int af = sa->sa_family; - unsigned char *a; - unsigned scopeid; - - switch (af) { - case AF_INET: - a = (void *)&((struct sockaddr_in *)sa)->sin_addr; - if (sl < sizeof(struct sockaddr_in)) return EAI_FAMILY; - mkptr4(ptr, a); - scopeid = 0; - break; - case AF_INET6: - a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr; - if (sl < sizeof(struct sockaddr_in6)) return EAI_FAMILY; - if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12)) - mkptr6(ptr, a); - else - mkptr4(ptr, a+12); - scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id; - break; - default: - return EAI_FAMILY; - } - - if (node && nodelen) { - buf[0] = 0; - if (!(flags & NI_NUMERICHOST)) { - reverse_hosts(buf, a, scopeid, af); - } - if (!*buf && !(flags & NI_NUMERICHOST)) { - unsigned char query[18+PTR_MAX], reply[512]; - int qlen = __res_mkquery(0, ptr, 1, RR_PTR, - 0, 0, 0, query, sizeof query); - query[3] = 0; /* don't need AD flag */ - int rlen = __res_send(query, qlen, reply, sizeof reply); - buf[0] = 0; - if (rlen > 0) - __dns_parse(reply, rlen, dns_parse_callback, buf); - } - if (!*buf) { - if (flags & NI_NAMEREQD) return EAI_NONAME; - inet_ntop(af, a, buf, sizeof buf); - if (scopeid) { - char *p = 0, tmp[IF_NAMESIZE+1]; - if (!(flags & NI_NUMERICSCOPE) && - (IN6_IS_ADDR_LINKLOCAL(a) || - IN6_IS_ADDR_MC_LINKLOCAL(a))) - p = if_indextoname(scopeid, tmp+1); - if (!p) - p = itoa(num, scopeid); - *--p = '%'; - strcat(buf, p); - } - } - if (strlen(buf) >= nodelen) return EAI_OVERFLOW; - strcpy(node, buf); - } - - if (serv && servlen) { - char *p = buf; - int port = ntohs(((struct sockaddr_in *)sa)->sin_port); - buf[0] = 0; - if (!(flags & NI_NUMERICSERV)) - reverse_services(buf, port, flags & NI_DGRAM); - if (!*p) - p = itoa(num, port); - if (strlen(p) >= servlen) - return EAI_OVERFLOW; - strcpy(serv, p); - } - - return 0; -} diff --git a/porting/liteos_a/user/src/network/getservbyname.c b/porting/liteos_a/user/src/network/getservbyname.c deleted file mode 100644 index dd3037678..000000000 --- a/porting/liteos_a/user/src/network/getservbyname.c +++ /dev/null @@ -1,12 +0,0 @@ -#define _GNU_SOURCE -#include - -struct servent *getservbyname(const char *name, const char *prots) -{ - static struct servent se; - static char *buf[2]; - struct servent *res; - if (getservbyname_r(name, prots, &se, (void *)buf, sizeof buf, &res)) - return 0; - return &se; -} diff --git a/porting/liteos_a/user/src/network/getservbyname_r.c b/porting/liteos_a/user/src/network/getservbyname_r.c deleted file mode 100644 index cad6317ab..000000000 --- a/porting/liteos_a/user/src/network/getservbyname_r.c +++ /dev/null @@ -1,55 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include "lookup.h" - -#define ALIGN (sizeof(struct { char a; char *b; }) - sizeof(char *)) - -int getservbyname_r(const char *name, const char *prots, - struct servent *se, char *buf, size_t buflen, struct servent **res) -{ - struct service servs[MAXSERVS]; - int cnt, proto, align; - - *res = 0; - - /* Don't treat numeric port number strings as service records. */ - char *end = ""; - strtoul(name, &end, 10); - if (!*end) return ENOENT; - - /* Align buffer */ - align = -(uintptr_t)buf & ALIGN-1; - if (buflen < 2*sizeof(char *)+align) - return ERANGE; - buf += align; - - if (!prots) proto = 0; - else if (!strcmp(prots, "tcp")) proto = IPPROTO_TCP; - else if (!strcmp(prots, "udp")) proto = IPPROTO_UDP; - else return EINVAL; - - cnt = __lookup_serv(servs, name, proto, 0, 0); - if (cnt<0) switch (cnt) { - case EAI_MEMORY: - case EAI_SYSTEM: - return ENOMEM; - default: - return ENOENT; - } - - se->s_name = (char *)name; - se->s_aliases = (void *)buf; - se->s_aliases[0] = se->s_name; - se->s_aliases[1] = 0; - se->s_port = htons(servs[0].port); - se->s_proto = servs[0].proto == IPPROTO_TCP ? "tcp" : "udp"; - - *res = se; - return 0; -} diff --git a/porting/liteos_a/user/src/network/getservbyport.c b/porting/liteos_a/user/src/network/getservbyport.c deleted file mode 100644 index c9ecbb11c..000000000 --- a/porting/liteos_a/user/src/network/getservbyport.c +++ /dev/null @@ -1,12 +0,0 @@ -#define _GNU_SOURCE -#include - -struct servent *getservbyport(int port, const char *prots) -{ - static struct servent se; - static long buf[32/sizeof(long)]; - struct servent *res; - if (getservbyport_r(port, prots, &se, (void *)buf, sizeof buf, &res)) - return 0; - return &se; -} diff --git a/porting/liteos_a/user/src/network/getservbyport_r.c b/porting/liteos_a/user/src/network/getservbyport_r.c deleted file mode 100644 index 65b239fd3..000000000 --- a/porting/liteos_a/user/src/network/getservbyport_r.c +++ /dev/null @@ -1,59 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include - -int getservbyport_r(int port, const char *prots, - struct servent *se, char *buf, size_t buflen, struct servent **res) -{ - int i; - struct sockaddr_in sin = { - .sin_family = AF_INET, - .sin_port = port, - }; - - if (!prots) { - int r = getservbyport_r(port, "tcp", se, buf, buflen, res); - if (r) r = getservbyport_r(port, "udp", se, buf, buflen, res); - return r; - } - *res = 0; - - /* Align buffer */ - i = (uintptr_t)buf & sizeof(char *)-1; - if (!i) i = sizeof(char *); - if (buflen < 3*sizeof(char *)-i) - return ERANGE; - buf += sizeof(char *)-i; - buflen -= sizeof(char *)-i; - - if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return EINVAL; - - se->s_port = port; - se->s_proto = (char *)prots; - se->s_aliases = (void *)buf; - buf += 2*sizeof(char *); - buflen -= 2*sizeof(char *); - se->s_aliases[1] = 0; - se->s_aliases[0] = se->s_name = buf; - - switch (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen, - strcmp(prots, "udp") ? 0 : NI_DGRAM)) { - case EAI_MEMORY: - case EAI_SYSTEM: - return ENOMEM; - default: - return ENOENT; - case 0: - break; - } - /* A numeric port string is not a service record. */ - if (strtol(buf, 0, 10)==ntohs(port)) return ENOENT; - - *res = se; - return 0; -} diff --git a/porting/liteos_a/user/src/network/if_indextoname.c b/porting/liteos_a/user/src/network/if_indextoname.c deleted file mode 100644 index e862ba476..000000000 --- a/porting/liteos_a/user/src/network/if_indextoname.c +++ /dev/null @@ -1,22 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include "syscall.h" - -char *if_indextoname(unsigned index, char *name) -{ - struct ifreq ifr; - int fd, r; - if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; - ifr.ifr_ifindex = index; - r = ioctl(fd, SIOCGIFNAME, &ifr); - __syscall(SYS_close, fd); - if (r < 0) { - if (errno == ENODEV) errno = ENXIO; - return 0; - } - return strncpy(name, ifr.ifr_name, IF_NAMESIZE); -} diff --git a/porting/liteos_a/user/src/network/if_nameindex.c b/porting/liteos_a/user/src/network/if_nameindex.c deleted file mode 100644 index a9990fa39..000000000 --- a/porting/liteos_a/user/src/network/if_nameindex.c +++ /dev/null @@ -1,115 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include "netlink.h" - -#define IFADDRS_HASH_SIZE 64 - -struct ifnamemap { - unsigned int hash_next; - unsigned int index; - unsigned char namelen; - char name[IFNAMSIZ]; -}; - -struct ifnameindexctx { - unsigned int num, allocated, str_bytes; - struct ifnamemap *list; - unsigned int hash[IFADDRS_HASH_SIZE]; -}; - -static int netlink_msg_to_nameindex(void *pctx, struct nlmsghdr *h) -{ - struct ifnameindexctx *ctx = pctx; - struct ifnamemap *map; - struct rtattr *rta; - unsigned int i; - int index, type, namelen, bucket; - - if (h->nlmsg_type == RTM_NEWLINK) { - struct ifinfomsg *ifi = NLMSG_DATA(h); - index = ifi->ifi_index; - type = IFLA_IFNAME; - rta = NLMSG_RTA(h, sizeof(*ifi)); - } else { - struct ifaddrmsg *ifa = NLMSG_DATA(h); - index = ifa->ifa_index; - type = IFA_LABEL; - rta = NLMSG_RTA(h, sizeof(*ifa)); - } - for (; NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) { - if (rta->rta_type != type) continue; - - namelen = RTA_DATALEN(rta) - 1; - if (namelen > IFNAMSIZ) return 0; - - /* suppress duplicates */ - bucket = index % IFADDRS_HASH_SIZE; - i = ctx->hash[bucket]; - while (i) { - map = &ctx->list[i-1]; - if (map->index == index && - map->namelen == namelen && - memcmp(map->name, RTA_DATA(rta), namelen) == 0) - return 0; - i = map->hash_next; - } - - if (ctx->num >= ctx->allocated) { - size_t a = ctx->allocated ? ctx->allocated * 2 + 1 : 8; - if (a > SIZE_MAX/sizeof *map) return -1; - map = realloc(ctx->list, a * sizeof *map); - if (!map) return -1; - ctx->list = map; - ctx->allocated = a; - } - map = &ctx->list[ctx->num]; - map->index = index; - map->namelen = namelen; - memcpy(map->name, RTA_DATA(rta), namelen); - ctx->str_bytes += namelen + 1; - ctx->num++; - map->hash_next = ctx->hash[bucket]; - ctx->hash[bucket] = ctx->num; - return 0; - } - return 0; -} - -struct if_nameindex *if_nameindex() -{ - struct ifnameindexctx _ctx, *ctx = &_ctx; - struct if_nameindex *ifs = 0, *d; - struct ifnamemap *s; - char *p; - int i; - int cs; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - memset(ctx, 0, sizeof(*ctx)); - if (__rtnetlink_enumerate(AF_UNSPEC, AF_INET, netlink_msg_to_nameindex, ctx) < 0) goto err; - - ifs = malloc(sizeof(struct if_nameindex[ctx->num+1]) + ctx->str_bytes); - if (!ifs) goto err; - - p = (char*)(ifs + ctx->num + 1); - for (i = ctx->num, d = ifs, s = ctx->list; i; i--, s++, d++) { - d->if_index = s->index; - d->if_name = p; - memcpy(p, s->name, s->namelen); - p += s->namelen; - *p++ = 0; - } - d->if_index = 0; - d->if_name = 0; -err: - pthread_setcancelstate(cs, 0); - free(ctx->list); - errno = ENOBUFS; - return ifs; -} diff --git a/porting/liteos_a/user/src/network/if_nametoindex.c b/porting/liteos_a/user/src/network/if_nametoindex.c deleted file mode 100644 index 52cb1c259..000000000 --- a/porting/liteos_a/user/src/network/if_nametoindex.c +++ /dev/null @@ -1,18 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include "syscall.h" - -unsigned if_nametoindex(const char *name) -{ - struct ifreq ifr; - int fd, r; - - if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; - strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); - r = ioctl(fd, SIOCGIFINDEX, &ifr); - __syscall(SYS_close, fd); - return r < 0 ? 0 : ifr.ifr_ifindex; -} diff --git a/porting/liteos_a/user/src/network/inet_legacy.c b/porting/liteos_a/user/src/network/inet_legacy.c deleted file mode 100644 index 6d91b6e17..000000000 --- a/porting/liteos_a/user/src/network/inet_legacy.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include - -in_addr_t inet_network(const char *p) -{ - return ntohl(inet_addr(p)); -} - -struct in_addr inet_makeaddr(in_addr_t n, in_addr_t h) -{ - if (n < 256) h |= n<<24; - else if (n < 65536) h |= n<<16; - else h |= n<<8; - return (struct in_addr){ htonl(h) }; -} - -in_addr_t inet_lnaof(struct in_addr in) -{ - uint32_t h = ntohl(in.s_addr); - if (h>>24 < 128) return h & 0xffffff; - if (h>>24 < 192) return h & 0xffff; - return h & 0xff; -} - -in_addr_t inet_netof(struct in_addr in) -{ - uint32_t h = ntohl(in.s_addr); - if (h>>24 < 128) return h >> 24; - if (h>>24 < 192) return h >> 16; - return h >> 8; -} diff --git a/porting/liteos_a/user/src/network/netname.c b/porting/liteos_a/user/src/network/netname.c deleted file mode 100644 index d11d7b00b..000000000 --- a/porting/liteos_a/user/src/network/netname.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include - -struct netent *getnetbyaddr(uint32_t net, int addrtype) -{ - struct netent *ne = NULL; - setnetent(1); - while (1) { - ne = getnetent(); - if (!ne) - break; - if (ne->n_net == net && ne->n_addrtype == addrtype) { - setnetent(0); - endnetent(); - return ne; - } - } - setnetent(0); - endnetent(); - return NULL; -} - -struct netent *getnetbyname(const char *netname) -{ - struct netent *ne = NULL; - setnetent(1); - while (1) { - ne = getnetent(); - if (!ne) - break; - if (strcmp(ne->n_name, netname) == 0) { - setnetent(0); - endnetent(); - return ne; - } - } - setnetent(0); - endnetent(); - return NULL; -} - diff --git a/porting/liteos_a/user/src/network/proto.c b/porting/liteos_a/user/src/network/proto.c deleted file mode 100644 index c4fd34efb..000000000 --- a/porting/liteos_a/user/src/network/proto.c +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include - -/* do we really need all these?? */ - -static int idx; -static const unsigned char protos[] = { - "\000ip\0" - "\001icmp\0" - "\002igmp\0" - "\003ggp\0" - "\004ipencap\0" - "\005st\0" - "\006tcp\0" - "\010egp\0" - "\014pup\0" - "\021udp\0" - "\024hmp\0" - "\026xns-idp\0" - "\033rdp\0" - "\035iso-tp4\0" - "\044xtp\0" - "\045ddp\0" - "\046idpr-cmtp\0" - "\051ipv6\0" - "\053ipv6-route\0" - "\054ipv6-frag\0" - "\055idrp\0" - "\056rsvp\0" - "\057gre\0" - "\062esp\0" - "\063ah\0" - "\071skip\0" - "\072ipv6-icmp\0" - "\073ipv6-nonxt\0" - "\074ipv6-opts\0" - "\111rspf\0" - "\121vmtp\0" - "\131ospf\0" - "\136ipip\0" - "\142encap\0" - "\147pim\0" - "\377raw" -}; - -void endprotoent(void) -{ - idx = 0; -} - -void setprotoent(int stayopen) -{ - idx = 0; -} - -struct protoent *getprotoent(void) -{ - static struct protoent p; - static const char *aliases; - if (idx >= sizeof protos) return NULL; - p.p_proto = protos[idx]; - p.p_name = (char *)&protos[idx+1]; - p.p_aliases = (char **)&aliases; - idx += strlen(p.p_name) + 2; - return &p; -} - -struct protoent *getprotobyname(const char *name) -{ - struct protoent *p; - endprotoent(); - do p = getprotoent(); - while (p && strcmp(name, p->p_name)); - return p; -} - -struct protoent *getprotobynumber(int num) -{ - struct protoent *p; - endprotoent(); - do p = getprotoent(); - while (p && p->p_proto != num); - return p; -} diff --git a/porting/liteos_a/user/src/network/recvmsg.c b/porting/liteos_a/user/src/network/recvmsg.c deleted file mode 100644 index 29f01cb62..000000000 --- a/porting/liteos_a/user/src/network/recvmsg.c +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" - -hidden void __convert_scm_timestamps(struct msghdr *, socklen_t); - -void __convert_scm_timestamps(struct msghdr *msg, socklen_t csize) -{ - if (SCM_TIMESTAMP == SCM_TIMESTAMP_OLD) return; - if (!msg->msg_control || !msg->msg_controllen) return; - - struct cmsghdr *cmsg, *last=0; - long tmp; - long long tvts[2]; - int type = 0; - - for (cmsg=CMSG_FIRSTHDR(msg); cmsg; cmsg=CMSG_NXTHDR(msg, cmsg)) { - if (cmsg->cmsg_level==SOL_SOCKET) switch (cmsg->cmsg_type) { - case SCM_TIMESTAMP_OLD: - if (type) break; - type = SCM_TIMESTAMP; - goto common; - case SCM_TIMESTAMPNS_OLD: - type = SCM_TIMESTAMPNS; - common: - memcpy(&tmp, CMSG_DATA(cmsg), sizeof tmp); - tvts[0] = tmp; - memcpy(&tmp, CMSG_DATA(cmsg) + sizeof tmp, sizeof tmp); - tvts[1] = tmp; - break; - } - last = cmsg; - } - if (!last || !type) return; - if (CMSG_SPACE(sizeof tvts) > csize-msg->msg_controllen) { - msg->msg_flags |= MSG_CTRUNC; - return; - } - msg->msg_controllen += CMSG_SPACE(sizeof tvts); - cmsg = CMSG_NXTHDR(msg, last); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = type; - cmsg->cmsg_len = CMSG_LEN(sizeof tvts); - memcpy(CMSG_DATA(cmsg), &tvts, sizeof tvts); -} - -ssize_t recvmsg(int fd, struct msghdr *msg, int flags) -{ - ssize_t r; - if (!msg) { - errno = EFAULT; - return -1; - } - socklen_t orig_controllen = msg->msg_controllen; -#if LONG_MAX > INT_MAX - struct msghdr h, *orig = msg; - if (msg) { - h = *msg; - h.__pad1 = h.__pad2 = 0; - msg = &h; - } -#endif - r = socketcall_cp(recvmsg, fd, msg, flags, 0, 0, 0); - if (r >= 0) __convert_scm_timestamps(msg, orig_controllen); -#if LONG_MAX > INT_MAX - if (orig) *orig = h; -#endif - return r; -} diff --git a/porting/liteos_a/user/src/network/serv.c b/porting/liteos_a/user/src/network/serv.c deleted file mode 100644 index 38f000721..000000000 --- a/porting/liteos_a/user/src/network/serv.c +++ /dev/null @@ -1,110 +0,0 @@ -#define _BSD_SOURCE -#include -#include -#include -#include -#include - -#define SERV_FILE_PATH "/etc/services" -#define BUFFER_SIZE 200 -#define MAX_PORT 65535 -#define MAX_ALIAS_NO 10 - -struct servdata -{ - FILE *servf; - int stayopen; - struct servent se; - char buf[BUFFER_SIZE]; - char *alias_list[MAX_ALIAS_NO + 1]; -}; -static struct servdata *sd = NULL; - -static int servent_resolv() -{ - if (sd->buf[0] == '#') - return -1; - char *ch, *temp, *ptr = NULL; - ch = strchr(sd->buf, '#'); - if (ch) - *ch = '\0'; - ch = strtok_r(sd->buf, " \t\n", &ptr); - temp = strtok_r(NULL, " \t\n", &ptr); - if (!ch || !temp) - return -1; - - sd->se.s_name = ch; - ch = strchr(temp, '/'); - if (ch == NULL) - return -1; - *ch++ = '\0'; - sd->se.s_port = atoi(temp); - if (sd->se.s_port < 1 || sd->se.s_port > MAX_PORT) - return -1; - sd->se.s_port = htons(sd->se.s_port); - if (strcmp(ch, "tcp") != 0 && strcmp(ch, "udp") != 0) - return -1; - sd->se.s_proto = ch; - - int i = 0; - while (i < MAX_ALIAS_NO) { - ch = strtok_r(NULL, " \t\n", &ptr); - sd->alias_list[i++] = ch; - } - sd->alias_list[MAX_ALIAS_NO] = NULL; - sd->se.s_aliases = sd->alias_list; - return 0; -} - -struct servent *getservent(void) -{ - if (sd == NULL) { - if ((sd = malloc(sizeof(struct servdata))) == NULL) { - errno = ENOMEM; - return NULL; - } - sd->servf = NULL; - sd->stayopen = 0; - } - if (!sd->servf && !(sd->servf = fopen(SERV_FILE_PATH, "r"))) { - errno = ENOENT; - free(sd); - sd = NULL; - return NULL; - } - do { - if (!fgets(sd->buf, BUFFER_SIZE - 1, sd->servf)) { - errno = EINVAL; - return NULL; - } - } while (servent_resolv()); - return &(sd->se); -} - -void endservent(void) -{ - if (sd == NULL) - return; - if (sd->stayopen == 0) { - if (sd->servf != NULL) - fclose(sd->servf); - free(sd); - sd = NULL; - } -} - -void setservent(int stayopen) -{ - if (sd == NULL) { - if ((sd = malloc(sizeof(struct servdata))) == NULL) { - errno = ENOMEM; - return; - } - sd->servf = NULL; - } - if (!sd->servf) - sd->servf = fopen(SERV_FILE_PATH, "r"); - else - rewind(sd->servf); - sd->stayopen = stayopen; -} \ No newline at end of file diff --git a/porting/liteos_a/user/src/network/sockatmark.c b/porting/liteos_a/user/src/network/sockatmark.c deleted file mode 100644 index 8162c8c73..000000000 --- a/porting/liteos_a/user/src/network/sockatmark.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include - -int sockatmark(int s) -{ - int ret; - unsupported_api(__FUNCTION__); - if (ioctl(s, SIOCATMARK, &ret) < 0) - return -1; - return ret; -} diff --git a/porting/liteos_a/user/src/network/socket.c b/porting/liteos_a/user/src/network/socket.c deleted file mode 100644 index 7eaa226bd..000000000 --- a/porting/liteos_a/user/src/network/socket.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "syscall.h" - -#if OHOS_PERMISSION_INTERNET -typedef uint8_t (*AllowFunc)(void); -static const char *LIB_NETSYS_CLIENT_NAME = "libnetsys_client.z.so"; -static const char *ALLOW_SOCKET_FUNC_NAME = "IsAllowInternet"; - -/* - * Read a flag from netsys_client, there is only one place to set this flag, is the - * founction named DoStartup in startup_appspawn. - * */ -uint8_t is_allow_internet(void) -{ - static uint8_t first_time = 1; - static uint8_t allow = 1; - - if (!first_time) { - return allow; - } - - void *handler = dlopen(LIB_NETSYS_CLIENT_NAME, RTLD_LAZY); - if (handler != NULL) { - AllowFunc func = (AllowFunc)dlsym(handler, ALLOW_SOCKET_FUNC_NAME); - if (func != NULL && func() == 0) { - allow = 0; - } - dlclose(handler); - } - first_time = 0; - return allow; -} -#endif - -int socket(int domain, int type, int protocol) -{ -#if OHOS_PERMISSION_INTERNET - if ((domain == AF_INET || domain == AF_INET6) && is_allow_internet() == 0) { - errno = EPERM; - return -1; - } -#endif - - int s = __socketcall(socket, domain, type, protocol, 0, 0, 0); - if ((s==-EINVAL || s==-EPROTONOSUPPORT) - && (type&(SOCK_CLOEXEC|SOCK_NONBLOCK))) { - s = __socketcall(socket, domain, - type & ~(SOCK_CLOEXEC|SOCK_NONBLOCK), - protocol, 0, 0, 0); - if (s < 0) return __syscall_ret(s); - if (type & SOCK_CLOEXEC) - __syscall(SYS_fcntl, s, F_SETFD, FD_CLOEXEC); - if (type & SOCK_NONBLOCK) - __syscall(SYS_fcntl, s, F_SETFL, O_NONBLOCK); - } - return __syscall_ret(s); -} diff --git a/porting/liteos_a/user/src/passwd/nscd_query.c b/porting/liteos_a/user/src/passwd/nscd_query.c deleted file mode 100644 index dc3406b85..000000000 --- a/porting/liteos_a/user/src/passwd/nscd_query.c +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "nscd.h" - -static const struct { - short sun_family; - char sun_path[21]; -} addr = { - AF_UNIX, - "/var/run/nscd/socket" -}; - -FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap) -{ - size_t i; - int fd; - FILE *f = 0; - int32_t req_buf[REQ_LEN] = { - NSCDVERSION, - req, - strnlen(key,LOGIN_NAME_MAX)+1 - }; - struct msghdr msg = { - .msg_iov = (struct iovec[]){ - {&req_buf, sizeof(req_buf)}, - {(char*)key, strlen(key)+1} - }, - .msg_iovlen = 2 - }; - int errno_save = errno; - - *swap = 0; -retry: - memset(buf, 0, len); - buf[0] = NSCDVERSION; - - fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (fd < 0) { - if (errno == EAFNOSUPPORT) { - f = fopen("/dev/null", "re"); - if (f) - errno = errno_save; - return f; - } - return 0; - } - - if(!(f = fdopen(fd, "r"))) { - close(fd); - return 0; - } - - if (req_buf[2] > LOGIN_NAME_MAX) - return f; - - if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - /* If there isn't a running nscd we simulate a "not found" - * result and the caller is responsible for calling - * fclose on the (unconnected) socket. The value of - * errno must be left unchanged in this case. */ - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) { - errno = errno_save; - return f; - } - goto error; - } - - if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) - goto error; - - if (!fread(buf, len, 1, f)) { - /* If the VERSION entry mismatches nscd will disconnect. The - * most likely cause is that the endianness mismatched. So, we - * byteswap and try once more. (if we already swapped, just - * fail out) - */ - if (ferror(f)) goto error; - if (!*swap) { - fclose(f); - for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) { - req_buf[i] = bswap_32(req_buf[i]); - } - *swap = 1; - goto retry; - } else { - errno = EIO; - goto error; - } - } - - if (*swap) { - for (i = 0; i < len/sizeof(buf[0]); i++) { - buf[i] = bswap_32(buf[i]); - } - } - - /* The first entry in every nscd response is the version number. This - * really shouldn't happen, and is evidence of some form of malformed - * response. - */ - if(buf[0] != NSCDVERSION) { - errno = EIO; - goto error; - } - - return f; -error: - fclose(f); - return 0; -} diff --git a/porting/liteos_a/user/src/process/_Fork.c b/porting/liteos_a/user/src/process/_Fork.c deleted file mode 100644 index 44b1c4f49..000000000 --- a/porting/liteos_a/user/src/process/_Fork.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include "syscall.h" -#include "libc.h" -#include "lock.h" -#include "pthread_impl.h" -#include "aio_impl.h" - -static void dummy(int x) { } -weak_alias(dummy, __aio_atfork); - -pid_t _Fork(void) -{ - pid_t ret; - sigset_t set; - __block_all_sigs(&set); - __aio_atfork(-1); - LOCK(__abort_lock); -#ifdef SYS_fork - ret = __syscall(SYS_fork); -#else - ret = __syscall(SYS_clone, SIGCHLD, 0); -#endif - if (!ret) { - pthread_t self = __pthread_self(); - self->tid = __syscall(SYS_gettid); - self->robust_list.off = 0; - self->robust_list.pending = 0; - self->next = self->prev = self; - __thread_list_lock = 0; - libc.threads_minus_1 = 0; - if (libc.need_locks) libc.need_locks = -1; - libc.exit = 0; - signal(SIGSYS, arm_do_signal); - } - UNLOCK(__abort_lock); - __aio_atfork(!ret); - __restore_sigs(&set); - return __syscall_ret(ret); -} diff --git a/porting/liteos_a/user/src/process/fexecve.c b/porting/liteos_a/user/src/process/fexecve.c deleted file mode 100644 index cc02ad936..000000000 --- a/porting/liteos_a/user/src/process/fexecve.c +++ /dev/null @@ -1,18 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include "syscall.h" -#include - -int fexecve(int fd, char *const argv[], char *const envp[]) -{ - unsupported_api(__FUNCTION__); - int r = __syscall(SYS_execveat, fd, "", argv, envp, AT_EMPTY_PATH); - if (r != -ENOSYS) return __syscall_ret(r); - char buf[15 + 3*sizeof(int)]; - __procfdname(buf, fd); - execve(buf, argv, envp); - if (errno == ENOENT) errno = EBADF; - return -1; -} diff --git a/porting/liteos_a/user/src/process/posix_spawn.c b/porting/liteos_a/user/src/process/posix_spawn.c deleted file mode 100644 index 46eeb5fd1..000000000 --- a/porting/liteos_a/user/src/process/posix_spawn.c +++ /dev/null @@ -1,217 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#include "lock.h" -#include "pthread_impl.h" -#include "fdop.h" - -struct args { - int p[2]; - sigset_t oldmask; - const char *path; - const posix_spawn_file_actions_t *fa; - const posix_spawnattr_t *restrict attr; - char *const *argv, *const *envp; -}; - -static int __sys_dup2(int old, int new) -{ -#ifdef SYS_dup2 - return __syscall(SYS_dup2, old, new); -#else - return __syscall(SYS_dup3, old, new, 0); -#endif -} - -static int child(void *args_vp) -{ - int i, ret; - struct sigaction sa = {0}; - struct args *args = args_vp; - int p = args->p[1]; - const posix_spawn_file_actions_t *fa = args->fa; - const posix_spawnattr_t *restrict attr = args->attr; - sigset_t hset; - - close(args->p[0]); - - /* All signal dispositions must be either SIG_DFL or SIG_IGN - * before signals are unblocked. Otherwise a signal handler - * from the parent might get run in the child while sharing - * memory, with unpredictable and dangerous results. To - * reduce overhead, sigaction has tracked for us which signals - * potentially have a signal handler. */ - __get_handler_set(&hset); - for (i=1; i<_NSIG; i++) { - if ((attr->__flags & POSIX_SPAWN_SETSIGDEF) - && sigismember(&attr->__def, i)) { - sa.sa_handler = SIG_DFL; - } else if (sigismember(&hset, i)) { - if (i-32<3U) { - sa.sa_handler = SIG_IGN; - } else { - __libc_sigaction(i, 0, &sa); - if (sa.sa_handler==SIG_IGN) continue; - sa.sa_handler = SIG_DFL; - } - } else { - continue; - } - __libc_sigaction(i, &sa, 0); - } - - if (attr->__flags & POSIX_SPAWN_SETSID) - if ((ret=__syscall(SYS_setsid)) < 0) - goto fail; - - if (attr->__flags & POSIX_SPAWN_SETPGROUP) - if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp))) - goto fail; - - /* Use syscalls directly because the library functions attempt - * to do a multi-threaded synchronized id-change, which would - * trash the parent's state. */ - if (attr->__flags & POSIX_SPAWN_RESETIDS) - if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) || - (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) ) - goto fail; - - if (fa && fa->__actions) { - struct fdop *op; - int fd; - for (op = fa->__actions; op->next; op = op->next); - for (; op; op = op->prev) { - /* It's possible that a file operation would clobber - * the pipe fd used for synchronizing with the - * parent. To avoid that, we dup the pipe onto - * an unoccupied fd. */ - if (op->fd == p) { - ret = __syscall(SYS_dup, p); - if (ret < 0) goto fail; - __syscall(SYS_close, p); - p = ret; - } - switch(op->cmd) { - case FDOP_CLOSE: - __syscall(SYS_close, op->fd); - break; - case FDOP_DUP2: - fd = op->srcfd; - if (fd == p) { - ret = -EBADF; - goto fail; - } - if (fd != op->fd) { - if ((ret=__sys_dup2(fd, op->fd))<0) - goto fail; - } else { - ret = __syscall(SYS_fcntl, fd, F_GETFD); - ret = __syscall(SYS_fcntl, fd, F_SETFD, - ret & ~FD_CLOEXEC); - if (ret<0) - goto fail; - } - break; - case FDOP_OPEN: - fd = __sys_open(op->path, op->oflag, op->mode); - if ((ret=fd) < 0) goto fail; - if (fd != op->fd) { - if ((ret=__sys_dup2(fd, op->fd))<0) - goto fail; - __syscall(SYS_close, fd); - } - break; - case FDOP_CHDIR: - ret = __syscall(SYS_chdir, op->path); - if (ret<0) goto fail; - break; - case FDOP_FCHDIR: - ret = __syscall(SYS_fchdir, op->fd); - if (ret<0) goto fail; - break; - } - } - } - - /* Close-on-exec flag may have been lost if we moved the pipe - * to a different fd. We don't use F_DUPFD_CLOEXEC above because - * it would fail on older kernels and atomicity is not needed -- - * in this process there are no threads or signal handlers. */ - __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC); - - pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK) - ? &attr->__mask : &args->oldmask, 0); - - int (*exec)(const char *, char *const *, char *const *) = - attr->__fn ? (int (*)())attr->__fn : execve; - - exec(args->path, args->argv, args->envp); - ret = -errno; - -fail: - /* Since sizeof errno < PIPE_BUF, the write is atomic. */ - ret = -ret; - if (ret) while (__syscall(SYS_write, p, &ret, sizeof ret) < 0); - _exit(127); -} - - -int posix_spawn(pid_t *restrict res, const char *restrict path, - const posix_spawn_file_actions_t *fa, - const posix_spawnattr_t *restrict attr, - char *const argv[restrict], char *const envp[restrict]) -{ - pid_t pid; - char stack[1024+PATH_MAX]; - int ec=0, cs; - struct args args; - - unsupported_api(__FUNCTION__); - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - - args.path = path; - args.fa = fa; - args.attr = attr ? attr : &(const posix_spawnattr_t){0}; - args.argv = argv; - args.envp = envp; - pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask); - - /* The lock guards both against seeing a SIGABRT disposition change - * by abort and against leaking the pipe fd to fork-without-exec. */ - LOCK(__abort_lock); - - if (pipe2(args.p, O_CLOEXEC)) { - UNLOCK(__abort_lock); - ec = errno; - goto fail; - } - - pid = __clone(child, stack+sizeof stack, - CLONE_VM|CLONE_VFORK|SIGCHLD, &args); - close(args.p[1]); - UNLOCK(__abort_lock); - - if (pid > 0) { - if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0; - else waitpid(pid, &(int){0}, 0); - } else { - ec = -pid; - } - - close(args.p[0]); - - if (!ec && res) *res = pid; - -fail: - pthread_sigmask(SIG_SETMASK, &args.oldmask, 0); - pthread_setcancelstate(cs, 0); - - return ec; -} diff --git a/porting/liteos_a/user/src/process/posix_spawn_file_actions_addclose.c b/porting/liteos_a/user/src/process/posix_spawn_file_actions_addclose.c deleted file mode 100644 index 3f8265848..000000000 --- a/porting/liteos_a/user/src/process/posix_spawn_file_actions_addclose.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#include "fdop.h" -#include - -int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa, int fd) -{ - if (fd < 0) return EBADF; - unsupported_api(__FUNCTION__); - struct fdop *op = malloc(sizeof *op); - if (!op) return ENOMEM; - op->cmd = FDOP_CLOSE; - op->fd = fd; - if ((op->next = fa->__actions)) op->next->prev = op; - op->prev = 0; - fa->__actions = op; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawn_file_actions_adddup2.c b/porting/liteos_a/user/src/process/posix_spawn_file_actions_adddup2.c deleted file mode 100644 index 49bf0fa96..000000000 --- a/porting/liteos_a/user/src/process/posix_spawn_file_actions_adddup2.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include "fdop.h" -#include - -int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa, int srcfd, int fd) -{ - if (srcfd < 0 || fd < 0) return EBADF; - unsupported_api(__FUNCTION__); - struct fdop *op = malloc(sizeof *op); - if (!op) return ENOMEM; - op->cmd = FDOP_DUP2; - op->srcfd = srcfd; - op->fd = fd; - if ((op->next = fa->__actions)) op->next->prev = op; - op->prev = 0; - fa->__actions = op; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawn_file_actions_addopen.c b/porting/liteos_a/user/src/process/posix_spawn_file_actions_addopen.c deleted file mode 100644 index 548fe2ebe..000000000 --- a/porting/liteos_a/user/src/process/posix_spawn_file_actions_addopen.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include -#include -#include "fdop.h" -#include - -int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *restrict fa, int fd, const char *restrict path, int flags, mode_t mode) -{ - if (fd < 0) return EBADF; - unsupported_api(__FUNCTION__); - struct fdop *op = malloc(sizeof *op + strlen(path) + 1); - if (!op) return ENOMEM; - op->cmd = FDOP_OPEN; - op->fd = fd; - op->oflag = flags; - op->mode = mode; - strcpy(op->path, path); - if ((op->next = fa->__actions)) op->next->prev = op; - op->prev = 0; - fa->__actions = op; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_destroy.c b/porting/liteos_a/user/src/process/posix_spawnattr_destroy.c deleted file mode 100644 index b66614fc4..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_destroy.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -int posix_spawnattr_destroy(posix_spawnattr_t *attr) -{ - unsupported_api(__FUNCTION__); - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_getflags.c b/porting/liteos_a/user/src/process/posix_spawnattr_getflags.c deleted file mode 100644 index d3942db77..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_getflags.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int posix_spawnattr_getflags(const posix_spawnattr_t *restrict attr, short *restrict flags) -{ - unsupported_api(__FUNCTION__); - *flags = attr->__flags; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_getpgroup.c b/porting/liteos_a/user/src/process/posix_spawnattr_getpgroup.c deleted file mode 100644 index be42a87e2..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_getpgroup.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int posix_spawnattr_getpgroup(const posix_spawnattr_t *restrict attr, pid_t *restrict pgrp) -{ - unsupported_api(__FUNCTION__); - *pgrp = attr->__pgrp; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_getsigdefault.c b/porting/liteos_a/user/src/process/posix_spawnattr_getsigdefault.c deleted file mode 100644 index 6687b8640..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_getsigdefault.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int posix_spawnattr_getsigdefault(const posix_spawnattr_t *restrict attr, sigset_t *restrict def) -{ - unsupported_api(__FUNCTION__); - *def = attr->__def; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_getsigmask.c b/porting/liteos_a/user/src/process/posix_spawnattr_getsigmask.c deleted file mode 100644 index 5d56dae47..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_getsigmask.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int posix_spawnattr_getsigmask(const posix_spawnattr_t *restrict attr, sigset_t *restrict mask) -{ - unsupported_api(__FUNCTION__); - *mask = attr->__mask; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_init.c b/porting/liteos_a/user/src/process/posix_spawnattr_init.c deleted file mode 100644 index cf2dc2754..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_init.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int posix_spawnattr_init(posix_spawnattr_t *attr) -{ - unsupported_api(__FUNCTION__); - *attr = (posix_spawnattr_t){ 0 }; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_sched.c b/porting/liteos_a/user/src/process/posix_spawnattr_sched.c deleted file mode 100644 index f7a3cc03f..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_sched.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include - -int posix_spawnattr_getschedparam(const posix_spawnattr_t *restrict attr, - struct sched_param *restrict schedparam) -{ - unsupported_api(__FUNCTION__); - return ENOSYS; -} - -int posix_spawnattr_setschedparam(posix_spawnattr_t *restrict attr, - const struct sched_param *restrict schedparam) -{ - unsupported_api(__FUNCTION__); - return ENOSYS; -} - -int posix_spawnattr_getschedpolicy(const posix_spawnattr_t *restrict attr, int *restrict policy) -{ - unsupported_api(__FUNCTION__); - return ENOSYS; -} - -int posix_spawnattr_setschedpolicy(posix_spawnattr_t *attr, int policy) -{ - unsupported_api(__FUNCTION__); - return ENOSYS; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_setflags.c b/porting/liteos_a/user/src/process/posix_spawnattr_setflags.c deleted file mode 100644 index afeb2af5f..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_setflags.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include - -int posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags) -{ - unsupported_api(__FUNCTION__); - const unsigned all_flags = - POSIX_SPAWN_RESETIDS | - POSIX_SPAWN_SETPGROUP | - POSIX_SPAWN_SETSIGDEF | - POSIX_SPAWN_SETSIGMASK | - POSIX_SPAWN_SETSCHEDPARAM | - POSIX_SPAWN_SETSCHEDULER | - POSIX_SPAWN_USEVFORK | - POSIX_SPAWN_SETSID; - if (flags & ~all_flags) return EINVAL; - attr->__flags = flags; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_setpgroup.c b/porting/liteos_a/user/src/process/posix_spawnattr_setpgroup.c deleted file mode 100644 index 217cb9e0b..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_setpgroup.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -int posix_spawnattr_setpgroup(posix_spawnattr_t *attr, pid_t pgrp) -{ - unsupported_api(__FUNCTION__); - attr->__pgrp = pgrp; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_setsigdefault.c b/porting/liteos_a/user/src/process/posix_spawnattr_setsigdefault.c deleted file mode 100644 index 4c142b343..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_setsigdefault.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -int posix_spawnattr_setsigdefault(posix_spawnattr_t *restrict attr, const sigset_t *restrict def) -{ - unsupported_api(__FUNCTION__); - - attr->__def = *def; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnattr_setsigmask.c b/porting/liteos_a/user/src/process/posix_spawnattr_setsigmask.c deleted file mode 100644 index 46173effe..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnattr_setsigmask.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -int posix_spawnattr_setsigmask(posix_spawnattr_t *restrict attr, const sigset_t *restrict mask) -{ - unsupported_api(__FUNCTION__); - - attr->__mask = *mask; - return 0; -} diff --git a/porting/liteos_a/user/src/process/posix_spawnp.c b/porting/liteos_a/user/src/process/posix_spawnp.c deleted file mode 100644 index e1ac20d68..000000000 --- a/porting/liteos_a/user/src/process/posix_spawnp.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include - -int posix_spawnp(pid_t *restrict res, const char *restrict file, - const posix_spawn_file_actions_t *fa, - const posix_spawnattr_t *restrict attr, - char *const argv[restrict], char *const envp[restrict]) -{ - posix_spawnattr_t spawnp_attr = { 0 }; - unsupported_api(__FUNCTION__); - if (attr) spawnp_attr = *attr; - spawnp_attr.__fn = (void *)__execvpe; - return posix_spawn(res, file, fa, &spawnp_attr, argv, envp); -} diff --git a/porting/liteos_a/user/src/process/system.c b/porting/liteos_a/user/src/process/system.c deleted file mode 100644 index 50aaa7532..000000000 --- a/porting/liteos_a/user/src/process/system.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "pthread_impl.h" - -extern char **__environ; - -int system(const char *cmd) -{ - pid_t pid; - sigset_t old, reset; - struct sigaction sa = { .sa_handler = SIG_IGN }, oldint, oldquit; - int status = -1, ret; - posix_spawnattr_t attr; - - unsupported_api(__FUNCTION__); - - pthread_testcancel(); - - if (!cmd) return 1; - - sigaction(SIGINT, &sa, &oldint); - sigaction(SIGQUIT, &sa, &oldquit); - sigaddset(&sa.sa_mask, SIGCHLD); - sigprocmask(SIG_BLOCK, &sa.sa_mask, &old); - - sigemptyset(&reset); - if (oldint.sa_handler != SIG_IGN) sigaddset(&reset, SIGINT); - if (oldquit.sa_handler != SIG_IGN) sigaddset(&reset, SIGQUIT); - posix_spawnattr_init(&attr); - posix_spawnattr_setsigmask(&attr, &old); - posix_spawnattr_setsigdefault(&attr, &reset); - posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF|POSIX_SPAWN_SETSIGMASK); - ret = posix_spawn(&pid, "/bin/sh", 0, &attr, - (char *[]){"sh", "-c", (char *)cmd, 0}, __environ); - posix_spawnattr_destroy(&attr); - - if (!ret) while (waitpid(pid, &status, 0)<0 && errno == EINTR); - sigaction(SIGINT, &oldint, NULL); - sigaction(SIGQUIT, &oldquit, NULL); - sigprocmask(SIG_SETMASK, &old, NULL); - - if (ret) errno = ret; - return status; -} diff --git a/porting/liteos_a/user/src/sched/affinity.c b/porting/liteos_a/user/src/sched/affinity.c deleted file mode 100644 index 14b061f2d..000000000 --- a/porting/liteos_a/user/src/sched/affinity.c +++ /dev/null @@ -1,46 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include "pthread_impl.h" -#include "syscall.h" - -int sched_setaffinity(pid_t tid, size_t size, const cpu_set_t *set) -{ - if (size != sizeof(cpu_set_t)) { - errno = EINVAL; - return -1; - } - return syscall(SYS_sched_setaffinity, tid, (unsigned short)(set->__bits[0]), MUSL_TYPE_PROCESS); -} - -int pthread_setaffinity_np(pthread_t td, size_t size, const cpu_set_t *set) -{ - if (size != sizeof(cpu_set_t)) { - return EINVAL; - } - return -__syscall(SYS_sched_setaffinity, td->tid, (unsigned short)(set->__bits[0]), MUSL_TYPE_THREAD); -} - -static int do_getaffinity(pid_t tid, size_t size, cpu_set_t *set, int flag) -{ - unsigned int cpuset; - if (size != sizeof(cpu_set_t)) { - return -EINVAL; - } - int ret = __syscall(SYS_sched_getaffinity, tid, &cpuset, flag); - if (ret < 0) { - return ret; - } - set->__bits[0] = (long)cpuset; - return 0; -} - -int sched_getaffinity(pid_t tid, size_t size, cpu_set_t *set) -{ - return __syscall_ret(do_getaffinity(tid, size, set, MUSL_TYPE_PROCESS)); -} - -int pthread_getaffinity_np(pthread_t td, size_t size, cpu_set_t *set) -{ - return -do_getaffinity(td->tid, size, set, MUSL_TYPE_THREAD); -} diff --git a/porting/liteos_a/user/src/sched/sched_getparam.c b/porting/liteos_a/user/src/sched/sched_getparam.c deleted file mode 100644 index 66b670d0d..000000000 --- a/porting/liteos_a/user/src/sched/sched_getparam.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#include "syscall.h" -#include "pthread_impl.h" - -int sched_getparam(pid_t pid, struct sched_param *param) -{ - int r; - if (!param) { - r = -EINVAL; - goto exit; - } - - memset(param, 0, sizeof(struct sched_param)); - r = __syscall(SYS_sched_getparam, pid, param, MUSL_TYPE_PROCESS); -exit: - return __syscall_ret(r); -} diff --git a/porting/liteos_a/user/src/sched/sched_getscheduler.c b/porting/liteos_a/user/src/sched/sched_getscheduler.c deleted file mode 100644 index 0f988f596..000000000 --- a/porting/liteos_a/user/src/sched/sched_getscheduler.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include "syscall.h" -#include "pthread_impl.h" - -int sched_getscheduler(pid_t pid) -{ - int r = __syscall(SYS_sched_getscheduler, pid, MUSL_TYPE_PROCESS); - return __syscall_ret(r); -} diff --git a/porting/liteos_a/user/src/sched/sched_setparam.c b/porting/liteos_a/user/src/sched/sched_setparam.c deleted file mode 100644 index d16020219..000000000 --- a/porting/liteos_a/user/src/sched/sched_setparam.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include "syscall.h" -#include "pthread_impl.h" - -int sched_setparam(pid_t pid, const struct sched_param *param) -{ - int r; - if (!param) { - r = -EINVAL; - goto exit; - } - - r = __syscall(SYS_sched_setparam, pid, param, MUSL_TYPE_PROCESS); -exit: - return __syscall_ret(r); -} diff --git a/porting/liteos_a/user/src/sched/sched_setscheduler.c b/porting/liteos_a/user/src/sched/sched_setscheduler.c deleted file mode 100644 index 558484610..000000000 --- a/porting/liteos_a/user/src/sched/sched_setscheduler.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include "syscall.h" -#include "pthread_impl.h" - -int sched_setscheduler(pid_t pid, int sched, const struct sched_param *param) -{ - int r; - if (!param) { - r = -EINVAL; - goto exit; - } - - r = __syscall(SYS_sched_setscheduler, pid, sched, param, MUSL_TYPE_PROCESS); -exit: - return __syscall_ret(r); -} diff --git a/porting/liteos_a/user/src/sched/sched_yield.c b/porting/liteos_a/user/src/sched/sched_yield.c deleted file mode 100644 index 3b5101dbe..000000000 --- a/porting/liteos_a/user/src/sched/sched_yield.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "syscall.h" - -int sched_yield() -{ - return syscall(SYS_sched_yield, 0); -} diff --git a/porting/liteos_a/user/src/select/select.c b/porting/liteos_a/user/src/select/select.c deleted file mode 100644 index b4b1ba988..000000000 --- a/porting/liteos_a/user/src/select/select.c +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -#include -#include "syscall.h" - -#define IS32BIT(x) !((x)+0x80000000ULL>>32) -#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) - -int select(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, struct timeval *restrict tv) -{ - time_t s = tv ? tv->tv_sec : 0; - suseconds_t us = tv ? tv->tv_usec : 0; - long ns; - const time_t max_time = (1ULL<<8*sizeof(time_t)-1)-1; - - if (s<0 || us<0) return __syscall_ret(-EINVAL); - if (us/1000000 > max_time - s) { - s = max_time; - us = 999999; - ns = 999999999; - } else { - s += us/1000000; - us %= 1000000; - ns = us*1000; - } - -#ifdef SYS_pselect6_time64 - int r = -ENOSYS; - if (SYS_pselect6 == SYS_pselect6_time64 || !IS32BIT(s)) - r = __syscall_cp(SYS_pselect6_time64, n, rfds, wfds, efds, - tv ? ((long long[]){s, ns}) : 0, - ((syscall_arg_t[]){ 0, _NSIG/8 })); - if (SYS_pselect6 == SYS_pselect6_time64 || r!=-ENOSYS) - return __syscall_ret(r); -#endif -#ifdef SYS_select - return syscall_cp(SYS_select, n, rfds, wfds, efds, - tv ? ((long long[]){s, us}) : 0); -#else - return syscall_cp(SYS_pselect6, n, rfds, wfds, efds, - tv ? ((long long[]){s, ns}) : 0, ((syscall_arg_t[]){ 0, _NSIG/8 })); -#endif -} diff --git a/porting/liteos_a/user/src/signal/getitimer.c b/porting/liteos_a/user/src/signal/getitimer.c deleted file mode 100644 index 8a8046a76..000000000 --- a/porting/liteos_a/user/src/signal/getitimer.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "syscall.h" - -int getitimer(int which, struct itimerval *old) -{ - return syscall(SYS_getitimer, which, old); -} diff --git a/porting/liteos_a/user/src/signal/psiginfo.c b/porting/liteos_a/user/src/signal/psiginfo.c deleted file mode 100644 index bdf5cae39..000000000 --- a/porting/liteos_a/user/src/signal/psiginfo.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -void psiginfo(const siginfo_t *si, const char *msg) -{ - unsupported_api(__FUNCTION__); - psignal(si->si_signo, msg); -} diff --git a/porting/liteos_a/user/src/signal/setitimer.c b/porting/liteos_a/user/src/signal/setitimer.c deleted file mode 100644 index 9ddac66eb..000000000 --- a/porting/liteos_a/user/src/signal/setitimer.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include "syscall.h" - -#define IS32BIT(x) !((x)+0x80000000ULL>>32) - -int setitimer(int which, const struct itimerval *restrict new, struct itimerval *restrict old) -{ - return syscall(SYS_setitimer, which, new, old); -} diff --git a/porting/liteos_a/user/src/signal/sigaction.c b/porting/liteos_a/user/src/signal/sigaction.c deleted file mode 100644 index b93073957..000000000 --- a/porting/liteos_a/user/src/signal/sigaction.c +++ /dev/null @@ -1,355 +0,0 @@ -#include -#include -#include -#include "stdbool.h" -#include "syscall.h" -#include "pthread_impl.h" -#include "libc.h" -#include "lock.h" -#include "ksigaction.h" - -#define DEFAULT_SIG_NUM 64 -#define SIGNO2SET(s) ((sigset_t)1 << (s)) -#define NULL_SIGNAL_SET ((sigset_t)0x00000000) -#define SET_BIT(bitmap, pos) (bitmap |= (1u << pos)) -#define CLEAR_BIT(bitmap, pos) (bitmap &= ~(1u << pos)) -#define CHECK_BIT(bitmap, pos) ((bitmap & (1u << pos)) ? 1 : 0) -#define SIG_FLAG_NOIGNORE 1 - -struct sigactq { - struct sigaction act; - bool ign_flag; - unsigned char signo; - unsigned char sigmask; - unsigned char reserve[2]; -}; -typedef struct sigactq sigactq_t; - -typedef void (*sa_sighandler_t)(int); -typedef struct sigaction sigaction_t; - -static sigactq_t g_sig_arr[DEFAULT_SIG_NUM]; -static pthread_spinlock_t sig_lite_lock; - -struct sig_default_act { - unsigned char singNo; - unsigned char flag; - sa_sighandler_t action; -}; - -static void __sig_core(int signo); -static void __sig_kill(int signo); -static void __sig_cont(int signo); -static void __sig_stop(int signo); -static void __sig_ignore(int signo); -static const struct sig_default_act sig_default_action[] = { - {SIGHUP, 0, __sig_kill}, - {SIGINT, 0, __sig_kill}, - {SIGQUIT, 0, __sig_core}, - {SIGILL, 0, __sig_core}, - {SIGTRAP, 0, __sig_core}, - {SIGABRT, 0, __sig_core}, - {SIGBUS, 0, __sig_core}, - {SIGFPE, 0, __sig_core}, - {SIGKILL, SIG_FLAG_NOIGNORE, __sig_kill}, - {SIGUSR1, 0, __sig_kill}, - {SIGSEGV, 0, __sig_core}, - {SIGUSR2, 0, __sig_kill}, - {SIGPIPE, 0, __sig_kill}, - {SIGALRM, 0, __sig_kill}, - {SIGTERM, 0, __sig_kill}, - {SIGSTKFLT, 0, __sig_kill}, - {SIGCHLD, 0, __sig_ignore}, - {SIGCONT, SIG_FLAG_NOIGNORE, __sig_cont}, - {SIGSTOP, SIG_FLAG_NOIGNORE, __sig_stop}, - {SIGTSTP, 0, __sig_stop}, - {SIGTTIN, 0, __sig_stop}, - {SIGTTOU, 0, __sig_stop}, - {SIGURG, 0, __sig_ignore}, - {SIGXCPU, 0, __sig_core}, - {SIGXFSZ, 0, __sig_core}, - {SIGVTALRM, 0, __sig_kill}, - {SIGPROF, 0, __sig_kill}, - {SIGWINCH, 0, __sig_ignore}, - {SIGIO, 0, __sig_kill}, - {SIGPWR, 0, __sig_kill}, - {SIGSYS, 0, __sig_ignore}, - {32, 0, __sig_ignore}, - {33, 0, __sig_ignore}, - {34, 0, __sig_ignore}, - {35, 0, __sig_ignore}, - {36, 0, __sig_ignore}, - {37, 0, __sig_ignore}, - {38, 0, __sig_ignore}, - {39, 0, __sig_ignore}, - {40, 0, __sig_ignore}, - {41, 0, __sig_ignore}, - {42, 0, __sig_ignore}, - {43, 0, __sig_ignore}, - {44, 0, __sig_ignore}, - {45, 0, __sig_ignore}, - {46, 0, __sig_ignore}, - {47, 0, __sig_ignore}, - {48, 0, __sig_ignore}, - {49, 0, __sig_ignore}, - {50, 0, __sig_ignore}, - {51, 0, __sig_ignore}, - {52, 0, __sig_ignore}, - {53, 0, __sig_ignore}, - {54, 0, __sig_ignore}, - {55, 0, __sig_ignore}, - {56, 0, __sig_ignore}, - {57, 0, __sig_ignore}, - {58, 0, __sig_ignore}, - {59, 0, __sig_ignore}, - {60, 0, __sig_ignore}, - {61, 0, __sig_ignore}, - {62, 0, __sig_ignore}, - {63, 0, __sig_ignore}, - {64, 0, __sig_ignore}, -}; - -static void __sig_core(int signo) -{ - exit(-1); -} - -static void __sig_kill(int signo) -{ - exit(-1); -} - -static void __sig_cont(int signo) -{ - return; -} - -static void __sig_stop(int signo) -{ - return; -} - -static void __sig_ignore(int signo) -{ - return; -} - -static sigactq_t *__sig_find_action(int sig) -{ - int i; - - for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { - if (g_sig_arr[i].signo == sig) { - return (g_sig_arr + i); - } - } -} - -static void __sig_copy_sigaction(sigaction_t *src, sigaction_t *dst) -{ - dst->sa_handler = src->sa_handler; - dst->sa_mask = src->sa_mask; - dst->sa_flags = src->sa_flags; -} - -static int __sig_cannot_catche(int sig, sa_sighandler_t handler) -{ - int i; - - for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { - if (sig == sig_default_action[i].singNo) { - return (sig_default_action[i].flag == SIG_FLAG_NOIGNORE) && (handler != SIG_DFL); - } - } - /* This sig can be catch and ignore return false */ - return 0; -} - -static void __sig_operation(unsigned int receivedSigno) -{ - int i; - sigset_t mask, oldmask; - - sigemptyset(&mask); - sigemptyset(&oldmask); - - for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { - if (!g_sig_arr[i].ign_flag && g_sig_arr[i].signo == receivedSigno && g_sig_arr[i].act.sa_handler) { - sigaddset(&mask, receivedSigno); - sigprocmask(SIG_BLOCK, &mask, &oldmask); - sigprocmask(SIG_BLOCK, &g_sig_arr[i].act.sa_mask, NULL); - (*g_sig_arr[i].act.sa_handler)(g_sig_arr[i].signo); - sigprocmask(SIG_SETMASK, &oldmask, NULL); - return; - } - } -} - -void arm_signal_process(unsigned int receivedSig) -{ - __sig_operation(receivedSig); -} - -static void __sig_add_def_action() -{ - int i; - - for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { - g_sig_arr[i].signo = (unsigned char)sig_default_action[i].singNo; - g_sig_arr[i].act.sa_handler = sig_default_action[i].action; - sigemptyset(&g_sig_arr[i].act.sa_mask); - g_sig_arr[i].act.sa_flags = sig_default_action[i].flag; - g_sig_arr[i].ign_flag = false; - } -} - -static sa_sighandler_t __sig_find_def_action(unsigned char signo) -{ - int i; - - for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { - if (signo == sig_default_action[i].singNo) { - return sig_default_action[i].action; - } - } - return NULL; -} - -static int __sig_dfl_opr(int sig, sigactq_t *sigact, const sigaction_t *act) -{ - sa_sighandler_t def_handler = NULL; - - def_handler = __sig_find_def_action(sig); - - if (def_handler != NULL) { - /* Replace it from signal action queue */ - sigact->act.sa_handler = def_handler; - sigact->act.sa_mask = act->sa_mask; - sigact->act.sa_flags = act->sa_flags; - } - return 0; -} - -static int __sig_action_opr(int sig, const sigaction_t *act, sigaction_t *oact) -{ - int ret = 0; - sa_sighandler_t handler = NULL; - sigactq_t *sigact = NULL; - - if (act == NULL) return -EINVAL; - - if (sig < SIGHUP || sig > (_NSIG - 1)) return -EINVAL; - - handler = act->sa_handler; - /* Skip sig that can not be catched */ - if (__sig_cannot_catche(sig, handler)) return -EINVAL; - - pthread_spin_lock(&sig_lite_lock); - sigact = __sig_find_action(sig); - if (sigact && oact) __sig_copy_sigaction(&sigact->act, oact); - - sigact->ign_flag = false; - - if (handler == SIG_IGN && sigact) { - sigact->ign_flag = true; - } else if (handler == SIG_DFL) { - ret = __sig_dfl_opr(sig, sigact, act); - } else { - sigact->act.sa_handler = handler; - sigact->act.sa_mask = act->sa_mask; - sigact->act.sa_flags = act->sa_flags; - } - - pthread_spin_unlock(&sig_lite_lock); - return ret; -} - -void __sig_init(void) -{ - signal(SIGSYS, arm_do_signal); - pthread_spin_init(&sig_lite_lock, 0); - __sig_add_def_action(); -} - -static int unmask_done; -static unsigned long handler_set[_NSIG/(8*sizeof(long))]; - -void __get_handler_set(sigset_t *set) -{ - memcpy(set, handler_set, sizeof handler_set); -} - -volatile int __eintr_valid_flag; - -int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) -{ - sigaction_t ksa, ksa_old; - int r = 0; - - if (sa) { - if ((uintptr_t)sa->sa_handler > 1UL) { - a_or_l(handler_set+(sig-1)/(8*sizeof(long)), - 1UL<<(sig-1)%(8*sizeof(long))); - - /* If pthread_create has not yet been called, - * implementation-internal signals might not - * yet have been unblocked. They must be - * unblocked before any signal handler is - * installed, so that an application cannot - * receive an illegal sigset_t (with them - * blocked) as part of the ucontext_t passed - * to the signal handler. */ - if (!libc.threaded && !unmask_done) { - __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, - SIGPT_SET, 0, _NSIG/8); - unmask_done = 1; - } - - if (!(sa->sa_flags & SA_RESTART)) { - a_store(&__eintr_valid_flag, 1); - } - } - ksa.sa_handler = sa->sa_handler; - ksa.sa_flags = sa->sa_flags | SA_RESTORER; - ksa.sa_restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore; - memcpy(&ksa.sa_mask, &sa->sa_mask, _NSIG/8); - } - - if (sig == SIGSYS) { - return syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8); - } else { - r = __sig_action_opr(sig, (const sigaction_t*)sa?&ksa:0, (sigaction_t*)old?&ksa_old:0); - } - if (old && !r) { - old->sa_handler = ksa_old.sa_handler; - old->sa_flags = ksa_old.sa_flags; - memcpy(&old->sa_mask, &ksa_old.sa_mask, _NSIG/8); - } - return __syscall_ret(r); -} - -int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) -{ - unsigned long set[_NSIG/(8*sizeof(long))]; - - if (sig-32U < 3 || sig-1U >= _NSIG-1) { - errno = EINVAL; - return -1; - } - - /* Doing anything with the disposition of SIGABRT requires a lock, - * so that it cannot be changed while abort is terminating the - * process and so any change made by abort can't be observed. */ - if (sig == SIGABRT) { - __block_all_sigs(&set); - LOCK(__abort_lock); - } - int r = __libc_sigaction(sig, sa, old); - if (sig == SIGABRT) { - UNLOCK(__abort_lock); - __restore_sigs(&set); - } - return r; -} - -weak_alias(__sigaction, sigaction); diff --git a/porting/liteos_a/user/src/signal/sigaltstack.c b/porting/liteos_a/user/src/signal/sigaltstack.c deleted file mode 100644 index 619e38750..000000000 --- a/porting/liteos_a/user/src/signal/sigaltstack.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include - -#include "syscall.h" - -int sigaltstack(const stack_t *restrict ss, stack_t *restrict old) -{ - unsupported_api(__FUNCTION__); - if (ss) { - if (!(ss->ss_flags & SS_DISABLE) && ss->ss_size < MINSIGSTKSZ) { - errno = ENOMEM; - return -1; - } - if (ss->ss_flags & SS_ONSTACK) { - errno = EINVAL; - return -1; - } - } - return syscall(SYS_sigaltstack, ss, old); -} diff --git a/porting/liteos_a/user/src/signal/siginterrupt.c b/porting/liteos_a/user/src/signal/siginterrupt.c deleted file mode 100644 index 487318b4a..000000000 --- a/porting/liteos_a/user/src/signal/siginterrupt.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include - -int siginterrupt(int sig, int flag) -{ - struct sigaction sa; - unsupported_api(__FUNCTION__); - sigaction(sig, 0, &sa); - if (flag) sa.sa_flags &= ~SA_RESTART; - else sa.sa_flags |= SA_RESTART; - - return sigaction(sig, &sa, 0); -} diff --git a/porting/liteos_a/user/src/signal/siglongjmp.c b/porting/liteos_a/user/src/signal/siglongjmp.c deleted file mode 100644 index 803f2dd86..000000000 --- a/porting/liteos_a/user/src/signal/siglongjmp.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include - -#include "syscall.h" -#include "pthread_impl.h" - -_Noreturn void siglongjmp(sigjmp_buf buf, int ret) -{ - unsupported_api(__FUNCTION__); - longjmp(buf, ret); -} diff --git a/porting/liteos_a/user/src/signal/sigqueue.c b/porting/liteos_a/user/src/signal/sigqueue.c deleted file mode 100644 index e169defa0..000000000 --- a/porting/liteos_a/user/src/signal/sigqueue.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -#include - -#include "syscall.h" -#include "pthread_impl.h" - -int sigqueue(pid_t pid, int sig, const union sigval value) -{ - siginfo_t si; - sigset_t set; - int r; - unsupported_api(__FUNCTION__); - memset(&si, 0, sizeof si); - si.si_signo = sig; - si.si_code = SI_QUEUE; - si.si_value = value; - si.si_uid = getuid(); - __block_app_sigs(&set); - si.si_pid = getpid(); - r = syscall(SYS_rt_sigqueueinfo, pid, sig, &si); - __restore_sigs(&set); - return r; -} diff --git a/porting/liteos_a/user/src/signal/sigsuspend.c b/porting/liteos_a/user/src/signal/sigsuspend.c deleted file mode 100644 index ab1f0bda4..000000000 --- a/porting/liteos_a/user/src/signal/sigsuspend.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include "syscall.h" - -int sigsuspend(const sigset_t *mask) -{ - int ret,retval; - sigset_t oldset; - - retval = sigprocmask(SIG_BLOCK, 0, &oldset); - if (retval != 0){ - return retval; - } - - ret = syscall_cp(SYS_rt_sigsuspend, mask, _NSIG/8); - - if (ret == -1){ - retval = sigprocmask(SIG_SETMASK, &oldset, 0); - if (retval != 0){ - return retval; - } - } - - return ret; -} \ No newline at end of file diff --git a/porting/liteos_a/user/src/stat/mkfifo.c b/porting/liteos_a/user/src/stat/mkfifo.c deleted file mode 100644 index 9081d7ef4..000000000 --- a/porting/liteos_a/user/src/stat/mkfifo.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "syscall.h" - -int mkfifo(const char *path, mode_t mode) -{ - return syscall(SYS_mkfifo, path, mode); -} diff --git a/porting/liteos_a/user/src/stat/mkfifoat.c b/porting/liteos_a/user/src/stat/mkfifoat.c deleted file mode 100644 index 768a31786..000000000 --- a/porting/liteos_a/user/src/stat/mkfifoat.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -int mkfifoat(int fd, const char *path, mode_t mode) -{ - unsupported_api(__FUNCTION__); - return mknodat(fd, path, mode | S_IFIFO, 0); -} diff --git a/porting/liteos_a/user/src/stat/mknod.c b/porting/liteos_a/user/src/stat/mknod.c deleted file mode 100644 index 5c40a9ee9..000000000 --- a/porting/liteos_a/user/src/stat/mknod.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include "syscall.h" -#include - -int mknod(const char *path, mode_t mode, dev_t dev) -{ - unsupported_api(__FUNCTION__); -#ifdef SYS_mknod - return syscall(SYS_mknod, path, mode, dev); -#else - return syscall(SYS_mknodat, AT_FDCWD, path, mode, dev); -#endif -} diff --git a/porting/liteos_a/user/src/stat/mknodat.c b/porting/liteos_a/user/src/stat/mknodat.c deleted file mode 100644 index 15510c456..000000000 --- a/porting/liteos_a/user/src/stat/mknodat.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include "syscall.h" -#include - -int mknodat(int fd, const char *path, mode_t mode, dev_t dev) -{ - unsupported_api(__FUNCTION__); - return syscall(SYS_mknodat, fd, path, mode, dev); -} diff --git a/porting/liteos_a/user/src/stdio/__stdio_exit.c b/porting/liteos_a/user/src/stdio/__stdio_exit.c deleted file mode 100644 index 0cb5755b9..000000000 --- a/porting/liteos_a/user/src/stdio/__stdio_exit.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "stdio_impl.h" - -static FILE *volatile dummy_file = 0; -weak_alias(dummy_file, __stdin_used); -weak_alias(dummy_file, __stdout_used); -weak_alias(dummy_file, __stderr_used); - -static void close_file(FILE *f) -{ - if (!f) return; - if (f->wpos != f->wbase) f->write(f, 0, 0); - if (f->rpos != f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR); -} - -void __stdio_exit(void) -{ - FILE *f; - for (f=*__ofl_lock(); f; f=f->next) close_file(f); - close_file(__stdin_used); - close_file(__stdout_used); - close_file(__stderr_used); -} - -weak_alias(__stdio_exit, __stdio_exit_needed); diff --git a/porting/liteos_a/user/src/stdio/open_wmemstream.c b/porting/liteos_a/user/src/stdio/open_wmemstream.c deleted file mode 100644 index 760da562b..000000000 --- a/porting/liteos_a/user/src/stdio/open_wmemstream.c +++ /dev/null @@ -1,104 +0,0 @@ -#include "stdio_impl.h" -#include -#include -#include -#include -#include -#include -#include "libc.h" - -struct cookie { - wchar_t **bufp; - size_t *sizep; - size_t pos; - wchar_t *buf; - size_t len; - size_t space; - mbstate_t mbs; -}; - -struct wms_FILE { - FILE f; - struct cookie c; - unsigned char buf[1]; -}; - -static off_t wms_seek(FILE *f, off_t off, int whence) -{ - ssize_t base; - struct cookie *c = f->cookie; - if (whence>2U) { -fail: - errno = EINVAL; - return -1; - } - base = (size_t [3]){0, c->pos, c->len}[whence]; - if (off < -base || off > SSIZE_MAX/4-base) goto fail; - memset(&c->mbs, 0, sizeof c->mbs); - return c->pos = base+off; -} - -static size_t wms_write(FILE *f, const unsigned char *buf, size_t len) -{ - struct cookie *c = f->cookie; - size_t len2; - wchar_t *newbuf; - if (len + c->pos >= c->space) { - len2 = 2*c->space+1 | c->pos+len+1; - if (len2 > SSIZE_MAX/4) return 0; - newbuf = realloc(c->buf, len2*4); - if (!newbuf) return 0; - *c->bufp = c->buf = newbuf; - memset(c->buf + c->space, 0, 4*(len2 - c->space)); - c->space = len2; - } - - len2 = mbsnrtowcs(c->buf+c->pos, (void *)&buf, len, c->space-c->pos, &c->mbs); - if (len2 == -1) return 0; - c->pos += len2; - if (c->pos >= c->len) c->len = c->pos; - *c->sizep = c->pos; - return len; -} - -static int wms_close(FILE *f) -{ - return 0; -} - -FILE *open_wmemstream(wchar_t **bufp, size_t *sizep) -{ - struct wms_FILE *f; - wchar_t *buf; - - unsupported_api(__FUNCTION__); - if (!(f=malloc(sizeof *f))) return 0; - if (!(buf=malloc(sizeof *buf))) { - free(f); - return 0; - } - memset(&f->f, 0, sizeof f->f); - memset(&f->c, 0, sizeof f->c); - f->f.cookie = &f->c; - - f->c.bufp = bufp; - f->c.sizep = sizep; - f->c.pos = f->c.len = f->c.space = *sizep = 0; - f->c.buf = *bufp = buf; - *buf = 0; - - f->f.flags = F_NORD; - f->f.fd = -1; - f->f.buf = f->buf; - f->f.buf_size = 0; - f->f.lbf = EOF; - f->f.write = wms_write; - f->f.seek = wms_seek; - f->f.close = wms_close; - - if (!libc.threaded) f->f.lock = -1; - - fwide(&f->f, 1); - - return __ofl_add(&f->f); -} diff --git a/porting/liteos_a/user/src/stdio/popen.c b/porting/liteos_a/user/src/stdio/popen.c deleted file mode 100644 index d7d832849..000000000 --- a/porting/liteos_a/user/src/stdio/popen.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "stdio_impl.h" -#include "syscall.h" - -extern char **__environ; - -FILE *popen(const char *cmd, const char *mode) -{ - int p[2], op, e; - pid_t pid; - FILE *f; - posix_spawn_file_actions_t fa; - - unsupported_api(__FUNCTION__); - if (*mode == 'r') { - op = 0; - } else if (*mode == 'w') { - op = 1; - } else { - errno = EINVAL; - return 0; - } - - if (pipe2(p, O_CLOEXEC)) return NULL; - f = fdopen(p[op], mode); - if (!f) { - __syscall(SYS_close, p[0]); - __syscall(SYS_close, p[1]); - return NULL; - } - - e = ENOMEM; - if (!posix_spawn_file_actions_init(&fa)) { - for (FILE *l = *__ofl_lock(); l; l=l->next) - if (l->pipe_pid && posix_spawn_file_actions_addclose(&fa, l->fd)) - goto fail; - if (!posix_spawn_file_actions_adddup2(&fa, p[1-op], 1-op)) { - if (!(e = posix_spawn(&pid, "/bin/sh", &fa, 0, - (char *[]){ "sh", "-c", (char *)cmd, 0 }, __environ))) { - posix_spawn_file_actions_destroy(&fa); - f->pipe_pid = pid; - if (!strchr(mode, 'e')) - fcntl(p[op], F_SETFD, 0); - __syscall(SYS_close, p[1-op]); - __ofl_unlock(); - return f; - } - } -fail: - __ofl_unlock(); - posix_spawn_file_actions_destroy(&fa); - } - fclose(f); - __syscall(SYS_close, p[1-op]); - - errno = e; - return 0; -} diff --git a/porting/liteos_a/user/src/stdio/tmpfile.c b/porting/liteos_a/user/src/stdio/tmpfile.c deleted file mode 100644 index ad02d4bed..000000000 --- a/porting/liteos_a/user/src/stdio/tmpfile.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -#include "stdio_impl.h" - -#define MAXTRIES 100 - -FILE *tmpfile(void) -{ - char s[] = "/tmp/tmpfile_XXXXXX"; - int fd; - FILE *f; - int try; - unsupported_api(__FUNCTION__); - for (try=0; try= 0) { -#ifdef SYS_unlink - __syscall(SYS_unlink, s); -#else - __syscall(SYS_unlinkat, AT_FDCWD, s, 0); -#endif - f = __fdopen(fd, "w+"); - if (!f) __syscall(SYS_close, fd); - return f; - } - } - return 0; -} - -weak_alias(tmpfile, tmpfile64); diff --git a/porting/liteos_a/user/src/stdio/tmpnam.c b/porting/liteos_a/user/src/stdio/tmpnam.c deleted file mode 100644 index c3d536487..000000000 --- a/porting/liteos_a/user/src/stdio/tmpnam.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "syscall.h" -#include "kstat.h" - -#define MAXTRIES 100 - -char *tmpnam(char *buf) -{ - static char internal[L_tmpnam]; - char s[] = "/tmp/tmpnam_XXXXXX"; - int try; - int r; - unsupported_api(__FUNCTION__); - for (try=0; try -#include -#include -#include - -int cfsetospeed(struct termios *tio, speed_t speed) -{ - unsupported_api(__FUNCTION__); - if (speed & ~CBAUD) { - errno = EINVAL; - return -1; - } - tio->c_cflag &= ~CBAUD; - tio->c_cflag |= speed; - return 0; -} - -int cfsetispeed(struct termios *tio, speed_t speed) -{ - unsupported_api(__FUNCTION__); - return speed ? cfsetospeed(tio, speed) : 0; -} - -weak_alias(cfsetospeed, cfsetspeed); diff --git a/porting/liteos_a/user/src/thread/__timedwait.c b/porting/liteos_a/user/src/thread/__timedwait.c deleted file mode 100644 index b4ca0fea1..000000000 --- a/porting/liteos_a/user/src/thread/__timedwait.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include "futex.h" -#include "syscall.h" -#include "pthread_impl.h" - -#define IS32BIT(x) !((x)+0x80000000ULL>>32) -#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) - -static int __futex4_cp(volatile void *addr, int op, int val, const struct timespec *to) -{ - int r; - unsigned int useconds = 0xffffffffU; -#if 0 -#ifdef SYS_futex_time64 - time_t s = to ? to->tv_sec : 0; - long ns = to ? to->tv_nsec : 0; - r = -ENOSYS; - if (SYS_futex == SYS_futex_time64 || !IS32BIT(s)) - r = __syscall_cp(SYS_futex_time64, addr, op, val, - to ? ((long long[]){s, ns}) : 0); - if (SYS_futex == SYS_futex_time64 || r!=-ENOSYS) return r; - to = to ? (void *)(long[]){CLAMP(s), ns} : 0; -#endif -#endif - - if (to) { - useconds = (to->tv_sec * 1000000 + to->tv_nsec / 1000); - if ((useconds == 0) && (to->tv_nsec != 0)) { - useconds = 1; - } - } - - r = __syscall_cp(SYS_futex, addr, op, val, useconds); - if (r != -ENOSYS) return r; - return __syscall_cp(SYS_futex, addr, op & ~FUTEX_PRIVATE, val, useconds); -} - -static volatile int dummy = 0; -weak_alias(dummy, __eintr_valid_flag); - -int __timedwait_cp(volatile int *addr, int val, - clockid_t clk, const struct timespec *at, int priv) -{ - int r; - struct timespec to, *top=0; - - if (priv) priv = FUTEX_PRIVATE; - - if (at) { - if (at->tv_nsec >= 1000000000UL) return EINVAL; - if (__clock_gettime(clk, &to)) return EINVAL; - to.tv_sec = at->tv_sec - to.tv_sec; - if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) { - to.tv_sec--; - to.tv_nsec += 1000000000; - } - if (to.tv_sec < 0) return ETIMEDOUT; - top = &to; - } - - r = -__futex4_cp(addr, FUTEX_WAIT|priv, val, top); - if (r != EINTR && r != ETIMEDOUT && r != ECANCELED) r = 0; - /* Mitigate bug in old kernels wrongly reporting EINTR for non- - * interrupting (SA_RESTART) signal handlers. This is only practical - * when NO interrupting signal handlers have been installed, and - * works by sigaction tracking whether that's the case. */ - if (r == EINTR && !__eintr_valid_flag) r = 0; - - return r; -} - -int __timedwait(volatile int *addr, int val, - clockid_t clk, const struct timespec *at, int priv) -{ - int cs, r; - __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - r = __timedwait_cp(addr, val, clk, at, priv); - __pthread_setcancelstate(cs, 0); - return r; -} diff --git a/porting/liteos_a/user/src/thread/__wait.c b/porting/liteos_a/user/src/thread/__wait.c deleted file mode 100644 index 587395611..000000000 --- a/porting/liteos_a/user/src/thread/__wait.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "pthread_impl.h" - -void __wait(volatile int *addr, volatile int *waiters, int val, int priv) -{ - int spins=100; - if (priv) priv = FUTEX_PRIVATE; - while (spins-- && (!waiters || !*waiters)) { - if (*addr==val) a_spin(); - else return; - } - if (waiters) a_inc(waiters); - while (*addr==val) { - __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0xffffffffu) != -ENOSYS - || __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0xffffffffu); - } - if (waiters) a_dec(waiters); -} diff --git a/porting/liteos_a/user/src/thread/arm/__set_thread_area.c b/porting/liteos_a/user/src/thread/arm/__set_thread_area.c deleted file mode 100644 index 4d73c5d3c..000000000 --- a/porting/liteos_a/user/src/thread/arm/__set_thread_area.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include "pthread_impl.h" -#include "libc.h" - -#define HWCAP_TLS (1 << 15) - -extern hidden const unsigned char - __a_barrier_oldkuser[], __a_barrier_v6[], __a_barrier_v7[], - __a_cas_v6[], __a_cas_v7[], - __a_gettp_cp15[]; - -#define __a_barrier_kuser 0xffff0fa0 -#define __a_barrier_oldkuser (uintptr_t)__a_barrier_oldkuser -#define __a_barrier_v6 (uintptr_t)__a_barrier_v6 -#define __a_barrier_v7 (uintptr_t)__a_barrier_v7 - -#define __a_cas_kuser 0xffff0fc0 -#define __a_cas_v6 (uintptr_t)__a_cas_v6 -#define __a_cas_v7 (uintptr_t)__a_cas_v7 - -#define __a_gettp_kuser 0xffff0fe0 -#define __a_gettp_cp15 (uintptr_t)__a_gettp_cp15 - -extern hidden uintptr_t __a_barrier_ptr, __a_cas_ptr, __a_gettp_ptr; - -int __set_thread_area(void *p) -{ -#ifdef SYS_set_thread_area - return __syscall(SYS_set_thread_area, p); -#else -#if !__ARM_ARCH_7A__ && !__ARM_ARCH_7R__ && __ARM_ARCH < 7 - if (__hwcap & HWCAP_TLS) { - size_t *aux; - __a_cas_ptr = __a_cas_v7; - __a_barrier_ptr = __a_barrier_v7; - for (aux=libc.auxv; *aux; aux+=2) { - if (*aux != AT_PLATFORM) continue; - const char *s = (void *)aux[1]; - if (s[0]!='v' || s[1]!='6' || s[2]-'0'<10u) break; - __a_cas_ptr = __a_cas_v6; - __a_barrier_ptr = __a_barrier_v6; - break; - } - } else { - int ver = *(int *)0xffff0ffc; - __a_gettp_ptr = __a_gettp_kuser; - __a_cas_ptr = __a_cas_kuser; - __a_barrier_ptr = __a_barrier_kuser; - if (ver < 2) a_crash(); - if (ver < 3) __a_barrier_ptr = __a_barrier_oldkuser; - } -#endif - return __syscall(0xf0005, p); -#endif -} diff --git a/porting/liteos_a/user/src/thread/pthread_attr_get.c b/porting/liteos_a/user/src/thread/pthread_attr_get.c deleted file mode 100644 index 00ec08fda..000000000 --- a/porting/liteos_a/user/src/thread/pthread_attr_get.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include "pthread_impl.h" - -int pthread_attr_getdetachstate(const pthread_attr_t *a, int *state) -{ - *state = a->_a_detach; - return 0; -} -int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size) -{ - *size = a->_a_guardsize; - return 0; -} - -int pthread_attr_getinheritsched(const pthread_attr_t *restrict a, int *restrict inherit) -{ - *inherit = a->_a_sched; - return 0; -} - -int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param) -{ - param->sched_priority = a->_a_prio; - return 0; -} - -int pthread_attr_getschedpolicy(const pthread_attr_t *restrict a, int *restrict policy) -{ - *policy = a->_a_policy; - return 0; -} - -int pthread_attr_getscope(const pthread_attr_t *restrict a, int *restrict scope) -{ - *scope = PTHREAD_SCOPE_PROCESS; - return 0; -} - -int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size) -{ - if (!a->_a_stackaddr) - return EINVAL; - *size = a->_a_stacksize; - *addr = (void *)(a->_a_stackaddr - *size); - return 0; -} - -int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size) -{ - *size = a->_a_stacksize; - return 0; -} - -int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict a, int *restrict pshared) -{ - *pshared = !!a->__attr; - return 0; -} - -int pthread_condattr_getclock(const pthread_condattr_t *restrict a, clockid_t *restrict clk) -{ - *clk = a->__attr & 0x7fffffff; - return 0; -} - -int pthread_condattr_getpshared(const pthread_condattr_t *restrict a, int *restrict pshared) -{ - *pshared = a->__attr>>31; - return 0; -} - -int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict a, int *restrict protocol) -{ - unsupported_api(__FUNCTION__); - *protocol = a->__attr / 8U % 2; - return 0; -} -int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict a, int *restrict pshared) -{ - *pshared = a->__attr / 128U % 2; - return 0; -} - -int pthread_mutexattr_getrobust(const pthread_mutexattr_t *restrict a, int *restrict robust) -{ - *robust = a->__attr / 4U % 2; - return 0; -} - -int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict a, int *restrict type) -{ - *type = a->__attr & 3; - return 0; -} - -int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict a, int *restrict pshared) -{ - *pshared = a->__attr[0]; - return 0; -} diff --git a/porting/liteos_a/user/src/thread/pthread_attr_init.c b/porting/liteos_a/user/src/thread/pthread_attr_init.c deleted file mode 100644 index aa43c71d6..000000000 --- a/porting/liteos_a/user/src/thread/pthread_attr_init.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "pthread_impl.h" - -int pthread_attr_init(pthread_attr_t *a) -{ - *a = (pthread_attr_t){0}; - __acquire_ptc(); - a->_a_stacksize = __default_stacksize; - a->_a_guardsize = __default_guardsize; - a->_a_policy = SCHED_RR; - a->_a_prio = 25; - __release_ptc(); - return 0; -} diff --git a/porting/liteos_a/user/src/thread/pthread_attr_setschedparam.c b/porting/liteos_a/user/src/thread/pthread_attr_setschedparam.c deleted file mode 100644 index 914ca3b06..000000000 --- a/porting/liteos_a/user/src/thread/pthread_attr_setschedparam.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "pthread_impl.h" - -int pthread_attr_setschedparam(pthread_attr_t *restrict a, const struct sched_param *restrict param) -{ - if ((a->_a_policy == SCHED_RR || a->_a_policy == SCHED_FIFO) && - (param->sched_priority < 0 || param->sched_priority > PTHREAD_PRIORITY_LOWEST)) { - return EINVAL; - } - a->_a_prio = param->sched_priority; - a->_a_deadline = param->sched_deadline; - a->_a_period = param->sched_period; - return 0; -} diff --git a/porting/liteos_a/user/src/thread/pthread_attr_setschedpolicy.c b/porting/liteos_a/user/src/thread/pthread_attr_setschedpolicy.c deleted file mode 100644 index cf3dd4040..000000000 --- a/porting/liteos_a/user/src/thread/pthread_attr_setschedpolicy.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "pthread_impl.h" - -int pthread_attr_setschedpolicy(pthread_attr_t *a, int policy) -{ - if (policy != SCHED_RR && policy != SCHED_FIFO && policy != SCHED_DEADLINE) { - return EINVAL; - } - a->_a_policy = policy; - return 0; -} diff --git a/porting/liteos_a/user/src/thread/pthread_attr_setscope.c b/porting/liteos_a/user/src/thread/pthread_attr_setscope.c deleted file mode 100644 index f632883bd..000000000 --- a/porting/liteos_a/user/src/thread/pthread_attr_setscope.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "pthread_impl.h" - -int pthread_attr_setscope(pthread_attr_t *a, int scope) -{ - switch (scope) { - case PTHREAD_SCOPE_SYSTEM: - return ENOTSUP; - case PTHREAD_SCOPE_PROCESS: - return 0; - default: - return EINVAL; - } -} diff --git a/porting/liteos_a/user/src/thread/pthread_barrier_wait.c b/porting/liteos_a/user/src/thread/pthread_barrier_wait.c deleted file mode 100644 index 5a56b5e44..000000000 --- a/porting/liteos_a/user/src/thread/pthread_barrier_wait.c +++ /dev/null @@ -1,111 +0,0 @@ -#include "pthread_impl.h" - -static int pshared_barrier_wait(pthread_barrier_t *b) -{ - int limit = (b->_b_limit & INT_MAX) + 1; - int ret = 0; - int v, w; - - if (limit==1) return PTHREAD_BARRIER_SERIAL_THREAD; - - while ((v=a_cas(&b->_b_lock, 0, limit))) - __wait(&b->_b_lock, &b->_b_waiters, v, 0); - - /* Wait for threads to get to the barrier */ - if (++b->_b_count == limit) { - a_store(&b->_b_count, 0); - ret = PTHREAD_BARRIER_SERIAL_THREAD; - if (b->_b_waiters2) __wake(&b->_b_count, -1, 0); - } else { - a_store(&b->_b_lock, 0); - if (b->_b_waiters) __wake(&b->_b_lock, 1, 0); - while ((v=b->_b_count)>0) - __wait(&b->_b_count, &b->_b_waiters2, v, 0); - } - - __vm_lock(); - - /* Ensure all threads have a vm lock before proceeding */ - if (a_fetch_add(&b->_b_count, -1)==1-limit) { - a_store(&b->_b_count, 0); - if (b->_b_waiters2) __wake(&b->_b_count, -1, 0); - } else { - while ((v=b->_b_count)) - __wait(&b->_b_count, &b->_b_waiters2, v, 0); - } - - /* Perform a recursive unlock suitable for self-sync'd destruction */ - do { - v = b->_b_lock; - w = b->_b_waiters; - } while (a_cas(&b->_b_lock, v, v==INT_MIN+1 ? 0 : v-1) != v); - - /* Wake a thread waiting to reuse or destroy the barrier */ - if (v==INT_MIN+1 || (v==1 && w)) - __wake(&b->_b_lock, 1, 0); - - __vm_unlock(); - - return ret; -} - -struct instance -{ - volatile int count; - volatile int last; - volatile int waiters; - volatile int finished; -}; - -int pthread_barrier_wait(pthread_barrier_t *b) -{ - int limit = b->_b_limit; - struct instance *inst; - - /* Trivial case: count was set at 1 */ - if (!limit) return PTHREAD_BARRIER_SERIAL_THREAD; - - /* Process-shared barriers require a separate, inefficient wait */ - if (limit < 0) return pshared_barrier_wait(b); - - /* Otherwise we need a lock on the barrier object */ - while (a_swap(&b->_b_lock, 1)) - __wait(&b->_b_lock, &b->_b_waiters, 1, 1); - inst = b->_b_inst; - - /* First thread to enter the barrier becomes the "instance owner" */ - if (!inst) { - struct instance new_inst = { 0 }; - int spins = 200; - b->_b_inst = inst = &new_inst; - a_store(&b->_b_lock, 0); - if (b->_b_waiters) __wake(&b->_b_lock, 1, 1); - while (spins-- && !inst->finished) - a_spin(); - a_inc(&inst->finished); - while (inst->finished == 1) - __syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0xffffffffu) != -ENOSYS - || __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0xffffffffu); - return PTHREAD_BARRIER_SERIAL_THREAD; - } - - /* Last thread to enter the barrier wakes all non-instance-owners */ - if (++inst->count == limit) { - b->_b_inst = 0; - a_store(&b->_b_lock, 0); - if (b->_b_waiters) __wake(&b->_b_lock, 1, 1); - a_store(&inst->last, 1); - if (inst->waiters) - __wake(&inst->last, -1, 1); - } else { - a_store(&b->_b_lock, 0); - if (b->_b_waiters) __wake(&b->_b_lock, 1, 1); - __wait(&inst->last, &inst->waiters, 0, 1); - } - - /* Last thread to exit the barrier wakes the instance owner */ - if (a_fetch_add(&inst->count,-1)==1 && a_fetch_add(&inst->finished,1)) - __wake(&inst->finished, 1, 1); - - return 0; -} diff --git a/porting/liteos_a/user/src/thread/pthread_cancel.c b/porting/liteos_a/user/src/thread/pthread_cancel.c deleted file mode 100644 index c372f05fb..000000000 --- a/porting/liteos_a/user/src/thread/pthread_cancel.c +++ /dev/null @@ -1,89 +0,0 @@ -#define _GNU_SOURCE -#include -#include "pthread_impl.h" -#include "syscall.h" - -hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c(); - -long __cancel() -{ - pthread_t self = __pthread_self(); - if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync) - pthread_exit(PTHREAD_CANCELED); - self->canceldisable = PTHREAD_CANCEL_DISABLE; - return -ECANCELED; -} - -long __syscall_cp_asm(volatile void *, syscall_arg_t, - syscall_arg_t, syscall_arg_t, syscall_arg_t, - syscall_arg_t, syscall_arg_t, syscall_arg_t); - -long __syscall_cp_c(syscall_arg_t nr, - syscall_arg_t u, syscall_arg_t v, syscall_arg_t w, - syscall_arg_t x, syscall_arg_t y, syscall_arg_t z) -{ - pthread_t self; - long r; - int st; - - if ((st=(self=__pthread_self())->canceldisable) - && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close)) - return __syscall(nr, u, v, w, x, y, z); - - r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z); - if (r==-EINTR && nr!=SYS_close && self->cancel && - self->canceldisable != PTHREAD_CANCEL_DISABLE) - r = __cancel(); - return r; -} - -static void _sigaddset(sigset_t *set, int sig) -{ - unsigned s = sig-1; - set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1); -} - -extern hidden const char __cp_begin[1], __cp_end[1], __cp_cancel[1]; - -static void cancel_handler(int sig, siginfo_t *si, void *ctx) -{ - pthread_t self = __pthread_self(); - a_barrier(); - if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return; - if(self->cancelasync){ - pthread_exit(PTHREAD_CANCELED); - } -} - -void __testcancel() -{ - pthread_t self = __pthread_self(); - if (self->cancel && !self->canceldisable) - __cancel(); -} - -static void init_cancellation() -{ - struct sigaction sa = { - .sa_flags = SA_SIGINFO | SA_RESTART, - .sa_sigaction = cancel_handler - }; - memset(&sa.sa_mask, -1, _NSIG/8); - __libc_sigaction(SIGCANCEL, &sa, 0); -} - -int pthread_cancel(pthread_t t) -{ - static int init; - if (!init) { - init_cancellation(); - init = 1; - } - a_store(&t->cancel, 1); - if (t == pthread_self()) { - if (t->canceldisable == PTHREAD_CANCEL_ENABLE && t->cancelasync) - pthread_exit(PTHREAD_CANCELED); - return 0; - } - return pthread_kill(t, SIGCANCEL); -} diff --git a/porting/liteos_a/user/src/thread/pthread_create.c b/porting/liteos_a/user/src/thread/pthread_create.c deleted file mode 100644 index c2545b1e3..000000000 --- a/porting/liteos_a/user/src/thread/pthread_create.c +++ /dev/null @@ -1,379 +0,0 @@ -#define _GNU_SOURCE -#include "pthread_impl.h" -#include "stdio_impl.h" -#include "libc.h" -#include "lock.h" -#include -#include -#include - -static void dummy_0() -{ -} -weak_alias(dummy_0, __acquire_ptc); -weak_alias(dummy_0, __release_ptc); -weak_alias(dummy_0, __pthread_tsd_run_dtors); -weak_alias(dummy_0, __do_orphaned_stdio_locks); -weak_alias(dummy_0, __dl_thread_cleanup); - -static int tl_lock_count; -static int tl_lock_waiters; - -void __tl_lock(void) -{ - int tid = __pthread_self()->tid; - int val = __thread_list_lock; - if (val == tid) { - tl_lock_count++; - return; - } - while ((val = a_cas(&__thread_list_lock, 0, tid))) - __wait(&__thread_list_lock, &tl_lock_waiters, val, 0); -} - -void __tl_unlock(void) -{ - if (tl_lock_count) { - tl_lock_count--; - return; - } - a_store(&__thread_list_lock, 0); - if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0); -} - -void __tl_sync(pthread_t td) -{ - a_barrier(); - int val = __thread_list_lock; - if (!val) return; - __wait(&__thread_list_lock, &tl_lock_waiters, val, 0); - if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0); -} - -_Noreturn void __pthread_exit(void *result) -{ - pthread_t self = __pthread_self(); - sigset_t set; - - self->canceldisable = 1; - self->cancelasync = 0; - self->result = result; - - while (self->cancelbuf) { - void (*f)(void *) = self->cancelbuf->__f; - void *x = self->cancelbuf->__x; - self->cancelbuf = self->cancelbuf->__next; - f(x); - } - - __pthread_tsd_run_dtors(); - - __block_app_sigs(&set); - - /* Access to target the exiting thread with syscalls that use - * its kernel tid is controlled by killlock. For detached threads, - * any use past this point would have undefined behavior, but for - * joinable threads it's a valid usage that must be handled. - * Signals must be blocked since pthread_kill must be AS-safe. */ - LOCK(self->killlock); - - /* The thread list lock must be AS-safe, and thus depends on - * application signals being blocked above. */ - __tl_lock(); - - /* If this is the only thread in the list, don't proceed with - * termination of the thread, but restore the previous lock and - * signal state to prepare for exit to call atexit handlers. */ - if (self->next == self) { - __tl_unlock(); - UNLOCK(self->killlock); - __restore_sigs(&set); - exit(0); - } - - /* At this point we are committed to thread termination. */ - - /* Process robust list in userspace to handle non-pshared mutexes - * and the detached thread case where the robust list head will - * be invalid when the kernel would process it. */ - __vm_lock(); - volatile void *volatile *rp; - while ((rp=self->robust_list.head) && rp != &self->robust_list.head) { - pthread_mutex_t *m = (void *)((char *)rp - - offsetof(pthread_mutex_t, _m_next)); - int waiters = m->_m_waiters; - int priv = (m->_m_type & 128) ^ 128; - self->robust_list.pending = rp; - self->robust_list.head = *rp; - int cont = a_swap(&m->_m_lock, 0x40000000); - self->robust_list.pending = 0; - if (cont < 0 || waiters) - __wake(&m->_m_lock, 1, priv); - } - __vm_unlock(); - - __do_orphaned_stdio_locks(); - __dl_thread_cleanup(); - - /* Last, unlink thread from the list. This change will not be visible - * until the lock is released, which only happens after SYS_exit - * has been called, via the exit futex address pointing at the lock. - * This needs to happen after any possible calls to LOCK() that might - * skip locking if process appears single-threaded. */ - if (!--libc.threads_minus_1) libc.need_locks = -1; - self->next->prev = self->prev; - self->prev->next = self->next; - self->prev = self->next = self; - - /* This atomic potentially competes with a concurrent pthread_detach - * call; the loser is responsible for freeing thread resources. */ - int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING); - -#if 0 - if (state==DT_DETACHED && self->map_base) { - /* Robust list will no longer be valid, and was already - * processed above, so unregister it with the kernel. */ - if (self->robust_list.off) - __syscall(SYS_set_robust_list, 0, 3*sizeof(long)); - - /* The following call unmaps the thread's stack mapping - * and then exits without touching the stack. */ - __unmapself(self->map_base, self->map_size); - } - - /* Wake any joiner. */ - a_store(&self->detach_state, DT_EXITED); - __wake(&self->detach_state, 1, 1); -#endif - - /* After the kernel thread exits, its tid may be reused. Clear it - * to prevent inadvertent use and inform functions that would use - * it that it's no longer available. */ - if (self->detach_state == DT_DETACHED) { - /* Detached threads must block even implementation-internal - * signals, since they will not have a stack in their last - * moments of existence. */ - __block_all_sigs(&set); - self->tid = 0; - } - - __tl_unlock(); - UNLOCK(self->killlock); - - for (;;) __syscall(SYS_exit, 0); -} - -void __do_cleanup_push(struct __ptcb *cb) -{ - struct pthread *self = __pthread_self(); - cb->__next = self->cancelbuf; - self->cancelbuf = cb; -} - -void __do_cleanup_pop(struct __ptcb *cb) -{ - __pthread_self()->cancelbuf = cb->__next; -} - -struct start_args { - void *(*start_func)(void *); - void *start_arg; - volatile int control; - unsigned long sig_mask[_NSIG/8/sizeof(long)]; -}; - -static int start(void *p) -{ - struct start_args *args = (struct start_args *)p; - __syscall(SYS_rt_sigprocmask, SIG_SETMASK, &args->sig_mask, 0, _NSIG/8); - __pthread_exit(args->start_func(args->start_arg)); - return 0; -} - -static int start_c11(void *p) -{ - struct start_args *args = (struct start_args *)p; - int (*start)(void*) = (int(*)(void*)) args->start_func; - __pthread_exit((void *)(uintptr_t)start(args->start_arg)); - return 0; -} - -#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE) - -/* pthread_key_create.c overrides this */ -static volatile size_t dummy = 0; -weak_alias(dummy, __pthread_tsd_size); -static void *dummy_tsd[1] = { 0 }; -weak_alias(dummy_tsd, __pthread_tsd_main); - -int __pthread_init_and_check_attr(const pthread_attr_t *restrict attrp, pthread_attr_t *attr) -{ - int policy = 0; - struct sched_param param = {0}; - int c11 = (attrp == __ATTRP_C11_THREAD); - int ret; - - if (attrp && !c11) memcpy(attr, attrp, sizeof(pthread_attr_t)); - - if (!attrp || c11) { - pthread_attr_init(attr); - } - - if (!attr->_a_sched) { - ret = pthread_getschedparam(pthread_self(), &policy, ¶m); - if (ret) return ret; - attr->_a_policy = policy; - attr->_a_prio = param.sched_priority; - attr->_a_deadline = param.sched_deadline; - attr->_a_period = param.sched_period; - } - - return 0; -} - -int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg) -{ - int ret, c11 = (attrp == __ATTRP_C11_THREAD); - size_t size, guard; - struct pthread *self, *new; - unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit; - unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND - | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS - | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED; - pthread_attr_t attr = { 0 }; - sigset_t set; - - if (!libc.can_do_threads) return ENOSYS; - if (!entry) return EINVAL; - self = __pthread_self(); - __acquire_ptc(); - - ret = __pthread_init_and_check_attr(attrp, &attr); - if (ret) { - __release_ptc(); - return ret; - } - - if (attr._a_stackaddr) { - size_t need = libc.tls_size + __pthread_tsd_size; - size = attr._a_stacksize; - stack = (void *)(attr._a_stackaddr & -16); - stack_limit = (void *)(attr._a_stackaddr - size); - /* Use application-provided stack for TLS only when - * it does not take more than ~12% or 2k of the - * application's stack space. */ - if (need < size/8 && need < 2048) { - tsd = stack - __pthread_tsd_size; - stack = tsd - libc.tls_size; - memset(stack, 0, need); - } else { - size = ROUND(need); - } - guard = 0; - } else { - guard = ROUND(attr._a_guardsize); - size = guard + ROUND(attr._a_stacksize - + libc.tls_size + __pthread_tsd_size); - } - - if (!tsd) { - if (guard) { - map = __mmap(0, size, PROT_READ|PROT_WRITE|PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (map == MAP_FAILED) goto fail; - if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE) - && errno != ENOSYS) { - __munmap(map, size); - goto fail; - } - } else { - map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (map == MAP_FAILED) goto fail; - } - tsd = map + size - __pthread_tsd_size; - if (!stack) { - stack = tsd - libc.tls_size; - stack_limit = map + guard; - } - } - - new = __copy_tls(tsd - libc.tls_size); - new->map_base = map; - new->map_size = size; - new->stack = stack; - new->stack_size = stack - stack_limit; - new->guard_size = guard; - new->self = new; - new->tsd = (void *)tsd; - new->locale = &libc.global_locale; - if (attr._a_detach) { - new->detach_state = DT_DETACHED; - } else { - new->detach_state = DT_JOINABLE; - } - new->robust_list.head = &new->robust_list.head; - new->canary = self->canary; - new->sysinfo = self->sysinfo; - - /* Setup argument structure for the new thread on its stack. - * It's safe to access from the caller only until the thread - * list is unlocked. */ - stack -= (uintptr_t)stack % sizeof(uintptr_t); - stack -= sizeof(struct start_args); - struct start_args *args = (void *)stack; - args->start_func = entry; - args->start_arg = arg; - args->control = attr._a_sched ? 1 : 0; - - /* Application signals (but not the synccall signal) must be - * blocked before the thread list lock can be taken, to ensure - * that the lock is AS-safe. */ - __block_app_sigs(&set); - - /* Ensure SIGCANCEL is unblocked in new thread. This requires - * working with a copy of the set so we can restore the - * original mask in the calling thread. */ - memcpy(&args->sig_mask, &set, sizeof args->sig_mask); - args->sig_mask[(SIGCANCEL-1)/8/sizeof(long)] &= - ~(1UL<<((SIGCANCEL-1)%(8*sizeof(long)))); - - __tl_lock(); - if (!libc.threads_minus_1++) libc.need_locks = 1; - ret = __thread_clone((c11 ? start_c11 : start), flags, new, stack); - - /* All clone failures translate to EAGAIN. If explicit scheduling - * was requested, attempt it before unlocking the thread list so - * that the failed thread is never exposed and so that we can - * clean up all transient resource usage before returning. */ - if (ret < 0) { - ret = -EAGAIN; - } else { - new->next = self->next; - new->prev = self; - new->next->prev = new; - new->prev->next = new; - - *res = new; - __tl_unlock(); - __restore_sigs(&set); - __release_ptc(); - ret = __syscall(SYS_sched_setscheduler, - new->tid, attr._a_policy, &attr._a_prio, MUSL_TYPE_THREAD); - } - - if (ret < 0) { - if (!--libc.threads_minus_1) libc.need_locks = 0; - __tl_unlock(); - __restore_sigs(&set); - __release_ptc(); - if (map) __munmap(map, size); - return -ret; - } - - return 0; -fail: - __release_ptc(); - return EAGAIN; -} - -weak_alias(__pthread_exit, pthread_exit); -weak_alias(__pthread_create, pthread_create); diff --git a/porting/liteos_a/user/src/thread/pthread_detach.c b/porting/liteos_a/user/src/thread/pthread_detach.c deleted file mode 100644 index a29e25308..000000000 --- a/porting/liteos_a/user/src/thread/pthread_detach.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "pthread_impl.h" -#include - -static int __pthread_detach(pthread_t t) -{ - int ret; - - /* If the cas fails, detach state is either already-detached - * or exiting/exited, and pthread_join will trap or cleanup. */ - if (a_cas(&t->detach_state, DT_JOINABLE, DT_DETACHED) != DT_JOINABLE) - return __pthread_join(t, 0); - ret = __syscall(SYS_pthread_set_detach, t->tid); - if (ret) a_swap(&t->detach_state, DT_JOINABLE); - if (ret == ESRCH) ret = 0; - return ret; -} - -weak_alias(__pthread_detach, pthread_detach); -weak_alias(__pthread_detach, thrd_detach); diff --git a/porting/liteos_a/user/src/thread/pthread_getschedparam.c b/porting/liteos_a/user/src/thread/pthread_getschedparam.c deleted file mode 100644 index 6b6ed1d37..000000000 --- a/porting/liteos_a/user/src/thread/pthread_getschedparam.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "pthread_impl.h" -#include "lock.h" - -int pthread_getschedparam(pthread_t t, int *restrict policy, struct sched_param *restrict param) -{ - int r; - sigset_t set; - __block_app_sigs(&set); - LOCK(t->killlock); - if (!t->tid) { - r = ESRCH; - } else { - r = __syscall(SYS_sched_getparam, t->tid, param, MUSL_TYPE_THREAD); - if (r >= 0) { - r = __syscall(SYS_sched_getscheduler, t->tid, MUSL_TYPE_THREAD); - if (r >= 0) { - *policy = r; - r = 0; - } - } - - if (r < 0) { - r = -r; - } - } - UNLOCK(t->killlock); - __restore_sigs(&set); - return r; -} diff --git a/porting/liteos_a/user/src/thread/pthread_join.c b/porting/liteos_a/user/src/thread/pthread_join.c deleted file mode 100644 index 9801bf14c..000000000 --- a/porting/liteos_a/user/src/thread/pthread_join.c +++ /dev/null @@ -1,58 +0,0 @@ -#define _GNU_SOURCE -#include "pthread_impl.h" -#include - -static void dummy1(pthread_t t) -{ -} -weak_alias(dummy1, __tl_sync); - -static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at) -{ - int state, cs, r = 0; - unsigned int tid; - pthread_t self = __pthread_self(); - __pthread_testcancel(); - __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0); - if (t == self) { - r = EINVAL; - goto out; - } - - switch (t->detach_state) { - case DT_JOINABLE: { - r = __syscall(SYS_pthread_join, t->tid); - break; - } - case DT_EXITING: - break; - case DT_DETACHED: - default: - r = EINVAL; - break; - } - -out: - __pthread_setcancelstate(cs, 0); - if (r == ESRCH || r == EINVAL) return r; - __tl_sync(t); - if (res) *res = t->result; - tid = t->tid; - t->tid = 0; - return __syscall(SYS_pthread_deatch, tid); -} - -int __pthread_join(pthread_t t, void **res) -{ - return __pthread_timedjoin_np(t, res, 0); -} - -static int __pthread_tryjoin_np(pthread_t t, void **res) -{ - return t->detach_state==DT_JOINABLE ? EBUSY : __pthread_join(t, res); -} - -weak_alias(__pthread_tryjoin_np, pthread_tryjoin_np); -weak_alias(__pthread_timedjoin_np, pthread_timedjoin_np); -weak_alias(__pthread_join, pthread_join); diff --git a/porting/liteos_a/user/src/thread/pthread_mutex_getprioceiling.c b/porting/liteos_a/user/src/thread/pthread_mutex_getprioceiling.c deleted file mode 100644 index c6699947f..000000000 --- a/porting/liteos_a/user/src/thread/pthread_mutex_getprioceiling.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include "pthread_impl.h" - -int pthread_mutex_getprioceiling(const pthread_mutex_t *restrict m, int *restrict ceiling) -{ - unsupported_api(__FUNCTION__); - return EINVAL; -} diff --git a/porting/liteos_a/user/src/thread/pthread_mutex_timedlock.c b/porting/liteos_a/user/src/thread/pthread_mutex_timedlock.c deleted file mode 100644 index 20689669f..000000000 --- a/porting/liteos_a/user/src/thread/pthread_mutex_timedlock.c +++ /dev/null @@ -1,90 +0,0 @@ -#include "pthread_impl.h" - -#define IS32BIT(x) !((x)+0x80000000ULL>>32) -#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) - -static int __futex4(volatile void *addr, int op, int val, const struct timespec *to) -{ -#ifdef SYS_futex_time64 - time_t s = to ? to->tv_sec : 0; - long ns = to ? to->tv_nsec : 0; - int r = -ENOSYS; - if (SYS_futex == SYS_futex_time64 || !IS32BIT(s)) - r = __syscall(SYS_futex_time64, addr, op, val, - to ? ((long long[]){s, ns}) : 0); - if (SYS_futex == SYS_futex_time64 || r!=-ENOSYS) return r; - to = to ? (void *)(long[]){CLAMP(s), ns} : 0; -#endif - return __syscall(SYS_futex, addr, op, val, to); -} - -static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct timespec *restrict at) -{ - int type = m->_m_type; - int priv = (type & 128) ^ 128; - pthread_t self = __pthread_self(); - int e; - - if (!priv) self->robust_list.pending = &m->_m_next; - - do e = -__futex4(&m->_m_lock, FUTEX_LOCK_PI|priv, 0, at); - while (e==EINTR); - if (e) self->robust_list.pending = 0; - - switch (e) { - case 0: - /* Catch spurious success for non-robust mutexes. */ - if (!(type&4) && ((m->_m_lock & 0x40000000) || m->_m_waiters)) { - a_store(&m->_m_waiters, -1); - __syscall(SYS_futex, &m->_m_lock, FUTEX_UNLOCK_PI|priv); - self->robust_list.pending = 0; - break; - } - /* Signal to trylock that we already have the lock. */ - m->_m_count = -1; - return __pthread_mutex_trylock(m); - case ETIMEDOUT: - return e; - case EDEADLK: - if ((type&3) == PTHREAD_MUTEX_ERRORCHECK) return e; - } - do e = __timedwait(&(int){0}, 0, CLOCK_REALTIME, at, 1); - while (e != ETIMEDOUT); - return e; -} - -int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at) -{ - if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL - && !a_cas(&m->_m_lock, 0, EBUSY)) - return 0; - - int type = m->_m_type; - int r, t, priv = (type & 128) ^ 128; - - r = __pthread_mutex_trylock(m); - if (r != EBUSY) return r; - - int spins = 100; - while (spins-- && m->_m_lock && !m->_m_waiters) a_spin(); - - while ((r=__pthread_mutex_trylock(m)) == EBUSY) { - r = m->_m_lock; - int own = r & 0x3fffffff; - if (!own && (!r || (type&4))) - continue; - if ((type&3) == PTHREAD_MUTEX_ERRORCHECK - && own == __pthread_self()->tid) - return EDEADLK; - - a_inc(&m->_m_waiters); - t = r | 0x80000000; - a_cas(&m->_m_lock, r, t); - r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, priv); - a_dec(&m->_m_waiters); - if (r && r != EINTR) break; - } - return r; -} - -weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock); diff --git a/porting/liteos_a/user/src/thread/pthread_mutex_trylock.c b/porting/liteos_a/user/src/thread/pthread_mutex_trylock.c deleted file mode 100644 index 8efd615f7..000000000 --- a/porting/liteos_a/user/src/thread/pthread_mutex_trylock.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "pthread_impl.h" - -int __pthread_mutex_trylock_owner(pthread_mutex_t *m) -{ - int old, own; - int type = m->_m_type; - pthread_t self = __pthread_self(); - int tid = self->tid; - - old = m->_m_lock; - own = old & 0x3fffffff; - if (own == tid) { - if ((type&PTHREAD_MUTEX_TYPE_MASK) == PTHREAD_MUTEX_RECURSIVE) { - if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN; - m->_m_count++; - return 0; - } - } - if (own == 0x3fffffff) return ENOTRECOVERABLE; - if (own || (old && !(type & 4))) return EBUSY; - - if (type & 128) { - if (!self->robust_list.off) { - self->robust_list.off = (char*)&m->_m_lock-(char *)&m->_m_next; - } - if (m->_m_waiters) tid |= 0x80000000; - self->robust_list.pending = &m->_m_next; - } - tid |= old & 0x40000000; - - if (a_cas(&m->_m_lock, old, tid) != old) { - self->robust_list.pending = 0; - return EBUSY; - } - - volatile void *next = self->robust_list.head; - m->_m_next = next; - m->_m_prev = &self->robust_list.head; - if (next != &self->robust_list.head) *(volatile void *volatile *) - ((char *)next - sizeof(void *)) = &m->_m_next; - self->robust_list.head = &m->_m_next; - self->robust_list.pending = 0; - - if (old) { - m->_m_count = 0; - return EOWNERDEAD; - } - - return 0; -} - -int __pthread_mutex_trylock(pthread_mutex_t *m) -{ - if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL) - return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY; - return __pthread_mutex_trylock_owner(m); -} - -weak_alias(__pthread_mutex_trylock, pthread_mutex_trylock); diff --git a/porting/liteos_a/user/src/thread/pthread_mutex_unlock.c b/porting/liteos_a/user/src/thread/pthread_mutex_unlock.c deleted file mode 100644 index d65108faa..000000000 --- a/porting/liteos_a/user/src/thread/pthread_mutex_unlock.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "pthread_impl.h" - -int __pthread_mutex_unlock(pthread_mutex_t *m) -{ - pthread_t self; - int waiters = m->_m_waiters; - int cont; - int type = m->_m_type & 15; - int priv = (m->_m_type & 128) ^ 128; - int new = 0; - int old; - - if (type != PTHREAD_MUTEX_NORMAL) { - self = __pthread_self(); - old = m->_m_lock; - int own = old & 0x3fffffff; - if (own != self->tid) - return EPERM; - if ((type&PTHREAD_MUTEX_TYPE_MASK) == PTHREAD_MUTEX_RECURSIVE && m->_m_count) - return m->_m_count--, 0; - if ((type&4) && (old&0x40000000)) - new = 0x7fffffff; - if (!priv) { - self->robust_list.pending = &m->_m_next; - __vm_lock(); - } - volatile void *prev = m->_m_prev; - volatile void *next = m->_m_next; - *(volatile void *volatile *)prev = next; - if (next != &self->robust_list.head) *(volatile void *volatile *) - ((char *)next - sizeof(void *)) = prev; - } - - cont = a_swap(&m->_m_lock, new); - - if (type != PTHREAD_MUTEX_NORMAL && !priv) { - self->robust_list.pending = 0; - __vm_unlock(); - } - if (waiters || cont<0) - __wake(&m->_m_lock, 1, priv); - return 0; -} - -weak_alias(__pthread_mutex_unlock, pthread_mutex_unlock); diff --git a/porting/liteos_a/user/src/thread/pthread_mutexattr_setprotocol.c b/porting/liteos_a/user/src/thread/pthread_mutexattr_setprotocol.c deleted file mode 100644 index 1dfd3626c..000000000 --- a/porting/liteos_a/user/src/thread/pthread_mutexattr_setprotocol.c +++ /dev/null @@ -1,30 +0,0 @@ -#include "pthread_impl.h" -#include -#include "syscall.h" - -static volatile int check_pi_result = -1; - -int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int protocol) -{ - int r; - unsupported_api(__FUNCTION__); - switch (protocol) { - case PTHREAD_PRIO_NONE: - a->__attr &= ~8; - return 0; - case PTHREAD_PRIO_INHERIT: - r = check_pi_result; - if (r < 0) { - volatile int lk = 0; - r = -__syscall(SYS_futex, &lk, FUTEX_LOCK_PI, 0, 0); - a_store(&check_pi_result, r); - } - if (r) return r; - a->__attr |= 8; - return 0; - case PTHREAD_PRIO_PROTECT: - return ENOTSUP; - default: - return EINVAL; - } -} diff --git a/porting/liteos_a/user/src/thread/pthread_mutexattr_setrobust.c b/porting/liteos_a/user/src/thread/pthread_mutexattr_setrobust.c deleted file mode 100644 index 9a9750d28..000000000 --- a/porting/liteos_a/user/src/thread/pthread_mutexattr_setrobust.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "pthread_impl.h" -#include "syscall.h" - -int pthread_mutexattr_setrobust(pthread_mutexattr_t *a, int robust) -{ - if (robust > 1U) return EINVAL; - if (robust) { - a->__attr |= 4; - return 0; - } - a->__attr &= ~4; - return 0; -} diff --git a/porting/liteos_a/user/src/thread/pthread_self.c b/porting/liteos_a/user/src/thread/pthread_self.c deleted file mode 100644 index 886ea4029..000000000 --- a/porting/liteos_a/user/src/thread/pthread_self.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "pthread_impl.h" -#include - -pthread_t __pthread_self() -{ - uintptr_t p; - p = __syscall(SYS_get_thread_area); - return (void *)(p - sizeof(struct pthread)); -} - -static pthread_t __pthread_self_internal() -{ - return __pthread_self(); -} - -weak_alias(__pthread_self_internal, pthread_self); -weak_alias(__pthread_self_internal, thrd_current); diff --git a/porting/liteos_a/user/src/thread/pthread_setschedparam.c b/porting/liteos_a/user/src/thread/pthread_setschedparam.c deleted file mode 100644 index 0a1a6837d..000000000 --- a/porting/liteos_a/user/src/thread/pthread_setschedparam.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "pthread_impl.h" -#include "lock.h" - -int pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param) -{ - int r; - sigset_t set; - __block_app_sigs(&set); - LOCK(t->killlock); - r = !t->tid ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, param, MUSL_TYPE_THREAD); - UNLOCK(t->killlock); - __restore_sigs(&set); - return r; -} diff --git a/porting/liteos_a/user/src/thread/pthread_setschedprio.c b/porting/liteos_a/user/src/thread/pthread_setschedprio.c deleted file mode 100644 index c21207140..000000000 --- a/porting/liteos_a/user/src/thread/pthread_setschedprio.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "pthread_impl.h" -#include "lock.h" - -int pthread_setschedprio(pthread_t t, int prio) -{ - int r; - struct sched_param param = { - .sched_priority = prio, - }; - - sigset_t set; - __block_app_sigs(&set); - LOCK(t->killlock); - r = !t->tid ? ESRCH : -__syscall(SYS_sched_setparam, t->tid, ¶m, MUSL_TYPE_THREAD); - UNLOCK(t->killlock); - __restore_sigs(&set); - return r; -} diff --git a/porting/liteos_a/user/src/thread/sem_open.c b/porting/liteos_a/user/src/thread/sem_open.c deleted file mode 100644 index 875064737..000000000 --- a/porting/liteos_a/user/src/thread/sem_open.c +++ /dev/null @@ -1,181 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lock.h" -#include "fork_impl.h" - -#define malloc __libc_malloc -#define calloc __libc_calloc -#define realloc undef -#define free undef - -static struct { - ino_t ino; - sem_t *sem; - int refcnt; -} *semtab; -static volatile int lock[1]; -volatile int *const __sem_open_lockptr = lock; - -#define FLAGS (O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK) - -sem_t *sem_open(const char *name, int flags, ...) -{ - va_list ap; - mode_t mode; - unsigned value; - int fd, i, e, slot, first=1, cnt, cs; - sem_t newsem; - void *map; - char tmp[64]; - struct timespec ts; - struct stat st; - char buf[NAME_MAX+10]; - - if (!(name = __shm_mapname(name, buf))) - return SEM_FAILED; - - LOCK(lock); - /* Allocate table if we don't have one yet */ - if (!semtab && !(semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX))) { - UNLOCK(lock); - return SEM_FAILED; - } - - /* Reserve a slot in case this semaphore is not mapped yet; - * this is necessary because there is no way to handle - * failures after creation of the file. */ - slot = -1; - for (cnt=i=0; i= 0) { - if (fstat(fd, &st) < 0 || - (map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { - close(fd); - goto fail; - } - break; - } - if (errno != ENOENT) - goto fail; - } - if (!(flags & O_CREAT)) - goto fail; - if (first) { - first = 0; - va_start(ap, flags); - mode = va_arg(ap, mode_t) & 0666; - value = va_arg(ap, unsigned); - va_end(ap); - if (value > SEM_VALUE_MAX) { - errno = EINVAL; - goto fail; - } - sem_init(&newsem, 1, value); - } - /* Create a temp file with the new semaphore contents - * and attempt to atomically link it as the new name */ - clock_gettime(CLOCK_REALTIME, &ts); - snprintf(tmp, sizeof(tmp), "/dev/shm/tmp-%d", (int)ts.tv_nsec); - fd = open(tmp, O_CREAT|O_EXCL|FLAGS, mode); - if (fd < 0) { - if (errno == EEXIST) continue; - goto fail; - } - if (write(fd, &newsem, sizeof newsem) != sizeof newsem || fstat(fd, &st) < 0 || - (map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { - close(fd); - unlink(tmp); - goto fail; - } - e = link(tmp, name) ? errno : 0; - unlink(tmp); - if (!e) break; - munmap(map, sizeof(sem_t)); - /* Failure is only fatal when doing an exclusive open; - * otherwise, next iteration will try to open the - * existing file. */ - if (e != EEXIST || flags == (O_CREAT|O_EXCL)) - goto fail; - } - - /* See if the newly mapped semaphore is already mapped. If - * so, unmap the new mapping and use the existing one. Otherwise, - * add it to the table of mapped semaphores. */ - LOCK(lock); - for (i=0; i -#include "syscall.h" - -void thrd_yield() -{ - __syscall(SYS_sched_yield, -1); -} diff --git a/porting/liteos_a/user/src/time/__map_file.c b/porting/liteos_a/user/src/time/__map_file.c deleted file mode 100644 index 80e0df3db..000000000 --- a/porting/liteos_a/user/src/time/__map_file.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include -#include "syscall.h" -#include "kstat.h" - -const char unsigned *__map_file(const char *pathname, size_t *size) -{ - struct kstat st; - const unsigned char *map = MAP_FAILED; - int fd = sys_open(pathname, O_RDONLY|O_CLOEXEC|O_NONBLOCK); - if (fd < 0) return 0; - if (!syscall(SYS_fstat, fd, &st)) { - map = __mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - *size = st.st_size; - } - if (map == MAP_FAILED) { - __syscall(SYS_close, fd); - } - return map == MAP_FAILED ? 0 : map; -} diff --git a/porting/liteos_a/user/src/time/clock.c b/porting/liteos_a/user/src/time/clock.c deleted file mode 100644 index 06994710b..000000000 --- a/porting/liteos_a/user/src/time/clock.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include - -clock_t clock() -{ - struct timespec ts; - - unsupported_api(__FUNCTION__); - if (__clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) - return -1; - - if (ts.tv_sec > LONG_MAX/1000000 - || ts.tv_nsec/1000 > LONG_MAX-1000000*ts.tv_sec) - return -1; - - return ts.tv_sec*1000000 + ts.tv_nsec/1000; -} diff --git a/porting/liteos_a/user/src/time/times.c b/porting/liteos_a/user/src/time/times.c deleted file mode 100644 index 9c50144f6..000000000 --- a/porting/liteos_a/user/src/time/times.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "syscall.h" - -clock_t times(struct tms *tms) -{ - return syscall(SYS_times, tms); -} diff --git a/porting/liteos_a/user/src/time/utime.c b/porting/liteos_a/user/src/time/utime.c deleted file mode 100644 index 36279de35..000000000 --- a/porting/liteos_a/user/src/time/utime.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include -#include -#include - -int utime(const char *path, const struct utimbuf *times) -{ - unsupported_api(__FUNCTION__); - return utimensat(AT_FDCWD, path, times ? ((struct timespec [2]){ - { .tv_sec = times->actime }, { .tv_sec = times->modtime }}) - : 0, 0); -} diff --git a/porting/liteos_a/user/src/unistd/ctermid.c b/porting/liteos_a/user/src/unistd/ctermid.c deleted file mode 100644 index dc30487f7..000000000 --- a/porting/liteos_a/user/src/unistd/ctermid.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include - -char *ctermid(char *s) -{ - unsupported_api(__FUNCTION__); - return s ? strcpy(s, "/dev/tty") : "/dev/tty"; -} diff --git a/porting/liteos_a/user/src/unistd/fdatasync.c b/porting/liteos_a/user/src/unistd/fdatasync.c deleted file mode 100644 index ee88d08da..000000000 --- a/porting/liteos_a/user/src/unistd/fdatasync.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -#include "syscall.h" - -int fdatasync(int fd) -{ - unsupported_api(__FUNCTION__); - return syscall_cp(SYS_fdatasync, fd); -} diff --git a/porting/liteos_a/user/src/unistd/lchown.c b/porting/liteos_a/user/src/unistd/lchown.c deleted file mode 100644 index e155ee02d..000000000 --- a/porting/liteos_a/user/src/unistd/lchown.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include "syscall.h" -#include - -int lchown(const char *path, uid_t uid, gid_t gid) -{ - unsupported_api(__FUNCTION__); -#ifdef SYS_lchown - return syscall(SYS_lchown, path, uid, gid); -#else - return syscall(SYS_fchownat, AT_FDCWD, path, uid, gid, AT_SYMLINK_NOFOLLOW); -#endif -} diff --git a/porting/liteos_a/user/src/unistd/pipe2.c b/porting/liteos_a/user/src/unistd/pipe2.c deleted file mode 100644 index 2125dd322..000000000 --- a/porting/liteos_a/user/src/unistd/pipe2.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include -#include "syscall.h" - -int pipe2(int fd[2], int flag) -{ - if (!flag) return pipe(fd); - unsupported_api(__FUNCTION__); - int ret = __syscall(SYS_pipe2, fd, flag); - if (ret != -ENOSYS) return __syscall_ret(ret); - ret = pipe(fd); - if (ret) return ret; - if (flag & O_CLOEXEC) { - __syscall(SYS_fcntl, fd[0], F_SETFD, FD_CLOEXEC); - __syscall(SYS_fcntl, fd[1], F_SETFD, FD_CLOEXEC); - } - if (flag & O_NONBLOCK) { - __syscall(SYS_fcntl, fd[0], F_SETFL, O_NONBLOCK); - __syscall(SYS_fcntl, fd[1], F_SETFL, O_NONBLOCK); - } - return 0; -} diff --git a/porting/liteos_a/user/src/unistd/setegid.c b/porting/liteos_a/user/src/unistd/setegid.c deleted file mode 100644 index 3abdfaf82..000000000 --- a/porting/liteos_a/user/src/unistd/setegid.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "libc.h" -#include "syscall.h" -#include "errno.h" - -int setegid(gid_t egid) -{ - if (egid == -1) { - errno = EINVAL; - return -1; - } - return __setxid(SYS_setresgid, -1, egid, -1); -} diff --git a/porting/liteos_a/user/src/unistd/seteuid.c b/porting/liteos_a/user/src/unistd/seteuid.c deleted file mode 100644 index ea2562c74..000000000 --- a/porting/liteos_a/user/src/unistd/seteuid.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "syscall.h" -#include "libc.h" -#include "errno.h" - -int seteuid(uid_t euid) -{ - if (euid == -1) { - errno = EINVAL; - return -1; - } - return __setxid(SYS_setresuid, -1, euid, -1); -} diff --git a/porting/liteos_a/user/src/unistd/setxid.c b/porting/liteos_a/user/src/unistd/setxid.c deleted file mode 100644 index bc7f40010..000000000 --- a/porting/liteos_a/user/src/unistd/setxid.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include "syscall.h" -#include "libc.h" - -struct ctx { - int id, eid, sid; - int nr, ret; -}; - -static void do_setxid(void *p) -{ - struct ctx *c = p; - if (c->ret<0) return; - int ret = __syscall(c->nr, c->id, c->eid, c->sid); - if (ret && !c->ret) { - /* If one thread fails to set ids after another has already - * succeeded, forcibly killing the process is the only safe - * thing to do. State is inconsistent and dangerous. Use - * SIGKILL because it is uncatchable. */ - __block_all_sigs(0); - __syscall(SYS_kill, __syscall(SYS_getpid), SIGKILL); - } - c->ret = ret; -} - -int __setxid(int nr, int id, int eid, int sid) -{ - /* ret is initially nonzero so that failure of the first thread does not - * trigger the safety kill above. */ - struct ctx c = { .nr = nr, .id = id, .eid = eid, .sid = sid, .ret = 1 }; - do_setxid(&c); - return __syscall_ret(c.ret); -} diff --git a/porting/liteos_a/user/src/unistd/tcgetpgrp.c b/porting/liteos_a/user/src/unistd/tcgetpgrp.c deleted file mode 100644 index 5fc8ae273..000000000 --- a/porting/liteos_a/user/src/unistd/tcgetpgrp.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include -#include - -pid_t tcgetpgrp(int fd) -{ - int pgrp; - unsupported_api(__FUNCTION__); - if (ioctl(fd, TIOCGPGRP, &pgrp) < 0) - return -1; - return pgrp; -} diff --git a/porting/liteos_a/user/src/unistd/ttyname.c b/porting/liteos_a/user/src/unistd/ttyname.c deleted file mode 100644 index 31f3dc189..000000000 --- a/porting/liteos_a/user/src/unistd/ttyname.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include -#include - -char *ttyname(int fd) -{ - static char buf[TTY_NAME_MAX]; - int result; - unsupported_api(__FUNCTION__); - if ((result = ttyname_r(fd, buf, sizeof buf))) { - errno = result; - return NULL; - } - return buf; -} diff --git a/porting/liteos_a/user_debug/Makefile b/porting/liteos_a/user_debug/Makefile deleted file mode 100644 index 0fb3f7b6a..000000000 --- a/porting/liteos_a/user_debug/Makefile +++ /dev/null @@ -1,237 +0,0 @@ -# -# Makefile for musl (requires GNU make) -# -# This is how simple every makefile should be... -# No, I take that back - actually most should be less than half this size. -# -# Use config.mak to override any of the following variables. -# Do not make changes here. -# - -srcdir = . -exec_prefix = /usr/local -bindir = $(exec_prefix)/bin - -prefix = /usr/local/musl -includedir = $(prefix)/include -libdir = $(prefix)/lib -syslibdir = /lib - -MALLOC_DIR = oldmalloc -SRC_DIRS = $(addprefix $(srcdir)/,src/* src/malloc/$(MALLOC_DIR) crt ldso $(COMPAT_SRC_DIRS)) -BASE_GLOBS = $(addsuffix /*.c,$(SRC_DIRS)) -ARCH_GLOBS = $(addsuffix /$(ARCH)/*.[csS],$(SRC_DIRS)) -BASE_SRCS = $(sort $(wildcard $(BASE_GLOBS))) -ARCH_SRCS = $(sort $(wildcard $(ARCH_GLOBS))) -BASE_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(BASE_SRCS))) -ARCH_OBJS = $(patsubst $(srcdir)/%,%.o,$(basename $(ARCH_SRCS))) -REPLACED_OBJS = $(sort $(subst /$(ARCH)/,/,$(ARCH_OBJS))) -ALL_OBJS = $(addprefix obj/, $(filter-out $(REPLACED_OBJS), $(sort $(BASE_OBJS) $(ARCH_OBJS)))) - -LIBC_OBJS = $(filter obj/src/%,$(ALL_OBJS)) $(filter obj/compat/%,$(ALL_OBJS)) -LDSO_OBJS = $(filter obj/ldso/%,$(ALL_OBJS:%.o=%.lo)) -CRT_OBJS = $(filter obj/crt/%,$(ALL_OBJS)) - -AOBJS = $(LIBC_OBJS) -LOBJS = $(LIBC_OBJS:.o=.lo) -GENH = obj/include/bits/alltypes.h obj/include/bits/syscall.h -GENH_INT = obj/src/internal/version.h -IMPH = $(addprefix $(srcdir)/, src/internal/stdio_impl.h src/internal/pthread_impl.h src/internal/locale_impl.h src/internal/libc.h) - -LDFLAGS = -LDFLAGS_AUTO = -LIBCC = -lgcc -CPPFLAGS = -CFLAGS = -CFLAGS_AUTO = -Os -pipe -CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc - -CFLAGS_ALL = $(CFLAGS_C99FSE) -CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I$(srcdir)/arch/$(ARCH) -I$(srcdir)/arch/generic -Iobj/src/internal -I$(srcdir)/src/include -I$(srcdir)/src/internal -Iobj/include -I$(srcdir)/include -CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO) $(CFLAGS) - -LDFLAGS_ALL = $(LDFLAGS_AUTO) $(LDFLAGS) - -AR = $(CROSS_COMPILE)ar -RANLIB = $(CROSS_COMPILE)ranlib -INSTALL = $(srcdir)/tools/install.sh - -ARCH_INCLUDES = $(wildcard $(srcdir)/arch/$(ARCH)/bits/*.h) -GENERIC_INCLUDES = $(wildcard $(srcdir)/arch/generic/bits/*.h) -INCLUDES = $(wildcard $(srcdir)/include/*.h $(srcdir)/include/*/*.h) -ALL_INCLUDES = $(sort $(INCLUDES:$(srcdir)/%=%) $(GENH:obj/%=%) $(ARCH_INCLUDES:$(srcdir)/arch/$(ARCH)/%=include/%) $(GENERIC_INCLUDES:$(srcdir)/arch/generic/%=include/%)) - -EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl -EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a) -CRT_LIBS = $(addprefix lib/,$(notdir $(CRT_OBJS))) -STATIC_LIBS = lib/libc.a -SHARED_LIBS = lib/libc.so -TOOL_LIBS = lib/musl-gcc.specs -ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS) -ALL_TOOLS = obj/musl-gcc - -WRAPCC_GCC = gcc -WRAPCC_CLANG = clang - -LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1 - --include config.mak --include $(srcdir)/arch/$(ARCH)/arch.mak - -ifeq ($(ARCH),) - -all: - @echo "Please set ARCH in config.mak before running make." - @exit 1 - -else - -all: $(ALL_LIBS) $(ALL_TOOLS) - -OBJ_DIRS = $(sort $(patsubst %/,%,$(dir $(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(GENH) $(GENH_INT))) obj/include) - -$(ALL_LIBS) $(ALL_TOOLS) $(ALL_OBJS) $(ALL_OBJS:%.o=%.lo) $(GENH) $(GENH_INT): | $(OBJ_DIRS) - -$(OBJ_DIRS): - mkdir -p $@ - -obj/include/bits/alltypes.h: $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in $(srcdir)/tools/mkalltypes.sed - sed -f $(srcdir)/tools/mkalltypes.sed $(srcdir)/arch/$(ARCH)/bits/alltypes.h.in $(srcdir)/include/alltypes.h.in > $@ - -obj/include/bits/syscall.h: $(srcdir)/arch/$(ARCH)/bits/syscall.h.in - cp $< $@ - sed -n -e s/__NR_/SYS_/p < $< >> $@ - -obj/src/internal/version.h: $(wildcard $(srcdir)/VERSION $(srcdir)/.git) - printf '#define VERSION "%s"\n' "$$(cd $(srcdir); sh tools/version.sh)" > $@ - -obj/src/internal/version.o obj/src/internal/version.lo: obj/src/internal/version.h - -obj/crt/rcrt1.o obj/ldso/dlstart.lo obj/ldso/dynlink.lo: $(srcdir)/src/internal/dynlink.h $(srcdir)/arch/$(ARCH)/reloc.h - -obj/crt/crt1.o obj/crt/scrt1.o obj/crt/rcrt1.o obj/ldso/dlstart.lo: $(srcdir)/arch/$(ARCH)/crt_arch.h - -obj/crt/rcrt1.o: $(srcdir)/ldso/dlstart.c - -obj/crt/Scrt1.o obj/crt/rcrt1.o: CFLAGS_ALL += -fPIC - -OPTIMIZE_SRCS = $(wildcard $(OPTIMIZE_GLOBS:%=$(srcdir)/src/%)) -$(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.o) $(OPTIMIZE_SRCS:$(srcdir)/%.c=obj/%.lo): CFLAGS += -O3 - -MEMOPS_OBJS = $(filter %/memcpy.o %/memmove.o %/memcmp.o %/memset.o, $(LIBC_OBJS)) -$(MEMOPS_OBJS) $(MEMOPS_OBJS:%.o=%.lo): CFLAGS_ALL += $(CFLAGS_MEMOPS) - -NOSSP_OBJS = $(CRT_OBJS) $(LDSO_OBJS) $(filter \ - %/__libc_start_main.o %/__init_tls.o %/__stack_chk_fail.o \ - %/__set_thread_area.o %/memset.o %/memcpy.o \ - , $(LIBC_OBJS)) -$(NOSSP_OBJS) $(NOSSP_OBJS:%.o=%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP) - -$(CRT_OBJS): CFLAGS_ALL += -DCRT - -$(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC - -CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $< - -# Choose invocation of assembler to be used -ifeq ($(ADD_CFI),yes) - AS_CMD = LC_ALL=C awk -f $(srcdir)/tools/add-cfi.common.awk -f $(srcdir)/tools/add-cfi.$(ARCH).awk $< | $(CC) $(CFLAGS_ALL) -x assembler -c -o $@ - -else - AS_CMD = $(CC_CMD) -endif - -obj/%.o: $(srcdir)/%.s - $(AS_CMD) - -obj/%.o: $(srcdir)/%.S - $(CC_CMD) - -obj/%.o: $(srcdir)/%.c $(GENH) $(IMPH) - $(CC_CMD) - -obj/%.lo: $(srcdir)/%.s - $(AS_CMD) - -obj/%.lo: $(srcdir)/%.S - $(CC_CMD) - -obj/%.lo: $(srcdir)/%.c $(GENH) $(IMPH) - $(CC_CMD) - -lib/libc.so: $(LOBJS) $(LDSO_OBJS) - $(CC) $(CFLAGS_ALL) $(LDFLAGS_ALL) -nostdlib -shared \ - -Wl,-e,_dlstart -o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC) - -lib/libc.a: $(AOBJS) - rm -f $@ - $(AR) rc $@ $(AOBJS) - $(RANLIB) $@ - -$(EMPTY_LIBS): - rm -f $@ - $(AR) rc $@ - -lib/%.o: obj/crt/$(ARCH)/%.o - cp $< $@ - -lib/%.o: obj/crt/%.o - cp $< $@ - -lib/musl-gcc.specs: $(srcdir)/tools/musl-gcc.specs.sh config.mak - sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@ - -obj/musl-gcc: config.mak - printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@ - chmod +x $@ - -obj/%-clang: $(srcdir)/tools/%-clang.in config.mak - sed -e 's!@CC@!$(WRAPCC_CLANG)!g' -e 's!@PREFIX@!$(prefix)!g' -e 's!@INCDIR@!$(includedir)!g' -e 's!@LIBDIR@!$(libdir)!g' -e 's!@LDSO@!$(LDSO_PATHNAME)!g' $< > $@ - chmod +x $@ - -$(DESTDIR)$(bindir)/%: obj/% - $(INSTALL) -D $< $@ - -$(DESTDIR)$(libdir)/%.so: lib/%.so - $(INSTALL) -D -m 755 $< $@ - -$(DESTDIR)$(libdir)/%: lib/% - $(INSTALL) -D -m 644 $< $@ - -$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/$(ARCH)/bits/% - $(INSTALL) -D -m 644 $< $@ - -$(DESTDIR)$(includedir)/bits/%: $(srcdir)/arch/generic/bits/% - $(INSTALL) -D -m 644 $< $@ - -$(DESTDIR)$(includedir)/bits/%: obj/include/bits/% - $(INSTALL) -D -m 644 $< $@ - -$(DESTDIR)$(includedir)/%: $(srcdir)/include/% - $(INSTALL) -D -m 644 $< $@ - -$(DESTDIR)$(LDSO_PATHNAME): $(DESTDIR)$(libdir)/libc.so - $(INSTALL) -D -l $(libdir)/libc.so $@ || true - -install-libs: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(if $(SHARED_LIBS),$(DESTDIR)$(LDSO_PATHNAME),) - -install-headers: $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%) - -install-tools: $(ALL_TOOLS:obj/%=$(DESTDIR)$(bindir)/%) - -install: install-libs install-headers install-tools - -musl-git-%.tar.gz: .git - git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ $(patsubst musl-git-%.tar.gz,%,$@) - -musl-%.tar.gz: .git - git --git-dir=$(srcdir)/.git archive --format=tar.gz --prefix=$(patsubst %.tar.gz,%,$@)/ -o $@ v$(patsubst musl-%.tar.gz,%,$@) - -endif - -clean: - rm -rf obj lib - -distclean: clean - rm -f config.mak - -.PHONY: all clean install install-libs install-headers install-tools diff --git a/porting/liteos_a/user_debug/ldso/dynlink.c b/porting/liteos_a/user_debug/ldso/dynlink.c deleted file mode 100644 index b1244cb07..000000000 --- a/porting/liteos_a/user_debug/ldso/dynlink.c +++ /dev/null @@ -1,2394 +0,0 @@ -#define _GNU_SOURCE -#define SYSCALL_NO_TLS 1 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "pthread_impl.h" -#include "fork_impl.h" -#include "libc.h" -#include "dynlink.h" - -#define malloc __libc_malloc -#define calloc __libc_calloc -#define realloc __libc_realloc -#define free __libc_free - -static void error(const char *, ...); - -#define MAXP2(a,b) (-(-(a)&-(b))) -#define ALIGN(x,y) ((x)+(y)-1 & -(y)) - -#define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m))) -#define countof(a) ((sizeof (a))/(sizeof (a)[0])) - -struct debug { - int ver; - void *head; - void (*bp)(void); - int state; - void *base; -}; - -struct td_index { - size_t args[2]; - struct td_index *next; -}; - -struct dso { -#if DL_FDPIC - struct fdpic_loadmap *loadmap; -#else - unsigned char *base; -#endif - char *name; - size_t *dynv; - struct dso *next, *prev; - - Phdr *phdr; - int phnum; - size_t phentsize; - Sym *syms; - Elf_Symndx *hashtab; - uint32_t *ghashtab; - int16_t *versym; - char *strings; - struct dso *syms_next, *lazy_next; - size_t *lazy, lazy_cnt; - unsigned char *map; - size_t map_len; - char relocated; - char constructed; - char kernel_mapped; - char mark; - char bfs_built; - char runtime_loaded; - struct dso **deps, *needed_by; - size_t ndeps_direct; - size_t next_dep; - pthread_t ctor_visitor; - char *rpath_orig, *rpath; - struct tls_module tls; - size_t tls_id; - size_t relro_start, relro_end; - uintptr_t *new_dtv; - unsigned char *new_tls; - struct td_index *td_index; - struct dso *fini_next; - char *shortname; -#if DL_FDPIC - unsigned char *base; -#else - struct fdpic_loadmap *loadmap; -#endif - struct funcdesc { - void *addr; - size_t *got; - } *funcdescs; - size_t *got; - char buf[]; -}; - -struct symdef { - Sym *sym; - struct dso *dso; -}; - -typedef void (*stage3_func)(size_t *, size_t *); - -static struct builtin_tls { - char c; - struct pthread pt; - void *space[16]; -} builtin_tls[1]; -#define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt) - -#define ADDEND_LIMIT 4096 -static size_t *saved_addends, *apply_addends_to; - -static struct dso ldso; -static struct dso *head, *tail, *fini_head, *syms_tail, *lazy_head; -static char *env_path, *sys_path; -static unsigned long long gencnt; -static int runtime; -static int ldd_mode; -static int ldso_fail; -static int noload; -static int shutting_down; -static jmp_buf *rtld_fail; -static pthread_rwlock_t lock; -static struct debug debug; -static struct tls_module *tls_tail; -static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN; -static size_t static_tls_cnt; -static pthread_mutex_t init_fini_lock; -static pthread_cond_t ctor_cond; -static struct dso *builtin_deps[2]; -static struct dso *const no_deps[1]; -static struct dso *builtin_ctor_queue[4]; -static struct dso **main_ctor_queue; -static struct fdpic_loadmap *app_loadmap; -static struct fdpic_dummy_loadmap app_dummy_loadmap; - -struct debug *_dl_debug_addr = &debug; - -extern hidden int __malloc_replaced; - -hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0; - -extern hidden void (*const __init_array_end)(void), (*const __fini_array_end)(void); - -weak_alias(__init_array_start, __init_array_end); -weak_alias(__fini_array_start, __fini_array_end); - -static int dl_strcmp(const char *l, const char *r) -{ - for (; *l==*r && *l; l++, r++); - return *(unsigned char *)l - *(unsigned char *)r; -} -#define strcmp(l,r) dl_strcmp(l,r) - -/* Compute load address for a virtual address in a given dso. */ -#if DL_FDPIC -static void *laddr(const struct dso *p, size_t v) -{ - size_t j=0; - if (!p->loadmap) return p->base + v; - for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++); - return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr); -} -static void *laddr_pg(const struct dso *p, size_t v) -{ - size_t j=0; - size_t pgsz = PAGE_SIZE; - if (!p->loadmap) return p->base + v; - for (j=0; ; j++) { - size_t a = p->loadmap->segs[j].p_vaddr; - size_t b = a + p->loadmap->segs[j].p_memsz; - a &= -pgsz; - b += pgsz-1; - b &= -pgsz; - if (v-aloadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr); -} -static void (*fdbarrier(void *p))() -{ - void (*fd)(); - __asm__("" : "=r"(fd) : "0"(p)); - return fd; -} -#define fpaddr(p, v) fdbarrier((&(struct funcdesc){ \ - laddr(p, v), (p)->got })) -#else -#define laddr(p, v) (void *)((p)->base + (v)) -#define laddr_pg(p, v) laddr(p, v) -#define fpaddr(p, v) ((void (*)())laddr(p, v)) -#endif - -static void decode_vec(size_t *v, size_t *a, size_t cnt) -{ - size_t i; - for (i=0; i>24 & 0xf0; - } - return h & 0xfffffff; -} - -static uint32_t gnu_hash(const char *s0) -{ - const unsigned char *s = (void *)s0; - uint_fast32_t h = 5381; - for (; *s; s++) - h += h*32 + *s; - return h; -} - -static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso) -{ - size_t i; - Sym *syms = dso->syms; - Elf_Symndx *hashtab = dso->hashtab; - char *strings = dso->strings; - for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) { - if ((!dso->versym || dso->versym[i] >= 0) - && (!strcmp(s, strings+syms[i].st_name))) - return syms+i; - } - return 0; -} - -static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s) -{ - uint32_t nbuckets = hashtab[0]; - uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4); - uint32_t i = buckets[h1 % nbuckets]; - - if (!i) return 0; - - uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]); - - for (h1 |= 1; ; i++) { - uint32_t h2 = *hashval++; - if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0) - && !strcmp(s, dso->strings + dso->syms[i].st_name)) - return dso->syms+i; - if (h2 & 1) break; - } - - return 0; -} - -static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask) -{ - const size_t *bloomwords = (const void *)(hashtab+4); - size_t f = bloomwords[fofs & (hashtab[2]-1)]; - if (!(f & fmask)) return 0; - - f >>= (h1 >> hashtab[3]) % (8 * sizeof f); - if (!(f & 1)) return 0; - - return gnu_lookup(h1, hashtab, dso, s); -} - -#define OK_TYPES (1<deps : 0; - for (; dso; dso=use_deps ? *deps++ : dso->syms_next) { - Sym *sym; - if ((ght = dso->ghashtab)) { - sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm); - } else { - if (!h) h = sysv_hash(s); - sym = sysv_lookup(s, h, dso); - } - if (!sym) continue; - if (!sym->st_shndx) - if (need_def || (sym->st_info&0xf) == STT_TLS - || ARCH_SYM_REJECT_UND(sym)) - continue; - if (!sym->st_value) - if ((sym->st_info&0xf) != STT_TLS) - continue; - if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue; - if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue; - def.sym = sym; - def.dso = dso; - break; - } - return def; -} - -static struct symdef find_sym(struct dso *dso, const char *s, int need_def) -{ - return find_sym2(dso, s, need_def, 0); -} - -static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride) -{ - unsigned char *base = dso->base; - Sym *syms = dso->syms; - char *strings = dso->strings; - Sym *sym; - const char *name; - void *ctx; - int type; - int sym_index; - struct symdef def; - size_t *reloc_addr; - size_t sym_val; - size_t tls_val; - size_t addend; - int skip_relative = 0, reuse_addends = 0, save_slot = 0; - - if (dso == &ldso) { - /* Only ldso's REL table needs addend saving/reuse. */ - if (rel == apply_addends_to) - reuse_addends = 1; - skip_relative = 1; - } - - for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { - if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue; - type = R_TYPE(rel[1]); - if (type == REL_NONE) continue; - reloc_addr = laddr(dso, rel[0]); - - if (stride > 2) { - addend = rel[2]; - } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) { - addend = 0; - } else if (reuse_addends) { - /* Save original addend in stage 2 where the dso - * chain consists of just ldso; otherwise read back - * saved addend since the inline one was clobbered. */ - if (head==&ldso) - saved_addends[save_slot] = *reloc_addr; - addend = saved_addends[save_slot++]; - } else { - addend = *reloc_addr; - } - - sym_index = R_SYM(rel[1]); - if (sym_index) { - sym = syms + sym_index; - name = strings + sym->st_name; - ctx = type==REL_COPY ? head->syms_next : head; - def = (sym->st_info>>4) == STB_LOCAL - ? (struct symdef){ .dso = dso, .sym = sym } - : find_sym(ctx, name, type==REL_PLT); - if (!def.sym && (sym->st_shndx != SHN_UNDEF - || sym->st_info>>4 != STB_WEAK)) { - if (dso->lazy && (type==REL_PLT || type==REL_GOT)) { - dso->lazy[3*dso->lazy_cnt+0] = rel[0]; - dso->lazy[3*dso->lazy_cnt+1] = rel[1]; - dso->lazy[3*dso->lazy_cnt+2] = addend; - dso->lazy_cnt++; - continue; - } - error("Error relocating %s: %s: symbol not found", - dso->name, name); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - } else { - sym = 0; - def.sym = 0; - def.dso = dso; - } - - sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0; - tls_val = def.sym ? def.sym->st_value : 0; - - if ((type == REL_TPOFF || type == REL_TPOFF_NEG) - && def.dso->tls_id > static_tls_cnt) { - error("Error relocating %s: %s: initial-exec TLS " - "resolves to dynamic definition in %s", - dso->name, name, def.dso->name); - longjmp(*rtld_fail, 1); - } - - switch(type) { - case REL_OFFSET: - addend -= (size_t)reloc_addr; - case REL_SYMBOLIC: - case REL_GOT: - case REL_PLT: - *reloc_addr = sym_val + addend; - break; - case REL_USYMBOLIC: - memcpy(reloc_addr, &(size_t){sym_val + addend}, sizeof(size_t)); - break; - case REL_RELATIVE: - *reloc_addr = (size_t)base + addend; - break; - case REL_SYM_OR_REL: - if (sym) *reloc_addr = sym_val + addend; - else *reloc_addr = (size_t)base + addend; - break; - case REL_COPY: - memcpy(reloc_addr, (void *)sym_val, sym->st_size); - break; - case REL_OFFSET32: - *(uint32_t *)reloc_addr = sym_val + addend - - (size_t)reloc_addr; - break; - case REL_FUNCDESC: - *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs - + (def.sym - def.dso->syms)) : 0; - break; - case REL_FUNCDESC_VAL: - if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val; - else *reloc_addr = sym_val; - reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0; - break; - case REL_DTPMOD: - *reloc_addr = def.dso->tls_id; - break; - case REL_DTPOFF: - *reloc_addr = tls_val + addend - DTP_OFFSET; - break; -#ifdef TLS_ABOVE_TP - case REL_TPOFF: - *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend; - break; -#else - case REL_TPOFF: - *reloc_addr = tls_val - def.dso->tls.offset + addend; - break; - case REL_TPOFF_NEG: - *reloc_addr = def.dso->tls.offset - tls_val + addend; - break; -#endif - case REL_TLSDESC: - if (stride<3) addend = reloc_addr[1]; - if (def.dso->tls_id > static_tls_cnt) { - struct td_index *new = malloc(sizeof *new); - if (!new) { - error( - "Error relocating %s: cannot allocate TLSDESC for %s", - dso->name, sym ? name : "(local)" ); - longjmp(*rtld_fail, 1); - } - new->next = dso->td_index; - dso->td_index = new; - new->args[0] = def.dso->tls_id; - new->args[1] = tls_val + addend - DTP_OFFSET; - reloc_addr[0] = (size_t)__tlsdesc_dynamic; - reloc_addr[1] = (size_t)new; - } else { - reloc_addr[0] = (size_t)__tlsdesc_static; -#ifdef TLS_ABOVE_TP - reloc_addr[1] = tls_val + def.dso->tls.offset - + TPOFF_K + addend; -#else - reloc_addr[1] = tls_val - def.dso->tls.offset - + addend; -#endif - } -#ifdef TLSDESC_BACKWARDS - /* Some archs (32-bit ARM at least) invert the order of - * the descriptor members. Fix them up here. */ - size_t tmp = reloc_addr[0]; - reloc_addr[0] = reloc_addr[1]; - reloc_addr[1] = tmp; -#endif - break; - default: - error("Error relocating %s: unsupported relocation type %d", - dso->name, type); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - } -} - -static void redo_lazy_relocs() -{ - struct dso *p = lazy_head, *next; - lazy_head = 0; - for (; p; p=next) { - next = p->lazy_next; - size_t size = p->lazy_cnt*3*sizeof(size_t); - p->lazy_cnt = 0; - do_relocs(p, p->lazy, size, 3); - if (p->lazy_cnt) { - p->lazy_next = lazy_head; - lazy_head = p; - } else { - free(p->lazy); - p->lazy = 0; - p->lazy_next = 0; - } - } -} - -/* A huge hack: to make up for the wastefulness of shared libraries - * needing at least a page of dirty memory even if they have no global - * data, we reclaim the gaps at the beginning and end of writable maps - * and "donate" them to the heap. */ - -static void reclaim(struct dso *dso, size_t start, size_t end) -{ - if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end; - if (end >= dso->relro_start && end < dso->relro_end) end = dso->relro_start; - if (start >= end) return; - char *base = laddr_pg(dso, start); - __malloc_donate(base, base+(end-start)); -} - -static void reclaim_gaps(struct dso *dso) -{ - Phdr *ph = dso->phdr; - size_t phcnt = dso->phnum; - - for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) { - if (ph->p_type!=PT_LOAD) continue; - if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue; - reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr); - reclaim(dso, ph->p_vaddr+ph->p_memsz, - ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE); - } -} - -static ssize_t read_loop(int fd, void *p, size_t n) -{ - for (size_t i=0; iloadmap) { - size_t i; - for (i=0; iloadmap->nsegs; i++) { - if (!dso->loadmap->segs[i].p_memsz) - continue; - munmap((void *)dso->loadmap->segs[i].addr, - dso->loadmap->segs[i].p_memsz); - } - free(dso->loadmap); - } else if (dso->map && dso->map_len) { - munmap(dso->map, dso->map_len); - } -} - -static void *map_library(int fd, struct dso *dso) -{ - Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; - void *allocated_buf=0; - size_t phsize; - size_t addr_min=SIZE_MAX, addr_max=0, map_len; - size_t this_min, this_max; - size_t nsegs = 0; - off_t off_start; - Ehdr *eh; - Phdr *ph, *ph0; - unsigned prot; - unsigned char *map=MAP_FAILED, *base; - size_t dyn=0; - size_t tls_image=0; - size_t i; - - ssize_t l = read(fd, buf, sizeof buf); - eh = buf; - if (l<0) return 0; - if (le_type != ET_DYN && eh->e_type != ET_EXEC)) - goto noexec; - phsize = eh->e_phentsize * eh->e_phnum; - if (phsize > sizeof buf - sizeof *eh) { - allocated_buf = malloc(phsize); - if (!allocated_buf) return 0; - l = pread(fd, allocated_buf, phsize, eh->e_phoff); - if (l < 0) goto error; - if (l != phsize) goto noexec; - ph = ph0 = allocated_buf; - } else if (eh->e_phoff + phsize > l) { - l = pread(fd, buf+1, phsize, eh->e_phoff); - if (l < 0) goto error; - if (l != phsize) goto noexec; - ph = ph0 = (void *)(buf + 1); - } else { - ph = ph0 = (void *)((char *)buf + eh->e_phoff); - } - for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { - if (ph->p_type == PT_DYNAMIC) { - dyn = ph->p_vaddr; - } else if (ph->p_type == PT_TLS) { - tls_image = ph->p_vaddr; - dso->tls.align = ph->p_align; - dso->tls.len = ph->p_filesz; - dso->tls.size = ph->p_memsz; - } else if (ph->p_type == PT_GNU_RELRO) { - dso->relro_start = ph->p_vaddr & -PAGE_SIZE; - dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; - } else if (ph->p_type == PT_GNU_STACK) { - if (!runtime && ph->p_memsz > __default_stacksize) { - __default_stacksize = - ph->p_memsz < DEFAULT_STACK_MAX ? - ph->p_memsz : DEFAULT_STACK_MAX; - } - } - if (ph->p_type != PT_LOAD) continue; - nsegs++; - if (ph->p_vaddr < addr_min) { - addr_min = ph->p_vaddr; - off_start = ph->p_offset; - prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | - ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | - ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); - } - if (ph->p_vaddr+ph->p_memsz > addr_max) { - addr_max = ph->p_vaddr+ph->p_memsz; - } - } - if (!dyn) goto noexec; - if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) { - dso->loadmap = calloc(1, sizeof *dso->loadmap - + nsegs * sizeof *dso->loadmap->segs); - if (!dso->loadmap) goto error; - dso->loadmap->nsegs = nsegs; - for (ph=ph0, i=0; ie_phentsize)) { - if (ph->p_type != PT_LOAD) continue; - prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | - ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | - ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); - map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1), - prot, MAP_PRIVATE, - fd, ph->p_offset & -PAGE_SIZE); - if (map == MAP_FAILED) { - unmap_library(dso); - goto error; - } - dso->loadmap->segs[i].addr = (size_t)map + - (ph->p_vaddr & PAGE_SIZE-1); - dso->loadmap->segs[i].p_vaddr = ph->p_vaddr; - dso->loadmap->segs[i].p_memsz = ph->p_memsz; - i++; - if (prot & PROT_WRITE) { - size_t brk = (ph->p_vaddr & PAGE_SIZE-1) - + ph->p_filesz; - size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE; - size_t pgend = brk + ph->p_memsz - ph->p_filesz - + PAGE_SIZE-1 & -PAGE_SIZE; - if (pgend > pgbrk && mmap_fixed(map+pgbrk, - pgend-pgbrk, prot, - MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, - -1, off_start) == MAP_FAILED) - goto error; - memset(map + brk, 0, pgbrk-brk); - } - } - map = (void *)dso->loadmap->segs[0].addr; - map_len = 0; - goto done_mapping; - } - addr_max += PAGE_SIZE-1; - addr_max &= -PAGE_SIZE; - addr_min &= -PAGE_SIZE; - off_start &= -PAGE_SIZE; - map_len = addr_max - addr_min + off_start; - /* The first time, we map too much, possibly even more than - * the length of the file. This is okay because we will not - * use the invalid part; we just need to reserve the right - * amount of virtual address space to map over later. */ - map = DL_NOMMU_SUPPORT - ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) - : mmap((void *)addr_min, map_len, prot, - MAP_PRIVATE, fd, off_start); - if (map==MAP_FAILED) goto error; - dso->map = map; - dso->map_len = map_len; - /* If the loaded file is not relocatable and the requested address is - * not available, then the load operation must fail. */ - if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) { - errno = EBUSY; - goto error; - } - base = map - addr_min; - dso->phdr = 0; - dso->phnum = 0; - for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { - if (ph->p_type != PT_LOAD) continue; - /* Check if the programs headers are in this load segment, and - * if so, record the address for use by dl_iterate_phdr. */ - if (!dso->phdr && eh->e_phoff >= ph->p_offset - && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) { - dso->phdr = (void *)(base + ph->p_vaddr - + (eh->e_phoff-ph->p_offset)); - dso->phnum = eh->e_phnum; - dso->phentsize = eh->e_phentsize; - } - this_min = ph->p_vaddr & -PAGE_SIZE; - this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; - off_start = ph->p_offset & -PAGE_SIZE; - prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | - ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | - ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); - if ((ph->p_flags & PF_R) && (ph->p_flags & PF_X) && (!(ph->p_flags & PF_W))) { - Phdr *next_ph = ph; - for (int j = i - 1; j > 0; j--) { - next_ph = (void *)((char *)next_ph+eh->e_phentsize); - if (next_ph->p_type != PT_LOAD) { - continue; - } - size_t p_vaddr = (next_ph->p_vaddr & -(PAGE_SIZE)); - if (p_vaddr > this_max) { - mprotect(base + this_max, p_vaddr - this_max , PROT_READ); - } - break; - } - } - /* Reuse the existing mapping for the lowest-address LOAD */ - if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT) - if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) - goto error; - if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) { - size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; - size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; - memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); - if (pgbrk-(size_t)base < this_max && mmap_fixed((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) - goto error; - } - } - for (i=0; ((size_t *)(base+dyn))[i]; i+=2) - if (((size_t *)(base+dyn))[i]==DT_TEXTREL) { - if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) - && errno != ENOSYS) - goto error; - break; - } -done_mapping: - dso->base = base; - dso->dynv = laddr(dso, dyn); - if (dso->tls.size) dso->tls.image = laddr(dso, tls_image); - free(allocated_buf); - return map; -noexec: - errno = ENOEXEC; -error: - if (map!=MAP_FAILED) unmap_library(dso); - free(allocated_buf); - return 0; -} - -static int path_open(const char *name, const char *s, char *buf, size_t buf_size) -{ - size_t l; - int fd; - for (;;) { - s += strspn(s, ":\n"); - l = strcspn(s, ":\n"); - if (l-1 >= INT_MAX) return -1; - if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) { - if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd; - switch (errno) { - case ENOENT: - case ENOTDIR: - case EACCES: - case ENAMETOOLONG: - break; - default: - /* Any negative value but -1 will inhibit - * futher path search. */ - return -2; - } - } - s += l; - } -} - -static int fixup_rpath(struct dso *p, char *buf, size_t buf_size) -{ - size_t n, l; - const char *s, *t, *origin; - char *d; - if (p->rpath || !p->rpath_orig) return 0; - if (!strchr(p->rpath_orig, '$')) { - p->rpath = p->rpath_orig; - return 0; - } - n = 0; - s = p->rpath_orig; - while ((t=strchr(s, '$'))) { - if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9)) - return 0; - s = t+1; - n++; - } - if (n > SSIZE_MAX/PATH_MAX) return 0; - - if (p->kernel_mapped) { - /* $ORIGIN searches cannot be performed for the main program - * when it is suid/sgid/AT_SECURE. This is because the - * pathname is under the control of the caller of execve. - * For libraries, however, $ORIGIN can be processed safely - * since the library's pathname came from a trusted source - * (either system paths or a call to dlopen). */ - if (libc.secure) - return 0; - l = readlink("/proc/self/exe", buf, buf_size); - if (l == -1) switch (errno) { - case ENOENT: - case ENOTDIR: - case EACCES: - break; - default: - return -1; - } - if (l >= buf_size) - return 0; - buf[l] = 0; - origin = buf; - } else { - origin = p->name; - } - t = strrchr(origin, '/'); - if (t) { - l = t-origin; - } else { - /* Normally p->name will always be an absolute or relative - * pathname containing at least one '/' character, but in the - * case where ldso was invoked as a command to execute a - * program in the working directory, app.name may not. Fix. */ - origin = "."; - l = 1; - } - /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */ - if (libc.secure && *origin != '/') - return 0; - p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1); - if (!p->rpath) return -1; - - d = p->rpath; - s = p->rpath_orig; - while ((t=strchr(s, '$'))) { - memcpy(d, s, t-s); - d += t-s; - memcpy(d, origin, l); - d += l; - /* It was determined previously that the '$' is followed - * either by "ORIGIN" or "{ORIGIN}". */ - s = t + 7 + 2*(t[1]=='{'); - } - strcpy(d, s); - return 0; -} - -static void decode_dyn(struct dso *p) -{ - size_t dyn[DYN_CNT]; - decode_vec(p->dynv, dyn, DYN_CNT); - p->syms = laddr(p, dyn[DT_SYMTAB]); - p->strings = laddr(p, dyn[DT_STRTAB]); - if (dyn[0]&(1<hashtab = laddr(p, dyn[DT_HASH]); - if (dyn[0]&(1<rpath_orig = p->strings + dyn[DT_RPATH]; - if (dyn[0]&(1<rpath_orig = p->strings + dyn[DT_RUNPATH]; - if (dyn[0]&(1<got = laddr(p, dyn[DT_PLTGOT]); - if (search_vec(p->dynv, dyn, DT_GNU_HASH)) - p->ghashtab = laddr(p, *dyn); - if (search_vec(p->dynv, dyn, DT_VERSYM)) - p->versym = laddr(p, *dyn); -} - -static size_t count_syms(struct dso *p) -{ - if (p->hashtab) return p->hashtab[1]; - - size_t nsym, i; - uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4); - uint32_t *hashval; - for (i = nsym = 0; i < p->ghashtab[0]; i++) { - if (buckets[i] > nsym) - nsym = buckets[i]; - } - if (nsym) { - hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]); - do nsym++; - while (!(*hashval++ & 1)); - } - return nsym; -} - -static void *dl_mmap(size_t n) -{ - void *p; - int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE; -#ifdef SYS_mmap2 - p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0); -#else - p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0); -#endif - return (unsigned long)p > -4096UL ? 0 : p; -} - -static void makefuncdescs(struct dso *p) -{ - static int self_done; - size_t nsym = count_syms(p); - size_t i, size = nsym * sizeof(*p->funcdescs); - - if (!self_done) { - p->funcdescs = dl_mmap(size); - self_done = 1; - } else { - p->funcdescs = malloc(size); - } - if (!p->funcdescs) { - if (!runtime) a_crash(); - error("Error allocating function descriptors for %s", p->name); - longjmp(*rtld_fail, 1); - } - for (i=0; isyms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) { - p->funcdescs[i].addr = laddr(p, p->syms[i].st_value); - p->funcdescs[i].got = p->got; - } else { - p->funcdescs[i].addr = 0; - p->funcdescs[i].got = 0; - } - } -} - -static struct dso *load_library(const char *name, struct dso *needed_by) -{ - char buf[2*NAME_MAX+2]; - char fullpath[2*NAME_MAX+2]; - const char *pathname; - unsigned char *map; - struct dso *p, temp_dso = {0}; - int fd; - struct stat st; - size_t alloc_size; - int n_th = 0; - int is_self = 0; - - if (!*name) { - errno = EINVAL; - return 0; - } - - /* Catch and block attempts to reload the implementation itself */ - if (name[0]=='l' && name[1]=='i' && name[2]=='b') { - static const char reserved[] = - "c.pthread.rt.m.dl.util.xnet."; - const char *rp, *next; - for (rp=reserved; *rp; rp=next) { - next = strchr(rp, '.') + 1; - if (strncmp(name+3, rp, next-rp) == 0) - break; - } - if (*rp) { - if (ldd_mode) { - /* Track which names have been resolved - * and only report each one once. */ - static unsigned reported; - unsigned mask = 1U<<(rp-reserved); - if (!(reported & mask)) { - reported |= mask; - dprintf(1, "\t%s => %s (%p)\n", - name, ldso.name, - ldso.base); - } - } - is_self = 1; - } - } - if (!strcmp(name, ldso.name)) is_self = 1; - if (is_self) { - if (!ldso.prev) { - tail->next = &ldso; - ldso.prev = tail; - tail = &ldso; - } - return &ldso; - } - if (strchr(name, '/')) { - pathname = name; - fd = open(name, O_RDONLY|O_CLOEXEC); - } else { - /* Search for the name to see if it's already loaded */ - for (p=head->next; p; p=p->next) { - if (p->shortname && !strcmp(p->shortname, name)) { - return p; - } - } - if (strlen(name) > NAME_MAX) return 0; - fd = -1; - if (env_path) fd = path_open(name, env_path, buf, sizeof buf); - for (p=needed_by; fd == -1 && p; p=p->needed_by) { - if (fixup_rpath(p, buf, sizeof buf) < 0) - fd = -2; /* Inhibit further search. */ - if (p->rpath) - fd = path_open(name, p->rpath, buf, sizeof buf); - } - if (fd == -1) { - if (!sys_path) { - char *prefix = 0; - size_t prefix_len; - if (ldso.name[0]=='/') { - char *s, *t, *z; - for (s=t=z=ldso.name; *s; s++) - if (*s=='/') z=t, t=s; - prefix_len = z-ldso.name; - if (prefix_len < PATH_MAX) - prefix = ldso.name; - } - if (!prefix) { - prefix = ""; - prefix_len = 0; - } - char etc_ldso_path[prefix_len + 1 - + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"]; - snprintf(etc_ldso_path, sizeof etc_ldso_path, - "%.*s/etc/ld-musl-" LDSO_ARCH ".path", - (int)prefix_len, prefix); - fd = open(etc_ldso_path, O_RDONLY|O_CLOEXEC); - if (fd>=0) { - size_t n = 0; - if (!fstat(fd, &st)) n = st.st_size; - if ((sys_path = malloc(n+1))) - sys_path[n] = 0; - if (!sys_path || read_loop(fd, sys_path, n)<0) { - free(sys_path); - sys_path = ""; - } - close(fd); - } else if (errno != ENOENT) { - sys_path = ""; - } - } - if (!sys_path || sys_path[0] == 0) sys_path = "/usr/lib:/lib:/usr/local/lib"; - fd = path_open(name, sys_path, buf, sizeof buf); - } - pathname = buf; - } - if (fd < 0) return 0; - - if (pathname[0] != '/') { - if (!realpath(pathname, fullpath)) { - close(fd); - return 0; - } - pathname = fullpath; - } - - for (p=head->next; p; p=p->next) { - if (!strcmp(p->name, pathname)) { - /* If this library was previously loaded with a - * pathname but a search found the same inode, - * setup its shortname so it can be found by name. */ - if (!p->shortname && pathname != name) - p->shortname = strrchr(p->name, '/')+1; - close(fd); - return p; - } - } - map = noload ? 0 : map_library(fd, &temp_dso); - close(fd); - if (!map) return 0; - - /* Avoid the danger of getting two versions of libc mapped into the - * same process when an absolute pathname was used. The symbols - * checked are chosen to catch both musl and glibc, and to avoid - * false positives from interposition-hack libraries. */ - decode_dyn(&temp_dso); - if (find_sym(&temp_dso, "__libc_start_main", 1).sym && - find_sym(&temp_dso, "stdin", 1).sym) { - unmap_library(&temp_dso); - return load_library("libc.so", needed_by); - } - /* Past this point, if we haven't reached runtime yet, ldso has - * committed either to use the mapped library or to abort execution. - * Unmapping is not possible, so we can safely reclaim gaps. */ - if (!runtime) reclaim_gaps(&temp_dso); - - /* Allocate storage for the new DSO. When there is TLS, this - * storage must include a reservation for all pre-existing - * threads to obtain copies of both the new TLS, and an - * extended DTV capable of storing an additional slot for - * the newly-loaded DSO. */ - alloc_size = sizeof *p + strlen(pathname) + 1; - if (runtime && temp_dso.tls.image) { - size_t per_th = temp_dso.tls.size + temp_dso.tls.align - + sizeof(void *) * (tls_cnt+3); - n_th = libc.threads_minus_1 + 1; - if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX; - else alloc_size += n_th * per_th; - } - p = calloc(1, alloc_size); - if (!p) { - unmap_library(&temp_dso); - return 0; - } - memcpy(p, &temp_dso, sizeof temp_dso); - p->needed_by = needed_by; - p->name = p->buf; - p->runtime_loaded = runtime; - strcpy(p->name, pathname); - /* Add a shortname only if name arg was not an explicit pathname. */ - if (pathname != name) p->shortname = strrchr(p->name, '/')+1; - if (p->tls.image) { - p->tls_id = ++tls_cnt; - tls_align = MAXP2(tls_align, p->tls.align); -#ifdef TLS_ABOVE_TP - p->tls.offset = tls_offset + ( (p->tls.align-1) & - (-tls_offset + (uintptr_t)p->tls.image) ); - tls_offset = p->tls.offset + p->tls.size; -#else - tls_offset += p->tls.size + p->tls.align - 1; - tls_offset -= (tls_offset + (uintptr_t)p->tls.image) - & (p->tls.align-1); - p->tls.offset = tls_offset; -#endif - p->new_dtv = (void *)(-sizeof(size_t) & - (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t))); - p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1)); - if (tls_tail) tls_tail->next = &p->tls; - else libc.tls_head = &p->tls; - tls_tail = &p->tls; - } - - tail->next = p; - p->prev = tail; - tail = p; - - if (DL_FDPIC) makefuncdescs(p); - - if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); - - return p; -} - -static void load_direct_deps(struct dso *p) -{ - size_t i, cnt=0; - - if (p->deps) return; - /* For head, all preloads are direct pseudo-dependencies. - * Count and include them now to avoid realloc later. */ - if (p==head) for (struct dso *q=p->next; q; q=q->next) - cnt++; - for (i=0; p->dynv[i]; i+=2) - if (p->dynv[i] == DT_NEEDED) cnt++; - /* Use builtin buffer for apps with no external deps, to - * preserve property of no runtime failure paths. */ - p->deps = (p==head && cnt<2) ? builtin_deps : - calloc(cnt+1, sizeof *p->deps); - if (!p->deps) { - error("Error loading dependencies for %s", p->name); - if (runtime) longjmp(*rtld_fail, 1); - } - cnt=0; - if (p==head) for (struct dso *q=p->next; q; q=q->next) - p->deps[cnt++] = q; - for (i=0; p->dynv[i]; i+=2) { - if (p->dynv[i] != DT_NEEDED) continue; - struct dso *dep = load_library(p->strings + p->dynv[i+1], p); - if (!dep) { - error("Error loading shared library %s: %m (needed by %s)", - p->strings + p->dynv[i+1], p->name); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - p->deps[cnt++] = dep; - } - p->deps[cnt] = 0; - p->ndeps_direct = cnt; -} - -static void load_deps(struct dso *p) -{ - if (p->deps) return; - for (; p; p=p->next) - load_direct_deps(p); -} - -static void extend_bfs_deps(struct dso *p) -{ - size_t i, j, cnt, ndeps_all; - struct dso **tmp; - - /* Can't use realloc if the original p->deps was allocated at - * program entry and malloc has been replaced, or if it's - * the builtin non-allocated trivial main program deps array. */ - int no_realloc = (__malloc_replaced && !p->runtime_loaded) - || p->deps == builtin_deps; - - if (p->bfs_built) return; - ndeps_all = p->ndeps_direct; - - /* Mark existing (direct) deps so they won't be duplicated. */ - for (i=0; p->deps[i]; i++) - p->deps[i]->mark = 1; - - /* For each dependency already in the list, copy its list of direct - * dependencies to the list, excluding any items already in the - * list. Note that the list this loop iterates over will grow during - * the loop, but since duplicates are excluded, growth is bounded. */ - for (i=0; p->deps[i]; i++) { - struct dso *dep = p->deps[i]; - for (j=cnt=0; jndeps_direct; j++) - if (!dep->deps[j]->mark) cnt++; - tmp = no_realloc ? - malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) : - realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1)); - if (!tmp) { - error("Error recording dependencies for %s", p->name); - if (runtime) longjmp(*rtld_fail, 1); - continue; - } - if (no_realloc) { - memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1)); - no_realloc = 0; - } - p->deps = tmp; - for (j=0; jndeps_direct; j++) { - if (dep->deps[j]->mark) continue; - dep->deps[j]->mark = 1; - p->deps[ndeps_all++] = dep->deps[j]; - } - p->deps[ndeps_all] = 0; - } - p->bfs_built = 1; - for (p=head; p; p=p->next) - p->mark = 0; -} - -static void load_preload(char *s) -{ - int tmp; - char *z; - for (z=s; *z; s=z) { - for ( ; *s && (isspace(*s) || *s==':'); s++); - for (z=s; *z && !isspace(*z) && *z!=':'; z++); - tmp = *z; - *z = 0; - load_library(s, 0); - *z = tmp; - } -} - -static void add_syms(struct dso *p) -{ - if (!p->syms_next && syms_tail != p) { - syms_tail->syms_next = p; - syms_tail = p; - } -} - -static void revert_syms(struct dso *old_tail) -{ - struct dso *p, *next; - /* Chop off the tail of the list of dsos that participate in - * the global symbol table, reverting them to RTLD_LOCAL. */ - for (p=old_tail; p; p=next) { - next = p->syms_next; - p->syms_next = 0; - } - syms_tail = old_tail; -} - -static void do_mips_relocs(struct dso *p, size_t *got) -{ - size_t i, j, rel[2]; - unsigned char *base = p->base; - i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO); - if (p==&ldso) { - got += i; - } else { - while (i--) *got++ += (size_t)base; - } - j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM); - i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); - Sym *sym = p->syms + j; - rel[0] = (unsigned char *)got - base; - for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) { - rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT); - do_relocs(p, rel, sizeof rel, 2); - } -} - -static void reloc_all(struct dso *p) -{ - size_t dyn[DYN_CNT]; - for (; p; p=p->next) { - if (p->relocated) continue; - decode_vec(p->dynv, dyn, DYN_CNT); - if (NEED_MIPS_GOT_RELOCS) - do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT])); - do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ], - 2+(dyn[DT_PLTREL]==DT_RELA)); - do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2); - do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3); - - if (head != &ldso && p->relro_start != p->relro_end && - mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ) - && errno != ENOSYS) { - error("Error relocating %s: RELRO protection failed: %m", - p->name); - if (runtime) longjmp(*rtld_fail, 1); - } - - p->relocated = 1; - } -} - -static void kernel_mapped_dso(struct dso *p) -{ - size_t min_addr = -1, max_addr = 0, cnt; - Phdr *ph = p->phdr; - for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) { - if (ph->p_type == PT_DYNAMIC) { - p->dynv = laddr(p, ph->p_vaddr); - } else if (ph->p_type == PT_GNU_RELRO) { - p->relro_start = ph->p_vaddr & -PAGE_SIZE; - p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; - } else if (ph->p_type == PT_GNU_STACK) { - if (!runtime && ph->p_memsz > __default_stacksize) { - __default_stacksize = - ph->p_memsz < DEFAULT_STACK_MAX ? - ph->p_memsz : DEFAULT_STACK_MAX; - } - } - if (ph->p_type != PT_LOAD) continue; - if (ph->p_vaddr < min_addr) - min_addr = ph->p_vaddr; - if (ph->p_vaddr+ph->p_memsz > max_addr) - max_addr = ph->p_vaddr+ph->p_memsz; - } - min_addr &= -PAGE_SIZE; - max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE; - p->map = p->base + min_addr; - p->map_len = max_addr - min_addr; - p->kernel_mapped = 1; -} - -void __libc_exit_fini() -{ - struct dso *p; - size_t dyn[DYN_CNT]; - pthread_t self = __pthread_self(); - - /* Take both locks before setting shutting_down, so that - * either lock is sufficient to read its value. The lock - * order matches that in dlopen to avoid deadlock. */ - pthread_rwlock_wrlock(&lock); - pthread_mutex_lock(&init_fini_lock); - shutting_down = 1; - pthread_rwlock_unlock(&lock); - for (p=fini_head; p; p=p->fini_next) { - while (p->ctor_visitor && p->ctor_visitor!=self) - pthread_cond_wait(&ctor_cond, &init_fini_lock); - if (!p->constructed) continue; - decode_vec(p->dynv, dyn, DYN_CNT); - if (dyn[0] & (1<bfs_built) { - for (cnt=0; dso->deps[cnt]; cnt++) - dso->deps[cnt]->mark = 0; - cnt++; /* self, not included in deps */ - } else { - for (cnt=0, p=head; p; cnt++, p=p->next) - p->mark = 0; - } - cnt++; /* termination slot */ - if (dso==head && cnt <= countof(builtin_ctor_queue)) - queue = builtin_ctor_queue; - else - queue = calloc(cnt, sizeof *queue); - - if (!queue) { - error("Error allocating constructor queue: %m\n"); - if (runtime) longjmp(*rtld_fail, 1); - return 0; - } - - /* Opposite ends of the allocated buffer serve as an output queue - * and a working stack. Setup initial stack with just the argument - * dso and initial queue empty... */ - stack = queue; - qpos = 0; - spos = cnt; - stack[--spos] = dso; - dso->next_dep = 0; - dso->mark = 1; - - /* Then perform pseudo-DFS sort, but ignoring circular deps. */ - while (sposnext_dep < p->ndeps_direct) { - if (p->deps[p->next_dep]->mark) { - p->next_dep++; - } else { - stack[--spos] = p; - p = p->deps[p->next_dep]; - p->next_dep = 0; - p->mark = 1; - } - } - queue[qpos++] = p; - } - queue[qpos] = 0; - for (i=0; imark = 0; - for (i=0; ictor_visitor && queue[i]->ctor_visitor->tid < 0) { - error("State of %s is inconsistent due to multithreaded fork\n", - queue[i]->name); - free(queue); - if (runtime) longjmp(*rtld_fail, 1); - } - - return queue; -} - -static void do_init_fini(struct dso **queue) -{ - struct dso *p; - size_t dyn[DYN_CNT], i; - pthread_t self = __pthread_self(); - - pthread_mutex_lock(&init_fini_lock); - for (i=0; (p=queue[i]); i++) { - while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down) - pthread_cond_wait(&ctor_cond, &init_fini_lock); - if (p->ctor_visitor || p->constructed) - continue; - p->ctor_visitor = self; - - decode_vec(p->dynv, dyn, DYN_CNT); - if (dyn[0] & ((1<fini_next = fini_head; - fini_head = p; - } - - pthread_mutex_unlock(&init_fini_lock); - -#ifndef NO_LEGACY_INITFINI - if ((dyn[0] & (1<ctor_visitor = 0; - p->constructed = 1; - pthread_cond_broadcast(&ctor_cond); - } - pthread_mutex_unlock(&init_fini_lock); -} - -void __libc_start_init(void) -{ - do_init_fini(main_ctor_queue); - if (!__malloc_replaced && main_ctor_queue != builtin_ctor_queue) - free(main_ctor_queue); - main_ctor_queue = 0; -} - -static void dl_debug_state(void) -{ -} - -weak_alias(dl_debug_state, _dl_debug_state); - -void __init_tls(size_t *auxv) -{ -} - -static void update_tls_size() -{ - libc.tls_cnt = tls_cnt; - libc.tls_align = tls_align; - libc.tls_size = ALIGN( - (1+tls_cnt) * sizeof(void *) + - tls_offset + - sizeof(struct pthread) + - tls_align * 2, - tls_align); -} - -static void install_new_tls(void) -{ - sigset_t set; - pthread_t self = __pthread_self(), td; - struct dso *dtv_provider = container_of(tls_tail, struct dso, tls); - uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv; - struct dso *p; - size_t i, j; - size_t old_cnt = self->dtv[0]; - - __block_app_sigs(&set); - __tl_lock(); - /* Copy existing dtv contents from all existing threads. */ - for (i=0, td=self; !i || td!=self; i++, td=td->next) { - memcpy(newdtv+i, td->dtv, - (old_cnt+1)*sizeof(uintptr_t)); - newdtv[i][0] = tls_cnt; - } - /* Install new dtls into the enlarged, uninstalled dtv copies. */ - for (p=head; ; p=p->next) { - if (p->tls_id <= old_cnt) continue; - unsigned char *mem = p->new_tls; - for (j=0; jtls.image - (uintptr_t)mem) - & (p->tls.align-1); - memcpy(new, p->tls.image, p->tls.len); - newdtv[j][p->tls_id] = - (uintptr_t)new + DTP_OFFSET; - mem += p->tls.size + p->tls.align; - } - if (p->tls_id == tls_cnt) break; - } - - /* Broadcast barrier to ensure contents of new dtv is visible - * if the new dtv pointer is. The __membarrier function has a - * fallback emulation using signals for kernels that lack the - * feature at the syscall level. */ - - __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0); - - /* Install new dtv for each thread. */ - for (j=0, td=self; !j || td!=self; j++, td=td->next) { - td->dtv = newdtv[j]; - } - - __tl_unlock(); - __restore_sigs(&set); -} - -/* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the - * following stage 2 and stage 3 functions via primitive symbolic lookup - * since it does not have access to their addresses to begin with. */ - -/* Stage 2 of the dynamic linker is called after relative relocations - * have been processed. It can make function calls to static functions - * and access string literals and static data, but cannot use extern - * symbols. Its job is to perform symbolic relocations on the dynamic - * linker itself, but some of the relocations performed may need to be - * replaced later due to copy relocations in the main program. */ - -hidden void __dls2(unsigned char *base, size_t *sp) -{ - size_t *auxv; - for (auxv=sp+1+*sp+1; *auxv; auxv++); - auxv++; - if (DL_FDPIC) { - void *p1 = (void *)sp[-2]; - void *p2 = (void *)sp[-1]; - if (!p1) { - size_t aux[AUX_CNT]; - decode_vec(auxv, aux, AUX_CNT); - if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE]; - else ldso.base = (void *)(aux[AT_PHDR] & -4096); - } - app_loadmap = p2 ? p1 : 0; - ldso.loadmap = p2 ? p2 : p1; - ldso.base = laddr(&ldso, 0); - } else { - ldso.base = base; - } - Ehdr *ehdr = (void *)ldso.base; - ldso.name = ldso.shortname = "libc.so"; - ldso.phnum = ehdr->e_phnum; - ldso.phdr = laddr(&ldso, ehdr->e_phoff); - ldso.phentsize = ehdr->e_phentsize; - kernel_mapped_dso(&ldso); - decode_dyn(&ldso); - - if (DL_FDPIC) makefuncdescs(&ldso); - - /* Prepare storage for to save clobbered REL addends so they - * can be reused in stage 3. There should be very few. If - * something goes wrong and there are a huge number, abort - * instead of risking stack overflow. */ - size_t dyn[DYN_CNT]; - decode_vec(ldso.dynv, dyn, DYN_CNT); - size_t *rel = laddr(&ldso, dyn[DT_REL]); - size_t rel_size = dyn[DT_RELSZ]; - size_t symbolic_rel_cnt = 0; - apply_addends_to = rel; - for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) - if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++; - if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash(); - size_t addends[symbolic_rel_cnt+1]; - saved_addends = addends; - - head = &ldso; - reloc_all(&ldso); - - ldso.relocated = 0; - - /* Call dynamic linker stage-2b, __dls2b, looking it up - * symbolically as a barrier against moving the address - * load across the above relocation processing. */ - struct symdef dls2b_def = find_sym(&ldso, "__dls2b", 0); - if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp, auxv); - else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp, auxv); -} - -/* Stage 2b sets up a valid thread pointer, which requires relocations - * completed in stage 2, and on which stage 3 is permitted to depend. - * This is done as a separate stage, with symbolic lookup as a barrier, - * so that loads of the thread pointer and &errno can be pure/const and - * thereby hoistable. */ - -void __dls2b(size_t *sp, size_t *auxv) -{ - /* Setup early thread pointer in builtin_tls for ldso/libc itself to - * use during dynamic linking. If possible it will also serve as the - * thread pointer at runtime. */ - search_vec(auxv, &__hwcap, AT_HWCAP); - libc.auxv = auxv; - libc.tls_size = sizeof builtin_tls; - libc.tls_align = tls_align; - if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) { - a_crash(); - } - - struct symdef dls3_def = find_sym(&ldso, "__dls3", 0); - if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp, auxv); - else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp, auxv); -} - -/* Stage 3 of the dynamic linker is called with the dynamic linker/libc - * fully functional. Its job is to load (if not already loaded) and - * process dependencies and relocations for the main application and - * transfer control to its entry point. */ - -void __dls3(size_t *sp, size_t *auxv) -{ - static struct dso app, vdso; - size_t aux[AUX_CNT]; - size_t i; - char *env_preload=0; - char *replace_argv0=0; - size_t vdso_base; - int argc = *sp; - char **argv = (void *)(sp+1); - char **argv_orig = argv; - char **envp = argv+argc+1; - - /* Find aux vector just past environ[] and use it to initialize - * global data that may be needed before we can make syscalls. */ - __environ = envp; - decode_vec(auxv, aux, AUX_CNT); - search_vec(auxv, &__sysinfo, AT_SYSINFO); - __pthread_self()->sysinfo = __sysinfo; - libc.page_size = aux[AT_PAGESZ]; - libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID] - || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]); - - /* Only trust user/env if kernel says we're not suid/sgid */ - if (!libc.secure) { - env_path = getenv("LD_LIBRARY_PATH"); - env_preload = getenv("LD_PRELOAD"); - } - - /* If the main program was already loaded by the kernel, - * AT_PHDR will point to some location other than the dynamic - * linker's program headers. */ - if (aux[AT_PHDR] != (size_t)ldso.phdr) { - size_t interp_off = 0; - size_t tls_image = 0; - /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */ - Phdr *phdr = app.phdr = (void *)aux[AT_PHDR]; - app.phnum = aux[AT_PHNUM]; - app.phentsize = aux[AT_PHENT]; - for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) { - if (phdr->p_type == PT_PHDR) - app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr); - else if (phdr->p_type == PT_INTERP) - interp_off = (size_t)phdr->p_vaddr; - else if (phdr->p_type == PT_TLS) { - tls_image = phdr->p_vaddr; - app.tls.len = phdr->p_filesz; - app.tls.size = phdr->p_memsz; - app.tls.align = phdr->p_align; - } - } - if (DL_FDPIC) app.loadmap = app_loadmap; - if (app.tls.size) app.tls.image = laddr(&app, tls_image); - if (interp_off) ldso.name = "/lib/libc.so"; -#if 0 - if ((aux[0] & (1UL<= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1; - argv++; - while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') { - char *opt = argv[0]+2; - *argv++ = (void *)-1; - if (!*opt) { - break; - } else if (!memcmp(opt, "list", 5)) { - ldd_mode = 1; - } else if (!memcmp(opt, "library-path", 12)) { - if (opt[12]=='=') env_path = opt+13; - else if (opt[12]) *argv = 0; - else if (*argv) env_path = *argv++; - } else if (!memcmp(opt, "preload", 7)) { - if (opt[7]=='=') env_preload = opt+8; - else if (opt[7]) *argv = 0; - else if (*argv) env_preload = *argv++; - } else if (!memcmp(opt, "argv0", 5)) { - if (opt[5]=='=') replace_argv0 = opt+6; - else if (opt[5]) *argv = 0; - else if (*argv) replace_argv0 = *argv++; - } else { - argv[0] = 0; - } - } - argv[-1] = (void *)(argc - (argv-argv_orig)); - if (!argv[0]) { - dprintf(2, "musl libc (" LDSO_ARCH ")\n" - "Version %s\n" - "Dynamic Program Loader\n" - "Usage: %s [options] [--] pathname%s\n", - __libc_version, ldname, - ldd_mode ? "" : " [args]"); - _exit(1); - } - fd = open(argv[0], O_RDONLY); - if (fd < 0) { - dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno)); - _exit(1); - } - Ehdr *ehdr = map_library(fd, &app); - if (!ehdr) { - dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]); - _exit(1); - } - close(fd); - ldso.name = ldname; - app.name = argv[0]; - aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry); - /* Find the name that would have been used for the dynamic - * linker had ldd not taken its place. */ - if (ldd_mode) { - for (i=0; insegs = 1; - app.loadmap->segs[0].addr = (size_t)app.map; - app.loadmap->segs[0].p_vaddr = (size_t)app.map - - (size_t)app.base; - app.loadmap->segs[0].p_memsz = app.map_len; - } - argv[-3] = (void *)app.loadmap; - } - - /* Initial dso chain consists only of the app. */ - head = tail = syms_tail = &app; - - /* Donate unused parts of app and library mapping to malloc */ - reclaim_gaps(&app); - reclaim_gaps(&ldso); - - /* Load preload/needed libraries, add symbols to global namespace. */ - ldso.deps = (struct dso **)no_deps; - if (env_preload) load_preload(env_preload); - load_deps(&app); - for (struct dso *p=head; p; p=p->next) - add_syms(p); - - /* Attach to vdso, if provided by the kernel, last so that it does - * not become part of the global namespace. */ - if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR) && vdso_base) { - Ehdr *ehdr = (void *)vdso_base; - Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff); - vdso.phnum = ehdr->e_phnum; - vdso.phentsize = ehdr->e_phentsize; - for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) { - if (phdr->p_type == PT_DYNAMIC) - vdso.dynv = (void *)(vdso_base + phdr->p_offset); - if (phdr->p_type == PT_LOAD) - vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset); - } - vdso.name = ""; - vdso.shortname = "OHOS-vdso.so"; - vdso.relocated = 1; - vdso.deps = (struct dso **)no_deps; - decode_dyn(&vdso); - vdso.prev = tail; - tail->next = &vdso; - tail = &vdso; - } - - for (i=0; app.dynv[i]; i+=2) { - if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG) - app.dynv[i+1] = (size_t)&debug; - if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) { - size_t *ptr = (size_t *) app.dynv[i+1]; - *ptr = (size_t)&debug; - } - } - - /* This must be done before final relocations, since it calls - * malloc, which may be provided by the application. Calling any - * application code prior to the jump to its entry point is not - * valid in our model and does not work with FDPIC, where there - * are additional relocation-like fixups that only the entry point - * code can see to perform. */ - main_ctor_queue = queue_ctors(&app); - - /* Initial TLS must also be allocated before final relocations - * might result in calloc being a call to application code. */ - update_tls_size(); - void *initial_tls = builtin_tls; - if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) { - initial_tls = calloc(libc.tls_size, 1); - if (!initial_tls) { - dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n", - argv[0], libc.tls_size); - _exit(127); - } - } - static_tls_cnt = tls_cnt; - - /* The main program must be relocated LAST since it may contain - * copy relocations which depend on libraries' relocations. */ - reloc_all(app.next); - reloc_all(&app); - - /* Actual copying to new TLS needs to happen after relocations, - * since the TLS images might have contained relocated addresses. */ - if (initial_tls != builtin_tls) { - if (__init_tp(__copy_tls(initial_tls)) < 0) { - a_crash(); - } - } else { - size_t tmp_tls_size = libc.tls_size; - pthread_t self = __pthread_self(); - /* Temporarily set the tls size to the full size of - * builtin_tls so that __copy_tls will use the same layout - * as it did for before. Then check, just to be safe. */ - libc.tls_size = sizeof builtin_tls; - if (__copy_tls((void*)builtin_tls) != self) a_crash(); - libc.tls_size = tmp_tls_size; - } - - if (ldso_fail) _exit(127); - if (ldd_mode) _exit(0); - - /* Determine if malloc was interposed by a replacement implementation - * so that calloc and the memalign family can harden against the - * possibility of incomplete replacement. */ - if (find_sym(head, "malloc", 1).dso != &ldso) - __malloc_replaced = 1; - if (find_sym(head, "aligned_alloc", 1).dso != &ldso) - __aligned_alloc_replaced = 1; - - /* Switch to runtime mode: any further failures in the dynamic - * linker are a reportable failure rather than a fatal startup - * error. */ - runtime = 1; - - debug.ver = 1; - debug.bp = dl_debug_state; - debug.head = head; - debug.base = ldso.base; - debug.state = RT_CONSISTENT; - _dl_debug_state(); - - if (replace_argv0) argv[0] = replace_argv0; - - errno = 0; - - CRTJMP((void *)aux[AT_ENTRY], argv-1); - for(;;); -} - -static void prepare_lazy(struct dso *p) -{ - size_t dyn[DYN_CNT], n, flags1=0; - decode_vec(p->dynv, dyn, DYN_CNT); - search_vec(p->dynv, &flags1, DT_FLAGS_1); - if (dyn[DT_BIND_NOW] || (dyn[DT_FLAGS] & DF_BIND_NOW) || (flags1 & DF_1_NOW)) - return; - n = dyn[DT_RELSZ]/2 + dyn[DT_RELASZ]/3 + dyn[DT_PLTRELSZ]/2 + 1; - if (NEED_MIPS_GOT_RELOCS) { - size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM); - size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); - n += i-j; - } - p->lazy = calloc(n, 3*sizeof(size_t)); - if (!p->lazy) { - error("Error preparing lazy relocation for %s: %m", p->name); - longjmp(*rtld_fail, 1); - } - p->lazy_next = lazy_head; - lazy_head = p; -} - -void *dlopen(const char *file, int mode) -{ - struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next; - struct tls_module *orig_tls_tail; - size_t orig_tls_cnt, orig_tls_offset, orig_tls_align; - size_t i; - int cs; - jmp_buf jb; - struct dso **volatile ctor_queue = 0; - - if (mode & ~(RTLD_LAZY | RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL | RTLD_LOCAL | RTLD_NODELETE)) { - error("invalid mode parameter for dlopen()."); - return NULL; - } - - if ((mode & (RTLD_LAZY | RTLD_NOW)) == 0) { - error("invalid mode, one of RTLD_LAZY and RTLD_NOW must be set."); - return NULL; - } - - if (!file) return head; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - pthread_rwlock_wrlock(&lock); - __inhibit_ptc(); - - debug.state = RT_ADD; - _dl_debug_state(); - - p = 0; - if (shutting_down) { - error("Cannot dlopen while program is exiting."); - goto end; - } - orig_tls_tail = tls_tail; - orig_tls_cnt = tls_cnt; - orig_tls_offset = tls_offset; - orig_tls_align = tls_align; - orig_lazy_head = lazy_head; - orig_syms_tail = syms_tail; - orig_tail = tail; - noload = mode & RTLD_NOLOAD; - - rtld_fail = &jb; - if (setjmp(*rtld_fail)) { - /* Clean up anything new that was (partially) loaded */ - revert_syms(orig_syms_tail); - for (p=orig_tail->next; p; p=next) { - next = p->next; - while (p->td_index) { - void *tmp = p->td_index->next; - free(p->td_index); - p->td_index = tmp; - } - free(p->funcdescs); - if (p->rpath != p->rpath_orig) - free(p->rpath); - free(p->deps); - unmap_library(p); - free(p); - } - free(ctor_queue); - ctor_queue = 0; - if (!orig_tls_tail) libc.tls_head = 0; - tls_tail = orig_tls_tail; - if (tls_tail) tls_tail->next = 0; - tls_cnt = orig_tls_cnt; - tls_offset = orig_tls_offset; - tls_align = orig_tls_align; - lazy_head = orig_lazy_head; - tail = orig_tail; - tail->next = 0; - p = 0; - goto end; - } else p = load_library(file, head); - - if (!p) { - error(noload ? - "Library %s is not already loaded" : - "Error loading shared library %s: %m", - file); - goto end; - } - - /* First load handling */ - load_deps(p); - extend_bfs_deps(p); - pthread_mutex_lock(&init_fini_lock); - int constructed = p->constructed; - pthread_mutex_unlock(&init_fini_lock); - if (!constructed) ctor_queue = queue_ctors(p); - if (!p->relocated && (mode & RTLD_LAZY)) { - prepare_lazy(p); - for (i=0; p->deps[i]; i++) - if (!p->deps[i]->relocated) - prepare_lazy(p->deps[i]); - } - if (!p->relocated || (mode & RTLD_GLOBAL)) { - /* Make new symbols global, at least temporarily, so we can do - * relocations. If not RTLD_GLOBAL, this is reverted below. */ - add_syms(p); - for (i=0; p->deps[i]; i++) - add_syms(p->deps[i]); - } - if (!p->relocated) { - reloc_all(p); - } - - /* If RTLD_GLOBAL was not specified, undo any new additions - * to the global symbol table. This is a nop if the library was - * previously loaded and already global. */ - if (!(mode & RTLD_GLOBAL)) - revert_syms(orig_syms_tail); - - /* Processing of deferred lazy relocations must not happen until - * the new libraries are committed; otherwise we could end up with - * relocations resolved to symbol definitions that get removed. */ - redo_lazy_relocs(); - - update_tls_size(); - if (tls_cnt != orig_tls_cnt) - install_new_tls(); - orig_tail = tail; -end: - debug.state = RT_CONSISTENT; - _dl_debug_state(); - __release_ptc(); - if (p) gencnt++; - pthread_rwlock_unlock(&lock); - if (ctor_queue) { - do_init_fini(ctor_queue); - free(ctor_queue); - } - pthread_setcancelstate(cs, 0); - return p; -} - -hidden int __dl_invalid_handle(void *h) -{ - struct dso *p; - for (p=head; p; p=p->next) if (h==p) return 0; - error("Invalid library handle %p", (void *)h); - return 1; -} - -static void *addr2dso(size_t a) -{ - struct dso *p; - size_t i; - if (DL_FDPIC) for (p=head; p; p=p->next) { - i = count_syms(p); - if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs)) - return p; - } - for (p=head; p; p=p->next) { - if (DL_FDPIC && p->loadmap) { - for (i=0; iloadmap->nsegs; i++) { - if (a-p->loadmap->segs[i].p_vaddr - < p->loadmap->segs[i].p_memsz) - return p; - } - } else { - Phdr *ph = p->phdr; - size_t phcnt = p->phnum; - size_t entsz = p->phentsize; - size_t base = (size_t)p->base; - for (; phcnt--; ph=(void *)((char *)ph+entsz)) { - if (ph->p_type != PT_LOAD) continue; - if (a-base-ph->p_vaddr < ph->p_memsz) - return p; - } - if (a-(size_t)p->map < p->map_len) - return 0; - } - } - return 0; -} - -static void *do_dlsym(struct dso *p, const char *s, void *ra) -{ - int use_deps = 0; - if (p == head || p == RTLD_DEFAULT) { - p = head; - } else if (p == RTLD_NEXT) { - p = addr2dso((size_t)ra); - if (!p) p=head; - p = p->next; - } else if (__dl_invalid_handle(p)) { - return 0; - } else - use_deps = 1; - struct symdef def = find_sym2(p, s, 0, use_deps); - if (!def.sym) { - error("Symbol not found: %s", s); - return 0; - } - if ((def.sym->st_info&0xf) == STT_TLS) - return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET}); - if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC) - return def.dso->funcdescs + (def.sym - def.dso->syms); - return laddr(def.dso, def.sym->st_value); -} - -int dladdr(const void *addr_arg, Dl_info *info) -{ - size_t addr = (size_t)addr_arg; - struct dso *p; - Sym *sym, *bestsym; - uint32_t nsym; - char *strings; - size_t best = 0; - size_t besterr = -1; - - pthread_rwlock_rdlock(&lock); - p = addr2dso(addr); - pthread_rwlock_unlock(&lock); - - if (!p) return 0; - - sym = p->syms; - strings = p->strings; - nsym = count_syms(p); - - if (DL_FDPIC) { - size_t idx = (addr-(size_t)p->funcdescs) - / sizeof(*p->funcdescs); - if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) { - best = (size_t)(p->funcdescs + idx); - bestsym = sym + idx; - besterr = 0; - } - } - - if (!best) for (; nsym; nsym--, sym++) { - if (sym->st_value - && (1<<(sym->st_info&0xf) & OK_TYPES) - && (1<<(sym->st_info>>4) & OK_BINDS)) { - size_t symaddr = (size_t)laddr(p, sym->st_value); - if (symaddr > addr || symaddr <= best) - continue; - best = symaddr; - bestsym = sym; - besterr = addr - symaddr; - if (addr == symaddr) - break; - } - } - - if (best && besterr > bestsym->st_size-1) { - best = 0; - bestsym = 0; - } - - info->dli_fname = p->name; - info->dli_fbase = p->map; - - if (!best) { - info->dli_sname = 0; - info->dli_saddr = 0; - return 1; - } - - if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC) - best = (size_t)(p->funcdescs + (bestsym - p->syms)); - info->dli_sname = strings + bestsym->st_name; - info->dli_saddr = (void *)best; - - return 1; -} - -hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra) -{ - void *res; - pthread_rwlock_rdlock(&lock); - res = do_dlsym(p, s, ra); - pthread_rwlock_unlock(&lock); - return res; -} - -hidden void *__dlsym_redir_time64(void *restrict p, const char *restrict s, void *restrict ra) -{ -#if _REDIR_TIME64 - const char *suffix, *suffix2 = ""; - char redir[36]; - - /* Map the symbol name to a time64 version of itself according to the - * pattern used for naming the redirected time64 symbols. */ - size_t l = strnlen(s, sizeof redir); - if (l<4 || l==sizeof redir) goto no_redir; - if (s[l-2]=='_' && s[l-1]=='r') { - l -= 2; - suffix2 = s+l; - } - if (l<4) goto no_redir; - if (!strcmp(s+l-4, "time")) suffix = "64"; - else suffix = "_time64"; - - /* Use the presence of the remapped symbol name in libc to determine - * whether it's one that requires time64 redirection; replace if so. */ - snprintf(redir, sizeof redir, "__%.*s%s%s", (int)l, s, suffix, suffix2); - if (find_sym(&ldso, redir, 1).sym) s = redir; -no_redir: -#endif - return __dlsym(p, s, ra); -} - -int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data) -{ - struct dso *current; - struct dl_phdr_info info; - int ret = 0; - for(current = head; current;) { - info.dlpi_addr = (uintptr_t)current->base; - info.dlpi_name = current->name; - info.dlpi_phdr = current->phdr; - info.dlpi_phnum = current->phnum; - info.dlpi_adds = gencnt; - info.dlpi_subs = 0; - info.dlpi_tls_modid = current->tls_id; - info.dlpi_tls_data = !current->tls_id ? 0 : - __tls_get_addr((tls_mod_off_t[]){current->tls_id,0}); - - ret = (callback)(&info, sizeof (info), data); - - if (ret != 0) break; - - pthread_rwlock_rdlock(&lock); - current = current->next; - pthread_rwlock_unlock(&lock); - } - return ret; -} - -static void error(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - if (!runtime) { - vdprintf(2, fmt, ap); - dprintf(2, "\n"); - ldso_fail = 1; - va_end(ap); - return; - } - __dl_vseterr(fmt, ap); - va_end(ap); -} diff --git a/porting/liteos_a/user_debug/src/env/__libc_start_main.c b/porting/liteos_a/user_debug/src/env/__libc_start_main.c deleted file mode 100644 index f78571838..000000000 --- a/porting/liteos_a/user_debug/src/env/__libc_start_main.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include -#include -#include -#include -#include "syscall.h" -#include "atomic.h" -#include "libc.h" - -static void dummy(void) {} -weak_alias(dummy, _init); - -extern weak hidden void (*const __init_array_start)(void), (*const __init_array_end)(void); -extern void parse_argv(int, char **); - -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] -#include -#include -#include -#include -#include -#include -#include "syscall.h" -#include "libc.h" -#include - -extern bool g_enable_check; -extern void mem_check_deinit(void); -extern void clean_recycle_list(bool clean_all); -pthread_mutex_t __exit_mutex = PTHREAD_MUTEX_INITIALIZER; - -static void dummy() -{ -} - -/* atexit.c and __stdio_exit.c override these. the latter is linked - * as a consequence of linking either __toread.c or __towrite.c. */ -weak_alias(dummy, __funcs_on_exit); -weak_alias(dummy, __stdio_exit); -weak_alias(dummy, _fini); - -extern weak hidden void (*const __fini_array_start)(void), (*const __fini_array_end)(void); - -static void libc_exit_fini(void) -{ - uintptr_t a = (uintptr_t)&__fini_array_end; - for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)())) - (*(void (**)())(a-sizeof(void(*)())))(); - _fini(); -} - -weak_alias(libc_exit_fini, __libc_exit_fini); - -_Noreturn void exit(int code) -{ - sigset_t set; - - __block_app_sigs(&set); - - int ret = pthread_mutex_trylock(&__exit_mutex); - if (ret == EBUSY) { - pthread_exit(NULL); - } - - if (g_enable_check) { - check_leak(); - check_heap_integrity(); - mem_check_deinit(); - clean_recycle_list(true); - } - __funcs_on_exit(); - __libc_exit_fini(); - __stdio_exit(); - _Exit(code); -} diff --git a/porting/liteos_a/user_debug/src/malloc/memalign.c b/porting/liteos_a/user_debug/src/malloc/memalign.c deleted file mode 100644 index ec6b1e617..000000000 --- a/porting/liteos_a/user_debug/src/malloc/memalign.c +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include "libc.h" -#include "pthread_impl.h" -#include "oldmalloc/malloc_impl.h" - -static inline void lock(volatile int *lk) -{ - if (libc.threads_minus_1) - while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1); -} - -static inline void unlock(volatile int *lk) -{ - if (lk[0]) { - a_store(lk, 0); - if (lk[1]) __wake(lk, 1, 1); - } -} - -void *__memalign(size_t align, size_t len) -{ - unsigned char *mem, *new; - - if ((align & -align) != align) { - errno = EINVAL; - return 0; - } - - if (len > SIZE_MAX - align || __malloc_replaced) { - errno = ENOMEM; - return 0; - } - - if (align <= SIZE_ALIGN) - return malloc(len); - - if (!(mem = malloc(len + align-1))) - return 0; - - new = (void *)((uintptr_t)mem + align-1 & -align); - if (new == mem) return mem; - - struct chunk *c = MEM_TO_CHUNK(mem); - struct chunk *n = MEM_TO_CHUNK(new); - - if (g_enable_check) { - int status = delete_node(mem); - if (status != 0) { - get_free_trace(mem); - a_crash(); - } - } - - if (IS_MMAPPED(c)) { - /* Apply difference between aligned and original - * address to the "extra" field of mmapped chunk. */ - n->psize = c->psize + (new-mem); - n->csize = c->csize - (new-mem); - if (g_enable_check) { - insert_node(CHUNK_TO_MEM(n), CHUNK_SIZE(n)); - } - return new; - } - - struct chunk *t = NEXT_CHUNK(c); - - /* Split the allocated chunk into two chunks. The aligned part - * that will be used has the size in its footer reduced by the - * difference between the aligned and original addresses, and - * the resulting size copied to its header. A new header and - * footer are written for the split-off part to be freed. */ - lock(g_mem_lock); - n->psize = c->csize = C_INUSE | (new-mem); - n->csize = t->psize -= new-mem; - calculate_checksum(c, n); - calculate_checksum(NULL, t); - unlock(g_mem_lock); - if (g_enable_check) { - insert_node(CHUNK_TO_MEM(n), CHUNK_SIZE(n)); - } - - __bin_chunk(c); - return new; -} - -weak_alias(__memalign, memalign); diff --git a/porting/liteos_a/user_debug/src/malloc/oldmalloc/aligned_alloc.c b/porting/liteos_a/user_debug/src/malloc/oldmalloc/aligned_alloc.c deleted file mode 100644 index 2377438b3..000000000 --- a/porting/liteos_a/user_debug/src/malloc/oldmalloc/aligned_alloc.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include "malloc_impl.h" -#include "dynlink.h" - -void *aligned_alloc(size_t align, size_t len) -{ - unsigned char *mem, *new; - - if ((align & -align) != align) { - errno = EINVAL; - return 0; - } - - if (len > SIZE_MAX - align || - (__malloc_replaced && !__aligned_alloc_replaced)) { - errno = ENOMEM; - return 0; - } - - if (align <= SIZE_ALIGN) - return malloc(len); - - if (!(mem = malloc(len + align-1))) - return 0; - - new = (void *)((uintptr_t)mem + align-1 & -align); - if (new == mem) return mem; - - struct chunk *c = MEM_TO_CHUNK(mem); - struct chunk *n = MEM_TO_CHUNK(new); - - if (IS_MMAPPED(c)) { - /* Apply difference between aligned and original - * address to the "extra" field of mmapped chunk. */ - n->psize = c->psize + (new-mem); - n->csize = c->csize - (new-mem); - return new; - } - - struct chunk *t = NEXT_CHUNK(c); - - /* Split the allocated chunk into two chunks. The aligned part - * that will be used has the size in its footer reduced by the - * difference between the aligned and original addresses, and - * the resulting size copied to its header. A new header and - * footer are written for the split-off part to be freed. */ - n->psize = c->csize = C_INUSE | (new-mem); - n->csize = t->psize -= new-mem; - - __bin_chunk(c); - return new; -} diff --git a/porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc.c b/porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc.c deleted file mode 100644 index 186c0b91a..000000000 --- a/porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc.c +++ /dev/null @@ -1,647 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include "libc.h" -#include "atomic.h" -#include "pthread_impl.h" -#include "malloc_impl.h" -#include "fork_impl.h" - -#define malloc __libc_malloc_impl -#define realloc __libc_realloc -#define free __libc_free - -#if defined(__GNUC__) && defined(__PIC__) -#define inline inline __attribute__((always_inline)) -#endif - -static struct { - volatile uint64_t binmap; - struct bin bins[64]; - volatile int split_merge_lock[2]; -} mal; - -/* Synchronization tools */ - -static inline void lock(volatile int *lk) -{ - int need_locks = libc.need_locks; - if (need_locks) { - while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1); - if (need_locks < 0) libc.need_locks = 0; - } -} - -static inline void unlock(volatile int *lk) -{ - if (lk[0]) { - a_store(lk, 0); - if (lk[1]) __wake(lk, 1, 1); - } -} - -static inline void lock_bin(int i) -{ - lock(mal.bins[i].lock); - if (!mal.bins[i].head) - mal.bins[i].head = mal.bins[i].tail = BIN_TO_CHUNK(i); -} - -static inline void unlock_bin(int i) -{ - unlock(mal.bins[i].lock); -} - -static int first_set(uint64_t x) -{ -#if 1 - return a_ctz_64(x); -#else - static const char debruijn64[64] = { - 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, - 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, - 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, - 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 - }; - static const char debruijn32[32] = { - 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, - 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 - }; - if (sizeof(long) < 8) { - uint32_t y = x; - if (!y) { - y = x>>32; - return 32 + debruijn32[(y&-y)*0x076be629 >> 27]; - } - return debruijn32[(y&-y)*0x076be629 >> 27]; - } - return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58]; -#endif -} - -static const unsigned char bin_tab[60] = { - 32,33,34,35,36,36,37,37,38,38,39,39, - 40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43, - 44,44,44,44,44,44,44,44,45,45,45,45,45,45,45,45, - 46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47, -}; - -static int bin_index(size_t x) -{ - x = x / SIZE_ALIGN - 1; - if (x <= 32) return x; - if (x < 512) return bin_tab[x/8-4]; - if (x > 0x1c00) return 63; - return bin_tab[x/128-4] + 16; -} - -static int bin_index_up(size_t x) -{ - x = x / SIZE_ALIGN - 1; - if (x <= 32) return x; - x--; - if (x < 512) return bin_tab[x/8-4] + 1; - return bin_tab[x/128-4] + 17; -} - -#if 0 -void __dump_heap(int x) -{ - struct chunk *c; - int i; - for (c = (void *)mal.heap; CHUNK_SIZE(c); c = NEXT_CHUNK(c)) - fprintf(stderr, "base %p size %zu (%d) flags %d/%d\n", - c, CHUNK_SIZE(c), bin_index(CHUNK_SIZE(c)), - c->csize & 15, - NEXT_CHUNK(c)->psize & 15); - for (i=0; i<64; i++) { - if (mal.bins[i].head != BIN_TO_CHUNK(i) && mal.bins[i].head) { - fprintf(stderr, "bin %d: %p\n", i, mal.bins[i].head); - if (!(mal.binmap & 1ULL< len ? b-len : 0; - if (new>a && old len ? b-len : 0; - if (new>a && old SIZE_MAX/2 - PAGE_SIZE) { - errno = ENOMEM; - return 0; - } - n += -n & PAGE_SIZE-1; - - if (!brk) { - brk = __syscall(SYS_brk, 0); - brk += -brk & PAGE_SIZE-1; - } - - if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n) - && __syscall(SYS_brk, brk+n)==brk+n) { - *pn = n; - brk += n; - return (void *)(brk-n); - } - - size_t min = (size_t)PAGE_SIZE << mmap_step/2; - if (n < min) n = min; - void *area = __mmap(0, n, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (area == MAP_FAILED) return 0; - *pn = n; - mmap_step++; - return area; -} - -static struct chunk *expand_heap(size_t n) -{ - static void *end; - void *p; - struct chunk *w; - - /* The argument n already accounts for the caller's chunk - * overhead needs, but if the heap can't be extended in-place, - * we need room for an extra zero-sized sentinel chunk. */ - n += SIZE_ALIGN; - - p = __expand_heap(&n); - if (!p) return 0; - - lock(g_mem_lock); - /* If not just expanding existing space, we need to make a - * new sentinel chunk below the allocated space. */ - if (p != end) { - /* Valid/safe because of the prologue increment. */ - n -= SIZE_ALIGN; - p = (char *)p + SIZE_ALIGN; - w = MEM_TO_CHUNK(p); - w->psize = 0 | C_INUSE; - insert_block_list(w); - } - - /* Record new heap end and fill in footer. */ - end = (char *)p + n; - w = MEM_TO_CHUNK(end); - w->psize = n | C_INUSE; - w->csize = 0 | C_INUSE; - - /* Fill in header, which may be new or may be replacing a - * zero-size sentinel header at the old end-of-heap. */ - w = MEM_TO_CHUNK(p); - w->csize = n | C_INUSE; - calculate_checksum(w, MEM_TO_CHUNK(end)); - - unlock(g_mem_lock); - - return w; -} - -static int adjust_size(size_t *n) -{ - /* Result of pointer difference must fit in ptrdiff_t. */ - if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) { - if (*n) { - errno = ENOMEM; - return -1; - } else { - *n = SIZE_ALIGN; - return 0; - } - } - *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK; - return 0; -} - -static void unbin(struct chunk *c, int i) -{ - if (c->prev == c->next) - a_and_64(&mal.binmap, ~(1ULL<prev->next = c->next; - c->next->prev = c->prev; - c->csize |= C_INUSE; - NEXT_CHUNK(c)->psize |= C_INUSE; -} - -static void bin_chunk(struct chunk *self, int i) -{ - self->next = BIN_TO_CHUNK(i); - self->prev = mal.bins[i].tail; - self->next->prev = self; - self->prev->next = self; - if (self->prev == BIN_TO_CHUNK(i)) - a_or_64(&mal.binmap, 1ULL<= n1 - DONTCARE) return; - - next = NEXT_CHUNK(self); - split = (void *)((char *)self + n); - lock(g_mem_lock); - split->psize = n | C_INUSE; - split->csize = n1-n; - next->psize = n1-n; - calculate_checksum(split, next); - self->csize = n | C_INUSE; - calculate_checksum(self, NULL); - unlock(g_mem_lock); - - int i = bin_index(n1-n); - lock_bin(i); - - bin_chunk(split, i); - - unlock_bin(i); -} - -void *malloc(size_t n) -{ - struct chunk *c; - int i, j; - uint64_t mask; - - if (adjust_size(&n) < 0) return 0; - - if (n > MMAP_THRESHOLD) { - size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE; - if (g_enable_check) { - /* Allocate two more pages for protection, loacted at the head and tail of user memory respectively */ - len += PAGE_SIZE << 1; - } - char *base = __mmap(0, len, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (base == (void *)-1) return 0; - if (g_enable_check) { - if (mprotect(base, PAGE_SIZE, PROT_NONE) || - mprotect(base + len - PAGE_SIZE, PAGE_SIZE, PROT_NONE)) { - printf("%s %d, mprotect failed, err: %s!\n", __func__, __LINE__, strerror(errno)); - } - base += PAGE_SIZE; - } - c = (void *)(base + SIZE_ALIGN - OVERHEAD); - c->csize = len - (SIZE_ALIGN - OVERHEAD); - c->psize = SIZE_ALIGN - OVERHEAD; - if (g_enable_check) { - c->csize -= PAGE_SIZE << 1; - insert_node(CHUNK_TO_MEM(c), CHUNK_SIZE(c)); - } - return CHUNK_TO_MEM(c); - } - - i = bin_index_up(n); - if (i<63 && (mal.binmap & (1ULL<psize; - char *base = (char *)self - extra; - size_t oldlen = n0 + extra; - size_t newlen = n + extra; - /* Crash on realloc of freed chunk */ - if (extra & 1) { - if (g_enable_check) { - get_free_trace(CHUNK_TO_MEM(self)); - a_crash(); - } else { - a_crash(); - } - } - if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) { - n0 = n; - goto copy_free_ret; - } - newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; - if (oldlen == newlen) return p; - if (g_enable_check) { - goto copy_realloc; - } - base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE); - if (base == (void *)-1) - goto copy_realloc; - self = (void *)(base + extra); - self->csize = newlen - extra; - return CHUNK_TO_MEM(self); - } - - next = NEXT_CHUNK(self); - - /* Crash on corrupted footer (likely from buffer overflow) */ - if (next->psize != self->csize) a_crash(); - - if (n < n0) { - int i = bin_index_up(n); - int j = bin_index(n0); - if (icsize = split->psize = n | C_INUSE; - split->csize = next->psize = n0-n | C_INUSE; - calculate_checksum(self, next); - unlock(g_mem_lock); - __bin_chunk(split); - return CHUNK_TO_MEM(self); - } - - lock(mal.split_merge_lock); - - size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next); - if (n0+nsize >= n) { - int i = bin_index(nsize); - lock_bin(i); - if (!(next->csize & C_INUSE)) { - unbin(next, i); - unlock_bin(i); - next = NEXT_CHUNK(next); - self->csize = next->psize = n0+nsize | C_INUSE; - trim(self, n); - if (g_enable_check) { - int status = delete_node(p); - if (status != 0) { - get_free_trace(CHUNK_TO_MEM(self)); - a_crash(); - } - insert_node(CHUNK_TO_MEM(self), CHUNK_SIZE(self)); - } - unlock(mal.split_merge_lock); - return CHUNK_TO_MEM(self); - } - unlock_bin(i); - } - unlock(mal.split_merge_lock); - -copy_realloc: - /* As a last resort, allocate a new chunk and copy to it. */ - new = malloc(n-OVERHEAD); - if (!new) return 0; -copy_free_ret: - memcpy(new, p, (npsize != self->csize) a_crash(); - - lock(mal.split_merge_lock); - - size_t osize = CHUNK_SIZE(self), size = osize; - - /* Since we hold split_merge_lock, only transition from free to - * in-use can race; in-use to free is impossible */ - lock(g_mem_lock); - size_t psize = self->psize & C_INUSE ? 0 : CHUNK_PSIZE(self); - size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next); - calculate_checksum(self, next); - unlock(g_mem_lock); - - if (psize) { - int i = bin_index(psize); - lock_bin(i); - if (!(self->psize & C_INUSE)) { - struct chunk *prev = PREV_CHUNK(self); - unbin(prev, i); - self = prev; - size += psize; - } - unlock_bin(i); - } - if (nsize) { - int i = bin_index(nsize); - lock_bin(i); - if (!(next->csize & C_INUSE)) { - unbin(next, i); - next = NEXT_CHUNK(next); - size += nsize; - } - unlock_bin(i); - } - - int i = bin_index(size); - lock_bin(i); - lock(g_mem_lock); - self->csize = size; - next->psize = size; - calculate_checksum(self, next); - unlock(g_mem_lock); - bin_chunk(self, i); - unlock(mal.split_merge_lock); - - /* Replace middle of large chunks with fresh zero pages */ - if (size > RECLAIM && (size^(size-osize)) > size-osize) { - uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE; - uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE; - int e = errno; -#if 0 - __madvise((void *)a, b-a, MADV_DONTNEED); -#else - __mmap((void *)a, b-a, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0); -#endif - errno = e; - } - - unlock_bin(i); -} - -static void unmap_chunk(struct chunk *self) -{ - size_t extra = self->psize; - char *base = (char *)self - extra; - size_t len = CHUNK_SIZE(self) + extra; - /* Crash on double free */ - if (extra & 1) { - if (g_enable_check) { - get_free_trace(CHUNK_TO_MEM(self)); - a_crash(); - } else { - a_crash(); - } - } - if (g_enable_check) { - base -= PAGE_SIZE; - len += PAGE_SIZE << 1; - } - int e = errno; - __munmap(base, len); - errno = e; -} - -void free(void *p) -{ - if (!p) return; - - struct chunk *self = MEM_TO_CHUNK(p); - if (g_enable_check) { - if (!IS_MMAPPED(self)) { - check_chunk_integrity(self); - } - int status = delete_node(p); - if (status != 0) { - get_free_trace(p); - a_crash(); - } - } - - if (IS_MMAPPED(self)) - unmap_chunk(self); - else { - if (g_enable_check) { - insert_free_tail(self); - if (g_recycle_size >= RECYCLE_SIZE_MAX) { - clean_recycle_list(false); - return; - } - if (g_recycle_num < RECYCLE_MAX) { - return; - } - self = get_free_head(); - } - __bin_chunk(self); - } -} - -void __malloc_donate(char *start, char *end) -{ - size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - BLOCK_HEAD); - size_t align_end_down = (SIZE_ALIGN-1) & (uintptr_t)end; - - /* Getting past this condition ensures that the padding for alignment - * and header overhead will not overflow and will leave a nonzero - * multiple of SIZE_ALIGN bytes between start and end. */ - if (end - start <= BLOCK_HEAD + align_start_up + align_end_down) - return; - start += align_start_up + BLOCK_HEAD; - end -= align_end_down; - - lock(g_mem_lock); - struct chunk *c = MEM_TO_CHUNK(start), *n = MEM_TO_CHUNK(end); - c->psize = n->csize = C_INUSE; - c->csize = n->psize = C_INUSE | (end-start); - calculate_checksum(c, n); - insert_block_list(c); - unlock(g_mem_lock); - __bin_chunk(c); -} - -void __malloc_atfork(int who) -{ - if (who<0) { - lock(mal.split_merge_lock); - for (int i=0; i<64; i++) - lock(mal.bins[i].lock); - } else if (!who) { - for (int i=0; i<64; i++) - unlock(mal.bins[i].lock); - unlock(mal.split_merge_lock); - } else { - for (int i=0; i<64; i++) - mal.bins[i].lock[0] = mal.bins[i].lock[1] = 0; - mal.split_merge_lock[1] = 0; - mal.split_merge_lock[0] = 0; - } -} diff --git a/porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc_impl.h b/porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc_impl.h deleted file mode 100644 index 6107f0b65..000000000 --- a/porting/liteos_a/user_debug/src/malloc/oldmalloc/malloc_impl.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef MALLOC_IMPL_H -#define MALLOC_IMPL_H - -#include -#include - -#define BACKTRACE_DEPTH_MAX 5 /* The max depth of backtrace */ -#define BACKTRACE_START_OFFSET 0 /* The start offset of backtrace */ -#define SECONDARY_CALL_OFFSET 2 /* The backtrace offset for secondary call backtrace() */ -#define BACKTRACE_OFFSET (BACKTRACE_START_OFFSET + SECONDARY_CALL_OFFSET) -#define PREFIX_PLACE_HOLDER 10 /* Reserve positions for file name prefix "pid()_" */ -#define PTHREAD_NUM_MAX 128 /* Same as number of task of kernel */ -#define NODE_MAGIC 0xFCFCFCFC /* Magic number for node of chunk */ -#define FREE_MAGIC 0xFE /* Magic number for filling freed heap memory not recycled to heap pool */ -#define RECYCLE_MAX 128 /* The queue size for free() to recycle */ -#define RECYCLE_SIZE_MAX 0x300000 /* The max sum size of freed chunk for recycle list */ -#define ITEM_BUFFER_SIZE 256 /* The buffer max size for one item of memory debug info */ -#define CHECK_POINT_TRACE_MAX 2 /* The trace max for check point */ - -hidden void *__expand_heap(size_t *); - -hidden void __malloc_donate(char *, char *); - -hidden void *__memalign(size_t, size_t); - -struct chunk { - unsigned int checksum; - size_t psize, csize; - struct chunk *next, *prev; -}; - -struct bin { - unsigned int checksum; - volatile int lock[2]; - struct chunk *head; - struct chunk *tail; -}; - -struct heap_block { - struct heap_block *next; - struct heap_block *prev; -}; - -struct list { - struct list *prev; - struct list *next; -}; - -struct node { - short tid, pid; - void *ptr; - size_t size; - void *lr[BACKTRACE_DEPTH_MAX]; - struct list list; -}; - -struct stat_bin { - volatile int lock[2]; - struct list head; - size_t t_total_size; -}; - -#define ROUNDUP(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) -#define SIZE_ALIGN ROUNDUP(sizeof(struct chunk), 0x10) -#define SIZE_MASK (-SIZE_ALIGN) -#define OVERHEAD (sizeof(struct chunk)) -#define BLOCK_HEAD (sizeof(struct heap_block) + OVERHEAD) -#define CHUNK_BLOCK_OFFSET (sizeof(struct heap_block)) -#define CHUNK_TO_BLOCK(c) (struct heap_block *)((char *)(c) - CHUNK_BLOCK_OFFSET) -#define BLOCK_TO_CHUNK(p) (struct chunk *)((char *)(p) + CHUNK_BLOCK_OFFSET) -#define MMAP_THRESHOLD (0x1c00*(4*sizeof(size_t))) -#define DONTCARE SIZE_ALIGN -#define RECLAIM 163840 - -#define CHUNK_SIZE(c) ((c)->csize & -2) -#define CHUNK_PSIZE(c) ((c)->psize & -2) -#define PREV_CHUNK(c) ((struct chunk *)((char *)(c) - CHUNK_PSIZE(c))) -#define NEXT_CHUNK(c) ((struct chunk *)((char *)(c) + CHUNK_SIZE(c))) -#define MEM_TO_CHUNK(p) (struct chunk *)((char *)(p) - OVERHEAD) -#define CHUNK_TO_MEM(c) (void *)((char *)(c) + OVERHEAD) -#define BIN_TO_CHUNK(i) (&mal.bins[i].checksum) - -#define C_INUSE ((size_t)1) - -#define IS_MMAPPED(c) !((c)->csize & (C_INUSE)) - -hidden void __bin_chunk(struct chunk *); - -hidden extern int __malloc_replaced; - -hidden extern bool g_enable_check; -hidden extern int g_recycle_num; -hidden extern size_t g_recycle_size; -hidden extern int g_mem_lock[]; -hidden extern void insert_node(void *ptr, size_t size); -hidden extern int delete_node(void *ptr); -hidden extern void insert_block_list(struct chunk *ptr); -hidden extern void insert_free_tail(struct chunk *self); -hidden extern struct chunk *get_free_head(void); -hidden extern void get_free_trace(void *ptr); -hidden extern void clean_recycle_list(bool clean_all); -hidden extern void check_chunk_integrity(struct chunk *cur); -hidden extern void calculate_checksum(struct chunk *cur, struct chunk *next); - -#endif diff --git a/porting/liteos_m/user/BUILD.gn b/porting/liteos_m/user/BUILD.gn deleted file mode 100644 index f828d3745..000000000 --- a/porting/liteos_m/user/BUILD.gn +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved. -# -# 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. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "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 COPYRIGHT HOLDER OR -# CONTRIBUTORS 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. - -import("//kernel/liteos_m/liteos.gni") - -module_name = get_path_info(rebase_path("."), "name") -module_group(module_name) { - modules = [ "src:user_libc" ] -} diff --git a/porting/liteos_m/user/include/limits.h b/porting/liteos_m/user/include/limits.h deleted file mode 100644 index 44da653b3..000000000 --- a/porting/liteos_m/user/include/limits.h +++ /dev/null @@ -1,166 +0,0 @@ -#ifndef _LIMITS_H -#define _LIMITS_H - -#include - -#include /* __LONG_MAX */ - -/* Support signed or unsigned plain-char */ - -#if '\xff' > 0 -#define CHAR_MIN 0 -#define CHAR_MAX 255 -#else -#define CHAR_MIN (-128) -#define CHAR_MAX 127 -#endif - -#define CHAR_BIT 8 -#define SCHAR_MIN (-128) -#define SCHAR_MAX 127 -#define UCHAR_MAX 255 -#define SHRT_MIN (-1-0x7fff) -#define SHRT_MAX 0x7fff -#define USHRT_MAX 0xffff -#define INT_MIN (-1-0x7fffffff) -#define INT_MAX 0x7fffffff -#define UINT_MAX 0xffffffffU -#define LONG_MIN (-LONG_MAX-1) -#define LONG_MAX __LONG_MAX -#define ULONG_MAX (2UL*LONG_MAX+1) -#define LLONG_MIN (-LLONG_MAX-1) -#define LLONG_MAX 0x7fffffffffffffffLL -#define ULLONG_MAX (2ULL*LLONG_MAX+1) - -#define MB_LEN_MAX 4 - -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ - || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) - -#include - -#define PIPE_BUF 4096 -#define FILESIZEBITS 64 -#ifndef NAME_MAX -#define NAME_MAX 255 -#endif -#define PATH_MAX 256 -#define NGROUPS_MAX 32 -#define ARG_MAX 4096 -#define IOV_MAX 1024 -#define SYMLOOP_MAX 40 -#define WORD_BIT 32 -#define SSIZE_MAX LONG_MAX -#define TZNAME_MAX 6 -#define TTY_NAME_MAX 32 -#define HOST_NAME_MAX 255 - -#if LONG_MAX == 0x7fffffffL -#define LONG_BIT 32 -#else -#define LONG_BIT 64 -#endif - -/* Implementation choices... */ - -#define PTHREAD_KEYS_MAX 128 -#define PTHREAD_STACK_MIN 2048 -#define PTHREAD_DESTRUCTOR_ITERATIONS 4 -#define SEM_VALUE_MAX 0x7fffffff -#define SEM_NSEMS_MAX 256 -#define DELAYTIMER_MAX 0x7fffffff -#define MQ_PRIO_MAX 1 -#define LOGIN_NAME_MAX 256 - -/* Arbitrary numbers... */ - -#define BC_BASE_MAX 99 -#define BC_DIM_MAX 2048 -#define BC_SCALE_MAX 99 -#define BC_STRING_MAX 1000 -#define CHARCLASS_NAME_MAX 14 -#define COLL_WEIGHTS_MAX 2 -#define EXPR_NEST_MAX 32 -#define LINE_MAX 4096 -#define RE_DUP_MAX 255 - -#define NL_ARGMAX 9 -#define NL_MSGMAX 32767 -#define NL_SETMAX 255 -#define NL_TEXTMAX 2048 - -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_XOPEN_SOURCE) - -#ifdef PAGESIZE -#define PAGE_SIZE PAGESIZE -#endif -#define NZERO 20 -#define NL_LANGMAX 32 - -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) \ - || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) - -#define NL_NMAX 16 - -#endif - -/* POSIX/SUS requirements follow. These numbers come directly - * from SUS and have nothing to do with the host system. */ - -#define _POSIX_AIO_LISTIO_MAX 2 -#define _POSIX_AIO_MAX 1 -#define _POSIX_ARG_MAX 4096 -#define _POSIX_CHILD_MAX 25 -#define _POSIX_CLOCKRES_MIN 20000000 -#define _POSIX_DELAYTIMER_MAX 32 -#define _POSIX_HOST_NAME_MAX 255 -#define _POSIX_LINK_MAX 8 -#define _POSIX_LOGIN_NAME_MAX 9 -#define _POSIX_MAX_CANON 255 -#define _POSIX_MAX_INPUT 255 -#define _POSIX_MQ_OPEN_MAX 8 -#define _POSIX_MQ_PRIO_MAX 32 -#define _POSIX_NAME_MAX 14 -#define _POSIX_NGROUPS_MAX 8 -#define _POSIX_OPEN_MAX 20 -#define _POSIX_PATH_MAX 256 -#define _POSIX_PIPE_BUF 512 -#define _POSIX_RE_DUP_MAX 255 -#define _POSIX_RTSIG_MAX 8 -#define _POSIX_SEM_NSEMS_MAX 256 -#define _POSIX_SEM_VALUE_MAX 32767 -#define _POSIX_SIGQUEUE_MAX 32 -#define _POSIX_SSIZE_MAX 32767 -#define _POSIX_STREAM_MAX 8 -#define _POSIX_SS_REPL_MAX 4 -#define _POSIX_SYMLINK_MAX 255 -#define _POSIX_SYMLOOP_MAX 8 -#define _POSIX_THREAD_DESTRUCTOR_ITERATIONS 4 -#define _POSIX_THREAD_KEYS_MAX 128 -#define _POSIX_THREAD_THREADS_MAX 64 -#define _POSIX_TIMER_MAX 32 -#define _POSIX_TRACE_EVENT_NAME_MAX 30 -#define _POSIX_TRACE_NAME_MAX 8 -#define _POSIX_TRACE_SYS_MAX 8 -#define _POSIX_TRACE_USER_EVENT_MAX 32 -#define _POSIX_TTY_NAME_MAX 9 -#define _POSIX_TZNAME_MAX 6 -#define _POSIX2_BC_BASE_MAX 99 -#define _POSIX2_BC_DIM_MAX 2048 -#define _POSIX2_BC_SCALE_MAX 99 -#define _POSIX2_BC_STRING_MAX 1000 -#define _POSIX2_CHARCLASS_NAME_MAX 14 -#define _POSIX2_COLL_WEIGHTS_MAX 2 -#define _POSIX2_EXPR_NEST_MAX 32 -#define _POSIX2_LINE_MAX 2048 -#define _POSIX2_RE_DUP_MAX 255 - -#define _XOPEN_IOV_MAX 16 -#define _XOPEN_NAME_MAX 255 -#define _XOPEN_PATH_MAX 1024 - -#endif diff --git a/porting/liteos_m/user/include/unistd.h b/porting/liteos_m/user/include/unistd.h deleted file mode 100644 index b4ff3db86..000000000 --- a/porting/liteos_m/user/include/unistd.h +++ /dev/null @@ -1,493 +0,0 @@ -#ifndef _UNISTD_H -#define _UNISTD_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define STDIN_FILENO 0 -#define STDOUT_FILENO 1 -#define STDERR_FILENO 2 - -#define SEEK_SET 0 -#define SEEK_CUR 1 -#define SEEK_END 2 - -#ifdef __cplusplus -#define NULL 0L -#else -#define NULL ((void*)0) -#endif - -#define __NEED_size_t -#define __NEED_ssize_t -#define __NEED_uid_t -#define __NEED_gid_t -#define __NEED_off_t -#define __NEED_pid_t -#define __NEED_intptr_t -#define __NEED_useconds_t - -#include - -int pipe(int [2]); -int pipe2(int [2], int); -int close(int); -int posix_close(int, int); -int dup(int); -int dup2(int, int); -int dup3(int, int, int); -off_t lseek(int, off_t, int); -int fsync(int); -int fdatasync(int); - -ssize_t read(int, void *, size_t); -ssize_t write(int, const void *, size_t); -ssize_t pread(int, void *, size_t, off_t); -ssize_t pwrite(int, const void *, size_t, off_t); - -int chown(const char *, uid_t, gid_t); -int fchown(int, uid_t, gid_t); -int lchown(const char *, uid_t, gid_t); -int fchownat(int, const char *, uid_t, gid_t, int); - -int link(const char *, const char *); -int linkat(int, const char *, int, const char *, int); -int symlink(const char *, const char *); -int symlinkat(const char *, int, const char *); -ssize_t readlink(const char *__restrict, char *__restrict, size_t); -ssize_t readlinkat(int, const char *__restrict, char *__restrict, size_t); -int unlink(const char *); -int unlinkat(int, const char *, int); -int rmdir(const char *); -int truncate(const char *, off_t); -int ftruncate(int, off_t); - -#define F_OK 0 -#define R_OK 4 -#define W_OK 2 -#define X_OK 1 - -int access(const char *, int); -int faccessat(int, const char *, int, int); - - -/* Format options */ -#define FMT_FAT 0x01 -#define FMT_FAT32 0x02 -#define FMT_ANY 0x07 -#define FMT_ERASE 0x08 - -/** - * @brief format FAT device (SD card, U disk, and MMC card), this function is OHOS-specific - * @param dev device name. - * @param sectors sectors per cluster, can be 0 OR power of 2. The sector size for standard FAT volumes is 512 bytes. - * -- sector number is 0 OR >128: automatically choose the appropriate cluster size. - * -- sector number is 1 ~ 128: cluster size = sectors per cluster * 512B. - * @param option file system type. - * -- FMT_FAT - * -- FMT_FAT32 - * -- FMT_ANY - * -- FMT_ERASE (USB not supported) - * @return format result - * @retval -1 format error - * @retval 0 format successful - */ -int format(const char *dev, int sectors, int option); - -int chdir(const char *); -int fchdir(int); -char *getcwd(char *, size_t); - -unsigned alarm(unsigned); -unsigned sleep(unsigned); -int pause(void); - -pid_t fork(void); -int execve(const char *, char *const [], char *const []); -int execv(const char *, char *const []); -int execle(const char *, const char *, ...); -int execl(const char *, const char *, ...); -int execvp(const char *, char *const []); -int execlp(const char *, const char *, ...); -int fexecve(int, char *const [], char *const []); -_Noreturn void _exit(int); - -pid_t getpid(void); -pid_t getppid(void); -pid_t getpgrp(void); -pid_t getpgid(pid_t); -int setpgid(pid_t, pid_t); -pid_t setsid(void); -pid_t getsid(pid_t); -char *ttyname(int); -int ttyname_r(int, char *, size_t); -int isatty(int); -pid_t tcgetpgrp(int); -int tcsetpgrp(int, pid_t); - -uid_t getuid(void); -uid_t geteuid(void); -gid_t getgid(void); -gid_t getegid(void); -int getgroups(int, gid_t []); -int setuid(uid_t); -int seteuid(uid_t); -int setgid(gid_t); -int setegid(gid_t); - -char *getlogin(void); -int getlogin_r(char *, size_t); -int gethostname(char *, size_t); -char *ctermid(char *); - -int getopt(int, char * const [], const char *); -extern char *optarg; -extern int optind, opterr, optopt; - -long pathconf(const char *, int); -long fpathconf(int, int); -long sysconf(int); -size_t confstr(int, char *, size_t); - -#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define F_ULOCK 0 -#define F_LOCK 1 -#define F_TLOCK 2 -#define F_TEST 3 -int setreuid(uid_t, uid_t); -int setregid(gid_t, gid_t); -int lockf(int, int, off_t); -long gethostid(void); -int nice(int); -void sync(void); -pid_t setpgrp(void); -char *crypt(const char *, const char *); -void encrypt(char *, int); -void swab(const void *__restrict, void *__restrict, ssize_t); -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) \ - || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) -int usleep(unsigned); -unsigned ualarm(unsigned, unsigned); -#endif - -#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) -#define L_SET 0 -#define L_INCR 1 -#define L_XTND 2 -int brk(void *); -void *sbrk(intptr_t); -pid_t vfork(void); -int vhangup(void); -int chroot(const char *); -int getpagesize(void); -int getdtablesize(void); -int sethostname(const char *, size_t); -int getdomainname(char *, size_t); -int setdomainname(const char *, size_t); -int setgroups(size_t, const gid_t *); -char *getpass(const char *); -int daemon(int, int); -void setusershell(void); -void endusershell(void); -char *getusershell(void); -int acct(const char *); -long syscall(long, ...); -int execvpe(const char *, char *const [], char *const []); -int issetugid(void); -int getentropy(void *, size_t); -extern int optreset; -#endif - -#ifdef _GNU_SOURCE -extern char **environ; -int setresuid(uid_t, uid_t, uid_t); -int setresgid(gid_t, gid_t, gid_t); -int getresuid(uid_t *, uid_t *, uid_t *); -int getresgid(gid_t *, gid_t *, gid_t *); -char *get_current_dir_name(void); -int syncfs(int); -int euidaccess(const char *, int); -int eaccess(const char *, int); -ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned); -#endif - -#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) -#define lseek64 lseek -#define pread64 pread -#define pwrite64 pwrite -#define truncate64 truncate -#define ftruncate64 ftruncate -#define lockf64 lockf -#define off64_t off_t -#endif - -#define POSIX_CLOSE_RESTART 0 - -#define _XOPEN_VERSION 700 -#define _XOPEN_UNIX 1 -#define _XOPEN_ENH_I18N 1 - -#define _POSIX_VERSION 200809L -#define _POSIX2_VERSION _POSIX_VERSION - -#define _POSIX_ADVISORY_INFO _POSIX_VERSION -#define _POSIX_CHOWN_RESTRICTED 1 -#define _POSIX_IPV6 _POSIX_VERSION -#define _POSIX_JOB_CONTROL 1 -#define _POSIX_MAPPED_FILES _POSIX_VERSION -#define _POSIX_MEMLOCK _POSIX_VERSION -#define _POSIX_MEMLOCK_RANGE _POSIX_VERSION -#define _POSIX_MEMORY_PROTECTION _POSIX_VERSION -#define _POSIX_MESSAGE_PASSING _POSIX_VERSION -#define _POSIX_FSYNC _POSIX_VERSION -#define _POSIX_NO_TRUNC 1 -#define _POSIX_RAW_SOCKETS _POSIX_VERSION -#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION -#define _POSIX_REGEXP 1 -#define _POSIX_SAVED_IDS 1 -#define _POSIX_SHELL 1 -#define _POSIX_SPAWN _POSIX_VERSION -#define _POSIX_VDISABLE 0 - -#define _POSIX_THREADS _POSIX_VERSION -#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION -#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION -#define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION -#define _POSIX_THREAD_ATTR_STACKSIZE _POSIX_VERSION -#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION -#define _POSIX_THREAD_CPUTIME _POSIX_VERSION -#define _POSIX_TIMERS _POSIX_VERSION -#define _POSIX_TIMEOUTS _POSIX_VERSION -#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION -#define _POSIX_CPUTIME _POSIX_VERSION -#define _POSIX_CLOCK_SELECTION _POSIX_VERSION -#define _POSIX_BARRIERS _POSIX_VERSION -#define _POSIX_SPIN_LOCKS _POSIX_VERSION -#define _POSIX_READER_WRITER_LOCKS _POSIX_VERSION -#define _POSIX_ASYNCHRONOUS_IO _POSIX_VERSION -#define _POSIX_SEMAPHORES _POSIX_VERSION -#define _POSIX_SHARED_MEMORY_OBJECTS _POSIX_VERSION - -#define _POSIX2_C_BIND _POSIX_VERSION - -#include - - - -#define _PC_LINK_MAX 0 -#define _PC_MAX_CANON 1 -#define _PC_MAX_INPUT 2 -#define _PC_NAME_MAX 3 -#define _PC_PATH_MAX 4 -#define _PC_PIPE_BUF 5 -#define _PC_CHOWN_RESTRICTED 6 -#define _PC_NO_TRUNC 7 -#define _PC_VDISABLE 8 -#define _PC_SYNC_IO 9 -#define _PC_ASYNC_IO 10 -#define _PC_PRIO_IO 11 -#define _PC_SOCK_MAXBUF 12 -#define _PC_FILESIZEBITS 13 -#define _PC_REC_INCR_XFER_SIZE 14 -#define _PC_REC_MAX_XFER_SIZE 15 -#define _PC_REC_MIN_XFER_SIZE 16 -#define _PC_REC_XFER_ALIGN 17 -#define _PC_ALLOC_SIZE_MIN 18 -#define _PC_SYMLINK_MAX 19 -#define _PC_2_SYMLINKS 20 - -#define _SC_ARG_MAX 0 -#define _SC_CHILD_MAX 1 -#define _SC_CLK_TCK 2 -#define _SC_NGROUPS_MAX 3 -#define _SC_OPEN_MAX 4 -#define _SC_STREAM_MAX 5 -#define _SC_TZNAME_MAX 6 -#define _SC_JOB_CONTROL 7 -#define _SC_SAVED_IDS 8 -#define _SC_REALTIME_SIGNALS 9 -#define _SC_PRIORITY_SCHEDULING 10 -#define _SC_TIMERS 11 -#define _SC_ASYNCHRONOUS_IO 12 -#define _SC_PRIORITIZED_IO 13 -#define _SC_SYNCHRONIZED_IO 14 -#define _SC_FSYNC 15 -#define _SC_MAPPED_FILES 16 -#define _SC_MEMLOCK 17 -#define _SC_MEMLOCK_RANGE 18 -#define _SC_MEMORY_PROTECTION 19 -#define _SC_MESSAGE_PASSING 20 -#define _SC_SEMAPHORES 21 -#define _SC_SHARED_MEMORY_OBJECTS 22 -#define _SC_AIO_LISTIO_MAX 23 -#define _SC_AIO_MAX 24 -#define _SC_AIO_PRIO_DELTA_MAX 25 -#define _SC_DELAYTIMER_MAX 26 -#define _SC_MQ_OPEN_MAX 27 -#define _SC_MQ_PRIO_MAX 28 -#define _SC_VERSION 29 -#define _SC_PAGE_SIZE 30 -#define _SC_PAGESIZE 30 /* !! */ -#define _SC_RTSIG_MAX 31 -#define _SC_SEM_NSEMS_MAX 32 -#define _SC_SEM_VALUE_MAX 33 -#define _SC_SIGQUEUE_MAX 34 -#define _SC_TIMER_MAX 35 -#define _SC_BC_BASE_MAX 36 -#define _SC_BC_DIM_MAX 37 -#define _SC_BC_SCALE_MAX 38 -#define _SC_BC_STRING_MAX 39 -#define _SC_COLL_WEIGHTS_MAX 40 -#define _SC_EXPR_NEST_MAX 42 -#define _SC_LINE_MAX 43 -#define _SC_RE_DUP_MAX 44 -#define _SC_2_VERSION 46 -#define _SC_2_C_BIND 47 -#define _SC_2_C_DEV 48 -#define _SC_2_FORT_DEV 49 -#define _SC_2_FORT_RUN 50 -#define _SC_2_SW_DEV 51 -#define _SC_2_LOCALEDEF 52 -#define _SC_UIO_MAXIOV 60 /* !! */ -#define _SC_IOV_MAX 60 -#define _SC_THREADS 67 -#define _SC_THREAD_SAFE_FUNCTIONS 68 -#define _SC_GETGR_R_SIZE_MAX 69 -#define _SC_GETPW_R_SIZE_MAX 70 -#define _SC_LOGIN_NAME_MAX 71 -#define _SC_TTY_NAME_MAX 72 -#define _SC_THREAD_DESTRUCTOR_ITERATIONS 73 -#define _SC_THREAD_KEYS_MAX 74 -#define _SC_THREAD_STACK_MIN 75 -#define _SC_THREAD_THREADS_MAX 76 -#define _SC_THREAD_ATTR_STACKADDR 77 -#define _SC_THREAD_ATTR_STACKSIZE 78 -#define _SC_THREAD_PRIORITY_SCHEDULING 79 -#define _SC_THREAD_PRIO_INHERIT 80 -#define _SC_THREAD_PRIO_PROTECT 81 -#define _SC_THREAD_PROCESS_SHARED 82 -#define _SC_NPROCESSORS_CONF 83 -#define _SC_NPROCESSORS_ONLN 84 -#define _SC_PHYS_PAGES 85 -#define _SC_AVPHYS_PAGES 86 -#define _SC_ATEXIT_MAX 87 -#define _SC_PASS_MAX 88 -#define _SC_XOPEN_VERSION 89 -#define _SC_XOPEN_XCU_VERSION 90 -#define _SC_XOPEN_UNIX 91 -#define _SC_XOPEN_CRYPT 92 -#define _SC_XOPEN_ENH_I18N 93 -#define _SC_XOPEN_SHM 94 -#define _SC_2_CHAR_TERM 95 -#define _SC_2_UPE 97 -#define _SC_XOPEN_XPG2 98 -#define _SC_XOPEN_XPG3 99 -#define _SC_XOPEN_XPG4 100 -#define _SC_NZERO 109 -#define _SC_XBS5_ILP32_OFF32 125 -#define _SC_XBS5_ILP32_OFFBIG 126 -#define _SC_XBS5_LP64_OFF64 127 -#define _SC_XBS5_LPBIG_OFFBIG 128 -#define _SC_XOPEN_LEGACY 129 -#define _SC_XOPEN_REALTIME 130 -#define _SC_XOPEN_REALTIME_THREADS 131 -#define _SC_ADVISORY_INFO 132 -#define _SC_BARRIERS 133 -#define _SC_CLOCK_SELECTION 137 -#define _SC_CPUTIME 138 -#define _SC_THREAD_CPUTIME 139 -#define _SC_MONOTONIC_CLOCK 149 -#define _SC_READER_WRITER_LOCKS 153 -#define _SC_SPIN_LOCKS 154 -#define _SC_REGEXP 155 -#define _SC_SHELL 157 -#define _SC_SPAWN 159 -#define _SC_SPORADIC_SERVER 160 -#define _SC_THREAD_SPORADIC_SERVER 161 -#define _SC_TIMEOUTS 164 -#define _SC_TYPED_MEMORY_OBJECTS 165 -#define _SC_2_PBS 168 -#define _SC_2_PBS_ACCOUNTING 169 -#define _SC_2_PBS_LOCATE 170 -#define _SC_2_PBS_MESSAGE 171 -#define _SC_2_PBS_TRACK 172 -#define _SC_SYMLOOP_MAX 173 -#define _SC_STREAMS 174 -#define _SC_2_PBS_CHECKPOINT 175 -#define _SC_V6_ILP32_OFF32 176 -#define _SC_V6_ILP32_OFFBIG 177 -#define _SC_V6_LP64_OFF64 178 -#define _SC_V6_LPBIG_OFFBIG 179 -#define _SC_HOST_NAME_MAX 180 -#define _SC_TRACE 181 -#define _SC_TRACE_EVENT_FILTER 182 -#define _SC_TRACE_INHERIT 183 -#define _SC_TRACE_LOG 184 - -#define _SC_IPV6 235 -#define _SC_RAW_SOCKETS 236 -#define _SC_V7_ILP32_OFF32 237 -#define _SC_V7_ILP32_OFFBIG 238 -#define _SC_V7_LP64_OFF64 239 -#define _SC_V7_LPBIG_OFFBIG 240 -#define _SC_SS_REPL_MAX 241 -#define _SC_TRACE_EVENT_NAME_MAX 242 -#define _SC_TRACE_NAME_MAX 243 -#define _SC_TRACE_SYS_MAX 244 -#define _SC_TRACE_USER_EVENT_MAX 245 -#define _SC_XOPEN_STREAMS 246 -#define _SC_THREAD_ROBUST_PRIO_INHERIT 247 -#define _SC_THREAD_ROBUST_PRIO_PROTECT 248 - -#define _CS_PATH 0 -#define _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS 1 -#define _CS_GNU_LIBC_VERSION 2 -#define _CS_GNU_LIBPTHREAD_VERSION 3 -#define _CS_POSIX_V5_WIDTH_RESTRICTED_ENVS 4 -#define _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS 5 - -#define _CS_POSIX_V6_ILP32_OFF32_CFLAGS 1116 -#define _CS_POSIX_V6_ILP32_OFF32_LDFLAGS 1117 -#define _CS_POSIX_V6_ILP32_OFF32_LIBS 1118 -#define _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS 1119 -#define _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS 1120 -#define _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS 1121 -#define _CS_POSIX_V6_ILP32_OFFBIG_LIBS 1122 -#define _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS 1123 -#define _CS_POSIX_V6_LP64_OFF64_CFLAGS 1124 -#define _CS_POSIX_V6_LP64_OFF64_LDFLAGS 1125 -#define _CS_POSIX_V6_LP64_OFF64_LIBS 1126 -#define _CS_POSIX_V6_LP64_OFF64_LINTFLAGS 1127 -#define _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS 1128 -#define _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS 1129 -#define _CS_POSIX_V6_LPBIG_OFFBIG_LIBS 1130 -#define _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS 1131 -#define _CS_POSIX_V7_ILP32_OFF32_CFLAGS 1132 -#define _CS_POSIX_V7_ILP32_OFF32_LDFLAGS 1133 -#define _CS_POSIX_V7_ILP32_OFF32_LIBS 1134 -#define _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS 1135 -#define _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS 1136 -#define _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS 1137 -#define _CS_POSIX_V7_ILP32_OFFBIG_LIBS 1138 -#define _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS 1139 -#define _CS_POSIX_V7_LP64_OFF64_CFLAGS 1140 -#define _CS_POSIX_V7_LP64_OFF64_LDFLAGS 1141 -#define _CS_POSIX_V7_LP64_OFF64_LIBS 1142 -#define _CS_POSIX_V7_LP64_OFF64_LINTFLAGS 1143 -#define _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS 1144 -#define _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS 1145 -#define _CS_POSIX_V7_LPBIG_OFFBIG_LIBS 1146 -#define _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS 1147 -#define _CS_V6_ENV 1148 -#define _CS_V7_ENV 1149 - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/porting/liteos_m/user/src/string/memcmp.c b/porting/liteos_m/user/src/string/memcmp.c deleted file mode 100644 index a2a84f121..000000000 --- a/porting/liteos_m/user/src/string/memcmp.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "usr_lib_define.h" -#include - -_LIBC_TEXT_SECTION int memcmp(const void *vl, const void *vr, size_t n) -{ - const unsigned char *l=vl, *r=vr; - for (; n && *l == *r; n--, l++, r++); - return n ? *l-*r : 0; -} diff --git a/porting/liteos_m/user/src/string/memcpy.c b/porting/liteos_m/user/src/string/memcpy.c deleted file mode 100644 index 5dfea78e5..000000000 --- a/porting/liteos_m/user/src/string/memcpy.c +++ /dev/null @@ -1,125 +0,0 @@ -#include "usr_lib_define.h" -#include -#include -#include - -_LIBC_TEXT_SECTION void *memcpy(void *restrict dest, const void *restrict src, size_t n) -{ - unsigned char *d = dest; - const unsigned char *s = src; - -#ifdef __GNUC__ - -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define LS >> -#define RS << -#else -#define LS << -#define RS >> -#endif - - typedef uint32_t __attribute__((__may_alias__)) u32; - uint32_t w, x; - - for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++; - - if ((uintptr_t)d % 4 == 0) { - for (; n>=16; s+=16, d+=16, n-=16) { - *(u32 *)(d+0) = *(u32 *)(s+0); - *(u32 *)(d+4) = *(u32 *)(s+4); - *(u32 *)(d+8) = *(u32 *)(s+8); - *(u32 *)(d+12) = *(u32 *)(s+12); - } - if (n&8) { - *(u32 *)(d+0) = *(u32 *)(s+0); - *(u32 *)(d+4) = *(u32 *)(s+4); - d += 8; s += 8; - } - if (n&4) { - *(u32 *)(d+0) = *(u32 *)(s+0); - d += 4; s += 4; - } - if (n&2) { - *d++ = *s++; *d++ = *s++; - } - if (n&1) { - *d = *s; - } - return dest; - } - - if (n >= 32) switch ((uintptr_t)d % 4) { - case 1: - w = *(u32 *)s; - *d++ = *s++; - *d++ = *s++; - *d++ = *s++; - n -= 3; - for (; n>=17; s+=16, d+=16, n-=16) { - x = *(u32 *)(s+1); - *(u32 *)(d+0) = (w LS 24) | (x RS 8); - w = *(u32 *)(s+5); - *(u32 *)(d+4) = (x LS 24) | (w RS 8); - x = *(u32 *)(s+9); - *(u32 *)(d+8) = (w LS 24) | (x RS 8); - w = *(u32 *)(s+13); - *(u32 *)(d+12) = (x LS 24) | (w RS 8); - } - break; - case 2: - w = *(u32 *)s; - *d++ = *s++; - *d++ = *s++; - n -= 2; - for (; n>=18; s+=16, d+=16, n-=16) { - x = *(u32 *)(s+2); - *(u32 *)(d+0) = (w LS 16) | (x RS 16); - w = *(u32 *)(s+6); - *(u32 *)(d+4) = (x LS 16) | (w RS 16); - x = *(u32 *)(s+10); - *(u32 *)(d+8) = (w LS 16) | (x RS 16); - w = *(u32 *)(s+14); - *(u32 *)(d+12) = (x LS 16) | (w RS 16); - } - break; - case 3: - w = *(u32 *)s; - *d++ = *s++; - n -= 1; - for (; n>=19; s+=16, d+=16, n-=16) { - x = *(u32 *)(s+3); - *(u32 *)(d+0) = (w LS 8) | (x RS 24); - w = *(u32 *)(s+7); - *(u32 *)(d+4) = (x LS 8) | (w RS 24); - x = *(u32 *)(s+11); - *(u32 *)(d+8) = (w LS 8) | (x RS 24); - w = *(u32 *)(s+15); - *(u32 *)(d+12) = (x LS 8) | (w RS 24); - } - break; - } - if (n&16) { - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - } - if (n&8) { - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - } - if (n&4) { - *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; - } - if (n&2) { - *d++ = *s++; *d++ = *s++; - } - if (n&1) { - *d = *s; - } - return dest; -#endif - - for (; n; n--) *d++ = *s++; - return dest; -} diff --git a/porting/liteos_m/user/src/string/memset.c b/porting/liteos_m/user/src/string/memset.c deleted file mode 100644 index 52b504590..000000000 --- a/porting/liteos_m/user/src/string/memset.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "usr_lib_define.h" -#include -#include - -_LIBC_TEXT_SECTION void *memset(void *dest, int c, size_t n) -{ - unsigned char *s = dest; - size_t k; - - /* Fill head and tail with minimal branching. Each - * conditional ensures that all the subsequently used - * offsets are well-defined and in the dest region. */ - - if (!n) return dest; - s[0] = c; - s[n-1] = c; - if (n <= 2) return dest; - s[1] = c; - s[2] = c; - s[n-2] = c; - s[n-3] = c; - if (n <= 6) return dest; - s[3] = c; - s[n-4] = c; - if (n <= 8) return dest; - - /* Advance pointer to align it at a 4-byte boundary, - * and truncate n to a multiple of 4. The previous code - * already took care of any head/tail that get cut off - * by the alignment. */ - - k = -(uintptr_t)s & 3; - s += k; - n -= k; - n &= -4; - -#ifdef __GNUC__ - typedef uint32_t __attribute__((__may_alias__)) u32; - typedef uint64_t __attribute__((__may_alias__)) u64; - - u32 c32 = ((u32)-1)/255 * (unsigned char)c; - - /* In preparation to copy 32 bytes at a time, aligned on - * an 8-byte bounary, fill head/tail up to 28 bytes each. - * As in the initial byte-based head/tail fill, each - * conditional below ensures that the subsequent offsets - * are valid (e.g. !(n<=24) implies n>=28). */ - - *(u32 *)(s+0) = c32; - *(u32 *)(s+n-4) = c32; - if (n <= 8) return dest; - *(u32 *)(s+4) = c32; - *(u32 *)(s+8) = c32; - *(u32 *)(s+n-12) = c32; - *(u32 *)(s+n-8) = c32; - if (n <= 24) return dest; - *(u32 *)(s+12) = c32; - *(u32 *)(s+16) = c32; - *(u32 *)(s+20) = c32; - *(u32 *)(s+24) = c32; - *(u32 *)(s+n-28) = c32; - *(u32 *)(s+n-24) = c32; - *(u32 *)(s+n-20) = c32; - *(u32 *)(s+n-16) = c32; - - /* Align to a multiple of 8 so we can fill 64 bits at a time, - * and avoid writing the same bytes twice as much as is - * practical without introducing additional branching. */ - - k = 24 + ((uintptr_t)s & 4); - s += k; - n -= k; - - /* If this loop is reached, 28 tail bytes have already been - * filled, so any remainder when n drops below 32 can be - * safely ignored. */ - - u64 c64 = c32 | ((u64)c32 << 32); - for (; n >= 32; n-=32, s+=32) { - *(u64 *)(s+0) = c64; - *(u64 *)(s+8) = c64; - *(u64 *)(s+16) = c64; - *(u64 *)(s+24) = c64; - } -#else - /* Pure C fallback with no aliasing violations. */ - for (; n; n--, s++) *s = c; -#endif - - return dest; -} diff --git a/porting/liteos_m/user/src/string/strcmp.c b/porting/liteos_m/user/src/string/strcmp.c deleted file mode 100644 index 9edc94b4e..000000000 --- a/porting/liteos_m/user/src/string/strcmp.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "usr_lib_define.h" -#include - -_LIBC_TEXT_SECTION int strcmp(const char *l, const char *r) -{ - for (; *l==*r && *l; l++, r++); - return *(unsigned char *)l - *(unsigned char *)r; -} diff --git a/porting/liteos_m/user/src/string/strlen.c b/porting/liteos_m/user/src/string/strlen.c deleted file mode 100644 index 922866db4..000000000 --- a/porting/liteos_m/user/src/string/strlen.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "usr_lib_define.h" -#include -#include -#include - -#define ALIGN (sizeof(size_t)) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -_LIBC_TEXT_SECTION size_t strlen(const char *s) -{ - const char *a = s; -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - const word *w; - for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a; - for (w = (const void *)s; !HASZERO(*w); w++); - s = (const void *)w; -#endif - for (; *s; s++); - return s-a; -} diff --git a/porting/liteos_m/user/src/thread/__lock.c b/porting/liteos_m/user/src/thread/__lock.c deleted file mode 100644 index 73fe57131..000000000 --- a/porting/liteos_m/user/src/thread/__lock.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -/* This lock primitive combines a flag (in the sign bit) and a - * congestion count (= threads inside the critical section, CS) in a - * single int that is accessed through atomic operations. The states - * of the int for value x are: - * - * x == 0: unlocked and no thread inside the critical section - * - * x < 0: locked with a congestion of x-INT_MIN, including the thread - * that holds the lock - * - * x > 0: unlocked with a congestion of x - * - * or in an equivalent formulation x is the congestion count or'ed - * with INT_MIN as a lock flag. - */ - -_LIBC_TEXT_SECTION void __lock(volatile int *l) -{ - /* fast path: INT_MIN for the lock, +1 for the congestion */ - int current = a_cas(l, 0, INT_MIN + 1); - if (!current) return; - /* A first spin loop, for medium congestion. */ - for (unsigned i = 0; i < 10; ++i) { - if (current < 0) current -= INT_MIN + 1; - // assertion: current >= 0 - int val = a_cas(l, current, INT_MIN + (current + 1)); - if (val == current) return; - current = val; - } - // Spinning failed, so mark ourselves as being inside the CS. - current = a_fetch_add(l, 1) + 1; - /* The main lock acquisition loop for heavy congestion. The only - * change to the value performed inside that loop is a successful - * lock via the CAS that acquires the lock. */ - for (;;) { - /* We can only go into wait, if we know that somebody holds the - * lock and will eventually wake us up, again. */ - if (current < 0) { - __futexwait(l, current, 1); - current -= INT_MIN + 1; - } - /* assertion: current > 0, the count includes us already. */ - int val = a_cas(l, current, INT_MIN + current); - if (val == current) return; - current = val; - } -} - -_LIBC_TEXT_SECTION void __unlock(volatile int *l) -{ - /* Check l[0] to see if we are multi-threaded. */ - if (l[0] < 0) { - if (a_fetch_add(l, -(INT_MIN + 1)) != (INT_MIN + 1)) { - __wake(l, 1, 1); - } - } -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_destroy.c b/porting/liteos_m/user/src/thread/pthread_attr_destroy.c deleted file mode 100644 index dbfc3a328..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_destroy.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_destroy(pthread_attr_t *a) -{ - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_get.c b/porting/liteos_m/user/src/thread/pthread_attr_get.c deleted file mode 100644 index db6408df9..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_get.c +++ /dev/null @@ -1,52 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_getdetachstate(const pthread_attr_t *a, int *state) -{ - *state = a->_a_detach; - return 0; -} - -_LIBC_TEXT_SECTION int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size) -{ - return ENOSYS; -} - -_LIBC_TEXT_SECTION int pthread_attr_getinheritsched(const pthread_attr_t *restrict a, int *restrict inherit) -{ - *inherit = a->_a_sched; - return 0; -} - -_LIBC_TEXT_SECTION int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param) -{ - param->sched_priority = a->_a_prio; - return 0; -} - -_LIBC_TEXT_SECTION int pthread_attr_getschedpolicy(const pthread_attr_t *restrict a, int *restrict policy) -{ - *policy = a->_a_policy; - return 0; -} - -_LIBC_TEXT_SECTION int pthread_attr_getscope(const pthread_attr_t *restrict a, int *restrict scope) -{ - *scope = PTHREAD_SCOPE_SYSTEM; - return 0; -} - -_LIBC_TEXT_SECTION int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size) -{ - if (!a->_a_stackaddr) - return EINVAL; - *size = a->_a_stacksize; - *addr = (void *)(a->_a_stackaddr - *size); - return 0; -} - -_LIBC_TEXT_SECTION int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size) -{ - *size = a->_a_stacksize; - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_init.c b/porting/liteos_m/user/src/thread/pthread_attr_init.c deleted file mode 100644 index d97684873..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_init.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_init(pthread_attr_t *a) -{ - *a = (pthread_attr_t){0}; - a->_a_stacksize = 0x2000; - a->_a_policy = SCHED_RR; - a->_a_prio = 25; - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setdetachstate.c b/porting/liteos_m/user/src/thread/pthread_attr_setdetachstate.c deleted file mode 100644 index 38e290219..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setdetachstate.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_setdetachstate(pthread_attr_t *a, int state) -{ - if (state > 1U) return EINVAL; - a->_a_detach = state; - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setguardsize.c b/porting/liteos_m/user/src/thread/pthread_attr_setguardsize.c deleted file mode 100644 index df5e7c2ae..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setguardsize.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_setguardsize(pthread_attr_t *a, size_t size) -{ - return ENOSYS; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setinheritsched.c b/porting/liteos_m/user/src/thread/pthread_attr_setinheritsched.c deleted file mode 100644 index 5d0ee635c..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setinheritsched.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" -#include "syscall.h" - -_LIBC_TEXT_SECTION int pthread_attr_setinheritsched(pthread_attr_t *a, int inherit) -{ - if (inherit > 1U) return EINVAL; - a->_a_sched = inherit; - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setschedparam.c b/porting/liteos_m/user/src/thread/pthread_attr_setschedparam.c deleted file mode 100644 index 6f06287d5..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setschedparam.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_setschedparam(pthread_attr_t *restrict a, const struct sched_param *restrict param) -{ - if (param->sched_priority < 0 || param->sched_priority > PTHREAD_PRIORITY_LOWEST) { - return EINVAL; - } - - a->_a_prio = param->sched_priority; - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setschedpolicy.c b/porting/liteos_m/user/src/thread/pthread_attr_setschedpolicy.c deleted file mode 100644 index 7ce06e6da..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setschedpolicy.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_setschedpolicy(pthread_attr_t *a, int policy) -{ - if (policy != SCHED_RR && policy != SCHED_FIFO) { - return EINVAL; - } - - a->_a_policy = policy; - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setscope.c b/porting/liteos_m/user/src/thread/pthread_attr_setscope.c deleted file mode 100644 index 54085aedd..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setscope.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_setscope(pthread_attr_t *a, int scope) -{ - switch (scope) { - case PTHREAD_SCOPE_SYSTEM: - return ENOTSUP; - case PTHREAD_SCOPE_PROCESS: - return 0; - default: - return EINVAL; - } -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setstack.c b/porting/liteos_m/user/src/thread/pthread_attr_setstack.c deleted file mode 100644 index 09092fee4..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setstack.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_setstack(pthread_attr_t *a, void *addr, size_t size) -{ - if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL; - a->_a_stackaddr = (size_t)addr + size; - a->_a_stacksize = size; -} diff --git a/porting/liteos_m/user/src/thread/pthread_attr_setstacksize.c b/porting/liteos_m/user/src/thread/pthread_attr_setstacksize.c deleted file mode 100644 index 75889099c..000000000 --- a/porting/liteos_m/user/src/thread/pthread_attr_setstacksize.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION int pthread_attr_setstacksize(pthread_attr_t *a, size_t size) -{ - if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL; - a->_a_stackaddr = 0; - a->_a_stacksize = size; - return 0; -} diff --git a/porting/liteos_m/user/src/thread/pthread_cancel.c b/porting/liteos_m/user/src/thread/pthread_cancel.c deleted file mode 100644 index 1ac61446d..000000000 --- a/porting/liteos_m/user/src/thread/pthread_cancel.c +++ /dev/null @@ -1,10 +0,0 @@ -#define _GNU_SOURCE -#include "usr_lib_define.h" -#include -#include "pthread_impl.h" -#include "syscall.h" - -_LIBC_TEXT_SECTION int pthread_cancel(pthread_t t) -{ - return ENOSYS; -} diff --git a/porting/liteos_m/user/src/thread/pthread_detach.c b/porting/liteos_m/user/src/thread/pthread_detach.c deleted file mode 100644 index 8a4431150..000000000 --- a/porting/liteos_m/user/src/thread/pthread_detach.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" -#include - -_LIBC_TEXT_SECTION static int __pthread_detach(pthread_t t) -{ - int ret; - - /* If the cas fails, detach state is either already-detached - * or exiting/exited, and pthread_join will trap or cleanup. */ - if (a_cas(&t->detach_state, DT_JOINABLE, DT_DETACHED) != DT_JOINABLE) - return __pthread_join(t, 0); - ret = __syscall(SYS_pthread_set_detach, t->tid); - if (ret) a_swap(&t->detach_state, DT_JOINABLE); - if (ret == ESRCH) ret = 0; - return ret; -} - -weak_alias(__pthread_detach, pthread_detach); diff --git a/porting/liteos_m/user/src/thread/pthread_getschedparam.c b/porting/liteos_m/user/src/thread/pthread_getschedparam.c deleted file mode 100644 index dbfd9edd5..000000000 --- a/porting/liteos_m/user/src/thread/pthread_getschedparam.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" -#include "lock.h" - -_LIBC_TEXT_SECTION int pthread_getschedparam(pthread_t t, int *restrict policy, struct sched_param *restrict param) -{ - int r; - LOCK(t->killlock); - if (!t->tid) { - r = ESRCH; - } else { - r = __syscall(SYS_sched_getparam, t->tid, MUSL_TYPE_THREAD); - if (r >= 0) { - param->sched_priority = r; - r = __syscall(SYS_sched_getscheduler, t->tid, MUSL_TYPE_THREAD); - if (r >= 0) { - *policy = r; - r = 0; - } - } - - if (r < 0) { - r = -r; - } - } - UNLOCK(t->killlock); - return r; -} diff --git a/porting/liteos_m/user/src/thread/pthread_join.c b/porting/liteos_m/user/src/thread/pthread_join.c deleted file mode 100644 index 2efa1c287..000000000 --- a/porting/liteos_m/user/src/thread/pthread_join.c +++ /dev/null @@ -1,41 +0,0 @@ -#define _GNU_SOURCE -#include "usr_lib_define.h" -#include "pthread_impl.h" - -_LIBC_TEXT_SECTION static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at) -{ - int state, cs, r = 0; - unsigned int tid; - pthread_t self = __pthread_self(); - if (t == self) { - r = EINVAL; - goto out; - } - - switch (t->detach_state) { - case DT_JOINABLE: { - r = __syscall(SYS_pthread_join, t->tid); - break; - } - case DT_EXITING: - break; - case DT_DETACHED: - default: - r = EINVAL; - break; - } - -out: - if (r == ESRCH || r == EINVAL) return r; - if (res) *res = t->result; - tid = t->tid; - t->tid = 0; - return __syscall(SYS_pthread_deatch, tid); -} - -_LIBC_TEXT_SECTION int __pthread_join(pthread_t t, void **res) -{ - return __pthread_timedjoin_np(t, res, 0); -} - -weak_alias(__pthread_join, pthread_join); diff --git a/porting/liteos_m/user/src/thread/pthread_self.c b/porting/liteos_m/user/src/thread/pthread_self.c deleted file mode 100644 index 15ae82d90..000000000 --- a/porting/liteos_m/user/src/thread/pthread_self.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" -#include - -_LIBC_TEXT_SECTION pthread_t __pthread_self() -{ - uintptr_t p; - p = __syscall(SYS_get_thread_area); - return (pthread_t)(p - sizeof(struct pthread)); -} - -weak_alias(__pthread_self, pthread_self); diff --git a/porting/liteos_m/user/src/thread/pthread_setschedparam.c b/porting/liteos_m/user/src/thread/pthread_setschedparam.c deleted file mode 100644 index b15728017..000000000 --- a/porting/liteos_m/user/src/thread/pthread_setschedparam.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "usr_lib_define.h" -#include "pthread_impl.h" -#include "lock.h" - -_LIBC_TEXT_SECTION int pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param) -{ - int r; - - if (policy != SCHED_RR && policy != SCHED_FIFO) { - return EINVAL; - } - - if (param->sched_priority < 0 || param->sched_priority > PTHREAD_PRIORITY_LOWEST) { - return EINVAL; - } - - LOCK(t->killlock); - r = !t->tid ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, param->sched_priority, MUSL_TYPE_THREAD); - UNLOCK(t->killlock); - return r; -} diff --git a/scripts/build_lite/Makefile b/scripts/build_lite/Makefile index 1e9a1daa5..b8ec6115c 100644 --- a/scripts/build_lite/Makefile +++ b/scripts/build_lite/Makefile @@ -126,11 +126,23 @@ musl_patch_for_liteos_a_user: nuttx_header_install_for_liteos_a_user musl_patch_for_liteos_a_user: linux_header_install_for_liteos_a_user musl_patch_for_liteos_a_user: optimized_routines_install_for_liteos_a_user musl_patch_for_liteos_a_user: musl_copy_for_liteos_a_user - $(HIDE) cp -rfp $(MUSLDIR)/porting/liteos_a/user/* $ #include #include +#include #include "syscall.h" #include "atomic.h" #include "pthread_impl.h" @@ -197,7 +198,9 @@ static void cleanup(void *ctx) __syscall(SYS_rt_sigqueueinfo, si.si_pid, si.si_signo, &si); } if (sev.sigev_notify == SIGEV_THREAD) { +#ifdef FEATURE_PTHREAD_CANCEL a_store(&__pthread_self()->cancel, 0); +#endif sev.sigev_notify_function(sev.sigev_value); } } @@ -323,16 +326,19 @@ static int submit(struct aiocb *cb, int op) int aio_read(struct aiocb *cb) { + UNSUPPORTED_API_VOID(LITEOS_A); return submit(cb, LIO_READ); } int aio_write(struct aiocb *cb) { + UNSUPPORTED_API_VOID(LITEOS_A); return submit(cb, LIO_WRITE); } int aio_fsync(int op, struct aiocb *cb) { + UNSUPPORTED_API_VOID(LITEOS_A); if (op != O_SYNC && op != O_DSYNC) { errno = EINVAL; return -1; @@ -342,11 +348,13 @@ int aio_fsync(int op, struct aiocb *cb) ssize_t aio_return(struct aiocb *cb) { + UNSUPPORTED_API_VOID(LITEOS_A); return cb->__ret; } int aio_error(const struct aiocb *cb) { + UNSUPPORTED_API_VOID(LITEOS_A); a_barrier(); return cb->__err & 0x7fffffff; } @@ -358,6 +366,7 @@ int aio_cancel(int fd, struct aiocb *cb) struct aio_thread *p; struct aio_queue *q; + UNSUPPORTED_API_VOID(LITEOS_A); /* Unspecified behavior case. Report an error. */ if (cb && fd != cb->aio_fildes) { errno = EINVAL; @@ -377,7 +386,11 @@ int aio_cancel(int fd, struct aiocb *cb) if (cb && cb != p->cb) continue; /* Transition target from running to running-with-waiters */ if (a_cas(&p->running, 1, -1)) { +#ifdef FEATURE_PTHREAD_CANCEL pthread_cancel(p->td); +#else + __syscall(SYS_tkill, p->td->tid, SIGCANCEL); +#endif __wait(&p->running, 0, -1, 1); if (p->err == ECANCELED) ret = AIO_CANCELED; } diff --git a/src/aio/aio_suspend.c b/src/aio/aio_suspend.c index 1c1060e34..792936fd6 100644 --- a/src/aio/aio_suspend.c +++ b/src/aio/aio_suspend.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "atomic.h" #include "pthread_impl.h" #include "aio_impl.h" @@ -13,6 +14,7 @@ int aio_suspend(const struct aiocb *const cbs[], int cnt, const struct timespec int nzcnt = 0; const struct aiocb *cb = 0; + UNSUPPORTED_API_VOID(LITEOS_A); pthread_testcancel(); if (cnt<0) { diff --git a/src/aio/lio_listio.c b/src/aio/lio_listio.c index 0799c15d8..b4bb237c8 100644 --- a/src/aio/lio_listio.c +++ b/src/aio/lio_listio.c @@ -3,6 +3,7 @@ #include #include #include "pthread_impl.h" +#include struct lio_state { struct sigevent *sev; @@ -68,6 +69,7 @@ static void *wait_thread(void *p) int lio_listio(int mode, struct aiocb *restrict const *restrict cbs, int cnt, struct sigevent *restrict sev) { + UNSUPPORTED_API_VOID(LITEOS_A); int i, ret; struct lio_state *st=0; diff --git a/src/conf/confstr.c b/src/conf/confstr.c index 02cb1aa25..604df7058 100644 --- a/src/conf/confstr.c +++ b/src/conf/confstr.c @@ -1,10 +1,12 @@ #include #include #include +#include size_t confstr(int name, char *buf, size_t len) { const char *s = ""; + UNSUPPORTED_API_VOID(LITEOS_A); if (!name) { s = "/bin:/usr/bin"; } else if ((name&~4U)!=1 && name-_CS_POSIX_V6_ILP32_OFF32_CFLAGS>33U) { diff --git a/src/conf/legacy.c b/src/conf/legacy.c index f1d9e3253..ddc863427 100644 --- a/src/conf/legacy.c +++ b/src/conf/legacy.c @@ -1,14 +1,70 @@ #include +#include +#include +#include +#include #include +#include "syscall.h" + +#define _GNU_SOURCE +#include + +int get_cpu_default() { + int i, cnt; + unsigned char set[128] = {1}; + __syscall(SYS_sched_getaffinity, 0, sizeof(set), set); + for ((i=cnt=0); (i < sizeof(set)); i++) + for (; set[i]; (set[i]&=set[i]-1), cnt++); + return cnt; +} int get_nprocs_conf() { - return sysconf(_SC_NPROCESSORS_CONF); + struct dirent *de; + int cnt = 0; + DIR *d = opendir("/sys/devices/system/cpu"); + if (!d) + return get_cpu_default(); + while ((de = readdir(d))) { + if ((de->d_type == DT_DIR) && + (strlen(de->d_name) > 3) && + (de->d_name[0] == 'c') && + (de->d_name[1] == 'p') && + (de->d_name[2] == 'u') && + (isdigit(de->d_name[3]))) + cnt++; + } + closedir(d); + return cnt; } int get_nprocs() { - return sysconf(_SC_NPROCESSORS_ONLN); + int cnt; + FILE* fp = fopen("/sys/devices/system/cpu/online", "re"); + if (fp != NULL) { + char buf[128]; + memset(buf, 0, sizeof(buf)); + if (!fgets(buf, sizeof(buf), fp)) { + return 1; + } + cnt = 0; + char *tmp, *p; + tmp = strtok_r(buf, ",", &p); + while (tmp != NULL) { + char *tok = strstr(tmp, "-"); + if (tok) { + tok++; + cnt += atoi(tok) - atoi(tmp) + 1; + } else { + cnt += 1; + } + tmp = strtok_r(NULL, ",", &p); + } + fclose(fp); + return cnt; + } + return get_cpu_default(); } long get_phys_pages() diff --git a/src/conf/sysconf.c b/src/conf/sysconf.c index 3baaed322..d41b7d0d5 100644 --- a/src/conf/sysconf.c +++ b/src/conf/sysconf.c @@ -24,6 +24,9 @@ long sysconf(int name) { +#ifdef __LITEOS_A__ + return syscall(SYS_sysconf, name); +#else static const short values[] = { [_SC_ARG_MAX] = JT_ARG_MAX, [_SC_CHILD_MAX] = RLIM(NPROC), @@ -194,13 +197,9 @@ long sysconf(int name) case JT_DELAYTIMER_MAX & 255: return DELAYTIMER_MAX; case JT_NPROCESSORS_CONF & 255: - case JT_NPROCESSORS_ONLN & 255: ; - unsigned char set[128] = {1}; - int i, cnt; - __syscall(SYS_sched_getaffinity, 0, sizeof set, set); - for (i=cnt=0; i #include #include +#include #include "crypt_des.h" @@ -11,6 +12,7 @@ void setkey(const char *key) unsigned char bkey[8]; int i, j; + UNSUPPORTED_API_VOID(LITEOS_A); for (i = 0; i < 8; i++) { bkey[i] = 0; for (j = 7; j >= 0; j--, key++) @@ -27,6 +29,7 @@ void encrypt(char *block, int edflag) int i, j; char *p; + UNSUPPORTED_API_VOID(LITEOS_A); p = block; for (i = 0; i < 2; i++) { b[i] = 0; diff --git a/src/ctype/towctrans.c b/src/ctype/towctrans.c index 76d137696..bdccc9677 100644 --- a/src/ctype/towctrans.c +++ b/src/ctype/towctrans.c @@ -1,4 +1,9 @@ #include +#ifdef LINUX_MUSL_SUPPORTED +#include +#include +#include +#endif static const unsigned char tab[]; @@ -60,14 +65,44 @@ static int casemap(unsigned c, int dir) return c0; } +#ifdef LINUX_MUSL_SUPPORTED +static void* g_hmicu_handle = NULL; +static wint_t (*g_hm_ucase_toupper)(wint_t); + +static void* find_hmicu_symbol(const char* symbol_name) { + if (!g_hmicu_handle) { + g_hmicu_handle = dlopen("libhmicuuc.z.so", RTLD_LOCAL); + } + return g_hmicu_handle ? dlsym(g_hmicu_handle, symbol_name) : NULL; +} +#endif + wint_t towlower(wint_t wc) { +#ifdef LINUX_MUSL_SUPPORTED + if (wc < 0x80) { + if (wc >= 'A' && wc <= 'Z') return wc | 0x20; + return wc; + } +#endif return casemap(wc, 0); } wint_t towupper(wint_t wc) { +#ifdef LINUX_MUSL_SUPPORTED + if (wc < 0x80) { + if (wc >= 'a' && wc <= 'z') return wc ^ 0x20; + return wc; + } + if (!g_hm_ucase_toupper) { + typedef wint_t (*f)(wint_t); + g_hm_ucase_toupper = (f)find_hmicu_symbol("ucase_toupper_72"); + } + return g_hm_ucase_toupper ? g_hm_ucase_toupper(wc) : casemap(wc, 1); +#else return casemap(wc, 1); +#endif } wint_t __towupper_l(wint_t c, locale_t l) diff --git a/src/ctype/wcswidth.c b/src/ctype/wcswidth.c index 5c8a5a4da..8fa8586fa 100644 --- a/src/ctype/wcswidth.c +++ b/src/ctype/wcswidth.c @@ -1,8 +1,10 @@ #include +#include int wcswidth(const wchar_t *wcs, size_t n) { int l=0, k=0; + UNSUPPORTED_API_VOID(LITEOS_A); for (; n-- && *wcs && (k = wcwidth(*wcs)) >= 0; l+=k, wcs++); return (k < 0) ? k : l; } diff --git a/src/dirent/__dirent.h b/src/dirent/__dirent.h index 828a5f178..23cf1d6f7 100644 --- a/src/dirent/__dirent.h +++ b/src/dirent/__dirent.h @@ -5,7 +5,12 @@ struct __dirstream int buf_pos; int buf_end; volatile int lock[1]; +#ifdef __LITEOS_A__ + // change buf len from 2048 to 4096 to support read 14 dirs at one readdir syscall + char buf[4096]; +#else /* Any changes to this struct must preserve the property: * offsetof(struct __dirent, buf) % sizeof(off_t) == 0 */ char buf[2048]; +#endif }; diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c index a93141ed3..34b87e5d3 100644 --- a/src/env/__init_tls.c +++ b/src/env/__init_tls.c @@ -8,27 +8,75 @@ #include "libc.h" #include "atomic.h" #include "syscall.h" +#ifdef __LITEOS_A__ +#include "stdio_impl.h" +#include "lock.h" +#endif volatile int __thread_list_lock; +#ifdef __LITEOS_A__ +static void *dummy_tsd[1] = { 0 }; +weak_alias(dummy_tsd, __pthread_tsd_main); +static FILE *volatile dummy_file = 0; +weak_alias(dummy_file, __stdin_used); +weak_alias(dummy_file, __stdout_used); +weak_alias(dummy_file, __stderr_used); + +static void dummy_0() +{ +} +weak_alias(dummy_0, __membarrier_init); + +static void init_file_lock(FILE *f) +{ + if (f && f->lock<0) f->lock = 0; +} +#endif int __init_tp(void *p) { pthread_t td = p; +#ifndef __LITEOS_A__ td->self = td; +#endif int r = __set_thread_area(TP_ADJ(p)); if (r < 0) return -1; if (!r) libc.can_do_threads = 1; +#ifdef __LITEOS_A__ + td->self = td; + libc.threaded = 0; +#endif td->detach_state = DT_JOINABLE; - td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock); +#ifdef __LITEOS_A__ + td->tid = __syscall(SYS_gettid); + td->pid = __syscall(SYS_getpid); +#else + td->tid = td->pid = __syscall(SYS_set_tid_address, &__thread_list_lock); +#endif + td->proc_tid = -1; td->locale = &libc.global_locale; td->robust_list.head = &td->robust_list.head; td->sysinfo = __sysinfo; td->next = td->prev = td; +#ifdef __LITEOS_A__ + td->tsd = (void **)__pthread_tsd_main; + for (FILE *f=*__ofl_lock(); f; f=f->next) + init_file_lock(f); + __ofl_unlock(); + init_file_lock(__stdin_used); + init_file_lock(__stdout_used); + init_file_lock(__stderr_used); + __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8); +#if 0 + __membarrier_init(); +#endif + libc.threaded = 1; +#endif return 0; } static struct builtin_tls { - char c; + char c[8]; struct pthread pt; void *space[16]; } builtin_tls[1]; @@ -52,7 +100,9 @@ void *__copy_tls(unsigned char *mem) for (i=1, p=libc.tls_head; p; i++, p=p->next) { dtv[i] = (uintptr_t)(mem + p->offset) + DTP_OFFSET; - memcpy(mem + p->offset, p->image, p->len); + if (p->image) { + memcpy(mem + p->offset, p->image, p->len); + } } #else dtv = (uintptr_t *)mem; @@ -63,7 +113,9 @@ void *__copy_tls(unsigned char *mem) for (i=1, p=libc.tls_head; p; i++, p=p->next) { dtv[i] = (uintptr_t)(mem - p->offset) + DTP_OFFSET; - memcpy(mem - p->offset, p->image, p->len); + if (p->image) { + memcpy(mem - p->offset, p->image, p->len); + } } #endif dtv[0] = libc.tls_cnt; diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c index c5b277bdc..81fc0c860 100644 --- a/src/env/__libc_start_main.c +++ b/src/env/__libc_start_main.c @@ -6,12 +6,15 @@ #include "syscall.h" #include "atomic.h" #include "libc.h" +#include "pthread_impl.h" static void dummy(void) {} weak_alias(dummy, _init); extern weak hidden void (*const __init_array_start)(void), (*const __init_array_end)(void); - +#ifdef __LITEOS_A__ +extern void parse_argv(int, char **); +#endif static void dummy1(void *p) {} weak_alias(dummy1, __init_ssp); @@ -69,11 +72,22 @@ 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; +#ifdef ENABLE_HWASAN +weak void __hwasan_library_loaded(unsigned long int base, const Elf64_Phdr* phdr, int phnum); +weak void __hwasan_library_unloaded(unsigned long int base, const Elf64_Phdr* phdr, int phnum); +weak void __hwasan_init(); +#endif + int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv, void (*init_dummy)(), void(*fini_dummy)(), void(*ldso_dummy)()) { char **envp = argv+argc+1; +#ifdef ENABLE_HWASAN + libc.load_hook = __hwasan_library_loaded; + libc.unload_hook = __hwasan_library_unloaded; + __hwasan_init(); +#endif /* 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. */ @@ -90,6 +104,16 @@ static int libc_start_main_stage2(int (*main)(int,char **,char **), int argc, ch { char **envp = argv+argc+1; __libc_start_init(); +#ifdef RESERVE_SIGNAL_STACK + pthread_reserve_signal_stack(); +#endif + errno = 0; + +#ifdef __LITEOS_A__ + __sig_init(); + + parse_argv(argc, argv); +#endif /* Pass control to the application */ exit(main(argc, argv, envp)); diff --git a/src/env/getenv.c b/src/env/getenv.c index a90d39cf7..d5fa6710a 100644 --- a/src/env/getenv.c +++ b/src/env/getenv.c @@ -4,10 +4,21 @@ char *getenv(const char *name) { - size_t l = __strchrnul(name, '=') - name; - if (l && !name[l] && __environ) - for (char **e = __environ; *e; e++) - if (!strncmp(name, *e, l) && l[*e] == '=') - return *e + l+1; + if (name == NULL || __environ == NULL) + return 0; + size_t i, l = 0; + const char *np; + char **p, *ep; + for (; *(name + l) && *(name + l) != '='; ++l); + for (p = __environ; (ep = *p) != NULL; ++p) { + for (np = name, i = l; i && *ep; i--) { + if (*ep++ != *np++) { + break; + } + } + if (i == 0 && *ep++ == '=') { + return (ep); + } + } return 0; } diff --git a/src/exit/abort.c b/src/exit/abort.c index f21f458ec..8b227891e 100644 --- a/src/exit/abort.c +++ b/src/exit/abort.c @@ -8,7 +8,19 @@ _Noreturn void abort(void) { +#ifdef __LITEOS_A__ + sigset_t set, pending; + sigemptyset(&set); + sigaddset(&set, SIGABRT); + + sigpending(&pending); + __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, &set, 0, _NSIG / 8); + if (!sigismember(&pending, SIGABRT)) { + raise(SIGABRT); + } +#else raise(SIGABRT); +#endif /* If there was a SIGABRT handler installed and it returned, or if * SIGABRT was blocked or ignored, take an AS-safe lock to prevent @@ -17,8 +29,12 @@ _Noreturn void abort(void) * the default action of abnormal termination. */ __block_all_sigs(0); LOCK(__abort_lock); +#ifdef __LITEOS_A__ + signal(SIGABRT, SIG_DFL); +#else __syscall(SYS_rt_sigaction, SIGABRT, &(struct k_sigaction){.handler = SIG_DFL}, 0, _NSIG/8); +#endif __syscall(SYS_tkill, __pthread_self()->tid, SIGABRT); __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, &(long[_NSIG/(8*sizeof(long))]){1UL<<(SIGABRT-1)}, 0, _NSIG/8); diff --git a/src/exit/exit.c b/src/exit/exit.c index a6869b375..37d3d87ef 100644 --- a/src/exit/exit.c +++ b/src/exit/exit.c @@ -1,7 +1,20 @@ #include #include #include "libc.h" +#ifdef __LITEOS_A__ +#include +#include +#include +#include +#include +#include "syscall.h" +#include +extern bool g_enable_check; +extern void mem_check_deinit(void); +extern void clean_recycle_list(bool clean_all); +pthread_mutex_t __exit_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif static void dummy() { } @@ -26,6 +39,23 @@ weak_alias(libc_exit_fini, __libc_exit_fini); _Noreturn void exit(int code) { +#ifdef __LITEOS_A__ + sigset_t set; + + __block_app_sigs(&set); + + int ret = pthread_mutex_trylock(&__exit_mutex); + if (ret == EBUSY) { + pthread_exit(NULL); + } + + if (g_enable_check) { + check_leak(); + check_heap_integrity(); + mem_check_deinit(); + clean_recycle_list(true); + } +#endif __funcs_on_exit(); __libc_exit_fini(); __stdio_exit(); diff --git a/porting/linux/user/src/exit/atexit.c b/src/exit/linux/atexit.c similarity index 100% rename from porting/linux/user/src/exit/atexit.c rename to src/exit/linux/atexit.c diff --git a/src/fcntl/fcntl.c b/src/fcntl/fcntl.c index d3bff5c48..2db095d9b 100644 --- a/src/fcntl/fcntl.c +++ b/src/fcntl/fcntl.c @@ -11,7 +11,9 @@ int fcntl(int fd, int cmd, ...) va_start(ap, cmd); arg = va_arg(ap, unsigned long); va_end(ap); +#ifndef __LITEOS_A__ if (cmd == F_SETFL) arg |= O_LARGEFILE; +#endif if (cmd == F_SETLKW) return syscall_cp(SYS_fcntl, fd, cmd, (void *)arg); if (cmd == F_GETOWN) { struct f_owner_ex ex; diff --git a/src/fcntl/open.c b/src/fcntl/open.c index 1d817a2d6..196d10435 100644 --- a/src/fcntl/open.c +++ b/src/fcntl/open.c @@ -14,9 +14,10 @@ int open(const char *filename, int flags, ...) } int fd = __sys_open_cp(filename, flags, mode); +#ifndef LINUX_MUSL_SUPPORTED if (fd>=0 && (flags & O_CLOEXEC)) __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); - +#endif return __syscall_ret(fd); } diff --git a/src/fcntl/posix_fadvise.c b/src/fcntl/posix_fadvise.c index 75b8e1aed..82b61f4de 100644 --- a/src/fcntl/posix_fadvise.c +++ b/src/fcntl/posix_fadvise.c @@ -1,8 +1,10 @@ #include #include "syscall.h" +#include int posix_fadvise(int fd, off_t base, off_t len, int advice) { + UNSUPPORTED_API_VOID(LITEOS_A); #if defined(SYSCALL_FADVISE_6_ARG) /* Some archs, at least arm and powerpc, have the syscall * arguments reordered to avoid needing 7 argument registers diff --git a/porting/linux/user/src/fortify/fortify.c b/src/fortify/linux/fortify.c similarity index 100% rename from porting/linux/user/src/fortify/fortify.c rename to src/fortify/linux/fortify.c diff --git a/porting/linux/user/src/gwp_asan/gwp_asan.c b/src/gwp_asan/linux/gwp_asan.c similarity index 100% rename from porting/linux/user/src/gwp_asan/gwp_asan.c rename to src/gwp_asan/linux/gwp_asan.c diff --git a/porting/linux/user/src/gwp_asan/gwp_asan.h b/src/gwp_asan/linux/gwp_asan.h similarity index 100% rename from porting/linux/user/src/gwp_asan/gwp_asan.h rename to src/gwp_asan/linux/gwp_asan.h diff --git a/porting/linux/user/src/hilog/hilog_adapter.c b/src/hilog/linux/hilog_adapter.c similarity index 100% rename from porting/linux/user/src/hilog/hilog_adapter.c rename to src/hilog/linux/hilog_adapter.c diff --git a/porting/linux/user/src/hilog/hilog_common.h b/src/hilog/linux/hilog_common.h similarity index 100% rename from porting/linux/user/src/hilog/hilog_common.h rename to src/hilog/linux/hilog_common.h diff --git a/porting/linux/user/src/hilog/output_p.inl b/src/hilog/linux/output_p.inl similarity index 100% rename from porting/linux/user/src/hilog/output_p.inl rename to src/hilog/linux/output_p.inl diff --git a/porting/linux/user/src/hilog/vsnprintf_s_p.c b/src/hilog/linux/vsnprintf_s_p.c similarity index 100% rename from porting/linux/user/src/hilog/vsnprintf_s_p.c rename to src/hilog/linux/vsnprintf_s_p.c diff --git a/porting/linux/user/src/hilog/vsnprintf_s_p.h b/src/hilog/linux/vsnprintf_s_p.h similarity index 100% rename from porting/linux/user/src/hilog/vsnprintf_s_p.h rename to src/hilog/linux/vsnprintf_s_p.h diff --git a/porting/linux/user/src/hook/common_def.h b/src/hook/linux/common_def.h similarity index 100% rename from porting/linux/user/src/hook/common_def.h rename to src/hook/linux/common_def.h diff --git a/porting/linux/user/src/hook/malloc_common.c b/src/hook/linux/malloc_common.c similarity index 100% rename from porting/linux/user/src/hook/malloc_common.c rename to src/hook/linux/malloc_common.c diff --git a/porting/linux/user/src/hook/memory_trace.c b/src/hook/linux/memory_trace.c similarity index 100% rename from porting/linux/user/src/hook/memory_trace.c rename to src/hook/linux/memory_trace.c diff --git a/porting/linux/user/src/hook/memory_trace.h b/src/hook/linux/memory_trace.h similarity index 100% rename from porting/linux/user/src/hook/memory_trace.h rename to src/hook/linux/memory_trace.h diff --git a/porting/linux/user/src/hook/musl_malloc.h b/src/hook/linux/musl_malloc.h similarity index 100% rename from porting/linux/user/src/hook/musl_malloc.h rename to src/hook/linux/musl_malloc.h diff --git a/porting/linux/user/src/hook/musl_malloc_dispatch.h b/src/hook/linux/musl_malloc_dispatch.h similarity index 100% rename from porting/linux/user/src/hook/musl_malloc_dispatch.h rename to src/hook/linux/musl_malloc_dispatch.h diff --git a/porting/linux/user/src/hook/musl_malloc_dispatch_table.h b/src/hook/linux/musl_malloc_dispatch_table.h similarity index 100% rename from porting/linux/user/src/hook/musl_malloc_dispatch_table.h rename to src/hook/linux/musl_malloc_dispatch_table.h diff --git a/porting/linux/user/src/hook/musl_preinit.c b/src/hook/linux/musl_preinit.c similarity index 100% rename from porting/linux/user/src/hook/musl_preinit.c rename to src/hook/linux/musl_preinit.c diff --git a/porting/linux/user/src/hook/musl_preinit_common.c b/src/hook/linux/musl_preinit_common.c similarity index 100% rename from porting/linux/user/src/hook/musl_preinit_common.c rename to src/hook/linux/musl_preinit_common.c diff --git a/porting/linux/user/src/hook/musl_preinit_common.h b/src/hook/linux/musl_preinit_common.h similarity index 100% rename from porting/linux/user/src/hook/musl_preinit_common.h rename to src/hook/linux/musl_preinit_common.h diff --git a/porting/linux/user/src/hook/musl_socket.h b/src/hook/linux/musl_socket.h similarity index 100% rename from porting/linux/user/src/hook/musl_socket.h rename to src/hook/linux/musl_socket.h diff --git a/porting/linux/user/src/hook/musl_socket_dispatch.h b/src/hook/linux/musl_socket_dispatch.h similarity index 100% rename from porting/linux/user/src/hook/musl_socket_dispatch.h rename to src/hook/linux/musl_socket_dispatch.h diff --git a/porting/linux/user/src/hook/musl_socket_preinit.c b/src/hook/linux/musl_socket_preinit.c similarity index 100% rename from porting/linux/user/src/hook/musl_socket_preinit.c rename to src/hook/linux/musl_socket_preinit.c diff --git a/porting/linux/user/src/hook/musl_socket_preinit_common.c b/src/hook/linux/musl_socket_preinit_common.c similarity index 100% rename from porting/linux/user/src/hook/musl_socket_preinit_common.c rename to src/hook/linux/musl_socket_preinit_common.c diff --git a/porting/linux/user/src/hook/musl_socket_preinit_common.h b/src/hook/linux/musl_socket_preinit_common.h similarity index 100% rename from porting/linux/user/src/hook/musl_socket_preinit_common.h rename to src/hook/linux/musl_socket_preinit_common.h diff --git a/porting/linux/user/src/hook/socket_common.c b/src/hook/linux/socket_common.c similarity index 100% rename from porting/linux/user/src/hook/socket_common.c rename to src/hook/linux/socket_common.c diff --git a/porting/liteos_m/user/hook/los_usr_libc.c b/src/hook/liteos_m/los_usr_libc.c similarity index 100% rename from porting/liteos_m/user/hook/los_usr_libc.c rename to src/hook/liteos_m/los_usr_libc.c diff --git a/porting/liteos_m/user/src/include/features.h b/src/include/liteos_m/features.h similarity index 100% rename from porting/liteos_m/user/src/include/features.h rename to src/include/liteos_m/features.h diff --git a/porting/linux/user/src/info/application_target_sdk_version.c b/src/info/linux/application_target_sdk_version.c similarity index 95% rename from porting/linux/user/src/info/application_target_sdk_version.c rename to src/info/linux/application_target_sdk_version.c index 00906c0f0..e00a23099 100644 --- a/porting/linux/user/src/info/application_target_sdk_version.c +++ b/src/info/linux/application_target_sdk_version.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/porting/linux/user/src/time/time.c b/src/info/linux/application_target_sdk_version.h similarity index 44% rename from porting/linux/user/src/time/time.c rename to src/info/linux/application_target_sdk_version.h index 679c14282..d8749e75c 100644 --- a/porting/linux/user/src/time/time.c +++ b/src/info/linux/application_target_sdk_version.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,42 +13,32 @@ * limitations under the License. */ -#include -#include -#include -#include "syscall.h" -#include "atomic.h" +#ifndef _INFO_APPLICATION_TARGET_SDK_VERSION_H +#define _INFO_APPLICATION_TARGET_SDK_VERSION_H -#ifdef VDSO_TIME_SYM - -static void *volatile vdso_time; +#ifdef __cplusplus +extern "C" { +#endif -static time_t time_init(time_t *t) -{ - __get_vdso_info(); - void *p = __get_vdso_addr(VDSO_TIME_VER, VDSO_TIME_SYM); - time_t (*f)(time_t *) = - (time_t (*)(time_t *))p; - a_cas_p(&vdso_time, (void *)time_init, p); - return f ? f(t) : -ENOSYS; -} +#define SDK_VERSION_FUTURE 9999 +#define SDK_VERSION_7 7 +#define SDK_VERSION_8 8 +#define SDK_VERSION_9 9 -static void *volatile vdso_time = (void *)time_init; +/** + * @brief Get the target sdk version number of the application. + * @return The target sdk version number. + */ +int get_application_target_sdk_version(void); -#endif +/** + * @brief Set the target sdk version number of the application. + * @param target The target sdk version number. + */ +void set_application_target_sdk_version(int target); -time_t time(time_t *t) -{ -#ifdef VDSO_TIME_SYM - time_t (*f)(time_t *) = - (time_t (*)(time_t *))vdso_time; - if (f) { - return f(t); - } +#ifdef __cplusplus +} #endif - struct timespec ts; - __clock_gettime(CLOCK_REALTIME, &ts); - if (t) *t = ts.tv_sec; - return ts.tv_sec; -} +#endif // _INFO_APPLICATION_TARGET_SDK_VERSION_H diff --git a/porting/linux/user/src/info/device_api_version.c b/src/info/linux/device_api_version.c similarity index 94% rename from porting/linux/user/src/info/device_api_version.c rename to src/info/linux/device_api_version.c index 86208c189..81efaa098 100644 --- a/porting/linux/user/src/info/device_api_version.c +++ b/src/info/linux/device_api_version.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,4 +22,4 @@ int get_device_api_version(void) // depend subsystem of syspara support the interface of get system property return API_VERSION_DEFAULT; -} \ No newline at end of file +} diff --git a/porting/linux/user/src/linux/reboot.c b/src/info/linux/device_api_version.h similarity index 60% rename from porting/linux/user/src/linux/reboot.c rename to src/info/linux/device_api_version.h index ed8c5a730..37cf04e46 100644 --- a/porting/linux/user/src/linux/reboot.c +++ b/src/info/linux/device_api_version.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,11 +13,21 @@ * limitations under the License. */ -#include -#include "syscall.h" +#ifndef _INFO_DEVICE_API_VERSION_H +#define _INFO_DEVICE_API_VERSION_H -int __reboot(int type) -{ - return syscall(SYS_reboot, RB_MAGIC1, RB_MAGIC2, type); +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Get the api version number of the device. + * @return The api version number of the device. + */ +int get_device_api_version(void); + +#ifdef __cplusplus } -weak_alias(__reboot, reboot); \ No newline at end of file +#endif + +#endif // _INFO_DEVICE_API_VERSION_H diff --git a/porting/linux/user/src/info/fatal_message.c b/src/info/linux/fatal_message.c similarity index 97% rename from porting/linux/user/src/info/fatal_message.c rename to src/info/linux/fatal_message.c index 5b6594160..7492d50ef 100644 --- a/porting/linux/user/src/info/fatal_message.c +++ b/src/info/linux/fatal_message.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/porting/linux/user/src/signal/siginterrupt.c b/src/info/linux/fatal_message.h similarity index 50% rename from porting/linux/user/src/signal/siginterrupt.c rename to src/info/linux/fatal_message.h index d1acd0a93..4b3664984 100644 --- a/porting/linux/user/src/signal/siginterrupt.c +++ b/src/info/linux/fatal_message.h @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,22 +13,35 @@ * limitations under the License. */ -#include -#include +#ifndef _INFO_FATAL_MESSAGE_H +#define _INFO_FATAL_MESSAGE_H -extern int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old); +#include +#include -int siginterrupt(int sig, int flag) -{ - if (sig-32U < 3 || sig-1U >= _NSIG-1) { - errno = EINVAL; - return -1; - } - struct sigaction sa; +#ifdef __cplusplus +extern "C" { +#endif - __libc_sigaction(sig, 0, &sa); - if (flag) sa.sa_flags &= ~SA_RESTART; - else sa.sa_flags |= SA_RESTART; +typedef struct fatal_msg { + size_t size; + char msg[0]; +} fatal_msg_t; - return __libc_sigaction(sig, &sa, 0); +/** + * @brief Set up fatal message + * @param msg The fatal message + */ +void set_fatal_message(const char *msg); + +/** + * @brief Get the set fatal message + * @return Address of fatal message + */ +fatal_msg_t *get_fatal_message(void); + +#ifdef __cplusplus } +#endif + +#endif // _INFO_FATAL_MESSAGE_H diff --git a/src/internal/dynlink.h b/src/internal/dynlink.h index 51c0639ff..c3cf404fe 100644 --- a/src/internal/dynlink.h +++ b/src/internal/dynlink.h @@ -6,11 +6,29 @@ #include #include #include +#ifdef LINUX_MUSL_SUPPORTED +#include +#include +#include +#include "libc.h" +#ifdef __HISPARK_LINUX__ +#include "../../ldso/namespace.h" +#else +#include "../../ldso/linux/namespace.h" +#endif +#endif #if UINTPTR_MAX == 0xffffffff typedef Elf32_Ehdr Ehdr; typedef Elf32_Phdr Phdr; typedef Elf32_Sym Sym; +#ifdef LINUX_MUSL_SUPPORTED +typedef Elf32_Shdr Shdr; +typedef Elf32_Verdaux Verdaux; +typedef Elf32_Verdef Verdef; +typedef Elf32_Vernaux Vernaux; +typedef Elf32_Verneed Verneed; +#endif #define R_TYPE(x) ((x)&255) #define R_SYM(x) ((x)>>8) #define R_INFO ELF32_R_INFO @@ -18,6 +36,13 @@ typedef Elf32_Sym Sym; typedef Elf64_Ehdr Ehdr; typedef Elf64_Phdr Phdr; typedef Elf64_Sym Sym; +#ifdef LINUX_MUSL_SUPPORTED +typedef Elf64_Shdr Shdr; +typedef Elf64_Verdaux Verdaux; +typedef Elf64_Verdef Verdef; +typedef Elf64_Vernaux Vernaux; +typedef Elf64_Verneed Verneed; +#endif #define R_TYPE(x) ((x)&0x7fffffff) #define R_SYM(x) ((x)>>32) #define R_INFO ELF64_R_INFO @@ -44,6 +69,117 @@ enum { REL_FUNCDESC, REL_FUNCDESC_VAL, }; +#ifdef LINUX_MUSL_SUPPORTED +struct td_index { + size_t args[2]; + struct td_index *next; +}; + +struct verinfo { + const char *s; + const char *v; + bool use_vna_hash; + uint32_t vna_hash; +}; + +struct sym_info_pair { + uint_fast32_t sym_h; + uint32_t sym_l; +}; + +struct dso { +#if DL_FDPIC + struct fdpic_loadmap *loadmap; +#else + unsigned char *base; +#endif + char *name; + size_t *dynv; + struct dso *next, *prev; + /* add namespace */ + ns_t *namespace; + int cache_sym_index; + struct dso *cache_dso; + Sym *cache_sym; + Phdr *phdr; + int phnum; + size_t phentsize; + Sym *syms; + Elf_Symndx *hashtab; + uint32_t *ghashtab; + int16_t *versym; + Verdef *verdef; + Verneed *verneed; + char *strings; + struct dso *syms_next, *lazy_next; + size_t *lazy, lazy_cnt; + unsigned char *map; + size_t map_len; + dev_t dev; + ino_t ino; + uint64_t file_offset; + pthread_t ctor_visitor; + char *rpath_orig, *rpath; + struct tls_module tls; + size_t tls_id; + size_t relro_start, relro_end; + uintptr_t *new_dtv; + unsigned char *new_tls; + struct td_index *td_index; + struct dso *fini_next; + char *shortname; +#if DL_FDPIC + unsigned char *base; +#else + struct fdpic_loadmap *loadmap; +#endif + struct funcdesc { + void *addr; + size_t *got; + } *funcdescs; + size_t *got; + struct dso **deps, *needed_by; + uint16_t ndeps_direct; + uint16_t next_dep; + uint16_t parents_count; + uint16_t parents_capacity; + struct dso **parents; + struct dso **reloc_can_search_dso_list; + uint16_t reloc_can_search_dso_count; + uint16_t reloc_can_search_dso_capacity; + /* mark the dso status */ + uint32_t flags; + uint8_t nr_dlopen; + bool is_global; + bool is_reloc_head_so_dep; + char relocated; + char constructed; + char kernel_mapped; + char mark; + char bfs_built; + char runtime_loaded; + char by_dlopen; + bool is_mapped_to_shadow; + struct dso_debug_info *debug_info; + char buf[]; +}; + +struct dso_debug_info { +#if DL_FDPIC + struct fdpic_loadmap *loadmap; +#else + unsigned char *base; +#endif + char *name; + size_t *dynv; + struct dso_debug_info *next, *prev; +}; + +struct symdef { + Sym *sym; + struct dso *dso; +}; +#endif struct fdpic_loadseg { uintptr_t addr, p_vaddr, p_memsz; @@ -93,12 +229,50 @@ struct fdpic_dummy_loadmap { #endif #define AUX_CNT 32 -#define DYN_CNT 32 +#ifdef LINUX_MUSL_SUPPORTED +#define DYN_CNT 37 + +#define DT_ANDROID_REL (DT_LOOS + 2) +#define DT_ANDROID_RELSZ (DT_LOOS + 3) + +#define DT_ANDROID_RELA (DT_LOOS + 4) +#define DT_ANDROID_RELASZ (DT_LOOS + 5) + +#define ANDROID_REL_SIGN_SIZE 4 + +#define RELOCATION_GROUPED_BY_INFO_FLAG 1 +#define RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG 2 +#define RELOCATION_GROUPED_BY_ADDEND_FLAG 4 +#define RELOCATION_GROUP_HAS_ADDEND_FLAG 8 typedef void (*stage2_func)(unsigned char *, size_t *); +#if DL_FDPIC +void *laddr(const struct dso *p, size_t v); +#endif + +#ifdef UNIT_TEST_STATIC + #define UT_STATIC +#else + #define UT_STATIC static +#endif + +void *addr2dso(size_t a); +UT_STATIC size_t count_syms(struct dso *p); +struct sym_info_pair gnu_hash(const char *s0); +struct symdef find_sym_impl( + struct dso *dso, struct verinfo *verinfo, struct sym_info_pair s_info_p, int need_def, ns_t *ns); + hidden void *__dlsym(void *restrict, const char *restrict, void *restrict); +hidden void *__dlvsym(void *restrict, const char *restrict, const char *restrict, void *restrict); +hidden int __dlclose(void *p); +#else +#define DYN_CNT 32 + +typedef void (*stage2_func)(unsigned char *, size_t *); +hidden void *__dlsym(void *restrict, const char *restrict, void *restrict); +#endif hidden void __dl_seterr(const char *, ...); hidden int __dl_invalid_handle(void *); hidden void __dl_vseterr(const char *, va_list); diff --git a/src/internal/libc.h b/src/internal/libc.h index 619bba861..571782a1c 100644 --- a/src/internal/libc.h +++ b/src/internal/libc.h @@ -4,11 +4,13 @@ #include #include #include +#include +#include struct __locale_map; struct __locale_struct { - const struct __locale_map *cat[6]; + const struct __locale_map *cat[LC_ALL]; }; struct tls_module { @@ -28,6 +30,13 @@ struct __libc { size_t tls_size, tls_align, tls_cnt; size_t page_size; struct __locale_struct global_locale; +#ifdef ENABLE_HWASAN + void (*load_hook)(unsigned long int base, const Elf64_Phdr* phdr, int phnum); + void (*unload_hook)(unsigned long int base, const Elf64_Phdr* phdr, int phnum); +#endif +#ifdef __LITEOS_A__ + int exit; +#endif }; #ifndef PAGE_SIZE @@ -45,6 +54,10 @@ hidden void __funcs_on_exit(void); hidden void __funcs_on_quick_exit(void); hidden void __libc_exit_fini(void); hidden void __fork_handler(int); +#ifdef __LITEOS_A__ +hidden void __sig_init(void); +hidden void arm_do_signal(int); +#endif extern hidden size_t __hwcap; extern hidden size_t __sysinfo; diff --git a/porting/linux/user/src/internal/hilog_adapter.h b/src/internal/linux/hilog_adapter.h similarity index 97% rename from porting/linux/user/src/internal/hilog_adapter.h rename to src/internal/linux/hilog_adapter.h index 98d4d7939..84fc5cc27 100644 --- a/porting/linux/user/src/internal/hilog_adapter.h +++ b/src/internal/linux/hilog_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/porting/linux/user/src/internal/malloc_config.h b/src/internal/linux/malloc_config.h similarity index 92% rename from porting/linux/user/src/internal/malloc_config.h rename to src/internal/linux/malloc_config.h index b9bcbb538..17d46fb60 100644 --- a/porting/linux/user/src/internal/malloc_config.h +++ b/src/internal/linux/malloc_config.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -40,3 +40,7 @@ #endif #endif // MALLOC_CONFIG_H + +#define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1) + +#endif diff --git a/porting/linux/user/src/internal/malloc_random.h b/src/internal/linux/malloc_random.h similarity index 91% rename from porting/linux/user/src/internal/malloc_random.h rename to src/internal/linux/malloc_random.h index 2edfa072d..82625f378 100644 --- a/porting/linux/user/src/internal/malloc_random.h +++ b/src/internal/linux/malloc_random.h @@ -1,20 +1,20 @@ -/* - * Copyright (C) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "malloc_config.h" - -void* encode_ptr(void *ptr, void *key); -void* next_key(void); +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "malloc_config.h" + +void* encode_ptr(void *ptr, void *key); +void* next_key(void); diff --git a/porting/linux/user/src/internal/musl_log.h b/src/internal/linux/musl_log.h similarity index 96% rename from porting/linux/user/src/internal/musl_log.h rename to src/internal/linux/musl_log.h index 62e15d3f7..54881e1b3 100644 --- a/porting/linux/user/src/internal/musl_log.h +++ b/src/internal/linux/musl_log.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/porting/linux/user/src/internal/network_conf_function.c b/src/internal/linux/network_conf_function.c similarity index 100% rename from porting/linux/user/src/internal/network_conf_function.c rename to src/internal/linux/network_conf_function.c diff --git a/porting/linux/user/src/internal/network_conf_function.h b/src/internal/linux/network_conf_function.h similarity index 100% rename from porting/linux/user/src/internal/network_conf_function.h rename to src/internal/linux/network_conf_function.h diff --git a/porting/linux/user/src/internal/proc_xid_impl.h b/src/internal/linux/proc_xid_impl.h similarity index 100% rename from porting/linux/user/src/internal/proc_xid_impl.h rename to src/internal/linux/proc_xid_impl.h diff --git a/porting/linux/user/src/internal/services.h b/src/internal/linux/services.h similarity index 100% rename from porting/linux/user/src/internal/services.h rename to src/internal/linux/services.h diff --git a/porting/linux/user/src/internal/vdso.c b/src/internal/linux/vdso.c similarity index 98% rename from porting/linux/user/src/internal/vdso.c rename to src/internal/linux/vdso.c index 3cb1d25b6..a080f2509 100644 --- a/porting/linux/user/src/internal/vdso.c +++ b/src/internal/linux/vdso.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/porting/liteos_m/user/src/internal/atomic.h b/src/internal/liteos_m/atomic.h similarity index 100% rename from porting/liteos_m/user/src/internal/atomic.h rename to src/internal/liteos_m/atomic.h diff --git a/porting/liteos_m/user/src/internal/futex.h b/src/internal/liteos_m/futex.h similarity index 100% rename from porting/liteos_m/user/src/internal/futex.h rename to src/internal/liteos_m/futex.h diff --git a/porting/liteos_m/user/src/libc_init.c b/src/internal/liteos_m/libc_init.c similarity index 100% rename from porting/liteos_m/user/src/libc_init.c rename to src/internal/liteos_m/libc_init.c diff --git a/porting/liteos_m/user/src/libc_start.c b/src/internal/liteos_m/libc_start.c similarity index 100% rename from porting/liteos_m/user/src/libc_start.c rename to src/internal/liteos_m/libc_start.c diff --git a/porting/liteos_m/user/src/internal/lock.h b/src/internal/liteos_m/lock.h similarity index 100% rename from porting/liteos_m/user/src/internal/lock.h rename to src/internal/liteos_m/lock.h diff --git a/porting/liteos_m/user/src/internal/pthread_impl.h b/src/internal/liteos_m/pthread_impl.h similarity index 100% rename from porting/liteos_m/user/src/internal/pthread_impl.h rename to src/internal/liteos_m/pthread_impl.h diff --git a/porting/liteos_m/user/src/internal/syscall.h b/src/internal/liteos_m/syscall.h similarity index 100% rename from porting/liteos_m/user/src/internal/syscall.h rename to src/internal/liteos_m/syscall.h diff --git a/src/internal/liteos_m/unsupported_api.h b/src/internal/liteos_m/unsupported_api.h new file mode 100644 index 000000000..658a1d62b --- /dev/null +++ b/src/internal/liteos_m/unsupported_api.h @@ -0,0 +1,33 @@ +#ifndef UNSUPPORTED_API_H +#define UNSUPPORTED_API_H + +#include +#include + +#define LITEOS_A "liteos_a" +#define LINUX "linux" + +static inline void unsupported_api(const char *func) +{ + fprintf(stderr, "[ERR]Unsupported API %s\n", func); +} + +static inline int unsupported_api_check(const char *osname) +{ +#ifdef __LITEOS_A__ + if (!strcmp(osname, LITEOS_A)) { + return 1; + } +#elif defined(LINUX_MUSL_SUPPORTED) + if (!strcmp(osname, LINUX)) { + return 1; + } +#endif + return 0; +} + +#define UNSUPPORTED_API_VOID(osname) \ + if (unsupported_api_check(osname) > 0) { \ + fprintf(stderr, "[ERR]Unsupported API %s\n", __FUNCTION__); \ + } +#endif diff --git a/src/internal/locale_impl.h b/src/internal/locale_impl.h index 4431a92eb..a843f0e73 100644 --- a/src/internal/locale_impl.h +++ b/src/internal/locale_impl.h @@ -7,12 +7,15 @@ #include "pthread_impl.h" #define LOCALE_NAME_MAX 23 +#define VALID 2 +#define INVALID 1 struct __locale_map { const void *map; size_t map_size; char name[LOCALE_NAME_MAX+1]; const struct __locale_map *next; + char flag; }; extern hidden volatile int __locale_lock[1]; diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h index de2b9d8b4..0abed875a 100644 --- a/src/internal/pthread_impl.h +++ b/src/internal/pthread_impl.h @@ -14,6 +14,7 @@ #include "pthread_arch.h" #define pthread __pthread +#define TLS_RESERVE_SLOT 15 struct pthread { /* Part 1 -- these fields may be external or @@ -31,37 +32,51 @@ struct pthread { uintptr_t canary; #endif - /* Part 2 -- implementation details, non-ABI. */ - int tid; - int errno_val; - volatile int detach_state; - volatile int cancel; - volatile unsigned char canceldisable, cancelasync; - unsigned char tsd_used:1; - unsigned char dlerror_flag:1; - unsigned char *map_base; - size_t map_size; - void *stack; - size_t stack_size; - size_t guard_size; - void *result; - struct __ptcb *cancelbuf; - void **tsd; - struct { - volatile void *volatile head; - long off; - volatile void *volatile pending; - } robust_list; + /* Part 2 -- implementation details, non-ABI. */ + int tid; + int pid; + int proc_tid; + int errno_val; + volatile int detach_state; +#ifdef FEATURE_PTHREAD_CANCEL + volatile int cancel; + volatile unsigned char canceldisable, cancelasync; +#endif + unsigned char tsd_used:1; + unsigned char dlerror_flag:1; + unsigned char *map_base; + size_t map_size; + void *stack; + size_t stack_size; + size_t guard_size; + void *result; + struct __ptcb *cancelbuf; + void **tsd; + struct { + volatile void *volatile head; + long off; + volatile void *volatile pending; + } robust_list; int h_errno_val; - volatile int timer_id; - locale_t locale; - volatile int killlock[1]; - char *dlerror_buf; - void *stdio_locks; + volatile int timer_id; + locale_t locale; + volatile int killlock[1]; + char *dlerror_buf; + void *stdio_locks; +#ifdef RESERVE_SIGNAL_STACK + void *signal_stack; +#endif /* Part 3 -- the positions of these fields relative to * the end of the structure is external and internal ABI. */ #ifdef TLS_ABOVE_TP + /* The tls_slots will be accessed by kernel, so don't use it. + * To solve the problem that the kernel isn't synchronized with the musl, + * so make pre/post reserved slots for musl. + * pre-reserved : tls_slots[0-4] + * kernel used : tls_slots[5-9] + * post-reserved : tls_slot[10-14] */ + void *tls_slots[TLS_RESERVE_SLOT]; uintptr_t canary; uintptr_t *dtv; #endif @@ -83,11 +98,17 @@ enum { #define _a_sched __u.__i[3*__SU+1] #define _a_policy __u.__i[3*__SU+2] #define _a_prio __u.__i[3*__SU+3] +#ifdef __LITEOS_A__ +#define _a_runtime __u.__i[3*__SU+3] +#define _a_deadline __u.__i[3*__SU+4] +#define _a_period __u.__i[3*__SU+5] +#endif #define _m_type __u.__i[0] #define _m_lock __u.__vi[1] #define _m_waiters __u.__vi[2] #define _m_prev __u.__p[3] #define _m_next __u.__p[4] +#define _m_clock __u.__i[4] #define _m_count __u.__i[5] #define _c_shared __u.__p[0] #define _c_seq __u.__vi[2] @@ -99,6 +120,7 @@ enum { #define _rw_lock __u.__vi[0] #define _rw_waiters __u.__vi[1] #define _rw_shared __u.__i[2] +#define _rw_clock __u.__i[4] #define _b_lock __u.__vi[0] #define _b_waiters __u.__vi[1] #define _b_limit __u.__i[2] @@ -116,11 +138,15 @@ enum { #ifdef TLS_ABOVE_TP #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + TP_OFFSET) +#ifndef __LITEOS_A__ #define __pthread_self() ((pthread_t)(__get_tp() - sizeof(struct __pthread) - TP_OFFSET)) +#endif #else #define TP_ADJ(p) (p) +#ifndef __LITEOS_A__ #define __pthread_self() ((pthread_t)__get_tp()) #endif +#endif #ifndef tls_mod_off_t #define tls_mod_off_t size_t @@ -132,11 +158,11 @@ enum { #define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 }) #define SIGPT_SET \ - ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ - [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) }) + ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ + [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) }) #define SIGTIMER_SET \ - ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ - 0x80000000 }) + ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ + 0x80000000 }) void *__tls_get_addr(tls_mod_off_t *); hidden int __init_tp(void *); @@ -158,6 +184,9 @@ extern hidden void *__pthread_tsd_main[]; extern hidden volatile int __eintr_valid_flag; hidden int __clone(int (*)(void *), void *, int, void *, ...); +#ifdef __LITEOS_A__ +hidden int __thread_clone(int (*func)(void *), int flags, struct pthread *thread, unsigned char *sp); +#endif hidden int __set_thread_area(void *); hidden int __libc_sigaction(int, const struct sigaction *, struct sigaction *); hidden void __unmapself(void *, size_t); @@ -167,18 +196,55 @@ hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec hidden void __wait(volatile int *, volatile int *, int, int); static inline void __wake(volatile void *addr, int cnt, int priv) { - if (priv) priv = FUTEX_PRIVATE; - if (cnt<0) cnt = INT_MAX; - __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS || - __syscall(SYS_futex, addr, FUTEX_WAKE, cnt); + if (priv) priv = FUTEX_PRIVATE; + if (cnt<0) cnt = INT_MAX; + __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS || + __syscall(SYS_futex, addr, FUTEX_WAKE, cnt); } static inline void __futexwait(volatile void *addr, int val, int priv) { - if (priv) priv = FUTEX_PRIVATE; + if (priv) priv = FUTEX_PRIVATE; +#ifdef __LITEOS_A__ + __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0xffffffffu) != -ENOSYS || + __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0xffffffffu); +#else __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS || __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0); +#endif +} + +#define MS_PER_S 1000 +#define US_PER_S 1000000 +static inline void __timespec_from_ms(struct timespec* ts, const unsigned ms) +{ + if (ts == NULL) { + return; + } + ts->tv_sec = ms / MS_PER_S; + ts->tv_nsec = (ms % MS_PER_S) * US_PER_S; +} + +#define NS_PER_S 1000000000 +static inline void __absolute_timespec_from_timespec(struct timespec *abs_ts, + const struct timespec *ts, clockid_t clock) +{ + if (abs_ts == NULL || ts == NULL) { + return; + } + clock_gettime(clock, abs_ts); + abs_ts->tv_sec += ts->tv_sec; + abs_ts->tv_nsec += ts->tv_nsec; + if (abs_ts->tv_nsec >= NS_PER_S) { + abs_ts->tv_nsec -= NS_PER_S; + abs_ts->tv_sec++; + } } +#ifdef RESERVE_SIGNAL_STACK +hidden void pthread_reserve_signal_stack(); +hidden void pthread_release_signal_stack(); +#endif + hidden void __acquire_ptc(void); hidden void __release_ptc(void); hidden void __inhibit_ptc(void); @@ -186,6 +252,7 @@ hidden void __inhibit_ptc(void); hidden void __tl_lock(void); hidden void __tl_unlock(void); hidden void __tl_sync(pthread_t); +hidden struct pthread* __pthread_list_find(pthread_t, const char*); extern hidden volatile int __thread_list_lock; @@ -194,12 +261,29 @@ extern hidden volatile int __abort_lock[1]; extern hidden unsigned __default_stacksize; extern hidden unsigned __default_guardsize; +#ifdef TARGET_STACK_SIZE +#define DEFAULT_STACK_SIZE TARGET_STACK_SIZE +#else #define DEFAULT_STACK_SIZE 131072 +#endif + +#ifdef TARGET_GUARD_SIZE +#define DEFAULT_GUARD_SIZE TARGET_GUARD_SIZE +#else #define DEFAULT_GUARD_SIZE 8192 +#endif #define DEFAULT_STACK_MAX (8<<20) #define DEFAULT_GUARD_MAX (1<<20) #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1) +#ifdef __LITEOS_A__ +#define MUSL_TYPE_THREAD (-1) +#define MUSL_TYPE_PROCESS (0) + +#define PTHREAD_MUTEX_TYPE_MASK 3 +#define PTHREAD_PRIORITY_LOWEST 31 +#endif + #endif diff --git a/src/internal/stdio_impl.h b/src/internal/stdio_impl.h index 0b2438d61..21152fab4 100644 --- a/src/internal/stdio_impl.h +++ b/src/internal/stdio_impl.h @@ -2,6 +2,7 @@ #define _STDIO_IMPL_H #include +#include #include "syscall.h" #define UNGET 8 @@ -17,6 +18,57 @@ #define F_ERR 32 #define F_SVB 64 #define F_APP 128 +#define F_NOBUF 256 +#define F_PBUF 512 + + +#define FILE_LIST_NEXT(fl) (fl->next) +#define FILE_LIST_HEAD(head) (head) +#define FILE_LIST_EMPTY(head) ((head) == NULL) +#define FILE_SAVELINK(name, link) void **name = (void *)&(link) +#define INVALID_LINK(x) do {(x) = (void *)-1;} while (0) + +#define FILE_LIST_CHECK_NEXT(fl) do { \ + if (FILE_LIST_NEXT(fl) != NULL && FILE_LIST_NEXT(fl)->prev != &fl->next) { \ + abort(); \ + } \ +} while(0) + +#define FILE_LIST_CHECK_PREV(fl) do { \ + if (*fl->prev != fl) { \ + abort();\ + } \ +} while(0) + +#define FILE_LIST_CHECK_HEAD(head) do { \ + if (FILE_LIST_HEAD(head) != NULL && \ + FILE_LIST_HEAD(head)->prev != &FILE_LIST_HEAD(head)) {\ + abort();\ + } \ +} while(0) + + +#define FILE_LIST_INSERT_HEAD(head, fl) do { \ + FILE_LIST_CHECK_HEAD((head)); \ + if ((FILE_LIST_NEXT((fl)) = FILE_LIST_HEAD((head))) != NULL) { \ + FILE_LIST_HEAD((head))->prev = &FILE_LIST_NEXT((fl)); \ + } \ + FILE_LIST_HEAD(head) = (fl); \ + (fl)->prev = &FILE_LIST_HEAD((head)); \ +} while(0) + +#define FILE_LIST_REMOVE(fl) do { \ + FILE_SAVELINK(oldnext, (fl)->next); \ + FILE_SAVELINK(oldprev, (fl)->prev); \ + FILE_LIST_CHECK_NEXT(fl); \ + FILE_LIST_CHECK_PREV(fl); \ + if (FILE_LIST_NEXT(fl) != NULL) { \ + FILE_LIST_NEXT(fl)->prev = fl->prev; \ + } \ + *fl->prev = FILE_LIST_NEXT(fl); \ + INVALID_LINK(*oldnext); \ + INVALID_LINK(*oldprev); \ +} while(0) struct _IO_FILE { unsigned flags; @@ -26,11 +78,24 @@ struct _IO_FILE { unsigned char *mustbezero_1; unsigned char *wbase; size_t (*read)(FILE *, unsigned char *, size_t); + size_t (*readx)(FILE *, unsigned char *, size_t); size_t (*write)(FILE *, const unsigned char *, size_t); off_t (*seek)(FILE *, off_t, int); unsigned char *buf; size_t buf_size; + /* when allocating buffer dynamically, base == buf - UNGET, + * free base when calling fclose. + * otherwise, base == NULL, cases: + * 1. in stdout, stdin, stdout, base is static array. + * 2. call setvbuf to set buffer or non-buffer. + * 3. call fmemopen, base == NULL && buf_size != 0. + */ + unsigned char *base; +#ifdef LINUX_MUSL_SUPPORTED + FILE **prev, *next; +#else FILE *prev, *next; +#endif int fd; int pipe_pid; long lockcount; @@ -55,13 +120,18 @@ hidden int __lockfile(FILE *); hidden void __unlockfile(FILE *); hidden size_t __stdio_read(FILE *, unsigned char *, size_t); +hidden size_t __stdio_readx(FILE *, unsigned char *, size_t); hidden size_t __stdio_write(FILE *, const unsigned char *, size_t); hidden size_t __stdout_write(FILE *, const unsigned char *, size_t); hidden off_t __stdio_seek(FILE *, off_t, int); hidden int __stdio_close(FILE *); +hidden int __fill_buffer(FILE *f); +hidden ssize_t __flush_buffer(FILE *f); + hidden int __toread(FILE *); hidden int __towrite(FILE *); +hidden int __falloc_buf(FILE *); hidden void __stdio_exit(void); hidden void __stdio_exit_needed(void); @@ -79,11 +149,14 @@ hidden size_t __fwritex(const unsigned char *, size_t, FILE *); hidden int __putc_unlocked(int, FILE *); hidden FILE *__fdopen(int, const char *); -hidden int __fmodeflags(const char *); +hidden FILE *__fdopenx(int, int); +hidden int __fmodeflags(const char *, int *); hidden FILE *__ofl_add(FILE *f); hidden FILE **__ofl_lock(void); hidden void __ofl_unlock(void); +hidden void __ofl_free(FILE *f); +hidden FILE *__ofl_alloc(); struct __pthread; hidden void __register_locked_file(FILE *, struct __pthread *); diff --git a/src/internal/syscall.h b/src/internal/syscall.h index d5f294d43..08a8a9ac6 100644 --- a/src/internal/syscall.h +++ b/src/internal/syscall.h @@ -55,8 +55,13 @@ hidden long __syscall_ret(unsigned long), #define __syscall_cp5(n,a,b,c,d,e) (__syscall_cp)(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),0) #define __syscall_cp6(n,a,b,c,d,e,f) (__syscall_cp)(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f)) +#ifdef FEATURE_PTHREAD_CANCEL #define __syscall_cp(...) __SYSCALL_DISP(__syscall_cp,__VA_ARGS__) #define syscall_cp(...) __syscall_ret(__syscall_cp(__VA_ARGS__)) +#else +#define __syscall_cp(...)__SYSCALL_DISP(__syscall,__VA_ARGS__) +#define syscall_cp(...) __syscall_ret(__syscall(__VA_ARGS__)) +#endif static inline long __alt_socketcall(int sys, int sock, int cp, long a, long b, long c, long d, long e, long f) { @@ -286,9 +291,11 @@ static inline long __alt_socketcall(int sys, int sock, int cp, long a, long b, l #define SYS_utimensat SYS_utimensat_time64 #endif +#ifndef LINUX_MUSL_SUPPORTED #ifndef SYS_pselect6 #define SYS_pselect6 SYS_pselect6_time64 #endif +#endif #ifndef SYS_ppoll #define SYS_ppoll SYS_ppoll_time64 @@ -395,4 +402,8 @@ hidden void __procfdname(char __buf[static 15+3*sizeof(int)], unsigned); hidden void *__vdsosym(const char *, const char *); +#ifdef LINUX_MUSL_SUPPORTED +hidden void __get_vdso_info(); +hidden void *__get_vdso_addr(const char *, const char *); +#endif #endif diff --git a/src/internal/unsupported_api.h b/src/internal/unsupported_api.h new file mode 100644 index 000000000..658a1d62b --- /dev/null +++ b/src/internal/unsupported_api.h @@ -0,0 +1,33 @@ +#ifndef UNSUPPORTED_API_H +#define UNSUPPORTED_API_H + +#include +#include + +#define LITEOS_A "liteos_a" +#define LINUX "linux" + +static inline void unsupported_api(const char *func) +{ + fprintf(stderr, "[ERR]Unsupported API %s\n", func); +} + +static inline int unsupported_api_check(const char *osname) +{ +#ifdef __LITEOS_A__ + if (!strcmp(osname, LITEOS_A)) { + return 1; + } +#elif defined(LINUX_MUSL_SUPPORTED) + if (!strcmp(osname, LINUX)) { + return 1; + } +#endif + return 0; +} + +#define UNSUPPORTED_API_VOID(osname) \ + if (unsupported_api_check(osname) > 0) { \ + fprintf(stderr, "[ERR]Unsupported API %s\n", __FUNCTION__); \ + } +#endif diff --git a/src/ipc/msgctl.c b/src/ipc/msgctl.c index 9c1144064..7de938e0d 100644 --- a/src/ipc/msgctl.c +++ b/src/ipc/msgctl.c @@ -1,5 +1,6 @@ #include #include +#include #include "syscall.h" #include "ipc.h" @@ -17,6 +18,7 @@ int msgctl(int q, int cmd, struct msqid_ds *buf) buf = &out; } #endif + UNSUPPORTED_API_VOID(LITEOS_A); #ifdef SYSCALL_IPC_BROKEN_MODE struct msqid_ds tmp; if (cmd == IPC_SET) { diff --git a/src/ipc/msgget.c b/src/ipc/msgget.c index 30a4b42b8..c415c73e6 100644 --- a/src/ipc/msgget.c +++ b/src/ipc/msgget.c @@ -1,9 +1,11 @@ #include +#include #include "syscall.h" #include "ipc.h" int msgget(key_t k, int flag) { + UNSUPPORTED_API_VOID(LITEOS_A); #ifndef SYS_ipc return syscall(SYS_msgget, k, flag); #else diff --git a/src/ipc/msgrcv.c b/src/ipc/msgrcv.c index 9d1034b14..2bced758b 100644 --- a/src/ipc/msgrcv.c +++ b/src/ipc/msgrcv.c @@ -1,9 +1,11 @@ #include +#include #include "syscall.h" #include "ipc.h" ssize_t msgrcv(int q, void *m, size_t len, long type, int flag) { + UNSUPPORTED_API_VOID(LITEOS_A); #ifndef SYS_ipc return syscall_cp(SYS_msgrcv, q, m, len, type, flag); #else diff --git a/src/ipc/msgsnd.c b/src/ipc/msgsnd.c index 99bb17e94..d61bec6a3 100644 --- a/src/ipc/msgsnd.c +++ b/src/ipc/msgsnd.c @@ -1,9 +1,11 @@ +#include #include #include "syscall.h" #include "ipc.h" int msgsnd(int q, const void *m, size_t len, int flag) { + UNSUPPORTED_API_VOID(LITEOS_A); #ifndef SYS_ipc return syscall_cp(SYS_msgsnd, q, m, len, flag); #else diff --git a/src/ipc/semctl.c b/src/ipc/semctl.c index bbb97d7ae..07b188049 100644 --- a/src/ipc/semctl.c +++ b/src/ipc/semctl.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "syscall.h" #include "ipc.h" @@ -16,6 +17,7 @@ union semun { int semctl(int id, int num, int cmd, ...) { + UNSUPPORTED_API_VOID(LITEOS_A); union semun arg = {0}; va_list ap; switch (cmd & ~IPC_TIME64) { diff --git a/src/ipc/semget.c b/src/ipc/semget.c index 2cdf626b5..f8e4221a6 100644 --- a/src/ipc/semget.c +++ b/src/ipc/semget.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "syscall.h" #include "ipc.h" @@ -10,6 +11,7 @@ int semget(key_t key, int n, int fl) * of struct semid_ds, and thus might not check that the * n fits in the correct (per POSIX) userspace type, so * we have to check here. */ + UNSUPPORTED_API_VOID(LITEOS_A); if (n > USHRT_MAX) return __syscall_ret(-EINVAL); #ifndef SYS_ipc return syscall(SYS_semget, key, n, fl); diff --git a/src/ipc/semop.c b/src/ipc/semop.c index 5f0c7deaf..7295c8bd6 100644 --- a/src/ipc/semop.c +++ b/src/ipc/semop.c @@ -1,9 +1,11 @@ #include +#include #include "syscall.h" #include "ipc.h" int semop(int id, struct sembuf *buf, size_t n) { + UNSUPPORTED_API_VOID(LITEOS_A); #ifndef SYS_ipc return syscall(SYS_semop, id, buf, n); #else diff --git a/porting/linux/user/src/ldso/aarch64/dlvsym.s b/src/ldso/aarch64/linux/dlvsym.s similarity index 100% rename from porting/linux/user/src/ldso/aarch64/dlvsym.s rename to src/ldso/aarch64/linux/dlvsym.s diff --git a/porting/linux/user/src/ldso/arm/dlvsym.s b/src/ldso/arm/linux/dlvsym.s similarity index 100% rename from porting/linux/user/src/ldso/arm/dlvsym.s rename to src/ldso/arm/linux/dlvsym.s diff --git a/src/ldso/dlclose.c b/src/ldso/dlclose.c index e437422a6..1d859809d 100644 --- a/src/ldso/dlclose.c +++ b/src/ldso/dlclose.c @@ -1,7 +1,19 @@ #include #include "dynlink.h" +#ifdef LINUX_MUSL_SUPPORTED +static int dummy(void *p) +{ + return __dl_invalid_handle(p); +} +weak_alias(dummy, __dlclose); +#endif + int dlclose(void *p) { +#ifdef LINUX_MUSL_SUPPORTED + return __dlclose(p); +#else return __dl_invalid_handle(p); +#endif } diff --git a/src/ldso/dlerror.c b/src/ldso/dlerror.c index afe59253e..5a39bbb6d 100644 --- a/src/ldso/dlerror.c +++ b/src/ldso/dlerror.c @@ -48,7 +48,11 @@ hidden void __dl_vseterr(const char *fmt, va_list ap) while (q) { void **p = *q; +#ifdef LINUX_MUSL_SUPPORTED + __libc_free(q); +#else free(q); +#endif q = p; } @@ -56,11 +60,19 @@ hidden void __dl_vseterr(const char *fmt, va_list ap) va_copy(ap2, ap); pthread_t self = __pthread_self(); if (self->dlerror_buf != (void *)-1) +#ifdef LINUX_MUSL_SUPPORTED + __libc_free(self->dlerror_buf); +#else free(self->dlerror_buf); +#endif size_t len = vsnprintf(0, 0, fmt, ap2); if (len < sizeof(void *)) len = sizeof(void *); va_end(ap2); +#ifdef LINUX_MUSL_SUPPORTED + char *buf = __libc_malloc(len+1); +#else char *buf = malloc(len+1); +#endif if (buf) { vsnprintf(buf, len+1, fmt, ap); } else { diff --git a/porting/linux/user/src/ldso/riscv64/dlvsym.s b/src/ldso/riscv64/linux/dlvsym.s similarity index 100% rename from porting/linux/user/src/ldso/riscv64/dlvsym.s rename to src/ldso/riscv64/linux/dlvsym.s diff --git a/porting/linux/user/src/ldso/x86_64/dlvsym.s b/src/ldso/x86_64/linux/dlvsym.s similarity index 100% rename from porting/linux/user/src/ldso/x86_64/dlvsym.s rename to src/ldso/x86_64/linux/dlvsym.s diff --git a/src/legacy/daemon.c b/src/legacy/daemon.c index 1568b1dcb..89c2a667d 100644 --- a/src/legacy/daemon.c +++ b/src/legacy/daemon.c @@ -1,9 +1,11 @@ #define _GNU_SOURCE #include #include +#include int daemon(int nochdir, int noclose) { + UNSUPPORTED_API_VOID(LITEOS_A); if (!nochdir && chdir("/")) return -1; if (!noclose) { diff --git a/src/legacy/getdtablesize.c b/src/legacy/getdtablesize.c index b30c1933a..0c83e3b3c 100644 --- a/src/legacy/getdtablesize.c +++ b/src/legacy/getdtablesize.c @@ -2,10 +2,14 @@ #include #include #include +#include + int getdtablesize(void) { struct rlimit rl; + + UNSUPPORTED_API_VOID(LITEOS_A); getrlimit(RLIMIT_NOFILE, &rl); return rl.rlim_cur < INT_MAX ? rl.rlim_cur : INT_MAX; } diff --git a/src/legacy/getusershell.c b/src/legacy/getusershell.c index 5fecdec2e..82f9ffea3 100644 --- a/src/legacy/getusershell.c +++ b/src/legacy/getusershell.c @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include #include +#include static const char defshells[] = "/bin/sh\n/bin/csh\n"; @@ -10,6 +11,7 @@ static FILE *f; void endusershell(void) { + UNSUPPORTED_API_VOID(LITEOS_A); if (f) fclose(f); f = 0; } diff --git a/src/legacy/isastream.c b/src/legacy/isastream.c index 4dafdb082..95ef2616a 100644 --- a/src/legacy/isastream.c +++ b/src/legacy/isastream.c @@ -1,7 +1,9 @@ #include #include +#include int isastream(int fd) { + UNSUPPORTED_API_VOID(LITEOS_A); return fcntl(fd, F_GETFD) < 0 ? -1 : 0; } diff --git a/src/legacy/ulimit.c b/src/legacy/ulimit.c index 1f59e8e61..d494994d5 100644 --- a/src/legacy/ulimit.c +++ b/src/legacy/ulimit.c @@ -1,10 +1,16 @@ #include #include #include +#include +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif long ulimit(int cmd, ...) { struct rlimit rl; + + UNSUPPORTED_API_VOID(LITEOS_A); getrlimit(RLIMIT_FSIZE, &rl); if (cmd == UL_SETFSIZE) { long val; @@ -15,5 +21,9 @@ long ulimit(int cmd, ...) rl.rlim_cur = 512ULL * val; if (setrlimit(RLIMIT_FSIZE, &rl)) return -1; } +#ifdef LINUX_MUSL_SUPPORTED + return rl.rlim_cur == RLIM_INFINITY? LONG_MAX : rl.rlim_cur / 512; +#else return rl.rlim_cur / 512; +#endif } diff --git a/src/legacy/utmpx.c b/src/legacy/utmpx.c index 7aa65da33..7803fed5e 100644 --- a/src/legacy/utmpx.c +++ b/src/legacy/utmpx.c @@ -2,41 +2,50 @@ #include #include #include +#include void endutxent(void) { + UNSUPPORTED_API_VOID(LITEOS_A); } void setutxent(void) { + UNSUPPORTED_API_VOID(LITEOS_A); } struct utmpx *getutxent(void) { + UNSUPPORTED_API_VOID(LITEOS_A); return NULL; } struct utmpx *getutxid(const struct utmpx *ut) { + UNSUPPORTED_API_VOID(LITEOS_A); return NULL; } struct utmpx *getutxline(const struct utmpx *ut) { + UNSUPPORTED_API_VOID(LITEOS_A); return NULL; } struct utmpx *pututxline(const struct utmpx *ut) { + UNSUPPORTED_API_VOID(LITEOS_A); return NULL; } void updwtmpx(const char *f, const struct utmpx *u) { + UNSUPPORTED_API_VOID(LITEOS_A); } static int __utmpxname(const char *f) { + UNSUPPORTED_API_VOID(LITEOS_A); errno = ENOTSUP; return -1; } diff --git a/porting/linux/user/src/linux/aarch64/flock.s b/src/linux/aarch64/linux/flock.s similarity index 100% rename from porting/linux/user/src/linux/aarch64/flock.s rename to src/linux/aarch64/linux/flock.s diff --git a/src/linux/adjtime.c b/src/linux/adjtime.c index 5a707f2f2..ede2b1012 100644 --- a/src/linux/adjtime.c +++ b/src/linux/adjtime.c @@ -3,9 +3,11 @@ #include #include #include "syscall.h" +#include int adjtime(const struct timeval *in, struct timeval *out) { + UNSUPPORTED_API_VOID(LITEOS_A); struct timex tx = { 0 }; if (in) { if (in->tv_sec > 1000 || in->tv_usec > 1000000000) { diff --git a/porting/linux/user/src/linux/arm/flock.s b/src/linux/arm/linux/flock.s similarity index 100% rename from porting/linux/user/src/linux/arm/flock.s rename to src/linux/arm/linux/flock.s diff --git a/src/linux/cache.c b/src/linux/cache.c index 0eb051c2d..6cd8f7b74 100644 --- a/src/linux/cache.c +++ b/src/linux/cache.c @@ -27,7 +27,12 @@ static void *volatile vdso_func; static int flush_icache_init(void *start, void *end, unsigned long int flags) { +#ifdef LINUX_MUSL_SUPPORTED + __get_vdso_info(); + void *p = __get_vdso_addr(VDSO_FLUSH_ICACHE_VER, VDSO_FLUSH_ICACHE_SYM); +#else void *p = __vdsosym(VDSO_FLUSH_ICACHE_VER, VDSO_FLUSH_ICACHE_SYM); +#endif int (*f)(void *, void *, unsigned long int) = (int (*)(void *, void *, unsigned long int))p; a_cas_p(&vdso_func, (void *)flush_icache_init, p); diff --git a/src/linux/clone.c b/src/linux/clone.c index 8c1af7d3d..cf038a603 100644 --- a/src/linux/clone.c +++ b/src/linux/clone.c @@ -2,14 +2,85 @@ #include #include #include +#include #include "pthread_impl.h" #include "syscall.h" +#include "libc.h" +#ifdef LINUX_MUSL_SUPPORTED +#include "proc_xid_impl.h" +#endif + +struct __clone_args { + int (*func)(void *); + void *arg; +}; + +#ifdef LINUX_MUSL_SUPPORTED +#if defined(__arm__) || defined(__aarch64__) +#define GET_SP_REG(stack) __asm__ __volatile__ ("mov %0, sp" : "=r"(stack)) +#elif defined(__x86_64__) +#ifdef LINUX_MUSL_SUPPORTED +#define GET_SP_REG(stack) do { \ + __asm__ __volatile__ ("mov %%rsp, %0;" : "=r"(stack) :); \ + stack = (void *)((uintptr_t)stack - 16); \ +} while (0) +#else +#define GET_SP_REG(stack) __asm__ __volatile__ ("mov %%rsp, %0;" : "=r"(stack) :) +#endif +#else +#define GET_SP_REG(stack) +#endif +#endif + +static int __start_child(void *clone_args) +{ + int status; + sigset_t set; + int (*func)(void *) = ((struct __clone_args *)clone_args)->func; + void *arg = ((struct __clone_args *)clone_args)->arg; +#ifdef LINUX_MUSL_SUPPORTED + free(clone_args); +#endif + + __block_all_sigs(&set); + pthread_t self = __pthread_self(); + self->tid = __syscall(SYS_gettid); +#ifdef __LITEOS_A__ + self->pid = __syscall(SYS_getpid); +#else + self->pid = self->tid; +#endif + self->proc_tid = -1; + self->robust_list.off = 0; + self->robust_list.pending = 0; + self->next = self->prev = self; + __thread_list_lock = 0; + libc.threads_minus_1 = 0; +#ifdef LINUX_MUSL_SUPPORTED + __clear_proc_pid(); +#endif +#ifdef __LITEOS_A__ + libc.exit = 0; + signal(SIGSYS, arm_do_signal); +#endif + __restore_sigs(&set); + + status = func(arg); + exit(status); +} int clone(int (*func)(void *), void *stack, int flags, void *arg, ...) { + int ret; + int (*clone_func)(void *) = func; + struct __clone_args *clone_args = (struct __clone_args *)arg; va_list ap; pid_t *ptid, *ctid; void *tls; + void *clone_stack = stack; +#ifdef __LITEOS_A__ + void *mmap_stack = NULL; +#endif va_start(ap, arg); ptid = va_arg(ap, pid_t *); @@ -17,5 +88,73 @@ int clone(int (*func)(void *), void *stack, int flags, void *arg, ...) ctid = va_arg(ap, pid_t *); va_end(ap); - return __syscall_ret(__clone(func, stack, flags, arg, ptid, tls, ctid)); + pthread_t self = __pthread_self(); + pid_t parent_pid = self->pid; + self->pid = 0; + pid_t caller_tid = self->tid; + +#ifdef LINUX_MUSL_SUPPORTED + if (!(flags & (CLONE_VM | CLONE_VFORK))) { + self->tid = -1; + if (func) { + clone_args = (struct __clone_args *)malloc(sizeof(struct __clone_args)); + if (clone_args == NULL) { + errno = ENOMEM; + return -1; + } + clone_args->func = clone_func; + clone_args->arg = arg; + clone_func = __start_child; + } + } + if (!stack) { + GET_SP_REG(stack); + } +#elif defined(__LITEOS_A__) + if (!(flags & (CLONE_VM | CLONE_VFORK))) { + self->tid = -1; + if (func) { + if (!stack) { + mmap_stack = __mmap(0, __default_stacksize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); + if (mmap_stack == MAP_FAILED) { + errno = ENOMEM; + return -1; + } + clone_args = (struct __clone_args *)((char *)mmap_stack + __default_stacksize); + } else { + clone_args = (struct __clone_args *)stack; + } + clone_args = (struct __clone_args *)((uintptr_t)clone_args - ((uintptr_t)clone_args % sizeof(uintptr_t))); + clone_args = (struct __clone_args *)((char *)clone_args - sizeof(struct __clone_args)); + clone_args->func = clone_func; + clone_args->arg = arg; + clone_func = __start_child; + clone_stack = (void *)clone_args; + } + } +#endif + ret = __syscall_ret(__clone(clone_func, clone_stack, flags, (void *)clone_args, ptid, tls, ctid)); + +#ifdef LINUX_MUSL_SUPPORTED + if (!(flags & (CLONE_VM | CLONE_VFORK)) && func) { + free(clone_args); + } +#elif defined(__LITEOS_A__) + if (mmap_stack) { + __munmap(mmap_stack, __default_stacksize); + } +#endif + + if (ret != 0) { + self->pid = parent_pid; + self->tid = caller_tid; + } else if (self->tid == -1) { + self->tid = __syscall(SYS_gettid); +#ifdef __LITEOS_A__ + self->pid = __syscall(SYS_getpid); +#else + self->pid = self->tid; +#endif + } + return ret; } diff --git a/src/linux/flock.c b/src/linux/flock.c index 87aa5cfed..6c8cb354d 100644 --- a/src/linux/flock.c +++ b/src/linux/flock.c @@ -1,7 +1,15 @@ #include #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +int __flock(int fd, int op) +#else int flock(int fd, int op) +#endif { return syscall(SYS_flock, fd, op); } + +#ifdef LINUX_MUSL_SUPPORTED +weak_alias(__flock, flock); +#endif diff --git a/porting/linux/user/src/linux/getprocpid.c b/src/linux/linux/getprocpid.c similarity index 100% rename from porting/linux/user/src/linux/getprocpid.c rename to src/linux/linux/getprocpid.c diff --git a/porting/liteos_a/user/src/linux/cap.c b/src/linux/liteos_a/cap.c similarity index 77% rename from porting/liteos_a/user/src/linux/cap.c rename to src/linux/liteos_a/cap.c index ffc669859..c6ae8635e 100644 --- a/porting/liteos_a/user/src/linux/cap.c +++ b/src/linux/liteos_a/cap.c @@ -1,34 +1,3 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * 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. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "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 COPYRIGHT HOLDER OR - * CONTRIBUTORS 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. - */ - #include #include #include diff --git a/src/linux/prctl.c b/src/linux/prctl.c index 19f4267cd..74b4560a5 100644 --- a/src/linux/prctl.c +++ b/src/linux/prctl.c @@ -1,8 +1,15 @@ #include #include #include "syscall.h" +#ifdef HOOK_ENABLE +int __libc_prctl(int op, ...); +#endif +#ifdef HOOK_ENABLE +int __libc_prctl(int op, ...) +#else int prctl(int op, ...) +#endif { unsigned long x[4]; int i; diff --git a/src/linux/reboot.c b/src/linux/reboot.c index 7f12af79b..68a9e4175 100644 --- a/src/linux/reboot.c +++ b/src/linux/reboot.c @@ -1,7 +1,8 @@ #include #include "syscall.h" -int reboot(int type) +int __reboot(int type) { - return syscall(SYS_reboot, 0xfee1dead, 672274793, type); + return syscall(SYS_reboot, RB_MAGIC1, RB_MAGIC2, type); } +weak_alias(__reboot, reboot); diff --git a/src/linux/settimeofday.c b/src/linux/settimeofday.c index 860fb5de9..5c9525286 100644 --- a/src/linux/settimeofday.c +++ b/src/linux/settimeofday.c @@ -6,8 +6,12 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz) { +#ifdef LINUX_MUSL_SUPPORTED + return syscall(SYS_settimeofday, tv, tz); +#else if (!tv) return 0; if (tv->tv_usec >= 1000000ULL) return __syscall_ret(-EINVAL); return clock_settime(CLOCK_REALTIME, &((struct timespec){ .tv_sec = tv->tv_sec, .tv_nsec = tv->tv_usec * 1000})); +#endif } diff --git a/porting/linux/user/src/linux/tgkill.c b/src/linux/tgkill.c similarity index 93% rename from porting/linux/user/src/linux/tgkill.c rename to src/linux/tgkill.c index b20efd938..37119024b 100644 --- a/porting/linux/user/src/linux/tgkill.c +++ b/src/linux/tgkill.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,4 +19,4 @@ int tgkill(int tgid, int tid, int sig) { return syscall(__NR_tgkill, tgid, tid, sig); -} \ No newline at end of file +} diff --git a/src/linux/utimes.c b/src/linux/utimes.c index 6ca025d95..84109a17e 100644 --- a/src/linux/utimes.c +++ b/src/linux/utimes.c @@ -1,8 +1,10 @@ #include #include "fcntl.h" #include "syscall.h" +#include int utimes(const char *path, const struct timeval times[2]) { + UNSUPPORTED_API_VOID(LITEOS_A); return __futimesat(AT_FDCWD, path, times); } diff --git a/porting/linux/user/src/linux/x86_64/flock.s b/src/linux/x86_64/linux/flock.s similarity index 100% rename from porting/linux/user/src/linux/x86_64/flock.s rename to src/linux/x86_64/linux/flock.s diff --git a/src/locale/c_locale.c b/src/locale/c_locale.c index 77ccf5873..582d0305f 100644 --- a/src/locale/c_locale.c +++ b/src/locale/c_locale.c @@ -6,7 +6,8 @@ static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 }; const struct __locale_map __c_dot_utf8 = { .map = empty_mo, .map_size = sizeof empty_mo, - .name = "C.UTF-8" + .name = "C.UTF-8", + .flag = VALID }; const struct __locale_struct __c_locale = { 0 }; diff --git a/src/locale/dcngettext.c b/src/locale/dcngettext.c index 0b53286db..24f6306fa 100644 --- a/src/locale/dcngettext.c +++ b/src/locale/dcngettext.c @@ -93,6 +93,24 @@ char *bindtextdomain(const char *domainname, const char *dirname) return (char *)p->dirname; } +#ifdef LINUX_MUSL_SUPPORTED +static const char catnames[][18] = { + "LC_CTYPE", + "LC_NUMERIC", + "LC_TIME", + "LC_COLLATE", + "LC_MONETARY", + "LC_MESSAGES", + "LC_PAPER", + "LC_NAME", + "LC_ADDRESS", + "LC_TELEPHONE", + "LC_MEASUREMENT", + "LC_IDENTIFICATION", +}; + +static const char catlens[] = { 8, 10, 7, 10, 11, 11, 8, 7, 10, 12, 14, 17 }; +#else static const char catnames[][12] = { "LC_CTYPE", "LC_NUMERIC", @@ -103,6 +121,7 @@ static const char catnames[][12] = { }; static const char catlens[] = { 8, 10, 7, 10, 11, 11 }; +#endif struct msgcat { struct msgcat *next; diff --git a/src/locale/freelocale.c b/src/locale/freelocale.c index 385d12069..46e23ce64 100644 --- a/src/locale/freelocale.c +++ b/src/locale/freelocale.c @@ -1,4 +1,5 @@ #include +#include #include "locale_impl.h" #define malloc undef @@ -8,6 +9,7 @@ void freelocale(locale_t l) { + UNSUPPORTED_API_VOID(LITEOS_A); if (__loc_is_allocated(l)) free(l); } diff --git a/src/locale/langinfo.c b/src/locale/langinfo.c index 14773093b..dd764817c 100644 --- a/src/locale/langinfo.c +++ b/src/locale/langinfo.c @@ -21,7 +21,8 @@ static const char c_time[] = "%m/%d/%y\0" "0123456789\0" "%a %b %e %T %Y\0" - "%H:%M:%S"; + "%H:%M:%S\0" + "am\0" "pm"; static const char c_messages[] = "^[yY]\0" "^[nN]\0" "yes\0" "no"; static const char c_numeric[] = ".\0" ""; @@ -44,7 +45,9 @@ char *__nl_langinfo_l(nl_item item, locale_t loc) str = c_numeric; break; case LC_TIME: - if (idx > 0x31) return ""; + if (idx > 0x33) { + return ""; + } str = c_time; break; case LC_MONETARY: diff --git a/src/locale/locale_map.c b/src/locale/locale_map.c index da61f7fc0..f8ace1090 100644 --- a/src/locale/locale_map.c +++ b/src/locale/locale_map.c @@ -19,6 +19,22 @@ const char *__lctrans_impl(const char *msg, const struct __locale_map *lm) return trans ? trans : msg; } +#ifdef LINUX_MUSL_SUPPORTED +static const char envvars[][18] = { + "LC_CTYPE", + "LC_NUMERIC", + "LC_TIME", + "LC_COLLATE", + "LC_MONETARY", + "LC_MESSAGES", + "LC_PAPER", + "LC_NAME", + "LC_ADDRESS", + "LC_TELEPHONE", + "LC_MEASUREMENT", + "LC_IDENTIFICATION", +}; +#else static const char envvars[][12] = { "LC_CTYPE", "LC_NUMERIC", @@ -27,6 +43,7 @@ static const char envvars[][12] = { "LC_MONETARY", "LC_MESSAGES", }; +#endif volatile int __locale_lock[1]; volatile int *const __locale_lockptr = __locale_lock; @@ -51,8 +68,13 @@ const struct __locale_map *__get_locale(int cat, const char *val) for (n=0; nnext) if (!strcmp(val, p->name)) return p; + if (!strcmp(val, "en_US.UTF-8") || !strcmp(val, "C.UTF-8")) { + /* If no locale definition was found, make a locale map + * object anyway to store the name, which is kept for the + * sake of being able to do message translations at the + * application level. */ + if (!new && (new = malloc(sizeof *new))) { + new->map = __c_dot_utf8.map; + new->map_size = __c_dot_utf8.map_size; + memcpy(new->name, val, n); + new->name[n] = 0; + new->next = loc_head; + new->flag = VALID; + loc_head = new; + } + } + if (!libc.secure) path = getenv("MUSL_LOCPATH"); /* FIXME: add a default path? */ @@ -87,6 +125,7 @@ const struct __locale_map *__get_locale(int cat, const char *val) memcpy(new->name, val, n); new->name[n] = 0; new->next = loc_head; + new->flag = VALID; loc_head = new; break; } @@ -102,6 +141,7 @@ const struct __locale_map *__get_locale(int cat, const char *val) memcpy(new->name, val, n); new->name[n] = 0; new->next = loc_head; + new->flag = INVALID; loc_head = new; } diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 9ac3cd386..451e593f8 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -25,7 +25,7 @@ static locale_t do_newlocale(int mask, const char *name, locale_t loc) for (int i=0; icat[i] : __get_locale(i, (mask & (1<flag == INVALID) return 0; } diff --git a/src/locale/setlocale.c b/src/locale/setlocale.c index 360c44376..45eb28a68 100644 --- a/src/locale/setlocale.c +++ b/src/locale/setlocale.c @@ -7,10 +7,20 @@ static char buf[LC_ALL*(LOCALE_NAME_MAX+1)]; +#ifdef LINUX_MUSL_SUPPORTED +static inline int Fresh(struct __locale_map *lm) +{ + if (lm != NULL) { + return lm->flag; + } + return VALID; +} +#endif + char *setlocale(int cat, const char *name) { const struct __locale_map *lm; - + char flag = VALID; if ((unsigned)cat > LC_ALL) return 0; LOCK(__locale_lock); @@ -38,6 +48,11 @@ char *setlocale(int cat, const char *name) UNLOCK(__locale_lock); return 0; } +#ifdef LINUX_MUSL_SUPPORTED + if(Fresh(lm) == INVALID) { + flag = INVALID; + } +#endif tmp_locale.cat[i] = lm; } libc.global_locale = tmp_locale; @@ -57,6 +72,9 @@ char *setlocale(int cat, const char *name) } *--s = 0; UNLOCK(__locale_lock); + if (flag == INVALID) { + return 0; + } return same==LC_ALL ? (char *)part : buf; } @@ -66,6 +84,9 @@ char *setlocale(int cat, const char *name) UNLOCK(__locale_lock); return 0; } +#ifdef LINUX_MUSL_SUPPORTED + flag = Fresh(lm); +#endif libc.global_locale.cat[cat] = lm; } else { lm = libc.global_locale.cat[cat]; @@ -73,6 +94,8 @@ char *setlocale(int cat, const char *name) char *ret = lm ? (char *)lm->name : "C"; UNLOCK(__locale_lock); - + if (flag == INVALID) { + return 0; + } return ret; } diff --git a/src/locale/strcoll.c b/src/locale/strcoll.c index dd3cbc480..1e633d656 100644 --- a/src/locale/strcoll.c +++ b/src/locale/strcoll.c @@ -9,7 +9,11 @@ int __strcoll_l(const char *l, const char *r, locale_t loc) int strcoll(const char *l, const char *r) { +#ifdef __LITEOS_A__ + return strcmp(l, r); +#else return __strcoll_l(l, r, CURRENT_LOCALE); +#endif } weak_alias(__strcoll_l, strcoll_l); diff --git a/src/malloc/free.c b/src/malloc/free.c index 3944f7b28..4026330e3 100644 --- a/src/malloc/free.c +++ b/src/malloc/free.c @@ -1,6 +1,15 @@ +#ifndef HOOK_ENABLE #include +#ifdef USE_JEMALLOC +extern void je_free(void* p); +#endif + void free(void *p) { +#ifdef USE_JEMALLOC + return je_free(p); +#endif __libc_free(p); } +#endif diff --git a/porting/linux/user/src/malloc/jemalloc/jemalloc.c b/src/malloc/jemalloc/linux/jemalloc.c similarity index 100% rename from porting/linux/user/src/malloc/jemalloc/jemalloc.c rename to src/malloc/jemalloc/linux/jemalloc.c diff --git a/src/malloc/libc_calloc.c b/src/malloc/libc_calloc.c index d25eabea4..6b96e2b07 100644 --- a/src/malloc/libc_calloc.c +++ b/src/malloc/libc_calloc.c @@ -1,4 +1,72 @@ +#ifdef LINUX_MUSL_SUPPORTED +#include +#include +#include +#include +#include "dynlink.h" + +static size_t mal0_clear(char *p, size_t n) +{ + const size_t pagesz = 4096; /* arbitrary */ + if (n < pagesz) return n; +#ifdef __GNUC__ + typedef uint64_t __attribute__((__may_alias__)) T; +#else + typedef unsigned char T; +#endif + char *pp = p + n; + size_t i = (uintptr_t)pp & (pagesz - 1); + for (;;) { + pp = memset(pp - i, 0, i); + if (pp - p < pagesz) return pp - p; + for (i = pagesz; i; i -= 2*sizeof(T), pp -= 2*sizeof(T)) + if (((T *)pp)[-1] | ((T *)pp)[-2]) + break; + } +} + +static int allzerop(void *p) +{ + return 0; +} +weak_alias(allzerop, __malloc_allzerop); + +void *__libc_calloc(size_t m, size_t n) +{ + if (n && m > (size_t)-1/n) { + errno = ENOMEM; + return 0; + } + n *= m; + void *p = __libc_malloc(n); + if (!p || (!__malloc_replaced && __malloc_allzerop(p))) + return p; + n = mal0_clear(p, n); + return memset(p, 0, n); +} + +#ifdef HOOK_ENABLE +void *hook_calloc(size_t m, size_t n) +{ + if (n && m > (size_t)-1/n) { + errno = ENOMEM; + return 0; + } + n *= m; +#ifdef LINUX_MUSL_SUPPORTED + void *p = __libc_malloc(n); +#else + void *p = malloc(n); +#endif + if (!p || (!__malloc_replaced && __malloc_allzerop(p))) + return p; + n = mal0_clear(p, n); + return memset(p, 0, n); +} +#endif +#else #define calloc __libc_calloc #define malloc __libc_malloc #include "calloc.c" +#endif diff --git a/porting/linux/user/src/malloc/calloc.c b/src/malloc/linux/calloc.c similarity index 100% rename from porting/linux/user/src/malloc/calloc.c rename to src/malloc/linux/calloc.c diff --git a/porting/linux/user/src/malloc/stats.c b/src/malloc/linux/stats.c similarity index 100% rename from porting/linux/user/src/malloc/stats.c rename to src/malloc/linux/stats.c diff --git a/src/malloc/lite_malloc.c b/src/malloc/lite_malloc.c index 43a988fbb..fcb5b6434 100644 --- a/src/malloc/lite_malloc.c +++ b/src/malloc/lite_malloc.c @@ -8,6 +8,10 @@ #include "syscall.h" #include "fork_impl.h" +#ifdef USE_JEMALLOC +extern void* je_malloc(size_t size); +#endif + #define ALIGN 16 /* This function returns true if the interval [old,new] @@ -112,6 +116,9 @@ void *__libc_malloc(size_t n) static void *default_malloc(size_t n) { +#ifdef USE_JEMALLOC + return je_malloc(n); +#endif return __libc_malloc_impl(n); } diff --git a/porting/liteos_a/user_debug/src/malloc/backtrace.c b/src/malloc/liteos_a/user_debug/backtrace.c similarity index 100% rename from porting/liteos_a/user_debug/src/malloc/backtrace.c rename to src/malloc/liteos_a/user_debug/backtrace.c diff --git a/porting/liteos_a/user_debug/src/malloc/memory_check.c b/src/malloc/liteos_a/user_debug/memory_check.c similarity index 100% rename from porting/liteos_a/user_debug/src/malloc/memory_check.c rename to src/malloc/liteos_a/user_debug/memory_check.c diff --git a/src/malloc/mallocng/aligned_alloc.c b/src/malloc/mallocng/aligned_alloc.c index e0862a83a..ac5847e1c 100644 --- a/src/malloc/mallocng/aligned_alloc.c +++ b/src/malloc/mallocng/aligned_alloc.c @@ -2,8 +2,15 @@ #include #include "meta.h" +#ifdef USE_JEMALLOC +extern void* je_memalign(size_t align, size_t len); +#endif + void *aligned_alloc(size_t align, size_t len) { +#ifdef USE_JEMALLOC + return je_memalign(align, len); +#endif if ((align & -align) != align) { errno = EINVAL; return 0; diff --git a/src/malloc/mallocng/donate.c b/src/malloc/mallocng/donate.c index 41d850f35..7c5a28473 100644 --- a/src/malloc/mallocng/donate.c +++ b/src/malloc/mallocng/donate.c @@ -20,7 +20,7 @@ static void donate(unsigned char *base, size_t len) m->avail_mask = 0; m->freed_mask = 1; m->mem = (void *)a; - m->mem->meta = m; + m->mem->meta = encode_ptr(m, ctx.secret); m->last_idx = 0; m->freeable = 0; m->sizeclass = sc; diff --git a/src/malloc/mallocng/malloc.c b/src/malloc/mallocng/malloc.c index d695ab8ec..11b50ae8f 100644 --- a/src/malloc/mallocng/malloc.c +++ b/src/malloc/mallocng/malloc.c @@ -3,10 +3,69 @@ #include #include #include +#ifdef LINUX_MUSL_SUPPORTED +#include +#endif #include #include "meta.h" +#ifdef USE_JEMALLOC +#ifdef USE_JEMALLOC_DFX_INTF +extern void je_malloc_disable(); +extern void je_malloc_enable(); +extern int je_iterate(uintptr_t base, size_t size, + void (*callback)(uintptr_t ptr, size_t size, void* arg), void* arg); +extern int je_mallopt(int param, int value); +#endif +#endif + +#ifdef MALLOC_SECURE_ALL +#include +#define RANDOM_BUFFER_LEN 512 +static uint8_t buffer[RANDOM_BUFFER_LEN] = { 0 }; +static size_t ri = RANDOM_BUFFER_LEN; + +static uint8_t get_random8() +{ + uint8_t num; + if ((ri >= RANDOM_BUFFER_LEN) || (buffer[0] == 0)) { + int fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) { + num = (uint8_t)get_random_secret(); + return num; + } + + read(fd, buffer, RANDOM_BUFFER_LEN); + close(fd); + ri = 0; + } + num = buffer[ri]; + ri++; + return num; +} + +static int get_randomIdx(int avail_mask, int last_idx) +{ + uint32_t mask; + uint32_t r; + uint32_t cmask; + int idx; + + mask = avail_mask; + r = get_random8() % last_idx; + cmask = ~((2u << (last_idx - r)) - 1); + + if (mask & cmask) { + idx = 31 - a_clz_32(mask & cmask); + } else { + idx = a_ctz_32(mask); + } + + return idx; +} +#endif + LOCK_OBJ_DEF; const uint16_t size_classes[] = { @@ -69,6 +128,9 @@ struct meta *alloc_meta(void) if (brk(new) != new) { ctx.brk = -1; } else { +#ifdef LINUX_MUSL_SUPPORTED + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ctx.brk, new - ctx.brk, "native_heap:meta"); +#endif if (need_guard) mmap((void *)ctx.brk, pagesize, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0); ctx.brk = new; @@ -164,7 +226,12 @@ static uint32_t try_avail(struct meta **pm) assert(mask); decay_bounces(m->sizeclass); } +#ifdef MALLOC_SECURE_ALL + int idx = get_randomIdx(mask, m->last_idx); + first = 1 << idx; +#else first = mask&-mask; +#endif m->avail_mask = mask-first; return first; } @@ -251,6 +318,9 @@ static struct meta *alloc_group(int sc, size_t req) free_meta(m); return 0; } +#ifdef LINUX_MUSL_SUPPORTED + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, needed, "native_heap:brk"); +#endif m->maplen = needed>>12; ctx.mmap_counter++; active_idx = (4096-UNIT)/size-1; @@ -275,7 +345,7 @@ static struct meta *alloc_group(int sc, size_t req) m->avail_mask = (2u<freed_mask = (2u<<(cnt-1))-1 - m->avail_mask; m->mem = (void *)p; - m->mem->meta = m; + m->mem->meta = encode_ptr(m, ctx.secret); m->mem->active_idx = active_idx; m->last_idx = cnt-1; m->freeable = 1; @@ -311,6 +381,9 @@ void *malloc(size_t n) MAP_PRIVATE|MAP_ANON, -1, 0); if (p==MAP_FAILED) return 0; wrlock(); +#ifdef LINUX_MUSL_SUPPORTED + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, needed, "native_heap:mmap"); +#endif step_seq(); g = alloc_meta(); if (!g) { @@ -319,7 +392,7 @@ void *malloc(size_t n) return 0; } g->mem = p; - g->mem->meta = g; + g->mem->meta = encode_ptr(g, ctx.secret); g->last_idx = 0; g->freeable = 1; g->sizeclass = 63; @@ -355,6 +428,16 @@ void *malloc(size_t n) for (;;) { mask = g ? g->avail_mask : 0; +#ifdef MALLOC_SECURE_ALL + if (!mask) break; + idx = get_randomIdx(mask, g->last_idx); + first = 1u << idx; + + if (RDLOCK_IS_EXCLUSIVE || !MT) + g->avail_mask = mask-first; + else if (a_cas(&g->avail_mask, mask, mask-first)!=mask) + continue; +#else first = mask&-mask; if (!first) break; if (RDLOCK_IS_EXCLUSIVE || !MT) @@ -362,6 +445,7 @@ void *malloc(size_t n) else if (a_cas(&g->avail_mask, mask, mask-first)!=mask) continue; idx = a_ctz_32(first); +#endif goto success; } upgradelock(); @@ -385,3 +469,38 @@ int is_allzero(void *p) return g->sizeclass >= 48 || get_stride(g) < UNIT*size_classes[g->sizeclass]; } + +int mallopt(int param, int value) +{ +#ifdef USE_JEMALLOC_DFX_INTF + return je_mallopt(param, value); +#endif + return 0; +} + +void malloc_disable(void) +{ +#ifdef USE_JEMALLOC_DFX_INTF + je_malloc_disable(); +#endif +} + +void malloc_enable(void) +{ +#ifdef USE_JEMALLOC_DFX_INTF + je_malloc_enable(); +#endif +} + +int malloc_iterate(void* base, size_t size, void (*callback)(void* base, size_t size, void* arg), void* arg) +{ +#ifdef USE_JEMALLOC_DFX_INTF + return je_iterate(base, size, callback, arg); +#endif + return 0; +} + +ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) +{ + return 0; +} diff --git a/src/malloc/mallocng/malloc_usable_size.c b/src/malloc/mallocng/malloc_usable_size.c index ce6a960c6..1819ab3c5 100644 --- a/src/malloc/mallocng/malloc_usable_size.c +++ b/src/malloc/mallocng/malloc_usable_size.c @@ -1,8 +1,19 @@ #include #include "meta.h" +#ifdef USE_JEMALLOC +extern size_t je_malloc_usable_size(void *p); +#endif + +#ifndef HOOK_ENABLE size_t malloc_usable_size(void *p) +#else +size_t __libc_malloc_usable_size(void *p) +#endif { +#ifdef USE_JEMALLOC + return je_malloc_usable_size(p); +#endif if (!p) return 0; struct meta *g = get_meta(p); int idx = get_slot_index(p); diff --git a/src/malloc/mallocng/meta.h b/src/malloc/mallocng/meta.h index 61ec53f9a..1eb0e320f 100644 --- a/src/malloc/mallocng/meta.h +++ b/src/malloc/mallocng/meta.h @@ -72,12 +72,27 @@ struct meta *alloc_meta(void); __attribute__((__visibility__("hidden"))) int is_allzero(void *); +static inline void *encode_ptr(void *ptr, uint64_t key) +{ +#ifdef MALLOC_FREELIST_HARDENED + return (void *)((uintptr_t)ptr ^ key); +#else + (void)key; + return ptr; +#endif +} + static inline void queue(struct meta **phead, struct meta *m) { assert(!m->next); assert(!m->prev); if (*phead) { struct meta *head = *phead; +#ifdef MALLOC_FREELIST_HARDENED + if (head->next->prev != head || head->prev->next != head) { + a_crash(); + } +#endif m->next = head; m->prev = head->prev; m->next->prev = m->prev->next = m; @@ -137,7 +152,7 @@ static inline struct meta *get_meta(const unsigned char *p) assert(offset > 0xffff); } const struct group *base = (const void *)(p - UNIT*offset - UNIT); - const struct meta *meta = base->meta; + const struct meta *meta = encode_ptr(base->meta, ctx.secret); assert(meta->mem == base); assert(index <= meta->last_idx); assert(!(meta->avail_mask & (1u< +#include +#include +#include "libc.h" +#include "pthread_impl.h" +#include "oldmalloc/malloc_impl.h" + +static inline void lock(volatile int *lk) +{ + if (libc.threads_minus_1) + while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1); +} + +static inline void unlock(volatile int *lk) +{ + if (lk[0]) { + a_store(lk, 0); + if (lk[1]) __wake(lk, 1, 1); + } +} + +void *__memalign(size_t align, size_t len) +{ + unsigned char *mem, *new; + + if ((align & -align) != align) { + errno = EINVAL; + return 0; + } + + if (len > SIZE_MAX - align || __malloc_replaced) { + errno = ENOMEM; + return 0; + } + + if (align <= SIZE_ALIGN) + return malloc(len); + + if (!(mem = malloc(len + align-1))) + return 0; + + new = (void *)((uintptr_t)mem + align-1 & -align); + if (new == mem) return mem; + + struct chunk *c = MEM_TO_CHUNK(mem); + struct chunk *n = MEM_TO_CHUNK(new); + + if (g_enable_check) { + int status = delete_node(mem); + if (status != 0) { + get_free_trace(mem); + a_crash(); + } + } + + if (IS_MMAPPED(c)) { + /* Apply difference between aligned and original + * address to the "extra" field of mmapped chunk. */ + n->psize = c->psize + (new-mem); + n->csize = c->csize - (new-mem); + if (g_enable_check) { + insert_node(CHUNK_TO_MEM(n), CHUNK_SIZE(n)); + } + return new; + } + + struct chunk *t = NEXT_CHUNK(c); + + /* Split the allocated chunk into two chunks. The aligned part + * that will be used has the size in its footer reduced by the + * difference between the aligned and original addresses, and + * the resulting size copied to its header. A new header and + * footer are written for the split-off part to be freed. */ + lock(g_mem_lock); + n->psize = c->csize = C_INUSE | (new-mem); + n->csize = t->psize -= new-mem; + calculate_checksum(c, n); + calculate_checksum(NULL, t); + unlock(g_mem_lock); + if (g_enable_check) { + insert_node(CHUNK_TO_MEM(n), CHUNK_SIZE(n)); + } + + __bin_chunk(c); + return new; +} + +weak_alias(__memalign, memalign); +#else #define _BSD_SOURCE #include @@ -5,3 +95,4 @@ void *memalign(size_t align, size_t len) { return aligned_alloc(align, len); } +#endif diff --git a/src/malloc/oldmalloc/aligned_alloc.c b/src/malloc/oldmalloc/aligned_alloc.c index 4adca3b4f..ab5f9033d 100644 --- a/src/malloc/oldmalloc/aligned_alloc.c +++ b/src/malloc/oldmalloc/aligned_alloc.c @@ -2,6 +2,9 @@ #include #include #include "malloc_impl.h" +#ifdef __LITEOS_A__ +#include "dynlink.h" +#endif void *aligned_alloc(size_t align, size_t len) { diff --git a/src/malloc/oldmalloc/malloc.c b/src/malloc/oldmalloc/malloc.c index 25d00d44d..1ed25be28 100644 --- a/src/malloc/oldmalloc/malloc.c +++ b/src/malloc/oldmalloc/malloc.c @@ -4,6 +4,10 @@ #include #include #include +#ifdef __LITEOS_A__ +#include +#include +#endif #include #include "libc.h" #include "atomic.h" @@ -159,7 +163,11 @@ static int traverses_stack_p(uintptr_t old, uintptr_t new) * and mmap minimum size rules. The caller is responsible for locking * to prevent concurrent calls. */ +#ifdef __LITEOS_A__ +void *__expand_heap(size_t *pn) +#else static void *__expand_heap(size_t *pn) +#endif { static uintptr_t brk; static unsigned mmap_step; @@ -207,6 +215,9 @@ static struct chunk *expand_heap(size_t n) p = __expand_heap(&n); if (!p) return 0; +#ifdef __LITEOS_A__ + lock(g_mem_lock); +#endif /* If not just expanding existing space, we need to make a * new sentinel chunk below the allocated space. */ if (p != end) { @@ -215,6 +226,9 @@ static struct chunk *expand_heap(size_t n) p = (char *)p + SIZE_ALIGN; w = MEM_TO_CHUNK(p); w->psize = 0 | C_INUSE; +#ifdef __LITEOS_A__ + insert_block_list(w); +#endif } /* Record new heap end and fill in footer. */ @@ -227,6 +241,11 @@ static struct chunk *expand_heap(size_t n) * zero-size sentinel header at the old end-of-heap. */ w = MEM_TO_CHUNK(p); w->csize = n | C_INUSE; +#ifdef __LITEOS_A__ + calculate_checksum(w, MEM_TO_CHUNK(end)); + + unlock(g_mem_lock); +#endif return w; } @@ -276,11 +295,20 @@ static void trim(struct chunk *self, size_t n) next = NEXT_CHUNK(self); split = (void *)((char *)self + n); - +#ifdef __LITEOS_A__ + lock(g_mem_lock); +#endif split->psize = n | C_INUSE; split->csize = n1-n; next->psize = n1-n; +#ifdef __LITEOS_A__ + calculate_checksum(split, next); +#endif self->csize = n | C_INUSE; +#ifdef __LITEOS_A__ + calculate_checksum(self, NULL); + unlock(g_mem_lock); +#endif int i = bin_index(n1-n); lock_bin(i); @@ -300,12 +328,33 @@ void *malloc(size_t n) if (n > MMAP_THRESHOLD) { size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE; +#ifdef __LITEOS_A__ + if (g_enable_check) { + /* Allocate two more pages for protection, loacted at the head and tail of user memory respectively */ + len += PAGE_SIZE << 1; + } +#endif char *base = __mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); if (base == (void *)-1) return 0; +#ifdef __LITEOS_A__ + if (g_enable_check) { + if (mprotect(base, PAGE_SIZE, PROT_NONE) || + mprotect(base + len - PAGE_SIZE, PAGE_SIZE, PROT_NONE)) { + printf("%s %d, mprotect failed, err: %s!\n", __func__, __LINE__, strerror(errno)); + } + base += PAGE_SIZE; + } +#endif c = (void *)(base + SIZE_ALIGN - OVERHEAD); c->csize = len - (SIZE_ALIGN - OVERHEAD); c->psize = SIZE_ALIGN - OVERHEAD; +#ifdef __LITEOS_A__ + if (g_enable_check) { + c->csize -= PAGE_SIZE << 1; + insert_node(CHUNK_TO_MEM(c), CHUNK_SIZE(c)); + } +#endif return CHUNK_TO_MEM(c); } @@ -340,6 +389,11 @@ void *malloc(size_t n) } } trim(c, n); +#ifdef __LITEOS_A__ + if (g_enable_check) { + insert_node(CHUNK_TO_MEM(c), CHUNK_SIZE(c)); + } +#endif unlock(mal.split_merge_lock); return CHUNK_TO_MEM(c); } @@ -370,13 +424,29 @@ void *realloc(void *p, size_t n) size_t oldlen = n0 + extra; size_t newlen = n + extra; /* Crash on realloc of freed chunk */ +#ifdef __LITEOS_A__ + if (extra & 1) { + if (g_enable_check) { + get_free_trace(CHUNK_TO_MEM(self)); + a_crash(); + } else { + a_crash(); + } + } +#else if (extra & 1) a_crash(); +#endif if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) { n0 = n; goto copy_free_ret; } newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; if (oldlen == newlen) return p; +#ifdef __LITEOS_A__ + if (g_enable_check) { + goto copy_realloc; + } +#endif base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE); if (base == (void *)-1) goto copy_realloc; @@ -396,8 +466,15 @@ void *realloc(void *p, size_t n) if (icsize = split->psize = n | C_INUSE; split->csize = next->psize = n0-n | C_INUSE; +#ifdef __LITEOS_A__ + calculate_checksum(self, next); + unlock(g_mem_lock); +#endif __bin_chunk(split); return CHUNK_TO_MEM(self); } @@ -414,6 +491,16 @@ void *realloc(void *p, size_t n) next = NEXT_CHUNK(next); self->csize = next->psize = n0+nsize | C_INUSE; trim(self, n); +#ifdef __LITEOS_A__ + if (g_enable_check) { + int status = delete_node(p); + if (status != 0) { + get_free_trace(CHUNK_TO_MEM(self)); + a_crash(); + } + insert_node(CHUNK_TO_MEM(self), CHUNK_SIZE(self)); + } +#endif unlock(mal.split_merge_lock); return CHUNK_TO_MEM(self); } @@ -444,8 +531,15 @@ void __bin_chunk(struct chunk *self) /* Since we hold split_merge_lock, only transition from free to * in-use can race; in-use to free is impossible */ +#ifdef __LITEOS_A__ + lock(g_mem_lock); +#endif size_t psize = self->psize & C_INUSE ? 0 : CHUNK_PSIZE(self); size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next); +#ifdef __LITEOS_A__ + calculate_checksum(self, next); + unlock(g_mem_lock); +#endif if (psize) { int i = bin_index(psize); @@ -471,9 +565,15 @@ void __bin_chunk(struct chunk *self) int i = bin_index(size); lock_bin(i); - +#ifdef __LITEOS_A__ + lock(g_mem_lock); +#endif self->csize = size; next->psize = size; +#ifdef __LITEOS_A__ + calculate_checksum(self, next); + unlock(g_mem_lock); +#endif bin_chunk(self, i); unlock(mal.split_merge_lock); @@ -482,7 +582,7 @@ void __bin_chunk(struct chunk *self) uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE; uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE; int e = errno; -#if 1 +#ifndef __LITEOS_A__ __madvise((void *)a, b-a, MADV_DONTNEED); #else __mmap((void *)a, b-a, PROT_READ|PROT_WRITE, @@ -500,7 +600,22 @@ static void unmap_chunk(struct chunk *self) char *base = (char *)self - extra; size_t len = CHUNK_SIZE(self) + extra; /* Crash on double free */ +#ifdef __LITEOS_A__ + if (extra & 1) { + if (g_enable_check) { + get_free_trace(CHUNK_TO_MEM(self)); + a_crash(); + } else { + a_crash(); + } + } + if (g_enable_check) { + base -= PAGE_SIZE; + len += PAGE_SIZE << 1; + } +#else if (extra & 1) a_crash(); +#endif int e = errno; __munmap(base, len); errno = e; @@ -511,29 +626,73 @@ void free(void *p) if (!p) return; struct chunk *self = MEM_TO_CHUNK(p); +#ifdef __LITEOS_A__ + if (g_enable_check) { + if (!IS_MMAPPED(self)) { + check_chunk_integrity(self); + } + int status = delete_node(p); + if (status != 0) { + get_free_trace(p); + a_crash(); + } + } +#endif if (IS_MMAPPED(self)) unmap_chunk(self); - else + else { +#ifdef __LITEOS_A__ + if (g_enable_check) { + insert_free_tail(self); + if (g_recycle_size >= RECYCLE_SIZE_MAX) { + clean_recycle_list(false); + return; + } + if (g_recycle_num < RECYCLE_MAX) { + return; + } + self = get_free_head(); + } +#endif __bin_chunk(self); + } } void __malloc_donate(char *start, char *end) { +#ifdef __LITEOS_A__ + size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - BLOCK_HEAD); +#else size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - OVERHEAD); +#endif size_t align_end_down = (SIZE_ALIGN-1) & (uintptr_t)end; /* Getting past this condition ensures that the padding for alignment * and header overhead will not overflow and will leave a nonzero * multiple of SIZE_ALIGN bytes between start and end. */ +#ifdef __LITEOS_A__ + if (end - start <= BLOCK_HEAD + align_start_up + align_end_down) + return; + start += align_start_up + BLOCK_HEAD; +#else if (end - start <= OVERHEAD + align_start_up + align_end_down) return; start += align_start_up + OVERHEAD; +#endif end -= align_end_down; +#ifdef __LITEOS_A__ + lock(g_mem_lock); +#endif struct chunk *c = MEM_TO_CHUNK(start), *n = MEM_TO_CHUNK(end); c->psize = n->csize = C_INUSE; c->csize = n->psize = C_INUSE | (end-start); +#ifdef __LITEOS_A__ + calculate_checksum(c, n); + insert_block_list(c); + unlock(g_mem_lock); +#endif __bin_chunk(c); } diff --git a/src/malloc/oldmalloc/malloc_impl.h b/src/malloc/oldmalloc/malloc_impl.h index e1cf4774c..0fdf614f4 100644 --- a/src/malloc/oldmalloc/malloc_impl.h +++ b/src/malloc/oldmalloc/malloc_impl.h @@ -2,24 +2,90 @@ #define MALLOC_IMPL_H #include +#ifdef __LITEOS_A__ +#include + +#define BACKTRACE_DEPTH_MAX 5 /* The max depth of backtrace */ +#define BACKTRACE_START_OFFSET 0 /* The start offset of backtrace */ +#define SECONDARY_CALL_OFFSET 2 /* The backtrace offset for secondary call backtrace() */ +#define BACKTRACE_OFFSET (BACKTRACE_START_OFFSET + SECONDARY_CALL_OFFSET) +#define PREFIX_PLACE_HOLDER 10 /* Reserve positions for file name prefix "pid()_" */ +#define PTHREAD_NUM_MAX 128 /* Same as number of task of kernel */ +#define NODE_MAGIC 0xFCFCFCFC /* Magic number for node of chunk */ +#define FREE_MAGIC 0xFE /* Magic number for filling freed heap memory not recycled to heap pool */ +#define RECYCLE_MAX 128 /* The queue size for free() to recycle */ +#define RECYCLE_SIZE_MAX 0x300000 /* The max sum size of freed chunk for recycle list */ +#define ITEM_BUFFER_SIZE 256 /* The buffer max size for one item of memory debug info */ +#define CHECK_POINT_TRACE_MAX 2 /* The trace max for check point */ + +hidden void *__expand_heap(size_t *); + +hidden void __malloc_donate(char *, char *); + +hidden void *__memalign(size_t, size_t); +#else #include "dynlink.h" +#endif struct chunk { +#ifdef __LITEOS_A__ + unsigned int checksum; +#endif size_t psize, csize; struct chunk *next, *prev; }; struct bin { +#ifdef __LITEOS_A__ + unsigned int checksum; +#endif volatile int lock[2]; struct chunk *head; struct chunk *tail; }; +#ifdef __LITEOS_A__ +struct heap_block { + struct heap_block *next; + struct heap_block *prev; +}; + +struct list { + struct list *prev; + struct list *next; +}; + +struct node { + short tid, pid; + void *ptr; + size_t size; + void *lr[BACKTRACE_DEPTH_MAX]; + struct list list; +}; + +struct stat_bin { + volatile int lock[2]; + struct list head; + size_t t_total_size; +}; + +#define ROUNDUP(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) +#define SIZE_ALIGN ROUNDUP(sizeof(struct chunk), 0x10) +#define SIZE_MASK (-SIZE_ALIGN) +#define OVERHEAD (sizeof(struct chunk)) +#define BLOCK_HEAD (sizeof(struct heap_block) + OVERHEAD) +#define CHUNK_BLOCK_OFFSET (sizeof(struct heap_block)) +#define CHUNK_TO_BLOCK(c) (struct heap_block *)((char *)(c) - CHUNK_BLOCK_OFFSET) +#define BLOCK_TO_CHUNK(p) (struct chunk *)((char *)(p) + CHUNK_BLOCK_OFFSET) +#define MMAP_THRESHOLD (0x1c00*(4*sizeof(size_t))) +#define DONTCARE SIZE_ALIGN +#else #define SIZE_ALIGN (4*sizeof(size_t)) #define SIZE_MASK (-SIZE_ALIGN) #define OVERHEAD (2*sizeof(size_t)) #define MMAP_THRESHOLD (0x1c00*SIZE_ALIGN) #define DONTCARE 16 +#endif #define RECLAIM 163840 #define CHUNK_SIZE(c) ((c)->csize & -2) @@ -28,7 +94,11 @@ struct bin { #define NEXT_CHUNK(c) ((struct chunk *)((char *)(c) + CHUNK_SIZE(c))) #define MEM_TO_CHUNK(p) (struct chunk *)((char *)(p) - OVERHEAD) #define CHUNK_TO_MEM(c) (void *)((char *)(c) + OVERHEAD) +#ifdef __LITEOS_A__ +#define BIN_TO_CHUNK(i) (&mal.bins[i].checksum) +#else #define BIN_TO_CHUNK(i) (MEM_TO_CHUNK(&mal.bins[i].head)) +#endif #define C_INUSE ((size_t)1) @@ -36,4 +106,21 @@ struct bin { hidden void __bin_chunk(struct chunk *); +#ifdef __LITEOS_A__ +hidden extern int __malloc_replaced; + +hidden extern bool g_enable_check; +hidden extern int g_recycle_num; +hidden extern size_t g_recycle_size; +hidden extern int g_mem_lock[]; +hidden extern void insert_node(void *ptr, size_t size); +hidden extern int delete_node(void *ptr); +hidden extern void insert_block_list(struct chunk *ptr); +hidden extern void insert_free_tail(struct chunk *self); +hidden extern struct chunk *get_free_head(void); +hidden extern void get_free_trace(void *ptr); +hidden extern void clean_recycle_list(bool clean_all); +hidden extern void check_chunk_integrity(struct chunk *cur); +hidden extern void calculate_checksum(struct chunk *cur, struct chunk *next); +#endif #endif diff --git a/src/malloc/realloc.c b/src/malloc/realloc.c index fb0e8b7c4..559ac91b7 100644 --- a/src/malloc/realloc.c +++ b/src/malloc/realloc.c @@ -1,6 +1,14 @@ +#ifndef HOOK_ENABLE #include +#ifdef USE_JEMALLOC +extern void* je_realloc(void* p, size_t newsize); +#endif void *realloc(void *p, size_t n) { +#ifdef USE_JEMALLOC + return je_realloc(p, n); +#endif return __libc_realloc(p, n); } +#endif diff --git a/porting/linux/user/src/misc/aarch64/fstat.s b/src/misc/aarch64/linux/fstat.s similarity index 100% rename from porting/linux/user/src/misc/aarch64/fstat.s rename to src/misc/aarch64/linux/fstat.s diff --git a/porting/linux/user/src/misc/aarch64/syscall.s b/src/misc/aarch64/linux/syscall.s similarity index 100% rename from porting/linux/user/src/misc/aarch64/syscall.s rename to src/misc/aarch64/linux/syscall.s diff --git a/src/misc/gethostid.c b/src/misc/gethostid.c index 25bb35db8..b7a5b3f7f 100644 --- a/src/misc/gethostid.c +++ b/src/misc/gethostid.c @@ -1,6 +1,39 @@ +#ifdef __LITEOS_A__ +#define _GNU_SOURCE + +#include +#include +#include + +#define HOSTID_NUM 16 +#else #include +#endif long gethostid() { +#ifdef __LITEOS_A__ + int ret; + struct in_addr in; + struct hostent *hst = NULL; + char **p = NULL; + char host[128] = {0}; + + ret = gethostname(host, sizeof(host)); + if (ret || host[0] == '\0') { + return -1; + } + + hst = gethostbyname(host); + if (hst == NULL) { + return -1; + } + + p = hst->h_addr_list; + memcpy(&in.s_addr, *p, sizeof(in.s_addr)); + + return (long)((in.s_addr << HOSTID_NUM) | (in.s_addr >> HOSTID_NUM)); +#else return 0; +#endif } diff --git a/src/misc/getpriority.c b/src/misc/getpriority.c index 5c0b1682b..379b3514d 100644 --- a/src/misc/getpriority.c +++ b/src/misc/getpriority.c @@ -3,7 +3,11 @@ int getpriority(int which, id_t who) { +#ifdef __LITEOS_A__ + return syscall(SYS_getpriority, which, who); +#else int ret = syscall(SYS_getpriority, which, who); if (ret < 0) return ret; return 20-ret; +#endif } diff --git a/src/misc/getrlimit.c b/src/misc/getrlimit.c index 2ab2f0f4f..c970bb056 100644 --- a/src/misc/getrlimit.c +++ b/src/misc/getrlimit.c @@ -6,6 +6,17 @@ int getrlimit(int resource, struct rlimit *rlim) { +#ifdef __LITEOS_A__ + unsigned long long k_rlim[2]; + + if (syscall(SYS_getrlimit, resource, k_rlim) < 0) + return -1; + + rlim->rlim_cur = k_rlim[0]; + rlim->rlim_max = k_rlim[1]; + + return 0; +#else unsigned long k_rlim[2]; int ret = syscall(SYS_prlimit64, 0, resource, 0, rlim); if (!ret) { @@ -21,6 +32,7 @@ int getrlimit(int resource, struct rlimit *rlim) FIX(rlim->rlim_cur); FIX(rlim->rlim_max); return 0; +#endif } weak_alias(getrlimit, getrlimit64); diff --git a/porting/liteos_a/user/src/misc/realpath.c b/src/misc/liteos_a/realpath.c similarity index 100% rename from porting/liteos_a/user/src/misc/realpath.c rename to src/misc/liteos_a/realpath.c diff --git a/src/misc/lockf.c b/src/misc/lockf.c index 16a80bec1..17dcc0ef3 100644 --- a/src/misc/lockf.c +++ b/src/misc/lockf.c @@ -1,6 +1,7 @@ #include #include #include +#include int lockf(int fd, int op, off_t size) { @@ -9,6 +10,8 @@ int lockf(int fd, int op, off_t size) .l_whence = SEEK_CUR, .l_len = size, }; + + UNSUPPORTED_API_VOID(LITEOS_A); switch (op) { case F_TEST: l.l_type = F_RDLCK; diff --git a/src/misc/nftw.c b/src/misc/nftw.c index 8dcff7fef..213794a7c 100644 --- a/src/misc/nftw.c +++ b/src/misc/nftw.c @@ -1,6 +1,8 @@ #include #include +#ifndef __LITEOS_A__ #include +#endif #include #include #include @@ -27,8 +29,10 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct history new; int type; int r; +#ifndef __LITEOS_A__ int dfd; int err; +#endif struct FTW lev; if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) { @@ -37,7 +41,15 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, else if (errno != EACCES) return -1; else type = FTW_NS; } else if (S_ISDIR(st.st_mode)) { +#ifdef __LITEOS_A__ + if (access(path, R_OK) < 0) { + type = FTW_DNR; + } else if (flags & FTW_DEPTH) { + type = FTW_DP; + } +#else if (flags & FTW_DEPTH) type = FTW_DP; +#endif else type = FTW_D; } else if (S_ISLNK(st.st_mode)) { if (flags & FTW_PHYS) type = FTW_SL; @@ -65,12 +77,14 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, lev.base = k; } +#ifndef __LITEOS_A__ if (type == FTW_D || type == FTW_DP) { dfd = open(path, O_RDONLY); err = errno; if (dfd < 0 && err == EACCES) type = FTW_DNR; if (!fd_limit) close(dfd); } +#endif if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) return r; @@ -80,11 +94,15 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, return 0; if ((type == FTW_D || type == FTW_DP) && fd_limit) { +#ifdef __LITEOS_A__ + DIR *d = opendir(path); +#else if (dfd < 0) { errno = err; return -1; } DIR *d = fdopendir(dfd); +#endif if (d) { struct dirent *de; while ((de = readdir(d))) { @@ -105,10 +123,16 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, } } closedir(d); +#ifdef __LITEOS_A__ + } else if (errno != EACCES) { + return -1; + } +#else } else { close(dfd); return -1; } +#endif } path[l] = 0; diff --git a/src/misc/openpty.c b/src/misc/openpty.c index c10740606..66bec2878 100644 --- a/src/misc/openpty.c +++ b/src/misc/openpty.c @@ -4,6 +4,7 @@ #include #include #include +#include /* Nonstandard, but vastly superior to the standard functions */ @@ -12,6 +13,7 @@ int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struc int m, s, n=0, cs; char buf[20]; + UNSUPPORTED_API_VOID(LITEOS_A); m = open("/dev/ptmx", O_RDWR|O_NOCTTY); if (m < 0) return -1; diff --git a/src/misc/pty.c b/src/misc/pty.c index a0577147a..8cd79f15f 100644 --- a/src/misc/pty.c +++ b/src/misc/pty.c @@ -4,9 +4,11 @@ #include #include #include "syscall.h" +#include int posix_openpt(int flags) { + UNSUPPORTED_API_VOID(LITEOS_A); int r = open("/dev/ptmx", flags); if (r < 0 && errno == ENOSPC) errno = EAGAIN; return r; @@ -14,6 +16,7 @@ int posix_openpt(int flags) int grantpt(int fd) { + UNSUPPORTED_API_VOID(LITEOS_A); return 0; } diff --git a/src/misc/setrlimit.c b/src/misc/setrlimit.c index 8340aee09..d257cb23f 100644 --- a/src/misc/setrlimit.c +++ b/src/misc/setrlimit.c @@ -6,8 +6,30 @@ #define MIN(a, b) ((a)<(b) ? (a) : (b)) #define FIX(x) do{ if ((x)>=SYSCALL_RLIM_INFINITY) (x)=RLIM_INFINITY; }while(0) +#ifdef __LITEOS_A__ +static int __setrlimit(int resource, const struct rlimit *rlim) +{ + unsigned long long k_rlim[2]; + struct rlimit tmp; + if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) { + tmp = *rlim; + FIX(tmp.rlim_cur); + FIX(tmp.rlim_max); + rlim = &tmp; + } + + k_rlim[0] = rlim->rlim_cur; + k_rlim[1] = rlim->rlim_max; + return __syscall(SYS_setrlimit, resource, k_rlim); +} +#endif + struct ctx { +#ifdef __LITEOS_A__ + const struct rlimit *rlim; +#else unsigned long lim[2]; +#endif int res; int err; }; @@ -16,11 +38,18 @@ static void do_setrlimit(void *p) { struct ctx *c = p; if (c->err>0) return; +#ifdef __LITEOS_A__ + c->err = -__setrlimit(c->res, c->rlim); +#else c->err = -__syscall(SYS_setrlimit, c->res, c->lim); +#endif } int setrlimit(int resource, const struct rlimit *rlim) { +#ifdef __LITEOS_A__ + struct ctx c = { .res = resource, .rlim = rlim, .err = -1 }; +#else struct rlimit tmp; if (SYSCALL_RLIM_INFINITY != RLIM_INFINITY) { tmp = *rlim; @@ -36,6 +65,7 @@ int setrlimit(int resource, const struct rlimit *rlim) .lim[1] = MIN(rlim->rlim_max, MIN(-1UL, SYSCALL_RLIM_INFINITY)), .res = resource, .err = -1 }; +#endif __synccall(do_setrlimit, &c); if (c.err) { if (c.err>0) errno = c.err; diff --git a/src/misc/syslog.c b/src/misc/syslog.c index 7dc0c1be5..f60e1abd6 100644 --- a/src/misc/syslog.c +++ b/src/misc/syslog.c @@ -11,6 +11,7 @@ #include #include "lock.h" #include "fork_impl.h" +#include static volatile int lock[1]; static char log_ident[32]; @@ -22,6 +23,7 @@ volatile int *const __syslog_lockptr = lock; int setlogmask(int maskpri) { + UNSUPPORTED_API_VOID(LITEOS_A); LOCK(lock); int ret = log_mask; if (maskpri) log_mask = maskpri; @@ -40,6 +42,7 @@ static const struct { void closelog(void) { int cs; + UNSUPPORTED_API_VOID(LITEOS_A); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); LOCK(lock); close(log_fd); @@ -50,6 +53,7 @@ void closelog(void) static void __openlog() { + UNSUPPORTED_API_VOID(LITEOS_A); log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr); } @@ -137,6 +141,7 @@ static void __vsyslog(int priority, const char *message, va_list ap) void syslog(int priority, const char *message, ...) { + UNSUPPORTED_API_VOID(LITEOS_A); va_list ap; va_start(ap, message); __vsyslog(priority, message, ap); diff --git a/src/misc/wordexp.c b/src/misc/wordexp.c index db83a69f3..ba6e37380 100644 --- a/src/misc/wordexp.c +++ b/src/misc/wordexp.c @@ -10,6 +10,7 @@ #include #include #include "pthread_impl.h" +#include static void reap(pid_t pid) { @@ -170,6 +171,7 @@ nospace: int wordexp(const char *restrict s, wordexp_t *restrict we, int flags) { int r, cs; + UNSUPPORTED_API_VOID(LITEOS_A); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); r = do_wordexp(s, we, flags); pthread_setcancelstate(cs, 0); diff --git a/src/mman/mlock.c b/src/mman/mlock.c index 71af582fe..b07eac4a3 100644 --- a/src/mman/mlock.c +++ b/src/mman/mlock.c @@ -1,8 +1,10 @@ #include #include "syscall.h" +#include int mlock(const void *addr, size_t len) { + UNSUPPORTED_API_VOID(LITEOS_A); #ifdef SYS_mlock return syscall(SYS_mlock, addr, len); #else diff --git a/src/mman/mlockall.c b/src/mman/mlockall.c index 0ba4e662c..332111ce2 100644 --- a/src/mman/mlockall.c +++ b/src/mman/mlockall.c @@ -1,7 +1,9 @@ #include #include "syscall.h" +#include int mlockall(int flags) { + UNSUPPORTED_API_VOID(LITEOS_A); return syscall(SYS_mlockall, flags); } diff --git a/src/mman/msync.c b/src/mman/msync.c index fcd8cdf9f..45c969554 100644 --- a/src/mman/msync.c +++ b/src/mman/msync.c @@ -1,7 +1,9 @@ #include #include "syscall.h" +#include int msync(void *start, size_t len, int flags) { + UNSUPPORTED_API_VOID(LITEOS_A); return syscall_cp(SYS_msync, start, len, flags); } diff --git a/src/mman/munlock.c b/src/mman/munlock.c index 2cccef0c5..014bfeae0 100644 --- a/src/mman/munlock.c +++ b/src/mman/munlock.c @@ -1,7 +1,9 @@ #include #include "syscall.h" +#include int munlock(const void *addr, size_t len) { + UNSUPPORTED_API_VOID(LITEOS_A); return syscall(SYS_munlock, addr, len); } diff --git a/src/mman/munlockall.c b/src/mman/munlockall.c index 6e9d39d68..a7638b91b 100644 --- a/src/mman/munlockall.c +++ b/src/mman/munlockall.c @@ -1,7 +1,9 @@ #include #include "syscall.h" +#include int munlockall(void) { + UNSUPPORTED_API_VOID(LITEOS_A); return syscall(SYS_munlockall); } diff --git a/src/mman/posix_madvise.c b/src/mman/posix_madvise.c index e5e5acb84..d49d08be0 100644 --- a/src/mman/posix_madvise.c +++ b/src/mman/posix_madvise.c @@ -1,9 +1,11 @@ #define _GNU_SOURCE #include #include "syscall.h" +#include int posix_madvise(void *addr, size_t len, int advice) { + UNSUPPORTED_API_VOID(LITEOS_A); if (advice == MADV_DONTNEED) return 0; return -__syscall(SYS_madvise, addr, len, advice); } diff --git a/src/mman/shm_open.c b/src/mman/shm_open.c index 79784bd30..39c09537f 100644 --- a/src/mman/shm_open.c +++ b/src/mman/shm_open.c @@ -5,6 +5,7 @@ #include #include #include +#include char *__shm_mapname(const char *name, char *buf) { @@ -26,6 +27,7 @@ char *__shm_mapname(const char *name, char *buf) int shm_open(const char *name, int flag, mode_t mode) { + UNSUPPORTED_API_VOID(LITEOS_A); int cs; char buf[NAME_MAX+10]; if (!(name = __shm_mapname(name, buf))) return -1; @@ -37,6 +39,7 @@ int shm_open(const char *name, int flag, mode_t mode) int shm_unlink(const char *name) { + UNSUPPORTED_API_VOID(LITEOS_A); char buf[NAME_MAX+10]; if (!(name = __shm_mapname(name, buf))) return -1; return unlink(name); diff --git a/src/mq/mq_close.c b/src/mq/mq_close.c index a61f094d4..e193b038b 100644 --- a/src/mq/mq_close.c +++ b/src/mq/mq_close.c @@ -3,5 +3,9 @@ int mq_close(mqd_t mqd) { +#ifdef __LITEOS_A__ + return syscall(SYS_mqclose, mqd); +#else return syscall(SYS_close, mqd); +#endif } diff --git a/src/mq/mq_notify.c b/src/mq/mq_notify.c index 221591c73..5bec63272 100644 --- a/src/mq/mq_notify.c +++ b/src/mq/mq_notify.c @@ -5,7 +5,11 @@ #include #include #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +#include "pthread_impl.h" +#endif +#ifndef __LITEOS_A__ struct args { pthread_barrier_t barrier; int sock; @@ -28,9 +32,18 @@ static void *start(void *p) func(val); return 0; } +#endif int mq_notify(mqd_t mqd, const struct sigevent *sev) { +#ifdef __LITEOS_A__ + if (!sev || sev->sigev_notify != SIGEV_THREAD) { + return syscall(SYS_mq_notify, mqd, sev); + } + + errno = ENOTSUP; + return -1; +#else struct args args = { .sev = sev }; pthread_attr_t attr; pthread_t td; @@ -64,10 +77,15 @@ int mq_notify(mqd_t mqd, const struct sigevent *sev) sev2.sigev_value.sival_ptr = (void *)&zeros; if (syscall(SYS_mq_notify, mqd, &sev2) < 0) { +#ifdef FEATURE_PTHREAD_CANCEL pthread_cancel(td); +#else + __syscall(SYS_tkill, td->tid, SIGCANCEL); +#endif __syscall(SYS_close, s); return -1; } return 0; +#endif } diff --git a/src/network/ent.c b/src/network/ent.c index c6e012306..12215ee99 100644 --- a/src/network/ent.c +++ b/src/network/ent.c @@ -1,22 +1,245 @@ + +#ifdef __LITEOS_A__ +#define _BSD_SOURCE +#include +#include +#include +#include +#include +#include +#endif #include +#include + +#ifdef __LITEOS_A__ +#define HOST_FILE_PATH "/etc/hosts" +#define NET_FILE_PATH "/etc/networks" +#define BUFFER_SIZE 200 +#define INET_ADDR_LEN 4 +#define INET6_ADDR_LEN 16 +#define MAX_ALIAS_NO 10 +#define MAX_ADDR_NO 10 +#define MAX_NAME_LEN 20 +#define MAX_ALIAS_LEN 20 + +struct hostdata { + FILE *hostf; + int stayopen; + struct hostent he; + char buf[BUFFER_SIZE]; + char *addr[MAX_ADDR_NO + 1]; + char addr_list[MAX_ADDR_NO + 1][INET6_ADDR_LEN + 1]; + char *aliases[MAX_ALIAS_NO + 1]; +}; + +struct netdata { + FILE *netf; + int stayopen; + struct netent ne; + char buf[BUFFER_SIZE]; + char *aliases[MAX_ALIAS_NO + 1]; +}; + +static struct hostdata *hd = NULL; +static struct netdata *nd = NULL; + +static int gethostent_resolv() +{ + char *ch, *ptr = NULL; + if (hd->buf[0] == '#') + return -1; + + ch = strchr(hd->buf, '#'); + if (ch) + *ch = '\0'; + ch = strtok_r(hd->buf, " \t\n", &ptr); + if (!ch) + return -1; + + if (inet_pton(AF_INET, ch, hd->addr_list[0]) == 1) { + hd->he.h_addrtype = AF_INET; + hd->he.h_length = INET_ADDR_LEN; + hd->addr_list[0][INET_ADDR_LEN] = '\0'; + } else if (inet_pton(AF_INET6, ch, hd->addr_list[0]) == 1) { + hd->he.h_addrtype = AF_INET6; + hd->he.h_length = INET6_ADDR_LEN; + hd->addr_list[0][INET6_ADDR_LEN] = '\0'; + } else { + return -1; + } + + hd->addr[0] = hd->addr_list[0]; + hd->addr[1] = NULL; + hd->he.h_addr_list = hd->addr; + + ch = strtok_r(NULL, " \t\n", &ptr); + if (!ch) + return -1; + hd->he.h_name = ch; + + int i = 0; + while (i < MAX_ALIAS_NO) { + ch = strtok_r(NULL, " \t\n", &ptr); + hd->aliases[i++] = ch; + } + + hd->aliases[MAX_ALIAS_NO] = NULL; + hd->he.h_aliases = hd->aliases; + return 0; +} +#endif -void sethostent(int x) +void sethostent(int stayopen) { +#ifdef __LITEOS_A__ + if (hd == NULL) { + if ((hd = malloc(sizeof(struct hostdata))) == NULL) { + h_errno = NO_RECOVERY; + return; + } + hd->hostf = NULL; + } + if (hd->hostf == NULL) + hd->hostf = fopen(NET_FILE_PATH, "r"); + else + rewind(hd->hostf); + hd->stayopen = stayopen; +#endif } -struct hostent *gethostent() +struct hostent *gethostent(void) { +#ifdef __LITEOS_A__ + if (hd == NULL) { + if ((hd = malloc(sizeof(struct hostdata))) == NULL) { + h_errno = NO_RECOVERY; + return NULL; + } + hd->hostf = NULL; + hd->stayopen = 0; + } + if (!hd->hostf && !(hd->hostf = fopen(HOST_FILE_PATH, "r"))) { + h_errno = NO_RECOVERY; + free(hd); + hd =NULL; + return NULL; + } + do { + if (!fgets(hd->buf, BUFFER_SIZE, hd->hostf)) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + } while (gethostent_resolv()); + + return &(hd->he); +#else + return 0; +#endif +} + +#ifdef __LITEOS_A__ +static int getnetent_resolv() +{ + if (nd->buf[0] == '#') + return -1; + char *ch, *ptr = NULL; + ch = strchr(nd->buf, '#'); + if (ch) + *ch = '\0'; + ch = strtok_r(nd->buf, " \t\n", &ptr); + if (!ch) + return -1; + nd->ne.n_name = ch; + nd->ne.n_addrtype = AF_INET; + + ch = strtok_r(NULL, " \t\n", &ptr); + if (!ch) + return -1; + nd->ne.n_net = inet_network(ch); + + int i = 0; + while ((ch = strtok_r(NULL, " \t\n", &ptr)) != NULL && i < MAX_ALIAS_NO) { + nd->aliases[i++] = ch; + } + nd->aliases[i] = NULL; + nd->ne.n_aliases = nd->aliases; return 0; } +#endif -struct netent *getnetent() +struct netent *getnetent(void) { +#ifdef __LITEOS_A__ + if (nd == NULL) { + if ((nd = malloc(sizeof(struct netdata))) == NULL) { + errno = ENOMEM; + return NULL; + } + nd->netf = NULL; + nd->stayopen = 0; + } + if (nd->netf == NULL && (nd->netf = fopen(NET_FILE_PATH, "r")) == NULL) { + errno = ENOENT; + free(nd); + nd = NULL; + return NULL; + } + while (1) { + if (fgets(nd->buf, BUFFER_SIZE, nd->netf) == NULL) + break; + if (getnetent_resolv() == 0) + return &(nd->ne); + } + + return NULL; +#else return 0; +#endif } void endhostent(void) { +#ifdef __LITEOS_A__ + if (hd == NULL) + return; + if (hd->stayopen == 0) { + if (hd->hostf != NULL) + fclose(hd->hostf); + free(hd); + hd = NULL; + } +#endif } +#ifdef __LITEOS_A__ +void setnetent(int stayopen) +{ + if (nd == NULL) { + if ((nd = malloc(sizeof(struct netdata))) == NULL) { + errno = ENOMEM; + return; + } + nd->netf = NULL; + } + if (nd->netf == NULL) + nd->netf = fopen(NET_FILE_PATH, "r"); + else + rewind(nd->netf); + nd->stayopen = stayopen; +} + +void endnetent(void) +{ + if (nd == NULL) + return; + if (!nd->stayopen) { + if (nd->netf != NULL) + fclose(nd->netf); + free(nd); + nd = NULL; + } +} +#else weak_alias(sethostent, setnetent); weak_alias(endhostent, endnetent); +#endif diff --git a/src/network/ether.c b/src/network/ether.c index 4304a972f..839f5581f 100644 --- a/src/network/ether.c +++ b/src/network/ether.c @@ -1,6 +1,14 @@ #include #include #include +#include +#ifdef __LITEOS_A__ +#include +#include + +#define ETHER_FILE_PATH "/etc/ethers" +#define BUFFER_SIZE 200 +#endif struct ether_addr *ether_aton_r (const char *x, struct ether_addr *p_a) { @@ -42,17 +50,109 @@ char *ether_ntoa (const struct ether_addr *p_a) { return ether_ntoa_r (p_a, x); } -int ether_line(const char *l, struct ether_addr *e, char *hostname) +int ether_line(const char *line, struct ether_addr *addr, char *hostname) { +#ifdef __LITEOS_A__ + char buf[BUFFER_SIZE], *ch, *ptr = NULL; + if (line[0] == '#') { + errno = EINVAL; + return -1; + } + strcpy(buf, line); + ch = strchr(buf, '#'); + if (ch) + *ch = '\0'; + ch = strtok_r(buf, " \t\n", &ptr); + ch = strtok_r(NULL, " \t\n", &ptr); + if (ch == NULL) { + errno = EINVAL; + return -1; + } + strcpy(hostname, ch); + + unsigned int ret; + ch = &buf[0]; + for (int i = 0; i < 6; i++) { + ch = strtok_r(ch, ":", &ptr); + if (ch == NULL || sscanf(ch, "%x", &ret) != 1) { + errno = EINVAL; + return -1; + } + addr->ether_addr_octet[i] = (uint8_t)ret; + ch = NULL; + } + + return 0; +#else return -1; +#endif } -int ether_ntohost(char *hostname, const struct ether_addr *e) -{ +int ether_ntohost(char *hostname, const struct ether_addr *addr) +{ +#ifdef __LITEOS_A__ + FILE *f = NULL; + char buf[BUFFER_SIZE + 1]; + char temp_name[BUFFER_SIZE]; + struct ether_addr *temp_addr; + + if (!(f = fopen(ETHER_FILE_PATH, "r"))) + return ENOENT; + temp_addr = (struct ether_addr *)malloc(sizeof(struct ether_addr)); + if (temp_addr == NULL) { + fclose(f); + return ENOMEM; + } + while (fgets(buf, BUFFER_SIZE, f)) { + if (ether_line(buf, temp_addr, temp_name) != 0) + continue; + if (memcmp(addr, temp_addr, 6) == 0) { + strcpy(hostname, temp_name); + free(temp_addr); + fclose(f); + return 0; + } + } + + free(temp_addr); + fclose(f); return -1; +#else + return -1; +#endif } -int ether_hostton(const char *hostname, struct ether_addr *e) + +int ether_hostton(const char *hostname, struct ether_addr *addr) { +#ifdef __LITEOS_A__ + FILE *f = NULL; + char buf[BUFFER_SIZE + 1]; + char temp_name[BUFFER_SIZE]; + struct ether_addr *temp_addr; + + temp_addr = (struct ether_addr *)malloc(sizeof(struct ether_addr)); + if (temp_addr == NULL) + return ENOMEM; + if (!(f = fopen(ETHER_FILE_PATH, "r"))) { + free(temp_addr); + return ENOENT; + } + while (fgets(buf, BUFFER_SIZE, f)) { + if (ether_line(buf, temp_addr, temp_name) != 0) + continue; + if (strcmp(hostname, temp_name) == 0) { + memcpy(addr, temp_addr, 6); + free(temp_addr); + fclose(f); + return 0; + } + } + + free(temp_addr); + fclose(f); + return -1; +#else return -1; +#endif } diff --git a/src/network/getaddrinfo.c b/src/network/getaddrinfo.c index 0d0a6222d..1a9c8eac4 100644 --- a/src/network/getaddrinfo.c +++ b/src/network/getaddrinfo.c @@ -9,6 +9,105 @@ #include #include "lookup.h" +#ifdef LINUX_MUSL_SUPPORTED +#define DNS_QUERY_SUCCESS 0 +#define DNS_QUERY_COMMOM_FAIL (-1) +#define GETADDRINFO_PRINT_DEBUG(...) + +int reportdnsresult(int netid, char* name, int usedtime, int queryret, struct addrinfo *res, struct queryparam *param) +{ +#if OHOS_DNS_PROXY_BY_NETSYS + if (dns_post_result_to_netsys_cache(netid, name, usedtime, queryret, res, param) == 0) { + GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext reportdnsresult fail\n"); + } +#endif + return 0; +} + +static custom_dns_resolver g_customdnsresolvehook; +static pthread_key_t g_recursiveKey; +static int* g_recursive; + +int setdnsresolvehook(custom_dns_resolver hookfunc) +{ + int ret = -1; + if (g_customdnsresolvehook) { + return ret; + } + if (hookfunc) { + g_customdnsresolvehook = hookfunc; + pthread_key_create(&g_recursiveKey, NULL); + ret = 0; + } + return ret; +} + +int removednsresolvehook() +{ + g_customdnsresolvehook = NULL; + if (g_recursive) { + free(g_recursive); + g_recursive = NULL; + } + if (g_recursiveKey) { + pthread_key_delete(g_recursiveKey); + g_recursiveKey = NULL; + } + return 0; +} +#endif + +#ifdef LINUX_MUSL_SUPPORTED +int getaddrinfo(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res) +{ + struct queryparam param = {0, 0, 0, 0, NULL}; + return getaddrinfo_ext(host, serv, hint, res, ¶m); +} + +int getaddrinfo_ext(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, + struct addrinfo **restrict res, struct queryparam *restrict param) +{ + int netid = 0; + int type = 0; + int usedtime = 0; + time_t t_start, t_end; + + if (!host && !serv) return EAI_NONAME; + if (!param) { + netid = 0; + type = 0; + } else { + netid = param->qp_netid; + type = param->qp_type; + } + + if (g_customdnsresolvehook) { + g_recursive = pthread_getspecific(g_recursiveKey); + if (g_recursive == NULL) { + int *newRecursive = malloc(sizeof(int)); + *newRecursive = 0; + pthread_setspecific(g_recursiveKey, newRecursive); + g_recursive = newRecursive; + } + if (*g_recursive == 0) { + ++(*g_recursive); + int ret = g_customdnsresolvehook(host, serv, hint, res); + --(*g_recursive); + return ret; + } + } + +#if OHOS_DNS_PROXY_BY_NETSYS + GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext netid:%{public}d type:%{public}d \n", netid, type); + if (type == QEURY_TYPE_NORMAL) { + if (dns_get_addr_info_from_netsys_cache2(netid, host, serv, hint, res) == 0) { + GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext get from netsys cache OK\n"); + reportdnsresult(netid, host, 0, DNS_QUERY_SUCCESS, *res, ¶m); + return 0; + } + } +#endif +#else // LINUX_MUSL_SUPPORTED int getaddrinfo(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res) { #if OHOS_DNS_PROXY_BY_NETSYS @@ -17,6 +116,7 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru return 0; } #endif +#endif // LINUX_MUSL_SUPPORTED struct service ports[MAXSERVS]; struct address addrs[MAXADDRS]; @@ -25,7 +125,9 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru int family = AF_UNSPEC, flags = 0, proto = 0, socktype = 0; struct aibuf *out; +#ifndef LINUX_MUSL_SUPPORTED if (!host && !serv) return EAI_NONAME; +#endif if (hint) { family = hint->ai_family; @@ -92,11 +194,24 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru } } +#ifdef LINUX_MUSL_SUPPORTED + t_start = time(NULL); + nservs = __lookup_serv(ports, serv, proto, socktype, flags); + if (nservs < 0) return nservs; + + naddrs = lookup_name_ext(addrs, canon, host, family, flags, netid); + t_end = time(NULL); + if (naddrs < 0) { + reportdnsresult(netid, host, difftime(t_end, t_start), DNS_QUERY_COMMOM_FAIL, NULL, ¶m); + return naddrs; + } +#else nservs = __lookup_serv(ports, serv, proto, socktype, flags); if (nservs < 0) return nservs; naddrs = __lookup_name(addrs, canon, host, family, flags); if (naddrs < 0) return naddrs; +#endif nais = nservs * naddrs; canon_len = strlen(canon); @@ -138,8 +253,19 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru } out[0].ref = nais; *res = &out->ai; + +#ifdef LINUX_MUSL_SUPPORTED + reportdnsresult(netid, host, difftime(t_end, t_start), DNS_QUERY_SUCCESS, *res, ¶m); + int cnt = predefined_host_is_contain_host(host); +#if OHOS_DNS_PROXY_BY_NETSYS + if (type == QEURY_TYPE_NORMAL && cnt == 0) { + dns_set_addr_info_to_netsys_cache2(netid, host, serv, hint, *res); + } +#endif +#else // LINUX_MUSL_SUPPORTED #if OHOS_DNS_PROXY_BY_NETSYS dns_set_addr_info_to_netsys_cache(host, serv, hint, *res); #endif +#endif // LINUX_MUSL_SUPPORTED return 0; } diff --git a/src/network/getifaddrs.c b/src/network/getifaddrs.c index fed75bd8d..474c1005d 100644 --- a/src/network/getifaddrs.c +++ b/src/network/getifaddrs.c @@ -8,6 +8,11 @@ #include #include #include "netlink.h" +#ifdef __LITEOS_A__ +#include +#include +#define MAX_IF_NO 10 +#endif #define IFADDRS_HASH_SIZE 64 @@ -46,12 +51,27 @@ struct ifaddrs_ctx { void freeifaddrs(struct ifaddrs *ifp) { +#ifdef __LITEOS_A__ + struct ifaddrs *tmp = NULL; + while (ifp) { + if (!ifp->ifa_name) + free(ifp); + if (!ifp->ifa_addr) + free(ifp->ifa_addr); + if (!ifp->ifa_netmask) + free(ifp->ifa_netmask); + tmp = ifp->ifa_next; + free(ifp); + ifp = tmp; + } +#else struct ifaddrs *n; while (ifp) { n = ifp->ifa_next; free(ifp); ifp = n; } +#endif } static void copy_addr(struct sockaddr **r, int af, union sockany *sa, void *addr, size_t addrlen, int ifindex) @@ -204,8 +224,91 @@ static int netlink_msg_to_ifaddr(void *pctx, struct nlmsghdr *h) return 0; } +#ifdef __LITEOS_A__ +static struct ifaddrs* ifaddrs_init() +{ + struct ifaddrs *ifa = NULL; + ifa = malloc(sizeof(struct ifaddrs)); + if (!ifa) + return NULL; + ifa->ifa_name = malloc(sizeof(char) *(IF_NAMESIZE + 1)); + ifa->ifa_addr = malloc(sizeof(struct sockaddr)); + ifa->ifa_netmask = malloc(sizeof(struct sockaddr)); + ifa->ifa_next = NULL; + if (!ifa->ifa_name || !ifa->ifa_addr || !ifa->ifa_netmask) { + free(ifa->ifa_name); + free(ifa->ifa_addr); + free(ifa->ifa_netmask); + free(ifa); + return NULL; + } + return ifa; +} +#endif + int getifaddrs(struct ifaddrs **ifap) { +#ifdef __LITEOS_A__ + if (ifap == NULL) { + errno = EINVAL; + return -1; + } + + int fd, ifno, ret; + struct ifreq ifr[MAX_IF_NO]; + struct ifconf ifconfig; + struct ifaddrs *ifstart = NULL; + + ifconfig.ifc_buf = ifr; + ifconfig.ifc_len = sizeof(ifr); + if ((fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) + return -1; + if (ioctl(fd, SIOCGIFCONF, &ifconfig) < 0) + goto exit; + if (ifconfig.ifc_len % sizeof(struct ifreq)) { + errno = EINVAL; + goto exit; + } + ifno = ifconfig.ifc_len / sizeof(struct ifreq); + + if (!(ifstart = ifaddrs_init())) { + errno = ENOMEM; + goto exit; + } + struct ifaddrs *ifa = ifstart; + + for (int i = 0; i < ifno; i++) { + memcpy(ifa->ifa_name, ifr[i].ifr_name, IF_NAMESIZE); + ifa->ifa_name[IF_NAMESIZE] = '\0'; + memcpy(ifa->ifa_addr, &ifr[i].ifr_addr, sizeof(struct sockaddr)); + if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) < 0) + goto error; + memcpy(ifa->ifa_netmask, &ifr[i].ifr_netmask, sizeof(struct sockaddr)); + + if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) < 0) + goto error; + ifa->ifa_flags = ifr[i].ifr_flags; + + if (i < ifno - 1) { + ifa->ifa_next = ifaddrs_init(); + if (!ifa->ifa_next) { + errno = ENOMEM; + goto error; + } + ifa = ifa->ifa_next; + } + } + + *ifap = ifstart; + __syscall(SYS_close, fd); + return 0; + +error: + freeifaddrs(ifstart); +exit: + __syscall(SYS_close, fd); + return -1; +#else struct ifaddrs_ctx _ctx, *ctx = &_ctx; int r; memset(ctx, 0, sizeof *ctx); @@ -213,4 +316,5 @@ int getifaddrs(struct ifaddrs **ifap) if (r == 0) *ifap = &ctx->first->ifa; else freeifaddrs(&ctx->first->ifa); return r; +#endif } diff --git a/src/network/getnameinfo.c b/src/network/getnameinfo.c index 949e18115..8b0316cbf 100644 --- a/src/network/getnameinfo.c +++ b/src/network/getnameinfo.c @@ -11,9 +11,20 @@ #include #include "lookup.h" #include "stdio_impl.h" +#ifdef LINUX_MUSL_SUPPORTED +#include "network_conf_function.h" +#endif #define PTR_MAX (64 + sizeof ".in-addr.arpa") #define RR_PTR 12 +#ifdef LINUX_MUSL_SUPPORTED +#define BREAK 0 +#define CONTINUE 1 +#define FIXED_HOSTS_MAX_LENGTH 2 +#define FIXED_HOSTS_STR_MAX_LENGTH 23 + +extern char fixed_hosts[FIXED_HOSTS_MAX_LENGTH][FIXED_HOSTS_STR_MAX_LENGTH]; +#endif static char *itoa(char *p, unsigned x) { p += 3*sizeof(int); @@ -42,19 +53,41 @@ static void mkptr6(char *s, const unsigned char *ip) strcpy(s, "ip6.arpa"); } +#ifdef LINUX_MUSL_SUPPORTED +static inline int get_hosts_str(char *line, int length, FILE *f, int *i) +{ + if (f) { + return fgets(line, sizeof line, f); + } + if (*i < FIXED_HOSTS_MAX_LENGTH) { + memcpy(line, fixed_hosts[*i], strlen(fixed_hosts[*i])); + (*i)++; + return 1; + } + return NULL; +} +#endif + static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family) { char line[512], *p, *z; unsigned char _buf[1032], atmp[16]; struct address iplit; FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); +#ifndef LINUX_MUSL_SUPPORTED if (!f) return; +#endif if (family == AF_INET) { memcpy(atmp+12, a, 4); memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12); a = atmp; } +#ifdef LINUX_MUSL_SUPPORTED + int i = 0; + while (i < FIXED_HOSTS_MAX_LENGTH && get_hosts_str(line, sizeof line, f, &i)) { +#else while (fgets(line, sizeof line, f)) { +#endif if ((p=strchr(line, '#'))) *p++='\n', *p=0; for (p=line; *p && !isspace(*p); p++); @@ -79,7 +112,13 @@ static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, i break; } } +#ifdef LINUX_MUSL_SUPPORTED + if (f) { + __fclose_ca(f); + } +#else __fclose_ca(f); +#endif } static void reverse_services(char *buf, int port, int dgram) @@ -88,8 +127,13 @@ static void reverse_services(char *buf, int port, int dgram) char line[128], *p, *z; unsigned char _buf[1032]; FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf); +#ifdef LINUX_MUSL_SUPPORTED + int indexPtr = 0; + while (get_services_str(line, f, &indexPtr)) { +#else if (!f) return; while (fgets(line, sizeof line, f)) { +#endif if ((p=strchr(line, '#'))) *p++='\n', *p=0; for (p=line; *p && !isspace(*p); p++); @@ -105,7 +149,13 @@ static void reverse_services(char *buf, int port, int dgram) memcpy(buf, line, p-line); break; } +#ifdef LINUX_MUSL_SUPPORTED + if (f) { + __fclose_ca(f); + } +#else __fclose_ca(f); +#endif } static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet) diff --git a/src/network/if_indextoname.c b/src/network/if_indextoname.c index 3b368bf0d..6bcc9819f 100644 --- a/src/network/if_indextoname.c +++ b/src/network/if_indextoname.c @@ -11,7 +11,11 @@ char *if_indextoname(unsigned index, char *name) struct ifreq ifr; int fd, r; +#ifdef __LITEOS_A__ + if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; +#else if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; +#endif ifr.ifr_ifindex = index; r = ioctl(fd, SIOCGIFNAME, &ifr); __syscall(SYS_close, fd); diff --git a/src/network/if_nametoindex.c b/src/network/if_nametoindex.c index 331413c68..dc11a69b5 100644 --- a/src/network/if_nametoindex.c +++ b/src/network/if_nametoindex.c @@ -9,8 +9,11 @@ unsigned if_nametoindex(const char *name) { struct ifreq ifr; int fd, r; - +#ifdef __LITEOS_A__ + if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; +#else if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; +#endif strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); r = ioctl(fd, SIOCGIFINDEX, &ifr); __syscall(SYS_close, fd); diff --git a/src/network/inet_legacy.c b/src/network/inet_legacy.c index 621b47b05..6d91b6e17 100644 --- a/src/network/inet_legacy.c +++ b/src/network/inet_legacy.c @@ -12,12 +12,12 @@ struct in_addr inet_makeaddr(in_addr_t n, in_addr_t h) if (n < 256) h |= n<<24; else if (n < 65536) h |= n<<16; else h |= n<<8; - return (struct in_addr){ h }; + return (struct in_addr){ htonl(h) }; } in_addr_t inet_lnaof(struct in_addr in) { - uint32_t h = in.s_addr; + uint32_t h = ntohl(in.s_addr); if (h>>24 < 128) return h & 0xffffff; if (h>>24 < 192) return h & 0xffff; return h & 0xff; @@ -25,7 +25,7 @@ in_addr_t inet_lnaof(struct in_addr in) in_addr_t inet_netof(struct in_addr in) { - uint32_t h = in.s_addr; + uint32_t h = ntohl(in.s_addr); if (h>>24 < 128) return h >> 24; if (h>>24 < 192) return h >> 16; return h >> 8; diff --git a/src/network/lookup.h b/src/network/lookup.h index 9928eaca9..aa40011a4 100644 --- a/src/network/lookup.h +++ b/src/network/lookup.h @@ -64,6 +64,15 @@ hidden int __get_resolv_conf(struct resolvconf *, char *, size_t); hidden int __res_msend_rc(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int, const struct resolvconf *); hidden int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *); +hidden int lookup_name_ext(struct address buf[static MAXADDRS], char canon[static 256], const char *name, + int family, int flags, int netid); +hidden int get_resolv_conf_ext(struct resolvconf *, char *, size_t, int netid); +hidden int res_msend_rc_ext(int, int, const unsigned char *const *, const int *, unsigned char *const *, + int *, int, const struct resolvconf *); +hidden int predefined_host_name_from_hosts(struct address buf[static MAXADDRS], + char canon[static 256], const char *name, int family); +hidden int predefined_host_is_contain_host(const char *host); +hidden int res_bind_socket(int, int); #if OHOS_DNS_PROXY_BY_NETSYS #define DNS_SO_PATH "libnetsys_client.z.so" @@ -73,6 +82,10 @@ hidden int __dns_parse(const unsigned char *, int, int (*)(void *, int, const vo #define OHOS_GET_CACHE_FUNC_NAME "NetSysGetResolvCache" #define OHOS_SET_CACHE_FUNC_NAME "NetSysSetResolvCache" #define OHOS_JUDGE_IPV6_FUNC_NAME "NetSysIsIpv6Enable" +#ifdef LINUX_MUSL_SUPPORTED +#define OHOS_POST_DNS_RESULT_FUNC_NAME "NetSysPostDnsResult" +#define OHOS_GET_DEFAULT_NET_FUNC_NAME "NetSysGetDefaultNetwork" +#endif #define MAX_RESULTS 32 #define MAX_CANON_NAME 256 #define MACRO_MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -116,6 +129,23 @@ typedef int32_t (*SetCache)(uint16_t netId, struct param_wrapper param, struct a typedef int (*JudgeIpv6)(uint16_t netId); +#ifdef LINUX_MUSL_SUPPORTED +typedef int (*PostDnsResult)(int netid, char* name, int usedtime, int queryfail, + struct addrinfo *res, struct queryparam *param); + +typedef int (*GetDefaultNet)(uint16_t netId, int32_t *currentnetid); +void dns_set_addr_info_to_netsys_cache2(const int netid, const char *__restrict host, const char *__restrict serv, + const struct addrinfo *__restrict hint, struct addrinfo *res); + +int dns_get_addr_info_from_netsys_cache2(const int netid, const char *__restrict host, const char *__restrict serv, + const struct addrinfo *__restrict hint, struct addrinfo **__restrict res); + +int dns_post_result_to_netsys_cache(int netid, char* name, int usedtime, int querypass, + struct addrinfo *res, struct queryparam *param); + +int dns_get_default_network(int *currentnetid); +#endif + /* If the memory holder points to stores NULL value, try to load symbol from the * dns lib into holder; otherwise, it does nothing. */ hidden void resolve_dns_sym(void **holder, const char *symbol); @@ -130,4 +160,16 @@ int dns_get_addr_info_from_netsys_cache(const char *__restrict host, const char #endif +#ifdef LINUX_MUSL_SUPPORTED +#if OHOS_FWMARK_CLIENT_BY_NETSYS +#define FWMARKCLIENT_SO_PATH "libfwmark_client.z.so" +#define OHOS_BIND_SOCKET_FUNC_NAME "BindSocket" + +typedef int32_t (*BindSocket)(int32_t fd, uint32_t netId); + +#define OHOS_NETSYS_BIND_SOCKET_FUNC_NAME "NetSysBindSocket" + +typedef int32_t (*BindSocket_Ext)(int32_t fd, uint32_t netId); +#endif +#endif #endif diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c index 3f00d7c6c..3826746c6 100644 --- a/src/network/lookup_name.c +++ b/src/network/lookup_name.c @@ -14,6 +14,32 @@ #include "lookup.h" #include "stdio_impl.h" #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +#include + +#define BREAK 0 +#define CONTINUE 1 +#define FIXED_HOSTS_MAX_LENGTH 2 +#define FIXED_HOSTS_STR_MAX_LENGTH 23 + +char fixed_hosts[][FIXED_HOSTS_STR_MAX_LENGTH] = { + "127.0.0.1 localhost\r\n\0", + "::1 ip6-localhost\r\n\0" +}; + +static inline int get_hosts_str(char *line, int length, FILE *f, int *i) +{ + if (f) { + return fgets(line, sizeof line, f); + } + if (*i < FIXED_HOSTS_MAX_LENGTH) { + memcpy(line, fixed_hosts[*i], strlen(fixed_hosts[*i])); + (*i)++; + return 1; + } + return NULL; +} +#endif #if OHOS_PERMISSION_INTERNET uint8_t is_allow_internet(void); @@ -57,6 +83,7 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati int cnt = 0, badfam = 0, have_canon = 0; unsigned char _buf[1032]; FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); +#ifndef LINUX_MUSL_SUPPORTED if (!f) switch (errno) { case ENOENT: case ENOTDIR: @@ -66,6 +93,10 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati return EAI_SYSTEM; } while (fgets(line, sizeof line, f) && cnt < MAXADDRS) { +#else + int i = 0; + while (i < FIXED_HOSTS_MAX_LENGTH && get_hosts_str(line, sizeof line, f, &i) && cnt < MAXADDRS) { +#endif char *p, *z; if ((p=strchr(line, '#'))) *p++='\n', *p=0; @@ -98,7 +129,13 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati memcpy(canon, p, z-p+1); } } +#ifdef LINUX_MUSL_SUPPORTED + if (f) { + __fclose_ca(f); + } +#else __fclose_ca(f); +#endif return cnt ? cnt : badfam; } @@ -139,7 +176,40 @@ static int dns_parse_callback(void *c, int rr, const void *data, int len, const return 0; } +#ifdef LINUX_MUSL_SUPPORTED +#if OHOS_DNS_PROXY_BY_NETSYS +static JudgeIpv6 load_ipv6_judger(void) +{ + static JudgeIpv6 ipv6_judger = NULL; + resolve_dns_sym((void **) &ipv6_judger, OHOS_JUDGE_IPV6_FUNC_NAME); + return ipv6_judger; +} +#endif + +static int IsIpv6Enable(int netid) +{ + int ret = 0; +#if OHOS_DNS_PROXY_BY_NETSYS + JudgeIpv6 func = load_ipv6_judger(); + if (!func) { + return -1; + } + + ret = func(netid); + if (ret < 0) { + return -1; + } +#endif + return ret; +} +#endif + +#ifdef LINUX_MUSL_SUPPORTED +static int name_from_dns(struct address buf[static MAXADDRS], + char canon[static 256],const char *name, int family, const struct resolvconf *conf, int netid) +#else static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, const struct resolvconf *conf) +#endif { unsigned char qbuf[2][280], abuf[2][512]; const unsigned char *qp[2] = { qbuf[0], qbuf[1] }; @@ -147,12 +217,35 @@ static int name_from_dns(struct address buf[static MAXADDRS], char canon[static int qlens[2], alens[2]; int i, nq = 0; struct dpc_ctx ctx = { .addrs = buf, .canon = canon }; +#ifdef LINUX_MUSL_SUPPORTED + int queryNum = 2; + static const struct { int af; int rr; } afrr_ipv6_enable[2] = { + { .af = AF_INET, .rr = RR_AAAA }, + { .af = AF_INET6, .rr = RR_A }, + }; + static const struct { int af; int rr; } afrr_ipv4_only[1] = { + { .af = AF_INET6, .rr = RR_A }, + }; + struct {int af; int rr;} *afrr = afrr_ipv6_enable; + + if (!IsIpv6Enable(netid) || (family == AF_INET)) { + if (family == AF_INET6) { + return EAI_SYSTEM; + } + queryNum = 1; + afrr = afrr_ipv4_only; + } else { + queryNum = 2; + afrr = afrr_ipv6_enable; + } + for (i = 0; i < queryNum; i++) { +#else static const struct { int af; int rr; } afrr[2] = { { .af = AF_INET6, .rr = RR_A }, { .af = AF_INET, .rr = RR_AAAA }, }; - for (i=0; i<2; i++) { +#endif if (family != afrr[i].af) { qlens[nq] = __res_mkquery(0, name, 1, afrr[i].rr, 0, 0, 0, qbuf[nq], sizeof *qbuf); @@ -163,7 +256,11 @@ static int name_from_dns(struct address buf[static MAXADDRS], char canon[static } } +#ifdef LINUX_MUSL_SUPPORTED + if (res_msend_rc_ext(netid, nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0) +#else if (__res_msend_rc(nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0) +#endif return EAI_SYSTEM; for (i=0; i=ndots or name ends in * a dot, which is an explicit request for global scope. */ @@ -221,13 +327,21 @@ static int name_from_dns_search(struct address buf[static MAXADDRS], char canon[ if (z-p < 256 - l - 1) { memcpy(canon+l+1, p, z-p); canon[z-p+1+l] = 0; +#ifdef LINUX_MUSL_SUPPORTED + int cnt = name_from_dns(buf, canon, canon, family, &conf, netid); +#else int cnt = name_from_dns(buf, canon, canon, family, &conf); +#endif if (cnt) return cnt; } } canon[l] = 0; +#ifdef LINUX_MUSL_SUPPORTED + return name_from_dns(buf, canon, name, family, &conf, netid); +#else return name_from_dns(buf, canon, name, family, &conf); +#endif } static const struct policy { @@ -304,10 +418,19 @@ static int addrcmp(const void *_a, const void *_b) return b->sortkey - a->sortkey; } +#ifdef LINUX_MUSL_SUPPORTED +int lookup_name_ext(struct address buf[static MAXADDRS], char canon[static 256], const char *name, + int family, int flags, int netid) +#else int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags) +#endif { int cnt = 0, i, j; +#if OHOS_DNS_PROXY_BY_NETSYS + DNS_CONFIG_PRINT("lookup_name_ext \n"); +#endif + *canon = 0; if (name) { /* reject empty name and check len so it fits into temp bufs */ @@ -329,8 +452,18 @@ int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], c cnt = name_from_null(buf, name, family, flags); if (!cnt) cnt = name_from_numeric(buf, name, family); if (!cnt && !(flags & AI_NUMERICHOST)) { +#ifdef LINUX_MUSL_SUPPORTED + cnt = predefined_host_name_from_hosts(buf, canon, name, family); + if (!cnt) { + cnt = name_from_hosts(buf, canon, name, family); + } + if (!cnt) { + cnt = name_from_dns_search(buf, canon, name, family, netid); + } +#else cnt = name_from_hosts(buf, canon, name, family); if (!cnt) cnt = name_from_dns_search(buf, canon, name, family); +#endif } if (cnt<=0) return cnt ? cnt : EAI_NONAME; @@ -434,3 +567,417 @@ int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], c return cnt; } + +#ifdef LINUX_MUSL_SUPPORTED +int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags) +{ + return lookup_name_ext(buf, canon, name, family, flags, 0); +} + +typedef struct _linknode { + struct _linknode *_next; + void *_data; +}linknode; + +typedef struct _linkList { + linknode *_phead; + int _count; +}linkList; + +static linkList *create_linklist(void); +static int destory_linklist(linkList *plist); +static int linklist_size(linkList *plist); +static linknode *get_linknode(linkList *plist, int index); +static int linklist_append_last(linkList *plist, void *pdata); +static int linklist_delete(linkList *plist, int index); +static int linklist_delete_first(linkList *plist); +static int linklist_delete_last(linkList *plist); + +typedef struct { + char* host; + char* ip; +}host_ip_pair; + +static const char GROUP_SEPARATOR[] = "|"; +static const char SEPARATOR[] = ","; + +static linkList *host_ips_list_ = NULL; + +static char *strsep(char **s, const char *ct) +{ + char *sbegin = *s; + char *end; + if (sbegin == NULL) { + return NULL; + } + end = strpbrk(sbegin, ct); + if (end) { + *end++ = '\0'; + } + *s = end; + return sbegin; +} + +int predefined_host_clear_all_hosts(void) +{ + if (host_ips_list_ != NULL) { + linknode *pnode = host_ips_list_->_phead; + while (pnode != NULL) { + free(pnode->_data); + pnode->_data = NULL; + pnode = pnode->_next; + } + + destory_linklist(host_ips_list_); + free(host_ips_list_); + host_ips_list_ = NULL; + return 0; + } + return -1; +} + +int predefined_host_remove_host(const char *host) +{ + int remove_cnt = 0; + if (host_ips_list_ != NULL) { + linknode *pnode = NULL; + host_ip_pair *pinfo = NULL; + int cnt = linklist_size(host_ips_list_); + + for (int i = cnt - 1; i >= 0; i--) { + pnode = get_linknode(host_ips_list_, i); + if (pnode != NULL) { + pinfo = (host_ip_pair*)pnode->_data; + if (strcmp(pinfo->host, host) == 0) { + free(pinfo); + linklist_delete(host_ips_list_, i); + remove_cnt++; + } + } + } + } + + return remove_cnt == 0 ? -1 : 0; +} + +static int _predefined_host_is_contain_host_ip(const char* host, const char* ip, int is_check_ip) +{ + if (host_ips_list_ != NULL) { + linknode *pnode = host_ips_list_->_phead; + + while (pnode != NULL) { + host_ip_pair *pinfo = (host_ip_pair*)pnode->_data; + if (strcmp(pinfo->host, host) == 0) { + if (is_check_ip) { + if (strcmp(pinfo->ip, ip) == 0) { + return 1; + } + } else { + return 1; + } + } + pnode = pnode->_next; + } + } + return 0; +} + +int predefined_host_is_contain_host(const char *host) +{ + return _predefined_host_is_contain_host_ip(host, NULL, 0); +} + +static int _predefined_host_add_record(char* host, char* ip) +{ + int head_len = sizeof(host_ip_pair); + int host_len = strlen(host); + int ip_len = strlen(ip); + int total = host_len + 1 + ip_len + 1 + head_len; + char *pdata = calloc(1, total); + host_ip_pair *pinfo = (host_ip_pair*)pdata; + + if (pdata == NULL) { + return EAI_NONAME; + } + + char* i_host = (char*)(pdata + head_len); + char* i_ip = (char*)(pdata + head_len + host_len + 1); + + memcpy(i_host, host, host_len + 1); + memcpy(i_ip, ip, ip_len + 1); + + pinfo->host = i_host; + pinfo->ip = i_ip; + + linklist_append_last(host_ips_list_, pdata); + return 0; +} + +static int _predefined_host_parse_host_ips(char* params) +{ + char* cmd = NULL; + int ret = 0; + int cnt = 0; + + while ((cmd = strsep(¶ms, GROUP_SEPARATOR)) != NULL) { + char* host = strsep(&cmd, SEPARATOR); + char* ip = NULL; + + while ((ip = strsep(&cmd, SEPARATOR)) != NULL) { + cnt++; + if (!_predefined_host_is_contain_host_ip(host, ip, 1)) { + ret = _predefined_host_add_record(host, ip); + if (ret != 0) { + return ret; + } + } + } + } + + return cnt > 0 ? 0 : -1; +} + +int predefined_host_set_hosts(const char* host_ips) +{ + if (host_ips == NULL) { + return EAI_NONAME; + } + + int len = strlen(host_ips); + if (len == 0) { + return EAI_NONAME; + } + + if (host_ips_list_ == NULL) { + host_ips_list_ = create_linklist(); + if (host_ips_list_ == NULL) { + return EAI_MEMORY; + } + } + + char *host_ips_str = calloc(1, len + 1); + if (host_ips_str == NULL) { + return EAI_MEMORY; + } + + memcpy(host_ips_str, host_ips, len + 1); + + int ret = _predefined_host_parse_host_ips(host_ips_str); + + free(host_ips_str); + + return ret; +} + +int predefined_host_set_host(const char* host, const char* ip) +{ + if (host == NULL || strlen(host) == 0 || ip == NULL || strlen(ip) == 0) { + return -1; + } + + if (host_ips_list_ == NULL) { + host_ips_list_ = create_linklist(); + if (host_ips_list_ == NULL) { + return EAI_NONAME; + } + } + + if (_predefined_host_is_contain_host_ip(host, ip, 1)) { + return 0; + } + + return _predefined_host_add_record(host, ip); +} + +int predefined_host_name_from_hosts( + struct address buf[static MAXADDRS], + char canon[static 256], const char *name, int family) +{ + int size = 256; + int cnt = 0; + if (host_ips_list_ != NULL) { + linknode *pnode = host_ips_list_->_phead; + + while (pnode != NULL && cnt < MAXADDRS) { + host_ip_pair *pinfo = (host_ip_pair*)pnode->_data; + if (strcmp(pinfo->host, name) == 0) { + if (__lookup_ipliteral(buf+cnt, pinfo->ip, family) == 1) { + cnt++; + } + } + pnode = pnode->_next; + } + } + + if (cnt > 0) { + memcpy(canon, name, size); + } + return cnt; +} + +static inline void free_linknodedata(linknode *pnode) +{ + if (NULL != pnode) { + free(pnode); + } +} + +static linknode *create_linknode(void *data) +{ + linknode *pnode = (linknode *)calloc(1, sizeof(linknode)); + if (NULL == pnode) { + return NULL; + } + pnode->_data = data; + pnode->_next = NULL; + + return pnode; +} + +static linkList *create_linklist(void) +{ + linkList *plist = (linkList *)calloc(1, sizeof(linkList)); + if (NULL == plist) { + return NULL; + } + plist->_phead = NULL; + plist->_count = 0; + + return plist; +} + +static int destory_linklist(linkList *plist) +{ + if (NULL == plist) { + return -1; + } + + linknode *pnode = plist->_phead; + linknode *ptmp = NULL; + while (pnode != NULL) { + ptmp = pnode; + pnode = pnode->_next; + free_linknodedata(ptmp); + } + + plist->_phead = NULL; + plist->_count = 0; + + return 0; +} + +static int linklist_size(linkList *plist) +{ + if (NULL == plist) { + return 0; + } + + return plist->_count; +} + +static linknode *get_linknode(linkList *plist, int index) +{ + if (index < 0 || index >= plist->_count) { + return NULL; + } + + int i = 0; + linknode *pnode = plist->_phead; + while ((i++) < index) { + pnode = pnode->_next; + } + + return pnode; +} + +static int linklist_append_last(linkList *plist, void *pdata) +{ + if (NULL == plist) { + return -1; + } + + linknode *pnode = create_linknode(pdata); + if (NULL == pnode) { + return -1; + } + + if (NULL == plist->_phead) { + plist->_phead = pnode; + plist->_count++; + } else { + linknode *plastnode = get_linknode(plist, plist->_count - 1); + plastnode->_next = pnode; + plist->_count++; + } + + return 0; +} + +static int linklist_delete(linkList *plist, int index) +{ + if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { + return -1; + } + + if (index == 0) { + return linklist_delete_first(plist); + } else if (index == (plist->_count - 1)) { + return linklist_delete_last(plist); + } else { + linknode *pindex = get_linknode(plist, index); + if (NULL == pindex) { + return -1; + } + linknode *preindex = get_linknode(plist, index - 1); + if (NULL == preindex) { + return -1; + } + + preindex->_next = pindex->_next; + + free_linknodedata(pindex); + plist->_count--; + } + + return 0; +} + +static int linklist_delete_first(linkList *plist) +{ + if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { + return -1; + } + + linknode *phead = plist->_phead; + plist->_phead = plist->_phead->_next; + + free_linknodedata(phead); + plist->_count--; + + return 0; +} + +static int linklist_delete_last(linkList *plist) +{ + if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { + return -1; + } + + linknode *plastsecondnode = get_linknode(plist, plist->_count - 2); + if (NULL != plastsecondnode) { + linknode *plastnode = plastsecondnode->_next; + plastsecondnode->_next = NULL; + + free_linknodedata(plastnode); + plist->_count--; + } else { + linknode *plastnode = get_linknode(plist, plist->_count - 1); + plist->_phead = NULL; + plist->_count = 0; + + free_linknodedata(plastnode); + } + + return 0; +} +#endif diff --git a/src/network/lookup_serv.c b/src/network/lookup_serv.c index ae3827785..409c00f40 100644 --- a/src/network/lookup_serv.c +++ b/src/network/lookup_serv.c @@ -8,6 +8,9 @@ #include #include "lookup.h" #include "stdio_impl.h" +#ifdef LINUX_MUSL_SUPPORTED +#include "network_conf_function.h" +#endif int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int proto, int socktype, int flags) { @@ -71,6 +74,7 @@ int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int pro unsigned char _buf[1032]; FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf); +#ifndef LINUX_MUSL_SUPPORTED if (!f) switch (errno) { case ENOENT: case ENOTDIR: @@ -81,6 +85,10 @@ int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int pro } while (fgets(line, sizeof line, f) && cnt < MAXSERVS) { +#else + int indexPtr = 0; + while (get_services_str(line, f, &indexPtr)) { +#endif if ((p=strchr(line, '#'))) *p++='\n', *p=0; /* Find service name */ @@ -109,6 +117,12 @@ int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int pro buf[cnt++].proto = IPPROTO_TCP; } } +#ifdef LINUX_MUSL_SUPPORTED + if (f) { + __fclose_ca(f); + } +#else __fclose_ca(f); +#endif return cnt > 0 ? cnt : EAI_SERVICE; } diff --git a/src/network/netname.c b/src/network/netname.c index ba6e66568..08cac94ed 100644 --- a/src/network/netname.c +++ b/src/network/netname.c @@ -1,12 +1,50 @@ #include +#ifdef __LITEOS_A__ +#include +#endif -struct netent *getnetbyaddr(uint32_t net, int type) +struct netent *getnetbyaddr(uint32_t net, int addrtype) { +#ifdef __LITEOS_A__ + struct netent *ne = NULL; + setnetent(1); + while (1) { + ne = getnetent(); + if (!ne) + break; + if (ne->n_net == net && ne->n_addrtype == addrtype) { + setnetent(0); + endnetent(); + return ne; + } + } + setnetent(0); + endnetent(); + return NULL; +#else return 0; +#endif } -struct netent *getnetbyname(const char *name) +struct netent *getnetbyname(const char *netname) { +#ifdef __LITEOS_A__ + struct netent *ne = NULL; + setnetent(1); + while (1) { + ne = getnetent(); + if (!ne) + break; + if (strcmp(ne->n_name, netname) == 0) { + setnetent(0); + endnetent(); + return ne; + } + } + setnetent(0); + endnetent(); + return NULL; +#else return 0; +#endif } - diff --git a/src/network/recvmsg.c b/src/network/recvmsg.c index 03641625e..979c6eabe 100644 --- a/src/network/recvmsg.c +++ b/src/network/recvmsg.c @@ -3,6 +3,9 @@ #include #include #include +#ifdef __LITEOS_A__ +#include +#endif #include "syscall.h" hidden void __convert_scm_timestamps(struct msghdr *, socklen_t); @@ -50,6 +53,12 @@ void __convert_scm_timestamps(struct msghdr *msg, socklen_t csize) ssize_t recvmsg(int fd, struct msghdr *msg, int flags) { ssize_t r; +#ifdef __LITEOS_A__ + if (!msg) { + errno = EFAULT; + return -1; + } +#endif socklen_t orig_controllen = msg->msg_controllen; #if LONG_MAX > INT_MAX struct msghdr h, *orig = msg; diff --git a/src/network/res_cache.c b/src/network/res_cache.c index 96838e5a5..2774947fb 100644 --- a/src/network/res_cache.c +++ b/src/network/res_cache.c @@ -18,8 +18,8 @@ #include #include #include -#include "lookup.h" #include +#include "lookup.h" #if OHOS_DNS_PROXY_BY_NETSYS @@ -39,9 +39,25 @@ static SetCache load_cache_setter(void) return cache_setter; } +#ifdef LINUX_MUSL_SUPPORTED +#define GETADDRINFO_PRINT_DEBUG(...) + +static PostDnsResult load_result_poster(void) +{ + static PostDnsResult result_poster = NULL; + resolve_dns_sym((void **) &result_poster, OHOS_POST_DNS_RESULT_FUNC_NAME); + return result_poster; +} + +void +dns_set_addr_info_to_netsys_cache2(const int netid, const char *restrict host, const char *restrict serv, + const struct addrinfo *restrict hint, struct addrinfo *res) +{ +#else void dns_set_addr_info_to_netsys_cache(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo *res) { +#endif SetCache func = load_cache_setter(); if (!func) { DNS_CONFIG_PRINT("%s: loading %s failed", __func__, OHOS_SET_CACHE_FUNC_NAME); @@ -49,17 +65,42 @@ hint, struct addrinfo *res) { } struct param_wrapper param = {(char *) host, (char *) serv, (struct addrinfo *) hint}; + +#ifdef LINUX_MUSL_SUPPORTED + int ret = func(netid, param, res); +#else int ret = func(0, param, res); +#endif if (ret < 0) { +#ifdef LINUX_MUSL_SUPPORTED + GETADDRINFO_PRINT_DEBUG("dns_set_addr_info_to_netsys_cache OHOS_SET_CACHE_FUNC_NAME err %d\n", ret); +#else DNS_CONFIG_PRINT("dns_set_addr_info_to_netsys_cache OHOS_SET_CACHE_FUNC_NAME err %d\n", ret); +#endif return; } +#ifdef LINUX_MUSL_SUPPORTED + GETADDRINFO_PRINT_DEBUG("set to netsys cache OK\n"); +#else DNS_CONFIG_PRINT("set to netsys cache OK\n"); +#endif } +#ifdef LINUX_MUSL_SUPPORTED +void dns_set_addr_info_to_netsys_cache(const char *restrict host, const char *restrict serv, + const struct addrinfo *restrict hint, struct addrinfo *res) +{ + dns_set_addr_info_to_netsys_cache2(0, host, serv, hint, res); +} + +int dns_get_addr_info_from_netsys_cache2(const int netid, const char *restrict host, const char *restrict serv, + const struct addrinfo *restrict hint, struct addrinfo **restrict res) +{ +#else int dns_get_addr_info_from_netsys_cache(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res) { +#endif GetCache func = load_cache_getter(); if (!func) { DNS_CONFIG_PRINT("%s: loading %s failed", __func__, OHOS_GET_CACHE_FUNC_NAME); @@ -69,15 +110,28 @@ int dns_get_addr_info_from_netsys_cache(const char *restrict host, const char *r struct addr_info_wrapper addr_info[MAX_RESULTS] = {0}; uint32_t num = 0; struct param_wrapper param = {(char *) host, (char *) serv, (struct addrinfo *) hint}; + +#ifdef LINUX_MUSL_SUPPORTED + int ret = func(netid, param, addr_info, &num); +#else int ret = func(0, param, addr_info, &num); +#endif if (ret < 0) { +#ifdef LINUX_MUSL_SUPPORTED + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache OHOS_GET_CACHE_FUNC_NAME err %d\n", ret); +#else DNS_CONFIG_PRINT("dns_get_addr_info_from_netsys_cache OHOS_GET_CACHE_FUNC_NAME err %d\n", ret); +#endif return -1; } num = MACRO_MIN(num, MACRO_MIN(MAX_RESULTS, MAXADDRS)); if (num == 0) { +#ifdef LINUX_MUSL_SUPPORTED + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache num is invalid err %u", num); +#else DNS_CONFIG_PRINT("dns_get_addr_info_from_netsys_cache num is invalid err %u", num); +#endif return -1; } @@ -112,8 +166,65 @@ int dns_get_addr_info_from_netsys_cache(const char *restrict host, const char *r out[0].ref = (short) num; *res = &out->ai; +#ifdef LINUX_MUSL_SUPPORTED + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache end\n"); +#else DNS_CONFIG_PRINT("dns_get_addr_info_from_netsys_cache end\n"); +#endif + return 0; +} + +#ifdef LINUX_MUSL_SUPPORTED +int dns_get_addr_info_from_netsys_cache(const char *restrict host, const char *restrict serv, + const struct addrinfo *restrict hint, struct addrinfo **restrict res) +{ + return dns_get_addr_info_from_netsys_cache2(0, host, serv, hint, res); +} + +int dns_post_result_to_netsys_cache(int netid, char* name, int usedtime, int queryret, + struct addrinfo *res, struct queryparam *param) +{ + PostDnsResult func = load_result_poster(); + if (!func) { + GETADDRINFO_PRINT_DEBUG("%s: loading %s failed", __func__, OHOS_POST_DNS_RESULT_FUNC_NAME); + return -1; + } + + int ret = func(netid, name, usedtime, queryret, res, param); + if (ret < 0) { + GETADDRINFO_PRINT_DEBUG("dns_set_result_to_netsys_cache OHOS_POST_DNS_RESULT_FUNC_NAME err %d\n", ret); + return -1; + } + + GETADDRINFO_PRINT_DEBUG("dns_post_result_to_netsys_cache OK\n"); return 0; } +int dns_get_default_network(int *currentnetid) +{ + void *handle = dlopen(DNS_SO_PATH, RTLD_LAZY); + if (handle == NULL) { + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlopen err %s\n", dlerror()); + return -1; + } + + GetDefaultNet func = dlsym(handle, OHOS_GET_DEFAULT_NET_FUNC_NAME); + if (func == NULL) { + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlsym err %s\n", dlerror()); + dlclose(handle); + return -1; + } + + int ret = func(0, currentnetid); + dlclose(handle); + + if (ret < 0) { + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache OHOS_GET_DEFAULT_NET_FUNC_NAME err %d\n", ret); + return -1; + } + + GETADDRINFO_PRINT_DEBUG("dns_post_result_to_netsys_cache OK %d\n", currentnetid); + return 0; +} +#endif #endif diff --git a/src/network/res_msend.c b/src/network/res_msend.c index 3e018009e..c93787755 100644 --- a/src/network/res_msend.c +++ b/src/network/res_msend.c @@ -30,6 +30,13 @@ static unsigned long mtime() int __res_msend_rc(int nqueries, const unsigned char *const *queries, const int *qlens, unsigned char *const *answers, int *alens, int asize, const struct resolvconf *conf) +{ + return res_msend_rc_ext(0, nqueries, queries, qlens, answers, alens, asize, conf); +} + +int res_msend_rc_ext(int netid, int nqueries, const unsigned char *const *queries, + const int *qlens, unsigned char *const *answers, int *alens, int asize, + const struct resolvconf *conf) { int fd; int timeout, attempts, retry_interval, servfail_retry; @@ -89,6 +96,15 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries, pthread_cleanup_push(cleanup, (void *)(intptr_t)fd); pthread_setcancelstate(cs, 0); +#ifdef LINUX_MUSL_SUPPORTED + /** + * Todo FwmarkClient::BindSocket + */ + if (netid > 0) { + res_bind_socket(fd, netid); + } +#endif + /* Convert any IPv4 addresses in a mixed environment to v4-mapped */ if (family == AF_INET6) { setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0); diff --git a/src/network/resolvconf.c b/src/network/resolvconf.c index 73de25fa5..6803065a5 100644 --- a/src/network/resolvconf.c +++ b/src/network/resolvconf.c @@ -5,11 +5,114 @@ #include #include #include + +#ifdef LINUX_MUSL_SUPPORTED +#include "hilog_adapter.h" + +#if 1 +#define SIGCHAIN_LOG_DOMAIN 0xD003F00 +#define SIGCHAIN_LOG_TAG "ressolvconf" +#define GETADDRINFO_PRINT_DEBUG(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \ + SIGCHAIN_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__)) +#else +#define GETADDRINFO_PRINT_DEBUG(...) +#endif + +#define DNS_RESOLV_CONF_PATH "/etc/resolv.conf" + +#if OHOS_DNS_PROXY_BY_NETSYS | OHOS_FWMARK_CLIENT_BY_NETSYS +#include "atomic.h" + +#include + +static void *open_dns_lib(void) +{ + static void *dns_lib_handle = NULL; + if (dns_lib_handle != NULL) { + a_barrier(); + return dns_lib_handle; + } + + void *lib = dlopen(DNS_SO_PATH, RTLD_LAZY); + if (lib == NULL) { + DNS_CONFIG_PRINT("%s: dlopen %s failed: %s", + __func__, DNS_SO_PATH, dlerror()); + return NULL; + } + + void *old_lib = a_cas_p(&dns_lib_handle, NULL, lib); + if (old_lib == NULL) { + DNS_CONFIG_PRINT("%s: %s loaded", __func__, DNS_SO_PATH); + return lib; + } else { + /* Another thread has already loaded the library, + * dlclose is invoked to make refcount correct */ + DNS_CONFIG_PRINT("%s: %s has been loaded by another thread", + __func__, DNS_SO_PATH); + if (dlclose(lib)) { + DNS_CONFIG_PRINT("%s: dlclose %s failed: %s", + __func__, DNS_SO_PATH, dlerror()); + } + return old_lib; + } +} + +static void *load_from_dns_lib(const char *symbol) +{ + void *lib_handle = open_dns_lib(); + if (lib_handle == NULL) { + return NULL; + } + + void *sym_addr = dlsym(lib_handle, symbol); + if (sym_addr == NULL) { + DNS_CONFIG_PRINT("%s: loading symbol %s with dlsym failed: %s", + __func__, symbol, dlerror()); + } + return sym_addr; +} + +void resolve_dns_sym(void **holder, const char *symbol) +{ + if (*holder != NULL) { + a_barrier(); + return; + } + + void *ptr = load_from_dns_lib(symbol); + if (ptr == NULL) { + return; + } + + void *old_ptr = a_cas_p(holder, NULL, ptr); + if (old_ptr != NULL) { + DNS_CONFIG_PRINT("%s: %s has been found by another thread", + __func__, symbol); + } else { + DNS_CONFIG_PRINT("%s: %s found", __func__, symbol); + } +} + +static GetConfig load_config_getter(void) +{ + static GetConfig config_getter = NULL; + resolve_dns_sym((void **) &config_getter, OHOS_GET_CONFIG_FUNC_NAME); + return config_getter; +} + +#endif +#else // LINUX_MUSL_SUPPORTED #if OHOS_DNS_PROXY_BY_NETSYS #include #endif +#endif // LINUX_MUSL_SUPPORTED int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz) +{ + return get_resolv_conf_ext(conf, search, search_sz, 0); +} + +int get_resolv_conf_ext(struct resolvconf *conf, char *search, size_t search_sz, int netid) { char line[256]; unsigned char _buf[256]; @@ -22,6 +125,21 @@ int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz) if (search) *search = 0; #if OHOS_DNS_PROXY_BY_NETSYS +#ifdef LINUX_MUSL_SUPPORTED + GetConfig func = load_config_getter(); + if (!func) { + DNS_CONFIG_PRINT("%s: loading %s failed, use %s as a fallback", + __func__, OHOS_GET_CONFIG_FUNC_NAME, DNS_RESOLV_CONF_PATH); + goto etc_resolv_conf; + } + + struct resolv_config config = {0}; + int ret = func(netid, &config); + if (ret < 0) { + DNS_CONFIG_PRINT("__get_resolv_conf OHOS_GET_CONFIG_FUNC_NAME err %d\n", ret); + return EAI_NONAME; + } +#else void *handle = dlopen(DNS_SO_PATH, RTLD_LAZY); if (handle == NULL) { DNS_CONFIG_PRINT("__get_resolv_conf dlopen err %s\n", dlerror()); @@ -42,10 +160,10 @@ int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz) DNS_CONFIG_PRINT("__get_resolv_conf OHOS_GET_CONFIG_FUNC_NAME err %d\n", ret); goto etc_resolv_conf; } - int32_t timeout_second = config.timeout_ms / 1000; #endif -#if OHOS_DNS_PROXY_BY_NETSYS + int32_t timeout_second = config.timeout_ms / 1000; + netsys_conf: if (timeout_second > 0) { if (timeout_second >= 60) { @@ -69,6 +187,9 @@ netsys_conf: nns++; } } + if (nns != 0) { + goto get_conf_ok; + } etc_resolv_conf: #endif @@ -142,7 +263,38 @@ no_resolv_conf: nns = 1; } +get_conf_ok: conf->nns = nns; return 0; } + +#ifdef LINUX_MUSL_SUPPORTED +int res_bind_socket(int fd, int netid) +{ + int ret = -1; + void* libhandler; + + GETADDRINFO_PRINT_DEBUG("res_bind_socket netid:%{public}d \n", netid); + +#ifdef OHOS_FWMARK_CLIENT_BY_NETSYS + libhandler = dlopen(FWMARKCLIENT_SO_PATH, RTLD_LAZY); + if (libhandler == NULL) { + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlopen err %s\n", dlerror()); + return -1; + } + + BindSocket_Ext func = dlsym(libhandler, OHOS_BIND_SOCKET_FUNC_NAME); + if (func == NULL) { + GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlsym err %s\n", dlerror()); + dlclose(libhandler); + return -1; + } else { + ret = func(fd, netid); + GETADDRINFO_PRINT_DEBUG("res_bind_socket ret %{public}d \n", ret); + dlclose(libhandler); + } +#endif + return ret; +} +#endif diff --git a/src/network/serv.c b/src/network/serv.c index 41424e806..f2ed83d2e 100644 --- a/src/network/serv.c +++ b/src/network/serv.c @@ -1,14 +1,124 @@ +#ifdef __LITEOS_A__ +#define _BSD_SOURCE +#include +#include +#include +#include +#include +#endif #include +#include -void endservent(void) +#ifdef __LITEOS_A__ +#define SERV_FILE_PATH "/etc/services" +#define BUFFER_SIZE 200 +#define MAX_PORT 65535 +#define MAX_ALIAS_NO 10 + +struct servdata { -} + FILE *servf; + int stayopen; + struct servent se; + char buf[BUFFER_SIZE]; + char *alias_list[MAX_ALIAS_NO + 1]; +}; +static struct servdata *sd = NULL; -void setservent(int stayopen) +static int servent_resolv() { + if (sd->buf[0] == '#') + return -1; + char *ch, *temp, *ptr = NULL; + ch = strchr(sd->buf, '#'); + if (ch) + *ch = '\0'; + ch = strtok_r(sd->buf, " \t\n", &ptr); + temp = strtok_r(NULL, " \t\n", &ptr); + if (!ch || !temp) + return -1; + + sd->se.s_name = ch; + ch = strchr(temp, '/'); + if (ch == NULL) + return -1; + *ch++ = '\0'; + sd->se.s_port = atoi(temp); + if (sd->se.s_port < 1 || sd->se.s_port > MAX_PORT) + return -1; + sd->se.s_port = htons(sd->se.s_port); + if (strcmp(ch, "tcp") != 0 && strcmp(ch, "udp") != 0) + return -1; + sd->se.s_proto = ch; + + int i = 0; + while (i < MAX_ALIAS_NO) { + ch = strtok_r(NULL, " \t\n", &ptr); + sd->alias_list[i++] = ch; + } + sd->alias_list[MAX_ALIAS_NO] = NULL; + sd->se.s_aliases = sd->alias_list; + return 0; } +#endif struct servent *getservent(void) { +#ifdef __LITEOS_A__ + if (sd == NULL) { + if ((sd = malloc(sizeof(struct servdata))) == NULL) { + errno = ENOMEM; + return NULL; + } + sd->servf = NULL; + sd->stayopen = 0; + } + if (!sd->servf && !(sd->servf = fopen(SERV_FILE_PATH, "r"))) { + errno = ENOENT; + free(sd); + sd = NULL; + return NULL; + } + do { + if (!fgets(sd->buf, BUFFER_SIZE - 1, sd->servf)) { + errno = EINVAL; + return NULL; + } + } while (servent_resolv()); + return &(sd->se); +#else return 0; +#endif +} + +void endservent(void) +{ +#ifdef __LITEOS_A__ + if (sd == NULL) + return; + if (sd->stayopen == 0) { + if (sd->servf != NULL) + fclose(sd->servf); + free(sd); + sd = NULL; + } +#endif +} + +void setservent(int stayopen) +{ +#ifdef __LITEOS_A__ + if (sd == NULL) { + if ((sd = malloc(sizeof(struct servdata))) == NULL) { + errno = ENOMEM; + return; + } + sd->servf = NULL; + } + if (!sd->servf) + sd->servf = fopen(SERV_FILE_PATH, "r"); + else + rewind(sd->servf); + sd->stayopen = stayopen; +#endif } diff --git a/src/network/sockatmark.c b/src/network/sockatmark.c index f474551aa..c56c7d79a 100644 --- a/src/network/sockatmark.c +++ b/src/network/sockatmark.c @@ -1,9 +1,11 @@ #include #include +#include int sockatmark(int s) { int ret; + UNSUPPORTED_API_VOID(LITEOS_A); if (ioctl(s, SIOCATMARK, &ret) < 0) return -1; return ret; diff --git a/src/network/socket.c b/src/network/socket.c index b857dbe77..cba949a32 100644 --- a/src/network/socket.c +++ b/src/network/socket.c @@ -37,7 +37,11 @@ uint8_t is_allow_internet(void) } #endif +#ifdef OHOS_SOCKET_HOOK_ENABLE +int __libc_socket(int domain, int type, int protocol) +#else int socket(int domain, int type, int protocol) +#endif { #if OHOS_PERMISSION_INTERNET if ((domain == AF_INET || domain == AF_INET6) && is_allow_internet() == 0) { diff --git a/src/passwd/getspnam_r.c b/src/passwd/getspnam_r.c index 541e85314..919fce367 100644 --- a/src/passwd/getspnam_r.c +++ b/src/passwd/getspnam_r.c @@ -15,8 +15,16 @@ static long xatol(char **s) { long x; + int sign; if (**s == ':' || **s == '\n') return -1; + sign = (int)(unsigned char)**s; + if (sign == '-' || sign == '+') { + ++*s; + } for (x=0; **s-'0'<10U; ++*s) x=10*x+(**s-'0'); + if (sign == '-') { + return -x; + } return x; } diff --git a/src/process/_Fork.c b/src/process/_Fork.c index da0638681..2146e5d82 100644 --- a/src/process/_Fork.c +++ b/src/process/_Fork.c @@ -5,6 +5,9 @@ #include "lock.h" #include "pthread_impl.h" #include "aio_impl.h" +#ifdef LINUX_MUSL_SUPPORTED +#include "proc_xid_impl.h" +#endif static void dummy(int x) { } weak_alias(dummy, __aio_atfork); @@ -24,12 +27,25 @@ pid_t _Fork(void) if (!ret) { pthread_t self = __pthread_self(); self->tid = __syscall(SYS_gettid); +#ifdef __LITEOS_A__ + self->pid = __syscall(SYS_getpid); +#else + self->pid = self->tid; +#endif + self->proc_tid = -1; self->robust_list.off = 0; self->robust_list.pending = 0; self->next = self->prev = self; __thread_list_lock = 0; libc.threads_minus_1 = 0; +#ifdef LINUX_MUSL_SUPPORTED + __clear_proc_pid(); +#endif if (libc.need_locks) libc.need_locks = -1; +#ifdef __LITEOS_A__ + libc.exit = 0; + signal(SIGSYS, arm_do_signal); +#endif } UNLOCK(__abort_lock); __aio_atfork(!ret); diff --git a/porting/linux/user/src/process/arm/__vfork.s b/src/process/arm/__vfork.s similarity index 100% rename from porting/linux/user/src/process/arm/__vfork.s rename to src/process/arm/__vfork.s diff --git a/src/process/fexecve.c b/src/process/fexecve.c index 554c1981b..4baa21c3c 100644 --- a/src/process/fexecve.c +++ b/src/process/fexecve.c @@ -3,9 +3,11 @@ #include #include #include "syscall.h" +#include int fexecve(int fd, char *const argv[], char *const envp[]) { + UNSUPPORTED_API_VOID(LITEOS_A); int r = __syscall(SYS_execveat, fd, "", argv, envp, AT_EMPTY_PATH); if (r != -ENOSYS) return __syscall_ret(r); char buf[15 + 3*sizeof(int)]; diff --git a/src/process/posix_spawn.c b/src/process/posix_spawn.c index 728551b36..687470af3 100644 --- a/src/process/posix_spawn.c +++ b/src/process/posix_spawn.c @@ -9,6 +9,7 @@ #include "lock.h" #include "pthread_impl.h" #include "fdop.h" +#include struct args { int p[2]; @@ -170,7 +171,8 @@ int posix_spawn(pid_t *restrict res, const char *restrict path, char stack[1024+PATH_MAX]; int ec=0, cs; struct args args; - + + UNSUPPORTED_API_VOID(LITEOS_A); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); args.path = path; diff --git a/src/process/posix_spawn_file_actions_addclose.c b/src/process/posix_spawn_file_actions_addclose.c index 0c2ef8fa3..d7f6cd423 100644 --- a/src/process/posix_spawn_file_actions_addclose.c +++ b/src/process/posix_spawn_file_actions_addclose.c @@ -2,10 +2,12 @@ #include #include #include "fdop.h" +#include int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa, int fd) { if (fd < 0) return EBADF; + UNSUPPORTED_API_VOID(LITEOS_A); struct fdop *op = malloc(sizeof *op); if (!op) return ENOMEM; op->cmd = FDOP_CLOSE; diff --git a/src/process/posix_spawn_file_actions_adddup2.c b/src/process/posix_spawn_file_actions_adddup2.c index addca4d4f..800361be2 100644 --- a/src/process/posix_spawn_file_actions_adddup2.c +++ b/src/process/posix_spawn_file_actions_adddup2.c @@ -2,10 +2,12 @@ #include #include #include "fdop.h" +#include int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa, int srcfd, int fd) { if (srcfd < 0 || fd < 0) return EBADF; + UNSUPPORTED_API_VOID(LITEOS_A); struct fdop *op = malloc(sizeof *op); if (!op) return ENOMEM; op->cmd = FDOP_DUP2; diff --git a/src/process/posix_spawn_file_actions_addopen.c b/src/process/posix_spawn_file_actions_addopen.c index 82bbcec9e..34b439c18 100644 --- a/src/process/posix_spawn_file_actions_addopen.c +++ b/src/process/posix_spawn_file_actions_addopen.c @@ -3,10 +3,12 @@ #include #include #include "fdop.h" +#include int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *restrict fa, int fd, const char *restrict path, int flags, mode_t mode) { if (fd < 0) return EBADF; + UNSUPPORTED_API_VOID(LITEOS_A); struct fdop *op = malloc(sizeof *op + strlen(path) + 1); if (!op) return ENOMEM; op->cmd = FDOP_OPEN; diff --git a/src/process/posix_spawnattr_destroy.c b/src/process/posix_spawnattr_destroy.c index fc714a1b2..dc628041d 100644 --- a/src/process/posix_spawnattr_destroy.c +++ b/src/process/posix_spawnattr_destroy.c @@ -1,6 +1,8 @@ #include +#include int posix_spawnattr_destroy(posix_spawnattr_t *attr) { + UNSUPPORTED_API_VOID(LITEOS_A); return 0; } diff --git a/src/process/posix_spawnattr_getflags.c b/src/process/posix_spawnattr_getflags.c index aa635ddaf..c8c69b800 100644 --- a/src/process/posix_spawnattr_getflags.c +++ b/src/process/posix_spawnattr_getflags.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_getflags(const posix_spawnattr_t *restrict attr, short *restrict flags) { + UNSUPPORTED_API_VOID(LITEOS_A); *flags = attr->__flags; return 0; } diff --git a/src/process/posix_spawnattr_getpgroup.c b/src/process/posix_spawnattr_getpgroup.c index 0480527d6..1a5cca465 100644 --- a/src/process/posix_spawnattr_getpgroup.c +++ b/src/process/posix_spawnattr_getpgroup.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_getpgroup(const posix_spawnattr_t *restrict attr, pid_t *restrict pgrp) { + UNSUPPORTED_API_VOID(LITEOS_A); *pgrp = attr->__pgrp; return 0; } diff --git a/src/process/posix_spawnattr_getsigdefault.c b/src/process/posix_spawnattr_getsigdefault.c index a49050aa4..b3054020c 100644 --- a/src/process/posix_spawnattr_getsigdefault.c +++ b/src/process/posix_spawnattr_getsigdefault.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_getsigdefault(const posix_spawnattr_t *restrict attr, sigset_t *restrict def) { + UNSUPPORTED_API_VOID(LITEOS_A); *def = attr->__def; return 0; } diff --git a/src/process/posix_spawnattr_getsigmask.c b/src/process/posix_spawnattr_getsigmask.c index f60ad7f37..832fd2ce1 100644 --- a/src/process/posix_spawnattr_getsigmask.c +++ b/src/process/posix_spawnattr_getsigmask.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_getsigmask(const posix_spawnattr_t *restrict attr, sigset_t *restrict mask) { + UNSUPPORTED_API_VOID(LITEOS_A); *mask = attr->__mask; return 0; } diff --git a/src/process/posix_spawnattr_init.c b/src/process/posix_spawnattr_init.c index 0dcd868f3..4bec212a6 100644 --- a/src/process/posix_spawnattr_init.c +++ b/src/process/posix_spawnattr_init.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_init(posix_spawnattr_t *attr) { + UNSUPPORTED_API_VOID(LITEOS_A); *attr = (posix_spawnattr_t){ 0 }; return 0; } diff --git a/src/process/posix_spawnattr_sched.c b/src/process/posix_spawnattr_sched.c index 3143635ba..571a404ac 100644 --- a/src/process/posix_spawnattr_sched.c +++ b/src/process/posix_spawnattr_sched.c @@ -1,25 +1,30 @@ #include #include #include +#include int posix_spawnattr_getschedparam(const posix_spawnattr_t *restrict attr, struct sched_param *restrict schedparam) { + UNSUPPORTED_API_VOID(LITEOS_A); return ENOSYS; } int posix_spawnattr_setschedparam(posix_spawnattr_t *restrict attr, const struct sched_param *restrict schedparam) { + UNSUPPORTED_API_VOID(LITEOS_A); return ENOSYS; } int posix_spawnattr_getschedpolicy(const posix_spawnattr_t *restrict attr, int *restrict policy) { + UNSUPPORTED_API_VOID(LITEOS_A); return ENOSYS; } int posix_spawnattr_setschedpolicy(posix_spawnattr_t *attr, int policy) { + UNSUPPORTED_API_VOID(LITEOS_A); return ENOSYS; } diff --git a/src/process/posix_spawnattr_setflags.c b/src/process/posix_spawnattr_setflags.c index 687809921..e5c35e89e 100644 --- a/src/process/posix_spawnattr_setflags.c +++ b/src/process/posix_spawnattr_setflags.c @@ -1,8 +1,10 @@ #include #include +#include int posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags) { + UNSUPPORTED_API_VOID(LITEOS_A); const unsigned all_flags = POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | diff --git a/src/process/posix_spawnattr_setpgroup.c b/src/process/posix_spawnattr_setpgroup.c index f39596a6e..4aa090d24 100644 --- a/src/process/posix_spawnattr_setpgroup.c +++ b/src/process/posix_spawnattr_setpgroup.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_setpgroup(posix_spawnattr_t *attr, pid_t pgrp) { + UNSUPPORTED_API_VOID(LITEOS_A); attr->__pgrp = pgrp; return 0; } diff --git a/src/process/posix_spawnattr_setsigdefault.c b/src/process/posix_spawnattr_setsigdefault.c index 568697267..b07c3a7a9 100644 --- a/src/process/posix_spawnattr_setsigdefault.c +++ b/src/process/posix_spawnattr_setsigdefault.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_setsigdefault(posix_spawnattr_t *restrict attr, const sigset_t *restrict def) { + UNSUPPORTED_API_VOID(LITEOS_A); attr->__def = *def; return 0; } diff --git a/src/process/posix_spawnattr_setsigmask.c b/src/process/posix_spawnattr_setsigmask.c index f2532f8e0..092fa2a8b 100644 --- a/src/process/posix_spawnattr_setsigmask.c +++ b/src/process/posix_spawnattr_setsigmask.c @@ -1,7 +1,9 @@ #include +#include int posix_spawnattr_setsigmask(posix_spawnattr_t *restrict attr, const sigset_t *restrict mask) { + UNSUPPORTED_API_VOID(LITEOS_A); attr->__mask = *mask; return 0; } diff --git a/src/process/posix_spawnp.c b/src/process/posix_spawnp.c index aad6133b9..5831bcaee 100644 --- a/src/process/posix_spawnp.c +++ b/src/process/posix_spawnp.c @@ -1,5 +1,6 @@ #include #include +#include int posix_spawnp(pid_t *restrict res, const char *restrict file, const posix_spawn_file_actions_t *fa, @@ -7,6 +8,7 @@ int posix_spawnp(pid_t *restrict res, const char *restrict file, char *const argv[restrict], char *const envp[restrict]) { posix_spawnattr_t spawnp_attr = { 0 }; + UNSUPPORTED_API_VOID(LITEOS_A); if (attr) spawnp_attr = *attr; spawnp_attr.__fn = (void *)__execvpe; return posix_spawn(res, file, fa, &spawnp_attr, argv, envp); diff --git a/src/process/system.c b/src/process/system.c index 5af59b809..1493b06a3 100644 --- a/src/process/system.c +++ b/src/process/system.c @@ -5,6 +5,7 @@ #include #include #include "pthread_impl.h" +#include extern char **__environ; @@ -16,6 +17,7 @@ int system(const char *cmd) int status = -1, ret; posix_spawnattr_t attr; + UNSUPPORTED_API_VOID(LITEOS_A); pthread_testcancel(); if (!cmd) return 1; diff --git a/src/process/vfork.c b/src/process/vfork.c index d430c13fc..00d1c1310 100644 --- a/src/process/vfork.c +++ b/src/process/vfork.c @@ -2,8 +2,12 @@ #include #include #include "syscall.h" +#include "pthread_impl.h" +#ifdef LINUX_MUSL_SUPPORTED +#include "proc_xid_impl.h" +#endif -pid_t vfork(void) +hidden pid_t __vfork(void) { /* vfork syscall cannot be made from C code */ #ifdef SYS_fork @@ -12,3 +16,20 @@ pid_t vfork(void) return syscall(SYS_clone, SIGCHLD, 0); #endif } + +pid_t vfork(void) +{ + pthread_t self = __pthread_self(); + pid_t parent_pid = self->pid; + self->pid = 0; + pid_t ret = __vfork(); + if (ret != 0) { + self->pid = parent_pid; + } else { + self->proc_tid = -1; +#ifdef LINUX_MUSL_SUPPORTED + __clear_proc_pid(); +#endif + } + return ret; +} diff --git a/porting/linux/user/src/process/x86_64/__vfork.s b/src/process/x86_64/__vfork.s similarity index 100% rename from porting/linux/user/src/process/x86_64/__vfork.s rename to src/process/x86_64/__vfork.s diff --git a/src/sched/affinity.c b/src/sched/affinity.c index 948ece413..d4b1e5218 100644 --- a/src/sched/affinity.c +++ b/src/sched/affinity.c @@ -6,14 +6,44 @@ int sched_setaffinity(pid_t tid, size_t size, const cpu_set_t *set) { +#ifdef __LITEOS_A__ + if (size != sizeof(cpu_set_t)) { + errno = EINVAL; + return -1; + } + return syscall(SYS_sched_setaffinity, tid, (unsigned short)(set->__bits[0]), MUSL_TYPE_PROCESS); +#else return syscall(SYS_sched_setaffinity, tid, size, set); +#endif } int pthread_setaffinity_np(pthread_t td, size_t size, const cpu_set_t *set) { +#ifdef __LITEOS_A__ + if (size != sizeof(cpu_set_t)) { + return EINVAL; + } + return -__syscall(SYS_sched_setaffinity, td->tid, (unsigned short)(set->__bits[0]), MUSL_TYPE_THREAD); +#else return -__syscall(SYS_sched_setaffinity, td->tid, size, set); +#endif } +#ifdef __LITEOS_A__ +static int do_getaffinity(pid_t tid, size_t size, cpu_set_t *set, int flag) +{ + unsigned int cpuset; + if (size != sizeof(cpu_set_t)) { + return -EINVAL; + } + int ret = __syscall(SYS_sched_getaffinity, tid, &cpuset, flag); + if (ret < 0) { + return ret; + } + set->__bits[0] = (long)cpuset; + return 0; +} +#else static int do_getaffinity(pid_t tid, size_t size, cpu_set_t *set) { long ret = __syscall(SYS_sched_getaffinity, tid, size, set); @@ -21,13 +51,22 @@ static int do_getaffinity(pid_t tid, size_t size, cpu_set_t *set) if (ret < size) memset((char *)set+ret, 0, size-ret); return 0; } +#endif int sched_getaffinity(pid_t tid, size_t size, cpu_set_t *set) { +#ifdef __LITEOS_A__ + return __syscall_ret(do_getaffinity(tid, size, set, MUSL_TYPE_PROCESS)); +#else return __syscall_ret(do_getaffinity(tid, size, set)); +#endif } int pthread_getaffinity_np(pthread_t td, size_t size, cpu_set_t *set) { +#ifdef __LITEOS_A__ + return -do_getaffinity(td->tid, size, set, MUSL_TYPE_THREAD); +#else return -do_getaffinity(td->tid, size, set); +#endif } diff --git a/porting/linux/user/src/sched/sched_cpualloc.c b/src/sched/sched_cpualloc.c similarity index 96% rename from porting/linux/user/src/sched/sched_cpualloc.c rename to src/sched/sched_cpualloc.c index e21827caf..f2c6b4944 100644 --- a/porting/linux/user/src/sched/sched_cpualloc.c +++ b/src/sched/sched_cpualloc.c @@ -1,11 +1,11 @@ -#define _GNU_SOURCE 1 -#include -#include - -cpu_set_t* __sched_cpualloc(size_t count) -{ - // The static analyzer complains that CPU_ALLOC_SIZE eventually expands to - // N * sizeof(unsigned long), which is incompatible with cpu_set_t. This is - // on purpose. - return (cpu_set_t*) malloc(CPU_ALLOC_SIZE(count)); // NOLINT -} +#define _GNU_SOURCE 1 +#include +#include + +cpu_set_t* __sched_cpualloc(size_t count) +{ + // The static analyzer complains that CPU_ALLOC_SIZE eventually expands to + // N * sizeof(unsigned long), which is incompatible with cpu_set_t. This is + // on purpose. + return (cpu_set_t*) malloc(CPU_ALLOC_SIZE(count)); // NOLINT +} diff --git a/src/sched/sched_getcpu.c b/src/sched/sched_getcpu.c index 4ec5eaf67..ac7da05e3 100644 --- a/src/sched/sched_getcpu.c +++ b/src/sched/sched_getcpu.c @@ -12,7 +12,12 @@ typedef long (*getcpu_f)(unsigned *, unsigned *, void *); static long getcpu_init(unsigned *cpu, unsigned *node, void *unused) { +#ifdef LINUX_MUSL_SUPPORTED + __get_vdso_info(); + void *p = __get_vdso_addr(VDSO_GETCPU_VER, VDSO_GETCPU_SYM); +#else void *p = __vdsosym(VDSO_GETCPU_VER, VDSO_GETCPU_SYM); +#endif getcpu_f f = (getcpu_f)p; a_cas_p(&vdso_func, (void *)getcpu_init, p); return f ? f(cpu, node, unused) : -ENOSYS; diff --git a/src/sched/sched_getparam.c b/src/sched/sched_getparam.c index 76f10e49d..22ce5a21b 100644 --- a/src/sched/sched_getparam.c +++ b/src/sched/sched_getparam.c @@ -1,8 +1,27 @@ #include #include #include "syscall.h" +#include +#ifdef __LITEOS_A__ +#include "pthread_impl.h" +#endif int sched_getparam(pid_t pid, struct sched_param *param) { - return __syscall_ret(-ENOSYS); + int r; + if (!param) { + r = -EINVAL; + goto exit; + } + memset(param, 0, sizeof(struct sched_param)); +#ifdef __LITEOS_A__ + r = __syscall(SYS_sched_getparam, pid, param, MUSL_TYPE_PROCESS); +#else + r = __syscall(SYS_sched_getparam, pid , param); + if (r >= 0) { + r = 0; + } +#endif +exit: + return __syscall_ret(r); } diff --git a/src/sched/sched_getscheduler.c b/src/sched/sched_getscheduler.c index 394e508b4..eddc89df6 100644 --- a/src/sched/sched_getscheduler.c +++ b/src/sched/sched_getscheduler.c @@ -1,8 +1,17 @@ #include #include #include "syscall.h" +#ifdef __LITEOS_A__ +#include "pthread_impl.h" +#endif int sched_getscheduler(pid_t pid) { - return __syscall_ret(-ENOSYS); + int r = -ENOSYS; +#ifdef __LITEOS_A__ + r = __syscall(SYS_sched_getscheduler, pid, MUSL_TYPE_PROCESS); +#else + r = __syscall(SYS_sched_getscheduler, pid); +#endif + return __syscall_ret(r); } diff --git a/src/sched/sched_setparam.c b/src/sched/sched_setparam.c index 18623ee49..8e1a38f5e 100644 --- a/src/sched/sched_setparam.c +++ b/src/sched/sched_setparam.c @@ -1,8 +1,22 @@ #include #include #include "syscall.h" +#ifdef __LITEOS_A__ +#include "pthread_impl.h" +#endif int sched_setparam(pid_t pid, const struct sched_param *param) { - return __syscall_ret(-ENOSYS); + int r; + if (!param) { + r = -EINVAL; + goto exit; + } +#ifdef __LITEOS_A__ + r = __syscall(SYS_sched_setparam, pid, param, MUSL_TYPE_PROCESS); +#else + r = __syscall(SYS_sched_setparam, pid, param); +#endif +exit: + return __syscall_ret(r); } diff --git a/src/sched/sched_setscheduler.c b/src/sched/sched_setscheduler.c index 4435f2164..f917c993f 100644 --- a/src/sched/sched_setscheduler.c +++ b/src/sched/sched_setscheduler.c @@ -1,8 +1,22 @@ #include #include #include "syscall.h" +#ifdef __LITEOS_A__ +#include "pthread_impl.h" +#endif int sched_setscheduler(pid_t pid, int sched, const struct sched_param *param) { - return __syscall_ret(-ENOSYS); + int r; + if (!param) { + r = -EINVAL; + goto exit; + } +#ifdef __LITEOS_A__ + r = __syscall(SYS_sched_setscheduler, pid, sched, param, MUSL_TYPE_PROCESS); +#else + r = __syscall(SYS_sched_setscheduler, pid, sched, param); +#endif +exit: + return __syscall_ret(r); } diff --git a/src/sched/sched_yield.c b/src/sched/sched_yield.c index ee6f0e7f1..bd95d458d 100644 --- a/src/sched/sched_yield.c +++ b/src/sched/sched_yield.c @@ -3,5 +3,9 @@ int sched_yield() { +#ifdef __LITEOS_A__ + return syscall(SYS_sched_yield, 0); +#else return syscall(SYS_sched_yield); +#endif } diff --git a/src/select/select.c b/src/select/select.c index 8a7868840..8e10c2da3 100644 --- a/src/select/select.c +++ b/src/select/select.c @@ -36,9 +36,17 @@ int select(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict #endif #ifdef SYS_select return syscall_cp(SYS_select, n, rfds, wfds, efds, +#ifdef __LITEOS_A__ + tv ? ((long long[]){s, us}) : 0); +#else tv ? ((long[]){s, us}) : 0); +#endif #else return syscall_cp(SYS_pselect6, n, rfds, wfds, efds, +#ifdef __LITEOS_A__ + tv ? ((long long[]){s, ns}) : 0, ((syscall_arg_t[]){ 0, _NSIG/8 })); +#else tv ? ((long[]){s, ns}) : 0, ((syscall_arg_t[]){ 0, _NSIG/8 })); #endif +#endif } diff --git a/porting/linux/user/src/sigchain/sigchain.c b/src/sigchain/linux/sigchain.c similarity index 100% rename from porting/linux/user/src/sigchain/sigchain.c rename to src/sigchain/linux/sigchain.c diff --git a/porting/liteos_a/user/src/signal/arm/restore.s b/src/signal/arm/liteos_a/restore.s similarity index 98% rename from porting/liteos_a/user/src/signal/arm/restore.s rename to src/signal/arm/liteos_a/restore.s index 5450c9a1b..f3cd31f10 100644 --- a/porting/liteos_a/user/src/signal/arm/restore.s +++ b/src/signal/arm/liteos_a/restore.s @@ -25,4 +25,4 @@ __restore: .type __restore_rt,%function __restore_rt: mov r7,#173 - swi 0x0 + swi 0x0 \ No newline at end of file diff --git a/src/signal/getitimer.c b/src/signal/getitimer.c index 36d1eb9dc..cbdf25550 100644 --- a/src/signal/getitimer.c +++ b/src/signal/getitimer.c @@ -3,6 +3,7 @@ int getitimer(int which, struct itimerval *old) { +#ifndef __LITEOS_A__ if (sizeof(time_t) > sizeof(long)) { long old32[4]; int r = __syscall(SYS_getitimer, which, old32); @@ -14,5 +15,6 @@ int getitimer(int which, struct itimerval *old) } return __syscall_ret(r); } +#endif return syscall(SYS_getitimer, which, old); } diff --git a/src/signal/psiginfo.c b/src/signal/psiginfo.c index 2b15982be..9b1d3105c 100644 --- a/src/signal/psiginfo.c +++ b/src/signal/psiginfo.c @@ -1,6 +1,8 @@ #include +#include void psiginfo(const siginfo_t *si, const char *msg) { + UNSUPPORTED_API_VOID(LITEOS_A); psignal(si->si_signo, msg); } diff --git a/src/signal/setitimer.c b/src/signal/setitimer.c index 0dfbeb4db..4cb0074ec 100644 --- a/src/signal/setitimer.c +++ b/src/signal/setitimer.c @@ -6,6 +6,7 @@ int setitimer(int which, const struct itimerval *restrict new, struct itimerval *restrict old) { +#ifndef __LITEOS_A__ if (sizeof(time_t) > sizeof(long)) { time_t is = new->it_interval.tv_sec, vs = new->it_value.tv_sec; long ius = new->it_interval.tv_usec, vus = new->it_value.tv_usec; @@ -22,5 +23,6 @@ int setitimer(int which, const struct itimerval *restrict new, struct itimerval } return __syscall_ret(r); } +#endif return syscall(SYS_setitimer, which, new, old); } diff --git a/src/signal/sigaction.c b/src/signal/sigaction.c index 2203471b2..9c419fdfb 100644 --- a/src/signal/sigaction.c +++ b/src/signal/sigaction.c @@ -1,12 +1,287 @@ #include #include #include +#include #include "syscall.h" #include "pthread_impl.h" #include "libc.h" #include "lock.h" #include "ksigaction.h" +#ifdef LINUX_MUSL_SUPPORTED +extern bool intercept_sigaction(int signo, const struct sigaction *restrict sa, struct sigaction *restrict old); + +static volatile int dummy_lock[1] = { 0 }; + +extern hidden volatile int __abort_lock[1]; + +weak_alias(dummy_lock, __abort_lock); +#endif + +#ifdef __LITEOS_A__ +#define DEFAULT_SIG_NUM 64 +#define SIGNO2SET(s) ((sigset_t)1 << (s)) +#define NULL_SIGNAL_SET ((sigset_t)0x00000000) +#define SET_BIT(bitmap, pos) (bitmap |= (1u << pos)) +#define CLEAR_BIT(bitmap, pos) (bitmap &= ~(1u << pos)) +#define CHECK_BIT(bitmap, pos) ((bitmap & (1u << pos)) ? 1 : 0) +#define SIG_FLAG_NOIGNORE 1 + +struct sigactq { + struct sigaction act; + bool ign_flag; + unsigned char signo; + unsigned char sigmask; + unsigned char reserve[2]; +}; +typedef struct sigactq sigactq_t; + +typedef void (*sa_sighandler_t)(int); +typedef struct sigaction sigaction_t; + +static sigactq_t g_sig_arr[DEFAULT_SIG_NUM]; +static pthread_spinlock_t sig_lite_lock; + +struct sig_default_act { + unsigned char singNo; + unsigned char flag; + sa_sighandler_t action; +}; + +static void __sig_core(int signo); +static void __sig_kill(int signo); +static void __sig_cont(int signo); +static void __sig_stop(int signo); +static void __sig_ignore(int signo); +static const struct sig_default_act sig_default_action[] = { + {SIGHUP, 0, __sig_kill}, + {SIGINT, 0, __sig_kill}, + {SIGQUIT, 0, __sig_core}, + {SIGILL, 0, __sig_core}, + {SIGTRAP, 0, __sig_core}, + {SIGABRT, 0, __sig_core}, + {SIGBUS, 0, __sig_core}, + {SIGFPE, 0, __sig_core}, + {SIGKILL, SIG_FLAG_NOIGNORE, __sig_kill}, + {SIGUSR1, 0, __sig_kill}, + {SIGSEGV, 0, __sig_core}, + {SIGUSR2, 0, __sig_kill}, + {SIGPIPE, 0, __sig_kill}, + {SIGALRM, 0, __sig_kill}, + {SIGTERM, 0, __sig_kill}, + {SIGSTKFLT, 0, __sig_kill}, + {SIGCHLD, 0, __sig_ignore}, + {SIGCONT, SIG_FLAG_NOIGNORE, __sig_cont}, + {SIGSTOP, SIG_FLAG_NOIGNORE, __sig_stop}, + {SIGTSTP, 0, __sig_stop}, + {SIGTTIN, 0, __sig_stop}, + {SIGTTOU, 0, __sig_stop}, + {SIGURG, 0, __sig_ignore}, + {SIGXCPU, 0, __sig_core}, + {SIGXFSZ, 0, __sig_core}, + {SIGVTALRM, 0, __sig_kill}, + {SIGPROF, 0, __sig_kill}, + {SIGWINCH, 0, __sig_ignore}, + {SIGIO, 0, __sig_kill}, + {SIGPWR, 0, __sig_kill}, + {SIGSYS, 0, __sig_ignore}, + {32, 0, __sig_ignore}, + {33, 0, __sig_ignore}, + {34, 0, __sig_ignore}, + {35, 0, __sig_ignore}, + {36, 0, __sig_ignore}, + {37, 0, __sig_ignore}, + {38, 0, __sig_ignore}, + {39, 0, __sig_ignore}, + {40, 0, __sig_ignore}, + {41, 0, __sig_ignore}, + {42, 0, __sig_ignore}, + {43, 0, __sig_ignore}, + {44, 0, __sig_ignore}, + {45, 0, __sig_ignore}, + {46, 0, __sig_ignore}, + {47, 0, __sig_ignore}, + {48, 0, __sig_ignore}, + {49, 0, __sig_ignore}, + {50, 0, __sig_ignore}, + {51, 0, __sig_ignore}, + {52, 0, __sig_ignore}, + {53, 0, __sig_ignore}, + {54, 0, __sig_ignore}, + {55, 0, __sig_ignore}, + {56, 0, __sig_ignore}, + {57, 0, __sig_ignore}, + {58, 0, __sig_ignore}, + {59, 0, __sig_ignore}, + {60, 0, __sig_ignore}, + {61, 0, __sig_ignore}, + {62, 0, __sig_ignore}, + {63, 0, __sig_ignore}, + {64, 0, __sig_ignore}, +}; + +static void __sig_core(int signo) +{ + exit(-1); +} + +static void __sig_kill(int signo) +{ + exit(-1); +} + +static void __sig_cont(int signo) +{ + return; +} + +static void __sig_stop(int signo) +{ + return; +} + +static void __sig_ignore(int signo) +{ + return; +} + +static sigactq_t *__sig_find_action(int sig) +{ + int i; + + for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { + if (g_sig_arr[i].signo == sig) { + return (g_sig_arr + i); + } + } +} + +static void __sig_copy_sigaction(sigaction_t *src, sigaction_t *dst) +{ + dst->sa_handler = src->sa_handler; + dst->sa_mask = src->sa_mask; + dst->sa_flags = src->sa_flags; +} + +static int __sig_cannot_catche(int sig, sa_sighandler_t handler) +{ + int i; + + for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { + if (sig == sig_default_action[i].singNo) { + return (sig_default_action[i].flag == SIG_FLAG_NOIGNORE) && (handler != SIG_DFL); + } + } + /* This sig can be catch and ignore return false */ + return 0; +} + +static void __sig_operation(unsigned int receivedSigno) +{ + int i; + sigset_t mask, oldmask; + + sigemptyset(&mask); + sigemptyset(&oldmask); + + for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { + if (!g_sig_arr[i].ign_flag && g_sig_arr[i].signo == receivedSigno && g_sig_arr[i].act.sa_handler) { + sigaddset(&mask, receivedSigno); + sigprocmask(SIG_BLOCK, &mask, &oldmask); + sigprocmask(SIG_BLOCK, &g_sig_arr[i].act.sa_mask, NULL); + (*g_sig_arr[i].act.sa_handler)(g_sig_arr[i].signo); + sigprocmask(SIG_SETMASK, &oldmask, NULL); + return; + } + } +} + +void arm_signal_process(unsigned int receivedSig) +{ + __sig_operation(receivedSig); +} + +static void __sig_add_def_action() +{ + int i; + + for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { + g_sig_arr[i].signo = (unsigned char)sig_default_action[i].singNo; + g_sig_arr[i].act.sa_handler = sig_default_action[i].action; + sigemptyset(&g_sig_arr[i].act.sa_mask); + g_sig_arr[i].act.sa_flags = sig_default_action[i].flag; + g_sig_arr[i].ign_flag = false; + } +} + +static sa_sighandler_t __sig_find_def_action(unsigned char signo) +{ + int i; + + for (i = 0; i < sizeof(sig_default_action) / sizeof(struct sig_default_act); i++) { + if (signo == sig_default_action[i].singNo) { + return sig_default_action[i].action; + } + } + return NULL; +} + +static int __sig_dfl_opr(int sig, sigactq_t *sigact, const sigaction_t *act) +{ + sa_sighandler_t def_handler = NULL; + + def_handler = __sig_find_def_action(sig); + + if (def_handler != NULL) { + /* Replace it from signal action queue */ + sigact->act.sa_handler = def_handler; + sigact->act.sa_mask = act->sa_mask; + sigact->act.sa_flags = act->sa_flags; + } + return 0; +} + +static int __sig_action_opr(int sig, const sigaction_t *act, sigaction_t *oact) +{ + int ret = 0; + sa_sighandler_t handler = NULL; + sigactq_t *sigact = NULL; + + if (act == NULL) return -EINVAL; + + if (sig < SIGHUP || sig > (_NSIG - 1)) return -EINVAL; + + handler = act->sa_handler; + /* Skip sig that can not be catched */ + if (__sig_cannot_catche(sig, handler)) return -EINVAL; + + pthread_spin_lock(&sig_lite_lock); + sigact = __sig_find_action(sig); + if (sigact && oact) __sig_copy_sigaction(&sigact->act, oact); + + sigact->ign_flag = false; + + if (handler == SIG_IGN && sigact) { + sigact->ign_flag = true; + } else if (handler == SIG_DFL) { + ret = __sig_dfl_opr(sig, sigact, act); + } else { + sigact->act.sa_handler = handler; + sigact->act.sa_mask = act->sa_mask; + sigact->act.sa_flags = act->sa_flags; + } + + pthread_spin_unlock(&sig_lite_lock); + return ret; +} + +void __sig_init(void) +{ + signal(SIGSYS, arm_do_signal); + pthread_spin_init(&sig_lite_lock, 0); + __sig_add_def_action(); +} +#endif static int unmask_done; static unsigned long handler_set[_NSIG/(8*sizeof(long))]; @@ -19,7 +294,15 @@ volatile int __eintr_valid_flag; int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) { +#ifdef __LITEOS_A__ + sigaction_t ksa, ksa_old; + int r = 0; +#else struct k_sigaction ksa, ksa_old; +#endif +#ifdef LINUX_MUSL_SUPPORTED + unsigned long set[_NSIG/(8*sizeof(long))]; +#endif if (sa) { if ((uintptr_t)sa->sa_handler > 1UL) { a_or_l(handler_set+(sig-1)/(8*sizeof(long)), @@ -43,29 +326,76 @@ int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigact a_store(&__eintr_valid_flag, 1); } } +#ifdef LINUX_MUSL_SUPPORTED + /* Changing the disposition of SIGABRT to anything but + * SIG_DFL requires a lock, so that it cannot be changed + * while abort is terminating the process after simply + * calling raise(SIGABRT) failed to do so. */ + if (sa->sa_handler != SIG_DFL && sig == SIGABRT) { + __block_all_sigs(&set); + LOCK(__abort_lock); + } +#endif +#ifdef __LITEOS_A__ + ksa.sa_handler = sa->sa_handler; + ksa.sa_flags = sa->sa_flags | SA_RESTORER; + ksa.sa_restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore; + memcpy(&ksa.sa_mask, &sa->sa_mask, _NSIG/8); +#else ksa.handler = sa->sa_handler; ksa.flags = sa->sa_flags | SA_RESTORER; ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore; memcpy(&ksa.mask, &sa->sa_mask, _NSIG/8); +#endif } + +#ifdef __LITEOS_A__ + if (sig == SIGSYS) { + return syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8); + } else { + r = __sig_action_opr(sig, (const sigaction_t*)sa?&ksa:0, (sigaction_t*)old?&ksa_old:0); + } +#else int r = __syscall(SYS_rt_sigaction, sig, sa?&ksa:0, old?&ksa_old:0, _NSIG/8); +#ifdef LINUX_MUSL_SUPPORTED + if (sig == SIGABRT && sa && sa->sa_handler != SIG_DFL) { + UNLOCK(__abort_lock); + __restore_sigs(&set); + } +#endif +#endif if (old && !r) { +#ifdef __LITEOS_A__ + old->sa_handler = ksa_old.sa_handler; + old->sa_flags = ksa_old.sa_flags; + memcpy(&old->sa_mask, &ksa_old.sa_mask, _NSIG/8); +#else old->sa_handler = ksa_old.handler; old->sa_flags = ksa_old.flags; memcpy(&old->sa_mask, &ksa_old.mask, _NSIG/8); +#endif } return __syscall_ret(r); } int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old) { +#ifndef LINUX_MUSL_SUPPORTED unsigned long set[_NSIG/(8*sizeof(long))]; - +#endif if (sig-32U < 3 || sig-1U >= _NSIG-1) { errno = EINVAL; return -1; } +#ifdef LINUX_MUSL_SUPPORTED + /* sigchain intercepts sigaction */ + if (intercept_sigaction(sig, sa, old)) { + return 0; + } + + return __libc_sigaction(sig, sa, old); +#else /* Doing anything with the disposition of SIGABRT requires a lock, * so that it cannot be changed while abort is terminating the * process and so any change made by abort can't be observed. */ @@ -79,6 +409,7 @@ int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction * __restore_sigs(&set); } return r; +#endif } weak_alias(__sigaction, sigaction); diff --git a/src/signal/sigaltstack.c b/src/signal/sigaltstack.c index d3a6e8215..50b2f7b1b 100644 --- a/src/signal/sigaltstack.c +++ b/src/signal/sigaltstack.c @@ -1,9 +1,11 @@ #include #include #include "syscall.h" +#include int sigaltstack(const stack_t *restrict ss, stack_t *restrict old) { + UNSUPPORTED_API_VOID(LITEOS_A); if (ss) { if (!(ss->ss_flags & SS_DISABLE) && ss->ss_size < MINSIGSTKSZ) { errno = ENOMEM; diff --git a/src/signal/siginterrupt.c b/src/signal/siginterrupt.c index 700634003..ac99295be 100644 --- a/src/signal/siginterrupt.c +++ b/src/signal/siginterrupt.c @@ -1,7 +1,26 @@ #include +#include +#ifdef LINUX_MUSL_SUPPORTED +#include +extern int __libc_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old); +#endif int siginterrupt(int sig, int flag) { +#ifdef LINUX_MUSL_SUPPORTED + if (sig-32U < 3 || sig-1U >= _NSIG-1) { + errno = EINVAL; + return -1; + } + struct sigaction sa; + + __libc_sigaction(sig, 0, &sa); + if (flag) sa.sa_flags &= ~SA_RESTART; + else sa.sa_flags |= SA_RESTART; + + return __libc_sigaction(sig, &sa, 0); +#else + UNSUPPORTED_API_VOID(LITEOS_A); struct sigaction sa; sigaction(sig, 0, &sa); @@ -9,4 +28,5 @@ int siginterrupt(int sig, int flag) else sa.sa_flags |= SA_RESTART; return sigaction(sig, &sa, 0); +#endif } diff --git a/src/signal/siglongjmp.c b/src/signal/siglongjmp.c index bc317acce..6a0b3a8b0 100644 --- a/src/signal/siglongjmp.c +++ b/src/signal/siglongjmp.c @@ -2,8 +2,10 @@ #include #include "syscall.h" #include "pthread_impl.h" +#include _Noreturn void siglongjmp(sigjmp_buf buf, int ret) { + UNSUPPORTED_API_VOID(LITEOS_A); longjmp(buf, ret); } diff --git a/src/signal/signal.c b/src/signal/signal.c index 7a6dd1727..2b20c6faa 100644 --- a/src/signal/signal.c +++ b/src/signal/signal.c @@ -1,5 +1,4 @@ #include -#include "syscall.h" void (*signal(int sig, void (*func)(int)))(int) { diff --git a/src/signal/sigqueue.c b/src/signal/sigqueue.c index b75f0c5ce..1a064b4ef 100644 --- a/src/signal/sigqueue.c +++ b/src/signal/sigqueue.c @@ -3,9 +3,11 @@ #include #include "syscall.h" #include "pthread_impl.h" +#include int sigqueue(pid_t pid, int sig, const union sigval value) { + UNSUPPORTED_API_VOID(LITEOS_A); siginfo_t si; sigset_t set; int r; diff --git a/src/signal/sigsuspend.c b/src/signal/sigsuspend.c index 36e0602c6..8deb8fe8c 100644 --- a/src/signal/sigsuspend.c +++ b/src/signal/sigsuspend.c @@ -3,5 +3,26 @@ int sigsuspend(const sigset_t *mask) { +#ifdef __LITEOS_A__ + int ret,retval; + sigset_t oldset; + + retval = sigprocmask(SIG_BLOCK, 0, &oldset); + if (retval != 0){ + return retval; + } + + ret = syscall_cp(SYS_rt_sigsuspend, mask, _NSIG/8); + + if (ret == -1){ + retval = sigprocmask(SIG_SETMASK, &oldset, 0); + if (retval != 0){ + return retval; + } + } + + return ret; +#else return syscall_cp(SYS_rt_sigsuspend, mask, _NSIG/8); +#endif } diff --git a/src/stat/fstat.c b/src/stat/fstat.c index 9bbb46dec..daaafc83c 100644 --- a/src/stat/fstat.c +++ b/src/stat/fstat.c @@ -4,12 +4,23 @@ #include #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +int __fstat(int fd, struct stat *st) +#else int fstat(int fd, struct stat *st) +#endif { if (fd<0) return __syscall_ret(-EBADF); return fstatat(fd, "", st, AT_EMPTY_PATH); } +#ifdef LINUX_MUSL_SUPPORTED +weak_alias(__fstat, fstat); +#if !_REDIR_TIME64 +weak_alias(__fstat, fstat64); +#endif +#else #if !_REDIR_TIME64 weak_alias(fstat, fstat64); #endif +#endif diff --git a/src/stat/mkfifo.c b/src/stat/mkfifo.c index 60efcf73f..b0d9be318 100644 --- a/src/stat/mkfifo.c +++ b/src/stat/mkfifo.c @@ -1,6 +1,13 @@ #include +#ifdef __LITEOS_A__ +#include "syscall.h" +#endif int mkfifo(const char *path, mode_t mode) { +#ifdef __LITEOS_A__ + return syscall(SYS_mkfifo, path, mode); +#else return mknod(path, mode | S_IFIFO, 0); +#endif } diff --git a/src/stat/mkfifoat.c b/src/stat/mkfifoat.c index d3a1f9708..98095f7fc 100644 --- a/src/stat/mkfifoat.c +++ b/src/stat/mkfifoat.c @@ -1,6 +1,8 @@ #include +#include int mkfifoat(int fd, const char *path, mode_t mode) { + UNSUPPORTED_API_VOID(LITEOS_A); return mknodat(fd, path, mode | S_IFIFO, 0); } diff --git a/src/stat/mknod.c b/src/stat/mknod.c index beebd84e0..5a4750439 100644 --- a/src/stat/mknod.c +++ b/src/stat/mknod.c @@ -1,9 +1,11 @@ #include #include #include "syscall.h" +#include int mknod(const char *path, mode_t mode, dev_t dev) { + UNSUPPORTED_API_VOID(LITEOS_A); #ifdef SYS_mknod return syscall(SYS_mknod, path, mode, dev); #else diff --git a/src/stat/mknodat.c b/src/stat/mknodat.c index 7c97c91aa..a79e857b8 100644 --- a/src/stat/mknodat.c +++ b/src/stat/mknodat.c @@ -1,7 +1,9 @@ #include #include "syscall.h" +#include int mknodat(int fd, const char *path, mode_t mode, dev_t dev) { + UNSUPPORTED_API_VOID(LITEOS_A); return syscall(SYS_mknodat, fd, path, mode, dev); } diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c index 116e78e56..4eb342104 100644 --- a/src/stdio/__fdopen.c +++ b/src/stdio/__fdopen.c @@ -1,61 +1,139 @@ +/* Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "stdio_impl.h" #include #include +#include #include #include #include #include "libc.h" -FILE *__fdopen(int fd, const char *mode) +static size_t __get_bufsize(int fd) { - FILE *f; - struct winsize wsz; + struct stat st; + size_t buf_size = 0; + + if (fstat(fd, &st) < 0) { + buf_size = BUFSIZ; + } else if (st.st_blksize == 0) { + buf_size = BUFSIZ; + } else { + buf_size = st.st_blksize; + } - /* Check for valid initial mode character */ - if (!strchr("rwa", *mode)) { - errno = EINVAL; + return buf_size; +} + +int __falloc_buf(FILE *f) +{ + /* return if already allocated, or F_NOBUF set */ + if (f->buf != NULL || f->buf_size != 0 || f->flags & F_NOBUF) { return 0; } - /* Allocate FILE+buffer or fail */ - if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0; + /* Default, base and buf are NULL,and buf_size = 0 */ + size_t buf_size = 0; - /* Zero-fill only the struct, not the buffer */ - memset(f, 0, sizeof *f); + /* get buffer size via file stat */ + buf_size = __get_bufsize(f->fd); + + /* alloc R/W buffer */ + f->base = (unsigned char *)malloc(UNGET + buf_size * sizeof(unsigned char)); + if (!f->base) { + errno = -ENOMEM; + return errno; + } + + /* reserve UNGET buffer */ + f->buf = f->base + UNGET; + f->buf_size = buf_size; + + return 0; +} + +FILE *__fdopen(int fd, const char *mode) +{ + FILE *f = NULL; + int file_flags = 0; + int mode_flags = 0; + + int flags = syscall(SYS_fcntl, fd, F_GETFL, 0); + if (flags == -1) { + return NULL; + } - /* Impose mode restrictions */ - if (!strchr(mode, '+')) f->flags = (*mode == 'r') ? F_NOWR : F_NORD; + /* Compute the flags to pass to open() */ + mode_flags = __fmodeflags(mode, &file_flags); + if (mode_flags < 0) { + return NULL; + } - /* Apply close-on-exec flag */ - if (strchr(mode, 'e')) __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); + if (mode_flags & O_CLOEXEC) { + __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); + } - /* Set append mode on fd if opened for append */ - if (*mode == 'a') { - int flags = __syscall(SYS_fcntl, fd, F_GETFL); + if (mode_flags & O_APPEND) { + flags = __syscall(SYS_fcntl, fd, F_GETFL); if (!(flags & O_APPEND)) __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND); - f->flags |= F_APP; } + f = __fdopenx(fd, file_flags); + if (f) { + return f; + } + + return NULL; +} +weak_alias(__fdopen, fdopen); + +FILE *__fdopenx(int fd, int flags) +{ + FILE *f = 0; + struct winsize wsz; + + /* Allocate FILE or fail */ + if (!(f = __ofl_alloc())) { + return NULL; + } + + /* Zero-fill only the struct, not the buffer */ + memset(f, 0, sizeof *f); + + f->flags = flags; f->fd = fd; - f->buf = (unsigned char *)f + sizeof *f + UNGET; - f->buf_size = BUFSIZ; /* Activate line buffered mode for terminals */ f->lbf = EOF; - if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) + if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)) { f->lbf = '\n'; + } /* Initialize op ptrs. No problem if some are unneeded. */ f->read = __stdio_read; f->write = __stdio_write; f->seek = __stdio_seek; f->close = __stdio_close; + f->readx = __stdio_readx; - if (!libc.threaded) f->lock = -1; + if (!libc.threaded) { + f->lock = -1; + } /* Add new FILE to open file list */ return __ofl_add(f); } -weak_alias(__fdopen, fdopen); diff --git a/src/stdio/__fmodeflags.c b/src/stdio/__fmodeflags.c index da9f23b63..f738b270a 100644 --- a/src/stdio/__fmodeflags.c +++ b/src/stdio/__fmodeflags.c @@ -1,16 +1,55 @@ +#include "stdio_impl.h" #include #include +#include -int __fmodeflags(const char *mode) +int __fmodeflags(const char *mode, int *flags) { - int flags; - if (strchr(mode, '+')) flags = O_RDWR; - else if (*mode == 'r') flags = O_RDONLY; - else flags = O_WRONLY; - if (strchr(mode, 'x')) flags |= O_EXCL; - if (strchr(mode, 'e')) flags |= O_CLOEXEC; - if (*mode != 'r') flags |= O_CREAT; - if (*mode == 'w') flags |= O_TRUNC; - if (*mode == 'a') flags |= O_APPEND; - return flags; + int mode_flags = 0; + int options = 0; + + switch (*mode) { + case 'r': + mode_flags = O_RDONLY; + *flags = F_NOWR; + break; + case 'w': + mode_flags = O_WRONLY; + options = O_TRUNC | O_CREAT; + *flags = F_NORD; + break; + case 'a': + mode_flags = O_WRONLY; + options = O_APPEND | O_CREAT; + *flags = F_NORD | F_APP; + break; + default: + errno = EINVAL; + return -EINVAL; + } + + mode++; + while (*mode != '\0') { + switch (*mode) { + case '+': + mode_flags = O_RDWR; + *flags &= ~(F_NORD | F_NOWR); + break; + case 'x': + /* need O_CREAT check */ + options |= O_EXCL; + break; + case 'e': + options |= O_CLOEXEC; + break; + case 'b': + break; + default: + /* only accept "+xeb" */ + break; + } + mode++; + } + + return mode_flags | options; } diff --git a/src/stdio/__stdio_exit.c b/src/stdio/__stdio_exit.c index a5e42c675..ad7f6d915 100644 --- a/src/stdio/__stdio_exit.c +++ b/src/stdio/__stdio_exit.c @@ -8,7 +8,9 @@ weak_alias(dummy_file, __stderr_used); static void close_file(FILE *f) { if (!f) return; +#ifndef __LITEOS_A__ FFINALLOCK(f); +#endif if (f->wpos != f->wbase) f->write(f, 0, 0); if (f->rpos != f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR); } diff --git a/src/stdio/__stdio_read.c b/src/stdio/__stdio_read.c index ea675da34..dd681e0b8 100644 --- a/src/stdio/__stdio_read.c +++ b/src/stdio/__stdio_read.c @@ -1,22 +1,34 @@ #include "stdio_impl.h" #include +size_t __stdio_readx(FILE *f, unsigned char *buf, size_t len) +{ + ssize_t cnt = syscall(SYS_read, f->fd, buf, len); + if (cnt <= 0) { + f->flags |= cnt ? F_ERR : F_EOF; + return 0; + } + return cnt; +} + size_t __stdio_read(FILE *f, unsigned char *buf, size_t len) { - struct iovec iov[2] = { + struct iovec iov_buf[2] = { { .iov_base = buf, .iov_len = len - !!f->buf_size }, { .iov_base = f->buf, .iov_len = f->buf_size } }; ssize_t cnt; - cnt = iov[0].iov_len ? syscall(SYS_readv, f->fd, iov, 2) - : syscall(SYS_read, f->fd, iov[1].iov_base, iov[1].iov_len); + cnt = iov_buf[0].iov_len ? syscall(SYS_readv, f->fd, iov_buf, 2) + : syscall(SYS_read, f->fd, iov_buf[1].iov_base, iov_buf[1].iov_len); if (cnt <= 0) { f->flags |= cnt ? F_ERR : F_EOF; return 0; } - if (cnt <= iov[0].iov_len) return cnt; - cnt -= iov[0].iov_len; + if (cnt <= iov_buf[0].iov_len) { + return cnt; + } + cnt -= iov_buf[0].iov_len; f->rpos = f->buf; f->rend = f->buf + cnt; if (f->buf_size) buf[len-1] = *f->rpos++; diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c index d2d89475b..6774a2946 100644 --- a/src/stdio/__stdio_write.c +++ b/src/stdio/__stdio_write.c @@ -1,34 +1,64 @@ #include "stdio_impl.h" #include +#include + +ssize_t __flush_buffer(FILE *f) +{ + ssize_t cnt = 0; + char *wbase = (char *)f->wbase; + size_t rem = f->wpos - f->wbase; + while (rem > 0) { + cnt = syscall(SYS_write, f->fd, wbase, rem); + if (cnt < 0) { + f->wpos = f->wbase = f->wend = 0; + f->flags |= F_ERR; + return cnt; + } + wbase += cnt; + rem -= cnt; + } + + /* reset file buffer */ + f->wend = f->buf + f->buf_size; + f->wpos = f->wbase = f->buf; + return cnt; +} size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) { - struct iovec iovs[2] = { - { .iov_base = f->wbase, .iov_len = f->wpos-f->wbase }, - { .iov_base = (void *)buf, .iov_len = len } - }; - struct iovec *iov = iovs; - size_t rem = iov[0].iov_len + iov[1].iov_len; - int iovcnt = 2; - ssize_t cnt; + size_t rem = len; + unsigned char *wbuf = (unsigned char *)buf; + + /* flush buffer first */ + ssize_t cnt = __flush_buffer(f); + if (cnt < 0) { + return 0; + } + for (;;) { - cnt = syscall(SYS_writev, f->fd, iov, iovcnt); - if (cnt == rem) { - f->wend = f->buf + f->buf_size; - f->wpos = f->wbase = f->buf; + if (f->lbf < 0 && rem <= f->wend - f->wpos) { + memcpy(f->wpos, wbuf, rem); + f->wpos += rem; return len; } + + /* write directly if + * 1. file buffer < rem + * 2. line buffer mode + */ + cnt = syscall(SYS_write, f->fd, wbuf, rem); if (cnt < 0) { f->wpos = f->wbase = f->wend = 0; f->flags |= F_ERR; - return iovcnt == 2 ? 0 : len-iov[0].iov_len; + return len - rem; } + rem -= cnt; - if (cnt > iov[0].iov_len) { - cnt -= iov[0].iov_len; - iov++; iovcnt--; + wbuf += cnt; + if (rem == 0) { + break; } - iov[0].iov_base = (char *)iov[0].iov_base + cnt; - iov[0].iov_len -= cnt; } + + return len; } diff --git a/src/stdio/__stdout_write.c b/src/stdio/__stdout_write.c index dd1ec60ff..cd78374dd 100644 --- a/src/stdio/__stdout_write.c +++ b/src/stdio/__stdout_write.c @@ -5,7 +5,14 @@ size_t __stdout_write(FILE *f, const unsigned char *buf, size_t len) { struct winsize wsz; f->write = __stdio_write; + /* + * write directly at first time. + * check f->flags & tty, and take effect later. + */ + size_t cnt = __stdio_write(f, buf, len); + if (!(f->flags & F_SVB) && __syscall(SYS_ioctl, f->fd, TIOCGWINSZ, &wsz)) f->lbf = -1; - return __stdio_write(f, buf, len); + + return cnt; } diff --git a/src/stdio/__toread.c b/src/stdio/__toread.c index f142ff09c..b03f878d3 100644 --- a/src/stdio/__toread.c +++ b/src/stdio/__toread.c @@ -9,7 +9,15 @@ int __toread(FILE *f) f->flags |= F_ERR; return EOF; } + + /* Alloc file buffer if needed */ + if (__falloc_buf(f) < 0) { + f->flags |= F_ERR; + return EOF; + } + f->rpos = f->rend = f->buf + f->buf_size; + return (f->flags & F_EOF) ? EOF : 0; } diff --git a/src/stdio/__towrite.c b/src/stdio/__towrite.c index 4c9c66ae7..382a3abd5 100644 --- a/src/stdio/__towrite.c +++ b/src/stdio/__towrite.c @@ -10,10 +10,15 @@ int __towrite(FILE *f) /* Clear read buffer (easier than summoning nasal demons) */ f->rpos = f->rend = 0; + /* Alloc file buffer if needed */ + if (__falloc_buf(f) < 0) { + f->flags |= F_ERR; + return EOF; + } + /* Activate write through the buffer. */ f->wpos = f->wbase = f->buf; f->wend = f->buf + f->buf_size; - return 0; } diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c index d594532bd..4f3ab1133 100644 --- a/src/stdio/fclose.c +++ b/src/stdio/fclose.c @@ -1,5 +1,6 @@ #include "stdio_impl.h" #include +#include static void dummy(FILE *f) { } weak_alias(dummy, __unlist_locked_file); @@ -9,6 +10,12 @@ int fclose(FILE *f) int r; FLOCK(f); + if (!f || f->fd < 0) { + errno = EBADF; + FUNLOCK(f); + return -EBADF; + } + r = fflush(f); r |= f->close(f); FUNLOCK(f); @@ -25,14 +32,15 @@ int fclose(FILE *f) __unlist_locked_file(f); - FILE **head = __ofl_lock(); - if (f->prev) f->prev->next = f->next; - if (f->next) f->next->prev = f->prev; - if (*head == f) *head = f->next; - __ofl_unlock(); - free(f->getln_buf); - free(f); + /* release base instead of buf which may be modified by setvbuf + * or iniitalize by local variable */ + free(f->base); + + /* set file to invalid descriptor */ + f->fd = -EBADF; + + __ofl_free(f); return r; } diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c index 6171f398d..4a2e81fd9 100644 --- a/src/stdio/fgets.c +++ b/src/stdio/fgets.c @@ -2,7 +2,6 @@ #include #define MIN(a,b) ((a)<(b) ? (a) : (b)) - char *fgets(char *restrict s, int n, FILE *restrict f) { char *p = s; @@ -22,7 +21,7 @@ char *fgets(char *restrict s, int n, FILE *restrict f) while (n) { if (f->rpos != f->rend) { - z = memchr(f->rpos, '\n', f->rend - f->rpos); + z = memchr(f->rpos, '\n', MIN(f->rend - f->rpos, n)); k = z ? z - f->rpos + 1 : f->rend - f->rpos; k = MIN(k, n); memcpy(p, f->rpos, k); @@ -45,4 +44,5 @@ char *fgets(char *restrict s, int n, FILE *restrict f) return s; } + weak_alias(fgets, fgets_unlocked); diff --git a/src/stdio/fileno.c b/src/stdio/fileno.c index 0bd0e9889..1eea733cb 100644 --- a/src/stdio/fileno.c +++ b/src/stdio/fileno.c @@ -1,11 +1,9 @@ #include "stdio_impl.h" #include -int fileno(FILE *f) +int fileno_unlocked(FILE *f) { - FLOCK(f); int fd = f->fd; - FUNLOCK(f); if (fd < 0) { errno = EBADF; return -1; @@ -13,4 +11,10 @@ int fileno(FILE *f) return fd; } -weak_alias(fileno, fileno_unlocked); +int fileno(FILE *f) +{ + FLOCK(f); + int fd = fileno_unlocked(f); + FUNLOCK(f); + return fd; +} diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c index 343e3e3fe..bb857e5a8 100644 --- a/src/stdio/fmemopen.c +++ b/src/stdio/fmemopen.c @@ -117,6 +117,7 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode) else if (plus) *f->c.buf = 0; f->f.read = mread; + f->f.readx = mread; f->f.write = mwrite; f->f.seek = mseek; f->f.close = mclose; diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c index e1b91e12d..b0e66aefd 100644 --- a/src/stdio/fopen.c +++ b/src/stdio/fopen.c @@ -5,26 +5,26 @@ FILE *fopen(const char *restrict filename, const char *restrict mode) { - FILE *f; - int fd; - int flags; - - /* Check for valid initial mode character */ - if (!strchr("rwa", *mode)) { - errno = EINVAL; - return 0; - } + FILE *f = NULL; + int fd = -1; + int file_flags = 0; + int mode_flags = 0; /* Compute the flags to pass to open() */ - flags = __fmodeflags(mode); + mode_flags = __fmodeflags(mode, &file_flags); + if (mode_flags < 0) { + return NULL; + } - fd = sys_open(filename, flags, 0666); + fd = sys_open(filename, mode_flags, 0666); if (fd < 0) return 0; - if (flags & O_CLOEXEC) + if (mode_flags & O_CLOEXEC) __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); - f = __fdopen(fd, mode); - if (f) return f; + f = __fdopenx(fd, file_flags); + if (f) { + return f; + } __syscall(SYS_close, fd); return 0; diff --git a/src/stdio/fread.c b/src/stdio/fread.c index a2116da61..38b685c1a 100644 --- a/src/stdio/fread.c +++ b/src/stdio/fread.c @@ -3,36 +3,80 @@ #define MIN(a,b) ((a)<(b) ? (a) : (b)) +int __fill_buffer(FILE *f) +{ + int r = __toread(f); + if (r != 0) { + return r; + } + + int k = f->readx(f, f->buf, f->buf_size); + if (k <= 0) { + f->flags |= (k == 0) ? F_EOF : F_ERR; + f->rpos = f->rend; + return k; + } + + f->rpos = f->buf; + f->rend = f->rpos + k; + + return 0; +} + size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f) { unsigned char *dest = destv; - size_t len = size*nmemb, l = len, k; - if (!size) nmemb = 0; + size_t len = size * nmemb, l = len, k; + if (!size) { + nmemb = 0; + } FLOCK(f); + /* allocate file buffer if needed */ + if (__falloc_buf(f) < 0) { + f->flags |= F_ERR; + goto exit; + } + f->mode |= f->mode-1; - if (f->rpos != f->rend) { - /* First exhaust the buffer. */ - k = MIN(f->rend - f->rpos, l); - memcpy(dest, f->rpos, k); - f->rpos += k; - dest += k; - l -= k; + while (l > 0) { + if (f->rpos != f->rend) { + /* First exhaust the buffer. */ + k = MIN(f->rend - f->rpos, l); + memcpy(dest, f->rpos, k); + f->rpos += k; + dest += k; + l -= k; + } + /* done */ + if (l == 0) { + goto exit; + } + /* if user buffer is longer than file buffer, + * maybe buffer size is 0, non-buffer mode, + * read directly */ + if (l > f->buf_size) { + break; + } + + if (__fill_buffer(f)) { + goto exit; + } } - + /* Read the remainder directly */ for (; l; l-=k, dest+=k) { - k = __toread(f) ? 0 : f->read(f, dest, l); + k = f->readx(f, dest, l); if (!k) { - FUNLOCK(f); - return (len-l)/size; + break; } } +exit: FUNLOCK(f); - return nmemb; + return (len - l) / size; } weak_alias(fread, fread_unlocked); diff --git a/src/stdio/freopen.c b/src/stdio/freopen.c index 615d4b47b..efd790ca4 100644 --- a/src/stdio/freopen.c +++ b/src/stdio/freopen.c @@ -12,7 +12,8 @@ FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f) { - int fl = __fmodeflags(mode); + int file_flags = 0; + int fl = __fmodeflags(mode, &file_flags); FILE *f2; FLOCK(f); @@ -33,6 +34,7 @@ FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *re f->flags = (f->flags & F_PERM) | f2->flags; f->read = f2->read; + f->readx = f2->readx; f->write = f2->write; f->seek = f2->seek; f->close = f2->close; diff --git a/src/stdio/ofl.c b/src/stdio/ofl.c index aad3d171d..4fe308e02 100644 --- a/src/stdio/ofl.c +++ b/src/stdio/ofl.c @@ -1,18 +1,144 @@ #include "stdio_impl.h" #include "lock.h" #include "fork_impl.h" +#include + +#define DEFAULT_ALLOC_FILE (8) + +static FILE *ofl_head = NULL; +static FILE *ofl_free = NULL; -static FILE *ofl_head; static volatile int ofl_lock[1]; volatile int *const __stdio_ofl_lockptr = ofl_lock; FILE **__ofl_lock() { LOCK(ofl_lock); +#ifdef LINUX_MUSL_SUPPORTED + return &FILE_LIST_HEAD(ofl_head); +#else return &ofl_head; +#endif } void __ofl_unlock() { UNLOCK(ofl_lock); } + +FILE *__ofl_alloc() +{ +#ifdef LINUX_MUSL_SUPPORTED + FILE *fsb = NULL; +#else + unsigned char *fsb = NULL; +#endif + size_t cnt = 0; + FILE *f = NULL; + + LOCK(ofl_lock); +#ifdef LINUX_MUSL_SUPPORTED + if (!FILE_LIST_EMPTY(ofl_free)) { + f = FILE_LIST_HEAD(ofl_free); + FILE_LIST_REMOVE(ofl_free); + UNLOCK(ofl_lock); + + return f; + } +#else + if (ofl_free) { + f = ofl_free; + ofl_free = ofl_free->next; + f->next = NULL; + f->prev = NULL; + UNLOCK(ofl_lock); + + return f; + } +#endif + UNLOCK(ofl_lock); + + /* alloc new FILEs(8) */ +#ifdef LINUX_MUSL_SUPPORTED + fsb = (FILE *)malloc(DEFAULT_ALLOC_FILE * sizeof(FILE)); +#else + fsb = (unsigned char *)malloc(DEFAULT_ALLOC_FILE * sizeof(FILE)); +#endif + if (!fsb) { + return NULL; + } + + LOCK(ofl_lock); +#ifdef LINUX_MUSL_SUPPORTED + for (cnt = 0; cnt < DEFAULT_ALLOC_FILE; cnt++) { + FILE_LIST_INSERT_HEAD(ofl_free, &fsb[cnt]); + } + + /* retrieve fist and move to next free FILE */ + f = FILE_LIST_HEAD(ofl_free); + FILE_LIST_REMOVE(ofl_free); +#else + ofl_free = (FILE*)fsb; + ofl_free->prev = NULL; + f = ofl_free; + + for (cnt = 1; cnt < DEFAULT_ALLOC_FILE; cnt++) { + FILE *tmp = (FILE*)(fsb + cnt * sizeof(FILE)); + tmp->next = NULL; + f->next = tmp; + tmp->prev = f; + f = f->next; + } + + /* reset and move to next free FILE */ + f = ofl_free; + ofl_free = ofl_free->next; + if (ofl_free) { + ofl_free->prev = NULL; + } + f->next = NULL; + f->prev = NULL; +#endif + + UNLOCK(ofl_lock); + return f; +} + +void __ofl_free(FILE *f) +{ + LOCK(ofl_lock); + if (!f) { +#ifdef LINUX_MUSL_SUPPORTED + UNLOCK(ofl_lock); +#endif + return; + } + +#ifdef LINUX_MUSL_SUPPORTED + /* remove from head list */ + FILE_LIST_REMOVE(f); + + /* push to free list */ + FILE_LIST_INSERT_HEAD(ofl_free, f); +#else + /* remove from head list */ + if (f->prev) { + f->prev->next = f->next; + } + if (f->next) { + f->next->prev = f->prev; + } + if (f == ofl_head) { + ofl_head = f->next; + } + + /* push to free list */ + f->next = ofl_free; + if (ofl_free) { + ofl_free->prev = f; + } + ofl_free = f; +#endif + + UNLOCK(ofl_lock); +} diff --git a/src/stdio/ofl_add.c b/src/stdio/ofl_add.c index d7de9f15a..8f3fe33df 100644 --- a/src/stdio/ofl_add.c +++ b/src/stdio/ofl_add.c @@ -3,9 +3,13 @@ FILE *__ofl_add(FILE *f) { FILE **head = __ofl_lock(); +#ifdef LINUX_MUSL_SUPPORTED + FILE_LIST_INSERT_HEAD(*head, f); +#else f->next = *head; if (*head) (*head)->prev = f; *head = f; +#endif __ofl_unlock(); return f; } diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c index ed1b561d9..86e88ad09 100644 --- a/src/stdio/open_wmemstream.c +++ b/src/stdio/open_wmemstream.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "libc.h" struct cookie { @@ -70,6 +71,7 @@ FILE *open_wmemstream(wchar_t **bufp, size_t *sizep) struct wms_FILE *f; wchar_t *buf; + UNSUPPORTED_API_VOID(LITEOS_A); if (!(f=malloc(sizeof *f))) return 0; if (!(buf=malloc(sizeof *buf))) { free(f); diff --git a/src/stdio/popen.c b/src/stdio/popen.c index 3ec833941..ea21d0905 100644 --- a/src/stdio/popen.c +++ b/src/stdio/popen.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "stdio_impl.h" #include "syscall.h" @@ -15,6 +16,7 @@ FILE *popen(const char *cmd, const char *mode) FILE *f; posix_spawn_file_actions_t fa; + UNSUPPORTED_API_VOID(LITEOS_A); if (*mode == 'r') { op = 0; } else if (*mode == 'w') { diff --git a/src/stdio/setvbuf.c b/src/stdio/setvbuf.c index 523dddc8b..2fcd41db3 100644 --- a/src/stdio/setvbuf.c +++ b/src/stdio/setvbuf.c @@ -12,6 +12,7 @@ int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size) if (type == _IONBF) { f->buf_size = 0; + f->flags |= F_NOBUF; } else if (type == _IOLBF || type == _IOFBF) { if (buf && size >= UNGET) { f->buf = (void *)(buf + UNGET); @@ -19,6 +20,7 @@ int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size) } if (type == _IOLBF && f->buf_size) f->lbf = '\n'; + f->flags &= ~F_NOBUF; } else { return -1; } diff --git a/src/stdio/stderr.c b/src/stdio/stderr.c index f2bc4648d..4dd73a5bb 100644 --- a/src/stdio/stderr.c +++ b/src/stdio/stderr.c @@ -7,7 +7,7 @@ hidden FILE __stderr_FILE = { .buf = buf+UNGET, .buf_size = 0, .fd = 2, - .flags = F_PERM | F_NORD, + .flags = F_PERM | F_NORD | F_NOBUF, .lbf = -1, .write = __stdio_write, .seek = __stdio_seek, diff --git a/src/stdio/stdin.c b/src/stdio/stdin.c index 5aa5262c2..ca7b27c33 100644 --- a/src/stdio/stdin.c +++ b/src/stdio/stdin.c @@ -9,6 +9,7 @@ hidden FILE __stdin_FILE = { .fd = 0, .flags = F_PERM | F_NOWR, .read = __stdio_read, + .readx = __stdio_readx, .seek = __stdio_seek, .close = __stdio_close, .lock = -1, diff --git a/src/stdio/tmpfile.c b/src/stdio/tmpfile.c index ae493987b..b67cb11be 100644 --- a/src/stdio/tmpfile.c +++ b/src/stdio/tmpfile.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "stdio_impl.h" #define MAXTRIES 100 @@ -11,6 +12,7 @@ FILE *tmpfile(void) int fd; FILE *f; int try; + UNSUPPORTED_API_VOID(LITEOS_A); for (try=0; try #include #include +#include #include "syscall.h" #include "kstat.h" @@ -15,6 +16,7 @@ char *tmpnam(char *buf) char s[] = "/tmp/tmpnam_XXXXXX"; int try; int r; + UNSUPPORTED_API_VOID(LITEOS_A); for (try=0; tryflags & F_ERR)) __fwritex((void *)s, l, f); + if (!l) return; + + /* write to file buffer if flag F_PBUF is available */ + if (!(f->flags & F_ERR) && !(f->flags & F_PBUF)) { + __fwritex((void *)s, l, f); + return; + } + + /* otherwise, copy to buffer directly */ + f->write(f, (void *)s, l); } static void pad(FILE *f, char c, int w, int l, int fl) { - char pad[256]; + char pad[16]; if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return; l = w - l; - memset(pad, c, l>sizeof pad ? sizeof pad : l); + __builtin_memset(pad, c, sizeof pad); for (; l >= sizeof pad; l -= sizeof pad) out(f, pad, sizeof pad); out(f, pad, l); @@ -427,7 +436,7 @@ static int getint(char **s) { return i; } -static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type) +static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type, char nl_arg_filled) { char *a, *z, *s=(char *)fmt; unsigned l10n=0, fl; @@ -437,7 +446,6 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, unsigned st, ps; int cnt=0, l=0; size_t i; - char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4]; const char *prefix; int t, pl; wchar_t wc[2], *ws; @@ -462,6 +470,14 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (l) continue; if (isdigit(s[1]) && s[2]=='$') { + if (!nl_arg_filled) { + va_list ap_copy; + va_copy(ap_copy, *ap); + if (printf_core(0, fmt, &ap_copy, nl_arg, nl_type, 1) < 0) { + return -1; + } + va_end(ap_copy); + } l10n=1; argpos = s[1]-'0'; s+=3; @@ -528,6 +544,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (!f) continue; + char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4]; z = buf + sizeof(buf); prefix = "-+ 0X0x"; pl = 0; @@ -665,28 +682,35 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) /* the copy allows passing va_list* even if va_list is an array */ va_copy(ap2, ap); - if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) { - va_end(ap2); - return -1; - } FLOCK(f); olderr = f->flags & F_ERR; if (f->mode < 1) f->flags &= ~F_ERR; - if (!f->buf_size) { + + if (!f->buf_size && f->buf != NULL) { saved_buf = f->buf; f->buf = internal_buf; f->buf_size = sizeof internal_buf; f->wpos = f->wbase = f->wend = 0; } if (!f->wend && __towrite(f)) ret = -1; - else ret = printf_core(f, fmt, &ap2, nl_arg, nl_type); + else { + ret = printf_core(f, fmt, &ap2, nl_arg, nl_type, 0); + } if (saved_buf) { - f->write(f, 0, 0); + if (!(f->flags & F_PBUF)) { + f->write(f, 0, 0); + } else { + *saved_buf = '\0'; + } if (!f->wpos) ret = -1; f->buf = saved_buf; f->buf_size = 0; f->wpos = f->wbase = f->wend = 0; + } else { + if (f->flags & F_PBUF) { + *f->wpos = '\0'; + } } if (f->flags & F_ERR) ret = -1; f->flags |= olderr; diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c index b78a374d3..67b48eea4 100644 --- a/src/stdio/vfscanf.c +++ b/src/stdio/vfscanf.c @@ -18,6 +18,11 @@ #define SIZE_l 1 #define SIZE_L 2 #define SIZE_ll 3 +#define BUF_LEN 513 + +#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH)) +extern int parsefloat(FILE *f, char *buf, char *end); +#endif static void store_int(void *dest, int size, unsigned long long i) { @@ -55,6 +60,10 @@ static void *arg_n(va_list ap, unsigned int n) int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap) { +#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH)) + char buf[BUF_LEN]; + char *endptr; +#endif int width; int size; int alloc = 0; @@ -302,6 +311,29 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap) case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': +#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH)) + if (width == 0 || width > sizeof(buf) - 1) + width = sizeof(buf) - 1; + int count = parsefloat(f, buf, buf + width); + if (count == 0) + goto match_fail; + if (dest) switch (size) { + case SIZE_def: + y = strtof(buf, &endptr); + *(float *)dest = y; + break; + case SIZE_l: + y = strtod(buf, &endptr); + *(double *)dest = y; + break; + case SIZE_L: + y = strtold(buf, &endptr); + *(long double *)dest = y; + break; + } + break; + } +#else y = __floatscan(f, size, 0); if (!shcnt(f)) goto match_fail; if (dest) switch (size) { @@ -317,7 +349,7 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap) } break; } - +#endif pos += shcnt(f); if (dest) matches++; } diff --git a/src/stdio/vsnprintf.c b/src/stdio/vsnprintf.c index b3510a634..0e7d7f365 100644 --- a/src/stdio/vsnprintf.c +++ b/src/stdio/vsnprintf.c @@ -14,35 +14,28 @@ struct cookie { static size_t sn_write(FILE *f, const unsigned char *s, size_t l) { struct cookie *c = f->cookie; - size_t k = MIN(c->n, f->wpos - f->wbase); - if (k) { - memcpy(c->s, f->wbase, k); - c->s += k; - c->n -= k; + size_t already_size = f->wpos - f->wbase; + if (already_size <= c->n) { + size_t k = MIN(l, c->n - already_size); + memcpy(f->wpos, s, k); + f->wpos += k; } - k = MIN(c->n, l); - if (k) { - memcpy(c->s, s, k); - c->s += k; - c->n -= k; - } - *c->s = 0; - f->wpos = f->wbase = f->buf; /* pretend to succeed, even if we discarded extra data */ return l; } int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list ap) { - unsigned char buf[1]; - char dummy[1]; + unsigned char dummy[1]; struct cookie c = { .s = n ? s : dummy, .n = n ? n-1 : 0 }; FILE f = { .lbf = EOF, - .write = sn_write, .lock = -1, - .buf = buf, + .buf = (unsigned char *)(n ? s: dummy), + .buf_size = n ? n - 1 : 0, + .flags = F_PBUF, .cookie = &c, + .write = sn_write, }; if (n > INT_MAX) { @@ -50,6 +43,5 @@ int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list ap) return -1; } - *c.s = 0; return vfprintf(&f, fmt, ap); } diff --git a/src/stdio/vsscanf.c b/src/stdio/vsscanf.c index 4d6d259b8..f0c4a7d01 100644 --- a/src/stdio/vsscanf.c +++ b/src/stdio/vsscanf.c @@ -1,25 +1,19 @@ #include "stdio_impl.h" #include +// Empty implementation because vfscanf operates rpos directly. static size_t string_read(FILE *f, unsigned char *buf, size_t len) { - char *src = f->cookie; - size_t k = len+256; - char *end = memchr(src, 0, k); - if (end) k = end-src; - if (k < len) len = k; - memcpy(buf, src, len); - f->rpos = (void *)(src+len); - f->rend = (void *)(src+k); - f->cookie = src+k; - return len; + return 0; } int vsscanf(const char *restrict s, const char *restrict fmt, va_list ap) { + size_t s_len = strlen(s); FILE f = { .buf = (void *)s, .cookie = (void *)s, - .read = string_read, .lock = -1 + .read = string_read, .lock = -1, + .buf_size = s_len, .rpos = s, .rend = s + s_len, }; return vfscanf(&f, fmt, ap); } diff --git a/porting/liteos_a/user/src/stdlib/qsort.c b/src/stdlib/liteos_a/qsort.c similarity index 100% rename from porting/liteos_a/user/src/stdlib/qsort.c rename to src/stdlib/liteos_a/qsort.c diff --git a/src/stdlib/strtod.c b/src/stdlib/strtod.c index 39b9daada..4bf2e1553 100644 --- a/src/stdlib/strtod.c +++ b/src/stdlib/strtod.c @@ -14,6 +14,7 @@ static long double strtox(const char *s, char **p, int prec) return y; } +#if !defined(LINUX_MUSL_SUPPORTED) || (!defined(MUSL_AARCH64_ARCH)) && (!defined(MUSL_ARM_ARCH)) float strtof(const char *restrict s, char **restrict p) { return strtox(s, p, 0); @@ -23,6 +24,7 @@ double strtod(const char *restrict s, char **restrict p) { return strtox(s, p, 1); } +#endif long double strtold(const char *restrict s, char **restrict p) { diff --git a/src/stdlib/strtol.c b/src/stdlib/strtol.c index bfefea69d..a43d6b88b 100644 --- a/src/stdlib/strtol.c +++ b/src/stdlib/strtol.c @@ -28,7 +28,11 @@ long long strtoll(const char *restrict s, char **restrict p, int base) return strtox(s, p, base, LLONG_MIN); } +#ifdef LINUX_MUSL_SUPPORTED +unsigned long strtoul_weak(const char *restrict s, char **restrict p, int base) +#else unsigned long strtoul(const char *restrict s, char **restrict p, int base) +#endif { return strtox(s, p, base, ULONG_MAX); } @@ -38,19 +42,34 @@ long strtol(const char *restrict s, char **restrict p, int base) return strtox(s, p, base, 0UL+LONG_MIN); } +#ifdef LINUX_MUSL_SUPPORTED +intmax_t strtoimax_weak(const char *restrict s, char **restrict p, int base) +#else intmax_t strtoimax(const char *restrict s, char **restrict p, int base) +#endif { return strtoll(s, p, base); } +#ifdef LINUX_MUSL_SUPPORTED +uintmax_t strtoumax_weak(const char *restrict s, char **restrict p, int base) +#else uintmax_t strtoumax(const char *restrict s, char **restrict p, int base) +#endif { return strtoull(s, p, base); } weak_alias(strtol, __strtol_internal); +#ifdef LINUX_MUSL_SUPPORTED +weak_alias(strtoul_weak, strtoul); +#endif weak_alias(strtoul, __strtoul_internal); weak_alias(strtoll, __strtoll_internal); weak_alias(strtoull, __strtoull_internal); +#ifdef LINUX_MUSL_SUPPORTED +weak_alias(strtoimax_weak, strtoimax); +weak_alias(strtoumax_weak, strtoumax); +#endif weak_alias(strtoimax, __strtoimax_internal); weak_alias(strtoumax, __strtoumax_internal); diff --git a/porting/liteos_a/user/src/string/arm/memcmp.S b/src/string/arm/liteos_a/memcmp.S similarity index 100% rename from porting/liteos_a/user/src/string/arm/memcmp.S rename to src/string/arm/liteos_a/memcmp.S diff --git a/porting/liteos_a/user/src/string/arm/memset.S b/src/string/arm/memset.S similarity index 100% rename from porting/liteos_a/user/src/string/arm/memset.S rename to src/string/arm/memset.S diff --git a/src/string/memcmp.c b/src/string/memcmp.c index bdbce9f0f..1d4fe7b25 100644 --- a/src/string/memcmp.c +++ b/src/string/memcmp.c @@ -1,5 +1,11 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int memcmp(const void *vl, const void *vr, size_t n) { const unsigned char *l=vl, *r=vr; diff --git a/src/string/memcpy.c b/src/string/memcpy.c index 06e88742b..cee76b388 100644 --- a/src/string/memcpy.c +++ b/src/string/memcpy.c @@ -1,7 +1,13 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include #include #include +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif void *memcpy(void *restrict dest, const void *restrict src, size_t n) { unsigned char *d = dest; diff --git a/src/string/memset.c b/src/string/memset.c index 5613a1486..59f66d7e2 100644 --- a/src/string/memset.c +++ b/src/string/memset.c @@ -1,6 +1,12 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include #include +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif void *memset(void *dest, int c, size_t n) { unsigned char *s = dest; diff --git a/src/string/stpncpy.c b/src/string/stpncpy.c index f57fa6b71..6fbbc7ad7 100644 --- a/src/string/stpncpy.c +++ b/src/string/stpncpy.c @@ -1,32 +1,20 @@ #include -#include -#include -#define ALIGN (sizeof(size_t)-1) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) - -char *__stpncpy(char *restrict d, const char *restrict s, size_t n) +char *__stpncpy(char *dst, const char *src, size_t n) { -#ifdef __GNUC__ - typedef size_t __attribute__((__may_alias__)) word; - word *wd; - const word *ws; - if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { - for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); - if (!n || !*s) goto tail; - wd=(void *)d; ws=(const void *)s; - for (; n>=sizeof(size_t) && !HASZERO(*ws); - n-=sizeof(size_t), ws++, wd++) *wd = *ws; - d=(void *)wd; s=(const void *)ws; + char *d = dst, *s = src; + dst = &dst[n]; + while (n > 0){ + if((*d++ = *src++) == 0){ + dst = d - 1; + while(--n > 0){ + *d++ = 0; + } + break; + } + n--; } -#endif - for (; n && (*d=*s); n--, s++, d++); -tail: - memset(d, 0, n); - return d; + return (dst); } weak_alias(__stpncpy, stpncpy); - diff --git a/src/string/strcasecmp.c b/src/string/strcasecmp.c index 002c6aa15..7fa71db6f 100644 --- a/src/string/strcasecmp.c +++ b/src/string/strcasecmp.c @@ -1,12 +1,13 @@ #include #include -int strcasecmp(const char *_l, const char *_r) +int __strcasecmp(const char *_l, const char *_r) { const unsigned char *l=(void *)_l, *r=(void *)_r; for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++); return tolower(*l) - tolower(*r); } +weak_alias(__strcasecmp, strcasecmp); int __strcasecmp_l(const char *l, const char *r, locale_t loc) { diff --git a/src/string/strcmp.c b/src/string/strcmp.c index 808bd8370..9f27c79fc 100644 --- a/src/string/strcmp.c +++ b/src/string/strcmp.c @@ -1,5 +1,11 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int strcmp(const char *l, const char *r) { for (; *l==*r && *l; l++, r++); diff --git a/src/string/strcspn.c b/src/string/strcspn.c index a0c617bd7..aeee7c9c1 100644 --- a/src/string/strcspn.c +++ b/src/string/strcspn.c @@ -1,17 +1,18 @@ #include -#define BITOP(a,b,op) \ - ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) - -size_t strcspn(const char *s, const char *c) +size_t strcspn(const char *string1, const char *string2) { - const char *a = s; - size_t byteset[32/sizeof(size_t)]; - - if (!c[0] || !c[1]) return __strchrnul(s, *c)-a; - - memset(byteset, 0, sizeof byteset); - for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); - for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++); - return s-a; +const char *a, *p; +char sc, c; + +for (a = string1;;) { +sc = *a++; +p = string2; +do { +if ((c = *p++) == sc) { +return (a - 1 - string1); + } +} while (c != 0); +} +/* NOTREACHED */ } diff --git a/src/string/strlen.c b/src/string/strlen.c index 309990f02..be5b6aff3 100644 --- a/src/string/strlen.c +++ b/src/string/strlen.c @@ -1,3 +1,6 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include #include #include @@ -7,6 +10,9 @@ #define HIGHS (ONES * (UCHAR_MAX/2+1)) #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif size_t strlen(const char *s) { const char *a = s; diff --git a/src/string/strncasecmp.c b/src/string/strncasecmp.c index e0ef93c20..fb298d652 100644 --- a/src/string/strncasecmp.c +++ b/src/string/strncasecmp.c @@ -1,13 +1,14 @@ #include #include -int strncasecmp(const char *_l, const char *_r, size_t n) +int __strncasecmp(const char *_l, const char *_r, size_t n) { const unsigned char *l=(void *)_l, *r=(void *)_r; if (!n--) return 0; for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--); return tolower(*l) - tolower(*r); } +weak_alias(__strncasecmp, strncasecmp); int __strncasecmp_l(const char *l, const char *r, size_t n, locale_t loc) { diff --git a/src/string/strncat.c b/src/string/strncat.c index 01ca2a231..01fc0a022 100644 --- a/src/string/strncat.c +++ b/src/string/strncat.c @@ -3,7 +3,7 @@ char *strncat(char *restrict d, const char *restrict s, size_t n) { char *a = d; - d += strlen(d); + while (*d != 0) d++; while (n && *s) n--, *d++ = *s++; *d++ = 0; return a; diff --git a/src/string/strncpy.c b/src/string/strncpy.c index 545892e6e..356e1e621 100644 --- a/src/string/strncpy.c +++ b/src/string/strncpy.c @@ -1,7 +1,14 @@ #include -char *strncpy(char *restrict d, const char *restrict s, size_t n) +char *strncpy(char *dst, const char *src, size_t n) { - __stpncpy(d, s, n); - return d; + char *d = dst, *s = src; + while (n > 0) { + if((*d = *s) != 0) { + s++; + } + d++; + n--; + } + return (dst); } diff --git a/src/termios/cfsetospeed.c b/src/termios/cfsetospeed.c index c9cbdd9d9..3a2979788 100644 --- a/src/termios/cfsetospeed.c +++ b/src/termios/cfsetospeed.c @@ -2,9 +2,11 @@ #include #include #include +#include int cfsetospeed(struct termios *tio, speed_t speed) { + UNSUPPORTED_API_VOID(LITEOS_A); if (speed & ~CBAUD) { errno = EINVAL; return -1; @@ -16,6 +18,7 @@ int cfsetospeed(struct termios *tio, speed_t speed) int cfsetispeed(struct termios *tio, speed_t speed) { + UNSUPPORTED_API_VOID(LITEOS_A); return speed ? cfsetospeed(tio, speed) : 0; } diff --git a/src/thread/__lock.c b/src/thread/__lock.c index 60eece49a..00ab65205 100644 --- a/src/thread/__lock.c +++ b/src/thread/__lock.c @@ -1,3 +1,6 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" /* This lock primitive combines a flag (in the sign bit) and a @@ -15,14 +18,18 @@ * or in an equivalent formulation x is the congestion count or'ed * with INT_MIN as a lock flag. */ - +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif void __lock(volatile int *l) { +#ifndef __LITEOS_M__ int need_locks = libc.need_locks; if (!need_locks) return; + if (need_locks < 0) libc.need_locks = 0; +#endif /* fast path: INT_MIN for the lock, +1 for the congestion */ int current = a_cas(l, 0, INT_MIN + 1); - if (need_locks < 0) libc.need_locks = 0; if (!current) return; /* A first spin loop, for medium congestion. */ for (unsigned i = 0; i < 10; ++i) { @@ -51,6 +58,9 @@ void __lock(volatile int *l) } } +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif void __unlock(volatile int *l) { /* Check l[0] to see if we are multi-threaded. */ diff --git a/src/thread/__timedwait.c b/src/thread/__timedwait.c index 666093be9..a9725b4e6 100644 --- a/src/thread/__timedwait.c +++ b/src/thread/__timedwait.c @@ -11,6 +11,19 @@ static int __futex4_cp(volatile void *addr, int op, int val, const struct timespec *to) { int r; +#ifdef __LITEOS_A__ + unsigned int useconds = 0xffffffffU; + if (to) { + useconds = (to->tv_sec * 1000000 + to->tv_nsec / 1000); + if ((useconds == 0) && (to->tv_nsec != 0)) { + useconds = 1; + } + } + + r = __syscall_cp(SYS_futex, addr, op, val, useconds); + if (r != -ENOSYS) return r; + return __syscall_cp(SYS_futex, addr, op & ~FUTEX_PRIVATE, val, useconds); +#else #ifdef SYS_futex_time64 time_t s = to ? to->tv_sec : 0; long ns = to ? to->tv_nsec : 0; @@ -24,6 +37,7 @@ static int __futex4_cp(volatile void *addr, int op, int val, const struct timesp r = __syscall_cp(SYS_futex, addr, op, val, to); if (r != -ENOSYS) return r; return __syscall_cp(SYS_futex, addr, op & ~FUTEX_PRIVATE, val, to); +#endif } static volatile int dummy = 0; diff --git a/src/thread/__wait.c b/src/thread/__wait.c index dc33c1a30..01b7cbb32 100644 --- a/src/thread/__wait.c +++ b/src/thread/__wait.c @@ -10,8 +10,13 @@ void __wait(volatile int *addr, volatile int *waiters, int val, int priv) } if (waiters) a_inc(waiters); while (*addr==val) { +#ifdef __LITEOS_A__ + __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0xffffffffu) != -ENOSYS + || __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0xffffffffu); +#else __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS || __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0); +#endif } if (waiters) a_dec(waiters); } diff --git a/src/thread/arm/__set_thread_area.c b/src/thread/arm/__set_thread_area.c index 09de65aab..0aaaa0858 100644 --- a/src/thread/arm/__set_thread_area.c +++ b/src/thread/arm/__set_thread_area.c @@ -26,6 +26,9 @@ extern hidden uintptr_t __a_barrier_ptr, __a_cas_ptr, __a_gettp_ptr; int __set_thread_area(void *p) { +#if defined(SYS_set_thread_area) && defined(__LITEOS_A__) + return __syscall(SYS_set_thread_area, p); +#else #if !__ARM_ARCH_7A__ && !__ARM_ARCH_7R__ && __ARM_ARCH < 7 if (__hwcap & HWCAP_TLS) { size_t *aux; @@ -49,4 +52,5 @@ int __set_thread_area(void *p) } #endif return __syscall(0xf0005, p); +#endif } diff --git a/src/thread/arm/clone.s b/src/thread/arm/clone.s index bb0965daf..79f8610f4 100644 --- a/src/thread/arm/clone.s +++ b/src/thread/arm/clone.s @@ -3,8 +3,18 @@ .global __clone .hidden __clone .type __clone,%function +.ifdef LINUX_MUSL_SUPPORTED +.cfi_startproc +.endif __clone: stmfd sp!,{r4,r5,r6,r7} + .ifdef LINUX_MUSL_SUPPORTED + .cfi_def_cfa_offset 16 + .cfi_rel_offset r4, 0 + .cfi_rel_offset r5, 4 + .cfi_rel_offset r6, 8 + .cfi_rel_offset r7, 12 + .endif mov r7,#120 mov r6,r3 mov r5,r0 @@ -17,6 +27,9 @@ __clone: tst r0,r0 beq 1f ldmfd sp!,{r4,r5,r6,r7} + .ifdef LINUX_MUSL_SUPPORTED + .cfi_def_cfa_offset 0 + .endif bx lr 1: mov r0,r6 @@ -26,3 +39,6 @@ __clone: b 2b 3: bx r5 +.ifdef LINUX_MUSL_SUPPORTED +.cfi_endproc +.endif diff --git a/src/thread/arm/syscall_cp.s b/src/thread/arm/syscall_cp.s index e607dd426..0b9dd7ecf 100644 --- a/src/thread/arm/syscall_cp.s +++ b/src/thread/arm/syscall_cp.s @@ -7,11 +7,23 @@ .hidden __cp_cancel .hidden __cancel .global __syscall_cp_asm +.ifndef LINUX_MUSL_SUPPORTED .hidden __syscall_cp_asm +.endif .type __syscall_cp_asm,%function +.ifdef LINUX_MUSL_SUPPORTED +.cfi_startproc +.endif __syscall_cp_asm: mov ip,sp stmfd sp!,{r4,r5,r6,r7} + .ifdef LINUX_MUSL_SUPPORTED + .cfi_def_cfa_offset 16 + .cfi_rel_offset r4, 0 + .cfi_rel_offset r5, 4 + .cfi_rel_offset r6, 8 + .cfi_rel_offset r7, 12 + .endif __cp_begin: ldr r0,[r0] cmp r0,#0 @@ -23,7 +35,16 @@ __cp_begin: svc 0 __cp_end: ldmfd sp!,{r4,r5,r6,r7} + .ifdef LINUX_MUSL_SUPPORTED + .cfi_def_cfa_offset 0 + .endif bx lr __cp_cancel: ldmfd sp!,{r4,r5,r6,r7} + .ifdef LINUX_MUSL_SUPPORTED + .cfi_def_cfa_offset 0 + .endif b __cancel +.ifdef LINUX_MUSL_SUPPORTED +.cfi_endproc +.endif diff --git a/porting/liteos_a/user/src/thread/pthread_clone.c b/src/thread/liteos_a/pthread_clone.c similarity index 100% rename from porting/liteos_a/user/src/thread/pthread_clone.c rename to src/thread/liteos_a/pthread_clone.c diff --git a/porting/liteos_m/user/src/thread/pthread_create.c b/src/thread/liteos_m/pthread_create.c similarity index 100% rename from porting/liteos_m/user/src/thread/pthread_create.c rename to src/thread/liteos_m/pthread_create.c diff --git a/src/thread/pthread_attr_destroy.c b/src/thread/pthread_attr_destroy.c index b5845dd0f..e5dcc11b7 100644 --- a/src/thread/pthread_attr_destroy.c +++ b/src/thread/pthread_attr_destroy.c @@ -1,5 +1,11 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_destroy(pthread_attr_t *a) { return 0; diff --git a/src/thread/pthread_attr_get.c b/src/thread/pthread_attr_get.c index f12ff4425..5f22a7a1c 100644 --- a/src/thread/pthread_attr_get.c +++ b/src/thread/pthread_attr_get.c @@ -1,40 +1,74 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#include +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_getdetachstate(const pthread_attr_t *a, int *state) { *state = a->_a_detach; return 0; } + +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size) +{ + return ENOSYS; +} +#else int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size) { *size = a->_a_guardsize; return 0; } +#endif +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_getinheritsched(const pthread_attr_t *restrict a, int *restrict inherit) { *inherit = a->_a_sched; return 0; } +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param) { param->sched_priority = a->_a_prio; return 0; } +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_getschedpolicy(const pthread_attr_t *restrict a, int *restrict policy) { *policy = a->_a_policy; return 0; } +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_getscope(const pthread_attr_t *restrict a, int *restrict scope) { +#ifdef __LITEOS_A__ + *scope = PTHREAD_SCOPE_PROCESS; +#else *scope = PTHREAD_SCOPE_SYSTEM; +#endif return 0; } +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size) { if (!a->_a_stackaddr) @@ -44,12 +78,16 @@ int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr return 0; } +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size) { *size = a->_a_stacksize; return 0; } +#ifndef __LITEOS_M__ int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict a, int *restrict pshared) { *pshared = !!a->__attr; @@ -70,6 +108,7 @@ int pthread_condattr_getpshared(const pthread_condattr_t *restrict a, int *restr int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict a, int *restrict protocol) { + UNSUPPORTED_API_VOID(LITEOS_A); *protocol = a->__attr / 8U % 2; return 0; } @@ -96,3 +135,4 @@ int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict a, int *r *pshared = a->__attr[0]; return 0; } +#endif diff --git a/src/thread/pthread_attr_init.c b/src/thread/pthread_attr_init.c index 463a8d207..5e3a51cb9 100644 --- a/src/thread/pthread_attr_init.c +++ b/src/thread/pthread_attr_init.c @@ -1,11 +1,27 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_init(pthread_attr_t *a) { *a = (pthread_attr_t){0}; +#ifdef __LITEOS_M__ + a->_a_stacksize = 0x2000; +#else __acquire_ptc(); a->_a_stacksize = __default_stacksize; a->_a_guardsize = __default_guardsize; +#endif +#if defined(__LITEOS_A__) || defined(__LITEOS_M__) + a->_a_policy = SCHED_RR; + a->_a_prio = 25; +#endif +#ifndef __LITEOS_M__ __release_ptc(); +#endif return 0; } diff --git a/src/thread/pthread_attr_setdetachstate.c b/src/thread/pthread_attr_setdetachstate.c index 1b7127839..7a926989b 100644 --- a/src/thread/pthread_attr_setdetachstate.c +++ b/src/thread/pthread_attr_setdetachstate.c @@ -1,5 +1,11 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_setdetachstate(pthread_attr_t *a, int state) { if (state > 1U) return EINVAL; diff --git a/src/thread/pthread_attr_setguardsize.c b/src/thread/pthread_attr_setguardsize.c index 1c5c60acb..b4681b9d0 100644 --- a/src/thread/pthread_attr_setguardsize.c +++ b/src/thread/pthread_attr_setguardsize.c @@ -1,8 +1,18 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION int pthread_attr_setguardsize(pthread_attr_t *a, size_t size) +{ + return ENOSYS; +} +#else int pthread_attr_setguardsize(pthread_attr_t *a, size_t size) { if (size > SIZE_MAX/8) return EINVAL; a->_a_guardsize = size; return 0; } +#endif diff --git a/src/thread/pthread_attr_setinheritsched.c b/src/thread/pthread_attr_setinheritsched.c index ca264be7c..34878d736 100644 --- a/src/thread/pthread_attr_setinheritsched.c +++ b/src/thread/pthread_attr_setinheritsched.c @@ -1,6 +1,12 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" #include "syscall.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_setinheritsched(pthread_attr_t *a, int inherit) { if (inherit > 1U) return EINVAL; diff --git a/src/thread/pthread_attr_setschedparam.c b/src/thread/pthread_attr_setschedparam.c index d4c1204fd..fc1ef0105 100644 --- a/src/thread/pthread_attr_setschedparam.c +++ b/src/thread/pthread_attr_setschedparam.c @@ -1,7 +1,25 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_setschedparam(pthread_attr_t *restrict a, const struct sched_param *restrict param) { +#ifdef __LITEOS_M__ + if (param->sched_priority < 0 || param->sched_priority > PTHREAD_PRIORITY_LOWEST) { + return EINVAL; + } +#elif defined(__LITEOS_A__) + if ((a->_a_policy == SCHED_RR || a->_a_policy == SCHED_FIFO) && + (param->sched_priority < 0 || param->sched_priority > PTHREAD_PRIORITY_LOWEST)) { + return EINVAL; + } + a->_a_deadline = param->sched_deadline; + a->_a_period = param->sched_period; +#endif a->_a_prio = param->sched_priority; return 0; } diff --git a/src/thread/pthread_attr_setschedpolicy.c b/src/thread/pthread_attr_setschedpolicy.c index bb71f393e..fa0732f27 100644 --- a/src/thread/pthread_attr_setschedpolicy.c +++ b/src/thread/pthread_attr_setschedpolicy.c @@ -1,7 +1,22 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_setschedpolicy(pthread_attr_t *a, int policy) { +#ifdef __LITEOS_M__ + if (policy != SCHED_RR && policy != SCHED_FIFO) { + return EINVAL; + } +#elif defined(__LITEOS_A__) + if (policy != SCHED_RR && policy != SCHED_FIFO && policy != SCHED_DEADLINE) { + return EINVAL; + } +#endif a->_a_policy = policy; return 0; } diff --git a/src/thread/pthread_attr_setscope.c b/src/thread/pthread_attr_setscope.c index 46b520c04..b3b00cd28 100644 --- a/src/thread/pthread_attr_setscope.c +++ b/src/thread/pthread_attr_setscope.c @@ -1,12 +1,25 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_setscope(pthread_attr_t *a, int scope) { switch (scope) { +#if defined(__LITEOS_A__) || defined(__LITEOS_M__) + case PTHREAD_SCOPE_SYSTEM: + return ENOTSUP; + case PTHREAD_SCOPE_PROCESS: + return 0; +#else case PTHREAD_SCOPE_SYSTEM: return 0; case PTHREAD_SCOPE_PROCESS: return ENOTSUP; +#endif default: return EINVAL; } diff --git a/src/thread/pthread_attr_setstack.c b/src/thread/pthread_attr_setstack.c index 1eddcbd6e..cfad288e3 100644 --- a/src/thread/pthread_attr_setstack.c +++ b/src/thread/pthread_attr_setstack.c @@ -1,9 +1,17 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_setstack(pthread_attr_t *a, void *addr, size_t size) { if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL; a->_a_stackaddr = (size_t)addr + size; a->_a_stacksize = size; +#ifndef __LITEOS_M__ return 0; +#endif } diff --git a/src/thread/pthread_attr_setstacksize.c b/src/thread/pthread_attr_setstacksize.c index 9c6a8806e..7b8f5b1f4 100644 --- a/src/thread/pthread_attr_setstacksize.c +++ b/src/thread/pthread_attr_setstacksize.c @@ -1,5 +1,11 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_attr_setstacksize(pthread_attr_t *a, size_t size) { if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL; diff --git a/src/thread/pthread_barrier_wait.c b/src/thread/pthread_barrier_wait.c index cc2a8bbf5..023a8ae9f 100644 --- a/src/thread/pthread_barrier_wait.c +++ b/src/thread/pthread_barrier_wait.c @@ -84,8 +84,13 @@ int pthread_barrier_wait(pthread_barrier_t *b) a_spin(); a_inc(&inst->finished); while (inst->finished == 1) +#ifdef __LITEOS_A__ + __syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0xffffffffu) != -ENOSYS + || __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0xffffffffu); +#else __syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0) != -ENOSYS || __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0); +#endif return PTHREAD_BARRIER_SERIAL_THREAD; } diff --git a/src/thread/pthread_cancel.c b/src/thread/pthread_cancel.c index 2f9d5e975..94f02742a 100644 --- a/src/thread/pthread_cancel.c +++ b/src/thread/pthread_cancel.c @@ -1,8 +1,18 @@ #define _GNU_SOURCE +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include #include "pthread_impl.h" #include "syscall.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION int pthread_cancel(pthread_t t) +{ + return ENOSYS; +} +#else +#ifdef FEATURE_PTHREAD_CANCEL hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c(); long __cancel() @@ -48,12 +58,19 @@ extern hidden const char __cp_begin[1], __cp_end[1], __cp_cancel[1]; static void cancel_handler(int sig, siginfo_t *si, void *ctx) { pthread_t self = __pthread_self(); +#ifndef __LITEOS_A__ ucontext_t *uc = ctx; uintptr_t pc = uc->uc_mcontext.MC_PC; +#endif a_barrier(); if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return; +#ifdef __LITEOS_A__ + if (self->cancelasync) { + pthread_exit(PTHREAD_CANCELED); + } +#else _sigaddset(&uc->uc_sigmask, SIGCANCEL); if (self->cancelasync || pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) { @@ -63,8 +80,8 @@ static void cancel_handler(int sig, siginfo_t *si, void *ctx) #endif return; } - __syscall(SYS_tkill, self->tid, SIGCANCEL); +#endif } void __testcancel() @@ -99,3 +116,13 @@ int pthread_cancel(pthread_t t) } return pthread_kill(t, SIGCANCEL); } +#else +hidden long __cancel() +{ +} + +int pthread_cancel(pthread_t t) +{ +} +#endif +#endif diff --git a/porting/linux/user/src/thread/pthread_cond_clockwait.c b/src/thread/pthread_cond_clockwait.c similarity index 100% rename from porting/linux/user/src/thread/pthread_cond_clockwait.c rename to src/thread/pthread_cond_clockwait.c diff --git a/src/thread/pthread_cond_init.c b/src/thread/pthread_cond_init.c index 8c484ddcd..bd2fc6a14 100644 --- a/src/thread/pthread_cond_init.c +++ b/src/thread/pthread_cond_init.c @@ -2,7 +2,7 @@ int pthread_cond_init(pthread_cond_t *restrict c, const pthread_condattr_t *restrict a) { - *c = (pthread_cond_t){0}; + __builtin_memset(c, 0, sizeof(pthread_cond_t)); if (a) { c->_c_clock = a->__attr & 0x7fffffff; if (a->__attr>>31) c->_c_shared = (void *)-1; diff --git a/porting/linux/user/src/thread/pthread_cond_timedwait_monotonic_np.c b/src/thread/pthread_cond_timedwait_monotonic_np.c similarity index 100% rename from porting/linux/user/src/thread/pthread_cond_timedwait_monotonic_np.c rename to src/thread/pthread_cond_timedwait_monotonic_np.c diff --git a/porting/linux/user/src/thread/pthread_cond_timeout_np.c b/src/thread/pthread_cond_timeout_np.c similarity index 100% rename from porting/linux/user/src/thread/pthread_cond_timeout_np.c rename to src/thread/pthread_cond_timeout_np.c diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c index d539880e6..a20d47aaf 100644 --- a/src/thread/pthread_create.c +++ b/src/thread/pthread_create.c @@ -6,6 +6,7 @@ #include #include #include +#ifndef __LITEOS_A__ #include void log_print(const char* info,...) @@ -15,6 +16,86 @@ void log_print(const char* info,...) vfprintf(stdout,info, ap); va_end(ap); } +#endif + +pid_t getpid(void); + +#ifdef LINUX_MUSL_SUPPORTED +#define ANON_STACK_NAME_SIZE 50 +#include "musl_log.h" +#include + + +void stack_naming(struct pthread *new){ + size_t size_len; + unsigned char *start_addr; + char name[ANON_STACK_NAME_SIZE]; + if (new->guard_size) { + snprintf(name, ANON_STACK_NAME_SIZE, "guard:%d", new->tid); + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, new->map_base, new->guard_size, name); + start_addr = new->map_base + new->guard_size; + size_len = new->map_size - new->guard_size; + memset(name, 0, ANON_STACK_NAME_SIZE); + } else { + start_addr = new->map_base; + size_len = new->map_size; + } + snprintf(name, ANON_STACK_NAME_SIZE, "stack:%d", new->tid); + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start_addr, size_len, name); +}; +#endif + +#ifdef RESERVE_SIGNAL_STACK +#if defined (__LP64__) +#define RESERVE_SIGNAL_STACK_SIZE (32 * 1024) +#else +#define RESERVE_SIGNAL_STACK_SIZE (20 * 1024) +#endif +void __pthread_reserve_signal_stack() +{ + void* stack = mmap(NULL, RESERVE_SIGNAL_STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0); + if (stack != MAP_FAILED) { + if (mprotect(stack, __default_guardsize, PROT_NONE) == -1) { + munmap(stack, RESERVE_SIGNAL_STACK_SIZE); + return; + } + } + + stack_t signal_stack; + signal_stack.ss_sp = (uint8_t*)stack + __default_guardsize; + signal_stack.ss_size = RESERVE_SIGNAL_STACK_SIZE - __default_guardsize; + signal_stack.ss_flags = 0; + sigaltstack(&signal_stack, NULL); + + pthread_t self = __pthread_self(); + self->signal_stack = stack; + char name[ANON_STACK_NAME_SIZE]; + snprintf(name, ANON_STACK_NAME_SIZE, "signal_stack:%d", __pthread_self()->tid); + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, signal_stack.ss_sp, signal_stack.ss_size, name); + return; +} + +void __pthread_release_signal_stack() +{ + pthread_t self = __pthread_self(); + if (self->signal_stack == NULL) { + return; + } + + stack_t signal_stack, old_stack; + memset(&signal_stack, 0, sizeof(signal_stack)); + signal_stack.ss_flags = SS_DISABLE; + sigaltstack(&signal_stack, &old_stack); + munmap(self->signal_stack, __default_guardsize); + if (old_stack.ss_flags != SS_DISABLE) { + munmap(old_stack.ss_sp, old_stack.ss_size); + } + self->signal_stack = NULL; +} + +weak_alias(__pthread_reserve_signal_stack, pthread_reserve_signal_stack); +weak_alias(__pthread_release_signal_stack, pthread_release_signal_stack); +#endif static void dummy_0() { @@ -24,7 +105,9 @@ weak_alias(dummy_0, __release_ptc); weak_alias(dummy_0, __pthread_tsd_run_dtors); weak_alias(dummy_0, __do_orphaned_stdio_locks); weak_alias(dummy_0, __dl_thread_cleanup); +#ifndef __LITEOS_A__ weak_alias(dummy_0, __membarrier_init); +#endif static int tl_lock_count; static int tl_lock_waiters; @@ -60,13 +143,21 @@ void __tl_sync(pthread_t td) if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0); } +#ifdef ENABLE_HWASAN +weak void __hwasan_thread_enter(); +weak void __hwasan_thread_exit(); + +__attribute__((no_sanitize("hwaddress"))) +#endif _Noreturn void __pthread_exit(void *result) { pthread_t self = __pthread_self(); sigset_t set; +#ifdef FEATURE_PTHREAD_CANCEL self->canceldisable = 1; self->cancelasync = 0; +#endif self->result = result; while (self->cancelbuf) { @@ -80,6 +171,7 @@ _Noreturn void __pthread_exit(void *result) __block_app_sigs(&set); +#ifndef __LITEOS_A__ /* This atomic potentially competes with a concurrent pthread_detach * call; the loser is responsible for freeing thread resources. */ int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING); @@ -91,7 +183,7 @@ _Noreturn void __pthread_exit(void *result) * issues that could lead to deadlock. */ __vm_wait(); } - +#endif /* Access to target the exiting thread with syscalls that use * its kernel tid is controlled by killlock. For detached threads, * any use past this point would have undefined behavior, but for @@ -103,14 +195,22 @@ _Noreturn void __pthread_exit(void *result) * application signals being blocked above. */ __tl_lock(); +#if defined(RESERVE_SIGNAL_STACK) + __pthread_release_signal_stack(); +#endif /* If this is the only thread in the list, don't proceed with * termination of the thread, but restore the previous lock and * signal state to prepare for exit to call atexit handlers. */ if (self->next == self) { __tl_unlock(); UNLOCK(self->killlock); +#ifndef __LITEOS_A__ self->detach_state = state; +#endif __restore_sigs(&set); +#ifdef ENABLE_HWASAN + __hwasan_thread_exit(); +#endif exit(0); } @@ -148,6 +248,24 @@ _Noreturn void __pthread_exit(void *result) self->prev->next = self->next; self->prev = self->next = self; +#ifdef __LITEOS_A__ + /* This atomic potentially competes with a concurrent pthread_detach + * call; the loser is responsible for freeing thread resources. */ + int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING); + + /* After the kernel thread exits, its tid may be reused. Clear it + * to prevent inadvertent use and inform functions that would use + * it that it's no longer available. */ + if (self->detach_state == DT_DETACHED) { + /* Detached threads must block even implementation-internal + * signals, since they will not have a stack in their last + * moments of existence. */ + __block_all_sigs(&set); + self->tid = 0; + } + + __tl_unlock(); +#else if (state==DT_DETACHED && self->map_base) { /* Detached threads must block even implementation-internal * signals, since they will not have a stack in their last @@ -172,8 +290,14 @@ _Noreturn void __pthread_exit(void *result) * to prevent inadvertent use and inform functions that would use * it that it's no longer available. */ self->tid = 0; +#endif + UNLOCK(self->killlock); +#ifdef ENABLE_HWASAN + __hwasan_thread_exit(); +#endif + for (;;) __syscall(SYS_exit, 0); } @@ -196,9 +320,16 @@ struct start_args { unsigned long sig_mask[_NSIG/8/sizeof(long)]; }; +#ifdef ENABLE_HWASAN +__attribute__((no_sanitize("hwaddress"))) +#endif static int start(void *p) { - struct start_args *args = p; +#ifdef ENABLE_HWASAN + __hwasan_thread_enter(); +#endif + struct start_args *args = (struct start_args *)p; +#ifndef __LITEOS_A__ int state = args->control; if (state) { if (a_cas(&args->control, 1, 2)==1) @@ -208,14 +339,27 @@ static int start(void *p) for (;;) __syscall(SYS_exit, 0); } } +#endif __syscall(SYS_rt_sigprocmask, SIG_SETMASK, &args->sig_mask, 0, _NSIG/8); +#if defined(RESERVE_SIGNAL_STACK) + __pthread_reserve_signal_stack(); +#endif __pthread_exit(args->start_func(args->start_arg)); return 0; } +#ifdef ENABLE_HWASAN +__attribute__((no_sanitize("hwaddress"))) +#endif static int start_c11(void *p) { - struct start_args *args = p; +#if defined(RESERVE_SIGNAL_STACK) + __pthread_reserve_signal_stack(); +#endif +#ifdef ENABLE_HWASAN + __hwasan_thread_enter(); +#endif + struct start_args *args = (struct start_args *)p; int (*start)(void*) = (int(*)(void*)) args->start_func; __pthread_exit((void *)(uintptr_t)start(args->start_arg)); return 0; @@ -229,6 +373,32 @@ weak_alias(dummy, __pthread_tsd_size); static void *dummy_tsd[1] = { 0 }; weak_alias(dummy_tsd, __pthread_tsd_main); +#ifdef __LITEOS_A__ +int __pthread_init_and_check_attr(const pthread_attr_t *restrict attrp, pthread_attr_t *attr) +{ + int policy = 0; + struct sched_param param = {0}; + int c11 = (attrp == __ATTRP_C11_THREAD); + int ret; + + if (attrp && !c11) memcpy(attr, attrp, sizeof(pthread_attr_t)); + + if (!attrp || c11) { + pthread_attr_init(attr); + } + + if (!attr->_a_sched) { + ret = pthread_getschedparam(pthread_self(), &policy, ¶m); + if (ret) return ret; + attr->_a_policy = policy; + attr->_a_prio = param.sched_priority; + attr->_a_deadline = param.sched_deadline; + attr->_a_period = param.sched_period; + } + + return 0; +} +#else static FILE *volatile dummy_file = 0; weak_alias(dummy_file, __stdin_used); weak_alias(dummy_file, __stdout_used); @@ -238,21 +408,34 @@ static void init_file_lock(FILE *f) { if (f && f->lock<0) f->lock = 0; } +#endif +#ifdef ENABLE_HWASAN +__attribute__((no_sanitize("hwaddress"))) +#endif int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg) { int ret, c11 = (attrp == __ATTRP_C11_THREAD); - size_t size, guard; + size_t size, guard, size_len; struct pthread *self, *new; - unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit; + unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit, *start_addr; unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED; pthread_attr_t attr = { 0 }; sigset_t set; - if (!libc.can_do_threads) return ENOSYS; + if (!libc.can_do_threads) { +#ifdef LINUX_MUSL_SUPPORTED + MUSL_LOGE("pthread_create: can't do threads, err: %{public}s", strerror(errno)); +#endif + return ENOSYS; + } +#ifdef __LITEOS_A__ + if (!entry) return EINVAL; +#endif self = __pthread_self(); +#ifndef __LITEOS_A__ if (!libc.threaded) { for (FILE *f=*__ofl_lock(); f; f=f->next) init_file_lock(f); @@ -266,13 +449,21 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att libc.threaded = 1; } if (attrp && !c11) attr = *attrp; - +#endif __acquire_ptc(); + +#ifdef __LITEOS_A__ + ret = __pthread_init_and_check_attr(attrp, &attr); + if (ret) { + __release_ptc(); + return ret; + } +#else if (!attrp || c11) { attr._a_stacksize = __default_stacksize; attr._a_guardsize = __default_guardsize; } - +#endif if (attr._a_stackaddr) { size_t need = libc.tls_size + __pthread_tsd_size; size = attr._a_stacksize; @@ -297,16 +488,33 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att if (!tsd) { if (guard) { +#ifdef __LITEOS_A__ + map = __mmap(0, size, PROT_READ|PROT_WRITE|PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0); +#else map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (map == MAP_FAILED) goto fail; +#endif + if (map == MAP_FAILED) { +#ifdef LINUX_MUSL_SUPPORTED + MUSL_LOGE("pthread_create: mmap PROT_NONE failed, err:%{public}s", strerror(errno)); +#endif + goto fail; + } if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE) && errno != ENOSYS) { +#ifdef LINUX_MUSL_SUPPORTED + MUSL_LOGE("pthread_create: mprotect failed, err:%{public}s", strerror(errno)); +#endif __munmap(map, size); goto fail; } } else { map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); - if (map == MAP_FAILED) goto fail; + if (map == MAP_FAILED) { +#ifdef LINUX_MUSL_SUPPORTED + MUSL_LOGE("pthread_create: mmap PROT_READ|PROT_WRITE failed, err:%{public}s", strerror(errno)); +#endif + goto fail; + } } tsd = map + size - __pthread_tsd_size; if (!stack) { @@ -322,6 +530,8 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att new->stack_size = stack - stack_limit; new->guard_size = guard; new->self = new; + new->pid = getpid(); + new->proc_tid = -1; new->tsd = (void *)tsd; new->locale = &libc.global_locale; if (attr._a_detach) { @@ -357,12 +567,33 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att __tl_lock(); if (!libc.threads_minus_1++) libc.need_locks = 1; +#ifdef __LITEOS_A__ + ret = __thread_clone((c11 ? start_c11 : start), flags, new, stack); +#else ret = __clone((c11 ? start_c11 : start), stack, flags, args, &new->tid, TP_ADJ(new), &__thread_list_lock); +#endif /* All clone failures translate to EAGAIN. If explicit scheduling * was requested, attempt it before unlocking the thread list so * that the failed thread is never exposed and so that we can * clean up all transient resource usage before returning. */ +#ifdef __LITEOS_A__ + if (ret < 0) { + ret = -EAGAIN; + } else { + new->next = self->next; + new->prev = self; + new->next->prev = new; + new->prev->next = new; + + *res = new; + __tl_unlock(); + __restore_sigs(&set); + __release_ptc(); + ret = __syscall(SYS_sched_setscheduler, + new->tid, attr._a_policy, &attr._a_prio, MUSL_TYPE_THREAD); + } +#else if (ret < 0) { ret = -EAGAIN; } else if (attr._a_sched) { @@ -375,6 +606,9 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att } if (ret >= 0) { +#ifdef LINUX_MUSL_SUPPORTED + stack_naming(new); +#endif new->next = self->next; new->prev = self; new->next->prev = new; @@ -385,13 +619,24 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att __tl_unlock(); __restore_sigs(&set); __release_ptc(); +#endif if (ret < 0) { +#ifdef __LITEOS_A__ + if (!--libc.threads_minus_1) libc.need_locks = 0; + __tl_unlock(); + __restore_sigs(&set); + __release_ptc(); +#endif if (map) __munmap(map, size); +#ifdef LINUX_MUSL_SUPPORTED + MUSL_LOGE("pthread_create: ret:%{public}d, err:%{public}s", ret, strerror(errno)); +#endif return -ret; } - +#ifndef __LITEOS_A__ *res = new; +#endif return 0; fail: __release_ptc(); @@ -401,6 +646,7 @@ fail: weak_alias(__pthread_exit, pthread_exit); weak_alias(__pthread_create, pthread_create); +#ifndef __LITEOS_A__ struct pthread* __pthread_list_find(pthread_t thread_id, const char* info) { struct pthread *thread = (struct pthread *)thread_id; @@ -430,4 +676,5 @@ pid_t __pthread_gettid_np(pthread_t t) __tl_unlock(); return thread ? thread->tid : -1; } -weak_alias(__pthread_gettid_np, pthread_gettid_np); \ No newline at end of file +weak_alias(__pthread_gettid_np, pthread_gettid_np); +#endif diff --git a/src/thread/pthread_detach.c b/src/thread/pthread_detach.c index 77772af2c..9e18b5909 100644 --- a/src/thread/pthread_detach.c +++ b/src/thread/pthread_detach.c @@ -1,14 +1,33 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" #include +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif static int __pthread_detach(pthread_t t) { /* If the cas fails, detach state is either already-detached * or exiting/exited, and pthread_join will trap or cleanup. */ if (a_cas(&t->detach_state, DT_JOINABLE, DT_DETACHED) != DT_JOINABLE) return __pthread_join(t, 0); +#if defined(__LITEOS_A__) || defined(__LITEOS_M__) + int ret = __syscall(SYS_pthread_set_detach, t->tid); + if (ret) { + a_swap(&t->detach_state, DT_JOINABLE); + } + if (ret == ESRCH) { + ret = 0; + } + return ret; +#else return 0; +#endif } weak_alias(__pthread_detach, pthread_detach); +#ifndef __LITEOS_M__ weak_alias(__pthread_detach, thrd_detach); +#endif diff --git a/src/thread/pthread_getschedparam.c b/src/thread/pthread_getschedparam.c index c098befb1..d7c3ddaa3 100644 --- a/src/thread/pthread_getschedparam.c +++ b/src/thread/pthread_getschedparam.c @@ -1,21 +1,53 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" #include "lock.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_getschedparam(pthread_t t, int *restrict policy, struct sched_param *restrict param) { int r; +#ifndef __LITEOS_M__ sigset_t set; __block_app_sigs(&set); +#endif LOCK(t->killlock); if (!t->tid) { r = ESRCH; } else { +#if defined(__LITEOS_A__) || defined(__LITEOS_M__) +#ifdef __LITEOS_M__ + r = __syscall(SYS_sched_getparam, t->tid, MUSL_TYPE_THREAD); +#else + r = __syscall(SYS_sched_getparam, t->tid, param, MUSL_TYPE_THREAD); +#endif + if (r >= 0) { +#ifdef __LITEOS_M__ + param->sched_priority = r; +#endif + r = __syscall(SYS_sched_getscheduler, t->tid, MUSL_TYPE_THREAD); + if (r >= 0) { + *policy = r; + r = 0; + } + } + + if (r < 0) { + r = -r; + } +#else r = -__syscall(SYS_sched_getparam, t->tid, param); if (!r) { *policy = __syscall(SYS_sched_getscheduler, t->tid); } +#endif } UNLOCK(t->killlock); +#ifndef __LITEOS_M__ __restore_sigs(&set); +#endif return r; } diff --git a/src/thread/pthread_join.c b/src/thread/pthread_join.c index 17dae85d7..dc497d44b 100644 --- a/src/thread/pthread_join.c +++ b/src/thread/pthread_join.c @@ -1,18 +1,61 @@ #define _GNU_SOURCE +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" #include +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at) +#else static void dummy1(pthread_t t) { } weak_alias(dummy1, __tl_sync); static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at) +#endif { int state, cs, r = 0; +#if defined(__LITEOS_A__) || defined(__LITEOS_M__) + unsigned int tid; + pthread_t self = __pthread_self(); +#endif +#ifndef __LITEOS_M__ __pthread_testcancel(); __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0); +#endif +#if defined(__LITEOS_A__) || defined(__LITEOS_M__) + if (t == self) { + r = EINVAL; + goto out; + } + switch (t->detach_state) { + case DT_JOINABLE: { + r = __syscall(SYS_pthread_join, t->tid); + break; + } + case DT_EXITING: + break; + case DT_DETACHED: + default: + r = EINVAL; + break; + } +out: +#ifdef __LITEOS_M__ + if (r == ESRCH || r == EINVAL) return r; +#else + __pthread_setcancelstate(cs, 0); + if (r == ESRCH || r == EINVAL) return r; + __tl_sync(t); +#endif + if (res) *res = t->result; + tid = t->tid; + t->tid = 0; + return __syscall(SYS_pthread_deatch, tid); +#else while ((state = t->detach_state) && r != ETIMEDOUT && r != EINVAL) { if (state >= DT_DETACHED) a_crash(); r = __timedwait_cp(&t->detach_state, state, CLOCK_REALTIME, at, 1); @@ -23,13 +66,18 @@ static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec if (res) *res = t->result; if (t->map_base) __munmap(t->map_base, t->map_size); return 0; +#endif } +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int __pthread_join(pthread_t t, void **res) { return __pthread_timedjoin_np(t, res, 0); } +#ifndef __LITEOS_M__ static int __pthread_tryjoin_np(pthread_t t, void **res) { return t->detach_state==DT_JOINABLE ? EBUSY : __pthread_join(t, res); @@ -37,4 +85,5 @@ static int __pthread_tryjoin_np(pthread_t t, void **res) weak_alias(__pthread_tryjoin_np, pthread_tryjoin_np); weak_alias(__pthread_timedjoin_np, pthread_timedjoin_np); +#endif weak_alias(__pthread_join, pthread_join); diff --git a/src/thread/pthread_kill.c b/src/thread/pthread_kill.c index 79ddb2097..92c2b776e 100644 --- a/src/thread/pthread_kill.c +++ b/src/thread/pthread_kill.c @@ -1,3 +1,4 @@ +#include #include "pthread_impl.h" #include "lock.h" @@ -10,8 +11,13 @@ int pthread_kill(pthread_t t, int sig) * must be async-cancel-safe. */ __block_all_sigs(&set); LOCK(t->killlock); +#ifdef LINUX_MUSL_SUPPORTED + r = t->tid ? -__syscall(__NR_tgkill, getpid(), t->tid, sig) + : (sig+0U >= _NSIG ? EINVAL : 0); +#else r = t->tid ? -__syscall(SYS_tkill, t->tid, sig) : (sig+0U >= _NSIG ? EINVAL : 0); +#endif UNLOCK(t->killlock); __restore_sigs(&set); return r; diff --git a/porting/linux/user/src/thread/pthread_mutex_clocklock.c b/src/thread/pthread_mutex_clocklock.c similarity index 100% rename from porting/linux/user/src/thread/pthread_mutex_clocklock.c rename to src/thread/pthread_mutex_clocklock.c diff --git a/src/thread/pthread_mutex_getprioceiling.c b/src/thread/pthread_mutex_getprioceiling.c index 8c75a6612..17f85639a 100644 --- a/src/thread/pthread_mutex_getprioceiling.c +++ b/src/thread/pthread_mutex_getprioceiling.c @@ -1,6 +1,8 @@ #include "pthread_impl.h" +#include int pthread_mutex_getprioceiling(const pthread_mutex_t *restrict m, int *restrict ceiling) { + UNSUPPORTED_API_VOID(LITEOS_A); return EINVAL; } diff --git a/src/thread/pthread_mutex_init.c b/src/thread/pthread_mutex_init.c index acf45a746..f606428db 100644 --- a/src/thread/pthread_mutex_init.c +++ b/src/thread/pthread_mutex_init.c @@ -2,7 +2,7 @@ int pthread_mutex_init(pthread_mutex_t *restrict m, const pthread_mutexattr_t *restrict a) { - *m = (pthread_mutex_t){0}; + __builtin_memset(m, 0, sizeof(pthread_mutex_t)); if (a) m->_m_type = a->__attr; return 0; } diff --git a/src/thread/pthread_mutex_lock.c b/src/thread/pthread_mutex_lock.c index 638d4b869..8152e2d71 100644 --- a/src/thread/pthread_mutex_lock.c +++ b/src/thread/pthread_mutex_lock.c @@ -1,12 +1,19 @@ #include "pthread_impl.h" +#ifdef LINUX_MUSL_SUPPORTED +extern int __pthread_mutex_timedlock_inner(pthread_mutex_t *restrict m, const struct timespec *restrict at); +#endif int __pthread_mutex_lock(pthread_mutex_t *m) { if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL && !a_cas(&m->_m_lock, 0, EBUSY)) return 0; +#ifdef LINUX_MUSL_SUPPORTED + return __pthread_mutex_timedlock_inner(m, 0); +#else return __pthread_mutex_timedlock(m, 0); +#endif } weak_alias(__pthread_mutex_lock, pthread_mutex_lock); diff --git a/porting/linux/user/src/thread/pthread_mutex_lock_timeout_np.c b/src/thread/pthread_mutex_lock_timeout_np.c similarity index 100% rename from porting/linux/user/src/thread/pthread_mutex_lock_timeout_np.c rename to src/thread/pthread_mutex_lock_timeout_np.c diff --git a/src/thread/pthread_mutex_timedlock.c b/src/thread/pthread_mutex_timedlock.c index 9279fc543..527b854ee 100644 --- a/src/thread/pthread_mutex_timedlock.c +++ b/src/thread/pthread_mutex_timedlock.c @@ -20,6 +20,7 @@ static int __futex4(volatile void *addr, int op, int val, const struct timespec static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct timespec *restrict at) { + int clock = m->_m_clock; int type = m->_m_type; int priv = (type & 128) ^ 128; pthread_t self = __pthread_self(); @@ -42,31 +43,33 @@ static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct } /* Signal to trylock that we already have the lock. */ m->_m_count = -1; - return __pthread_mutex_trylock(m); + return __pthread_mutex_trylock_owner(m); case ETIMEDOUT: return e; case EDEADLK: if ((type&3) == PTHREAD_MUTEX_ERRORCHECK) return e; } - do e = __timedwait(&(int){0}, 0, CLOCK_REALTIME, at, 1); + do e = __timedwait(&(int){0}, 0, clock, at, 1); while (e != ETIMEDOUT); return e; } -int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at) +int __pthread_mutex_timedlock_inner(pthread_mutex_t *restrict m, const struct timespec *restrict at) { - if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL - && !a_cas(&m->_m_lock, 0, EBUSY)) - return 0; - int type = m->_m_type; - int r, t, priv = (type & 128) ^ 128; - + int r; + // PI +#ifndef __LITEOS_A__ + if (type&8) { + r = __pthread_mutex_trylock_owner(m); + if (r != EBUSY) return r; + return pthread_mutex_timedlock_pi(m, at); + } +#endif r = __pthread_mutex_trylock(m); if (r != EBUSY) return r; - - if (type&8) return pthread_mutex_timedlock_pi(m, at); - + int clock = (m->_m_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME; + int t, priv = (type & 128) ^ 128; int spins = 100; while (spins-- && m->_m_lock && !m->_m_waiters) a_spin(); @@ -82,11 +85,19 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec a_inc(&m->_m_waiters); t = r | 0x80000000; a_cas(&m->_m_lock, r, t); - r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, priv); + r = __timedwait(&m->_m_lock, t, clock, at, priv); a_dec(&m->_m_waiters); if (r && r != EINTR) break; } return r; } +int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at) +{ + if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL + && !a_cas(&m->_m_lock, 0, EBUSY)) + return 0; + return __pthread_mutex_timedlock_inner(m, at); +} + weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock); diff --git a/porting/linux/user/src/thread/pthread_mutex_timedlock_monotonic_np.c b/src/thread/pthread_mutex_timedlock_monotonic_np.c similarity index 100% rename from porting/linux/user/src/thread/pthread_mutex_timedlock_monotonic_np.c rename to src/thread/pthread_mutex_timedlock_monotonic_np.c diff --git a/src/thread/pthread_mutex_trylock.c b/src/thread/pthread_mutex_trylock.c index a24e7c58a..afe49b052 100644 --- a/src/thread/pthread_mutex_trylock.c +++ b/src/thread/pthread_mutex_trylock.c @@ -9,6 +9,15 @@ int __pthread_mutex_trylock_owner(pthread_mutex_t *m) old = m->_m_lock; own = old & 0x3fffffff; +#ifdef __LITEOS_A__ + if (own == tid) { + if ((type&PTHREAD_MUTEX_TYPE_MASK) == PTHREAD_MUTEX_RECURSIVE) { + if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN; + m->_m_count++; + return 0; + } + } +#else if (own == tid) { if ((type&8) && m->_m_count<0) { old &= 0x40000000; @@ -21,13 +30,16 @@ int __pthread_mutex_trylock_owner(pthread_mutex_t *m) return 0; } } +#endif if (own == 0x3fffffff) return ENOTRECOVERABLE; if (own || (old && !(type & 4))) return EBUSY; if (type & 128) { if (!self->robust_list.off) { self->robust_list.off = (char*)&m->_m_lock-(char *)&m->_m_next; +#ifndef __LITEOS_A__ __syscall(SYS_set_robust_list, &self->robust_list, 3*sizeof(long)); +#endif } if (m->_m_waiters) tid |= 0x80000000; self->robust_list.pending = &m->_m_next; @@ -36,10 +48,13 @@ int __pthread_mutex_trylock_owner(pthread_mutex_t *m) if (a_cas(&m->_m_lock, old, tid) != old) { self->robust_list.pending = 0; +#ifndef __LITEOS_A__ if ((type&12)==12 && m->_m_waiters) return ENOTRECOVERABLE; +#endif return EBUSY; } +#ifndef __LITEOS_A__ success: if ((type&8) && m->_m_waiters) { int priv = (type & 128) ^ 128; @@ -47,6 +62,7 @@ success: self->robust_list.pending = 0; return (type&4) ? ENOTRECOVERABLE : EBUSY; } +#endif volatile void *next = self->robust_list.head; m->_m_next = next; diff --git a/src/thread/pthread_mutex_unlock.c b/src/thread/pthread_mutex_unlock.c index b66423e6c..dc6943e60 100644 --- a/src/thread/pthread_mutex_unlock.c +++ b/src/thread/pthread_mutex_unlock.c @@ -16,7 +16,11 @@ int __pthread_mutex_unlock(pthread_mutex_t *m) int own = old & 0x3fffffff; if (own != self->tid) return EPERM; +#ifdef __LITEOS_A__ + if ((type & PTHREAD_MUTEX_TYPE_MASK) == PTHREAD_MUTEX_RECURSIVE && m->_m_count) +#else if ((type&3) == PTHREAD_MUTEX_RECURSIVE && m->_m_count) +#endif return m->_m_count--, 0; if ((type&4) && (old&0x40000000)) new = 0x7fffffff; @@ -30,6 +34,9 @@ int __pthread_mutex_unlock(pthread_mutex_t *m) if (next != &self->robust_list.head) *(volatile void *volatile *) ((char *)next - sizeof(void *)) = prev; } +#ifdef __LITEOS_A__ + cont = a_swap(&m->_m_lock, new); +#else if (type&8) { if (old<0 || a_cas(&m->_m_lock, old, new)!=old) { if (new) a_store(&m->_m_waiters, -1); @@ -40,6 +47,8 @@ int __pthread_mutex_unlock(pthread_mutex_t *m) } else { cont = a_swap(&m->_m_lock, new); } +#endif + if (type != PTHREAD_MUTEX_NORMAL && !priv) { self->robust_list.pending = 0; __vm_unlock(); diff --git a/src/thread/pthread_mutexattr_setprotocol.c b/src/thread/pthread_mutexattr_setprotocol.c index 8b80c1ce9..828980fd3 100644 --- a/src/thread/pthread_mutexattr_setprotocol.c +++ b/src/thread/pthread_mutexattr_setprotocol.c @@ -1,11 +1,14 @@ #include "pthread_impl.h" #include "syscall.h" +#include static volatile int check_pi_result = -1; int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int protocol) { int r; + + UNSUPPORTED_API_VOID(LITEOS_A); switch (protocol) { case PTHREAD_PRIO_NONE: a->__attr &= ~8; diff --git a/src/thread/pthread_mutexattr_setrobust.c b/src/thread/pthread_mutexattr_setrobust.c index 30a9ac3be..8033072df 100644 --- a/src/thread/pthread_mutexattr_setrobust.c +++ b/src/thread/pthread_mutexattr_setrobust.c @@ -1,12 +1,15 @@ #include "pthread_impl.h" #include "syscall.h" +#ifndef __LITEOS_A__ static volatile int check_robust_result = -1; +#endif int pthread_mutexattr_setrobust(pthread_mutexattr_t *a, int robust) { if (robust > 1U) return EINVAL; if (robust) { +#ifndef __LITEOS_A__ int r = check_robust_result; if (r < 0) { void *p; @@ -15,6 +18,7 @@ int pthread_mutexattr_setrobust(pthread_mutexattr_t *a, int robust) a_store(&check_robust_result, r); } if (r) return r; +#endif a->__attr |= 4; return 0; } diff --git a/src/thread/pthread_once.c b/src/thread/pthread_once.c index 8e8d40ae8..69f77f186 100644 --- a/src/thread/pthread_once.c +++ b/src/thread/pthread_once.c @@ -36,6 +36,17 @@ hidden int __pthread_once_full(pthread_once_t *control, void (*init)(void)) } } +#if defined(MUSL_AARCH64_ARCH) +int __pthread_once(pthread_once_t *control, void (*init)(void)) +{ + /* Return immediately if init finished before, use load aquire to ensure that + * effects of the init routine are visible to the caller. */ + if (a_ldar((volatile int *)control) == 2) { + return 0; + } + return __pthread_once_full(control, init); +} +#else int __pthread_once(pthread_once_t *control, void (*init)(void)) { /* Return immediately if init finished before, but ensure that @@ -46,5 +57,6 @@ int __pthread_once(pthread_once_t *control, void (*init)(void)) } return __pthread_once_full(control, init); } +#endif weak_alias(__pthread_once, pthread_once); diff --git a/porting/linux/user/src/thread/pthread_rwlock_clockrdlock.c b/src/thread/pthread_rwlock_clockrdlock.c similarity index 100% rename from porting/linux/user/src/thread/pthread_rwlock_clockrdlock.c rename to src/thread/pthread_rwlock_clockrdlock.c diff --git a/porting/linux/user/src/thread/pthread_rwlock_clockwrlock.c b/src/thread/pthread_rwlock_clockwrlock.c similarity index 100% rename from porting/linux/user/src/thread/pthread_rwlock_clockwrlock.c rename to src/thread/pthread_rwlock_clockwrlock.c diff --git a/src/thread/pthread_rwlock_init.c b/src/thread/pthread_rwlock_init.c index a2c0b478c..27cf8723e 100644 --- a/src/thread/pthread_rwlock_init.c +++ b/src/thread/pthread_rwlock_init.c @@ -2,7 +2,7 @@ int pthread_rwlock_init(pthread_rwlock_t *restrict rw, const pthread_rwlockattr_t *restrict a) { - *rw = (pthread_rwlock_t){0}; + __builtin_memset(rw, 0, sizeof(pthread_rwlock_t)); if (a) rw->_rw_shared = a->__attr[0]*128; return 0; } diff --git a/src/thread/pthread_rwlock_timedrdlock.c b/src/thread/pthread_rwlock_timedrdlock.c index 8cdd8ecf1..0152a347e 100644 --- a/src/thread/pthread_rwlock_timedrdlock.c +++ b/src/thread/pthread_rwlock_timedrdlock.c @@ -3,6 +3,7 @@ int __pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at) { int r, t; + int clock = (rw->_rw_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME; r = pthread_rwlock_tryrdlock(rw); if (r != EBUSY) return r; @@ -15,7 +16,7 @@ int __pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct tim t = r | 0x80000000; a_inc(&rw->_rw_waiters); a_cas(&rw->_rw_lock, r, t); - r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, rw->_rw_shared^128); + r = __timedwait(&rw->_rw_lock, t, clock, at, rw->_rw_shared^128); a_dec(&rw->_rw_waiters); if (r && r != EINTR) return r; } diff --git a/porting/linux/user/src/thread/pthread_rwlock_timedrdlock_monotonic_np.c b/src/thread/pthread_rwlock_timedrdlock_monotonic_np.c similarity index 100% rename from porting/linux/user/src/thread/pthread_rwlock_timedrdlock_monotonic_np.c rename to src/thread/pthread_rwlock_timedrdlock_monotonic_np.c diff --git a/src/thread/pthread_rwlock_timedwrlock.c b/src/thread/pthread_rwlock_timedwrlock.c index d77706e6b..747c22670 100644 --- a/src/thread/pthread_rwlock_timedwrlock.c +++ b/src/thread/pthread_rwlock_timedwrlock.c @@ -2,6 +2,10 @@ int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at) { + if (rw == NULL) { + return EINVAL; + } + int clock = (rw->_rw_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME; int r, t; r = pthread_rwlock_trywrlock(rw); @@ -15,7 +19,7 @@ int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct tim t = r | 0x80000000; a_inc(&rw->_rw_waiters); a_cas(&rw->_rw_lock, r, t); - r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, rw->_rw_shared^128); + r = __timedwait(&rw->_rw_lock, t, clock, at, rw->_rw_shared^128); a_dec(&rw->_rw_waiters); if (r && r != EINTR) return r; } diff --git a/porting/linux/user/src/thread/pthread_rwlock_timedwrlock_monotonic_np.c b/src/thread/pthread_rwlock_timedwrlock_monotonic_np.c similarity index 100% rename from porting/linux/user/src/thread/pthread_rwlock_timedwrlock_monotonic_np.c rename to src/thread/pthread_rwlock_timedwrlock_monotonic_np.c diff --git a/src/thread/pthread_self.c b/src/thread/pthread_self.c index bd3bf95bb..da87c9edb 100644 --- a/src/thread/pthread_self.c +++ b/src/thread/pthread_self.c @@ -1,6 +1,28 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" #include +#ifdef __LITEOS_A__ +pthread_t __pthread_self() +{ + uintptr_t p; + p = __syscall(SYS_get_thread_area); + return (void *)(p - sizeof(struct pthread)); +} +#elif defined(__LITEOS_M__) +_LIBC_TEXT_SECTION pthread_t __pthread_self() +{ + uintptr_t p; + p = __syscall(SYS_get_thread_area); + return (pthread_t)(p - sizeof(struct pthread)); +} +#endif + +#ifdef __LITEOS_M__ +weak_alias(__pthread_self, pthread_self); +#else static pthread_t __pthread_self_internal() { return __pthread_self(); @@ -8,3 +30,4 @@ static pthread_t __pthread_self_internal() weak_alias(__pthread_self_internal, pthread_self); weak_alias(__pthread_self_internal, thrd_current); +#endif diff --git a/src/thread/pthread_setcancelstate.c b/src/thread/pthread_setcancelstate.c index 5ab8c338f..bbb22abdd 100644 --- a/src/thread/pthread_setcancelstate.c +++ b/src/thread/pthread_setcancelstate.c @@ -2,11 +2,13 @@ int __pthread_setcancelstate(int new, int *old) { +#ifdef FEATURE_PTHREAD_CANCEL if (new > 2U) return EINVAL; struct pthread *self = __pthread_self(); if (old) *old = self->canceldisable; self->canceldisable = new; return 0; +#endif } weak_alias(__pthread_setcancelstate, pthread_setcancelstate); diff --git a/src/thread/pthread_setcanceltype.c b/src/thread/pthread_setcanceltype.c index bf0a3f383..851485869 100644 --- a/src/thread/pthread_setcanceltype.c +++ b/src/thread/pthread_setcanceltype.c @@ -2,10 +2,12 @@ int pthread_setcanceltype(int new, int *old) { +#ifdef FEATURE_PTHREAD_CANCEL struct pthread *self = __pthread_self(); if (new > 1U) return EINVAL; if (old) *old = self->cancelasync; self->cancelasync = new; if (new) pthread_testcancel(); return 0; +#endif } diff --git a/src/thread/pthread_setschedparam.c b/src/thread/pthread_setschedparam.c index 76d4d45a3..ab9ea6bcf 100644 --- a/src/thread/pthread_setschedparam.c +++ b/src/thread/pthread_setschedparam.c @@ -1,14 +1,38 @@ +#ifdef __LITEOS_M__ +#include "usr_lib_define.h" +#endif #include "pthread_impl.h" #include "lock.h" +#ifdef __LITEOS_M__ +_LIBC_TEXT_SECTION +#endif int pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param) { int r; +#ifdef __LITEOS_M__ + if (policy != SCHED_RR && policy != SCHED_FIFO) { + return EINVAL; + } + + if (param->sched_priority < 0 || param->sched_priority > PTHREAD_PRIORITY_LOWEST) { + return EINVAL; + } +#else sigset_t set; __block_app_sigs(&set); +#endif LOCK(t->killlock); +#ifdef __LITEOS_A__ + r = !t->tid ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, param, MUSL_TYPE_THREAD); +#elif defined(__LITEOS_M__) + r = !t->tid ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, param->sched_priority, MUSL_TYPE_THREAD); +#else r = !t->tid ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, param); +#endif UNLOCK(t->killlock); +#ifndef __LITEOS_M__ __restore_sigs(&set); +#endif return r; } diff --git a/src/thread/pthread_setschedprio.c b/src/thread/pthread_setschedprio.c index fc2e13ddb..6998bb6ee 100644 --- a/src/thread/pthread_setschedprio.c +++ b/src/thread/pthread_setschedprio.c @@ -4,10 +4,20 @@ int pthread_setschedprio(pthread_t t, int prio) { int r; +#ifdef __LITEOS_A__ + struct sched_param param = { + .sched_priority = prio, + }; +#endif + sigset_t set; __block_app_sigs(&set); LOCK(t->killlock); +#ifdef __LITEOS_A__ + r = !t->tid ? ESRCH : -__syscall(SYS_sched_setparam, t->tid, ¶m, MUSL_TYPE_THREAD); +#else r = !t->tid ? ESRCH : -__syscall(SYS_sched_setparam, t->tid, &prio); +#endif UNLOCK(t->killlock); __restore_sigs(&set); return r; diff --git a/src/thread/pthread_sigmask.c b/src/thread/pthread_sigmask.c index f188782a3..5e2f8c51d 100644 --- a/src/thread/pthread_sigmask.c +++ b/src/thread/pthread_sigmask.c @@ -1,12 +1,60 @@ #include #include +#ifdef LINUX_MUSL_SUPPORTED +#include +#include +#include +#include +#endif #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +#ifdef OHOS_ENABLE_PARAMETER +#include "sys_param.h" +#endif + +extern void intercept_pthread_sigmask(int how, sigset_t *restrict set); +static const char *param_name = "musl.sigchain.procmask"; + +/** + * @brief Get whether sigchain mask is enabled + * @retval True if the sigchain mask is enable, or false. + */ +bool get_sigchain_mask_enable() +{ +#ifdef OHOS_ENABLE_PARAMETER + static CachedHandle sigchain_procmask_handle = NULL; + if (sigchain_procmask_handle == NULL) { + sigchain_procmask_handle = CachedParameterCreate(param_name, "false"); + } + char *param_value = CachedParameterGet(sigchain_procmask_handle); + if (param_value != NULL) { + if (strcmp(param_value, "true") == 0) { + return true; + } + } +#endif + return false; +} +#endif + int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict old) { int ret; if (set && (unsigned)how - SIG_BLOCK > 2U) return EINVAL; +#ifdef LINUX_MUSL_SUPPORTED + /* sigchain intercepts pthread_sigmask */ + if (set && get_sigchain_mask_enable()) { + sigset_t tmpset = *set; + intercept_pthread_sigmask(how, &tmpset); + const sigset_t *new_set_ptr = &tmpset; + ret = -__syscall(SYS_rt_sigprocmask, how, new_set_ptr, old, _NSIG/8); + } else { + ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8); + } +#else ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8); +#endif if (!ret && old) { if (sizeof old->__bits[0] == 8) { old->__bits[0] &= ~0x380000000ULL; diff --git a/src/thread/sem_open.c b/src/thread/sem_open.c index 0ad29de96..12f8f7896 100644 --- a/src/thread/sem_open.c +++ b/src/thread/sem_open.c @@ -92,7 +92,9 @@ sem_t *sem_open(const char *name, int flags, ...) close(fd); goto fail; } +#ifndef __LITEOS_A__ close(fd); +#endif break; } if (errno != ENOENT) @@ -127,7 +129,9 @@ sem_t *sem_open(const char *name, int flags, ...) unlink(tmp); goto fail; } +#ifndef __LITEOS_A__ close(fd); +#endif e = link(tmp, name) ? errno : 0; unlink(tmp); if (!e) break; @@ -170,7 +174,11 @@ int sem_close(sem_t *sem) int i; LOCK(lock); for (i=0; i__val[0] <= 0 && !sem->__val[1]) a_spin(); @@ -29,3 +25,12 @@ int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at) } return 0; } + +int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at) +{ + pthread_testcancel(); + + if (!sem_trywait(sem)) return 0; + + return __sem_timedwait(sem, at); +} diff --git a/src/thread/thrd_yield.c b/src/thread/thrd_yield.c index f7ad13219..efe75780b 100644 --- a/src/thread/thrd_yield.c +++ b/src/thread/thrd_yield.c @@ -3,5 +3,9 @@ void thrd_yield() { +#ifdef __LITEOS_A__ + __syscall(SYS_sched_yield, -1); +#else __syscall(SYS_sched_yield); +#endif } diff --git a/src/time/__map_file.c b/src/time/__map_file.c index d3cefa828..2b0a854cf 100644 --- a/src/time/__map_file.c +++ b/src/time/__map_file.c @@ -14,6 +14,13 @@ const char unsigned *__map_file(const char *pathname, size_t *size) map = __mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); *size = st.st_size; } +#ifdef __LITEOS_A__ + if (map == MAP_FAILED) { + __syscall(SYS_close, fd); + } +#else __syscall(SYS_close, fd); +#endif + return map == MAP_FAILED ? 0 : map; } diff --git a/src/time/__tz.c b/src/time/__tz.c index c34b3eb75..6e475a1f9 100644 --- a/src/time/__tz.c +++ b/src/time/__tz.c @@ -9,6 +9,13 @@ #include "lock.h" #include "fork_impl.h" +#include "time_impl.h" +#ifdef OHOS_ENABLE_PARAMETER +#include "sys_param.h" +#define SYSPARAM_LENGTH 40 +#endif +#define __TZ_VERSION__ '2' + #define malloc __libc_malloc #define calloc undef #define realloc undef @@ -25,6 +32,7 @@ weak_alias(__tzname, tzname); static char std_name[TZNAME_MAX+1]; static char dst_name[TZNAME_MAX+1]; const char __utc[] = "UTC"; +const char __gmt[] = "GMT"; static int dst_off; static int r0[5], r1[5]; @@ -129,11 +137,31 @@ static void do_tzset() const char *try, *s, *p; const unsigned char *map = 0; size_t i; +#ifdef LINUX_MUSL_SUPPORTED + static const char search[] = + "/etc/zoneinfo/\0/usr/share/zoneinfo/\0/share/zoneinfo/\0"; +#else static const char search[] = "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0"; +#endif s = getenv("TZ"); - if (!s) s = "/etc/localtime"; + if (!s) { +#if defined(OHOS_ENABLE_PARAMETER) && defined(LINUX_MUSL_SUPPORTED) + static CachedHandle tz_param_handle = NULL; + if (tz_param_handle == NULL) { + tz_param_handle = CachedParameterCreate("persist.time.timezone", "/etc/localtime"); + } + const char *tz_param_value = CachedParameterGet(tz_param_handle); + if (tz_param_value != NULL){ + s = tz_param_value; + } else { + s = "/etc/localtime"; + } +#else + s = "/etc/localtime"; +#endif + } if (!*s) s = __utc; if (old_tz && !strcmp(s, old_tz)) return; @@ -169,6 +197,33 @@ static void do_tzset() /* Non-suid can use an absolute tzfile pathname or a relative * pathame beginning with "."; in secure mode, only the * standard path will be searched. */ +#ifdef LINUX_MUSL_SUPPORTED + int flag = 1; + if (!posix_form) { + if (*s == ':') s++; + if (*s == '/' || *s == '.') { + /* The path is invalid, use the default value. */ + flag = 0; + if (!libc.secure || !strcmp(s, "/etc/localtime")) { + map = __map_file(s, &map_size); + } + } + } + + if (flag) { + /* Adapt to time zone names, such as Asia/Shanghai or Shanghai*/ + size_t l = strlen(s); + if (l <= NAME_MAX && !strchr(s, '.')) { + memcpy(pathname, s, l+1); + pathname[l] = 0; + for (try=search; !map && *try; try+=l+1) { + l = strlen(try); + memcpy(pathname-l, try, l); + map = __map_file(pathname-l, &map_size); + } + } + } +#else if (!posix_form) { if (*s == ':') s++; if (*s == '/' || *s == '.') { @@ -188,6 +243,8 @@ static void do_tzset() } if (!map) s = __utc; } +#endif + if (map && (map_size < 44 || memcmp(map, "TZif", 4))) { __munmap((void *)map, map_size); map = 0; @@ -197,6 +254,32 @@ static void do_tzset() zi = map; if (map) { int scale = 2; + /* + * map[0]-map[3]: magic, it is TZif + * map[4]: version, '\0' or '2' or '3' as of 2013 + * map[5]-map[19]: reserved; must be zero + * map[20]-map[23]: The number of UT/local indicators stored in the file. + * map[24]-map[27]: The number of standard/wall indicators stored in the file. + * map[24]-map[31]: The number of leap seconds for which data entries are stored in the file. + * map[32]-map[35]: The number of transition times for which data entries are stored in the file. + * map[36]-map[39]: The number of local time types for which data entries are + * stored in the file (must not be zero). + * map[40]-map[43]: The number of bytes of time zone abbreviation strings stored in the file. + + * If map[4] is '2' or greater, the above is followed by a second instance + * of tzhead and a second instance of the data in which each coded transition + * time uses 8 rather than 4 chars, + * then a POSIX-TZ-environment-variable-style string for use in handling + * instants after the last transition time stored in the file + * (with nothing between the newlines if there is no POSIX representation for + * such instants). + + * If map[4] is '3' or greater, the above is extended as follows. + * First, the POSIX TZ string's hour offset may range from -167 + * through 167 as compared to the POSIX-required 0 through 24. + * Second, its DST start time may be January 1 at 00:00 and its stop + * time December 31 at 24:00 plus the difference between DST and + * standard time, indicating DST all year. */ if (map[4]!='1') { size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6); trans = zi+skip+44+44; diff --git a/src/time/clock.c b/src/time/clock.c index 6724012b9..1748b55ab 100644 --- a/src/time/clock.c +++ b/src/time/clock.c @@ -1,10 +1,13 @@ #include #include +#include clock_t clock() { struct timespec ts; + UNSUPPORTED_API_VOID(LITEOS_A); + if (__clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) return -1; diff --git a/src/time/clock_getres.c b/src/time/clock_getres.c index 81c670376..2685e0650 100644 --- a/src/time/clock_getres.c +++ b/src/time/clock_getres.c @@ -1,13 +1,48 @@ #include #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +#include +#include +#include "atomic.h" + +#ifdef VDSO_CGR_SYM + +static void *volatile vdso_cgr; + +static int cgr_init(clockid_t clk, struct timespec *ts) +{ + __get_vdso_info(); + void *p = __get_vdso_addr(VDSO_CGR_VER, VDSO_CGR_SYM); + int (*f)(clockid_t, struct timespec *) = + (int (*)(clockid_t, struct timespec *))p; + a_cas_p(&vdso_cgr, (void *)cgr_init, p); + return f ? f(clk, ts) : -ENOSYS; +} + +static void *volatile vdso_cgr = (void *)cgr_init; + +#endif +#endif + int clock_getres(clockid_t clk, struct timespec *ts) { + int r; +#if defined(VDSO_CGR_SYM) && defined(LINUX_MUSL_SUPPORTED) + int (*f)(clockid_t, struct timespec *) = + (int (*)(clockid_t, struct timespec *))vdso_cgr; + if (f) { + r = f(clk, ts); + if (!r) return r; + if (r == -EINVAL) return __syscall_ret(r); + } +#endif + #ifdef SYS_clock_getres_time64 /* On a 32-bit arch, use the old syscall if it exists. */ if (SYS_clock_getres != SYS_clock_getres_time64) { long ts32[2]; - int r = __syscall(SYS_clock_getres, clk, ts32); + r = __syscall(SYS_clock_getres, clk, ts32); if (!r && ts) { ts->tv_sec = ts32[0]; ts->tv_nsec = ts32[1]; diff --git a/src/time/clock_gettime.c b/src/time/clock_gettime.c index 3e1d0975b..fa6aee0be 100644 --- a/src/time/clock_gettime.c +++ b/src/time/clock_gettime.c @@ -33,10 +33,19 @@ static int cgt_time32_wrap(clockid_t clk, struct timespec *ts) static int cgt_init(clockid_t clk, struct timespec *ts) { +#ifdef LINUX_MUSL_SUPPORTED + __get_vdso_info(); + void *p = __get_vdso_addr(VDSO_CGT_VER, VDSO_CGT_SYM); +#else void *p = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM); +#endif #ifdef VDSO_CGT32_SYM if (!p) { +#ifdef LINUX_MUSL_SUPPORTED + void *q = __get_vdso_addr(VDSO_CGT32_VER, VDSO_CGT32_SYM); +#else void *q = __vdsosym(VDSO_CGT32_VER, VDSO_CGT32_SYM); +#endif if (q) { a_cas_p(&vdso_func_32, 0, q); p = cgt_time32_wrap; diff --git a/src/time/gettimeofday.c b/src/time/gettimeofday.c index 691f8e904..b19b8477b 100644 --- a/src/time/gettimeofday.c +++ b/src/time/gettimeofday.c @@ -2,8 +2,44 @@ #include #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +#include +#include +#include +#include "atomic.h" + +#ifdef VDSO_GTD_SYM + +static void *volatile vdso_gtd; + +static int gtd_init(struct timeval *tv, void *tz) +{ + __get_vdso_info(); + void *p = __get_vdso_addr(VDSO_GTD_VER, VDSO_GTD_SYM); + int (*f)(struct timeval *, void *) = + (int (*)(struct timval *, void *))p; + a_cas_p(&vdso_gtd, (void *)gtd_init, p); + return f ? f(tv, tz) : -ENOSYS; +} + +static void *volatile vdso_gtd = (void *)gtd_init; + +#endif +#endif + int gettimeofday(struct timeval *restrict tv, void *restrict tz) { +#if defined(VDSO_GTD_SYM) && defined(LINUX_MUSL_SUPPORTED) + int r; + int (*f)(struct timeval *, void *) = + (int (*)(struct timeval *, void *))vdso_gtd; + if (f) { + r = f(tv, tz); + if (!r) return r; + if (r == -EINVAL) return __syscall_ret(r); + } +#endif + struct timespec ts; if (!tv) return 0; clock_gettime(CLOCK_REALTIME, &ts); diff --git a/src/time/gmtime_r.c b/src/time/gmtime_r.c index 22aec2c2e..ededd7756 100644 --- a/src/time/gmtime_r.c +++ b/src/time/gmtime_r.c @@ -9,7 +9,7 @@ struct tm *__gmtime_r(const time_t *restrict t, struct tm *restrict tm) } tm->tm_isdst = 0; tm->__tm_gmtoff = 0; - tm->__tm_zone = __utc; + tm->__tm_zone = __gmt; return tm; } diff --git a/src/time/strftime.c b/src/time/strftime.c index cc53d5369..6ca73ea91 100644 --- a/src/time/strftime.c +++ b/src/time/strftime.c @@ -104,6 +104,19 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * val = tm->tm_yday+1; width = 3; goto number; + case 'k': + def_pad = '_'; + val = tm->tm_hour; + goto number; + case 'l': + def_pad = '_'; + val = tm->tm_hour; + if(!val) { + val = 12; + } else if (val > 12) { + val -= 12; + } + goto number; case 'm': val = tm->tm_mon+1; goto number; @@ -116,6 +129,9 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * case 'p': item = tm->tm_hour >= 12 ? PM_STR : AM_STR; goto nl_strcat; + case 'P': + item = tm->tm_hour >= 12 ? PM_STR_LOWER : AM_STR_LOWER; + goto nl_strcat; case 'r': item = T_FMT_AMPM; goto nl_strftime; @@ -145,6 +161,9 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * case 'W': val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7; goto number; + case 'v': + fmt = "%e-%b-%Y"; + goto recu_strftime; case 'V': val = week_num(tm); goto number; diff --git a/src/time/strptime.c b/src/time/strptime.c index c54a0d8c4..f4d6f9bd6 100644 --- a/src/time/strptime.c +++ b/src/time/strptime.c @@ -6,6 +6,89 @@ #include #include +int __getzonename(const char *restrict s, struct tm *restrict tm) +{ + const char *p = s; + struct tm old; + memcpy(&old, tm, sizeof(struct tm)); + /* Possible time zone names like +XXX or -XXX */ + if (*p == '+' || *p == '-') { + p++; + } + + /* The time zone name is adjacent to the offset second data, + * and the following symbol belongs to the offset second */ + while (*p && (*p != '+' && *p != '-' && *p != ' ')) { + p++; + } + + /* In the structure struct tm, tm_zone is declared as const char * type, so use static */ + static char buf[16] = {0}; + memset(buf, 0x0, sizeof(buf)); + int len = p - s; + memcpy(buf, s, len); + tm->__tm_zone = buf; + + /* Re-fetch local data, extract tm_isdst flag. */ + time_t t = mktime(&old); + struct tm *tmp = localtime(&t); + if (tmp) { + tm->tm_isdst = tmp->tm_isdst; + } + return len; +} + +int __getgmtoff(const char *restrict s, struct tm *restrict tm) +{ + const char *p = s; + int sign = 1; + int i; + int isexit = 0; + long m = 0; + long h = 0; + + /* The possible formats for time offset are HHMM(-HHMM) or HH:MM(-HH:MM) */ + if (*p == '-') { + sign = -1; + } + p++; + tm->__tm_gmtoff = 0; + + /* get hours */ + for (i=0; i<2 && *p; i++, p++) { + if (isdigit(*p)) { + h = h * 10 + (*p - 0x30); + } else { + p--; + isexit = 1; + break; + } + } + + if (!isexit) { + /* Possible time zone formats are HH:MM. */ + if (*p == ':') { + *p++; + } + + /* get minutes */ + for (i=0; i<2 && *p; i++, p++) { + if (isdigit(*p)) { + m = m * 10 + (*p - 0x30); + } else { + p--; + isexit = 1; + break; + } + } + } + + /* Convert hours and minutes to seconds */ + tm->__tm_gmtoff = sign * (h * 3600 + m * 60); + + return p - s; +} + char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm) { int i, w, neg, adj, min, range, *dest, dummy; @@ -59,11 +142,31 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri s = strptime(s, "%m/%d/%y", tm); if (!s) return 0; break; + case 'F': + s = strptime(s, "%Y-%m-%d", tm); + if (!s) { + return 0; + } + break; + case 'g': + dest = &tm->tm_year; + min = 0; + range = 99; + w = 2; + want_century = 0; + goto numeric_digits; + case 'G': + do { + ++s; + } while (isdigit(*s)); + continue; + case 'k': case 'H': dest = &tm->tm_hour; min = 0; range = 24; goto numeric_range; + case 'l': case 'I': dest = &tm->tm_hour; min = 1; @@ -90,6 +193,7 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri for (; *s && isspace(*s); s++); break; case 'p': + case 'P': ex = nl_langinfo(AM_STR); len = strlen(ex); if (!strncasecmp(s, ex, len)) { @@ -114,6 +218,21 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri s = strptime(s, "%H:%M", tm); if (!s) return 0; break; + case 's': { + time_t secs = 0; + if (!isdigit(*s)) { + return 0; + } + do { + secs *= 10; + secs += *s - '0'; + s++; + } while (isdigit(*s)); + if(localtime_r(&secs, tm) == NULL) { + return 0; + } + break; + } case 'S': dest = &tm->tm_sec; min = 0; @@ -123,6 +242,23 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri s = strptime(s, "%H:%M:%S", tm); if (!s) return 0; break; + case 'u': { + if (!isdigit(*s)) { + return 0; + } + int wday = 0; + int rulim = 7; + do { + wday *= 10; + wday += *s++ - '0'; + rulim /= 10; + } while ((wday * 10 < 7) && rulim && isdigit(*s)); + if (wday < 1 || wday > 7) { + return 0; + } + tm->tm_wday = wday % 7; + continue; + } case 'U': case 'W': /* Throw away result, for now. (FIXME?) */ @@ -135,6 +271,27 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri min = 0; range = 7; goto numeric_range; + case 'v': + if (!(s = strptime(s, "%e-%b-%Y", tm))) { + return 0; + } + break; + case 'V': { + int r = 0; + int rulim = 53; + if (!isdigit(*s)) { + return 0; + } + do { + r *= 10; + r += *s++ - '0'; + rulim /= 10; + } while ((r * 10 < 53) && rulim && isdigit(*s)); + if (r < 0 || r > 53) { + return 0; + } + continue; + } case 'x': s = strptime(s, nl_langinfo(D_FMT), tm); if (!s) return 0; @@ -154,6 +311,13 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri adj = 1900; want_century = 0; goto numeric_digits; + case 'z': + s += __getgmtoff((const char *)s, tm); + continue; + case 'Z': + tzset(); + s += __getzonename((const char *)s, tm); + continue; case '%': if (*s++ != '%') return 0; break; diff --git a/src/time/time.c b/src/time/time.c index ad0480f9e..77f146e14 100644 --- a/src/time/time.c +++ b/src/time/time.c @@ -1,8 +1,39 @@ #include #include "syscall.h" +#ifdef LINUX_MUSL_SUPPORTED +#include +#include +#include "atomic.h" + +#ifdef VDSO_TIME_SYM + +static void *volatile vdso_time; + +static time_t time_init(time_t *t) +{ + __get_vdso_info(); + void *p = __get_vdso_addr(VDSO_TIME_VER, VDSO_TIME_SYM); + time_t (*f)(time_t *) = + (time_t (*)(time_t *))p; + a_cas_p(&vdso_time, (void *)time_init, p); + return f ? f(t) : -ENOSYS; +} + +static void *volatile vdso_time = (void *)time_init; + +#endif +#endif time_t time(time_t *t) { +#if defined(VDSO_TIME_SYM) && defined(LINUX_MUSL_SUPPORTED) + time_t (*f)(time_t *) = + (time_t (*)(time_t *))vdso_time; + if (f) { + return f(t); + } +#endif + struct timespec ts; __clock_gettime(CLOCK_REALTIME, &ts); if (t) *t = ts.tv_sec; diff --git a/src/time/time_impl.h b/src/time/time_impl.h index f26d80053..44f84228d 100644 --- a/src/time/time_impl.h +++ b/src/time/time_impl.h @@ -1,3 +1,6 @@ +#ifndef __TIME_IMPL_H__ +#define __TIME_IMPL_H__ + #include hidden int __days_in_month(int, int); @@ -9,3 +12,6 @@ hidden int __secs_to_tm(long long, struct tm *); hidden void __secs_to_zone(long long, int, int *, long *, long *, const char **); hidden const char *__strftime_fmt_1(char (*)[100], size_t *, int, const struct tm *, locale_t, int); extern hidden const char __utc[]; +extern hidden const char __gmt[]; + +#endif diff --git a/src/time/timer_create.c b/src/time/timer_create.c index 4bef23905..09d3d1341 100644 --- a/src/time/timer_create.c +++ b/src/time/timer_create.c @@ -25,10 +25,12 @@ static void cleanup_fromsig(void *p) { pthread_t self = __pthread_self(); __pthread_tsd_run_dtors(); +#ifdef FEATURE_PTHREAD_CANCEL self->cancel = 0; - self->cancelbuf = 0; self->canceldisable = 0; self->cancelasync = 0; +#endif + self->cancelbuf = 0; __reset_tls(); longjmp(p, 1); } diff --git a/src/time/times.c b/src/time/times.c index c4a100f79..7ece8ec26 100644 --- a/src/time/times.c +++ b/src/time/times.c @@ -3,5 +3,9 @@ clock_t times(struct tms *tms) { +#ifdef __LITEOS_A__ + return syscall(SYS_times, tms); +#else return __syscall(SYS_times, tms); +#endif } diff --git a/src/time/utime.c b/src/time/utime.c index e7592b297..c251ab69d 100644 --- a/src/time/utime.c +++ b/src/time/utime.c @@ -2,9 +2,11 @@ #include #include #include +#include int utime(const char *path, const struct utimbuf *times) { + UNSUPPORTED_API_VOID(LITEOS_A); return utimensat(AT_FDCWD, path, times ? ((struct timespec [2]){ { .tv_sec = times->actime }, { .tv_sec = times->modtime }}) : 0, 0); diff --git a/porting/linux/user/src/trace/trace_marker.c b/src/trace/linux/trace_marker.c similarity index 99% rename from porting/linux/user/src/trace/trace_marker.c rename to src/trace/linux/trace_marker.c index 60ddf6e61..ccaa6c14e 100644 --- a/porting/linux/user/src/trace/trace_marker.c +++ b/src/trace/linux/trace_marker.c @@ -1,5 +1,6 @@ + /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -241,3 +242,4 @@ void trace_marker_count(uint64_t label, const char *message, int value) // close file descriptor of trace_marker close(trace_marker_fd); } + diff --git a/src/unistd/ctermid.c b/src/unistd/ctermid.c index 1612770af..446086e72 100644 --- a/src/unistd/ctermid.c +++ b/src/unistd/ctermid.c @@ -1,7 +1,9 @@ #include #include +#include char *ctermid(char *s) { + UNSUPPORTED_API_VOID(LITEOS_A); return s ? strcpy(s, "/dev/tty") : "/dev/tty"; } diff --git a/src/unistd/fdatasync.c b/src/unistd/fdatasync.c index 3895ae530..9ebd3aa9d 100644 --- a/src/unistd/fdatasync.c +++ b/src/unistd/fdatasync.c @@ -1,7 +1,9 @@ #include #include "syscall.h" +#include int fdatasync(int fd) { + UNSUPPORTED_API_VOID(LITEOS_A); return syscall_cp(SYS_fdatasync, fd); } diff --git a/src/unistd/getpid.c b/src/unistd/getpid.c index a6d4e6d1b..ccc62e770 100644 --- a/src/unistd/getpid.c +++ b/src/unistd/getpid.c @@ -1,7 +1,17 @@ #include +#include "pthread_impl.h" #include "syscall.h" +static pid_t __get_cached_pid() +{ + return __pthread_self()->pid; +} + pid_t getpid(void) { + pid_t pid = __get_cached_pid(); + if (pid != 0) { + return pid; + } return __syscall(SYS_getpid); } diff --git a/src/unistd/lchown.c b/src/unistd/lchown.c index ccd5ee025..7a7d02c08 100644 --- a/src/unistd/lchown.c +++ b/src/unistd/lchown.c @@ -1,9 +1,11 @@ #include #include #include "syscall.h" +#include int lchown(const char *path, uid_t uid, gid_t gid) { + UNSUPPORTED_API_VOID(LITEOS_A); #ifdef SYS_lchown return syscall(SYS_lchown, path, uid, gid); #else diff --git a/porting/liteos_a/user/src/unistd/format.c b/src/unistd/liteos_a/format.c similarity index 100% rename from porting/liteos_a/user/src/unistd/format.c rename to src/unistd/liteos_a/format.c diff --git a/src/unistd/pipe2.c b/src/unistd/pipe2.c index f24f74fb0..054850229 100644 --- a/src/unistd/pipe2.c +++ b/src/unistd/pipe2.c @@ -2,10 +2,13 @@ #include #include #include "syscall.h" +#include int pipe2(int fd[2], int flag) { if (!flag) return pipe(fd); + + UNSUPPORTED_API_VOID(LITEOS_A); int ret = __syscall(SYS_pipe2, fd, flag); if (ret != -ENOSYS) return __syscall_ret(ret); ret = pipe(fd); diff --git a/src/unistd/setegid.c b/src/unistd/setegid.c index e6da2573c..89dbaca71 100644 --- a/src/unistd/setegid.c +++ b/src/unistd/setegid.c @@ -1,8 +1,17 @@ #include #include "libc.h" #include "syscall.h" +#ifdef __LITEOS_A__ +#include "errno.h" +#endif int setegid(gid_t egid) { +#ifdef __LITEOS_A__ + if (egid == -1) { + errno = EINVAL; + return -1; + } +#endif return __setxid(SYS_setresgid, -1, egid, -1); } diff --git a/src/unistd/seteuid.c b/src/unistd/seteuid.c index ef8b9df43..8a69cd499 100644 --- a/src/unistd/seteuid.c +++ b/src/unistd/seteuid.c @@ -1,8 +1,17 @@ #include #include "syscall.h" #include "libc.h" +#ifdef __LITEOS_A__ +#include "errno.h" +#endif int seteuid(uid_t euid) { +#ifdef __LITEOS_A__ + if (euid == -1) { + errno = EINVAL; + return -1; + } +#endif return __setxid(SYS_setresuid, -1, euid, -1); } diff --git a/src/unistd/setxid.c b/src/unistd/setxid.c index 487c1a160..80004e78b 100644 --- a/src/unistd/setxid.c +++ b/src/unistd/setxid.c @@ -29,6 +29,10 @@ int __setxid(int nr, int id, int eid, int sid) /* ret is initially nonzero so that failure of the first thread does not * trigger the safety kill above. */ struct ctx c = { .nr = nr, .id = id, .eid = eid, .sid = sid, .ret = 1 }; +#ifdef __LITEOS_A__ + do_setxid(&c); +#else __synccall(do_setxid, &c); +#endif return __syscall_ret(c.ret); } diff --git a/src/unistd/tcgetpgrp.c b/src/unistd/tcgetpgrp.c index 50080c7e8..ec0429e4c 100644 --- a/src/unistd/tcgetpgrp.c +++ b/src/unistd/tcgetpgrp.c @@ -1,10 +1,12 @@ #include #include #include +#include pid_t tcgetpgrp(int fd) { int pgrp; + UNSUPPORTED_API_VOID(LITEOS_A); if (ioctl(fd, TIOCGPGRP, &pgrp) < 0) return -1; return pgrp; diff --git a/src/unistd/ttyname.c b/src/unistd/ttyname.c index 0f3e11411..049165b54 100644 --- a/src/unistd/ttyname.c +++ b/src/unistd/ttyname.c @@ -1,11 +1,13 @@ #include #include #include +#include char *ttyname(int fd) { static char buf[TTY_NAME_MAX]; int result; + UNSUPPORTED_API_VOID(LITEOS_A); if ((result = ttyname_r(fd, buf, sizeof buf))) { errno = result; return NULL; -- Gitee