![](/img/trans.png)
[英]How to prevent “error: 'symbol' undeclared here” despite EXPORT_SYMBOL in a Linux kernel module?
[英]How to export a struct between two kernel modules using EXPORT_SYMBOL or equivalent?
我有一個內核模塊,其結構如下:
struct test {
int a;
int b;
.....
}
我已經創建了這個結構的一個實例數組:
struct test foo[8];
我想使用EXPORT_SYMBOL
導出此結構或數組“foo”,並在其他內核模塊中訪問foo[0].a
。
我試過EXPORT_SYMBOL(foo);
來自provider模塊和extern struct test * foo;
在接收器模塊但我無法訪問變量。 請指出我犯錯的地方。
這是一些代碼:
內核模塊1:
#include <....>
#include "test_config.h"
....
MODULE_LICENSE("GPL");
struct test {
int a;
int b;
.....
}
test_t foo[8];
//EXPORT_SYMBOL(foo);
/*Code to create sysctl variables out of these members of the struct test*/
int init_module(void)
{
printk(KERN_INFO "Hello World\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye Cruel World\n");
}
Kerne模塊2:
#include <linux/module.h>
#include <linux/kernel.h>
#include "test_config.h"
int init_module(void)
{
test_t foo[8];
printk ("Value of foo is :: %d\n", foo[0].a);
foo[0].a++;
printk(KERN_INFO "Hello Again World\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye Again Cruel World\n");
}
這是帶有結構定義的頭文件:
#ifndef __TEST_CONFIG
#define __TEST_CONFIG
typedef struct test
{
int a;
int b
int c;
int d;
float e;
}test_t;
#endif
在模塊A中你已經采取了
struct test foo[8];
並使其成為
EXPORT_SYMBOL(foo);
因此,要在另一個模塊B中使用它,您需要添加
extern struct test foo[8];
並確保在模塊B中使用它時,應首先加載模塊A.
如果你不想導出整個數組但只想導出指針
在模塊中
struct test foo[8];
struct *test temp = &foo(0);
EXPORT_SYMBOL(temp);
在模塊B中
extern struct *test temp;
並將memorys作為temp[0].a
還有一個例子
請參閱http://lxr.free-electrons.com/source/sound/core/init.c?v=2.6.35;a=arm#L48
48 struct snd_card *snd_cards[SNDRV_CARDS];
49 EXPORT_SYMBOL(snd_cards);
因此它被用作
281 extern struct snd_card *snd_cards[SNDRV_CARDS];
在http://lxr.free-electrons.com/source/include/sound/core.h?v=2.6.35#L281
最后更新
#include <....>
#include "test_config.h"
....
MODULE_LICENSE("GPL");
test_t foo[8];
EXPORT_SYMBOL(foo);
int init_module(void)
{
printk(KERN_INFO "Hello World\n");
foo[0].a = 10; // set some value.
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye Cruel World\n");
}
現在模塊2
#include <linux/module.h>
#include <linux/kernel.h>
#include "test_config.h"
extern test_t foo[8];
int init_module(void)
{
printk ("Value of foo is :: %d\n", foo[0].a); // it should print 10
printk(KERN_INFO "Hello Again World\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye Again Cruel World\n");
}
頭文件。
#ifndef __TEST_CONFIG
#define __TEST_CONFIG
typedef struct test
{
int a;
int b
int c;
int d;
float e;
}test_t;
#endif
應首先加載模塊1,然后對模塊2進行加密。
該答案給出Mr.32有點不解決我的問題。 所以我在不使用結構的情況下實現了這個,並為每個成員a,b,c創建了單獨的數組,以便繼續我的任務......
在做了一些實驗后,我能夠達到導出結構的原始要求。
我修改了頭文件,如下所示:
#ifndef __TEST_CONFIG
#define __TEST_CONFIG
struct test
{
int a;
int b
int c;
int d;
float e;
};
extern struct test foo[8];
#endif
執行此操作后,我在提供程序模塊中將此結構定義為:
struct test foo[8];
EXPORT_SYMBOL(foo);
並在接收器模塊中包含標頭並將其引用為:
extern struct test foo[8];
通過執行這些更改,我可以通過執行foo[0].a;
來訪問第二個模塊中第一個模塊的值foo[0].a;
。
在內核模塊編譯期間,Module.symvers文件在同一目錄中生成(默認情況下與源文件一起)。 如果在具有不同目錄的某些不同源中導出和導入符號,則需要將Module.symvers文件復制到其他源文件的目錄中。 有時為了清理構建,這些文件將被刪除。 如果導出符號,則刪除文件將無法解析外部符號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.