c - 在 LKM 中使用 ELF 部分

标签 c linux module linux-kernel kernel-module

我在 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/

相关文章:

linux - 使用 ZSH 评估数组内容时顺序错误

linux - 为什么shell会在这种情况下启动另一个shell进程?

perl - 我使用什么 Perl 模块从 Windows 上的相对路径获取绝对路径(包括文件名)?

haskell - 如何安全地分解单一的 mkYesod block

c - 在宏中使用 if 语句

c - 埃拉托色尼分段筛算法

c - 如何避免变量自动分配到我的指针指向的内存单元的情况?

linux - Linux 发行版通常会安装手册页吗?

python - 我在 python 中遇到 "no module named ad"错误

python - 无法安装 Kivy : Cython/GCC error