如何编译 glibc 以在没有操作系统的情况下使用
我想将 glibc 的函数编译为一个目标文件,然后将其链接到我在没有任何操作系统的计算机上运行的程序。某些功能,例如 open
,我希望以 ENOSYS
失败。其他函数我将自己编写,例如 putchar
,然后让 glibc 使用它自己的这些函数(例如 printf
)。我还想使用不需要文件系统或进程管理系统或类似系统的函数,例如 strlen
。我该怎么做?
I would like to compile the functions of glibc to an object file which will then be linked to a program which I am running on a computer without any operating system. Some functions, such as open
, I want to just fail with ENOSYS
. Other functions I will write myself, such as putchar
, and then have glibc use those functions in it's own (like printf
). I also want to use functions that don't need a file system or process management system or anything like that, such as strlen
. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数 C 库严重依赖内核,因此移植它们是不合理的。但由于其中大部分不需要实现,因此您可以轻松地使用一些存根和 gcc 内置函数的原型。
您可以使用弱符号轻松实现存根:
然后定义原型变得微不足道:
实际功能可能是:
如果您需要一些不平凡的功能,例如
printf
,请从 uClibc 而不是 glibc 获取它(或其他一些较小的实现)。Most C libraries rely on the kernel heavily, so it's not reasonable to port 'em. But since most of it doesn't need to be implemented, you can get away easily with a few prototypes for stubs and gcc builtins.
You can implement stubs easily using weak symbols:
Then defining the prototypes becomes trivial:
And the actual functions could be:
If you need some non-trivial function, like
printf
, take it from uClibc instead of glibc (or some other smaller implementation).