在Linux内核中链接共享库 [英] Linking shared library in linux kernel

查看:251
本文介绍了在Linux内核中链接共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改linux内核.

I would like to modify the linux kernel.

我想使用文件kernel/panic.c中共享库(.so文件)中的函数.

I would like to use functions from a shared library (an .so file) in file kernel/panic.c.

不幸的是,我不知道如何编译它.

Unfortunately I don't know how to compile it.

当我将其放入Makefile时,出现以下错误:

When I put it in to the Makefile I receive the following error:

ld: attempted static link of dynamic object.

是否可以将共享库文件放入Linux内核,还是需要重新编译我的库以获得目标文件.

Is there a way to put the shared library file to the Linux kernel or do I need to recompile my library to gain an object file.

推荐答案

不可能将共享库链接到内核代码中( ld-linux(8) ...)您应该考虑制作一个内核模块(并使用 modprobe(8)加载它).阅读可加载内核模块的操作方法.

It is not possible to link shared library into kernel code (ELF shared objects are a user-space thing, using ld-linux(8)...) You should consider making a kernel module (and use modprobe(8) to load it). Read Loadable Kernel Module HowTo.

内核模块*.ko在概念上类似于共享对象*.so,但是

kernel modules *.ko are conceptually similar to shared objects *.so but the linking mechanism is different.

顺便说一句,您通常应该避免编写内核代码,而应该更喜欢对应用程序代码进行编码.换句话说,修改内核通常不是一个好主意,并且对此表示反对.

BTW, you generally should avoid writing kernel code and should prefer coding application code. In other words, modifying the kernel is generally a bad idea and is frowned upon.

此外,内核空间中可用的API与用户空间API不同(后者扩展了 C标准库 POSIX 函数).例如,内核模块(和内核代码)没有(因此无法调用)fopenfprintffork;因此,不能调用fopenfprintffork.内核是一个独立的C应用程序.另外,内核代码不能使用任何浮点运算!

Also, the API available in kernel space is not the same as user space API (which extends the C standard library and POSIX functions). For example, kernel modules (and kernel code) don't have (so cannot call) fopen or fprintf or fork; the kernel is a freestanding C application. Also, kernel code cannot use any floating point operation!

Userland应用程序正在使用系统调用与内核进行交互. http://man7.org/linux/man-pages/man2/syscalls.2.html"rel =" nofollow noreferrer> syscalls(2)(而libc正在使用它们,例如用于printf system(3)).内核代码(包括内核模块)不能直接使用syscall(因为它们是内核提供的),请参见

Userland applications are interacting with the kernel using system calls listed in syscalls(2) (and the libc is using them, e.g. for printf or system(3)). Kernel code (including kernel modules) cannot use directly syscalls (since they are provided by the kernel, see syscalls(2)).

也请阅读> 高级Linux编程 (主要是关于应用程序编程)和 操作系统:三个简单的部分 (以获得有关操作系统的更广阔的视野.

Read also Advanced Linux Programming (mostly about application programming) and Operating Systems: Three Easy Pieces (to get a broader view about OSes).

这篇关于在Linux内核中链接共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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