简体   繁体   English

Linux裸系统调用,而不是glibc

[英]Linux bare system calls, not glibc

I'm reading an article that explains how to call bare syscalls without passing through glibc. 我正在阅读一篇文章,解释如何在不通过glibc的情况下调用裸系统调用。 To call chmod and exit , use: 要调用chmodexit ,请使用:

#include <linux/unistd.h>
_syscall2(int,chmod,char*,f,int,m)
_syscall1(int,exit,int,r)

My gcc complains about them. 我的gcc抱怨他们。 What are their use, how do they work? 它们的用途是什么,它们如何工作?

$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0
$ gcc e.c 
e.c:2:15: error: unknown type name ‘setresuid’; did you mean ‘__NR_setresuid’?
 _syscall3(int,setresuid,int,r,int,e,int,s)
               ^~~~~~~~~
               __NR_setresuid
e.c:2:29: error: unknown type name ‘r’
 _syscall3(int,setresuid,int,r,int,e,int,s)
                             ^
e.c:2:35: error: unknown type name ‘e’
 _syscall3(int,setresuid,int,r,int,e,int,s)
                                   ^
e.c:2:41: error: unknown type name ‘s’
 _syscall3(int,setresuid,int,r,int,e,int,s)
                                         ^

Your article is probably obsolete. 你的文章可能已经过时了。

If you code in C, there is no reason to avoid using the syscalls(2) (notice the plural) as documented. 如果您使用C编码,则没有理由避免使用系统调用(2) (注意复数)。 Be also aware of the vdso(7) . 还要注意vdso(7) You could use some other C standard library than the glibc (eg musl-libc , dietlibc , etc...) and you might (but that is not recommended) statically link it. 您可以使用一些其他C标准库而不是glibc (例如musl-libcdietlibc等等),您可能(但不建议)静态链接它。

You might use syscall(2) (notice the singular) instead. 您可以使用系统调用(2) (注意单数)。 I see no reason to do that, eg use read(2) or mmap(2) without syscall . 我认为没有理由这样做,例如在没有syscall情况下使用read(2)mmap(2)

The Assembly HowTo might be an interesting read (beware, it might be too 32 bits centric, most Linux PCs today are 64 bits x86-64). 程序集HowTo可能是一个有趣的读物(注意,它可能是32位中心,今天大多数Linux PC是64位x86-64)。

See also osdev.org 另见osdev.org

BTW, some old Unixes (eg Solaris) had a libsys providing just the syscalls, and their libc linked to it. 顺便说一下,一些旧的Unix(例如Solaris)有一个libsys 提供系统调用,并且它们的libc链接到它。 I would like a libsys too! 我也想要一个libsys But on current Linux systems, it does not really matter, since almost every process (running some dynamically linked ELF executable) is mmap(2) -ing, after ld-linux.so(8) , several segments and sections of your libc.so.6 ; 但是在当前的Linux系统上,它并不重要,因为几乎每个进程(运行一些动态链接的 ELF可执行文件)都是mmap(2) -ing,在ld-linux.so(8)之后 ,你的libc.so.6几个段和部分libc.so.6 ; for details, read Drepper's How to write a shared library (since it also explains in details how shared libraries actually work). 有关详细信息,请阅读Drepper的“ 如何编写共享库” (因为它还详细说明了共享库的实际工作方式)。 Use also pmap(1) on some running process (eg pmap $$ in a shell). 在一些正在运行的进程上也使用pmap(1) (例如shell中的pmap $$ )。

Some rare syscalls (eg userfaultfd(2) today 2Q2019) are not known by the glibc . 一些罕见的系统调用(例如,今天2Q2019的userfaultfd(2) )不知道glibc They are an exception, because most system calls are wrapped by your libc (the wrapping usually just deals with errno(3) setting on failure). 它们是一个例外,因为大多数 系统调用都是由你的libc包装的(包装通常只是处理失败时的errno(3)设置)。 Be aware of strace(1) . 注意strace(1)

And you also should read Operating Systems: Three Easy Pieces (it is a freely downloadable book, explaining the role of, and reason for, system calls) 你还应该阅读操作系统:三个简单的部分 (这是一本免费下载的书,解释了系统调用的作用和原因)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM