]> Git Repo - linux.git/blob - include/linux/moduleloader.h
livepatch,x86: Clear relocation targets on a module removal
[linux.git] / include / linux / moduleloader.h
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.  */
17 int 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 */
23 unsigned 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. */
27 void *module_alloc(unsigned long size);
28
29 /* Free memory returned from module_alloc. */
30 void 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  */
35 bool 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  */
40 bool 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
47 int apply_relocate(Elf_Shdr *sechdrs,
48                    const char *strtab,
49                    unsigned int symindex,
50                    unsigned int relsec,
51                    struct module *mod);
52 #else
53 static 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
70 int apply_relocate_add(Elf_Shdr *sechdrs,
71                        const char *strtab,
72                        unsigned int symindex,
73                        unsigned int relsec,
74                        struct module *mod);
75 #ifdef CONFIG_LIVEPATCH
76 /*
77  * Some architectures (namely x86_64 and ppc64) perform sanity checks when
78  * applying relocations.  If a patched module gets unloaded and then later
79  * reloaded (and re-patched), klp re-applies relocations to the replacement
80  * function(s).  Any leftover relocations from the previous loading of the
81  * patched module might trigger the sanity checks.
82  *
83  * To prevent that, when unloading a patched module, clear out any relocations
84  * that might trigger arch-specific sanity checks on a future module reload.
85  */
86 void clear_relocate_add(Elf_Shdr *sechdrs,
87                    const char *strtab,
88                    unsigned int symindex,
89                    unsigned int relsec,
90                    struct module *me);
91 #endif
92 #else
93 static inline int apply_relocate_add(Elf_Shdr *sechdrs,
94                                      const char *strtab,
95                                      unsigned int symindex,
96                                      unsigned int relsec,
97                                      struct module *me)
98 {
99         printk(KERN_ERR "module %s: REL relocation unsupported\n",
100                module_name(me));
101         return -ENOEXEC;
102 }
103 #endif
104
105 /* Any final processing of module before access.  Return -error or 0. */
106 int module_finalize(const Elf_Ehdr *hdr,
107                     const Elf_Shdr *sechdrs,
108                     struct module *mod);
109
110 /* Any cleanup needed when module leaves. */
111 void module_arch_cleanup(struct module *mod);
112
113 /* Any cleanup before freeing mod->module_init */
114 void module_arch_freeing_init(struct module *mod);
115
116 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
117                 !defined(CONFIG_KASAN_VMALLOC)
118 #include <linux/kasan.h>
119 #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
120 #else
121 #define MODULE_ALIGN PAGE_SIZE
122 #endif
123
124 #endif
This page took 0.044581 seconds and 4 git commands to generate.