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 /* arch may override to do additional checking of ELF header architecture */ 17 bool module_elf_check_arch(Elf_Ehdr *hdr); 18 19 /* Adjust arch-specific sections. Return 0 on success. */ 20 int module_frob_arch_sections(Elf_Ehdr *hdr, 21 Elf_Shdr *sechdrs, 22 char *secstrings, 23 struct module *mod); 24 25 /* Additional bytes needed by arch in front of individual sections */ 26 unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section); 27 28 /* Allocator used for allocating struct module, core sections and init 29 sections. Returns NULL on failure. */ 30 void *module_alloc(unsigned long size); 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 /* Describes whether within_module_init() will consider this an init section 43 * or not. This behaviour changes with CONFIG_MODULE_UNLOAD. 44 */ 45 bool module_init_layout_section(const char *sname); 46 47 /* 48 * Apply the given relocation to the (simplified) ELF. Return -error 49 * or 0. 50 */ 51 #ifdef CONFIG_MODULES_USE_ELF_REL 52 int apply_relocate(Elf_Shdr *sechdrs, 53 const char *strtab, 54 unsigned int symindex, 55 unsigned int relsec, 56 struct module *mod); 57 #else 58 static inline int apply_relocate(Elf_Shdr *sechdrs, 59 const char *strtab, 60 unsigned int symindex, 61 unsigned int relsec, 62 struct module *me) 63 { 64 printk(KERN_ERR "module %s: REL relocation unsupported\n", 65 module_name(me)); 66 return -ENOEXEC; 67 } 68 #endif 69 70 /* 71 * Apply the given add relocation to the (simplified) ELF. Return 72 * -error or 0 73 */ 74 #ifdef CONFIG_MODULES_USE_ELF_RELA 75 int apply_relocate_add(Elf_Shdr *sechdrs, 76 const char *strtab, 77 unsigned int symindex, 78 unsigned int relsec, 79 struct module *mod); 80 #ifdef CONFIG_LIVEPATCH 81 /* 82 * Some architectures (namely x86_64 and ppc64) perform sanity checks when 83 * applying relocations. If a patched module gets unloaded and then later 84 * reloaded (and re-patched), klp re-applies relocations to the replacement 85 * function(s). Any leftover relocations from the previous loading of the 86 * patched module might trigger the sanity checks. 87 * 88 * To prevent that, when unloading a patched module, clear out any relocations 89 * that might trigger arch-specific sanity checks on a future module reload. 90 */ 91 void clear_relocate_add(Elf_Shdr *sechdrs, 92 const char *strtab, 93 unsigned int symindex, 94 unsigned int relsec, 95 struct module *me); 96 #endif 97 #else 98 static inline int apply_relocate_add(Elf_Shdr *sechdrs, 99 const char *strtab, 100 unsigned int symindex, 101 unsigned int relsec, 102 struct module *me) 103 { 104 printk(KERN_ERR "module %s: REL relocation unsupported\n", 105 module_name(me)); 106 return -ENOEXEC; 107 } 108 #endif 109 110 /* Any final processing of module before access. Return -error or 0. */ 111 int module_finalize(const Elf_Ehdr *hdr, 112 const Elf_Shdr *sechdrs, 113 struct module *mod); 114 115 #ifdef CONFIG_MODULES 116 void flush_module_init_free_work(void); 117 #else 118 static inline void flush_module_init_free_work(void) 119 { 120 } 121 #endif 122 123 /* Any cleanup needed when module leaves. */ 124 void module_arch_cleanup(struct module *mod); 125 126 /* Any cleanup before freeing mod->module_init */ 127 void module_arch_freeing_init(struct module *mod); 128 129 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ 130 !defined(CONFIG_KASAN_VMALLOC) 131 #include <linux/kasan.h> 132 #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) 133 #else 134 #define MODULE_ALIGN PAGE_SIZE 135 #endif 136 137 #endif 138