我在 C 中有这个 POC,它在自定义部分中保存了一些结构,然后迭代这些结构,显示它们的内容。
#include <stdio.h>
char a, b, c;
struct counter_info {
int counter;
char *name;
} __attribute__((packed));
#define __PUT_STUFF_IN_SECTION(_name) \
do{ \
static struct counter_info __counter_info_##_name \
__attribute((__section__("counters"))) \
__attribute((__used__)) = { \
.name = #_name, \
.counter = 0, \
}; \
}while(0)
extern struct counter_info __start_counters;
extern struct counter_info __stop_counters;
int main(int argc, char **argv){
printf("Start %p\n", &__start_counters);
__PUT_STUFF_IN_SECTION(a);
__PUT_STUFF_IN_SECTION(b);
__PUT_STUFF_IN_SECTION(c);
struct counter_info *iter = &__start_counters;
for(; iter < &__stop_counters; ++iter){
printf("Name: %s | Counter: %d.\n", iter->name, iter->counter);
}
printf("End %p\n", &__stop_counters);
return 0;
}
输出:
Name: c | Counter: 0.
Name: b | Counter: 0.
Name: a | Counter: 0.
输出符合预期,所以我尝试在内核模块中做同样的事情:
你好-1.c
#include <linux/module.h>
#include <linux/kernel.h>
char a, b, c;
struct counter_info {
int counter;
char *name;
} __attribute__((packed));
#define __PUT_STUFF_IN_SECTION(_name) \
do{ \
static struct counter_info __counter_info_##_name \
__attribute((__section__("counters"))) \
__attribute((__used__)) = { \
.name = #_name, \
.counter = 0, \
}; \
}while(0)
extern struct counter_info __start_counters;
extern struct counter_info __stop_counters;
int init_module(void){
__PUT_STUFF_IN_SECTION(a);
__PUT_STUFF_IN_SECTION(b);
__PUT_STUFF_IN_SECTION(c);
return 0;
}
void cleanup_module(void){
struct counter_info *iter = &__start_counters;
for(; iter < &__stop_counters; ++iter){
printk(KERN_INFO "Name: %s | Counter: %d.\n", iter->name, iter->counter);
}
}
生成文件:
obj-m += hello-1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
但是我在编译模块时收到了这些警告:
WARNING: "__stop_counters" [/media/sf_procmon/procmon_kmodule/test/hello-1.ko] undefined!
WARNING: "__start_counters" [/media/sf_procmon/procmon_kmodule/test/hello-1.ko] undefined!
我的问题是:为什么不起作用以及我应该如何在 LKM 中使用 section 属性?
编辑:
我看到了这个答案Initialize global array of function pointers at either compile-time, or run-time before main()我也试过这样做:
生成文件
ccflags-y := -Wl,-Tlinkerscript.ld
obj-m += hello-1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
linkerscript.ld
SECTIONS
{
.rel.rodata.counters : {
PROVIDE(start_counters = .);
*(counters)
PROVIDE(stop_counters = .);
}
}
INSERT AFTER .text;
但我不断收到相同的警告。我不确定我是否对链接描述文件做错了什么,或者这不是我的问题的解决方案。
编辑:
我正在编辑我的问题,希望有人能给我一个解决方法。在编译时,一些结构被声明并填充数据。每个结构都在 block 中声明,因此我无法通过名称访问它们,也无法在外部声明它们。我也不知道结构的确切数量,因为它可以从编译更改为编译。我需要的是一种访问它们的方法(遍历它们)。实际上,我不关心结构是保存在一个部分中还是使用其他魔法,只要我可以迭代它们。
最佳答案
这对我有用:
生成文件
obj-m := example.o
example-y += hello.o
ldflags-y += -T$(M)/layout.lds
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
layout.lds
SECTIONS
{
.counters : {
__start_counters = . ;
*(.counters)
__stop_counters = . ;
}
}
你好.c
#include <linux/module.h>
#include <linux/kernel.h>
char a, b, c;
asm (".section .counters, \"aw\"");
typedef struct {
int counter;
char *name;
} __attribute__((packed)) counter_info_t;
#define __PUT_STUFF_IN_SECTION(_name) \
do{ \
static counter_info_t __counter_info_##_name \
__attribute((unused,section(".counters"))) = { \
.name = #_name, \
.counter = 0, \
}; \
}while(0)
extern counter_info_t __start_counters[];
extern counter_info_t __stop_counters[];
int init_module(void){
__PUT_STUFF_IN_SECTION(a);
__PUT_STUFF_IN_SECTION(b);
__PUT_STUFF_IN_SECTION(c);
return 0;
}
void cleanup_module(void){
counter_info_t *iter = __start_counters;
for(; iter < __stop_counters; ++iter){
printk(KERN_INFO "Name: %s | Counter: %d.\n", iter->name, iter->counter);
}
重点是使用ldflags-y
变量。
关于c - 在 LKM 中使用 ELF 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18673149/