blob: 4fa67a8b22652e30737fd3158d73ac84da426b65 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#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
Jonas Bonn74e08fc2011-06-30 21:22:11 +02009/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/* Adjust arch-specific sections. Return 0 on success. */
17int module_frob_arch_sections(Elf_Ehdr *hdr,
18 Elf_Shdr *sechdrs,
19 char *secstrings,
20 struct module *mod);
21
Helge Deller088af9a2008-12-31 12:31:18 +010022/* Additional bytes needed by arch in front of individual sections */
23unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Allocator used for allocating struct module, core sections and init
26 sections. Returns NULL on failure. */
27void *module_alloc(unsigned long size);
28
29/* Free memory returned from module_alloc. */
Rusty Russellbe1f2212015-01-20 09:07:05 +103030void module_memfree(void *module_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Vincent Whitchurch23189762020-05-14 11:36:41 +010032/* Determines if the section name is an init section (that is only used during
33 * module loading).
34 */
35bool module_init_section(const char *name);
36
Matthias Schiffer38b37d62019-06-07 12:49:11 +020037/* Determines if the section name is an exit section (that is only used during
38 * module unloading)
39 */
40bool module_exit_section(const char *name);
41
David Howells786d35d2012-09-28 14:31:03 +093042/*
43 * Apply the given relocation to the (simplified) ELF. Return -error
44 * or 0.
45 */
46#ifdef CONFIG_MODULES_USE_ELF_REL
Linus Torvalds1da177e2005-04-16 15:20:36 -070047int apply_relocate(Elf_Shdr *sechdrs,
48 const char *strtab,
49 unsigned int symindex,
50 unsigned int relsec,
51 struct module *mod);
David Howells786d35d2012-09-28 14:31:03 +093052#else
53static inline int apply_relocate(Elf_Shdr *sechdrs,
54 const char *strtab,
55 unsigned int symindex,
56 unsigned int relsec,
57 struct module *me)
58{
Rusty Russell3a611c32014-07-27 07:23:01 +093059 printk(KERN_ERR "module %s: REL relocation unsupported\n",
60 module_name(me));
David Howells786d35d2012-09-28 14:31:03 +093061 return -ENOEXEC;
62}
63#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Howells786d35d2012-09-28 14:31:03 +093065/*
66 * Apply the given add relocation to the (simplified) ELF. Return
67 * -error or 0
68 */
69#ifdef CONFIG_MODULES_USE_ELF_RELA
Linus Torvalds1da177e2005-04-16 15:20:36 -070070int apply_relocate_add(Elf_Shdr *sechdrs,
71 const char *strtab,
72 unsigned int symindex,
73 unsigned int relsec,
74 struct module *mod);
David Howells786d35d2012-09-28 14:31:03 +093075#else
76static inline int apply_relocate_add(Elf_Shdr *sechdrs,
77 const char *strtab,
78 unsigned int symindex,
79 unsigned int relsec,
80 struct module *me)
81{
Rusty Russell3a611c32014-07-27 07:23:01 +093082 printk(KERN_ERR "module %s: REL relocation unsupported\n",
83 module_name(me));
David Howells786d35d2012-09-28 14:31:03 +093084 return -ENOEXEC;
85}
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/* Any final processing of module before access. Return -error or 0. */
89int module_finalize(const Elf_Ehdr *hdr,
90 const Elf_Shdr *sechdrs,
91 struct module *mod);
92
93/* Any cleanup needed when module leaves. */
94void module_arch_cleanup(struct module *mod);
95
Rusty Russelld453cde2015-01-20 09:07:04 +103096/* Any cleanup before freeing mod->module_init */
97void module_arch_freeing_init(struct module *mod);
Andrey Ryabinind3733e52015-03-12 16:26:14 -070098
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -080099#if defined(CONFIG_KASAN) && !defined(CONFIG_KASAN_VMALLOC)
Andrey Ryabinind3733e52015-03-12 16:26:14 -0700100#include <linux/kasan.h>
101#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
102#else
103#define MODULE_ALIGN PAGE_SIZE
104#endif
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif