linux 相关库函数与系统调用动态实现

//库函数

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 _strncmp(const char *, const char *, size_t);
int _strcmp(const char *, const char *);
int _memcmp(const void *, const void *, unsigned int);
char _toupper(char c);
void Memset(void *mem, unsigned char byte, unsigned int len)
{
    unsigned char *p = (unsigned 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 long) __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] - 10) + ‘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 == ‘\0‘)
            return 0;
    return 0;
}

int _strcmp(const char *s1, const char *s2)
{
    for ( ; *s1 == *s2; s1++, s2++)
        if (*s1 == ‘\0‘)
            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;

}

//系统调用

int _getuid(void);
int _unlink(const char *path);
int _mprotect(void * addr, unsigned long len, int prot);
int _puts(char *str);
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 _lseek(long, long, unsigned int);
void Exit(long);
void *_mmap(void *, unsigned long, unsigned long, unsigned long,  long, unsigned long);
int _munmap(void *, size_t);
long _open(const char *, unsigned long, long);
long _write(long, char *, unsigned long);
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 $10, %%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 $5, %%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 $60, %%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 flags, long mode)
{
    long ret;
    __asm__ volatile(
                     "mov %0, %%rdi\n"
                     "mov %1, %%rsi\n"
                     "mov %2, %%rdx\n"
                     "mov $2, %%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 $1, %%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 $0, %%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 $3, %%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;

}

原文地址:http://blog.51cto.com/haidragon/2134748

时间: 2024-11-02 04:42:56

linux 相关库函数与系统调用动态实现的相关文章

Linux 库函数与系统调用的关系与区别

上周总结了<C 标准库的基础 IO>,其实这些功能函数通过「系统调用」也能实现相应功能.这次文章并不是要详细介绍各系统调用接口的使用方法,而是要深入理解「库函数」与「系统」调用之间的关系和区别. 一.系统调用 系统调用,我们可以理解是操作系统为用户提供的一系列操作的接口(API),这些接口提供了对系统硬件设备功能的操作.这么说可能会比较抽象,举个例子,我们最熟悉的 hello world 程序会在屏幕上打印出信息.程序中调用了 printf() 函数,而库函数 printf 本质上是调用了系统

Linux使用静态库和动态库

Linux使用静态库和动态库 (一)库的概念 库是可以复用的代码,在一些大的项目中常常会用到库. 本质上说:库是一种可执行代码的二进制形式,可以被操作系统载入内存执行. 一般说库是说两种: 静态库:linux下.a文件.windows下.lib文件 动态库:linux下.so文件.windows下.dll文件 最近花了一些时间把linux下编译.链接等研究了一下,作为一个菜鸟记录并分享一蛤. (二)静态库与动态库 程序的编译运行要经过以下步骤: 1.源文件(.h .cpp等) 2.预编译 3.编

[翻译]-Linux上C++类的动态加载

摘要:本文是翻译文章,主要介绍了运行时重载C++类的技术,包括了Linux上的动态加载接口.C++类的动态加载技术点及实现.自动加载技术等.最后给出了两个应用案例及相关的源代码.   关键字:动态加载,C++类,Linux 原文链接:http://porky.linuxjournal.com:8080/LJ/073/3687.html   推荐: (原文)http://www.tldp.org/HOWTO/text/C++-dlopen (翻译)http://hi.baidu.com/clive

linux内核学习之四 系统调用

一  概念区分 提到linux系统调用,不得不区分几个比较容易混淆的概念: 系统调用:系统调用就是一种特殊的接口.通过这个接口,用户可以访问内核空间.系统调用规定了用户进程进入内核的具体位置. 应用程序接口(API,Application Programming Interface):是一些预定义的函数.API提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节. c库:c库实现了linux的主要API,包括标准的C库函数和系统调用.同时lin

【Linux&amp;Unix--exec 与 fock 系统调用】

个人学习整理,如有不足之处,请不吝指教.转载请注明:@CSU-Max 系列博文: Linux&Unix学习第一弹 -- 文件描述符与权限   Linux&Unix学习第二弹 -- 文件描述符与权限 进程和程序 简单的说,进程与应用程序的区别在于应用程序作为一个静态文件存储在计算机系统的硬盘等存储空间中,而进程则是处于动态条件下由操作系统维护的系统资源管理实体. (1)进程更能真实地描述并发,而程序不能. (2)进程由程序和数据两部分组成,进程是竞争计算机系统有限资源的基本单位,也是进程处理

Linux内核分析——扒开系统调用的三层皮(上)

马悦+原创作品转载请注明出处+<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.用户态.内核态和中断处理过程 1.用户通过库函数与系统调用联系起来. 2.在高执行级别下,代码可以执行特权指令,访问任意的物理地址,这种CPU执行级别就对应着内核态.而在相应的低级别执行状态下代码的掌控范围受到限制.只能在对应级别允许的范围内活动. 3.intel x86 CPU有四种不同的执行级别0-3.Linux只取两种,0级

Linux下静态库与动态库

一.基本概念 1.1.什么是库        在 windows 平台和 linux 平台下都大量存在着库. 本质上来说库是一种可执行的二进制代码(但不可以独立执行),可以被操作系统载入内存执行. 由于 windows 和 linux 的平台不同(主要是编译器.汇编器和连接器 的不同),因此二者库的二进制是不兼容的. 本文仅限于介绍 linux 下的库. 1.2. 库的种类 linux下的库有两种:静态库和共享库(动态库). 二者的不同点在于代码被载入的时刻不同: 静态库的代码在编译过程中已经被

Linux 静态链接库和动态连接库

(0)文件夹 VMware 下安装Ubuntu的吐血经历 零基础学习Shell编程 Linux下的makefile的妙用 Linux调试神器 -- gdb 十分钟学会Python的基本类型 Linux 静态链接库和动态连接库 一:静态链接库的应用  三步走~~~ ##g++ -c StaticMath.cpp ##ar -crv libstaticmath.a StaticMath.o ##g++ -o run test_a.cpp -L. -lstaticmath #[@sjs_37_33 l

linux下静态库和动态库一些东西

http://www.cnblogs.com/changefuture/archive/2011/12/22/2297460.html Linux  动态链接库和静态库示例 文件预览 文件目录树如下,如你所见,非常简单. libtest/ |-- lt.c |-- lt.h `-- test.c 代码 #lt.c /* lt.c * */ #include <stdio.h> void myprint(void) { printf("Linux library test!\n&quo