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/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/elf.h>
27 #include <linux/seq_file.h>
28 #include <linux/syscalls.h>
29 #include <linux/fcntl.h>
30 #include <linux/rcupdate.h>
31 #include <linux/capability.h>
32 #include <linux/cpu.h>
33 #include <linux/moduleparam.h>
34 #include <linux/errno.h>
35 #include <linux/err.h>
36 #include <linux/vermagic.h>
37 #include <linux/notifier.h>
38 #include <linux/stop_machine.h>
39 #include <linux/device.h>
40 #include <linux/string.h>
41 #include <linux/sched.h>
42 #include <asm/uaccess.h>
43 #include <asm/semaphore.h>
44 #include <asm/cacheflush.h>
49 #define DEBUGP(fmt , a...)
52 #ifndef ARCH_SHF_SMALL
53 #define ARCH_SHF_SMALL 0
56 /* If this is set, the section belongs in the init part of the module */
57 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
59 /* Protects module list */
60 static DEFINE_SPINLOCK(modlist_lock);
62 /* List of modules, protected by module_mutex AND modlist_lock */
63 static DECLARE_MUTEX(module_mutex);
64 static LIST_HEAD(modules);
66 static DECLARE_MUTEX(notify_mutex);
67 static struct notifier_block * module_notify_list;
69 int register_module_notifier(struct notifier_block * nb)
73 err = notifier_chain_register(&module_notify_list, nb);
77 EXPORT_SYMBOL(register_module_notifier);
79 int unregister_module_notifier(struct notifier_block * nb)
83 err = notifier_chain_unregister(&module_notify_list, nb);
87 EXPORT_SYMBOL(unregister_module_notifier);
89 /* We require a truly strong try_module_get() */
90 static inline int strong_try_module_get(struct module *mod)
92 if (mod && mod->state == MODULE_STATE_COMING)
94 return try_module_get(mod);
97 /* A thread that wants to hold a reference to a module only while it
98 * is running can call ths to safely exit.
99 * nfsd and lockd use this.
101 void __module_put_and_exit(struct module *mod, long code)
106 EXPORT_SYMBOL(__module_put_and_exit);
108 /* Find a module section: 0 means not found. */
109 static unsigned int find_sec(Elf_Ehdr *hdr,
111 const char *secstrings,
116 for (i = 1; i < hdr->e_shnum; i++)
117 /* Alloc bit cleared means "ignore it." */
118 if ((sechdrs[i].sh_flags & SHF_ALLOC)
119 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
124 /* Provided by the linker */
125 extern const struct kernel_symbol __start___ksymtab[];
126 extern const struct kernel_symbol __stop___ksymtab[];
127 extern const struct kernel_symbol __start___ksymtab_gpl[];
128 extern const struct kernel_symbol __stop___ksymtab_gpl[];
129 extern const unsigned long __start___kcrctab[];
130 extern const unsigned long __start___kcrctab_gpl[];
132 #ifndef CONFIG_MODVERSIONS
133 #define symversion(base, idx) NULL
135 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
138 /* lookup symbol in given range of kernel_symbols */
139 static const struct kernel_symbol *lookup_symbol(const char *name,
140 const struct kernel_symbol *start,
141 const struct kernel_symbol *stop)
143 const struct kernel_symbol *ks = start;
144 for (; ks < stop; ks++)
145 if (strcmp(ks->name, name) == 0)
150 /* Find a symbol, return value, crc and module which owns it */
151 static unsigned long __find_symbol(const char *name,
152 struct module **owner,
153 const unsigned long **crc,
157 const struct kernel_symbol *ks;
159 /* Core kernel first. */
161 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
163 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
167 ks = lookup_symbol(name, __start___ksymtab_gpl,
168 __stop___ksymtab_gpl);
170 *crc = symversion(__start___kcrctab_gpl,
171 (ks - __start___ksymtab_gpl));
176 /* Now try modules. */
177 list_for_each_entry(mod, &modules, list) {
179 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
181 *crc = symversion(mod->crcs, (ks - mod->syms));
186 ks = lookup_symbol(name, mod->gpl_syms,
187 mod->gpl_syms + mod->num_gpl_syms);
189 *crc = symversion(mod->gpl_crcs,
190 (ks - mod->gpl_syms));
195 DEBUGP("Failed to find symbol %s\n", name);
199 /* Find a symbol in this elf symbol table */
200 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
201 unsigned int symindex,
206 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
208 /* Search (defined) internal symbols first. */
209 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
210 if (sym[i].st_shndx != SHN_UNDEF
211 && strcmp(name, strtab + sym[i].st_name) == 0)
212 return sym[i].st_value;
217 /* Search for module by name: must hold module_mutex. */
218 static struct module *find_module(const char *name)
222 list_for_each_entry(mod, &modules, list) {
223 if (strcmp(mod->name, name) == 0)
230 /* Number of blocks used and allocated. */
231 static unsigned int pcpu_num_used, pcpu_num_allocated;
232 /* Size of each block. -ve means used. */
233 static int *pcpu_size;
235 static int split_block(unsigned int i, unsigned short size)
237 /* Reallocation required? */
238 if (pcpu_num_used + 1 > pcpu_num_allocated) {
239 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
244 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
245 pcpu_num_allocated *= 2;
250 /* Insert a new subblock */
251 memmove(&pcpu_size[i+1], &pcpu_size[i],
252 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
255 pcpu_size[i+1] -= size;
260 static inline unsigned int block_size(int val)
267 /* Created by linker magic */
268 extern char __per_cpu_start[], __per_cpu_end[];
270 static void *percpu_modalloc(unsigned long size, unsigned long align,
277 if (align > SMP_CACHE_BYTES) {
278 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
279 name, align, SMP_CACHE_BYTES);
280 align = SMP_CACHE_BYTES;
283 ptr = __per_cpu_start;
284 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
285 /* Extra for alignment requirement. */
286 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
287 BUG_ON(i == 0 && extra != 0);
289 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
292 /* Transfer extra to previous block. */
293 if (pcpu_size[i-1] < 0)
294 pcpu_size[i-1] -= extra;
296 pcpu_size[i-1] += extra;
297 pcpu_size[i] -= extra;
300 /* Split block if warranted */
301 if (pcpu_size[i] - size > sizeof(unsigned long))
302 if (!split_block(i, size))
306 pcpu_size[i] = -pcpu_size[i];
310 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
315 static void percpu_modfree(void *freeme)
318 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
320 /* First entry is core kernel percpu data. */
321 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
323 pcpu_size[i] = -pcpu_size[i];
330 /* Merge with previous? */
331 if (pcpu_size[i-1] >= 0) {
332 pcpu_size[i-1] += pcpu_size[i];
334 memmove(&pcpu_size[i], &pcpu_size[i+1],
335 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
338 /* Merge with next? */
339 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
340 pcpu_size[i] += pcpu_size[i+1];
342 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
343 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
347 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
349 const char *secstrings)
351 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
354 static int percpu_modinit(void)
357 pcpu_num_allocated = 2;
358 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
360 /* Static in-kernel percpu data (used). */
361 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
363 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
364 if (pcpu_size[1] < 0) {
365 printk(KERN_ERR "No per-cpu room for modules.\n");
371 __initcall(percpu_modinit);
372 #else /* ... !CONFIG_SMP */
373 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
378 static inline void percpu_modfree(void *pcpuptr)
382 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
384 const char *secstrings)
388 static inline void percpu_modcopy(void *pcpudst, const void *src,
391 /* pcpusec should be 0, and size of that section should be 0. */
394 #endif /* CONFIG_SMP */
396 #ifdef CONFIG_MODULE_UNLOAD
397 #define MODINFO_ATTR(field) \
398 static void setup_modinfo_##field(struct module *mod, const char *s) \
400 mod->field = kstrdup(s, GFP_KERNEL); \
402 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
403 struct module *mod, char *buffer) \
405 return sprintf(buffer, "%s\n", mod->field); \
407 static int modinfo_##field##_exists(struct module *mod) \
409 return mod->field != NULL; \
411 static void free_modinfo_##field(struct module *mod) \
416 static struct module_attribute modinfo_##field = { \
417 .attr = { .name = __stringify(field), .mode = 0444, \
418 .owner = THIS_MODULE }, \
419 .show = show_modinfo_##field, \
420 .setup = setup_modinfo_##field, \
421 .test = modinfo_##field##_exists, \
422 .free = free_modinfo_##field, \
425 MODINFO_ATTR(version);
426 MODINFO_ATTR(srcversion);
428 static struct module_attribute *modinfo_attrs[] = {
434 /* Init the unload section of the module. */
435 static void module_unload_init(struct module *mod)
439 INIT_LIST_HEAD(&mod->modules_which_use_me);
440 for (i = 0; i < NR_CPUS; i++)
441 local_set(&mod->ref[i].count, 0);
442 /* Hold reference count during initialization. */
443 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
444 /* Backwards compatibility macros put refcount during init. */
445 mod->waiter = current;
448 /* modules using other modules */
451 struct list_head list;
452 struct module *module_which_uses;
455 /* Does a already use b? */
456 static int already_uses(struct module *a, struct module *b)
458 struct module_use *use;
460 list_for_each_entry(use, &b->modules_which_use_me, list) {
461 if (use->module_which_uses == a) {
462 DEBUGP("%s uses %s!\n", a->name, b->name);
466 DEBUGP("%s does not use %s!\n", a->name, b->name);
470 /* Module a uses b */
471 static int use_module(struct module *a, struct module *b)
473 struct module_use *use;
474 if (b == NULL || already_uses(a, b)) return 1;
476 if (!strong_try_module_get(b))
479 DEBUGP("Allocating new usage for %s.\n", a->name);
480 use = kmalloc(sizeof(*use), GFP_ATOMIC);
482 printk("%s: out of memory loading\n", a->name);
487 use->module_which_uses = a;
488 list_add(&use->list, &b->modules_which_use_me);
492 /* Clear the unload stuff of the module. */
493 static void module_unload_free(struct module *mod)
497 list_for_each_entry(i, &modules, list) {
498 struct module_use *use;
500 list_for_each_entry(use, &i->modules_which_use_me, list) {
501 if (use->module_which_uses == mod) {
502 DEBUGP("%s unusing %s\n", mod->name, i->name);
504 list_del(&use->list);
506 /* There can be at most one match. */
513 #ifdef CONFIG_MODULE_FORCE_UNLOAD
514 static inline int try_force_unload(unsigned int flags)
516 int ret = (flags & O_TRUNC);
518 add_taint(TAINT_FORCED_RMMOD);
522 static inline int try_force_unload(unsigned int flags)
526 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
535 /* Whole machine is stopped with interrupts off when this runs. */
536 static int __try_stop_module(void *_sref)
538 struct stopref *sref = _sref;
540 /* If it's not unused, quit unless we are told to block. */
541 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
542 if (!(*sref->forced = try_force_unload(sref->flags)))
546 /* Mark it as dying. */
547 sref->mod->state = MODULE_STATE_GOING;
551 static int try_stop_module(struct module *mod, int flags, int *forced)
553 struct stopref sref = { mod, flags, forced };
555 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
558 unsigned int module_refcount(struct module *mod)
560 unsigned int i, total = 0;
562 for (i = 0; i < NR_CPUS; i++)
563 total += local_read(&mod->ref[i].count);
566 EXPORT_SYMBOL(module_refcount);
568 /* This exists whether we can unload or not */
569 static void free_module(struct module *mod);
571 static void wait_for_zero_refcount(struct module *mod)
573 /* Since we might sleep for some time, drop the semaphore first */
576 DEBUGP("Looking at refcount...\n");
577 set_current_state(TASK_UNINTERRUPTIBLE);
578 if (module_refcount(mod) == 0)
582 current->state = TASK_RUNNING;
587 sys_delete_module(const char __user *name_user, unsigned int flags)
590 char name[MODULE_NAME_LEN];
593 if (!capable(CAP_SYS_MODULE))
596 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
598 name[MODULE_NAME_LEN-1] = '\0';
600 if (down_interruptible(&module_mutex) != 0)
603 mod = find_module(name);
609 if (!list_empty(&mod->modules_which_use_me)) {
610 /* Other modules depend on us: get rid of them first. */
615 /* Doing init or already dying? */
616 if (mod->state != MODULE_STATE_LIVE) {
617 /* FIXME: if (force), slam module count and wake up
619 DEBUGP("%s already dying\n", mod->name);
624 /* If it has an init func, it must have an exit func to unload */
625 if ((mod->init != NULL && mod->exit == NULL)
627 forced = try_force_unload(flags);
629 /* This module can't be removed */
635 /* Set this up before setting mod->state */
636 mod->waiter = current;
638 /* Stop the machine so refcounts can't move and disable module. */
639 ret = try_stop_module(mod, flags, &forced);
643 /* Never wait if forced. */
644 if (!forced && module_refcount(mod) != 0)
645 wait_for_zero_refcount(mod);
647 /* Final destruction now noone is using it. */
648 if (mod->exit != NULL) {
660 static void print_unload_info(struct seq_file *m, struct module *mod)
662 struct module_use *use;
663 int printed_something = 0;
665 seq_printf(m, " %u ", module_refcount(mod));
667 /* Always include a trailing , so userspace can differentiate
668 between this and the old multi-field proc format. */
669 list_for_each_entry(use, &mod->modules_which_use_me, list) {
670 printed_something = 1;
671 seq_printf(m, "%s,", use->module_which_uses->name);
675 printed_something = 1;
676 seq_printf(m, "[unsafe],");
679 if (mod->init != NULL && mod->exit == NULL) {
680 printed_something = 1;
681 seq_printf(m, "[permanent],");
684 if (!printed_something)
688 void __symbol_put(const char *symbol)
690 struct module *owner;
692 const unsigned long *crc;
694 spin_lock_irqsave(&modlist_lock, flags);
695 if (!__find_symbol(symbol, &owner, &crc, 1))
698 spin_unlock_irqrestore(&modlist_lock, flags);
700 EXPORT_SYMBOL(__symbol_put);
702 void symbol_put_addr(void *addr)
706 spin_lock_irqsave(&modlist_lock, flags);
707 if (!kernel_text_address((unsigned long)addr))
710 module_put(module_text_address((unsigned long)addr));
711 spin_unlock_irqrestore(&modlist_lock, flags);
713 EXPORT_SYMBOL_GPL(symbol_put_addr);
715 static ssize_t show_refcnt(struct module_attribute *mattr,
716 struct module *mod, char *buffer)
718 /* sysfs holds a reference */
719 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
722 static struct module_attribute refcnt = {
723 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
727 #else /* !CONFIG_MODULE_UNLOAD */
728 static void print_unload_info(struct seq_file *m, struct module *mod)
730 /* We don't know the usage count, or what modules are using. */
731 seq_printf(m, " - -");
734 static inline void module_unload_free(struct module *mod)
738 static inline int use_module(struct module *a, struct module *b)
740 return strong_try_module_get(b);
743 static inline void module_unload_init(struct module *mod)
746 #endif /* CONFIG_MODULE_UNLOAD */
748 #ifdef CONFIG_OBSOLETE_MODPARM
749 /* Bounds checking done below */
750 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
752 strcpy(kp->arg, val);
756 static int set_obsolete(const char *val, struct kernel_param *kp)
758 unsigned int min, max;
759 unsigned int size, maxsize;
763 struct obsolete_modparm *obsparm = kp->arg;
766 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
770 /* type is: [min[-max]]{b,h,i,l,s} */
772 min = simple_strtol(p, &endp, 10);
773 if (endp == obsparm->type)
775 else if (*endp == '-') {
777 max = simple_strtol(p, &endp, 10);
782 return param_array(kp->name, val, min, max, obsparm->addr,
783 1, param_set_byte, &dummy);
785 return param_array(kp->name, val, min, max, obsparm->addr,
786 sizeof(short), param_set_short, &dummy);
788 return param_array(kp->name, val, min, max, obsparm->addr,
789 sizeof(int), param_set_int, &dummy);
791 return param_array(kp->name, val, min, max, obsparm->addr,
792 sizeof(long), param_set_long, &dummy);
794 return param_array(kp->name, val, min, max, obsparm->addr,
795 sizeof(char *), param_set_charp, &dummy);
798 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
799 and the decl is "char xxx[5][50];" */
801 maxsize = simple_strtol(p, &endp, 10);
802 /* We check lengths here (yes, this is a hack). */
804 while (p[size = strcspn(p, ",")]) {
811 return param_array(kp->name, val, min, max, obsparm->addr,
812 maxsize, obsparm_copy_string, &dummy);
814 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
818 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
822 static int obsolete_params(const char *name,
824 struct obsolete_modparm obsparm[],
827 unsigned int symindex,
830 struct kernel_param *kp;
834 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
838 for (i = 0; i < num; i++) {
839 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
841 snprintf(sym_name, sizeof(sym_name), "%s%s",
842 MODULE_SYMBOL_PREFIX, obsparm[i].name);
844 kp[i].name = obsparm[i].name;
846 kp[i].set = set_obsolete;
849 = (void *)find_local_symbol(sechdrs, symindex, strtab,
851 if (!obsparm[i].addr) {
852 printk("%s: falsely claims to have parameter %s\n",
853 name, obsparm[i].name);
857 kp[i].arg = &obsparm[i];
860 ret = parse_args(name, args, kp, num, NULL);
866 static int obsolete_params(const char *name,
868 struct obsolete_modparm obsparm[],
871 unsigned int symindex,
875 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
879 #endif /* CONFIG_OBSOLETE_MODPARM */
881 static const char vermagic[] = VERMAGIC_STRING;
883 #ifdef CONFIG_MODVERSIONS
884 static int check_version(Elf_Shdr *sechdrs,
885 unsigned int versindex,
888 const unsigned long *crc)
890 unsigned int i, num_versions;
891 struct modversion_info *versions;
893 /* Exporting module didn't supply crcs? OK, we're already tainted. */
897 versions = (void *) sechdrs[versindex].sh_addr;
898 num_versions = sechdrs[versindex].sh_size
899 / sizeof(struct modversion_info);
901 for (i = 0; i < num_versions; i++) {
902 if (strcmp(versions[i].name, symname) != 0)
905 if (versions[i].crc == *crc)
907 printk("%s: disagrees about version of symbol %s\n",
909 DEBUGP("Found checksum %lX vs module %lX\n",
910 *crc, versions[i].crc);
913 /* Not in module's version table. OK, but that taints the kernel. */
914 if (!(tainted & TAINT_FORCED_MODULE)) {
915 printk("%s: no version for \"%s\" found: kernel tainted.\n",
917 add_taint(TAINT_FORCED_MODULE);
922 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
923 unsigned int versindex,
926 const unsigned long *crc;
927 struct module *owner;
929 if (!__find_symbol("struct_module", &owner, &crc, 1))
931 return check_version(sechdrs, versindex, "struct_module", mod,
935 /* First part is kernel version, which we ignore. */
936 static inline int same_magic(const char *amagic, const char *bmagic)
938 amagic += strcspn(amagic, " ");
939 bmagic += strcspn(bmagic, " ");
940 return strcmp(amagic, bmagic) == 0;
943 static inline int check_version(Elf_Shdr *sechdrs,
944 unsigned int versindex,
947 const unsigned long *crc)
952 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
953 unsigned int versindex,
959 static inline int same_magic(const char *amagic, const char *bmagic)
961 return strcmp(amagic, bmagic) == 0;
963 #endif /* CONFIG_MODVERSIONS */
965 /* Resolve a symbol for this module. I.e. if we find one, record usage.
966 Must be holding module_mutex. */
967 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
968 unsigned int versindex,
972 struct module *owner;
974 const unsigned long *crc;
976 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
978 /* use_module can fail due to OOM, or module unloading */
979 if (!check_version(sechdrs, versindex, name, mod, crc) ||
980 !use_module(mod, owner))
988 * /sys/module/foo/sections stuff
989 * J. Corbet <corbet@lwn.net>
991 #ifdef CONFIG_KALLSYMS
992 static ssize_t module_sect_show(struct module_attribute *mattr,
993 struct module *mod, char *buf)
995 struct module_sect_attr *sattr =
996 container_of(mattr, struct module_sect_attr, mattr);
997 return sprintf(buf, "0x%lx\n", sattr->address);
1000 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1001 char *secstrings, Elf_Shdr *sechdrs)
1003 unsigned int nloaded = 0, i, size[2];
1004 struct module_sect_attrs *sect_attrs;
1005 struct module_sect_attr *sattr;
1006 struct attribute **gattr;
1008 /* Count loaded sections and allocate structures */
1009 for (i = 0; i < nsect; i++)
1010 if (sechdrs[i].sh_flags & SHF_ALLOC)
1012 size[0] = ALIGN(sizeof(*sect_attrs)
1013 + nloaded * sizeof(sect_attrs->attrs[0]),
1014 sizeof(sect_attrs->grp.attrs[0]));
1015 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1016 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
1019 /* Setup section attributes. */
1020 sect_attrs->grp.name = "sections";
1021 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1023 sattr = §_attrs->attrs[0];
1024 gattr = §_attrs->grp.attrs[0];
1025 for (i = 0; i < nsect; i++) {
1026 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1028 sattr->address = sechdrs[i].sh_addr;
1029 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1030 MODULE_SECT_NAME_LEN);
1031 sattr->mattr.show = module_sect_show;
1032 sattr->mattr.store = NULL;
1033 sattr->mattr.attr.name = sattr->name;
1034 sattr->mattr.attr.owner = mod;
1035 sattr->mattr.attr.mode = S_IRUGO;
1036 *(gattr++) = &(sattr++)->mattr.attr;
1040 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1043 mod->sect_attrs = sect_attrs;
1049 static void remove_sect_attrs(struct module *mod)
1051 if (mod->sect_attrs) {
1052 sysfs_remove_group(&mod->mkobj.kobj,
1053 &mod->sect_attrs->grp);
1054 /* We are positive that no one is using any sect attrs
1055 * at this point. Deallocate immediately. */
1056 kfree(mod->sect_attrs);
1057 mod->sect_attrs = NULL;
1063 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1064 char *sectstrings, Elf_Shdr *sechdrs)
1068 static inline void remove_sect_attrs(struct module *mod)
1071 #endif /* CONFIG_KALLSYMS */
1074 #ifdef CONFIG_MODULE_UNLOAD
1075 static inline int module_add_refcnt_attr(struct module *mod)
1077 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1079 static void module_remove_refcnt_attr(struct module *mod)
1081 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1084 static inline int module_add_refcnt_attr(struct module *mod)
1088 static void module_remove_refcnt_attr(struct module *mod)
1093 #ifdef CONFIG_MODULE_UNLOAD
1094 static int module_add_modinfo_attrs(struct module *mod)
1096 struct module_attribute *attr;
1100 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1102 (attr->test && attr->test(mod)))
1103 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1108 static void module_remove_modinfo_attrs(struct module *mod)
1110 struct module_attribute *attr;
1113 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1114 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1120 static int mod_sysfs_setup(struct module *mod,
1121 struct kernel_param *kparam,
1122 unsigned int num_params)
1126 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1127 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1130 kobj_set_kset_s(&mod->mkobj, module_subsys);
1131 mod->mkobj.mod = mod;
1132 err = kobject_register(&mod->mkobj.kobj);
1136 err = module_add_refcnt_attr(mod);
1140 err = module_param_sysfs_setup(mod, kparam, num_params);
1144 #ifdef CONFIG_MODULE_UNLOAD
1145 err = module_add_modinfo_attrs(mod);
1153 kobject_unregister(&mod->mkobj.kobj);
1158 static void mod_kobject_remove(struct module *mod)
1160 #ifdef CONFIG_MODULE_UNLOAD
1161 module_remove_modinfo_attrs(mod);
1163 module_remove_refcnt_attr(mod);
1164 module_param_sysfs_remove(mod);
1166 kobject_unregister(&mod->mkobj.kobj);
1170 * unlink the module with the whole machine is stopped with interrupts off
1171 * - this defends against kallsyms not taking locks
1173 static int __unlink_module(void *_mod)
1175 struct module *mod = _mod;
1176 list_del(&mod->list);
1180 /* Free a module, remove from lists, etc (must hold module mutex). */
1181 static void free_module(struct module *mod)
1183 /* Delete from various lists */
1184 stop_machine_run(__unlink_module, mod, NR_CPUS);
1185 remove_sect_attrs(mod);
1186 mod_kobject_remove(mod);
1188 /* Arch-specific cleanup. */
1189 module_arch_cleanup(mod);
1191 /* Module unload stuff */
1192 module_unload_free(mod);
1194 /* This may be NULL, but that's OK */
1195 module_free(mod, mod->module_init);
1198 percpu_modfree(mod->percpu);
1200 /* Finally, free the core (containing the module structure) */
1201 module_free(mod, mod->module_core);
1204 void *__symbol_get(const char *symbol)
1206 struct module *owner;
1207 unsigned long value, flags;
1208 const unsigned long *crc;
1210 spin_lock_irqsave(&modlist_lock, flags);
1211 value = __find_symbol(symbol, &owner, &crc, 1);
1212 if (value && !strong_try_module_get(owner))
1214 spin_unlock_irqrestore(&modlist_lock, flags);
1216 return (void *)value;
1218 EXPORT_SYMBOL_GPL(__symbol_get);
1221 * Ensure that an exported symbol [global namespace] does not already exist
1222 * in the Kernel or in some other modules exported symbol table.
1224 static int verify_export_symbols(struct module *mod)
1226 const char *name = NULL;
1227 unsigned long i, ret = 0;
1228 struct module *owner;
1229 const unsigned long *crc;
1231 for (i = 0; i < mod->num_syms; i++)
1232 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1233 name = mod->syms[i].name;
1238 for (i = 0; i < mod->num_gpl_syms; i++)
1239 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1240 name = mod->gpl_syms[i].name;
1247 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1248 mod->name, name, module_name(owner));
1253 /* Change all symbols so that sh_value encodes the pointer directly. */
1254 static int simplify_symbols(Elf_Shdr *sechdrs,
1255 unsigned int symindex,
1257 unsigned int versindex,
1258 unsigned int pcpuindex,
1261 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1262 unsigned long secbase;
1263 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1266 for (i = 1; i < n; i++) {
1267 switch (sym[i].st_shndx) {
1269 /* We compiled with -fno-common. These are not
1270 supposed to happen. */
1271 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1272 printk("%s: please compile with -fno-common\n",
1278 /* Don't need to do anything */
1279 DEBUGP("Absolute symbol: 0x%08lx\n",
1280 (long)sym[i].st_value);
1285 = resolve_symbol(sechdrs, versindex,
1286 strtab + sym[i].st_name, mod);
1288 /* Ok if resolved. */
1289 if (sym[i].st_value != 0)
1292 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1295 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1296 mod->name, strtab + sym[i].st_name);
1301 /* Divert to percpu allocation if a percpu var. */
1302 if (sym[i].st_shndx == pcpuindex)
1303 secbase = (unsigned long)mod->percpu;
1305 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1306 sym[i].st_value += secbase;
1314 /* Update size with this section: return offset. */
1315 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1319 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1320 *size = ret + sechdr->sh_size;
1324 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1325 might -- code, read-only data, read-write data, small data. Tally
1326 sizes, and place the offsets into sh_entsize fields: high bit means it
1328 static void layout_sections(struct module *mod,
1329 const Elf_Ehdr *hdr,
1331 const char *secstrings)
1333 static unsigned long const masks[][2] = {
1334 /* NOTE: all executable code must be the first section
1335 * in this array; otherwise modify the text_size
1336 * finder in the two loops below */
1337 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1338 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1339 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1340 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1344 for (i = 0; i < hdr->e_shnum; i++)
1345 sechdrs[i].sh_entsize = ~0UL;
1347 DEBUGP("Core section allocation order:\n");
1348 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1349 for (i = 0; i < hdr->e_shnum; ++i) {
1350 Elf_Shdr *s = &sechdrs[i];
1352 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1353 || (s->sh_flags & masks[m][1])
1354 || s->sh_entsize != ~0UL
1355 || strncmp(secstrings + s->sh_name,
1358 s->sh_entsize = get_offset(&mod->core_size, s);
1359 DEBUGP("\t%s\n", secstrings + s->sh_name);
1362 mod->core_text_size = mod->core_size;
1365 DEBUGP("Init section allocation order:\n");
1366 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1367 for (i = 0; i < hdr->e_shnum; ++i) {
1368 Elf_Shdr *s = &sechdrs[i];
1370 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1371 || (s->sh_flags & masks[m][1])
1372 || s->sh_entsize != ~0UL
1373 || strncmp(secstrings + s->sh_name,
1376 s->sh_entsize = (get_offset(&mod->init_size, s)
1377 | INIT_OFFSET_MASK);
1378 DEBUGP("\t%s\n", secstrings + s->sh_name);
1381 mod->init_text_size = mod->init_size;
1385 static inline int license_is_gpl_compatible(const char *license)
1387 return (strcmp(license, "GPL") == 0
1388 || strcmp(license, "GPL v2") == 0
1389 || strcmp(license, "GPL and additional rights") == 0
1390 || strcmp(license, "Dual BSD/GPL") == 0
1391 || strcmp(license, "Dual MPL/GPL") == 0);
1394 static void set_license(struct module *mod, const char *license)
1397 license = "unspecified";
1399 mod->license_gplok = license_is_gpl_compatible(license);
1400 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1401 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1402 mod->name, license);
1403 add_taint(TAINT_PROPRIETARY_MODULE);
1407 /* Parse tag=value strings from .modinfo section */
1408 static char *next_string(char *string, unsigned long *secsize)
1410 /* Skip non-zero chars */
1413 if ((*secsize)-- <= 1)
1417 /* Skip any zero padding. */
1418 while (!string[0]) {
1420 if ((*secsize)-- <= 1)
1426 static char *get_modinfo(Elf_Shdr *sechdrs,
1431 unsigned int taglen = strlen(tag);
1432 unsigned long size = sechdrs[info].sh_size;
1434 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1435 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1436 return p + taglen + 1;
1441 #ifdef CONFIG_MODULE_UNLOAD
1442 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1443 unsigned int infoindex)
1445 struct module_attribute *attr;
1448 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1451 get_modinfo(sechdrs,
1458 #ifdef CONFIG_KALLSYMS
1459 int is_exported(const char *name, const struct module *mod)
1461 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1464 if (lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1471 static char elf_type(const Elf_Sym *sym,
1473 const char *secstrings,
1476 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1477 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1482 if (sym->st_shndx == SHN_UNDEF)
1484 if (sym->st_shndx == SHN_ABS)
1486 if (sym->st_shndx >= SHN_LORESERVE)
1488 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1490 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1491 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1492 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1494 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1499 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1500 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1505 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1506 ".debug", strlen(".debug")) == 0)
1511 static void add_kallsyms(struct module *mod,
1513 unsigned int symindex,
1514 unsigned int strindex,
1515 const char *secstrings)
1519 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1520 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1521 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1523 /* Set types up while we still have access to sections. */
1524 for (i = 0; i < mod->num_symtab; i++)
1525 mod->symtab[i].st_info
1526 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1529 static inline void add_kallsyms(struct module *mod,
1531 unsigned int symindex,
1532 unsigned int strindex,
1533 const char *secstrings)
1536 #endif /* CONFIG_KALLSYMS */
1538 /* Allocate and load the module: note that size of section 0 is always
1539 zero, and we rely on this for optional sections. */
1540 static struct module *load_module(void __user *umod,
1542 const char __user *uargs)
1546 char *secstrings, *args, *modmagic, *strtab = NULL;
1547 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1548 exportindex, modindex, obsparmindex, infoindex, gplindex,
1549 crcindex, gplcrcindex, versindex, pcpuindex;
1553 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1554 struct exception_table_entry *extable;
1555 mm_segment_t old_fs;
1557 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1559 if (len < sizeof(*hdr))
1560 return ERR_PTR(-ENOEXEC);
1562 /* Suck in entire file: we'll want most of it. */
1563 /* vmalloc barfs on "unusual" numbers. Check here */
1564 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1565 return ERR_PTR(-ENOMEM);
1566 if (copy_from_user(hdr, umod, len) != 0) {
1571 /* Sanity checks against insmoding binaries or wrong arch,
1572 weird elf version */
1573 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1574 || hdr->e_type != ET_REL
1575 || !elf_check_arch(hdr)
1576 || hdr->e_shentsize != sizeof(*sechdrs)) {
1581 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1584 /* Convenience variables */
1585 sechdrs = (void *)hdr + hdr->e_shoff;
1586 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1587 sechdrs[0].sh_addr = 0;
1589 for (i = 1; i < hdr->e_shnum; i++) {
1590 if (sechdrs[i].sh_type != SHT_NOBITS
1591 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1594 /* Mark all sections sh_addr with their address in the
1596 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1598 /* Internal symbols and strings. */
1599 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1601 strindex = sechdrs[i].sh_link;
1602 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1604 #ifndef CONFIG_MODULE_UNLOAD
1605 /* Don't load .exit sections */
1606 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1607 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1611 modindex = find_sec(hdr, sechdrs, secstrings,
1612 ".gnu.linkonce.this_module");
1614 printk(KERN_WARNING "No module found in object\n");
1618 mod = (void *)sechdrs[modindex].sh_addr;
1620 if (symindex == 0) {
1621 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1627 /* Optional sections */
1628 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1629 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1630 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1631 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1632 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1633 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1634 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1635 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1636 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1637 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1639 /* Don't keep modinfo section */
1640 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1641 #ifdef CONFIG_KALLSYMS
1642 /* Keep symbol and string tables for decoding later. */
1643 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1644 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1647 /* Check module struct version now, before we try to use module. */
1648 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1653 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1654 /* This is allowed: modprobe --force will invalidate it. */
1656 add_taint(TAINT_FORCED_MODULE);
1657 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1659 } else if (!same_magic(modmagic, vermagic)) {
1660 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1661 mod->name, modmagic, vermagic);
1666 /* Now copy in args */
1667 arglen = strlen_user(uargs);
1672 args = kmalloc(arglen, GFP_KERNEL);
1677 if (copy_from_user(args, uargs, arglen) != 0) {
1682 /* Userspace could have altered the string after the strlen_user() */
1683 args[arglen - 1] = '\0';
1685 if (find_module(mod->name)) {
1690 mod->state = MODULE_STATE_COMING;
1692 /* Allow arches to frob section contents and sizes. */
1693 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1698 /* We have a special allocation for this section. */
1699 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1700 sechdrs[pcpuindex].sh_addralign,
1706 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1707 mod->percpu = percpu;
1710 /* Determine total sizes, and put offsets in sh_entsize. For now
1711 this is done generically; there doesn't appear to be any
1712 special cases for the architectures. */
1713 layout_sections(mod, hdr, sechdrs, secstrings);
1715 /* Do the allocs. */
1716 ptr = module_alloc(mod->core_size);
1721 memset(ptr, 0, mod->core_size);
1722 mod->module_core = ptr;
1724 ptr = module_alloc(mod->init_size);
1725 if (!ptr && mod->init_size) {
1729 memset(ptr, 0, mod->init_size);
1730 mod->module_init = ptr;
1732 /* Transfer each section which specifies SHF_ALLOC */
1733 DEBUGP("final section addresses:\n");
1734 for (i = 0; i < hdr->e_shnum; i++) {
1737 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1740 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1741 dest = mod->module_init
1742 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1744 dest = mod->module_core + sechdrs[i].sh_entsize;
1746 if (sechdrs[i].sh_type != SHT_NOBITS)
1747 memcpy(dest, (void *)sechdrs[i].sh_addr,
1748 sechdrs[i].sh_size);
1749 /* Update sh_addr to point to copy in image. */
1750 sechdrs[i].sh_addr = (unsigned long)dest;
1751 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1753 /* Module has been moved. */
1754 mod = (void *)sechdrs[modindex].sh_addr;
1756 /* Now we've moved module, initialize linked lists, etc. */
1757 module_unload_init(mod);
1759 /* Set up license info based on the info section */
1760 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1762 if (strcmp(mod->name, "ndiswrapper") == 0)
1763 add_taint(TAINT_PROPRIETARY_MODULE);
1764 if (strcmp(mod->name, "driverloader") == 0)
1765 add_taint(TAINT_PROPRIETARY_MODULE);
1767 #ifdef CONFIG_MODULE_UNLOAD
1768 /* Set up MODINFO_ATTR fields */
1769 setup_modinfo(mod, sechdrs, infoindex);
1772 /* Fix up syms, so that st_value is a pointer to location. */
1773 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1778 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1779 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1780 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1782 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1783 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1784 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1786 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1788 #ifdef CONFIG_MODVERSIONS
1789 if ((mod->num_syms && !crcindex) ||
1790 (mod->num_gpl_syms && !gplcrcindex)) {
1791 printk(KERN_WARNING "%s: No versions for exported symbols."
1792 " Tainting kernel.\n", mod->name);
1793 add_taint(TAINT_FORCED_MODULE);
1797 /* Now do relocations. */
1798 for (i = 1; i < hdr->e_shnum; i++) {
1799 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1800 unsigned int info = sechdrs[i].sh_info;
1802 /* Not a valid relocation section? */
1803 if (info >= hdr->e_shnum)
1806 /* Don't bother with non-allocated sections */
1807 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1810 if (sechdrs[i].sh_type == SHT_REL)
1811 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1812 else if (sechdrs[i].sh_type == SHT_RELA)
1813 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1819 /* Find duplicate symbols */
1820 err = verify_export_symbols(mod);
1825 /* Set up and sort exception table */
1826 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1827 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1828 sort_extable(extable, extable + mod->num_exentries);
1830 /* Finally, copy percpu area over. */
1831 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1832 sechdrs[pcpuindex].sh_size);
1834 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1836 err = module_finalize(hdr, sechdrs, mod);
1840 /* flush the icache in correct context */
1845 * Flush the instruction cache, since we've played with text.
1846 * Do it before processing of module parameters, so the module
1847 * can provide parameter accessor functions of its own.
1849 if (mod->module_init)
1850 flush_icache_range((unsigned long)mod->module_init,
1851 (unsigned long)mod->module_init
1853 flush_icache_range((unsigned long)mod->module_core,
1854 (unsigned long)mod->module_core + mod->core_size);
1860 err = obsolete_params(mod->name, mod->args,
1861 (struct obsolete_modparm *)
1862 sechdrs[obsparmindex].sh_addr,
1863 sechdrs[obsparmindex].sh_size
1864 / sizeof(struct obsolete_modparm),
1866 (char *)sechdrs[strindex].sh_addr);
1868 printk(KERN_WARNING "%s: Ignoring new-style "
1869 "parameters in presence of obsolete ones\n",
1872 /* Size of section 0 is 0, so this works well if no params */
1873 err = parse_args(mod->name, mod->args,
1874 (struct kernel_param *)
1875 sechdrs[setupindex].sh_addr,
1876 sechdrs[setupindex].sh_size
1877 / sizeof(struct kernel_param),
1883 err = mod_sysfs_setup(mod,
1884 (struct kernel_param *)
1885 sechdrs[setupindex].sh_addr,
1886 sechdrs[setupindex].sh_size
1887 / sizeof(struct kernel_param));
1890 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1892 /* Get rid of temporary copy */
1899 module_arch_cleanup(mod);
1901 module_unload_free(mod);
1902 module_free(mod, mod->module_init);
1904 module_free(mod, mod->module_core);
1907 percpu_modfree(percpu);
1912 return ERR_PTR(err);
1915 printk(KERN_ERR "Module len %lu truncated\n", len);
1921 * link the module with the whole machine is stopped with interrupts off
1922 * - this defends against kallsyms not taking locks
1924 static int __link_module(void *_mod)
1926 struct module *mod = _mod;
1927 list_add(&mod->list, &modules);
1931 /* This is where the real work happens */
1933 sys_init_module(void __user *umod,
1935 const char __user *uargs)
1940 /* Must have permission */
1941 if (!capable(CAP_SYS_MODULE))
1944 /* Only one module load at a time, please */
1945 if (down_interruptible(&module_mutex) != 0)
1948 /* Do all the hard work */
1949 mod = load_module(umod, len, uargs);
1952 return PTR_ERR(mod);
1955 /* Now sew it into the lists. They won't access us, since
1956 strong_try_module_get() will fail. */
1957 stop_machine_run(__link_module, mod, NR_CPUS);
1959 /* Drop lock so they can recurse */
1962 down(¬ify_mutex);
1963 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1966 /* Start the module */
1967 if (mod->init != NULL)
1970 /* Init routine failed: abort. Try to protect us from
1971 buggy refcounters. */
1972 mod->state = MODULE_STATE_GOING;
1973 synchronize_sched();
1975 printk(KERN_ERR "%s: module is now stuck!\n",
1979 down(&module_mutex);
1986 /* Now it's a first class citizen! */
1987 down(&module_mutex);
1988 mod->state = MODULE_STATE_LIVE;
1989 /* Drop initial reference. */
1991 module_free(mod, mod->module_init);
1992 mod->module_init = NULL;
1994 mod->init_text_size = 0;
2000 static inline int within(unsigned long addr, void *start, unsigned long size)
2002 return ((void *)addr >= start && (void *)addr < start + size);
2005 #ifdef CONFIG_KALLSYMS
2007 * This ignores the intensely annoying "mapping symbols" found
2008 * in ARM ELF files: $a, $t and $d.
2010 static inline int is_arm_mapping_symbol(const char *str)
2012 return str[0] == '$' && strchr("atd", str[1])
2013 && (str[2] == '\0' || str[2] == '.');
2016 static const char *get_ksymbol(struct module *mod,
2018 unsigned long *size,
2019 unsigned long *offset)
2021 unsigned int i, best = 0;
2022 unsigned long nextval;
2024 /* At worse, next value is at end of module */
2025 if (within(addr, mod->module_init, mod->init_size))
2026 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2028 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2030 /* Scan for closest preceeding symbol, and next symbol. (ELF
2031 starts real symbols at 1). */
2032 for (i = 1; i < mod->num_symtab; i++) {
2033 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2036 /* We ignore unnamed symbols: they're uninformative
2037 * and inserted at a whim. */
2038 if (mod->symtab[i].st_value <= addr
2039 && mod->symtab[i].st_value > mod->symtab[best].st_value
2040 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2041 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2043 if (mod->symtab[i].st_value > addr
2044 && mod->symtab[i].st_value < nextval
2045 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2046 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2047 nextval = mod->symtab[i].st_value;
2053 *size = nextval - mod->symtab[best].st_value;
2054 *offset = addr - mod->symtab[best].st_value;
2055 return mod->strtab + mod->symtab[best].st_name;
2058 /* For kallsyms to ask for address resolution. NULL means not found.
2059 We don't lock, as this is used for oops resolution and races are a
2061 const char *module_address_lookup(unsigned long addr,
2062 unsigned long *size,
2063 unsigned long *offset,
2068 list_for_each_entry(mod, &modules, list) {
2069 if (within(addr, mod->module_init, mod->init_size)
2070 || within(addr, mod->module_core, mod->core_size)) {
2071 *modname = mod->name;
2072 return get_ksymbol(mod, addr, size, offset);
2078 struct module *module_get_kallsym(unsigned int symnum,
2079 unsigned long *value,
2085 down(&module_mutex);
2086 list_for_each_entry(mod, &modules, list) {
2087 if (symnum < mod->num_symtab) {
2088 *value = mod->symtab[symnum].st_value;
2089 *type = mod->symtab[symnum].st_info;
2091 mod->strtab + mod->symtab[symnum].st_name,
2096 symnum -= mod->num_symtab;
2102 static unsigned long mod_find_symname(struct module *mod, const char *name)
2106 for (i = 0; i < mod->num_symtab; i++)
2107 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2108 mod->symtab[i].st_info != 'U')
2109 return mod->symtab[i].st_value;
2113 /* Look for this name: can be of form module:name. */
2114 unsigned long module_kallsyms_lookup_name(const char *name)
2118 unsigned long ret = 0;
2120 /* Don't lock: we're in enough trouble already. */
2121 if ((colon = strchr(name, ':')) != NULL) {
2123 if ((mod = find_module(name)) != NULL)
2124 ret = mod_find_symname(mod, colon+1);
2127 list_for_each_entry(mod, &modules, list)
2128 if ((ret = mod_find_symname(mod, name)) != 0)
2133 #endif /* CONFIG_KALLSYMS */
2135 /* Called by the /proc file system to return a list of modules. */
2136 static void *m_start(struct seq_file *m, loff_t *pos)
2138 struct list_head *i;
2141 down(&module_mutex);
2142 list_for_each(i, &modules) {
2151 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2153 struct list_head *i = p;
2155 if (i->next == &modules)
2160 static void m_stop(struct seq_file *m, void *p)
2165 static int m_show(struct seq_file *m, void *p)
2167 struct module *mod = list_entry(p, struct module, list);
2168 seq_printf(m, "%s %lu",
2169 mod->name, mod->init_size + mod->core_size);
2170 print_unload_info(m, mod);
2172 /* Informative for users. */
2173 seq_printf(m, " %s",
2174 mod->state == MODULE_STATE_GOING ? "Unloading":
2175 mod->state == MODULE_STATE_COMING ? "Loading":
2177 /* Used by oprofile and other similar tools. */
2178 seq_printf(m, " 0x%p", mod->module_core);
2180 seq_printf(m, "\n");
2184 /* Format: modulename size refcount deps address
2186 Where refcount is a number or -, and deps is a comma-separated list
2189 struct seq_operations modules_op = {
2196 /* Given an address, look for it in the module exception tables. */
2197 const struct exception_table_entry *search_module_extables(unsigned long addr)
2199 unsigned long flags;
2200 const struct exception_table_entry *e = NULL;
2203 spin_lock_irqsave(&modlist_lock, flags);
2204 list_for_each_entry(mod, &modules, list) {
2205 if (mod->num_exentries == 0)
2208 e = search_extable(mod->extable,
2209 mod->extable + mod->num_exentries - 1,
2214 spin_unlock_irqrestore(&modlist_lock, flags);
2216 /* Now, if we found one, we are running inside it now, hence
2217 we cannot unload the module, hence no refcnt needed. */
2221 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2222 struct module *__module_text_address(unsigned long addr)
2226 list_for_each_entry(mod, &modules, list)
2227 if (within(addr, mod->module_init, mod->init_text_size)
2228 || within(addr, mod->module_core, mod->core_text_size))
2233 struct module *module_text_address(unsigned long addr)
2236 unsigned long flags;
2238 spin_lock_irqsave(&modlist_lock, flags);
2239 mod = __module_text_address(addr);
2240 spin_unlock_irqrestore(&modlist_lock, flags);
2245 /* Don't grab lock, we're oopsing. */
2246 void print_modules(void)
2250 printk("Modules linked in:");
2251 list_for_each_entry(mod, &modules, list)
2252 printk(" %s", mod->name);
2256 void module_add_driver(struct module *mod, struct device_driver *drv)
2261 /* Don't check return code; this call is idempotent */
2262 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2264 EXPORT_SYMBOL(module_add_driver);
2266 void module_remove_driver(struct device_driver *drv)
2270 sysfs_remove_link(&drv->kobj, "module");
2272 EXPORT_SYMBOL(module_remove_driver);
2274 #ifdef CONFIG_MODVERSIONS
2275 /* Generate the signature for struct module here, too, for modversions. */
2276 void struct_module(struct module *mod) { return; }
2277 EXPORT_SYMBOL(struct_module);