Library functions
void Memset (void *mem, unsigned char byte, unsigned int len), void _memcpy (void *, void *, unsigned int), int _printf (char * , ...); char * itoa (Long, char *), char * ITOX (Long, char *), int _puts (char *), int _puts_nl (char *); size_t _strlen (char *); int _str NCMP (const char *, const char *, size_t), int _strcmp (const char *, const char *), int _memcmp (const void *, const void *, U nsigned int); Char _toupper (char c); void Memset (void *mem, unsigned char byte, unsigned int len) {unsigned char *p = (UN Signed char *) mem; int i = len; while (i--) {*p = byte; p++; }}void _memcpy (void *dst, void *src, unsigned int len) {int i; unsigned char *s = (unsigned char *) src; unsigned char *d = (unsigned char *) DST; for (i = 0; i < len; i++) {*d = *s; s++, d++; }}int _printf (char *fmt, ...) {int in_p; unsigned long DWORD; unsigned int word; Char numbuf[26] = {0}; __builtin_va_list alist; In_p; __builtin_va_start ((alist), (FMT)); In_p = 0; while (*FMT) {if (*fmt!= '% ' &&!in_p) {_write (1, FMT, 1); in_p = 0; } else if (*fmt!= '% ') {switch (*FMT) {case ' s ': DWORD = (Unsigned lon g) __builtin_va_arg (alist, long); _puts ((char *) DWORD); Break Case ' u ': Word = (unsigned int) __builtin_va_arg (alist, int); _puts (itoa (Word, numbuf)); Break Case ' d ': Word = (unsigned int) __builtin_va_arg (alist, int); _puts (itoa (Word, numbuf)); Break Case ' X ': DWORD = (unsigned long) __builtin_va_arg (alist, long); _puts (Itox (DWORD, numbuf)); Break Default: _write (1, FMT, 1); Break } in_p = 0; } else { In_p = 1; } fmt++; } return 1;} char * ITOA (long x, char *t) {int i; Int J; i = 0; do {T[i] = (x% 10) + ' 0 '; x/= 10; i++; } while (x!=0); T[i] = 0; for (J=0; J < I/2; J + +) {T[j] ^= t[i-j-1]; T[i-j-1] ^= t[j]; T[J] ^= t[i-j-1]; } return t;} char * ITOX (long x, char *t) {int i; Int J; i = 0; do {T[i] = (x% 16); /* Char Conversion */if (T[i] > 9) t[i] = (T[i]-ten) + ' a '; else T[i] + = ' 0 '; x/= 16; i++; } while (x! = 0); T[i] = 0; for (J=0; J < I/2; J + +) {T[j] ^= t[i-j-1]; T[i-j-1] ^= t[j]; T[J] ^= t[i-j-1]; } return t;} int _puts (char *str) {_write (1, str, _strlen (str)); _fsync (1); return 1;} int _puts_nl (char *str) {_write (1, str, _strlen (str)); _write (1, "\ n", 1); _fsync (1); return 1;} size_t _strlen (char *s) { size_t sz; for (sz=0;s[sz];sz++); return sz;} int _strncmp (const char *S1, const char *S2, size_t N) {for (; n > 0; s1++, s2++,--n) if (*s1! = *s2) Return ((* (* (unsigned char *) S1 < * (unsigned char *) s2)-1: + 1); else if (*s1 = = ') ' return 0; return 0;} int _strcmp (const char *S1, const char *s2) {for (; *s1 = = *s2; s1++, s2++) if (*s1 = = ') ' Return 0; Return ((* (* (unsigned char *) S1 < * (unsigned char *) s2)-1: + 1);} int _memcmp (const void *S1, const void *S2, unsigned int n) {unsigned char U1, U2; for (; n--; s1++, s2++) {u1 = * (unsigned char *) s1; U2 = * (unsigned char *) s2; if (u1! = U2) {return (U1-U2); }}}char _toupper (char c) {if (C >= ' a ' && c <= ' z ') return (c = C + ' A '-' a '); return c;}
//System call
int _getuid (void), int _unlink (const char *path), int _mprotect (void * addr, unsigned long len, int prot); int _puts (char *st r); int _puts_nl (char *str); int _fsync (int fd); Long _ptrace (long request, long pid, void *addr, void *data); int _prctl (Long option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5); int _fstat (long, void *); long _ls Eek (long, long, unsigned int), void Exit (long), void *_mmap (void *, unsigned long, unsigned long, unsigned long, long, unsi gned long); int _munmap (void *, size_t); Long _open (const char *, unsigned long, long); Long _write (Long, char *, unsigned lo NG); int _read (Long, char *, unsigned long), int _getdents64 (unsigned int fd, struct Linux_dirent64 *dirp, unsigned int count), int _rename (const char *, const char *), int _close (unsigned int), int _gettimeofday (struct timeval *, struct timezone *); int _getuid (void) {unsigned long ret; __asm__ volatile ("mov $102,%rax\n" "Syscall"); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; int _unlink (const char *path) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "Mov $87,%%rax\n" "Syscall":: "G" ( path)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; int _mprotect (void * addr, unsigned long len, int prot) {unsigned long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "mov%2,%%rdx\n" "mov $ $,%%rax\n" "Syscall":: "G" (addr), "G" (Len), "G" (prot)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; int _puts (char *str) {_write (1, str, _strlen (str)); _fsync (1); return 1;} int _puts_nl (char *str) {_write (1, str, _strlen (str)); _write (1, "\ n", 1); _fsync (1); return 1;} int _fsync (int fd) {long ret; __asm__ volatile ( "Mov%0,%%rdi\n" "Mov $74,%%rax\n" "Syscall":: "G" (FD)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; Long _ptrace (long request, long pid, void *addr, void *data) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov% 2, %%rdx\n "" mov%3,%%r10\n "" Mov $101,%%rax\n "" Syscall ":: "G" (Request), "G" (PID), "G" (addr), "G" (data); ASM ("mov%%rax,%0": "=r" (ret)); return ret;} int _prctl (long option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov% 2, %%rdx\n "" mov%3,%%r10\n "" Mov $157,%%rax\n "" syscall\n ":: "G" (option), "G" (ARG2),"G" (Arg3), "G" (ARG4), "G" (ARG5)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; int _fstat (long fd, void *buf) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov $ $, %%rax\n "" Syscall "::" G "(FD)," G "(BUF)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; Long _lseek (long fd, long offset, unsigned int whence) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov% 2, %%rdx\n "" Mov $8,%%rax\n "" Syscall "::" G "(FD)," G "(offset)," G "(whence)); ASM ("mov%%rax,%0": "=r" (ret)); return ret;} void Exit (Long status) {__asm__ volatile ("mov%0,%%rdi\n" "Mov $,%%rax\n" " Syscall "::" R "(status));} void *_mmap (void *addr, unsigned long len, unsigned long prot, unsigned long flags, long FD, unsigned lonG off) {long mmap_fd = FD; unsigned long mmap_off = off; unsigned long mmap_flags = flags; unsigned long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov% 2, %%rdx\n "" mov%3,%%r10\n "" Mov%4,%%r8\n "" mov%5,%%r9\n " "Mov $9,%%rax\n" "syscall\n":: "G" (addr), "G" (Len), "G" (Prot), "G" (Flags), "G" ( MMAP_FD), "G" (Mmap_off)); ASM ("mov%%rax,%0": "=r" (ret)); return (void *) ret; int _munmap (void *addr, size_t len) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov $11 ,%%rax\n "Syscall":: "G" (addr), "G" (Len)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; Long _open (const char *path, unsigned long, long) {long ret; __asm__ volatile ("mov%0,%%rdi\n "" mov%1,%%rsi\n "" mov%2,%%rdx\n "" Mov $%%rax\ N "" Syscall "::" G "(Path)," G "(Flags)," G "(mode)); ASM ("mov%%rax,%0": "=r" (ret)); return ret;} Long _write (Long fd, char *buf, unsigned long len) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov% 2, %%rdx\n "" mov $ $,%%rax\n "" Syscall "::" G "(FD)," G "(BUF)," G "(Len)); ASM ("mov%%rax,%0": "=r" (ret)); return ret;} int _read (long fd, char *buf, unsigned long len) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov% 2, %%rdx\n "" Mov $,%%rax\n "" Syscall "::" G "(FD)," G "(BUF)," G "(Len)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; int _getdents64 (unsigned int fd, struct LINux_dirent64 *dirp, unsigned int count) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov% 2, %%rdx\n "" Mov $217,%%rax\n "" Syscall "::" G "(FD)," G "(DIRP)," G "(count)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; int _rename (const char *old, const char *new) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov%1,%%rsi\n" "Mov $82 ,%%rax\n "Syscall":: "G" (old), "G" (new)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret; int _close (unsigned int fd) {long ret; __asm__ volatile ("mov%0,%%rdi\n" "mov $ $,%%rax\n" "Syscall "::" G "(FD)); return (int) ret; int _gettimeofday (struct timeval *tv, struct timezone *tz) {long ret; __asm__ volatile ("mov% 0,%%rdi\n "" mov%1,%%rsi\n "" Mov $96,%%rax\n "" Syscall "::" G "( TV), "G" (TZ)); ASM ("mov%%rax,%0": "=r" (ret)); return (int) ret;
Linux related library functions and system calls dynamically implemented