xref: /linux-6.15/include/linux/moduleloader.h (revision d453cded)
11da177e4SLinus Torvalds #ifndef _LINUX_MODULELOADER_H
21da177e4SLinus Torvalds #define _LINUX_MODULELOADER_H
31da177e4SLinus Torvalds /* The stuff needed for archs to support modules. */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/module.h>
61da177e4SLinus Torvalds #include <linux/elf.h>
71da177e4SLinus Torvalds 
874e08fcfSJonas Bonn /* These may be implemented by architectures that need to hook into the
974e08fcfSJonas Bonn  * module loader code.  Architectures that don't need to do anything special
1074e08fcfSJonas Bonn  * can just rely on the 'weak' default hooks defined in kernel/module.c.
1174e08fcfSJonas Bonn  * Note, however, that at least one of apply_relocate or apply_relocate_add
1274e08fcfSJonas Bonn  * must be implemented by each architecture.
1374e08fcfSJonas Bonn  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /* Adjust arch-specific sections.  Return 0 on success.  */
161da177e4SLinus Torvalds int module_frob_arch_sections(Elf_Ehdr *hdr,
171da177e4SLinus Torvalds 			      Elf_Shdr *sechdrs,
181da177e4SLinus Torvalds 			      char *secstrings,
191da177e4SLinus Torvalds 			      struct module *mod);
201da177e4SLinus Torvalds 
21088af9a6SHelge Deller /* Additional bytes needed by arch in front of individual sections */
22088af9a6SHelge Deller unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
23088af9a6SHelge Deller 
241da177e4SLinus Torvalds /* Allocator used for allocating struct module, core sections and init
251da177e4SLinus Torvalds    sections.  Returns NULL on failure. */
261da177e4SLinus Torvalds void *module_alloc(unsigned long size);
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /* Free memory returned from module_alloc. */
291da177e4SLinus Torvalds void module_free(struct module *mod, void *module_region);
301da177e4SLinus Torvalds 
31786d35d4SDavid Howells /*
32786d35d4SDavid Howells  * Apply the given relocation to the (simplified) ELF.  Return -error
33786d35d4SDavid Howells  * or 0.
34786d35d4SDavid Howells  */
35786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_REL
361da177e4SLinus Torvalds int apply_relocate(Elf_Shdr *sechdrs,
371da177e4SLinus Torvalds 		   const char *strtab,
381da177e4SLinus Torvalds 		   unsigned int symindex,
391da177e4SLinus Torvalds 		   unsigned int relsec,
401da177e4SLinus Torvalds 		   struct module *mod);
41786d35d4SDavid Howells #else
42786d35d4SDavid Howells static inline int apply_relocate(Elf_Shdr *sechdrs,
43786d35d4SDavid Howells 				 const char *strtab,
44786d35d4SDavid Howells 				 unsigned int symindex,
45786d35d4SDavid Howells 				 unsigned int relsec,
46786d35d4SDavid Howells 				 struct module *me)
47786d35d4SDavid Howells {
483a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
493a611c3cSRusty Russell 	       module_name(me));
50786d35d4SDavid Howells 	return -ENOEXEC;
51786d35d4SDavid Howells }
52786d35d4SDavid Howells #endif
531da177e4SLinus Torvalds 
54786d35d4SDavid Howells /*
55786d35d4SDavid Howells  * Apply the given add relocation to the (simplified) ELF.  Return
56786d35d4SDavid Howells  * -error or 0
57786d35d4SDavid Howells  */
58786d35d4SDavid Howells #ifdef CONFIG_MODULES_USE_ELF_RELA
591da177e4SLinus Torvalds int apply_relocate_add(Elf_Shdr *sechdrs,
601da177e4SLinus Torvalds 		       const char *strtab,
611da177e4SLinus Torvalds 		       unsigned int symindex,
621da177e4SLinus Torvalds 		       unsigned int relsec,
631da177e4SLinus Torvalds 		       struct module *mod);
64786d35d4SDavid Howells #else
65786d35d4SDavid Howells static inline int apply_relocate_add(Elf_Shdr *sechdrs,
66786d35d4SDavid Howells 				     const char *strtab,
67786d35d4SDavid Howells 				     unsigned int symindex,
68786d35d4SDavid Howells 				     unsigned int relsec,
69786d35d4SDavid Howells 				     struct module *me)
70786d35d4SDavid Howells {
713a611c3cSRusty Russell 	printk(KERN_ERR "module %s: REL relocation unsupported\n",
723a611c3cSRusty Russell 	       module_name(me));
73786d35d4SDavid Howells 	return -ENOEXEC;
74786d35d4SDavid Howells }
75786d35d4SDavid Howells #endif
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* Any final processing of module before access.  Return -error or 0. */
781da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr,
791da177e4SLinus Torvalds 		    const Elf_Shdr *sechdrs,
801da177e4SLinus Torvalds 		    struct module *mod);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* Any cleanup needed when module leaves. */
831da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod);
841da177e4SLinus Torvalds 
85*d453cdedSRusty Russell /* Any cleanup before freeing mod->module_init */
86*d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod);
871da177e4SLinus Torvalds #endif
88