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 /* Free memory returned from module_alloc. */ 33 void module_memfree(void *module_region); 34 35 /* Determines if the section name is an init section (that is only used during 36 * module loading). 37 */ 38 bool module_init_section(const char *name); 39 40 /* Determines if the section name is an exit section (that is only used during 41 * module unloading) 42 */ 43 bool module_exit_section(const char *name); 44 45 /* Describes whether within_module_init() will consider this an init section 46 * or not. This behaviour changes with CONFIG_MODULE_UNLOAD. 47 */ 48 bool module_init_layout_section(const char *sname); 49 50 /* 51 * Apply the given relocation to the (simplified) ELF. Return -error 52 * or 0. 53 */ 54 #ifdef CONFIG_MODULES_USE_ELF_REL 55 int apply_relocate(Elf_Shdr *sechdrs, 56 const char *strtab, 57 unsigned int symindex, 58 unsigned int relsec, 59 struct module *mod); 60 #else 61 static inline int apply_relocate(Elf_Shdr *sechdrs, 62 const char *strtab, 63 unsigned int symindex, 64 unsigned int relsec, 65 struct module *me) 66 { 67 printk(KERN_ERR "module %s: REL relocation unsupported\n", 68 module_name(me)); 69 return -ENOEXEC; 70 } 71 #endif 72 73 /* 74 * Apply the given add relocation to the (simplified) ELF. Return 75 * -error or 0 76 */ 77 #ifdef CONFIG_MODULES_USE_ELF_RELA 78 int apply_relocate_add(Elf_Shdr *sechdrs, 79 const char *strtab, 80 unsigned int symindex, 81 unsigned int relsec, 82 struct module *mod); 83 #ifdef CONFIG_LIVEPATCH 84 /* 85 * Some architectures (namely x86_64 and ppc64) perform sanity checks when 86 * applying relocations. If a patched module gets unloaded and then later 87 * reloaded (and re-patched), klp re-applies relocations to the replacement 88 * function(s). Any leftover relocations from the previous loading of the 89 * patched module might trigger the sanity checks. 90 * 91 * To prevent that, when unloading a patched module, clear out any relocations 92 * that might trigger arch-specific sanity checks on a future module reload. 93 */ 94 void clear_relocate_add(Elf_Shdr *sechdrs, 95 const char *strtab, 96 unsigned int symindex, 97 unsigned int relsec, 98 struct module *me); 99 #endif 100 #else 101 static inline int apply_relocate_add(Elf_Shdr *sechdrs, 102 const char *strtab, 103 unsigned int symindex, 104 unsigned int relsec, 105 struct module *me) 106 { 107 printk(KERN_ERR "module %s: REL relocation unsupported\n", 108 module_name(me)); 109 return -ENOEXEC; 110 } 111 #endif 112 113 /* Any final processing of module before access. Return -error or 0. */ 114 int module_finalize(const Elf_Ehdr *hdr, 115 const Elf_Shdr *sechdrs, 116 struct module *mod); 117 118 /* Any cleanup needed when module leaves. */ 119 void module_arch_cleanup(struct module *mod); 120 121 /* Any cleanup before freeing mod->module_init */ 122 void module_arch_freeing_init(struct module *mod); 123 124 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ 125 !defined(CONFIG_KASAN_VMALLOC) 126 #include <linux/kasan.h> 127 #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) 128 #else 129 #define MODULE_ALIGN PAGE_SIZE 130 #endif 131 132 #endif 133