xref: /linux-6.15/include/linux/moduleloader.h (revision 0fea6e9a)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_MODULELOADER_H
31da177e4SLinus Torvalds #define _LINUX_MODULELOADER_H
41da177e4SLinus Torvalds /* The stuff needed for archs to support modules. */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/elf.h>
81da177e4SLinus Torvalds 
974e08fcfSJonas Bonn /* These may be implemented by architectures that need to hook into the
1074e08fcfSJonas Bonn  * module loader code.  Architectures that don't need to do anything special
1174e08fcfSJonas Bonn  * can just rely on the 'weak' default hooks defined in kernel/module.c.
1274e08fcfSJonas Bonn  * Note, however, that at least one of apply_relocate or apply_relocate_add
1374e08fcfSJonas Bonn  * must be implemented by each architecture.
1474e08fcfSJonas Bonn  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /* Adjust arch-specific sections.  Return 0 on success.  */
171da177e4SLinus Torvalds int module_frob_arch_sections(Elf_Ehdr *hdr,
181da177e4SLinus Torvalds 			      Elf_Shdr *sechdrs,
191da177e4SLinus Torvalds 			      char *secstrings,
201da177e4SLinus Torvalds 			      struct module *mod);
211da177e4SLinus Torvalds 
22088af9a6SHelge Deller /* Additional bytes needed by arch in front of individual sections */
23088af9a6SHelge Deller unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
24088af9a6SHelge Deller 
251da177e4SLinus Torvalds /* Allocator used for allocating struct module, core sections and init
261da177e4SLinus Torvalds    sections.  Returns NULL on failure. */
271da177e4SLinus Torvalds void *module_alloc(unsigned long size);
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /* Free memory returned from module_alloc. */
30be1f221cSRusty Russell void module_memfree(void *module_region);
311da177e4SLinus Torvalds 
3223189766SVincent Whitchurch /* Determines if the section name is an init section (that is only used during
3323189766SVincent Whitchurch  * module loading).
3423189766SVincent Whitchurch  */
3523189766SVincent Whitchurch bool module_init_section(const char *name);
3623189766SVincent Whitchurch 
3738b37d63SMatthias Schiffer /* Determines if the section name is an exit section (that is only used during
3838b37d63SMatthias Schiffer  * module unloading)
3938b37d63SMatthias Schiffer  */
4038b37d63SMatthias Schiffer bool module_exit_section(const char *name);
4138b37d63SMatthias Schiffer 
42786d35d4SDavid Howells /*
43786d35d4SDavid Howells  * Apply the given relocation to the (simplified) ELF.  Return -error
44786d35d4SDavid Howells  * or 0.
45786d35d4SDavid Howells  */
46786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_REL
471da177e4SLinus Torvalds int apply_relocate(Elf_Shdr *sechdrs,
481da177e4SLinus Torvalds 		   const char *strtab,
491da177e4SLinus Torvalds 		   unsigned int symindex,
501da177e4SLinus Torvalds 		   unsigned int relsec,
511da177e4SLinus Torvalds 		   struct module *mod);
52786d35d4SDavid Howells #else
53786d35d4SDavid Howells static inline int apply_relocate(Elf_Shdr *sechdrs,
54786d35d4SDavid Howells 				 const char *strtab,
55786d35d4SDavid Howells 				 unsigned int symindex,
56786d35d4SDavid Howells 				 unsigned int relsec,
57786d35d4SDavid Howells 				 struct module *me)
58786d35d4SDavid Howells {
593a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
603a611c3cSRusty Russell 	       module_name(me));
61786d35d4SDavid Howells 	return -ENOEXEC;
62786d35d4SDavid Howells }
63786d35d4SDavid Howells #endif
641da177e4SLinus Torvalds 
65786d35d4SDavid Howells /*
66786d35d4SDavid Howells  * Apply the given add relocation to the (simplified) ELF.  Return
67786d35d4SDavid Howells  * -error or 0
68786d35d4SDavid Howells  */
69786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_RELA
701da177e4SLinus Torvalds int apply_relocate_add(Elf_Shdr *sechdrs,
711da177e4SLinus Torvalds 		       const char *strtab,
721da177e4SLinus Torvalds 		       unsigned int symindex,
731da177e4SLinus Torvalds 		       unsigned int relsec,
741da177e4SLinus Torvalds 		       struct module *mod);
75786d35d4SDavid Howells #else
76786d35d4SDavid Howells static inline int apply_relocate_add(Elf_Shdr *sechdrs,
77786d35d4SDavid Howells 				     const char *strtab,
78786d35d4SDavid Howells 				     unsigned int symindex,
79786d35d4SDavid Howells 				     unsigned int relsec,
80786d35d4SDavid Howells 				     struct module *me)
81786d35d4SDavid Howells {
823a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
833a611c3cSRusty Russell 	       module_name(me));
84786d35d4SDavid Howells 	return -ENOEXEC;
85786d35d4SDavid Howells }
86786d35d4SDavid Howells #endif
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* Any final processing of module before access.  Return -error or 0. */
891da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr,
901da177e4SLinus Torvalds 		    const Elf_Shdr *sechdrs,
911da177e4SLinus Torvalds 		    struct module *mod);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* Any cleanup needed when module leaves. */
941da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod);
951da177e4SLinus Torvalds 
96d453cdedSRusty Russell /* Any cleanup before freeing mod->module_init */
97d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod);
98d3733e5cSAndrey Ryabinin 
99*0fea6e9aSAndrey Konovalov #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
100*0fea6e9aSAndrey Konovalov 		!defined(CONFIG_KASAN_VMALLOC)
101d3733e5cSAndrey Ryabinin #include <linux/kasan.h>
102d3733e5cSAndrey Ryabinin #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
103d3733e5cSAndrey Ryabinin #else
104d3733e5cSAndrey Ryabinin #define MODULE_ALIGN PAGE_SIZE
105d3733e5cSAndrey Ryabinin #endif
106d3733e5cSAndrey Ryabinin 
1071da177e4SLinus Torvalds #endif
108