1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/syscalls.h>
28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/stop_machine.h>
37 #include <asm/uaccess.h>
38 #include <asm/semaphore.h>
39 #include <asm/pgalloc.h>
40 #include <asm/cacheflush.h>
45 #define DEBUGP(fmt , a...)
48 #ifndef ARCH_SHF_SMALL
49 #define ARCH_SHF_SMALL 0
52 /* If this is set, the section belongs in the init part of the module */
53 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
55 #define symbol_is(literal, string) \
56 (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
58 /* Protects module list */
59 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
61 /* List of modules, protected by module_mutex AND modlist_lock */
62 static DECLARE_MUTEX(module_mutex);
63 static LIST_HEAD(modules);
65 static DECLARE_MUTEX(notify_mutex);
66 static struct notifier_block * module_notify_list;
68 int register_module_notifier(struct notifier_block * nb)
72 err = notifier_chain_register(&module_notify_list, nb);
76 EXPORT_SYMBOL(register_module_notifier);
78 int unregister_module_notifier(struct notifier_block * nb)
82 err = notifier_chain_unregister(&module_notify_list, nb);
86 EXPORT_SYMBOL(unregister_module_notifier);
88 /* We require a truly strong try_module_get() */
89 static inline int strong_try_module_get(struct module *mod)
91 if (mod && mod->state == MODULE_STATE_COMING)
93 return try_module_get(mod);
96 /* Stub function for modules which don't have an initfn */
101 EXPORT_SYMBOL(init_module);
103 /* A thread that wants to hold a reference to a module only while it
104 * is running can call ths to safely exit.
105 * nfsd and lockd use this.
107 void __module_put_and_exit(struct module *mod, long code)
112 EXPORT_SYMBOL(__module_put_and_exit);
114 /* Find a module section: 0 means not found. */
115 static unsigned int find_sec(Elf_Ehdr *hdr,
117 const char *secstrings,
122 for (i = 1; i < hdr->e_shnum; i++)
123 /* Alloc bit cleared means "ignore it." */
124 if ((sechdrs[i].sh_flags & SHF_ALLOC)
125 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
130 /* Provided by the linker */
131 extern const struct kernel_symbol __start___ksymtab[];
132 extern const struct kernel_symbol __stop___ksymtab[];
133 extern const struct kernel_symbol __start___ksymtab_gpl[];
134 extern const struct kernel_symbol __stop___ksymtab_gpl[];
135 extern const unsigned long __start___kcrctab[];
136 extern const unsigned long __start___kcrctab_gpl[];
138 #ifndef CONFIG_MODVERSIONS
139 #define symversion(base, idx) NULL
141 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
144 /* Find a symbol, return value, crc and module which owns it */
145 static unsigned long __find_symbol(const char *name,
146 struct module **owner,
147 const unsigned long **crc,
153 /* Core kernel first. */
155 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
156 if (strcmp(__start___ksymtab[i].name, name) == 0) {
157 *crc = symversion(__start___kcrctab, i);
158 return __start___ksymtab[i].value;
162 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
163 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
164 *crc = symversion(__start___kcrctab_gpl, i);
165 return __start___ksymtab_gpl[i].value;
169 /* Now try modules. */
170 list_for_each_entry(mod, &modules, list) {
172 for (i = 0; i < mod->num_syms; i++)
173 if (strcmp(mod->syms[i].name, name) == 0) {
174 *crc = symversion(mod->crcs, i);
175 return mod->syms[i].value;
179 for (i = 0; i < mod->num_gpl_syms; i++) {
180 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
181 *crc = symversion(mod->gpl_crcs, i);
182 return mod->gpl_syms[i].value;
187 DEBUGP("Failed to find symbol %s\n", name);
191 /* Find a symbol in this elf symbol table */
192 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
193 unsigned int symindex,
198 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
200 /* Search (defined) internal symbols first. */
201 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
202 if (sym[i].st_shndx != SHN_UNDEF
203 && strcmp(name, strtab + sym[i].st_name) == 0)
204 return sym[i].st_value;
209 /* Search for module by name: must hold module_mutex. */
210 static struct module *find_module(const char *name)
214 list_for_each_entry(mod, &modules, list) {
215 if (strcmp(mod->name, name) == 0)
222 /* Number of blocks used and allocated. */
223 static unsigned int pcpu_num_used, pcpu_num_allocated;
224 /* Size of each block. -ve means used. */
225 static int *pcpu_size;
227 static int split_block(unsigned int i, unsigned short size)
229 /* Reallocation required? */
230 if (pcpu_num_used + 1 > pcpu_num_allocated) {
231 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
236 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
237 pcpu_num_allocated *= 2;
242 /* Insert a new subblock */
243 memmove(&pcpu_size[i+1], &pcpu_size[i],
244 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
247 pcpu_size[i+1] -= size;
252 static inline unsigned int block_size(int val)
259 /* Created by linker magic */
260 extern char __per_cpu_start[], __per_cpu_end[];
262 static void *percpu_modalloc(unsigned long size, unsigned long align)
268 BUG_ON(align > SMP_CACHE_BYTES);
270 ptr = __per_cpu_start;
271 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
272 /* Extra for alignment requirement. */
273 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
274 BUG_ON(i == 0 && extra != 0);
276 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
279 /* Transfer extra to previous block. */
280 if (pcpu_size[i-1] < 0)
281 pcpu_size[i-1] -= extra;
283 pcpu_size[i-1] += extra;
284 pcpu_size[i] -= extra;
287 /* Split block if warranted */
288 if (pcpu_size[i] - size > sizeof(unsigned long))
289 if (!split_block(i, size))
293 pcpu_size[i] = -pcpu_size[i];
297 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
302 static void percpu_modfree(void *freeme)
305 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
307 /* First entry is core kernel percpu data. */
308 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
310 pcpu_size[i] = -pcpu_size[i];
317 /* Merge with previous? */
318 if (pcpu_size[i-1] >= 0) {
319 pcpu_size[i-1] += pcpu_size[i];
321 memmove(&pcpu_size[i], &pcpu_size[i+1],
322 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
325 /* Merge with next? */
326 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
327 pcpu_size[i] += pcpu_size[i+1];
329 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
330 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
334 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
336 const char *secstrings)
338 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
341 static int percpu_modinit(void)
344 pcpu_num_allocated = 2;
345 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
347 /* Static in-kernel percpu data (used). */
348 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
350 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
351 if (pcpu_size[1] < 0) {
352 printk(KERN_ERR "No per-cpu room for modules.\n");
358 __initcall(percpu_modinit);
359 #else /* ... !CONFIG_SMP */
360 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
364 static inline void percpu_modfree(void *pcpuptr)
368 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
370 const char *secstrings)
374 static inline void percpu_modcopy(void *pcpudst, const void *src,
377 /* pcpusec should be 0, and size of that section should be 0. */
380 #endif /* CONFIG_SMP */
382 #ifdef CONFIG_MODULE_UNLOAD
383 /* Init the unload section of the module. */
384 static void module_unload_init(struct module *mod)
388 INIT_LIST_HEAD(&mod->modules_which_use_me);
389 for (i = 0; i < NR_CPUS; i++)
390 local_set(&mod->ref[i].count, 0);
391 /* Hold reference count during initialization. */
392 local_set(&mod->ref[smp_processor_id()].count, 1);
393 /* Backwards compatibility macros put refcount during init. */
394 mod->waiter = current;
397 /* modules using other modules */
400 struct list_head list;
401 struct module *module_which_uses;
404 /* Does a already use b? */
405 static int already_uses(struct module *a, struct module *b)
407 struct module_use *use;
409 list_for_each_entry(use, &b->modules_which_use_me, list) {
410 if (use->module_which_uses == a) {
411 DEBUGP("%s uses %s!\n", a->name, b->name);
415 DEBUGP("%s does not use %s!\n", a->name, b->name);
419 /* Module a uses b */
420 static int use_module(struct module *a, struct module *b)
422 struct module_use *use;
423 if (b == NULL || already_uses(a, b)) return 1;
425 if (!strong_try_module_get(b))
428 DEBUGP("Allocating new usage for %s.\n", a->name);
429 use = kmalloc(sizeof(*use), GFP_ATOMIC);
431 printk("%s: out of memory loading\n", a->name);
436 use->module_which_uses = a;
437 list_add(&use->list, &b->modules_which_use_me);
441 /* Clear the unload stuff of the module. */
442 static void module_unload_free(struct module *mod)
446 list_for_each_entry(i, &modules, list) {
447 struct module_use *use;
449 list_for_each_entry(use, &i->modules_which_use_me, list) {
450 if (use->module_which_uses == mod) {
451 DEBUGP("%s unusing %s\n", mod->name, i->name);
453 list_del(&use->list);
455 /* There can be at most one match. */
462 #ifdef CONFIG_MODULE_FORCE_UNLOAD
463 static inline int try_force(unsigned int flags)
465 int ret = (flags & O_TRUNC);
467 tainted |= TAINT_FORCED_MODULE;
471 static inline int try_force(unsigned int flags)
475 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
484 /* Whole machine is stopped with interrupts off when this runs. */
485 static inline int __try_stop_module(void *_sref)
487 struct stopref *sref = _sref;
489 /* If it's not unused, quit unless we are told to block. */
490 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
491 if (!(*sref->forced = try_force(sref->flags)))
495 /* Mark it as dying. */
496 sref->mod->state = MODULE_STATE_GOING;
500 static int try_stop_module(struct module *mod, int flags, int *forced)
502 struct stopref sref = { mod, flags, forced };
504 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
507 unsigned int module_refcount(struct module *mod)
509 unsigned int i, total = 0;
511 for (i = 0; i < NR_CPUS; i++)
512 total += local_read(&mod->ref[i].count);
515 EXPORT_SYMBOL(module_refcount);
517 /* This exists whether we can unload or not */
518 static void free_module(struct module *mod);
520 /* Stub function for modules which don't have an exitfn */
521 void cleanup_module(void)
524 EXPORT_SYMBOL(cleanup_module);
526 static void wait_for_zero_refcount(struct module *mod)
528 /* Since we might sleep for some time, drop the semaphore first */
531 DEBUGP("Looking at refcount...\n");
532 set_current_state(TASK_UNINTERRUPTIBLE);
533 if (module_refcount(mod) == 0)
537 current->state = TASK_RUNNING;
542 sys_delete_module(const char __user *name_user, unsigned int flags)
545 char name[MODULE_NAME_LEN];
548 if (!capable(CAP_SYS_MODULE))
551 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
553 name[MODULE_NAME_LEN-1] = '\0';
555 if (down_interruptible(&module_mutex) != 0)
558 mod = find_module(name);
564 if (!list_empty(&mod->modules_which_use_me)) {
565 /* Other modules depend on us: get rid of them first. */
570 /* Doing init or already dying? */
571 if (mod->state != MODULE_STATE_LIVE) {
572 /* FIXME: if (force), slam module count and wake up
574 DEBUGP("%s already dying\n", mod->name);
579 /* If it has an init func, it must have an exit func to unload */
580 if ((mod->init != init_module && mod->exit == cleanup_module)
582 forced = try_force(flags);
584 /* This module can't be removed */
590 /* Set this up before setting mod->state */
591 mod->waiter = current;
593 /* Stop the machine so refcounts can't move and disable module. */
594 ret = try_stop_module(mod, flags, &forced);
596 /* Never wait if forced. */
597 if (!forced && module_refcount(mod) != 0)
598 wait_for_zero_refcount(mod);
600 /* Final destruction now noone is using it. */
611 static void print_unload_info(struct seq_file *m, struct module *mod)
613 struct module_use *use;
614 int printed_something = 0;
616 seq_printf(m, " %u ", module_refcount(mod));
618 /* Always include a trailing , so userspace can differentiate
619 between this and the old multi-field proc format. */
620 list_for_each_entry(use, &mod->modules_which_use_me, list) {
621 printed_something = 1;
622 seq_printf(m, "%s,", use->module_which_uses->name);
626 printed_something = 1;
627 seq_printf(m, "[unsafe],");
630 if (mod->init != init_module && mod->exit == cleanup_module) {
631 printed_something = 1;
632 seq_printf(m, "[permanent],");
635 if (!printed_something)
639 void __symbol_put(const char *symbol)
641 struct module *owner;
643 const unsigned long *crc;
645 spin_lock_irqsave(&modlist_lock, flags);
646 if (!__find_symbol(symbol, &owner, &crc, 1))
649 spin_unlock_irqrestore(&modlist_lock, flags);
651 EXPORT_SYMBOL(__symbol_put);
653 void symbol_put_addr(void *addr)
657 spin_lock_irqsave(&modlist_lock, flags);
658 if (!kernel_text_address((unsigned long)addr))
661 module_put(module_text_address((unsigned long)addr));
662 spin_unlock_irqrestore(&modlist_lock, flags);
664 EXPORT_SYMBOL_GPL(symbol_put_addr);
666 #else /* !CONFIG_MODULE_UNLOAD */
667 static void print_unload_info(struct seq_file *m, struct module *mod)
669 /* We don't know the usage count, or what modules are using. */
670 seq_printf(m, " - -");
673 static inline void module_unload_free(struct module *mod)
677 static inline int use_module(struct module *a, struct module *b)
679 return strong_try_module_get(b);
682 static inline void module_unload_init(struct module *mod)
687 sys_delete_module(const char *name_user, unsigned int flags)
692 #endif /* CONFIG_MODULE_UNLOAD */
694 #ifdef CONFIG_OBSOLETE_MODPARM
695 static int param_set_byte(const char *val, struct kernel_param *kp)
700 if (!val) return -EINVAL;
701 l = simple_strtol(val, &endp, 0);
702 if (endp == val || *endp || ((char)l != l))
704 *((char *)kp->arg) = l;
708 /* Bounds checking done below */
709 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
711 strcpy(kp->arg, val);
715 int set_obsolete(const char *val, struct kernel_param *kp)
717 unsigned int min, max;
718 unsigned int size, maxsize;
722 struct obsolete_modparm *obsparm = kp->arg;
725 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
729 /* type is: [min[-max]]{b,h,i,l,s} */
731 min = simple_strtol(p, &endp, 10);
732 if (endp == obsparm->type)
734 else if (*endp == '-') {
736 max = simple_strtol(p, &endp, 10);
741 return param_array(kp->name, val, min, max, obsparm->addr,
742 1, param_set_byte, &dummy);
744 return param_array(kp->name, val, min, max, obsparm->addr,
745 sizeof(short), param_set_short, &dummy);
747 return param_array(kp->name, val, min, max, obsparm->addr,
748 sizeof(int), param_set_int, &dummy);
750 return param_array(kp->name, val, min, max, obsparm->addr,
751 sizeof(long), param_set_long, &dummy);
753 return param_array(kp->name, val, min, max, obsparm->addr,
754 sizeof(char *), param_set_charp, &dummy);
757 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
758 and the decl is "char xxx[5][50];" */
760 maxsize = simple_strtol(p, &endp, 10);
761 /* We check lengths here (yes, this is a hack). */
763 while (p[size = strcspn(p, ",")]) {
770 return param_array(kp->name, val, min, max, obsparm->addr,
771 maxsize, obsparm_copy_string, &dummy);
773 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
777 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
781 static int obsolete_params(const char *name,
783 struct obsolete_modparm obsparm[],
786 unsigned int symindex,
789 struct kernel_param *kp;
793 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
797 for (i = 0; i < num; i++) {
798 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
800 snprintf(sym_name, sizeof(sym_name), "%s%s",
801 MODULE_SYMBOL_PREFIX, obsparm[i].name);
803 kp[i].name = obsparm[i].name;
805 kp[i].set = set_obsolete;
808 = (void *)find_local_symbol(sechdrs, symindex, strtab,
810 if (!obsparm[i].addr) {
811 printk("%s: falsely claims to have parameter %s\n",
812 name, obsparm[i].name);
816 kp[i].arg = &obsparm[i];
819 ret = parse_args(name, args, kp, num, NULL);
825 static int obsolete_params(const char *name,
827 struct obsolete_modparm obsparm[],
830 unsigned int symindex,
834 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
838 #endif /* CONFIG_OBSOLETE_MODPARM */
840 static const char vermagic[] = VERMAGIC_STRING;
842 #ifdef CONFIG_MODVERSIONS
843 static int check_version(Elf_Shdr *sechdrs,
844 unsigned int versindex,
847 const unsigned long *crc)
849 unsigned int i, num_versions;
850 struct modversion_info *versions;
852 /* Exporting module didn't supply crcs? OK, we're already tainted. */
856 versions = (void *) sechdrs[versindex].sh_addr;
857 num_versions = sechdrs[versindex].sh_size
858 / sizeof(struct modversion_info);
860 for (i = 0; i < num_versions; i++) {
861 if (strcmp(versions[i].name, symname) != 0)
864 if (versions[i].crc == *crc)
866 printk("%s: disagrees about version of symbol %s\n",
868 DEBUGP("Found checksum %lX vs module %lX\n",
869 *crc, versions[i].crc);
872 /* Not in module's version table. OK, but that taints the kernel. */
873 if (!(tainted & TAINT_FORCED_MODULE)) {
874 printk("%s: no version for \"%s\" found: kernel tainted.\n",
876 tainted |= TAINT_FORCED_MODULE;
881 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
882 unsigned int versindex,
885 const unsigned long *crc;
886 struct module *owner;
888 if (!__find_symbol("struct_module", &owner, &crc, 1))
890 return check_version(sechdrs, versindex, "struct_module", mod,
894 /* First part is kernel version, which we ignore. */
895 static inline int same_magic(const char *amagic, const char *bmagic)
897 amagic += strcspn(amagic, " ");
898 bmagic += strcspn(bmagic, " ");
899 return strcmp(amagic, bmagic) == 0;
902 static inline int check_version(Elf_Shdr *sechdrs,
903 unsigned int versindex,
906 const unsigned long *crc)
911 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
912 unsigned int versindex,
918 static inline int same_magic(const char *amagic, const char *bmagic)
920 return strcmp(amagic, bmagic) == 0;
922 #endif /* CONFIG_MODVERSIONS */
924 /* Resolve a symbol for this module. I.e. if we find one, record usage.
925 Must be holding module_mutex. */
926 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
927 unsigned int versindex,
931 struct module *owner;
933 const unsigned long *crc;
935 spin_lock_irq(&modlist_lock);
936 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
938 /* use_module can fail due to OOM, or module unloading */
939 if (!check_version(sechdrs, versindex, name, mod, crc) ||
940 !use_module(mod, owner))
943 spin_unlock_irq(&modlist_lock);
947 /* Free a module, remove from lists, etc (must hold module mutex). */
948 static void free_module(struct module *mod)
950 /* Delete from various lists */
951 spin_lock_irq(&modlist_lock);
952 list_del(&mod->list);
953 spin_unlock_irq(&modlist_lock);
955 /* Arch-specific cleanup. */
956 module_arch_cleanup(mod);
958 /* Module unload stuff */
959 module_unload_free(mod);
961 /* This may be NULL, but that's OK */
962 module_free(mod, mod->module_init);
965 percpu_modfree(mod->percpu);
967 /* Finally, free the core (containing the module structure) */
968 module_free(mod, mod->module_core);
971 void *__symbol_get(const char *symbol)
973 struct module *owner;
974 unsigned long value, flags;
975 const unsigned long *crc;
977 spin_lock_irqsave(&modlist_lock, flags);
978 value = __find_symbol(symbol, &owner, &crc, 1);
979 if (value && !strong_try_module_get(owner))
981 spin_unlock_irqrestore(&modlist_lock, flags);
983 return (void *)value;
985 EXPORT_SYMBOL_GPL(__symbol_get);
987 /* Change all symbols so that sh_value encodes the pointer directly. */
988 static int simplify_symbols(Elf_Shdr *sechdrs,
989 unsigned int symindex,
991 unsigned int versindex,
992 unsigned int pcpuindex,
995 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
996 unsigned long secbase;
997 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1000 for (i = 1; i < n; i++) {
1001 switch (sym[i].st_shndx) {
1003 /* We compiled with -fno-common. These are not
1004 supposed to happen. */
1005 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1006 printk("%s: please compile with -fno-common\n",
1012 /* Don't need to do anything */
1013 DEBUGP("Absolute symbol: 0x%08lx\n",
1014 (long)sym[i].st_value);
1019 = resolve_symbol(sechdrs, versindex,
1020 strtab + sym[i].st_name, mod);
1022 /* Ok if resolved. */
1023 if (sym[i].st_value != 0)
1026 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1029 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1030 mod->name, strtab + sym[i].st_name);
1035 /* Divert to percpu allocation if a percpu var. */
1036 if (sym[i].st_shndx == pcpuindex)
1037 secbase = (unsigned long)mod->percpu;
1039 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1040 sym[i].st_value += secbase;
1048 /* Update size with this section: return offset. */
1049 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1053 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1054 *size = ret + sechdr->sh_size;
1058 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1059 might -- code, read-only data, read-write data, small data. Tally
1060 sizes, and place the offsets into sh_entsize fields: high bit means it
1062 static void layout_sections(struct module *mod,
1063 const Elf_Ehdr *hdr,
1065 const char *secstrings)
1067 static unsigned long const masks[][2] = {
1068 /* NOTE: all executable code must be the first section
1069 * in this array; otherwise modify the text_size
1070 * finder in the two loops below */
1071 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1072 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1073 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1074 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1078 for (i = 0; i < hdr->e_shnum; i++)
1079 sechdrs[i].sh_entsize = ~0UL;
1081 DEBUGP("Core section allocation order:\n");
1082 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1083 for (i = 0; i < hdr->e_shnum; ++i) {
1084 Elf_Shdr *s = &sechdrs[i];
1086 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1087 || (s->sh_flags & masks[m][1])
1088 || s->sh_entsize != ~0UL
1089 || strncmp(secstrings + s->sh_name,
1092 s->sh_entsize = get_offset(&mod->core_size, s);
1093 DEBUGP("\t%s\n", secstrings + s->sh_name);
1096 mod->core_text_size = mod->core_size;
1099 DEBUGP("Init section allocation order:\n");
1100 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1101 for (i = 0; i < hdr->e_shnum; ++i) {
1102 Elf_Shdr *s = &sechdrs[i];
1104 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1105 || (s->sh_flags & masks[m][1])
1106 || s->sh_entsize != ~0UL
1107 || strncmp(secstrings + s->sh_name,
1110 s->sh_entsize = (get_offset(&mod->init_size, s)
1111 | INIT_OFFSET_MASK);
1112 DEBUGP("\t%s\n", secstrings + s->sh_name);
1115 mod->init_text_size = mod->init_size;
1119 static inline int license_is_gpl_compatible(const char *license)
1121 return (strcmp(license, "GPL") == 0
1122 || strcmp(license, "GPL v2") == 0
1123 || strcmp(license, "GPL and additional rights") == 0
1124 || strcmp(license, "Dual BSD/GPL") == 0
1125 || strcmp(license, "Dual MPL/GPL") == 0);
1128 static void set_license(struct module *mod, const char *license)
1131 license = "unspecified";
1133 mod->license_gplok = license_is_gpl_compatible(license);
1134 if (!mod->license_gplok) {
1135 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1136 mod->name, license);
1137 tainted |= TAINT_PROPRIETARY_MODULE;
1141 /* Parse tag=value strings from .modinfo section */
1142 static char *next_string(char *string, unsigned long *secsize)
1144 /* Skip non-zero chars */
1147 if ((*secsize)-- <= 1)
1151 /* Skip any zero padding. */
1152 while (!string[0]) {
1154 if ((*secsize)-- <= 1)
1160 static char *get_modinfo(Elf_Shdr *sechdrs,
1165 unsigned int taglen = strlen(tag);
1166 unsigned long size = sechdrs[info].sh_size;
1168 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1169 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1170 return p + taglen + 1;
1175 #ifdef CONFIG_KALLSYMS
1176 int is_exported(const char *name, const struct module *mod)
1181 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1182 if (strcmp(__start___ksymtab[i].name, name) == 0)
1186 for (i = 0; i < mod->num_syms; i++)
1187 if (strcmp(mod->syms[i].name, name) == 0)
1193 static char elf_type(const Elf_Sym *sym,
1195 const char *secstrings,
1198 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1199 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1204 if (sym->st_shndx == SHN_UNDEF)
1206 if (sym->st_shndx == SHN_ABS)
1208 if (sym->st_shndx >= SHN_LORESERVE)
1210 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1212 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1213 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1214 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1216 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1221 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1222 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1227 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1228 ".debug", strlen(".debug")) == 0)
1233 static void add_kallsyms(struct module *mod,
1235 unsigned int symindex,
1236 unsigned int strindex,
1237 const char *secstrings)
1241 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1242 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1243 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1245 /* Set types up while we still have access to sections. */
1246 for (i = 0; i < mod->num_symtab; i++)
1247 mod->symtab[i].st_info
1248 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1251 static inline void add_kallsyms(struct module *mod,
1253 unsigned int symindex,
1254 unsigned int strindex,
1255 const char *secstrings)
1258 #endif /* CONFIG_KALLSYMS */
1260 /* Allocate and load the module: note that size of section 0 is always
1261 zero, and we rely on this for optional sections. */
1262 static struct module *load_module(void __user *umod,
1264 const char __user *uargs)
1268 char *secstrings, *args, *modmagic, *strtab = NULL;
1269 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1270 exportindex, modindex, obsparmindex, infoindex, gplindex,
1271 crcindex, gplcrcindex, versindex, pcpuindex;
1275 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1276 struct exception_table_entry *extable;
1278 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1280 if (len < sizeof(*hdr))
1281 return ERR_PTR(-ENOEXEC);
1283 /* Suck in entire file: we'll want most of it. */
1284 /* vmalloc barfs on "unusual" numbers. Check here */
1285 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1286 return ERR_PTR(-ENOMEM);
1287 if (copy_from_user(hdr, umod, len) != 0) {
1292 /* Sanity checks against insmoding binaries or wrong arch,
1293 weird elf version */
1294 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1295 || hdr->e_type != ET_REL
1296 || !elf_check_arch(hdr)
1297 || hdr->e_shentsize != sizeof(*sechdrs)) {
1302 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1305 /* Convenience variables */
1306 sechdrs = (void *)hdr + hdr->e_shoff;
1307 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1308 sechdrs[0].sh_addr = 0;
1310 /* And these should exist, but gcc whinges if we don't init them */
1311 symindex = strindex = 0;
1313 for (i = 1; i < hdr->e_shnum; i++) {
1314 if (sechdrs[i].sh_type != SHT_NOBITS
1315 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1318 /* Mark all sections sh_addr with their address in the
1320 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1322 /* Internal symbols and strings. */
1323 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1325 strindex = sechdrs[i].sh_link;
1326 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1328 #ifndef CONFIG_MODULE_UNLOAD
1329 /* Don't load .exit sections */
1330 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1331 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1335 modindex = find_sec(hdr, sechdrs, secstrings,
1336 ".gnu.linkonce.this_module");
1338 printk(KERN_WARNING "No module found in object\n");
1342 mod = (void *)sechdrs[modindex].sh_addr;
1344 /* Optional sections */
1345 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1346 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1347 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1348 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1349 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1350 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1351 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1352 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1353 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1354 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1356 /* Don't keep modinfo section */
1357 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1358 #ifdef CONFIG_KALLSYMS
1359 /* Keep symbol and string tables for decoding later. */
1360 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1361 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1364 /* Check module struct version now, before we try to use module. */
1365 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1370 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1371 /* This is allowed: modprobe --force will invalidate it. */
1373 tainted |= TAINT_FORCED_MODULE;
1374 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1376 } else if (!same_magic(modmagic, vermagic)) {
1377 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1378 mod->name, modmagic, vermagic);
1383 /* Now copy in args */
1384 arglen = strlen_user(uargs);
1389 args = kmalloc(arglen, GFP_KERNEL);
1394 if (copy_from_user(args, uargs, arglen) != 0) {
1399 if (find_module(mod->name)) {
1404 mod->state = MODULE_STATE_COMING;
1406 /* Allow arches to frob section contents and sizes. */
1407 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1412 /* We have a special allocation for this section. */
1413 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1414 sechdrs[pcpuindex].sh_addralign);
1419 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1420 mod->percpu = percpu;
1423 /* Determine total sizes, and put offsets in sh_entsize. For now
1424 this is done generically; there doesn't appear to be any
1425 special cases for the architectures. */
1426 layout_sections(mod, hdr, sechdrs, secstrings);
1428 /* Do the allocs. */
1429 ptr = module_alloc(mod->core_size);
1434 memset(ptr, 0, mod->core_size);
1435 mod->module_core = ptr;
1437 ptr = module_alloc(mod->init_size);
1438 if (!ptr && mod->init_size) {
1442 memset(ptr, 0, mod->init_size);
1443 mod->module_init = ptr;
1445 /* Transfer each section which specifies SHF_ALLOC */
1446 DEBUGP("final section addresses:\n");
1447 for (i = 0; i < hdr->e_shnum; i++) {
1450 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1453 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1454 dest = mod->module_init
1455 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1457 dest = mod->module_core + sechdrs[i].sh_entsize;
1459 if (sechdrs[i].sh_type != SHT_NOBITS)
1460 memcpy(dest, (void *)sechdrs[i].sh_addr,
1461 sechdrs[i].sh_size);
1462 /* Update sh_addr to point to copy in image. */
1463 sechdrs[i].sh_addr = (unsigned long)dest;
1464 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1466 /* Module has been moved. */
1467 mod = (void *)sechdrs[modindex].sh_addr;
1469 /* Now we've moved module, initialize linked lists, etc. */
1470 module_unload_init(mod);
1472 /* Set up license info based on the info section */
1473 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1475 /* Fix up syms, so that st_value is a pointer to location. */
1476 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1481 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1482 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1483 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1485 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1486 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1487 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1489 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1491 #ifdef CONFIG_MODVERSIONS
1492 if ((mod->num_syms && !crcindex) ||
1493 (mod->num_gpl_syms && !gplcrcindex)) {
1494 printk(KERN_WARNING "%s: No versions for exported symbols."
1495 " Tainting kernel.\n", mod->name);
1496 tainted |= TAINT_FORCED_MODULE;
1500 /* Now do relocations. */
1501 for (i = 1; i < hdr->e_shnum; i++) {
1502 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1503 unsigned int info = sechdrs[i].sh_info;
1505 /* Not a valid relocation section? */
1506 if (info >= hdr->e_shnum)
1509 /* Don't bother with non-allocated sections */
1510 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1513 if (sechdrs[i].sh_type == SHT_REL)
1514 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1515 else if (sechdrs[i].sh_type == SHT_RELA)
1516 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1522 /* Set up and sort exception table */
1523 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1524 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1525 sort_extable(extable, extable + mod->num_exentries);
1527 /* Finally, copy percpu area over. */
1528 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1529 sechdrs[pcpuindex].sh_size);
1531 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1533 err = module_finalize(hdr, sechdrs, mod);
1539 err = obsolete_params(mod->name, mod->args,
1540 (struct obsolete_modparm *)
1541 sechdrs[obsparmindex].sh_addr,
1542 sechdrs[obsparmindex].sh_size
1543 / sizeof(struct obsolete_modparm),
1545 (char *)sechdrs[strindex].sh_addr);
1547 printk(KERN_WARNING "%s: Ignoring new-style "
1548 "parameters in presence of obsolete ones\n",
1551 /* Size of section 0 is 0, so this works well if no params */
1552 err = parse_args(mod->name, mod->args,
1553 (struct kernel_param *)
1554 sechdrs[setupindex].sh_addr,
1555 sechdrs[setupindex].sh_size
1556 / sizeof(struct kernel_param),
1562 /* Get rid of temporary copy */
1569 module_arch_cleanup(mod);
1571 module_unload_free(mod);
1572 module_free(mod, mod->module_init);
1574 module_free(mod, mod->module_core);
1577 percpu_modfree(percpu);
1582 if (err < 0) return ERR_PTR(err);
1586 printk(KERN_ERR "Module len %lu truncated\n", len);
1591 /* This is where the real work happens */
1593 sys_init_module(void __user *umod,
1595 const char __user *uargs)
1600 /* Must have permission */
1601 if (!capable(CAP_SYS_MODULE))
1604 /* Only one module load at a time, please */
1605 if (down_interruptible(&module_mutex) != 0)
1608 /* Do all the hard work */
1609 mod = load_module(umod, len, uargs);
1612 return PTR_ERR(mod);
1615 /* Flush the instruction cache, since we've played with text */
1616 if (mod->module_init)
1617 flush_icache_range((unsigned long)mod->module_init,
1618 (unsigned long)mod->module_init
1620 flush_icache_range((unsigned long)mod->module_core,
1621 (unsigned long)mod->module_core + mod->core_size);
1623 /* Now sew it into the lists. They won't access us, since
1624 strong_try_module_get() will fail. */
1625 spin_lock_irq(&modlist_lock);
1626 list_add(&mod->list, &modules);
1627 spin_unlock_irq(&modlist_lock);
1629 /* Drop lock so they can recurse */
1632 down(¬ify_mutex);
1633 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1636 /* Start the module */
1639 /* Init routine failed: abort. Try to protect us from
1640 buggy refcounters. */
1641 mod->state = MODULE_STATE_GOING;
1642 synchronize_kernel();
1644 printk(KERN_ERR "%s: module is now stuck!\n",
1648 down(&module_mutex);
1655 /* Now it's a first class citizen! */
1656 down(&module_mutex);
1657 mod->state = MODULE_STATE_LIVE;
1658 /* Drop initial reference. */
1660 module_free(mod, mod->module_init);
1661 mod->module_init = NULL;
1663 mod->init_text_size = 0;
1669 static inline int within(unsigned long addr, void *start, unsigned long size)
1671 return ((void *)addr >= start && (void *)addr < start + size);
1674 #ifdef CONFIG_KALLSYMS
1675 static const char *get_ksymbol(struct module *mod,
1677 unsigned long *size,
1678 unsigned long *offset)
1680 unsigned int i, best = 0;
1681 unsigned long nextval;
1683 /* At worse, next value is at end of module */
1684 if (within(addr, mod->module_init, mod->init_size))
1685 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1687 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1689 /* Scan for closest preceeding symbol, and next symbol. (ELF
1690 starts real symbols at 1). */
1691 for (i = 1; i < mod->num_symtab; i++) {
1692 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1695 /* We ignore unnamed symbols: they're uninformative
1696 * and inserted at a whim. */
1697 if (mod->symtab[i].st_value <= addr
1698 && mod->symtab[i].st_value > mod->symtab[best].st_value
1699 && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
1701 if (mod->symtab[i].st_value > addr
1702 && mod->symtab[i].st_value < nextval
1703 && *(mod->strtab + mod->symtab[i].st_name) != '\0')
1704 nextval = mod->symtab[i].st_value;
1710 *size = nextval - mod->symtab[best].st_value;
1711 *offset = addr - mod->symtab[best].st_value;
1712 return mod->strtab + mod->symtab[best].st_name;
1715 /* For kallsyms to ask for address resolution. NULL means not found.
1716 We don't lock, as this is used for oops resolution and races are a
1718 const char *module_address_lookup(unsigned long addr,
1719 unsigned long *size,
1720 unsigned long *offset,
1725 list_for_each_entry(mod, &modules, list) {
1726 if (within(addr, mod->module_init, mod->init_size)
1727 || within(addr, mod->module_core, mod->core_size)) {
1728 *modname = mod->name;
1729 return get_ksymbol(mod, addr, size, offset);
1735 struct module *module_get_kallsym(unsigned int symnum,
1736 unsigned long *value,
1742 down(&module_mutex);
1743 list_for_each_entry(mod, &modules, list) {
1744 if (symnum < mod->num_symtab) {
1745 *value = mod->symtab[symnum].st_value;
1746 *type = mod->symtab[symnum].st_info;
1748 mod->strtab + mod->symtab[symnum].st_name,
1753 symnum -= mod->num_symtab;
1759 static unsigned long mod_find_symname(struct module *mod, const char *name)
1763 for (i = 0; i < mod->num_symtab; i++)
1764 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
1765 return mod->symtab[i].st_value;
1769 /* Look for this name: can be of form module:name. */
1770 unsigned long module_kallsyms_lookup_name(const char *name)
1774 unsigned long ret = 0;
1776 /* Don't lock: we're in enough trouble already. */
1777 if ((colon = strchr(name, ':')) != NULL) {
1779 if ((mod = find_module(name)) != NULL)
1780 ret = mod_find_symname(mod, colon+1);
1783 list_for_each_entry(mod, &modules, list)
1784 if ((ret = mod_find_symname(mod, name)) != 0)
1789 #endif /* CONFIG_KALLSYMS */
1791 /* Called by the /proc file system to return a list of modules. */
1792 static void *m_start(struct seq_file *m, loff_t *pos)
1794 struct list_head *i;
1797 down(&module_mutex);
1798 list_for_each(i, &modules) {
1807 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1809 struct list_head *i = p;
1811 if (i->next == &modules)
1816 static void m_stop(struct seq_file *m, void *p)
1821 static int m_show(struct seq_file *m, void *p)
1823 struct module *mod = list_entry(p, struct module, list);
1824 seq_printf(m, "%s %lu",
1825 mod->name, mod->init_size + mod->core_size);
1826 print_unload_info(m, mod);
1828 /* Informative for users. */
1829 seq_printf(m, " %s",
1830 mod->state == MODULE_STATE_GOING ? "Unloading":
1831 mod->state == MODULE_STATE_COMING ? "Loading":
1833 /* Used by oprofile and other similar tools. */
1834 seq_printf(m, " 0x%p", mod->module_core);
1836 seq_printf(m, "\n");
1840 /* Format: modulename size refcount deps address
1842 Where refcount is a number or -, and deps is a comma-separated list
1845 struct seq_operations modules_op = {
1852 /* Given an address, look for it in the module exception tables. */
1853 const struct exception_table_entry *search_module_extables(unsigned long addr)
1855 unsigned long flags;
1856 const struct exception_table_entry *e = NULL;
1859 spin_lock_irqsave(&modlist_lock, flags);
1860 list_for_each_entry(mod, &modules, list) {
1861 if (mod->num_exentries == 0)
1864 e = search_extable(mod->extable,
1865 mod->extable + mod->num_exentries - 1,
1870 spin_unlock_irqrestore(&modlist_lock, flags);
1872 /* Now, if we found one, we are running inside it now, hence
1873 we cannot unload the module, hence no refcnt needed. */
1877 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
1878 struct module *module_text_address(unsigned long addr)
1882 list_for_each_entry(mod, &modules, list)
1883 if (within(addr, mod->module_init, mod->init_text_size)
1884 || within(addr, mod->module_core, mod->core_text_size))
1889 #ifdef CONFIG_MODVERSIONS
1890 /* Generate the signature for struct module here, too, for modversions. */
1891 void struct_module(struct module *mod) { return; }
1892 EXPORT_SYMBOL(struct_module);