2

我正在尝试了解 linux 源文件/home/akash/Downloads/linux-3.4.3/fs/binfmt_elf.c 的一部分代码如下我卡住的地方。

static struct linux_binfmt elf_format = {
        .module         = THIS_MODULE,
        .load_binary    = load_elf_binary,
        .load_shlib     = load_elf_library,
        .core_dump      = elf_core_dump,
        .min_coredump   = ELF_EXEC_PAGESIZE,
};

它指的是文件include/linux/binfmt.h

struct linux_binfmt {
        struct list_head lh;
        struct module *module;
        int (*load_binary)(struct linux_binprm *, struct  pt_regs * regs);
        int (*load_shlib)(struct file *);
        int (*core_dump)(struct coredump_params *cprm);
        unsigned long min_coredump;     /* minimal dump size */
};

请对此进行一些解释..

4

2 回答 2

5

It is the initialisation of a structure of type struct linux_binfmt named elf_format (with static storage duration, meaning it exists for the lifetime of the program and is initialised exactly once), using syntax introduced in C99 that allows the specific structure member being initialised to be specified. Prior to C99 the initializer of a struct required the values to be listed in the order the members are declared in the struct.

For example:

struct struct_a { int a; char c; };

In C89 it was not possible to explicitly initialise c only:

struct struct_a s = { 0, 'f' }; /* Have to provide an initial value for
                                   'a', the 0, in order to provide an
                                   initial value for 'c', the f. */

but in C99 the new syntax made it possible:

struct struct_a s = { .c = 'f' };

The types of struct members:

  • lh is of type struct list_head, which is not explicitly initialised. lh will be default initialised (any members of lh that are pointers are initialized to a null pointer and any arithmetic types will be initialized to zero).
  • module is of type struct module* and is initialised to THIS_MODULE.
  • load_binary is a pointer to a function that returns an int and takes arguments of type struct linux_binptrm* and struct pt_regs, and is initialised to a function called load_elf_binary.
  • load_shlib is a pointer to a function that returns an int and takes an argument of type struct file* and is initialised to a function called load_elf_library.
  • core_dump is a pointer to a function that returns an int and takes an argument of struct coredump_params* and is initialised to a function called elf_core_dump.
  • min_coredump is of type unsigned long and is initialised to ELF_EXEC_PAGESIZE.

Refer to section 6.7.8 Initialization of the C99 standard for a full description on initialization syntax and rules.

于 2012-10-11T07:08:35.590 回答
0

elf_format is a variable of type linux_binfmt declared as static.

Values assigned for the members of elf_format

    .module         = THIS_MODULE,
    .load_binary    = load_elf_binary,    /* Function pointer assigned with functions */
    .load_shlib     = load_elf_library,   // do --
    .core_dump      = elf_core_dump,      // do --
    .min_coredump   = ELF_EXEC_PAGESIZE,
于 2012-10-11T07:07:56.213 回答