Home > Back-end >  linux kernel code without type declaration that isn't a comment, what does it mean?
linux kernel code without type declaration that isn't a comment, what does it mean?

Time:06-19

I am reading Linux's source code and i have come accross the following section:

/*
 * This begins the randomizable portion of task_struct. Only
 * scheduling-critical items should be added above here.
 */
randomized_struct_fields_start

void                *stack;
refcount_t          usage;
/* Per task flags (PF_*), defined further below: */
unsigned int            flags;
unsigned int            ptrace;

i am confused regarding "randomized_struct_fields_start" its just some loose text without ";", without data type declaration, "//" or "/**/"... does anyone know what this is? I believe my question is about the c language specifically and not linux, if i am wrong please tell me and i will ask the question somewhere else.

CodePudding user response:

Linux Kernel is indexed online https://elixir.bootlin.com/linux/latest/ident/randomized_struct_fields_start . From https://elixir.bootlin.com/linux/latest/source/include/linux/compiler-gcc.h#L73:

#if defined(RANDSTRUCT_PLUGIN) && !defined(__CHECKER__)
#define __randomize_layout __attribute__((randomize_layout))
#define __no_randomize_layout __attribute__((no_randomize_layout))
/* This anon struct can add padding, so only enable it under randstruct. */
#define randomized_struct_fields_start  struct {
#define randomized_struct_fields_end    } __randomize_layout;
#endif

From https://elixir.bootlin.com/linux/latest/source/include/linux/compiler_types.h#L254 :

#ifndef randomized_struct_fields_start
# define randomized_struct_fields_start
# define randomized_struct_fields_end
#endif

what this is?

It's a macro that expands to struct { on gcc compiler with RANDSTRUCT_PLUGIN, and it expands to nothing otherwise.

"Expands" in this case literally means "replaced".

  • Related