Skip to content

Commit eb3057d

Browse files
Frantisek Hrbatarustyrussell
authored andcommitted
kernel: add support for init_array constructors
This adds the .init_array section as yet another section with constructors. This is needed because gcc could add __gcov_init calls to .init_array or .ctors section, depending on gcc (and binutils) version . v2: - reuse mod->ctors for .init_array section for modules, because gcc uses .ctors or .init_array, but not both at the same time v3: - fail to load if that does happen somehow. Signed-off-by: Frantisek Hrbata <fhrbata@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent eed380f commit eb3057d

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎include/asm-generic/vmlinux.lds.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@
473473
#define KERNEL_CTORS() . = ALIGN(8); \
474474
VMLINUX_SYMBOL(__ctors_start) = .; \
475475
*(.ctors) \
476+
*(.init_array) \
476477
VMLINUX_SYMBOL(__ctors_end) = .;
477478
#else
478479
#define KERNEL_CTORS()

‎kernel/module.c‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
27082708
return 0;
27092709
}
27102710

2711-
static void find_module_sections(struct module *mod, struct load_info *info)
2711+
static int find_module_sections(struct module *mod, struct load_info *info)
27122712
{
27132713
mod->kp = section_objs(info, "__param",
27142714
sizeof(*mod->kp), &mod->num_kp);
@@ -2738,6 +2738,18 @@ static void find_module_sections(struct module *mod, struct load_info *info)
27382738
#ifdef CONFIG_CONSTRUCTORS
27392739
mod->ctors = section_objs(info, ".ctors",
27402740
sizeof(*mod->ctors), &mod->num_ctors);
2741+
if (!mod->ctors)
2742+
mod->ctors = section_objs(info, ".init_array",
2743+
sizeof(*mod->ctors), &mod->num_ctors);
2744+
else if (find_sec(info, ".init_array")) {
2745+
/*
2746+
* This shouldn't happen with same compiler and binutils
2747+
* building all parts of the module.
2748+
*/
2749+
printk(KERN_WARNING "%s: has both .ctors and .init_array.\n",
2750+
mod->name);
2751+
return -EINVAL;
2752+
}
27412753
#endif
27422754

27432755
#ifdef CONFIG_TRACEPOINTS
@@ -2776,6 +2788,8 @@ static void find_module_sections(struct module *mod, struct load_info *info)
27762788

27772789
info->debug = section_objs(info, "__verbose",
27782790
sizeof(*info->debug), &info->num_debug);
2791+
2792+
return 0;
27792793
}
27802794

27812795
static int move_module(struct module *mod, struct load_info *info)
@@ -3233,7 +3247,9 @@ static int load_module(struct load_info *info, const char __user *uargs,
32333247

32343248
/* Now we've got everything in the final locations, we can
32353249
* find optional sections. */
3236-
find_module_sections(mod, info);
3250+
err = find_module_sections(mod, info);
3251+
if (err)
3252+
goto free_unload;
32373253

32383254
err = check_module_license_and_versions(mod);
32393255
if (err)

0 commit comments

Comments
 (0)