Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_MODULELOADER_H
3#define _LINUX_MODULELOADER_H
4/* The stuff needed for archs to support modules. */
5
6#include <linux/module.h>
7#include <linux/elf.h>
8
9/* These may be implemented by architectures that need to hook into the
10 * module loader code. Architectures that don't need to do anything special
11 * can just rely on the 'weak' default hooks defined in kernel/module.c.
12 * Note, however, that at least one of apply_relocate or apply_relocate_add
13 * must be implemented by each architecture.
14 */
15
16/* Adjust arch-specific sections. Return 0 on success. */
17int module_frob_arch_sections(Elf_Ehdr *hdr,
18 Elf_Shdr *sechdrs,
19 char *secstrings,
20 struct module *mod);
21
22/* Additional bytes needed by arch in front of individual sections */
23unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
24
25/* Allocator used for allocating struct module, core sections and init
26 sections. Returns NULL on failure. */
27void *module_alloc(unsigned long size);
28
29/* Free memory returned from module_alloc. */
30void module_memfree(void *module_region);
31
32/* Determines if the section name is an init section (that is only used during
33 * module loading).
34 */
35bool module_init_section(const char *name);
36
37/* Determines if the section name is an exit section (that is only used during
38 * module unloading)
39 */
40bool module_exit_section(const char *name);
41
42/*
43 * Apply the given relocation to the (simplified) ELF. Return -error
44 * or 0.
45 */
46#ifdef CONFIG_MODULES_USE_ELF_REL
47int apply_relocate(Elf_Shdr *sechdrs,
48 const char *strtab,
49 unsigned int symindex,
50 unsigned int relsec,
51 struct module *mod);
52#else
53static inline int apply_relocate(Elf_Shdr *sechdrs,
54 const char *strtab,
55 unsigned int symindex,
56 unsigned int relsec,
57 struct module *me)
58{
59 printk(KERN_ERR "module %s: REL relocation unsupported\n",
60 module_name(me));
61 return -ENOEXEC;
62}
63#endif
64
65/*
66 * Apply the given add relocation to the (simplified) ELF. Return
67 * -error or 0
68 */
69#ifdef CONFIG_MODULES_USE_ELF_RELA
70int apply_relocate_add(Elf_Shdr *sechdrs,
71 const char *strtab,
72 unsigned int symindex,
73 unsigned int relsec,
74 struct module *mod);
75#else
76static inline int apply_relocate_add(Elf_Shdr *sechdrs,
77 const char *strtab,
78 unsigned int symindex,
79 unsigned int relsec,
80 struct module *me)
81{
82 printk(KERN_ERR "module %s: REL relocation unsupported\n",
83 module_name(me));
84 return -ENOEXEC;
85}
86#endif
87
88/* Any final processing of module before access. Return -error or 0. */
89int module_finalize(const Elf_Ehdr *hdr,
90 const Elf_Shdr *sechdrs,
91 struct module *mod);
92
93/* Any cleanup needed when module leaves. */
94void module_arch_cleanup(struct module *mod);
95
96/* Any cleanup before freeing mod->module_init */
97void module_arch_freeing_init(struct module *mod);
98
99#if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
100 !defined(CONFIG_KASAN_VMALLOC)
101#include <linux/kasan.h>
102#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
103#else
104#define MODULE_ALIGN PAGE_SIZE
105#endif
106
107#endif
1#ifndef _LINUX_MODULELOADER_H
2#define _LINUX_MODULELOADER_H
3/* The stuff needed for archs to support modules. */
4
5#include <linux/module.h>
6#include <linux/elf.h>
7
8/* These may be implemented by architectures that need to hook into the
9 * module loader code. Architectures that don't need to do anything special
10 * can just rely on the 'weak' default hooks defined in kernel/module.c.
11 * Note, however, that at least one of apply_relocate or apply_relocate_add
12 * must be implemented by each architecture.
13 */
14
15/* Adjust arch-specific sections. Return 0 on success. */
16int module_frob_arch_sections(Elf_Ehdr *hdr,
17 Elf_Shdr *sechdrs,
18 char *secstrings,
19 struct module *mod);
20
21/* Additional bytes needed by arch in front of individual sections */
22unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
23
24/* Allocator used for allocating struct module, core sections and init
25 sections. Returns NULL on failure. */
26void *module_alloc(unsigned long size);
27
28/* Free memory returned from module_alloc. */
29void module_memfree(void *module_region);
30
31/*
32 * Apply the given relocation to the (simplified) ELF. Return -error
33 * or 0.
34 */
35#ifdef CONFIG_MODULES_USE_ELF_REL
36int apply_relocate(Elf_Shdr *sechdrs,
37 const char *strtab,
38 unsigned int symindex,
39 unsigned int relsec,
40 struct module *mod);
41#else
42static inline int apply_relocate(Elf_Shdr *sechdrs,
43 const char *strtab,
44 unsigned int symindex,
45 unsigned int relsec,
46 struct module *me)
47{
48 printk(KERN_ERR "module %s: REL relocation unsupported\n",
49 module_name(me));
50 return -ENOEXEC;
51}
52#endif
53
54/*
55 * Apply the given add relocation to the (simplified) ELF. Return
56 * -error or 0
57 */
58#ifdef CONFIG_MODULES_USE_ELF_RELA
59int apply_relocate_add(Elf_Shdr *sechdrs,
60 const char *strtab,
61 unsigned int symindex,
62 unsigned int relsec,
63 struct module *mod);
64#else
65static inline int apply_relocate_add(Elf_Shdr *sechdrs,
66 const char *strtab,
67 unsigned int symindex,
68 unsigned int relsec,
69 struct module *me)
70{
71 printk(KERN_ERR "module %s: REL relocation unsupported\n",
72 module_name(me));
73 return -ENOEXEC;
74}
75#endif
76
77/* Any final processing of module before access. Return -error or 0. */
78int module_finalize(const Elf_Ehdr *hdr,
79 const Elf_Shdr *sechdrs,
80 struct module *mod);
81
82/* Any cleanup needed when module leaves. */
83void module_arch_cleanup(struct module *mod);
84
85/* Any cleanup before freeing mod->module_init */
86void module_arch_freeing_init(struct module *mod);
87
88#ifdef CONFIG_KASAN
89#include <linux/kasan.h>
90#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
91#else
92#define MODULE_ALIGN PAGE_SIZE
93#endif
94
95#endif