编译 C++在没有运行库的Linux下

发布于 2024-11-02 07:57:22 字数 777 浏览 5 评论 0原文

我最近开始探索生成的代码使用 C++ 运行时库的方式。

大多数情况下,我很好奇,但我也想评估开发用 C++ 启动内核所需的最少内容所需的工作量。

所以我开始实现自己的运行时库,但我有一个小问题。

int main(int argc, char **argv)
{
  return 0;
}

使用以下命令编译它:

$ g++ -ffreestand -nostdlib -fno-builtin -fno-rtti -fno-exceptions -c main.cpp

我收到此警告:

/usr/bin/ld: 警告: 找不到入口符号 _start;默认为 00000000080480b8

然后,当我尝试执行生成的二进制文件时,出现“分段错误”。 我尝试编译“main.cpp”和一个ASM 文件。

[global _start]
[extern main]

_start:
  call main

使用“ld”手动链接目标文件,我没有收到警告,但二进制文件仍然引发“分段错误”。

我想我错过了一些东西。可能需要在调用“main”之前和之后完成某些操作,例如系统 C 库在“__libc_start_main”中所做的事情。

另外,如果有人推荐我应该阅读的有关该主题的网站、文档或书籍,我将非常感激。

谢谢,

帕特里克

I have recently started to explore the way that the C++ runtime library is used by the generated code.

Mostly I am very curious, but I also want to evaluate the amount of work needed to develop the minimum required stuff to start a kernel in C++.

So I started to implement my own runtime library, but I have a small issue.

int main(int argc, char **argv)
{
  return 0;
}

Compiling this with the following command:

$ g++ -ffreestanding -nostdlib
-fno-builtin -fno-rtti -fno-exceptions -c main.cpp

I get this warning:

/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080480b8

Then when I try to execute the binary produced I get a "Segmentation fault".
I have tried to compile "main.cpp" and an ASM file.

[global _start]
[extern main]

_start:
  call main

Linking the object files manually with "ld", I don't have the warning but the binary is still raising a "Segmentation fault".

I suppose I am missing something. Probably something that has to be done before and after the call to "main" as the system C library does in "__libc_start_main" for instance.

Also, if someone has any recommendation of websites, documentation or books about this topic that I should read, I would really appreciate it.

Thanks,

Patrick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最后的乘客 2024-11-09 07:57:22

好的,感谢 QuantumMechanic 的链接,我设法找到了问题:
http://www.muppetlabs.com/~breadbox/software/tiny/teensy .html

我刚刚忘记了linux编程的基础知识,更重要的是程序结束是如何处理的。

基本上,我需要生成系统调用中断“exit”来处理程序的结束。

[BITS 32]

[global _start]
[extern main]

_start:
  call main
  mov ebx, eax  ; Move the returned value in the register used as argument of exit()
  mov eax, 1    ; Indicates the id of the syscall to execute
  int 0x80      ; Triggers the syscall interrupt

所以现在我可以使用我自己的 RTL 在 Linux 上编译任何 C++ 程序来进行一些测试。

请注意,如果永远不会到达“main”函数的末尾,则内核中将不需要它。

谢谢 !

Okay so thanks to QuantumMechanic's link I managed to find the problem:
http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html

I just forgot my basics in linux programming, more importantly how program end is handled.

Basically, I needed to generate the syscall interrupt "exit" to handle the end of the program.

[BITS 32]

[global _start]
[extern main]

_start:
  call main
  mov ebx, eax  ; Move the returned value in the register used as argument of exit()
  mov eax, 1    ; Indicates the id of the syscall to execute
  int 0x80      ; Triggers the syscall interrupt

So now I can compile any C++ program on linux using my own RTL to make some tests.

Note that it won't be needed in the kernel should the end of the "main" function never be reached.

Thanks !

以歌曲疗慰 2024-11-09 07:57:22

如果您愿意使用 gcc 安装的一小部分 (200K),则可以链接到 libgcc_s。这将为您提供让程序执行静态初始化并调用 main 所需的所有代码。

If you're willing to use a small (200K) portion of gcc's install, you can link in libgcc_s. This will give you all the code you need to have your program do static init and call main.

眼眸里的那抹悲凉 2024-11-09 07:57:22

当您构建内核时,您必须考虑它如何启动。通常这是引导加载程序的责任,而且它们远非标准。这肯定与启动 Linux 应用程序不同。因此,当您无论如何都要忽略这种启动方式时,过多担心 _start 并不明智。

When you're building a kernel, you'll have to consider how it starts. Typically that's the bootloader's responsibility, and they're far from standard. It's certainly unlike starting Linux apps. Therefore it's not really sensible to worry too much about _start when you're going to ignore that way of starting anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文