LINUX:是否可以编写不依赖于libc库的工作程序? [英] LINUX: Is it possible to write a working program that does not rely on the libc library?

查看:372
本文介绍了LINUX:是否可以编写不依赖于libc库的工作程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否可以在可执行的 C 程序语言中编写程序,尽管不使用单个库调用,甚至没有退出()?

I wonder if I could write a program in the C-programming language that is executable, albeit not using a single library call, e.g. not even exit()?

如果是这样,它显然不会依赖于库(libc,ld-linux)。

If so, it obviously wouldn't depend on libraries (libc, ld-linux) at all.

推荐答案

我怀疑你可以写这样的东西,但是最终需要一个无休止的循环,因为你不能要求操作系统退出你的过程你不能做任何有用的事情。

I suspect you could write such a thing, but it would need to have an endless loop at the end, because you can't ask the operation system to exit your process. And you couldn't do anything useful.

从编译ELF程序开始,查看ELF规范,并组合标题,程序段和程序所需的其他部分。内核将加载您的代码并跳转到一些初始地址。你可以在那里放一个无尽的循环。但是不知道一些汇编人员,从一开始就没有希望。

Well start with compiling an ELF program, look into the ELF spec and craft together the header, the program segments and the other parts you need for a program. The kernel would load your code and jump to some initial address. You could place an endless loop there. But without knowing some assembler, that's hopeless from the start on anyway.

start.S 文件可能作为起点。尝试更改它,以便您可以从其中组装独立的可执行文件。那个start.S文件是所有ELF应用程序的入口点,并且是调用 __ libc_start_main 的调用 main 。你只是改变它,以适应你的需要。

The start.S file as used by glibc may be useful as a start point. Try to change it so that you can assemble a stand-alone executable out of it. That start.S file is the entry point of all ELF applications, and is the one that calls __libc_start_main which in turn calls main. You just change it so it fits your needs.

好的,那是理论上的。但现在呢,有什么实际用途呢?

Ok, that was theoretical. But now, what practical use does that have?

嗯。有一个名为 libgloss 的库,为在嵌入式系统上运行的程序提供了一个最小的界面。 newlib C库使用该库作为其系统调用接口。一般的想法是libgloss是C库和操作系统之间的层次。因此,它还包含操作系统跳入的启动文件。这两个库都是GNU binutils项目的一部分。我已经使用它们来为另一个操作系统和另一个处理器做接口,但似乎没有一个Linux的libgloss端口,所以如果你打电话给系统,你必须自己做,因为其他人已经说过。

Well. There is a library called libgloss that provides a minimal interface for programs that are meant to run on embedded systems. The newlib C library uses that one as its system-call interface. The general idea is that libgloss is the layer between the C library and the operation system. As such, it also contains the startup files that the operation system jumps into. Both these libraries are part of the GNU binutils project. I've used them to do the interface for another OS and another processor, but there does not seem to be a libgloss port for Linux, so if you call system calls, you will have to do it on your own, as others already stated.

绝对可以用C编程语言编写程序。 linux内核是一个很好的例子。但也可以使用用户程序。但是最低限度要求的是运行时库(如果你想做任何严重的事情)。这样一个包含真正的基本功能,如memcpy,基本宏等。 C标准具有独特的一致性模式,称为独立式,它只需要非常有限的一组功能,也适用于内核。实际上,我有关于x86汇编器的线索,但是我已经尝试了一个非常简单的C程序:

It is absolutely possible to write programs in the C programming language. The linux kernel is a good example of such a program. But also user programs are possible. But what is minimally required is a runtime library (if you want to do any serious stuff). Such one would contain really basic functions, like memcpy, basic macros and so on. The C Standard has a special conformance mode called freestanding, which requires only a very limited set of functionality, suitable also for kernels. Actually, i have no clue about x86 assembler, but i've tried my luck for a very simple C program:

/* gcc -nostdlib start.c */
int main(int, char**, char**);

void _start(int args)
{
    /* we do not care about arguments for main. start.S in 
     * glibc documents how the kernel passes them though.
     */
    int c = main(0,0,0);

    /* do the system-call for exit. */
    asm("movl   %0,%%ebx\n" /* first argument */
        "movl   $1,%%eax\n" /* syscall 1 */
        "int    $0x80"      /* fire interrupt */
        : : "r"(c) :"%eax", "%ebx");
}

int main(int argc, char** argv, char** env) {
    /* yeah here we can do some stuff */
    return 42;
}

我们很高兴,它实际上是编译并运行:)

We're happy, it actually compiles and runs :)

这篇关于LINUX:是否可以编写不依赖于libc库的工作程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆